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 = 8 (in Bytes!?)
119 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE << 8, 0, 8, buf, 8, 0);
120 //usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE, 0, 64, buf, 8, 0);
122 printf("===========\nafter usb control msg:\n");
123 hexdump(buf, sizeof(buf));
127 u8 address = usb_next_address();
128 dev->bMaxPacketSize0 = (u8) buf[7] ? (u8) buf[7] : 1; //dirty? /* setup real ep0 fifo size */
129 devdescr_size = (u8) buf[0]; /* save real length of device descriptor */
131 /* define new adress */
132 usb_control_msg(dev, 0x00, SET_ADDRESS, address << 8, 0, 0, buf, 8, 0);
133 dev->address = address;
135 /* get complete device descriptor */
136 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, devdescr_size, buf, 8,
139 /* save only really neccessary values for this small usbstack */
140 dev->bDeviceClass = (u8) buf[4];
141 dev->bDeviceSubClass = (u8) buf[5];
142 dev->bDeviceProtocoll = (u8) buf[6];
143 dev->idVendor = (u16) (buf[9] << 8) | (buf[8]);
144 dev->idProduct = (u16) (buf[11] << 8) | (buf[10]);
145 dev->bcdDevice = (u16) (buf[13] << 8) | (buf[12]);
147 printf( "bDeviceClass 0x%02X\n"
148 "bDeviceSubClass 0x%02X\n"
149 "bDeviceProtocoll 0x%02X\n"
152 "bcdDevice 0x%04X\n", dev->bDeviceClass,
153 dev->bDeviceSubClass, dev->bDeviceProtocoll,
154 dev->idVendor, dev->idProduct, dev->bcdDevice);
155 /* for lewurms keyboard it should be:
165 // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit
166 // entprechenden funktionen
167 // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
169 /* add device to device list */
170 element *tmp = (element *) malloc(sizeof(element));
171 tmp->data = (void *) dev;
172 list_add_tail(core.devices, tmp);
181 * Find currently detached device and remove
184 u8 usb_remove_device(usb_device * dev)
186 // FIXME!!!! dieser quatsch ist nur temporaer
187 free(core.devices->head);
189 core.devices = list_create();
194 * Register new driver at usb stack.
196 u8 usb_register_driver(usb_driver * dev)
198 /* add driver to driver list */
199 element *tmp = (element *) malloc(sizeof(element));
200 tmp->data = (void *) dev;
202 list_add_tail(core.drivers, tmp);
206 * first check to find a suitable device
207 * (root hub drivers need this call here)
216 * Call every probe function from every registered
217 * driver, to check if there is a valid driver
218 * for the new device.
220 void usb_probe_driver()
222 // call ever registered driver
224 element *iterator = core.drivers->head;
225 while (iterator != NULL) {
226 drv = (usb_driver *) iterator->data;
228 iterator = iterator->next;
235 usb_irp *usb_get_irp()
243 u8 usb_remove_irp(usb_irp * irp)
250 * Takes usb_irp and split it into
251 * several usb packeges (SETUP,IN,OUT)
252 * In the usbstack they are transported with the
253 * usb_transfer_descriptor data structure.
255 u16 usb_submit_irp(usb_irp *irp)
257 usb_transfer_descriptor *td;
259 u16 restlength = irp->len;
260 char *td_buf_ptr = irp->buffer;
263 //u8 togl=irp->dev->epTogl[(irp->endpoint & 0x7F)];
264 u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
270 /* alle requests mit dem gleichen algorithmus zerteilen
271 * das einzige ist der spezielle get_Device_descriptor request
272 * bei dem eine laenge von 64 angegeben ist.
273 * wenn man an adresse 0 einen get_device_desciptor schickt
274 * dann reichen die ersten 8 byte.
277 /***************** Setup Stage ***********************/
278 td = usb_create_transfer_descriptor(irp);
279 td->pid = USB_PID_SETUP;
280 td->buffer = irp->buffer;
282 /* control message are always 8 bytes */
284 memcpy(mybuf, td->buffer, td->actlen);
287 td->togl = togl; /* start with data0 */
292 /**** send token ****/
296 memcpy(td->buffer, mybuf, td->actlen);
299 /***************** Data Stage ***********************/
301 * You can see at bit 7 of bmRequestType if this stage is used,
302 * default requests are always 8 byte greate, from
303 * host to device. Stage 3 is only neccessary if the request
304 * expected datas from the device.
305 * bit7 - 1 = from device to host -> yes we need data stage
306 * bit7 - 0 = from host to device -> no send zero packet
308 * nach einem setup token kann nur ein IN token in stage 3 folgen
309 * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
314 * - es kommen noch Daten mit PID_IN an
315 * - host beendet mit PID_OUT DATA1 Zero
317 * Host zu Device (wie set address)
318 * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
320 usb_device_request *setup = (usb_device_request *) irp->buffer;
321 u8 bmRequestType = setup->bmRequestType;
323 if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */
325 /* schleife die die tds generiert */
327 td = usb_create_transfer_descriptor(irp);
328 td->actlen = irp->epsize;
329 /* stop loop if all bytes are send */
330 if (restlength <= irp->epsize) {
332 td->actlen = restlength;
335 td->buffer = td_buf_ptr;
336 /* move pointer for next packet */
337 td_buf_ptr = td_buf_ptr + irp->epsize;
339 td->pid = USB_PID_IN;
346 /* wenn device descriptor von adresse 0 angefragt wird werden nur
347 * die ersten 8 byte abgefragt
349 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue >> 8) == 1
350 && td->devaddress == 0) {
351 runloop = 0; /* stop loop */
354 /**** send token ****/
355 printf("togl: %d\n", togl);
358 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
359 restlength = restlength - irp->epsize;
364 /***************** Status Stage ***********************/
365 /* Zero packet for end */
366 td = usb_create_transfer_descriptor(irp);
367 td->togl = 1; /* zero data packet = always DATA1 packet */
372 * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
373 * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
375 if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */
376 td->pid = USB_PID_OUT;
378 td->pid = USB_PID_IN;
380 /**** send token ****/
381 printf("togl: %d\n", togl);
389 core.stdout("bulk\r\n");
391 //u16 restlength = irp->len;
392 //char * td_buf_ptr=irp->buffer;
394 /* schleife die die tds generiert */
397 td = usb_create_transfer_descriptor(irp);
398 td->endpoint = td->endpoint & 0x7F; /* clear direction bit */
400 /* max packet size for given endpoint */
401 td->actlen = irp->epsize;
403 /* Generate In Packet */
404 if (irp->endpoint & 0x80)
405 td->pid = USB_PID_IN;
407 /* Generate Out Packet */
408 td->pid = USB_PID_OUT;
410 /* stop loop if all bytes are send */
411 if (restlength <= irp->epsize) {
413 td->actlen = restlength;
416 td->buffer = td_buf_ptr;
417 /* move pointer for next packet */
418 td_buf_ptr = td_buf_ptr + irp->epsize;
425 /**** send token ****/
426 printf("togl: %d\n", togl);
431 //if(td->pid == USB_PID_OUT) {
432 //if(togl==0) togl=1; else togl=0;
434 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
445 * Create a transfer descriptor with an parent irp.
447 usb_transfer_descriptor *usb_create_transfer_descriptor(usb_irp * irp)
449 usb_transfer_descriptor *td =
450 (usb_transfer_descriptor *) malloc(sizeof(usb_transfer_descriptor));
452 td->devaddress = irp->dev->address;
453 td->endpoint = irp->endpoint;
455 td->state = USB_TRANSFER_DESCR_NONE;
456 td->maxp = irp->epsize;