Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.60
      1 /*	$NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss 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  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $");
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/proc.h>
     51 #include <sys/kernel.h>
     52 #include <sys/fcntl.h>
     53 #if defined(__NetBSD__) || defined(__OpenBSD__)
     54 #include <sys/device.h>
     55 #include <sys/ioctl.h>
     56 #elif defined(__FreeBSD__)
     57 #include <sys/ioccom.h>
     58 #include <sys/module.h>
     59 #include <sys/bus.h>
     60 #endif
     61 #include <sys/uio.h>
     62 #include <sys/conf.h>
     63 #include <sys/vnode.h>
     64 #include <sys/syslog.h>
     65 
     66 #include <machine/vmparam.h>	/* PAGE_SIZE */
     67 
     68 #include <dev/usb/usb.h>
     69 #include <dev/usb/usbdi.h>
     70 #include <dev/usb/usbdi_util.h>
     71 #include <dev/usb/usbdevs.h>
     72 #include <dev/usb/usb_quirks.h>
     73 
     74 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     75 #define	STEP		hz/4
     76 
     77 #define	LPTPRI		(PZERO+8)
     78 #define	ULPT_BSIZE	PAGE_SIZE
     79 
     80 #define ULPT_READS_PER_SEC 5
     81 #define ULPT_READ_TIMO 10
     82 
     83 #ifdef ULPT_DEBUG
     84 #define DPRINTF(x)	if (ulptdebug) logprintf x
     85 #define DPRINTFN(n,x)	if (ulptdebug>(n)) logprintf x
     86 int	ulptdebug = 0;
     87 #else
     88 #define DPRINTF(x)
     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 	USBBASEDEVICE 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 	usb_callout_t sc_read_callout;
    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,
    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 USB_DECLARE_DRIVER(ulpt);
    199 
    200 USB_MATCH(ulpt)
    201 {
    202 	USB_MATCH_START(ulpt, uaa);
    203 	usb_interface_descriptor_t *id;
    204 
    205 	DPRINTFN(10,("ulpt_match\n"));
    206 	if (uaa->iface == NULL)
    207 		return (UMATCH_NONE);
    208 	id = usbd_get_interface_descriptor(uaa->iface);
    209 	if (id != NULL &&
    210 	    id->bInterfaceClass == UICLASS_PRINTER &&
    211 	    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
    212 	    (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
    213 	     id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
    214 	     id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
    215 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    216 	return (UMATCH_NONE);
    217 }
    218 
    219 USB_ATTACH(ulpt)
    220 {
    221 	USB_ATTACH_START(ulpt, sc, uaa);
    222 	usbd_device_handle dev = uaa->device;
    223 	usbd_interface_handle iface = uaa->iface;
    224 	usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
    225 	usb_interface_descriptor_t *id, *iend;
    226 	usb_config_descriptor_t *cdesc;
    227 	usbd_status err;
    228 	char devinfo[1024];
    229 	usb_endpoint_descriptor_t *ed;
    230 	u_int8_t epcount;
    231 	int i, altno;
    232 
    233 	DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
    234 	usbd_devinfo(dev, 0, devinfo);
    235 	USB_ATTACH_SETUP;
    236 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    237 	       devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
    238 
    239 	/* XXX
    240 	 * Stepping through the alternate settings needs to be abstracted out.
    241 	 */
    242 	cdesc = usbd_get_config_descriptor(dev);
    243 	if (cdesc == NULL) {
    244 		printf("%s: failed to get configuration descriptor\n",
    245 		       USBDEVNAME(sc->sc_dev));
    246 		USB_ATTACH_ERROR_RETURN;
    247 	}
    248 	iend = (usb_interface_descriptor_t *)
    249 		   ((char *)cdesc + UGETW(cdesc->wTotalLength));
    250 #ifdef DIAGNOSTIC
    251 	if (ifcd < (usb_interface_descriptor_t *)cdesc ||
    252 	    ifcd >= iend)
    253 		panic("ulpt: iface desc out of range");
    254 #endif
    255 	/* Step through all the descriptors looking for bidir mode */
    256 	for (id = ifcd, altno = 0;
    257 	     id < iend;
    258 	     id = (void *)((char *)id + id->bLength)) {
    259 		if (id->bDescriptorType == UDESC_INTERFACE &&
    260 		    id->bInterfaceNumber == ifcd->bInterfaceNumber) {
    261 			if (id->bInterfaceClass == UICLASS_PRINTER &&
    262 			    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
    263 			    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
    264 			     id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
    265 				goto found;
    266 			altno++;
    267 		}
    268 	}
    269 	id = ifcd;		/* not found, use original */
    270  found:
    271 	if (id != ifcd) {
    272 		/* Found a new bidir setting */
    273 		DPRINTF(("ulpt_attach: set altno = %d\n", altno));
    274 		err = usbd_set_interface(iface, altno);
    275 		if (err) {
    276 			printf("%s: setting alternate interface failed\n",
    277 			       USBDEVNAME(sc->sc_dev));
    278 			sc->sc_dying = 1;
    279 			USB_ATTACH_ERROR_RETURN;
    280 		}
    281 	}
    282 
    283 	epcount = 0;
    284 	(void)usbd_endpoint_count(iface, &epcount);
    285 
    286 	sc->sc_in = -1;
    287 	sc->sc_out = -1;
    288 	for (i = 0; i < epcount; i++) {
    289 		ed = usbd_interface2endpoint_descriptor(iface, i);
    290 		if (ed == NULL) {
    291 			printf("%s: couldn't get ep %d\n",
    292 			    USBDEVNAME(sc->sc_dev), i);
    293 			USB_ATTACH_ERROR_RETURN;
    294 		}
    295 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    296 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    297 			sc->sc_in = ed->bEndpointAddress;
    298 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    299 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    300 			sc->sc_out = ed->bEndpointAddress;
    301 		}
    302 	}
    303 	if (sc->sc_out == -1) {
    304 		printf("%s: could not find bulk out endpoint\n",
    305 		    USBDEVNAME(sc->sc_dev));
    306 		sc->sc_dying = 1;
    307 		USB_ATTACH_ERROR_RETURN;
    308 	}
    309 
    310 	if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
    311 		/* This device doesn't handle reading properly. */
    312 		sc->sc_in = -1;
    313 	}
    314 
    315 	printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
    316 	       sc->sc_in >= 0 ? "bi" : "uni");
    317 
    318 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
    319 
    320 	sc->sc_iface = iface;
    321 	sc->sc_ifaceno = id->bInterfaceNumber;
    322 	sc->sc_udev = dev;
    323 
    324 #if 0
    325 /*
    326  * This code is disabled because for some mysterious reason it causes
    327  * printing not to work.  But only sometimes, and mostly with
    328  * UHCI and less often with OHCI.  *sigh*
    329  */
    330 	{
    331 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    332 	usb_device_request_t req;
    333 	int len, alen;
    334 
    335 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    336 	req.bRequest = UR_GET_DEVICE_ID;
    337 	USETW(req.wValue, cd->bConfigurationValue);
    338 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    339 	USETW(req.wLength, sizeof devinfo - 1);
    340 	err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
    341 		  &alen, USBD_DEFAULT_TIMEOUT);
    342 	if (err) {
    343 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    344 	} else if (alen <= 2) {
    345 		printf("%s: empty device id, no printer connected?\n",
    346 		       USBDEVNAME(sc->sc_dev));
    347 	} else {
    348 		/* devinfo now contains an IEEE-1284 device ID */
    349 		len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
    350 		if (len > sizeof devinfo - 3)
    351 			len = sizeof devinfo - 3;
    352 		devinfo[len] = 0;
    353 		printf("%s: device id <", USBDEVNAME(sc->sc_dev));
    354 		ieee1284_print_id(devinfo+2);
    355 		printf(">\n");
    356 	}
    357 	}
    358 #endif
    359 
    360 #if defined(__FreeBSD__)
    361 	sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
    362 		UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
    363 	sc->dev_noprime = make_dev(&ulpt_cdevsw,
    364 		device_get_unit(self)|ULPT_NOPRIME,
    365 		UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
    366 #endif
    367 
    368 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    369 			   USBDEV(sc->sc_dev));
    370 
    371 	USB_ATTACH_SUCCESS_RETURN;
    372 }
    373 
    374 #if defined(__NetBSD__) || defined(__OpenBSD__)
    375 int
    376 ulpt_activate(device_ptr_t self, enum devact act)
    377 {
    378 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    379 
    380 	switch (act) {
    381 	case DVACT_ACTIVATE:
    382 		return (EOPNOTSUPP);
    383 
    384 	case DVACT_DEACTIVATE:
    385 		sc->sc_dying = 1;
    386 		break;
    387 	}
    388 	return (0);
    389 }
    390 #endif
    391 
    392 USB_DETACH(ulpt)
    393 {
    394 	USB_DETACH_START(ulpt, sc);
    395 	int s;
    396 #if defined(__NetBSD__) || defined(__OpenBSD__)
    397 	int maj, mn;
    398 #elif defined(__FreeBSD__)
    399 	struct vnode *vp;
    400 #endif
    401 
    402 	DPRINTF(("ulpt_detach: sc=%p\n", sc));
    403 
    404 	sc->sc_dying = 1;
    405 	if (sc->sc_out_pipe != NULL)
    406 		usbd_abort_pipe(sc->sc_out_pipe);
    407 	if (sc->sc_in_pipe != NULL)
    408 		usbd_abort_pipe(sc->sc_in_pipe);
    409 
    410 	s = splusb();
    411 	if (--sc->sc_refcnt >= 0) {
    412 		/* There is noone to wake, aborting the pipe is enough */
    413 		/* Wait for processes to go away. */
    414 		usb_detach_wait(USBDEV(sc->sc_dev));
    415 	}
    416 	splx(s);
    417 
    418 #if defined(__NetBSD__) || defined(__OpenBSD__)
    419 	/* locate the major number */
    420 #if defined(__NetBSD__)
    421 	maj = cdevsw_lookup_major(&ulpt_cdevsw);
    422 #elif defined(__OpenBSD__)
    423 	for (maj = 0; maj < nchrdev; maj++)
    424 		if (cdevsw[maj].d_open == ulptopen)
    425 			break;
    426 #endif
    427 
    428 	/* Nuke the vnodes for any open instances (calls close). */
    429 	mn = self->dv_unit;
    430 	vdevgone(maj, mn, mn, VCHR);
    431 	vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
    432 #elif defined(__FreeBSD__)
    433 	vp = SLIST_FIRST(&sc->dev->si_hlist);
    434 	if (vp)
    435 		VOP_REVOKE(vp, REVOKEALL);
    436 	vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
    437 	if (vp)
    438 		VOP_REVOKE(vp, REVOKEALL);
    439 
    440 	destroy_dev(sc->dev);
    441 	destroy_dev(sc->dev_noprime);
    442 #endif
    443 
    444 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    445 			   USBDEV(sc->sc_dev));
    446 
    447 	return (0);
    448 }
    449 
    450 int
    451 ulpt_status(struct ulpt_softc *sc)
    452 {
    453 	usb_device_request_t req;
    454 	usbd_status err;
    455 	u_char status;
    456 
    457 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    458 	req.bRequest = UR_GET_PORT_STATUS;
    459 	USETW(req.wValue, 0);
    460 	USETW(req.wIndex, sc->sc_ifaceno);
    461 	USETW(req.wLength, 1);
    462 	err = usbd_do_request(sc->sc_udev, &req, &status);
    463 	DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
    464 	if (!err)
    465 		return (status);
    466 	else
    467 		return (0);
    468 }
    469 
    470 void
    471 ulpt_reset(struct ulpt_softc *sc)
    472 {
    473 	usb_device_request_t req;
    474 
    475 	DPRINTFN(1, ("ulpt_reset\n"));
    476 	req.bRequest = UR_SOFT_RESET;
    477 	USETW(req.wValue, 0);
    478 	USETW(req.wIndex, sc->sc_ifaceno);
    479 	USETW(req.wLength, 0);
    480 
    481 	/*
    482 	 * There was a mistake in the USB printer 1.0 spec that gave the
    483 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
    484 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
    485 	 * so we try both.
    486 	 */
    487 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    488 	if (usbd_do_request(sc->sc_udev, &req, 0)) {	/* 1.0 */
    489 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    490 		(void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
    491 	}
    492 }
    493 
    494 #if 0
    495 static void
    496 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    497 {
    498 	struct ulpt_softc *sc = priv;
    499 
    500 	DPRINTFN(2,("ulpt_input: got some data\n"));
    501 	/* Do it again. */
    502 	if (xfer == sc->sc_in_xfer1)
    503 		usbd_transfer(sc->sc_in_xfer2);
    504 	else
    505 		usbd_transfer(sc->sc_in_xfer1);
    506 }
    507 #endif
    508 
    509 int ulptusein = 1;
    510 
    511 /*
    512  * Reset the printer, then wait until it's selected and not busy.
    513  */
    514 int
    515 ulptopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    516 {
    517 	u_char flags = ULPTFLAGS(dev);
    518 	struct ulpt_softc *sc;
    519 	usbd_status err;
    520 	int spin, error;
    521 
    522 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    523 
    524 	if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
    525 		return (ENXIO);
    526 
    527 	if (sc->sc_state)
    528 		return (EBUSY);
    529 
    530 	sc->sc_state = ULPT_INIT;
    531 	sc->sc_flags = flags;
    532 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    533 
    534 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
    535 	/* Ignoring these flags might not be a good idea */
    536 	if ((flags & ~ULPT_NOPRIME) != 0)
    537 		printf("ulptopen: flags ignored: %b\n", flags,
    538 			"\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
    539 #endif
    540 
    541 
    542 	error = 0;
    543 	sc->sc_refcnt++;
    544 
    545 	if ((flags & ULPT_NOPRIME) == 0)
    546 		ulpt_reset(sc);
    547 
    548 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    549 		DPRINTF(("ulpt_open: waiting a while\n"));
    550 		if (spin >= TIMEOUT) {
    551 			error = EBUSY;
    552 			sc->sc_state = 0;
    553 			goto done;
    554 		}
    555 
    556 		/* wait 1/4 second, give up if we get a signal */
    557 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    558 		if (error != EWOULDBLOCK) {
    559 			sc->sc_state = 0;
    560 			goto done;
    561 		}
    562 
    563 		if (sc->sc_dying) {
    564 			error = ENXIO;
    565 			sc->sc_state = 0;
    566 			goto done;
    567 		}
    568 	}
    569 
    570 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
    571 	if (err) {
    572 		error = EIO;
    573 		goto err0;
    574 	}
    575 	sc->sc_out_xfer = usbd_alloc_xfer(sc->sc_udev);
    576 	if (sc->sc_out_xfer == NULL) {
    577 		error = ENOMEM;
    578 		goto err1;
    579 	}
    580 	sc->sc_out_buf = usbd_alloc_buffer(sc->sc_out_xfer, ULPT_BSIZE);
    581 	if (sc->sc_out_buf == NULL) {
    582 		error = ENOMEM;
    583 		goto err2;
    584 	}
    585 
    586 	if (ulptusein && sc->sc_in != -1) {
    587 		DPRINTF(("ulpt_open: open input pipe\n"));
    588 		err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
    589 		if (err) {
    590 			error = EIO;
    591 			goto err2;
    592 		}
    593 		sc->sc_in_xfer = usbd_alloc_xfer(sc->sc_udev);
    594 		if (sc->sc_in_xfer == NULL) {
    595 			error = ENOMEM;
    596 			goto err3;
    597 		}
    598 		sc->sc_in_buf = usbd_alloc_buffer(sc->sc_in_xfer, ULPT_BSIZE);
    599 		if (sc->sc_in_buf == NULL) {
    600 			error = ENOMEM;
    601 			goto err4;
    602 		}
    603 
    604 		/* If it's not opened for read the set up a reader. */
    605 		if (!(flags & FREAD)) {
    606 			DPRINTF(("ulpt_open: start read callout\n"));
    607 			usb_callout_init(sc->sc_read_callout);
    608 			usb_callout(sc->sc_read_callout, hz/5, ulpt_tick, sc);
    609 			sc->sc_has_callout = 1;
    610 		}
    611 	}
    612 
    613 	sc->sc_state = ULPT_OPEN;
    614 	goto done;
    615 
    616  err4:
    617 	usbd_free_xfer(sc->sc_in_xfer);
    618 	sc->sc_in_xfer = NULL;
    619  err3:
    620 	usbd_close_pipe(sc->sc_in_pipe);
    621 	sc->sc_in_pipe = NULL;
    622  err2:
    623 	usbd_free_xfer(sc->sc_out_xfer);
    624 	sc->sc_out_xfer = NULL;
    625  err1:
    626 	usbd_close_pipe(sc->sc_out_pipe);
    627 	sc->sc_out_pipe = NULL;
    628  err0:
    629 	sc->sc_state = 0;
    630 
    631  done:
    632 	if (--sc->sc_refcnt < 0)
    633 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    634 
    635 	DPRINTF(("ulptopen: done, error=%d\n", error));
    636 	return (error);
    637 }
    638 
    639 int
    640 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
    641 {
    642 	u_char new;
    643 
    644 	status = (status ^ LPS_INVERT) & LPS_MASK;
    645 	new = status & ~sc->sc_laststatus;
    646 	sc->sc_laststatus = status;
    647 
    648 	if (new & LPS_SELECT)
    649 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    650 	else if (new & LPS_NOPAPER)
    651 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    652 	else if (new & LPS_NERR)
    653 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    654 
    655 	return (status);
    656 }
    657 
    658 int
    659 ulptclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    660 {
    661 	struct ulpt_softc *sc;
    662 
    663 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    664 
    665 	if (sc->sc_state != ULPT_OPEN)
    666 		/* We are being forced to close before the open completed. */
    667 		return (0);
    668 
    669 	if (sc->sc_has_callout) {
    670 		usb_uncallout(sc->sc_read_callout, ulpt_tick, sc);
    671 		sc->sc_has_callout = 0;
    672 	}
    673 
    674 	if (sc->sc_out_pipe != NULL) {
    675 		usbd_abort_pipe(sc->sc_out_pipe);
    676 		usbd_close_pipe(sc->sc_out_pipe);
    677 		sc->sc_out_pipe = NULL;
    678 	}
    679 	if (sc->sc_out_xfer != NULL) {
    680 		usbd_free_xfer(sc->sc_out_xfer);
    681 		sc->sc_out_xfer = NULL;
    682 	}
    683 
    684 	if (sc->sc_in_pipe != NULL) {
    685 		usbd_abort_pipe(sc->sc_in_pipe);
    686 		usbd_close_pipe(sc->sc_in_pipe);
    687 		sc->sc_in_pipe = NULL;
    688 	}
    689 	if (sc->sc_in_xfer != NULL) {
    690 		usbd_free_xfer(sc->sc_in_xfer);
    691 		sc->sc_in_xfer = NULL;
    692 	}
    693 
    694 	sc->sc_state = 0;
    695 
    696 	DPRINTF(("ulptclose: closed\n"));
    697 	return (0);
    698 }
    699 
    700 int
    701 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
    702 {
    703 	u_int32_t n;
    704 	int error = 0;
    705 	void *bufp;
    706 	usbd_xfer_handle xfer;
    707 	usbd_status err;
    708 
    709 	DPRINTF(("ulptwrite\n"));
    710 	xfer = sc->sc_out_xfer;
    711 	bufp = sc->sc_out_buf;
    712 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    713 		ulpt_statusmsg(ulpt_status(sc), sc);
    714 		error = uiomove(bufp, n, uio);
    715 		if (error)
    716 			break;
    717 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    718 		err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
    719 			  USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
    720 		if (err) {
    721 			DPRINTF(("ulptwrite: error=%d\n", err));
    722 			error = EIO;
    723 			break;
    724 		}
    725 	}
    726 
    727 	return (error);
    728 }
    729 
    730 int
    731 ulptwrite(dev_t dev, struct uio *uio, int flags)
    732 {
    733 	struct ulpt_softc *sc;
    734 	int error;
    735 
    736 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    737 
    738 	if (sc->sc_dying)
    739 		return (EIO);
    740 
    741 	sc->sc_refcnt++;
    742 	error = ulpt_do_write(sc, uio, flags);
    743 	if (--sc->sc_refcnt < 0)
    744 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    745 	return (error);
    746 }
    747 
    748 int
    749 ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
    750 {
    751 	u_int32_t n, on;
    752 	int error = 0;
    753 	void *bufp;
    754 	usbd_xfer_handle xfer;
    755 	usbd_status err;
    756 
    757 	DPRINTF(("ulptread\n"));
    758 
    759 	if (sc->sc_in_pipe == NULL)
    760 		return 0;
    761 
    762 	xfer = sc->sc_in_xfer;
    763 	bufp = sc->sc_in_buf;
    764 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    765 		DPRINTFN(1, ("ulptread: transfer %d bytes\n", n));
    766 		on = n;
    767 		err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
    768 			  USBD_NO_COPY | USBD_SHORT_XFER_OK,
    769 			  USBD_NO_TIMEOUT, bufp, &n, "ulptrd");
    770 		if (err) {
    771 			DPRINTF(("ulptread: error=%d\n", err));
    772 			error = EIO;
    773 			break;
    774 		}
    775 		error = uiomove(bufp, n, uio);
    776 		if (error)
    777 			break;
    778 		if (on != n)
    779 			break;
    780 	}
    781 
    782 	return (error);
    783 }
    784 
    785 int
    786 ulptread(dev_t dev, struct uio *uio, int flags)
    787 {
    788 	struct ulpt_softc *sc;
    789 	int error;
    790 
    791 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    792 
    793 	if (sc->sc_dying)
    794 		return (EIO);
    795 
    796 	sc->sc_refcnt++;
    797 	error = ulpt_do_read(sc, uio, flags);
    798 	if (--sc->sc_refcnt < 0)
    799 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    800 	return (error);
    801 }
    802 
    803 void
    804 ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    805 	     usbd_status status)
    806 {
    807 	usbd_status err;
    808 	u_int32_t n;
    809 	usbd_private_handle xsc;
    810 	struct ulpt_softc *sc;
    811 
    812 	usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
    813 	sc = xsc;
    814 
    815 	DPRINTFN(1,("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
    816 
    817 #ifdef ULPT_DEBUG
    818 	if (!err && n > 0)
    819 		DPRINTF(("ulpt_tick: discarding %d bytes\n", n));
    820 #endif
    821 	if (!err || err == USBD_TIMEOUT)
    822 		usb_callout(sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
    823 			    ulpt_tick, sc);
    824 }
    825 
    826 void
    827 ulpt_tick(void *xsc)
    828 {
    829 	struct ulpt_softc *sc = xsc;
    830 	usbd_status err;
    831 
    832 	if (sc == NULL || sc->sc_dying)
    833 		return;
    834 
    835 	DPRINTFN(1,("ulpt_tick: start sc=%p\n", sc));
    836 
    837 	usbd_setup_xfer(sc->sc_in_xfer, sc->sc_in_pipe, sc, sc->sc_in_buf,
    838 			ULPT_BSIZE, USBD_NO_COPY | USBD_SHORT_XFER_OK,
    839 			ULPT_READ_TIMO, ulpt_read_cb);
    840 	err = usbd_transfer(sc->sc_in_xfer);
    841 	DPRINTFN(1,("ulpt_tick: err=%d\n", err));
    842 }
    843 
    844 int
    845 ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
    846 {
    847 	int error = 0;
    848 
    849 	switch (cmd) {
    850 	default:
    851 		error = ENODEV;
    852 	}
    853 
    854 	return (error);
    855 }
    856 
    857 #if 0
    858 /* XXX This does not belong here. */
    859 /*
    860  * Print select parts of a IEEE 1284 device ID.
    861  */
    862 void
    863 ieee1284_print_id(char *str)
    864 {
    865 	char *p, *q;
    866 
    867 	for (p = str-1; p; p = strchr(p, ';')) {
    868 		p++;		/* skip ';' */
    869 		if (strncmp(p, "MFG:", 4) == 0 ||
    870 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    871 		    strncmp(p, "MDL:", 4) == 0 ||
    872 		    strncmp(p, "MODEL:", 6) == 0) {
    873 			q = strchr(p, ';');
    874 			if (q)
    875 				printf("%.*s", (int)(q - p + 1), p);
    876 		}
    877 	}
    878 }
    879 #endif
    880 
    881 #if defined(__FreeBSD__)
    882 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    883 #endif
    884 
    885 
    886 
    887 
    888 
    889 #if 0
    890 		usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
    891 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    892 		    USBD_NO_TIMEOUT, ulpt_input);
    893 		usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
    894 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    895 		    USBD_NO_TIMEOUT, ulpt_input);
    896 		usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
    897 #endif
    898