root / usb-linux.c @ 1247c5f7
History | View | Annotate | Download (13.5 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 | a594cfbf | bellard | const char *devname); |
48 | bb36d470 | bellard | |
49 | a594cfbf | bellard | //#define DEBUG
|
50 | bb36d470 | bellard | |
51 | bb36d470 | bellard | #define USBDEVFS_PATH "/proc/bus/usb" |
52 | bb36d470 | bellard | |
53 | bb36d470 | bellard | typedef struct USBHostDevice { |
54 | bb36d470 | bellard | USBDevice dev; |
55 | bb36d470 | bellard | int fd;
|
56 | bb36d470 | bellard | } USBHostDevice; |
57 | bb36d470 | bellard | |
58 | bb36d470 | bellard | static void usb_host_handle_reset(USBDevice *dev) |
59 | bb36d470 | bellard | { |
60 | bb36d470 | bellard | #if 0
|
61 | bb36d470 | bellard | USBHostDevice *s = (USBHostDevice *)dev;
|
62 | bb36d470 | bellard | /* USBDEVFS_RESET, but not the first time as it has already be
|
63 | bb36d470 | bellard | done by the host OS */
|
64 | bb36d470 | bellard | ioctl(s->fd, USBDEVFS_RESET);
|
65 | bb36d470 | bellard | #endif
|
66 | bb36d470 | bellard | } |
67 | bb36d470 | bellard | |
68 | bb36d470 | bellard | static int usb_host_handle_control(USBDevice *dev, |
69 | bb36d470 | bellard | int request,
|
70 | bb36d470 | bellard | int value,
|
71 | bb36d470 | bellard | int index,
|
72 | bb36d470 | bellard | int length,
|
73 | bb36d470 | bellard | uint8_t *data) |
74 | bb36d470 | bellard | { |
75 | bb36d470 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
76 | bb36d470 | bellard | struct usb_ctrltransfer ct;
|
77 | bb36d470 | bellard | int ret;
|
78 | bb36d470 | bellard | |
79 | bb36d470 | bellard | if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
|
80 | bb36d470 | bellard | /* specific SET_ADDRESS support */
|
81 | bb36d470 | bellard | dev->addr = value; |
82 | bb36d470 | bellard | return 0; |
83 | bb36d470 | bellard | } else {
|
84 | bb36d470 | bellard | ct.bRequestType = request >> 8;
|
85 | bb36d470 | bellard | ct.bRequest = request; |
86 | bb36d470 | bellard | ct.wValue = value; |
87 | bb36d470 | bellard | ct.wIndex = index; |
88 | bb36d470 | bellard | ct.wLength = length; |
89 | bb36d470 | bellard | ct.timeout = 50;
|
90 | bb36d470 | bellard | ct.data = data; |
91 | bb36d470 | bellard | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
92 | bb36d470 | bellard | if (ret < 0) { |
93 | bb36d470 | bellard | switch(errno) {
|
94 | bb36d470 | bellard | case ETIMEDOUT:
|
95 | bb36d470 | bellard | return USB_RET_NAK;
|
96 | bb36d470 | bellard | default:
|
97 | bb36d470 | bellard | return USB_RET_STALL;
|
98 | bb36d470 | bellard | } |
99 | bb36d470 | bellard | } else {
|
100 | bb36d470 | bellard | return ret;
|
101 | bb36d470 | bellard | } |
102 | bb36d470 | bellard | } |
103 | bb36d470 | bellard | } |
104 | bb36d470 | bellard | |
105 | bb36d470 | bellard | static int usb_host_handle_data(USBDevice *dev, int pid, |
106 | bb36d470 | bellard | uint8_t devep, |
107 | bb36d470 | bellard | uint8_t *data, int len)
|
108 | bb36d470 | bellard | { |
109 | bb36d470 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
110 | bb36d470 | bellard | struct usbdevfs_bulktransfer bt;
|
111 | bb36d470 | bellard | int ret;
|
112 | bb36d470 | bellard | |
113 | bb36d470 | bellard | /* XXX: optimize and handle all data types by looking at the
|
114 | bb36d470 | bellard | config descriptor */
|
115 | bb36d470 | bellard | if (pid == USB_TOKEN_IN)
|
116 | bb36d470 | bellard | devep |= 0x80;
|
117 | bb36d470 | bellard | bt.ep = devep; |
118 | bb36d470 | bellard | bt.len = len; |
119 | bb36d470 | bellard | bt.timeout = 50;
|
120 | bb36d470 | bellard | bt.data = data; |
121 | bb36d470 | bellard | ret = ioctl(s->fd, USBDEVFS_BULK, &bt); |
122 | bb36d470 | bellard | if (ret < 0) { |
123 | bb36d470 | bellard | switch(errno) {
|
124 | bb36d470 | bellard | case ETIMEDOUT:
|
125 | bb36d470 | bellard | return USB_RET_NAK;
|
126 | bb36d470 | bellard | case EPIPE:
|
127 | bb36d470 | bellard | default:
|
128 | bb36d470 | bellard | #ifdef DEBUG
|
129 | bb36d470 | bellard | printf("handle_data: errno=%d\n", errno);
|
130 | bb36d470 | bellard | #endif
|
131 | bb36d470 | bellard | return USB_RET_STALL;
|
132 | bb36d470 | bellard | } |
133 | bb36d470 | bellard | } else {
|
134 | bb36d470 | bellard | return ret;
|
135 | bb36d470 | bellard | } |
136 | bb36d470 | bellard | } |
137 | bb36d470 | bellard | |
138 | bb36d470 | bellard | /* XXX: exclude high speed devices or implement EHCI */
|
139 | a594cfbf | bellard | USBDevice *usb_host_device_open(const char *devname) |
140 | bb36d470 | bellard | { |
141 | bb36d470 | bellard | int fd, interface, ret, i;
|
142 | bb36d470 | bellard | USBHostDevice *dev; |
143 | bb36d470 | bellard | struct usbdevfs_connectinfo ci;
|
144 | bb36d470 | bellard | uint8_t descr[1024];
|
145 | a594cfbf | bellard | char buf[1024]; |
146 | bb36d470 | bellard | int descr_len, dev_descr_len, config_descr_len, nb_interfaces;
|
147 | a594cfbf | bellard | int bus_num, addr;
|
148 | bb36d470 | bellard | |
149 | a594cfbf | bellard | if (usb_host_find_device(&bus_num, &addr, devname) < 0) |
150 | a594cfbf | bellard | return NULL; |
151 | a594cfbf | bellard | |
152 | a594cfbf | bellard | snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d", |
153 | a594cfbf | bellard | bus_num, addr); |
154 | a594cfbf | bellard | fd = open(buf, O_RDWR); |
155 | bb36d470 | bellard | if (fd < 0) { |
156 | a594cfbf | bellard | perror(buf); |
157 | a594cfbf | bellard | return NULL; |
158 | bb36d470 | bellard | } |
159 | bb36d470 | bellard | |
160 | bb36d470 | bellard | /* read the config description */
|
161 | bb36d470 | bellard | descr_len = read(fd, descr, sizeof(descr));
|
162 | bb36d470 | bellard | if (descr_len <= 0) { |
163 | bb36d470 | bellard | perror("read descr");
|
164 | bb36d470 | bellard | goto fail;
|
165 | bb36d470 | bellard | } |
166 | bb36d470 | bellard | |
167 | bb36d470 | bellard | i = 0;
|
168 | bb36d470 | bellard | dev_descr_len = descr[0];
|
169 | bb36d470 | bellard | if (dev_descr_len > descr_len)
|
170 | bb36d470 | bellard | goto fail;
|
171 | bb36d470 | bellard | i += dev_descr_len; |
172 | bb36d470 | bellard | config_descr_len = descr[i]; |
173 | bb36d470 | bellard | if (i + config_descr_len > descr_len)
|
174 | bb36d470 | bellard | goto fail;
|
175 | bb36d470 | bellard | nb_interfaces = descr[i + 4];
|
176 | bb36d470 | bellard | if (nb_interfaces != 1) { |
177 | bb36d470 | bellard | /* NOTE: currently we grab only one interface */
|
178 | a594cfbf | bellard | fprintf(stderr, "usb_host: only one interface supported\n");
|
179 | a594cfbf | bellard | goto fail;
|
180 | a594cfbf | bellard | } |
181 | a594cfbf | bellard | |
182 | a594cfbf | bellard | #ifdef USBDEVFS_DISCONNECT
|
183 | a594cfbf | bellard | /* earlier Linux 2.4 do not support that */
|
184 | 868bfe2b | bellard | { |
185 | 868bfe2b | bellard | struct usbdevfs_ioctl ctrl;
|
186 | 868bfe2b | bellard | ctrl.ioctl_code = USBDEVFS_DISCONNECT; |
187 | 868bfe2b | bellard | ctrl.ifno = 0;
|
188 | 868bfe2b | bellard | ret = ioctl(fd, USBDEVFS_IOCTL, &ctrl); |
189 | 868bfe2b | bellard | if (ret < 0 && errno != ENODATA) { |
190 | 868bfe2b | bellard | perror("USBDEVFS_DISCONNECT");
|
191 | 868bfe2b | bellard | goto fail;
|
192 | 868bfe2b | bellard | } |
193 | bb36d470 | bellard | } |
194 | a594cfbf | bellard | #endif
|
195 | a594cfbf | bellard | |
196 | bb36d470 | bellard | /* XXX: only grab if all interfaces are free */
|
197 | bb36d470 | bellard | interface = 0;
|
198 | bb36d470 | bellard | ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interface); |
199 | bb36d470 | bellard | if (ret < 0) { |
200 | bb36d470 | bellard | if (errno == EBUSY) {
|
201 | a594cfbf | bellard | fprintf(stderr, "usb_host: device already grabbed\n");
|
202 | bb36d470 | bellard | } else {
|
203 | bb36d470 | bellard | perror("USBDEVFS_CLAIMINTERFACE");
|
204 | bb36d470 | bellard | } |
205 | bb36d470 | bellard | fail:
|
206 | bb36d470 | bellard | close(fd); |
207 | a594cfbf | bellard | return NULL; |
208 | bb36d470 | bellard | } |
209 | bb36d470 | bellard | |
210 | bb36d470 | bellard | ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci); |
211 | bb36d470 | bellard | if (ret < 0) { |
212 | bb36d470 | bellard | perror("USBDEVFS_CONNECTINFO");
|
213 | bb36d470 | bellard | goto fail;
|
214 | bb36d470 | bellard | } |
215 | bb36d470 | bellard | |
216 | bb36d470 | bellard | #ifdef DEBUG
|
217 | a594cfbf | bellard | printf("host USB device %d.%d grabbed\n", bus_num, addr);
|
218 | bb36d470 | bellard | #endif
|
219 | bb36d470 | bellard | |
220 | bb36d470 | bellard | dev = qemu_mallocz(sizeof(USBHostDevice));
|
221 | bb36d470 | bellard | if (!dev)
|
222 | bb36d470 | bellard | goto fail;
|
223 | bb36d470 | bellard | dev->fd = fd; |
224 | bb36d470 | bellard | if (ci.slow)
|
225 | bb36d470 | bellard | dev->dev.speed = USB_SPEED_LOW; |
226 | bb36d470 | bellard | else
|
227 | bb36d470 | bellard | dev->dev.speed = USB_SPEED_HIGH; |
228 | a594cfbf | bellard | dev->dev.handle_packet = usb_generic_handle_packet; |
229 | bb36d470 | bellard | |
230 | bb36d470 | bellard | dev->dev.handle_reset = usb_host_handle_reset; |
231 | bb36d470 | bellard | dev->dev.handle_control = usb_host_handle_control; |
232 | bb36d470 | bellard | dev->dev.handle_data = usb_host_handle_data; |
233 | a594cfbf | bellard | return (USBDevice *)dev;
|
234 | a594cfbf | bellard | } |
235 | bb36d470 | bellard | |
236 | a594cfbf | bellard | static int get_tag_value(char *buf, int buf_size, |
237 | a594cfbf | bellard | const char *str, const char *tag, |
238 | a594cfbf | bellard | const char *stopchars) |
239 | a594cfbf | bellard | { |
240 | a594cfbf | bellard | const char *p; |
241 | a594cfbf | bellard | char *q;
|
242 | a594cfbf | bellard | p = strstr(str, tag); |
243 | a594cfbf | bellard | if (!p)
|
244 | a594cfbf | bellard | return -1; |
245 | a594cfbf | bellard | p += strlen(tag); |
246 | a594cfbf | bellard | while (isspace(*p))
|
247 | a594cfbf | bellard | p++; |
248 | a594cfbf | bellard | q = buf; |
249 | a594cfbf | bellard | while (*p != '\0' && !strchr(stopchars, *p)) { |
250 | a594cfbf | bellard | if ((q - buf) < (buf_size - 1)) |
251 | a594cfbf | bellard | *q++ = *p; |
252 | a594cfbf | bellard | p++; |
253 | a594cfbf | bellard | } |
254 | a594cfbf | bellard | *q = '\0';
|
255 | a594cfbf | bellard | return q - buf;
|
256 | bb36d470 | bellard | } |
257 | bb36d470 | bellard | |
258 | a594cfbf | bellard | static int usb_host_scan(void *opaque, USBScanFunc *func) |
259 | bb36d470 | bellard | { |
260 | a594cfbf | bellard | FILE *f; |
261 | a594cfbf | bellard | char line[1024]; |
262 | bb36d470 | bellard | char buf[1024]; |
263 | a594cfbf | bellard | int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
|
264 | a594cfbf | bellard | int ret;
|
265 | a594cfbf | bellard | char product_name[512]; |
266 | a594cfbf | bellard | |
267 | a594cfbf | bellard | f = fopen(USBDEVFS_PATH "/devices", "r"); |
268 | a594cfbf | bellard | if (!f) {
|
269 | a594cfbf | bellard | term_printf("Could not open %s\n", USBDEVFS_PATH "/devices"); |
270 | a594cfbf | bellard | return 0; |
271 | a594cfbf | bellard | } |
272 | a594cfbf | bellard | device_count = 0;
|
273 | a594cfbf | bellard | bus_num = addr = speed = class_id = product_id = vendor_id = 0;
|
274 | a594cfbf | bellard | ret = 0;
|
275 | bb36d470 | bellard | for(;;) {
|
276 | a594cfbf | bellard | if (fgets(line, sizeof(line), f) == NULL) |
277 | bb36d470 | bellard | break;
|
278 | a594cfbf | bellard | if (strlen(line) > 0) |
279 | a594cfbf | bellard | line[strlen(line) - 1] = '\0'; |
280 | a594cfbf | bellard | if (line[0] == 'T' && line[1] == ':') { |
281 | a594cfbf | bellard | if (device_count) {
|
282 | a594cfbf | bellard | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
283 | a594cfbf | bellard | product_id, product_name, speed); |
284 | a594cfbf | bellard | if (ret)
|
285 | a594cfbf | bellard | goto the_end;
|
286 | a594cfbf | bellard | } |
287 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) |
288 | a594cfbf | bellard | goto fail;
|
289 | a594cfbf | bellard | bus_num = atoi(buf); |
290 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) |
291 | a594cfbf | bellard | goto fail;
|
292 | a594cfbf | bellard | addr = atoi(buf); |
293 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) |
294 | a594cfbf | bellard | goto fail;
|
295 | a594cfbf | bellard | if (!strcmp(buf, "480")) |
296 | a594cfbf | bellard | speed = USB_SPEED_HIGH; |
297 | a594cfbf | bellard | else if (!strcmp(buf, "1.5")) |
298 | a594cfbf | bellard | speed = USB_SPEED_LOW; |
299 | a594cfbf | bellard | else
|
300 | a594cfbf | bellard | speed = USB_SPEED_FULL; |
301 | a594cfbf | bellard | product_name[0] = '\0'; |
302 | a594cfbf | bellard | class_id = 0xff;
|
303 | a594cfbf | bellard | device_count++; |
304 | a594cfbf | bellard | product_id = 0;
|
305 | a594cfbf | bellard | vendor_id = 0;
|
306 | a594cfbf | bellard | } else if (line[0] == 'P' && line[1] == ':') { |
307 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) |
308 | a594cfbf | bellard | goto fail;
|
309 | a594cfbf | bellard | vendor_id = strtoul(buf, NULL, 16); |
310 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) |
311 | a594cfbf | bellard | goto fail;
|
312 | a594cfbf | bellard | product_id = strtoul(buf, NULL, 16); |
313 | a594cfbf | bellard | } else if (line[0] == 'S' && line[1] == ':') { |
314 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) |
315 | a594cfbf | bellard | goto fail;
|
316 | a594cfbf | bellard | pstrcpy(product_name, sizeof(product_name), buf);
|
317 | a594cfbf | bellard | } else if (line[0] == 'D' && line[1] == ':') { |
318 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) |
319 | a594cfbf | bellard | goto fail;
|
320 | a594cfbf | bellard | class_id = strtoul(buf, NULL, 16); |
321 | bb36d470 | bellard | } |
322 | a594cfbf | bellard | fail: ;
|
323 | a594cfbf | bellard | } |
324 | a594cfbf | bellard | if (device_count) {
|
325 | a594cfbf | bellard | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
326 | a594cfbf | bellard | product_id, product_name, speed); |
327 | bb36d470 | bellard | } |
328 | a594cfbf | bellard | the_end:
|
329 | a594cfbf | bellard | fclose(f); |
330 | a594cfbf | bellard | return ret;
|
331 | bb36d470 | bellard | } |
332 | bb36d470 | bellard | |
333 | a594cfbf | bellard | typedef struct FindDeviceState { |
334 | a594cfbf | bellard | int vendor_id;
|
335 | a594cfbf | bellard | int product_id;
|
336 | a594cfbf | bellard | int bus_num;
|
337 | a594cfbf | bellard | int addr;
|
338 | a594cfbf | bellard | } FindDeviceState; |
339 | a594cfbf | bellard | |
340 | a594cfbf | bellard | static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, |
341 | a594cfbf | bellard | int class_id,
|
342 | a594cfbf | bellard | int vendor_id, int product_id, |
343 | a594cfbf | bellard | const char *product_name, int speed) |
344 | bb36d470 | bellard | { |
345 | a594cfbf | bellard | FindDeviceState *s = opaque; |
346 | a594cfbf | bellard | if (vendor_id == s->vendor_id &&
|
347 | a594cfbf | bellard | product_id == s->product_id) { |
348 | a594cfbf | bellard | s->bus_num = bus_num; |
349 | a594cfbf | bellard | s->addr = addr; |
350 | a594cfbf | bellard | return 1; |
351 | a594cfbf | bellard | } else {
|
352 | a594cfbf | bellard | return 0; |
353 | a594cfbf | bellard | } |
354 | a594cfbf | bellard | } |
355 | bb36d470 | bellard | |
356 | a594cfbf | bellard | /* the syntax is :
|
357 | a594cfbf | bellard | 'bus.addr' (decimal numbers) or
|
358 | a594cfbf | bellard | 'vendor_id:product_id' (hexa numbers) */
|
359 | a594cfbf | bellard | static int usb_host_find_device(int *pbus_num, int *paddr, |
360 | a594cfbf | bellard | const char *devname) |
361 | a594cfbf | bellard | { |
362 | a594cfbf | bellard | const char *p; |
363 | a594cfbf | bellard | int ret;
|
364 | a594cfbf | bellard | FindDeviceState fs; |
365 | a594cfbf | bellard | |
366 | a594cfbf | bellard | p = strchr(devname, '.');
|
367 | a594cfbf | bellard | if (p) {
|
368 | a594cfbf | bellard | *pbus_num = strtoul(devname, NULL, 0); |
369 | a594cfbf | bellard | *paddr = strtoul(p + 1, NULL, 0); |
370 | a594cfbf | bellard | return 0; |
371 | a594cfbf | bellard | } |
372 | a594cfbf | bellard | p = strchr(devname, ':');
|
373 | a594cfbf | bellard | if (p) {
|
374 | a594cfbf | bellard | fs.vendor_id = strtoul(devname, NULL, 16); |
375 | a594cfbf | bellard | fs.product_id = strtoul(p + 1, NULL, 16); |
376 | a594cfbf | bellard | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
377 | a594cfbf | bellard | if (ret) {
|
378 | a594cfbf | bellard | *pbus_num = fs.bus_num; |
379 | a594cfbf | bellard | *paddr = fs.addr; |
380 | a594cfbf | bellard | return 0; |
381 | bb36d470 | bellard | } |
382 | bb36d470 | bellard | } |
383 | a594cfbf | bellard | return -1; |
384 | bb36d470 | bellard | } |
385 | bb36d470 | bellard | |
386 | a594cfbf | bellard | /**********************/
|
387 | a594cfbf | bellard | /* USB host device info */
|
388 | a594cfbf | bellard | |
389 | a594cfbf | bellard | struct usb_class_info {
|
390 | a594cfbf | bellard | int class;
|
391 | a594cfbf | bellard | const char *class_name; |
392 | a594cfbf | bellard | }; |
393 | a594cfbf | bellard | |
394 | a594cfbf | bellard | static const struct usb_class_info usb_class_info[] = { |
395 | a594cfbf | bellard | { USB_CLASS_AUDIO, "Audio"},
|
396 | a594cfbf | bellard | { USB_CLASS_COMM, "Communication"},
|
397 | a594cfbf | bellard | { USB_CLASS_HID, "HID"},
|
398 | a594cfbf | bellard | { USB_CLASS_HUB, "Hub" },
|
399 | a594cfbf | bellard | { USB_CLASS_PHYSICAL, "Physical" },
|
400 | a594cfbf | bellard | { USB_CLASS_PRINTER, "Printer" },
|
401 | a594cfbf | bellard | { USB_CLASS_MASS_STORAGE, "Storage" },
|
402 | a594cfbf | bellard | { USB_CLASS_CDC_DATA, "Data" },
|
403 | a594cfbf | bellard | { USB_CLASS_APP_SPEC, "Application Specific" },
|
404 | a594cfbf | bellard | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
|
405 | a594cfbf | bellard | { USB_CLASS_STILL_IMAGE, "Still Image" },
|
406 | a594cfbf | bellard | { USB_CLASS_CSCID, "Smart Card" },
|
407 | a594cfbf | bellard | { USB_CLASS_CONTENT_SEC, "Content Security" },
|
408 | a594cfbf | bellard | { -1, NULL } |
409 | a594cfbf | bellard | }; |
410 | a594cfbf | bellard | |
411 | a594cfbf | bellard | static const char *usb_class_str(uint8_t class) |
412 | bb36d470 | bellard | { |
413 | a594cfbf | bellard | const struct usb_class_info *p; |
414 | a594cfbf | bellard | for(p = usb_class_info; p->class != -1; p++) { |
415 | a594cfbf | bellard | if (p->class == class)
|
416 | a594cfbf | bellard | break;
|
417 | bb36d470 | bellard | } |
418 | a594cfbf | bellard | return p->class_name;
|
419 | a594cfbf | bellard | } |
420 | a594cfbf | bellard | |
421 | a594cfbf | bellard | void usb_info_device(int bus_num, int addr, int class_id, |
422 | a594cfbf | bellard | int vendor_id, int product_id, |
423 | a594cfbf | bellard | const char *product_name, |
424 | a594cfbf | bellard | int speed)
|
425 | a594cfbf | bellard | { |
426 | a594cfbf | bellard | const char *class_str, *speed_str; |
427 | a594cfbf | bellard | |
428 | a594cfbf | bellard | switch(speed) {
|
429 | a594cfbf | bellard | case USB_SPEED_LOW:
|
430 | a594cfbf | bellard | speed_str = "1.5";
|
431 | a594cfbf | bellard | break;
|
432 | a594cfbf | bellard | case USB_SPEED_FULL:
|
433 | a594cfbf | bellard | speed_str = "12";
|
434 | a594cfbf | bellard | break;
|
435 | a594cfbf | bellard | case USB_SPEED_HIGH:
|
436 | a594cfbf | bellard | speed_str = "480";
|
437 | a594cfbf | bellard | break;
|
438 | a594cfbf | bellard | default:
|
439 | a594cfbf | bellard | speed_str = "?";
|
440 | a594cfbf | bellard | break;
|
441 | a594cfbf | bellard | } |
442 | a594cfbf | bellard | |
443 | a594cfbf | bellard | term_printf(" Device %d.%d, speed %s Mb/s\n",
|
444 | a594cfbf | bellard | bus_num, addr, speed_str); |
445 | a594cfbf | bellard | class_str = usb_class_str(class_id); |
446 | a594cfbf | bellard | if (class_str)
|
447 | a594cfbf | bellard | term_printf(" %s:", class_str);
|
448 | a594cfbf | bellard | else
|
449 | a594cfbf | bellard | term_printf(" Class %02x:", class_id);
|
450 | a594cfbf | bellard | term_printf(" USB device %04x:%04x", vendor_id, product_id);
|
451 | a594cfbf | bellard | if (product_name[0] != '\0') |
452 | a594cfbf | bellard | term_printf(", %s", product_name);
|
453 | a594cfbf | bellard | term_printf("\n");
|
454 | a594cfbf | bellard | } |
455 | a594cfbf | bellard | |
456 | a594cfbf | bellard | static int usb_host_info_device(void *opaque, int bus_num, int addr, |
457 | a594cfbf | bellard | int class_id,
|
458 | a594cfbf | bellard | int vendor_id, int product_id, |
459 | a594cfbf | bellard | const char *product_name, |
460 | a594cfbf | bellard | int speed)
|
461 | a594cfbf | bellard | { |
462 | a594cfbf | bellard | usb_info_device(bus_num, addr, class_id, vendor_id, product_id, |
463 | a594cfbf | bellard | product_name, speed); |
464 | a594cfbf | bellard | return 0; |
465 | a594cfbf | bellard | } |
466 | a594cfbf | bellard | |
467 | a594cfbf | bellard | void usb_host_info(void) |
468 | a594cfbf | bellard | { |
469 | a594cfbf | bellard | usb_host_scan(NULL, usb_host_info_device);
|
470 | bb36d470 | bellard | } |
471 | bb36d470 | bellard | |
472 | bb36d470 | bellard | #else
|
473 | bb36d470 | bellard | |
474 | a594cfbf | bellard | void usb_host_info(void) |
475 | a594cfbf | bellard | { |
476 | a594cfbf | bellard | term_printf("USB host devices not supported\n");
|
477 | a594cfbf | bellard | } |
478 | a594cfbf | bellard | |
479 | bb36d470 | bellard | /* XXX: modify configure to compile the right host driver */
|
480 | a594cfbf | bellard | USBDevice *usb_host_device_open(const char *devname) |
481 | bb36d470 | bellard | { |
482 | bb36d470 | bellard | return NULL; |
483 | bb36d470 | bellard | } |
484 | bb36d470 | bellard | |
485 | bb36d470 | bellard | #endif |