Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / usb / host-legacy.c
1 /*
2  * Linux host USB redirector
3  *
4  * Copyright (c) 2005 Fabrice Bellard
5  *
6  * Copyright (c) 2008 Max Krasnyansky
7  *      Support for host device auto connect & disconnect
8  *      Major rewrite to support fully async operation
9  *
10  * Copyright 2008 TJ <linux@tjworld.net>
11  *      Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12  *      to the legacy /proc/bus/usb USB device discovery and handling
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this software and associated documentation files (the "Software"), to deal
16  * in the Software without restriction, including without limitation the rights
17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  * copies of the Software, and to permit persons to whom the Software is
19  * furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30  * THE SOFTWARE.
31  */
32
33 #include "qemu-common.h"
34 #include "hw/usb.h"
35 #include "hw/usb/host.h"
36
37 /*
38  * Autoconnect filter
39  * Format:
40  *    auto:bus:dev[:vid:pid]
41  *    auto:bus.dev[:vid:pid]
42  *
43  *    bus  - bus number    (dec, * means any)
44  *    dev  - device number (dec, * means any)
45  *    vid  - vendor id     (hex, * means any)
46  *    pid  - product id    (hex, * means any)
47  *
48  *    See 'lsusb' output.
49  */
50 static int parse_filter(const char *spec, struct USBAutoFilter *f)
51 {
52     enum { BUS, DEV, VID, PID, DONE };
53     const char *p = spec;
54     int i;
55
56     f->bus_num    = 0;
57     f->addr       = 0;
58     f->vendor_id  = 0;
59     f->product_id = 0;
60
61     for (i = BUS; i < DONE; i++) {
62         p = strpbrk(p, ":.");
63         if (!p) {
64             break;
65         }
66         p++;
67
68         if (*p == '*') {
69             continue;
70         }
71         switch (i) {
72         case BUS:
73             f->bus_num = strtol(p, NULL, 10);
74             break;
75         case DEV:
76             f->addr    = strtol(p, NULL, 10);
77             break;
78         case VID:
79             f->vendor_id  = strtol(p, NULL, 16);
80             break;
81         case PID:
82             f->product_id = strtol(p, NULL, 16);
83             break;
84         }
85     }
86
87     if (i < DEV) {
88         fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
89         return -1;
90     }
91
92     return 0;
93 }
94
95 USBDevice *usb_host_device_open(USBBus *bus, const char *devname)
96 {
97     struct USBAutoFilter filter;
98     USBDevice *dev;
99     char *p;
100
101     dev = usb_create(bus, "usb-host");
102
103     if (strstr(devname, "auto:")) {
104         if (parse_filter(devname, &filter) < 0) {
105             goto fail;
106         }
107     } else {
108         p = strchr(devname, '.');
109         if (p) {
110             filter.bus_num    = strtoul(devname, NULL, 0);
111             filter.addr       = strtoul(p + 1, NULL, 0);
112             filter.vendor_id  = 0;
113             filter.product_id = 0;
114         } else {
115             p = strchr(devname, ':');
116             if (p) {
117                 filter.bus_num    = 0;
118                 filter.addr       = 0;
119                 filter.vendor_id  = strtoul(devname, NULL, 16);
120                 filter.product_id = strtoul(p + 1, NULL, 16);
121             } else {
122                 goto fail;
123             }
124         }
125     }
126
127     qdev_prop_set_uint32(&dev->qdev, "hostbus",   filter.bus_num);
128     qdev_prop_set_uint32(&dev->qdev, "hostaddr",  filter.addr);
129     qdev_prop_set_uint32(&dev->qdev, "vendorid",  filter.vendor_id);
130     qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
131     return dev;
132
133 fail:
134     object_unparent(OBJECT(dev));
135     return NULL;
136 }
137
138 static void usb_host_register_types(void)
139 {
140     usb_legacy_register("usb-host", "host", usb_host_device_open);
141 }
142
143 type_init(usb_host_register_types)