5c191e7814f017d61b2531f31b0ac04d08def4c8
[ppcskel.git] / usb / core / usb.c
1 /*
2  * Copyright (c) 2007, Benedikt Sauter <sauter@ixbat.de>
3  * All rights reserved.
4  *
5  * Short descripton of file:
6  * I take the function names and parameters mainly from
7  * libusb.sf.net.
8  *
9  *
10  * Redistribution and use in source and binary forms, with or without 
11  * modification, are permitted provided that the following conditions 
12  * are met:
13  *
14  *   * Redistributions of source code must retain the above copyright 
15  *     notice, this list of conditions and the following disclaimer.
16  *   * Redistributions in binary form must reproduce the above 
17  *     copyright notice, this list of conditions and the following 
18  *     disclaimer in the documentation and/or other materials provided 
19  *     with the distribution.
20  *   * Neither the name of the FH Augsburg nor the names of its 
21  *     contributors may be used to endorse or promote products derived 
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
27  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
28  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
34  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include "usb.h"
38 #include "core.h"
39 #include "../host/host.h"
40 #include "../usbspec/usb11spec.h"
41 #include "../../malloc.h"
42 #include "../../string.h"
43
44 #define cleargbuf() memset(gbuf, 0, 0xffff)
45 /* internal global buffer */
46 static u8 gbuf[0xffff];
47
48 /******************* Device Operations **********************/
49 /**
50  * Open a device with verndor- and product-id for a communication.
51  */
52 struct usb_device *usb_open(u32 vendor_id, u32 product_id)
53 {
54         struct usb_device* dev;
55         struct element * iterator = core.devices->head;
56         while(iterator != NULL) {
57                 dev = (struct usb_device*)iterator->data;
58                 
59                 if(dev->idVendor==vendor_id&&dev->idProduct==product_id)
60                         return dev;
61
62                 iterator=iterator->next;
63         }
64
65         return NULL;
66 }
67
68
69 /**
70  * Open a device with an class code for a communication.
71  */
72 struct usb_device *usb_open_class(u8 class)
73 {
74         struct usb_device* dev;
75         struct element * iterator = core.devices->head;
76         while(iterator != NULL) {
77                 dev = (struct usb_device*)iterator->data;
78                 
79                 if(dev->bDeviceClass==class)
80                         return dev;
81
82                 iterator=iterator->next;
83         }
84         return NULL;
85 }
86
87 /* Close device after a communication.
88  */
89 s8 usb_close(struct usb_device *dev)
90 {
91
92         return 0;
93 }
94
95 s8 usb_reset(struct usb_device *dev)
96 {
97
98
99         return 0;
100 }
101
102
103 /******************* Control Transfer **********************/
104 /**
105  * Create a control transfer.
106  */
107 s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
108                 u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
109 {
110         struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
111         irp->dev = dev;
112         irp->endpoint = 0;
113
114         irp->epsize = dev->bMaxPacketSize0;
115         irp->type = USB_CTRL;
116
117         buf[0]=(u8)requesttype;
118         buf[1]=(u8)request;
119         buf[2]=(u8)(value);
120         buf[3]=(u8)(value >> 8);
121         buf[4]=(u8)(index);
122         buf[5]=(u8)(index >> 8);
123         buf[6]=(u8)(length);
124         buf[7]=(u8)(length >> 8);
125
126         irp->buffer = buf;
127         irp->len = length;
128         irp->timeout = timeout;
129
130         usb_submit_irp(irp);
131         free(irp);
132
133         return 0;
134 }
135
136 s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
137 {
138         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
139         return 0;
140 }
141
142 s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid)
143 {
144
145         return 0;
146 }
147
148 char *usb_get_string_simple(struct usb_device *dev, u8 index)
149 {
150         cleargbuf();
151         usb_get_descriptor(dev, STRING, index, gbuf, (u8) 8);
152         usb_get_descriptor(dev, STRING, index, gbuf, gbuf[0]);
153
154         char *str = (char*)malloc((gbuf[0]/2));
155         memset(str, '\0', (gbuf[0]/2));
156
157         u16 i;
158         for(i=0; i<(gbuf[0]/2)-1; i++) {
159                 str[i] = gbuf[2+(i*2)];
160         }
161
162         return str;
163 }
164
165 /* ask first 8 bytes of device descriptor with this special 
166  * GET Descriptor Request, when device address = 0
167  */
168 s8 usb_get_desc_dev_simple(struct usb_device *dev)
169 {
170         cleargbuf();
171         usb_get_descriptor(dev, DEVICE, 0, gbuf, 8);
172
173         if(!gbuf[7]) {
174                 printf("FU: %d\n", gbuf[7]);
175                 return -2;
176         }
177         dev->bMaxPacketSize0 = gbuf[7];
178         return 0;
179 }
180
181 s8 usb_get_desc_dev(struct usb_device *dev)
182 {
183         cleargbuf();
184         if (usb_get_desc_dev_simple(dev) < 0) {
185                 return -1;
186         }
187         usb_get_descriptor(dev, DEVICE, 0, gbuf, gbuf[0]);
188
189         dev->bLength = gbuf[0];
190         dev->bDescriptorType = gbuf[1];
191         dev->bcdUSB = (u16) (gbuf[3] << 8 | gbuf[2]);
192         dev->bDeviceClass = gbuf[4];
193         dev->bDeviceSubClass = gbuf[5];
194         dev->bDeviceProtocoll = gbuf[6];
195         dev->idVendor = (u16) (gbuf[9] << 8) | (gbuf[8]);
196         dev->idProduct = (u16) (gbuf[11] << 8) | (gbuf[10]);
197         dev->bcdDevice = (u16) (gbuf[13] << 8) | (gbuf[12]);
198         dev->iManufacturer = gbuf[14];
199         dev->iProduct = gbuf[15];
200         dev->iSerialNumber = gbuf[16];
201         dev->bNumConfigurations = gbuf[17];
202
203         u8 i;
204         struct usb_conf *conf = dev->conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
205         for(i=0; i <= dev->bNumConfigurations; i++) {
206                 if(i!=0) {
207                         conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
208                 }
209                 usb_get_desc_config_ext(dev, i, conf);
210         }
211
212         return 0;
213 }
214
215 s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, struct usb_conf *conf)
216 {
217         cleargbuf();
218         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, 8);
219         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, gbuf[0]);
220
221         conf->bLength = gbuf[0];
222         conf->bDescriptorType = gbuf[1];
223         conf->wTotalLength = (u16) (gbuf[3] << 8 | gbuf[2]);
224         conf->bNumInterfaces = gbuf[4];
225         conf->bConfigurationValue = gbuf[5];
226         conf->iConfiguration = gbuf[6];
227         conf->bmAttributes = gbuf[7];
228         conf->bMaxPower = gbuf[8];
229         conf->intf = NULL;
230
231         return 0;
232 }
233
234 /* returns more information about CONFIGURATION, including
235  * INTERFACE(s) and ENDPOINT(s)
236  * usb_get_desc_configuration() must be called for this device before
237  */
238 s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index, struct usb_conf *conf)
239 {
240         cleargbuf();
241
242         usb_get_desc_configuration(dev, index, conf);
243         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
244
245         u8 i,j,off=9;
246         struct usb_intf *ifs = dev->conf->intf = (struct usb_intf*) malloc(sizeof(struct usb_intf));
247         for(i=1; i <= dev->conf->bNumInterfaces; i++) {
248                 if(i!=1) {
249                         ifs->next = (struct usb_intf*) malloc(sizeof(struct usb_intf));
250                         ifs = ifs->next;
251                 }
252                 ifs->bLength = gbuf[off+0];
253                 ifs->bDescriptorType = gbuf[off+1];
254                 ifs->bInterfaceNumber = gbuf[off+2];
255                 ifs->bAlternateSetting = gbuf[off+3];
256                 ifs->bNumEndpoints = gbuf[off+4];
257                 ifs->bInterfaceClass = gbuf[off+5];
258                 ifs->bInterfaceSubClass = gbuf[off+6];
259                 ifs->bInterfaceProtocol = gbuf[off+7];
260                 ifs->iInterface = gbuf[off+8];
261
262                 off += 9;
263
264                 struct usb_endp *ep = ifs->endp = (struct usb_endp*) malloc(sizeof(struct usb_endp));
265                 for(j=1; j <= ifs->bNumEndpoints; j++) {
266                         /* skip HID Device Descriptor (see lsusb) */
267                         if(gbuf[off+1] == 33) {
268                                 j--;
269                                 off += 9;
270                                 continue;
271                         }
272
273                         if(j!=1) {
274                                 ep->next = (struct usb_endp*) malloc(sizeof(struct usb_endp));
275                                 ep = ep->next;
276                         }
277
278                         ep->bLength = gbuf[off+0];
279                         ep->bDescriptorType = gbuf[off+1];
280                         ep->bEndpointAddress = gbuf[off+2];
281                         ep->bmAttributes = gbuf[off+3];
282                         ep->wMaxPacketSize = (u16) ((gbuf[off+5] << 8) | (gbuf[off+4]));
283                         ep->bInterval = gbuf[off+6];
284
285                         off += 7;
286                 }
287         }
288         return 0;
289 }
290
291 s8 usb_set_address(struct usb_device *dev, u8 address)
292 {
293         cleargbuf();
294         usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, gbuf, 0);
295         hexdump((void*) gbuf, 8);
296         wait_ms(210);
297         return 0;
298 }
299
300
301 u8 usb_get_configuration(struct usb_device *dev)
302 {
303         cleargbuf();
304         usb_control_msg(dev, 0x80, GET_CONFIGURATION, 0, 0, 4, gbuf, 0);
305         printf("=============\nafter usb_get_configuration:\n");
306         hexdump((void*) gbuf, 8);
307         return gbuf[0];
308 }
309
310 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
311 {
312         cleargbuf();
313         usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, gbuf, 0);
314         printf("=============\nafter usb_set_configuration:\n");
315         hexdump((void*) gbuf, 8);
316         wait_ms(20);
317         return 0;
318 }
319
320 s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
321 {
322
323         return 0;
324 }
325
326
327
328 /******************* Bulk Transfer **********************/
329 /**
330  * Write to an a bulk endpoint.
331  */
332 s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
333 {
334         struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
335         irp->dev = dev;
336         //irp->devaddress = dev->address;
337         
338         irp->endpoint = ep;
339         irp->epsize = dev->epSize[ep]; // ermitteln
340         irp->type = USB_BULK;
341
342         irp->buffer = buf;
343         irp->len = size;
344         irp->timeout = timeout;
345
346         usb_submit_irp(irp);
347         free(irp);
348
349         return 0;
350 }
351
352 /**
353  * Read from an bulk endpoint.
354  */
355 s8 usb_bulk_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
356 {
357         struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
358         //irp->devaddress = dev->address;
359         irp->dev = dev;
360         
361         irp->endpoint = ep | 0x80;      // from device to host
362         irp->epsize = dev->epSize[ep]; // ermitteln
363         irp->type = USB_BULK;
364
365         irp->buffer = buf;
366         irp->len = size;
367         irp->timeout = timeout;
368
369         usb_submit_irp(irp);
370         free(irp);
371
372         return 0;
373 }
374
375
376 /******************* Interrupt Transfer **********************/
377 /**
378  * Write to an interrupt endpoint.
379  */
380 s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
381 {
382         return 0;
383 }
384
385 /**
386  * Read from an interrupt endpoint.
387  */
388 s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
389 {
390         struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
391         irp->dev = dev;
392         irp->endpoint = ep; //wtf? |80; //from device to host
393         irp->epsize = dev->epSize[ep]; // ermitteln
394         irp->type = USB_INTR;
395
396         irp->buffer = buf;
397         irp->len = size;
398         irp->timeout = timeout;
399
400         usb_submit_irp(irp);
401         free(irp);
402
403         return 0;
404 }
405
406
407 /******************* Isochron Transfer **********************/
408
409 /**
410  * Write to an isochron endpoint.
411  */
412 s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
413 {
414
415         return 0;
416 }
417
418 /**
419  * Read from an isochron endpoint.
420  */
421 s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
422 {
423
424
425         return 0;
426 }
427
428
429 //#endif        //_USB_H_