Commit | Line | Data |
---|---|---|
128537ce MP |
1 | /* |
2 | * Apple "Magic" Wireless Mouse driver | |
3 | * | |
4 | * Copyright (c) 2010 Michael Poole <mdpoole@troilus.org> | |
5 | */ | |
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 | ||
14 | #include <linux/device.h> | |
15 | #include <linux/hid.h> | |
16 | #include <linux/module.h> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
128537ce MP |
18 | #include <linux/usb.h> |
19 | ||
20 | #include "hid-ids.h" | |
21 | ||
71b38bd4 | 22 | static bool emulate_3button = true; |
128537ce MP |
23 | module_param(emulate_3button, bool, 0644); |
24 | MODULE_PARM_DESC(emulate_3button, "Emulate a middle button"); | |
25 | ||
26 | static int middle_button_start = -350; | |
27 | static int middle_button_stop = +350; | |
28 | ||
71b38bd4 | 29 | static bool emulate_scroll_wheel = true; |
128537ce MP |
30 | module_param(emulate_scroll_wheel, bool, 0644); |
31 | MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel"); | |
32 | ||
0b778e76 CD |
33 | static unsigned int scroll_speed = 32; |
34 | static int param_set_scroll_speed(const char *val, struct kernel_param *kp) { | |
35 | unsigned long speed; | |
36 | if (!val || strict_strtoul(val, 0, &speed) || speed > 63) | |
37 | return -EINVAL; | |
38 | scroll_speed = speed; | |
39 | return 0; | |
40 | } | |
41 | module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644); | |
42 | MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)"); | |
43 | ||
9846f350 CD |
44 | static bool scroll_acceleration = false; |
45 | module_param(scroll_acceleration, bool, 0644); | |
46 | MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events"); | |
47 | ||
71b38bd4 | 48 | static bool report_touches = true; |
128537ce MP |
49 | module_param(report_touches, bool, 0644); |
50 | MODULE_PARM_DESC(report_touches, "Emit touch records (otherwise, only use them for emulation)"); | |
51 | ||
71b38bd4 | 52 | static bool report_undeciphered; |
128537ce MP |
53 | module_param(report_undeciphered, bool, 0644); |
54 | MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event"); | |
55 | ||
56 | #define TOUCH_REPORT_ID 0x29 | |
57 | /* These definitions are not precise, but they're close enough. (Bits | |
58 | * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem | |
59 | * to be some kind of bit mask -- 0x20 may be a near-field reading, | |
60 | * and 0x40 is actual contact, and 0x10 may be a start/stop or change | |
61 | * indication.) | |
62 | */ | |
63 | #define TOUCH_STATE_MASK 0xf0 | |
64 | #define TOUCH_STATE_NONE 0x00 | |
65 | #define TOUCH_STATE_START 0x30 | |
66 | #define TOUCH_STATE_DRAG 0x40 | |
67 | ||
0b778e76 CD |
68 | #define SCROLL_ACCEL_DEFAULT 7 |
69 | ||
128537ce MP |
70 | /** |
71 | * struct magicmouse_sc - Tracks Magic Mouse-specific data. | |
72 | * @input: Input device through which we report events. | |
73 | * @quirks: Currently unused. | |
74 | * @last_timestamp: Timestamp from most recent (18-bit) touch report | |
75 | * (units of milliseconds over short windows, but seems to | |
76 | * increase faster when there are no touches). | |
77 | * @delta_time: 18-bit difference between the two most recent touch | |
78 | * reports from the mouse. | |
79 | * @ntouches: Number of touches in most recent touch report. | |
80 | * @scroll_accel: Number of consecutive scroll motions. | |
81 | * @scroll_jiffies: Time of last scroll motion. | |
82 | * @touches: Most recent data for a touch, indexed by tracking ID. | |
83 | * @tracking_ids: Mapping of current touch input data to @touches. | |
84 | */ | |
85 | struct magicmouse_sc { | |
86 | struct input_dev *input; | |
87 | unsigned long quirks; | |
88 | ||
89 | int last_timestamp; | |
90 | int delta_time; | |
91 | int ntouches; | |
92 | int scroll_accel; | |
93 | unsigned long scroll_jiffies; | |
94 | ||
95 | struct { | |
96 | short x; | |
97 | short y; | |
98 | short scroll_y; | |
99 | u8 size; | |
100 | } touches[16]; | |
101 | int tracking_ids[16]; | |
102 | }; | |
103 | ||
104 | static int magicmouse_firm_touch(struct magicmouse_sc *msc) | |
105 | { | |
106 | int touch = -1; | |
107 | int ii; | |
108 | ||
109 | /* If there is only one "firm" touch, set touch to its | |
110 | * tracking ID. | |
111 | */ | |
112 | for (ii = 0; ii < msc->ntouches; ii++) { | |
113 | int idx = msc->tracking_ids[ii]; | |
114 | if (msc->touches[idx].size < 8) { | |
115 | /* Ignore this touch. */ | |
116 | } else if (touch >= 0) { | |
117 | touch = -1; | |
118 | break; | |
119 | } else { | |
120 | touch = idx; | |
121 | } | |
122 | } | |
123 | ||
124 | return touch; | |
125 | } | |
126 | ||
127 | static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state) | |
128 | { | |
71b38bd4 MP |
129 | int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 | |
130 | test_bit(BTN_RIGHT, msc->input->key) << 1 | | |
131 | test_bit(BTN_MIDDLE, msc->input->key) << 2; | |
128537ce MP |
132 | |
133 | if (emulate_3button) { | |
134 | int id; | |
135 | ||
136 | /* If some button was pressed before, keep it held | |
137 | * down. Otherwise, if there's exactly one firm | |
138 | * touch, use that to override the mouse's guess. | |
139 | */ | |
140 | if (state == 0) { | |
141 | /* The button was released. */ | |
142 | } else if (last_state != 0) { | |
143 | state = last_state; | |
144 | } else if ((id = magicmouse_firm_touch(msc)) >= 0) { | |
145 | int x = msc->touches[id].x; | |
146 | if (x < middle_button_start) | |
147 | state = 1; | |
148 | else if (x > middle_button_stop) | |
149 | state = 2; | |
150 | else | |
151 | state = 4; | |
152 | } /* else: we keep the mouse's guess */ | |
153 | ||
154 | input_report_key(msc->input, BTN_MIDDLE, state & 4); | |
155 | } | |
156 | ||
157 | input_report_key(msc->input, BTN_LEFT, state & 1); | |
158 | input_report_key(msc->input, BTN_RIGHT, state & 2); | |
159 | ||
160 | if (state != last_state) | |
0b778e76 | 161 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT; |
128537ce MP |
162 | } |
163 | ||
164 | static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata) | |
165 | { | |
166 | struct input_dev *input = msc->input; | |
167 | __s32 x_y = tdata[0] << 8 | tdata[1] << 16 | tdata[2] << 24; | |
168 | int misc = tdata[5] | tdata[6] << 8; | |
169 | int id = (misc >> 6) & 15; | |
170 | int x = x_y << 12 >> 20; | |
171 | int y = -(x_y >> 20); | |
172 | ||
173 | /* Store tracking ID and other fields. */ | |
174 | msc->tracking_ids[raw_id] = id; | |
175 | msc->touches[id].x = x; | |
176 | msc->touches[id].y = y; | |
177 | msc->touches[id].size = misc & 63; | |
178 | ||
179 | /* If requested, emulate a scroll wheel by detecting small | |
ef566d30 | 180 | * vertical touch motions. |
128537ce | 181 | */ |
ef566d30 | 182 | if (emulate_scroll_wheel) { |
128537ce MP |
183 | unsigned long now = jiffies; |
184 | int step = msc->touches[id].scroll_y - y; | |
185 | ||
128537ce MP |
186 | /* Calculate and apply the scroll motion. */ |
187 | switch (tdata[7] & TOUCH_STATE_MASK) { | |
188 | case TOUCH_STATE_START: | |
189 | msc->touches[id].scroll_y = y; | |
0b778e76 CD |
190 | |
191 | /* Reset acceleration after half a second. */ | |
192 | if (scroll_acceleration && time_before(now, | |
193 | msc->scroll_jiffies + HZ / 2)) | |
194 | msc->scroll_accel = max_t(int, | |
195 | msc->scroll_accel - 1, 1); | |
196 | else | |
197 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT; | |
198 | ||
128537ce MP |
199 | break; |
200 | case TOUCH_STATE_DRAG: | |
0b778e76 | 201 | step /= (64 - (int)scroll_speed) * msc->scroll_accel; |
128537ce | 202 | if (step != 0) { |
0b778e76 CD |
203 | msc->touches[id].scroll_y -= step * |
204 | (64 - scroll_speed) * msc->scroll_accel; | |
128537ce MP |
205 | msc->scroll_jiffies = now; |
206 | input_report_rel(input, REL_WHEEL, step); | |
207 | } | |
208 | break; | |
209 | } | |
210 | } | |
211 | ||
212 | /* Generate the input events for this touch. */ | |
213 | if (report_touches) { | |
71b38bd4 | 214 | int orientation = (misc >> 10) - 32; |
128537ce MP |
215 | |
216 | input_report_abs(input, ABS_MT_TRACKING_ID, id); | |
217 | input_report_abs(input, ABS_MT_TOUCH_MAJOR, tdata[3]); | |
218 | input_report_abs(input, ABS_MT_TOUCH_MINOR, tdata[4]); | |
219 | input_report_abs(input, ABS_MT_ORIENTATION, orientation); | |
220 | input_report_abs(input, ABS_MT_POSITION_X, x); | |
221 | input_report_abs(input, ABS_MT_POSITION_Y, y); | |
222 | ||
71b38bd4 | 223 | if (report_undeciphered) |
128537ce | 224 | input_event(input, EV_MSC, MSC_RAW, tdata[7]); |
128537ce MP |
225 | |
226 | input_mt_sync(input); | |
227 | } | |
228 | } | |
229 | ||
230 | static int magicmouse_raw_event(struct hid_device *hdev, | |
231 | struct hid_report *report, u8 *data, int size) | |
232 | { | |
233 | struct magicmouse_sc *msc = hid_get_drvdata(hdev); | |
234 | struct input_dev *input = msc->input; | |
235 | int x, y, ts, ii, clicks; | |
236 | ||
237 | switch (data[0]) { | |
238 | case 0x10: | |
239 | if (size != 6) | |
240 | return 0; | |
241 | x = (__s16)(data[2] | data[3] << 8); | |
242 | y = (__s16)(data[4] | data[5] << 8); | |
243 | clicks = data[1]; | |
244 | break; | |
245 | case TOUCH_REPORT_ID: | |
246 | /* Expect six bytes of prefix, and N*8 bytes of touch data. */ | |
247 | if (size < 6 || ((size - 6) % 8) != 0) | |
248 | return 0; | |
249 | ts = data[3] >> 6 | data[4] << 2 | data[5] << 10; | |
250 | msc->delta_time = (ts - msc->last_timestamp) & 0x3ffff; | |
251 | msc->last_timestamp = ts; | |
252 | msc->ntouches = (size - 6) / 8; | |
253 | for (ii = 0; ii < msc->ntouches; ii++) | |
254 | magicmouse_emit_touch(msc, ii, data + ii * 8 + 6); | |
255 | /* When emulating three-button mode, it is important | |
256 | * to have the current touch information before | |
257 | * generating a click event. | |
258 | */ | |
259 | x = (signed char)data[1]; | |
260 | y = (signed char)data[2]; | |
261 | clicks = data[3]; | |
262 | break; | |
263 | case 0x20: /* Theoretically battery status (0-100), but I have | |
264 | * never seen it -- maybe it is only upon request. | |
265 | */ | |
266 | case 0x60: /* Unknown, maybe laser on/off. */ | |
267 | case 0x61: /* Laser reflection status change. | |
268 | * data[1]: 0 = spotted, 1 = lost | |
269 | */ | |
270 | default: | |
271 | return 0; | |
272 | } | |
273 | ||
274 | magicmouse_emit_buttons(msc, clicks & 3); | |
275 | input_report_rel(input, REL_X, x); | |
276 | input_report_rel(input, REL_Y, y); | |
277 | input_sync(input); | |
278 | return 1; | |
279 | } | |
280 | ||
281 | static int magicmouse_input_open(struct input_dev *dev) | |
282 | { | |
283 | struct hid_device *hid = input_get_drvdata(dev); | |
284 | ||
285 | return hid->ll_driver->open(hid); | |
286 | } | |
287 | ||
288 | static void magicmouse_input_close(struct input_dev *dev) | |
289 | { | |
290 | struct hid_device *hid = input_get_drvdata(dev); | |
291 | ||
292 | hid->ll_driver->close(hid); | |
293 | } | |
294 | ||
295 | static void magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev) | |
296 | { | |
297 | input_set_drvdata(input, hdev); | |
298 | input->event = hdev->ll_driver->hidinput_input_event; | |
299 | input->open = magicmouse_input_open; | |
300 | input->close = magicmouse_input_close; | |
301 | ||
302 | input->name = hdev->name; | |
303 | input->phys = hdev->phys; | |
304 | input->uniq = hdev->uniq; | |
305 | input->id.bustype = hdev->bus; | |
306 | input->id.vendor = hdev->vendor; | |
307 | input->id.product = hdev->product; | |
308 | input->id.version = hdev->version; | |
309 | input->dev.parent = hdev->dev.parent; | |
310 | ||
71b38bd4 MP |
311 | __set_bit(EV_KEY, input->evbit); |
312 | __set_bit(BTN_LEFT, input->keybit); | |
313 | __set_bit(BTN_RIGHT, input->keybit); | |
128537ce | 314 | if (emulate_3button) |
71b38bd4 MP |
315 | __set_bit(BTN_MIDDLE, input->keybit); |
316 | __set_bit(BTN_TOOL_FINGER, input->keybit); | |
128537ce | 317 | |
71b38bd4 MP |
318 | __set_bit(EV_REL, input->evbit); |
319 | __set_bit(REL_X, input->relbit); | |
320 | __set_bit(REL_Y, input->relbit); | |
128537ce | 321 | if (emulate_scroll_wheel) |
71b38bd4 | 322 | __set_bit(REL_WHEEL, input->relbit); |
128537ce MP |
323 | |
324 | if (report_touches) { | |
71b38bd4 MP |
325 | __set_bit(EV_ABS, input->evbit); |
326 | ||
327 | input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 15, 0, 0); | |
328 | input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 4, 0); | |
329 | input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 4, 0); | |
330 | input_set_abs_params(input, ABS_MT_ORIENTATION, -32, 31, 1, 0); | |
331 | input_set_abs_params(input, ABS_MT_POSITION_X, -1100, 1358, | |
332 | 4, 0); | |
128537ce MP |
333 | /* Note: Touch Y position from the device is inverted relative |
334 | * to how pointer motion is reported (and relative to how USB | |
335 | * HID recommends the coordinates work). This driver keeps | |
336 | * the origin at the same position, and just uses the additive | |
337 | * inverse of the reported Y. | |
338 | */ | |
71b38bd4 MP |
339 | input_set_abs_params(input, ABS_MT_POSITION_Y, -1589, 2047, |
340 | 4, 0); | |
128537ce MP |
341 | } |
342 | ||
343 | if (report_undeciphered) { | |
71b38bd4 MP |
344 | __set_bit(EV_MSC, input->evbit); |
345 | __set_bit(MSC_RAW, input->mscbit); | |
128537ce MP |
346 | } |
347 | } | |
348 | ||
349 | static int magicmouse_probe(struct hid_device *hdev, | |
350 | const struct hid_device_id *id) | |
351 | { | |
352 | __u8 feature_1[] = { 0xd7, 0x01 }; | |
353 | __u8 feature_2[] = { 0xf8, 0x01, 0x32 }; | |
354 | struct input_dev *input; | |
355 | struct magicmouse_sc *msc; | |
356 | struct hid_report *report; | |
357 | int ret; | |
358 | ||
359 | msc = kzalloc(sizeof(*msc), GFP_KERNEL); | |
360 | if (msc == NULL) { | |
361 | dev_err(&hdev->dev, "can't alloc magicmouse descriptor\n"); | |
362 | return -ENOMEM; | |
363 | } | |
364 | ||
0b778e76 CD |
365 | msc->scroll_accel = SCROLL_ACCEL_DEFAULT; |
366 | ||
128537ce MP |
367 | msc->quirks = id->driver_data; |
368 | hid_set_drvdata(hdev, msc); | |
369 | ||
370 | ret = hid_parse(hdev); | |
371 | if (ret) { | |
372 | dev_err(&hdev->dev, "magicmouse hid parse failed\n"); | |
373 | goto err_free; | |
374 | } | |
375 | ||
23d02116 | 376 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
128537ce MP |
377 | if (ret) { |
378 | dev_err(&hdev->dev, "magicmouse hw start failed\n"); | |
379 | goto err_free; | |
380 | } | |
381 | ||
23d02116 JK |
382 | /* we are handling the input ourselves */ |
383 | hidinput_disconnect(hdev); | |
384 | ||
128537ce MP |
385 | report = hid_register_report(hdev, HID_INPUT_REPORT, TOUCH_REPORT_ID); |
386 | if (!report) { | |
387 | dev_err(&hdev->dev, "unable to register touch report\n"); | |
388 | ret = -ENOMEM; | |
71b38bd4 | 389 | goto err_stop_hw; |
128537ce MP |
390 | } |
391 | report->size = 6; | |
392 | ||
393 | ret = hdev->hid_output_raw_report(hdev, feature_1, sizeof(feature_1), | |
394 | HID_FEATURE_REPORT); | |
395 | if (ret != sizeof(feature_1)) { | |
396 | dev_err(&hdev->dev, "unable to request touch data (1:%d)\n", | |
397 | ret); | |
71b38bd4 | 398 | goto err_stop_hw; |
128537ce MP |
399 | } |
400 | ret = hdev->hid_output_raw_report(hdev, feature_2, | |
401 | sizeof(feature_2), HID_FEATURE_REPORT); | |
402 | if (ret != sizeof(feature_2)) { | |
403 | dev_err(&hdev->dev, "unable to request touch data (2:%d)\n", | |
404 | ret); | |
71b38bd4 | 405 | goto err_stop_hw; |
128537ce MP |
406 | } |
407 | ||
408 | input = input_allocate_device(); | |
409 | if (!input) { | |
410 | dev_err(&hdev->dev, "can't alloc input device\n"); | |
411 | ret = -ENOMEM; | |
71b38bd4 | 412 | goto err_stop_hw; |
128537ce MP |
413 | } |
414 | magicmouse_setup_input(input, hdev); | |
415 | ||
416 | ret = input_register_device(input); | |
417 | if (ret) { | |
418 | dev_err(&hdev->dev, "input device registration failed\n"); | |
71b38bd4 | 419 | goto err_input; |
128537ce MP |
420 | } |
421 | msc->input = input; | |
422 | ||
423 | return 0; | |
71b38bd4 | 424 | err_input: |
128537ce | 425 | input_free_device(input); |
71b38bd4 MP |
426 | err_stop_hw: |
427 | hid_hw_stop(hdev); | |
428 | err_free: | |
128537ce MP |
429 | kfree(msc); |
430 | return ret; | |
431 | } | |
432 | ||
433 | static void magicmouse_remove(struct hid_device *hdev) | |
434 | { | |
28918c21 MP |
435 | struct magicmouse_sc *msc = hid_get_drvdata(hdev); |
436 | ||
128537ce | 437 | hid_hw_stop(hdev); |
28918c21 MP |
438 | input_unregister_device(msc->input); |
439 | kfree(msc); | |
128537ce MP |
440 | } |
441 | ||
442 | static const struct hid_device_id magic_mice[] = { | |
443 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE), | |
444 | .driver_data = 0 }, | |
445 | { } | |
446 | }; | |
447 | MODULE_DEVICE_TABLE(hid, magic_mice); | |
448 | ||
449 | static struct hid_driver magicmouse_driver = { | |
450 | .name = "magicmouse", | |
451 | .id_table = magic_mice, | |
452 | .probe = magicmouse_probe, | |
453 | .remove = magicmouse_remove, | |
454 | .raw_event = magicmouse_raw_event, | |
455 | }; | |
456 | ||
457 | static int __init magicmouse_init(void) | |
458 | { | |
459 | int ret; | |
460 | ||
461 | ret = hid_register_driver(&magicmouse_driver); | |
462 | if (ret) | |
463 | printk(KERN_ERR "can't register magicmouse driver\n"); | |
464 | ||
465 | return ret; | |
466 | } | |
467 | ||
468 | static void __exit magicmouse_exit(void) | |
469 | { | |
470 | hid_unregister_driver(&magicmouse_driver); | |
471 | } | |
472 | ||
473 | module_init(magicmouse_init); | |
474 | module_exit(magicmouse_exit); | |
475 | MODULE_LICENSE("GPL"); |