added lsusb and some more config stuff. except of some (simple) setter
[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 /**
106  * Create a control transfer.
107  */
108 s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
109                 u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
110 {
111         struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
112         irp->dev = dev;
113         irp->endpoint = 0;
114         
115         irp->epsize = dev->bMaxPacketSize0;
116         irp->type = USB_CTRL;
117
118         buf[0]=(u8)requesttype;
119         buf[1]=(u8)request;
120         buf[2]=(u8)(value);
121         buf[3]=(u8)(value >> 8);
122         buf[4]=(u8)(index);
123         buf[5]=(u8)(index >> 8);
124         buf[6]=(u8)(length);
125         buf[7]=(u8)(length >> 8);
126
127         irp->buffer = buf;
128         irp->len = length;
129         irp->timeout = timeout;
130
131         usb_submit_irp(irp);
132         free(irp);
133
134         return 0;
135 }
136
137 s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
138 {
139         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
140         return 0;
141 }
142
143 s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid)
144 {
145
146         return 0;
147 }
148
149 char *usb_get_string_simple(struct usb_device *dev, u8 index)
150 {
151         cleargbuf();
152         usb_get_descriptor(dev, STRING, index, gbuf, (u8) 8);
153         usb_get_descriptor(dev, STRING, index, gbuf, gbuf[0]);
154
155         char *str = (char*)malloc((gbuf[0]/2));
156         memset(str, '\0', (gbuf[0]/2));
157
158         u16 i;
159         for(i=0; i<(gbuf[0]/2)-1; i++) {
160                 str[i] = gbuf[2+(i*2)];
161         }
162
163         return str;
164 }
165
166 /* ask first 8 bytes of device descriptor with this special 
167  * GET Descriptor Request, when device address = 0
168  */
169 s8 usb_get_desc_dev_simple(struct usb_device *dev)
170 {
171         cleargbuf();
172         usb_get_descriptor(dev, DEVICE, 0, gbuf, 8);
173
174         if(!gbuf[7]) {
175                 printf("FU: %d\n", gbuf[7]);
176                 return -2;
177         }
178         dev->bMaxPacketSize0 = gbuf[7];
179         return 0;
180 }
181
182 s8 usb_get_desc_dev(struct usb_device *dev)
183 {
184         cleargbuf();
185         if (usb_get_desc_dev_simple(dev) < 0) {
186                 return -1;
187         }
188         usb_get_descriptor(dev, DEVICE, 0, gbuf, gbuf[0]);
189
190         dev->bLength = gbuf[0];
191         dev->bDescriptorType = gbuf[1];
192         dev->bcdUSB = (u16) (gbuf[3] << 8 | gbuf[2]);
193         dev->bDeviceClass = gbuf[4];
194         dev->bDeviceSubClass = gbuf[5];
195         dev->bDeviceProtocoll = gbuf[6];
196         dev->idVendor = (u16) (gbuf[9] << 8) | (gbuf[8]);
197         dev->idProduct = (u16) (gbuf[11] << 8) | (gbuf[10]);
198         dev->bcdDevice = (u16) (gbuf[13] << 8) | (gbuf[12]);
199         dev->iManufacturer = gbuf[14];
200         dev->iProduct = gbuf[15];
201         dev->iSerialNumber = gbuf[16];
202         dev->bNumConfigurations = gbuf[17];
203
204         u8 i;
205         struct usb_conf *conf = dev->conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
206         for(i=0; i <= dev->bNumConfigurations; i++) {
207                 if(i!=0) {
208                         conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
209                 }
210                 usb_get_desc_config_ext(dev, i, conf);
211         }
212
213         return 0;
214 }
215
216 s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, struct usb_conf *conf)
217 {
218         cleargbuf();
219         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, 8);
220         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, gbuf[0]);
221
222         conf->bLength = gbuf[0];
223         conf->bDescriptorType = gbuf[1];
224         conf->wTotalLength = (u16) (gbuf[3] << 8 | gbuf[2]);
225         conf->bNumInterfaces = gbuf[4];
226         conf->bConfigurationValue = gbuf[5];
227         conf->iConfiguration = gbuf[6];
228         conf->bmAttributes = gbuf[7];
229         conf->bMaxPower = gbuf[8];
230         conf->intf = NULL;
231
232         return 0;
233 }
234
235 /* returns more information about CONFIGURATION, including
236  * INTERFACE(s) and ENDPOINT(s)
237  * usb_get_desc_configuration() must be called for this device before
238  */
239 s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index, struct usb_conf *conf)
240 {
241         cleargbuf();
242
243         usb_get_desc_configuration(dev, index, conf);
244         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
245
246         u8 i,j,off=9;
247         struct usb_intf *ifs = dev->conf->intf = (struct usb_intf*) malloc(sizeof(struct usb_intf));
248         for(i=1; i <= dev->conf->bNumInterfaces; i++) {
249                 if(i!=1) {
250                         ifs->next = (struct usb_intf*) malloc(sizeof(struct usb_intf));
251                         ifs = ifs->next;
252                 }
253                 ifs->bLength = gbuf[off+0];
254                 ifs->bDescriptorType = gbuf[off+1];
255                 ifs->bInterfaceNumber = gbuf[off+2];
256                 ifs->bAlternateSetting = gbuf[off+3];
257                 ifs->bNumEndpoints = gbuf[off+4];
258                 ifs->bInterfaceClass = gbuf[off+5];
259                 ifs->bInterfaceSubClass = gbuf[off+6];
260                 ifs->bInterfaceProtocol = gbuf[off+7];
261                 ifs->iInterface = gbuf[off+8];
262
263                 off += 9;
264
265                 struct usb_endp *ep = ifs->endp = (struct usb_endp*) malloc(sizeof(struct usb_endp));
266                 for(j=1; j <= ifs->bNumEndpoints; j++) {
267                         /* skip HID Device Descriptor (see lsusb) */
268                         if(gbuf[off+1] == 33) {
269                                 j--;
270                                 off += 9;
271                                 continue;
272                         }
273
274                         if(j!=1) {
275                                 ep->next = (struct usb_endp*) malloc(sizeof(struct usb_endp));
276                                 ep = ep->next;
277                         }
278
279                         ep->bLength = gbuf[off+0];
280                         ep->bDescriptorType = gbuf[off+1];
281                         ep->bEndpointAddress = gbuf[off+2];
282                         ep->bmAttributes = gbuf[off+3];
283                         ep->wMaxPacketSize = (u16) ((gbuf[off+5] << 8) | (gbuf[off+4]));
284                         ep->bInterval = gbuf[off+6];
285
286                         off += 7;
287                 }
288         }
289
290         printf("=============\nafter usb_get_desc_config_ext:\n");
291         hexdump((void*) gbuf, dev->conf->wTotalLength);
292         return 0;
293 }
294
295 s8 usb_set_address(struct usb_device *dev, u8 address)
296 {
297         cleargbuf();
298         usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, gbuf, 0);
299         return 0;
300 }
301
302 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
303 {
304         cleargbuf();
305         usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, gbuf, 0);
306         return 0;
307 }
308
309 s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
310 {
311
312         return 0;
313 }
314
315
316
317 /******************* Bulk Transfer **********************/
318
319 /**
320  * Write to an a bulk endpoint.
321  */
322 s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
323 {
324         struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
325         irp->dev = dev;
326         //irp->devaddress = dev->address;
327         
328         irp->endpoint = ep;
329         irp->epsize = dev->epSize[ep]; // ermitteln
330         irp->type = USB_BULK;
331
332         irp->buffer = buf;
333         irp->len = size;
334         irp->timeout = timeout;
335
336         usb_submit_irp(irp);
337         free(irp);
338
339         return 0;
340 }
341
342 /**
343  * Read from an bulk endpoint.
344  */
345 s8 usb_bulk_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
346 {
347         struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
348         //irp->devaddress = dev->address;
349         irp->dev = dev;
350         
351         irp->endpoint = ep | 0x80;      // from device to host
352         irp->epsize = dev->epSize[ep]; // ermitteln
353         irp->type = USB_BULK;
354
355         irp->buffer = buf;
356         irp->len = size;
357         irp->timeout = timeout;
358
359         usb_submit_irp(irp);
360         free(irp);
361
362         return 0;
363 }
364
365
366 /******************* Interrupt Transfer **********************/
367 /**
368  * Write to an interrupt endpoint.
369  */
370 s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
371 {
372
373         return 0;
374 }
375
376 /**
377  * Read from an interrupt endpoint.
378  */
379 s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
380 {
381
382         return 0;
383 }
384
385
386 /******************* Isochron Transfer **********************/
387
388 /**
389  * Write to an isochron endpoint.
390  */
391 s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
392 {
393
394         return 0;
395 }
396
397 /**
398  * Read from an isochron endpoint.
399  */
400 s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
401 {
402
403
404         return 0;
405 }
406
407
408 //#endif        //_USB_H_