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