blob: cd706354496ae3c599974528d503c9c6ceaabbaa [file] [log] [blame]
Michael Poole128537c2010-02-06 12:24:36 -05001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Michael Poole128537c2010-02-06 12:24:36 -050018#include <linux/usb.h>
19
20#include "hid-ids.h"
21
Michael Poole71b38bd2010-02-11 00:32:57 -050022static bool emulate_3button = true;
Michael Poole128537c2010-02-06 12:24:36 -050023module_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
Michael Poole71b38bd2010-02-11 00:32:57 -050029static bool emulate_scroll_wheel = true;
Michael Poole128537c2010-02-06 12:24:36 -050030module_param(emulate_scroll_wheel, bool, 0644);
31MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
32
Michael Poole71b38bd2010-02-11 00:32:57 -050033static bool report_touches = true;
Michael Poole128537c2010-02-06 12:24:36 -050034module_param(report_touches, bool, 0644);
35MODULE_PARM_DESC(report_touches, "Emit touch records (otherwise, only use them for emulation)");
36
Michael Poole71b38bd2010-02-11 00:32:57 -050037static bool report_undeciphered;
Michael Poole128537c2010-02-06 12:24:36 -050038module_param(report_undeciphered, bool, 0644);
39MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
40
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.)
47 */
48#define TOUCH_STATE_MASK 0xf0
49#define TOUCH_STATE_NONE 0x00
50#define TOUCH_STATE_START 0x30
51#define TOUCH_STATE_DRAG 0x40
52
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.
67 */
68struct magicmouse_sc {
69 struct input_dev *input;
70 unsigned long quirks;
71
72 int last_timestamp;
73 int delta_time;
74 int ntouches;
75 int scroll_accel;
76 unsigned long scroll_jiffies;
77
78 struct {
79 short x;
80 short y;
81 short scroll_y;
82 u8 size;
83 } touches[16];
84 int tracking_ids[16];
85};
86
87static int magicmouse_firm_touch(struct magicmouse_sc *msc)
88{
89 int touch = -1;
90 int ii;
91
92 /* If there is only one "firm" touch, set touch to its
93 * tracking ID.
94 */
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;
104 }
105 }
106
107 return touch;
108}
109
110static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
111{
Michael Poole71b38bd2010-02-11 00:32:57 -0500112 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;
Michael Poole128537c2010-02-06 12:24:36 -0500115
116 if (emulate_3button) {
117 int id;
118
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.
122 */
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 */
136
137 input_report_key(msc->input, BTN_MIDDLE, state & 4);
138 }
139
140 input_report_key(msc->input, BTN_LEFT, state & 1);
141 input_report_key(msc->input, BTN_RIGHT, state & 2);
142
143 if (state != last_state)
144 msc->scroll_accel = 0;
145}
146
147static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
148{
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);
155
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;
161
162 /* If requested, emulate a scroll wheel by detecting small
Chase Douglasef566d32010-06-02 10:28:25 -0400163 * vertical touch motions.
Michael Poole128537c2010-02-06 12:24:36 -0500164 */
Chase Douglasef566d32010-06-02 10:28:25 -0400165 if (emulate_scroll_wheel) {
Michael Poole128537c2010-02-06 12:24:36 -0500166 static const int accel_profile[] = {
167 256, 228, 192, 160, 128, 96, 64, 32,
168 };
169 unsigned long now = jiffies;
170 int step = msc->touches[id].scroll_y - y;
171
172 /* Reset acceleration after half a second. */
173 if (time_after(now, msc->scroll_jiffies + HZ / 2))
174 msc->scroll_accel = 0;
175
176 /* Calculate and apply the scroll motion. */
177 switch (tdata[7] & TOUCH_STATE_MASK) {
178 case TOUCH_STATE_START:
179 msc->touches[id].scroll_y = y;
Michael Poole71b38bd2010-02-11 00:32:57 -0500180 msc->scroll_accel = min_t(int, msc->scroll_accel + 1,
Michael Poole128537c2010-02-06 12:24:36 -0500181 ARRAY_SIZE(accel_profile) - 1);
182 break;
183 case TOUCH_STATE_DRAG:
184 step = step / accel_profile[msc->scroll_accel];
185 if (step != 0) {
186 msc->touches[id].scroll_y = y;
187 msc->scroll_jiffies = now;
188 input_report_rel(input, REL_WHEEL, step);
189 }
190 break;
191 }
192 }
193
194 /* Generate the input events for this touch. */
195 if (report_touches) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500196 int orientation = (misc >> 10) - 32;
Michael Poole128537c2010-02-06 12:24:36 -0500197
198 input_report_abs(input, ABS_MT_TRACKING_ID, id);
199 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tdata[3]);
200 input_report_abs(input, ABS_MT_TOUCH_MINOR, tdata[4]);
201 input_report_abs(input, ABS_MT_ORIENTATION, orientation);
202 input_report_abs(input, ABS_MT_POSITION_X, x);
203 input_report_abs(input, ABS_MT_POSITION_Y, y);
204
Michael Poole71b38bd2010-02-11 00:32:57 -0500205 if (report_undeciphered)
Michael Poole128537c2010-02-06 12:24:36 -0500206 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
Michael Poole128537c2010-02-06 12:24:36 -0500207
208 input_mt_sync(input);
209 }
210}
211
212static int magicmouse_raw_event(struct hid_device *hdev,
213 struct hid_report *report, u8 *data, int size)
214{
215 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
216 struct input_dev *input = msc->input;
217 int x, y, ts, ii, clicks;
218
219 switch (data[0]) {
220 case 0x10:
221 if (size != 6)
222 return 0;
223 x = (__s16)(data[2] | data[3] << 8);
224 y = (__s16)(data[4] | data[5] << 8);
225 clicks = data[1];
226 break;
227 case TOUCH_REPORT_ID:
228 /* Expect six bytes of prefix, and N*8 bytes of touch data. */
229 if (size < 6 || ((size - 6) % 8) != 0)
230 return 0;
231 ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
232 msc->delta_time = (ts - msc->last_timestamp) & 0x3ffff;
233 msc->last_timestamp = ts;
234 msc->ntouches = (size - 6) / 8;
235 for (ii = 0; ii < msc->ntouches; ii++)
236 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
237 /* When emulating three-button mode, it is important
238 * to have the current touch information before
239 * generating a click event.
240 */
241 x = (signed char)data[1];
242 y = (signed char)data[2];
243 clicks = data[3];
244 break;
245 case 0x20: /* Theoretically battery status (0-100), but I have
246 * never seen it -- maybe it is only upon request.
247 */
248 case 0x60: /* Unknown, maybe laser on/off. */
249 case 0x61: /* Laser reflection status change.
250 * data[1]: 0 = spotted, 1 = lost
251 */
252 default:
253 return 0;
254 }
255
256 magicmouse_emit_buttons(msc, clicks & 3);
257 input_report_rel(input, REL_X, x);
258 input_report_rel(input, REL_Y, y);
259 input_sync(input);
260 return 1;
261}
262
263static int magicmouse_input_open(struct input_dev *dev)
264{
265 struct hid_device *hid = input_get_drvdata(dev);
266
267 return hid->ll_driver->open(hid);
268}
269
270static void magicmouse_input_close(struct input_dev *dev)
271{
272 struct hid_device *hid = input_get_drvdata(dev);
273
274 hid->ll_driver->close(hid);
275}
276
277static void magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
278{
279 input_set_drvdata(input, hdev);
280 input->event = hdev->ll_driver->hidinput_input_event;
281 input->open = magicmouse_input_open;
282 input->close = magicmouse_input_close;
283
284 input->name = hdev->name;
285 input->phys = hdev->phys;
286 input->uniq = hdev->uniq;
287 input->id.bustype = hdev->bus;
288 input->id.vendor = hdev->vendor;
289 input->id.product = hdev->product;
290 input->id.version = hdev->version;
291 input->dev.parent = hdev->dev.parent;
292
Michael Poole71b38bd2010-02-11 00:32:57 -0500293 __set_bit(EV_KEY, input->evbit);
294 __set_bit(BTN_LEFT, input->keybit);
295 __set_bit(BTN_RIGHT, input->keybit);
Michael Poole128537c2010-02-06 12:24:36 -0500296 if (emulate_3button)
Michael Poole71b38bd2010-02-11 00:32:57 -0500297 __set_bit(BTN_MIDDLE, input->keybit);
298 __set_bit(BTN_TOOL_FINGER, input->keybit);
Michael Poole128537c2010-02-06 12:24:36 -0500299
Michael Poole71b38bd2010-02-11 00:32:57 -0500300 __set_bit(EV_REL, input->evbit);
301 __set_bit(REL_X, input->relbit);
302 __set_bit(REL_Y, input->relbit);
Michael Poole128537c2010-02-06 12:24:36 -0500303 if (emulate_scroll_wheel)
Michael Poole71b38bd2010-02-11 00:32:57 -0500304 __set_bit(REL_WHEEL, input->relbit);
Michael Poole128537c2010-02-06 12:24:36 -0500305
306 if (report_touches) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500307 __set_bit(EV_ABS, input->evbit);
Michael Poole128537c2010-02-06 12:24:36 -0500308
Michael Poole71b38bd2010-02-11 00:32:57 -0500309 input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 15, 0, 0);
310 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 4, 0);
311 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 4, 0);
312 input_set_abs_params(input, ABS_MT_ORIENTATION, -32, 31, 1, 0);
313 input_set_abs_params(input, ABS_MT_POSITION_X, -1100, 1358,
314 4, 0);
Michael Poole128537c2010-02-06 12:24:36 -0500315 /* Note: Touch Y position from the device is inverted relative
316 * to how pointer motion is reported (and relative to how USB
317 * HID recommends the coordinates work). This driver keeps
318 * the origin at the same position, and just uses the additive
319 * inverse of the reported Y.
320 */
Michael Poole71b38bd2010-02-11 00:32:57 -0500321 input_set_abs_params(input, ABS_MT_POSITION_Y, -1589, 2047,
322 4, 0);
Michael Poole128537c2010-02-06 12:24:36 -0500323 }
324
325 if (report_undeciphered) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500326 __set_bit(EV_MSC, input->evbit);
327 __set_bit(MSC_RAW, input->mscbit);
Michael Poole128537c2010-02-06 12:24:36 -0500328 }
329}
330
331static int magicmouse_probe(struct hid_device *hdev,
332 const struct hid_device_id *id)
333{
334 __u8 feature_1[] = { 0xd7, 0x01 };
335 __u8 feature_2[] = { 0xf8, 0x01, 0x32 };
336 struct input_dev *input;
337 struct magicmouse_sc *msc;
338 struct hid_report *report;
339 int ret;
340
341 msc = kzalloc(sizeof(*msc), GFP_KERNEL);
342 if (msc == NULL) {
343 dev_err(&hdev->dev, "can't alloc magicmouse descriptor\n");
344 return -ENOMEM;
345 }
346
347 msc->quirks = id->driver_data;
348 hid_set_drvdata(hdev, msc);
349
350 ret = hid_parse(hdev);
351 if (ret) {
352 dev_err(&hdev->dev, "magicmouse hid parse failed\n");
353 goto err_free;
354 }
355
Jiri Kosina23d02112010-05-12 16:01:26 +0200356 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Michael Poole128537c2010-02-06 12:24:36 -0500357 if (ret) {
358 dev_err(&hdev->dev, "magicmouse hw start failed\n");
359 goto err_free;
360 }
361
Jiri Kosina23d02112010-05-12 16:01:26 +0200362 /* we are handling the input ourselves */
363 hidinput_disconnect(hdev);
364
Michael Poole128537c2010-02-06 12:24:36 -0500365 report = hid_register_report(hdev, HID_INPUT_REPORT, TOUCH_REPORT_ID);
366 if (!report) {
367 dev_err(&hdev->dev, "unable to register touch report\n");
368 ret = -ENOMEM;
Michael Poole71b38bd2010-02-11 00:32:57 -0500369 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500370 }
371 report->size = 6;
372
373 ret = hdev->hid_output_raw_report(hdev, feature_1, sizeof(feature_1),
374 HID_FEATURE_REPORT);
375 if (ret != sizeof(feature_1)) {
376 dev_err(&hdev->dev, "unable to request touch data (1:%d)\n",
377 ret);
Michael Poole71b38bd2010-02-11 00:32:57 -0500378 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500379 }
380 ret = hdev->hid_output_raw_report(hdev, feature_2,
381 sizeof(feature_2), HID_FEATURE_REPORT);
382 if (ret != sizeof(feature_2)) {
383 dev_err(&hdev->dev, "unable to request touch data (2:%d)\n",
384 ret);
Michael Poole71b38bd2010-02-11 00:32:57 -0500385 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500386 }
387
388 input = input_allocate_device();
389 if (!input) {
390 dev_err(&hdev->dev, "can't alloc input device\n");
391 ret = -ENOMEM;
Michael Poole71b38bd2010-02-11 00:32:57 -0500392 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500393 }
394 magicmouse_setup_input(input, hdev);
395
396 ret = input_register_device(input);
397 if (ret) {
398 dev_err(&hdev->dev, "input device registration failed\n");
Michael Poole71b38bd2010-02-11 00:32:57 -0500399 goto err_input;
Michael Poole128537c2010-02-06 12:24:36 -0500400 }
401 msc->input = input;
402
403 return 0;
Michael Poole71b38bd2010-02-11 00:32:57 -0500404err_input:
Michael Poole128537c2010-02-06 12:24:36 -0500405 input_free_device(input);
Michael Poole71b38bd2010-02-11 00:32:57 -0500406err_stop_hw:
407 hid_hw_stop(hdev);
408err_free:
Michael Poole128537c2010-02-06 12:24:36 -0500409 kfree(msc);
410 return ret;
411}
412
413static void magicmouse_remove(struct hid_device *hdev)
414{
Michael Poole28918c22010-03-09 06:47:35 -0500415 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
416
Michael Poole128537c2010-02-06 12:24:36 -0500417 hid_hw_stop(hdev);
Michael Poole28918c22010-03-09 06:47:35 -0500418 input_unregister_device(msc->input);
419 kfree(msc);
Michael Poole128537c2010-02-06 12:24:36 -0500420}
421
422static const struct hid_device_id magic_mice[] = {
423 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE),
424 .driver_data = 0 },
425 { }
426};
427MODULE_DEVICE_TABLE(hid, magic_mice);
428
429static struct hid_driver magicmouse_driver = {
430 .name = "magicmouse",
431 .id_table = magic_mice,
432 .probe = magicmouse_probe,
433 .remove = magicmouse_remove,
434 .raw_event = magicmouse_raw_event,
435};
436
437static int __init magicmouse_init(void)
438{
439 int ret;
440
441 ret = hid_register_driver(&magicmouse_driver);
442 if (ret)
443 printk(KERN_ERR "can't register magicmouse driver\n");
444
445 return ret;
446}
447
448static void __exit magicmouse_exit(void)
449{
450 hid_unregister_driver(&magicmouse_driver);
451}
452
453module_init(magicmouse_init);
454module_exit(magicmouse_exit);
455MODULE_LICENSE("GPL");