xref: /freebsd/sys/dev/usb/serial/ulpt.c (revision 0957b409)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 /*	$NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $	*/
5 
6 /*-
7  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
8  *
9  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
10  * All rights reserved.
11  *
12  * This code is derived from software contributed to The NetBSD Foundation
13  * by Lennart Augustsson (lennart@augustsson.net) at
14  * Carlstedt Research & Technology.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
40  * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
41  */
42 
43 #include <sys/stdint.h>
44 #include <sys/stddef.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/types.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/bus.h>
51 #include <sys/module.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/condvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/sx.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
60 #include <sys/priv.h>
61 #include <sys/syslog.h>
62 #include <sys/selinfo.h>
63 #include <sys/conf.h>
64 #include <sys/fcntl.h>
65 
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbhid.h>
70 #include "usbdevs.h"
71 
72 #define	USB_DEBUG_VAR ulpt_debug
73 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/usb_process.h>
75 
76 #ifdef USB_DEBUG
77 static int ulpt_debug = 0;
78 
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
80 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RWTUN,
81     &ulpt_debug, 0, "Debug level");
82 #endif
83 
84 #define	ULPT_BSIZE		(1<<15)	/* bytes */
85 #define	ULPT_IFQ_MAXLEN         2	/* units */
86 
87 #define	UR_GET_DEVICE_ID        0x00
88 #define	UR_GET_PORT_STATUS      0x01
89 #define	UR_SOFT_RESET           0x02
90 
91 #define	LPS_NERR		0x08	/* printer no error */
92 #define	LPS_SELECT		0x10	/* printer selected */
93 #define	LPS_NOPAPER		0x20	/* printer out of paper */
94 #define	LPS_INVERT      (LPS_SELECT|LPS_NERR)
95 #define	LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
96 
97 enum {
98 	ULPT_BULK_DT_WR,
99 	ULPT_BULK_DT_RD,
100 	ULPT_INTR_DT_RD,
101 	ULPT_N_TRANSFER,
102 };
103 
104 struct ulpt_softc {
105 	struct usb_fifo_sc sc_fifo;
106 	struct usb_fifo_sc sc_fifo_noreset;
107 	struct mtx sc_mtx;
108 	struct usb_callout sc_watchdog;
109 
110 	device_t sc_dev;
111 	struct usb_device *sc_udev;
112 	struct usb_fifo *sc_fifo_open[2];
113 	struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
114 
115 	int	sc_fflags;		/* current open flags, FREAD and
116 					 * FWRITE */
117 	uint8_t	sc_iface_no;
118 	uint8_t	sc_last_status;
119 	uint8_t	sc_zlps;		/* number of consequtive zero length
120 					 * packets received */
121 };
122 
123 /* prototypes */
124 
125 static device_probe_t ulpt_probe;
126 static device_attach_t ulpt_attach;
127 static device_detach_t ulpt_detach;
128 
129 static usb_callback_t ulpt_write_callback;
130 static usb_callback_t ulpt_read_callback;
131 static usb_callback_t ulpt_status_callback;
132 
133 static void	ulpt_reset(struct ulpt_softc *);
134 static void	ulpt_watchdog(void *);
135 
136 static usb_fifo_close_t ulpt_close;
137 static usb_fifo_cmd_t ulpt_start_read;
138 static usb_fifo_cmd_t ulpt_start_write;
139 static usb_fifo_cmd_t ulpt_stop_read;
140 static usb_fifo_cmd_t ulpt_stop_write;
141 static usb_fifo_ioctl_t ulpt_ioctl;
142 static usb_fifo_open_t ulpt_open;
143 static usb_fifo_open_t unlpt_open;
144 
145 static struct usb_fifo_methods ulpt_fifo_methods = {
146 	.f_close = &ulpt_close,
147 	.f_ioctl = &ulpt_ioctl,
148 	.f_open = &ulpt_open,
149 	.f_start_read = &ulpt_start_read,
150 	.f_start_write = &ulpt_start_write,
151 	.f_stop_read = &ulpt_stop_read,
152 	.f_stop_write = &ulpt_stop_write,
153 	.basename[0] = "ulpt",
154 };
155 
156 static struct usb_fifo_methods unlpt_fifo_methods = {
157 	.f_close = &ulpt_close,
158 	.f_ioctl = &ulpt_ioctl,
159 	.f_open = &unlpt_open,
160 	.f_start_read = &ulpt_start_read,
161 	.f_start_write = &ulpt_start_write,
162 	.f_stop_read = &ulpt_stop_read,
163 	.f_stop_write = &ulpt_stop_write,
164 	.basename[0] = "unlpt",
165 };
166 
167 static void
168 ulpt_reset(struct ulpt_softc *sc)
169 {
170 	struct usb_device_request req;
171 
172 	DPRINTFN(2, "\n");
173 
174 	req.bRequest = UR_SOFT_RESET;
175 	USETW(req.wValue, 0);
176 	USETW(req.wIndex, sc->sc_iface_no);
177 	USETW(req.wLength, 0);
178 
179 	/*
180 	 * There was a mistake in the USB printer 1.0 spec that gave the
181 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
182 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
183 	 * so we try both.
184 	 */
185 
186 	mtx_lock(&sc->sc_mtx);
187 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
188 	if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
189 	    &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {	/* 1.0 */
190 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
191 		if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
192 		    &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {	/* 1.1 */
193 			/* ignore error */
194 		}
195 	}
196 	mtx_unlock(&sc->sc_mtx);
197 }
198 
199 static void
200 ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
201 {
202 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
203 	struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
204 	struct usb_page_cache *pc;
205 	int actlen, max;
206 
207 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
208 
209 	if (f == NULL) {
210 		/* should not happen */
211 		DPRINTF("no FIFO\n");
212 		return;
213 	}
214 	DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
215 
216 	switch (USB_GET_STATE(xfer)) {
217 	case USB_ST_TRANSFERRED:
218 	case USB_ST_SETUP:
219 tr_setup:
220 		pc = usbd_xfer_get_frame(xfer, 0);
221 		max = usbd_xfer_max_len(xfer);
222 		if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
223 			usbd_xfer_set_frame_len(xfer, 0, actlen);
224 			usbd_transfer_submit(xfer);
225 		}
226 		break;
227 
228 	default:			/* Error */
229 		if (error != USB_ERR_CANCELLED) {
230 			/* try to clear stall first */
231 			usbd_xfer_set_stall(xfer);
232 			goto tr_setup;
233 		}
234 		break;
235 	}
236 }
237 
238 static void
239 ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
240 {
241 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
242 	struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
243 	struct usb_page_cache *pc;
244 	int actlen;
245 
246 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
247 
248 	if (f == NULL) {
249 		/* should not happen */
250 		DPRINTF("no FIFO\n");
251 		return;
252 	}
253 	DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
254 
255 	switch (USB_GET_STATE(xfer)) {
256 	case USB_ST_TRANSFERRED:
257 
258 		if (actlen == 0) {
259 
260 			if (sc->sc_zlps == 4) {
261 				/* enable BULK throttle */
262 				usbd_xfer_set_interval(xfer, 500); /* ms */
263 			} else {
264 				sc->sc_zlps++;
265 			}
266 		} else {
267 			/* disable BULK throttle */
268 
269 			usbd_xfer_set_interval(xfer, 0);
270 			sc->sc_zlps = 0;
271 		}
272 
273 		pc = usbd_xfer_get_frame(xfer, 0);
274 		usb_fifo_put_data(f, pc, 0, actlen, 1);
275 
276 	case USB_ST_SETUP:
277 tr_setup:
278 		if (usb_fifo_put_bytes_max(f) != 0) {
279 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
280 			usbd_transfer_submit(xfer);
281 		}
282 		break;
283 
284 	default:			/* Error */
285 		/* disable BULK throttle */
286 		usbd_xfer_set_interval(xfer, 0);
287 		sc->sc_zlps = 0;
288 
289 		if (error != USB_ERR_CANCELLED) {
290 			/* try to clear stall first */
291 			usbd_xfer_set_stall(xfer);
292 			goto tr_setup;
293 		}
294 		break;
295 	}
296 }
297 
298 static void
299 ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
300 {
301 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
302 	struct usb_device_request req;
303 	struct usb_page_cache *pc;
304 	uint8_t cur_status;
305 	uint8_t new_status;
306 
307 	switch (USB_GET_STATE(xfer)) {
308 	case USB_ST_TRANSFERRED:
309 
310 		pc = usbd_xfer_get_frame(xfer, 1);
311 		usbd_copy_out(pc, 0, &cur_status, 1);
312 
313 		cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
314 		new_status = cur_status & ~sc->sc_last_status;
315 		sc->sc_last_status = cur_status;
316 
317 		if (new_status & LPS_SELECT)
318 			log(LOG_NOTICE, "%s: offline\n",
319 			    device_get_nameunit(sc->sc_dev));
320 		else if (new_status & LPS_NOPAPER)
321 			log(LOG_NOTICE, "%s: out of paper\n",
322 			    device_get_nameunit(sc->sc_dev));
323 		else if (new_status & LPS_NERR)
324 			log(LOG_NOTICE, "%s: output error\n",
325 			    device_get_nameunit(sc->sc_dev));
326 		break;
327 
328 	case USB_ST_SETUP:
329 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
330 		req.bRequest = UR_GET_PORT_STATUS;
331 		USETW(req.wValue, 0);
332 		req.wIndex[0] = sc->sc_iface_no;
333 		req.wIndex[1] = 0;
334 		USETW(req.wLength, 1);
335 
336 		pc = usbd_xfer_get_frame(xfer, 0);
337 		usbd_copy_in(pc, 0, &req, sizeof(req));
338 
339 		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
340 		usbd_xfer_set_frame_len(xfer, 1, 1);
341 		usbd_xfer_set_frames(xfer, 2);
342 		usbd_transfer_submit(xfer);
343 		break;
344 
345 	default:			/* Error */
346 		DPRINTF("error=%s\n", usbd_errstr(error));
347 		if (error != USB_ERR_CANCELLED) {
348 			/* wait for next watchdog timeout */
349 		}
350 		break;
351 	}
352 }
353 
354 static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
355 	[ULPT_BULK_DT_WR] = {
356 		.type = UE_BULK,
357 		.endpoint = UE_ADDR_ANY,
358 		.direction = UE_DIR_OUT,
359 		.bufsize = ULPT_BSIZE,
360 		.flags = {.pipe_bof = 1,.proxy_buffer = 1},
361 		.callback = &ulpt_write_callback,
362 	},
363 
364 	[ULPT_BULK_DT_RD] = {
365 		.type = UE_BULK,
366 		.endpoint = UE_ADDR_ANY,
367 		.direction = UE_DIR_IN,
368 		.bufsize = ULPT_BSIZE,
369 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
370 		.callback = &ulpt_read_callback,
371 	},
372 
373 	[ULPT_INTR_DT_RD] = {
374 		.type = UE_CONTROL,
375 		.endpoint = 0x00,	/* Control pipe */
376 		.direction = UE_DIR_ANY,
377 		.bufsize = sizeof(struct usb_device_request) + 1,
378 		.callback = &ulpt_status_callback,
379 		.timeout = 1000,	/* 1 second */
380 	},
381 };
382 
383 static void
384 ulpt_start_read(struct usb_fifo *fifo)
385 {
386 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
387 
388 	usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
389 }
390 
391 static void
392 ulpt_stop_read(struct usb_fifo *fifo)
393 {
394 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
395 
396 	usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
397 }
398 
399 static void
400 ulpt_start_write(struct usb_fifo *fifo)
401 {
402 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
403 
404 	usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
405 }
406 
407 static void
408 ulpt_stop_write(struct usb_fifo *fifo)
409 {
410 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
411 
412 	usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
413 }
414 
415 static int
416 ulpt_open(struct usb_fifo *fifo, int fflags)
417 {
418 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
419 
420 	/* we assume that open is a serial process */
421 
422 	if (sc->sc_fflags == 0) {
423 
424 		/* reset USB parallel port */
425 
426 		ulpt_reset(sc);
427 	}
428 	return (unlpt_open(fifo, fflags));
429 }
430 
431 static int
432 unlpt_open(struct usb_fifo *fifo, int fflags)
433 {
434 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
435 
436 	if (sc->sc_fflags & fflags) {
437 		return (EBUSY);
438 	}
439 	if (fflags & FREAD) {
440 		/* clear stall first */
441 		mtx_lock(&sc->sc_mtx);
442 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
443 		mtx_unlock(&sc->sc_mtx);
444 		if (usb_fifo_alloc_buffer(fifo,
445 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
446 		    ULPT_IFQ_MAXLEN)) {
447 			return (ENOMEM);
448 		}
449 		/* set which FIFO is opened */
450 		sc->sc_fifo_open[USB_FIFO_RX] = fifo;
451 	}
452 	if (fflags & FWRITE) {
453 		/* clear stall first */
454 		mtx_lock(&sc->sc_mtx);
455 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
456 		mtx_unlock(&sc->sc_mtx);
457 		if (usb_fifo_alloc_buffer(fifo,
458 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
459 		    ULPT_IFQ_MAXLEN)) {
460 			return (ENOMEM);
461 		}
462 		/* set which FIFO is opened */
463 		sc->sc_fifo_open[USB_FIFO_TX] = fifo;
464 	}
465 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
466 	return (0);
467 }
468 
469 static void
470 ulpt_close(struct usb_fifo *fifo, int fflags)
471 {
472 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
473 
474 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
475 
476 	if (fflags & (FREAD | FWRITE)) {
477 		usb_fifo_free_buffer(fifo);
478 	}
479 }
480 
481 static int
482 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
483     int fflags)
484 {
485 	return (ENODEV);
486 }
487 
488 static const STRUCT_USB_HOST_ID ulpt_devs[] = {
489 	/* Uni-directional USB printer */
490 	{USB_IFACE_CLASS(UICLASS_PRINTER),
491 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
492 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
493 
494 	/* Bi-directional USB printer */
495 	{USB_IFACE_CLASS(UICLASS_PRINTER),
496 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
497 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
498 
499 	/* 1284 USB printer */
500 	{USB_IFACE_CLASS(UICLASS_PRINTER),
501 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
502 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
503 };
504 
505 static int
506 ulpt_probe(device_t dev)
507 {
508 	struct usb_attach_arg *uaa = device_get_ivars(dev);
509 	int error;
510 
511 	DPRINTFN(11, "\n");
512 
513 	if (uaa->usb_mode != USB_MODE_HOST)
514 		return (ENXIO);
515 
516 	error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
517 	if (error)
518 		return (error);
519 
520 	return (BUS_PROBE_GENERIC);
521 }
522 
523 static int
524 ulpt_attach(device_t dev)
525 {
526 	struct usb_attach_arg *uaa = device_get_ivars(dev);
527 	struct ulpt_softc *sc = device_get_softc(dev);
528 	struct usb_interface_descriptor *id;
529 	int unit = device_get_unit(dev);
530 	int error;
531 	uint8_t iface_index = uaa->info.bIfaceIndex;
532 	uint8_t alt_index;
533 
534 	DPRINTFN(11, "sc=%p\n", sc);
535 
536 	sc->sc_dev = dev;
537 	sc->sc_udev = uaa->device;
538 
539 	device_set_usb_desc(dev);
540 
541 	mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
542 
543 	usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
544 
545 	/* search through all the descriptors looking for bidir mode */
546 
547 	id = usbd_get_interface_descriptor(uaa->iface);
548 	alt_index = 0xFF;
549 	while (1) {
550 		if (id == NULL) {
551 			break;
552 		}
553 		if ((id->bDescriptorType == UDESC_INTERFACE) &&
554 		    (id->bLength >= sizeof(*id))) {
555 			if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
556 				break;
557 			} else {
558 				alt_index++;
559 				if ((id->bInterfaceClass == UICLASS_PRINTER) &&
560 				    (id->bInterfaceSubClass == UISUBCLASS_PRINTER) &&
561 				    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
562 					goto found;
563 				}
564 			}
565 		}
566 		id = (void *)usb_desc_foreach(
567 		    usbd_get_config_descriptor(uaa->device), (void *)id);
568 	}
569 	goto detach;
570 
571 found:
572 
573 	DPRINTF("setting alternate "
574 	    "config number: %d\n", alt_index);
575 
576 	if (alt_index) {
577 
578 		error = usbd_set_alt_interface_index
579 		    (uaa->device, iface_index, alt_index);
580 
581 		if (error) {
582 			DPRINTF("could not set alternate "
583 			    "config, error=%s\n", usbd_errstr(error));
584 			goto detach;
585 		}
586 	}
587 	sc->sc_iface_no = id->bInterfaceNumber;
588 
589 	error = usbd_transfer_setup(uaa->device, &iface_index,
590 	    sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
591 	    sc, &sc->sc_mtx);
592 	if (error) {
593 		DPRINTF("error=%s\n", usbd_errstr(error));
594 		goto detach;
595 	}
596 	device_printf(sc->sc_dev, "using bi-directional mode\n");
597 
598 #if 0
599 /*
600  * This code is disabled because for some mysterious reason it causes
601  * printing not to work.  But only sometimes, and mostly with
602  * UHCI and less often with OHCI.  *sigh*
603  */
604 	{
605 		struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
606 		struct usb_device_request req;
607 		int len, alen;
608 
609 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
610 		req.bRequest = UR_GET_DEVICE_ID;
611 		USETW(req.wValue, cd->bConfigurationValue);
612 		USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
613 		USETW(req.wLength, sizeof devinfo - 1);
614 		error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
615 		    &alen, USB_DEFAULT_TIMEOUT);
616 		if (error) {
617 			device_printf(sc->sc_dev, "cannot get device id\n");
618 		} else if (alen <= 2) {
619 			device_printf(sc->sc_dev, "empty device id, no "
620 			    "printer connected?\n");
621 		} else {
622 			/* devinfo now contains an IEEE-1284 device ID */
623 			len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
624 			if (len > sizeof devinfo - 3)
625 				len = sizeof devinfo - 3;
626 			devinfo[len] = 0;
627 			printf("%s: device id <", device_get_nameunit(sc->sc_dev));
628 			ieee1284_print_id(devinfo + 2);
629 			printf(">\n");
630 		}
631 	}
632 #endif
633 
634 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
635 	    &ulpt_fifo_methods, &sc->sc_fifo,
636 	    unit, -1, uaa->info.bIfaceIndex,
637 	    UID_ROOT, GID_OPERATOR, 0644);
638 	if (error) {
639 		goto detach;
640 	}
641 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
642 	    &unlpt_fifo_methods, &sc->sc_fifo_noreset,
643 	    unit, -1, uaa->info.bIfaceIndex,
644 	    UID_ROOT, GID_OPERATOR, 0644);
645 	if (error) {
646 		goto detach;
647 	}
648 	/* start reading of status */
649 
650 	mtx_lock(&sc->sc_mtx);
651 	ulpt_watchdog(sc);
652 	mtx_unlock(&sc->sc_mtx);
653 	return (0);
654 
655 detach:
656 	ulpt_detach(dev);
657 	return (ENOMEM);
658 }
659 
660 static int
661 ulpt_detach(device_t dev)
662 {
663 	struct ulpt_softc *sc = device_get_softc(dev);
664 
665 	DPRINTF("sc=%p\n", sc);
666 
667 	usb_fifo_detach(&sc->sc_fifo);
668 	usb_fifo_detach(&sc->sc_fifo_noreset);
669 
670 	mtx_lock(&sc->sc_mtx);
671 	usb_callout_stop(&sc->sc_watchdog);
672 	mtx_unlock(&sc->sc_mtx);
673 
674 	usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
675 	usb_callout_drain(&sc->sc_watchdog);
676 	mtx_destroy(&sc->sc_mtx);
677 
678 	return (0);
679 }
680 
681 #if 0
682 /* XXX This does not belong here. */
683 
684 /*
685  * Compare two strings until the second ends.
686  */
687 
688 static uint8_t
689 ieee1284_compare(const char *a, const char *b)
690 {
691 	while (1) {
692 
693 		if (*b == 0) {
694 			break;
695 		}
696 		if (*a != *b) {
697 			return 1;
698 		}
699 		b++;
700 		a++;
701 	}
702 	return 0;
703 }
704 
705 /*
706  * Print select parts of an IEEE 1284 device ID.
707  */
708 void
709 ieee1284_print_id(char *str)
710 {
711 	char *p, *q;
712 
713 	for (p = str - 1; p; p = strchr(p, ';')) {
714 		p++;			/* skip ';' */
715 		if (ieee1284_compare(p, "MFG:") == 0 ||
716 		    ieee1284_compare(p, "MANUFACTURER:") == 0 ||
717 		    ieee1284_compare(p, "MDL:") == 0 ||
718 		    ieee1284_compare(p, "MODEL:") == 0) {
719 			q = strchr(p, ';');
720 			if (q)
721 				printf("%.*s", (int)(q - p + 1), p);
722 		}
723 	}
724 }
725 
726 #endif
727 
728 static void
729 ulpt_watchdog(void *arg)
730 {
731 	struct ulpt_softc *sc = arg;
732 
733 	mtx_assert(&sc->sc_mtx, MA_OWNED);
734 
735 	/*
736 	 * Only read status while the device is not opened, due to
737 	 * possible hardware or firmware bug in some printers.
738 	 */
739 	if (sc->sc_fflags == 0)
740 		usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
741 
742 	usb_callout_reset(&sc->sc_watchdog,
743 	    hz, &ulpt_watchdog, sc);
744 }
745 
746 static devclass_t ulpt_devclass;
747 
748 static device_method_t ulpt_methods[] = {
749 	DEVMETHOD(device_probe, ulpt_probe),
750 	DEVMETHOD(device_attach, ulpt_attach),
751 	DEVMETHOD(device_detach, ulpt_detach),
752 	DEVMETHOD_END
753 };
754 
755 static driver_t ulpt_driver = {
756 	.name = "ulpt",
757 	.methods = ulpt_methods,
758 	.size = sizeof(struct ulpt_softc),
759 };
760 
761 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0);
762 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
763 MODULE_VERSION(ulpt, 1);
764 USB_PNP_HOST_INFO(ulpt_devs);
765