license stuff
[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         struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
122         irp->dev = dev;
123         irp->endpoint = 0;
124
125         irp->epsize = dev->bMaxPacketSize0;
126         irp->type = USB_CTRL;
127
128         buf[0]=(u8)requesttype;
129         buf[1]=(u8)request;
130         buf[2]=(u8)(value);
131         buf[3]=(u8)(value >> 8);
132         buf[4]=(u8)(index);
133         buf[5]=(u8)(index >> 8);
134         buf[6]=(u8)(length);
135         buf[7]=(u8)(length >> 8);
136
137         irp->buffer = buf;
138         irp->len = length;
139         irp->timeout = timeout;
140
141         usb_submit_irp(irp);
142         free(irp);
143
144         return 0;
145 }
146
147 s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
148 {
149         usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
150         return 0;
151 }
152
153 s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid)
154 {
155
156         return 0;
157 }
158
159 char *usb_get_string_simple(struct usb_device *dev, u8 index)
160 {
161         cleargbuf();
162         usb_get_descriptor(dev, STRING, index, gbuf, (u8) 8);
163         usb_get_descriptor(dev, STRING, index, gbuf, gbuf[0]);
164
165         char *str = (char*)malloc((gbuf[0]/2));
166         memset(str, '\0', (gbuf[0]/2));
167
168         u16 i;
169         for(i=0; i<(gbuf[0]/2)-1; i++) {
170                 str[i] = gbuf[2+(i*2)];
171         }
172
173         return str;
174 }
175
176 /* ask first 8 bytes of device descriptor with this special 
177  * GET Descriptor Request, when device address = 0
178  */
179 s8 usb_get_desc_dev_simple(struct usb_device *dev)
180 {
181         cleargbuf();
182         usb_get_descriptor(dev, DEVICE, 0, gbuf, 8);
183
184         if(!gbuf[7]) {
185                 printf("FU: %d\n", gbuf[7]);
186                 return -2;
187         }
188         dev->bMaxPacketSize0 = gbuf[7];
189         return 0;
190 }
191
192 s8 usb_get_desc_dev(struct usb_device *dev)
193 {
194         cleargbuf();
195         if (usb_get_desc_dev_simple(dev) < 0) {
196                 return -1;
197         }
198         usb_get_descriptor(dev, DEVICE, 0, gbuf, gbuf[0]);
199
200         dev->bLength = gbuf[0];
201         dev->bDescriptorType = gbuf[1];
202         dev->bcdUSB = (u16) (gbuf[3] << 8 | gbuf[2]);
203         dev->bDeviceClass = gbuf[4];
204         dev->bDeviceSubClass = gbuf[5];
205         dev->bDeviceProtocoll = gbuf[6];
206         dev->idVendor = (u16) (gbuf[9] << 8) | (gbuf[8]);
207         dev->idProduct = (u16) (gbuf[11] << 8) | (gbuf[10]);
208         dev->bcdDevice = (u16) (gbuf[13] << 8) | (gbuf[12]);
209         dev->iManufacturer = gbuf[14];
210         dev->iProduct = gbuf[15];
211         dev->iSerialNumber = gbuf[16];
212         dev->bNumConfigurations = gbuf[17];
213
214         u8 i;
215         struct usb_conf *conf = dev->conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
216         for(i=0; i <= dev->bNumConfigurations; i++) {
217                 if(i!=0) {
218                         conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
219                 }
220                 usb_get_desc_config_ext(dev, i, conf);
221         }
222
223         return 0;
224 }
225
226 s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, struct usb_conf *conf)
227 {
228         cleargbuf();
229         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, 8);
230         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, gbuf[0]);
231
232         conf->bLength = gbuf[0];
233         conf->bDescriptorType = gbuf[1];
234         conf->wTotalLength = (u16) (gbuf[3] << 8 | gbuf[2]);
235         conf->bNumInterfaces = gbuf[4];
236         conf->bConfigurationValue = gbuf[5];
237         conf->iConfiguration = gbuf[6];
238         conf->bmAttributes = gbuf[7];
239         conf->bMaxPower = gbuf[8];
240         conf->intf = NULL;
241
242         return 0;
243 }
244
245 /* returns more information about CONFIGURATION, including
246  * INTERFACE(s) and ENDPOINT(s)
247  * usb_get_desc_configuration() must be called for this device before
248  */
249 s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index, struct usb_conf *conf)
250 {
251         cleargbuf();
252
253         usb_get_desc_configuration(dev, index, conf);
254         usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
255
256         u8 i,j,off=9;
257         struct usb_intf *ifs = dev->conf->intf = (struct usb_intf*) malloc(sizeof(struct usb_intf));
258         for(i=1; i <= dev->conf->bNumInterfaces; i++) {
259                 if(i!=1) {
260                         ifs->next = (struct usb_intf*) malloc(sizeof(struct usb_intf));
261                         ifs = ifs->next;
262                 }
263                 ifs->bLength = gbuf[off+0];
264                 ifs->bDescriptorType = gbuf[off+1];
265                 ifs->bInterfaceNumber = gbuf[off+2];
266                 ifs->bAlternateSetting = gbuf[off+3];
267                 ifs->bNumEndpoints = gbuf[off+4];
268                 ifs->bInterfaceClass = gbuf[off+5];
269                 ifs->bInterfaceSubClass = gbuf[off+6];
270                 ifs->bInterfaceProtocol = gbuf[off+7];
271                 ifs->iInterface = gbuf[off+8];
272
273                 off += 9;
274
275                 struct usb_endp *ep = ifs->endp = (struct usb_endp*) malloc(sizeof(struct usb_endp));
276                 for(j=1; j <= ifs->bNumEndpoints; j++) {
277                         /* skip HID Device Descriptor (see lsusb) */
278                         if(gbuf[off+1] == 33) {
279                                 j--;
280                                 off += 9;
281                                 continue;
282                         }
283
284                         if(j!=1) {
285                                 ep->next = (struct usb_endp*) malloc(sizeof(struct usb_endp));
286                                 ep = ep->next;
287                         }
288
289                         ep->bLength = gbuf[off+0];
290                         ep->bDescriptorType = gbuf[off+1];
291                         ep->bEndpointAddress = gbuf[off+2];
292                         ep->bmAttributes = gbuf[off+3];
293                         ep->wMaxPacketSize = (u16) ((gbuf[off+5] << 8) | (gbuf[off+4]));
294                         ep->bInterval = gbuf[off+6];
295
296                         off += 7;
297                 }
298         }
299         return 0;
300 }
301
302 s8 usb_set_address(struct usb_device *dev, u8 address)
303 {
304         cleargbuf();
305         usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, gbuf, 0);
306         hexdump((void*) gbuf, 8);
307         wait_ms(210);
308         return 0;
309 }
310
311
312 u8 usb_get_configuration(struct usb_device *dev)
313 {
314         cleargbuf();
315         usb_control_msg(dev, 0x80, GET_CONFIGURATION, 0, 0, 4, gbuf, 0);
316         printf("=============\nafter usb_get_configuration:\n");
317         hexdump((void*) gbuf, 8);
318         return gbuf[0];
319 }
320
321 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
322 {
323         cleargbuf();
324         usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, gbuf, 0);
325         printf("=============\nafter usb_set_configuration:\n");
326         hexdump((void*) gbuf, 8);
327         wait_ms(20);
328         return 0;
329 }
330
331 s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
332 {
333
334         return 0;
335 }
336
337
338
339 /******************* Bulk Transfer **********************/
340 /**
341  * Write to an a bulk endpoint.
342  */
343 s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
344 {
345         struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
346         irp->dev = dev;
347         //irp->devaddress = dev->address;
348         
349         irp->endpoint = ep;
350         irp->epsize = dev->epSize[ep]; // ermitteln
351         irp->type = USB_BULK;
352
353         irp->buffer = buf;
354         irp->len = size;
355         irp->timeout = timeout;
356
357         usb_submit_irp(irp);
358         free(irp);
359
360         return 0;
361 }
362
363 /**
364  * Read from an bulk endpoint.
365  */
366 s8 usb_bulk_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
367 {
368         struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
369         //irp->devaddress = dev->address;
370         irp->dev = dev;
371         
372         irp->endpoint = ep | 0x80;      // from device to host
373         irp->epsize = dev->epSize[ep]; // ermitteln
374         irp->type = USB_BULK;
375
376         irp->buffer = buf;
377         irp->len = size;
378         irp->timeout = timeout;
379
380         usb_submit_irp(irp);
381         free(irp);
382
383         return 0;
384 }
385
386
387 /******************* Interrupt Transfer **********************/
388 /**
389  * Write to an interrupt endpoint.
390  */
391 s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
392 {
393         return 0;
394 }
395
396 /**
397  * Read from an interrupt endpoint.
398  */
399 s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
400 {
401         struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
402         irp->dev = dev;
403         irp->endpoint = ep; //wtf? |80; //from device to host
404         irp->epsize = dev->epSize[ep]; // ermitteln
405         irp->type = USB_INTR;
406
407         irp->buffer = buf;
408         irp->len = size;
409         irp->timeout = timeout;
410
411         usb_submit_irp(irp);
412         free(irp);
413
414         return 0;
415 }
416
417
418 /******************* Isochron Transfer **********************/
419
420 /**
421  * Write to an isochron endpoint.
422  */
423 s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
424 {
425
426         return 0;
427 }
428
429 /**
430  * Read from an isochron endpoint.
431  */
432 s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
433 {
434
435
436         return 0;
437 }
438
439
440 //#endif        //_USB_H_