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