hid-magicmouse.c 17 KB

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