]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/uscanner.c
This commit was generated by cvs2svn to compensate for changes in r102786,
[FreeBSD/FreeBSD.git] / sys / dev / usb / uscanner.c
1 /*      $NetBSD: uscanner.c,v 1.30 2002/07/11 21:14:36 augustss Exp$    */
2 /*      $FreeBSD$       */
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 #if __FreeBSD_version >= 500014
59 #include <sys/selinfo.h>
60 #else
61 #include <sys/select.h>
62 #endif
63 #include <sys/proc.h>
64 #include <sys/vnode.h>
65 #include <sys/poll.h>
66 #include <sys/conf.h>
67 #include <sys/sysctl.h>
68
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
72
73 #include <dev/usb/usbdevs.h>
74
75 #ifdef USB_DEBUG
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");
82 #else
83 #define DPRINTF(x)
84 #define DPRINTFN(n,x)
85 #endif
86
87 struct uscan_info {
88         struct usb_devno devno;
89         u_int flags;
90 #define USC_KEEP_OPEN 1
91 };
92
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 },
100
101   /* AGFA */
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 },
112
113   /* Avision */
114  {{ USB_VENDOR_AVISION, USB_PRODUCT_AVISION_1200U }, 0 },
115
116   /* Canon */
117  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N656U }, 0 },
118
119   /* Kye */
120  {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
121
122   /* HP */
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 },
135
136   /* Microtek */
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 },
145
146   /* Mustek */
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 },
155
156   /* National */
157  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW1200 }, 0 },
158  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW2400 }, 0 },
159
160   /* Primax */
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 },
174
175   /* Epson */
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 },
185
186   /* UMAX */
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 },
192
193   /* Visioneer */
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 },
201
202   /* Ultima */
203  {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
204
205 };
206 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
207
208 #define USCANNER_BUFFERSIZE     1024
209
210 struct uscanner_softc {
211         USBBASEDEVICE           sc_dev;         /* base device */
212         usbd_device_handle      sc_udev;
213         usbd_interface_handle   sc_iface;
214
215         u_int                   sc_dev_flags;
216
217         usbd_pipe_handle        sc_bulkin_pipe;
218         int                     sc_bulkin;
219         usbd_xfer_handle        sc_bulkin_xfer;
220         void                    *sc_bulkin_buffer;
221         int                     sc_bulkin_bufferlen;
222         int                     sc_bulkin_datalen;
223
224         usbd_pipe_handle        sc_bulkout_pipe;
225         int                     sc_bulkout;
226         usbd_xfer_handle        sc_bulkout_xfer;
227         void                    *sc_bulkout_buffer;
228         int                     sc_bulkout_bufferlen;
229         int                     sc_bulkout_datalen;
230
231         u_char                  sc_state;
232 #define USCANNER_OPEN           0x01    /* opened */
233
234         int                     sc_refcnt;
235         u_char                  sc_dying;
236 };
237
238 #if defined(__NetBSD__) || defined(__OpenBSD__)
239 cdev_decl(uscanner);
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;
246
247 #define USCANNER_CDEV_MAJOR     156
248
249 Static struct cdevsw uscanner_cdevsw = {
250         /* open */      uscanneropen,
251         /* close */     uscannerclose,
252         /* read */      uscannerread,
253         /* write */     uscannerwrite,
254         /* ioctl */     noioctl,
255         /* poll */      uscannerpoll,
256         /* mmap */      nommap,
257         /* strategy */  nostrategy,
258         /* name */      "uscanner",
259         /* maj */       USCANNER_CDEV_MAJOR,
260         /* dump */      nodump,
261         /* psize */     nopsize,
262         /* flags */     0,
263 #if __FreeBSD_version < 500014
264         /* bmaj */      -1
265 #endif
266 };
267 #endif
268
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 *);
272
273 #define USCANNERUNIT(n) (minor(n))
274
275 USB_DECLARE_DRIVER(uscanner);
276
277 USB_MATCH(uscanner)
278 {
279         USB_MATCH_START(uscanner, uaa);
280
281         if (uaa->iface != NULL)
282                 return UMATCH_NONE;
283
284         return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
285                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
286 }
287
288 USB_ATTACH(uscanner)
289 {
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;
293         char devinfo[1024];
294         int i;
295         usbd_status err;
296
297         usbd_devinfo(uaa->device, 0, devinfo);
298         USB_ATTACH_SETUP;
299         printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
300
301         sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
302
303         sc->sc_udev = uaa->device;
304
305         err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
306         if (err) {
307                 printf("%s: setting config no failed\n",
308                     USBDEVNAME(sc->sc_dev));
309                 USB_ATTACH_ERROR_RETURN;
310         }
311
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;
320         }
321
322         /* Find the two first bulk endpoints */
323         for (i = 0 ; i < id->bNumEndpoints; i++) {
324                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
325                 if (ed == 0) {
326                         printf("%s: could not read endpoint descriptor\n",
327                                USBDEVNAME(sc->sc_dev));
328                         USB_ATTACH_ERROR_RETURN;
329                 }
330
331                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
332                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
333                         ed_bulkin = ed;
334                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
335                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
336                         ed_bulkout = ed;
337                 }
338
339                 if (ed_bulkin && ed_bulkout)    /* found all we need */
340                         break;
341         }
342
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;
348         }
349
350         sc->sc_bulkin = ed_bulkin->bEndpointAddress;
351         sc->sc_bulkout = ed_bulkout->bEndpointAddress;
352
353 #ifdef __FreeBSD__
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));
357 #endif
358
359         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
360                            USBDEV(sc->sc_dev));
361
362         USB_ATTACH_SUCCESS_RETURN;
363 }
364
365 int
366 uscanneropen(dev_t dev, int flag, int mode, usb_proc_ptr p)
367 {
368         struct uscanner_softc *sc;
369         int unit = USCANNERUNIT(dev);
370         usbd_status err;
371
372         USB_GET_SC_OPEN(uscanner, unit, sc);
373
374         DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
375                      flag, mode, unit));
376
377         if (sc->sc_dying)
378                 return (ENXIO);
379
380         if (sc->sc_state & USCANNER_OPEN)
381                 return (EBUSY);
382
383         sc->sc_state |= USCANNER_OPEN;
384
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 */
388
389         sc->sc_bulkin_bufferlen = USCANNER_BUFFERSIZE;
390         sc->sc_bulkout_bufferlen = USCANNER_BUFFERSIZE;
391
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);
396                 if (err) {
397                         printf("%s: cannot open bulk-in pipe (addr %d)\n",
398                                USBDEVNAME(sc->sc_dev), sc->sc_bulkin);
399                         uscanner_do_close(sc);
400                         return (EIO);
401                 }
402         }
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);
406                 if (err) {
407                         printf("%s: cannot open bulk-out pipe (addr %d)\n",
408                                USBDEVNAME(sc->sc_dev), sc->sc_bulkout);
409                         uscanner_do_close(sc);
410                         return (EIO);
411                 }
412         }
413
414         sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
415         if (sc->sc_bulkin_xfer == NULL) {
416                 uscanner_do_close(sc);
417                 return (ENOMEM);
418         }
419         sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
420         if (sc->sc_bulkout_xfer == NULL) {
421                 uscanner_do_close(sc);
422                 return (ENOMEM);
423         }
424
425         return (0);     /* success */
426 }
427
428 int
429 uscannerclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
430 {
431         struct uscanner_softc *sc;
432
433         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
434
435         DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
436                      flag, mode, USCANNERUNIT(dev)));
437
438 #ifdef DIAGNOSTIC
439         if (!(sc->sc_state & USCANNER_OPEN)) {
440                 printf("uscannerclose: not open\n");
441                 return (EINVAL);
442         }
443 #endif
444
445         uscanner_do_close(sc);
446
447         return (0);
448 }
449
450 void
451 uscanner_do_close(struct uscanner_softc *sc)
452 {
453         if (sc->sc_bulkin_xfer) {
454                 usbd_free_xfer(sc->sc_bulkin_xfer);
455                 sc->sc_bulkin_xfer = NULL;
456         }
457         if (sc->sc_bulkout_xfer) {
458                 usbd_free_xfer(sc->sc_bulkout_xfer);
459                 sc->sc_bulkout_xfer = NULL;
460         }
461
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;
467                 }
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;
472                 }
473         }
474
475         if (sc->sc_bulkin_buffer) {
476                 free(sc->sc_bulkin_buffer, M_USBDEV);
477                 sc->sc_bulkin_buffer = NULL;
478         }
479         if (sc->sc_bulkout_buffer) {
480                 free(sc->sc_bulkout_buffer, M_USBDEV);
481                 sc->sc_bulkout_buffer = NULL;
482         }
483
484         sc->sc_state &= ~USCANNER_OPEN;
485 }
486
487 Static int
488 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
489 {
490         u_int32_t n, tn;
491         usbd_status err;
492         int error = 0;
493
494         DPRINTFN(5, ("%s: uscannerread\n", USBDEVNAME(sc->sc_dev)));
495
496         if (sc->sc_dying)
497                 return (EIO);
498
499         while ((n = min(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
500                 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
501                 tn = n;
502
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,
507                         "uscnrb");
508                 if (err) {
509                         if (err == USBD_INTERRUPTED)
510                                 error = EINTR;
511                         else if (err == USBD_TIMEOUT)
512                                 error = ETIMEDOUT;
513                         else
514                                 error = EIO;
515                         break;
516                 }
517                 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
518                 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
519                 if (error || tn < n)
520                         break;
521         }
522
523         return (error);
524 }
525
526 int
527 uscannerread(dev_t dev, struct uio *uio, int flag)
528 {
529         struct uscanner_softc *sc;
530         int error;
531
532         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
533
534         sc->sc_refcnt++;
535         error = uscanner_do_read(sc, uio, flag);
536         if (--sc->sc_refcnt < 0)
537                 usb_detach_wakeup(USBDEV(sc->sc_dev));
538
539         return (error);
540 }
541
542 Static int
543 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
544 {
545         u_int32_t n;
546         int error = 0;
547         usbd_status err;
548
549         DPRINTFN(5, ("%s: uscanner_do_write\n", USBDEVNAME(sc->sc_dev)));
550
551         if (sc->sc_dying)
552                 return (EIO);
553
554         while ((n = min(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
555                 error = uiomove(sc->sc_bulkout_buffer, n, uio);
556                 if (error)
557                         break;
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,
561                         0, USBD_NO_TIMEOUT,
562                         sc->sc_bulkout_buffer, &n,
563                         "uscnwb");
564                 if (err) {
565                         if (err == USBD_INTERRUPTED)
566                                 error = EINTR;
567                         else
568                                 error = EIO;
569                         break;
570                 }
571         }
572
573         return (error);
574 }
575
576 int
577 uscannerwrite(dev_t dev, struct uio *uio, int flag)
578 {
579         struct uscanner_softc *sc;
580         int error;
581
582         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
583
584         sc->sc_refcnt++;
585         error = uscanner_do_write(sc, uio, flag);
586         if (--sc->sc_refcnt < 0)
587                 usb_detach_wakeup(USBDEV(sc->sc_dev));
588         return (error);
589 }
590
591 #if defined(__NetBSD__) || defined(__OpenBSD__)
592 int
593 uscanner_activate(device_ptr_t self, enum devact act)
594 {
595         struct uscanner_softc *sc = (struct uscanner_softc *)self;
596
597         switch (act) {
598         case DVACT_ACTIVATE:
599                 return (EOPNOTSUPP);
600                 break;
601
602         case DVACT_DEACTIVATE:
603                 sc->sc_dying = 1;
604                 break;
605         }
606         return (0);
607 }
608 #endif
609
610 USB_DETACH(uscanner)
611 {
612         USB_DETACH_START(uscanner, sc);
613         int s;
614 #if defined(__NetBSD__) || defined(__OpenBSD__)
615         int maj, mn;
616 #elif defined(__FreeBSD__)
617         dev_t dev;
618         struct vnode *vp;
619 #endif
620
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));
625 #endif
626
627         sc->sc_dying = 1;
628         sc->sc_dev_flags = 0;   /* make close really close device */
629
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);
635
636         s = splusb();
637         if (--sc->sc_refcnt >= 0) {
638                 /* Wait for processes to go away. */
639                 usb_detach_wait(USBDEV(sc->sc_dev));
640         }
641         splx(s);
642
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)
647                         break;
648
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);
656         if (vp)
657                 VOP_REVOKE(vp, REVOKEALL);
658         destroy_dev(dev);
659 #endif
660
661         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
662                            USBDEV(sc->sc_dev));
663
664         return (0);
665 }
666
667 int
668 uscannerpoll(dev_t dev, int events, usb_proc_ptr p)
669 {
670         struct uscanner_softc *sc;
671         int revents = 0;
672
673         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
674
675         if (sc->sc_dying)
676                 return (EIO);
677
678         /*
679          * We have no easy way of determining if a read will
680          * yield any data or a write will happen.
681          * Pretend they will.
682          */
683         revents |= events &
684                    (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
685
686         return (revents);
687 }
688
689 #if defined(__FreeBSD__)
690 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);
691 #endif