Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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