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