2 * Copyright (c) 2000 Iwasa Kazmi
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 FOR
18 * 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
26 * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson.
27 * This code includes software developed by the NetBSD Foundation, Inc. and
32 * $FreeBSD: src/sys/dev/usb/urio.c,v 1.28 2003/08/25 22:01:06 joe Exp $
33 * $DragonFly: src/sys/dev/usbmisc/urio/urio.c,v 1.27 2008/08/14 20:55:54 hasso Exp $
37 * 2000/3/24 added NetBSD/OpenBSD support (from Alex Nemirovsky)
38 * 2000/3/07 use two bulk-pipe handles for read and write (Dirk)
39 * 2000/3/06 change major number(143), and copyright header
40 * some fix for 4.0 (Dirk)
41 * 2000/3/05 codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
42 * 2000/3/01 remove retry code from urioioctl()
43 * change method of bulk transfer (no interrupt)
44 * 2000/2/28 small fixes for new rio_usb.h
45 * 2000/2/24 first version.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
54 #include <sys/fcntl.h>
55 #include <sys/filio.h>
60 #include <sys/select.h>
62 #include <sys/sysctl.h>
64 #include <sys/thread2.h>
66 #include <bus/usb/usb.h>
67 #include <bus/usb/usbdi.h>
68 #include <bus/usb/usbdi_util.h>
70 #include <bus/usb/rio500_usb.h>
73 #define DPRINTF(x) if (uriodebug) kprintf x
74 #define DPRINTFN(n,x) if (uriodebug>(n)) kprintf x
76 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
77 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
78 &uriodebug, 0, "urio debug level");
84 /* difference of usbd interface */
97 #define URIO_CDEV_MAJOR 143
99 static struct dev_ops urio_ops = {
100 { "urio", URIO_CDEV_MAJOR, 0 },
102 .d_close = urioclose,
104 .d_write = uriowrite,
105 .d_ioctl = urioioctl,
107 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
108 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
111 #define URIO_BBSIZE 1024
115 usbd_device_handle sc_udev;
116 usbd_interface_handle sc_iface;
119 usbd_pipe_handle sc_pipeh_in;
120 usbd_pipe_handle sc_pipeh_out;
126 #define URIOUNIT(n) (minor(n))
128 #define RIO_RW_TIMEOUT 4000 /* ms */
130 static device_probe_t urio_match;
131 static device_attach_t urio_attach;
132 static device_detach_t urio_detach;
134 static devclass_t urio_devclass;
136 static kobj_method_t urio_methods[] = {
137 DEVMETHOD(device_probe, urio_match),
138 DEVMETHOD(device_attach, urio_attach),
139 DEVMETHOD(device_detach, urio_detach),
143 static driver_t urio_driver = {
146 sizeof(struct urio_softc)
149 static const struct usb_devno urio_devs[] = {
150 { USB_DEVICE(0x045a, 0x5001) }, /* Diamond Multimedia Rio 600 */
151 { USB_DEVICE(0x045a, 0x5002) }, /* Diamond Multimedia Rio 800 */
152 { USB_DEVICE(0x0841, 0x0001) }, /* Diamond Multimedia Rio 500 */
155 MODULE_DEPEND(urio, usb, 1, 1, 1);
158 urio_match(device_t self)
160 struct usb_attach_arg *uaa = device_get_ivars(self);
162 DPRINTFN(10,("urio_match\n"));
163 if (uaa->iface == NULL)
164 return (UMATCH_NONE);
166 return (usb_lookup(urio_devs, uaa->vendor, uaa->product) != NULL ?
167 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
171 urio_attach(device_t self)
173 struct urio_softc *sc = device_get_softc(self);
174 struct usb_attach_arg *uaa = device_get_ivars(self);
175 usbd_interface_handle iface;
178 char * ermsg = "<none>";
181 DPRINTFN(10,("urio_attach: sc=%p\n", sc));
183 sc->sc_udev = uaa->device;
185 if ((!uaa->device) || (!uaa->iface)) {
186 ermsg = "device or iface";
189 sc->sc_iface = iface = uaa->iface;
192 sc->sc_pipeh_out = 0;
195 r = usbd_endpoint_count(iface, &epcount);
196 if (r != USBD_NORMAL_COMPLETION) {
201 sc->sc_epaddr[RIO_OUT] = 0xff;
202 sc->sc_epaddr[RIO_IN] = 0x00;
204 for (i = 0; i < epcount; i++) {
205 usb_endpoint_descriptor_t *edesc =
206 usbd_interface2endpoint_descriptor(iface, i);
210 ermsg = "interface endpoint";
214 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
216 sc->sc_epaddr[d] = edesc->bEndpointAddress;
218 if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
219 sc->sc_epaddr[RIO_IN] == 0x00) {
224 dev_ops_add(&urio_ops, -1, device_get_unit(self));
225 make_dev(&urio_ops, device_get_unit(self),
226 UID_ROOT, GID_OPERATOR,
227 0644, "urio%d", device_get_unit(self));
229 DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
234 kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
240 urioopen(struct dev_open_args *ap)
242 cdev_t dev = ap->a_head.a_dev;
244 struct urio_softc * sc;
246 int unit = URIOUNIT(dev);
247 sc = devclass_get_softc(urio_devclass, unit);
251 DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
252 ap->a_oflags, ap->a_devtype, unit));
257 if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
262 sc->sc_pipeh_out = 0;
263 if (usbd_open_pipe(sc->sc_iface,
264 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
265 != USBD_NORMAL_COMPLETION)
270 if (usbd_open_pipe(sc->sc_iface,
271 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
272 != USBD_NORMAL_COMPLETION)
274 usbd_close_pipe(sc->sc_pipeh_in);
276 sc->sc_pipeh_out = 0;
283 urioclose(struct dev_close_args *ap)
285 cdev_t dev = ap->a_head.a_dev;
287 struct urio_softc * sc;
289 int unit = URIOUNIT(dev);
290 sc = devclass_get_softc(urio_devclass, unit);
292 DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n",
293 ap->a_fflag, ap->a_devtype, unit));
295 usbd_close_pipe(sc->sc_pipeh_in);
297 if (sc->sc_pipeh_out)
298 usbd_close_pipe(sc->sc_pipeh_out);
301 sc->sc_pipeh_out = 0;
308 urioread(struct dev_read_args *ap)
310 cdev_t dev = ap->a_head.a_dev;
311 struct uio *uio = ap->a_uio;
313 struct urio_softc * sc;
314 usbd_xfer_handle reqh;
316 usbd_request_handle reqh;
317 usbd_private_handle r_priv;
319 usbd_status r_status;
321 int unit = URIOUNIT(dev);
323 char buf[URIO_BBSIZE];
327 sc = devclass_get_softc(urio_devclass, unit);
329 DPRINTFN(5, ("urioread: %d\n", unit));
335 reqh = usbd_alloc_xfer(sc->sc_udev);
337 reqh = usbd_alloc_request();
341 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
342 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
345 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
346 0, RIO_RW_TIMEOUT, 0);
348 r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
349 0, RIO_RW_TIMEOUT, 0);
350 if (r != USBD_NORMAL_COMPLETION) {
355 r = usbd_sync_transfer(reqh);
356 if (r != USBD_NORMAL_COMPLETION) {
357 DPRINTFN(1, ("urioread: error=%d\n", r));
358 usbd_clear_endpoint_stall(sc->sc_pipeh_in);
364 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
366 usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
369 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
370 error = uiomove(buf, tn, uio);
375 usbd_free_xfer(reqh);
377 usbd_free_request(reqh);
384 uriowrite(struct dev_write_args *ap)
386 cdev_t dev = ap->a_head.a_dev;
387 struct uio *uio = ap->a_uio;
389 struct urio_softc * sc;
390 usbd_xfer_handle reqh;
392 usbd_request_handle reqh;
394 int unit = URIOUNIT(dev);
396 char buf[URIO_BBSIZE];
400 sc = devclass_get_softc(urio_devclass, unit);
402 DPRINTFN(5, ("uriowrite: %d\n", unit));
408 reqh = usbd_alloc_xfer(sc->sc_udev);
410 reqh = usbd_alloc_request();
414 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
415 error = uiomove(buf, n, uio);
418 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
420 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
421 0, RIO_RW_TIMEOUT, 0);
423 r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
424 0, RIO_RW_TIMEOUT, 0);
425 if (r != USBD_NORMAL_COMPLETION) {
430 r = usbd_sync_transfer(reqh);
431 if (r != USBD_NORMAL_COMPLETION) {
432 DPRINTFN(1, ("uriowrite: error=%d\n", r));
433 usbd_clear_endpoint_stall(sc->sc_pipeh_out);
438 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
443 usbd_free_xfer(reqh);
445 usbd_free_request(reqh);
453 urioioctl(struct dev_ioctl_args *ap)
455 cdev_t dev = ap->a_head.a_dev;
457 struct urio_softc * sc;
459 int unit = URIOUNIT(dev);
460 struct RioCommand *rio_cmd;
461 int requesttype, len;
464 usb_device_request_t req;
465 int req_flags = 0, req_actlen = 0;
470 sc = devclass_get_softc(urio_devclass, unit);
473 case RIO_RECV_COMMAND:
474 if (!(ap->a_fflag & FWRITE))
476 rio_cmd = (struct RioCommand *)ap->a_data;
479 len = rio_cmd->length;
481 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
482 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
483 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
486 case RIO_SEND_COMMAND:
487 if (!(ap->a_fflag & FWRITE))
489 rio_cmd = (struct RioCommand *)ap->a_data;
492 len = rio_cmd->length;
494 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
495 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
496 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
504 /* Send rio control message */
505 req.bmRequestType = requesttype;
506 req.bRequest = rio_cmd->request;
507 USETW(req.wValue, rio_cmd->value);
508 USETW(req.wIndex, rio_cmd->index);
509 USETW(req.wLength, len);
511 if (len < 0 || len > 32767)
514 iov.iov_base = (caddr_t)rio_cmd->buffer;
520 uio.uio_segflg = UIO_USERSPACE;
522 req.bmRequestType & UT_READ ?
523 UIO_READ : UIO_WRITE;
524 uio.uio_td = curthread;
525 ptr = kmalloc(len, M_TEMP, M_WAITOK);
526 if (uio.uio_rw == UIO_WRITE) {
527 error = uiomove(ptr, len, &uio);
533 r = usbd_do_request_flags(sc->sc_udev, &req,
534 ptr, req_flags, &req_actlen,
535 USBD_DEFAULT_TIMEOUT);
536 if (r == USBD_NORMAL_COMPLETION) {
539 if (uio.uio_rw == UIO_READ) {
540 error = uiomove(ptr, len, &uio);
554 urio_detach(device_t self)
556 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
557 dev_ops_remove(&urio_ops, -1, device_get_unit(self));
558 /* XXX not implemented yet */
559 device_set_desc(self, NULL);
563 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);