root / usb-linux.c @ 50d3eeae
History | View | Annotate | Download (14.8 kB)
1 | bb36d470 | bellard | /*
|
---|---|---|---|
2 | bb36d470 | bellard | * Linux host USB redirector
|
3 | bb36d470 | bellard | *
|
4 | bb36d470 | bellard | * Copyright (c) 2005 Fabrice Bellard
|
5 | bb36d470 | bellard | *
|
6 | bb36d470 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | bb36d470 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | bb36d470 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | bb36d470 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | bb36d470 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | bb36d470 | bellard | * furnished to do so, subject to the following conditions:
|
12 | bb36d470 | bellard | *
|
13 | bb36d470 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | bb36d470 | bellard | * all copies or substantial portions of the Software.
|
15 | bb36d470 | bellard | *
|
16 | bb36d470 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | bb36d470 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | bb36d470 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | bb36d470 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | bb36d470 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | bb36d470 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | bb36d470 | bellard | * THE SOFTWARE.
|
23 | bb36d470 | bellard | */
|
24 | bb36d470 | bellard | #include "vl.h" |
25 | bb36d470 | bellard | |
26 | bb36d470 | bellard | #if defined(__linux__)
|
27 | bb36d470 | bellard | #include <dirent.h> |
28 | bb36d470 | bellard | #include <sys/ioctl.h> |
29 | bb36d470 | bellard | #include <linux/usbdevice_fs.h> |
30 | bb36d470 | bellard | #include <linux/version.h> |
31 | bb36d470 | bellard | |
32 | bb36d470 | bellard | /* We redefine it to avoid version problems */
|
33 | bb36d470 | bellard | struct usb_ctrltransfer {
|
34 | bb36d470 | bellard | uint8_t bRequestType; |
35 | bb36d470 | bellard | uint8_t bRequest; |
36 | bb36d470 | bellard | uint16_t wValue; |
37 | bb36d470 | bellard | uint16_t wIndex; |
38 | bb36d470 | bellard | uint16_t wLength; |
39 | bb36d470 | bellard | uint32_t timeout; |
40 | bb36d470 | bellard | void *data;
|
41 | bb36d470 | bellard | }; |
42 | bb36d470 | bellard | |
43 | a594cfbf | bellard | typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id, |
44 | a594cfbf | bellard | int vendor_id, int product_id, |
45 | a594cfbf | bellard | const char *product_name, int speed); |
46 | a594cfbf | bellard | static int usb_host_find_device(int *pbus_num, int *paddr, |
47 | 1f6e24e7 | bellard | char *product_name, int product_name_size, |
48 | a594cfbf | bellard | const char *devname); |
49 | bb36d470 | bellard | |
50 | a594cfbf | bellard | //#define DEBUG
|
51 | bb36d470 | bellard | |
52 | bb36d470 | bellard | #define USBDEVFS_PATH "/proc/bus/usb" |
53 | 1f6e24e7 | bellard | #define PRODUCT_NAME_SZ 32 |
54 | bb36d470 | bellard | |
55 | bb36d470 | bellard | typedef struct USBHostDevice { |
56 | bb36d470 | bellard | USBDevice dev; |
57 | bb36d470 | bellard | int fd;
|
58 | bb36d470 | bellard | } USBHostDevice; |
59 | bb36d470 | bellard | |
60 | 059809e4 | bellard | static void usb_host_handle_reset(USBDevice *dev) |
61 | bb36d470 | bellard | { |
62 | bb36d470 | bellard | #if 0
|
63 | bb36d470 | bellard | USBHostDevice *s = (USBHostDevice *)dev;
|
64 | bb36d470 | bellard | /* USBDEVFS_RESET, but not the first time as it has already be
|
65 | bb36d470 | bellard | done by the host OS */
|
66 | bb36d470 | bellard | ioctl(s->fd, USBDEVFS_RESET);
|
67 | bb36d470 | bellard | #endif
|
68 | bb36d470 | bellard | } |
69 | bb36d470 | bellard | |
70 | 059809e4 | bellard | static void usb_host_handle_destroy(USBDevice *dev) |
71 | 059809e4 | bellard | { |
72 | 059809e4 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
73 | 059809e4 | bellard | |
74 | 059809e4 | bellard | if (s->fd >= 0) |
75 | 059809e4 | bellard | close(s->fd); |
76 | 059809e4 | bellard | qemu_free(s); |
77 | 059809e4 | bellard | } |
78 | 059809e4 | bellard | |
79 | bb36d470 | bellard | static int usb_host_handle_control(USBDevice *dev, |
80 | bb36d470 | bellard | int request,
|
81 | bb36d470 | bellard | int value,
|
82 | bb36d470 | bellard | int index,
|
83 | bb36d470 | bellard | int length,
|
84 | bb36d470 | bellard | uint8_t *data) |
85 | bb36d470 | bellard | { |
86 | bb36d470 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
87 | bb36d470 | bellard | struct usb_ctrltransfer ct;
|
88 | bb36d470 | bellard | int ret;
|
89 | bb36d470 | bellard | |
90 | bb36d470 | bellard | if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
|
91 | bb36d470 | bellard | /* specific SET_ADDRESS support */
|
92 | bb36d470 | bellard | dev->addr = value; |
93 | bb36d470 | bellard | return 0; |
94 | bb36d470 | bellard | } else {
|
95 | bb36d470 | bellard | ct.bRequestType = request >> 8;
|
96 | bb36d470 | bellard | ct.bRequest = request; |
97 | bb36d470 | bellard | ct.wValue = value; |
98 | bb36d470 | bellard | ct.wIndex = index; |
99 | bb36d470 | bellard | ct.wLength = length; |
100 | bb36d470 | bellard | ct.timeout = 50;
|
101 | bb36d470 | bellard | ct.data = data; |
102 | bb36d470 | bellard | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
103 | bb36d470 | bellard | if (ret < 0) { |
104 | bb36d470 | bellard | switch(errno) {
|
105 | bb36d470 | bellard | case ETIMEDOUT:
|
106 | bb36d470 | bellard | return USB_RET_NAK;
|
107 | bb36d470 | bellard | default:
|
108 | bb36d470 | bellard | return USB_RET_STALL;
|
109 | bb36d470 | bellard | } |
110 | bb36d470 | bellard | } else {
|
111 | bb36d470 | bellard | return ret;
|
112 | bb36d470 | bellard | } |
113 | bb36d470 | bellard | } |
114 | bb36d470 | bellard | } |
115 | bb36d470 | bellard | |
116 | 4d611c9a | pbrook | static int usb_host_handle_data(USBDevice *dev, USBPacket *p) |
117 | bb36d470 | bellard | { |
118 | bb36d470 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
119 | bb36d470 | bellard | struct usbdevfs_bulktransfer bt;
|
120 | bb36d470 | bellard | int ret;
|
121 | 4d611c9a | pbrook | uint8_t devep = p->devep; |
122 | bb36d470 | bellard | |
123 | bb36d470 | bellard | /* XXX: optimize and handle all data types by looking at the
|
124 | bb36d470 | bellard | config descriptor */
|
125 | 4d611c9a | pbrook | if (p->pid == USB_TOKEN_IN)
|
126 | bb36d470 | bellard | devep |= 0x80;
|
127 | bb36d470 | bellard | bt.ep = devep; |
128 | 4d611c9a | pbrook | bt.len = p->len; |
129 | bb36d470 | bellard | bt.timeout = 50;
|
130 | 4d611c9a | pbrook | bt.data = p->data; |
131 | bb36d470 | bellard | ret = ioctl(s->fd, USBDEVFS_BULK, &bt); |
132 | bb36d470 | bellard | if (ret < 0) { |
133 | bb36d470 | bellard | switch(errno) {
|
134 | bb36d470 | bellard | case ETIMEDOUT:
|
135 | bb36d470 | bellard | return USB_RET_NAK;
|
136 | bb36d470 | bellard | case EPIPE:
|
137 | bb36d470 | bellard | default:
|
138 | bb36d470 | bellard | #ifdef DEBUG
|
139 | bb36d470 | bellard | printf("handle_data: errno=%d\n", errno);
|
140 | bb36d470 | bellard | #endif
|
141 | bb36d470 | bellard | return USB_RET_STALL;
|
142 | bb36d470 | bellard | } |
143 | bb36d470 | bellard | } else {
|
144 | bb36d470 | bellard | return ret;
|
145 | bb36d470 | bellard | } |
146 | bb36d470 | bellard | } |
147 | bb36d470 | bellard | |
148 | bb36d470 | bellard | /* XXX: exclude high speed devices or implement EHCI */
|
149 | a594cfbf | bellard | USBDevice *usb_host_device_open(const char *devname) |
150 | bb36d470 | bellard | { |
151 | bb36d470 | bellard | int fd, interface, ret, i;
|
152 | bb36d470 | bellard | USBHostDevice *dev; |
153 | bb36d470 | bellard | struct usbdevfs_connectinfo ci;
|
154 | bb36d470 | bellard | uint8_t descr[1024];
|
155 | a594cfbf | bellard | char buf[1024]; |
156 | bb36d470 | bellard | int descr_len, dev_descr_len, config_descr_len, nb_interfaces;
|
157 | a594cfbf | bellard | int bus_num, addr;
|
158 | 1f6e24e7 | bellard | char product_name[PRODUCT_NAME_SZ];
|
159 | bb36d470 | bellard | |
160 | 1f6e24e7 | bellard | if (usb_host_find_device(&bus_num, &addr,
|
161 | 1f6e24e7 | bellard | product_name, sizeof(product_name),
|
162 | 1f6e24e7 | bellard | devname) < 0)
|
163 | a594cfbf | bellard | return NULL; |
164 | a594cfbf | bellard | |
165 | a594cfbf | bellard | snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d", |
166 | a594cfbf | bellard | bus_num, addr); |
167 | a594cfbf | bellard | fd = open(buf, O_RDWR); |
168 | bb36d470 | bellard | if (fd < 0) { |
169 | a594cfbf | bellard | perror(buf); |
170 | a594cfbf | bellard | return NULL; |
171 | bb36d470 | bellard | } |
172 | bb36d470 | bellard | |
173 | bb36d470 | bellard | /* read the config description */
|
174 | bb36d470 | bellard | descr_len = read(fd, descr, sizeof(descr));
|
175 | bb36d470 | bellard | if (descr_len <= 0) { |
176 | bb36d470 | bellard | perror("read descr");
|
177 | bb36d470 | bellard | goto fail;
|
178 | bb36d470 | bellard | } |
179 | bb36d470 | bellard | |
180 | bb36d470 | bellard | i = 0;
|
181 | bb36d470 | bellard | dev_descr_len = descr[0];
|
182 | bb36d470 | bellard | if (dev_descr_len > descr_len)
|
183 | bb36d470 | bellard | goto fail;
|
184 | bb36d470 | bellard | i += dev_descr_len; |
185 | bb36d470 | bellard | config_descr_len = descr[i]; |
186 | bb36d470 | bellard | if (i + config_descr_len > descr_len)
|
187 | bb36d470 | bellard | goto fail;
|
188 | bb36d470 | bellard | nb_interfaces = descr[i + 4];
|
189 | bb36d470 | bellard | if (nb_interfaces != 1) { |
190 | bb36d470 | bellard | /* NOTE: currently we grab only one interface */
|
191 | a594cfbf | bellard | fprintf(stderr, "usb_host: only one interface supported\n");
|
192 | a594cfbf | bellard | goto fail;
|
193 | a594cfbf | bellard | } |
194 | a594cfbf | bellard | |
195 | a594cfbf | bellard | #ifdef USBDEVFS_DISCONNECT
|
196 | a594cfbf | bellard | /* earlier Linux 2.4 do not support that */
|
197 | 868bfe2b | bellard | { |
198 | 868bfe2b | bellard | struct usbdevfs_ioctl ctrl;
|
199 | 868bfe2b | bellard | ctrl.ioctl_code = USBDEVFS_DISCONNECT; |
200 | 868bfe2b | bellard | ctrl.ifno = 0;
|
201 | 868bfe2b | bellard | ret = ioctl(fd, USBDEVFS_IOCTL, &ctrl); |
202 | 868bfe2b | bellard | if (ret < 0 && errno != ENODATA) { |
203 | 868bfe2b | bellard | perror("USBDEVFS_DISCONNECT");
|
204 | 868bfe2b | bellard | goto fail;
|
205 | 868bfe2b | bellard | } |
206 | bb36d470 | bellard | } |
207 | a594cfbf | bellard | #endif
|
208 | a594cfbf | bellard | |
209 | bb36d470 | bellard | /* XXX: only grab if all interfaces are free */
|
210 | bb36d470 | bellard | interface = 0;
|
211 | bb36d470 | bellard | ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interface); |
212 | bb36d470 | bellard | if (ret < 0) { |
213 | bb36d470 | bellard | if (errno == EBUSY) {
|
214 | a594cfbf | bellard | fprintf(stderr, "usb_host: device already grabbed\n");
|
215 | bb36d470 | bellard | } else {
|
216 | bb36d470 | bellard | perror("USBDEVFS_CLAIMINTERFACE");
|
217 | bb36d470 | bellard | } |
218 | bb36d470 | bellard | fail:
|
219 | bb36d470 | bellard | close(fd); |
220 | a594cfbf | bellard | return NULL; |
221 | bb36d470 | bellard | } |
222 | bb36d470 | bellard | |
223 | bb36d470 | bellard | ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci); |
224 | bb36d470 | bellard | if (ret < 0) { |
225 | bb36d470 | bellard | perror("USBDEVFS_CONNECTINFO");
|
226 | bb36d470 | bellard | goto fail;
|
227 | bb36d470 | bellard | } |
228 | bb36d470 | bellard | |
229 | bb36d470 | bellard | #ifdef DEBUG
|
230 | a594cfbf | bellard | printf("host USB device %d.%d grabbed\n", bus_num, addr);
|
231 | bb36d470 | bellard | #endif
|
232 | bb36d470 | bellard | |
233 | bb36d470 | bellard | dev = qemu_mallocz(sizeof(USBHostDevice));
|
234 | bb36d470 | bellard | if (!dev)
|
235 | bb36d470 | bellard | goto fail;
|
236 | bb36d470 | bellard | dev->fd = fd; |
237 | bb36d470 | bellard | if (ci.slow)
|
238 | bb36d470 | bellard | dev->dev.speed = USB_SPEED_LOW; |
239 | bb36d470 | bellard | else
|
240 | bb36d470 | bellard | dev->dev.speed = USB_SPEED_HIGH; |
241 | a594cfbf | bellard | dev->dev.handle_packet = usb_generic_handle_packet; |
242 | bb36d470 | bellard | |
243 | bb36d470 | bellard | dev->dev.handle_reset = usb_host_handle_reset; |
244 | bb36d470 | bellard | dev->dev.handle_control = usb_host_handle_control; |
245 | bb36d470 | bellard | dev->dev.handle_data = usb_host_handle_data; |
246 | 059809e4 | bellard | dev->dev.handle_destroy = usb_host_handle_destroy; |
247 | 1f6e24e7 | bellard | |
248 | 1f6e24e7 | bellard | if (product_name[0] == '\0') |
249 | 1f6e24e7 | bellard | snprintf(dev->dev.devname, sizeof(dev->dev.devname),
|
250 | 1f6e24e7 | bellard | "host:%s", devname);
|
251 | 1f6e24e7 | bellard | else
|
252 | 1f6e24e7 | bellard | pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
|
253 | 1f6e24e7 | bellard | product_name); |
254 | 1f6e24e7 | bellard | |
255 | a594cfbf | bellard | return (USBDevice *)dev;
|
256 | a594cfbf | bellard | } |
257 | bb36d470 | bellard | |
258 | a594cfbf | bellard | static int get_tag_value(char *buf, int buf_size, |
259 | a594cfbf | bellard | const char *str, const char *tag, |
260 | a594cfbf | bellard | const char *stopchars) |
261 | a594cfbf | bellard | { |
262 | a594cfbf | bellard | const char *p; |
263 | a594cfbf | bellard | char *q;
|
264 | a594cfbf | bellard | p = strstr(str, tag); |
265 | a594cfbf | bellard | if (!p)
|
266 | a594cfbf | bellard | return -1; |
267 | a594cfbf | bellard | p += strlen(tag); |
268 | a594cfbf | bellard | while (isspace(*p))
|
269 | a594cfbf | bellard | p++; |
270 | a594cfbf | bellard | q = buf; |
271 | a594cfbf | bellard | while (*p != '\0' && !strchr(stopchars, *p)) { |
272 | a594cfbf | bellard | if ((q - buf) < (buf_size - 1)) |
273 | a594cfbf | bellard | *q++ = *p; |
274 | a594cfbf | bellard | p++; |
275 | a594cfbf | bellard | } |
276 | a594cfbf | bellard | *q = '\0';
|
277 | a594cfbf | bellard | return q - buf;
|
278 | bb36d470 | bellard | } |
279 | bb36d470 | bellard | |
280 | a594cfbf | bellard | static int usb_host_scan(void *opaque, USBScanFunc *func) |
281 | bb36d470 | bellard | { |
282 | a594cfbf | bellard | FILE *f; |
283 | a594cfbf | bellard | char line[1024]; |
284 | bb36d470 | bellard | char buf[1024]; |
285 | a594cfbf | bellard | int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
|
286 | a594cfbf | bellard | int ret;
|
287 | a594cfbf | bellard | char product_name[512]; |
288 | a594cfbf | bellard | |
289 | a594cfbf | bellard | f = fopen(USBDEVFS_PATH "/devices", "r"); |
290 | a594cfbf | bellard | if (!f) {
|
291 | a594cfbf | bellard | term_printf("Could not open %s\n", USBDEVFS_PATH "/devices"); |
292 | a594cfbf | bellard | return 0; |
293 | a594cfbf | bellard | } |
294 | a594cfbf | bellard | device_count = 0;
|
295 | a594cfbf | bellard | bus_num = addr = speed = class_id = product_id = vendor_id = 0;
|
296 | a594cfbf | bellard | ret = 0;
|
297 | bb36d470 | bellard | for(;;) {
|
298 | a594cfbf | bellard | if (fgets(line, sizeof(line), f) == NULL) |
299 | bb36d470 | bellard | break;
|
300 | a594cfbf | bellard | if (strlen(line) > 0) |
301 | a594cfbf | bellard | line[strlen(line) - 1] = '\0'; |
302 | a594cfbf | bellard | if (line[0] == 'T' && line[1] == ':') { |
303 | 38ca0f6d | pbrook | if (device_count && (vendor_id || product_id)) {
|
304 | 38ca0f6d | pbrook | /* New device. Add the previously discovered device. */
|
305 | a594cfbf | bellard | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
306 | a594cfbf | bellard | product_id, product_name, speed); |
307 | a594cfbf | bellard | if (ret)
|
308 | a594cfbf | bellard | goto the_end;
|
309 | a594cfbf | bellard | } |
310 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) |
311 | a594cfbf | bellard | goto fail;
|
312 | a594cfbf | bellard | bus_num = atoi(buf); |
313 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) |
314 | a594cfbf | bellard | goto fail;
|
315 | a594cfbf | bellard | addr = atoi(buf); |
316 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) |
317 | a594cfbf | bellard | goto fail;
|
318 | a594cfbf | bellard | if (!strcmp(buf, "480")) |
319 | a594cfbf | bellard | speed = USB_SPEED_HIGH; |
320 | a594cfbf | bellard | else if (!strcmp(buf, "1.5")) |
321 | a594cfbf | bellard | speed = USB_SPEED_LOW; |
322 | a594cfbf | bellard | else
|
323 | a594cfbf | bellard | speed = USB_SPEED_FULL; |
324 | a594cfbf | bellard | product_name[0] = '\0'; |
325 | a594cfbf | bellard | class_id = 0xff;
|
326 | a594cfbf | bellard | device_count++; |
327 | a594cfbf | bellard | product_id = 0;
|
328 | a594cfbf | bellard | vendor_id = 0;
|
329 | a594cfbf | bellard | } else if (line[0] == 'P' && line[1] == ':') { |
330 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) |
331 | a594cfbf | bellard | goto fail;
|
332 | a594cfbf | bellard | vendor_id = strtoul(buf, NULL, 16); |
333 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) |
334 | a594cfbf | bellard | goto fail;
|
335 | a594cfbf | bellard | product_id = strtoul(buf, NULL, 16); |
336 | a594cfbf | bellard | } else if (line[0] == 'S' && line[1] == ':') { |
337 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) |
338 | a594cfbf | bellard | goto fail;
|
339 | a594cfbf | bellard | pstrcpy(product_name, sizeof(product_name), buf);
|
340 | a594cfbf | bellard | } else if (line[0] == 'D' && line[1] == ':') { |
341 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) |
342 | a594cfbf | bellard | goto fail;
|
343 | a594cfbf | bellard | class_id = strtoul(buf, NULL, 16); |
344 | bb36d470 | bellard | } |
345 | a594cfbf | bellard | fail: ;
|
346 | a594cfbf | bellard | } |
347 | 38ca0f6d | pbrook | if (device_count && (vendor_id || product_id)) {
|
348 | 38ca0f6d | pbrook | /* Add the last device. */
|
349 | a594cfbf | bellard | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
350 | a594cfbf | bellard | product_id, product_name, speed); |
351 | bb36d470 | bellard | } |
352 | a594cfbf | bellard | the_end:
|
353 | a594cfbf | bellard | fclose(f); |
354 | a594cfbf | bellard | return ret;
|
355 | bb36d470 | bellard | } |
356 | bb36d470 | bellard | |
357 | a594cfbf | bellard | typedef struct FindDeviceState { |
358 | a594cfbf | bellard | int vendor_id;
|
359 | a594cfbf | bellard | int product_id;
|
360 | a594cfbf | bellard | int bus_num;
|
361 | a594cfbf | bellard | int addr;
|
362 | 1f6e24e7 | bellard | char product_name[PRODUCT_NAME_SZ];
|
363 | a594cfbf | bellard | } FindDeviceState; |
364 | a594cfbf | bellard | |
365 | a594cfbf | bellard | static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, |
366 | a594cfbf | bellard | int class_id,
|
367 | a594cfbf | bellard | int vendor_id, int product_id, |
368 | a594cfbf | bellard | const char *product_name, int speed) |
369 | bb36d470 | bellard | { |
370 | a594cfbf | bellard | FindDeviceState *s = opaque; |
371 | 1f6e24e7 | bellard | if ((vendor_id == s->vendor_id &&
|
372 | 1f6e24e7 | bellard | product_id == s->product_id) || |
373 | 1f6e24e7 | bellard | (bus_num == s->bus_num && |
374 | 1f6e24e7 | bellard | addr == s->addr)) { |
375 | 1f6e24e7 | bellard | pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name); |
376 | a594cfbf | bellard | s->bus_num = bus_num; |
377 | a594cfbf | bellard | s->addr = addr; |
378 | a594cfbf | bellard | return 1; |
379 | a594cfbf | bellard | } else {
|
380 | a594cfbf | bellard | return 0; |
381 | a594cfbf | bellard | } |
382 | a594cfbf | bellard | } |
383 | bb36d470 | bellard | |
384 | a594cfbf | bellard | /* the syntax is :
|
385 | a594cfbf | bellard | 'bus.addr' (decimal numbers) or
|
386 | a594cfbf | bellard | 'vendor_id:product_id' (hexa numbers) */
|
387 | a594cfbf | bellard | static int usb_host_find_device(int *pbus_num, int *paddr, |
388 | 1f6e24e7 | bellard | char *product_name, int product_name_size, |
389 | a594cfbf | bellard | const char *devname) |
390 | a594cfbf | bellard | { |
391 | a594cfbf | bellard | const char *p; |
392 | a594cfbf | bellard | int ret;
|
393 | a594cfbf | bellard | FindDeviceState fs; |
394 | a594cfbf | bellard | |
395 | a594cfbf | bellard | p = strchr(devname, '.');
|
396 | a594cfbf | bellard | if (p) {
|
397 | a594cfbf | bellard | *pbus_num = strtoul(devname, NULL, 0); |
398 | a594cfbf | bellard | *paddr = strtoul(p + 1, NULL, 0); |
399 | 1f6e24e7 | bellard | fs.bus_num = *pbus_num; |
400 | 1f6e24e7 | bellard | fs.addr = *paddr; |
401 | 1f6e24e7 | bellard | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
402 | 1f6e24e7 | bellard | if (ret)
|
403 | 1f6e24e7 | bellard | pstrcpy(product_name, product_name_size, fs.product_name); |
404 | a594cfbf | bellard | return 0; |
405 | a594cfbf | bellard | } |
406 | a594cfbf | bellard | p = strchr(devname, ':');
|
407 | a594cfbf | bellard | if (p) {
|
408 | a594cfbf | bellard | fs.vendor_id = strtoul(devname, NULL, 16); |
409 | a594cfbf | bellard | fs.product_id = strtoul(p + 1, NULL, 16); |
410 | a594cfbf | bellard | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
411 | a594cfbf | bellard | if (ret) {
|
412 | a594cfbf | bellard | *pbus_num = fs.bus_num; |
413 | a594cfbf | bellard | *paddr = fs.addr; |
414 | 1f6e24e7 | bellard | pstrcpy(product_name, product_name_size, fs.product_name); |
415 | a594cfbf | bellard | return 0; |
416 | bb36d470 | bellard | } |
417 | bb36d470 | bellard | } |
418 | a594cfbf | bellard | return -1; |
419 | bb36d470 | bellard | } |
420 | bb36d470 | bellard | |
421 | a594cfbf | bellard | /**********************/
|
422 | a594cfbf | bellard | /* USB host device info */
|
423 | a594cfbf | bellard | |
424 | a594cfbf | bellard | struct usb_class_info {
|
425 | a594cfbf | bellard | int class;
|
426 | a594cfbf | bellard | const char *class_name; |
427 | a594cfbf | bellard | }; |
428 | a594cfbf | bellard | |
429 | a594cfbf | bellard | static const struct usb_class_info usb_class_info[] = { |
430 | a594cfbf | bellard | { USB_CLASS_AUDIO, "Audio"},
|
431 | a594cfbf | bellard | { USB_CLASS_COMM, "Communication"},
|
432 | a594cfbf | bellard | { USB_CLASS_HID, "HID"},
|
433 | a594cfbf | bellard | { USB_CLASS_HUB, "Hub" },
|
434 | a594cfbf | bellard | { USB_CLASS_PHYSICAL, "Physical" },
|
435 | a594cfbf | bellard | { USB_CLASS_PRINTER, "Printer" },
|
436 | a594cfbf | bellard | { USB_CLASS_MASS_STORAGE, "Storage" },
|
437 | a594cfbf | bellard | { USB_CLASS_CDC_DATA, "Data" },
|
438 | a594cfbf | bellard | { USB_CLASS_APP_SPEC, "Application Specific" },
|
439 | a594cfbf | bellard | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
|
440 | a594cfbf | bellard | { USB_CLASS_STILL_IMAGE, "Still Image" },
|
441 | a594cfbf | bellard | { USB_CLASS_CSCID, "Smart Card" },
|
442 | a594cfbf | bellard | { USB_CLASS_CONTENT_SEC, "Content Security" },
|
443 | a594cfbf | bellard | { -1, NULL } |
444 | a594cfbf | bellard | }; |
445 | a594cfbf | bellard | |
446 | a594cfbf | bellard | static const char *usb_class_str(uint8_t class) |
447 | bb36d470 | bellard | { |
448 | a594cfbf | bellard | const struct usb_class_info *p; |
449 | a594cfbf | bellard | for(p = usb_class_info; p->class != -1; p++) { |
450 | a594cfbf | bellard | if (p->class == class)
|
451 | a594cfbf | bellard | break;
|
452 | bb36d470 | bellard | } |
453 | a594cfbf | bellard | return p->class_name;
|
454 | a594cfbf | bellard | } |
455 | a594cfbf | bellard | |
456 | a594cfbf | bellard | void usb_info_device(int bus_num, int addr, int class_id, |
457 | a594cfbf | bellard | int vendor_id, int product_id, |
458 | a594cfbf | bellard | const char *product_name, |
459 | a594cfbf | bellard | int speed)
|
460 | a594cfbf | bellard | { |
461 | a594cfbf | bellard | const char *class_str, *speed_str; |
462 | a594cfbf | bellard | |
463 | a594cfbf | bellard | switch(speed) {
|
464 | a594cfbf | bellard | case USB_SPEED_LOW:
|
465 | a594cfbf | bellard | speed_str = "1.5";
|
466 | a594cfbf | bellard | break;
|
467 | a594cfbf | bellard | case USB_SPEED_FULL:
|
468 | a594cfbf | bellard | speed_str = "12";
|
469 | a594cfbf | bellard | break;
|
470 | a594cfbf | bellard | case USB_SPEED_HIGH:
|
471 | a594cfbf | bellard | speed_str = "480";
|
472 | a594cfbf | bellard | break;
|
473 | a594cfbf | bellard | default:
|
474 | a594cfbf | bellard | speed_str = "?";
|
475 | a594cfbf | bellard | break;
|
476 | a594cfbf | bellard | } |
477 | a594cfbf | bellard | |
478 | a594cfbf | bellard | term_printf(" Device %d.%d, speed %s Mb/s\n",
|
479 | a594cfbf | bellard | bus_num, addr, speed_str); |
480 | a594cfbf | bellard | class_str = usb_class_str(class_id); |
481 | a594cfbf | bellard | if (class_str)
|
482 | a594cfbf | bellard | term_printf(" %s:", class_str);
|
483 | a594cfbf | bellard | else
|
484 | a594cfbf | bellard | term_printf(" Class %02x:", class_id);
|
485 | a594cfbf | bellard | term_printf(" USB device %04x:%04x", vendor_id, product_id);
|
486 | a594cfbf | bellard | if (product_name[0] != '\0') |
487 | a594cfbf | bellard | term_printf(", %s", product_name);
|
488 | a594cfbf | bellard | term_printf("\n");
|
489 | a594cfbf | bellard | } |
490 | a594cfbf | bellard | |
491 | a594cfbf | bellard | static int usb_host_info_device(void *opaque, int bus_num, int addr, |
492 | a594cfbf | bellard | int class_id,
|
493 | a594cfbf | bellard | int vendor_id, int product_id, |
494 | a594cfbf | bellard | const char *product_name, |
495 | a594cfbf | bellard | int speed)
|
496 | a594cfbf | bellard | { |
497 | a594cfbf | bellard | usb_info_device(bus_num, addr, class_id, vendor_id, product_id, |
498 | a594cfbf | bellard | product_name, speed); |
499 | a594cfbf | bellard | return 0; |
500 | a594cfbf | bellard | } |
501 | a594cfbf | bellard | |
502 | a594cfbf | bellard | void usb_host_info(void) |
503 | a594cfbf | bellard | { |
504 | a594cfbf | bellard | usb_host_scan(NULL, usb_host_info_device);
|
505 | bb36d470 | bellard | } |
506 | bb36d470 | bellard | |
507 | bb36d470 | bellard | #else
|
508 | bb36d470 | bellard | |
509 | a594cfbf | bellard | void usb_host_info(void) |
510 | a594cfbf | bellard | { |
511 | a594cfbf | bellard | term_printf("USB host devices not supported\n");
|
512 | a594cfbf | bellard | } |
513 | a594cfbf | bellard | |
514 | bb36d470 | bellard | /* XXX: modify configure to compile the right host driver */
|
515 | a594cfbf | bellard | USBDevice *usb_host_device_open(const char *devname) |
516 | bb36d470 | bellard | { |
517 | bb36d470 | bellard | return NULL; |
518 | bb36d470 | bellard | } |
519 | bb36d470 | bellard | |
520 | bb36d470 | bellard | #endif |