bleh, tmp commit...
[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 /*
38         ppcskel - a Free Software replacement for the Nintendo/BroadOn bootloader.
39         libusb like interface
40
41 Copyright (C) 2009     Bernhard Urban <lewurm@gmx.net>
42 Copyright (C) 2009     Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
43
44 # This code is licensed to you under the terms of the GNU GPL, version 2;
45 # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
46 */
47
48 #include "usb.h"
49 #include "core.h"
50 #include "../host/host.h"
51 #include "../usbspec/usb11spec.h"
52 #include "../../malloc.h"
53 #include "../../string.h"
54
55 #define cleargbuf() memset(gbuf, 0, 0xffff)
56 /* internal global buffer */
57 static u8 gbuf[0xffff];
58
59 /******************* Device Operations **********************/
60 /**
61  * Open a device with verndor- and product-id for a communication.
62  */
63 struct usb_device *usb_open(u32 vendor_id, u32 product_id)
64 {
65         struct usb_device* dev;
66         struct element * iterator = core.devices->head;
67         while(iterator != NULL) {
68                 dev = (struct usb_device*)iterator->data;
69                 
70                 if(dev->idVendor==vendor_id&&dev->idProduct==product_id)
71                         return dev;
72
73                 iterator=iterator->next;
74         }
75
76         return NULL;
77 }
78
79
80 /**
81  * Open a device with an class code for a communication.
82  */
83 struct usb_device *usb_open_class(u8 class)
84 {
85         struct usb_device* dev;
86         struct element * iterator = core.devices->head;
87         while(iterator != NULL) {
88                 dev = (struct usb_device*)iterator->data;
89                 
90                 if(dev->bDeviceClass==class)
91                         return dev;
92
93                 iterator=iterator->next;
94         }
95         return NULL;
96 }
97
98 /* Close device after a communication.
99  */
100 s8 usb_close(struct usb_device *dev)
101 {
102
103         return 0;
104 }
105
106 s8 usb_reset(struct usb_device *dev)
107 {
108
109
110         return 0;
111 }
112
113
114 /******************* Control Transfer **********************/
115 /**
116  * Create a control transfer.
117  */
118 s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
119                 u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
120 {
121         return usb_control_msg_pl(dev, requesttype, request, value, index, length, buf, timeout, NULL);
122 }
123
124 s8 usb_control_msg_pl(struct usb_device *dev, u8 requesttype, u8 request,
125                 u16 value, u16 index, u16 length, u8 *buf, u16 timeout, u8 *payload)
126 {
127         static struct usb_irp irp;
128         memset(&irp, 0, sizeof(struct usb_irp));
129
130         irp.dev = dev;
131         irp.endpoint = 0;
132
133         irp.epsize = dev->bMaxPacketSize0;
134         irp.type = USB_CTRL;
135
136         buf[0]=(u8)requesttype;
137         buf[1]=(u8)request;
138         buf[2]=(u8)(value);
139         buf[3]=(u8)(value >> 8);
140         buf[4]=(u8)(index);
141         buf[5]=(u8)(index >> 8);
142         buf[6]=(u8)(length);
143         buf[7]=(u8)(length >> 8);
144
145         if(payload) {
146                 u16 i;
147                 for(i=8; (i-8)<length; i++)
148                         buf[i] = payload[i-8];
149         }
150
151         irp.buffer = buf;
152         irp.len = length;
153         irp.timeout = timeout;
154
155         usb_submit_irp(&irp);
156
157         return 0;
158 }
159
160 s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
161 {
162         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
163         return 0;
164 }
165
166 s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid)
167 {
168
169         return 0;
170 }
171
172 char *usb_get_string_simple(struct usb_device *dev, u8 index)
173 {
174         cleargbuf();
175         usb_get_descriptor(dev, STRING, index, gbuf, (u8) 8);
176         usb_get_descriptor(dev, STRING, index, gbuf, gbuf[0]);
177
178         char *str = (char*)malloc((gbuf[0]/2));
179         memset(str, '\0', (gbuf[0]/2));
180
181         u16 i;
182         for(i=0; i<(gbuf[0]/2)-1; i++) {
183                 str[i] = gbuf[2+(i*2)];
184         }
185
186         return str;
187 }
188
189 /* ask first 8 bytes of device descriptor with this special 
190  * GET Descriptor Request, when device address = 0
191  */
192 s8 usb_get_desc_dev_simple(struct usb_device *dev)
193 {
194         cleargbuf();
195         usb_get_descriptor(dev, DEVICE, 0, gbuf, 8);
196
197         if(!gbuf[7]) {
198                 printf("FU: %d\n", gbuf[7]);
199                 return -2;
200         }
201         dev->bMaxPacketSize0 = gbuf[7];
202         return 0;
203 }
204
205 s8 usb_get_desc_dev(struct usb_device *dev)
206 {
207         cleargbuf();
208         if (usb_get_desc_dev_simple(dev) < 0) {
209                 return -1;
210         }
211         usb_get_descriptor(dev, DEVICE, 0, gbuf, gbuf[0]);
212
213         dev->bLength = gbuf[0];
214         dev->bDescriptorType = gbuf[1];
215         dev->bcdUSB = (u16) (gbuf[3] << 8 | gbuf[2]);
216         dev->bDeviceClass = gbuf[4];
217         dev->bDeviceSubClass = gbuf[5];
218         dev->bDeviceProtocoll = gbuf[6];
219         dev->idVendor = (u16) (gbuf[9] << 8) | (gbuf[8]);
220         dev->idProduct = (u16) (gbuf[11] << 8) | (gbuf[10]);
221         dev->bcdDevice = (u16) (gbuf[13] << 8) | (gbuf[12]);
222         dev->iManufacturer = gbuf[14];
223         dev->iProduct = gbuf[15];
224         dev->iSerialNumber = gbuf[16];
225         dev->bNumConfigurations = gbuf[17];
226
227         u8 i;
228         struct usb_conf *conf = dev->conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
229         for(i=0; i <= dev->bNumConfigurations; i++) {
230                 if(i!=0) {
231                         conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
232                 }
233                 usb_get_desc_config_ext(dev, i, conf);
234         }
235
236         return 0;
237 }
238
239 s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, struct usb_conf *conf)
240 {
241         cleargbuf();
242         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, 8);
243         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, gbuf[0]);
244
245         conf->bLength = gbuf[0];
246         conf->bDescriptorType = gbuf[1];
247         conf->wTotalLength = (u16) (gbuf[3] << 8 | gbuf[2]);
248         conf->bNumInterfaces = gbuf[4];
249         conf->bConfigurationValue = gbuf[5];
250         conf->iConfiguration = gbuf[6];
251         conf->bmAttributes = gbuf[7];
252         conf->bMaxPower = gbuf[8];
253         conf->intf = NULL;
254
255         return 0;
256 }
257
258 /* returns more information about CONFIGURATION, including
259  * INTERFACE(s) and ENDPOINT(s)
260  * usb_get_desc_configuration() must be called for this device before
261  */
262 s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index, struct usb_conf *conf)
263 {
264         cleargbuf();
265
266         usb_get_desc_configuration(dev, index, conf);
267         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
268
269         u8 i,j,off=9;
270         struct usb_intf *ifs = dev->conf->intf = (struct usb_intf*) malloc(sizeof(struct usb_intf));
271         for(i=1; i <= dev->conf->bNumInterfaces; i++) {
272                 if(i!=1) {
273                         ifs->next = (struct usb_intf*) malloc(sizeof(struct usb_intf));
274                         ifs = ifs->next;
275                 }
276                 ifs->bLength = gbuf[off+0];
277                 ifs->bDescriptorType = gbuf[off+1];
278                 ifs->bInterfaceNumber = gbuf[off+2];
279                 ifs->bAlternateSetting = gbuf[off+3];
280                 ifs->bNumEndpoints = gbuf[off+4];
281                 ifs->bInterfaceClass = gbuf[off+5];
282                 ifs->bInterfaceSubClass = gbuf[off+6];
283                 ifs->bInterfaceProtocol = gbuf[off+7];
284                 ifs->iInterface = gbuf[off+8];
285
286                 off += 9;
287
288                 struct usb_endp *ep = ifs->endp = (struct usb_endp*) malloc(sizeof(struct usb_endp));
289                 for(j=1; j <= ifs->bNumEndpoints; j++) {
290                         /* skip HID Device Descriptor (see lsusb) */
291                         if(gbuf[off+1] == 33) {
292                                 j--;
293                                 off += 9;
294                                 continue;
295                         }
296
297                         if(j!=1) {
298                                 ep->next = (struct usb_endp*) malloc(sizeof(struct usb_endp));
299                                 ep = ep->next;
300                         }
301
302                         ep->bLength = gbuf[off+0];
303                         ep->bDescriptorType = gbuf[off+1];
304                         ep->bEndpointAddress = gbuf[off+2];
305                         ep->bmAttributes = gbuf[off+3];
306                         ep->wMaxPacketSize = (u16) ((gbuf[off+5] << 8) | (gbuf[off+4]));
307                         ep->bInterval = gbuf[off+6];
308
309                         off += 7;
310                 }
311         }
312         return 0;
313 }
314
315 s8 usb_set_address(struct usb_device *dev, u8 address)
316 {
317         cleargbuf();
318         usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, gbuf, 0);
319         wait_ms(210);
320         return 0;
321 }
322
323
324 u8 usb_get_configuration(struct usb_device *dev)
325 {
326         cleargbuf();
327         usb_control_msg(dev, 0x80, GET_CONFIGURATION, 0, 0, 4, gbuf, 0);
328         return gbuf[0];
329 }
330
331 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
332 {
333         cleargbuf();
334         usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, gbuf, 0);
335         wait_ms(20);
336         return 0;
337 }
338
339 s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
340 {
341
342         return 0;
343 }
344
345 static s8 usb_gen_rw(struct usb_device *dev, u8 ep, u8 *buf, u16 size, u8 timeout, u8 type) {
346         static struct usb_irp irp;
347         memset(&irp, 0, sizeof(struct usb_irp));
348
349         irp.dev = dev;
350         irp.endpoint = ep;
351         irp.epsize = dev->epSize[ep & 0x7F];
352         irp.type = type;
353
354         irp.buffer = buf;
355         irp.len = size;
356         irp.timeout = timeout;
357
358         usb_submit_irp(&irp);
359         return 0;
360 }
361
362 /******************* Bulk Transfer **********************/
363 /**
364  * Write to an a bulk endpoint.
365  */
366 s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u16 size, u8 timeout)
367 {
368         return usb_gen_rw(dev, ep, buf, size, timeout, USB_BULK);
369 }
370
371 /**
372  * Read from an bulk endpoint.
373  */
374 s8 usb_bulk_read(struct usb_device *dev, u8 ep, u8 *buf, u16 size, u8 timeout)
375 {
376         return usb_gen_rw(dev, ep|0x80, buf, size, timeout, USB_BULK);
377 }
378
379
380 /******************* Interrupt Transfer **********************/
381 /**
382  * Write to an interrupt endpoint.
383  */
384 s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u16 size, u8 timeout)
385 {
386         return usb_gen_rw(dev, ep, buf, size, timeout, USB_INTR);
387 }
388
389 /**
390  * Read from an interrupt endpoint.
391  */
392 s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u16 size, u8 timeout)
393 {
394         return usb_gen_rw(dev, ep|0x80, buf, size, timeout, USB_INTR);
395 }
396
397
398 /******************* Isochron Transfer **********************/
399
400 /**
401  * Write to an isochron endpoint.
402  */
403 s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u16 size, u8 timeout)
404 {
405
406         return 0;
407 }
408
409 /**
410  * Read from an isochron endpoint.
411  */
412 s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u16 size, u8 timeout)
413 {
414
415
416         return 0;
417 }
418