@get_descriptor|String: disable control quirk *sigh* and keep in mind to do not reque...
[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         /* send at first time only 8 bytes */
94         dev->bMaxPacketSize0 = 8;
95
96         dev->epSize[0] = 64;
97         dev->epSize[1] = 64;
98         dev->epSize[2] = 64;
99
100         dev->epTogl[0] = 0;
101         dev->epTogl[1] = 0;
102         dev->epTogl[2] = 0;
103
104         char buf[64];
105         memset(buf, 0, sizeof(buf));
106
107         /* ask first 8 bytes of device descriptor with this special 
108          * GET Descriptor Request, when device address = 0
109          */
110
111         /*
112          * see page 253 in usb_20.pdf
113          * 
114          * bmRequestType = 0x80 = 10000000B
115          * bRequest = GET_DESCRIPTOR
116          * wValue = DEVICE (Descriptor Type)
117          * wIndex = 0
118          * wLength = 64 // in fact just 8 bytes
119          */
120         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE << 8, 0, 64, buf, 8, 0);
121
122         printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
123         hexdump(buf, sizeof(buf));
124
125         u8 devdescr_size;
126         u8 address = usb_next_address();
127
128         /* setup real ep0 fifo size */
129         dev->bMaxPacketSize0 = (u8) buf[7];
130         if(!(u8)buf[7]) {
131                 printf("FU\n");
132                 return (void*)1;
133         }
134
135         /* save real length of device descriptor */
136         devdescr_size = (u8) buf[0];
137
138         /* define new adress */
139         usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 8, 0);
140         dev->address = address;
141         printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
142         hexdump(buf, sizeof(buf));
143         printf("address: %d\n", address);
144
145
146         /* get complete device descriptor */
147         memset(buf, 0, 64);
148         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE<<8, 0, devdescr_size, buf, 8, 0);
149
150         printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
151         hexdump(buf, sizeof(buf));
152
153         /* save only really neccessary values for this small usbstack */
154         dev->bDeviceClass = (u8) buf[4];
155         dev->bDeviceSubClass = (u8) buf[5];
156         dev->bDeviceProtocoll = (u8) buf[6];
157         dev->idVendor = (u16) (buf[9] << 8) | (buf[8]);
158         dev->idProduct = (u16) (buf[11] << 8) | (buf[10]);
159         dev->bcdDevice = (u16) (buf[13] << 8) | (buf[12]);
160
161         printf( "bDeviceClass 0x%02X\n"
162                         "bDeviceSubClass 0x%02X\n"
163                         "bDeviceProtocoll 0x%02X\n"
164                         "idVendor 0x%04X\n"
165                         "idProduct 0x%04X\n"
166                         "bcdDevice 0x%04X\n", dev->bDeviceClass, 
167                         dev->bDeviceSubClass, dev->bDeviceProtocoll,
168                         dev->idVendor, dev->idProduct, dev->bcdDevice);
169
170 #if 1
171         memset(buf, 0, 64);
172         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8)|2, 0, 0x1a, buf, 8, 0);
173         hexdump(buf, sizeof(buf));
174         printf("String Descriptor [1]: ");
175         u8 i;
176         for (i=2; i<buf[0]; i+=2)
177                 printf("%c", buf[i]);
178         printf("\n");
179 #endif
180
181         /*
182         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8) | 2, 0, 0x20, buf, 8, 0);
183         printf("String Descriptor [2]: ");
184         for (i=2; i<buf[0]; i+=2)
185                 printf("%c", buf[i]);
186         printf("\n");
187         */
188
189         // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit 
190         // entprechenden funktionen
191         // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
192
193 #if 0
194         /* add device to device list */
195         element *tmp = (element *) malloc(sizeof(element));
196         tmp->data = (void *) dev;
197         list_add_tail(core.devices, tmp);
198
199         usb_probe_driver();
200 #endif
201
202         return dev;
203 }
204
205 /**
206  * Find currently detached device and remove
207  * data structures
208  */
209 u8 usb_remove_device(usb_device * dev)
210 {
211         // FIXME!!!! dieser quatsch ist nur temporaer
212         free(core.devices->head);
213         free(core.devices);
214         core.devices = list_create();
215         return 1;
216 }
217
218 /**
219  * Register new driver at usb stack.
220  */
221 u8 usb_register_driver(usb_driver * dev)
222 {
223         /* add driver to driver list */
224         element *tmp = (element *) malloc(sizeof(element));
225         tmp->data = (void *) dev;
226         tmp->next = NULL;
227         list_add_tail(core.drivers, tmp);
228
229
230         /** 
231          * first check to find a suitable device 
232          * (root hub drivers need this call here)
233          */
234         dev->probe();
235
236         return 1;
237 }
238
239
240 /**
241  * Call every probe function from every registered
242  * driver, to check if there is a valid driver
243  * for the new device.  
244  */
245 void usb_probe_driver()
246 {
247         // call ever registered driver  
248         usb_driver *drv;
249         element *iterator = core.drivers->head;
250         while (iterator != NULL) {
251                 drv = (usb_driver *) iterator->data;
252                 drv->probe();
253                 iterator = iterator->next;
254         }
255 }
256
257 /**
258  * Not implemented.
259  */
260 usb_irp *usb_get_irp()
261 {
262         return 0;
263 }
264
265 /**
266  * Not implemented.
267  */
268 u8 usb_remove_irp(usb_irp * irp)
269 {
270
271         return 1;
272 }
273
274 /**
275  * Takes usb_irp and split it into
276  * several usb packeges (SETUP,IN,OUT)
277  * In the usbstack they are transported with the
278  * usb_transfer_descriptor data structure.
279  */
280 u16 usb_submit_irp(usb_irp *irp)
281 {
282         usb_transfer_descriptor *td;
283         u8 runloop = 1;
284         u16 restlength = irp->len;
285         char *td_buf_ptr = irp->buffer;
286         char mybuf[64];
287
288         u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
289
290         switch (irp->type) {
291         case USB_CTRL:
292                 /* alle requests mit dem gleichen algorithmus zerteilen
293                  * das einzige ist der spezielle get_Device_descriptor request
294                  * bei dem eine laenge von 64 angegeben ist.
295                  * wenn man an adresse 0 einen get_device_desciptor schickt
296                  * dann reichen die ersten 8 byte.
297                  */
298
299                 /***************** Setup Stage ***********************/
300                 td = usb_create_transfer_descriptor(irp);
301                 td->pid = USB_PID_SETUP;
302                 td->buffer = irp->buffer;
303
304                 /* control message are always 8 bytes */
305                 td->actlen = 8;
306
307                 togl = 0;
308                 /* start with data0 */
309                 td->togl = togl;
310                 togl = togl ? 0 : 1;
311
312                 /**** send token ****/
313                 hcdi_enqueue(td);
314
315                 /***************** Data Stage ***********************/
316                 /**
317                  * You can see at bit 7 of bmRequestType if this stage is used,
318                  * default requests are always 8 byte greate, from
319                  * host to device. Stage 3 is only neccessary if the request
320                  * expected datas from the device.
321                  * bit7 - 1 = from device to host -> yes we need data stage
322                  * bit7 - 0 = from host to device -> no send zero packet
323                  *
324                  * nach einem setup token kann nur ein IN token in stage 3 folgen
325                  * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
326                  *
327                  *
328                  * bit7 = 1
329                  *      Device to Host
330                  *      - es kommen noch Daten mit PID_IN an
331                  *      - host beendet mit PID_OUT DATA1 Zero
332                  * bit7 - 0
333                  *      Host zu Device (wie set address)
334                  *      - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
335                  */
336                 memcpy(mybuf, irp->buffer, td->actlen);
337                 usb_device_request *setup = (usb_device_request *) mybuf;
338                 u8 bmRequestType = setup->bmRequestType;
339                 free(td);
340
341                 /* check bit 7 of bmRequestType */
342                 if (bmRequestType & 0x80) { 
343                         /* schleife die die tds generiert */
344                         while (runloop && (restlength > 0)) {
345                                 printf("restlength: %d\t irp->epsize: %d\n", restlength, irp->epsize);
346                                 td = usb_create_transfer_descriptor(irp);
347                                 td->actlen = irp->epsize;
348                                 /* stop loop if all bytes are send */
349                                 if (restlength < irp->epsize) {
350                                         runloop = 0;
351                                         td->actlen = restlength;
352                                 }
353
354                                 td->buffer = td_buf_ptr;
355                                 /* move pointer for next packet */
356                                 td_buf_ptr += irp->epsize;
357
358                                 td->pid = USB_PID_IN;
359                                 td->togl = togl;
360                                 togl = togl ? 0 : 1;
361
362                                 /* wenn device descriptor von adresse 0 angefragt wird werden nur
363                                  * die ersten 8 byte abgefragt
364                                  */
365                                 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
366                                                 && td->devaddress == 0) {
367                                         /* stop loop */
368                                         runloop = 0;
369                                 }
370
371                                 /**** send token ****/
372                                 hcdi_enqueue(td);
373
374                                 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
375                                 restlength = restlength - irp->epsize;
376                                 free(td);
377                         }
378                 }
379
380
381                 /***************** Status Stage ***********************/
382                 /* Zero packet for end */
383                 td = usb_create_transfer_descriptor(irp);
384                 td->togl = 1;                                                           /* zero data packet = always DATA1 packet */
385                 td->actlen = 0;
386                 td->buffer = NULL;
387
388                 /**
389                  * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
390                  * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
391                  */
392                 /* check bit 7 of bmRequestType */
393                 if (bmRequestType & 0x80) {
394                         td->pid = USB_PID_OUT;
395                 } else {
396                         td->pid = USB_PID_IN;
397                 }
398                 /**** send token ****/
399                 hcdi_enqueue(td);
400                 free(td);
401                 break;
402
403         case USB_BULK:
404                 core.stdout("bulk\r\n");
405                 //u8 runloop=1;
406                 //u16 restlength = irp->len;
407                 //char * td_buf_ptr=irp->buffer;
408
409                 /* schleife die die tds generiert */
410                 while (runloop) {
411
412                         td = usb_create_transfer_descriptor(irp);
413                         td->endpoint = td->endpoint & 0x7F;                             /* clear direction bit */
414
415                         /* max packet size for given endpoint */
416                         td->actlen = irp->epsize;
417
418                         /* Generate In Packet  */
419                         if (irp->endpoint & 0x80)
420                                 td->pid = USB_PID_IN;
421                         else
422                                 /* Generate Out Packet */
423                                 td->pid = USB_PID_OUT;
424
425                         /* stop loop if all bytes are send */
426                         if (restlength <= irp->epsize) {
427                                 runloop = 0;
428                                 td->actlen = restlength;
429                         }
430
431                         td->buffer = td_buf_ptr;
432                         /* move pointer for next packet */
433                         td_buf_ptr = td_buf_ptr + irp->epsize;
434
435                         td->togl = togl;
436                         if (togl == 0)
437                                 togl = 1;
438                         else
439                                 togl = 0;
440                                 /**** send token ****/
441                         hcdi_enqueue(td);
442                         free(td);
443                 }
444                 /* next togl */
445                 //if(td->pid == USB_PID_OUT) {
446                 //if(togl==0) togl=1; else togl=0;
447                 //}
448                 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
449
450                 break;
451         }
452         hcdi_fire();
453
454         return 1;
455 }
456
457
458
459 /** 
460  * Create a transfer descriptor with an parent irp.
461  */
462 usb_transfer_descriptor *usb_create_transfer_descriptor(usb_irp * irp)
463 {
464         usb_transfer_descriptor *td =
465                         (usb_transfer_descriptor *) malloc(sizeof(usb_transfer_descriptor));
466
467         td->devaddress = irp->dev->address;
468         td->endpoint = irp->endpoint;
469         td->iso = 0;
470         td->state = USB_TRANSFER_DESCR_NONE;
471         td->maxp = irp->epsize;
472
473         return td;
474 }
475