linux-user: Add default configs for mips64[el]
[qemu.git] / usb-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 int 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 return 0;
143 } else if ((request >> 8) == UT_WRITE_DEVICE &&
144 (request & 0xff) == UR_SET_CONFIG) {
145
146 ensure_eps_closed(s); /* can't do this without all eps closed */
147
148 ret = ioctl(s->devfd, USB_SET_CONFIG, &value);
149 if (ret < 0) {
150 #ifdef DEBUG
151 printf("handle_control: failed to set configuration - %s\n",
152 strerror(errno));
153 #endif
154 return USB_RET_STALL;
155 }
156
157 return 0;
158 } else if ((request >> 8) == UT_WRITE_INTERFACE &&
159 (request & 0xff) == UR_SET_INTERFACE) {
160
161 aiface.uai_interface_index = index;
162 aiface.uai_alt_no = value;
163
164 ensure_eps_closed(s); /* can't do this without all eps closed */
165 ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface);
166 if (ret < 0) {
167 #ifdef DEBUG
168 printf("handle_control: failed to set alternate interface - %s\n",
169 strerror(errno));
170 #endif
171 return USB_RET_STALL;
172 }
173
174 return 0;
175 } else {
176 req.ucr_request.bmRequestType = request >> 8;
177 req.ucr_request.bRequest = request & 0xff;
178 USETW(req.ucr_request.wValue, value);
179 USETW(req.ucr_request.wIndex, index);
180 USETW(req.ucr_request.wLength, length);
181 req.ucr_data = data;
182 req.ucr_flags = USBD_SHORT_XFER_OK;
183
184 ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout);
185 #if defined(__NetBSD__) || defined(__OpenBSD__)
186 if (ret < 0 && errno != EINVAL) {
187 #else
188 if (ret < 0) {
189 #endif
190 #ifdef DEBUG
191 printf("handle_control: setting timeout failed - %s\n",
192 strerror(errno));
193 #endif
194 }
195
196 ret = ioctl(s->devfd, USB_DO_REQUEST, &req);
197 /* ugen returns EIO for usbd_do_request_ no matter what
198 * happens with the transfer */
199 if (ret < 0) {
200 #ifdef DEBUG
201 printf("handle_control: error after request - %s\n",
202 strerror(errno));
203 #endif
204 return USB_RET_NAK; // STALL
205 } else {
206 return req.ucr_actlen;
207 }
208 }
209 }
210
211 static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
212 {
213 USBHostDevice *s = (USBHostDevice *)dev;
214 int ret, fd, mode;
215 int one = 1, shortpacket = 0, timeout = 50;
216 sigset_t new_mask, old_mask;
217 uint8_t devep = p->devep;
218
219 /* protect data transfers from SIGALRM signal */
220 sigemptyset(&new_mask);
221 sigaddset(&new_mask, SIGALRM);
222 sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
223
224 if (p->pid == USB_TOKEN_IN) {
225 devep |= 0x80;
226 mode = O_RDONLY;
227 shortpacket = 1;
228 } else {
229 mode = O_WRONLY;
230 }
231
232 fd = ensure_ep_open(s, devep, mode);
233 if (fd < 0) {
234 sigprocmask(SIG_SETMASK, &old_mask, NULL);
235 return USB_RET_NODEV;
236 }
237
238 if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) {
239 #ifdef DEBUG
240 printf("handle_data: failed to set timeout - %s\n",
241 strerror(errno));
242 #endif
243 }
244
245 if (shortpacket) {
246 if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) {
247 #ifdef DEBUG
248 printf("handle_data: failed to set short xfer mode - %s\n",
249 strerror(errno));
250 #endif
251 sigprocmask(SIG_SETMASK, &old_mask, NULL);
252 }
253 }
254
255 if (p->pid == USB_TOKEN_IN)
256 ret = readv(fd, p->iov.iov, p->iov.niov);
257 else
258 ret = writev(fd, p->iov.iov, p->iov.niov);
259
260 sigprocmask(SIG_SETMASK, &old_mask, NULL);
261
262 if (ret < 0) {
263 #ifdef DEBUG
264 printf("handle_data: error after %s data - %s\n",
265 pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno));
266 #endif
267 switch(errno) {
268 case ETIMEDOUT:
269 case EINTR:
270 return USB_RET_NAK;
271 default:
272 return USB_RET_STALL;
273 }
274 } else {
275 return ret;
276 }
277 }
278
279 static void usb_host_handle_destroy(USBDevice *opaque)
280 {
281 USBHostDevice *s = (USBHostDevice *)opaque;
282 int i;
283
284 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
285 if (s->ep_fd[i] >= 0)
286 close(s->ep_fd[i]);
287
288 if (s->devfd < 0)
289 return;
290
291 close(s->devfd);
292
293 g_free(s);
294 }
295
296 static int usb_host_initfn(USBDevice *dev)
297 {
298 return 0;
299 }
300
301 USBDevice *usb_host_device_open(const char *devname)
302 {
303 struct usb_device_info bus_info, dev_info;
304 USBDevice *d = NULL, *ret = NULL;
305 USBHostDevice *dev;
306 char ctlpath[PATH_MAX + 1];
307 char buspath[PATH_MAX + 1];
308 int bfd, dfd, bus, address, i;
309 int ugendebug = UGEN_DEBUG_LEVEL;
310
311 if (usb_host_find_device(&bus, &address, devname) < 0) {
312 goto fail;
313 }
314
315 snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
316
317 bfd = open(buspath, O_RDWR);
318 if (bfd < 0) {
319 #ifdef DEBUG
320 printf("usb_host_device_open: failed to open usb bus - %s\n",
321 strerror(errno));
322 #endif
323 goto fail;
324 }
325
326 bus_info.udi_addr = address;
327 if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) {
328 #ifdef DEBUG
329 printf("usb_host_device_open: failed to grab bus information - %s\n",
330 strerror(errno));
331 #endif
332 goto fail_bfd;
333 }
334
335 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
336 snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]);
337 #else
338 snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]);
339 #endif
340
341 dfd = open(ctlpath, O_RDWR);
342 if (dfd < 0) {
343 dfd = open(ctlpath, O_RDONLY);
344 if (dfd < 0) {
345 #ifdef DEBUG
346 printf("usb_host_device_open: failed to open usb device %s - %s\n",
347 ctlpath, strerror(errno));
348 #endif
349 }
350 goto fail_dfd;
351 }
352
353 if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) {
354 #ifdef DEBUG
355 printf("usb_host_device_open: failed to grab device info - %s\n",
356 strerror(errno));
357 #endif
358 goto fail_dfd;
359 }
360
361 d = usb_create(NULL /* FIXME */, "usb-host");
362 dev = DO_UPCAST(USBHostDevice, dev, d);
363
364 if (dev_info.udi_speed == 1) {
365 dev->dev.speed = USB_SPEED_LOW - 1;
366 dev->dev.speedmask = USB_SPEED_MASK_LOW;
367 } else {
368 dev->dev.speed = USB_SPEED_FULL - 1;
369 dev->dev.speedmask = USB_SPEED_MASK_FULL;
370 }
371
372 if (strncmp(dev_info.udi_product, "product", 7) != 0) {
373 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
374 dev_info.udi_product);
375 } else {
376 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
377 "host:%s", devname);
378 }
379
380 pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/");
381 pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]);
382
383 /* Mark the endpoints as not yet open */
384 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
385 dev->ep_fd[i] = -1;
386 }
387
388 ioctl(dfd, USB_SETDEBUG, &ugendebug);
389
390 ret = (USBDevice *)dev;
391
392 fail_dfd:
393 close(dfd);
394 fail_bfd:
395 close(bfd);
396 fail:
397 return ret;
398 }
399
400 static void usb_host_class_initfn(ObjectClass *klass, void *data)
401 {
402 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
403
404 uc->product_desc = "USB Host Device";
405 uc->init = usb_host_initfn;
406 uc->handle_packet = usb_generic_handle_packet;
407 uc->handle_reset = usb_host_handle_reset;
408 uc->handle_control = usb_host_handle_control;
409 uc->handle_data = usb_host_handle_data;
410 uc->handle_destroy = usb_host_handle_destroy;
411 }
412
413 static struct DeviceInfo usb_host_dev_info = {
414 .name = "usb-host",
415 .size = sizeof(USBHostDevice),
416 .class_init= usb_host_initfn,
417 };
418
419 static void usb_host_register_devices(void)
420 {
421 usb_qdev_register(&usb_host_dev_info, NULL, NULL);
422 }
423 device_init(usb_host_register_devices)
424
425 static int usb_host_scan(void *opaque, USBScanFunc *func)
426 {
427 struct usb_device_info bus_info;
428 struct usb_device_info dev_info;
429 uint16_t vendor_id, product_id, class_id, speed;
430 int bfd, dfd, bus, address;
431 char busbuf[20], devbuf[20], product_name[256];
432 int ret = 0;
433
434 for (bus = 0; bus < 10; bus++) {
435
436 snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus);
437 bfd = open(busbuf, O_RDWR);
438 if (bfd < 0)
439 continue;
440
441 for (address = 1; address < 127; address++) {
442
443 bus_info.udi_addr = address;
444 if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0)
445 continue;
446
447 /* only list devices that can be used by generic layer */
448 if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0)
449 continue;
450
451 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
452 snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]);
453 #else
454 snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]);
455 #endif
456
457 dfd = open(devbuf, O_RDONLY);
458 if (dfd < 0) {
459 #ifdef DEBUG
460 printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
461 strerror(errno));
462 #endif
463 continue;
464 }
465
466 if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0)
467 printf("usb_host_scan: couldn't get device information for %s - %s\n",
468 devbuf, strerror(errno));
469
470 /* XXX: might need to fixup endianness of word values before copying over */
471
472 vendor_id = dev_info.udi_vendorNo;
473 product_id = dev_info.udi_productNo;
474 class_id = dev_info.udi_class;
475 speed = dev_info.udi_speed;
476
477 if (strncmp(dev_info.udi_product, "product", 7) != 0)
478 pstrcpy(product_name, sizeof(product_name),
479 dev_info.udi_product);
480 else
481 product_name[0] = '\0';
482
483 ret = func(opaque, bus, address, class_id, vendor_id,
484 product_id, product_name, speed);
485
486 close(dfd);
487
488 if (ret)
489 goto the_end;
490 }
491
492 close(bfd);
493 }
494
495 the_end:
496 return ret;
497 }
498
499 typedef struct FindDeviceState {
500 int vendor_id;
501 int product_id;
502 int bus_num;
503 int addr;
504 } FindDeviceState;
505
506 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
507 int class_id,
508 int vendor_id, int product_id,
509 const char *product_name, int speed)
510 {
511 FindDeviceState *s = opaque;
512 if (vendor_id == s->vendor_id &&
513 product_id == s->product_id) {
514 s->bus_num = bus_num;
515 s->addr = addr;
516 return 1;
517 } else {
518 return 0;
519 }
520 }
521
522
523 /* the syntax is :
524 'bus.addr' (decimal numbers) or
525 'vendor_id:product_id' (hexa numbers) */
526 static int usb_host_find_device(int *pbus_num, int *paddr,
527 const char *devname)
528 {
529 const char *p;
530 int ret;
531 FindDeviceState fs;
532
533 p = strchr(devname, '.');
534 if (p) {
535 *pbus_num = strtoul(devname, NULL, 0);
536 *paddr = strtoul(p + 1, NULL, 0);
537 return 0;
538 }
539 p = strchr(devname, ':');
540 if (p) {
541 fs.vendor_id = strtoul(devname, NULL, 16);
542 fs.product_id = strtoul(p + 1, NULL, 16);
543 ret = usb_host_scan(&fs, usb_host_find_device_scan);
544 if (ret) {
545 *pbus_num = fs.bus_num;
546 *paddr = fs.addr;
547 return 0;
548 }
549 }
550 return -1;
551 }
552
553 /**********************/
554 /* USB host device info */
555
556 struct usb_class_info {
557 int class;
558 const char *class_name;
559 };
560
561 static const struct usb_class_info usb_class_info[] = {
562 { USB_CLASS_AUDIO, "Audio"},
563 { USB_CLASS_COMM, "Communication"},
564 { USB_CLASS_HID, "HID"},
565 { USB_CLASS_HUB, "Hub" },
566 { USB_CLASS_PHYSICAL, "Physical" },
567 { USB_CLASS_PRINTER, "Printer" },
568 { USB_CLASS_MASS_STORAGE, "Storage" },
569 { USB_CLASS_CDC_DATA, "Data" },
570 { USB_CLASS_APP_SPEC, "Application Specific" },
571 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
572 { USB_CLASS_STILL_IMAGE, "Still Image" },
573 { USB_CLASS_CSCID, "Smart Card" },
574 { USB_CLASS_CONTENT_SEC, "Content Security" },
575 { -1, NULL }
576 };
577
578 static const char *usb_class_str(uint8_t class)
579 {
580 const struct usb_class_info *p;
581 for (p = usb_class_info; p->class != -1; p++) {
582 if (p->class == class)
583 break;
584 }
585 return p->class_name;
586 }
587
588 static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
589 int vendor_id, int product_id,
590 const char *product_name,
591 int speed)
592 {
593 const char *class_str, *speed_str;
594
595 switch(speed) {
596 case USB_SPEED_LOW:
597 speed_str = "1.5";
598 break;
599 case USB_SPEED_FULL:
600 speed_str = "12";
601 break;
602 case USB_SPEED_HIGH:
603 speed_str = "480";
604 break;
605 default:
606 speed_str = "?";
607 break;
608 }
609
610 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
611 bus_num, addr, speed_str);
612 class_str = usb_class_str(class_id);
613 if (class_str)
614 monitor_printf(mon, " %s:", class_str);
615 else
616 monitor_printf(mon, " Class %02x:", class_id);
617 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
618 if (product_name[0] != '\0')
619 monitor_printf(mon, ", %s", product_name);
620 monitor_printf(mon, "\n");
621 }
622
623 static int usb_host_info_device(void *opaque,
624 int bus_num, int addr,
625 int class_id,
626 int vendor_id, int product_id,
627 const char *product_name,
628 int speed)
629 {
630 Monitor *mon = opaque;
631
632 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
633 product_name, speed);
634 return 0;
635 }
636
637 void usb_host_info(Monitor *mon)
638 {
639 usb_host_scan(mon, usb_host_info_device);
640 }
641
642 /* XXX add this */
643 int usb_host_device_close(const char *devname)
644 {
645 return 0;
646 }