| 1 | |
| 2 | |
| 3 | * Apple "Magic" Wireless Mouse driver
|
| 4 | *
|
| 5 | * Copyright (c) 2010 Michael Poole <[email protected]>
|
| 6 | * Copyright (c) 2010 Chase Douglas <[email protected]>
|
| 7 |
|
| 8 |
|
| 9 | |
| 10 |
|
| 11 |
|
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
| 13 |
|
| 14 | #include <linux/device.h>
|
| 15 | #include <linux/hid.h>
|
| 16 | #include <linux/input/mt.h>
|
| 17 | #include <linux/module.h>
|
| 18 | #include <linux/slab.h>
|
| 19 | #include <linux/workqueue.h>
|
| 20 |
|
| 21 | #include "hid-ids.h"
|
| 22 |
|
| 23 | static bool emulate_3button = true;
|
| 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 |
|
| 30 | static bool emulate_scroll_wheel = true;
|
| 31 | module_param(emulate_scroll_wheel, bool, 0644);
|
| 32 | MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
|
| 33 |
|
| 34 | static unsigned int scroll_speed = 32;
|
| 35 | static int param_set_scroll_speed(const char *val,
|
| 36 | const struct kernel_param *kp) {
|
| 37 | unsigned long speed;
|
| 38 | if (!val || kstrtoul(val, 0, &speed) || speed > 63)
|
| 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 |
|
| 46 | static bool scroll_acceleration = false;
|
| 47 | module_param(scroll_acceleration, bool, 0644);
|
| 48 | MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
|
| 49 |
|
| 50 | static bool report_undeciphered;
|
| 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 |
|
| 54 | #define TRACKPAD2_2021_BT_VERSION 0x110
|
| 55 | #define TRACKPAD_2024_BT_VERSION 0x314
|
| 56 |
|
| 57 | #define TRACKPAD_REPORT_ID 0x28
|
| 58 | #define TRACKPAD2_USB_REPORT_ID 0x02
|
| 59 | #define TRACKPAD2_BT_REPORT_ID 0x31
|
| 60 | #define MOUSE_REPORT_ID 0x29
|
| 61 | #define MOUSE2_REPORT_ID 0x12
|
| 62 | #define DOUBLE_REPORT_ID 0xf7
|
| 63 | #define USB_BATTERY_TIMEOUT_SEC 60
|
| 64 |
|
| 65 | |
| 66 | * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
|
| 67 | * to be some kind of bit mask -- 0x20 may be a near-field reading,
|
| 68 | * and 0x40 is actual contact, and 0x10 may be a start/stop or change
|
| 69 | * indication.)
|
| 70 |
|
| 71 | #define TOUCH_STATE_MASK 0xf0
|
| 72 | #define TOUCH_STATE_NONE 0x00
|
| 73 | #define TOUCH_STATE_START 0x30
|
| 74 | #define TOUCH_STATE_DRAG 0x40
|
| 75 |
|
| 76 |
|
| 77 | #define SCROLL_HR_STEPS 10
|
| 78 | #define SCROLL_HR_MULT (120 / SCROLL_HR_STEPS)
|
| 79 | #define SCROLL_HR_THRESHOLD 90
|
| 80 | #define SCROLL_ACCEL_DEFAULT 7
|
| 81 |
|
| 82 | |
| 83 | * are in units.
|
| 84 | #define MOUSE_DIMENSION_X (float)9056
|
| 85 | #define MOUSE_MIN_X -1100
|
| 86 | #define MOUSE_MAX_X 1258
|
| 87 | #define MOUSE_RES_X ((MOUSE_MAX_X - MOUSE_MIN_X) / (MOUSE_DIMENSION_X / 100))
|
| 88 | #define MOUSE_DIMENSION_Y (float)5152
|
| 89 | #define MOUSE_MIN_Y -1589
|
| 90 | #define MOUSE_MAX_Y 2047
|
| 91 | #define MOUSE_RES_Y ((MOUSE_MAX_Y - MOUSE_MIN_Y) / (MOUSE_DIMENSION_Y / 100))
|
| 92 |
|
| 93 | #define TRACKPAD_DIMENSION_X (float)13000
|
| 94 | #define TRACKPAD_MIN_X -2909
|
| 95 | #define TRACKPAD_MAX_X 3167
|
| 96 | #define TRACKPAD_RES_X \
|
| 97 | ((TRACKPAD_MAX_X - TRACKPAD_MIN_X) / (TRACKPAD_DIMENSION_X / 100))
|
| 98 | #define TRACKPAD_DIMENSION_Y (float)11000
|
| 99 | #define TRACKPAD_MIN_Y -2456
|
| 100 | #define TRACKPAD_MAX_Y 2565
|
| 101 | #define TRACKPAD_RES_Y \
|
| 102 | ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
|
| 103 |
|
| 104 | #define TRACKPAD2_DIMENSION_X (float)16000
|
| 105 | #define TRACKPAD2_MIN_X -3678
|
| 106 | #define TRACKPAD2_MAX_X 3934
|
| 107 | #define TRACKPAD2_RES_X \
|
| 108 | ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
|
| 109 | #define TRACKPAD2_DIMENSION_Y (float)11490
|
| 110 | #define TRACKPAD2_MIN_Y -2478
|
| 111 | #define TRACKPAD2_MAX_Y 2587
|
| 112 | #define TRACKPAD2_RES_Y \
|
| 113 | ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
|
| 114 |
|
| 115 | |
| 116 | * struct magicmouse_sc - Tracks Magic Mouse-specific data.
|
| 117 | * @input: Input device through which we report events.
|
| 118 | * @quirks: Currently unused.
|
| 119 | * @ntouches: Number of touches in most recent touch report.
|
| 120 | * @scroll_accel: Number of consecutive scroll motions.
|
| 121 | * @scroll_jiffies: Time of last scroll motion.
|
| 122 | * @touches: Most recent data for a touch, indexed by tracking ID.
|
| 123 | * @tracking_ids: Mapping of current touch input data to @touches.
|
| 124 | * @hdev: Pointer to the underlying HID device.
|
| 125 | * @work: Workqueue to handle initialization retry for quirky devices.
|
| 126 | * @battery_timer: Timer for obtaining battery level information.
|
| 127 |
|
| 128 | struct magicmouse_sc {
|
| 129 | struct input_dev *input;
|
| 130 | unsigned long quirks;
|
| 131 |
|
| 132 | int ntouches;
|
| 133 | int scroll_accel;
|
| 134 | unsigned long scroll_jiffies;
|
| 135 |
|
| 136 | struct {
|
| 137 | short x;
|
| 138 | short y;
|
| 139 | short scroll_x;
|
| 140 | short scroll_y;
|
| 141 | short scroll_x_hr;
|
| 142 | short scroll_y_hr;
|
| 143 | u8 size;
|
| 144 | bool scroll_x_active;
|
| 145 | bool scroll_y_active;
|
| 146 | } touches[16];
|
| 147 | int tracking_ids[16];
|
| 148 |
|
| 149 | struct hid_device *hdev;
|
| 150 | struct delayed_work work;
|
| 151 | struct timer_list battery_timer;
|
| 152 | };
|
| 153 |
|
| 154 | static int magicmouse_firm_touch(struct magicmouse_sc *msc)
|
| 155 | {
|
| 156 | int touch = -1;
|
| 157 | int ii;
|
| 158 |
|
| 159 | |
| 160 | * tracking ID.
|
| 161 |
|
| 162 | for (ii = 0; ii < msc->ntouches; ii++) {
|
| 163 | int idx = msc->tracking_ids[ii];
|
| 164 | if (msc->touches[idx].size < 8) {
|
| 165 |
|
| 166 | } else if (touch >= 0) {
|
| 167 | touch = -1;
|
| 168 | break;
|
| 169 | } else {
|
| 170 | touch = idx;
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | return touch;
|
| 175 | }
|
| 176 |
|
| 177 | static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
|
| 178 | {
|
| 179 | int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
|
| 180 | test_bit(BTN_RIGHT, msc->input->key) << 1 |
|
| 181 | test_bit(BTN_MIDDLE, msc->input->key) << 2;
|
| 182 |
|
| 183 | if (emulate_3button) {
|
| 184 | int id;
|
| 185 |
|
| 186 | |
| 187 | * down. Otherwise, if there's exactly one firm
|
| 188 | * touch, use that to override the mouse's guess.
|
| 189 |
|
| 190 | if (state == 0) {
|
| 191 |
|
| 192 | } else if (last_state != 0) {
|
| 193 | state = last_state;
|
| 194 | } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
|
| 195 | int x = msc->touches[id].x;
|
| 196 | if (x < middle_button_start)
|
| 197 | state = 1;
|
| 198 | else if (x > middle_button_stop)
|
| 199 | state = 2;
|
| 200 | else
|
| 201 | state = 4;
|
| 202 | }
|
| 203 |
|
| 204 | input_report_key(msc->input, BTN_MIDDLE, state & 4);
|
| 205 | }
|
| 206 |
|
| 207 | input_report_key(msc->input, BTN_LEFT, state & 1);
|
| 208 | input_report_key(msc->input, BTN_RIGHT, state & 2);
|
| 209 |
|
| 210 | if (state != last_state)
|
| 211 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
|
| 212 | }
|
| 213 |
|
| 214 | static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
|
| 215 | {
|
| 216 | struct input_dev *input = msc->input;
|
| 217 | int id, x, y, size, orientation, touch_major, touch_minor, state, down;
|
| 218 | int pressure = 0;
|
| 219 |
|
| 220 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
|
| 221 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
|
| 222 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
|
| 223 | id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
|
| 224 | x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
|
| 225 | y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
|
| 226 | size = tdata[5] & 0x3f;
|
| 227 | orientation = (tdata[6] >> 2) - 32;
|
| 228 | touch_major = tdata[3];
|
| 229 | touch_minor = tdata[4];
|
| 230 | state = tdata[7] & TOUCH_STATE_MASK;
|
| 231 | down = state != TOUCH_STATE_NONE;
|
| 232 | } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
|
| 233 | input->id.product ==
|
| 234 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
|
| 235 | id = tdata[8] & 0xf;
|
| 236 | x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
|
| 237 | y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
|
| 238 | size = tdata[6];
|
| 239 | orientation = (tdata[8] >> 5) - 4;
|
| 240 | touch_major = tdata[4];
|
| 241 | touch_minor = tdata[5];
|
| 242 | pressure = tdata[7];
|
| 243 | state = tdata[3] & 0xC0;
|
| 244 | down = state == 0x80;
|
| 245 | } else {
|
| 246 | id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
|
| 247 | x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
|
| 248 | y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
|
| 249 | size = tdata[6] & 0x3f;
|
| 250 | orientation = (tdata[7] >> 2) - 32;
|
| 251 | touch_major = tdata[4];
|
| 252 | touch_minor = tdata[5];
|
| 253 | state = tdata[8] & TOUCH_STATE_MASK;
|
| 254 | down = state != TOUCH_STATE_NONE;
|
| 255 | }
|
| 256 |
|
| 257 |
|
| 258 | msc->tracking_ids[raw_id] = id;
|
| 259 | msc->touches[id].x = x;
|
| 260 | msc->touches[id].y = y;
|
| 261 | msc->touches[id].size = size;
|
| 262 |
|
| 263 | |
| 264 | * vertical touch motions.
|
| 265 |
|
| 266 | if (emulate_scroll_wheel &&
|
| 267 | input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
|
| 268 | input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
|
| 269 | unsigned long now = jiffies;
|
| 270 | int step_x = msc->touches[id].scroll_x - x;
|
| 271 | int step_y = msc->touches[id].scroll_y - y;
|
| 272 | int step_hr =
|
| 273 | max_t(int,
|
| 274 | ((64 - (int)scroll_speed) * msc->scroll_accel) /
|
| 275 | SCROLL_HR_STEPS,
|
| 276 | 1);
|
| 277 | int step_x_hr = msc->touches[id].scroll_x_hr - x;
|
| 278 | int step_y_hr = msc->touches[id].scroll_y_hr - y;
|
| 279 |
|
| 280 |
|
| 281 | switch (state) {
|
| 282 | case TOUCH_STATE_START:
|
| 283 | msc->touches[id].scroll_x = x;
|
| 284 | msc->touches[id].scroll_y = y;
|
| 285 | msc->touches[id].scroll_x_hr = x;
|
| 286 | msc->touches[id].scroll_y_hr = y;
|
| 287 | msc->touches[id].scroll_x_active = false;
|
| 288 | msc->touches[id].scroll_y_active = false;
|
| 289 |
|
| 290 |
|
| 291 | if (scroll_acceleration && time_before(now,
|
| 292 | msc->scroll_jiffies + HZ / 2))
|
| 293 | msc->scroll_accel = max_t(int,
|
| 294 | msc->scroll_accel - 1, 1);
|
| 295 | else
|
| 296 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
|
| 297 |
|
| 298 | break;
|
| 299 | case TOUCH_STATE_DRAG:
|
| 300 | step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
|
| 301 | if (step_x != 0) {
|
| 302 | msc->touches[id].scroll_x -= step_x *
|
| 303 | (64 - scroll_speed) * msc->scroll_accel;
|
| 304 | msc->scroll_jiffies = now;
|
| 305 | input_report_rel(input, REL_HWHEEL, -step_x);
|
| 306 | }
|
| 307 |
|
| 308 | step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
|
| 309 | if (step_y != 0) {
|
| 310 | msc->touches[id].scroll_y -= step_y *
|
| 311 | (64 - scroll_speed) * msc->scroll_accel;
|
| 312 | msc->scroll_jiffies = now;
|
| 313 | input_report_rel(input, REL_WHEEL, step_y);
|
| 314 | }
|
| 315 |
|
| 316 | if (!msc->touches[id].scroll_x_active &&
|
| 317 | abs(step_x_hr) > SCROLL_HR_THRESHOLD) {
|
| 318 | msc->touches[id].scroll_x_active = true;
|
| 319 | msc->touches[id].scroll_x_hr = x;
|
| 320 | step_x_hr = 0;
|
| 321 | }
|
| 322 |
|
| 323 | step_x_hr /= step_hr;
|
| 324 | if (step_x_hr != 0 &&
|
| 325 | msc->touches[id].scroll_x_active) {
|
| 326 | msc->touches[id].scroll_x_hr -= step_x_hr *
|
| 327 | step_hr;
|
| 328 | input_report_rel(input,
|
| 329 | REL_HWHEEL_HI_RES,
|
| 330 | -step_x_hr * SCROLL_HR_MULT);
|
| 331 | }
|
| 332 |
|
| 333 | if (!msc->touches[id].scroll_y_active &&
|
| 334 | abs(step_y_hr) > SCROLL_HR_THRESHOLD) {
|
| 335 | msc->touches[id].scroll_y_active = true;
|
| 336 | msc->touches[id].scroll_y_hr = y;
|
| 337 | step_y_hr = 0;
|
| 338 | }
|
| 339 |
|
| 340 | step_y_hr /= step_hr;
|
| 341 | if (step_y_hr != 0 &&
|
| 342 | msc->touches[id].scroll_y_active) {
|
| 343 | msc->touches[id].scroll_y_hr -= step_y_hr *
|
| 344 | step_hr;
|
| 345 | input_report_rel(input,
|
| 346 | REL_WHEEL_HI_RES,
|
| 347 | step_y_hr * SCROLL_HR_MULT);
|
| 348 | }
|
| 349 | break;
|
| 350 | }
|
| 351 | }
|
| 352 |
|
| 353 | if (down)
|
| 354 | msc->ntouches++;
|
| 355 |
|
| 356 | input_mt_slot(input, id);
|
| 357 | input_mt_report_slot_state(input, MT_TOOL_FINGER, down);
|
| 358 |
|
| 359 |
|
| 360 | if (down) {
|
| 361 | input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major << 2);
|
| 362 | input_report_abs(input, ABS_MT_TOUCH_MINOR, touch_minor << 2);
|
| 363 | input_report_abs(input, ABS_MT_ORIENTATION, -orientation);
|
| 364 | input_report_abs(input, ABS_MT_POSITION_X, x);
|
| 365 | input_report_abs(input, ABS_MT_POSITION_Y, y);
|
| 366 |
|
| 367 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
|
| 368 | input->id.product ==
|
| 369 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC)
|
| 370 | input_report_abs(input, ABS_MT_PRESSURE, pressure);
|
| 371 |
|
| 372 | if (report_undeciphered) {
|
| 373 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
|
| 374 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
|
| 375 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC)
|
| 376 | input_event(input, EV_MSC, MSC_RAW, tdata[7]);
|
| 377 | else if (input->id.product !=
|
| 378 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
|
| 379 | input->id.product !=
|
| 380 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC)
|
| 381 | input_event(input, EV_MSC, MSC_RAW, tdata[8]);
|
| 382 | }
|
| 383 | }
|
| 384 | }
|
| 385 |
|
| 386 | static int __magicmouse_raw_event(struct hid_device *hdev,
|
| 387 | struct hid_report *report, u8 *data, int size, bool nested)
|
| 388 | {
|
| 389 | struct magicmouse_sc *msc = hid_get_drvdata(hdev);
|
| 390 | struct input_dev *input = msc->input;
|
| 391 | int x = 0, y = 0, ii, clicks = 0, npoints;
|
| 392 |
|
| 393 |
|
| 394 | if (size < 1)
|
| 395 | return 0;
|
| 396 |
|
| 397 | switch (data[0]) {
|
| 398 | case TRACKPAD_REPORT_ID:
|
| 399 | case TRACKPAD2_BT_REPORT_ID:
|
| 400 |
|
| 401 | if (size < 4 || ((size - 4) % 9) != 0)
|
| 402 | return 0;
|
| 403 | npoints = (size - 4) / 9;
|
| 404 | if (npoints > 15) {
|
| 405 | hid_warn(hdev, "invalid size value (%d) for TRACKPAD_REPORT_ID\n",
|
| 406 | size);
|
| 407 | return 0;
|
| 408 | }
|
| 409 | msc->ntouches = 0;
|
| 410 | for (ii = 0; ii < npoints; ii++)
|
| 411 | magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
|
| 412 |
|
| 413 | clicks = data[1];
|
| 414 |
|
| 415 | |
| 416 | * are unused here.
|
| 417 | *
|
| 418 | * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
|
| 419 |
|
| 420 | break;
|
| 421 | case TRACKPAD2_USB_REPORT_ID:
|
| 422 |
|
| 423 | if (size < 12 || ((size - 12) % 9) != 0)
|
| 424 | return 0;
|
| 425 | npoints = (size - 12) / 9;
|
| 426 | if (npoints > 15) {
|
| 427 | hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
|
| 428 | size);
|
| 429 | return 0;
|
| 430 | }
|
| 431 | msc->ntouches = 0;
|
| 432 | for (ii = 0; ii < npoints; ii++)
|
| 433 | magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
|
| 434 |
|
| 435 | clicks = data[1];
|
| 436 | break;
|
| 437 | case MOUSE_REPORT_ID:
|
| 438 |
|
| 439 | if (size < 6 || ((size - 6) % 8) != 0)
|
| 440 | return 0;
|
| 441 | npoints = (size - 6) / 8;
|
| 442 | if (npoints > 15) {
|
| 443 | hid_warn(hdev, "invalid size value (%d) for MOUSE_REPORT_ID\n",
|
| 444 | size);
|
| 445 | return 0;
|
| 446 | }
|
| 447 | msc->ntouches = 0;
|
| 448 | for (ii = 0; ii < npoints; ii++)
|
| 449 | magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
|
| 450 |
|
| 451 | |
| 452 | * to have the current touch information before
|
| 453 | * generating a click event.
|
| 454 |
|
| 455 | x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
|
| 456 | y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
|
| 457 | clicks = data[3];
|
| 458 |
|
| 459 | |
| 460 | * are unused here.
|
| 461 | *
|
| 462 | * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
|
| 463 |
|
| 464 | break;
|
| 465 | case MOUSE2_REPORT_ID:
|
| 466 |
|
| 467 | if (size != 8 && (size < 14 || (size - 14) % 8 != 0))
|
| 468 | return 0;
|
| 469 | npoints = (size - 14) / 8;
|
| 470 | if (npoints > 15) {
|
| 471 | hid_warn(hdev, "invalid size value (%d) for MOUSE2_REPORT_ID\n",
|
| 472 | size);
|
| 473 | return 0;
|
| 474 | }
|
| 475 | msc->ntouches = 0;
|
| 476 | for (ii = 0; ii < npoints; ii++)
|
| 477 | magicmouse_emit_touch(msc, ii, data + ii * 8 + 14);
|
| 478 |
|
| 479 | |
| 480 | * to have the current touch information before
|
| 481 | * generating a click event.
|
| 482 |
|
| 483 | x = (int)((data[3] << 24) | (data[2] << 16)) >> 16;
|
| 484 | y = (int)((data[5] << 24) | (data[4] << 16)) >> 16;
|
| 485 | clicks = data[1];
|
| 486 |
|
| 487 | |
| 488 | * are unused here.
|
| 489 | *
|
| 490 | * ts = data[11] >> 6 | data[12] << 2 | data[13] << 10;
|
| 491 |
|
| 492 | break;
|
| 493 | case DOUBLE_REPORT_ID:
|
| 494 | |
| 495 | * packet.
|
| 496 |
|
| 497 |
|
| 498 | |
| 499 | * A double report only ever wraps two normal reports, so it is
|
| 500 | * never nested. Refuse to recurse a second time; otherwise a
|
| 501 | * malicious device could chain DOUBLE_REPORT_ID packets to drive
|
| 502 | * unbounded recursion and overflow the kernel stack.
|
| 503 |
|
| 504 | if (nested)
|
| 505 | return 0;
|
| 506 |
|
| 507 |
|
| 508 | if (size < 2)
|
| 509 | return 0;
|
| 510 |
|
| 511 | if (size < data[1] + 2) {
|
| 512 | hid_warn(hdev,
|
| 513 | "received report length (%d) was smaller than specified (%d)",
|
| 514 | size, data[1] + 2);
|
| 515 | return 0;
|
| 516 | }
|
| 517 |
|
| 518 | __magicmouse_raw_event(hdev, report, data + 2, data[1], true);
|
| 519 | __magicmouse_raw_event(hdev, report, data + 2 + data[1],
|
| 520 | size - 2 - data[1], true);
|
| 521 | return 0;
|
| 522 | default:
|
| 523 | return 0;
|
| 524 | }
|
| 525 |
|
| 526 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
|
| 527 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
|
| 528 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
|
| 529 | magicmouse_emit_buttons(msc, clicks & 3);
|
| 530 | input_report_rel(input, REL_X, x);
|
| 531 | input_report_rel(input, REL_Y, y);
|
| 532 | } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
|
| 533 | input->id.product ==
|
| 534 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
|
| 535 | input_mt_sync_frame(input);
|
| 536 | input_report_key(input, BTN_MOUSE, clicks & 1);
|
| 537 | } else {
|
| 538 | input_report_key(input, BTN_MOUSE, clicks & 1);
|
| 539 | input_mt_report_pointer_emulation(input, true);
|
| 540 | }
|
| 541 |
|
| 542 | input_sync(input);
|
| 543 | return 1;
|
| 544 | }
|
| 545 |
|
| 546 | static int magicmouse_raw_event(struct hid_device *hdev,
|
| 547 | struct hid_report *report, u8 *data, int size)
|
| 548 | {
|
| 549 | return __magicmouse_raw_event(hdev, report, data, size, false);
|
| 550 | }
|
| 551 |
|
| 552 | static int magicmouse_event(struct hid_device *hdev, struct hid_field *field,
|
| 553 | struct hid_usage *usage, __s32 value)
|
| 554 | {
|
| 555 | struct magicmouse_sc *msc = hid_get_drvdata(hdev);
|
| 556 | if ((msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
|
| 557 | msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) &&
|
| 558 | field->report->id == MOUSE2_REPORT_ID) {
|
| 559 | |
| 560 | * magic_mouse_raw_event has done all the work. Skip hidinput.
|
| 561 | *
|
| 562 | * Specifically, hidinput may modify BTN_LEFT and BTN_RIGHT,
|
| 563 | * breaking emulate_3button.
|
| 564 |
|
| 565 | return 1;
|
| 566 | }
|
| 567 | return 0;
|
| 568 | }
|
| 569 |
|
| 570 | static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
|
| 571 | {
|
| 572 | int error;
|
| 573 | int mt_flags = 0;
|
| 574 |
|
| 575 | __set_bit(EV_KEY, input->evbit);
|
| 576 |
|
| 577 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
|
| 578 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
|
| 579 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
|
| 580 | __set_bit(BTN_LEFT, input->keybit);
|
| 581 | __set_bit(BTN_RIGHT, input->keybit);
|
| 582 | if (emulate_3button)
|
| 583 | __set_bit(BTN_MIDDLE, input->keybit);
|
| 584 |
|
| 585 | __set_bit(EV_REL, input->evbit);
|
| 586 | __set_bit(REL_X, input->relbit);
|
| 587 | __set_bit(REL_Y, input->relbit);
|
| 588 | if (emulate_scroll_wheel) {
|
| 589 | __set_bit(REL_WHEEL, input->relbit);
|
| 590 | __set_bit(REL_HWHEEL, input->relbit);
|
| 591 | __set_bit(REL_WHEEL_HI_RES, input->relbit);
|
| 592 | __set_bit(REL_HWHEEL_HI_RES, input->relbit);
|
| 593 | }
|
| 594 | } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
|
| 595 | input->id.product ==
|
| 596 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
|
| 597 | |
| 598 | * automatically personalized, e.g., "José Expósito's Trackpad".
|
| 599 | * When connected through Bluetooth, the personalized name is
|
| 600 | * reported, however, when connected through USB the generic
|
| 601 | * name is reported.
|
| 602 | * Set the device name to ensure the same driver settings get
|
| 603 | * loaded, whether connected through bluetooth or USB.
|
| 604 |
|
| 605 | if (hdev->vendor == BT_VENDOR_ID_APPLE) {
|
| 606 | if (input->id.version == TRACKPAD2_2021_BT_VERSION)
|
| 607 | input->name = "Apple Inc. Magic Trackpad 2021";
|
| 608 | else if (input->id.version == TRACKPAD_2024_BT_VERSION) {
|
| 609 | input->name = "Apple Inc. Magic Trackpad USB-C";
|
| 610 | } else {
|
| 611 | input->name = "Apple Inc. Magic Trackpad";
|
| 612 | }
|
| 613 | } else {
|
| 614 | input->name = hdev->name;
|
| 615 | }
|
| 616 |
|
| 617 | __clear_bit(EV_MSC, input->evbit);
|
| 618 | __clear_bit(BTN_0, input->keybit);
|
| 619 | __clear_bit(BTN_RIGHT, input->keybit);
|
| 620 | __clear_bit(BTN_MIDDLE, input->keybit);
|
| 621 | __set_bit(BTN_MOUSE, input->keybit);
|
| 622 | __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
|
| 623 | __set_bit(BTN_TOOL_FINGER, input->keybit);
|
| 624 |
|
| 625 | mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
|
| 626 | INPUT_MT_TRACK;
|
| 627 | } else {
|
| 628 | |
| 629 | * for Magic Trackpad. There really is only one physical
|
| 630 | * button (BTN_LEFT == BTN_MOUSE). Make sure we don't
|
| 631 | * advertise buttons that don't exist...
|
| 632 |
|
| 633 | __clear_bit(BTN_RIGHT, input->keybit);
|
| 634 | __clear_bit(BTN_MIDDLE, input->keybit);
|
| 635 | __set_bit(BTN_MOUSE, input->keybit);
|
| 636 | __set_bit(BTN_TOOL_FINGER, input->keybit);
|
| 637 | __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
|
| 638 | __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
|
| 639 | __set_bit(BTN_TOOL_QUADTAP, input->keybit);
|
| 640 | __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
|
| 641 | __set_bit(BTN_TOUCH, input->keybit);
|
| 642 | __set_bit(INPUT_PROP_POINTER, input->propbit);
|
| 643 | __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
|
| 644 | }
|
| 645 |
|
| 646 |
|
| 647 | __set_bit(EV_ABS, input->evbit);
|
| 648 |
|
| 649 | error = input_mt_init_slots(input, 16, mt_flags);
|
| 650 | if (error)
|
| 651 | return error;
|
| 652 | input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
|
| 653 | 4, 0);
|
| 654 | input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
|
| 655 | 4, 0);
|
| 656 |
|
| 657 | |
| 658 | * to how pointer motion is reported (and relative to how USB
|
| 659 | * HID recommends the coordinates work). This driver keeps
|
| 660 | * the origin at the same position, and just uses the additive
|
| 661 | * inverse of the reported Y.
|
| 662 |
|
| 663 | if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
|
| 664 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
|
| 665 | input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC) {
|
| 666 | input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
|
| 667 | input_set_abs_params(input, ABS_MT_POSITION_X,
|
| 668 | MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
|
| 669 | input_set_abs_params(input, ABS_MT_POSITION_Y,
|
| 670 | MOUSE_MIN_Y, MOUSE_MAX_Y, 4, 0);
|
| 671 |
|
| 672 | input_abs_set_res(input, ABS_MT_POSITION_X,
|
| 673 | MOUSE_RES_X);
|
| 674 | input_abs_set_res(input, ABS_MT_POSITION_Y,
|
| 675 | MOUSE_RES_Y);
|
| 676 | } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
|
| 677 | input->id.product ==
|
| 678 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
|
| 679 | input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
|
| 680 | input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
|
| 681 | input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
|
| 682 | input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
|
| 683 | TRACKPAD2_MAX_X, 0, 0);
|
| 684 | input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
|
| 685 | TRACKPAD2_MAX_Y, 0, 0);
|
| 686 | input_set_abs_params(input, ABS_MT_POSITION_X,
|
| 687 | TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
|
| 688 | input_set_abs_params(input, ABS_MT_POSITION_Y,
|
| 689 | TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
|
| 690 |
|
| 691 | input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
|
| 692 | input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
|
| 693 | input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
|
| 694 | input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
|
| 695 | } else {
|
| 696 | input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
|
| 697 | input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
|
| 698 | TRACKPAD_MAX_X, 4, 0);
|
| 699 | input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
|
| 700 | TRACKPAD_MAX_Y, 4, 0);
|
| 701 | input_set_abs_params(input, ABS_MT_POSITION_X,
|
| 702 | TRACKPAD_MIN_X, TRACKPAD_MAX_X, 4, 0);
|
| 703 | input_set_abs_params(input, ABS_MT_POSITION_Y,
|
| 704 | TRACKPAD_MIN_Y, TRACKPAD_MAX_Y, 4, 0);
|
| 705 |
|
| 706 | input_abs_set_res(input, ABS_X, TRACKPAD_RES_X);
|
| 707 | input_abs_set_res(input, ABS_Y, TRACKPAD_RES_Y);
|
| 708 | input_abs_set_res(input, ABS_MT_POSITION_X,
|
| 709 | TRACKPAD_RES_X);
|
| 710 | input_abs_set_res(input, ABS_MT_POSITION_Y,
|
| 711 | TRACKPAD_RES_Y);
|
| 712 | }
|
| 713 |
|
| 714 | input_set_events_per_packet(input, 60);
|
| 715 |
|
| 716 | if (report_undeciphered &&
|
| 717 | input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
|
| 718 | input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) {
|
| 719 | __set_bit(EV_MSC, input->evbit);
|
| 720 | __set_bit(MSC_RAW, input->mscbit);
|
| 721 | }
|
| 722 |
|
| 723 | |
| 724 | * hid-input may mark device as using autorepeat, but neither
|
| 725 | * the trackpad, nor the mouse actually want it.
|
| 726 |
|
| 727 | __clear_bit(EV_REP, input->evbit);
|
| 728 |
|
| 729 | return 0;
|
| 730 | }
|
| 731 |
|
| 732 | static int magicmouse_input_mapping(struct hid_device *hdev,
|
| 733 | struct hid_input *hi, struct hid_field *field,
|
| 734 | struct hid_usage *usage, unsigned long **bit, int *max)
|
| 735 | {
|
| 736 | struct magicmouse_sc *msc = hid_get_drvdata(hdev);
|
| 737 |
|
| 738 | if (!msc->input)
|
| 739 | msc->input = hi->input;
|
| 740 |
|
| 741 |
|
| 742 | if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
|
| 743 | hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
|
| 744 | hi->input->id.product ==
|
| 745 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
|
| 746 | field->flags & HID_MAIN_ITEM_RELATIVE)
|
| 747 | return -1;
|
| 748 |
|
| 749 | return 0;
|
| 750 | }
|
| 751 |
|
| 752 | static int magicmouse_input_configured(struct hid_device *hdev,
|
| 753 | struct hid_input *hi)
|
| 754 |
|
| 755 | {
|
| 756 | struct magicmouse_sc *msc = hid_get_drvdata(hdev);
|
| 757 | int ret;
|
| 758 |
|
| 759 | if (!msc->input) {
|
| 760 | hid_err(hdev, "magicmouse setup input failed (no input)");
|
| 761 | return -EINVAL;
|
| 762 | }
|
| 763 |
|
| 764 | ret = magicmouse_setup_input(msc->input, hdev);
|
| 765 | if (ret) {
|
| 766 | hid_err(hdev, "magicmouse setup input failed (%d)\n", ret);
|
| 767 |
|
| 768 | msc->input = NULL;
|
| 769 | return ret;
|
| 770 | }
|
| 771 |
|
| 772 | return 0;
|
| 773 | }
|
| 774 |
|
| 775 | static int magicmouse_enable_multitouch(struct hid_device *hdev)
|
| 776 | {
|
| 777 | const u8 *feature;
|
| 778 | const u8 feature_mt[] = { 0xD7, 0x01 };
|
| 779 | const u8 feature_mt_mouse2[] = { 0xF1, 0x02, 0x01 };
|
| 780 | const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
|
| 781 | const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
|
| 782 | u8 *buf;
|
| 783 | int ret;
|
| 784 | int feature_size;
|
| 785 |
|
| 786 | switch (hdev->product) {
|
| 787 | case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2:
|
| 788 | case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC:
|
| 789 | switch (hdev->vendor) {
|
| 790 | case BT_VENDOR_ID_APPLE:
|
| 791 | feature_size = sizeof(feature_mt_trackpad2_bt);
|
| 792 | feature = feature_mt_trackpad2_bt;
|
| 793 | break;
|
| 794 | default:
|
| 795 | feature_size = sizeof(feature_mt_trackpad2_usb);
|
| 796 | feature = feature_mt_trackpad2_usb;
|
| 797 | }
|
| 798 | break;
|
| 799 | case USB_DEVICE_ID_APPLE_MAGICMOUSE2:
|
| 800 | case USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC:
|
| 801 | feature_size = sizeof(feature_mt_mouse2);
|
| 802 | feature = feature_mt_mouse2;
|
| 803 | break;
|
| 804 | default:
|
| 805 | feature_size = sizeof(feature_mt);
|
| 806 | feature = feature_mt;
|
| 807 | }
|
| 808 |
|
| 809 | buf = kmemdup(feature, feature_size, GFP_KERNEL);
|
| 810 | if (!buf)
|
| 811 | return -ENOMEM;
|
| 812 |
|
| 813 | ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
|
| 814 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
|
| 815 | kfree(buf);
|
| 816 | return ret;
|
| 817 | }
|
| 818 |
|
| 819 | static void magicmouse_enable_mt_work(struct work_struct *work)
|
| 820 | {
|
| 821 | struct magicmouse_sc *msc =
|
| 822 | container_of(work, struct magicmouse_sc, work.work);
|
| 823 | int ret;
|
| 824 |
|
| 825 | ret = magicmouse_enable_multitouch(msc->hdev);
|
| 826 | if (ret < 0)
|
| 827 | hid_err(msc->hdev, "unable to request touch data (%d)\n", ret);
|
| 828 | }
|
| 829 |
|
| 830 | static bool is_usb_magicmouse2(__u32 vendor, __u32 product)
|
| 831 | {
|
| 832 | if (vendor != USB_VENDOR_ID_APPLE)
|
| 833 | return false;
|
| 834 | return product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
|
| 835 | product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC;
|
| 836 | }
|
| 837 |
|
| 838 | static bool is_usb_magictrackpad2(__u32 vendor, __u32 product)
|
| 839 | {
|
| 840 | if (vendor != USB_VENDOR_ID_APPLE)
|
| 841 | return false;
|
| 842 | return product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
|
| 843 | product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC;
|
| 844 | }
|
| 845 |
|
| 846 | static bool is_bt_magictrackpad2(__u32 vendor, __u32 product)
|
| 847 | {
|
| 848 | return vendor == BT_VENDOR_ID_APPLE &&
|
| 849 | product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC;
|
| 850 | }
|
| 851 |
|
| 852 | static int magicmouse_fetch_battery(struct hid_device *hdev)
|
| 853 | {
|
| 854 | #ifdef CONFIG_HID_BATTERY_STRENGTH
|
| 855 | struct hid_report_enum *report_enum;
|
| 856 | struct hid_report *report;
|
| 857 | struct hid_battery *bat;
|
| 858 |
|
| 859 | bat = hid_get_battery(hdev);
|
| 860 | if (!bat ||
|
| 861 | (!is_usb_magicmouse2(hdev->vendor, hdev->product) &&
|
| 862 | !is_usb_magictrackpad2(hdev->vendor, hdev->product) &&
|
| 863 | !is_bt_magictrackpad2(hdev->vendor, hdev->product)))
|
| 864 | return -1;
|
| 865 |
|
| 866 | report_enum = &hdev->report_enum[bat->report_type];
|
| 867 | report = report_enum->report_id_hash[bat->report_id];
|
| 868 |
|
| 869 | if (!report || report->maxfield < 1)
|
| 870 | return -1;
|
| 871 |
|
| 872 | if (bat->capacity == bat->max)
|
| 873 | return -1;
|
| 874 |
|
| 875 | hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
|
| 876 | return 0;
|
| 877 | #else
|
| 878 | return -1;
|
| 879 | #endif
|
| 880 | }
|
| 881 |
|
| 882 | static void magicmouse_battery_timer_tick(struct timer_list *t)
|
| 883 | {
|
| 884 | struct magicmouse_sc *msc = timer_container_of(msc, t, battery_timer);
|
| 885 | struct hid_device *hdev = msc->hdev;
|
| 886 |
|
| 887 | if (magicmouse_fetch_battery(hdev) == 0) {
|
| 888 | mod_timer(&msc->battery_timer,
|
| 889 | jiffies + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC));
|
| 890 | }
|
| 891 | }
|
| 892 |
|
| 893 | static int magicmouse_probe(struct hid_device *hdev,
|
| 894 | const struct hid_device_id *id)
|
| 895 | {
|
| 896 | struct magicmouse_sc *msc;
|
| 897 | struct hid_report *report;
|
| 898 | int ret;
|
| 899 |
|
| 900 | msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
|
| 901 | if (msc == NULL) {
|
| 902 | hid_err(hdev, "can't alloc magicmouse descriptor\n");
|
| 903 | return -ENOMEM;
|
| 904 | }
|
| 905 |
|
| 906 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
|
| 907 | msc->hdev = hdev;
|
| 908 | INIT_DEFERRABLE_WORK(&msc->work, magicmouse_enable_mt_work);
|
| 909 |
|
| 910 | msc->quirks = id->driver_data;
|
| 911 | hid_set_drvdata(hdev, msc);
|
| 912 |
|
| 913 | ret = hid_parse(hdev);
|
| 914 | if (ret) {
|
| 915 | hid_err(hdev, "magicmouse hid parse failed\n");
|
| 916 | return ret;
|
| 917 | }
|
| 918 |
|
| 919 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
|
| 920 | if (ret) {
|
| 921 | hid_err(hdev, "magicmouse hw start failed\n");
|
| 922 | return ret;
|
| 923 | }
|
| 924 |
|
| 925 | |
| 926 | * When hidinput_connect() fails it frees every input device it
|
| 927 | * created, but that does not fail hid_hw_start(): the core simply
|
| 928 | * does not claim an input. msc->input, cached in ->input_mapping
|
| 929 | * while the report descriptor was parsed, would then be a dangling
|
| 930 | * pointer that passes every NULL check. Trust the core's claim.
|
| 931 |
|
| 932 | if (!(hdev->claimed & HID_CLAIMED_INPUT))
|
| 933 | msc->input = NULL;
|
| 934 |
|
| 935 | if (is_usb_magicmouse2(id->vendor, id->product) ||
|
| 936 | is_usb_magictrackpad2(id->vendor, id->product)) {
|
| 937 | timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0);
|
| 938 | mod_timer(&msc->battery_timer,
|
| 939 | jiffies + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC));
|
| 940 | magicmouse_fetch_battery(hdev);
|
| 941 | }
|
| 942 |
|
| 943 | if (is_usb_magicmouse2(id->vendor, id->product) ||
|
| 944 | (is_usb_magictrackpad2(id->vendor, id->product) &&
|
| 945 | hdev->type != HID_TYPE_USBMOUSE))
|
| 946 | return 0;
|
| 947 |
|
| 948 | if (!msc->input) {
|
| 949 | hid_err(hdev, "magicmouse input not registered\n");
|
| 950 | ret = -ENOMEM;
|
| 951 | goto err_stop_hw;
|
| 952 | }
|
| 953 |
|
| 954 | switch (id->product) {
|
| 955 | case USB_DEVICE_ID_APPLE_MAGICMOUSE:
|
| 956 | report = hid_register_report(hdev, HID_INPUT_REPORT, MOUSE_REPORT_ID, 0);
|
| 957 | break;
|
| 958 | case USB_DEVICE_ID_APPLE_MAGICMOUSE2:
|
| 959 | case USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC:
|
| 960 | report = hid_register_report(hdev, HID_INPUT_REPORT, MOUSE2_REPORT_ID, 0);
|
| 961 | break;
|
| 962 | case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2:
|
| 963 | case USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC:
|
| 964 | switch (id->vendor) {
|
| 965 | case BT_VENDOR_ID_APPLE:
|
| 966 | report = hid_register_report(hdev, HID_INPUT_REPORT,
|
| 967 | TRACKPAD2_BT_REPORT_ID, 0);
|
| 968 | break;
|
| 969 | default:
|
| 970 | report = hid_register_report(hdev, HID_INPUT_REPORT,
|
| 971 | TRACKPAD2_USB_REPORT_ID, 0);
|
| 972 | }
|
| 973 | break;
|
| 974 | default:
|
| 975 | report = hid_register_report(hdev, HID_INPUT_REPORT,
|
| 976 | TRACKPAD_REPORT_ID, 0);
|
| 977 | report = hid_register_report(hdev, HID_INPUT_REPORT,
|
| 978 | DOUBLE_REPORT_ID, 0);
|
| 979 | }
|
| 980 |
|
| 981 | if (!report) {
|
| 982 | hid_err(hdev, "unable to register touch report\n");
|
| 983 | ret = -ENOMEM;
|
| 984 | goto err_stop_hw;
|
| 985 | }
|
| 986 | report->size = 6;
|
| 987 |
|
| 988 | |
| 989 | * Some devices repond with 'invalid report id' when feature
|
| 990 | * report switching it into multitouch mode is sent to it.
|
| 991 | *
|
| 992 | * This results in -EIO from the _raw low-level transport callback,
|
| 993 | * but there seems to be no other way of switching the mode.
|
| 994 | * Thus the super-ugly hacky success check below.
|
| 995 |
|
| 996 | ret = magicmouse_enable_multitouch(hdev);
|
| 997 | if (ret != -EIO && ret < 0) {
|
| 998 | hid_err(hdev, "unable to request touch data (%d)\n", ret);
|
| 999 | goto err_stop_hw;
|
| 1000 | }
|
| 1001 | if (ret == -EIO && (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
|
| 1002 | id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC)) {
|
| 1003 | schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
|
| 1004 | }
|
| 1005 |
|
| 1006 | |
| 1007 | * Query the Bluetooth Magic Trackpad USB-C battery as done for USB.
|
| 1008 | * Start io first: probe holds driver_input_lock and the synchronous
|
| 1009 | * GET_REPORT reply would otherwise be dropped.
|
| 1010 |
|
| 1011 | if (is_bt_magictrackpad2(id->vendor, id->product)) {
|
| 1012 | hid_device_io_start(hdev);
|
| 1013 | magicmouse_fetch_battery(hdev);
|
| 1014 | }
|
| 1015 |
|
| 1016 | return 0;
|
| 1017 | err_stop_hw:
|
| 1018 | if (is_usb_magicmouse2(id->vendor, id->product) ||
|
| 1019 | is_usb_magictrackpad2(id->vendor, id->product))
|
| 1020 | timer_delete_sync(&msc->battery_timer);
|
| 1021 |
|
| 1022 | hid_hw_stop(hdev);
|
| 1023 | return ret;
|
| 1024 | }
|
| 1025 |
|
| 1026 | static void magicmouse_remove(struct hid_device *hdev)
|
| 1027 | {
|
| 1028 | struct magicmouse_sc *msc = hid_get_drvdata(hdev);
|
| 1029 |
|
| 1030 | if (msc) {
|
| 1031 | cancel_delayed_work_sync(&msc->work);
|
| 1032 | if (is_usb_magicmouse2(hdev->vendor, hdev->product) ||
|
| 1033 | is_usb_magictrackpad2(hdev->vendor, hdev->product))
|
| 1034 | timer_delete_sync(&msc->battery_timer);
|
| 1035 | }
|
| 1036 |
|
| 1037 | hid_hw_stop(hdev);
|
| 1038 | }
|
| 1039 |
|
| 1040 | #ifdef CONFIG_PM
|
| 1041 | static int magicmouse_reset_resume(struct hid_device *hdev)
|
| 1042 | {
|
| 1043 | struct magicmouse_sc *msc = hid_get_drvdata(hdev);
|
| 1044 |
|
| 1045 | |
| 1046 | * enable report. Only the HID_TYPE_USBMOUSE interface accepts it, and
|
| 1047 | * it must be deferred. Sending it inline here is too early.
|
| 1048 |
|
| 1049 | if (msc && hdev->type == HID_TYPE_USBMOUSE)
|
| 1050 | schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
|
| 1051 |
|
| 1052 | return 0;
|
| 1053 | }
|
| 1054 | #endif
|
| 1055 |
|
| 1056 | static const __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc,
|
| 1057 | unsigned int *rsize)
|
| 1058 | {
|
| 1059 | |
| 1060 | * Change the usage from:
|
| 1061 | * 0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1) 0
|
| 1062 | * 0x09, 0x0b, // Usage (Vendor Usage 0x0b) 3
|
| 1063 | * To:
|
| 1064 | * 0x05, 0x01, // Usage Page (Generic Desktop) 0
|
| 1065 | * 0x09, 0x02, // Usage (Mouse) 2
|
| 1066 |
|
| 1067 | if ((is_usb_magicmouse2(hdev->vendor, hdev->product) ||
|
| 1068 | is_usb_magictrackpad2(hdev->vendor, hdev->product)) &&
|
| 1069 | *rsize >= 83 && rdesc[46] == 0x84 && rdesc[58] == 0x85) {
|
| 1070 | hid_info(hdev,
|
| 1071 | "fixing up magicmouse battery report descriptor\n");
|
| 1072 | *rsize = *rsize - 1;
|
| 1073 | rdesc = rdesc + 1;
|
| 1074 |
|
| 1075 | rdesc[0] = 0x05;
|
| 1076 | rdesc[1] = 0x01;
|
| 1077 | rdesc[2] = 0x09;
|
| 1078 | rdesc[3] = 0x02;
|
| 1079 | }
|
| 1080 |
|
| 1081 | return rdesc;
|
| 1082 | }
|
| 1083 |
|
| 1084 | static const struct hid_device_id magic_mice[] = {
|
| 1085 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
|
| 1086 | USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
|
| 1087 | { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
|
| 1088 | USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
|
| 1089 | { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
|
| 1090 | USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
|
| 1091 | { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
|
| 1092 | USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC), .driver_data = 0 },
|
| 1093 | { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
|
| 1094 | USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC), .driver_data = 0 },
|
| 1095 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
|
| 1096 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
|
| 1097 | { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
|
| 1098 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
|
| 1099 | { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
|
| 1100 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
|
| 1101 | { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
|
| 1102 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC), .driver_data = 0 },
|
| 1103 | { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
|
| 1104 | USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC), .driver_data = 0 },
|
| 1105 | { }
|
| 1106 | };
|
| 1107 | MODULE_DEVICE_TABLE(hid, magic_mice);
|
| 1108 |
|
| 1109 | static struct hid_driver magicmouse_driver = {
|
| 1110 | .name = "magicmouse",
|
| 1111 | .id_table = magic_mice,
|
| 1112 | .probe = magicmouse_probe,
|
| 1113 | .remove = magicmouse_remove,
|
| 1114 | .report_fixup = magicmouse_report_fixup,
|
| 1115 | .raw_event = magicmouse_raw_event,
|
| 1116 | .event = magicmouse_event,
|
| 1117 | .input_mapping = magicmouse_input_mapping,
|
| 1118 | .input_configured = magicmouse_input_configured,
|
| 1119 | #ifdef CONFIG_PM
|
| 1120 | .reset_resume = magicmouse_reset_resume,
|
| 1121 | #endif
|
| 1122 | };
|
| 1123 | module_hid_driver(magicmouse_driver);
|
| 1124 |
|
| 1125 | MODULE_DESCRIPTION("Apple \"Magic\" Wireless Mouse driver");
|
| 1126 | MODULE_LICENSE("GPL");
|