xref: /freebsd/sys/dev/usb/serial/ulpt.c (revision ca48e43ba9ee73a07cdbad8365117793b01273bb)
102ac6454SAndrew Thompson /*	$NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*-
4*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5718cf2ccSPedro F. Giffuni  *
602ac6454SAndrew Thompson  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
702ac6454SAndrew Thompson  * All rights reserved.
802ac6454SAndrew Thompson  *
902ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
1002ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
1102ac6454SAndrew Thompson  * Carlstedt Research & Technology.
1202ac6454SAndrew Thompson  *
1302ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1402ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1502ac6454SAndrew Thompson  * are met:
1602ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1702ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1802ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
2002ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
2102ac6454SAndrew Thompson  *
2202ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2302ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2402ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2502ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2602ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2702ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2802ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2902ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3002ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3102ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3202ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3302ac6454SAndrew Thompson  */
3402ac6454SAndrew Thompson 
3502ac6454SAndrew Thompson /*
3602ac6454SAndrew Thompson  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
3702ac6454SAndrew Thompson  * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
3802ac6454SAndrew Thompson  */
3902ac6454SAndrew Thompson 
40ed6d949aSAndrew Thompson #include <sys/stdint.h>
41ed6d949aSAndrew Thompson #include <sys/stddef.h>
42ed6d949aSAndrew Thompson #include <sys/param.h>
43ed6d949aSAndrew Thompson #include <sys/queue.h>
44ed6d949aSAndrew Thompson #include <sys/types.h>
45ed6d949aSAndrew Thompson #include <sys/systm.h>
46ed6d949aSAndrew Thompson #include <sys/kernel.h>
47ed6d949aSAndrew Thompson #include <sys/bus.h>
48ed6d949aSAndrew Thompson #include <sys/module.h>
49ed6d949aSAndrew Thompson #include <sys/lock.h>
50ed6d949aSAndrew Thompson #include <sys/mutex.h>
51ed6d949aSAndrew Thompson #include <sys/condvar.h>
52ed6d949aSAndrew Thompson #include <sys/sysctl.h>
53ed6d949aSAndrew Thompson #include <sys/sx.h>
54ed6d949aSAndrew Thompson #include <sys/unistd.h>
55ed6d949aSAndrew Thompson #include <sys/callout.h>
56ed6d949aSAndrew Thompson #include <sys/malloc.h>
57ed6d949aSAndrew Thompson #include <sys/priv.h>
58ed6d949aSAndrew Thompson #include <sys/syslog.h>
59ed6d949aSAndrew Thompson #include <sys/selinfo.h>
60ed6d949aSAndrew Thompson #include <sys/conf.h>
61ed6d949aSAndrew Thompson #include <sys/fcntl.h>
62ed6d949aSAndrew Thompson 
6302ac6454SAndrew Thompson #include <dev/usb/usb.h>
64ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
65ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
66ed6d949aSAndrew Thompson #include <dev/usb/usbhid.h>
67ed6d949aSAndrew Thompson #include "usbdevs.h"
6802ac6454SAndrew Thompson 
6902ac6454SAndrew Thompson #define	USB_DEBUG_VAR ulpt_debug
7002ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7102ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7202ac6454SAndrew Thompson 
73b850ecc1SAndrew Thompson #ifdef USB_DEBUG
7402ac6454SAndrew Thompson static int ulpt_debug = 0;
7502ac6454SAndrew Thompson 
76f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
77f8d2b1f3SPawel Biernacki     "USB ulpt");
78ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RWTUN,
7902ac6454SAndrew Thompson     &ulpt_debug, 0, "Debug level");
8002ac6454SAndrew Thompson #endif
8102ac6454SAndrew Thompson 
8202ac6454SAndrew Thompson #define	ULPT_BSIZE		(1<<15)	/* bytes */
8302ac6454SAndrew Thompson #define	ULPT_IFQ_MAXLEN         2	/* units */
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson #define	UR_GET_DEVICE_ID        0x00
8602ac6454SAndrew Thompson #define	UR_GET_PORT_STATUS      0x01
8702ac6454SAndrew Thompson #define	UR_SOFT_RESET           0x02
8802ac6454SAndrew Thompson 
8902ac6454SAndrew Thompson #define	LPS_NERR		0x08	/* printer no error */
9002ac6454SAndrew Thompson #define	LPS_SELECT		0x10	/* printer selected */
9102ac6454SAndrew Thompson #define	LPS_NOPAPER		0x20	/* printer out of paper */
9202ac6454SAndrew Thompson #define	LPS_INVERT      (LPS_SELECT|LPS_NERR)
9302ac6454SAndrew Thompson #define	LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
9402ac6454SAndrew Thompson 
9502ac6454SAndrew Thompson enum {
9602ac6454SAndrew Thompson 	ULPT_BULK_DT_WR,
9702ac6454SAndrew Thompson 	ULPT_BULK_DT_RD,
9802ac6454SAndrew Thompson 	ULPT_INTR_DT_RD,
9902ac6454SAndrew Thompson 	ULPT_N_TRANSFER,
10002ac6454SAndrew Thompson };
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson struct ulpt_softc {
103760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo;
104760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo_noreset;
10502ac6454SAndrew Thompson 	struct mtx sc_mtx;
106760bc48eSAndrew Thompson 	struct usb_callout sc_watchdog;
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson 	device_t sc_dev;
109760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
110760bc48eSAndrew Thompson 	struct usb_fifo *sc_fifo_open[2];
111760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson 	int	sc_fflags;		/* current open flags, FREAD and
11402ac6454SAndrew Thompson 					 * FWRITE */
11502ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
11602ac6454SAndrew Thompson 	uint8_t	sc_last_status;
11702ac6454SAndrew Thompson 	uint8_t	sc_zlps;		/* number of consequtive zero length
11802ac6454SAndrew Thompson 					 * packets received */
11902ac6454SAndrew Thompson };
12002ac6454SAndrew Thompson 
12102ac6454SAndrew Thompson /* prototypes */
12202ac6454SAndrew Thompson 
12302ac6454SAndrew Thompson static device_probe_t ulpt_probe;
12402ac6454SAndrew Thompson static device_attach_t ulpt_attach;
12502ac6454SAndrew Thompson static device_detach_t ulpt_detach;
12602ac6454SAndrew Thompson 
127e0a69b51SAndrew Thompson static usb_callback_t ulpt_write_callback;
128e0a69b51SAndrew Thompson static usb_callback_t ulpt_read_callback;
129e0a69b51SAndrew Thompson static usb_callback_t ulpt_status_callback;
13002ac6454SAndrew Thompson 
13102ac6454SAndrew Thompson static void	ulpt_reset(struct ulpt_softc *);
13202ac6454SAndrew Thompson static void	ulpt_watchdog(void *);
13302ac6454SAndrew Thompson 
134e0a69b51SAndrew Thompson static usb_fifo_close_t ulpt_close;
135e0a69b51SAndrew Thompson static usb_fifo_cmd_t ulpt_start_read;
136e0a69b51SAndrew Thompson static usb_fifo_cmd_t ulpt_start_write;
137e0a69b51SAndrew Thompson static usb_fifo_cmd_t ulpt_stop_read;
138e0a69b51SAndrew Thompson static usb_fifo_cmd_t ulpt_stop_write;
139e0a69b51SAndrew Thompson static usb_fifo_ioctl_t ulpt_ioctl;
140e0a69b51SAndrew Thompson static usb_fifo_open_t ulpt_open;
141e0a69b51SAndrew Thompson static usb_fifo_open_t unlpt_open;
14202ac6454SAndrew Thompson 
143760bc48eSAndrew Thompson static struct usb_fifo_methods ulpt_fifo_methods = {
14402ac6454SAndrew Thompson 	.f_close = &ulpt_close,
14502ac6454SAndrew Thompson 	.f_ioctl = &ulpt_ioctl,
14602ac6454SAndrew Thompson 	.f_open = &ulpt_open,
14702ac6454SAndrew Thompson 	.f_start_read = &ulpt_start_read,
14802ac6454SAndrew Thompson 	.f_start_write = &ulpt_start_write,
14902ac6454SAndrew Thompson 	.f_stop_read = &ulpt_stop_read,
15002ac6454SAndrew Thompson 	.f_stop_write = &ulpt_stop_write,
15102ac6454SAndrew Thompson 	.basename[0] = "ulpt",
15202ac6454SAndrew Thompson };
15302ac6454SAndrew Thompson 
154760bc48eSAndrew Thompson static struct usb_fifo_methods unlpt_fifo_methods = {
15502ac6454SAndrew Thompson 	.f_close = &ulpt_close,
15602ac6454SAndrew Thompson 	.f_ioctl = &ulpt_ioctl,
15702ac6454SAndrew Thompson 	.f_open = &unlpt_open,
15802ac6454SAndrew Thompson 	.f_start_read = &ulpt_start_read,
15902ac6454SAndrew Thompson 	.f_start_write = &ulpt_start_write,
16002ac6454SAndrew Thompson 	.f_stop_read = &ulpt_stop_read,
16102ac6454SAndrew Thompson 	.f_stop_write = &ulpt_stop_write,
16202ac6454SAndrew Thompson 	.basename[0] = "unlpt",
16302ac6454SAndrew Thompson };
16402ac6454SAndrew Thompson 
16502ac6454SAndrew Thompson static void
ulpt_reset(struct ulpt_softc * sc)16602ac6454SAndrew Thompson ulpt_reset(struct ulpt_softc *sc)
16702ac6454SAndrew Thompson {
168760bc48eSAndrew Thompson 	struct usb_device_request req;
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
17102ac6454SAndrew Thompson 
17202ac6454SAndrew Thompson 	req.bRequest = UR_SOFT_RESET;
17302ac6454SAndrew Thompson 	USETW(req.wValue, 0);
17402ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_iface_no);
17502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
17602ac6454SAndrew Thompson 
17702ac6454SAndrew Thompson 	/*
17802ac6454SAndrew Thompson 	 * There was a mistake in the USB printer 1.0 spec that gave the
17902ac6454SAndrew Thompson 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
18002ac6454SAndrew Thompson 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
18102ac6454SAndrew Thompson 	 * so we try both.
18202ac6454SAndrew Thompson 	 */
18302ac6454SAndrew Thompson 
18402ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
18502ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
186a593f6b8SAndrew Thompson 	if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
18702ac6454SAndrew Thompson 	    &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {	/* 1.0 */
18802ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
189a593f6b8SAndrew Thompson 		if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
19002ac6454SAndrew Thompson 		    &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {	/* 1.1 */
19102ac6454SAndrew Thompson 			/* ignore error */
19202ac6454SAndrew Thompson 		}
19302ac6454SAndrew Thompson 	}
19402ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
19502ac6454SAndrew Thompson }
19602ac6454SAndrew Thompson 
19702ac6454SAndrew Thompson static void
ulpt_write_callback(struct usb_xfer * xfer,usb_error_t error)198ed6d949aSAndrew Thompson ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
19902ac6454SAndrew Thompson {
200ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
201760bc48eSAndrew Thompson 	struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
202ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
203ed6d949aSAndrew Thompson 	int actlen, max;
204ed6d949aSAndrew Thompson 
205ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
20602ac6454SAndrew Thompson 
20702ac6454SAndrew Thompson 	if (f == NULL) {
20802ac6454SAndrew Thompson 		/* should not happen */
20902ac6454SAndrew Thompson 		DPRINTF("no FIFO\n");
21002ac6454SAndrew Thompson 		return;
21102ac6454SAndrew Thompson 	}
212ed6d949aSAndrew Thompson 	DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
21302ac6454SAndrew Thompson 
21402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
21502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
21602ac6454SAndrew Thompson 	case USB_ST_SETUP:
21702ac6454SAndrew Thompson tr_setup:
218ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
219ed6d949aSAndrew Thompson 		max = usbd_xfer_max_len(xfer);
220ed6d949aSAndrew Thompson 		if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
221ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
222a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
22302ac6454SAndrew Thompson 		}
22402ac6454SAndrew Thompson 		break;
22502ac6454SAndrew Thompson 
22602ac6454SAndrew Thompson 	default:			/* Error */
227ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
22802ac6454SAndrew Thompson 			/* try to clear stall first */
229ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
23002ac6454SAndrew Thompson 			goto tr_setup;
23102ac6454SAndrew Thompson 		}
23202ac6454SAndrew Thompson 		break;
23302ac6454SAndrew Thompson 	}
23402ac6454SAndrew Thompson }
23502ac6454SAndrew Thompson 
23602ac6454SAndrew Thompson static void
ulpt_read_callback(struct usb_xfer * xfer,usb_error_t error)237ed6d949aSAndrew Thompson ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
23802ac6454SAndrew Thompson {
239ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
240760bc48eSAndrew Thompson 	struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
241ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
242ed6d949aSAndrew Thompson 	int actlen;
243ed6d949aSAndrew Thompson 
244ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
24502ac6454SAndrew Thompson 
24602ac6454SAndrew Thompson 	if (f == NULL) {
24702ac6454SAndrew Thompson 		/* should not happen */
24802ac6454SAndrew Thompson 		DPRINTF("no FIFO\n");
24902ac6454SAndrew Thompson 		return;
25002ac6454SAndrew Thompson 	}
25102ac6454SAndrew Thompson 	DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
25202ac6454SAndrew Thompson 
25302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
25402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
25502ac6454SAndrew Thompson 
256ed6d949aSAndrew Thompson 		if (actlen == 0) {
25702ac6454SAndrew Thompson 			if (sc->sc_zlps == 4) {
25802ac6454SAndrew Thompson 				/* enable BULK throttle */
259ed6d949aSAndrew Thompson 				usbd_xfer_set_interval(xfer, 500); /* ms */
26002ac6454SAndrew Thompson 			} else {
26102ac6454SAndrew Thompson 				sc->sc_zlps++;
26202ac6454SAndrew Thompson 			}
26302ac6454SAndrew Thompson 		} else {
26402ac6454SAndrew Thompson 			/* disable BULK throttle */
26502ac6454SAndrew Thompson 
266ed6d949aSAndrew Thompson 			usbd_xfer_set_interval(xfer, 0);
26702ac6454SAndrew Thompson 			sc->sc_zlps = 0;
26802ac6454SAndrew Thompson 		}
26902ac6454SAndrew Thompson 
270ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
271ed6d949aSAndrew Thompson 		usb_fifo_put_data(f, pc, 0, actlen, 1);
27202ac6454SAndrew Thompson 
27302ac6454SAndrew Thompson 	case USB_ST_SETUP:
27402ac6454SAndrew Thompson tr_setup:
275a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(f) != 0) {
276ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
277a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
27802ac6454SAndrew Thompson 		}
27902ac6454SAndrew Thompson 		break;
28002ac6454SAndrew Thompson 
28102ac6454SAndrew Thompson 	default:			/* Error */
28202ac6454SAndrew Thompson 		/* disable BULK throttle */
283ed6d949aSAndrew Thompson 		usbd_xfer_set_interval(xfer, 0);
28402ac6454SAndrew Thompson 		sc->sc_zlps = 0;
28502ac6454SAndrew Thompson 
286ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
28702ac6454SAndrew Thompson 			/* try to clear stall first */
288ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
28902ac6454SAndrew Thompson 			goto tr_setup;
29002ac6454SAndrew Thompson 		}
29102ac6454SAndrew Thompson 		break;
29202ac6454SAndrew Thompson 	}
29302ac6454SAndrew Thompson }
29402ac6454SAndrew Thompson 
29502ac6454SAndrew Thompson static void
ulpt_status_callback(struct usb_xfer * xfer,usb_error_t error)296ed6d949aSAndrew Thompson ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
29702ac6454SAndrew Thompson {
298ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usbd_xfer_softc(xfer);
299760bc48eSAndrew Thompson 	struct usb_device_request req;
300ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
30102ac6454SAndrew Thompson 	uint8_t cur_status;
30202ac6454SAndrew Thompson 	uint8_t new_status;
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
30502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
30602ac6454SAndrew Thompson 
307ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 1);
308ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, &cur_status, 1);
30902ac6454SAndrew Thompson 
31002ac6454SAndrew Thompson 		cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
31102ac6454SAndrew Thompson 		new_status = cur_status & ~sc->sc_last_status;
31202ac6454SAndrew Thompson 		sc->sc_last_status = cur_status;
31302ac6454SAndrew Thompson 
31402ac6454SAndrew Thompson 		if (new_status & LPS_SELECT)
31502ac6454SAndrew Thompson 			log(LOG_NOTICE, "%s: offline\n",
31602ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev));
31702ac6454SAndrew Thompson 		else if (new_status & LPS_NOPAPER)
31802ac6454SAndrew Thompson 			log(LOG_NOTICE, "%s: out of paper\n",
31902ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev));
32002ac6454SAndrew Thompson 		else if (new_status & LPS_NERR)
32102ac6454SAndrew Thompson 			log(LOG_NOTICE, "%s: output error\n",
32202ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev));
32302ac6454SAndrew Thompson 		break;
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson 	case USB_ST_SETUP:
32602ac6454SAndrew Thompson 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
32702ac6454SAndrew Thompson 		req.bRequest = UR_GET_PORT_STATUS;
32802ac6454SAndrew Thompson 		USETW(req.wValue, 0);
32902ac6454SAndrew Thompson 		req.wIndex[0] = sc->sc_iface_no;
33002ac6454SAndrew Thompson 		req.wIndex[1] = 0;
33102ac6454SAndrew Thompson 		USETW(req.wLength, 1);
33202ac6454SAndrew Thompson 
333ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
334ed6d949aSAndrew Thompson 		usbd_copy_in(pc, 0, &req, sizeof(req));
33502ac6454SAndrew Thompson 
336ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
337ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 1, 1);
338ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, 2);
339a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
34002ac6454SAndrew Thompson 		break;
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson 	default:			/* Error */
343ed6d949aSAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(error));
344ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
34502ac6454SAndrew Thompson 			/* wait for next watchdog timeout */
34602ac6454SAndrew Thompson 		}
34702ac6454SAndrew Thompson 		break;
34802ac6454SAndrew Thompson 	}
34902ac6454SAndrew Thompson }
35002ac6454SAndrew Thompson 
351760bc48eSAndrew Thompson static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
35202ac6454SAndrew Thompson 	[ULPT_BULK_DT_WR] = {
35302ac6454SAndrew Thompson 		.type = UE_BULK,
35402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
35502ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
3564eae601eSAndrew Thompson 		.bufsize = ULPT_BSIZE,
3574eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.proxy_buffer = 1},
3584eae601eSAndrew Thompson 		.callback = &ulpt_write_callback,
35902ac6454SAndrew Thompson 	},
36002ac6454SAndrew Thompson 
36102ac6454SAndrew Thompson 	[ULPT_BULK_DT_RD] = {
36202ac6454SAndrew Thompson 		.type = UE_BULK,
36302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
36402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
3654eae601eSAndrew Thompson 		.bufsize = ULPT_BSIZE,
3664eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
3674eae601eSAndrew Thompson 		.callback = &ulpt_read_callback,
36802ac6454SAndrew Thompson 	},
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 	[ULPT_INTR_DT_RD] = {
37102ac6454SAndrew Thompson 		.type = UE_CONTROL,
37202ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
37302ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
374760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request) + 1,
3754eae601eSAndrew Thompson 		.callback = &ulpt_status_callback,
3764eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
37702ac6454SAndrew Thompson 	},
37802ac6454SAndrew Thompson };
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson static void
ulpt_start_read(struct usb_fifo * fifo)381760bc48eSAndrew Thompson ulpt_start_read(struct usb_fifo *fifo)
38202ac6454SAndrew Thompson {
383ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
38402ac6454SAndrew Thompson 
385a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
38602ac6454SAndrew Thompson }
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson static void
ulpt_stop_read(struct usb_fifo * fifo)389760bc48eSAndrew Thompson ulpt_stop_read(struct usb_fifo *fifo)
39002ac6454SAndrew Thompson {
391ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
39202ac6454SAndrew Thompson 
393a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
39402ac6454SAndrew Thompson }
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson static void
ulpt_start_write(struct usb_fifo * fifo)397760bc48eSAndrew Thompson ulpt_start_write(struct usb_fifo *fifo)
39802ac6454SAndrew Thompson {
399ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
40002ac6454SAndrew Thompson 
401a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
40202ac6454SAndrew Thompson }
40302ac6454SAndrew Thompson 
40402ac6454SAndrew Thompson static void
ulpt_stop_write(struct usb_fifo * fifo)405760bc48eSAndrew Thompson ulpt_stop_write(struct usb_fifo *fifo)
40602ac6454SAndrew Thompson {
407ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
40802ac6454SAndrew Thompson 
409a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
41002ac6454SAndrew Thompson }
41102ac6454SAndrew Thompson 
41202ac6454SAndrew Thompson static int
ulpt_open(struct usb_fifo * fifo,int fflags)413760bc48eSAndrew Thompson ulpt_open(struct usb_fifo *fifo, int fflags)
41402ac6454SAndrew Thompson {
415ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson 	/* we assume that open is a serial process */
41802ac6454SAndrew Thompson 
41902ac6454SAndrew Thompson 	if (sc->sc_fflags == 0) {
42020733245SPedro F. Giffuni 		/* reset USB parallel port */
4214743e6daSAlfred Perlstein 
42202ac6454SAndrew Thompson 		ulpt_reset(sc);
42302ac6454SAndrew Thompson 	}
424ee3e3ff5SAndrew Thompson 	return (unlpt_open(fifo, fflags));
42502ac6454SAndrew Thompson }
42602ac6454SAndrew Thompson 
42702ac6454SAndrew Thompson static int
unlpt_open(struct usb_fifo * fifo,int fflags)428760bc48eSAndrew Thompson unlpt_open(struct usb_fifo *fifo, int fflags)
42902ac6454SAndrew Thompson {
430ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson 	if (sc->sc_fflags & fflags) {
43302ac6454SAndrew Thompson 		return (EBUSY);
43402ac6454SAndrew Thompson 	}
43502ac6454SAndrew Thompson 	if (fflags & FREAD) {
436ec97e9caSHans Petter Selasky 		/* clear stall first */
437ec97e9caSHans Petter Selasky 		mtx_lock(&sc->sc_mtx);
438ec97e9caSHans Petter Selasky 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
439ec97e9caSHans Petter Selasky 		mtx_unlock(&sc->sc_mtx);
440a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
441ed6d949aSAndrew Thompson 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
44202ac6454SAndrew Thompson 		    ULPT_IFQ_MAXLEN)) {
44302ac6454SAndrew Thompson 			return (ENOMEM);
44402ac6454SAndrew Thompson 		}
44502ac6454SAndrew Thompson 		/* set which FIFO is opened */
44602ac6454SAndrew Thompson 		sc->sc_fifo_open[USB_FIFO_RX] = fifo;
44702ac6454SAndrew Thompson 	}
44802ac6454SAndrew Thompson 	if (fflags & FWRITE) {
44902ac6454SAndrew Thompson 		/* clear stall first */
45002ac6454SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
451ec97e9caSHans Petter Selasky 		usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
45202ac6454SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
453a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
454ed6d949aSAndrew Thompson 		    usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
45502ac6454SAndrew Thompson 		    ULPT_IFQ_MAXLEN)) {
45602ac6454SAndrew Thompson 			return (ENOMEM);
45702ac6454SAndrew Thompson 		}
45802ac6454SAndrew Thompson 		/* set which FIFO is opened */
45902ac6454SAndrew Thompson 		sc->sc_fifo_open[USB_FIFO_TX] = fifo;
46002ac6454SAndrew Thompson 	}
46102ac6454SAndrew Thompson 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
46202ac6454SAndrew Thompson 	return (0);
46302ac6454SAndrew Thompson }
46402ac6454SAndrew Thompson 
46502ac6454SAndrew Thompson static void
ulpt_close(struct usb_fifo * fifo,int fflags)466760bc48eSAndrew Thompson ulpt_close(struct usb_fifo *fifo, int fflags)
46702ac6454SAndrew Thompson {
468ed6d949aSAndrew Thompson 	struct ulpt_softc *sc = usb_fifo_softc(fifo);
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 	if (fflags & (FREAD | FWRITE)) {
473a593f6b8SAndrew Thompson 		usb_fifo_free_buffer(fifo);
47402ac6454SAndrew Thompson 	}
47502ac6454SAndrew Thompson }
47602ac6454SAndrew Thompson 
47702ac6454SAndrew Thompson static int
ulpt_ioctl(struct usb_fifo * fifo,u_long cmd,void * data,int fflags)478760bc48eSAndrew Thompson ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
479ee3e3ff5SAndrew Thompson     int fflags)
48002ac6454SAndrew Thompson {
48102ac6454SAndrew Thompson 	return (ENODEV);
48202ac6454SAndrew Thompson }
48302ac6454SAndrew Thompson 
484f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID ulpt_devs[] = {
485f1a16106SHans Petter Selasky 	/* Uni-directional USB printer */
486f1a16106SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_PRINTER),
487f1a16106SHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
488f1a16106SHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
489f1a16106SHans Petter Selasky 
490f1a16106SHans Petter Selasky 	/* Bi-directional USB printer */
491f1a16106SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_PRINTER),
492f1a16106SHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
493f1a16106SHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
494f1a16106SHans Petter Selasky 
495f1a16106SHans Petter Selasky 	/* 1284 USB printer */
496f1a16106SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_PRINTER),
497f1a16106SHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
498f1a16106SHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
49988162f7aSHans Petter Selasky 
50088162f7aSHans Petter Selasky 	/* Epson printer */
50188162f7aSHans Petter Selasky 	{USB_VENDOR(USB_VENDOR_EPSON),
50288162f7aSHans Petter Selasky 	 USB_PRODUCT(USB_PRODUCT_EPSON_TMU220B),
50388162f7aSHans Petter Selasky 	 USB_IFACE_CLASS(UICLASS_VENDOR),
50488162f7aSHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_VENDOR),
50588162f7aSHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
506f1a16106SHans Petter Selasky };
507f1a16106SHans Petter Selasky 
50802ac6454SAndrew Thompson static int
ulpt_probe(device_t dev)50902ac6454SAndrew Thompson ulpt_probe(device_t dev)
51002ac6454SAndrew Thompson {
511760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
512f1a16106SHans Petter Selasky 	int error;
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
51502ac6454SAndrew Thompson 
516f1a16106SHans Petter Selasky 	if (uaa->usb_mode != USB_MODE_HOST)
51702ac6454SAndrew Thompson 		return (ENXIO);
518f1a16106SHans Petter Selasky 
519f1a16106SHans Petter Selasky 	error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
520f1a16106SHans Petter Selasky 	if (error)
521f1a16106SHans Petter Selasky 		return (error);
522f1a16106SHans Petter Selasky 
523f1a16106SHans Petter Selasky 	return (BUS_PROBE_GENERIC);
52402ac6454SAndrew Thompson }
52502ac6454SAndrew Thompson 
52602ac6454SAndrew Thompson static int
ulpt_attach(device_t dev)52702ac6454SAndrew Thompson ulpt_attach(device_t dev)
52802ac6454SAndrew Thompson {
529760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
53002ac6454SAndrew Thompson 	struct ulpt_softc *sc = device_get_softc(dev);
531760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
53202ac6454SAndrew Thompson 	int unit = device_get_unit(dev);
53302ac6454SAndrew Thompson 	int error;
53402ac6454SAndrew Thompson 	uint8_t iface_index = uaa->info.bIfaceIndex;
53502ac6454SAndrew Thompson 	uint8_t alt_index;
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson 	DPRINTFN(11, "sc=%p\n", sc);
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson 	sc->sc_dev = dev;
54002ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
54102ac6454SAndrew Thompson 
542a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
54302ac6454SAndrew Thompson 
54402ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
54502ac6454SAndrew Thompson 
546a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	/* search through all the descriptors looking for bidir mode */
54902ac6454SAndrew Thompson 
550a593f6b8SAndrew Thompson 	id = usbd_get_interface_descriptor(uaa->iface);
5516d917491SHans Petter Selasky 	alt_index = 0xFF;
55202ac6454SAndrew Thompson 	while (1) {
55302ac6454SAndrew Thompson 		if (id == NULL) {
55402ac6454SAndrew Thompson 			break;
55502ac6454SAndrew Thompson 		}
55602ac6454SAndrew Thompson 		if ((id->bDescriptorType == UDESC_INTERFACE) &&
55702ac6454SAndrew Thompson 		    (id->bLength >= sizeof(*id))) {
55802ac6454SAndrew Thompson 			if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
55902ac6454SAndrew Thompson 				break;
56002ac6454SAndrew Thompson 			} else {
56102ac6454SAndrew Thompson 				alt_index++;
56288162f7aSHans Petter Selasky 				if ((id->bInterfaceClass == UICLASS_PRINTER ||
56388162f7aSHans Petter Selasky 				     id->bInterfaceClass == UICLASS_VENDOR) &&
56488162f7aSHans Petter Selasky 				    (id->bInterfaceSubClass == UISUBCLASS_PRINTER ||
56588162f7aSHans Petter Selasky 				     id->bInterfaceSubClass == UISUBCLASS_VENDOR) &&
56602ac6454SAndrew Thompson 				    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
56702ac6454SAndrew Thompson 					goto found;
56802ac6454SAndrew Thompson 				}
56902ac6454SAndrew Thompson 			}
57002ac6454SAndrew Thompson 		}
571a593f6b8SAndrew Thompson 		id = (void *)usb_desc_foreach(
572a593f6b8SAndrew Thompson 		    usbd_get_config_descriptor(uaa->device), (void *)id);
57302ac6454SAndrew Thompson 	}
57402ac6454SAndrew Thompson 	goto detach;
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson found:
57702ac6454SAndrew Thompson 
57802ac6454SAndrew Thompson 	DPRINTF("setting alternate "
57902ac6454SAndrew Thompson 	    "config number: %d\n", alt_index);
58002ac6454SAndrew Thompson 
58102ac6454SAndrew Thompson 	if (alt_index) {
582a593f6b8SAndrew Thompson 		error = usbd_set_alt_interface_index
58302ac6454SAndrew Thompson 		    (uaa->device, iface_index, alt_index);
58402ac6454SAndrew Thompson 
58502ac6454SAndrew Thompson 		if (error) {
58602ac6454SAndrew Thompson 			DPRINTF("could not set alternate "
587a593f6b8SAndrew Thompson 			    "config, error=%s\n", usbd_errstr(error));
58802ac6454SAndrew Thompson 			goto detach;
58902ac6454SAndrew Thompson 		}
59002ac6454SAndrew Thompson 	}
59102ac6454SAndrew Thompson 	sc->sc_iface_no = id->bInterfaceNumber;
59202ac6454SAndrew Thompson 
593a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
59402ac6454SAndrew Thompson 	    sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
59502ac6454SAndrew Thompson 	    sc, &sc->sc_mtx);
59602ac6454SAndrew Thompson 	if (error) {
597a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(error));
59802ac6454SAndrew Thompson 		goto detach;
59902ac6454SAndrew Thompson 	}
60002ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "using bi-directional mode\n");
60102ac6454SAndrew Thompson 
60202ac6454SAndrew Thompson #if 0
60302ac6454SAndrew Thompson /*
60402ac6454SAndrew Thompson  * This code is disabled because for some mysterious reason it causes
60502ac6454SAndrew Thompson  * printing not to work.  But only sometimes, and mostly with
60602ac6454SAndrew Thompson  * UHCI and less often with OHCI.  *sigh*
60702ac6454SAndrew Thompson  */
60802ac6454SAndrew Thompson 	{
609a593f6b8SAndrew Thompson 		struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
610760bc48eSAndrew Thompson 		struct usb_device_request req;
61102ac6454SAndrew Thompson 		int len, alen;
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
61402ac6454SAndrew Thompson 		req.bRequest = UR_GET_DEVICE_ID;
61502ac6454SAndrew Thompson 		USETW(req.wValue, cd->bConfigurationValue);
61602ac6454SAndrew Thompson 		USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
61702ac6454SAndrew Thompson 		USETW(req.wLength, sizeof devinfo - 1);
618a593f6b8SAndrew Thompson 		error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
61902ac6454SAndrew Thompson 		    &alen, USB_DEFAULT_TIMEOUT);
62002ac6454SAndrew Thompson 		if (error) {
62102ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "cannot get device id\n");
62202ac6454SAndrew Thompson 		} else if (alen <= 2) {
62302ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "empty device id, no "
62402ac6454SAndrew Thompson 			    "printer connected?\n");
62502ac6454SAndrew Thompson 		} else {
62602ac6454SAndrew Thompson 			/* devinfo now contains an IEEE-1284 device ID */
62702ac6454SAndrew Thompson 			len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
62802ac6454SAndrew Thompson 			if (len > sizeof devinfo - 3)
62902ac6454SAndrew Thompson 				len = sizeof devinfo - 3;
63002ac6454SAndrew Thompson 			devinfo[len] = 0;
63102ac6454SAndrew Thompson 			printf("%s: device id <", device_get_nameunit(sc->sc_dev));
63202ac6454SAndrew Thompson 			ieee1284_print_id(devinfo + 2);
63302ac6454SAndrew Thompson 			printf(">\n");
63402ac6454SAndrew Thompson 		}
63502ac6454SAndrew Thompson 	}
63602ac6454SAndrew Thompson #endif
63702ac6454SAndrew Thompson 
638a593f6b8SAndrew Thompson 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
63902ac6454SAndrew Thompson 	    &ulpt_fifo_methods, &sc->sc_fifo,
6406d917491SHans Petter Selasky 	    unit, -1, uaa->info.bIfaceIndex,
641ee3e3ff5SAndrew Thompson 	    UID_ROOT, GID_OPERATOR, 0644);
64202ac6454SAndrew Thompson 	if (error) {
64302ac6454SAndrew Thompson 		goto detach;
64402ac6454SAndrew Thompson 	}
645a593f6b8SAndrew Thompson 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
64602ac6454SAndrew Thompson 	    &unlpt_fifo_methods, &sc->sc_fifo_noreset,
6476d917491SHans Petter Selasky 	    unit, -1, uaa->info.bIfaceIndex,
648ee3e3ff5SAndrew Thompson 	    UID_ROOT, GID_OPERATOR, 0644);
64902ac6454SAndrew Thompson 	if (error) {
65002ac6454SAndrew Thompson 		goto detach;
65102ac6454SAndrew Thompson 	}
65202ac6454SAndrew Thompson 	/* start reading of status */
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
65502ac6454SAndrew Thompson 	ulpt_watchdog(sc);
65602ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
65702ac6454SAndrew Thompson 	return (0);
65802ac6454SAndrew Thompson 
65902ac6454SAndrew Thompson detach:
66002ac6454SAndrew Thompson 	ulpt_detach(dev);
66102ac6454SAndrew Thompson 	return (ENOMEM);
66202ac6454SAndrew Thompson }
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson static int
ulpt_detach(device_t dev)66502ac6454SAndrew Thompson ulpt_detach(device_t dev)
66602ac6454SAndrew Thompson {
66702ac6454SAndrew Thompson 	struct ulpt_softc *sc = device_get_softc(dev);
66802ac6454SAndrew Thompson 
66902ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
67002ac6454SAndrew Thompson 
671a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo);
672a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo_noreset);
67302ac6454SAndrew Thompson 
67402ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
675a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_watchdog);
67602ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
67702ac6454SAndrew Thompson 
678a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
679a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_watchdog);
68002ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson 	return (0);
68302ac6454SAndrew Thompson }
68402ac6454SAndrew Thompson 
68502ac6454SAndrew Thompson #if 0
68602ac6454SAndrew Thompson /* XXX This does not belong here. */
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson /*
68902ac6454SAndrew Thompson  * Compare two strings until the second ends.
69002ac6454SAndrew Thompson  */
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson static uint8_t
69302ac6454SAndrew Thompson ieee1284_compare(const char *a, const char *b)
69402ac6454SAndrew Thompson {
69502ac6454SAndrew Thompson 	while (1) {
69602ac6454SAndrew Thompson 		if (*b == 0) {
69702ac6454SAndrew Thompson 			break;
69802ac6454SAndrew Thompson 		}
69902ac6454SAndrew Thompson 		if (*a != *b) {
70002ac6454SAndrew Thompson 			return 1;
70102ac6454SAndrew Thompson 		}
70202ac6454SAndrew Thompson 		b++;
70302ac6454SAndrew Thompson 		a++;
70402ac6454SAndrew Thompson 	}
70502ac6454SAndrew Thompson 	return 0;
70602ac6454SAndrew Thompson }
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson /*
70902ac6454SAndrew Thompson  * Print select parts of an IEEE 1284 device ID.
71002ac6454SAndrew Thompson  */
71102ac6454SAndrew Thompson void
71202ac6454SAndrew Thompson ieee1284_print_id(char *str)
71302ac6454SAndrew Thompson {
71402ac6454SAndrew Thompson 	char *p, *q;
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 	for (p = str - 1; p; p = strchr(p, ';')) {
71702ac6454SAndrew Thompson 		p++;			/* skip ';' */
71802ac6454SAndrew Thompson 		if (ieee1284_compare(p, "MFG:") == 0 ||
71902ac6454SAndrew Thompson 		    ieee1284_compare(p, "MANUFACTURER:") == 0 ||
72002ac6454SAndrew Thompson 		    ieee1284_compare(p, "MDL:") == 0 ||
72102ac6454SAndrew Thompson 		    ieee1284_compare(p, "MODEL:") == 0) {
72202ac6454SAndrew Thompson 			q = strchr(p, ';');
72302ac6454SAndrew Thompson 			if (q)
72402ac6454SAndrew Thompson 				printf("%.*s", (int)(q - p + 1), p);
72502ac6454SAndrew Thompson 		}
72602ac6454SAndrew Thompson 	}
72702ac6454SAndrew Thompson }
72802ac6454SAndrew Thompson 
72902ac6454SAndrew Thompson #endif
73002ac6454SAndrew Thompson 
73102ac6454SAndrew Thompson static void
ulpt_watchdog(void * arg)73202ac6454SAndrew Thompson ulpt_watchdog(void *arg)
73302ac6454SAndrew Thompson {
73402ac6454SAndrew Thompson 	struct ulpt_softc *sc = arg;
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson 	mtx_assert(&sc->sc_mtx, MA_OWNED);
73702ac6454SAndrew Thompson 
7384743e6daSAlfred Perlstein 	/*
7394743e6daSAlfred Perlstein 	 * Only read status while the device is not opened, due to
7404743e6daSAlfred Perlstein 	 * possible hardware or firmware bug in some printers.
7414743e6daSAlfred Perlstein 	 */
7424743e6daSAlfred Perlstein 	if (sc->sc_fflags == 0)
743a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
74402ac6454SAndrew Thompson 
745a593f6b8SAndrew Thompson 	usb_callout_reset(&sc->sc_watchdog,
74602ac6454SAndrew Thompson 	    hz, &ulpt_watchdog, sc);
74702ac6454SAndrew Thompson }
74802ac6454SAndrew Thompson 
74902ac6454SAndrew Thompson static device_method_t ulpt_methods[] = {
75002ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ulpt_probe),
75102ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ulpt_attach),
75202ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ulpt_detach),
7535805d178SHans Petter Selasky 	DEVMETHOD_END
75402ac6454SAndrew Thompson };
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson static driver_t ulpt_driver = {
75702ac6454SAndrew Thompson 	.name = "ulpt",
75802ac6454SAndrew Thompson 	.methods = ulpt_methods,
75902ac6454SAndrew Thompson 	.size = sizeof(struct ulpt_softc),
76002ac6454SAndrew Thompson };
76102ac6454SAndrew Thompson 
762bc9372d7SJohn Baldwin DRIVER_MODULE(ulpt, uhub, ulpt_driver, NULL, NULL);
76302ac6454SAndrew Thompson MODULE_DEPEND(ulpt, usb, 1, 1, 1);
764910cb8feSAndrew Thompson MODULE_VERSION(ulpt, 1);
765f809f280SWarner Losh USB_PNP_HOST_INFO(ulpt_devs);
766