1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HID driver for Sony DualSense(TM) controller.
5 * Copyright (c) 2020-2022 Sony Interactive Entertainment
8 #include <linux/bits.h>
9 #include <linux/crc32.h>
10 #include <linux/device.h>
11 #include <linux/hid.h>
12 #include <linux/idr.h>
13 #include <linux/input/mt.h>
14 #include <linux/leds.h>
15 #include <linux/led-class-multicolor.h>
16 #include <linux/module.h>
18 #include <linux/unaligned.h>
22 /* List of connected playstation devices. */
23 static DEFINE_MUTEX(ps_devices_lock
);
24 static LIST_HEAD(ps_devices_list
);
26 static DEFINE_IDA(ps_player_id_allocator
);
28 #define HID_PLAYSTATION_VERSION_PATCH 0x8000
31 PS_TYPE_PS4_DUALSHOCK4
,
32 PS_TYPE_PS5_DUALSENSE
,
35 /* Base class for playstation devices. */
37 struct list_head list
;
38 struct hid_device
*hdev
;
43 struct power_supply_desc battery_desc
;
44 struct power_supply
*battery
;
45 uint8_t battery_capacity
;
48 const char *input_dev_name
; /* Name of primary input device. */
49 uint8_t mac_address
[6]; /* Note: stored in little endian order. */
53 int (*parse_report
)(struct ps_device
*dev
, struct hid_report
*report
, u8
*data
, int size
);
54 void (*remove
)(struct ps_device
*dev
);
57 /* Calibration data for playstation motion sensors. */
58 struct ps_calibration_data
{
69 enum led_brightness (*brightness_get
)(struct led_classdev
*cdev
);
70 int (*brightness_set
)(struct led_classdev
*cdev
, enum led_brightness
);
71 int (*blink_set
)(struct led_classdev
*led
, unsigned long *on
, unsigned long *off
);
74 /* Seed values for DualShock4 / DualSense CRC32 for different report types. */
75 #define PS_INPUT_CRC32_SEED 0xA1
76 #define PS_OUTPUT_CRC32_SEED 0xA2
77 #define PS_FEATURE_CRC32_SEED 0xA3
79 #define DS_INPUT_REPORT_USB 0x01
80 #define DS_INPUT_REPORT_USB_SIZE 64
81 #define DS_INPUT_REPORT_BT 0x31
82 #define DS_INPUT_REPORT_BT_SIZE 78
83 #define DS_OUTPUT_REPORT_USB 0x02
84 #define DS_OUTPUT_REPORT_USB_SIZE 63
85 #define DS_OUTPUT_REPORT_BT 0x31
86 #define DS_OUTPUT_REPORT_BT_SIZE 78
88 #define DS_FEATURE_REPORT_CALIBRATION 0x05
89 #define DS_FEATURE_REPORT_CALIBRATION_SIZE 41
90 #define DS_FEATURE_REPORT_PAIRING_INFO 0x09
91 #define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20
92 #define DS_FEATURE_REPORT_FIRMWARE_INFO 0x20
93 #define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE 64
95 /* Button masks for DualSense input report. */
96 #define DS_BUTTONS0_HAT_SWITCH GENMASK(3, 0)
97 #define DS_BUTTONS0_SQUARE BIT(4)
98 #define DS_BUTTONS0_CROSS BIT(5)
99 #define DS_BUTTONS0_CIRCLE BIT(6)
100 #define DS_BUTTONS0_TRIANGLE BIT(7)
101 #define DS_BUTTONS1_L1 BIT(0)
102 #define DS_BUTTONS1_R1 BIT(1)
103 #define DS_BUTTONS1_L2 BIT(2)
104 #define DS_BUTTONS1_R2 BIT(3)
105 #define DS_BUTTONS1_CREATE BIT(4)
106 #define DS_BUTTONS1_OPTIONS BIT(5)
107 #define DS_BUTTONS1_L3 BIT(6)
108 #define DS_BUTTONS1_R3 BIT(7)
109 #define DS_BUTTONS2_PS_HOME BIT(0)
110 #define DS_BUTTONS2_TOUCHPAD BIT(1)
111 #define DS_BUTTONS2_MIC_MUTE BIT(2)
113 /* Status field of DualSense input report. */
114 #define DS_STATUS_BATTERY_CAPACITY GENMASK(3, 0)
115 #define DS_STATUS_CHARGING GENMASK(7, 4)
116 #define DS_STATUS_CHARGING_SHIFT 4
118 /* Feature version from DualSense Firmware Info report. */
119 #define DS_FEATURE_VERSION(major, minor) ((major & 0xff) << 8 | (minor & 0xff))
122 * Status of a DualSense touch point contact.
123 * Contact IDs, with highest bit set are 'inactive'
124 * and any associated data is then invalid.
126 #define DS_TOUCH_POINT_INACTIVE BIT(7)
128 /* Magic value required in tag field of Bluetooth output report. */
129 #define DS_OUTPUT_TAG 0x10
130 /* Flags for DualSense output report. */
131 #define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0)
132 #define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1)
133 #define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0)
134 #define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1)
135 #define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2)
136 #define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3)
137 #define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4)
138 #define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1)
139 #define DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2 BIT(2)
140 #define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4)
141 #define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1)
143 /* DualSense hardware limits */
144 #define DS_ACC_RES_PER_G 8192
145 #define DS_ACC_RANGE (4*DS_ACC_RES_PER_G)
146 #define DS_GYRO_RES_PER_DEG_S 1024
147 #define DS_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S)
148 #define DS_TOUCHPAD_WIDTH 1920
149 #define DS_TOUCHPAD_HEIGHT 1080
152 struct ps_device base
;
153 struct input_dev
*gamepad
;
154 struct input_dev
*sensors
;
155 struct input_dev
*touchpad
;
157 /* Update version is used as a feature/capability version. */
158 uint16_t update_version
;
160 /* Calibration data for accelerometer and gyroscope. */
161 struct ps_calibration_data accel_calib_data
[3];
162 struct ps_calibration_data gyro_calib_data
[3];
164 /* Timestamp for sensor data */
165 bool sensor_timestamp_initialized
;
166 uint32_t prev_sensor_timestamp
;
167 uint32_t sensor_timestamp_us
;
169 /* Compatible rumble state */
170 bool use_vibration_v2
;
176 struct led_classdev_mc lightbar
;
177 bool update_lightbar
;
178 uint8_t lightbar_red
;
179 uint8_t lightbar_green
;
180 uint8_t lightbar_blue
;
183 bool update_mic_mute
;
185 bool last_btn_mic_state
;
188 bool update_player_leds
;
189 uint8_t player_leds_state
;
190 struct led_classdev player_leds
[5];
192 struct work_struct output_worker
;
193 bool output_worker_initialized
;
194 void *output_report_dmabuf
;
195 uint8_t output_seq
; /* Sequence number for output report. */
198 struct dualsense_touch_point
{
201 uint8_t x_hi
:4, y_lo
:4;
204 static_assert(sizeof(struct dualsense_touch_point
) == 4);
206 /* Main DualSense input report excluding any BT/USB specific headers. */
207 struct dualsense_input_report
{
216 __le16 gyro
[3]; /* x, y, z */
217 __le16 accel
[3]; /* x, y, z */
218 __le32 sensor_timestamp
;
222 struct dualsense_touch_point points
[2];
224 uint8_t reserved3
[12];
226 uint8_t reserved4
[10];
228 /* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */
229 static_assert(sizeof(struct dualsense_input_report
) == DS_INPUT_REPORT_USB_SIZE
- 1);
231 /* Common data between DualSense BT/USB main output report. */
232 struct dualsense_output_report_common
{
236 /* For DualShock 4 compatibility mode. */
242 uint8_t mute_button_led
;
244 uint8_t power_save_control
;
245 uint8_t reserved2
[28];
247 /* LEDs and lightbar */
249 uint8_t reserved3
[2];
250 uint8_t lightbar_setup
;
251 uint8_t led_brightness
;
253 uint8_t lightbar_red
;
254 uint8_t lightbar_green
;
255 uint8_t lightbar_blue
;
257 static_assert(sizeof(struct dualsense_output_report_common
) == 47);
259 struct dualsense_output_report_bt
{
260 uint8_t report_id
; /* 0x31 */
263 struct dualsense_output_report_common common
;
264 uint8_t reserved
[24];
267 static_assert(sizeof(struct dualsense_output_report_bt
) == DS_OUTPUT_REPORT_BT_SIZE
);
269 struct dualsense_output_report_usb
{
270 uint8_t report_id
; /* 0x02 */
271 struct dualsense_output_report_common common
;
272 uint8_t reserved
[15];
274 static_assert(sizeof(struct dualsense_output_report_usb
) == DS_OUTPUT_REPORT_USB_SIZE
);
277 * The DualSense has a main output report used to control most features. It is
278 * largely the same between Bluetooth and USB except for different headers and CRC.
279 * This structure hide the differences between the two to simplify sending output reports.
281 struct dualsense_output_report
{
282 uint8_t *data
; /* Start of data */
283 uint8_t len
; /* Size of output report */
285 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
286 struct dualsense_output_report_bt
*bt
;
287 /* Points to USB data payload in case for a USB report else NULL. */
288 struct dualsense_output_report_usb
*usb
;
289 /* Points to common section of report, so past any headers. */
290 struct dualsense_output_report_common
*common
;
293 #define DS4_INPUT_REPORT_USB 0x01
294 #define DS4_INPUT_REPORT_USB_SIZE 64
295 #define DS4_INPUT_REPORT_BT_MINIMAL 0x01
296 #define DS4_INPUT_REPORT_BT_MINIMAL_SIZE 10
297 #define DS4_INPUT_REPORT_BT 0x11
298 #define DS4_INPUT_REPORT_BT_SIZE 78
299 #define DS4_OUTPUT_REPORT_USB 0x05
300 #define DS4_OUTPUT_REPORT_USB_SIZE 32
301 #define DS4_OUTPUT_REPORT_BT 0x11
302 #define DS4_OUTPUT_REPORT_BT_SIZE 78
304 #define DS4_FEATURE_REPORT_CALIBRATION 0x02
305 #define DS4_FEATURE_REPORT_CALIBRATION_SIZE 37
306 #define DS4_FEATURE_REPORT_CALIBRATION_BT 0x05
307 #define DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE 41
308 #define DS4_FEATURE_REPORT_FIRMWARE_INFO 0xa3
309 #define DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE 49
310 #define DS4_FEATURE_REPORT_PAIRING_INFO 0x12
311 #define DS4_FEATURE_REPORT_PAIRING_INFO_SIZE 16
314 * Status of a DualShock4 touch point contact.
315 * Contact IDs, with highest bit set are 'inactive'
316 * and any associated data is then invalid.
318 #define DS4_TOUCH_POINT_INACTIVE BIT(7)
320 /* Status field of DualShock4 input report. */
321 #define DS4_STATUS0_BATTERY_CAPACITY GENMASK(3, 0)
322 #define DS4_STATUS0_CABLE_STATE BIT(4)
323 /* Battery status within batery_status field. */
324 #define DS4_BATTERY_STATUS_FULL 11
325 /* Status1 bit2 contains dongle connection state:
329 #define DS4_STATUS1_DONGLE_STATE BIT(2)
331 /* The lower 6 bits of hw_control of the Bluetooth main output report
332 * control the interval at which Dualshock 4 reports data:
339 #define DS4_OUTPUT_HWCTL_BT_POLL_MASK 0x3F
340 /* Default to 4ms poll interval, which is same as USB (not adjustable). */
341 #define DS4_BT_DEFAULT_POLL_INTERVAL_MS 4
342 #define DS4_OUTPUT_HWCTL_CRC32 0x40
343 #define DS4_OUTPUT_HWCTL_HID 0x80
345 /* Flags for DualShock4 output report. */
346 #define DS4_OUTPUT_VALID_FLAG0_MOTOR 0x01
347 #define DS4_OUTPUT_VALID_FLAG0_LED 0x02
348 #define DS4_OUTPUT_VALID_FLAG0_LED_BLINK 0x04
350 /* DualShock4 hardware limits */
351 #define DS4_ACC_RES_PER_G 8192
352 #define DS4_ACC_RANGE (4*DS_ACC_RES_PER_G)
353 #define DS4_GYRO_RES_PER_DEG_S 1024
354 #define DS4_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S)
355 #define DS4_LIGHTBAR_MAX_BLINK 255 /* 255 centiseconds */
356 #define DS4_TOUCHPAD_WIDTH 1920
357 #define DS4_TOUCHPAD_HEIGHT 942
359 enum dualshock4_dongle_state
{
367 struct ps_device base
;
368 struct input_dev
*gamepad
;
369 struct input_dev
*sensors
;
370 struct input_dev
*touchpad
;
372 /* Calibration data for accelerometer and gyroscope. */
373 struct ps_calibration_data accel_calib_data
[3];
374 struct ps_calibration_data gyro_calib_data
[3];
376 /* Only used on dongle to track state transitions. */
377 enum dualshock4_dongle_state dongle_state
;
378 /* Used during calibration. */
379 struct work_struct dongle_hotplug_worker
;
381 /* Timestamp for sensor data */
382 bool sensor_timestamp_initialized
;
383 uint32_t prev_sensor_timestamp
;
384 uint32_t sensor_timestamp_us
;
386 /* Bluetooth poll interval */
387 bool update_bt_poll_interval
;
388 uint8_t bt_poll_interval
;
395 bool update_lightbar
;
396 bool update_lightbar_blink
;
397 bool lightbar_enabled
; /* For use by global LED control. */
398 uint8_t lightbar_red
;
399 uint8_t lightbar_green
;
400 uint8_t lightbar_blue
;
401 uint8_t lightbar_blink_on
; /* In increments of 10ms. */
402 uint8_t lightbar_blink_off
; /* In increments of 10ms. */
403 struct led_classdev lightbar_leds
[4];
405 struct work_struct output_worker
;
406 bool output_worker_initialized
;
407 void *output_report_dmabuf
;
410 struct dualshock4_touch_point
{
413 uint8_t x_hi
:4, y_lo
:4;
416 static_assert(sizeof(struct dualshock4_touch_point
) == 4);
418 struct dualshock4_touch_report
{
420 struct dualshock4_touch_point points
[2];
422 static_assert(sizeof(struct dualshock4_touch_report
) == 9);
424 /* Main DualShock4 input report excluding any BT/USB specific headers. */
425 struct dualshock4_input_report_common
{
432 __le16 sensor_timestamp
;
433 uint8_t sensor_temperature
;
434 __le16 gyro
[3]; /* x, y, z */
435 __le16 accel
[3]; /* x, y, z */
436 uint8_t reserved2
[5];
441 static_assert(sizeof(struct dualshock4_input_report_common
) == 32);
443 struct dualshock4_input_report_usb
{
444 uint8_t report_id
; /* 0x01 */
445 struct dualshock4_input_report_common common
;
446 uint8_t num_touch_reports
;
447 struct dualshock4_touch_report touch_reports
[3];
450 static_assert(sizeof(struct dualshock4_input_report_usb
) == DS4_INPUT_REPORT_USB_SIZE
);
452 struct dualshock4_input_report_bt
{
453 uint8_t report_id
; /* 0x11 */
455 struct dualshock4_input_report_common common
;
456 uint8_t num_touch_reports
;
457 struct dualshock4_touch_report touch_reports
[4]; /* BT has 4 compared to 3 for USB */
458 uint8_t reserved2
[2];
461 static_assert(sizeof(struct dualshock4_input_report_bt
) == DS4_INPUT_REPORT_BT_SIZE
);
463 /* Common data between Bluetooth and USB DualShock4 output reports. */
464 struct dualshock4_output_report_common
{
473 uint8_t lightbar_red
;
474 uint8_t lightbar_green
;
475 uint8_t lightbar_blue
;
476 uint8_t lightbar_blink_on
;
477 uint8_t lightbar_blink_off
;
480 struct dualshock4_output_report_usb
{
481 uint8_t report_id
; /* 0x5 */
482 struct dualshock4_output_report_common common
;
483 uint8_t reserved
[21];
485 static_assert(sizeof(struct dualshock4_output_report_usb
) == DS4_OUTPUT_REPORT_USB_SIZE
);
487 struct dualshock4_output_report_bt
{
488 uint8_t report_id
; /* 0x11 */
490 uint8_t audio_control
;
491 struct dualshock4_output_report_common common
;
492 uint8_t reserved
[61];
495 static_assert(sizeof(struct dualshock4_output_report_bt
) == DS4_OUTPUT_REPORT_BT_SIZE
);
498 * The DualShock4 has a main output report used to control most features. It is
499 * largely the same between Bluetooth and USB except for different headers and CRC.
500 * This structure hide the differences between the two to simplify sending output reports.
502 struct dualshock4_output_report
{
503 uint8_t *data
; /* Start of data */
504 uint8_t len
; /* Size of output report */
506 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
507 struct dualshock4_output_report_bt
*bt
;
508 /* Points to USB data payload in case for a USB report else NULL. */
509 struct dualshock4_output_report_usb
*usb
;
510 /* Points to common section of report, so past any headers. */
511 struct dualshock4_output_report_common
*common
;
515 * Common gamepad buttons across DualShock 3 / 4 and DualSense.
516 * Note: for device with a touchpad, touchpad button is not included
517 * as it will be part of the touchpad device.
519 static const int ps_gamepad_buttons
[] = {
520 BTN_WEST
, /* Square */
521 BTN_NORTH
, /* Triangle */
522 BTN_EAST
, /* Circle */
523 BTN_SOUTH
, /* Cross */
528 BTN_SELECT
, /* Create (PS5) / Share (PS4) */
529 BTN_START
, /* Option */
532 BTN_MODE
, /* PS Home */
535 static const struct {int x
; int y
; } ps_gamepad_hat_mapping
[] = {
536 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
540 static int dualshock4_get_calibration_data(struct dualshock4
*ds4
);
541 static inline void dualsense_schedule_work(struct dualsense
*ds
);
542 static inline void dualshock4_schedule_work(struct dualshock4
*ds4
);
543 static void dualsense_set_lightbar(struct dualsense
*ds
, uint8_t red
, uint8_t green
, uint8_t blue
);
544 static void dualshock4_set_default_lightbar_colors(struct dualshock4
*ds4
);
547 * Add a new ps_device to ps_devices if it doesn't exist.
548 * Return error on duplicate device, which can happen if the same
549 * device is connected using both Bluetooth and USB.
551 static int ps_devices_list_add(struct ps_device
*dev
)
553 struct ps_device
*entry
;
555 mutex_lock(&ps_devices_lock
);
556 list_for_each_entry(entry
, &ps_devices_list
, list
) {
557 if (!memcmp(entry
->mac_address
, dev
->mac_address
, sizeof(dev
->mac_address
))) {
558 hid_err(dev
->hdev
, "Duplicate device found for MAC address %pMR.\n",
560 mutex_unlock(&ps_devices_lock
);
565 list_add_tail(&dev
->list
, &ps_devices_list
);
566 mutex_unlock(&ps_devices_lock
);
570 static int ps_devices_list_remove(struct ps_device
*dev
)
572 mutex_lock(&ps_devices_lock
);
573 list_del(&dev
->list
);
574 mutex_unlock(&ps_devices_lock
);
578 static int ps_device_set_player_id(struct ps_device
*dev
)
580 int ret
= ida_alloc(&ps_player_id_allocator
, GFP_KERNEL
);
585 dev
->player_id
= ret
;
589 static void ps_device_release_player_id(struct ps_device
*dev
)
591 ida_free(&ps_player_id_allocator
, dev
->player_id
);
593 dev
->player_id
= U32_MAX
;
596 static struct input_dev
*ps_allocate_input_dev(struct hid_device
*hdev
, const char *name_suffix
)
598 struct input_dev
*input_dev
;
600 input_dev
= devm_input_allocate_device(&hdev
->dev
);
602 return ERR_PTR(-ENOMEM
);
604 input_dev
->id
.bustype
= hdev
->bus
;
605 input_dev
->id
.vendor
= hdev
->vendor
;
606 input_dev
->id
.product
= hdev
->product
;
607 input_dev
->id
.version
= hdev
->version
;
608 input_dev
->uniq
= hdev
->uniq
;
611 input_dev
->name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
, "%s %s", hdev
->name
,
613 if (!input_dev
->name
)
614 return ERR_PTR(-ENOMEM
);
616 input_dev
->name
= hdev
->name
;
619 input_set_drvdata(input_dev
, hdev
);
624 static enum power_supply_property ps_power_supply_props
[] = {
625 POWER_SUPPLY_PROP_STATUS
,
626 POWER_SUPPLY_PROP_PRESENT
,
627 POWER_SUPPLY_PROP_CAPACITY
,
628 POWER_SUPPLY_PROP_SCOPE
,
631 static int ps_battery_get_property(struct power_supply
*psy
,
632 enum power_supply_property psp
,
633 union power_supply_propval
*val
)
635 struct ps_device
*dev
= power_supply_get_drvdata(psy
);
636 uint8_t battery_capacity
;
641 spin_lock_irqsave(&dev
->lock
, flags
);
642 battery_capacity
= dev
->battery_capacity
;
643 battery_status
= dev
->battery_status
;
644 spin_unlock_irqrestore(&dev
->lock
, flags
);
647 case POWER_SUPPLY_PROP_STATUS
:
648 val
->intval
= battery_status
;
650 case POWER_SUPPLY_PROP_PRESENT
:
653 case POWER_SUPPLY_PROP_CAPACITY
:
654 val
->intval
= battery_capacity
;
656 case POWER_SUPPLY_PROP_SCOPE
:
657 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
667 static int ps_device_register_battery(struct ps_device
*dev
)
669 struct power_supply
*battery
;
670 struct power_supply_config battery_cfg
= { .drv_data
= dev
};
673 dev
->battery_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
674 dev
->battery_desc
.properties
= ps_power_supply_props
;
675 dev
->battery_desc
.num_properties
= ARRAY_SIZE(ps_power_supply_props
);
676 dev
->battery_desc
.get_property
= ps_battery_get_property
;
677 dev
->battery_desc
.name
= devm_kasprintf(&dev
->hdev
->dev
, GFP_KERNEL
,
678 "ps-controller-battery-%pMR", dev
->mac_address
);
679 if (!dev
->battery_desc
.name
)
682 battery
= devm_power_supply_register(&dev
->hdev
->dev
, &dev
->battery_desc
, &battery_cfg
);
683 if (IS_ERR(battery
)) {
684 ret
= PTR_ERR(battery
);
685 hid_err(dev
->hdev
, "Unable to register battery device: %d\n", ret
);
688 dev
->battery
= battery
;
690 ret
= power_supply_powers(dev
->battery
, &dev
->hdev
->dev
);
692 hid_err(dev
->hdev
, "Unable to activate battery device: %d\n", ret
);
699 /* Compute crc32 of HID data and compare against expected CRC. */
700 static bool ps_check_crc32(uint8_t seed
, uint8_t *data
, size_t len
, uint32_t report_crc
)
704 crc
= crc32_le(0xFFFFFFFF, &seed
, 1);
705 crc
= ~crc32_le(crc
, data
, len
);
707 return crc
== report_crc
;
710 static struct input_dev
*ps_gamepad_create(struct hid_device
*hdev
,
711 int (*play_effect
)(struct input_dev
*, void *, struct ff_effect
*))
713 struct input_dev
*gamepad
;
717 gamepad
= ps_allocate_input_dev(hdev
, NULL
);
719 return ERR_CAST(gamepad
);
721 input_set_abs_params(gamepad
, ABS_X
, 0, 255, 0, 0);
722 input_set_abs_params(gamepad
, ABS_Y
, 0, 255, 0, 0);
723 input_set_abs_params(gamepad
, ABS_Z
, 0, 255, 0, 0);
724 input_set_abs_params(gamepad
, ABS_RX
, 0, 255, 0, 0);
725 input_set_abs_params(gamepad
, ABS_RY
, 0, 255, 0, 0);
726 input_set_abs_params(gamepad
, ABS_RZ
, 0, 255, 0, 0);
728 input_set_abs_params(gamepad
, ABS_HAT0X
, -1, 1, 0, 0);
729 input_set_abs_params(gamepad
, ABS_HAT0Y
, -1, 1, 0, 0);
731 for (i
= 0; i
< ARRAY_SIZE(ps_gamepad_buttons
); i
++)
732 input_set_capability(gamepad
, EV_KEY
, ps_gamepad_buttons
[i
]);
734 #if IS_ENABLED(CONFIG_PLAYSTATION_FF)
736 input_set_capability(gamepad
, EV_FF
, FF_RUMBLE
);
737 input_ff_create_memless(gamepad
, NULL
, play_effect
);
741 ret
= input_register_device(gamepad
);
748 static int ps_get_report(struct hid_device
*hdev
, uint8_t report_id
, uint8_t *buf
, size_t size
,
753 ret
= hid_hw_raw_request(hdev
, report_id
, buf
, size
, HID_FEATURE_REPORT
,
756 hid_err(hdev
, "Failed to retrieve feature with reportID %d: %d\n", report_id
, ret
);
761 hid_err(hdev
, "Invalid byte count transferred, expected %zu got %d\n", size
, ret
);
765 if (buf
[0] != report_id
) {
766 hid_err(hdev
, "Invalid reportID received, expected %d got %d\n", report_id
, buf
[0]);
770 if (hdev
->bus
== BUS_BLUETOOTH
&& check_crc
) {
771 /* Last 4 bytes contains crc32. */
772 uint8_t crc_offset
= size
- 4;
773 uint32_t report_crc
= get_unaligned_le32(&buf
[crc_offset
]);
775 if (!ps_check_crc32(PS_FEATURE_CRC32_SEED
, buf
, crc_offset
, report_crc
)) {
776 hid_err(hdev
, "CRC check failed for reportID=%d\n", report_id
);
784 static int ps_led_register(struct ps_device
*ps_dev
, struct led_classdev
*led
,
785 const struct ps_led_info
*led_info
)
789 if (led_info
->name
) {
790 led
->name
= devm_kasprintf(&ps_dev
->hdev
->dev
, GFP_KERNEL
,
791 "%s:%s:%s", ps_dev
->input_dev_name
, led_info
->color
, led_info
->name
);
793 /* Backwards compatible mode for hid-sony, but not compliant with LED class spec. */
794 led
->name
= devm_kasprintf(&ps_dev
->hdev
->dev
, GFP_KERNEL
,
795 "%s:%s", ps_dev
->input_dev_name
, led_info
->color
);
802 led
->max_brightness
= led_info
->max_brightness
;
803 led
->flags
= LED_CORE_SUSPENDRESUME
;
804 led
->brightness_get
= led_info
->brightness_get
;
805 led
->brightness_set_blocking
= led_info
->brightness_set
;
806 led
->blink_set
= led_info
->blink_set
;
808 ret
= devm_led_classdev_register(&ps_dev
->hdev
->dev
, led
);
810 hid_err(ps_dev
->hdev
, "Failed to register LED %s: %d\n", led_info
->name
, ret
);
817 /* Register a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */
818 static int ps_lightbar_register(struct ps_device
*ps_dev
, struct led_classdev_mc
*lightbar_mc_dev
,
819 int (*brightness_set
)(struct led_classdev
*, enum led_brightness
))
821 struct hid_device
*hdev
= ps_dev
->hdev
;
822 struct mc_subled
*mc_led_info
;
823 struct led_classdev
*led_cdev
;
826 mc_led_info
= devm_kmalloc_array(&hdev
->dev
, 3, sizeof(*mc_led_info
),
827 GFP_KERNEL
| __GFP_ZERO
);
831 mc_led_info
[0].color_index
= LED_COLOR_ID_RED
;
832 mc_led_info
[1].color_index
= LED_COLOR_ID_GREEN
;
833 mc_led_info
[2].color_index
= LED_COLOR_ID_BLUE
;
835 lightbar_mc_dev
->subled_info
= mc_led_info
;
836 lightbar_mc_dev
->num_colors
= 3;
838 led_cdev
= &lightbar_mc_dev
->led_cdev
;
839 led_cdev
->name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
, "%s:rgb:indicator",
840 ps_dev
->input_dev_name
);
843 led_cdev
->brightness
= 255;
844 led_cdev
->max_brightness
= 255;
845 led_cdev
->brightness_set_blocking
= brightness_set
;
847 ret
= devm_led_classdev_multicolor_register(&hdev
->dev
, lightbar_mc_dev
);
849 hid_err(hdev
, "Cannot register multicolor LED device\n");
856 static struct input_dev
*ps_sensors_create(struct hid_device
*hdev
, int accel_range
, int accel_res
,
857 int gyro_range
, int gyro_res
)
859 struct input_dev
*sensors
;
862 sensors
= ps_allocate_input_dev(hdev
, "Motion Sensors");
864 return ERR_CAST(sensors
);
866 __set_bit(INPUT_PROP_ACCELEROMETER
, sensors
->propbit
);
867 __set_bit(EV_MSC
, sensors
->evbit
);
868 __set_bit(MSC_TIMESTAMP
, sensors
->mscbit
);
871 input_set_abs_params(sensors
, ABS_X
, -accel_range
, accel_range
, 16, 0);
872 input_set_abs_params(sensors
, ABS_Y
, -accel_range
, accel_range
, 16, 0);
873 input_set_abs_params(sensors
, ABS_Z
, -accel_range
, accel_range
, 16, 0);
874 input_abs_set_res(sensors
, ABS_X
, accel_res
);
875 input_abs_set_res(sensors
, ABS_Y
, accel_res
);
876 input_abs_set_res(sensors
, ABS_Z
, accel_res
);
879 input_set_abs_params(sensors
, ABS_RX
, -gyro_range
, gyro_range
, 16, 0);
880 input_set_abs_params(sensors
, ABS_RY
, -gyro_range
, gyro_range
, 16, 0);
881 input_set_abs_params(sensors
, ABS_RZ
, -gyro_range
, gyro_range
, 16, 0);
882 input_abs_set_res(sensors
, ABS_RX
, gyro_res
);
883 input_abs_set_res(sensors
, ABS_RY
, gyro_res
);
884 input_abs_set_res(sensors
, ABS_RZ
, gyro_res
);
886 ret
= input_register_device(sensors
);
893 static struct input_dev
*ps_touchpad_create(struct hid_device
*hdev
, int width
, int height
,
894 unsigned int num_contacts
)
896 struct input_dev
*touchpad
;
899 touchpad
= ps_allocate_input_dev(hdev
, "Touchpad");
900 if (IS_ERR(touchpad
))
901 return ERR_CAST(touchpad
);
903 /* Map button underneath touchpad to BTN_LEFT. */
904 input_set_capability(touchpad
, EV_KEY
, BTN_LEFT
);
905 __set_bit(INPUT_PROP_BUTTONPAD
, touchpad
->propbit
);
907 input_set_abs_params(touchpad
, ABS_MT_POSITION_X
, 0, width
- 1, 0, 0);
908 input_set_abs_params(touchpad
, ABS_MT_POSITION_Y
, 0, height
- 1, 0, 0);
910 ret
= input_mt_init_slots(touchpad
, num_contacts
, INPUT_MT_POINTER
);
914 ret
= input_register_device(touchpad
);
921 static ssize_t
firmware_version_show(struct device
*dev
,
922 struct device_attribute
925 struct hid_device
*hdev
= to_hid_device(dev
);
926 struct ps_device
*ps_dev
= hid_get_drvdata(hdev
);
928 return sysfs_emit(buf
, "0x%08x\n", ps_dev
->fw_version
);
931 static DEVICE_ATTR_RO(firmware_version
);
933 static ssize_t
hardware_version_show(struct device
*dev
,
934 struct device_attribute
937 struct hid_device
*hdev
= to_hid_device(dev
);
938 struct ps_device
*ps_dev
= hid_get_drvdata(hdev
);
940 return sysfs_emit(buf
, "0x%08x\n", ps_dev
->hw_version
);
943 static DEVICE_ATTR_RO(hardware_version
);
945 static struct attribute
*ps_device_attrs
[] = {
946 &dev_attr_firmware_version
.attr
,
947 &dev_attr_hardware_version
.attr
,
950 ATTRIBUTE_GROUPS(ps_device
);
952 static int dualsense_get_calibration_data(struct dualsense
*ds
)
954 struct hid_device
*hdev
= ds
->base
.hdev
;
955 short gyro_pitch_bias
, gyro_pitch_plus
, gyro_pitch_minus
;
956 short gyro_yaw_bias
, gyro_yaw_plus
, gyro_yaw_minus
;
957 short gyro_roll_bias
, gyro_roll_plus
, gyro_roll_minus
;
958 short gyro_speed_plus
, gyro_speed_minus
;
959 short acc_x_plus
, acc_x_minus
;
960 short acc_y_plus
, acc_y_minus
;
961 short acc_z_plus
, acc_z_minus
;
968 buf
= kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE
, GFP_KERNEL
);
972 ret
= ps_get_report(ds
->base
.hdev
, DS_FEATURE_REPORT_CALIBRATION
, buf
,
973 DS_FEATURE_REPORT_CALIBRATION_SIZE
, true);
975 hid_err(ds
->base
.hdev
, "Failed to retrieve DualSense calibration info: %d\n", ret
);
979 gyro_pitch_bias
= get_unaligned_le16(&buf
[1]);
980 gyro_yaw_bias
= get_unaligned_le16(&buf
[3]);
981 gyro_roll_bias
= get_unaligned_le16(&buf
[5]);
982 gyro_pitch_plus
= get_unaligned_le16(&buf
[7]);
983 gyro_pitch_minus
= get_unaligned_le16(&buf
[9]);
984 gyro_yaw_plus
= get_unaligned_le16(&buf
[11]);
985 gyro_yaw_minus
= get_unaligned_le16(&buf
[13]);
986 gyro_roll_plus
= get_unaligned_le16(&buf
[15]);
987 gyro_roll_minus
= get_unaligned_le16(&buf
[17]);
988 gyro_speed_plus
= get_unaligned_le16(&buf
[19]);
989 gyro_speed_minus
= get_unaligned_le16(&buf
[21]);
990 acc_x_plus
= get_unaligned_le16(&buf
[23]);
991 acc_x_minus
= get_unaligned_le16(&buf
[25]);
992 acc_y_plus
= get_unaligned_le16(&buf
[27]);
993 acc_y_minus
= get_unaligned_le16(&buf
[29]);
994 acc_z_plus
= get_unaligned_le16(&buf
[31]);
995 acc_z_minus
= get_unaligned_le16(&buf
[33]);
998 * Set gyroscope calibration and normalization parameters.
999 * Data values will be normalized to 1/DS_GYRO_RES_PER_DEG_S degree/s.
1001 speed_2x
= (gyro_speed_plus
+ gyro_speed_minus
);
1002 ds
->gyro_calib_data
[0].abs_code
= ABS_RX
;
1003 ds
->gyro_calib_data
[0].bias
= 0;
1004 ds
->gyro_calib_data
[0].sens_numer
= speed_2x
*DS_GYRO_RES_PER_DEG_S
;
1005 ds
->gyro_calib_data
[0].sens_denom
= abs(gyro_pitch_plus
- gyro_pitch_bias
) +
1006 abs(gyro_pitch_minus
- gyro_pitch_bias
);
1008 ds
->gyro_calib_data
[1].abs_code
= ABS_RY
;
1009 ds
->gyro_calib_data
[1].bias
= 0;
1010 ds
->gyro_calib_data
[1].sens_numer
= speed_2x
*DS_GYRO_RES_PER_DEG_S
;
1011 ds
->gyro_calib_data
[1].sens_denom
= abs(gyro_yaw_plus
- gyro_yaw_bias
) +
1012 abs(gyro_yaw_minus
- gyro_yaw_bias
);
1014 ds
->gyro_calib_data
[2].abs_code
= ABS_RZ
;
1015 ds
->gyro_calib_data
[2].bias
= 0;
1016 ds
->gyro_calib_data
[2].sens_numer
= speed_2x
*DS_GYRO_RES_PER_DEG_S
;
1017 ds
->gyro_calib_data
[2].sens_denom
= abs(gyro_roll_plus
- gyro_roll_bias
) +
1018 abs(gyro_roll_minus
- gyro_roll_bias
);
1021 * Sanity check gyro calibration data. This is needed to prevent crashes
1022 * during report handling of virtual, clone or broken devices not implementing
1023 * calibration data properly.
1025 for (i
= 0; i
< ARRAY_SIZE(ds
->gyro_calib_data
); i
++) {
1026 if (ds
->gyro_calib_data
[i
].sens_denom
== 0) {
1027 hid_warn(hdev
, "Invalid gyro calibration data for axis (%d), disabling calibration.",
1028 ds
->gyro_calib_data
[i
].abs_code
);
1029 ds
->gyro_calib_data
[i
].bias
= 0;
1030 ds
->gyro_calib_data
[i
].sens_numer
= DS_GYRO_RANGE
;
1031 ds
->gyro_calib_data
[i
].sens_denom
= S16_MAX
;
1036 * Set accelerometer calibration and normalization parameters.
1037 * Data values will be normalized to 1/DS_ACC_RES_PER_G g.
1039 range_2g
= acc_x_plus
- acc_x_minus
;
1040 ds
->accel_calib_data
[0].abs_code
= ABS_X
;
1041 ds
->accel_calib_data
[0].bias
= acc_x_plus
- range_2g
/ 2;
1042 ds
->accel_calib_data
[0].sens_numer
= 2*DS_ACC_RES_PER_G
;
1043 ds
->accel_calib_data
[0].sens_denom
= range_2g
;
1045 range_2g
= acc_y_plus
- acc_y_minus
;
1046 ds
->accel_calib_data
[1].abs_code
= ABS_Y
;
1047 ds
->accel_calib_data
[1].bias
= acc_y_plus
- range_2g
/ 2;
1048 ds
->accel_calib_data
[1].sens_numer
= 2*DS_ACC_RES_PER_G
;
1049 ds
->accel_calib_data
[1].sens_denom
= range_2g
;
1051 range_2g
= acc_z_plus
- acc_z_minus
;
1052 ds
->accel_calib_data
[2].abs_code
= ABS_Z
;
1053 ds
->accel_calib_data
[2].bias
= acc_z_plus
- range_2g
/ 2;
1054 ds
->accel_calib_data
[2].sens_numer
= 2*DS_ACC_RES_PER_G
;
1055 ds
->accel_calib_data
[2].sens_denom
= range_2g
;
1058 * Sanity check accelerometer calibration data. This is needed to prevent crashes
1059 * during report handling of virtual, clone or broken devices not implementing calibration
1062 for (i
= 0; i
< ARRAY_SIZE(ds
->accel_calib_data
); i
++) {
1063 if (ds
->accel_calib_data
[i
].sens_denom
== 0) {
1064 hid_warn(hdev
, "Invalid accelerometer calibration data for axis (%d), disabling calibration.",
1065 ds
->accel_calib_data
[i
].abs_code
);
1066 ds
->accel_calib_data
[i
].bias
= 0;
1067 ds
->accel_calib_data
[i
].sens_numer
= DS_ACC_RANGE
;
1068 ds
->accel_calib_data
[i
].sens_denom
= S16_MAX
;
1078 static int dualsense_get_firmware_info(struct dualsense
*ds
)
1083 buf
= kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE
, GFP_KERNEL
);
1087 ret
= ps_get_report(ds
->base
.hdev
, DS_FEATURE_REPORT_FIRMWARE_INFO
, buf
,
1088 DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE
, true);
1090 hid_err(ds
->base
.hdev
, "Failed to retrieve DualSense firmware info: %d\n", ret
);
1094 ds
->base
.hw_version
= get_unaligned_le32(&buf
[24]);
1095 ds
->base
.fw_version
= get_unaligned_le32(&buf
[28]);
1097 /* Update version is some kind of feature version. It is distinct from
1098 * the firmware version as there can be many different variations of a
1099 * controller over time with the same physical shell, but with different
1100 * PCBs and other internal changes. The update version (internal name) is
1101 * used as a means to detect what features are available and change behavior.
1102 * Note: the version is different between DualSense and DualSense Edge.
1104 ds
->update_version
= get_unaligned_le16(&buf
[44]);
1111 static int dualsense_get_mac_address(struct dualsense
*ds
)
1116 buf
= kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE
, GFP_KERNEL
);
1120 ret
= ps_get_report(ds
->base
.hdev
, DS_FEATURE_REPORT_PAIRING_INFO
, buf
,
1121 DS_FEATURE_REPORT_PAIRING_INFO_SIZE
, true);
1123 hid_err(ds
->base
.hdev
, "Failed to retrieve DualSense pairing info: %d\n", ret
);
1127 memcpy(ds
->base
.mac_address
, &buf
[1], sizeof(ds
->base
.mac_address
));
1134 static int dualsense_lightbar_set_brightness(struct led_classdev
*cdev
,
1135 enum led_brightness brightness
)
1137 struct led_classdev_mc
*mc_cdev
= lcdev_to_mccdev(cdev
);
1138 struct dualsense
*ds
= container_of(mc_cdev
, struct dualsense
, lightbar
);
1139 uint8_t red
, green
, blue
;
1141 led_mc_calc_color_components(mc_cdev
, brightness
);
1142 red
= mc_cdev
->subled_info
[0].brightness
;
1143 green
= mc_cdev
->subled_info
[1].brightness
;
1144 blue
= mc_cdev
->subled_info
[2].brightness
;
1146 dualsense_set_lightbar(ds
, red
, green
, blue
);
1150 static enum led_brightness
dualsense_player_led_get_brightness(struct led_classdev
*led
)
1152 struct hid_device
*hdev
= to_hid_device(led
->dev
->parent
);
1153 struct dualsense
*ds
= hid_get_drvdata(hdev
);
1155 return !!(ds
->player_leds_state
& BIT(led
- ds
->player_leds
));
1158 static int dualsense_player_led_set_brightness(struct led_classdev
*led
, enum led_brightness value
)
1160 struct hid_device
*hdev
= to_hid_device(led
->dev
->parent
);
1161 struct dualsense
*ds
= hid_get_drvdata(hdev
);
1162 unsigned long flags
;
1163 unsigned int led_index
;
1165 spin_lock_irqsave(&ds
->base
.lock
, flags
);
1167 led_index
= led
- ds
->player_leds
;
1168 if (value
== LED_OFF
)
1169 ds
->player_leds_state
&= ~BIT(led_index
);
1171 ds
->player_leds_state
|= BIT(led_index
);
1173 ds
->update_player_leds
= true;
1174 spin_unlock_irqrestore(&ds
->base
.lock
, flags
);
1176 dualsense_schedule_work(ds
);
1181 static void dualsense_init_output_report(struct dualsense
*ds
, struct dualsense_output_report
*rp
,
1184 struct hid_device
*hdev
= ds
->base
.hdev
;
1186 if (hdev
->bus
== BUS_BLUETOOTH
) {
1187 struct dualsense_output_report_bt
*bt
= buf
;
1189 memset(bt
, 0, sizeof(*bt
));
1190 bt
->report_id
= DS_OUTPUT_REPORT_BT
;
1191 bt
->tag
= DS_OUTPUT_TAG
; /* Tag must be set. Exact meaning is unclear. */
1194 * Highest 4-bit is a sequence number, which needs to be increased
1195 * every report. Lowest 4-bit is tag and can be zero for now.
1197 bt
->seq_tag
= (ds
->output_seq
<< 4) | 0x0;
1198 if (++ds
->output_seq
== 16)
1202 rp
->len
= sizeof(*bt
);
1205 rp
->common
= &bt
->common
;
1207 struct dualsense_output_report_usb
*usb
= buf
;
1209 memset(usb
, 0, sizeof(*usb
));
1210 usb
->report_id
= DS_OUTPUT_REPORT_USB
;
1213 rp
->len
= sizeof(*usb
);
1216 rp
->common
= &usb
->common
;
1220 static inline void dualsense_schedule_work(struct dualsense
*ds
)
1222 unsigned long flags
;
1224 spin_lock_irqsave(&ds
->base
.lock
, flags
);
1225 if (ds
->output_worker_initialized
)
1226 schedule_work(&ds
->output_worker
);
1227 spin_unlock_irqrestore(&ds
->base
.lock
, flags
);
1231 * Helper function to send DualSense output reports. Applies a CRC at the end of a report
1232 * for Bluetooth reports.
1234 static void dualsense_send_output_report(struct dualsense
*ds
,
1235 struct dualsense_output_report
*report
)
1237 struct hid_device
*hdev
= ds
->base
.hdev
;
1239 /* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */
1242 uint8_t seed
= PS_OUTPUT_CRC32_SEED
;
1244 crc
= crc32_le(0xFFFFFFFF, &seed
, 1);
1245 crc
= ~crc32_le(crc
, report
->data
, report
->len
- 4);
1247 report
->bt
->crc32
= cpu_to_le32(crc
);
1250 hid_hw_output_report(hdev
, report
->data
, report
->len
);
1253 static void dualsense_output_worker(struct work_struct
*work
)
1255 struct dualsense
*ds
= container_of(work
, struct dualsense
, output_worker
);
1256 struct dualsense_output_report report
;
1257 struct dualsense_output_report_common
*common
;
1258 unsigned long flags
;
1260 dualsense_init_output_report(ds
, &report
, ds
->output_report_dmabuf
);
1261 common
= report
.common
;
1263 spin_lock_irqsave(&ds
->base
.lock
, flags
);
1265 if (ds
->update_rumble
) {
1266 /* Select classic rumble style haptics and enable it. */
1267 common
->valid_flag0
|= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT
;
1268 if (ds
->use_vibration_v2
)
1269 common
->valid_flag2
|= DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2
;
1271 common
->valid_flag0
|= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION
;
1272 common
->motor_left
= ds
->motor_left
;
1273 common
->motor_right
= ds
->motor_right
;
1274 ds
->update_rumble
= false;
1277 if (ds
->update_lightbar
) {
1278 common
->valid_flag1
|= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE
;
1279 common
->lightbar_red
= ds
->lightbar_red
;
1280 common
->lightbar_green
= ds
->lightbar_green
;
1281 common
->lightbar_blue
= ds
->lightbar_blue
;
1283 ds
->update_lightbar
= false;
1286 if (ds
->update_player_leds
) {
1287 common
->valid_flag1
|= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE
;
1288 common
->player_leds
= ds
->player_leds_state
;
1290 ds
->update_player_leds
= false;
1293 if (ds
->update_mic_mute
) {
1294 common
->valid_flag1
|= DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE
;
1295 common
->mute_button_led
= ds
->mic_muted
;
1297 if (ds
->mic_muted
) {
1298 /* Disable microphone */
1299 common
->valid_flag1
|= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE
;
1300 common
->power_save_control
|= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE
;
1302 /* Enable microphone */
1303 common
->valid_flag1
|= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE
;
1304 common
->power_save_control
&= ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE
;
1307 ds
->update_mic_mute
= false;
1310 spin_unlock_irqrestore(&ds
->base
.lock
, flags
);
1312 dualsense_send_output_report(ds
, &report
);
1315 static int dualsense_parse_report(struct ps_device
*ps_dev
, struct hid_report
*report
,
1318 struct hid_device
*hdev
= ps_dev
->hdev
;
1319 struct dualsense
*ds
= container_of(ps_dev
, struct dualsense
, base
);
1320 struct dualsense_input_report
*ds_report
;
1321 uint8_t battery_data
, battery_capacity
, charging_status
, value
;
1323 uint32_t sensor_timestamp
;
1325 unsigned long flags
;
1329 * DualSense in USB uses the full HID report for reportID 1, but
1330 * Bluetooth uses a minimal HID report for reportID 1 and reports
1331 * the full report using reportID 49.
1333 if (hdev
->bus
== BUS_USB
&& report
->id
== DS_INPUT_REPORT_USB
&&
1334 size
== DS_INPUT_REPORT_USB_SIZE
) {
1335 ds_report
= (struct dualsense_input_report
*)&data
[1];
1336 } else if (hdev
->bus
== BUS_BLUETOOTH
&& report
->id
== DS_INPUT_REPORT_BT
&&
1337 size
== DS_INPUT_REPORT_BT_SIZE
) {
1338 /* Last 4 bytes of input report contain crc32 */
1339 uint32_t report_crc
= get_unaligned_le32(&data
[size
- 4]);
1341 if (!ps_check_crc32(PS_INPUT_CRC32_SEED
, data
, size
- 4, report_crc
)) {
1342 hid_err(hdev
, "DualSense input CRC's check failed\n");
1346 ds_report
= (struct dualsense_input_report
*)&data
[2];
1348 hid_err(hdev
, "Unhandled reportID=%d\n", report
->id
);
1352 input_report_abs(ds
->gamepad
, ABS_X
, ds_report
->x
);
1353 input_report_abs(ds
->gamepad
, ABS_Y
, ds_report
->y
);
1354 input_report_abs(ds
->gamepad
, ABS_RX
, ds_report
->rx
);
1355 input_report_abs(ds
->gamepad
, ABS_RY
, ds_report
->ry
);
1356 input_report_abs(ds
->gamepad
, ABS_Z
, ds_report
->z
);
1357 input_report_abs(ds
->gamepad
, ABS_RZ
, ds_report
->rz
);
1359 value
= ds_report
->buttons
[0] & DS_BUTTONS0_HAT_SWITCH
;
1360 if (value
>= ARRAY_SIZE(ps_gamepad_hat_mapping
))
1361 value
= 8; /* center */
1362 input_report_abs(ds
->gamepad
, ABS_HAT0X
, ps_gamepad_hat_mapping
[value
].x
);
1363 input_report_abs(ds
->gamepad
, ABS_HAT0Y
, ps_gamepad_hat_mapping
[value
].y
);
1365 input_report_key(ds
->gamepad
, BTN_WEST
, ds_report
->buttons
[0] & DS_BUTTONS0_SQUARE
);
1366 input_report_key(ds
->gamepad
, BTN_SOUTH
, ds_report
->buttons
[0] & DS_BUTTONS0_CROSS
);
1367 input_report_key(ds
->gamepad
, BTN_EAST
, ds_report
->buttons
[0] & DS_BUTTONS0_CIRCLE
);
1368 input_report_key(ds
->gamepad
, BTN_NORTH
, ds_report
->buttons
[0] & DS_BUTTONS0_TRIANGLE
);
1369 input_report_key(ds
->gamepad
, BTN_TL
, ds_report
->buttons
[1] & DS_BUTTONS1_L1
);
1370 input_report_key(ds
->gamepad
, BTN_TR
, ds_report
->buttons
[1] & DS_BUTTONS1_R1
);
1371 input_report_key(ds
->gamepad
, BTN_TL2
, ds_report
->buttons
[1] & DS_BUTTONS1_L2
);
1372 input_report_key(ds
->gamepad
, BTN_TR2
, ds_report
->buttons
[1] & DS_BUTTONS1_R2
);
1373 input_report_key(ds
->gamepad
, BTN_SELECT
, ds_report
->buttons
[1] & DS_BUTTONS1_CREATE
);
1374 input_report_key(ds
->gamepad
, BTN_START
, ds_report
->buttons
[1] & DS_BUTTONS1_OPTIONS
);
1375 input_report_key(ds
->gamepad
, BTN_THUMBL
, ds_report
->buttons
[1] & DS_BUTTONS1_L3
);
1376 input_report_key(ds
->gamepad
, BTN_THUMBR
, ds_report
->buttons
[1] & DS_BUTTONS1_R3
);
1377 input_report_key(ds
->gamepad
, BTN_MODE
, ds_report
->buttons
[2] & DS_BUTTONS2_PS_HOME
);
1378 input_sync(ds
->gamepad
);
1381 * The DualSense has an internal microphone, which can be muted through a mute button
1382 * on the device. The driver is expected to read the button state and program the device
1383 * to mute/unmute audio at the hardware level.
1385 btn_mic_state
= !!(ds_report
->buttons
[2] & DS_BUTTONS2_MIC_MUTE
);
1386 if (btn_mic_state
&& !ds
->last_btn_mic_state
) {
1387 spin_lock_irqsave(&ps_dev
->lock
, flags
);
1388 ds
->update_mic_mute
= true;
1389 ds
->mic_muted
= !ds
->mic_muted
; /* toggle */
1390 spin_unlock_irqrestore(&ps_dev
->lock
, flags
);
1392 /* Schedule updating of microphone state at hardware level. */
1393 dualsense_schedule_work(ds
);
1395 ds
->last_btn_mic_state
= btn_mic_state
;
1397 /* Parse and calibrate gyroscope data. */
1398 for (i
= 0; i
< ARRAY_SIZE(ds_report
->gyro
); i
++) {
1399 int raw_data
= (short)le16_to_cpu(ds_report
->gyro
[i
]);
1400 int calib_data
= mult_frac(ds
->gyro_calib_data
[i
].sens_numer
,
1401 raw_data
, ds
->gyro_calib_data
[i
].sens_denom
);
1403 input_report_abs(ds
->sensors
, ds
->gyro_calib_data
[i
].abs_code
, calib_data
);
1406 /* Parse and calibrate accelerometer data. */
1407 for (i
= 0; i
< ARRAY_SIZE(ds_report
->accel
); i
++) {
1408 int raw_data
= (short)le16_to_cpu(ds_report
->accel
[i
]);
1409 int calib_data
= mult_frac(ds
->accel_calib_data
[i
].sens_numer
,
1410 raw_data
- ds
->accel_calib_data
[i
].bias
,
1411 ds
->accel_calib_data
[i
].sens_denom
);
1413 input_report_abs(ds
->sensors
, ds
->accel_calib_data
[i
].abs_code
, calib_data
);
1416 /* Convert timestamp (in 0.33us unit) to timestamp_us */
1417 sensor_timestamp
= le32_to_cpu(ds_report
->sensor_timestamp
);
1418 if (!ds
->sensor_timestamp_initialized
) {
1419 ds
->sensor_timestamp_us
= DIV_ROUND_CLOSEST(sensor_timestamp
, 3);
1420 ds
->sensor_timestamp_initialized
= true;
1424 if (ds
->prev_sensor_timestamp
> sensor_timestamp
)
1425 delta
= (U32_MAX
- ds
->prev_sensor_timestamp
+ sensor_timestamp
+ 1);
1427 delta
= sensor_timestamp
- ds
->prev_sensor_timestamp
;
1428 ds
->sensor_timestamp_us
+= DIV_ROUND_CLOSEST(delta
, 3);
1430 ds
->prev_sensor_timestamp
= sensor_timestamp
;
1431 input_event(ds
->sensors
, EV_MSC
, MSC_TIMESTAMP
, ds
->sensor_timestamp_us
);
1432 input_sync(ds
->sensors
);
1434 for (i
= 0; i
< ARRAY_SIZE(ds_report
->points
); i
++) {
1435 struct dualsense_touch_point
*point
= &ds_report
->points
[i
];
1436 bool active
= (point
->contact
& DS_TOUCH_POINT_INACTIVE
) ? false : true;
1438 input_mt_slot(ds
->touchpad
, i
);
1439 input_mt_report_slot_state(ds
->touchpad
, MT_TOOL_FINGER
, active
);
1442 int x
= (point
->x_hi
<< 8) | point
->x_lo
;
1443 int y
= (point
->y_hi
<< 4) | point
->y_lo
;
1445 input_report_abs(ds
->touchpad
, ABS_MT_POSITION_X
, x
);
1446 input_report_abs(ds
->touchpad
, ABS_MT_POSITION_Y
, y
);
1449 input_mt_sync_frame(ds
->touchpad
);
1450 input_report_key(ds
->touchpad
, BTN_LEFT
, ds_report
->buttons
[2] & DS_BUTTONS2_TOUCHPAD
);
1451 input_sync(ds
->touchpad
);
1453 battery_data
= ds_report
->status
& DS_STATUS_BATTERY_CAPACITY
;
1454 charging_status
= (ds_report
->status
& DS_STATUS_CHARGING
) >> DS_STATUS_CHARGING_SHIFT
;
1456 switch (charging_status
) {
1459 * Each unit of battery data corresponds to 10%
1460 * 0 = 0-9%, 1 = 10-19%, .. and 10 = 100%
1462 battery_capacity
= min(battery_data
* 10 + 5, 100);
1463 battery_status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1466 battery_capacity
= min(battery_data
* 10 + 5, 100);
1467 battery_status
= POWER_SUPPLY_STATUS_CHARGING
;
1470 battery_capacity
= 100;
1471 battery_status
= POWER_SUPPLY_STATUS_FULL
;
1473 case 0xa: /* voltage or temperature out of range */
1474 case 0xb: /* temperature error */
1475 battery_capacity
= 0;
1476 battery_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1478 case 0xf: /* charging error */
1480 battery_capacity
= 0;
1481 battery_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
1484 spin_lock_irqsave(&ps_dev
->lock
, flags
);
1485 ps_dev
->battery_capacity
= battery_capacity
;
1486 ps_dev
->battery_status
= battery_status
;
1487 spin_unlock_irqrestore(&ps_dev
->lock
, flags
);
1492 static int dualsense_play_effect(struct input_dev
*dev
, void *data
, struct ff_effect
*effect
)
1494 struct hid_device
*hdev
= input_get_drvdata(dev
);
1495 struct dualsense
*ds
= hid_get_drvdata(hdev
);
1496 unsigned long flags
;
1498 if (effect
->type
!= FF_RUMBLE
)
1501 spin_lock_irqsave(&ds
->base
.lock
, flags
);
1502 ds
->update_rumble
= true;
1503 ds
->motor_left
= effect
->u
.rumble
.strong_magnitude
/ 256;
1504 ds
->motor_right
= effect
->u
.rumble
.weak_magnitude
/ 256;
1505 spin_unlock_irqrestore(&ds
->base
.lock
, flags
);
1507 dualsense_schedule_work(ds
);
1511 static void dualsense_remove(struct ps_device
*ps_dev
)
1513 struct dualsense
*ds
= container_of(ps_dev
, struct dualsense
, base
);
1514 unsigned long flags
;
1516 spin_lock_irqsave(&ds
->base
.lock
, flags
);
1517 ds
->output_worker_initialized
= false;
1518 spin_unlock_irqrestore(&ds
->base
.lock
, flags
);
1520 cancel_work_sync(&ds
->output_worker
);
1523 static int dualsense_reset_leds(struct dualsense
*ds
)
1525 struct dualsense_output_report report
;
1528 buf
= kzalloc(sizeof(struct dualsense_output_report_bt
), GFP_KERNEL
);
1532 dualsense_init_output_report(ds
, &report
, buf
);
1534 * On Bluetooth the DualSense outputs an animation on the lightbar
1535 * during startup and maintains a color afterwards. We need to explicitly
1536 * reconfigure the lightbar before we can do any programming later on.
1537 * In USB the lightbar is not on by default, but redoing the setup there
1540 report
.common
->valid_flag2
= DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE
;
1541 report
.common
->lightbar_setup
= DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT
; /* Fade light out. */
1542 dualsense_send_output_report(ds
, &report
);
1548 static void dualsense_set_lightbar(struct dualsense
*ds
, uint8_t red
, uint8_t green
, uint8_t blue
)
1550 unsigned long flags
;
1552 spin_lock_irqsave(&ds
->base
.lock
, flags
);
1553 ds
->update_lightbar
= true;
1554 ds
->lightbar_red
= red
;
1555 ds
->lightbar_green
= green
;
1556 ds
->lightbar_blue
= blue
;
1557 spin_unlock_irqrestore(&ds
->base
.lock
, flags
);
1559 dualsense_schedule_work(ds
);
1562 static void dualsense_set_player_leds(struct dualsense
*ds
)
1565 * The DualSense controller has a row of 5 LEDs used for player ids.
1566 * Behavior on the PlayStation 5 console is to center the player id
1567 * across the LEDs, so e.g. player 1 would be "--x--" with x being 'on'.
1568 * Follow a similar mapping here.
1570 static const int player_ids
[5] = {
1573 BIT(4) | BIT(2) | BIT(0),
1574 BIT(4) | BIT(3) | BIT(1) | BIT(0),
1575 BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0)
1578 uint8_t player_id
= ds
->base
.player_id
% ARRAY_SIZE(player_ids
);
1580 ds
->update_player_leds
= true;
1581 ds
->player_leds_state
= player_ids
[player_id
];
1582 dualsense_schedule_work(ds
);
1585 static struct ps_device
*dualsense_create(struct hid_device
*hdev
)
1587 struct dualsense
*ds
;
1588 struct ps_device
*ps_dev
;
1589 uint8_t max_output_report_size
;
1592 static const struct ps_led_info player_leds_info
[] = {
1593 { LED_FUNCTION_PLAYER1
, "white", 1, dualsense_player_led_get_brightness
,
1594 dualsense_player_led_set_brightness
},
1595 { LED_FUNCTION_PLAYER2
, "white", 1, dualsense_player_led_get_brightness
,
1596 dualsense_player_led_set_brightness
},
1597 { LED_FUNCTION_PLAYER3
, "white", 1, dualsense_player_led_get_brightness
,
1598 dualsense_player_led_set_brightness
},
1599 { LED_FUNCTION_PLAYER4
, "white", 1, dualsense_player_led_get_brightness
,
1600 dualsense_player_led_set_brightness
},
1601 { LED_FUNCTION_PLAYER5
, "white", 1, dualsense_player_led_get_brightness
,
1602 dualsense_player_led_set_brightness
}
1605 ds
= devm_kzalloc(&hdev
->dev
, sizeof(*ds
), GFP_KERNEL
);
1607 return ERR_PTR(-ENOMEM
);
1610 * Patch version to allow userspace to distinguish between
1611 * hid-generic vs hid-playstation axis and button mapping.
1613 hdev
->version
|= HID_PLAYSTATION_VERSION_PATCH
;
1616 ps_dev
->hdev
= hdev
;
1617 spin_lock_init(&ps_dev
->lock
);
1618 ps_dev
->battery_capacity
= 100; /* initial value until parse_report. */
1619 ps_dev
->battery_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
1620 ps_dev
->parse_report
= dualsense_parse_report
;
1621 ps_dev
->remove
= dualsense_remove
;
1622 INIT_WORK(&ds
->output_worker
, dualsense_output_worker
);
1623 ds
->output_worker_initialized
= true;
1624 hid_set_drvdata(hdev
, ds
);
1626 max_output_report_size
= sizeof(struct dualsense_output_report_bt
);
1627 ds
->output_report_dmabuf
= devm_kzalloc(&hdev
->dev
, max_output_report_size
, GFP_KERNEL
);
1628 if (!ds
->output_report_dmabuf
)
1629 return ERR_PTR(-ENOMEM
);
1631 ret
= dualsense_get_mac_address(ds
);
1633 hid_err(hdev
, "Failed to get MAC address from DualSense\n");
1634 return ERR_PTR(ret
);
1636 snprintf(hdev
->uniq
, sizeof(hdev
->uniq
), "%pMR", ds
->base
.mac_address
);
1638 ret
= dualsense_get_firmware_info(ds
);
1640 hid_err(hdev
, "Failed to get firmware info from DualSense\n");
1641 return ERR_PTR(ret
);
1644 /* Original DualSense firmware simulated classic controller rumble through
1645 * its new haptics hardware. It felt different from classic rumble users
1646 * were used to. Since then new firmwares were introduced to change behavior
1647 * and make this new 'v2' behavior default on PlayStation and other platforms.
1648 * The original DualSense requires a new enough firmware as bundled with PS5
1649 * software released in 2021. DualSense edge supports it out of the box.
1650 * Both devices also support the old mode, but it is not really used.
1652 if (hdev
->product
== USB_DEVICE_ID_SONY_PS5_CONTROLLER
) {
1653 /* Feature version 2.21 introduced new vibration method. */
1654 ds
->use_vibration_v2
= ds
->update_version
>= DS_FEATURE_VERSION(2, 21);
1655 } else if (hdev
->product
== USB_DEVICE_ID_SONY_PS5_CONTROLLER_2
) {
1656 ds
->use_vibration_v2
= true;
1659 ret
= ps_devices_list_add(ps_dev
);
1661 return ERR_PTR(ret
);
1663 ret
= dualsense_get_calibration_data(ds
);
1665 hid_err(hdev
, "Failed to get calibration data from DualSense\n");
1669 ds
->gamepad
= ps_gamepad_create(hdev
, dualsense_play_effect
);
1670 if (IS_ERR(ds
->gamepad
)) {
1671 ret
= PTR_ERR(ds
->gamepad
);
1674 /* Use gamepad input device name as primary device name for e.g. LEDs */
1675 ps_dev
->input_dev_name
= dev_name(&ds
->gamepad
->dev
);
1677 ds
->sensors
= ps_sensors_create(hdev
, DS_ACC_RANGE
, DS_ACC_RES_PER_G
,
1678 DS_GYRO_RANGE
, DS_GYRO_RES_PER_DEG_S
);
1679 if (IS_ERR(ds
->sensors
)) {
1680 ret
= PTR_ERR(ds
->sensors
);
1684 ds
->touchpad
= ps_touchpad_create(hdev
, DS_TOUCHPAD_WIDTH
, DS_TOUCHPAD_HEIGHT
, 2);
1685 if (IS_ERR(ds
->touchpad
)) {
1686 ret
= PTR_ERR(ds
->touchpad
);
1690 ret
= ps_device_register_battery(ps_dev
);
1695 * The hardware may have control over the LEDs (e.g. in Bluetooth on startup).
1696 * Reset the LEDs (lightbar, mute, player leds), so we can control them
1699 ret
= dualsense_reset_leds(ds
);
1703 ret
= ps_lightbar_register(ps_dev
, &ds
->lightbar
, dualsense_lightbar_set_brightness
);
1707 /* Set default lightbar color. */
1708 dualsense_set_lightbar(ds
, 0, 0, 128); /* blue */
1710 for (i
= 0; i
< ARRAY_SIZE(player_leds_info
); i
++) {
1711 const struct ps_led_info
*led_info
= &player_leds_info
[i
];
1713 ret
= ps_led_register(ps_dev
, &ds
->player_leds
[i
], led_info
);
1718 ret
= ps_device_set_player_id(ps_dev
);
1720 hid_err(hdev
, "Failed to assign player id for DualSense: %d\n", ret
);
1724 /* Set player LEDs to our player id. */
1725 dualsense_set_player_leds(ds
);
1728 * Reporting hardware and firmware is important as there are frequent updates, which
1729 * can change behavior.
1731 hid_info(hdev
, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n",
1732 ds
->base
.hw_version
, ds
->base
.fw_version
);
1737 ps_devices_list_remove(ps_dev
);
1738 return ERR_PTR(ret
);
1741 static void dualshock4_dongle_calibration_work(struct work_struct
*work
)
1743 struct dualshock4
*ds4
= container_of(work
, struct dualshock4
, dongle_hotplug_worker
);
1744 unsigned long flags
;
1745 enum dualshock4_dongle_state dongle_state
;
1748 ret
= dualshock4_get_calibration_data(ds4
);
1750 /* This call is very unlikely to fail for the dongle. When it
1751 * fails we are probably in a very bad state, so mark the
1752 * dongle as disabled. We will re-enable the dongle if a new
1753 * DS4 hotplug is detect from sony_raw_event as any issues
1754 * are likely resolved then (the dongle is quite stupid).
1756 hid_err(ds4
->base
.hdev
, "DualShock 4 USB dongle: calibration failed, disabling device\n");
1757 dongle_state
= DONGLE_DISABLED
;
1759 hid_info(ds4
->base
.hdev
, "DualShock 4 USB dongle: calibration completed\n");
1760 dongle_state
= DONGLE_CONNECTED
;
1763 spin_lock_irqsave(&ds4
->base
.lock
, flags
);
1764 ds4
->dongle_state
= dongle_state
;
1765 spin_unlock_irqrestore(&ds4
->base
.lock
, flags
);
1768 static int dualshock4_get_calibration_data(struct dualshock4
*ds4
)
1770 struct hid_device
*hdev
= ds4
->base
.hdev
;
1771 short gyro_pitch_bias
, gyro_pitch_plus
, gyro_pitch_minus
;
1772 short gyro_yaw_bias
, gyro_yaw_plus
, gyro_yaw_minus
;
1773 short gyro_roll_bias
, gyro_roll_plus
, gyro_roll_minus
;
1774 short gyro_speed_plus
, gyro_speed_minus
;
1775 short acc_x_plus
, acc_x_minus
;
1776 short acc_y_plus
, acc_y_minus
;
1777 short acc_z_plus
, acc_z_minus
;
1784 if (ds4
->base
.hdev
->bus
== BUS_USB
) {
1787 buf
= kzalloc(DS4_FEATURE_REPORT_CALIBRATION_SIZE
, GFP_KERNEL
);
1790 goto transfer_failed
;
1793 /* We should normally receive the feature report data we asked
1794 * for, but hidraw applications such as Steam can issue feature
1795 * reports as well. In particular for Dongle reconnects, Steam
1796 * and this function are competing resulting in often receiving
1797 * data for a different HID report, so retry a few times.
1799 for (retries
= 0; retries
< 3; retries
++) {
1800 ret
= ps_get_report(hdev
, DS4_FEATURE_REPORT_CALIBRATION
, buf
,
1801 DS4_FEATURE_REPORT_CALIBRATION_SIZE
, true);
1804 hid_warn(hdev
, "Retrying DualShock 4 get calibration report (0x02) request\n");
1808 hid_warn(hdev
, "Failed to retrieve DualShock4 calibration info: %d\n", ret
);
1810 goto transfer_failed
;
1815 } else { /* Bluetooth */
1816 buf
= kzalloc(DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE
, GFP_KERNEL
);
1819 goto transfer_failed
;
1822 ret
= ps_get_report(hdev
, DS4_FEATURE_REPORT_CALIBRATION_BT
, buf
,
1823 DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE
, true);
1826 hid_warn(hdev
, "Failed to retrieve DualShock4 calibration info: %d\n", ret
);
1827 goto transfer_failed
;
1831 /* Transfer succeeded - parse the calibration data received. */
1832 gyro_pitch_bias
= get_unaligned_le16(&buf
[1]);
1833 gyro_yaw_bias
= get_unaligned_le16(&buf
[3]);
1834 gyro_roll_bias
= get_unaligned_le16(&buf
[5]);
1835 if (ds4
->base
.hdev
->bus
== BUS_USB
) {
1836 gyro_pitch_plus
= get_unaligned_le16(&buf
[7]);
1837 gyro_pitch_minus
= get_unaligned_le16(&buf
[9]);
1838 gyro_yaw_plus
= get_unaligned_le16(&buf
[11]);
1839 gyro_yaw_minus
= get_unaligned_le16(&buf
[13]);
1840 gyro_roll_plus
= get_unaligned_le16(&buf
[15]);
1841 gyro_roll_minus
= get_unaligned_le16(&buf
[17]);
1844 gyro_pitch_plus
= get_unaligned_le16(&buf
[7]);
1845 gyro_yaw_plus
= get_unaligned_le16(&buf
[9]);
1846 gyro_roll_plus
= get_unaligned_le16(&buf
[11]);
1847 gyro_pitch_minus
= get_unaligned_le16(&buf
[13]);
1848 gyro_yaw_minus
= get_unaligned_le16(&buf
[15]);
1849 gyro_roll_minus
= get_unaligned_le16(&buf
[17]);
1851 gyro_speed_plus
= get_unaligned_le16(&buf
[19]);
1852 gyro_speed_minus
= get_unaligned_le16(&buf
[21]);
1853 acc_x_plus
= get_unaligned_le16(&buf
[23]);
1854 acc_x_minus
= get_unaligned_le16(&buf
[25]);
1855 acc_y_plus
= get_unaligned_le16(&buf
[27]);
1856 acc_y_minus
= get_unaligned_le16(&buf
[29]);
1857 acc_z_plus
= get_unaligned_le16(&buf
[31]);
1858 acc_z_minus
= get_unaligned_le16(&buf
[33]);
1860 /* Done parsing the buffer, so let's free it. */
1864 * Set gyroscope calibration and normalization parameters.
1865 * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
1867 speed_2x
= (gyro_speed_plus
+ gyro_speed_minus
);
1868 ds4
->gyro_calib_data
[0].abs_code
= ABS_RX
;
1869 ds4
->gyro_calib_data
[0].bias
= 0;
1870 ds4
->gyro_calib_data
[0].sens_numer
= speed_2x
*DS4_GYRO_RES_PER_DEG_S
;
1871 ds4
->gyro_calib_data
[0].sens_denom
= abs(gyro_pitch_plus
- gyro_pitch_bias
) +
1872 abs(gyro_pitch_minus
- gyro_pitch_bias
);
1874 ds4
->gyro_calib_data
[1].abs_code
= ABS_RY
;
1875 ds4
->gyro_calib_data
[1].bias
= 0;
1876 ds4
->gyro_calib_data
[1].sens_numer
= speed_2x
*DS4_GYRO_RES_PER_DEG_S
;
1877 ds4
->gyro_calib_data
[1].sens_denom
= abs(gyro_yaw_plus
- gyro_yaw_bias
) +
1878 abs(gyro_yaw_minus
- gyro_yaw_bias
);
1880 ds4
->gyro_calib_data
[2].abs_code
= ABS_RZ
;
1881 ds4
->gyro_calib_data
[2].bias
= 0;
1882 ds4
->gyro_calib_data
[2].sens_numer
= speed_2x
*DS4_GYRO_RES_PER_DEG_S
;
1883 ds4
->gyro_calib_data
[2].sens_denom
= abs(gyro_roll_plus
- gyro_roll_bias
) +
1884 abs(gyro_roll_minus
- gyro_roll_bias
);
1887 * Set accelerometer calibration and normalization parameters.
1888 * Data values will be normalized to 1/DS4_ACC_RES_PER_G g.
1890 range_2g
= acc_x_plus
- acc_x_minus
;
1891 ds4
->accel_calib_data
[0].abs_code
= ABS_X
;
1892 ds4
->accel_calib_data
[0].bias
= acc_x_plus
- range_2g
/ 2;
1893 ds4
->accel_calib_data
[0].sens_numer
= 2*DS4_ACC_RES_PER_G
;
1894 ds4
->accel_calib_data
[0].sens_denom
= range_2g
;
1896 range_2g
= acc_y_plus
- acc_y_minus
;
1897 ds4
->accel_calib_data
[1].abs_code
= ABS_Y
;
1898 ds4
->accel_calib_data
[1].bias
= acc_y_plus
- range_2g
/ 2;
1899 ds4
->accel_calib_data
[1].sens_numer
= 2*DS4_ACC_RES_PER_G
;
1900 ds4
->accel_calib_data
[1].sens_denom
= range_2g
;
1902 range_2g
= acc_z_plus
- acc_z_minus
;
1903 ds4
->accel_calib_data
[2].abs_code
= ABS_Z
;
1904 ds4
->accel_calib_data
[2].bias
= acc_z_plus
- range_2g
/ 2;
1905 ds4
->accel_calib_data
[2].sens_numer
= 2*DS4_ACC_RES_PER_G
;
1906 ds4
->accel_calib_data
[2].sens_denom
= range_2g
;
1910 * Sanity check gyro calibration data. This is needed to prevent crashes
1911 * during report handling of virtual, clone or broken devices not implementing
1912 * calibration data properly.
1914 for (i
= 0; i
< ARRAY_SIZE(ds4
->gyro_calib_data
); i
++) {
1915 if (ds4
->gyro_calib_data
[i
].sens_denom
== 0) {
1916 ds4
->gyro_calib_data
[i
].abs_code
= ABS_RX
+ i
;
1917 hid_warn(hdev
, "Invalid gyro calibration data for axis (%d), disabling calibration.",
1918 ds4
->gyro_calib_data
[i
].abs_code
);
1919 ds4
->gyro_calib_data
[i
].bias
= 0;
1920 ds4
->gyro_calib_data
[i
].sens_numer
= DS4_GYRO_RANGE
;
1921 ds4
->gyro_calib_data
[i
].sens_denom
= S16_MAX
;
1926 * Sanity check accelerometer calibration data. This is needed to prevent crashes
1927 * during report handling of virtual, clone or broken devices not implementing calibration
1930 for (i
= 0; i
< ARRAY_SIZE(ds4
->accel_calib_data
); i
++) {
1931 if (ds4
->accel_calib_data
[i
].sens_denom
== 0) {
1932 ds4
->accel_calib_data
[i
].abs_code
= ABS_X
+ i
;
1933 hid_warn(hdev
, "Invalid accelerometer calibration data for axis (%d), disabling calibration.",
1934 ds4
->accel_calib_data
[i
].abs_code
);
1935 ds4
->accel_calib_data
[i
].bias
= 0;
1936 ds4
->accel_calib_data
[i
].sens_numer
= DS4_ACC_RANGE
;
1937 ds4
->accel_calib_data
[i
].sens_denom
= S16_MAX
;
1944 static int dualshock4_get_firmware_info(struct dualshock4
*ds4
)
1949 buf
= kzalloc(DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE
, GFP_KERNEL
);
1953 /* Note USB and BT support the same feature report, but this report
1954 * lacks CRC support, so must be disabled in ps_get_report.
1956 ret
= ps_get_report(ds4
->base
.hdev
, DS4_FEATURE_REPORT_FIRMWARE_INFO
, buf
,
1957 DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE
, false);
1959 hid_err(ds4
->base
.hdev
, "Failed to retrieve DualShock4 firmware info: %d\n", ret
);
1963 ds4
->base
.hw_version
= get_unaligned_le16(&buf
[35]);
1964 ds4
->base
.fw_version
= get_unaligned_le16(&buf
[41]);
1971 static int dualshock4_get_mac_address(struct dualshock4
*ds4
)
1973 struct hid_device
*hdev
= ds4
->base
.hdev
;
1977 if (hdev
->bus
== BUS_USB
) {
1978 buf
= kzalloc(DS4_FEATURE_REPORT_PAIRING_INFO_SIZE
, GFP_KERNEL
);
1982 ret
= ps_get_report(hdev
, DS4_FEATURE_REPORT_PAIRING_INFO
, buf
,
1983 DS4_FEATURE_REPORT_PAIRING_INFO_SIZE
, false);
1985 hid_err(hdev
, "Failed to retrieve DualShock4 pairing info: %d\n", ret
);
1989 memcpy(ds4
->base
.mac_address
, &buf
[1], sizeof(ds4
->base
.mac_address
));
1991 /* Rely on HIDP for Bluetooth */
1992 if (strlen(hdev
->uniq
) != 17)
1995 ret
= sscanf(hdev
->uniq
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1996 &ds4
->base
.mac_address
[5], &ds4
->base
.mac_address
[4],
1997 &ds4
->base
.mac_address
[3], &ds4
->base
.mac_address
[2],
1998 &ds4
->base
.mac_address
[1], &ds4
->base
.mac_address
[0]);
2000 if (ret
!= sizeof(ds4
->base
.mac_address
))
2011 static enum led_brightness
dualshock4_led_get_brightness(struct led_classdev
*led
)
2013 struct hid_device
*hdev
= to_hid_device(led
->dev
->parent
);
2014 struct dualshock4
*ds4
= hid_get_drvdata(hdev
);
2015 unsigned int led_index
;
2017 led_index
= led
- ds4
->lightbar_leds
;
2018 switch (led_index
) {
2020 return ds4
->lightbar_red
;
2022 return ds4
->lightbar_green
;
2024 return ds4
->lightbar_blue
;
2026 return ds4
->lightbar_enabled
;
2032 static int dualshock4_led_set_blink(struct led_classdev
*led
, unsigned long *delay_on
,
2033 unsigned long *delay_off
)
2035 struct hid_device
*hdev
= to_hid_device(led
->dev
->parent
);
2036 struct dualshock4
*ds4
= hid_get_drvdata(hdev
);
2037 unsigned long flags
;
2039 spin_lock_irqsave(&ds4
->base
.lock
, flags
);
2041 if (!*delay_on
&& !*delay_off
) {
2042 /* Default to 1 Hz (50 centiseconds on, 50 centiseconds off). */
2043 ds4
->lightbar_blink_on
= 50;
2044 ds4
->lightbar_blink_off
= 50;
2046 /* Blink delays in centiseconds. */
2047 ds4
->lightbar_blink_on
= min_t(unsigned long, *delay_on
/10, DS4_LIGHTBAR_MAX_BLINK
);
2048 ds4
->lightbar_blink_off
= min_t(unsigned long, *delay_off
/10, DS4_LIGHTBAR_MAX_BLINK
);
2051 ds4
->update_lightbar_blink
= true;
2053 spin_unlock_irqrestore(&ds4
->base
.lock
, flags
);
2055 dualshock4_schedule_work(ds4
);
2057 /* Report scaled values back to LED subsystem */
2058 *delay_on
= ds4
->lightbar_blink_on
* 10;
2059 *delay_off
= ds4
->lightbar_blink_off
* 10;
2064 static int dualshock4_led_set_brightness(struct led_classdev
*led
, enum led_brightness value
)
2066 struct hid_device
*hdev
= to_hid_device(led
->dev
->parent
);
2067 struct dualshock4
*ds4
= hid_get_drvdata(hdev
);
2068 unsigned long flags
;
2069 unsigned int led_index
;
2071 spin_lock_irqsave(&ds4
->base
.lock
, flags
);
2073 led_index
= led
- ds4
->lightbar_leds
;
2074 switch (led_index
) {
2076 ds4
->lightbar_red
= value
;
2079 ds4
->lightbar_green
= value
;
2082 ds4
->lightbar_blue
= value
;
2085 ds4
->lightbar_enabled
= !!value
;
2087 /* brightness = 0 also cancels blinking in Linux. */
2088 if (!ds4
->lightbar_enabled
) {
2089 ds4
->lightbar_blink_off
= 0;
2090 ds4
->lightbar_blink_on
= 0;
2091 ds4
->update_lightbar_blink
= true;
2095 ds4
->update_lightbar
= true;
2097 spin_unlock_irqrestore(&ds4
->base
.lock
, flags
);
2099 dualshock4_schedule_work(ds4
);
2104 static void dualshock4_init_output_report(struct dualshock4
*ds4
,
2105 struct dualshock4_output_report
*rp
, void *buf
)
2107 struct hid_device
*hdev
= ds4
->base
.hdev
;
2109 if (hdev
->bus
== BUS_BLUETOOTH
) {
2110 struct dualshock4_output_report_bt
*bt
= buf
;
2112 memset(bt
, 0, sizeof(*bt
));
2113 bt
->report_id
= DS4_OUTPUT_REPORT_BT
;
2116 rp
->len
= sizeof(*bt
);
2119 rp
->common
= &bt
->common
;
2121 struct dualshock4_output_report_usb
*usb
= buf
;
2123 memset(usb
, 0, sizeof(*usb
));
2124 usb
->report_id
= DS4_OUTPUT_REPORT_USB
;
2127 rp
->len
= sizeof(*usb
);
2130 rp
->common
= &usb
->common
;
2134 static void dualshock4_output_worker(struct work_struct
*work
)
2136 struct dualshock4
*ds4
= container_of(work
, struct dualshock4
, output_worker
);
2137 struct dualshock4_output_report report
;
2138 struct dualshock4_output_report_common
*common
;
2139 unsigned long flags
;
2141 dualshock4_init_output_report(ds4
, &report
, ds4
->output_report_dmabuf
);
2142 common
= report
.common
;
2144 spin_lock_irqsave(&ds4
->base
.lock
, flags
);
2147 * Some 3rd party gamepads expect updates to rumble and lightbar
2148 * together, and setting one may cancel the other.
2150 * Let's maximise compatibility by always sending rumble and lightbar
2151 * updates together, even when only one has been scheduled, resulting
2154 * ds4->valid_flag0 >= 0x03
2156 * Hopefully this will maximise compatibility with third-party pads.
2158 * Any further update bits, such as 0x04 for lightbar blinking, will
2159 * be or'd on top of this like before.
2161 if (ds4
->update_rumble
|| ds4
->update_lightbar
) {
2162 ds4
->update_rumble
= true; /* 0x01 */
2163 ds4
->update_lightbar
= true; /* 0x02 */
2166 if (ds4
->update_rumble
) {
2167 /* Select classic rumble style haptics and enable it. */
2168 common
->valid_flag0
|= DS4_OUTPUT_VALID_FLAG0_MOTOR
;
2169 common
->motor_left
= ds4
->motor_left
;
2170 common
->motor_right
= ds4
->motor_right
;
2171 ds4
->update_rumble
= false;
2174 if (ds4
->update_lightbar
) {
2175 common
->valid_flag0
|= DS4_OUTPUT_VALID_FLAG0_LED
;
2176 /* Comptabile behavior with hid-sony, which used a dummy global LED to
2177 * allow enabling/disabling the lightbar. The global LED maps to
2180 common
->lightbar_red
= ds4
->lightbar_enabled
? ds4
->lightbar_red
: 0;
2181 common
->lightbar_green
= ds4
->lightbar_enabled
? ds4
->lightbar_green
: 0;
2182 common
->lightbar_blue
= ds4
->lightbar_enabled
? ds4
->lightbar_blue
: 0;
2183 ds4
->update_lightbar
= false;
2186 if (ds4
->update_lightbar_blink
) {
2187 common
->valid_flag0
|= DS4_OUTPUT_VALID_FLAG0_LED_BLINK
;
2188 common
->lightbar_blink_on
= ds4
->lightbar_blink_on
;
2189 common
->lightbar_blink_off
= ds4
->lightbar_blink_off
;
2190 ds4
->update_lightbar_blink
= false;
2193 spin_unlock_irqrestore(&ds4
->base
.lock
, flags
);
2195 /* Bluetooth packets need additional flags as well as a CRC in the last 4 bytes. */
2198 uint8_t seed
= PS_OUTPUT_CRC32_SEED
;
2200 /* Hardware control flags need to set to let the device know
2201 * there is HID data as well as CRC.
2203 report
.bt
->hw_control
= DS4_OUTPUT_HWCTL_HID
| DS4_OUTPUT_HWCTL_CRC32
;
2205 if (ds4
->update_bt_poll_interval
) {
2206 report
.bt
->hw_control
|= ds4
->bt_poll_interval
;
2207 ds4
->update_bt_poll_interval
= false;
2210 crc
= crc32_le(0xFFFFFFFF, &seed
, 1);
2211 crc
= ~crc32_le(crc
, report
.data
, report
.len
- 4);
2213 report
.bt
->crc32
= cpu_to_le32(crc
);
2216 hid_hw_output_report(ds4
->base
.hdev
, report
.data
, report
.len
);
2219 static int dualshock4_parse_report(struct ps_device
*ps_dev
, struct hid_report
*report
,
2222 struct hid_device
*hdev
= ps_dev
->hdev
;
2223 struct dualshock4
*ds4
= container_of(ps_dev
, struct dualshock4
, base
);
2224 struct dualshock4_input_report_common
*ds4_report
;
2225 struct dualshock4_touch_report
*touch_reports
;
2226 uint8_t battery_capacity
, num_touch_reports
, value
;
2227 int battery_status
, i
, j
;
2228 uint16_t sensor_timestamp
;
2229 unsigned long flags
;
2230 bool is_minimal
= false;
2233 * DualShock4 in USB uses the full HID report for reportID 1, but
2234 * Bluetooth uses a minimal HID report for reportID 1 and reports
2235 * the full report using reportID 17.
2237 if (hdev
->bus
== BUS_USB
&& report
->id
== DS4_INPUT_REPORT_USB
&&
2238 size
== DS4_INPUT_REPORT_USB_SIZE
) {
2239 struct dualshock4_input_report_usb
*usb
= (struct dualshock4_input_report_usb
*)data
;
2241 ds4_report
= &usb
->common
;
2242 num_touch_reports
= usb
->num_touch_reports
;
2243 touch_reports
= usb
->touch_reports
;
2244 } else if (hdev
->bus
== BUS_BLUETOOTH
&& report
->id
== DS4_INPUT_REPORT_BT
&&
2245 size
== DS4_INPUT_REPORT_BT_SIZE
) {
2246 struct dualshock4_input_report_bt
*bt
= (struct dualshock4_input_report_bt
*)data
;
2247 uint32_t report_crc
= get_unaligned_le32(&bt
->crc32
);
2249 /* Last 4 bytes of input report contains CRC. */
2250 if (!ps_check_crc32(PS_INPUT_CRC32_SEED
, data
, size
- 4, report_crc
)) {
2251 hid_err(hdev
, "DualShock4 input CRC's check failed\n");
2255 ds4_report
= &bt
->common
;
2256 num_touch_reports
= bt
->num_touch_reports
;
2257 touch_reports
= bt
->touch_reports
;
2258 } else if (hdev
->bus
== BUS_BLUETOOTH
&&
2259 report
->id
== DS4_INPUT_REPORT_BT_MINIMAL
&&
2260 size
== DS4_INPUT_REPORT_BT_MINIMAL_SIZE
) {
2261 /* Some third-party pads never switch to the full 0x11 report.
2262 * The short 0x01 report is 10 bytes long:
2263 * u8 report_id == 0x01
2264 * u8 first_bytes_of_full_report[9]
2265 * So let's reuse the full report parser, and stop it after
2266 * parsing the buttons.
2268 ds4_report
= (struct dualshock4_input_report_common
*)&data
[1];
2271 hid_err(hdev
, "Unhandled reportID=%d\n", report
->id
);
2275 input_report_abs(ds4
->gamepad
, ABS_X
, ds4_report
->x
);
2276 input_report_abs(ds4
->gamepad
, ABS_Y
, ds4_report
->y
);
2277 input_report_abs(ds4
->gamepad
, ABS_RX
, ds4_report
->rx
);
2278 input_report_abs(ds4
->gamepad
, ABS_RY
, ds4_report
->ry
);
2279 input_report_abs(ds4
->gamepad
, ABS_Z
, ds4_report
->z
);
2280 input_report_abs(ds4
->gamepad
, ABS_RZ
, ds4_report
->rz
);
2282 value
= ds4_report
->buttons
[0] & DS_BUTTONS0_HAT_SWITCH
;
2283 if (value
>= ARRAY_SIZE(ps_gamepad_hat_mapping
))
2284 value
= 8; /* center */
2285 input_report_abs(ds4
->gamepad
, ABS_HAT0X
, ps_gamepad_hat_mapping
[value
].x
);
2286 input_report_abs(ds4
->gamepad
, ABS_HAT0Y
, ps_gamepad_hat_mapping
[value
].y
);
2288 input_report_key(ds4
->gamepad
, BTN_WEST
, ds4_report
->buttons
[0] & DS_BUTTONS0_SQUARE
);
2289 input_report_key(ds4
->gamepad
, BTN_SOUTH
, ds4_report
->buttons
[0] & DS_BUTTONS0_CROSS
);
2290 input_report_key(ds4
->gamepad
, BTN_EAST
, ds4_report
->buttons
[0] & DS_BUTTONS0_CIRCLE
);
2291 input_report_key(ds4
->gamepad
, BTN_NORTH
, ds4_report
->buttons
[0] & DS_BUTTONS0_TRIANGLE
);
2292 input_report_key(ds4
->gamepad
, BTN_TL
, ds4_report
->buttons
[1] & DS_BUTTONS1_L1
);
2293 input_report_key(ds4
->gamepad
, BTN_TR
, ds4_report
->buttons
[1] & DS_BUTTONS1_R1
);
2294 input_report_key(ds4
->gamepad
, BTN_TL2
, ds4_report
->buttons
[1] & DS_BUTTONS1_L2
);
2295 input_report_key(ds4
->gamepad
, BTN_TR2
, ds4_report
->buttons
[1] & DS_BUTTONS1_R2
);
2296 input_report_key(ds4
->gamepad
, BTN_SELECT
, ds4_report
->buttons
[1] & DS_BUTTONS1_CREATE
);
2297 input_report_key(ds4
->gamepad
, BTN_START
, ds4_report
->buttons
[1] & DS_BUTTONS1_OPTIONS
);
2298 input_report_key(ds4
->gamepad
, BTN_THUMBL
, ds4_report
->buttons
[1] & DS_BUTTONS1_L3
);
2299 input_report_key(ds4
->gamepad
, BTN_THUMBR
, ds4_report
->buttons
[1] & DS_BUTTONS1_R3
);
2300 input_report_key(ds4
->gamepad
, BTN_MODE
, ds4_report
->buttons
[2] & DS_BUTTONS2_PS_HOME
);
2301 input_sync(ds4
->gamepad
);
2306 /* Parse and calibrate gyroscope data. */
2307 for (i
= 0; i
< ARRAY_SIZE(ds4_report
->gyro
); i
++) {
2308 int raw_data
= (short)le16_to_cpu(ds4_report
->gyro
[i
]);
2309 int calib_data
= mult_frac(ds4
->gyro_calib_data
[i
].sens_numer
,
2310 raw_data
, ds4
->gyro_calib_data
[i
].sens_denom
);
2312 input_report_abs(ds4
->sensors
, ds4
->gyro_calib_data
[i
].abs_code
, calib_data
);
2315 /* Parse and calibrate accelerometer data. */
2316 for (i
= 0; i
< ARRAY_SIZE(ds4_report
->accel
); i
++) {
2317 int raw_data
= (short)le16_to_cpu(ds4_report
->accel
[i
]);
2318 int calib_data
= mult_frac(ds4
->accel_calib_data
[i
].sens_numer
,
2319 raw_data
- ds4
->accel_calib_data
[i
].bias
,
2320 ds4
->accel_calib_data
[i
].sens_denom
);
2322 input_report_abs(ds4
->sensors
, ds4
->accel_calib_data
[i
].abs_code
, calib_data
);
2325 /* Convert timestamp (in 5.33us unit) to timestamp_us */
2326 sensor_timestamp
= le16_to_cpu(ds4_report
->sensor_timestamp
);
2327 if (!ds4
->sensor_timestamp_initialized
) {
2328 ds4
->sensor_timestamp_us
= DIV_ROUND_CLOSEST(sensor_timestamp
*16, 3);
2329 ds4
->sensor_timestamp_initialized
= true;
2333 if (ds4
->prev_sensor_timestamp
> sensor_timestamp
)
2334 delta
= (U16_MAX
- ds4
->prev_sensor_timestamp
+ sensor_timestamp
+ 1);
2336 delta
= sensor_timestamp
- ds4
->prev_sensor_timestamp
;
2337 ds4
->sensor_timestamp_us
+= DIV_ROUND_CLOSEST(delta
*16, 3);
2339 ds4
->prev_sensor_timestamp
= sensor_timestamp
;
2340 input_event(ds4
->sensors
, EV_MSC
, MSC_TIMESTAMP
, ds4
->sensor_timestamp_us
);
2341 input_sync(ds4
->sensors
);
2343 for (i
= 0; i
< num_touch_reports
; i
++) {
2344 struct dualshock4_touch_report
*touch_report
= &touch_reports
[i
];
2346 for (j
= 0; j
< ARRAY_SIZE(touch_report
->points
); j
++) {
2347 struct dualshock4_touch_point
*point
= &touch_report
->points
[j
];
2348 bool active
= (point
->contact
& DS4_TOUCH_POINT_INACTIVE
) ? false : true;
2350 input_mt_slot(ds4
->touchpad
, j
);
2351 input_mt_report_slot_state(ds4
->touchpad
, MT_TOOL_FINGER
, active
);
2354 int x
= (point
->x_hi
<< 8) | point
->x_lo
;
2355 int y
= (point
->y_hi
<< 4) | point
->y_lo
;
2357 input_report_abs(ds4
->touchpad
, ABS_MT_POSITION_X
, x
);
2358 input_report_abs(ds4
->touchpad
, ABS_MT_POSITION_Y
, y
);
2361 input_mt_sync_frame(ds4
->touchpad
);
2362 input_sync(ds4
->touchpad
);
2364 input_report_key(ds4
->touchpad
, BTN_LEFT
, ds4_report
->buttons
[2] & DS_BUTTONS2_TOUCHPAD
);
2367 * Interpretation of the battery_capacity data depends on the cable state.
2368 * When no cable is connected (bit4 is 0):
2369 * - 0:10: percentage in units of 10%.
2370 * When a cable is plugged in:
2371 * - 0-10: percentage in units of 10%.
2372 * - 11: battery is full
2373 * - 14: not charging due to Voltage or temperature error
2374 * - 15: charge error
2376 if (ds4_report
->status
[0] & DS4_STATUS0_CABLE_STATE
) {
2377 uint8_t battery_data
= ds4_report
->status
[0] & DS4_STATUS0_BATTERY_CAPACITY
;
2379 if (battery_data
< 10) {
2380 /* Take the mid-point for each battery capacity value,
2381 * because on the hardware side 0 = 0-9%, 1=10-19%, etc.
2382 * This matches official platform behavior, which does
2385 battery_capacity
= battery_data
* 10 + 5;
2386 battery_status
= POWER_SUPPLY_STATUS_CHARGING
;
2387 } else if (battery_data
== 10) {
2388 battery_capacity
= 100;
2389 battery_status
= POWER_SUPPLY_STATUS_CHARGING
;
2390 } else if (battery_data
== DS4_BATTERY_STATUS_FULL
) {
2391 battery_capacity
= 100;
2392 battery_status
= POWER_SUPPLY_STATUS_FULL
;
2393 } else { /* 14, 15 and undefined values */
2394 battery_capacity
= 0;
2395 battery_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
2398 uint8_t battery_data
= ds4_report
->status
[0] & DS4_STATUS0_BATTERY_CAPACITY
;
2400 if (battery_data
< 10)
2401 battery_capacity
= battery_data
* 10 + 5;
2403 battery_capacity
= 100;
2405 battery_status
= POWER_SUPPLY_STATUS_DISCHARGING
;
2408 spin_lock_irqsave(&ps_dev
->lock
, flags
);
2409 ps_dev
->battery_capacity
= battery_capacity
;
2410 ps_dev
->battery_status
= battery_status
;
2411 spin_unlock_irqrestore(&ps_dev
->lock
, flags
);
2416 static int dualshock4_dongle_parse_report(struct ps_device
*ps_dev
, struct hid_report
*report
,
2419 struct dualshock4
*ds4
= container_of(ps_dev
, struct dualshock4
, base
);
2420 bool connected
= false;
2422 /* The dongle reports data using the main USB report (0x1) no matter whether a controller
2423 * is connected with mostly zeros. The report does contain dongle status, which we use to
2424 * determine if a controller is connected and if so we forward to the regular DualShock4
2427 if (data
[0] == DS4_INPUT_REPORT_USB
&& size
== DS4_INPUT_REPORT_USB_SIZE
) {
2428 struct dualshock4_input_report_common
*ds4_report
= (struct dualshock4_input_report_common
*)&data
[1];
2429 unsigned long flags
;
2431 connected
= ds4_report
->status
[1] & DS4_STATUS1_DONGLE_STATE
? false : true;
2433 if (ds4
->dongle_state
== DONGLE_DISCONNECTED
&& connected
) {
2434 hid_info(ps_dev
->hdev
, "DualShock 4 USB dongle: controller connected\n");
2436 dualshock4_set_default_lightbar_colors(ds4
);
2438 spin_lock_irqsave(&ps_dev
->lock
, flags
);
2439 ds4
->dongle_state
= DONGLE_CALIBRATING
;
2440 spin_unlock_irqrestore(&ps_dev
->lock
, flags
);
2442 schedule_work(&ds4
->dongle_hotplug_worker
);
2444 /* Don't process the report since we don't have
2445 * calibration data, but let hidraw have it anyway.
2448 } else if ((ds4
->dongle_state
== DONGLE_CONNECTED
||
2449 ds4
->dongle_state
== DONGLE_DISABLED
) && !connected
) {
2450 hid_info(ps_dev
->hdev
, "DualShock 4 USB dongle: controller disconnected\n");
2452 spin_lock_irqsave(&ps_dev
->lock
, flags
);
2453 ds4
->dongle_state
= DONGLE_DISCONNECTED
;
2454 spin_unlock_irqrestore(&ps_dev
->lock
, flags
);
2456 /* Return 0, so hidraw can get the report. */
2458 } else if (ds4
->dongle_state
== DONGLE_CALIBRATING
||
2459 ds4
->dongle_state
== DONGLE_DISABLED
||
2460 ds4
->dongle_state
== DONGLE_DISCONNECTED
) {
2461 /* Return 0, so hidraw can get the report. */
2467 return dualshock4_parse_report(ps_dev
, report
, data
, size
);
2472 static int dualshock4_play_effect(struct input_dev
*dev
, void *data
, struct ff_effect
*effect
)
2474 struct hid_device
*hdev
= input_get_drvdata(dev
);
2475 struct dualshock4
*ds4
= hid_get_drvdata(hdev
);
2476 unsigned long flags
;
2478 if (effect
->type
!= FF_RUMBLE
)
2481 spin_lock_irqsave(&ds4
->base
.lock
, flags
);
2482 ds4
->update_rumble
= true;
2483 ds4
->motor_left
= effect
->u
.rumble
.strong_magnitude
/ 256;
2484 ds4
->motor_right
= effect
->u
.rumble
.weak_magnitude
/ 256;
2485 spin_unlock_irqrestore(&ds4
->base
.lock
, flags
);
2487 dualshock4_schedule_work(ds4
);
2491 static void dualshock4_remove(struct ps_device
*ps_dev
)
2493 struct dualshock4
*ds4
= container_of(ps_dev
, struct dualshock4
, base
);
2494 unsigned long flags
;
2496 spin_lock_irqsave(&ds4
->base
.lock
, flags
);
2497 ds4
->output_worker_initialized
= false;
2498 spin_unlock_irqrestore(&ds4
->base
.lock
, flags
);
2500 cancel_work_sync(&ds4
->output_worker
);
2502 if (ps_dev
->hdev
->product
== USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE
)
2503 cancel_work_sync(&ds4
->dongle_hotplug_worker
);
2506 static inline void dualshock4_schedule_work(struct dualshock4
*ds4
)
2508 unsigned long flags
;
2510 spin_lock_irqsave(&ds4
->base
.lock
, flags
);
2511 if (ds4
->output_worker_initialized
)
2512 schedule_work(&ds4
->output_worker
);
2513 spin_unlock_irqrestore(&ds4
->base
.lock
, flags
);
2516 static void dualshock4_set_bt_poll_interval(struct dualshock4
*ds4
, uint8_t interval
)
2518 ds4
->bt_poll_interval
= interval
;
2519 ds4
->update_bt_poll_interval
= true;
2520 dualshock4_schedule_work(ds4
);
2523 /* Set default lightbar color based on player. */
2524 static void dualshock4_set_default_lightbar_colors(struct dualshock4
*ds4
)
2526 /* Use same player colors as PlayStation 4.
2527 * Array of colors is in RGB.
2529 static const int player_colors
[4][3] = {
2530 { 0x00, 0x00, 0x40 }, /* Blue */
2531 { 0x40, 0x00, 0x00 }, /* Red */
2532 { 0x00, 0x40, 0x00 }, /* Green */
2533 { 0x20, 0x00, 0x20 } /* Pink */
2536 uint8_t player_id
= ds4
->base
.player_id
% ARRAY_SIZE(player_colors
);
2538 ds4
->lightbar_enabled
= true;
2539 ds4
->lightbar_red
= player_colors
[player_id
][0];
2540 ds4
->lightbar_green
= player_colors
[player_id
][1];
2541 ds4
->lightbar_blue
= player_colors
[player_id
][2];
2543 ds4
->update_lightbar
= true;
2544 dualshock4_schedule_work(ds4
);
2547 static struct ps_device
*dualshock4_create(struct hid_device
*hdev
)
2549 struct dualshock4
*ds4
;
2550 struct ps_device
*ps_dev
;
2551 uint8_t max_output_report_size
;
2554 /* The DualShock4 has an RGB lightbar, which the original hid-sony driver
2555 * exposed as a set of 4 LEDs for the 3 color channels and a global control.
2556 * Ideally this should have used the multi-color LED class, which didn't exist
2557 * yet. In addition the driver used a naming scheme not compliant with the LED
2558 * naming spec by using "<mac_address>:<color>", which contained many colons.
2559 * We use a more compliant by using "<device_name>:<color>" name now. Ideally
2560 * would have been "<device_name>:<color>:indicator", but that would break
2561 * existing applications (e.g. Android). Nothing matches against MAC address.
2563 static const struct ps_led_info lightbar_leds_info
[] = {
2564 { NULL
, "red", 255, dualshock4_led_get_brightness
, dualshock4_led_set_brightness
},
2565 { NULL
, "green", 255, dualshock4_led_get_brightness
, dualshock4_led_set_brightness
},
2566 { NULL
, "blue", 255, dualshock4_led_get_brightness
, dualshock4_led_set_brightness
},
2567 { NULL
, "global", 1, dualshock4_led_get_brightness
, dualshock4_led_set_brightness
,
2568 dualshock4_led_set_blink
},
2571 ds4
= devm_kzalloc(&hdev
->dev
, sizeof(*ds4
), GFP_KERNEL
);
2573 return ERR_PTR(-ENOMEM
);
2576 * Patch version to allow userspace to distinguish between
2577 * hid-generic vs hid-playstation axis and button mapping.
2579 hdev
->version
|= HID_PLAYSTATION_VERSION_PATCH
;
2581 ps_dev
= &ds4
->base
;
2582 ps_dev
->hdev
= hdev
;
2583 spin_lock_init(&ps_dev
->lock
);
2584 ps_dev
->battery_capacity
= 100; /* initial value until parse_report. */
2585 ps_dev
->battery_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
2586 ps_dev
->parse_report
= dualshock4_parse_report
;
2587 ps_dev
->remove
= dualshock4_remove
;
2588 INIT_WORK(&ds4
->output_worker
, dualshock4_output_worker
);
2589 ds4
->output_worker_initialized
= true;
2590 hid_set_drvdata(hdev
, ds4
);
2592 max_output_report_size
= sizeof(struct dualshock4_output_report_bt
);
2593 ds4
->output_report_dmabuf
= devm_kzalloc(&hdev
->dev
, max_output_report_size
, GFP_KERNEL
);
2594 if (!ds4
->output_report_dmabuf
)
2595 return ERR_PTR(-ENOMEM
);
2597 if (hdev
->product
== USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE
) {
2598 ds4
->dongle_state
= DONGLE_DISCONNECTED
;
2599 INIT_WORK(&ds4
->dongle_hotplug_worker
, dualshock4_dongle_calibration_work
);
2601 /* Override parse report for dongle specific hotplug handling. */
2602 ps_dev
->parse_report
= dualshock4_dongle_parse_report
;
2605 ret
= dualshock4_get_mac_address(ds4
);
2607 hid_err(hdev
, "Failed to get MAC address from DualShock4\n");
2608 return ERR_PTR(ret
);
2610 snprintf(hdev
->uniq
, sizeof(hdev
->uniq
), "%pMR", ds4
->base
.mac_address
);
2612 ret
= dualshock4_get_firmware_info(ds4
);
2614 hid_warn(hdev
, "Failed to get firmware info from DualShock4\n");
2615 hid_warn(hdev
, "HW/FW version data in sysfs will be invalid.\n");
2618 ret
= ps_devices_list_add(ps_dev
);
2620 return ERR_PTR(ret
);
2622 ret
= dualshock4_get_calibration_data(ds4
);
2624 hid_warn(hdev
, "Failed to get calibration data from DualShock4\n");
2625 hid_warn(hdev
, "Gyroscope and accelerometer will be inaccurate.\n");
2628 ds4
->gamepad
= ps_gamepad_create(hdev
, dualshock4_play_effect
);
2629 if (IS_ERR(ds4
->gamepad
)) {
2630 ret
= PTR_ERR(ds4
->gamepad
);
2634 /* Use gamepad input device name as primary device name for e.g. LEDs */
2635 ps_dev
->input_dev_name
= dev_name(&ds4
->gamepad
->dev
);
2637 ds4
->sensors
= ps_sensors_create(hdev
, DS4_ACC_RANGE
, DS4_ACC_RES_PER_G
,
2638 DS4_GYRO_RANGE
, DS4_GYRO_RES_PER_DEG_S
);
2639 if (IS_ERR(ds4
->sensors
)) {
2640 ret
= PTR_ERR(ds4
->sensors
);
2644 ds4
->touchpad
= ps_touchpad_create(hdev
, DS4_TOUCHPAD_WIDTH
, DS4_TOUCHPAD_HEIGHT
, 2);
2645 if (IS_ERR(ds4
->touchpad
)) {
2646 ret
= PTR_ERR(ds4
->touchpad
);
2650 ret
= ps_device_register_battery(ps_dev
);
2654 for (i
= 0; i
< ARRAY_SIZE(lightbar_leds_info
); i
++) {
2655 const struct ps_led_info
*led_info
= &lightbar_leds_info
[i
];
2657 ret
= ps_led_register(ps_dev
, &ds4
->lightbar_leds
[i
], led_info
);
2662 dualshock4_set_bt_poll_interval(ds4
, DS4_BT_DEFAULT_POLL_INTERVAL_MS
);
2664 ret
= ps_device_set_player_id(ps_dev
);
2666 hid_err(hdev
, "Failed to assign player id for DualShock4: %d\n", ret
);
2670 dualshock4_set_default_lightbar_colors(ds4
);
2673 * Reporting hardware and firmware is important as there are frequent updates, which
2674 * can change behavior.
2676 hid_info(hdev
, "Registered DualShock4 controller hw_version=0x%08x fw_version=0x%08x\n",
2677 ds4
->base
.hw_version
, ds4
->base
.fw_version
);
2681 ps_devices_list_remove(ps_dev
);
2682 return ERR_PTR(ret
);
2685 static int ps_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
2688 struct ps_device
*dev
= hid_get_drvdata(hdev
);
2690 if (dev
&& dev
->parse_report
)
2691 return dev
->parse_report(dev
, report
, data
, size
);
2696 static int ps_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
2698 struct ps_device
*dev
;
2701 ret
= hid_parse(hdev
);
2703 hid_err(hdev
, "Parse failed\n");
2707 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
2709 hid_err(hdev
, "Failed to start HID device\n");
2713 ret
= hid_hw_open(hdev
);
2715 hid_err(hdev
, "Failed to open HID device\n");
2719 if (id
->driver_data
== PS_TYPE_PS4_DUALSHOCK4
) {
2720 dev
= dualshock4_create(hdev
);
2722 hid_err(hdev
, "Failed to create dualshock4.\n");
2726 } else if (id
->driver_data
== PS_TYPE_PS5_DUALSENSE
) {
2727 dev
= dualsense_create(hdev
);
2729 hid_err(hdev
, "Failed to create dualsense.\n");
2744 static void ps_remove(struct hid_device
*hdev
)
2746 struct ps_device
*dev
= hid_get_drvdata(hdev
);
2748 ps_devices_list_remove(dev
);
2749 ps_device_release_player_id(dev
);
2758 static const struct hid_device_id ps_devices
[] = {
2759 /* Sony DualShock 4 controllers for PS4 */
2760 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER
),
2761 .driver_data
= PS_TYPE_PS4_DUALSHOCK4
},
2762 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER
),
2763 .driver_data
= PS_TYPE_PS4_DUALSHOCK4
},
2764 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2
),
2765 .driver_data
= PS_TYPE_PS4_DUALSHOCK4
},
2766 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2
),
2767 .driver_data
= PS_TYPE_PS4_DUALSHOCK4
},
2768 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE
),
2769 .driver_data
= PS_TYPE_PS4_DUALSHOCK4
},
2771 /* Sony DualSense controllers for PS5 */
2772 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS5_CONTROLLER
),
2773 .driver_data
= PS_TYPE_PS5_DUALSENSE
},
2774 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS5_CONTROLLER
),
2775 .driver_data
= PS_TYPE_PS5_DUALSENSE
},
2776 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS5_CONTROLLER_2
),
2777 .driver_data
= PS_TYPE_PS5_DUALSENSE
},
2778 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS5_CONTROLLER_2
),
2779 .driver_data
= PS_TYPE_PS5_DUALSENSE
},
2782 MODULE_DEVICE_TABLE(hid
, ps_devices
);
2784 static struct hid_driver ps_driver
= {
2785 .name
= "playstation",
2786 .id_table
= ps_devices
,
2788 .remove
= ps_remove
,
2789 .raw_event
= ps_raw_event
,
2791 .dev_groups
= ps_device_groups
,
2795 static int __init
ps_init(void)
2797 return hid_register_driver(&ps_driver
);
2800 static void __exit
ps_exit(void)
2802 hid_unregister_driver(&ps_driver
);
2803 ida_destroy(&ps_player_id_allocator
);
2806 module_init(ps_init
);
2807 module_exit(ps_exit
);
2809 MODULE_AUTHOR("Sony Interactive Entertainment");
2810 MODULE_DESCRIPTION("HID Driver for PlayStation peripherals.");
2811 MODULE_LICENSE("GPL");