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