f8b40b007d9f85819ba0196175789a1ff4c1b601
[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  */
5
6 /* Also already merged from NetBSD:
7  *      $NetBSD: uscanner.c,v 1.33 2002/09/23 05:51:24 simonb Exp $
8  */
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/select.h>
61 #include <sys/proc.h>
62 #include <sys/event.h>
63 #include <sys/conf.h>
64 #include <sys/sysctl.h>
65 #include <sys/thread2.h>
66
67 #include <bus/usb/usb.h>
68 #include <bus/usb/usbdi.h>
69 #include <bus/usb/usbdi_util.h>
70
71 #ifdef USB_DEBUG
72 #define DPRINTF(x)      if (uscannerdebug) kprintf x
73 #define DPRINTFN(n,x)   if (uscannerdebug>(n)) kprintf x
74 int     uscannerdebug = 0;
75 SYSCTL_NODE(_hw_usb, OID_AUTO, uscanner, CTLFLAG_RW, 0, "USB uscanner");
76 SYSCTL_INT(_hw_usb_uscanner, OID_AUTO, debug, CTLFLAG_RW,
77            &uscannerdebug, 0, "uscanner debug level");
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n,x)
81 #endif
82
83 struct uscan_info {
84         struct usb_devno devno;
85         u_int flags;
86 #define USC_KEEP_OPEN 1
87 };
88
89 /* Table of scanners that may work with this driver. */
90 static const struct uscan_info uscanner_devs[] = {
91   /* Acer Peripherals */
92  {{ USB_DEVICE(0x04a5, 0x2022) }, 0 }, /* Acerscan 320U */
93  {{ USB_DEVICE(0x04a5, 0x20b0) }, 0 }, /* Benq 3300U/4300U */
94  {{ USB_DEVICE(0x04a5, 0x2040) }, 0 }, /* Acerscan 640U */
95  {{ USB_DEVICE(0x04a5, 0x20be) }, 0 }, /* Acerscan 640BT */
96  {{ USB_DEVICE(0x04a5, 0x2060) }, 0 }, /* Acerscan 620U */
97  {{ USB_DEVICE(0x04a5, 0x20c0) }, 0 }, /* Acerscan 1240U */
98  {{ USB_DEVICE(0x04a5, 0x12a6) }, 0 }, /* Acerscan C310U */
99   /* AGFA */
100  {{ USB_DEVICE(0x06bd, 0x0002) }, 0 }, /* SnapScan 1236U */
101  {{ USB_DEVICE(0x06bd, 0x0001) }, 0 }, /* SnapScan 1212U */
102  {{ USB_DEVICE(0x06bd, 0x2061) }, 0 }, /* SnapScan 1212U */
103  {{ USB_DEVICE(0x06bd, 0x0100) }, 0 }, /* SnapScan Touch */
104  {{ USB_DEVICE(0x06bd, 0x208d) }, 0 }, /* SnapScan e40 */
105  {{ USB_DEVICE(0x06bd, 0x208f) }, 0 }, /* SnapScan e50 */
106  {{ USB_DEVICE(0x06bd, 0x2091) }, 0 }, /* SnapScan e20 */
107  {{ USB_DEVICE(0x06bd, 0x2095) }, 0 }, /* SnapScan e25 */
108  {{ USB_DEVICE(0x06bd, 0x2097) }, 0 }, /* SnapScan e26 */
109  {{ USB_DEVICE(0x06bd, 0x20fd) }, 0 }, /* SnapScan e52 */
110   /* Avision */
111  {{ USB_DEVICE(0x0638, 0x0268) }, 0 }, /* Avision 1200U */
112   /* Canon */
113  {{ USB_DEVICE(0x04a9, 0x2206) }, 0 }, /* CanoScan N656U */
114  {{ USB_DEVICE(0x04a9, 0x220d) }, 0 }, /* CanoScan N676U */
115  {{ USB_DEVICE(0x04a9, 0x2207) }, 0 }, /* CanoScan N1220U */
116  {{ USB_DEVICE(0x04a9, 0x2208) }, 0 }, /* CanoScan D660U */
117  {{ USB_DEVICE(0x04a9, 0x220e) }, 0 }, /* CanoScan N1240U */
118  {{ USB_DEVICE(0x04a9, 0x2220) }, 0 }, /* CanoScan LIDE 25 */
119   /* Kye */
120  {{ USB_DEVICE(0x0458, 0x2001) }, 0 }, /* ColorPage Vivid-Pro */
121   /* HP */
122  {{ USB_DEVICE(0x03f0, 0x0605) }, 0 }, /* ScanJet 2200C */
123  {{ USB_DEVICE(0x03f0, 0x0205) }, 0 }, /* ScanJet 3300C */
124  {{ USB_DEVICE(0x03f0, 0x0405) }, 0 }, /* ScanJet 3400cse */
125  {{ USB_DEVICE(0x03f0, 0x0101) }, 0 }, /* Scanjet 4100C */
126  {{ USB_DEVICE(0x03f0, 0x0105) }, 0 }, /* ScanJet 4200C */
127  {{ USB_DEVICE(0x03f0, 0x0305) }, 0 }, /* Scanjet 4300C */
128  {{ USB_DEVICE(0x03f0, 0x3005) }, 0 }, /* ScanJet 4670v */
129  {{ USB_DEVICE(0x03f0, 0x0102) }, 0 }, /* Photosmart S20 */
130  {{ USB_DEVICE(0x03f0, 0x0401) }, 0 }, /* Scanjet 5200C */
131  {{ USB_DEVICE(0x03f0, 0x0701) }, 0 }, /* Scanjet 5300C */
132  {{ USB_DEVICE(0x03f0, 0x1005) }, 0 }, /* Scanjet 5400C */
133  {{ USB_DEVICE(0x03f0, 0x0201) }, 0 }, /* ScanJet 6200C */
134  {{ USB_DEVICE(0x03f0, 0x0601) }, 0 }, /* Scanjet 6300C */
135  {{ USB_DEVICE(0x03f0, 0x0b01) }, 0 }, /* Scanjet 82x0C */
136   /* Scanlogic */
137  {{ USB_DEVICE(0x04ce, 0x0300) }, 0 }, /* Phantom 336CX - C3 */
138   /* Microtek */
139  {{ USB_DEVICE(0x05da, 0x0099) }, 0 }, /* Phantom 336CX - C3 */
140  {{ USB_DEVICE(0x05da, 0x0094) }, 0 }, /* ScanMaker X6 - X6U */
141  {{ USB_DEVICE(0x05da, 0x00a0) }, 0 }, /* Phantom 336CX - C3 */
142  {{ USB_DEVICE(0x05da, 0x009a) }, 0 }, /* Phantom C6 */
143  {{ USB_DEVICE(0x05da, 0x00a3) }, 0 }, /* ScanMaker V6USL */
144  {{ USB_DEVICE(0x05da, 0x80a3) }, 0 }, /* ScanMaker V6USL */
145  {{ USB_DEVICE(0x05da, 0x80ac) }, 0 }, /* ScanMaker V6UL */
146   /* Minolta */
147  {{ USB_DEVICE(0x0686, 0x400e) }, 0 }, /* Dimage 5400 */
148   /* Mustek */
149  {{ USB_DEVICE(0x055f, 0x0001) }, 0 }, /* 1200 CU */
150  {{ USB_DEVICE(0x055f, 0x0010) }, 0 }, /* BearPaw 1200F */
151  {{ USB_DEVICE(0x055f, 0x021e) }, 0 }, /* BearPaw 1200TA */
152  {{ USB_DEVICE(0x055f, 0x0873) }, 0 }, /* 600 USB */
153  {{ USB_DEVICE(0x055f, 0x0002) }, 0 }, /* 600 CU */
154  {{ USB_DEVICE(0x055f, 0x0003) }, 0 }, /* 1200 USB */
155  {{ USB_DEVICE(0x055f, 0x0006) }, 0 }, /* 1200 UB */
156  {{ USB_DEVICE(0x055f, 0x0007) }, 0 }, /* 1200 USB */
157  {{ USB_DEVICE(0x055f, 0x0008) }, 0 }, /* 1200 CU */
158   /* National */
159  {{ USB_DEVICE(0x0400, 0x1000) }, 0 }, /* BearPaw 1200 */
160  {{ USB_DEVICE(0x0400, 0x1001) }, 0 }, /* BearPaw 2400 */
161   /* Nikon */
162  {{ USB_DEVICE(0x04b0, 0x4000) }, 0 }, /* CoolScan LS40 ED */
163   /* Primax */
164  {{ USB_DEVICE(0x0461, 0x0300) }, 0 }, /* G2-200 */
165  {{ USB_DEVICE(0x0461, 0x0301) }, 0 }, /* G2E-300 */
166  {{ USB_DEVICE(0x0461, 0x0302) }, 0 }, /* G2-300 */
167  {{ USB_DEVICE(0x0461, 0x0303) }, 0 }, /* G2E-300 */
168  {{ USB_DEVICE(0x0461, 0x0340) }, 0 }, /* Colorado USB 9600 */
169  {{ USB_DEVICE(0x0461, 0x0341) }, 0 }, /* Colorado 600u */
170  {{ USB_DEVICE(0x0461, 0x0345) }, 0 }, /* Visioneer 6200 */
171  {{ USB_DEVICE(0x0461, 0x0360) }, 0 }, /* Colorado USB 19200 */
172  {{ USB_DEVICE(0x0461, 0x0361) }, 0 }, /* Colorado 1200u */
173  {{ USB_DEVICE(0x0461, 0x0380) }, 0 }, /* G2-600 */
174  {{ USB_DEVICE(0x0461, 0x0381) }, 0 }, /* ReadyScan 636i */
175  {{ USB_DEVICE(0x0461, 0x0382) }, 0 }, /* G2-600 */
176  {{ USB_DEVICE(0x0461, 0x0383) }, 0 }, /* G2E-600 */
177   /* Epson */
178  {{ USB_DEVICE(0x04b8, 0x0101) }, 0 }, /* Perfection 636U/636Photo */
179  {{ USB_DEVICE(0x04b8, 0x0103) }, 0 }, /* Perfection 610 */
180  {{ USB_DEVICE(0x04b8, 0x0104) }, 0 }, /* Perfection 1200U/1200Photo */
181  {{ USB_DEVICE(0x04b8, 0x010b) }, 0 }, /* Perfection 1240U/1240Photo */
182  {{ USB_DEVICE(0x04b8, 0x010f) }, 0 }, /* Perfection 1250U/1250Photo */
183  {{ USB_DEVICE(0x04b8, 0x0107) }, 0 }, /* Expression 1600 */
184  {{ USB_DEVICE(0x04b8, 0x010a) }, 0 }, /* Perfection 1640SU */
185  {{ USB_DEVICE(0x04b8, 0x010c) }, 0 }, /* Perfection 640U */
186  {{ USB_DEVICE(0x04b8, 0x0110) }, 0 }, /* Perfection 1650 */
187  {{ USB_DEVICE(0x04b8, 0x011e) }, 0 }, /* Perfection 1660 */
188  {{ USB_DEVICE(0x04b8, 0x011d) }, 0 }, /* Perfection 1260 */
189  {{ USB_DEVICE(0x04b8, 0x011f) }, 0 }, /* Perfection 1670 */
190  {{ USB_DEVICE(0x04b8, 0x0120) }, 0 }, /* Perfection 1270 */
191  {{ USB_DEVICE(0x04b8, 0x080f) }, 0 }, /* Stylus Photo RX425 */
192  {{ USB_DEVICE(0x04b8, 0x011c) }, USC_KEEP_OPEN }, /* Perfection 3200 */
193  {{ USB_DEVICE(0x04b8, 0x0112) }, USC_KEEP_OPEN }, /* GT-9700F */
194  {{ USB_DEVICE(0x04b8, 0x011b) }, 0 }, /* GT-9300UF */
195  {{ USB_DEVICE(0x04b8, 0x0121) }, 0 }, /* Perfection 2480 */
196  {{ USB_DEVICE(0x04b8, 0x080e) }, USC_KEEP_OPEN }, /* CX-3500/3600/3650 MFP */
197  {{ USB_DEVICE(0x04b8, 0x0122) }, 0 }, /* Perfection 3590 */
198  {{ USB_DEVICE(0x04b8, 0x0820) }, 0 }, /* CX4200 MP */
199  {{ USB_DEVICE(0x04b8, 0x012a) }, 0 }, /* Perfection 4990 Photo */
200  {{ USB_DEVICE(0x04b8, 0x082b) }, 0 }, /* DX-50x0 MFP */
201  {{ USB_DEVICE(0x04b8, 0x082e) }, 0 }, /* DX-60x0 MFP */
202   /* UMAX */
203  {{ USB_DEVICE(0x1606, 0x0010) }, 0 }, /* Astra 1220U */
204  {{ USB_DEVICE(0x1606, 0x0002) }, 0 }, /* Astra 1236U */
205  {{ USB_DEVICE(0x1606, 0x0030) }, 0 }, /* Astra 2000U */
206  {{ USB_DEVICE(0x1606, 0x0130) }, 0 }, /* Astra 2100U */
207  {{ USB_DEVICE(0x1606, 0x0230) }, 0 }, /* Astra 2200U */
208  {{ USB_DEVICE(0x1606, 0x0060) }, 0 }, /* Astra 3400 */
209   /* Visioneer */
210  {{ USB_DEVICE(0x04a7, 0x0224) }, 0 }, /* Scanport 3000 */
211  {{ USB_DEVICE(0x04a7, 0x0221) }, 0 }, /* OneTouch 5300 */
212  {{ USB_DEVICE(0x04a7, 0x0211) }, 0 }, /* OneTouch 7600 */
213  {{ USB_DEVICE(0x04a7, 0x0231) }, 0 }, /* OneTouch 6100 */
214  {{ USB_DEVICE(0x04a7, 0x0311) }, 0 }, /* OneTouch 6200 */
215  {{ USB_DEVICE(0x04a7, 0x0321) }, 0 }, /* OneTouch 8100 */
216  {{ USB_DEVICE(0x04a7, 0x0331) }, 0 }, /* OneTouch 8600 */
217   /* Ultima */
218  {{ USB_DEVICE(0x05d8, 0x4002) }, 0 }, /* 1200 UB Plus */
219 };
220 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
221
222 #define USCANNER_BUFFERSIZE     1024
223
224 struct uscanner_softc {
225         device_t                sc_dev;         /* base device */
226         usbd_device_handle      sc_udev;
227         usbd_interface_handle   sc_iface;
228
229         u_int                   sc_dev_flags;
230
231         usbd_pipe_handle        sc_bulkin_pipe;
232         int                     sc_bulkin;
233         usbd_xfer_handle        sc_bulkin_xfer;
234         void                    *sc_bulkin_buffer;
235         int                     sc_bulkin_bufferlen;
236         int                     sc_bulkin_datalen;
237
238         usbd_pipe_handle        sc_bulkout_pipe;
239         int                     sc_bulkout;
240         usbd_xfer_handle        sc_bulkout_xfer;
241         void                    *sc_bulkout_buffer;
242         int                     sc_bulkout_bufferlen;
243         int                     sc_bulkout_datalen;
244
245         u_char                  sc_state;
246 #define USCANNER_OPEN           0x01    /* opened */
247
248         int                     sc_refcnt;
249         u_char                  sc_dying;
250 };
251
252 d_open_t  uscanneropen;
253 d_close_t uscannerclose;
254 d_read_t  uscannerread;
255 d_write_t uscannerwrite;
256 d_kqfilter_t uscannerkqfilter;
257
258 static struct dev_ops uscanner_ops = {
259         { "uscanner", 0, 0 },
260         .d_open =       uscanneropen,
261         .d_close =      uscannerclose,
262         .d_read =       uscannerread,
263         .d_write =      uscannerwrite,
264         .d_kqfilter =   uscannerkqfilter
265 };
266
267 static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
268 static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
269 static void uscanner_do_close(struct uscanner_softc *);
270
271 #define USCANNERUNIT(n) (minor(n))
272
273 static device_probe_t uscanner_match;
274 static device_attach_t uscanner_attach;
275 static device_detach_t uscanner_detach;
276
277 static devclass_t uscanner_devclass;
278
279 static kobj_method_t uscanner_methods[] = {
280         DEVMETHOD(device_probe, uscanner_match),
281         DEVMETHOD(device_attach, uscanner_attach),
282         DEVMETHOD(device_detach, uscanner_detach),
283         {0,0}
284 };
285
286 static driver_t uscanner_driver = {
287         "uscanner",
288         uscanner_methods,
289         sizeof(struct uscanner_softc)
290 };
291
292 MODULE_DEPEND(uscanner, usb, 1, 1, 1);
293
294 static int
295 uscanner_match(device_t self)
296 {
297         struct usb_attach_arg *uaa = device_get_ivars(self);
298
299         if (uaa->iface != NULL)
300                 return UMATCH_NONE;
301
302         return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
303                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
304 }
305
306 static int
307 uscanner_attach(device_t self)
308 {
309         struct uscanner_softc *sc = device_get_softc(self);
310         struct usb_attach_arg *uaa = device_get_ivars(self);
311         usb_interface_descriptor_t *id = 0;
312         usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
313         int i;
314         usbd_status err;
315
316         sc->sc_dev = self;
317
318         sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
319
320         sc->sc_udev = uaa->device;
321
322         err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
323         if (err) {
324                 kprintf("%s: setting config no failed\n",
325                     device_get_nameunit(sc->sc_dev));
326                 return ENXIO;
327         }
328
329         /* XXX We only check the first interface */
330         err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
331         if (!err && sc->sc_iface)
332             id = usbd_get_interface_descriptor(sc->sc_iface);
333         if (err || id == 0) {
334                 kprintf("%s: could not get interface descriptor, err=%d,id=%p\n",
335                        device_get_nameunit(sc->sc_dev), err, id);
336                 return ENXIO;
337         }
338
339         /* Find the two first bulk endpoints */
340         for (i = 0 ; i < id->bNumEndpoints; i++) {
341                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
342                 if (ed == 0) {
343                         kprintf("%s: could not read endpoint descriptor\n",
344                                device_get_nameunit(sc->sc_dev));
345                         return ENXIO;
346                 }
347
348                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
349                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
350                         ed_bulkin = ed;
351                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
352                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
353                         ed_bulkout = ed;
354                 }
355
356                 if (ed_bulkin && ed_bulkout)    /* found all we need */
357                         break;
358         }
359
360         /* Verify that we goething sensible */
361         if (ed_bulkin == NULL || ed_bulkout == NULL) {
362                 kprintf("%s: bulk-in and/or bulk-out endpoint not found\n",
363                         device_get_nameunit(sc->sc_dev));
364                 return ENXIO;
365         }
366
367         sc->sc_bulkin = ed_bulkin->bEndpointAddress;
368         sc->sc_bulkout = ed_bulkout->bEndpointAddress;
369
370         /* the main device, ctrl endpoint */
371         make_dev(&uscanner_ops, device_get_unit(sc->sc_dev),
372                  UID_ROOT, GID_OPERATOR, 0644,
373                  "%s", device_get_nameunit(sc->sc_dev));
374
375         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
376                            sc->sc_dev);
377
378         return 0;
379 }
380
381 int
382 uscanneropen(struct dev_open_args *ap)
383 {
384         cdev_t dev = ap->a_head.a_dev;
385         struct uscanner_softc *sc;
386         int unit = USCANNERUNIT(dev);
387         usbd_status err;
388
389         sc = devclass_get_softc(uscanner_devclass, unit);
390         if (sc == NULL)
391                 return (ENXIO);
392
393         DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
394                      ap->a_oflags, ap->a_devtype, unit));
395
396         if (sc->sc_dying)
397                 return (ENXIO);
398
399         if (sc->sc_state & USCANNER_OPEN)
400                 return (EBUSY);
401
402         sc->sc_state |= USCANNER_OPEN;
403
404         sc->sc_bulkin_buffer = kmalloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
405         sc->sc_bulkout_buffer = kmalloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
406         /* No need to check buffers for NULL since we have WAITOK */
407
408         sc->sc_bulkin_bufferlen = USCANNER_BUFFERSIZE;
409         sc->sc_bulkout_bufferlen = USCANNER_BUFFERSIZE;
410
411         /* We have decided on which endpoints to use, now open the pipes */
412         if (sc->sc_bulkin_pipe == NULL) {
413                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
414                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
415                 if (err) {
416                         kprintf("%s: cannot open bulk-in pipe (addr %d)\n",
417                                device_get_nameunit(sc->sc_dev), sc->sc_bulkin);
418                         uscanner_do_close(sc);
419                         return (EIO);
420                 }
421         }
422         if (sc->sc_bulkout_pipe == NULL) {
423                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
424                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
425                 if (err) {
426                         kprintf("%s: cannot open bulk-out pipe (addr %d)\n",
427                                device_get_nameunit(sc->sc_dev), sc->sc_bulkout);
428                         uscanner_do_close(sc);
429                         return (EIO);
430                 }
431         }
432
433         sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
434         if (sc->sc_bulkin_xfer == NULL) {
435                 uscanner_do_close(sc);
436                 return (ENOMEM);
437         }
438         sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
439         if (sc->sc_bulkout_xfer == NULL) {
440                 uscanner_do_close(sc);
441                 return (ENOMEM);
442         }
443
444         return (0);     /* success */
445 }
446
447 int
448 uscannerclose(struct dev_close_args *ap)
449 {
450         cdev_t dev = ap->a_head.a_dev;
451         struct uscanner_softc *sc;
452
453         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
454
455         DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
456                      ap->a_fflag, ap->a_devtype, USCANNERUNIT(dev)));
457
458 #ifdef DIAGNOSTIC
459         if (!(sc->sc_state & USCANNER_OPEN)) {
460                 kprintf("uscannerclose: not open\n");
461                 return (EINVAL);
462         }
463 #endif
464
465         uscanner_do_close(sc);
466
467         return (0);
468 }
469
470 void
471 uscanner_do_close(struct uscanner_softc *sc)
472 {
473         if (sc->sc_bulkin_xfer) {
474                 usbd_free_xfer(sc->sc_bulkin_xfer);
475                 sc->sc_bulkin_xfer = NULL;
476         }
477         if (sc->sc_bulkout_xfer) {
478                 usbd_free_xfer(sc->sc_bulkout_xfer);
479                 sc->sc_bulkout_xfer = NULL;
480         }
481
482         if (!(sc->sc_dev_flags & USC_KEEP_OPEN)) {
483                 if (sc->sc_bulkin_pipe != NULL) {
484                         usbd_abort_pipe(sc->sc_bulkin_pipe);
485                         usbd_close_pipe(sc->sc_bulkin_pipe);
486                         sc->sc_bulkin_pipe = NULL;
487                 }
488                 if (sc->sc_bulkout_pipe != NULL) {
489                         usbd_abort_pipe(sc->sc_bulkout_pipe);
490                         usbd_close_pipe(sc->sc_bulkout_pipe);
491                         sc->sc_bulkout_pipe = NULL;
492                 }
493         }
494
495         if (sc->sc_bulkin_buffer) {
496                 kfree(sc->sc_bulkin_buffer, M_USBDEV);
497                 sc->sc_bulkin_buffer = NULL;
498         }
499         if (sc->sc_bulkout_buffer) {
500                 kfree(sc->sc_bulkout_buffer, M_USBDEV);
501                 sc->sc_bulkout_buffer = NULL;
502         }
503
504         sc->sc_state &= ~USCANNER_OPEN;
505 }
506
507 static int
508 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
509 {
510         u_int32_t n, tn;
511         usbd_status err;
512         int error = 0;
513
514         DPRINTFN(5, ("%s: uscannerread\n", device_get_nameunit(sc->sc_dev)));
515
516         if (sc->sc_dying)
517                 return (EIO);
518
519         while ((n = szmin(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
520                 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
521                 tn = n;
522
523                 err = usbd_bulk_transfer(
524                         sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
525                         USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
526                         sc->sc_bulkin_buffer, &tn,
527                         "uscnrb");
528                 if (err) {
529                         if (err == USBD_INTERRUPTED)
530                                 error = EINTR;
531                         else if (err == USBD_TIMEOUT)
532                                 error = ETIMEDOUT;
533                         else
534                                 error = EIO;
535                         break;
536                 }
537                 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
538                 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
539                 if (error || tn < n)
540                         break;
541         }
542
543         return (error);
544 }
545
546 int
547 uscannerread(struct dev_read_args *ap)
548 {
549         cdev_t dev = ap->a_head.a_dev;
550         struct uscanner_softc *sc;
551         int error;
552
553         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
554
555         sc->sc_refcnt++;
556         error = uscanner_do_read(sc, ap->a_uio, ap->a_ioflag);
557         if (--sc->sc_refcnt < 0)
558                 usb_detach_wakeup(sc->sc_dev);
559
560         return (error);
561 }
562
563 static int
564 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
565 {
566         u_int32_t n;
567         int error = 0;
568         usbd_status err;
569
570         DPRINTFN(5, ("%s: uscanner_do_write\n", device_get_nameunit(sc->sc_dev)));
571
572         if (sc->sc_dying)
573                 return (EIO);
574
575         while ((n = szmin(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
576                 error = uiomove(sc->sc_bulkout_buffer, n, uio);
577                 if (error)
578                         break;
579                 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
580                 err = usbd_bulk_transfer(
581                         sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
582                         0, USBD_NO_TIMEOUT,
583                         sc->sc_bulkout_buffer, &n,
584                         "uscnwb");
585                 if (err) {
586                         if (err == USBD_INTERRUPTED)
587                                 error = EINTR;
588                         else
589                                 error = EIO;
590                         break;
591                 }
592         }
593
594         return (error);
595 }
596
597 int
598 uscannerwrite(struct dev_write_args *ap)
599 {
600         cdev_t dev = ap->a_head.a_dev;
601         struct uscanner_softc *sc;
602         int error;
603
604         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
605
606         sc->sc_refcnt++;
607         error = uscanner_do_write(sc, ap->a_uio, ap->a_ioflag);
608         if (--sc->sc_refcnt < 0)
609                 usb_detach_wakeup(sc->sc_dev);
610         return (error);
611 }
612
613 static int
614 uscanner_detach(device_t self)
615 {
616         struct uscanner_softc *sc = device_get_softc(self);
617
618         DPRINTF(("uscanner_detach: sc=%p\n", sc));
619
620         sc->sc_dying = 1;
621         sc->sc_dev_flags = 0;   /* make close really close device */
622
623         /* Abort all pipes.  Causes processes waiting for transfer to wake. */
624         if (sc->sc_bulkin_pipe != NULL)
625                 usbd_abort_pipe(sc->sc_bulkin_pipe);
626         if (sc->sc_bulkout_pipe != NULL)
627                 usbd_abort_pipe(sc->sc_bulkout_pipe);
628
629         crit_enter();
630         if (--sc->sc_refcnt >= 0) {
631                 /* Wait for processes to go away. */
632                 usb_detach_wait(sc->sc_dev);
633         }
634         crit_exit();
635
636         /* destroy the device for the control endpoint */
637         dev_ops_remove_minor(&uscanner_ops, /*-1, */device_get_unit(sc->sc_dev));
638
639         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
640                            sc->sc_dev);
641
642         return (0);
643 }
644
645 static void
646 uscannerfilt_detach(struct knote *kn) {}
647
648 static int
649 uscannerfilt(struct knote *kn, long hint)
650 {
651         /*
652          * We have no easy way of determining if a read will
653          * yield any data or a write will happen.
654          * Pretend they will.
655          */
656         return (1);
657 }
658
659 static struct filterops uscannerfiltops =
660         { FILTEROP_ISFD, NULL, uscannerfilt_detach, uscannerfilt };
661
662 int
663 uscannerkqfilter(struct dev_kqfilter_args *ap)
664 {
665 /* XXX
666         cdev_t dev = ap->a_head.a_dev;
667         struct uscanner_softc *sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
668
669         if (sc->sc_dying)
670                 return (EIO);
671 */
672
673         ap->a_result = 0;
674         ap->a_kn->kn_fop = &uscannerfiltops;
675         return (0);
676 }
677
678 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, NULL);
679