json-parser: propagate error from parser
[qemu.git] / usb-bsd.c
blob9bab6e353be2532fd20235a399d0bac2e836efe0
1 /*
2 * BSD host USB redirector
4 * Copyright (c) 2006 Lonnie Mendez
5 * Portions of code and concepts borrowed from
6 * usb-linux.c and libusb's bsd.c and are copyright their respective owners.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "qemu-common.h"
28 #include "monitor.h"
29 #include "hw/usb.h"
31 /* usb.h declares these */
32 #undef USB_SPEED_HIGH
33 #undef USB_SPEED_FULL
34 #undef USB_SPEED_LOW
36 #include <sys/ioctl.h>
37 #ifndef __DragonFly__
38 #include <dev/usb/usb.h>
39 #else
40 #include <bus/usb/usb.h>
41 #endif
42 #include <signal.h>
44 /* This value has maximum potential at 16.
45 * You should also set hw.usb.debug to gain
46 * more detailed view.
48 //#define DEBUG
49 #define UGEN_DEBUG_LEVEL 0
52 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
53 int vendor_id, int product_id,
54 const char *product_name, int speed);
55 static int usb_host_find_device(int *pbus_num, int *paddr,
56 const char *devname);
58 typedef struct USBHostDevice {
59 USBDevice dev;
60 int ep_fd[USB_MAX_ENDPOINTS];
61 int devfd;
62 char devpath[32];
63 } USBHostDevice;
66 #if 0
67 static int ensure_ep_open(USBHostDevice *dev, int ep, int mode)
69 char buf[32];
70 int fd;
72 /* Get the address for this endpoint */
73 ep = UE_GET_ADDR(ep);
75 if (dev->ep_fd[ep] < 0) {
76 #if defined(__FreeBSD__) || defined(__DragonFly__)
77 snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->devpath, ep);
78 #else
79 snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->devpath, ep);
80 #endif
81 /* Try to open it O_RDWR first for those devices which have in and out
82 * endpoints with the same address (eg 0x02 and 0x82)
84 fd = open(buf, O_RDWR);
85 if (fd < 0 && errno == ENXIO)
86 fd = open(buf, mode);
87 if (fd < 0) {
88 #ifdef DEBUG
89 printf("ensure_ep_open: failed to open device endpoint %s: %s\n",
90 buf, strerror(errno));
91 #endif
93 dev->ep_fd[ep] = fd;
96 return dev->ep_fd[ep];
99 static void ensure_eps_closed(USBHostDevice *dev)
101 int epnum = 1;
103 if (!dev)
104 return;
106 while (epnum < USB_MAX_ENDPOINTS) {
107 if (dev->ep_fd[epnum] >= 0) {
108 close(dev->ep_fd[epnum]);
109 dev->ep_fd[epnum] = -1;
111 epnum++;
114 #endif
116 static void usb_host_handle_reset(USBDevice *dev)
118 #if 0
119 USBHostDevice *s = (USBHostDevice *)dev;
120 #endif
123 #if 0
124 /* XXX:
125 * -check device states against transfer requests
126 * and return appropriate response
128 static int usb_host_handle_control(USBDevice *dev,
129 USBPacket *p,
130 int request,
131 int value,
132 int index,
133 int length,
134 uint8_t *data)
136 USBHostDevice *s = (USBHostDevice *)dev;
137 struct usb_ctl_request req;
138 struct usb_alt_interface aiface;
139 int ret, timeout = 50;
141 if ((request >> 8) == UT_WRITE_DEVICE &&
142 (request & 0xff) == UR_SET_ADDRESS) {
144 /* specific SET_ADDRESS support */
145 dev->addr = value;
146 return 0;
147 } else if ((request >> 8) == UT_WRITE_DEVICE &&
148 (request & 0xff) == UR_SET_CONFIG) {
150 ensure_eps_closed(s); /* can't do this without all eps closed */
152 ret = ioctl(s->devfd, USB_SET_CONFIG, &value);
153 if (ret < 0) {
154 #ifdef DEBUG
155 printf("handle_control: failed to set configuration - %s\n",
156 strerror(errno));
157 #endif
158 return USB_RET_STALL;
161 return 0;
162 } else if ((request >> 8) == UT_WRITE_INTERFACE &&
163 (request & 0xff) == UR_SET_INTERFACE) {
165 aiface.uai_interface_index = index;
166 aiface.uai_alt_no = value;
168 ensure_eps_closed(s); /* can't do this without all eps closed */
169 ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface);
170 if (ret < 0) {
171 #ifdef DEBUG
172 printf("handle_control: failed to set alternate interface - %s\n",
173 strerror(errno));
174 #endif
175 return USB_RET_STALL;
178 return 0;
179 } else {
180 req.ucr_request.bmRequestType = request >> 8;
181 req.ucr_request.bRequest = request & 0xff;
182 USETW(req.ucr_request.wValue, value);
183 USETW(req.ucr_request.wIndex, index);
184 USETW(req.ucr_request.wLength, length);
185 req.ucr_data = data;
186 req.ucr_flags = USBD_SHORT_XFER_OK;
188 ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout);
189 #if defined(__NetBSD__) || defined(__OpenBSD__)
190 if (ret < 0 && errno != EINVAL) {
191 #else
192 if (ret < 0) {
193 #endif
194 #ifdef DEBUG
195 printf("handle_control: setting timeout failed - %s\n",
196 strerror(errno));
197 #endif
200 ret = ioctl(s->devfd, USB_DO_REQUEST, &req);
201 /* ugen returns EIO for usbd_do_request_ no matter what
202 * happens with the transfer */
203 if (ret < 0) {
204 #ifdef DEBUG
205 printf("handle_control: error after request - %s\n",
206 strerror(errno));
207 #endif
208 return USB_RET_NAK; // STALL
209 } else {
210 return req.ucr_actlen;
215 static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
217 USBHostDevice *s = (USBHostDevice *)dev;
218 int ret, fd, mode;
219 int one = 1, shortpacket = 0, timeout = 50;
220 sigset_t new_mask, old_mask;
221 uint8_t devep = p->devep;
223 /* protect data transfers from SIGALRM signal */
224 sigemptyset(&new_mask);
225 sigaddset(&new_mask, SIGALRM);
226 sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
228 if (p->pid == USB_TOKEN_IN) {
229 devep |= 0x80;
230 mode = O_RDONLY;
231 shortpacket = 1;
232 } else {
233 mode = O_WRONLY;
236 fd = ensure_ep_open(s, devep, mode);
237 if (fd < 0) {
238 sigprocmask(SIG_SETMASK, &old_mask, NULL);
239 return USB_RET_NODEV;
242 if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) {
243 #ifdef DEBUG
244 printf("handle_data: failed to set timeout - %s\n",
245 strerror(errno));
246 #endif
249 if (shortpacket) {
250 if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) {
251 #ifdef DEBUG
252 printf("handle_data: failed to set short xfer mode - %s\n",
253 strerror(errno));
254 #endif
255 sigprocmask(SIG_SETMASK, &old_mask, NULL);
259 if (p->pid == USB_TOKEN_IN)
260 ret = read(fd, p->data, p->len);
261 else
262 ret = write(fd, p->data, p->len);
264 sigprocmask(SIG_SETMASK, &old_mask, NULL);
266 if (ret < 0) {
267 #ifdef DEBUG
268 printf("handle_data: error after %s data - %s\n",
269 pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno));
270 #endif
271 switch(errno) {
272 case ETIMEDOUT:
273 case EINTR:
274 return USB_RET_NAK;
275 default:
276 return USB_RET_STALL;
278 } else {
279 return ret;
282 #endif
284 static void usb_host_handle_destroy(USBDevice *opaque)
286 USBHostDevice *s = (USBHostDevice *)opaque;
287 int i;
289 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
290 if (s->ep_fd[i] >= 0)
291 close(s->ep_fd[i]);
293 if (s->devfd < 0)
294 return;
296 close(s->devfd);
298 qemu_free(s);
301 static int usb_host_initfn(USBDevice *dev)
303 return 0;
306 USBDevice *usb_host_device_open(const char *devname)
308 struct usb_device_info bus_info, dev_info;
309 USBDevice *d = NULL;
310 USBHostDevice *dev, *ret = NULL;
311 char ctlpath[PATH_MAX + 1];
312 char buspath[PATH_MAX + 1];
313 int bfd, dfd, bus, address, i;
314 int ugendebug = UGEN_DEBUG_LEVEL;
316 if (usb_host_find_device(&bus, &address, devname) < 0) {
317 goto fail;
320 snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
322 bfd = open(buspath, O_RDWR);
323 if (bfd < 0) {
324 #ifdef DEBUG
325 printf("usb_host_device_open: failed to open usb bus - %s\n",
326 strerror(errno));
327 #endif
328 goto fail;
331 bus_info.udi_addr = address;
332 if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) {
333 #ifdef DEBUG
334 printf("usb_host_device_open: failed to grab bus information - %s\n",
335 strerror(errno));
336 #endif
337 goto fail_bfd;
340 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
341 snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]);
342 #else
343 snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]);
344 #endif
346 dfd = open(ctlpath, O_RDWR);
347 if (dfd < 0) {
348 dfd = open(ctlpath, O_RDONLY);
349 if (dfd < 0) {
350 #ifdef DEBUG
351 printf("usb_host_device_open: failed to open usb device %s - %s\n",
352 ctlpath, strerror(errno));
353 #endif
355 goto fail_dfd;
358 if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) {
359 #ifdef DEBUG
360 printf("usb_host_device_open: failed to grab device info - %s\n",
361 strerror(errno));
362 #endif
363 goto fail_dfd;
366 d = usb_create(NULL /* FIXME */, "usb-host");
367 dev = DO_UPCAST(USBHostDevice, dev, d);
369 if (dev_info.udi_speed == 1) {
370 dev->dev.speed = USB_SPEED_LOW - 1;
371 } else {
372 dev->dev.speed = USB_SPEED_FULL - 1;
375 if (strncmp(dev_info.udi_product, "product", 7) != 0) {
376 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
377 dev_info.udi_product);
378 } else {
379 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
380 "host:%s", devname);
383 pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/");
384 pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]);
386 /* Mark the endpoints as not yet open */
387 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
388 dev->ep_fd[i] = -1;
391 ioctl(dfd, USB_SETDEBUG, &ugendebug);
393 ret = (USBDevice *)dev;
395 fail_dfd:
396 close(dfd);
397 fail_bfd:
398 close(bfd);
399 fail:
400 return ret;
403 static struct USBDeviceInfo usb_host_dev_info = {
404 .product_desc = "USB Host Device",
405 .qdev.name = "usb-host",
406 .qdev.size = sizeof(USBHostDevice),
407 .init = usb_host_initfn,
408 .handle_packet = usb_generic_handle_packet,
409 .handle_reset = usb_host_handle_reset,
410 #if 0
411 .handle_control = usb_host_handle_control,
412 .handle_data = usb_host_handle_data,
413 #endif
414 .handle_destroy = usb_host_handle_destroy,
417 static void usb_host_register_devices(void)
419 usb_qdev_register(&usb_host_dev_info);
421 device_init(usb_host_register_devices)
423 static int usb_host_scan(void *opaque, USBScanFunc *func)
425 struct usb_device_info bus_info;
426 struct usb_device_info dev_info;
427 uint16_t vendor_id, product_id, class_id, speed;
428 int bfd, dfd, bus, address;
429 char busbuf[20], devbuf[20], product_name[256];
430 int ret = 0;
432 for (bus = 0; bus < 10; bus++) {
434 snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus);
435 bfd = open(busbuf, O_RDWR);
436 if (bfd < 0)
437 continue;
439 for (address = 1; address < 127; address++) {
441 bus_info.udi_addr = address;
442 if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0)
443 continue;
445 /* only list devices that can be used by generic layer */
446 if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0)
447 continue;
449 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
450 snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]);
451 #else
452 snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]);
453 #endif
455 dfd = open(devbuf, O_RDONLY);
456 if (dfd < 0) {
457 #ifdef DEBUG
458 printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
459 strerror(errno));
460 #endif
461 continue;
464 if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0)
465 printf("usb_host_scan: couldn't get device information for %s - %s\n",
466 devbuf, strerror(errno));
468 /* XXX: might need to fixup endianness of word values before copying over */
470 vendor_id = dev_info.udi_vendorNo;
471 product_id = dev_info.udi_productNo;
472 class_id = dev_info.udi_class;
473 speed = dev_info.udi_speed;
475 if (strncmp(dev_info.udi_product, "product", 7) != 0)
476 pstrcpy(product_name, sizeof(product_name),
477 dev_info.udi_product);
478 else
479 product_name[0] = '\0';
481 ret = func(opaque, bus, address, class_id, vendor_id,
482 product_id, product_name, speed);
484 close(dfd);
486 if (ret)
487 goto the_end;
490 close(bfd);
493 the_end:
494 return ret;
497 typedef struct FindDeviceState {
498 int vendor_id;
499 int product_id;
500 int bus_num;
501 int addr;
502 } FindDeviceState;
504 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
505 int class_id,
506 int vendor_id, int product_id,
507 const char *product_name, int speed)
509 FindDeviceState *s = opaque;
510 if (vendor_id == s->vendor_id &&
511 product_id == s->product_id) {
512 s->bus_num = bus_num;
513 s->addr = addr;
514 return 1;
515 } else {
516 return 0;
521 /* the syntax is :
522 'bus.addr' (decimal numbers) or
523 'vendor_id:product_id' (hexa numbers) */
524 static int usb_host_find_device(int *pbus_num, int *paddr,
525 const char *devname)
527 const char *p;
528 int ret;
529 FindDeviceState fs;
531 p = strchr(devname, '.');
532 if (p) {
533 *pbus_num = strtoul(devname, NULL, 0);
534 *paddr = strtoul(p + 1, NULL, 0);
535 return 0;
537 p = strchr(devname, ':');
538 if (p) {
539 fs.vendor_id = strtoul(devname, NULL, 16);
540 fs.product_id = strtoul(p + 1, NULL, 16);
541 ret = usb_host_scan(&fs, usb_host_find_device_scan);
542 if (ret) {
543 *pbus_num = fs.bus_num;
544 *paddr = fs.addr;
545 return 0;
548 return -1;
551 /**********************/
552 /* USB host device info */
554 struct usb_class_info {
555 int class;
556 const char *class_name;
559 static const struct usb_class_info usb_class_info[] = {
560 { USB_CLASS_AUDIO, "Audio"},
561 { USB_CLASS_COMM, "Communication"},
562 { USB_CLASS_HID, "HID"},
563 { USB_CLASS_HUB, "Hub" },
564 { USB_CLASS_PHYSICAL, "Physical" },
565 { USB_CLASS_PRINTER, "Printer" },
566 { USB_CLASS_MASS_STORAGE, "Storage" },
567 { USB_CLASS_CDC_DATA, "Data" },
568 { USB_CLASS_APP_SPEC, "Application Specific" },
569 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
570 { USB_CLASS_STILL_IMAGE, "Still Image" },
571 { USB_CLASS_CSCID, "Smart Card" },
572 { USB_CLASS_CONTENT_SEC, "Content Security" },
573 { -1, NULL }
576 static const char *usb_class_str(uint8_t class)
578 const struct usb_class_info *p;
579 for (p = usb_class_info; p->class != -1; p++) {
580 if (p->class == class)
581 break;
583 return p->class_name;
586 static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
587 int vendor_id, int product_id,
588 const char *product_name,
589 int speed)
591 const char *class_str, *speed_str;
593 switch(speed) {
594 case USB_SPEED_LOW:
595 speed_str = "1.5";
596 break;
597 case USB_SPEED_FULL:
598 speed_str = "12";
599 break;
600 case USB_SPEED_HIGH:
601 speed_str = "480";
602 break;
603 default:
604 speed_str = "?";
605 break;
608 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
609 bus_num, addr, speed_str);
610 class_str = usb_class_str(class_id);
611 if (class_str)
612 monitor_printf(mon, " %s:", class_str);
613 else
614 monitor_printf(mon, " Class %02x:", class_id);
615 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
616 if (product_name[0] != '\0')
617 monitor_printf(mon, ", %s", product_name);
618 monitor_printf(mon, "\n");
621 static int usb_host_info_device(void *opaque,
622 int bus_num, int addr,
623 int class_id,
624 int vendor_id, int product_id,
625 const char *product_name,
626 int speed)
628 Monitor *mon = opaque;
630 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
631 product_name, speed);
632 return 0;
635 void usb_host_info(Monitor *mon)
637 usb_host_scan(mon, usb_host_info_device);
640 /* XXX add this */
641 int usb_host_device_close(const char *devname)
643 return 0;