weird ISI exception bug, WZF
[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("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
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
264         switch (irp->type) {
265         case USB_CTRL:
266                 /* alle requests mit dem gleichen algorithmus zerteilen
267                  * das einzige ist der spezielle get_Device_descriptor request
268                  * bei dem eine laenge von 64 angegeben ist.
269                  * wenn man an adresse 0 einen get_device_desciptor schickt
270                  * dann reichen die ersten 8 byte.
271                  */
272
273                 /***************** Setup Stage ***********************/
274                 td = usb_create_transfer_descriptor(irp);
275                 td->pid = USB_PID_SETUP;
276                 td->buffer = irp->buffer;
277
278                 /* control message are always 8 bytes */
279                 td->actlen = 8;
280                 memcpy(mybuf, td->buffer, td->actlen);
281
282                 togl = 0;
283                 /* start with data0 */
284                 td->togl = togl;
285                 togl = togl ? 0 : 1;
286
287                 /**** send token ****/
288                 hcdi_enqueue(td);
289
290                 /***************** Data Stage ***********************/
291                 /**
292                  * You can see at bit 7 of bmRequestType if this stage is used,
293                  * default requests are always 8 byte greate, from
294                  * host to device. Stage 3 is only neccessary if the request
295                  * expected datas from the device.
296                  * bit7 - 1 = from device to host -> yes we need data stage
297                  * bit7 - 0 = from host to device -> no send zero packet
298                  *
299                  * nach einem setup token kann nur ein IN token in stage 3 folgen
300                  * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt.
301                  *
302                  *
303                  * bit7 = 1
304                  *      Device to Host
305                  *      - es kommen noch Daten mit PID_IN an
306                  *      - host beendet mit PID_OUT DATA1 Zero
307                  * bit7 - 0
308                  *      Host zu Device (wie set address)
309                  *      - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
310                  */
311                 memcpy(mybuf, irp->buffer, td->actlen);
312                 usb_device_request *setup = (usb_device_request *) mybuf;
313                 u8 bmRequestType = setup->bmRequestType;
314                 free(td);
315
316                 /* check bit 7 of bmRequestType */
317                 if (bmRequestType & 0x80) { 
318                         /* schleife die die tds generiert */
319                         while (runloop) {
320                                 td = usb_create_transfer_descriptor(irp);
321                                 td->actlen = irp->epsize;
322                                 /* stop loop if all bytes are send */
323                                 if (restlength <= irp->epsize) {
324                                         runloop = 0;
325                                         td->actlen = restlength;
326                                 }
327
328                                 td->buffer = td_buf_ptr;
329                                 /* move pointer for next packet */
330                                 td_buf_ptr = td_buf_ptr + irp->epsize;
331
332                                 td->pid = USB_PID_IN;
333                                 td->togl = togl;
334                                 togl = togl ? 0 : 1;
335
336                                 /* wenn device descriptor von adresse 0 angefragt wird werden nur
337                                  * die ersten 8 byte abgefragt
338                                  */
339                                 if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1
340                                                 && td->devaddress == 0) {
341                                         /* stop loop */
342                                         runloop = 0;
343                                 }
344
345                                 /**** send token ****/
346                                 hcdi_enqueue(td);
347
348                                 /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */
349                                 restlength = restlength - irp->epsize;
350                                 free(td);
351                         }
352                 }
353
354
355                 /***************** Status Stage ***********************/
356                 /* Zero packet for end */
357                 td = usb_create_transfer_descriptor(irp);
358                 td->togl = 1;                                                           /* zero data packet = always DATA1 packet */
359                 td->actlen = 0;
360                 td->buffer = NULL;
361
362                 /**
363                  * bit7 = 1, host beendet mit PID_OUT DATA1 Zero
364                  * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung
365                  */
366                 /* check bit 7 of bmRequestType */
367                 if (bmRequestType & 0x80) {
368                         td->pid = USB_PID_OUT;
369                 } else {
370                         td->pid = USB_PID_IN;
371                 }
372                 /**** send token ****/
373                 hcdi_enqueue(td);
374                 free(td);
375                 break;
376
377         case USB_BULK:
378                 core.stdout("bulk\r\n");
379                 //u8 runloop=1;
380                 //u16 restlength = irp->len;
381                 //char * td_buf_ptr=irp->buffer;
382
383                 /* schleife die die tds generiert */
384                 while (runloop) {
385
386                         td = usb_create_transfer_descriptor(irp);
387                         td->endpoint = td->endpoint & 0x7F;                             /* clear direction bit */
388
389                         /* max packet size for given endpoint */
390                         td->actlen = irp->epsize;
391
392                         /* Generate In Packet  */
393                         if (irp->endpoint & 0x80)
394                                 td->pid = USB_PID_IN;
395                         else
396                                 /* Generate Out Packet */
397                                 td->pid = USB_PID_OUT;
398
399                         /* stop loop if all bytes are send */
400                         if (restlength <= irp->epsize) {
401                                 runloop = 0;
402                                 td->actlen = restlength;
403                         }
404
405                         td->buffer = td_buf_ptr;
406                         /* move pointer for next packet */
407                         td_buf_ptr = td_buf_ptr + irp->epsize;
408
409                         td->togl = togl;
410                         if (togl == 0)
411                                 togl = 1;
412                         else
413                                 togl = 0;
414                                 /**** send token ****/
415                         hcdi_enqueue(td);
416                         free(td);
417                 }
418                 /* next togl */
419                 //if(td->pid == USB_PID_OUT) {
420                 //if(togl==0) togl=1; else togl=0;
421                 //}
422                 irp->dev->epTogl[(irp->endpoint & 0x7F)] = togl;
423
424                 break;
425         }
426         hcdi_fire();
427
428         return 1;
429 }
430
431
432
433 /** 
434  * Create a transfer descriptor with an parent irp.
435  */
436 usb_transfer_descriptor *usb_create_transfer_descriptor(usb_irp * irp)
437 {
438         usb_transfer_descriptor *td =
439                         (usb_transfer_descriptor *) malloc(sizeof(usb_transfer_descriptor));
440
441         td->devaddress = irp->dev->address;
442         td->endpoint = irp->endpoint;
443         td->iso = 0;
444         td->state = USB_TRANSFER_DESCR_NONE;
445         td->maxp = irp->epsize;
446
447         return td;
448 }
449