urio.c revision 227461
1184610Salfred/*-
2184610Salfred * Copyright (c) 2000 Iwasa Kazmi
3184610Salfred * All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions, and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18184610Salfred * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred *
26184610Salfred * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson.
27184610Salfred * This code includes software developed by the NetBSD Foundation, Inc. and
28184610Salfred * its contributors.
29184610Salfred */
30184610Salfred
31184610Salfred#include <sys/cdefs.h>
32184610Salfred__FBSDID("$FreeBSD: head/sys/dev/usb/storage/urio.c 227461 2011-11-12 08:16:45Z hselasky $");
33184610Salfred
34184610Salfred
35184610Salfred/*
36184610Salfred * 2000/3/24  added NetBSD/OpenBSD support (from Alex Nemirovsky)
37184610Salfred * 2000/3/07  use two bulk-pipe handles for read and write (Dirk)
38184610Salfred * 2000/3/06  change major number(143), and copyright header
39184610Salfred *            some fix for 4.0 (Dirk)
40184610Salfred * 2000/3/05  codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
41184610Salfred * 2000/3/01  remove retry code from urioioctl()
42184610Salfred *            change method of bulk transfer (no interrupt)
43184610Salfred * 2000/2/28  small fixes for new rio_usb.h
44184610Salfred * 2000/2/24  first version.
45184610Salfred */
46184610Salfred
47194677Sthompsa#include <sys/stdint.h>
48194677Sthompsa#include <sys/stddef.h>
49194677Sthompsa#include <sys/param.h>
50194677Sthompsa#include <sys/queue.h>
51194677Sthompsa#include <sys/types.h>
52194677Sthompsa#include <sys/systm.h>
53194677Sthompsa#include <sys/kernel.h>
54194677Sthompsa#include <sys/bus.h>
55194677Sthompsa#include <sys/module.h>
56194677Sthompsa#include <sys/lock.h>
57194677Sthompsa#include <sys/mutex.h>
58194677Sthompsa#include <sys/condvar.h>
59194677Sthompsa#include <sys/sysctl.h>
60194677Sthompsa#include <sys/sx.h>
61194677Sthompsa#include <sys/unistd.h>
62194677Sthompsa#include <sys/callout.h>
63194677Sthompsa#include <sys/malloc.h>
64194677Sthompsa#include <sys/priv.h>
65194677Sthompsa#include <sys/conf.h>
66194677Sthompsa#include <sys/fcntl.h>
67194677Sthompsa
68194677Sthompsa#include <dev/usb/usb.h>
69194677Sthompsa#include <dev/usb/usbdi.h>
70188746Sthompsa#include "usbdevs.h"
71194677Sthompsa
72188942Sthompsa#include <dev/usb/usb_ioctl.h>
73194677Sthompsa#include <dev/usb/usb_generic.h>
74184610Salfred
75184610Salfred#define	USB_DEBUG_VAR urio_debug
76188942Sthompsa#include <dev/usb/usb_debug.h>
77184610Salfred
78194677Sthompsa#include <dev/usb/storage/rio500_usb.h>
79194677Sthompsa
80207077Sthompsa#ifdef USB_DEBUG
81184610Salfredstatic int urio_debug = 0;
82184610Salfred
83227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
84192502SthompsaSYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
85184610Salfred    &urio_debug, 0, "urio debug level");
86184610Salfred#endif
87184610Salfred
88184610Salfred#define	URIO_T_WR     0
89184610Salfred#define	URIO_T_RD     1
90184610Salfred#define	URIO_T_WR_CS  2
91184610Salfred#define	URIO_T_RD_CS  3
92184610Salfred#define	URIO_T_MAX    4
93184610Salfred
94184610Salfred#define	URIO_BSIZE	(1<<12)		/* bytes */
95184610Salfred#define	URIO_IFQ_MAXLEN      2		/* units */
96184610Salfred
97184610Salfredstruct urio_softc {
98192984Sthompsa	struct usb_fifo_sc sc_fifo;
99184610Salfred	struct mtx sc_mtx;
100184610Salfred
101192984Sthompsa	struct usb_device *sc_udev;
102192984Sthompsa	struct usb_xfer *sc_xfer[URIO_T_MAX];
103184610Salfred
104184610Salfred	uint8_t	sc_flags;
105184610Salfred#define	URIO_FLAG_READ_STALL    0x01	/* read transfer stalled */
106184610Salfred#define	URIO_FLAG_WRITE_STALL   0x02	/* write transfer stalled */
107184610Salfred
108184610Salfred	uint8_t	sc_name[16];
109184610Salfred};
110184610Salfred
111184610Salfred/* prototypes */
112184610Salfred
113184610Salfredstatic device_probe_t urio_probe;
114184610Salfredstatic device_attach_t urio_attach;
115184610Salfredstatic device_detach_t urio_detach;
116184610Salfred
117193045Sthompsastatic usb_callback_t urio_write_callback;
118193045Sthompsastatic usb_callback_t urio_write_clear_stall_callback;
119193045Sthompsastatic usb_callback_t urio_read_callback;
120193045Sthompsastatic usb_callback_t urio_read_clear_stall_callback;
121184610Salfred
122193045Sthompsastatic usb_fifo_close_t urio_close;
123193045Sthompsastatic usb_fifo_cmd_t urio_start_read;
124193045Sthompsastatic usb_fifo_cmd_t urio_start_write;
125193045Sthompsastatic usb_fifo_cmd_t urio_stop_read;
126193045Sthompsastatic usb_fifo_cmd_t urio_stop_write;
127193045Sthompsastatic usb_fifo_ioctl_t urio_ioctl;
128193045Sthompsastatic usb_fifo_open_t urio_open;
129184610Salfred
130192984Sthompsastatic struct usb_fifo_methods urio_fifo_methods = {
131184610Salfred	.f_close = &urio_close,
132184610Salfred	.f_ioctl = &urio_ioctl,
133184610Salfred	.f_open = &urio_open,
134184610Salfred	.f_start_read = &urio_start_read,
135184610Salfred	.f_start_write = &urio_start_write,
136184610Salfred	.f_stop_read = &urio_stop_read,
137184610Salfred	.f_stop_write = &urio_stop_write,
138184610Salfred	.basename[0] = "urio",
139184610Salfred};
140184610Salfred
141192984Sthompsastatic const struct usb_config urio_config[URIO_T_MAX] = {
142184610Salfred	[URIO_T_WR] = {
143184610Salfred		.type = UE_BULK,
144184610Salfred		.endpoint = UE_ADDR_ANY,
145184610Salfred		.direction = UE_DIR_OUT,
146190734Sthompsa		.bufsize = URIO_BSIZE,
147190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.proxy_buffer = 1,},
148190734Sthompsa		.callback = &urio_write_callback,
149184610Salfred	},
150184610Salfred
151184610Salfred	[URIO_T_RD] = {
152184610Salfred		.type = UE_BULK,
153184610Salfred		.endpoint = UE_ADDR_ANY,
154184610Salfred		.direction = UE_DIR_IN,
155190734Sthompsa		.bufsize = URIO_BSIZE,
156190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1,},
157190734Sthompsa		.callback = &urio_read_callback,
158184610Salfred	},
159184610Salfred
160184610Salfred	[URIO_T_WR_CS] = {
161184610Salfred		.type = UE_CONTROL,
162184610Salfred		.endpoint = 0x00,	/* Control pipe */
163184610Salfred		.direction = UE_DIR_ANY,
164192984Sthompsa		.bufsize = sizeof(struct usb_device_request),
165190734Sthompsa		.callback = &urio_write_clear_stall_callback,
166190734Sthompsa		.timeout = 1000,	/* 1 second */
167190734Sthompsa		.interval = 50,	/* 50ms */
168184610Salfred	},
169184610Salfred
170184610Salfred	[URIO_T_RD_CS] = {
171184610Salfred		.type = UE_CONTROL,
172184610Salfred		.endpoint = 0x00,	/* Control pipe */
173184610Salfred		.direction = UE_DIR_ANY,
174192984Sthompsa		.bufsize = sizeof(struct usb_device_request),
175190734Sthompsa		.callback = &urio_read_clear_stall_callback,
176190734Sthompsa		.timeout = 1000,	/* 1 second */
177190734Sthompsa		.interval = 50,	/* 50ms */
178184610Salfred	},
179184610Salfred};
180184610Salfred
181184610Salfredstatic devclass_t urio_devclass;
182184610Salfred
183184610Salfredstatic device_method_t urio_methods[] = {
184184610Salfred	/* Device interface */
185184610Salfred	DEVMETHOD(device_probe, urio_probe),
186184610Salfred	DEVMETHOD(device_attach, urio_attach),
187184610Salfred	DEVMETHOD(device_detach, urio_detach),
188184610Salfred	{0, 0}
189184610Salfred};
190184610Salfred
191184610Salfredstatic driver_t urio_driver = {
192184610Salfred	.name = "urio",
193184610Salfred	.methods = urio_methods,
194184610Salfred	.size = sizeof(struct urio_softc),
195184610Salfred};
196184610Salfred
197189275SthompsaDRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, NULL, 0);
198188942SthompsaMODULE_DEPEND(urio, usb, 1, 1, 1);
199212122SthompsaMODULE_VERSION(urio, 1);
200184610Salfred
201223511Shselaskystatic const STRUCT_USB_HOST_ID urio_devs[] = {
202223511Shselasky	{USB_VPI(USB_VENDOR_DIAMOND, USB_PRODUCT_DIAMOND_RIO500USB, 0)},
203223511Shselasky	{USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO600USB, 0)},
204223511Shselasky	{USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO800USB, 0)},
205223511Shselasky};
206223511Shselasky
207184610Salfredstatic int
208184610Salfredurio_probe(device_t dev)
209184610Salfred{
210192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
211184610Salfred
212223511Shselasky	if (uaa->usb_mode != USB_MODE_HOST)
213184610Salfred		return (ENXIO);
214223511Shselasky	if (uaa->info.bConfigIndex != 0)
215184610Salfred		return (ENXIO);
216223511Shselasky	if (uaa->info.bIfaceIndex != 0)
217223511Shselasky		return (ENXIO);
218223511Shselasky
219223511Shselasky	return (usbd_lookup_id_by_uaa(urio_devs, sizeof(urio_devs), uaa));
220184610Salfred}
221184610Salfred
222184610Salfredstatic int
223184610Salfredurio_attach(device_t dev)
224184610Salfred{
225192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
226184610Salfred	struct urio_softc *sc = device_get_softc(dev);
227184610Salfred	int error;
228184610Salfred
229194228Sthompsa	device_set_usb_desc(dev);
230184610Salfred
231184610Salfred	sc->sc_udev = uaa->device;
232184610Salfred
233184610Salfred	mtx_init(&sc->sc_mtx, "urio lock", NULL, MTX_DEF | MTX_RECURSE);
234184610Salfred
235184610Salfred	snprintf(sc->sc_name, sizeof(sc->sc_name),
236184610Salfred	    "%s", device_get_nameunit(dev));
237184610Salfred
238194228Sthompsa	error = usbd_transfer_setup(uaa->device,
239184610Salfred	    &uaa->info.bIfaceIndex, sc->sc_xfer,
240184610Salfred	    urio_config, URIO_T_MAX, sc, &sc->sc_mtx);
241184610Salfred
242184610Salfred	if (error) {
243194228Sthompsa		DPRINTF("error=%s\n", usbd_errstr(error));
244184610Salfred		goto detach;
245184610Salfred	}
246184610Salfred
247194228Sthompsa	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
248184610Salfred	    &urio_fifo_methods, &sc->sc_fifo,
249189110Sthompsa	    device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
250189110Sthompsa	    UID_ROOT, GID_OPERATOR, 0644);
251184610Salfred	if (error) {
252184610Salfred		goto detach;
253184610Salfred	}
254184610Salfred	return (0);			/* success */
255184610Salfred
256184610Salfreddetach:
257184610Salfred	urio_detach(dev);
258184610Salfred	return (ENOMEM);		/* failure */
259184610Salfred}
260184610Salfred
261184610Salfredstatic void
262194677Sthompsaurio_write_callback(struct usb_xfer *xfer, usb_error_t error)
263184610Salfred{
264194677Sthompsa	struct urio_softc *sc = usbd_xfer_softc(xfer);
265192984Sthompsa	struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX];
266194677Sthompsa	struct usb_page_cache *pc;
267184610Salfred	uint32_t actlen;
268184610Salfred
269184610Salfred	switch (USB_GET_STATE(xfer)) {
270184610Salfred	case USB_ST_TRANSFERRED:
271184610Salfred	case USB_ST_SETUP:
272184610Salfred		if (sc->sc_flags & URIO_FLAG_WRITE_STALL) {
273194228Sthompsa			usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]);
274184610Salfred			return;
275184610Salfred		}
276194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
277194677Sthompsa		if (usb_fifo_get_data(f, pc, 0,
278194677Sthompsa		    usbd_xfer_max_len(xfer), &actlen, 0)) {
279184610Salfred
280194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, actlen);
281194228Sthompsa			usbd_transfer_submit(xfer);
282184610Salfred		}
283184610Salfred		return;
284184610Salfred
285184610Salfred	default:			/* Error */
286194677Sthompsa		if (error != USB_ERR_CANCELLED) {
287184610Salfred			/* try to clear stall first */
288184610Salfred			sc->sc_flags |= URIO_FLAG_WRITE_STALL;
289194228Sthompsa			usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]);
290184610Salfred		}
291184610Salfred		return;
292184610Salfred	}
293184610Salfred}
294184610Salfred
295184610Salfredstatic void
296194677Sthompsaurio_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
297184610Salfred{
298194677Sthompsa	struct urio_softc *sc = usbd_xfer_softc(xfer);
299192984Sthompsa	struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_WR];
300184610Salfred
301194228Sthompsa	if (usbd_clear_stall_callback(xfer, xfer_other)) {
302184610Salfred		DPRINTF("stall cleared\n");
303184610Salfred		sc->sc_flags &= ~URIO_FLAG_WRITE_STALL;
304194228Sthompsa		usbd_transfer_start(xfer_other);
305184610Salfred	}
306184610Salfred}
307184610Salfred
308184610Salfredstatic void
309194677Sthompsaurio_read_callback(struct usb_xfer *xfer, usb_error_t error)
310184610Salfred{
311194677Sthompsa	struct urio_softc *sc = usbd_xfer_softc(xfer);
312192984Sthompsa	struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_RX];
313194677Sthompsa	struct usb_page_cache *pc;
314194677Sthompsa	int actlen;
315184610Salfred
316194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
317194677Sthompsa
318184610Salfred	switch (USB_GET_STATE(xfer)) {
319184610Salfred	case USB_ST_TRANSFERRED:
320194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
321194677Sthompsa		usb_fifo_put_data(f, pc, 0, actlen, 1);
322184610Salfred
323184610Salfred	case USB_ST_SETUP:
324184610Salfred		if (sc->sc_flags & URIO_FLAG_READ_STALL) {
325194228Sthompsa			usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]);
326184610Salfred			return;
327184610Salfred		}
328194228Sthompsa		if (usb_fifo_put_bytes_max(f) != 0) {
329194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
330194228Sthompsa			usbd_transfer_submit(xfer);
331184610Salfred		}
332184610Salfred		return;
333184610Salfred
334184610Salfred	default:			/* Error */
335194677Sthompsa		if (error != USB_ERR_CANCELLED) {
336184610Salfred			/* try to clear stall first */
337184610Salfred			sc->sc_flags |= URIO_FLAG_READ_STALL;
338194228Sthompsa			usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]);
339184610Salfred		}
340184610Salfred		return;
341184610Salfred	}
342184610Salfred}
343184610Salfred
344184610Salfredstatic void
345194677Sthompsaurio_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
346184610Salfred{
347194677Sthompsa	struct urio_softc *sc = usbd_xfer_softc(xfer);
348192984Sthompsa	struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_RD];
349184610Salfred
350194228Sthompsa	if (usbd_clear_stall_callback(xfer, xfer_other)) {
351184610Salfred		DPRINTF("stall cleared\n");
352184610Salfred		sc->sc_flags &= ~URIO_FLAG_READ_STALL;
353194228Sthompsa		usbd_transfer_start(xfer_other);
354184610Salfred	}
355184610Salfred}
356184610Salfred
357184610Salfredstatic void
358192984Sthompsaurio_start_read(struct usb_fifo *fifo)
359184610Salfred{
360194677Sthompsa	struct urio_softc *sc = usb_fifo_softc(fifo);
361184610Salfred
362194228Sthompsa	usbd_transfer_start(sc->sc_xfer[URIO_T_RD]);
363184610Salfred}
364184610Salfred
365184610Salfredstatic void
366192984Sthompsaurio_stop_read(struct usb_fifo *fifo)
367184610Salfred{
368194677Sthompsa	struct urio_softc *sc = usb_fifo_softc(fifo);
369184610Salfred
370194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[URIO_T_RD_CS]);
371194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[URIO_T_RD]);
372184610Salfred}
373184610Salfred
374184610Salfredstatic void
375192984Sthompsaurio_start_write(struct usb_fifo *fifo)
376184610Salfred{
377194677Sthompsa	struct urio_softc *sc = usb_fifo_softc(fifo);
378184610Salfred
379194228Sthompsa	usbd_transfer_start(sc->sc_xfer[URIO_T_WR]);
380184610Salfred}
381184610Salfred
382184610Salfredstatic void
383192984Sthompsaurio_stop_write(struct usb_fifo *fifo)
384184610Salfred{
385194677Sthompsa	struct urio_softc *sc = usb_fifo_softc(fifo);
386184610Salfred
387194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[URIO_T_WR_CS]);
388194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[URIO_T_WR]);
389184610Salfred}
390184610Salfred
391184610Salfredstatic int
392192984Sthompsaurio_open(struct usb_fifo *fifo, int fflags)
393184610Salfred{
394194677Sthompsa	struct urio_softc *sc = usb_fifo_softc(fifo);
395184610Salfred
396184610Salfred	if (fflags & FREAD) {
397184610Salfred		/* clear stall first */
398184610Salfred		mtx_lock(&sc->sc_mtx);
399184610Salfred		sc->sc_flags |= URIO_FLAG_READ_STALL;
400184610Salfred		mtx_unlock(&sc->sc_mtx);
401184610Salfred
402194228Sthompsa		if (usb_fifo_alloc_buffer(fifo,
403194677Sthompsa		    usbd_xfer_max_len(sc->sc_xfer[URIO_T_RD]),
404184610Salfred		    URIO_IFQ_MAXLEN)) {
405184610Salfred			return (ENOMEM);
406184610Salfred		}
407184610Salfred	}
408184610Salfred	if (fflags & FWRITE) {
409184610Salfred		/* clear stall first */
410184610Salfred		sc->sc_flags |= URIO_FLAG_WRITE_STALL;
411184610Salfred
412194228Sthompsa		if (usb_fifo_alloc_buffer(fifo,
413194677Sthompsa		    usbd_xfer_max_len(sc->sc_xfer[URIO_T_WR]),
414184610Salfred		    URIO_IFQ_MAXLEN)) {
415184610Salfred			return (ENOMEM);
416184610Salfred		}
417184610Salfred	}
418184610Salfred	return (0);			/* success */
419184610Salfred}
420184610Salfred
421184610Salfredstatic void
422192984Sthompsaurio_close(struct usb_fifo *fifo, int fflags)
423184610Salfred{
424184610Salfred	if (fflags & (FREAD | FWRITE)) {
425194228Sthompsa		usb_fifo_free_buffer(fifo);
426184610Salfred	}
427184610Salfred}
428184610Salfred
429184610Salfredstatic int
430192984Sthompsaurio_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
431189110Sthompsa    int fflags)
432184610Salfred{
433192984Sthompsa	struct usb_ctl_request ur;
434184610Salfred	struct RioCommand *rio_cmd;
435184610Salfred	int error;
436184610Salfred
437184610Salfred	switch (cmd) {
438184610Salfred	case RIO_RECV_COMMAND:
439184610Salfred		if (!(fflags & FWRITE)) {
440184610Salfred			error = EPERM;
441184610Salfred			goto done;
442184610Salfred		}
443227461Shselasky		memset(&ur, 0, sizeof(ur));
444184610Salfred		rio_cmd = addr;
445184610Salfred		ur.ucr_request.bmRequestType =
446184610Salfred		    rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
447184610Salfred		break;
448184610Salfred
449184610Salfred	case RIO_SEND_COMMAND:
450184610Salfred		if (!(fflags & FWRITE)) {
451184610Salfred			error = EPERM;
452184610Salfred			goto done;
453184610Salfred		}
454227461Shselasky		memset(&ur, 0, sizeof(ur));
455184610Salfred		rio_cmd = addr;
456184610Salfred		ur.ucr_request.bmRequestType =
457184610Salfred		    rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
458184610Salfred		break;
459184610Salfred
460184610Salfred	default:
461184610Salfred		error = EINVAL;
462184610Salfred		goto done;
463184610Salfred	}
464184610Salfred
465184610Salfred	DPRINTFN(2, "Sending command\n");
466184610Salfred
467184610Salfred	/* Send rio control message */
468184610Salfred	ur.ucr_request.bRequest = rio_cmd->request;
469184610Salfred	USETW(ur.ucr_request.wValue, rio_cmd->value);
470184610Salfred	USETW(ur.ucr_request.wIndex, rio_cmd->index);
471184610Salfred	USETW(ur.ucr_request.wLength, rio_cmd->length);
472184610Salfred	ur.ucr_data = rio_cmd->buffer;
473184610Salfred
474184610Salfred	/* reuse generic USB code */
475184610Salfred	error = ugen_do_request(fifo, &ur);
476184610Salfred
477184610Salfreddone:
478184610Salfred	return (error);
479184610Salfred}
480184610Salfred
481184610Salfredstatic int
482184610Salfredurio_detach(device_t dev)
483184610Salfred{
484184610Salfred	struct urio_softc *sc = device_get_softc(dev);
485184610Salfred
486184610Salfred	DPRINTF("\n");
487184610Salfred
488194228Sthompsa	usb_fifo_detach(&sc->sc_fifo);
489184610Salfred
490194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, URIO_T_MAX);
491184610Salfred
492184610Salfred	mtx_destroy(&sc->sc_mtx);
493184610Salfred
494184610Salfred	return (0);
495184610Salfred}
496