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