]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/legacy/dev/usb/uscanner.c
Move usb to a graveyard location under sys/legacy/dev, it is intended that the
[FreeBSD/FreeBSD.git] / sys / legacy / 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
94  /*
95   * These first two entries are duplicates of known-working units,
96   * so one can patch them to test support for newer devices
97   * without rebuilding the module.
98   */
99  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_6000 }, 0 },    /* duplicate */
100  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_6000 }, 0 },    /* duplicate */
101
102   /* Acer Peripherals */
103  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_320U }, 0 },
104  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_4300U }, 0 },
105  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_640U }, 0 },
106  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_640BT }, 0 },
107  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_620U }, 0 },
108  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_1240U }, 0 },
109  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_C310U }, 0 },
110
111   /* AGFA */
112  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1236U }, 0 },
113  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U }, 0 },
114  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U2 }, 0 },
115  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANTOUCH }, 0 },
116  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE40 }, 0 },
117  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE50 }, 0 },
118  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE20 }, 0 },
119  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE25 }, 0 },
120  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE26 }, 0 },
121  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE52 }, 0 },
122
123   /* Avision */
124  {{ USB_VENDOR_AVISION, USB_PRODUCT_AVISION_1200U }, 0 },
125
126   /* Canon */
127  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N656U }, 0 },
128  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N676U }, 0 },
129  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N1220U }, 0 },
130  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_D660U }, 0 },
131  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N1240U }, 0 },
132  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_LIDE25 }, 0 },
133
134   /* Kye */
135  {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
136
137   /* HP */
138  {{ USB_VENDOR_HP, USB_PRODUCT_HP_2200C }, 0 },
139  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3300C }, 0 },
140  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3400CSE }, 0 },
141  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4100C }, 0 },
142  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4200C }, 0 },
143  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4300C }, 0 },
144  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4470C }, 0 },
145  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4670V }, 0 },
146  {{ USB_VENDOR_HP, USB_PRODUCT_HP_S20 }, 0 },
147  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5200C }, 0 },
148  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5300C }, 0 },
149  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5400C }, 0 },
150  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6200C }, 0 },
151  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6300C }, 0 },
152  {{ USB_VENDOR_HP, USB_PRODUCT_HP_82x0C }, 0 },
153
154   /* Microtek */
155  {{ USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_336CX }, 0 },
156  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_X6U }, 0 },
157  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX }, 0 },
158  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX2 }, 0 },
159  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_C6 }, 0 },
160  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL }, 0 },
161  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL2 }, 0 },
162  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6UL }, 0 },
163
164  /* Minolta */
165  {{ USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_5400 }, 0 },
166
167   /* Mustek */
168  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CU }, 0 },
169  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200F }, 0 },
170  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200TA }, 0 },
171  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600USB }, 0 },
172  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600CU }, 0 },
173  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USB }, 0 },
174  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200UB }, 0 },
175  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USBPLUS }, 0 },
176  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CUPLUS }, 0 },
177
178   /* National */
179  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW1200 }, 0 },
180  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW2400 }, 0 },
181
182   /* Nikon */
183  {{ USB_VENDOR_NIKON, USB_PRODUCT_NIKON_LS40 }, 0 },
184
185   /* Primax */
186  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2X300 }, 0 },
187  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E300 }, 0 },
188  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2300 }, 0 },
189  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E3002 }, 0 },
190  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_9600 }, 0 },
191  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_600U }, 0 },
192  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_6200 }, 0 },
193  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_19200 }, 0 },
194  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_1200U }, 0 },
195  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G600 }, 0 },
196  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_636I }, 0 },
197  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2600 }, 0 },
198  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E600 }, 0 },
199
200   /* Epson */
201  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_636 }, 0 },
202  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_610 }, 0 },
203  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1200 }, 0 },
204  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1240 }, 0 },
205  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1250 }, 0 },
206  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1600 }, 0 },
207  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1640 }, 0 },
208  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_640U }, 0 },
209  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1650 }, 0 },
210  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1660 }, 0 },
211  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1670 }, 0 },
212  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1260 }, 0 },
213  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1270 }, 0 },
214  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_RX425 }, 0 },
215  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_3200 }, USC_KEEP_OPEN },
216  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9700F }, USC_KEEP_OPEN },
217  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9300UF }, 0 },
218  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_2480 }, 0 },
219  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_3500 }, USC_KEEP_OPEN },
220  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_3590 }, 0 },
221  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_4200 }, 0 },
222  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_4800 }, 0 },
223  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_4990 }, 0 },
224  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_5000 }, 0 },
225  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_6000 }, 0 },
226  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_CX5400 }, 0 },
227  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_DX7400 }, 0 },
228  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_DX8400 }, 0 },
229  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_CX5400 }, 0 },
230  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_DX3800 }, 0 },
231  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_DX4000 }, 0 },
232  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_NX300 }, 0 },
233  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_SX200 }, 0 },
234  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_SX400 }, 0 },
235
236   /* UMAX */
237  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1220U }, 0 },
238  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1236U }, 0 },
239  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2000U }, 0 },
240  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2100U }, 0 },
241  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2200U }, 0 },
242  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA3400 }, 0 },
243
244   /* Visioneer */
245  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_3000 }, 0 },
246  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_5300 }, 0 },
247  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_7600 }, 0 },
248  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6100 }, 0 },
249  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6200 }, 0 },
250  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8100 }, 0 },
251  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8600 }, 0 },
252
253   /* Ultima */
254  {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
255
256 };
257 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
258
259 #define USCANNER_BUFFERSIZE     1024
260
261 struct uscanner_softc {
262         device_t                sc_dev;         /* base device */
263         usbd_device_handle      sc_udev;
264         usbd_interface_handle   sc_iface;
265         struct cdev *dev;
266
267         u_int                   sc_dev_flags;
268
269         usbd_pipe_handle        sc_bulkin_pipe;
270         int                     sc_bulkin;
271         usbd_xfer_handle        sc_bulkin_xfer;
272         void                    *sc_bulkin_buffer;
273         int                     sc_bulkin_bufferlen;
274         int                     sc_bulkin_datalen;
275
276         usbd_pipe_handle        sc_bulkout_pipe;
277         int                     sc_bulkout;
278         usbd_xfer_handle        sc_bulkout_xfer;
279         void                    *sc_bulkout_buffer;
280         int                     sc_bulkout_bufferlen;
281         int                     sc_bulkout_datalen;
282
283         u_char                  sc_state;
284 #define USCANNER_OPEN           0x01    /* opened */
285
286         int                     sc_refcnt;
287         u_char                  sc_dying;
288 };
289
290 d_open_t  uscanneropen;
291 d_close_t uscannerclose;
292 d_read_t  uscannerread;
293 d_write_t uscannerwrite;
294 d_poll_t  uscannerpoll;
295
296
297 static struct cdevsw uscanner_cdevsw = {
298         .d_version =    D_VERSION,
299         .d_flags =      D_NEEDGIANT,
300         .d_open =       uscanneropen,
301         .d_close =      uscannerclose,
302         .d_read =       uscannerread,
303         .d_write =      uscannerwrite,
304         .d_poll =       uscannerpoll,
305         .d_name =       "uscanner",
306 };
307
308 static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
309 static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
310 static void uscanner_do_close(struct uscanner_softc *);
311
312 #define USCANNERUNIT(n) (dev2unit(n))
313
314 static device_probe_t uscanner_match;
315 static device_attach_t uscanner_attach;
316 static device_detach_t uscanner_detach;
317
318 static device_method_t uscanner_methods[] = {
319         /* Device interface */
320         DEVMETHOD(device_probe,         uscanner_match),
321         DEVMETHOD(device_attach,        uscanner_attach),
322         DEVMETHOD(device_detach,        uscanner_detach),
323
324         { 0, 0 }
325 };
326
327 static driver_t uscanner_driver = {
328         "uscanner",
329         uscanner_methods,
330         sizeof(struct uscanner_softc)
331 };
332
333 static devclass_t uscanner_devclass;
334
335 static int
336 uscanner_match(device_t self)
337 {
338         struct usb_attach_arg *uaa = device_get_ivars(self);
339         usb_interface_descriptor_t *id;
340
341         if (uaa->iface == NULL)
342                 return UMATCH_NONE;     /* do not grab the entire device */
343
344         if (uscanner_lookup(uaa->vendor, uaa->product) == NULL)
345                 return UMATCH_NONE;     /* not in the list of known devices */
346         id = usbd_get_interface_descriptor(uaa->iface);
347         if (id == NULL)
348                 return UMATCH_NONE;
349
350         /*
351          * There isn't a specific UICLASS for scanners, many vendors use
352          * UICLASS_VENDOR, so detecting the right interface is not so easy.
353          * But certainly we can exclude PRINTER and MASS - which some
354          * multifunction devices implement.
355          */
356         if (id->bInterfaceClass == UICLASS_PRINTER ||
357             id->bInterfaceClass == UICLASS_MASS)
358                 return UMATCH_NONE;
359
360         return UMATCH_VENDOR_PRODUCT;   /* ok we found it */
361 }
362
363 static int
364 uscanner_attach(device_t self)
365 {
366         struct uscanner_softc *sc = device_get_softc(self);
367         struct usb_attach_arg *uaa = device_get_ivars(self);
368         usb_interface_descriptor_t *id = 0;
369         usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
370         int i;
371         usbd_status err;
372         int ifnum;
373
374         sc->sc_dev = self;
375         sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
376         sc->sc_udev = uaa->device;
377
378         id = usbd_get_interface_descriptor(uaa->iface);
379         ifnum = id->bInterfaceNumber;
380 #if 0
381         /*
382          * This was in the original driver, but we cannot change the
383          * configuration of the whole device while attaching only to
384          * one of its interfaces. This can kill other already-attached
385          * driver, and/or possibly prevent this driver from attaching
386          * if an error occurs in set_config_no.
387          * If a device need setting the configuration, this must be done
388          * before attaching drivers to the various interfaces.
389          */
390         err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
391         if (err) {
392                 printf("%s: setting config no failed\n",
393                     device_get_nameunit(sc->sc_dev));
394                 return ENXIO;
395         }
396 #endif
397         err = usbd_device2interface_handle(sc->sc_udev, ifnum, &sc->sc_iface);
398         if (!err && sc->sc_iface)
399             id = usbd_get_interface_descriptor(sc->sc_iface);
400         if (err || id == 0) {
401                 printf("%s: could not get interface descriptor, err=%d,id=%p\n",
402                        device_get_nameunit(sc->sc_dev), err, id);
403                 return ENXIO;
404         }
405
406         /* Find the two first bulk endpoints */
407         for (i = 0 ; i < id->bNumEndpoints; i++) {
408                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
409                 if (ed == 0) {
410                         printf("%s: could not read endpoint descriptor\n",
411                                device_get_nameunit(sc->sc_dev));
412                         return ENXIO;
413                 }
414
415                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
416                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
417                         ed_bulkin = ed;
418                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
419                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
420                         ed_bulkout = ed;
421                 }
422
423                 if (ed_bulkin && ed_bulkout)    /* found all we need */
424                         break;
425         }
426
427         /* Verify that we goething sensible */
428         if (ed_bulkin == NULL || ed_bulkout == NULL) {
429                 printf("%s: bulk-in and/or bulk-out endpoint not found\n",
430                         device_get_nameunit(sc->sc_dev));
431                 return ENXIO;
432         }
433
434         sc->sc_bulkin = ed_bulkin->bEndpointAddress;
435         sc->sc_bulkout = ed_bulkout->bEndpointAddress;
436
437         /* the main device, ctrl endpoint */
438         sc->dev = make_dev(&uscanner_cdevsw, device_get_unit(sc->sc_dev),
439                 UID_ROOT, GID_OPERATOR, 0644, "%s", device_get_nameunit(sc->sc_dev));
440         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,sc->sc_dev);
441
442         return 0;
443 }
444
445 int
446 uscanneropen(struct cdev *dev, int flag, int mode, struct thread *p)
447 {
448         struct uscanner_softc *sc;
449         int unit = USCANNERUNIT(dev);
450         usbd_status err;
451
452         sc = devclass_get_softc(uscanner_devclass, unit);
453         if (sc == NULL)
454                 return (ENXIO);
455
456         DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
457                      flag, mode, unit));
458
459         if (sc->sc_dying)
460                 return (ENXIO);
461
462         if (sc->sc_state & USCANNER_OPEN)
463                 return (EBUSY);
464
465         sc->sc_state |= USCANNER_OPEN;
466
467         sc->sc_bulkin_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
468         sc->sc_bulkout_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
469         /* No need to check buffers for NULL since we have WAITOK */
470
471         sc->sc_bulkin_bufferlen = USCANNER_BUFFERSIZE;
472         sc->sc_bulkout_bufferlen = USCANNER_BUFFERSIZE;
473
474         /* We have decided on which endpoints to use, now open the pipes */
475         if (sc->sc_bulkin_pipe == NULL) {
476                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
477                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
478                 if (err) {
479                         printf("%s: cannot open bulk-in pipe (addr %d)\n",
480                                device_get_nameunit(sc->sc_dev), sc->sc_bulkin);
481                         uscanner_do_close(sc);
482                         return (EIO);
483                 }
484         }
485         if (sc->sc_bulkout_pipe == NULL) {
486                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
487                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
488                 if (err) {
489                         printf("%s: cannot open bulk-out pipe (addr %d)\n",
490                                device_get_nameunit(sc->sc_dev), sc->sc_bulkout);
491                         uscanner_do_close(sc);
492                         return (EIO);
493                 }
494         }
495
496         sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
497         if (sc->sc_bulkin_xfer == NULL) {
498                 uscanner_do_close(sc);
499                 return (ENOMEM);
500         }
501         sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
502         if (sc->sc_bulkout_xfer == NULL) {
503                 uscanner_do_close(sc);
504                 return (ENOMEM);
505         }
506
507         return (0);     /* success */
508 }
509
510 int
511 uscannerclose(struct cdev *dev, int flag, int mode, struct thread *p)
512 {
513         struct uscanner_softc *sc;
514
515         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
516         DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
517                      flag, mode, USCANNERUNIT(dev)));
518
519 #ifdef DIAGNOSTIC
520         if (!(sc->sc_state & USCANNER_OPEN)) {
521                 printf("uscannerclose: not open\n");
522                 return (EINVAL);
523         }
524 #endif
525
526         uscanner_do_close(sc);
527
528         return (0);
529 }
530
531 void
532 uscanner_do_close(struct uscanner_softc *sc)
533 {
534         if (sc->sc_bulkin_xfer) {
535                 usbd_free_xfer(sc->sc_bulkin_xfer);
536                 sc->sc_bulkin_xfer = NULL;
537         }
538         if (sc->sc_bulkout_xfer) {
539                 usbd_free_xfer(sc->sc_bulkout_xfer);
540                 sc->sc_bulkout_xfer = NULL;
541         }
542
543         if (!(sc->sc_dev_flags & USC_KEEP_OPEN)) {
544                 if (sc->sc_bulkin_pipe != NULL) {
545                         usbd_abort_pipe(sc->sc_bulkin_pipe);
546                         usbd_close_pipe(sc->sc_bulkin_pipe);
547                         sc->sc_bulkin_pipe = NULL;
548                 }
549                 if (sc->sc_bulkout_pipe != NULL) {
550                         usbd_abort_pipe(sc->sc_bulkout_pipe);
551                         usbd_close_pipe(sc->sc_bulkout_pipe);
552                         sc->sc_bulkout_pipe = NULL;
553                 }
554         }
555
556         if (sc->sc_bulkin_buffer) {
557                 free(sc->sc_bulkin_buffer, M_USBDEV);
558                 sc->sc_bulkin_buffer = NULL;
559         }
560         if (sc->sc_bulkout_buffer) {
561                 free(sc->sc_bulkout_buffer, M_USBDEV);
562                 sc->sc_bulkout_buffer = NULL;
563         }
564
565         sc->sc_state &= ~USCANNER_OPEN;
566 }
567
568 static int
569 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
570 {
571         u_int32_t n, tn;
572         usbd_status err;
573         int error = 0;
574
575         DPRINTFN(5, ("%s: uscannerread\n", device_get_nameunit(sc->sc_dev)));
576
577         if (sc->sc_dying)
578                 return (EIO);
579
580         while ((n = min(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
581                 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
582                 tn = n;
583
584                 err = usbd_bulk_transfer(
585                         sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
586                         USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
587                         sc->sc_bulkin_buffer, &tn,
588                         "uscnrb");
589                 if (err) {
590                         if (err == USBD_INTERRUPTED)
591                                 error = EINTR;
592                         else if (err == USBD_TIMEOUT)
593                                 error = ETIMEDOUT;
594                         else
595                                 error = EIO;
596                         break;
597                 }
598                 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
599                 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
600                 if (error || tn < n)
601                         break;
602         }
603
604         return (error);
605 }
606
607 int
608 uscannerread(struct cdev *dev, struct uio *uio, int flag)
609 {
610         struct uscanner_softc *sc;
611         int error;
612
613         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
614         sc->sc_refcnt++;
615         error = uscanner_do_read(sc, uio, flag);
616         if (--sc->sc_refcnt < 0)
617                 usb_detach_wakeup(sc->sc_dev);
618
619         return (error);
620 }
621
622 static int
623 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
624 {
625         u_int32_t n;
626         int error = 0;
627         usbd_status err;
628
629         DPRINTFN(5, ("%s: uscanner_do_write\n", device_get_nameunit(sc->sc_dev)));
630
631         if (sc->sc_dying)
632                 return (EIO);
633
634         while ((n = min(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
635                 error = uiomove(sc->sc_bulkout_buffer, n, uio);
636                 if (error)
637                         break;
638                 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
639                 err = usbd_bulk_transfer(
640                         sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
641                         0, USBD_NO_TIMEOUT,
642                         sc->sc_bulkout_buffer, &n,
643                         "uscnwb");
644                 if (err) {
645                         if (err == USBD_INTERRUPTED)
646                                 error = EINTR;
647                         else
648                                 error = EIO;
649                         break;
650                 }
651         }
652
653         return (error);
654 }
655
656 int
657 uscannerwrite(struct cdev *dev, struct uio *uio, int flag)
658 {
659         struct uscanner_softc *sc;
660         int error;
661
662         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
663         sc->sc_refcnt++;
664         error = uscanner_do_write(sc, uio, flag);
665         if (--sc->sc_refcnt < 0)
666                 usb_detach_wakeup(sc->sc_dev);
667         return (error);
668 }
669
670 static int
671 uscanner_detach(device_t self)
672 {
673         struct uscanner_softc *sc = device_get_softc(self);
674         int s;
675
676         DPRINTF(("uscanner_detach: sc=%p\n", sc));
677
678         sc->sc_dying = 1;
679         sc->sc_dev_flags = 0;   /* make close really close device */
680
681         /* Abort all pipes.  Causes processes waiting for transfer to wake. */
682         if (sc->sc_bulkin_pipe != NULL)
683                 usbd_abort_pipe(sc->sc_bulkin_pipe);
684         if (sc->sc_bulkout_pipe != NULL)
685                 usbd_abort_pipe(sc->sc_bulkout_pipe);
686
687         s = splusb();
688         if (--sc->sc_refcnt >= 0) {
689                 /* Wait for processes to go away. */
690                 usb_detach_wait(sc->sc_dev);
691         }
692         splx(s);
693
694         /* destroy the device for the control endpoint */
695         destroy_dev(sc->dev);
696         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
697
698         return (0);
699 }
700
701 int
702 uscannerpoll(struct cdev *dev, int events, struct thread *p)
703 {
704         struct uscanner_softc *sc;
705         int revents = 0;
706
707         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
708         if (sc->sc_dying)
709                 return (EIO);
710
711         /*
712          * We have no easy way of determining if a read will
713          * yield any data or a write will happen.
714          * Pretend they will.
715          */
716         revents |= events &
717                    (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
718
719         return (revents);
720 }
721
722 MODULE_DEPEND(uscanner, usb, 1, 1, 1);
723 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);