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 usb_device *usb_open(u32 vendor_id, u32 product_id)
53 element * iterator = core.devices->head;
54 while(iterator != NULL) {
55 dev = (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 usb_device *usb_open_class(u8 class)
73 element * iterator = core.devices->head;
74 while(iterator != NULL) {
75 dev = (usb_device*)iterator->data;
77 if(dev->bDeviceClass==class)
80 iterator=iterator->next;
85 /* Close device after a communication.
87 s8 usb_close(usb_device *dev)
93 /* ask first 8 bytes of device descriptor with this special
94 * GET Descriptor Request, when device address = 0
96 s8 usb_get_dev_desc_simple(usb_device *dev, u8 *buf, u8 size)
101 usb_get_descriptor(dev, DEVICE, 0, buf, 8);
105 s8 usb_get_dev_desc(usb_device *dev, u8 *buf, u8 size, u8 dev_desc_size)
107 printf("WTF SIZE: %X\n", size>= dev_desc_size ? dev_desc_size : size);
108 usb_get_descriptor(dev, DEVICE, 0, buf, size >= dev_desc_size ? dev_desc_size : size);
112 s8 usb_set_address(usb_device *dev, u8 address)
115 usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 0);
119 s8 usb_set_configuration(usb_device *dev, u8 configuration)
125 s8 usb_set_altinterface(usb_device *dev, u8 alternate)
131 s8 usb_reset(usb_device *dev)
139 /******************* Control Transfer **********************/
142 * Create a control transfer.
144 s8 usb_control_msg(usb_device *dev, u8 requesttype, u8 request,
145 u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
147 usb_irp *irp = (usb_irp*)malloc(sizeof(usb_irp));
151 irp->epsize = dev->bMaxPacketSize0;
152 irp->type = USB_CTRL;
154 buf[0]=(u8)requesttype;
157 buf[3]=(u8)(value >> 8);
159 buf[5]=(u8)(index >> 8);
161 buf[7]=(u8)(length >> 8);
165 irp->timeout = timeout;
174 s8 usb_get_string(usb_device *dev, u8 index, u8 langid, u8 *buf, u8 buflen)
181 s8 usb_get_string_simple(usb_device *dev, u8 index, u8 *buf, u8 size)
183 usb_get_descriptor(dev, STRING, index, buf, (u8) 8);
184 usb_get_descriptor(dev, STRING, index, buf, size >= buf[0] ? (u8) buf[0] : size);
188 s8 usb_get_descriptor(usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
190 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
195 /******************* Bulk Transfer **********************/
198 * Write to an a bulk endpoint.
200 s8 usb_bulk_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
202 usb_irp * irp = (usb_irp*)malloc(sizeof(usb_irp));
204 //irp->devaddress = dev->address;
207 irp->epsize = dev->epSize[ep]; // ermitteln
208 irp->type = USB_BULK;
212 irp->timeout = timeout;
221 * Read from an bulk endpoint.
223 s8 usb_bulk_read(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
225 usb_irp * irp = (usb_irp*)malloc(sizeof(usb_irp));
226 //irp->devaddress = dev->address;
229 irp->endpoint = ep | 0x80; // from device to host
230 irp->epsize = dev->epSize[ep]; // ermitteln
231 irp->type = USB_BULK;
235 irp->timeout = timeout;
244 /******************* Interrupt Transfer **********************/
246 * Write to an interrupt endpoint.
248 s8 usb_interrupt_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
255 * Read from an interrupt endpoint.
257 s8 usb_interrupt_read(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
264 /******************* Isochron Transfer **********************/
267 * Write to an isochron endpoint.
269 s8 usb_isochron_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
276 * Read from an isochron endpoint.
278 s8 usb_isochron_read(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)