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