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