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