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