xref: /freebsd/sys/dev/usb/input/uep.c (revision 9c916c627f1101d584cbf35444fe32a2bc16c07f)
1f25a8a01SGleb Smirnoff /*-
2f25a8a01SGleb Smirnoff  * Copyright 2010, Gleb Smirnoff <glebius@FreeBSD.org>
3f25a8a01SGleb Smirnoff  * All rights reserved.
4f25a8a01SGleb Smirnoff  *
5f25a8a01SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
6f25a8a01SGleb Smirnoff  * modification, are permitted provided that the following conditions
7f25a8a01SGleb Smirnoff  * are met:
8f25a8a01SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
9f25a8a01SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
10f25a8a01SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
11f25a8a01SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
12f25a8a01SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
13f25a8a01SGleb Smirnoff  *
14f25a8a01SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15f25a8a01SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16f25a8a01SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17f25a8a01SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18f25a8a01SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19f25a8a01SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20f25a8a01SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f25a8a01SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f25a8a01SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f25a8a01SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24f25a8a01SGleb Smirnoff  * SUCH DAMAGE.
25f25a8a01SGleb Smirnoff  *
26f25a8a01SGleb Smirnoff  * $FreeBSD$
27f25a8a01SGleb Smirnoff  */
28f25a8a01SGleb Smirnoff 
29f25a8a01SGleb Smirnoff /*
30f25a8a01SGleb Smirnoff  *  http://home.eeti.com.tw/web20/drivers/Software%20Programming%20Guide_v2.0.pdf
31f25a8a01SGleb Smirnoff  */
32f25a8a01SGleb Smirnoff 
33f25a8a01SGleb Smirnoff #include <sys/param.h>
34f25a8a01SGleb Smirnoff #include <sys/bus.h>
35f25a8a01SGleb Smirnoff #include <sys/callout.h>
36f25a8a01SGleb Smirnoff #include <sys/conf.h>
37f25a8a01SGleb Smirnoff #include <sys/kernel.h>
38f25a8a01SGleb Smirnoff #include <sys/lock.h>
39f25a8a01SGleb Smirnoff #include <sys/module.h>
40f25a8a01SGleb Smirnoff #include <sys/mutex.h>
41f25a8a01SGleb Smirnoff #include <sys/sysctl.h>
42f25a8a01SGleb Smirnoff #include <sys/systm.h>
43f25a8a01SGleb Smirnoff 
44f25a8a01SGleb Smirnoff #include <dev/usb/usb.h>
45f25a8a01SGleb Smirnoff #include <dev/usb/usbdi.h>
46f25a8a01SGleb Smirnoff #include <dev/usb/usbdi_util.h>
47f25a8a01SGleb Smirnoff #include <dev/usb/usbhid.h>
48f25a8a01SGleb Smirnoff #include "usbdevs.h"
49f25a8a01SGleb Smirnoff 
50f25a8a01SGleb Smirnoff #include <sys/ioccom.h>
51f25a8a01SGleb Smirnoff #include <sys/fcntl.h>
52f25a8a01SGleb Smirnoff #include <sys/tty.h>
53f25a8a01SGleb Smirnoff 
54f25a8a01SGleb Smirnoff #define USB_DEBUG_VAR uep_debug
55f25a8a01SGleb Smirnoff #include <dev/usb/usb_debug.h>
56f25a8a01SGleb Smirnoff 
57f25a8a01SGleb Smirnoff #ifdef USB_DEBUG
58f25a8a01SGleb Smirnoff static int uep_debug = 0;
59f25a8a01SGleb Smirnoff 
60f25a8a01SGleb Smirnoff SYSCTL_NODE(_hw_usb, OID_AUTO, uep, CTLFLAG_RW, 0, "USB uep");
61f25a8a01SGleb Smirnoff SYSCTL_INT(_hw_usb_uep, OID_AUTO, debug, CTLFLAG_RW,
62f25a8a01SGleb Smirnoff     &uep_debug, 0, "Debug level");
63f25a8a01SGleb Smirnoff #endif
64f25a8a01SGleb Smirnoff 
65f25a8a01SGleb Smirnoff #define UEP_MAX_X		2047
66f25a8a01SGleb Smirnoff #define UEP_MAX_Y		2047
67f25a8a01SGleb Smirnoff 
68f25a8a01SGleb Smirnoff #define UEP_DOWN		0x01
69f25a8a01SGleb Smirnoff #define UEP_PACKET_LEN_MAX	16
70f25a8a01SGleb Smirnoff #define UEP_PACKET_LEN_REPORT	5
71f25a8a01SGleb Smirnoff #define UEP_PACKET_LEN_REPORT2	6
72f25a8a01SGleb Smirnoff #define UEP_PACKET_DIAG		0x0a
73f25a8a01SGleb Smirnoff #define UEP_PACKET_REPORT_MASK		0xe0
74f25a8a01SGleb Smirnoff #define UEP_PACKET_REPORT		0x80
75f25a8a01SGleb Smirnoff #define UEP_PACKET_REPORT_PRESSURE	0xc0
76f25a8a01SGleb Smirnoff #define UEP_PACKET_REPORT_PLAYER	0xa0
77f25a8a01SGleb Smirnoff #define	UEP_PACKET_LEN_MASK
78f25a8a01SGleb Smirnoff 
79f25a8a01SGleb Smirnoff #define UEP_FIFO_BUF_SIZE	8	/* bytes */
80f25a8a01SGleb Smirnoff #define UEP_FIFO_QUEUE_MAXLEN	50	/* units */
81f25a8a01SGleb Smirnoff 
82f25a8a01SGleb Smirnoff enum {
83f25a8a01SGleb Smirnoff 	UEP_INTR_DT,
84f25a8a01SGleb Smirnoff 	UEP_N_TRANSFER,
85f25a8a01SGleb Smirnoff };
86f25a8a01SGleb Smirnoff 
87f25a8a01SGleb Smirnoff struct uep_softc {
88f25a8a01SGleb Smirnoff 	struct mtx mtx;
89f25a8a01SGleb Smirnoff 
90f25a8a01SGleb Smirnoff 	struct usb_xfer *xfer[UEP_N_TRANSFER];
91f25a8a01SGleb Smirnoff 	struct usb_fifo_sc fifo;
92f25a8a01SGleb Smirnoff 
93f25a8a01SGleb Smirnoff 	u_int		pollrate;
94f25a8a01SGleb Smirnoff 	u_int		state;
95f25a8a01SGleb Smirnoff #define UEP_ENABLED	0x01
96f25a8a01SGleb Smirnoff 
97f25a8a01SGleb Smirnoff 	/* Reassembling buffer. */
98f25a8a01SGleb Smirnoff 	u_char		buf[UEP_PACKET_LEN_MAX];
99f25a8a01SGleb Smirnoff 	uint8_t		buf_len;
100f25a8a01SGleb Smirnoff };
101f25a8a01SGleb Smirnoff 
102f25a8a01SGleb Smirnoff static usb_callback_t uep_intr_callback;
103f25a8a01SGleb Smirnoff 
104f25a8a01SGleb Smirnoff static device_probe_t	uep_probe;
105f25a8a01SGleb Smirnoff static device_attach_t	uep_attach;
106f25a8a01SGleb Smirnoff static device_detach_t	uep_detach;
107f25a8a01SGleb Smirnoff 
108f25a8a01SGleb Smirnoff static usb_fifo_cmd_t	uep_start_read;
109f25a8a01SGleb Smirnoff static usb_fifo_cmd_t	uep_stop_read;
110f25a8a01SGleb Smirnoff static usb_fifo_open_t	uep_open;
111f25a8a01SGleb Smirnoff static usb_fifo_close_t	uep_close;
112f25a8a01SGleb Smirnoff 
113f25a8a01SGleb Smirnoff static void uep_put_queue(struct uep_softc *, u_char *);
114f25a8a01SGleb Smirnoff 
115f25a8a01SGleb Smirnoff static struct usb_fifo_methods uep_fifo_methods = {
116f25a8a01SGleb Smirnoff 	.f_open = &uep_open,
117f25a8a01SGleb Smirnoff 	.f_close = &uep_close,
118f25a8a01SGleb Smirnoff 	.f_start_read = &uep_start_read,
119f25a8a01SGleb Smirnoff 	.f_stop_read = &uep_stop_read,
120f25a8a01SGleb Smirnoff 	.basename[0] = "uep",
121f25a8a01SGleb Smirnoff };
122f25a8a01SGleb Smirnoff 
123f25a8a01SGleb Smirnoff static int
124f25a8a01SGleb Smirnoff get_pkt_len(u_char *buf)
125f25a8a01SGleb Smirnoff {
126f25a8a01SGleb Smirnoff 	if (buf[0] == UEP_PACKET_DIAG) {
127f25a8a01SGleb Smirnoff 		int len;
128f25a8a01SGleb Smirnoff 
129f25a8a01SGleb Smirnoff 		len = buf[1] + 2;
130f25a8a01SGleb Smirnoff 		if (len > UEP_PACKET_LEN_MAX) {
131f25a8a01SGleb Smirnoff 			DPRINTF("bad packet len %u\n", len);
132f25a8a01SGleb Smirnoff 			return (UEP_PACKET_LEN_MAX);
133f25a8a01SGleb Smirnoff 		}
134f25a8a01SGleb Smirnoff 
135f25a8a01SGleb Smirnoff 		return (len);
136f25a8a01SGleb Smirnoff 	}
137f25a8a01SGleb Smirnoff 
138f25a8a01SGleb Smirnoff 	switch (buf[0] & UEP_PACKET_REPORT_MASK) {
139f25a8a01SGleb Smirnoff 	case UEP_PACKET_REPORT:
140f25a8a01SGleb Smirnoff 		return (UEP_PACKET_LEN_REPORT);
141f25a8a01SGleb Smirnoff 	case UEP_PACKET_REPORT_PRESSURE:
142f25a8a01SGleb Smirnoff 	case UEP_PACKET_REPORT_PLAYER:
143f25a8a01SGleb Smirnoff 	case UEP_PACKET_REPORT_PRESSURE | UEP_PACKET_REPORT_PLAYER:
144f25a8a01SGleb Smirnoff 		return (UEP_PACKET_LEN_REPORT2);
145f25a8a01SGleb Smirnoff 	default:
146f25a8a01SGleb Smirnoff 		DPRINTF("bad packet len 0\n");
147f25a8a01SGleb Smirnoff 		return (0);
148f25a8a01SGleb Smirnoff 	}
149f25a8a01SGleb Smirnoff }
150f25a8a01SGleb Smirnoff 
151f25a8a01SGleb Smirnoff static void
152f25a8a01SGleb Smirnoff uep_process_pkt(struct uep_softc *sc, u_char *buf)
153f25a8a01SGleb Smirnoff {
154f25a8a01SGleb Smirnoff 	int32_t x, y;
155f25a8a01SGleb Smirnoff 
156f25a8a01SGleb Smirnoff 	if ((buf[0] & 0xFE) != 0x80) {
157f25a8a01SGleb Smirnoff 		DPRINTF("bad input packet format 0x%.2x\n", buf[0]);
158f25a8a01SGleb Smirnoff 		return;
159f25a8a01SGleb Smirnoff 	}
160f25a8a01SGleb Smirnoff 
161f25a8a01SGleb Smirnoff 	/*
162f25a8a01SGleb Smirnoff 	 * Packet format is 5 bytes:
163f25a8a01SGleb Smirnoff 	 *
164f25a8a01SGleb Smirnoff 	 * 1000000T
165f25a8a01SGleb Smirnoff 	 * 0000AAAA
166f25a8a01SGleb Smirnoff 	 * 0AAAAAAA
167f25a8a01SGleb Smirnoff 	 * 0000BBBB
168f25a8a01SGleb Smirnoff 	 * 0BBBBBBB
169f25a8a01SGleb Smirnoff 	 *
170f25a8a01SGleb Smirnoff 	 * T: 1=touched 0=not touched
171f25a8a01SGleb Smirnoff 	 * A: bits of axis A position, MSB to LSB
172f25a8a01SGleb Smirnoff 	 * B: bits of axis B position, MSB to LSB
173f25a8a01SGleb Smirnoff 	 *
174f25a8a01SGleb Smirnoff 	 * For the unit I have, which is CTF1020-S from CarTFT.com,
175f25a8a01SGleb Smirnoff 	 * A = X and B = Y. But in NetBSD uep(4) it is other way round :)
176f25a8a01SGleb Smirnoff 	 *
177f25a8a01SGleb Smirnoff 	 * The controller sends a stream of T=1 events while the
178f25a8a01SGleb Smirnoff 	 * panel is touched, followed by a single T=0 event.
179f25a8a01SGleb Smirnoff 	 *
180f25a8a01SGleb Smirnoff 	 */
181f25a8a01SGleb Smirnoff 
182f25a8a01SGleb Smirnoff 	x = (buf[1] << 7) | buf[2];
183f25a8a01SGleb Smirnoff 	y = (buf[3] << 7) | buf[4];
184f25a8a01SGleb Smirnoff 
185f25a8a01SGleb Smirnoff 	DPRINTFN(2, "x %u y %u\n", x, y);
186f25a8a01SGleb Smirnoff 
187f25a8a01SGleb Smirnoff 	uep_put_queue(sc, buf);
188f25a8a01SGleb Smirnoff }
189f25a8a01SGleb Smirnoff 
190f25a8a01SGleb Smirnoff static void
191f25a8a01SGleb Smirnoff uep_intr_callback(struct usb_xfer *xfer, usb_error_t error)
192f25a8a01SGleb Smirnoff {
193f25a8a01SGleb Smirnoff 	struct uep_softc *sc = usbd_xfer_softc(xfer);
194f25a8a01SGleb Smirnoff 	int len;
195f25a8a01SGleb Smirnoff 
196f25a8a01SGleb Smirnoff 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
197f25a8a01SGleb Smirnoff 
198f25a8a01SGleb Smirnoff 	switch (USB_GET_STATE(xfer)) {
199f25a8a01SGleb Smirnoff 	case USB_ST_TRANSFERRED:
200f25a8a01SGleb Smirnoff 	    {
201f25a8a01SGleb Smirnoff 		struct usb_page_cache *pc;
202f25a8a01SGleb Smirnoff 		u_char buf[17], *p;
203f25a8a01SGleb Smirnoff 		int pkt_len;
204f25a8a01SGleb Smirnoff 
205f25a8a01SGleb Smirnoff 		if (len > sizeof(buf)) {
206f25a8a01SGleb Smirnoff 			DPRINTF("bad input length %d\n", len);
207f25a8a01SGleb Smirnoff 			goto tr_setup;
208f25a8a01SGleb Smirnoff 		}
209f25a8a01SGleb Smirnoff 
210f25a8a01SGleb Smirnoff 		pc = usbd_xfer_get_frame(xfer, 0);
211f25a8a01SGleb Smirnoff 		usbd_copy_out(pc, 0, buf, len);
212f25a8a01SGleb Smirnoff 
213f25a8a01SGleb Smirnoff 		/*
214f25a8a01SGleb Smirnoff 		 * The below code mimics Linux a lot. I don't know
215f25a8a01SGleb Smirnoff 		 * why NetBSD reads complete packets, but we need
216f25a8a01SGleb Smirnoff 		 * to reassamble 'em like Linux does (tries?).
217f25a8a01SGleb Smirnoff 		 */
218f25a8a01SGleb Smirnoff 		if (sc->buf_len > 0) {
219f25a8a01SGleb Smirnoff 			int res;
220f25a8a01SGleb Smirnoff 
221f25a8a01SGleb Smirnoff 			if (sc->buf_len == 1)
222f25a8a01SGleb Smirnoff 				sc->buf[1] = buf[0];
223f25a8a01SGleb Smirnoff 
224f25a8a01SGleb Smirnoff 			if ((pkt_len = get_pkt_len(sc->buf)) == 0)
225f25a8a01SGleb Smirnoff 				goto tr_setup;
226f25a8a01SGleb Smirnoff 
227f25a8a01SGleb Smirnoff 			res = pkt_len - sc->buf_len;
228f25a8a01SGleb Smirnoff 			memcpy(sc->buf + sc->buf_len, buf, res);
229f25a8a01SGleb Smirnoff 			uep_process_pkt(sc, sc->buf);
230f25a8a01SGleb Smirnoff 			sc->buf_len = 0;
231f25a8a01SGleb Smirnoff 
232f25a8a01SGleb Smirnoff 			p = buf + res;
233f25a8a01SGleb Smirnoff 			len -= res;
234f25a8a01SGleb Smirnoff 		} else
235f25a8a01SGleb Smirnoff 			p = buf;
236f25a8a01SGleb Smirnoff 
237f25a8a01SGleb Smirnoff 		if (len == 1) {
238f25a8a01SGleb Smirnoff 			sc->buf[0] = buf[0];
239f25a8a01SGleb Smirnoff 			sc->buf_len = 1;
240f25a8a01SGleb Smirnoff 
241f25a8a01SGleb Smirnoff 			goto tr_setup;
242f25a8a01SGleb Smirnoff 		}
243f25a8a01SGleb Smirnoff 
244f25a8a01SGleb Smirnoff 		while (len > 0) {
245f25a8a01SGleb Smirnoff 			if ((pkt_len = get_pkt_len(p)) == 0)
246f25a8a01SGleb Smirnoff 				goto tr_setup;
247f25a8a01SGleb Smirnoff 
248f25a8a01SGleb Smirnoff 			/* full packet: process */
249f25a8a01SGleb Smirnoff 			if (pkt_len <= len) {
250f25a8a01SGleb Smirnoff 				uep_process_pkt(sc, p);
251f25a8a01SGleb Smirnoff 			} else {
252f25a8a01SGleb Smirnoff 				/* incomplete packet: save in buffer */
253f25a8a01SGleb Smirnoff 				memcpy(sc->buf, p, len);
254f25a8a01SGleb Smirnoff 				sc->buf_len = len;
255f25a8a01SGleb Smirnoff 			}
256f25a8a01SGleb Smirnoff 			p += pkt_len;
257f25a8a01SGleb Smirnoff 			len -= pkt_len;
258f25a8a01SGleb Smirnoff 		}
259f25a8a01SGleb Smirnoff 	    }
260f25a8a01SGleb Smirnoff 	case USB_ST_SETUP:
261f25a8a01SGleb Smirnoff 	tr_setup:
262f25a8a01SGleb Smirnoff 		/* check if we can put more data into the FIFO */
263f25a8a01SGleb Smirnoff 		if (usb_fifo_put_bytes_max(sc->fifo.fp[USB_FIFO_RX]) != 0) {
264f25a8a01SGleb Smirnoff 			usbd_xfer_set_frame_len(xfer, 0,
265f25a8a01SGleb Smirnoff 			    usbd_xfer_max_len(xfer));
266f25a8a01SGleb Smirnoff 			usbd_transfer_submit(xfer);
267f25a8a01SGleb Smirnoff                 }
268f25a8a01SGleb Smirnoff 		break;
269f25a8a01SGleb Smirnoff 
270f25a8a01SGleb Smirnoff 	default:
271f25a8a01SGleb Smirnoff 		if (error != USB_ERR_CANCELLED) {
272f25a8a01SGleb Smirnoff 			/* try clear stall first */
273f25a8a01SGleb Smirnoff 			usbd_xfer_set_stall(xfer);
274f25a8a01SGleb Smirnoff 			goto tr_setup;
275f25a8a01SGleb Smirnoff 		}
276f25a8a01SGleb Smirnoff 		break;
277f25a8a01SGleb Smirnoff 	}
278f25a8a01SGleb Smirnoff }
279f25a8a01SGleb Smirnoff 
280f25a8a01SGleb Smirnoff static const struct usb_config uep_config[UEP_N_TRANSFER] = {
281f25a8a01SGleb Smirnoff 	[UEP_INTR_DT] = {
282f25a8a01SGleb Smirnoff 		.type = UE_INTERRUPT,
283f25a8a01SGleb Smirnoff 		.endpoint = UE_ADDR_ANY,
284f25a8a01SGleb Smirnoff 		.direction = UE_DIR_IN,
285f25a8a01SGleb Smirnoff 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
286f25a8a01SGleb Smirnoff 		.bufsize = 0,   /* use wMaxPacketSize */
287f25a8a01SGleb Smirnoff 		.callback = &uep_intr_callback,
288f25a8a01SGleb Smirnoff 	},
289f25a8a01SGleb Smirnoff };
290f25a8a01SGleb Smirnoff 
291*9c916c62SHans Petter Selasky static const STRUCT_USB_HOST_ID uep_devs[] = {
292*9c916c62SHans Petter Selasky 	{USB_VPI(USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL, 0)},
293*9c916c62SHans Petter Selasky 	{USB_VPI(USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL2, 0)},
294*9c916c62SHans Petter Selasky 	{USB_VPI(USB_VENDOR_EGALAX2, USB_PRODUCT_EGALAX2_TPANEL, 0)},
295*9c916c62SHans Petter Selasky };
296*9c916c62SHans Petter Selasky 
297f25a8a01SGleb Smirnoff static int
298f25a8a01SGleb Smirnoff uep_probe(device_t dev)
299f25a8a01SGleb Smirnoff {
300f25a8a01SGleb Smirnoff 	struct usb_attach_arg *uaa = device_get_ivars(dev);
301f25a8a01SGleb Smirnoff 
302f25a8a01SGleb Smirnoff 	if (uaa->usb_mode != USB_MODE_HOST)
303f25a8a01SGleb Smirnoff 		return (ENXIO);
304*9c916c62SHans Petter Selasky 	if (uaa->info.bConfigIndex != 0)
305f25a8a01SGleb Smirnoff 		return (ENXIO);
306*9c916c62SHans Petter Selasky 	if (uaa->info.bIfaceIndex != 0)
307*9c916c62SHans Petter Selasky 		return (ENXIO);
308*9c916c62SHans Petter Selasky 
309*9c916c62SHans Petter Selasky 	return (usbd_lookup_id_by_uaa(uep_devs, sizeof(uep_devs), uaa));
310f25a8a01SGleb Smirnoff }
311f25a8a01SGleb Smirnoff 
312f25a8a01SGleb Smirnoff static int
313f25a8a01SGleb Smirnoff uep_attach(device_t dev)
314f25a8a01SGleb Smirnoff {
315f25a8a01SGleb Smirnoff 	struct usb_attach_arg *uaa = device_get_ivars(dev);
316f25a8a01SGleb Smirnoff 	struct uep_softc *sc = device_get_softc(dev);
317f25a8a01SGleb Smirnoff 	int error;
318f25a8a01SGleb Smirnoff 
319f25a8a01SGleb Smirnoff 	device_set_usb_desc(dev);
320f25a8a01SGleb Smirnoff 
321f25a8a01SGleb Smirnoff 	mtx_init(&sc->mtx, "uep lock", NULL, MTX_DEF);
322f25a8a01SGleb Smirnoff 
323f25a8a01SGleb Smirnoff 	error = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
324f25a8a01SGleb Smirnoff 	    sc->xfer, uep_config, UEP_N_TRANSFER, sc, &sc->mtx);
325f25a8a01SGleb Smirnoff 
326f25a8a01SGleb Smirnoff 	if (error) {
327f25a8a01SGleb Smirnoff 		DPRINTF("usbd_transfer_setup error=%s\n", usbd_errstr(error));
328f25a8a01SGleb Smirnoff 		goto detach;
329f25a8a01SGleb Smirnoff 	}
330f25a8a01SGleb Smirnoff 
331f25a8a01SGleb Smirnoff 	error = usb_fifo_attach(uaa->device, sc, &sc->mtx, &uep_fifo_methods,
332f25a8a01SGleb Smirnoff 	    &sc->fifo, device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
333f25a8a01SGleb Smirnoff 	    UID_ROOT, GID_OPERATOR, 0644);
334f25a8a01SGleb Smirnoff 
335f25a8a01SGleb Smirnoff         if (error) {
336f25a8a01SGleb Smirnoff 		DPRINTF("usb_fifo_attach error=%s\n", usbd_errstr(error));
337f25a8a01SGleb Smirnoff                 goto detach;
338f25a8a01SGleb Smirnoff         }
339f25a8a01SGleb Smirnoff 
340f25a8a01SGleb Smirnoff 	sc->buf_len = 0;
341f25a8a01SGleb Smirnoff 
342f25a8a01SGleb Smirnoff 	return (0);
343f25a8a01SGleb Smirnoff 
344f25a8a01SGleb Smirnoff detach:
345f25a8a01SGleb Smirnoff 	uep_detach(dev);
346f25a8a01SGleb Smirnoff 
347f25a8a01SGleb Smirnoff 	return (ENOMEM); /* XXX */
348f25a8a01SGleb Smirnoff }
349f25a8a01SGleb Smirnoff 
350f25a8a01SGleb Smirnoff static int
351f25a8a01SGleb Smirnoff uep_detach(device_t dev)
352f25a8a01SGleb Smirnoff {
353f25a8a01SGleb Smirnoff 	struct uep_softc *sc = device_get_softc(dev);
354f25a8a01SGleb Smirnoff 
355f25a8a01SGleb Smirnoff 	usb_fifo_detach(&sc->fifo);
356f25a8a01SGleb Smirnoff 
357f25a8a01SGleb Smirnoff 	usbd_transfer_unsetup(sc->xfer, UEP_N_TRANSFER);
358f25a8a01SGleb Smirnoff 
359f25a8a01SGleb Smirnoff 	mtx_destroy(&sc->mtx);
360f25a8a01SGleb Smirnoff 
361f25a8a01SGleb Smirnoff 	return (0);
362f25a8a01SGleb Smirnoff }
363f25a8a01SGleb Smirnoff 
364f25a8a01SGleb Smirnoff static void
365f25a8a01SGleb Smirnoff uep_start_read(struct usb_fifo *fifo)
366f25a8a01SGleb Smirnoff {
367f25a8a01SGleb Smirnoff 	struct uep_softc *sc = usb_fifo_softc(fifo);
368f25a8a01SGleb Smirnoff 	u_int rate;
369f25a8a01SGleb Smirnoff 
370f25a8a01SGleb Smirnoff 	if ((rate = sc->pollrate) > 1000)
371f25a8a01SGleb Smirnoff 		rate = 1000;
372f25a8a01SGleb Smirnoff 
373f25a8a01SGleb Smirnoff 	if (rate > 0 && sc->xfer[UEP_INTR_DT] != NULL) {
374f25a8a01SGleb Smirnoff 		usbd_transfer_stop(sc->xfer[UEP_INTR_DT]);
375f25a8a01SGleb Smirnoff 		usbd_xfer_set_interval(sc->xfer[UEP_INTR_DT], 1000 / rate);
376f25a8a01SGleb Smirnoff 		sc->pollrate = 0;
377f25a8a01SGleb Smirnoff 	}
378f25a8a01SGleb Smirnoff 
379f25a8a01SGleb Smirnoff 	usbd_transfer_start(sc->xfer[UEP_INTR_DT]);
380f25a8a01SGleb Smirnoff }
381f25a8a01SGleb Smirnoff 
382f25a8a01SGleb Smirnoff static void
383f25a8a01SGleb Smirnoff uep_stop_read(struct usb_fifo *fifo)
384f25a8a01SGleb Smirnoff {
385f25a8a01SGleb Smirnoff 	struct uep_softc *sc = usb_fifo_softc(fifo);
386f25a8a01SGleb Smirnoff 
387f25a8a01SGleb Smirnoff 	usbd_transfer_stop(sc->xfer[UEP_INTR_DT]);
388f25a8a01SGleb Smirnoff }
389f25a8a01SGleb Smirnoff 
390f25a8a01SGleb Smirnoff static void
391f25a8a01SGleb Smirnoff uep_put_queue(struct uep_softc *sc, u_char *buf)
392f25a8a01SGleb Smirnoff {
393f25a8a01SGleb Smirnoff 	usb_fifo_put_data_linear(sc->fifo.fp[USB_FIFO_RX], buf,
394f25a8a01SGleb Smirnoff 	    UEP_PACKET_LEN_REPORT, 1);
395f25a8a01SGleb Smirnoff }
396f25a8a01SGleb Smirnoff 
397f25a8a01SGleb Smirnoff static int
398f25a8a01SGleb Smirnoff uep_open(struct usb_fifo *fifo, int fflags)
399f25a8a01SGleb Smirnoff {
400f25a8a01SGleb Smirnoff 	if (fflags & FREAD) {
401f25a8a01SGleb Smirnoff 		struct uep_softc *sc = usb_fifo_softc(fifo);
402f25a8a01SGleb Smirnoff 
403f25a8a01SGleb Smirnoff 		if (sc->state & UEP_ENABLED)
404f25a8a01SGleb Smirnoff 			return (EBUSY);
405f25a8a01SGleb Smirnoff 		if (usb_fifo_alloc_buffer(fifo, UEP_FIFO_BUF_SIZE,
406f25a8a01SGleb Smirnoff 		    UEP_FIFO_QUEUE_MAXLEN))
407f25a8a01SGleb Smirnoff 			return (ENOMEM);
408f25a8a01SGleb Smirnoff 
409f25a8a01SGleb Smirnoff 		sc->state |= UEP_ENABLED;
410f25a8a01SGleb Smirnoff 	}
411f25a8a01SGleb Smirnoff 
412f25a8a01SGleb Smirnoff 	return (0);
413f25a8a01SGleb Smirnoff }
414f25a8a01SGleb Smirnoff 
415f25a8a01SGleb Smirnoff static void
416f25a8a01SGleb Smirnoff uep_close(struct usb_fifo *fifo, int fflags)
417f25a8a01SGleb Smirnoff {
418f25a8a01SGleb Smirnoff 	if (fflags & FREAD) {
419f25a8a01SGleb Smirnoff 		struct uep_softc *sc = usb_fifo_softc(fifo);
420f25a8a01SGleb Smirnoff 
421f25a8a01SGleb Smirnoff 		sc->state &= ~(UEP_ENABLED);
422f25a8a01SGleb Smirnoff 		usb_fifo_free_buffer(fifo);
423f25a8a01SGleb Smirnoff 	}
424f25a8a01SGleb Smirnoff }
425f25a8a01SGleb Smirnoff 
426f25a8a01SGleb Smirnoff static devclass_t uep_devclass;
427f25a8a01SGleb Smirnoff 
428f25a8a01SGleb Smirnoff static device_method_t uep_methods[] = {
429f25a8a01SGleb Smirnoff 	DEVMETHOD(device_probe, uep_probe),
430f25a8a01SGleb Smirnoff        	DEVMETHOD(device_attach, uep_attach),
431f25a8a01SGleb Smirnoff 	DEVMETHOD(device_detach, uep_detach),
432f25a8a01SGleb Smirnoff 	{ 0, 0 },
433f25a8a01SGleb Smirnoff };
434f25a8a01SGleb Smirnoff 
435f25a8a01SGleb Smirnoff static driver_t uep_driver = {
436f25a8a01SGleb Smirnoff 	.name = "uep",
437f25a8a01SGleb Smirnoff 	.methods = uep_methods,
438f25a8a01SGleb Smirnoff 	.size = sizeof(struct uep_softc),
439f25a8a01SGleb Smirnoff };
440f25a8a01SGleb Smirnoff 
441f25a8a01SGleb Smirnoff DRIVER_MODULE(uep, uhub, uep_driver, uep_devclass, NULL, NULL);
442f25a8a01SGleb Smirnoff MODULE_DEPEND(uep, usb, 1, 1, 1);
443910cb8feSAndrew Thompson MODULE_VERSION(uep, 1);
444