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