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