1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2012 Huang Wen Hui
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "opt_evdev.h"
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/fcntl.h>
41 #include <sys/file.h>
42 #include <sys/selinfo.h>
43 #include <sys/poll.h>
44 #include <sys/sysctl.h>
45
46 #include <dev/hid/hid.h>
47
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usbhid.h>
52
53 #include "usbdevs.h"
54
55 #define USB_DEBUG_VAR wsp_debug
56 #include <dev/usb/usb_debug.h>
57
58 #ifdef EVDEV_SUPPORT
59 #include <dev/evdev/input.h>
60 #include <dev/evdev/evdev.h>
61 #endif
62
63 #include <sys/mouse.h>
64
65 #define WSP_DRIVER_NAME "wsp"
66 #define WSP_BUFFER_MAX 1024
67
68 #define WSP_CLAMP(x,low,high) do { \
69 if ((x) < (low)) \
70 (x) = (low); \
71 else if ((x) > (high)) \
72 (x) = (high); \
73 } while (0)
74
75 /* Tunables */
76 static SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
77 "USB wsp");
78
79 #ifdef USB_DEBUG
80 enum wsp_log_level {
81 WSP_LLEVEL_DISABLED = 0,
82 WSP_LLEVEL_ERROR,
83 WSP_LLEVEL_DEBUG, /* for troubleshooting */
84 WSP_LLEVEL_INFO, /* for diagnostics */
85 };
86 static int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */
87
88 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RWTUN,
89 &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level (0-3)");
90 #endif /* USB_DEBUG */
91
92 static struct wsp_tuning {
93 int scale_factor;
94 int scroll_finger_count;
95 int horizontal_swipe_finger_count;
96 int z_factor;
97 int z_invert;
98 int t_factor;
99 int t_invert;
100 int pressure_touch_threshold;
101 int pressure_untouch_threshold;
102 int pressure_tap_threshold;
103 int scr_threshold;
104 int max_finger_diameter;
105 int max_scroll_finger_distance;
106 int max_double_tap_distance;
107 int enable_single_tap_clicks;
108 int enable_single_tap_movement;
109 }
110 wsp_tuning =
111 {
112 .scale_factor = 12,
113 .scroll_finger_count = 2,
114 .horizontal_swipe_finger_count = 3,
115 .z_factor = 5,
116 .z_invert = 0,
117 .t_factor = 0,
118 .t_invert = 0,
119 .pressure_touch_threshold = 50,
120 .pressure_untouch_threshold = 10,
121 .pressure_tap_threshold = 120,
122 .scr_threshold = 20,
123 .max_finger_diameter = 1900,
124 .max_scroll_finger_distance = 8192,
125 .max_double_tap_distance = 2500,
126 .enable_single_tap_clicks = 1,
127 .enable_single_tap_movement = 1,
128 };
129
130 static void
wsp_running_rangecheck(struct wsp_tuning * ptun)131 wsp_running_rangecheck(struct wsp_tuning *ptun)
132 {
133 WSP_CLAMP(ptun->scale_factor, 1, 63);
134 WSP_CLAMP(ptun->scroll_finger_count, 0, 3);
135 WSP_CLAMP(ptun->horizontal_swipe_finger_count, 0, 3);
136 WSP_CLAMP(ptun->z_factor, 0, 63);
137 WSP_CLAMP(ptun->z_invert, 0, 1);
138 WSP_CLAMP(ptun->t_factor, 0, 63);
139 WSP_CLAMP(ptun->t_invert, 0, 1);
140 WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255);
141 WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255);
142 WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255);
143 WSP_CLAMP(ptun->max_finger_diameter, 1, 2400);
144 WSP_CLAMP(ptun->max_scroll_finger_distance, 1, 16384);
145 WSP_CLAMP(ptun->max_double_tap_distance, 1, 16384);
146 WSP_CLAMP(ptun->scr_threshold, 1, 255);
147 WSP_CLAMP(ptun->enable_single_tap_clicks, 0, 1);
148 WSP_CLAMP(ptun->enable_single_tap_movement, 0, 1);
149 }
150
151 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RWTUN,
152 &wsp_tuning.scale_factor, 0, "movement scale factor");
153 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scroll_finger_count, CTLFLAG_RWTUN,
154 &wsp_tuning.scroll_finger_count, 0, "amount of fingers to use scrolling gesture");
155 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, horizontal_swipe_finger_count, CTLFLAG_RWTUN,
156 &wsp_tuning.horizontal_swipe_finger_count, 0, "amount of fingers to use horizontal swipe gesture");
157 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RWTUN,
158 &wsp_tuning.z_factor, 0, "Z-axis (vertical) scale factor");
159 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_invert, CTLFLAG_RWTUN,
160 &wsp_tuning.z_invert, 0, "enable (vertical) Z-axis inversion");
161 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, t_factor, CTLFLAG_RWTUN,
162 &wsp_tuning.t_factor, 0, "T-axis (horizontal) scale factor");
163 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, t_invert, CTLFLAG_RWTUN,
164 &wsp_tuning.t_invert, 0, "enable T-axis (horizontal) inversion");
165 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RWTUN,
166 &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold");
167 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RWTUN,
168 &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold");
169 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RWTUN,
170 &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold");
171 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, max_finger_diameter, CTLFLAG_RWTUN,
172 &wsp_tuning.max_finger_diameter, 0, "maximum finger diameter");
173 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, max_scroll_finger_distance, CTLFLAG_RWTUN,
174 &wsp_tuning.max_scroll_finger_distance, 0, "maximum scroll finger distance");
175 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, max_double_tap_distance, CTLFLAG_RWTUN,
176 &wsp_tuning.max_double_tap_distance, 0, "maximum double-finger click distance");
177 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_threshold, CTLFLAG_RWTUN,
178 &wsp_tuning.scr_threshold, 0, "scrolling threshold");
179 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, enable_single_tap_clicks, CTLFLAG_RWTUN,
180 &wsp_tuning.enable_single_tap_clicks, 0, "enable single tap clicks");
181 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, enable_single_tap_movement, CTLFLAG_RWTUN,
182 &wsp_tuning.enable_single_tap_movement, 0, "enable single tap movement");
183
184
185 /*
186 * Some tables, structures, definitions and constant values for the
187 * touchpad protocol has been copied from Linux's
188 * "drivers/input/mouse/bcm5974.c" which has the following copyright
189 * holders under GPLv2. All device specific code in this driver has
190 * been written from scratch. The decoding algorithm is based on
191 * output from FreeBSD's usbdump.
192 *
193 * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
194 * Copyright (C) 2008 Scott Shawcroft (scott.shawcroft@gmail.com)
195 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
196 * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
197 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
198 * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
199 * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
200 * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
201 * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
202 */
203
204 /* button data structure */
205 struct bt_data {
206 uint8_t unknown1; /* constant */
207 uint8_t button; /* left button */
208 uint8_t rel_x; /* relative x coordinate */
209 uint8_t rel_y; /* relative y coordinate */
210 } __packed;
211
212 /* trackpad header types */
213 enum tp_type {
214 TYPE1, /* plain trackpad */
215 TYPE2, /* button integrated in trackpad */
216 TYPE3, /* additional header fields since June 2013 */
217 TYPE4, /* additional header field for pressure data */
218 TYPE_CNT
219 };
220
221 /* trackpad finger data offsets, le16-aligned */
222 #define FINGER_TYPE1 (13 * 2)
223 #define FINGER_TYPE2 (15 * 2)
224 #define FINGER_TYPE3 (19 * 2)
225 #define FINGER_TYPE4 (23 * 2)
226
227 /* trackpad button data offsets */
228 #define BUTTON_TYPE2 15
229 #define BUTTON_TYPE3 23
230 #define BUTTON_TYPE4 31
231
232 /* list of device capability bits */
233 #define HAS_INTEGRATED_BUTTON 1
234 #define SUPPORTS_FORCETOUCH 2
235
236 /* trackpad finger data block size */
237 #define FSIZE_TYPE1 (14 * 2)
238 #define FSIZE_TYPE2 (14 * 2)
239 #define FSIZE_TYPE3 (14 * 2)
240 #define FSIZE_TYPE4 (15 * 2)
241
242 struct wsp_tp {
243 uint8_t caps; /* device capability bitmask */
244 uint8_t button; /* offset to button data */
245 uint8_t offset; /* offset to trackpad finger data */
246 uint8_t fsize; /* bytes in single finger block */
247 uint8_t delta; /* offset from header to finger struct */
248 uint8_t iface_index;
249 uint8_t um_size; /* usb control message length */
250 uint8_t um_req_idx; /* usb control message index */
251 uint8_t um_switch_idx; /* usb control message mode switch index */
252 uint8_t um_switch_on; /* usb control message mode switch on */
253 uint8_t um_switch_off; /* usb control message mode switch off */
254 } const static wsp_tp[TYPE_CNT] = {
255 [TYPE1] = {
256 .caps = 0,
257 .button = 0,
258 .offset = FINGER_TYPE1,
259 .fsize = FSIZE_TYPE1,
260 .delta = 0,
261 .iface_index = 0,
262 .um_size = 8,
263 .um_req_idx = 0x00,
264 .um_switch_idx = 0,
265 .um_switch_on = 0x01,
266 .um_switch_off = 0x08,
267 },
268 [TYPE2] = {
269 .caps = HAS_INTEGRATED_BUTTON,
270 .button = BUTTON_TYPE2,
271 .offset = FINGER_TYPE2,
272 .fsize = FSIZE_TYPE2,
273 .delta = 0,
274 .iface_index = 0,
275 .um_size = 8,
276 .um_req_idx = 0x00,
277 .um_switch_idx = 0,
278 .um_switch_on = 0x01,
279 .um_switch_off = 0x08,
280 },
281 [TYPE3] = {
282 .caps = HAS_INTEGRATED_BUTTON,
283 .button = BUTTON_TYPE3,
284 .offset = FINGER_TYPE3,
285 .fsize = FSIZE_TYPE3,
286 .delta = 0,
287 },
288 [TYPE4] = {
289 .caps = HAS_INTEGRATED_BUTTON | SUPPORTS_FORCETOUCH,
290 .button = BUTTON_TYPE4,
291 .offset = FINGER_TYPE4,
292 .fsize = FSIZE_TYPE4,
293 .delta = 2,
294 .iface_index = 2,
295 .um_size = 2,
296 .um_req_idx = 0x02,
297 .um_switch_idx = 1,
298 .um_switch_on = 0x01,
299 .um_switch_off = 0x00,
300 },
301 };
302
303 /* trackpad finger header - little endian */
304 struct tp_header {
305 uint8_t flag;
306 uint8_t sn0;
307 uint16_t wFixed0;
308 uint32_t dwSn1;
309 uint32_t dwFixed1;
310 uint16_t wLength;
311 uint8_t nfinger;
312 uint8_t ibt;
313 int16_t wUnknown[6];
314 uint8_t q1;
315 uint8_t q2;
316 } __packed;
317
318 /* trackpad finger structure - little endian */
319 struct tp_finger {
320 int16_t origin; /* zero when switching track finger */
321 int16_t abs_x; /* absolute x coodinate */
322 int16_t abs_y; /* absolute y coodinate */
323 int16_t rel_x; /* relative x coodinate */
324 int16_t rel_y; /* relative y coodinate */
325 int16_t tool_major; /* tool area, major axis */
326 int16_t tool_minor; /* tool area, minor axis */
327 int16_t orientation; /* 16384 when point, else 15 bit angle */
328 int16_t touch_major; /* touch area, major axis */
329 int16_t touch_minor; /* touch area, minor axis */
330 int16_t unused[2]; /* zeros */
331 int16_t pressure; /* pressure on forcetouch touchpad */
332 int16_t multi; /* one finger: varies, more fingers:
333 * constant */
334 } __packed;
335
336 /* trackpad finger data size, empirically at least ten fingers */
337 #ifdef EVDEV_SUPPORT
338 #define MAX_FINGERS MAX_MT_SLOTS
339 #else
340 #define MAX_FINGERS 16
341 #endif
342 #define SIZEOF_FINGER sizeof(struct tp_finger)
343 #define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER)
344 #define MAX_FINGER_ORIENTATION 16384
345
346 #if (WSP_BUFFER_MAX < ((MAX_FINGERS * FSIZE_TYPE4) + FINGER_TYPE4))
347 #error "WSP_BUFFER_MAX is too small"
348 #endif
349
350 enum {
351 WSP_FLAG_WELLSPRING1,
352 WSP_FLAG_WELLSPRING2,
353 WSP_FLAG_WELLSPRING3,
354 WSP_FLAG_WELLSPRING4,
355 WSP_FLAG_WELLSPRING4A,
356 WSP_FLAG_WELLSPRING5,
357 WSP_FLAG_WELLSPRING6A,
358 WSP_FLAG_WELLSPRING6,
359 WSP_FLAG_WELLSPRING5A,
360 WSP_FLAG_WELLSPRING7,
361 WSP_FLAG_WELLSPRING7A,
362 WSP_FLAG_WELLSPRING8,
363 WSP_FLAG_WELLSPRING9,
364 WSP_FLAG_MAX,
365 };
366
367 /* device-specific parameters */
368 struct wsp_param {
369 int snratio; /* signal-to-noise ratio */
370 int min; /* device minimum reading */
371 int max; /* device maximum reading */
372 int size; /* physical size, mm */
373 };
374
375 /* device-specific configuration */
376 struct wsp_dev_params {
377 const struct wsp_tp* tp;
378 struct wsp_param p; /* finger pressure limits */
379 struct wsp_param w; /* finger width limits */
380 struct wsp_param x; /* horizontal limits */
381 struct wsp_param y; /* vertical limits */
382 struct wsp_param o; /* orientation limits */
383 };
384
385 /* logical signal quality */
386 #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
387 #define SN_WIDTH 25 /* width signal-to-noise ratio */
388 #define SN_COORD 250 /* coordinate signal-to-noise ratio */
389 #define SN_ORIENT 10 /* orientation signal-to-noise ratio */
390
391 static const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = {
392 [WSP_FLAG_WELLSPRING1] = {
393 .tp = wsp_tp + TYPE1,
394 .p = { SN_PRESSURE, 0, 256, 0 },
395 .w = { SN_WIDTH, 0, 2048, 0 },
396 .x = { SN_COORD, -4824, 5342, 105 },
397 .y = { SN_COORD, -172, 5820, 75 },
398 .o = { SN_ORIENT,
399 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
400 },
401 [WSP_FLAG_WELLSPRING2] = {
402 .tp = wsp_tp + TYPE1,
403 .p = { SN_PRESSURE, 0, 256, 0 },
404 .w = { SN_WIDTH, 0, 2048, 0 },
405 .x = { SN_COORD, -4824, 4824, 105 },
406 .y = { SN_COORD, -172, 4290, 75 },
407 .o = { SN_ORIENT,
408 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
409 },
410 [WSP_FLAG_WELLSPRING3] = {
411 .tp = wsp_tp + TYPE2,
412 .p = { SN_PRESSURE, 0, 300, 0 },
413 .w = { SN_WIDTH, 0, 2048, 0 },
414 .x = { SN_COORD, -4460, 5166, 105 },
415 .y = { SN_COORD, -75, 6700, 75 },
416 .o = { SN_ORIENT,
417 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
418 },
419 [WSP_FLAG_WELLSPRING4] = {
420 .tp = wsp_tp + TYPE2,
421 .p = { SN_PRESSURE, 0, 300, 0 },
422 .w = { SN_WIDTH, 0, 2048, 0 },
423 .x = { SN_COORD, -4620, 5140, 105 },
424 .y = { SN_COORD, -150, 6600, 75 },
425 .o = { SN_ORIENT,
426 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
427 },
428 [WSP_FLAG_WELLSPRING4A] = {
429 .tp = wsp_tp + TYPE2,
430 .p = { SN_PRESSURE, 0, 300, 0 },
431 .w = { SN_WIDTH, 0, 2048, 0 },
432 .x = { SN_COORD, -4616, 5112, 105 },
433 .y = { SN_COORD, -142, 5234, 75 },
434 .o = { SN_ORIENT,
435 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
436 },
437 [WSP_FLAG_WELLSPRING5] = {
438 .tp = wsp_tp + TYPE2,
439 .p = { SN_PRESSURE, 0, 300, 0 },
440 .w = { SN_WIDTH, 0, 2048, 0 },
441 .x = { SN_COORD, -4415, 5050, 105 },
442 .y = { SN_COORD, -55, 6680, 75 },
443 .o = { SN_ORIENT,
444 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
445 },
446 [WSP_FLAG_WELLSPRING6] = {
447 .tp = wsp_tp + TYPE2,
448 .p = { SN_PRESSURE, 0, 300, 0 },
449 .w = { SN_WIDTH, 0, 2048, 0 },
450 .x = { SN_COORD, -4620, 5140, 105 },
451 .y = { SN_COORD, -150, 6600, 75 },
452 .o = { SN_ORIENT,
453 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
454 },
455 [WSP_FLAG_WELLSPRING5A] = {
456 .tp = wsp_tp + TYPE2,
457 .p = { SN_PRESSURE, 0, 300, 0 },
458 .w = { SN_WIDTH, 0, 2048, 0 },
459 .x = { SN_COORD, -4750, 5280, 105 },
460 .y = { SN_COORD, -150, 6730, 75 },
461 .o = { SN_ORIENT,
462 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
463 },
464 [WSP_FLAG_WELLSPRING6A] = {
465 .tp = wsp_tp + TYPE2,
466 .p = { SN_PRESSURE, 0, 300, 0 },
467 .w = { SN_WIDTH, 0, 2048, 0 },
468 .x = { SN_COORD, -4620, 5140, 105 },
469 .y = { SN_COORD, -150, 6600, 75 },
470 .o = { SN_ORIENT,
471 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
472 },
473 [WSP_FLAG_WELLSPRING7] = {
474 .tp = wsp_tp + TYPE2,
475 .p = { SN_PRESSURE, 0, 300, 0 },
476 .w = { SN_WIDTH, 0, 2048, 0 },
477 .x = { SN_COORD, -4750, 5280, 105 },
478 .y = { SN_COORD, -150, 6730, 75 },
479 .o = { SN_ORIENT,
480 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
481 },
482 [WSP_FLAG_WELLSPRING7A] = {
483 .tp = wsp_tp + TYPE2,
484 .p = { SN_PRESSURE, 0, 300, 0 },
485 .w = { SN_WIDTH, 0, 2048, 0 },
486 .x = { SN_COORD, -4750, 5280, 105 },
487 .y = { SN_COORD, -150, 6730, 75 },
488 .o = { SN_ORIENT,
489 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
490 },
491 [WSP_FLAG_WELLSPRING8] = {
492 .tp = wsp_tp + TYPE3,
493 .p = { SN_PRESSURE, 0, 300, 0 },
494 .w = { SN_WIDTH, 0, 2048, 0 },
495 .x = { SN_COORD, -4620, 5140, 105 },
496 .y = { SN_COORD, -150, 6600, 75 },
497 .o = { SN_ORIENT,
498 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
499 },
500 [WSP_FLAG_WELLSPRING9] = {
501 .tp = wsp_tp + TYPE4,
502 .p = { SN_PRESSURE, 0, 300, 0 },
503 .w = { SN_WIDTH, 0, 2048, 0 },
504 .x = { SN_COORD, -4828, 5345, 105 },
505 .y = { SN_COORD, -203, 6803, 75 },
506 .o = { SN_ORIENT,
507 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
508 },
509 };
510 #define WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
511
512 static const STRUCT_USB_HOST_ID wsp_devs[] = {
513 /* MacbookAir1.1 */
514 WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1),
515 WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1),
516 WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1),
517
518 /* MacbookProPenryn, aka wellspring2 */
519 WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2),
520 WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2),
521 WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2),
522
523 /* Macbook5,1 (unibody), aka wellspring3 */
524 WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3),
525 WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3),
526 WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3),
527
528 /* MacbookAir3,2 (unibody), aka wellspring4 */
529 WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4),
530 WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4),
531 WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4),
532
533 /* MacbookAir3,1 (unibody), aka wellspring4 */
534 WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A),
535 WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A),
536 WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A),
537
538 /* Macbook8 (unibody, March 2011) */
539 WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5),
540 WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5),
541 WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5),
542
543 /* MacbookAir4,1 (unibody, July 2011) */
544 WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A),
545 WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A),
546 WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A),
547
548 /* MacbookAir4,2 (unibody, July 2011) */
549 WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6),
550 WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6),
551 WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6),
552
553 /* Macbook8,2 (unibody) */
554 WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A),
555 WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A),
556 WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A),
557
558 /* MacbookPro10,1 (unibody, June 2012) */
559 /* MacbookPro11,1-3 (unibody, June 2013) */
560 WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7),
561 WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7),
562 WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7),
563
564 /* MacbookPro10,2 (unibody, October 2012) */
565 WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A),
566 WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A),
567 WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A),
568
569 /* MacbookAir6,2 (unibody, June 2013) */
570 WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8),
571 WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8),
572 WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8),
573
574 /* MacbookPro12,1 MacbookPro11,4 */
575 WSP_DEV(APPLE, WELLSPRING9_ANSI, WSP_FLAG_WELLSPRING9),
576 WSP_DEV(APPLE, WELLSPRING9_ISO, WSP_FLAG_WELLSPRING9),
577 WSP_DEV(APPLE, WELLSPRING9_JIS, WSP_FLAG_WELLSPRING9),
578 };
579
580 #define WSP_FIFO_BUF_SIZE 8 /* bytes */
581 #define WSP_FIFO_QUEUE_MAXLEN 50 /* units */
582
583 enum {
584 WSP_INTR_DT,
585 WSP_N_TRANSFER,
586 };
587
588 struct wsp_softc {
589 struct usb_device *sc_usb_device;
590 struct mtx sc_mutex; /* for synchronization */
591 struct usb_xfer *sc_xfer[WSP_N_TRANSFER];
592 struct usb_fifo_sc sc_fifo;
593
594 const struct wsp_dev_params *sc_params; /* device configuration */
595
596 #ifdef EVDEV_SUPPORT
597 struct evdev_dev *sc_evdev;
598 #endif
599 mousehw_t sc_hw;
600 mousemode_t sc_mode;
601 u_int sc_pollrate;
602 mousestatus_t sc_status;
603 int sc_fflags;
604 u_int sc_state;
605 #define WSP_ENABLED 0x01
606 #define WSP_EVDEV_OPENED 0x02
607
608 struct tp_finger *index[MAX_FINGERS]; /* finger index data */
609 int16_t pos_x[MAX_FINGERS]; /* position array */
610 int16_t pos_y[MAX_FINGERS]; /* position array */
611 int16_t pre_pos_x[MAX_FINGERS]; /* previous position array */
612 int16_t pre_pos_y[MAX_FINGERS]; /* previous position array */
613 u_int sc_touch; /* touch status */
614 #define WSP_UNTOUCH 0x00
615 #define WSP_FIRST_TOUCH 0x01
616 #define WSP_SECOND_TOUCH 0x02
617 #define WSP_TOUCHING 0x04
618 int dx_sum; /* x axis cumulative movement */
619 int dy_sum; /* y axis cumulative movement */
620 int dz_sum; /* z axis cumulative movement */
621 int dz_count;
622 #define WSP_DZ_MAX_COUNT 32
623 int dt_sum; /* T-axis cumulative movement */
624 int rdx; /* x axis remainder of divide by scale_factor */
625 int rdy; /* y axis remainder of divide by scale_factor */
626 int rdz; /* z axis remainder of divide by scale_factor */
627 int tp_datalen;
628 uint8_t o_ntouch; /* old touch finger status */
629 uint8_t finger; /* 0 or 1 *, check which finger moving */
630 uint16_t intr_count;
631 #define WSP_TAP_THRESHOLD 3
632 #define WSP_TAP_MAX_COUNT 20
633 int distance; /* the distance of 2 fingers */
634 uint8_t ibtn; /* button status in tapping */
635 uint8_t ntaps; /* finger status in tapping */
636 uint8_t scr_mode; /* scroll status in movement */
637 #define WSP_SCR_NONE 0
638 #define WSP_SCR_VER 1
639 #define WSP_SCR_HOR 2
640 uint8_t tp_data[WSP_BUFFER_MAX] __aligned(4); /* trackpad transferred data */
641 };
642
643 /*
644 * function prototypes
645 */
646 static usb_fifo_cmd_t wsp_fifo_start_read;
647 static usb_fifo_cmd_t wsp_fifo_stop_read;
648 static usb_fifo_open_t wsp_open;
649 static usb_fifo_close_t wsp_close;
650 static usb_fifo_ioctl_t wsp_ioctl;
651
652 static struct usb_fifo_methods wsp_fifo_methods = {
653 .f_open = &wsp_open,
654 .f_close = &wsp_close,
655 .f_ioctl = &wsp_ioctl,
656 .f_start_read = &wsp_fifo_start_read,
657 .f_stop_read = &wsp_fifo_stop_read,
658 .basename[0] = WSP_DRIVER_NAME,
659 };
660
661 #ifdef EVDEV_SUPPORT
662 static evdev_open_t wsp_ev_open;
663 static evdev_close_t wsp_ev_close;
664 static const struct evdev_methods wsp_evdev_methods = {
665 .ev_open = &wsp_ev_open,
666 .ev_close = &wsp_ev_close,
667 };
668 #endif
669
670 /* device initialization and shutdown */
671 static int wsp_enable(struct wsp_softc *sc);
672 static void wsp_disable(struct wsp_softc *sc);
673
674 /* updating fifo */
675 static void wsp_reset_buf(struct wsp_softc *sc);
676 static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t);
677
678 /* Device methods. */
679 static device_probe_t wsp_probe;
680 static device_attach_t wsp_attach;
681 static device_detach_t wsp_detach;
682 static usb_callback_t wsp_intr_callback;
683
684 static const struct usb_config wsp_config[WSP_N_TRANSFER] = {
685 [WSP_INTR_DT] = {
686 .type = UE_INTERRUPT,
687 .endpoint = UE_ADDR_ANY,
688 .direction = UE_DIR_IN,
689 .flags = {
690 .pipe_bof = 0,
691 .short_xfer_ok = 1,
692 },
693 .bufsize = WSP_BUFFER_MAX,
694 .callback = &wsp_intr_callback,
695 },
696 };
697
698 static usb_error_t
wsp_set_device_mode(struct wsp_softc * sc,uint8_t on)699 wsp_set_device_mode(struct wsp_softc *sc, uint8_t on)
700 {
701 const struct wsp_dev_params *params = sc->sc_params;
702 uint8_t mode_bytes[8];
703 usb_error_t err;
704
705 /* Type 3 does not require a mode switch */
706 if (params->tp == wsp_tp + TYPE3)
707 return 0;
708
709 err = usbd_req_get_report(sc->sc_usb_device, NULL,
710 mode_bytes, params->tp->um_size, params->tp->iface_index,
711 UHID_FEATURE_REPORT, params->tp->um_req_idx);
712
713 if (err != USB_ERR_NORMAL_COMPLETION) {
714 DPRINTF("Failed to read device mode (%d)\n", err);
715 return (err);
716 }
717
718 /*
719 * XXX Need to wait at least 250ms for hardware to get
720 * ready. The device mode handling appears to be handled
721 * asynchronously and we should not issue these commands too
722 * quickly.
723 */
724 pause("WHW", hz / 4);
725
726 mode_bytes[params->tp->um_switch_idx] =
727 on ? params->tp->um_switch_on : params->tp->um_switch_off;
728
729 return (usbd_req_set_report(sc->sc_usb_device, NULL,
730 mode_bytes, params->tp->um_size, params->tp->iface_index,
731 UHID_FEATURE_REPORT, params->tp->um_req_idx));
732 }
733
734 static int
wsp_enable(struct wsp_softc * sc)735 wsp_enable(struct wsp_softc *sc)
736 {
737 /* reset status */
738 memset(&sc->sc_status, 0, sizeof(sc->sc_status));
739 sc->sc_state |= WSP_ENABLED;
740
741 DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n");
742 return (0);
743 }
744
745 static void
wsp_disable(struct wsp_softc * sc)746 wsp_disable(struct wsp_softc *sc)
747 {
748 sc->sc_state &= ~WSP_ENABLED;
749 DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n");
750 }
751
752 static int
wsp_probe(device_t self)753 wsp_probe(device_t self)
754 {
755 struct usb_attach_arg *uaa = device_get_ivars(self);
756 struct usb_interface_descriptor *id;
757 struct usb_interface *iface;
758 uint8_t i;
759
760 if (uaa->usb_mode != USB_MODE_HOST)
761 return (ENXIO);
762
763 /* figure out first interface matching */
764 for (i = 1;; i++) {
765 iface = usbd_get_iface(uaa->device, i);
766 if (iface == NULL || i == 3)
767 return (ENXIO);
768 id = iface->idesc;
769 if ((id == NULL) ||
770 (id->bInterfaceClass != UICLASS_HID) ||
771 (id->bInterfaceProtocol != 0 &&
772 id->bInterfaceProtocol != UIPROTO_MOUSE))
773 continue;
774 break;
775 }
776 /* check if we are attaching to the first match */
777 if (uaa->info.bIfaceIndex != i)
778 return (ENXIO);
779 if (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa) != 0)
780 return (ENXIO);
781
782 return (BUS_PROBE_DEFAULT);
783 }
784
785 static int
wsp_attach(device_t dev)786 wsp_attach(device_t dev)
787 {
788 struct wsp_softc *sc = device_get_softc(dev);
789 struct usb_attach_arg *uaa = device_get_ivars(dev);
790 usb_error_t err;
791 void *d_ptr = NULL;
792 uint16_t d_len;
793
794 DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc);
795
796 /* Get HID descriptor */
797 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
798 &d_len, M_TEMP, uaa->info.bIfaceIndex);
799
800 if (err == USB_ERR_NORMAL_COMPLETION) {
801 /* Get HID report descriptor length */
802 sc->tp_datalen = hid_report_size_max(d_ptr, d_len, hid_input,
803 NULL);
804 free(d_ptr, M_TEMP);
805
806 if (sc->tp_datalen <= 0 || sc->tp_datalen > WSP_BUFFER_MAX) {
807 DPRINTF("Invalid datalength or too big "
808 "datalength: %d\n", sc->tp_datalen);
809 return (ENXIO);
810 }
811 } else {
812 return (ENXIO);
813 }
814
815 sc->sc_usb_device = uaa->device;
816
817 /* get device specific configuration */
818 sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa);
819
820 /*
821 * By default the touchpad behaves like a HID device, sending
822 * packets with reportID = 8. Such reports contain only
823 * limited information. They encode movement deltas and button
824 * events, but do not include data from the pressure
825 * sensors. The device input mode can be switched from HID
826 * reports to raw sensor data using vendor-specific USB
827 * control commands:
828 */
829
830 /*
831 * During re-enumeration of the device we need to force the
832 * device back into HID mode before switching it to RAW
833 * mode. Else the device does not work like expected.
834 */
835 err = wsp_set_device_mode(sc, 0);
836 if (err != USB_ERR_NORMAL_COMPLETION) {
837 DPRINTF("Failed to set mode to HID MODE (%d)\n", err);
838 return (ENXIO);
839 }
840
841 err = wsp_set_device_mode(sc, 1);
842 if (err != USB_ERR_NORMAL_COMPLETION) {
843 DPRINTF("failed to set mode to RAW MODE (%d)\n", err);
844 return (ENXIO);
845 }
846
847 mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE);
848
849 err = usbd_transfer_setup(uaa->device,
850 &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config,
851 WSP_N_TRANSFER, sc, &sc->sc_mutex);
852 if (err) {
853 DPRINTF("error=%s\n", usbd_errstr(err));
854 goto detach;
855 }
856 if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex,
857 &wsp_fifo_methods, &sc->sc_fifo,
858 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
859 UID_ROOT, GID_OPERATOR, 0644)) {
860 goto detach;
861 }
862 device_set_usb_desc(dev);
863
864 sc->sc_hw.buttons = 3;
865 sc->sc_hw.iftype = MOUSE_IF_USB;
866 sc->sc_hw.type = MOUSE_PAD;
867 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
868 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
869 sc->sc_mode.rate = -1;
870 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
871 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
872 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
873 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
874
875 sc->sc_touch = WSP_UNTOUCH;
876 sc->scr_mode = WSP_SCR_NONE;
877
878 #ifdef EVDEV_SUPPORT
879 sc->sc_evdev = evdev_alloc();
880 evdev_set_name(sc->sc_evdev, device_get_desc(dev));
881 evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
882 evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
883 uaa->info.idProduct, 0);
884 evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
885 evdev_set_methods(sc->sc_evdev, sc, &wsp_evdev_methods);
886 evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
887 evdev_support_event(sc->sc_evdev, EV_SYN);
888 evdev_support_event(sc->sc_evdev, EV_ABS);
889 evdev_support_event(sc->sc_evdev, EV_KEY);
890
891 #define WSP_SUPPORT_ABS(evdev, code, param) \
892 evdev_support_abs((evdev), (code), (param).min, (param).max, \
893 ((param).max - (param).min) / (param).snratio, 0, \
894 (param).size != 0 ? ((param).max - (param).min) / (param).size : 0);
895
896 /* finger position */
897 WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_POSITION_X, sc->sc_params->x);
898 WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_POSITION_Y, sc->sc_params->y);
899 /* finger pressure */
900 if ((sc->sc_params->tp->caps & SUPPORTS_FORCETOUCH) != 0)
901 WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_PRESSURE, sc->sc_params->p);
902 /* finger major/minor axis */
903 WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_TOUCH_MAJOR, sc->sc_params->w);
904 WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_TOUCH_MINOR, sc->sc_params->w);
905 /* finger major/minor approach */
906 WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_WIDTH_MAJOR, sc->sc_params->w);
907 WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_WIDTH_MINOR, sc->sc_params->w);
908 /* finger orientation */
909 WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_ORIENTATION, sc->sc_params->o);
910 /* button properties */
911 evdev_support_key(sc->sc_evdev, BTN_LEFT);
912 if ((sc->sc_params->tp->caps & HAS_INTEGRATED_BUTTON) != 0)
913 evdev_support_prop(sc->sc_evdev, INPUT_PROP_BUTTONPAD);
914 /* Enable automatic touch assignment for type B MT protocol */
915 evdev_support_abs(sc->sc_evdev, ABS_MT_SLOT,
916 0, MAX_FINGERS - 1, 0, 0, 0);
917 evdev_support_abs(sc->sc_evdev, ABS_MT_TRACKING_ID,
918 -1, MAX_FINGERS - 1, 0, 0, 0);
919 evdev_set_flag(sc->sc_evdev, EVDEV_FLAG_MT_TRACK);
920 evdev_set_flag(sc->sc_evdev, EVDEV_FLAG_MT_AUTOREL);
921 /* Synaptics compatibility events */
922 evdev_set_flag(sc->sc_evdev, EVDEV_FLAG_MT_STCOMPAT);
923
924 err = evdev_register(sc->sc_evdev);
925 if (err)
926 goto detach;
927 #endif
928
929 return (0);
930
931 detach:
932 wsp_detach(dev);
933 return (ENOMEM);
934 }
935
936 static int
wsp_detach(device_t dev)937 wsp_detach(device_t dev)
938 {
939 struct wsp_softc *sc = device_get_softc(dev);
940
941 (void) wsp_set_device_mode(sc, 0);
942
943 mtx_lock(&sc->sc_mutex);
944 if (sc->sc_state & WSP_ENABLED)
945 wsp_disable(sc);
946 mtx_unlock(&sc->sc_mutex);
947
948 usb_fifo_detach(&sc->sc_fifo);
949
950 #ifdef EVDEV_SUPPORT
951 evdev_free(sc->sc_evdev);
952 #endif
953
954 usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER);
955
956 mtx_destroy(&sc->sc_mutex);
957
958 return (0);
959 }
960
961 static void
wsp_intr_callback(struct usb_xfer * xfer,usb_error_t error)962 wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error)
963 {
964 struct wsp_softc *sc = usbd_xfer_softc(xfer);
965 const struct wsp_dev_params *params = sc->sc_params;
966 struct usb_page_cache *pc;
967 struct tp_finger *f;
968 struct wsp_tuning tun = wsp_tuning;
969 int ntouch = 0; /* the finger number in touch */
970 int ibt = 0; /* button status */
971 int dx = 0;
972 int dy = 0;
973 int dz = 0;
974 int rdx = 0;
975 int rdy = 0;
976 int rdz = 0;
977 int len;
978 int i;
979 #ifdef EVDEV_SUPPORT
980 int slot = 0;
981 #endif
982
983 wsp_running_rangecheck(&tun);
984
985 if (sc->dz_count == 0)
986 sc->dz_count = WSP_DZ_MAX_COUNT;
987
988 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
989
990 switch (USB_GET_STATE(xfer)) {
991 case USB_ST_TRANSFERRED:
992
993 /* copy out received data */
994 pc = usbd_xfer_get_frame(xfer, 0);
995 usbd_copy_out(pc, 0, sc->tp_data, len);
996
997 if ((len < params->tp->offset + params->tp->fsize) ||
998 ((len - params->tp->offset) % params->tp->fsize) != 0) {
999 DPRINTFN(WSP_LLEVEL_INFO, "Invalid length: %d, %x, %x\n",
1000 len, sc->tp_data[0], sc->tp_data[1]);
1001 goto tr_setup;
1002 }
1003
1004 if (len < sc->tp_datalen) {
1005 /* make sure we don't process old data */
1006 memset(sc->tp_data + len, 0, sc->tp_datalen - len);
1007 }
1008
1009 if (params->tp != wsp_tp + TYPE1) {
1010 ibt = sc->tp_data[params->tp->button];
1011 ntouch = sc->tp_data[params->tp->button - 1];
1012 } else
1013 ntouch = (len - params->tp->offset) / params->tp->fsize;
1014
1015 /* range check */
1016 if (ntouch < 0)
1017 ntouch = 0;
1018 else if (ntouch > MAX_FINGERS)
1019 ntouch = MAX_FINGERS;
1020
1021 for (i = 0; i != ntouch; i++) {
1022 f = (struct tp_finger *)(sc->tp_data + params->tp->offset + params->tp->delta + i * params->tp->fsize);
1023 /* swap endianness, if any */
1024 if (le16toh(0x1234) != 0x1234) {
1025 f->origin = le16toh((uint16_t)f->origin);
1026 f->abs_x = le16toh((uint16_t)f->abs_x);
1027 f->abs_y = le16toh((uint16_t)f->abs_y);
1028 f->rel_x = le16toh((uint16_t)f->rel_x);
1029 f->rel_y = le16toh((uint16_t)f->rel_y);
1030 f->tool_major = le16toh((uint16_t)f->tool_major);
1031 f->tool_minor = le16toh((uint16_t)f->tool_minor);
1032 f->orientation = le16toh((uint16_t)f->orientation);
1033 f->touch_major = le16toh((uint16_t)f->touch_major);
1034 f->touch_minor = le16toh((uint16_t)f->touch_minor);
1035 f->pressure = le16toh((uint16_t)f->pressure);
1036 f->multi = le16toh((uint16_t)f->multi);
1037 }
1038 DPRINTFN(WSP_LLEVEL_INFO,
1039 "[%d]ibt=%d, taps=%d, o=%4d, ax=%5d, ay=%5d, "
1040 "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%4x, "
1041 "tchmaj=%4d, tchmin=%4d, presure=%4d, m=%4x\n",
1042 i, ibt, ntouch, f->origin, f->abs_x, f->abs_y,
1043 f->rel_x, f->rel_y, f->tool_major, f->tool_minor, f->orientation,
1044 f->touch_major, f->touch_minor, f->pressure, f->multi);
1045 sc->pos_x[i] = f->abs_x;
1046 sc->pos_y[i] = -f->abs_y;
1047 sc->index[i] = f;
1048 #ifdef EVDEV_SUPPORT
1049 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE && f->touch_major != 0) {
1050 union evdev_mt_slot slot_data = {
1051 .id = slot,
1052 .x = f->abs_x,
1053 .y = params->y.min + params->y.max - f->abs_y,
1054 .p = f->pressure,
1055 .maj = f->touch_major << 1,
1056 .min = f->touch_minor << 1,
1057 .w_maj = f->tool_major << 1,
1058 .w_min = f->tool_minor << 1,
1059 .ori = params->o.max - f->orientation,
1060 };
1061 evdev_mt_push_slot(sc->sc_evdev, slot, &slot_data);
1062 slot++;
1063 }
1064 #endif
1065 }
1066
1067 #ifdef EVDEV_SUPPORT
1068 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
1069 evdev_push_key(sc->sc_evdev, BTN_LEFT, ibt);
1070 evdev_sync(sc->sc_evdev);
1071 if ((sc->sc_fflags & FREAD) == 0 ||
1072 usb_fifo_put_bytes_max(
1073 sc->sc_fifo.fp[USB_FIFO_RX]) == 0)
1074 goto tr_setup;
1075 }
1076 #endif
1077 sc->sc_status.flags &= ~MOUSE_POSCHANGED;
1078 sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
1079 sc->sc_status.obutton = sc->sc_status.button;
1080 sc->sc_status.button = 0;
1081
1082 if (ntouch == 2) {
1083 sc->distance = max(sc->distance, max(
1084 abs(sc->pos_x[0] - sc->pos_x[1]),
1085 abs(sc->pos_y[0] - sc->pos_y[1])));
1086 }
1087
1088 if (ibt != 0) {
1089 if (params->tp->caps & HAS_INTEGRATED_BUTTON) {
1090 switch (ntouch) {
1091 case 1:
1092 sc->sc_status.button |= MOUSE_BUTTON1DOWN;
1093 break;
1094 case 2:
1095 if (sc->distance < tun.max_double_tap_distance && abs(sc->dx_sum) < 5 &&
1096 abs(sc->dy_sum) < 5)
1097 sc->sc_status.button |= MOUSE_BUTTON3DOWN;
1098 else
1099 sc->sc_status.button |= MOUSE_BUTTON1DOWN;
1100 break;
1101 case 3:
1102 sc->sc_status.button |= MOUSE_BUTTON2DOWN;
1103 break;
1104 default:
1105 break;
1106 }
1107 } else {
1108 sc->sc_status.button |= MOUSE_BUTTON1DOWN;
1109 }
1110
1111 sc->ibtn = 1;
1112 }
1113 sc->intr_count++;
1114
1115 if (sc->ntaps < ntouch) {
1116 switch (ntouch) {
1117 case 1:
1118 if (sc->index[0]->touch_major > tun.pressure_tap_threshold &&
1119 sc->index[0]->tool_major <= tun.max_finger_diameter)
1120 sc->ntaps = 1;
1121 break;
1122 case 2:
1123 if (sc->index[0]->touch_major > tun.pressure_tap_threshold-30 &&
1124 sc->index[1]->touch_major > tun.pressure_tap_threshold-30)
1125 sc->ntaps = 2;
1126 break;
1127 case 3:
1128 if (sc->index[0]->touch_major > tun.pressure_tap_threshold-40 &&
1129 sc->index[1]->touch_major > tun.pressure_tap_threshold-40 &&
1130 sc->index[2]->touch_major > tun.pressure_tap_threshold-40)
1131 sc->ntaps = 3;
1132 break;
1133 default:
1134 break;
1135 }
1136 }
1137
1138 if (sc->index[0]->touch_major < tun.pressure_untouch_threshold &&
1139 sc->sc_status.button == 0) {
1140 sc->sc_touch = WSP_UNTOUCH;
1141 if (sc->intr_count < WSP_TAP_MAX_COUNT &&
1142 sc->intr_count > WSP_TAP_THRESHOLD &&
1143 sc->ntaps && sc->ibtn == 0) {
1144 /*
1145 * Add a pair of events (button-down and
1146 * button-up).
1147 */
1148 switch (sc->ntaps) {
1149 case 1:
1150 if (!(params->tp->caps & HAS_INTEGRATED_BUTTON) || tun.enable_single_tap_clicks) {
1151 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
1152 DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
1153 }
1154 break;
1155 case 2:
1156 DPRINTFN(WSP_LLEVEL_INFO, "sum_x=%5d, sum_y=%5d\n",
1157 sc->dx_sum, sc->dy_sum);
1158 if (sc->distance < tun.max_double_tap_distance && abs(sc->dx_sum) < 5 &&
1159 abs(sc->dy_sum) < 5) {
1160 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
1161 DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
1162 }
1163 break;
1164 case 3:
1165 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
1166 break;
1167 default:
1168 /* we don't handle taps of more than three fingers */
1169 break;
1170 }
1171 wsp_add_to_queue(sc, 0, 0, 0, 0); /* button release */
1172 }
1173
1174 if (sc->scr_mode == WSP_SCR_HOR && sc->ntaps == tun.horizontal_swipe_finger_count
1175 && tun.horizontal_swipe_finger_count > 0 && (sc->dt_sum / tun.scr_threshold) != 0) {
1176 /*
1177 * translate T-axis swipe into button
1178 * presses 3 and 4 (forward/back)
1179 */
1180 if (sc->dt_sum > 0)
1181 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
1182 else if (sc->dt_sum < 0)
1183 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
1184 }
1185
1186 sc->dz_count = WSP_DZ_MAX_COUNT;
1187 sc->dz_sum = 0;
1188 sc->intr_count = 0;
1189 sc->ibtn = 0;
1190 sc->ntaps = 0;
1191 sc->finger = 0;
1192 sc->distance = 0;
1193 sc->dt_sum = 0;
1194 sc->dx_sum = 0;
1195 sc->dy_sum = 0;
1196 sc->rdx = 0;
1197 sc->rdy = 0;
1198 sc->rdz = 0;
1199 sc->scr_mode = WSP_SCR_NONE;
1200 } else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold &&
1201 sc->sc_touch == WSP_UNTOUCH) { /* ignore first touch */
1202 sc->sc_touch = WSP_FIRST_TOUCH;
1203 } else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold &&
1204 sc->sc_touch == WSP_FIRST_TOUCH) { /* ignore second touch */
1205 sc->sc_touch = WSP_SECOND_TOUCH;
1206 DPRINTFN(WSP_LLEVEL_INFO, "First pre_x[0]=%5d, pre_y[0]=%5d\n",
1207 sc->pre_pos_x[0], sc->pre_pos_y[0]);
1208 } else {
1209 if (sc->sc_touch == WSP_SECOND_TOUCH)
1210 sc->sc_touch = WSP_TOUCHING;
1211
1212 if (ntouch != 0 &&
1213 sc->index[0]->touch_major >= tun.pressure_touch_threshold) {
1214 dx = sc->pos_x[0] - sc->pre_pos_x[0];
1215 dy = sc->pos_y[0] - sc->pre_pos_y[0];
1216
1217 /* Optionally ignore movement during button is releasing */
1218 if (tun.enable_single_tap_movement != 1 && sc->ibtn != 0 && sc->sc_status.button == 0)
1219 dx = dy = 0;
1220
1221 /* Ignore movement if ntouch changed */
1222 if (sc->o_ntouch != ntouch)
1223 dx = dy = 0;
1224
1225 /* Ignore unexpected movement when typing (palm detection) */
1226 if (ntouch == 1 && sc->index[0]->tool_major > tun.max_finger_diameter)
1227 dx = dy = 0;
1228
1229 if (sc->ibtn != 0 && ntouch == 1 &&
1230 sc->intr_count < WSP_TAP_MAX_COUNT &&
1231 abs(sc->dx_sum) < 1 && abs(sc->dy_sum) < 1 )
1232 dx = dy = 0;
1233
1234 if (ntouch == 2 && sc->sc_status.button != 0) {
1235 dx = sc->pos_x[sc->finger] - sc->pre_pos_x[sc->finger];
1236 dy = sc->pos_y[sc->finger] - sc->pre_pos_y[sc->finger];
1237
1238 /*
1239 * Ignore movement of switch finger or
1240 * movement from ibt=0 to ibt=1
1241 */
1242 if (sc->index[0]->origin == 0 || sc->index[1]->origin == 0 ||
1243 sc->sc_status.obutton != sc->sc_status.button) {
1244 dx = dy = 0;
1245 sc->finger = 0;
1246 }
1247 if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) <
1248 (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) &&
1249 sc->finger == 0) {
1250 sc->sc_touch = WSP_SECOND_TOUCH;
1251 dx = dy = 0;
1252 sc->finger = 1;
1253 }
1254 if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) >=
1255 (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) &&
1256 sc->finger == 1) {
1257 sc->sc_touch = WSP_SECOND_TOUCH;
1258 dx = dy = 0;
1259 sc->finger = 0;
1260 }
1261 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
1262 dx, dy, sc->finger);
1263 }
1264 if (sc->dz_count--) {
1265 if (sc->scr_mode == WSP_SCR_HOR) {
1266 rdz = (dx + sc->rdz) % tun.scale_factor;
1267 sc->dz_sum -= (dx + sc->rdz) / tun.scale_factor;
1268 } else if (sc->scr_mode == WSP_SCR_VER) {
1269 rdz = (dy + sc->rdz) % tun.scale_factor;
1270 sc->dz_sum -= (dy + sc->rdz) / tun.scale_factor;
1271 }
1272 sc->rdz = rdz;
1273 }
1274 if (sc->scr_mode == WSP_SCR_VER && (tun.z_factor == 0 || (sc->dz_sum / tun.z_factor) != 0))
1275 sc->dz_count = 0;
1276 else if (sc->scr_mode == WSP_SCR_HOR && (tun.t_factor == 0 || (sc->dz_sum / tun.t_factor) != 0))
1277 sc->dz_count = 0;
1278 }
1279 rdx = (dx + sc->rdx) % tun.scale_factor;
1280 dx = (dx + sc->rdx) / tun.scale_factor;
1281 sc->rdx = rdx;
1282
1283 rdy = (dy + sc->rdy) % tun.scale_factor;
1284 dy = (dy + sc->rdy) / tun.scale_factor;
1285 sc->rdy = rdy;
1286
1287 sc->dx_sum += dx;
1288 sc->dy_sum += dy;
1289
1290 if (sc->sc_status.button == 0 && ntouch > 0) {
1291 if (ntouch == tun.scroll_finger_count || ntouch == tun.horizontal_swipe_finger_count) {
1292 if (sc->scr_mode == WSP_SCR_NONE && abs(sc->dx_sum) + abs(sc->dy_sum) > tun.scr_threshold)
1293 sc->scr_mode = abs(sc->dx_sum) > abs(sc->dy_sum) * 2 ? WSP_SCR_HOR : WSP_SCR_VER;
1294
1295 DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n", sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
1296 }
1297
1298 if (ntouch == tun.scroll_finger_count) { /* preference scrolling over swipe if tun.scroll_finger_count == tun.horizontal_swipe_finger_count */
1299 if (sc->scr_mode == WSP_SCR_HOR) {
1300 sc->sc_status.button = 1 << 5;
1301 }
1302 dx = dy = dz = 0;
1303 dz = 0;
1304 sc->dt_sum = 0;
1305 if (sc->distance <= tun.max_scroll_finger_distance && sc->dz_count == 0) {
1306 if (sc->scr_mode == WSP_SCR_VER) {
1307 if (tun.z_factor > 0)
1308 dz = (sc->dz_sum / tun.z_factor) * (tun.z_invert ? -1 : 1);
1309 } else if (sc->scr_mode == WSP_SCR_HOR) {
1310 if (tun.t_factor > 0)
1311 dz = (sc->dz_sum / tun.t_factor) * (tun.t_invert ? -1 : 1);
1312 }
1313 }
1314 } else if (ntouch == tun.horizontal_swipe_finger_count) {
1315 if (sc->scr_mode == WSP_SCR_HOR) {
1316 sc->dt_sum += dx * (tun.t_invert ? -1 : 1);
1317 } else {
1318 sc->dt_sum = 0;
1319 }
1320 dx = dy = dz = 0;
1321 }
1322 }
1323
1324 if (ntouch == 3)
1325 dx = dy = dz = 0;
1326
1327 if (ntouch != tun.horizontal_swipe_finger_count)
1328 sc->dt_sum = 0;
1329
1330 if (ntouch == 0)
1331 sc->scr_mode = WSP_SCR_NONE;
1332
1333 if (sc->intr_count < WSP_TAP_MAX_COUNT &&
1334 abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3)
1335 dx = dy = dz = 0;
1336 else
1337 sc->intr_count = WSP_TAP_MAX_COUNT;
1338 if (dx || dy || dz)
1339 sc->sc_status.flags |= MOUSE_POSCHANGED;
1340 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
1341 dx, dy, dz, sc->sc_touch, sc->sc_status.button);
1342 sc->sc_status.dx += dx;
1343 sc->sc_status.dy += dy;
1344 sc->sc_status.dz += dz;
1345
1346 wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
1347 if (sc->dz_count == 0) {
1348 sc->dz_sum = 0;
1349 sc->rdz = 0;
1350 }
1351 }
1352 sc->pre_pos_x[0] = sc->pos_x[0];
1353 sc->pre_pos_y[0] = sc->pos_y[0];
1354
1355 if (ntouch == 2 && sc->sc_status.button != 0) {
1356 sc->pre_pos_x[sc->finger] = sc->pos_x[sc->finger];
1357 sc->pre_pos_y[sc->finger] = sc->pos_y[sc->finger];
1358 }
1359 sc->o_ntouch = ntouch;
1360
1361 case USB_ST_SETUP:
1362 tr_setup:
1363 /* check if we can put more data into the FIFO */
1364 if (
1365 #ifdef EVDEV_SUPPORT
1366 ((evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) != 0 &&
1367 (sc->sc_state & WSP_EVDEV_OPENED) != 0) ||
1368 #endif
1369 usb_fifo_put_bytes_max(
1370 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
1371 usbd_xfer_set_frame_len(xfer, 0,
1372 sc->tp_datalen);
1373 usbd_transfer_submit(xfer);
1374 }
1375 break;
1376
1377 default: /* Error */
1378 if (error != USB_ERR_CANCELLED) {
1379 /* try clear stall first */
1380 usbd_xfer_set_stall(xfer);
1381 goto tr_setup;
1382 }
1383 break;
1384 }
1385 }
1386
1387 static void
wsp_add_to_queue(struct wsp_softc * sc,int dx,int dy,int dz,uint32_t buttons_in)1388 wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
1389 uint32_t buttons_in)
1390 {
1391 uint32_t buttons_out;
1392 uint8_t buf[8];
1393
1394 dx = imin(dx, 254);
1395 dx = imax(dx, -256);
1396 dy = imin(dy, 254);
1397 dy = imax(dy, -256);
1398 dz = imin(dz, 126);
1399 dz = imax(dz, -128);
1400
1401 buttons_out = MOUSE_MSC_BUTTONS;
1402 if (buttons_in & MOUSE_BUTTON1DOWN)
1403 buttons_out &= ~MOUSE_MSC_BUTTON1UP;
1404 else if (buttons_in & MOUSE_BUTTON2DOWN)
1405 buttons_out &= ~MOUSE_MSC_BUTTON2UP;
1406 else if (buttons_in & MOUSE_BUTTON3DOWN)
1407 buttons_out &= ~MOUSE_MSC_BUTTON3UP;
1408
1409 /* Encode the mouse data in standard format; refer to mouse(4) */
1410 buf[0] = sc->sc_mode.syncmask[1];
1411 buf[0] |= buttons_out;
1412 buf[1] = dx >> 1;
1413 buf[2] = dy >> 1;
1414 buf[3] = dx - (dx >> 1);
1415 buf[4] = dy - (dy >> 1);
1416 /* Encode extra bytes for level 1 */
1417 if (sc->sc_mode.level == 1) {
1418 buf[5] = dz >> 1; /* dz / 2 */
1419 buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1420 buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1421 }
1422
1423 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1424 sc->sc_mode.packetsize, 1);
1425 }
1426
1427 static void
wsp_reset_buf(struct wsp_softc * sc)1428 wsp_reset_buf(struct wsp_softc *sc)
1429 {
1430 /* reset read queue */
1431 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1432 }
1433
1434 static void
wsp_start_read(struct wsp_softc * sc)1435 wsp_start_read(struct wsp_softc *sc)
1436 {
1437 int rate;
1438
1439 /* Check if we should override the default polling interval */
1440 rate = sc->sc_pollrate;
1441 /* Range check rate */
1442 if (rate > 1000)
1443 rate = 1000;
1444 /* Check for set rate */
1445 if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1446 /* Stop current transfer, if any */
1447 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1448 /* Set new interval */
1449 usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1450 /* Only set pollrate once */
1451 sc->sc_pollrate = 0;
1452 }
1453 usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1454 }
1455
1456 static void
wsp_stop_read(struct wsp_softc * sc)1457 wsp_stop_read(struct wsp_softc *sc)
1458 {
1459 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1460 }
1461
1462 static int
wsp_open(struct usb_fifo * fifo,int fflags)1463 wsp_open(struct usb_fifo *fifo, int fflags)
1464 {
1465 struct wsp_softc *sc = usb_fifo_softc(fifo);
1466 int rc = 0;
1467
1468 DPRINTFN(WSP_LLEVEL_INFO, "\n");
1469
1470 if (sc->sc_fflags & fflags)
1471 return (EBUSY);
1472
1473 if (fflags & FREAD) {
1474 if (usb_fifo_alloc_buffer(fifo,
1475 WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1476 return (ENOMEM);
1477 }
1478 #ifdef EVDEV_SUPPORT
1479 if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1480 #endif
1481 rc = wsp_enable(sc);
1482 if (rc != 0) {
1483 usb_fifo_free_buffer(fifo);
1484 return (rc);
1485 }
1486 }
1487 sc->sc_fflags |= fflags & (FREAD | FWRITE);
1488 return (0);
1489 }
1490
1491 static void
wsp_close(struct usb_fifo * fifo,int fflags)1492 wsp_close(struct usb_fifo *fifo, int fflags)
1493 {
1494 struct wsp_softc *sc = usb_fifo_softc(fifo);
1495
1496 if (fflags & FREAD) {
1497 #ifdef EVDEV_SUPPORT
1498 if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1499 #endif
1500 wsp_disable(sc);
1501 usb_fifo_free_buffer(fifo);
1502 }
1503
1504 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1505 }
1506
1507 static void
wsp_fifo_start_read(struct usb_fifo * fifo)1508 wsp_fifo_start_read(struct usb_fifo *fifo)
1509 {
1510 struct wsp_softc *sc = usb_fifo_softc(fifo);
1511
1512 wsp_start_read(sc);
1513 }
1514
1515 static void
wsp_fifo_stop_read(struct usb_fifo * fifo)1516 wsp_fifo_stop_read(struct usb_fifo *fifo)
1517 {
1518 struct wsp_softc *sc = usb_fifo_softc(fifo);
1519
1520 #ifdef EVDEV_SUPPORT
1521 if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1522 #endif
1523 wsp_stop_read(sc);
1524 }
1525
1526 #ifdef EVDEV_SUPPORT
1527 static int
wsp_ev_open(struct evdev_dev * evdev)1528 wsp_ev_open(struct evdev_dev *evdev)
1529 {
1530 struct wsp_softc *sc = evdev_get_softc(evdev);
1531 int rc = 0;
1532
1533 mtx_lock(&sc->sc_mutex);
1534 if (sc->sc_fflags == 0)
1535 rc = wsp_enable(sc);
1536 if (rc == 0) {
1537 wsp_start_read(sc);
1538 sc->sc_state |= WSP_EVDEV_OPENED;
1539 }
1540 mtx_unlock(&sc->sc_mutex);
1541
1542 return (rc);
1543 }
1544
1545 static int
wsp_ev_close(struct evdev_dev * evdev)1546 wsp_ev_close(struct evdev_dev *evdev)
1547 {
1548 struct wsp_softc *sc = evdev_get_softc(evdev);
1549
1550 mtx_lock(&sc->sc_mutex);
1551 sc->sc_state &= ~WSP_EVDEV_OPENED;
1552 if (sc->sc_fflags == 0)
1553 wsp_stop_read(sc);
1554 mtx_unlock(&sc->sc_mutex);
1555
1556 return (0);
1557 }
1558 #endif
1559
1560 int
wsp_ioctl(struct usb_fifo * fifo,u_long cmd,void * addr,int fflags)1561 wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1562 {
1563 struct wsp_softc *sc = usb_fifo_softc(fifo);
1564 mousemode_t mode;
1565 int error = 0;
1566
1567 mtx_lock(&sc->sc_mutex);
1568
1569 switch (cmd) {
1570 case MOUSE_GETHWINFO:
1571 *(mousehw_t *)addr = sc->sc_hw;
1572 break;
1573 case MOUSE_GETMODE:
1574 *(mousemode_t *)addr = sc->sc_mode;
1575 break;
1576 case MOUSE_SETMODE:
1577 mode = *(mousemode_t *)addr;
1578
1579 if (mode.level == -1)
1580 /* Don't change the current setting */
1581 ;
1582 else if ((mode.level < 0) || (mode.level > 1)) {
1583 error = EINVAL;
1584 goto done;
1585 }
1586 sc->sc_mode.level = mode.level;
1587 sc->sc_pollrate = mode.rate;
1588 sc->sc_hw.buttons = 3;
1589
1590 if (sc->sc_mode.level == 0) {
1591 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1592 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1593 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1594 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1595 } else if (sc->sc_mode.level == 1) {
1596 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1597 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1598 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1599 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1600 }
1601 wsp_reset_buf(sc);
1602 break;
1603 case MOUSE_GETLEVEL:
1604 *(int *)addr = sc->sc_mode.level;
1605 break;
1606 case MOUSE_SETLEVEL:
1607 if (*(int *)addr < 0 || *(int *)addr > 1) {
1608 error = EINVAL;
1609 goto done;
1610 }
1611 sc->sc_mode.level = *(int *)addr;
1612 sc->sc_hw.buttons = 3;
1613
1614 if (sc->sc_mode.level == 0) {
1615 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1616 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1617 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1618 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1619 } else if (sc->sc_mode.level == 1) {
1620 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1621 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1622 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1623 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1624 }
1625 wsp_reset_buf(sc);
1626 break;
1627 case MOUSE_GETSTATUS:{
1628 mousestatus_t *status = (mousestatus_t *)addr;
1629
1630 *status = sc->sc_status;
1631 sc->sc_status.obutton = sc->sc_status.button;
1632 sc->sc_status.button = 0;
1633 sc->sc_status.dx = 0;
1634 sc->sc_status.dy = 0;
1635 sc->sc_status.dz = 0;
1636
1637 if (status->dx || status->dy || status->dz)
1638 status->flags |= MOUSE_POSCHANGED;
1639 if (status->button != status->obutton)
1640 status->flags |= MOUSE_BUTTONSCHANGED;
1641 break;
1642 }
1643 default:
1644 error = ENOTTY;
1645 }
1646
1647 done:
1648 mtx_unlock(&sc->sc_mutex);
1649 return (error);
1650 }
1651
1652 static device_method_t wsp_methods[] = {
1653 /* Device interface */
1654 DEVMETHOD(device_probe, wsp_probe),
1655 DEVMETHOD(device_attach, wsp_attach),
1656 DEVMETHOD(device_detach, wsp_detach),
1657 DEVMETHOD_END
1658 };
1659
1660 static driver_t wsp_driver = {
1661 .name = WSP_DRIVER_NAME,
1662 .methods = wsp_methods,
1663 .size = sizeof(struct wsp_softc)
1664 };
1665
1666 DRIVER_MODULE(wsp, uhub, wsp_driver, NULL, NULL);
1667 MODULE_DEPEND(wsp, usb, 1, 1, 1);
1668 MODULE_DEPEND(wsp, hid, 1, 1, 1);
1669 #ifdef EVDEV_SUPPORT
1670 MODULE_DEPEND(wsp, evdev, 1, 1, 1);
1671 #endif
1672 MODULE_VERSION(wsp, 1);
1673 USB_PNP_HOST_INFO(wsp_devs);
1674