Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   Apple "Magic" Wireless Mouse driver
0004  *
0005  *   Copyright (c) 2010 Michael Poole <mdpoole@troilus.org>
0006  *   Copyright (c) 2010 Chase Douglas <chase.douglas@canonical.com>
0007  */
0008 
0009 /*
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/device.h>
0015 #include <linux/hid.h>
0016 #include <linux/input/mt.h>
0017 #include <linux/module.h>
0018 #include <linux/slab.h>
0019 #include <linux/workqueue.h>
0020 
0021 #include "hid-ids.h"
0022 
0023 static bool emulate_3button = true;
0024 module_param(emulate_3button, bool, 0644);
0025 MODULE_PARM_DESC(emulate_3button, "Emulate a middle button");
0026 
0027 static int middle_button_start = -350;
0028 static int middle_button_stop = +350;
0029 
0030 static bool emulate_scroll_wheel = true;
0031 module_param(emulate_scroll_wheel, bool, 0644);
0032 MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
0033 
0034 static unsigned int scroll_speed = 32;
0035 static int param_set_scroll_speed(const char *val,
0036                   const struct kernel_param *kp) {
0037     unsigned long speed;
0038     if (!val || kstrtoul(val, 0, &speed) || speed > 63)
0039         return -EINVAL;
0040     scroll_speed = speed;
0041     return 0;
0042 }
0043 module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644);
0044 MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)");
0045 
0046 static bool scroll_acceleration = false;
0047 module_param(scroll_acceleration, bool, 0644);
0048 MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
0049 
0050 static bool report_undeciphered;
0051 module_param(report_undeciphered, bool, 0644);
0052 MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
0053 
0054 #define TRACKPAD2_2021_BT_VERSION 0x110
0055 
0056 #define TRACKPAD_REPORT_ID 0x28
0057 #define TRACKPAD2_USB_REPORT_ID 0x02
0058 #define TRACKPAD2_BT_REPORT_ID 0x31
0059 #define MOUSE_REPORT_ID    0x29
0060 #define MOUSE2_REPORT_ID   0x12
0061 #define DOUBLE_REPORT_ID   0xf7
0062 #define USB_BATTERY_TIMEOUT_MS 60000
0063 
0064 /* These definitions are not precise, but they're close enough.  (Bits
0065  * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
0066  * to be some kind of bit mask -- 0x20 may be a near-field reading,
0067  * and 0x40 is actual contact, and 0x10 may be a start/stop or change
0068  * indication.)
0069  */
0070 #define TOUCH_STATE_MASK  0xf0
0071 #define TOUCH_STATE_NONE  0x00
0072 #define TOUCH_STATE_START 0x30
0073 #define TOUCH_STATE_DRAG  0x40
0074 
0075 /* Number of high-resolution events for each low-resolution detent. */
0076 #define SCROLL_HR_STEPS 10
0077 #define SCROLL_HR_MULT (120 / SCROLL_HR_STEPS)
0078 #define SCROLL_HR_THRESHOLD 90 /* units */
0079 #define SCROLL_ACCEL_DEFAULT 7
0080 
0081 /* Touch surface information. Dimension is in hundredths of a mm, min and max
0082  * are in units. */
0083 #define MOUSE_DIMENSION_X (float)9056
0084 #define MOUSE_MIN_X -1100
0085 #define MOUSE_MAX_X 1258
0086 #define MOUSE_RES_X ((MOUSE_MAX_X - MOUSE_MIN_X) / (MOUSE_DIMENSION_X / 100))
0087 #define MOUSE_DIMENSION_Y (float)5152
0088 #define MOUSE_MIN_Y -1589
0089 #define MOUSE_MAX_Y 2047
0090 #define MOUSE_RES_Y ((MOUSE_MAX_Y - MOUSE_MIN_Y) / (MOUSE_DIMENSION_Y / 100))
0091 
0092 #define TRACKPAD_DIMENSION_X (float)13000
0093 #define TRACKPAD_MIN_X -2909
0094 #define TRACKPAD_MAX_X 3167
0095 #define TRACKPAD_RES_X \
0096     ((TRACKPAD_MAX_X - TRACKPAD_MIN_X) / (TRACKPAD_DIMENSION_X / 100))
0097 #define TRACKPAD_DIMENSION_Y (float)11000
0098 #define TRACKPAD_MIN_Y -2456
0099 #define TRACKPAD_MAX_Y 2565
0100 #define TRACKPAD_RES_Y \
0101     ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
0102 
0103 #define TRACKPAD2_DIMENSION_X (float)16000
0104 #define TRACKPAD2_MIN_X -3678
0105 #define TRACKPAD2_MAX_X 3934
0106 #define TRACKPAD2_RES_X \
0107     ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
0108 #define TRACKPAD2_DIMENSION_Y (float)11490
0109 #define TRACKPAD2_MIN_Y -2478
0110 #define TRACKPAD2_MAX_Y 2587
0111 #define TRACKPAD2_RES_Y \
0112     ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
0113 
0114 /**
0115  * struct magicmouse_sc - Tracks Magic Mouse-specific data.
0116  * @input: Input device through which we report events.
0117  * @quirks: Currently unused.
0118  * @ntouches: Number of touches in most recent touch report.
0119  * @scroll_accel: Number of consecutive scroll motions.
0120  * @scroll_jiffies: Time of last scroll motion.
0121  * @touches: Most recent data for a touch, indexed by tracking ID.
0122  * @tracking_ids: Mapping of current touch input data to @touches.
0123  */
0124 struct magicmouse_sc {
0125     struct input_dev *input;
0126     unsigned long quirks;
0127 
0128     int ntouches;
0129     int scroll_accel;
0130     unsigned long scroll_jiffies;
0131 
0132     struct {
0133         short x;
0134         short y;
0135         short scroll_x;
0136         short scroll_y;
0137         short scroll_x_hr;
0138         short scroll_y_hr;
0139         u8 size;
0140         bool scroll_x_active;
0141         bool scroll_y_active;
0142     } touches[16];
0143     int tracking_ids[16];
0144 
0145     struct hid_device *hdev;
0146     struct delayed_work work;
0147     struct timer_list battery_timer;
0148 };
0149 
0150 static int magicmouse_firm_touch(struct magicmouse_sc *msc)
0151 {
0152     int touch = -1;
0153     int ii;
0154 
0155     /* If there is only one "firm" touch, set touch to its
0156      * tracking ID.
0157      */
0158     for (ii = 0; ii < msc->ntouches; ii++) {
0159         int idx = msc->tracking_ids[ii];
0160         if (msc->touches[idx].size < 8) {
0161             /* Ignore this touch. */
0162         } else if (touch >= 0) {
0163             touch = -1;
0164             break;
0165         } else {
0166             touch = idx;
0167         }
0168     }
0169 
0170     return touch;
0171 }
0172 
0173 static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
0174 {
0175     int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
0176         test_bit(BTN_RIGHT, msc->input->key) << 1 |
0177         test_bit(BTN_MIDDLE, msc->input->key) << 2;
0178 
0179     if (emulate_3button) {
0180         int id;
0181 
0182         /* If some button was pressed before, keep it held
0183          * down.  Otherwise, if there's exactly one firm
0184          * touch, use that to override the mouse's guess.
0185          */
0186         if (state == 0) {
0187             /* The button was released. */
0188         } else if (last_state != 0) {
0189             state = last_state;
0190         } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
0191             int x = msc->touches[id].x;
0192             if (x < middle_button_start)
0193                 state = 1;
0194             else if (x > middle_button_stop)
0195                 state = 2;
0196             else
0197                 state = 4;
0198         } /* else: we keep the mouse's guess */
0199 
0200         input_report_key(msc->input, BTN_MIDDLE, state & 4);
0201     }
0202 
0203     input_report_key(msc->input, BTN_LEFT, state & 1);
0204     input_report_key(msc->input, BTN_RIGHT, state & 2);
0205 
0206     if (state != last_state)
0207         msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
0208 }
0209 
0210 static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
0211 {
0212     struct input_dev *input = msc->input;
0213     int id, x, y, size, orientation, touch_major, touch_minor, state, down;
0214     int pressure = 0;
0215 
0216     if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
0217         input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
0218         id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
0219         x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
0220         y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
0221         size = tdata[5] & 0x3f;
0222         orientation = (tdata[6] >> 2) - 32;
0223         touch_major = tdata[3];
0224         touch_minor = tdata[4];
0225         state = tdata[7] & TOUCH_STATE_MASK;
0226         down = state != TOUCH_STATE_NONE;
0227     } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
0228         id = tdata[8] & 0xf;
0229         x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
0230         y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
0231         size = tdata[6];
0232         orientation = (tdata[8] >> 5) - 4;
0233         touch_major = tdata[4];
0234         touch_minor = tdata[5];
0235         pressure = tdata[7];
0236         state = tdata[3] & 0xC0;
0237         down = state == 0x80;
0238     } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
0239         id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
0240         x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
0241         y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
0242         size = tdata[6] & 0x3f;
0243         orientation = (tdata[7] >> 2) - 32;
0244         touch_major = tdata[4];
0245         touch_minor = tdata[5];
0246         state = tdata[8] & TOUCH_STATE_MASK;
0247         down = state != TOUCH_STATE_NONE;
0248     }
0249 
0250     /* Store tracking ID and other fields. */
0251     msc->tracking_ids[raw_id] = id;
0252     msc->touches[id].x = x;
0253     msc->touches[id].y = y;
0254     msc->touches[id].size = size;
0255 
0256     /* If requested, emulate a scroll wheel by detecting small
0257      * vertical touch motions.
0258      */
0259     if (emulate_scroll_wheel && (input->id.product !=
0260             USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) {
0261         unsigned long now = jiffies;
0262         int step_x = msc->touches[id].scroll_x - x;
0263         int step_y = msc->touches[id].scroll_y - y;
0264         int step_hr =
0265             max_t(int,
0266                   ((64 - (int)scroll_speed) * msc->scroll_accel) /
0267                     SCROLL_HR_STEPS,
0268                   1);
0269         int step_x_hr = msc->touches[id].scroll_x_hr - x;
0270         int step_y_hr = msc->touches[id].scroll_y_hr - y;
0271 
0272         /* Calculate and apply the scroll motion. */
0273         switch (state) {
0274         case TOUCH_STATE_START:
0275             msc->touches[id].scroll_x = x;
0276             msc->touches[id].scroll_y = y;
0277             msc->touches[id].scroll_x_hr = x;
0278             msc->touches[id].scroll_y_hr = y;
0279             msc->touches[id].scroll_x_active = false;
0280             msc->touches[id].scroll_y_active = false;
0281 
0282             /* Reset acceleration after half a second. */
0283             if (scroll_acceleration && time_before(now,
0284                         msc->scroll_jiffies + HZ / 2))
0285                 msc->scroll_accel = max_t(int,
0286                         msc->scroll_accel - 1, 1);
0287             else
0288                 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
0289 
0290             break;
0291         case TOUCH_STATE_DRAG:
0292             step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
0293             if (step_x != 0) {
0294                 msc->touches[id].scroll_x -= step_x *
0295                     (64 - scroll_speed) * msc->scroll_accel;
0296                 msc->scroll_jiffies = now;
0297                 input_report_rel(input, REL_HWHEEL, -step_x);
0298             }
0299 
0300             step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
0301             if (step_y != 0) {
0302                 msc->touches[id].scroll_y -= step_y *
0303                     (64 - scroll_speed) * msc->scroll_accel;
0304                 msc->scroll_jiffies = now;
0305                 input_report_rel(input, REL_WHEEL, step_y);
0306             }
0307 
0308             if (!msc->touches[id].scroll_x_active &&
0309                 abs(step_x_hr) > SCROLL_HR_THRESHOLD) {
0310                 msc->touches[id].scroll_x_active = true;
0311                 msc->touches[id].scroll_x_hr = x;
0312                 step_x_hr = 0;
0313             }
0314 
0315             step_x_hr /= step_hr;
0316             if (step_x_hr != 0 &&
0317                 msc->touches[id].scroll_x_active) {
0318                 msc->touches[id].scroll_x_hr -= step_x_hr *
0319                     step_hr;
0320                 input_report_rel(input,
0321                          REL_HWHEEL_HI_RES,
0322                          -step_x_hr * SCROLL_HR_MULT);
0323             }
0324 
0325             if (!msc->touches[id].scroll_y_active &&
0326                 abs(step_y_hr) > SCROLL_HR_THRESHOLD) {
0327                 msc->touches[id].scroll_y_active = true;
0328                 msc->touches[id].scroll_y_hr = y;
0329                 step_y_hr = 0;
0330             }
0331 
0332             step_y_hr /= step_hr;
0333             if (step_y_hr != 0 &&
0334                 msc->touches[id].scroll_y_active) {
0335                 msc->touches[id].scroll_y_hr -= step_y_hr *
0336                     step_hr;
0337                 input_report_rel(input,
0338                          REL_WHEEL_HI_RES,
0339                          step_y_hr * SCROLL_HR_MULT);
0340             }
0341             break;
0342         }
0343     }
0344 
0345     if (down)
0346         msc->ntouches++;
0347 
0348     input_mt_slot(input, id);
0349     input_mt_report_slot_state(input, MT_TOOL_FINGER, down);
0350 
0351     /* Generate the input events for this touch. */
0352     if (down) {
0353         input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major << 2);
0354         input_report_abs(input, ABS_MT_TOUCH_MINOR, touch_minor << 2);
0355         input_report_abs(input, ABS_MT_ORIENTATION, -orientation);
0356         input_report_abs(input, ABS_MT_POSITION_X, x);
0357         input_report_abs(input, ABS_MT_POSITION_Y, y);
0358 
0359         if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
0360             input_report_abs(input, ABS_MT_PRESSURE, pressure);
0361 
0362         if (report_undeciphered) {
0363             if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
0364                 input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
0365                 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
0366             else if (input->id.product !=
0367                     USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
0368                 input_event(input, EV_MSC, MSC_RAW, tdata[8]);
0369         }
0370     }
0371 }
0372 
0373 static int magicmouse_raw_event(struct hid_device *hdev,
0374         struct hid_report *report, u8 *data, int size)
0375 {
0376     struct magicmouse_sc *msc = hid_get_drvdata(hdev);
0377     struct input_dev *input = msc->input;
0378     int x = 0, y = 0, ii, clicks = 0, npoints;
0379 
0380     switch (data[0]) {
0381     case TRACKPAD_REPORT_ID:
0382     case TRACKPAD2_BT_REPORT_ID:
0383         /* Expect four bytes of prefix, and N*9 bytes of touch data. */
0384         if (size < 4 || ((size - 4) % 9) != 0)
0385             return 0;
0386         npoints = (size - 4) / 9;
0387         if (npoints > 15) {
0388             hid_warn(hdev, "invalid size value (%d) for TRACKPAD_REPORT_ID\n",
0389                     size);
0390             return 0;
0391         }
0392         msc->ntouches = 0;
0393         for (ii = 0; ii < npoints; ii++)
0394             magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
0395 
0396         clicks = data[1];
0397 
0398         /* The following bits provide a device specific timestamp. They
0399          * are unused here.
0400          *
0401          * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
0402          */
0403         break;
0404     case TRACKPAD2_USB_REPORT_ID:
0405         /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
0406         if (size < 12 || ((size - 12) % 9) != 0)
0407             return 0;
0408         npoints = (size - 12) / 9;
0409         if (npoints > 15) {
0410             hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
0411                     size);
0412             return 0;
0413         }
0414         msc->ntouches = 0;
0415         for (ii = 0; ii < npoints; ii++)
0416             magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
0417 
0418         clicks = data[1];
0419         break;
0420     case MOUSE_REPORT_ID:
0421         /* Expect six bytes of prefix, and N*8 bytes of touch data. */
0422         if (size < 6 || ((size - 6) % 8) != 0)
0423             return 0;
0424         npoints = (size - 6) / 8;
0425         if (npoints > 15) {
0426             hid_warn(hdev, "invalid size value (%d) for MOUSE_REPORT_ID\n",
0427                     size);
0428             return 0;
0429         }
0430         msc->ntouches = 0;
0431         for (ii = 0; ii < npoints; ii++)
0432             magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
0433 
0434         /* When emulating three-button mode, it is important
0435          * to have the current touch information before
0436          * generating a click event.
0437          */
0438         x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
0439         y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
0440         clicks = data[3];
0441 
0442         /* The following bits provide a device specific timestamp. They
0443          * are unused here.
0444          *
0445          * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
0446          */
0447         break;
0448     case MOUSE2_REPORT_ID:
0449         /* Size is either 8 or (14 + 8 * N) */
0450         if (size != 8 && (size < 14 || (size - 14) % 8 != 0))
0451             return 0;
0452         npoints = (size - 14) / 8;
0453         if (npoints > 15) {
0454             hid_warn(hdev, "invalid size value (%d) for MOUSE2_REPORT_ID\n",
0455                     size);
0456             return 0;
0457         }
0458         msc->ntouches = 0;
0459         for (ii = 0; ii < npoints; ii++)
0460             magicmouse_emit_touch(msc, ii, data + ii * 8 + 14);
0461 
0462         /* When emulating three-button mode, it is important
0463          * to have the current touch information before
0464          * generating a click event.
0465          */
0466         x = (int)((data[3] << 24) | (data[2] << 16)) >> 16;
0467         y = (int)((data[5] << 24) | (data[4] << 16)) >> 16;
0468         clicks = data[1];
0469 
0470         /* The following bits provide a device specific timestamp. They
0471          * are unused here.
0472          *
0473          * ts = data[11] >> 6 | data[12] << 2 | data[13] << 10;
0474          */
0475         break;
0476     case DOUBLE_REPORT_ID:
0477         /* Sometimes the trackpad sends two touch reports in one
0478          * packet.
0479          */
0480         magicmouse_raw_event(hdev, report, data + 2, data[1]);
0481         magicmouse_raw_event(hdev, report, data + 2 + data[1],
0482             size - 2 - data[1]);
0483         break;
0484     default:
0485         return 0;
0486     }
0487 
0488     if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
0489         input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
0490         magicmouse_emit_buttons(msc, clicks & 3);
0491         input_report_rel(input, REL_X, x);
0492         input_report_rel(input, REL_Y, y);
0493     } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
0494         input_mt_sync_frame(input);
0495         input_report_key(input, BTN_MOUSE, clicks & 1);
0496     } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
0497         input_report_key(input, BTN_MOUSE, clicks & 1);
0498         input_mt_report_pointer_emulation(input, true);
0499     }
0500 
0501     input_sync(input);
0502     return 1;
0503 }
0504 
0505 static int magicmouse_event(struct hid_device *hdev, struct hid_field *field,
0506         struct hid_usage *usage, __s32 value)
0507 {
0508     struct magicmouse_sc *msc = hid_get_drvdata(hdev);
0509     if (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 &&
0510         field->report->id == MOUSE2_REPORT_ID) {
0511         /*
0512          * magic_mouse_raw_event has done all the work. Skip hidinput.
0513          *
0514          * Specifically, hidinput may modify BTN_LEFT and BTN_RIGHT,
0515          * breaking emulate_3button.
0516          */
0517         return 1;
0518     }
0519     return 0;
0520 }
0521 
0522 static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
0523 {
0524     int error;
0525     int mt_flags = 0;
0526 
0527     __set_bit(EV_KEY, input->evbit);
0528 
0529     if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
0530         input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
0531         __set_bit(BTN_LEFT, input->keybit);
0532         __set_bit(BTN_RIGHT, input->keybit);
0533         if (emulate_3button)
0534             __set_bit(BTN_MIDDLE, input->keybit);
0535 
0536         __set_bit(EV_REL, input->evbit);
0537         __set_bit(REL_X, input->relbit);
0538         __set_bit(REL_Y, input->relbit);
0539         if (emulate_scroll_wheel) {
0540             __set_bit(REL_WHEEL, input->relbit);
0541             __set_bit(REL_HWHEEL, input->relbit);
0542             __set_bit(REL_WHEEL_HI_RES, input->relbit);
0543             __set_bit(REL_HWHEEL_HI_RES, input->relbit);
0544         }
0545     } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
0546         /* If the trackpad has been connected to a Mac, the name is
0547          * automatically personalized, e.g., "José Expósito's Trackpad".
0548          * When connected through Bluetooth, the personalized name is
0549          * reported, however, when connected through USB the generic
0550          * name is reported.
0551          * Set the device name to ensure the same driver settings get
0552          * loaded, whether connected through bluetooth or USB.
0553          */
0554         if (hdev->vendor == BT_VENDOR_ID_APPLE) {
0555             if (input->id.version == TRACKPAD2_2021_BT_VERSION)
0556                 input->name = "Apple Inc. Magic Trackpad";
0557             else
0558                 input->name = "Apple Inc. Magic Trackpad 2";
0559         } else { /* USB_VENDOR_ID_APPLE */
0560             input->name = hdev->name;
0561         }
0562 
0563         __clear_bit(EV_MSC, input->evbit);
0564         __clear_bit(BTN_0, input->keybit);
0565         __clear_bit(BTN_RIGHT, input->keybit);
0566         __clear_bit(BTN_MIDDLE, input->keybit);
0567         __set_bit(BTN_MOUSE, input->keybit);
0568         __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
0569         __set_bit(BTN_TOOL_FINGER, input->keybit);
0570 
0571         mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
0572                 INPUT_MT_TRACK;
0573     } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
0574         /* input->keybit is initialized with incorrect button info
0575          * for Magic Trackpad. There really is only one physical
0576          * button (BTN_LEFT == BTN_MOUSE). Make sure we don't
0577          * advertise buttons that don't exist...
0578          */
0579         __clear_bit(BTN_RIGHT, input->keybit);
0580         __clear_bit(BTN_MIDDLE, input->keybit);
0581         __set_bit(BTN_MOUSE, input->keybit);
0582         __set_bit(BTN_TOOL_FINGER, input->keybit);
0583         __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
0584         __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
0585         __set_bit(BTN_TOOL_QUADTAP, input->keybit);
0586         __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
0587         __set_bit(BTN_TOUCH, input->keybit);
0588         __set_bit(INPUT_PROP_POINTER, input->propbit);
0589         __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
0590     }
0591 
0592 
0593     __set_bit(EV_ABS, input->evbit);
0594 
0595     error = input_mt_init_slots(input, 16, mt_flags);
0596     if (error)
0597         return error;
0598     input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
0599                  4, 0);
0600     input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
0601                  4, 0);
0602 
0603     /* Note: Touch Y position from the device is inverted relative
0604      * to how pointer motion is reported (and relative to how USB
0605      * HID recommends the coordinates work).  This driver keeps
0606      * the origin at the same position, and just uses the additive
0607      * inverse of the reported Y.
0608      */
0609     if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE ||
0610         input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
0611         input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
0612         input_set_abs_params(input, ABS_MT_POSITION_X,
0613                      MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
0614         input_set_abs_params(input, ABS_MT_POSITION_Y,
0615                      MOUSE_MIN_Y, MOUSE_MAX_Y, 4, 0);
0616 
0617         input_abs_set_res(input, ABS_MT_POSITION_X,
0618                   MOUSE_RES_X);
0619         input_abs_set_res(input, ABS_MT_POSITION_Y,
0620                   MOUSE_RES_Y);
0621     } else if (input->id.product ==  USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
0622         input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
0623         input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
0624         input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
0625         input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
0626                      TRACKPAD2_MAX_X, 0, 0);
0627         input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
0628                      TRACKPAD2_MAX_Y, 0, 0);
0629         input_set_abs_params(input, ABS_MT_POSITION_X,
0630                      TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
0631         input_set_abs_params(input, ABS_MT_POSITION_Y,
0632                      TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
0633 
0634         input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
0635         input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
0636         input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
0637         input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
0638     } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
0639         input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
0640         input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
0641                      TRACKPAD_MAX_X, 4, 0);
0642         input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
0643                      TRACKPAD_MAX_Y, 4, 0);
0644         input_set_abs_params(input, ABS_MT_POSITION_X,
0645                      TRACKPAD_MIN_X, TRACKPAD_MAX_X, 4, 0);
0646         input_set_abs_params(input, ABS_MT_POSITION_Y,
0647                      TRACKPAD_MIN_Y, TRACKPAD_MAX_Y, 4, 0);
0648 
0649         input_abs_set_res(input, ABS_X, TRACKPAD_RES_X);
0650         input_abs_set_res(input, ABS_Y, TRACKPAD_RES_Y);
0651         input_abs_set_res(input, ABS_MT_POSITION_X,
0652                   TRACKPAD_RES_X);
0653         input_abs_set_res(input, ABS_MT_POSITION_Y,
0654                   TRACKPAD_RES_Y);
0655     }
0656 
0657     input_set_events_per_packet(input, 60);
0658 
0659     if (report_undeciphered &&
0660         input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
0661         __set_bit(EV_MSC, input->evbit);
0662         __set_bit(MSC_RAW, input->mscbit);
0663     }
0664 
0665     /*
0666      * hid-input may mark device as using autorepeat, but neither
0667      * the trackpad, nor the mouse actually want it.
0668      */
0669     __clear_bit(EV_REP, input->evbit);
0670 
0671     return 0;
0672 }
0673 
0674 static int magicmouse_input_mapping(struct hid_device *hdev,
0675         struct hid_input *hi, struct hid_field *field,
0676         struct hid_usage *usage, unsigned long **bit, int *max)
0677 {
0678     struct magicmouse_sc *msc = hid_get_drvdata(hdev);
0679 
0680     if (!msc->input)
0681         msc->input = hi->input;
0682 
0683     /* Magic Trackpad does not give relative data after switching to MT */
0684     if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
0685          hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
0686         field->flags & HID_MAIN_ITEM_RELATIVE)
0687         return -1;
0688 
0689     return 0;
0690 }
0691 
0692 static int magicmouse_input_configured(struct hid_device *hdev,
0693         struct hid_input *hi)
0694 
0695 {
0696     struct magicmouse_sc *msc = hid_get_drvdata(hdev);
0697     int ret;
0698 
0699     ret = magicmouse_setup_input(msc->input, hdev);
0700     if (ret) {
0701         hid_err(hdev, "magicmouse setup input failed (%d)\n", ret);
0702         /* clean msc->input to notify probe() of the failure */
0703         msc->input = NULL;
0704         return ret;
0705     }
0706 
0707     return 0;
0708 }
0709 
0710 static int magicmouse_enable_multitouch(struct hid_device *hdev)
0711 {
0712     const u8 *feature;
0713     const u8 feature_mt[] = { 0xD7, 0x01 };
0714     const u8 feature_mt_mouse2[] = { 0xF1, 0x02, 0x01 };
0715     const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
0716     const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
0717     u8 *buf;
0718     int ret;
0719     int feature_size;
0720 
0721     if (hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
0722         if (hdev->vendor == BT_VENDOR_ID_APPLE) {
0723             feature_size = sizeof(feature_mt_trackpad2_bt);
0724             feature = feature_mt_trackpad2_bt;
0725         } else { /* USB_VENDOR_ID_APPLE */
0726             feature_size = sizeof(feature_mt_trackpad2_usb);
0727             feature = feature_mt_trackpad2_usb;
0728         }
0729     } else if (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
0730         feature_size = sizeof(feature_mt_mouse2);
0731         feature = feature_mt_mouse2;
0732     } else {
0733         feature_size = sizeof(feature_mt);
0734         feature = feature_mt;
0735     }
0736 
0737     buf = kmemdup(feature, feature_size, GFP_KERNEL);
0738     if (!buf)
0739         return -ENOMEM;
0740 
0741     ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
0742                 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
0743     kfree(buf);
0744     return ret;
0745 }
0746 
0747 static void magicmouse_enable_mt_work(struct work_struct *work)
0748 {
0749     struct magicmouse_sc *msc =
0750         container_of(work, struct magicmouse_sc, work.work);
0751     int ret;
0752 
0753     ret = magicmouse_enable_multitouch(msc->hdev);
0754     if (ret < 0)
0755         hid_err(msc->hdev, "unable to request touch data (%d)\n", ret);
0756 }
0757 
0758 static int magicmouse_fetch_battery(struct hid_device *hdev)
0759 {
0760 #ifdef CONFIG_HID_BATTERY_STRENGTH
0761     struct hid_report_enum *report_enum;
0762     struct hid_report *report;
0763 
0764     if (!hdev->battery || hdev->vendor != USB_VENDOR_ID_APPLE ||
0765         (hdev->product != USB_DEVICE_ID_APPLE_MAGICMOUSE2 &&
0766          hdev->product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2))
0767         return -1;
0768 
0769     report_enum = &hdev->report_enum[hdev->battery_report_type];
0770     report = report_enum->report_id_hash[hdev->battery_report_id];
0771 
0772     if (!report || report->maxfield < 1)
0773         return -1;
0774 
0775     if (hdev->battery_capacity == hdev->battery_max)
0776         return -1;
0777 
0778     hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
0779     return 0;
0780 #else
0781     return -1;
0782 #endif
0783 }
0784 
0785 static void magicmouse_battery_timer_tick(struct timer_list *t)
0786 {
0787     struct magicmouse_sc *msc = from_timer(msc, t, battery_timer);
0788     struct hid_device *hdev = msc->hdev;
0789 
0790     if (magicmouse_fetch_battery(hdev) == 0) {
0791         mod_timer(&msc->battery_timer,
0792               jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
0793     }
0794 }
0795 
0796 static int magicmouse_probe(struct hid_device *hdev,
0797     const struct hid_device_id *id)
0798 {
0799     struct magicmouse_sc *msc;
0800     struct hid_report *report;
0801     int ret;
0802 
0803     msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
0804     if (msc == NULL) {
0805         hid_err(hdev, "can't alloc magicmouse descriptor\n");
0806         return -ENOMEM;
0807     }
0808 
0809     msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
0810     msc->hdev = hdev;
0811     INIT_DEFERRABLE_WORK(&msc->work, magicmouse_enable_mt_work);
0812 
0813     msc->quirks = id->driver_data;
0814     hid_set_drvdata(hdev, msc);
0815 
0816     ret = hid_parse(hdev);
0817     if (ret) {
0818         hid_err(hdev, "magicmouse hid parse failed\n");
0819         return ret;
0820     }
0821 
0822     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0823     if (ret) {
0824         hid_err(hdev, "magicmouse hw start failed\n");
0825         return ret;
0826     }
0827 
0828     timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0);
0829     mod_timer(&msc->battery_timer,
0830           jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
0831     magicmouse_fetch_battery(hdev);
0832 
0833     if (id->vendor == USB_VENDOR_ID_APPLE &&
0834         (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
0835          (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 && hdev->type != HID_TYPE_USBMOUSE)))
0836         return 0;
0837 
0838     if (!msc->input) {
0839         hid_err(hdev, "magicmouse input not registered\n");
0840         ret = -ENOMEM;
0841         goto err_stop_hw;
0842     }
0843 
0844     if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
0845         report = hid_register_report(hdev, HID_INPUT_REPORT,
0846             MOUSE_REPORT_ID, 0);
0847     else if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2)
0848         report = hid_register_report(hdev, HID_INPUT_REPORT,
0849             MOUSE2_REPORT_ID, 0);
0850     else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
0851         if (id->vendor == BT_VENDOR_ID_APPLE)
0852             report = hid_register_report(hdev, HID_INPUT_REPORT,
0853                 TRACKPAD2_BT_REPORT_ID, 0);
0854         else /* USB_VENDOR_ID_APPLE */
0855             report = hid_register_report(hdev, HID_INPUT_REPORT,
0856                 TRACKPAD2_USB_REPORT_ID, 0);
0857     } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
0858         report = hid_register_report(hdev, HID_INPUT_REPORT,
0859             TRACKPAD_REPORT_ID, 0);
0860         report = hid_register_report(hdev, HID_INPUT_REPORT,
0861             DOUBLE_REPORT_ID, 0);
0862     }
0863 
0864     if (!report) {
0865         hid_err(hdev, "unable to register touch report\n");
0866         ret = -ENOMEM;
0867         goto err_stop_hw;
0868     }
0869     report->size = 6;
0870 
0871     /*
0872      * Some devices repond with 'invalid report id' when feature
0873      * report switching it into multitouch mode is sent to it.
0874      *
0875      * This results in -EIO from the _raw low-level transport callback,
0876      * but there seems to be no other way of switching the mode.
0877      * Thus the super-ugly hacky success check below.
0878      */
0879     ret = magicmouse_enable_multitouch(hdev);
0880     if (ret != -EIO && ret < 0) {
0881         hid_err(hdev, "unable to request touch data (%d)\n", ret);
0882         goto err_stop_hw;
0883     }
0884     if (ret == -EIO && id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
0885         schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
0886     }
0887 
0888     return 0;
0889 err_stop_hw:
0890     del_timer_sync(&msc->battery_timer);
0891     hid_hw_stop(hdev);
0892     return ret;
0893 }
0894 
0895 static void magicmouse_remove(struct hid_device *hdev)
0896 {
0897     struct magicmouse_sc *msc = hid_get_drvdata(hdev);
0898 
0899     if (msc) {
0900         cancel_delayed_work_sync(&msc->work);
0901         del_timer_sync(&msc->battery_timer);
0902     }
0903 
0904     hid_hw_stop(hdev);
0905 }
0906 
0907 static __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc,
0908                      unsigned int *rsize)
0909 {
0910     /*
0911      * Change the usage from:
0912      *   0x06, 0x00, 0xff, // Usage Page (Vendor Defined Page 1)  0
0913      *   0x09, 0x0b,       // Usage (Vendor Usage 0x0b)           3
0914      * To:
0915      *   0x05, 0x01,       // Usage Page (Generic Desktop)        0
0916      *   0x09, 0x02,       // Usage (Mouse)                       2
0917      */
0918     if (hdev->vendor == USB_VENDOR_ID_APPLE &&
0919         (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
0920          hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
0921         *rsize == 83 && rdesc[46] == 0x84 && rdesc[58] == 0x85) {
0922         hid_info(hdev,
0923              "fixing up magicmouse battery report descriptor\n");
0924         *rsize = *rsize - 1;
0925         rdesc = kmemdup(rdesc + 1, *rsize, GFP_KERNEL);
0926         if (!rdesc)
0927             return NULL;
0928 
0929         rdesc[0] = 0x05;
0930         rdesc[1] = 0x01;
0931         rdesc[2] = 0x09;
0932         rdesc[3] = 0x02;
0933     }
0934 
0935     return rdesc;
0936 }
0937 
0938 static const struct hid_device_id magic_mice[] = {
0939     { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
0940         USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
0941     { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
0942         USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
0943     { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
0944         USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
0945     { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
0946         USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
0947     { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
0948         USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
0949     { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
0950         USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
0951     { }
0952 };
0953 MODULE_DEVICE_TABLE(hid, magic_mice);
0954 
0955 static struct hid_driver magicmouse_driver = {
0956     .name = "magicmouse",
0957     .id_table = magic_mice,
0958     .probe = magicmouse_probe,
0959     .remove = magicmouse_remove,
0960     .report_fixup = magicmouse_report_fixup,
0961     .raw_event = magicmouse_raw_event,
0962     .event = magicmouse_event,
0963     .input_mapping = magicmouse_input_mapping,
0964     .input_configured = magicmouse_input_configured,
0965 };
0966 module_hid_driver(magicmouse_driver);
0967 
0968 MODULE_LICENSE("GPL");