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