Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.51
      1 /*	$NetBSD: ulpt.c,v 1.51 2002/08/15 09:32: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 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.51 2002/08/15 09:32: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 #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 out endpoint\n",
    280 		    USBDEVNAME(sc->sc_dev));
    281 		sc->sc_dying = 1;
    282 		USB_ATTACH_ERROR_RETURN;
    283 	}
    284 
    285 	if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
    286 		/* This device doesn't handle reading properly. */
    287 		sc->sc_in = -1;
    288 	}
    289 
    290 	printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
    291 	       sc->sc_in >= 0 ? "bi" : "uni");
    292 
    293 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
    294 
    295 	sc->sc_iface = iface;
    296 	sc->sc_ifaceno = id->bInterfaceNumber;
    297 	sc->sc_udev = dev;
    298 
    299 #if 0
    300 /*
    301  * This code is disabled because for some mysterious reason it causes
    302  * printing not to work.  But only sometimes, and mostly with
    303  * UHCI and less often with OHCI.  *sigh*
    304  */
    305 	{
    306 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    307 	usb_device_request_t req;
    308 	int len, alen;
    309 
    310 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    311 	req.bRequest = UR_GET_DEVICE_ID;
    312 	USETW(req.wValue, cd->bConfigurationValue);
    313 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    314 	USETW(req.wLength, sizeof devinfo - 1);
    315 	err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
    316 		  &alen, USBD_DEFAULT_TIMEOUT);
    317 	if (err) {
    318 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    319 	} else if (alen <= 2) {
    320 		printf("%s: empty device id, no printer connected?\n",
    321 		       USBDEVNAME(sc->sc_dev));
    322 	} else {
    323 		/* devinfo now contains an IEEE-1284 device ID */
    324 		len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
    325 		if (len > sizeof devinfo - 3)
    326 			len = sizeof devinfo - 3;
    327 		devinfo[len] = 0;
    328 		printf("%s: device id <", USBDEVNAME(sc->sc_dev));
    329 		ieee1284_print_id(devinfo+2);
    330 		printf(">\n");
    331 	}
    332 	}
    333 #endif
    334 
    335 #if defined(__FreeBSD__)
    336 	sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
    337 		UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
    338 	sc->dev_noprime = make_dev(&ulpt_cdevsw,
    339 		device_get_unit(self)|ULPT_NOPRIME,
    340 		UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
    341 #endif
    342 
    343 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    344 			   USBDEV(sc->sc_dev));
    345 
    346 	USB_ATTACH_SUCCESS_RETURN;
    347 }
    348 
    349 #if defined(__NetBSD__) || defined(__OpenBSD__)
    350 int
    351 ulpt_activate(device_ptr_t self, enum devact act)
    352 {
    353 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    354 
    355 	switch (act) {
    356 	case DVACT_ACTIVATE:
    357 		return (EOPNOTSUPP);
    358 		break;
    359 
    360 	case DVACT_DEACTIVATE:
    361 		sc->sc_dying = 1;
    362 		break;
    363 	}
    364 	return (0);
    365 }
    366 #endif
    367 
    368 USB_DETACH(ulpt)
    369 {
    370 	USB_DETACH_START(ulpt, sc);
    371 	int s;
    372 #if defined(__NetBSD__) || defined(__OpenBSD__)
    373 	int maj, mn;
    374 #elif defined(__FreeBSD__)
    375 	struct vnode *vp;
    376 #endif
    377 
    378 	DPRINTF(("ulpt_detach: sc=%p\n", sc));
    379 
    380 	sc->sc_dying = 1;
    381 	if (sc->sc_out_pipe != NULL)
    382 		usbd_abort_pipe(sc->sc_out_pipe);
    383 	if (sc->sc_in_pipe != NULL)
    384 		usbd_abort_pipe(sc->sc_in_pipe);
    385 
    386 	s = splusb();
    387 	if (--sc->sc_refcnt >= 0) {
    388 		/* There is noone to wake, aborting the pipe is enough */
    389 		/* Wait for processes to go away. */
    390 		usb_detach_wait(USBDEV(sc->sc_dev));
    391 	}
    392 	splx(s);
    393 
    394 #if defined(__NetBSD__) || defined(__OpenBSD__)
    395 	/* locate the major number */
    396 	for (maj = 0; maj < nchrdev; maj++)
    397 		if (cdevsw[maj].d_open == ulptopen)
    398 			break;
    399 
    400 	/* Nuke the vnodes for any open instances (calls close). */
    401 	mn = self->dv_unit;
    402 	vdevgone(maj, mn, mn, VCHR);
    403 #elif defined(__FreeBSD__)
    404 	vp = SLIST_FIRST(&sc->dev->si_hlist);
    405 	if (vp)
    406 		VOP_REVOKE(vp, REVOKEALL);
    407 	vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
    408 	if (vp)
    409 		VOP_REVOKE(vp, REVOKEALL);
    410 
    411 	destroy_dev(sc->dev);
    412 	destroy_dev(sc->dev_noprime);
    413 #endif
    414 
    415 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    416 			   USBDEV(sc->sc_dev));
    417 
    418 	return (0);
    419 }
    420 
    421 int
    422 ulpt_status(struct ulpt_softc *sc)
    423 {
    424 	usb_device_request_t req;
    425 	usbd_status err;
    426 	u_char status;
    427 
    428 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    429 	req.bRequest = UR_GET_PORT_STATUS;
    430 	USETW(req.wValue, 0);
    431 	USETW(req.wIndex, sc->sc_ifaceno);
    432 	USETW(req.wLength, 1);
    433 	err = usbd_do_request(sc->sc_udev, &req, &status);
    434 	DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
    435 	if (!err)
    436 		return (status);
    437 	else
    438 		return (0);
    439 }
    440 
    441 void
    442 ulpt_reset(struct ulpt_softc *sc)
    443 {
    444 	usb_device_request_t req;
    445 
    446 	DPRINTFN(1, ("ulpt_reset\n"));
    447 	req.bRequest = UR_SOFT_RESET;
    448 	USETW(req.wValue, 0);
    449 	USETW(req.wIndex, sc->sc_ifaceno);
    450 	USETW(req.wLength, 0);
    451 
    452 	/*
    453 	 * There was a mistake in the USB printer 1.0 spec that gave the
    454 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
    455 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
    456 	 * so we try both.
    457 	 */
    458 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    459 	if (usbd_do_request(sc->sc_udev, &req, 0)) {	/* 1.0 */
    460 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    461 		(void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
    462 	}
    463 }
    464 
    465 static void
    466 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    467 {
    468 	struct ulpt_softc *sc = priv;
    469 
    470 	DPRINTFN(2,("ulpt_input: got some data\n"));
    471 	/* Do it again. */
    472 	if (xfer == sc->sc_in_xfer1)
    473 		usbd_transfer(sc->sc_in_xfer2);
    474 	else
    475 		usbd_transfer(sc->sc_in_xfer1);
    476 }
    477 
    478 int ulptusein = 1;
    479 
    480 /*
    481  * Reset the printer, then wait until it's selected and not busy.
    482  */
    483 int
    484 ulptopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    485 {
    486 	u_char flags = ULPTFLAGS(dev);
    487 	struct ulpt_softc *sc;
    488 	usbd_status err;
    489 	int spin, error;
    490 
    491 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    492 
    493 	if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
    494 		return (ENXIO);
    495 
    496 	if (sc->sc_state)
    497 		return (EBUSY);
    498 
    499 	sc->sc_state = ULPT_INIT;
    500 	sc->sc_flags = flags;
    501 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    502 
    503 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
    504 	/* Ignoring these flags might not be a good idea */
    505 	if ((flags & ~ULPT_NOPRIME) != 0)
    506 		printf("ulptopen: flags ignored: %b\n", flags,
    507 			"\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
    508 #endif
    509 
    510 
    511 	error = 0;
    512 	sc->sc_refcnt++;
    513 
    514 	if ((flags & ULPT_NOPRIME) == 0)
    515 		ulpt_reset(sc);
    516 
    517 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    518 		DPRINTF(("ulpt_open: waiting a while\n"));
    519 		if (spin >= TIMEOUT) {
    520 			error = EBUSY;
    521 			sc->sc_state = 0;
    522 			goto done;
    523 		}
    524 
    525 		/* wait 1/4 second, give up if we get a signal */
    526 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    527 		if (error != EWOULDBLOCK) {
    528 			sc->sc_state = 0;
    529 			goto done;
    530 		}
    531 
    532 		if (sc->sc_dying) {
    533 			error = ENXIO;
    534 			sc->sc_state = 0;
    535 			goto done;
    536 		}
    537 	}
    538 
    539 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
    540 	if (err) {
    541 		sc->sc_state = 0;
    542 		error = EIO;
    543 		goto done;
    544 	}
    545 	if (ulptusein && sc->sc_in != -1) {
    546 		DPRINTF(("ulpt_open: open input pipe\n"));
    547 		err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
    548 		if (err) {
    549 			error = EIO;
    550 			usbd_close_pipe(sc->sc_out_pipe);
    551 			sc->sc_out_pipe = NULL;
    552 			sc->sc_state = 0;
    553 			goto done;
    554 		}
    555 		sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
    556 		sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
    557 		if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
    558 			error = ENOMEM;
    559 			if (sc->sc_in_xfer1 != NULL) {
    560 				usbd_free_xfer(sc->sc_in_xfer1);
    561 				sc->sc_in_xfer1 = NULL;
    562 			}
    563 			if (sc->sc_in_xfer2 != NULL) {
    564 				usbd_free_xfer(sc->sc_in_xfer2);
    565 				sc->sc_in_xfer2 = NULL;
    566 			}
    567 			usbd_close_pipe(sc->sc_out_pipe);
    568 			sc->sc_out_pipe = NULL;
    569 			usbd_close_pipe(sc->sc_in_pipe);
    570 			sc->sc_in_pipe = NULL;
    571 			sc->sc_state = 0;
    572 			goto done;
    573 		}
    574 		usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
    575 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    576 		    USBD_NO_TIMEOUT, ulpt_input);
    577 		usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
    578 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    579 		    USBD_NO_TIMEOUT, ulpt_input);
    580 		usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
    581 	}
    582 
    583 	sc->sc_state = ULPT_OPEN;
    584 
    585  done:
    586 	if (--sc->sc_refcnt < 0)
    587 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    588 
    589 	DPRINTF(("ulptopen: done, error=%d\n", error));
    590 	return (error);
    591 }
    592 
    593 int
    594 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
    595 {
    596 	u_char new;
    597 
    598 	status = (status ^ LPS_INVERT) & LPS_MASK;
    599 	new = status & ~sc->sc_laststatus;
    600 	sc->sc_laststatus = status;
    601 
    602 	if (new & LPS_SELECT)
    603 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    604 	else if (new & LPS_NOPAPER)
    605 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    606 	else if (new & LPS_NERR)
    607 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    608 
    609 	return (status);
    610 }
    611 
    612 int
    613 ulptclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    614 {
    615 	struct ulpt_softc *sc;
    616 
    617 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    618 
    619 	if (sc->sc_state != ULPT_OPEN)
    620 		/* We are being forced to close before the open completed. */
    621 		return (0);
    622 
    623 	if (sc->sc_out_pipe != NULL) {
    624 		usbd_close_pipe(sc->sc_out_pipe);
    625 		sc->sc_out_pipe = NULL;
    626 	}
    627 	if (sc->sc_in_pipe != NULL) {
    628 		usbd_abort_pipe(sc->sc_in_pipe);
    629 		usbd_close_pipe(sc->sc_in_pipe);
    630 		sc->sc_in_pipe = NULL;
    631 		if (sc->sc_in_xfer1 != NULL) {
    632 			usbd_free_xfer(sc->sc_in_xfer1);
    633 			sc->sc_in_xfer1 = NULL;
    634 		}
    635 		if (sc->sc_in_xfer2 != NULL) {
    636 			usbd_free_xfer(sc->sc_in_xfer2);
    637 			sc->sc_in_xfer2 = NULL;
    638 		}
    639 	}
    640 
    641 	sc->sc_state = 0;
    642 
    643 	DPRINTF(("ulptclose: closed\n"));
    644 	return (0);
    645 }
    646 
    647 int
    648 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
    649 {
    650 	u_int32_t n;
    651 	int error = 0;
    652 	void *bufp;
    653 	usbd_xfer_handle xfer;
    654 	usbd_status err;
    655 
    656 	DPRINTF(("ulptwrite\n"));
    657 	xfer = usbd_alloc_xfer(sc->sc_udev);
    658 	if (xfer == NULL)
    659 		return (ENOMEM);
    660 	bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
    661 	if (bufp == NULL) {
    662 		usbd_free_xfer(xfer);
    663 		return (ENOMEM);
    664 	}
    665 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    666 		ulpt_statusmsg(ulpt_status(sc), sc);
    667 		error = uiomove(bufp, n, uio);
    668 		if (error)
    669 			break;
    670 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    671 		err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
    672 			  USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
    673 		if (err) {
    674 			DPRINTF(("ulptwrite: error=%d\n", err));
    675 			error = EIO;
    676 			break;
    677 		}
    678 	}
    679 	usbd_free_xfer(xfer);
    680 
    681 	return (error);
    682 }
    683 
    684 int
    685 ulptwrite(dev_t dev, struct uio *uio, int flags)
    686 {
    687 	struct ulpt_softc *sc;
    688 	int error;
    689 
    690 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    691 
    692 	if (sc->sc_dying)
    693 		return (EIO);
    694 
    695 	sc->sc_refcnt++;
    696 	error = ulpt_do_write(sc, uio, flags);
    697 	if (--sc->sc_refcnt < 0)
    698 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    699 	return (error);
    700 }
    701 
    702 int
    703 ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
    704 {
    705 	int error = 0;
    706 
    707 	switch (cmd) {
    708 	default:
    709 		error = ENODEV;
    710 	}
    711 
    712 	return (error);
    713 }
    714 
    715 #if 0
    716 /* XXX This does not belong here. */
    717 /*
    718  * Print select parts of a IEEE 1284 device ID.
    719  */
    720 void
    721 ieee1284_print_id(char *str)
    722 {
    723 	char *p, *q;
    724 
    725 	for (p = str-1; p; p = strchr(p, ';')) {
    726 		p++;		/* skip ';' */
    727 		if (strncmp(p, "MFG:", 4) == 0 ||
    728 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    729 		    strncmp(p, "MDL:", 4) == 0 ||
    730 		    strncmp(p, "MODEL:", 6) == 0) {
    731 			q = strchr(p, ';');
    732 			if (q)
    733 				printf("%.*s", (int)(q - p + 1), p);
    734 		}
    735 	}
    736 }
    737 #endif
    738 
    739 #if defined(__FreeBSD__)
    740 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    741 #endif
    742