root / usb-bsd.c @ 50132156
History | View | Annotate | Download (17.3 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 | 620150dc | Blue Swirl | #if 0
|
67 | 68063649 | blueswir1 | static int ensure_ep_open(USBHostDevice *dev, int ep, int mode)
|
68 | 68063649 | blueswir1 | {
|
69 | 68063649 | blueswir1 | char buf[32];
|
70 | 68063649 | blueswir1 | int fd;
|
71 | 68063649 | blueswir1 | |
72 | 68063649 | blueswir1 | /* Get the address for this endpoint */
|
73 | 68063649 | blueswir1 | ep = UE_GET_ADDR(ep);
|
74 | 68063649 | blueswir1 | |
75 | 68063649 | blueswir1 | if (dev->ep_fd[ep] < 0) {
|
76 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
77 | 68063649 | blueswir1 | snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->devpath, ep);
|
78 | 68063649 | blueswir1 | #else
|
79 | 68063649 | blueswir1 | snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->devpath, ep); |
80 | 68063649 | blueswir1 | #endif
|
81 | 68063649 | blueswir1 | /* Try to open it O_RDWR first for those devices which have in and out
|
82 | 68063649 | blueswir1 | * endpoints with the same address (eg 0x02 and 0x82)
|
83 | 68063649 | blueswir1 | */
|
84 | 68063649 | blueswir1 | fd = open(buf, O_RDWR); |
85 | 68063649 | blueswir1 | if (fd < 0 && errno == ENXIO) |
86 | 68063649 | blueswir1 | fd = open(buf, mode); |
87 | 68063649 | blueswir1 | if (fd < 0) { |
88 | 68063649 | blueswir1 | #ifdef DEBUG
|
89 | 68063649 | blueswir1 | printf("ensure_ep_open: failed to open device endpoint %s: %s\n",
|
90 | 68063649 | blueswir1 | buf, strerror(errno)); |
91 | 68063649 | blueswir1 | #endif
|
92 | 68063649 | blueswir1 | } |
93 | 68063649 | blueswir1 | dev->ep_fd[ep] = fd; |
94 | 68063649 | blueswir1 | } |
95 | 68063649 | blueswir1 | |
96 | 68063649 | blueswir1 | return dev->ep_fd[ep];
|
97 | 68063649 | blueswir1 | } |
98 | 68063649 | blueswir1 | |
99 | 68063649 | blueswir1 | static void ensure_eps_closed(USBHostDevice *dev) |
100 | 68063649 | blueswir1 | { |
101 | 68063649 | blueswir1 | int epnum = 1; |
102 | 68063649 | blueswir1 | |
103 | 68063649 | blueswir1 | if (!dev)
|
104 | 68063649 | blueswir1 | return;
|
105 | 68063649 | blueswir1 | |
106 | 68063649 | blueswir1 | while (epnum < USB_MAX_ENDPOINTS) {
|
107 | 68063649 | blueswir1 | if (dev->ep_fd[epnum] >= 0) { |
108 | 68063649 | blueswir1 | close(dev->ep_fd[epnum]); |
109 | 68063649 | blueswir1 | dev->ep_fd[epnum] = -1;
|
110 | 68063649 | blueswir1 | } |
111 | 68063649 | blueswir1 | epnum++; |
112 | 68063649 | blueswir1 | } |
113 | 68063649 | blueswir1 | } |
114 | 620150dc | Blue Swirl | #endif
|
115 | 68063649 | blueswir1 | |
116 | 68063649 | blueswir1 | static void usb_host_handle_reset(USBDevice *dev) |
117 | 68063649 | blueswir1 | { |
118 | 68063649 | blueswir1 | #if 0
|
119 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)dev;
|
120 | 68063649 | blueswir1 | #endif
|
121 | 68063649 | blueswir1 | } |
122 | 68063649 | blueswir1 | |
123 | 620150dc | Blue Swirl | #if 0
|
124 | 68063649 | blueswir1 | /* XXX:
|
125 | 68063649 | blueswir1 | * -check device states against transfer requests
|
126 | 68063649 | blueswir1 | * and return appropriate response
|
127 | 68063649 | blueswir1 | */
|
128 | 68063649 | blueswir1 | static int usb_host_handle_control(USBDevice *dev,
|
129 | 68063649 | blueswir1 | int request,
|
130 | 68063649 | blueswir1 | int value,
|
131 | 68063649 | blueswir1 | int index,
|
132 | 68063649 | blueswir1 | int length,
|
133 | 68063649 | blueswir1 | uint8_t *data)
|
134 | 68063649 | blueswir1 | {
|
135 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)dev;
|
136 | 68063649 | blueswir1 | struct usb_ctl_request req;
|
137 | 68063649 | blueswir1 | struct usb_alt_interface aiface;
|
138 | 68063649 | blueswir1 | int ret, timeout = 50;
|
139 | 68063649 | blueswir1 | |
140 | 68063649 | blueswir1 | if ((request >> 8) == UT_WRITE_DEVICE &&
|
141 | 68063649 | blueswir1 | (request & 0xff) == UR_SET_ADDRESS) {
|
142 | 68063649 | blueswir1 | |
143 | 68063649 | blueswir1 | /* specific SET_ADDRESS support */
|
144 | 68063649 | blueswir1 | dev->addr = value;
|
145 | 68063649 | blueswir1 | return 0;
|
146 | 68063649 | blueswir1 | } else if ((request >> 8) == UT_WRITE_DEVICE &&
|
147 | 68063649 | blueswir1 | (request & 0xff) == UR_SET_CONFIG) {
|
148 | 68063649 | blueswir1 | |
149 | 68063649 | blueswir1 | ensure_eps_closed(s); /* can't do this without all eps closed */
|
150 | 68063649 | blueswir1 | |
151 | 68063649 | blueswir1 | ret = ioctl(s->devfd, USB_SET_CONFIG, &value);
|
152 | 68063649 | blueswir1 | if (ret < 0) {
|
153 | 68063649 | blueswir1 | #ifdef DEBUG
|
154 | 68063649 | blueswir1 | printf("handle_control: failed to set configuration - %s\n",
|
155 | 68063649 | blueswir1 | strerror(errno));
|
156 | 68063649 | blueswir1 | #endif
|
157 | 68063649 | blueswir1 | return USB_RET_STALL;
|
158 | 68063649 | blueswir1 | } |
159 | 68063649 | blueswir1 | |
160 | 68063649 | blueswir1 | return 0; |
161 | 68063649 | blueswir1 | } else if ((request >> 8) == UT_WRITE_INTERFACE && |
162 | 68063649 | blueswir1 | (request & 0xff) == UR_SET_INTERFACE) {
|
163 | 68063649 | blueswir1 | |
164 | 68063649 | blueswir1 | aiface.uai_interface_index = index; |
165 | 68063649 | blueswir1 | aiface.uai_alt_no = value; |
166 | 68063649 | blueswir1 | |
167 | 68063649 | blueswir1 | ensure_eps_closed(s); /* can't do this without all eps closed */
|
168 | 68063649 | blueswir1 | ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface); |
169 | 68063649 | blueswir1 | if (ret < 0) { |
170 | 68063649 | blueswir1 | #ifdef DEBUG
|
171 | 68063649 | blueswir1 | printf("handle_control: failed to set alternate interface - %s\n",
|
172 | 68063649 | blueswir1 | strerror(errno)); |
173 | 68063649 | blueswir1 | #endif
|
174 | 68063649 | blueswir1 | return USB_RET_STALL;
|
175 | 68063649 | blueswir1 | } |
176 | 68063649 | blueswir1 | |
177 | 68063649 | blueswir1 | return 0; |
178 | 68063649 | blueswir1 | } else {
|
179 | 68063649 | blueswir1 | req.ucr_request.bmRequestType = request >> 8;
|
180 | 68063649 | blueswir1 | req.ucr_request.bRequest = request & 0xff;
|
181 | 68063649 | blueswir1 | USETW(req.ucr_request.wValue, value); |
182 | 68063649 | blueswir1 | USETW(req.ucr_request.wIndex, index); |
183 | 68063649 | blueswir1 | USETW(req.ucr_request.wLength, length); |
184 | 68063649 | blueswir1 | req.ucr_data = data; |
185 | 68063649 | blueswir1 | req.ucr_flags = USBD_SHORT_XFER_OK; |
186 | 68063649 | blueswir1 | |
187 | 68063649 | blueswir1 | ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout); |
188 | 00766a4e | blueswir1 | #if defined(__NetBSD__) || defined(__OpenBSD__)
|
189 | 68063649 | blueswir1 | if (ret < 0 && errno != EINVAL) { |
190 | 68063649 | blueswir1 | #else
|
191 | 68063649 | blueswir1 | if (ret < 0) { |
192 | 68063649 | blueswir1 | #endif
|
193 | 68063649 | blueswir1 | #ifdef DEBUG
|
194 | 68063649 | blueswir1 | printf("handle_control: setting timeout failed - %s\n",
|
195 | 68063649 | blueswir1 | strerror(errno)); |
196 | 68063649 | blueswir1 | #endif
|
197 | 68063649 | blueswir1 | } |
198 | 68063649 | blueswir1 | |
199 | 68063649 | blueswir1 | ret = ioctl(s->devfd, USB_DO_REQUEST, &req); |
200 | 68063649 | blueswir1 | /* ugen returns EIO for usbd_do_request_ no matter what
|
201 | 68063649 | blueswir1 | * happens with the transfer */
|
202 | 68063649 | blueswir1 | if (ret < 0) { |
203 | 68063649 | blueswir1 | #ifdef DEBUG
|
204 | 68063649 | blueswir1 | printf("handle_control: error after request - %s\n",
|
205 | 68063649 | blueswir1 | strerror(errno)); |
206 | 68063649 | blueswir1 | #endif
|
207 | 68063649 | blueswir1 | return USB_RET_NAK; // STALL |
208 | 68063649 | blueswir1 | } else {
|
209 | 68063649 | blueswir1 | return req.ucr_actlen;
|
210 | 68063649 | blueswir1 | } |
211 | 68063649 | blueswir1 | } |
212 | 68063649 | blueswir1 | } |
213 | 68063649 | blueswir1 | |
214 | 68063649 | blueswir1 | static int usb_host_handle_data(USBDevice *dev, USBPacket *p) |
215 | 68063649 | blueswir1 | { |
216 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)dev; |
217 | 68063649 | blueswir1 | int ret, fd, mode;
|
218 | 68063649 | blueswir1 | int one = 1, shortpacket = 0, timeout = 50; |
219 | 68063649 | blueswir1 | sigset_t new_mask, old_mask; |
220 | 68063649 | blueswir1 | uint8_t devep = p->devep; |
221 | 68063649 | blueswir1 | |
222 | 68063649 | blueswir1 | /* protect data transfers from SIGALRM signal */
|
223 | 68063649 | blueswir1 | sigemptyset(&new_mask); |
224 | 68063649 | blueswir1 | sigaddset(&new_mask, SIGALRM); |
225 | 68063649 | blueswir1 | sigprocmask(SIG_BLOCK, &new_mask, &old_mask); |
226 | 68063649 | blueswir1 | |
227 | 68063649 | blueswir1 | if (p->pid == USB_TOKEN_IN) {
|
228 | 68063649 | blueswir1 | devep |= 0x80;
|
229 | 68063649 | blueswir1 | mode = O_RDONLY; |
230 | 68063649 | blueswir1 | shortpacket = 1;
|
231 | 68063649 | blueswir1 | } else {
|
232 | 68063649 | blueswir1 | mode = O_WRONLY; |
233 | 68063649 | blueswir1 | } |
234 | 68063649 | blueswir1 | |
235 | 68063649 | blueswir1 | fd = ensure_ep_open(s, devep, mode); |
236 | 68063649 | blueswir1 | if (fd < 0) { |
237 | 68063649 | blueswir1 | sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
238 | 68063649 | blueswir1 | return USB_RET_NODEV;
|
239 | 68063649 | blueswir1 | } |
240 | 68063649 | blueswir1 | |
241 | 68063649 | blueswir1 | if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) { |
242 | 68063649 | blueswir1 | #ifdef DEBUG
|
243 | 68063649 | blueswir1 | printf("handle_data: failed to set timeout - %s\n",
|
244 | 68063649 | blueswir1 | strerror(errno)); |
245 | 68063649 | blueswir1 | #endif
|
246 | 68063649 | blueswir1 | } |
247 | 68063649 | blueswir1 | |
248 | 68063649 | blueswir1 | if (shortpacket) {
|
249 | 68063649 | blueswir1 | if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) { |
250 | 68063649 | blueswir1 | #ifdef DEBUG
|
251 | 68063649 | blueswir1 | printf("handle_data: failed to set short xfer mode - %s\n",
|
252 | 68063649 | blueswir1 | strerror(errno)); |
253 | 68063649 | blueswir1 | #endif
|
254 | 68063649 | blueswir1 | sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
255 | 68063649 | blueswir1 | } |
256 | 68063649 | blueswir1 | } |
257 | 68063649 | blueswir1 | |
258 | 68063649 | blueswir1 | if (p->pid == USB_TOKEN_IN)
|
259 | 68063649 | blueswir1 | ret = read(fd, p->data, p->len); |
260 | 68063649 | blueswir1 | else
|
261 | 68063649 | blueswir1 | ret = write(fd, p->data, p->len); |
262 | 68063649 | blueswir1 | |
263 | 68063649 | blueswir1 | sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
264 | 68063649 | blueswir1 | |
265 | 68063649 | blueswir1 | if (ret < 0) { |
266 | 68063649 | blueswir1 | #ifdef DEBUG
|
267 | 68063649 | blueswir1 | printf("handle_data: error after %s data - %s\n",
|
268 | 68063649 | blueswir1 | pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno)); |
269 | 68063649 | blueswir1 | #endif
|
270 | 68063649 | blueswir1 | switch(errno) {
|
271 | 68063649 | blueswir1 | case ETIMEDOUT:
|
272 | 68063649 | blueswir1 | case EINTR:
|
273 | 68063649 | blueswir1 | return USB_RET_NAK;
|
274 | 68063649 | blueswir1 | default:
|
275 | 68063649 | blueswir1 | return USB_RET_STALL;
|
276 | 68063649 | blueswir1 | } |
277 | 68063649 | blueswir1 | } else {
|
278 | 68063649 | blueswir1 | return ret;
|
279 | 68063649 | blueswir1 | } |
280 | 68063649 | blueswir1 | } |
281 | 620150dc | Blue Swirl | #endif
|
282 | 68063649 | blueswir1 | |
283 | 68063649 | blueswir1 | static void usb_host_handle_destroy(USBDevice *opaque) |
284 | 68063649 | blueswir1 | { |
285 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)opaque; |
286 | 68063649 | blueswir1 | int i;
|
287 | 68063649 | blueswir1 | |
288 | 68063649 | blueswir1 | for (i = 0; i < USB_MAX_ENDPOINTS; i++) |
289 | 68063649 | blueswir1 | if (s->ep_fd[i] >= 0) |
290 | 68063649 | blueswir1 | close(s->ep_fd[i]); |
291 | 68063649 | blueswir1 | |
292 | 68063649 | blueswir1 | if (s->devfd < 0) |
293 | 68063649 | blueswir1 | return;
|
294 | 68063649 | blueswir1 | |
295 | 68063649 | blueswir1 | close(s->devfd); |
296 | 68063649 | blueswir1 | |
297 | 68063649 | blueswir1 | qemu_free(s); |
298 | 68063649 | blueswir1 | } |
299 | 68063649 | blueswir1 | |
300 | 620150dc | Blue Swirl | static int usb_host_initfn(USBDevice *dev) |
301 | 620150dc | Blue Swirl | { |
302 | 620150dc | Blue Swirl | return 0; |
303 | 620150dc | Blue Swirl | } |
304 | 620150dc | Blue Swirl | |
305 | 68063649 | blueswir1 | USBDevice *usb_host_device_open(const char *devname) |
306 | 68063649 | blueswir1 | { |
307 | 68063649 | blueswir1 | struct usb_device_info bus_info, dev_info;
|
308 | 620150dc | Blue Swirl | USBDevice *d = NULL;
|
309 | 68063649 | blueswir1 | USBHostDevice *dev; |
310 | 68063649 | blueswir1 | char ctlpath[PATH_MAX + 1]; |
311 | 68063649 | blueswir1 | char buspath[PATH_MAX + 1]; |
312 | 68063649 | blueswir1 | int bfd, dfd, bus, address, i;
|
313 | 68063649 | blueswir1 | int ugendebug = UGEN_DEBUG_LEVEL;
|
314 | 68063649 | blueswir1 | |
315 | 68063649 | blueswir1 | if (usb_host_find_device(&bus, &address, devname) < 0) |
316 | 68063649 | blueswir1 | return NULL; |
317 | 68063649 | blueswir1 | |
318 | 68063649 | blueswir1 | snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
|
319 | 68063649 | blueswir1 | |
320 | 68063649 | blueswir1 | bfd = open(buspath, O_RDWR); |
321 | 68063649 | blueswir1 | if (bfd < 0) { |
322 | 68063649 | blueswir1 | #ifdef DEBUG
|
323 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to open usb bus - %s\n",
|
324 | 68063649 | blueswir1 | strerror(errno)); |
325 | 68063649 | blueswir1 | #endif
|
326 | 68063649 | blueswir1 | return NULL; |
327 | 68063649 | blueswir1 | } |
328 | 68063649 | blueswir1 | |
329 | 68063649 | blueswir1 | bus_info.udi_addr = address; |
330 | 68063649 | blueswir1 | if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) { |
331 | 68063649 | blueswir1 | #ifdef DEBUG
|
332 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to grab bus information - %s\n",
|
333 | 68063649 | blueswir1 | strerror(errno)); |
334 | 68063649 | blueswir1 | #endif
|
335 | 68063649 | blueswir1 | return NULL; |
336 | 68063649 | blueswir1 | } |
337 | 68063649 | blueswir1 | |
338 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
339 | 68063649 | blueswir1 | snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]); |
340 | 68063649 | blueswir1 | #else
|
341 | 68063649 | blueswir1 | snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]); |
342 | 68063649 | blueswir1 | #endif
|
343 | 68063649 | blueswir1 | |
344 | 68063649 | blueswir1 | dfd = open(ctlpath, O_RDWR); |
345 | 68063649 | blueswir1 | if (dfd < 0) { |
346 | 68063649 | blueswir1 | dfd = open(ctlpath, O_RDONLY); |
347 | 68063649 | blueswir1 | if (dfd < 0) { |
348 | 68063649 | blueswir1 | #ifdef DEBUG
|
349 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to open usb device %s - %s\n",
|
350 | 68063649 | blueswir1 | ctlpath, strerror(errno)); |
351 | 68063649 | blueswir1 | #endif
|
352 | 68063649 | blueswir1 | } |
353 | 68063649 | blueswir1 | } |
354 | 68063649 | blueswir1 | |
355 | 68063649 | blueswir1 | if (dfd >= 0) { |
356 | 68063649 | blueswir1 | if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) { |
357 | 68063649 | blueswir1 | #ifdef DEBUG
|
358 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to grab device info - %s\n",
|
359 | 68063649 | blueswir1 | strerror(errno)); |
360 | 68063649 | blueswir1 | #endif
|
361 | 68063649 | blueswir1 | goto fail;
|
362 | 68063649 | blueswir1 | } |
363 | 68063649 | blueswir1 | |
364 | 620150dc | Blue Swirl | d = usb_create(NULL /* FIXME */, "USB Host Device"); |
365 | 620150dc | Blue Swirl | dev = DO_UPCAST(USBHostDevice, dev, d); |
366 | 620150dc | Blue Swirl | |
367 | 68063649 | blueswir1 | if (dev_info.udi_speed == 1) |
368 | 68063649 | blueswir1 | dev->dev.speed = USB_SPEED_LOW - 1;
|
369 | 68063649 | blueswir1 | else
|
370 | 68063649 | blueswir1 | dev->dev.speed = USB_SPEED_FULL - 1;
|
371 | 68063649 | blueswir1 | |
372 | 68063649 | blueswir1 | if (strncmp(dev_info.udi_product, "product", 7) != 0) |
373 | 68063649 | blueswir1 | pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
|
374 | 68063649 | blueswir1 | dev_info.udi_product); |
375 | 68063649 | blueswir1 | else
|
376 | 68063649 | blueswir1 | snprintf(dev->dev.devname, sizeof(dev->dev.devname),
|
377 | 68063649 | blueswir1 | "host:%s", devname);
|
378 | 68063649 | blueswir1 | |
379 | 68063649 | blueswir1 | pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/"); |
380 | 00766a4e | blueswir1 | pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]); |
381 | 68063649 | blueswir1 | |
382 | 68063649 | blueswir1 | /* Mark the endpoints as not yet open */
|
383 | 68063649 | blueswir1 | for (i = 0; i < USB_MAX_ENDPOINTS; i++) |
384 | 68063649 | blueswir1 | dev->ep_fd[i] = -1;
|
385 | 68063649 | blueswir1 | |
386 | 68063649 | blueswir1 | ioctl(dfd, USB_SETDEBUG, &ugendebug); |
387 | 68063649 | blueswir1 | |
388 | 68063649 | blueswir1 | return (USBDevice *)dev;
|
389 | 68063649 | blueswir1 | } |
390 | 68063649 | blueswir1 | |
391 | 68063649 | blueswir1 | fail:
|
392 | 68063649 | blueswir1 | return NULL; |
393 | 68063649 | blueswir1 | } |
394 | 68063649 | blueswir1 | |
395 | 620150dc | Blue Swirl | static struct USBDeviceInfo usb_host_dev_info = { |
396 | 620150dc | Blue Swirl | .qdev.name = "USB Host Device",
|
397 | 620150dc | Blue Swirl | .qdev.size = sizeof(USBHostDevice),
|
398 | 620150dc | Blue Swirl | .init = usb_host_initfn, |
399 | 620150dc | Blue Swirl | .handle_packet = usb_generic_handle_packet, |
400 | 620150dc | Blue Swirl | .handle_reset = usb_host_handle_reset, |
401 | 620150dc | Blue Swirl | #if 0
|
402 | 620150dc | Blue Swirl | .handle_control = usb_host_handle_control,
|
403 | 620150dc | Blue Swirl | .handle_data = usb_host_handle_data,
|
404 | 620150dc | Blue Swirl | #endif
|
405 | 620150dc | Blue Swirl | .handle_destroy = usb_host_handle_destroy, |
406 | 620150dc | Blue Swirl | }; |
407 | 620150dc | Blue Swirl | |
408 | 620150dc | Blue Swirl | static void usb_host_register_devices(void) |
409 | 620150dc | Blue Swirl | { |
410 | 620150dc | Blue Swirl | usb_qdev_register(&usb_host_dev_info); |
411 | 620150dc | Blue Swirl | } |
412 | 620150dc | Blue Swirl | device_init(usb_host_register_devices) |
413 | 620150dc | Blue Swirl | |
414 | 68063649 | blueswir1 | static int usb_host_scan(void *opaque, USBScanFunc *func) |
415 | 68063649 | blueswir1 | { |
416 | 68063649 | blueswir1 | struct usb_device_info bus_info;
|
417 | 68063649 | blueswir1 | struct usb_device_info dev_info;
|
418 | 68063649 | blueswir1 | uint16_t vendor_id, product_id, class_id, speed; |
419 | 68063649 | blueswir1 | int bfd, dfd, bus, address;
|
420 | 68063649 | blueswir1 | char busbuf[20], devbuf[20], product_name[256]; |
421 | 68063649 | blueswir1 | int ret = 0; |
422 | 68063649 | blueswir1 | |
423 | 68063649 | blueswir1 | for (bus = 0; bus < 10; bus++) { |
424 | 68063649 | blueswir1 | |
425 | 68063649 | blueswir1 | snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus); |
426 | 68063649 | blueswir1 | bfd = open(busbuf, O_RDWR); |
427 | 68063649 | blueswir1 | if (bfd < 0) |
428 | 68063649 | blueswir1 | continue;
|
429 | 68063649 | blueswir1 | |
430 | 68063649 | blueswir1 | for (address = 1; address < 127; address++) { |
431 | 68063649 | blueswir1 | |
432 | 68063649 | blueswir1 | bus_info.udi_addr = address; |
433 | 68063649 | blueswir1 | if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) |
434 | 68063649 | blueswir1 | continue;
|
435 | 68063649 | blueswir1 | |
436 | 68063649 | blueswir1 | /* only list devices that can be used by generic layer */
|
437 | 68063649 | blueswir1 | if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0) |
438 | 68063649 | blueswir1 | continue;
|
439 | 68063649 | blueswir1 | |
440 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
441 | 68063649 | blueswir1 | snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]); |
442 | 68063649 | blueswir1 | #else
|
443 | 68063649 | blueswir1 | snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]); |
444 | 68063649 | blueswir1 | #endif
|
445 | 68063649 | blueswir1 | |
446 | 68063649 | blueswir1 | dfd = open(devbuf, O_RDONLY); |
447 | 68063649 | blueswir1 | if (dfd < 0) { |
448 | 68063649 | blueswir1 | #ifdef DEBUG
|
449 | 68063649 | blueswir1 | printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
|
450 | 68063649 | blueswir1 | strerror(errno)); |
451 | 68063649 | blueswir1 | #endif
|
452 | 68063649 | blueswir1 | continue;
|
453 | 68063649 | blueswir1 | } |
454 | 68063649 | blueswir1 | |
455 | 68063649 | blueswir1 | if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) |
456 | 68063649 | blueswir1 | printf("usb_host_scan: couldn't get device information for %s - %s\n",
|
457 | 68063649 | blueswir1 | devbuf, strerror(errno)); |
458 | 68063649 | blueswir1 | |
459 | 68063649 | blueswir1 | // XXX: might need to fixup endianess of word values before copying over
|
460 | 68063649 | blueswir1 | |
461 | 68063649 | blueswir1 | vendor_id = dev_info.udi_vendorNo; |
462 | 68063649 | blueswir1 | product_id = dev_info.udi_productNo; |
463 | 68063649 | blueswir1 | class_id = dev_info.udi_class; |
464 | 68063649 | blueswir1 | speed = dev_info.udi_speed; |
465 | 68063649 | blueswir1 | |
466 | 68063649 | blueswir1 | if (strncmp(dev_info.udi_product, "product", 7) != 0) |
467 | 68063649 | blueswir1 | pstrcpy(product_name, sizeof(product_name),
|
468 | 68063649 | blueswir1 | dev_info.udi_product); |
469 | 68063649 | blueswir1 | else
|
470 | 68063649 | blueswir1 | product_name[0] = '\0'; |
471 | 68063649 | blueswir1 | |
472 | 68063649 | blueswir1 | ret = func(opaque, bus, address, class_id, vendor_id, |
473 | 68063649 | blueswir1 | product_id, product_name, speed); |
474 | 68063649 | blueswir1 | |
475 | 68063649 | blueswir1 | close(dfd); |
476 | 68063649 | blueswir1 | |
477 | 68063649 | blueswir1 | if (ret)
|
478 | 68063649 | blueswir1 | goto the_end;
|
479 | 68063649 | blueswir1 | } |
480 | 68063649 | blueswir1 | |
481 | 68063649 | blueswir1 | close(bfd); |
482 | 68063649 | blueswir1 | } |
483 | 68063649 | blueswir1 | |
484 | 68063649 | blueswir1 | the_end:
|
485 | 68063649 | blueswir1 | return ret;
|
486 | 68063649 | blueswir1 | } |
487 | 68063649 | blueswir1 | |
488 | 68063649 | blueswir1 | typedef struct FindDeviceState { |
489 | 68063649 | blueswir1 | int vendor_id;
|
490 | 68063649 | blueswir1 | int product_id;
|
491 | 68063649 | blueswir1 | int bus_num;
|
492 | 68063649 | blueswir1 | int addr;
|
493 | 68063649 | blueswir1 | } FindDeviceState; |
494 | 68063649 | blueswir1 | |
495 | 68063649 | blueswir1 | static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, |
496 | 68063649 | blueswir1 | int class_id,
|
497 | 68063649 | blueswir1 | int vendor_id, int product_id, |
498 | 68063649 | blueswir1 | const char *product_name, int speed) |
499 | 68063649 | blueswir1 | { |
500 | 68063649 | blueswir1 | FindDeviceState *s = opaque; |
501 | 68063649 | blueswir1 | if (vendor_id == s->vendor_id &&
|
502 | 68063649 | blueswir1 | product_id == s->product_id) { |
503 | 68063649 | blueswir1 | s->bus_num = bus_num; |
504 | 68063649 | blueswir1 | s->addr = addr; |
505 | 68063649 | blueswir1 | return 1; |
506 | 68063649 | blueswir1 | } else {
|
507 | 68063649 | blueswir1 | return 0; |
508 | 68063649 | blueswir1 | } |
509 | 68063649 | blueswir1 | } |
510 | 68063649 | blueswir1 | |
511 | 68063649 | blueswir1 | |
512 | 68063649 | blueswir1 | /* the syntax is :
|
513 | 68063649 | blueswir1 | 'bus.addr' (decimal numbers) or
|
514 | 68063649 | blueswir1 | 'vendor_id:product_id' (hexa numbers) */
|
515 | 68063649 | blueswir1 | static int usb_host_find_device(int *pbus_num, int *paddr, |
516 | 68063649 | blueswir1 | const char *devname) |
517 | 68063649 | blueswir1 | { |
518 | 68063649 | blueswir1 | const char *p; |
519 | 68063649 | blueswir1 | int ret;
|
520 | 68063649 | blueswir1 | FindDeviceState fs; |
521 | 68063649 | blueswir1 | |
522 | 68063649 | blueswir1 | p = strchr(devname, '.');
|
523 | 68063649 | blueswir1 | if (p) {
|
524 | 68063649 | blueswir1 | *pbus_num = strtoul(devname, NULL, 0); |
525 | 68063649 | blueswir1 | *paddr = strtoul(p + 1, NULL, 0); |
526 | 68063649 | blueswir1 | return 0; |
527 | 68063649 | blueswir1 | } |
528 | 68063649 | blueswir1 | p = strchr(devname, ':');
|
529 | 68063649 | blueswir1 | if (p) {
|
530 | 68063649 | blueswir1 | fs.vendor_id = strtoul(devname, NULL, 16); |
531 | 68063649 | blueswir1 | fs.product_id = strtoul(p + 1, NULL, 16); |
532 | 68063649 | blueswir1 | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
533 | 68063649 | blueswir1 | if (ret) {
|
534 | 68063649 | blueswir1 | *pbus_num = fs.bus_num; |
535 | 68063649 | blueswir1 | *paddr = fs.addr; |
536 | 68063649 | blueswir1 | return 0; |
537 | 68063649 | blueswir1 | } |
538 | 68063649 | blueswir1 | } |
539 | 68063649 | blueswir1 | return -1; |
540 | 68063649 | blueswir1 | } |
541 | 68063649 | blueswir1 | |
542 | 68063649 | blueswir1 | /**********************/
|
543 | 68063649 | blueswir1 | /* USB host device info */
|
544 | 68063649 | blueswir1 | |
545 | 68063649 | blueswir1 | struct usb_class_info {
|
546 | 68063649 | blueswir1 | int class;
|
547 | 68063649 | blueswir1 | const char *class_name; |
548 | 68063649 | blueswir1 | }; |
549 | 68063649 | blueswir1 | |
550 | 68063649 | blueswir1 | static const struct usb_class_info usb_class_info[] = { |
551 | 68063649 | blueswir1 | { USB_CLASS_AUDIO, "Audio"},
|
552 | 68063649 | blueswir1 | { USB_CLASS_COMM, "Communication"},
|
553 | 68063649 | blueswir1 | { USB_CLASS_HID, "HID"},
|
554 | 68063649 | blueswir1 | { USB_CLASS_HUB, "Hub" },
|
555 | 68063649 | blueswir1 | { USB_CLASS_PHYSICAL, "Physical" },
|
556 | 68063649 | blueswir1 | { USB_CLASS_PRINTER, "Printer" },
|
557 | 68063649 | blueswir1 | { USB_CLASS_MASS_STORAGE, "Storage" },
|
558 | 68063649 | blueswir1 | { USB_CLASS_CDC_DATA, "Data" },
|
559 | 68063649 | blueswir1 | { USB_CLASS_APP_SPEC, "Application Specific" },
|
560 | 68063649 | blueswir1 | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
|
561 | 68063649 | blueswir1 | { USB_CLASS_STILL_IMAGE, "Still Image" },
|
562 | 68063649 | blueswir1 | { USB_CLASS_CSCID, "Smart Card" },
|
563 | 68063649 | blueswir1 | { USB_CLASS_CONTENT_SEC, "Content Security" },
|
564 | 68063649 | blueswir1 | { -1, NULL } |
565 | 68063649 | blueswir1 | }; |
566 | 68063649 | blueswir1 | |
567 | 68063649 | blueswir1 | static const char *usb_class_str(uint8_t class) |
568 | 68063649 | blueswir1 | { |
569 | 68063649 | blueswir1 | const struct usb_class_info *p; |
570 | 68063649 | blueswir1 | for (p = usb_class_info; p->class != -1; p++) { |
571 | 68063649 | blueswir1 | if (p->class == class)
|
572 | 68063649 | blueswir1 | break;
|
573 | 68063649 | blueswir1 | } |
574 | 68063649 | blueswir1 | return p->class_name;
|
575 | 68063649 | blueswir1 | } |
576 | 68063649 | blueswir1 | |
577 | 179da8af | Blue Swirl | static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id, |
578 | 00766a4e | blueswir1 | int vendor_id, int product_id, |
579 | 00766a4e | blueswir1 | const char *product_name, |
580 | 00766a4e | blueswir1 | int speed)
|
581 | 68063649 | blueswir1 | { |
582 | 68063649 | blueswir1 | const char *class_str, *speed_str; |
583 | 68063649 | blueswir1 | |
584 | 68063649 | blueswir1 | switch(speed) {
|
585 | 68063649 | blueswir1 | case USB_SPEED_LOW:
|
586 | 68063649 | blueswir1 | speed_str = "1.5";
|
587 | 68063649 | blueswir1 | break;
|
588 | 68063649 | blueswir1 | case USB_SPEED_FULL:
|
589 | 68063649 | blueswir1 | speed_str = "12";
|
590 | 68063649 | blueswir1 | break;
|
591 | 68063649 | blueswir1 | case USB_SPEED_HIGH:
|
592 | 68063649 | blueswir1 | speed_str = "480";
|
593 | 68063649 | blueswir1 | break;
|
594 | 68063649 | blueswir1 | default:
|
595 | 68063649 | blueswir1 | speed_str = "?";
|
596 | 68063649 | blueswir1 | break;
|
597 | 68063649 | blueswir1 | } |
598 | 68063649 | blueswir1 | |
599 | 376253ec | aliguori | monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
|
600 | 376253ec | aliguori | bus_num, addr, speed_str); |
601 | 68063649 | blueswir1 | class_str = usb_class_str(class_id); |
602 | 68063649 | blueswir1 | if (class_str)
|
603 | 376253ec | aliguori | monitor_printf(mon, " %s:", class_str);
|
604 | 68063649 | blueswir1 | else
|
605 | 376253ec | aliguori | monitor_printf(mon, " Class %02x:", class_id);
|
606 | 376253ec | aliguori | monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
|
607 | 68063649 | blueswir1 | if (product_name[0] != '\0') |
608 | 376253ec | aliguori | monitor_printf(mon, ", %s", product_name);
|
609 | 376253ec | aliguori | monitor_printf(mon, "\n");
|
610 | 68063649 | blueswir1 | } |
611 | 68063649 | blueswir1 | |
612 | 376253ec | aliguori | static int usb_host_info_device(void *opaque, |
613 | 376253ec | aliguori | int bus_num, int addr, |
614 | 68063649 | blueswir1 | int class_id,
|
615 | 68063649 | blueswir1 | int vendor_id, int product_id, |
616 | 68063649 | blueswir1 | const char *product_name, |
617 | 68063649 | blueswir1 | int speed)
|
618 | 68063649 | blueswir1 | { |
619 | 179da8af | Blue Swirl | Monitor *mon = opaque; |
620 | 179da8af | Blue Swirl | |
621 | 179da8af | Blue Swirl | usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id, |
622 | 68063649 | blueswir1 | product_name, speed); |
623 | 68063649 | blueswir1 | return 0; |
624 | 68063649 | blueswir1 | } |
625 | 68063649 | blueswir1 | |
626 | 376253ec | aliguori | void usb_host_info(Monitor *mon)
|
627 | 68063649 | blueswir1 | { |
628 | 179da8af | Blue Swirl | usb_host_scan(mon, usb_host_info_device); |
629 | 68063649 | blueswir1 | } |
630 | 68063649 | blueswir1 | |
631 | 68063649 | blueswir1 | /* XXX add this */
|
632 | 68063649 | blueswir1 | int usb_host_device_close(const char *devname) |
633 | 68063649 | blueswir1 | { |
634 | 68063649 | blueswir1 | return 0; |
635 | 68063649 | blueswir1 | } |