Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / usbmisc / uscanner / uscanner.c
1 /*      $NetBSD: uscanner.c,v 1.26 2001/12/31 12:15:22 augustss Exp $   */
2 /*      $FreeBSD: src/sys/dev/usb/uscanner.c,v 1.2.2.12 2003/01/27 09:48:57 joe Exp $   */
3
4 /*
5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
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).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
28  *
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.
40  */
41
42
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>
51 #include <sys/bus.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/filio.h>
55 #endif
56 #include <sys/tty.h>
57 #include <sys/file.h>
58 #include <sys/select.h>
59 #include <sys/proc.h>
60 #include <sys/vnode.h>
61 #include <sys/poll.h>
62 #include <sys/conf.h>
63 #include <sys/sysctl.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68
69 #include <dev/usb/usbdevs.h>
70
71 #ifdef USB_DEBUG
72 #define DPRINTF(x)      if (uscannerdebug) logprintf x
73 #define DPRINTFN(n,x)   if (uscannerdebug>(n)) logprintf x
74 int     uscannerdebug = 0;
75 SYSCTL_NODE(_hw_usb, OID_AUTO, uscanner, CTLFLAG_RW, 0, "USB uscanner");
76 SYSCTL_INT(_hw_usb_uscanner, OID_AUTO, debug, CTLFLAG_RW,
77            &uscannerdebug, 0, "uscanner debug level");
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n,x)
81 #endif
82
83 struct uscan_info {
84         struct usb_devno devno;
85         u_int flags;
86 #define USC_KEEP_OPEN 1
87 };
88
89 /* Table of scanners that may work with this driver. */
90 static const struct uscan_info uscanner_devs[] = {
91   /* Acer Peripherals */
92  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_320U }, 0 },
93  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_640U }, 0 },
94  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_620U }, 0 },
95  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_C310U }, 0 },
96
97   /* AGFA */
98  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1236U }, 0 },
99  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U }, 0 },
100  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U2 }, 0 },
101  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANTOUCH }, 0 },
102  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE40 }, 0 },
103  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE50 }, 0 },
104  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE20 }, 0 },
105  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE25 }, 0 },
106  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE26 }, 0 },
107  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE52 }, 0 },
108
109   /* Avision */
110  {{ USB_VENDOR_AVISION, USB_PRODUCT_AVISION_1200U }, 0 },
111
112   /* Canon */
113  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N656U }, 0 },
114
115   /* Kye */
116  {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
117
118   /* HP */
119  {{ USB_VENDOR_HP, USB_PRODUCT_HP_2200C }, 0 },
120  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3300C }, 0 },
121  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3400CSE }, 0 },
122  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4100C }, 0 },
123  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4200C }, 0 },
124  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4300C }, 0 },
125  {{ USB_VENDOR_HP, USB_PRODUCT_HP_S20 }, 0 },
126  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5200C }, 0 },
127  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5300C }, 0 },
128  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5400C }, 0 },
129  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6200C }, 0 },
130  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6300C }, 0 },
131
132   /* Microtek */
133  {{ USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_336CX }, 0 },
134  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_X6U }, 0 },
135  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX }, 0 },
136  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX2 }, 0 },
137  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_C6 }, 0 },
138  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL }, 0 },
139  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL2 }, 0 },
140  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6UL }, 0 },
141
142   /* Mustek */
143  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CU }, 0 },
144  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200F }, 0 },
145  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600USB }, 0 },
146  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600CU }, 0 },
147  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USB }, 0 },
148  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200UB }, 0 },
149  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USBPLUS }, 0 },
150  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CUPLUS }, 0 },
151
152   /* National */
153  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW1200 }, 0 },
154  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW2400 }, 0 },
155
156   /* Primax */
157  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2X300 }, 0 },
158  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E300 }, 0 },
159  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2300 }, 0 },
160  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E3002 }, 0 },
161  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_9600 }, 0 },
162  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_600U }, 0 },
163  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_6200 }, 0 },
164  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_19200 }, 0 },
165  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_1200U }, 0 },
166  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G600 }, 0 },
167  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_636I }, 0 },
168  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2600 }, 0 },
169  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E600 }, 0 },
170
171   /* Epson */
172  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_636 }, 0 },
173  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_610 }, 0 },
174  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1200 }, 0 },
175  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1240 }, 0 },
176  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1600 }, 0 },
177  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1640 }, 0 },
178  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_640U }, 0 },
179  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1650 }, 0 },
180  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1660 }, 0 },
181  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1260 }, 0 },
182  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9700F }, USC_KEEP_OPEN },
183
184   /* UMAX */
185  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1220U }, 0 },
186  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1236U }, 0 },
187  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2000U }, 0 },
188  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2200U }, 0 },
189  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA3400 }, 0 },
190
191   /* Visioneer */
192  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_3000 }, 0 },
193  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_5300 }, 0 },
194  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_7600 }, 0 },
195  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6100 }, 0 },
196  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6200 }, 0 },
197  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8100 }, 0 },
198  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8600 }, 0 },
199
200   /* Ultima */
201  {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
202
203 };
204 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
205
206 #define USCANNER_BUFFERSIZE     1024
207
208 struct uscanner_softc {
209         USBBASEDEVICE           sc_dev;         /* base device */
210         usbd_device_handle      sc_udev;
211         usbd_interface_handle   sc_iface;
212
213         u_int                   sc_dev_flags;
214
215         usbd_pipe_handle        sc_bulkin_pipe;
216         int                     sc_bulkin;
217         usbd_xfer_handle        sc_bulkin_xfer;
218         void                    *sc_bulkin_buffer;
219         int                     sc_bulkin_bufferlen;
220         int                     sc_bulkin_datalen;
221
222         usbd_pipe_handle        sc_bulkout_pipe;
223         int                     sc_bulkout;
224         usbd_xfer_handle        sc_bulkout_xfer;
225         void                    *sc_bulkout_buffer;
226         int                     sc_bulkout_bufferlen;
227         int                     sc_bulkout_datalen;
228
229         u_char                  sc_state;
230 #define USCANNER_OPEN           0x01    /* opened */
231
232         int                     sc_refcnt;
233         u_char                  sc_dying;
234 };
235
236 #if defined(__NetBSD__) || defined(__OpenBSD__)
237 cdev_decl(uscanner);
238 #elif defined(__FreeBSD__)
239 d_open_t  uscanneropen;
240 d_close_t uscannerclose;
241 d_read_t  uscannerread;
242 d_write_t uscannerwrite;
243 d_ioctl_t uscannerioctl;
244 d_poll_t  uscannerpoll;
245
246 #define USCANNER_CDEV_MAJOR     156
247
248 Static struct cdevsw uscanner_cdevsw = {
249         /* open */      uscanneropen,
250         /* close */     uscannerclose,
251         /* read */      uscannerread,
252         /* write */     uscannerwrite,
253         /* ioctl */     uscannerioctl,
254         /* poll */      uscannerpoll,
255         /* mmap */      nommap,
256         /* strategy */  nostrategy,
257         /* name */      "uscanner",
258         /* maj */       USCANNER_CDEV_MAJOR,
259         /* dump */      nodump,
260         /* psize */     nopsize,
261         /* flags */     0,
262         /* bmaj */      -1
263 };
264 #endif
265
266 Static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
267 Static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
268 Static void uscanner_do_close(struct uscanner_softc *);
269
270 #define USCANNERUNIT(n) (minor(n))
271
272 USB_DECLARE_DRIVER(uscanner);
273
274 USB_MATCH(uscanner)
275 {
276         USB_MATCH_START(uscanner, uaa);
277
278         if (uaa->iface != NULL)
279                 return UMATCH_NONE;
280
281         return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
282                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
283 }
284
285 USB_ATTACH(uscanner)
286 {
287         USB_ATTACH_START(uscanner, sc, uaa);
288         usb_interface_descriptor_t *id = 0;
289         usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
290         char devinfo[1024];
291         int i;
292         usbd_status err;
293
294         usbd_devinfo(uaa->device, 0, devinfo);
295         USB_ATTACH_SETUP;
296         printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
297
298         sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
299
300         sc->sc_udev = uaa->device;
301
302         err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
303         if (err) {
304                 printf("%s: setting config no failed\n",
305                     USBDEVNAME(sc->sc_dev));
306                 USB_ATTACH_ERROR_RETURN;
307         }
308
309         /* XXX We only check the first interface */
310         err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
311         if (!err && sc->sc_iface)
312             id = usbd_get_interface_descriptor(sc->sc_iface);
313         if (err || id == 0) {
314                 printf("%s: could not get interface descriptor, err=%d,id=%p\n",
315                        USBDEVNAME(sc->sc_dev), err, id);
316                 USB_ATTACH_ERROR_RETURN;
317         }
318
319         /* Find the two first bulk endpoints */
320         for (i = 0 ; i < id->bNumEndpoints; i++) {
321                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
322                 if (ed == 0) {
323                         printf("%s: could not read endpoint descriptor\n",
324                                USBDEVNAME(sc->sc_dev));
325                         USB_ATTACH_ERROR_RETURN;
326                 }
327
328                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
329                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
330                         ed_bulkin = ed;
331                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
332                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
333                         ed_bulkout = ed;
334                 }
335
336                 if (ed_bulkin && ed_bulkout)    /* found all we need */
337                         break;
338         }
339
340         /* Verify that we goething sensible */
341         if (ed_bulkin == NULL || ed_bulkout == NULL) {
342                 printf("%s: bulk-in and/or bulk-out endpoint not found\n",
343                         USBDEVNAME(sc->sc_dev));
344                 USB_ATTACH_ERROR_RETURN;
345         }
346
347         sc->sc_bulkin = ed_bulkin->bEndpointAddress;
348         sc->sc_bulkout = ed_bulkout->bEndpointAddress;
349
350 #ifdef __FreeBSD__
351         /* the main device, ctrl endpoint */
352         make_dev(&uscanner_cdevsw, USBDEVUNIT(sc->sc_dev),
353                 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
354 #endif
355
356         USB_ATTACH_SUCCESS_RETURN;
357 }
358
359 int
360 uscanneropen(dev_t dev, int flag, int mode, usb_proc_ptr p)
361 {
362         struct uscanner_softc *sc;
363         int unit = USCANNERUNIT(dev);
364         usbd_status err;
365
366         USB_GET_SC_OPEN(uscanner, unit, sc);
367
368         DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n", 
369                      flag, mode, unit));
370
371         if (sc->sc_dying)
372                 return (ENXIO);
373
374         if (sc->sc_state & USCANNER_OPEN)
375                 return (EBUSY);
376
377         sc->sc_state |= USCANNER_OPEN;
378
379         sc->sc_bulkin_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
380         sc->sc_bulkout_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
381         /* No need to check buffers for NULL since we have WAITOK */
382
383         sc->sc_bulkin_bufferlen = USCANNER_BUFFERSIZE;
384         sc->sc_bulkout_bufferlen = USCANNER_BUFFERSIZE;
385
386         /* We have decided on which endpoints to use, now open the pipes */
387         if (sc->sc_bulkin_pipe == NULL) {
388                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
389                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
390                 if (err) {
391                         printf("%s: cannot open bulk-in pipe (addr %d)\n",
392                                USBDEVNAME(sc->sc_dev), sc->sc_bulkin);
393                         uscanner_do_close(sc);
394                         return (EIO);
395                 }
396         }
397         if (sc->sc_bulkout_pipe == NULL) {
398                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
399                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
400                 if (err) {
401                         printf("%s: cannot open bulk-out pipe (addr %d)\n",
402                                USBDEVNAME(sc->sc_dev), sc->sc_bulkout);
403                         uscanner_do_close(sc);
404                         return (EIO);
405                 }
406         }
407
408         sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
409         if (sc->sc_bulkin_xfer == NULL) {
410                 uscanner_do_close(sc);
411                 return (ENOMEM);
412         }
413         sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
414         if (sc->sc_bulkout_xfer == NULL) {
415                 uscanner_do_close(sc);
416                 return (ENOMEM);
417         }
418
419         return (0);     /* success */
420 }
421
422 int
423 uscannerclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
424 {
425         struct uscanner_softc *sc;
426
427         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
428
429         DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
430                      flag, mode, USCANNERUNIT(dev)));
431
432 #ifdef DIAGNOSTIC
433         if (!(sc->sc_state & USCANNER_OPEN)) {
434                 printf("uscannerclose: not open\n");
435                 return (EINVAL);
436         }
437 #endif
438
439         uscanner_do_close(sc);
440
441         return (0);
442 }
443
444 void
445 uscanner_do_close(struct uscanner_softc *sc)
446 {
447         if (sc->sc_bulkin_xfer) {
448                 usbd_free_xfer(sc->sc_bulkin_xfer);
449                 sc->sc_bulkin_xfer = NULL;
450         }
451         if (sc->sc_bulkout_xfer) {
452                 usbd_free_xfer(sc->sc_bulkout_xfer);
453                 sc->sc_bulkout_xfer = NULL;
454         }
455
456         if (!(sc->sc_dev_flags & USC_KEEP_OPEN)) {
457                 if (sc->sc_bulkin_pipe != NULL) {
458                         usbd_abort_pipe(sc->sc_bulkin_pipe);
459                         usbd_close_pipe(sc->sc_bulkin_pipe);
460                         sc->sc_bulkin_pipe = NULL;
461                 }
462                 if (sc->sc_bulkout_pipe != NULL) {
463                         usbd_abort_pipe(sc->sc_bulkout_pipe);
464                         usbd_close_pipe(sc->sc_bulkout_pipe);
465                         sc->sc_bulkout_pipe = NULL;
466                 }
467         }
468
469         if (sc->sc_bulkin_buffer) {
470                 free(sc->sc_bulkin_buffer, M_USBDEV);
471                 sc->sc_bulkin_buffer = NULL;
472         }
473         if (sc->sc_bulkout_buffer) {
474                 free(sc->sc_bulkout_buffer, M_USBDEV);
475                 sc->sc_bulkout_buffer = NULL;
476         }
477
478         sc->sc_state &= ~USCANNER_OPEN;
479 }
480
481 Static int
482 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
483 {
484         u_int32_t n, tn;
485         usbd_status err;
486         int error = 0;
487
488         DPRINTFN(5, ("%s: uscannerread\n", USBDEVNAME(sc->sc_dev)));
489
490         if (sc->sc_dying)
491                 return (EIO);
492
493         while ((n = min(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
494                 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
495                 tn = n;
496
497                 err = usbd_bulk_transfer(
498                         sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
499                         USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
500                         sc->sc_bulkin_buffer, &tn,
501                         "uscnrb");
502                 if (err) {
503                         if (err == USBD_INTERRUPTED)
504                                 error = EINTR;
505                         else if (err == USBD_TIMEOUT)
506                                 error = ETIMEDOUT;
507                         else
508                                 error = EIO;
509                         break;
510                 }
511                 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
512                 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
513                 if (error || tn < n)
514                         break;
515         }
516
517         return (error);
518 }
519
520 int
521 uscannerread(dev_t dev, struct uio *uio, int flag)
522 {
523         struct uscanner_softc *sc;
524         int error;
525
526         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
527
528         sc->sc_refcnt++;
529         error = uscanner_do_read(sc, uio, flag);
530         if (--sc->sc_refcnt < 0)
531                 usb_detach_wakeup(USBDEV(sc->sc_dev));
532
533         return (error);
534 }
535
536 Static int
537 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
538 {
539         u_int32_t n;
540         int error = 0;
541         usbd_status err;
542
543         DPRINTFN(5, ("%s: uscanner_do_write\n", USBDEVNAME(sc->sc_dev)));
544
545         if (sc->sc_dying)
546                 return (EIO);
547
548         while ((n = min(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
549                 error = uiomove(sc->sc_bulkout_buffer, n, uio);
550                 if (error)
551                         break;
552                 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
553                 err = usbd_bulk_transfer(
554                         sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
555                         0, USBD_NO_TIMEOUT,
556                         sc->sc_bulkout_buffer, &n,
557                         "uscnwb");
558                 if (err) {
559                         if (err == USBD_INTERRUPTED)
560                                 error = EINTR;
561                         else
562                                 error = EIO;
563                         break;
564                 }
565         }
566
567         return (error);
568 }
569
570 int
571 uscannerwrite(dev_t dev, struct uio *uio, int flag)
572 {
573         struct uscanner_softc *sc;
574         int error;
575
576         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
577
578         sc->sc_refcnt++;
579         error = uscanner_do_write(sc, uio, flag);
580         if (--sc->sc_refcnt < 0)
581                 usb_detach_wakeup(USBDEV(sc->sc_dev));
582         return (error);
583 }
584
585 #if defined(__NetBSD__) || defined(__OpenBSD__)
586 int
587 uscanner_activate(device_ptr_t self, enum devact act)
588 {
589         struct uscanner_softc *sc = (struct uscanner_softc *)self;
590
591         switch (act) {
592         case DVACT_ACTIVATE:
593                 return (EOPNOTSUPP);
594                 break;
595
596         case DVACT_DEACTIVATE:
597                 sc->sc_dying = 1;
598                 break;
599         }
600         return (0);
601 }
602 #endif
603
604 USB_DETACH(uscanner)
605 {
606         USB_DETACH_START(uscanner, sc);
607         int s;
608 #if defined(__NetBSD__) || defined(__OpenBSD__)
609         int maj, mn;
610 #elif defined(__FreeBSD__)
611         dev_t dev;
612         struct vnode *vp;
613 #endif
614
615 #if defined(__NetBSD__) || defined(__OpenBSD__)
616         DPRINTF(("uscanner_detach: sc=%p flags=%d\n", sc, flags));
617 #elif defined(__FreeBSD__)
618         DPRINTF(("uscanner_detach: sc=%p\n", sc));
619 #endif
620
621         sc->sc_dying = 1;
622         sc->sc_dev_flags = 0;   /* make close really close device */
623
624         /* Abort all pipes.  Causes processes waiting for transfer to wake. */
625         if (sc->sc_bulkin_pipe != NULL)
626                 usbd_abort_pipe(sc->sc_bulkin_pipe);
627         if (sc->sc_bulkout_pipe != NULL)
628                 usbd_abort_pipe(sc->sc_bulkout_pipe);
629
630         s = splusb();
631         if (--sc->sc_refcnt >= 0) {
632                 /* Wait for processes to go away. */
633                 usb_detach_wait(USBDEV(sc->sc_dev));
634         }
635         splx(s);
636
637 #if defined(__NetBSD__) || defined(__OpenBSD__)
638         /* locate the major number */
639         for (maj = 0; maj < nchrdev; maj++)
640                 if (cdevsw[maj].d_open == uscanneropen)
641                         break;
642
643         /* Nuke the vnodes for any open instances (calls close). */
644         mn = self->dv_unit * USB_MAX_ENDPOINTS;
645         vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
646 #elif defined(__FreeBSD__)
647         /* destroy the device for the control endpoint */
648         dev = makedev(USCANNER_CDEV_MAJOR, USBDEVUNIT(sc->sc_dev));
649         vp = SLIST_FIRST(&dev->si_hlist);
650         if (vp)
651                 VOP_REVOKE(vp, REVOKEALL);
652         destroy_dev(dev);
653 #endif
654
655         return (0);
656 }
657
658 int
659 uscannerpoll(dev_t dev, int events, usb_proc_ptr p)
660 {
661         struct uscanner_softc *sc;
662         int revents = 0;
663
664         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
665
666         if (sc->sc_dying)
667                 return (EIO);
668
669         /* 
670          * We have no easy way of determining if a read will
671          * yield any data or a write will happen.
672          * Pretend they will.
673          */
674         revents |= events & 
675                    (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
676
677         return (revents);
678 }
679
680 int
681 uscannerioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
682 {
683         return (EINVAL);
684 }
685
686 #if defined(__FreeBSD__)
687 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);
688 #endif