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 $
36 * 2000/3/24 added NetBSD/OpenBSD support (from Alex Nemirovsky)
37 * 2000/3/07 use two bulk-pipe handles for read and write (Dirk)
38 * 2000/3/06 change major number(143), and copyright header
39 * some fix for 4.0 (Dirk)
40 * 2000/3/05 codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
41 * 2000/3/01 remove retry code from urioioctl()
42 * change method of bulk transfer (no interrupt)
43 * 2000/2/28 small fixes for new rio_usb.h
44 * 2000/2/24 first version.
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
53 #include <sys/fcntl.h>
54 #include <sys/filio.h>
59 #include <sys/select.h>
61 #include <sys/sysctl.h>
63 #include <sys/thread2.h>
65 #include <bus/usb/usb.h>
66 #include <bus/usb/usbdi.h>
67 #include <bus/usb/usbdi_util.h>
69 #include <bus/usb/rio500_usb.h>
72 #define DPRINTF(x) if (uriodebug) kprintf x
73 #define DPRINTFN(n,x) if (uriodebug>(n)) kprintf x
75 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
76 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
77 &uriodebug, 0, "urio debug level");
83 /* difference of usbd interface */
96 static struct dev_ops urio_ops = {
101 .d_write = uriowrite,
102 .d_ioctl = urioioctl,
104 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
105 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
108 #define URIO_BBSIZE 1024
112 usbd_device_handle sc_udev;
113 usbd_interface_handle sc_iface;
116 usbd_pipe_handle sc_pipeh_in;
117 usbd_pipe_handle sc_pipeh_out;
123 #define URIOUNIT(n) (minor(n))
125 #define RIO_RW_TIMEOUT 4000 /* ms */
127 static device_probe_t urio_match;
128 static device_attach_t urio_attach;
129 static device_detach_t urio_detach;
131 static devclass_t urio_devclass;
133 static kobj_method_t urio_methods[] = {
134 DEVMETHOD(device_probe, urio_match),
135 DEVMETHOD(device_attach, urio_attach),
136 DEVMETHOD(device_detach, urio_detach),
140 static driver_t urio_driver = {
143 sizeof(struct urio_softc)
146 static const struct usb_devno urio_devs[] = {
147 { USB_DEVICE(0x045a, 0x5001) }, /* Diamond Multimedia Rio 600 */
148 { USB_DEVICE(0x045a, 0x5002) }, /* Diamond Multimedia Rio 800 */
149 { USB_DEVICE(0x0841, 0x0001) }, /* Diamond Multimedia Rio 500 */
152 MODULE_DEPEND(urio, usb, 1, 1, 1);
155 urio_match(device_t self)
157 struct usb_attach_arg *uaa = device_get_ivars(self);
159 DPRINTFN(10,("urio_match\n"));
160 if (uaa->iface == NULL)
161 return (UMATCH_NONE);
163 return (usb_lookup(urio_devs, uaa->vendor, uaa->product) != NULL ?
164 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
168 urio_attach(device_t self)
170 struct urio_softc *sc = device_get_softc(self);
171 struct usb_attach_arg *uaa = device_get_ivars(self);
172 usbd_interface_handle iface;
175 char * ermsg = "<none>";
178 DPRINTFN(10,("urio_attach: sc=%p\n", sc));
180 sc->sc_udev = uaa->device;
182 if ((!uaa->device) || (!uaa->iface)) {
183 ermsg = "device or iface";
186 sc->sc_iface = iface = uaa->iface;
189 sc->sc_pipeh_out = 0;
192 r = usbd_endpoint_count(iface, &epcount);
193 if (r != USBD_NORMAL_COMPLETION) {
198 sc->sc_epaddr[RIO_OUT] = 0xff;
199 sc->sc_epaddr[RIO_IN] = 0x00;
201 for (i = 0; i < epcount; i++) {
202 usb_endpoint_descriptor_t *edesc =
203 usbd_interface2endpoint_descriptor(iface, i);
207 ermsg = "interface endpoint";
211 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
213 sc->sc_epaddr[d] = edesc->bEndpointAddress;
215 if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
216 sc->sc_epaddr[RIO_IN] == 0x00) {
221 make_dev(&urio_ops, device_get_unit(self),
222 UID_ROOT, GID_OPERATOR,
223 0644, "urio%d", device_get_unit(self));
225 DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
230 kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
236 urioopen(struct dev_open_args *ap)
238 cdev_t dev = ap->a_head.a_dev;
240 struct urio_softc * sc;
242 int unit = URIOUNIT(dev);
243 sc = devclass_get_softc(urio_devclass, unit);
247 DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
248 ap->a_oflags, ap->a_devtype, unit));
253 if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
258 sc->sc_pipeh_out = 0;
259 if (usbd_open_pipe(sc->sc_iface,
260 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
261 != USBD_NORMAL_COMPLETION)
266 if (usbd_open_pipe(sc->sc_iface,
267 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
268 != USBD_NORMAL_COMPLETION)
270 usbd_close_pipe(sc->sc_pipeh_in);
272 sc->sc_pipeh_out = 0;
279 urioclose(struct dev_close_args *ap)
281 cdev_t dev = ap->a_head.a_dev;
283 struct urio_softc * sc;
285 int unit = URIOUNIT(dev);
286 sc = devclass_get_softc(urio_devclass, unit);
288 DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n",
289 ap->a_fflag, ap->a_devtype, unit));
291 usbd_close_pipe(sc->sc_pipeh_in);
293 if (sc->sc_pipeh_out)
294 usbd_close_pipe(sc->sc_pipeh_out);
297 sc->sc_pipeh_out = 0;
304 urioread(struct dev_read_args *ap)
306 cdev_t dev = ap->a_head.a_dev;
307 struct uio *uio = ap->a_uio;
309 struct urio_softc * sc;
310 usbd_xfer_handle reqh;
312 usbd_request_handle reqh;
313 usbd_private_handle r_priv;
315 usbd_status r_status;
317 int unit = URIOUNIT(dev);
319 char buf[URIO_BBSIZE];
323 sc = devclass_get_softc(urio_devclass, unit);
325 DPRINTFN(5, ("urioread: %d\n", unit));
331 reqh = usbd_alloc_xfer(sc->sc_udev);
333 reqh = usbd_alloc_request();
337 while ((n = szmin(URIO_BBSIZE, uio->uio_resid)) != 0) {
338 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
341 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
342 0, RIO_RW_TIMEOUT, 0);
344 r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
345 0, RIO_RW_TIMEOUT, 0);
346 if (r != USBD_NORMAL_COMPLETION) {
351 r = usbd_sync_transfer(reqh);
352 if (r != USBD_NORMAL_COMPLETION) {
353 DPRINTFN(1, ("urioread: error=%d\n", r));
354 usbd_clear_endpoint_stall(sc->sc_pipeh_in);
360 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
362 usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
365 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
366 error = uiomove(buf, tn, uio);
371 usbd_free_xfer(reqh);
373 usbd_free_request(reqh);
380 uriowrite(struct dev_write_args *ap)
382 cdev_t dev = ap->a_head.a_dev;
383 struct uio *uio = ap->a_uio;
385 struct urio_softc * sc;
386 usbd_xfer_handle reqh;
388 usbd_request_handle reqh;
390 int unit = URIOUNIT(dev);
392 char buf[URIO_BBSIZE];
396 sc = devclass_get_softc(urio_devclass, unit);
398 DPRINTFN(5, ("uriowrite: %d\n", unit));
404 reqh = usbd_alloc_xfer(sc->sc_udev);
406 reqh = usbd_alloc_request();
410 while ((n = szmin(URIO_BBSIZE, uio->uio_resid)) != 0) {
411 error = uiomove(buf, n, uio);
414 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
416 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
417 0, RIO_RW_TIMEOUT, 0);
419 r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
420 0, RIO_RW_TIMEOUT, 0);
421 if (r != USBD_NORMAL_COMPLETION) {
426 r = usbd_sync_transfer(reqh);
427 if (r != USBD_NORMAL_COMPLETION) {
428 DPRINTFN(1, ("uriowrite: error=%d\n", r));
429 usbd_clear_endpoint_stall(sc->sc_pipeh_out);
434 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
439 usbd_free_xfer(reqh);
441 usbd_free_request(reqh);
449 urioioctl(struct dev_ioctl_args *ap)
451 cdev_t dev = ap->a_head.a_dev;
453 struct urio_softc * sc;
455 int unit = URIOUNIT(dev);
456 struct RioCommand *rio_cmd;
457 int requesttype, len;
460 usb_device_request_t req;
461 int req_flags = 0, req_actlen = 0;
466 sc = devclass_get_softc(urio_devclass, unit);
469 case RIO_RECV_COMMAND:
470 if (!(ap->a_fflag & FWRITE))
472 rio_cmd = (struct RioCommand *)ap->a_data;
475 len = rio_cmd->length;
477 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
478 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
479 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
482 case RIO_SEND_COMMAND:
483 if (!(ap->a_fflag & FWRITE))
485 rio_cmd = (struct RioCommand *)ap->a_data;
488 len = rio_cmd->length;
490 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
491 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
492 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
500 /* Send rio control message */
501 req.bmRequestType = requesttype;
502 req.bRequest = rio_cmd->request;
503 USETW(req.wValue, rio_cmd->value);
504 USETW(req.wIndex, rio_cmd->index);
505 USETW(req.wLength, len);
507 if (len < 0 || len > 32767)
510 iov.iov_base = (caddr_t)rio_cmd->buffer;
516 uio.uio_segflg = UIO_USERSPACE;
518 req.bmRequestType & UT_READ ?
519 UIO_READ : UIO_WRITE;
520 uio.uio_td = curthread;
521 ptr = kmalloc(len, M_TEMP, M_WAITOK);
522 if (uio.uio_rw == UIO_WRITE) {
523 error = uiomove(ptr, len, &uio);
529 r = usbd_do_request_flags(sc->sc_udev, &req,
530 ptr, req_flags, &req_actlen,
531 USBD_DEFAULT_TIMEOUT);
532 if (r == USBD_NORMAL_COMPLETION) {
535 if (uio.uio_rw == UIO_READ) {
536 error = uiomove(ptr, len, &uio);
550 urio_detach(device_t self)
552 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
553 dev_ops_remove_minor(&urio_ops, /*-1, */device_get_unit(self));
554 /* XXX not implemented yet */
555 device_set_desc(self, NULL);
559 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, NULL);