1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include "qemu/osdep.h"
34#include "qemu-common.h"
35#include "hw/usb.h"
36#include "hw/usb/host.h"
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51static int parse_filter(const char *spec, struct USBAutoFilter *f)
52{
53 enum { BUS, DEV, VID, PID, DONE };
54 const char *p = spec;
55 int i;
56
57 f->bus_num = 0;
58 f->addr = 0;
59 f->vendor_id = 0;
60 f->product_id = 0;
61
62 for (i = BUS; i < DONE; i++) {
63 p = strpbrk(p, ":.");
64 if (!p) {
65 break;
66 }
67 p++;
68
69 if (*p == '*') {
70 continue;
71 }
72 switch (i) {
73 case BUS:
74 f->bus_num = strtol(p, NULL, 10);
75 break;
76 case DEV:
77 f->addr = strtol(p, NULL, 10);
78 break;
79 case VID:
80 f->vendor_id = strtol(p, NULL, 16);
81 break;
82 case PID:
83 f->product_id = strtol(p, NULL, 16);
84 break;
85 }
86 }
87
88 if (i < DEV) {
89 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
90 return -1;
91 }
92
93 return 0;
94}
95
96USBDevice *usb_host_device_open(USBBus *bus, const char *devname)
97{
98 struct USBAutoFilter filter;
99 USBDevice *dev;
100 char *p;
101
102 dev = usb_create(bus, "usb-host");
103
104 if (strstr(devname, "auto:")) {
105 if (parse_filter(devname, &filter) < 0) {
106 goto fail;
107 }
108 } else {
109 p = strchr(devname, '.');
110 if (p) {
111 filter.bus_num = strtoul(devname, NULL, 0);
112 filter.addr = strtoul(p + 1, NULL, 0);
113 filter.vendor_id = 0;
114 filter.product_id = 0;
115 } else {
116 p = strchr(devname, ':');
117 if (p) {
118 filter.bus_num = 0;
119 filter.addr = 0;
120 filter.vendor_id = strtoul(devname, NULL, 16);
121 filter.product_id = strtoul(p + 1, NULL, 16);
122 } else {
123 goto fail;
124 }
125 }
126 }
127
128 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
129 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
130 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
131 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
132 return dev;
133
134fail:
135 object_unparent(OBJECT(dev));
136 return NULL;
137}
138
139static void usb_host_register_types(void)
140{
141 usb_legacy_register("usb-host", "host", usb_host_device_open);
142}
143
144type_init(usb_host_register_types)
145