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;
107 memset(buf, 0, sizeof(buf));
110 memset(buf, 0, sizeof(buf));
111 ret = usb_get_desc_dev_simple(dev, buf, sizeof(buf));
113 printf("=============\nbuf: 0x%08X\nafter usb_get_dev_desc_simple(ret: %d):\n", buf, ret);
114 hexdump(buf, sizeof(buf));
120 /* set MaxPacketSize */
122 u8 address = usb_next_address();
123 ret = usb_set_address(dev, address);
124 dev->address = address;
126 printf("set address to %d\n", dev->address);
129 memset(buf, 0, sizeof(buf));
130 ret = usb_get_desc_dev(dev, buf, sizeof(buf));
132 printf("=============\nbuf: 0x%08X\nafter usb_get_dev_desc(ret: %d):\n", buf, ret);
133 hexdump(buf, sizeof(buf));
136 char *man, *prod, *serial;
137 if(dev->iManufacturer) {
138 memset(buf, 0, sizeof(buf));
139 man = usb_get_string_simple(dev, dev->iManufacturer, buf, sizeof(buf));
141 man = (char*) malloc(11);
142 memset(man, '\0', sizeof(man));
143 strlcpy(man, "no String", 10);
146 memset(buf, 0, sizeof(buf));
147 prod = usb_get_string_simple(dev, dev->iProduct, buf, sizeof(buf));
149 prod = (char*) malloc(11);
150 memset(prod, '\0', sizeof(prod));
151 strlcpy(prod, "no String", 10);
153 if(dev->iSerialNumber) {
154 memset(buf, 0, sizeof(buf));
155 serial = usb_get_string_simple(dev, dev->iSerialNumber, buf, sizeof(buf));
157 serial = (char*) malloc(11);
158 memset(serial, '\0', sizeof(serial));
159 strlcpy(serial, "no String", 10);
163 printf( "bLength 0x%02X\n"
164 "bDescriptorType 0x%02X\n"
166 "bDeviceClass 0x%02X\n"
167 "bDeviceSubClass 0x%02X\n"
168 "bDeviceProtocoll 0x%02X\n"
172 "iManufacturer(0x%02X): \"%s\"\n"
173 "iProduct(0x%02X): \"%s\"\n"
174 "iSerialNumber(0x%02X): \"%s\"\n"
175 "bNumConfigurations 0x%02X\n", dev->bLength, dev->bDeviceClass,
176 dev->bcdUSB, dev->bDescriptorType, dev->bDeviceSubClass,
177 dev->bDeviceProtocoll, dev->idVendor, dev->idProduct, dev->bcdDevice,
178 dev->iManufacturer, man,
180 dev->iSerialNumber, serial,
181 dev->bNumConfigurations);
183 memset(buf, 0, sizeof(buf));
184 /* in the most cases usb devices have just one configuration descriptor */
185 ret = usb_get_desc_configuration(dev, 0, buf, sizeof(buf));
186 printf("=============\nbuf: 0x%08X\nafter usb_get_desc_configuration(ret: %d):\n", buf, ret);
187 hexdump(buf, sizeof(buf));
189 printf("interfaces: %d\n", dev->conf->bNumInterfaces);
191 for(i = 1; i <= dev->conf->bNumInterfaces; i++) {
192 memset(buf, 0, sizeof(buf));
193 ret = usb_get_desc_interface(dev, i, buf, sizeof(buf));
194 printf("=============\nbuf: 0x%08X\nafter usb_get_desc_interface_%d(ret: %d):\n", buf, i, ret);
195 hexdump(buf, sizeof(buf));
201 usb_get_descriptor(dev, DEVICE, 0, buf, 8);
203 usb_get_descriptor(dev, DEVICE, 0, buf, size >= buf[0] ? buf[0] : size);
206 memset(buf, 0, sizeof(buf));
207 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, 8, buf, 0);
208 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
209 hexdump(buf, sizeof(buf));
211 memset(buf, 0, sizeof(buf));
212 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, buf[0], buf, 0);
213 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
214 hexdump(buf, sizeof(buf));
216 memset(buf, 0, sizeof(buf));
217 usb_get_string_simple(dev, 1, buf);
218 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
219 hexdump(buf, sizeof(buf));
225 /* setup real ep0 fifo size */
226 dev->bMaxPacketSize0 = (u8) buf[7];
232 /* save real length of device descriptor */
233 devdescr_size = (u8) buf[0];
235 /* define new adress */
236 memset(buf, 0, sizeof(buf));
237 usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 8, 0);
238 dev->address = address;
239 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
240 hexdump(buf, sizeof(buf));
241 printf("address: %d\n", address);
244 /* get complete device descriptor */
245 memset(buf, 0, sizeof(buf));
246 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE<<8, 0, devdescr_size, buf, 8, 0);
248 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
249 hexdump(buf, sizeof(buf));
251 /* save only really neccessary values for this small usbstack */
255 memset(buf, 0, sizeof(buf));
256 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8)|2, 0, 0x1a, buf, 8, 0);
257 hexdump(buf, sizeof(buf));
258 printf("String Descriptor [1]: ");
260 for (i=2; i<buf[0]; i+=2)
261 printf("%c", buf[i]);
266 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8) | 2, 0, 0x20, buf, 8, 0);
267 printf("String Descriptor [2]: ");
268 for (i=2; i<buf[0]; i+=2)
269 printf("%c", buf[i]);
273 // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit
274 // entprechenden funktionen
275 // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
278 /* add device to device list */
279 element *tmp = (element *) malloc(sizeof(element));
280 tmp->data = (void *) dev;
281 list_add_tail(core.devices, tmp);
290 * Find currently detached device and remove
293 u8 usb_remove_device(struct usb_device * dev)
295 // FIXME!!!! dieser quatsch ist nur temporaer
296 free(core.devices->head);
298 core.devices = list_create();
303 * Register new driver at usb stack.
305 u8 usb_register_driver(struct usb_driver * dev)
307 /* add driver to driver list */
308 struct element *tmp = (struct element *) malloc(sizeof(struct element));
309 tmp->data = (void *) dev;
311 list_add_tail(core.drivers, tmp);
315 * first check to find a suitable device
316 * (root hub drivers need this call here)
325 * Call every probe function from every registered
326 * driver, to check if there is a valid driver
327 * for the new device.
329 void usb_probe_driver()
331 // call ever registered driver
332 struct usb_driver *drv;
333 struct element *iterator = core.drivers->head;
334 while (iterator != NULL) {
335 drv = (struct usb_driver *) iterator->data;
337 iterator = iterator->next;
344 struct usb_irp *usb_get_irp()
352 u8 usb_remove_irp(struct usb_irp *irp)
359 * Takes usb_irp and split it into
360 * several usb packeges (SETUP,IN,OUT)
361 * In the usbstack they are transported with the
362 * usb_transfer_descriptor data structure.
364 u16 usb_submit_irp(struct usb_irp *irp)
366 struct usb_transfer_descriptor *td;
368 u16 restlength = irp->len;
369 u8 *td_buf_ptr = irp->buffer;
372 u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
376 /* alle requests mit dem gleichen algorithmus zerteilen
377 * das einzige ist der spezielle get_Device_descriptor request
378 * bei dem eine laenge von 64 angegeben ist.
379 * wenn man an adresse 0 einen get_device_desciptor schickt
380 * dann reichen die ersten 8 byte.
383 /***************** Setup Stage ***********************/
384 td = usb_create_transfer_descriptor(irp);
385 td->pid = USB_PID_SETUP;
386 td->buffer = irp->buffer;
388 /* control message are always 8 bytes */
392 /* start with data0 */
396 /**** send token ****/
399 /***************** Data Stage ***********************/
401 * You can see at bit 7 of bmRequestType if this stage is used,
402 * default requests are always 8 byte greate, from
403 * host to device. Stage 3 is only neccessary if the request
404 * expected datas from the device.
405 * bit7 - 1 = from device to host -> yes we need data stage
406 * bit7 - 0 = from host to device -> no send zero packet
408 * nach einem setup token kann nur ein IN token in stage 3 folgen
409 * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
414 * - es kommen noch Daten mit PID_IN an
415 * - host beendet mit PID_OUT DATA1 Zero
417 * Host zu Device (wie set address)
418 * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
420 memcpy(mybuf, irp->buffer, td->actlen);
421 usb_device_request *setup = (usb_device_request *) mybuf;
422 u8 bmRequestType = setup->bmRequestType;
425 /* check bit 7 of bmRequestType */
426 if (bmRequestType & 0x80) {
427 /* schleife die die tds generiert */
428 while (runloop && (restlength > 0)) {
429 td = usb_create_transfer_descriptor(irp);
430 td->actlen = irp->epsize;
431 /* stop loop if all bytes are send */
432 if (restlength < irp->epsize) {
434 td->actlen = restlength;
437 td->buffer = td_buf_ptr;
438 /* move pointer for next packet */
439 td_buf_ptr += irp->epsize;
441 td->pid = USB_PID_IN;
445 /* wenn device descriptor von adresse 0 angefragt wird werden nur
446 * die ersten 8 byte abgefragt
448 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
449 && td->devaddress == 0) {
454 /**** send token ****/
457 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
458 restlength = restlength - irp->epsize;
464 /***************** Status Stage ***********************/
465 /* Zero packet for end */
466 td = usb_create_transfer_descriptor(irp);
467 td->togl = 1; /* zero data packet = always DATA1 packet */
472 * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
473 * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
475 /* check bit 7 of bmRequestType */
476 if (bmRequestType & 0x80) {
477 td->pid = USB_PID_OUT;
479 td->pid = USB_PID_IN;
481 /**** send token ****/
487 core.stdout("bulk\r\n");
489 //u16 restlength = irp->len;
490 //char * td_buf_ptr=irp->buffer;
492 /* schleife die die tds generiert */
495 td = usb_create_transfer_descriptor(irp);
496 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
498 /* max packet size for given endpoint */
499 td->actlen = irp->epsize;
501 /* Generate In Packet */
502 if (irp->endpoint & 0x80)
503 td->pid = USB_PID_IN;
505 /* Generate Out Packet */
506 td->pid = USB_PID_OUT;
508 /* stop loop if all bytes are send */
509 if (restlength <= irp->epsize) {
511 td->actlen = restlength;
514 td->buffer = td_buf_ptr;
515 /* move pointer for next packet */
516 td_buf_ptr = td_buf_ptr + irp->epsize;
523 /**** send token ****/
528 //if(td->pid == USB_PID_OUT) {
529 //if(togl==0) togl=1; else togl=0;
531 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
543 * Create a transfer descriptor with an parent irp.
545 struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
547 struct usb_transfer_descriptor *td =
548 (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
550 td->devaddress = irp->dev->address;
551 td->endpoint = irp->endpoint;
553 td->state = USB_TRANSFER_DESCR_NONE;
554 td->maxp = irp->epsize;