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.
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()
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 /* send at first time only 8 bytes */
95 dev->bMaxPacketSize0 = 8;
106 ret = usb_get_desc_dev_simple(dev);
111 /* set MaxPacketSize */
113 u8 address = usb_next_address();
114 ret = usb_set_address(dev, address);
115 dev->address = address;
117 printf("set address to %d\n", dev->address);
120 ret = usb_get_desc_dev(dev);
122 char *man, *prod, *serial;
123 if(dev->iManufacturer) {
124 man = usb_get_string_simple(dev, dev->iManufacturer);
126 man = (char*) malloc(11);
127 memset(man, '\0', sizeof(man));
128 strlcpy(man, "no String", 10);
131 prod = usb_get_string_simple(dev, dev->iProduct);
133 prod = (char*) malloc(11);
134 memset(prod, '\0', sizeof(prod));
135 strlcpy(prod, "no String", 10);
137 if(dev->iSerialNumber) {
138 serial = usb_get_string_simple(dev, dev->iSerialNumber);
140 serial = (char*) malloc(11);
141 memset(serial, '\0', sizeof(serial));
142 strlcpy(serial, "no String", 10);
145 printf( "bLength 0x%02X\n"
146 "bDescriptorType 0x%02X\n"
148 "bDeviceClass 0x%02X\n"
149 "bDeviceSubClass 0x%02X\n"
150 "bDeviceProtocoll 0x%02X\n"
154 "iManufacturer(0x%02X): \"%s\"\n"
155 "iProduct(0x%02X): \"%s\"\n"
156 "iSerialNumber(0x%02X): \"%s\"\n"
157 "bNumConfigurations 0x%02X\n", dev->bLength, dev->bDeviceClass,
158 dev->bcdUSB, dev->bDescriptorType, dev->bDeviceSubClass,
159 dev->bDeviceProtocoll, dev->idVendor, dev->idProduct, dev->bcdDevice,
160 dev->iManufacturer, man,
162 dev->iSerialNumber, serial,
163 dev->bNumConfigurations);
165 /* in the most cases usb devices have just one configuration descriptor */
166 ret = usb_get_desc_config_ext(dev, 0);
168 /* select configuration */
169 ret = usb_set_configuration(dev, dev->conf->bConfigurationValue);
170 printf("=============\nusb_set_configuration(ret: %d) %d\n", ret, dev->conf->bConfigurationValue);
175 printf("=============\ninterfaces: %d\n", dev->conf->bNumInterfaces);
177 for(i = 1; i <= dev->conf->bNumInterfaces; i++) {
178 memset(buf, 0, sizeof(buf));
179 ret = usb_get_desc_interface(dev, 1, buf, sizeof(buf));
180 printf("=============\nafter usb_get_desc_interface_%d(ret: %d):\n", i, ret);
181 hexdump(buf, sizeof(buf));
188 usb_get_descriptor(dev, DEVICE, 0, buf, 8);
190 usb_get_descriptor(dev, DEVICE, 0, buf, size >= buf[0] ? buf[0] : size);
193 memset(buf, 0, sizeof(buf));
194 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, 8, buf, 0);
195 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
196 hexdump(buf, sizeof(buf));
198 memset(buf, 0, sizeof(buf));
199 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, buf[0], buf, 0);
200 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
201 hexdump(buf, sizeof(buf));
203 memset(buf, 0, sizeof(buf));
204 usb_get_string_simple(dev, 1, buf);
205 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
206 hexdump(buf, sizeof(buf));
212 /* setup real ep0 fifo size */
213 dev->bMaxPacketSize0 = (u8) buf[7];
219 /* save real length of device descriptor */
220 devdescr_size = (u8) buf[0];
222 /* define new adress */
223 memset(buf, 0, sizeof(buf));
224 usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 8, 0);
225 dev->address = address;
226 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
227 hexdump(buf, sizeof(buf));
228 printf("address: %d\n", address);
231 /* get complete device descriptor */
232 memset(buf, 0, sizeof(buf));
233 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE<<8, 0, devdescr_size, buf, 8, 0);
235 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
236 hexdump(buf, sizeof(buf));
238 /* save only really neccessary values for this small usbstack */
242 memset(buf, 0, sizeof(buf));
243 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8)|2, 0, 0x1a, buf, 8, 0);
244 hexdump(buf, sizeof(buf));
245 printf("String Descriptor [1]: ");
247 for (i=2; i<buf[0]; i+=2)
248 printf("%c", buf[i]);
253 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8) | 2, 0, 0x20, buf, 8, 0);
254 printf("String Descriptor [2]: ");
255 for (i=2; i<buf[0]; i+=2)
256 printf("%c", buf[i]);
260 // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit
261 // entprechenden funktionen
262 // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
265 /* add device to device list */
266 element *tmp = (element *) malloc(sizeof(element));
267 tmp->data = (void *) dev;
268 list_add_tail(core.devices, tmp);
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 ****/
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 ****/
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 ****/
474 core.stdout("bulk\r\n");
476 //u16 restlength = irp->len;
477 //char * td_buf_ptr=irp->buffer;
479 /* schleife die die tds generiert */
482 td = usb_create_transfer_descriptor(irp);
483 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
485 /* max packet size for given endpoint */
486 td->actlen = irp->epsize;
488 /* Generate In Packet */
489 if (irp->endpoint & 0x80)
490 td->pid = USB_PID_IN;
492 /* Generate Out Packet */
493 td->pid = USB_PID_OUT;
495 /* stop loop if all bytes are send */
496 if (restlength <= irp->epsize) {
498 td->actlen = restlength;
501 td->buffer = td_buf_ptr;
502 /* move pointer for next packet */
503 td_buf_ptr = td_buf_ptr + irp->epsize;
510 /**** send token ****/
515 //if(td->pid == USB_PID_OUT) {
516 //if(togl==0) togl=1; else togl=0;
518 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
530 * Create a transfer descriptor with an parent irp.
532 struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
534 struct usb_transfer_descriptor *td =
535 (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
537 td->devaddress = irp->dev->address;
538 td->endpoint = irp->endpoint;
540 td->state = USB_TRANSFER_DESCR_NONE;
541 td->maxp = irp->epsize;