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