xref: /freebsd/sys/dev/usb/serial/ulpt.c (revision c697fb7f)
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 
261 			if (sc->sc_zlps == 4) {
262 				/* enable BULK throttle */
263 				usbd_xfer_set_interval(xfer, 500); /* ms */
264 			} else {
265 				sc->sc_zlps++;
266 			}
267 		} else {
268 			/* disable BULK throttle */
269 
270 			usbd_xfer_set_interval(xfer, 0);
271 			sc->sc_zlps = 0;
272 		}
273 
274 		pc = usbd_xfer_get_frame(xfer, 0);
275 		usb_fifo_put_data(f, pc, 0, actlen, 1);
276 
277 	case USB_ST_SETUP:
278 tr_setup:
279 		if (usb_fifo_put_bytes_max(f) != 0) {
280 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
281 			usbd_transfer_submit(xfer);
282 		}
283 		break;
284 
285 	default:			/* Error */
286 		/* disable BULK throttle */
287 		usbd_xfer_set_interval(xfer, 0);
288 		sc->sc_zlps = 0;
289 
290 		if (error != USB_ERR_CANCELLED) {
291 			/* try to clear stall first */
292 			usbd_xfer_set_stall(xfer);
293 			goto tr_setup;
294 		}
295 		break;
296 	}
297 }
298 
299 static void
300 ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
301 {
302 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
303 	struct usb_device_request req;
304 	struct usb_page_cache *pc;
305 	uint8_t cur_status;
306 	uint8_t new_status;
307 
308 	switch (USB_GET_STATE(xfer)) {
309 	case USB_ST_TRANSFERRED:
310 
311 		pc = usbd_xfer_get_frame(xfer, 1);
312 		usbd_copy_out(pc, 0, &cur_status, 1);
313 
314 		cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
315 		new_status = cur_status & ~sc->sc_last_status;
316 		sc->sc_last_status = cur_status;
317 
318 		if (new_status & LPS_SELECT)
319 			log(LOG_NOTICE, "%s: offline\n",
320 			    device_get_nameunit(sc->sc_dev));
321 		else if (new_status & LPS_NOPAPER)
322 			log(LOG_NOTICE, "%s: out of paper\n",
323 			    device_get_nameunit(sc->sc_dev));
324 		else if (new_status & LPS_NERR)
325 			log(LOG_NOTICE, "%s: output error\n",
326 			    device_get_nameunit(sc->sc_dev));
327 		break;
328 
329 	case USB_ST_SETUP:
330 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
331 		req.bRequest = UR_GET_PORT_STATUS;
332 		USETW(req.wValue, 0);
333 		req.wIndex[0] = sc->sc_iface_no;
334 		req.wIndex[1] = 0;
335 		USETW(req.wLength, 1);
336 
337 		pc = usbd_xfer_get_frame(xfer, 0);
338 		usbd_copy_in(pc, 0, &req, sizeof(req));
339 
340 		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
341 		usbd_xfer_set_frame_len(xfer, 1, 1);
342 		usbd_xfer_set_frames(xfer, 2);
343 		usbd_transfer_submit(xfer);
344 		break;
345 
346 	default:			/* Error */
347 		DPRINTF("error=%s\n", usbd_errstr(error));
348 		if (error != USB_ERR_CANCELLED) {
349 			/* wait for next watchdog timeout */
350 		}
351 		break;
352 	}
353 }
354 
355 static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
356 	[ULPT_BULK_DT_WR] = {
357 		.type = UE_BULK,
358 		.endpoint = UE_ADDR_ANY,
359 		.direction = UE_DIR_OUT,
360 		.bufsize = ULPT_BSIZE,
361 		.flags = {.pipe_bof = 1,.proxy_buffer = 1},
362 		.callback = &ulpt_write_callback,
363 	},
364 
365 	[ULPT_BULK_DT_RD] = {
366 		.type = UE_BULK,
367 		.endpoint = UE_ADDR_ANY,
368 		.direction = UE_DIR_IN,
369 		.bufsize = ULPT_BSIZE,
370 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
371 		.callback = &ulpt_read_callback,
372 	},
373 
374 	[ULPT_INTR_DT_RD] = {
375 		.type = UE_CONTROL,
376 		.endpoint = 0x00,	/* Control pipe */
377 		.direction = UE_DIR_ANY,
378 		.bufsize = sizeof(struct usb_device_request) + 1,
379 		.callback = &ulpt_status_callback,
380 		.timeout = 1000,	/* 1 second */
381 	},
382 };
383 
384 static void
385 ulpt_start_read(struct usb_fifo *fifo)
386 {
387 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
388 
389 	usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
390 }
391 
392 static void
393 ulpt_stop_read(struct usb_fifo *fifo)
394 {
395 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
396 
397 	usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
398 }
399 
400 static void
401 ulpt_start_write(struct usb_fifo *fifo)
402 {
403 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
404 
405 	usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
406 }
407 
408 static void
409 ulpt_stop_write(struct usb_fifo *fifo)
410 {
411 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
412 
413 	usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
414 }
415 
416 static int
417 ulpt_open(struct usb_fifo *fifo, int fflags)
418 {
419 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
420 
421 	/* we assume that open is a serial process */
422 
423 	if (sc->sc_fflags == 0) {
424 
425 		/* reset USB parallel port */
426 
427 		ulpt_reset(sc);
428 	}
429 	return (unlpt_open(fifo, fflags));
430 }
431 
432 static int
433 unlpt_open(struct usb_fifo *fifo, int fflags)
434 {
435 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
436 
437 	if (sc->sc_fflags & fflags) {
438 		return (EBUSY);
439 	}
440 	if (fflags & FREAD) {
441 		/* clear stall first */
442 		mtx_lock(&sc->sc_mtx);
443 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
444 		mtx_unlock(&sc->sc_mtx);
445 		if (usb_fifo_alloc_buffer(fifo,
446 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
447 		    ULPT_IFQ_MAXLEN)) {
448 			return (ENOMEM);
449 		}
450 		/* set which FIFO is opened */
451 		sc->sc_fifo_open[USB_FIFO_RX] = fifo;
452 	}
453 	if (fflags & FWRITE) {
454 		/* clear stall first */
455 		mtx_lock(&sc->sc_mtx);
456 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
457 		mtx_unlock(&sc->sc_mtx);
458 		if (usb_fifo_alloc_buffer(fifo,
459 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
460 		    ULPT_IFQ_MAXLEN)) {
461 			return (ENOMEM);
462 		}
463 		/* set which FIFO is opened */
464 		sc->sc_fifo_open[USB_FIFO_TX] = fifo;
465 	}
466 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
467 	return (0);
468 }
469 
470 static void
471 ulpt_close(struct usb_fifo *fifo, int fflags)
472 {
473 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
474 
475 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
476 
477 	if (fflags & (FREAD | FWRITE)) {
478 		usb_fifo_free_buffer(fifo);
479 	}
480 }
481 
482 static int
483 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
484     int fflags)
485 {
486 	return (ENODEV);
487 }
488 
489 static const STRUCT_USB_HOST_ID ulpt_devs[] = {
490 	/* Uni-directional USB printer */
491 	{USB_IFACE_CLASS(UICLASS_PRINTER),
492 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
493 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
494 
495 	/* Bi-directional USB printer */
496 	{USB_IFACE_CLASS(UICLASS_PRINTER),
497 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
498 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
499 
500 	/* 1284 USB printer */
501 	{USB_IFACE_CLASS(UICLASS_PRINTER),
502 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
503 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
504 };
505 
506 static int
507 ulpt_probe(device_t dev)
508 {
509 	struct usb_attach_arg *uaa = device_get_ivars(dev);
510 	int error;
511 
512 	DPRINTFN(11, "\n");
513 
514 	if (uaa->usb_mode != USB_MODE_HOST)
515 		return (ENXIO);
516 
517 	error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
518 	if (error)
519 		return (error);
520 
521 	return (BUS_PROBE_GENERIC);
522 }
523 
524 static int
525 ulpt_attach(device_t dev)
526 {
527 	struct usb_attach_arg *uaa = device_get_ivars(dev);
528 	struct ulpt_softc *sc = device_get_softc(dev);
529 	struct usb_interface_descriptor *id;
530 	int unit = device_get_unit(dev);
531 	int error;
532 	uint8_t iface_index = uaa->info.bIfaceIndex;
533 	uint8_t alt_index;
534 
535 	DPRINTFN(11, "sc=%p\n", sc);
536 
537 	sc->sc_dev = dev;
538 	sc->sc_udev = uaa->device;
539 
540 	device_set_usb_desc(dev);
541 
542 	mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
543 
544 	usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
545 
546 	/* search through all the descriptors looking for bidir mode */
547 
548 	id = usbd_get_interface_descriptor(uaa->iface);
549 	alt_index = 0xFF;
550 	while (1) {
551 		if (id == NULL) {
552 			break;
553 		}
554 		if ((id->bDescriptorType == UDESC_INTERFACE) &&
555 		    (id->bLength >= sizeof(*id))) {
556 			if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
557 				break;
558 			} else {
559 				alt_index++;
560 				if ((id->bInterfaceClass == UICLASS_PRINTER) &&
561 				    (id->bInterfaceSubClass == UISUBCLASS_PRINTER) &&
562 				    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
563 					goto found;
564 				}
565 			}
566 		}
567 		id = (void *)usb_desc_foreach(
568 		    usbd_get_config_descriptor(uaa->device), (void *)id);
569 	}
570 	goto detach;
571 
572 found:
573 
574 	DPRINTF("setting alternate "
575 	    "config number: %d\n", alt_index);
576 
577 	if (alt_index) {
578 
579 		error = usbd_set_alt_interface_index
580 		    (uaa->device, iface_index, alt_index);
581 
582 		if (error) {
583 			DPRINTF("could not set alternate "
584 			    "config, error=%s\n", usbd_errstr(error));
585 			goto detach;
586 		}
587 	}
588 	sc->sc_iface_no = id->bInterfaceNumber;
589 
590 	error = usbd_transfer_setup(uaa->device, &iface_index,
591 	    sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
592 	    sc, &sc->sc_mtx);
593 	if (error) {
594 		DPRINTF("error=%s\n", usbd_errstr(error));
595 		goto detach;
596 	}
597 	device_printf(sc->sc_dev, "using bi-directional mode\n");
598 
599 #if 0
600 /*
601  * This code is disabled because for some mysterious reason it causes
602  * printing not to work.  But only sometimes, and mostly with
603  * UHCI and less often with OHCI.  *sigh*
604  */
605 	{
606 		struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
607 		struct usb_device_request req;
608 		int len, alen;
609 
610 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
611 		req.bRequest = UR_GET_DEVICE_ID;
612 		USETW(req.wValue, cd->bConfigurationValue);
613 		USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
614 		USETW(req.wLength, sizeof devinfo - 1);
615 		error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
616 		    &alen, USB_DEFAULT_TIMEOUT);
617 		if (error) {
618 			device_printf(sc->sc_dev, "cannot get device id\n");
619 		} else if (alen <= 2) {
620 			device_printf(sc->sc_dev, "empty device id, no "
621 			    "printer connected?\n");
622 		} else {
623 			/* devinfo now contains an IEEE-1284 device ID */
624 			len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
625 			if (len > sizeof devinfo - 3)
626 				len = sizeof devinfo - 3;
627 			devinfo[len] = 0;
628 			printf("%s: device id <", device_get_nameunit(sc->sc_dev));
629 			ieee1284_print_id(devinfo + 2);
630 			printf(">\n");
631 		}
632 	}
633 #endif
634 
635 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
636 	    &ulpt_fifo_methods, &sc->sc_fifo,
637 	    unit, -1, uaa->info.bIfaceIndex,
638 	    UID_ROOT, GID_OPERATOR, 0644);
639 	if (error) {
640 		goto detach;
641 	}
642 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
643 	    &unlpt_fifo_methods, &sc->sc_fifo_noreset,
644 	    unit, -1, uaa->info.bIfaceIndex,
645 	    UID_ROOT, GID_OPERATOR, 0644);
646 	if (error) {
647 		goto detach;
648 	}
649 	/* start reading of status */
650 
651 	mtx_lock(&sc->sc_mtx);
652 	ulpt_watchdog(sc);
653 	mtx_unlock(&sc->sc_mtx);
654 	return (0);
655 
656 detach:
657 	ulpt_detach(dev);
658 	return (ENOMEM);
659 }
660 
661 static int
662 ulpt_detach(device_t dev)
663 {
664 	struct ulpt_softc *sc = device_get_softc(dev);
665 
666 	DPRINTF("sc=%p\n", sc);
667 
668 	usb_fifo_detach(&sc->sc_fifo);
669 	usb_fifo_detach(&sc->sc_fifo_noreset);
670 
671 	mtx_lock(&sc->sc_mtx);
672 	usb_callout_stop(&sc->sc_watchdog);
673 	mtx_unlock(&sc->sc_mtx);
674 
675 	usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
676 	usb_callout_drain(&sc->sc_watchdog);
677 	mtx_destroy(&sc->sc_mtx);
678 
679 	return (0);
680 }
681 
682 #if 0
683 /* XXX This does not belong here. */
684 
685 /*
686  * Compare two strings until the second ends.
687  */
688 
689 static uint8_t
690 ieee1284_compare(const char *a, const char *b)
691 {
692 	while (1) {
693 
694 		if (*b == 0) {
695 			break;
696 		}
697 		if (*a != *b) {
698 			return 1;
699 		}
700 		b++;
701 		a++;
702 	}
703 	return 0;
704 }
705 
706 /*
707  * Print select parts of an IEEE 1284 device ID.
708  */
709 void
710 ieee1284_print_id(char *str)
711 {
712 	char *p, *q;
713 
714 	for (p = str - 1; p; p = strchr(p, ';')) {
715 		p++;			/* skip ';' */
716 		if (ieee1284_compare(p, "MFG:") == 0 ||
717 		    ieee1284_compare(p, "MANUFACTURER:") == 0 ||
718 		    ieee1284_compare(p, "MDL:") == 0 ||
719 		    ieee1284_compare(p, "MODEL:") == 0) {
720 			q = strchr(p, ';');
721 			if (q)
722 				printf("%.*s", (int)(q - p + 1), p);
723 		}
724 	}
725 }
726 
727 #endif
728 
729 static void
730 ulpt_watchdog(void *arg)
731 {
732 	struct ulpt_softc *sc = arg;
733 
734 	mtx_assert(&sc->sc_mtx, MA_OWNED);
735 
736 	/*
737 	 * Only read status while the device is not opened, due to
738 	 * possible hardware or firmware bug in some printers.
739 	 */
740 	if (sc->sc_fflags == 0)
741 		usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
742 
743 	usb_callout_reset(&sc->sc_watchdog,
744 	    hz, &ulpt_watchdog, sc);
745 }
746 
747 static devclass_t ulpt_devclass;
748 
749 static device_method_t ulpt_methods[] = {
750 	DEVMETHOD(device_probe, ulpt_probe),
751 	DEVMETHOD(device_attach, ulpt_attach),
752 	DEVMETHOD(device_detach, ulpt_detach),
753 	DEVMETHOD_END
754 };
755 
756 static driver_t ulpt_driver = {
757 	.name = "ulpt",
758 	.methods = ulpt_methods,
759 	.size = sizeof(struct ulpt_softc),
760 };
761 
762 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0);
763 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
764 MODULE_VERSION(ulpt, 1);
765 USB_PNP_HOST_INFO(ulpt_devs);
766