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