Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Apple "Magic" Wireless Mouse driver |
| 4 | * |
| 5 | * Copyright (c) 2010 Michael Poole <mdpoole@troilus.org> |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 6 | * Copyright (c) 2010 Chase Douglas <chase.douglas@canonical.com> |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 10 | */ |
| 11 | |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/hid.h> |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 16 | #include <linux/input/mt.h> |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 17 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 20 | |
| 21 | #include "hid-ids.h" |
| 22 | |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 23 | static bool emulate_3button = true; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 24 | module_param(emulate_3button, bool, 0644); |
| 25 | MODULE_PARM_DESC(emulate_3button, "Emulate a middle button"); |
| 26 | |
| 27 | static int middle_button_start = -350; |
| 28 | static int middle_button_stop = +350; |
| 29 | |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 30 | static bool emulate_scroll_wheel = true; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 31 | module_param(emulate_scroll_wheel, bool, 0644); |
| 32 | MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel"); |
| 33 | |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 34 | static unsigned int scroll_speed = 32; |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 35 | static int param_set_scroll_speed(const char *val, |
| 36 | const struct kernel_param *kp) { |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 37 | unsigned long speed; |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 38 | if (!val || kstrtoul(val, 0, &speed) || speed > 63) |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 39 | return -EINVAL; |
| 40 | scroll_speed = speed; |
| 41 | return 0; |
| 42 | } |
| 43 | module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644); |
| 44 | MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)"); |
| 45 | |
Chase Douglas | 9846f35 | 2010-06-02 10:28:27 -0400 | [diff] [blame] | 46 | static bool scroll_acceleration = false; |
| 47 | module_param(scroll_acceleration, bool, 0644); |
| 48 | MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events"); |
| 49 | |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 50 | static bool report_undeciphered; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 51 | module_param(report_undeciphered, bool, 0644); |
| 52 | MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event"); |
| 53 | |
José Expósito | 5768701 | 2021-12-12 19:21:00 +0100 | [diff] [blame] | 54 | #define TRACKPAD2_2021_BT_VERSION 0x110 |
| 55 | |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 56 | #define TRACKPAD_REPORT_ID 0x28 |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 57 | #define TRACKPAD2_USB_REPORT_ID 0x02 |
| 58 | #define TRACKPAD2_BT_REPORT_ID 0x31 |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 59 | #define MOUSE_REPORT_ID 0x29 |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 60 | #define MOUSE2_REPORT_ID 0x12 |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 61 | #define DOUBLE_REPORT_ID 0xf7 |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 62 | #define USB_BATTERY_TIMEOUT_MS 60000 |
| 63 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 64 | /* These definitions are not precise, but they're close enough. (Bits |
| 65 | * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem |
| 66 | * to be some kind of bit mask -- 0x20 may be a near-field reading, |
| 67 | * and 0x40 is actual contact, and 0x10 may be a start/stop or change |
| 68 | * indication.) |
| 69 | */ |
| 70 | #define TOUCH_STATE_MASK 0xf0 |
| 71 | #define TOUCH_STATE_NONE 0x00 |
| 72 | #define TOUCH_STATE_START 0x30 |
| 73 | #define TOUCH_STATE_DRAG 0x40 |
| 74 | |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 75 | /* Number of high-resolution events for each low-resolution detent. */ |
| 76 | #define SCROLL_HR_STEPS 10 |
| 77 | #define SCROLL_HR_MULT (120 / SCROLL_HR_STEPS) |
José Expósito | 9d60648 | 2021-07-07 17:58:22 +0200 | [diff] [blame] | 78 | #define SCROLL_HR_THRESHOLD 90 /* units */ |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 79 | #define SCROLL_ACCEL_DEFAULT 7 |
| 80 | |
Chase Douglas | 4f6fdf0 | 2011-08-05 09:16:57 -0700 | [diff] [blame] | 81 | /* Touch surface information. Dimension is in hundredths of a mm, min and max |
| 82 | * are in units. */ |
| 83 | #define MOUSE_DIMENSION_X (float)9056 |
| 84 | #define MOUSE_MIN_X -1100 |
| 85 | #define MOUSE_MAX_X 1258 |
| 86 | #define MOUSE_RES_X ((MOUSE_MAX_X - MOUSE_MIN_X) / (MOUSE_DIMENSION_X / 100)) |
| 87 | #define MOUSE_DIMENSION_Y (float)5152 |
| 88 | #define MOUSE_MIN_Y -1589 |
| 89 | #define MOUSE_MAX_Y 2047 |
| 90 | #define MOUSE_RES_Y ((MOUSE_MAX_Y - MOUSE_MIN_Y) / (MOUSE_DIMENSION_Y / 100)) |
| 91 | |
| 92 | #define TRACKPAD_DIMENSION_X (float)13000 |
| 93 | #define TRACKPAD_MIN_X -2909 |
| 94 | #define TRACKPAD_MAX_X 3167 |
| 95 | #define TRACKPAD_RES_X \ |
| 96 | ((TRACKPAD_MAX_X - TRACKPAD_MIN_X) / (TRACKPAD_DIMENSION_X / 100)) |
| 97 | #define TRACKPAD_DIMENSION_Y (float)11000 |
| 98 | #define TRACKPAD_MIN_Y -2456 |
| 99 | #define TRACKPAD_MAX_Y 2565 |
| 100 | #define TRACKPAD_RES_Y \ |
| 101 | ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100)) |
| 102 | |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 103 | #define TRACKPAD2_DIMENSION_X (float)16000 |
| 104 | #define TRACKPAD2_MIN_X -3678 |
| 105 | #define TRACKPAD2_MAX_X 3934 |
| 106 | #define TRACKPAD2_RES_X \ |
| 107 | ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100)) |
| 108 | #define TRACKPAD2_DIMENSION_Y (float)11490 |
| 109 | #define TRACKPAD2_MIN_Y -2478 |
| 110 | #define TRACKPAD2_MAX_Y 2587 |
| 111 | #define TRACKPAD2_RES_Y \ |
| 112 | ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100)) |
| 113 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 114 | /** |
| 115 | * struct magicmouse_sc - Tracks Magic Mouse-specific data. |
| 116 | * @input: Input device through which we report events. |
| 117 | * @quirks: Currently unused. |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 118 | * @ntouches: Number of touches in most recent touch report. |
| 119 | * @scroll_accel: Number of consecutive scroll motions. |
| 120 | * @scroll_jiffies: Time of last scroll motion. |
| 121 | * @touches: Most recent data for a touch, indexed by tracking ID. |
| 122 | * @tracking_ids: Mapping of current touch input data to @touches. |
| 123 | */ |
| 124 | struct magicmouse_sc { |
| 125 | struct input_dev *input; |
| 126 | unsigned long quirks; |
| 127 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 128 | int ntouches; |
| 129 | int scroll_accel; |
| 130 | unsigned long scroll_jiffies; |
| 131 | |
| 132 | struct { |
| 133 | short x; |
| 134 | short y; |
Chase Douglas | c042668 | 2010-06-20 21:32:31 -0400 | [diff] [blame] | 135 | short scroll_x; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 136 | short scroll_y; |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 137 | short scroll_x_hr; |
| 138 | short scroll_y_hr; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 139 | u8 size; |
José Expósito | 9d60648 | 2021-07-07 17:58:22 +0200 | [diff] [blame] | 140 | bool scroll_x_active; |
| 141 | bool scroll_y_active; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 142 | } touches[16]; |
| 143 | int tracking_ids[16]; |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 144 | |
| 145 | struct hid_device *hdev; |
| 146 | struct delayed_work work; |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 147 | struct timer_list battery_timer; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static int magicmouse_firm_touch(struct magicmouse_sc *msc) |
| 151 | { |
| 152 | int touch = -1; |
| 153 | int ii; |
| 154 | |
| 155 | /* If there is only one "firm" touch, set touch to its |
| 156 | * tracking ID. |
| 157 | */ |
| 158 | for (ii = 0; ii < msc->ntouches; ii++) { |
| 159 | int idx = msc->tracking_ids[ii]; |
| 160 | if (msc->touches[idx].size < 8) { |
| 161 | /* Ignore this touch. */ |
| 162 | } else if (touch >= 0) { |
| 163 | touch = -1; |
| 164 | break; |
| 165 | } else { |
| 166 | touch = idx; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return touch; |
| 171 | } |
| 172 | |
| 173 | static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state) |
| 174 | { |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 175 | int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 | |
| 176 | test_bit(BTN_RIGHT, msc->input->key) << 1 | |
| 177 | test_bit(BTN_MIDDLE, msc->input->key) << 2; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 178 | |
| 179 | if (emulate_3button) { |
| 180 | int id; |
| 181 | |
| 182 | /* If some button was pressed before, keep it held |
| 183 | * down. Otherwise, if there's exactly one firm |
| 184 | * touch, use that to override the mouse's guess. |
| 185 | */ |
| 186 | if (state == 0) { |
| 187 | /* The button was released. */ |
| 188 | } else if (last_state != 0) { |
| 189 | state = last_state; |
| 190 | } else if ((id = magicmouse_firm_touch(msc)) >= 0) { |
| 191 | int x = msc->touches[id].x; |
| 192 | if (x < middle_button_start) |
| 193 | state = 1; |
| 194 | else if (x > middle_button_stop) |
| 195 | state = 2; |
| 196 | else |
| 197 | state = 4; |
| 198 | } /* else: we keep the mouse's guess */ |
| 199 | |
| 200 | input_report_key(msc->input, BTN_MIDDLE, state & 4); |
| 201 | } |
| 202 | |
| 203 | input_report_key(msc->input, BTN_LEFT, state & 1); |
| 204 | input_report_key(msc->input, BTN_RIGHT, state & 2); |
| 205 | |
| 206 | if (state != last_state) |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 207 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata) |
| 211 | { |
| 212 | struct input_dev *input = msc->input; |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 213 | int id, x, y, size, orientation, touch_major, touch_minor, state, down; |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 214 | int pressure = 0; |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 215 | |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 216 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE || |
| 217 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 218 | id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf; |
| 219 | x = (tdata[1] << 28 | tdata[0] << 20) >> 20; |
| 220 | y = -((tdata[2] << 24 | tdata[1] << 16) >> 20); |
| 221 | size = tdata[5] & 0x3f; |
| 222 | orientation = (tdata[6] >> 2) - 32; |
| 223 | touch_major = tdata[3]; |
| 224 | touch_minor = tdata[4]; |
| 225 | state = tdata[7] & TOUCH_STATE_MASK; |
| 226 | down = state != TOUCH_STATE_NONE; |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 227 | } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { |
| 228 | id = tdata[8] & 0xf; |
| 229 | x = (tdata[1] << 27 | tdata[0] << 19) >> 19; |
| 230 | y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19); |
| 231 | size = tdata[6]; |
| 232 | orientation = (tdata[8] >> 5) - 4; |
| 233 | touch_major = tdata[4]; |
| 234 | touch_minor = tdata[5]; |
| 235 | pressure = tdata[7]; |
| 236 | state = tdata[3] & 0xC0; |
| 237 | down = state == 0x80; |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 238 | } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */ |
| 239 | id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf; |
| 240 | x = (tdata[1] << 27 | tdata[0] << 19) >> 19; |
| 241 | y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19); |
| 242 | size = tdata[6] & 0x3f; |
| 243 | orientation = (tdata[7] >> 2) - 32; |
| 244 | touch_major = tdata[4]; |
| 245 | touch_minor = tdata[5]; |
| 246 | state = tdata[8] & TOUCH_STATE_MASK; |
| 247 | down = state != TOUCH_STATE_NONE; |
| 248 | } |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 249 | |
| 250 | /* Store tracking ID and other fields. */ |
| 251 | msc->tracking_ids[raw_id] = id; |
| 252 | msc->touches[id].x = x; |
| 253 | msc->touches[id].y = y; |
Chase Douglas | 6de048b | 2010-08-31 21:56:20 -0400 | [diff] [blame] | 254 | msc->touches[id].size = size; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 255 | |
| 256 | /* If requested, emulate a scroll wheel by detecting small |
Chase Douglas | ef566d3 | 2010-06-02 10:28:25 -0400 | [diff] [blame] | 257 | * vertical touch motions. |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 258 | */ |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 259 | if (emulate_scroll_wheel && (input->id.product != |
| 260 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) { |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 261 | unsigned long now = jiffies; |
Chase Douglas | c042668 | 2010-06-20 21:32:31 -0400 | [diff] [blame] | 262 | int step_x = msc->touches[id].scroll_x - x; |
| 263 | int step_y = msc->touches[id].scroll_y - y; |
Claudia Pellegrino | a109111 | 2021-11-14 03:53:27 +0100 | [diff] [blame] | 264 | int step_hr = |
| 265 | max_t(int, |
| 266 | ((64 - (int)scroll_speed) * msc->scroll_accel) / |
| 267 | SCROLL_HR_STEPS, |
| 268 | 1); |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 269 | int step_x_hr = msc->touches[id].scroll_x_hr - x; |
| 270 | int step_y_hr = msc->touches[id].scroll_y_hr - y; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 271 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 272 | /* Calculate and apply the scroll motion. */ |
Chase Douglas | 6de048b | 2010-08-31 21:56:20 -0400 | [diff] [blame] | 273 | switch (state) { |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 274 | case TOUCH_STATE_START: |
Chase Douglas | c042668 | 2010-06-20 21:32:31 -0400 | [diff] [blame] | 275 | msc->touches[id].scroll_x = x; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 276 | msc->touches[id].scroll_y = y; |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 277 | msc->touches[id].scroll_x_hr = x; |
| 278 | msc->touches[id].scroll_y_hr = y; |
José Expósito | 9d60648 | 2021-07-07 17:58:22 +0200 | [diff] [blame] | 279 | msc->touches[id].scroll_x_active = false; |
| 280 | msc->touches[id].scroll_y_active = false; |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 281 | |
| 282 | /* Reset acceleration after half a second. */ |
| 283 | if (scroll_acceleration && time_before(now, |
| 284 | msc->scroll_jiffies + HZ / 2)) |
| 285 | msc->scroll_accel = max_t(int, |
| 286 | msc->scroll_accel - 1, 1); |
| 287 | else |
| 288 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT; |
| 289 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 290 | break; |
| 291 | case TOUCH_STATE_DRAG: |
Chase Douglas | c042668 | 2010-06-20 21:32:31 -0400 | [diff] [blame] | 292 | step_x /= (64 - (int)scroll_speed) * msc->scroll_accel; |
| 293 | if (step_x != 0) { |
| 294 | msc->touches[id].scroll_x -= step_x * |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 295 | (64 - scroll_speed) * msc->scroll_accel; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 296 | msc->scroll_jiffies = now; |
Chase Douglas | c042668 | 2010-06-20 21:32:31 -0400 | [diff] [blame] | 297 | input_report_rel(input, REL_HWHEEL, -step_x); |
| 298 | } |
| 299 | |
| 300 | step_y /= (64 - (int)scroll_speed) * msc->scroll_accel; |
| 301 | if (step_y != 0) { |
| 302 | msc->touches[id].scroll_y -= step_y * |
| 303 | (64 - scroll_speed) * msc->scroll_accel; |
| 304 | msc->scroll_jiffies = now; |
| 305 | input_report_rel(input, REL_WHEEL, step_y); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 306 | } |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 307 | |
José Expósito | 9d60648 | 2021-07-07 17:58:22 +0200 | [diff] [blame] | 308 | if (!msc->touches[id].scroll_x_active && |
| 309 | abs(step_x_hr) > SCROLL_HR_THRESHOLD) { |
| 310 | msc->touches[id].scroll_x_active = true; |
| 311 | msc->touches[id].scroll_x_hr = x; |
| 312 | step_x_hr = 0; |
| 313 | } |
| 314 | |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 315 | step_x_hr /= step_hr; |
José Expósito | 9d60648 | 2021-07-07 17:58:22 +0200 | [diff] [blame] | 316 | if (step_x_hr != 0 && |
| 317 | msc->touches[id].scroll_x_active) { |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 318 | msc->touches[id].scroll_x_hr -= step_x_hr * |
| 319 | step_hr; |
| 320 | input_report_rel(input, |
| 321 | REL_HWHEEL_HI_RES, |
| 322 | -step_x_hr * SCROLL_HR_MULT); |
| 323 | } |
| 324 | |
José Expósito | 9d60648 | 2021-07-07 17:58:22 +0200 | [diff] [blame] | 325 | if (!msc->touches[id].scroll_y_active && |
| 326 | abs(step_y_hr) > SCROLL_HR_THRESHOLD) { |
| 327 | msc->touches[id].scroll_y_active = true; |
| 328 | msc->touches[id].scroll_y_hr = y; |
| 329 | step_y_hr = 0; |
| 330 | } |
| 331 | |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 332 | step_y_hr /= step_hr; |
José Expósito | 9d60648 | 2021-07-07 17:58:22 +0200 | [diff] [blame] | 333 | if (step_y_hr != 0 && |
| 334 | msc->touches[id].scroll_y_active) { |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 335 | msc->touches[id].scroll_y_hr -= step_y_hr * |
| 336 | step_hr; |
| 337 | input_report_rel(input, |
| 338 | REL_WHEEL_HI_RES, |
| 339 | step_y_hr * SCROLL_HR_MULT); |
| 340 | } |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 345 | if (down) |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 346 | msc->ntouches++; |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 347 | |
| 348 | input_mt_slot(input, id); |
| 349 | input_mt_report_slot_state(input, MT_TOOL_FINGER, down); |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 350 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 351 | /* Generate the input events for this touch. */ |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 352 | if (down) { |
Henrik Rydberg | 921990b | 2010-08-31 21:56:24 -0400 | [diff] [blame] | 353 | input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major << 2); |
| 354 | input_report_abs(input, ABS_MT_TOUCH_MINOR, touch_minor << 2); |
Henrik Rydberg | 2d9ca4e | 2011-03-09 18:38:57 +0100 | [diff] [blame] | 355 | input_report_abs(input, ABS_MT_ORIENTATION, -orientation); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 356 | input_report_abs(input, ABS_MT_POSITION_X, x); |
| 357 | input_report_abs(input, ABS_MT_POSITION_Y, y); |
| 358 | |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 359 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) |
| 360 | input_report_abs(input, ABS_MT_PRESSURE, pressure); |
| 361 | |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 362 | if (report_undeciphered) { |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 363 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE || |
| 364 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 365 | input_event(input, EV_MSC, MSC_RAW, tdata[7]); |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 366 | else if (input->id.product != |
| 367 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 368 | input_event(input, EV_MSC, MSC_RAW, tdata[8]); |
| 369 | } |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | static int magicmouse_raw_event(struct hid_device *hdev, |
| 374 | struct hid_report *report, u8 *data, int size) |
| 375 | { |
| 376 | struct magicmouse_sc *msc = hid_get_drvdata(hdev); |
| 377 | struct input_dev *input = msc->input; |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 378 | int x = 0, y = 0, ii, clicks = 0, npoints; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 379 | |
| 380 | switch (data[0]) { |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 381 | case TRACKPAD_REPORT_ID: |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 382 | case TRACKPAD2_BT_REPORT_ID: |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 383 | /* Expect four bytes of prefix, and N*9 bytes of touch data. */ |
| 384 | if (size < 4 || ((size - 4) % 9) != 0) |
| 385 | return 0; |
| 386 | npoints = (size - 4) / 9; |
Jiri Kosina | c54def7 | 2014-08-27 09:12:24 +0200 | [diff] [blame] | 387 | if (npoints > 15) { |
| 388 | hid_warn(hdev, "invalid size value (%d) for TRACKPAD_REPORT_ID\n", |
| 389 | size); |
| 390 | return 0; |
| 391 | } |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 392 | msc->ntouches = 0; |
| 393 | for (ii = 0; ii < npoints; ii++) |
| 394 | magicmouse_emit_touch(msc, ii, data + ii * 9 + 4); |
| 395 | |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 396 | clicks = data[1]; |
| 397 | |
| 398 | /* The following bits provide a device specific timestamp. They |
| 399 | * are unused here. |
| 400 | * |
| 401 | * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10; |
| 402 | */ |
| 403 | break; |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 404 | case TRACKPAD2_USB_REPORT_ID: |
| 405 | /* Expect twelve bytes of prefix and N*9 bytes of touch data. */ |
| 406 | if (size < 12 || ((size - 12) % 9) != 0) |
| 407 | return 0; |
| 408 | npoints = (size - 12) / 9; |
| 409 | if (npoints > 15) { |
| 410 | hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n", |
| 411 | size); |
| 412 | return 0; |
| 413 | } |
| 414 | msc->ntouches = 0; |
| 415 | for (ii = 0; ii < npoints; ii++) |
| 416 | magicmouse_emit_touch(msc, ii, data + ii * 9 + 12); |
| 417 | |
| 418 | clicks = data[1]; |
| 419 | break; |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 420 | case MOUSE_REPORT_ID: |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 421 | /* Expect six bytes of prefix, and N*8 bytes of touch data. */ |
| 422 | if (size < 6 || ((size - 6) % 8) != 0) |
| 423 | return 0; |
Chase Douglas | 0228db7 | 2010-09-02 16:49:52 +0200 | [diff] [blame] | 424 | npoints = (size - 6) / 8; |
Jiri Kosina | c54def7 | 2014-08-27 09:12:24 +0200 | [diff] [blame] | 425 | if (npoints > 15) { |
| 426 | hid_warn(hdev, "invalid size value (%d) for MOUSE_REPORT_ID\n", |
| 427 | size); |
| 428 | return 0; |
| 429 | } |
Chase Douglas | 0228db7 | 2010-09-02 16:49:52 +0200 | [diff] [blame] | 430 | msc->ntouches = 0; |
| 431 | for (ii = 0; ii < npoints; ii++) |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 432 | magicmouse_emit_touch(msc, ii, data + ii * 8 + 6); |
Chase Douglas | e3612e8 | 2010-07-05 09:57:52 -0400 | [diff] [blame] | 433 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 434 | /* When emulating three-button mode, it is important |
| 435 | * to have the current touch information before |
| 436 | * generating a click event. |
| 437 | */ |
Michael Poole | 7d876c0 | 2010-07-05 10:50:09 -0400 | [diff] [blame] | 438 | x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22; |
| 439 | y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 440 | clicks = data[3]; |
Chase Douglas | c61b7ce | 2010-09-02 16:51:54 +0200 | [diff] [blame] | 441 | |
| 442 | /* The following bits provide a device specific timestamp. They |
| 443 | * are unused here. |
| 444 | * |
| 445 | * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10; |
| 446 | */ |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 447 | break; |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 448 | case MOUSE2_REPORT_ID: |
| 449 | /* Size is either 8 or (14 + 8 * N) */ |
| 450 | if (size != 8 && (size < 14 || (size - 14) % 8 != 0)) |
| 451 | return 0; |
| 452 | npoints = (size - 14) / 8; |
| 453 | if (npoints > 15) { |
| 454 | hid_warn(hdev, "invalid size value (%d) for MOUSE2_REPORT_ID\n", |
| 455 | size); |
| 456 | return 0; |
| 457 | } |
| 458 | msc->ntouches = 0; |
| 459 | for (ii = 0; ii < npoints; ii++) |
| 460 | magicmouse_emit_touch(msc, ii, data + ii * 8 + 14); |
| 461 | |
| 462 | /* When emulating three-button mode, it is important |
| 463 | * to have the current touch information before |
| 464 | * generating a click event. |
| 465 | */ |
| 466 | x = (int)((data[3] << 24) | (data[2] << 16)) >> 16; |
| 467 | y = (int)((data[5] << 24) | (data[4] << 16)) >> 16; |
| 468 | clicks = data[1]; |
| 469 | |
| 470 | /* The following bits provide a device specific timestamp. They |
| 471 | * are unused here. |
| 472 | * |
| 473 | * ts = data[11] >> 6 | data[12] << 2 | data[13] << 10; |
| 474 | */ |
| 475 | break; |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 476 | case DOUBLE_REPORT_ID: |
| 477 | /* Sometimes the trackpad sends two touch reports in one |
| 478 | * packet. |
| 479 | */ |
| 480 | magicmouse_raw_event(hdev, report, data + 2, data[1]); |
| 481 | magicmouse_raw_event(hdev, report, data + 2 + data[1], |
| 482 | size - 2 - data[1]); |
| 483 | break; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 484 | default: |
| 485 | return 0; |
| 486 | } |
| 487 | |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 488 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE || |
| 489 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 490 | magicmouse_emit_buttons(msc, clicks & 3); |
| 491 | input_report_rel(input, REL_X, x); |
| 492 | input_report_rel(input, REL_Y, y); |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 493 | } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { |
| 494 | input_mt_sync_frame(input); |
| 495 | input_report_key(input, BTN_MOUSE, clicks & 1); |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 496 | } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */ |
| 497 | input_report_key(input, BTN_MOUSE, clicks & 1); |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 498 | input_mt_report_pointer_emulation(input, true); |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 501 | input_sync(input); |
| 502 | return 1; |
| 503 | } |
| 504 | |
John Chen | 3dcc5f7 | 2021-03-30 19:33:17 +0800 | [diff] [blame] | 505 | static int magicmouse_event(struct hid_device *hdev, struct hid_field *field, |
| 506 | struct hid_usage *usage, __s32 value) |
| 507 | { |
| 508 | struct magicmouse_sc *msc = hid_get_drvdata(hdev); |
| 509 | if (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 && |
| 510 | field->report->id == MOUSE2_REPORT_ID) { |
| 511 | /* |
| 512 | * magic_mouse_raw_event has done all the work. Skip hidinput. |
| 513 | * |
| 514 | * Specifically, hidinput may modify BTN_LEFT and BTN_RIGHT, |
| 515 | * breaking emulate_3button. |
| 516 | */ |
| 517 | return 1; |
| 518 | } |
| 519 | return 0; |
| 520 | } |
| 521 | |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 522 | static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev) |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 523 | { |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 524 | int error; |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 525 | int mt_flags = 0; |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 526 | |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 527 | __set_bit(EV_KEY, input->evbit); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 528 | |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 529 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE || |
| 530 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 531 | __set_bit(BTN_LEFT, input->keybit); |
| 532 | __set_bit(BTN_RIGHT, input->keybit); |
| 533 | if (emulate_3button) |
| 534 | __set_bit(BTN_MIDDLE, input->keybit); |
| 535 | |
| 536 | __set_bit(EV_REL, input->evbit); |
| 537 | __set_bit(REL_X, input->relbit); |
| 538 | __set_bit(REL_Y, input->relbit); |
| 539 | if (emulate_scroll_wheel) { |
| 540 | __set_bit(REL_WHEEL, input->relbit); |
| 541 | __set_bit(REL_HWHEEL, input->relbit); |
José Expósito | d4b9f10 | 2021-07-07 17:58:21 +0200 | [diff] [blame] | 542 | __set_bit(REL_WHEEL_HI_RES, input->relbit); |
| 543 | __set_bit(REL_HWHEEL_HI_RES, input->relbit); |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 544 | } |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 545 | } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { |
José Expósito | 0aa45fc | 2021-12-12 19:20:59 +0100 | [diff] [blame] | 546 | /* If the trackpad has been connected to a Mac, the name is |
| 547 | * automatically personalized, e.g., "José Expósito's Trackpad". |
| 548 | * When connected through Bluetooth, the personalized name is |
| 549 | * reported, however, when connected through USB the generic |
| 550 | * name is reported. |
| 551 | * Set the device name to ensure the same driver settings get |
| 552 | * loaded, whether connected through bluetooth or USB. |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 553 | */ |
José Expósito | 5768701 | 2021-12-12 19:21:00 +0100 | [diff] [blame] | 554 | if (hdev->vendor == BT_VENDOR_ID_APPLE) { |
| 555 | if (input->id.version == TRACKPAD2_2021_BT_VERSION) |
| 556 | input->name = "Apple Inc. Magic Trackpad"; |
| 557 | else |
| 558 | input->name = "Apple Inc. Magic Trackpad 2"; |
| 559 | } else { /* USB_VENDOR_ID_APPLE */ |
José Expósito | 0aa45fc | 2021-12-12 19:20:59 +0100 | [diff] [blame] | 560 | input->name = hdev->name; |
José Expósito | 5768701 | 2021-12-12 19:21:00 +0100 | [diff] [blame] | 561 | } |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 562 | |
| 563 | __clear_bit(EV_MSC, input->evbit); |
| 564 | __clear_bit(BTN_0, input->keybit); |
| 565 | __clear_bit(BTN_RIGHT, input->keybit); |
| 566 | __clear_bit(BTN_MIDDLE, input->keybit); |
| 567 | __set_bit(BTN_MOUSE, input->keybit); |
| 568 | __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); |
| 569 | __set_bit(BTN_TOOL_FINGER, input->keybit); |
| 570 | |
| 571 | mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED | |
| 572 | INPUT_MT_TRACK; |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 573 | } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */ |
Daniel van Vugt | bca6214 | 2011-10-14 13:39:34 +0800 | [diff] [blame] | 574 | /* input->keybit is initialized with incorrect button info |
| 575 | * for Magic Trackpad. There really is only one physical |
| 576 | * button (BTN_LEFT == BTN_MOUSE). Make sure we don't |
| 577 | * advertise buttons that don't exist... |
| 578 | */ |
| 579 | __clear_bit(BTN_RIGHT, input->keybit); |
| 580 | __clear_bit(BTN_MIDDLE, input->keybit); |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 581 | __set_bit(BTN_MOUSE, input->keybit); |
Daniel Stone | 53145c2 | 2017-06-15 13:35:50 +0100 | [diff] [blame] | 582 | __set_bit(BTN_TOOL_FINGER, input->keybit); |
| 583 | __set_bit(BTN_TOOL_DOUBLETAP, input->keybit); |
| 584 | __set_bit(BTN_TOOL_TRIPLETAP, input->keybit); |
| 585 | __set_bit(BTN_TOOL_QUADTAP, input->keybit); |
| 586 | __set_bit(BTN_TOOL_QUINTTAP, input->keybit); |
| 587 | __set_bit(BTN_TOUCH, input->keybit); |
| 588 | __set_bit(INPUT_PROP_POINTER, input->propbit); |
Chase Douglas | 503f7d5 | 2012-02-13 20:12:31 -0800 | [diff] [blame] | 589 | __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); |
Chase Douglas | c042668 | 2010-06-20 21:32:31 -0400 | [diff] [blame] | 590 | } |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 591 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 592 | |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 593 | __set_bit(EV_ABS, input->evbit); |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 594 | |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 595 | error = input_mt_init_slots(input, 16, mt_flags); |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 596 | if (error) |
| 597 | return error; |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 598 | input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2, |
| 599 | 4, 0); |
| 600 | input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2, |
| 601 | 4, 0); |
Chase Douglas | 4f6fdf0 | 2011-08-05 09:16:57 -0700 | [diff] [blame] | 602 | |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 603 | /* Note: Touch Y position from the device is inverted relative |
| 604 | * to how pointer motion is reported (and relative to how USB |
| 605 | * HID recommends the coordinates work). This driver keeps |
| 606 | * the origin at the same position, and just uses the additive |
| 607 | * inverse of the reported Y. |
| 608 | */ |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 609 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE || |
| 610 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 611 | input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0); |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 612 | input_set_abs_params(input, ABS_MT_POSITION_X, |
| 613 | MOUSE_MIN_X, MOUSE_MAX_X, 4, 0); |
| 614 | input_set_abs_params(input, ABS_MT_POSITION_Y, |
| 615 | MOUSE_MIN_Y, MOUSE_MAX_Y, 4, 0); |
Chase Douglas | 4f6fdf0 | 2011-08-05 09:16:57 -0700 | [diff] [blame] | 616 | |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 617 | input_abs_set_res(input, ABS_MT_POSITION_X, |
| 618 | MOUSE_RES_X); |
| 619 | input_abs_set_res(input, ABS_MT_POSITION_Y, |
| 620 | MOUSE_RES_Y); |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 621 | } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { |
| 622 | input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0); |
| 623 | input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0); |
| 624 | input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0); |
| 625 | input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X, |
| 626 | TRACKPAD2_MAX_X, 0, 0); |
| 627 | input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y, |
| 628 | TRACKPAD2_MAX_Y, 0, 0); |
| 629 | input_set_abs_params(input, ABS_MT_POSITION_X, |
| 630 | TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0); |
| 631 | input_set_abs_params(input, ABS_MT_POSITION_Y, |
| 632 | TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0); |
| 633 | |
| 634 | input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X); |
| 635 | input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y); |
| 636 | input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X); |
| 637 | input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y); |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 638 | } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */ |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 639 | input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0); |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 640 | input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X, |
| 641 | TRACKPAD_MAX_X, 4, 0); |
| 642 | input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y, |
| 643 | TRACKPAD_MAX_Y, 4, 0); |
| 644 | input_set_abs_params(input, ABS_MT_POSITION_X, |
| 645 | TRACKPAD_MIN_X, TRACKPAD_MAX_X, 4, 0); |
| 646 | input_set_abs_params(input, ABS_MT_POSITION_Y, |
| 647 | TRACKPAD_MIN_Y, TRACKPAD_MAX_Y, 4, 0); |
Chase Douglas | cc5e0f0 | 2011-04-01 17:03:39 -0400 | [diff] [blame] | 648 | |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 649 | input_abs_set_res(input, ABS_X, TRACKPAD_RES_X); |
| 650 | input_abs_set_res(input, ABS_Y, TRACKPAD_RES_Y); |
| 651 | input_abs_set_res(input, ABS_MT_POSITION_X, |
| 652 | TRACKPAD_RES_X); |
| 653 | input_abs_set_res(input, ABS_MT_POSITION_Y, |
| 654 | TRACKPAD_RES_Y); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 655 | } |
| 656 | |
Yufeng Shen | 6264307 | 2012-07-04 12:14:43 -0400 | [diff] [blame] | 657 | input_set_events_per_packet(input, 60); |
| 658 | |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 659 | if (report_undeciphered && |
| 660 | input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 661 | __set_bit(EV_MSC, input->evbit); |
| 662 | __set_bit(MSC_RAW, input->mscbit); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 663 | } |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 664 | |
Dmitry Torokhov | 6363d20 | 2020-05-24 16:51:34 -0700 | [diff] [blame] | 665 | /* |
| 666 | * hid-input may mark device as using autorepeat, but neither |
| 667 | * the trackpad, nor the mouse actually want it. |
| 668 | */ |
| 669 | __clear_bit(EV_REP, input->evbit); |
| 670 | |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 671 | return 0; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 672 | } |
| 673 | |
Michael Poole | 64eb105 | 2010-09-24 13:58:18 +0200 | [diff] [blame] | 674 | static int magicmouse_input_mapping(struct hid_device *hdev, |
| 675 | struct hid_input *hi, struct hid_field *field, |
| 676 | struct hid_usage *usage, unsigned long **bit, int *max) |
| 677 | { |
| 678 | struct magicmouse_sc *msc = hid_get_drvdata(hdev); |
| 679 | |
| 680 | if (!msc->input) |
| 681 | msc->input = hi->input; |
| 682 | |
Chase Douglas | 6a66bbd | 2010-12-08 15:08:04 -0800 | [diff] [blame] | 683 | /* Magic Trackpad does not give relative data after switching to MT */ |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 684 | if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD || |
| 685 | hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) && |
Chase Douglas | 6a66bbd | 2010-12-08 15:08:04 -0800 | [diff] [blame] | 686 | field->flags & HID_MAIN_ITEM_RELATIVE) |
| 687 | return -1; |
| 688 | |
Michael Poole | 64eb105 | 2010-09-24 13:58:18 +0200 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
Dmitry Torokhov | 9154301 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 692 | static int magicmouse_input_configured(struct hid_device *hdev, |
Benjamin Tissoires | f1a9a14 | 2013-04-02 11:11:52 +0200 | [diff] [blame] | 693 | struct hid_input *hi) |
| 694 | |
| 695 | { |
| 696 | struct magicmouse_sc *msc = hid_get_drvdata(hdev); |
Dmitry Torokhov | 9154301 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 697 | int ret; |
Benjamin Tissoires | f1a9a14 | 2013-04-02 11:11:52 +0200 | [diff] [blame] | 698 | |
Dmitry Torokhov | 9154301 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 699 | ret = magicmouse_setup_input(msc->input, hdev); |
Benjamin Tissoires | f1a9a14 | 2013-04-02 11:11:52 +0200 | [diff] [blame] | 700 | if (ret) { |
| 701 | hid_err(hdev, "magicmouse setup input failed (%d)\n", ret); |
| 702 | /* clean msc->input to notify probe() of the failure */ |
| 703 | msc->input = NULL; |
Dmitry Torokhov | 9154301 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 704 | return ret; |
Benjamin Tissoires | f1a9a14 | 2013-04-02 11:11:52 +0200 | [diff] [blame] | 705 | } |
Dmitry Torokhov | 9154301 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 706 | |
| 707 | return 0; |
Benjamin Tissoires | f1a9a14 | 2013-04-02 11:11:52 +0200 | [diff] [blame] | 708 | } |
| 709 | |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 710 | static int magicmouse_enable_multitouch(struct hid_device *hdev) |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 711 | { |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 712 | const u8 *feature; |
| 713 | const u8 feature_mt[] = { 0xD7, 0x01 }; |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 714 | const u8 feature_mt_mouse2[] = { 0xF1, 0x02, 0x01 }; |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 715 | const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 }; |
| 716 | const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 }; |
Benjamin Tissoires | b7a87ad | 2016-11-21 11:48:41 +0100 | [diff] [blame] | 717 | u8 *buf; |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 718 | int ret; |
| 719 | int feature_size; |
| 720 | |
| 721 | if (hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { |
| 722 | if (hdev->vendor == BT_VENDOR_ID_APPLE) { |
| 723 | feature_size = sizeof(feature_mt_trackpad2_bt); |
| 724 | feature = feature_mt_trackpad2_bt; |
| 725 | } else { /* USB_VENDOR_ID_APPLE */ |
| 726 | feature_size = sizeof(feature_mt_trackpad2_usb); |
| 727 | feature = feature_mt_trackpad2_usb; |
| 728 | } |
| 729 | } else if (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { |
| 730 | feature_size = sizeof(feature_mt_mouse2); |
| 731 | feature = feature_mt_mouse2; |
| 732 | } else { |
| 733 | feature_size = sizeof(feature_mt); |
| 734 | feature = feature_mt; |
| 735 | } |
| 736 | |
| 737 | buf = kmemdup(feature, feature_size, GFP_KERNEL); |
| 738 | if (!buf) |
| 739 | return -ENOMEM; |
| 740 | |
| 741 | ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size, |
| 742 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT); |
| 743 | kfree(buf); |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | static void magicmouse_enable_mt_work(struct work_struct *work) |
| 748 | { |
| 749 | struct magicmouse_sc *msc = |
| 750 | container_of(work, struct magicmouse_sc, work.work); |
| 751 | int ret; |
| 752 | |
| 753 | ret = magicmouse_enable_multitouch(msc->hdev); |
| 754 | if (ret < 0) |
| 755 | hid_err(msc->hdev, "unable to request touch data (%d)\n", ret); |
| 756 | } |
| 757 | |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 758 | static int magicmouse_fetch_battery(struct hid_device *hdev) |
| 759 | { |
| 760 | #ifdef CONFIG_HID_BATTERY_STRENGTH |
| 761 | struct hid_report_enum *report_enum; |
| 762 | struct hid_report *report; |
| 763 | |
| 764 | if (!hdev->battery || hdev->vendor != USB_VENDOR_ID_APPLE || |
| 765 | (hdev->product != USB_DEVICE_ID_APPLE_MAGICMOUSE2 && |
| 766 | hdev->product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) |
| 767 | return -1; |
| 768 | |
| 769 | report_enum = &hdev->report_enum[hdev->battery_report_type]; |
| 770 | report = report_enum->report_id_hash[hdev->battery_report_id]; |
| 771 | |
| 772 | if (!report || report->maxfield < 1) |
| 773 | return -1; |
| 774 | |
| 775 | if (hdev->battery_capacity == hdev->battery_max) |
| 776 | return -1; |
| 777 | |
| 778 | hid_hw_request(hdev, report, HID_REQ_GET_REPORT); |
| 779 | return 0; |
| 780 | #else |
| 781 | return -1; |
| 782 | #endif |
| 783 | } |
| 784 | |
| 785 | static void magicmouse_battery_timer_tick(struct timer_list *t) |
| 786 | { |
| 787 | struct magicmouse_sc *msc = from_timer(msc, t, battery_timer); |
| 788 | struct hid_device *hdev = msc->hdev; |
| 789 | |
| 790 | if (magicmouse_fetch_battery(hdev) == 0) { |
| 791 | mod_timer(&msc->battery_timer, |
| 792 | jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS)); |
| 793 | } |
| 794 | } |
| 795 | |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 796 | static int magicmouse_probe(struct hid_device *hdev, |
| 797 | const struct hid_device_id *id) |
| 798 | { |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 799 | struct magicmouse_sc *msc; |
| 800 | struct hid_report *report; |
| 801 | int ret; |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 802 | |
Benjamin Tissoires | abf832b | 2013-07-24 19:38:04 +0200 | [diff] [blame] | 803 | msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 804 | if (msc == NULL) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 805 | hid_err(hdev, "can't alloc magicmouse descriptor\n"); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 806 | return -ENOMEM; |
| 807 | } |
| 808 | |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 809 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT; |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 810 | msc->hdev = hdev; |
| 811 | INIT_DEFERRABLE_WORK(&msc->work, magicmouse_enable_mt_work); |
Chase Douglas | 0b778e7 | 2010-06-20 21:32:30 -0400 | [diff] [blame] | 812 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 813 | msc->quirks = id->driver_data; |
| 814 | hid_set_drvdata(hdev, msc); |
| 815 | |
| 816 | ret = hid_parse(hdev); |
| 817 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 818 | hid_err(hdev, "magicmouse hid parse failed\n"); |
Benjamin Tissoires | abf832b | 2013-07-24 19:38:04 +0200 | [diff] [blame] | 819 | return ret; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 820 | } |
| 821 | |
Jiri Kosina | 23d0211 | 2010-05-12 16:01:26 +0200 | [diff] [blame] | 822 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 823 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 824 | hid_err(hdev, "magicmouse hw start failed\n"); |
Benjamin Tissoires | abf832b | 2013-07-24 19:38:04 +0200 | [diff] [blame] | 825 | return ret; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 826 | } |
| 827 | |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 828 | timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0); |
| 829 | mod_timer(&msc->battery_timer, |
| 830 | jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS)); |
| 831 | magicmouse_fetch_battery(hdev); |
| 832 | |
| 833 | if (id->vendor == USB_VENDOR_ID_APPLE && |
| 834 | (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 || |
| 835 | (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 && hdev->type != HID_TYPE_USBMOUSE))) |
| 836 | return 0; |
| 837 | |
Benjamin Tissoires | f1a9a14 | 2013-04-02 11:11:52 +0200 | [diff] [blame] | 838 | if (!msc->input) { |
| 839 | hid_err(hdev, "magicmouse input not registered\n"); |
| 840 | ret = -ENOMEM; |
| 841 | goto err_stop_hw; |
Yufeng Shen | a6d1bc1 | 2012-07-04 12:15:43 -0400 | [diff] [blame] | 842 | } |
Jiri Kosina | 23d0211 | 2010-05-12 16:01:26 +0200 | [diff] [blame] | 843 | |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 844 | if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE) |
| 845 | report = hid_register_report(hdev, HID_INPUT_REPORT, |
Benjamin Tissoires | f07b3c1 | 2018-04-24 10:04:33 +0200 | [diff] [blame] | 846 | MOUSE_REPORT_ID, 0); |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 847 | else if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) |
| 848 | report = hid_register_report(hdev, HID_INPUT_REPORT, |
| 849 | MOUSE2_REPORT_ID, 0); |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 850 | else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { |
| 851 | if (id->vendor == BT_VENDOR_ID_APPLE) |
| 852 | report = hid_register_report(hdev, HID_INPUT_REPORT, |
| 853 | TRACKPAD2_BT_REPORT_ID, 0); |
| 854 | else /* USB_VENDOR_ID_APPLE */ |
| 855 | report = hid_register_report(hdev, HID_INPUT_REPORT, |
| 856 | TRACKPAD2_USB_REPORT_ID, 0); |
| 857 | } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */ |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 858 | report = hid_register_report(hdev, HID_INPUT_REPORT, |
Benjamin Tissoires | f07b3c1 | 2018-04-24 10:04:33 +0200 | [diff] [blame] | 859 | TRACKPAD_REPORT_ID, 0); |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 860 | report = hid_register_report(hdev, HID_INPUT_REPORT, |
Benjamin Tissoires | f07b3c1 | 2018-04-24 10:04:33 +0200 | [diff] [blame] | 861 | DOUBLE_REPORT_ID, 0); |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 862 | } |
| 863 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 864 | if (!report) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 865 | hid_err(hdev, "unable to register touch report\n"); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 866 | ret = -ENOMEM; |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 867 | goto err_stop_hw; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 868 | } |
| 869 | report->size = 6; |
| 870 | |
Jiri Kosina | 35d851d | 2011-08-25 14:21:37 +0200 | [diff] [blame] | 871 | /* |
| 872 | * Some devices repond with 'invalid report id' when feature |
| 873 | * report switching it into multitouch mode is sent to it. |
| 874 | * |
| 875 | * This results in -EIO from the _raw low-level transport callback, |
| 876 | * but there seems to be no other way of switching the mode. |
| 877 | * Thus the super-ugly hacky success check below. |
| 878 | */ |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 879 | ret = magicmouse_enable_multitouch(hdev); |
| 880 | if (ret != -EIO && ret < 0) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 881 | hid_err(hdev, "unable to request touch data (%d)\n", ret); |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 882 | goto err_stop_hw; |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 883 | } |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 884 | if (ret == -EIO && id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { |
| 885 | schedule_delayed_work(&msc->work, msecs_to_jiffies(500)); |
| 886 | } |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 887 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 888 | return 0; |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 889 | err_stop_hw: |
Christophe JAILLET | 33812fc | 2021-12-28 22:09:17 +0100 | [diff] [blame] | 890 | del_timer_sync(&msc->battery_timer); |
Michael Poole | 71b38bd | 2010-02-11 00:32:57 -0500 | [diff] [blame] | 891 | hid_hw_stop(hdev); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 892 | return ret; |
| 893 | } |
| 894 | |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 895 | static void magicmouse_remove(struct hid_device *hdev) |
| 896 | { |
| 897 | struct magicmouse_sc *msc = hid_get_drvdata(hdev); |
José Expósito | 4fb1251 | 2021-05-10 08:22:37 +0200 | [diff] [blame] | 898 | |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 899 | if (msc) { |
José Expósito | 4fb1251 | 2021-05-10 08:22:37 +0200 | [diff] [blame] | 900 | cancel_delayed_work_sync(&msc->work); |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 901 | del_timer_sync(&msc->battery_timer); |
| 902 | } |
José Expósito | 4fb1251 | 2021-05-10 08:22:37 +0200 | [diff] [blame] | 903 | |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 904 | hid_hw_stop(hdev); |
| 905 | } |
| 906 | |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 907 | static __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
| 908 | unsigned int *rsize) |
| 909 | { |
| 910 | /* |
| 911 | * Change the usage from: |
| 912 | * 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 0 |
| 913 | * 0x09, 0x0b, // Usage (Vendor Usage 0x0b) 3 |
| 914 | * To: |
| 915 | * 0x05, 0x01, // Usage Page (Generic Desktop) 0 |
| 916 | * 0x09, 0x02, // Usage (Mouse) 2 |
| 917 | */ |
| 918 | if (hdev->vendor == USB_VENDOR_ID_APPLE && |
| 919 | (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 || |
| 920 | hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) && |
| 921 | *rsize == 83 && rdesc[46] == 0x84 && rdesc[58] == 0x85) { |
| 922 | hid_info(hdev, |
| 923 | "fixing up magicmouse battery report descriptor\n"); |
| 924 | *rsize = *rsize - 1; |
| 925 | rdesc = kmemdup(rdesc + 1, *rsize, GFP_KERNEL); |
| 926 | if (!rdesc) |
| 927 | return NULL; |
| 928 | |
| 929 | rdesc[0] = 0x05; |
| 930 | rdesc[1] = 0x01; |
| 931 | rdesc[2] = 0x09; |
| 932 | rdesc[3] = 0x02; |
| 933 | } |
| 934 | |
| 935 | return rdesc; |
| 936 | } |
| 937 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 938 | static const struct hid_device_id magic_mice[] = { |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 939 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, |
| 940 | USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 }, |
John Chen | 2b0c086 | 2021-03-30 19:33:16 +0800 | [diff] [blame] | 941 | { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, |
| 942 | USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 }, |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 943 | { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, |
| 944 | USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 }, |
Chase Douglas | a462230 | 2010-08-31 21:56:23 -0400 | [diff] [blame] | 945 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, |
| 946 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 }, |
Sean O'Brien | 9d7b186 | 2018-10-02 15:53:37 -0700 | [diff] [blame] | 947 | { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, |
| 948 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 }, |
| 949 | { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, |
| 950 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 }, |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 951 | { } |
| 952 | }; |
| 953 | MODULE_DEVICE_TABLE(hid, magic_mice); |
| 954 | |
| 955 | static struct hid_driver magicmouse_driver = { |
| 956 | .name = "magicmouse", |
| 957 | .id_table = magic_mice, |
| 958 | .probe = magicmouse_probe, |
John Chen | c0dc558 | 2021-03-30 19:33:18 +0800 | [diff] [blame] | 959 | .remove = magicmouse_remove, |
José Expósito | 0b91b4e | 2021-11-18 17:52:08 +0100 | [diff] [blame] | 960 | .report_fixup = magicmouse_report_fixup, |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 961 | .raw_event = magicmouse_raw_event, |
John Chen | 3dcc5f7 | 2021-03-30 19:33:17 +0800 | [diff] [blame] | 962 | .event = magicmouse_event, |
Michael Poole | 64eb105 | 2010-09-24 13:58:18 +0200 | [diff] [blame] | 963 | .input_mapping = magicmouse_input_mapping, |
Benjamin Tissoires | f1a9a14 | 2013-04-02 11:11:52 +0200 | [diff] [blame] | 964 | .input_configured = magicmouse_input_configured, |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 965 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 966 | module_hid_driver(magicmouse_driver); |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 967 | |
Michael Poole | 128537c | 2010-02-06 12:24:36 -0500 | [diff] [blame] | 968 | MODULE_LICENSE("GPL"); |