hid-magicmouse.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Apple "Magic" Wireless Mouse driver
  3. *
  4. * Copyright (c) 2010 Michael Poole <mdpoole@troilus.org>
  5. * Copyright (c) 2010 Chase Douglas <chase.douglas@canonical.com>
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/hid.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/usb.h>
  18. #include "hid-ids.h"
  19. static bool emulate_3button = true;
  20. module_param(emulate_3button, bool, 0644);
  21. MODULE_PARM_DESC(emulate_3button, "Emulate a middle button");
  22. static int middle_button_start = -350;
  23. static int middle_button_stop = +350;
  24. static bool emulate_scroll_wheel = true;
  25. module_param(emulate_scroll_wheel, bool, 0644);
  26. MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
  27. static unsigned int scroll_speed = 32;
  28. static int param_set_scroll_speed(const char *val, struct kernel_param *kp) {
  29. unsigned long speed;
  30. if (!val || strict_strtoul(val, 0, &speed) || speed > 63)
  31. return -EINVAL;
  32. scroll_speed = speed;
  33. return 0;
  34. }
  35. module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644);
  36. MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)");
  37. static bool scroll_acceleration = false;
  38. module_param(scroll_acceleration, bool, 0644);
  39. MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
  40. static bool report_touches = true;
  41. module_param(report_touches, bool, 0644);
  42. MODULE_PARM_DESC(report_touches, "Emit touch records (otherwise, only use them for emulation)");
  43. static bool report_undeciphered;
  44. module_param(report_undeciphered, bool, 0644);
  45. MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
  46. #define TRACKPAD_REPORT_ID 0x28
  47. #define MOUSE_REPORT_ID 0x29
  48. #define DOUBLE_REPORT_ID 0xf7
  49. /* These definitions are not precise, but they're close enough. (Bits
  50. * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
  51. * to be some kind of bit mask -- 0x20 may be a near-field reading,
  52. * and 0x40 is actual contact, and 0x10 may be a start/stop or change
  53. * indication.)
  54. */
  55. #define TOUCH_STATE_MASK 0xf0
  56. #define TOUCH_STATE_NONE 0x00
  57. #define TOUCH_STATE_START 0x30
  58. #define TOUCH_STATE_DRAG 0x40
  59. #define SCROLL_ACCEL_DEFAULT 7
  60. /* Single touch emulation should only begin when no touches are currently down.
  61. * This is true when single_touch_id is equal to NO_TOUCHES. If multiple touches
  62. * are down and the touch providing for single touch emulation is lifted,
  63. * single_touch_id is equal to SINGLE_TOUCH_UP. While single touch emulation is
  64. * occuring, single_touch_id corresponds with the tracking id of the touch used.
  65. */
  66. #define NO_TOUCHES -1
  67. #define SINGLE_TOUCH_UP -2
  68. /**
  69. * struct magicmouse_sc - Tracks Magic Mouse-specific data.
  70. * @input: Input device through which we report events.
  71. * @quirks: Currently unused.
  72. * @ntouches: Number of touches in most recent touch report.
  73. * @scroll_accel: Number of consecutive scroll motions.
  74. * @scroll_jiffies: Time of last scroll motion.
  75. * @touches: Most recent data for a touch, indexed by tracking ID.
  76. * @tracking_ids: Mapping of current touch input data to @touches.
  77. */
  78. struct magicmouse_sc {
  79. struct input_dev *input;
  80. unsigned long quirks;
  81. int ntouches;
  82. int scroll_accel;
  83. unsigned long scroll_jiffies;
  84. struct {
  85. short x;
  86. short y;
  87. short scroll_x;
  88. short scroll_y;
  89. u8 size;
  90. } touches[16];
  91. int tracking_ids[16];
  92. int single_touch_id;
  93. };
  94. static int magicmouse_firm_touch(struct magicmouse_sc *msc)
  95. {
  96. int touch = -1;
  97. int ii;
  98. /* If there is only one "firm" touch, set touch to its
  99. * tracking ID.
  100. */
  101. for (ii = 0; ii < msc->ntouches; ii++) {
  102. int idx = msc->tracking_ids[ii];
  103. if (msc->touches[idx].size < 8) {
  104. /* Ignore this touch. */
  105. } else if (touch >= 0) {
  106. touch = -1;
  107. break;
  108. } else {
  109. touch = idx;
  110. }
  111. }
  112. return touch;
  113. }
  114. static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
  115. {
  116. int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
  117. test_bit(BTN_RIGHT, msc->input->key) << 1 |
  118. test_bit(BTN_MIDDLE, msc->input->key) << 2;
  119. if (emulate_3button) {
  120. int id;
  121. /* If some button was pressed before, keep it held
  122. * down. Otherwise, if there's exactly one firm
  123. * touch, use that to override the mouse's guess.
  124. */
  125. if (state == 0) {
  126. /* The button was released. */
  127. } else if (last_state != 0) {
  128. state = last_state;
  129. } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
  130. int x = msc->touches[id].x;
  131. if (x < middle_button_start)
  132. state = 1;
  133. else if (x > middle_button_stop)
  134. state = 2;
  135. else
  136. state = 4;
  137. } /* else: we keep the mouse's guess */
  138. input_report_key(msc->input, BTN_MIDDLE, state & 4);
  139. }
  140. input_report_key(msc->input, BTN_LEFT, state & 1);
  141. input_report_key(msc->input, BTN_RIGHT, state & 2);
  142. if (state != last_state)
  143. msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
  144. }
  145. static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
  146. {
  147. struct input_dev *input = msc->input;
  148. int id, x, y, size, orientation, touch_major, touch_minor, state, down;
  149. if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
  150. id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
  151. x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
  152. y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
  153. size = tdata[5] & 0x3f;
  154. orientation = (tdata[6] >> 2) - 32;
  155. touch_major = tdata[3];
  156. touch_minor = tdata[4];
  157. state = tdata[7] & TOUCH_STATE_MASK;
  158. down = state != TOUCH_STATE_NONE;
  159. } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  160. id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
  161. x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
  162. y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
  163. size = tdata[6] & 0x3f;
  164. orientation = (tdata[7] >> 2) - 32;
  165. touch_major = tdata[4];
  166. touch_minor = tdata[5];
  167. state = tdata[8] & TOUCH_STATE_MASK;
  168. down = state != TOUCH_STATE_NONE;
  169. }
  170. /* Store tracking ID and other fields. */
  171. msc->tracking_ids[raw_id] = id;
  172. msc->touches[id].x = x;
  173. msc->touches[id].y = y;
  174. msc->touches[id].size = size;
  175. /* If requested, emulate a scroll wheel by detecting small
  176. * vertical touch motions.
  177. */
  178. if (emulate_scroll_wheel) {
  179. unsigned long now = jiffies;
  180. int step_x = msc->touches[id].scroll_x - x;
  181. int step_y = msc->touches[id].scroll_y - y;
  182. /* Calculate and apply the scroll motion. */
  183. switch (state) {
  184. case TOUCH_STATE_START:
  185. msc->touches[id].scroll_x = x;
  186. msc->touches[id].scroll_y = y;
  187. /* Reset acceleration after half a second. */
  188. if (scroll_acceleration && time_before(now,
  189. msc->scroll_jiffies + HZ / 2))
  190. msc->scroll_accel = max_t(int,
  191. msc->scroll_accel - 1, 1);
  192. else
  193. msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
  194. break;
  195. case TOUCH_STATE_DRAG:
  196. step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
  197. if (step_x != 0) {
  198. msc->touches[id].scroll_x -= step_x *
  199. (64 - scroll_speed) * msc->scroll_accel;
  200. msc->scroll_jiffies = now;
  201. input_report_rel(input, REL_HWHEEL, -step_x);
  202. }
  203. step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
  204. if (step_y != 0) {
  205. msc->touches[id].scroll_y -= step_y *
  206. (64 - scroll_speed) * msc->scroll_accel;
  207. msc->scroll_jiffies = now;
  208. input_report_rel(input, REL_WHEEL, step_y);
  209. }
  210. break;
  211. }
  212. }
  213. if (down) {
  214. msc->ntouches++;
  215. if (msc->single_touch_id == NO_TOUCHES)
  216. msc->single_touch_id = id;
  217. } else if (msc->single_touch_id == id)
  218. msc->single_touch_id = SINGLE_TOUCH_UP;
  219. /* Generate the input events for this touch. */
  220. if (report_touches && down) {
  221. input_report_abs(input, ABS_MT_TRACKING_ID, id);
  222. input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major << 2);
  223. input_report_abs(input, ABS_MT_TOUCH_MINOR, touch_minor << 2);
  224. input_report_abs(input, ABS_MT_ORIENTATION, orientation);
  225. input_report_abs(input, ABS_MT_POSITION_X, x);
  226. input_report_abs(input, ABS_MT_POSITION_Y, y);
  227. if (report_undeciphered) {
  228. if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
  229. input_event(input, EV_MSC, MSC_RAW, tdata[7]);
  230. else /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  231. input_event(input, EV_MSC, MSC_RAW, tdata[8]);
  232. }
  233. input_mt_sync(input);
  234. }
  235. }
  236. static int magicmouse_raw_event(struct hid_device *hdev,
  237. struct hid_report *report, u8 *data, int size)
  238. {
  239. struct magicmouse_sc *msc = hid_get_drvdata(hdev);
  240. struct input_dev *input = msc->input;
  241. int x = 0, y = 0, ii, clicks = 0, npoints;
  242. switch (data[0]) {
  243. case TRACKPAD_REPORT_ID:
  244. /* Expect four bytes of prefix, and N*9 bytes of touch data. */
  245. if (size < 4 || ((size - 4) % 9) != 0)
  246. return 0;
  247. npoints = (size - 4) / 9;
  248. msc->ntouches = 0;
  249. for (ii = 0; ii < npoints; ii++)
  250. magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
  251. /* We don't need an MT sync here because trackpad emits a
  252. * BTN_TOUCH event in a new frame when all touches are released.
  253. */
  254. if (msc->ntouches == 0)
  255. msc->single_touch_id = NO_TOUCHES;
  256. clicks = data[1];
  257. /* The following bits provide a device specific timestamp. They
  258. * are unused here.
  259. *
  260. * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
  261. */
  262. break;
  263. case MOUSE_REPORT_ID:
  264. /* Expect six bytes of prefix, and N*8 bytes of touch data. */
  265. if (size < 6 || ((size - 6) % 8) != 0)
  266. return 0;
  267. npoints = (size - 6) / 8;
  268. msc->ntouches = 0;
  269. for (ii = 0; ii < npoints; ii++)
  270. magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
  271. if (report_touches && msc->ntouches == 0)
  272. input_mt_sync(input);
  273. /* When emulating three-button mode, it is important
  274. * to have the current touch information before
  275. * generating a click event.
  276. */
  277. x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
  278. y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
  279. clicks = data[3];
  280. /* The following bits provide a device specific timestamp. They
  281. * are unused here.
  282. *
  283. * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
  284. */
  285. break;
  286. case DOUBLE_REPORT_ID:
  287. /* Sometimes the trackpad sends two touch reports in one
  288. * packet.
  289. */
  290. magicmouse_raw_event(hdev, report, data + 2, data[1]);
  291. magicmouse_raw_event(hdev, report, data + 2 + data[1],
  292. size - 2 - data[1]);
  293. break;
  294. default:
  295. return 0;
  296. }
  297. if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
  298. magicmouse_emit_buttons(msc, clicks & 3);
  299. input_report_rel(input, REL_X, x);
  300. input_report_rel(input, REL_Y, y);
  301. } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  302. input_report_key(input, BTN_MOUSE, clicks & 1);
  303. input_report_key(input, BTN_TOUCH, msc->ntouches > 0);
  304. input_report_key(input, BTN_TOOL_FINGER, msc->ntouches == 1);
  305. input_report_key(input, BTN_TOOL_DOUBLETAP, msc->ntouches == 2);
  306. input_report_key(input, BTN_TOOL_TRIPLETAP, msc->ntouches == 3);
  307. input_report_key(input, BTN_TOOL_QUADTAP, msc->ntouches == 4);
  308. if (msc->single_touch_id >= 0) {
  309. input_report_abs(input, ABS_X,
  310. msc->touches[msc->single_touch_id].x);
  311. input_report_abs(input, ABS_Y,
  312. msc->touches[msc->single_touch_id].y);
  313. }
  314. }
  315. input_sync(input);
  316. return 1;
  317. }
  318. static void magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
  319. {
  320. __set_bit(EV_KEY, input->evbit);
  321. if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
  322. __set_bit(BTN_LEFT, input->keybit);
  323. __set_bit(BTN_RIGHT, input->keybit);
  324. if (emulate_3button)
  325. __set_bit(BTN_MIDDLE, input->keybit);
  326. __set_bit(EV_REL, input->evbit);
  327. __set_bit(REL_X, input->relbit);
  328. __set_bit(REL_Y, input->relbit);
  329. if (emulate_scroll_wheel) {
  330. __set_bit(REL_WHEEL, input->relbit);
  331. __set_bit(REL_HWHEEL, input->relbit);
  332. }
  333. } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  334. __set_bit(BTN_MOUSE, input->keybit);
  335. __set_bit(BTN_TOOL_FINGER, input->keybit);
  336. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  337. __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
  338. __set_bit(BTN_TOOL_QUADTAP, input->keybit);
  339. __set_bit(BTN_TOUCH, input->keybit);
  340. }
  341. if (report_touches) {
  342. __set_bit(EV_ABS, input->evbit);
  343. input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 15, 0, 0);
  344. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 4, 0);
  345. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 4, 0);
  346. input_set_abs_params(input, ABS_MT_ORIENTATION, -32, 31, 1, 0);
  347. /* Note: Touch Y position from the device is inverted relative
  348. * to how pointer motion is reported (and relative to how USB
  349. * HID recommends the coordinates work). This driver keeps
  350. * the origin at the same position, and just uses the additive
  351. * inverse of the reported Y.
  352. */
  353. if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
  354. input_set_abs_params(input, ABS_MT_POSITION_X, -1100,
  355. 1358, 4, 0);
  356. input_set_abs_params(input, ABS_MT_POSITION_Y, -1589,
  357. 2047, 4, 0);
  358. } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  359. input_set_abs_params(input, ABS_X, -2909, 3167, 4, 0);
  360. input_set_abs_params(input, ABS_Y, -2456, 2565, 4, 0);
  361. input_set_abs_params(input, ABS_MT_POSITION_X, -2909,
  362. 3167, 4, 0);
  363. input_set_abs_params(input, ABS_MT_POSITION_Y, -2456,
  364. 2565, 4, 0);
  365. }
  366. }
  367. if (report_undeciphered) {
  368. __set_bit(EV_MSC, input->evbit);
  369. __set_bit(MSC_RAW, input->mscbit);
  370. }
  371. }
  372. static int magicmouse_input_mapping(struct hid_device *hdev,
  373. struct hid_input *hi, struct hid_field *field,
  374. struct hid_usage *usage, unsigned long **bit, int *max)
  375. {
  376. struct magicmouse_sc *msc = hid_get_drvdata(hdev);
  377. if (!msc->input)
  378. msc->input = hi->input;
  379. return 0;
  380. }
  381. static int magicmouse_probe(struct hid_device *hdev,
  382. const struct hid_device_id *id)
  383. {
  384. __u8 feature[] = { 0xd7, 0x01 };
  385. struct magicmouse_sc *msc;
  386. struct hid_report *report;
  387. int ret;
  388. msc = kzalloc(sizeof(*msc), GFP_KERNEL);
  389. if (msc == NULL) {
  390. dev_err(&hdev->dev, "can't alloc magicmouse descriptor\n");
  391. return -ENOMEM;
  392. }
  393. msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
  394. msc->quirks = id->driver_data;
  395. hid_set_drvdata(hdev, msc);
  396. msc->single_touch_id = NO_TOUCHES;
  397. ret = hid_parse(hdev);
  398. if (ret) {
  399. dev_err(&hdev->dev, "magicmouse hid parse failed\n");
  400. goto err_free;
  401. }
  402. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  403. if (ret) {
  404. dev_err(&hdev->dev, "magicmouse hw start failed\n");
  405. goto err_free;
  406. }
  407. /* We do this after hid-input is done parsing reports so that
  408. * hid-input uses the most natural button and axis IDs.
  409. */
  410. if (msc->input)
  411. magicmouse_setup_input(msc->input, hdev);
  412. if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
  413. report = hid_register_report(hdev, HID_INPUT_REPORT,
  414. MOUSE_REPORT_ID);
  415. else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  416. report = hid_register_report(hdev, HID_INPUT_REPORT,
  417. TRACKPAD_REPORT_ID);
  418. report = hid_register_report(hdev, HID_INPUT_REPORT,
  419. DOUBLE_REPORT_ID);
  420. }
  421. if (!report) {
  422. dev_err(&hdev->dev, "unable to register touch report\n");
  423. ret = -ENOMEM;
  424. goto err_stop_hw;
  425. }
  426. report->size = 6;
  427. ret = hdev->hid_output_raw_report(hdev, feature, sizeof(feature),
  428. HID_FEATURE_REPORT);
  429. if (ret != sizeof(feature)) {
  430. dev_err(&hdev->dev, "unable to request touch data (%d)\n",
  431. ret);
  432. goto err_stop_hw;
  433. }
  434. return 0;
  435. err_stop_hw:
  436. hid_hw_stop(hdev);
  437. err_free:
  438. kfree(msc);
  439. return ret;
  440. }
  441. static void magicmouse_remove(struct hid_device *hdev)
  442. {
  443. struct magicmouse_sc *msc = hid_get_drvdata(hdev);
  444. hid_hw_stop(hdev);
  445. kfree(msc);
  446. }
  447. static const struct hid_device_id magic_mice[] = {
  448. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  449. USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
  450. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  451. USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
  452. { }
  453. };
  454. MODULE_DEVICE_TABLE(hid, magic_mice);
  455. static struct hid_driver magicmouse_driver = {
  456. .name = "magicmouse",
  457. .id_table = magic_mice,
  458. .probe = magicmouse_probe,
  459. .remove = magicmouse_remove,
  460. .raw_event = magicmouse_raw_event,
  461. .input_mapping = magicmouse_input_mapping,
  462. };
  463. static int __init magicmouse_init(void)
  464. {
  465. int ret;
  466. ret = hid_register_driver(&magicmouse_driver);
  467. if (ret)
  468. printk(KERN_ERR "can't register magicmouse driver\n");
  469. return ret;
  470. }
  471. static void __exit magicmouse_exit(void)
  472. {
  473. hid_unregister_driver(&magicmouse_driver);
  474. }
  475. module_init(magicmouse_init);
  476. module_exit(magicmouse_exit);
  477. MODULE_LICENSE("GPL");