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