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