56
struct usb_ctrlrequest {
46
64
typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
47
65
int vendor_id, int product_id,
48
66
const char *product_name, int speed);
49
67
static int usb_host_find_device(int *pbus_num, int *paddr,
50
68
char *product_name, int product_name_size,
51
69
const char *devname);
57
#define USBDEVFS_PATH "/proc/bus/usb"
73
#define dprintf printf
78
#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
80
#define USBPROCBUS_PATH "/proc/bus/usb"
58
81
#define PRODUCT_NAME_SZ 32
59
#define SIG_ISOCOMPLETE (SIGRTMIN+7)
60
82
#define MAX_ENDPOINTS 16
62
struct sigaction sigact;
83
#define USBDEVBUS_PATH "/dev/bus/usb"
84
#define USBSYSBUS_PATH "/sys/bus/usb"
86
static char *usb_host_device_path;
93
static int usb_fs_type;
64
95
/* endpoint association data */
69
/* FIXME: move USBPacket to PendingURB */
109
* Control transfer state.
110
* Note that 'buffer' _must_ follow 'req' field because
111
* we need contigious buffer when we submit control URB.
117
struct usb_ctrlrequest req;
118
uint8_t buffer[1024];
70
121
typedef struct USBHostDevice {
131
struct ctrl_struct ctrl;
75
132
struct endp_data endp_table[MAX_ENDPOINTS];
134
/* Host side address */
138
struct USBHostDevice *next;
82
typedef struct PendingURB {
83
struct usbdevfs_urb *urb;
85
struct PendingURB *next;
88
static PendingURB *pending_urbs = NULL;
90
static int add_pending_urb(struct usbdevfs_urb *urb)
92
PendingURB *purb = qemu_mallocz(sizeof(PendingURB));
96
purb->next = pending_urbs;
103
static int del_pending_urb(struct usbdevfs_urb *urb)
105
PendingURB *purb = pending_urbs;
106
PendingURB *prev = NULL;
108
while (purb && purb->urb != urb) {
113
if (purb && purb->urb == urb) {
115
prev->next = purb->next;
117
pending_urbs = purb->next;
141
static int is_isoc(USBHostDevice *s, int ep)
143
return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
146
static int is_halted(USBHostDevice *s, int ep)
148
return s->endp_table[ep - 1].halted;
151
static void clear_halt(USBHostDevice *s, int ep)
153
s->endp_table[ep - 1].halted = 0;
156
static void set_halt(USBHostDevice *s, int ep)
158
s->endp_table[ep - 1].halted = 1;
161
static USBHostDevice *hostdev_list;
163
static void hostdev_link(USBHostDevice *dev)
165
dev->next = hostdev_list;
169
static void hostdev_unlink(USBHostDevice *dev)
171
USBHostDevice *pdev = hostdev_list;
172
USBHostDevice **prev = &hostdev_list;
126
static PendingURB *get_pending_urb(struct usbdevfs_urb *urb)
185
static USBHostDevice *hostdev_find(int bus_num, int addr)
128
PendingURB *purb = pending_urbs;
130
while (purb && purb->urb != urb) {
134
if (purb && purb->urb == urb) {
187
USBHostDevice *s = hostdev_list;
189
if (s->bus_num == bus_num && s->addr == addr)
141
static int usb_host_update_interfaces(USBHostDevice *dev, int configuration)
198
* We always allocate one isoc descriptor even for bulk transfers
199
* to simplify allocation and casts.
201
typedef struct AsyncURB
203
struct usbdevfs_urb urb;
204
struct usbdevfs_iso_packet_desc isocpd;
210
static AsyncURB *async_alloc(void)
212
return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
215
static void async_free(AsyncURB *aurb)
220
static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
222
switch(s->ctrl.state) {
223
case CTRL_STATE_SETUP:
224
if (p->len < s->ctrl.len)
225
s->ctrl.len = p->len;
226
s->ctrl.state = CTRL_STATE_DATA;
231
s->ctrl.state = CTRL_STATE_IDLE;
240
static void async_complete(void *opaque)
242
USBHostDevice *s = opaque;
248
int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
253
if (errno == ENODEV && !s->closing) {
254
printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
255
usb_device_del_addr(0, s->dev.addr);
259
dprintf("husb: async. reap urb failed errno %d\n", errno);
265
dprintf("husb: async completed. aurb %p status %d alen %d\n",
266
aurb, aurb->urb.status, aurb->urb.actual_length);
269
switch (aurb->urb.status) {
271
p->len = aurb->urb.actual_length;
272
if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
273
async_complete_ctrl(s, p);
277
set_halt(s, p->devep);
280
p->len = USB_RET_NAK;
284
usb_packet_complete(p);
291
static void async_cancel(USBPacket *unused, void *opaque)
293
AsyncURB *aurb = opaque;
294
USBHostDevice *s = aurb->hdev;
296
dprintf("husb: async cancel. aurb %p\n", aurb);
298
/* Mark it as dead (see async_complete above) */
301
int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
303
dprintf("husb: async. discard urb failed errno %d\n", errno);
307
static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
143
309
int dev_descr_len, config_descr_len;
144
310
int interface, nb_interfaces, nb_configurations;
198
369
ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
200
371
if (errno == EBUSY) {
202
"usb_host: warning - device already grabbed\n");
372
printf("husb: update iface. device already grabbed\n");
204
perror("USBDEVFS_CLAIMINTERFACE");
374
perror("husb: failed to claim interface");
212
printf("usb_host: %d interfaces claimed for configuration %d\n",
381
printf("husb: %d interfaces claimed for configuration %d\n",
213
382
nb_interfaces, configuration);
384
dev->ninterfaces = nb_interfaces;
385
dev->configuration = configuration;
389
static int usb_host_release_interfaces(USBHostDevice *s)
393
dprintf("husb: releasing interfaces\n");
395
for (i = 0; i < s->ninterfaces; i++) {
396
ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
398
perror("husb: failed to release interface");
219
406
static void usb_host_handle_reset(USBDevice *dev)
222
USBHostDevice *s = (USBHostDevice *)dev;
223
/* USBDEVFS_RESET, but not the first time as it has already be
224
done by the host OS */
408
USBHostDevice *s = (USBHostDevice *) dev;
410
dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
225
412
ioctl(s->fd, USBDEVFS_RESET);
414
usb_host_claim_interfaces(s, s->configuration);
229
417
static void usb_host_handle_destroy(USBDevice *dev)
231
419
USBHostDevice *s = (USBHostDevice *)dev;
423
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
238
435
static int usb_linux_update_endp_table(USBHostDevice *s);
240
static int usb_host_handle_control(USBDevice *dev,
247
USBHostDevice *s = (USBHostDevice *)dev;
248
struct usb_ctrltransfer ct;
437
static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
439
struct usbdevfs_urb *urb;
443
aurb = async_alloc();
449
if (p->pid == USB_TOKEN_IN)
450
urb->endpoint = p->devep | 0x80;
452
urb->endpoint = p->devep;
454
if (is_halted(s, p->devep)) {
455
ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
457
dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
458
urb->endpoint, errno);
461
clear_halt(s, p->devep);
464
urb->buffer = p->data;
465
urb->buffer_length = p->len;
467
if (is_isoc(s, p->devep)) {
468
/* Setup ISOC transfer */
469
urb->type = USBDEVFS_URB_TYPE_ISO;
470
urb->flags = USBDEVFS_URB_ISO_ASAP;
471
urb->number_of_packets = 1;
472
urb->iso_frame_desc[0].length = p->len;
474
/* Setup bulk transfer */
475
urb->type = USBDEVFS_URB_TYPE_BULK;
478
urb->usercontext = s;
480
ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
482
dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
485
dprintf("husb: submit failed. errno %d\n", errno);
493
return USB_RET_STALL;
497
usb_defer_packet(p, async_cancel, aurb);
498
return USB_RET_ASYNC;
501
static int ctrl_error(void)
503
if (errno == ETIMEDOUT)
506
return USB_RET_STALL;
509
static int usb_host_set_address(USBHostDevice *s, int addr)
511
dprintf("husb: ctrl set addr %u\n", addr);
516
static int usb_host_set_config(USBHostDevice *s, int config)
518
usb_host_release_interfaces(s);
520
int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
522
dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
527
usb_host_claim_interfaces(s, config);
531
static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
249
533
struct usbdevfs_setinterface si;
250
int intf_update_required = 0;
253
if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
254
/* specific SET_ADDRESS support */
257
} else if (request == ((USB_RECIP_INTERFACE << 8) |
258
USB_REQ_SET_INTERFACE)) {
259
/* set alternate setting for the interface */
260
si.interface = index;
261
si.altsetting = value;
262
ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
263
usb_linux_update_endp_table(s);
264
} else if (request == (DeviceOutRequest | USB_REQ_SET_CONFIGURATION)) {
266
printf("usb_host_handle_control: SET_CONFIGURATION request - "
267
"config %d\n", value & 0xff);
269
if (s->configuration != (value & 0xff)) {
270
s->configuration = (value & 0xff);
271
intf_update_required = 1;
276
ct.bRequestType = request >> 8;
277
ct.bRequest = request;
283
ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
291
return USB_RET_STALL;
294
if (intf_update_required) {
296
printf("usb_host_handle_control: updating interfaces\n");
298
usb_host_update_interfaces(s, value & 0xff);
304
static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p);
306
static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
308
USBHostDevice *s = (USBHostDevice *)dev;
309
struct usbdevfs_bulktransfer bt;
311
uint8_t devep = p->devep;
313
if (s->endp_table[p->devep - 1].type == USBDEVFS_URB_TYPE_ISO) {
314
return usb_host_handle_isoch(dev, p);
317
/* XXX: optimize and handle all data types by looking at the
319
if (p->pid == USB_TOKEN_IN)
325
ret = ioctl(s->fd, USBDEVFS_BULK, &bt);
333
printf("handle_data: errno=%d\n", errno);
335
return USB_RET_STALL;
343
static void urb_completion_pipe_read(void *opaque)
345
USBHostDevice *s = opaque;
346
USBPacket *p = s->packet;
347
PendingURB *pending_urb = NULL;
348
struct usbdevfs_urb *purb = NULL;
351
len = read(s->pipe_fds[0], &pending_urb, sizeof(pending_urb));
352
if (len != sizeof(pending_urb)) {
353
printf("urb_completion: error reading pending_urb, len=%d\n", len);
357
/* FIXME: handle pending_urb->status */
358
del_pending_urb(pending_urb->urb);
365
ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
367
printf("urb_completion: REAPURBNDELAY ioctl=%d errno=%d\n",
373
if (purb == pending_urb->urb) {
374
printf("urb_completion: urb mismatch reaped=%p pending=%p\n",
379
p->len = purb->actual_length;
380
usb_packet_complete(p);
385
static void isoch_done(int signum, siginfo_t *info, void *context)
387
struct usbdevfs_urb *urb = (struct usbdevfs_urb *)info->si_addr;
388
USBHostDevice *s = (USBHostDevice *)urb->usercontext;
391
if (info->si_code != SI_ASYNCIO ||
392
info->si_signo != SIG_ISOCOMPLETE) {
396
purb = get_pending_urb(urb);
398
purb->status = info->si_errno;
399
write(s->pipe_fds[1], &purb, sizeof(purb));
404
static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p)
406
USBHostDevice *s = (USBHostDevice *)dev;
407
struct usbdevfs_urb *urb, *purb = NULL;
409
uint8_t devep = p->devep;
411
if (p->pid == USB_TOKEN_IN)
414
urb = qemu_mallocz(sizeof(struct usbdevfs_urb) +
415
sizeof(struct usbdevfs_iso_packet_desc));
417
printf("usb_host_handle_isoch: malloc failed\n");
421
urb->type = USBDEVFS_URB_TYPE_ISO;
422
urb->endpoint = devep;
424
urb->flags = USBDEVFS_URB_ISO_ASAP;
425
urb->buffer = p->data;
426
urb->buffer_length = p->len;
427
urb->actual_length = 0;
428
urb->start_frame = 0;
429
urb->error_count = 0;
431
urb->signr = SIG_ISOCOMPLETE;
536
si.interface = iface;
538
ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
540
dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
541
iface, alt, ret, errno);
546
usb_linux_update_endp_table(s);
550
static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
552
struct usbdevfs_urb *urb;
554
int ret, value, index;
557
* Process certain standard device requests.
558
* These are infrequent and are processed synchronously.
560
value = le16_to_cpu(s->ctrl.req.wValue);
561
index = le16_to_cpu(s->ctrl.req.wIndex);
563
dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
564
s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
567
if (s->ctrl.req.bRequestType == 0) {
568
switch (s->ctrl.req.bRequest) {
569
case USB_REQ_SET_ADDRESS:
570
return usb_host_set_address(s, value);
572
case USB_REQ_SET_CONFIGURATION:
573
return usb_host_set_config(s, value & 0xff);
577
if (s->ctrl.req.bRequestType == 1 &&
578
s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
579
return usb_host_set_interface(s, index, value);
581
/* The rest are asynchronous */
583
aurb = async_alloc();
588
* Setup ctrl transfer.
590
* s->ctrl is layed out such that data buffer immediately follows
591
* 'req' struct which is exactly what usbdevfs expects.
595
urb->type = USBDEVFS_URB_TYPE_CONTROL;
596
urb->endpoint = p->devep;
598
urb->buffer = &s->ctrl.req;
599
urb->buffer_length = 8 + s->ctrl.len;
435
601
urb->usercontext = s;
436
urb->number_of_packets = 1;
437
urb->iso_frame_desc[0].length = p->len;
438
urb->iso_frame_desc[0].actual_length = 0;
439
urb->iso_frame_desc[0].status = 0;
440
603
ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
442
if (!add_pending_urb(urb)) {
443
printf("usb_host_handle_isoch: add_pending_urb failed %p\n", urb);
446
printf("usb_host_handle_isoch: SUBMITURB ioctl=%d errno=%d\n",
605
dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
608
dprintf("husb: submit failed. errno %d\n", errno);
451
613
return USB_RET_NAK;
454
616
return USB_RET_STALL;
458
/* FIXME: handle urbs_ready together with sync io
459
* workaround for injecting the signaled urbs into current frame */
460
if (s->urbs_ready > 0) {
461
ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
463
ret = purb->actual_length;
620
usb_defer_packet(p, async_cancel, aurb);
470
621
return USB_RET_ASYNC;
472
ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
474
if (del_pending_urb(purb)) {
475
ret = purb->actual_length;
624
static int do_token_setup(USBDevice *dev, USBPacket *p)
626
USBHostDevice *s = (USBHostDevice *) dev;
630
return USB_RET_STALL;
632
memcpy(&s->ctrl.req, p->data, 8);
633
s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
635
s->ctrl.state = CTRL_STATE_SETUP;
637
if (s->ctrl.req.bRequestType & USB_DIR_IN) {
638
ret = usb_host_handle_control(s, p);
642
if (ret < s->ctrl.len)
644
s->ctrl.state = CTRL_STATE_DATA;
646
if (s->ctrl.len == 0)
647
s->ctrl.state = CTRL_STATE_ACK;
649
s->ctrl.state = CTRL_STATE_DATA;
655
static int do_token_in(USBDevice *dev, USBPacket *p)
657
USBHostDevice *s = (USBHostDevice *) dev;
661
return usb_host_handle_data(s, p);
663
switch(s->ctrl.state) {
665
if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
666
ret = usb_host_handle_control(s, p);
667
if (ret == USB_RET_ASYNC)
668
return USB_RET_ASYNC;
670
s->ctrl.state = CTRL_STATE_IDLE;
671
return ret > 0 ? 0 : ret;
676
case CTRL_STATE_DATA:
677
if (s->ctrl.req.bRequestType & USB_DIR_IN) {
678
int len = s->ctrl.len - s->ctrl.offset;
681
memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
682
s->ctrl.offset += len;
683
if (s->ctrl.offset >= s->ctrl.len)
684
s->ctrl.state = CTRL_STATE_ACK;
688
s->ctrl.state = CTRL_STATE_IDLE;
689
return USB_RET_STALL;
692
return USB_RET_STALL;
696
static int do_token_out(USBDevice *dev, USBPacket *p)
698
USBHostDevice *s = (USBHostDevice *) dev;
701
return usb_host_handle_data(s, p);
703
switch(s->ctrl.state) {
705
if (s->ctrl.req.bRequestType & USB_DIR_IN) {
706
s->ctrl.state = CTRL_STATE_IDLE;
478
printf("usb_host_handle_isoch: del_pending_urb failed %p\n", purb);
482
printf("usb_host_handle_isoch: REAPURBNDELAY ioctl=%d errno=%d\n",
709
/* ignore additional output */
713
case CTRL_STATE_DATA:
714
if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
715
int len = s->ctrl.len - s->ctrl.offset;
718
memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
719
s->ctrl.offset += len;
720
if (s->ctrl.offset >= s->ctrl.len)
721
s->ctrl.state = CTRL_STATE_ACK;
725
s->ctrl.state = CTRL_STATE_IDLE;
726
return USB_RET_STALL;
729
return USB_RET_STALL;
735
* Called by the HC (host controller).
737
* Returns length of the transaction or one of the USB_RET_XXX codes.
739
static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
743
s->state = USB_STATE_ATTACHED;
747
s->state = USB_STATE_NOTATTACHED;
751
s->remote_wakeup = 0;
753
s->state = USB_STATE_DEFAULT;
758
/* Rest of the PIDs must match our address */
759
if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
760
return USB_RET_NODEV;
763
case USB_TOKEN_SETUP:
764
return do_token_setup(s, p);
767
return do_token_in(s, p);
770
return do_token_out(s, p);
773
return USB_RET_STALL;
490
777
/* returns 1 on problem encountered or 0 for success */
666
953
dev->dev.speed = USB_SPEED_LOW;
668
955
dev->dev.speed = USB_SPEED_HIGH;
669
dev->dev.handle_packet = usb_generic_handle_packet;
671
dev->dev.handle_reset = usb_host_handle_reset;
672
dev->dev.handle_control = usb_host_handle_control;
673
dev->dev.handle_data = usb_host_handle_data;
957
dev->dev.handle_packet = usb_host_handle_packet;
958
dev->dev.handle_reset = usb_host_handle_reset;
674
959
dev->dev.handle_destroy = usb_host_handle_destroy;
676
if (product_name[0] == '\0')
961
if (!prod_name || prod_name[0] == '\0')
677
962
snprintf(dev->dev.devname, sizeof(dev->dev.devname),
963
"host:%d.%d", bus_num, addr);
680
965
pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
684
/* set up the signal handlers */
685
sigemptyset(&sigact.sa_mask);
686
sigact.sa_sigaction = isoch_done;
687
sigact.sa_flags = SA_SIGINFO;
688
sigact.sa_restorer = 0;
689
ret = sigaction(SIG_ISOCOMPLETE, &sigact, NULL);
691
perror("usb_host_device_open: sigaction failed");
695
if (pipe(dev->pipe_fds) < 0) {
696
perror("usb_host_device_open: pipe creation failed");
699
fcntl(dev->pipe_fds[0], F_SETFL, O_NONBLOCK | O_ASYNC);
700
fcntl(dev->pipe_fds[1], F_SETFL, O_NONBLOCK);
701
qemu_set_fd_handler(dev->pipe_fds[0], urb_completion_pipe_read, NULL, dev);
704
return (USBDevice *)dev;
968
/* USB devio uses 'write' flag to check for async completions */
969
qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
973
return (USBDevice *) dev;
983
static int usb_host_auto_add(const char *spec);
984
static int usb_host_auto_del(const char *spec);
986
USBDevice *usb_host_device_open(const char *devname)
989
char product_name[PRODUCT_NAME_SZ];
991
if (strstr(devname, "auto:")) {
992
usb_host_auto_add(devname);
996
if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1000
if (hostdev_find(bus_num, addr)) {
1001
term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
1005
return usb_host_device_open_addr(bus_num, addr, product_name);
1008
int usb_host_device_close(const char *devname)
1010
char product_name[PRODUCT_NAME_SZ];
1014
if (strstr(devname, "auto:"))
1015
return usb_host_auto_del(devname);
1017
if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1021
s = hostdev_find(bus_num, addr);
1023
usb_device_del_addr(0, s->dev.addr);
712
1030
static int get_tag_value(char *buf, int buf_size,
713
1031
const char *str, const char *tag,
714
1032
const char *stopchars)
804
1132
product_id, product_name, speed);
1141
* Read sys file-system device file
1143
* @line address of buffer to put file contents in
1144
* @line_size size of line
1145
* @device_file path to device file (printf format string)
1146
* @device_name device being opened (inserted into device_file)
1148
* @return 0 failed, 1 succeeded ('line' contains data)
1150
static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
1154
char filename[PATH_MAX];
1156
snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1158
f = fopen(filename, "r");
1160
fgets(line, line_size, f);
1164
term_printf("husb: could not open %s\n", filename);
1171
* Use /sys/bus/usb/devices/ directory to determine host's USB
1174
* This code is based on Robert Schiele's original patches posted to
1175
* the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1177
static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1181
int bus_num, addr, speed, class_id, product_id, vendor_id;
1183
char product_name[512];
1186
dir = opendir(USBSYSBUS_PATH "/devices");
1188
perror("husb: cannot open devices directory");
1192
while ((de = readdir(dir))) {
1193
if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1194
char *tmpstr = de->d_name;
1195
if (!strncmp(de->d_name, "usb", 3))
1197
bus_num = atoi(tmpstr);
1199
if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name))
1201
if (sscanf(line, "%d", &addr) != 1)
1204
if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1207
if (sscanf(line, "%x", &class_id) != 1)
1210
if (!usb_host_read_file(line, sizeof(line), "idVendor", de->d_name))
1212
if (sscanf(line, "%x", &vendor_id) != 1)
1215
if (!usb_host_read_file(line, sizeof(line), "idProduct",
1218
if (sscanf(line, "%x", &product_id) != 1)
1221
if (!usb_host_read_file(line, sizeof(line), "product",
1225
if (strlen(line) > 0)
1226
line[strlen(line) - 1] = '\0';
1227
pstrcpy(product_name, sizeof(product_name), line);
1230
if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name))
1232
if (!strcmp(line, "480\n"))
1233
speed = USB_SPEED_HIGH;
1234
else if (!strcmp(line, "1.5\n"))
1235
speed = USB_SPEED_LOW;
1237
speed = USB_SPEED_FULL;
1239
ret = func(opaque, bus_num, addr, class_id, vendor_id,
1240
product_id, product_name, speed);
1252
* Determine how to access the host's USB devices and call the
1253
* specific support function.
1255
static int usb_host_scan(void *opaque, USBScanFunc *func)
1260
const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1261
char devpath[PATH_MAX];
1263
/* only check the host once */
1265
f = fopen(USBPROCBUS_PATH "/devices", "r");
1267
/* devices found in /proc/bus/usb/ */
1268
strcpy(devpath, USBPROCBUS_PATH);
1269
usb_fs_type = USB_FS_PROC;
1271
dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1274
/* try additional methods if an access method hasn't been found yet */
1275
f = fopen(USBDEVBUS_PATH "/devices", "r");
1277
/* devices found in /dev/bus/usb/ */
1278
strcpy(devpath, USBDEVBUS_PATH);
1279
usb_fs_type = USB_FS_DEV;
1281
dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1284
dir = opendir(USBSYSBUS_PATH "/devices");
1286
/* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1287
strcpy(devpath, USBDEVBUS_PATH);
1288
usb_fs_type = USB_FS_SYS;
1290
dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1295
term_printf("husb: unable to access USB devices\n");
1299
/* the module setting (used later for opening devices) */
1300
usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1301
strcpy(usb_host_device_path, devpath);
1302
term_printf("husb: using %s file-system with %s\n", fs_type[usb_fs_type], usb_host_device_path);
1305
switch (usb_fs_type) {
1308
ret = usb_host_scan_dev(opaque, func);
1311
ret = usb_host_scan_sys(opaque, func);
1320
struct USBAutoFilter {
1321
struct USBAutoFilter *next;
1328
static QEMUTimer *usb_auto_timer;
1329
static struct USBAutoFilter *usb_auto_filter;
1331
static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1332
int class_id, int vendor_id, int product_id,
1333
const char *product_name, int speed)
1335
struct USBAutoFilter *f;
1336
struct USBDevice *dev;
1342
for (f = usb_auto_filter; f; f = f->next) {
1343
if (f->bus_num >= 0 && f->bus_num != bus_num)
1346
if (f->addr >= 0 && f->addr != addr)
1349
if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
1352
if (f->product_id >= 0 && f->product_id != product_id)
1355
/* We got a match */
1357
/* Allredy attached ? */
1358
if (hostdev_find(bus_num, addr))
1361
dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1363
dev = usb_host_device_open_addr(bus_num, addr, product_name);
1365
usb_device_add_dev(dev);
1371
static void usb_host_auto_timer(void *unused)
1373
usb_host_scan(NULL, usb_host_auto_scan);
1374
qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1378
* Autoconnect filter
1380
* auto:bus:dev[:vid:pid]
1381
* auto:bus.dev[:vid:pid]
1383
* bus - bus number (dec, * means any)
1384
* dev - device number (dec, * means any)
1385
* vid - vendor id (hex, * means any)
1386
* pid - product id (hex, * means any)
1388
* See 'lsusb' output.
1390
static int parse_filter(const char *spec, struct USBAutoFilter *f)
1392
enum { BUS, DEV, VID, PID, DONE };
1393
const char *p = spec;
1401
for (i = BUS; i < DONE; i++) {
1402
p = strpbrk(p, ":.");
1410
case BUS: f->bus_num = strtol(p, NULL, 10); break;
1411
case DEV: f->addr = strtol(p, NULL, 10); break;
1412
case VID: f->vendor_id = strtol(p, NULL, 16); break;
1413
case PID: f->product_id = strtol(p, NULL, 16); break;
1418
fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1425
static int match_filter(const struct USBAutoFilter *f1,
1426
const struct USBAutoFilter *f2)
1428
return f1->bus_num == f2->bus_num &&
1429
f1->addr == f2->addr &&
1430
f1->vendor_id == f2->vendor_id &&
1431
f1->product_id == f2->product_id;
1434
static int usb_host_auto_add(const char *spec)
1436
struct USBAutoFilter filter, *f;
1438
if (parse_filter(spec, &filter) < 0)
1441
f = qemu_mallocz(sizeof(*f));
1445
if (!usb_auto_filter) {
1447
* First entry. Init and start the monitor.
1448
* Right now we're using timer to check for new devices.
1449
* If this turns out to be too expensive we can move that into a
1452
usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
1453
if (!usb_auto_timer) {
1454
fprintf(stderr, "husb: failed to allocate auto scan timer\n");
1459
/* Check for new devices every two seconds */
1460
qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1463
dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1464
f->bus_num, f->addr, f->vendor_id, f->product_id);
1466
f->next = usb_auto_filter;
1467
usb_auto_filter = f;
1472
static int usb_host_auto_del(const char *spec)
1474
struct USBAutoFilter *pf = usb_auto_filter;
1475
struct USBAutoFilter **prev = &usb_auto_filter;
1476
struct USBAutoFilter filter;
1478
if (parse_filter(spec, &filter) < 0)
1482
if (match_filter(pf, &filter)) {
1483
dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1484
pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
1488
if (!usb_auto_filter) {
1489
/* No more filters. Stop scanning. */
1490
qemu_del_timer(usb_auto_timer);
1491
qemu_free_timer(usb_auto_timer);
811
1504
typedef struct FindDeviceState {