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