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