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.
35 ppcskel - a Free Software replacement for the Nintendo/BroadOn bootloader.
38 Copyright (C) 2009 Bernhard Urban <lewurm@gmx.net>
39 Copyright (C) 2009 Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
41 # This code is licensed to you under the terms of the GNU GPL, version 2;
42 # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
46 #include "../host/host.h"
48 #include "../usbspec/usb11spec.h"
49 #include "../lib/list.h"
50 #include "../../malloc.h"
51 #include "../../bootmii_ppc.h" //printf
52 #include "../../string.h" //memset
55 * Initialize USB stack.
57 void usb_init(u32 reg)
59 core.drivers = list_create();
60 core.devices = list_create();
66 * Get next free usb device address.
70 u8 addr = core.nextaddress;
77 * Call this function periodically for
78 * control and transfer management.
82 // call ever registered driver
83 struct usb_driver *drv;
84 struct element *iterator = core.drivers->head;
85 while (iterator != NULL) {
86 drv = (struct usb_driver *) iterator->data;
88 iterator = iterator->next;
94 * Enumerate new device and create data structures
95 * for the core. usb_add_device expected that
96 * the device answers to address zero.
98 struct usb_device *usb_add_device(u8 lowspeed, u32 reg)
100 struct usb_device *dev = (struct usb_device *) malloc(sizeof(struct usb_device));
101 dev->conf = (struct usb_conf *) malloc(sizeof(struct usb_conf));
103 dev->fullspeed = lowspeed ? 0 : 1;
104 /* send at first time only 8 bytes for lowspeed devices
105 * 64 bytes for fullspeed
107 dev->bMaxPacketSize0 = lowspeed ? 8 : 64;
119 ret = usb_get_desc_dev_simple(dev);
126 volatile u8 wzf = 11;
128 printf("WTF WTF WTF WTF padding??? WTFWTF WTF\n");
129 printf("WTF WTF WTF WTF padding??? WTF WTF WTF\n");
130 printf("WTF TF WTF WTF padding??? WTF WTF WTF\n");
131 printf("WTF WTF TF WTF padding??? WTF WTWTF\n");
132 printf("TF WTF WTF WTF padding??? WTF WTF WTF\n");
133 printf("WTF WTF WTF WT padding??? WTF WF WTF\n");
134 printf("WTF WTF WTF WTF padding??? WTF WTF WTF\n");
135 printf("WTF WTF WTF WTF padding??? WT WTF WTF\n");
136 printf("WTF WTF WTF WTF pdding??? WTF WTF WTF\n");
137 printf("WTF WTF WTF WTF paddin??? WTF WTF WTF\n");
138 printf("WTF WTF WTF WTF padding??? WTF WTF WTF\n");
139 printf("WTF WTF WTF WTF padding?? WT WTF WTF\n");
140 printf("WTF WTF WTF WTF padding??? WTF WTF WTF\n");
141 printf("WTF WTF WTF WTF padding??? WTFWTF WTF\n");
144 u8 address = usb_next_address();
145 ret = usb_set_address(dev, address);
146 dev->address = address;
147 printf("set address to %d\n", dev->address);
149 /* get device descriptor&co */
150 ret = usb_get_desc_dev(dev);
154 /* print device info */
157 /* add device to device list */
158 struct element *tmp = (struct element *) malloc(sizeof(struct element));
159 tmp->data = (void *) dev;
160 list_add_tail(core.devices, tmp);
167 void lsusb(struct usb_device *dev)
169 printf("=== Device Descriptor === \n");
170 printf("bLength 0x%02X\n", dev->bLength);
171 printf("bDescriptorType 0x%02X\n", dev->bDeviceClass);
172 printf("bcdUSB 0x%04X\n", dev->bcdUSB);
173 printf("bDeviceClass 0x%02X\n", dev->bDeviceClass);
174 printf("bDeviceSubClass 0x%02X\n", dev->bDeviceSubClass);
175 printf("bDeviceProtocoll 0x%02X\n", dev->bDeviceProtocoll);
176 printf("bMaxPacketSize 0x%02X\n", dev->bMaxPacketSize0);
177 printf("idVendor 0x%04X\n", dev->idVendor);
178 printf("idProduct 0x%04X\n", dev->idProduct);
179 printf("bcdDevice 0x%04X\n", dev->bcdDevice);
180 printf("iManufacturer(0x%02X): \"%s\"\n", dev->iManufacturer, dev->iManufacturer ? usb_get_string_simple(dev, dev->iManufacturer) : "no String");
181 printf("iProduct(0x%02X): \"%s\"\n", dev->iProduct, dev->iProduct ? usb_get_string_simple(dev, dev->iProduct) : "no String");
182 printf("iSerialNumber(0x%02X): \"%s\"\n", dev->iSerialNumber, dev->iSerialNumber ? usb_get_string_simple(dev, dev->iSerialNumber) : "no String");
183 printf("bNumConfigurations 0x%02X\n", dev->bNumConfigurations);
186 struct usb_conf *conf = dev->conf;
187 for(c=0; c <= dev->bNumConfigurations; c++) {
188 printf(" === Configuration Descriptor %d ===\n", c+1);
189 printf(" bLength 0x%02X\n", conf->bLength);
190 printf(" bDescriptorType 0x%02X\n", conf->bDescriptorType);
191 printf(" wTotalLength 0x%04X\n", conf->wTotalLength);
192 printf(" bNumInterfaces 0x%02X\n", conf->bNumInterfaces);
193 printf(" bConfigurationValue 0x%02X\n", conf->bConfigurationValue);
194 printf(" iConfiguration (0x%02X): \"%s\"\n", conf->iConfiguration, conf->iConfiguration ? usb_get_string_simple(dev, conf->iConfiguration) : "no String");
195 printf(" bmAttributes 0x%02X\n", conf->bmAttributes);
196 printf(" bMaxPower 0x%02X\n", conf->bMaxPower);
198 struct usb_intf *ifs = conf->intf;
199 for(i=1; i <= conf->bNumInterfaces; i++) {
200 printf(" === Interface Descriptor %d ===\n", i);
201 printf(" bLength 0x%02X\n", ifs->bLength);
202 printf(" bDescriptorType 0x%02X\n", ifs->bDescriptorType);
203 printf(" bInterfaceNumber 0x%02X\n", ifs->bInterfaceNumber);
204 printf(" bAlternateSetting 0x%02X\n", ifs->bAlternateSetting);
205 printf(" bNumEndpoints 0x%02X\n", ifs->bNumEndpoints);
206 printf(" bInterfaceClass 0x%02X\n", ifs->bInterfaceClass);
207 printf(" bInterfaceSubClass 0x%02X\n", ifs->bInterfaceSubClass);
208 printf(" bInterfaceProtocol 0x%02X\n", ifs->bInterfaceProtocol);
209 printf(" iInterface (0x%02X): \"%s\"\n", ifs->iInterface, ifs->iInterface ? usb_get_string_simple(dev, ifs->iInterface) : "no String");
211 struct usb_endp *ed = ifs->endp;
212 for(e=1; e <= ifs->bNumEndpoints; e++) {
213 printf(" === Endpoint Descriptor %d ===\n", e);
214 printf(" bLength 0x%02X\n", ed->bLength);
215 printf(" bDescriptorType 0x%02X\n", ed->bDescriptorType);
216 printf(" bEndpointAddress 0x%02X\n", ed->bEndpointAddress);
217 printf(" bmAttributes 0x%02X\n", ed->bmAttributes);
218 printf(" wMaxPacketSize 0x%02X\n", ed->wMaxPacketSize);
219 printf(" bInterval 0x%02X\n", ed->bInterval);
232 * Find currently detached device and remove
235 u8 usb_remove_device(struct usb_device * dev)
237 // FIXME!!!! dieser quatsch ist nur temporaer
238 free(core.devices->head);
240 core.devices = list_create();
245 * Register new driver at usb stack.
247 u8 usb_register_driver(struct usb_driver *dev)
249 /* add driver to driver list */
250 struct element *tmp = (struct element *) malloc(sizeof(struct element));
251 tmp->data = (void *) dev;
253 list_add_tail(core.drivers, tmp);
256 * first check to find a suitable device
257 * (root hub drivers need this call here)
266 * Call every probe function from every registered
267 * driver, to check if there is a valid driver
268 * for the new device.
270 void usb_probe_driver()
272 // call ever registered driver
273 struct usb_driver *drv;
274 struct element *iterator = core.drivers->head;
275 while (iterator != NULL) {
276 drv = (struct usb_driver *) iterator->data;
278 iterator = iterator->next;
285 struct usb_irp *usb_get_irp()
293 u8 usb_remove_irp(struct usb_irp *irp)
300 * Takes usb_irp and split it into
301 * several usb packeges (SETUP,IN,OUT)
302 * In the usbstack they are transported with the
303 * usb_transfer_descriptor data structure.
305 u16 usb_submit_irp(struct usb_irp *irp)
307 struct usb_transfer_descriptor *td;
309 u16 restlength = irp->len;
310 u8 *td_buf_ptr = irp->buffer;
313 u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
317 /* alle requests mit dem gleichen algorithmus zerteilen
318 * das einzige ist der spezielle get_Device_descriptor request
319 * bei dem eine laenge von 64 angegeben ist.
320 * wenn man an adresse 0 einen get_device_desciptor schickt
321 * dann reichen die ersten 8 byte.
324 /***************** Setup Stage ***********************/
325 td = usb_create_transfer_descriptor(irp);
326 td->pid = USB_PID_SETUP;
327 td->buffer = irp->buffer;
329 /* control message are always 8 bytes */
333 /* start with data0 */
337 /**** send token ****/
338 hcdi_enqueue(td, irp->dev->ohci);
340 /***************** Data Stage ***********************/
342 * You can see at bit 7 of bmRequestType if this stage is used,
343 * default requests are always 8 byte greate, from
344 * host to device. Stage 3 is only neccessary if the request
345 * expected datas from the device.
346 * bit7 - 1 = from device to host -> yes we need data stage
347 * bit7 - 0 = from host to device -> no send zero packet
349 * nach einem setup token kann nur ein IN token in stage 3 folgen
350 * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
355 * - es kommen noch Daten mit PID_IN an
356 * - host beendet mit PID_OUT DATA1 Zero
358 * Host zu Device (wie set address)
359 * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
361 memcpy(mybuf, irp->buffer, td->actlen);
362 usb_device_request *setup = (usb_device_request *) mybuf;
363 u8 bmRequestType = setup->bmRequestType;
366 /* check bit 7 of bmRequestType */
367 if (bmRequestType & 0x80) {
368 /* schleife die die tds generiert */
369 while (runloop && (restlength > 0)) {
370 td = usb_create_transfer_descriptor(irp);
371 td->actlen = irp->epsize;
372 /* stop loop if all bytes are send */
373 if (restlength < irp->epsize) {
375 td->actlen = restlength;
378 td->buffer = td_buf_ptr;
379 /* move pointer for next packet */
380 td_buf_ptr += irp->epsize;
382 td->pid = USB_PID_IN;
386 /* wenn device descriptor von adresse 0 angefragt wird werden nur
387 * die ersten 8 byte abgefragt
389 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
390 && td->devaddress == 0) {
395 /**** send token ****/
396 hcdi_enqueue(td, irp->dev->ohci);
398 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
399 restlength = restlength - irp->epsize;
405 /***************** Status Stage ***********************/
406 /* Zero packet for end */
407 td = usb_create_transfer_descriptor(irp);
408 td->togl = 1; /* zero data packet = always DATA1 packet */
413 * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
414 * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
416 /* check bit 7 of bmRequestType */
417 if (bmRequestType & 0x80) {
418 td->pid = USB_PID_OUT;
420 td->pid = USB_PID_IN;
422 /**** send token ****/
423 hcdi_enqueue(td, irp->dev->ohci);
429 //u16 restlength = irp->len;
430 //char * td_buf_ptr=irp->buffer;
432 /* schleife die die tds generiert */
434 td = usb_create_transfer_descriptor(irp);
435 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
437 /* max packet size for given endpoint */
438 td->actlen = irp->epsize;
440 /* Generate In Packet */
441 if (irp->endpoint & 0x80)
442 td->pid = USB_PID_IN;
444 /* Generate Out Packet */
445 td->pid = USB_PID_OUT;
447 /* stop loop if all bytes are send */
448 if (restlength <= irp->epsize) {
450 td->actlen = restlength;
453 td->buffer = td_buf_ptr;
454 /* move pointer for next packet */
455 td_buf_ptr = td_buf_ptr + irp->epsize;
459 /**** send token ****/
460 hcdi_enqueue(td, irp->dev->ohci);
464 //if(td->pid == USB_PID_OUT) {
465 //if(togl==0) togl=1; else togl=0;
467 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
473 //u16 restlength = irp->len;
474 //char * td_buf_ptr=irp->buffer;
476 /* schleife die die tds generiert */
477 while (runloop && (restlength > 0)) {
478 td = usb_create_transfer_descriptor(irp);
479 /* max packet size for given endpoint */
480 td->actlen = irp->epsize;
482 td->pid = USB_PID_IN;
483 /* TODO: USB_PID_OUT */
485 /* stop loop if all bytes are send */
486 if (restlength < irp->epsize) {
488 td->actlen = restlength;
491 td->buffer = td_buf_ptr;
492 /* move pointer for next packet */
493 td_buf_ptr += irp->epsize;
498 /**** send token ****/
499 hcdi_enqueue(td, irp->dev->ohci);
500 restlength = restlength - irp->epsize;
504 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
506 hcdi_fire(irp->dev->ohci);
514 * Create a transfer descriptor with an parent irp.
516 struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
518 struct usb_transfer_descriptor *td =
519 (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
521 td->devaddress = irp->dev->address;
522 td->endpoint = irp->endpoint;
524 td->state = USB_TRANSFER_DESCR_NONE;
525 td->maxp = irp->epsize;
526 td->fullspeed = irp->dev->fullspeed;
527 td->type = irp->type;