Deleted Added
full compact
ums.c (188942) ums.c (188981)
1/*-
2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (lennart@augustsson.net) at
7 * Carlstedt Research & Technology.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (lennart@augustsson.net) at
7 * Carlstedt Research & Technology.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/dev/usb/input/ums.c 188942 2009-02-23 18:31:00Z thompsa $");
39__FBSDID("$FreeBSD: head/sys/dev/usb/input/ums.c 188981 2009-02-24 03:34:05Z thompsa $");
40
41/*
42 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
43 */
44
45#include "usbdevs.h"
46#include <dev/usb/usb.h>
47#include <dev/usb/usb_mfunc.h>
48#include <dev/usb/usb_error.h>
49#include <dev/usb/usbhid.h>
50
51#define USB_DEBUG_VAR ums_debug
52
53#include <dev/usb/usb_core.h>
54#include <dev/usb/usb_util.h>
55#include <dev/usb/usb_debug.h>
56#include <dev/usb/usb_busdma.h>
57#include <dev/usb/usb_process.h>
58#include <dev/usb/usb_transfer.h>
59#include <dev/usb/usb_request.h>
60#include <dev/usb/usb_dynamic.h>
61#include <dev/usb/usb_mbuf.h>
62#include <dev/usb/usb_dev.h>
63#include <dev/usb/usb_hid.h>
64
65#include <dev/usb/quirk/usb_quirk.h>
66
67#include <sys/ioccom.h>
68#include <sys/filio.h>
69#include <sys/tty.h>
70#include <sys/mouse.h>
71
72#if USB_DEBUG
73static int ums_debug = 0;
74
75SYSCTL_NODE(_hw_usb2, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
76SYSCTL_INT(_hw_usb2_ums, OID_AUTO, debug, CTLFLAG_RW,
77 &ums_debug, 0, "Debug level");
78#endif
79
80#define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
81#define MOUSE_FLAGS (HIO_RELATIVE)
82
83#define UMS_BUF_SIZE 8 /* bytes */
84#define UMS_IFQ_MAXLEN 50 /* units */
85#define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
86#define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
87
88enum {
89 UMS_INTR_DT,
40
41/*
42 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
43 */
44
45#include "usbdevs.h"
46#include <dev/usb/usb.h>
47#include <dev/usb/usb_mfunc.h>
48#include <dev/usb/usb_error.h>
49#include <dev/usb/usbhid.h>
50
51#define USB_DEBUG_VAR ums_debug
52
53#include <dev/usb/usb_core.h>
54#include <dev/usb/usb_util.h>
55#include <dev/usb/usb_debug.h>
56#include <dev/usb/usb_busdma.h>
57#include <dev/usb/usb_process.h>
58#include <dev/usb/usb_transfer.h>
59#include <dev/usb/usb_request.h>
60#include <dev/usb/usb_dynamic.h>
61#include <dev/usb/usb_mbuf.h>
62#include <dev/usb/usb_dev.h>
63#include <dev/usb/usb_hid.h>
64
65#include <dev/usb/quirk/usb_quirk.h>
66
67#include <sys/ioccom.h>
68#include <sys/filio.h>
69#include <sys/tty.h>
70#include <sys/mouse.h>
71
72#if USB_DEBUG
73static int ums_debug = 0;
74
75SYSCTL_NODE(_hw_usb2, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
76SYSCTL_INT(_hw_usb2_ums, OID_AUTO, debug, CTLFLAG_RW,
77 &ums_debug, 0, "Debug level");
78#endif
79
80#define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
81#define MOUSE_FLAGS (HIO_RELATIVE)
82
83#define UMS_BUF_SIZE 8 /* bytes */
84#define UMS_IFQ_MAXLEN 50 /* units */
85#define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
86#define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
87
88enum {
89 UMS_INTR_DT,
90 UMS_INTR_CS,
91 UMS_N_TRANSFER = 2,
90 UMS_N_TRANSFER,
92};
93
94struct ums_softc {
95 struct usb2_fifo_sc sc_fifo;
96 struct mtx sc_mtx;
97 struct usb2_callout sc_callout;
98 struct hid_location sc_loc_w;
99 struct hid_location sc_loc_x;
100 struct hid_location sc_loc_y;
101 struct hid_location sc_loc_z;
102 struct hid_location sc_loc_t;
103 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
104 mousehw_t sc_hw;
105 mousemode_t sc_mode;
106 mousestatus_t sc_status;
107
108 struct usb2_xfer *sc_xfer[UMS_N_TRANSFER];
109
110 uint32_t sc_flags;
111#define UMS_FLAG_X_AXIS 0x0001
112#define UMS_FLAG_Y_AXIS 0x0002
113#define UMS_FLAG_Z_AXIS 0x0004
114#define UMS_FLAG_T_AXIS 0x0008
115#define UMS_FLAG_SBU 0x0010 /* spurious button up events */
91};
92
93struct ums_softc {
94 struct usb2_fifo_sc sc_fifo;
95 struct mtx sc_mtx;
96 struct usb2_callout sc_callout;
97 struct hid_location sc_loc_w;
98 struct hid_location sc_loc_x;
99 struct hid_location sc_loc_y;
100 struct hid_location sc_loc_z;
101 struct hid_location sc_loc_t;
102 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
103 mousehw_t sc_hw;
104 mousemode_t sc_mode;
105 mousestatus_t sc_status;
106
107 struct usb2_xfer *sc_xfer[UMS_N_TRANSFER];
108
109 uint32_t sc_flags;
110#define UMS_FLAG_X_AXIS 0x0001
111#define UMS_FLAG_Y_AXIS 0x0002
112#define UMS_FLAG_Z_AXIS 0x0004
113#define UMS_FLAG_T_AXIS 0x0008
114#define UMS_FLAG_SBU 0x0010 /* spurious button up events */
116#define UMS_FLAG_INTR_STALL 0x0020 /* set if transfer error */
117#define UMS_FLAG_REVZ 0x0040 /* Z-axis is reversed */
118#define UMS_FLAG_W_AXIS 0x0080
115#define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
116#define UMS_FLAG_W_AXIS 0x0040
119
120 uint8_t sc_buttons;
121 uint8_t sc_iid;
122 uint8_t sc_temp[64];
123};
124
125static void ums_put_queue_timeout(void *__sc);
126
117
118 uint8_t sc_buttons;
119 uint8_t sc_iid;
120 uint8_t sc_temp[64];
121};
122
123static void ums_put_queue_timeout(void *__sc);
124
127static usb2_callback_t ums_clear_stall_callback;
128static usb2_callback_t ums_intr_callback;
129
130static device_probe_t ums_probe;
131static device_attach_t ums_attach;
132static device_detach_t ums_detach;
133
134static usb2_fifo_cmd_t ums_start_read;
135static usb2_fifo_cmd_t ums_stop_read;
136static usb2_fifo_open_t ums_open;
137static usb2_fifo_close_t ums_close;
138static usb2_fifo_ioctl_t ums_ioctl;
139
140static void ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, int32_t dz, int32_t dt, int32_t buttons);
141
142static struct usb2_fifo_methods ums_fifo_methods = {
143 .f_open = &ums_open,
144 .f_close = &ums_close,
145 .f_ioctl = &ums_ioctl,
146 .f_start_read = &ums_start_read,
147 .f_stop_read = &ums_stop_read,
148 .basename[0] = "ums",
149};
150
151static void
152ums_put_queue_timeout(void *__sc)
153{
154 struct ums_softc *sc = __sc;
155
156 mtx_assert(&sc->sc_mtx, MA_OWNED);
157
158 ums_put_queue(sc, 0, 0, 0, 0, 0);
159}
160
161static void
125static usb2_callback_t ums_intr_callback;
126
127static device_probe_t ums_probe;
128static device_attach_t ums_attach;
129static device_detach_t ums_detach;
130
131static usb2_fifo_cmd_t ums_start_read;
132static usb2_fifo_cmd_t ums_stop_read;
133static usb2_fifo_open_t ums_open;
134static usb2_fifo_close_t ums_close;
135static usb2_fifo_ioctl_t ums_ioctl;
136
137static void ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, int32_t dz, int32_t dt, int32_t buttons);
138
139static struct usb2_fifo_methods ums_fifo_methods = {
140 .f_open = &ums_open,
141 .f_close = &ums_close,
142 .f_ioctl = &ums_ioctl,
143 .f_start_read = &ums_start_read,
144 .f_stop_read = &ums_stop_read,
145 .basename[0] = "ums",
146};
147
148static void
149ums_put_queue_timeout(void *__sc)
150{
151 struct ums_softc *sc = __sc;
152
153 mtx_assert(&sc->sc_mtx, MA_OWNED);
154
155 ums_put_queue(sc, 0, 0, 0, 0, 0);
156}
157
158static void
162ums_clear_stall_callback(struct usb2_xfer *xfer)
163{
164 struct ums_softc *sc = xfer->priv_sc;
165 struct usb2_xfer *xfer_other = sc->sc_xfer[UMS_INTR_DT];
166
167 if (usb2_clear_stall_callback(xfer, xfer_other)) {
168 DPRINTF("stall cleared\n");
169 sc->sc_flags &= ~UMS_FLAG_INTR_STALL;
170 usb2_transfer_start(xfer_other);
171 }
172}
173
174static void
175ums_intr_callback(struct usb2_xfer *xfer)
176{
177 struct ums_softc *sc = xfer->priv_sc;
178 uint8_t *buf = sc->sc_temp;
179 uint16_t len = xfer->actlen;
180 int32_t buttons = 0;
181 int32_t dw;
182 int32_t dx;
183 int32_t dy;
184 int32_t dz;
185 int32_t dt;
186 uint8_t i;
187
188 switch (USB_GET_STATE(xfer)) {
189 case USB_ST_TRANSFERRED:
190 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
191
192 if (len > sizeof(sc->sc_temp)) {
193 DPRINTFN(6, "truncating large packet to %zu bytes\n",
194 sizeof(sc->sc_temp));
195 len = sizeof(sc->sc_temp);
196 }
159ums_intr_callback(struct usb2_xfer *xfer)
160{
161 struct ums_softc *sc = xfer->priv_sc;
162 uint8_t *buf = sc->sc_temp;
163 uint16_t len = xfer->actlen;
164 int32_t buttons = 0;
165 int32_t dw;
166 int32_t dx;
167 int32_t dy;
168 int32_t dz;
169 int32_t dt;
170 uint8_t i;
171
172 switch (USB_GET_STATE(xfer)) {
173 case USB_ST_TRANSFERRED:
174 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
175
176 if (len > sizeof(sc->sc_temp)) {
177 DPRINTFN(6, "truncating large packet to %zu bytes\n",
178 sizeof(sc->sc_temp));
179 len = sizeof(sc->sc_temp);
180 }
197 if (len == 0) {
181 if (len == 0)
198 goto tr_setup;
182 goto tr_setup;
199 }
183
200 usb2_copy_out(xfer->frbuffers, 0, buf, len);
201
202 DPRINTFN(6, "data = %02x %02x %02x %02x "
203 "%02x %02x %02x %02x\n",
204 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
205 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
206 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
207 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
208
209 /*
184 usb2_copy_out(xfer->frbuffers, 0, buf, len);
185
186 DPRINTFN(6, "data = %02x %02x %02x %02x "
187 "%02x %02x %02x %02x\n",
188 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
189 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
190 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
191 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
192
193 /*
210 * The M$ Wireless Intellimouse 2.0 sends 1 extra leading byte
211 * of data compared to most USB mice. This byte frequently
212 * switches from 0x01 (usual state) to 0x02. I assume it is to
213 * allow extra, non-standard, reporting (say battery-life).
194 * The M$ Wireless Intellimouse 2.0 sends 1 extra
195 * leading byte of data compared to most USB
196 * mice. This byte frequently switches from 0x01
197 * (usual state) to 0x02. I assume it is to allow
198 * extra, non-standard, reporting (say battery-life).
214 *
199 *
215 * However at the same time it generates a left-click message
216 * on the button byte which causes spurious left-click's where
217 * there shouldn't be. This should sort that. Currently it's
218 * the only user of UMS_FLAG_T_AXIS so use it as an
219 * identifier.
200 * However at the same time it generates a left-click
201 * message on the button byte which causes spurious
202 * left-click's where there shouldn't be. This should
203 * sort that. Currently it's the only user of
204 * UMS_FLAG_T_AXIS so use it as an identifier.
220 *
221 *
205 *
206 *
222 * UPDATE: This problem affects the M$ Wireless Notebook Optical Mouse,
223 * too. However, the leading byte for this mouse is normally 0x11,
224 * and the phantom mouse click occurs when its 0x14.
207 * UPDATE: This problem affects the M$ Wireless
208 * Notebook Optical Mouse, too. However, the leading
209 * byte for this mouse is normally 0x11, and the
210 * phantom mouse click occurs when its 0x14.
225 *
226 * We probably should switch to some more official quirk.
227 */
228 if (sc->sc_iid) {
229 if (sc->sc_flags & UMS_FLAG_T_AXIS) {
230 if (*buf == 0x02) {
231 goto tr_setup;
232 }
233 } else {
234 if (*buf != sc->sc_iid) {
235 goto tr_setup;
236 }
237 }
238
239 len--;
240 buf++;
241
242 } else {
243 if (sc->sc_flags & UMS_FLAG_SBU) {
244 if ((*buf == 0x14) || (*buf == 0x15)) {
245 goto tr_setup;
246 }
247 }
248 }
249
250 dw = (sc->sc_flags & UMS_FLAG_W_AXIS) ?
251 hid_get_data(buf, len, &sc->sc_loc_w) : 0;
252
253 dx = (sc->sc_flags & UMS_FLAG_X_AXIS) ?
254 hid_get_data(buf, len, &sc->sc_loc_x) : 0;
255
256 dy = (sc->sc_flags & UMS_FLAG_Y_AXIS) ?
257 -hid_get_data(buf, len, &sc->sc_loc_y) : 0;
258
259 dz = (sc->sc_flags & UMS_FLAG_Z_AXIS) ?
260 -hid_get_data(buf, len, &sc->sc_loc_z) : 0;
261
262 if (sc->sc_flags & UMS_FLAG_REVZ) {
263 dz = -dz;
264 }
265 dt = (sc->sc_flags & UMS_FLAG_T_AXIS) ?
266 -hid_get_data(buf, len, &sc->sc_loc_t): 0;
267
268 for (i = 0; i < sc->sc_buttons; i++) {
269 if (hid_get_data(buf, len, &sc->sc_loc_btn[i])) {
270 buttons |= (1 << UMS_BUT(i));
271 }
272 }
273
274 if (dx || dy || dz || dt || dw ||
275 (buttons != sc->sc_status.button)) {
276
277 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
278 dx, dy, dz, dt, dw, buttons);
279
280 sc->sc_status.button = buttons;
281 sc->sc_status.dx += dx;
282 sc->sc_status.dy += dy;
283 sc->sc_status.dz += dz;
284 /*
285 * sc->sc_status.dt += dt;
286 * no way to export this yet
287 */
288
289 /*
211 *
212 * We probably should switch to some more official quirk.
213 */
214 if (sc->sc_iid) {
215 if (sc->sc_flags & UMS_FLAG_T_AXIS) {
216 if (*buf == 0x02) {
217 goto tr_setup;
218 }
219 } else {
220 if (*buf != sc->sc_iid) {
221 goto tr_setup;
222 }
223 }
224
225 len--;
226 buf++;
227
228 } else {
229 if (sc->sc_flags & UMS_FLAG_SBU) {
230 if ((*buf == 0x14) || (*buf == 0x15)) {
231 goto tr_setup;
232 }
233 }
234 }
235
236 dw = (sc->sc_flags & UMS_FLAG_W_AXIS) ?
237 hid_get_data(buf, len, &sc->sc_loc_w) : 0;
238
239 dx = (sc->sc_flags & UMS_FLAG_X_AXIS) ?
240 hid_get_data(buf, len, &sc->sc_loc_x) : 0;
241
242 dy = (sc->sc_flags & UMS_FLAG_Y_AXIS) ?
243 -hid_get_data(buf, len, &sc->sc_loc_y) : 0;
244
245 dz = (sc->sc_flags & UMS_FLAG_Z_AXIS) ?
246 -hid_get_data(buf, len, &sc->sc_loc_z) : 0;
247
248 if (sc->sc_flags & UMS_FLAG_REVZ) {
249 dz = -dz;
250 }
251 dt = (sc->sc_flags & UMS_FLAG_T_AXIS) ?
252 -hid_get_data(buf, len, &sc->sc_loc_t): 0;
253
254 for (i = 0; i < sc->sc_buttons; i++) {
255 if (hid_get_data(buf, len, &sc->sc_loc_btn[i])) {
256 buttons |= (1 << UMS_BUT(i));
257 }
258 }
259
260 if (dx || dy || dz || dt || dw ||
261 (buttons != sc->sc_status.button)) {
262
263 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
264 dx, dy, dz, dt, dw, buttons);
265
266 sc->sc_status.button = buttons;
267 sc->sc_status.dx += dx;
268 sc->sc_status.dy += dy;
269 sc->sc_status.dz += dz;
270 /*
271 * sc->sc_status.dt += dt;
272 * no way to export this yet
273 */
274
275 /*
290 * The Qtronix keyboard has a built in PS/2 port for a mouse.
291 * The firmware once in a while posts a spurious button up
292 * event. This event we ignore by doing a timeout for 50 msecs.
293 * If we receive dx=dy=dz=buttons=0 before we add the event to
294 * the queue.
295 * In any other case we delete the timeout event.
276 * The Qtronix keyboard has a built in PS/2
277 * port for a mouse. The firmware once in a
278 * while posts a spurious button up
279 * event. This event we ignore by doing a
280 * timeout for 50 msecs. If we receive
281 * dx=dy=dz=buttons=0 before we add the event
282 * to the queue. In any other case we delete
283 * the timeout event.
296 */
297 if ((sc->sc_flags & UMS_FLAG_SBU) &&
298 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
299 (dw == 0) && (buttons == 0)) {
300
301 usb2_callout_reset(&sc->sc_callout, hz / 20,
302 &ums_put_queue_timeout, sc);
303 } else {
304
305 usb2_callout_stop(&sc->sc_callout);
306
307 ums_put_queue(sc, dx, dy, dz, dt, buttons);
308 }
309 }
310 case USB_ST_SETUP:
311tr_setup:
284 */
285 if ((sc->sc_flags & UMS_FLAG_SBU) &&
286 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
287 (dw == 0) && (buttons == 0)) {
288
289 usb2_callout_reset(&sc->sc_callout, hz / 20,
290 &ums_put_queue_timeout, sc);
291 } else {
292
293 usb2_callout_stop(&sc->sc_callout);
294
295 ums_put_queue(sc, dx, dy, dz, dt, buttons);
296 }
297 }
298 case USB_ST_SETUP:
299tr_setup:
312 if (sc->sc_flags & UMS_FLAG_INTR_STALL) {
313 usb2_transfer_start(sc->sc_xfer[UMS_INTR_CS]);
314 } else {
315 /* check if we can put more data into the FIFO */
316 if (usb2_fifo_put_bytes_max(
317 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
318 xfer->frlengths[0] = xfer->max_data_length;
319 usb2_start_hardware(xfer);
320 }
300 /* check if we can put more data into the FIFO */
301 if (usb2_fifo_put_bytes_max(
302 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
303 xfer->frlengths[0] = xfer->max_data_length;
304 usb2_start_hardware(xfer);
321 }
305 }
322 return;
306 break;
323
324 default: /* Error */
325 if (xfer->error != USB_ERR_CANCELLED) {
307
308 default: /* Error */
309 if (xfer->error != USB_ERR_CANCELLED) {
326 /* start clear stall */
327 sc->sc_flags |= UMS_FLAG_INTR_STALL;
328 usb2_transfer_start(sc->sc_xfer[UMS_INTR_CS]);
310 /* try clear stall first */
311 xfer->flags.stall_pipe = 1;
312 goto tr_setup;
329 }
313 }
330 return;
314 break;
331 }
332}
333
334static const struct usb2_config ums_config[UMS_N_TRANSFER] = {
335
336 [UMS_INTR_DT] = {
337 .type = UE_INTERRUPT,
338 .endpoint = UE_ADDR_ANY,
339 .direction = UE_DIR_IN,
340 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
341 .mh.bufsize = 0, /* use wMaxPacketSize */
342 .mh.callback = &ums_intr_callback,
343 },
315 }
316}
317
318static const struct usb2_config ums_config[UMS_N_TRANSFER] = {
319
320 [UMS_INTR_DT] = {
321 .type = UE_INTERRUPT,
322 .endpoint = UE_ADDR_ANY,
323 .direction = UE_DIR_IN,
324 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
325 .mh.bufsize = 0, /* use wMaxPacketSize */
326 .mh.callback = &ums_intr_callback,
327 },
344
345 [UMS_INTR_CS] = {
346 .type = UE_CONTROL,
347 .endpoint = 0x00, /* Control pipe */
348 .direction = UE_DIR_ANY,
349 .mh.bufsize = sizeof(struct usb2_device_request),
350 .mh.callback = &ums_clear_stall_callback,
351 .mh.timeout = 1000, /* 1 second */
352 .mh.interval = 50, /* 50ms */
353 },
354};
355
356static int
357ums_probe(device_t dev)
358{
359 struct usb2_attach_arg *uaa = device_get_ivars(dev);
360 struct usb2_interface_descriptor *id;
361 void *d_ptr;
328};
329
330static int
331ums_probe(device_t dev)
332{
333 struct usb2_attach_arg *uaa = device_get_ivars(dev);
334 struct usb2_interface_descriptor *id;
335 void *d_ptr;
362 int32_t error = 0;
336 int error;
363 uint16_t d_len;
364
365 DPRINTFN(11, "\n");
366
337 uint16_t d_len;
338
339 DPRINTFN(11, "\n");
340
367 if (uaa->usb2_mode != USB_MODE_HOST) {
341 if (uaa->usb2_mode != USB_MODE_HOST)
368 return (ENXIO);
342 return (ENXIO);
369 }
370 if (uaa->iface == NULL) {
371 return (ENXIO);
372 }
343
373 id = usb2_get_interface_descriptor(uaa->iface);
374
375 if ((id == NULL) ||
344 id = usb2_get_interface_descriptor(uaa->iface);
345
346 if ((id == NULL) ||
376 (id->bInterfaceClass != UICLASS_HID)) {
347 (id->bInterfaceClass != UICLASS_HID))
377 return (ENXIO);
348 return (ENXIO);
378 }
379 error = usb2_req_get_hid_desc
380 (uaa->device, &Giant,
349
350 error = usb2_req_get_hid_desc(uaa->device, &Giant,
381 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
382
351 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
352
383 if (error) {
353 if (error)
384 return (ENXIO);
354 return (ENXIO);
385 }
355
386 if (hid_is_collection(d_ptr, d_len,
356 if (hid_is_collection(d_ptr, d_len,
387 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
357 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
388 error = 0;
358 error = 0;
389 } else if ((id->bInterfaceSubClass == UISUBCLASS_BOOT) &&
390 (id->bInterfaceProtocol == UIPROTO_MOUSE)) {
359 else if ((id->bInterfaceSubClass == UISUBCLASS_BOOT) &&
360 (id->bInterfaceProtocol == UIPROTO_MOUSE))
391 error = 0;
361 error = 0;
392 } else {
362 else
393 error = ENXIO;
363 error = ENXIO;
394 }
395
396 free(d_ptr, M_TEMP);
397 return (error);
398}
399
400static int
401ums_attach(device_t dev)
402{
403 struct usb2_attach_arg *uaa = device_get_ivars(dev);
404 struct ums_softc *sc = device_get_softc(dev);
405 void *d_ptr = NULL;
406 int unit = device_get_unit(dev);
364
365 free(d_ptr, M_TEMP);
366 return (error);
367}
368
369static int
370ums_attach(device_t dev)
371{
372 struct usb2_attach_arg *uaa = device_get_ivars(dev);
373 struct ums_softc *sc = device_get_softc(dev);
374 void *d_ptr = NULL;
375 int unit = device_get_unit(dev);
407 int32_t isize;
376 int isize;
377 int isizebits;
378 int err;
408 uint32_t flags;
379 uint32_t flags;
409 int32_t err;
410 uint16_t d_len;
411 uint8_t i;
412
413 DPRINTFN(11, "sc=%p\n", sc);
414
415 device_set_usb2_desc(dev);
416
417 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
418
419 usb2_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
420
421 /*
422 * Force the report (non-boot) protocol.
423 *
424 * Mice without boot protocol support may choose not to implement
425 * Set_Protocol at all; Ignore any error.
426 */
427 err = usb2_req_set_protocol(uaa->device, NULL, uaa->info.bIfaceIndex, 1);
428
429 err = usb2_transfer_setup(uaa->device,
430 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
431 UMS_N_TRANSFER, sc, &sc->sc_mtx);
432
433 if (err) {
434 DPRINTF("error=%s\n", usb2_errstr(err));
435 goto detach;
436 }
437 err = usb2_req_get_hid_desc
438 (uaa->device, &Giant, &d_ptr,
439 &d_len, M_TEMP, uaa->info.bIfaceIndex);
440
441 if (err) {
442 device_printf(dev, "error reading report description\n");
443 goto detach;
444 }
445 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
380 uint16_t d_len;
381 uint8_t i;
382
383 DPRINTFN(11, "sc=%p\n", sc);
384
385 device_set_usb2_desc(dev);
386
387 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
388
389 usb2_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
390
391 /*
392 * Force the report (non-boot) protocol.
393 *
394 * Mice without boot protocol support may choose not to implement
395 * Set_Protocol at all; Ignore any error.
396 */
397 err = usb2_req_set_protocol(uaa->device, NULL, uaa->info.bIfaceIndex, 1);
398
399 err = usb2_transfer_setup(uaa->device,
400 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
401 UMS_N_TRANSFER, sc, &sc->sc_mtx);
402
403 if (err) {
404 DPRINTF("error=%s\n", usb2_errstr(err));
405 goto detach;
406 }
407 err = usb2_req_get_hid_desc
408 (uaa->device, &Giant, &d_ptr,
409 &d_len, M_TEMP, uaa->info.bIfaceIndex);
410
411 if (err) {
412 device_printf(dev, "error reading report description\n");
413 goto detach;
414 }
415 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
446 hid_input, &sc->sc_loc_x, &flags)) {
416 hid_input, &sc->sc_loc_x, &flags, &sc->sc_iid)) {
447
448 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
449 sc->sc_flags |= UMS_FLAG_X_AXIS;
450 }
451 }
452 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
417
418 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
419 sc->sc_flags |= UMS_FLAG_X_AXIS;
420 }
421 }
422 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
453 hid_input, &sc->sc_loc_y, &flags)) {
423 hid_input, &sc->sc_loc_y, &flags, &sc->sc_iid)) {
454
455 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
456 sc->sc_flags |= UMS_FLAG_Y_AXIS;
457 }
458 }
459 /* Try the wheel first as the Z activator since it's tradition. */
460 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
424
425 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
426 sc->sc_flags |= UMS_FLAG_Y_AXIS;
427 }
428 }
429 /* Try the wheel first as the Z activator since it's tradition. */
430 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
461 HUG_WHEEL), hid_input, &sc->sc_loc_z, &flags) ||
431 HUG_WHEEL), hid_input, &sc->sc_loc_z, &flags, &sc->sc_iid) ||
462 hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
432 hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
463 HUG_TWHEEL), hid_input, &sc->sc_loc_z, &flags)) {
433 HUG_TWHEEL), hid_input, &sc->sc_loc_z, &flags, &sc->sc_iid)) {
464 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
465 sc->sc_flags |= UMS_FLAG_Z_AXIS;
466 }
467 /*
468 * We might have both a wheel and Z direction, if so put
469 * put the Z on the W coordinate.
470 */
471 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
434 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
435 sc->sc_flags |= UMS_FLAG_Z_AXIS;
436 }
437 /*
438 * We might have both a wheel and Z direction, if so put
439 * put the Z on the W coordinate.
440 */
441 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
472 HUG_Z), hid_input, &sc->sc_loc_w, &flags)) {
442 HUG_Z), hid_input, &sc->sc_loc_w, &flags, &sc->sc_iid)) {
473
474 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
475 sc->sc_flags |= UMS_FLAG_W_AXIS;
476 }
477 }
478 } else if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
443
444 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
445 sc->sc_flags |= UMS_FLAG_W_AXIS;
446 }
447 }
448 } else if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
479 HUG_Z), hid_input, &sc->sc_loc_z, &flags)) {
449 HUG_Z), hid_input, &sc->sc_loc_z, &flags, &sc->sc_iid)) {
480
481 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
482 sc->sc_flags |= UMS_FLAG_Z_AXIS;
483 }
484 }
485 /*
486 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
487 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
488 * to know that the byte after the wheel is the tilt axis.
489 * There are no other HID axis descriptors other than X,Y and
490 * TWHEEL
491 */
492 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_TWHEEL),
450
451 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
452 sc->sc_flags |= UMS_FLAG_Z_AXIS;
453 }
454 }
455 /*
456 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
457 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
458 * to know that the byte after the wheel is the tilt axis.
459 * There are no other HID axis descriptors other than X,Y and
460 * TWHEEL
461 */
462 if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_TWHEEL),
493 hid_input, &sc->sc_loc_t, &flags)) {
463 hid_input, &sc->sc_loc_t, &flags, &sc->sc_iid)) {
494
495 sc->sc_loc_t.pos += 8;
496
497 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
498 sc->sc_flags |= UMS_FLAG_T_AXIS;
499 }
500 }
501 /* figure out the number of buttons */
502
503 for (i = 0; i < UMS_BUTTON_MAX; i++) {
504 if (!hid_locate(d_ptr, d_len, HID_USAGE2(HUP_BUTTON, (i + 1)),
464
465 sc->sc_loc_t.pos += 8;
466
467 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
468 sc->sc_flags |= UMS_FLAG_T_AXIS;
469 }
470 }
471 /* figure out the number of buttons */
472
473 for (i = 0; i < UMS_BUTTON_MAX; i++) {
474 if (!hid_locate(d_ptr, d_len, HID_USAGE2(HUP_BUTTON, (i + 1)),
505 hid_input, &sc->sc_loc_btn[i], NULL)) {
475 hid_input, &sc->sc_loc_btn[i], NULL, &sc->sc_iid)) {
506 break;
507 }
508 }
509
510 sc->sc_buttons = i;
511
476 break;
477 }
478 }
479
480 sc->sc_buttons = i;
481
512 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
482 isize = hid_report_size(d_ptr, d_len, hid_input, NULL);
513
514 /*
515 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
516 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
517 * all of its other button positions are all off. It also reports that
518 * it has two addional buttons and a tilt wheel.
519 */
520 if (usb2_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
521 sc->sc_flags = (UMS_FLAG_X_AXIS |
522 UMS_FLAG_Y_AXIS |
523 UMS_FLAG_Z_AXIS |
524 UMS_FLAG_SBU);
525 sc->sc_buttons = 3;
526 isize = 5;
527 sc->sc_iid = 0;
528 /* 1st byte of descriptor report contains garbage */
529 sc->sc_loc_x.pos = 16;
530 sc->sc_loc_y.pos = 24;
531 sc->sc_loc_z.pos = 32;
532 sc->sc_loc_btn[0].pos = 8;
533 sc->sc_loc_btn[1].pos = 9;
534 sc->sc_loc_btn[2].pos = 10;
535 }
483
484 /*
485 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
486 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
487 * all of its other button positions are all off. It also reports that
488 * it has two addional buttons and a tilt wheel.
489 */
490 if (usb2_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
491 sc->sc_flags = (UMS_FLAG_X_AXIS |
492 UMS_FLAG_Y_AXIS |
493 UMS_FLAG_Z_AXIS |
494 UMS_FLAG_SBU);
495 sc->sc_buttons = 3;
496 isize = 5;
497 sc->sc_iid = 0;
498 /* 1st byte of descriptor report contains garbage */
499 sc->sc_loc_x.pos = 16;
500 sc->sc_loc_y.pos = 24;
501 sc->sc_loc_z.pos = 32;
502 sc->sc_loc_btn[0].pos = 8;
503 sc->sc_loc_btn[1].pos = 9;
504 sc->sc_loc_btn[2].pos = 10;
505 }
506
536 /*
507 /*
537 * The Microsoft Wireless Notebook Optical Mouse 3000 Model 1049 has
538 * five Report IDs: 19 23 24 17 18 (in the order they appear in report
539 * descriptor), it seems that report id 17 contains the necessary
540 * mouse information(3-buttons,X,Y,wheel) so we specify it manually.
508 * Some Microsoft devices have incorrectly high location
509 * positions. Correct this:
541 */
510 */
542 if ((uaa->info.idVendor == USB_VENDOR_MICROSOFT) &&
543 (uaa->info.idProduct == USB_PRODUCT_MICROSOFT_WLNOTEBOOK3)) {
544 sc->sc_flags = (UMS_FLAG_X_AXIS |
545 UMS_FLAG_Y_AXIS |
546 UMS_FLAG_Z_AXIS);
547 sc->sc_buttons = 3;
548 isize = 5;
549 sc->sc_iid = 17;
550 sc->sc_loc_x.pos = 8;
551 sc->sc_loc_y.pos = 16;
552 sc->sc_loc_z.pos = 24;
553 sc->sc_loc_btn[0].pos = 0;
554 sc->sc_loc_btn[1].pos = 1;
555 sc->sc_loc_btn[2].pos = 2;
511 isizebits = isize * 8;
512 if ((sc->sc_iid != 0) && (isizebits > 8)) {
513 isizebits -= 8; /* remove size of report ID */
514 sc->sc_loc_w.pos %= isizebits;
515 sc->sc_loc_x.pos %= isizebits;
516 sc->sc_loc_y.pos %= isizebits;
517 sc->sc_loc_z.pos %= isizebits;
518 sc->sc_loc_t.pos %= isizebits;
519 for (i = 0; i != UMS_BUTTON_MAX; i++)
520 sc->sc_loc_btn[i].pos %= isizebits;
556 }
521 }
522
557 if (usb2_test_quirk(uaa, UQ_MS_REVZ)) {
558 /* Some wheels need the Z axis reversed. */
559 sc->sc_flags |= UMS_FLAG_REVZ;
560 }
561 if (isize > sc->sc_xfer[UMS_INTR_DT]->max_frame_size) {
562 DPRINTF("WARNING: report size, %d bytes, is larger "
563 "than interrupt size, %d bytes!\n",
564 isize, sc->sc_xfer[UMS_INTR_DT]->max_frame_size);
565 }
566 /* announce information about the mouse */
567
568 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates\n",
569 (sc->sc_buttons),
570 (sc->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
571 (sc->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
572 (sc->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
573 (sc->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
574 (sc->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "");
575
576 free(d_ptr, M_TEMP);
577 d_ptr = NULL;
578
579#if USB_DEBUG
580 DPRINTF("sc=%p\n", sc);
581 DPRINTF("X\t%d/%d\n", sc->sc_loc_x.pos, sc->sc_loc_x.size);
582 DPRINTF("Y\t%d/%d\n", sc->sc_loc_y.pos, sc->sc_loc_y.size);
583 DPRINTF("Z\t%d/%d\n", sc->sc_loc_z.pos, sc->sc_loc_z.size);
584 DPRINTF("T\t%d/%d\n", sc->sc_loc_t.pos, sc->sc_loc_t.size);
585 DPRINTF("W\t%d/%d\n", sc->sc_loc_w.pos, sc->sc_loc_w.size);
586
587 for (i = 0; i < sc->sc_buttons; i++) {
588 DPRINTF("B%d\t%d/%d\n",
589 i + 1, sc->sc_loc_btn[i].pos, sc->sc_loc_btn[i].size);
590 }
591 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
592#endif
593
594 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
595 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
596 else
597 sc->sc_hw.buttons = sc->sc_buttons;
598
599 sc->sc_hw.iftype = MOUSE_IF_USB;
600 sc->sc_hw.type = MOUSE_MOUSE;
601 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
602 sc->sc_hw.hwid = 0;
603
604 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
605 sc->sc_mode.rate = -1;
606 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
607 sc->sc_mode.accelfactor = 0;
608 sc->sc_mode.level = 0;
609 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
610 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
611 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
612
613 sc->sc_status.flags = 0;
614 sc->sc_status.button = 0;
615 sc->sc_status.obutton = 0;
616 sc->sc_status.dx = 0;
617 sc->sc_status.dy = 0;
618 sc->sc_status.dz = 0;
619
620 /* set interface permissions */
621 usb2_set_iface_perm(uaa->device, uaa->info.bIfaceIndex,
622 UID_ROOT, GID_OPERATOR, 0644);
623
624 err = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx,
625 &ums_fifo_methods, &sc->sc_fifo,
626 unit, 0 - 1, uaa->info.bIfaceIndex);
627 if (err) {
628 goto detach;
629 }
630 return (0);
631
632detach:
633 if (d_ptr) {
634 free(d_ptr, M_TEMP);
635 }
636 ums_detach(dev);
637 return (ENOMEM);
638}
639
640static int
641ums_detach(device_t self)
642{
643 struct ums_softc *sc = device_get_softc(self);
644
645 DPRINTF("sc=%p\n", sc);
646
647 usb2_fifo_detach(&sc->sc_fifo);
648
649 usb2_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
650
651 usb2_callout_drain(&sc->sc_callout);
652
653 mtx_destroy(&sc->sc_mtx);
654
655 return (0);
656}
657
658static void
659ums_start_read(struct usb2_fifo *fifo)
660{
661 struct ums_softc *sc = fifo->priv_sc0;
662
663 usb2_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
664}
665
666static void
667ums_stop_read(struct usb2_fifo *fifo)
668{
669 struct ums_softc *sc = fifo->priv_sc0;
670
523 if (usb2_test_quirk(uaa, UQ_MS_REVZ)) {
524 /* Some wheels need the Z axis reversed. */
525 sc->sc_flags |= UMS_FLAG_REVZ;
526 }
527 if (isize > sc->sc_xfer[UMS_INTR_DT]->max_frame_size) {
528 DPRINTF("WARNING: report size, %d bytes, is larger "
529 "than interrupt size, %d bytes!\n",
530 isize, sc->sc_xfer[UMS_INTR_DT]->max_frame_size);
531 }
532 /* announce information about the mouse */
533
534 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates\n",
535 (sc->sc_buttons),
536 (sc->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
537 (sc->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
538 (sc->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
539 (sc->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
540 (sc->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "");
541
542 free(d_ptr, M_TEMP);
543 d_ptr = NULL;
544
545#if USB_DEBUG
546 DPRINTF("sc=%p\n", sc);
547 DPRINTF("X\t%d/%d\n", sc->sc_loc_x.pos, sc->sc_loc_x.size);
548 DPRINTF("Y\t%d/%d\n", sc->sc_loc_y.pos, sc->sc_loc_y.size);
549 DPRINTF("Z\t%d/%d\n", sc->sc_loc_z.pos, sc->sc_loc_z.size);
550 DPRINTF("T\t%d/%d\n", sc->sc_loc_t.pos, sc->sc_loc_t.size);
551 DPRINTF("W\t%d/%d\n", sc->sc_loc_w.pos, sc->sc_loc_w.size);
552
553 for (i = 0; i < sc->sc_buttons; i++) {
554 DPRINTF("B%d\t%d/%d\n",
555 i + 1, sc->sc_loc_btn[i].pos, sc->sc_loc_btn[i].size);
556 }
557 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
558#endif
559
560 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
561 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
562 else
563 sc->sc_hw.buttons = sc->sc_buttons;
564
565 sc->sc_hw.iftype = MOUSE_IF_USB;
566 sc->sc_hw.type = MOUSE_MOUSE;
567 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
568 sc->sc_hw.hwid = 0;
569
570 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
571 sc->sc_mode.rate = -1;
572 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
573 sc->sc_mode.accelfactor = 0;
574 sc->sc_mode.level = 0;
575 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
576 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
577 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
578
579 sc->sc_status.flags = 0;
580 sc->sc_status.button = 0;
581 sc->sc_status.obutton = 0;
582 sc->sc_status.dx = 0;
583 sc->sc_status.dy = 0;
584 sc->sc_status.dz = 0;
585
586 /* set interface permissions */
587 usb2_set_iface_perm(uaa->device, uaa->info.bIfaceIndex,
588 UID_ROOT, GID_OPERATOR, 0644);
589
590 err = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx,
591 &ums_fifo_methods, &sc->sc_fifo,
592 unit, 0 - 1, uaa->info.bIfaceIndex);
593 if (err) {
594 goto detach;
595 }
596 return (0);
597
598detach:
599 if (d_ptr) {
600 free(d_ptr, M_TEMP);
601 }
602 ums_detach(dev);
603 return (ENOMEM);
604}
605
606static int
607ums_detach(device_t self)
608{
609 struct ums_softc *sc = device_get_softc(self);
610
611 DPRINTF("sc=%p\n", sc);
612
613 usb2_fifo_detach(&sc->sc_fifo);
614
615 usb2_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
616
617 usb2_callout_drain(&sc->sc_callout);
618
619 mtx_destroy(&sc->sc_mtx);
620
621 return (0);
622}
623
624static void
625ums_start_read(struct usb2_fifo *fifo)
626{
627 struct ums_softc *sc = fifo->priv_sc0;
628
629 usb2_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
630}
631
632static void
633ums_stop_read(struct usb2_fifo *fifo)
634{
635 struct ums_softc *sc = fifo->priv_sc0;
636
671 usb2_transfer_stop(sc->sc_xfer[UMS_INTR_CS]);
672 usb2_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
673 usb2_callout_stop(&sc->sc_callout);
674}
675
676
677#if ((MOUSE_SYS_PACKETSIZE != 8) || \
678 (MOUSE_MSC_PACKETSIZE != 5))
679#error "Software assumptions are not met. Please update code."
680#endif
681
682static void
683ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
684 int32_t dz, int32_t dt, int32_t buttons)
685{
686 uint8_t buf[8];
687
688 if (1) {
689
690 if (dx > 254)
691 dx = 254;
692 if (dx < -256)
693 dx = -256;
694 if (dy > 254)
695 dy = 254;
696 if (dy < -256)
697 dy = -256;
698 if (dz > 126)
699 dz = 126;
700 if (dz < -128)
701 dz = -128;
702 if (dt > 126)
703 dt = 126;
704 if (dt < -128)
705 dt = -128;
706
707 buf[0] = sc->sc_mode.syncmask[1];
708 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
709 buf[1] = dx >> 1;
710 buf[2] = dy >> 1;
711 buf[3] = dx - (dx >> 1);
712 buf[4] = dy - (dy >> 1);
713
714 if (sc->sc_mode.level == 1) {
715 buf[5] = dz >> 1;
716 buf[6] = dz - (dz >> 1);
717 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
718 }
719 usb2_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
720 sc->sc_mode.packetsize, 1);
721
722 } else {
723 DPRINTF("Buffer full, discarded packet\n");
724 }
725}
726
727static void
728ums_reset_buf(struct ums_softc *sc)
729{
730 /* reset read queue */
731 usb2_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
732}
733
734static int
735ums_open(struct usb2_fifo *fifo, int fflags, struct thread *td)
736{
737 struct ums_softc *sc = fifo->priv_sc0;
738
739 DPRINTFN(2, "\n");
740
741 if (fflags & FREAD) {
742
743 /* reset status */
744
745 sc->sc_status.flags = 0;
746 sc->sc_status.button = 0;
747 sc->sc_status.obutton = 0;
748 sc->sc_status.dx = 0;
749 sc->sc_status.dy = 0;
750 sc->sc_status.dz = 0;
751 /* sc->sc_status.dt = 0; */
752
753 if (usb2_fifo_alloc_buffer(fifo,
754 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
755 return (ENOMEM);
756 }
757 }
758 return (0);
759}
760
761static void
762ums_close(struct usb2_fifo *fifo, int fflags, struct thread *td)
763{
764 if (fflags & FREAD) {
765 usb2_fifo_free_buffer(fifo);
766 }
767}
768
769static int
770ums_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr,
771 int fflags, struct thread *td)
772{
773 struct ums_softc *sc = fifo->priv_sc0;
774 mousemode_t mode;
775 int error = 0;
776
777 DPRINTFN(2, "\n");
778
779 mtx_lock(&sc->sc_mtx);
780
781 switch (cmd) {
782 case MOUSE_GETHWINFO:
783 *(mousehw_t *)addr = sc->sc_hw;
784 break;
785
786 case MOUSE_GETMODE:
787 *(mousemode_t *)addr = sc->sc_mode;
788 break;
789
790 case MOUSE_SETMODE:
791 mode = *(mousemode_t *)addr;
792
793 if (mode.level == -1) {
794 /* don't change the current setting */
795 } else if ((mode.level < 0) || (mode.level > 1)) {
796 error = EINVAL;
797 goto done;
798 } else {
799 sc->sc_mode.level = mode.level;
800 }
801
802 if (sc->sc_mode.level == 0) {
803 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
804 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
805 else
806 sc->sc_hw.buttons = sc->sc_buttons;
807 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
808 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
809 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
810 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
811 } else if (sc->sc_mode.level == 1) {
812 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
813 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
814 else
815 sc->sc_hw.buttons = sc->sc_buttons;
816 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
817 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
818 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
819 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
820 }
821 ums_reset_buf(sc);
822 break;
823
824 case MOUSE_GETLEVEL:
825 *(int *)addr = sc->sc_mode.level;
826 break;
827
828 case MOUSE_SETLEVEL:
829 if (*(int *)addr < 0 || *(int *)addr > 1) {
830 error = EINVAL;
831 goto done;
832 }
833 sc->sc_mode.level = *(int *)addr;
834
835 if (sc->sc_mode.level == 0) {
836 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
837 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
838 else
839 sc->sc_hw.buttons = sc->sc_buttons;
840 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
841 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
842 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
843 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
844 } else if (sc->sc_mode.level == 1) {
845 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
846 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
847 else
848 sc->sc_hw.buttons = sc->sc_buttons;
849 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
850 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
851 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
852 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
853 }
854 ums_reset_buf(sc);
855 break;
856
857 case MOUSE_GETSTATUS:{
858 mousestatus_t *status = (mousestatus_t *)addr;
859
860 *status = sc->sc_status;
861 sc->sc_status.obutton = sc->sc_status.button;
862 sc->sc_status.button = 0;
863 sc->sc_status.dx = 0;
864 sc->sc_status.dy = 0;
865 sc->sc_status.dz = 0;
866 /* sc->sc_status.dt = 0; */
867
868 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
869 status->flags |= MOUSE_POSCHANGED;
870 }
871 if (status->button != status->obutton) {
872 status->flags |= MOUSE_BUTTONSCHANGED;
873 }
874 break;
875 }
876 default:
877 error = ENOTTY;
878 }
879
880done:
881 mtx_unlock(&sc->sc_mtx);
882 return (error);
883}
884
885static devclass_t ums_devclass;
886
887static device_method_t ums_methods[] = {
888 DEVMETHOD(device_probe, ums_probe),
889 DEVMETHOD(device_attach, ums_attach),
890 DEVMETHOD(device_detach, ums_detach),
891 {0, 0}
892};
893
894static driver_t ums_driver = {
895 .name = "ums",
896 .methods = ums_methods,
897 .size = sizeof(struct ums_softc),
898};
899
900DRIVER_MODULE(ums, ushub, ums_driver, ums_devclass, NULL, 0);
901MODULE_DEPEND(ums, usb, 1, 1, 1);
637 usb2_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
638 usb2_callout_stop(&sc->sc_callout);
639}
640
641
642#if ((MOUSE_SYS_PACKETSIZE != 8) || \
643 (MOUSE_MSC_PACKETSIZE != 5))
644#error "Software assumptions are not met. Please update code."
645#endif
646
647static void
648ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
649 int32_t dz, int32_t dt, int32_t buttons)
650{
651 uint8_t buf[8];
652
653 if (1) {
654
655 if (dx > 254)
656 dx = 254;
657 if (dx < -256)
658 dx = -256;
659 if (dy > 254)
660 dy = 254;
661 if (dy < -256)
662 dy = -256;
663 if (dz > 126)
664 dz = 126;
665 if (dz < -128)
666 dz = -128;
667 if (dt > 126)
668 dt = 126;
669 if (dt < -128)
670 dt = -128;
671
672 buf[0] = sc->sc_mode.syncmask[1];
673 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
674 buf[1] = dx >> 1;
675 buf[2] = dy >> 1;
676 buf[3] = dx - (dx >> 1);
677 buf[4] = dy - (dy >> 1);
678
679 if (sc->sc_mode.level == 1) {
680 buf[5] = dz >> 1;
681 buf[6] = dz - (dz >> 1);
682 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
683 }
684 usb2_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
685 sc->sc_mode.packetsize, 1);
686
687 } else {
688 DPRINTF("Buffer full, discarded packet\n");
689 }
690}
691
692static void
693ums_reset_buf(struct ums_softc *sc)
694{
695 /* reset read queue */
696 usb2_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
697}
698
699static int
700ums_open(struct usb2_fifo *fifo, int fflags, struct thread *td)
701{
702 struct ums_softc *sc = fifo->priv_sc0;
703
704 DPRINTFN(2, "\n");
705
706 if (fflags & FREAD) {
707
708 /* reset status */
709
710 sc->sc_status.flags = 0;
711 sc->sc_status.button = 0;
712 sc->sc_status.obutton = 0;
713 sc->sc_status.dx = 0;
714 sc->sc_status.dy = 0;
715 sc->sc_status.dz = 0;
716 /* sc->sc_status.dt = 0; */
717
718 if (usb2_fifo_alloc_buffer(fifo,
719 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
720 return (ENOMEM);
721 }
722 }
723 return (0);
724}
725
726static void
727ums_close(struct usb2_fifo *fifo, int fflags, struct thread *td)
728{
729 if (fflags & FREAD) {
730 usb2_fifo_free_buffer(fifo);
731 }
732}
733
734static int
735ums_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr,
736 int fflags, struct thread *td)
737{
738 struct ums_softc *sc = fifo->priv_sc0;
739 mousemode_t mode;
740 int error = 0;
741
742 DPRINTFN(2, "\n");
743
744 mtx_lock(&sc->sc_mtx);
745
746 switch (cmd) {
747 case MOUSE_GETHWINFO:
748 *(mousehw_t *)addr = sc->sc_hw;
749 break;
750
751 case MOUSE_GETMODE:
752 *(mousemode_t *)addr = sc->sc_mode;
753 break;
754
755 case MOUSE_SETMODE:
756 mode = *(mousemode_t *)addr;
757
758 if (mode.level == -1) {
759 /* don't change the current setting */
760 } else if ((mode.level < 0) || (mode.level > 1)) {
761 error = EINVAL;
762 goto done;
763 } else {
764 sc->sc_mode.level = mode.level;
765 }
766
767 if (sc->sc_mode.level == 0) {
768 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
769 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
770 else
771 sc->sc_hw.buttons = sc->sc_buttons;
772 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
773 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
774 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
775 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
776 } else if (sc->sc_mode.level == 1) {
777 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
778 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
779 else
780 sc->sc_hw.buttons = sc->sc_buttons;
781 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
782 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
783 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
784 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
785 }
786 ums_reset_buf(sc);
787 break;
788
789 case MOUSE_GETLEVEL:
790 *(int *)addr = sc->sc_mode.level;
791 break;
792
793 case MOUSE_SETLEVEL:
794 if (*(int *)addr < 0 || *(int *)addr > 1) {
795 error = EINVAL;
796 goto done;
797 }
798 sc->sc_mode.level = *(int *)addr;
799
800 if (sc->sc_mode.level == 0) {
801 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
802 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
803 else
804 sc->sc_hw.buttons = sc->sc_buttons;
805 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
806 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
807 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
808 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
809 } else if (sc->sc_mode.level == 1) {
810 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
811 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
812 else
813 sc->sc_hw.buttons = sc->sc_buttons;
814 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
815 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
816 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
817 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
818 }
819 ums_reset_buf(sc);
820 break;
821
822 case MOUSE_GETSTATUS:{
823 mousestatus_t *status = (mousestatus_t *)addr;
824
825 *status = sc->sc_status;
826 sc->sc_status.obutton = sc->sc_status.button;
827 sc->sc_status.button = 0;
828 sc->sc_status.dx = 0;
829 sc->sc_status.dy = 0;
830 sc->sc_status.dz = 0;
831 /* sc->sc_status.dt = 0; */
832
833 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
834 status->flags |= MOUSE_POSCHANGED;
835 }
836 if (status->button != status->obutton) {
837 status->flags |= MOUSE_BUTTONSCHANGED;
838 }
839 break;
840 }
841 default:
842 error = ENOTTY;
843 }
844
845done:
846 mtx_unlock(&sc->sc_mtx);
847 return (error);
848}
849
850static devclass_t ums_devclass;
851
852static device_method_t ums_methods[] = {
853 DEVMETHOD(device_probe, ums_probe),
854 DEVMETHOD(device_attach, ums_attach),
855 DEVMETHOD(device_detach, ums_detach),
856 {0, 0}
857};
858
859static driver_t ums_driver = {
860 .name = "ums",
861 .methods = ums_methods,
862 .size = sizeof(struct ums_softc),
863};
864
865DRIVER_MODULE(ums, ushub, ums_driver, ums_devclass, NULL, 0);
866MODULE_DEPEND(ums, usb, 1, 1, 1);