The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/usbmisc/uscanner/uscanner.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* 
    2  * $NetBSD: uscanner.c,v 1.30 2002/07/11 21:14:36 augustss Exp $
    3  * $FreeBSD: src/sys/dev/usb/uscanner.c,v 1.48 2003/12/22 19:58:27 sanpei Exp $
    4  */
    5 
    6 /* Also already merged from NetBSD:
    7  *      $NetBSD: uscanner.c,v 1.33 2002/09/23 05:51:24 simonb Exp $
    8  */
    9 
   10 /*
   11  * Copyright (c) 2000 The NetBSD Foundation, Inc.
   12  * All rights reserved.
   13  *
   14  * This code is derived from software contributed to The NetBSD Foundation
   15  * by Lennart Augustsson (lennart@augustsson.net) at
   16  * Carlstedt Research & Technology
   17  * and Nick Hibma (n_hibma@qubesoft.com).
   18  *
   19  * Redistribution and use in source and binary forms, with or without
   20  * modification, are permitted provided that the following conditions
   21  * are met:
   22  * 1. Redistributions of source code must retain the above copyright
   23  *    notice, this list of conditions and the following disclaimer.
   24  * 2. Redistributions in binary form must reproduce the above copyright
   25  *    notice, this list of conditions and the following disclaimer in the
   26  *    documentation and/or other materials provided with the distribution.
   27  * 3. All advertising materials mentioning features or use of this software
   28  *    must display the following acknowledgement:
   29  *        This product includes software developed by the NetBSD
   30  *        Foundation, Inc. and its contributors.
   31  * 4. Neither the name of The NetBSD Foundation nor the names of its
   32  *    contributors may be used to endorse or promote products derived
   33  *    from this software without specific prior written permission.
   34  *
   35  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   36  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   37  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   40  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   41  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   42  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   43  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   44  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   45  * POSSIBILITY OF SUCH DAMAGE.
   46  */
   47 
   48 
   49 #include <sys/param.h>
   50 #include <sys/systm.h>
   51 #include <sys/kernel.h>
   52 #include <sys/malloc.h>
   53 #include <sys/module.h>
   54 #include <sys/bus.h>
   55 #include <sys/conf.h>
   56 #include <sys/fcntl.h>
   57 #include <sys/filio.h>
   58 #include <sys/tty.h>
   59 #include <sys/file.h>
   60 #include <sys/select.h>
   61 #include <sys/proc.h>
   62 #include <sys/event.h>
   63 #include <sys/sysctl.h>
   64 #include <sys/thread2.h>
   65 
   66 #include <bus/usb/usb.h>
   67 #include <bus/usb/usbdi.h>
   68 #include <bus/usb/usbdi_util.h>
   69 
   70 #ifdef USB_DEBUG
   71 #define DPRINTF(x)      if (uscannerdebug) kprintf x
   72 #define DPRINTFN(n,x)   if (uscannerdebug>(n)) kprintf x
   73 int     uscannerdebug = 0;
   74 SYSCTL_NODE(_hw_usb, OID_AUTO, uscanner, CTLFLAG_RW, 0, "USB uscanner");
   75 SYSCTL_INT(_hw_usb_uscanner, OID_AUTO, debug, CTLFLAG_RW,
   76            &uscannerdebug, 0, "uscanner debug level");
   77 #else
   78 #define DPRINTF(x)
   79 #define DPRINTFN(n,x)
   80 #endif
   81 
   82 struct uscan_info {
   83         struct usb_devno devno;
   84         u_int flags;
   85 #define USC_KEEP_OPEN 1
   86 };
   87 
   88 /* Table of scanners that may work with this driver. */
   89 static const struct uscan_info uscanner_devs[] = {
   90   /* Acer Peripherals */
   91  {{ USB_DEVICE(0x04a5, 0x2022) }, 0 }, /* Acerscan 320U */
   92  {{ USB_DEVICE(0x04a5, 0x20b0) }, 0 }, /* Benq 3300U/4300U */
   93  {{ USB_DEVICE(0x04a5, 0x2040) }, 0 }, /* Acerscan 640U */
   94  {{ USB_DEVICE(0x04a5, 0x20be) }, 0 }, /* Acerscan 640BT */
   95  {{ USB_DEVICE(0x04a5, 0x2060) }, 0 }, /* Acerscan 620U */
   96  {{ USB_DEVICE(0x04a5, 0x20c0) }, 0 }, /* Acerscan 1240U */
   97  {{ USB_DEVICE(0x04a5, 0x12a6) }, 0 }, /* Acerscan C310U */
   98   /* AGFA */
   99  {{ USB_DEVICE(0x06bd, 0x0002) }, 0 }, /* SnapScan 1236U */
  100  {{ USB_DEVICE(0x06bd, 0x0001) }, 0 }, /* SnapScan 1212U */
  101  {{ USB_DEVICE(0x06bd, 0x2061) }, 0 }, /* SnapScan 1212U */
  102  {{ USB_DEVICE(0x06bd, 0x0100) }, 0 }, /* SnapScan Touch */
  103  {{ USB_DEVICE(0x06bd, 0x208d) }, 0 }, /* SnapScan e40 */
  104  {{ USB_DEVICE(0x06bd, 0x208f) }, 0 }, /* SnapScan e50 */
  105  {{ USB_DEVICE(0x06bd, 0x2091) }, 0 }, /* SnapScan e20 */
  106  {{ USB_DEVICE(0x06bd, 0x2095) }, 0 }, /* SnapScan e25 */
  107  {{ USB_DEVICE(0x06bd, 0x2097) }, 0 }, /* SnapScan e26 */
  108  {{ USB_DEVICE(0x06bd, 0x20fd) }, 0 }, /* SnapScan e52 */
  109   /* Avision */
  110  {{ USB_DEVICE(0x0638, 0x0268) }, 0 }, /* Avision 1200U */
  111   /* Canon */
  112  {{ USB_DEVICE(0x04a9, 0x2206) }, 0 }, /* CanoScan N656U */
  113  {{ USB_DEVICE(0x04a9, 0x220d) }, 0 }, /* CanoScan N676U */
  114  {{ USB_DEVICE(0x04a9, 0x2207) }, 0 }, /* CanoScan N1220U */
  115  {{ USB_DEVICE(0x04a9, 0x2208) }, 0 }, /* CanoScan D660U */
  116  {{ USB_DEVICE(0x04a9, 0x220e) }, 0 }, /* CanoScan N1240U */
  117  {{ USB_DEVICE(0x04a9, 0x2220) }, 0 }, /* CanoScan LIDE 25 */
  118   /* Kye */
  119  {{ USB_DEVICE(0x0458, 0x2001) }, 0 }, /* ColorPage Vivid-Pro */
  120   /* HP */
  121  {{ USB_DEVICE(0x03f0, 0x0605) }, 0 }, /* ScanJet 2200C */
  122  {{ USB_DEVICE(0x03f0, 0x0205) }, 0 }, /* ScanJet 3300C */
  123  {{ USB_DEVICE(0x03f0, 0x0405) }, 0 }, /* ScanJet 3400cse */
  124  {{ USB_DEVICE(0x03f0, 0x0101) }, 0 }, /* Scanjet 4100C */
  125  {{ USB_DEVICE(0x03f0, 0x0105) }, 0 }, /* ScanJet 4200C */
  126  {{ USB_DEVICE(0x03f0, 0x0305) }, 0 }, /* Scanjet 4300C */
  127  {{ USB_DEVICE(0x03f0, 0x3005) }, 0 }, /* ScanJet 4670v */
  128  {{ USB_DEVICE(0x03f0, 0x0102) }, 0 }, /* Photosmart S20 */
  129  {{ USB_DEVICE(0x03f0, 0x0401) }, 0 }, /* Scanjet 5200C */
  130  {{ USB_DEVICE(0x03f0, 0x0701) }, 0 }, /* Scanjet 5300C */
  131  {{ USB_DEVICE(0x03f0, 0x1005) }, 0 }, /* Scanjet 5400C */
  132  {{ USB_DEVICE(0x03f0, 0x0201) }, 0 }, /* ScanJet 6200C */
  133  {{ USB_DEVICE(0x03f0, 0x0601) }, 0 }, /* Scanjet 6300C */
  134  {{ USB_DEVICE(0x03f0, 0x0b01) }, 0 }, /* Scanjet 82x0C */
  135   /* Scanlogic */
  136  {{ USB_DEVICE(0x04ce, 0x0300) }, 0 }, /* Phantom 336CX - C3 */
  137   /* Microtek */
  138  {{ USB_DEVICE(0x05da, 0x0099) }, 0 }, /* Phantom 336CX - C3 */
  139  {{ USB_DEVICE(0x05da, 0x0094) }, 0 }, /* ScanMaker X6 - X6U */
  140  {{ USB_DEVICE(0x05da, 0x00a0) }, 0 }, /* Phantom 336CX - C3 */
  141  {{ USB_DEVICE(0x05da, 0x009a) }, 0 }, /* Phantom C6 */
  142  {{ USB_DEVICE(0x05da, 0x00a3) }, 0 }, /* ScanMaker V6USL */
  143  {{ USB_DEVICE(0x05da, 0x80a3) }, 0 }, /* ScanMaker V6USL */
  144  {{ USB_DEVICE(0x05da, 0x80ac) }, 0 }, /* ScanMaker V6UL */
  145   /* Minolta */
  146  {{ USB_DEVICE(0x0686, 0x400e) }, 0 }, /* Dimage 5400 */
  147   /* Mustek */
  148  {{ USB_DEVICE(0x055f, 0x0001) }, 0 }, /* 1200 CU */
  149  {{ USB_DEVICE(0x055f, 0x0010) }, 0 }, /* BearPaw 1200F */
  150  {{ USB_DEVICE(0x055f, 0x021e) }, 0 }, /* BearPaw 1200TA */
  151  {{ USB_DEVICE(0x055f, 0x0873) }, 0 }, /* 600 USB */
  152  {{ USB_DEVICE(0x055f, 0x0002) }, 0 }, /* 600 CU */
  153  {{ USB_DEVICE(0x055f, 0x0003) }, 0 }, /* 1200 USB */
  154  {{ USB_DEVICE(0x055f, 0x0006) }, 0 }, /* 1200 UB */
  155  {{ USB_DEVICE(0x055f, 0x0007) }, 0 }, /* 1200 USB */
  156  {{ USB_DEVICE(0x055f, 0x0008) }, 0 }, /* 1200 CU */
  157   /* National */
  158  {{ USB_DEVICE(0x0400, 0x1000) }, 0 }, /* BearPaw 1200 */
  159  {{ USB_DEVICE(0x0400, 0x1001) }, 0 }, /* BearPaw 2400 */
  160   /* Nikon */
  161  {{ USB_DEVICE(0x04b0, 0x4000) }, 0 }, /* CoolScan LS40 ED */
  162   /* Primax */
  163  {{ USB_DEVICE(0x0461, 0x0300) }, 0 }, /* G2-200 */
  164  {{ USB_DEVICE(0x0461, 0x0301) }, 0 }, /* G2E-300 */
  165  {{ USB_DEVICE(0x0461, 0x0302) }, 0 }, /* G2-300 */
  166  {{ USB_DEVICE(0x0461, 0x0303) }, 0 }, /* G2E-300 */
  167  {{ USB_DEVICE(0x0461, 0x0340) }, 0 }, /* Colorado USB 9600 */
  168  {{ USB_DEVICE(0x0461, 0x0341) }, 0 }, /* Colorado 600u */
  169  {{ USB_DEVICE(0x0461, 0x0345) }, 0 }, /* Visioneer 6200 */
  170  {{ USB_DEVICE(0x0461, 0x0360) }, 0 }, /* Colorado USB 19200 */
  171  {{ USB_DEVICE(0x0461, 0x0361) }, 0 }, /* Colorado 1200u */
  172  {{ USB_DEVICE(0x0461, 0x0380) }, 0 }, /* G2-600 */
  173  {{ USB_DEVICE(0x0461, 0x0381) }, 0 }, /* ReadyScan 636i */
  174  {{ USB_DEVICE(0x0461, 0x0382) }, 0 }, /* G2-600 */
  175  {{ USB_DEVICE(0x0461, 0x0383) }, 0 }, /* G2E-600 */
  176   /* Epson */
  177  {{ USB_DEVICE(0x04b8, 0x0101) }, 0 }, /* Perfection 636U/636Photo */
  178  {{ USB_DEVICE(0x04b8, 0x0103) }, 0 }, /* Perfection 610 */
  179  {{ USB_DEVICE(0x04b8, 0x0104) }, 0 }, /* Perfection 1200U/1200Photo */
  180  {{ USB_DEVICE(0x04b8, 0x010b) }, 0 }, /* Perfection 1240U/1240Photo */
  181  {{ USB_DEVICE(0x04b8, 0x010f) }, 0 }, /* Perfection 1250U/1250Photo */
  182  {{ USB_DEVICE(0x04b8, 0x0107) }, 0 }, /* Expression 1600 */
  183  {{ USB_DEVICE(0x04b8, 0x010a) }, 0 }, /* Perfection 1640SU */
  184  {{ USB_DEVICE(0x04b8, 0x010c) }, 0 }, /* Perfection 640U */
  185  {{ USB_DEVICE(0x04b8, 0x0110) }, 0 }, /* Perfection 1650 */
  186  {{ USB_DEVICE(0x04b8, 0x011e) }, 0 }, /* Perfection 1660 */
  187  {{ USB_DEVICE(0x04b8, 0x011d) }, 0 }, /* Perfection 1260 */
  188  {{ USB_DEVICE(0x04b8, 0x011f) }, 0 }, /* Perfection 1670 */
  189  {{ USB_DEVICE(0x04b8, 0x0120) }, 0 }, /* Perfection 1270 */
  190  {{ USB_DEVICE(0x04b8, 0x080f) }, 0 }, /* Stylus Photo RX425 */
  191  {{ USB_DEVICE(0x04b8, 0x011c) }, USC_KEEP_OPEN }, /* Perfection 3200 */
  192  {{ USB_DEVICE(0x04b8, 0x0112) }, USC_KEEP_OPEN }, /* GT-9700F */
  193  {{ USB_DEVICE(0x04b8, 0x011b) }, 0 }, /* GT-9300UF */
  194  {{ USB_DEVICE(0x04b8, 0x0121) }, 0 }, /* Perfection 2480 */
  195  {{ USB_DEVICE(0x04b8, 0x080e) }, USC_KEEP_OPEN }, /* CX-3500/3600/3650 MFP */
  196  {{ USB_DEVICE(0x04b8, 0x0122) }, 0 }, /* Perfection 3590 */
  197  {{ USB_DEVICE(0x04b8, 0x0820) }, 0 }, /* CX4200 MP */
  198  {{ USB_DEVICE(0x04b8, 0x012a) }, 0 }, /* Perfection 4990 Photo */
  199  {{ USB_DEVICE(0x04b8, 0x082b) }, 0 }, /* DX-50x0 MFP */
  200  {{ USB_DEVICE(0x04b8, 0x082e) }, 0 }, /* DX-60x0 MFP */
  201   /* UMAX */
  202  {{ USB_DEVICE(0x1606, 0x0010) }, 0 }, /* Astra 1220U */
  203  {{ USB_DEVICE(0x1606, 0x0002) }, 0 }, /* Astra 1236U */
  204  {{ USB_DEVICE(0x1606, 0x0030) }, 0 }, /* Astra 2000U */
  205  {{ USB_DEVICE(0x1606, 0x0130) }, 0 }, /* Astra 2100U */
  206  {{ USB_DEVICE(0x1606, 0x0230) }, 0 }, /* Astra 2200U */
  207  {{ USB_DEVICE(0x1606, 0x0060) }, 0 }, /* Astra 3400 */
  208   /* Visioneer */
  209  {{ USB_DEVICE(0x04a7, 0x0224) }, 0 }, /* Scanport 3000 */
  210  {{ USB_DEVICE(0x04a7, 0x0221) }, 0 }, /* OneTouch 5300 */
  211  {{ USB_DEVICE(0x04a7, 0x0211) }, 0 }, /* OneTouch 7600 */
  212  {{ USB_DEVICE(0x04a7, 0x0231) }, 0 }, /* OneTouch 6100 */
  213  {{ USB_DEVICE(0x04a7, 0x0311) }, 0 }, /* OneTouch 6200 */
  214  {{ USB_DEVICE(0x04a7, 0x0321) }, 0 }, /* OneTouch 8100 */
  215  {{ USB_DEVICE(0x04a7, 0x0331) }, 0 }, /* OneTouch 8600 */
  216   /* Ultima */
  217  {{ USB_DEVICE(0x05d8, 0x4002) }, 0 }, /* 1200 UB Plus */
  218 };
  219 #define uscanner_lookup(v, p) ((const struct uscan_info *)usb_lookup(uscanner_devs, v, p))
  220 
  221 #define USCANNER_BUFFERSIZE     1024
  222 
  223 struct uscanner_softc {
  224         device_t                sc_dev;         /* base device */
  225         usbd_device_handle      sc_udev;
  226         usbd_interface_handle   sc_iface;
  227 
  228         u_int                   sc_dev_flags;
  229 
  230         usbd_pipe_handle        sc_bulkin_pipe;
  231         int                     sc_bulkin;
  232         usbd_xfer_handle        sc_bulkin_xfer;
  233         void                    *sc_bulkin_buffer;
  234         int                     sc_bulkin_bufferlen;
  235         int                     sc_bulkin_datalen;
  236 
  237         usbd_pipe_handle        sc_bulkout_pipe;
  238         int                     sc_bulkout;
  239         usbd_xfer_handle        sc_bulkout_xfer;
  240         void                    *sc_bulkout_buffer;
  241         int                     sc_bulkout_bufferlen;
  242         int                     sc_bulkout_datalen;
  243 
  244         u_char                  sc_state;
  245 #define USCANNER_OPEN           0x01    /* opened */
  246 
  247         int                     sc_refcnt;
  248         u_char                  sc_dying;
  249 };
  250 
  251 d_open_t  uscanneropen;
  252 d_close_t uscannerclose;
  253 d_read_t  uscannerread;
  254 d_write_t uscannerwrite;
  255 d_kqfilter_t uscannerkqfilter;
  256 
  257 static struct dev_ops uscanner_ops = {
  258         { "uscanner", 0, 0 },
  259         .d_open =       uscanneropen,
  260         .d_close =      uscannerclose,
  261         .d_read =       uscannerread,
  262         .d_write =      uscannerwrite,
  263         .d_kqfilter =   uscannerkqfilter
  264 };
  265 
  266 static int uscanner_do_read(struct uscanner_softc *, struct uio *, int);
  267 static int uscanner_do_write(struct uscanner_softc *, struct uio *, int);
  268 static void uscanner_do_close(struct uscanner_softc *);
  269 
  270 #define USCANNERUNIT(n) (minor(n))
  271 
  272 static device_probe_t uscanner_match;
  273 static device_attach_t uscanner_attach;
  274 static device_detach_t uscanner_detach;
  275 
  276 static devclass_t uscanner_devclass;
  277 
  278 static kobj_method_t uscanner_methods[] = {
  279         DEVMETHOD(device_probe, uscanner_match),
  280         DEVMETHOD(device_attach, uscanner_attach),
  281         DEVMETHOD(device_detach, uscanner_detach),
  282         DEVMETHOD_END
  283 };
  284 
  285 static driver_t uscanner_driver = {
  286         "uscanner",
  287         uscanner_methods,
  288         sizeof(struct uscanner_softc)
  289 };
  290 
  291 MODULE_DEPEND(uscanner, usb, 1, 1, 1);
  292 
  293 static int
  294 uscanner_match(device_t self)
  295 {
  296         struct usb_attach_arg *uaa = device_get_ivars(self);
  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 static int
  306 uscanner_attach(device_t self)
  307 {
  308         struct uscanner_softc *sc = device_get_softc(self);
  309         struct usb_attach_arg *uaa = device_get_ivars(self);
  310         usb_interface_descriptor_t *id = NULL;
  311         usb_endpoint_descriptor_t *ed, *ed_bulkin = NULL, *ed_bulkout = NULL;
  312         int i;
  313         usbd_status err;
  314 
  315         sc->sc_dev = self;
  316 
  317         sc->sc_dev_flags = uscanner_lookup(uaa->vendor, uaa->product)->flags;
  318 
  319         sc->sc_udev = uaa->device;
  320 
  321         err = usbd_set_config_no(uaa->device, 1, 1); /* XXX */
  322         if (err) {
  323                 kprintf("%s: setting config no failed\n",
  324                     device_get_nameunit(sc->sc_dev));
  325                 return ENXIO;
  326         }
  327 
  328         /* XXX We only check the first interface */
  329         err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
  330         if (!err && sc->sc_iface)
  331             id = usbd_get_interface_descriptor(sc->sc_iface);
  332         if (err || id == NULL) {
  333                 kprintf("%s: could not get interface descriptor, err=%d,id=%p\n",
  334                        device_get_nameunit(sc->sc_dev), err, id);
  335                 return ENXIO;
  336         }
  337 
  338         /* Find the two first bulk endpoints */
  339         for (i = 0 ; i < id->bNumEndpoints; i++) {
  340                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
  341                 if (ed == NULL) {
  342                         kprintf("%s: could not read endpoint descriptor\n",
  343                                device_get_nameunit(sc->sc_dev));
  344                         return ENXIO;
  345                 }
  346 
  347                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
  348                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
  349                         ed_bulkin = ed;
  350                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
  351                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
  352                         ed_bulkout = ed;
  353                 }
  354 
  355                 if (ed_bulkin && ed_bulkout)    /* found all we need */
  356                         break;
  357         }
  358 
  359         /* Verify that we goething sensible */
  360         if (ed_bulkin == NULL || ed_bulkout == NULL) {
  361                 kprintf("%s: bulk-in and/or bulk-out endpoint not found\n",
  362                         device_get_nameunit(sc->sc_dev));
  363                 return ENXIO;
  364         }
  365 
  366         sc->sc_bulkin = ed_bulkin->bEndpointAddress;
  367         sc->sc_bulkout = ed_bulkout->bEndpointAddress;
  368 
  369         /* the main device, ctrl endpoint */
  370         make_dev(&uscanner_ops, device_get_unit(sc->sc_dev),
  371                  UID_ROOT, GID_OPERATOR, 0644,
  372                  "%s", device_get_nameunit(sc->sc_dev));
  373 
  374         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
  375                            sc->sc_dev);
  376 
  377         return 0;
  378 }
  379 
  380 int
  381 uscanneropen(struct dev_open_args *ap)
  382 {
  383         cdev_t dev = ap->a_head.a_dev;
  384         struct uscanner_softc *sc;
  385         int unit = USCANNERUNIT(dev);
  386         usbd_status err;
  387 
  388         sc = devclass_get_softc(uscanner_devclass, unit);
  389         if (sc == NULL)
  390                 return (ENXIO);
  391 
  392         DPRINTFN(5, ("uscanneropen: flag=%d, mode=%d, unit=%d\n",
  393                      ap->a_oflags, ap->a_devtype, 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 = kmalloc(USCANNER_BUFFERSIZE, M_USBDEV, M_WAITOK);
  404         sc->sc_bulkout_buffer = kmalloc(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                         kprintf("%s: cannot open bulk-in pipe (addr %d)\n",
  416                                device_get_nameunit(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                         kprintf("%s: cannot open bulk-out pipe (addr %d)\n",
  426                                device_get_nameunit(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(struct dev_close_args *ap)
  448 {
  449         cdev_t dev = ap->a_head.a_dev;
  450         struct uscanner_softc *sc;
  451 
  452         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
  453 
  454         DPRINTFN(5, ("uscannerclose: flag=%d, mode=%d, unit=%d\n",
  455                      ap->a_fflag, ap->a_devtype, USCANNERUNIT(dev)));
  456 
  457 #ifdef DIAGNOSTIC
  458         if (!(sc->sc_state & USCANNER_OPEN)) {
  459                 kprintf("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                 kfree(sc->sc_bulkin_buffer, M_USBDEV);
  496                 sc->sc_bulkin_buffer = NULL;
  497         }
  498         if (sc->sc_bulkout_buffer) {
  499                 kfree(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", device_get_nameunit(sc->sc_dev)));
  514 
  515         if (sc->sc_dying)
  516                 return (EIO);
  517 
  518         while ((n = szmin(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 dev_read_args *ap)
  547 {
  548         cdev_t dev = ap->a_head.a_dev;
  549         struct uscanner_softc *sc;
  550         int error;
  551 
  552         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
  553 
  554         sc->sc_refcnt++;
  555         error = uscanner_do_read(sc, ap->a_uio, ap->a_ioflag);
  556         if (--sc->sc_refcnt < 0)
  557                 usb_detach_wakeup(sc->sc_dev);
  558 
  559         return (error);
  560 }
  561 
  562 static int
  563 uscanner_do_write(struct uscanner_softc *sc, struct uio *uio, int flag)
  564 {
  565         u_int32_t n;
  566         int error = 0;
  567         usbd_status err;
  568 
  569         DPRINTFN(5, ("%s: uscanner_do_write\n", device_get_nameunit(sc->sc_dev)));
  570 
  571         if (sc->sc_dying)
  572                 return (EIO);
  573 
  574         while ((n = szmin(sc->sc_bulkout_bufferlen, uio->uio_resid)) != 0) {
  575                 error = uiomove(sc->sc_bulkout_buffer, n, uio);
  576                 if (error)
  577                         break;
  578                 DPRINTFN(1, ("uscanner_do_write: transfer %d bytes\n", n));
  579                 err = usbd_bulk_transfer(
  580                         sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
  581                         0, USBD_NO_TIMEOUT,
  582                         sc->sc_bulkout_buffer, &n,
  583                         "uscnwb");
  584                 if (err) {
  585                         if (err == USBD_INTERRUPTED)
  586                                 error = EINTR;
  587                         else
  588                                 error = EIO;
  589                         break;
  590                 }
  591         }
  592 
  593         return (error);
  594 }
  595 
  596 int
  597 uscannerwrite(struct dev_write_args *ap)
  598 {
  599         cdev_t dev = ap->a_head.a_dev;
  600         struct uscanner_softc *sc;
  601         int error;
  602 
  603         sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
  604 
  605         sc->sc_refcnt++;
  606         error = uscanner_do_write(sc, ap->a_uio, ap->a_ioflag);
  607         if (--sc->sc_refcnt < 0)
  608                 usb_detach_wakeup(sc->sc_dev);
  609         return (error);
  610 }
  611 
  612 static int
  613 uscanner_detach(device_t self)
  614 {
  615         struct uscanner_softc *sc = device_get_softc(self);
  616 
  617         DPRINTF(("uscanner_detach: sc=%p\n", sc));
  618 
  619         sc->sc_dying = 1;
  620         sc->sc_dev_flags = 0;   /* make close really close device */
  621 
  622         /* Abort all pipes.  Causes processes waiting for transfer to wake. */
  623         if (sc->sc_bulkin_pipe != NULL)
  624                 usbd_abort_pipe(sc->sc_bulkin_pipe);
  625         if (sc->sc_bulkout_pipe != NULL)
  626                 usbd_abort_pipe(sc->sc_bulkout_pipe);
  627 
  628         crit_enter();
  629         if (--sc->sc_refcnt >= 0) {
  630                 /* Wait for processes to go away. */
  631                 usb_detach_wait(sc->sc_dev);
  632         }
  633         crit_exit();
  634 
  635         /* destroy the device for the control endpoint */
  636         dev_ops_remove_minor(&uscanner_ops, /*-1, */device_get_unit(sc->sc_dev));
  637 
  638         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
  639                            sc->sc_dev);
  640 
  641         return (0);
  642 }
  643 
  644 static void
  645 uscannerfilt_detach(struct knote *kn) {}
  646 
  647 static int
  648 uscannerfilt(struct knote *kn, long hint)
  649 {
  650         /*
  651          * We have no easy way of determining if a read will
  652          * yield any data or a write will happen.
  653          * Pretend they will.
  654          */
  655         return (1);
  656 }
  657 
  658 static struct filterops uscannerfiltops =
  659         { FILTEROP_ISFD, NULL, uscannerfilt_detach, uscannerfilt };
  660 
  661 int
  662 uscannerkqfilter(struct dev_kqfilter_args *ap)
  663 {
  664 /* XXX
  665         cdev_t dev = ap->a_head.a_dev;
  666         struct uscanner_softc *sc = devclass_get_softc(uscanner_devclass, USCANNERUNIT(dev));
  667 
  668         if (sc->sc_dying)
  669                 return (EIO);
  670 */
  671 
  672         ap->a_result = 0;
  673         ap->a_kn->kn_fop = &uscannerfiltops;
  674         return (0);
  675 }
  676 
  677 DRIVER_MODULE(uscanner, uhub, uscanner_driver, uscanner_devclass, usbd_driver_load, NULL);
  678 

Cache object: 3855516022ca367c21fa521853226e1d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.