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