Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.83
      1  1.83    plunky /*	$NetBSD: ulpt.c,v 1.83 2009/09/23 19:07:19 plunky Exp $	*/
      2  1.30  augustss /*	$FreeBSD: src/sys/dev/usb/ulpt.c,v 1.24 1999/11/17 22:33:44 n_hibma Exp $	*/
      3   1.1  augustss 
      4   1.1  augustss /*
      5  1.60  augustss  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
      6   1.1  augustss  * All rights reserved.
      7   1.1  augustss  *
      8   1.3  augustss  * This code is derived from software contributed to The NetBSD Foundation
      9  1.37  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10   1.3  augustss  * Carlstedt Research & Technology.
     11   1.1  augustss  *
     12   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13   1.1  augustss  * modification, are permitted provided that the following conditions
     14   1.1  augustss  * are met:
     15   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20   1.1  augustss  *
     21   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     32  1.11  augustss  */
     33  1.11  augustss 
     34  1.11  augustss /*
     35  1.40  augustss  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
     36   1.1  augustss  */
     37  1.44     lukem 
     38  1.44     lukem #include <sys/cdefs.h>
     39  1.83    plunky __KERNEL_RCSID(0, "$NetBSD: ulpt.c,v 1.83 2009/09/23 19:07:19 plunky Exp $");
     40  1.27  augustss 
     41   1.1  augustss #include <sys/param.h>
     42   1.1  augustss #include <sys/systm.h>
     43   1.1  augustss #include <sys/proc.h>
     44   1.1  augustss #include <sys/kernel.h>
     45  1.60  augustss #include <sys/fcntl.h>
     46  1.35  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
     47   1.8  augustss #include <sys/device.h>
     48   1.1  augustss #include <sys/ioctl.h>
     49   1.8  augustss #elif defined(__FreeBSD__)
     50   1.8  augustss #include <sys/ioccom.h>
     51   1.8  augustss #include <sys/module.h>
     52   1.8  augustss #include <sys/bus.h>
     53   1.8  augustss #endif
     54   1.1  augustss #include <sys/uio.h>
     55   1.1  augustss #include <sys/conf.h>
     56  1.12  augustss #include <sys/vnode.h>
     57   1.1  augustss #include <sys/syslog.h>
     58   1.1  augustss 
     59  1.60  augustss #include <machine/vmparam.h>	/* PAGE_SIZE */
     60  1.60  augustss 
     61   1.1  augustss #include <dev/usb/usb.h>
     62   1.1  augustss #include <dev/usb/usbdi.h>
     63   1.1  augustss #include <dev/usb/usbdi_util.h>
     64   1.1  augustss #include <dev/usb/usbdevs.h>
     65   1.1  augustss #include <dev/usb/usb_quirks.h>
     66   1.1  augustss 
     67   1.1  augustss #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     68   1.1  augustss #define	STEP		hz/4
     69   1.1  augustss 
     70   1.1  augustss #define	LPTPRI		(PZERO+8)
     71  1.60  augustss #define	ULPT_BSIZE	PAGE_SIZE
     72  1.60  augustss 
     73  1.60  augustss #define ULPT_READS_PER_SEC 5
     74  1.79       gdt /* XXX Why is 10 us a reasonable value? */
     75  1.60  augustss #define ULPT_READ_TIMO 10
     76   1.1  augustss 
     77  1.27  augustss #ifdef ULPT_DEBUG
     78  1.79       gdt #define DPRINTFN(n,x)	if (ulptdebug>=(n)) logprintf x
     79   1.1  augustss int	ulptdebug = 0;
     80  1.79       gdt /*
     81  1.79       gdt  * The strategy for debug levels is:
     82  1.79       gdt  *   1: attach-time operations
     83  1.79       gdt  *   2: open/close/status/reset
     84  1.79       gdt  *   3: read/write basic
     85  1.79       gdt  *   4: read/write details
     86  1.79       gdt  *  10: left over from previous debug code
     87  1.79       gdt  */
     88   1.1  augustss #else
     89   1.1  augustss #define DPRINTFN(n,x)
     90   1.1  augustss #endif
     91   1.1  augustss 
     92   1.1  augustss #define UR_GET_DEVICE_ID 0
     93   1.1  augustss #define UR_GET_PORT_STATUS 1
     94   1.1  augustss #define UR_SOFT_RESET 2
     95   1.1  augustss 
     96   1.1  augustss #define	LPS_NERR		0x08	/* printer no error */
     97   1.1  augustss #define	LPS_SELECT		0x10	/* printer selected */
     98   1.1  augustss #define	LPS_NOPAPER		0x20	/* printer out of paper */
     99   1.1  augustss #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
    100   1.1  augustss #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
    101   1.1  augustss 
    102   1.1  augustss struct ulpt_softc {
    103  1.20  augustss 	USBBASEDEVICE sc_dev;
    104   1.1  augustss 	usbd_device_handle sc_udev;	/* device */
    105   1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
    106   1.1  augustss 	int sc_ifaceno;
    107  1.42  augustss 
    108  1.42  augustss 	int sc_out;
    109  1.42  augustss 	usbd_pipe_handle sc_out_pipe;	/* bulk out pipe */
    110  1.60  augustss 	usbd_xfer_handle sc_out_xfer;
    111  1.60  augustss 	void *sc_out_buf;
    112  1.42  augustss 
    113  1.42  augustss 	int sc_in;
    114  1.42  augustss 	usbd_pipe_handle sc_in_pipe;	/* bulk in pipe */
    115  1.60  augustss 	usbd_xfer_handle sc_in_xfer;
    116  1.60  augustss 	void *sc_in_buf;
    117  1.60  augustss 
    118  1.79       gdt 	usb_callout_t sc_read_callout;	/* to drain input on write-only opens */
    119  1.60  augustss 	int sc_has_callout;
    120   1.1  augustss 
    121   1.1  augustss 	u_char sc_state;
    122   1.1  augustss #define	ULPT_OPEN	0x01	/* device is open */
    123   1.1  augustss #define	ULPT_OBUSY	0x02	/* printer is busy doing output */
    124   1.1  augustss #define	ULPT_INIT	0x04	/* waiting to initialize for open */
    125   1.1  augustss 	u_char sc_flags;
    126   1.1  augustss #define	ULPT_NOPRIME	0x40	/* don't prime on open */
    127   1.1  augustss 	u_char sc_laststatus;
    128  1.12  augustss 
    129  1.12  augustss 	int sc_refcnt;
    130  1.12  augustss 	u_char sc_dying;
    131  1.27  augustss 
    132  1.27  augustss #if defined(__FreeBSD__)
    133  1.27  augustss 	dev_t dev;
    134  1.27  augustss 	dev_t dev_noprime;
    135  1.27  augustss #endif
    136   1.1  augustss };
    137   1.1  augustss 
    138  1.52   gehenna #if defined(__NetBSD__)
    139  1.52   gehenna dev_type_open(ulptopen);
    140  1.52   gehenna dev_type_close(ulptclose);
    141  1.52   gehenna dev_type_write(ulptwrite);
    142  1.60  augustss dev_type_read(ulptread);
    143  1.52   gehenna dev_type_ioctl(ulptioctl);
    144  1.52   gehenna 
    145  1.52   gehenna const struct cdevsw ulpt_cdevsw = {
    146  1.60  augustss 	ulptopen, ulptclose, ulptread, ulptwrite, ulptioctl,
    147  1.74  christos 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
    148  1.52   gehenna };
    149  1.52   gehenna #elif defined(__OpenBSD__)
    150  1.28  augustss cdev_decl(ulpt);
    151  1.27  augustss #elif defined(__FreeBSD__)
    152  1.34  augustss Static d_open_t ulptopen;
    153  1.34  augustss Static d_close_t ulptclose;
    154  1.34  augustss Static d_write_t ulptwrite;
    155  1.34  augustss Static d_ioctl_t ulptioctl;
    156  1.27  augustss 
    157  1.27  augustss #define ULPT_CDEV_MAJOR 113
    158  1.27  augustss 
    159  1.34  augustss Static struct cdevsw ulpt_cdevsw = {
    160  1.27  augustss 	/* open */	ulptopen,
    161  1.27  augustss 	/* close */	ulptclose,
    162  1.27  augustss 	/* read */	noread,
    163  1.27  augustss 	/* write */	ulptwrite,
    164  1.27  augustss 	/* ioctl */	ulptioctl,
    165  1.27  augustss 	/* poll */	nopoll,
    166  1.27  augustss 	/* mmap */	nommap,
    167  1.27  augustss 	/* strategy */	nostrategy,
    168  1.27  augustss 	/* name */	"ulpt",
    169  1.27  augustss 	/* maj */	ULPT_CDEV_MAJOR,
    170  1.27  augustss 	/* dump */	nodump,
    171  1.27  augustss 	/* psize */	nopsize,
    172  1.27  augustss 	/* flags */	0,
    173  1.47  augustss #if !defined(__FreeBSD__) || (__FreeBSD__ < 5)
    174  1.27  augustss 	/* bmaj */	-1
    175  1.47  augustss #endif
    176  1.27  augustss };
    177  1.27  augustss #endif
    178  1.27  augustss 
    179  1.38  augustss void ulpt_disco(void *);
    180   1.1  augustss 
    181  1.38  augustss int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
    182  1.60  augustss int ulpt_do_read(struct ulpt_softc *, struct uio *uio, int);
    183  1.38  augustss int ulpt_status(struct ulpt_softc *);
    184  1.38  augustss void ulpt_reset(struct ulpt_softc *);
    185  1.38  augustss int ulpt_statusmsg(u_char, struct ulpt_softc *);
    186  1.60  augustss void ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    187  1.60  augustss 		  usbd_status status);
    188  1.60  augustss void ulpt_tick(void *xsc);
    189   1.1  augustss 
    190  1.42  augustss #if 0
    191  1.38  augustss void ieee1284_print_id(char *);
    192  1.42  augustss #endif
    193  1.16  augustss 
    194   1.1  augustss #define	ULPTUNIT(s)	(minor(s) & 0x1f)
    195   1.1  augustss #define	ULPTFLAGS(s)	(minor(s) & 0xe0)
    196   1.1  augustss 
    197  1.27  augustss 
    198   1.8  augustss USB_DECLARE_DRIVER(ulpt);
    199   1.1  augustss 
    200   1.8  augustss USB_MATCH(ulpt)
    201   1.1  augustss {
    202  1.78  drochner 	USB_IFMATCH_START(ulpt, uaa);
    203  1.50  augustss 
    204  1.79       gdt 	/* XXX Print something useful, or don't. */
    205   1.1  augustss 	DPRINTFN(10,("ulpt_match\n"));
    206  1.78  drochner 
    207  1.78  drochner 	if (uaa->class == UICLASS_PRINTER &&
    208  1.78  drochner 	    uaa->subclass == UISUBCLASS_PRINTER &&
    209  1.78  drochner 	    (uaa->proto == UIPROTO_PRINTER_UNI ||
    210  1.78  drochner 	     uaa->proto == UIPROTO_PRINTER_BI ||
    211  1.78  drochner 	     uaa->proto == UIPROTO_PRINTER_1284))
    212   1.1  augustss 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    213   1.1  augustss 	return (UMATCH_NONE);
    214   1.1  augustss }
    215   1.1  augustss 
    216   1.8  augustss USB_ATTACH(ulpt)
    217   1.1  augustss {
    218  1.78  drochner 	USB_IFATTACH_START(ulpt, sc, uaa);
    219   1.1  augustss 	usbd_device_handle dev = uaa->device;
    220   1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    221  1.42  augustss 	usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
    222  1.69  christos 	const usb_interface_descriptor_t *id;
    223  1.42  augustss 	usbd_status err;
    224  1.67  augustss 	char *devinfop;
    225   1.1  augustss 	usb_endpoint_descriptor_t *ed;
    226  1.42  augustss 	u_int8_t epcount;
    227  1.42  augustss 	int i, altno;
    228  1.65  augustss 	usbd_desc_iter_t iter;
    229  1.50  augustss 
    230  1.81      cube 	sc->sc_dev = self;
    231  1.81      cube 
    232  1.83    plunky 	aprint_naive("\n");
    233  1.83    plunky 	aprint_normal("\n");
    234  1.83    plunky 
    235  1.67  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    236  1.81      cube 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
    237  1.67  augustss 	       devinfop, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
    238  1.67  augustss 	usbd_devinfo_free(devinfop);
    239   1.1  augustss 
    240  1.65  augustss 	/* Loop through descriptors looking for a bidir mode. */
    241  1.65  augustss 	usb_desc_iter_init(dev, &iter);
    242  1.65  augustss 	for (altno = 0;;) {
    243  1.69  christos 		id = (const usb_interface_descriptor_t *)usb_desc_iter_next(&iter);
    244  1.65  augustss 		if (!id)
    245  1.65  augustss 			break;
    246  1.42  augustss 		if (id->bDescriptorType == UDESC_INTERFACE &&
    247  1.42  augustss 		    id->bInterfaceNumber == ifcd->bInterfaceNumber) {
    248  1.42  augustss 			if (id->bInterfaceClass == UICLASS_PRINTER &&
    249  1.42  augustss 			    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
    250  1.56  augustss 			    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
    251  1.56  augustss 			     id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
    252  1.42  augustss 				goto found;
    253  1.42  augustss 			altno++;
    254  1.66     perry 		}
    255  1.42  augustss 	}
    256  1.42  augustss 	id = ifcd;		/* not found, use original */
    257  1.42  augustss  found:
    258  1.42  augustss 	if (id != ifcd) {
    259  1.42  augustss 		/* Found a new bidir setting */
    260  1.79       gdt 		DPRINTFN(1, ("ulpt_attach: set altno = %d\n", altno));
    261  1.42  augustss 		err = usbd_set_interface(iface, altno);
    262  1.42  augustss 		if (err) {
    263  1.81      cube 			aprint_error_dev(self,
    264  1.81      cube 			    "setting alternate interface failed\n");
    265  1.42  augustss 			sc->sc_dying = 1;
    266  1.42  augustss 			USB_ATTACH_ERROR_RETURN;
    267  1.42  augustss 		}
    268   1.1  augustss 	}
    269   1.1  augustss 
    270  1.42  augustss 	epcount = 0;
    271  1.42  augustss 	(void)usbd_endpoint_count(iface, &epcount);
    272  1.42  augustss 
    273  1.42  augustss 	sc->sc_in = -1;
    274  1.42  augustss 	sc->sc_out = -1;
    275  1.42  augustss 	for (i = 0; i < epcount; i++) {
    276  1.42  augustss 		ed = usbd_interface2endpoint_descriptor(iface, i);
    277  1.42  augustss 		if (ed == NULL) {
    278  1.81      cube 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    279  1.42  augustss 			USB_ATTACH_ERROR_RETURN;
    280  1.42  augustss 		}
    281  1.42  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    282  1.42  augustss 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    283  1.42  augustss 			sc->sc_in = ed->bEndpointAddress;
    284  1.42  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    285  1.42  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    286  1.42  augustss 			sc->sc_out = ed->bEndpointAddress;
    287  1.42  augustss 		}
    288  1.42  augustss 	}
    289  1.42  augustss 	if (sc->sc_out == -1) {
    290  1.81      cube 		aprint_error_dev(self, "could not find bulk out endpoint\n");
    291  1.12  augustss 		sc->sc_dying = 1;
    292   1.8  augustss 		USB_ATTACH_ERROR_RETURN;
    293  1.12  augustss 	}
    294  1.42  augustss 
    295  1.42  augustss 	if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
    296  1.42  augustss 		/* This device doesn't handle reading properly. */
    297  1.42  augustss 		sc->sc_in = -1;
    298  1.42  augustss 	}
    299  1.51  augustss 
    300  1.81      cube 	aprint_normal_dev(self, "using %s-directional mode\n",
    301  1.51  augustss 	       sc->sc_in >= 0 ? "bi" : "uni");
    302  1.42  augustss 
    303  1.42  augustss 	sc->sc_iface = iface;
    304   1.1  augustss 	sc->sc_ifaceno = id->bInterfaceNumber;
    305  1.42  augustss 	sc->sc_udev = dev;
    306   1.1  augustss 
    307  1.22  augustss #if 0
    308  1.22  augustss /*
    309  1.36  augustss  * This code is disabled because for some mysterious reason it causes
    310  1.22  augustss  * printing not to work.  But only sometimes, and mostly with
    311  1.22  augustss  * UHCI and less often with OHCI.  *sigh*
    312  1.22  augustss  */
    313  1.22  augustss 	{
    314  1.22  augustss 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    315  1.22  augustss 	usb_device_request_t req;
    316  1.22  augustss 	int len, alen;
    317  1.22  augustss 
    318   1.1  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    319   1.1  augustss 	req.bRequest = UR_GET_DEVICE_ID;
    320   1.1  augustss 	USETW(req.wValue, cd->bConfigurationValue);
    321   1.1  augustss 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    322  1.68     soren 	USETW(req.wLength, DEVINFOSIZE - 1);
    323  1.68     soren 	err = usbd_do_request_flags(dev, &req, devinfop, USBD_SHORT_XFER_OK,
    324  1.48  augustss 		  &alen, USBD_DEFAULT_TIMEOUT);
    325  1.28  augustss 	if (err) {
    326  1.16  augustss 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    327  1.16  augustss 	} else if (alen <= 2) {
    328  1.16  augustss 		printf("%s: empty device id, no printer connected?\n",
    329  1.16  augustss 		       USBDEVNAME(sc->sc_dev));
    330  1.16  augustss 	} else {
    331  1.68     soren 		/* devinfop now contains an IEEE-1284 device ID */
    332  1.68     soren 		len = ((devinfop[0] & 0xff) << 8) | (devinfop[1] & 0xff);
    333  1.68     soren 		if (len > DEVINFOSIZE - 3)
    334  1.68     soren 			len = DEVINFOSIZE - 3;
    335  1.68     soren 		devinfop[len] = 0;
    336  1.16  augustss 		printf("%s: device id <", USBDEVNAME(sc->sc_dev));
    337  1.68     soren 		ieee1284_print_id(devinfop+2);
    338  1.16  augustss 		printf(">\n");
    339   1.1  augustss 	}
    340  1.22  augustss 	}
    341  1.22  augustss #endif
    342   1.1  augustss 
    343  1.27  augustss #if defined(__FreeBSD__)
    344  1.27  augustss 	sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
    345  1.27  augustss 		UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
    346  1.27  augustss 	sc->dev_noprime = make_dev(&ulpt_cdevsw,
    347  1.27  augustss 		device_get_unit(self)|ULPT_NOPRIME,
    348  1.27  augustss 		UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
    349  1.27  augustss #endif
    350  1.27  augustss 
    351  1.31  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    352  1.31  augustss 			   USBDEV(sc->sc_dev));
    353  1.31  augustss 
    354  1.79       gdt 	DPRINTFN(1, ("ulpt_attach: sc=%p in=%d out=%d\n",
    355  1.79       gdt 		     sc, sc->sc_out, sc->sc_in));
    356  1.79       gdt 
    357   1.8  augustss 	USB_ATTACH_SUCCESS_RETURN;
    358   1.1  augustss }
    359   1.1  augustss 
    360  1.27  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    361   1.1  augustss int
    362  1.38  augustss ulpt_activate(device_ptr_t self, enum devact act)
    363  1.12  augustss {
    364  1.81      cube 	struct ulpt_softc *sc = device_private(self);
    365  1.17  augustss 
    366  1.17  augustss 	switch (act) {
    367  1.17  augustss 	case DVACT_ACTIVATE:
    368  1.17  augustss 		return (EOPNOTSUPP);
    369  1.17  augustss 
    370  1.17  augustss 	case DVACT_DEACTIVATE:
    371  1.17  augustss 		sc->sc_dying = 1;
    372  1.17  augustss 		break;
    373  1.17  augustss 	}
    374  1.12  augustss 	return (0);
    375  1.12  augustss }
    376  1.27  augustss #endif
    377  1.12  augustss 
    378  1.27  augustss USB_DETACH(ulpt)
    379  1.12  augustss {
    380  1.27  augustss 	USB_DETACH_START(ulpt, sc);
    381  1.27  augustss 	int s;
    382  1.27  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    383  1.12  augustss 	int maj, mn;
    384  1.47  augustss #elif defined(__FreeBSD__)
    385  1.47  augustss 	struct vnode *vp;
    386  1.47  augustss #endif
    387  1.12  augustss 
    388  1.79       gdt 	DPRINTFN(1, ("ulpt_detach: sc=%p\n", sc));
    389  1.12  augustss 
    390  1.12  augustss 	sc->sc_dying = 1;
    391  1.42  augustss 	if (sc->sc_out_pipe != NULL)
    392  1.42  augustss 		usbd_abort_pipe(sc->sc_out_pipe);
    393  1.42  augustss 	if (sc->sc_in_pipe != NULL)
    394  1.42  augustss 		usbd_abort_pipe(sc->sc_in_pipe);
    395  1.12  augustss 
    396  1.12  augustss 	s = splusb();
    397  1.12  augustss 	if (--sc->sc_refcnt >= 0) {
    398  1.12  augustss 		/* There is noone to wake, aborting the pipe is enough */
    399  1.12  augustss 		/* Wait for processes to go away. */
    400  1.20  augustss 		usb_detach_wait(USBDEV(sc->sc_dev));
    401  1.12  augustss 	}
    402  1.12  augustss 	splx(s);
    403  1.12  augustss 
    404  1.27  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    405  1.12  augustss 	/* locate the major number */
    406  1.52   gehenna #if defined(__NetBSD__)
    407  1.52   gehenna 	maj = cdevsw_lookup_major(&ulpt_cdevsw);
    408  1.52   gehenna #elif defined(__OpenBSD__)
    409  1.12  augustss 	for (maj = 0; maj < nchrdev; maj++)
    410  1.12  augustss 		if (cdevsw[maj].d_open == ulptopen)
    411  1.12  augustss 			break;
    412  1.52   gehenna #endif
    413  1.12  augustss 
    414  1.12  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    415  1.72   thorpej 	mn = device_unit(self);
    416  1.12  augustss 	vdevgone(maj, mn, mn, VCHR);
    417  1.57       scw 	vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
    418  1.27  augustss #elif defined(__FreeBSD__)
    419  1.47  augustss 	vp = SLIST_FIRST(&sc->dev->si_hlist);
    420  1.47  augustss 	if (vp)
    421  1.47  augustss 		VOP_REVOKE(vp, REVOKEALL);
    422  1.47  augustss 	vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
    423  1.47  augustss 	if (vp)
    424  1.47  augustss 		VOP_REVOKE(vp, REVOKEALL);
    425  1.27  augustss 
    426  1.30  augustss 	destroy_dev(sc->dev);
    427  1.30  augustss 	destroy_dev(sc->dev_noprime);
    428  1.27  augustss #endif
    429  1.31  augustss 
    430  1.31  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    431  1.31  augustss 			   USBDEV(sc->sc_dev));
    432  1.12  augustss 
    433  1.12  augustss 	return (0);
    434  1.12  augustss }
    435  1.12  augustss 
    436  1.12  augustss int
    437  1.38  augustss ulpt_status(struct ulpt_softc *sc)
    438   1.1  augustss {
    439   1.1  augustss 	usb_device_request_t req;
    440  1.28  augustss 	usbd_status err;
    441   1.1  augustss 	u_char status;
    442   1.1  augustss 
    443   1.1  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    444   1.1  augustss 	req.bRequest = UR_GET_PORT_STATUS;
    445   1.1  augustss 	USETW(req.wValue, 0);
    446   1.1  augustss 	USETW(req.wIndex, sc->sc_ifaceno);
    447   1.1  augustss 	USETW(req.wLength, 1);
    448  1.28  augustss 	err = usbd_do_request(sc->sc_udev, &req, &status);
    449  1.79       gdt 	DPRINTFN(2, ("ulpt_status: status=0x%02x err=%d\n", status, err));
    450  1.28  augustss 	if (!err)
    451   1.1  augustss 		return (status);
    452   1.1  augustss 	else
    453   1.1  augustss 		return (0);
    454   1.1  augustss }
    455   1.1  augustss 
    456   1.1  augustss void
    457  1.38  augustss ulpt_reset(struct ulpt_softc *sc)
    458   1.1  augustss {
    459   1.1  augustss 	usb_device_request_t req;
    460   1.1  augustss 
    461  1.79       gdt 	DPRINTFN(2, ("ulpt_reset\n"));
    462   1.1  augustss 	req.bRequest = UR_SOFT_RESET;
    463   1.1  augustss 	USETW(req.wValue, 0);
    464   1.1  augustss 	USETW(req.wIndex, sc->sc_ifaceno);
    465   1.1  augustss 	USETW(req.wLength, 0);
    466  1.41  augustss 
    467  1.41  augustss 	/*
    468  1.41  augustss 	 * There was a mistake in the USB printer 1.0 spec that gave the
    469  1.42  augustss 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
    470  1.41  augustss 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
    471  1.41  augustss 	 * so we try both.
    472  1.41  augustss 	 */
    473  1.49  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    474  1.42  augustss 	if (usbd_do_request(sc->sc_udev, &req, 0)) {	/* 1.0 */
    475  1.41  augustss 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    476  1.42  augustss 		(void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
    477  1.41  augustss 	}
    478   1.1  augustss }
    479   1.1  augustss 
    480  1.42  augustss int ulptusein = 1;
    481  1.42  augustss 
    482   1.1  augustss /*
    483   1.1  augustss  * Reset the printer, then wait until it's selected and not busy.
    484   1.1  augustss  */
    485   1.1  augustss int
    486  1.76  christos ulptopen(dev_t dev, int flag, int mode, struct lwp *l)
    487   1.1  augustss {
    488   1.1  augustss 	u_char flags = ULPTFLAGS(dev);
    489  1.26  augustss 	struct ulpt_softc *sc;
    490  1.28  augustss 	usbd_status err;
    491   1.1  augustss 	int spin, error;
    492  1.26  augustss 
    493   1.8  augustss 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    494   1.1  augustss 
    495  1.30  augustss 	if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
    496  1.13  augustss 		return (ENXIO);
    497   1.1  augustss 
    498   1.1  augustss 	if (sc->sc_state)
    499  1.13  augustss 		return (EBUSY);
    500   1.1  augustss 
    501   1.1  augustss 	sc->sc_state = ULPT_INIT;
    502   1.1  augustss 	sc->sc_flags = flags;
    503  1.79       gdt 	DPRINTFN(2, ("ulptopen: flags=0x%x\n", (unsigned)flags));
    504   1.1  augustss 
    505  1.27  augustss #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
    506  1.27  augustss 	/* Ignoring these flags might not be a good idea */
    507  1.27  augustss 	if ((flags & ~ULPT_NOPRIME) != 0)
    508  1.27  augustss 		printf("ulptopen: flags ignored: %b\n", flags,
    509  1.27  augustss 			"\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
    510  1.27  augustss #endif
    511  1.27  augustss 
    512  1.27  augustss 
    513  1.39  augustss 	error = 0;
    514  1.39  augustss 	sc->sc_refcnt++;
    515  1.39  augustss 
    516  1.64   mycroft #if 0 /* XXX causes some printers to disconnect */
    517   1.1  augustss 	if ((flags & ULPT_NOPRIME) == 0)
    518   1.1  augustss 		ulpt_reset(sc);
    519  1.64   mycroft #endif
    520   1.1  augustss 
    521   1.1  augustss 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    522  1.79       gdt 		DPRINTFN(2, ("ulpt_open: waiting a while\n"));
    523   1.1  augustss 		if (spin >= TIMEOUT) {
    524  1.39  augustss 			error = EBUSY;
    525   1.1  augustss 			sc->sc_state = 0;
    526  1.39  augustss 			goto done;
    527   1.1  augustss 		}
    528   1.1  augustss 
    529   1.1  augustss 		/* wait 1/4 second, give up if we get a signal */
    530  1.77  christos 		error = tsleep((void *)sc, LPTPRI | PCATCH, "ulptop", STEP);
    531   1.1  augustss 		if (error != EWOULDBLOCK) {
    532   1.1  augustss 			sc->sc_state = 0;
    533  1.39  augustss 			goto done;
    534  1.39  augustss 		}
    535  1.39  augustss 
    536  1.39  augustss 		if (sc->sc_dying) {
    537  1.39  augustss 			error = ENXIO;
    538  1.39  augustss 			sc->sc_state = 0;
    539  1.39  augustss 			goto done;
    540   1.1  augustss 		}
    541   1.1  augustss 	}
    542   1.1  augustss 
    543  1.42  augustss 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
    544  1.28  augustss 	if (err) {
    545  1.39  augustss 		error = EIO;
    546  1.60  augustss 		goto err0;
    547  1.60  augustss 	}
    548  1.60  augustss 	sc->sc_out_xfer = usbd_alloc_xfer(sc->sc_udev);
    549  1.60  augustss 	if (sc->sc_out_xfer == NULL) {
    550  1.60  augustss 		error = ENOMEM;
    551  1.60  augustss 		goto err1;
    552  1.60  augustss 	}
    553  1.60  augustss 	sc->sc_out_buf = usbd_alloc_buffer(sc->sc_out_xfer, ULPT_BSIZE);
    554  1.60  augustss 	if (sc->sc_out_buf == NULL) {
    555  1.60  augustss 		error = ENOMEM;
    556  1.60  augustss 		goto err2;
    557   1.1  augustss 	}
    558  1.60  augustss 
    559  1.42  augustss 	if (ulptusein && sc->sc_in != -1) {
    560  1.79       gdt 		DPRINTFN(2, ("ulpt_open: opening input pipe %d\n", sc->sc_in));
    561  1.42  augustss 		err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
    562  1.42  augustss 		if (err) {
    563  1.42  augustss 			error = EIO;
    564  1.60  augustss 			goto err2;
    565  1.60  augustss 		}
    566  1.60  augustss 		sc->sc_in_xfer = usbd_alloc_xfer(sc->sc_udev);
    567  1.60  augustss 		if (sc->sc_in_xfer == NULL) {
    568  1.60  augustss 			error = ENOMEM;
    569  1.60  augustss 			goto err3;
    570  1.42  augustss 		}
    571  1.60  augustss 		sc->sc_in_buf = usbd_alloc_buffer(sc->sc_in_xfer, ULPT_BSIZE);
    572  1.60  augustss 		if (sc->sc_in_buf == NULL) {
    573  1.42  augustss 			error = ENOMEM;
    574  1.60  augustss 			goto err4;
    575  1.60  augustss 		}
    576  1.60  augustss 
    577  1.68     soren 		/* If it's not opened for read then set up a reader. */
    578  1.70    rpaulo 		if (!(flag & FREAD)) {
    579  1.79       gdt 			DPRINTFN(2, ("ulpt_open: start read callout\n"));
    580  1.60  augustss 			usb_callout_init(sc->sc_read_callout);
    581  1.60  augustss 			usb_callout(sc->sc_read_callout, hz/5, ulpt_tick, sc);
    582  1.60  augustss 			sc->sc_has_callout = 1;
    583  1.42  augustss 		}
    584  1.42  augustss 	}
    585   1.1  augustss 
    586   1.1  augustss 	sc->sc_state = ULPT_OPEN;
    587  1.60  augustss 	goto done;
    588  1.60  augustss 
    589  1.60  augustss  err4:
    590  1.60  augustss 	usbd_free_xfer(sc->sc_in_xfer);
    591  1.60  augustss 	sc->sc_in_xfer = NULL;
    592  1.60  augustss  err3:
    593  1.60  augustss 	usbd_close_pipe(sc->sc_in_pipe);
    594  1.60  augustss 	sc->sc_in_pipe = NULL;
    595  1.60  augustss  err2:
    596  1.60  augustss 	usbd_free_xfer(sc->sc_out_xfer);
    597  1.60  augustss 	sc->sc_out_xfer = NULL;
    598  1.60  augustss  err1:
    599  1.60  augustss 	usbd_close_pipe(sc->sc_out_pipe);
    600  1.60  augustss 	sc->sc_out_pipe = NULL;
    601  1.60  augustss  err0:
    602  1.60  augustss 	sc->sc_state = 0;
    603   1.1  augustss 
    604  1.39  augustss  done:
    605  1.39  augustss 	if (--sc->sc_refcnt < 0)
    606  1.39  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    607  1.39  augustss 
    608  1.79       gdt 	DPRINTFN(2, ("ulptopen: done, error=%d\n", error));
    609  1.39  augustss 	return (error);
    610   1.1  augustss }
    611   1.1  augustss 
    612  1.79       gdt /*
    613  1.79       gdt  * XXX Document return value semantics.
    614  1.79       gdt  */
    615   1.1  augustss int
    616  1.38  augustss ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
    617   1.1  augustss {
    618   1.1  augustss 	u_char new;
    619   1.1  augustss 
    620   1.1  augustss 	status = (status ^ LPS_INVERT) & LPS_MASK;
    621   1.1  augustss 	new = status & ~sc->sc_laststatus;
    622   1.1  augustss 	sc->sc_laststatus = status;
    623   1.1  augustss 
    624   1.1  augustss 	if (new & LPS_SELECT)
    625   1.8  augustss 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    626  1.79       gdt 	if (new & LPS_NOPAPER)
    627   1.8  augustss 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    628  1.79       gdt 	if (new & LPS_NERR)
    629   1.8  augustss 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    630   1.1  augustss 
    631  1.13  augustss 	return (status);
    632   1.1  augustss }
    633   1.1  augustss 
    634   1.1  augustss int
    635  1.76  christos ulptclose(dev_t dev, int flag, int mode,
    636  1.76  christos     struct lwp *l)
    637   1.1  augustss {
    638  1.26  augustss 	struct ulpt_softc *sc;
    639  1.26  augustss 
    640   1.8  augustss 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    641  1.13  augustss 
    642  1.13  augustss 	if (sc->sc_state != ULPT_OPEN)
    643  1.13  augustss 		/* We are being forced to close before the open completed. */
    644  1.13  augustss 		return (0);
    645   1.1  augustss 
    646  1.60  augustss 	if (sc->sc_has_callout) {
    647  1.79       gdt 		DPRINTFN(2, ("ulptclose: stopping read callout\n"));
    648  1.60  augustss 		usb_uncallout(sc->sc_read_callout, ulpt_tick, sc);
    649  1.60  augustss 		sc->sc_has_callout = 0;
    650  1.60  augustss 	}
    651  1.60  augustss 
    652  1.42  augustss 	if (sc->sc_out_pipe != NULL) {
    653  1.60  augustss 		usbd_abort_pipe(sc->sc_out_pipe);
    654  1.42  augustss 		usbd_close_pipe(sc->sc_out_pipe);
    655  1.42  augustss 		sc->sc_out_pipe = NULL;
    656  1.42  augustss 	}
    657  1.60  augustss 	if (sc->sc_out_xfer != NULL) {
    658  1.60  augustss 		usbd_free_xfer(sc->sc_out_xfer);
    659  1.60  augustss 		sc->sc_out_xfer = NULL;
    660  1.60  augustss 	}
    661  1.66     perry 
    662  1.42  augustss 	if (sc->sc_in_pipe != NULL) {
    663  1.42  augustss 		usbd_abort_pipe(sc->sc_in_pipe);
    664  1.42  augustss 		usbd_close_pipe(sc->sc_in_pipe);
    665  1.42  augustss 		sc->sc_in_pipe = NULL;
    666  1.60  augustss 	}
    667  1.60  augustss 	if (sc->sc_in_xfer != NULL) {
    668  1.60  augustss 		usbd_free_xfer(sc->sc_in_xfer);
    669  1.60  augustss 		sc->sc_in_xfer = NULL;
    670  1.42  augustss 	}
    671   1.1  augustss 
    672   1.1  augustss 	sc->sc_state = 0;
    673   1.1  augustss 
    674  1.79       gdt 	DPRINTFN(2, ("ulptclose: closed\n"));
    675   1.1  augustss 	return (0);
    676   1.1  augustss }
    677   1.1  augustss 
    678   1.1  augustss int
    679  1.76  christos ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
    680   1.1  augustss {
    681  1.15  augustss 	u_int32_t n;
    682   1.1  augustss 	int error = 0;
    683  1.23  augustss 	void *bufp;
    684  1.28  augustss 	usbd_xfer_handle xfer;
    685  1.28  augustss 	usbd_status err;
    686   1.1  augustss 
    687  1.79       gdt 	DPRINTFN(3, ("ulptwrite\n"));
    688  1.60  augustss 	xfer = sc->sc_out_xfer;
    689  1.60  augustss 	bufp = sc->sc_out_buf;
    690   1.1  augustss 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    691   1.1  augustss 		ulpt_statusmsg(ulpt_status(sc), sc);
    692  1.23  augustss 		error = uiomove(bufp, n, uio);
    693   1.6  augustss 		if (error)
    694   1.6  augustss 			break;
    695  1.79       gdt 		DPRINTFN(4, ("ulptwrite: transfer %d bytes\n", n));
    696  1.50  augustss 		err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
    697  1.28  augustss 			  USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
    698  1.28  augustss 		if (err) {
    699  1.79       gdt 			DPRINTFN(3, ("ulptwrite: error=%d\n", err));
    700   1.1  augustss 			error = EIO;
    701   1.1  augustss 			break;
    702   1.1  augustss 		}
    703   1.1  augustss 	}
    704  1.12  augustss 
    705  1.12  augustss 	return (error);
    706  1.12  augustss }
    707  1.12  augustss 
    708  1.12  augustss int
    709  1.38  augustss ulptwrite(dev_t dev, struct uio *uio, int flags)
    710  1.12  augustss {
    711  1.26  augustss 	struct ulpt_softc *sc;
    712  1.26  augustss 	int error;
    713  1.26  augustss 
    714  1.12  augustss 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    715  1.21  augustss 
    716  1.21  augustss 	if (sc->sc_dying)
    717  1.21  augustss 		return (EIO);
    718  1.12  augustss 
    719  1.12  augustss 	sc->sc_refcnt++;
    720  1.12  augustss 	error = ulpt_do_write(sc, uio, flags);
    721  1.12  augustss 	if (--sc->sc_refcnt < 0)
    722  1.20  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    723   1.1  augustss 	return (error);
    724   1.1  augustss }
    725   1.1  augustss 
    726  1.79       gdt /*
    727  1.79       gdt  * Perform a read operation according to the given uio.
    728  1.79       gdt  * This should respect nonblocking I/O status.
    729  1.79       gdt  *
    730  1.79       gdt  * XXX Doing a short read when more data is available seems to be
    731  1.79       gdt  * problematic.  See
    732  1.79       gdt  * http://www.freebsd.org/cgi/query-pr.cgi?pr=91538&cat= for a fix.
    733  1.79       gdt  * However, this will be unnecessary given a proper fix for the next
    734  1.79       gdt  * problem, and most actual callers read a lot.
    735  1.79       gdt  *
    736  1.79       gdt  * XXX This code should interact properly with select/poll, and that
    737  1.79       gdt  * requires the USB transactions to be queued and function before the
    738  1.79       gdt  * user does a read.  Read will then consume data from a buffer, and
    739  1.79       gdt  * not interact with the device. See ucom.c for an example of how to
    740  1.79       gdt  * do this.
    741  1.79       gdt  */
    742   1.1  augustss int
    743  1.76  christos ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
    744  1.60  augustss {
    745  1.79       gdt 	u_int32_t n, nread, nreq;
    746  1.79       gdt 	int error = 0, nonblocking, timeout;
    747  1.60  augustss 	void *bufp;
    748  1.60  augustss 	usbd_xfer_handle xfer;
    749  1.79       gdt 	usbd_status err = USBD_NORMAL_COMPLETION;
    750  1.60  augustss 
    751  1.79       gdt 	/* XXX Resolve with background reader process.  KASSERT? */
    752  1.79       gdt 	if (sc->sc_in_pipe == NULL)
    753  1.79       gdt 		return EIO;
    754  1.60  augustss 
    755  1.79       gdt 	if (flags & IO_NDELAY)
    756  1.79       gdt 		nonblocking = 1;
    757  1.79       gdt 	else
    758  1.79       gdt 		nonblocking = 0;
    759  1.79       gdt 
    760  1.79       gdt 	if (nonblocking)
    761  1.79       gdt 		timeout = USBD_DEFAULT_TIMEOUT; /* 5 ms */
    762  1.79       gdt 	else
    763  1.79       gdt 		timeout = USBD_NO_TIMEOUT;
    764  1.79       gdt 
    765  1.82    bouyer 	DPRINTFN(3, ("ulptread nonblocking=%d uio_reside=%ld timeout=%d\n",
    766  1.82    bouyer 		     nonblocking, (u_long)uio->uio_resid, timeout));
    767  1.60  augustss 
    768  1.60  augustss 	xfer = sc->sc_in_xfer;
    769  1.60  augustss 	bufp = sc->sc_in_buf;
    770  1.79       gdt 	nread = 0;
    771  1.79       gdt 	while ((nreq = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    772  1.79       gdt 		KASSERT(error == 0);
    773  1.79       gdt 		if (error != 0) {
    774  1.79       gdt 			printf("ulptread: pre-switch error %d != 0", error);
    775  1.79       gdt 			goto done;
    776  1.79       gdt 		}
    777  1.79       gdt 
    778  1.79       gdt 		/*
    779  1.79       gdt 		 * XXX Even with the short timeout, this will tsleep,
    780  1.79       gdt 		 * but it should be adequately prompt in practice.
    781  1.79       gdt 		 */
    782  1.79       gdt 		n = nreq;
    783  1.79       gdt 		DPRINTFN(4, ("ulptread: transfer %d bytes, nonblocking=%d timeout=%d\n",
    784  1.79       gdt 			     n, nonblocking, timeout));
    785  1.60  augustss 		err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
    786  1.60  augustss 			  USBD_NO_COPY | USBD_SHORT_XFER_OK,
    787  1.79       gdt 			  timeout, bufp, &n, "ulptrd");
    788  1.79       gdt 
    789  1.79       gdt 		DPRINTFN(4, ("ulptread: transfer complete nreq %d n %d nread %d err %d\n",
    790  1.79       gdt 			     nreq, n, nread, err));
    791  1.79       gdt 		/*
    792  1.79       gdt 		 * Process "err" return, jumping to done if we set "error".
    793  1.79       gdt 		 */
    794  1.79       gdt 		switch (err) {
    795  1.79       gdt 		case USBD_NORMAL_COMPLETION:
    796  1.79       gdt 			if (n == 0) {
    797  1.79       gdt 				DPRINTFN(3, ("ulptread: NORMAL n==0\n"));
    798  1.79       gdt 			}
    799  1.79       gdt 			break;
    800  1.79       gdt 
    801  1.79       gdt 		case USBD_SHORT_XFER:
    802  1.79       gdt 			/* We said SHORT_XFER_OK, so shouldn't happen. */
    803  1.79       gdt 			DPRINTFN(3, ("ulptread: SHORT n=%d\n", n));
    804  1.79       gdt 			break;
    805  1.79       gdt 
    806  1.79       gdt 		case USBD_TIMEOUT:
    807  1.79       gdt 			if (nonblocking == 0) {
    808  1.79       gdt 				/* XXX Cannot happen; perhaps KASSERT. */
    809  1.79       gdt 				printf("ulptread: timeout in blocking mode\n");
    810  1.79       gdt 				error = EIO;
    811  1.79       gdt 				goto done;
    812  1.79       gdt 			}
    813  1.79       gdt 
    814  1.79       gdt 			DPRINTFN(3, ("ulptread: TIMEOUT n %d nread %d error %d\n",
    815  1.79       gdt 				     n, nread, error));
    816  1.79       gdt 			/*
    817  1.79       gdt 			 * Don't set error until we understand why
    818  1.79       gdt 			 * this happens.
    819  1.79       gdt 			 */
    820  1.79       gdt 			break;
    821  1.79       gdt 
    822  1.79       gdt 		case USBD_INTERRUPTED:
    823  1.79       gdt 			/*
    824  1.79       gdt 			 * The tsleep in usbd_bulk_transfer was
    825  1.79       gdt 			 * interrupted.  Reflect it to the caller so
    826  1.79       gdt 			 * that reading can be interrupted.
    827  1.79       gdt 			 */
    828  1.79       gdt 			error = EINTR;
    829  1.79       gdt 			DPRINTFN(3, ("ulptread: EINTR error %d\n", error));
    830  1.79       gdt 			goto done;
    831  1.79       gdt 			break;
    832  1.79       gdt 
    833  1.79       gdt 		default:
    834  1.79       gdt 			/* Assume all other return codes are really errors. */
    835  1.60  augustss 			error = EIO;
    836  1.79       gdt 			DPRINTFN(3, ("ulptread: n %d err %d error %d\n",
    837  1.79       gdt 				     n, err, error));
    838  1.79       gdt 			goto done;
    839  1.60  augustss 			break;
    840  1.60  augustss 		}
    841  1.79       gdt 		/* XXX KASSERT */
    842  1.79       gdt 		if (error != 0) {
    843  1.79       gdt 			printf("ulptread: post-switch error %d != 0", error);
    844  1.79       gdt 			goto done;
    845  1.79       gdt 		}
    846  1.79       gdt 
    847  1.79       gdt 		if (n > 0) {
    848  1.79       gdt 			/*
    849  1.79       gdt 			 * Record progress to enable later choosing
    850  1.79       gdt 			 * between short reads and EWOULDBLOCK.
    851  1.79       gdt 			 */
    852  1.79       gdt 			nread += n;
    853  1.79       gdt 
    854  1.79       gdt 			/* Copy to userspace, giving up on any error. */
    855  1.79       gdt 			error = uiomove(bufp, n, uio);
    856  1.79       gdt 			if (error != 0)
    857  1.79       gdt 				break;
    858  1.79       gdt 		} else {
    859  1.79       gdt 			/*
    860  1.79       gdt 			 * We read 0 bytes, and therefore are done,
    861  1.79       gdt 			 * even if we aren't in nonblocking mode.
    862  1.79       gdt 			 */
    863  1.79       gdt 			if (error == 0 && nread == 0)
    864  1.79       gdt 				error = EWOULDBLOCK;
    865  1.79       gdt 			DPRINTFN(3, ("ulptread: read 0=>done error %d\n",
    866  1.79       gdt 				     error));
    867  1.79       gdt 			goto done;
    868  1.79       gdt 		}
    869  1.79       gdt 
    870  1.79       gdt 		/*
    871  1.79       gdt 		 * A short transfer indicates no more data will be
    872  1.79       gdt 		 * forthcoming.  Terminate this read regardless of
    873  1.79       gdt 		 * whether we are in nonblocking mode.  XXX Reconsider
    874  1.79       gdt 		 * for blocking mode; maybe we should continue to
    875  1.79       gdt 		 * block, but maybe it just doesn't make senes to do
    876  1.79       gdt 		 * blocking reads from devices like this.
    877  1.79       gdt 		 */
    878  1.79       gdt 		if (err == USBD_SHORT_XFER) {
    879  1.79       gdt 			DPRINTFN(3, ("ulptread: SHORT=>done n %d nread %d err %d error %d\n",
    880  1.79       gdt 				     n, nread, err, error));
    881  1.60  augustss 			break;
    882  1.79       gdt 		}
    883  1.60  augustss 	}
    884  1.60  augustss 
    885  1.79       gdt done:
    886  1.79       gdt 	DPRINTFN(3, ("ulptread: finished n %d nread %d err %d error %d\n",
    887  1.79       gdt 			     n, nread, err, error));
    888  1.60  augustss 	return (error);
    889  1.60  augustss }
    890  1.60  augustss 
    891  1.60  augustss int
    892  1.60  augustss ulptread(dev_t dev, struct uio *uio, int flags)
    893  1.60  augustss {
    894  1.60  augustss 	struct ulpt_softc *sc;
    895  1.60  augustss 	int error;
    896  1.60  augustss 
    897  1.60  augustss 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    898  1.60  augustss 
    899  1.60  augustss 	if (sc->sc_dying)
    900  1.60  augustss 		return (EIO);
    901  1.60  augustss 
    902  1.60  augustss 	sc->sc_refcnt++;
    903  1.60  augustss 	error = ulpt_do_read(sc, uio, flags);
    904  1.60  augustss 	if (--sc->sc_refcnt < 0)
    905  1.60  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    906  1.60  augustss 	return (error);
    907  1.60  augustss }
    908  1.60  augustss 
    909  1.60  augustss void
    910  1.76  christos ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    911  1.76  christos 	     usbd_status status)
    912  1.60  augustss {
    913  1.60  augustss 	usbd_status err;
    914  1.60  augustss 	u_int32_t n;
    915  1.60  augustss 	usbd_private_handle xsc;
    916  1.60  augustss 	struct ulpt_softc *sc;
    917  1.60  augustss 
    918  1.60  augustss 	usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
    919  1.60  augustss 	sc = xsc;
    920  1.60  augustss 
    921  1.79       gdt 	DPRINTFN(4, ("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
    922  1.60  augustss 
    923  1.60  augustss #ifdef ULPT_DEBUG
    924  1.60  augustss 	if (!err && n > 0)
    925  1.79       gdt 		DPRINTFN(3, ("ulpt_tick: discarding %d bytes\n", n));
    926  1.60  augustss #endif
    927  1.60  augustss 	if (!err || err == USBD_TIMEOUT)
    928  1.60  augustss 		usb_callout(sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
    929  1.60  augustss 			    ulpt_tick, sc);
    930  1.60  augustss }
    931  1.60  augustss 
    932  1.79       gdt /*
    933  1.79       gdt  * For devices which are not opened for reading, this function is
    934  1.79       gdt  * called continuously to start read bulk transfers to avoid the
    935  1.79       gdt  * printer overflowing its output buffer.
    936  1.79       gdt  *
    937  1.79       gdt  * XXX This should be adapted for continuous reads to allow select to
    938  1.79       gdt  * work; see do_ulpt_read().
    939  1.79       gdt  */
    940  1.60  augustss void
    941  1.60  augustss ulpt_tick(void *xsc)
    942  1.60  augustss {
    943  1.60  augustss 	struct ulpt_softc *sc = xsc;
    944  1.60  augustss 	usbd_status err;
    945  1.60  augustss 
    946  1.60  augustss 	if (sc == NULL || sc->sc_dying)
    947  1.60  augustss 		return;
    948  1.60  augustss 
    949  1.60  augustss 	usbd_setup_xfer(sc->sc_in_xfer, sc->sc_in_pipe, sc, sc->sc_in_buf,
    950  1.60  augustss 			ULPT_BSIZE, USBD_NO_COPY | USBD_SHORT_XFER_OK,
    951  1.60  augustss 			ULPT_READ_TIMO, ulpt_read_cb);
    952  1.60  augustss 	err = usbd_transfer(sc->sc_in_xfer);
    953  1.79       gdt 	DPRINTFN(3, ("ulpt_tick: sc=%p err=%d\n", sc, err));
    954  1.60  augustss }
    955  1.60  augustss 
    956  1.60  augustss int
    957  1.77  christos ulptioctl(dev_t dev, u_long cmd, void *data,
    958  1.76  christos     int flag, struct lwp *l)
    959   1.1  augustss {
    960  1.79       gdt 	struct ulpt_softc *sc;
    961  1.79       gdt 
    962  1.79       gdt 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    963  1.79       gdt 
    964  1.79       gdt 	switch (cmd) {
    965  1.79       gdt 	case FIONBIO:
    966  1.79       gdt 		return 0;
    967  1.79       gdt 	}
    968  1.79       gdt 
    969  1.75  christos 	return ENODEV;
    970  1.16  augustss }
    971  1.16  augustss 
    972  1.22  augustss #if 0
    973  1.16  augustss /* XXX This does not belong here. */
    974  1.16  augustss /*
    975  1.17  augustss  * Print select parts of a IEEE 1284 device ID.
    976  1.16  augustss  */
    977  1.16  augustss void
    978  1.38  augustss ieee1284_print_id(char *str)
    979  1.16  augustss {
    980  1.16  augustss 	char *p, *q;
    981  1.16  augustss 
    982  1.16  augustss 	for (p = str-1; p; p = strchr(p, ';')) {
    983  1.16  augustss 		p++;		/* skip ';' */
    984  1.16  augustss 		if (strncmp(p, "MFG:", 4) == 0 ||
    985  1.16  augustss 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    986  1.16  augustss 		    strncmp(p, "MDL:", 4) == 0 ||
    987  1.16  augustss 		    strncmp(p, "MODEL:", 6) == 0) {
    988  1.16  augustss 			q = strchr(p, ';');
    989  1.16  augustss 			if (q)
    990  1.16  augustss 				printf("%.*s", (int)(q - p + 1), p);
    991  1.16  augustss 		}
    992  1.16  augustss 	}
    993   1.1  augustss }
    994  1.22  augustss #endif
    995   1.8  augustss 
    996   1.8  augustss #if defined(__FreeBSD__)
    997  1.30  augustss DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    998  1.60  augustss #endif
    999