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