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