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
75 element *iterator = core.drivers->head;
76 while (iterator != NULL) {
77 drv = (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 usb_device *usb_add_device()
91 usb_device *dev = (usb_device *) malloc(sizeof(usb_device));
93 dev->bMaxPacketSize0 = 8; /* send at first time only 8 bytes */
104 memset(buf, 0, sizeof(buf));
106 /* ask first 8 bytes of device descriptor with this special
107 * GET Descriptor Request, when device address = 0
111 * see page 253 in usb_20.pdf
113 * bmRequestType = 0x80 = 10000000B
114 * bRequest = GET_DESCRIPTOR
115 * wValue = DEVICE (Descriptor Type)
117 * wLength = 64 // in fact just 8 bytes
119 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE << 8, 0, 64, buf, 8, 0);
121 printf("===========\nafter usb control msg:\n");
122 hexdump(buf, sizeof(buf));
126 u8 address = usb_next_address();
127 dev->bMaxPacketSize0 = (u8) buf[7] ? (u8) buf[7] : 1; //dirty? /* setup real ep0 fifo size */
128 devdescr_size = (u8) buf[0]; /* save real length of device descriptor */
130 /* define new adress */
131 usb_control_msg(dev, 0x00, SET_ADDRESS, address << 8, 0, 0, buf, 8, 0);
132 dev->address = address;
134 /* get complete device descriptor */
135 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, devdescr_size, buf, 8,
138 /* save only really neccessary values for this small usbstack */
139 dev->bDeviceClass = (u8) buf[4];
140 dev->bDeviceSubClass = (u8) buf[5];
141 dev->bDeviceProtocoll = (u8) buf[6];
142 dev->idVendor = (u16) (buf[9] << 8) | (buf[8]);
143 dev->idProduct = (u16) (buf[11] << 8) | (buf[10]);
144 dev->bcdDevice = (u16) (buf[13] << 8) | (buf[12]);
146 printf( "bDeviceClass 0x%02X\n"
147 "bDeviceSubClass 0x%02X\n"
148 "bDeviceProtocoll 0x%02X\n"
151 "bcdDevice 0x%04X\n", dev->bDeviceClass,
152 dev->bDeviceSubClass, dev->bDeviceProtocoll,
153 dev->idVendor, dev->idProduct, dev->bcdDevice);
154 /* for lewurms keyboard it should be:
164 // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit
165 // entprechenden funktionen
166 // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
168 /* add device to device list */
169 element *tmp = (element *) malloc(sizeof(element));
170 tmp->data = (void *) dev;
171 list_add_tail(core.devices, tmp);
180 * Find currently detached device and remove
183 u8 usb_remove_device(usb_device * dev)
185 // FIXME!!!! dieser quatsch ist nur temporaer
186 free(core.devices->head);
188 core.devices = list_create();
193 * Register new driver at usb stack.
195 u8 usb_register_driver(usb_driver * dev)
197 /* add driver to driver list */
198 element *tmp = (element *) malloc(sizeof(element));
199 tmp->data = (void *) dev;
201 list_add_tail(core.drivers, tmp);
205 * first check to find a suitable device
206 * (root hub drivers need this call here)
215 * Call every probe function from every registered
216 * driver, to check if there is a valid driver
217 * for the new device.
219 void usb_probe_driver()
221 // call ever registered driver
223 element *iterator = core.drivers->head;
224 while (iterator != NULL) {
225 drv = (usb_driver *) iterator->data;
227 iterator = iterator->next;
234 usb_irp *usb_get_irp()
242 u8 usb_remove_irp(usb_irp * irp)
249 * Takes usb_irp and split it into
250 * several usb packeges (SETUP,IN,OUT)
251 * In the usbstack they are transported with the
252 * usb_transfer_descriptor data structure.
254 u16 usb_submit_irp(usb_irp *irp)
256 usb_transfer_descriptor *td;
258 u16 restlength = irp->len;
259 char *td_buf_ptr = irp->buffer;
262 //u8 togl=irp->dev->epTogl[(irp->endpoint & 0x7F)];
263 u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
269 /* alle requests mit dem gleichen algorithmus zerteilen
270 * das einzige ist der spezielle get_Device_descriptor request
271 * bei dem eine laenge von 64 angegeben ist.
272 * wenn man an adresse 0 einen get_device_desciptor schickt
273 * dann reichen die ersten 8 byte.
276 /***************** Setup Stage ***********************/
277 td = usb_create_transfer_descriptor(irp);
278 td->pid = USB_PID_SETUP;
279 td->buffer = irp->buffer;
281 /* control message are always 8 bytes */
283 memcpy(mybuf, td->buffer, td->actlen);
286 td->togl = togl; /* start with data0 */
291 /**** send token ****/
295 memcpy(td->buffer, mybuf, td->actlen);
298 /***************** Data Stage ***********************/
300 * You can see at bit 7 of bmRequestType if this stage is used,
301 * default requests are always 8 byte greate, from
302 * host to device. Stage 3 is only neccessary if the request
303 * expected datas from the device.
304 * bit7 - 1 = from device to host -> yes we need data stage
305 * bit7 - 0 = from host to device -> no send zero packet
307 * nach einem setup token kann nur ein IN token in stage 3 folgen
308 * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
313 * - es kommen noch Daten mit PID_IN an
314 * - host beendet mit PID_OUT DATA1 Zero
316 * Host zu Device (wie set address)
317 * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
319 usb_device_request *setup = (usb_device_request *) irp->buffer;
320 u8 bmRequestType = setup->bmRequestType;
322 if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */
324 /* schleife die die tds generiert */
326 td = usb_create_transfer_descriptor(irp);
327 td->actlen = irp->epsize;
328 /* stop loop if all bytes are send */
329 if (restlength <= irp->epsize) {
331 td->actlen = restlength;
334 td->buffer = td_buf_ptr;
335 /* move pointer for next packet */
336 td_buf_ptr = td_buf_ptr + irp->epsize;
338 td->pid = USB_PID_IN;
345 /* wenn device descriptor von adresse 0 angefragt wird werden nur
346 * die ersten 8 byte abgefragt
348 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
349 && td->devaddress == 0) {
350 runloop = 0; /* stop loop */
353 /**** send token ****/
354 printf("togl: %d\n", togl);
357 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
358 restlength = restlength - irp->epsize;
363 /***************** Status Stage ***********************/
364 /* Zero packet for end */
365 td = usb_create_transfer_descriptor(irp);
366 td->togl = 1; /* zero data packet = always DATA1 packet */
371 * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
372 * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
374 if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */
375 td->pid = USB_PID_OUT;
377 td->pid = USB_PID_IN;
379 /**** send token ****/
380 printf("togl: %d\n", togl);
388 core.stdout("bulk\r\n");
390 //u16 restlength = irp->len;
391 //char * td_buf_ptr=irp->buffer;
393 /* schleife die die tds generiert */
396 td = usb_create_transfer_descriptor(irp);
397 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
399 /* max packet size for given endpoint */
400 td->actlen = irp->epsize;
402 /* Generate In Packet */
403 if (irp->endpoint & 0x80)
404 td->pid = USB_PID_IN;
406 /* Generate Out Packet */
407 td->pid = USB_PID_OUT;
409 /* stop loop if all bytes are send */
410 if (restlength <= irp->epsize) {
412 td->actlen = restlength;
415 td->buffer = td_buf_ptr;
416 /* move pointer for next packet */
417 td_buf_ptr = td_buf_ptr + irp->epsize;
424 /**** send token ****/
425 printf("togl: %d\n", togl);
430 //if(td->pid == USB_PID_OUT) {
431 //if(togl==0) togl=1; else togl=0;
433 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
444 * Create a transfer descriptor with an parent irp.
446 usb_transfer_descriptor *usb_create_transfer_descriptor(usb_irp * irp)
448 usb_transfer_descriptor *td =
449 (usb_transfer_descriptor *) malloc(sizeof(usb_transfer_descriptor));
451 td->devaddress = irp->dev->address;
452 td->endpoint = irp->endpoint;
454 td->state = USB_TRANSFER_DESCR_NONE;
455 td->maxp = irp->epsize;