e2643c951720b8f4f31aeb164351841b18342eeb
[ppcskel.git] / usb / core / core.c
1 /*
2  * Copyright (c) 2006, Benedikt Sauter <sauter@ixbat.de>
3  * All rights reserved.
4  *
5  * Short descripton of file:
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions 
10  * are met:
11  *
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.
21  *
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.
33  */
34
35 //#include <stdlib.h>
36 #include "core.h"
37 #include "../host/host.h"
38 #include "usb.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
44
45 /**
46  * Initialize USB stack.
47  */
48 void usb_init(u32 reg)
49 {
50         core.drivers = list_create();
51         core.devices = list_create();
52         core.nextaddress = 1;
53         hcdi_init(reg);
54 }
55
56 /**
57  * Get next free usb device address.
58  */
59 u8 usb_next_address()
60 {
61         u8 addr = core.nextaddress;
62         core.nextaddress++;
63         return addr;
64 }
65
66
67 /**
68  * Call this function periodically for 
69  * control and transfer management.
70  */
71 void usb_periodic()
72 {
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;
78                 drv->check();
79                 iterator = iterator->next;
80         }
81 }
82
83
84 /** 
85  * Enumerate new device and create data structures 
86  * for the core. usb_add_device expected that
87  * the device answers to address zero.
88  */
89 struct usb_device *usb_add_device(u8 lowspeed, u32 reg)
90 {
91         struct usb_device *dev = (struct usb_device *) malloc(sizeof(struct usb_device));
92         dev->conf = (struct usb_conf *) malloc(sizeof(struct usb_conf));
93         dev->address = 0;
94         dev->fullspeed = lowspeed ? 0 : 1;
95         /* send at first time only 8 bytes for lowspeed devices
96          * 64 bytes for fullspeed
97          */
98         dev->bMaxPacketSize0 = lowspeed ? 8 : 64;
99         dev->ohci = reg;
100
101         dev->epSize[0] = 8;
102         dev->epSize[1] = 64;
103         dev->epSize[2] = 64;
104
105         dev->epTogl[0] = 0;
106         dev->epTogl[1] = 0;
107         dev->epTogl[2] = 0;
108
109         s8 ret;
110         ret = usb_get_desc_dev_simple(dev);
111         if(ret < 0) {
112                 return (void*) -1;
113         }
114
115 #define WTF
116 #ifdef WTF
117         printf("lololololool PADDING WTF :O lolololololo \n");
118         printf("lolololool PADDING WTF :O lololoololo \n");
119         printf("lololololool PADDING WTF :O lolololololo \n");
120         printf("lololololool PADDING WTF :O lolololololo \n");
121         printf("lololololool PADDING WTF :O lolololololo \n");
122         printf("lololololool PADDING WTF :O lolololololo \n");
123         printf("lololololool PADDING WTF :O lolololololo \n");
124         printf("lololololool PADDING WTF :O lolololololo \n");
125         printf("lololololool PADDING WTF :O lolololololo \n");
126         printf("lolololool PADDING WTF :O lololoololo \n");
127         printf("lololololool PADDING WTF :O lolololololo \n");
128         printf("lololololool PADDING WTF :O lolololololo \n");
129         printf("lolololool PADDING WTF :O lololoololo \n");
130         printf("lololololool PADDING WTF :O lolololololo \n");
131         printf("lololololool PADDING WTF :O lolololololo \n");
132         printf("lolololool PADDING WTF :O lololoololo \n");
133         printf("lololololool PADDING WTF :O lolololololo \n");
134         printf("lololololool PADDING WTF :O lolololololo \n");
135         printf("lolololool PADDING WTF :O lololoololo \n");
136         printf("lololololool PADDING WTF :O lolololololo \n");
137         printf("lolololool PADDING WTF :O lolololololo \n");
138         printf("lololololool PADDING WTF :O lolololololo \n");
139         printf("lollllool PADDING WTF :O lolololololo \n");
140         printf("lololololool PADDING WTF :O lolololololo \n");
141         printf("lolololool PADDING WTF :O lololoololo \n");
142         printf("lololololool PADDING WTF :O lolololololo \n");
143         printf("lolololool PADDING WTF :O lolololololo \n");
144         printf("lololololool PADDING WTF :O lolololololo \n");
145         printf("lolololool PADDING WTF :O lolololololo \n");
146         printf("lololololool PADDING WTF :O lolololololo \n");
147         printf("lollllool PADDING WTF :O lolololololo \n");
148         printf("lololololool PADDING WTF :O lolololololo \n");
149         printf("lolololool PADDING WTF :O lololoololo \n");
150         printf("lololololool PADDING WTF :O lolololololo \n");
151         printf("lolololool PADDING WTF :O lolololololo \n");
152         printf("lololololool PADDING WTF :O lolololololo \n");
153         printf("lolololool PADDING WTF :O lololoololo \n");
154         printf("lololololool PADDING WTF :O lolololololo \n");
155         printf("lolololool PADDING WTF :O lolololololo \n");
156         printf("lololololool PADDING WTF :O lolololololo \n");
157         printf("lollllool PADDING WTF :O lolololololo \n");
158         printf("lololololool PADDING WTF :O lolololololo \n");
159         printf("lolololool PADDING WTF :O lololoololo \n");
160         printf("lololololool PADDING WTF :O lolololololo \n");
161         printf("lolololool PADDING WTF :O lolololololo \n");
162         printf("lololololool PADDING WTF :O lolololololo \n");
163         printf("lololololool PADDING WTF :O lolololololo \n");
164         printf("lolololool PADDING WTF :O lolololololo \n");
165         printf("lololololool PADDING WTF :O lolololololo \n");
166         printf("lollllool PADDING WTF :O lolololololo \n");
167         printf("lololololool PADDING WTF :O lolololololo \n");
168         printf("lolololool PADDING WTF :O lololoololo \n");
169         printf("lololololool PADDING WTF :O lolololololo \n");
170         printf("lolololool PADDING WTF :O lolololololo \n");
171         printf("lololololool PADDING WTF :O lolololololo \n");
172         printf("lolololool PADDING WTF :O lolololololo \n");
173         printf("lololololool PADDING WTF :O lolololololo \n");
174         printf("lollllool PADDING WTF :O lolololololo \n");
175         printf("lololololool PADDING WTF :O lolololololo \n");
176         printf("lolololool PADDING WTF :O lololoololo \n");
177         printf("lololololool PADDING WTF :O lolololololo \n");
178         printf("lolololool PADDING WTF :O lolololololo \n");
179         printf("lololololool PADDING WTF :O lolololololo \n");
180         printf("lolololool PADDING WTF :O lololoololo \n");
181         printf("lololololool PADDING WTF :O lolololololo \n");
182         printf("lolololool PADDING WTF :O lolololololo \n");
183         printf("lololololool PADDING WTF :O lolololololo \n");
184         printf("lollllool PADDING WTF :O lolololololo \n");
185         printf("lololololool PADDING WTF :O lolololololo \n");
186         printf("lolololool PADDING WTF :O lololoololo \n");
187         printf("lololololool PADDING WTF :O lolololololo \n");
188         printf("lolololool PADDING WTF :O lolololololo \n");
189         printf("lololololool PADDING WTF :O lolololololo \n");
190         printf("lolololool PADDING WTF :O lololoololo \n");
191         printf("lololololool PADDING WTF :O lolololololo \n");
192         printf("lolololool PADDING WTF :O lolololololo \n");
193         printf("lololololool PADDING WTF :O lolololololo \n");
194         printf("lollllool PADDING WTF :O lolololololo \n");
195         printf("lololololool PADDING WTF :O lolololololo \n");
196         printf("lolololool PADDING WTF :O lololoololo \n");
197         printf("lololololool PADDING WTF :O lolololololo \n");
198         printf("lolololool PADDING WTF :O lolololololo \n");
199         printf("lololololool PADDING WTF :O lolololololo \n");
200         printf("lolololool PADDING WTF :O lololoololo \n");
201         printf("lololololool PADDING WTF :O lolololololo \n");
202         printf("lolololool PADDING WTF :O lolololololo \n");
203         printf("lololololool PADDING WTF :O lolololololo \n");
204         printf("lollllool PADDING WTF :O lolololololo \n");
205         printf("lololololool PADDING WTF :O lolololololo \n");
206         printf("lolololool PADDING WTF :O lololoololo \n");
207         printf("lololololool PADDING WTF :O lolololololo \n");
208         printf("lolololool PADDING WTF :O lolololololo \n");
209         printf("lololololool PADDING WTF :O lolololololo \n");
210         printf("lolololool PADDING WTF :O lololoololo \n");
211         printf("lololololool PADDING WTF :O lolololololo \n");
212         printf("lolololool PADDING WTF :O lolololololo \n");
213         printf("lololololool PADDING WTF :O lolololololo \n");
214         printf("lollllool PADDING WTF :O lolololololo \n");
215         printf("lololololool PADDING WTF :O lolololololo \n");
216         printf("lolololool PADDING WTF :O lololoololo \n");
217         printf("lololololool PADDING WTF :O lolololololo \n");
218         printf("lolololool PADDING WTF :O lolololololo \n");
219         printf("lololololool PADDING WTF :O lolololololo \n");
220         printf("lolololool PADDING WTF :O lololoololo \n");
221         printf("lololololool PADDING WTF :O lolololololo \n");
222         printf("lolololool PADDING WTF :O lolololololo \n");
223         printf("lololololool PADDING WTF :O lolololololo \n");
224         printf("lollllool PADDING WTF :O lolololololo \n");
225         printf("lololololool PADDING WTF :O lolololololo \n");
226         printf("lolololool PADDING WTF :O lololoololo \n");
227         printf("lololololool PADDING WTF :O lolololololo \n");
228         printf("lolololool PADDING WTF :O lolololololo \n");
229         printf("lololololool PADDING WTF :O lolololololo \n");
230         printf("lolololool PADDING WTF :O lololoololo \n");
231         printf("lololololool PADDING WTF :O lolololololo \n");
232         printf("lolololool PADDING WTF :O lolololololo \n");
233         printf("lololololool PADDING WTF :O lolololololo \n");
234         printf("lollllool PADDING WTF :O lolololololo \n");
235         printf("lololololool PADDING WTF :O lolololololo \n");
236         printf("lolololool PADDING WTF :O lololoololo \n");
237         printf("lololololool PADDING WTF :O lolololololo \n");
238         printf("lolololool PADDING WTF :O lolololololo \n");
239         printf("lololololool PADDING WTF :O lolololololo \n");
240         printf("lololololool PADDING WTF :O lolololololo \n");
241         printf("lolololool PADDING WTF :O lolololololo \n");
242         printf("lololololool PADDING WTF :O lolololololo \n");
243         printf("lollllool PADDING WTF :O lolololololo \n");
244         printf("lololololool PADDING WTF :O lolololololo \n");
245         printf("lolololool PADDING WTF :O lololoololo \n");
246         printf("lololololool PADDING WTF :O lolololololo \n");
247         printf("lolololool PADDING WTF :O lolololololo \n");
248         printf("lololololool PADDING WTF :O lolololololo \n");
249         printf("lollllool PADDING WTF :O lolololololo \n");
250         printf("lololololool PADDING WTF :O lolololololo \n");
251         printf("lolololool PADDING WTF :O lololoololo \n");
252         printf("lololololool PADDING WTF :O lolololololo \n");
253         printf("lolololool PADDING WTF :O lolololololo \n");
254         printf("lololololool PADDING WTF :O lolololololo \n");
255         printf("lollllool PADDING WTF :O lolololololo \n");
256         printf("lololololool PADDING WTF :O lolololololo \n");
257         printf("lolololool PADDING WTF :O lololoololo \n");
258         printf("lololololool PADDING WTF :O lolololololo \n");
259         printf("lolololool PADDING WTF :O lolololololo \n");
260         printf("lololololool PADDING WTF :O lolololololo \n");
261         printf("lollllool PADDING WTF :O lolololololo \n");
262         printf("lolololool PADDING WTF :O lolololololo \n");
263         printf("lololololool PADDING WTF :O lolololololo \n");
264         printf("lollllool PADDING WTF :O lolololololo \n");
265 #endif
266         u8 address = usb_next_address();
267         ret = usb_set_address(dev, address);
268         dev->address = address;
269         printf("set address to %d\n", dev->address);
270
271         /* get device descriptor&co */
272         ret = usb_get_desc_dev(dev);
273         if(ret < 0)
274                 return (void*) -1;
275
276         /* print device info */
277         //lsusb(dev);
278
279
280 #if 1
281         /* add device to device list */
282         struct element *tmp = (struct element *) malloc(sizeof(struct element));
283         tmp->data = (void *) dev;
284         list_add_tail(core.devices, tmp);
285
286         usb_probe_driver();
287 #endif
288
289         return dev;
290 }
291
292 void lsusb(struct usb_device *dev)
293 {
294         printf("=== Device Descriptor === \n");
295         printf("bLength 0x%02X\n", dev->bLength);
296         printf("bDescriptorType 0x%02X\n", dev->bDeviceClass);
297         printf("bcdUSB 0x%04X\n", dev->bcdUSB);
298         printf("bDeviceClass 0x%02X\n", dev->bDeviceClass);
299         printf("bDeviceSubClass 0x%02X\n", dev->bDeviceSubClass);
300         printf("bDeviceProtocoll 0x%02X\n", dev->bDeviceProtocoll);
301         printf("bMaxPacketSize 0x%02X\n", dev->bMaxPacketSize0);
302         printf("idVendor 0x%04X\n", dev->idVendor);
303         printf("idProduct 0x%04X\n", dev->idProduct);
304         printf("bcdDevice 0x%04X\n", dev->bcdDevice);
305         printf("iManufacturer(0x%02X): \"%s\"\n", dev->iManufacturer, dev->iManufacturer ? usb_get_string_simple(dev, dev->iManufacturer) : "no String");
306         printf("iProduct(0x%02X): \"%s\"\n", dev->iProduct, dev->iProduct ? usb_get_string_simple(dev, dev->iProduct) : "no String");
307         printf("iSerialNumber(0x%02X): \"%s\"\n", dev->iSerialNumber, dev->iSerialNumber ? usb_get_string_simple(dev, dev->iSerialNumber) : "no String");
308         printf("bNumConfigurations 0x%02X\n", dev->bNumConfigurations);
309
310         u8 c, i, e;
311         struct usb_conf *conf = dev->conf;
312         for(c=0; c <= dev->bNumConfigurations; c++) {
313                 printf("  === Configuration Descriptor %d ===\n", c+1);
314                 printf("  bLength 0x%02X\n", conf->bLength);
315                 printf("  bDescriptorType 0x%02X\n", conf->bDescriptorType);
316                 printf("  wTotalLength 0x%04X\n", conf->wTotalLength);
317                 printf("  bNumInterfaces 0x%02X\n", conf->bNumInterfaces);
318                 printf("  bConfigurationValue 0x%02X\n", conf->bConfigurationValue);
319                 printf("  iConfiguration (0x%02X): \"%s\"\n", conf->iConfiguration, conf->iConfiguration ? usb_get_string_simple(dev, conf->iConfiguration) : "no String");
320                 printf("  bmAttributes 0x%02X\n", conf->bmAttributes);
321                 printf("  bMaxPower 0x%02X\n", conf->bMaxPower);
322
323                 struct usb_intf *ifs = conf->intf;
324                 for(i=1; i <= conf->bNumInterfaces; i++) {
325                         printf("    === Interface Descriptor %d ===\n", i);
326                         printf("    bLength 0x%02X\n", ifs->bLength);
327                         printf("    bDescriptorType 0x%02X\n", ifs->bDescriptorType);
328                         printf("    bInterfaceNumber 0x%02X\n", ifs->bInterfaceNumber);
329                         printf("    bAlternateSetting 0x%02X\n", ifs->bAlternateSetting);
330                         printf("    bNumEndpoints 0x%02X\n", ifs->bNumEndpoints);
331                         printf("    bInterfaceClass 0x%02X\n", ifs->bInterfaceClass);
332                         printf("    bInterfaceSubClass 0x%02X\n", ifs->bInterfaceSubClass);
333                         printf("    bInterfaceProtocol 0x%02X\n", ifs->bInterfaceProtocol);
334                         printf("    iInterface (0x%02X): \"%s\"\n", ifs->iInterface, ifs->iInterface ? usb_get_string_simple(dev, ifs->iInterface) : "no String");
335
336                         struct usb_endp *ed = ifs->endp;
337                         for(e=1; e <= ifs->bNumEndpoints; e++) {
338                                 printf("      === Endpoint Descriptor %d ===\n", e);
339                                 printf("      bLength 0x%02X\n", ed->bLength);
340                                 printf("      bDescriptorType 0x%02X\n", ed->bDescriptorType);
341                                 printf("      bEndpointAddress 0x%02X\n", ed->bEndpointAddress);
342                                 printf("      bmAttributes 0x%02X\n", ed->bmAttributes);
343                                 printf("      wMaxPacketSize 0x%02X\n", ed->wMaxPacketSize);
344                                 printf("      bInterval 0x%02X\n", ed->bInterval);
345
346                                 ed = ed->next;
347                         } //endpoint
348
349                         ifs = ifs->next;
350                 } //interface
351
352                 conf = conf->next;
353         } //configuration
354 }
355
356 /**
357  * Find currently detached device and remove
358  * data structures
359  */
360 u8 usb_remove_device(struct usb_device * dev)
361 {
362         // FIXME!!!! dieser quatsch ist nur temporaer
363         free(core.devices->head);
364         free(core.devices);
365         core.devices = list_create();
366         return 1;
367 }
368
369 /**
370  * Register new driver at usb stack.
371  */
372 u8 usb_register_driver(struct usb_driver *dev)
373 {
374         /* add driver to driver list */
375         struct element *tmp = (struct element *) malloc(sizeof(struct element));
376         tmp->data = (void *) dev;
377         tmp->next = NULL;
378         list_add_tail(core.drivers, tmp);
379
380         /** 
381          * first check to find a suitable device 
382          * (root hub drivers need this call here)
383          */
384         dev->probe();
385
386         return 1;
387 }
388
389
390 /**
391  * Call every probe function from every registered
392  * driver, to check if there is a valid driver
393  * for the new device.  
394  */
395 void usb_probe_driver()
396 {
397         // call ever registered driver  
398         struct usb_driver *drv;
399         struct element *iterator = core.drivers->head;
400         while (iterator != NULL) {
401                 drv = (struct usb_driver *) iterator->data;
402                 drv->probe();
403                 iterator = iterator->next;
404         }
405 }
406
407 /**
408  * Not implemented.
409  */
410 struct usb_irp *usb_get_irp()
411 {
412         return 0;
413 }
414
415 /**
416  * Not implemented.
417  */
418 u8 usb_remove_irp(struct usb_irp *irp)
419 {
420
421         return 1;
422 }
423
424 /**
425  * Takes usb_irp and split it into
426  * several usb packeges (SETUP,IN,OUT)
427  * In the usbstack they are transported with the
428  * usb_transfer_descriptor data structure.
429  */
430 u16 usb_submit_irp(struct usb_irp *irp)
431 {
432         struct usb_transfer_descriptor *td;
433         u8 runloop = 1;
434         u16 restlength = irp->len;
435         u8 *td_buf_ptr = irp->buffer;
436         u8 mybuf[64];
437
438         u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
439
440         switch (irp->type) {
441         case USB_CTRL:
442                 /* alle requests mit dem gleichen algorithmus zerteilen
443                  * das einzige ist der spezielle get_Device_descriptor request
444                  * bei dem eine laenge von 64 angegeben ist.
445                  * wenn man an adresse 0 einen get_device_desciptor schickt
446                  * dann reichen die ersten 8 byte.
447                  */
448
449                 /***************** Setup Stage ***********************/
450                 td = usb_create_transfer_descriptor(irp);
451                 td->pid = USB_PID_SETUP;
452                 td->buffer = irp->buffer;
453
454                 /* control message are always 8 bytes */
455                 td->actlen = 8;
456
457                 togl = 0;
458                 /* start with data0 */
459                 td->togl = togl;
460                 togl = togl ? 0 : 1;
461
462                 /**** send token ****/
463                 hcdi_enqueue(td, irp->dev->ohci);
464
465                 /***************** Data Stage ***********************/
466                 /**
467                  * You can see at bit 7 of bmRequestType if this stage is used,
468                  * default requests are always 8 byte greate, from
469                  * host to device. Stage 3 is only neccessary if the request
470                  * expected datas from the device.
471                  * bit7 - 1 = from device to host -> yes we need data stage
472                  * bit7 - 0 = from host to device -> no send zero packet
473                  *
474                  * nach einem setup token kann nur ein IN token in stage 3 folgen
475                  * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
476                  *
477                  *
478                  * bit7 = 1
479                  *      Device to Host
480                  *      - es kommen noch Daten mit PID_IN an
481                  *      - host beendet mit PID_OUT DATA1 Zero
482                  * bit7 - 0
483                  *      Host zu Device (wie set address)
484                  *      - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
485                  */
486                 memcpy(mybuf, irp->buffer, td->actlen);
487                 usb_device_request *setup = (usb_device_request *) mybuf;
488                 u8 bmRequestType = setup->bmRequestType;
489                 free(td);
490
491                 /* check bit 7 of bmRequestType */
492                 if (bmRequestType & 0x80) { 
493                         /* schleife die die tds generiert */
494                         while (runloop && (restlength > 0)) {
495                                 td = usb_create_transfer_descriptor(irp);
496                                 td->actlen = irp->epsize;
497                                 /* stop loop if all bytes are send */
498                                 if (restlength < irp->epsize) {
499                                         runloop = 0;
500                                         td->actlen = restlength;
501                                 }
502
503                                 td->buffer = td_buf_ptr;
504                                 /* move pointer for next packet */
505                                 td_buf_ptr += irp->epsize;
506
507                                 td->pid = USB_PID_IN;
508                                 td->togl = togl;
509                                 togl = togl ? 0 : 1;
510
511                                 /* wenn device descriptor von adresse 0 angefragt wird werden nur
512                                  * die ersten 8 byte abgefragt
513                                  */
514                                 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
515                                                 && td->devaddress == 0) {
516                                         /* stop loop */
517                                         runloop = 0;
518                                 }
519
520                                 /**** send token ****/
521                                 hcdi_enqueue(td, irp->dev->ohci);
522
523                                 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
524                                 restlength = restlength - irp->epsize;
525                                 free(td);
526                         }
527                 }
528
529
530                 /***************** Status Stage ***********************/
531                 /* Zero packet for end */
532                 td = usb_create_transfer_descriptor(irp);
533                 td->togl = 1;                                                           /* zero data packet = always DATA1 packet */
534                 td->actlen = 0;
535                 td->buffer = NULL;
536
537                 /**
538                  * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
539                  * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
540                  */
541                 /* check bit 7 of bmRequestType */
542                 if (bmRequestType & 0x80) {
543                         td->pid = USB_PID_OUT;
544                 } else {
545                         td->pid = USB_PID_IN;
546                 }
547                 /**** send token ****/
548                 hcdi_enqueue(td, irp->dev->ohci);
549                 free(td);
550                 break;
551
552         case USB_BULK:
553                 //u8 runloop=1;
554                 //u16 restlength = irp->len;
555                 //char * td_buf_ptr=irp->buffer;
556
557                 /* schleife die die tds generiert */
558                 while (runloop) {
559                         td = usb_create_transfer_descriptor(irp);
560                         td->endpoint = td->endpoint & 0x7F;                             /* clear direction bit */
561
562                         /* max packet size for given endpoint */
563                         td->actlen = irp->epsize;
564
565                         /* Generate In Packet  */
566                         if (irp->endpoint & 0x80)
567                                 td->pid = USB_PID_IN;
568                         else
569                                 /* Generate Out Packet */
570                                 td->pid = USB_PID_OUT;
571
572                         /* stop loop if all bytes are send */
573                         if (restlength <= irp->epsize) {
574                                 runloop = 0;
575                                 td->actlen = restlength;
576                         }
577
578                         td->buffer = td_buf_ptr;
579                         /* move pointer for next packet */
580                         td_buf_ptr = td_buf_ptr + irp->epsize;
581
582                         td->togl = togl;
583                         togl = togl ? 0 : 1;
584                                 /**** send token ****/
585                         hcdi_enqueue(td, irp->dev->ohci);
586                         free(td);
587                 }
588                 /* next togl */
589                 //if(td->pid == USB_PID_OUT) {
590                 //if(togl==0) togl=1; else togl=0;
591                 //}
592                 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
593
594                 break;
595         
596         case USB_INTR:
597                 //u8 runloop=1;
598                 //u16 restlength = irp->len;
599                 //char * td_buf_ptr=irp->buffer;
600
601                 /* schleife die die tds generiert */
602                 while (runloop && (restlength > 0)) {
603                         td = usb_create_transfer_descriptor(irp);
604                         /* max packet size for given endpoint */
605                         td->actlen = irp->epsize;
606
607                         td->pid = USB_PID_IN;
608                         /* TODO: USB_PID_OUT */
609
610                         /* stop loop if all bytes are send */
611                         if (restlength < irp->epsize) {
612                                 runloop = 0;
613                                 td->actlen = restlength;
614                         }
615
616                         td->buffer = td_buf_ptr;
617                         /* move pointer for next packet */
618                         td_buf_ptr += irp->epsize;
619
620                         td->togl = togl;
621                         togl = togl ? 0 : 1;
622                                 
623                         /**** send token ****/
624                         hcdi_enqueue(td, irp->dev->ohci);
625                         restlength = restlength - irp->epsize;
626                         free(td);
627                 }
628                 break;
629                 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
630         }
631         hcdi_fire(irp->dev->ohci);
632
633         return 1;
634 }
635
636
637
638 /** 
639  * Create a transfer descriptor with an parent irp.
640  */
641 struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
642 {
643         struct usb_transfer_descriptor *td =
644                         (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
645
646         td->devaddress = irp->dev->address;
647         td->endpoint = irp->endpoint;
648         td->iso = 0;
649         td->state = USB_TRANSFER_DESCR_NONE;
650         td->maxp = irp->epsize;
651         td->fullspeed = irp->dev->fullspeed;
652         td->type = irp->type;
653
654         return td;
655 }
656