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