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