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