Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.49.10.3
      1 /*	$NetBSD: ulpt.c,v 1.49.10.3 2003/01/27 06:05:48 jmc 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.10.3 2003/01/27 06:05:48 jmc 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 	vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
    404 #elif defined(__FreeBSD__)
    405 	vp = SLIST_FIRST(&sc->dev->si_hlist);
    406 	if (vp)
    407 		VOP_REVOKE(vp, REVOKEALL);
    408 	vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
    409 	if (vp)
    410 		VOP_REVOKE(vp, REVOKEALL);
    411 
    412 	destroy_dev(sc->dev);
    413 	destroy_dev(sc->dev_noprime);
    414 #endif
    415 
    416 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    417 			   USBDEV(sc->sc_dev));
    418 
    419 	return (0);
    420 }
    421 
    422 int
    423 ulpt_status(struct ulpt_softc *sc)
    424 {
    425 	usb_device_request_t req;
    426 	usbd_status err;
    427 	u_char status;
    428 
    429 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    430 	req.bRequest = UR_GET_PORT_STATUS;
    431 	USETW(req.wValue, 0);
    432 	USETW(req.wIndex, sc->sc_ifaceno);
    433 	USETW(req.wLength, 1);
    434 	err = usbd_do_request(sc->sc_udev, &req, &status);
    435 	DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
    436 	if (!err)
    437 		return (status);
    438 	else
    439 		return (0);
    440 }
    441 
    442 void
    443 ulpt_reset(struct ulpt_softc *sc)
    444 {
    445 	usb_device_request_t req;
    446 
    447 	DPRINTFN(1, ("ulpt_reset\n"));
    448 	req.bRequest = UR_SOFT_RESET;
    449 	USETW(req.wValue, 0);
    450 	USETW(req.wIndex, sc->sc_ifaceno);
    451 	USETW(req.wLength, 0);
    452 
    453 	/*
    454 	 * There was a mistake in the USB printer 1.0 spec that gave the
    455 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
    456 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
    457 	 * so we try both.
    458 	 */
    459 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    460 	if (usbd_do_request(sc->sc_udev, &req, 0)) {	/* 1.0 */
    461 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    462 		(void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
    463 	}
    464 }
    465 
    466 static void
    467 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    468 {
    469 	struct ulpt_softc *sc = priv;
    470 
    471 	DPRINTFN(2,("ulpt_input: got some data\n"));
    472 	/* Do it again. */
    473 	if (xfer == sc->sc_in_xfer1)
    474 		usbd_transfer(sc->sc_in_xfer2);
    475 	else
    476 		usbd_transfer(sc->sc_in_xfer1);
    477 }
    478 
    479 int ulptusein = 1;
    480 
    481 /*
    482  * Reset the printer, then wait until it's selected and not busy.
    483  */
    484 int
    485 ulptopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    486 {
    487 	u_char flags = ULPTFLAGS(dev);
    488 	struct ulpt_softc *sc;
    489 	usbd_status err;
    490 	int spin, error;
    491 
    492 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    493 
    494 	if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
    495 		return (ENXIO);
    496 
    497 	if (sc->sc_state)
    498 		return (EBUSY);
    499 
    500 	sc->sc_state = ULPT_INIT;
    501 	sc->sc_flags = flags;
    502 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    503 
    504 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
    505 	/* Ignoring these flags might not be a good idea */
    506 	if ((flags & ~ULPT_NOPRIME) != 0)
    507 		printf("ulptopen: flags ignored: %b\n", flags,
    508 			"\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
    509 #endif
    510 
    511 
    512 	error = 0;
    513 	sc->sc_refcnt++;
    514 
    515 	if ((flags & ULPT_NOPRIME) == 0)
    516 		ulpt_reset(sc);
    517 
    518 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    519 		DPRINTF(("ulpt_open: waiting a while\n"));
    520 		if (spin >= TIMEOUT) {
    521 			error = EBUSY;
    522 			sc->sc_state = 0;
    523 			goto done;
    524 		}
    525 
    526 		/* wait 1/4 second, give up if we get a signal */
    527 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    528 		if (error != EWOULDBLOCK) {
    529 			sc->sc_state = 0;
    530 			goto done;
    531 		}
    532 
    533 		if (sc->sc_dying) {
    534 			error = ENXIO;
    535 			sc->sc_state = 0;
    536 			goto done;
    537 		}
    538 	}
    539 
    540 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
    541 	if (err) {
    542 		sc->sc_state = 0;
    543 		error = EIO;
    544 		goto done;
    545 	}
    546 	if (ulptusein && sc->sc_in != -1) {
    547 		DPRINTF(("ulpt_open: open input pipe\n"));
    548 		err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
    549 		if (err) {
    550 			error = EIO;
    551 			usbd_close_pipe(sc->sc_out_pipe);
    552 			sc->sc_out_pipe = NULL;
    553 			sc->sc_state = 0;
    554 			goto done;
    555 		}
    556 		sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
    557 		sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
    558 		if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
    559 			error = ENOMEM;
    560 			if (sc->sc_in_xfer1 != NULL) {
    561 				usbd_free_xfer(sc->sc_in_xfer1);
    562 				sc->sc_in_xfer1 = NULL;
    563 			}
    564 			if (sc->sc_in_xfer2 != NULL) {
    565 				usbd_free_xfer(sc->sc_in_xfer2);
    566 				sc->sc_in_xfer2 = NULL;
    567 			}
    568 			usbd_close_pipe(sc->sc_out_pipe);
    569 			sc->sc_out_pipe = NULL;
    570 			usbd_close_pipe(sc->sc_in_pipe);
    571 			sc->sc_in_pipe = NULL;
    572 			sc->sc_state = 0;
    573 			goto done;
    574 		}
    575 		usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
    576 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    577 		    USBD_NO_TIMEOUT, ulpt_input);
    578 		usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
    579 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    580 		    USBD_NO_TIMEOUT, ulpt_input);
    581 		usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
    582 	}
    583 
    584 	sc->sc_state = ULPT_OPEN;
    585 
    586  done:
    587 	if (--sc->sc_refcnt < 0)
    588 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    589 
    590 	DPRINTF(("ulptopen: done, error=%d\n", error));
    591 	return (error);
    592 }
    593 
    594 int
    595 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
    596 {
    597 	u_char new;
    598 
    599 	status = (status ^ LPS_INVERT) & LPS_MASK;
    600 	new = status & ~sc->sc_laststatus;
    601 	sc->sc_laststatus = status;
    602 
    603 	if (new & LPS_SELECT)
    604 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    605 	else if (new & LPS_NOPAPER)
    606 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    607 	else if (new & LPS_NERR)
    608 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    609 
    610 	return (status);
    611 }
    612 
    613 int
    614 ulptclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    615 {
    616 	struct ulpt_softc *sc;
    617 
    618 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    619 
    620 	if (sc->sc_state != ULPT_OPEN)
    621 		/* We are being forced to close before the open completed. */
    622 		return (0);
    623 
    624 	if (sc->sc_out_pipe != NULL) {
    625 		usbd_close_pipe(sc->sc_out_pipe);
    626 		sc->sc_out_pipe = NULL;
    627 	}
    628 	if (sc->sc_in_pipe != NULL) {
    629 		usbd_abort_pipe(sc->sc_in_pipe);
    630 		usbd_close_pipe(sc->sc_in_pipe);
    631 		sc->sc_in_pipe = NULL;
    632 		if (sc->sc_in_xfer1 != NULL) {
    633 			usbd_free_xfer(sc->sc_in_xfer1);
    634 			sc->sc_in_xfer1 = NULL;
    635 		}
    636 		if (sc->sc_in_xfer2 != NULL) {
    637 			usbd_free_xfer(sc->sc_in_xfer2);
    638 			sc->sc_in_xfer2 = NULL;
    639 		}
    640 	}
    641 
    642 	sc->sc_state = 0;
    643 
    644 	DPRINTF(("ulptclose: closed\n"));
    645 	return (0);
    646 }
    647 
    648 int
    649 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
    650 {
    651 	u_int32_t n;
    652 	int error = 0;
    653 	void *bufp;
    654 	usbd_xfer_handle xfer;
    655 	usbd_status err;
    656 
    657 	DPRINTF(("ulptwrite\n"));
    658 	xfer = usbd_alloc_xfer(sc->sc_udev);
    659 	if (xfer == NULL)
    660 		return (ENOMEM);
    661 	bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
    662 	if (bufp == NULL) {
    663 		usbd_free_xfer(xfer);
    664 		return (ENOMEM);
    665 	}
    666 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    667 		ulpt_statusmsg(ulpt_status(sc), sc);
    668 		error = uiomove(bufp, n, uio);
    669 		if (error)
    670 			break;
    671 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    672 		err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
    673 			  USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
    674 		if (err) {
    675 			DPRINTF(("ulptwrite: error=%d\n", err));
    676 			error = EIO;
    677 			break;
    678 		}
    679 	}
    680 	usbd_free_xfer(xfer);
    681 
    682 	return (error);
    683 }
    684 
    685 int
    686 ulptwrite(dev_t dev, struct uio *uio, int flags)
    687 {
    688 	struct ulpt_softc *sc;
    689 	int error;
    690 
    691 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    692 
    693 	if (sc->sc_dying)
    694 		return (EIO);
    695 
    696 	sc->sc_refcnt++;
    697 	error = ulpt_do_write(sc, uio, flags);
    698 	if (--sc->sc_refcnt < 0)
    699 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    700 	return (error);
    701 }
    702 
    703 int
    704 ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
    705 {
    706 	int error = 0;
    707 
    708 	switch (cmd) {
    709 	default:
    710 		error = ENODEV;
    711 	}
    712 
    713 	return (error);
    714 }
    715 
    716 #if 0
    717 /* XXX This does not belong here. */
    718 /*
    719  * Print select parts of a IEEE 1284 device ID.
    720  */
    721 void
    722 ieee1284_print_id(char *str)
    723 {
    724 	char *p, *q;
    725 
    726 	for (p = str-1; p; p = strchr(p, ';')) {
    727 		p++;		/* skip ';' */
    728 		if (strncmp(p, "MFG:", 4) == 0 ||
    729 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    730 		    strncmp(p, "MDL:", 4) == 0 ||
    731 		    strncmp(p, "MODEL:", 6) == 0) {
    732 			q = strchr(p, ';');
    733 			if (q)
    734 				printf("%.*s", (int)(q - p + 1), p);
    735 		}
    736 	}
    737 }
    738 #endif
    739 
    740 #if defined(__FreeBSD__)
    741 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    742 #endif
    743