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