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/usb/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 /*      $NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $       */
    2 
    3 /*-
    4  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Lennart Augustsson (lennart@augustsson.net) at
    9  * Carlstedt Research & Technology.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *        This product includes software developed by the NetBSD
   22  *        Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 #include <sys/cdefs.h>
   41 __FBSDID("$FreeBSD: releng/7.3/sys/dev/usb/ulpt.c 170969 2007-06-21 14:42:34Z imp $");
   42 
   43 /*
   44  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
   45  */
   46 
   47 /* XXXimp: need to migrate from devclass_get_softc */
   48 
   49 #include <sys/param.h>
   50 #include <sys/systm.h>
   51 #include <sys/proc.h>
   52 #include <sys/kernel.h>
   53 #include <sys/fcntl.h>
   54 #include <sys/ioccom.h>
   55 #include <sys/module.h>
   56 #include <sys/bus.h>
   57 #include <sys/uio.h>
   58 #include <sys/conf.h>
   59 #include <sys/syslog.h>
   60 #include <sys/sysctl.h>
   61 
   62 #include <dev/usb/usb.h>
   63 #include <dev/usb/usbdi.h>
   64 #include <dev/usb/usbdi_util.h>
   65 #include "usbdevs.h"
   66 #include <dev/usb/usb_quirks.h>
   67 
   68 #define TIMEOUT         hz*16   /* wait up to 16 seconds for a ready */
   69 #define STEP            hz/4
   70 
   71 #define LPTPRI          (PZERO+8)
   72 #define ULPT_BSIZE      PAGE_SIZE
   73 
   74 #define ULPT_READS_PER_SEC 5
   75 #define ULPT_READ_TIMO 10
   76 
   77 #ifdef USB_DEBUG
   78 #define DPRINTF(x)      if (ulptdebug) printf x
   79 #define DPRINTFN(n,x)   if (ulptdebug>(n)) printf x
   80 int     ulptdebug = 0;
   81 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
   82 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
   83            &ulptdebug, 0, "ulpt debug level");
   84 #else
   85 #define DPRINTF(x)
   86 #define DPRINTFN(n,x)
   87 #endif
   88 
   89 #define UR_GET_DEVICE_ID 0
   90 #define UR_GET_PORT_STATUS 1
   91 #define UR_SOFT_RESET 2
   92 
   93 #define LPS_NERR                0x08    /* printer no error */
   94 #define LPS_SELECT              0x10    /* printer selected */
   95 #define LPS_NOPAPER             0x20    /* printer out of paper */
   96 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
   97 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
   98 
   99 struct ulpt_softc {
  100         device_t sc_dev;
  101         usbd_device_handle sc_udev;     /* device */
  102         usbd_interface_handle sc_iface; /* interface */
  103         int sc_ifaceno;
  104 
  105         int sc_out;
  106         usbd_pipe_handle sc_out_pipe;   /* bulk out pipe */
  107         usbd_xfer_handle sc_out_xfer;
  108         void *sc_out_buf;
  109 
  110         int sc_in;
  111         usbd_pipe_handle sc_in_pipe;    /* bulk in pipe */
  112         usbd_xfer_handle sc_in_xfer;
  113         void *sc_in_buf;
  114 
  115         struct callout sc_read_callout;
  116         int sc_has_callout;
  117 
  118         u_char sc_state;
  119 #define ULPT_OPEN       0x01    /* device is open */
  120 #define ULPT_OBUSY      0x02    /* printer is busy doing output */
  121 #define ULPT_INIT       0x04    /* waiting to initialize for open */
  122         u_char sc_flags;
  123 #define ULPT_NOPRIME    0x40    /* don't prime on open */
  124         u_char sc_laststatus;
  125 
  126         int sc_refcnt;
  127         u_char sc_dying;
  128 
  129         struct cdev *dev;
  130         struct cdev *dev_noprime;
  131 };
  132 
  133 static d_open_t ulptopen;
  134 static d_close_t ulptclose;
  135 static d_write_t ulptwrite;
  136 static d_read_t ulptread;
  137 static d_ioctl_t ulptioctl;
  138 
  139 
  140 static struct cdevsw ulpt_cdevsw = {
  141         .d_version =    D_VERSION,
  142         .d_flags =      D_NEEDGIANT,
  143         .d_open =       ulptopen,
  144         .d_close =      ulptclose,
  145         .d_write =      ulptwrite,
  146         .d_read =       ulptread,
  147         .d_ioctl =      ulptioctl,
  148         .d_name =       "ulpt",
  149 };
  150 
  151 void ulpt_disco(void *);
  152 
  153 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
  154 int ulpt_do_read(struct ulpt_softc *, struct uio *uio, int);
  155 int ulpt_status(struct ulpt_softc *);
  156 void ulpt_reset(struct ulpt_softc *);
  157 int ulpt_statusmsg(u_char, struct ulpt_softc *);
  158 void ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
  159                   usbd_status status);
  160 void ulpt_tick(void *xsc);
  161 
  162 #if 0
  163 void ieee1284_print_id(char *);
  164 #endif
  165 
  166 #define ULPTUNIT(s)     (minor(s) & 0x1f)
  167 #define ULPTFLAGS(s)    (minor(s) & 0xe0)
  168 
  169 static device_probe_t ulpt_match;
  170 static device_attach_t ulpt_attach;
  171 static device_detach_t ulpt_detach;
  172 
  173 static device_method_t ulpt_methods[] = {
  174         /* Device interface */
  175         DEVMETHOD(device_probe,         ulpt_match),
  176         DEVMETHOD(device_attach,        ulpt_attach),
  177         DEVMETHOD(device_detach,        ulpt_detach),
  178 
  179         { 0, 0 }
  180 };
  181 
  182 static driver_t ulpt_driver = {
  183         "ulpt",
  184         ulpt_methods,
  185         sizeof(struct ulpt_softc)
  186 };
  187 
  188 static devclass_t ulpt_devclass;
  189 
  190 MODULE_DEPEND(umass, usb, 1, 1, 1);
  191 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
  192 
  193 static int
  194 ulpt_match(device_t self)
  195 {
  196         struct usb_attach_arg *uaa = device_get_ivars(self);
  197         usb_interface_descriptor_t *id;
  198 
  199         DPRINTFN(10,("ulpt_match\n"));
  200         if (uaa->iface == NULL)
  201                 return (UMATCH_NONE);
  202         id = usbd_get_interface_descriptor(uaa->iface);
  203         if (id != NULL &&
  204             id->bInterfaceClass == UICLASS_PRINTER &&
  205             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
  206             (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
  207              id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
  208              id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
  209                 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
  210         return (UMATCH_NONE);
  211 }
  212 
  213 static int
  214 ulpt_attach(device_t self)
  215 {
  216         struct ulpt_softc *sc = device_get_softc(self);
  217         struct usb_attach_arg *uaa = device_get_ivars(self);
  218         usbd_device_handle dev = uaa->device;
  219         usbd_interface_handle iface = uaa->iface;
  220         usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
  221         usb_interface_descriptor_t *id, *iend;
  222         usb_config_descriptor_t *cdesc;
  223         usbd_status err;
  224         usb_endpoint_descriptor_t *ed;
  225         u_int8_t epcount;
  226         int i, altno;
  227 
  228         DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
  229         sc->sc_dev = self;
  230 
  231         /* XXX
  232          * Stepping through the alternate settings needs to be abstracted out.
  233          */
  234         cdesc = usbd_get_config_descriptor(dev);
  235         if (cdesc == NULL) {
  236                 printf("%s: failed to get configuration descriptor\n",
  237                        device_get_nameunit(sc->sc_dev));
  238                 return ENXIO;
  239         }
  240         iend = (usb_interface_descriptor_t *)
  241                    ((char *)cdesc + UGETW(cdesc->wTotalLength));
  242 #ifdef DIAGNOSTIC
  243         if (ifcd < (usb_interface_descriptor_t *)cdesc ||
  244             ifcd >= iend)
  245                 panic("ulpt: iface desc out of range");
  246 #endif
  247         /* Step through all the descriptors looking for bidir mode */
  248         for (id = ifcd, altno = 0;
  249              id < iend;
  250              id = (void *)((char *)id + id->bLength)) {
  251                 if (id->bDescriptorType == UDESC_INTERFACE &&
  252                     id->bInterfaceNumber == ifcd->bInterfaceNumber) {
  253                         if (id->bInterfaceClass == UICLASS_PRINTER &&
  254                             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
  255                             (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
  256                              id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
  257                                 goto found;
  258                         altno++;
  259                 }
  260         }
  261         id = ifcd;              /* not found, use original */
  262  found:
  263         if (id != ifcd) {
  264                 /* Found a new bidir setting */
  265                 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
  266                 err = usbd_set_interface(iface, altno);
  267                 if (err) {
  268                         printf("%s: setting alternate interface failed\n",
  269                                device_get_nameunit(sc->sc_dev));
  270                         sc->sc_dying = 1;
  271                         return ENXIO;
  272                 }
  273         }
  274 
  275         epcount = 0;
  276         (void)usbd_endpoint_count(iface, &epcount);
  277 
  278         sc->sc_in = -1;
  279         sc->sc_out = -1;
  280         for (i = 0; i < epcount; i++) {
  281                 ed = usbd_interface2endpoint_descriptor(iface, i);
  282                 if (ed == NULL) {
  283                         printf("%s: couldn't get ep %d\n",
  284                             device_get_nameunit(sc->sc_dev), i);
  285                         return ENXIO;
  286                 }
  287                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  288                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
  289                         sc->sc_in = ed->bEndpointAddress;
  290                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
  291                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
  292                         sc->sc_out = ed->bEndpointAddress;
  293                 }
  294         }
  295         if (sc->sc_out == -1) {
  296                 printf("%s: could not find bulk out endpoint\n",
  297                     device_get_nameunit(sc->sc_dev));
  298                 sc->sc_dying = 1;
  299                 return ENXIO;
  300         }
  301 
  302         if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
  303                 /* This device doesn't handle reading properly. */
  304                 sc->sc_in = -1;
  305         }
  306 
  307         printf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev),
  308                sc->sc_in >= 0 ? "bi" : "uni");
  309 
  310         DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
  311 
  312         sc->sc_iface = iface;
  313         sc->sc_ifaceno = id->bInterfaceNumber;
  314         sc->sc_udev = dev;
  315 
  316 #if 0
  317 /*
  318  * This code is disabled because for some mysterious reason it causes
  319  * printing not to work.  But only sometimes, and mostly with
  320  * UHCI and less often with OHCI.  *sigh*
  321  */
  322         {
  323         usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
  324         usb_device_request_t req;
  325         int len, alen;
  326 
  327         req.bmRequestType = UT_READ_CLASS_INTERFACE;
  328         req.bRequest = UR_GET_DEVICE_ID;
  329         USETW(req.wValue, cd->bConfigurationValue);
  330         USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
  331         USETW(req.wLength, sizeof devinfo - 1);
  332         err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
  333                   &alen, USBD_DEFAULT_TIMEOUT);
  334         if (err) {
  335                 printf("%s: cannot get device id\n", device_get_nameunit(sc->sc_dev));
  336         } else if (alen <= 2) {
  337                 printf("%s: empty device id, no printer connected?\n",
  338                        device_get_nameunit(sc->sc_dev));
  339         } else {
  340                 /* devinfo now contains an IEEE-1284 device ID */
  341                 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
  342                 if (len > sizeof devinfo - 3)
  343                         len = sizeof devinfo - 3;
  344                 devinfo[len] = 0;
  345                 printf("%s: device id <", device_get_nameunit(sc->sc_dev));
  346                 ieee1284_print_id(devinfo+2);
  347                 printf(">\n");
  348         }
  349         }
  350 #endif
  351 
  352         sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
  353                 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
  354         sc->dev_noprime = make_dev(&ulpt_cdevsw,
  355                 device_get_unit(self)|ULPT_NOPRIME,
  356                 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
  357 
  358         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
  359 
  360         return 0;
  361 }
  362 
  363 
  364 static int
  365 ulpt_detach(device_t self)
  366 {
  367         struct ulpt_softc *sc = device_get_softc(self);
  368         int s;
  369 
  370         DPRINTF(("ulpt_detach: sc=%p\n", sc));
  371 
  372         sc->sc_dying = 1;
  373         if (sc->sc_out_pipe != NULL)
  374                 usbd_abort_pipe(sc->sc_out_pipe);
  375         if (sc->sc_in_pipe != NULL)
  376                 usbd_abort_pipe(sc->sc_in_pipe);
  377 
  378         s = splusb();
  379         if (--sc->sc_refcnt >= 0) {
  380                 /* There is noone to wake, aborting the pipe is enough */
  381                 /* Wait for processes to go away. */
  382                 usb_detach_wait(sc->sc_dev);
  383         }
  384         splx(s);
  385 
  386         destroy_dev(sc->dev);
  387         destroy_dev(sc->dev_noprime);
  388 
  389         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
  390 
  391         return (0);
  392 }
  393 
  394 int
  395 ulpt_status(struct ulpt_softc *sc)
  396 {
  397         usb_device_request_t req;
  398         usbd_status err;
  399         u_char status;
  400 
  401         req.bmRequestType = UT_READ_CLASS_INTERFACE;
  402         req.bRequest = UR_GET_PORT_STATUS;
  403         USETW(req.wValue, 0);
  404         USETW(req.wIndex, sc->sc_ifaceno);
  405         USETW(req.wLength, 1);
  406         err = usbd_do_request(sc->sc_udev, &req, &status);
  407         DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
  408         if (!err)
  409                 return (status);
  410         else
  411                 return (0);
  412 }
  413 
  414 void
  415 ulpt_reset(struct ulpt_softc *sc)
  416 {
  417         usb_device_request_t req;
  418 
  419         DPRINTFN(1, ("ulpt_reset\n"));
  420         req.bRequest = UR_SOFT_RESET;
  421         USETW(req.wValue, 0);
  422         USETW(req.wIndex, sc->sc_ifaceno);
  423         USETW(req.wLength, 0);
  424 
  425         /*
  426          * There was a mistake in the USB printer 1.0 spec that gave the
  427          * request type as UT_WRITE_CLASS_OTHER; it should have been
  428          * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
  429          * so we try both.
  430          */
  431         req.bmRequestType = UT_WRITE_CLASS_OTHER;
  432         if (usbd_do_request(sc->sc_udev, &req, 0)) {    /* 1.0 */
  433                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
  434                 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
  435         }
  436 }
  437 #if 0
  438 static void
  439 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
  440 {
  441         struct ulpt_softc *sc = priv;
  442         u_int32_t count;
  443 
  444         /* Don't loop on errors or 0-length input. */
  445         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
  446         if (status != USBD_NORMAL_COMPLETION || count == 0)
  447                 return;
  448 
  449         DPRINTFN(2,("ulpt_input: got some data\n"));
  450         /* Do it again. */
  451         if (xfer == sc->sc_in_xfer1)
  452                 usbd_transfer(sc->sc_in_xfer2);
  453         else
  454                 usbd_transfer(sc->sc_in_xfer1);
  455 }
  456 #endif
  457 
  458 int ulptusein = 1;
  459 
  460 /*
  461  * Reset the printer, then wait until it's selected and not busy.
  462  */
  463 int
  464 ulptopen(struct cdev *dev, int flag, int mode, struct thread *p)
  465 {
  466         u_char flags = ULPTFLAGS(dev);
  467         struct ulpt_softc *sc;
  468         usbd_status err;
  469         int error;
  470 
  471         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
  472         if (sc == NULL)
  473                 return (ENXIO);
  474 
  475         if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
  476                 return (ENXIO);
  477 
  478         if (sc->sc_state)
  479                 return (EBUSY);
  480 
  481         sc->sc_state = ULPT_INIT;
  482         sc->sc_flags = flags;
  483         DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
  484 
  485 #if defined(USB_DEBUG)
  486         /* Ignoring these flags might not be a good idea */
  487         if ((flags & ~ULPT_NOPRIME) != 0)
  488                 printf("ulptopen: flags ignored: %b\n", flags,
  489                         "\2\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
  490 #endif
  491 
  492 
  493         error = 0;
  494         sc->sc_refcnt++;
  495 
  496         if ((flags & ULPT_NOPRIME) == 0) {
  497                 ulpt_reset(sc);
  498                 if (sc->sc_dying) {
  499                         error = ENXIO;
  500                         sc->sc_state = 0;
  501                         goto done;
  502                 }
  503         }
  504 
  505         err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
  506         if (err) {
  507                 error = EIO;
  508                 goto err0;
  509         }
  510         sc->sc_out_xfer = usbd_alloc_xfer(sc->sc_udev);
  511         if (sc->sc_out_xfer == NULL) {
  512                 error = ENOMEM;
  513                 goto err1;
  514         }
  515         sc->sc_out_buf = usbd_alloc_buffer(sc->sc_out_xfer, ULPT_BSIZE);
  516         if (sc->sc_out_buf == NULL) {
  517                 error = ENOMEM;
  518                 goto err2;
  519         }
  520 
  521         if (ulptusein && sc->sc_in != -1) {
  522                 DPRINTF(("ulpt_open: open input pipe\n"));
  523                 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
  524                 if (err) {
  525                         error = EIO;
  526                         goto err2;
  527                 }
  528                 sc->sc_in_xfer = usbd_alloc_xfer(sc->sc_udev);
  529                 if (sc->sc_in_xfer == NULL) {
  530                         error = ENOMEM;
  531                         goto err3;
  532                 }
  533                 sc->sc_in_buf = usbd_alloc_buffer(sc->sc_in_xfer, ULPT_BSIZE);
  534                 if (sc->sc_in_buf == NULL) {
  535                         error = ENOMEM;
  536                         goto err4;
  537                 }
  538 
  539                 /* If it's not opened for read the set up a reader. */
  540                 if (!(flag & FREAD)) {
  541                         DPRINTF(("ulpt_open: start read callout\n"));
  542                         callout_init(&sc->sc_read_callout, 0);
  543                         callout_reset(&sc->sc_read_callout, hz/5, ulpt_tick,
  544                             sc);
  545                         sc->sc_has_callout = 1;
  546                 }
  547         }
  548 
  549         sc->sc_state = ULPT_OPEN;
  550         goto done;
  551 
  552  err4:
  553         usbd_free_xfer(sc->sc_in_xfer);
  554         sc->sc_in_xfer = NULL;
  555  err3:
  556         usbd_close_pipe(sc->sc_in_pipe);
  557         sc->sc_in_pipe = NULL;
  558  err2:
  559         usbd_free_xfer(sc->sc_out_xfer);
  560         sc->sc_out_xfer = NULL;
  561  err1:
  562         usbd_close_pipe(sc->sc_out_pipe);
  563         sc->sc_out_pipe = NULL;
  564  err0:
  565         sc->sc_state = 0;
  566 
  567  done:
  568         if (--sc->sc_refcnt < 0)
  569                 usb_detach_wakeup(sc->sc_dev);
  570 
  571         DPRINTF(("ulptopen: done, error=%d\n", error));
  572         return (error);
  573 }
  574 
  575 int
  576 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
  577 {
  578         u_char new;
  579 
  580         status = (status ^ LPS_INVERT) & LPS_MASK;
  581         new = status & ~sc->sc_laststatus;
  582         sc->sc_laststatus = status;
  583 
  584         if (new & LPS_SELECT)
  585                 log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev));
  586         else if (new & LPS_NOPAPER)
  587                 log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev));
  588         else if (new & LPS_NERR)
  589                 log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev));
  590 
  591         return (status);
  592 }
  593 
  594 int
  595 ulptclose(struct cdev *dev, int flag, int mode, struct thread *p)
  596 {
  597         struct ulpt_softc *sc;
  598 
  599         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
  600 
  601         if (sc->sc_state != ULPT_OPEN)
  602                 /* We are being forced to close before the open completed. */
  603                 return (0);
  604 
  605         if (sc->sc_has_callout) {
  606                 callout_stop(&sc->sc_read_callout);
  607                 sc->sc_has_callout = 0;
  608         }
  609 
  610         if (sc->sc_out_pipe != NULL) {
  611                 usbd_abort_pipe(sc->sc_out_pipe);
  612                 usbd_close_pipe(sc->sc_out_pipe);
  613                 sc->sc_out_pipe = NULL;
  614         }
  615         if (sc->sc_out_xfer != NULL) {
  616                 usbd_free_xfer(sc->sc_out_xfer);
  617                 sc->sc_out_xfer = NULL;
  618         }
  619 
  620         if (sc->sc_in_pipe != NULL) {
  621                 usbd_abort_pipe(sc->sc_in_pipe);
  622                 usbd_close_pipe(sc->sc_in_pipe);
  623                 sc->sc_in_pipe = NULL;
  624         }
  625         if (sc->sc_in_xfer != NULL) {
  626                 usbd_free_xfer(sc->sc_in_xfer);
  627                 sc->sc_in_xfer = NULL;
  628         }
  629 
  630         sc->sc_state = 0;
  631 
  632         DPRINTF(("ulptclose: closed\n"));
  633         return (0);
  634 }
  635 
  636 int
  637 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
  638 {
  639         u_int32_t n;
  640         int error = 0;
  641         void *bufp;
  642         usbd_xfer_handle xfer;
  643         usbd_status err;
  644 
  645         DPRINTF(("ulptwrite\n"));
  646         xfer = sc->sc_out_xfer;
  647         bufp = sc->sc_out_buf;
  648         while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
  649                 ulpt_statusmsg(ulpt_status(sc), sc);
  650                 error = uiomove(bufp, n, uio);
  651                 if (error)
  652                         break;
  653                 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
  654                 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
  655                           USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
  656                 if (err) {
  657                         DPRINTF(("ulptwrite: error=%d\n", err));
  658                         error = EIO;
  659                         break;
  660                 }
  661         }
  662 
  663         return (error);
  664 }
  665 
  666 int
  667 ulptwrite(struct cdev *dev, struct uio *uio, int flags)
  668 {
  669         struct ulpt_softc *sc;
  670         int error;
  671 
  672         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
  673 
  674         if (sc->sc_dying)
  675                 return (EIO);
  676 
  677         sc->sc_refcnt++;
  678         error = ulpt_do_write(sc, uio, flags);
  679         if (--sc->sc_refcnt < 0)
  680                 usb_detach_wakeup(sc->sc_dev);
  681         return (error);
  682 }
  683 
  684 int
  685 ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
  686 {
  687         u_int32_t n, on;
  688         int error = 0;
  689         void *bufp;
  690         usbd_xfer_handle xfer;
  691         usbd_status err;
  692 
  693         DPRINTF(("ulptread\n"));
  694 
  695         if (sc->sc_in_pipe == NULL)
  696                 return 0;
  697 
  698         xfer = sc->sc_in_xfer;
  699         bufp = sc->sc_in_buf;
  700         while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
  701                 DPRINTFN(1, ("ulptread: transfer %d bytes\n", n));
  702                 on = n;
  703                 err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
  704                           USBD_NO_COPY | USBD_SHORT_XFER_OK,
  705                           USBD_NO_TIMEOUT, bufp, &n, "ulptrd");
  706                 if (err) {
  707                         DPRINTF(("ulptread: error=%d\n", err));
  708                         error = EIO;
  709                         break;
  710                 }
  711                 error = uiomove(bufp, n, uio);
  712                 if (error)
  713                         break;
  714                 if (on != n)
  715                         break;
  716         }
  717 
  718         return (error);
  719 }
  720 
  721 int
  722 ulptread(struct cdev *dev, struct uio *uio, int flags)
  723 {
  724         struct ulpt_softc *sc;
  725         int error;
  726 
  727         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
  728 
  729         if (sc->sc_dying)
  730                 return (EIO);
  731 
  732         sc->sc_refcnt++;
  733         error = ulpt_do_read(sc, uio, flags);
  734         if (--sc->sc_refcnt < 0)
  735                 usb_detach_wakeup(sc->sc_dev);
  736         return (error);
  737 }
  738 
  739 void
  740 ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
  741              usbd_status status)
  742 {
  743         usbd_status err;
  744         u_int32_t n;
  745         usbd_private_handle xsc;
  746         struct ulpt_softc *sc;
  747 
  748         usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
  749         sc = xsc;
  750 
  751         DPRINTFN(1,("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
  752 
  753 #ifdef ULPT_DEBUG
  754         if (!err && n > 0)
  755                 DPRINTF(("ulpt_tick: discarding %d bytes\n", n));
  756 #endif
  757         if (!err || err == USBD_TIMEOUT)
  758                 callout_reset(&sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
  759                     ulpt_tick, sc);
  760 }
  761 
  762 void
  763 ulpt_tick(void *xsc)
  764 {
  765         struct ulpt_softc *sc = xsc;
  766         usbd_status err;
  767 
  768         if (sc == NULL || sc->sc_dying)
  769                 return;
  770 
  771         DPRINTFN(1,("ulpt_tick: start sc=%p\n", sc));
  772 
  773         usbd_setup_xfer(sc->sc_in_xfer, sc->sc_in_pipe, sc, sc->sc_in_buf,
  774                         ULPT_BSIZE, USBD_NO_COPY | USBD_SHORT_XFER_OK,
  775                         ULPT_READ_TIMO, ulpt_read_cb);
  776         err = usbd_transfer(sc->sc_in_xfer);
  777         DPRINTFN(1,("ulpt_tick: err=%d\n", err));
  778 }
  779 
  780 int
  781 ulptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *p)
  782 {
  783         int error = 0;
  784 
  785         switch (cmd) {
  786         default:
  787                 error = ENODEV;
  788         }
  789 
  790         return (error);
  791 }
  792 
  793 #if 0
  794 /* XXX This does not belong here. */
  795 /*
  796  * Print select parts of an IEEE 1284 device ID.
  797  */
  798 void
  799 ieee1284_print_id(char *str)
  800 {
  801         char *p, *q;
  802 
  803         for (p = str-1; p; p = strchr(p, ';')) {
  804                 p++;            /* skip ';' */
  805                 if (strncmp(p, "MFG:", 4) == 0 ||
  806                     strncmp(p, "MANUFACTURER:", 14) == 0 ||
  807                     strncmp(p, "MDL:", 4) == 0 ||
  808                     strncmp(p, "MODEL:", 6) == 0) {
  809                         q = strchr(p, ';');
  810                         if (q)
  811                                 printf("%.*s", (int)(q - p + 1), p);
  812                 }
  813         }
  814 }
  815 #endif

Cache object: 8eaf6d066071a1d4ec601a9880ddb5b3


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