3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
49 #include <sys/fcntl.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usb_ioctl.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
56 #define USB_DEBUG_VAR ugen_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_dev.h>
60 #include <dev/usb/usb_mbuf.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_util.h>
67 #include <dev/usb/usb_hub.h>
68 #include <dev/usb/usb_generic.h>
69 #include <dev/usb/usb_transfer.h>
71 #include <dev/usb/usb_controller.h>
72 #include <dev/usb/usb_bus.h>
73 #endif /* USB_GLOBAL_INCLUDE_FILE */
79 #define UGEN_BULK_FS_BUFFER_SIZE (64*32) /* bytes */
80 #define UGEN_BULK_HS_BUFFER_SIZE (1024*32) /* bytes */
81 #define UGEN_HW_FRAMES 50 /* number of milliseconds per transfer */
83 /* function prototypes */
85 static usb_callback_t ugen_read_clear_stall_callback;
86 static usb_callback_t ugen_write_clear_stall_callback;
87 static usb_callback_t ugen_ctrl_read_callback;
88 static usb_callback_t ugen_ctrl_write_callback;
89 static usb_callback_t ugen_isoc_read_callback;
90 static usb_callback_t ugen_isoc_write_callback;
91 static usb_callback_t ugen_ctrl_fs_callback;
93 static usb_fifo_open_t ugen_open;
94 static usb_fifo_close_t ugen_close;
95 static usb_fifo_ioctl_t ugen_ioctl;
96 static usb_fifo_ioctl_t ugen_ioctl_post;
97 static usb_fifo_cmd_t ugen_start_read;
98 static usb_fifo_cmd_t ugen_start_write;
99 static usb_fifo_cmd_t ugen_stop_io;
101 static int ugen_transfer_setup(struct usb_fifo *,
102 const struct usb_config *, uint8_t);
103 static int ugen_open_pipe_write(struct usb_fifo *);
104 static int ugen_open_pipe_read(struct usb_fifo *);
105 static int ugen_set_config(struct usb_fifo *, uint8_t);
106 static int ugen_set_interface(struct usb_fifo *, uint8_t, uint8_t);
107 static int ugen_get_cdesc(struct usb_fifo *, struct usb_gen_descriptor *);
108 static int ugen_get_sdesc(struct usb_fifo *, struct usb_gen_descriptor *);
109 static int ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd);
110 static int usb_gen_fill_deviceinfo(struct usb_fifo *,
111 struct usb_device_info *);
112 static int ugen_re_enumerate(struct usb_fifo *);
113 static int ugen_iface_ioctl(struct usb_fifo *, u_long, void *, int);
114 static uint8_t ugen_fs_get_complete(struct usb_fifo *, uint8_t *);
115 static int ugen_fs_uninit(struct usb_fifo *f);
119 struct usb_fifo_methods usb_ugen_methods = {
120 .f_open = &ugen_open,
121 .f_close = &ugen_close,
122 .f_ioctl = &ugen_ioctl,
123 .f_ioctl_post = &ugen_ioctl_post,
124 .f_start_read = &ugen_start_read,
125 .f_stop_read = &ugen_stop_io,
126 .f_start_write = &ugen_start_write,
127 .f_stop_write = &ugen_stop_io,
131 static int ugen_debug = 0;
133 static SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB generic");
134 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RWTUN, &ugen_debug,
142 ugen_transfer_setup(struct usb_fifo *f,
143 const struct usb_config *setup, uint8_t n_setup)
145 struct usb_endpoint *ep = usb_fifo_softc(f);
146 struct usb_device *udev = f->udev;
147 uint8_t iface_index = ep->iface_index;
150 mtx_unlock(f->priv_mtx);
153 * "usbd_transfer_setup()" can sleep so one needs to make a wrapper,
154 * exiting the mutex and checking things
156 error = usbd_transfer_setup(udev, &iface_index, f->xfer,
157 setup, n_setup, f, f->priv_mtx);
160 if (f->xfer[0]->nframes == 1) {
161 error = usb_fifo_alloc_buffer(f,
162 f->xfer[0]->max_data_length, 2);
164 error = usb_fifo_alloc_buffer(f,
165 f->xfer[0]->max_frame_size,
166 2 * f->xfer[0]->nframes);
169 usbd_transfer_unsetup(f->xfer, n_setup);
172 mtx_lock(f->priv_mtx);
178 ugen_open(struct usb_fifo *f, int fflags)
180 struct usb_endpoint *ep = usb_fifo_softc(f);
181 struct usb_endpoint_descriptor *ed = ep->edesc;
184 DPRINTFN(6, "flag=0x%x\n", fflags);
186 mtx_lock(f->priv_mtx);
187 switch (usbd_get_speed(f->udev)) {
190 f->nframes = UGEN_HW_FRAMES;
191 f->bufsize = UGEN_BULK_FS_BUFFER_SIZE;
194 f->nframes = UGEN_HW_FRAMES * 8;
195 f->bufsize = UGEN_BULK_HS_BUFFER_SIZE;
199 type = ed->bmAttributes & UE_XFERTYPE;
200 if (type == UE_INTERRUPT) {
201 f->bufsize = 0; /* use "wMaxPacketSize" */
203 f->timeout = USB_NO_TIMEOUT;
206 mtx_unlock(f->priv_mtx);
212 ugen_close(struct usb_fifo *f, int fflags)
214 DPRINTFN(6, "flag=0x%x\n", fflags);
218 mtx_lock(f->priv_mtx);
219 usbd_transfer_stop(f->xfer[0]);
220 usbd_transfer_stop(f->xfer[1]);
221 mtx_unlock(f->priv_mtx);
223 usbd_transfer_unsetup(f->xfer, 2);
224 usb_fifo_free_buffer(f);
226 if (ugen_fs_uninit(f)) {
227 /* ignore any errors - we are closing */
228 DPRINTFN(6, "no FIFOs\n");
233 ugen_open_pipe_write(struct usb_fifo *f)
235 struct usb_config usb_config[2];
236 struct usb_endpoint *ep = usb_fifo_softc(f);
237 struct usb_endpoint_descriptor *ed = ep->edesc;
239 mtx_assert(f->priv_mtx, MA_OWNED);
241 if (f->xfer[0] || f->xfer[1]) {
242 /* transfers are already opened */
245 memset(usb_config, 0, sizeof(usb_config));
247 usb_config[1].type = UE_CONTROL;
248 usb_config[1].endpoint = 0;
249 usb_config[1].direction = UE_DIR_ANY;
250 usb_config[1].timeout = 1000; /* 1 second */
251 usb_config[1].interval = 50;/* 50 milliseconds */
252 usb_config[1].bufsize = sizeof(struct usb_device_request);
253 usb_config[1].callback = &ugen_write_clear_stall_callback;
254 usb_config[1].usb_mode = USB_MODE_HOST;
256 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE;
257 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR;
258 usb_config[0].stream_id = 0; /* XXX support more stream ID's */
259 usb_config[0].direction = UE_DIR_TX;
260 usb_config[0].interval = USB_DEFAULT_INTERVAL;
261 usb_config[0].flags.proxy_buffer = 1;
262 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */
264 switch (ed->bmAttributes & UE_XFERTYPE) {
268 usb_config[0].flags.force_short_xfer = 1;
270 usb_config[0].callback = &ugen_ctrl_write_callback;
271 usb_config[0].timeout = f->timeout;
272 usb_config[0].frames = 1;
273 usb_config[0].bufsize = f->bufsize;
274 if (ugen_transfer_setup(f, usb_config, 2)) {
277 /* first transfer does not clear stall */
282 usb_config[0].flags.short_xfer_ok = 1;
283 usb_config[0].bufsize = 0; /* use default */
284 usb_config[0].frames = f->nframes;
285 usb_config[0].callback = &ugen_isoc_write_callback;
286 usb_config[0].timeout = 0;
288 /* clone configuration */
289 usb_config[1] = usb_config[0];
291 if (ugen_transfer_setup(f, usb_config, 2)) {
302 ugen_open_pipe_read(struct usb_fifo *f)
304 struct usb_config usb_config[2];
305 struct usb_endpoint *ep = usb_fifo_softc(f);
306 struct usb_endpoint_descriptor *ed = ep->edesc;
308 mtx_assert(f->priv_mtx, MA_OWNED);
310 if (f->xfer[0] || f->xfer[1]) {
311 /* transfers are already opened */
314 memset(usb_config, 0, sizeof(usb_config));
316 usb_config[1].type = UE_CONTROL;
317 usb_config[1].endpoint = 0;
318 usb_config[1].direction = UE_DIR_ANY;
319 usb_config[1].timeout = 1000; /* 1 second */
320 usb_config[1].interval = 50;/* 50 milliseconds */
321 usb_config[1].bufsize = sizeof(struct usb_device_request);
322 usb_config[1].callback = &ugen_read_clear_stall_callback;
323 usb_config[1].usb_mode = USB_MODE_HOST;
325 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE;
326 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR;
327 usb_config[0].stream_id = 0; /* XXX support more stream ID's */
328 usb_config[0].direction = UE_DIR_RX;
329 usb_config[0].interval = USB_DEFAULT_INTERVAL;
330 usb_config[0].flags.proxy_buffer = 1;
331 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */
333 switch (ed->bmAttributes & UE_XFERTYPE) {
337 usb_config[0].flags.short_xfer_ok = 1;
339 usb_config[0].timeout = f->timeout;
340 usb_config[0].frames = 1;
341 usb_config[0].callback = &ugen_ctrl_read_callback;
342 usb_config[0].bufsize = f->bufsize;
344 if (ugen_transfer_setup(f, usb_config, 2)) {
347 /* first transfer does not clear stall */
352 usb_config[0].flags.short_xfer_ok = 1;
353 usb_config[0].bufsize = 0; /* use default */
354 usb_config[0].frames = f->nframes;
355 usb_config[0].callback = &ugen_isoc_read_callback;
356 usb_config[0].timeout = 0;
358 /* clone configuration */
359 usb_config[1] = usb_config[0];
361 if (ugen_transfer_setup(f, usb_config, 2)) {
373 ugen_start_read(struct usb_fifo *f)
375 /* check that pipes are open */
376 if (ugen_open_pipe_read(f)) {
378 usb_fifo_put_data_error(f);
380 /* start transfers */
381 usbd_transfer_start(f->xfer[0]);
382 usbd_transfer_start(f->xfer[1]);
386 ugen_start_write(struct usb_fifo *f)
388 /* check that pipes are open */
389 if (ugen_open_pipe_write(f)) {
391 usb_fifo_get_data_error(f);
393 /* start transfers */
394 usbd_transfer_start(f->xfer[0]);
395 usbd_transfer_start(f->xfer[1]);
399 ugen_stop_io(struct usb_fifo *f)
402 usbd_transfer_stop(f->xfer[0]);
403 usbd_transfer_stop(f->xfer[1]);
407 ugen_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
409 struct usb_fifo *f = usbd_xfer_softc(xfer);
412 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes);
414 switch (USB_GET_STATE(xfer)) {
415 case USB_ST_TRANSFERRED:
416 if (xfer->actlen == 0) {
417 if (f->fifo_zlp != 4) {
421 * Throttle a little bit we have multiple ZLPs
424 xfer->interval = 64; /* ms */
431 usb_fifo_put_data(f, xfer->frbuffers, 0,
436 usbd_transfer_start(f->xfer[1]);
439 USB_IF_POLL(&f->free_q, m);
441 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
442 usbd_transfer_submit(xfer);
447 if (xfer->error != USB_ERR_CANCELLED) {
448 /* send a zero length packet to userland */
449 usb_fifo_put_data(f, xfer->frbuffers, 0, 0, 1);
452 usbd_transfer_start(f->xfer[1]);
459 ugen_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
461 struct usb_fifo *f = usbd_xfer_softc(xfer);
462 usb_frlength_t actlen;
464 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes);
466 switch (USB_GET_STATE(xfer)) {
468 case USB_ST_TRANSFERRED:
470 * If writing is in stall, just jump to clear stall
471 * callback and solve the situation.
474 usbd_transfer_start(f->xfer[1]);
478 * Write data, setup and perform hardware transfer.
480 if (usb_fifo_get_data(f, xfer->frbuffers, 0,
481 xfer->max_data_length, &actlen, 0)) {
482 usbd_xfer_set_frame_len(xfer, 0, actlen);
483 usbd_transfer_submit(xfer);
488 if (xfer->error != USB_ERR_CANCELLED) {
490 usbd_transfer_start(f->xfer[1]);
497 ugen_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
499 struct usb_fifo *f = usbd_xfer_softc(xfer);
500 struct usb_xfer *xfer_other = f->xfer[0];
502 if (f->flag_stall == 0) {
506 if (usbd_clear_stall_callback(xfer, xfer_other)) {
507 DPRINTFN(5, "f=%p: stall cleared\n", f);
509 usbd_transfer_start(xfer_other);
514 ugen_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
516 struct usb_fifo *f = usbd_xfer_softc(xfer);
517 struct usb_xfer *xfer_other = f->xfer[0];
519 if (f->flag_stall == 0) {
523 if (usbd_clear_stall_callback(xfer, xfer_other)) {
524 DPRINTFN(5, "f=%p: stall cleared\n", f);
526 usbd_transfer_start(xfer_other);
531 ugen_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error)
533 struct usb_fifo *f = usbd_xfer_softc(xfer);
534 usb_frlength_t offset;
537 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes);
539 switch (USB_GET_STATE(xfer)) {
540 case USB_ST_TRANSFERRED:
542 DPRINTFN(6, "actlen=%d\n", xfer->actlen);
546 for (n = 0; n != xfer->aframes; n++) {
547 usb_fifo_put_data(f, xfer->frbuffers, offset,
548 xfer->frlengths[n], 1);
549 offset += xfer->max_frame_size;
554 for (n = 0; n != xfer->nframes; n++) {
555 /* setup size for next transfer */
556 usbd_xfer_set_frame_len(xfer, n, xfer->max_frame_size);
558 usbd_transfer_submit(xfer);
562 if (xfer->error == USB_ERR_CANCELLED) {
570 ugen_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error)
572 struct usb_fifo *f = usbd_xfer_softc(xfer);
573 usb_frlength_t actlen;
574 usb_frlength_t offset;
577 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes);
579 switch (USB_GET_STATE(xfer)) {
580 case USB_ST_TRANSFERRED:
584 for (n = 0; n != xfer->nframes; n++) {
585 if (usb_fifo_get_data(f, xfer->frbuffers, offset,
586 xfer->max_frame_size, &actlen, 1)) {
587 usbd_xfer_set_frame_len(xfer, n, actlen);
594 for (; n != xfer->nframes; n++) {
595 /* fill in zero frames */
596 usbd_xfer_set_frame_len(xfer, n, 0);
598 usbd_transfer_submit(xfer);
602 if (xfer->error == USB_ERR_CANCELLED) {
610 ugen_set_config(struct usb_fifo *f, uint8_t index)
612 DPRINTFN(2, "index %u\n", index);
614 if (f->udev->flags.usb_mode != USB_MODE_HOST) {
615 /* not possible in device side mode */
619 /* make sure all FIFO's are gone */
620 /* else there can be a deadlock */
621 if (ugen_fs_uninit(f)) {
622 /* ignore any errors */
623 DPRINTFN(6, "no FIFOs\n");
626 if (usbd_start_set_config(f->udev, index) != 0)
633 ugen_set_interface(struct usb_fifo *f,
634 uint8_t iface_index, uint8_t alt_index)
636 DPRINTFN(2, "%u, %u\n", iface_index, alt_index);
638 if (f->udev->flags.usb_mode != USB_MODE_HOST) {
639 /* not possible in device side mode */
642 /* make sure all FIFO's are gone */
643 /* else there can be a deadlock */
644 if (ugen_fs_uninit(f)) {
645 /* ignore any errors */
646 DPRINTFN(6, "no FIFOs\n");
648 /* change setting - will free generic FIFOs, if any */
649 if (usbd_set_alt_interface_index(f->udev, iface_index, alt_index)) {
652 /* probe and attach */
653 if (usb_probe_and_attach(f->udev, iface_index)) {
659 /*------------------------------------------------------------------------*
662 * This function will retrieve the complete configuration descriptor
663 * at the given index.
664 *------------------------------------------------------------------------*/
666 ugen_get_cdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd)
668 struct usb_config_descriptor *cdesc;
669 struct usb_device *udev = f->udev;
676 if (ugd->ugd_data == NULL) {
677 /* userland pointer should not be zero */
680 if ((ugd->ugd_config_index == USB_UNCONFIG_INDEX) ||
681 (ugd->ugd_config_index == udev->curr_config_index)) {
682 cdesc = usbd_get_config_descriptor(udev);
688 #if (USB_HAVE_FIXED_CONFIG == 0)
689 if (usbd_req_get_config_desc_full(udev,
690 NULL, &cdesc, ugd->ugd_config_index)) {
695 /* configuration descriptor data is shared */
700 len = UGETW(cdesc->wTotalLength);
701 if (len > ugd->ugd_maxlen) {
702 len = ugd->ugd_maxlen;
704 DPRINTFN(6, "len=%u\n", len);
706 ugd->ugd_actlen = len;
709 error = copyout(cdesc, ugd->ugd_data, len);
712 usbd_free_config_desc(udev, cdesc);
718 ugen_get_sdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd)
725 /* Protect scratch area */
726 do_unlock = usbd_ctrl_lock(f->udev);
728 ptr = f->udev->scratch.data;
729 size = sizeof(f->udev->scratch.data);
731 if (usbd_req_get_string_desc(f->udev, NULL, ptr,
732 size, ugd->ugd_lang_id, ugd->ugd_string_index)) {
736 if (size > ((uint8_t *)ptr)[0]) {
737 size = ((uint8_t *)ptr)[0];
739 if (size > ugd->ugd_maxlen) {
740 size = ugd->ugd_maxlen;
742 ugd->ugd_actlen = size;
745 error = copyout(ptr, ugd->ugd_data, size);
748 usbd_ctrl_unlock(f->udev);
753 /*------------------------------------------------------------------------*
754 * ugen_get_iface_driver
756 * This function generates an USB interface description for userland.
761 *------------------------------------------------------------------------*/
763 ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd)
765 struct usb_device *udev = f->udev;
766 struct usb_interface *iface;
776 if ((ugd->ugd_data == NULL) || (ugd->ugd_maxlen == 0)) {
777 /* userland pointer should not be zero */
781 iface = usbd_get_iface(udev, ugd->ugd_iface_index);
782 if ((iface == NULL) || (iface->idesc == NULL)) {
783 /* invalid interface index */
787 /* read out device nameunit string, if any */
788 if ((iface->subdev != NULL) &&
789 device_is_attached(iface->subdev) &&
790 (ptr = device_get_nameunit(iface->subdev)) &&
791 (desc = device_get_desc(iface->subdev))) {
793 /* print description */
794 snprintf(buf, sizeof(buf), "%s: <%s>", ptr, desc);
797 maxlen = ugd->ugd_maxlen - 1;
802 /* update actual length, including terminating zero */
803 ugd->ugd_actlen = len + 1;
805 /* copy out interface description */
806 error = copyout(buf, ugd->ugd_data, ugd->ugd_actlen);
808 /* zero length string is default */
809 error = copyout("", ugd->ugd_data, 1);
814 /*------------------------------------------------------------------------*
815 * usb_gen_fill_deviceinfo
817 * This function dumps information about an USB device to the
818 * structure pointed to by the "di" argument.
823 *------------------------------------------------------------------------*/
825 usb_gen_fill_deviceinfo(struct usb_fifo *f, struct usb_device_info *di)
827 struct usb_device *udev;
828 struct usb_device *hub;
832 bzero(di, sizeof(di[0]));
834 di->udi_bus = device_get_unit(udev->bus->bdev);
835 di->udi_addr = udev->address;
836 di->udi_index = udev->device_index;
837 strlcpy(di->udi_serial, usb_get_serial(udev), sizeof(di->udi_serial));
838 strlcpy(di->udi_vendor, usb_get_manufacturer(udev), sizeof(di->udi_vendor));
839 strlcpy(di->udi_product, usb_get_product(udev), sizeof(di->udi_product));
840 usb_printbcd(di->udi_release, sizeof(di->udi_release),
841 UGETW(udev->ddesc.bcdDevice));
842 di->udi_vendorNo = UGETW(udev->ddesc.idVendor);
843 di->udi_productNo = UGETW(udev->ddesc.idProduct);
844 di->udi_releaseNo = UGETW(udev->ddesc.bcdDevice);
845 di->udi_class = udev->ddesc.bDeviceClass;
846 di->udi_subclass = udev->ddesc.bDeviceSubClass;
847 di->udi_protocol = udev->ddesc.bDeviceProtocol;
848 di->udi_config_no = udev->curr_config_no;
849 di->udi_config_index = udev->curr_config_index;
850 di->udi_power = udev->flags.self_powered ? 0 : udev->power;
851 di->udi_speed = udev->speed;
852 di->udi_mode = udev->flags.usb_mode;
853 di->udi_power_mode = udev->power_mode;
854 di->udi_suspended = udev->flags.peer_suspended;
856 hub = udev->parent_hub;
858 di->udi_hubaddr = hub->address;
859 di->udi_hubindex = hub->device_index;
860 di->udi_hubport = udev->port_no;
865 /*------------------------------------------------------------------------*
871 *------------------------------------------------------------------------*/
873 ugen_check_request(struct usb_device *udev, struct usb_device_request *req)
875 struct usb_endpoint *ep;
879 * Avoid requests that would damage the bus integrity:
881 if (((req->bmRequestType == UT_WRITE_DEVICE) &&
882 (req->bRequest == UR_SET_ADDRESS)) ||
883 ((req->bmRequestType == UT_WRITE_DEVICE) &&
884 (req->bRequest == UR_SET_CONFIG)) ||
885 ((req->bmRequestType == UT_WRITE_INTERFACE) &&
886 (req->bRequest == UR_SET_INTERFACE))) {
888 * These requests can be useful for testing USB drivers.
890 error = priv_check(curthread, PRIV_DRIVER);
896 * Special case - handle clearing of stall
898 if (req->bmRequestType == UT_WRITE_ENDPOINT) {
900 ep = usbd_get_ep_by_addr(udev, req->wIndex[0]);
904 if ((req->bRequest == UR_CLEAR_FEATURE) &&
905 (UGETW(req->wValue) == UF_ENDPOINT_HALT)) {
906 usbd_clear_data_toggle(udev, ep);
909 /* TODO: add more checks to verify the interface index */
915 ugen_do_request(struct usb_fifo *f, struct usb_ctl_request *ur)
921 if (ugen_check_request(f->udev, &ur->ucr_request)) {
924 len = UGETW(ur->ucr_request.wLength);
926 /* check if "ucr_data" is valid */
928 if (ur->ucr_data == NULL) {
932 /* do the USB request */
933 error = usbd_do_request_flags
934 (f->udev, NULL, &ur->ucr_request, ur->ucr_data,
935 (ur->ucr_flags & USB_SHORT_XFER_OK) |
936 USB_USER_DATA_PTR, &actlen,
937 USB_DEFAULT_TIMEOUT);
939 ur->ucr_actlen = actlen;
947 /*------------------------------------------------------------------------
949 *------------------------------------------------------------------------*/
951 ugen_re_enumerate(struct usb_fifo *f)
953 struct usb_device *udev = f->udev;
957 * This request can be useful for testing USB drivers:
959 error = priv_check(curthread, PRIV_DRIVER);
963 if (udev->flags.usb_mode != USB_MODE_HOST) {
964 /* not possible in device side mode */
965 DPRINTFN(6, "device mode\n");
968 /* make sure all FIFO's are gone */
969 /* else there can be a deadlock */
970 if (ugen_fs_uninit(f)) {
971 /* ignore any errors */
972 DPRINTFN(6, "no FIFOs\n");
974 /* start re-enumeration of device */
975 usbd_start_re_enumerate(udev);
980 ugen_fs_uninit(struct usb_fifo *f)
982 if (f->fs_xfer == NULL) {
985 usbd_transfer_unsetup(f->fs_xfer, f->fs_ep_max);
986 free(f->fs_xfer, M_USB);
990 f->flag_iscomplete = 0;
991 usb_fifo_free_buffer(f);
996 ugen_fs_get_complete(struct usb_fifo *f, uint8_t *pindex)
1000 USB_IF_DEQUEUE(&f->used_q, m);
1003 *pindex = *((uint8_t *)(m->cur_data_ptr));
1005 USB_IF_ENQUEUE(&f->free_q, m);
1007 return (0); /* success */
1010 *pindex = 0; /* fix compiler warning */
1012 f->flag_iscomplete = 0;
1014 return (1); /* failure */
1018 ugen_fs_set_complete(struct usb_fifo *f, uint8_t index)
1022 USB_IF_DEQUEUE(&f->free_q, m);
1025 /* can happen during close */
1026 DPRINTF("out of buffers\n");
1031 *((uint8_t *)(m->cur_data_ptr)) = index;
1033 USB_IF_ENQUEUE(&f->used_q, m);
1035 f->flag_iscomplete = 1;
1041 ugen_fs_copy_in(struct usb_fifo *f, uint8_t ep_index)
1043 struct usb_device_request *req;
1044 struct usb_xfer *xfer;
1045 struct usb_fs_endpoint fs_ep;
1046 void *uaddr; /* userland pointer */
1048 usb_frlength_t offset;
1055 if (ep_index >= f->fs_ep_max) {
1058 xfer = f->fs_xfer[ep_index];
1062 mtx_lock(f->priv_mtx);
1063 if (usbd_transfer_pending(xfer)) {
1064 mtx_unlock(f->priv_mtx);
1065 return (EBUSY); /* should not happen */
1067 mtx_unlock(f->priv_mtx);
1069 error = copyin(f->fs_ep_ptr +
1070 ep_index, &fs_ep, sizeof(fs_ep));
1074 /* security checks */
1076 if (fs_ep.nFrames > xfer->max_frame_count) {
1077 xfer->error = USB_ERR_INVAL;
1080 if (fs_ep.nFrames == 0) {
1081 xfer->error = USB_ERR_INVAL;
1084 error = copyin(fs_ep.ppBuffer,
1085 &uaddr, sizeof(uaddr));
1089 /* reset first frame */
1090 usbd_xfer_set_frame_offset(xfer, 0, 0);
1092 if (xfer->flags_int.control_xfr) {
1094 req = xfer->frbuffers[0].buffer;
1096 error = copyin(fs_ep.pLength,
1097 &length, sizeof(length));
1101 if (length != sizeof(*req)) {
1102 xfer->error = USB_ERR_INVAL;
1106 error = copyin(uaddr, req, length);
1111 if (ugen_check_request(f->udev, req)) {
1112 xfer->error = USB_ERR_INVAL;
1115 usbd_xfer_set_frame_len(xfer, 0, length);
1117 /* Host mode only ! */
1118 if ((req->bmRequestType &
1119 (UT_READ | UT_WRITE)) == UT_READ) {
1125 offset = sizeof(*req);
1128 /* Device and Host mode */
1129 if (USB_GET_DATA_ISREAD(xfer)) {
1138 rem = usbd_xfer_max_len(xfer);
1139 xfer->nframes = fs_ep.nFrames;
1140 xfer->timeout = fs_ep.timeout;
1141 if (xfer->timeout > 65535) {
1142 xfer->timeout = 65535;
1144 if (fs_ep.flags & USB_FS_FLAG_SINGLE_SHORT_OK)
1145 xfer->flags.short_xfer_ok = 1;
1147 xfer->flags.short_xfer_ok = 0;
1149 if (fs_ep.flags & USB_FS_FLAG_MULTI_SHORT_OK)
1150 xfer->flags.short_frames_ok = 1;
1152 xfer->flags.short_frames_ok = 0;
1154 if (fs_ep.flags & USB_FS_FLAG_FORCE_SHORT)
1155 xfer->flags.force_short_xfer = 1;
1157 xfer->flags.force_short_xfer = 0;
1159 if (fs_ep.flags & USB_FS_FLAG_CLEAR_STALL)
1160 usbd_xfer_set_stall(xfer);
1162 xfer->flags.stall_pipe = 0;
1164 for (; n != xfer->nframes; n++) {
1166 error = copyin(fs_ep.pLength + n,
1167 &length, sizeof(length));
1171 usbd_xfer_set_frame_len(xfer, n, length);
1174 xfer->error = USB_ERR_INVAL;
1181 /* we need to know the source buffer */
1182 error = copyin(fs_ep.ppBuffer + n,
1183 &uaddr, sizeof(uaddr));
1187 if (xfer->flags_int.isochronous_xfr) {
1188 /* get kernel buffer address */
1189 kaddr = xfer->frbuffers[0].buffer;
1190 kaddr = USB_ADD_BYTES(kaddr, offset);
1192 /* set current frame offset */
1193 usbd_xfer_set_frame_offset(xfer, offset, n);
1195 /* get kernel buffer address */
1196 kaddr = xfer->frbuffers[n].buffer;
1200 error = copyin(uaddr, kaddr, length);
1210 mtx_lock(f->priv_mtx);
1211 ugen_fs_set_complete(f, ep_index);
1212 mtx_unlock(f->priv_mtx);
1217 ugen_fs_copy_out(struct usb_fifo *f, uint8_t ep_index)
1219 struct usb_device_request *req;
1220 struct usb_xfer *xfer;
1221 struct usb_fs_endpoint fs_ep;
1222 struct usb_fs_endpoint *fs_ep_uptr; /* userland ptr */
1223 void *uaddr; /* userland ptr */
1225 usb_frlength_t offset;
1233 if (ep_index >= f->fs_ep_max)
1236 xfer = f->fs_xfer[ep_index];
1240 mtx_lock(f->priv_mtx);
1241 if (usbd_transfer_pending(xfer)) {
1242 mtx_unlock(f->priv_mtx);
1243 return (EBUSY); /* should not happen */
1245 mtx_unlock(f->priv_mtx);
1247 fs_ep_uptr = f->fs_ep_ptr + ep_index;
1248 error = copyin(fs_ep_uptr, &fs_ep, sizeof(fs_ep));
1252 fs_ep.status = xfer->error;
1253 fs_ep.aFrames = xfer->aframes;
1254 fs_ep.isoc_time_complete = xfer->isoc_time_complete;
1258 if (xfer->flags_int.control_xfr) {
1259 req = xfer->frbuffers[0].buffer;
1261 /* Host mode only ! */
1262 if ((req->bmRequestType & (UT_READ | UT_WRITE)) == UT_READ) {
1267 if (xfer->nframes == 0)
1268 n = 0; /* should never happen */
1272 /* Device and Host mode */
1273 if (USB_GET_DATA_ISREAD(xfer)) {
1281 /* Update lengths and copy out data */
1283 rem = usbd_xfer_max_len(xfer);
1286 for (; n != xfer->nframes; n++) {
1288 /* get initial length into "temp" */
1289 error = copyin(fs_ep.pLength + n,
1290 &temp, sizeof(temp));
1295 /* the userland length has been corrupted */
1296 DPRINTF("corrupt userland length "
1297 "%u > %u\n", temp, rem);
1298 fs_ep.status = USB_ERR_INVAL;
1303 /* get actual transfer length */
1304 length = xfer->frlengths[n];
1305 if (length > temp) {
1307 fs_ep.status = USB_ERR_INVAL;
1308 DPRINTF("data overflow %u > %u\n",
1314 /* we need to know the destination buffer */
1315 error = copyin(fs_ep.ppBuffer + n,
1316 &uaddr, sizeof(uaddr));
1320 if (xfer->flags_int.isochronous_xfr) {
1321 /* only one frame buffer */
1322 kaddr = USB_ADD_BYTES(
1323 xfer->frbuffers[0].buffer, offset);
1325 /* multiple frame buffers */
1326 kaddr = xfer->frbuffers[n].buffer;
1330 error = copyout(kaddr, uaddr, length);
1336 * Update offset according to initial length, which is
1337 * needed by isochronous transfers!
1342 error = copyout(&length,
1343 fs_ep.pLength + n, sizeof(length));
1350 /* update "aFrames" */
1351 error = copyout(&fs_ep.aFrames, &fs_ep_uptr->aFrames,
1352 sizeof(fs_ep.aFrames));
1356 /* update "isoc_time_complete" */
1357 error = copyout(&fs_ep.isoc_time_complete,
1358 &fs_ep_uptr->isoc_time_complete,
1359 sizeof(fs_ep.isoc_time_complete));
1362 /* update "status" */
1363 error = copyout(&fs_ep.status, &fs_ep_uptr->status,
1364 sizeof(fs_ep.status));
1370 ugen_fifo_in_use(struct usb_fifo *f, int fflags)
1372 struct usb_fifo *f_rx;
1373 struct usb_fifo *f_tx;
1375 f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX];
1376 f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX];
1378 if ((fflags & FREAD) && f_rx &&
1379 (f_rx->xfer[0] || f_rx->xfer[1])) {
1380 return (1); /* RX FIFO in use */
1382 if ((fflags & FWRITE) && f_tx &&
1383 (f_tx->xfer[0] || f_tx->xfer[1])) {
1384 return (1); /* TX FIFO in use */
1386 return (0); /* not in use */
1390 ugen_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
1392 struct usb_config usb_config[1];
1393 struct usb_device_request req;
1395 struct usb_fs_complete *pcomp;
1396 struct usb_fs_start *pstart;
1397 struct usb_fs_stop *pstop;
1398 struct usb_fs_open *popen;
1399 struct usb_fs_open_stream *popen_stream;
1400 struct usb_fs_close *pclose;
1401 struct usb_fs_clear_stall_sync *pstall;
1404 struct usb_endpoint *ep;
1405 struct usb_endpoint_descriptor *ed;
1406 struct usb_xfer *xfer;
1408 uint8_t iface_index;
1415 DPRINTFN(6, "cmd=0x%08lx\n", cmd);
1418 case USB_FS_COMPLETE:
1419 mtx_lock(f->priv_mtx);
1420 error = ugen_fs_get_complete(f, &ep_index);
1421 mtx_unlock(f->priv_mtx);
1427 u.pcomp->ep_index = ep_index;
1428 error = ugen_fs_copy_out(f, u.pcomp->ep_index);
1432 error = ugen_fs_copy_in(f, u.pstart->ep_index);
1435 mtx_lock(f->priv_mtx);
1436 xfer = f->fs_xfer[u.pstart->ep_index];
1437 usbd_transfer_start(xfer);
1438 mtx_unlock(f->priv_mtx);
1442 if (u.pstop->ep_index >= f->fs_ep_max) {
1446 mtx_lock(f->priv_mtx);
1447 xfer = f->fs_xfer[u.pstart->ep_index];
1448 if (usbd_transfer_pending(xfer)) {
1449 usbd_transfer_stop(xfer);
1451 * Check if the USB transfer was stopped
1452 * before it was even started. Else a cancel
1453 * callback will be pending.
1455 if (!xfer->flags_int.transferring) {
1456 ugen_fs_set_complete(xfer->priv_sc,
1457 USB_P2U(xfer->priv_fifo));
1460 mtx_unlock(f->priv_mtx);
1464 case USB_FS_OPEN_STREAM:
1465 if (u.popen->ep_index >= f->fs_ep_max) {
1469 if (f->fs_xfer[u.popen->ep_index] != NULL) {
1473 if (u.popen->max_bufsize > USB_FS_MAX_BUFSIZE) {
1474 u.popen->max_bufsize = USB_FS_MAX_BUFSIZE;
1476 if (u.popen->max_frames & USB_FS_MAX_FRAMES_PRE_SCALE) {
1478 u.popen->max_frames &= ~USB_FS_MAX_FRAMES_PRE_SCALE;
1482 if (u.popen->max_frames > USB_FS_MAX_FRAMES) {
1483 u.popen->max_frames = USB_FS_MAX_FRAMES;
1486 if (u.popen->max_frames == 0) {
1490 ep = usbd_get_ep_by_addr(f->udev, u.popen->ep_no);
1500 iface_index = ep->iface_index;
1502 memset(usb_config, 0, sizeof(usb_config));
1504 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE;
1505 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR;
1506 usb_config[0].direction = ed->bEndpointAddress & (UE_DIR_OUT | UE_DIR_IN);
1507 usb_config[0].interval = USB_DEFAULT_INTERVAL;
1508 usb_config[0].flags.proxy_buffer = 1;
1510 usb_config[0].flags.pre_scale_frames = 1;
1511 usb_config[0].callback = &ugen_ctrl_fs_callback;
1512 usb_config[0].timeout = 0; /* no timeout */
1513 usb_config[0].frames = u.popen->max_frames;
1514 usb_config[0].bufsize = u.popen->max_bufsize;
1515 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */
1516 if (cmd == USB_FS_OPEN_STREAM)
1517 usb_config[0].stream_id = u.popen_stream->stream_id;
1519 if (usb_config[0].type == UE_CONTROL) {
1520 if (f->udev->flags.usb_mode != USB_MODE_HOST) {
1526 isread = ((usb_config[0].endpoint &
1527 (UE_DIR_IN | UE_DIR_OUT)) == UE_DIR_IN);
1529 if (f->udev->flags.usb_mode != USB_MODE_HOST) {
1532 /* check permissions */
1534 if (!(fflags & FREAD)) {
1539 if (!(fflags & FWRITE)) {
1545 error = usbd_transfer_setup(f->udev, &iface_index,
1546 f->fs_xfer + u.popen->ep_index, usb_config, 1,
1549 /* update maximums */
1550 u.popen->max_packet_length =
1551 f->fs_xfer[u.popen->ep_index]->max_frame_size;
1552 u.popen->max_bufsize =
1553 f->fs_xfer[u.popen->ep_index]->max_data_length;
1554 /* update number of frames */
1555 u.popen->max_frames =
1556 f->fs_xfer[u.popen->ep_index]->nframes;
1557 /* store index of endpoint */
1558 f->fs_xfer[u.popen->ep_index]->priv_fifo =
1559 ((uint8_t *)0) + u.popen->ep_index;
1566 if (u.pclose->ep_index >= f->fs_ep_max) {
1570 if (f->fs_xfer[u.pclose->ep_index] == NULL) {
1574 usbd_transfer_unsetup(f->fs_xfer + u.pclose->ep_index, 1);
1577 case USB_FS_CLEAR_STALL_SYNC:
1578 if (u.pstall->ep_index >= f->fs_ep_max) {
1582 if (f->fs_xfer[u.pstall->ep_index] == NULL) {
1586 if (f->udev->flags.usb_mode != USB_MODE_HOST) {
1590 mtx_lock(f->priv_mtx);
1591 error = usbd_transfer_pending(f->fs_xfer[u.pstall->ep_index]);
1592 mtx_unlock(f->priv_mtx);
1597 ep = f->fs_xfer[u.pstall->ep_index]->endpoint;
1599 /* setup a clear-stall packet */
1600 req.bmRequestType = UT_WRITE_ENDPOINT;
1601 req.bRequest = UR_CLEAR_FEATURE;
1602 USETW(req.wValue, UF_ENDPOINT_HALT);
1603 req.wIndex[0] = ep->edesc->bEndpointAddress;
1605 USETW(req.wLength, 0);
1607 error = usbd_do_request(f->udev, NULL, &req, NULL);
1609 usbd_clear_data_toggle(f->udev, ep);
1620 DPRINTFN(6, "error=%d\n", error);
1626 ugen_set_short_xfer(struct usb_fifo *f, void *addr)
1635 if (f->flag_short == t) {
1636 /* same value like before - accept */
1639 if (f->xfer[0] || f->xfer[1]) {
1640 /* cannot change this during transfer */
1648 ugen_set_timeout(struct usb_fifo *f, void *addr)
1650 f->timeout = *(int *)addr;
1651 if (f->timeout > 65535) {
1652 /* limit user input */
1659 ugen_get_frame_size(struct usb_fifo *f, void *addr)
1662 *(int *)addr = f->xfer[0]->max_frame_size;
1670 ugen_set_buffer_size(struct usb_fifo *f, void *addr)
1674 if (*(int *)addr < 0)
1675 t = 0; /* use "wMaxPacketSize" */
1676 else if (*(int *)addr < (256 * 1024))
1681 if (f->bufsize == t) {
1682 /* same value like before - accept */
1685 if (f->xfer[0] || f->xfer[1]) {
1686 /* cannot change this during transfer */
1694 ugen_get_buffer_size(struct usb_fifo *f, void *addr)
1696 *(int *)addr = f->bufsize;
1701 ugen_get_iface_desc(struct usb_fifo *f,
1702 struct usb_interface_descriptor *idesc)
1704 struct usb_interface *iface;
1706 iface = usbd_get_iface(f->udev, f->iface_index);
1707 if (iface && iface->idesc) {
1708 *idesc = *(iface->idesc);
1716 ugen_get_endpoint_desc(struct usb_fifo *f,
1717 struct usb_endpoint_descriptor *ed)
1719 struct usb_endpoint *ep;
1721 ep = usb_fifo_softc(f);
1723 if (ep && ep->edesc) {
1732 ugen_set_power_mode(struct usb_fifo *f, int mode)
1734 struct usb_device *udev = f->udev;
1738 if ((udev == NULL) ||
1739 (udev->parent_hub == NULL)) {
1742 err = priv_check(curthread, PRIV_DRIVER);
1746 /* get old power mode */
1747 old_mode = udev->power_mode;
1749 /* if no change, then just return */
1750 if (old_mode == mode)
1754 case USB_POWER_MODE_OFF:
1755 if (udev->flags.usb_mode == USB_MODE_HOST &&
1756 udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
1757 udev->re_enumerate_wait = USB_RE_ENUM_PWR_OFF;
1759 /* set power mode will wake up the explore thread */
1762 case USB_POWER_MODE_ON:
1763 case USB_POWER_MODE_SAVE:
1766 case USB_POWER_MODE_RESUME:
1768 /* let USB-powerd handle resume */
1769 USB_BUS_LOCK(udev->bus);
1770 udev->pwr_save.write_refs++;
1771 udev->pwr_save.last_xfer_time = ticks;
1772 USB_BUS_UNLOCK(udev->bus);
1774 /* set new power mode */
1775 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1777 /* wait for resume to complete */
1778 usb_pause_mtx(NULL, hz / 4);
1780 /* clear write reference */
1781 USB_BUS_LOCK(udev->bus);
1782 udev->pwr_save.write_refs--;
1783 USB_BUS_UNLOCK(udev->bus);
1785 mode = USB_POWER_MODE_SAVE;
1788 case USB_POWER_MODE_SUSPEND:
1790 /* let USB-powerd handle suspend */
1791 USB_BUS_LOCK(udev->bus);
1792 udev->pwr_save.last_xfer_time = ticks - (256 * hz);
1793 USB_BUS_UNLOCK(udev->bus);
1795 mode = USB_POWER_MODE_SAVE;
1803 return (ENXIO); /* I/O failure */
1805 /* if we are powered off we need to re-enumerate first */
1806 if (old_mode == USB_POWER_MODE_OFF) {
1807 if (udev->flags.usb_mode == USB_MODE_HOST &&
1808 udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
1809 udev->re_enumerate_wait = USB_RE_ENUM_START;
1811 /* set power mode will wake up the explore thread */
1814 /* set new power mode */
1815 usbd_set_power_mode(udev, mode);
1817 return (0); /* success */
1821 ugen_get_power_mode(struct usb_fifo *f)
1823 struct usb_device *udev = f->udev;
1826 return (USB_POWER_MODE_ON);
1828 return (udev->power_mode);
1832 ugen_get_port_path(struct usb_fifo *f, struct usb_device_port_path *dpp)
1834 struct usb_device *udev = f->udev;
1835 struct usb_device *next;
1836 unsigned int nlevel = 0;
1841 dpp->udp_bus = device_get_unit(udev->bus->bdev);
1842 dpp->udp_index = udev->device_index;
1844 /* count port levels */
1846 while (next->parent_hub != NULL) {
1848 next = next->parent_hub;
1851 /* check if too many levels */
1852 if (nlevel > USB_DEVICE_PORT_PATH_MAX)
1855 /* store total level of ports */
1856 dpp->udp_port_level = nlevel;
1858 /* store port index array */
1860 while (next->parent_hub != NULL) {
1861 dpp->udp_port_no[--nlevel] = next->port_no;
1862 next = next->parent_hub;
1864 return (0); /* success */
1867 return (EINVAL); /* failure */
1871 ugen_get_power_usage(struct usb_fifo *f)
1873 struct usb_device *udev = f->udev;
1878 return (udev->power);
1882 ugen_do_port_feature(struct usb_fifo *f, uint8_t port_no,
1883 uint8_t set, uint16_t feature)
1885 struct usb_device *udev = f->udev;
1886 struct usb_hub *hub;
1889 err = priv_check(curthread, PRIV_DRIVER);
1896 if ((udev == NULL) ||
1897 (udev->hub == NULL)) {
1902 if (port_no > hub->nports) {
1906 err = usbd_req_set_port_feature(udev,
1907 NULL, port_no, feature);
1909 err = usbd_req_clear_port_feature(udev,
1910 NULL, port_no, feature);
1913 return (ENXIO); /* failure */
1915 return (0); /* success */
1919 ugen_iface_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
1921 struct usb_fifo *f_rx;
1922 struct usb_fifo *f_tx;
1925 f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX];
1926 f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX];
1929 case USB_SET_RX_SHORT_XFER:
1930 if (fflags & FREAD) {
1931 error = ugen_set_short_xfer(f_rx, addr);
1937 case USB_SET_TX_FORCE_SHORT:
1938 if (fflags & FWRITE) {
1939 error = ugen_set_short_xfer(f_tx, addr);
1945 case USB_SET_RX_TIMEOUT:
1946 if (fflags & FREAD) {
1947 error = ugen_set_timeout(f_rx, addr);
1953 case USB_SET_TX_TIMEOUT:
1954 if (fflags & FWRITE) {
1955 error = ugen_set_timeout(f_tx, addr);
1961 case USB_GET_RX_FRAME_SIZE:
1962 if (fflags & FREAD) {
1963 error = ugen_get_frame_size(f_rx, addr);
1969 case USB_GET_TX_FRAME_SIZE:
1970 if (fflags & FWRITE) {
1971 error = ugen_get_frame_size(f_tx, addr);
1977 case USB_SET_RX_BUFFER_SIZE:
1978 if (fflags & FREAD) {
1979 error = ugen_set_buffer_size(f_rx, addr);
1985 case USB_SET_TX_BUFFER_SIZE:
1986 if (fflags & FWRITE) {
1987 error = ugen_set_buffer_size(f_tx, addr);
1993 case USB_GET_RX_BUFFER_SIZE:
1994 if (fflags & FREAD) {
1995 error = ugen_get_buffer_size(f_rx, addr);
2001 case USB_GET_TX_BUFFER_SIZE:
2002 if (fflags & FWRITE) {
2003 error = ugen_get_buffer_size(f_tx, addr);
2009 case USB_GET_RX_INTERFACE_DESC:
2010 if (fflags & FREAD) {
2011 error = ugen_get_iface_desc(f_rx, addr);
2017 case USB_GET_TX_INTERFACE_DESC:
2018 if (fflags & FWRITE) {
2019 error = ugen_get_iface_desc(f_tx, addr);
2025 case USB_GET_RX_ENDPOINT_DESC:
2026 if (fflags & FREAD) {
2027 error = ugen_get_endpoint_desc(f_rx, addr);
2033 case USB_GET_TX_ENDPOINT_DESC:
2034 if (fflags & FWRITE) {
2035 error = ugen_get_endpoint_desc(f_tx, addr);
2041 case USB_SET_RX_STALL_FLAG:
2042 if ((fflags & FREAD) && (*(int *)addr)) {
2043 f_rx->flag_stall = 1;
2047 case USB_SET_TX_STALL_FLAG:
2048 if ((fflags & FWRITE) && (*(int *)addr)) {
2049 f_tx->flag_stall = 1;
2061 ugen_ioctl_post(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
2064 struct usb_interface_descriptor *idesc;
2065 struct usb_alt_interface *ai;
2066 struct usb_device_descriptor *ddesc;
2067 struct usb_config_descriptor *cdesc;
2068 struct usb_device_stats *stat;
2069 struct usb_fs_init *pinit;
2070 struct usb_fs_uninit *puninit;
2071 struct usb_device_port_path *dpp;
2076 struct usb_device_descriptor *dtemp;
2077 struct usb_config_descriptor *ctemp;
2078 struct usb_interface *iface;
2084 DPRINTFN(6, "cmd=0x%08lx\n", cmd);
2088 usb_needs_explore_all();
2092 if (!(fflags & FWRITE)) {
2096 usb_debug = *(int *)addr;
2099 case USB_GET_CONFIG:
2100 *(int *)addr = f->udev->curr_config_index;
2103 case USB_SET_CONFIG:
2104 if (!(fflags & FWRITE)) {
2108 error = ugen_set_config(f, *(int *)addr);
2111 case USB_GET_ALTINTERFACE:
2112 iface = usbd_get_iface(f->udev,
2113 u.ai->uai_interface_index);
2114 if (iface && iface->idesc) {
2115 u.ai->uai_alt_index = iface->alt_index;
2121 case USB_SET_ALTINTERFACE:
2122 if (!(fflags & FWRITE)) {
2126 error = ugen_set_interface(f,
2127 u.ai->uai_interface_index, u.ai->uai_alt_index);
2130 case USB_GET_DEVICE_DESC:
2131 dtemp = usbd_get_device_descriptor(f->udev);
2139 case USB_GET_CONFIG_DESC:
2140 ctemp = usbd_get_config_descriptor(f->udev);
2148 case USB_GET_FULL_DESC:
2149 error = ugen_get_cdesc(f, addr);
2152 case USB_GET_STRING_DESC:
2153 error = ugen_get_sdesc(f, addr);
2156 case USB_GET_IFACE_DRIVER:
2157 error = ugen_get_iface_driver(f, addr);
2161 case USB_DO_REQUEST:
2162 if (!(fflags & FWRITE)) {
2166 error = ugen_do_request(f, addr);
2169 case USB_DEVICEINFO:
2170 case USB_GET_DEVICEINFO:
2171 error = usb_gen_fill_deviceinfo(f, addr);
2174 case USB_DEVICESTATS:
2175 for (n = 0; n != 4; n++) {
2177 u.stat->uds_requests_fail[n] =
2178 f->udev->bus->stats_err.uds_requests[n];
2180 u.stat->uds_requests_ok[n] =
2181 f->udev->bus->stats_ok.uds_requests[n];
2185 case USB_DEVICEENUMERATE:
2186 error = ugen_re_enumerate(f);
2189 case USB_GET_PLUGTIME:
2190 *u.ptime = f->udev->plugtime;
2193 case USB_CLAIM_INTERFACE:
2194 case USB_RELEASE_INTERFACE:
2198 case USB_IFACE_DRIVER_ACTIVE:
2202 iface = usbd_get_iface(f->udev, n);
2204 if (iface && iface->subdev)
2210 case USB_IFACE_DRIVER_DETACH:
2212 error = priv_check(curthread, PRIV_DRIVER);
2219 if (n == USB_IFACE_INDEX_ANY) {
2225 * Detach the currently attached driver.
2227 usb_detach_device(f->udev, n, 0);
2230 * Set parent to self, this should keep attach away
2231 * until the next set configuration event.
2233 usbd_set_parent_iface(f->udev, n, n);
2236 case USB_SET_POWER_MODE:
2237 error = ugen_set_power_mode(f, *u.pint);
2240 case USB_GET_POWER_MODE:
2241 *u.pint = ugen_get_power_mode(f);
2244 case USB_GET_DEV_PORT_PATH:
2245 error = ugen_get_port_path(f, u.dpp);
2248 case USB_GET_POWER_USAGE:
2249 *u.pint = ugen_get_power_usage(f);
2252 case USB_SET_PORT_ENABLE:
2253 error = ugen_do_port_feature(f,
2254 *u.pint, 1, UHF_PORT_ENABLE);
2257 case USB_SET_PORT_DISABLE:
2258 error = ugen_do_port_feature(f,
2259 *u.pint, 0, UHF_PORT_ENABLE);
2263 /* verify input parameters */
2264 if (u.pinit->pEndpoints == NULL) {
2268 if (u.pinit->ep_index_max > 127) {
2272 if (u.pinit->ep_index_max == 0) {
2276 if (f->fs_xfer != NULL) {
2280 if (f->dev_ep_index != 0) {
2284 if (ugen_fifo_in_use(f, fflags)) {
2288 error = usb_fifo_alloc_buffer(f, 1, u.pinit->ep_index_max);
2292 f->fs_xfer = malloc(sizeof(f->fs_xfer[0]) *
2293 u.pinit->ep_index_max, M_USB, M_WAITOK | M_ZERO);
2294 if (f->fs_xfer == NULL) {
2295 usb_fifo_free_buffer(f);
2299 f->fs_ep_max = u.pinit->ep_index_max;
2300 f->fs_ep_ptr = u.pinit->pEndpoints;
2304 if (u.puninit->dummy != 0) {
2308 error = ugen_fs_uninit(f);
2312 mtx_lock(f->priv_mtx);
2313 error = ugen_iface_ioctl(f, cmd, addr, fflags);
2314 mtx_unlock(f->priv_mtx);
2317 DPRINTFN(6, "error=%d\n", error);
2322 ugen_ctrl_fs_callback(struct usb_xfer *xfer, usb_error_t error)
2324 ; /* workaround for a bug in "indent" */
2326 DPRINTF("st=%u alen=%u aframes=%u\n",
2327 USB_GET_STATE(xfer), xfer->actlen, xfer->aframes);
2329 switch (USB_GET_STATE(xfer)) {
2331 usbd_transfer_submit(xfer);
2334 ugen_fs_set_complete(xfer->priv_sc, USB_P2U(xfer->priv_fifo));
2338 #endif /* USB_HAVE_UGEN */