]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/uscanner.c
This commit was generated by cvs2svn to compensate for changes in r135471,
[FreeBSD/FreeBSD.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 #if defined(__NetBSD__) || defined(__OpenBSD__)
54 #include <sys/device.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <sys/conf.h>
59 #include <sys/fcntl.h>
60 #include <sys/filio.h>
61 #endif
62 #include <sys/tty.h>
63 #include <sys/file.h>
64 #if __FreeBSD_version >= 500014
65 #include <sys/selinfo.h>
66 #else
67 #include <sys/select.h>
68 #endif
69 #include <sys/proc.h>
70 #include <sys/vnode.h>
71 #include <sys/poll.h>
72 #include <sys/conf.h>
73 #include <sys/sysctl.h>
74
75 #include <dev/usb/usb.h>
76 #include <dev/usb/usbdi.h>
77 #include <dev/usb/usbdi_util.h>
78
79 #include "usbdevs.h"
80
81 #ifdef USB_DEBUG
82 #define DPRINTF(x)      if (uscannerdebug) logprintf x
83 #define DPRINTFN(n,x)   if (uscannerdebug>(n)) logprintf x
84 int     uscannerdebug = 0;
85 SYSCTL_NODE(_hw_usb, OID_AUTO, uscanner, CTLFLAG_RW, 0, "USB uscanner");
86 SYSCTL_INT(_hw_usb_uscanner, OID_AUTO, debug, CTLFLAG_RW,
87            &uscannerdebug, 0, "uscanner debug level");
88 #else
89 #define DPRINTF(x)
90 #define DPRINTFN(n,x)
91 #endif
92
93 struct uscan_info {
94         struct usb_devno devno;
95         u_int flags;
96 #define USC_KEEP_OPEN 1
97 };
98
99 /* Table of scanners that may work with this driver. */
100 static const struct uscan_info uscanner_devs[] = {
101   /* Acer Peripherals */
102  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_320U }, 0 },
103  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_640U }, 0 },
104  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_620U }, 0 },
105  {{ USB_VENDOR_ACERP, USB_PRODUCT_ACERP_ACERSCAN_C310U }, 0 },
106
107   /* AGFA */
108  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1236U }, 0 },
109  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U }, 0 },
110  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCAN1212U2 }, 0 },
111  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANTOUCH }, 0 },
112  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE40 }, 0 },
113  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE50 }, 0 },
114  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE20 }, 0 },
115  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE25 }, 0 },
116  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE26 }, 0 },
117  {{ USB_VENDOR_AGFA, USB_PRODUCT_AGFA_SNAPSCANE52 }, 0 },
118
119   /* Avision */
120  {{ USB_VENDOR_AVISION, USB_PRODUCT_AVISION_1200U }, 0 },
121
122   /* Canon */
123  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N656U }, 0 },
124  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N676U }, 0 },
125  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N1220U }, 0 },
126  {{ USB_VENDOR_CANON, USB_PRODUCT_CANON_N1240U }, 0 },
127
128   /* Kye */
129  {{ USB_VENDOR_KYE, USB_PRODUCT_KYE_VIVIDPRO }, 0 },
130
131   /* HP */
132  {{ USB_VENDOR_HP, USB_PRODUCT_HP_2200C }, 0 },
133  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3300C }, 0 },
134  {{ USB_VENDOR_HP, USB_PRODUCT_HP_3400CSE }, 0 },
135  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4100C }, 0 },
136  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4200C }, 0 },
137  {{ USB_VENDOR_HP, USB_PRODUCT_HP_4300C }, 0 },
138  {{ USB_VENDOR_HP, USB_PRODUCT_HP_S20 }, 0 },
139  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5200C }, 0 },
140  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5300C }, 0 },
141  {{ USB_VENDOR_HP, USB_PRODUCT_HP_5400C }, 0 },
142  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6200C }, 0 },
143  {{ USB_VENDOR_HP, USB_PRODUCT_HP_6300C }, 0 },
144
145   /* Microtek */
146  {{ USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_336CX }, 0 },
147  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_X6U }, 0 },
148  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX }, 0 },
149  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_336CX2 }, 0 },
150  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_C6 }, 0 },
151  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL }, 0 },
152  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6USL2 }, 0 },
153  {{ USB_VENDOR_MICROTEK, USB_PRODUCT_MICROTEK_V6UL }, 0 },
154
155  /* Minolta */
156  {{ USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_5400 }, 0 },
157
158   /* Mustek */
159  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CU }, 0 },
160  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200F }, 0 },
161  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_BEARPAW1200TA }, 0 },
162  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600USB }, 0 },
163  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_600CU }, 0 },
164  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USB }, 0 },
165  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200UB }, 0 },
166  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200USBPLUS }, 0 },
167  {{ USB_VENDOR_MUSTEK, USB_PRODUCT_MUSTEK_1200CUPLUS }, 0 },
168
169   /* National */
170  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW1200 }, 0 },
171  {{ USB_VENDOR_NATIONAL, USB_PRODUCT_NATIONAL_BEARPAW2400 }, 0 },
172
173   /* Nikon */
174  {{ USB_VENDOR_NIKON, USB_PRODUCT_NIKON_LS40 }, 0 },
175
176   /* Primax */
177  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2X300 }, 0 },
178  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E300 }, 0 },
179  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2300 }, 0 },
180  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E3002 }, 0 },
181  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_9600 }, 0 },
182  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_600U }, 0 },
183  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_6200 }, 0 },
184  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_19200 }, 0 },
185  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_1200U }, 0 },
186  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G600 }, 0 },
187  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_636I }, 0 },
188  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2600 }, 0 },
189  {{ USB_VENDOR_PRIMAX, USB_PRODUCT_PRIMAX_G2E600 }, 0 },
190
191   /* Epson */
192  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_636 }, 0 },
193  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_610 }, 0 },
194  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1200 }, 0 },
195  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1240 }, 0 },
196  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1250 }, 0 },
197  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1600 }, 0 },
198  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1640 }, 0 },
199  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_640U }, 0 },
200  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1650 }, 0 },
201  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1660 }, 0 },
202  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1670 }, 0 },
203  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_1260 }, 0 },
204  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_3200 }, USC_KEEP_OPEN },
205  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9700F }, USC_KEEP_OPEN },
206  {{ USB_VENDOR_EPSON, USB_PRODUCT_EPSON_GT9300UF }, 0 },
207
208   /* UMAX */
209  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1220U }, 0 },
210  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA1236U }, 0 },
211  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2000U }, 0 },
212  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2100U }, 0 },
213  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA2200U }, 0 },
214  {{ USB_VENDOR_UMAX, USB_PRODUCT_UMAX_ASTRA3400 }, 0 },
215
216   /* Visioneer */
217  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_3000 }, 0 },
218  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_5300 }, 0 },
219  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_7600 }, 0 },
220  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6100 }, 0 },
221  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_6200 }, 0 },
222  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8100 }, 0 },
223  {{ USB_VENDOR_VISIONEER, USB_PRODUCT_VISIONEER_8600 }, 0 },
224
225   /* Ultima */
226  {{ USB_VENDOR_ULTIMA, USB_PRODUCT_ULTIMA_1200UBPLUS }, 0 },
227
228 };
229 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
230
231 #define USCANNER_BUFFERSIZE     1024
232
233 struct uscanner_softc {
234         USBBASEDEVICE           sc_dev;         /* base device */
235         usbd_device_handle      sc_udev;
236         usbd_interface_handle   sc_iface;
237 #if defined(__FreeBSD__)
238         struct cdev *dev;
239 #endif
240
241         u_int                   sc_dev_flags;
242
243         usbd_pipe_handle        sc_bulkin_pipe;
244         int                     sc_bulkin;
245         usbd_xfer_handle        sc_bulkin_xfer;
246         void                    *sc_bulkin_buffer;
247         int                     sc_bulkin_bufferlen;
248         int                     sc_bulkin_datalen;
249
250         usbd_pipe_handle        sc_bulkout_pipe;
251         int                     sc_bulkout;
252         usbd_xfer_handle        sc_bulkout_xfer;
253         void                    *sc_bulkout_buffer;
254         int                     sc_bulkout_bufferlen;
255         int                     sc_bulkout_datalen;
256
257         u_char                  sc_state;
258 #define USCANNER_OPEN           0x01    /* opened */
259
260         int                     sc_refcnt;
261         u_char                  sc_dying;
262 };
263
264 #if defined(__NetBSD__) || defined(__OpenBSD__)
265 cdev_decl(uscanner);
266 #elif defined(__FreeBSD__)
267 d_open_t  uscanneropen;
268 d_close_t uscannerclose;
269 d_read_t  uscannerread;
270 d_write_t uscannerwrite;
271 d_poll_t  uscannerpoll;
272
273
274 Static struct cdevsw uscanner_cdevsw = {
275         .d_version =    D_VERSION,
276         .d_flags =      D_NEEDGIANT,
277         .d_open =       uscanneropen,
278         .d_close =      uscannerclose,
279         .d_read =       uscannerread,
280         .d_write =      uscannerwrite,
281         .d_poll =       uscannerpoll,
282         .d_name =       "uscanner",
283 #if __FreeBSD_version < 500014
284         .d_bmaj         -1
285 #endif
286 };
287 #endif
288
289 Static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
290 Static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
291 Static void uscanner_do_close(struct uscanner_softc *);
292
293 #define USCANNERUNIT(n) (minor(n))
294
295 USB_DECLARE_DRIVER(uscanner);
296
297 USB_MATCH(uscanner)
298 {
299         USB_MATCH_START(uscanner, uaa);
300
301         if (uaa->iface != NULL)
302                 return UMATCH_NONE;
303
304         return (uscanner_lookup(uaa->vendor, uaa->product) != NULL ?
305                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
306 }
307
308 USB_ATTACH(uscanner)
309 {
310         USB_ATTACH_START(uscanner, sc, uaa);
311         usb_interface_descriptor_t *id = 0;
312         usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
313         char devinfo[1024];
314         int i;
315         usbd_status err;
316
317         usbd_devinfo(uaa->device, 0, devinfo);
318         USB_ATTACH_SETUP;
319
320         sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
321
322         sc->sc_udev = uaa->device;
323
324         err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
325         if (err) {
326                 printf("%s: setting config no failed\n",
327                     USBDEVNAME(sc->sc_dev));
328                 USB_ATTACH_ERROR_RETURN;
329         }
330
331         /* XXX We only check the first interface */
332         err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
333         if (!err && sc->sc_iface)
334             id = usbd_get_interface_descriptor(sc->sc_iface);
335         if (err || id == 0) {
336                 printf("%s: could not get interface descriptor, err=%d,id=%p\n",
337                        USBDEVNAME(sc->sc_dev), err, id);
338                 USB_ATTACH_ERROR_RETURN;
339         }
340
341         /* Find the two first bulk endpoints */
342         for (i = 0 ; i < id->bNumEndpoints; i++) {
343                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
344                 if (ed == 0) {
345                         printf("%s: could not read endpoint descriptor\n",
346                                USBDEVNAME(sc->sc_dev));
347                         USB_ATTACH_ERROR_RETURN;
348                 }
349
350                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
351                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
352                         ed_bulkin = ed;
353                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
354                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
355                         ed_bulkout = ed;
356                 }
357
358                 if (ed_bulkin && ed_bulkout)    /* found all we need */
359                         break;
360         }
361
362         /* Verify that we goething sensible */
363         if (ed_bulkin == NULL || ed_bulkout == NULL) {
364                 printf("%s: bulk-in and/or bulk-out endpoint not found\n",
365                         USBDEVNAME(sc->sc_dev));
366                 USB_ATTACH_ERROR_RETURN;
367         }
368
369         sc->sc_bulkin = ed_bulkin->bEndpointAddress;
370         sc->sc_bulkout = ed_bulkout->bEndpointAddress;
371
372 #ifdef __FreeBSD__
373         /* the main device, ctrl endpoint */
374         sc->dev = make_dev(&uscanner_cdevsw, USBDEVUNIT(sc->sc_dev),
375                 UID_ROOT, GID_OPERATOR, 0644, "%s", USBDEVNAME(sc->sc_dev));
376 #endif
377
378         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
379                            USBDEV(sc->sc_dev));
380
381         USB_ATTACH_SUCCESS_RETURN;
382 }
383
384 int
385 uscanneropen(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
386 {
387         struct uscanner_softc *sc;
388         int unit = USCANNERUNIT(dev);
389         usbd_status err;
390
391         USB_GET_SC_OPEN(uscanner, unit, sc);
392
393         DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
394                      flag, mode, 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 = malloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
405         sc->sc_bulkout_buffer = malloc(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                         printf("%s: cannot open bulk-in pipe (addr %d)\n",
417                                USBDEVNAME(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                         printf("%s: cannot open bulk-out pipe (addr %d)\n",
427                                USBDEVNAME(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 cdev *dev, int flag, int mode, usb_proc_ptr p)
449 {
450         struct uscanner_softc *sc;
451
452         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
453
454         DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
455                      flag, mode, USCANNERUNIT(dev)));
456
457 #ifdef DIAGNOSTIC
458         if (!(sc->sc_state & USCANNER_OPEN)) {
459                 printf("uscannerclose: not open\n");
460                 return (EINVAL);
461         }
462 #endif
463
464         uscanner_do_close(sc);
465
466         return (0);
467 }
468
469 void
470 uscanner_do_close(struct uscanner_softc *sc)
471 {
472         if (sc->sc_bulkin_xfer) {
473                 usbd_free_xfer(sc->sc_bulkin_xfer);
474                 sc->sc_bulkin_xfer = NULL;
475         }
476         if (sc->sc_bulkout_xfer) {
477                 usbd_free_xfer(sc->sc_bulkout_xfer);
478                 sc->sc_bulkout_xfer = NULL;
479         }
480
481         if (!(sc->sc_dev_flags & USC_KEEP_OPEN)) {
482                 if (sc->sc_bulkin_pipe != NULL) {
483                         usbd_abort_pipe(sc->sc_bulkin_pipe);
484                         usbd_close_pipe(sc->sc_bulkin_pipe);
485                         sc->sc_bulkin_pipe = NULL;
486                 }
487                 if (sc->sc_bulkout_pipe != NULL) {
488                         usbd_abort_pipe(sc->sc_bulkout_pipe);
489                         usbd_close_pipe(sc->sc_bulkout_pipe);
490                         sc->sc_bulkout_pipe = NULL;
491                 }
492         }
493
494         if (sc->sc_bulkin_buffer) {
495                 free(sc->sc_bulkin_buffer, M_USBDEV);
496                 sc->sc_bulkin_buffer = NULL;
497         }
498         if (sc->sc_bulkout_buffer) {
499                 free(sc->sc_bulkout_buffer, M_USBDEV);
500                 sc->sc_bulkout_buffer = NULL;
501         }
502
503         sc->sc_state &= ~USCANNER_OPEN;
504 }
505
506 Static int
507 uscanner_do_read(struct uscanner_softc *sc, struct uio *uio, int flag)
508 {
509         u_int32_t n, tn;
510         usbd_status err;
511         int error = 0;
512
513         DPRINTFN(5, ("%s: uscannerread\n", USBDEVNAME(sc->sc_dev)));
514
515         if (sc->sc_dying)
516                 return (EIO);
517
518         while ((n = min(sc->sc_bulkin_bufferlen, uio->uio_resid)) != 0) {
519                 DPRINTFN(1, ("uscannerread: start transfer %d bytes\n",n));
520                 tn = n;
521
522                 err = usbd_bulk_transfer(
523                         sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
524                         USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
525                         sc->sc_bulkin_buffer, &tn,
526                         "uscnrb");
527                 if (err) {
528                         if (err == USBD_INTERRUPTED)
529                                 error = EINTR;
530                         else if (err == USBD_TIMEOUT)
531                                 error = ETIMEDOUT;
532                         else
533                                 error = EIO;
534                         break;
535                 }
536                 DPRINTFN(1, ("uscannerread: got %d bytes\n", tn));
537                 error = uiomove(sc->sc_bulkin_buffer, tn, uio);
538                 if (error || tn < n)
539                         break;
540         }
541
542         return (error);
543 }
544
545 int
546 uscannerread(struct cdev *dev, struct uio *uio, int flag)
547 {
548         struct uscanner_softc *sc;
549         int error;
550
551         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
552
553         sc->sc_refcnt++;
554         error = uscanner_do_read(sc, uio, flag);
555         if (--sc->sc_refcnt < 0)
556                 usb_detach_wakeup(USBDEV(sc->sc_dev));
557
558         return (error);
559 }
560
561 Static int
562 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
563 {
564         u_int32_t n;
565         int error = 0;
566         usbd_status err;
567
568         DPRINTFN(5, ("%s: uscanner_do_write\n", USBDEVNAME(sc->sc_dev)));
569
570         if (sc->sc_dying)
571                 return (EIO);
572
573         while ((n = min(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
574                 error = uiomove(sc->sc_bulkout_buffer, n, uio);
575                 if (error)
576                         break;
577                 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
578                 err = usbd_bulk_transfer(
579                         sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
580                         0, USBD_NO_TIMEOUT,
581                         sc->sc_bulkout_buffer, &n,
582                         "uscnwb");
583                 if (err) {
584                         if (err == USBD_INTERRUPTED)
585                                 error = EINTR;
586                         else
587                                 error = EIO;
588                         break;
589                 }
590         }
591
592         return (error);
593 }
594
595 int
596 uscannerwrite(struct cdev *dev, struct uio *uio, int flag)
597 {
598         struct uscanner_softc *sc;
599         int error;
600
601         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
602
603         sc->sc_refcnt++;
604         error = uscanner_do_write(sc, uio, flag);
605         if (--sc->sc_refcnt < 0)
606                 usb_detach_wakeup(USBDEV(sc->sc_dev));
607         return (error);
608 }
609
610 #if defined(__NetBSD__) || defined(__OpenBSD__)
611 int
612 uscanner_activate(device_ptr_t self, enum devact act)
613 {
614         struct uscanner_softc *sc = (struct uscanner_softc *)self;
615
616         switch (act) {
617         case DVACT_ACTIVATE:
618                 return (EOPNOTSUPP);
619
620         case DVACT_DEACTIVATE:
621                 sc->sc_dying = 1;
622                 break;
623         }
624         return (0);
625 }
626 #endif
627
628 USB_DETACH(uscanner)
629 {
630         USB_DETACH_START(uscanner, sc);
631         int s;
632 #if defined(__NetBSD__) || defined(__OpenBSD__)
633         int maj, mn;
634 #endif
635
636 #if defined(__NetBSD__) || defined(__OpenBSD__)
637         DPRINTF(("uscanner_detach: sc=%p flags=%d\n", sc, flags));
638 #elif defined(__FreeBSD__)
639         DPRINTF(("uscanner_detach: sc=%p\n", sc));
640 #endif
641
642         sc->sc_dying = 1;
643         sc->sc_dev_flags = 0;   /* make close really close device */
644
645         /* Abort all pipes.  Causes processes waiting for transfer to wake. */
646         if (sc->sc_bulkin_pipe != NULL)
647                 usbd_abort_pipe(sc->sc_bulkin_pipe);
648         if (sc->sc_bulkout_pipe != NULL)
649                 usbd_abort_pipe(sc->sc_bulkout_pipe);
650
651         s = splusb();
652         if (--sc->sc_refcnt >= 0) {
653                 /* Wait for processes to go away. */
654                 usb_detach_wait(USBDEV(sc->sc_dev));
655         }
656         splx(s);
657
658 #if defined(__NetBSD__) || defined(__OpenBSD__)
659         /* locate the major number */
660         for (maj = 0; maj < nchrdev; maj++)
661                 if (cdevsw[maj].d_open == uscanneropen)
662                         break;
663
664         /* Nuke the vnodes for any open instances (calls close). */
665         mn = self->dv_unit * USB_MAX_ENDPOINTS;
666         vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
667 #elif defined(__FreeBSD__)
668         /* destroy the device for the control endpoint */
669         destroy_dev(sc->dev);
670 #endif
671
672         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
673                            USBDEV(sc->sc_dev));
674
675         return (0);
676 }
677
678 int
679 uscannerpoll(struct cdev *dev, int events, usb_proc_ptr p)
680 {
681         struct uscanner_softc *sc;
682         int revents = 0;
683
684         USB_GET_SC(uscanner, USCANNERUNIT(dev), sc);
685
686         if (sc->sc_dying)
687                 return (EIO);
688
689         /*
690          * We have no easy way of determining if a read will
691          * yield any data or a write will happen.
692          * Pretend they will.
693          */
694         revents |= events &
695                    (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
696
697         return (revents);
698 }
699
700 #if defined(__FreeBSD__)
701 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, 0);
702 #endif