hid-magicmouse.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. if (npoints > 15) {
  270. hid_warn(hdev, "invalid size value (%d) for TRACKPAD_REPORT_ID\n",
  271. size);
  272. return 0;
  273. }
  274. msc->ntouches = 0;
  275. for (ii = 0; ii < npoints; ii++)
  276. magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
  277. /* We don't need an MT sync here because trackpad emits a
  278. * BTN_TOUCH event in a new frame when all touches are released.
  279. */
  280. if (msc->ntouches == 0)
  281. msc->single_touch_id = NO_TOUCHES;
  282. clicks = data[1];
  283. /* The following bits provide a device specific timestamp. They
  284. * are unused here.
  285. *
  286. * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
  287. */
  288. break;
  289. case MOUSE_REPORT_ID:
  290. /* Expect six bytes of prefix, and N*8 bytes of touch data. */
  291. if (size < 6 || ((size - 6) % 8) != 0)
  292. return 0;
  293. npoints = (size - 6) / 8;
  294. if (npoints > 15) {
  295. hid_warn(hdev, "invalid size value (%d) for MOUSE_REPORT_ID\n",
  296. size);
  297. return 0;
  298. }
  299. msc->ntouches = 0;
  300. for (ii = 0; ii < npoints; ii++)
  301. magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
  302. if (report_touches && msc->ntouches == 0)
  303. input_mt_sync(input);
  304. /* When emulating three-button mode, it is important
  305. * to have the current touch information before
  306. * generating a click event.
  307. */
  308. x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
  309. y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
  310. clicks = data[3];
  311. /* The following bits provide a device specific timestamp. They
  312. * are unused here.
  313. *
  314. * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
  315. */
  316. break;
  317. case DOUBLE_REPORT_ID:
  318. /* Sometimes the trackpad sends two touch reports in one
  319. * packet.
  320. */
  321. magicmouse_raw_event(hdev, report, data + 2, data[1]);
  322. magicmouse_raw_event(hdev, report, data + 2 + data[1],
  323. size - 2 - data[1]);
  324. break;
  325. default:
  326. return 0;
  327. }
  328. if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
  329. magicmouse_emit_buttons(msc, clicks & 3);
  330. input_report_rel(input, REL_X, x);
  331. input_report_rel(input, REL_Y, y);
  332. } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  333. input_report_key(input, BTN_MOUSE, clicks & 1);
  334. input_report_key(input, BTN_TOUCH, msc->ntouches > 0);
  335. input_report_key(input, BTN_TOOL_FINGER, msc->ntouches == 1);
  336. input_report_key(input, BTN_TOOL_DOUBLETAP, msc->ntouches == 2);
  337. input_report_key(input, BTN_TOOL_TRIPLETAP, msc->ntouches == 3);
  338. input_report_key(input, BTN_TOOL_QUADTAP, msc->ntouches == 4);
  339. if (msc->single_touch_id >= 0) {
  340. input_report_abs(input, ABS_X,
  341. msc->touches[msc->single_touch_id].x);
  342. input_report_abs(input, ABS_Y,
  343. msc->touches[msc->single_touch_id].y);
  344. }
  345. }
  346. input_sync(input);
  347. return 1;
  348. }
  349. static int magicmouse_setup_input(struct hid_device *hdev, struct hid_input *hi)
  350. {
  351. struct input_dev *input = hi->input;
  352. __set_bit(EV_KEY, input->evbit);
  353. if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
  354. __set_bit(BTN_LEFT, input->keybit);
  355. __set_bit(BTN_RIGHT, input->keybit);
  356. if (emulate_3button)
  357. __set_bit(BTN_MIDDLE, input->keybit);
  358. __set_bit(EV_REL, input->evbit);
  359. __set_bit(REL_X, input->relbit);
  360. __set_bit(REL_Y, input->relbit);
  361. if (emulate_scroll_wheel) {
  362. __set_bit(REL_WHEEL, input->relbit);
  363. __set_bit(REL_HWHEEL, input->relbit);
  364. }
  365. } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  366. /* input->keybit is initialized with incorrect button info
  367. * for Magic Trackpad. There really is only one physical
  368. * button (BTN_LEFT == BTN_MOUSE). Make sure we don't
  369. * advertise buttons that don't exist...
  370. */
  371. __clear_bit(BTN_RIGHT, input->keybit);
  372. __clear_bit(BTN_MIDDLE, input->keybit);
  373. __set_bit(BTN_MOUSE, input->keybit);
  374. __set_bit(BTN_TOOL_FINGER, input->keybit);
  375. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  376. __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
  377. __set_bit(BTN_TOOL_QUADTAP, input->keybit);
  378. __set_bit(BTN_TOUCH, input->keybit);
  379. __set_bit(INPUT_PROP_POINTER, input->propbit);
  380. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  381. }
  382. if (report_touches) {
  383. __set_bit(EV_ABS, input->evbit);
  384. input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 15, 0, 0);
  385. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 4, 0);
  386. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 4, 0);
  387. input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
  388. /* Note: Touch Y position from the device is inverted relative
  389. * to how pointer motion is reported (and relative to how USB
  390. * HID recommends the coordinates work). This driver keeps
  391. * the origin at the same position, and just uses the additive
  392. * inverse of the reported Y.
  393. */
  394. if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
  395. input_set_abs_params(input, ABS_MT_POSITION_X,
  396. MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
  397. input_set_abs_params(input, ABS_MT_POSITION_Y,
  398. MOUSE_MIN_Y, MOUSE_MAX_Y, 4, 0);
  399. input_abs_set_res(input, ABS_MT_POSITION_X,
  400. MOUSE_RES_X);
  401. input_abs_set_res(input, ABS_MT_POSITION_Y,
  402. MOUSE_RES_Y);
  403. } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  404. input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
  405. TRACKPAD_MAX_X, 4, 0);
  406. input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
  407. TRACKPAD_MAX_Y, 4, 0);
  408. input_set_abs_params(input, ABS_MT_POSITION_X,
  409. TRACKPAD_MIN_X, TRACKPAD_MAX_X, 4, 0);
  410. input_set_abs_params(input, ABS_MT_POSITION_Y,
  411. TRACKPAD_MIN_Y, TRACKPAD_MAX_Y, 4, 0);
  412. input_abs_set_res(input, ABS_X, TRACKPAD_RES_X);
  413. input_abs_set_res(input, ABS_Y, TRACKPAD_RES_Y);
  414. input_abs_set_res(input, ABS_MT_POSITION_X,
  415. TRACKPAD_RES_X);
  416. input_abs_set_res(input, ABS_MT_POSITION_Y,
  417. TRACKPAD_RES_Y);
  418. }
  419. input_set_events_per_packet(input, 60);
  420. }
  421. if (report_undeciphered) {
  422. __set_bit(EV_MSC, input->evbit);
  423. __set_bit(MSC_RAW, input->mscbit);
  424. }
  425. return 0;
  426. }
  427. static int magicmouse_input_mapping(struct hid_device *hdev,
  428. struct hid_input *hi, struct hid_field *field,
  429. struct hid_usage *usage, unsigned long **bit, int *max)
  430. {
  431. struct magicmouse_sc *msc = hid_get_drvdata(hdev);
  432. if (!msc->input)
  433. msc->input = hi->input;
  434. /* Magic Trackpad does not give relative data after switching to MT */
  435. if (hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD &&
  436. field->flags & HID_MAIN_ITEM_RELATIVE)
  437. return -1;
  438. return 0;
  439. }
  440. static int magicmouse_probe(struct hid_device *hdev,
  441. const struct hid_device_id *id)
  442. {
  443. __u8 feature[] = { 0xd7, 0x01 };
  444. struct magicmouse_sc *msc;
  445. struct hid_report *report;
  446. int ret;
  447. msc = kzalloc(sizeof(*msc), GFP_KERNEL);
  448. if (msc == NULL) {
  449. hid_err(hdev, "can't alloc magicmouse descriptor\n");
  450. return -ENOMEM;
  451. }
  452. msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
  453. msc->quirks = id->driver_data;
  454. hid_set_drvdata(hdev, msc);
  455. msc->single_touch_id = NO_TOUCHES;
  456. ret = hid_parse(hdev);
  457. if (ret) {
  458. hid_err(hdev, "magicmouse hid parse failed\n");
  459. goto err_free;
  460. }
  461. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  462. if (ret) {
  463. hid_err(hdev, "magicmouse hw start failed\n");
  464. goto err_free;
  465. }
  466. if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
  467. report = hid_register_report(hdev, HID_INPUT_REPORT,
  468. MOUSE_REPORT_ID);
  469. else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
  470. report = hid_register_report(hdev, HID_INPUT_REPORT,
  471. TRACKPAD_REPORT_ID);
  472. report = hid_register_report(hdev, HID_INPUT_REPORT,
  473. DOUBLE_REPORT_ID);
  474. }
  475. if (!report) {
  476. hid_err(hdev, "unable to register touch report\n");
  477. ret = -ENOMEM;
  478. goto err_stop_hw;
  479. }
  480. report->size = 6;
  481. /*
  482. * Some devices repond with 'invalid report id' when feature
  483. * report switching it into multitouch mode is sent to it.
  484. *
  485. * This results in -EIO from the _raw low-level transport callback,
  486. * but there seems to be no other way of switching the mode.
  487. * Thus the super-ugly hacky success check below.
  488. */
  489. ret = hdev->hid_output_raw_report(hdev, feature, sizeof(feature),
  490. HID_FEATURE_REPORT);
  491. if (ret != -EIO && ret != sizeof(feature)) {
  492. hid_err(hdev, "unable to request touch data (%d)\n", ret);
  493. goto err_stop_hw;
  494. }
  495. return 0;
  496. err_stop_hw:
  497. hid_hw_stop(hdev);
  498. err_free:
  499. kfree(msc);
  500. return ret;
  501. }
  502. static void magicmouse_remove(struct hid_device *hdev)
  503. {
  504. struct magicmouse_sc *msc = hid_get_drvdata(hdev);
  505. hid_hw_stop(hdev);
  506. kfree(msc);
  507. }
  508. static const struct hid_device_id magic_mice[] = {
  509. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  510. USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
  511. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
  512. USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
  513. { }
  514. };
  515. MODULE_DEVICE_TABLE(hid, magic_mice);
  516. static struct hid_driver magicmouse_driver = {
  517. .name = "magicmouse",
  518. .id_table = magic_mice,
  519. .probe = magicmouse_probe,
  520. .remove = magicmouse_remove,
  521. .raw_event = magicmouse_raw_event,
  522. .input_mapping = magicmouse_input_mapping,
  523. .input_register = magicmouse_setup_input,
  524. };
  525. static int __init magicmouse_init(void)
  526. {
  527. int ret;
  528. ret = hid_register_driver(&magicmouse_driver);
  529. if (ret)
  530. pr_err("can't register magicmouse driver\n");
  531. return ret;
  532. }
  533. static void __exit magicmouse_exit(void)
  534. {
  535. hid_unregister_driver(&magicmouse_driver);
  536. }
  537. module_init(magicmouse_init);
  538. module_exit(magicmouse_exit);
  539. MODULE_LICENSE("GPL");