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