first attempt to get interrupt transfers working. epic fail :(
[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] = 64;
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("lololololool PADDING WTF :O lolololololo \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("lololololool PADDING WTF :O lolololololo \n");
127         printf("lololololool PADDING WTF :O lolololololo \n");
128         printf("lololololool PADDING WTF :O lolololololo \n");
129         printf("lololololool PADDING WTF :O lolololololo \n");
130         printf("lololololool PADDING WTF :O lolololololo \n");
131         printf("lololololool PADDING WTF :O lolololololo \n");
132         printf("lololol PADDING WTF :O lolololololo \n");
133         printf("lololololool PADDING WTF :O lolololololo \n");
134         printf("lolololool PADDING WTF :O lolololololo \n");
135         printf("lololololool PADDING WTF :O lolololololo \n");
136         printf("lolololool PADDING WTF :O lolololololo \n");
137         printf("lololololool PADDING WTF :O lolololololo \n");
138         printf("lollllool PADDING WTF :O lolololololo \n");
139         printf("lololololool PADDING WTF :O lolololololo \n");
140         printf("lololololool PADDING WTF :O lolololololo \n");
141         printf("lololololool PADDING WTF :O lolololololo \n");
142         printf("lololololool PADDING WTF :O lolololololo \n");
143         printf("lololololool PADDING WTF :O lolololololo \n");
144         printf("lololololool PADDING WTF :O lolololololo \n");
145         printf("lololololool PADDING WTF :O lolololololo \n");
146         printf("lololololool PADDING WTF :O lolololololo \n");
147         printf("lololololool PADDING WTF :O lolololololo \n");
148         printf("lololololool PADDING WTF :O lolololololo \n");
149 #endif
150         u8 address = usb_next_address();
151         ret = usb_set_address(dev, address);
152         dev->address = address;
153         printf("set address to %d\n", dev->address);
154
155         /* get device descriptor&co */
156         ret = usb_get_desc_dev(dev);
157         if(ret < 0)
158                 return (void*) -1;
159
160         /* print device info */
161         lsusb(dev);
162
163
164 #if 1
165         /* select configuration */
166         ret = usb_set_configuration(dev, 1);
167         printf("=============\nusb_set_configuration(ret: %d): %d\n", ret, dev->conf->bConfigurationValue);
168         printf("=============\nusb_get_configuration: %d (should be 1 here)\n", usb_get_configuration(dev));
169 #endif
170
171
172         u8 buf[8];
173 #if 1
174         /* select interface */
175         memset(buf, 0, 8);
176         printf("interfacenumber: %d\n", dev->conf->intf->bInterfaceNumber);
177         usb_control_msg(dev, 0x01, SET_INTERFACE, 0, 0, 0, buf, 0);
178         printf("=============\nusb_set_interface: %d\n", 0);
179         hexdump((void*)buf, 8);
180 #if 0
181         memset(buf, 0, 8);
182         usb_control_msg(dev, 0x81, GET_INTERFACE, 0, 0, 4, buf, 0);
183         printf("=============\nusb_get_interface: %d\n", buf[0]);
184         hexdump((void*)buf, 8);
185 #endif
186 #endif
187
188         /* I just don't know why on some devices 
189          * {s,g}et_{configuration,interface} won't work.
190          * may the setter works and getter are poorly implemented?
191          * however, I try here some interrupt inputs, assuming 
192          * the setters are fine*/
193
194         memset(buf, 0, 8);
195         s8 epnum = dev->conf->intf->endp->bEndpointAddress & 0xf;
196         printf("epnum: 0x%04X\n", epnum);
197         u8 muh = 10;
198         while(muh--) {
199                 (void) usb_interrupt_read(dev, epnum, buf, 8, 0);
200                 printf("============\nusb_interrupt_read:\n");
201                 hexdump((void*)buf, 8);
202                 udelay(2000000);
203         }
204
205 #if 0
206         /* add device to device list */
207         element *tmp = (element *) malloc(sizeof(element));
208         tmp->data = (void *) dev;
209         list_add_tail(core.devices, tmp);
210
211         usb_probe_driver();
212 #endif
213
214         return dev;
215 }
216
217 void lsusb(struct usb_device *dev)
218 {
219         printf("=== Device Descriptor === \n");
220         printf("bLength 0x%02X\n", dev->bLength);
221         printf("bDescriptorType 0x%02X\n", dev->bDeviceClass);
222         printf("bcdUSB 0x%04X\n", dev->bcdUSB);
223         printf("bDeviceClass 0x%02X\n", dev->bDeviceClass);
224         printf("bDeviceSubClass 0x%02X\n", dev->bDeviceSubClass);
225         printf("bDeviceProtocoll 0x%02X\n", dev->bDeviceProtocoll);
226         printf("bMaxPacketSize 0x%02X\n", dev->bMaxPacketSize0);
227         printf("idVendor 0x%04X\n", dev->idVendor);
228         printf("idProduct 0x%04X\n", dev->idProduct);
229         printf("bcdDevice 0x%04X\n", dev->bcdDevice);
230         printf("iManufacturer(0x%02X): \"%s\"\n", dev->iManufacturer, dev->iManufacturer ? usb_get_string_simple(dev, dev->iManufacturer) : "no String");
231         printf("iProduct(0x%02X): \"%s\"\n", dev->iProduct, dev->iProduct ? usb_get_string_simple(dev, dev->iProduct) : "no String");
232         printf("iSerialNumber(0x%02X): \"%s\"\n", dev->iSerialNumber, dev->iSerialNumber ? usb_get_string_simple(dev, dev->iSerialNumber) : "no String");
233         printf("bNumConfigurations 0x%02X\n", dev->bNumConfigurations);
234
235         u8 c, i, e;
236         struct usb_conf *conf = dev->conf;
237         for(c=0; c <= dev->bNumConfigurations; c++) {
238                 printf("  === Configuration Descriptor %d ===\n", c+1);
239                 printf("  bLength 0x%02X\n", conf->bLength);
240                 printf("  bDescriptorType 0x%02X\n", conf->bDescriptorType);
241                 printf("  wTotalLength 0x%04X\n", conf->wTotalLength);
242                 printf("  bNumInterfaces 0x%02X\n", conf->bNumInterfaces);
243                 printf("  bConfigurationValue 0x%02X\n", conf->bConfigurationValue);
244                 printf("  iConfiguration (0x%02X): \"%s\"\n", conf->iConfiguration, conf->iConfiguration ? usb_get_string_simple(dev, conf->iConfiguration) : "no String");
245                 printf("  bmAttributes 0x%02X\n", conf->bmAttributes);
246                 printf("  bMaxPower 0x%02X\n", conf->bMaxPower);
247
248                 struct usb_intf *ifs = conf->intf;
249                 for(i=1; i <= conf->bNumInterfaces; i++) {
250                         printf("    === Interface Descriptor %d ===\n", i);
251                         printf("    bLength 0x%02X\n", ifs->bLength);
252                         printf("    bDescriptorType 0x%02X\n", ifs->bDescriptorType);
253                         printf("    bInterfaceNumber 0x%02X\n", ifs->bInterfaceNumber);
254                         printf("    bAlternateSetting 0x%02X\n", ifs->bAlternateSetting);
255                         printf("    bNumEndpoints 0x%02X\n", ifs->bNumEndpoints);
256                         printf("    bInterfaceClass 0x%02X\n", ifs->bInterfaceClass);
257                         printf("    bInterfaceSubClass 0x%02X\n", ifs->bInterfaceSubClass);
258                         printf("    bInterfaceProtocol 0x%02X\n", ifs->bInterfaceProtocol);
259                         printf("    iInterface (0x%02X): \"%s\"\n", ifs->iInterface, ifs->iInterface ? usb_get_string_simple(dev, ifs->iInterface) : "no String");
260
261                         struct usb_endp *ed = ifs->endp;
262                         for(e=1; e <= ifs->bNumEndpoints; e++) {
263                                 printf("      === Endpoint Descriptor %d ===\n", e);
264                                 printf("      bLength 0x%02X\n", ed->bLength);
265                                 printf("      bDescriptorType 0x%02X\n", ed->bDescriptorType);
266                                 printf("      bEndpointAddress 0x%02X\n", ed->bEndpointAddress);
267                                 printf("      bmAttributes 0x%02X\n", ed->bmAttributes);
268                                 printf("      wMaxPacketSize 0x%02X\n", ed->wMaxPacketSize);
269                                 printf("      bInterval 0x%02X\n", ed->bInterval);
270
271                                 ed = ed->next;
272                         } //endpoint
273
274                         ifs = ifs->next;
275                 } //interface
276
277                 conf = conf->next;
278         } //configuration
279 }
280
281 /**
282  * Find currently detached device and remove
283  * data structures
284  */
285 u8 usb_remove_device(struct usb_device * dev)
286 {
287         // FIXME!!!! dieser quatsch ist nur temporaer
288         free(core.devices->head);
289         free(core.devices);
290         core.devices = list_create();
291         return 1;
292 }
293
294 /**
295  * Register new driver at usb stack.
296  */
297 u8 usb_register_driver(struct usb_driver * dev)
298 {
299         /* add driver to driver list */
300         struct element *tmp = (struct element *) malloc(sizeof(struct element));
301         tmp->data = (void *) dev;
302         tmp->next = NULL;
303         list_add_tail(core.drivers, tmp);
304
305
306         /** 
307          * first check to find a suitable device 
308          * (root hub drivers need this call here)
309          */
310         dev->probe();
311
312         return 1;
313 }
314
315
316 /**
317  * Call every probe function from every registered
318  * driver, to check if there is a valid driver
319  * for the new device.  
320  */
321 void usb_probe_driver()
322 {
323         // call ever registered driver  
324         struct usb_driver *drv;
325         struct element *iterator = core.drivers->head;
326         while (iterator != NULL) {
327                 drv = (struct usb_driver *) iterator->data;
328                 drv->probe();
329                 iterator = iterator->next;
330         }
331 }
332
333 /**
334  * Not implemented.
335  */
336 struct usb_irp *usb_get_irp()
337 {
338         return 0;
339 }
340
341 /**
342  * Not implemented.
343  */
344 u8 usb_remove_irp(struct usb_irp *irp)
345 {
346
347         return 1;
348 }
349
350 /**
351  * Takes usb_irp and split it into
352  * several usb packeges (SETUP,IN,OUT)
353  * In the usbstack they are transported with the
354  * usb_transfer_descriptor data structure.
355  */
356 u16 usb_submit_irp(struct usb_irp *irp)
357 {
358         struct usb_transfer_descriptor *td;
359         u8 runloop = 1;
360         u16 restlength = irp->len;
361         u8 *td_buf_ptr = irp->buffer;
362         u8 mybuf[64];
363
364         u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
365
366         switch (irp->type) {
367         case USB_CTRL:
368                 /* alle requests mit dem gleichen algorithmus zerteilen
369                  * das einzige ist der spezielle get_Device_descriptor request
370                  * bei dem eine laenge von 64 angegeben ist.
371                  * wenn man an adresse 0 einen get_device_desciptor schickt
372                  * dann reichen die ersten 8 byte.
373                  */
374
375                 /***************** Setup Stage ***********************/
376                 td = usb_create_transfer_descriptor(irp);
377                 td->pid = USB_PID_SETUP;
378                 td->buffer = irp->buffer;
379
380                 /* control message are always 8 bytes */
381                 td->actlen = 8;
382
383                 togl = 0;
384                 /* start with data0 */
385                 td->togl = togl;
386                 togl = togl ? 0 : 1;
387
388                 /**** send token ****/
389                 hcdi_enqueue(td, irp->dev->ohci);
390
391                 /***************** Data Stage ***********************/
392                 /**
393                  * You can see at bit 7 of bmRequestType if this stage is used,
394                  * default requests are always 8 byte greate, from
395                  * host to device. Stage 3 is only neccessary if the request
396                  * expected datas from the device.
397                  * bit7 - 1 = from device to host -> yes we need data stage
398                  * bit7 - 0 = from host to device -> no send zero packet
399                  *
400                  * nach einem setup token kann nur ein IN token in stage 3 folgen
401                  * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
402                  *
403                  *
404                  * bit7 = 1
405                  *      Device to Host
406                  *      - es kommen noch Daten mit PID_IN an
407                  *      - host beendet mit PID_OUT DATA1 Zero
408                  * bit7 - 0
409                  *      Host zu Device (wie set address)
410                  *      - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
411                  */
412                 memcpy(mybuf, irp->buffer, td->actlen);
413                 usb_device_request *setup = (usb_device_request *) mybuf;
414                 u8 bmRequestType = setup->bmRequestType;
415                 free(td);
416
417                 /* check bit 7 of bmRequestType */
418                 if (bmRequestType & 0x80) { 
419                         /* schleife die die tds generiert */
420                         while (runloop && (restlength > 0)) {
421                                 td = usb_create_transfer_descriptor(irp);
422                                 td->actlen = irp->epsize;
423                                 /* stop loop if all bytes are send */
424                                 if (restlength < irp->epsize) {
425                                         runloop = 0;
426                                         td->actlen = restlength;
427                                 }
428
429                                 td->buffer = td_buf_ptr;
430                                 /* move pointer for next packet */
431                                 td_buf_ptr += irp->epsize;
432
433                                 td->pid = USB_PID_IN;
434                                 td->togl = togl;
435                                 togl = togl ? 0 : 1;
436
437                                 /* wenn device descriptor von adresse 0 angefragt wird werden nur
438                                  * die ersten 8 byte abgefragt
439                                  */
440                                 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
441                                                 && td->devaddress == 0) {
442                                         /* stop loop */
443                                         runloop = 0;
444                                 }
445
446                                 /**** send token ****/
447                                 hcdi_enqueue(td, irp->dev->ohci);
448
449                                 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
450                                 restlength = restlength - irp->epsize;
451                                 free(td);
452                         }
453                 }
454
455
456                 /***************** Status Stage ***********************/
457                 /* Zero packet for end */
458                 td = usb_create_transfer_descriptor(irp);
459                 td->togl = 1;                                                           /* zero data packet = always DATA1 packet */
460                 td->actlen = 0;
461                 td->buffer = NULL;
462
463                 /**
464                  * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
465                  * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
466                  */
467                 /* check bit 7 of bmRequestType */
468                 if (bmRequestType & 0x80) {
469                         td->pid = USB_PID_OUT;
470                 } else {
471                         td->pid = USB_PID_IN;
472                 }
473                 /**** send token ****/
474                 hcdi_enqueue(td, irp->dev->ohci);
475                 free(td);
476                 break;
477
478         case USB_BULK:
479                 //u8 runloop=1;
480                 //u16 restlength = irp->len;
481                 //char * td_buf_ptr=irp->buffer;
482
483                 /* schleife die die tds generiert */
484                 while (runloop) {
485                         td = usb_create_transfer_descriptor(irp);
486                         td->endpoint = td->endpoint & 0x7F;                             /* clear direction bit */
487
488                         /* max packet size for given endpoint */
489                         td->actlen = irp->epsize;
490
491                         /* Generate In Packet  */
492                         if (irp->endpoint & 0x80)
493                                 td->pid = USB_PID_IN;
494                         else
495                                 /* Generate Out Packet */
496                                 td->pid = USB_PID_OUT;
497
498                         /* stop loop if all bytes are send */
499                         if (restlength <= irp->epsize) {
500                                 runloop = 0;
501                                 td->actlen = restlength;
502                         }
503
504                         td->buffer = td_buf_ptr;
505                         /* move pointer for next packet */
506                         td_buf_ptr = td_buf_ptr + irp->epsize;
507
508                         td->togl = togl;
509                         togl = togl ? 0 : 1;
510                                 /**** send token ****/
511                         hcdi_enqueue(td, irp->dev->ohci);
512                         free(td);
513                 }
514                 /* next togl */
515                 //if(td->pid == USB_PID_OUT) {
516                 //if(togl==0) togl=1; else togl=0;
517                 //}
518                 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
519
520                 break;
521         
522         case USB_INTR:
523                 //u8 runloop=1;
524                 //u16 restlength = irp->len;
525                 //char * td_buf_ptr=irp->buffer;
526
527                 /* schleife die die tds generiert */
528                 while (runloop && (restlength > 0)) {
529                         td = usb_create_transfer_descriptor(irp);
530                         /* max packet size for given endpoint */
531                         td->actlen = irp->epsize;
532
533                         td->pid = USB_PID_IN;
534                         /* TODO: USB_PID_OUT */
535
536                         /* stop loop if all bytes are send */
537                         if (restlength < irp->epsize) {
538                                 runloop = 0;
539                                 td->actlen = restlength;
540                         }
541
542                         td->buffer = td_buf_ptr;
543                         /* move pointer for next packet */
544                         td_buf_ptr += irp->epsize;
545
546                         td->togl = togl;
547                         togl = togl ? 0 : 1;
548                                 
549                                 /**** send token ****/
550                         hcdi_enqueue(td, irp->dev->ohci);
551                         restlength = restlength - irp->epsize;
552                         free(td);
553                 }
554                 break;
555                 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
556         }
557         hcdi_fire(irp->dev->ohci);
558
559         return 1;
560 }
561
562
563
564 /** 
565  * Create a transfer descriptor with an parent irp.
566  */
567 struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
568 {
569         struct usb_transfer_descriptor *td =
570                         (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
571
572         td->devaddress = irp->dev->address;
573         td->endpoint = irp->endpoint;
574         td->iso = 0;
575         td->state = USB_TRANSFER_DESCR_NONE;
576         td->maxp = irp->epsize;
577         td->fullspeed = irp->dev->fullspeed;
578         td->type = irp->type;
579
580         return td;
581 }
582