root / usb-bsd.c @ 079d0b7f
History | View | Annotate | Download (17.7 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 | |
43 | 68063649 | blueswir1 | /* This value has maximum potential at 16.
|
44 | 68063649 | blueswir1 | * You should also set hw.usb.debug to gain
|
45 | 68063649 | blueswir1 | * more detailed view.
|
46 | 68063649 | blueswir1 | */
|
47 | 68063649 | blueswir1 | //#define DEBUG
|
48 | 68063649 | blueswir1 | #define UGEN_DEBUG_LEVEL 0 |
49 | 68063649 | blueswir1 | |
50 | 68063649 | blueswir1 | |
51 | 68063649 | blueswir1 | typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id, |
52 | 68063649 | blueswir1 | int vendor_id, int product_id, |
53 | 68063649 | blueswir1 | const char *product_name, int speed); |
54 | 68063649 | blueswir1 | static int usb_host_find_device(int *pbus_num, int *paddr, |
55 | 68063649 | blueswir1 | const char *devname); |
56 | 68063649 | blueswir1 | |
57 | 68063649 | blueswir1 | typedef struct USBHostDevice { |
58 | 68063649 | blueswir1 | USBDevice dev; |
59 | 68063649 | blueswir1 | int ep_fd[USB_MAX_ENDPOINTS];
|
60 | 68063649 | blueswir1 | int devfd;
|
61 | 68063649 | blueswir1 | char devpath[32]; |
62 | 68063649 | blueswir1 | } USBHostDevice; |
63 | 68063649 | blueswir1 | |
64 | 68063649 | blueswir1 | |
65 | 68063649 | blueswir1 | static int ensure_ep_open(USBHostDevice *dev, int ep, int mode) |
66 | 68063649 | blueswir1 | { |
67 | 68063649 | blueswir1 | char buf[32]; |
68 | 68063649 | blueswir1 | int fd;
|
69 | 68063649 | blueswir1 | |
70 | 68063649 | blueswir1 | /* Get the address for this endpoint */
|
71 | 68063649 | blueswir1 | ep = UE_GET_ADDR(ep); |
72 | 68063649 | blueswir1 | |
73 | 68063649 | blueswir1 | if (dev->ep_fd[ep] < 0) { |
74 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
75 | 68063649 | blueswir1 | snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->devpath, ep); |
76 | 68063649 | blueswir1 | #else
|
77 | 68063649 | blueswir1 | snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->devpath, ep); |
78 | 68063649 | blueswir1 | #endif
|
79 | 68063649 | blueswir1 | /* Try to open it O_RDWR first for those devices which have in and out
|
80 | 68063649 | blueswir1 | * endpoints with the same address (eg 0x02 and 0x82)
|
81 | 68063649 | blueswir1 | */
|
82 | 68063649 | blueswir1 | fd = open(buf, O_RDWR); |
83 | 68063649 | blueswir1 | if (fd < 0 && errno == ENXIO) |
84 | 68063649 | blueswir1 | fd = open(buf, mode); |
85 | 68063649 | blueswir1 | if (fd < 0) { |
86 | 68063649 | blueswir1 | #ifdef DEBUG
|
87 | 68063649 | blueswir1 | printf("ensure_ep_open: failed to open device endpoint %s: %s\n",
|
88 | 68063649 | blueswir1 | buf, strerror(errno)); |
89 | 68063649 | blueswir1 | #endif
|
90 | 68063649 | blueswir1 | } |
91 | 68063649 | blueswir1 | dev->ep_fd[ep] = fd; |
92 | 68063649 | blueswir1 | } |
93 | 68063649 | blueswir1 | |
94 | 68063649 | blueswir1 | return dev->ep_fd[ep];
|
95 | 68063649 | blueswir1 | } |
96 | 68063649 | blueswir1 | |
97 | 68063649 | blueswir1 | static void ensure_eps_closed(USBHostDevice *dev) |
98 | 68063649 | blueswir1 | { |
99 | 68063649 | blueswir1 | int epnum = 1; |
100 | 68063649 | blueswir1 | |
101 | 68063649 | blueswir1 | if (!dev)
|
102 | 68063649 | blueswir1 | return;
|
103 | 68063649 | blueswir1 | |
104 | 68063649 | blueswir1 | while (epnum < USB_MAX_ENDPOINTS) {
|
105 | 68063649 | blueswir1 | if (dev->ep_fd[epnum] >= 0) { |
106 | 68063649 | blueswir1 | close(dev->ep_fd[epnum]); |
107 | 68063649 | blueswir1 | dev->ep_fd[epnum] = -1;
|
108 | 68063649 | blueswir1 | } |
109 | 68063649 | blueswir1 | epnum++; |
110 | 68063649 | blueswir1 | } |
111 | 68063649 | blueswir1 | } |
112 | 68063649 | blueswir1 | |
113 | 68063649 | blueswir1 | static void usb_host_handle_reset(USBDevice *dev) |
114 | 68063649 | blueswir1 | { |
115 | 68063649 | blueswir1 | #if 0
|
116 | 68063649 | blueswir1 | USBHostDevice *s = (USBHostDevice *)dev;
|
117 | 68063649 | blueswir1 | #endif
|
118 | 68063649 | blueswir1 | } |
119 | 68063649 | blueswir1 | |
120 | 68063649 | blueswir1 | /* XXX:
|
121 | 68063649 | blueswir1 | * -check device states against transfer requests
|
122 | 68063649 | blueswir1 | * and return appropriate response
|
123 | 68063649 | blueswir1 | */
|
124 | 68063649 | blueswir1 | static int usb_host_handle_control(USBDevice *dev, |
125 | 007fd62f | Hans de Goede | USBPacket *p, |
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 | 079d0b7f | Gerd Hoffmann | uint8_t devep = p->ep->nr; |
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 | 4f4321c1 | Gerd Hoffmann | ret = readv(fd, p->iov.iov, p->iov.niov); |
257 | 68063649 | blueswir1 | else
|
258 | 4f4321c1 | Gerd Hoffmann | ret = writev(fd, p->iov.iov, p->iov.niov); |
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 | 7267c094 | Anthony Liguori | g_free(s); |
294 | 68063649 | blueswir1 | } |
295 | 68063649 | blueswir1 | |
296 | 620150dc | Blue Swirl | static int usb_host_initfn(USBDevice *dev) |
297 | 620150dc | Blue Swirl | { |
298 | 620150dc | Blue Swirl | return 0; |
299 | 620150dc | Blue Swirl | } |
300 | 620150dc | Blue Swirl | |
301 | 68063649 | blueswir1 | USBDevice *usb_host_device_open(const char *devname) |
302 | 68063649 | blueswir1 | { |
303 | 68063649 | blueswir1 | struct usb_device_info bus_info, dev_info;
|
304 | fa57ee8e | Gerd Hoffmann | USBDevice *d = NULL, *ret = NULL; |
305 | fa57ee8e | Gerd Hoffmann | USBHostDevice *dev; |
306 | 68063649 | blueswir1 | char ctlpath[PATH_MAX + 1]; |
307 | 68063649 | blueswir1 | char buspath[PATH_MAX + 1]; |
308 | 68063649 | blueswir1 | int bfd, dfd, bus, address, i;
|
309 | 68063649 | blueswir1 | int ugendebug = UGEN_DEBUG_LEVEL;
|
310 | 68063649 | blueswir1 | |
311 | 1a20a032 | Blue Swirl | if (usb_host_find_device(&bus, &address, devname) < 0) { |
312 | 1a20a032 | Blue Swirl | goto fail;
|
313 | 1a20a032 | Blue Swirl | } |
314 | 68063649 | blueswir1 | |
315 | 68063649 | blueswir1 | snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
|
316 | 68063649 | blueswir1 | |
317 | 68063649 | blueswir1 | bfd = open(buspath, O_RDWR); |
318 | 68063649 | blueswir1 | if (bfd < 0) { |
319 | 68063649 | blueswir1 | #ifdef DEBUG
|
320 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to open usb bus - %s\n",
|
321 | 68063649 | blueswir1 | strerror(errno)); |
322 | 68063649 | blueswir1 | #endif
|
323 | 1a20a032 | Blue Swirl | goto fail;
|
324 | 68063649 | blueswir1 | } |
325 | 68063649 | blueswir1 | |
326 | 68063649 | blueswir1 | bus_info.udi_addr = address; |
327 | 68063649 | blueswir1 | if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) { |
328 | 68063649 | blueswir1 | #ifdef DEBUG
|
329 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to grab bus information - %s\n",
|
330 | 68063649 | blueswir1 | strerror(errno)); |
331 | 68063649 | blueswir1 | #endif
|
332 | 1a20a032 | Blue Swirl | goto fail_bfd;
|
333 | 68063649 | blueswir1 | } |
334 | 68063649 | blueswir1 | |
335 | a167ba50 | Aurelien Jarno | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
336 | 68063649 | blueswir1 | snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]); |
337 | 68063649 | blueswir1 | #else
|
338 | 68063649 | blueswir1 | snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]); |
339 | 68063649 | blueswir1 | #endif
|
340 | 68063649 | blueswir1 | |
341 | 68063649 | blueswir1 | dfd = open(ctlpath, O_RDWR); |
342 | 68063649 | blueswir1 | if (dfd < 0) { |
343 | 68063649 | blueswir1 | dfd = open(ctlpath, O_RDONLY); |
344 | 68063649 | blueswir1 | if (dfd < 0) { |
345 | 68063649 | blueswir1 | #ifdef DEBUG
|
346 | 68063649 | blueswir1 | printf("usb_host_device_open: failed to open usb device %s - %s\n",
|
347 | 68063649 | blueswir1 | ctlpath, strerror(errno)); |
348 | 68063649 | blueswir1 | #endif
|
349 | 68063649 | blueswir1 | } |
350 | 1a20a032 | Blue Swirl | goto fail_dfd;
|
351 | 68063649 | blueswir1 | } |
352 | 68063649 | blueswir1 | |
353 | 1a20a032 | Blue Swirl | if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) { |
354 | 68063649 | blueswir1 | #ifdef DEBUG
|
355 | 1a20a032 | Blue Swirl | printf("usb_host_device_open: failed to grab device info - %s\n",
|
356 | 1a20a032 | Blue Swirl | strerror(errno)); |
357 | 68063649 | blueswir1 | #endif
|
358 | 1a20a032 | Blue Swirl | goto fail_dfd;
|
359 | 1a20a032 | Blue Swirl | } |
360 | 68063649 | blueswir1 | |
361 | 1a20a032 | Blue Swirl | d = usb_create(NULL /* FIXME */, "usb-host"); |
362 | 1a20a032 | Blue Swirl | dev = DO_UPCAST(USBHostDevice, dev, d); |
363 | 620150dc | Blue Swirl | |
364 | 1a20a032 | Blue Swirl | if (dev_info.udi_speed == 1) { |
365 | 1a20a032 | Blue Swirl | dev->dev.speed = USB_SPEED_LOW - 1;
|
366 | ba3f9bfb | Hans de Goede | dev->dev.speedmask = USB_SPEED_MASK_LOW; |
367 | 1a20a032 | Blue Swirl | } else {
|
368 | 1a20a032 | Blue Swirl | dev->dev.speed = USB_SPEED_FULL - 1;
|
369 | ba3f9bfb | Hans de Goede | dev->dev.speedmask = USB_SPEED_MASK_FULL; |
370 | 1a20a032 | Blue Swirl | } |
371 | 68063649 | blueswir1 | |
372 | 1a20a032 | Blue Swirl | if (strncmp(dev_info.udi_product, "product", 7) != 0) { |
373 | 1a20a032 | Blue Swirl | pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
|
374 | 1a20a032 | Blue Swirl | dev_info.udi_product); |
375 | 1a20a032 | Blue Swirl | } else {
|
376 | 1a20a032 | Blue Swirl | snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
|
377 | 1a20a032 | Blue Swirl | "host:%s", devname);
|
378 | 1a20a032 | Blue Swirl | } |
379 | 68063649 | blueswir1 | |
380 | 1a20a032 | Blue Swirl | pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/"); |
381 | 1a20a032 | Blue Swirl | pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]); |
382 | 68063649 | blueswir1 | |
383 | 1a20a032 | Blue Swirl | /* Mark the endpoints as not yet open */
|
384 | 1a20a032 | Blue Swirl | for (i = 0; i < USB_MAX_ENDPOINTS; i++) { |
385 | 1a20a032 | Blue Swirl | dev->ep_fd[i] = -1;
|
386 | 1a20a032 | Blue Swirl | } |
387 | 68063649 | blueswir1 | |
388 | 1a20a032 | Blue Swirl | ioctl(dfd, USB_SETDEBUG, &ugendebug); |
389 | 68063649 | blueswir1 | |
390 | 1a20a032 | Blue Swirl | ret = (USBDevice *)dev; |
391 | 68063649 | blueswir1 | |
392 | 1a20a032 | Blue Swirl | fail_dfd:
|
393 | 1a20a032 | Blue Swirl | close(dfd); |
394 | 1a20a032 | Blue Swirl | fail_bfd:
|
395 | 1a20a032 | Blue Swirl | close(bfd); |
396 | 68063649 | blueswir1 | fail:
|
397 | 1a20a032 | Blue Swirl | return ret;
|
398 | 68063649 | blueswir1 | } |
399 | 68063649 | blueswir1 | |
400 | 62aed765 | Anthony Liguori | static void usb_host_class_initfn(ObjectClass *klass, void *data) |
401 | 62aed765 | Anthony Liguori | { |
402 | 62aed765 | Anthony Liguori | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
403 | 62aed765 | Anthony Liguori | |
404 | 62aed765 | Anthony Liguori | uc->product_desc = "USB Host Device";
|
405 | 62aed765 | Anthony Liguori | uc->init = usb_host_initfn; |
406 | 62aed765 | Anthony Liguori | uc->handle_reset = usb_host_handle_reset; |
407 | 62aed765 | Anthony Liguori | uc->handle_control = usb_host_handle_control; |
408 | 62aed765 | Anthony Liguori | uc->handle_data = usb_host_handle_data; |
409 | 62aed765 | Anthony Liguori | uc->handle_destroy = usb_host_handle_destroy; |
410 | 62aed765 | Anthony Liguori | } |
411 | 62aed765 | Anthony Liguori | |
412 | d217d208 | Anthony Liguori | static TypeInfo usb_host_dev_info = {
|
413 | d217d208 | Anthony Liguori | .name = "usb-host",
|
414 | d217d208 | Anthony Liguori | .parent = TYPE_USB_DEVICE, |
415 | d217d208 | Anthony Liguori | .instance_size = sizeof(USBHostDevice),
|
416 | 53a19a30 | Blue Swirl | .class_init = usb_host_class_initfn, |
417 | 620150dc | Blue Swirl | }; |
418 | 620150dc | Blue Swirl | |
419 | 620150dc | Blue Swirl | static void usb_host_register_devices(void) |
420 | 620150dc | Blue Swirl | { |
421 | d217d208 | Anthony Liguori | type_register_static(&usb_host_dev_info); |
422 | 620150dc | Blue Swirl | } |
423 | 620150dc | Blue Swirl | device_init(usb_host_register_devices) |
424 | 620150dc | Blue Swirl | |
425 | 68063649 | blueswir1 | static int usb_host_scan(void *opaque, USBScanFunc *func) |
426 | 68063649 | blueswir1 | { |
427 | 68063649 | blueswir1 | struct usb_device_info bus_info;
|
428 | 68063649 | blueswir1 | struct usb_device_info dev_info;
|
429 | 68063649 | blueswir1 | uint16_t vendor_id, product_id, class_id, speed; |
430 | 68063649 | blueswir1 | int bfd, dfd, bus, address;
|
431 | 68063649 | blueswir1 | char busbuf[20], devbuf[20], product_name[256]; |
432 | 68063649 | blueswir1 | int ret = 0; |
433 | 68063649 | blueswir1 | |
434 | 68063649 | blueswir1 | for (bus = 0; bus < 10; bus++) { |
435 | 68063649 | blueswir1 | |
436 | 68063649 | blueswir1 | snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus); |
437 | 68063649 | blueswir1 | bfd = open(busbuf, O_RDWR); |
438 | 68063649 | blueswir1 | if (bfd < 0) |
439 | 68063649 | blueswir1 | continue;
|
440 | 68063649 | blueswir1 | |
441 | 68063649 | blueswir1 | for (address = 1; address < 127; address++) { |
442 | 68063649 | blueswir1 | |
443 | 68063649 | blueswir1 | bus_info.udi_addr = address; |
444 | 68063649 | blueswir1 | if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) |
445 | 68063649 | blueswir1 | continue;
|
446 | 68063649 | blueswir1 | |
447 | 68063649 | blueswir1 | /* only list devices that can be used by generic layer */
|
448 | 68063649 | blueswir1 | if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0) |
449 | 68063649 | blueswir1 | continue;
|
450 | 68063649 | blueswir1 | |
451 | a167ba50 | Aurelien Jarno | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
452 | 68063649 | blueswir1 | snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]); |
453 | 68063649 | blueswir1 | #else
|
454 | 68063649 | blueswir1 | snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]); |
455 | 68063649 | blueswir1 | #endif
|
456 | 68063649 | blueswir1 | |
457 | 68063649 | blueswir1 | dfd = open(devbuf, O_RDONLY); |
458 | 68063649 | blueswir1 | if (dfd < 0) { |
459 | 68063649 | blueswir1 | #ifdef DEBUG
|
460 | 68063649 | blueswir1 | printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
|
461 | 68063649 | blueswir1 | strerror(errno)); |
462 | 68063649 | blueswir1 | #endif
|
463 | 68063649 | blueswir1 | continue;
|
464 | 68063649 | blueswir1 | } |
465 | 68063649 | blueswir1 | |
466 | 68063649 | blueswir1 | if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) |
467 | 68063649 | blueswir1 | printf("usb_host_scan: couldn't get device information for %s - %s\n",
|
468 | 68063649 | blueswir1 | devbuf, strerror(errno)); |
469 | 68063649 | blueswir1 | |
470 | 4ff9786c | Stefan Weil | /* XXX: might need to fixup endianness of word values before copying over */
|
471 | 68063649 | blueswir1 | |
472 | 68063649 | blueswir1 | vendor_id = dev_info.udi_vendorNo; |
473 | 68063649 | blueswir1 | product_id = dev_info.udi_productNo; |
474 | 68063649 | blueswir1 | class_id = dev_info.udi_class; |
475 | 68063649 | blueswir1 | speed = dev_info.udi_speed; |
476 | 68063649 | blueswir1 | |
477 | 68063649 | blueswir1 | if (strncmp(dev_info.udi_product, "product", 7) != 0) |
478 | 68063649 | blueswir1 | pstrcpy(product_name, sizeof(product_name),
|
479 | 68063649 | blueswir1 | dev_info.udi_product); |
480 | 68063649 | blueswir1 | else
|
481 | 68063649 | blueswir1 | product_name[0] = '\0'; |
482 | 68063649 | blueswir1 | |
483 | 68063649 | blueswir1 | ret = func(opaque, bus, address, class_id, vendor_id, |
484 | 68063649 | blueswir1 | product_id, product_name, speed); |
485 | 68063649 | blueswir1 | |
486 | 68063649 | blueswir1 | close(dfd); |
487 | 68063649 | blueswir1 | |
488 | 68063649 | blueswir1 | if (ret)
|
489 | 68063649 | blueswir1 | goto the_end;
|
490 | 68063649 | blueswir1 | } |
491 | 68063649 | blueswir1 | |
492 | 68063649 | blueswir1 | close(bfd); |
493 | 68063649 | blueswir1 | } |
494 | 68063649 | blueswir1 | |
495 | 68063649 | blueswir1 | the_end:
|
496 | 68063649 | blueswir1 | return ret;
|
497 | 68063649 | blueswir1 | } |
498 | 68063649 | blueswir1 | |
499 | 68063649 | blueswir1 | typedef struct FindDeviceState { |
500 | 68063649 | blueswir1 | int vendor_id;
|
501 | 68063649 | blueswir1 | int product_id;
|
502 | 68063649 | blueswir1 | int bus_num;
|
503 | 68063649 | blueswir1 | int addr;
|
504 | 68063649 | blueswir1 | } FindDeviceState; |
505 | 68063649 | blueswir1 | |
506 | 68063649 | blueswir1 | static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, |
507 | 68063649 | blueswir1 | int class_id,
|
508 | 68063649 | blueswir1 | int vendor_id, int product_id, |
509 | 68063649 | blueswir1 | const char *product_name, int speed) |
510 | 68063649 | blueswir1 | { |
511 | 68063649 | blueswir1 | FindDeviceState *s = opaque; |
512 | 68063649 | blueswir1 | if (vendor_id == s->vendor_id &&
|
513 | 68063649 | blueswir1 | product_id == s->product_id) { |
514 | 68063649 | blueswir1 | s->bus_num = bus_num; |
515 | 68063649 | blueswir1 | s->addr = addr; |
516 | 68063649 | blueswir1 | return 1; |
517 | 68063649 | blueswir1 | } else {
|
518 | 68063649 | blueswir1 | return 0; |
519 | 68063649 | blueswir1 | } |
520 | 68063649 | blueswir1 | } |
521 | 68063649 | blueswir1 | |
522 | 68063649 | blueswir1 | |
523 | 68063649 | blueswir1 | /* the syntax is :
|
524 | 68063649 | blueswir1 | 'bus.addr' (decimal numbers) or
|
525 | 68063649 | blueswir1 | 'vendor_id:product_id' (hexa numbers) */
|
526 | 68063649 | blueswir1 | static int usb_host_find_device(int *pbus_num, int *paddr, |
527 | 68063649 | blueswir1 | const char *devname) |
528 | 68063649 | blueswir1 | { |
529 | 68063649 | blueswir1 | const char *p; |
530 | 68063649 | blueswir1 | int ret;
|
531 | 68063649 | blueswir1 | FindDeviceState fs; |
532 | 68063649 | blueswir1 | |
533 | 68063649 | blueswir1 | p = strchr(devname, '.');
|
534 | 68063649 | blueswir1 | if (p) {
|
535 | 68063649 | blueswir1 | *pbus_num = strtoul(devname, NULL, 0); |
536 | 68063649 | blueswir1 | *paddr = strtoul(p + 1, NULL, 0); |
537 | 68063649 | blueswir1 | return 0; |
538 | 68063649 | blueswir1 | } |
539 | 68063649 | blueswir1 | p = strchr(devname, ':');
|
540 | 68063649 | blueswir1 | if (p) {
|
541 | 68063649 | blueswir1 | fs.vendor_id = strtoul(devname, NULL, 16); |
542 | 68063649 | blueswir1 | fs.product_id = strtoul(p + 1, NULL, 16); |
543 | 68063649 | blueswir1 | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
544 | 68063649 | blueswir1 | if (ret) {
|
545 | 68063649 | blueswir1 | *pbus_num = fs.bus_num; |
546 | 68063649 | blueswir1 | *paddr = fs.addr; |
547 | 68063649 | blueswir1 | return 0; |
548 | 68063649 | blueswir1 | } |
549 | 68063649 | blueswir1 | } |
550 | 68063649 | blueswir1 | return -1; |
551 | 68063649 | blueswir1 | } |
552 | 68063649 | blueswir1 | |
553 | 68063649 | blueswir1 | /**********************/
|
554 | 68063649 | blueswir1 | /* USB host device info */
|
555 | 68063649 | blueswir1 | |
556 | 68063649 | blueswir1 | struct usb_class_info {
|
557 | 68063649 | blueswir1 | int class;
|
558 | 68063649 | blueswir1 | const char *class_name; |
559 | 68063649 | blueswir1 | }; |
560 | 68063649 | blueswir1 | |
561 | 68063649 | blueswir1 | static const struct usb_class_info usb_class_info[] = { |
562 | 68063649 | blueswir1 | { USB_CLASS_AUDIO, "Audio"},
|
563 | 68063649 | blueswir1 | { USB_CLASS_COMM, "Communication"},
|
564 | 68063649 | blueswir1 | { USB_CLASS_HID, "HID"},
|
565 | 68063649 | blueswir1 | { USB_CLASS_HUB, "Hub" },
|
566 | 68063649 | blueswir1 | { USB_CLASS_PHYSICAL, "Physical" },
|
567 | 68063649 | blueswir1 | { USB_CLASS_PRINTER, "Printer" },
|
568 | 68063649 | blueswir1 | { USB_CLASS_MASS_STORAGE, "Storage" },
|
569 | 68063649 | blueswir1 | { USB_CLASS_CDC_DATA, "Data" },
|
570 | 68063649 | blueswir1 | { USB_CLASS_APP_SPEC, "Application Specific" },
|
571 | 68063649 | blueswir1 | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
|
572 | 68063649 | blueswir1 | { USB_CLASS_STILL_IMAGE, "Still Image" },
|
573 | 68063649 | blueswir1 | { USB_CLASS_CSCID, "Smart Card" },
|
574 | 68063649 | blueswir1 | { USB_CLASS_CONTENT_SEC, "Content Security" },
|
575 | 68063649 | blueswir1 | { -1, NULL } |
576 | 68063649 | blueswir1 | }; |
577 | 68063649 | blueswir1 | |
578 | 68063649 | blueswir1 | static const char *usb_class_str(uint8_t class) |
579 | 68063649 | blueswir1 | { |
580 | 68063649 | blueswir1 | const struct usb_class_info *p; |
581 | 68063649 | blueswir1 | for (p = usb_class_info; p->class != -1; p++) { |
582 | 68063649 | blueswir1 | if (p->class == class)
|
583 | 68063649 | blueswir1 | break;
|
584 | 68063649 | blueswir1 | } |
585 | 68063649 | blueswir1 | return p->class_name;
|
586 | 68063649 | blueswir1 | } |
587 | 68063649 | blueswir1 | |
588 | 179da8af | Blue Swirl | static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id, |
589 | 00766a4e | blueswir1 | int vendor_id, int product_id, |
590 | 00766a4e | blueswir1 | const char *product_name, |
591 | 00766a4e | blueswir1 | int speed)
|
592 | 68063649 | blueswir1 | { |
593 | 68063649 | blueswir1 | const char *class_str, *speed_str; |
594 | 68063649 | blueswir1 | |
595 | 68063649 | blueswir1 | switch(speed) {
|
596 | 68063649 | blueswir1 | case USB_SPEED_LOW:
|
597 | 68063649 | blueswir1 | speed_str = "1.5";
|
598 | 68063649 | blueswir1 | break;
|
599 | 68063649 | blueswir1 | case USB_SPEED_FULL:
|
600 | 68063649 | blueswir1 | speed_str = "12";
|
601 | 68063649 | blueswir1 | break;
|
602 | 68063649 | blueswir1 | case USB_SPEED_HIGH:
|
603 | 68063649 | blueswir1 | speed_str = "480";
|
604 | 68063649 | blueswir1 | break;
|
605 | 68063649 | blueswir1 | default:
|
606 | 68063649 | blueswir1 | speed_str = "?";
|
607 | 68063649 | blueswir1 | break;
|
608 | 68063649 | blueswir1 | } |
609 | 68063649 | blueswir1 | |
610 | 376253ec | aliguori | monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
|
611 | 376253ec | aliguori | bus_num, addr, speed_str); |
612 | 68063649 | blueswir1 | class_str = usb_class_str(class_id); |
613 | 68063649 | blueswir1 | if (class_str)
|
614 | 376253ec | aliguori | monitor_printf(mon, " %s:", class_str);
|
615 | 68063649 | blueswir1 | else
|
616 | 376253ec | aliguori | monitor_printf(mon, " Class %02x:", class_id);
|
617 | 376253ec | aliguori | monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
|
618 | 68063649 | blueswir1 | if (product_name[0] != '\0') |
619 | 376253ec | aliguori | monitor_printf(mon, ", %s", product_name);
|
620 | 376253ec | aliguori | monitor_printf(mon, "\n");
|
621 | 68063649 | blueswir1 | } |
622 | 68063649 | blueswir1 | |
623 | 376253ec | aliguori | static int usb_host_info_device(void *opaque, |
624 | 376253ec | aliguori | int bus_num, int addr, |
625 | 68063649 | blueswir1 | int class_id,
|
626 | 68063649 | blueswir1 | int vendor_id, int product_id, |
627 | 68063649 | blueswir1 | const char *product_name, |
628 | 68063649 | blueswir1 | int speed)
|
629 | 68063649 | blueswir1 | { |
630 | 179da8af | Blue Swirl | Monitor *mon = opaque; |
631 | 179da8af | Blue Swirl | |
632 | 179da8af | Blue Swirl | usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id, |
633 | 68063649 | blueswir1 | product_name, speed); |
634 | 68063649 | blueswir1 | return 0; |
635 | 68063649 | blueswir1 | } |
636 | 68063649 | blueswir1 | |
637 | 376253ec | aliguori | void usb_host_info(Monitor *mon)
|
638 | 68063649 | blueswir1 | { |
639 | 179da8af | Blue Swirl | usb_host_scan(mon, usb_host_info_device); |
640 | 68063649 | blueswir1 | } |
641 | 68063649 | blueswir1 | |
642 | 68063649 | blueswir1 | /* XXX add this */
|
643 | 68063649 | blueswir1 | int usb_host_device_close(const char *devname) |
644 | 68063649 | blueswir1 | { |
645 | 68063649 | blueswir1 | return 0; |
646 | 68063649 | blueswir1 | } |