1261260Shselasky/*- 2261260Shselasky * Copyright (c) 2012 Huang Wen Hui 3261260Shselasky * All rights reserved. 4261260Shselasky * 5261260Shselasky * Redistribution and use in source and binary forms, with or without 6261260Shselasky * modification, are permitted provided that the following conditions 7261260Shselasky * are met: 8261260Shselasky * 1. Redistributions of source code must retain the above copyright 9261260Shselasky * notice, this list of conditions and the following disclaimer. 10261260Shselasky * 2. Redistributions in binary form must reproduce the above copyright 11261260Shselasky * notice, this list of conditions and the following disclaimer in the 12261260Shselasky * documentation and/or other materials provided with the distribution. 13261260Shselasky * 14261260Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15261260Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16261260Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17261260Shselasky * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18261260Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19261260Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20261260Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21261260Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22261260Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23261260Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24261260Shselasky * SUCH DAMAGE. 25261260Shselasky */ 26261260Shselasky 27261260Shselasky#include <sys/cdefs.h> 28261260Shselasky__FBSDID("$FreeBSD: stable/10/sys/dev/usb/input/wsp.c 331997 2018-04-04 08:45:41Z hselasky $"); 29261260Shselasky 30261260Shselasky#include <sys/param.h> 31261260Shselasky#include <sys/systm.h> 32261260Shselasky#include <sys/kernel.h> 33261260Shselasky#include <sys/malloc.h> 34261260Shselasky#include <sys/module.h> 35261260Shselasky#include <sys/lock.h> 36261260Shselasky#include <sys/mutex.h> 37261260Shselasky#include <sys/bus.h> 38261260Shselasky#include <sys/conf.h> 39261260Shselasky#include <sys/fcntl.h> 40261260Shselasky#include <sys/file.h> 41261260Shselasky#include <sys/selinfo.h> 42261260Shselasky#include <sys/poll.h> 43261260Shselasky#include <sys/sysctl.h> 44261260Shselasky 45261260Shselasky#include <dev/usb/usb.h> 46261260Shselasky#include <dev/usb/usbdi.h> 47261260Shselasky#include <dev/usb/usbdi_util.h> 48261260Shselasky#include <dev/usb/usbhid.h> 49261260Shselasky 50261260Shselasky#include "usbdevs.h" 51261260Shselasky 52261260Shselasky#define USB_DEBUG_VAR wsp_debug 53261260Shselasky#include <dev/usb/usb_debug.h> 54261260Shselasky 55261260Shselasky#include <sys/mouse.h> 56261260Shselasky 57261260Shselasky#define WSP_DRIVER_NAME "wsp" 58262369Shselasky#define WSP_BUFFER_MAX 1024 59261260Shselasky 60261260Shselasky#define WSP_CLAMP(x,low,high) do { \ 61261260Shselasky if ((x) < (low)) \ 62261260Shselasky (x) = (low); \ 63261260Shselasky else if ((x) > (high)) \ 64261260Shselasky (x) = (high); \ 65261260Shselasky} while (0) 66261260Shselasky 67261260Shselasky/* Tunables */ 68261260Shselaskystatic SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW, 0, "USB wsp"); 69261260Shselasky 70261260Shselasky#ifdef USB_DEBUG 71261260Shselaskyenum wsp_log_level { 72261260Shselasky WSP_LLEVEL_DISABLED = 0, 73261260Shselasky WSP_LLEVEL_ERROR, 74261260Shselasky WSP_LLEVEL_DEBUG, /* for troubleshooting */ 75261260Shselasky WSP_LLEVEL_INFO, /* for diagnostics */ 76261260Shselasky}; 77261260Shselaskystatic int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */ 78261260Shselasky 79261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RW, 80261260Shselasky &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level"); 81261260Shselasky#endif /* USB_DEBUG */ 82261260Shselasky 83261260Shselaskystatic struct wsp_tuning { 84261260Shselasky int scale_factor; 85261260Shselasky int z_factor; 86261260Shselasky int pressure_touch_threshold; 87261260Shselasky int pressure_untouch_threshold; 88261260Shselasky int pressure_tap_threshold; 89261260Shselasky int scr_hor_threshold; 90261260Shselasky} 91261260Shselasky wsp_tuning = 92261260Shselasky{ 93261260Shselasky .scale_factor = 12, 94261260Shselasky .z_factor = 5, 95261260Shselasky .pressure_touch_threshold = 50, 96261260Shselasky .pressure_untouch_threshold = 10, 97291065Shselasky .pressure_tap_threshold = 120, 98291065Shselasky .scr_hor_threshold = 20, 99261260Shselasky}; 100261260Shselasky 101261260Shselaskystatic void 102261260Shselaskywsp_runing_rangecheck(struct wsp_tuning *ptun) 103261260Shselasky{ 104261260Shselasky WSP_CLAMP(ptun->scale_factor, 1, 63); 105261260Shselasky WSP_CLAMP(ptun->z_factor, 1, 63); 106261260Shselasky WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255); 107261260Shselasky WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255); 108261260Shselasky WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255); 109261260Shselasky WSP_CLAMP(ptun->scr_hor_threshold, 1, 255); 110261260Shselasky} 111261260Shselasky 112261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RW, 113261260Shselasky &wsp_tuning.scale_factor, 0, "movement scale factor"); 114261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RW, 115261260Shselasky &wsp_tuning.z_factor, 0, "Z-axis scale factor"); 116261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RW, 117261260Shselasky &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold"); 118261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RW, 119261260Shselasky &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold"); 120261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RW, 121261260Shselasky &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold"); 122261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RW, 123261260Shselasky &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold"); 124261260Shselasky 125261509Shselasky/* 126262369Shselasky * Some tables, structures, definitions and constant values for the 127262369Shselasky * touchpad protocol has been copied from Linux's 128261509Shselasky * "drivers/input/mouse/bcm5974.c" which has the following copyright 129261509Shselasky * holders under GPLv2. All device specific code in this driver has 130261509Shselasky * been written from scratch. The decoding algorithm is based on 131262369Shselasky * output from FreeBSD's usbdump. 132261509Shselasky * 133261509Shselasky * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se) 134261509Shselasky * Copyright (C) 2008 Scott Shawcroft (scott.shawcroft@gmail.com) 135261509Shselasky * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) 136261509Shselasky * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net) 137261509Shselasky * Copyright (C) 2005 Stelian Pop (stelian@popies.net) 138261509Shselasky * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de) 139261509Shselasky * Copyright (C) 2005 Peter Osterlund (petero2@telia.com) 140261509Shselasky * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch) 141261509Shselasky * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch) 142261509Shselasky */ 143261509Shselasky 144261260Shselasky/* button data structure */ 145261260Shselaskystruct bt_data { 146261260Shselasky uint8_t unknown1; /* constant */ 147261260Shselasky uint8_t button; /* left button */ 148261260Shselasky uint8_t rel_x; /* relative x coordinate */ 149261260Shselasky uint8_t rel_y; /* relative y coordinate */ 150261260Shselasky} __packed; 151261260Shselasky 152261260Shselasky/* trackpad header types */ 153261260Shselaskyenum tp_type { 154261260Shselasky TYPE1, /* plain trackpad */ 155261260Shselasky TYPE2, /* button integrated in trackpad */ 156291065Shselasky TYPE3, /* additional header fields since June 2013 */ 157291065Shselasky TYPE4 /* additional header field for pressure data */ 158261260Shselasky}; 159261260Shselasky 160261260Shselasky/* trackpad finger data offsets, le16-aligned */ 161261260Shselasky#define FINGER_TYPE1 (13 * 2) 162261260Shselasky#define FINGER_TYPE2 (15 * 2) 163261260Shselasky#define FINGER_TYPE3 (19 * 2) 164291065Shselasky#define FINGER_TYPE4 (23 * 2) 165261260Shselasky 166261260Shselasky/* trackpad button data offsets */ 167261260Shselasky#define BUTTON_TYPE2 15 168261260Shselasky#define BUTTON_TYPE3 23 169291065Shselasky#define BUTTON_TYPE4 31 170261260Shselasky 171261260Shselasky/* list of device capability bits */ 172261260Shselasky#define HAS_INTEGRATED_BUTTON 1 173261260Shselasky 174291065Shselasky/* trackpad finger data block size */ 175291065Shselasky#define FSIZE_TYPE1 (14 * 2) 176291065Shselasky#define FSIZE_TYPE2 (14 * 2) 177291065Shselasky#define FSIZE_TYPE3 (14 * 2) 178291065Shselasky#define FSIZE_TYPE4 (15 * 2) 179291065Shselasky 180261260Shselasky/* trackpad finger header - little endian */ 181261260Shselaskystruct tp_header { 182261260Shselasky uint8_t flag; 183261260Shselasky uint8_t sn0; 184261260Shselasky uint16_t wFixed0; 185261260Shselasky uint32_t dwSn1; 186261260Shselasky uint32_t dwFixed1; 187261260Shselasky uint16_t wLength; 188261260Shselasky uint8_t nfinger; 189261260Shselasky uint8_t ibt; 190261260Shselasky int16_t wUnknown[6]; 191261260Shselasky uint8_t q1; 192261260Shselasky uint8_t q2; 193261260Shselasky} __packed; 194261260Shselasky 195261260Shselasky/* trackpad finger structure - little endian */ 196261260Shselaskystruct tp_finger { 197261260Shselasky int16_t origin; /* zero when switching track finger */ 198261260Shselasky int16_t abs_x; /* absolute x coodinate */ 199261260Shselasky int16_t abs_y; /* absolute y coodinate */ 200261260Shselasky int16_t rel_x; /* relative x coodinate */ 201261260Shselasky int16_t rel_y; /* relative y coodinate */ 202261260Shselasky int16_t tool_major; /* tool area, major axis */ 203261260Shselasky int16_t tool_minor; /* tool area, minor axis */ 204261260Shselasky int16_t orientation; /* 16384 when point, else 15 bit angle */ 205261260Shselasky int16_t touch_major; /* touch area, major axis */ 206261260Shselasky int16_t touch_minor; /* touch area, minor axis */ 207291065Shselasky int16_t unused[2]; /* zeros */ 208291065Shselasky int16_t pressure; /* pressure on forcetouch touchpad */ 209261260Shselasky int16_t multi; /* one finger: varies, more fingers: 210291065Shselasky * constant */ 211261260Shselasky} __packed; 212261260Shselasky 213261260Shselasky/* trackpad finger data size, empirically at least ten fingers */ 214261260Shselasky#define MAX_FINGERS 16 215261260Shselasky#define SIZEOF_FINGER sizeof(struct tp_finger) 216261260Shselasky#define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER) 217261260Shselasky 218291065Shselasky#if (WSP_BUFFER_MAX < ((MAX_FINGERS * FSIZE_TYPE4) + FINGER_TYPE4)) 219262369Shselasky#error "WSP_BUFFER_MAX is too small" 220262369Shselasky#endif 221261260Shselasky 222261260Shselaskyenum { 223261260Shselasky WSP_FLAG_WELLSPRING1, 224261260Shselasky WSP_FLAG_WELLSPRING2, 225261260Shselasky WSP_FLAG_WELLSPRING3, 226261260Shselasky WSP_FLAG_WELLSPRING4, 227261260Shselasky WSP_FLAG_WELLSPRING4A, 228261260Shselasky WSP_FLAG_WELLSPRING5, 229261260Shselasky WSP_FLAG_WELLSPRING6A, 230261260Shselasky WSP_FLAG_WELLSPRING6, 231261260Shselasky WSP_FLAG_WELLSPRING5A, 232261260Shselasky WSP_FLAG_WELLSPRING7, 233261260Shselasky WSP_FLAG_WELLSPRING7A, 234261260Shselasky WSP_FLAG_WELLSPRING8, 235291065Shselasky WSP_FLAG_WELLSPRING9, 236261260Shselasky WSP_FLAG_MAX, 237261260Shselasky}; 238261260Shselasky 239261260Shselasky/* device-specific configuration */ 240261260Shselaskystruct wsp_dev_params { 241261260Shselasky uint8_t caps; /* device capability bitmask */ 242261260Shselasky uint8_t tp_type; /* type of trackpad interface */ 243291065Shselasky uint8_t tp_button; /* offset to button data */ 244261260Shselasky uint8_t tp_offset; /* offset to trackpad finger data */ 245291065Shselasky uint8_t tp_fsize; /* bytes in single finger block */ 246291065Shselasky uint8_t tp_delta; /* offset from header to finger struct */ 247291065Shselasky uint8_t iface_index; 248291065Shselasky uint8_t um_size; /* usb control message length */ 249291065Shselasky uint8_t um_req_val; /* usb control message value */ 250291065Shselasky uint8_t um_req_idx; /* usb control message index */ 251291065Shselasky uint8_t um_switch_idx; /* usb control message mode switch index */ 252291065Shselasky uint8_t um_switch_on; /* usb control message mode switch on */ 253291065Shselasky uint8_t um_switch_off; /* usb control message mode switch off */ 254261260Shselasky}; 255261260Shselasky 256261260Shselaskystatic const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = { 257261260Shselasky [WSP_FLAG_WELLSPRING1] = { 258261260Shselasky .caps = 0, 259261260Shselasky .tp_type = TYPE1, 260291065Shselasky .tp_button = 0, 261261260Shselasky .tp_offset = FINGER_TYPE1, 262291065Shselasky .tp_fsize = FSIZE_TYPE1, 263291065Shselasky .tp_delta = 0, 264291065Shselasky .iface_index = 0, 265291065Shselasky .um_size = 8, 266291065Shselasky .um_req_val = 0x03, 267291065Shselasky .um_req_idx = 0x00, 268291065Shselasky .um_switch_idx = 0, 269291065Shselasky .um_switch_on = 0x01, 270291065Shselasky .um_switch_off = 0x08, 271261260Shselasky }, 272261260Shselasky [WSP_FLAG_WELLSPRING2] = { 273261260Shselasky .caps = 0, 274261260Shselasky .tp_type = TYPE1, 275291065Shselasky .tp_button = 0, 276261260Shselasky .tp_offset = FINGER_TYPE1, 277291065Shselasky .tp_fsize = FSIZE_TYPE1, 278291065Shselasky .tp_delta = 0, 279291065Shselasky .iface_index = 0, 280291065Shselasky .um_size = 8, 281291065Shselasky .um_req_val = 0x03, 282291065Shselasky .um_req_idx = 0x00, 283291065Shselasky .um_switch_idx = 0, 284291065Shselasky .um_switch_on = 0x01, 285291065Shselasky .um_switch_off = 0x08, 286261260Shselasky }, 287261260Shselasky [WSP_FLAG_WELLSPRING3] = { 288261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 289261260Shselasky .tp_type = TYPE2, 290291065Shselasky .tp_button = BUTTON_TYPE2, 291261260Shselasky .tp_offset = FINGER_TYPE2, 292291065Shselasky .tp_fsize = FSIZE_TYPE2, 293291065Shselasky .tp_delta = 0, 294291065Shselasky .iface_index = 0, 295291065Shselasky .um_size = 8, 296291065Shselasky .um_req_val = 0x03, 297291065Shselasky .um_req_idx = 0x00, 298291065Shselasky .um_switch_idx = 0, 299291065Shselasky .um_switch_on = 0x01, 300291065Shselasky .um_switch_off = 0x08, 301261260Shselasky }, 302261260Shselasky [WSP_FLAG_WELLSPRING4] = { 303261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 304261260Shselasky .tp_type = TYPE2, 305291065Shselasky .tp_button = BUTTON_TYPE2, 306261260Shselasky .tp_offset = FINGER_TYPE2, 307291065Shselasky .tp_fsize = FSIZE_TYPE2, 308291065Shselasky .tp_delta = 0, 309291065Shselasky .iface_index = 0, 310291065Shselasky .um_size = 8, 311291065Shselasky .um_req_val = 0x03, 312291065Shselasky .um_req_idx = 0x00, 313291065Shselasky .um_switch_idx = 0, 314291065Shselasky .um_switch_on = 0x01, 315291065Shselasky .um_switch_off = 0x08, 316261260Shselasky }, 317261260Shselasky [WSP_FLAG_WELLSPRING4A] = { 318261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 319261260Shselasky .tp_type = TYPE2, 320291065Shselasky .tp_button = BUTTON_TYPE2, 321261260Shselasky .tp_offset = FINGER_TYPE2, 322291065Shselasky .tp_fsize = FSIZE_TYPE2, 323291065Shselasky .tp_delta = 0, 324291065Shselasky .iface_index = 0, 325291065Shselasky .um_size = 8, 326291065Shselasky .um_req_val = 0x03, 327291065Shselasky .um_req_idx = 0x00, 328291065Shselasky .um_switch_idx = 0, 329291065Shselasky .um_switch_on = 0x01, 330291065Shselasky .um_switch_off = 0x08, 331261260Shselasky }, 332261260Shselasky [WSP_FLAG_WELLSPRING5] = { 333261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 334261260Shselasky .tp_type = TYPE2, 335291065Shselasky .tp_button = BUTTON_TYPE2, 336261260Shselasky .tp_offset = FINGER_TYPE2, 337291065Shselasky .tp_fsize = FSIZE_TYPE2, 338291065Shselasky .tp_delta = 0, 339291065Shselasky .iface_index = 0, 340291065Shselasky .um_size = 8, 341291065Shselasky .um_req_val = 0x03, 342291065Shselasky .um_req_idx = 0x00, 343291065Shselasky .um_switch_idx = 0, 344291065Shselasky .um_switch_on = 0x01, 345291065Shselasky .um_switch_off = 0x08, 346261260Shselasky }, 347261260Shselasky [WSP_FLAG_WELLSPRING6] = { 348261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 349261260Shselasky .tp_type = TYPE2, 350291065Shselasky .tp_button = BUTTON_TYPE2, 351261260Shselasky .tp_offset = FINGER_TYPE2, 352291065Shselasky .tp_fsize = FSIZE_TYPE2, 353291065Shselasky .tp_delta = 0, 354291065Shselasky .iface_index = 0, 355291065Shselasky .um_size = 8, 356291065Shselasky .um_req_val = 0x03, 357291065Shselasky .um_req_idx = 0x00, 358291065Shselasky .um_switch_idx = 0, 359291065Shselasky .um_switch_on = 0x01, 360291065Shselasky .um_switch_off = 0x08, 361261260Shselasky }, 362261260Shselasky [WSP_FLAG_WELLSPRING5A] = { 363261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 364261260Shselasky .tp_type = TYPE2, 365291065Shselasky .tp_button = BUTTON_TYPE2, 366261260Shselasky .tp_offset = FINGER_TYPE2, 367291065Shselasky .tp_fsize = FSIZE_TYPE2, 368291065Shselasky .tp_delta = 0, 369291065Shselasky .iface_index = 0, 370291065Shselasky .um_size = 8, 371291065Shselasky .um_req_val = 0x03, 372291065Shselasky .um_req_idx = 0x00, 373291065Shselasky .um_switch_idx = 0, 374291065Shselasky .um_switch_on = 0x01, 375291065Shselasky .um_switch_off = 0x08, 376261260Shselasky }, 377261260Shselasky [WSP_FLAG_WELLSPRING6A] = { 378261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 379261260Shselasky .tp_type = TYPE2, 380291065Shselasky .tp_button = BUTTON_TYPE2, 381261260Shselasky .tp_offset = FINGER_TYPE2, 382291065Shselasky .tp_fsize = FSIZE_TYPE2, 383291065Shselasky .tp_delta = 0, 384291065Shselasky .um_size = 8, 385291065Shselasky .um_req_val = 0x03, 386291065Shselasky .um_req_idx = 0x00, 387291065Shselasky .um_switch_idx = 0, 388291065Shselasky .um_switch_on = 0x01, 389291065Shselasky .um_switch_off = 0x08, 390261260Shselasky }, 391261260Shselasky [WSP_FLAG_WELLSPRING7] = { 392261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 393261260Shselasky .tp_type = TYPE2, 394291065Shselasky .tp_button = BUTTON_TYPE2, 395261260Shselasky .tp_offset = FINGER_TYPE2, 396291065Shselasky .tp_fsize = FSIZE_TYPE2, 397291065Shselasky .tp_delta = 0, 398291065Shselasky .iface_index = 0, 399291065Shselasky .um_size = 8, 400291065Shselasky .um_req_val = 0x03, 401291065Shselasky .um_req_idx = 0x00, 402291065Shselasky .um_switch_idx = 0, 403291065Shselasky .um_switch_on = 0x01, 404291065Shselasky .um_switch_off = 0x08, 405261260Shselasky }, 406261260Shselasky [WSP_FLAG_WELLSPRING7A] = { 407261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 408261260Shselasky .tp_type = TYPE2, 409291065Shselasky .tp_button = BUTTON_TYPE2, 410261260Shselasky .tp_offset = FINGER_TYPE2, 411291065Shselasky .tp_fsize = FSIZE_TYPE2, 412291065Shselasky .tp_delta = 0, 413291065Shselasky .iface_index = 0, 414291065Shselasky .um_size = 8, 415291065Shselasky .um_req_val = 0x03, 416291065Shselasky .um_req_idx = 0x00, 417291065Shselasky .um_switch_idx = 0, 418291065Shselasky .um_switch_on = 0x01, 419291065Shselasky .um_switch_off = 0x08, 420261260Shselasky }, 421261260Shselasky [WSP_FLAG_WELLSPRING8] = { 422261260Shselasky .caps = HAS_INTEGRATED_BUTTON, 423261260Shselasky .tp_type = TYPE3, 424291065Shselasky .tp_button = BUTTON_TYPE3, 425261260Shselasky .tp_offset = FINGER_TYPE3, 426291065Shselasky .tp_fsize = FSIZE_TYPE3, 427291065Shselasky .tp_delta = 0, 428291065Shselasky .iface_index = 0, 429291065Shselasky .um_size = 8, 430291065Shselasky .um_req_val = 0x03, 431291065Shselasky .um_req_idx = 0x00, 432291065Shselasky .um_switch_idx = 0, 433291065Shselasky .um_switch_on = 0x01, 434291065Shselasky .um_switch_off = 0x08, 435261260Shselasky }, 436291065Shselasky [WSP_FLAG_WELLSPRING9] = { 437291065Shselasky .caps = HAS_INTEGRATED_BUTTON, 438291065Shselasky .tp_type = TYPE4, 439291065Shselasky .tp_button = BUTTON_TYPE4, 440291065Shselasky .tp_offset = FINGER_TYPE4, 441291065Shselasky .tp_fsize = FSIZE_TYPE4, 442291065Shselasky .tp_delta = 2, 443291065Shselasky .iface_index = 2, 444291065Shselasky .um_size = 2, 445291065Shselasky .um_req_val = 0x03, 446291065Shselasky .um_req_idx = 0x02, 447291065Shselasky .um_switch_idx = 1, 448291065Shselasky .um_switch_on = 0x01, 449291065Shselasky .um_switch_off = 0x00, 450291065Shselasky }, 451261260Shselasky}; 452261260Shselasky 453261260Shselasky#define WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) } 454261260Shselasky 455261260Shselaskystatic const STRUCT_USB_HOST_ID wsp_devs[] = { 456261260Shselasky /* MacbookAir1.1 */ 457261260Shselasky WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1), 458261260Shselasky WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1), 459261260Shselasky WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1), 460261260Shselasky 461261260Shselasky /* MacbookProPenryn, aka wellspring2 */ 462261260Shselasky WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2), 463261260Shselasky WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2), 464261260Shselasky WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2), 465261260Shselasky 466261260Shselasky /* Macbook5,1 (unibody), aka wellspring3 */ 467261260Shselasky WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3), 468261260Shselasky WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3), 469261260Shselasky WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3), 470261260Shselasky 471261260Shselasky /* MacbookAir3,2 (unibody), aka wellspring4 */ 472261260Shselasky WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4), 473261260Shselasky WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4), 474261260Shselasky WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4), 475261260Shselasky 476261260Shselasky /* MacbookAir3,1 (unibody), aka wellspring4 */ 477261260Shselasky WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A), 478261260Shselasky WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A), 479261260Shselasky WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A), 480261260Shselasky 481261260Shselasky /* Macbook8 (unibody, March 2011) */ 482261260Shselasky WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5), 483261260Shselasky WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5), 484261260Shselasky WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5), 485261260Shselasky 486261260Shselasky /* MacbookAir4,1 (unibody, July 2011) */ 487261260Shselasky WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A), 488261260Shselasky WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A), 489261260Shselasky WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A), 490261260Shselasky 491261260Shselasky /* MacbookAir4,2 (unibody, July 2011) */ 492261260Shselasky WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6), 493261260Shselasky WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6), 494261260Shselasky WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6), 495261260Shselasky 496261260Shselasky /* Macbook8,2 (unibody) */ 497261260Shselasky WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A), 498261260Shselasky WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A), 499261260Shselasky WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A), 500261260Shselasky 501261260Shselasky /* MacbookPro10,1 (unibody, June 2012) */ 502291065Shselasky /* MacbookPro11,1-3 (unibody, June 2013) */ 503261260Shselasky WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7), 504261260Shselasky WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7), 505261260Shselasky WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7), 506261260Shselasky 507261260Shselasky /* MacbookPro10,2 (unibody, October 2012) */ 508261260Shselasky WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A), 509261260Shselasky WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A), 510261260Shselasky WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A), 511261260Shselasky 512261260Shselasky /* MacbookAir6,2 (unibody, June 2013) */ 513261260Shselasky WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8), 514261260Shselasky WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8), 515261260Shselasky WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8), 516291065Shselasky 517291065Shselasky /* MacbookPro12,1 MacbookPro11,4 */ 518291065Shselasky WSP_DEV(APPLE, WELLSPRING9_ANSI, WSP_FLAG_WELLSPRING9), 519291065Shselasky WSP_DEV(APPLE, WELLSPRING9_ISO, WSP_FLAG_WELLSPRING9), 520291065Shselasky WSP_DEV(APPLE, WELLSPRING9_JIS, WSP_FLAG_WELLSPRING9), 521261260Shselasky}; 522261260Shselasky 523261260Shselasky#define WSP_FIFO_BUF_SIZE 8 /* bytes */ 524261260Shselasky#define WSP_FIFO_QUEUE_MAXLEN 50 /* units */ 525261260Shselasky 526261260Shselaskyenum { 527261260Shselasky WSP_INTR_DT, 528261260Shselasky WSP_N_TRANSFER, 529261260Shselasky}; 530261260Shselasky 531261260Shselaskystruct wsp_softc { 532261260Shselasky struct usb_device *sc_usb_device; 533261260Shselasky struct mtx sc_mutex; /* for synchronization */ 534261260Shselasky struct usb_xfer *sc_xfer[WSP_N_TRANSFER]; 535261260Shselasky struct usb_fifo_sc sc_fifo; 536261260Shselasky 537261260Shselasky const struct wsp_dev_params *sc_params; /* device configuration */ 538261260Shselasky 539261260Shselasky mousehw_t sc_hw; 540261260Shselasky mousemode_t sc_mode; 541261260Shselasky u_int sc_pollrate; 542261260Shselasky mousestatus_t sc_status; 543261260Shselasky u_int sc_state; 544261260Shselasky#define WSP_ENABLED 0x01 545261260Shselasky 546261260Shselasky struct tp_finger *index[MAX_FINGERS]; /* finger index data */ 547261260Shselasky int16_t pos_x[MAX_FINGERS]; /* position array */ 548261260Shselasky int16_t pos_y[MAX_FINGERS]; /* position array */ 549261260Shselasky u_int sc_touch; /* touch status */ 550261260Shselasky#define WSP_UNTOUCH 0x00 551261260Shselasky#define WSP_FIRST_TOUCH 0x01 552261260Shselasky#define WSP_SECOND_TOUCH 0x02 553261260Shselasky#define WSP_TOUCHING 0x04 554261260Shselasky int16_t pre_pos_x; /* previous position array */ 555261260Shselasky int16_t pre_pos_y; /* previous position array */ 556261260Shselasky int dx_sum; /* x axis cumulative movement */ 557261260Shselasky int dy_sum; /* y axis cumulative movement */ 558261260Shselasky int dz_sum; /* z axis cumulative movement */ 559261260Shselasky int dz_count; 560261260Shselasky#define WSP_DZ_MAX_COUNT 32 561261260Shselasky int dt_sum; /* T-axis cumulative movement */ 562263208Shselasky int rdx; /* x axis remainder of divide by scale_factor */ 563263208Shselasky int rdy; /* y axis remainder of divide by scale_factor */ 564263208Shselasky int rdz; /* z axis remainder of divide by scale_factor */ 565262369Shselasky int tp_datalen; 566261509Shselasky uint8_t o_ntouch; /* old touch finger status */ 567261260Shselasky uint8_t finger; /* 0 or 1 *, check which finger moving */ 568261260Shselasky uint16_t intr_count; 569261260Shselasky#define WSP_TAP_THRESHOLD 3 570261260Shselasky#define WSP_TAP_MAX_COUNT 20 571261260Shselasky int distance; /* the distance of 2 fingers */ 572261260Shselasky#define MAX_DISTANCE 2500 /* the max allowed distance */ 573261260Shselasky uint8_t ibtn; /* button status in tapping */ 574261260Shselasky uint8_t ntaps; /* finger status in tapping */ 575261260Shselasky uint8_t scr_mode; /* scroll status in movement */ 576261260Shselasky#define WSP_SCR_NONE 0 577261260Shselasky#define WSP_SCR_VER 1 578261260Shselasky#define WSP_SCR_HOR 2 579262369Shselasky uint8_t tp_data[WSP_BUFFER_MAX] __aligned(4); /* trackpad transferred data */ 580261260Shselasky}; 581261260Shselasky 582261260Shselasky/* 583261260Shselasky * function prototypes 584261260Shselasky */ 585261260Shselaskystatic usb_fifo_cmd_t wsp_start_read; 586261260Shselaskystatic usb_fifo_cmd_t wsp_stop_read; 587261260Shselaskystatic usb_fifo_open_t wsp_open; 588261260Shselaskystatic usb_fifo_close_t wsp_close; 589261260Shselaskystatic usb_fifo_ioctl_t wsp_ioctl; 590261260Shselasky 591261260Shselaskystatic struct usb_fifo_methods wsp_fifo_methods = { 592261260Shselasky .f_open = &wsp_open, 593261260Shselasky .f_close = &wsp_close, 594261260Shselasky .f_ioctl = &wsp_ioctl, 595261260Shselasky .f_start_read = &wsp_start_read, 596261260Shselasky .f_stop_read = &wsp_stop_read, 597261260Shselasky .basename[0] = WSP_DRIVER_NAME, 598261260Shselasky}; 599261260Shselasky 600261260Shselasky/* device initialization and shutdown */ 601261260Shselaskystatic int wsp_enable(struct wsp_softc *sc); 602261260Shselaskystatic void wsp_disable(struct wsp_softc *sc); 603261260Shselasky 604261260Shselasky/* updating fifo */ 605261260Shselaskystatic void wsp_reset_buf(struct wsp_softc *sc); 606261260Shselaskystatic void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t); 607261260Shselasky 608261260Shselasky/* Device methods. */ 609261260Shselaskystatic device_probe_t wsp_probe; 610261260Shselaskystatic device_attach_t wsp_attach; 611261260Shselaskystatic device_detach_t wsp_detach; 612261260Shselaskystatic usb_callback_t wsp_intr_callback; 613261260Shselasky 614262369Shselaskystatic const struct usb_config wsp_config[WSP_N_TRANSFER] = { 615261260Shselasky [WSP_INTR_DT] = { 616261260Shselasky .type = UE_INTERRUPT, 617261260Shselasky .endpoint = UE_ADDR_ANY, 618261260Shselasky .direction = UE_DIR_IN, 619261260Shselasky .flags = { 620261260Shselasky .pipe_bof = 0, 621261260Shselasky .short_xfer_ok = 1, 622261260Shselasky }, 623262369Shselasky .bufsize = WSP_BUFFER_MAX, 624261260Shselasky .callback = &wsp_intr_callback, 625261260Shselasky }, 626261260Shselasky}; 627261260Shselasky 628261509Shselaskystatic usb_error_t 629291065Shselaskywsp_set_device_mode(struct wsp_softc *sc, uint8_t on) 630261260Shselasky{ 631291065Shselasky const struct wsp_dev_params *params = sc->sc_params; 632261509Shselasky uint8_t mode_bytes[8]; 633261260Shselasky usb_error_t err; 634261260Shselasky 635291065Shselasky /* Type 3 does not require a mode switch */ 636291065Shselasky if (params->tp_type == TYPE3) 637291065Shselasky return 0; 638291065Shselasky 639261509Shselasky err = usbd_req_get_report(sc->sc_usb_device, NULL, 640291065Shselasky mode_bytes, params->um_size, params->iface_index, 641291065Shselasky params->um_req_val, params->um_req_idx); 642261260Shselasky 643261509Shselasky if (err != USB_ERR_NORMAL_COMPLETION) { 644261509Shselasky DPRINTF("Failed to read device mode (%d)\n", err); 645261509Shselasky return (err); 646261509Shselasky } 647261260Shselasky 648261509Shselasky /* 649261509Shselasky * XXX Need to wait at least 250ms for hardware to get 650261509Shselasky * ready. The device mode handling appears to be handled 651261509Shselasky * asynchronously and we should not issue these commands too 652261509Shselasky * quickly. 653261509Shselasky */ 654261509Shselasky pause("WHW", hz / 4); 655261260Shselasky 656291065Shselasky mode_bytes[params->um_switch_idx] = 657291065Shselasky on ? params->um_switch_on : params->um_switch_off; 658261260Shselasky 659261509Shselasky return (usbd_req_set_report(sc->sc_usb_device, NULL, 660291065Shselasky mode_bytes, params->um_size, params->iface_index, 661291065Shselasky params->um_req_val, params->um_req_idx)); 662261260Shselasky} 663261260Shselasky 664261260Shselaskystatic int 665261260Shselaskywsp_enable(struct wsp_softc *sc) 666261260Shselasky{ 667261260Shselasky /* reset status */ 668261260Shselasky memset(&sc->sc_status, 0, sizeof(sc->sc_status)); 669261260Shselasky sc->sc_state |= WSP_ENABLED; 670261260Shselasky 671261260Shselasky DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n"); 672261260Shselasky return (0); 673261260Shselasky} 674261260Shselasky 675261260Shselaskystatic void 676261260Shselaskywsp_disable(struct wsp_softc *sc) 677261260Shselasky{ 678261260Shselasky sc->sc_state &= ~WSP_ENABLED; 679261260Shselasky DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n"); 680261260Shselasky} 681261260Shselasky 682261260Shselaskystatic int 683261260Shselaskywsp_probe(device_t self) 684261260Shselasky{ 685261260Shselasky struct usb_attach_arg *uaa = device_get_ivars(self); 686291065Shselasky struct usb_interface_descriptor *id; 687291065Shselasky struct usb_interface *iface; 688291065Shselasky uint8_t i; 689261260Shselasky 690261260Shselasky if (uaa->usb_mode != USB_MODE_HOST) 691261260Shselasky return (ENXIO); 692261260Shselasky 693291065Shselasky /* figure out first interface matching */ 694291065Shselasky for (i = 1;; i++) { 695291065Shselasky iface = usbd_get_iface(uaa->device, i); 696291065Shselasky if (iface == NULL || i == 3) 697291065Shselasky return (ENXIO); 698291065Shselasky id = iface->idesc; 699291065Shselasky if ((id == NULL) || 700291065Shselasky (id->bInterfaceClass != UICLASS_HID) || 701291065Shselasky (id->bInterfaceProtocol != 0 && 702291065Shselasky id->bInterfaceProtocol != UIPROTO_MOUSE)) 703291065Shselasky continue; 704291065Shselasky break; 705291065Shselasky } 706291065Shselasky /* check if we are attaching to the first match */ 707291065Shselasky if (uaa->info.bIfaceIndex != i) 708261260Shselasky return (ENXIO); 709261260Shselasky return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa)); 710261260Shselasky} 711261260Shselasky 712261260Shselaskystatic int 713261260Shselaskywsp_attach(device_t dev) 714261260Shselasky{ 715261260Shselasky struct wsp_softc *sc = device_get_softc(dev); 716261260Shselasky struct usb_attach_arg *uaa = device_get_ivars(dev); 717261260Shselasky usb_error_t err; 718262369Shselasky void *d_ptr = NULL; 719262369Shselasky uint16_t d_len; 720261260Shselasky 721261260Shselasky DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc); 722261260Shselasky 723262369Shselasky /* Get HID descriptor */ 724262369Shselasky err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr, 725262369Shselasky &d_len, M_TEMP, uaa->info.bIfaceIndex); 726262369Shselasky 727262369Shselasky if (err == USB_ERR_NORMAL_COMPLETION) { 728262369Shselasky /* Get HID report descriptor length */ 729262369Shselasky sc->tp_datalen = hid_report_size(d_ptr, d_len, hid_input, NULL); 730262369Shselasky free(d_ptr, M_TEMP); 731262369Shselasky 732262369Shselasky if (sc->tp_datalen <= 0 || sc->tp_datalen > WSP_BUFFER_MAX) { 733262369Shselasky DPRINTF("Invalid datalength or too big " 734262369Shselasky "datalength: %d\n", sc->tp_datalen); 735262369Shselasky return (ENXIO); 736262369Shselasky } 737262369Shselasky } else { 738262369Shselasky return (ENXIO); 739262369Shselasky } 740262369Shselasky 741261260Shselasky sc->sc_usb_device = uaa->device; 742261260Shselasky 743291065Shselasky /* get device specific configuration */ 744291065Shselasky sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa); 745291065Shselasky 746261260Shselasky /* 747261509Shselasky * By default the touchpad behaves like a HID device, sending 748261260Shselasky * packets with reportID = 8. Such reports contain only 749261260Shselasky * limited information. They encode movement deltas and button 750261260Shselasky * events, but do not include data from the pressure 751261260Shselasky * sensors. The device input mode can be switched from HID 752261260Shselasky * reports to raw sensor data using vendor-specific USB 753261509Shselasky * control commands: 754261260Shselasky */ 755261509Shselasky 756261509Shselasky /* 757261509Shselasky * During re-enumeration of the device we need to force the 758261509Shselasky * device back into HID mode before switching it to RAW 759261509Shselasky * mode. Else the device does not work like expected. 760261509Shselasky */ 761291065Shselasky err = wsp_set_device_mode(sc, 0); 762261260Shselasky if (err != USB_ERR_NORMAL_COMPLETION) { 763261509Shselasky DPRINTF("Failed to set mode to HID MODE (%d)\n", err); 764261260Shselasky return (ENXIO); 765261260Shselasky } 766261509Shselasky 767291065Shselasky err = wsp_set_device_mode(sc, 1); 768261509Shselasky if (err != USB_ERR_NORMAL_COMPLETION) { 769261509Shselasky DPRINTF("failed to set mode to RAW MODE (%d)\n", err); 770261260Shselasky return (ENXIO); 771261260Shselasky } 772261509Shselasky 773261260Shselasky mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE); 774261260Shselasky 775261260Shselasky err = usbd_transfer_setup(uaa->device, 776261260Shselasky &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config, 777261260Shselasky WSP_N_TRANSFER, sc, &sc->sc_mutex); 778261260Shselasky if (err) { 779261260Shselasky DPRINTF("error=%s\n", usbd_errstr(err)); 780261260Shselasky goto detach; 781261260Shselasky } 782261260Shselasky if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex, 783261260Shselasky &wsp_fifo_methods, &sc->sc_fifo, 784261260Shselasky device_get_unit(dev), -1, uaa->info.bIfaceIndex, 785261260Shselasky UID_ROOT, GID_OPERATOR, 0644)) { 786261260Shselasky goto detach; 787261260Shselasky } 788261260Shselasky device_set_usb_desc(dev); 789261260Shselasky 790261260Shselasky sc->sc_hw.buttons = 3; 791261260Shselasky sc->sc_hw.iftype = MOUSE_IF_USB; 792261260Shselasky sc->sc_hw.type = MOUSE_PAD; 793261260Shselasky sc->sc_hw.model = MOUSE_MODEL_GENERIC; 794261260Shselasky sc->sc_mode.protocol = MOUSE_PROTO_MSC; 795261260Shselasky sc->sc_mode.rate = -1; 796261260Shselasky sc->sc_mode.resolution = MOUSE_RES_UNKNOWN; 797261260Shselasky sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 798261260Shselasky sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 799261260Shselasky sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 800261260Shselasky 801261260Shselasky sc->sc_touch = WSP_UNTOUCH; 802261260Shselasky sc->scr_mode = WSP_SCR_NONE; 803261260Shselasky 804261260Shselasky return (0); 805261260Shselasky 806261260Shselaskydetach: 807261260Shselasky wsp_detach(dev); 808261260Shselasky return (ENOMEM); 809261260Shselasky} 810261260Shselasky 811261260Shselaskystatic int 812261260Shselaskywsp_detach(device_t dev) 813261260Shselasky{ 814261260Shselasky struct wsp_softc *sc = device_get_softc(dev); 815261260Shselasky 816291065Shselasky (void) wsp_set_device_mode(sc, 0); 817261260Shselasky 818261260Shselasky mtx_lock(&sc->sc_mutex); 819261260Shselasky if (sc->sc_state & WSP_ENABLED) 820261260Shselasky wsp_disable(sc); 821261260Shselasky mtx_unlock(&sc->sc_mutex); 822261260Shselasky 823261260Shselasky usb_fifo_detach(&sc->sc_fifo); 824261260Shselasky 825261260Shselasky usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER); 826261260Shselasky 827261260Shselasky mtx_destroy(&sc->sc_mutex); 828261260Shselasky 829261260Shselasky return (0); 830261260Shselasky} 831261260Shselasky 832261260Shselaskystatic void 833261260Shselaskywsp_intr_callback(struct usb_xfer *xfer, usb_error_t error) 834261260Shselasky{ 835261260Shselasky struct wsp_softc *sc = usbd_xfer_softc(xfer); 836261260Shselasky const struct wsp_dev_params *params = sc->sc_params; 837261260Shselasky struct usb_page_cache *pc; 838261260Shselasky struct tp_finger *f; 839261260Shselasky struct tp_header *h; 840261260Shselasky struct wsp_tuning tun = wsp_tuning; 841261260Shselasky int ntouch = 0; /* the finger number in touch */ 842261260Shselasky int ibt = 0; /* button status */ 843261260Shselasky int dx = 0; 844261260Shselasky int dy = 0; 845261260Shselasky int dz = 0; 846263208Shselasky int rdx = 0; 847263208Shselasky int rdy = 0; 848263208Shselasky int rdz = 0; 849261260Shselasky int len; 850261260Shselasky int i; 851261260Shselasky 852261260Shselasky wsp_runing_rangecheck(&tun); 853261260Shselasky 854261260Shselasky if (sc->dz_count == 0) 855261260Shselasky sc->dz_count = WSP_DZ_MAX_COUNT; 856261260Shselasky 857261260Shselasky usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 858261260Shselasky 859261260Shselasky switch (USB_GET_STATE(xfer)) { 860261260Shselasky case USB_ST_TRANSFERRED: 861261260Shselasky 862262369Shselasky /* copy out received data */ 863261260Shselasky pc = usbd_xfer_get_frame(xfer, 0); 864261260Shselasky usbd_copy_out(pc, 0, sc->tp_data, len); 865261260Shselasky 866291065Shselasky if ((len < params->tp_offset + params->tp_fsize) || 867291065Shselasky ((len - params->tp_offset) % params->tp_fsize) != 0) { 868291065Shselasky DPRINTFN(WSP_LLEVEL_INFO, "Invalid length: %d, %x, %x\n", 869291065Shselasky len, sc->tp_data[0], sc->tp_data[1]); 870291065Shselasky goto tr_setup; 871291065Shselasky } 872291065Shselasky 873262369Shselasky if (len < sc->tp_datalen) { 874262369Shselasky /* make sure we don't process old data */ 875262369Shselasky memset(sc->tp_data + len, 0, sc->tp_datalen - len); 876262369Shselasky } 877262369Shselasky 878261260Shselasky h = (struct tp_header *)(sc->tp_data); 879261260Shselasky 880291065Shselasky if (params->tp_type >= TYPE2) { 881291065Shselasky ibt = sc->tp_data[params->tp_button]; 882291065Shselasky ntouch = sc->tp_data[params->tp_button - 1]; 883261260Shselasky } 884261260Shselasky /* range check */ 885261260Shselasky if (ntouch < 0) 886261260Shselasky ntouch = 0; 887261260Shselasky else if (ntouch > MAX_FINGERS) 888261260Shselasky ntouch = MAX_FINGERS; 889261260Shselasky 890261260Shselasky for (i = 0; i != ntouch; i++) { 891291065Shselasky f = (struct tp_finger *)(sc->tp_data + params->tp_offset + params->tp_delta + i * params->tp_fsize); 892261260Shselasky /* swap endianness, if any */ 893261260Shselasky if (le16toh(0x1234) != 0x1234) { 894291065Shselasky f->origin = le16toh((uint16_t)f->origin); 895291065Shselasky f->abs_x = le16toh((uint16_t)f->abs_x); 896291065Shselasky f->abs_y = le16toh((uint16_t)f->abs_y); 897291065Shselasky f->rel_x = le16toh((uint16_t)f->rel_x); 898291065Shselasky f->rel_y = le16toh((uint16_t)f->rel_y); 899291065Shselasky f->tool_major = le16toh((uint16_t)f->tool_major); 900291065Shselasky f->tool_minor = le16toh((uint16_t)f->tool_minor); 901291065Shselasky f->orientation = le16toh((uint16_t)f->orientation); 902291065Shselasky f->touch_major = le16toh((uint16_t)f->touch_major); 903291065Shselasky f->touch_minor = le16toh((uint16_t)f->touch_minor); 904291065Shselasky f->pressure = le16toh((uint16_t)f->pressure); 905291065Shselasky f->multi = le16toh((uint16_t)f->multi); 906261260Shselasky } 907291065Shselasky DPRINTFN(WSP_LLEVEL_INFO, 908291065Shselasky "[%d]ibt=%d, taps=%d, o=%4d, ax=%5d, ay=%5d, " 909291065Shselasky "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%4x, " 910291065Shselasky "tchmaj=%4d, tchmin=%4d, presure=%4d, m=%4x\n", 911291065Shselasky i, ibt, ntouch, f->origin, f->abs_x, f->abs_y, 912291065Shselasky f->rel_x, f->rel_y, f->tool_major, f->tool_minor, f->orientation, 913291065Shselasky f->touch_major, f->touch_minor, f->pressure, f->multi); 914291065Shselasky sc->pos_x[i] = f->abs_x; 915291065Shselasky sc->pos_y[i] = -f->abs_y; 916291065Shselasky sc->index[i] = f; 917261260Shselasky } 918261260Shselasky 919261260Shselasky sc->sc_status.flags &= ~MOUSE_POSCHANGED; 920261260Shselasky sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED; 921261260Shselasky sc->sc_status.obutton = sc->sc_status.button; 922261260Shselasky sc->sc_status.button = 0; 923261260Shselasky 924261260Shselasky if (ibt != 0) { 925331997Shselasky if ((params->caps & HAS_INTEGRATED_BUTTON) && ntouch == 2) 926331997Shselasky sc->sc_status.button |= MOUSE_BUTTON3DOWN; 927331997Shselasky else if ((params->caps & HAS_INTEGRATED_BUTTON) && ntouch == 3) 928331997Shselasky sc->sc_status.button |= MOUSE_BUTTON2DOWN; 929331997Shselasky else 930331997Shselasky sc->sc_status.button |= MOUSE_BUTTON1DOWN; 931261260Shselasky sc->ibtn = 1; 932261260Shselasky } 933291065Shselasky sc->intr_count++; 934261260Shselasky 935261509Shselasky if (sc->ntaps < ntouch) { 936261509Shselasky switch (ntouch) { 937261260Shselasky case 1: 938291065Shselasky if (sc->index[0]->touch_major > tun.pressure_tap_threshold && 939291065Shselasky sc->index[0]->tool_major <= 1200) 940261260Shselasky sc->ntaps = 1; 941261260Shselasky break; 942261260Shselasky case 2: 943291065Shselasky if (sc->index[0]->touch_major > tun.pressure_tap_threshold-30 && 944291065Shselasky sc->index[1]->touch_major > tun.pressure_tap_threshold-30) 945261260Shselasky sc->ntaps = 2; 946261260Shselasky break; 947261260Shselasky case 3: 948291065Shselasky if (sc->index[0]->touch_major > tun.pressure_tap_threshold-40 && 949291065Shselasky sc->index[1]->touch_major > tun.pressure_tap_threshold-40 && 950291065Shselasky sc->index[2]->touch_major > tun.pressure_tap_threshold-40) 951261260Shselasky sc->ntaps = 3; 952261260Shselasky break; 953261260Shselasky default: 954261260Shselasky break; 955261260Shselasky } 956261260Shselasky } 957261509Shselasky if (ntouch == 2) { 958261260Shselasky sc->distance = max(sc->distance, max( 959261260Shselasky abs(sc->pos_x[0] - sc->pos_x[1]), 960261260Shselasky abs(sc->pos_y[0] - sc->pos_y[1]))); 961261260Shselasky } 962291065Shselasky if (sc->index[0]->touch_major < tun.pressure_untouch_threshold && 963261260Shselasky sc->sc_status.button == 0) { 964261260Shselasky sc->sc_touch = WSP_UNTOUCH; 965261260Shselasky if (sc->intr_count < WSP_TAP_MAX_COUNT && 966261260Shselasky sc->intr_count > WSP_TAP_THRESHOLD && 967261260Shselasky sc->ntaps && sc->ibtn == 0) { 968261260Shselasky /* 969261260Shselasky * Add a pair of events (button-down and 970261260Shselasky * button-up). 971261260Shselasky */ 972261260Shselasky switch (sc->ntaps) { 973261260Shselasky case 1: 974262369Shselasky if (!(params->caps & HAS_INTEGRATED_BUTTON)) { 975262369Shselasky wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN); 976262369Shselasky DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n"); 977262369Shselasky } 978261260Shselasky break; 979261260Shselasky case 2: 980263072Shselasky DPRINTFN(WSP_LLEVEL_INFO, "sum_x=%5d, sum_y=%5d\n", 981263072Shselasky sc->dx_sum, sc->dy_sum); 982263072Shselasky if (sc->distance < MAX_DISTANCE && abs(sc->dx_sum) < 5 && 983263072Shselasky abs(sc->dy_sum) < 5) { 984261260Shselasky wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN); 985263072Shselasky DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n"); 986263072Shselasky } 987261260Shselasky break; 988261260Shselasky case 3: 989261260Shselasky wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN); 990261260Shselasky break; 991261260Shselasky default: 992261260Shselasky /* we don't handle taps of more than three fingers */ 993261260Shselasky break; 994261260Shselasky } 995261260Shselasky wsp_add_to_queue(sc, 0, 0, 0, 0); /* button release */ 996261260Shselasky } 997263072Shselasky if ((sc->dt_sum / tun.scr_hor_threshold) != 0 && 998261260Shselasky sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) { 999261260Shselasky 1000261260Shselasky /* 1001261260Shselasky * translate T-axis into button presses 1002261260Shselasky * until further 1003261260Shselasky */ 1004261260Shselasky if (sc->dt_sum > 0) 1005261260Shselasky wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3); 1006261260Shselasky else if (sc->dt_sum < 0) 1007261260Shselasky wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4); 1008261260Shselasky } 1009261260Shselasky sc->dz_count = WSP_DZ_MAX_COUNT; 1010261260Shselasky sc->dz_sum = 0; 1011261260Shselasky sc->intr_count = 0; 1012261260Shselasky sc->ibtn = 0; 1013261260Shselasky sc->ntaps = 0; 1014261260Shselasky sc->finger = 0; 1015261260Shselasky sc->distance = 0; 1016261260Shselasky sc->dt_sum = 0; 1017261260Shselasky sc->dx_sum = 0; 1018261260Shselasky sc->dy_sum = 0; 1019263208Shselasky sc->rdx = 0; 1020263208Shselasky sc->rdy = 0; 1021263208Shselasky sc->rdz = 0; 1022261260Shselasky sc->scr_mode = WSP_SCR_NONE; 1023291065Shselasky } else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold && 1024261260Shselasky sc->sc_touch == WSP_UNTOUCH) { /* ignore first touch */ 1025261260Shselasky sc->sc_touch = WSP_FIRST_TOUCH; 1026291065Shselasky } else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold && 1027261260Shselasky sc->sc_touch == WSP_FIRST_TOUCH) { /* ignore second touch */ 1028261260Shselasky sc->sc_touch = WSP_SECOND_TOUCH; 1029261260Shselasky DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n", 1030261260Shselasky sc->pre_pos_x, sc->pre_pos_y); 1031261260Shselasky } else { 1032261260Shselasky if (sc->sc_touch == WSP_SECOND_TOUCH) 1033261260Shselasky sc->sc_touch = WSP_TOUCHING; 1034261260Shselasky 1035261509Shselasky if (ntouch != 0 && 1036291065Shselasky sc->index[0]->touch_major >= tun.pressure_touch_threshold) { 1037261260Shselasky dx = sc->pos_x[0] - sc->pre_pos_x; 1038261260Shselasky dy = sc->pos_y[0] - sc->pre_pos_y; 1039261509Shselasky 1040291065Shselasky /* Ignore movement during button is releasing */ 1041291065Shselasky if (sc->ibtn != 0 && sc->sc_status.button == 0) 1042291065Shselasky dx = dy = 0; 1043291065Shselasky 1044261509Shselasky /* Ignore movement if ntouch changed */ 1045291065Shselasky if (sc->o_ntouch != ntouch) 1046291065Shselasky dx = dy = 0; 1047261509Shselasky 1048291065Shselasky /* Ignore unexpeted movment when typing */ 1049291065Shselasky if (ntouch == 1 && sc->index[0]->tool_major > 1200) 1050291065Shselasky dx = dy = 0; 1051291065Shselasky 1052291065Shselasky if (sc->ibtn != 0 && ntouch == 1 && 1053291065Shselasky sc->intr_count < WSP_TAP_MAX_COUNT && 1054291065Shselasky abs(sc->dx_sum) < 1 && abs(sc->dy_sum) < 1 ) 1055291065Shselasky dx = dy = 0; 1056291065Shselasky 1057261509Shselasky if (ntouch == 2 && sc->sc_status.button != 0) { 1058261260Shselasky dx = sc->pos_x[sc->finger] - sc->pre_pos_x; 1059261260Shselasky dy = sc->pos_y[sc->finger] - sc->pre_pos_y; 1060261509Shselasky 1061261509Shselasky /* 1062261509Shselasky * Ignore movement of switch finger or 1063261509Shselasky * movement from ibt=0 to ibt=1 1064261509Shselasky */ 1065291065Shselasky if (sc->index[0]->origin == 0 || sc->index[1]->origin == 0 || 1066261509Shselasky sc->sc_status.obutton != sc->sc_status.button) { 1067291065Shselasky dx = dy = 0; 1068261260Shselasky sc->finger = 0; 1069261260Shselasky } 1070291065Shselasky if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) < 1071291065Shselasky (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) && 1072261260Shselasky sc->finger == 0) { 1073261260Shselasky sc->sc_touch = WSP_SECOND_TOUCH; 1074291065Shselasky dx = dy = 0; 1075261260Shselasky sc->finger = 1; 1076261260Shselasky } 1077291065Shselasky if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) >= 1078291065Shselasky (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) && 1079261260Shselasky sc->finger == 1) { 1080261260Shselasky sc->sc_touch = WSP_SECOND_TOUCH; 1081291065Shselasky dx = dy = 0; 1082261260Shselasky sc->finger = 0; 1083261260Shselasky } 1084261260Shselasky DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n", 1085261260Shselasky dx, dy, sc->finger); 1086261260Shselasky } 1087263208Shselasky if (sc->dz_count--) { 1088263208Shselasky rdz = (dy + sc->rdz) % tun.scale_factor; 1089263208Shselasky sc->dz_sum -= (dy + sc->rdz) / tun.scale_factor; 1090263208Shselasky sc->rdz = rdz; 1091263208Shselasky } 1092261260Shselasky if ((sc->dz_sum / tun.z_factor) != 0) 1093261260Shselasky sc->dz_count = 0; 1094261260Shselasky } 1095263208Shselasky rdx = (dx + sc->rdx) % tun.scale_factor; 1096263208Shselasky dx = (dx + sc->rdx) / tun.scale_factor; 1097263208Shselasky sc->rdx = rdx; 1098263208Shselasky 1099263208Shselasky rdy = (dy + sc->rdy) % tun.scale_factor; 1100263208Shselasky dy = (dy + sc->rdy) / tun.scale_factor; 1101263208Shselasky sc->rdy = rdy; 1102263208Shselasky 1103261260Shselasky sc->dx_sum += dx; 1104261260Shselasky sc->dy_sum += dy; 1105261260Shselasky 1106261509Shselasky if (ntouch == 2 && sc->sc_status.button == 0) { 1107261260Shselasky if (sc->scr_mode == WSP_SCR_NONE && 1108263072Shselasky abs(sc->dx_sum) + abs(sc->dy_sum) > tun.scr_hor_threshold) 1109261260Shselasky sc->scr_mode = abs(sc->dx_sum) > 1110291065Shselasky abs(sc->dy_sum) * 2 ? WSP_SCR_HOR : WSP_SCR_VER; 1111261260Shselasky DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n", 1112261260Shselasky sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum); 1113261260Shselasky if (sc->scr_mode == WSP_SCR_HOR) 1114261260Shselasky sc->dt_sum += dx; 1115261260Shselasky else 1116261260Shselasky sc->dt_sum = 0; 1117261260Shselasky 1118291065Shselasky dx = dy = 0; 1119261260Shselasky if (sc->dz_count == 0) 1120261260Shselasky dz = sc->dz_sum / tun.z_factor; 1121263072Shselasky if (sc->scr_mode == WSP_SCR_HOR || 1122263072Shselasky abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE || 1123261260Shselasky abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE) 1124261260Shselasky dz = 0; 1125261260Shselasky } 1126291065Shselasky if (ntouch == 3) 1127291065Shselasky dx = dy = dz = 0; 1128261260Shselasky if (sc->intr_count < WSP_TAP_MAX_COUNT && 1129291065Shselasky abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) 1130261260Shselasky dx = dy = dz = 0; 1131291065Shselasky else 1132261260Shselasky sc->intr_count = WSP_TAP_MAX_COUNT; 1133261260Shselasky if (dx || dy || dz) 1134261260Shselasky sc->sc_status.flags |= MOUSE_POSCHANGED; 1135261260Shselasky DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n", 1136261260Shselasky dx, dy, dz, sc->sc_touch, sc->sc_status.button); 1137261260Shselasky sc->sc_status.dx += dx; 1138261260Shselasky sc->sc_status.dy += dy; 1139261260Shselasky sc->sc_status.dz += dz; 1140261260Shselasky 1141261260Shselasky wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button); 1142263208Shselasky if (sc->dz_count == 0) { 1143261260Shselasky sc->dz_sum = 0; 1144263208Shselasky sc->rdz = 0; 1145263208Shselasky } 1146261260Shselasky 1147261260Shselasky } 1148261260Shselasky sc->pre_pos_x = sc->pos_x[0]; 1149261260Shselasky sc->pre_pos_y = sc->pos_y[0]; 1150261260Shselasky 1151261509Shselasky if (ntouch == 2 && sc->sc_status.button != 0) { 1152261260Shselasky sc->pre_pos_x = sc->pos_x[sc->finger]; 1153261260Shselasky sc->pre_pos_y = sc->pos_y[sc->finger]; 1154261260Shselasky } 1155261509Shselasky sc->o_ntouch = ntouch; 1156261509Shselasky 1157261260Shselasky case USB_ST_SETUP: 1158261260Shselaskytr_setup: 1159261260Shselasky /* check if we can put more data into the FIFO */ 1160261260Shselasky if (usb_fifo_put_bytes_max( 1161261260Shselasky sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { 1162261260Shselasky usbd_xfer_set_frame_len(xfer, 0, 1163262369Shselasky sc->tp_datalen); 1164261260Shselasky usbd_transfer_submit(xfer); 1165261260Shselasky } 1166261260Shselasky break; 1167261260Shselasky 1168261260Shselasky default: /* Error */ 1169261260Shselasky if (error != USB_ERR_CANCELLED) { 1170261260Shselasky /* try clear stall first */ 1171261260Shselasky usbd_xfer_set_stall(xfer); 1172261260Shselasky goto tr_setup; 1173261260Shselasky } 1174261260Shselasky break; 1175261260Shselasky } 1176261260Shselasky} 1177261260Shselasky 1178261260Shselaskystatic void 1179261260Shselaskywsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz, 1180261260Shselasky uint32_t buttons_in) 1181261260Shselasky{ 1182261260Shselasky uint32_t buttons_out; 1183261260Shselasky uint8_t buf[8]; 1184261260Shselasky 1185261260Shselasky dx = imin(dx, 254); 1186261260Shselasky dx = imax(dx, -256); 1187261260Shselasky dy = imin(dy, 254); 1188261260Shselasky dy = imax(dy, -256); 1189261260Shselasky dz = imin(dz, 126); 1190261260Shselasky dz = imax(dz, -128); 1191261260Shselasky 1192261260Shselasky buttons_out = MOUSE_MSC_BUTTONS; 1193261260Shselasky if (buttons_in & MOUSE_BUTTON1DOWN) 1194261260Shselasky buttons_out &= ~MOUSE_MSC_BUTTON1UP; 1195261260Shselasky else if (buttons_in & MOUSE_BUTTON2DOWN) 1196261260Shselasky buttons_out &= ~MOUSE_MSC_BUTTON2UP; 1197261260Shselasky else if (buttons_in & MOUSE_BUTTON3DOWN) 1198261260Shselasky buttons_out &= ~MOUSE_MSC_BUTTON3UP; 1199261260Shselasky 1200261260Shselasky /* Encode the mouse data in standard format; refer to mouse(4) */ 1201261260Shselasky buf[0] = sc->sc_mode.syncmask[1]; 1202261260Shselasky buf[0] |= buttons_out; 1203261260Shselasky buf[1] = dx >> 1; 1204261260Shselasky buf[2] = dy >> 1; 1205261260Shselasky buf[3] = dx - (dx >> 1); 1206261260Shselasky buf[4] = dy - (dy >> 1); 1207261260Shselasky /* Encode extra bytes for level 1 */ 1208261260Shselasky if (sc->sc_mode.level == 1) { 1209261260Shselasky buf[5] = dz >> 1; /* dz / 2 */ 1210261260Shselasky buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */ 1211261260Shselasky buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS); 1212261260Shselasky } 1213261260Shselasky usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf, 1214261260Shselasky sc->sc_mode.packetsize, 1); 1215261260Shselasky} 1216261260Shselasky 1217261260Shselaskystatic void 1218261260Shselaskywsp_reset_buf(struct wsp_softc *sc) 1219261260Shselasky{ 1220261260Shselasky /* reset read queue */ 1221261260Shselasky usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]); 1222261260Shselasky} 1223261260Shselasky 1224261260Shselaskystatic void 1225261260Shselaskywsp_start_read(struct usb_fifo *fifo) 1226261260Shselasky{ 1227261260Shselasky struct wsp_softc *sc = usb_fifo_softc(fifo); 1228261260Shselasky int rate; 1229261260Shselasky 1230261260Shselasky /* Check if we should override the default polling interval */ 1231261260Shselasky rate = sc->sc_pollrate; 1232261260Shselasky /* Range check rate */ 1233261260Shselasky if (rate > 1000) 1234261260Shselasky rate = 1000; 1235261260Shselasky /* Check for set rate */ 1236261260Shselasky if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) { 1237261260Shselasky /* Stop current transfer, if any */ 1238261260Shselasky usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]); 1239261260Shselasky /* Set new interval */ 1240261260Shselasky usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate); 1241261260Shselasky /* Only set pollrate once */ 1242261260Shselasky sc->sc_pollrate = 0; 1243261260Shselasky } 1244261260Shselasky usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]); 1245261260Shselasky} 1246261260Shselasky 1247261260Shselaskystatic void 1248261260Shselaskywsp_stop_read(struct usb_fifo *fifo) 1249261260Shselasky{ 1250261260Shselasky struct wsp_softc *sc = usb_fifo_softc(fifo); 1251261260Shselasky 1252261260Shselasky usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]); 1253261260Shselasky} 1254261260Shselasky 1255261260Shselasky 1256261260Shselaskystatic int 1257261260Shselaskywsp_open(struct usb_fifo *fifo, int fflags) 1258261260Shselasky{ 1259261260Shselasky DPRINTFN(WSP_LLEVEL_INFO, "\n"); 1260261260Shselasky 1261261260Shselasky if (fflags & FREAD) { 1262261260Shselasky struct wsp_softc *sc = usb_fifo_softc(fifo); 1263261260Shselasky int rc; 1264261260Shselasky 1265261260Shselasky if (sc->sc_state & WSP_ENABLED) 1266261260Shselasky return (EBUSY); 1267261260Shselasky 1268261260Shselasky if (usb_fifo_alloc_buffer(fifo, 1269261260Shselasky WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) { 1270261260Shselasky return (ENOMEM); 1271261260Shselasky } 1272261260Shselasky rc = wsp_enable(sc); 1273261260Shselasky if (rc != 0) { 1274261260Shselasky usb_fifo_free_buffer(fifo); 1275261260Shselasky return (rc); 1276261260Shselasky } 1277261260Shselasky } 1278261260Shselasky return (0); 1279261260Shselasky} 1280261260Shselasky 1281261260Shselaskystatic void 1282261260Shselaskywsp_close(struct usb_fifo *fifo, int fflags) 1283261260Shselasky{ 1284261260Shselasky if (fflags & FREAD) { 1285261260Shselasky struct wsp_softc *sc = usb_fifo_softc(fifo); 1286261260Shselasky 1287261260Shselasky wsp_disable(sc); 1288261260Shselasky usb_fifo_free_buffer(fifo); 1289261260Shselasky } 1290261260Shselasky} 1291261260Shselasky 1292261260Shselaskyint 1293261260Shselaskywsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 1294261260Shselasky{ 1295261260Shselasky struct wsp_softc *sc = usb_fifo_softc(fifo); 1296261260Shselasky mousemode_t mode; 1297261260Shselasky int error = 0; 1298261260Shselasky 1299261260Shselasky mtx_lock(&sc->sc_mutex); 1300261260Shselasky 1301261260Shselasky switch (cmd) { 1302261260Shselasky case MOUSE_GETHWINFO: 1303261260Shselasky *(mousehw_t *)addr = sc->sc_hw; 1304261260Shselasky break; 1305261260Shselasky case MOUSE_GETMODE: 1306261260Shselasky *(mousemode_t *)addr = sc->sc_mode; 1307261260Shselasky break; 1308261260Shselasky case MOUSE_SETMODE: 1309261260Shselasky mode = *(mousemode_t *)addr; 1310261260Shselasky 1311261260Shselasky if (mode.level == -1) 1312261260Shselasky /* Don't change the current setting */ 1313261260Shselasky ; 1314261260Shselasky else if ((mode.level < 0) || (mode.level > 1)) { 1315261260Shselasky error = EINVAL; 1316261260Shselasky goto done; 1317261260Shselasky } 1318261260Shselasky sc->sc_mode.level = mode.level; 1319261260Shselasky sc->sc_pollrate = mode.rate; 1320261260Shselasky sc->sc_hw.buttons = 3; 1321261260Shselasky 1322261260Shselasky if (sc->sc_mode.level == 0) { 1323261260Shselasky sc->sc_mode.protocol = MOUSE_PROTO_MSC; 1324261260Shselasky sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 1325261260Shselasky sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 1326261260Shselasky sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 1327261260Shselasky } else if (sc->sc_mode.level == 1) { 1328261260Shselasky sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 1329261260Shselasky sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 1330261260Shselasky sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 1331261260Shselasky sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 1332261260Shselasky } 1333261260Shselasky wsp_reset_buf(sc); 1334261260Shselasky break; 1335261260Shselasky case MOUSE_GETLEVEL: 1336261260Shselasky *(int *)addr = sc->sc_mode.level; 1337261260Shselasky break; 1338261260Shselasky case MOUSE_SETLEVEL: 1339261260Shselasky if (*(int *)addr < 0 || *(int *)addr > 1) { 1340261260Shselasky error = EINVAL; 1341261260Shselasky goto done; 1342261260Shselasky } 1343261260Shselasky sc->sc_mode.level = *(int *)addr; 1344261260Shselasky sc->sc_hw.buttons = 3; 1345261260Shselasky 1346261260Shselasky if (sc->sc_mode.level == 0) { 1347261260Shselasky sc->sc_mode.protocol = MOUSE_PROTO_MSC; 1348261260Shselasky sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 1349261260Shselasky sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 1350261260Shselasky sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 1351261260Shselasky } else if (sc->sc_mode.level == 1) { 1352261260Shselasky sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 1353261260Shselasky sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 1354261260Shselasky sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 1355261260Shselasky sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 1356261260Shselasky } 1357261260Shselasky wsp_reset_buf(sc); 1358261260Shselasky break; 1359261260Shselasky case MOUSE_GETSTATUS:{ 1360261260Shselasky mousestatus_t *status = (mousestatus_t *)addr; 1361261260Shselasky 1362261260Shselasky *status = sc->sc_status; 1363261260Shselasky sc->sc_status.obutton = sc->sc_status.button; 1364261260Shselasky sc->sc_status.button = 0; 1365261260Shselasky sc->sc_status.dx = 0; 1366261260Shselasky sc->sc_status.dy = 0; 1367261260Shselasky sc->sc_status.dz = 0; 1368261260Shselasky 1369261260Shselasky if (status->dx || status->dy || status->dz) 1370261260Shselasky status->flags |= MOUSE_POSCHANGED; 1371261260Shselasky if (status->button != status->obutton) 1372261260Shselasky status->flags |= MOUSE_BUTTONSCHANGED; 1373261260Shselasky break; 1374261260Shselasky } 1375261260Shselasky default: 1376261260Shselasky error = ENOTTY; 1377261260Shselasky } 1378261260Shselasky 1379261260Shselaskydone: 1380261260Shselasky mtx_unlock(&sc->sc_mutex); 1381261260Shselasky return (error); 1382261260Shselasky} 1383261260Shselasky 1384261260Shselaskystatic device_method_t wsp_methods[] = { 1385261260Shselasky /* Device interface */ 1386261260Shselasky DEVMETHOD(device_probe, wsp_probe), 1387261260Shselasky DEVMETHOD(device_attach, wsp_attach), 1388261260Shselasky DEVMETHOD(device_detach, wsp_detach), 1389261260Shselasky DEVMETHOD_END 1390261260Shselasky}; 1391261260Shselasky 1392261260Shselaskystatic driver_t wsp_driver = { 1393261260Shselasky .name = WSP_DRIVER_NAME, 1394261260Shselasky .methods = wsp_methods, 1395261260Shselasky .size = sizeof(struct wsp_softc) 1396261260Shselasky}; 1397261260Shselasky 1398261260Shselaskystatic devclass_t wsp_devclass; 1399261260Shselasky 1400261260ShselaskyDRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0); 1401261260ShselaskyMODULE_DEPEND(wsp, usb, 1, 1, 1); 1402261260ShselaskyMODULE_VERSION(wsp, 1); 1403