control_quirk seems to be evil and our buf must be "80 06 00 01 00 00 40 00" for...
[ppcskel.git] / usb / core / core.c
1 /*
2  * Copyright (c) 2006, Benedikt Sauter <sauter@ixbat.de>
3  * All rights reserved.
4  *
5  * Short descripton of file:
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions 
10  * are met:
11  *
12  *       * Redistributions of source code must retain the above copyright 
13  *               notice, this list of conditions and the following disclaimer.
14  *       * Redistributions in binary form must reproduce the above 
15  *               copyright notice, this list of conditions and the following 
16  *               disclaimer in the documentation and/or other materials provided 
17  *               with the distribution.
18  *       * Neither the name of the FH Augsburg nor the names of its 
19  *               contributors may be used to endorse or promote products derived 
20  *               from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 //#include <stdlib.h>
36 #include "core.h"
37 #include "../host/host.h"
38 #include "usb.h"
39 #include "../usbspec/usb11spec.h"
40 #include "../lib/list.h"
41 #include "../../malloc.h"
42 #include "../../bootmii_ppc.h" //printf
43 #include "../../string.h" //memset
44
45 /**
46  * Initialize USB stack.
47  */
48 void usb_init()
49 {
50         core.drivers = list_create();
51         core.devices = list_create();
52         core.nextaddress = 1;
53         hcdi_init();
54 }
55
56 /**
57  * Get next free usb device address.
58  */
59 u8 usb_next_address()
60 {
61         u8 addr = core.nextaddress;
62         core.nextaddress++;
63         return addr;
64 }
65
66
67 /**
68  * Call this function periodically for 
69  * control and transfer management.
70  */
71 void usb_periodic()
72 {
73         // call ever registered driver  
74         usb_driver *drv;
75         element *iterator = core.drivers->head;
76         while (iterator != NULL) {
77                 drv = (usb_driver *) iterator->data;
78                 drv->check();
79                 iterator = iterator->next;
80         }
81 }
82
83
84 /** 
85  * Enumerate new device and create data structures 
86  * for the core. usb_add_device expected that
87  * the device answers to address zero.
88  */
89 usb_device *usb_add_device()
90 {
91         usb_device *dev = (usb_device *) malloc(sizeof(usb_device));
92         dev->address = 0;
93         dev->bMaxPacketSize0 = 8;       /* send at first time only 8 bytes */
94
95         dev->epSize[0] = 64;
96         dev->epSize[1] = 64;
97         dev->epSize[2] = 64;
98
99         dev->epTogl[0] = 0;
100         dev->epTogl[1] = 0;
101         dev->epTogl[2] = 0;
102
103         char buf[64];
104         memset(buf, 0, sizeof(buf));
105
106         /* ask first 8 bytes of device descriptor with this special 
107          * GET Descriptor Request, when device address = 0
108          */
109
110         /*
111          * see page 253 in usb_20.pdf
112          * 
113          * bmRequestType = 0x80 = 10000000B
114          * bRequest = GET_DESCRIPTOR
115          * wValue = DEVICE (Descriptor Type)
116          * wIndex = 0
117          * wLength = 8 (in Bytes!?)
118          */
119         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE << 8, 0, 64, buf, 8, 0);
120         //usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE, 0, 64, buf, 8, 0);
121
122         /* 
123          * length (here =64) should be "number of byte to transfer", not 
124          * (as here) "number of bits to transfer.
125          * ?
126          */
127         //usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, 64, buf, 8, 0);
128         printf("===========\nafter usb control msg:\n");
129         hexdump(buf, sizeof(buf));
130
131 #if 0
132         u8 devdescr_size;
133         u8 address = usb_next_address();
134         dev->bMaxPacketSize0 = (u8) buf[7] ? (u8) buf[7] : 1; //dirty?  /* setup real ep0 fifo size */
135         devdescr_size = (u8) buf[0];    /* save real length of device descriptor */
136
137         /* define new adress */
138         usb_control_msg(dev, 0x00, SET_ADDRESS, address << 8, 0, 0, buf, 8, 0);
139         dev->address = address;
140
141         /* get complete device descriptor */
142         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, devdescr_size, buf, 8,
143                                                                         0);
144
145         /* save only really neccessary values for this small usbstack */
146         dev->bDeviceClass = (u8) buf[4];
147         dev->bDeviceSubClass = (u8) buf[5];
148         dev->bDeviceProtocoll = (u8) buf[6];
149         dev->idVendor = (u16) (buf[9] << 8) | (buf[8]);
150         dev->idProduct = (u16) (buf[11] << 8) | (buf[10]);
151         dev->bcdDevice = (u16) (buf[13] << 8) | (buf[12]);
152
153         printf( "bDeviceClass 0x%02X\n"
154                         "bDeviceSubClass 0x%02X\n"
155                         "bDeviceProtocoll 0x%02X\n"
156                         "idVendor 0x%04X\n"
157                         "idProduct 0x%04X\n"
158                         "bcdDevice 0x%04X\n", dev->bDeviceClass, 
159                         dev->bDeviceSubClass, dev->bDeviceProtocoll,
160                         dev->idVendor, dev->idProduct, dev->bcdDevice);
161         /* for lewurms keyboard it should be:
162          * bDeviceClass                 0
163          * bDeviceSubClass              0
164          * bDeviceClass                 0
165          * idVendor                                             0x049f
166          * idProduct                                    0x000e
167          * bcdDevice                                            1.00
168          */
169
170
171         // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit 
172         // entprechenden funktionen
173         // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
174
175         /* add device to device list */
176         element *tmp = (element *) malloc(sizeof(element));
177         tmp->data = (void *) dev;
178         list_add_tail(core.devices, tmp);
179
180         usb_probe_driver();
181 #endif
182
183         return dev;
184 }
185
186 /**
187  * Find currently detached device and remove
188  * data structures
189  */
190 u8 usb_remove_device(usb_device * dev)
191 {
192         // FIXME!!!! dieser quatsch ist nur temporaer
193         free(core.devices->head);
194         free(core.devices);
195         core.devices = list_create();
196         return 1;
197 }
198
199 /**
200  * Register new driver at usb stack.
201  */
202 u8 usb_register_driver(usb_driver * dev)
203 {
204         /* add driver to driver list */
205         element *tmp = (element *) malloc(sizeof(element));
206         tmp->data = (void *) dev;
207         tmp->next = NULL;
208         list_add_tail(core.drivers, tmp);
209
210
211         /** 
212          * first check to find a suitable device 
213          * (root hub drivers need this call here)
214          */
215         dev->probe();
216
217         return 1;
218 }
219
220
221 /**
222  * Call every probe function from every registered
223  * driver, to check if there is a valid driver
224  * for the new device.  
225  */
226 void usb_probe_driver()
227 {
228         // call ever registered driver  
229         usb_driver *drv;
230         element *iterator = core.drivers->head;
231         while (iterator != NULL) {
232                 drv = (usb_driver *) iterator->data;
233                 drv->probe();
234                 iterator = iterator->next;
235         }
236 }
237
238 /**
239  * Not implemented.
240  */
241 usb_irp *usb_get_irp()
242 {
243         return 0;
244 }
245
246 /**
247  * Not implemented.
248  */
249 u8 usb_remove_irp(usb_irp * irp)
250 {
251
252         return 1;
253 }
254
255 /**
256  * Takes usb_irp and split it into
257  * several usb packeges (SETUP,IN,OUT)
258  * In the usbstack they are transported with the
259  * usb_transfer_descriptor data structure.
260  */
261 u16 usb_submit_irp(usb_irp *irp)
262 {
263         usb_transfer_descriptor *td;
264         u8 runloop = 1;
265         u16 restlength = irp->len;
266         char *td_buf_ptr = irp->buffer;
267         char mybuf[64];
268
269         //u8 togl=irp->dev->epTogl[(irp->endpoint & 0x7F)];
270         u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
271         //u8 togl=0;
272
273         switch (irp->type) {
274         case USB_CTRL:
275
276                 /* alle requests mit dem gleichen algorithmus zerteilen
277                  * das einzige ist der spezielle get_Device_descriptor request
278                  * bei dem eine laenge von 64 angegeben ist.
279                  * wenn man an adresse 0 einen get_device_desciptor schickt
280                  * dann reichen die ersten 8 byte.
281                  */
282
283                         /***************** Setup Stage ***********************/
284                 td = usb_create_transfer_descriptor(irp);
285                 td->pid = USB_PID_SETUP;
286                 td->buffer = irp->buffer;
287
288                 /* control message are always 8 bytes */
289                 td->actlen = 8;
290                 memcpy(mybuf, td->buffer, td->actlen);
291
292                 togl = 0;
293                 td->togl = togl;                                                /* start with data0 */
294                 if (togl == 0)
295                         togl = 1;
296                 else
297                         togl = 0;
298                         /**** send token ****/
299                 printf("togl: %d\n", togl);
300                 hcdi_enqueue(td);
301 #if 0
302                 break;
303                 memcpy(td->buffer, mybuf, td->actlen);
304 #endif
305
306                         /***************** Data Stage ***********************/
307                         /**
308                          * You can see at bit 7 of bmRequestType if this stage is used,
309                          * default requests are always 8 byte greate, from
310                          * host to device. Stage 3 is only neccessary if the request
311                          * expected datas from the device.
312                          * bit7 - 1 = from device to host -> yes we need data stage
313                          * bit7 - 0 = from host to device -> no send zero packet
314                          *
315                          * nach einem setup token kann nur ein IN token in stage 3 folgen
316                          * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
317                          *
318                          *
319                          * bit7 = 1
320                          *      Device to Host
321                          *      - es kommen noch Daten mit PID_IN an
322                          *      - host beendet mit PID_OUT DATA1 Zero
323                          * bit7 - 0
324                          *      Host zu Device (wie set address)
325                          *      - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
326                          */
327                 usb_device_request *setup = (usb_device_request *) irp->buffer;
328                 u8 bmRequestType = setup->bmRequestType;
329
330                 if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */
331
332                         /* schleife die die tds generiert */
333                         while (runloop) {
334                                 td = usb_create_transfer_descriptor(irp);
335                                 td->actlen = irp->epsize;
336                                 /* stop loop if all bytes are send */
337                                 if (restlength <= irp->epsize) {
338                                         runloop = 0;
339                                         td->actlen = restlength;
340                                 }
341
342                                 td->buffer = td_buf_ptr;
343                                 /* move pointer for next packet */
344                                 td_buf_ptr = td_buf_ptr + irp->epsize;
345
346                                 td->pid = USB_PID_IN;
347                                 td->togl = togl;
348                                 if (togl == 0)
349                                         togl = 1;
350                                 else
351                                         togl = 0;
352
353                                 /* wenn device descriptor von adresse 0 angefragt wird werden nur
354                                  * die ersten 8 byte abgefragt
355                                  */
356                                 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue >> 8) == 1
357                                                 && td->devaddress == 0) {
358                                         runloop = 0;                                    /* stop loop */
359                                 }
360
361                                         /**** send token ****/
362                                 printf("togl: %d\n", togl);
363                                 hcdi_enqueue(td);
364
365                                 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
366                                 restlength = restlength - irp->epsize;
367                         }
368                 }
369
370
371                         /***************** Status Stage ***********************/
372                 /* Zero packet for end */
373                 td = usb_create_transfer_descriptor(irp);
374                 td->togl = 1;                                                           /* zero data packet = always DATA1 packet */
375                 td->actlen = 0;
376                 td->buffer = NULL;
377
378                         /**
379                          * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
380                          * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
381                          */
382                 if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */
383                         td->pid = USB_PID_OUT;
384                 } else {
385                         td->pid = USB_PID_IN;
386                 }
387                         /**** send token ****/
388                 printf("togl: %d\n", togl);
389                 hcdi_enqueue(td);
390                 free(td);
391
392
393                 break;
394         case USB_BULK:
395
396                 core.stdout("bulk\r\n");
397                 //u8 runloop=1;
398                 //u16 restlength = irp->len;
399                 //char * td_buf_ptr=irp->buffer;
400
401                 /* schleife die die tds generiert */
402                 while (runloop) {
403
404                         td = usb_create_transfer_descriptor(irp);
405                         td->endpoint = td->endpoint & 0x7F;                             /* clear direction bit */
406
407                         /* max packet size for given endpoint */
408                         td->actlen = irp->epsize;
409
410                         /* Generate In Packet  */
411                         if (irp->endpoint & 0x80)
412                                 td->pid = USB_PID_IN;
413                         else
414                                 /* Generate Out Packet */
415                                 td->pid = USB_PID_OUT;
416
417                         /* stop loop if all bytes are send */
418                         if (restlength <= irp->epsize) {
419                                 runloop = 0;
420                                 td->actlen = restlength;
421                         }
422
423                         td->buffer = td_buf_ptr;
424                         /* move pointer for next packet */
425                         td_buf_ptr = td_buf_ptr + irp->epsize;
426
427                         td->togl = togl;
428                         if (togl == 0)
429                                 togl = 1;
430                         else
431                                 togl = 0;
432                                 /**** send token ****/
433                         printf("togl: %d\n", togl);
434                         hcdi_enqueue(td);
435                         free(td);
436                 }
437                 /* next togl */
438                 //if(td->pid == USB_PID_OUT) {
439                 //if(togl==0) togl=1; else togl=0;
440                 //}
441                 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
442
443                 break;
444         }
445
446         return 1;
447 }
448
449
450
451 /** 
452  * Create a transfer descriptor with an parent irp.
453  */
454 usb_transfer_descriptor *usb_create_transfer_descriptor(usb_irp * irp)
455 {
456         usb_transfer_descriptor *td =
457                         (usb_transfer_descriptor *) malloc(sizeof(usb_transfer_descriptor));
458
459         td->devaddress = irp->dev->address;
460         td->endpoint = irp->endpoint;
461         td->iso = 0;
462         td->state = USB_TRANSFER_DESCR_NONE;
463         td->maxp = irp->epsize;
464
465         return td;
466 }
467