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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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 #if defined(__NetBSD__)
52 #include <sys/device.h>
53 #include <sys/ioctl.h>
54 #elif defined(__FreeBSD__)
55 #include <sys/module.h>
57 #include <sys/ioccom.h>
59 #include <sys/fcntl.h>
60 #include <sys/filio.h>
65 #include <sys/selinfo.h>
67 #include <sys/sysctl.h>
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbdi.h>
72 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/rio500_usb.h>
78 #define DPRINTF(x) if (uriodebug) logprintf x
79 #define DPRINTFN(n,x) if (uriodebug>(n)) logprintf x
81 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
82 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
83 &uriodebug, 0, "urio debug level");
89 /* difference of usbd interface */
96 #if defined(__NetBSD__)
97 int urioopen(dev_t, int, int, struct proc *);
98 int urioclose(dev_t, int, int, struct proc *p);
99 int urioread(dev_t, struct uio *uio, int);
100 int uriowrite(dev_t, struct uio *uio, int);
101 int urioioctl(dev_t, u_long, caddr_t, int, struct proc *);
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 :\
107 #elif defined(__FreeBSD__)
115 static struct cdevsw urio_cdevsw = {
116 .d_version = D_VERSION,
117 .d_flags = D_NEEDGIANT,
119 .d_close = urioclose,
121 .d_write = uriowrite,
122 .d_ioctl = urioioctl,
125 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
126 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
128 #endif /*defined(__FreeBSD__)*/
130 #define URIO_BBSIZE 1024
134 usbd_device_handle sc_udev;
135 usbd_interface_handle sc_iface;
138 usbd_pipe_handle sc_pipeh_in;
139 usbd_pipe_handle sc_pipeh_out;
143 #if defined(__FreeBSD__)
144 struct cdev *sc_dev_t;
145 #endif /* defined(__FreeBSD__) */
146 #if defined(__NetBSD__) || defined(__OpenBSD__)
151 #define URIOUNIT(n) (minor(n))
153 #define RIO_RW_TIMEOUT 4000 /* ms */
155 USB_DECLARE_DRIVER(urio);
159 USB_MATCH_START(urio, uaa);
160 usb_device_descriptor_t *dd;
162 DPRINTFN(10,("urio_match\n"));
166 dd = usbd_get_device_descriptor(uaa->device);
169 ((UGETW(dd->idVendor) == USB_VENDOR_DIAMOND &&
170 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND_RIO500USB) ||
171 (UGETW(dd->idVendor) == USB_VENDOR_DIAMOND2 &&
172 (UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO600USB ||
173 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO800USB))))
174 return UMATCH_VENDOR_PRODUCT;
181 USB_ATTACH_START(urio, sc, uaa);
182 usbd_device_handle udev;
183 usbd_interface_handle iface;
185 #if defined(__NetBSD__) || defined(__OpenBSD__)
189 char * ermsg = "<none>";
192 DPRINTFN(10,("urio_attach: sc=%p\n", sc));
194 sc->sc_udev = udev = uaa->device;
196 #if defined(__FreeBSD__)
197 if ((!uaa->device) || (!uaa->iface)) {
198 ermsg = "device or iface";
201 sc->sc_iface = iface = uaa->iface;
202 #elif defined(__NetBSD__) || defined(__OpenBSD__)
207 r = usbd_interface_count(udev, &niface);
212 r = usbd_device2interface_handle(udev, 0, &iface);
217 sc->sc_iface = iface;
221 sc->sc_pipeh_out = 0;
224 r = usbd_endpoint_count(iface, &epcount);
225 if (r != USBD_NORMAL_COMPLETION) {
230 sc->sc_epaddr[RIO_OUT] = 0xff;
231 sc->sc_epaddr[RIO_IN] = 0x00;
233 for (i = 0; i < epcount; i++) {
234 usb_endpoint_descriptor_t *edesc =
235 usbd_interface2endpoint_descriptor(iface, i);
239 ermsg = "interface endpoint";
243 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
245 sc->sc_epaddr[d] = edesc->bEndpointAddress;
247 if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
248 sc->sc_epaddr[RIO_IN] == 0x00) {
253 #if defined(__FreeBSD__)
254 /* XXX no error trapping, no storing of struct cdev **/
255 sc->sc_dev_t = make_dev(&urio_cdevsw, device_get_unit(self),
256 UID_ROOT, GID_OPERATOR,
257 0644, "urio%d", device_get_unit(self));
258 #elif defined(__NetBSD__) || defined(__OpenBSD__)
259 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
263 DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
268 printf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
274 urioopen(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
277 struct urio_softc * sc;
279 int unit = URIOUNIT(dev);
280 USB_GET_SC_OPEN(urio, unit, sc);
282 DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
288 if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
293 sc->sc_pipeh_out = 0;
294 if (usbd_open_pipe(sc->sc_iface,
295 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
296 != USBD_NORMAL_COMPLETION)
301 if (usbd_open_pipe(sc->sc_iface,
302 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
303 != USBD_NORMAL_COMPLETION)
305 usbd_close_pipe(sc->sc_pipeh_in);
307 sc->sc_pipeh_out = 0;
314 urioclose(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
317 struct urio_softc * sc;
319 int unit = URIOUNIT(dev);
320 USB_GET_SC(urio, unit, sc);
322 DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n", flag, mode, unit));
324 usbd_close_pipe(sc->sc_pipeh_in);
326 if (sc->sc_pipeh_out)
327 usbd_close_pipe(sc->sc_pipeh_out);
330 sc->sc_pipeh_out = 0;
337 urioread(struct cdev *dev, struct uio *uio, int flag)
340 struct urio_softc * sc;
341 usbd_xfer_handle reqh;
343 usbd_request_handle reqh;
344 usbd_private_handle r_priv;
346 usbd_status r_status;
348 int unit = URIOUNIT(dev);
350 char buf[URIO_BBSIZE];
354 USB_GET_SC(urio, unit, sc);
356 DPRINTFN(5, ("urioread: %d\n", unit));
362 reqh = usbd_alloc_xfer(sc->sc_udev);
364 reqh = usbd_alloc_request();
368 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
369 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
372 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
373 0, RIO_RW_TIMEOUT, 0);
375 r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
376 0, RIO_RW_TIMEOUT, 0);
377 if (r != USBD_NORMAL_COMPLETION) {
382 r = usbd_sync_transfer(reqh);
383 if (r != USBD_NORMAL_COMPLETION) {
384 DPRINTFN(1, ("urioread: error=%d\n", r));
385 usbd_clear_endpoint_stall(sc->sc_pipeh_in);
391 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
393 usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
396 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
397 error = uiomove(buf, tn, uio);
402 usbd_free_xfer(reqh);
404 usbd_free_request(reqh);
411 uriowrite(struct cdev *dev, struct uio *uio, int flag)
414 struct urio_softc * sc;
415 usbd_xfer_handle reqh;
417 usbd_request_handle reqh;
419 int unit = URIOUNIT(dev);
421 char buf[URIO_BBSIZE];
425 USB_GET_SC(urio, unit, sc);
427 DPRINTFN(5, ("uriowrite: %d\n", unit));
433 reqh = usbd_alloc_xfer(sc->sc_udev);
435 reqh = usbd_alloc_request();
439 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
440 error = uiomove(buf, n, uio);
443 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
445 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
446 0, RIO_RW_TIMEOUT, 0);
448 r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
449 0, RIO_RW_TIMEOUT, 0);
450 if (r != USBD_NORMAL_COMPLETION) {
455 r = usbd_sync_transfer(reqh);
456 if (r != USBD_NORMAL_COMPLETION) {
457 DPRINTFN(1, ("uriowrite: error=%d\n", r));
458 usbd_clear_endpoint_stall(sc->sc_pipeh_out);
463 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
468 usbd_free_xfer(reqh);
470 usbd_free_request(reqh);
478 urioioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
481 struct urio_softc * sc;
483 int unit = URIOUNIT(dev);
484 struct RioCommand *rio_cmd;
485 int requesttype, len;
488 usb_device_request_t req;
489 int req_flags = 0, req_actlen = 0;
494 USB_GET_SC(urio, unit, sc);
497 case RIO_RECV_COMMAND:
498 if (!(flag & FWRITE))
500 rio_cmd = (struct RioCommand *)addr;
503 len = rio_cmd->length;
505 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
506 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
507 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
510 case RIO_SEND_COMMAND:
511 if (!(flag & FWRITE))
513 rio_cmd = (struct RioCommand *)addr;
516 len = rio_cmd->length;
518 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
519 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
520 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
528 /* Send rio control message */
529 req.bmRequestType = requesttype;
530 req.bRequest = rio_cmd->request;
531 USETW(req.wValue, rio_cmd->value);
532 USETW(req.wIndex, rio_cmd->index);
533 USETW(req.wLength, len);
535 if (len < 0 || len > 32767)
538 iov.iov_base = (caddr_t)rio_cmd->buffer;
544 uio.uio_segflg = UIO_USERSPACE;
546 req.bmRequestType & UT_READ ?
547 UIO_READ : UIO_WRITE;
549 ptr = malloc(len, M_TEMP, M_WAITOK);
550 if (uio.uio_rw == UIO_WRITE) {
551 error = uiomove(ptr, len, &uio);
557 r = usbd_do_request_flags(sc->sc_udev, &req,
558 ptr, req_flags, &req_actlen,
559 USBD_DEFAULT_TIMEOUT);
560 if (r == USBD_NORMAL_COMPLETION) {
563 if (uio.uio_rw == UIO_READ) {
564 error = uiomove(ptr, len, &uio);
578 #if defined(__NetBSD__) || defined(__OpenBSD__)
580 urio_activate(device_t self, enum devact act)
582 struct urio_softc *sc = (struct urio_softc *)self;
589 case DVACT_DEACTIVATE:
598 USB_DETACH_START(urio, sc);
599 struct urio_endpoint *sce;
602 #if defined(__NetBSD__) || defined(__OpenBSD__)
605 DPRINTF(("urio_detach: sc=%p flags=%d\n", sc, flags));
606 #elif defined(__FreeBSD__)
607 DPRINTF(("urio_detach: sc=%p\n", sc));
611 /* Abort all pipes. Causes processes waiting for transfer to wake. */
613 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
614 for (dir = OUT; dir <= IN; dir++) {
615 sce = &sc->sc_endpoints[i][dir];
616 if (sce && sce->pipeh)
617 usbd_abort_pipe(sce->pipeh);
622 if (--sc->sc_refcnt >= 0) {
624 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
625 wakeup(&sc->sc_endpoints[i][IN]);
626 /* Wait for processes to go away. */
627 usb_detach_wait(USBDEV(sc->sc_dev));
632 usbd_abort_pipe(sc->sc_pipeh_in);
634 if (sc->sc_pipeh_out)
635 usbd_abort_pipe(sc->sc_pipeh_out);
638 if (--sc->sc_refcnt >= 0) {
639 /* Wait for processes to go away. */
640 usb_detach_wait(USBDEV(sc->sc_dev));
645 #if defined(__NetBSD__) || defined(__OpenBSD__)
646 /* locate the major number */
647 for (maj = 0; maj < nchrdev; maj++)
648 if (cdevsw[maj].d_open == urioopen)
651 /* Nuke the vnodes for any open instances (calls close). */
652 mn = self->dv_unit * USB_MAX_ENDPOINTS;
653 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
656 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
661 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
663 #if defined(__FreeBSD__)
665 urio_detach(device_t self)
667 struct urio_softc *sc = device_get_softc(self);
669 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
670 destroy_dev(sc->sc_dev_t);
671 /* XXX not implemented yet */
672 device_set_desc(self, NULL);
676 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);