2 * Copyright (c) 2006, Benedikt Sauter <sauter@ixbat.de>
5 * Short descripton of file:
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
37 #include "../host/host.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
46 * Initialize USB stack.
48 void usb_init(u32 reg)
50 core.drivers = list_create();
51 core.devices = list_create();
57 * Get next free usb device address.
61 u8 addr = core.nextaddress;
68 * Call this function periodically for
69 * control and transfer management.
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;
79 iterator = iterator->next;
85 * Enumerate new device and create data structures
86 * for the core. usb_add_device expected that
87 * the device answers to address zero.
89 struct usb_device *usb_add_device(u8 lowspeed, u32 reg)
91 struct usb_device *dev = (struct usb_device *) malloc(sizeof(struct usb_device));
92 dev->conf = (struct usb_conf *) malloc(sizeof(struct usb_conf));
94 dev->fullspeed = lowspeed ? 0 : 1;
95 /* send at first time only 8 bytes for lowspeed devices
96 * 64 bytes for fullspeed
98 dev->bMaxPacketSize0 = lowspeed ? 8 : 64;
110 ret = usb_get_desc_dev_simple(dev);
117 printf("lololol PADDING WTF :O lolololololo \n");
118 printf("lololololool PADDING WTF :O lolololololo \n");
119 printf("lolololool PADDING WTF :O lololoololo \n");
120 printf("lololololool PADDING WTF :O lolololololo \n");
121 printf("lolololool PADDING WTF :O lolololololo \n");
122 printf("lololololool PADDING WTF :O lolololololo \n");
123 printf("lollllool PADDING WTF :O lolololololo \n");
125 u8 address = usb_next_address();
126 ret = usb_set_address(dev, address);
127 dev->address = address;
128 printf("set address to %d\n", dev->address);
130 /* get device descriptor&co */
131 ret = usb_get_desc_dev(dev);
135 /* print device info */
139 #define GET_PROTOCOL 0x03
140 #define SET_PROTOCOL 0x0b
147 /* select configuration */
149 ret = usb_set_configuration(dev, 1);
150 printf("=============\nusb_set_configuration(ret: %d): %d\n", ret, dev->conf->bConfigurationValue);
154 printf("=============\nusb_get_configuration: %d (should be 1 here)\n", usb_get_configuration(dev));
159 memset(buf, 0, 0xff);
160 usb_control_msg(dev, (1<<5)|1, SET_CONFIGURATION, 0x200, 0x0, 0x0, buf, 0);
161 printf("wtf request\n");
162 hexdump((void*)buf, 0x8);
168 /* see p48 in HID spec */
170 usb_control_msg(dev, SETit, SET_PROTOCOL, 0 /*boot protocol*/, 0 /*interface number*/, 0 /*length*/, buf, 0);
171 printf("=============\nhid_set_protocol %d\n", 0);
172 hexdump((void*)buf, 8);
176 usb_control_msg(dev, GETit, GET_PROTOCOL, 0, 0, 4, buf, 0);
177 printf("=============\nusb_get_protocol: %d\n", buf[0]);
178 hexdump((void*)buf, 8);
182 /* I just don't know why on some devices
183 * {s,g}et_{configuration,interface} won't work.
184 * may the setter works and getter are poorly implemented?
185 * however, I try here some interrupt inputs, assuming
186 * the setters are fine*/
189 s8 epnum = dev->conf->intf->endp->bEndpointAddress & 0xf;
190 printf("epnum: 0x%04X\n", epnum);
193 (void) usb_interrupt_read(dev, epnum, buf, 8, 0);
194 printf("============\nusb_interrupt_read:\n");
195 hexdump((void*)buf, 8);
201 /* add device to device list */
202 element *tmp = (element *) malloc(sizeof(element));
203 tmp->data = (void *) dev;
204 list_add_tail(core.devices, tmp);
212 void lsusb(struct usb_device *dev)
214 printf("=== Device Descriptor === \n");
215 printf("bLength 0x%02X\n", dev->bLength);
216 printf("bDescriptorType 0x%02X\n", dev->bDeviceClass);
217 printf("bcdUSB 0x%04X\n", dev->bcdUSB);
218 printf("bDeviceClass 0x%02X\n", dev->bDeviceClass);
219 printf("bDeviceSubClass 0x%02X\n", dev->bDeviceSubClass);
220 printf("bDeviceProtocoll 0x%02X\n", dev->bDeviceProtocoll);
221 printf("bMaxPacketSize 0x%02X\n", dev->bMaxPacketSize0);
222 printf("idVendor 0x%04X\n", dev->idVendor);
223 printf("idProduct 0x%04X\n", dev->idProduct);
224 printf("bcdDevice 0x%04X\n", dev->bcdDevice);
225 printf("iManufacturer(0x%02X): \"%s\"\n", dev->iManufacturer, dev->iManufacturer ? usb_get_string_simple(dev, dev->iManufacturer) : "no String");
226 printf("iProduct(0x%02X): \"%s\"\n", dev->iProduct, dev->iProduct ? usb_get_string_simple(dev, dev->iProduct) : "no String");
227 printf("iSerialNumber(0x%02X): \"%s\"\n", dev->iSerialNumber, dev->iSerialNumber ? usb_get_string_simple(dev, dev->iSerialNumber) : "no String");
228 printf("bNumConfigurations 0x%02X\n", dev->bNumConfigurations);
231 struct usb_conf *conf = dev->conf;
232 for(c=0; c <= dev->bNumConfigurations; c++) {
233 printf(" === Configuration Descriptor %d ===\n", c+1);
234 printf(" bLength 0x%02X\n", conf->bLength);
235 printf(" bDescriptorType 0x%02X\n", conf->bDescriptorType);
236 printf(" wTotalLength 0x%04X\n", conf->wTotalLength);
237 printf(" bNumInterfaces 0x%02X\n", conf->bNumInterfaces);
238 printf(" bConfigurationValue 0x%02X\n", conf->bConfigurationValue);
239 printf(" iConfiguration (0x%02X): \"%s\"\n", conf->iConfiguration, conf->iConfiguration ? usb_get_string_simple(dev, conf->iConfiguration) : "no String");
240 printf(" bmAttributes 0x%02X\n", conf->bmAttributes);
241 printf(" bMaxPower 0x%02X\n", conf->bMaxPower);
243 struct usb_intf *ifs = conf->intf;
244 for(i=1; i <= conf->bNumInterfaces; i++) {
245 printf(" === Interface Descriptor %d ===\n", i);
246 printf(" bLength 0x%02X\n", ifs->bLength);
247 printf(" bDescriptorType 0x%02X\n", ifs->bDescriptorType);
248 printf(" bInterfaceNumber 0x%02X\n", ifs->bInterfaceNumber);
249 printf(" bAlternateSetting 0x%02X\n", ifs->bAlternateSetting);
250 printf(" bNumEndpoints 0x%02X\n", ifs->bNumEndpoints);
251 printf(" bInterfaceClass 0x%02X\n", ifs->bInterfaceClass);
252 printf(" bInterfaceSubClass 0x%02X\n", ifs->bInterfaceSubClass);
253 printf(" bInterfaceProtocol 0x%02X\n", ifs->bInterfaceProtocol);
254 printf(" iInterface (0x%02X): \"%s\"\n", ifs->iInterface, ifs->iInterface ? usb_get_string_simple(dev, ifs->iInterface) : "no String");
256 struct usb_endp *ed = ifs->endp;
257 for(e=1; e <= ifs->bNumEndpoints; e++) {
258 printf(" === Endpoint Descriptor %d ===\n", e);
259 printf(" bLength 0x%02X\n", ed->bLength);
260 printf(" bDescriptorType 0x%02X\n", ed->bDescriptorType);
261 printf(" bEndpointAddress 0x%02X\n", ed->bEndpointAddress);
262 printf(" bmAttributes 0x%02X\n", ed->bmAttributes);
263 printf(" wMaxPacketSize 0x%02X\n", ed->wMaxPacketSize);
264 printf(" bInterval 0x%02X\n", ed->bInterval);
277 * Find currently detached device and remove
280 u8 usb_remove_device(struct usb_device * dev)
282 // FIXME!!!! dieser quatsch ist nur temporaer
283 free(core.devices->head);
285 core.devices = list_create();
290 * Register new driver at usb stack.
292 u8 usb_register_driver(struct usb_driver * dev)
294 /* add driver to driver list */
295 struct element *tmp = (struct element *) malloc(sizeof(struct element));
296 tmp->data = (void *) dev;
298 list_add_tail(core.drivers, tmp);
302 * first check to find a suitable device
303 * (root hub drivers need this call here)
312 * Call every probe function from every registered
313 * driver, to check if there is a valid driver
314 * for the new device.
316 void usb_probe_driver()
318 // call ever registered driver
319 struct usb_driver *drv;
320 struct element *iterator = core.drivers->head;
321 while (iterator != NULL) {
322 drv = (struct usb_driver *) iterator->data;
324 iterator = iterator->next;
331 struct usb_irp *usb_get_irp()
339 u8 usb_remove_irp(struct usb_irp *irp)
346 * Takes usb_irp and split it into
347 * several usb packeges (SETUP,IN,OUT)
348 * In the usbstack they are transported with the
349 * usb_transfer_descriptor data structure.
351 u16 usb_submit_irp(struct usb_irp *irp)
353 struct usb_transfer_descriptor *td;
355 u16 restlength = irp->len;
356 u8 *td_buf_ptr = irp->buffer;
359 u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
363 /* alle requests mit dem gleichen algorithmus zerteilen
364 * das einzige ist der spezielle get_Device_descriptor request
365 * bei dem eine laenge von 64 angegeben ist.
366 * wenn man an adresse 0 einen get_device_desciptor schickt
367 * dann reichen die ersten 8 byte.
370 /***************** Setup Stage ***********************/
371 td = usb_create_transfer_descriptor(irp);
372 td->pid = USB_PID_SETUP;
373 td->buffer = irp->buffer;
375 /* control message are always 8 bytes */
379 /* start with data0 */
383 /**** send token ****/
384 hcdi_enqueue(td, irp->dev->ohci);
386 /***************** Data Stage ***********************/
388 * You can see at bit 7 of bmRequestType if this stage is used,
389 * default requests are always 8 byte greate, from
390 * host to device. Stage 3 is only neccessary if the request
391 * expected datas from the device.
392 * bit7 - 1 = from device to host -> yes we need data stage
393 * bit7 - 0 = from host to device -> no send zero packet
395 * nach einem setup token kann nur ein IN token in stage 3 folgen
396 * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
401 * - es kommen noch Daten mit PID_IN an
402 * - host beendet mit PID_OUT DATA1 Zero
404 * Host zu Device (wie set address)
405 * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
407 memcpy(mybuf, irp->buffer, td->actlen);
408 usb_device_request *setup = (usb_device_request *) mybuf;
409 u8 bmRequestType = setup->bmRequestType;
412 /* check bit 7 of bmRequestType */
413 if (bmRequestType & 0x80) {
414 /* schleife die die tds generiert */
415 while (runloop && (restlength > 0)) {
416 td = usb_create_transfer_descriptor(irp);
417 td->actlen = irp->epsize;
418 /* stop loop if all bytes are send */
419 if (restlength < irp->epsize) {
421 td->actlen = restlength;
424 td->buffer = td_buf_ptr;
425 /* move pointer for next packet */
426 td_buf_ptr += irp->epsize;
428 td->pid = USB_PID_IN;
432 /* wenn device descriptor von adresse 0 angefragt wird werden nur
433 * die ersten 8 byte abgefragt
435 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
436 && td->devaddress == 0) {
441 /**** send token ****/
442 hcdi_enqueue(td, irp->dev->ohci);
444 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
445 restlength = restlength - irp->epsize;
451 /***************** Status Stage ***********************/
452 /* Zero packet for end */
453 td = usb_create_transfer_descriptor(irp);
454 td->togl = 1; /* zero data packet = always DATA1 packet */
459 * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
460 * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
462 /* check bit 7 of bmRequestType */
463 if (bmRequestType & 0x80) {
464 td->pid = USB_PID_OUT;
466 td->pid = USB_PID_IN;
468 /**** send token ****/
469 hcdi_enqueue(td, irp->dev->ohci);
475 //u16 restlength = irp->len;
476 //char * td_buf_ptr=irp->buffer;
478 /* schleife die die tds generiert */
480 td = usb_create_transfer_descriptor(irp);
481 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
483 /* max packet size for given endpoint */
484 td->actlen = irp->epsize;
486 /* Generate In Packet */
487 if (irp->endpoint & 0x80)
488 td->pid = USB_PID_IN;
490 /* Generate Out Packet */
491 td->pid = USB_PID_OUT;
493 /* stop loop if all bytes are send */
494 if (restlength <= irp->epsize) {
496 td->actlen = restlength;
499 td->buffer = td_buf_ptr;
500 /* move pointer for next packet */
501 td_buf_ptr = td_buf_ptr + irp->epsize;
505 /**** send token ****/
506 hcdi_enqueue(td, irp->dev->ohci);
510 //if(td->pid == USB_PID_OUT) {
511 //if(togl==0) togl=1; else togl=0;
513 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
519 //u16 restlength = irp->len;
520 //char * td_buf_ptr=irp->buffer;
522 /* schleife die die tds generiert */
523 while (runloop && (restlength > 0)) {
524 td = usb_create_transfer_descriptor(irp);
525 /* max packet size for given endpoint */
526 td->actlen = irp->epsize;
528 td->pid = USB_PID_IN;
529 /* TODO: USB_PID_OUT */
531 /* stop loop if all bytes are send */
532 if (restlength < irp->epsize) {
534 td->actlen = restlength;
537 td->buffer = td_buf_ptr;
538 /* move pointer for next packet */
539 td_buf_ptr += irp->epsize;
544 /**** send token ****/
545 hcdi_enqueue(td, irp->dev->ohci);
546 restlength = restlength - irp->epsize;
550 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
552 hcdi_fire(irp->dev->ohci);
560 * Create a transfer descriptor with an parent irp.
562 struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
564 struct usb_transfer_descriptor *td =
565 (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
567 td->devaddress = irp->dev->address;
568 td->endpoint = irp->endpoint;
570 td->state = USB_TRANSFER_DESCR_NONE;
571 td->maxp = irp->epsize;
572 td->fullspeed = irp->dev->fullspeed;
573 td->type = irp->type;