hid-magicmouse.c 18 KB

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