removed unused code
[qemu/mini2440.git] / usb-linux.c
blob0a13753d4806ddae083bb11e51432dec38eb41ac
1 /*
2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "vl.h"
26 #if defined(__linux__)
27 #include <dirent.h>
28 #include <sys/ioctl.h>
29 #include <linux/compiler.h>
30 #include <linux/usbdevice_fs.h>
31 #include <linux/version.h>
33 /* We redefine it to avoid version problems */
34 struct usb_ctrltransfer {
35 uint8_t bRequestType;
36 uint8_t bRequest;
37 uint16_t wValue;
38 uint16_t wIndex;
39 uint16_t wLength;
40 uint32_t timeout;
41 void *data;
44 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
45 int vendor_id, int product_id,
46 const char *product_name, int speed);
47 static int usb_host_find_device(int *pbus_num, int *paddr,
48 char *product_name, int product_name_size,
49 const char *devname);
51 //#define DEBUG
53 #define USBDEVFS_PATH "/proc/bus/usb"
54 #define PRODUCT_NAME_SZ 32
56 typedef struct USBHostDevice {
57 USBDevice dev;
58 int fd;
59 } USBHostDevice;
61 static void usb_host_handle_reset(USBDevice *dev)
63 #if 0
64 USBHostDevice *s = (USBHostDevice *)dev;
65 /* USBDEVFS_RESET, but not the first time as it has already be
66 done by the host OS */
67 ioctl(s->fd, USBDEVFS_RESET);
68 #endif
71 static void usb_host_handle_destroy(USBDevice *dev)
73 USBHostDevice *s = (USBHostDevice *)dev;
75 if (s->fd >= 0)
76 close(s->fd);
77 qemu_free(s);
80 static int usb_host_handle_control(USBDevice *dev,
81 int request,
82 int value,
83 int index,
84 int length,
85 uint8_t *data)
87 USBHostDevice *s = (USBHostDevice *)dev;
88 struct usb_ctrltransfer ct;
89 int ret;
91 if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
92 /* specific SET_ADDRESS support */
93 dev->addr = value;
94 return 0;
95 } else {
96 ct.bRequestType = request >> 8;
97 ct.bRequest = request;
98 ct.wValue = value;
99 ct.wIndex = index;
100 ct.wLength = length;
101 ct.timeout = 50;
102 ct.data = data;
103 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
104 if (ret < 0) {
105 switch(errno) {
106 case ETIMEDOUT:
107 return USB_RET_NAK;
108 default:
109 return USB_RET_STALL;
111 } else {
112 return ret;
117 static int usb_host_handle_data(USBDevice *dev, int pid,
118 uint8_t devep,
119 uint8_t *data, int len)
121 USBHostDevice *s = (USBHostDevice *)dev;
122 struct usbdevfs_bulktransfer bt;
123 int ret;
125 /* XXX: optimize and handle all data types by looking at the
126 config descriptor */
127 if (pid == USB_TOKEN_IN)
128 devep |= 0x80;
129 bt.ep = devep;
130 bt.len = len;
131 bt.timeout = 50;
132 bt.data = data;
133 ret = ioctl(s->fd, USBDEVFS_BULK, &bt);
134 if (ret < 0) {
135 switch(errno) {
136 case ETIMEDOUT:
137 return USB_RET_NAK;
138 case EPIPE:
139 default:
140 #ifdef DEBUG
141 printf("handle_data: errno=%d\n", errno);
142 #endif
143 return USB_RET_STALL;
145 } else {
146 return ret;
150 /* XXX: exclude high speed devices or implement EHCI */
151 USBDevice *usb_host_device_open(const char *devname)
153 int fd, interface, ret, i;
154 USBHostDevice *dev;
155 struct usbdevfs_connectinfo ci;
156 uint8_t descr[1024];
157 char buf[1024];
158 int descr_len, dev_descr_len, config_descr_len, nb_interfaces;
159 int bus_num, addr;
160 char product_name[PRODUCT_NAME_SZ];
162 if (usb_host_find_device(&bus_num, &addr,
163 product_name, sizeof(product_name),
164 devname) < 0)
165 return NULL;
167 snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d",
168 bus_num, addr);
169 fd = open(buf, O_RDWR);
170 if (fd < 0) {
171 perror(buf);
172 return NULL;
175 /* read the config description */
176 descr_len = read(fd, descr, sizeof(descr));
177 if (descr_len <= 0) {
178 perror("read descr");
179 goto fail;
182 i = 0;
183 dev_descr_len = descr[0];
184 if (dev_descr_len > descr_len)
185 goto fail;
186 i += dev_descr_len;
187 config_descr_len = descr[i];
188 if (i + config_descr_len > descr_len)
189 goto fail;
190 nb_interfaces = descr[i + 4];
191 if (nb_interfaces != 1) {
192 /* NOTE: currently we grab only one interface */
193 fprintf(stderr, "usb_host: only one interface supported\n");
194 goto fail;
197 #ifdef USBDEVFS_DISCONNECT
198 /* earlier Linux 2.4 do not support that */
200 struct usbdevfs_ioctl ctrl;
201 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
202 ctrl.ifno = 0;
203 ret = ioctl(fd, USBDEVFS_IOCTL, &ctrl);
204 if (ret < 0 && errno != ENODATA) {
205 perror("USBDEVFS_DISCONNECT");
206 goto fail;
209 #endif
211 /* XXX: only grab if all interfaces are free */
212 interface = 0;
213 ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interface);
214 if (ret < 0) {
215 if (errno == EBUSY) {
216 fprintf(stderr, "usb_host: device already grabbed\n");
217 } else {
218 perror("USBDEVFS_CLAIMINTERFACE");
220 fail:
221 close(fd);
222 return NULL;
225 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
226 if (ret < 0) {
227 perror("USBDEVFS_CONNECTINFO");
228 goto fail;
231 #ifdef DEBUG
232 printf("host USB device %d.%d grabbed\n", bus_num, addr);
233 #endif
235 dev = qemu_mallocz(sizeof(USBHostDevice));
236 if (!dev)
237 goto fail;
238 dev->fd = fd;
239 if (ci.slow)
240 dev->dev.speed = USB_SPEED_LOW;
241 else
242 dev->dev.speed = USB_SPEED_HIGH;
243 dev->dev.handle_packet = usb_generic_handle_packet;
245 dev->dev.handle_reset = usb_host_handle_reset;
246 dev->dev.handle_control = usb_host_handle_control;
247 dev->dev.handle_data = usb_host_handle_data;
248 dev->dev.handle_destroy = usb_host_handle_destroy;
250 if (product_name[0] == '\0')
251 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
252 "host:%s", devname);
253 else
254 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
255 product_name);
257 return (USBDevice *)dev;
260 static int get_tag_value(char *buf, int buf_size,
261 const char *str, const char *tag,
262 const char *stopchars)
264 const char *p;
265 char *q;
266 p = strstr(str, tag);
267 if (!p)
268 return -1;
269 p += strlen(tag);
270 while (isspace(*p))
271 p++;
272 q = buf;
273 while (*p != '\0' && !strchr(stopchars, *p)) {
274 if ((q - buf) < (buf_size - 1))
275 *q++ = *p;
276 p++;
278 *q = '\0';
279 return q - buf;
282 static int usb_host_scan(void *opaque, USBScanFunc *func)
284 FILE *f;
285 char line[1024];
286 char buf[1024];
287 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
288 int ret;
289 char product_name[512];
291 f = fopen(USBDEVFS_PATH "/devices", "r");
292 if (!f) {
293 term_printf("Could not open %s\n", USBDEVFS_PATH "/devices");
294 return 0;
296 device_count = 0;
297 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
298 ret = 0;
299 for(;;) {
300 if (fgets(line, sizeof(line), f) == NULL)
301 break;
302 if (strlen(line) > 0)
303 line[strlen(line) - 1] = '\0';
304 if (line[0] == 'T' && line[1] == ':') {
305 if (device_count && (vendor_id || product_id)) {
306 /* New device. Add the previously discovered device. */
307 ret = func(opaque, bus_num, addr, class_id, vendor_id,
308 product_id, product_name, speed);
309 if (ret)
310 goto the_end;
312 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
313 goto fail;
314 bus_num = atoi(buf);
315 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
316 goto fail;
317 addr = atoi(buf);
318 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
319 goto fail;
320 if (!strcmp(buf, "480"))
321 speed = USB_SPEED_HIGH;
322 else if (!strcmp(buf, "1.5"))
323 speed = USB_SPEED_LOW;
324 else
325 speed = USB_SPEED_FULL;
326 product_name[0] = '\0';
327 class_id = 0xff;
328 device_count++;
329 product_id = 0;
330 vendor_id = 0;
331 } else if (line[0] == 'P' && line[1] == ':') {
332 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
333 goto fail;
334 vendor_id = strtoul(buf, NULL, 16);
335 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
336 goto fail;
337 product_id = strtoul(buf, NULL, 16);
338 } else if (line[0] == 'S' && line[1] == ':') {
339 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
340 goto fail;
341 pstrcpy(product_name, sizeof(product_name), buf);
342 } else if (line[0] == 'D' && line[1] == ':') {
343 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
344 goto fail;
345 class_id = strtoul(buf, NULL, 16);
347 fail: ;
349 if (device_count && (vendor_id || product_id)) {
350 /* Add the last device. */
351 ret = func(opaque, bus_num, addr, class_id, vendor_id,
352 product_id, product_name, speed);
354 the_end:
355 fclose(f);
356 return ret;
359 typedef struct FindDeviceState {
360 int vendor_id;
361 int product_id;
362 int bus_num;
363 int addr;
364 char product_name[PRODUCT_NAME_SZ];
365 } FindDeviceState;
367 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
368 int class_id,
369 int vendor_id, int product_id,
370 const char *product_name, int speed)
372 FindDeviceState *s = opaque;
373 if ((vendor_id == s->vendor_id &&
374 product_id == s->product_id) ||
375 (bus_num == s->bus_num &&
376 addr == s->addr)) {
377 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
378 s->bus_num = bus_num;
379 s->addr = addr;
380 return 1;
381 } else {
382 return 0;
386 /* the syntax is :
387 'bus.addr' (decimal numbers) or
388 'vendor_id:product_id' (hexa numbers) */
389 static int usb_host_find_device(int *pbus_num, int *paddr,
390 char *product_name, int product_name_size,
391 const char *devname)
393 const char *p;
394 int ret;
395 FindDeviceState fs;
397 p = strchr(devname, '.');
398 if (p) {
399 *pbus_num = strtoul(devname, NULL, 0);
400 *paddr = strtoul(p + 1, NULL, 0);
401 fs.bus_num = *pbus_num;
402 fs.addr = *paddr;
403 ret = usb_host_scan(&fs, usb_host_find_device_scan);
404 if (ret)
405 pstrcpy(product_name, product_name_size, fs.product_name);
406 return 0;
408 p = strchr(devname, ':');
409 if (p) {
410 fs.vendor_id = strtoul(devname, NULL, 16);
411 fs.product_id = strtoul(p + 1, NULL, 16);
412 ret = usb_host_scan(&fs, usb_host_find_device_scan);
413 if (ret) {
414 *pbus_num = fs.bus_num;
415 *paddr = fs.addr;
416 pstrcpy(product_name, product_name_size, fs.product_name);
417 return 0;
420 return -1;
423 /**********************/
424 /* USB host device info */
426 struct usb_class_info {
427 int class;
428 const char *class_name;
431 static const struct usb_class_info usb_class_info[] = {
432 { USB_CLASS_AUDIO, "Audio"},
433 { USB_CLASS_COMM, "Communication"},
434 { USB_CLASS_HID, "HID"},
435 { USB_CLASS_HUB, "Hub" },
436 { USB_CLASS_PHYSICAL, "Physical" },
437 { USB_CLASS_PRINTER, "Printer" },
438 { USB_CLASS_MASS_STORAGE, "Storage" },
439 { USB_CLASS_CDC_DATA, "Data" },
440 { USB_CLASS_APP_SPEC, "Application Specific" },
441 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
442 { USB_CLASS_STILL_IMAGE, "Still Image" },
443 { USB_CLASS_CSCID, "Smart Card" },
444 { USB_CLASS_CONTENT_SEC, "Content Security" },
445 { -1, NULL }
448 static const char *usb_class_str(uint8_t class)
450 const struct usb_class_info *p;
451 for(p = usb_class_info; p->class != -1; p++) {
452 if (p->class == class)
453 break;
455 return p->class_name;
458 void usb_info_device(int bus_num, int addr, int class_id,
459 int vendor_id, int product_id,
460 const char *product_name,
461 int speed)
463 const char *class_str, *speed_str;
465 switch(speed) {
466 case USB_SPEED_LOW:
467 speed_str = "1.5";
468 break;
469 case USB_SPEED_FULL:
470 speed_str = "12";
471 break;
472 case USB_SPEED_HIGH:
473 speed_str = "480";
474 break;
475 default:
476 speed_str = "?";
477 break;
480 term_printf(" Device %d.%d, speed %s Mb/s\n",
481 bus_num, addr, speed_str);
482 class_str = usb_class_str(class_id);
483 if (class_str)
484 term_printf(" %s:", class_str);
485 else
486 term_printf(" Class %02x:", class_id);
487 term_printf(" USB device %04x:%04x", vendor_id, product_id);
488 if (product_name[0] != '\0')
489 term_printf(", %s", product_name);
490 term_printf("\n");
493 static int usb_host_info_device(void *opaque, int bus_num, int addr,
494 int class_id,
495 int vendor_id, int product_id,
496 const char *product_name,
497 int speed)
499 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
500 product_name, speed);
501 return 0;
504 void usb_host_info(void)
506 usb_host_scan(NULL, usb_host_info_device);
509 #else
511 void usb_host_info(void)
513 term_printf("USB host devices not supported\n");
516 /* XXX: modify configure to compile the right host driver */
517 USBDevice *usb_host_device_open(const char *devname)
519 return NULL;
522 #endif