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
31 /* $FreeBSD: src/sys/dev/usb/urio.c,v 1.11.2.4 2002/11/06 14:41:01 joe Exp $ */
34 * 2000/3/24 added NetBSD/OpenBSD support (from Alex Nemirovsky)
35 * 2000/3/07 use two bulk-pipe handles for read and write (Dirk)
36 * 2000/3/06 change major number(143), and copyright header
37 * some fix for 4.0 (Dirk)
38 * 2000/3/05 codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
39 * 2000/3/01 remove retry code from urioioctl()
40 * change method of bulk transfer (no interrupt)
41 * 2000/2/28 small fixes for new rio_usb.h
42 * 2000/2/24 first version.
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #if defined(__NetBSD__)
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #elif defined(__FreeBSD__)
53 #include <sys/module.h>
55 #include <sys/ioccom.h>
57 #include <sys/fcntl.h>
58 #include <sys/filio.h>
63 #if __FreeBSD_version >= 500014
64 #include <sys/selinfo.h>
66 #include <sys/select.h>
68 #include <sys/vnode.h>
70 #include <sys/sysctl.h>
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdi_util.h>
76 #include <dev/usb/usbdevs.h>
77 #include <dev/usb/rio500_usb.h>
80 #define DPRINTF(x) if (uriodebug) logprintf x
81 #define DPRINTFN(n,x) if (uriodebug>(n)) logprintf x
83 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
84 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
85 &uriodebug, 0, "urio debug level");
91 /* difference of usbd interface */
92 #if defined(__FreeBSD__)
93 #if (__FreeBSD__ >= 4)
98 #elif defined(__NetBSD__) || defined(__OpenBSD__)
106 #if defined(__NetBSD__)
107 int urioopen(dev_t, int, int, usb_proc_ptr);
108 int urioclose(dev_t, int, int, usb_proc_ptr);
109 int urioread(dev_t, struct uio *uio, int);
110 int uriowrite(dev_t, struct uio *uio, int);
111 int urioioctl(dev_t, u_long, caddr_t, int, usb_proc_ptr);
114 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
115 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
117 #elif defined(__FreeBSD__)
124 #define URIO_CDEV_MAJOR 143
126 #if (__FreeBSD__ >= 4)
127 Static struct cdevsw urio_cdevsw = {
128 urioopen, urioclose, urioread, uriowrite,
129 urioioctl, nopoll, nommap, nostrategy,
130 "urio", URIO_CDEV_MAJOR,nodump, nopsize,
132 #if (__FreeBSD__ < 5)
136 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
137 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
140 Static struct cdevsw urio_cdevsw = {
141 urioopen, urioclose, urioread, uriowrite,
142 urioioctl, nostop, nullreset, nodevtotty,
143 seltrue, nommap, nostrat,
146 #define USBBASEDEVICE bdevice
147 #define RIO_UE_GET_DIR(p) UE_GET_IN(p)
150 #endif /*defined(__FreeBSD__)*/
152 #define URIO_BBSIZE 1024
155 USBBASEDEVICE sc_dev;
156 usbd_device_handle sc_udev;
157 usbd_interface_handle sc_iface;
160 usbd_pipe_handle sc_pipeh_in;
161 usbd_pipe_handle sc_pipeh_out;
165 #if defined(__NetBSD__) || defined(__OpenBSD__)
170 #define URIOUNIT(n) (minor(n))
172 #define RIO_RW_TIMEOUT 4000 /* ms */
174 USB_DECLARE_DRIVER(urio);
178 USB_MATCH_START(urio, uaa);
179 usb_device_descriptor_t *dd;
181 DPRINTFN(10,("urio_match\n"));
185 dd = usbd_get_device_descriptor(uaa->device);
188 ((UGETW(dd->idVendor) == USB_VENDOR_DIAMOND &&
189 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND_RIO500USB) ||
190 (UGETW(dd->idVendor) == USB_VENDOR_DIAMOND2 &&
191 (UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO600USB ||
192 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO800USB))))
193 return UMATCH_VENDOR_PRODUCT;
200 USB_ATTACH_START(urio, sc, uaa);
202 usbd_device_handle udev;
203 usbd_interface_handle iface;
205 #if defined(__NetBSD__) || defined(__OpenBSD__)
209 char * ermsg = "<none>";
212 DPRINTFN(10,("urio_attach: sc=%p\n", sc));
213 usbd_devinfo(uaa->device, 0, devinfo);
215 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
217 sc->sc_udev = udev = uaa->device;
219 #if defined(__FreeBSD__)
220 if ((!uaa->device) || (!uaa->iface)) {
221 ermsg = "device or iface";
224 sc->sc_iface = iface = uaa->iface;
225 #elif defined(__NetBSD__) || defined(__OpenBSD__)
230 r = usbd_interface_count(udev, &niface);
235 r = usbd_device2interface_handle(udev, 0, &iface);
240 sc->sc_iface = iface;
244 sc->sc_pipeh_out = 0;
247 r = usbd_endpoint_count(iface, &epcount);
248 if (r != USBD_NORMAL_COMPLETION) {
253 sc->sc_epaddr[RIO_OUT] = 0xff;
254 sc->sc_epaddr[RIO_IN] = 0x00;
256 for (i = 0; i < epcount; i++) {
257 usb_endpoint_descriptor_t *edesc =
258 usbd_interface2endpoint_descriptor(iface, i);
262 ermsg = "interface endpoint";
266 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
268 sc->sc_epaddr[d] = edesc->bEndpointAddress;
270 if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
271 sc->sc_epaddr[RIO_IN] == 0x00) {
276 #if defined(__FreeBSD__)
277 #if (__FreeBSD__ >= 4)
278 /* XXX no error trapping, no storing of dev_t */
279 (void) make_dev(&urio_cdevsw, device_get_unit(self),
280 UID_ROOT, GID_OPERATOR,
281 0644, "urio%d", device_get_unit(self));
283 #elif defined(__NetBSD__) || defined(__OpenBSD__)
284 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
288 DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
290 USB_ATTACH_SUCCESS_RETURN;
293 printf("%s: could not find %s\n", USBDEVNAME(sc->sc_dev),ermsg);
294 USB_ATTACH_ERROR_RETURN;
299 urioopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
302 struct urio_softc * sc;
304 int unit = URIOUNIT(dev);
305 USB_GET_SC_OPEN(urio, unit, sc);
307 DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
313 if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
318 sc->sc_pipeh_out = 0;
319 if (usbd_open_pipe(sc->sc_iface,
320 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
321 != USBD_NORMAL_COMPLETION)
326 if (usbd_open_pipe(sc->sc_iface,
327 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
328 != USBD_NORMAL_COMPLETION)
330 usbd_close_pipe(sc->sc_pipeh_in);
332 sc->sc_pipeh_out = 0;
339 urioclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
342 struct urio_softc * sc;
344 int unit = URIOUNIT(dev);
345 USB_GET_SC(urio, unit, sc);
347 DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n", flag, mode, unit));
349 usbd_close_pipe(sc->sc_pipeh_in);
351 if (sc->sc_pipeh_out)
352 usbd_close_pipe(sc->sc_pipeh_out);
355 sc->sc_pipeh_out = 0;
362 urioread(dev_t dev, struct uio *uio, int flag)
365 struct urio_softc * sc;
366 usbd_xfer_handle reqh;
368 usbd_request_handle reqh;
369 usbd_private_handle r_priv;
371 usbd_status r_status;
373 int unit = URIOUNIT(dev);
375 char buf[URIO_BBSIZE];
379 USB_GET_SC(urio, unit, sc);
381 DPRINTFN(5, ("urioread: %d\n", unit));
387 reqh = usbd_alloc_xfer(sc->sc_udev);
389 reqh = usbd_alloc_request();
393 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
394 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
397 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
398 0, RIO_RW_TIMEOUT, 0);
400 r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
401 0, RIO_RW_TIMEOUT, 0);
402 if (r != USBD_NORMAL_COMPLETION) {
407 r = usbd_sync_transfer(reqh);
408 if (r != USBD_NORMAL_COMPLETION) {
409 DPRINTFN(1, ("urioread: error=%d\n", r));
410 usbd_clear_endpoint_stall(sc->sc_pipeh_in);
416 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
418 usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
421 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
422 error = uiomove(buf, tn, uio);
427 usbd_free_xfer(reqh);
429 usbd_free_request(reqh);
436 uriowrite(dev_t dev, struct uio *uio, int flag)
439 struct urio_softc * sc;
440 usbd_xfer_handle reqh;
442 usbd_request_handle reqh;
444 int unit = URIOUNIT(dev);
446 char buf[URIO_BBSIZE];
450 USB_GET_SC(urio, unit, sc);
452 DPRINTFN(5, ("uriowrite: %d\n", unit));
458 reqh = usbd_alloc_xfer(sc->sc_udev);
460 reqh = usbd_alloc_request();
464 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
465 error = uiomove(buf, n, uio);
468 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
470 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
471 0, RIO_RW_TIMEOUT, 0);
473 r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
474 0, RIO_RW_TIMEOUT, 0);
475 if (r != USBD_NORMAL_COMPLETION) {
480 r = usbd_sync_transfer(reqh);
481 if (r != USBD_NORMAL_COMPLETION) {
482 DPRINTFN(1, ("uriowrite: error=%d\n", r));
483 usbd_clear_endpoint_stall(sc->sc_pipeh_out);
488 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
493 usbd_free_xfer(reqh);
495 usbd_free_request(reqh);
503 urioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
506 struct urio_softc * sc;
508 int unit = URIOUNIT(dev);
509 struct RioCommand *rio_cmd;
510 int requesttype, len;
513 usb_device_request_t req;
514 int req_flags = 0, req_actlen = 0;
519 USB_GET_SC(urio, unit, sc);
522 case RIO_RECV_COMMAND:
523 if (!(flag & FWRITE))
525 rio_cmd = (struct RioCommand *)addr;
528 len = rio_cmd->length;
530 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
531 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
532 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
535 case RIO_SEND_COMMAND:
536 if (!(flag & FWRITE))
538 rio_cmd = (struct RioCommand *)addr;
541 len = rio_cmd->length;
543 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
544 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
545 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
553 /* Send rio control message */
554 req.bmRequestType = requesttype;
555 req.bRequest = rio_cmd->request;
556 USETW(req.wValue, rio_cmd->value);
557 USETW(req.wIndex, rio_cmd->index);
558 USETW(req.wLength, len);
560 if (len < 0 || len > 32767)
563 iov.iov_base = (caddr_t)rio_cmd->buffer;
569 uio.uio_segflg = UIO_USERSPACE;
571 req.bmRequestType & UT_READ ?
572 UIO_READ : UIO_WRITE;
574 ptr = malloc(len, M_TEMP, M_WAITOK);
575 if (uio.uio_rw == UIO_WRITE) {
576 error = uiomove(ptr, len, &uio);
582 r = usbd_do_request_flags(sc->sc_udev, &req,
583 ptr, req_flags, &req_actlen);
584 if (r == USBD_NORMAL_COMPLETION) {
587 if (uio.uio_rw == UIO_READ) {
588 error = uiomove(ptr, len, &uio);
602 #if defined(__NetBSD__) || defined(__OpenBSD__)
604 urio_activate(device_ptr_t self, enum devact act)
606 struct urio_softc *sc = (struct urio_softc *)self;
613 case DVACT_DEACTIVATE:
622 USB_DETACH_START(urio, sc);
623 struct urio_endpoint *sce;
626 #if defined(__NetBSD__) || defined(__OpenBSD__)
629 DPRINTF(("urio_detach: sc=%p flags=%d\n", sc, flags));
630 #elif defined(__FreeBSD__)
631 DPRINTF(("urio_detach: sc=%p\n", sc));
635 /* Abort all pipes. Causes processes waiting for transfer to wake. */
637 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
638 for (dir = OUT; dir <= IN; dir++) {
639 sce = &sc->sc_endpoints[i][dir];
640 if (sce && sce->pipeh)
641 usbd_abort_pipe(sce->pipeh);
646 if (--sc->sc_refcnt >= 0) {
648 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
649 wakeup(&sc->sc_endpoints[i][IN]);
650 /* Wait for processes to go away. */
651 usb_detach_wait(USBDEV(sc->sc_dev));
656 usbd_abort_pipe(sc->sc_pipeh_in);
658 if (sc->sc_pipeh_out)
659 usbd_abort_pipe(sc->sc_pipeh_out);
662 if (--sc->sc_refcnt >= 0) {
663 /* Wait for processes to go away. */
664 usb_detach_wait(USBDEV(sc->sc_dev));
669 #if defined(__NetBSD__) || defined(__OpenBSD__)
670 /* locate the major number */
671 for (maj = 0; maj < nchrdev; maj++)
672 if (cdevsw[maj].d_open == urioopen)
675 /* Nuke the vnodes for any open instances (calls close). */
676 mn = self->dv_unit * USB_MAX_ENDPOINTS;
677 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
678 #elif defined(__FreeBSD__)
679 /* XXX not implemented yet */
682 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
687 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
689 #if defined(__FreeBSD__)
691 urio_detach(device_t self)
693 DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
694 device_set_desc(self, NULL);
698 #if (__FreeBSD__ >= 4)
699 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);
701 CDEV_DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass,
702 URIO_CDEV_MAJOR, urio_cdevsw, usbd_driver_load, 0);