xref: /freebsd/sys/dev/usb/serial/ulpt.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
102ac6454SAndrew Thompson #include <sys/cdefs.h>
202ac6454SAndrew Thompson /*	$NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $	*/
302ac6454SAndrew Thompson 
402ac6454SAndrew Thompson /*-
5*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
6718cf2ccSPedro F. Giffuni  *
702ac6454SAndrew Thompson  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
802ac6454SAndrew Thompson  * All rights reserved.
902ac6454SAndrew Thompson  *
1002ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
1102ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
1202ac6454SAndrew Thompson  * Carlstedt Research & Technology.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1502ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1602ac6454SAndrew Thompson  * are met:
1702ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1802ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1902ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
2002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
2102ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
2202ac6454SAndrew Thompson  *
2302ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2402ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2502ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2602ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2702ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2802ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2902ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3002ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3102ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3202ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3302ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3402ac6454SAndrew Thompson  */
3502ac6454SAndrew Thompson 
3602ac6454SAndrew Thompson /*
3702ac6454SAndrew Thompson  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
3802ac6454SAndrew Thompson  * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
3902ac6454SAndrew Thompson  */
4002ac6454SAndrew Thompson 
41ed6d949aSAndrew Thompson #include <sys/stdint.h>
42ed6d949aSAndrew Thompson #include <sys/stddef.h>
43ed6d949aSAndrew Thompson #include <sys/param.h>
44ed6d949aSAndrew Thompson #include <sys/queue.h>
45ed6d949aSAndrew Thompson #include <sys/types.h>
46ed6d949aSAndrew Thompson #include <sys/systm.h>
47ed6d949aSAndrew Thompson #include <sys/kernel.h>
48ed6d949aSAndrew Thompson #include <sys/bus.h>
49ed6d949aSAndrew Thompson #include <sys/module.h>
50ed6d949aSAndrew Thompson #include <sys/lock.h>
51ed6d949aSAndrew Thompson #include <sys/mutex.h>
52ed6d949aSAndrew Thompson #include <sys/condvar.h>
53ed6d949aSAndrew Thompson #include <sys/sysctl.h>
54ed6d949aSAndrew Thompson #include <sys/sx.h>
55ed6d949aSAndrew Thompson #include <sys/unistd.h>
56ed6d949aSAndrew Thompson #include <sys/callout.h>
57ed6d949aSAndrew Thompson #include <sys/malloc.h>
58ed6d949aSAndrew Thompson #include <sys/priv.h>
59ed6d949aSAndrew Thompson #include <sys/syslog.h>
60ed6d949aSAndrew Thompson #include <sys/selinfo.h>
61ed6d949aSAndrew Thompson #include <sys/conf.h>
62ed6d949aSAndrew Thompson #include <sys/fcntl.h>
63ed6d949aSAndrew Thompson 
6402ac6454SAndrew Thompson #include <dev/usb/usb.h>
65ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
66ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
67ed6d949aSAndrew Thompson #include <dev/usb/usbhid.h>
68ed6d949aSAndrew Thompson #include "usbdevs.h"
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson #define	USB_DEBUG_VAR ulpt_debug
7102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7202ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7302ac6454SAndrew Thompson 
74b850ecc1SAndrew Thompson #ifdef USB_DEBUG
7502ac6454SAndrew Thompson static int ulpt_debug = 0;
7602ac6454SAndrew Thompson 
77f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
78f8d2b1f3SPawel Biernacki     "USB ulpt");
79ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RWTUN,
8002ac6454SAndrew Thompson     &ulpt_debug, 0, "Debug level");
8102ac6454SAndrew Thompson #endif
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson #define	ULPT_BSIZE		(1<<15)	/* bytes */
8402ac6454SAndrew Thompson #define	ULPT_IFQ_MAXLEN         2	/* units */
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson #define	UR_GET_DEVICE_ID        0x00
8702ac6454SAndrew Thompson #define	UR_GET_PORT_STATUS      0x01
8802ac6454SAndrew Thompson #define	UR_SOFT_RESET           0x02
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson #define	LPS_NERR		0x08	/* printer no error */
9102ac6454SAndrew Thompson #define	LPS_SELECT		0x10	/* printer selected */
9202ac6454SAndrew Thompson #define	LPS_NOPAPER		0x20	/* printer out of paper */
9302ac6454SAndrew Thompson #define	LPS_INVERT      (LPS_SELECT|LPS_NERR)
9402ac6454SAndrew Thompson #define	LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson enum {
9702ac6454SAndrew Thompson 	ULPT_BULK_DT_WR,
9802ac6454SAndrew Thompson 	ULPT_BULK_DT_RD,
9902ac6454SAndrew Thompson 	ULPT_INTR_DT_RD,
10002ac6454SAndrew Thompson 	ULPT_N_TRANSFER,
10102ac6454SAndrew Thompson };
10202ac6454SAndrew Thompson 
10302ac6454SAndrew Thompson struct ulpt_softc {
104760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo;
105760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo_noreset;
10602ac6454SAndrew Thompson 	struct mtx sc_mtx;
107760bc48eSAndrew Thompson 	struct usb_callout sc_watchdog;
10802ac6454SAndrew Thompson 
10902ac6454SAndrew Thompson 	device_t sc_dev;
110760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
111760bc48eSAndrew Thompson 	struct usb_fifo *sc_fifo_open[2];
112760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
11302ac6454SAndrew Thompson 
11402ac6454SAndrew Thompson 	int	sc_fflags;		/* current open flags, FREAD and
11502ac6454SAndrew Thompson 					 * FWRITE */
11602ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
11702ac6454SAndrew Thompson 	uint8_t	sc_last_status;
11802ac6454SAndrew Thompson 	uint8_t	sc_zlps;		/* number of consequtive zero length
11902ac6454SAndrew Thompson 					 * packets received */
12002ac6454SAndrew Thompson };
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson /* prototypes */
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson static device_probe_t ulpt_probe;
12502ac6454SAndrew Thompson static device_attach_t ulpt_attach;
12602ac6454SAndrew Thompson static device_detach_t ulpt_detach;
12702ac6454SAndrew Thompson 
128e0a69b51SAndrew Thompson static usb_callback_t ulpt_write_callback;
129e0a69b51SAndrew Thompson static usb_callback_t ulpt_read_callback;
130e0a69b51SAndrew Thompson static usb_callback_t ulpt_status_callback;
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson static void	ulpt_reset(struct ulpt_softc *);
13302ac6454SAndrew Thompson static void	ulpt_watchdog(void *);
13402ac6454SAndrew Thompson 
135e0a69b51SAndrew Thompson static usb_fifo_close_t ulpt_close;
136e0a69b51SAndrew Thompson static usb_fifo_cmd_t ulpt_start_read;
137e0a69b51SAndrew Thompson static usb_fifo_cmd_t ulpt_start_write;
138e0a69b51SAndrew Thompson static usb_fifo_cmd_t ulpt_stop_read;
139e0a69b51SAndrew Thompson static usb_fifo_cmd_t ulpt_stop_write;
140e0a69b51SAndrew Thompson static usb_fifo_ioctl_t ulpt_ioctl;
141e0a69b51SAndrew Thompson static usb_fifo_open_t ulpt_open;
142e0a69b51SAndrew Thompson static usb_fifo_open_t unlpt_open;
14302ac6454SAndrew Thompson 
144760bc48eSAndrew Thompson static struct usb_fifo_methods ulpt_fifo_methods = {
14502ac6454SAndrew Thompson 	.f_close = &ulpt_close,
14602ac6454SAndrew Thompson 	.f_ioctl = &ulpt_ioctl,
14702ac6454SAndrew Thompson 	.f_open = &ulpt_open,
14802ac6454SAndrew Thompson 	.f_start_read = &ulpt_start_read,
14902ac6454SAndrew Thompson 	.f_start_write = &ulpt_start_write,
15002ac6454SAndrew Thompson 	.f_stop_read = &ulpt_stop_read,
15102ac6454SAndrew Thompson 	.f_stop_write = &ulpt_stop_write,
15202ac6454SAndrew Thompson 	.basename[0] = "ulpt",
15302ac6454SAndrew Thompson };
15402ac6454SAndrew Thompson 
155760bc48eSAndrew Thompson static struct usb_fifo_methods unlpt_fifo_methods = {
15602ac6454SAndrew Thompson 	.f_close = &ulpt_close,
15702ac6454SAndrew Thompson 	.f_ioctl = &ulpt_ioctl,
15802ac6454SAndrew Thompson 	.f_open = &unlpt_open,
15902ac6454SAndrew Thompson 	.f_start_read = &ulpt_start_read,
16002ac6454SAndrew Thompson 	.f_start_write = &ulpt_start_write,
16102ac6454SAndrew Thompson 	.f_stop_read = &ulpt_stop_read,
16202ac6454SAndrew Thompson 	.f_stop_write = &ulpt_stop_write,
16302ac6454SAndrew Thompson 	.basename[0] = "unlpt",
16402ac6454SAndrew Thompson };
16502ac6454SAndrew Thompson 
16602ac6454SAndrew Thompson static void
ulpt_reset(struct ulpt_softc * sc)16702ac6454SAndrew Thompson ulpt_reset(struct ulpt_softc *sc)
16802ac6454SAndrew Thompson {
169760bc48eSAndrew Thompson 	struct usb_device_request req;
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 	req.bRequest = UR_SOFT_RESET;
17402ac6454SAndrew Thompson 	USETW(req.wValue, 0);
17502ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_iface_no);
17602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
17702ac6454SAndrew Thompson 
17802ac6454SAndrew Thompson 	/*
17902ac6454SAndrew Thompson 	 * There was a mistake in the USB printer 1.0 spec that gave the
18002ac6454SAndrew Thompson 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
18102ac6454SAndrew Thompson 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
18202ac6454SAndrew Thompson 	 * so we try both.
18302ac6454SAndrew Thompson 	 */
18402ac6454SAndrew Thompson 
18502ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
18602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
187a593f6b8SAndrew Thompson 	if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
18802ac6454SAndrew Thompson 	    &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {	/* 1.0 */
18902ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
190a593f6b8SAndrew Thompson 		if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
19102ac6454SAndrew Thompson 		    &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {	/* 1.1 */
19202ac6454SAndrew Thompson 			/* ignore error */
19302ac6454SAndrew Thompson 		}
19402ac6454SAndrew Thompson 	}
19502ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
19602ac6454SAndrew Thompson }
19702ac6454SAndrew Thompson 
19802ac6454SAndrew Thompson static void
ulpt_write_callback(struct usb_xfer * xfer,usb_error_t error)199ed6d949aSAndrew Thompson ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
20002ac6454SAndrew Thompson {
201ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
202760bc48eSAndrew Thompson 	struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
203ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
204ed6d949aSAndrew Thompson 	int actlen, max;
205ed6d949aSAndrew Thompson 
206ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
20702ac6454SAndrew Thompson 
20802ac6454SAndrew Thompson 	if (f == NULL) {
20902ac6454SAndrew Thompson 		/* should not happen */
21002ac6454SAndrew Thompson 		DPRINTF("no FIFO\n");
21102ac6454SAndrew Thompson 		return;
21202ac6454SAndrew Thompson 	}
213ed6d949aSAndrew Thompson 	DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
21402ac6454SAndrew Thompson 
21502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
21602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
21702ac6454SAndrew Thompson 	case USB_ST_SETUP:
21802ac6454SAndrew Thompson tr_setup:
219ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
220ed6d949aSAndrew Thompson 		max = usbd_xfer_max_len(xfer);
221ed6d949aSAndrew Thompson 		if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
222ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
223a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
22402ac6454SAndrew Thompson 		}
22502ac6454SAndrew Thompson 		break;
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson 	default:			/* Error */
228ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
22902ac6454SAndrew Thompson 			/* try to clear stall first */
230ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
23102ac6454SAndrew Thompson 			goto tr_setup;
23202ac6454SAndrew Thompson 		}
23302ac6454SAndrew Thompson 		break;
23402ac6454SAndrew Thompson 	}
23502ac6454SAndrew Thompson }
23602ac6454SAndrew Thompson 
23702ac6454SAndrew Thompson static void
ulpt_read_callback(struct usb_xfer * xfer,usb_error_t error)238ed6d949aSAndrew Thompson ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
23902ac6454SAndrew Thompson {
240ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
241760bc48eSAndrew Thompson 	struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
242ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
243ed6d949aSAndrew Thompson 	int actlen;
244ed6d949aSAndrew Thompson 
245ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson 	if (f == NULL) {
24802ac6454SAndrew Thompson 		/* should not happen */
24902ac6454SAndrew Thompson 		DPRINTF("no FIFO\n");
25002ac6454SAndrew Thompson 		return;
25102ac6454SAndrew Thompson 	}
25202ac6454SAndrew Thompson 	DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
25502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
25602ac6454SAndrew Thompson 
257ed6d949aSAndrew Thompson 		if (actlen == 0) {
25802ac6454SAndrew Thompson 			if (sc->sc_zlps == 4) {
25902ac6454SAndrew Thompson 				/* enable BULK throttle */
260ed6d949aSAndrew Thompson 				usbd_xfer_set_interval(xfer, 500); /* ms */
26102ac6454SAndrew Thompson 			} else {
26202ac6454SAndrew Thompson 				sc->sc_zlps++;
26302ac6454SAndrew Thompson 			}
26402ac6454SAndrew Thompson 		} else {
26502ac6454SAndrew Thompson 			/* disable BULK throttle */
26602ac6454SAndrew Thompson 
267ed6d949aSAndrew Thompson 			usbd_xfer_set_interval(xfer, 0);
26802ac6454SAndrew Thompson 			sc->sc_zlps = 0;
26902ac6454SAndrew Thompson 		}
27002ac6454SAndrew Thompson 
271ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
272ed6d949aSAndrew Thompson 		usb_fifo_put_data(f, pc, 0, actlen, 1);
27302ac6454SAndrew Thompson 
27402ac6454SAndrew Thompson 	case USB_ST_SETUP:
27502ac6454SAndrew Thompson tr_setup:
276a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(f) != 0) {
277ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
278a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
27902ac6454SAndrew Thompson 		}
28002ac6454SAndrew Thompson 		break;
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 	default:			/* Error */
28302ac6454SAndrew Thompson 		/* disable BULK throttle */
284ed6d949aSAndrew Thompson 		usbd_xfer_set_interval(xfer, 0);
28502ac6454SAndrew Thompson 		sc->sc_zlps = 0;
28602ac6454SAndrew Thompson 
287ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
28802ac6454SAndrew Thompson 			/* try to clear stall first */
289ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
29002ac6454SAndrew Thompson 			goto tr_setup;
29102ac6454SAndrew Thompson 		}
29202ac6454SAndrew Thompson 		break;
29302ac6454SAndrew Thompson 	}
29402ac6454SAndrew Thompson }
29502ac6454SAndrew Thompson 
29602ac6454SAndrew Thompson static void
ulpt_status_callback(struct usb_xfer * xfer,usb_error_t error)297ed6d949aSAndrew Thompson ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
29802ac6454SAndrew Thompson {
299ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
300760bc48eSAndrew Thompson 	struct usb_device_request req;
301ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
30202ac6454SAndrew Thompson 	uint8_t cur_status;
30302ac6454SAndrew Thompson 	uint8_t new_status;
30402ac6454SAndrew Thompson 
30502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
30602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
30702ac6454SAndrew Thompson 
308ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 1);
309ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, &cur_status, 1);
31002ac6454SAndrew Thompson 
31102ac6454SAndrew Thompson 		cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
31202ac6454SAndrew Thompson 		new_status = cur_status & ~sc->sc_last_status;
31302ac6454SAndrew Thompson 		sc->sc_last_status = cur_status;
31402ac6454SAndrew Thompson 
31502ac6454SAndrew Thompson 		if (new_status & LPS_SELECT)
31602ac6454SAndrew Thompson 			log(LOG_NOTICE, "%s: offline\n",
31702ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev));
31802ac6454SAndrew Thompson 		else if (new_status & LPS_NOPAPER)
31902ac6454SAndrew Thompson 			log(LOG_NOTICE, "%s: out of paper\n",
32002ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev));
32102ac6454SAndrew Thompson 		else if (new_status & LPS_NERR)
32202ac6454SAndrew Thompson 			log(LOG_NOTICE, "%s: output error\n",
32302ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev));
32402ac6454SAndrew Thompson 		break;
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson 	case USB_ST_SETUP:
32702ac6454SAndrew Thompson 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
32802ac6454SAndrew Thompson 		req.bRequest = UR_GET_PORT_STATUS;
32902ac6454SAndrew Thompson 		USETW(req.wValue, 0);
33002ac6454SAndrew Thompson 		req.wIndex[0] = sc->sc_iface_no;
33102ac6454SAndrew Thompson 		req.wIndex[1] = 0;
33202ac6454SAndrew Thompson 		USETW(req.wLength, 1);
33302ac6454SAndrew Thompson 
334ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
335ed6d949aSAndrew Thompson 		usbd_copy_in(pc, 0, &req, sizeof(req));
33602ac6454SAndrew Thompson 
337ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
338ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 1, 1);
339ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, 2);
340a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
34102ac6454SAndrew Thompson 		break;
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 	default:			/* Error */
344ed6d949aSAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(error));
345ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
34602ac6454SAndrew Thompson 			/* wait for next watchdog timeout */
34702ac6454SAndrew Thompson 		}
34802ac6454SAndrew Thompson 		break;
34902ac6454SAndrew Thompson 	}
35002ac6454SAndrew Thompson }
35102ac6454SAndrew Thompson 
352760bc48eSAndrew Thompson static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
35302ac6454SAndrew Thompson 	[ULPT_BULK_DT_WR] = {
35402ac6454SAndrew Thompson 		.type = UE_BULK,
35502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
35602ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
3574eae601eSAndrew Thompson 		.bufsize = ULPT_BSIZE,
3584eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.proxy_buffer = 1},
3594eae601eSAndrew Thompson 		.callback = &ulpt_write_callback,
36002ac6454SAndrew Thompson 	},
36102ac6454SAndrew Thompson 
36202ac6454SAndrew Thompson 	[ULPT_BULK_DT_RD] = {
36302ac6454SAndrew Thompson 		.type = UE_BULK,
36402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
36502ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
3664eae601eSAndrew Thompson 		.bufsize = ULPT_BSIZE,
3674eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
3684eae601eSAndrew Thompson 		.callback = &ulpt_read_callback,
36902ac6454SAndrew Thompson 	},
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 	[ULPT_INTR_DT_RD] = {
37202ac6454SAndrew Thompson 		.type = UE_CONTROL,
37302ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
37402ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
375760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request) + 1,
3764eae601eSAndrew Thompson 		.callback = &ulpt_status_callback,
3774eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
37802ac6454SAndrew Thompson 	},
37902ac6454SAndrew Thompson };
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson static void
ulpt_start_read(struct usb_fifo * fifo)382760bc48eSAndrew Thompson ulpt_start_read(struct usb_fifo *fifo)
38302ac6454SAndrew Thompson {
384ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
38502ac6454SAndrew Thompson 
386a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
38702ac6454SAndrew Thompson }
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson static void
ulpt_stop_read(struct usb_fifo * fifo)390760bc48eSAndrew Thompson ulpt_stop_read(struct usb_fifo *fifo)
39102ac6454SAndrew Thompson {
392ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
39302ac6454SAndrew Thompson 
394a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
39502ac6454SAndrew Thompson }
39602ac6454SAndrew Thompson 
39702ac6454SAndrew Thompson static void
ulpt_start_write(struct usb_fifo * fifo)398760bc48eSAndrew Thompson ulpt_start_write(struct usb_fifo *fifo)
39902ac6454SAndrew Thompson {
400ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
40102ac6454SAndrew Thompson 
402a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
40302ac6454SAndrew Thompson }
40402ac6454SAndrew Thompson 
40502ac6454SAndrew Thompson static void
ulpt_stop_write(struct usb_fifo * fifo)406760bc48eSAndrew Thompson ulpt_stop_write(struct usb_fifo *fifo)
40702ac6454SAndrew Thompson {
408ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
40902ac6454SAndrew Thompson 
410a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
41102ac6454SAndrew Thompson }
41202ac6454SAndrew Thompson 
41302ac6454SAndrew Thompson static int
ulpt_open(struct usb_fifo * fifo,int fflags)414760bc48eSAndrew Thompson ulpt_open(struct usb_fifo *fifo, int fflags)
41502ac6454SAndrew Thompson {
416ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson 	/* we assume that open is a serial process */
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson 	if (sc->sc_fflags == 0) {
42120733245SPedro F. Giffuni 		/* reset USB parallel port */
4224743e6daSAlfred Perlstein 
42302ac6454SAndrew Thompson 		ulpt_reset(sc);
42402ac6454SAndrew Thompson 	}
425ee3e3ff5SAndrew Thompson 	return (unlpt_open(fifo, fflags));
42602ac6454SAndrew Thompson }
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson static int
unlpt_open(struct usb_fifo * fifo,int fflags)429760bc48eSAndrew Thompson unlpt_open(struct usb_fifo *fifo, int fflags)
43002ac6454SAndrew Thompson {
431ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson 	if (sc->sc_fflags & fflags) {
43402ac6454SAndrew Thompson 		return (EBUSY);
43502ac6454SAndrew Thompson 	}
43602ac6454SAndrew Thompson 	if (fflags & FREAD) {
437ec97e9caSHans Petter Selasky 		/* clear stall first */
438ec97e9caSHans Petter Selasky 		mtx_lock(&sc->sc_mtx);
439ec97e9caSHans Petter Selasky 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
440ec97e9caSHans Petter Selasky 		mtx_unlock(&sc->sc_mtx);
441a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
442ed6d949aSAndrew Thompson 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
44302ac6454SAndrew Thompson 		    ULPT_IFQ_MAXLEN)) {
44402ac6454SAndrew Thompson 			return (ENOMEM);
44502ac6454SAndrew Thompson 		}
44602ac6454SAndrew Thompson 		/* set which FIFO is opened */
44702ac6454SAndrew Thompson 		sc->sc_fifo_open[USB_FIFO_RX] = fifo;
44802ac6454SAndrew Thompson 	}
44902ac6454SAndrew Thompson 	if (fflags & FWRITE) {
45002ac6454SAndrew Thompson 		/* clear stall first */
45102ac6454SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
452ec97e9caSHans Petter Selasky 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
45302ac6454SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
454a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
455ed6d949aSAndrew Thompson 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
45602ac6454SAndrew Thompson 		    ULPT_IFQ_MAXLEN)) {
45702ac6454SAndrew Thompson 			return (ENOMEM);
45802ac6454SAndrew Thompson 		}
45902ac6454SAndrew Thompson 		/* set which FIFO is opened */
46002ac6454SAndrew Thompson 		sc->sc_fifo_open[USB_FIFO_TX] = fifo;
46102ac6454SAndrew Thompson 	}
46202ac6454SAndrew Thompson 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
46302ac6454SAndrew Thompson 	return (0);
46402ac6454SAndrew Thompson }
46502ac6454SAndrew Thompson 
46602ac6454SAndrew Thompson static void
ulpt_close(struct usb_fifo * fifo,int fflags)467760bc48eSAndrew Thompson ulpt_close(struct usb_fifo *fifo, int fflags)
46802ac6454SAndrew Thompson {
469ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson 	if (fflags & (FREAD | FWRITE)) {
474a593f6b8SAndrew Thompson 		usb_fifo_free_buffer(fifo);
47502ac6454SAndrew Thompson 	}
47602ac6454SAndrew Thompson }
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson static int
ulpt_ioctl(struct usb_fifo * fifo,u_long cmd,void * data,int fflags)479760bc48eSAndrew Thompson ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
480ee3e3ff5SAndrew Thompson     int fflags)
48102ac6454SAndrew Thompson {
48202ac6454SAndrew Thompson 	return (ENODEV);
48302ac6454SAndrew Thompson }
48402ac6454SAndrew Thompson 
485f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID ulpt_devs[] = {
486f1a16106SHans Petter Selasky 	/* Uni-directional USB printer */
487f1a16106SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_PRINTER),
488f1a16106SHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
489f1a16106SHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
490f1a16106SHans Petter Selasky 
491f1a16106SHans Petter Selasky 	/* Bi-directional USB printer */
492f1a16106SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_PRINTER),
493f1a16106SHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
494f1a16106SHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
495f1a16106SHans Petter Selasky 
496f1a16106SHans Petter Selasky 	/* 1284 USB printer */
497f1a16106SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_PRINTER),
498f1a16106SHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
499f1a16106SHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
50088162f7aSHans Petter Selasky 
50188162f7aSHans Petter Selasky 	/* Epson printer */
50288162f7aSHans Petter Selasky 	{USB_VENDOR(USB_VENDOR_EPSON),
50388162f7aSHans Petter Selasky 	 USB_PRODUCT(USB_PRODUCT_EPSON_TMU220B),
50488162f7aSHans Petter Selasky 	 USB_IFACE_CLASS(UICLASS_VENDOR),
50588162f7aSHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_VENDOR),
50688162f7aSHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
507f1a16106SHans Petter Selasky };
508f1a16106SHans Petter Selasky 
50902ac6454SAndrew Thompson static int
ulpt_probe(device_t dev)51002ac6454SAndrew Thompson ulpt_probe(device_t dev)
51102ac6454SAndrew Thompson {
512760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
513f1a16106SHans Petter Selasky 	int error;
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
51602ac6454SAndrew Thompson 
517f1a16106SHans Petter Selasky 	if (uaa->usb_mode != USB_MODE_HOST)
51802ac6454SAndrew Thompson 		return (ENXIO);
519f1a16106SHans Petter Selasky 
520f1a16106SHans Petter Selasky 	error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
521f1a16106SHans Petter Selasky 	if (error)
522f1a16106SHans Petter Selasky 		return (error);
523f1a16106SHans Petter Selasky 
524f1a16106SHans Petter Selasky 	return (BUS_PROBE_GENERIC);
52502ac6454SAndrew Thompson }
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson static int
ulpt_attach(device_t dev)52802ac6454SAndrew Thompson ulpt_attach(device_t dev)
52902ac6454SAndrew Thompson {
530760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
53102ac6454SAndrew Thompson 	struct ulpt_softc *sc = device_get_softc(dev);
532760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
53302ac6454SAndrew Thompson 	int unit = device_get_unit(dev);
53402ac6454SAndrew Thompson 	int error;
53502ac6454SAndrew Thompson 	uint8_t iface_index = uaa->info.bIfaceIndex;
53602ac6454SAndrew Thompson 	uint8_t alt_index;
53702ac6454SAndrew Thompson 
53802ac6454SAndrew Thompson 	DPRINTFN(11, "sc=%p\n", sc);
53902ac6454SAndrew Thompson 
54002ac6454SAndrew Thompson 	sc->sc_dev = dev;
54102ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
54202ac6454SAndrew Thompson 
543a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
54402ac6454SAndrew Thompson 
54502ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
54602ac6454SAndrew Thompson 
547a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 	/* search through all the descriptors looking for bidir mode */
55002ac6454SAndrew Thompson 
551a593f6b8SAndrew Thompson 	id = usbd_get_interface_descriptor(uaa->iface);
5526d917491SHans Petter Selasky 	alt_index = 0xFF;
55302ac6454SAndrew Thompson 	while (1) {
55402ac6454SAndrew Thompson 		if (id == NULL) {
55502ac6454SAndrew Thompson 			break;
55602ac6454SAndrew Thompson 		}
55702ac6454SAndrew Thompson 		if ((id->bDescriptorType == UDESC_INTERFACE) &&
55802ac6454SAndrew Thompson 		    (id->bLength >= sizeof(*id))) {
55902ac6454SAndrew Thompson 			if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
56002ac6454SAndrew Thompson 				break;
56102ac6454SAndrew Thompson 			} else {
56202ac6454SAndrew Thompson 				alt_index++;
56388162f7aSHans Petter Selasky 				if ((id->bInterfaceClass == UICLASS_PRINTER ||
56488162f7aSHans Petter Selasky 				     id->bInterfaceClass == UICLASS_VENDOR) &&
56588162f7aSHans Petter Selasky 				    (id->bInterfaceSubClass == UISUBCLASS_PRINTER ||
56688162f7aSHans Petter Selasky 				     id->bInterfaceSubClass == UISUBCLASS_VENDOR) &&
56702ac6454SAndrew Thompson 				    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
56802ac6454SAndrew Thompson 					goto found;
56902ac6454SAndrew Thompson 				}
57002ac6454SAndrew Thompson 			}
57102ac6454SAndrew Thompson 		}
572a593f6b8SAndrew Thompson 		id = (void *)usb_desc_foreach(
573a593f6b8SAndrew Thompson 		    usbd_get_config_descriptor(uaa->device), (void *)id);
57402ac6454SAndrew Thompson 	}
57502ac6454SAndrew Thompson 	goto detach;
57602ac6454SAndrew Thompson 
57702ac6454SAndrew Thompson found:
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson 	DPRINTF("setting alternate "
58002ac6454SAndrew Thompson 	    "config number: %d\n", alt_index);
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 	if (alt_index) {
583a593f6b8SAndrew Thompson 		error = usbd_set_alt_interface_index
58402ac6454SAndrew Thompson 		    (uaa->device, iface_index, alt_index);
58502ac6454SAndrew Thompson 
58602ac6454SAndrew Thompson 		if (error) {
58702ac6454SAndrew Thompson 			DPRINTF("could not set alternate "
588a593f6b8SAndrew Thompson 			    "config, error=%s\n", usbd_errstr(error));
58902ac6454SAndrew Thompson 			goto detach;
59002ac6454SAndrew Thompson 		}
59102ac6454SAndrew Thompson 	}
59202ac6454SAndrew Thompson 	sc->sc_iface_no = id->bInterfaceNumber;
59302ac6454SAndrew Thompson 
594a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
59502ac6454SAndrew Thompson 	    sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
59602ac6454SAndrew Thompson 	    sc, &sc->sc_mtx);
59702ac6454SAndrew Thompson 	if (error) {
598a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(error));
59902ac6454SAndrew Thompson 		goto detach;
60002ac6454SAndrew Thompson 	}
60102ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "using bi-directional mode\n");
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson #if 0
60402ac6454SAndrew Thompson /*
60502ac6454SAndrew Thompson  * This code is disabled because for some mysterious reason it causes
60602ac6454SAndrew Thompson  * printing not to work.  But only sometimes, and mostly with
60702ac6454SAndrew Thompson  * UHCI and less often with OHCI.  *sigh*
60802ac6454SAndrew Thompson  */
60902ac6454SAndrew Thompson 	{
610a593f6b8SAndrew Thompson 		struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
611760bc48eSAndrew Thompson 		struct usb_device_request req;
61202ac6454SAndrew Thompson 		int len, alen;
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
61502ac6454SAndrew Thompson 		req.bRequest = UR_GET_DEVICE_ID;
61602ac6454SAndrew Thompson 		USETW(req.wValue, cd->bConfigurationValue);
61702ac6454SAndrew Thompson 		USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
61802ac6454SAndrew Thompson 		USETW(req.wLength, sizeof devinfo - 1);
619a593f6b8SAndrew Thompson 		error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
62002ac6454SAndrew Thompson 		    &alen, USB_DEFAULT_TIMEOUT);
62102ac6454SAndrew Thompson 		if (error) {
62202ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "cannot get device id\n");
62302ac6454SAndrew Thompson 		} else if (alen <= 2) {
62402ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "empty device id, no "
62502ac6454SAndrew Thompson 			    "printer connected?\n");
62602ac6454SAndrew Thompson 		} else {
62702ac6454SAndrew Thompson 			/* devinfo now contains an IEEE-1284 device ID */
62802ac6454SAndrew Thompson 			len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
62902ac6454SAndrew Thompson 			if (len > sizeof devinfo - 3)
63002ac6454SAndrew Thompson 				len = sizeof devinfo - 3;
63102ac6454SAndrew Thompson 			devinfo[len] = 0;
63202ac6454SAndrew Thompson 			printf("%s: device id <", device_get_nameunit(sc->sc_dev));
63302ac6454SAndrew Thompson 			ieee1284_print_id(devinfo + 2);
63402ac6454SAndrew Thompson 			printf(">\n");
63502ac6454SAndrew Thompson 		}
63602ac6454SAndrew Thompson 	}
63702ac6454SAndrew Thompson #endif
63802ac6454SAndrew Thompson 
639a593f6b8SAndrew Thompson 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
64002ac6454SAndrew Thompson 	    &ulpt_fifo_methods, &sc->sc_fifo,
6416d917491SHans Petter Selasky 	    unit, -1, uaa->info.bIfaceIndex,
642ee3e3ff5SAndrew Thompson 	    UID_ROOT, GID_OPERATOR, 0644);
64302ac6454SAndrew Thompson 	if (error) {
64402ac6454SAndrew Thompson 		goto detach;
64502ac6454SAndrew Thompson 	}
646a593f6b8SAndrew Thompson 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
64702ac6454SAndrew Thompson 	    &unlpt_fifo_methods, &sc->sc_fifo_noreset,
6486d917491SHans Petter Selasky 	    unit, -1, uaa->info.bIfaceIndex,
649ee3e3ff5SAndrew Thompson 	    UID_ROOT, GID_OPERATOR, 0644);
65002ac6454SAndrew Thompson 	if (error) {
65102ac6454SAndrew Thompson 		goto detach;
65202ac6454SAndrew Thompson 	}
65302ac6454SAndrew Thompson 	/* start reading of status */
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
65602ac6454SAndrew Thompson 	ulpt_watchdog(sc);
65702ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
65802ac6454SAndrew Thompson 	return (0);
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson detach:
66102ac6454SAndrew Thompson 	ulpt_detach(dev);
66202ac6454SAndrew Thompson 	return (ENOMEM);
66302ac6454SAndrew Thompson }
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson static int
ulpt_detach(device_t dev)66602ac6454SAndrew Thompson ulpt_detach(device_t dev)
66702ac6454SAndrew Thompson {
66802ac6454SAndrew Thompson 	struct ulpt_softc *sc = device_get_softc(dev);
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
67102ac6454SAndrew Thompson 
672a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo);
673a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo_noreset);
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
676a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_watchdog);
67702ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
67802ac6454SAndrew Thompson 
679a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
680a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_watchdog);
68102ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
68202ac6454SAndrew Thompson 
68302ac6454SAndrew Thompson 	return (0);
68402ac6454SAndrew Thompson }
68502ac6454SAndrew Thompson 
68602ac6454SAndrew Thompson #if 0
68702ac6454SAndrew Thompson /* XXX This does not belong here. */
68802ac6454SAndrew Thompson 
68902ac6454SAndrew Thompson /*
69002ac6454SAndrew Thompson  * Compare two strings until the second ends.
69102ac6454SAndrew Thompson  */
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson static uint8_t
69402ac6454SAndrew Thompson ieee1284_compare(const char *a, const char *b)
69502ac6454SAndrew Thompson {
69602ac6454SAndrew Thompson 	while (1) {
69702ac6454SAndrew Thompson 		if (*b == 0) {
69802ac6454SAndrew Thompson 			break;
69902ac6454SAndrew Thompson 		}
70002ac6454SAndrew Thompson 		if (*a != *b) {
70102ac6454SAndrew Thompson 			return 1;
70202ac6454SAndrew Thompson 		}
70302ac6454SAndrew Thompson 		b++;
70402ac6454SAndrew Thompson 		a++;
70502ac6454SAndrew Thompson 	}
70602ac6454SAndrew Thompson 	return 0;
70702ac6454SAndrew Thompson }
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson /*
71002ac6454SAndrew Thompson  * Print select parts of an IEEE 1284 device ID.
71102ac6454SAndrew Thompson  */
71202ac6454SAndrew Thompson void
71302ac6454SAndrew Thompson ieee1284_print_id(char *str)
71402ac6454SAndrew Thompson {
71502ac6454SAndrew Thompson 	char *p, *q;
71602ac6454SAndrew Thompson 
71702ac6454SAndrew Thompson 	for (p = str - 1; p; p = strchr(p, ';')) {
71802ac6454SAndrew Thompson 		p++;			/* skip ';' */
71902ac6454SAndrew Thompson 		if (ieee1284_compare(p, "MFG:") == 0 ||
72002ac6454SAndrew Thompson 		    ieee1284_compare(p, "MANUFACTURER:") == 0 ||
72102ac6454SAndrew Thompson 		    ieee1284_compare(p, "MDL:") == 0 ||
72202ac6454SAndrew Thompson 		    ieee1284_compare(p, "MODEL:") == 0) {
72302ac6454SAndrew Thompson 			q = strchr(p, ';');
72402ac6454SAndrew Thompson 			if (q)
72502ac6454SAndrew Thompson 				printf("%.*s", (int)(q - p + 1), p);
72602ac6454SAndrew Thompson 		}
72702ac6454SAndrew Thompson 	}
72802ac6454SAndrew Thompson }
72902ac6454SAndrew Thompson 
73002ac6454SAndrew Thompson #endif
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson static void
ulpt_watchdog(void * arg)73302ac6454SAndrew Thompson ulpt_watchdog(void *arg)
73402ac6454SAndrew Thompson {
73502ac6454SAndrew Thompson 	struct ulpt_softc *sc = arg;
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 	mtx_assert(&sc->sc_mtx, MA_OWNED);
73802ac6454SAndrew Thompson 
7394743e6daSAlfred Perlstein 	/*
7404743e6daSAlfred Perlstein 	 * Only read status while the device is not opened, due to
7414743e6daSAlfred Perlstein 	 * possible hardware or firmware bug in some printers.
7424743e6daSAlfred Perlstein 	 */
7434743e6daSAlfred Perlstein 	if (sc->sc_fflags == 0)
744a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
74502ac6454SAndrew Thompson 
746a593f6b8SAndrew Thompson 	usb_callout_reset(&sc->sc_watchdog,
74702ac6454SAndrew Thompson 	    hz, &ulpt_watchdog, sc);
74802ac6454SAndrew Thompson }
74902ac6454SAndrew Thompson 
75002ac6454SAndrew Thompson static device_method_t ulpt_methods[] = {
75102ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ulpt_probe),
75202ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ulpt_attach),
75302ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ulpt_detach),
7545805d178SHans Petter Selasky 	DEVMETHOD_END
75502ac6454SAndrew Thompson };
75602ac6454SAndrew Thompson 
75702ac6454SAndrew Thompson static driver_t ulpt_driver = {
75802ac6454SAndrew Thompson 	.name = "ulpt",
75902ac6454SAndrew Thompson 	.methods = ulpt_methods,
76002ac6454SAndrew Thompson 	.size = sizeof(struct ulpt_softc),
76102ac6454SAndrew Thompson };
76202ac6454SAndrew Thompson 
763bc9372d7SJohn Baldwin DRIVER_MODULE(ulpt, uhub, ulpt_driver, NULL, NULL);
76402ac6454SAndrew Thompson MODULE_DEPEND(ulpt, usb, 1, 1, 1);
765910cb8feSAndrew Thompson MODULE_VERSION(ulpt, 1);
766f809f280SWarner Losh USB_PNP_HOST_INFO(ulpt_devs);
767