1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /* $FreeBSD$ */ |
---|
4 | /*- |
---|
5 | * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
26 | * SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #include <sys/stdint.h> |
---|
30 | #include <sys/stddef.h> |
---|
31 | #include <rtems/bsd/sys/param.h> |
---|
32 | #include <sys/queue.h> |
---|
33 | #include <rtems/bsd/sys/types.h> |
---|
34 | #include <sys/systm.h> |
---|
35 | #include <sys/kernel.h> |
---|
36 | #include <sys/bus.h> |
---|
37 | #include <sys/module.h> |
---|
38 | #include <rtems/bsd/sys/lock.h> |
---|
39 | #include <sys/mutex.h> |
---|
40 | #include <sys/condvar.h> |
---|
41 | #include <sys/sysctl.h> |
---|
42 | #include <sys/sx.h> |
---|
43 | #include <rtems/bsd/sys/unistd.h> |
---|
44 | #include <sys/callout.h> |
---|
45 | #include <sys/malloc.h> |
---|
46 | #include <sys/priv.h> |
---|
47 | #include <sys/conf.h> |
---|
48 | #include <sys/fcntl.h> |
---|
49 | |
---|
50 | #include <dev/usb/usb.h> |
---|
51 | #include <dev/usb/usb_ioctl.h> |
---|
52 | #include <dev/usb/usbdi.h> |
---|
53 | #include <dev/usb/usbdi_util.h> |
---|
54 | |
---|
55 | #define USB_DEBUG_VAR ugen_debug |
---|
56 | |
---|
57 | #include <dev/usb/usb_core.h> |
---|
58 | #include <dev/usb/usb_dev.h> |
---|
59 | #include <dev/usb/usb_mbuf.h> |
---|
60 | #include <dev/usb/usb_process.h> |
---|
61 | #include <dev/usb/usb_device.h> |
---|
62 | #include <dev/usb/usb_debug.h> |
---|
63 | #include <dev/usb/usb_request.h> |
---|
64 | #include <dev/usb/usb_busdma.h> |
---|
65 | #include <dev/usb/usb_util.h> |
---|
66 | #include <dev/usb/usb_hub.h> |
---|
67 | #include <dev/usb/usb_generic.h> |
---|
68 | #include <dev/usb/usb_transfer.h> |
---|
69 | |
---|
70 | #include <dev/usb/usb_controller.h> |
---|
71 | #include <dev/usb/usb_bus.h> |
---|
72 | |
---|
73 | #if USB_HAVE_UGEN |
---|
74 | |
---|
75 | /* defines */ |
---|
76 | |
---|
77 | #define UGEN_BULK_FS_BUFFER_SIZE (64*32) /* bytes */ |
---|
78 | #define UGEN_BULK_HS_BUFFER_SIZE (1024*32) /* bytes */ |
---|
79 | #define UGEN_HW_FRAMES 50 /* number of milliseconds per transfer */ |
---|
80 | |
---|
81 | /* function prototypes */ |
---|
82 | |
---|
83 | static usb_callback_t ugen_read_clear_stall_callback; |
---|
84 | static usb_callback_t ugen_write_clear_stall_callback; |
---|
85 | static usb_callback_t ugen_ctrl_read_callback; |
---|
86 | static usb_callback_t ugen_ctrl_write_callback; |
---|
87 | static usb_callback_t ugen_isoc_read_callback; |
---|
88 | static usb_callback_t ugen_isoc_write_callback; |
---|
89 | static usb_callback_t ugen_ctrl_fs_callback; |
---|
90 | |
---|
91 | static usb_fifo_open_t ugen_open; |
---|
92 | static usb_fifo_close_t ugen_close; |
---|
93 | static usb_fifo_ioctl_t ugen_ioctl; |
---|
94 | static usb_fifo_ioctl_t ugen_ioctl_post; |
---|
95 | static usb_fifo_cmd_t ugen_start_read; |
---|
96 | static usb_fifo_cmd_t ugen_start_write; |
---|
97 | static usb_fifo_cmd_t ugen_stop_io; |
---|
98 | |
---|
99 | static int ugen_transfer_setup(struct usb_fifo *, |
---|
100 | const struct usb_config *, uint8_t); |
---|
101 | static int ugen_open_pipe_write(struct usb_fifo *); |
---|
102 | static int ugen_open_pipe_read(struct usb_fifo *); |
---|
103 | static int ugen_set_config(struct usb_fifo *, uint8_t); |
---|
104 | static int ugen_set_interface(struct usb_fifo *, uint8_t, uint8_t); |
---|
105 | static int ugen_get_cdesc(struct usb_fifo *, struct usb_gen_descriptor *); |
---|
106 | static int ugen_get_sdesc(struct usb_fifo *, struct usb_gen_descriptor *); |
---|
107 | static int ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd); |
---|
108 | static int usb_gen_fill_deviceinfo(struct usb_fifo *, |
---|
109 | struct usb_device_info *); |
---|
110 | static int ugen_re_enumerate(struct usb_fifo *); |
---|
111 | static int ugen_iface_ioctl(struct usb_fifo *, u_long, void *, int); |
---|
112 | static uint8_t ugen_fs_get_complete(struct usb_fifo *, uint8_t *); |
---|
113 | static int ugen_fs_uninit(struct usb_fifo *f); |
---|
114 | |
---|
115 | /* structures */ |
---|
116 | |
---|
117 | struct usb_fifo_methods usb_ugen_methods = { |
---|
118 | .f_open = &ugen_open, |
---|
119 | .f_close = &ugen_close, |
---|
120 | .f_ioctl = &ugen_ioctl, |
---|
121 | .f_ioctl_post = &ugen_ioctl_post, |
---|
122 | .f_start_read = &ugen_start_read, |
---|
123 | .f_stop_read = &ugen_stop_io, |
---|
124 | .f_start_write = &ugen_start_write, |
---|
125 | .f_stop_write = &ugen_stop_io, |
---|
126 | }; |
---|
127 | |
---|
128 | #ifdef USB_DEBUG |
---|
129 | static int ugen_debug = 0; |
---|
130 | |
---|
131 | static SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB generic"); |
---|
132 | SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &ugen_debug, |
---|
133 | 0, "Debug level"); |
---|
134 | TUNABLE_INT("hw.usb.ugen.debug", &ugen_debug); |
---|
135 | #endif |
---|
136 | |
---|
137 | |
---|
138 | /* prototypes */ |
---|
139 | |
---|
140 | static int |
---|
141 | ugen_transfer_setup(struct usb_fifo *f, |
---|
142 | const struct usb_config *setup, uint8_t n_setup) |
---|
143 | { |
---|
144 | struct usb_endpoint *ep = usb_fifo_softc(f); |
---|
145 | struct usb_device *udev = f->udev; |
---|
146 | uint8_t iface_index = ep->iface_index; |
---|
147 | int error; |
---|
148 | |
---|
149 | mtx_unlock(f->priv_mtx); |
---|
150 | |
---|
151 | /* |
---|
152 | * "usbd_transfer_setup()" can sleep so one needs to make a wrapper, |
---|
153 | * exiting the mutex and checking things |
---|
154 | */ |
---|
155 | error = usbd_transfer_setup(udev, &iface_index, f->xfer, |
---|
156 | setup, n_setup, f, f->priv_mtx); |
---|
157 | if (error == 0) { |
---|
158 | |
---|
159 | if (f->xfer[0]->nframes == 1) { |
---|
160 | error = usb_fifo_alloc_buffer(f, |
---|
161 | f->xfer[0]->max_data_length, 2); |
---|
162 | } else { |
---|
163 | error = usb_fifo_alloc_buffer(f, |
---|
164 | f->xfer[0]->max_frame_size, |
---|
165 | 2 * f->xfer[0]->nframes); |
---|
166 | } |
---|
167 | if (error) { |
---|
168 | usbd_transfer_unsetup(f->xfer, n_setup); |
---|
169 | } |
---|
170 | } |
---|
171 | mtx_lock(f->priv_mtx); |
---|
172 | |
---|
173 | return (error); |
---|
174 | } |
---|
175 | |
---|
176 | static int |
---|
177 | ugen_open(struct usb_fifo *f, int fflags) |
---|
178 | { |
---|
179 | struct usb_endpoint *ep = usb_fifo_softc(f); |
---|
180 | struct usb_endpoint_descriptor *ed = ep->edesc; |
---|
181 | uint8_t type; |
---|
182 | |
---|
183 | DPRINTFN(6, "flag=0x%x\n", fflags); |
---|
184 | |
---|
185 | mtx_lock(f->priv_mtx); |
---|
186 | switch (usbd_get_speed(f->udev)) { |
---|
187 | case USB_SPEED_LOW: |
---|
188 | case USB_SPEED_FULL: |
---|
189 | f->nframes = UGEN_HW_FRAMES; |
---|
190 | f->bufsize = UGEN_BULK_FS_BUFFER_SIZE; |
---|
191 | break; |
---|
192 | default: |
---|
193 | f->nframes = UGEN_HW_FRAMES * 8; |
---|
194 | f->bufsize = UGEN_BULK_HS_BUFFER_SIZE; |
---|
195 | break; |
---|
196 | } |
---|
197 | |
---|
198 | type = ed->bmAttributes & UE_XFERTYPE; |
---|
199 | if (type == UE_INTERRUPT) { |
---|
200 | f->bufsize = 0; /* use "wMaxPacketSize" */ |
---|
201 | } |
---|
202 | f->timeout = USB_NO_TIMEOUT; |
---|
203 | f->flag_short = 0; |
---|
204 | f->fifo_zlp = 0; |
---|
205 | mtx_unlock(f->priv_mtx); |
---|
206 | |
---|
207 | return (0); |
---|
208 | } |
---|
209 | |
---|
210 | static void |
---|
211 | ugen_close(struct usb_fifo *f, int fflags) |
---|
212 | { |
---|
213 | DPRINTFN(6, "flag=0x%x\n", fflags); |
---|
214 | |
---|
215 | /* cleanup */ |
---|
216 | |
---|
217 | mtx_lock(f->priv_mtx); |
---|
218 | usbd_transfer_stop(f->xfer[0]); |
---|
219 | usbd_transfer_stop(f->xfer[1]); |
---|
220 | mtx_unlock(f->priv_mtx); |
---|
221 | |
---|
222 | usbd_transfer_unsetup(f->xfer, 2); |
---|
223 | usb_fifo_free_buffer(f); |
---|
224 | |
---|
225 | if (ugen_fs_uninit(f)) { |
---|
226 | /* ignore any errors - we are closing */ |
---|
227 | DPRINTFN(6, "no FIFOs\n"); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | static int |
---|
232 | ugen_open_pipe_write(struct usb_fifo *f) |
---|
233 | { |
---|
234 | struct usb_config usb_config[2]; |
---|
235 | struct usb_endpoint *ep = usb_fifo_softc(f); |
---|
236 | struct usb_endpoint_descriptor *ed = ep->edesc; |
---|
237 | |
---|
238 | mtx_assert(f->priv_mtx, MA_OWNED); |
---|
239 | |
---|
240 | if (f->xfer[0] || f->xfer[1]) { |
---|
241 | /* transfers are already opened */ |
---|
242 | return (0); |
---|
243 | } |
---|
244 | memset(usb_config, 0, sizeof(usb_config)); |
---|
245 | |
---|
246 | usb_config[1].type = UE_CONTROL; |
---|
247 | usb_config[1].endpoint = 0; |
---|
248 | usb_config[1].direction = UE_DIR_ANY; |
---|
249 | usb_config[1].timeout = 1000; /* 1 second */ |
---|
250 | usb_config[1].interval = 50;/* 50 milliseconds */ |
---|
251 | usb_config[1].bufsize = sizeof(struct usb_device_request); |
---|
252 | usb_config[1].callback = &ugen_write_clear_stall_callback; |
---|
253 | usb_config[1].usb_mode = USB_MODE_HOST; |
---|
254 | |
---|
255 | usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; |
---|
256 | usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; |
---|
257 | usb_config[0].direction = UE_DIR_TX; |
---|
258 | usb_config[0].interval = USB_DEFAULT_INTERVAL; |
---|
259 | usb_config[0].flags.proxy_buffer = 1; |
---|
260 | usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ |
---|
261 | |
---|
262 | switch (ed->bmAttributes & UE_XFERTYPE) { |
---|
263 | case UE_INTERRUPT: |
---|
264 | case UE_BULK: |
---|
265 | if (f->flag_short) { |
---|
266 | usb_config[0].flags.force_short_xfer = 1; |
---|
267 | } |
---|
268 | usb_config[0].callback = &ugen_ctrl_write_callback; |
---|
269 | usb_config[0].timeout = f->timeout; |
---|
270 | usb_config[0].frames = 1; |
---|
271 | usb_config[0].bufsize = f->bufsize; |
---|
272 | if (ugen_transfer_setup(f, usb_config, 2)) { |
---|
273 | return (EIO); |
---|
274 | } |
---|
275 | /* first transfer does not clear stall */ |
---|
276 | f->flag_stall = 0; |
---|
277 | break; |
---|
278 | |
---|
279 | case UE_ISOCHRONOUS: |
---|
280 | usb_config[0].flags.short_xfer_ok = 1; |
---|
281 | usb_config[0].bufsize = 0; /* use default */ |
---|
282 | usb_config[0].frames = f->nframes; |
---|
283 | usb_config[0].callback = &ugen_isoc_write_callback; |
---|
284 | usb_config[0].timeout = 0; |
---|
285 | |
---|
286 | /* clone configuration */ |
---|
287 | usb_config[1] = usb_config[0]; |
---|
288 | |
---|
289 | if (ugen_transfer_setup(f, usb_config, 2)) { |
---|
290 | return (EIO); |
---|
291 | } |
---|
292 | break; |
---|
293 | default: |
---|
294 | return (EINVAL); |
---|
295 | } |
---|
296 | return (0); |
---|
297 | } |
---|
298 | |
---|
299 | static int |
---|
300 | ugen_open_pipe_read(struct usb_fifo *f) |
---|
301 | { |
---|
302 | struct usb_config usb_config[2]; |
---|
303 | struct usb_endpoint *ep = usb_fifo_softc(f); |
---|
304 | struct usb_endpoint_descriptor *ed = ep->edesc; |
---|
305 | |
---|
306 | mtx_assert(f->priv_mtx, MA_OWNED); |
---|
307 | |
---|
308 | if (f->xfer[0] || f->xfer[1]) { |
---|
309 | /* transfers are already opened */ |
---|
310 | return (0); |
---|
311 | } |
---|
312 | memset(usb_config, 0, sizeof(usb_config)); |
---|
313 | |
---|
314 | usb_config[1].type = UE_CONTROL; |
---|
315 | usb_config[1].endpoint = 0; |
---|
316 | usb_config[1].direction = UE_DIR_ANY; |
---|
317 | usb_config[1].timeout = 1000; /* 1 second */ |
---|
318 | usb_config[1].interval = 50;/* 50 milliseconds */ |
---|
319 | usb_config[1].bufsize = sizeof(struct usb_device_request); |
---|
320 | usb_config[1].callback = &ugen_read_clear_stall_callback; |
---|
321 | usb_config[1].usb_mode = USB_MODE_HOST; |
---|
322 | |
---|
323 | usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; |
---|
324 | usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; |
---|
325 | usb_config[0].direction = UE_DIR_RX; |
---|
326 | usb_config[0].interval = USB_DEFAULT_INTERVAL; |
---|
327 | usb_config[0].flags.proxy_buffer = 1; |
---|
328 | usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ |
---|
329 | |
---|
330 | switch (ed->bmAttributes & UE_XFERTYPE) { |
---|
331 | case UE_INTERRUPT: |
---|
332 | case UE_BULK: |
---|
333 | if (f->flag_short) { |
---|
334 | usb_config[0].flags.short_xfer_ok = 1; |
---|
335 | } |
---|
336 | usb_config[0].timeout = f->timeout; |
---|
337 | usb_config[0].frames = 1; |
---|
338 | usb_config[0].callback = &ugen_ctrl_read_callback; |
---|
339 | usb_config[0].bufsize = f->bufsize; |
---|
340 | |
---|
341 | if (ugen_transfer_setup(f, usb_config, 2)) { |
---|
342 | return (EIO); |
---|
343 | } |
---|
344 | /* first transfer does not clear stall */ |
---|
345 | f->flag_stall = 0; |
---|
346 | break; |
---|
347 | |
---|
348 | case UE_ISOCHRONOUS: |
---|
349 | usb_config[0].flags.short_xfer_ok = 1; |
---|
350 | usb_config[0].bufsize = 0; /* use default */ |
---|
351 | usb_config[0].frames = f->nframes; |
---|
352 | usb_config[0].callback = &ugen_isoc_read_callback; |
---|
353 | usb_config[0].timeout = 0; |
---|
354 | |
---|
355 | /* clone configuration */ |
---|
356 | usb_config[1] = usb_config[0]; |
---|
357 | |
---|
358 | if (ugen_transfer_setup(f, usb_config, 2)) { |
---|
359 | return (EIO); |
---|
360 | } |
---|
361 | break; |
---|
362 | |
---|
363 | default: |
---|
364 | return (EINVAL); |
---|
365 | } |
---|
366 | return (0); |
---|
367 | } |
---|
368 | |
---|
369 | static void |
---|
370 | ugen_start_read(struct usb_fifo *f) |
---|
371 | { |
---|
372 | /* check that pipes are open */ |
---|
373 | if (ugen_open_pipe_read(f)) { |
---|
374 | /* signal error */ |
---|
375 | usb_fifo_put_data_error(f); |
---|
376 | } |
---|
377 | /* start transfers */ |
---|
378 | usbd_transfer_start(f->xfer[0]); |
---|
379 | usbd_transfer_start(f->xfer[1]); |
---|
380 | } |
---|
381 | |
---|
382 | static void |
---|
383 | ugen_start_write(struct usb_fifo *f) |
---|
384 | { |
---|
385 | /* check that pipes are open */ |
---|
386 | if (ugen_open_pipe_write(f)) { |
---|
387 | /* signal error */ |
---|
388 | usb_fifo_get_data_error(f); |
---|
389 | } |
---|
390 | /* start transfers */ |
---|
391 | usbd_transfer_start(f->xfer[0]); |
---|
392 | usbd_transfer_start(f->xfer[1]); |
---|
393 | } |
---|
394 | |
---|
395 | static void |
---|
396 | ugen_stop_io(struct usb_fifo *f) |
---|
397 | { |
---|
398 | /* stop transfers */ |
---|
399 | usbd_transfer_stop(f->xfer[0]); |
---|
400 | usbd_transfer_stop(f->xfer[1]); |
---|
401 | } |
---|
402 | |
---|
403 | static void |
---|
404 | ugen_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error) |
---|
405 | { |
---|
406 | struct usb_fifo *f = usbd_xfer_softc(xfer); |
---|
407 | struct usb_mbuf *m; |
---|
408 | |
---|
409 | DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); |
---|
410 | |
---|
411 | switch (USB_GET_STATE(xfer)) { |
---|
412 | case USB_ST_TRANSFERRED: |
---|
413 | if (xfer->actlen == 0) { |
---|
414 | if (f->fifo_zlp != 4) { |
---|
415 | f->fifo_zlp++; |
---|
416 | } else { |
---|
417 | /* |
---|
418 | * Throttle a little bit we have multiple ZLPs |
---|
419 | * in a row! |
---|
420 | */ |
---|
421 | xfer->interval = 64; /* ms */ |
---|
422 | } |
---|
423 | } else { |
---|
424 | /* clear throttle */ |
---|
425 | xfer->interval = 0; |
---|
426 | f->fifo_zlp = 0; |
---|
427 | } |
---|
428 | usb_fifo_put_data(f, xfer->frbuffers, 0, |
---|
429 | xfer->actlen, 1); |
---|
430 | |
---|
431 | case USB_ST_SETUP: |
---|
432 | if (f->flag_stall) { |
---|
433 | usbd_transfer_start(f->xfer[1]); |
---|
434 | break; |
---|
435 | } |
---|
436 | USB_IF_POLL(&f->free_q, m); |
---|
437 | if (m) { |
---|
438 | usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); |
---|
439 | usbd_transfer_submit(xfer); |
---|
440 | } |
---|
441 | break; |
---|
442 | |
---|
443 | default: /* Error */ |
---|
444 | if (xfer->error != USB_ERR_CANCELLED) { |
---|
445 | /* send a zero length packet to userland */ |
---|
446 | usb_fifo_put_data(f, xfer->frbuffers, 0, 0, 1); |
---|
447 | f->flag_stall = 1; |
---|
448 | f->fifo_zlp = 0; |
---|
449 | usbd_transfer_start(f->xfer[1]); |
---|
450 | } |
---|
451 | break; |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | static void |
---|
456 | ugen_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error) |
---|
457 | { |
---|
458 | struct usb_fifo *f = usbd_xfer_softc(xfer); |
---|
459 | usb_frlength_t actlen; |
---|
460 | |
---|
461 | DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); |
---|
462 | |
---|
463 | switch (USB_GET_STATE(xfer)) { |
---|
464 | case USB_ST_SETUP: |
---|
465 | case USB_ST_TRANSFERRED: |
---|
466 | /* |
---|
467 | * If writing is in stall, just jump to clear stall |
---|
468 | * callback and solve the situation. |
---|
469 | */ |
---|
470 | if (f->flag_stall) { |
---|
471 | usbd_transfer_start(f->xfer[1]); |
---|
472 | break; |
---|
473 | } |
---|
474 | /* |
---|
475 | * Write data, setup and perform hardware transfer. |
---|
476 | */ |
---|
477 | if (usb_fifo_get_data(f, xfer->frbuffers, 0, |
---|
478 | xfer->max_data_length, &actlen, 0)) { |
---|
479 | usbd_xfer_set_frame_len(xfer, 0, actlen); |
---|
480 | usbd_transfer_submit(xfer); |
---|
481 | } |
---|
482 | break; |
---|
483 | |
---|
484 | default: /* Error */ |
---|
485 | if (xfer->error != USB_ERR_CANCELLED) { |
---|
486 | f->flag_stall = 1; |
---|
487 | usbd_transfer_start(f->xfer[1]); |
---|
488 | } |
---|
489 | break; |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | static void |
---|
494 | ugen_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) |
---|
495 | { |
---|
496 | struct usb_fifo *f = usbd_xfer_softc(xfer); |
---|
497 | struct usb_xfer *xfer_other = f->xfer[0]; |
---|
498 | |
---|
499 | if (f->flag_stall == 0) { |
---|
500 | /* nothing to do */ |
---|
501 | return; |
---|
502 | } |
---|
503 | if (usbd_clear_stall_callback(xfer, xfer_other)) { |
---|
504 | DPRINTFN(5, "f=%p: stall cleared\n", f); |
---|
505 | f->flag_stall = 0; |
---|
506 | usbd_transfer_start(xfer_other); |
---|
507 | } |
---|
508 | } |
---|
509 | |
---|
510 | static void |
---|
511 | ugen_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) |
---|
512 | { |
---|
513 | struct usb_fifo *f = usbd_xfer_softc(xfer); |
---|
514 | struct usb_xfer *xfer_other = f->xfer[0]; |
---|
515 | |
---|
516 | if (f->flag_stall == 0) { |
---|
517 | /* nothing to do */ |
---|
518 | return; |
---|
519 | } |
---|
520 | if (usbd_clear_stall_callback(xfer, xfer_other)) { |
---|
521 | DPRINTFN(5, "f=%p: stall cleared\n", f); |
---|
522 | f->flag_stall = 0; |
---|
523 | usbd_transfer_start(xfer_other); |
---|
524 | } |
---|
525 | } |
---|
526 | |
---|
527 | static void |
---|
528 | ugen_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error) |
---|
529 | { |
---|
530 | struct usb_fifo *f = usbd_xfer_softc(xfer); |
---|
531 | usb_frlength_t offset; |
---|
532 | usb_frcount_t n; |
---|
533 | |
---|
534 | DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); |
---|
535 | |
---|
536 | switch (USB_GET_STATE(xfer)) { |
---|
537 | case USB_ST_TRANSFERRED: |
---|
538 | |
---|
539 | DPRINTFN(6, "actlen=%d\n", xfer->actlen); |
---|
540 | |
---|
541 | offset = 0; |
---|
542 | |
---|
543 | for (n = 0; n != xfer->aframes; n++) { |
---|
544 | usb_fifo_put_data(f, xfer->frbuffers, offset, |
---|
545 | xfer->frlengths[n], 1); |
---|
546 | offset += xfer->max_frame_size; |
---|
547 | } |
---|
548 | |
---|
549 | case USB_ST_SETUP: |
---|
550 | tr_setup: |
---|
551 | for (n = 0; n != xfer->nframes; n++) { |
---|
552 | /* setup size for next transfer */ |
---|
553 | usbd_xfer_set_frame_len(xfer, n, xfer->max_frame_size); |
---|
554 | } |
---|
555 | usbd_transfer_submit(xfer); |
---|
556 | break; |
---|
557 | |
---|
558 | default: /* Error */ |
---|
559 | if (xfer->error == USB_ERR_CANCELLED) { |
---|
560 | break; |
---|
561 | } |
---|
562 | goto tr_setup; |
---|
563 | } |
---|
564 | } |
---|
565 | |
---|
566 | static void |
---|
567 | ugen_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error) |
---|
568 | { |
---|
569 | struct usb_fifo *f = usbd_xfer_softc(xfer); |
---|
570 | usb_frlength_t actlen; |
---|
571 | usb_frlength_t offset; |
---|
572 | usb_frcount_t n; |
---|
573 | |
---|
574 | DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); |
---|
575 | |
---|
576 | switch (USB_GET_STATE(xfer)) { |
---|
577 | case USB_ST_TRANSFERRED: |
---|
578 | case USB_ST_SETUP: |
---|
579 | tr_setup: |
---|
580 | offset = 0; |
---|
581 | for (n = 0; n != xfer->nframes; n++) { |
---|
582 | if (usb_fifo_get_data(f, xfer->frbuffers, offset, |
---|
583 | xfer->max_frame_size, &actlen, 1)) { |
---|
584 | usbd_xfer_set_frame_len(xfer, n, actlen); |
---|
585 | offset += actlen; |
---|
586 | } else { |
---|
587 | break; |
---|
588 | } |
---|
589 | } |
---|
590 | |
---|
591 | for (; n != xfer->nframes; n++) { |
---|
592 | /* fill in zero frames */ |
---|
593 | usbd_xfer_set_frame_len(xfer, n, 0); |
---|
594 | } |
---|
595 | usbd_transfer_submit(xfer); |
---|
596 | break; |
---|
597 | |
---|
598 | default: /* Error */ |
---|
599 | if (xfer->error == USB_ERR_CANCELLED) { |
---|
600 | break; |
---|
601 | } |
---|
602 | goto tr_setup; |
---|
603 | } |
---|
604 | } |
---|
605 | |
---|
606 | static int |
---|
607 | ugen_set_config(struct usb_fifo *f, uint8_t index) |
---|
608 | { |
---|
609 | DPRINTFN(2, "index %u\n", index); |
---|
610 | |
---|
611 | if (f->udev->flags.usb_mode != USB_MODE_HOST) { |
---|
612 | /* not possible in device side mode */ |
---|
613 | return (ENOTTY); |
---|
614 | } |
---|
615 | |
---|
616 | /* make sure all FIFO's are gone */ |
---|
617 | /* else there can be a deadlock */ |
---|
618 | if (ugen_fs_uninit(f)) { |
---|
619 | /* ignore any errors */ |
---|
620 | DPRINTFN(6, "no FIFOs\n"); |
---|
621 | } |
---|
622 | |
---|
623 | if (usbd_start_set_config(f->udev, index) != 0) |
---|
624 | return (EIO); |
---|
625 | |
---|
626 | return (0); |
---|
627 | } |
---|
628 | |
---|
629 | static int |
---|
630 | ugen_set_interface(struct usb_fifo *f, |
---|
631 | uint8_t iface_index, uint8_t alt_index) |
---|
632 | { |
---|
633 | DPRINTFN(2, "%u, %u\n", iface_index, alt_index); |
---|
634 | |
---|
635 | if (f->udev->flags.usb_mode != USB_MODE_HOST) { |
---|
636 | /* not possible in device side mode */ |
---|
637 | return (ENOTTY); |
---|
638 | } |
---|
639 | /* make sure all FIFO's are gone */ |
---|
640 | /* else there can be a deadlock */ |
---|
641 | if (ugen_fs_uninit(f)) { |
---|
642 | /* ignore any errors */ |
---|
643 | DPRINTFN(6, "no FIFOs\n"); |
---|
644 | } |
---|
645 | /* change setting - will free generic FIFOs, if any */ |
---|
646 | if (usbd_set_alt_interface_index(f->udev, iface_index, alt_index)) { |
---|
647 | return (EIO); |
---|
648 | } |
---|
649 | /* probe and attach */ |
---|
650 | if (usb_probe_and_attach(f->udev, iface_index)) { |
---|
651 | return (EIO); |
---|
652 | } |
---|
653 | return (0); |
---|
654 | } |
---|
655 | |
---|
656 | /*------------------------------------------------------------------------* |
---|
657 | * ugen_get_cdesc |
---|
658 | * |
---|
659 | * This function will retrieve the complete configuration descriptor |
---|
660 | * at the given index. |
---|
661 | *------------------------------------------------------------------------*/ |
---|
662 | static int |
---|
663 | ugen_get_cdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd) |
---|
664 | { |
---|
665 | struct usb_config_descriptor *cdesc; |
---|
666 | struct usb_device *udev = f->udev; |
---|
667 | int error; |
---|
668 | uint16_t len; |
---|
669 | uint8_t free_data; |
---|
670 | |
---|
671 | DPRINTFN(6, "\n"); |
---|
672 | |
---|
673 | if (ugd->ugd_data == NULL) { |
---|
674 | /* userland pointer should not be zero */ |
---|
675 | return (EINVAL); |
---|
676 | } |
---|
677 | if ((ugd->ugd_config_index == USB_UNCONFIG_INDEX) || |
---|
678 | (ugd->ugd_config_index == udev->curr_config_index)) { |
---|
679 | cdesc = usbd_get_config_descriptor(udev); |
---|
680 | if (cdesc == NULL) { |
---|
681 | return (ENXIO); |
---|
682 | } |
---|
683 | free_data = 0; |
---|
684 | |
---|
685 | } else { |
---|
686 | if (usbd_req_get_config_desc_full(udev, |
---|
687 | NULL, &cdesc, M_USBDEV, |
---|
688 | ugd->ugd_config_index)) { |
---|
689 | return (ENXIO); |
---|
690 | } |
---|
691 | free_data = 1; |
---|
692 | } |
---|
693 | |
---|
694 | len = UGETW(cdesc->wTotalLength); |
---|
695 | if (len > ugd->ugd_maxlen) { |
---|
696 | len = ugd->ugd_maxlen; |
---|
697 | } |
---|
698 | DPRINTFN(6, "len=%u\n", len); |
---|
699 | |
---|
700 | ugd->ugd_actlen = len; |
---|
701 | ugd->ugd_offset = 0; |
---|
702 | |
---|
703 | error = copyout(cdesc, ugd->ugd_data, len); |
---|
704 | |
---|
705 | if (free_data) { |
---|
706 | free(cdesc, M_USBDEV); |
---|
707 | } |
---|
708 | return (error); |
---|
709 | } |
---|
710 | |
---|
711 | /* |
---|
712 | * This function is called having the enumeration SX locked which |
---|
713 | * protects the scratch area used. |
---|
714 | */ |
---|
715 | static int |
---|
716 | ugen_get_sdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd) |
---|
717 | { |
---|
718 | void *ptr; |
---|
719 | uint16_t size; |
---|
720 | int error; |
---|
721 | |
---|
722 | ptr = f->udev->scratch.data; |
---|
723 | size = sizeof(f->udev->scratch.data); |
---|
724 | |
---|
725 | if (usbd_req_get_string_desc(f->udev, NULL, ptr, |
---|
726 | size, ugd->ugd_lang_id, ugd->ugd_string_index)) { |
---|
727 | error = EINVAL; |
---|
728 | } else { |
---|
729 | |
---|
730 | if (size > ((uint8_t *)ptr)[0]) { |
---|
731 | size = ((uint8_t *)ptr)[0]; |
---|
732 | } |
---|
733 | if (size > ugd->ugd_maxlen) { |
---|
734 | size = ugd->ugd_maxlen; |
---|
735 | } |
---|
736 | ugd->ugd_actlen = size; |
---|
737 | ugd->ugd_offset = 0; |
---|
738 | |
---|
739 | error = copyout(ptr, ugd->ugd_data, size); |
---|
740 | } |
---|
741 | return (error); |
---|
742 | } |
---|
743 | |
---|
744 | /*------------------------------------------------------------------------* |
---|
745 | * ugen_get_iface_driver |
---|
746 | * |
---|
747 | * This function generates an USB interface description for userland. |
---|
748 | * |
---|
749 | * Returns: |
---|
750 | * 0: Success |
---|
751 | * Else: Failure |
---|
752 | *------------------------------------------------------------------------*/ |
---|
753 | static int |
---|
754 | ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd) |
---|
755 | { |
---|
756 | struct usb_device *udev = f->udev; |
---|
757 | struct usb_interface *iface; |
---|
758 | const char *ptr; |
---|
759 | const char *desc; |
---|
760 | unsigned int len; |
---|
761 | unsigned int maxlen; |
---|
762 | char buf[128]; |
---|
763 | int error; |
---|
764 | |
---|
765 | DPRINTFN(6, "\n"); |
---|
766 | |
---|
767 | if ((ugd->ugd_data == NULL) || (ugd->ugd_maxlen == 0)) { |
---|
768 | /* userland pointer should not be zero */ |
---|
769 | return (EINVAL); |
---|
770 | } |
---|
771 | |
---|
772 | iface = usbd_get_iface(udev, ugd->ugd_iface_index); |
---|
773 | if ((iface == NULL) || (iface->idesc == NULL)) { |
---|
774 | /* invalid interface index */ |
---|
775 | return (EINVAL); |
---|
776 | } |
---|
777 | |
---|
778 | /* read out device nameunit string, if any */ |
---|
779 | if ((iface->subdev != NULL) && |
---|
780 | device_is_attached(iface->subdev) && |
---|
781 | (ptr = device_get_nameunit(iface->subdev)) && |
---|
782 | (desc = device_get_desc(iface->subdev))) { |
---|
783 | |
---|
784 | /* print description */ |
---|
785 | snprintf(buf, sizeof(buf), "%s: <%s>", ptr, desc); |
---|
786 | |
---|
787 | /* range checks */ |
---|
788 | maxlen = ugd->ugd_maxlen - 1; |
---|
789 | len = strlen(buf); |
---|
790 | if (len > maxlen) |
---|
791 | len = maxlen; |
---|
792 | |
---|
793 | /* update actual length, including terminating zero */ |
---|
794 | ugd->ugd_actlen = len + 1; |
---|
795 | |
---|
796 | /* copy out interface description */ |
---|
797 | error = copyout(buf, ugd->ugd_data, ugd->ugd_actlen); |
---|
798 | } else { |
---|
799 | /* zero length string is default */ |
---|
800 | error = copyout("", ugd->ugd_data, 1); |
---|
801 | } |
---|
802 | return (error); |
---|
803 | } |
---|
804 | |
---|
805 | /*------------------------------------------------------------------------* |
---|
806 | * usb_gen_fill_deviceinfo |
---|
807 | * |
---|
808 | * This function dumps information about an USB device to the |
---|
809 | * structure pointed to by the "di" argument. |
---|
810 | * |
---|
811 | * Returns: |
---|
812 | * 0: Success |
---|
813 | * Else: Failure |
---|
814 | *------------------------------------------------------------------------*/ |
---|
815 | static int |
---|
816 | usb_gen_fill_deviceinfo(struct usb_fifo *f, struct usb_device_info *di) |
---|
817 | { |
---|
818 | struct usb_device *udev; |
---|
819 | struct usb_device *hub; |
---|
820 | |
---|
821 | udev = f->udev; |
---|
822 | |
---|
823 | bzero(di, sizeof(di[0])); |
---|
824 | |
---|
825 | di->udi_bus = device_get_unit(udev->bus->bdev); |
---|
826 | di->udi_addr = udev->address; |
---|
827 | di->udi_index = udev->device_index; |
---|
828 | strlcpy(di->udi_serial, usb_get_serial(udev), sizeof(di->udi_serial)); |
---|
829 | strlcpy(di->udi_vendor, usb_get_manufacturer(udev), sizeof(di->udi_vendor)); |
---|
830 | strlcpy(di->udi_product, usb_get_product(udev), sizeof(di->udi_product)); |
---|
831 | usb_printbcd(di->udi_release, sizeof(di->udi_release), |
---|
832 | UGETW(udev->ddesc.bcdDevice)); |
---|
833 | di->udi_vendorNo = UGETW(udev->ddesc.idVendor); |
---|
834 | di->udi_productNo = UGETW(udev->ddesc.idProduct); |
---|
835 | di->udi_releaseNo = UGETW(udev->ddesc.bcdDevice); |
---|
836 | di->udi_class = udev->ddesc.bDeviceClass; |
---|
837 | di->udi_subclass = udev->ddesc.bDeviceSubClass; |
---|
838 | di->udi_protocol = udev->ddesc.bDeviceProtocol; |
---|
839 | di->udi_config_no = udev->curr_config_no; |
---|
840 | di->udi_config_index = udev->curr_config_index; |
---|
841 | di->udi_power = udev->flags.self_powered ? 0 : udev->power; |
---|
842 | di->udi_speed = udev->speed; |
---|
843 | di->udi_mode = udev->flags.usb_mode; |
---|
844 | di->udi_power_mode = udev->power_mode; |
---|
845 | di->udi_suspended = udev->flags.peer_suspended; |
---|
846 | |
---|
847 | hub = udev->parent_hub; |
---|
848 | if (hub) { |
---|
849 | di->udi_hubaddr = hub->address; |
---|
850 | di->udi_hubindex = hub->device_index; |
---|
851 | di->udi_hubport = udev->port_no; |
---|
852 | } |
---|
853 | return (0); |
---|
854 | } |
---|
855 | |
---|
856 | /*------------------------------------------------------------------------* |
---|
857 | * ugen_check_request |
---|
858 | * |
---|
859 | * Return values: |
---|
860 | * 0: Access allowed |
---|
861 | * Else: No access |
---|
862 | *------------------------------------------------------------------------*/ |
---|
863 | static int |
---|
864 | ugen_check_request(struct usb_device *udev, struct usb_device_request *req) |
---|
865 | { |
---|
866 | struct usb_endpoint *ep; |
---|
867 | int error; |
---|
868 | |
---|
869 | /* |
---|
870 | * Avoid requests that would damage the bus integrity: |
---|
871 | */ |
---|
872 | if (((req->bmRequestType == UT_WRITE_DEVICE) && |
---|
873 | (req->bRequest == UR_SET_ADDRESS)) || |
---|
874 | ((req->bmRequestType == UT_WRITE_DEVICE) && |
---|
875 | (req->bRequest == UR_SET_CONFIG)) || |
---|
876 | ((req->bmRequestType == UT_WRITE_INTERFACE) && |
---|
877 | (req->bRequest == UR_SET_INTERFACE))) { |
---|
878 | /* |
---|
879 | * These requests can be useful for testing USB drivers. |
---|
880 | */ |
---|
881 | error = priv_check(curthread, PRIV_DRIVER); |
---|
882 | if (error) { |
---|
883 | return (error); |
---|
884 | } |
---|
885 | } |
---|
886 | /* |
---|
887 | * Special case - handle clearing of stall |
---|
888 | */ |
---|
889 | if (req->bmRequestType == UT_WRITE_ENDPOINT) { |
---|
890 | |
---|
891 | ep = usbd_get_ep_by_addr(udev, req->wIndex[0]); |
---|
892 | if (ep == NULL) { |
---|
893 | return (EINVAL); |
---|
894 | } |
---|
895 | if ((req->bRequest == UR_CLEAR_FEATURE) && |
---|
896 | (UGETW(req->wValue) == UF_ENDPOINT_HALT)) { |
---|
897 | usbd_clear_data_toggle(udev, ep); |
---|
898 | } |
---|
899 | } |
---|
900 | /* TODO: add more checks to verify the interface index */ |
---|
901 | |
---|
902 | return (0); |
---|
903 | } |
---|
904 | |
---|
905 | int |
---|
906 | ugen_do_request(struct usb_fifo *f, struct usb_ctl_request *ur) |
---|
907 | { |
---|
908 | int error; |
---|
909 | uint16_t len; |
---|
910 | uint16_t actlen; |
---|
911 | |
---|
912 | if (ugen_check_request(f->udev, &ur->ucr_request)) { |
---|
913 | return (EPERM); |
---|
914 | } |
---|
915 | len = UGETW(ur->ucr_request.wLength); |
---|
916 | |
---|
917 | /* check if "ucr_data" is valid */ |
---|
918 | if (len != 0) { |
---|
919 | if (ur->ucr_data == NULL) { |
---|
920 | return (EFAULT); |
---|
921 | } |
---|
922 | } |
---|
923 | /* do the USB request */ |
---|
924 | error = usbd_do_request_flags |
---|
925 | (f->udev, NULL, &ur->ucr_request, ur->ucr_data, |
---|
926 | (ur->ucr_flags & USB_SHORT_XFER_OK) | |
---|
927 | USB_USER_DATA_PTR, &actlen, |
---|
928 | USB_DEFAULT_TIMEOUT); |
---|
929 | |
---|
930 | ur->ucr_actlen = actlen; |
---|
931 | |
---|
932 | if (error) { |
---|
933 | error = EIO; |
---|
934 | } |
---|
935 | return (error); |
---|
936 | } |
---|
937 | |
---|
938 | /*------------------------------------------------------------------------ |
---|
939 | * ugen_re_enumerate |
---|
940 | *------------------------------------------------------------------------*/ |
---|
941 | static int |
---|
942 | ugen_re_enumerate(struct usb_fifo *f) |
---|
943 | { |
---|
944 | struct usb_device *udev = f->udev; |
---|
945 | int error; |
---|
946 | |
---|
947 | /* |
---|
948 | * This request can be useful for testing USB drivers: |
---|
949 | */ |
---|
950 | error = priv_check(curthread, PRIV_DRIVER); |
---|
951 | if (error) { |
---|
952 | return (error); |
---|
953 | } |
---|
954 | if (udev->flags.usb_mode != USB_MODE_HOST) { |
---|
955 | /* not possible in device side mode */ |
---|
956 | DPRINTFN(6, "device mode\n"); |
---|
957 | return (ENOTTY); |
---|
958 | } |
---|
959 | /* make sure all FIFO's are gone */ |
---|
960 | /* else there can be a deadlock */ |
---|
961 | if (ugen_fs_uninit(f)) { |
---|
962 | /* ignore any errors */ |
---|
963 | DPRINTFN(6, "no FIFOs\n"); |
---|
964 | } |
---|
965 | /* start re-enumeration of device */ |
---|
966 | usbd_start_re_enumerate(udev); |
---|
967 | return (0); |
---|
968 | } |
---|
969 | |
---|
970 | int |
---|
971 | ugen_fs_uninit(struct usb_fifo *f) |
---|
972 | { |
---|
973 | if (f->fs_xfer == NULL) { |
---|
974 | return (EINVAL); |
---|
975 | } |
---|
976 | usbd_transfer_unsetup(f->fs_xfer, f->fs_ep_max); |
---|
977 | free(f->fs_xfer, M_USB); |
---|
978 | f->fs_xfer = NULL; |
---|
979 | f->fs_ep_max = 0; |
---|
980 | f->fs_ep_ptr = NULL; |
---|
981 | f->flag_iscomplete = 0; |
---|
982 | usb_fifo_free_buffer(f); |
---|
983 | return (0); |
---|
984 | } |
---|
985 | |
---|
986 | static uint8_t |
---|
987 | ugen_fs_get_complete(struct usb_fifo *f, uint8_t *pindex) |
---|
988 | { |
---|
989 | struct usb_mbuf *m; |
---|
990 | |
---|
991 | USB_IF_DEQUEUE(&f->used_q, m); |
---|
992 | |
---|
993 | if (m) { |
---|
994 | *pindex = *((uint8_t *)(m->cur_data_ptr)); |
---|
995 | |
---|
996 | USB_IF_ENQUEUE(&f->free_q, m); |
---|
997 | |
---|
998 | return (0); /* success */ |
---|
999 | } else { |
---|
1000 | |
---|
1001 | *pindex = 0; /* fix compiler warning */ |
---|
1002 | |
---|
1003 | f->flag_iscomplete = 0; |
---|
1004 | } |
---|
1005 | return (1); /* failure */ |
---|
1006 | } |
---|
1007 | |
---|
1008 | static void |
---|
1009 | ugen_fs_set_complete(struct usb_fifo *f, uint8_t index) |
---|
1010 | { |
---|
1011 | struct usb_mbuf *m; |
---|
1012 | |
---|
1013 | USB_IF_DEQUEUE(&f->free_q, m); |
---|
1014 | |
---|
1015 | if (m == NULL) { |
---|
1016 | /* can happen during close */ |
---|
1017 | DPRINTF("out of buffers\n"); |
---|
1018 | return; |
---|
1019 | } |
---|
1020 | USB_MBUF_RESET(m); |
---|
1021 | |
---|
1022 | *((uint8_t *)(m->cur_data_ptr)) = index; |
---|
1023 | |
---|
1024 | USB_IF_ENQUEUE(&f->used_q, m); |
---|
1025 | |
---|
1026 | f->flag_iscomplete = 1; |
---|
1027 | |
---|
1028 | usb_fifo_wakeup(f); |
---|
1029 | } |
---|
1030 | |
---|
1031 | static int |
---|
1032 | ugen_fs_copy_in(struct usb_fifo *f, uint8_t ep_index) |
---|
1033 | { |
---|
1034 | struct usb_device_request *req; |
---|
1035 | struct usb_xfer *xfer; |
---|
1036 | struct usb_fs_endpoint fs_ep; |
---|
1037 | void *uaddr; /* userland pointer */ |
---|
1038 | void *kaddr; |
---|
1039 | usb_frlength_t offset; |
---|
1040 | usb_frlength_t rem; |
---|
1041 | usb_frcount_t n; |
---|
1042 | uint32_t length; |
---|
1043 | int error; |
---|
1044 | uint8_t isread; |
---|
1045 | |
---|
1046 | if (ep_index >= f->fs_ep_max) { |
---|
1047 | return (EINVAL); |
---|
1048 | } |
---|
1049 | xfer = f->fs_xfer[ep_index]; |
---|
1050 | if (xfer == NULL) { |
---|
1051 | return (EINVAL); |
---|
1052 | } |
---|
1053 | mtx_lock(f->priv_mtx); |
---|
1054 | if (usbd_transfer_pending(xfer)) { |
---|
1055 | mtx_unlock(f->priv_mtx); |
---|
1056 | return (EBUSY); /* should not happen */ |
---|
1057 | } |
---|
1058 | mtx_unlock(f->priv_mtx); |
---|
1059 | |
---|
1060 | error = copyin(f->fs_ep_ptr + |
---|
1061 | ep_index, &fs_ep, sizeof(fs_ep)); |
---|
1062 | if (error) { |
---|
1063 | return (error); |
---|
1064 | } |
---|
1065 | /* security checks */ |
---|
1066 | |
---|
1067 | if (fs_ep.nFrames > xfer->max_frame_count) { |
---|
1068 | xfer->error = USB_ERR_INVAL; |
---|
1069 | goto complete; |
---|
1070 | } |
---|
1071 | if (fs_ep.nFrames == 0) { |
---|
1072 | xfer->error = USB_ERR_INVAL; |
---|
1073 | goto complete; |
---|
1074 | } |
---|
1075 | error = copyin(fs_ep.ppBuffer, |
---|
1076 | &uaddr, sizeof(uaddr)); |
---|
1077 | if (error) { |
---|
1078 | return (error); |
---|
1079 | } |
---|
1080 | /* reset first frame */ |
---|
1081 | usbd_xfer_set_frame_offset(xfer, 0, 0); |
---|
1082 | |
---|
1083 | if (xfer->flags_int.control_xfr) { |
---|
1084 | |
---|
1085 | req = xfer->frbuffers[0].buffer; |
---|
1086 | |
---|
1087 | error = copyin(fs_ep.pLength, |
---|
1088 | &length, sizeof(length)); |
---|
1089 | if (error) { |
---|
1090 | return (error); |
---|
1091 | } |
---|
1092 | if (length != sizeof(*req)) { |
---|
1093 | xfer->error = USB_ERR_INVAL; |
---|
1094 | goto complete; |
---|
1095 | } |
---|
1096 | if (length != 0) { |
---|
1097 | error = copyin(uaddr, req, length); |
---|
1098 | if (error) { |
---|
1099 | return (error); |
---|
1100 | } |
---|
1101 | } |
---|
1102 | if (ugen_check_request(f->udev, req)) { |
---|
1103 | xfer->error = USB_ERR_INVAL; |
---|
1104 | goto complete; |
---|
1105 | } |
---|
1106 | usbd_xfer_set_frame_len(xfer, 0, length); |
---|
1107 | |
---|
1108 | /* Host mode only ! */ |
---|
1109 | if ((req->bmRequestType & |
---|
1110 | (UT_READ | UT_WRITE)) == UT_READ) { |
---|
1111 | isread = 1; |
---|
1112 | } else { |
---|
1113 | isread = 0; |
---|
1114 | } |
---|
1115 | n = 1; |
---|
1116 | offset = sizeof(*req); |
---|
1117 | |
---|
1118 | } else { |
---|
1119 | /* Device and Host mode */ |
---|
1120 | if (USB_GET_DATA_ISREAD(xfer)) { |
---|
1121 | isread = 1; |
---|
1122 | } else { |
---|
1123 | isread = 0; |
---|
1124 | } |
---|
1125 | n = 0; |
---|
1126 | offset = 0; |
---|
1127 | } |
---|
1128 | |
---|
1129 | rem = usbd_xfer_max_len(xfer); |
---|
1130 | xfer->nframes = fs_ep.nFrames; |
---|
1131 | xfer->timeout = fs_ep.timeout; |
---|
1132 | if (xfer->timeout > 65535) { |
---|
1133 | xfer->timeout = 65535; |
---|
1134 | } |
---|
1135 | if (fs_ep.flags & USB_FS_FLAG_SINGLE_SHORT_OK) |
---|
1136 | xfer->flags.short_xfer_ok = 1; |
---|
1137 | else |
---|
1138 | xfer->flags.short_xfer_ok = 0; |
---|
1139 | |
---|
1140 | if (fs_ep.flags & USB_FS_FLAG_MULTI_SHORT_OK) |
---|
1141 | xfer->flags.short_frames_ok = 1; |
---|
1142 | else |
---|
1143 | xfer->flags.short_frames_ok = 0; |
---|
1144 | |
---|
1145 | if (fs_ep.flags & USB_FS_FLAG_FORCE_SHORT) |
---|
1146 | xfer->flags.force_short_xfer = 1; |
---|
1147 | else |
---|
1148 | xfer->flags.force_short_xfer = 0; |
---|
1149 | |
---|
1150 | if (fs_ep.flags & USB_FS_FLAG_CLEAR_STALL) |
---|
1151 | usbd_xfer_set_stall(xfer); |
---|
1152 | else |
---|
1153 | xfer->flags.stall_pipe = 0; |
---|
1154 | |
---|
1155 | for (; n != xfer->nframes; n++) { |
---|
1156 | |
---|
1157 | error = copyin(fs_ep.pLength + n, |
---|
1158 | &length, sizeof(length)); |
---|
1159 | if (error) { |
---|
1160 | break; |
---|
1161 | } |
---|
1162 | usbd_xfer_set_frame_len(xfer, n, length); |
---|
1163 | |
---|
1164 | if (length > rem) { |
---|
1165 | xfer->error = USB_ERR_INVAL; |
---|
1166 | goto complete; |
---|
1167 | } |
---|
1168 | rem -= length; |
---|
1169 | |
---|
1170 | if (!isread) { |
---|
1171 | |
---|
1172 | /* we need to know the source buffer */ |
---|
1173 | error = copyin(fs_ep.ppBuffer + n, |
---|
1174 | &uaddr, sizeof(uaddr)); |
---|
1175 | if (error) { |
---|
1176 | break; |
---|
1177 | } |
---|
1178 | if (xfer->flags_int.isochronous_xfr) { |
---|
1179 | /* get kernel buffer address */ |
---|
1180 | kaddr = xfer->frbuffers[0].buffer; |
---|
1181 | kaddr = USB_ADD_BYTES(kaddr, offset); |
---|
1182 | } else { |
---|
1183 | /* set current frame offset */ |
---|
1184 | usbd_xfer_set_frame_offset(xfer, offset, n); |
---|
1185 | |
---|
1186 | /* get kernel buffer address */ |
---|
1187 | kaddr = xfer->frbuffers[n].buffer; |
---|
1188 | } |
---|
1189 | |
---|
1190 | /* move data */ |
---|
1191 | error = copyin(uaddr, kaddr, length); |
---|
1192 | if (error) { |
---|
1193 | break; |
---|
1194 | } |
---|
1195 | } |
---|
1196 | offset += length; |
---|
1197 | } |
---|
1198 | return (error); |
---|
1199 | |
---|
1200 | complete: |
---|
1201 | mtx_lock(f->priv_mtx); |
---|
1202 | ugen_fs_set_complete(f, ep_index); |
---|
1203 | mtx_unlock(f->priv_mtx); |
---|
1204 | return (0); |
---|
1205 | } |
---|
1206 | |
---|
1207 | static int |
---|
1208 | ugen_fs_copy_out(struct usb_fifo *f, uint8_t ep_index) |
---|
1209 | { |
---|
1210 | struct usb_device_request *req; |
---|
1211 | struct usb_xfer *xfer; |
---|
1212 | struct usb_fs_endpoint fs_ep; |
---|
1213 | struct usb_fs_endpoint *fs_ep_uptr; /* userland ptr */ |
---|
1214 | void *uaddr; /* userland ptr */ |
---|
1215 | void *kaddr; |
---|
1216 | usb_frlength_t offset; |
---|
1217 | usb_frlength_t rem; |
---|
1218 | usb_frcount_t n; |
---|
1219 | uint32_t length; |
---|
1220 | uint32_t temp; |
---|
1221 | int error; |
---|
1222 | uint8_t isread; |
---|
1223 | |
---|
1224 | if (ep_index >= f->fs_ep_max) |
---|
1225 | return (EINVAL); |
---|
1226 | |
---|
1227 | xfer = f->fs_xfer[ep_index]; |
---|
1228 | if (xfer == NULL) |
---|
1229 | return (EINVAL); |
---|
1230 | |
---|
1231 | mtx_lock(f->priv_mtx); |
---|
1232 | if (usbd_transfer_pending(xfer)) { |
---|
1233 | mtx_unlock(f->priv_mtx); |
---|
1234 | return (EBUSY); /* should not happen */ |
---|
1235 | } |
---|
1236 | mtx_unlock(f->priv_mtx); |
---|
1237 | |
---|
1238 | fs_ep_uptr = f->fs_ep_ptr + ep_index; |
---|
1239 | error = copyin(fs_ep_uptr, &fs_ep, sizeof(fs_ep)); |
---|
1240 | if (error) { |
---|
1241 | return (error); |
---|
1242 | } |
---|
1243 | fs_ep.status = xfer->error; |
---|
1244 | fs_ep.aFrames = xfer->aframes; |
---|
1245 | fs_ep.isoc_time_complete = xfer->isoc_time_complete; |
---|
1246 | if (xfer->error) { |
---|
1247 | goto complete; |
---|
1248 | } |
---|
1249 | if (xfer->flags_int.control_xfr) { |
---|
1250 | req = xfer->frbuffers[0].buffer; |
---|
1251 | |
---|
1252 | /* Host mode only ! */ |
---|
1253 | if ((req->bmRequestType & (UT_READ | UT_WRITE)) == UT_READ) { |
---|
1254 | isread = 1; |
---|
1255 | } else { |
---|
1256 | isread = 0; |
---|
1257 | } |
---|
1258 | if (xfer->nframes == 0) |
---|
1259 | n = 0; /* should never happen */ |
---|
1260 | else |
---|
1261 | n = 1; |
---|
1262 | } else { |
---|
1263 | /* Device and Host mode */ |
---|
1264 | if (USB_GET_DATA_ISREAD(xfer)) { |
---|
1265 | isread = 1; |
---|
1266 | } else { |
---|
1267 | isread = 0; |
---|
1268 | } |
---|
1269 | n = 0; |
---|
1270 | } |
---|
1271 | |
---|
1272 | /* Update lengths and copy out data */ |
---|
1273 | |
---|
1274 | rem = usbd_xfer_max_len(xfer); |
---|
1275 | offset = 0; |
---|
1276 | |
---|
1277 | for (; n != xfer->nframes; n++) { |
---|
1278 | |
---|
1279 | /* get initial length into "temp" */ |
---|
1280 | error = copyin(fs_ep.pLength + n, |
---|
1281 | &temp, sizeof(temp)); |
---|
1282 | if (error) { |
---|
1283 | return (error); |
---|
1284 | } |
---|
1285 | if (temp > rem) { |
---|
1286 | /* the userland length has been corrupted */ |
---|
1287 | DPRINTF("corrupt userland length " |
---|
1288 | "%u > %u\n", temp, rem); |
---|
1289 | fs_ep.status = USB_ERR_INVAL; |
---|
1290 | goto complete; |
---|
1291 | } |
---|
1292 | rem -= temp; |
---|
1293 | |
---|
1294 | /* get actual transfer length */ |
---|
1295 | length = xfer->frlengths[n]; |
---|
1296 | if (length > temp) { |
---|
1297 | /* data overflow */ |
---|
1298 | fs_ep.status = USB_ERR_INVAL; |
---|
1299 | DPRINTF("data overflow %u > %u\n", |
---|
1300 | length, temp); |
---|
1301 | goto complete; |
---|
1302 | } |
---|
1303 | if (isread) { |
---|
1304 | |
---|
1305 | /* we need to know the destination buffer */ |
---|
1306 | error = copyin(fs_ep.ppBuffer + n, |
---|
1307 | &uaddr, sizeof(uaddr)); |
---|
1308 | if (error) { |
---|
1309 | return (error); |
---|
1310 | } |
---|
1311 | if (xfer->flags_int.isochronous_xfr) { |
---|
1312 | /* only one frame buffer */ |
---|
1313 | kaddr = USB_ADD_BYTES( |
---|
1314 | xfer->frbuffers[0].buffer, offset); |
---|
1315 | } else { |
---|
1316 | /* multiple frame buffers */ |
---|
1317 | kaddr = xfer->frbuffers[n].buffer; |
---|
1318 | } |
---|
1319 | |
---|
1320 | /* move data */ |
---|
1321 | error = copyout(kaddr, uaddr, length); |
---|
1322 | if (error) { |
---|
1323 | return (error); |
---|
1324 | } |
---|
1325 | } |
---|
1326 | /* |
---|
1327 | * Update offset according to initial length, which is |
---|
1328 | * needed by isochronous transfers! |
---|
1329 | */ |
---|
1330 | offset += temp; |
---|
1331 | |
---|
1332 | /* update length */ |
---|
1333 | error = copyout(&length, |
---|
1334 | fs_ep.pLength + n, sizeof(length)); |
---|
1335 | if (error) { |
---|
1336 | return (error); |
---|
1337 | } |
---|
1338 | } |
---|
1339 | |
---|
1340 | complete: |
---|
1341 | /* update "aFrames" */ |
---|
1342 | error = copyout(&fs_ep.aFrames, &fs_ep_uptr->aFrames, |
---|
1343 | sizeof(fs_ep.aFrames)); |
---|
1344 | if (error) |
---|
1345 | goto done; |
---|
1346 | |
---|
1347 | /* update "isoc_time_complete" */ |
---|
1348 | error = copyout(&fs_ep.isoc_time_complete, |
---|
1349 | &fs_ep_uptr->isoc_time_complete, |
---|
1350 | sizeof(fs_ep.isoc_time_complete)); |
---|
1351 | if (error) |
---|
1352 | goto done; |
---|
1353 | /* update "status" */ |
---|
1354 | error = copyout(&fs_ep.status, &fs_ep_uptr->status, |
---|
1355 | sizeof(fs_ep.status)); |
---|
1356 | done: |
---|
1357 | return (error); |
---|
1358 | } |
---|
1359 | |
---|
1360 | static uint8_t |
---|
1361 | ugen_fifo_in_use(struct usb_fifo *f, int fflags) |
---|
1362 | { |
---|
1363 | struct usb_fifo *f_rx; |
---|
1364 | struct usb_fifo *f_tx; |
---|
1365 | |
---|
1366 | f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX]; |
---|
1367 | f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX]; |
---|
1368 | |
---|
1369 | if ((fflags & FREAD) && f_rx && |
---|
1370 | (f_rx->xfer[0] || f_rx->xfer[1])) { |
---|
1371 | return (1); /* RX FIFO in use */ |
---|
1372 | } |
---|
1373 | if ((fflags & FWRITE) && f_tx && |
---|
1374 | (f_tx->xfer[0] || f_tx->xfer[1])) { |
---|
1375 | return (1); /* TX FIFO in use */ |
---|
1376 | } |
---|
1377 | return (0); /* not in use */ |
---|
1378 | } |
---|
1379 | |
---|
1380 | static int |
---|
1381 | ugen_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags) |
---|
1382 | { |
---|
1383 | struct usb_config usb_config[1]; |
---|
1384 | struct usb_device_request req; |
---|
1385 | union { |
---|
1386 | struct usb_fs_complete *pcomp; |
---|
1387 | struct usb_fs_start *pstart; |
---|
1388 | struct usb_fs_stop *pstop; |
---|
1389 | struct usb_fs_open *popen; |
---|
1390 | struct usb_fs_close *pclose; |
---|
1391 | struct usb_fs_clear_stall_sync *pstall; |
---|
1392 | void *addr; |
---|
1393 | } u; |
---|
1394 | struct usb_endpoint *ep; |
---|
1395 | struct usb_endpoint_descriptor *ed; |
---|
1396 | struct usb_xfer *xfer; |
---|
1397 | int error = 0; |
---|
1398 | uint8_t iface_index; |
---|
1399 | uint8_t isread; |
---|
1400 | uint8_t ep_index; |
---|
1401 | uint8_t pre_scale; |
---|
1402 | |
---|
1403 | u.addr = addr; |
---|
1404 | |
---|
1405 | DPRINTFN(6, "cmd=0x%08lx\n", cmd); |
---|
1406 | |
---|
1407 | switch (cmd) { |
---|
1408 | case USB_FS_COMPLETE: |
---|
1409 | mtx_lock(f->priv_mtx); |
---|
1410 | error = ugen_fs_get_complete(f, &ep_index); |
---|
1411 | mtx_unlock(f->priv_mtx); |
---|
1412 | |
---|
1413 | if (error) { |
---|
1414 | error = EBUSY; |
---|
1415 | break; |
---|
1416 | } |
---|
1417 | u.pcomp->ep_index = ep_index; |
---|
1418 | error = ugen_fs_copy_out(f, u.pcomp->ep_index); |
---|
1419 | break; |
---|
1420 | |
---|
1421 | case USB_FS_START: |
---|
1422 | error = ugen_fs_copy_in(f, u.pstart->ep_index); |
---|
1423 | if (error) |
---|
1424 | break; |
---|
1425 | mtx_lock(f->priv_mtx); |
---|
1426 | xfer = f->fs_xfer[u.pstart->ep_index]; |
---|
1427 | usbd_transfer_start(xfer); |
---|
1428 | mtx_unlock(f->priv_mtx); |
---|
1429 | break; |
---|
1430 | |
---|
1431 | case USB_FS_STOP: |
---|
1432 | if (u.pstop->ep_index >= f->fs_ep_max) { |
---|
1433 | error = EINVAL; |
---|
1434 | break; |
---|
1435 | } |
---|
1436 | mtx_lock(f->priv_mtx); |
---|
1437 | xfer = f->fs_xfer[u.pstart->ep_index]; |
---|
1438 | if (usbd_transfer_pending(xfer)) { |
---|
1439 | usbd_transfer_stop(xfer); |
---|
1440 | /* |
---|
1441 | * Check if the USB transfer was stopped |
---|
1442 | * before it was even started. Else a cancel |
---|
1443 | * callback will be pending. |
---|
1444 | */ |
---|
1445 | if (!xfer->flags_int.transferring) { |
---|
1446 | ugen_fs_set_complete(xfer->priv_sc, |
---|
1447 | USB_P2U(xfer->priv_fifo)); |
---|
1448 | } |
---|
1449 | } |
---|
1450 | mtx_unlock(f->priv_mtx); |
---|
1451 | break; |
---|
1452 | |
---|
1453 | case USB_FS_OPEN: |
---|
1454 | if (u.popen->ep_index >= f->fs_ep_max) { |
---|
1455 | error = EINVAL; |
---|
1456 | break; |
---|
1457 | } |
---|
1458 | if (f->fs_xfer[u.popen->ep_index] != NULL) { |
---|
1459 | error = EBUSY; |
---|
1460 | break; |
---|
1461 | } |
---|
1462 | if (u.popen->max_bufsize > USB_FS_MAX_BUFSIZE) { |
---|
1463 | u.popen->max_bufsize = USB_FS_MAX_BUFSIZE; |
---|
1464 | } |
---|
1465 | if (u.popen->max_frames & USB_FS_MAX_FRAMES_PRE_SCALE) { |
---|
1466 | pre_scale = 1; |
---|
1467 | u.popen->max_frames &= ~USB_FS_MAX_FRAMES_PRE_SCALE; |
---|
1468 | } else { |
---|
1469 | pre_scale = 0; |
---|
1470 | } |
---|
1471 | if (u.popen->max_frames > USB_FS_MAX_FRAMES) { |
---|
1472 | u.popen->max_frames = USB_FS_MAX_FRAMES; |
---|
1473 | break; |
---|
1474 | } |
---|
1475 | if (u.popen->max_frames == 0) { |
---|
1476 | error = EINVAL; |
---|
1477 | break; |
---|
1478 | } |
---|
1479 | ep = usbd_get_ep_by_addr(f->udev, u.popen->ep_no); |
---|
1480 | if (ep == NULL) { |
---|
1481 | error = EINVAL; |
---|
1482 | break; |
---|
1483 | } |
---|
1484 | ed = ep->edesc; |
---|
1485 | if (ed == NULL) { |
---|
1486 | error = ENXIO; |
---|
1487 | break; |
---|
1488 | } |
---|
1489 | iface_index = ep->iface_index; |
---|
1490 | |
---|
1491 | memset(usb_config, 0, sizeof(usb_config)); |
---|
1492 | |
---|
1493 | usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; |
---|
1494 | usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; |
---|
1495 | usb_config[0].direction = ed->bEndpointAddress & (UE_DIR_OUT | UE_DIR_IN); |
---|
1496 | usb_config[0].interval = USB_DEFAULT_INTERVAL; |
---|
1497 | usb_config[0].flags.proxy_buffer = 1; |
---|
1498 | if (pre_scale != 0) |
---|
1499 | usb_config[0].flags.pre_scale_frames = 1; |
---|
1500 | usb_config[0].callback = &ugen_ctrl_fs_callback; |
---|
1501 | usb_config[0].timeout = 0; /* no timeout */ |
---|
1502 | usb_config[0].frames = u.popen->max_frames; |
---|
1503 | usb_config[0].bufsize = u.popen->max_bufsize; |
---|
1504 | usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ |
---|
1505 | |
---|
1506 | if (usb_config[0].type == UE_CONTROL) { |
---|
1507 | if (f->udev->flags.usb_mode != USB_MODE_HOST) { |
---|
1508 | error = EINVAL; |
---|
1509 | break; |
---|
1510 | } |
---|
1511 | } else { |
---|
1512 | |
---|
1513 | isread = ((usb_config[0].endpoint & |
---|
1514 | (UE_DIR_IN | UE_DIR_OUT)) == UE_DIR_IN); |
---|
1515 | |
---|
1516 | if (f->udev->flags.usb_mode != USB_MODE_HOST) { |
---|
1517 | isread = !isread; |
---|
1518 | } |
---|
1519 | /* check permissions */ |
---|
1520 | if (isread) { |
---|
1521 | if (!(fflags & FREAD)) { |
---|
1522 | error = EPERM; |
---|
1523 | break; |
---|
1524 | } |
---|
1525 | } else { |
---|
1526 | if (!(fflags & FWRITE)) { |
---|
1527 | error = EPERM; |
---|
1528 | break; |
---|
1529 | } |
---|
1530 | } |
---|
1531 | } |
---|
1532 | error = usbd_transfer_setup(f->udev, &iface_index, |
---|
1533 | f->fs_xfer + u.popen->ep_index, usb_config, 1, |
---|
1534 | f, f->priv_mtx); |
---|
1535 | if (error == 0) { |
---|
1536 | /* update maximums */ |
---|
1537 | u.popen->max_packet_length = |
---|
1538 | f->fs_xfer[u.popen->ep_index]->max_frame_size; |
---|
1539 | u.popen->max_bufsize = |
---|
1540 | f->fs_xfer[u.popen->ep_index]->max_data_length; |
---|
1541 | /* update number of frames */ |
---|
1542 | u.popen->max_frames = |
---|
1543 | f->fs_xfer[u.popen->ep_index]->nframes; |
---|
1544 | /* store index of endpoint */ |
---|
1545 | f->fs_xfer[u.popen->ep_index]->priv_fifo = |
---|
1546 | ((uint8_t *)0) + u.popen->ep_index; |
---|
1547 | } else { |
---|
1548 | error = ENOMEM; |
---|
1549 | } |
---|
1550 | break; |
---|
1551 | |
---|
1552 | case USB_FS_CLOSE: |
---|
1553 | if (u.pclose->ep_index >= f->fs_ep_max) { |
---|
1554 | error = EINVAL; |
---|
1555 | break; |
---|
1556 | } |
---|
1557 | if (f->fs_xfer[u.pclose->ep_index] == NULL) { |
---|
1558 | error = EINVAL; |
---|
1559 | break; |
---|
1560 | } |
---|
1561 | usbd_transfer_unsetup(f->fs_xfer + u.pclose->ep_index, 1); |
---|
1562 | break; |
---|
1563 | |
---|
1564 | case USB_FS_CLEAR_STALL_SYNC: |
---|
1565 | if (u.pstall->ep_index >= f->fs_ep_max) { |
---|
1566 | error = EINVAL; |
---|
1567 | break; |
---|
1568 | } |
---|
1569 | if (f->fs_xfer[u.pstall->ep_index] == NULL) { |
---|
1570 | error = EINVAL; |
---|
1571 | break; |
---|
1572 | } |
---|
1573 | if (f->udev->flags.usb_mode != USB_MODE_HOST) { |
---|
1574 | error = EINVAL; |
---|
1575 | break; |
---|
1576 | } |
---|
1577 | mtx_lock(f->priv_mtx); |
---|
1578 | error = usbd_transfer_pending(f->fs_xfer[u.pstall->ep_index]); |
---|
1579 | mtx_unlock(f->priv_mtx); |
---|
1580 | |
---|
1581 | if (error) { |
---|
1582 | return (EBUSY); |
---|
1583 | } |
---|
1584 | ep = f->fs_xfer[u.pstall->ep_index]->endpoint; |
---|
1585 | |
---|
1586 | /* setup a clear-stall packet */ |
---|
1587 | req.bmRequestType = UT_WRITE_ENDPOINT; |
---|
1588 | req.bRequest = UR_CLEAR_FEATURE; |
---|
1589 | USETW(req.wValue, UF_ENDPOINT_HALT); |
---|
1590 | req.wIndex[0] = ep->edesc->bEndpointAddress; |
---|
1591 | req.wIndex[1] = 0; |
---|
1592 | USETW(req.wLength, 0); |
---|
1593 | |
---|
1594 | error = usbd_do_request(f->udev, NULL, &req, NULL); |
---|
1595 | if (error == 0) { |
---|
1596 | usbd_clear_data_toggle(f->udev, ep); |
---|
1597 | } else { |
---|
1598 | error = ENXIO; |
---|
1599 | } |
---|
1600 | break; |
---|
1601 | |
---|
1602 | default: |
---|
1603 | error = ENOIOCTL; |
---|
1604 | break; |
---|
1605 | } |
---|
1606 | |
---|
1607 | DPRINTFN(6, "error=%d\n", error); |
---|
1608 | |
---|
1609 | return (error); |
---|
1610 | } |
---|
1611 | |
---|
1612 | static int |
---|
1613 | ugen_set_short_xfer(struct usb_fifo *f, void *addr) |
---|
1614 | { |
---|
1615 | uint8_t t; |
---|
1616 | |
---|
1617 | if (*(int *)addr) |
---|
1618 | t = 1; |
---|
1619 | else |
---|
1620 | t = 0; |
---|
1621 | |
---|
1622 | if (f->flag_short == t) { |
---|
1623 | /* same value like before - accept */ |
---|
1624 | return (0); |
---|
1625 | } |
---|
1626 | if (f->xfer[0] || f->xfer[1]) { |
---|
1627 | /* cannot change this during transfer */ |
---|
1628 | return (EBUSY); |
---|
1629 | } |
---|
1630 | f->flag_short = t; |
---|
1631 | return (0); |
---|
1632 | } |
---|
1633 | |
---|
1634 | static int |
---|
1635 | ugen_set_timeout(struct usb_fifo *f, void *addr) |
---|
1636 | { |
---|
1637 | f->timeout = *(int *)addr; |
---|
1638 | if (f->timeout > 65535) { |
---|
1639 | /* limit user input */ |
---|
1640 | f->timeout = 65535; |
---|
1641 | } |
---|
1642 | return (0); |
---|
1643 | } |
---|
1644 | |
---|
1645 | static int |
---|
1646 | ugen_get_frame_size(struct usb_fifo *f, void *addr) |
---|
1647 | { |
---|
1648 | if (f->xfer[0]) { |
---|
1649 | *(int *)addr = f->xfer[0]->max_frame_size; |
---|
1650 | } else { |
---|
1651 | return (EINVAL); |
---|
1652 | } |
---|
1653 | return (0); |
---|
1654 | } |
---|
1655 | |
---|
1656 | static int |
---|
1657 | ugen_set_buffer_size(struct usb_fifo *f, void *addr) |
---|
1658 | { |
---|
1659 | usb_frlength_t t; |
---|
1660 | |
---|
1661 | if (*(int *)addr < 0) |
---|
1662 | t = 0; /* use "wMaxPacketSize" */ |
---|
1663 | else if (*(int *)addr < (256 * 1024)) |
---|
1664 | t = *(int *)addr; |
---|
1665 | else |
---|
1666 | t = 256 * 1024; |
---|
1667 | |
---|
1668 | if (f->bufsize == t) { |
---|
1669 | /* same value like before - accept */ |
---|
1670 | return (0); |
---|
1671 | } |
---|
1672 | if (f->xfer[0] || f->xfer[1]) { |
---|
1673 | /* cannot change this during transfer */ |
---|
1674 | return (EBUSY); |
---|
1675 | } |
---|
1676 | f->bufsize = t; |
---|
1677 | return (0); |
---|
1678 | } |
---|
1679 | |
---|
1680 | static int |
---|
1681 | ugen_get_buffer_size(struct usb_fifo *f, void *addr) |
---|
1682 | { |
---|
1683 | *(int *)addr = f->bufsize; |
---|
1684 | return (0); |
---|
1685 | } |
---|
1686 | |
---|
1687 | static int |
---|
1688 | ugen_get_iface_desc(struct usb_fifo *f, |
---|
1689 | struct usb_interface_descriptor *idesc) |
---|
1690 | { |
---|
1691 | struct usb_interface *iface; |
---|
1692 | |
---|
1693 | iface = usbd_get_iface(f->udev, f->iface_index); |
---|
1694 | if (iface && iface->idesc) { |
---|
1695 | *idesc = *(iface->idesc); |
---|
1696 | } else { |
---|
1697 | return (EIO); |
---|
1698 | } |
---|
1699 | return (0); |
---|
1700 | } |
---|
1701 | |
---|
1702 | static int |
---|
1703 | ugen_get_endpoint_desc(struct usb_fifo *f, |
---|
1704 | struct usb_endpoint_descriptor *ed) |
---|
1705 | { |
---|
1706 | struct usb_endpoint *ep; |
---|
1707 | |
---|
1708 | ep = usb_fifo_softc(f); |
---|
1709 | |
---|
1710 | if (ep && ep->edesc) { |
---|
1711 | *ed = *ep->edesc; |
---|
1712 | } else { |
---|
1713 | return (EINVAL); |
---|
1714 | } |
---|
1715 | return (0); |
---|
1716 | } |
---|
1717 | |
---|
1718 | static int |
---|
1719 | ugen_set_power_mode(struct usb_fifo *f, int mode) |
---|
1720 | { |
---|
1721 | struct usb_device *udev = f->udev; |
---|
1722 | int err; |
---|
1723 | uint8_t old_mode; |
---|
1724 | |
---|
1725 | if ((udev == NULL) || |
---|
1726 | (udev->parent_hub == NULL)) { |
---|
1727 | return (EINVAL); |
---|
1728 | } |
---|
1729 | err = priv_check(curthread, PRIV_DRIVER); |
---|
1730 | if (err) |
---|
1731 | return (err); |
---|
1732 | |
---|
1733 | /* get old power mode */ |
---|
1734 | old_mode = udev->power_mode; |
---|
1735 | |
---|
1736 | /* if no change, then just return */ |
---|
1737 | if (old_mode == mode) |
---|
1738 | return (0); |
---|
1739 | |
---|
1740 | switch (mode) { |
---|
1741 | case USB_POWER_MODE_OFF: |
---|
1742 | if (udev->flags.usb_mode == USB_MODE_HOST && |
---|
1743 | udev->re_enumerate_wait == USB_RE_ENUM_DONE) { |
---|
1744 | udev->re_enumerate_wait = USB_RE_ENUM_PWR_OFF; |
---|
1745 | } |
---|
1746 | /* set power mode will wake up the explore thread */ |
---|
1747 | break; |
---|
1748 | |
---|
1749 | case USB_POWER_MODE_ON: |
---|
1750 | case USB_POWER_MODE_SAVE: |
---|
1751 | break; |
---|
1752 | |
---|
1753 | case USB_POWER_MODE_RESUME: |
---|
1754 | #if USB_HAVE_POWERD |
---|
1755 | /* let USB-powerd handle resume */ |
---|
1756 | USB_BUS_LOCK(udev->bus); |
---|
1757 | udev->pwr_save.write_refs++; |
---|
1758 | udev->pwr_save.last_xfer_time = ticks; |
---|
1759 | USB_BUS_UNLOCK(udev->bus); |
---|
1760 | |
---|
1761 | /* set new power mode */ |
---|
1762 | usbd_set_power_mode(udev, USB_POWER_MODE_SAVE); |
---|
1763 | |
---|
1764 | /* wait for resume to complete */ |
---|
1765 | usb_pause_mtx(NULL, hz / 4); |
---|
1766 | |
---|
1767 | /* clear write reference */ |
---|
1768 | USB_BUS_LOCK(udev->bus); |
---|
1769 | udev->pwr_save.write_refs--; |
---|
1770 | USB_BUS_UNLOCK(udev->bus); |
---|
1771 | #endif |
---|
1772 | mode = USB_POWER_MODE_SAVE; |
---|
1773 | break; |
---|
1774 | |
---|
1775 | case USB_POWER_MODE_SUSPEND: |
---|
1776 | #if USB_HAVE_POWERD |
---|
1777 | /* let USB-powerd handle suspend */ |
---|
1778 | USB_BUS_LOCK(udev->bus); |
---|
1779 | udev->pwr_save.last_xfer_time = ticks - (256 * hz); |
---|
1780 | USB_BUS_UNLOCK(udev->bus); |
---|
1781 | #endif |
---|
1782 | mode = USB_POWER_MODE_SAVE; |
---|
1783 | break; |
---|
1784 | |
---|
1785 | default: |
---|
1786 | return (EINVAL); |
---|
1787 | } |
---|
1788 | |
---|
1789 | if (err) |
---|
1790 | return (ENXIO); /* I/O failure */ |
---|
1791 | |
---|
1792 | /* if we are powered off we need to re-enumerate first */ |
---|
1793 | if (old_mode == USB_POWER_MODE_OFF) { |
---|
1794 | if (udev->flags.usb_mode == USB_MODE_HOST && |
---|
1795 | udev->re_enumerate_wait == USB_RE_ENUM_DONE) { |
---|
1796 | udev->re_enumerate_wait = USB_RE_ENUM_START; |
---|
1797 | } |
---|
1798 | /* set power mode will wake up the explore thread */ |
---|
1799 | } |
---|
1800 | |
---|
1801 | /* set new power mode */ |
---|
1802 | usbd_set_power_mode(udev, mode); |
---|
1803 | |
---|
1804 | return (0); /* success */ |
---|
1805 | } |
---|
1806 | |
---|
1807 | static int |
---|
1808 | ugen_get_power_mode(struct usb_fifo *f) |
---|
1809 | { |
---|
1810 | struct usb_device *udev = f->udev; |
---|
1811 | |
---|
1812 | if (udev == NULL) |
---|
1813 | return (USB_POWER_MODE_ON); |
---|
1814 | |
---|
1815 | return (udev->power_mode); |
---|
1816 | } |
---|
1817 | |
---|
1818 | static int |
---|
1819 | ugen_get_port_path(struct usb_fifo *f, struct usb_device_port_path *dpp) |
---|
1820 | { |
---|
1821 | struct usb_device *udev = f->udev; |
---|
1822 | struct usb_device *next; |
---|
1823 | unsigned int nlevel = 0; |
---|
1824 | |
---|
1825 | if (udev == NULL) |
---|
1826 | goto error; |
---|
1827 | |
---|
1828 | dpp->udp_bus = device_get_unit(udev->bus->bdev); |
---|
1829 | dpp->udp_index = udev->device_index; |
---|
1830 | |
---|
1831 | /* count port levels */ |
---|
1832 | next = udev; |
---|
1833 | while (next->parent_hub != NULL) { |
---|
1834 | nlevel++; |
---|
1835 | next = next->parent_hub; |
---|
1836 | } |
---|
1837 | |
---|
1838 | /* check if too many levels */ |
---|
1839 | if (nlevel > USB_DEVICE_PORT_PATH_MAX) |
---|
1840 | goto error; |
---|
1841 | |
---|
1842 | /* store total level of ports */ |
---|
1843 | dpp->udp_port_level = nlevel; |
---|
1844 | |
---|
1845 | /* store port index array */ |
---|
1846 | next = udev; |
---|
1847 | while (next->parent_hub != NULL) { |
---|
1848 | dpp->udp_port_no[--nlevel] = next->port_no; |
---|
1849 | next = next->parent_hub; |
---|
1850 | } |
---|
1851 | return (0); /* success */ |
---|
1852 | |
---|
1853 | error: |
---|
1854 | return (EINVAL); /* failure */ |
---|
1855 | } |
---|
1856 | |
---|
1857 | static int |
---|
1858 | ugen_get_power_usage(struct usb_fifo *f) |
---|
1859 | { |
---|
1860 | struct usb_device *udev = f->udev; |
---|
1861 | |
---|
1862 | if (udev == NULL) |
---|
1863 | return (0); |
---|
1864 | |
---|
1865 | return (udev->power); |
---|
1866 | } |
---|
1867 | |
---|
1868 | static int |
---|
1869 | ugen_do_port_feature(struct usb_fifo *f, uint8_t port_no, |
---|
1870 | uint8_t set, uint16_t feature) |
---|
1871 | { |
---|
1872 | struct usb_device *udev = f->udev; |
---|
1873 | struct usb_hub *hub; |
---|
1874 | int err; |
---|
1875 | |
---|
1876 | err = priv_check(curthread, PRIV_DRIVER); |
---|
1877 | if (err) { |
---|
1878 | return (err); |
---|
1879 | } |
---|
1880 | if (port_no == 0) { |
---|
1881 | return (EINVAL); |
---|
1882 | } |
---|
1883 | if ((udev == NULL) || |
---|
1884 | (udev->hub == NULL)) { |
---|
1885 | return (EINVAL); |
---|
1886 | } |
---|
1887 | hub = udev->hub; |
---|
1888 | |
---|
1889 | if (port_no > hub->nports) { |
---|
1890 | return (EINVAL); |
---|
1891 | } |
---|
1892 | if (set) |
---|
1893 | err = usbd_req_set_port_feature(udev, |
---|
1894 | NULL, port_no, feature); |
---|
1895 | else |
---|
1896 | err = usbd_req_clear_port_feature(udev, |
---|
1897 | NULL, port_no, feature); |
---|
1898 | |
---|
1899 | if (err) |
---|
1900 | return (ENXIO); /* failure */ |
---|
1901 | |
---|
1902 | return (0); /* success */ |
---|
1903 | } |
---|
1904 | |
---|
1905 | static int |
---|
1906 | ugen_iface_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags) |
---|
1907 | { |
---|
1908 | struct usb_fifo *f_rx; |
---|
1909 | struct usb_fifo *f_tx; |
---|
1910 | int error = 0; |
---|
1911 | |
---|
1912 | f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX]; |
---|
1913 | f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX]; |
---|
1914 | |
---|
1915 | switch (cmd) { |
---|
1916 | case USB_SET_RX_SHORT_XFER: |
---|
1917 | if (fflags & FREAD) { |
---|
1918 | error = ugen_set_short_xfer(f_rx, addr); |
---|
1919 | } else { |
---|
1920 | error = EINVAL; |
---|
1921 | } |
---|
1922 | break; |
---|
1923 | |
---|
1924 | case USB_SET_TX_FORCE_SHORT: |
---|
1925 | if (fflags & FWRITE) { |
---|
1926 | error = ugen_set_short_xfer(f_tx, addr); |
---|
1927 | } else { |
---|
1928 | error = EINVAL; |
---|
1929 | } |
---|
1930 | break; |
---|
1931 | |
---|
1932 | case USB_SET_RX_TIMEOUT: |
---|
1933 | if (fflags & FREAD) { |
---|
1934 | error = ugen_set_timeout(f_rx, addr); |
---|
1935 | } else { |
---|
1936 | error = EINVAL; |
---|
1937 | } |
---|
1938 | break; |
---|
1939 | |
---|
1940 | case USB_SET_TX_TIMEOUT: |
---|
1941 | if (fflags & FWRITE) { |
---|
1942 | error = ugen_set_timeout(f_tx, addr); |
---|
1943 | } else { |
---|
1944 | error = EINVAL; |
---|
1945 | } |
---|
1946 | break; |
---|
1947 | |
---|
1948 | case USB_GET_RX_FRAME_SIZE: |
---|
1949 | if (fflags & FREAD) { |
---|
1950 | error = ugen_get_frame_size(f_rx, addr); |
---|
1951 | } else { |
---|
1952 | error = EINVAL; |
---|
1953 | } |
---|
1954 | break; |
---|
1955 | |
---|
1956 | case USB_GET_TX_FRAME_SIZE: |
---|
1957 | if (fflags & FWRITE) { |
---|
1958 | error = ugen_get_frame_size(f_tx, addr); |
---|
1959 | } else { |
---|
1960 | error = EINVAL; |
---|
1961 | } |
---|
1962 | break; |
---|
1963 | |
---|
1964 | case USB_SET_RX_BUFFER_SIZE: |
---|
1965 | if (fflags & FREAD) { |
---|
1966 | error = ugen_set_buffer_size(f_rx, addr); |
---|
1967 | } else { |
---|
1968 | error = EINVAL; |
---|
1969 | } |
---|
1970 | break; |
---|
1971 | |
---|
1972 | case USB_SET_TX_BUFFER_SIZE: |
---|
1973 | if (fflags & FWRITE) { |
---|
1974 | error = ugen_set_buffer_size(f_tx, addr); |
---|
1975 | } else { |
---|
1976 | error = EINVAL; |
---|
1977 | } |
---|
1978 | break; |
---|
1979 | |
---|
1980 | case USB_GET_RX_BUFFER_SIZE: |
---|
1981 | if (fflags & FREAD) { |
---|
1982 | error = ugen_get_buffer_size(f_rx, addr); |
---|
1983 | } else { |
---|
1984 | error = EINVAL; |
---|
1985 | } |
---|
1986 | break; |
---|
1987 | |
---|
1988 | case USB_GET_TX_BUFFER_SIZE: |
---|
1989 | if (fflags & FWRITE) { |
---|
1990 | error = ugen_get_buffer_size(f_tx, addr); |
---|
1991 | } else { |
---|
1992 | error = EINVAL; |
---|
1993 | } |
---|
1994 | break; |
---|
1995 | |
---|
1996 | case USB_GET_RX_INTERFACE_DESC: |
---|
1997 | if (fflags & FREAD) { |
---|
1998 | error = ugen_get_iface_desc(f_rx, addr); |
---|
1999 | } else { |
---|
2000 | error = EINVAL; |
---|
2001 | } |
---|
2002 | break; |
---|
2003 | |
---|
2004 | case USB_GET_TX_INTERFACE_DESC: |
---|
2005 | if (fflags & FWRITE) { |
---|
2006 | error = ugen_get_iface_desc(f_tx, addr); |
---|
2007 | } else { |
---|
2008 | error = EINVAL; |
---|
2009 | } |
---|
2010 | break; |
---|
2011 | |
---|
2012 | case USB_GET_RX_ENDPOINT_DESC: |
---|
2013 | if (fflags & FREAD) { |
---|
2014 | error = ugen_get_endpoint_desc(f_rx, addr); |
---|
2015 | } else { |
---|
2016 | error = EINVAL; |
---|
2017 | } |
---|
2018 | break; |
---|
2019 | |
---|
2020 | case USB_GET_TX_ENDPOINT_DESC: |
---|
2021 | if (fflags & FWRITE) { |
---|
2022 | error = ugen_get_endpoint_desc(f_tx, addr); |
---|
2023 | } else { |
---|
2024 | error = EINVAL; |
---|
2025 | } |
---|
2026 | break; |
---|
2027 | |
---|
2028 | case USB_SET_RX_STALL_FLAG: |
---|
2029 | if ((fflags & FREAD) && (*(int *)addr)) { |
---|
2030 | f_rx->flag_stall = 1; |
---|
2031 | } |
---|
2032 | break; |
---|
2033 | |
---|
2034 | case USB_SET_TX_STALL_FLAG: |
---|
2035 | if ((fflags & FWRITE) && (*(int *)addr)) { |
---|
2036 | f_tx->flag_stall = 1; |
---|
2037 | } |
---|
2038 | break; |
---|
2039 | |
---|
2040 | default: |
---|
2041 | error = ENOIOCTL; |
---|
2042 | break; |
---|
2043 | } |
---|
2044 | return (error); |
---|
2045 | } |
---|
2046 | |
---|
2047 | static int |
---|
2048 | ugen_ioctl_post(struct usb_fifo *f, u_long cmd, void *addr, int fflags) |
---|
2049 | { |
---|
2050 | union { |
---|
2051 | struct usb_interface_descriptor *idesc; |
---|
2052 | struct usb_alt_interface *ai; |
---|
2053 | struct usb_device_descriptor *ddesc; |
---|
2054 | struct usb_config_descriptor *cdesc; |
---|
2055 | struct usb_device_stats *stat; |
---|
2056 | struct usb_fs_init *pinit; |
---|
2057 | struct usb_fs_uninit *puninit; |
---|
2058 | struct usb_device_port_path *dpp; |
---|
2059 | uint32_t *ptime; |
---|
2060 | void *addr; |
---|
2061 | int *pint; |
---|
2062 | } u; |
---|
2063 | struct usb_device_descriptor *dtemp; |
---|
2064 | struct usb_config_descriptor *ctemp; |
---|
2065 | struct usb_interface *iface; |
---|
2066 | int error = 0; |
---|
2067 | uint8_t n; |
---|
2068 | |
---|
2069 | u.addr = addr; |
---|
2070 | |
---|
2071 | DPRINTFN(6, "cmd=0x%08lx\n", cmd); |
---|
2072 | |
---|
2073 | switch (cmd) { |
---|
2074 | case USB_DISCOVER: |
---|
2075 | usb_needs_explore_all(); |
---|
2076 | break; |
---|
2077 | |
---|
2078 | case USB_SETDEBUG: |
---|
2079 | if (!(fflags & FWRITE)) { |
---|
2080 | error = EPERM; |
---|
2081 | break; |
---|
2082 | } |
---|
2083 | usb_debug = *(int *)addr; |
---|
2084 | break; |
---|
2085 | |
---|
2086 | case USB_GET_CONFIG: |
---|
2087 | *(int *)addr = f->udev->curr_config_index; |
---|
2088 | break; |
---|
2089 | |
---|
2090 | case USB_SET_CONFIG: |
---|
2091 | if (!(fflags & FWRITE)) { |
---|
2092 | error = EPERM; |
---|
2093 | break; |
---|
2094 | } |
---|
2095 | error = ugen_set_config(f, *(int *)addr); |
---|
2096 | break; |
---|
2097 | |
---|
2098 | case USB_GET_ALTINTERFACE: |
---|
2099 | iface = usbd_get_iface(f->udev, |
---|
2100 | u.ai->uai_interface_index); |
---|
2101 | if (iface && iface->idesc) { |
---|
2102 | u.ai->uai_alt_index = iface->alt_index; |
---|
2103 | } else { |
---|
2104 | error = EINVAL; |
---|
2105 | } |
---|
2106 | break; |
---|
2107 | |
---|
2108 | case USB_SET_ALTINTERFACE: |
---|
2109 | if (!(fflags & FWRITE)) { |
---|
2110 | error = EPERM; |
---|
2111 | break; |
---|
2112 | } |
---|
2113 | error = ugen_set_interface(f, |
---|
2114 | u.ai->uai_interface_index, u.ai->uai_alt_index); |
---|
2115 | break; |
---|
2116 | |
---|
2117 | case USB_GET_DEVICE_DESC: |
---|
2118 | dtemp = usbd_get_device_descriptor(f->udev); |
---|
2119 | if (!dtemp) { |
---|
2120 | error = EIO; |
---|
2121 | break; |
---|
2122 | } |
---|
2123 | *u.ddesc = *dtemp; |
---|
2124 | break; |
---|
2125 | |
---|
2126 | case USB_GET_CONFIG_DESC: |
---|
2127 | ctemp = usbd_get_config_descriptor(f->udev); |
---|
2128 | if (!ctemp) { |
---|
2129 | error = EIO; |
---|
2130 | break; |
---|
2131 | } |
---|
2132 | *u.cdesc = *ctemp; |
---|
2133 | break; |
---|
2134 | |
---|
2135 | case USB_GET_FULL_DESC: |
---|
2136 | error = ugen_get_cdesc(f, addr); |
---|
2137 | break; |
---|
2138 | |
---|
2139 | case USB_GET_STRING_DESC: |
---|
2140 | error = ugen_get_sdesc(f, addr); |
---|
2141 | break; |
---|
2142 | |
---|
2143 | case USB_GET_IFACE_DRIVER: |
---|
2144 | error = ugen_get_iface_driver(f, addr); |
---|
2145 | break; |
---|
2146 | |
---|
2147 | case USB_REQUEST: |
---|
2148 | case USB_DO_REQUEST: |
---|
2149 | if (!(fflags & FWRITE)) { |
---|
2150 | error = EPERM; |
---|
2151 | break; |
---|
2152 | } |
---|
2153 | error = ugen_do_request(f, addr); |
---|
2154 | break; |
---|
2155 | |
---|
2156 | case USB_DEVICEINFO: |
---|
2157 | case USB_GET_DEVICEINFO: |
---|
2158 | error = usb_gen_fill_deviceinfo(f, addr); |
---|
2159 | break; |
---|
2160 | |
---|
2161 | case USB_DEVICESTATS: |
---|
2162 | for (n = 0; n != 4; n++) { |
---|
2163 | |
---|
2164 | u.stat->uds_requests_fail[n] = |
---|
2165 | f->udev->bus->stats_err.uds_requests[n]; |
---|
2166 | |
---|
2167 | u.stat->uds_requests_ok[n] = |
---|
2168 | f->udev->bus->stats_ok.uds_requests[n]; |
---|
2169 | } |
---|
2170 | break; |
---|
2171 | |
---|
2172 | case USB_DEVICEENUMERATE: |
---|
2173 | error = ugen_re_enumerate(f); |
---|
2174 | break; |
---|
2175 | |
---|
2176 | case USB_GET_PLUGTIME: |
---|
2177 | *u.ptime = f->udev->plugtime; |
---|
2178 | break; |
---|
2179 | |
---|
2180 | case USB_CLAIM_INTERFACE: |
---|
2181 | case USB_RELEASE_INTERFACE: |
---|
2182 | /* TODO */ |
---|
2183 | break; |
---|
2184 | |
---|
2185 | case USB_IFACE_DRIVER_ACTIVE: |
---|
2186 | |
---|
2187 | n = *u.pint & 0xFF; |
---|
2188 | |
---|
2189 | iface = usbd_get_iface(f->udev, n); |
---|
2190 | |
---|
2191 | if (iface && iface->subdev) |
---|
2192 | error = 0; |
---|
2193 | else |
---|
2194 | error = ENXIO; |
---|
2195 | break; |
---|
2196 | |
---|
2197 | case USB_IFACE_DRIVER_DETACH: |
---|
2198 | |
---|
2199 | error = priv_check(curthread, PRIV_DRIVER); |
---|
2200 | |
---|
2201 | if (error) |
---|
2202 | break; |
---|
2203 | |
---|
2204 | n = *u.pint & 0xFF; |
---|
2205 | |
---|
2206 | if (n == USB_IFACE_INDEX_ANY) { |
---|
2207 | error = EINVAL; |
---|
2208 | break; |
---|
2209 | } |
---|
2210 | |
---|
2211 | /* |
---|
2212 | * Detach the currently attached driver. |
---|
2213 | */ |
---|
2214 | usb_detach_device(f->udev, n, 0); |
---|
2215 | |
---|
2216 | /* |
---|
2217 | * Set parent to self, this should keep attach away |
---|
2218 | * until the next set configuration event. |
---|
2219 | */ |
---|
2220 | usbd_set_parent_iface(f->udev, n, n); |
---|
2221 | break; |
---|
2222 | |
---|
2223 | case USB_SET_POWER_MODE: |
---|
2224 | error = ugen_set_power_mode(f, *u.pint); |
---|
2225 | break; |
---|
2226 | |
---|
2227 | case USB_GET_POWER_MODE: |
---|
2228 | *u.pint = ugen_get_power_mode(f); |
---|
2229 | break; |
---|
2230 | |
---|
2231 | case USB_GET_DEV_PORT_PATH: |
---|
2232 | error = ugen_get_port_path(f, u.dpp); |
---|
2233 | break; |
---|
2234 | |
---|
2235 | case USB_GET_POWER_USAGE: |
---|
2236 | *u.pint = ugen_get_power_usage(f); |
---|
2237 | break; |
---|
2238 | |
---|
2239 | case USB_SET_PORT_ENABLE: |
---|
2240 | error = ugen_do_port_feature(f, |
---|
2241 | *u.pint, 1, UHF_PORT_ENABLE); |
---|
2242 | break; |
---|
2243 | |
---|
2244 | case USB_SET_PORT_DISABLE: |
---|
2245 | error = ugen_do_port_feature(f, |
---|
2246 | *u.pint, 0, UHF_PORT_ENABLE); |
---|
2247 | break; |
---|
2248 | |
---|
2249 | case USB_FS_INIT: |
---|
2250 | /* verify input parameters */ |
---|
2251 | if (u.pinit->pEndpoints == NULL) { |
---|
2252 | error = EINVAL; |
---|
2253 | break; |
---|
2254 | } |
---|
2255 | if (u.pinit->ep_index_max > 127) { |
---|
2256 | error = EINVAL; |
---|
2257 | break; |
---|
2258 | } |
---|
2259 | if (u.pinit->ep_index_max == 0) { |
---|
2260 | error = EINVAL; |
---|
2261 | break; |
---|
2262 | } |
---|
2263 | if (f->fs_xfer != NULL) { |
---|
2264 | error = EBUSY; |
---|
2265 | break; |
---|
2266 | } |
---|
2267 | if (f->dev_ep_index != 0) { |
---|
2268 | error = EINVAL; |
---|
2269 | break; |
---|
2270 | } |
---|
2271 | if (ugen_fifo_in_use(f, fflags)) { |
---|
2272 | error = EBUSY; |
---|
2273 | break; |
---|
2274 | } |
---|
2275 | error = usb_fifo_alloc_buffer(f, 1, u.pinit->ep_index_max); |
---|
2276 | if (error) { |
---|
2277 | break; |
---|
2278 | } |
---|
2279 | f->fs_xfer = malloc(sizeof(f->fs_xfer[0]) * |
---|
2280 | u.pinit->ep_index_max, M_USB, M_WAITOK | M_ZERO); |
---|
2281 | if (f->fs_xfer == NULL) { |
---|
2282 | usb_fifo_free_buffer(f); |
---|
2283 | error = ENOMEM; |
---|
2284 | break; |
---|
2285 | } |
---|
2286 | f->fs_ep_max = u.pinit->ep_index_max; |
---|
2287 | f->fs_ep_ptr = u.pinit->pEndpoints; |
---|
2288 | break; |
---|
2289 | |
---|
2290 | case USB_FS_UNINIT: |
---|
2291 | if (u.puninit->dummy != 0) { |
---|
2292 | error = EINVAL; |
---|
2293 | break; |
---|
2294 | } |
---|
2295 | error = ugen_fs_uninit(f); |
---|
2296 | break; |
---|
2297 | |
---|
2298 | default: |
---|
2299 | mtx_lock(f->priv_mtx); |
---|
2300 | error = ugen_iface_ioctl(f, cmd, addr, fflags); |
---|
2301 | mtx_unlock(f->priv_mtx); |
---|
2302 | break; |
---|
2303 | } |
---|
2304 | DPRINTFN(6, "error=%d\n", error); |
---|
2305 | return (error); |
---|
2306 | } |
---|
2307 | |
---|
2308 | static void |
---|
2309 | ugen_ctrl_fs_callback(struct usb_xfer *xfer, usb_error_t error) |
---|
2310 | { |
---|
2311 | ; /* workaround for a bug in "indent" */ |
---|
2312 | |
---|
2313 | DPRINTF("st=%u alen=%u aframes=%u\n", |
---|
2314 | USB_GET_STATE(xfer), xfer->actlen, xfer->aframes); |
---|
2315 | |
---|
2316 | switch (USB_GET_STATE(xfer)) { |
---|
2317 | case USB_ST_SETUP: |
---|
2318 | usbd_transfer_submit(xfer); |
---|
2319 | break; |
---|
2320 | default: |
---|
2321 | ugen_fs_set_complete(xfer->priv_sc, USB_P2U(xfer->priv_fifo)); |
---|
2322 | break; |
---|
2323 | } |
---|
2324 | } |
---|
2325 | #endif /* USB_HAVE_UGEN */ |
---|