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.26 2007/11/06 07:37:01 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/ioccom.h>
55 #include <sys/fcntl.h>
56 #include <sys/filio.h>
61 #include <sys/select.h>
63 #include <sys/sysctl.h>
65 #include <sys/thread2.h>
67 #include <bus/usb/usb.h>
68 #include <bus/usb/usbdi.h>
69 #include <bus/usb/usbdi_util.h>
71 #include <bus/usb/rio500_usb.h>
74 #define DPRINTF(x) if (uriodebug) kprintf x
75 #define DPRINTFN(n,x) if (uriodebug>(n)) kprintf x
77 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
78 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
79 &uriodebug, 0, "urio debug level");
85 /* difference of usbd interface */
98 #define URIO_CDEV_MAJOR 143
100 static struct dev_ops urio_ops = {
101 { "urio", URIO_CDEV_MAJOR, 0 },
103 .d_close = urioclose,
105 .d_write = uriowrite,
106 .d_ioctl = urioioctl,
108 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
109 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
112 #define URIO_BBSIZE 1024
116 usbd_device_handle sc_udev;
117 usbd_interface_handle sc_iface;
120 usbd_pipe_handle sc_pipeh_in;
121 usbd_pipe_handle sc_pipeh_out;
127 #define URIOUNIT(n) (minor(n))
129 #define RIO_RW_TIMEOUT 4000 /* ms */
131 static device_probe_t urio_match;
132 static device_attach_t urio_attach;
133 static device_detach_t urio_detach;
135 static devclass_t urio_devclass;
137 static kobj_method_t urio_methods[] = {
138 DEVMETHOD(device_probe, urio_match),
139 DEVMETHOD(device_attach, urio_attach),
140 DEVMETHOD(device_detach, urio_detach),
144 static driver_t urio_driver = {
147 sizeof(struct urio_softc)
150 static const struct usb_devno urio_devs[] = {
151 { USB_DEVICE(0x045a, 0x5001) }, /* Diamond Multimedia Rio 600 */
152 { USB_DEVICE(0x045a, 0x5002) }, /* Diamond Multimedia Rio 800 */
153 { USB_DEVICE(0x0841, 0x0001) }, /* Diamond Multimedia Rio 500 */
156 MODULE_DEPEND(urio, usb, 1, 1, 1);
159 urio_match(device_t self)
161 struct usb_attach_arg *uaa = device_get_ivars(self);
163 DPRINTFN(10,("urio_match\n"));
164 if (uaa->iface == NULL)
165 return (UMATCH_NONE);
167 return (usb_lookup(urio_devs, uaa->vendor, uaa->product) != NULL ?
168 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
172 urio_attach(device_t self)
174 struct urio_softc *sc = device_get_softc(self);
175 struct usb_attach_arg *uaa = device_get_ivars(self);
176 usbd_device_handle udev;
177 usbd_interface_handle iface;
180 char * ermsg = "<none>";
183 DPRINTFN(10,("urio_attach: sc=%p\n", sc));
185 sc->sc_udev = udev = uaa->device;
187 if ((!uaa->device) || (!uaa->iface)) {
188 ermsg = "device or iface";
191 sc->sc_iface = iface = uaa->iface;
194 sc->sc_pipeh_out = 0;
197 r = usbd_endpoint_count(iface, &epcount);
198 if (r != USBD_NORMAL_COMPLETION) {
203 sc->sc_epaddr[RIO_OUT] = 0xff;
204 sc->sc_epaddr[RIO_IN] = 0x00;
206 for (i = 0; i < epcount; i++) {
207 usb_endpoint_descriptor_t *edesc =
208 usbd_interface2endpoint_descriptor(iface, i);
212 ermsg = "interface endpoint";
216 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
218 sc->sc_epaddr[d] = edesc->bEndpointAddress;
220 if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
221 sc->sc_epaddr[RIO_IN] == 0x00) {
226 dev_ops_add(&urio_ops, -1, device_get_unit(self));
227 make_dev(&urio_ops, device_get_unit(self),
228 UID_ROOT, GID_OPERATOR,
229 0644, "urio%d", device_get_unit(self));
231 DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
236 kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
242 urioopen(struct dev_open_args *ap)
244 cdev_t dev = ap->a_head.a_dev;
246 struct urio_softc * sc;
248 int unit = URIOUNIT(dev);
249 sc = devclass_get_softc(urio_devclass, unit);
253 DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
254 ap->a_oflags, ap->a_devtype, unit));
259 if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
264 sc->sc_pipeh_out = 0;
265 if (usbd_open_pipe(sc->sc_iface,
266 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
267 != USBD_NORMAL_COMPLETION)
272 if (usbd_open_pipe(sc->sc_iface,
273 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
274 != USBD_NORMAL_COMPLETION)
276 usbd_close_pipe(sc->sc_pipeh_in);
278 sc->sc_pipeh_out = 0;
285 urioclose(struct dev_close_args *ap)
287 cdev_t dev = ap->a_head.a_dev;
289 struct urio_softc * sc;
291 int unit = URIOUNIT(dev);
292 sc = devclass_get_softc(urio_devclass, unit);
294 DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n",
295 ap->a_fflag, ap->a_devtype, unit));
297 usbd_close_pipe(sc->sc_pipeh_in);
299 if (sc->sc_pipeh_out)
300 usbd_close_pipe(sc->sc_pipeh_out);
303 sc->sc_pipeh_out = 0;
310 urioread(struct dev_read_args *ap)
312 cdev_t dev = ap->a_head.a_dev;
313 struct uio *uio = ap->a_uio;
315 struct urio_softc * sc;
316 usbd_xfer_handle reqh;
318 usbd_request_handle reqh;
319 usbd_private_handle r_priv;
321 usbd_status r_status;
323 int unit = URIOUNIT(dev);
325 char buf[URIO_BBSIZE];
329 sc = devclass_get_softc(urio_devclass, unit);
331 DPRINTFN(5, ("urioread: %d\n", unit));
337 reqh = usbd_alloc_xfer(sc->sc_udev);
339 reqh = usbd_alloc_request();
343 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
344 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
347 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
348 0, RIO_RW_TIMEOUT, 0);
350 r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
351 0, RIO_RW_TIMEOUT, 0);
352 if (r != USBD_NORMAL_COMPLETION) {
357 r = usbd_sync_transfer(reqh);
358 if (r != USBD_NORMAL_COMPLETION) {
359 DPRINTFN(1, ("urioread: error=%d\n", r));
360 usbd_clear_endpoint_stall(sc->sc_pipeh_in);
366 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
368 usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
371 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
372 error = uiomove(buf, tn, uio);
377 usbd_free_xfer(reqh);
379 usbd_free_request(reqh);
386 uriowrite(struct dev_write_args *ap)
388 cdev_t dev = ap->a_head.a_dev;
389 struct uio *uio = ap->a_uio;
391 struct urio_softc * sc;
392 usbd_xfer_handle reqh;
394 usbd_request_handle reqh;
396 int unit = URIOUNIT(dev);
398 char buf[URIO_BBSIZE];
402 sc = devclass_get_softc(urio_devclass, unit);
404 DPRINTFN(5, ("uriowrite: %d\n", unit));
410 reqh = usbd_alloc_xfer(sc->sc_udev);
412 reqh = usbd_alloc_request();
416 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
417 error = uiomove(buf, n, uio);
420 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
422 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
423 0, RIO_RW_TIMEOUT, 0);
425 r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
426 0, RIO_RW_TIMEOUT, 0);
427 if (r != USBD_NORMAL_COMPLETION) {
432 r = usbd_sync_transfer(reqh);
433 if (r != USBD_NORMAL_COMPLETION) {
434 DPRINTFN(1, ("uriowrite: error=%d\n", r));
435 usbd_clear_endpoint_stall(sc->sc_pipeh_out);
440 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
445 usbd_free_xfer(reqh);
447 usbd_free_request(reqh);
455 urioioctl(struct dev_ioctl_args *ap)
457 cdev_t dev = ap->a_head.a_dev;
459 struct urio_softc * sc;
461 int unit = URIOUNIT(dev);
462 struct RioCommand *rio_cmd;
463 int requesttype, len;
466 usb_device_request_t req;
467 int req_flags = 0, req_actlen = 0;
472 sc = devclass_get_softc(urio_devclass, unit);
475 case RIO_RECV_COMMAND:
476 if (!(ap->a_fflag & FWRITE))
478 rio_cmd = (struct RioCommand *)ap->a_data;
481 len = rio_cmd->length;
483 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
484 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
485 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
488 case RIO_SEND_COMMAND:
489 if (!(ap->a_fflag & FWRITE))
491 rio_cmd = (struct RioCommand *)ap->a_data;
494 len = rio_cmd->length;
496 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
497 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
498 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
506 /* Send rio control message */
507 req.bmRequestType = requesttype;
508 req.bRequest = rio_cmd->request;
509 USETW(req.wValue, rio_cmd->value);
510 USETW(req.wIndex, rio_cmd->index);
511 USETW(req.wLength, len);
513 if (len < 0 || len > 32767)
516 iov.iov_base = (caddr_t)rio_cmd->buffer;
522 uio.uio_segflg = UIO_USERSPACE;
524 req.bmRequestType & UT_READ ?
525 UIO_READ : UIO_WRITE;
526 uio.uio_td = curthread;
527 ptr = kmalloc(len, M_TEMP, M_WAITOK);
528 if (uio.uio_rw == UIO_WRITE) {
529 error = uiomove(ptr, len, &uio);
535 r = usbd_do_request_flags(sc->sc_udev, &req,
536 ptr, req_flags, &req_actlen,
537 USBD_DEFAULT_TIMEOUT);
538 if (r == USBD_NORMAL_COMPLETION) {
541 if (uio.uio_rw == UIO_READ) {
542 error = uiomove(ptr, len, &uio);
556 urio_detach(device_t self)
558 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
559 dev_ops_remove(&urio_ops, -1, device_get_unit(self));
560 /* XXX not implemented yet */
561 device_set_desc(self, NULL);
565 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);