further refactoring; usb_get_desc_interfac doesn't work yet
[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 #define LEN 128
106         u8 buf[LEN];
107         memset(buf, 0, sizeof(buf));
108
109         s8 ret;
110         memset(buf, 0, sizeof(buf));
111         ret = usb_get_desc_dev_simple(dev, buf, sizeof(buf));
112 #ifdef _DU_CORE_ADD
113         printf("=============\nbuf: 0x%08X\nafter usb_get_dev_desc_simple(ret: %d):\n", buf, ret);
114         hexdump(buf, sizeof(buf));
115 #endif
116         if(ret < 0) {
117                 return (void*) -1;
118         }
119
120         /* set MaxPacketSize */
121
122         u8 address = usb_next_address();
123         ret = usb_set_address(dev, address);
124         dev->address = address;
125 #ifdef _DU_CORE_ADD
126         printf("set address to %d\n", dev->address);
127 #endif
128
129         memset(buf, 0, sizeof(buf));
130         ret = usb_get_desc_dev(dev, buf, sizeof(buf));
131 #ifdef _DU_CORE_ADD
132         printf("=============\nbuf: 0x%08X\nafter usb_get_dev_desc(ret: %d):\n", buf, ret);
133         hexdump(buf, sizeof(buf));
134 #endif
135
136         char *man, *prod, *serial;
137         if(dev->iManufacturer) {
138                 memset(buf, 0, sizeof(buf));
139                 man = usb_get_string_simple(dev, dev->iManufacturer, buf, sizeof(buf));
140         } else {
141                 man = (char*) malloc(11);
142                 memset(man, '\0', sizeof(man));
143                 strlcpy(man, "no String", 10);
144         }
145         if(dev->iProduct) {
146                 memset(buf, 0, sizeof(buf));
147                 prod = usb_get_string_simple(dev, dev->iProduct, buf, sizeof(buf));
148         } else {
149                 prod = (char*) malloc(11);
150                 memset(prod, '\0', sizeof(prod));
151                 strlcpy(prod, "no String", 10);
152         }
153         if(dev->iSerialNumber) {
154                 memset(buf, 0, sizeof(buf));
155                 serial = usb_get_string_simple(dev, dev->iSerialNumber, buf, sizeof(buf));
156         } else {
157                 serial = (char*) malloc(11);
158                 memset(serial, '\0', sizeof(serial));
159                 strlcpy(serial, "no String", 10);
160         }
161
162
163         printf( "bLength 0x%02X\n"
164                         "bDescriptorType 0x%02X\n"
165                         "bcdUSB 0x%02X\n"
166                         "bDeviceClass 0x%02X\n"
167                         "bDeviceSubClass 0x%02X\n"
168                         "bDeviceProtocoll 0x%02X\n"
169                         "idVendor 0x%04X\n"
170                         "idProduct 0x%04X\n"
171                         "bcdDevice 0x%04X\n"
172                         "iManufacturer(0x%02X): \"%s\"\n"
173                         "iProduct(0x%02X): \"%s\"\n"
174                         "iSerialNumber(0x%02X): \"%s\"\n"
175                         "bNumConfigurations 0x%02X\n", dev->bLength, dev->bDeviceClass,
176                         dev->bcdUSB, dev->bDescriptorType, dev->bDeviceSubClass, 
177                         dev->bDeviceProtocoll, dev->idVendor, dev->idProduct, dev->bcdDevice, 
178                         dev->iManufacturer, man,
179                         dev->iProduct, prod,
180                         dev->iSerialNumber, serial,
181                         dev->bNumConfigurations);
182
183         memset(buf, 0, sizeof(buf));
184         /* in the most cases usb devices have just one configuration descriptor */
185         ret = usb_get_desc_configuration(dev, 0, buf, sizeof(buf));
186         printf("=============\nbuf: 0x%08X\nafter usb_get_desc_configuration(ret: %d):\n", buf, ret);
187         hexdump(buf, sizeof(buf));
188
189         printf("interfaces: %d\n", dev->conf->bNumInterfaces);
190         u8 i;
191         for(i = 1; i <= dev->conf->bNumInterfaces; i++) {
192                 memset(buf, 0, sizeof(buf));
193                 ret = usb_get_desc_interface(dev, i, buf, sizeof(buf));
194                 printf("=============\nbuf: 0x%08X\nafter usb_get_desc_interface_%d(ret: %d):\n", buf, i, ret);
195                 hexdump(buf, sizeof(buf));
196         }
197
198
199
200         /*
201         usb_get_descriptor(dev, DEVICE, 0, buf, 8);
202         memset(buf, 0, 8);
203         usb_get_descriptor(dev, DEVICE, 0, buf, size >= buf[0] ? buf[0] : size);
204         */
205 #if 0
206         memset(buf, 0, sizeof(buf));
207         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, 8, buf, 0);
208         printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
209         hexdump(buf, sizeof(buf));
210
211         memset(buf, 0, sizeof(buf));
212         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, buf[0], buf, 0);
213         printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
214         hexdump(buf, sizeof(buf));
215
216         memset(buf, 0, sizeof(buf));
217         usb_get_string_simple(dev, 1, buf);
218         printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
219         hexdump(buf, sizeof(buf));
220 #endif
221
222 #if 0
223         u8 devdescr_size;
224
225         /* setup real ep0 fifo size */
226         dev->bMaxPacketSize0 = (u8) buf[7];
227         if(!(u8)buf[7]) {
228                 printf("FU\n");
229                 return (void*)1;
230         }
231
232         /* save real length of device descriptor */
233         devdescr_size = (u8) buf[0];
234
235         /* define new adress */
236         memset(buf, 0, sizeof(buf));
237         usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 8, 0);
238         dev->address = address;
239         printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
240         hexdump(buf, sizeof(buf));
241         printf("address: %d\n", address);
242
243
244         /* get complete device descriptor */
245         memset(buf, 0, sizeof(buf));
246         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE<<8, 0, devdescr_size, buf, 8, 0);
247
248         printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
249         hexdump(buf, sizeof(buf));
250
251         /* save only really neccessary values for this small usbstack */
252 #endif
253
254 #if 0
255         memset(buf, 0, sizeof(buf));
256         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8)|2, 0, 0x1a, buf, 8, 0);
257         hexdump(buf, sizeof(buf));
258         printf("String Descriptor [1]: ");
259         u8 i;
260         for (i=2; i<buf[0]; i+=2)
261                 printf("%c", buf[i]);
262         printf("\n");
263 #endif
264
265         /*
266         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8) | 2, 0, 0x20, buf, 8, 0);
267         printf("String Descriptor [2]: ");
268         for (i=2; i<buf[0]; i+=2)
269                 printf("%c", buf[i]);
270         printf("\n");
271         */
272
273         // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit 
274         // entprechenden funktionen
275         // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
276
277 #if 0
278         /* add device to device list */
279         element *tmp = (element *) malloc(sizeof(element));
280         tmp->data = (void *) dev;
281         list_add_tail(core.devices, tmp);
282
283         usb_probe_driver();
284 #endif
285
286         return dev;
287 }
288
289 /**
290  * Find currently detached device and remove
291  * data structures
292  */
293 u8 usb_remove_device(struct usb_device * dev)
294 {
295         // FIXME!!!! dieser quatsch ist nur temporaer
296         free(core.devices->head);
297         free(core.devices);
298         core.devices = list_create();
299         return 1;
300 }
301
302 /**
303  * Register new driver at usb stack.
304  */
305 u8 usb_register_driver(struct usb_driver * dev)
306 {
307         /* add driver to driver list */
308         struct element *tmp = (struct element *) malloc(sizeof(struct element));
309         tmp->data = (void *) dev;
310         tmp->next = NULL;
311         list_add_tail(core.drivers, tmp);
312
313
314         /** 
315          * first check to find a suitable device 
316          * (root hub drivers need this call here)
317          */
318         dev->probe();
319
320         return 1;
321 }
322
323
324 /**
325  * Call every probe function from every registered
326  * driver, to check if there is a valid driver
327  * for the new device.  
328  */
329 void usb_probe_driver()
330 {
331         // call ever registered driver  
332         struct usb_driver *drv;
333         struct element *iterator = core.drivers->head;
334         while (iterator != NULL) {
335                 drv = (struct usb_driver *) iterator->data;
336                 drv->probe();
337                 iterator = iterator->next;
338         }
339 }
340
341 /**
342  * Not implemented.
343  */
344 struct usb_irp *usb_get_irp()
345 {
346         return 0;
347 }
348
349 /**
350  * Not implemented.
351  */
352 u8 usb_remove_irp(struct usb_irp *irp)
353 {
354
355         return 1;
356 }
357
358 /**
359  * Takes usb_irp and split it into
360  * several usb packeges (SETUP,IN,OUT)
361  * In the usbstack they are transported with the
362  * usb_transfer_descriptor data structure.
363  */
364 u16 usb_submit_irp(struct usb_irp *irp)
365 {
366         struct usb_transfer_descriptor *td;
367         u8 runloop = 1;
368         u16 restlength = irp->len;
369         u8 *td_buf_ptr = irp->buffer;
370         u8 mybuf[64];
371
372         u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)];
373
374         switch (irp->type) {
375         case USB_CTRL:
376                 /* alle requests mit dem gleichen algorithmus zerteilen
377                  * das einzige ist der spezielle get_Device_descriptor request
378                  * bei dem eine laenge von 64 angegeben ist.
379                  * wenn man an adresse 0 einen get_device_desciptor schickt
380                  * dann reichen die ersten 8 byte.
381                  */
382
383                 /***************** Setup Stage ***********************/
384                 td = usb_create_transfer_descriptor(irp);
385                 td->pid = USB_PID_SETUP;
386                 td->buffer = irp->buffer;
387
388                 /* control message are always 8 bytes */
389                 td->actlen = 8;
390
391                 togl = 0;
392                 /* start with data0 */
393                 td->togl = togl;
394                 togl = togl ? 0 : 1;
395
396                 /**** send token ****/
397                 hcdi_enqueue(td);
398
399                 /***************** Data Stage ***********************/
400                 /**
401                  * You can see at bit 7 of bmRequestType if this stage is used,
402                  * default requests are always 8 byte greate, from
403                  * host to device. Stage 3 is only neccessary if the request
404                  * expected datas from the device.
405                  * bit7 - 1 = from device to host -> yes we need data stage
406                  * bit7 - 0 = from host to device -> no send zero packet
407                  *
408                  * nach einem setup token kann nur ein IN token in stage 3 folgen
409                  * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
410                  *
411                  *
412                  * bit7 = 1
413                  *      Device to Host
414                  *      - es kommen noch Daten mit PID_IN an
415                  *      - host beendet mit PID_OUT DATA1 Zero
416                  * bit7 - 0
417                  *      Host zu Device (wie set address)
418                  *      - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
419                  */
420                 memcpy(mybuf, irp->buffer, td->actlen);
421                 usb_device_request *setup = (usb_device_request *) mybuf;
422                 u8 bmRequestType = setup->bmRequestType;
423                 free(td);
424
425                 /* check bit 7 of bmRequestType */
426                 if (bmRequestType & 0x80) { 
427                         /* schleife die die tds generiert */
428                         while (runloop && (restlength > 0)) {
429                                 td = usb_create_transfer_descriptor(irp);
430                                 td->actlen = irp->epsize;
431                                 /* stop loop if all bytes are send */
432                                 if (restlength < irp->epsize) {
433                                         runloop = 0;
434                                         td->actlen = restlength;
435                                 }
436
437                                 td->buffer = td_buf_ptr;
438                                 /* move pointer for next packet */
439                                 td_buf_ptr += irp->epsize;
440
441                                 td->pid = USB_PID_IN;
442                                 td->togl = togl;
443                                 togl = togl ? 0 : 1;
444
445                                 /* wenn device descriptor von adresse 0 angefragt wird werden nur
446                                  * die ersten 8 byte abgefragt
447                                  */
448                                 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
449                                                 && td->devaddress == 0) {
450                                         /* stop loop */
451                                         runloop = 0;
452                                 }
453
454                                 /**** send token ****/
455                                 hcdi_enqueue(td);
456
457                                 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
458                                 restlength = restlength - irp->epsize;
459                                 free(td);
460                         }
461                 }
462
463
464                 /***************** Status Stage ***********************/
465                 /* Zero packet for end */
466                 td = usb_create_transfer_descriptor(irp);
467                 td->togl = 1;                                                           /* zero data packet = always DATA1 packet */
468                 td->actlen = 0;
469                 td->buffer = NULL;
470
471                 /**
472                  * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
473                  * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
474                  */
475                 /* check bit 7 of bmRequestType */
476                 if (bmRequestType & 0x80) {
477                         td->pid = USB_PID_OUT;
478                 } else {
479                         td->pid = USB_PID_IN;
480                 }
481                 /**** send token ****/
482                 hcdi_enqueue(td);
483                 free(td);
484                 break;
485
486         case USB_BULK:
487                 core.stdout("bulk\r\n");
488                 //u8 runloop=1;
489                 //u16 restlength = irp->len;
490                 //char * td_buf_ptr=irp->buffer;
491
492                 /* schleife die die tds generiert */
493                 while (runloop) {
494
495                         td = usb_create_transfer_descriptor(irp);
496                         td->endpoint = td->endpoint & 0x7F;                             /* clear direction bit */
497
498                         /* max packet size for given endpoint */
499                         td->actlen = irp->epsize;
500
501                         /* Generate In Packet  */
502                         if (irp->endpoint & 0x80)
503                                 td->pid = USB_PID_IN;
504                         else
505                                 /* Generate Out Packet */
506                                 td->pid = USB_PID_OUT;
507
508                         /* stop loop if all bytes are send */
509                         if (restlength <= irp->epsize) {
510                                 runloop = 0;
511                                 td->actlen = restlength;
512                         }
513
514                         td->buffer = td_buf_ptr;
515                         /* move pointer for next packet */
516                         td_buf_ptr = td_buf_ptr + irp->epsize;
517
518                         td->togl = togl;
519                         if (togl == 0)
520                                 togl = 1;
521                         else
522                                 togl = 0;
523                                 /**** send token ****/
524                         hcdi_enqueue(td);
525                         free(td);
526                 }
527                 /* next togl */
528                 //if(td->pid == USB_PID_OUT) {
529                 //if(togl==0) togl=1; else togl=0;
530                 //}
531                 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
532
533                 break;
534         }
535         hcdi_fire();
536
537         return 1;
538 }
539
540
541
542 /** 
543  * Create a transfer descriptor with an parent irp.
544  */
545 struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
546 {
547         struct usb_transfer_descriptor *td =
548                         (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
549
550         td->devaddress = irp->dev->address;
551         td->endpoint = irp->endpoint;
552         td->iso = 0;
553         td->state = USB_TRANSFER_DESCR_NONE;
554         td->maxp = irp->epsize;
555
556         return td;
557 }
558