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