hid-magicmouse.c 18 KB

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