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