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