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