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