1f25a8a01SGleb Smirnoff /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro 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
29f25a8a01SGleb Smirnoff /*
30e626c40eSKevin Lo * http://www.eeti.com.tw/pdf/Software%20Programming%20Guide_v2.0.pdf
31f25a8a01SGleb Smirnoff */
32f25a8a01SGleb Smirnoff
3326f3e847SVladimir Kondratyev #include "opt_evdev.h"
3426f3e847SVladimir Kondratyev
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
5226f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
5326f3e847SVladimir Kondratyev #include <dev/evdev/input.h>
5426f3e847SVladimir Kondratyev #include <dev/evdev/evdev.h>
5526f3e847SVladimir Kondratyev #else
56f25a8a01SGleb Smirnoff #include <sys/ioccom.h>
57f25a8a01SGleb Smirnoff #include <sys/fcntl.h>
5826f3e847SVladimir Kondratyev #endif
59f25a8a01SGleb Smirnoff
60f25a8a01SGleb Smirnoff #define USB_DEBUG_VAR uep_debug
61f25a8a01SGleb Smirnoff #include <dev/usb/usb_debug.h>
62f25a8a01SGleb Smirnoff
63f25a8a01SGleb Smirnoff #ifdef USB_DEBUG
64f25a8a01SGleb Smirnoff static int uep_debug = 0;
65f25a8a01SGleb Smirnoff
66f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
67f8d2b1f3SPawel Biernacki "USB uep");
68ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uep, OID_AUTO, debug, CTLFLAG_RWTUN,
69f25a8a01SGleb Smirnoff &uep_debug, 0, "Debug level");
70f25a8a01SGleb Smirnoff #endif
71f25a8a01SGleb Smirnoff
72f25a8a01SGleb Smirnoff #define UEP_MAX_X 2047
73f25a8a01SGleb Smirnoff #define UEP_MAX_Y 2047
74f25a8a01SGleb Smirnoff
75f25a8a01SGleb Smirnoff #define UEP_DOWN 0x01
76f25a8a01SGleb Smirnoff #define UEP_PACKET_LEN_MAX 16
77f25a8a01SGleb Smirnoff #define UEP_PACKET_LEN_REPORT 5
78f25a8a01SGleb Smirnoff #define UEP_PACKET_LEN_REPORT2 6
79f25a8a01SGleb Smirnoff #define UEP_PACKET_DIAG 0x0a
80f25a8a01SGleb Smirnoff #define UEP_PACKET_REPORT_MASK 0xe0
81f25a8a01SGleb Smirnoff #define UEP_PACKET_REPORT 0x80
82f25a8a01SGleb Smirnoff #define UEP_PACKET_REPORT_PRESSURE 0xc0
83f25a8a01SGleb Smirnoff #define UEP_PACKET_REPORT_PLAYER 0xa0
84f25a8a01SGleb Smirnoff #define UEP_PACKET_LEN_MASK
85f25a8a01SGleb Smirnoff
86f25a8a01SGleb Smirnoff #define UEP_FIFO_BUF_SIZE 8 /* bytes */
87f25a8a01SGleb Smirnoff #define UEP_FIFO_QUEUE_MAXLEN 50 /* units */
88f25a8a01SGleb Smirnoff
89f25a8a01SGleb Smirnoff enum {
90f25a8a01SGleb Smirnoff UEP_INTR_DT,
91f25a8a01SGleb Smirnoff UEP_N_TRANSFER,
92f25a8a01SGleb Smirnoff };
93f25a8a01SGleb Smirnoff
94f25a8a01SGleb Smirnoff struct uep_softc {
95f25a8a01SGleb Smirnoff struct mtx mtx;
96f25a8a01SGleb Smirnoff
97f25a8a01SGleb Smirnoff struct usb_xfer *xfer[UEP_N_TRANSFER];
9826f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
9926f3e847SVladimir Kondratyev struct evdev_dev *evdev;
10026f3e847SVladimir Kondratyev #else
101f25a8a01SGleb Smirnoff struct usb_fifo_sc fifo;
102f25a8a01SGleb Smirnoff
103f25a8a01SGleb Smirnoff u_int pollrate;
104f25a8a01SGleb Smirnoff u_int state;
105f25a8a01SGleb Smirnoff #define UEP_ENABLED 0x01
10626f3e847SVladimir Kondratyev #endif
107f25a8a01SGleb Smirnoff
108f25a8a01SGleb Smirnoff /* Reassembling buffer. */
109f25a8a01SGleb Smirnoff u_char buf[UEP_PACKET_LEN_MAX];
110f25a8a01SGleb Smirnoff uint8_t buf_len;
111f25a8a01SGleb Smirnoff };
112f25a8a01SGleb Smirnoff
113f25a8a01SGleb Smirnoff static usb_callback_t uep_intr_callback;
114f25a8a01SGleb Smirnoff
115f25a8a01SGleb Smirnoff static device_probe_t uep_probe;
116f25a8a01SGleb Smirnoff static device_attach_t uep_attach;
117f25a8a01SGleb Smirnoff static device_detach_t uep_detach;
118f25a8a01SGleb Smirnoff
11926f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
12026f3e847SVladimir Kondratyev
12126f3e847SVladimir Kondratyev static evdev_open_t uep_ev_open;
12226f3e847SVladimir Kondratyev static evdev_close_t uep_ev_close;
12326f3e847SVladimir Kondratyev
12426f3e847SVladimir Kondratyev static const struct evdev_methods uep_evdev_methods = {
12526f3e847SVladimir Kondratyev .ev_open = &uep_ev_open,
12626f3e847SVladimir Kondratyev .ev_close = &uep_ev_close,
12726f3e847SVladimir Kondratyev };
12826f3e847SVladimir Kondratyev
12926f3e847SVladimir Kondratyev #else /* !EVDEV_SUPPORT */
13026f3e847SVladimir Kondratyev
131f25a8a01SGleb Smirnoff static usb_fifo_cmd_t uep_start_read;
132f25a8a01SGleb Smirnoff static usb_fifo_cmd_t uep_stop_read;
133f25a8a01SGleb Smirnoff static usb_fifo_open_t uep_open;
134f25a8a01SGleb Smirnoff static usb_fifo_close_t uep_close;
135f25a8a01SGleb Smirnoff
136f25a8a01SGleb Smirnoff static void uep_put_queue(struct uep_softc *, u_char *);
137f25a8a01SGleb Smirnoff
138f25a8a01SGleb Smirnoff static struct usb_fifo_methods uep_fifo_methods = {
139f25a8a01SGleb Smirnoff .f_open = &uep_open,
140f25a8a01SGleb Smirnoff .f_close = &uep_close,
141f25a8a01SGleb Smirnoff .f_start_read = &uep_start_read,
142f25a8a01SGleb Smirnoff .f_stop_read = &uep_stop_read,
143f25a8a01SGleb Smirnoff .basename[0] = "uep",
144f25a8a01SGleb Smirnoff };
14526f3e847SVladimir Kondratyev #endif /* !EVDEV_SUPPORT */
146f25a8a01SGleb Smirnoff
147f25a8a01SGleb Smirnoff static int
get_pkt_len(u_char * buf)148f25a8a01SGleb Smirnoff get_pkt_len(u_char *buf)
149f25a8a01SGleb Smirnoff {
150f25a8a01SGleb Smirnoff if (buf[0] == UEP_PACKET_DIAG) {
151f25a8a01SGleb Smirnoff int len;
152f25a8a01SGleb Smirnoff
153f25a8a01SGleb Smirnoff len = buf[1] + 2;
154f25a8a01SGleb Smirnoff if (len > UEP_PACKET_LEN_MAX) {
155f25a8a01SGleb Smirnoff DPRINTF("bad packet len %u\n", len);
156f25a8a01SGleb Smirnoff return (UEP_PACKET_LEN_MAX);
157f25a8a01SGleb Smirnoff }
158f25a8a01SGleb Smirnoff
159f25a8a01SGleb Smirnoff return (len);
160f25a8a01SGleb Smirnoff }
161f25a8a01SGleb Smirnoff
162f25a8a01SGleb Smirnoff switch (buf[0] & UEP_PACKET_REPORT_MASK) {
163f25a8a01SGleb Smirnoff case UEP_PACKET_REPORT:
164f25a8a01SGleb Smirnoff return (UEP_PACKET_LEN_REPORT);
165f25a8a01SGleb Smirnoff case UEP_PACKET_REPORT_PRESSURE:
166f25a8a01SGleb Smirnoff case UEP_PACKET_REPORT_PLAYER:
167f25a8a01SGleb Smirnoff case UEP_PACKET_REPORT_PRESSURE | UEP_PACKET_REPORT_PLAYER:
168f25a8a01SGleb Smirnoff return (UEP_PACKET_LEN_REPORT2);
169f25a8a01SGleb Smirnoff default:
170f25a8a01SGleb Smirnoff DPRINTF("bad packet len 0\n");
171f25a8a01SGleb Smirnoff return (0);
172f25a8a01SGleb Smirnoff }
173f25a8a01SGleb Smirnoff }
174f25a8a01SGleb Smirnoff
175f25a8a01SGleb Smirnoff static void
uep_process_pkt(struct uep_softc * sc,u_char * buf)176f25a8a01SGleb Smirnoff uep_process_pkt(struct uep_softc *sc, u_char *buf)
177f25a8a01SGleb Smirnoff {
178ffd8101eSJohn Baldwin int32_t x __usbdebug_used, y __usbdebug_used;
17926f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
18026f3e847SVladimir Kondratyev int touch;
18126f3e847SVladimir Kondratyev #endif
182f25a8a01SGleb Smirnoff
183f25a8a01SGleb Smirnoff if ((buf[0] & 0xFE) != 0x80) {
184f25a8a01SGleb Smirnoff DPRINTF("bad input packet format 0x%.2x\n", buf[0]);
185f25a8a01SGleb Smirnoff return;
186f25a8a01SGleb Smirnoff }
187f25a8a01SGleb Smirnoff
188f25a8a01SGleb Smirnoff /*
189f25a8a01SGleb Smirnoff * Packet format is 5 bytes:
190f25a8a01SGleb Smirnoff *
191f25a8a01SGleb Smirnoff * 1000000T
192f25a8a01SGleb Smirnoff * 0000AAAA
193f25a8a01SGleb Smirnoff * 0AAAAAAA
194f25a8a01SGleb Smirnoff * 0000BBBB
195f25a8a01SGleb Smirnoff * 0BBBBBBB
196f25a8a01SGleb Smirnoff *
197f25a8a01SGleb Smirnoff * T: 1=touched 0=not touched
198f25a8a01SGleb Smirnoff * A: bits of axis A position, MSB to LSB
199f25a8a01SGleb Smirnoff * B: bits of axis B position, MSB to LSB
200f25a8a01SGleb Smirnoff *
201f25a8a01SGleb Smirnoff * For the unit I have, which is CTF1020-S from CarTFT.com,
202f25a8a01SGleb Smirnoff * A = X and B = Y. But in NetBSD uep(4) it is other way round :)
203f25a8a01SGleb Smirnoff *
204f25a8a01SGleb Smirnoff * The controller sends a stream of T=1 events while the
205f25a8a01SGleb Smirnoff * panel is touched, followed by a single T=0 event.
206f25a8a01SGleb Smirnoff *
207f25a8a01SGleb Smirnoff */
208f25a8a01SGleb Smirnoff
209f25a8a01SGleb Smirnoff x = (buf[1] << 7) | buf[2];
210f25a8a01SGleb Smirnoff y = (buf[3] << 7) | buf[4];
211f25a8a01SGleb Smirnoff
212f25a8a01SGleb Smirnoff DPRINTFN(2, "x %u y %u\n", x, y);
213f25a8a01SGleb Smirnoff
21426f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
21526f3e847SVladimir Kondratyev touch = buf[0] & (1 << 0);
21626f3e847SVladimir Kondratyev if (touch) {
21726f3e847SVladimir Kondratyev evdev_push_abs(sc->evdev, ABS_X, x);
21826f3e847SVladimir Kondratyev evdev_push_abs(sc->evdev, ABS_Y, y);
21926f3e847SVladimir Kondratyev }
22026f3e847SVladimir Kondratyev evdev_push_key(sc->evdev, BTN_TOUCH, touch);
22126f3e847SVladimir Kondratyev evdev_sync(sc->evdev);
22226f3e847SVladimir Kondratyev #else
223f25a8a01SGleb Smirnoff uep_put_queue(sc, buf);
22426f3e847SVladimir Kondratyev #endif
225f25a8a01SGleb Smirnoff }
226f25a8a01SGleb Smirnoff
227f25a8a01SGleb Smirnoff static void
uep_intr_callback(struct usb_xfer * xfer,usb_error_t error)228f25a8a01SGleb Smirnoff uep_intr_callback(struct usb_xfer *xfer, usb_error_t error)
229f25a8a01SGleb Smirnoff {
230f25a8a01SGleb Smirnoff struct uep_softc *sc = usbd_xfer_softc(xfer);
231f25a8a01SGleb Smirnoff int len;
232f25a8a01SGleb Smirnoff
233f25a8a01SGleb Smirnoff usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
234f25a8a01SGleb Smirnoff
235f25a8a01SGleb Smirnoff switch (USB_GET_STATE(xfer)) {
236f25a8a01SGleb Smirnoff case USB_ST_TRANSFERRED:
237f25a8a01SGleb Smirnoff {
238f25a8a01SGleb Smirnoff struct usb_page_cache *pc;
239f25a8a01SGleb Smirnoff u_char buf[17], *p;
240f25a8a01SGleb Smirnoff int pkt_len;
241f25a8a01SGleb Smirnoff
2426d917491SHans Petter Selasky if (len > (int)sizeof(buf)) {
243f25a8a01SGleb Smirnoff DPRINTF("bad input length %d\n", len);
244f25a8a01SGleb Smirnoff goto tr_setup;
245f25a8a01SGleb Smirnoff }
246f25a8a01SGleb Smirnoff
247f25a8a01SGleb Smirnoff pc = usbd_xfer_get_frame(xfer, 0);
248f25a8a01SGleb Smirnoff usbd_copy_out(pc, 0, buf, len);
249f25a8a01SGleb Smirnoff
250f25a8a01SGleb Smirnoff /*
251f25a8a01SGleb Smirnoff * The below code mimics Linux a lot. I don't know
252f25a8a01SGleb Smirnoff * why NetBSD reads complete packets, but we need
253f25a8a01SGleb Smirnoff * to reassamble 'em like Linux does (tries?).
254f25a8a01SGleb Smirnoff */
255f25a8a01SGleb Smirnoff if (sc->buf_len > 0) {
256f25a8a01SGleb Smirnoff int res;
257f25a8a01SGleb Smirnoff
258f25a8a01SGleb Smirnoff if (sc->buf_len == 1)
259f25a8a01SGleb Smirnoff sc->buf[1] = buf[0];
260f25a8a01SGleb Smirnoff
261f25a8a01SGleb Smirnoff if ((pkt_len = get_pkt_len(sc->buf)) == 0)
262f25a8a01SGleb Smirnoff goto tr_setup;
263f25a8a01SGleb Smirnoff
264f25a8a01SGleb Smirnoff res = pkt_len - sc->buf_len;
265f25a8a01SGleb Smirnoff memcpy(sc->buf + sc->buf_len, buf, res);
266f25a8a01SGleb Smirnoff uep_process_pkt(sc, sc->buf);
267f25a8a01SGleb Smirnoff sc->buf_len = 0;
268f25a8a01SGleb Smirnoff
269f25a8a01SGleb Smirnoff p = buf + res;
270f25a8a01SGleb Smirnoff len -= res;
271f25a8a01SGleb Smirnoff } else
272f25a8a01SGleb Smirnoff p = buf;
273f25a8a01SGleb Smirnoff
274f25a8a01SGleb Smirnoff if (len == 1) {
275f25a8a01SGleb Smirnoff sc->buf[0] = buf[0];
276f25a8a01SGleb Smirnoff sc->buf_len = 1;
277f25a8a01SGleb Smirnoff
278f25a8a01SGleb Smirnoff goto tr_setup;
279f25a8a01SGleb Smirnoff }
280f25a8a01SGleb Smirnoff
281f25a8a01SGleb Smirnoff while (len > 0) {
282f25a8a01SGleb Smirnoff if ((pkt_len = get_pkt_len(p)) == 0)
283f25a8a01SGleb Smirnoff goto tr_setup;
284f25a8a01SGleb Smirnoff
285f25a8a01SGleb Smirnoff /* full packet: process */
286f25a8a01SGleb Smirnoff if (pkt_len <= len) {
287f25a8a01SGleb Smirnoff uep_process_pkt(sc, p);
288f25a8a01SGleb Smirnoff } else {
289f25a8a01SGleb Smirnoff /* incomplete packet: save in buffer */
290f25a8a01SGleb Smirnoff memcpy(sc->buf, p, len);
291f25a8a01SGleb Smirnoff sc->buf_len = len;
292f25a8a01SGleb Smirnoff }
293f25a8a01SGleb Smirnoff p += pkt_len;
294f25a8a01SGleb Smirnoff len -= pkt_len;
295f25a8a01SGleb Smirnoff }
296f25a8a01SGleb Smirnoff }
297f25a8a01SGleb Smirnoff case USB_ST_SETUP:
298f25a8a01SGleb Smirnoff tr_setup:
29926f3e847SVladimir Kondratyev #ifndef EVDEV_SUPPORT
300f25a8a01SGleb Smirnoff /* check if we can put more data into the FIFO */
30126f3e847SVladimir Kondratyev if (usb_fifo_put_bytes_max(sc->fifo.fp[USB_FIFO_RX]) == 0)
30226f3e847SVladimir Kondratyev break;
30326f3e847SVladimir Kondratyev #endif
30426f3e847SVladimir Kondratyev usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
305f25a8a01SGleb Smirnoff usbd_transfer_submit(xfer);
306f25a8a01SGleb Smirnoff break;
307f25a8a01SGleb Smirnoff
308f25a8a01SGleb Smirnoff default:
309f25a8a01SGleb Smirnoff if (error != USB_ERR_CANCELLED) {
310f25a8a01SGleb Smirnoff /* try clear stall first */
311f25a8a01SGleb Smirnoff usbd_xfer_set_stall(xfer);
312f25a8a01SGleb Smirnoff goto tr_setup;
313f25a8a01SGleb Smirnoff }
314f25a8a01SGleb Smirnoff break;
315f25a8a01SGleb Smirnoff }
316f25a8a01SGleb Smirnoff }
317f25a8a01SGleb Smirnoff
318f25a8a01SGleb Smirnoff static const struct usb_config uep_config[UEP_N_TRANSFER] = {
319f25a8a01SGleb Smirnoff [UEP_INTR_DT] = {
320f25a8a01SGleb Smirnoff .type = UE_INTERRUPT,
321f25a8a01SGleb Smirnoff .endpoint = UE_ADDR_ANY,
322f25a8a01SGleb Smirnoff .direction = UE_DIR_IN,
323f25a8a01SGleb Smirnoff .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
324f25a8a01SGleb Smirnoff .bufsize = 0, /* use wMaxPacketSize */
325f25a8a01SGleb Smirnoff .callback = &uep_intr_callback,
326f25a8a01SGleb Smirnoff },
327f25a8a01SGleb Smirnoff };
328f25a8a01SGleb Smirnoff
3299c916c62SHans Petter Selasky static const STRUCT_USB_HOST_ID uep_devs[] = {
3309c916c62SHans Petter Selasky {USB_VPI(USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL, 0)},
3319c916c62SHans Petter Selasky {USB_VPI(USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL2, 0)},
3329c916c62SHans Petter Selasky {USB_VPI(USB_VENDOR_EGALAX2, USB_PRODUCT_EGALAX2_TPANEL, 0)},
3339c916c62SHans Petter Selasky };
3349c916c62SHans Petter Selasky
335f25a8a01SGleb Smirnoff static int
uep_probe(device_t dev)336f25a8a01SGleb Smirnoff uep_probe(device_t dev)
337f25a8a01SGleb Smirnoff {
338f25a8a01SGleb Smirnoff struct usb_attach_arg *uaa = device_get_ivars(dev);
339f25a8a01SGleb Smirnoff
340f25a8a01SGleb Smirnoff if (uaa->usb_mode != USB_MODE_HOST)
341f25a8a01SGleb Smirnoff return (ENXIO);
3429c916c62SHans Petter Selasky if (uaa->info.bConfigIndex != 0)
343f25a8a01SGleb Smirnoff return (ENXIO);
3449c916c62SHans Petter Selasky if (uaa->info.bIfaceIndex != 0)
3459c916c62SHans Petter Selasky return (ENXIO);
3469c916c62SHans Petter Selasky
3479c916c62SHans Petter Selasky return (usbd_lookup_id_by_uaa(uep_devs, sizeof(uep_devs), uaa));
348f25a8a01SGleb Smirnoff }
349f25a8a01SGleb Smirnoff
350f25a8a01SGleb Smirnoff static int
uep_attach(device_t dev)351f25a8a01SGleb Smirnoff uep_attach(device_t dev)
352f25a8a01SGleb Smirnoff {
353f25a8a01SGleb Smirnoff struct usb_attach_arg *uaa = device_get_ivars(dev);
354f25a8a01SGleb Smirnoff struct uep_softc *sc = device_get_softc(dev);
355f25a8a01SGleb Smirnoff int error;
356f25a8a01SGleb Smirnoff
357f25a8a01SGleb Smirnoff device_set_usb_desc(dev);
358f25a8a01SGleb Smirnoff
359f25a8a01SGleb Smirnoff mtx_init(&sc->mtx, "uep lock", NULL, MTX_DEF);
360f25a8a01SGleb Smirnoff
361f25a8a01SGleb Smirnoff error = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
362f25a8a01SGleb Smirnoff sc->xfer, uep_config, UEP_N_TRANSFER, sc, &sc->mtx);
363f25a8a01SGleb Smirnoff
364f25a8a01SGleb Smirnoff if (error) {
365f25a8a01SGleb Smirnoff DPRINTF("usbd_transfer_setup error=%s\n", usbd_errstr(error));
366f25a8a01SGleb Smirnoff goto detach;
367f25a8a01SGleb Smirnoff }
368f25a8a01SGleb Smirnoff
36926f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
37026f3e847SVladimir Kondratyev sc->evdev = evdev_alloc();
37126f3e847SVladimir Kondratyev evdev_set_name(sc->evdev, device_get_desc(dev));
37226f3e847SVladimir Kondratyev evdev_set_phys(sc->evdev, device_get_nameunit(dev));
37326f3e847SVladimir Kondratyev evdev_set_id(sc->evdev, BUS_USB, uaa->info.idVendor,
37426f3e847SVladimir Kondratyev uaa->info.idProduct, 0);
37526f3e847SVladimir Kondratyev evdev_set_serial(sc->evdev, usb_get_serial(uaa->device));
37626f3e847SVladimir Kondratyev evdev_set_methods(sc->evdev, sc, &uep_evdev_methods);
37726f3e847SVladimir Kondratyev evdev_support_prop(sc->evdev, INPUT_PROP_DIRECT);
37826f3e847SVladimir Kondratyev evdev_support_event(sc->evdev, EV_SYN);
37926f3e847SVladimir Kondratyev evdev_support_event(sc->evdev, EV_ABS);
38026f3e847SVladimir Kondratyev evdev_support_event(sc->evdev, EV_KEY);
38126f3e847SVladimir Kondratyev evdev_support_key(sc->evdev, BTN_TOUCH);
3825af73ad5SVladimir Kondratyev evdev_support_abs(sc->evdev, ABS_X, 0, UEP_MAX_X, 0, 0, 0);
3835af73ad5SVladimir Kondratyev evdev_support_abs(sc->evdev, ABS_Y, 0, UEP_MAX_Y, 0, 0, 0);
38426f3e847SVladimir Kondratyev
38526f3e847SVladimir Kondratyev error = evdev_register_mtx(sc->evdev, &sc->mtx);
38626f3e847SVladimir Kondratyev if (error) {
38726f3e847SVladimir Kondratyev DPRINTF("evdev_register_mtx error=%s\n", usbd_errstr(error));
38826f3e847SVladimir Kondratyev goto detach;
38926f3e847SVladimir Kondratyev }
39026f3e847SVladimir Kondratyev #else /* !EVDEV_SUPPORT */
391f25a8a01SGleb Smirnoff error = usb_fifo_attach(uaa->device, sc, &sc->mtx, &uep_fifo_methods,
3926d917491SHans Petter Selasky &sc->fifo, device_get_unit(dev), -1, uaa->info.bIfaceIndex,
393f25a8a01SGleb Smirnoff UID_ROOT, GID_OPERATOR, 0644);
394f25a8a01SGleb Smirnoff
395f25a8a01SGleb Smirnoff if (error) {
396f25a8a01SGleb Smirnoff DPRINTF("usb_fifo_attach error=%s\n", usbd_errstr(error));
397f25a8a01SGleb Smirnoff goto detach;
398f25a8a01SGleb Smirnoff }
39926f3e847SVladimir Kondratyev #endif /* !EVDEV_SUPPORT */
400f25a8a01SGleb Smirnoff
401f25a8a01SGleb Smirnoff sc->buf_len = 0;
402f25a8a01SGleb Smirnoff
403f25a8a01SGleb Smirnoff return (0);
404f25a8a01SGleb Smirnoff
405f25a8a01SGleb Smirnoff detach:
406f25a8a01SGleb Smirnoff uep_detach(dev);
407f25a8a01SGleb Smirnoff
408f25a8a01SGleb Smirnoff return (ENOMEM); /* XXX */
409f25a8a01SGleb Smirnoff }
410f25a8a01SGleb Smirnoff
411f25a8a01SGleb Smirnoff static int
uep_detach(device_t dev)412f25a8a01SGleb Smirnoff uep_detach(device_t dev)
413f25a8a01SGleb Smirnoff {
414f25a8a01SGleb Smirnoff struct uep_softc *sc = device_get_softc(dev);
415f25a8a01SGleb Smirnoff
41626f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
41726f3e847SVladimir Kondratyev evdev_free(sc->evdev);
41826f3e847SVladimir Kondratyev #else
419f25a8a01SGleb Smirnoff usb_fifo_detach(&sc->fifo);
42026f3e847SVladimir Kondratyev #endif
421f25a8a01SGleb Smirnoff
422f25a8a01SGleb Smirnoff usbd_transfer_unsetup(sc->xfer, UEP_N_TRANSFER);
423f25a8a01SGleb Smirnoff
424f25a8a01SGleb Smirnoff mtx_destroy(&sc->mtx);
425f25a8a01SGleb Smirnoff
426f25a8a01SGleb Smirnoff return (0);
427f25a8a01SGleb Smirnoff }
428f25a8a01SGleb Smirnoff
42926f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
43026f3e847SVladimir Kondratyev
431911aed94SVladimir Kondratyev static int
uep_ev_close(struct evdev_dev * evdev)432911aed94SVladimir Kondratyev uep_ev_close(struct evdev_dev *evdev)
43326f3e847SVladimir Kondratyev {
434911aed94SVladimir Kondratyev struct uep_softc *sc = evdev_get_softc(evdev);
43526f3e847SVladimir Kondratyev
43626f3e847SVladimir Kondratyev mtx_assert(&sc->mtx, MA_OWNED);
43726f3e847SVladimir Kondratyev usbd_transfer_stop(sc->xfer[UEP_INTR_DT]);
438911aed94SVladimir Kondratyev
439911aed94SVladimir Kondratyev return (0);
44026f3e847SVladimir Kondratyev }
44126f3e847SVladimir Kondratyev
44226f3e847SVladimir Kondratyev static int
uep_ev_open(struct evdev_dev * evdev)443911aed94SVladimir Kondratyev uep_ev_open(struct evdev_dev *evdev)
44426f3e847SVladimir Kondratyev {
445911aed94SVladimir Kondratyev struct uep_softc *sc = evdev_get_softc(evdev);
44626f3e847SVladimir Kondratyev
44726f3e847SVladimir Kondratyev mtx_assert(&sc->mtx, MA_OWNED);
44826f3e847SVladimir Kondratyev usbd_transfer_start(sc->xfer[UEP_INTR_DT]);
44926f3e847SVladimir Kondratyev
45026f3e847SVladimir Kondratyev return (0);
45126f3e847SVladimir Kondratyev }
45226f3e847SVladimir Kondratyev
45326f3e847SVladimir Kondratyev #else /* !EVDEV_SUPPORT */
45426f3e847SVladimir Kondratyev
455f25a8a01SGleb Smirnoff static void
uep_start_read(struct usb_fifo * fifo)456f25a8a01SGleb Smirnoff uep_start_read(struct usb_fifo *fifo)
457f25a8a01SGleb Smirnoff {
458f25a8a01SGleb Smirnoff struct uep_softc *sc = usb_fifo_softc(fifo);
459f25a8a01SGleb Smirnoff u_int rate;
460f25a8a01SGleb Smirnoff
461f25a8a01SGleb Smirnoff if ((rate = sc->pollrate) > 1000)
462f25a8a01SGleb Smirnoff rate = 1000;
463f25a8a01SGleb Smirnoff
464f25a8a01SGleb Smirnoff if (rate > 0 && sc->xfer[UEP_INTR_DT] != NULL) {
465f25a8a01SGleb Smirnoff usbd_transfer_stop(sc->xfer[UEP_INTR_DT]);
466f25a8a01SGleb Smirnoff usbd_xfer_set_interval(sc->xfer[UEP_INTR_DT], 1000 / rate);
467f25a8a01SGleb Smirnoff sc->pollrate = 0;
468f25a8a01SGleb Smirnoff }
469f25a8a01SGleb Smirnoff
470f25a8a01SGleb Smirnoff usbd_transfer_start(sc->xfer[UEP_INTR_DT]);
471f25a8a01SGleb Smirnoff }
472f25a8a01SGleb Smirnoff
473f25a8a01SGleb Smirnoff static void
uep_stop_read(struct usb_fifo * fifo)474f25a8a01SGleb Smirnoff uep_stop_read(struct usb_fifo *fifo)
475f25a8a01SGleb Smirnoff {
476f25a8a01SGleb Smirnoff struct uep_softc *sc = usb_fifo_softc(fifo);
477f25a8a01SGleb Smirnoff
478f25a8a01SGleb Smirnoff usbd_transfer_stop(sc->xfer[UEP_INTR_DT]);
479f25a8a01SGleb Smirnoff }
480f25a8a01SGleb Smirnoff
481f25a8a01SGleb Smirnoff static void
uep_put_queue(struct uep_softc * sc,u_char * buf)482f25a8a01SGleb Smirnoff uep_put_queue(struct uep_softc *sc, u_char *buf)
483f25a8a01SGleb Smirnoff {
484f25a8a01SGleb Smirnoff usb_fifo_put_data_linear(sc->fifo.fp[USB_FIFO_RX], buf,
485f25a8a01SGleb Smirnoff UEP_PACKET_LEN_REPORT, 1);
486f25a8a01SGleb Smirnoff }
487f25a8a01SGleb Smirnoff
488f25a8a01SGleb Smirnoff static int
uep_open(struct usb_fifo * fifo,int fflags)489f25a8a01SGleb Smirnoff uep_open(struct usb_fifo *fifo, int fflags)
490f25a8a01SGleb Smirnoff {
491f25a8a01SGleb Smirnoff if (fflags & FREAD) {
492f25a8a01SGleb Smirnoff struct uep_softc *sc = usb_fifo_softc(fifo);
493f25a8a01SGleb Smirnoff
494f25a8a01SGleb Smirnoff if (sc->state & UEP_ENABLED)
495f25a8a01SGleb Smirnoff return (EBUSY);
496f25a8a01SGleb Smirnoff if (usb_fifo_alloc_buffer(fifo, UEP_FIFO_BUF_SIZE,
497f25a8a01SGleb Smirnoff UEP_FIFO_QUEUE_MAXLEN))
498f25a8a01SGleb Smirnoff return (ENOMEM);
499f25a8a01SGleb Smirnoff
500f25a8a01SGleb Smirnoff sc->state |= UEP_ENABLED;
501f25a8a01SGleb Smirnoff }
502f25a8a01SGleb Smirnoff
503f25a8a01SGleb Smirnoff return (0);
504f25a8a01SGleb Smirnoff }
505f25a8a01SGleb Smirnoff
506f25a8a01SGleb Smirnoff static void
uep_close(struct usb_fifo * fifo,int fflags)507f25a8a01SGleb Smirnoff uep_close(struct usb_fifo *fifo, int fflags)
508f25a8a01SGleb Smirnoff {
509f25a8a01SGleb Smirnoff if (fflags & FREAD) {
510f25a8a01SGleb Smirnoff struct uep_softc *sc = usb_fifo_softc(fifo);
511f25a8a01SGleb Smirnoff
512f25a8a01SGleb Smirnoff sc->state &= ~(UEP_ENABLED);
513f25a8a01SGleb Smirnoff usb_fifo_free_buffer(fifo);
514f25a8a01SGleb Smirnoff }
515f25a8a01SGleb Smirnoff }
51626f3e847SVladimir Kondratyev #endif /* !EVDEV_SUPPORT */
517f25a8a01SGleb Smirnoff
518f25a8a01SGleb Smirnoff static device_method_t uep_methods[] = {
519f25a8a01SGleb Smirnoff DEVMETHOD(device_probe, uep_probe),
520f25a8a01SGleb Smirnoff DEVMETHOD(device_attach, uep_attach),
521f25a8a01SGleb Smirnoff DEVMETHOD(device_detach, uep_detach),
522f25a8a01SGleb Smirnoff { 0, 0 },
523f25a8a01SGleb Smirnoff };
524f25a8a01SGleb Smirnoff
525f25a8a01SGleb Smirnoff static driver_t uep_driver = {
526f25a8a01SGleb Smirnoff .name = "uep",
527f25a8a01SGleb Smirnoff .methods = uep_methods,
528f25a8a01SGleb Smirnoff .size = sizeof(struct uep_softc),
529f25a8a01SGleb Smirnoff };
530f25a8a01SGleb Smirnoff
531bc9372d7SJohn Baldwin DRIVER_MODULE(uep, uhub, uep_driver, NULL, NULL);
532f25a8a01SGleb Smirnoff MODULE_DEPEND(uep, usb, 1, 1, 1);
53326f3e847SVladimir Kondratyev #ifdef EVDEV_SUPPORT
53426f3e847SVladimir Kondratyev MODULE_DEPEND(uep, evdev, 1, 1, 1);
53526f3e847SVladimir Kondratyev #endif
536910cb8feSAndrew Thompson MODULE_VERSION(uep, 1);
537f809f280SWarner Losh USB_PNP_HOST_INFO(uep_devs);
538