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"
45 /******************* Device Operations **********************/
48 * Open a device with verndor- and product-id for a communication.
50 struct usb_device *usb_open(u32 vendor_id, u32 product_id)
52 struct usb_device* dev;
53 struct element * iterator = core.devices->head;
54 while(iterator != NULL) {
55 dev = (struct usb_device*)iterator->data;
57 if(dev->idVendor==vendor_id&&dev->idProduct==product_id)
60 iterator=iterator->next;
68 * Open a device with an class code for a communication.
70 struct usb_device *usb_open_class(u8 class)
72 struct usb_device* dev;
73 struct element * iterator = core.devices->head;
74 while(iterator != NULL) {
75 dev = (struct usb_device*)iterator->data;
77 if(dev->bDeviceClass==class)
80 iterator=iterator->next;
85 /* Close device after a communication.
87 s8 usb_close(struct usb_device *dev)
93 s8 usb_reset(struct usb_device *dev)
101 /******************* Control Transfer **********************/
104 * Create a control transfer.
106 s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
107 u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
109 struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
113 irp->epsize = dev->bMaxPacketSize0;
114 irp->type = USB_CTRL;
116 buf[0]=(u8)requesttype;
119 buf[3]=(u8)(value >> 8);
121 buf[5]=(u8)(index >> 8);
123 buf[7]=(u8)(length >> 8);
127 irp->timeout = timeout;
136 s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid, u8 *buf, u8 buflen)
143 char *usb_get_string_simple(struct usb_device *dev, u8 index, u8 *buf, u8 size)
148 usb_get_descriptor(dev, STRING, index, buf, (u8) 8);
149 size = size >= (buf[0]) ? (buf[0]) : size;
150 usb_get_descriptor(dev, STRING, index, buf, size);
152 char *str = (char*)malloc((size/2));
153 memset(str, '\0', (size/2));
156 for(i=0; i<(size/2)-1; i++) {
157 str[i] = buf[2+(i*2)];
158 printf("%c", str[i]);
165 s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
167 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
171 /* ask first 8 bytes of device descriptor with this special
172 * GET Descriptor Request, when device address = 0
174 s8 usb_get_desc_dev_simple(struct usb_device *dev, u8 *buf, u8 size)
179 usb_get_descriptor(dev, DEVICE, 0, buf, 8);
182 printf("FU: %d\n", buf[7]);
185 dev->bMaxPacketSize0 = buf[7];
189 s8 usb_get_desc_dev(struct usb_device *dev, u8 *buf, u8 size)
191 if (size < 0x12 || usb_get_desc_dev_simple(dev, buf, size) < 0) {
194 usb_get_descriptor(dev, DEVICE, 0, buf, size >= buf[0] ? buf[0] : size);
196 dev->bLength = buf[0];
197 dev->bDescriptorType = buf[1];
198 dev->bcdUSB = (u16) (buf[3] << 8 | buf[2]);
199 dev->bDeviceClass = buf[4];
200 dev->bDeviceSubClass = buf[5];
201 dev->bDeviceProtocoll = buf[6];
202 dev->idVendor = (u16) (buf[9] << 8) | (buf[8]);
203 dev->idProduct = (u16) (buf[11] << 8) | (buf[10]);
204 dev->bcdDevice = (u16) (buf[13] << 8) | (buf[12]);
205 dev->iManufacturer = buf[14];
206 dev->iProduct = buf[15];
207 dev->iSerialNumber = buf[16];
208 dev->bNumConfigurations = buf[17];
213 s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, u8 *buf, u8 size)
218 usb_get_descriptor(dev, CONFIGURATION, index, buf, 8);
219 usb_get_descriptor(dev, CONFIGURATION, index, buf, size >= buf[0] ? buf[0] : size);
221 dev->conf->bLength = buf[0];
222 dev->conf->bDescriptorType = buf[1];
223 dev->conf->wTotalLength = (u16) (buf[3] << 8 | buf[2]);
224 dev->conf->bNumInterfaces = buf[4];
225 dev->conf->bConfigurationValue = buf[5];
226 dev->conf->iConfiguration = buf[6];
227 dev->conf->bmAttributes = buf[7];
228 dev->conf->bMaxPower = buf[8];
232 s8 usb_get_desc_interface(struct usb_device *dev, u8 index, u8 *buf, u8 size)
237 usb_get_descriptor(dev, INTERFACE, index, buf, 8);
238 usb_get_descriptor(dev, INTERFACE, index, buf, size >= buf[0] ? buf[0] : size);
243 s8 usb_set_address(struct usb_device *dev, u8 address)
246 usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 0);
250 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
256 s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
264 /******************* Bulk Transfer **********************/
267 * Write to an a bulk endpoint.
269 s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
271 struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
273 //irp->devaddress = dev->address;
276 irp->epsize = dev->epSize[ep]; // ermitteln
277 irp->type = USB_BULK;
281 irp->timeout = timeout;
290 * Read from an bulk endpoint.
292 s8 usb_bulk_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
294 struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
295 //irp->devaddress = dev->address;
298 irp->endpoint = ep | 0x80; // from device to host
299 irp->epsize = dev->epSize[ep]; // ermitteln
300 irp->type = USB_BULK;
304 irp->timeout = timeout;
313 /******************* Interrupt Transfer **********************/
315 * Write to an interrupt endpoint.
317 s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
324 * Read from an interrupt endpoint.
326 s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
333 /******************* Isochron Transfer **********************/
336 * Write to an isochron endpoint.
338 s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
345 * Read from an isochron endpoint.
347 s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)