0f85c6370b2217df37241cec6a0461e8416ad03c
[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 //wtf??!
38 //#ifndef _USB_H_
39 //#define _USB_H_
40
41 //#include <stdlib.h>
42
43 #include "usb.h"
44 #include "core.h"
45 #include "../host/host.h"
46 #include "../usbspec/usb11spec.h"
47 #include "../../malloc.h"
48
49
50 /******************* Device Operations **********************/
51
52 /**
53  * Open a device with verndor- and product-id for a communication.
54  */
55 usb_device * usb_open(u32 vendor_id, u32 product_id)
56 {
57         usb_device* dev;
58         element * iterator = core.devices->head;
59         while(iterator != NULL) {
60                 dev = (usb_device*)iterator->data;
61                 
62                 if(dev->idVendor==vendor_id&&dev->idProduct==product_id)
63                         return dev;
64
65                 iterator=iterator->next;
66         }
67
68         return NULL;
69 }
70
71
72 /**
73  * Open a device with an class code for a communication.
74  */
75 usb_device * usb_open_class(u8 class)
76 {
77         usb_device* dev;
78         element * iterator = core.devices->head;
79         while(iterator != NULL) {
80                 dev = (usb_device*)iterator->data;
81                 
82                 if(dev->bDeviceClass==class)
83                         return dev;
84
85                 iterator=iterator->next;
86         }
87         return NULL;
88 }
89
90 /**
91  * Close device after a communication.
92  */
93 u8 usb_close(usb_device *dev)
94 {
95
96         return 0;
97 }
98
99 u8 usb_get_device_descriptor(usb_device *dev, char *buf,u8 size)
100 {
101
102         return 0;
103 }
104
105 u8 usb_set_address(usb_device *dev, u8 address)
106 {
107
108         return 0;
109 }
110
111 u8 usb_set_configuration(usb_device *dev, u8 configuration)
112 {
113
114         return 0;
115 }
116
117 u8 usb_set_altinterface(usb_device *dev, u8 alternate)
118 {
119
120         return 0;
121 }
122
123 u8 usb_reset(usb_device *dev)
124 {
125
126
127         return 0;
128 }
129
130
131 /******************* Control Transfer **********************/
132
133 /**
134  * Create a control transfer.
135  */
136 u8 usb_control_msg(usb_device *dev, u8 requesttype, u8 request, u16 value, u16 index, u16 length,char *buf, u16 size, u16 timeout)
137 {
138         usb_irp *irp = (usb_irp*)malloc(sizeof(usb_irp));
139         irp->dev = dev;
140         irp->endpoint = 0;
141         
142         irp->epsize = dev->bMaxPacketSize0;
143         irp->type = USB_CTRL;
144
145         buf[0]=(char)requesttype;
146         buf[1]=(char)request;
147         buf[2]=(char)(value);
148         buf[3]=(char)(value >> 8);
149         buf[4]=(char)(index);
150         buf[5]=(char)(index >> 8);
151         buf[6]=(char)(length);
152         buf[7]=(char)(length >> 8);
153
154         irp->buffer = buf;
155         irp->len = length;
156         irp->timeout = timeout;
157
158         usb_submit_irp(irp);
159         free(irp);
160
161         return 0;
162 }
163
164
165 u8 usb_get_string(usb_device *dev, u8 index, u8 langid, char *buf, u8 buflen)
166 {
167
168         return 0;
169 }
170
171
172 u8 usb_get_string_simple(usb_device *dev, u8 index, char *buf, u8 buflen)
173 {
174
175         return 0;
176 }
177
178 u8 usb_get_descriptor(usb_device *dev, unsigned char type, unsigned char index, void *buf, u8 size)
179 {
180
181         return 0;
182 }
183
184
185 /******************* Bulk Transfer **********************/
186
187 /**
188  * Write to an a bulk endpoint.
189  */
190 u8 usb_bulk_write(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
191 {
192         usb_irp * irp = (usb_irp*)malloc(sizeof(usb_irp));
193         irp->dev = dev;
194         //irp->devaddress = dev->address;
195         
196         irp->endpoint = ep;
197         irp->epsize = dev->epSize[ep]; // ermitteln
198         irp->type = USB_BULK;
199
200         irp->buffer = buf;
201         irp->len = size;
202         irp->timeout = timeout;
203
204         usb_submit_irp(irp);
205         free(irp);
206
207         return 0;
208 }
209
210 /**
211  * Read from an bulk endpoint.
212  */
213 u8 usb_bulk_read(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
214 {
215         usb_irp * irp = (usb_irp*)malloc(sizeof(usb_irp));
216         //irp->devaddress = dev->address;
217         irp->dev = dev;
218         
219         irp->endpoint = ep | 0x80;      // from device to host
220         irp->epsize = dev->epSize[ep]; // ermitteln
221         irp->type = USB_BULK;
222
223         irp->buffer = buf;
224         irp->len = size;
225         irp->timeout = timeout;
226
227         usb_submit_irp(irp);
228         free(irp);
229
230         return 0;
231 }
232
233
234 /******************* Interrupt Transfer **********************/
235 /**
236  * Write to an interrupt endpoint.
237  */
238 u8 usb_interrupt_write(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
239 {
240
241         return 0;
242 }
243
244 /**
245  * Read from an interrupt endpoint.
246  */
247 u8 usb_interrupt_read(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
248 {
249
250         return 0;
251 }
252
253
254 /******************* Isochron Transfer **********************/
255
256 /**
257  * Write to an isochron endpoint.
258  */
259 u8 usb_isochron_write(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
260 {
261
262         return 0;
263 }
264
265 /**
266  * Read from an isochron endpoint.
267  */
268 u8 usb_isochron_read(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
269 {
270
271
272         return 0;
273 }
274
275
276 //#endif        //_USB_H_