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("=============\nafter usb_get_dev_desc_simple(ret: %d):\n", 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("=============\nafter usb_get_dev_desc(ret: %d):\n", 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("=============\nafter usb_get_desc_configuration(ret: %d):\n", ret);
187 hexdump(buf, sizeof(buf));
189 memset(buf, 0, sizeof(buf));
190 ret = usb_get_descriptor(dev, CONFIGURATION, 0, buf, dev->conf->wTotalLength);
191 printf("=============\nafter usb_get_HACK(ret: %d):\n", ret);
192 hexdump(buf, sizeof(buf));
194 /* select configuration */
195 ret = usb_set_configuration(dev, dev->conf->bConfigurationValue);
196 printf("=============\nusb_set_configuration(ret: %d) %d\n", ret, dev->conf->bConfigurationValue);
201 printf("=============\ninterfaces: %d\n", dev->conf->bNumInterfaces);
203 for(i = 1; i <= dev->conf->bNumInterfaces; i++) {
204 memset(buf, 0, sizeof(buf));
205 ret = usb_get_desc_interface(dev, 1, buf, sizeof(buf));
206 printf("=============\nafter usb_get_desc_interface_%d(ret: %d):\n", i, ret);
207 hexdump(buf, sizeof(buf));
214 usb_get_descriptor(dev, DEVICE, 0, buf, 8);
216 usb_get_descriptor(dev, DEVICE, 0, buf, size >= buf[0] ? buf[0] : size);
219 memset(buf, 0, sizeof(buf));
220 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, 8, buf, 0);
221 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
222 hexdump(buf, sizeof(buf));
224 memset(buf, 0, sizeof(buf));
225 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, buf[0], buf, 0);
226 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
227 hexdump(buf, sizeof(buf));
229 memset(buf, 0, sizeof(buf));
230 usb_get_string_simple(dev, 1, buf);
231 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
232 hexdump(buf, sizeof(buf));
238 /* setup real ep0 fifo size */
239 dev->bMaxPacketSize0 = (u8) buf[7];
245 /* save real length of device descriptor */
246 devdescr_size = (u8) buf[0];
248 /* define new adress */
249 memset(buf, 0, sizeof(buf));
250 usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 8, 0);
251 dev->address = address;
252 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
253 hexdump(buf, sizeof(buf));
254 printf("address: %d\n", address);
257 /* get complete device descriptor */
258 memset(buf, 0, sizeof(buf));
259 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE<<8, 0, devdescr_size, buf, 8, 0);
261 printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
262 hexdump(buf, sizeof(buf));
264 /* save only really neccessary values for this small usbstack */
268 memset(buf, 0, sizeof(buf));
269 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8)|2, 0, 0x1a, buf, 8, 0);
270 hexdump(buf, sizeof(buf));
271 printf("String Descriptor [1]: ");
273 for (i=2; i<buf[0]; i+=2)
274 printf("%c", buf[i]);
279 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8) | 2, 0, 0x20, buf, 8, 0);
280 printf("String Descriptor [2]: ");
281 for (i=2; i<buf[0]; i+=2)
282 printf("%c", buf[i]);
286 // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit
287 // entprechenden funktionen
288 // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
291 /* add device to device list */
292 element *tmp = (element *) malloc(sizeof(element));
293 tmp->data = (void *) dev;
294 list_add_tail(core.devices, tmp);
303 * Find currently detached device and remove
306 u8 usb_remove_device(struct usb_device * dev)
308 // FIXME!!!! dieser quatsch ist nur temporaer
309 free(core.devices->head);
311 core.devices = list_create();
316 * Register new driver at usb stack.
318 u8 usb_register_driver(struct usb_driver * dev)
320 /* add driver to driver list */
321 struct element *tmp = (struct element *) malloc(sizeof(struct element));
322 tmp->data = (void *) dev;
324 list_add_tail(core.drivers, tmp);
328 * first check to find a suitable device
329 * (root hub drivers need this call here)
338 * Call every probe function from every registered
339 * driver, to check if there is a valid driver
340 * for the new device.
342 void usb_probe_driver()
344 // call ever registered driver
345 struct usb_driver *drv;
346 struct element *iterator = core.drivers->head;
347 while (iterator != NULL) {
348 drv = (struct usb_driver *) iterator->data;
350 iterator = iterator->next;
357 struct usb_irp *usb_get_irp()
365 u8 usb_remove_irp(struct usb_irp *irp)
372 * Takes usb_irp and split it into
373 * several usb packeges (SETUP,IN,OUT)
374 * In the usbstack they are transported with the
375 * usb_transfer_descriptor data structure.
377 u16 usb_submit_irp(struct usb_irp *irp)
379 struct usb_transfer_descriptor *td;
381 u16 restlength = irp->len;
382 u8 *td_buf_ptr = irp->buffer;
385 u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
389 /* alle requests mit dem gleichen algorithmus zerteilen
390 * das einzige ist der spezielle get_Device_descriptor request
391 * bei dem eine laenge von 64 angegeben ist.
392 * wenn man an adresse 0 einen get_device_desciptor schickt
393 * dann reichen die ersten 8 byte.
396 /***************** Setup Stage ***********************/
397 td = usb_create_transfer_descriptor(irp);
398 td->pid = USB_PID_SETUP;
399 td->buffer = irp->buffer;
401 /* control message are always 8 bytes */
405 /* start with data0 */
409 /**** send token ****/
412 /***************** Data Stage ***********************/
414 * You can see at bit 7 of bmRequestType if this stage is used,
415 * default requests are always 8 byte greate, from
416 * host to device. Stage 3 is only neccessary if the request
417 * expected datas from the device.
418 * bit7 - 1 = from device to host -> yes we need data stage
419 * bit7 - 0 = from host to device -> no send zero packet
421 * nach einem setup token kann nur ein IN token in stage 3 folgen
422 * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
427 * - es kommen noch Daten mit PID_IN an
428 * - host beendet mit PID_OUT DATA1 Zero
430 * Host zu Device (wie set address)
431 * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
433 memcpy(mybuf, irp->buffer, td->actlen);
434 usb_device_request *setup = (usb_device_request *) mybuf;
435 u8 bmRequestType = setup->bmRequestType;
438 /* check bit 7 of bmRequestType */
439 if (bmRequestType & 0x80) {
440 /* schleife die die tds generiert */
441 while (runloop && (restlength > 0)) {
442 td = usb_create_transfer_descriptor(irp);
443 td->actlen = irp->epsize;
444 /* stop loop if all bytes are send */
445 if (restlength < irp->epsize) {
447 td->actlen = restlength;
450 td->buffer = td_buf_ptr;
451 /* move pointer for next packet */
452 td_buf_ptr += irp->epsize;
454 td->pid = USB_PID_IN;
458 /* wenn device descriptor von adresse 0 angefragt wird werden nur
459 * die ersten 8 byte abgefragt
461 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
462 && td->devaddress == 0) {
467 /**** send token ****/
470 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
471 restlength = restlength - irp->epsize;
477 /***************** Status Stage ***********************/
478 /* Zero packet for end */
479 td = usb_create_transfer_descriptor(irp);
480 td->togl = 1; /* zero data packet = always DATA1 packet */
485 * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
486 * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
488 /* check bit 7 of bmRequestType */
489 if (bmRequestType & 0x80) {
490 td->pid = USB_PID_OUT;
492 td->pid = USB_PID_IN;
494 /**** send token ****/
500 core.stdout("bulk\r\n");
502 //u16 restlength = irp->len;
503 //char * td_buf_ptr=irp->buffer;
505 /* schleife die die tds generiert */
508 td = usb_create_transfer_descriptor(irp);
509 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
511 /* max packet size for given endpoint */
512 td->actlen = irp->epsize;
514 /* Generate In Packet */
515 if (irp->endpoint & 0x80)
516 td->pid = USB_PID_IN;
518 /* Generate Out Packet */
519 td->pid = USB_PID_OUT;
521 /* stop loop if all bytes are send */
522 if (restlength <= irp->epsize) {
524 td->actlen = restlength;
527 td->buffer = td_buf_ptr;
528 /* move pointer for next packet */
529 td_buf_ptr = td_buf_ptr + irp->epsize;
536 /**** send token ****/
541 //if(td->pid == USB_PID_OUT) {
542 //if(togl==0) togl=1; else togl=0;
544 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
556 * Create a transfer descriptor with an parent irp.
558 struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
560 struct usb_transfer_descriptor *td =
561 (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
563 td->devaddress = irp->dev->address;
564 td->endpoint = irp->endpoint;
566 td->state = USB_TRANSFER_DESCR_NONE;
567 td->maxp = irp->epsize;