xref: /freebsd/sys/dev/usb/serial/ulpt.c (revision 58d84ef87094691bf1ad9608964ec85f120cd34a)
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 	/* Epson printer */
504 	{USB_VENDOR(USB_VENDOR_EPSON),
505 	 USB_PRODUCT(USB_PRODUCT_EPSON_TMU220B),
506 	 USB_IFACE_CLASS(UICLASS_VENDOR),
507 	 USB_IFACE_SUBCLASS(UISUBCLASS_VENDOR),
508 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
509 };
510 
511 static int
512 ulpt_probe(device_t dev)
513 {
514 	struct usb_attach_arg *uaa = device_get_ivars(dev);
515 	int error;
516 
517 	DPRINTFN(11, "\n");
518 
519 	if (uaa->usb_mode != USB_MODE_HOST)
520 		return (ENXIO);
521 
522 	error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
523 	if (error)
524 		return (error);
525 
526 	return (BUS_PROBE_GENERIC);
527 }
528 
529 static int
530 ulpt_attach(device_t dev)
531 {
532 	struct usb_attach_arg *uaa = device_get_ivars(dev);
533 	struct ulpt_softc *sc = device_get_softc(dev);
534 	struct usb_interface_descriptor *id;
535 	int unit = device_get_unit(dev);
536 	int error;
537 	uint8_t iface_index = uaa->info.bIfaceIndex;
538 	uint8_t alt_index;
539 
540 	DPRINTFN(11, "sc=%p\n", sc);
541 
542 	sc->sc_dev = dev;
543 	sc->sc_udev = uaa->device;
544 
545 	device_set_usb_desc(dev);
546 
547 	mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
548 
549 	usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
550 
551 	/* search through all the descriptors looking for bidir mode */
552 
553 	id = usbd_get_interface_descriptor(uaa->iface);
554 	alt_index = 0xFF;
555 	while (1) {
556 		if (id == NULL) {
557 			break;
558 		}
559 		if ((id->bDescriptorType == UDESC_INTERFACE) &&
560 		    (id->bLength >= sizeof(*id))) {
561 			if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
562 				break;
563 			} else {
564 				alt_index++;
565 				if ((id->bInterfaceClass == UICLASS_PRINTER ||
566 				     id->bInterfaceClass == UICLASS_VENDOR) &&
567 				    (id->bInterfaceSubClass == UISUBCLASS_PRINTER ||
568 				     id->bInterfaceSubClass == UISUBCLASS_VENDOR) &&
569 				    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
570 					goto found;
571 				}
572 			}
573 		}
574 		id = (void *)usb_desc_foreach(
575 		    usbd_get_config_descriptor(uaa->device), (void *)id);
576 	}
577 	goto detach;
578 
579 found:
580 
581 	DPRINTF("setting alternate "
582 	    "config number: %d\n", alt_index);
583 
584 	if (alt_index) {
585 		error = usbd_set_alt_interface_index
586 		    (uaa->device, iface_index, alt_index);
587 
588 		if (error) {
589 			DPRINTF("could not set alternate "
590 			    "config, error=%s\n", usbd_errstr(error));
591 			goto detach;
592 		}
593 	}
594 	sc->sc_iface_no = id->bInterfaceNumber;
595 
596 	error = usbd_transfer_setup(uaa->device, &iface_index,
597 	    sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
598 	    sc, &sc->sc_mtx);
599 	if (error) {
600 		DPRINTF("error=%s\n", usbd_errstr(error));
601 		goto detach;
602 	}
603 	device_printf(sc->sc_dev, "using bi-directional mode\n");
604 
605 #if 0
606 /*
607  * This code is disabled because for some mysterious reason it causes
608  * printing not to work.  But only sometimes, and mostly with
609  * UHCI and less often with OHCI.  *sigh*
610  */
611 	{
612 		struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
613 		struct usb_device_request req;
614 		int len, alen;
615 
616 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
617 		req.bRequest = UR_GET_DEVICE_ID;
618 		USETW(req.wValue, cd->bConfigurationValue);
619 		USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
620 		USETW(req.wLength, sizeof devinfo - 1);
621 		error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
622 		    &alen, USB_DEFAULT_TIMEOUT);
623 		if (error) {
624 			device_printf(sc->sc_dev, "cannot get device id\n");
625 		} else if (alen <= 2) {
626 			device_printf(sc->sc_dev, "empty device id, no "
627 			    "printer connected?\n");
628 		} else {
629 			/* devinfo now contains an IEEE-1284 device ID */
630 			len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
631 			if (len > sizeof devinfo - 3)
632 				len = sizeof devinfo - 3;
633 			devinfo[len] = 0;
634 			printf("%s: device id <", device_get_nameunit(sc->sc_dev));
635 			ieee1284_print_id(devinfo + 2);
636 			printf(">\n");
637 		}
638 	}
639 #endif
640 
641 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
642 	    &ulpt_fifo_methods, &sc->sc_fifo,
643 	    unit, -1, uaa->info.bIfaceIndex,
644 	    UID_ROOT, GID_OPERATOR, 0644);
645 	if (error) {
646 		goto detach;
647 	}
648 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
649 	    &unlpt_fifo_methods, &sc->sc_fifo_noreset,
650 	    unit, -1, uaa->info.bIfaceIndex,
651 	    UID_ROOT, GID_OPERATOR, 0644);
652 	if (error) {
653 		goto detach;
654 	}
655 	/* start reading of status */
656 
657 	mtx_lock(&sc->sc_mtx);
658 	ulpt_watchdog(sc);
659 	mtx_unlock(&sc->sc_mtx);
660 	return (0);
661 
662 detach:
663 	ulpt_detach(dev);
664 	return (ENOMEM);
665 }
666 
667 static int
668 ulpt_detach(device_t dev)
669 {
670 	struct ulpt_softc *sc = device_get_softc(dev);
671 
672 	DPRINTF("sc=%p\n", sc);
673 
674 	usb_fifo_detach(&sc->sc_fifo);
675 	usb_fifo_detach(&sc->sc_fifo_noreset);
676 
677 	mtx_lock(&sc->sc_mtx);
678 	usb_callout_stop(&sc->sc_watchdog);
679 	mtx_unlock(&sc->sc_mtx);
680 
681 	usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
682 	usb_callout_drain(&sc->sc_watchdog);
683 	mtx_destroy(&sc->sc_mtx);
684 
685 	return (0);
686 }
687 
688 #if 0
689 /* XXX This does not belong here. */
690 
691 /*
692  * Compare two strings until the second ends.
693  */
694 
695 static uint8_t
696 ieee1284_compare(const char *a, const char *b)
697 {
698 	while (1) {
699 		if (*b == 0) {
700 			break;
701 		}
702 		if (*a != *b) {
703 			return 1;
704 		}
705 		b++;
706 		a++;
707 	}
708 	return 0;
709 }
710 
711 /*
712  * Print select parts of an IEEE 1284 device ID.
713  */
714 void
715 ieee1284_print_id(char *str)
716 {
717 	char *p, *q;
718 
719 	for (p = str - 1; p; p = strchr(p, ';')) {
720 		p++;			/* skip ';' */
721 		if (ieee1284_compare(p, "MFG:") == 0 ||
722 		    ieee1284_compare(p, "MANUFACTURER:") == 0 ||
723 		    ieee1284_compare(p, "MDL:") == 0 ||
724 		    ieee1284_compare(p, "MODEL:") == 0) {
725 			q = strchr(p, ';');
726 			if (q)
727 				printf("%.*s", (int)(q - p + 1), p);
728 		}
729 	}
730 }
731 
732 #endif
733 
734 static void
735 ulpt_watchdog(void *arg)
736 {
737 	struct ulpt_softc *sc = arg;
738 
739 	mtx_assert(&sc->sc_mtx, MA_OWNED);
740 
741 	/*
742 	 * Only read status while the device is not opened, due to
743 	 * possible hardware or firmware bug in some printers.
744 	 */
745 	if (sc->sc_fflags == 0)
746 		usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
747 
748 	usb_callout_reset(&sc->sc_watchdog,
749 	    hz, &ulpt_watchdog, sc);
750 }
751 
752 static device_method_t ulpt_methods[] = {
753 	DEVMETHOD(device_probe, ulpt_probe),
754 	DEVMETHOD(device_attach, ulpt_attach),
755 	DEVMETHOD(device_detach, ulpt_detach),
756 	DEVMETHOD_END
757 };
758 
759 static driver_t ulpt_driver = {
760 	.name = "ulpt",
761 	.methods = ulpt_methods,
762 	.size = sizeof(struct ulpt_softc),
763 };
764 
765 DRIVER_MODULE(ulpt, uhub, ulpt_driver, NULL, NULL);
766 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
767 MODULE_VERSION(ulpt, 1);
768 USB_PNP_HOST_INFO(ulpt_devs);
769