hid-magicmouse.c 13 KB

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