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.
38 ppcskel - a Free Software replacement for the Nintendo/BroadOn bootloader.
41 Copyright (C) 2009 Bernhard Urban <lewurm@gmx.net>
42 Copyright (C) 2009 Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
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
50 #include "../host/host.h"
51 #include "../usbspec/usb11spec.h"
52 #include "../../malloc.h"
53 #include "../../string.h"
55 #define cleargbuf() memset(gbuf, 0, 0xffff)
56 /* internal global buffer */
57 static u8 gbuf[0xffff];
59 /******************* Device Operations **********************/
61 * Open a device with verndor- and product-id for a communication.
63 struct usb_device *usb_open(u32 vendor_id, u32 product_id)
65 struct usb_device* dev;
66 struct element * iterator = core.devices->head;
67 while(iterator != NULL) {
68 dev = (struct usb_device*)iterator->data;
70 if(dev->idVendor==vendor_id&&dev->idProduct==product_id)
73 iterator=iterator->next;
81 * Open a device with an class code for a communication.
83 struct usb_device *usb_open_class(u8 class)
85 struct usb_device* dev;
86 struct element * iterator = core.devices->head;
87 while(iterator != NULL) {
88 dev = (struct usb_device*)iterator->data;
90 if(dev->bDeviceClass==class)
93 iterator=iterator->next;
98 /* Close device after a communication.
100 s8 usb_close(struct usb_device *dev)
106 s8 usb_reset(struct usb_device *dev)
114 /******************* Control Transfer **********************/
116 * Create a control transfer.
118 s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
119 u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
121 static struct usb_irp irp;
122 memset(&irp, 0, sizeof(struct usb_irp));
127 irp.epsize = dev->bMaxPacketSize0;
130 buf[0]=(u8)requesttype;
133 buf[3]=(u8)(value >> 8);
135 buf[5]=(u8)(index >> 8);
137 buf[7]=(u8)(length >> 8);
141 irp.timeout = timeout;
143 usb_submit_irp(&irp);
148 s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
150 usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
154 s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid)
160 char *usb_get_string_simple(struct usb_device *dev, u8 index)
163 usb_get_descriptor(dev, STRING, index, gbuf, (u8) 8);
164 usb_get_descriptor(dev, STRING, index, gbuf, gbuf[0]);
166 char *str = (char*)malloc((gbuf[0]/2));
167 memset(str, '\0', (gbuf[0]/2));
170 for(i=0; i<(gbuf[0]/2)-1; i++) {
171 str[i] = gbuf[2+(i*2)];
177 /* ask first 8 bytes of device descriptor with this special
178 * GET Descriptor Request, when device address = 0
180 s8 usb_get_desc_dev_simple(struct usb_device *dev)
183 usb_get_descriptor(dev, DEVICE, 0, gbuf, 8);
186 printf("FU: %d\n", gbuf[7]);
189 dev->bMaxPacketSize0 = gbuf[7];
193 s8 usb_get_desc_dev(struct usb_device *dev)
196 if (usb_get_desc_dev_simple(dev) < 0) {
199 usb_get_descriptor(dev, DEVICE, 0, gbuf, gbuf[0]);
201 dev->bLength = gbuf[0];
202 dev->bDescriptorType = gbuf[1];
203 dev->bcdUSB = (u16) (gbuf[3] << 8 | gbuf[2]);
204 dev->bDeviceClass = gbuf[4];
205 dev->bDeviceSubClass = gbuf[5];
206 dev->bDeviceProtocoll = gbuf[6];
207 dev->idVendor = (u16) (gbuf[9] << 8) | (gbuf[8]);
208 dev->idProduct = (u16) (gbuf[11] << 8) | (gbuf[10]);
209 dev->bcdDevice = (u16) (gbuf[13] << 8) | (gbuf[12]);
210 dev->iManufacturer = gbuf[14];
211 dev->iProduct = gbuf[15];
212 dev->iSerialNumber = gbuf[16];
213 dev->bNumConfigurations = gbuf[17];
216 struct usb_conf *conf = dev->conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
217 for(i=0; i <= dev->bNumConfigurations; i++) {
219 conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
221 usb_get_desc_config_ext(dev, i, conf);
227 s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, struct usb_conf *conf)
230 usb_get_descriptor(dev, CONFIGURATION, index, gbuf, 8);
231 usb_get_descriptor(dev, CONFIGURATION, index, gbuf, gbuf[0]);
233 conf->bLength = gbuf[0];
234 conf->bDescriptorType = gbuf[1];
235 conf->wTotalLength = (u16) (gbuf[3] << 8 | gbuf[2]);
236 conf->bNumInterfaces = gbuf[4];
237 conf->bConfigurationValue = gbuf[5];
238 conf->iConfiguration = gbuf[6];
239 conf->bmAttributes = gbuf[7];
240 conf->bMaxPower = gbuf[8];
246 /* returns more information about CONFIGURATION, including
247 * INTERFACE(s) and ENDPOINT(s)
248 * usb_get_desc_configuration() must be called for this device before
250 s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index, struct usb_conf *conf)
254 usb_get_desc_configuration(dev, index, conf);
255 usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
258 struct usb_intf *ifs = dev->conf->intf = (struct usb_intf*) malloc(sizeof(struct usb_intf));
259 for(i=1; i <= dev->conf->bNumInterfaces; i++) {
261 ifs->next = (struct usb_intf*) malloc(sizeof(struct usb_intf));
264 ifs->bLength = gbuf[off+0];
265 ifs->bDescriptorType = gbuf[off+1];
266 ifs->bInterfaceNumber = gbuf[off+2];
267 ifs->bAlternateSetting = gbuf[off+3];
268 ifs->bNumEndpoints = gbuf[off+4];
269 ifs->bInterfaceClass = gbuf[off+5];
270 ifs->bInterfaceSubClass = gbuf[off+6];
271 ifs->bInterfaceProtocol = gbuf[off+7];
272 ifs->iInterface = gbuf[off+8];
276 struct usb_endp *ep = ifs->endp = (struct usb_endp*) malloc(sizeof(struct usb_endp));
277 for(j=1; j <= ifs->bNumEndpoints; j++) {
278 /* skip HID Device Descriptor (see lsusb) */
279 if(gbuf[off+1] == 33) {
286 ep->next = (struct usb_endp*) malloc(sizeof(struct usb_endp));
290 ep->bLength = gbuf[off+0];
291 ep->bDescriptorType = gbuf[off+1];
292 ep->bEndpointAddress = gbuf[off+2];
293 ep->bmAttributes = gbuf[off+3];
294 ep->wMaxPacketSize = (u16) ((gbuf[off+5] << 8) | (gbuf[off+4]));
295 ep->bInterval = gbuf[off+6];
303 s8 usb_set_address(struct usb_device *dev, u8 address)
306 usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, gbuf, 0);
312 u8 usb_get_configuration(struct usb_device *dev)
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);
321 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
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);
331 s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
337 static s8 usb_gen_rw(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout, u8 type) {
338 static struct usb_irp irp;
339 memset(&irp, 0, sizeof(struct usb_irp));
343 irp.epsize = dev->epSize[ep & 0x7F];
348 irp.timeout = timeout;
350 usb_submit_irp(&irp);
354 /******************* Bulk Transfer **********************/
356 * Write to an a bulk endpoint.
358 s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
360 return usb_gen_rw(dev, ep, buf, size, timeout, USB_BULK);
364 * Read from an bulk endpoint.
366 s8 usb_bulk_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
368 return usb_gen_rw(dev, ep|0x80, buf, size, timeout, USB_BULK);
372 /******************* Interrupt Transfer **********************/
374 * Write to an interrupt endpoint.
376 s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
378 return usb_gen_rw(dev, ep, buf, size, timeout, USB_INTR);
382 * Read from an interrupt endpoint.
384 s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
386 return usb_gen_rw(dev, ep|0x80, buf, size, timeout, USB_INTR);
390 /******************* Isochron Transfer **********************/
393 * Write to an isochron endpoint.
395 s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
402 * Read from an isochron endpoint.
404 s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)