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