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
45 * Initialize USB stack.
49 core.drivers = list_create();
50 core.devices = list_create();
56 * Get next free usb device address.
60 u8 addr = core.nextaddress;
67 * Call this function periodically for
68 * control and transfer management.
72 // call ever registered driver
74 element *iterator = core.drivers->head;
75 while (iterator != NULL) {
76 drv = (usb_driver *) iterator->data;
78 iterator = iterator->next;
84 * Enumerate new device and create data structures
85 * for the core. usb_add_device expected that
86 * the device answers to address zero.
88 usb_device *usb_add_device()
90 usb_device *dev = (usb_device *) malloc(sizeof(usb_device));
92 dev->bMaxPacketSize0 = 8; /* send at first time only 8 bytes */
104 /* ask first 8 bytes of device descriptor with this special
105 * GET Descriptor Request, when device address = 0
109 * see page 253 in usb_20.pdf
111 * bmRequestType = 0x80 = 10000000B
112 * bRequest = GET_DESCRIPTOR
113 * wValue = DEVICE (Descriptor Type)
115 * wLength = 8 (in Bytes!?)
117 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE, 0, 8, buf, 8, 0);
120 * length (here =64) should be "number of byte to transfer", not
121 * (as here) "number of bits to transfer.
124 //usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, 64, buf, 8, 0);
125 printf("===========\nafter usb control msg:\n");
126 hexdump(buf, sizeof(buf));
130 u8 address = usb_next_address();
131 dev->bMaxPacketSize0 = (u8) buf[7] ? (u8) buf[7] : 1; //dirty? /* setup real ep0 fifo size */
132 devdescr_size = (u8) buf[0]; /* save real length of device descriptor */
134 /* define new adress */
135 usb_control_msg(dev, 0x00, SET_ADDRESS, address << 8, 0, 0, buf, 8, 0);
136 dev->address = address;
138 /* get complete device descriptor */
139 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, devdescr_size, buf, 8,
142 /* save only really neccessary values for this small usbstack */
143 dev->bDeviceClass = (u8) buf[4];
144 dev->bDeviceSubClass = (u8) buf[5];
145 dev->bDeviceProtocoll = (u8) buf[6];
146 dev->idVendor = (u16) (buf[9] << 8) | (buf[8]);
147 dev->idProduct = (u16) (buf[11] << 8) | (buf[10]);
148 dev->bcdDevice = (u16) (buf[13] << 8) | (buf[12]);
150 printf( "bDeviceClass 0x%02X\n"
151 "bDeviceSubClass 0x%02X\n"
152 "bDeviceProtocoll 0x%02X\n"
155 "bcdDevice 0x%04X\n", dev->bDeviceClass,
156 dev->bDeviceSubClass, dev->bDeviceProtocoll,
157 dev->idVendor, dev->idProduct, dev->bcdDevice);
158 /* for lewurms keyboard it should be:
168 // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit
169 // entprechenden funktionen
170 // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
172 /* add device to device list */
173 element *tmp = (element *) malloc(sizeof(element));
174 tmp->data = (void *) dev;
175 list_add_tail(core.devices, tmp);
184 * Find currently detached device and remove
187 u8 usb_remove_device(usb_device * dev)
189 // FIXME!!!! dieser quatsch ist nur temporaer
190 free(core.devices->head);
192 core.devices = list_create();
197 * Register new driver at usb stack.
199 u8 usb_register_driver(usb_driver * dev)
201 /* add driver to driver list */
202 element *tmp = (element *) malloc(sizeof(element));
203 tmp->data = (void *) dev;
205 list_add_tail(core.drivers, tmp);
209 * first check to find a suitable device
210 * (root hub drivers need this call here)
219 * Call every probe function from every registered
220 * driver, to check if there is a valid driver
221 * for the new device.
223 void usb_probe_driver()
225 // call ever registered driver
227 element *iterator = core.drivers->head;
228 while (iterator != NULL) {
229 drv = (usb_driver *) iterator->data;
231 iterator = iterator->next;
238 usb_irp *usb_get_irp()
246 u8 usb_remove_irp(usb_irp * irp)
253 * Takes usb_irp and split it into
254 * several usb packeges (SETUP,IN,OUT)
255 * In the usbstack they are transported with the
256 * usb_transfer_descriptor data structure.
258 u16 usb_submit_irp(usb_irp *irp)
260 usb_transfer_descriptor *td;
262 u16 restlength = irp->len;
263 char *td_buf_ptr = irp->buffer;
265 //u8 togl=irp->dev->epTogl[(irp->endpoint & 0x7F)];
266 u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
272 /* alle requests mit dem gleichen algorithmus zerteilen
273 * das einzige ist der spezielle get_Device_descriptor request
274 * bei dem eine laenge von 64 angegeben ist.
275 * wenn man an adresse 0 einen get_device_desciptor schickt
276 * dann reichen die ersten 8 byte.
279 /***************** Setup Stage ***********************/
280 td = usb_create_transfer_descriptor(irp);
281 td->pid = USB_PID_SETUP;
282 td->buffer = irp->buffer;
283 td->actlen = 8; /* control message are always 8 bytes */
286 td->togl = togl; /* start with data0 */
291 /**** send token ****/
294 /***************** Data Stage ***********************/
296 * You can see at bit 7 of bmRequestType if this stage is used,
297 * default requests are always 8 byte greate, from
298 * host to device. Stage 3 is only neccessary if the request
299 * expected datas from the device.
300 * bit7 - 1 = from device to host -> yes we need data stage
301 * bit7 - 0 = from host to device -> no send zero packet
303 * nach einem setup token kann nur ein IN token in stage 3 folgen
304 * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
309 * - es kommen noch Daten mit PID_IN an
310 * - host beendet mit PID_OUT DATA1 Zero
312 * Host zu Device (wie set address)
313 * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
315 usb_device_request *setup = (usb_device_request *) irp->buffer;
316 u8 bmRequestType = setup->bmRequestType;
318 if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */
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;
341 /* wenn device descriptor von adresse 0 angefragt wird werden nur
342 * die ersten 8 byte abgefragt
344 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue >> 8) == 1
345 && td->devaddress == 0) {
346 runloop = 0; /* stop loop */
349 /**** send token ****/
352 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
353 restlength = restlength - irp->epsize;
358 /***************** Status Stage ***********************/
359 /* Zero packet for end */
360 td = usb_create_transfer_descriptor(irp);
361 td->togl = 1; /* zero data packet = always DATA1 packet */
366 * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
367 * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
369 if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */
370 td->pid = USB_PID_OUT;
372 td->pid = USB_PID_IN;
374 /**** send token ****/
382 core.stdout("bulk\r\n");
384 //u16 restlength = irp->len;
385 //char * td_buf_ptr=irp->buffer;
387 /* schleife die die tds generiert */
390 td = usb_create_transfer_descriptor(irp);
391 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
393 /* max packet size for given endpoint */
394 td->actlen = irp->epsize;
396 /* Generate In Packet */
397 if (irp->endpoint & 0x80)
398 td->pid = USB_PID_IN;
400 /* Generate Out Packet */
401 td->pid = USB_PID_OUT;
403 /* stop loop if all bytes are send */
404 if (restlength <= irp->epsize) {
406 td->actlen = restlength;
409 td->buffer = td_buf_ptr;
410 /* move pointer for next packet */
411 td_buf_ptr = td_buf_ptr + irp->epsize;
418 /**** send token ****/
423 //if(td->pid == USB_PID_OUT) {
424 //if(togl==0) togl=1; else togl=0;
426 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
437 * Create a transfer descriptor with an parent irp.
439 usb_transfer_descriptor *usb_create_transfer_descriptor(usb_irp * irp)
441 usb_transfer_descriptor *td =
442 (usb_transfer_descriptor *) malloc(sizeof(usb_transfer_descriptor));
444 td->devaddress = irp->dev->address;
445 td->endpoint = irp->endpoint;
447 td->state = USB_TRANSFER_DESCR_NONE;
448 td->maxp = irp->epsize;