3d5ff235d8beebd7f7a89e9d7a00bbb34db4a1b1
[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.21 2007/07/02 06:43:31 hasso 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 #include <sys/module.h>
55 #include <sys/bus.h>
56 #include <sys/conf.h>
57 #include <sys/fcntl.h>
58 #include <sys/filio.h>
59 #include <sys/tty.h>
60 #include <sys/file.h>
61 #include <sys/select.h>
62 #include <sys/proc.h>
63 #include <sys/vnode.h>
64 #include <sys/poll.h>
65 #include <sys/conf.h>
66 #include <sys/sysctl.h>
67 #include <sys/thread2.h>
68
69 #include <bus/usb/usb.h>
70 #include <bus/usb/usbdi.h>
71 #include <bus/usb/usbdi_util.h>
72
73 #include <bus/usb/usbdevs.h>
74
75 #ifdef USB_DEBUG
76 #define DPRINTF(x)      if (uscannerdebug) kprintf x
77 #define DPRINTFN(n,x)   if (uscannerdebug>(n)) kprintf 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  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N676U }, 0 },
119  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_D660U }, 0 },
120
121   /* Kye */
122  {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
123
124   /* HP */
125  {{ USB_VENDOR_HP, USB_PRODUCT_HP_2200C }, 0 },
126  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3300C }, 0 },
127  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3400CSE }, 0 },
128  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4100C }, 0 },
129  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4200C }, 0 },
130  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4300C }, 0 },
131  {{ USB_VENDOR_HP, USB_PRODUCT_HP_S20 }, 0 },
132  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5200C }, 0 },
133  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5300C }, 0 },
134  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5400C }, 0 },
135  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6200C }, 0 },
136  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6300C }, 0 },
137
138   /* Microtek */
139  {{ USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_336CX }, 0 },
140  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_X6U }, 0 },
141  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX }, 0 },
142  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX2 }, 0 },
143  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_C6 }, 0 },
144  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL }, 0 },
145  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL2 }, 0 },
146  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6UL }, 0 },
147
148   /* Minolta */
149  {{ USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_5400 }, 0 },
150
151   /* Mustek */
152  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CU }, 0 },
153  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200F }, 0 },
154  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200TA }, 0 },
155  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600USB }, 0 },
156  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600CU }, 0 },
157  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USB }, 0 },
158  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200UB }, 0 },
159  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USBPLUS }, 0 },
160  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CUPLUS }, 0 },
161
162   /* National */
163  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW1200 }, 0 },
164  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW2400 }, 0 },
165
166   /* Primax */
167  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2X300 }, 0 },
168  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E300 }, 0 },
169  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2300 }, 0 },
170  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E3002 }, 0 },
171  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_9600 }, 0 },
172  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_600U }, 0 },
173  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_6200 }, 0 },
174  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_19200 }, 0 },
175  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_1200U }, 0 },
176  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G600 }, 0 },
177  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_636I }, 0 },
178  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2600 }, 0 },
179  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E600 }, 0 },
180
181   /* Epson */
182  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_636 }, 0 },
183  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_610 }, 0 },
184  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1200 }, 0 },
185  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1240 }, 0 },
186  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1250 }, 0 },
187  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1600 }, 0 },
188  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1640 }, 0 },
189  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_640U }, 0 },
190  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1650 }, 0 },
191  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1660 }, 0 },
192  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1260 }, 0 },
193  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9700F }, USC_KEEP_OPEN },
194  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9300UF }, 0 },
195
196   /* UMAX */
197  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1220U }, 0 },
198  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1236U }, 0 },
199  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2000U }, 0 },
200  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2100U }, 0 },
201  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2200U }, 0 },
202  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA3400 }, 0 },
203
204   /* Visioneer */
205  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_3000 }, 0 },
206  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_5300 }, 0 },
207  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_7600 }, 0 },
208  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6100 }, 0 },
209  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6200 }, 0 },
210  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8100 }, 0 },
211  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8600 }, 0 },
212
213   /* Ultima */
214  {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
215
216 };
217 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
218
219 #define USCANNER_BUFFERSIZE     1024
220
221 struct uscanner_softc {
222         device_t                sc_dev;         /* base device */
223         usbd_device_handle      sc_udev;
224         usbd_interface_handle   sc_iface;
225
226         u_int                   sc_dev_flags;
227
228         usbd_pipe_handle        sc_bulkin_pipe;
229         int                     sc_bulkin;
230         usbd_xfer_handle        sc_bulkin_xfer;
231         void                    *sc_bulkin_buffer;
232         int                     sc_bulkin_bufferlen;
233         int                     sc_bulkin_datalen;
234
235         usbd_pipe_handle        sc_bulkout_pipe;
236         int                     sc_bulkout;
237         usbd_xfer_handle        sc_bulkout_xfer;
238         void                    *sc_bulkout_buffer;
239         int                     sc_bulkout_bufferlen;
240         int                     sc_bulkout_datalen;
241
242         u_char                  sc_state;
243 #define USCANNER_OPEN           0x01    /* opened */
244
245         int                     sc_refcnt;
246         u_char                  sc_dying;
247 };
248
249 d_open_t  uscanneropen;
250 d_close_t uscannerclose;
251 d_read_t  uscannerread;
252 d_write_t uscannerwrite;
253 d_poll_t  uscannerpoll;
254
255 #define USCANNER_CDEV_MAJOR     156
256
257 static struct dev_ops uscanner_ops = {
258         { "uscanner", USCANNER_CDEV_MAJOR, 0 },
259         .d_open =       uscanneropen,
260         .d_close =      uscannerclose,
261         .d_read =       uscannerread,
262         .d_write =      uscannerwrite,
263         .d_poll =       uscannerpoll,
264 };
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 static int
275 uscanner_match(device_t self)
276 {
277         struct usb_attach_arg *uaa = device_get_ivars(self);
278
279         if (uaa->iface != NULL)
280                 return UMATCH_NONE;
281
282         return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
283                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
284 }
285
286 static int
287 uscanner_attach(device_t self)
288 {
289         struct uscanner_softc *sc = device_get_softc(self);
290         struct usb_attach_arg *uaa = device_get_ivars(self);
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         sc->sc_dev = self;
299         device_set_desc_copy(self, devinfo);
300         kprintf("%s: %s\n", device_get_nameunit(sc->sc_dev), devinfo);
301
302         sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
303
304         sc->sc_udev = uaa->device;
305
306         err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
307         if (err) {
308                 kprintf("%s: setting config no failed\n",
309                     device_get_nameunit(sc->sc_dev));
310                 return ENXIO;
311         }
312
313         /* XXX We only check the first interface */
314         err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
315         if (!err && sc->sc_iface)
316             id = usbd_get_interface_descriptor(sc->sc_iface);
317         if (err || id == 0) {
318                 kprintf("%s: could not get interface descriptor, err=%d,id=%p\n",
319                        device_get_nameunit(sc->sc_dev), err, id);
320                 return ENXIO;
321         }
322
323         /* Find the two first bulk endpoints */
324         for (i = 0 ; i < id->bNumEndpoints; i++) {
325                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
326                 if (ed == 0) {
327                         kprintf("%s: could not read endpoint descriptor\n",
328                                device_get_nameunit(sc->sc_dev));
329                         return ENXIO;
330                 }
331
332                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
333                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
334                         ed_bulkin = ed;
335                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
336                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
337                         ed_bulkout = ed;
338                 }
339
340                 if (ed_bulkin && ed_bulkout)    /* found all we need */
341                         break;
342         }
343
344         /* Verify that we goething sensible */
345         if (ed_bulkin == NULL || ed_bulkout == NULL) {
346                 kprintf("%s: bulk-in and/or bulk-out endpoint not found\n",
347                         device_get_nameunit(sc->sc_dev));
348                 return ENXIO;
349         }
350
351         sc->sc_bulkin = ed_bulkin->bEndpointAddress;
352         sc->sc_bulkout = ed_bulkout->bEndpointAddress;
353
354         /* the main device, ctrl endpoint */
355         dev_ops_add(&uscanner_ops, -1, device_get_unit(sc->sc_dev));
356         make_dev(&uscanner_ops, device_get_unit(sc->sc_dev),
357                 UID_ROOT, GID_OPERATOR, 0644, "%s", device_get_nameunit(sc->sc_dev));
358
359         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
360                            sc->sc_dev);
361
362         return 0;
363 }
364
365 int
366 uscanneropen(struct dev_open_args *ap)
367 {
368         cdev_t dev = ap->a_head.a_dev;
369         struct uscanner_softc *sc;
370         int unit = USCANNERUNIT(dev);
371         usbd_status err;
372
373         sc = devclass_get_softc(uscanner_devclass, unit);
374         if (sc == NULL)
375                 return (ENXIO);
376
377         DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
378                      ap->a_oflags, ap->a_devtype, unit));
379
380         if (sc->sc_dying)
381                 return (ENXIO);
382
383         if (sc->sc_state & USCANNER_OPEN)
384                 return (EBUSY);
385
386         sc->sc_state |= USCANNER_OPEN;
387
388         sc->sc_bulkin_buffer = kmalloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
389         sc->sc_bulkout_buffer = kmalloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
390         /* No need to check buffers for NULL since we have WAITOK */
391
392         sc->sc_bulkin_bufferlen = USCANNER_BUFFERSIZE;
393         sc->sc_bulkout_bufferlen = USCANNER_BUFFERSIZE;
394
395         /* We have decided on which endpoints to use, now open the pipes */
396         if (sc->sc_bulkin_pipe == NULL) {
397                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
398                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
399                 if (err) {
400                         kprintf("%s: cannot open bulk-in pipe (addr %d)\n",
401                                device_get_nameunit(sc->sc_dev), sc->sc_bulkin);
402                         uscanner_do_close(sc);
403                         return (EIO);
404                 }
405         }
406         if (sc->sc_bulkout_pipe == NULL) {
407                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
408                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
409                 if (err) {
410                         kprintf("%s: cannot open bulk-out pipe (addr %d)\n",
411                                device_get_nameunit(sc->sc_dev), sc->sc_bulkout);
412                         uscanner_do_close(sc);
413                         return (EIO);
414                 }
415         }
416
417         sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
418         if (sc->sc_bulkin_xfer == NULL) {
419                 uscanner_do_close(sc);
420                 return (ENOMEM);
421         }
422         sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
423         if (sc->sc_bulkout_xfer == NULL) {
424                 uscanner_do_close(sc);
425                 return (ENOMEM);
426         }
427
428         return (0);     /* success */
429 }
430
431 int
432 uscannerclose(struct dev_close_args *ap)
433 {
434         cdev_t dev = ap->a_head.a_dev;
435         struct uscanner_softc *sc;
436
437         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
438
439         DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
440                      ap->a_fflag, ap->a_devtype, USCANNERUNIT(dev)));
441
442 #ifdef DIAGNOSTIC
443         if (!(sc->sc_state & USCANNER_OPEN)) {
444                 kprintf("uscannerclose: not open\n");
445                 return (EINVAL);
446         }
447 #endif
448
449         uscanner_do_close(sc);
450
451         return (0);
452 }
453
454 void
455 uscanner_do_close(struct uscanner_softc *sc)
456 {
457         if (sc->sc_bulkin_xfer) {
458                 usbd_free_xfer(sc->sc_bulkin_xfer);
459                 sc->sc_bulkin_xfer = NULL;
460         }
461         if (sc->sc_bulkout_xfer) {
462                 usbd_free_xfer(sc->sc_bulkout_xfer);
463                 sc->sc_bulkout_xfer = NULL;
464         }
465
466         if (!(sc->sc_dev_flags & USC_KEEP_OPEN)) {
467                 if (sc->sc_bulkin_pipe != NULL) {
468                         usbd_abort_pipe(sc->sc_bulkin_pipe);
469                         usbd_close_pipe(sc->sc_bulkin_pipe);
470                         sc->sc_bulkin_pipe = NULL;
471                 }
472                 if (sc->sc_bulkout_pipe != NULL) {
473                         usbd_abort_pipe(sc->sc_bulkout_pipe);
474                         usbd_close_pipe(sc->sc_bulkout_pipe);
475                         sc->sc_bulkout_pipe = NULL;
476                 }
477         }
478
479         if (sc->sc_bulkin_buffer) {
480                 kfree(sc->sc_bulkin_buffer, M_USBDEV);
481                 sc->sc_bulkin_buffer = NULL;
482         }
483         if (sc->sc_bulkout_buffer) {
484                 kfree(sc->sc_bulkout_buffer, M_USBDEV);
485                 sc->sc_bulkout_buffer = NULL;
486         }
487
488         sc->sc_state &= ~USCANNER_OPEN;
489 }
490
491 static int
492 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
493 {
494         u_int32_t n, tn;
495         usbd_status err;
496         int error = 0;
497
498         DPRINTFN(5, ("%s: uscannerread\n", device_get_nameunit(sc->sc_dev)));
499
500         if (sc->sc_dying)
501                 return (EIO);
502
503         while ((n = min(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
504                 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
505                 tn = n;
506
507                 err = usbd_bulk_transfer(
508                         sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
509                         USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
510                         sc->sc_bulkin_buffer, &tn,
511                         "uscnrb");
512                 if (err) {
513                         if (err == USBD_INTERRUPTED)
514                                 error = EINTR;
515                         else if (err == USBD_TIMEOUT)
516                                 error = ETIMEDOUT;
517                         else
518                                 error = EIO;
519                         break;
520                 }
521                 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
522                 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
523                 if (error || tn < n)
524                         break;
525         }
526
527         return (error);
528 }
529
530 int
531 uscannerread(struct dev_read_args *ap)
532 {
533         cdev_t dev = ap->a_head.a_dev;
534         struct uscanner_softc *sc;
535         int error;
536
537         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
538
539         sc->sc_refcnt++;
540         error = uscanner_do_read(sc, ap->a_uio, ap->a_ioflag);
541         if (--sc->sc_refcnt < 0)
542                 usb_detach_wakeup(sc->sc_dev);
543
544         return (error);
545 }
546
547 static int
548 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
549 {
550         u_int32_t n;
551         int error = 0;
552         usbd_status err;
553
554         DPRINTFN(5, ("%s: uscanner_do_write\n", device_get_nameunit(sc->sc_dev)));
555
556         if (sc->sc_dying)
557                 return (EIO);
558
559         while ((n = min(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
560                 error = uiomove(sc->sc_bulkout_buffer, n, uio);
561                 if (error)
562                         break;
563                 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
564                 err = usbd_bulk_transfer(
565                         sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
566                         0, USBD_NO_TIMEOUT,
567                         sc->sc_bulkout_buffer, &n,
568                         "uscnwb");
569                 if (err) {
570                         if (err == USBD_INTERRUPTED)
571                                 error = EINTR;
572                         else
573                                 error = EIO;
574                         break;
575                 }
576         }
577
578         return (error);
579 }
580
581 int
582 uscannerwrite(struct dev_write_args *ap)
583 {
584         cdev_t dev = ap->a_head.a_dev;
585         struct uscanner_softc *sc;
586         int error;
587
588         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
589
590         sc->sc_refcnt++;
591         error = uscanner_do_write(sc, ap->a_uio, ap->a_ioflag);
592         if (--sc->sc_refcnt < 0)
593                 usb_detach_wakeup(sc->sc_dev);
594         return (error);
595 }
596
597 static int
598 uscanner_detach(device_t self)
599 {
600         struct uscanner_softc *sc = device_get_softc(self);
601
602         DPRINTF(("uscanner_detach: sc=%p\n", sc));
603
604         sc->sc_dying = 1;
605         sc->sc_dev_flags = 0;   /* make close really close device */
606
607         /* Abort all pipes.  Causes processes waiting for transfer to wake. */
608         if (sc->sc_bulkin_pipe != NULL)
609                 usbd_abort_pipe(sc->sc_bulkin_pipe);
610         if (sc->sc_bulkout_pipe != NULL)
611                 usbd_abort_pipe(sc->sc_bulkout_pipe);
612
613         crit_enter();
614         if (--sc->sc_refcnt >= 0) {
615                 /* Wait for processes to go away. */
616                 usb_detach_wait(sc->sc_dev);
617         }
618         crit_exit();
619
620         /* destroy the device for the control endpoint */
621         dev_ops_remove(&uscanner_ops, -1, device_get_unit(sc->sc_dev));
622
623         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
624                            sc->sc_dev);
625
626         return (0);
627 }
628
629 int
630 uscannerpoll(struct dev_poll_args *ap)
631 {
632         cdev_t dev = ap->a_head.a_dev;
633         struct uscanner_softc *sc;
634         int revents = 0;
635
636         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
637
638         if (sc->sc_dying)
639                 return (EIO);
640
641         /*
642          * We have no easy way of determining if a read will
643          * yield any data or a write will happen.
644          * Pretend they will.
645          */
646         revents |= ap->a_events &
647                    (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
648
649         ap->a_events = revents;
650         return (0);
651 }
652
653 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);
654