1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HID driver for Sony DualSense(TM) controller.
5 * Copyright (c) 2020 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 <asm/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
30 /* Base class for playstation devices. */
32 struct list_head list;
33 struct hid_device *hdev;
38 struct power_supply_desc battery_desc;
39 struct power_supply *battery;
40 uint8_t battery_capacity;
43 const char *input_dev_name; /* Name of primary input device. */
44 uint8_t mac_address[6]; /* Note: stored in little endian order. */
48 int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size);
51 /* Calibration data for playstation motion sensors. */
52 struct ps_calibration_data {
62 enum led_brightness (*brightness_get)(struct led_classdev *cdev);
63 int (*brightness_set)(struct led_classdev *cdev, enum led_brightness);
66 /* Seed values for DualShock4 / DualSense CRC32 for different report types. */
67 #define PS_INPUT_CRC32_SEED 0xA1
68 #define PS_OUTPUT_CRC32_SEED 0xA2
69 #define PS_FEATURE_CRC32_SEED 0xA3
71 #define DS_INPUT_REPORT_USB 0x01
72 #define DS_INPUT_REPORT_USB_SIZE 64
73 #define DS_INPUT_REPORT_BT 0x31
74 #define DS_INPUT_REPORT_BT_SIZE 78
75 #define DS_OUTPUT_REPORT_USB 0x02
76 #define DS_OUTPUT_REPORT_USB_SIZE 63
77 #define DS_OUTPUT_REPORT_BT 0x31
78 #define DS_OUTPUT_REPORT_BT_SIZE 78
80 #define DS_FEATURE_REPORT_CALIBRATION 0x05
81 #define DS_FEATURE_REPORT_CALIBRATION_SIZE 41
82 #define DS_FEATURE_REPORT_PAIRING_INFO 0x09
83 #define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20
84 #define DS_FEATURE_REPORT_FIRMWARE_INFO 0x20
85 #define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE 64
87 /* Button masks for DualSense input report. */
88 #define DS_BUTTONS0_HAT_SWITCH GENMASK(3, 0)
89 #define DS_BUTTONS0_SQUARE BIT(4)
90 #define DS_BUTTONS0_CROSS BIT(5)
91 #define DS_BUTTONS0_CIRCLE BIT(6)
92 #define DS_BUTTONS0_TRIANGLE BIT(7)
93 #define DS_BUTTONS1_L1 BIT(0)
94 #define DS_BUTTONS1_R1 BIT(1)
95 #define DS_BUTTONS1_L2 BIT(2)
96 #define DS_BUTTONS1_R2 BIT(3)
97 #define DS_BUTTONS1_CREATE BIT(4)
98 #define DS_BUTTONS1_OPTIONS BIT(5)
99 #define DS_BUTTONS1_L3 BIT(6)
100 #define DS_BUTTONS1_R3 BIT(7)
101 #define DS_BUTTONS2_PS_HOME BIT(0)
102 #define DS_BUTTONS2_TOUCHPAD BIT(1)
103 #define DS_BUTTONS2_MIC_MUTE BIT(2)
105 /* Status field of DualSense input report. */
106 #define DS_STATUS_BATTERY_CAPACITY GENMASK(3, 0)
107 #define DS_STATUS_CHARGING GENMASK(7, 4)
108 #define DS_STATUS_CHARGING_SHIFT 4
111 * Status of a DualSense touch point contact.
112 * Contact IDs, with highest bit set are 'inactive'
113 * and any associated data is then invalid.
115 #define DS_TOUCH_POINT_INACTIVE BIT(7)
117 /* Magic value required in tag field of Bluetooth output report. */
118 #define DS_OUTPUT_TAG 0x10
119 /* Flags for DualSense output report. */
120 #define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0)
121 #define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1)
122 #define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0)
123 #define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1)
124 #define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2)
125 #define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3)
126 #define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4)
127 #define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1)
128 #define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4)
129 #define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1)
131 /* DualSense hardware limits */
132 #define DS_ACC_RES_PER_G 8192
133 #define DS_ACC_RANGE (4*DS_ACC_RES_PER_G)
134 #define DS_GYRO_RES_PER_DEG_S 1024
135 #define DS_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S)
136 #define DS_TOUCHPAD_WIDTH 1920
137 #define DS_TOUCHPAD_HEIGHT 1080
140 struct ps_device base;
141 struct input_dev *gamepad;
142 struct input_dev *sensors;
143 struct input_dev *touchpad;
145 /* Calibration data for accelerometer and gyroscope. */
146 struct ps_calibration_data accel_calib_data[3];
147 struct ps_calibration_data gyro_calib_data[3];
149 /* Timestamp for sensor data */
150 bool sensor_timestamp_initialized;
151 uint32_t prev_sensor_timestamp;
152 uint32_t sensor_timestamp_us;
154 /* Compatible rumble state */
160 struct led_classdev_mc lightbar;
161 bool update_lightbar;
162 uint8_t lightbar_red;
163 uint8_t lightbar_green;
164 uint8_t lightbar_blue;
167 bool update_mic_mute;
169 bool last_btn_mic_state;
172 bool update_player_leds;
173 uint8_t player_leds_state;
174 struct led_classdev player_leds[5];
176 struct work_struct output_worker;
177 void *output_report_dmabuf;
178 uint8_t output_seq; /* Sequence number for output report. */
181 struct dualsense_touch_point {
184 uint8_t x_hi:4, y_lo:4;
187 static_assert(sizeof(struct dualsense_touch_point) == 4);
189 /* Main DualSense input report excluding any BT/USB specific headers. */
190 struct dualsense_input_report {
199 __le16 gyro[3]; /* x, y, z */
200 __le16 accel[3]; /* x, y, z */
201 __le32 sensor_timestamp;
205 struct dualsense_touch_point points[2];
207 uint8_t reserved3[12];
209 uint8_t reserved4[10];
211 /* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */
212 static_assert(sizeof(struct dualsense_input_report) == DS_INPUT_REPORT_USB_SIZE - 1);
214 /* Common data between DualSense BT/USB main output report. */
215 struct dualsense_output_report_common {
219 /* For DualShock 4 compatibility mode. */
225 uint8_t mute_button_led;
227 uint8_t power_save_control;
228 uint8_t reserved2[28];
230 /* LEDs and lightbar */
232 uint8_t reserved3[2];
233 uint8_t lightbar_setup;
234 uint8_t led_brightness;
236 uint8_t lightbar_red;
237 uint8_t lightbar_green;
238 uint8_t lightbar_blue;
240 static_assert(sizeof(struct dualsense_output_report_common) == 47);
242 struct dualsense_output_report_bt {
243 uint8_t report_id; /* 0x31 */
246 struct dualsense_output_report_common common;
247 uint8_t reserved[24];
250 static_assert(sizeof(struct dualsense_output_report_bt) == DS_OUTPUT_REPORT_BT_SIZE);
252 struct dualsense_output_report_usb {
253 uint8_t report_id; /* 0x02 */
254 struct dualsense_output_report_common common;
255 uint8_t reserved[15];
257 static_assert(sizeof(struct dualsense_output_report_usb) == DS_OUTPUT_REPORT_USB_SIZE);
260 * The DualSense has a main output report used to control most features. It is
261 * largely the same between Bluetooth and USB except for different headers and CRC.
262 * This structure hide the differences between the two to simplify sending output reports.
264 struct dualsense_output_report {
265 uint8_t *data; /* Start of data */
266 uint8_t len; /* Size of output report */
268 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
269 struct dualsense_output_report_bt *bt;
270 /* Points to USB data payload in case for a USB report else NULL. */
271 struct dualsense_output_report_usb *usb;
272 /* Points to common section of report, so past any headers. */
273 struct dualsense_output_report_common *common;
277 * Common gamepad buttons across DualShock 3 / 4 and DualSense.
278 * Note: for device with a touchpad, touchpad button is not included
279 * as it will be part of the touchpad device.
281 static const int ps_gamepad_buttons[] = {
282 BTN_WEST, /* Square */
283 BTN_NORTH, /* Triangle */
284 BTN_EAST, /* Circle */
285 BTN_SOUTH, /* Cross */
290 BTN_SELECT, /* Create (PS5) / Share (PS4) */
291 BTN_START, /* Option */
294 BTN_MODE, /* PS Home */
297 static const struct {int x; int y; } ps_gamepad_hat_mapping[] = {
298 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
302 static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue);
305 * Add a new ps_device to ps_devices if it doesn't exist.
306 * Return error on duplicate device, which can happen if the same
307 * device is connected using both Bluetooth and USB.
309 static int ps_devices_list_add(struct ps_device *dev)
311 struct ps_device *entry;
313 mutex_lock(&ps_devices_lock);
314 list_for_each_entry(entry, &ps_devices_list, list) {
315 if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) {
316 hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n",
318 mutex_unlock(&ps_devices_lock);
323 list_add_tail(&dev->list, &ps_devices_list);
324 mutex_unlock(&ps_devices_lock);
328 static int ps_devices_list_remove(struct ps_device *dev)
330 mutex_lock(&ps_devices_lock);
331 list_del(&dev->list);
332 mutex_unlock(&ps_devices_lock);
336 static int ps_device_set_player_id(struct ps_device *dev)
338 int ret = ida_alloc(&ps_player_id_allocator, GFP_KERNEL);
343 dev->player_id = ret;
347 static void ps_device_release_player_id(struct ps_device *dev)
349 ida_free(&ps_player_id_allocator, dev->player_id);
351 dev->player_id = U32_MAX;
354 static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix)
356 struct input_dev *input_dev;
358 input_dev = devm_input_allocate_device(&hdev->dev);
360 return ERR_PTR(-ENOMEM);
362 input_dev->id.bustype = hdev->bus;
363 input_dev->id.vendor = hdev->vendor;
364 input_dev->id.product = hdev->product;
365 input_dev->id.version = hdev->version;
366 input_dev->uniq = hdev->uniq;
369 input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name,
371 if (!input_dev->name)
372 return ERR_PTR(-ENOMEM);
374 input_dev->name = hdev->name;
377 input_set_drvdata(input_dev, hdev);
382 static enum power_supply_property ps_power_supply_props[] = {
383 POWER_SUPPLY_PROP_STATUS,
384 POWER_SUPPLY_PROP_PRESENT,
385 POWER_SUPPLY_PROP_CAPACITY,
386 POWER_SUPPLY_PROP_SCOPE,
389 static int ps_battery_get_property(struct power_supply *psy,
390 enum power_supply_property psp,
391 union power_supply_propval *val)
393 struct ps_device *dev = power_supply_get_drvdata(psy);
394 uint8_t battery_capacity;
399 spin_lock_irqsave(&dev->lock, flags);
400 battery_capacity = dev->battery_capacity;
401 battery_status = dev->battery_status;
402 spin_unlock_irqrestore(&dev->lock, flags);
405 case POWER_SUPPLY_PROP_STATUS:
406 val->intval = battery_status;
408 case POWER_SUPPLY_PROP_PRESENT:
411 case POWER_SUPPLY_PROP_CAPACITY:
412 val->intval = battery_capacity;
414 case POWER_SUPPLY_PROP_SCOPE:
415 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
425 static int ps_device_register_battery(struct ps_device *dev)
427 struct power_supply *battery;
428 struct power_supply_config battery_cfg = { .drv_data = dev };
431 dev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
432 dev->battery_desc.properties = ps_power_supply_props;
433 dev->battery_desc.num_properties = ARRAY_SIZE(ps_power_supply_props);
434 dev->battery_desc.get_property = ps_battery_get_property;
435 dev->battery_desc.name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
436 "ps-controller-battery-%pMR", dev->mac_address);
437 if (!dev->battery_desc.name)
440 battery = devm_power_supply_register(&dev->hdev->dev, &dev->battery_desc, &battery_cfg);
441 if (IS_ERR(battery)) {
442 ret = PTR_ERR(battery);
443 hid_err(dev->hdev, "Unable to register battery device: %d\n", ret);
446 dev->battery = battery;
448 ret = power_supply_powers(dev->battery, &dev->hdev->dev);
450 hid_err(dev->hdev, "Unable to activate battery device: %d\n", ret);
457 /* Compute crc32 of HID data and compare against expected CRC. */
458 static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc)
462 crc = crc32_le(0xFFFFFFFF, &seed, 1);
463 crc = ~crc32_le(crc, data, len);
465 return crc == report_crc;
468 static struct input_dev *ps_gamepad_create(struct hid_device *hdev,
469 int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
471 struct input_dev *gamepad;
475 gamepad = ps_allocate_input_dev(hdev, NULL);
477 return ERR_CAST(gamepad);
479 input_set_abs_params(gamepad, ABS_X, 0, 255, 0, 0);
480 input_set_abs_params(gamepad, ABS_Y, 0, 255, 0, 0);
481 input_set_abs_params(gamepad, ABS_Z, 0, 255, 0, 0);
482 input_set_abs_params(gamepad, ABS_RX, 0, 255, 0, 0);
483 input_set_abs_params(gamepad, ABS_RY, 0, 255, 0, 0);
484 input_set_abs_params(gamepad, ABS_RZ, 0, 255, 0, 0);
486 input_set_abs_params(gamepad, ABS_HAT0X, -1, 1, 0, 0);
487 input_set_abs_params(gamepad, ABS_HAT0Y, -1, 1, 0, 0);
489 for (i = 0; i < ARRAY_SIZE(ps_gamepad_buttons); i++)
490 input_set_capability(gamepad, EV_KEY, ps_gamepad_buttons[i]);
492 #if IS_ENABLED(CONFIG_PLAYSTATION_FF)
494 input_set_capability(gamepad, EV_FF, FF_RUMBLE);
495 input_ff_create_memless(gamepad, NULL, play_effect);
499 ret = input_register_device(gamepad);
506 static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *buf, size_t size)
510 ret = hid_hw_raw_request(hdev, report_id, buf, size, HID_FEATURE_REPORT,
513 hid_err(hdev, "Failed to retrieve feature with reportID %d: %d\n", report_id, ret);
518 hid_err(hdev, "Invalid byte count transferred, expected %zu got %d\n", size, ret);
522 if (buf[0] != report_id) {
523 hid_err(hdev, "Invalid reportID received, expected %d got %d\n", report_id, buf[0]);
527 if (hdev->bus == BUS_BLUETOOTH) {
528 /* Last 4 bytes contains crc32. */
529 uint8_t crc_offset = size - 4;
530 uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]);
532 if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) {
533 hid_err(hdev, "CRC check failed for reportID=%d\n", report_id);
541 static int ps_led_register(struct ps_device *ps_dev, struct led_classdev *led,
542 const struct ps_led_info *led_info)
546 led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL,
547 "%s:%s:%s", ps_dev->input_dev_name, led_info->color, led_info->name);
553 led->max_brightness = 1;
554 led->flags = LED_CORE_SUSPENDRESUME;
555 led->brightness_get = led_info->brightness_get;
556 led->brightness_set_blocking = led_info->brightness_set;
558 ret = devm_led_classdev_register(&ps_dev->hdev->dev, led);
560 hid_err(ps_dev->hdev, "Failed to register LED %s: %d\n", led_info->name, ret);
567 /* Register a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */
568 static int ps_lightbar_register(struct ps_device *ps_dev, struct led_classdev_mc *lightbar_mc_dev,
569 int (*brightness_set)(struct led_classdev *, enum led_brightness))
571 struct hid_device *hdev = ps_dev->hdev;
572 struct mc_subled *mc_led_info;
573 struct led_classdev *led_cdev;
576 mc_led_info = devm_kmalloc_array(&hdev->dev, 3, sizeof(*mc_led_info),
577 GFP_KERNEL | __GFP_ZERO);
581 mc_led_info[0].color_index = LED_COLOR_ID_RED;
582 mc_led_info[1].color_index = LED_COLOR_ID_GREEN;
583 mc_led_info[2].color_index = LED_COLOR_ID_BLUE;
585 lightbar_mc_dev->subled_info = mc_led_info;
586 lightbar_mc_dev->num_colors = 3;
588 led_cdev = &lightbar_mc_dev->led_cdev;
589 led_cdev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s:rgb:indicator",
590 ps_dev->input_dev_name);
593 led_cdev->brightness = 255;
594 led_cdev->max_brightness = 255;
595 led_cdev->brightness_set_blocking = brightness_set;
597 ret = devm_led_classdev_multicolor_register(&hdev->dev, lightbar_mc_dev);
599 hid_err(hdev, "Cannot register multicolor LED device\n");
606 static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res,
607 int gyro_range, int gyro_res)
609 struct input_dev *sensors;
612 sensors = ps_allocate_input_dev(hdev, "Motion Sensors");
614 return ERR_CAST(sensors);
616 __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit);
617 __set_bit(EV_MSC, sensors->evbit);
618 __set_bit(MSC_TIMESTAMP, sensors->mscbit);
621 input_set_abs_params(sensors, ABS_X, -accel_range, accel_range, 16, 0);
622 input_set_abs_params(sensors, ABS_Y, -accel_range, accel_range, 16, 0);
623 input_set_abs_params(sensors, ABS_Z, -accel_range, accel_range, 16, 0);
624 input_abs_set_res(sensors, ABS_X, accel_res);
625 input_abs_set_res(sensors, ABS_Y, accel_res);
626 input_abs_set_res(sensors, ABS_Z, accel_res);
629 input_set_abs_params(sensors, ABS_RX, -gyro_range, gyro_range, 16, 0);
630 input_set_abs_params(sensors, ABS_RY, -gyro_range, gyro_range, 16, 0);
631 input_set_abs_params(sensors, ABS_RZ, -gyro_range, gyro_range, 16, 0);
632 input_abs_set_res(sensors, ABS_RX, gyro_res);
633 input_abs_set_res(sensors, ABS_RY, gyro_res);
634 input_abs_set_res(sensors, ABS_RZ, gyro_res);
636 ret = input_register_device(sensors);
643 static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, int height,
644 unsigned int num_contacts)
646 struct input_dev *touchpad;
649 touchpad = ps_allocate_input_dev(hdev, "Touchpad");
650 if (IS_ERR(touchpad))
651 return ERR_CAST(touchpad);
653 /* Map button underneath touchpad to BTN_LEFT. */
654 input_set_capability(touchpad, EV_KEY, BTN_LEFT);
655 __set_bit(INPUT_PROP_BUTTONPAD, touchpad->propbit);
657 input_set_abs_params(touchpad, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
658 input_set_abs_params(touchpad, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
660 ret = input_mt_init_slots(touchpad, num_contacts, INPUT_MT_POINTER);
664 ret = input_register_device(touchpad);
671 static ssize_t firmware_version_show(struct device *dev,
672 struct device_attribute
675 struct hid_device *hdev = to_hid_device(dev);
676 struct ps_device *ps_dev = hid_get_drvdata(hdev);
678 return sysfs_emit(buf, "0x%08x\n", ps_dev->fw_version);
681 static DEVICE_ATTR_RO(firmware_version);
683 static ssize_t hardware_version_show(struct device *dev,
684 struct device_attribute
687 struct hid_device *hdev = to_hid_device(dev);
688 struct ps_device *ps_dev = hid_get_drvdata(hdev);
690 return sysfs_emit(buf, "0x%08x\n", ps_dev->hw_version);
693 static DEVICE_ATTR_RO(hardware_version);
695 static struct attribute *ps_device_attributes[] = {
696 &dev_attr_firmware_version.attr,
697 &dev_attr_hardware_version.attr,
701 static const struct attribute_group ps_device_attribute_group = {
702 .attrs = ps_device_attributes,
705 static int dualsense_get_calibration_data(struct dualsense *ds)
707 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
708 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
709 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
710 short gyro_speed_plus, gyro_speed_minus;
711 short acc_x_plus, acc_x_minus;
712 short acc_y_plus, acc_y_minus;
713 short acc_z_plus, acc_z_minus;
719 buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL);
723 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_CALIBRATION, buf,
724 DS_FEATURE_REPORT_CALIBRATION_SIZE);
726 hid_err(ds->base.hdev, "Failed to retrieve DualSense calibration info: %d\n", ret);
730 gyro_pitch_bias = get_unaligned_le16(&buf[1]);
731 gyro_yaw_bias = get_unaligned_le16(&buf[3]);
732 gyro_roll_bias = get_unaligned_le16(&buf[5]);
733 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
734 gyro_pitch_minus = get_unaligned_le16(&buf[9]);
735 gyro_yaw_plus = get_unaligned_le16(&buf[11]);
736 gyro_yaw_minus = get_unaligned_le16(&buf[13]);
737 gyro_roll_plus = get_unaligned_le16(&buf[15]);
738 gyro_roll_minus = get_unaligned_le16(&buf[17]);
739 gyro_speed_plus = get_unaligned_le16(&buf[19]);
740 gyro_speed_minus = get_unaligned_le16(&buf[21]);
741 acc_x_plus = get_unaligned_le16(&buf[23]);
742 acc_x_minus = get_unaligned_le16(&buf[25]);
743 acc_y_plus = get_unaligned_le16(&buf[27]);
744 acc_y_minus = get_unaligned_le16(&buf[29]);
745 acc_z_plus = get_unaligned_le16(&buf[31]);
746 acc_z_minus = get_unaligned_le16(&buf[33]);
749 * Set gyroscope calibration and normalization parameters.
750 * Data values will be normalized to 1/DS_GYRO_RES_PER_DEG_S degree/s.
752 speed_2x = (gyro_speed_plus + gyro_speed_minus);
753 ds->gyro_calib_data[0].abs_code = ABS_RX;
754 ds->gyro_calib_data[0].bias = gyro_pitch_bias;
755 ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
756 ds->gyro_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
758 ds->gyro_calib_data[1].abs_code = ABS_RY;
759 ds->gyro_calib_data[1].bias = gyro_yaw_bias;
760 ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
761 ds->gyro_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
763 ds->gyro_calib_data[2].abs_code = ABS_RZ;
764 ds->gyro_calib_data[2].bias = gyro_roll_bias;
765 ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
766 ds->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
769 * Set accelerometer calibration and normalization parameters.
770 * Data values will be normalized to 1/DS_ACC_RES_PER_G g.
772 range_2g = acc_x_plus - acc_x_minus;
773 ds->accel_calib_data[0].abs_code = ABS_X;
774 ds->accel_calib_data[0].bias = acc_x_plus - range_2g / 2;
775 ds->accel_calib_data[0].sens_numer = 2*DS_ACC_RES_PER_G;
776 ds->accel_calib_data[0].sens_denom = range_2g;
778 range_2g = acc_y_plus - acc_y_minus;
779 ds->accel_calib_data[1].abs_code = ABS_Y;
780 ds->accel_calib_data[1].bias = acc_y_plus - range_2g / 2;
781 ds->accel_calib_data[1].sens_numer = 2*DS_ACC_RES_PER_G;
782 ds->accel_calib_data[1].sens_denom = range_2g;
784 range_2g = acc_z_plus - acc_z_minus;
785 ds->accel_calib_data[2].abs_code = ABS_Z;
786 ds->accel_calib_data[2].bias = acc_z_plus - range_2g / 2;
787 ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G;
788 ds->accel_calib_data[2].sens_denom = range_2g;
795 static int dualsense_get_firmware_info(struct dualsense *ds)
800 buf = kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL);
804 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_FIRMWARE_INFO, buf,
805 DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE);
807 hid_err(ds->base.hdev, "Failed to retrieve DualSense firmware info: %d\n", ret);
811 ds->base.hw_version = get_unaligned_le32(&buf[24]);
812 ds->base.fw_version = get_unaligned_le32(&buf[28]);
819 static int dualsense_get_mac_address(struct dualsense *ds)
824 buf = kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL);
828 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_PAIRING_INFO, buf,
829 DS_FEATURE_REPORT_PAIRING_INFO_SIZE);
831 hid_err(ds->base.hdev, "Failed to retrieve DualSense pairing info: %d\n", ret);
835 memcpy(ds->base.mac_address, &buf[1], sizeof(ds->base.mac_address));
842 static int dualsense_lightbar_set_brightness(struct led_classdev *cdev,
843 enum led_brightness brightness)
845 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
846 struct dualsense *ds = container_of(mc_cdev, struct dualsense, lightbar);
847 uint8_t red, green, blue;
849 led_mc_calc_color_components(mc_cdev, brightness);
850 red = mc_cdev->subled_info[0].brightness;
851 green = mc_cdev->subled_info[1].brightness;
852 blue = mc_cdev->subled_info[2].brightness;
854 dualsense_set_lightbar(ds, red, green, blue);
858 static enum led_brightness dualsense_player_led_get_brightness(struct led_classdev *led)
860 struct hid_device *hdev = to_hid_device(led->dev->parent);
861 struct dualsense *ds = hid_get_drvdata(hdev);
863 return !!(ds->player_leds_state & BIT(led - ds->player_leds));
866 static int dualsense_player_led_set_brightness(struct led_classdev *led, enum led_brightness value)
868 struct hid_device *hdev = to_hid_device(led->dev->parent);
869 struct dualsense *ds = hid_get_drvdata(hdev);
871 unsigned int led_index;
873 spin_lock_irqsave(&ds->base.lock, flags);
875 led_index = led - ds->player_leds;
876 if (value == LED_OFF)
877 ds->player_leds_state &= ~BIT(led_index);
879 ds->player_leds_state |= BIT(led_index);
881 ds->update_player_leds = true;
882 spin_unlock_irqrestore(&ds->base.lock, flags);
884 schedule_work(&ds->output_worker);
889 static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp,
892 struct hid_device *hdev = ds->base.hdev;
894 if (hdev->bus == BUS_BLUETOOTH) {
895 struct dualsense_output_report_bt *bt = buf;
897 memset(bt, 0, sizeof(*bt));
898 bt->report_id = DS_OUTPUT_REPORT_BT;
899 bt->tag = DS_OUTPUT_TAG; /* Tag must be set. Exact meaning is unclear. */
902 * Highest 4-bit is a sequence number, which needs to be increased
903 * every report. Lowest 4-bit is tag and can be zero for now.
905 bt->seq_tag = (ds->output_seq << 4) | 0x0;
906 if (++ds->output_seq == 16)
910 rp->len = sizeof(*bt);
913 rp->common = &bt->common;
915 struct dualsense_output_report_usb *usb = buf;
917 memset(usb, 0, sizeof(*usb));
918 usb->report_id = DS_OUTPUT_REPORT_USB;
921 rp->len = sizeof(*usb);
924 rp->common = &usb->common;
929 * Helper function to send DualSense output reports. Applies a CRC at the end of a report
930 * for Bluetooth reports.
932 static void dualsense_send_output_report(struct dualsense *ds,
933 struct dualsense_output_report *report)
935 struct hid_device *hdev = ds->base.hdev;
937 /* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */
940 uint8_t seed = PS_OUTPUT_CRC32_SEED;
942 crc = crc32_le(0xFFFFFFFF, &seed, 1);
943 crc = ~crc32_le(crc, report->data, report->len - 4);
945 report->bt->crc32 = cpu_to_le32(crc);
948 hid_hw_output_report(hdev, report->data, report->len);
951 static void dualsense_output_worker(struct work_struct *work)
953 struct dualsense *ds = container_of(work, struct dualsense, output_worker);
954 struct dualsense_output_report report;
955 struct dualsense_output_report_common *common;
958 dualsense_init_output_report(ds, &report, ds->output_report_dmabuf);
959 common = report.common;
961 spin_lock_irqsave(&ds->base.lock, flags);
963 if (ds->update_rumble) {
964 /* Select classic rumble style haptics and enable it. */
965 common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT;
966 common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION;
967 common->motor_left = ds->motor_left;
968 common->motor_right = ds->motor_right;
969 ds->update_rumble = false;
972 if (ds->update_lightbar) {
973 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE;
974 common->lightbar_red = ds->lightbar_red;
975 common->lightbar_green = ds->lightbar_green;
976 common->lightbar_blue = ds->lightbar_blue;
978 ds->update_lightbar = false;
981 if (ds->update_player_leds) {
982 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE;
983 common->player_leds = ds->player_leds_state;
985 ds->update_player_leds = false;
988 if (ds->update_mic_mute) {
989 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE;
990 common->mute_button_led = ds->mic_muted;
993 /* Disable microphone */
994 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
995 common->power_save_control |= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
997 /* Enable microphone */
998 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
999 common->power_save_control &= ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
1002 ds->update_mic_mute = false;
1005 spin_unlock_irqrestore(&ds->base.lock, flags);
1007 dualsense_send_output_report(ds, &report);
1010 static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *report,
1013 struct hid_device *hdev = ps_dev->hdev;
1014 struct dualsense *ds = container_of(ps_dev, struct dualsense, base);
1015 struct dualsense_input_report *ds_report;
1016 uint8_t battery_data, battery_capacity, charging_status, value;
1018 uint32_t sensor_timestamp;
1020 unsigned long flags;
1024 * DualSense in USB uses the full HID report for reportID 1, but
1025 * Bluetooth uses a minimal HID report for reportID 1 and reports
1026 * the full report using reportID 49.
1028 if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB &&
1029 size == DS_INPUT_REPORT_USB_SIZE) {
1030 ds_report = (struct dualsense_input_report *)&data[1];
1031 } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT &&
1032 size == DS_INPUT_REPORT_BT_SIZE) {
1033 /* Last 4 bytes of input report contain crc32 */
1034 uint32_t report_crc = get_unaligned_le32(&data[size - 4]);
1036 if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) {
1037 hid_err(hdev, "DualSense input CRC's check failed\n");
1041 ds_report = (struct dualsense_input_report *)&data[2];
1043 hid_err(hdev, "Unhandled reportID=%d\n", report->id);
1047 input_report_abs(ds->gamepad, ABS_X, ds_report->x);
1048 input_report_abs(ds->gamepad, ABS_Y, ds_report->y);
1049 input_report_abs(ds->gamepad, ABS_RX, ds_report->rx);
1050 input_report_abs(ds->gamepad, ABS_RY, ds_report->ry);
1051 input_report_abs(ds->gamepad, ABS_Z, ds_report->z);
1052 input_report_abs(ds->gamepad, ABS_RZ, ds_report->rz);
1054 value = ds_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH;
1055 if (value >= ARRAY_SIZE(ps_gamepad_hat_mapping))
1056 value = 8; /* center */
1057 input_report_abs(ds->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x);
1058 input_report_abs(ds->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y);
1060 input_report_key(ds->gamepad, BTN_WEST, ds_report->buttons[0] & DS_BUTTONS0_SQUARE);
1061 input_report_key(ds->gamepad, BTN_SOUTH, ds_report->buttons[0] & DS_BUTTONS0_CROSS);
1062 input_report_key(ds->gamepad, BTN_EAST, ds_report->buttons[0] & DS_BUTTONS0_CIRCLE);
1063 input_report_key(ds->gamepad, BTN_NORTH, ds_report->buttons[0] & DS_BUTTONS0_TRIANGLE);
1064 input_report_key(ds->gamepad, BTN_TL, ds_report->buttons[1] & DS_BUTTONS1_L1);
1065 input_report_key(ds->gamepad, BTN_TR, ds_report->buttons[1] & DS_BUTTONS1_R1);
1066 input_report_key(ds->gamepad, BTN_TL2, ds_report->buttons[1] & DS_BUTTONS1_L2);
1067 input_report_key(ds->gamepad, BTN_TR2, ds_report->buttons[1] & DS_BUTTONS1_R2);
1068 input_report_key(ds->gamepad, BTN_SELECT, ds_report->buttons[1] & DS_BUTTONS1_CREATE);
1069 input_report_key(ds->gamepad, BTN_START, ds_report->buttons[1] & DS_BUTTONS1_OPTIONS);
1070 input_report_key(ds->gamepad, BTN_THUMBL, ds_report->buttons[1] & DS_BUTTONS1_L3);
1071 input_report_key(ds->gamepad, BTN_THUMBR, ds_report->buttons[1] & DS_BUTTONS1_R3);
1072 input_report_key(ds->gamepad, BTN_MODE, ds_report->buttons[2] & DS_BUTTONS2_PS_HOME);
1073 input_sync(ds->gamepad);
1076 * The DualSense has an internal microphone, which can be muted through a mute button
1077 * on the device. The driver is expected to read the button state and program the device
1078 * to mute/unmute audio at the hardware level.
1080 btn_mic_state = !!(ds_report->buttons[2] & DS_BUTTONS2_MIC_MUTE);
1081 if (btn_mic_state && !ds->last_btn_mic_state) {
1082 spin_lock_irqsave(&ps_dev->lock, flags);
1083 ds->update_mic_mute = true;
1084 ds->mic_muted = !ds->mic_muted; /* toggle */
1085 spin_unlock_irqrestore(&ps_dev->lock, flags);
1087 /* Schedule updating of microphone state at hardware level. */
1088 schedule_work(&ds->output_worker);
1090 ds->last_btn_mic_state = btn_mic_state;
1092 /* Parse and calibrate gyroscope data. */
1093 for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) {
1094 int raw_data = (short)le16_to_cpu(ds_report->gyro[i]);
1095 int calib_data = mult_frac(ds->gyro_calib_data[i].sens_numer,
1096 raw_data - ds->gyro_calib_data[i].bias,
1097 ds->gyro_calib_data[i].sens_denom);
1099 input_report_abs(ds->sensors, ds->gyro_calib_data[i].abs_code, calib_data);
1102 /* Parse and calibrate accelerometer data. */
1103 for (i = 0; i < ARRAY_SIZE(ds_report->accel); i++) {
1104 int raw_data = (short)le16_to_cpu(ds_report->accel[i]);
1105 int calib_data = mult_frac(ds->accel_calib_data[i].sens_numer,
1106 raw_data - ds->accel_calib_data[i].bias,
1107 ds->accel_calib_data[i].sens_denom);
1109 input_report_abs(ds->sensors, ds->accel_calib_data[i].abs_code, calib_data);
1112 /* Convert timestamp (in 0.33us unit) to timestamp_us */
1113 sensor_timestamp = le32_to_cpu(ds_report->sensor_timestamp);
1114 if (!ds->sensor_timestamp_initialized) {
1115 ds->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp, 3);
1116 ds->sensor_timestamp_initialized = true;
1120 if (ds->prev_sensor_timestamp > sensor_timestamp)
1121 delta = (U32_MAX - ds->prev_sensor_timestamp + sensor_timestamp + 1);
1123 delta = sensor_timestamp - ds->prev_sensor_timestamp;
1124 ds->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta, 3);
1126 ds->prev_sensor_timestamp = sensor_timestamp;
1127 input_event(ds->sensors, EV_MSC, MSC_TIMESTAMP, ds->sensor_timestamp_us);
1128 input_sync(ds->sensors);
1130 for (i = 0; i < ARRAY_SIZE(ds_report->points); i++) {
1131 struct dualsense_touch_point *point = &ds_report->points[i];
1132 bool active = (point->contact & DS_TOUCH_POINT_INACTIVE) ? false : true;
1134 input_mt_slot(ds->touchpad, i);
1135 input_mt_report_slot_state(ds->touchpad, MT_TOOL_FINGER, active);
1138 int x = (point->x_hi << 8) | point->x_lo;
1139 int y = (point->y_hi << 4) | point->y_lo;
1141 input_report_abs(ds->touchpad, ABS_MT_POSITION_X, x);
1142 input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, y);
1145 input_mt_sync_frame(ds->touchpad);
1146 input_report_key(ds->touchpad, BTN_LEFT, ds_report->buttons[2] & DS_BUTTONS2_TOUCHPAD);
1147 input_sync(ds->touchpad);
1149 battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY;
1150 charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT;
1152 switch (charging_status) {
1155 * Each unit of battery data corresponds to 10%
1156 * 0 = 0-9%, 1 = 10-19%, .. and 10 = 100%
1158 battery_capacity = min(battery_data * 10 + 5, 100);
1159 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
1162 battery_capacity = min(battery_data * 10 + 5, 100);
1163 battery_status = POWER_SUPPLY_STATUS_CHARGING;
1166 battery_capacity = 100;
1167 battery_status = POWER_SUPPLY_STATUS_FULL;
1169 case 0xa: /* voltage or temperature out of range */
1170 case 0xb: /* temperature error */
1171 battery_capacity = 0;
1172 battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1174 case 0xf: /* charging error */
1176 battery_capacity = 0;
1177 battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
1180 spin_lock_irqsave(&ps_dev->lock, flags);
1181 ps_dev->battery_capacity = battery_capacity;
1182 ps_dev->battery_status = battery_status;
1183 spin_unlock_irqrestore(&ps_dev->lock, flags);
1188 static int dualsense_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
1190 struct hid_device *hdev = input_get_drvdata(dev);
1191 struct dualsense *ds = hid_get_drvdata(hdev);
1192 unsigned long flags;
1194 if (effect->type != FF_RUMBLE)
1197 spin_lock_irqsave(&ds->base.lock, flags);
1198 ds->update_rumble = true;
1199 ds->motor_left = effect->u.rumble.strong_magnitude / 256;
1200 ds->motor_right = effect->u.rumble.weak_magnitude / 256;
1201 spin_unlock_irqrestore(&ds->base.lock, flags);
1203 schedule_work(&ds->output_worker);
1207 static int dualsense_reset_leds(struct dualsense *ds)
1209 struct dualsense_output_report report;
1212 buf = kzalloc(sizeof(struct dualsense_output_report_bt), GFP_KERNEL);
1216 dualsense_init_output_report(ds, &report, buf);
1218 * On Bluetooth the DualSense outputs an animation on the lightbar
1219 * during startup and maintains a color afterwards. We need to explicitly
1220 * reconfigure the lightbar before we can do any programming later on.
1221 * In USB the lightbar is not on by default, but redoing the setup there
1224 report.common->valid_flag2 = DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE;
1225 report.common->lightbar_setup = DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT; /* Fade light out. */
1226 dualsense_send_output_report(ds, &report);
1232 static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue)
1234 unsigned long flags;
1236 spin_lock_irqsave(&ds->base.lock, flags);
1237 ds->update_lightbar = true;
1238 ds->lightbar_red = red;
1239 ds->lightbar_green = green;
1240 ds->lightbar_blue = blue;
1241 spin_unlock_irqrestore(&ds->base.lock, flags);
1243 schedule_work(&ds->output_worker);
1246 static void dualsense_set_player_leds(struct dualsense *ds)
1249 * The DualSense controller has a row of 5 LEDs used for player ids.
1250 * Behavior on the PlayStation 5 console is to center the player id
1251 * across the LEDs, so e.g. player 1 would be "--x--" with x being 'on'.
1252 * Follow a similar mapping here.
1254 static const int player_ids[5] = {
1257 BIT(4) | BIT(2) | BIT(0),
1258 BIT(4) | BIT(3) | BIT(1) | BIT(0),
1259 BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0)
1262 uint8_t player_id = ds->base.player_id % ARRAY_SIZE(player_ids);
1264 ds->update_player_leds = true;
1265 ds->player_leds_state = player_ids[player_id];
1266 schedule_work(&ds->output_worker);
1269 static struct ps_device *dualsense_create(struct hid_device *hdev)
1271 struct dualsense *ds;
1272 struct ps_device *ps_dev;
1273 uint8_t max_output_report_size;
1276 static const struct ps_led_info player_leds_info[] = {
1277 { LED_FUNCTION_PLAYER1, "white", dualsense_player_led_get_brightness,
1278 dualsense_player_led_set_brightness },
1279 { LED_FUNCTION_PLAYER2, "white", dualsense_player_led_get_brightness,
1280 dualsense_player_led_set_brightness },
1281 { LED_FUNCTION_PLAYER3, "white", dualsense_player_led_get_brightness,
1282 dualsense_player_led_set_brightness },
1283 { LED_FUNCTION_PLAYER4, "white", dualsense_player_led_get_brightness,
1284 dualsense_player_led_set_brightness },
1285 { LED_FUNCTION_PLAYER5, "white", dualsense_player_led_get_brightness,
1286 dualsense_player_led_set_brightness }
1289 ds = devm_kzalloc(&hdev->dev, sizeof(*ds), GFP_KERNEL);
1291 return ERR_PTR(-ENOMEM);
1294 * Patch version to allow userspace to distinguish between
1295 * hid-generic vs hid-playstation axis and button mapping.
1297 hdev->version |= HID_PLAYSTATION_VERSION_PATCH;
1300 ps_dev->hdev = hdev;
1301 spin_lock_init(&ps_dev->lock);
1302 ps_dev->battery_capacity = 100; /* initial value until parse_report. */
1303 ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
1304 ps_dev->parse_report = dualsense_parse_report;
1305 INIT_WORK(&ds->output_worker, dualsense_output_worker);
1306 hid_set_drvdata(hdev, ds);
1308 max_output_report_size = sizeof(struct dualsense_output_report_bt);
1309 ds->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL);
1310 if (!ds->output_report_dmabuf)
1311 return ERR_PTR(-ENOMEM);
1313 ret = dualsense_get_mac_address(ds);
1315 hid_err(hdev, "Failed to get MAC address from DualSense\n");
1316 return ERR_PTR(ret);
1318 snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address);
1320 ret = dualsense_get_firmware_info(ds);
1322 hid_err(hdev, "Failed to get firmware info from DualSense\n");
1323 return ERR_PTR(ret);
1326 ret = ps_devices_list_add(ps_dev);
1328 return ERR_PTR(ret);
1330 ret = dualsense_get_calibration_data(ds);
1332 hid_err(hdev, "Failed to get calibration data from DualSense\n");
1336 ds->gamepad = ps_gamepad_create(hdev, dualsense_play_effect);
1337 if (IS_ERR(ds->gamepad)) {
1338 ret = PTR_ERR(ds->gamepad);
1341 /* Use gamepad input device name as primary device name for e.g. LEDs */
1342 ps_dev->input_dev_name = dev_name(&ds->gamepad->dev);
1344 ds->sensors = ps_sensors_create(hdev, DS_ACC_RANGE, DS_ACC_RES_PER_G,
1345 DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S);
1346 if (IS_ERR(ds->sensors)) {
1347 ret = PTR_ERR(ds->sensors);
1351 ds->touchpad = ps_touchpad_create(hdev, DS_TOUCHPAD_WIDTH, DS_TOUCHPAD_HEIGHT, 2);
1352 if (IS_ERR(ds->touchpad)) {
1353 ret = PTR_ERR(ds->touchpad);
1357 ret = ps_device_register_battery(ps_dev);
1362 * The hardware may have control over the LEDs (e.g. in Bluetooth on startup).
1363 * Reset the LEDs (lightbar, mute, player leds), so we can control them
1366 ret = dualsense_reset_leds(ds);
1370 ret = ps_lightbar_register(ps_dev, &ds->lightbar, dualsense_lightbar_set_brightness);
1374 /* Set default lightbar color. */
1375 dualsense_set_lightbar(ds, 0, 0, 128); /* blue */
1377 for (i = 0; i < ARRAY_SIZE(player_leds_info); i++) {
1378 const struct ps_led_info *led_info = &player_leds_info[i];
1380 ret = ps_led_register(ps_dev, &ds->player_leds[i], led_info);
1385 ret = ps_device_set_player_id(ps_dev);
1387 hid_err(hdev, "Failed to assign player id for DualSense: %d\n", ret);
1391 /* Set player LEDs to our player id. */
1392 dualsense_set_player_leds(ds);
1395 * Reporting hardware and firmware is important as there are frequent updates, which
1396 * can change behavior.
1398 hid_info(hdev, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n",
1399 ds->base.hw_version, ds->base.fw_version);
1404 ps_devices_list_remove(ps_dev);
1405 return ERR_PTR(ret);
1408 static int ps_raw_event(struct hid_device *hdev, struct hid_report *report,
1411 struct ps_device *dev = hid_get_drvdata(hdev);
1413 if (dev && dev->parse_report)
1414 return dev->parse_report(dev, report, data, size);
1419 static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id)
1421 struct ps_device *dev;
1424 ret = hid_parse(hdev);
1426 hid_err(hdev, "Parse failed\n");
1430 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1432 hid_err(hdev, "Failed to start HID device\n");
1436 ret = hid_hw_open(hdev);
1438 hid_err(hdev, "Failed to open HID device\n");
1442 if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER) {
1443 dev = dualsense_create(hdev);
1445 hid_err(hdev, "Failed to create dualsense.\n");
1451 ret = devm_device_add_group(&hdev->dev, &ps_device_attribute_group);
1453 hid_err(hdev, "Failed to register sysfs nodes.\n");
1466 static void ps_remove(struct hid_device *hdev)
1468 struct ps_device *dev = hid_get_drvdata(hdev);
1470 ps_devices_list_remove(dev);
1471 ps_device_release_player_id(dev);
1477 static const struct hid_device_id ps_devices[] = {
1478 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
1479 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
1482 MODULE_DEVICE_TABLE(hid, ps_devices);
1484 static struct hid_driver ps_driver = {
1485 .name = "playstation",
1486 .id_table = ps_devices,
1488 .remove = ps_remove,
1489 .raw_event = ps_raw_event,
1492 static int __init ps_init(void)
1494 return hid_register_driver(&ps_driver);
1497 static void __exit ps_exit(void)
1499 hid_unregister_driver(&ps_driver);
1500 ida_destroy(&ps_player_id_allocator);
1503 module_init(ps_init);
1504 module_exit(ps_exit);
1506 MODULE_AUTHOR("Sony Interactive Entertainment");
1507 MODULE_DESCRIPTION("HID Driver for PlayStation peripherals.");
1508 MODULE_LICENSE("GPL");