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