xref: /freebsd/sys/dev/usb/serial/ulpt.c (revision 1f474190)
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 | CTLFLAG_MPSAFE, 0,
80     "USB ulpt");
81 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RWTUN,
82     &ulpt_debug, 0, "Debug level");
83 #endif
84 
85 #define	ULPT_BSIZE		(1<<15)	/* bytes */
86 #define	ULPT_IFQ_MAXLEN         2	/* units */
87 
88 #define	UR_GET_DEVICE_ID        0x00
89 #define	UR_GET_PORT_STATUS      0x01
90 #define	UR_SOFT_RESET           0x02
91 
92 #define	LPS_NERR		0x08	/* printer no error */
93 #define	LPS_SELECT		0x10	/* printer selected */
94 #define	LPS_NOPAPER		0x20	/* printer out of paper */
95 #define	LPS_INVERT      (LPS_SELECT|LPS_NERR)
96 #define	LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
97 
98 enum {
99 	ULPT_BULK_DT_WR,
100 	ULPT_BULK_DT_RD,
101 	ULPT_INTR_DT_RD,
102 	ULPT_N_TRANSFER,
103 };
104 
105 struct ulpt_softc {
106 	struct usb_fifo_sc sc_fifo;
107 	struct usb_fifo_sc sc_fifo_noreset;
108 	struct mtx sc_mtx;
109 	struct usb_callout sc_watchdog;
110 
111 	device_t sc_dev;
112 	struct usb_device *sc_udev;
113 	struct usb_fifo *sc_fifo_open[2];
114 	struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
115 
116 	int	sc_fflags;		/* current open flags, FREAD and
117 					 * FWRITE */
118 	uint8_t	sc_iface_no;
119 	uint8_t	sc_last_status;
120 	uint8_t	sc_zlps;		/* number of consequtive zero length
121 					 * packets received */
122 };
123 
124 /* prototypes */
125 
126 static device_probe_t ulpt_probe;
127 static device_attach_t ulpt_attach;
128 static device_detach_t ulpt_detach;
129 
130 static usb_callback_t ulpt_write_callback;
131 static usb_callback_t ulpt_read_callback;
132 static usb_callback_t ulpt_status_callback;
133 
134 static void	ulpt_reset(struct ulpt_softc *);
135 static void	ulpt_watchdog(void *);
136 
137 static usb_fifo_close_t ulpt_close;
138 static usb_fifo_cmd_t ulpt_start_read;
139 static usb_fifo_cmd_t ulpt_start_write;
140 static usb_fifo_cmd_t ulpt_stop_read;
141 static usb_fifo_cmd_t ulpt_stop_write;
142 static usb_fifo_ioctl_t ulpt_ioctl;
143 static usb_fifo_open_t ulpt_open;
144 static usb_fifo_open_t unlpt_open;
145 
146 static struct usb_fifo_methods ulpt_fifo_methods = {
147 	.f_close = &ulpt_close,
148 	.f_ioctl = &ulpt_ioctl,
149 	.f_open = &ulpt_open,
150 	.f_start_read = &ulpt_start_read,
151 	.f_start_write = &ulpt_start_write,
152 	.f_stop_read = &ulpt_stop_read,
153 	.f_stop_write = &ulpt_stop_write,
154 	.basename[0] = "ulpt",
155 };
156 
157 static struct usb_fifo_methods unlpt_fifo_methods = {
158 	.f_close = &ulpt_close,
159 	.f_ioctl = &ulpt_ioctl,
160 	.f_open = &unlpt_open,
161 	.f_start_read = &ulpt_start_read,
162 	.f_start_write = &ulpt_start_write,
163 	.f_stop_read = &ulpt_stop_read,
164 	.f_stop_write = &ulpt_stop_write,
165 	.basename[0] = "unlpt",
166 };
167 
168 static void
169 ulpt_reset(struct ulpt_softc *sc)
170 {
171 	struct usb_device_request req;
172 
173 	DPRINTFN(2, "\n");
174 
175 	req.bRequest = UR_SOFT_RESET;
176 	USETW(req.wValue, 0);
177 	USETW(req.wIndex, sc->sc_iface_no);
178 	USETW(req.wLength, 0);
179 
180 	/*
181 	 * There was a mistake in the USB printer 1.0 spec that gave the
182 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
183 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
184 	 * so we try both.
185 	 */
186 
187 	mtx_lock(&sc->sc_mtx);
188 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
189 	if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
190 	    &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {	/* 1.0 */
191 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
192 		if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
193 		    &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {	/* 1.1 */
194 			/* ignore error */
195 		}
196 	}
197 	mtx_unlock(&sc->sc_mtx);
198 }
199 
200 static void
201 ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
202 {
203 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
204 	struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
205 	struct usb_page_cache *pc;
206 	int actlen, max;
207 
208 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
209 
210 	if (f == NULL) {
211 		/* should not happen */
212 		DPRINTF("no FIFO\n");
213 		return;
214 	}
215 	DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
216 
217 	switch (USB_GET_STATE(xfer)) {
218 	case USB_ST_TRANSFERRED:
219 	case USB_ST_SETUP:
220 tr_setup:
221 		pc = usbd_xfer_get_frame(xfer, 0);
222 		max = usbd_xfer_max_len(xfer);
223 		if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
224 			usbd_xfer_set_frame_len(xfer, 0, actlen);
225 			usbd_transfer_submit(xfer);
226 		}
227 		break;
228 
229 	default:			/* Error */
230 		if (error != USB_ERR_CANCELLED) {
231 			/* try to clear stall first */
232 			usbd_xfer_set_stall(xfer);
233 			goto tr_setup;
234 		}
235 		break;
236 	}
237 }
238 
239 static void
240 ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
241 {
242 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
243 	struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
244 	struct usb_page_cache *pc;
245 	int actlen;
246 
247 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
248 
249 	if (f == NULL) {
250 		/* should not happen */
251 		DPRINTF("no FIFO\n");
252 		return;
253 	}
254 	DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
255 
256 	switch (USB_GET_STATE(xfer)) {
257 	case USB_ST_TRANSFERRED:
258 
259 		if (actlen == 0) {
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 		/* reset USB parallel port */
424 
425 		ulpt_reset(sc);
426 	}
427 	return (unlpt_open(fifo, fflags));
428 }
429 
430 static int
431 unlpt_open(struct usb_fifo *fifo, int fflags)
432 {
433 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
434 
435 	if (sc->sc_fflags & fflags) {
436 		return (EBUSY);
437 	}
438 	if (fflags & FREAD) {
439 		/* clear stall first */
440 		mtx_lock(&sc->sc_mtx);
441 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
442 		mtx_unlock(&sc->sc_mtx);
443 		if (usb_fifo_alloc_buffer(fifo,
444 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
445 		    ULPT_IFQ_MAXLEN)) {
446 			return (ENOMEM);
447 		}
448 		/* set which FIFO is opened */
449 		sc->sc_fifo_open[USB_FIFO_RX] = fifo;
450 	}
451 	if (fflags & FWRITE) {
452 		/* clear stall first */
453 		mtx_lock(&sc->sc_mtx);
454 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
455 		mtx_unlock(&sc->sc_mtx);
456 		if (usb_fifo_alloc_buffer(fifo,
457 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
458 		    ULPT_IFQ_MAXLEN)) {
459 			return (ENOMEM);
460 		}
461 		/* set which FIFO is opened */
462 		sc->sc_fifo_open[USB_FIFO_TX] = fifo;
463 	}
464 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
465 	return (0);
466 }
467 
468 static void
469 ulpt_close(struct usb_fifo *fifo, int fflags)
470 {
471 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
472 
473 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
474 
475 	if (fflags & (FREAD | FWRITE)) {
476 		usb_fifo_free_buffer(fifo);
477 	}
478 }
479 
480 static int
481 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
482     int fflags)
483 {
484 	return (ENODEV);
485 }
486 
487 static const STRUCT_USB_HOST_ID ulpt_devs[] = {
488 	/* Uni-directional USB printer */
489 	{USB_IFACE_CLASS(UICLASS_PRINTER),
490 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
491 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
492 
493 	/* Bi-directional USB printer */
494 	{USB_IFACE_CLASS(UICLASS_PRINTER),
495 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
496 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
497 
498 	/* 1284 USB printer */
499 	{USB_IFACE_CLASS(UICLASS_PRINTER),
500 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
501 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
502 };
503 
504 static int
505 ulpt_probe(device_t dev)
506 {
507 	struct usb_attach_arg *uaa = device_get_ivars(dev);
508 	int error;
509 
510 	DPRINTFN(11, "\n");
511 
512 	if (uaa->usb_mode != USB_MODE_HOST)
513 		return (ENXIO);
514 
515 	error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
516 	if (error)
517 		return (error);
518 
519 	return (BUS_PROBE_GENERIC);
520 }
521 
522 static int
523 ulpt_attach(device_t dev)
524 {
525 	struct usb_attach_arg *uaa = device_get_ivars(dev);
526 	struct ulpt_softc *sc = device_get_softc(dev);
527 	struct usb_interface_descriptor *id;
528 	int unit = device_get_unit(dev);
529 	int error;
530 	uint8_t iface_index = uaa->info.bIfaceIndex;
531 	uint8_t alt_index;
532 
533 	DPRINTFN(11, "sc=%p\n", sc);
534 
535 	sc->sc_dev = dev;
536 	sc->sc_udev = uaa->device;
537 
538 	device_set_usb_desc(dev);
539 
540 	mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
541 
542 	usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
543 
544 	/* search through all the descriptors looking for bidir mode */
545 
546 	id = usbd_get_interface_descriptor(uaa->iface);
547 	alt_index = 0xFF;
548 	while (1) {
549 		if (id == NULL) {
550 			break;
551 		}
552 		if ((id->bDescriptorType == UDESC_INTERFACE) &&
553 		    (id->bLength >= sizeof(*id))) {
554 			if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
555 				break;
556 			} else {
557 				alt_index++;
558 				if ((id->bInterfaceClass == UICLASS_PRINTER) &&
559 				    (id->bInterfaceSubClass == UISUBCLASS_PRINTER) &&
560 				    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
561 					goto found;
562 				}
563 			}
564 		}
565 		id = (void *)usb_desc_foreach(
566 		    usbd_get_config_descriptor(uaa->device), (void *)id);
567 	}
568 	goto detach;
569 
570 found:
571 
572 	DPRINTF("setting alternate "
573 	    "config number: %d\n", alt_index);
574 
575 	if (alt_index) {
576 		error = usbd_set_alt_interface_index
577 		    (uaa->device, iface_index, alt_index);
578 
579 		if (error) {
580 			DPRINTF("could not set alternate "
581 			    "config, error=%s\n", usbd_errstr(error));
582 			goto detach;
583 		}
584 	}
585 	sc->sc_iface_no = id->bInterfaceNumber;
586 
587 	error = usbd_transfer_setup(uaa->device, &iface_index,
588 	    sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
589 	    sc, &sc->sc_mtx);
590 	if (error) {
591 		DPRINTF("error=%s\n", usbd_errstr(error));
592 		goto detach;
593 	}
594 	device_printf(sc->sc_dev, "using bi-directional mode\n");
595 
596 #if 0
597 /*
598  * This code is disabled because for some mysterious reason it causes
599  * printing not to work.  But only sometimes, and mostly with
600  * UHCI and less often with OHCI.  *sigh*
601  */
602 	{
603 		struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
604 		struct usb_device_request req;
605 		int len, alen;
606 
607 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
608 		req.bRequest = UR_GET_DEVICE_ID;
609 		USETW(req.wValue, cd->bConfigurationValue);
610 		USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
611 		USETW(req.wLength, sizeof devinfo - 1);
612 		error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
613 		    &alen, USB_DEFAULT_TIMEOUT);
614 		if (error) {
615 			device_printf(sc->sc_dev, "cannot get device id\n");
616 		} else if (alen <= 2) {
617 			device_printf(sc->sc_dev, "empty device id, no "
618 			    "printer connected?\n");
619 		} else {
620 			/* devinfo now contains an IEEE-1284 device ID */
621 			len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
622 			if (len > sizeof devinfo - 3)
623 				len = sizeof devinfo - 3;
624 			devinfo[len] = 0;
625 			printf("%s: device id <", device_get_nameunit(sc->sc_dev));
626 			ieee1284_print_id(devinfo + 2);
627 			printf(">\n");
628 		}
629 	}
630 #endif
631 
632 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
633 	    &ulpt_fifo_methods, &sc->sc_fifo,
634 	    unit, -1, uaa->info.bIfaceIndex,
635 	    UID_ROOT, GID_OPERATOR, 0644);
636 	if (error) {
637 		goto detach;
638 	}
639 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
640 	    &unlpt_fifo_methods, &sc->sc_fifo_noreset,
641 	    unit, -1, uaa->info.bIfaceIndex,
642 	    UID_ROOT, GID_OPERATOR, 0644);
643 	if (error) {
644 		goto detach;
645 	}
646 	/* start reading of status */
647 
648 	mtx_lock(&sc->sc_mtx);
649 	ulpt_watchdog(sc);
650 	mtx_unlock(&sc->sc_mtx);
651 	return (0);
652 
653 detach:
654 	ulpt_detach(dev);
655 	return (ENOMEM);
656 }
657 
658 static int
659 ulpt_detach(device_t dev)
660 {
661 	struct ulpt_softc *sc = device_get_softc(dev);
662 
663 	DPRINTF("sc=%p\n", sc);
664 
665 	usb_fifo_detach(&sc->sc_fifo);
666 	usb_fifo_detach(&sc->sc_fifo_noreset);
667 
668 	mtx_lock(&sc->sc_mtx);
669 	usb_callout_stop(&sc->sc_watchdog);
670 	mtx_unlock(&sc->sc_mtx);
671 
672 	usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
673 	usb_callout_drain(&sc->sc_watchdog);
674 	mtx_destroy(&sc->sc_mtx);
675 
676 	return (0);
677 }
678 
679 #if 0
680 /* XXX This does not belong here. */
681 
682 /*
683  * Compare two strings until the second ends.
684  */
685 
686 static uint8_t
687 ieee1284_compare(const char *a, const char *b)
688 {
689 	while (1) {
690 		if (*b == 0) {
691 			break;
692 		}
693 		if (*a != *b) {
694 			return 1;
695 		}
696 		b++;
697 		a++;
698 	}
699 	return 0;
700 }
701 
702 /*
703  * Print select parts of an IEEE 1284 device ID.
704  */
705 void
706 ieee1284_print_id(char *str)
707 {
708 	char *p, *q;
709 
710 	for (p = str - 1; p; p = strchr(p, ';')) {
711 		p++;			/* skip ';' */
712 		if (ieee1284_compare(p, "MFG:") == 0 ||
713 		    ieee1284_compare(p, "MANUFACTURER:") == 0 ||
714 		    ieee1284_compare(p, "MDL:") == 0 ||
715 		    ieee1284_compare(p, "MODEL:") == 0) {
716 			q = strchr(p, ';');
717 			if (q)
718 				printf("%.*s", (int)(q - p + 1), p);
719 		}
720 	}
721 }
722 
723 #endif
724 
725 static void
726 ulpt_watchdog(void *arg)
727 {
728 	struct ulpt_softc *sc = arg;
729 
730 	mtx_assert(&sc->sc_mtx, MA_OWNED);
731 
732 	/*
733 	 * Only read status while the device is not opened, due to
734 	 * possible hardware or firmware bug in some printers.
735 	 */
736 	if (sc->sc_fflags == 0)
737 		usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
738 
739 	usb_callout_reset(&sc->sc_watchdog,
740 	    hz, &ulpt_watchdog, sc);
741 }
742 
743 static devclass_t ulpt_devclass;
744 
745 static device_method_t ulpt_methods[] = {
746 	DEVMETHOD(device_probe, ulpt_probe),
747 	DEVMETHOD(device_attach, ulpt_attach),
748 	DEVMETHOD(device_detach, ulpt_detach),
749 	DEVMETHOD_END
750 };
751 
752 static driver_t ulpt_driver = {
753 	.name = "ulpt",
754 	.methods = ulpt_methods,
755 	.size = sizeof(struct ulpt_softc),
756 };
757 
758 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0);
759 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
760 MODULE_VERSION(ulpt, 1);
761 USB_PNP_HOST_INFO(ulpt_devs);
762