2 * Copyright (c) 2007, Benedikt Sauter <sauter@ixbat.de>
5 * Short descripton of file:
6 * I take the function names and parameters mainly from
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
39 #include "../host/host.h"
40 #include "../usbspec/usb11spec.h"
41 #include "../../malloc.h"
42 #include "../../string.h"
44 #define cleargbuf() memset(gbuf, 0, 0xffff)
45 /* internal global buffer */
46 static u8 gbuf[0xffff];
48 /******************* Device Operations **********************/
50 * Open a device with verndor- and product-id for a communication.
52 struct usb_device *usb_open(u32 vendor_id, u32 product_id)
54 struct usb_device* dev;
55 struct element * iterator = core.devices->head;
56 while(iterator != NULL) {
57 dev = (struct usb_device*)iterator->data;
59 if(dev->idVendor==vendor_id&&dev->idProduct==product_id)
62 iterator=iterator->next;
70 * Open a device with an class code for a communication.
72 struct usb_device *usb_open_class(u8 class)
74 struct usb_device* dev;
75 struct element * iterator = core.devices->head;
76 while(iterator != NULL) {
77 dev = (struct usb_device*)iterator->data;
79 if(dev->bDeviceClass==class)
82 iterator=iterator->next;
87 /* Close device after a communication.
89 s8 usb_close(struct usb_device *dev)
95 s8 usb_reset(struct usb_device *dev)
103 /******************* Control Transfer **********************/
105 * Create a control transfer.
107 s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
108 u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
110 struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
114 irp->epsize = dev->bMaxPacketSize0;
115 irp->type = USB_CTRL;
117 buf[0]=(u8)requesttype;
120 buf[3]=(u8)(value >> 8);
122 buf[5]=(u8)(index >> 8);
124 buf[7]=(u8)(length >> 8);
128 irp->timeout = timeout;
136 s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
138 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
142 s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid)
148 char *usb_get_string_simple(struct usb_device *dev, u8 index)
151 usb_get_descriptor(dev, STRING, index, gbuf, (u8) 8);
152 usb_get_descriptor(dev, STRING, index, gbuf, gbuf[0]);
154 char *str = (char*)malloc((gbuf[0]/2));
155 memset(str, '\0', (gbuf[0]/2));
158 for(i=0; i<(gbuf[0]/2)-1; i++) {
159 str[i] = gbuf[2+(i*2)];
165 /* ask first 8 bytes of device descriptor with this special
166 * GET Descriptor Request, when device address = 0
168 s8 usb_get_desc_dev_simple(struct usb_device *dev)
171 usb_get_descriptor(dev, DEVICE, 0, gbuf, 8);
174 printf("FU: %d\n", gbuf[7]);
177 dev->bMaxPacketSize0 = gbuf[7];
181 s8 usb_get_desc_dev(struct usb_device *dev)
184 if (usb_get_desc_dev_simple(dev) < 0) {
187 usb_get_descriptor(dev, DEVICE, 0, gbuf, gbuf[0]);
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];
204 struct usb_conf *conf = dev->conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
205 for(i=0; i <= dev->bNumConfigurations; i++) {
207 conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
209 usb_get_desc_config_ext(dev, i, conf);
215 s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, struct usb_conf *conf)
218 usb_get_descriptor(dev, CONFIGURATION, index, gbuf, 8);
219 usb_get_descriptor(dev, CONFIGURATION, index, gbuf, gbuf[0]);
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];
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
238 s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index, struct usb_conf *conf)
242 usb_get_desc_configuration(dev, index, conf);
243 usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
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++) {
249 ifs->next = (struct usb_intf*) malloc(sizeof(struct usb_intf));
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];
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) {
274 ep->next = (struct usb_endp*) malloc(sizeof(struct usb_endp));
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];
291 s8 usb_set_address(struct usb_device *dev, u8 address)
294 usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, gbuf, 0);
300 u8 usb_get_configuration(struct usb_device *dev)
303 usb_control_msg(dev, 0x80, GET_CONFIGURATION, 0, 0, 4, gbuf, 0);
304 printf("=============\nafter usb_get_configuration:\n");
305 hexdump((void*) gbuf, 8);
309 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
312 usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, gbuf, 0);
317 s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
325 /******************* Bulk Transfer **********************/
327 * Write to an a bulk endpoint.
329 s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
331 struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
333 //irp->devaddress = dev->address;
336 irp->epsize = dev->epSize[ep]; // ermitteln
337 irp->type = USB_BULK;
341 irp->timeout = timeout;
350 * Read from an bulk endpoint.
352 s8 usb_bulk_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
354 struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
355 //irp->devaddress = dev->address;
358 irp->endpoint = ep | 0x80; // from device to host
359 irp->epsize = dev->epSize[ep]; // ermitteln
360 irp->type = USB_BULK;
364 irp->timeout = timeout;
373 /******************* Interrupt Transfer **********************/
375 * Write to an interrupt endpoint.
377 s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
383 * Read from an interrupt endpoint.
385 s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
387 struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
389 irp->endpoint = ep | 0x80; //from device to host
390 irp->epsize = dev->epSize[ep]; // ermitteln
391 irp->type = USB_INTR;
395 irp->timeout = timeout;
397 printf("interupt_read\n");
405 /******************* Isochron Transfer **********************/
408 * Write to an isochron endpoint.
410 s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
417 * Read from an isochron endpoint.
419 s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)