xref: /openbsd-src/sys/dev/usb/ulpt.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: ulpt.c,v 1.17 2003/05/19 00:35:43 nate Exp $ */
2 /*	$NetBSD: ulpt.c,v 1.57 2003/01/05 10:19:42 scw Exp $	*/
3 /*	$FreeBSD: src/sys/dev/usb/ulpt.c,v 1.24 1999/11/17 22:33:44 n_hibma Exp $	*/
4 
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*
43  * Printer Class spec:
44  *   http://www.usb.org/developers/data/devclass/usbprint11.pdf
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/device.h>
53 #include <sys/ioctl.h>
54 #elif defined(__FreeBSD__)
55 #include <sys/ioccom.h>
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #endif
59 #include <sys/uio.h>
60 #include <sys/conf.h>
61 #include <sys/vnode.h>
62 #include <sys/syslog.h>
63 
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbdevs.h>
68 #include <dev/usb/usb_quirks.h>
69 
70 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
71 #define	STEP		hz/4
72 
73 #define	LPTPRI		(PZERO+8)
74 #define	ULPT_BSIZE	16384
75 
76 #ifdef ULPT_DEBUG
77 #define DPRINTF(x)	if (ulptdebug) logprintf x
78 #define DPRINTFN(n,x)	if (ulptdebug>(n)) logprintf x
79 int	ulptdebug = 0;
80 #else
81 #define DPRINTF(x)
82 #define DPRINTFN(n,x)
83 #endif
84 
85 #define UR_GET_DEVICE_ID 0
86 #define UR_GET_PORT_STATUS 1
87 #define UR_SOFT_RESET 2
88 
89 #define	LPS_NERR		0x08	/* printer no error */
90 #define	LPS_SELECT		0x10	/* printer selected */
91 #define	LPS_NOPAPER		0x20	/* printer out of paper */
92 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
93 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
94 
95 struct ulpt_softc {
96 	USBBASEDEVICE sc_dev;
97 	usbd_device_handle sc_udev;	/* device */
98 	usbd_interface_handle sc_iface;	/* interface */
99 	int sc_ifaceno;
100 
101 	int sc_out;
102 	usbd_pipe_handle sc_out_pipe;	/* bulk out pipe */
103 
104 	int sc_in;
105 	usbd_pipe_handle sc_in_pipe;	/* bulk in pipe */
106 	usbd_xfer_handle sc_in_xfer1;
107 	usbd_xfer_handle sc_in_xfer2;
108 	u_char sc_junk[64];	/* somewhere to dump input */
109 
110 	u_char sc_state;
111 #define	ULPT_OPEN	0x01	/* device is open */
112 #define	ULPT_OBUSY	0x02	/* printer is busy doing output */
113 #define	ULPT_INIT	0x04	/* waiting to initialize for open */
114 	u_char sc_flags;
115 #define	ULPT_NOPRIME	0x40	/* don't prime on open */
116 	u_char sc_laststatus;
117 
118 	int sc_refcnt;
119 	u_char sc_dying;
120 
121 #if defined(__FreeBSD__)
122 	dev_t dev;
123 	dev_t dev_noprime;
124 #endif
125 };
126 
127 #if defined(__NetBSD__)
128 dev_type_open(ulptopen);
129 dev_type_close(ulptclose);
130 dev_type_write(ulptwrite);
131 dev_type_ioctl(ulptioctl);
132 
133 const struct cdevsw ulpt_cdevsw = {
134 	ulptopen, ulptclose, noread, ulptwrite, ulptioctl,
135 	nostop, notty, nopoll, nommap, nokqfilter,
136 };
137 #elif defined(__FreeBSD__)
138 Static d_open_t ulptopen;
139 Static d_close_t ulptclose;
140 Static d_write_t ulptwrite;
141 Static d_ioctl_t ulptioctl;
142 
143 #define ULPT_CDEV_MAJOR 113
144 
145 Static struct cdevsw ulpt_cdevsw = {
146 	/* open */	ulptopen,
147 	/* close */	ulptclose,
148 	/* read */	noread,
149 	/* write */	ulptwrite,
150 	/* ioctl */	ulptioctl,
151 	/* poll */	nopoll,
152 	/* mmap */	nommap,
153 	/* strategy */	nostrategy,
154 	/* name */	"ulpt",
155 	/* maj */	ULPT_CDEV_MAJOR,
156 	/* dump */	nodump,
157 	/* psize */	nopsize,
158 	/* flags */	0,
159 #if !defined(__FreeBSD__) || (__FreeBSD__ < 5)
160 	/* bmaj */	-1
161 #endif
162 };
163 #endif
164 
165 void ulpt_disco(void *);
166 
167 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
168 int ulpt_status(struct ulpt_softc *);
169 void ulpt_reset(struct ulpt_softc *);
170 int ulpt_statusmsg(u_char, struct ulpt_softc *);
171 
172 #if 0
173 void ieee1284_print_id(char *);
174 #endif
175 
176 #define	ULPTUNIT(s)	(minor(s) & 0x1f)
177 #define	ULPTFLAGS(s)	(minor(s) & 0xe0)
178 
179 
180 USB_DECLARE_DRIVER(ulpt);
181 
182 USB_MATCH(ulpt)
183 {
184 	USB_MATCH_START(ulpt, uaa);
185 	usb_interface_descriptor_t *id;
186 
187 	DPRINTFN(10,("ulpt_match\n"));
188 	if (uaa->iface == NULL)
189 		return (UMATCH_NONE);
190 	id = usbd_get_interface_descriptor(uaa->iface);
191 	if (id != NULL &&
192 	    id->bInterfaceClass == UICLASS_PRINTER &&
193 	    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
194 	    ((id->bInterfaceProtocol == UIPROTO_PRINTER_UNI) ||
195 	     (id->bInterfaceProtocol == UIPROTO_PRINTER_BI) ||
196 	     (id->bInterfaceProtocol == UIPROTO_PRINTER_1284)))
197 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
198 	return (UMATCH_NONE);
199 }
200 
201 USB_ATTACH(ulpt)
202 {
203 	USB_ATTACH_START(ulpt, sc, uaa);
204 	usbd_device_handle dev = uaa->device;
205 	usbd_interface_handle iface = uaa->iface;
206 	usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
207 	usb_interface_descriptor_t *id, *iend;
208 	usb_config_descriptor_t *cdesc;
209 	usbd_status err;
210 	char devinfo[1024];
211 	usb_endpoint_descriptor_t *ed;
212 	u_int8_t epcount;
213 	int i, altno;
214 
215 	DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
216 	usbd_devinfo(dev, 0, devinfo, sizeof devinfo);
217 	USB_ATTACH_SETUP;
218 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
219 	       devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
220 
221 	/* XXX
222 	 * Stepping through the alternate settings needs to be abstracted out.
223 	 */
224 	cdesc = usbd_get_config_descriptor(dev);
225 	if (cdesc == NULL) {
226 		printf("%s: failed to get configuration descriptor\n",
227 		       USBDEVNAME(sc->sc_dev));
228 		USB_ATTACH_ERROR_RETURN;
229 	}
230 	iend = (usb_interface_descriptor_t *)
231 		   ((char *)cdesc + UGETW(cdesc->wTotalLength));
232 #ifdef DIAGNOSTIC
233 	if (ifcd < (usb_interface_descriptor_t *)cdesc ||
234 	    ifcd >= iend)
235 		panic("ulpt: iface desc out of range");
236 #endif
237 	/* Step through all the descriptors looking for bidir mode */
238 	for (id = ifcd, altno = 0;
239 	     id < iend;
240 	     id = (void *)((char *)id + id->bLength)) {
241 		if (id->bDescriptorType == UDESC_INTERFACE &&
242 		    id->bInterfaceNumber == ifcd->bInterfaceNumber) {
243 			if (id->bInterfaceClass == UICLASS_PRINTER &&
244 			    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
245 			    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
246 			     id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
247 				goto found;
248 			altno++;
249 		}
250 	}
251 	id = ifcd;		/* not found, use original */
252  found:
253 	if (id != ifcd) {
254 		/* Found a new bidir setting */
255 		DPRINTF(("ulpt_attach: set altno = %d\n", altno));
256 		err = usbd_set_interface(iface, altno);
257 		if (err) {
258 			printf("%s: setting alternate interface failed\n",
259 			       USBDEVNAME(sc->sc_dev));
260 			sc->sc_dying = 1;
261 			USB_ATTACH_ERROR_RETURN;
262 		}
263 	}
264 
265 	epcount = 0;
266 	(void)usbd_endpoint_count(iface, &epcount);
267 
268 	sc->sc_in = -1;
269 	sc->sc_out = -1;
270 	for (i = 0; i < epcount; i++) {
271 		ed = usbd_interface2endpoint_descriptor(iface, i);
272 		if (ed == NULL) {
273 			printf("%s: couldn't get ep %d\n",
274 			    USBDEVNAME(sc->sc_dev), i);
275 			USB_ATTACH_ERROR_RETURN;
276 		}
277 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
278 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
279 			sc->sc_in = ed->bEndpointAddress;
280 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
281 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
282 			sc->sc_out = ed->bEndpointAddress;
283 		}
284 	}
285 	if (sc->sc_out == -1) {
286 		printf("%s: could not find bulk out endpoint\n",
287 		    USBDEVNAME(sc->sc_dev));
288 		sc->sc_dying = 1;
289 		USB_ATTACH_ERROR_RETURN;
290 	}
291 
292 	if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
293 		/* This device doesn't handle reading properly. */
294 		sc->sc_in = -1;
295 	}
296 
297 	printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
298 	       sc->sc_in >= 0 ? "bi" : "uni");
299 
300 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
301 
302 	sc->sc_iface = iface;
303 	sc->sc_ifaceno = id->bInterfaceNumber;
304 	sc->sc_udev = dev;
305 
306 #if 0
307 /*
308  * This code is disabled because for some mysterious reason it causes
309  * printing not to work.  But only sometimes, and mostly with
310  * UHCI and less often with OHCI.  *sigh*
311  */
312 	{
313 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
314 	usb_device_request_t req;
315 	int len, alen;
316 
317 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
318 	req.bRequest = UR_GET_DEVICE_ID;
319 	USETW(req.wValue, cd->bConfigurationValue);
320 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
321 	USETW(req.wLength, sizeof devinfo - 1);
322 	err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
323 		  &alen, USBD_DEFAULT_TIMEOUT);
324 	if (err) {
325 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
326 	} else if (alen <= 2) {
327 		printf("%s: empty device id, no printer connected?\n",
328 		       USBDEVNAME(sc->sc_dev));
329 	} else {
330 		/* devinfo now contains an IEEE-1284 device ID */
331 		len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
332 		if (len > sizeof devinfo - 3)
333 			len = sizeof devinfo - 3;
334 		devinfo[len] = 0;
335 		printf("%s: device id <", USBDEVNAME(sc->sc_dev));
336 		ieee1284_print_id(devinfo+2);
337 		printf(">\n");
338 	}
339 	}
340 #endif
341 
342 #if defined(__FreeBSD__)
343 	sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
344 		UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
345 	sc->dev_noprime = make_dev(&ulpt_cdevsw,
346 		device_get_unit(self)|ULPT_NOPRIME,
347 		UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
348 #endif
349 
350 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
351 			   USBDEV(sc->sc_dev));
352 
353 	USB_ATTACH_SUCCESS_RETURN;
354 }
355 
356 #if defined(__NetBSD__) || defined(__OpenBSD__)
357 int
358 ulpt_activate(device_ptr_t self, enum devact act)
359 {
360 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
361 
362 	switch (act) {
363 	case DVACT_ACTIVATE:
364 		return (EOPNOTSUPP);
365 
366 	case DVACT_DEACTIVATE:
367 		sc->sc_dying = 1;
368 		break;
369 	}
370 	return (0);
371 }
372 #endif
373 
374 USB_DETACH(ulpt)
375 {
376 	USB_DETACH_START(ulpt, sc);
377 	int s;
378 #if defined(__NetBSD__) || defined(__OpenBSD__)
379 	int maj, mn;
380 #elif defined(__FreeBSD__)
381 	struct vnode *vp;
382 #endif
383 
384 	DPRINTF(("ulpt_detach: sc=%p\n", sc));
385 
386 	sc->sc_dying = 1;
387 	if (sc->sc_out_pipe != NULL)
388 		usbd_abort_pipe(sc->sc_out_pipe);
389 	if (sc->sc_in_pipe != NULL)
390 		usbd_abort_pipe(sc->sc_in_pipe);
391 
392 	s = splusb();
393 	if (--sc->sc_refcnt >= 0) {
394 		/* There is noone to wake, aborting the pipe is enough */
395 		/* Wait for processes to go away. */
396 		usb_detach_wait(USBDEV(sc->sc_dev));
397 	}
398 	splx(s);
399 
400 #if defined(__NetBSD__) || defined(__OpenBSD__)
401 	/* locate the major number */
402 #if defined(__NetBSD__)
403 	maj = cdevsw_lookup_major(&ulpt_cdevsw);
404 #elif defined(__OpenBSD__)
405 	for (maj = 0; maj < nchrdev; maj++)
406 		if (cdevsw[maj].d_open == ulptopen)
407 			break;
408 #endif
409 
410 	/* Nuke the vnodes for any open instances (calls close). */
411 	mn = self->dv_unit;
412 	vdevgone(maj, mn, mn, VCHR);
413 	vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
414 #elif defined(__FreeBSD__)
415 	vp = SLIST_FIRST(&sc->dev->si_hlist);
416 	if (vp)
417 		VOP_REVOKE(vp, REVOKEALL);
418 	vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
419 	if (vp)
420 		VOP_REVOKE(vp, REVOKEALL);
421 
422 	destroy_dev(sc->dev);
423 	destroy_dev(sc->dev_noprime);
424 #endif
425 
426 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
427 			   USBDEV(sc->sc_dev));
428 
429 	return (0);
430 }
431 
432 int
433 ulpt_status(struct ulpt_softc *sc)
434 {
435 	usb_device_request_t req;
436 	usbd_status err;
437 	u_char status;
438 
439 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
440 	req.bRequest = UR_GET_PORT_STATUS;
441 	USETW(req.wValue, 0);
442 	USETW(req.wIndex, sc->sc_ifaceno);
443 	USETW(req.wLength, 1);
444 	err = usbd_do_request(sc->sc_udev, &req, &status);
445 	DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
446 	if (!err)
447 		return (status);
448 	else
449 		return (0);
450 }
451 
452 void
453 ulpt_reset(struct ulpt_softc *sc)
454 {
455 	usb_device_request_t req;
456 
457 	DPRINTFN(1, ("ulpt_reset\n"));
458 	req.bRequest = UR_SOFT_RESET;
459 	USETW(req.wValue, 0);
460 	USETW(req.wIndex, sc->sc_ifaceno);
461 	USETW(req.wLength, 0);
462 
463 	/*
464 	 * There was a mistake in the USB printer 1.0 spec that gave the
465 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
466 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
467 	 * so we try both.
468 	 */
469 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
470 	if (usbd_do_request(sc->sc_udev, &req, 0)) {	/* 1.0 */
471 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
472 		(void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
473 	}
474 }
475 
476 static void
477 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
478 {
479 	struct ulpt_softc *sc = priv;
480 
481 	DPRINTFN(2,("ulpt_input: got some data\n"));
482 	/* Do it again. */
483 	if (xfer == sc->sc_in_xfer1)
484 		usbd_transfer(sc->sc_in_xfer2);
485 	else
486 		usbd_transfer(sc->sc_in_xfer1);
487 }
488 
489 int ulptusein = 1;
490 
491 /*
492  * Reset the printer, then wait until it's selected and not busy.
493  */
494 int
495 ulptopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
496 {
497 	u_char flags = ULPTFLAGS(dev);
498 	struct ulpt_softc *sc;
499 	usbd_status err;
500 	int spin, error;
501 
502 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
503 
504 	if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
505 		return (ENXIO);
506 
507 	if (sc->sc_state)
508 		return (EBUSY);
509 
510 	sc->sc_state = ULPT_INIT;
511 	sc->sc_flags = flags;
512 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
513 
514 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
515 	/* Ignoring these flags might not be a good idea */
516 	if ((flags & ~ULPT_NOPRIME) != 0)
517 		printf("ulptopen: flags ignored: %b\n", flags,
518 			"\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
519 #endif
520 
521 
522 	error = 0;
523 	sc->sc_refcnt++;
524 
525 	if ((flags & ULPT_NOPRIME) == 0)
526 		ulpt_reset(sc);
527 
528 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
529 		DPRINTF(("ulpt_open: waiting a while\n"));
530 		if (spin >= TIMEOUT) {
531 			error = EBUSY;
532 			sc->sc_state = 0;
533 			goto done;
534 		}
535 
536 		/* wait 1/4 second, give up if we get a signal */
537 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
538 		if (error != EWOULDBLOCK) {
539 			sc->sc_state = 0;
540 			goto done;
541 		}
542 
543 		if (sc->sc_dying) {
544 			error = ENXIO;
545 			sc->sc_state = 0;
546 			goto done;
547 		}
548 	}
549 
550 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
551 	if (err) {
552 		sc->sc_state = 0;
553 		error = EIO;
554 		goto done;
555 	}
556 	if (ulptusein && sc->sc_in != -1) {
557 		DPRINTF(("ulpt_open: open input pipe\n"));
558 		err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
559 		if (err) {
560 			error = EIO;
561 			usbd_close_pipe(sc->sc_out_pipe);
562 			sc->sc_out_pipe = NULL;
563 			sc->sc_state = 0;
564 			goto done;
565 		}
566 		sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
567 		sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
568 		if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
569 			error = ENOMEM;
570 			if (sc->sc_in_xfer1 != NULL) {
571 				usbd_free_xfer(sc->sc_in_xfer1);
572 				sc->sc_in_xfer1 = NULL;
573 			}
574 			if (sc->sc_in_xfer2 != NULL) {
575 				usbd_free_xfer(sc->sc_in_xfer2);
576 				sc->sc_in_xfer2 = NULL;
577 			}
578 			usbd_close_pipe(sc->sc_out_pipe);
579 			sc->sc_out_pipe = NULL;
580 			usbd_close_pipe(sc->sc_in_pipe);
581 			sc->sc_in_pipe = NULL;
582 			sc->sc_state = 0;
583 			goto done;
584 		}
585 		usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
586 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
587 		    USBD_NO_TIMEOUT, ulpt_input);
588 		usbd_setup_xfer(sc->sc_in_xfer2, 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_transfer(sc->sc_in_xfer1); /* ignore failed start */
592 	}
593 
594 	sc->sc_state = ULPT_OPEN;
595 
596  done:
597 	if (--sc->sc_refcnt < 0)
598 		usb_detach_wakeup(USBDEV(sc->sc_dev));
599 
600 	DPRINTF(("ulptopen: done, error=%d\n", error));
601 	return (error);
602 }
603 
604 int
605 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
606 {
607 	u_char new;
608 
609 	status = (status ^ LPS_INVERT) & LPS_MASK;
610 	new = status & ~sc->sc_laststatus;
611 	sc->sc_laststatus = status;
612 
613 	if (new & LPS_SELECT)
614 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
615 	else if (new & LPS_NOPAPER)
616 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
617 	else if (new & LPS_NERR)
618 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
619 
620 	return (status);
621 }
622 
623 int
624 ulptclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
625 {
626 	struct ulpt_softc *sc;
627 
628 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
629 
630 	if (sc->sc_state != ULPT_OPEN)
631 		/* We are being forced to close before the open completed. */
632 		return (0);
633 
634 	if (sc->sc_out_pipe != NULL) {
635 		usbd_close_pipe(sc->sc_out_pipe);
636 		sc->sc_out_pipe = NULL;
637 	}
638 	if (sc->sc_in_pipe != NULL) {
639 		usbd_abort_pipe(sc->sc_in_pipe);
640 		usbd_close_pipe(sc->sc_in_pipe);
641 		sc->sc_in_pipe = NULL;
642 		if (sc->sc_in_xfer1 != NULL) {
643 			usbd_free_xfer(sc->sc_in_xfer1);
644 			sc->sc_in_xfer1 = NULL;
645 		}
646 		if (sc->sc_in_xfer2 != NULL) {
647 			usbd_free_xfer(sc->sc_in_xfer2);
648 			sc->sc_in_xfer2 = NULL;
649 		}
650 	}
651 
652 	sc->sc_state = 0;
653 
654 	DPRINTF(("ulptclose: closed\n"));
655 	return (0);
656 }
657 
658 int
659 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
660 {
661 	u_int32_t n;
662 	int error = 0;
663 	void *bufp;
664 	usbd_xfer_handle xfer;
665 	usbd_status err;
666 
667 	DPRINTF(("ulptwrite\n"));
668 	xfer = usbd_alloc_xfer(sc->sc_udev);
669 	if (xfer == NULL)
670 		return (ENOMEM);
671 	bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
672 	if (bufp == NULL) {
673 		usbd_free_xfer(xfer);
674 		return (ENOMEM);
675 	}
676 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
677 		ulpt_statusmsg(ulpt_status(sc), sc);
678 		error = uiomove(bufp, n, uio);
679 		if (error)
680 			break;
681 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
682 		err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
683 			  USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
684 		if (err) {
685 			DPRINTF(("ulptwrite: error=%d\n", err));
686 			error = EIO;
687 			break;
688 		}
689 	}
690 	usbd_free_xfer(xfer);
691 
692 	return (error);
693 }
694 
695 int
696 ulptwrite(dev_t dev, struct uio *uio, int flags)
697 {
698 	struct ulpt_softc *sc;
699 	int error;
700 
701 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
702 
703 	if (sc->sc_dying)
704 		return (EIO);
705 
706 	sc->sc_refcnt++;
707 	error = ulpt_do_write(sc, uio, flags);
708 	if (--sc->sc_refcnt < 0)
709 		usb_detach_wakeup(USBDEV(sc->sc_dev));
710 	return (error);
711 }
712 
713 int
714 ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
715 {
716 	int error = 0;
717 
718 	switch (cmd) {
719 	default:
720 		error = ENODEV;
721 	}
722 
723 	return (error);
724 }
725 
726 #if 0
727 /* XXX This does not belong here. */
728 /*
729  * Print select parts of a IEEE 1284 device ID.
730  */
731 void
732 ieee1284_print_id(char *str)
733 {
734 	char *p, *q;
735 
736 	for (p = str-1; p; p = strchr(p, ';')) {
737 		p++;		/* skip ';' */
738 		if (strncmp(p, "MFG:", 4) == 0 ||
739 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
740 		    strncmp(p, "MDL:", 4) == 0 ||
741 		    strncmp(p, "MODEL:", 6) == 0) {
742 			q = strchr(p, ';');
743 			if (q)
744 				printf("%.*s", (int)(q - p + 1), p);
745 		}
746 	}
747 }
748 #endif
749 
750 #if defined(__FreeBSD__)
751 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
752 #endif
753