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