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