1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Apple "Magic" Wireless Mouse driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2010 Michael Poole <mdpoole@troilus.org>
6*4882a593Smuzhiyun * Copyright (c) 2010 Chase Douglas <chase.douglas@canonical.com>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/device.h>
15*4882a593Smuzhiyun #include <linux/hid.h>
16*4882a593Smuzhiyun #include <linux/input/mt.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "hid-ids.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun static bool emulate_3button = true;
23*4882a593Smuzhiyun module_param(emulate_3button, bool, 0644);
24*4882a593Smuzhiyun MODULE_PARM_DESC(emulate_3button, "Emulate a middle button");
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun static int middle_button_start = -350;
27*4882a593Smuzhiyun static int middle_button_stop = +350;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun static bool emulate_scroll_wheel = true;
30*4882a593Smuzhiyun module_param(emulate_scroll_wheel, bool, 0644);
31*4882a593Smuzhiyun MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun static unsigned int scroll_speed = 32;
param_set_scroll_speed(const char * val,const struct kernel_param * kp)34*4882a593Smuzhiyun static int param_set_scroll_speed(const char *val,
35*4882a593Smuzhiyun const struct kernel_param *kp) {
36*4882a593Smuzhiyun unsigned long speed;
37*4882a593Smuzhiyun if (!val || kstrtoul(val, 0, &speed) || speed > 63)
38*4882a593Smuzhiyun return -EINVAL;
39*4882a593Smuzhiyun scroll_speed = speed;
40*4882a593Smuzhiyun return 0;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644);
43*4882a593Smuzhiyun MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)");
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun static bool scroll_acceleration = false;
46*4882a593Smuzhiyun module_param(scroll_acceleration, bool, 0644);
47*4882a593Smuzhiyun MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun static bool report_undeciphered;
50*4882a593Smuzhiyun module_param(report_undeciphered, bool, 0644);
51*4882a593Smuzhiyun MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #define TRACKPAD_REPORT_ID 0x28
54*4882a593Smuzhiyun #define TRACKPAD2_USB_REPORT_ID 0x02
55*4882a593Smuzhiyun #define TRACKPAD2_BT_REPORT_ID 0x31
56*4882a593Smuzhiyun #define MOUSE_REPORT_ID 0x29
57*4882a593Smuzhiyun #define DOUBLE_REPORT_ID 0xf7
58*4882a593Smuzhiyun /* These definitions are not precise, but they're close enough. (Bits
59*4882a593Smuzhiyun * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
60*4882a593Smuzhiyun * to be some kind of bit mask -- 0x20 may be a near-field reading,
61*4882a593Smuzhiyun * and 0x40 is actual contact, and 0x10 may be a start/stop or change
62*4882a593Smuzhiyun * indication.)
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun #define TOUCH_STATE_MASK 0xf0
65*4882a593Smuzhiyun #define TOUCH_STATE_NONE 0x00
66*4882a593Smuzhiyun #define TOUCH_STATE_START 0x30
67*4882a593Smuzhiyun #define TOUCH_STATE_DRAG 0x40
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define SCROLL_ACCEL_DEFAULT 7
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* Touch surface information. Dimension is in hundredths of a mm, min and max
72*4882a593Smuzhiyun * are in units. */
73*4882a593Smuzhiyun #define MOUSE_DIMENSION_X (float)9056
74*4882a593Smuzhiyun #define MOUSE_MIN_X -1100
75*4882a593Smuzhiyun #define MOUSE_MAX_X 1258
76*4882a593Smuzhiyun #define MOUSE_RES_X ((MOUSE_MAX_X - MOUSE_MIN_X) / (MOUSE_DIMENSION_X / 100))
77*4882a593Smuzhiyun #define MOUSE_DIMENSION_Y (float)5152
78*4882a593Smuzhiyun #define MOUSE_MIN_Y -1589
79*4882a593Smuzhiyun #define MOUSE_MAX_Y 2047
80*4882a593Smuzhiyun #define MOUSE_RES_Y ((MOUSE_MAX_Y - MOUSE_MIN_Y) / (MOUSE_DIMENSION_Y / 100))
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun #define TRACKPAD_DIMENSION_X (float)13000
83*4882a593Smuzhiyun #define TRACKPAD_MIN_X -2909
84*4882a593Smuzhiyun #define TRACKPAD_MAX_X 3167
85*4882a593Smuzhiyun #define TRACKPAD_RES_X \
86*4882a593Smuzhiyun ((TRACKPAD_MAX_X - TRACKPAD_MIN_X) / (TRACKPAD_DIMENSION_X / 100))
87*4882a593Smuzhiyun #define TRACKPAD_DIMENSION_Y (float)11000
88*4882a593Smuzhiyun #define TRACKPAD_MIN_Y -2456
89*4882a593Smuzhiyun #define TRACKPAD_MAX_Y 2565
90*4882a593Smuzhiyun #define TRACKPAD_RES_Y \
91*4882a593Smuzhiyun ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun #define TRACKPAD2_DIMENSION_X (float)16000
94*4882a593Smuzhiyun #define TRACKPAD2_MIN_X -3678
95*4882a593Smuzhiyun #define TRACKPAD2_MAX_X 3934
96*4882a593Smuzhiyun #define TRACKPAD2_RES_X \
97*4882a593Smuzhiyun ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
98*4882a593Smuzhiyun #define TRACKPAD2_DIMENSION_Y (float)11490
99*4882a593Smuzhiyun #define TRACKPAD2_MIN_Y -2478
100*4882a593Smuzhiyun #define TRACKPAD2_MAX_Y 2587
101*4882a593Smuzhiyun #define TRACKPAD2_RES_Y \
102*4882a593Smuzhiyun ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun * struct magicmouse_sc - Tracks Magic Mouse-specific data.
106*4882a593Smuzhiyun * @input: Input device through which we report events.
107*4882a593Smuzhiyun * @quirks: Currently unused.
108*4882a593Smuzhiyun * @ntouches: Number of touches in most recent touch report.
109*4882a593Smuzhiyun * @scroll_accel: Number of consecutive scroll motions.
110*4882a593Smuzhiyun * @scroll_jiffies: Time of last scroll motion.
111*4882a593Smuzhiyun * @touches: Most recent data for a touch, indexed by tracking ID.
112*4882a593Smuzhiyun * @tracking_ids: Mapping of current touch input data to @touches.
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun struct magicmouse_sc {
115*4882a593Smuzhiyun struct input_dev *input;
116*4882a593Smuzhiyun unsigned long quirks;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun int ntouches;
119*4882a593Smuzhiyun int scroll_accel;
120*4882a593Smuzhiyun unsigned long scroll_jiffies;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun struct {
123*4882a593Smuzhiyun short x;
124*4882a593Smuzhiyun short y;
125*4882a593Smuzhiyun short scroll_x;
126*4882a593Smuzhiyun short scroll_y;
127*4882a593Smuzhiyun u8 size;
128*4882a593Smuzhiyun } touches[16];
129*4882a593Smuzhiyun int tracking_ids[16];
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun
magicmouse_firm_touch(struct magicmouse_sc * msc)132*4882a593Smuzhiyun static int magicmouse_firm_touch(struct magicmouse_sc *msc)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun int touch = -1;
135*4882a593Smuzhiyun int ii;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /* If there is only one "firm" touch, set touch to its
138*4882a593Smuzhiyun * tracking ID.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun for (ii = 0; ii < msc->ntouches; ii++) {
141*4882a593Smuzhiyun int idx = msc->tracking_ids[ii];
142*4882a593Smuzhiyun if (msc->touches[idx].size < 8) {
143*4882a593Smuzhiyun /* Ignore this touch. */
144*4882a593Smuzhiyun } else if (touch >= 0) {
145*4882a593Smuzhiyun touch = -1;
146*4882a593Smuzhiyun break;
147*4882a593Smuzhiyun } else {
148*4882a593Smuzhiyun touch = idx;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun return touch;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
magicmouse_emit_buttons(struct magicmouse_sc * msc,int state)155*4882a593Smuzhiyun static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
158*4882a593Smuzhiyun test_bit(BTN_RIGHT, msc->input->key) << 1 |
159*4882a593Smuzhiyun test_bit(BTN_MIDDLE, msc->input->key) << 2;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun if (emulate_3button) {
162*4882a593Smuzhiyun int id;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* If some button was pressed before, keep it held
165*4882a593Smuzhiyun * down. Otherwise, if there's exactly one firm
166*4882a593Smuzhiyun * touch, use that to override the mouse's guess.
167*4882a593Smuzhiyun */
168*4882a593Smuzhiyun if (state == 0) {
169*4882a593Smuzhiyun /* The button was released. */
170*4882a593Smuzhiyun } else if (last_state != 0) {
171*4882a593Smuzhiyun state = last_state;
172*4882a593Smuzhiyun } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
173*4882a593Smuzhiyun int x = msc->touches[id].x;
174*4882a593Smuzhiyun if (x < middle_button_start)
175*4882a593Smuzhiyun state = 1;
176*4882a593Smuzhiyun else if (x > middle_button_stop)
177*4882a593Smuzhiyun state = 2;
178*4882a593Smuzhiyun else
179*4882a593Smuzhiyun state = 4;
180*4882a593Smuzhiyun } /* else: we keep the mouse's guess */
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun input_report_key(msc->input, BTN_MIDDLE, state & 4);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun input_report_key(msc->input, BTN_LEFT, state & 1);
186*4882a593Smuzhiyun input_report_key(msc->input, BTN_RIGHT, state & 2);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun if (state != last_state)
189*4882a593Smuzhiyun msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
magicmouse_emit_touch(struct magicmouse_sc * msc,int raw_id,u8 * tdata)192*4882a593Smuzhiyun static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun struct input_dev *input = msc->input;
195*4882a593Smuzhiyun int id, x, y, size, orientation, touch_major, touch_minor, state, down;
196*4882a593Smuzhiyun int pressure = 0;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
199*4882a593Smuzhiyun id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
200*4882a593Smuzhiyun x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
201*4882a593Smuzhiyun y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
202*4882a593Smuzhiyun size = tdata[5] & 0x3f;
203*4882a593Smuzhiyun orientation = (tdata[6] >> 2) - 32;
204*4882a593Smuzhiyun touch_major = tdata[3];
205*4882a593Smuzhiyun touch_minor = tdata[4];
206*4882a593Smuzhiyun state = tdata[7] & TOUCH_STATE_MASK;
207*4882a593Smuzhiyun down = state != TOUCH_STATE_NONE;
208*4882a593Smuzhiyun } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
209*4882a593Smuzhiyun id = tdata[8] & 0xf;
210*4882a593Smuzhiyun x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
211*4882a593Smuzhiyun y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
212*4882a593Smuzhiyun size = tdata[6];
213*4882a593Smuzhiyun orientation = (tdata[8] >> 5) - 4;
214*4882a593Smuzhiyun touch_major = tdata[4];
215*4882a593Smuzhiyun touch_minor = tdata[5];
216*4882a593Smuzhiyun pressure = tdata[7];
217*4882a593Smuzhiyun state = tdata[3] & 0xC0;
218*4882a593Smuzhiyun down = state == 0x80;
219*4882a593Smuzhiyun } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
220*4882a593Smuzhiyun id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
221*4882a593Smuzhiyun x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
222*4882a593Smuzhiyun y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
223*4882a593Smuzhiyun size = tdata[6] & 0x3f;
224*4882a593Smuzhiyun orientation = (tdata[7] >> 2) - 32;
225*4882a593Smuzhiyun touch_major = tdata[4];
226*4882a593Smuzhiyun touch_minor = tdata[5];
227*4882a593Smuzhiyun state = tdata[8] & TOUCH_STATE_MASK;
228*4882a593Smuzhiyun down = state != TOUCH_STATE_NONE;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* Store tracking ID and other fields. */
232*4882a593Smuzhiyun msc->tracking_ids[raw_id] = id;
233*4882a593Smuzhiyun msc->touches[id].x = x;
234*4882a593Smuzhiyun msc->touches[id].y = y;
235*4882a593Smuzhiyun msc->touches[id].size = size;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /* If requested, emulate a scroll wheel by detecting small
238*4882a593Smuzhiyun * vertical touch motions.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun if (emulate_scroll_wheel && (input->id.product !=
241*4882a593Smuzhiyun USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) {
242*4882a593Smuzhiyun unsigned long now = jiffies;
243*4882a593Smuzhiyun int step_x = msc->touches[id].scroll_x - x;
244*4882a593Smuzhiyun int step_y = msc->touches[id].scroll_y - y;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /* Calculate and apply the scroll motion. */
247*4882a593Smuzhiyun switch (state) {
248*4882a593Smuzhiyun case TOUCH_STATE_START:
249*4882a593Smuzhiyun msc->touches[id].scroll_x = x;
250*4882a593Smuzhiyun msc->touches[id].scroll_y = y;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* Reset acceleration after half a second. */
253*4882a593Smuzhiyun if (scroll_acceleration && time_before(now,
254*4882a593Smuzhiyun msc->scroll_jiffies + HZ / 2))
255*4882a593Smuzhiyun msc->scroll_accel = max_t(int,
256*4882a593Smuzhiyun msc->scroll_accel - 1, 1);
257*4882a593Smuzhiyun else
258*4882a593Smuzhiyun msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun break;
261*4882a593Smuzhiyun case TOUCH_STATE_DRAG:
262*4882a593Smuzhiyun step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
263*4882a593Smuzhiyun if (step_x != 0) {
264*4882a593Smuzhiyun msc->touches[id].scroll_x -= step_x *
265*4882a593Smuzhiyun (64 - scroll_speed) * msc->scroll_accel;
266*4882a593Smuzhiyun msc->scroll_jiffies = now;
267*4882a593Smuzhiyun input_report_rel(input, REL_HWHEEL, -step_x);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
271*4882a593Smuzhiyun if (step_y != 0) {
272*4882a593Smuzhiyun msc->touches[id].scroll_y -= step_y *
273*4882a593Smuzhiyun (64 - scroll_speed) * msc->scroll_accel;
274*4882a593Smuzhiyun msc->scroll_jiffies = now;
275*4882a593Smuzhiyun input_report_rel(input, REL_WHEEL, step_y);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun break;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun if (down)
282*4882a593Smuzhiyun msc->ntouches++;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun input_mt_slot(input, id);
285*4882a593Smuzhiyun input_mt_report_slot_state(input, MT_TOOL_FINGER, down);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* Generate the input events for this touch. */
288*4882a593Smuzhiyun if (down) {
289*4882a593Smuzhiyun input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major << 2);
290*4882a593Smuzhiyun input_report_abs(input, ABS_MT_TOUCH_MINOR, touch_minor << 2);
291*4882a593Smuzhiyun input_report_abs(input, ABS_MT_ORIENTATION, -orientation);
292*4882a593Smuzhiyun input_report_abs(input, ABS_MT_POSITION_X, x);
293*4882a593Smuzhiyun input_report_abs(input, ABS_MT_POSITION_Y, y);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
296*4882a593Smuzhiyun input_report_abs(input, ABS_MT_PRESSURE, pressure);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun if (report_undeciphered) {
299*4882a593Smuzhiyun if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
300*4882a593Smuzhiyun input_event(input, EV_MSC, MSC_RAW, tdata[7]);
301*4882a593Smuzhiyun else if (input->id.product !=
302*4882a593Smuzhiyun USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
303*4882a593Smuzhiyun input_event(input, EV_MSC, MSC_RAW, tdata[8]);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
magicmouse_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)308*4882a593Smuzhiyun static int magicmouse_raw_event(struct hid_device *hdev,
309*4882a593Smuzhiyun struct hid_report *report, u8 *data, int size)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun struct magicmouse_sc *msc = hid_get_drvdata(hdev);
312*4882a593Smuzhiyun struct input_dev *input = msc->input;
313*4882a593Smuzhiyun int x = 0, y = 0, ii, clicks = 0, npoints;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun switch (data[0]) {
316*4882a593Smuzhiyun case TRACKPAD_REPORT_ID:
317*4882a593Smuzhiyun case TRACKPAD2_BT_REPORT_ID:
318*4882a593Smuzhiyun /* Expect four bytes of prefix, and N*9 bytes of touch data. */
319*4882a593Smuzhiyun if (size < 4 || ((size - 4) % 9) != 0)
320*4882a593Smuzhiyun return 0;
321*4882a593Smuzhiyun npoints = (size - 4) / 9;
322*4882a593Smuzhiyun if (npoints > 15) {
323*4882a593Smuzhiyun hid_warn(hdev, "invalid size value (%d) for TRACKPAD_REPORT_ID\n",
324*4882a593Smuzhiyun size);
325*4882a593Smuzhiyun return 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun msc->ntouches = 0;
328*4882a593Smuzhiyun for (ii = 0; ii < npoints; ii++)
329*4882a593Smuzhiyun magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun clicks = data[1];
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /* The following bits provide a device specific timestamp. They
334*4882a593Smuzhiyun * are unused here.
335*4882a593Smuzhiyun *
336*4882a593Smuzhiyun * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
337*4882a593Smuzhiyun */
338*4882a593Smuzhiyun break;
339*4882a593Smuzhiyun case TRACKPAD2_USB_REPORT_ID:
340*4882a593Smuzhiyun /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
341*4882a593Smuzhiyun if (size < 12 || ((size - 12) % 9) != 0)
342*4882a593Smuzhiyun return 0;
343*4882a593Smuzhiyun npoints = (size - 12) / 9;
344*4882a593Smuzhiyun if (npoints > 15) {
345*4882a593Smuzhiyun hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
346*4882a593Smuzhiyun size);
347*4882a593Smuzhiyun return 0;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun msc->ntouches = 0;
350*4882a593Smuzhiyun for (ii = 0; ii < npoints; ii++)
351*4882a593Smuzhiyun magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun clicks = data[1];
354*4882a593Smuzhiyun break;
355*4882a593Smuzhiyun case MOUSE_REPORT_ID:
356*4882a593Smuzhiyun /* Expect six bytes of prefix, and N*8 bytes of touch data. */
357*4882a593Smuzhiyun if (size < 6 || ((size - 6) % 8) != 0)
358*4882a593Smuzhiyun return 0;
359*4882a593Smuzhiyun npoints = (size - 6) / 8;
360*4882a593Smuzhiyun if (npoints > 15) {
361*4882a593Smuzhiyun hid_warn(hdev, "invalid size value (%d) for MOUSE_REPORT_ID\n",
362*4882a593Smuzhiyun size);
363*4882a593Smuzhiyun return 0;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun msc->ntouches = 0;
366*4882a593Smuzhiyun for (ii = 0; ii < npoints; ii++)
367*4882a593Smuzhiyun magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /* When emulating three-button mode, it is important
370*4882a593Smuzhiyun * to have the current touch information before
371*4882a593Smuzhiyun * generating a click event.
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
374*4882a593Smuzhiyun y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
375*4882a593Smuzhiyun clicks = data[3];
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun /* The following bits provide a device specific timestamp. They
378*4882a593Smuzhiyun * are unused here.
379*4882a593Smuzhiyun *
380*4882a593Smuzhiyun * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
381*4882a593Smuzhiyun */
382*4882a593Smuzhiyun break;
383*4882a593Smuzhiyun case DOUBLE_REPORT_ID:
384*4882a593Smuzhiyun /* Sometimes the trackpad sends two touch reports in one
385*4882a593Smuzhiyun * packet.
386*4882a593Smuzhiyun */
387*4882a593Smuzhiyun magicmouse_raw_event(hdev, report, data + 2, data[1]);
388*4882a593Smuzhiyun magicmouse_raw_event(hdev, report, data + 2 + data[1],
389*4882a593Smuzhiyun size - 2 - data[1]);
390*4882a593Smuzhiyun return 0;
391*4882a593Smuzhiyun default:
392*4882a593Smuzhiyun return 0;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
396*4882a593Smuzhiyun magicmouse_emit_buttons(msc, clicks & 3);
397*4882a593Smuzhiyun input_report_rel(input, REL_X, x);
398*4882a593Smuzhiyun input_report_rel(input, REL_Y, y);
399*4882a593Smuzhiyun } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
400*4882a593Smuzhiyun input_mt_sync_frame(input);
401*4882a593Smuzhiyun input_report_key(input, BTN_MOUSE, clicks & 1);
402*4882a593Smuzhiyun } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
403*4882a593Smuzhiyun input_report_key(input, BTN_MOUSE, clicks & 1);
404*4882a593Smuzhiyun input_mt_report_pointer_emulation(input, true);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun input_sync(input);
408*4882a593Smuzhiyun return 1;
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun
magicmouse_setup_input(struct input_dev * input,struct hid_device * hdev)411*4882a593Smuzhiyun static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun int error;
414*4882a593Smuzhiyun int mt_flags = 0;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun __set_bit(EV_KEY, input->evbit);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
419*4882a593Smuzhiyun __set_bit(BTN_LEFT, input->keybit);
420*4882a593Smuzhiyun __set_bit(BTN_RIGHT, input->keybit);
421*4882a593Smuzhiyun if (emulate_3button)
422*4882a593Smuzhiyun __set_bit(BTN_MIDDLE, input->keybit);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun __set_bit(EV_REL, input->evbit);
425*4882a593Smuzhiyun __set_bit(REL_X, input->relbit);
426*4882a593Smuzhiyun __set_bit(REL_Y, input->relbit);
427*4882a593Smuzhiyun if (emulate_scroll_wheel) {
428*4882a593Smuzhiyun __set_bit(REL_WHEEL, input->relbit);
429*4882a593Smuzhiyun __set_bit(REL_HWHEEL, input->relbit);
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
432*4882a593Smuzhiyun /* setting the device name to ensure the same driver settings
433*4882a593Smuzhiyun * get loaded, whether connected through bluetooth or USB
434*4882a593Smuzhiyun */
435*4882a593Smuzhiyun input->name = "Apple Inc. Magic Trackpad 2";
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun __clear_bit(EV_MSC, input->evbit);
438*4882a593Smuzhiyun __clear_bit(BTN_0, input->keybit);
439*4882a593Smuzhiyun __clear_bit(BTN_RIGHT, input->keybit);
440*4882a593Smuzhiyun __clear_bit(BTN_MIDDLE, input->keybit);
441*4882a593Smuzhiyun __set_bit(BTN_MOUSE, input->keybit);
442*4882a593Smuzhiyun __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
443*4882a593Smuzhiyun __set_bit(BTN_TOOL_FINGER, input->keybit);
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
446*4882a593Smuzhiyun INPUT_MT_TRACK;
447*4882a593Smuzhiyun } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
448*4882a593Smuzhiyun /* input->keybit is initialized with incorrect button info
449*4882a593Smuzhiyun * for Magic Trackpad. There really is only one physical
450*4882a593Smuzhiyun * button (BTN_LEFT == BTN_MOUSE). Make sure we don't
451*4882a593Smuzhiyun * advertise buttons that don't exist...
452*4882a593Smuzhiyun */
453*4882a593Smuzhiyun __clear_bit(BTN_RIGHT, input->keybit);
454*4882a593Smuzhiyun __clear_bit(BTN_MIDDLE, input->keybit);
455*4882a593Smuzhiyun __set_bit(BTN_MOUSE, input->keybit);
456*4882a593Smuzhiyun __set_bit(BTN_TOOL_FINGER, input->keybit);
457*4882a593Smuzhiyun __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
458*4882a593Smuzhiyun __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
459*4882a593Smuzhiyun __set_bit(BTN_TOOL_QUADTAP, input->keybit);
460*4882a593Smuzhiyun __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
461*4882a593Smuzhiyun __set_bit(BTN_TOUCH, input->keybit);
462*4882a593Smuzhiyun __set_bit(INPUT_PROP_POINTER, input->propbit);
463*4882a593Smuzhiyun __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun __set_bit(EV_ABS, input->evbit);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun error = input_mt_init_slots(input, 16, mt_flags);
470*4882a593Smuzhiyun if (error)
471*4882a593Smuzhiyun return error;
472*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
473*4882a593Smuzhiyun 4, 0);
474*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
475*4882a593Smuzhiyun 4, 0);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /* Note: Touch Y position from the device is inverted relative
478*4882a593Smuzhiyun * to how pointer motion is reported (and relative to how USB
479*4882a593Smuzhiyun * HID recommends the coordinates work). This driver keeps
480*4882a593Smuzhiyun * the origin at the same position, and just uses the additive
481*4882a593Smuzhiyun * inverse of the reported Y.
482*4882a593Smuzhiyun */
483*4882a593Smuzhiyun if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
484*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
485*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_POSITION_X,
486*4882a593Smuzhiyun MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
487*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_POSITION_Y,
488*4882a593Smuzhiyun MOUSE_MIN_Y, MOUSE_MAX_Y, 4, 0);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun input_abs_set_res(input, ABS_MT_POSITION_X,
491*4882a593Smuzhiyun MOUSE_RES_X);
492*4882a593Smuzhiyun input_abs_set_res(input, ABS_MT_POSITION_Y,
493*4882a593Smuzhiyun MOUSE_RES_Y);
494*4882a593Smuzhiyun } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
495*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
496*4882a593Smuzhiyun input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
497*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
498*4882a593Smuzhiyun input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
499*4882a593Smuzhiyun TRACKPAD2_MAX_X, 0, 0);
500*4882a593Smuzhiyun input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
501*4882a593Smuzhiyun TRACKPAD2_MAX_Y, 0, 0);
502*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_POSITION_X,
503*4882a593Smuzhiyun TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
504*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_POSITION_Y,
505*4882a593Smuzhiyun TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
508*4882a593Smuzhiyun input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
509*4882a593Smuzhiyun input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
510*4882a593Smuzhiyun input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
511*4882a593Smuzhiyun } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
512*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
513*4882a593Smuzhiyun input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
514*4882a593Smuzhiyun TRACKPAD_MAX_X, 4, 0);
515*4882a593Smuzhiyun input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
516*4882a593Smuzhiyun TRACKPAD_MAX_Y, 4, 0);
517*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_POSITION_X,
518*4882a593Smuzhiyun TRACKPAD_MIN_X, TRACKPAD_MAX_X, 4, 0);
519*4882a593Smuzhiyun input_set_abs_params(input, ABS_MT_POSITION_Y,
520*4882a593Smuzhiyun TRACKPAD_MIN_Y, TRACKPAD_MAX_Y, 4, 0);
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun input_abs_set_res(input, ABS_X, TRACKPAD_RES_X);
523*4882a593Smuzhiyun input_abs_set_res(input, ABS_Y, TRACKPAD_RES_Y);
524*4882a593Smuzhiyun input_abs_set_res(input, ABS_MT_POSITION_X,
525*4882a593Smuzhiyun TRACKPAD_RES_X);
526*4882a593Smuzhiyun input_abs_set_res(input, ABS_MT_POSITION_Y,
527*4882a593Smuzhiyun TRACKPAD_RES_Y);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun input_set_events_per_packet(input, 60);
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun if (report_undeciphered &&
533*4882a593Smuzhiyun input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
534*4882a593Smuzhiyun __set_bit(EV_MSC, input->evbit);
535*4882a593Smuzhiyun __set_bit(MSC_RAW, input->mscbit);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun /*
539*4882a593Smuzhiyun * hid-input may mark device as using autorepeat, but neither
540*4882a593Smuzhiyun * the trackpad, nor the mouse actually want it.
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun __clear_bit(EV_REP, input->evbit);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun return 0;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
magicmouse_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)547*4882a593Smuzhiyun static int magicmouse_input_mapping(struct hid_device *hdev,
548*4882a593Smuzhiyun struct hid_input *hi, struct hid_field *field,
549*4882a593Smuzhiyun struct hid_usage *usage, unsigned long **bit, int *max)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun struct magicmouse_sc *msc = hid_get_drvdata(hdev);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun if (!msc->input)
554*4882a593Smuzhiyun msc->input = hi->input;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun /* Magic Trackpad does not give relative data after switching to MT */
557*4882a593Smuzhiyun if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
558*4882a593Smuzhiyun hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
559*4882a593Smuzhiyun field->flags & HID_MAIN_ITEM_RELATIVE)
560*4882a593Smuzhiyun return -1;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun return 0;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
magicmouse_input_configured(struct hid_device * hdev,struct hid_input * hi)565*4882a593Smuzhiyun static int magicmouse_input_configured(struct hid_device *hdev,
566*4882a593Smuzhiyun struct hid_input *hi)
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun {
569*4882a593Smuzhiyun struct magicmouse_sc *msc = hid_get_drvdata(hdev);
570*4882a593Smuzhiyun int ret;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun ret = magicmouse_setup_input(msc->input, hdev);
573*4882a593Smuzhiyun if (ret) {
574*4882a593Smuzhiyun hid_err(hdev, "magicmouse setup input failed (%d)\n", ret);
575*4882a593Smuzhiyun /* clean msc->input to notify probe() of the failure */
576*4882a593Smuzhiyun msc->input = NULL;
577*4882a593Smuzhiyun return ret;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun return 0;
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun
magicmouse_probe(struct hid_device * hdev,const struct hid_device_id * id)584*4882a593Smuzhiyun static int magicmouse_probe(struct hid_device *hdev,
585*4882a593Smuzhiyun const struct hid_device_id *id)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun const u8 *feature;
588*4882a593Smuzhiyun const u8 feature_mt[] = { 0xD7, 0x01 };
589*4882a593Smuzhiyun const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
590*4882a593Smuzhiyun const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
591*4882a593Smuzhiyun u8 *buf;
592*4882a593Smuzhiyun struct magicmouse_sc *msc;
593*4882a593Smuzhiyun struct hid_report *report;
594*4882a593Smuzhiyun int ret;
595*4882a593Smuzhiyun int feature_size;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun if (id->vendor == USB_VENDOR_ID_APPLE &&
598*4882a593Smuzhiyun id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
599*4882a593Smuzhiyun hdev->type != HID_TYPE_USBMOUSE)
600*4882a593Smuzhiyun return -ENODEV;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
603*4882a593Smuzhiyun if (msc == NULL) {
604*4882a593Smuzhiyun hid_err(hdev, "can't alloc magicmouse descriptor\n");
605*4882a593Smuzhiyun return -ENOMEM;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun msc->quirks = id->driver_data;
611*4882a593Smuzhiyun hid_set_drvdata(hdev, msc);
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun ret = hid_parse(hdev);
614*4882a593Smuzhiyun if (ret) {
615*4882a593Smuzhiyun hid_err(hdev, "magicmouse hid parse failed\n");
616*4882a593Smuzhiyun return ret;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
620*4882a593Smuzhiyun if (ret) {
621*4882a593Smuzhiyun hid_err(hdev, "magicmouse hw start failed\n");
622*4882a593Smuzhiyun return ret;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun if (!msc->input) {
626*4882a593Smuzhiyun hid_err(hdev, "magicmouse input not registered\n");
627*4882a593Smuzhiyun ret = -ENOMEM;
628*4882a593Smuzhiyun goto err_stop_hw;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
632*4882a593Smuzhiyun report = hid_register_report(hdev, HID_INPUT_REPORT,
633*4882a593Smuzhiyun MOUSE_REPORT_ID, 0);
634*4882a593Smuzhiyun else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
635*4882a593Smuzhiyun if (id->vendor == BT_VENDOR_ID_APPLE)
636*4882a593Smuzhiyun report = hid_register_report(hdev, HID_INPUT_REPORT,
637*4882a593Smuzhiyun TRACKPAD2_BT_REPORT_ID, 0);
638*4882a593Smuzhiyun else /* USB_VENDOR_ID_APPLE */
639*4882a593Smuzhiyun report = hid_register_report(hdev, HID_INPUT_REPORT,
640*4882a593Smuzhiyun TRACKPAD2_USB_REPORT_ID, 0);
641*4882a593Smuzhiyun } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
642*4882a593Smuzhiyun report = hid_register_report(hdev, HID_INPUT_REPORT,
643*4882a593Smuzhiyun TRACKPAD_REPORT_ID, 0);
644*4882a593Smuzhiyun report = hid_register_report(hdev, HID_INPUT_REPORT,
645*4882a593Smuzhiyun DOUBLE_REPORT_ID, 0);
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun if (!report) {
649*4882a593Smuzhiyun hid_err(hdev, "unable to register touch report\n");
650*4882a593Smuzhiyun ret = -ENOMEM;
651*4882a593Smuzhiyun goto err_stop_hw;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun report->size = 6;
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
656*4882a593Smuzhiyun if (id->vendor == BT_VENDOR_ID_APPLE) {
657*4882a593Smuzhiyun feature_size = sizeof(feature_mt_trackpad2_bt);
658*4882a593Smuzhiyun feature = feature_mt_trackpad2_bt;
659*4882a593Smuzhiyun } else { /* USB_VENDOR_ID_APPLE */
660*4882a593Smuzhiyun feature_size = sizeof(feature_mt_trackpad2_usb);
661*4882a593Smuzhiyun feature = feature_mt_trackpad2_usb;
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun } else {
664*4882a593Smuzhiyun feature_size = sizeof(feature_mt);
665*4882a593Smuzhiyun feature = feature_mt;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun buf = kmemdup(feature, feature_size, GFP_KERNEL);
669*4882a593Smuzhiyun if (!buf) {
670*4882a593Smuzhiyun ret = -ENOMEM;
671*4882a593Smuzhiyun goto err_stop_hw;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /*
675*4882a593Smuzhiyun * Some devices repond with 'invalid report id' when feature
676*4882a593Smuzhiyun * report switching it into multitouch mode is sent to it.
677*4882a593Smuzhiyun *
678*4882a593Smuzhiyun * This results in -EIO from the _raw low-level transport callback,
679*4882a593Smuzhiyun * but there seems to be no other way of switching the mode.
680*4882a593Smuzhiyun * Thus the super-ugly hacky success check below.
681*4882a593Smuzhiyun */
682*4882a593Smuzhiyun ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
683*4882a593Smuzhiyun HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
684*4882a593Smuzhiyun kfree(buf);
685*4882a593Smuzhiyun if (ret != -EIO && ret != feature_size) {
686*4882a593Smuzhiyun hid_err(hdev, "unable to request touch data (%d)\n", ret);
687*4882a593Smuzhiyun goto err_stop_hw;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun return 0;
691*4882a593Smuzhiyun err_stop_hw:
692*4882a593Smuzhiyun hid_hw_stop(hdev);
693*4882a593Smuzhiyun return ret;
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun static const struct hid_device_id magic_mice[] = {
697*4882a593Smuzhiyun { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
698*4882a593Smuzhiyun USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
699*4882a593Smuzhiyun { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
700*4882a593Smuzhiyun USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
701*4882a593Smuzhiyun { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
702*4882a593Smuzhiyun USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
703*4882a593Smuzhiyun { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
704*4882a593Smuzhiyun USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
705*4882a593Smuzhiyun { }
706*4882a593Smuzhiyun };
707*4882a593Smuzhiyun MODULE_DEVICE_TABLE(hid, magic_mice);
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun static struct hid_driver magicmouse_driver = {
710*4882a593Smuzhiyun .name = "magicmouse",
711*4882a593Smuzhiyun .id_table = magic_mice,
712*4882a593Smuzhiyun .probe = magicmouse_probe,
713*4882a593Smuzhiyun .raw_event = magicmouse_raw_event,
714*4882a593Smuzhiyun .input_mapping = magicmouse_input_mapping,
715*4882a593Smuzhiyun .input_configured = magicmouse_input_configured,
716*4882a593Smuzhiyun };
717*4882a593Smuzhiyun module_hid_driver(magicmouse_driver);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun MODULE_LICENSE("GPL");
720