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);
120 // length == 8 => no STALL?! :O
121 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE << 8, 0, 64, buf, 8, 0);
123 printf("===========\nafter usb control msg:\n");
124 hexdump(buf, sizeof(buf));
128 u8 address = usb_next_address();
129 dev->bMaxPacketSize0 = (u8) buf[7] ? (u8) buf[7] : 1; //dirty? /* setup real ep0 fifo size */
130 devdescr_size = (u8) buf[0]; /* save real length of device descriptor */
132 /* define new adress */
133 usb_control_msg(dev, 0x00, SET_ADDRESS, address << 8, 0, 0, buf, 8, 0);
134 dev->address = address;
136 /* get complete device descriptor */
137 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, devdescr_size, buf, 8,
140 /* save only really neccessary values for this small usbstack */
141 dev->bDeviceClass = (u8) buf[4];
142 dev->bDeviceSubClass = (u8) buf[5];
143 dev->bDeviceProtocoll = (u8) buf[6];
144 dev->idVendor = (u16) (buf[9] << 8) | (buf[8]);
145 dev->idProduct = (u16) (buf[11] << 8) | (buf[10]);
146 dev->bcdDevice = (u16) (buf[13] << 8) | (buf[12]);
148 printf( "bDeviceClass 0x%02X\n"
149 "bDeviceSubClass 0x%02X\n"
150 "bDeviceProtocoll 0x%02X\n"
153 "bcdDevice 0x%04X\n", dev->bDeviceClass,
154 dev->bDeviceSubClass, dev->bDeviceProtocoll,
155 dev->idVendor, dev->idProduct, dev->bcdDevice);
156 /* for lewurms keyboard it should be:
166 // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit
167 // entprechenden funktionen
168 // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
170 /* add device to device list */
171 element *tmp = (element *) malloc(sizeof(element));
172 tmp->data = (void *) dev;
173 list_add_tail(core.devices, tmp);
182 * Find currently detached device and remove
185 u8 usb_remove_device(usb_device * dev)
187 // FIXME!!!! dieser quatsch ist nur temporaer
188 free(core.devices->head);
190 core.devices = list_create();
195 * Register new driver at usb stack.
197 u8 usb_register_driver(usb_driver * dev)
199 /* add driver to driver list */
200 element *tmp = (element *) malloc(sizeof(element));
201 tmp->data = (void *) dev;
203 list_add_tail(core.drivers, tmp);
207 * first check to find a suitable device
208 * (root hub drivers need this call here)
217 * Call every probe function from every registered
218 * driver, to check if there is a valid driver
219 * for the new device.
221 void usb_probe_driver()
223 // call ever registered driver
225 element *iterator = core.drivers->head;
226 while (iterator != NULL) {
227 drv = (usb_driver *) iterator->data;
229 iterator = iterator->next;
236 usb_irp *usb_get_irp()
244 u8 usb_remove_irp(usb_irp * irp)
251 * Takes usb_irp and split it into
252 * several usb packeges (SETUP,IN,OUT)
253 * In the usbstack they are transported with the
254 * usb_transfer_descriptor data structure.
256 u16 usb_submit_irp(usb_irp *irp)
258 usb_transfer_descriptor *td;
260 u16 restlength = irp->len;
261 char *td_buf_ptr = irp->buffer;
264 u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
268 /* alle requests mit dem gleichen algorithmus zerteilen
269 * das einzige ist der spezielle get_Device_descriptor request
270 * bei dem eine laenge von 64 angegeben ist.
271 * wenn man an adresse 0 einen get_device_desciptor schickt
272 * dann reichen die ersten 8 byte.
275 /***************** Setup Stage ***********************/
276 td = usb_create_transfer_descriptor(irp);
277 td->pid = USB_PID_SETUP;
278 td->buffer = irp->buffer;
280 /* control message are always 8 bytes */
282 memcpy(mybuf, td->buffer, td->actlen);
285 /* start with data0 */
289 /**** send token ****/
292 /***************** Data Stage ***********************/
294 * You can see at bit 7 of bmRequestType if this stage is used,
295 * default requests are always 8 byte greate, from
296 * host to device. Stage 3 is only neccessary if the request
297 * expected datas from the device.
298 * bit7 - 1 = from device to host -> yes we need data stage
299 * bit7 - 0 = from host to device -> no send zero packet
301 * nach einem setup token kann nur ein IN token in stage 3 folgen
302 * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
307 * - es kommen noch Daten mit PID_IN an
308 * - host beendet mit PID_OUT DATA1 Zero
310 * Host zu Device (wie set address)
311 * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
313 memcpy(mybuf, irp->buffer, td->actlen);
314 usb_device_request *setup = (usb_device_request *) mybuf;
315 u8 bmRequestType = setup->bmRequestType;
318 /* check bit 7 of bmRequestType */
319 if (bmRequestType & 0x80) {
320 /* schleife die die tds generiert */
322 td = usb_create_transfer_descriptor(irp);
323 td->actlen = irp->epsize;
324 /* stop loop if all bytes are send */
325 if (restlength <= irp->epsize) {
327 td->actlen = restlength;
330 td->buffer = td_buf_ptr;
331 /* move pointer for next packet */
332 td_buf_ptr = td_buf_ptr + irp->epsize;
334 td->pid = USB_PID_IN;
338 /* wenn device descriptor von adresse 0 angefragt wird werden nur
339 * die ersten 8 byte abgefragt
341 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
342 && td->devaddress == 0) {
347 /**** send token ****/
350 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
351 restlength = restlength - irp->epsize;
357 /***************** Status Stage ***********************/
358 /* Zero packet for end */
359 td = usb_create_transfer_descriptor(irp);
360 td->togl = 1; /* zero data packet = always DATA1 packet */
365 * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
366 * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
368 /* check bit 7 of bmRequestType */
369 if (bmRequestType & 0x80) {
370 td->pid = USB_PID_OUT;
372 td->pid = USB_PID_IN;
374 /**** send token ****/
380 core.stdout("bulk\r\n");
382 //u16 restlength = irp->len;
383 //char * td_buf_ptr=irp->buffer;
385 /* schleife die die tds generiert */
388 td = usb_create_transfer_descriptor(irp);
389 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
391 /* max packet size for given endpoint */
392 td->actlen = irp->epsize;
394 /* Generate In Packet */
395 if (irp->endpoint & 0x80)
396 td->pid = USB_PID_IN;
398 /* Generate Out Packet */
399 td->pid = USB_PID_OUT;
401 /* stop loop if all bytes are send */
402 if (restlength <= irp->epsize) {
404 td->actlen = restlength;
407 td->buffer = td_buf_ptr;
408 /* move pointer for next packet */
409 td_buf_ptr = td_buf_ptr + irp->epsize;
416 /**** send token ****/
421 //if(td->pid == USB_PID_OUT) {
422 //if(togl==0) togl=1; else togl=0;
424 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
436 * Create a transfer descriptor with an parent irp.
438 usb_transfer_descriptor *usb_create_transfer_descriptor(usb_irp * irp)
440 usb_transfer_descriptor *td =
441 (usb_transfer_descriptor *) malloc(sizeof(usb_transfer_descriptor));
443 td->devaddress = irp->dev->address;
444 td->endpoint = irp->endpoint;
446 td->state = USB_TRANSFER_DESCR_NONE;
447 td->maxp = irp->epsize;