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