1 /* $NetBSD: uscanner.c,v 1.30 2002/07/11 21:14:36 augustss Exp$ */
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology
11 * and Nick Hibma (n_hibma@qubesoft.com).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #if defined(__NetBSD__) || defined(__OpenBSD__)
48 #include <sys/device.h>
49 #elif defined(__FreeBSD__)
50 #include <sys/module.h>
53 #include <sys/fcntl.h>
54 #include <sys/filio.h>
58 #if __FreeBSD_version >= 500014
59 #include <sys/selinfo.h>
61 #include <sys/select.h>
64 #include <sys/vnode.h>
67 #include <sys/sysctl.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
73 #include <dev/usb/usbdevs.h>
76 #define DPRINTF(x) if (uscannerdebug) logprintf x
77 #define DPRINTFN(n,x) if (uscannerdebug>(n)) logprintf x
78 int uscannerdebug = 0;
79 SYSCTL_NODE(_hw_usb, OID_AUTO, uscanner, CTLFLAG_RW, 0, "USB uscanner");
80 SYSCTL_INT(_hw_usb_uscanner, OID_AUTO, debug, CTLFLAG_RW,
81 &uscannerdebug, 0, "uscanner debug level");
88 struct usb_devno devno;
90 #define USC_KEEP_OPEN 1
93 /* Table of scanners that may work with this driver. */
94 static const struct uscan_info uscanner_devs[] = {
95 /* Acer Peripherals */
96 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_320U }, 0 },
97 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_640U }, 0 },
98 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_620U }, 0 },
99 {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_C310U }, 0 },
102 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1236U }, 0 },
103 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U }, 0 },
104 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U2 }, 0 },
105 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANTOUCH }, 0 },
106 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE40 }, 0 },
107 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE50 }, 0 },
108 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE20 }, 0 },
109 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE25 }, 0 },
110 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE26 }, 0 },
111 {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE52 }, 0 },
114 {{ USB_VENDOR_AVISION, USB_PRODUCT_AVISION_1200U }, 0 },
117 {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N656U }, 0 },
120 {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
123 {{ USB_VENDOR_HP, USB_PRODUCT_HP_2200C }, 0 },
124 {{ USB_VENDOR_HP, USB_PRODUCT_HP_3300C }, 0 },
125 {{ USB_VENDOR_HP, USB_PRODUCT_HP_3400CSE }, 0 },
126 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4100C }, 0 },
127 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4200C }, 0 },
128 {{ USB_VENDOR_HP, USB_PRODUCT_HP_4300C }, 0 },
129 {{ USB_VENDOR_HP, USB_PRODUCT_HP_S20 }, 0 },
130 {{ USB_VENDOR_HP, USB_PRODUCT_HP_5200C }, 0 },
131 {{ USB_VENDOR_HP, USB_PRODUCT_HP_5300C }, 0 },
132 {{ USB_VENDOR_HP, USB_PRODUCT_HP_5400C }, 0 },
133 {{ USB_VENDOR_HP, USB_PRODUCT_HP_6200C }, 0 },
134 {{ USB_VENDOR_HP, USB_PRODUCT_HP_6300C }, 0 },
137 {{ USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_336CX }, 0 },
138 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_X6U }, 0 },
139 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX }, 0 },
140 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX2 }, 0 },
141 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_C6 }, 0 },
142 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL }, 0 },
143 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL2 }, 0 },
144 {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6UL }, 0 },
147 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CU }, 0 },
148 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200F }, 0 },
149 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600USB }, 0 },
150 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600CU }, 0 },
151 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USB }, 0 },
152 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200UB }, 0 },
153 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USBPLUS }, 0 },
154 {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CUPLUS }, 0 },
157 {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW1200 }, 0 },
158 {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW2400 }, 0 },
161 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2X300 }, 0 },
162 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E300 }, 0 },
163 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2300 }, 0 },
164 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E3002 }, 0 },
165 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_9600 }, 0 },
166 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_600U }, 0 },
167 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_6200 }, 0 },
168 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_19200 }, 0 },
169 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_1200U }, 0 },
170 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G600 }, 0 },
171 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_636I }, 0 },
172 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2600 }, 0 },
173 {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E600 }, 0 },
176 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_636 }, 0 },
177 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_610 }, 0 },
178 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1200 }, 0 },
179 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1240 }, 0 },
180 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1600 }, 0 },
181 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1640 }, 0 },
182 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_640U }, 0 },
183 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1650 }, 0 },
184 {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9700F }, USC_KEEP_OPEN },
187 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1220U }, 0 },
188 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1236U }, 0 },
189 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2000U }, 0 },
190 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2200U }, 0 },
191 {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA3400 }, 0 },
194 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_3000 }, 0 },
195 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_5300 }, 0 },
196 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_7600 }, 0 },
197 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6100 }, 0 },
198 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6200 }, 0 },
199 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8100 }, 0 },
200 {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8600 }, 0 },
203 {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
206 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
208 #define USCANNER_BUFFERSIZE 1024
210 struct uscanner_softc {
211 USBBASEDEVICE sc_dev; /* base device */
212 usbd_device_handle sc_udev;
213 usbd_interface_handle sc_iface;
217 usbd_pipe_handle sc_bulkin_pipe;
219 usbd_xfer_handle sc_bulkin_xfer;
220 void *sc_bulkin_buffer;
221 int sc_bulkin_bufferlen;
222 int sc_bulkin_datalen;
224 usbd_pipe_handle sc_bulkout_pipe;
226 usbd_xfer_handle sc_bulkout_xfer;
227 void *sc_bulkout_buffer;
228 int sc_bulkout_bufferlen;
229 int sc_bulkout_datalen;
232 #define USCANNER_OPEN 0x01 /* opened */
238 #if defined(__NetBSD__) || defined(__OpenBSD__)
240 #elif defined(__FreeBSD__)
241 d_open_t uscanneropen;
242 d_close_t uscannerclose;
243 d_read_t uscannerread;
244 d_write_t uscannerwrite;
245 d_poll_t uscannerpoll;
247 #define USCANNER_CDEV_MAJOR 156
249 Static struct cdevsw uscanner_cdevsw = {
250 /* open */ uscanneropen,
251 /* close */ uscannerclose,
252 /* read */ uscannerread,
253 /* write */ uscannerwrite,
255 /* poll */ uscannerpoll,
257 /* strategy */ nostrategy,
258 /* name */ "uscanner",
259 /* maj */ USCANNER_CDEV_MAJOR,
263 #if __FreeBSD_version < 500014
269 Static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
270 Static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
271 Static void uscanner_do_close(struct uscanner_softc *);
273 #define USCANNERUNIT(n) (minor(n))
275 USB_DECLARE_DRIVER(uscanner);
279 USB_MATCH_START(uscanner, uaa);
281 if (uaa->iface != NULL)
284 return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
285 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
290 USB_ATTACH_START(uscanner, sc, uaa);
291 usb_interface_descriptor_t *id = 0;
292 usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
297 usbd_devinfo(uaa->device, 0, devinfo);
299 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
301 sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
303 sc->sc_udev = uaa->device;
305 err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
307 printf("%s: setting config no failed\n",
308 USBDEVNAME(sc->sc_dev));
309 USB_ATTACH_ERROR_RETURN;
312 /* XXX We only check the first interface */
313 err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
314 if (!err && sc->sc_iface)
315 id = usbd_get_interface_descriptor(sc->sc_iface);
316 if (err || id == 0) {
317 printf("%s: could not get interface descriptor, err=%d,id=%p\n",
318 USBDEVNAME(sc->sc_dev), err, id);
319 USB_ATTACH_ERROR_RETURN;
322 /* Find the two first bulk endpoints */
323 for (i = 0 ; i < id->bNumEndpoints; i++) {
324 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
326 printf("%s: could not read endpoint descriptor\n",
327 USBDEVNAME(sc->sc_dev));
328 USB_ATTACH_ERROR_RETURN;
331 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
332 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
334 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
335 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
339 if (ed_bulkin && ed_bulkout) /* found all we need */
343 /* Verify that we goething sensible */
344 if (ed_bulkin == NULL || ed_bulkout == NULL) {
345 printf("%s: bulk-in and/or bulk-out endpoint not found\n",
346 USBDEVNAME(sc->sc_dev));
347 USB_ATTACH_ERROR_RETURN;
350 sc->sc_bulkin = ed_bulkin->bEndpointAddress;
351 sc->sc_bulkout = ed_bulkout->bEndpointAddress;
354 /* the main device, ctrl endpoint */
355 make_dev(&uscanner_cdevsw, USBDEVUNIT(sc->sc_dev),
356 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
359 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
362 USB_ATTACH_SUCCESS_RETURN;
366 uscanneropen(dev_t dev, int flag, int mode, usb_proc_ptr p)
368 struct uscanner_softc *sc;
369 int unit = USCANNERUNIT(dev);
372 USB_GET_SC_OPEN(uscanner, unit, sc);
374 DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
380 if (sc->sc_state & USCANNER_OPEN)
383 sc->sc_state |= USCANNER_OPEN;
385 sc->sc_bulkin_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
386 sc->sc_bulkout_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
387 /* No need to check buffers for NULL since we have WAITOK */
389 sc->sc_bulkin_bufferlen = USCANNER_BUFFERSIZE;
390 sc->sc_bulkout_bufferlen = USCANNER_BUFFERSIZE;
392 /* We have decided on which endpoints to use, now open the pipes */
393 if (sc->sc_bulkin_pipe == NULL) {
394 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
395 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
397 printf("%s: cannot open bulk-in pipe (addr %d)\n",
398 USBDEVNAME(sc->sc_dev), sc->sc_bulkin);
399 uscanner_do_close(sc);
403 if (sc->sc_bulkout_pipe == NULL) {
404 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
405 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
407 printf("%s: cannot open bulk-out pipe (addr %d)\n",
408 USBDEVNAME(sc->sc_dev), sc->sc_bulkout);
409 uscanner_do_close(sc);
414 sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
415 if (sc->sc_bulkin_xfer == NULL) {
416 uscanner_do_close(sc);
419 sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
420 if (sc->sc_bulkout_xfer == NULL) {
421 uscanner_do_close(sc);
425 return (0); /* success */
429 uscannerclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
431 struct uscanner_softc *sc;
433 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
435 DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
436 flag, mode, USCANNERUNIT(dev)));
439 if (!(sc->sc_state & USCANNER_OPEN)) {
440 printf("uscannerclose: not open\n");
445 uscanner_do_close(sc);
451 uscanner_do_close(struct uscanner_softc *sc)
453 if (sc->sc_bulkin_xfer) {
454 usbd_free_xfer(sc->sc_bulkin_xfer);
455 sc->sc_bulkin_xfer = NULL;
457 if (sc->sc_bulkout_xfer) {
458 usbd_free_xfer(sc->sc_bulkout_xfer);
459 sc->sc_bulkout_xfer = NULL;
462 if (!(sc->sc_dev_flags & USC_KEEP_OPEN)) {
463 if (sc->sc_bulkin_pipe != NULL) {
464 usbd_abort_pipe(sc->sc_bulkin_pipe);
465 usbd_close_pipe(sc->sc_bulkin_pipe);
466 sc->sc_bulkin_pipe = NULL;
468 if (sc->sc_bulkout_pipe != NULL) {
469 usbd_abort_pipe(sc->sc_bulkout_pipe);
470 usbd_close_pipe(sc->sc_bulkout_pipe);
471 sc->sc_bulkout_pipe = NULL;
475 if (sc->sc_bulkin_buffer) {
476 free(sc->sc_bulkin_buffer, M_USBDEV);
477 sc->sc_bulkin_buffer = NULL;
479 if (sc->sc_bulkout_buffer) {
480 free(sc->sc_bulkout_buffer, M_USBDEV);
481 sc->sc_bulkout_buffer = NULL;
484 sc->sc_state &= ~USCANNER_OPEN;
488 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
494 DPRINTFN(5, ("%s: uscannerread\n", USBDEVNAME(sc->sc_dev)));
499 while ((n = min(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
500 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
503 err = usbd_bulk_transfer(
504 sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
505 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
506 sc->sc_bulkin_buffer, &tn,
509 if (err == USBD_INTERRUPTED)
511 else if (err == USBD_TIMEOUT)
517 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
518 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
527 uscannerread(dev_t dev, struct uio *uio, int flag)
529 struct uscanner_softc *sc;
532 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
535 error = uscanner_do_read(sc, uio, flag);
536 if (--sc->sc_refcnt < 0)
537 usb_detach_wakeup(USBDEV(sc->sc_dev));
543 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
549 DPRINTFN(5, ("%s: uscanner_do_write\n", USBDEVNAME(sc->sc_dev)));
554 while ((n = min(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
555 error = uiomove(sc->sc_bulkout_buffer, n, uio);
558 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
559 err = usbd_bulk_transfer(
560 sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
562 sc->sc_bulkout_buffer, &n,
565 if (err == USBD_INTERRUPTED)
577 uscannerwrite(dev_t dev, struct uio *uio, int flag)
579 struct uscanner_softc *sc;
582 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
585 error = uscanner_do_write(sc, uio, flag);
586 if (--sc->sc_refcnt < 0)
587 usb_detach_wakeup(USBDEV(sc->sc_dev));
591 #if defined(__NetBSD__) || defined(__OpenBSD__)
593 uscanner_activate(device_ptr_t self, enum devact act)
595 struct uscanner_softc *sc = (struct uscanner_softc *)self;
602 case DVACT_DEACTIVATE:
612 USB_DETACH_START(uscanner, sc);
614 #if defined(__NetBSD__) || defined(__OpenBSD__)
616 #elif defined(__FreeBSD__)
621 #if defined(__NetBSD__) || defined(__OpenBSD__)
622 DPRINTF(("uscanner_detach: sc=%p flags=%d\n", sc, flags));
623 #elif defined(__FreeBSD__)
624 DPRINTF(("uscanner_detach: sc=%p\n", sc));
628 sc->sc_dev_flags = 0; /* make close really close device */
630 /* Abort all pipes. Causes processes waiting for transfer to wake. */
631 if (sc->sc_bulkin_pipe != NULL)
632 usbd_abort_pipe(sc->sc_bulkin_pipe);
633 if (sc->sc_bulkout_pipe != NULL)
634 usbd_abort_pipe(sc->sc_bulkout_pipe);
637 if (--sc->sc_refcnt >= 0) {
638 /* Wait for processes to go away. */
639 usb_detach_wait(USBDEV(sc->sc_dev));
643 #if defined(__NetBSD__) || defined(__OpenBSD__)
644 /* locate the major number */
645 for (maj = 0; maj < nchrdev; maj++)
646 if (cdevsw[maj].d_open == uscanneropen)
649 /* Nuke the vnodes for any open instances (calls close). */
650 mn = self->dv_unit * USB_MAX_ENDPOINTS;
651 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
652 #elif defined(__FreeBSD__)
653 /* destroy the device for the control endpoint */
654 dev = makedev(USCANNER_CDEV_MAJOR, USBDEVUNIT(sc->sc_dev));
655 vp = SLIST_FIRST(&dev->si_hlist);
657 VOP_REVOKE(vp, REVOKEALL);
661 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
668 uscannerpoll(dev_t dev, int events, usb_proc_ptr p)
670 struct uscanner_softc *sc;
673 USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
679 * We have no easy way of determining if a read will
680 * yield any data or a write will happen.
684 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
689 #if defined(__FreeBSD__)
690 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);