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