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