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.11 2005/06/02 20:41:04 dillon 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 #if defined(__NetBSD__)
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #elif defined(__FreeBSD__) || defined(__DragonFly__)
56 #include <sys/module.h>
58 #include <sys/ioccom.h>
60 #include <sys/fcntl.h>
61 #include <sys/filio.h>
66 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
67 #include <sys/selinfo.h>
69 #include <sys/select.h>
71 #include <sys/vnode.h>
73 #include <sys/sysctl.h>
75 #include <sys/thread2.h>
77 #include <bus/usb/usb.h>
78 #include <bus/usb/usbdi.h>
79 #include <bus/usb/usbdi_util.h>
81 #include <bus/usb/usbdevs.h>
82 #include <bus/usb/rio500_usb.h>
85 #define DPRINTF(x) if (uriodebug) logprintf x
86 #define DPRINTFN(n,x) if (uriodebug>(n)) logprintf x
88 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
89 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
90 &uriodebug, 0, "urio debug level");
96 /* difference of usbd interface */
103 #if defined(__NetBSD__)
104 int urioopen(dev_t, int, int, struct proc *);
105 int urioclose(dev_t, int, int, struct proc *p);
106 int urioread(dev_t, struct uio *uio, int);
107 int uriowrite(dev_t, struct uio *uio, int);
108 int urioioctl(dev_t, u_long, caddr_t, int, struct proc *);
111 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
112 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
114 #elif defined(__FreeBSD__) || defined(__DragonFly__)
121 #define URIO_CDEV_MAJOR 143
123 Static struct cdevsw urio_cdevsw = {
125 /* cmaj */ URIO_CDEV_MAJOR,
129 urioopen, urioclose, urioread, uriowrite,
130 urioioctl, nopoll, nommap, nostrategy,
133 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
134 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
136 #endif /*defined(__FreeBSD__)*/
138 #define URIO_BBSIZE 1024
141 USBBASEDEVICE sc_dev;
142 usbd_device_handle sc_udev;
143 usbd_interface_handle sc_iface;
146 usbd_pipe_handle sc_pipeh_in;
147 usbd_pipe_handle sc_pipeh_out;
151 #if defined(__NetBSD__) || defined(__OpenBSD__)
156 #define URIOUNIT(n) (minor(n))
158 #define RIO_RW_TIMEOUT 4000 /* ms */
160 USB_DECLARE_DRIVER(urio);
164 USB_MATCH_START(urio, uaa);
165 usb_device_descriptor_t *dd;
167 DPRINTFN(10,("urio_match\n"));
171 dd = usbd_get_device_descriptor(uaa->device);
174 ((UGETW(dd->idVendor) == USB_VENDOR_DIAMOND &&
175 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND_RIO500USB) ||
176 (UGETW(dd->idVendor) == USB_VENDOR_DIAMOND2 &&
177 (UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO600USB ||
178 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO800USB))))
179 return UMATCH_VENDOR_PRODUCT;
186 USB_ATTACH_START(urio, sc, uaa);
188 usbd_device_handle udev;
189 usbd_interface_handle iface;
191 #if defined(__NetBSD__) || defined(__OpenBSD__)
195 char * ermsg = "<none>";
198 DPRINTFN(10,("urio_attach: sc=%p\n", sc));
199 usbd_devinfo(uaa->device, 0, devinfo);
201 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
203 sc->sc_udev = udev = uaa->device;
205 #if defined(__FreeBSD__) || defined(__DragonFly__)
206 if ((!uaa->device) || (!uaa->iface)) {
207 ermsg = "device or iface";
210 sc->sc_iface = iface = uaa->iface;
211 #elif defined(__NetBSD__) || defined(__OpenBSD__)
216 r = usbd_interface_count(udev, &niface);
221 r = usbd_device2interface_handle(udev, 0, &iface);
226 sc->sc_iface = iface;
230 sc->sc_pipeh_out = 0;
233 r = usbd_endpoint_count(iface, &epcount);
234 if (r != USBD_NORMAL_COMPLETION) {
239 sc->sc_epaddr[RIO_OUT] = 0xff;
240 sc->sc_epaddr[RIO_IN] = 0x00;
242 for (i = 0; i < epcount; i++) {
243 usb_endpoint_descriptor_t *edesc =
244 usbd_interface2endpoint_descriptor(iface, i);
248 ermsg = "interface endpoint";
252 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
254 sc->sc_epaddr[d] = edesc->bEndpointAddress;
256 if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
257 sc->sc_epaddr[RIO_IN] == 0x00) {
262 #if defined(__FreeBSD__) || defined(__DragonFly__)
263 cdevsw_add(&urio_cdevsw, -1, device_get_unit(self));
264 make_dev(&urio_cdevsw, device_get_unit(self),
265 UID_ROOT, GID_OPERATOR,
266 0644, "urio%d", device_get_unit(self));
267 #elif defined(__NetBSD__) || defined(__OpenBSD__)
268 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
272 DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
274 USB_ATTACH_SUCCESS_RETURN;
277 printf("%s: could not find %s\n", USBDEVNAME(sc->sc_dev),ermsg);
278 USB_ATTACH_ERROR_RETURN;
283 urioopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
286 struct urio_softc * sc;
288 int unit = URIOUNIT(dev);
289 USB_GET_SC_OPEN(urio, unit, sc);
291 DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
297 if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
302 sc->sc_pipeh_out = 0;
303 if (usbd_open_pipe(sc->sc_iface,
304 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
305 != USBD_NORMAL_COMPLETION)
310 if (usbd_open_pipe(sc->sc_iface,
311 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
312 != USBD_NORMAL_COMPLETION)
314 usbd_close_pipe(sc->sc_pipeh_in);
316 sc->sc_pipeh_out = 0;
323 urioclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
326 struct urio_softc * sc;
328 int unit = URIOUNIT(dev);
329 USB_GET_SC(urio, unit, sc);
331 DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n", flag, mode, unit));
333 usbd_close_pipe(sc->sc_pipeh_in);
335 if (sc->sc_pipeh_out)
336 usbd_close_pipe(sc->sc_pipeh_out);
339 sc->sc_pipeh_out = 0;
346 urioread(dev_t dev, struct uio *uio, int flag)
349 struct urio_softc * sc;
350 usbd_xfer_handle reqh;
352 usbd_request_handle reqh;
353 usbd_private_handle r_priv;
355 usbd_status r_status;
357 int unit = URIOUNIT(dev);
359 char buf[URIO_BBSIZE];
363 USB_GET_SC(urio, unit, sc);
365 DPRINTFN(5, ("urioread: %d\n", unit));
371 reqh = usbd_alloc_xfer(sc->sc_udev);
373 reqh = usbd_alloc_request();
377 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
378 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
381 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
382 0, RIO_RW_TIMEOUT, 0);
384 r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
385 0, RIO_RW_TIMEOUT, 0);
386 if (r != USBD_NORMAL_COMPLETION) {
391 r = usbd_sync_transfer(reqh);
392 if (r != USBD_NORMAL_COMPLETION) {
393 DPRINTFN(1, ("urioread: error=%d\n", r));
394 usbd_clear_endpoint_stall(sc->sc_pipeh_in);
400 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
402 usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
405 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
406 error = uiomove(buf, tn, uio);
411 usbd_free_xfer(reqh);
413 usbd_free_request(reqh);
420 uriowrite(dev_t dev, struct uio *uio, int flag)
423 struct urio_softc * sc;
424 usbd_xfer_handle reqh;
426 usbd_request_handle reqh;
428 int unit = URIOUNIT(dev);
430 char buf[URIO_BBSIZE];
434 USB_GET_SC(urio, unit, sc);
436 DPRINTFN(5, ("uriowrite: %d\n", unit));
442 reqh = usbd_alloc_xfer(sc->sc_udev);
444 reqh = usbd_alloc_request();
448 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
449 error = uiomove(buf, n, uio);
452 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
454 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
455 0, RIO_RW_TIMEOUT, 0);
457 r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
458 0, RIO_RW_TIMEOUT, 0);
459 if (r != USBD_NORMAL_COMPLETION) {
464 r = usbd_sync_transfer(reqh);
465 if (r != USBD_NORMAL_COMPLETION) {
466 DPRINTFN(1, ("uriowrite: error=%d\n", r));
467 usbd_clear_endpoint_stall(sc->sc_pipeh_out);
472 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
477 usbd_free_xfer(reqh);
479 usbd_free_request(reqh);
487 urioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
490 struct urio_softc * sc;
492 int unit = URIOUNIT(dev);
493 struct RioCommand *rio_cmd;
494 int requesttype, len;
497 usb_device_request_t req;
498 int req_flags = 0, req_actlen = 0;
503 USB_GET_SC(urio, unit, sc);
506 case RIO_RECV_COMMAND:
507 if (!(flag & FWRITE))
509 rio_cmd = (struct RioCommand *)addr;
512 len = rio_cmd->length;
514 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
515 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
516 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
519 case RIO_SEND_COMMAND:
520 if (!(flag & FWRITE))
522 rio_cmd = (struct RioCommand *)addr;
525 len = rio_cmd->length;
527 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
528 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
529 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
537 /* Send rio control message */
538 req.bmRequestType = requesttype;
539 req.bRequest = rio_cmd->request;
540 USETW(req.wValue, rio_cmd->value);
541 USETW(req.wIndex, rio_cmd->index);
542 USETW(req.wLength, len);
544 if (len < 0 || len > 32767)
547 iov.iov_base = (caddr_t)rio_cmd->buffer;
553 uio.uio_segflg = UIO_USERSPACE;
555 req.bmRequestType & UT_READ ?
556 UIO_READ : UIO_WRITE;
558 ptr = malloc(len, M_TEMP, M_WAITOK);
559 if (uio.uio_rw == UIO_WRITE) {
560 error = uiomove(ptr, len, &uio);
566 r = usbd_do_request_flags(sc->sc_udev, &req,
567 ptr, req_flags, &req_actlen,
568 USBD_DEFAULT_TIMEOUT);
569 if (r == USBD_NORMAL_COMPLETION) {
572 if (uio.uio_rw == UIO_READ) {
573 error = uiomove(ptr, len, &uio);
587 #if defined(__NetBSD__) || defined(__OpenBSD__)
589 urio_activate(device_ptr_t self, enum devact act)
591 struct urio_softc *sc = (struct urio_softc *)self;
598 case DVACT_DEACTIVATE:
607 USB_DETACH_START(urio, sc);
608 struct urio_endpoint *sce;
611 #if defined(__NetBSD__) || defined(__OpenBSD__)
614 DPRINTF(("urio_detach: sc=%p flags=%d\n", sc, flags));
615 #elif defined(__FreeBSD__) || defined(__DragonFly__)
616 DPRINTF(("urio_detach: sc=%p\n", sc));
620 /* Abort all pipes. Causes processes waiting for transfer to wake. */
622 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
623 for (dir = OUT; dir <= IN; dir++) {
624 sce = &sc->sc_endpoints[i][dir];
625 if (sce && sce->pipeh)
626 usbd_abort_pipe(sce->pipeh);
631 if (--sc->sc_refcnt >= 0) {
633 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
634 wakeup(&sc->sc_endpoints[i][IN]);
635 /* Wait for processes to go away. */
636 usb_detach_wait(USBDEV(sc->sc_dev));
641 usbd_abort_pipe(sc->sc_pipeh_in);
643 if (sc->sc_pipeh_out)
644 usbd_abort_pipe(sc->sc_pipeh_out);
647 if (--sc->sc_refcnt >= 0) {
648 /* Wait for processes to go away. */
649 usb_detach_wait(USBDEV(sc->sc_dev));
654 #if defined(__NetBSD__) || defined(__OpenBSD__)
655 /* locate the major number */
656 for (maj = 0; maj < nchrdev; maj++)
657 if (cdevsw[maj].d_open == urioopen)
660 /* Nuke the vnodes for any open instances (calls close). */
661 mn = self->dv_unit * USB_MAX_ENDPOINTS;
662 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
665 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
670 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
672 #if defined(__FreeBSD__) || defined(__DragonFly__)
674 urio_detach(device_t self)
676 DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
677 cdevsw_remove(&urio_cdevsw, -1, device_get_unit(self));
678 /* XXX not implemented yet */
679 device_set_desc(self, NULL);
683 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);