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