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