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/ulpt/ulpt.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: ulpt.c,v 1.55 2002/10/23 09:14:01 jdolecek Exp $
    3  * $FreeBSD: src/sys/dev/usb/ulpt.c,v 1.59 2003/09/28 20:48:13 phk Exp $
    4  */
    5 
    6 /*
    7  * Copyright (c) 1998 The NetBSD Foundation, Inc.
    8  * All rights reserved.
    9  *
   10  * This code is derived from software contributed to The NetBSD Foundation
   11  * by Lennart Augustsson (lennart@augustsson.net) at
   12  * Carlstedt Research & Technology.
   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  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
   45  */
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/proc.h>
   50 #include <sys/kernel.h>
   51 #include <sys/module.h>
   52 #include <sys/bus.h>
   53 #include <sys/uio.h>
   54 #include <sys/conf.h>
   55 #include <sys/event.h>
   56 #include <sys/device.h>
   57 #include <sys/syslog.h>
   58 #include <sys/sysctl.h>
   59 #include <sys/devfs.h>
   60 #include <sys/thread2.h>
   61 
   62 #include <bus/usb/usb.h>
   63 #include <bus/usb/usbdi.h>
   64 #include <bus/usb/usbdi_util.h>
   65 #include <bus/usb/usb_quirks.h>
   66 
   67 #define TIMEOUT         hz*16   /* wait up to 16 seconds for a ready */
   68 #define STEP            hz/4
   69 
   70 #define ULPT_BSIZE      16384
   71 #define ULPT_JUNK_SIZE  512
   72 
   73 #ifdef USB_DEBUG
   74 #define DPRINTF(x)      if (ulptdebug) kprintf x
   75 #define DPRINTFN(n,x)   if (ulptdebug>=(n)) kprintf x
   76 int     ulptdebug = 0;
   77 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
   78 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
   79            &ulptdebug, 0, "ulpt debug level");
   80 #else
   81 #define DPRINTF(x)
   82 #define DPRINTFN(n,x)
   83 #endif
   84 
   85 #define UR_GET_DEVICE_ID 0
   86 #define UR_GET_PORT_STATUS 1
   87 #define UR_SOFT_RESET 2
   88 
   89 #define LPS_NERR                0x08    /* printer no error */
   90 #define LPS_SELECT              0x10    /* printer selected */
   91 #define LPS_NOPAPER             0x20    /* printer out of paper */
   92 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
   93 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
   94 
   95 struct ulpt_softc {
   96         device_t sc_dev;
   97         usbd_device_handle sc_udev;     /* device */
   98         usbd_interface_handle sc_iface; /* interface */
   99         int sc_ifaceno;
  100         cdev_t  sc_cdev1;
  101         cdev_t  sc_cdev2;
  102 
  103         int sc_out;
  104         usbd_pipe_handle sc_out_pipe;   /* bulk out pipe */
  105 
  106         int sc_in;
  107         usbd_pipe_handle sc_in_pipe;    /* bulk in pipe */
  108         usbd_xfer_handle sc_in_xfer1;
  109         usbd_xfer_handle sc_in_xfer2;
  110         u_char *sc_junk1;               /* somewhere to dump input */
  111         u_char *sc_junk2;               /* somewhere to dump input */
  112 
  113         u_char sc_state;
  114 #define ULPT_OPEN       0x01    /* device is open */
  115 #define ULPT_OBUSY      0x02    /* printer is busy doing output */
  116 #define ULPT_INIT       0x04    /* waiting to initialize for open */
  117         u_char sc_flags;
  118 #define ULPT_NOPRIME    0x40    /* don't prime on open */
  119         u_char sc_laststatus;
  120 
  121         int sc_refcnt;
  122         u_char sc_dying;
  123         struct kqinfo   sc_wkq;
  124         int vendor;
  125         int product;
  126 };
  127 
  128 static d_open_t ulptopen;
  129 static d_close_t ulptclose;
  130 static d_write_t ulptwrite;
  131 static d_ioctl_t ulptioctl;
  132 static d_kqfilter_t ulptkqfilter;
  133 
  134 static void ulpt_filt_detach(struct knote *);
  135 static int ulpt_filt(struct knote *, long);
  136 
  137 static struct dev_ops ulpt_ops = {
  138         { "ulpt", 0, 0 },
  139         .d_open =       ulptopen,
  140         .d_close =      ulptclose,
  141         .d_write =      ulptwrite,
  142         .d_ioctl =      ulptioctl,
  143         .d_kqfilter =   ulptkqfilter
  144 };
  145 
  146 void ulpt_disco(void *);
  147 
  148 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
  149 int ulpt_status(struct ulpt_softc *);
  150 void ulpt_reset(struct ulpt_softc *);
  151 int ulpt_statusmsg(u_char, struct ulpt_softc *);
  152 
  153 #if 0
  154 void ieee1284_print_id(char *);
  155 #endif
  156 
  157 #define ULPTUNIT(s)     (minor(s) & 0x1f)
  158 #define ULPTFLAGS(s)    (minor(s) & 0xe0)
  159 
  160 
  161 static device_probe_t ulpt_match;
  162 static device_attach_t ulpt_attach;
  163 static device_detach_t ulpt_detach;
  164 
  165 static devclass_t ulpt_devclass;
  166 
  167 static kobj_method_t ulpt_methods[] = {
  168         DEVMETHOD(device_probe, ulpt_match),
  169         DEVMETHOD(device_attach, ulpt_attach),
  170         DEVMETHOD(device_detach, ulpt_detach),
  171         DEVMETHOD_END
  172 };
  173 
  174 static driver_t ulpt_driver = {
  175         "ulpt",
  176         ulpt_methods,
  177         sizeof(struct ulpt_softc)
  178 };
  179 
  180 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
  181 
  182 static int
  183 ulpt_match(device_t self)
  184 {
  185         struct usb_attach_arg *uaa = device_get_ivars(self);
  186         usb_interface_descriptor_t *id;
  187 
  188         DPRINTFN(10,("ulpt_match\n"));
  189         if (uaa->iface == NULL)
  190                 return (UMATCH_NONE);
  191         id = usbd_get_interface_descriptor(uaa->iface);
  192         if (id != NULL &&
  193             id->bInterfaceClass == UICLASS_PRINTER &&
  194             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
  195             (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
  196              id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
  197              id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
  198                 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
  199         return (UMATCH_NONE);
  200 }
  201 
  202 static int
  203 ulpt_attach(device_t self)
  204 {
  205         struct ulpt_softc *sc = device_get_softc(self);
  206         struct usb_attach_arg *uaa = device_get_ivars(self);
  207         usbd_device_handle dev = uaa->device;
  208         usbd_interface_handle iface = uaa->iface;
  209         usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
  210         usb_interface_descriptor_t *id, *iend;
  211         usb_config_descriptor_t *cdesc;
  212         usbd_status err;
  213         usb_endpoint_descriptor_t *ed;
  214         u_int8_t epcount;
  215         int i, altno;
  216 
  217         DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
  218         sc->sc_dev = self;
  219 
  220         /* XXX
  221          * Stepping through the alternate settings needs to be abstracted out.
  222          */
  223         cdesc = usbd_get_config_descriptor(dev);
  224         if (cdesc == NULL) {
  225                 kprintf("%s: failed to get configuration descriptor\n",
  226                        device_get_nameunit(sc->sc_dev));
  227                 return ENXIO;
  228         }
  229         iend = (usb_interface_descriptor_t *)
  230                    ((char *)cdesc + UGETW(cdesc->wTotalLength));
  231 #ifdef DIAGNOSTIC
  232         if (ifcd < (usb_interface_descriptor_t *)cdesc ||
  233             ifcd >= iend)
  234                 panic("ulpt: iface desc out of range");
  235 #endif
  236         /* Step through all the descriptors looking for bidir mode */
  237         for (id = ifcd, altno = 0;
  238              id < iend;
  239              id = (void *)((char *)id + id->bLength)) {
  240                 if (id->bDescriptorType == UDESC_INTERFACE &&
  241                     id->bInterfaceNumber == ifcd->bInterfaceNumber) {
  242                         if (id->bInterfaceClass == UICLASS_PRINTER &&
  243                             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
  244                             (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
  245                              id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
  246                                 goto found;
  247                         altno++;
  248                 }
  249         }
  250         id = ifcd;              /* not found, use original */
  251  found:
  252         if (id != ifcd) {
  253                 /* Found a new bidir setting */
  254                 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
  255                 err = usbd_set_interface(iface, altno);
  256                 if (err) {
  257                         kprintf("%s: setting alternate interface failed\n",
  258                                device_get_nameunit(sc->sc_dev));
  259                         sc->sc_dying = 1;
  260                         return ENXIO;
  261                 }
  262         }
  263 
  264         epcount = 0;
  265         (void)usbd_endpoint_count(iface, &epcount);
  266 
  267         sc->sc_in = -1;
  268         sc->sc_out = -1;
  269         sc->vendor = uaa->vendor;
  270         sc->product = uaa->product;
  271         for (i = 0; i < epcount; i++) {
  272                 ed = usbd_interface2endpoint_descriptor(iface, i);
  273                 if (ed == NULL) {
  274                         kprintf("%s: couldn't get ep %d\n",
  275                             device_get_nameunit(sc->sc_dev), i);
  276                         return ENXIO;
  277                 }
  278                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  279                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
  280                         sc->sc_in = ed->bEndpointAddress;
  281                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
  282                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
  283                         sc->sc_out = ed->bEndpointAddress;
  284                 }
  285         }
  286         if (sc->sc_out == -1) {
  287                 kprintf("%s: could not find bulk out endpoint\n",
  288                     device_get_nameunit(sc->sc_dev));
  289                 sc->sc_dying = 1;
  290                 return ENXIO;
  291         }
  292 
  293         if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
  294                 /* This device doesn't handle reading properly. */
  295                 sc->sc_in = -1;
  296         }
  297 
  298         kprintf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev),
  299                sc->sc_in >= 0 ? "bi" : "uni");
  300 
  301         DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
  302 
  303         sc->sc_iface = iface;
  304         sc->sc_ifaceno = id->bInterfaceNumber;
  305         sc->sc_udev = dev;
  306         sc->sc_junk1 = NULL;
  307         sc->sc_junk2 = NULL;
  308 
  309         sc->sc_cdev1 = make_dev(&ulpt_ops, device_get_unit(self),
  310                                 UID_ROOT, GID_OPERATOR, 0644,
  311                                 "ulpt%d", device_get_unit(self));
  312         sc->sc_cdev2 = make_dev(&ulpt_ops, device_get_unit(self)|ULPT_NOPRIME,
  313                                 UID_ROOT, GID_OPERATOR, 0644,
  314                                 "unlpt%d", device_get_unit(self));
  315 
  316         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
  317                            sc->sc_dev);
  318 
  319         return 0;
  320 }
  321 
  322 static int
  323 ulpt_detach(device_t self)
  324 {
  325         struct ulpt_softc *sc = device_get_softc(self);
  326 
  327         DPRINTF(("ulpt_detach: sc=%p\n", sc));
  328 
  329         sc->sc_dying = 1;
  330         if (sc->sc_out_pipe != NULL)
  331                 usbd_abort_pipe(sc->sc_out_pipe);
  332         if (sc->sc_in_pipe != NULL)
  333                 usbd_abort_pipe(sc->sc_in_pipe);
  334 
  335         /*
  336          * Wait for any ongoing operations to complete before we actually
  337          * close things down.
  338          */
  339 
  340         crit_enter();
  341         --sc->sc_refcnt;
  342         if (sc->sc_refcnt >= 0) {
  343                 kprintf("%s: waiting for idle\n", device_get_nameunit(sc->sc_dev));
  344                 while (sc->sc_refcnt >= 0)
  345                         usb_detach_wait(sc->sc_dev);
  346                 kprintf("%s: idle wait done\n", device_get_nameunit(sc->sc_dev));
  347         }
  348         crit_exit();
  349 
  350         dev_ops_remove_minor(&ulpt_ops, /*-1, */device_get_unit(self));
  351         devfs_assume_knotes(sc->sc_cdev1, &sc->sc_wkq);
  352         devfs_assume_knotes(sc->sc_cdev2, &sc->sc_wkq);
  353 
  354         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
  355 
  356         return (0);
  357 }
  358 
  359 int
  360 ulpt_status(struct ulpt_softc *sc)
  361 {
  362         usb_device_request_t req;
  363         usbd_status err;
  364         u_char status;
  365 
  366         req.bmRequestType = UT_READ_CLASS_INTERFACE;
  367         req.bRequest = UR_GET_PORT_STATUS;
  368         USETW(req.wValue, 0);
  369         USETW(req.wIndex, sc->sc_ifaceno);
  370         USETW(req.wLength, 1);
  371         err = usbd_do_request(sc->sc_udev, &req, &status);
  372         DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
  373         if (!err)
  374                 return (status);
  375         else
  376                 return (0);
  377 }
  378 
  379 void
  380 ulpt_reset(struct ulpt_softc *sc)
  381 {
  382         usb_device_request_t req;
  383         int other_fails;
  384 
  385         /* The Brother HL1240 doesn't handle UT_WRITE_CLASS_OTHER */
  386         other_fails = (sc->vendor == 0x04f9 && sc->product == 0x0006);
  387 
  388         DPRINTFN(1, ("ulpt_reset\n"));
  389         req.bRequest = UR_SOFT_RESET;
  390         USETW(req.wValue, 0);
  391         USETW(req.wIndex, sc->sc_ifaceno);
  392         USETW(req.wLength, 0);
  393 
  394         /*
  395          * There was a mistake in the USB printer 1.0 spec that gave the
  396          * request type as UT_WRITE_CLASS_OTHER; it should have been
  397          * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
  398          * so we try both.
  399          */
  400         req.bmRequestType = UT_WRITE_CLASS_OTHER;
  401         /* Some printers don't handle UT_WRITE_CLASS_OTHER */
  402         if (other_fails || usbd_do_request(sc->sc_udev, &req, 0)) {/* 1.0 */
  403                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
  404                 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
  405         }
  406 }
  407 
  408 static void
  409 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
  410 {
  411         struct ulpt_softc *sc = priv;
  412         u_int32_t count;
  413 
  414         /* Don't loop on errors or 0-length input. */
  415         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
  416         if (status != USBD_NORMAL_COMPLETION || count == 0)
  417                 return;
  418 
  419         DPRINTFN(2,("ulpt_input: got some data\n"));
  420         /* Do it again. */
  421         if (xfer == sc->sc_in_xfer1)
  422                 usbd_transfer(sc->sc_in_xfer2);
  423         else
  424                 usbd_transfer(sc->sc_in_xfer1);
  425 }
  426 
  427 int ulptusein = 1;
  428 
  429 /*
  430  * Reset the printer, then wait until it's selected and not busy.
  431  */
  432 int
  433 ulptopen(struct dev_open_args *ap)
  434 {
  435         cdev_t dev = ap->a_head.a_dev;
  436         u_char flags = ULPTFLAGS(dev);
  437         struct ulpt_softc *sc;
  438         usbd_status err;
  439         int spin, error;
  440 
  441         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
  442         if (sc == NULL)
  443                 return (ENXIO);
  444 
  445         if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
  446                 return (ENXIO);
  447 
  448         if (sc->sc_state)
  449                 return (EBUSY);
  450 
  451         sc->sc_state = ULPT_INIT;
  452         sc->sc_flags = flags;
  453         DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
  454 
  455 #if defined(USB_DEBUG)
  456         /* Ignoring these flags might not be a good idea */
  457         if ((flags & ~ULPT_NOPRIME) != 0)
  458                 kprintf("ulptopen: flags ignored: %b\n", flags,
  459                         "\2\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
  460 #endif
  461 
  462         error = 0;
  463         sc->sc_refcnt++;
  464 
  465         if ((flags & ULPT_NOPRIME) == 0) {
  466                 ulpt_reset(sc);
  467                 if (sc->sc_dying) {
  468                         error = ENXIO;
  469                         sc->sc_state = 0;
  470                         goto done;
  471                 }
  472         }
  473 
  474         for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
  475                 DPRINTF(("ulpt_open: waiting a while\n"));
  476                 if (spin >= TIMEOUT) {
  477                         error = EBUSY;
  478                         sc->sc_state = 0;
  479                         goto done;
  480                 }
  481 
  482                 /* wait 1/4 second, give up if we get a signal */
  483                 error = tsleep((caddr_t)sc, PCATCH, "ulptop", STEP);
  484                 if (error != EWOULDBLOCK) {
  485                         sc->sc_state = 0;
  486                         goto done;
  487                 }
  488 
  489                 if (sc->sc_dying) {
  490                         error = ENXIO;
  491                         sc->sc_state = 0;
  492                         goto done;
  493                 }
  494         }
  495 
  496         err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
  497         if (err) {
  498                 sc->sc_state = 0;
  499                 error = EIO;
  500                 goto done;
  501         }
  502 
  503         if (ulptusein && sc->sc_in != -1) {
  504                 DPRINTF(("ulpt_open: open input pipe\n"));
  505                 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
  506                 if (err) {
  507                         error = EIO;
  508                         usbd_close_pipe(sc->sc_out_pipe);
  509                         sc->sc_out_pipe = NULL;
  510                         sc->sc_state = 0;
  511                         goto done;
  512                 }
  513                 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
  514                 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
  515                 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
  516                         error = ENOMEM;
  517                         if (sc->sc_in_xfer1 != NULL) {
  518                                 usbd_free_xfer(sc->sc_in_xfer1);
  519                                 sc->sc_in_xfer1 = NULL;
  520                         }
  521                         if (sc->sc_in_xfer2 != NULL) {
  522                                 usbd_free_xfer(sc->sc_in_xfer2);
  523                                 sc->sc_in_xfer2 = NULL;
  524                         }
  525                         usbd_close_pipe(sc->sc_out_pipe);
  526                         sc->sc_out_pipe = NULL;
  527                         usbd_close_pipe(sc->sc_in_pipe);
  528                         sc->sc_in_pipe = NULL;
  529                         sc->sc_state = 0;
  530                         goto done;
  531                 }
  532                 sc->sc_junk1 = usbd_alloc_buffer(sc->sc_in_xfer1,
  533                                                  ULPT_JUNK_SIZE);
  534                 sc->sc_junk2 = usbd_alloc_buffer(sc->sc_in_xfer2,
  535                                                  ULPT_JUNK_SIZE);
  536                 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
  537                     sc->sc_junk1, ULPT_JUNK_SIZE, USBD_SHORT_XFER_OK,
  538                     USBD_NO_TIMEOUT, ulpt_input);
  539                 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
  540                     sc->sc_junk2, ULPT_JUNK_SIZE, USBD_SHORT_XFER_OK,
  541                     USBD_NO_TIMEOUT, ulpt_input);
  542                 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
  543         }
  544 
  545         sc->sc_state = ULPT_OPEN;
  546 
  547 done:
  548         if (--sc->sc_refcnt < 0)
  549                 usb_detach_wakeup(sc->sc_dev);
  550 
  551         DPRINTF(("ulptopen: done, error=%d\n", error));
  552         return (error);
  553 }
  554 
  555 int
  556 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
  557 {
  558         u_char new;
  559 
  560         status = (status ^ LPS_INVERT) & LPS_MASK;
  561         new = status & ~sc->sc_laststatus;
  562         sc->sc_laststatus = status;
  563 
  564         if (new & LPS_SELECT)
  565                 log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev));
  566         else if (new & LPS_NOPAPER)
  567                 log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev));
  568         else if (new & LPS_NERR)
  569                 log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev));
  570 
  571         return (status);
  572 }
  573 
  574 int
  575 ulptclose(struct dev_close_args *ap)
  576 {
  577         cdev_t dev = ap->a_head.a_dev;
  578         struct ulpt_softc *sc;
  579 
  580         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
  581 
  582         if (sc == NULL)
  583                 return (0);
  584 
  585         if (sc->sc_state != ULPT_OPEN)
  586                 /* We are being forced to close before the open completed. */
  587                 return (0);
  588 
  589         if (sc->sc_out_pipe != NULL) {
  590                 usbd_close_pipe(sc->sc_out_pipe);
  591                 sc->sc_out_pipe = NULL;
  592         }
  593         if (sc->sc_in_pipe != NULL) {
  594                 usbd_abort_pipe(sc->sc_in_pipe);
  595                 usbd_close_pipe(sc->sc_in_pipe);
  596                 sc->sc_in_pipe = NULL;
  597                 if (sc->sc_in_xfer1 != NULL) {
  598                         usbd_free_xfer(sc->sc_in_xfer1);
  599                         sc->sc_in_xfer1 = NULL;
  600                 }
  601                 if (sc->sc_in_xfer2 != NULL) {
  602                         usbd_free_xfer(sc->sc_in_xfer2);
  603                         sc->sc_in_xfer2 = NULL;
  604                 }
  605         }
  606 
  607         sc->sc_state = 0;
  608 
  609         DPRINTF(("ulptclose: closed\n"));
  610         return (0);
  611 }
  612 
  613 int
  614 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
  615 {
  616         u_int32_t n;
  617         int error = 0;
  618         void *bufp;
  619         usbd_xfer_handle xfer;
  620         usbd_status err;
  621 
  622         DPRINTF(("ulptwrite\n"));
  623         xfer = usbd_alloc_xfer(sc->sc_udev);
  624         if (xfer == NULL)
  625                 return (ENOMEM);
  626         bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
  627         if (bufp == NULL) {
  628                 usbd_free_xfer(xfer);
  629                 return (ENOMEM);
  630         }
  631         while ((n = szmin(ULPT_BSIZE, uio->uio_resid)) != 0) {
  632                 ulpt_statusmsg(ulpt_status(sc), sc);
  633                 error = uiomove(bufp, n, uio);
  634                 if (error) {
  635                         DPRINTF(("ulpt_do_write: uiomove error = %d\n", error));
  636                         break;
  637                 }
  638                 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
  639                 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
  640                           USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
  641                 if (err) {
  642                         DPRINTF(("ulptwrite: error=%d\n", err));
  643                         error = EIO;
  644                         break;
  645                 }
  646         }
  647         usbd_free_xfer(xfer);
  648 
  649         return (error);
  650 }
  651 
  652 int
  653 ulptwrite(struct dev_write_args *ap)
  654 {
  655         cdev_t dev = ap->a_head.a_dev;
  656         struct ulpt_softc *sc;
  657         int error;
  658 
  659         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
  660 
  661         if (sc == NULL || sc->sc_dying)
  662                 return (EIO);
  663 
  664         sc->sc_refcnt++;
  665         error = ulpt_do_write(sc, ap->a_uio, ap->a_ioflag);
  666         DPRINTF(("ulpt_write: error = %d\n", error));
  667         if (--sc->sc_refcnt < 0)
  668                 usb_detach_wakeup(sc->sc_dev);
  669         return (error);
  670 }
  671 
  672 int
  673 ulptioctl(struct dev_ioctl_args *ap)
  674 {
  675         int error = 0;
  676 
  677         switch (ap->a_cmd) {
  678         default:
  679                 error = ENODEV;
  680         }
  681 
  682         return (error);
  683 }
  684 
  685 static struct filterops ulpt_filtops =
  686         { FILTEROP_ISFD, NULL, ulpt_filt_detach, ulpt_filt };
  687 
  688 static int
  689 ulptkqfilter(struct dev_kqfilter_args *ap)
  690 {
  691         cdev_t dev = ap->a_head.a_dev;
  692         struct knote *kn = ap->a_kn;
  693         struct ulpt_softc *sc;
  694         struct klist *klist;
  695 
  696         ap->a_result = 0;
  697 
  698         switch(kn->kn_filter) {
  699         case EVFILT_WRITE:
  700                 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
  701                 kn->kn_fop = &ulpt_filtops;
  702                 kn->kn_hook = (caddr_t)sc;
  703                 break;
  704         default:
  705                 ap->a_result = EOPNOTSUPP;
  706                 return (0);
  707         }
  708 
  709         klist = &sc->sc_wkq.ki_note;
  710         knote_insert(klist, kn);
  711 
  712         return(0);
  713 }
  714 
  715 static void
  716 ulpt_filt_detach(struct knote *kn)
  717 {
  718         struct ulpt_softc *sc = (struct ulpt_softc *)kn->kn_hook;
  719         struct klist *klist;
  720 
  721         klist = &sc->sc_wkq.ki_note;
  722         knote_remove(klist, kn);
  723 }
  724 
  725 static int
  726 ulpt_filt(struct knote *kn, long hint)
  727 {
  728 /*        struct ulpt_softc *sc = (struct ulpt_softc *)kn->kn_hook;*/
  729 
  730         return 1;
  731 }
  732 
  733 #if 0
  734 /* XXX This does not belong here. */
  735 /*
  736  * Print select parts of an IEEE 1284 device ID.
  737  */
  738 void
  739 ieee1284_print_id(char *str)
  740 {
  741         char *p, *q;
  742 
  743         for (p = str-1; p; p = strchr(p, ';')) {
  744                 p++;            /* skip ';' */
  745                 if (strncmp(p, "MFG:", 4) == 0 ||
  746                     strncmp(p, "MANUFACTURER:", 14) == 0 ||
  747                     strncmp(p, "MDL:", 4) == 0 ||
  748                     strncmp(p, "MODEL:", 6) == 0) {
  749                         q = strchr(p, ';');
  750                         if (q)
  751                                 kprintf("%.*s", (int)(q - p + 1), p);
  752                 }
  753         }
  754 }
  755 #endif
  756 
  757 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, NULL);
  758 

Cache object: 9d8bd2e6af701c828400defab17ead1f


[ 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.