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)  *  HID driver for Sony DualSense(TM) controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Copyright (c) 2020 Sony Interactive Entertainment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/bits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/input/mt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) /* List of connected playstation devices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) static DEFINE_MUTEX(ps_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) static LIST_HEAD(ps_devices_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) static DEFINE_IDA(ps_player_id_allocator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define HID_PLAYSTATION_VERSION_PATCH 0x8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) /* Base class for playstation devices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) struct ps_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	struct hid_device *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	uint32_t player_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	struct power_supply_desc battery_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	struct power_supply *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	uint8_t battery_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	int battery_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	uint8_t mac_address[6]; /* Note: stored in little endian order. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	uint32_t hw_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	uint32_t fw_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) /* Calibration data for playstation motion sensors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) struct ps_calibration_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	int abs_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	short bias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	int sens_numer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	int sens_denom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) /* Seed values for DualShock4 / DualSense CRC32 for different report types. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define PS_INPUT_CRC32_SEED	0xA1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #define PS_OUTPUT_CRC32_SEED	0xA2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define PS_FEATURE_CRC32_SEED	0xA3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define DS_INPUT_REPORT_USB			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define DS_INPUT_REPORT_USB_SIZE		64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define DS_INPUT_REPORT_BT			0x31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define DS_INPUT_REPORT_BT_SIZE			78
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define DS_OUTPUT_REPORT_USB			0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define DS_OUTPUT_REPORT_USB_SIZE		63
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define DS_OUTPUT_REPORT_BT			0x31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define DS_OUTPUT_REPORT_BT_SIZE		78
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define DS_FEATURE_REPORT_CALIBRATION		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define DS_FEATURE_REPORT_CALIBRATION_SIZE	41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define DS_FEATURE_REPORT_PAIRING_INFO		0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define DS_FEATURE_REPORT_PAIRING_INFO_SIZE	20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define DS_FEATURE_REPORT_FIRMWARE_INFO		0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) /* Button masks for DualSense input report. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define DS_BUTTONS0_HAT_SWITCH	GENMASK(3, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define DS_BUTTONS0_SQUARE	BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define DS_BUTTONS0_CROSS	BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define DS_BUTTONS0_CIRCLE	BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define DS_BUTTONS0_TRIANGLE	BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define DS_BUTTONS1_L1		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define DS_BUTTONS1_R1		BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define DS_BUTTONS1_L2		BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define DS_BUTTONS1_R2		BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define DS_BUTTONS1_CREATE	BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define DS_BUTTONS1_OPTIONS	BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #define DS_BUTTONS1_L3		BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define DS_BUTTONS1_R3		BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define DS_BUTTONS2_PS_HOME	BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define DS_BUTTONS2_TOUCHPAD	BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define DS_BUTTONS2_MIC_MUTE	BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) /* Status field of DualSense input report. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define DS_STATUS_BATTERY_CAPACITY	GENMASK(3, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define DS_STATUS_CHARGING		GENMASK(7, 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #define DS_STATUS_CHARGING_SHIFT	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * Status of a DualSense touch point contact.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * Contact IDs, with highest bit set are 'inactive'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * and any associated data is then invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) #define DS_TOUCH_POINT_INACTIVE BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  /* Magic value required in tag field of Bluetooth output report. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) #define DS_OUTPUT_TAG 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) /* Flags for DualSense output report. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) #define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) #define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) #define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) #define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) #define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) #define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) /* DualSense hardware limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define DS_ACC_RES_PER_G	8192
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) #define DS_ACC_RANGE		(4*DS_ACC_RES_PER_G)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) #define DS_GYRO_RES_PER_DEG_S	1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) #define DS_GYRO_RANGE		(2048*DS_GYRO_RES_PER_DEG_S)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define DS_TOUCHPAD_WIDTH	1920
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) #define DS_TOUCHPAD_HEIGHT	1080
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) struct dualsense {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	struct ps_device base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct input_dev *gamepad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct input_dev *sensors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	struct input_dev *touchpad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	/* Calibration data for accelerometer and gyroscope. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct ps_calibration_data accel_calib_data[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct ps_calibration_data gyro_calib_data[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	/* Timestamp for sensor data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	bool sensor_timestamp_initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	uint32_t prev_sensor_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	uint32_t sensor_timestamp_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	/* Compatible rumble state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	bool update_rumble;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	uint8_t motor_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	uint8_t motor_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	/* RGB lightbar */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	bool update_lightbar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	uint8_t lightbar_red;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	uint8_t lightbar_green;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	uint8_t lightbar_blue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	/* Microphone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	bool update_mic_mute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	bool mic_muted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	bool last_btn_mic_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	/* Player leds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	bool update_player_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	uint8_t player_leds_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	struct led_classdev player_leds[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	struct work_struct output_worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	void *output_report_dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	uint8_t output_seq; /* Sequence number for output report. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) struct dualsense_touch_point {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	uint8_t contact;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	uint8_t x_lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	uint8_t x_hi:4, y_lo:4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	uint8_t y_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static_assert(sizeof(struct dualsense_touch_point) == 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) /* Main DualSense input report excluding any BT/USB specific headers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) struct dualsense_input_report {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	uint8_t x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	uint8_t rx, ry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	uint8_t z, rz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	uint8_t seq_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	uint8_t buttons[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	uint8_t reserved[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	/* Motion sensors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	__le16 gyro[3]; /* x, y, z */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	__le16 accel[3]; /* x, y, z */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	__le32 sensor_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	uint8_t reserved2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	/* Touchpad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	struct dualsense_touch_point points[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	uint8_t reserved3[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	uint8_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	uint8_t reserved4[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) /* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) static_assert(sizeof(struct dualsense_input_report) == DS_INPUT_REPORT_USB_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) /* Common data between DualSense BT/USB main output report. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) struct dualsense_output_report_common {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	uint8_t valid_flag0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	uint8_t valid_flag1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	/* For DualShock 4 compatibility mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	uint8_t motor_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	uint8_t motor_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	/* Audio controls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	uint8_t reserved[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	uint8_t mute_button_led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	uint8_t power_save_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	uint8_t reserved2[28];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	/* LEDs and lightbar */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	uint8_t valid_flag2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	uint8_t reserved3[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	uint8_t lightbar_setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	uint8_t led_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	uint8_t player_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	uint8_t lightbar_red;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	uint8_t lightbar_green;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	uint8_t lightbar_blue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) static_assert(sizeof(struct dualsense_output_report_common) == 47);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) struct dualsense_output_report_bt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	uint8_t report_id; /* 0x31 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	uint8_t seq_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	uint8_t tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	struct dualsense_output_report_common common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	uint8_t reserved[24];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	__le32 crc32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) static_assert(sizeof(struct dualsense_output_report_bt) == DS_OUTPUT_REPORT_BT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) struct dualsense_output_report_usb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	uint8_t report_id; /* 0x02 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct dualsense_output_report_common common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	uint8_t reserved[15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) static_assert(sizeof(struct dualsense_output_report_usb) == DS_OUTPUT_REPORT_USB_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  * The DualSense has a main output report used to control most features. It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)  * largely the same between Bluetooth and USB except for different headers and CRC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251)  * This structure hide the differences between the two to simplify sending output reports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) struct dualsense_output_report {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	uint8_t *data; /* Start of data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	uint8_t len; /* Size of output report */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	/* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	struct dualsense_output_report_bt *bt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	/* Points to USB data payload in case for a USB report else NULL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct dualsense_output_report_usb *usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	/* Points to common section of report, so past any headers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	struct dualsense_output_report_common *common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * Common gamepad buttons across DualShock 3 / 4 and DualSense.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  * Note: for device with a touchpad, touchpad button is not included
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268)  *        as it will be part of the touchpad device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) static const int ps_gamepad_buttons[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	BTN_WEST, /* Square */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	BTN_NORTH, /* Triangle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	BTN_EAST, /* Circle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	BTN_SOUTH, /* Cross */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	BTN_TL, /* L1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	BTN_TR, /* R1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	BTN_TL2, /* L2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	BTN_TR2, /* R2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	BTN_SELECT, /* Create (PS5) / Share (PS4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	BTN_START, /* Option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	BTN_THUMBL, /* L3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	BTN_THUMBR, /* R3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	BTN_MODE, /* PS Home */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) static const struct {int x; int y; } ps_gamepad_hat_mapping[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	{0, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292)  * Add a new ps_device to ps_devices if it doesn't exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  * Return error on duplicate device, which can happen if the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  * device is connected using both Bluetooth and USB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) static int ps_devices_list_add(struct ps_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	struct ps_device *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	mutex_lock(&ps_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	list_for_each_entry(entry, &ps_devices_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 					dev->mac_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			mutex_unlock(&ps_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	list_add_tail(&dev->list, &ps_devices_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	mutex_unlock(&ps_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static int ps_devices_list_remove(struct ps_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	mutex_lock(&ps_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	list_del(&dev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	mutex_unlock(&ps_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) static int ps_device_set_player_id(struct ps_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	int ret = ida_alloc(&ps_player_id_allocator, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	dev->player_id = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) static void ps_device_release_player_id(struct ps_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	ida_free(&ps_player_id_allocator, dev->player_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	dev->player_id = U32_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	input_dev = devm_input_allocate_device(&hdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	if (!input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	input_dev->id.bustype = hdev->bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	input_dev->id.vendor = hdev->vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	input_dev->id.product = hdev->product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	input_dev->id.version = hdev->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	input_dev->uniq = hdev->uniq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	if (name_suffix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 				name_suffix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		if (!input_dev->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 			return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		input_dev->name = hdev->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	input_set_drvdata(input_dev, hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	return input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) static enum power_supply_property ps_power_supply_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	POWER_SUPPLY_PROP_SCOPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) static int ps_battery_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	struct ps_device *dev = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	uint8_t battery_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	int battery_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	spin_lock_irqsave(&dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	battery_capacity = dev->battery_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	battery_status = dev->battery_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	spin_unlock_irqrestore(&dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		val->intval = battery_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		val->intval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		val->intval = battery_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	case POWER_SUPPLY_PROP_SCOPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) static int ps_device_register_battery(struct ps_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	struct power_supply *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	struct power_supply_config battery_cfg = { .drv_data = dev };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	dev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	dev->battery_desc.properties = ps_power_supply_props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	dev->battery_desc.num_properties = ARRAY_SIZE(ps_power_supply_props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	dev->battery_desc.get_property = ps_battery_get_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	dev->battery_desc.name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			"ps-controller-battery-%pMR", dev->mac_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (!dev->battery_desc.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	battery = devm_power_supply_register(&dev->hdev->dev, &dev->battery_desc, &battery_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	if (IS_ERR(battery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		ret = PTR_ERR(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		hid_err(dev->hdev, "Unable to register battery device: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	dev->battery = battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	ret = power_supply_powers(dev->battery, &dev->hdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		hid_err(dev->hdev, "Unable to activate battery device: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) /* Compute crc32 of HID data and compare against expected CRC. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	uint32_t crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	crc = crc32_le(0xFFFFFFFF, &seed, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	crc = ~crc32_le(crc, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	return crc == report_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) static struct input_dev *ps_gamepad_create(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	struct input_dev *gamepad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	gamepad = ps_allocate_input_dev(hdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (IS_ERR(gamepad))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		return ERR_CAST(gamepad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	input_set_abs_params(gamepad, ABS_X, 0, 255, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	input_set_abs_params(gamepad, ABS_Y, 0, 255, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	input_set_abs_params(gamepad, ABS_Z, 0, 255, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	input_set_abs_params(gamepad, ABS_RX, 0, 255, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	input_set_abs_params(gamepad, ABS_RY, 0, 255, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	input_set_abs_params(gamepad, ABS_RZ, 0, 255, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	input_set_abs_params(gamepad, ABS_HAT0X, -1, 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	input_set_abs_params(gamepad, ABS_HAT0Y, -1, 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	for (i = 0; i < ARRAY_SIZE(ps_gamepad_buttons); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		input_set_capability(gamepad, EV_KEY, ps_gamepad_buttons[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) #if IS_ENABLED(CONFIG_PLAYSTATION_FF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	if (play_effect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		input_set_capability(gamepad, EV_FF, FF_RUMBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		input_ff_create_memless(gamepad, NULL, play_effect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	ret = input_register_device(gamepad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	return gamepad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	ret = hid_hw_raw_request(hdev, report_id, buf, size, HID_FEATURE_REPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 				 HID_REQ_GET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		hid_err(hdev, "Failed to retrieve feature with reportID %d: %d\n", report_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (ret != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		hid_err(hdev, "Invalid byte count transferred, expected %zu got %d\n", size, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (buf[0] != report_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		hid_err(hdev, "Invalid reportID received, expected %d got %d\n", report_id, buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (hdev->bus == BUS_BLUETOOTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		/* Last 4 bytes contains crc32. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		uint8_t crc_offset = size - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			hid_err(hdev, "CRC check failed for reportID=%d\n", report_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		int gyro_range, int gyro_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	struct input_dev *sensors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	sensors = ps_allocate_input_dev(hdev, "Motion Sensors");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	if (IS_ERR(sensors))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		return ERR_CAST(sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	__set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	__set_bit(EV_MSC, sensors->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	__set_bit(MSC_TIMESTAMP, sensors->mscbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	/* Accelerometer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	input_set_abs_params(sensors, ABS_X, -accel_range, accel_range, 16, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	input_set_abs_params(sensors, ABS_Y, -accel_range, accel_range, 16, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	input_set_abs_params(sensors, ABS_Z, -accel_range, accel_range, 16, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	input_abs_set_res(sensors, ABS_X, accel_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	input_abs_set_res(sensors, ABS_Y, accel_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	input_abs_set_res(sensors, ABS_Z, accel_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	/* Gyroscope */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	input_set_abs_params(sensors, ABS_RX, -gyro_range, gyro_range, 16, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	input_set_abs_params(sensors, ABS_RY, -gyro_range, gyro_range, 16, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	input_set_abs_params(sensors, ABS_RZ, -gyro_range, gyro_range, 16, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	input_abs_set_res(sensors, ABS_RX, gyro_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	input_abs_set_res(sensors, ABS_RY, gyro_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	input_abs_set_res(sensors, ABS_RZ, gyro_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	ret = input_register_device(sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	return sensors;
^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 struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, int height,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		unsigned int num_contacts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	struct input_dev *touchpad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	touchpad = ps_allocate_input_dev(hdev, "Touchpad");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (IS_ERR(touchpad))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		return ERR_CAST(touchpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	/* Map button underneath touchpad to BTN_LEFT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	input_set_capability(touchpad, EV_KEY, BTN_LEFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	__set_bit(INPUT_PROP_BUTTONPAD, touchpad->propbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	input_set_abs_params(touchpad, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	input_set_abs_params(touchpad, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	ret = input_mt_init_slots(touchpad, num_contacts, INPUT_MT_POINTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	ret = input_register_device(touchpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	return touchpad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) static ssize_t firmware_version_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 				struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 				*attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	struct ps_device *ps_dev = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	return sysfs_emit(buf, "0x%08x\n", ps_dev->fw_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) static DEVICE_ATTR_RO(firmware_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) static ssize_t hardware_version_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 				struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 				*attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	struct ps_device *ps_dev = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	return sysfs_emit(buf, "0x%08x\n", ps_dev->hw_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) static DEVICE_ATTR_RO(hardware_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) static struct attribute *ps_device_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	&dev_attr_firmware_version.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	&dev_attr_hardware_version.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) static const struct attribute_group ps_device_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	.attrs = ps_device_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) static int dualsense_get_calibration_data(struct dualsense *ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	short gyro_speed_plus, gyro_speed_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	short acc_x_plus, acc_x_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	short acc_y_plus, acc_y_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	short acc_z_plus, acc_z_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	int speed_2x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	int range_2g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	uint8_t *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_CALIBRATION, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			DS_FEATURE_REPORT_CALIBRATION_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		hid_err(ds->base.hdev, "Failed to retrieve DualSense calibration info: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	gyro_pitch_bias  = get_unaligned_le16(&buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	gyro_yaw_bias    = get_unaligned_le16(&buf[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	gyro_roll_bias   = get_unaligned_le16(&buf[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	gyro_pitch_plus  = get_unaligned_le16(&buf[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	gyro_pitch_minus = get_unaligned_le16(&buf[9]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	gyro_yaw_plus    = get_unaligned_le16(&buf[11]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	gyro_yaw_minus   = get_unaligned_le16(&buf[13]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	gyro_roll_plus   = get_unaligned_le16(&buf[15]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	gyro_roll_minus  = get_unaligned_le16(&buf[17]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	gyro_speed_plus  = get_unaligned_le16(&buf[19]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	gyro_speed_minus = get_unaligned_le16(&buf[21]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	acc_x_plus       = get_unaligned_le16(&buf[23]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	acc_x_minus      = get_unaligned_le16(&buf[25]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	acc_y_plus       = get_unaligned_le16(&buf[27]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	acc_y_minus      = get_unaligned_le16(&buf[29]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	acc_z_plus       = get_unaligned_le16(&buf[31]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	acc_z_minus      = get_unaligned_le16(&buf[33]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	 * Set gyroscope calibration and normalization parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	 * Data values will be normalized to 1/DS_GYRO_RES_PER_DEG_S degree/s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	speed_2x = (gyro_speed_plus + gyro_speed_minus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	ds->gyro_calib_data[0].abs_code = ABS_RX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	ds->gyro_calib_data[0].bias = gyro_pitch_bias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	ds->gyro_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	ds->gyro_calib_data[1].abs_code = ABS_RY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	ds->gyro_calib_data[1].bias = gyro_yaw_bias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	ds->gyro_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	ds->gyro_calib_data[2].abs_code = ABS_RZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	ds->gyro_calib_data[2].bias = gyro_roll_bias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	ds->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	 * Set accelerometer calibration and normalization parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	 * Data values will be normalized to 1/DS_ACC_RES_PER_G g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	range_2g = acc_x_plus - acc_x_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	ds->accel_calib_data[0].abs_code = ABS_X;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	ds->accel_calib_data[0].bias = acc_x_plus - range_2g / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	ds->accel_calib_data[0].sens_numer = 2*DS_ACC_RES_PER_G;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	ds->accel_calib_data[0].sens_denom = range_2g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	range_2g = acc_y_plus - acc_y_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	ds->accel_calib_data[1].abs_code = ABS_Y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	ds->accel_calib_data[1].bias = acc_y_plus - range_2g / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	ds->accel_calib_data[1].sens_numer = 2*DS_ACC_RES_PER_G;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	ds->accel_calib_data[1].sens_denom = range_2g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	range_2g = acc_z_plus - acc_z_minus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	ds->accel_calib_data[2].abs_code = ABS_Z;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	ds->accel_calib_data[2].bias = acc_z_plus - range_2g / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	ds->accel_calib_data[2].sens_denom = range_2g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) static int dualsense_get_firmware_info(struct dualsense *ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	uint8_t *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	buf = kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_FIRMWARE_INFO, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		hid_err(ds->base.hdev, "Failed to retrieve DualSense firmware info: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	ds->base.hw_version = get_unaligned_le32(&buf[24]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	ds->base.fw_version = get_unaligned_le32(&buf[28]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) static int dualsense_get_mac_address(struct dualsense *ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	uint8_t *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	buf = kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_PAIRING_INFO, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			DS_FEATURE_REPORT_PAIRING_INFO_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		hid_err(ds->base.hdev, "Failed to retrieve DualSense pairing info: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	memcpy(ds->base.mac_address, &buf[1], sizeof(ds->base.mac_address));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		void *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	struct hid_device *hdev = ds->base.hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	if (hdev->bus == BUS_BLUETOOTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		struct dualsense_output_report_bt *bt = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		memset(bt, 0, sizeof(*bt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		bt->report_id = DS_OUTPUT_REPORT_BT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		bt->tag = DS_OUTPUT_TAG; /* Tag must be set. Exact meaning is unclear. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		 * Highest 4-bit is a sequence number, which needs to be increased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		 * every report. Lowest 4-bit is tag and can be zero for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		bt->seq_tag = (ds->output_seq << 4) | 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		if (++ds->output_seq == 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 			ds->output_seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		rp->data = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		rp->len = sizeof(*bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		rp->bt = bt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		rp->usb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		rp->common = &bt->common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	} else { /* USB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		struct dualsense_output_report_usb *usb = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		memset(usb, 0, sizeof(*usb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		usb->report_id = DS_OUTPUT_REPORT_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		rp->data = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		rp->len = sizeof(*usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		rp->bt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		rp->usb = usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		rp->common = &usb->common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  * Helper function to send DualSense output reports. Applies a CRC at the end of a report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  * for Bluetooth reports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) static void dualsense_send_output_report(struct dualsense *ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		struct dualsense_output_report *report)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	struct hid_device *hdev = ds->base.hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	/* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (report->bt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		uint32_t crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		uint8_t seed = PS_OUTPUT_CRC32_SEED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		crc = crc32_le(0xFFFFFFFF, &seed, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		crc = ~crc32_le(crc, report->data, report->len - 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		report->bt->crc32 = cpu_to_le32(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	hid_hw_output_report(hdev, report->data, report->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) static void dualsense_output_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct dualsense *ds = container_of(work, struct dualsense, output_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	struct dualsense_output_report report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	struct dualsense_output_report_common *common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	dualsense_init_output_report(ds, &report, ds->output_report_dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	common = report.common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	spin_lock_irqsave(&ds->base.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	if (ds->update_rumble) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		/* Select classic rumble style haptics and enable it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		common->motor_left = ds->motor_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		common->motor_right = ds->motor_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		ds->update_rumble = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	if (ds->update_lightbar) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		common->lightbar_red = ds->lightbar_red;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		common->lightbar_green = ds->lightbar_green;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		common->lightbar_blue = ds->lightbar_blue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		ds->update_lightbar = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	if (ds->update_player_leds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		common->player_leds = ds->player_leds_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		ds->update_player_leds = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (ds->update_mic_mute) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		common->mute_button_led = ds->mic_muted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		if (ds->mic_muted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			/* Disable microphone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			common->power_save_control |= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			/* Enable microphone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			common->power_save_control &= ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		ds->update_mic_mute = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	spin_unlock_irqrestore(&ds->base.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	dualsense_send_output_report(ds, &report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		u8 *data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	struct hid_device *hdev = ps_dev->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	struct dualsense *ds = container_of(ps_dev, struct dualsense, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	struct dualsense_input_report *ds_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	uint8_t battery_data, battery_capacity, charging_status, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	int battery_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	uint32_t sensor_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	bool btn_mic_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	 * DualSense in USB uses the full HID report for reportID 1, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	 * Bluetooth uses a minimal HID report for reportID 1 and reports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	 * the full report using reportID 49.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 			size == DS_INPUT_REPORT_USB_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		ds_report = (struct dualsense_input_report *)&data[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	} else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			size == DS_INPUT_REPORT_BT_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		/* Last 4 bytes of input report contain crc32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		uint32_t report_crc = get_unaligned_le32(&data[size - 4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			hid_err(hdev, "DualSense input CRC's check failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		ds_report = (struct dualsense_input_report *)&data[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		hid_err(hdev, "Unhandled reportID=%d\n", report->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	input_report_abs(ds->gamepad, ABS_X,  ds_report->x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	input_report_abs(ds->gamepad, ABS_Y,  ds_report->y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	input_report_abs(ds->gamepad, ABS_RX, ds_report->rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	input_report_abs(ds->gamepad, ABS_RY, ds_report->ry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	input_report_abs(ds->gamepad, ABS_Z,  ds_report->z);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	input_report_abs(ds->gamepad, ABS_RZ, ds_report->rz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	value = ds_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (value >= ARRAY_SIZE(ps_gamepad_hat_mapping))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		value = 8; /* center */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	input_report_abs(ds->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	input_report_abs(ds->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	input_report_key(ds->gamepad, BTN_WEST,   ds_report->buttons[0] & DS_BUTTONS0_SQUARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	input_report_key(ds->gamepad, BTN_SOUTH,  ds_report->buttons[0] & DS_BUTTONS0_CROSS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	input_report_key(ds->gamepad, BTN_EAST,   ds_report->buttons[0] & DS_BUTTONS0_CIRCLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	input_report_key(ds->gamepad, BTN_NORTH,  ds_report->buttons[0] & DS_BUTTONS0_TRIANGLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	input_report_key(ds->gamepad, BTN_TL,     ds_report->buttons[1] & DS_BUTTONS1_L1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	input_report_key(ds->gamepad, BTN_TR,     ds_report->buttons[1] & DS_BUTTONS1_R1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	input_report_key(ds->gamepad, BTN_TL2,    ds_report->buttons[1] & DS_BUTTONS1_L2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	input_report_key(ds->gamepad, BTN_TR2,    ds_report->buttons[1] & DS_BUTTONS1_R2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	input_report_key(ds->gamepad, BTN_SELECT, ds_report->buttons[1] & DS_BUTTONS1_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	input_report_key(ds->gamepad, BTN_START,  ds_report->buttons[1] & DS_BUTTONS1_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	input_report_key(ds->gamepad, BTN_THUMBL, ds_report->buttons[1] & DS_BUTTONS1_L3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	input_report_key(ds->gamepad, BTN_THUMBR, ds_report->buttons[1] & DS_BUTTONS1_R3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	input_report_key(ds->gamepad, BTN_MODE,   ds_report->buttons[2] & DS_BUTTONS2_PS_HOME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	input_sync(ds->gamepad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	 * The DualSense has an internal microphone, which can be muted through a mute button
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	 * on the device. The driver is expected to read the button state and program the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	 * to mute/unmute audio at the hardware level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	btn_mic_state = !!(ds_report->buttons[2] & DS_BUTTONS2_MIC_MUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	if (btn_mic_state && !ds->last_btn_mic_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		spin_lock_irqsave(&ps_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		ds->update_mic_mute = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		ds->mic_muted = !ds->mic_muted; /* toggle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		spin_unlock_irqrestore(&ps_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		/* Schedule updating of microphone state at hardware level. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		schedule_work(&ds->output_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	ds->last_btn_mic_state = btn_mic_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	/* Parse and calibrate gyroscope data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		int raw_data = (short)le16_to_cpu(ds_report->gyro[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		int calib_data = mult_frac(ds->gyro_calib_data[i].sens_numer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 					   raw_data - ds->gyro_calib_data[i].bias,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 					   ds->gyro_calib_data[i].sens_denom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		input_report_abs(ds->sensors, ds->gyro_calib_data[i].abs_code, calib_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	/* Parse and calibrate accelerometer data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	for (i = 0; i < ARRAY_SIZE(ds_report->accel); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		int raw_data = (short)le16_to_cpu(ds_report->accel[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		int calib_data = mult_frac(ds->accel_calib_data[i].sens_numer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 					   raw_data - ds->accel_calib_data[i].bias,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 					   ds->accel_calib_data[i].sens_denom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		input_report_abs(ds->sensors, ds->accel_calib_data[i].abs_code, calib_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	/* Convert timestamp (in 0.33us unit) to timestamp_us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	sensor_timestamp = le32_to_cpu(ds_report->sensor_timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	if (!ds->sensor_timestamp_initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		ds->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		ds->sensor_timestamp_initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		uint32_t delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		if (ds->prev_sensor_timestamp > sensor_timestamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			delta = (U32_MAX - ds->prev_sensor_timestamp + sensor_timestamp + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			delta = sensor_timestamp - ds->prev_sensor_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		ds->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	ds->prev_sensor_timestamp = sensor_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	input_event(ds->sensors, EV_MSC, MSC_TIMESTAMP, ds->sensor_timestamp_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	input_sync(ds->sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	for (i = 0; i < ARRAY_SIZE(ds_report->points); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		struct dualsense_touch_point *point = &ds_report->points[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		bool active = (point->contact & DS_TOUCH_POINT_INACTIVE) ? false : true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		input_mt_slot(ds->touchpad, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		input_mt_report_slot_state(ds->touchpad, MT_TOOL_FINGER, active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			int x = (point->x_hi << 8) | point->x_lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			int y = (point->y_hi << 4) | point->y_lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			input_report_abs(ds->touchpad, ABS_MT_POSITION_X, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	input_mt_sync_frame(ds->touchpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	input_report_key(ds->touchpad, BTN_LEFT, ds_report->buttons[2] & DS_BUTTONS2_TOUCHPAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	input_sync(ds->touchpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	switch (charging_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	case 0x0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		 * Each unit of battery data corresponds to 10%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		 * 0 = 0-9%, 1 = 10-19%, .. and 10 = 100%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		battery_capacity = min(battery_data * 10 + 5, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	case 0x1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		battery_capacity = min(battery_data * 10 + 5, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		battery_status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	case 0x2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		battery_capacity = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		battery_status = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	case 0xa: /* voltage or temperature out of range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	case 0xb: /* temperature error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		battery_capacity = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	case 0xf: /* charging error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		battery_capacity = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	spin_lock_irqsave(&ps_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	ps_dev->battery_capacity = battery_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	ps_dev->battery_status = battery_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	spin_unlock_irqrestore(&ps_dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) static int dualsense_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	struct hid_device *hdev = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	struct dualsense *ds = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	if (effect->type != FF_RUMBLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	spin_lock_irqsave(&ds->base.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	ds->update_rumble = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	ds->motor_left = effect->u.rumble.strong_magnitude / 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	ds->motor_right = effect->u.rumble.weak_magnitude / 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	spin_unlock_irqrestore(&ds->base.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	schedule_work(&ds->output_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) static int dualsense_reset_leds(struct dualsense *ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	struct dualsense_output_report report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	uint8_t *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	buf = kzalloc(sizeof(struct dualsense_output_report_bt), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	dualsense_init_output_report(ds, &report, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	 * On Bluetooth the DualSense outputs an animation on the lightbar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	 * during startup and maintains a color afterwards. We need to explicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	 * reconfigure the lightbar before we can do any programming later on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	 * In USB the lightbar is not on by default, but redoing the setup there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	 * doesn't hurt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	report.common->valid_flag2 = DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	report.common->lightbar_setup = DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT; /* Fade light out. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	dualsense_send_output_report(ds, &report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	ds->update_lightbar = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	ds->lightbar_red = red;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	ds->lightbar_green = green;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	ds->lightbar_blue = blue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	schedule_work(&ds->output_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) static void dualsense_set_player_leds(struct dualsense *ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	 * The DualSense controller has a row of 5 LEDs used for player ids.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	 * Behavior on the PlayStation 5 console is to center the player id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	 * across the LEDs, so e.g. player 1 would be "--x--" with x being 'on'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	 * Follow a similar mapping here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	static const int player_ids[5] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		BIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		BIT(3) | BIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		BIT(4) | BIT(2) | BIT(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		BIT(4) | BIT(3) | BIT(1) | BIT(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	uint8_t player_id = ds->base.player_id % ARRAY_SIZE(player_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	ds->update_player_leds = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	ds->player_leds_state = player_ids[player_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	schedule_work(&ds->output_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) static struct ps_device *dualsense_create(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	struct dualsense *ds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	struct ps_device *ps_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	uint8_t max_output_report_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	ds = devm_kzalloc(&hdev->dev, sizeof(*ds), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	if (!ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	 * Patch version to allow userspace to distinguish between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	 * hid-generic vs hid-playstation axis and button mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	hdev->version |= HID_PLAYSTATION_VERSION_PATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	ps_dev = &ds->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	ps_dev->hdev = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	spin_lock_init(&ps_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	ps_dev->battery_capacity = 100; /* initial value until parse_report. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	ps_dev->parse_report = dualsense_parse_report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	INIT_WORK(&ds->output_worker, dualsense_output_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	hid_set_drvdata(hdev, ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	max_output_report_size = sizeof(struct dualsense_output_report_bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	ds->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if (!ds->output_report_dmabuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	ret = dualsense_get_mac_address(ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		hid_err(hdev, "Failed to get MAC address from DualSense\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	ret = dualsense_get_firmware_info(ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		hid_err(hdev, "Failed to get firmware info from DualSense\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	ret = ps_devices_list_add(ps_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	ret = dualsense_get_calibration_data(ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		hid_err(hdev, "Failed to get calibration data from DualSense\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	ds->gamepad = ps_gamepad_create(hdev, dualsense_play_effect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	if (IS_ERR(ds->gamepad)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		ret = PTR_ERR(ds->gamepad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	ds->sensors = ps_sensors_create(hdev, DS_ACC_RANGE, DS_ACC_RES_PER_G,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 			DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	if (IS_ERR(ds->sensors)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		ret = PTR_ERR(ds->sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	ds->touchpad = ps_touchpad_create(hdev, DS_TOUCHPAD_WIDTH, DS_TOUCHPAD_HEIGHT, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	if (IS_ERR(ds->touchpad)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		ret = PTR_ERR(ds->touchpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	ret = ps_device_register_battery(ps_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	 * The hardware may have control over the LEDs (e.g. in Bluetooth on startup).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	 * Reset the LEDs (lightbar, mute, player leds), so we can control them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	 * from software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	ret = dualsense_reset_leds(ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	dualsense_set_lightbar(ds, 0, 0, 128); /* blue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	ret = ps_device_set_player_id(ps_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		hid_err(hdev, "Failed to assign player id for DualSense: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	/* Set player LEDs to our player id. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	dualsense_set_player_leds(ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	 * Reporting hardware and firmware is important as there are frequent updates, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	 * can change behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	hid_info(hdev, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			ds->base.hw_version, ds->base.fw_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	return &ds->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	ps_devices_list_remove(ps_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) static int ps_raw_event(struct hid_device *hdev, struct hid_report *report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		u8 *data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	struct ps_device *dev = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	if (dev && dev->parse_report)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		return dev->parse_report(dev, report, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	struct ps_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	ret = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		hid_err(hdev, "Parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		hid_err(hdev, "Failed to start HID device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	ret = hid_hw_open(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		hid_err(hdev, "Failed to open HID device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		goto err_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		dev = dualsense_create(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		if (IS_ERR(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 			hid_err(hdev, "Failed to create dualsense.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			ret = PTR_ERR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			goto err_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	ret = devm_device_add_group(&hdev->dev, &ps_device_attribute_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		hid_err(hdev, "Failed to register sysfs nodes.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		goto err_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) err_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	hid_hw_close(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) err_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) static void ps_remove(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	struct ps_device *dev = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	ps_devices_list_remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	ps_device_release_player_id(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	hid_hw_close(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) static const struct hid_device_id ps_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) MODULE_DEVICE_TABLE(hid, ps_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) static struct hid_driver ps_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	.name		= "playstation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	.id_table	= ps_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	.probe		= ps_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	.remove		= ps_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	.raw_event	= ps_raw_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) static int __init ps_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	return hid_register_driver(&ps_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) static void __exit ps_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	hid_unregister_driver(&ps_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	ida_destroy(&ps_player_id_allocator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) module_init(ps_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) module_exit(ps_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) MODULE_AUTHOR("Sony Interactive Entertainment");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) MODULE_DESCRIPTION("HID Driver for PlayStation peripherals.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) MODULE_LICENSE("GPL");