2 * Apple "Magic" Wireless Mouse driver
4 * Copyright (c) 2010 Michael Poole <mdpoole@troilus.org>
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)
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>
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 scroll_acceleration = false;
34 module_param(scroll_acceleration, bool, 0644);
35 MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
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)");
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");
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
52 #define TOUCH_STATE_MASK 0xf0
53 #define TOUCH_STATE_NONE 0x00
54 #define TOUCH_STATE_START 0x30
55 #define TOUCH_STATE_DRAG 0x40
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.
72 struct magicmouse_sc {
73 struct input_dev *input;
80 unsigned long scroll_jiffies;
91 static int magicmouse_firm_touch(struct magicmouse_sc *msc)
96 /* If there is only one "firm" touch, set touch to its
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) {
114 static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
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;
120 if (emulate_3button) {
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.
128 /* The button was released. */
129 } else if (last_state != 0) {
131 } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
132 int x = msc->touches[id].x;
133 if (x < middle_button_start)
135 else if (x > middle_button_stop)
139 } /* else: we keep the mouse's guess */
141 input_report_key(msc->input, BTN_MIDDLE, state & 4);
144 input_report_key(msc->input, BTN_LEFT, state & 1);
145 input_report_key(msc->input, BTN_RIGHT, state & 2);
147 if (state != last_state)
148 msc->scroll_accel = 0;
151 static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
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);
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;
166 /* If requested, emulate a scroll wheel by detecting small
167 * vertical touch motions.
169 if (emulate_scroll_wheel) {
170 static const int accel_profile[] = {
171 256, 228, 192, 160, 128, 96, 64, 32,
173 unsigned long now = jiffies;
174 int step = msc->touches[id].scroll_y - y;
176 /* Reset acceleration after half a second. */
177 if (time_after(now, msc->scroll_jiffies + HZ / 2))
178 msc->scroll_accel = 0;
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);
189 case TOUCH_STATE_DRAG:
190 step = step / accel_profile[msc->scroll_accel];
192 msc->touches[id].scroll_y = y;
193 msc->scroll_jiffies = now;
194 input_report_rel(input, REL_WHEEL, step);
200 /* Generate the input events for this touch. */
201 if (report_touches) {
202 int orientation = (misc >> 10) - 32;
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);
211 if (report_undeciphered)
212 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
214 input_mt_sync(input);
218 static int magicmouse_raw_event(struct hid_device *hdev,
219 struct hid_report *report, u8 *data, int size)
221 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
222 struct input_dev *input = msc->input;
223 int x, y, ts, ii, clicks;
229 x = (__s16)(data[2] | data[3] << 8);
230 y = (__s16)(data[4] | data[5] << 8);
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)
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.
247 x = (signed char)data[1];
248 y = (signed char)data[2];
251 case 0x20: /* Theoretically battery status (0-100), but I have
252 * never seen it -- maybe it is only upon request.
254 case 0x60: /* Unknown, maybe laser on/off. */
255 case 0x61: /* Laser reflection status change.
256 * data[1]: 0 = spotted, 1 = lost
262 magicmouse_emit_buttons(msc, clicks & 3);
263 input_report_rel(input, REL_X, x);
264 input_report_rel(input, REL_Y, y);
269 static int magicmouse_input_open(struct input_dev *dev)
271 struct hid_device *hid = input_get_drvdata(dev);
273 return hid->ll_driver->open(hid);
276 static void magicmouse_input_close(struct input_dev *dev)
278 struct hid_device *hid = input_get_drvdata(dev);
280 hid->ll_driver->close(hid);
283 static void magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
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;
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;
299 __set_bit(EV_KEY, input->evbit);
300 __set_bit(BTN_LEFT, input->keybit);
301 __set_bit(BTN_RIGHT, input->keybit);
303 __set_bit(BTN_MIDDLE, input->keybit);
304 __set_bit(BTN_TOOL_FINGER, input->keybit);
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);
312 if (report_touches) {
313 __set_bit(EV_ABS, input->evbit);
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,
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.
327 input_set_abs_params(input, ABS_MT_POSITION_Y, -1589, 2047,
331 if (report_undeciphered) {
332 __set_bit(EV_MSC, input->evbit);
333 __set_bit(MSC_RAW, input->mscbit);
337 static int magicmouse_probe(struct hid_device *hdev,
338 const struct hid_device_id *id)
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;
347 msc = kzalloc(sizeof(*msc), GFP_KERNEL);
349 dev_err(&hdev->dev, "can't alloc magicmouse descriptor\n");
353 msc->quirks = id->driver_data;
354 hid_set_drvdata(hdev, msc);
356 ret = hid_parse(hdev);
358 dev_err(&hdev->dev, "magicmouse hid parse failed\n");
362 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
364 dev_err(&hdev->dev, "magicmouse hw start failed\n");
368 /* we are handling the input ourselves */
369 hidinput_disconnect(hdev);
371 report = hid_register_report(hdev, HID_INPUT_REPORT, TOUCH_REPORT_ID);
373 dev_err(&hdev->dev, "unable to register touch report\n");
379 ret = hdev->hid_output_raw_report(hdev, feature_1, sizeof(feature_1),
381 if (ret != sizeof(feature_1)) {
382 dev_err(&hdev->dev, "unable to request touch data (1:%d)\n",
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",
394 input = input_allocate_device();
396 dev_err(&hdev->dev, "can't alloc input device\n");
400 magicmouse_setup_input(input, hdev);
402 ret = input_register_device(input);
404 dev_err(&hdev->dev, "input device registration failed\n");
411 input_free_device(input);
419 static void magicmouse_remove(struct hid_device *hdev)
421 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
424 input_unregister_device(msc->input);
428 static const struct hid_device_id magic_mice[] = {
429 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE),
433 MODULE_DEVICE_TABLE(hid, magic_mice);
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,
443 static int __init magicmouse_init(void)
447 ret = hid_register_driver(&magicmouse_driver);
449 printk(KERN_ERR "can't register magicmouse driver\n");
454 static void __exit magicmouse_exit(void)
456 hid_unregister_driver(&magicmouse_driver);
459 module_init(magicmouse_init);
460 module_exit(magicmouse_exit);
461 MODULE_LICENSE("GPL");