root / usb-bsd.c @ 67f0875e
History | View | Annotate | Download (16.9 kB)
1 | 68063649 | blueswir1 | /*
|
---|---|---|---|
2 | 68063649 | blueswir1 | * BSD host USB redirector
|
3 | 68063649 | blueswir1 | *
|
4 | 68063649 | blueswir1 | * Copyright (c) 2006 Lonnie Mendez
|
5 | 68063649 | blueswir1 | * Portions of code and concepts borrowed from
|
6 | 68063649 | blueswir1 | * usb-linux.c and libusb's bsd.c and are copyright their respective owners.
|
7 | 68063649 | blueswir1 | *
|
8 | 68063649 | blueswir1 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 | 68063649 | blueswir1 | * of this software and associated documentation files (the "Software"), to deal
|
10 | 68063649 | blueswir1 | * in the Software without restriction, including without limitation the rights
|
11 | 68063649 | blueswir1 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 | 68063649 | blueswir1 | * copies of the Software, and to permit persons to whom the Software is
|
13 | 68063649 | blueswir1 | * furnished to do so, subject to the following conditions:
|
14 | 68063649 | blueswir1 | *
|
15 | 68063649 | blueswir1 | * The above copyright notice and this permission notice shall be included in
|
16 | 68063649 | blueswir1 | * all copies or substantial portions of the Software.
|
17 | 68063649 | blueswir1 | *
|
18 | 68063649 | blueswir1 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 | 68063649 | blueswir1 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 | 68063649 | blueswir1 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
21 | 68063649 | blueswir1 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 | 68063649 | blueswir1 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 | 68063649 | blueswir1 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24 | 68063649 | blueswir1 | * THE SOFTWARE.
|
25 | 68063649 | blueswir1 | */
|
26 | 68063649 | blueswir1 | |
27 | 68063649 | blueswir1 | #include "qemu-common.h" |
28 | 0b8a988c | blueswir1 | #include "monitor.h" |
29 | 68063649 | blueswir1 | #include "hw/usb.h" |
30 | 68063649 | blueswir1 | |
31 | 68063649 | blueswir1 | /* usb.h declares these */
|
32 | 68063649 | blueswir1 | #undef USB_SPEED_HIGH
|
33 | 68063649 | blueswir1 | #undef USB_SPEED_FULL
|
34 | 68063649 | blueswir1 | #undef USB_SPEED_LOW
|
35 | 68063649 | blueswir1 | |
36 | 68063649 | blueswir1 | #include <sys/ioctl.h> |
37 | c5e97233 | blueswir1 | #ifndef __DragonFly__
|
38 | 68063649 | blueswir1 | #include <dev/usb/usb.h> |
39 | c5e97233 | blueswir1 | #else
|
40 | c5e97233 | blueswir1 | #include <bus/usb/usb.h> |
41 | c5e97233 | blueswir1 | #endif
|
42 | 68063649 | blueswir1 | #include <signal.h> |
43 | 68063649 | blueswir1 | |
44 | 68063649 | blueswir1 | /* This value has maximum potential at 16.
|
45 | 68063649 | blueswir1 | * You should also set hw.usb.debug to gain
|
46 | 68063649 | blueswir1 | * more detailed view.
|
47 | 68063649 | blueswir1 | */
|
48 | 68063649 | blueswir1 | //#define DEBUG
|
49 | 68063649 | blueswir1 | #define UGEN_DEBUG_LEVEL 0 |
50 | 68063649 | blueswir1 | |
51 | 68063649 | blueswir1 | |
52 | 68063649 | blueswir1 | typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id, |
53 | 68063649 | blueswir1 | int vendor_id, int product_id, |
54 | 68063649 | blueswir1 | const char *product_name, int speed); |
55 | 68063649 | blueswir1 | static int usb_host_find_device(int *pbus_num, int *paddr, |
56 | 68063649 | blueswir1 | const char *devname); |
57 | 68063649 | blueswir1 | |
58 | 68063649 | blueswir1 | typedef struct USBHostDevice { |
59 | 68063649 | blueswir1 | USBDevice dev; |
60 | 68063649 | blueswir1 | int ep_fd[USB_MAX_ENDPOINTS];
|
61 | 68063649 | blueswir1 | int devfd;
|
62 | 68063649 | blueswir1 | char devpath[32]; |
63 | 68063649 | blueswir1 | } USBHostDevice; |
64 | 68063649 | blueswir1 | |
65 | 68063649 | blueswir1 | |
66 | 68063649 | blueswir1 | static int ensure_ep_open(USBHostDevice *dev, int ep, int mode) |
67 | 68063649 | blueswir1 | { |
68 | 68063649 | blueswir1 | char buf[32]; |
69 | 68063649 | blueswir1 | int fd;
|
70 | 68063649 | blueswir1 | |
71 | 68063649 | blueswir1 | /* Get the address for this endpoint */
|
72 | 68063649 | blueswir1 | ep = UE_GET_ADDR(ep); |
73 | 68063649 | blueswir1 | |
74 | 68063649 | blueswir1 | if (dev->ep_fd[ep] < 0) { |
75 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
76 | 68063649 | blueswir1 | snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->devpath, ep); |
77 | 68063649 | blueswir1 | #else
|
78 | 68063649 | blueswir1 | snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->devpath, ep); |
79 | 68063649 | blueswir1 | #endif
|
80 | 68063649 | blueswir1 | /* Try to open it O_RDWR first for those devices which have in and out
|
81 | 68063649 | blueswir1 | * endpoints with the same address (eg 0x02 and 0x82)
|
82 | 68063649 | blueswir1 | */
|
83 | 68063649 | blueswir1 | fd = open(buf, O_RDWR); |
84 | 68063649 | blueswir1 | if (fd < 0 && errno == ENXIO) |
85 | 68063649 | blueswir1 | fd = open(buf, mode); |
86 | 68063649 | blueswir1 | if (fd < 0) { |
87 | 68063649 | blueswir1 | #ifdef DEBUG
|
88 | 68063649 | blueswir1 | printf("ensure_ep_open: failed to open device endpoint %s: %s\n",
|
89 | 68063649 | blueswir1 | buf, strerror(errno)); |
90 | 68063649 | blueswir1 | #endif
|
91 | 68063649 | blueswir1 | } |
92 | 68063649 | blueswir1 | dev->ep_fd[ep] = fd; |
93 | 68063649 | blueswir1 | } |
94 | 68063649 | blueswir1 | |
95 | 68063649 | blueswir1 | return dev->ep_fd[ep];
|
96 | 68063649 | blueswir1 | } |
97 | 68063649 | blueswir1 | |
98 | 68063649 | blueswir1 | static void ensure_eps_closed(USBHostDevice *dev) |
99 | 68063649 | blueswir1 | { |
100 | 68063649 | blueswir1 | int epnum = 1; |
101 | 68063649 | blueswir1 | |
102 | 68063649 | blueswir1 | if (!dev)
|
103 | 68063649 | blueswir1 | return;
|
104 | 68063649 | blueswir1 | |
105 | 68063649 | blueswir1 | while (epnum < USB_MAX_ENDPOINTS) {
|
106 | 68063649 | blueswir1 | if (dev->ep_fd[epnum] >= 0) { |
107 | 68063649 | blueswir1 | close(dev->ep_fd[epnum]); |
108 | 68063649 | blueswir1 | dev->ep_fd[epnum] = -1;
|
109 | 68063649 | blueswir1 | } |
110 | 68063649 | blueswir1 | epnum++; |
111 | 68063649 | blueswir1 | } |
112 | 68063649 | blueswir1 | } |
113 | 68063649 | blueswir1 | |
114 | 68063649 | blueswir1 | static void usb_host_handle_reset(USBDevice *dev) |
115 | 68063649 | blueswir1 | { |
116 | 68063649 | blueswir1 | #if 0
|
117 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)dev;
|
118 | 68063649 | blueswir1 | #endif
|
119 | 68063649 | blueswir1 | } |
120 | 68063649 | blueswir1 | |
121 | 68063649 | blueswir1 | /* XXX:
|
122 | 68063649 | blueswir1 | * -check device states against transfer requests
|
123 | 68063649 | blueswir1 | * and return appropriate response
|
124 | 68063649 | blueswir1 | */
|
125 | 68063649 | blueswir1 | static int usb_host_handle_control(USBDevice *dev, |
126 | 68063649 | blueswir1 | int request,
|
127 | 68063649 | blueswir1 | int value,
|
128 | 68063649 | blueswir1 | int index,
|
129 | 68063649 | blueswir1 | int length,
|
130 | 68063649 | blueswir1 | uint8_t *data) |
131 | 68063649 | blueswir1 | { |
132 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)dev; |
133 | 68063649 | blueswir1 | struct usb_ctl_request req;
|
134 | 68063649 | blueswir1 | struct usb_alt_interface aiface;
|
135 | 68063649 | blueswir1 | int ret, timeout = 50; |
136 | 68063649 | blueswir1 | |
137 | 68063649 | blueswir1 | if ((request >> 8) == UT_WRITE_DEVICE && |
138 | 68063649 | blueswir1 | (request & 0xff) == UR_SET_ADDRESS) {
|
139 | 68063649 | blueswir1 | |
140 | 68063649 | blueswir1 | /* specific SET_ADDRESS support */
|
141 | 68063649 | blueswir1 | dev->addr = value; |
142 | 68063649 | blueswir1 | return 0; |
143 | 68063649 | blueswir1 | } else if ((request >> 8) == UT_WRITE_DEVICE && |
144 | 68063649 | blueswir1 | (request & 0xff) == UR_SET_CONFIG) {
|
145 | 68063649 | blueswir1 | |
146 | 68063649 | blueswir1 | ensure_eps_closed(s); /* can't do this without all eps closed */
|
147 | 68063649 | blueswir1 | |
148 | 68063649 | blueswir1 | ret = ioctl(s->devfd, USB_SET_CONFIG, &value); |
149 | 68063649 | blueswir1 | if (ret < 0) { |
150 | 68063649 | blueswir1 | #ifdef DEBUG
|
151 | 68063649 | blueswir1 | printf("handle_control: failed to set configuration - %s\n",
|
152 | 68063649 | blueswir1 | strerror(errno)); |
153 | 68063649 | blueswir1 | #endif
|
154 | 68063649 | blueswir1 | return USB_RET_STALL;
|
155 | 68063649 | blueswir1 | } |
156 | 68063649 | blueswir1 | |
157 | 68063649 | blueswir1 | return 0; |
158 | 68063649 | blueswir1 | } else if ((request >> 8) == UT_WRITE_INTERFACE && |
159 | 68063649 | blueswir1 | (request & 0xff) == UR_SET_INTERFACE) {
|
160 | 68063649 | blueswir1 | |
161 | 68063649 | blueswir1 | aiface.uai_interface_index = index; |
162 | 68063649 | blueswir1 | aiface.uai_alt_no = value; |
163 | 68063649 | blueswir1 | |
164 | 68063649 | blueswir1 | ensure_eps_closed(s); /* can't do this without all eps closed */
|
165 | 68063649 | blueswir1 | ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface); |
166 | 68063649 | blueswir1 | if (ret < 0) { |
167 | 68063649 | blueswir1 | #ifdef DEBUG
|
168 | 68063649 | blueswir1 | printf("handle_control: failed to set alternate interface - %s\n",
|
169 | 68063649 | blueswir1 | strerror(errno)); |
170 | 68063649 | blueswir1 | #endif
|
171 | 68063649 | blueswir1 | return USB_RET_STALL;
|
172 | 68063649 | blueswir1 | } |
173 | 68063649 | blueswir1 | |
174 | 68063649 | blueswir1 | return 0; |
175 | 68063649 | blueswir1 | } else {
|
176 | 68063649 | blueswir1 | req.ucr_request.bmRequestType = request >> 8;
|
177 | 68063649 | blueswir1 | req.ucr_request.bRequest = request & 0xff;
|
178 | 68063649 | blueswir1 | USETW(req.ucr_request.wValue, value); |
179 | 68063649 | blueswir1 | USETW(req.ucr_request.wIndex, index); |
180 | 68063649 | blueswir1 | USETW(req.ucr_request.wLength, length); |
181 | 68063649 | blueswir1 | req.ucr_data = data; |
182 | 68063649 | blueswir1 | req.ucr_flags = USBD_SHORT_XFER_OK; |
183 | 68063649 | blueswir1 | |
184 | 68063649 | blueswir1 | ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout); |
185 | 00766a4e | blueswir1 | #if defined(__NetBSD__) || defined(__OpenBSD__)
|
186 | 68063649 | blueswir1 | if (ret < 0 && errno != EINVAL) { |
187 | 68063649 | blueswir1 | #else
|
188 | 68063649 | blueswir1 | if (ret < 0) { |
189 | 68063649 | blueswir1 | #endif
|
190 | 68063649 | blueswir1 | #ifdef DEBUG
|
191 | 68063649 | blueswir1 | printf("handle_control: setting timeout failed - %s\n",
|
192 | 68063649 | blueswir1 | strerror(errno)); |
193 | 68063649 | blueswir1 | #endif
|
194 | 68063649 | blueswir1 | } |
195 | 68063649 | blueswir1 | |
196 | 68063649 | blueswir1 | ret = ioctl(s->devfd, USB_DO_REQUEST, &req); |
197 | 68063649 | blueswir1 | /* ugen returns EIO for usbd_do_request_ no matter what
|
198 | 68063649 | blueswir1 | * happens with the transfer */
|
199 | 68063649 | blueswir1 | if (ret < 0) { |
200 | 68063649 | blueswir1 | #ifdef DEBUG
|
201 | 68063649 | blueswir1 | printf("handle_control: error after request - %s\n",
|
202 | 68063649 | blueswir1 | strerror(errno)); |
203 | 68063649 | blueswir1 | #endif
|
204 | 68063649 | blueswir1 | return USB_RET_NAK; // STALL |
205 | 68063649 | blueswir1 | } else {
|
206 | 68063649 | blueswir1 | return req.ucr_actlen;
|
207 | 68063649 | blueswir1 | } |
208 | 68063649 | blueswir1 | } |
209 | 68063649 | blueswir1 | } |
210 | 68063649 | blueswir1 | |
211 | 68063649 | blueswir1 | static int usb_host_handle_data(USBDevice *dev, USBPacket *p) |
212 | 68063649 | blueswir1 | { |
213 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)dev; |
214 | 68063649 | blueswir1 | int ret, fd, mode;
|
215 | 68063649 | blueswir1 | int one = 1, shortpacket = 0, timeout = 50; |
216 | 68063649 | blueswir1 | sigset_t new_mask, old_mask; |
217 | 68063649 | blueswir1 | uint8_t devep = p->devep; |
218 | 68063649 | blueswir1 | |
219 | 68063649 | blueswir1 | /* protect data transfers from SIGALRM signal */
|
220 | 68063649 | blueswir1 | sigemptyset(&new_mask); |
221 | 68063649 | blueswir1 | sigaddset(&new_mask, SIGALRM); |
222 | 68063649 | blueswir1 | sigprocmask(SIG_BLOCK, &new_mask, &old_mask); |
223 | 68063649 | blueswir1 | |
224 | 68063649 | blueswir1 | if (p->pid == USB_TOKEN_IN) {
|
225 | 68063649 | blueswir1 | devep |= 0x80;
|
226 | 68063649 | blueswir1 | mode = O_RDONLY; |
227 | 68063649 | blueswir1 | shortpacket = 1;
|
228 | 68063649 | blueswir1 | } else {
|
229 | 68063649 | blueswir1 | mode = O_WRONLY; |
230 | 68063649 | blueswir1 | } |
231 | 68063649 | blueswir1 | |
232 | 68063649 | blueswir1 | fd = ensure_ep_open(s, devep, mode); |
233 | 68063649 | blueswir1 | if (fd < 0) { |
234 | 68063649 | blueswir1 | sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
235 | 68063649 | blueswir1 | return USB_RET_NODEV;
|
236 | 68063649 | blueswir1 | } |
237 | 68063649 | blueswir1 | |
238 | 68063649 | blueswir1 | if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) { |
239 | 68063649 | blueswir1 | #ifdef DEBUG
|
240 | 68063649 | blueswir1 | printf("handle_data: failed to set timeout - %s\n",
|
241 | 68063649 | blueswir1 | strerror(errno)); |
242 | 68063649 | blueswir1 | #endif
|
243 | 68063649 | blueswir1 | } |
244 | 68063649 | blueswir1 | |
245 | 68063649 | blueswir1 | if (shortpacket) {
|
246 | 68063649 | blueswir1 | if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) { |
247 | 68063649 | blueswir1 | #ifdef DEBUG
|
248 | 68063649 | blueswir1 | printf("handle_data: failed to set short xfer mode - %s\n",
|
249 | 68063649 | blueswir1 | strerror(errno)); |
250 | 68063649 | blueswir1 | #endif
|
251 | 68063649 | blueswir1 | sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
252 | 68063649 | blueswir1 | } |
253 | 68063649 | blueswir1 | } |
254 | 68063649 | blueswir1 | |
255 | 68063649 | blueswir1 | if (p->pid == USB_TOKEN_IN)
|
256 | 68063649 | blueswir1 | ret = read(fd, p->data, p->len); |
257 | 68063649 | blueswir1 | else
|
258 | 68063649 | blueswir1 | ret = write(fd, p->data, p->len); |
259 | 68063649 | blueswir1 | |
260 | 68063649 | blueswir1 | sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
261 | 68063649 | blueswir1 | |
262 | 68063649 | blueswir1 | if (ret < 0) { |
263 | 68063649 | blueswir1 | #ifdef DEBUG
|
264 | 68063649 | blueswir1 | printf("handle_data: error after %s data - %s\n",
|
265 | 68063649 | blueswir1 | pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno)); |
266 | 68063649 | blueswir1 | #endif
|
267 | 68063649 | blueswir1 | switch(errno) {
|
268 | 68063649 | blueswir1 | case ETIMEDOUT:
|
269 | 68063649 | blueswir1 | case EINTR:
|
270 | 68063649 | blueswir1 | return USB_RET_NAK;
|
271 | 68063649 | blueswir1 | default:
|
272 | 68063649 | blueswir1 | return USB_RET_STALL;
|
273 | 68063649 | blueswir1 | } |
274 | 68063649 | blueswir1 | } else {
|
275 | 68063649 | blueswir1 | return ret;
|
276 | 68063649 | blueswir1 | } |
277 | 68063649 | blueswir1 | } |
278 | 68063649 | blueswir1 | |
279 | 68063649 | blueswir1 | static void usb_host_handle_destroy(USBDevice *opaque) |
280 | 68063649 | blueswir1 | { |
281 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)opaque; |
282 | 68063649 | blueswir1 | int i;
|
283 | 68063649 | blueswir1 | |
284 | 68063649 | blueswir1 | for (i = 0; i < USB_MAX_ENDPOINTS; i++) |
285 | 68063649 | blueswir1 | if (s->ep_fd[i] >= 0) |
286 | 68063649 | blueswir1 | close(s->ep_fd[i]); |
287 | 68063649 | blueswir1 | |
288 | 68063649 | blueswir1 | if (s->devfd < 0) |
289 | 68063649 | blueswir1 | return;
|
290 | 68063649 | blueswir1 | |
291 | 68063649 | blueswir1 | close(s->devfd); |
292 | 68063649 | blueswir1 | |
293 | 68063649 | blueswir1 | qemu_free(s); |
294 | 68063649 | blueswir1 | } |
295 | 68063649 | blueswir1 | |
296 | 68063649 | blueswir1 | USBDevice *usb_host_device_open(const char *devname) |
297 | 68063649 | blueswir1 | { |
298 | 68063649 | blueswir1 | struct usb_device_info bus_info, dev_info;
|
299 | 68063649 | blueswir1 | USBHostDevice *dev; |
300 | 68063649 | blueswir1 | char ctlpath[PATH_MAX + 1]; |
301 | 68063649 | blueswir1 | char buspath[PATH_MAX + 1]; |
302 | 68063649 | blueswir1 | int bfd, dfd, bus, address, i;
|
303 | 68063649 | blueswir1 | int ugendebug = UGEN_DEBUG_LEVEL;
|
304 | 68063649 | blueswir1 | |
305 | 68063649 | blueswir1 | if (usb_host_find_device(&bus, &address, devname) < 0) |
306 | 68063649 | blueswir1 | return NULL; |
307 | 68063649 | blueswir1 | |
308 | 68063649 | blueswir1 | snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
|
309 | 68063649 | blueswir1 | |
310 | 68063649 | blueswir1 | bfd = open(buspath, O_RDWR); |
311 | 68063649 | blueswir1 | if (bfd < 0) { |
312 | 68063649 | blueswir1 | #ifdef DEBUG
|
313 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to open usb bus - %s\n",
|
314 | 68063649 | blueswir1 | strerror(errno)); |
315 | 68063649 | blueswir1 | #endif
|
316 | 68063649 | blueswir1 | return NULL; |
317 | 68063649 | blueswir1 | } |
318 | 68063649 | blueswir1 | |
319 | 68063649 | blueswir1 | bus_info.udi_addr = address; |
320 | 68063649 | blueswir1 | if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) { |
321 | 68063649 | blueswir1 | #ifdef DEBUG
|
322 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to grab bus information - %s\n",
|
323 | 68063649 | blueswir1 | strerror(errno)); |
324 | 68063649 | blueswir1 | #endif
|
325 | 68063649 | blueswir1 | return NULL; |
326 | 68063649 | blueswir1 | } |
327 | 68063649 | blueswir1 | |
328 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
329 | 68063649 | blueswir1 | snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]); |
330 | 68063649 | blueswir1 | #else
|
331 | 68063649 | blueswir1 | snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]); |
332 | 68063649 | blueswir1 | #endif
|
333 | 68063649 | blueswir1 | |
334 | 68063649 | blueswir1 | dfd = open(ctlpath, O_RDWR); |
335 | 68063649 | blueswir1 | if (dfd < 0) { |
336 | 68063649 | blueswir1 | dfd = open(ctlpath, O_RDONLY); |
337 | 68063649 | blueswir1 | if (dfd < 0) { |
338 | 68063649 | blueswir1 | #ifdef DEBUG
|
339 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to open usb device %s - %s\n",
|
340 | 68063649 | blueswir1 | ctlpath, strerror(errno)); |
341 | 68063649 | blueswir1 | #endif
|
342 | 68063649 | blueswir1 | } |
343 | 68063649 | blueswir1 | } |
344 | 68063649 | blueswir1 | |
345 | 68063649 | blueswir1 | if (dfd >= 0) { |
346 | 68063649 | blueswir1 | dev = qemu_mallocz(sizeof(USBHostDevice));
|
347 | 68063649 | blueswir1 | dev->devfd = dfd; |
348 | 68063649 | blueswir1 | |
349 | 68063649 | blueswir1 | if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) { |
350 | 68063649 | blueswir1 | #ifdef DEBUG
|
351 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to grab device info - %s\n",
|
352 | 68063649 | blueswir1 | strerror(errno)); |
353 | 68063649 | blueswir1 | #endif
|
354 | 68063649 | blueswir1 | goto fail;
|
355 | 68063649 | blueswir1 | } |
356 | 68063649 | blueswir1 | |
357 | 68063649 | blueswir1 | if (dev_info.udi_speed == 1) |
358 | 68063649 | blueswir1 | dev->dev.speed = USB_SPEED_LOW - 1;
|
359 | 68063649 | blueswir1 | else
|
360 | 68063649 | blueswir1 | dev->dev.speed = USB_SPEED_FULL - 1;
|
361 | 68063649 | blueswir1 | |
362 | 68063649 | blueswir1 | dev->dev.handle_packet = usb_generic_handle_packet; |
363 | 68063649 | blueswir1 | |
364 | 68063649 | blueswir1 | dev->dev.handle_reset = usb_host_handle_reset; |
365 | 68063649 | blueswir1 | dev->dev.handle_control = usb_host_handle_control; |
366 | 68063649 | blueswir1 | dev->dev.handle_data = usb_host_handle_data; |
367 | 68063649 | blueswir1 | dev->dev.handle_destroy = usb_host_handle_destroy; |
368 | 68063649 | blueswir1 | |
369 | 68063649 | blueswir1 | if (strncmp(dev_info.udi_product, "product", 7) != 0) |
370 | 68063649 | blueswir1 | pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
|
371 | 68063649 | blueswir1 | dev_info.udi_product); |
372 | 68063649 | blueswir1 | else
|
373 | 68063649 | blueswir1 | snprintf(dev->dev.devname, sizeof(dev->dev.devname),
|
374 | 68063649 | blueswir1 | "host:%s", devname);
|
375 | 68063649 | blueswir1 | |
376 | 68063649 | blueswir1 | pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/"); |
377 | 00766a4e | blueswir1 | pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]); |
378 | 68063649 | blueswir1 | |
379 | 68063649 | blueswir1 | /* Mark the endpoints as not yet open */
|
380 | 68063649 | blueswir1 | for (i = 0; i < USB_MAX_ENDPOINTS; i++) |
381 | 68063649 | blueswir1 | dev->ep_fd[i] = -1;
|
382 | 68063649 | blueswir1 | |
383 | 68063649 | blueswir1 | ioctl(dfd, USB_SETDEBUG, &ugendebug); |
384 | 68063649 | blueswir1 | |
385 | 68063649 | blueswir1 | return (USBDevice *)dev;
|
386 | 68063649 | blueswir1 | } |
387 | 68063649 | blueswir1 | |
388 | 68063649 | blueswir1 | fail:
|
389 | 68063649 | blueswir1 | return NULL; |
390 | 68063649 | blueswir1 | } |
391 | 68063649 | blueswir1 | |
392 | 68063649 | blueswir1 | static int usb_host_scan(void *opaque, USBScanFunc *func) |
393 | 68063649 | blueswir1 | { |
394 | 68063649 | blueswir1 | struct usb_device_info bus_info;
|
395 | 68063649 | blueswir1 | struct usb_device_info dev_info;
|
396 | 68063649 | blueswir1 | uint16_t vendor_id, product_id, class_id, speed; |
397 | 68063649 | blueswir1 | int bfd, dfd, bus, address;
|
398 | 68063649 | blueswir1 | char busbuf[20], devbuf[20], product_name[256]; |
399 | 68063649 | blueswir1 | int ret = 0; |
400 | 68063649 | blueswir1 | |
401 | 68063649 | blueswir1 | for (bus = 0; bus < 10; bus++) { |
402 | 68063649 | blueswir1 | |
403 | 68063649 | blueswir1 | snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus); |
404 | 68063649 | blueswir1 | bfd = open(busbuf, O_RDWR); |
405 | 68063649 | blueswir1 | if (bfd < 0) |
406 | 68063649 | blueswir1 | continue;
|
407 | 68063649 | blueswir1 | |
408 | 68063649 | blueswir1 | for (address = 1; address < 127; address++) { |
409 | 68063649 | blueswir1 | |
410 | 68063649 | blueswir1 | bus_info.udi_addr = address; |
411 | 68063649 | blueswir1 | if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) |
412 | 68063649 | blueswir1 | continue;
|
413 | 68063649 | blueswir1 | |
414 | 68063649 | blueswir1 | /* only list devices that can be used by generic layer */
|
415 | 68063649 | blueswir1 | if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0) |
416 | 68063649 | blueswir1 | continue;
|
417 | 68063649 | blueswir1 | |
418 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
419 | 68063649 | blueswir1 | snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]); |
420 | 68063649 | blueswir1 | #else
|
421 | 68063649 | blueswir1 | snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]); |
422 | 68063649 | blueswir1 | #endif
|
423 | 68063649 | blueswir1 | |
424 | 68063649 | blueswir1 | dfd = open(devbuf, O_RDONLY); |
425 | 68063649 | blueswir1 | if (dfd < 0) { |
426 | 68063649 | blueswir1 | #ifdef DEBUG
|
427 | 68063649 | blueswir1 | printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
|
428 | 68063649 | blueswir1 | strerror(errno)); |
429 | 68063649 | blueswir1 | #endif
|
430 | 68063649 | blueswir1 | continue;
|
431 | 68063649 | blueswir1 | } |
432 | 68063649 | blueswir1 | |
433 | 68063649 | blueswir1 | if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) |
434 | 68063649 | blueswir1 | printf("usb_host_scan: couldn't get device information for %s - %s\n",
|
435 | 68063649 | blueswir1 | devbuf, strerror(errno)); |
436 | 68063649 | blueswir1 | |
437 | 68063649 | blueswir1 | // XXX: might need to fixup endianess of word values before copying over
|
438 | 68063649 | blueswir1 | |
439 | 68063649 | blueswir1 | vendor_id = dev_info.udi_vendorNo; |
440 | 68063649 | blueswir1 | product_id = dev_info.udi_productNo; |
441 | 68063649 | blueswir1 | class_id = dev_info.udi_class; |
442 | 68063649 | blueswir1 | speed = dev_info.udi_speed; |
443 | 68063649 | blueswir1 | |
444 | 68063649 | blueswir1 | if (strncmp(dev_info.udi_product, "product", 7) != 0) |
445 | 68063649 | blueswir1 | pstrcpy(product_name, sizeof(product_name),
|
446 | 68063649 | blueswir1 | dev_info.udi_product); |
447 | 68063649 | blueswir1 | else
|
448 | 68063649 | blueswir1 | product_name[0] = '\0'; |
449 | 68063649 | blueswir1 | |
450 | 68063649 | blueswir1 | ret = func(opaque, bus, address, class_id, vendor_id, |
451 | 68063649 | blueswir1 | product_id, product_name, speed); |
452 | 68063649 | blueswir1 | |
453 | 68063649 | blueswir1 | close(dfd); |
454 | 68063649 | blueswir1 | |
455 | 68063649 | blueswir1 | if (ret)
|
456 | 68063649 | blueswir1 | goto the_end;
|
457 | 68063649 | blueswir1 | } |
458 | 68063649 | blueswir1 | |
459 | 68063649 | blueswir1 | close(bfd); |
460 | 68063649 | blueswir1 | } |
461 | 68063649 | blueswir1 | |
462 | 68063649 | blueswir1 | the_end:
|
463 | 68063649 | blueswir1 | return ret;
|
464 | 68063649 | blueswir1 | } |
465 | 68063649 | blueswir1 | |
466 | 68063649 | blueswir1 | typedef struct FindDeviceState { |
467 | 68063649 | blueswir1 | int vendor_id;
|
468 | 68063649 | blueswir1 | int product_id;
|
469 | 68063649 | blueswir1 | int bus_num;
|
470 | 68063649 | blueswir1 | int addr;
|
471 | 68063649 | blueswir1 | } FindDeviceState; |
472 | 68063649 | blueswir1 | |
473 | 68063649 | blueswir1 | static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, |
474 | 68063649 | blueswir1 | int class_id,
|
475 | 68063649 | blueswir1 | int vendor_id, int product_id, |
476 | 68063649 | blueswir1 | const char *product_name, int speed) |
477 | 68063649 | blueswir1 | { |
478 | 68063649 | blueswir1 | FindDeviceState *s = opaque; |
479 | 68063649 | blueswir1 | if (vendor_id == s->vendor_id &&
|
480 | 68063649 | blueswir1 | product_id == s->product_id) { |
481 | 68063649 | blueswir1 | s->bus_num = bus_num; |
482 | 68063649 | blueswir1 | s->addr = addr; |
483 | 68063649 | blueswir1 | return 1; |
484 | 68063649 | blueswir1 | } else {
|
485 | 68063649 | blueswir1 | return 0; |
486 | 68063649 | blueswir1 | } |
487 | 68063649 | blueswir1 | } |
488 | 68063649 | blueswir1 | |
489 | 68063649 | blueswir1 | |
490 | 68063649 | blueswir1 | /* the syntax is :
|
491 | 68063649 | blueswir1 | 'bus.addr' (decimal numbers) or
|
492 | 68063649 | blueswir1 | 'vendor_id:product_id' (hexa numbers) */
|
493 | 68063649 | blueswir1 | static int usb_host_find_device(int *pbus_num, int *paddr, |
494 | 68063649 | blueswir1 | const char *devname) |
495 | 68063649 | blueswir1 | { |
496 | 68063649 | blueswir1 | const char *p; |
497 | 68063649 | blueswir1 | int ret;
|
498 | 68063649 | blueswir1 | FindDeviceState fs; |
499 | 68063649 | blueswir1 | |
500 | 68063649 | blueswir1 | p = strchr(devname, '.');
|
501 | 68063649 | blueswir1 | if (p) {
|
502 | 68063649 | blueswir1 | *pbus_num = strtoul(devname, NULL, 0); |
503 | 68063649 | blueswir1 | *paddr = strtoul(p + 1, NULL, 0); |
504 | 68063649 | blueswir1 | return 0; |
505 | 68063649 | blueswir1 | } |
506 | 68063649 | blueswir1 | p = strchr(devname, ':');
|
507 | 68063649 | blueswir1 | if (p) {
|
508 | 68063649 | blueswir1 | fs.vendor_id = strtoul(devname, NULL, 16); |
509 | 68063649 | blueswir1 | fs.product_id = strtoul(p + 1, NULL, 16); |
510 | 68063649 | blueswir1 | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
511 | 68063649 | blueswir1 | if (ret) {
|
512 | 68063649 | blueswir1 | *pbus_num = fs.bus_num; |
513 | 68063649 | blueswir1 | *paddr = fs.addr; |
514 | 68063649 | blueswir1 | return 0; |
515 | 68063649 | blueswir1 | } |
516 | 68063649 | blueswir1 | } |
517 | 68063649 | blueswir1 | return -1; |
518 | 68063649 | blueswir1 | } |
519 | 68063649 | blueswir1 | |
520 | 68063649 | blueswir1 | /**********************/
|
521 | 68063649 | blueswir1 | /* USB host device info */
|
522 | 68063649 | blueswir1 | |
523 | 68063649 | blueswir1 | struct usb_class_info {
|
524 | 68063649 | blueswir1 | int class;
|
525 | 68063649 | blueswir1 | const char *class_name; |
526 | 68063649 | blueswir1 | }; |
527 | 68063649 | blueswir1 | |
528 | 68063649 | blueswir1 | static const struct usb_class_info usb_class_info[] = { |
529 | 68063649 | blueswir1 | { USB_CLASS_AUDIO, "Audio"},
|
530 | 68063649 | blueswir1 | { USB_CLASS_COMM, "Communication"},
|
531 | 68063649 | blueswir1 | { USB_CLASS_HID, "HID"},
|
532 | 68063649 | blueswir1 | { USB_CLASS_HUB, "Hub" },
|
533 | 68063649 | blueswir1 | { USB_CLASS_PHYSICAL, "Physical" },
|
534 | 68063649 | blueswir1 | { USB_CLASS_PRINTER, "Printer" },
|
535 | 68063649 | blueswir1 | { USB_CLASS_MASS_STORAGE, "Storage" },
|
536 | 68063649 | blueswir1 | { USB_CLASS_CDC_DATA, "Data" },
|
537 | 68063649 | blueswir1 | { USB_CLASS_APP_SPEC, "Application Specific" },
|
538 | 68063649 | blueswir1 | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
|
539 | 68063649 | blueswir1 | { USB_CLASS_STILL_IMAGE, "Still Image" },
|
540 | 68063649 | blueswir1 | { USB_CLASS_CSCID, "Smart Card" },
|
541 | 68063649 | blueswir1 | { USB_CLASS_CONTENT_SEC, "Content Security" },
|
542 | 68063649 | blueswir1 | { -1, NULL } |
543 | 68063649 | blueswir1 | }; |
544 | 68063649 | blueswir1 | |
545 | 68063649 | blueswir1 | static const char *usb_class_str(uint8_t class) |
546 | 68063649 | blueswir1 | { |
547 | 68063649 | blueswir1 | const struct usb_class_info *p; |
548 | 68063649 | blueswir1 | for (p = usb_class_info; p->class != -1; p++) { |
549 | 68063649 | blueswir1 | if (p->class == class)
|
550 | 68063649 | blueswir1 | break;
|
551 | 68063649 | blueswir1 | } |
552 | 68063649 | blueswir1 | return p->class_name;
|
553 | 68063649 | blueswir1 | } |
554 | 68063649 | blueswir1 | |
555 | 179da8af | Blue Swirl | static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id, |
556 | 00766a4e | blueswir1 | int vendor_id, int product_id, |
557 | 00766a4e | blueswir1 | const char *product_name, |
558 | 00766a4e | blueswir1 | int speed)
|
559 | 68063649 | blueswir1 | { |
560 | 68063649 | blueswir1 | const char *class_str, *speed_str; |
561 | 68063649 | blueswir1 | |
562 | 68063649 | blueswir1 | switch(speed) {
|
563 | 68063649 | blueswir1 | case USB_SPEED_LOW:
|
564 | 68063649 | blueswir1 | speed_str = "1.5";
|
565 | 68063649 | blueswir1 | break;
|
566 | 68063649 | blueswir1 | case USB_SPEED_FULL:
|
567 | 68063649 | blueswir1 | speed_str = "12";
|
568 | 68063649 | blueswir1 | break;
|
569 | 68063649 | blueswir1 | case USB_SPEED_HIGH:
|
570 | 68063649 | blueswir1 | speed_str = "480";
|
571 | 68063649 | blueswir1 | break;
|
572 | 68063649 | blueswir1 | default:
|
573 | 68063649 | blueswir1 | speed_str = "?";
|
574 | 68063649 | blueswir1 | break;
|
575 | 68063649 | blueswir1 | } |
576 | 68063649 | blueswir1 | |
577 | 376253ec | aliguori | monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
|
578 | 376253ec | aliguori | bus_num, addr, speed_str); |
579 | 68063649 | blueswir1 | class_str = usb_class_str(class_id); |
580 | 68063649 | blueswir1 | if (class_str)
|
581 | 376253ec | aliguori | monitor_printf(mon, " %s:", class_str);
|
582 | 68063649 | blueswir1 | else
|
583 | 376253ec | aliguori | monitor_printf(mon, " Class %02x:", class_id);
|
584 | 376253ec | aliguori | monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
|
585 | 68063649 | blueswir1 | if (product_name[0] != '\0') |
586 | 376253ec | aliguori | monitor_printf(mon, ", %s", product_name);
|
587 | 376253ec | aliguori | monitor_printf(mon, "\n");
|
588 | 68063649 | blueswir1 | } |
589 | 68063649 | blueswir1 | |
590 | 376253ec | aliguori | static int usb_host_info_device(void *opaque, |
591 | 376253ec | aliguori | int bus_num, int addr, |
592 | 68063649 | blueswir1 | int class_id,
|
593 | 68063649 | blueswir1 | int vendor_id, int product_id, |
594 | 68063649 | blueswir1 | const char *product_name, |
595 | 68063649 | blueswir1 | int speed)
|
596 | 68063649 | blueswir1 | { |
597 | 179da8af | Blue Swirl | Monitor *mon = opaque; |
598 | 179da8af | Blue Swirl | |
599 | 179da8af | Blue Swirl | usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id, |
600 | 68063649 | blueswir1 | product_name, speed); |
601 | 68063649 | blueswir1 | return 0; |
602 | 68063649 | blueswir1 | } |
603 | 68063649 | blueswir1 | |
604 | 376253ec | aliguori | void usb_host_info(Monitor *mon)
|
605 | 68063649 | blueswir1 | { |
606 | 179da8af | Blue Swirl | usb_host_scan(mon, usb_host_info_device); |
607 | 68063649 | blueswir1 | } |
608 | 68063649 | blueswir1 | |
609 | 68063649 | blueswir1 | /* XXX add this */
|
610 | 68063649 | blueswir1 | int usb_host_device_close(const char *devname) |
611 | 68063649 | blueswir1 | { |
612 | 68063649 | blueswir1 | return 0; |
613 | 68063649 | blueswir1 | } |