Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.49
      1 /*	$NetBSD: ulpt.c,v 1.49 2002/02/25 22:39:01 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 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.49 2002/02/25 22:39:01 augustss Exp $");
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/proc.h>
     51 #include <sys/kernel.h>
     52 #if defined(__NetBSD__) || defined(__OpenBSD__)
     53 #include <sys/device.h>
     54 #include <sys/ioctl.h>
     55 #elif defined(__FreeBSD__)
     56 #include <sys/ioccom.h>
     57 #include <sys/module.h>
     58 #include <sys/bus.h>
     59 #endif
     60 #include <sys/uio.h>
     61 #include <sys/conf.h>
     62 #include <sys/vnode.h>
     63 #include <sys/syslog.h>
     64 
     65 #include <dev/usb/usb.h>
     66 #include <dev/usb/usbdi.h>
     67 #include <dev/usb/usbdi_util.h>
     68 #include <dev/usb/usbdevs.h>
     69 #include <dev/usb/usb_quirks.h>
     70 
     71 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     72 #define	STEP		hz/4
     73 
     74 #define	LPTPRI		(PZERO+8)
     75 #define	ULPT_BSIZE	16384
     76 
     77 #ifdef ULPT_DEBUG
     78 #define DPRINTF(x)	if (ulptdebug) logprintf x
     79 #define DPRINTFN(n,x)	if (ulptdebug>(n)) logprintf x
     80 int	ulptdebug = 0;
     81 #else
     82 #define DPRINTF(x)
     83 #define DPRINTFN(n,x)
     84 #endif
     85 
     86 #define UR_GET_DEVICE_ID 0
     87 #define UR_GET_PORT_STATUS 1
     88 #define UR_SOFT_RESET 2
     89 
     90 #define	LPS_NERR		0x08	/* printer no error */
     91 #define	LPS_SELECT		0x10	/* printer selected */
     92 #define	LPS_NOPAPER		0x20	/* printer out of paper */
     93 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
     94 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
     95 
     96 struct ulpt_softc {
     97 	USBBASEDEVICE sc_dev;
     98 	usbd_device_handle sc_udev;	/* device */
     99 	usbd_interface_handle sc_iface;	/* interface */
    100 	int sc_ifaceno;
    101 
    102 	int sc_out;
    103 	usbd_pipe_handle sc_out_pipe;	/* bulk out pipe */
    104 
    105 	int sc_in;
    106 	usbd_pipe_handle sc_in_pipe;	/* bulk in pipe */
    107 	usbd_xfer_handle sc_in_xfer1;
    108 	usbd_xfer_handle sc_in_xfer2;
    109 	u_char sc_junk[64];	/* somewhere to dump input */
    110 
    111 	u_char sc_state;
    112 #define	ULPT_OPEN	0x01	/* device is open */
    113 #define	ULPT_OBUSY	0x02	/* printer is busy doing output */
    114 #define	ULPT_INIT	0x04	/* waiting to initialize for open */
    115 	u_char sc_flags;
    116 #define	ULPT_NOPRIME	0x40	/* don't prime on open */
    117 	u_char sc_laststatus;
    118 
    119 	int sc_refcnt;
    120 	u_char sc_dying;
    121 
    122 #if defined(__FreeBSD__)
    123 	dev_t dev;
    124 	dev_t dev_noprime;
    125 #endif
    126 };
    127 
    128 #if defined(__NetBSD__) || defined(__OpenBSD__)
    129 cdev_decl(ulpt);
    130 #elif defined(__FreeBSD__)
    131 Static d_open_t ulptopen;
    132 Static d_close_t ulptclose;
    133 Static d_write_t ulptwrite;
    134 Static d_ioctl_t ulptioctl;
    135 
    136 #define ULPT_CDEV_MAJOR 113
    137 
    138 Static struct cdevsw ulpt_cdevsw = {
    139 	/* open */	ulptopen,
    140 	/* close */	ulptclose,
    141 	/* read */	noread,
    142 	/* write */	ulptwrite,
    143 	/* ioctl */	ulptioctl,
    144 	/* poll */	nopoll,
    145 	/* mmap */	nommap,
    146 	/* strategy */	nostrategy,
    147 	/* name */	"ulpt",
    148 	/* maj */	ULPT_CDEV_MAJOR,
    149 	/* dump */	nodump,
    150 	/* psize */	nopsize,
    151 	/* flags */	0,
    152 #if !defined(__FreeBSD__) || (__FreeBSD__ < 5)
    153 	/* bmaj */	-1
    154 #endif
    155 };
    156 #endif
    157 
    158 void ulpt_disco(void *);
    159 
    160 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
    161 int ulpt_status(struct ulpt_softc *);
    162 void ulpt_reset(struct ulpt_softc *);
    163 int ulpt_statusmsg(u_char, struct ulpt_softc *);
    164 
    165 #if 0
    166 void ieee1284_print_id(char *);
    167 #endif
    168 
    169 #define	ULPTUNIT(s)	(minor(s) & 0x1f)
    170 #define	ULPTFLAGS(s)	(minor(s) & 0xe0)
    171 
    172 
    173 USB_DECLARE_DRIVER(ulpt);
    174 
    175 USB_MATCH(ulpt)
    176 {
    177 	USB_MATCH_START(ulpt, uaa);
    178 	usb_interface_descriptor_t *id;
    179 
    180 	DPRINTFN(10,("ulpt_match\n"));
    181 	if (uaa->iface == NULL)
    182 		return (UMATCH_NONE);
    183 	id = usbd_get_interface_descriptor(uaa->iface);
    184 	if (id != NULL &&
    185 	    id->bInterfaceClass == UICLASS_PRINTER &&
    186 	    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
    187 	    (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
    188 	     id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
    189 	     id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
    190 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    191 	return (UMATCH_NONE);
    192 }
    193 
    194 USB_ATTACH(ulpt)
    195 {
    196 	USB_ATTACH_START(ulpt, sc, uaa);
    197 	usbd_device_handle dev = uaa->device;
    198 	usbd_interface_handle iface = uaa->iface;
    199 	usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
    200 	usb_interface_descriptor_t *id, *iend;
    201 	usb_config_descriptor_t *cdesc;
    202 	usbd_status err;
    203 	char devinfo[1024];
    204 	usb_endpoint_descriptor_t *ed;
    205 	u_int8_t epcount;
    206 	int i, altno;
    207 
    208 	DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
    209 	usbd_devinfo(dev, 0, devinfo);
    210 	USB_ATTACH_SETUP;
    211 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    212 	       devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
    213 
    214 	/* XXX
    215 	 * Stepping through the alternate settings needs to be abstracted out.
    216 	 */
    217 	cdesc = usbd_get_config_descriptor(dev);
    218 	if (cdesc == NULL) {
    219 		printf("%s: failed to get configuration descriptor\n",
    220 		       USBDEVNAME(sc->sc_dev));
    221 		USB_ATTACH_ERROR_RETURN;
    222 	}
    223 	iend = (usb_interface_descriptor_t *)
    224 		   ((char *)cdesc + UGETW(cdesc->wTotalLength));
    225 #ifdef DIAGNOSTIC
    226 	if (ifcd < (usb_interface_descriptor_t *)cdesc ||
    227 	    ifcd >= iend)
    228 		panic("ulpt: iface desc out of range\n");
    229 #endif
    230 	/* Step through all the descriptors looking for bidir mode */
    231 	for (id = ifcd, altno = 0;
    232 	     id < iend;
    233 	     id = (void *)((char *)id + id->bLength)) {
    234 		if (id->bDescriptorType == UDESC_INTERFACE &&
    235 		    id->bInterfaceNumber == ifcd->bInterfaceNumber) {
    236 			if (id->bInterfaceClass == UICLASS_PRINTER &&
    237 			    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
    238 			    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
    239 			     id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
    240 				goto found;
    241 			altno++;
    242 		}
    243 	}
    244 	id = ifcd;		/* not found, use original */
    245  found:
    246 	if (id != ifcd) {
    247 		/* Found a new bidir setting */
    248 		DPRINTF(("ulpt_attach: set altno = %d\n", altno));
    249 		err = usbd_set_interface(iface, altno);
    250 		if (err) {
    251 			printf("%s: setting alternate interface failed\n",
    252 			       USBDEVNAME(sc->sc_dev));
    253 			sc->sc_dying = 1;
    254 			USB_ATTACH_ERROR_RETURN;
    255 		}
    256 	}
    257 
    258 	epcount = 0;
    259 	(void)usbd_endpoint_count(iface, &epcount);
    260 
    261 	sc->sc_in = -1;
    262 	sc->sc_out = -1;
    263 	for (i = 0; i < epcount; i++) {
    264 		ed = usbd_interface2endpoint_descriptor(iface, i);
    265 		if (ed == NULL) {
    266 			printf("%s: couldn't get ep %d\n",
    267 			    USBDEVNAME(sc->sc_dev), i);
    268 			USB_ATTACH_ERROR_RETURN;
    269 		}
    270 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    271 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    272 			sc->sc_in = ed->bEndpointAddress;
    273 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    274 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    275 			sc->sc_out = ed->bEndpointAddress;
    276 		}
    277 	}
    278 	if (sc->sc_out == -1) {
    279 		printf("%s: could not find bulk endpoint\n",
    280 		    USBDEVNAME(sc->sc_dev));
    281 		sc->sc_dying = 1;
    282 		USB_ATTACH_ERROR_RETURN;
    283 	}
    284 	printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
    285 	       sc->sc_in >= 0 ? "bi" : "uni");
    286 
    287 	if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
    288 		/* This device doesn't handle reading properly. */
    289 		sc->sc_in = -1;
    290 	}
    291 
    292 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
    293 
    294 	sc->sc_iface = iface;
    295 	sc->sc_ifaceno = id->bInterfaceNumber;
    296 	sc->sc_udev = dev;
    297 
    298 #if 0
    299 /*
    300  * This code is disabled because for some mysterious reason it causes
    301  * printing not to work.  But only sometimes, and mostly with
    302  * UHCI and less often with OHCI.  *sigh*
    303  */
    304 	{
    305 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    306 	usb_device_request_t req;
    307 	int len, alen;
    308 
    309 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    310 	req.bRequest = UR_GET_DEVICE_ID;
    311 	USETW(req.wValue, cd->bConfigurationValue);
    312 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    313 	USETW(req.wLength, sizeof devinfo - 1);
    314 	err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
    315 		  &alen, USBD_DEFAULT_TIMEOUT);
    316 	if (err) {
    317 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    318 	} else if (alen <= 2) {
    319 		printf("%s: empty device id, no printer connected?\n",
    320 		       USBDEVNAME(sc->sc_dev));
    321 	} else {
    322 		/* devinfo now contains an IEEE-1284 device ID */
    323 		len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
    324 		if (len > sizeof devinfo - 3)
    325 			len = sizeof devinfo - 3;
    326 		devinfo[len] = 0;
    327 		printf("%s: device id <", USBDEVNAME(sc->sc_dev));
    328 		ieee1284_print_id(devinfo+2);
    329 		printf(">\n");
    330 	}
    331 	}
    332 #endif
    333 
    334 #if defined(__FreeBSD__)
    335 	sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
    336 		UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
    337 	sc->dev_noprime = make_dev(&ulpt_cdevsw,
    338 		device_get_unit(self)|ULPT_NOPRIME,
    339 		UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
    340 #endif
    341 
    342 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    343 			   USBDEV(sc->sc_dev));
    344 
    345 	USB_ATTACH_SUCCESS_RETURN;
    346 }
    347 
    348 #if defined(__NetBSD__) || defined(__OpenBSD__)
    349 int
    350 ulpt_activate(device_ptr_t self, enum devact act)
    351 {
    352 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    353 
    354 	switch (act) {
    355 	case DVACT_ACTIVATE:
    356 		return (EOPNOTSUPP);
    357 		break;
    358 
    359 	case DVACT_DEACTIVATE:
    360 		sc->sc_dying = 1;
    361 		break;
    362 	}
    363 	return (0);
    364 }
    365 #endif
    366 
    367 USB_DETACH(ulpt)
    368 {
    369 	USB_DETACH_START(ulpt, sc);
    370 	int s;
    371 #if defined(__NetBSD__) || defined(__OpenBSD__)
    372 	int maj, mn;
    373 #elif defined(__FreeBSD__)
    374 	struct vnode *vp;
    375 #endif
    376 
    377 	DPRINTF(("ulpt_detach: sc=%p\n", sc));
    378 
    379 	sc->sc_dying = 1;
    380 	if (sc->sc_out_pipe != NULL)
    381 		usbd_abort_pipe(sc->sc_out_pipe);
    382 	if (sc->sc_in_pipe != NULL)
    383 		usbd_abort_pipe(sc->sc_in_pipe);
    384 
    385 	s = splusb();
    386 	if (--sc->sc_refcnt >= 0) {
    387 		/* There is noone to wake, aborting the pipe is enough */
    388 		/* Wait for processes to go away. */
    389 		usb_detach_wait(USBDEV(sc->sc_dev));
    390 	}
    391 	splx(s);
    392 
    393 #if defined(__NetBSD__) || defined(__OpenBSD__)
    394 	/* locate the major number */
    395 	for (maj = 0; maj < nchrdev; maj++)
    396 		if (cdevsw[maj].d_open == ulptopen)
    397 			break;
    398 
    399 	/* Nuke the vnodes for any open instances (calls close). */
    400 	mn = self->dv_unit;
    401 	vdevgone(maj, mn, mn, VCHR);
    402 #elif defined(__FreeBSD__)
    403 	vp = SLIST_FIRST(&sc->dev->si_hlist);
    404 	if (vp)
    405 		VOP_REVOKE(vp, REVOKEALL);
    406 	vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
    407 	if (vp)
    408 		VOP_REVOKE(vp, REVOKEALL);
    409 
    410 	destroy_dev(sc->dev);
    411 	destroy_dev(sc->dev_noprime);
    412 #endif
    413 
    414 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    415 			   USBDEV(sc->sc_dev));
    416 
    417 	return (0);
    418 }
    419 
    420 int
    421 ulpt_status(struct ulpt_softc *sc)
    422 {
    423 	usb_device_request_t req;
    424 	usbd_status err;
    425 	u_char status;
    426 
    427 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    428 	req.bRequest = UR_GET_PORT_STATUS;
    429 	USETW(req.wValue, 0);
    430 	USETW(req.wIndex, sc->sc_ifaceno);
    431 	USETW(req.wLength, 1);
    432 	err = usbd_do_request(sc->sc_udev, &req, &status);
    433 	DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
    434 	if (!err)
    435 		return (status);
    436 	else
    437 		return (0);
    438 }
    439 
    440 void
    441 ulpt_reset(struct ulpt_softc *sc)
    442 {
    443 	usb_device_request_t req;
    444 
    445 	DPRINTFN(1, ("ulpt_reset\n"));
    446 	req.bRequest = UR_SOFT_RESET;
    447 	USETW(req.wValue, 0);
    448 	USETW(req.wIndex, sc->sc_ifaceno);
    449 	USETW(req.wLength, 0);
    450 
    451 	/*
    452 	 * There was a mistake in the USB printer 1.0 spec that gave the
    453 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
    454 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
    455 	 * so we try both.
    456 	 */
    457 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    458 	if (usbd_do_request(sc->sc_udev, &req, 0)) {	/* 1.0 */
    459 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    460 		(void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
    461 	}
    462 }
    463 
    464 static void
    465 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    466 {
    467 	struct ulpt_softc *sc = priv;
    468 
    469 	DPRINTFN(2,("ulpt_input: got some data\n"));
    470 	/* Do it again. */
    471 	if (xfer == sc->sc_in_xfer1)
    472 		usbd_transfer(sc->sc_in_xfer2);
    473 	else
    474 		usbd_transfer(sc->sc_in_xfer1);
    475 }
    476 
    477 int ulptusein = 1;
    478 
    479 /*
    480  * Reset the printer, then wait until it's selected and not busy.
    481  */
    482 int
    483 ulptopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    484 {
    485 	u_char flags = ULPTFLAGS(dev);
    486 	struct ulpt_softc *sc;
    487 	usbd_status err;
    488 	int spin, error;
    489 
    490 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    491 
    492 	if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
    493 		return (ENXIO);
    494 
    495 	if (sc->sc_state)
    496 		return (EBUSY);
    497 
    498 	sc->sc_state = ULPT_INIT;
    499 	sc->sc_flags = flags;
    500 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    501 
    502 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
    503 	/* Ignoring these flags might not be a good idea */
    504 	if ((flags & ~ULPT_NOPRIME) != 0)
    505 		printf("ulptopen: flags ignored: %b\n", flags,
    506 			"\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
    507 #endif
    508 
    509 
    510 	error = 0;
    511 	sc->sc_refcnt++;
    512 
    513 	if ((flags & ULPT_NOPRIME) == 0)
    514 		ulpt_reset(sc);
    515 
    516 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    517 		DPRINTF(("ulpt_open: waiting a while\n"));
    518 		if (spin >= TIMEOUT) {
    519 			error = EBUSY;
    520 			sc->sc_state = 0;
    521 			goto done;
    522 		}
    523 
    524 		/* wait 1/4 second, give up if we get a signal */
    525 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    526 		if (error != EWOULDBLOCK) {
    527 			sc->sc_state = 0;
    528 			goto done;
    529 		}
    530 
    531 		if (sc->sc_dying) {
    532 			error = ENXIO;
    533 			sc->sc_state = 0;
    534 			goto done;
    535 		}
    536 	}
    537 
    538 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
    539 	if (err) {
    540 		sc->sc_state = 0;
    541 		error = EIO;
    542 		goto done;
    543 	}
    544 	if (ulptusein && sc->sc_in != -1) {
    545 		DPRINTF(("ulpt_open: open input pipe\n"));
    546 		err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
    547 		if (err) {
    548 			error = EIO;
    549 			usbd_close_pipe(sc->sc_out_pipe);
    550 			sc->sc_out_pipe = NULL;
    551 			sc->sc_state = 0;
    552 			goto done;
    553 		}
    554 		sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
    555 		sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
    556 		if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
    557 			error = ENOMEM;
    558 			if (sc->sc_in_xfer1 != NULL) {
    559 				usbd_free_xfer(sc->sc_in_xfer1);
    560 				sc->sc_in_xfer1 = NULL;
    561 			}
    562 			if (sc->sc_in_xfer2 != NULL) {
    563 				usbd_free_xfer(sc->sc_in_xfer2);
    564 				sc->sc_in_xfer2 = NULL;
    565 			}
    566 			usbd_close_pipe(sc->sc_out_pipe);
    567 			sc->sc_out_pipe = NULL;
    568 			usbd_close_pipe(sc->sc_in_pipe);
    569 			sc->sc_in_pipe = NULL;
    570 			sc->sc_state = 0;
    571 			goto done;
    572 		}
    573 		usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
    574 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    575 		    USBD_NO_TIMEOUT, ulpt_input);
    576 		usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
    577 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    578 		    USBD_NO_TIMEOUT, ulpt_input);
    579 		usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
    580 	}
    581 
    582 	sc->sc_state = ULPT_OPEN;
    583 
    584  done:
    585 	if (--sc->sc_refcnt < 0)
    586 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    587 
    588 	DPRINTF(("ulptopen: done, error=%d\n", error));
    589 	return (error);
    590 }
    591 
    592 int
    593 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
    594 {
    595 	u_char new;
    596 
    597 	status = (status ^ LPS_INVERT) & LPS_MASK;
    598 	new = status & ~sc->sc_laststatus;
    599 	sc->sc_laststatus = status;
    600 
    601 	if (new & LPS_SELECT)
    602 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    603 	else if (new & LPS_NOPAPER)
    604 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    605 	else if (new & LPS_NERR)
    606 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    607 
    608 	return (status);
    609 }
    610 
    611 int
    612 ulptclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    613 {
    614 	struct ulpt_softc *sc;
    615 
    616 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    617 
    618 	if (sc->sc_state != ULPT_OPEN)
    619 		/* We are being forced to close before the open completed. */
    620 		return (0);
    621 
    622 	if (sc->sc_out_pipe != NULL) {
    623 		usbd_close_pipe(sc->sc_out_pipe);
    624 		sc->sc_out_pipe = NULL;
    625 	}
    626 	if (sc->sc_in_pipe != NULL) {
    627 		usbd_abort_pipe(sc->sc_in_pipe);
    628 		usbd_close_pipe(sc->sc_in_pipe);
    629 		sc->sc_in_pipe = NULL;
    630 		if (sc->sc_in_xfer1 != NULL) {
    631 			usbd_free_xfer(sc->sc_in_xfer1);
    632 			sc->sc_in_xfer1 = NULL;
    633 		}
    634 		if (sc->sc_in_xfer2 != NULL) {
    635 			usbd_free_xfer(sc->sc_in_xfer2);
    636 			sc->sc_in_xfer2 = NULL;
    637 		}
    638 	}
    639 
    640 	sc->sc_state = 0;
    641 
    642 	DPRINTF(("ulptclose: closed\n"));
    643 	return (0);
    644 }
    645 
    646 int
    647 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
    648 {
    649 	u_int32_t n;
    650 	int error = 0;
    651 	void *bufp;
    652 	usbd_xfer_handle xfer;
    653 	usbd_status err;
    654 
    655 	DPRINTF(("ulptwrite\n"));
    656 	xfer = usbd_alloc_xfer(sc->sc_udev);
    657 	if (xfer == NULL)
    658 		return (ENOMEM);
    659 	bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
    660 	if (bufp == NULL) {
    661 		usbd_free_xfer(xfer);
    662 		return (ENOMEM);
    663 	}
    664 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    665 		ulpt_statusmsg(ulpt_status(sc), sc);
    666 		error = uiomove(bufp, n, uio);
    667 		if (error)
    668 			break;
    669 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    670 		err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
    671 			  USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
    672 		if (err) {
    673 			DPRINTF(("ulptwrite: error=%d\n", err));
    674 			error = EIO;
    675 			break;
    676 		}
    677 	}
    678 	usbd_free_xfer(xfer);
    679 
    680 	return (error);
    681 }
    682 
    683 int
    684 ulptwrite(dev_t dev, struct uio *uio, int flags)
    685 {
    686 	struct ulpt_softc *sc;
    687 	int error;
    688 
    689 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    690 
    691 	if (sc->sc_dying)
    692 		return (EIO);
    693 
    694 	sc->sc_refcnt++;
    695 	error = ulpt_do_write(sc, uio, flags);
    696 	if (--sc->sc_refcnt < 0)
    697 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    698 	return (error);
    699 }
    700 
    701 int
    702 ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
    703 {
    704 	int error = 0;
    705 
    706 	switch (cmd) {
    707 	default:
    708 		error = ENODEV;
    709 	}
    710 
    711 	return (error);
    712 }
    713 
    714 #if 0
    715 /* XXX This does not belong here. */
    716 /*
    717  * Print select parts of a IEEE 1284 device ID.
    718  */
    719 void
    720 ieee1284_print_id(char *str)
    721 {
    722 	char *p, *q;
    723 
    724 	for (p = str-1; p; p = strchr(p, ';')) {
    725 		p++;		/* skip ';' */
    726 		if (strncmp(p, "MFG:", 4) == 0 ||
    727 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    728 		    strncmp(p, "MDL:", 4) == 0 ||
    729 		    strncmp(p, "MODEL:", 6) == 0) {
    730 			q = strchr(p, ';');
    731 			if (q)
    732 				printf("%.*s", (int)(q - p + 1), p);
    733 		}
    734 	}
    735 }
    736 #endif
    737 
    738 #if defined(__FreeBSD__)
    739 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    740 #endif
    741