Merge from vendor branch OPENSSH:
[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.10 2005/06/02 20:41:05 dillon 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 #if defined(__NetBSD__) || defined(__OpenBSD__)
55 #include <sys/device.h>
56 #elif defined(__FreeBSD__) || defined(__DragonFly__)
57 #include <sys/module.h>
58 #include <sys/bus.h>
59 #include <sys/conf.h>
60 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #endif
63 #include <sys/tty.h>
64 #include <sys/file.h>
65 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
66 #include <sys/selinfo.h>
67 #else
68 #include <sys/select.h>
69 #endif
70 #include <sys/proc.h>
71 #include <sys/vnode.h>
72 #include <sys/poll.h>
73 #include <sys/conf.h>
74 #include <sys/sysctl.h>
75 #include <sys/thread2.h>
76
77 #include <bus/usb/usb.h>
78 #include <bus/usb/usbdi.h>
79 #include <bus/usb/usbdi_util.h>
80
81 #include <bus/usb/usbdevs.h>
82
83 #ifdef USB_DEBUG
84 #define DPRINTF(x)      if (uscannerdebug) logprintf x
85 #define DPRINTFN(n,x)   if (uscannerdebug>(n)) logprintf x
86 int     uscannerdebug = 0;
87 SYSCTL_NODE(_hw_usb, OID_AUTO, uscanner, CTLFLAG_RW, 0, "USB uscanner");
88 SYSCTL_INT(_hw_usb_uscanner, OID_AUTO, debug, CTLFLAG_RW,
89            &uscannerdebug, 0, "uscanner debug level");
90 #else
91 #define DPRINTF(x)
92 #define DPRINTFN(n,x)
93 #endif
94
95 struct uscan_info {
96         struct usb_devno devno;
97         u_int flags;
98 #define USC_KEEP_OPEN 1
99 };
100
101 /* Table of scanners that may work with this driver. */
102 static const struct uscan_info uscanner_devs[] = {
103   /* Acer Peripherals */
104  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_320U }, 0 },
105  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_640U }, 0 },
106  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_620U }, 0 },
107  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_C310U }, 0 },
108
109   /* AGFA */
110  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1236U }, 0 },
111  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U }, 0 },
112  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U2 }, 0 },
113  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANTOUCH }, 0 },
114  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE40 }, 0 },
115  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE50 }, 0 },
116  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE20 }, 0 },
117  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE25 }, 0 },
118  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE26 }, 0 },
119  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE52 }, 0 },
120
121   /* Avision */
122  {{ USB_VENDOR_AVISION, USB_PRODUCT_AVISION_1200U }, 0 },
123
124   /* Canon */
125  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N656U }, 0 },
126
127   /* Kye */
128  {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
129
130   /* HP */
131  {{ USB_VENDOR_HP, USB_PRODUCT_HP_2200C }, 0 },
132  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3300C }, 0 },
133  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3400CSE }, 0 },
134  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4100C }, 0 },
135  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4200C }, 0 },
136  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4300C }, 0 },
137  {{ USB_VENDOR_HP, USB_PRODUCT_HP_S20 }, 0 },
138  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5200C }, 0 },
139  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5300C }, 0 },
140  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5400C }, 0 },
141  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6200C }, 0 },
142  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6300C }, 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   /* Primax */
173  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2X300 }, 0 },
174  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E300 }, 0 },
175  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2300 }, 0 },
176  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E3002 }, 0 },
177  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_9600 }, 0 },
178  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_600U }, 0 },
179  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_6200 }, 0 },
180  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_19200 }, 0 },
181  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_1200U }, 0 },
182  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G600 }, 0 },
183  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_636I }, 0 },
184  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2600 }, 0 },
185  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E600 }, 0 },
186
187   /* Epson */
188  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_636 }, 0 },
189  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_610 }, 0 },
190  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1200 }, 0 },
191  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1240 }, 0 },
192  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1250 }, 0 },
193  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1600 }, 0 },
194  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1640 }, 0 },
195  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_640U }, 0 },
196  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1650 }, 0 },
197  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1660 }, 0 },
198  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1260 }, 0 },
199  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9700F }, USC_KEEP_OPEN },
200  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9300UF }, 0 },
201
202   /* UMAX */
203  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1220U }, 0 },
204  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1236U }, 0 },
205  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2000U }, 0 },
206  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2100U }, 0 },
207  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2200U }, 0 },
208  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA3400 }, 0 },
209
210   /* Visioneer */
211  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_3000 }, 0 },
212  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_5300 }, 0 },
213  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_7600 }, 0 },
214  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6100 }, 0 },
215  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6200 }, 0 },
216  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8100 }, 0 },
217  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8600 }, 0 },
218
219   /* Ultima */
220  {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
221
222 };
223 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
224
225 #define USCANNER_BUFFERSIZE     1024
226
227 struct uscanner_softc {
228         USBBASEDEVICE           sc_dev;         /* base device */
229         usbd_device_handle      sc_udev;
230         usbd_interface_handle   sc_iface;
231
232         u_int                   sc_dev_flags;
233
234         usbd_pipe_handle        sc_bulkin_pipe;
235         int                     sc_bulkin;
236         usbd_xfer_handle        sc_bulkin_xfer;
237         void                    *sc_bulkin_buffer;
238         int                     sc_bulkin_bufferlen;
239         int                     sc_bulkin_datalen;
240
241         usbd_pipe_handle        sc_bulkout_pipe;
242         int                     sc_bulkout;
243         usbd_xfer_handle        sc_bulkout_xfer;
244         void                    *sc_bulkout_buffer;
245         int                     sc_bulkout_bufferlen;
246         int                     sc_bulkout_datalen;
247
248         u_char                  sc_state;
249 #define USCANNER_OPEN           0x01    /* opened */
250
251         int                     sc_refcnt;
252         u_char                  sc_dying;
253 };
254
255 #if defined(__NetBSD__) || defined(__OpenBSD__)
256 cdev_decl(uscanner);
257 #elif defined(__FreeBSD__) || defined(__DragonFly__)
258 d_open_t  uscanneropen;
259 d_close_t uscannerclose;
260 d_read_t  uscannerread;
261 d_write_t uscannerwrite;
262 d_poll_t  uscannerpoll;
263
264 #define USCANNER_CDEV_MAJOR     156
265
266 Static struct cdevsw uscanner_cdevsw = {
267         /* name */      "uscanner",
268         /* maj */       USCANNER_CDEV_MAJOR,
269         /* flags */     0,
270         /* port */      NULL,
271         /* clone */     NULL,
272
273         /* open */      uscanneropen,
274         /* close */     uscannerclose,
275         /* read */      uscannerread,
276         /* write */     uscannerwrite,
277         /* ioctl */     noioctl,
278         /* poll */      uscannerpoll,
279         /* mmap */      nommap,
280         /* strategy */  nostrategy,
281         /* dump */      nodump,
282         /* psize */     nopsize
283 };
284 #endif
285
286 Static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
287 Static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
288 Static void uscanner_do_close(struct uscanner_softc *);
289
290 #define USCANNERUNIT(n) (minor(n))
291
292 USB_DECLARE_DRIVER(uscanner);
293
294 USB_MATCH(uscanner)
295 {
296         USB_MATCH_START(uscanner, uaa);
297
298         if (uaa->iface != NULL)
299                 return UMATCH_NONE;
300
301         return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
302                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
303 }
304
305 USB_ATTACH(uscanner)
306 {
307         USB_ATTACH_START(uscanner, sc, uaa);
308         usb_interface_descriptor_t *id = 0;
309         usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
310         char devinfo[1024];
311         int i;
312         usbd_status err;
313
314         usbd_devinfo(uaa->device, 0, devinfo);
315         USB_ATTACH_SETUP;
316         printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
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                 printf("%s: setting config no failed\n",
325                     USBDEVNAME(sc->sc_dev));
326                 USB_ATTACH_ERROR_RETURN;
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                 printf("%s: could not get interface descriptor, err=%d,id=%p\n",
335                        USBDEVNAME(sc->sc_dev), err, id);
336                 USB_ATTACH_ERROR_RETURN;
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                         printf("%s: could not read endpoint descriptor\n",
344                                USBDEVNAME(sc->sc_dev));
345                         USB_ATTACH_ERROR_RETURN;
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                 printf("%s: bulk-in and/or bulk-out endpoint not found\n",
363                         USBDEVNAME(sc->sc_dev));
364                 USB_ATTACH_ERROR_RETURN;
365         }
366
367         sc->sc_bulkin = ed_bulkin->bEndpointAddress;
368         sc->sc_bulkout = ed_bulkout->bEndpointAddress;
369
370 #if defined(__FreeBSD__) || defined(__DragonFly__)
371         /* the main device, ctrl endpoint */
372         cdevsw_add(&uscanner_cdevsw, -1, USBDEVUNIT(sc->sc_dev));
373         make_dev(&uscanner_cdevsw, USBDEVUNIT(sc->sc_dev),
374                 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
375 #endif
376
377         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
378                            USBDEV(sc->sc_dev));
379
380         USB_ATTACH_SUCCESS_RETURN;
381 }
382
383 int
384 uscanneropen(dev_t dev, int flag, int mode, usb_proc_ptr p)
385 {
386         struct uscanner_softc *sc;
387         int unit = USCANNERUNIT(dev);
388         usbd_status err;
389
390         USB_GET_SC_OPEN(uscanner, unit, sc);
391
392         DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
393                      flag, mode, unit));
394
395         if (sc->sc_dying)
396                 return (ENXIO);
397
398         if (sc->sc_state & USCANNER_OPEN)
399                 return (EBUSY);
400
401         sc->sc_state |= USCANNER_OPEN;
402
403         sc->sc_bulkin_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
404         sc->sc_bulkout_buffer = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
405         /* No need to check buffers for NULL since we have WAITOK */
406
407         sc->sc_bulkin_bufferlen = USCANNER_BUFFERSIZE;
408         sc->sc_bulkout_bufferlen = USCANNER_BUFFERSIZE;
409
410         /* We have decided on which endpoints to use, now open the pipes */
411         if (sc->sc_bulkin_pipe == NULL) {
412                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
413                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
414                 if (err) {
415                         printf("%s: cannot open bulk-in pipe (addr %d)\n",
416                                USBDEVNAME(sc->sc_dev), sc->sc_bulkin);
417                         uscanner_do_close(sc);
418                         return (EIO);
419                 }
420         }
421         if (sc->sc_bulkout_pipe == NULL) {
422                 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
423                                      USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
424                 if (err) {
425                         printf("%s: cannot open bulk-out pipe (addr %d)\n",
426                                USBDEVNAME(sc->sc_dev), sc->sc_bulkout);
427                         uscanner_do_close(sc);
428                         return (EIO);
429                 }
430         }
431
432         sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
433         if (sc->sc_bulkin_xfer == NULL) {
434                 uscanner_do_close(sc);
435                 return (ENOMEM);
436         }
437         sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
438         if (sc->sc_bulkout_xfer == NULL) {
439                 uscanner_do_close(sc);
440                 return (ENOMEM);
441         }
442
443         return (0);     /* success */
444 }
445
446 int
447 uscannerclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
448 {
449         struct uscanner_softc *sc;
450
451         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
452
453         DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
454                      flag, mode, USCANNERUNIT(dev)));
455
456 #ifdef DIAGNOSTIC
457         if (!(sc->sc_state & USCANNER_OPEN)) {
458                 printf("uscannerclose: not open\n");
459                 return (EINVAL);
460         }
461 #endif
462
463         uscanner_do_close(sc);
464
465         return (0);
466 }
467
468 void
469 uscanner_do_close(struct uscanner_softc *sc)
470 {
471         if (sc->sc_bulkin_xfer) {
472                 usbd_free_xfer(sc->sc_bulkin_xfer);
473                 sc->sc_bulkin_xfer = NULL;
474         }
475         if (sc->sc_bulkout_xfer) {
476                 usbd_free_xfer(sc->sc_bulkout_xfer);
477                 sc->sc_bulkout_xfer = NULL;
478         }
479
480         if (!(sc->sc_dev_flags & USC_KEEP_OPEN)) {
481                 if (sc->sc_bulkin_pipe != NULL) {
482                         usbd_abort_pipe(sc->sc_bulkin_pipe);
483                         usbd_close_pipe(sc->sc_bulkin_pipe);
484                         sc->sc_bulkin_pipe = NULL;
485                 }
486                 if (sc->sc_bulkout_pipe != NULL) {
487                         usbd_abort_pipe(sc->sc_bulkout_pipe);
488                         usbd_close_pipe(sc->sc_bulkout_pipe);
489                         sc->sc_bulkout_pipe = NULL;
490                 }
491         }
492
493         if (sc->sc_bulkin_buffer) {
494                 free(sc->sc_bulkin_buffer, M_USBDEV);
495                 sc->sc_bulkin_buffer = NULL;
496         }
497         if (sc->sc_bulkout_buffer) {
498                 free(sc->sc_bulkout_buffer, M_USBDEV);
499                 sc->sc_bulkout_buffer = NULL;
500         }
501
502         sc->sc_state &= ~USCANNER_OPEN;
503 }
504
505 Static int
506 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
507 {
508         u_int32_t n, tn;
509         usbd_status err;
510         int error = 0;
511
512         DPRINTFN(5, ("%s: uscannerread\n", USBDEVNAME(sc->sc_dev)));
513
514         if (sc->sc_dying)
515                 return (EIO);
516
517         while ((n = min(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
518                 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
519                 tn = n;
520
521                 err = usbd_bulk_transfer(
522                         sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
523                         USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
524                         sc->sc_bulkin_buffer, &tn,
525                         "uscnrb");
526                 if (err) {
527                         if (err == USBD_INTERRUPTED)
528                                 error = EINTR;
529                         else if (err == USBD_TIMEOUT)
530                                 error = ETIMEDOUT;
531                         else
532                                 error = EIO;
533                         break;
534                 }
535                 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
536                 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
537                 if (error || tn < n)
538                         break;
539         }
540
541         return (error);
542 }
543
544 int
545 uscannerread(dev_t dev, struct uio *uio, int flag)
546 {
547         struct uscanner_softc *sc;
548         int error;
549
550         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
551
552         sc->sc_refcnt++;
553         error = uscanner_do_read(sc, uio, flag);
554         if (--sc->sc_refcnt < 0)
555                 usb_detach_wakeup(USBDEV(sc->sc_dev));
556
557         return (error);
558 }
559
560 Static int
561 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
562 {
563         u_int32_t n;
564         int error = 0;
565         usbd_status err;
566
567         DPRINTFN(5, ("%s: uscanner_do_write\n", USBDEVNAME(sc->sc_dev)));
568
569         if (sc->sc_dying)
570                 return (EIO);
571
572         while ((n = min(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
573                 error = uiomove(sc->sc_bulkout_buffer, n, uio);
574                 if (error)
575                         break;
576                 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
577                 err = usbd_bulk_transfer(
578                         sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
579                         0, USBD_NO_TIMEOUT,
580                         sc->sc_bulkout_buffer, &n,
581                         "uscnwb");
582                 if (err) {
583                         if (err == USBD_INTERRUPTED)
584                                 error = EINTR;
585                         else
586                                 error = EIO;
587                         break;
588                 }
589         }
590
591         return (error);
592 }
593
594 int
595 uscannerwrite(dev_t dev, struct uio *uio, int flag)
596 {
597         struct uscanner_softc *sc;
598         int error;
599
600         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
601
602         sc->sc_refcnt++;
603         error = uscanner_do_write(sc, uio, flag);
604         if (--sc->sc_refcnt < 0)
605                 usb_detach_wakeup(USBDEV(sc->sc_dev));
606         return (error);
607 }
608
609 #if defined(__NetBSD__) || defined(__OpenBSD__)
610 int
611 uscanner_activate(device_ptr_t self, enum devact act)
612 {
613         struct uscanner_softc *sc = (struct uscanner_softc *)self;
614
615         switch (act) {
616         case DVACT_ACTIVATE:
617                 return (EOPNOTSUPP);
618
619         case DVACT_DEACTIVATE:
620                 sc->sc_dying = 1;
621                 break;
622         }
623         return (0);
624 }
625 #endif
626
627 USB_DETACH(uscanner)
628 {
629         USB_DETACH_START(uscanner, sc);
630 #if defined(__NetBSD__) || defined(__OpenBSD__)
631         int maj, mn;
632 #endif
633
634 #if defined(__NetBSD__) || defined(__OpenBSD__)
635         DPRINTF(("uscanner_detach: sc=%p flags=%d\n", sc, flags));
636 #elif defined(__FreeBSD__) || defined(__DragonFly__)
637         DPRINTF(("uscanner_detach: sc=%p\n", sc));
638 #endif
639
640         sc->sc_dying = 1;
641         sc->sc_dev_flags = 0;   /* make close really close device */
642
643         /* Abort all pipes.  Causes processes waiting for transfer to wake. */
644         if (sc->sc_bulkin_pipe != NULL)
645                 usbd_abort_pipe(sc->sc_bulkin_pipe);
646         if (sc->sc_bulkout_pipe != NULL)
647                 usbd_abort_pipe(sc->sc_bulkout_pipe);
648
649         crit_enter();
650         if (--sc->sc_refcnt >= 0) {
651                 /* Wait for processes to go away. */
652                 usb_detach_wait(USBDEV(sc->sc_dev));
653         }
654         crit_exit();
655
656 #if defined(__NetBSD__) || defined(__OpenBSD__)
657         /* locate the major number */
658         for (maj = 0; maj < nchrdev; maj++)
659                 if (cdevsw[maj].d_open == uscanneropen)
660                         break;
661
662         /* Nuke the vnodes for any open instances (calls close). */
663         mn = self->dv_unit * USB_MAX_ENDPOINTS;
664         vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
665 #elif defined(__FreeBSD__) || defined(__DragonFly__)
666         /* destroy the device for the control endpoint */
667         cdevsw_remove(&uscanner_cdevsw, -1, USBDEVUNIT(sc->sc_dev));
668 #endif
669
670         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
671                            USBDEV(sc->sc_dev));
672
673         return (0);
674 }
675
676 int
677 uscannerpoll(dev_t dev, int events, usb_proc_ptr p)
678 {
679         struct uscanner_softc *sc;
680         int revents = 0;
681
682         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
683
684         if (sc->sc_dying)
685                 return (EIO);
686
687         /*
688          * We have no easy way of determining if a read will
689          * yield any data or a write will happen.
690          * Pretend they will.
691          */
692         revents |= events &
693                    (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
694
695         return (revents);
696 }
697
698 #if defined(__FreeBSD__) || defined(__DragonFly__)
699 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);
700 #endif