hid-magicmouse.c 29 KB

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