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