hid-magicmouse.c 31 KB

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