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