]> git.proxmox.com Git - mirror_qemu.git/blob - hw/usb/host-bsd.c
virtfs-proxy-helper: use setresuid and setresgid
[mirror_qemu.git] / hw / usb / host-bsd.c
1 /*
2 * BSD host USB redirector
3 *
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.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 */
26
27 #include "qemu-common.h"
28 #include "monitor.h"
29 #include "hw/usb.h"
30
31 /* usb.h declares these */
32 #undef USB_SPEED_HIGH
33 #undef USB_SPEED_FULL
34 #undef USB_SPEED_LOW
35
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
43 /* This value has maximum potential at 16.
44 * You should also set hw.usb.debug to gain
45 * more detailed view.
46 */
47 //#define DEBUG
48 #define UGEN_DEBUG_LEVEL 0
49
50
51 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
52 int vendor_id, int product_id,
53 const char *product_name, int speed);
54 static int usb_host_find_device(int *pbus_num, int *paddr,
55 const char *devname);
56
57 typedef struct USBHostDevice {
58 USBDevice dev;
59 int ep_fd[USB_MAX_ENDPOINTS];
60 int devfd;
61 char devpath[32];
62 } USBHostDevice;
63
64
65 static int ensure_ep_open(USBHostDevice *dev, int ep, int mode)
66 {
67 char buf[32];
68 int fd;
69
70 /* Get the address for this endpoint */
71 ep = UE_GET_ADDR(ep);
72
73 if (dev->ep_fd[ep] < 0) {
74 #if defined(__FreeBSD__) || defined(__DragonFly__)
75 snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->devpath, ep);
76 #else
77 snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->devpath, ep);
78 #endif
79 /* Try to open it O_RDWR first for those devices which have in and out
80 * endpoints with the same address (eg 0x02 and 0x82)
81 */
82 fd = open(buf, O_RDWR);
83 if (fd < 0 && errno == ENXIO)
84 fd = open(buf, mode);
85 if (fd < 0) {
86 #ifdef DEBUG
87 printf("ensure_ep_open: failed to open device endpoint %s: %s\n",
88 buf, strerror(errno));
89 #endif
90 }
91 dev->ep_fd[ep] = fd;
92 }
93
94 return dev->ep_fd[ep];
95 }
96
97 static void ensure_eps_closed(USBHostDevice *dev)
98 {
99 int epnum = 1;
100
101 if (!dev)
102 return;
103
104 while (epnum < USB_MAX_ENDPOINTS) {
105 if (dev->ep_fd[epnum] >= 0) {
106 close(dev->ep_fd[epnum]);
107 dev->ep_fd[epnum] = -1;
108 }
109 epnum++;
110 }
111 }
112
113 static void usb_host_handle_reset(USBDevice *dev)
114 {
115 #if 0
116 USBHostDevice *s = (USBHostDevice *)dev;
117 #endif
118 }
119
120 /* XXX:
121 * -check device states against transfer requests
122 * and return appropriate response
123 */
124 static void usb_host_handle_control(USBDevice *dev,
125 USBPacket *p,
126 int request,
127 int value,
128 int index,
129 int length,
130 uint8_t *data)
131 {
132 USBHostDevice *s = (USBHostDevice *)dev;
133 struct usb_ctl_request req;
134 struct usb_alt_interface aiface;
135 int ret, timeout = 50;
136
137 if ((request >> 8) == UT_WRITE_DEVICE &&
138 (request & 0xff) == UR_SET_ADDRESS) {
139
140 /* specific SET_ADDRESS support */
141 dev->addr = value;
142 } else if ((request >> 8) == UT_WRITE_DEVICE &&
143 (request & 0xff) == UR_SET_CONFIG) {
144
145 ensure_eps_closed(s); /* can't do this without all eps closed */
146
147 ret = ioctl(s->devfd, USB_SET_CONFIG, &value);
148 if (ret < 0) {
149 #ifdef DEBUG
150 printf("handle_control: failed to set configuration - %s\n",
151 strerror(errno));
152 #endif
153 p->status = USB_RET_STALL;
154 }
155 } else if ((request >> 8) == UT_WRITE_INTERFACE &&
156 (request & 0xff) == UR_SET_INTERFACE) {
157
158 aiface.uai_interface_index = index;
159 aiface.uai_alt_no = value;
160
161 ensure_eps_closed(s); /* can't do this without all eps closed */
162 ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface);
163 if (ret < 0) {
164 #ifdef DEBUG
165 printf("handle_control: failed to set alternate interface - %s\n",
166 strerror(errno));
167 #endif
168 p->status = USB_RET_STALL;
169 }
170 } else {
171 req.ucr_request.bmRequestType = request >> 8;
172 req.ucr_request.bRequest = request & 0xff;
173 USETW(req.ucr_request.wValue, value);
174 USETW(req.ucr_request.wIndex, index);
175 USETW(req.ucr_request.wLength, length);
176 req.ucr_data = data;
177 req.ucr_flags = USBD_SHORT_XFER_OK;
178
179 ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout);
180 #if defined(__NetBSD__) || defined(__OpenBSD__)
181 if (ret < 0 && errno != EINVAL) {
182 #else
183 if (ret < 0) {
184 #endif
185 #ifdef DEBUG
186 printf("handle_control: setting timeout failed - %s\n",
187 strerror(errno));
188 #endif
189 }
190
191 ret = ioctl(s->devfd, USB_DO_REQUEST, &req);
192 /* ugen returns EIO for usbd_do_request_ no matter what
193 * happens with the transfer */
194 if (ret < 0) {
195 #ifdef DEBUG
196 printf("handle_control: error after request - %s\n",
197 strerror(errno));
198 #endif
199 p->status = USB_RET_NAK; /* STALL */
200 } else {
201 p->actual_length = req.ucr_actlen;
202 }
203 }
204 }
205
206 static void usb_host_handle_data(USBDevice *dev, USBPacket *p)
207 {
208 USBHostDevice *s = (USBHostDevice *)dev;
209 int ret, fd, mode;
210 int one = 1, shortpacket = 0, timeout = 50;
211 sigset_t new_mask, old_mask;
212 uint8_t devep = p->ep->nr;
213
214 /* protect data transfers from SIGALRM signal */
215 sigemptyset(&new_mask);
216 sigaddset(&new_mask, SIGALRM);
217 sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
218
219 if (p->pid == USB_TOKEN_IN) {
220 devep |= 0x80;
221 mode = O_RDONLY;
222 shortpacket = 1;
223 } else {
224 mode = O_WRONLY;
225 }
226
227 fd = ensure_ep_open(s, devep, mode);
228 if (fd < 0) {
229 sigprocmask(SIG_SETMASK, &old_mask, NULL);
230 p->status = USB_RET_NODEV;
231 return;
232 }
233
234 if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) {
235 #ifdef DEBUG
236 printf("handle_data: failed to set timeout - %s\n",
237 strerror(errno));
238 #endif
239 }
240
241 if (shortpacket) {
242 if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) {
243 #ifdef DEBUG
244 printf("handle_data: failed to set short xfer mode - %s\n",
245 strerror(errno));
246 #endif
247 sigprocmask(SIG_SETMASK, &old_mask, NULL);
248 }
249 }
250
251 if (p->pid == USB_TOKEN_IN)
252 ret = readv(fd, p->iov.iov, p->iov.niov);
253 else
254 ret = writev(fd, p->iov.iov, p->iov.niov);
255
256 sigprocmask(SIG_SETMASK, &old_mask, NULL);
257
258 if (ret < 0) {
259 #ifdef DEBUG
260 printf("handle_data: error after %s data - %s\n",
261 pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno));
262 #endif
263 switch(errno) {
264 case ETIMEDOUT:
265 case EINTR:
266 p->status = USB_RET_NAK;
267 break;
268 default:
269 p->status = USB_RET_STALL;
270 }
271 } else {
272 p->actual_length = ret;
273 }
274 }
275
276 static void usb_host_handle_destroy(USBDevice *opaque)
277 {
278 USBHostDevice *s = (USBHostDevice *)opaque;
279 int i;
280
281 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
282 if (s->ep_fd[i] >= 0)
283 close(s->ep_fd[i]);
284
285 if (s->devfd < 0)
286 return;
287
288 close(s->devfd);
289
290 g_free(s);
291 }
292
293 static int usb_host_initfn(USBDevice *dev)
294 {
295 return 0;
296 }
297
298 USBDevice *usb_host_device_open(USBBus *guest_bus, const char *devname)
299 {
300 struct usb_device_info bus_info, dev_info;
301 USBDevice *d = NULL, *ret = NULL;
302 USBHostDevice *dev;
303 char ctlpath[PATH_MAX + 1];
304 char buspath[PATH_MAX + 1];
305 int bfd, dfd, bus, address, i;
306 int ugendebug = UGEN_DEBUG_LEVEL;
307
308 if (usb_host_find_device(&bus, &address, devname) < 0) {
309 goto fail;
310 }
311
312 snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
313
314 bfd = open(buspath, O_RDWR);
315 if (bfd < 0) {
316 #ifdef DEBUG
317 printf("usb_host_device_open: failed to open usb bus - %s\n",
318 strerror(errno));
319 #endif
320 goto fail;
321 }
322
323 bus_info.udi_addr = address;
324 if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) {
325 #ifdef DEBUG
326 printf("usb_host_device_open: failed to grab bus information - %s\n",
327 strerror(errno));
328 #endif
329 goto fail_bfd;
330 }
331
332 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
333 snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]);
334 #else
335 snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]);
336 #endif
337
338 dfd = open(ctlpath, O_RDWR);
339 if (dfd < 0) {
340 dfd = open(ctlpath, O_RDONLY);
341 if (dfd < 0) {
342 #ifdef DEBUG
343 printf("usb_host_device_open: failed to open usb device %s - %s\n",
344 ctlpath, strerror(errno));
345 #endif
346 }
347 goto fail_dfd;
348 }
349
350 if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) {
351 #ifdef DEBUG
352 printf("usb_host_device_open: failed to grab device info - %s\n",
353 strerror(errno));
354 #endif
355 goto fail_dfd;
356 }
357
358 d = usb_create(guest_bus, "usb-host");
359 dev = DO_UPCAST(USBHostDevice, dev, d);
360
361 if (dev_info.udi_speed == 1) {
362 dev->dev.speed = USB_SPEED_LOW - 1;
363 dev->dev.speedmask = USB_SPEED_MASK_LOW;
364 } else {
365 dev->dev.speed = USB_SPEED_FULL - 1;
366 dev->dev.speedmask = USB_SPEED_MASK_FULL;
367 }
368
369 if (strncmp(dev_info.udi_product, "product", 7) != 0) {
370 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
371 dev_info.udi_product);
372 } else {
373 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
374 "host:%s", devname);
375 }
376
377 pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/");
378 pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]);
379
380 /* Mark the endpoints as not yet open */
381 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
382 dev->ep_fd[i] = -1;
383 }
384
385 ioctl(dfd, USB_SETDEBUG, &ugendebug);
386
387 ret = (USBDevice *)dev;
388
389 fail_dfd:
390 close(dfd);
391 fail_bfd:
392 close(bfd);
393 fail:
394 return ret;
395 }
396
397 static void usb_host_class_initfn(ObjectClass *klass, void *data)
398 {
399 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
400
401 uc->product_desc = "USB Host Device";
402 uc->init = usb_host_initfn;
403 uc->handle_reset = usb_host_handle_reset;
404 uc->handle_control = usb_host_handle_control;
405 uc->handle_data = usb_host_handle_data;
406 uc->handle_destroy = usb_host_handle_destroy;
407 }
408
409 static TypeInfo usb_host_dev_info = {
410 .name = "usb-host",
411 .parent = TYPE_USB_DEVICE,
412 .instance_size = sizeof(USBHostDevice),
413 .class_init = usb_host_class_initfn,
414 };
415
416 static void usb_host_register_types(void)
417 {
418 type_register_static(&usb_host_dev_info);
419 }
420
421 type_init(usb_host_register_types)
422
423 static int usb_host_scan(void *opaque, USBScanFunc *func)
424 {
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;
431
432 for (bus = 0; bus < 10; bus++) {
433
434 snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus);
435 bfd = open(busbuf, O_RDWR);
436 if (bfd < 0)
437 continue;
438
439 for (address = 1; address < 127; address++) {
440
441 bus_info.udi_addr = address;
442 if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0)
443 continue;
444
445 /* only list devices that can be used by generic layer */
446 if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0)
447 continue;
448
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
454
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;
462 }
463
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));
467
468 /* XXX: might need to fixup endianness of word values before copying over */
469
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;
474
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';
480
481 ret = func(opaque, bus, address, class_id, vendor_id,
482 product_id, product_name, speed);
483
484 close(dfd);
485
486 if (ret)
487 goto the_end;
488 }
489
490 close(bfd);
491 }
492
493 the_end:
494 return ret;
495 }
496
497 typedef struct FindDeviceState {
498 int vendor_id;
499 int product_id;
500 int bus_num;
501 int addr;
502 } FindDeviceState;
503
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)
508 {
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;
517 }
518 }
519
520
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)
526 {
527 const char *p;
528 int ret;
529 FindDeviceState fs;
530
531 p = strchr(devname, '.');
532 if (p) {
533 *pbus_num = strtoul(devname, NULL, 0);
534 *paddr = strtoul(p + 1, NULL, 0);
535 return 0;
536 }
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;
546 }
547 }
548 return -1;
549 }
550
551 /**********************/
552 /* USB host device info */
553
554 struct usb_class_info {
555 int class;
556 const char *class_name;
557 };
558
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 }
574 };
575
576 static const char *usb_class_str(uint8_t class)
577 {
578 const struct usb_class_info *p;
579 for (p = usb_class_info; p->class != -1; p++) {
580 if (p->class == class)
581 break;
582 }
583 return p->class_name;
584 }
585
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)
590 {
591 const char *class_str, *speed_str;
592
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;
606 }
607
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");
619 }
620
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)
627 {
628 Monitor *mon = opaque;
629
630 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
631 product_name, speed);
632 return 0;
633 }
634
635 void usb_host_info(Monitor *mon)
636 {
637 usb_host_scan(mon, usb_host_info_device);
638 }
639
640 /* XXX add this */
641 int usb_host_device_close(const char *devname)
642 {
643 return 0;
644 }