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