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 #include <sys/module.h>
53 #include <sys/ioccom.h>
54 #include <sys/fcntl.h>
55 #include <sys/filio.h>
60 #include <sys/selinfo.h>
62 #include <sys/sysctl.h>
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
70 #include <dev/usb/rio500_usb.h>
73 #define DPRINTF(x) if (uriodebug) printf x
74 #define DPRINTFN(n,x) if (uriodebug>(n)) printf x
76 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
77 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
78 &uriodebug, 0, "urio debug level");
84 /* difference of usbd interface */
98 static struct cdevsw urio_cdevsw = {
99 .d_version = D_VERSION,
100 .d_flags = D_NEEDGIANT,
102 .d_close = urioclose,
104 .d_write = uriowrite,
105 .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;
125 struct cdev *sc_dev_t;
129 #define URIOUNIT(n) (minor(n))
131 #define RIO_RW_TIMEOUT 4000 /* ms */
133 static device_probe_t urio_match;
134 static device_attach_t urio_attach;
135 static device_detach_t urio_detach;
137 static device_method_t urio_methods[] = {
138 /* Device interface */
139 DEVMETHOD(device_probe, urio_match),
140 DEVMETHOD(device_attach, urio_attach),
141 DEVMETHOD(device_detach, urio_detach),
146 static driver_t urio_driver = {
149 sizeof(struct urio_softc)
152 static devclass_t urio_devclass;
155 urio_match(device_t self)
157 struct usb_attach_arg *uaa = device_get_ivars(self);
158 usb_device_descriptor_t *dd;
160 DPRINTFN(10,("urio_match\n"));
164 dd = usbd_get_device_descriptor(uaa->device);
167 ((UGETW(dd->idVendor) == USB_VENDOR_DIAMOND &&
168 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND_RIO500USB) ||
169 (UGETW(dd->idVendor) == USB_VENDOR_DIAMOND2 &&
170 (UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO600USB ||
171 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO800USB))))
172 return UMATCH_VENDOR_PRODUCT;
178 urio_attach(device_t self)
180 struct urio_softc *sc = device_get_softc(self);
181 struct usb_attach_arg *uaa = device_get_ivars(self);
182 usbd_device_handle udev;
183 usbd_interface_handle iface;
186 char * ermsg = "<none>";
189 DPRINTFN(10,("urio_attach: sc=%p\n", sc));
191 sc->sc_udev = udev = uaa->device;
193 if ((!uaa->device) || (!uaa->iface)) {
194 ermsg = "device or iface";
197 sc->sc_iface = iface = uaa->iface;
200 sc->sc_pipeh_out = 0;
203 r = usbd_endpoint_count(iface, &epcount);
204 if (r != USBD_NORMAL_COMPLETION) {
209 sc->sc_epaddr[RIO_OUT] = 0xff;
210 sc->sc_epaddr[RIO_IN] = 0x00;
212 for (i = 0; i < epcount; i++) {
213 usb_endpoint_descriptor_t *edesc =
214 usbd_interface2endpoint_descriptor(iface, i);
218 ermsg = "interface endpoint";
222 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
224 sc->sc_epaddr[d] = edesc->bEndpointAddress;
226 if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
227 sc->sc_epaddr[RIO_IN] == 0x00) {
232 sc->sc_dev_t = make_dev(&urio_cdevsw, device_get_unit(self),
233 UID_ROOT, GID_OPERATOR,
234 0644, "urio%d", device_get_unit(self));
235 DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
240 printf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
246 urioopen(struct cdev *dev, int flag, int mode, struct thread *p)
248 struct urio_softc * sc;
249 int unit = URIOUNIT(dev);
250 sc = devclass_get_softc(urio_devclass, unit);
254 DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
260 if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
265 sc->sc_pipeh_out = 0;
266 if (usbd_open_pipe(sc->sc_iface,
267 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
268 != USBD_NORMAL_COMPLETION)
273 if (usbd_open_pipe(sc->sc_iface,
274 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
275 != USBD_NORMAL_COMPLETION)
277 usbd_close_pipe(sc->sc_pipeh_in);
279 sc->sc_pipeh_out = 0;
286 urioclose(struct cdev *dev, int flag, int mode, struct thread *p)
288 struct urio_softc * sc;
289 int unit = URIOUNIT(dev);
290 sc = devclass_get_softc(urio_devclass, unit);
292 DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n", flag, mode, unit));
294 usbd_close_pipe(sc->sc_pipeh_in);
296 if (sc->sc_pipeh_out)
297 usbd_close_pipe(sc->sc_pipeh_out);
300 sc->sc_pipeh_out = 0;
307 urioread(struct cdev *dev, struct uio *uio, int flag)
309 struct urio_softc * sc;
310 usbd_xfer_handle reqh;
311 int unit = URIOUNIT(dev);
313 char buf[URIO_BBSIZE];
317 sc = devclass_get_softc(urio_devclass, unit);
319 DPRINTFN(5, ("urioread: %d\n", unit));
324 reqh = usbd_alloc_xfer(sc->sc_udev);
327 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
328 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
330 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
331 0, RIO_RW_TIMEOUT, 0);
332 r = usbd_sync_transfer(reqh);
333 if (r != USBD_NORMAL_COMPLETION) {
334 DPRINTFN(1, ("urioread: error=%d\n", r));
335 usbd_clear_endpoint_stall(sc->sc_pipeh_in);
340 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
342 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
343 error = uiomove(buf, tn, uio);
347 usbd_free_xfer(reqh);
352 uriowrite(struct cdev *dev, struct uio *uio, int flag)
354 struct urio_softc * sc;
355 usbd_xfer_handle reqh;
356 int unit = URIOUNIT(dev);
358 char buf[URIO_BBSIZE];
362 sc = devclass_get_softc(urio_devclass, unit);
363 DPRINTFN(5, ("uriowrite: %d\n", unit));
368 reqh = usbd_alloc_xfer(sc->sc_udev);
371 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
372 error = uiomove(buf, n, uio);
375 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
376 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
377 0, RIO_RW_TIMEOUT, 0);
378 r = usbd_sync_transfer(reqh);
379 if (r != USBD_NORMAL_COMPLETION) {
380 DPRINTFN(1, ("uriowrite: error=%d\n", r));
381 usbd_clear_endpoint_stall(sc->sc_pipeh_out);
385 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
388 usbd_free_xfer(reqh);
394 urioioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *p)
396 struct urio_softc * sc;
397 int unit = URIOUNIT(dev);
398 struct RioCommand *rio_cmd;
399 int requesttype, len;
402 usb_device_request_t req;
403 int req_flags = 0, req_actlen = 0;
408 sc = devclass_get_softc(urio_devclass, unit);
410 case RIO_RECV_COMMAND:
411 if (!(flag & FWRITE))
413 rio_cmd = (struct RioCommand *)addr;
416 len = rio_cmd->length;
418 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
419 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
420 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
423 case RIO_SEND_COMMAND:
424 if (!(flag & FWRITE))
426 rio_cmd = (struct RioCommand *)addr;
429 len = rio_cmd->length;
431 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
432 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
433 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
441 /* Send rio control message */
442 req.bmRequestType = requesttype;
443 req.bRequest = rio_cmd->request;
444 USETW(req.wValue, rio_cmd->value);
445 USETW(req.wIndex, rio_cmd->index);
446 USETW(req.wLength, len);
448 if (len < 0 || len > 32767)
451 iov.iov_base = (caddr_t)rio_cmd->buffer;
457 uio.uio_segflg = UIO_USERSPACE;
459 req.bmRequestType & UT_READ ?
460 UIO_READ : UIO_WRITE;
462 ptr = malloc(len, M_TEMP, M_WAITOK);
463 if (uio.uio_rw == UIO_WRITE) {
464 error = uiomove(ptr, len, &uio);
470 r = usbd_do_request_flags(sc->sc_udev, &req,
471 ptr, req_flags, &req_actlen,
472 USBD_DEFAULT_TIMEOUT);
473 if (r == USBD_NORMAL_COMPLETION) {
476 if (uio.uio_rw == UIO_READ) {
477 error = uiomove(ptr, len, &uio);
491 urio_detach(device_t self)
493 struct urio_softc *sc = device_get_softc(self);
496 DPRINTF(("urio_detach: sc=%p\n", sc));
499 usbd_abort_pipe(sc->sc_pipeh_in);
501 if (sc->sc_pipeh_out)
502 usbd_abort_pipe(sc->sc_pipeh_out);
505 if (--sc->sc_refcnt >= 0) {
506 /* Wait for processes to go away. */
507 usb_detach_wait(sc->sc_dev);
511 destroy_dev(sc->sc_dev_t);
512 /* XXX not implemented yet */
513 device_set_desc(self, NULL);
517 MODULE_DEPEND(uscanner, usb, 1, 1, 1);
518 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);