hid-magicmouse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Apple "Magic" Wireless Mouse driver
  3. *
  4. * Copyright (c) 2010 Michael Poole <mdpoole@troilus.org>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/hid.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include "hid-ids.h"
  17. static bool emulate_3button = true;
  18. module_param(emulate_3button, bool, 0644);
  19. MODULE_PARM_DESC(emulate_3button, "Emulate a middle button");
  20. static int middle_button_start = -350;
  21. static int middle_button_stop = +350;
  22. static bool emulate_scroll_wheel = true;
  23. module_param(emulate_scroll_wheel, bool, 0644);
  24. MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
  25. static bool report_touches = true;
  26. module_param(report_touches, bool, 0644);
  27. MODULE_PARM_DESC(report_touches, "Emit touch records (otherwise, only use them for emulation)");
  28. static bool report_undeciphered;
  29. module_param(report_undeciphered, bool, 0644);
  30. MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
  31. #define TOUCH_REPORT_ID 0x29
  32. /* These definitions are not precise, but they're close enough. (Bits
  33. * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
  34. * to be some kind of bit mask -- 0x20 may be a near-field reading,
  35. * and 0x40 is actual contact, and 0x10 may be a start/stop or change
  36. * indication.)
  37. */
  38. #define TOUCH_STATE_MASK 0xf0
  39. #define TOUCH_STATE_NONE 0x00
  40. #define TOUCH_STATE_START 0x30
  41. #define TOUCH_STATE_DRAG 0x40
  42. /**
  43. * struct magicmouse_sc - Tracks Magic Mouse-specific data.
  44. * @input: Input device through which we report events.
  45. * @quirks: Currently unused.
  46. * @last_timestamp: Timestamp from most recent (18-bit) touch report
  47. * (units of milliseconds over short windows, but seems to
  48. * increase faster when there are no touches).
  49. * @delta_time: 18-bit difference between the two most recent touch
  50. * reports from the mouse.
  51. * @ntouches: Number of touches in most recent touch report.
  52. * @scroll_accel: Number of consecutive scroll motions.
  53. * @scroll_jiffies: Time of last scroll motion.
  54. * @touches: Most recent data for a touch, indexed by tracking ID.
  55. * @tracking_ids: Mapping of current touch input data to @touches.
  56. */
  57. struct magicmouse_sc {
  58. struct input_dev *input;
  59. unsigned long quirks;
  60. int last_timestamp;
  61. int delta_time;
  62. int ntouches;
  63. int scroll_accel;
  64. unsigned long scroll_jiffies;
  65. struct {
  66. short x;
  67. short y;
  68. short scroll_y;
  69. u8 size;
  70. } touches[16];
  71. int tracking_ids[16];
  72. };
  73. static int magicmouse_firm_touch(struct magicmouse_sc *msc)
  74. {
  75. int touch = -1;
  76. int ii;
  77. /* If there is only one "firm" touch, set touch to its
  78. * tracking ID.
  79. */
  80. for (ii = 0; ii < msc->ntouches; ii++) {
  81. int idx = msc->tracking_ids[ii];
  82. if (msc->touches[idx].size < 8) {
  83. /* Ignore this touch. */
  84. } else if (touch >= 0) {
  85. touch = -1;
  86. break;
  87. } else {
  88. touch = idx;
  89. }
  90. }
  91. return touch;
  92. }
  93. static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
  94. {
  95. int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
  96. test_bit(BTN_RIGHT, msc->input->key) << 1 |
  97. test_bit(BTN_MIDDLE, msc->input->key) << 2;
  98. if (emulate_3button) {
  99. int id;
  100. /* If some button was pressed before, keep it held
  101. * down. Otherwise, if there's exactly one firm
  102. * touch, use that to override the mouse's guess.
  103. */
  104. if (state == 0) {
  105. /* The button was released. */
  106. } else if (last_state != 0) {
  107. state = last_state;
  108. } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
  109. int x = msc->touches[id].x;
  110. if (x < middle_button_start)
  111. state = 1;
  112. else if (x > middle_button_stop)
  113. state = 2;
  114. else
  115. state = 4;
  116. } /* else: we keep the mouse's guess */
  117. input_report_key(msc->input, BTN_MIDDLE, state & 4);
  118. }
  119. input_report_key(msc->input, BTN_LEFT, state & 1);
  120. input_report_key(msc->input, BTN_RIGHT, state & 2);
  121. if (state != last_state)
  122. msc->scroll_accel = 0;
  123. }
  124. static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
  125. {
  126. struct input_dev *input = msc->input;
  127. __s32 x_y = tdata[0] << 8 | tdata[1] << 16 | tdata[2] << 24;
  128. int misc = tdata[5] | tdata[6] << 8;
  129. int id = (misc >> 6) & 15;
  130. int x = x_y << 12 >> 20;
  131. int y = -(x_y >> 20);
  132. /* Store tracking ID and other fields. */
  133. msc->tracking_ids[raw_id] = id;
  134. msc->touches[id].x = x;
  135. msc->touches[id].y = y;
  136. msc->touches[id].size = misc & 63;
  137. /* If requested, emulate a scroll wheel by detecting small
  138. * vertical touch motions along the middle of the mouse.
  139. */
  140. if (emulate_scroll_wheel &&
  141. middle_button_start < x && x < middle_button_stop) {
  142. static const int accel_profile[] = {
  143. 256, 228, 192, 160, 128, 96, 64, 32,
  144. };
  145. unsigned long now = jiffies;
  146. int step = msc->touches[id].scroll_y - y;
  147. /* Reset acceleration after half a second. */
  148. if (time_after(now, msc->scroll_jiffies + HZ / 2))
  149. msc->scroll_accel = 0;
  150. /* Calculate and apply the scroll motion. */
  151. switch (tdata[7] & TOUCH_STATE_MASK) {
  152. case TOUCH_STATE_START:
  153. msc->touches[id].scroll_y = y;
  154. msc->scroll_accel = min_t(int, msc->scroll_accel + 1,
  155. ARRAY_SIZE(accel_profile) - 1);
  156. break;
  157. case TOUCH_STATE_DRAG:
  158. step = step / accel_profile[msc->scroll_accel];
  159. if (step != 0) {
  160. msc->touches[id].scroll_y = y;
  161. msc->scroll_jiffies = now;
  162. input_report_rel(input, REL_WHEEL, step);
  163. }
  164. break;
  165. }
  166. }
  167. /* Generate the input events for this touch. */
  168. if (report_touches) {
  169. int orientation = (misc >> 10) - 32;
  170. input_report_abs(input, ABS_MT_TRACKING_ID, id);
  171. input_report_abs(input, ABS_MT_TOUCH_MAJOR, tdata[3]);
  172. input_report_abs(input, ABS_MT_TOUCH_MINOR, tdata[4]);
  173. input_report_abs(input, ABS_MT_ORIENTATION, orientation);
  174. input_report_abs(input, ABS_MT_POSITION_X, x);
  175. input_report_abs(input, ABS_MT_POSITION_Y, y);
  176. if (report_undeciphered)
  177. input_event(input, EV_MSC, MSC_RAW, tdata[7]);
  178. input_mt_sync(input);
  179. }
  180. }
  181. static int magicmouse_raw_event(struct hid_device *hdev,
  182. struct hid_report *report, u8 *data, int size)
  183. {
  184. struct magicmouse_sc *msc = hid_get_drvdata(hdev);
  185. struct input_dev *input = msc->input;
  186. int x, y, ts, ii, clicks;
  187. switch (data[0]) {
  188. case 0x10:
  189. if (size != 6)
  190. return 0;
  191. x = (__s16)(data[2] | data[3] << 8);
  192. y = (__s16)(data[4] | data[5] << 8);
  193. clicks = data[1];
  194. break;
  195. case TOUCH_REPORT_ID:
  196. /* Expect six bytes of prefix, and N*8 bytes of touch data. */
  197. if (size < 6 || ((size - 6) % 8) != 0)
  198. return 0;
  199. ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
  200. msc->delta_time = (ts - msc->last_timestamp) & 0x3ffff;
  201. msc->last_timestamp = ts;
  202. msc->ntouches = (size - 6) / 8;
  203. for (ii = 0; ii < msc->ntouches; ii++)
  204. magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
  205. /* When emulating three-button mode, it is important
  206. * to have the current touch information before
  207. * generating a click event.
  208. */
  209. x = (signed char)data[1];
  210. y = (signed char)data[2];
  211. clicks = data[3];
  212. break;
  213. case 0x20: /* Theoretically battery status (0-100), but I have
  214. * never seen it -- maybe it is only upon request.
  215. */
  216. case 0x60: /* Unknown, maybe laser on/off. */
  217. case 0x61: /* Laser reflection status change.
  218. * data[1]: 0 = spotted, 1 = lost
  219. */
  220. default:
  221. return 0;
  222. }
  223. magicmouse_emit_buttons(msc, clicks & 3);
  224. input_report_rel(input, REL_X, x);
  225. input_report_rel(input, REL_Y, y);
  226. input_sync(input);
  227. return 1;
  228. }
  229. static int magicmouse_input_open(struct input_dev *dev)
  230. {
  231. struct hid_device *hid = input_get_drvdata(dev);
  232. return hid->ll_driver->open(hid);
  233. }
  234. static void magicmouse_input_close(struct input_dev *dev)
  235. {
  236. struct hid_device *hid = input_get_drvdata(dev);
  237. hid->ll_driver->close(hid);
  238. }
  239. static void magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
  240. {
  241. input_set_drvdata(input, hdev);
  242. input->event = hdev->ll_driver->hidinput_input_event;
  243. input->open = magicmouse_input_open;
  244. input->close = magicmouse_input_close;
  245. input->name = hdev->name;
  246. input->phys = hdev->phys;
  247. input->uniq = hdev->uniq;
  248. input->id.bustype = hdev->bus;
  249. input->id.vendor = hdev->vendor;
  250. input->id.product = hdev->product;
  251. input->id.version = hdev->version;
  252. input->dev.parent = hdev->dev.parent;
  253. __set_bit(EV_KEY, input->evbit);
  254. __set_bit(BTN_LEFT, input->keybit);
  255. __set_bit(BTN_RIGHT, input->keybit);
  256. if (emulate_3button)
  257. __set_bit(BTN_MIDDLE, input->keybit);
  258. __set_bit(BTN_TOOL_FINGER, input->keybit);
  259. __set_bit(EV_REL, input->evbit);
  260. __set_bit(REL_X, input->relbit);
  261. __set_bit(REL_Y, input->relbit);
  262. if (emulate_scroll_wheel)
  263. __set_bit(REL_WHEEL, input->relbit);
  264. if (report_touches) {
  265. __set_bit(EV_ABS, input->evbit);
  266. input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 15, 0, 0);
  267. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 4, 0);
  268. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 4, 0);
  269. input_set_abs_params(input, ABS_MT_ORIENTATION, -32, 31, 1, 0);
  270. input_set_abs_params(input, ABS_MT_POSITION_X, -1100, 1358,
  271. 4, 0);
  272. /* Note: Touch Y position from the device is inverted relative
  273. * to how pointer motion is reported (and relative to how USB
  274. * HID recommends the coordinates work). This driver keeps
  275. * the origin at the same position, and just uses the additive
  276. * inverse of the reported Y.
  277. */
  278. input_set_abs_params(input, ABS_MT_POSITION_Y, -1589, 2047,
  279. 4, 0);
  280. }
  281. if (report_undeciphered) {
  282. __set_bit(EV_MSC, input->evbit);
  283. __set_bit(MSC_RAW, input->mscbit);
  284. }
  285. }
  286. static int magicmouse_probe(struct hid_device *hdev,
  287. const struct hid_device_id *id)
  288. {
  289. __u8 feature_1[] = { 0xd7, 0x01 };
  290. __u8 feature_2[] = { 0xf8, 0x01, 0x32 };
  291. struct input_dev *input;
  292. struct magicmouse_sc *msc;
  293. struct hid_report *report;
  294. int ret;
  295. msc = kzalloc(sizeof(*msc), GFP_KERNEL);
  296. if (msc == NULL) {
  297. dev_err(&hdev->dev, "can't alloc magicmouse descriptor\n");
  298. return -ENOMEM;
  299. }
  300. msc->quirks = id->driver_data;
  301. hid_set_drvdata(hdev, msc);
  302. ret = hid_parse(hdev);
  303. if (ret) {
  304. dev_err(&hdev->dev, "magicmouse hid parse failed\n");
  305. goto err_free;
  306. }
  307. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  308. if (ret) {
  309. dev_err(&hdev->dev, "magicmouse hw start failed\n");
  310. goto err_free;
  311. }
  312. report = hid_register_report(hdev, HID_INPUT_REPORT, TOUCH_REPORT_ID);
  313. if (!report) {
  314. dev_err(&hdev->dev, "unable to register touch report\n");
  315. ret = -ENOMEM;
  316. goto err_stop_hw;
  317. }
  318. report->size = 6;
  319. ret = hdev->hid_output_raw_report(hdev, feature_1, sizeof(feature_1),
  320. HID_FEATURE_REPORT);
  321. if (ret != sizeof(feature_1)) {
  322. dev_err(&hdev->dev, "unable to request touch data (1:%d)\n",
  323. ret);
  324. goto err_stop_hw;
  325. }
  326. ret = hdev->hid_output_raw_report(hdev, feature_2,
  327. sizeof(feature_2), HID_FEATURE_REPORT);
  328. if (ret != sizeof(feature_2)) {
  329. dev_err(&hdev->dev, "unable to request touch data (2:%d)\n",
  330. ret);
  331. goto err_stop_hw;
  332. }
  333. input = input_allocate_device();
  334. if (!input) {
  335. dev_err(&hdev->dev, "can't alloc input device\n");
  336. ret = -ENOMEM;
  337. goto err_stop_hw;
  338. }
  339. magicmouse_setup_input(input, hdev);
  340. ret = input_register_device(input);
  341. if (ret) {
  342. dev_err(&hdev->dev, "input device registration failed\n");
  343. goto err_input;
  344. }
  345. msc->input = input;
  346. return 0;
  347. err_input:
  348. input_free_device(input);
  349. err_stop_hw:
  350. hid_hw_stop(hdev);
  351. err_free:
  352. kfree(msc);
  353. return ret;
  354. }
  355. static void magicmouse_remove(struct hid_device *hdev)
  356. {
  357. hid_hw_stop(hdev);
  358. kfree(hid_get_drvdata(hdev));
  359. }
  360. static const struct hid_device_id magic_mice[] = {
  361. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE),
  362. .driver_data = 0 },
  363. { }
  364. };
  365. MODULE_DEVICE_TABLE(hid, magic_mice);
  366. static struct hid_driver magicmouse_driver = {
  367. .name = "magicmouse",
  368. .id_table = magic_mice,
  369. .probe = magicmouse_probe,
  370. .remove = magicmouse_remove,
  371. .raw_event = magicmouse_raw_event,
  372. };
  373. static int __init magicmouse_init(void)
  374. {
  375. int ret;
  376. ret = hid_register_driver(&magicmouse_driver);
  377. if (ret)
  378. printk(KERN_ERR "can't register magicmouse driver\n");
  379. return ret;
  380. }
  381. static void __exit magicmouse_exit(void)
  382. {
  383. hid_unregister_driver(&magicmouse_driver);
  384. }
  385. module_init(magicmouse_init);
  386. module_exit(magicmouse_exit);
  387. MODULE_LICENSE("GPL");