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