1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * HID driver for Sony DualSense(TM) controller. 4 * 5 * Copyright (c) 2020-2022 Sony Interactive Entertainment 6 */ 7 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> 17 18 #include <asm/unaligned.h> 19 20 #include "hid-ids.h" 21 22 /* List of connected playstation devices. */ 23 static DEFINE_MUTEX(ps_devices_lock); 24 static LIST_HEAD(ps_devices_list); 25 26 static DEFINE_IDA(ps_player_id_allocator); 27 28 #define HID_PLAYSTATION_VERSION_PATCH 0x8000 29 30 /* Base class for playstation devices. */ 31 struct ps_device { 32 struct list_head list; 33 struct hid_device *hdev; 34 spinlock_t lock; 35 36 uint32_t player_id; 37 38 struct power_supply_desc battery_desc; 39 struct power_supply *battery; 40 uint8_t battery_capacity; 41 int battery_status; 42 43 const char *input_dev_name; /* Name of primary input device. */ 44 uint8_t mac_address[6]; /* Note: stored in little endian order. */ 45 uint32_t hw_version; 46 uint32_t fw_version; 47 48 int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size); 49 void (*remove)(struct ps_device *dev); 50 }; 51 52 /* Calibration data for playstation motion sensors. */ 53 struct ps_calibration_data { 54 int abs_code; 55 short bias; 56 int sens_numer; 57 int sens_denom; 58 }; 59 60 struct ps_led_info { 61 const char *name; 62 const char *color; 63 int max_brightness; 64 enum led_brightness (*brightness_get)(struct led_classdev *cdev); 65 int (*brightness_set)(struct led_classdev *cdev, enum led_brightness); 66 int (*blink_set)(struct led_classdev *led, unsigned long *on, unsigned long *off); 67 }; 68 69 /* Seed values for DualShock4 / DualSense CRC32 for different report types. */ 70 #define PS_INPUT_CRC32_SEED 0xA1 71 #define PS_OUTPUT_CRC32_SEED 0xA2 72 #define PS_FEATURE_CRC32_SEED 0xA3 73 74 #define DS_INPUT_REPORT_USB 0x01 75 #define DS_INPUT_REPORT_USB_SIZE 64 76 #define DS_INPUT_REPORT_BT 0x31 77 #define DS_INPUT_REPORT_BT_SIZE 78 78 #define DS_OUTPUT_REPORT_USB 0x02 79 #define DS_OUTPUT_REPORT_USB_SIZE 63 80 #define DS_OUTPUT_REPORT_BT 0x31 81 #define DS_OUTPUT_REPORT_BT_SIZE 78 82 83 #define DS_FEATURE_REPORT_CALIBRATION 0x05 84 #define DS_FEATURE_REPORT_CALIBRATION_SIZE 41 85 #define DS_FEATURE_REPORT_PAIRING_INFO 0x09 86 #define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20 87 #define DS_FEATURE_REPORT_FIRMWARE_INFO 0x20 88 #define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE 64 89 90 /* Button masks for DualSense input report. */ 91 #define DS_BUTTONS0_HAT_SWITCH GENMASK(3, 0) 92 #define DS_BUTTONS0_SQUARE BIT(4) 93 #define DS_BUTTONS0_CROSS BIT(5) 94 #define DS_BUTTONS0_CIRCLE BIT(6) 95 #define DS_BUTTONS0_TRIANGLE BIT(7) 96 #define DS_BUTTONS1_L1 BIT(0) 97 #define DS_BUTTONS1_R1 BIT(1) 98 #define DS_BUTTONS1_L2 BIT(2) 99 #define DS_BUTTONS1_R2 BIT(3) 100 #define DS_BUTTONS1_CREATE BIT(4) 101 #define DS_BUTTONS1_OPTIONS BIT(5) 102 #define DS_BUTTONS1_L3 BIT(6) 103 #define DS_BUTTONS1_R3 BIT(7) 104 #define DS_BUTTONS2_PS_HOME BIT(0) 105 #define DS_BUTTONS2_TOUCHPAD BIT(1) 106 #define DS_BUTTONS2_MIC_MUTE BIT(2) 107 108 /* Status field of DualSense input report. */ 109 #define DS_STATUS_BATTERY_CAPACITY GENMASK(3, 0) 110 #define DS_STATUS_CHARGING GENMASK(7, 4) 111 #define DS_STATUS_CHARGING_SHIFT 4 112 113 /* Feature version from DualSense Firmware Info report. */ 114 #define DS_FEATURE_VERSION(major, minor) ((major & 0xff) << 8 | (minor & 0xff)) 115 116 /* 117 * Status of a DualSense touch point contact. 118 * Contact IDs, with highest bit set are 'inactive' 119 * and any associated data is then invalid. 120 */ 121 #define DS_TOUCH_POINT_INACTIVE BIT(7) 122 123 /* Magic value required in tag field of Bluetooth output report. */ 124 #define DS_OUTPUT_TAG 0x10 125 /* Flags for DualSense output report. */ 126 #define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0) 127 #define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1) 128 #define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0) 129 #define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1) 130 #define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2) 131 #define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3) 132 #define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4) 133 #define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1) 134 #define DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2 BIT(2) 135 #define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4) 136 #define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1) 137 138 /* DualSense hardware limits */ 139 #define DS_ACC_RES_PER_G 8192 140 #define DS_ACC_RANGE (4*DS_ACC_RES_PER_G) 141 #define DS_GYRO_RES_PER_DEG_S 1024 142 #define DS_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S) 143 #define DS_TOUCHPAD_WIDTH 1920 144 #define DS_TOUCHPAD_HEIGHT 1080 145 146 struct dualsense { 147 struct ps_device base; 148 struct input_dev *gamepad; 149 struct input_dev *sensors; 150 struct input_dev *touchpad; 151 152 /* Update version is used as a feature/capability version. */ 153 uint16_t update_version; 154 155 /* Calibration data for accelerometer and gyroscope. */ 156 struct ps_calibration_data accel_calib_data[3]; 157 struct ps_calibration_data gyro_calib_data[3]; 158 159 /* Timestamp for sensor data */ 160 bool sensor_timestamp_initialized; 161 uint32_t prev_sensor_timestamp; 162 uint32_t sensor_timestamp_us; 163 164 /* Compatible rumble state */ 165 bool use_vibration_v2; 166 bool update_rumble; 167 uint8_t motor_left; 168 uint8_t motor_right; 169 170 /* RGB lightbar */ 171 struct led_classdev_mc lightbar; 172 bool update_lightbar; 173 uint8_t lightbar_red; 174 uint8_t lightbar_green; 175 uint8_t lightbar_blue; 176 177 /* Microphone */ 178 bool update_mic_mute; 179 bool mic_muted; 180 bool last_btn_mic_state; 181 182 /* Player leds */ 183 bool update_player_leds; 184 uint8_t player_leds_state; 185 struct led_classdev player_leds[5]; 186 187 struct work_struct output_worker; 188 bool output_worker_initialized; 189 void *output_report_dmabuf; 190 uint8_t output_seq; /* Sequence number for output report. */ 191 }; 192 193 struct dualsense_touch_point { 194 uint8_t contact; 195 uint8_t x_lo; 196 uint8_t x_hi:4, y_lo:4; 197 uint8_t y_hi; 198 } __packed; 199 static_assert(sizeof(struct dualsense_touch_point) == 4); 200 201 /* Main DualSense input report excluding any BT/USB specific headers. */ 202 struct dualsense_input_report { 203 uint8_t x, y; 204 uint8_t rx, ry; 205 uint8_t z, rz; 206 uint8_t seq_number; 207 uint8_t buttons[4]; 208 uint8_t reserved[4]; 209 210 /* Motion sensors */ 211 __le16 gyro[3]; /* x, y, z */ 212 __le16 accel[3]; /* x, y, z */ 213 __le32 sensor_timestamp; 214 uint8_t reserved2; 215 216 /* Touchpad */ 217 struct dualsense_touch_point points[2]; 218 219 uint8_t reserved3[12]; 220 uint8_t status; 221 uint8_t reserved4[10]; 222 } __packed; 223 /* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */ 224 static_assert(sizeof(struct dualsense_input_report) == DS_INPUT_REPORT_USB_SIZE - 1); 225 226 /* Common data between DualSense BT/USB main output report. */ 227 struct dualsense_output_report_common { 228 uint8_t valid_flag0; 229 uint8_t valid_flag1; 230 231 /* For DualShock 4 compatibility mode. */ 232 uint8_t motor_right; 233 uint8_t motor_left; 234 235 /* Audio controls */ 236 uint8_t reserved[4]; 237 uint8_t mute_button_led; 238 239 uint8_t power_save_control; 240 uint8_t reserved2[28]; 241 242 /* LEDs and lightbar */ 243 uint8_t valid_flag2; 244 uint8_t reserved3[2]; 245 uint8_t lightbar_setup; 246 uint8_t led_brightness; 247 uint8_t player_leds; 248 uint8_t lightbar_red; 249 uint8_t lightbar_green; 250 uint8_t lightbar_blue; 251 } __packed; 252 static_assert(sizeof(struct dualsense_output_report_common) == 47); 253 254 struct dualsense_output_report_bt { 255 uint8_t report_id; /* 0x31 */ 256 uint8_t seq_tag; 257 uint8_t tag; 258 struct dualsense_output_report_common common; 259 uint8_t reserved[24]; 260 __le32 crc32; 261 } __packed; 262 static_assert(sizeof(struct dualsense_output_report_bt) == DS_OUTPUT_REPORT_BT_SIZE); 263 264 struct dualsense_output_report_usb { 265 uint8_t report_id; /* 0x02 */ 266 struct dualsense_output_report_common common; 267 uint8_t reserved[15]; 268 } __packed; 269 static_assert(sizeof(struct dualsense_output_report_usb) == DS_OUTPUT_REPORT_USB_SIZE); 270 271 /* 272 * The DualSense has a main output report used to control most features. It is 273 * largely the same between Bluetooth and USB except for different headers and CRC. 274 * This structure hide the differences between the two to simplify sending output reports. 275 */ 276 struct dualsense_output_report { 277 uint8_t *data; /* Start of data */ 278 uint8_t len; /* Size of output report */ 279 280 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */ 281 struct dualsense_output_report_bt *bt; 282 /* Points to USB data payload in case for a USB report else NULL. */ 283 struct dualsense_output_report_usb *usb; 284 /* Points to common section of report, so past any headers. */ 285 struct dualsense_output_report_common *common; 286 }; 287 288 #define DS4_INPUT_REPORT_USB 0x01 289 #define DS4_INPUT_REPORT_USB_SIZE 64 290 #define DS4_INPUT_REPORT_BT 0x11 291 #define DS4_INPUT_REPORT_BT_SIZE 78 292 #define DS4_OUTPUT_REPORT_USB 0x05 293 #define DS4_OUTPUT_REPORT_USB_SIZE 32 294 #define DS4_OUTPUT_REPORT_BT 0x11 295 #define DS4_OUTPUT_REPORT_BT_SIZE 78 296 297 #define DS4_FEATURE_REPORT_CALIBRATION 0x02 298 #define DS4_FEATURE_REPORT_CALIBRATION_SIZE 37 299 #define DS4_FEATURE_REPORT_CALIBRATION_BT 0x05 300 #define DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE 41 301 #define DS4_FEATURE_REPORT_FIRMWARE_INFO 0xa3 302 #define DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE 49 303 #define DS4_FEATURE_REPORT_PAIRING_INFO 0x12 304 #define DS4_FEATURE_REPORT_PAIRING_INFO_SIZE 16 305 306 /* 307 * Status of a DualShock4 touch point contact. 308 * Contact IDs, with highest bit set are 'inactive' 309 * and any associated data is then invalid. 310 */ 311 #define DS4_TOUCH_POINT_INACTIVE BIT(7) 312 313 /* Status field of DualShock4 input report. */ 314 #define DS4_STATUS0_BATTERY_CAPACITY GENMASK(3, 0) 315 #define DS4_STATUS0_CABLE_STATE BIT(4) 316 /* Battery status within batery_status field. */ 317 #define DS4_BATTERY_STATUS_FULL 11 318 /* Status1 bit2 contains dongle connection state: 319 * 0 = connectd 320 * 1 = disconnected 321 */ 322 #define DS4_STATUS1_DONGLE_STATE BIT(2) 323 324 /* The lower 6 bits of hw_control of the Bluetooth main output report 325 * control the interval at which Dualshock 4 reports data: 326 * 0x00 - 1ms 327 * 0x01 - 1ms 328 * 0x02 - 2ms 329 * 0x3E - 62ms 330 * 0x3F - disabled 331 */ 332 #define DS4_OUTPUT_HWCTL_BT_POLL_MASK 0x3F 333 /* Default to 4ms poll interval, which is same as USB (not adjustable). */ 334 #define DS4_BT_DEFAULT_POLL_INTERVAL_MS 4 335 #define DS4_OUTPUT_HWCTL_CRC32 0x40 336 #define DS4_OUTPUT_HWCTL_HID 0x80 337 338 /* Flags for DualShock4 output report. */ 339 #define DS4_OUTPUT_VALID_FLAG0_MOTOR 0x01 340 #define DS4_OUTPUT_VALID_FLAG0_LED 0x02 341 #define DS4_OUTPUT_VALID_FLAG0_LED_BLINK 0x04 342 343 /* DualShock4 hardware limits */ 344 #define DS4_ACC_RES_PER_G 8192 345 #define DS4_ACC_RANGE (4*DS_ACC_RES_PER_G) 346 #define DS4_GYRO_RES_PER_DEG_S 1024 347 #define DS4_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S) 348 #define DS4_LIGHTBAR_MAX_BLINK 255 /* 255 centiseconds */ 349 #define DS4_TOUCHPAD_WIDTH 1920 350 #define DS4_TOUCHPAD_HEIGHT 942 351 352 enum dualshock4_dongle_state { 353 DONGLE_DISCONNECTED, 354 DONGLE_CALIBRATING, 355 DONGLE_CONNECTED, 356 DONGLE_DISABLED 357 }; 358 359 struct dualshock4 { 360 struct ps_device base; 361 struct input_dev *gamepad; 362 struct input_dev *sensors; 363 struct input_dev *touchpad; 364 365 /* Calibration data for accelerometer and gyroscope. */ 366 struct ps_calibration_data accel_calib_data[3]; 367 struct ps_calibration_data gyro_calib_data[3]; 368 369 /* Only used on dongle to track state transitions. */ 370 enum dualshock4_dongle_state dongle_state; 371 /* Used during calibration. */ 372 struct work_struct dongle_hotplug_worker; 373 374 /* Timestamp for sensor data */ 375 bool sensor_timestamp_initialized; 376 uint32_t prev_sensor_timestamp; 377 uint32_t sensor_timestamp_us; 378 379 /* Bluetooth poll interval */ 380 bool update_bt_poll_interval; 381 uint8_t bt_poll_interval; 382 383 bool update_rumble; 384 uint8_t motor_left; 385 uint8_t motor_right; 386 387 /* Lightbar leds */ 388 bool update_lightbar; 389 bool update_lightbar_blink; 390 bool lightbar_enabled; /* For use by global LED control. */ 391 uint8_t lightbar_red; 392 uint8_t lightbar_green; 393 uint8_t lightbar_blue; 394 uint8_t lightbar_blink_on; /* In increments of 10ms. */ 395 uint8_t lightbar_blink_off; /* In increments of 10ms. */ 396 struct led_classdev lightbar_leds[4]; 397 398 struct work_struct output_worker; 399 bool output_worker_initialized; 400 void *output_report_dmabuf; 401 }; 402 403 struct dualshock4_touch_point { 404 uint8_t contact; 405 uint8_t x_lo; 406 uint8_t x_hi:4, y_lo:4; 407 uint8_t y_hi; 408 } __packed; 409 static_assert(sizeof(struct dualshock4_touch_point) == 4); 410 411 struct dualshock4_touch_report { 412 uint8_t timestamp; 413 struct dualshock4_touch_point points[2]; 414 } __packed; 415 static_assert(sizeof(struct dualshock4_touch_report) == 9); 416 417 /* Main DualShock4 input report excluding any BT/USB specific headers. */ 418 struct dualshock4_input_report_common { 419 uint8_t x, y; 420 uint8_t rx, ry; 421 uint8_t buttons[3]; 422 uint8_t z, rz; 423 424 /* Motion sensors */ 425 __le16 sensor_timestamp; 426 uint8_t sensor_temperature; 427 __le16 gyro[3]; /* x, y, z */ 428 __le16 accel[3]; /* x, y, z */ 429 uint8_t reserved2[5]; 430 431 uint8_t status[2]; 432 uint8_t reserved3; 433 } __packed; 434 static_assert(sizeof(struct dualshock4_input_report_common) == 32); 435 436 struct dualshock4_input_report_usb { 437 uint8_t report_id; /* 0x01 */ 438 struct dualshock4_input_report_common common; 439 uint8_t num_touch_reports; 440 struct dualshock4_touch_report touch_reports[3]; 441 uint8_t reserved[3]; 442 } __packed; 443 static_assert(sizeof(struct dualshock4_input_report_usb) == DS4_INPUT_REPORT_USB_SIZE); 444 445 struct dualshock4_input_report_bt { 446 uint8_t report_id; /* 0x11 */ 447 uint8_t reserved[2]; 448 struct dualshock4_input_report_common common; 449 uint8_t num_touch_reports; 450 struct dualshock4_touch_report touch_reports[4]; /* BT has 4 compared to 3 for USB */ 451 uint8_t reserved2[2]; 452 __le32 crc32; 453 } __packed; 454 static_assert(sizeof(struct dualshock4_input_report_bt) == DS4_INPUT_REPORT_BT_SIZE); 455 456 /* Common data between Bluetooth and USB DualShock4 output reports. */ 457 struct dualshock4_output_report_common { 458 uint8_t valid_flag0; 459 uint8_t valid_flag1; 460 461 uint8_t reserved; 462 463 uint8_t motor_right; 464 uint8_t motor_left; 465 466 uint8_t lightbar_red; 467 uint8_t lightbar_green; 468 uint8_t lightbar_blue; 469 uint8_t lightbar_blink_on; 470 uint8_t lightbar_blink_off; 471 } __packed; 472 473 struct dualshock4_output_report_usb { 474 uint8_t report_id; /* 0x5 */ 475 struct dualshock4_output_report_common common; 476 uint8_t reserved[21]; 477 } __packed; 478 static_assert(sizeof(struct dualshock4_output_report_usb) == DS4_OUTPUT_REPORT_USB_SIZE); 479 480 struct dualshock4_output_report_bt { 481 uint8_t report_id; /* 0x11 */ 482 uint8_t hw_control; 483 uint8_t audio_control; 484 struct dualshock4_output_report_common common; 485 uint8_t reserved[61]; 486 __le32 crc32; 487 } __packed; 488 static_assert(sizeof(struct dualshock4_output_report_bt) == DS4_OUTPUT_REPORT_BT_SIZE); 489 490 /* 491 * The DualShock4 has a main output report used to control most features. It is 492 * largely the same between Bluetooth and USB except for different headers and CRC. 493 * This structure hide the differences between the two to simplify sending output reports. 494 */ 495 struct dualshock4_output_report { 496 uint8_t *data; /* Start of data */ 497 uint8_t len; /* Size of output report */ 498 499 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */ 500 struct dualshock4_output_report_bt *bt; 501 /* Points to USB data payload in case for a USB report else NULL. */ 502 struct dualshock4_output_report_usb *usb; 503 /* Points to common section of report, so past any headers. */ 504 struct dualshock4_output_report_common *common; 505 }; 506 507 /* 508 * Common gamepad buttons across DualShock 3 / 4 and DualSense. 509 * Note: for device with a touchpad, touchpad button is not included 510 * as it will be part of the touchpad device. 511 */ 512 static const int ps_gamepad_buttons[] = { 513 BTN_WEST, /* Square */ 514 BTN_NORTH, /* Triangle */ 515 BTN_EAST, /* Circle */ 516 BTN_SOUTH, /* Cross */ 517 BTN_TL, /* L1 */ 518 BTN_TR, /* R1 */ 519 BTN_TL2, /* L2 */ 520 BTN_TR2, /* R2 */ 521 BTN_SELECT, /* Create (PS5) / Share (PS4) */ 522 BTN_START, /* Option */ 523 BTN_THUMBL, /* L3 */ 524 BTN_THUMBR, /* R3 */ 525 BTN_MODE, /* PS Home */ 526 }; 527 528 static const struct {int x; int y; } ps_gamepad_hat_mapping[] = { 529 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, 530 {0, 0}, 531 }; 532 533 static int dualshock4_get_calibration_data(struct dualshock4 *ds4); 534 static inline void dualsense_schedule_work(struct dualsense *ds); 535 static inline void dualshock4_schedule_work(struct dualshock4 *ds4); 536 static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue); 537 static void dualshock4_set_default_lightbar_colors(struct dualshock4 *ds4); 538 539 /* 540 * Add a new ps_device to ps_devices if it doesn't exist. 541 * Return error on duplicate device, which can happen if the same 542 * device is connected using both Bluetooth and USB. 543 */ 544 static int ps_devices_list_add(struct ps_device *dev) 545 { 546 struct ps_device *entry; 547 548 mutex_lock(&ps_devices_lock); 549 list_for_each_entry(entry, &ps_devices_list, list) { 550 if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) { 551 hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n", 552 dev->mac_address); 553 mutex_unlock(&ps_devices_lock); 554 return -EEXIST; 555 } 556 } 557 558 list_add_tail(&dev->list, &ps_devices_list); 559 mutex_unlock(&ps_devices_lock); 560 return 0; 561 } 562 563 static int ps_devices_list_remove(struct ps_device *dev) 564 { 565 mutex_lock(&ps_devices_lock); 566 list_del(&dev->list); 567 mutex_unlock(&ps_devices_lock); 568 return 0; 569 } 570 571 static int ps_device_set_player_id(struct ps_device *dev) 572 { 573 int ret = ida_alloc(&ps_player_id_allocator, GFP_KERNEL); 574 575 if (ret < 0) 576 return ret; 577 578 dev->player_id = ret; 579 return 0; 580 } 581 582 static void ps_device_release_player_id(struct ps_device *dev) 583 { 584 ida_free(&ps_player_id_allocator, dev->player_id); 585 586 dev->player_id = U32_MAX; 587 } 588 589 static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix) 590 { 591 struct input_dev *input_dev; 592 593 input_dev = devm_input_allocate_device(&hdev->dev); 594 if (!input_dev) 595 return ERR_PTR(-ENOMEM); 596 597 input_dev->id.bustype = hdev->bus; 598 input_dev->id.vendor = hdev->vendor; 599 input_dev->id.product = hdev->product; 600 input_dev->id.version = hdev->version; 601 input_dev->uniq = hdev->uniq; 602 603 if (name_suffix) { 604 input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name, 605 name_suffix); 606 if (!input_dev->name) 607 return ERR_PTR(-ENOMEM); 608 } else { 609 input_dev->name = hdev->name; 610 } 611 612 input_set_drvdata(input_dev, hdev); 613 614 return input_dev; 615 } 616 617 static enum power_supply_property ps_power_supply_props[] = { 618 POWER_SUPPLY_PROP_STATUS, 619 POWER_SUPPLY_PROP_PRESENT, 620 POWER_SUPPLY_PROP_CAPACITY, 621 POWER_SUPPLY_PROP_SCOPE, 622 }; 623 624 static int ps_battery_get_property(struct power_supply *psy, 625 enum power_supply_property psp, 626 union power_supply_propval *val) 627 { 628 struct ps_device *dev = power_supply_get_drvdata(psy); 629 uint8_t battery_capacity; 630 int battery_status; 631 unsigned long flags; 632 int ret = 0; 633 634 spin_lock_irqsave(&dev->lock, flags); 635 battery_capacity = dev->battery_capacity; 636 battery_status = dev->battery_status; 637 spin_unlock_irqrestore(&dev->lock, flags); 638 639 switch (psp) { 640 case POWER_SUPPLY_PROP_STATUS: 641 val->intval = battery_status; 642 break; 643 case POWER_SUPPLY_PROP_PRESENT: 644 val->intval = 1; 645 break; 646 case POWER_SUPPLY_PROP_CAPACITY: 647 val->intval = battery_capacity; 648 break; 649 case POWER_SUPPLY_PROP_SCOPE: 650 val->intval = POWER_SUPPLY_SCOPE_DEVICE; 651 break; 652 default: 653 ret = -EINVAL; 654 break; 655 } 656 657 return ret; 658 } 659 660 static int ps_device_register_battery(struct ps_device *dev) 661 { 662 struct power_supply *battery; 663 struct power_supply_config battery_cfg = { .drv_data = dev }; 664 int ret; 665 666 dev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; 667 dev->battery_desc.properties = ps_power_supply_props; 668 dev->battery_desc.num_properties = ARRAY_SIZE(ps_power_supply_props); 669 dev->battery_desc.get_property = ps_battery_get_property; 670 dev->battery_desc.name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL, 671 "ps-controller-battery-%pMR", dev->mac_address); 672 if (!dev->battery_desc.name) 673 return -ENOMEM; 674 675 battery = devm_power_supply_register(&dev->hdev->dev, &dev->battery_desc, &battery_cfg); 676 if (IS_ERR(battery)) { 677 ret = PTR_ERR(battery); 678 hid_err(dev->hdev, "Unable to register battery device: %d\n", ret); 679 return ret; 680 } 681 dev->battery = battery; 682 683 ret = power_supply_powers(dev->battery, &dev->hdev->dev); 684 if (ret) { 685 hid_err(dev->hdev, "Unable to activate battery device: %d\n", ret); 686 return ret; 687 } 688 689 return 0; 690 } 691 692 /* Compute crc32 of HID data and compare against expected CRC. */ 693 static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc) 694 { 695 uint32_t crc; 696 697 crc = crc32_le(0xFFFFFFFF, &seed, 1); 698 crc = ~crc32_le(crc, data, len); 699 700 return crc == report_crc; 701 } 702 703 static struct input_dev *ps_gamepad_create(struct hid_device *hdev, 704 int (*play_effect)(struct input_dev *, void *, struct ff_effect *)) 705 { 706 struct input_dev *gamepad; 707 unsigned int i; 708 int ret; 709 710 gamepad = ps_allocate_input_dev(hdev, NULL); 711 if (IS_ERR(gamepad)) 712 return ERR_CAST(gamepad); 713 714 input_set_abs_params(gamepad, ABS_X, 0, 255, 0, 0); 715 input_set_abs_params(gamepad, ABS_Y, 0, 255, 0, 0); 716 input_set_abs_params(gamepad, ABS_Z, 0, 255, 0, 0); 717 input_set_abs_params(gamepad, ABS_RX, 0, 255, 0, 0); 718 input_set_abs_params(gamepad, ABS_RY, 0, 255, 0, 0); 719 input_set_abs_params(gamepad, ABS_RZ, 0, 255, 0, 0); 720 721 input_set_abs_params(gamepad, ABS_HAT0X, -1, 1, 0, 0); 722 input_set_abs_params(gamepad, ABS_HAT0Y, -1, 1, 0, 0); 723 724 for (i = 0; i < ARRAY_SIZE(ps_gamepad_buttons); i++) 725 input_set_capability(gamepad, EV_KEY, ps_gamepad_buttons[i]); 726 727 #if IS_ENABLED(CONFIG_PLAYSTATION_FF) 728 if (play_effect) { 729 input_set_capability(gamepad, EV_FF, FF_RUMBLE); 730 input_ff_create_memless(gamepad, NULL, play_effect); 731 } 732 #endif 733 734 ret = input_register_device(gamepad); 735 if (ret) 736 return ERR_PTR(ret); 737 738 return gamepad; 739 } 740 741 static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *buf, size_t size, 742 bool check_crc) 743 { 744 int ret; 745 746 ret = hid_hw_raw_request(hdev, report_id, buf, size, HID_FEATURE_REPORT, 747 HID_REQ_GET_REPORT); 748 if (ret < 0) { 749 hid_err(hdev, "Failed to retrieve feature with reportID %d: %d\n", report_id, ret); 750 return ret; 751 } 752 753 if (ret != size) { 754 hid_err(hdev, "Invalid byte count transferred, expected %zu got %d\n", size, ret); 755 return -EINVAL; 756 } 757 758 if (buf[0] != report_id) { 759 hid_err(hdev, "Invalid reportID received, expected %d got %d\n", report_id, buf[0]); 760 return -EINVAL; 761 } 762 763 if (hdev->bus == BUS_BLUETOOTH && check_crc) { 764 /* Last 4 bytes contains crc32. */ 765 uint8_t crc_offset = size - 4; 766 uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]); 767 768 if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) { 769 hid_err(hdev, "CRC check failed for reportID=%d\n", report_id); 770 return -EILSEQ; 771 } 772 } 773 774 return 0; 775 } 776 777 static int ps_led_register(struct ps_device *ps_dev, struct led_classdev *led, 778 const struct ps_led_info *led_info) 779 { 780 int ret; 781 782 if (led_info->name) { 783 led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, 784 "%s:%s:%s", ps_dev->input_dev_name, led_info->color, led_info->name); 785 } else { 786 /* Backwards compatible mode for hid-sony, but not compliant with LED class spec. */ 787 led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, 788 "%s:%s", ps_dev->input_dev_name, led_info->color); 789 } 790 791 if (!led->name) 792 return -ENOMEM; 793 794 led->brightness = 0; 795 led->max_brightness = led_info->max_brightness; 796 led->flags = LED_CORE_SUSPENDRESUME; 797 led->brightness_get = led_info->brightness_get; 798 led->brightness_set_blocking = led_info->brightness_set; 799 led->blink_set = led_info->blink_set; 800 801 ret = devm_led_classdev_register(&ps_dev->hdev->dev, led); 802 if (ret) { 803 hid_err(ps_dev->hdev, "Failed to register LED %s: %d\n", led_info->name, ret); 804 return ret; 805 } 806 807 return 0; 808 } 809 810 /* Register a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */ 811 static int ps_lightbar_register(struct ps_device *ps_dev, struct led_classdev_mc *lightbar_mc_dev, 812 int (*brightness_set)(struct led_classdev *, enum led_brightness)) 813 { 814 struct hid_device *hdev = ps_dev->hdev; 815 struct mc_subled *mc_led_info; 816 struct led_classdev *led_cdev; 817 int ret; 818 819 mc_led_info = devm_kmalloc_array(&hdev->dev, 3, sizeof(*mc_led_info), 820 GFP_KERNEL | __GFP_ZERO); 821 if (!mc_led_info) 822 return -ENOMEM; 823 824 mc_led_info[0].color_index = LED_COLOR_ID_RED; 825 mc_led_info[1].color_index = LED_COLOR_ID_GREEN; 826 mc_led_info[2].color_index = LED_COLOR_ID_BLUE; 827 828 lightbar_mc_dev->subled_info = mc_led_info; 829 lightbar_mc_dev->num_colors = 3; 830 831 led_cdev = &lightbar_mc_dev->led_cdev; 832 led_cdev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s:rgb:indicator", 833 ps_dev->input_dev_name); 834 if (!led_cdev->name) 835 return -ENOMEM; 836 led_cdev->brightness = 255; 837 led_cdev->max_brightness = 255; 838 led_cdev->brightness_set_blocking = brightness_set; 839 840 ret = devm_led_classdev_multicolor_register(&hdev->dev, lightbar_mc_dev); 841 if (ret < 0) { 842 hid_err(hdev, "Cannot register multicolor LED device\n"); 843 return ret; 844 } 845 846 return 0; 847 } 848 849 static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res, 850 int gyro_range, int gyro_res) 851 { 852 struct input_dev *sensors; 853 int ret; 854 855 sensors = ps_allocate_input_dev(hdev, "Motion Sensors"); 856 if (IS_ERR(sensors)) 857 return ERR_CAST(sensors); 858 859 __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit); 860 __set_bit(EV_MSC, sensors->evbit); 861 __set_bit(MSC_TIMESTAMP, sensors->mscbit); 862 863 /* Accelerometer */ 864 input_set_abs_params(sensors, ABS_X, -accel_range, accel_range, 16, 0); 865 input_set_abs_params(sensors, ABS_Y, -accel_range, accel_range, 16, 0); 866 input_set_abs_params(sensors, ABS_Z, -accel_range, accel_range, 16, 0); 867 input_abs_set_res(sensors, ABS_X, accel_res); 868 input_abs_set_res(sensors, ABS_Y, accel_res); 869 input_abs_set_res(sensors, ABS_Z, accel_res); 870 871 /* Gyroscope */ 872 input_set_abs_params(sensors, ABS_RX, -gyro_range, gyro_range, 16, 0); 873 input_set_abs_params(sensors, ABS_RY, -gyro_range, gyro_range, 16, 0); 874 input_set_abs_params(sensors, ABS_RZ, -gyro_range, gyro_range, 16, 0); 875 input_abs_set_res(sensors, ABS_RX, gyro_res); 876 input_abs_set_res(sensors, ABS_RY, gyro_res); 877 input_abs_set_res(sensors, ABS_RZ, gyro_res); 878 879 ret = input_register_device(sensors); 880 if (ret) 881 return ERR_PTR(ret); 882 883 return sensors; 884 } 885 886 static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, int height, 887 unsigned int num_contacts) 888 { 889 struct input_dev *touchpad; 890 int ret; 891 892 touchpad = ps_allocate_input_dev(hdev, "Touchpad"); 893 if (IS_ERR(touchpad)) 894 return ERR_CAST(touchpad); 895 896 /* Map button underneath touchpad to BTN_LEFT. */ 897 input_set_capability(touchpad, EV_KEY, BTN_LEFT); 898 __set_bit(INPUT_PROP_BUTTONPAD, touchpad->propbit); 899 900 input_set_abs_params(touchpad, ABS_MT_POSITION_X, 0, width - 1, 0, 0); 901 input_set_abs_params(touchpad, ABS_MT_POSITION_Y, 0, height - 1, 0, 0); 902 903 ret = input_mt_init_slots(touchpad, num_contacts, INPUT_MT_POINTER); 904 if (ret) 905 return ERR_PTR(ret); 906 907 ret = input_register_device(touchpad); 908 if (ret) 909 return ERR_PTR(ret); 910 911 return touchpad; 912 } 913 914 static ssize_t firmware_version_show(struct device *dev, 915 struct device_attribute 916 *attr, char *buf) 917 { 918 struct hid_device *hdev = to_hid_device(dev); 919 struct ps_device *ps_dev = hid_get_drvdata(hdev); 920 921 return sysfs_emit(buf, "0x%08x\n", ps_dev->fw_version); 922 } 923 924 static DEVICE_ATTR_RO(firmware_version); 925 926 static ssize_t hardware_version_show(struct device *dev, 927 struct device_attribute 928 *attr, char *buf) 929 { 930 struct hid_device *hdev = to_hid_device(dev); 931 struct ps_device *ps_dev = hid_get_drvdata(hdev); 932 933 return sysfs_emit(buf, "0x%08x\n", ps_dev->hw_version); 934 } 935 936 static DEVICE_ATTR_RO(hardware_version); 937 938 static struct attribute *ps_device_attrs[] = { 939 &dev_attr_firmware_version.attr, 940 &dev_attr_hardware_version.attr, 941 NULL 942 }; 943 ATTRIBUTE_GROUPS(ps_device); 944 945 static int dualsense_get_calibration_data(struct dualsense *ds) 946 { 947 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus; 948 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus; 949 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus; 950 short gyro_speed_plus, gyro_speed_minus; 951 short acc_x_plus, acc_x_minus; 952 short acc_y_plus, acc_y_minus; 953 short acc_z_plus, acc_z_minus; 954 int speed_2x; 955 int range_2g; 956 int ret = 0; 957 uint8_t *buf; 958 959 buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL); 960 if (!buf) 961 return -ENOMEM; 962 963 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_CALIBRATION, buf, 964 DS_FEATURE_REPORT_CALIBRATION_SIZE, true); 965 if (ret) { 966 hid_err(ds->base.hdev, "Failed to retrieve DualSense calibration info: %d\n", ret); 967 goto err_free; 968 } 969 970 gyro_pitch_bias = get_unaligned_le16(&buf[1]); 971 gyro_yaw_bias = get_unaligned_le16(&buf[3]); 972 gyro_roll_bias = get_unaligned_le16(&buf[5]); 973 gyro_pitch_plus = get_unaligned_le16(&buf[7]); 974 gyro_pitch_minus = get_unaligned_le16(&buf[9]); 975 gyro_yaw_plus = get_unaligned_le16(&buf[11]); 976 gyro_yaw_minus = get_unaligned_le16(&buf[13]); 977 gyro_roll_plus = get_unaligned_le16(&buf[15]); 978 gyro_roll_minus = get_unaligned_le16(&buf[17]); 979 gyro_speed_plus = get_unaligned_le16(&buf[19]); 980 gyro_speed_minus = get_unaligned_le16(&buf[21]); 981 acc_x_plus = get_unaligned_le16(&buf[23]); 982 acc_x_minus = get_unaligned_le16(&buf[25]); 983 acc_y_plus = get_unaligned_le16(&buf[27]); 984 acc_y_minus = get_unaligned_le16(&buf[29]); 985 acc_z_plus = get_unaligned_le16(&buf[31]); 986 acc_z_minus = get_unaligned_le16(&buf[33]); 987 988 /* 989 * Set gyroscope calibration and normalization parameters. 990 * Data values will be normalized to 1/DS_GYRO_RES_PER_DEG_S degree/s. 991 */ 992 speed_2x = (gyro_speed_plus + gyro_speed_minus); 993 ds->gyro_calib_data[0].abs_code = ABS_RX; 994 ds->gyro_calib_data[0].bias = 0; 995 ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; 996 ds->gyro_calib_data[0].sens_denom = abs(gyro_pitch_plus - gyro_pitch_bias) + 997 abs(gyro_pitch_minus - gyro_pitch_bias); 998 999 ds->gyro_calib_data[1].abs_code = ABS_RY; 1000 ds->gyro_calib_data[1].bias = 0; 1001 ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; 1002 ds->gyro_calib_data[1].sens_denom = abs(gyro_yaw_plus - gyro_yaw_bias) + 1003 abs(gyro_yaw_minus - gyro_yaw_bias); 1004 1005 ds->gyro_calib_data[2].abs_code = ABS_RZ; 1006 ds->gyro_calib_data[2].bias = 0; 1007 ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; 1008 ds->gyro_calib_data[2].sens_denom = abs(gyro_roll_plus - gyro_roll_bias) + 1009 abs(gyro_roll_minus - gyro_roll_bias); 1010 1011 /* 1012 * Set accelerometer calibration and normalization parameters. 1013 * Data values will be normalized to 1/DS_ACC_RES_PER_G g. 1014 */ 1015 range_2g = acc_x_plus - acc_x_minus; 1016 ds->accel_calib_data[0].abs_code = ABS_X; 1017 ds->accel_calib_data[0].bias = acc_x_plus - range_2g / 2; 1018 ds->accel_calib_data[0].sens_numer = 2*DS_ACC_RES_PER_G; 1019 ds->accel_calib_data[0].sens_denom = range_2g; 1020 1021 range_2g = acc_y_plus - acc_y_minus; 1022 ds->accel_calib_data[1].abs_code = ABS_Y; 1023 ds->accel_calib_data[1].bias = acc_y_plus - range_2g / 2; 1024 ds->accel_calib_data[1].sens_numer = 2*DS_ACC_RES_PER_G; 1025 ds->accel_calib_data[1].sens_denom = range_2g; 1026 1027 range_2g = acc_z_plus - acc_z_minus; 1028 ds->accel_calib_data[2].abs_code = ABS_Z; 1029 ds->accel_calib_data[2].bias = acc_z_plus - range_2g / 2; 1030 ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G; 1031 ds->accel_calib_data[2].sens_denom = range_2g; 1032 1033 err_free: 1034 kfree(buf); 1035 return ret; 1036 } 1037 1038 1039 static int dualsense_get_firmware_info(struct dualsense *ds) 1040 { 1041 uint8_t *buf; 1042 int ret; 1043 1044 buf = kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL); 1045 if (!buf) 1046 return -ENOMEM; 1047 1048 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_FIRMWARE_INFO, buf, 1049 DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, true); 1050 if (ret) { 1051 hid_err(ds->base.hdev, "Failed to retrieve DualSense firmware info: %d\n", ret); 1052 goto err_free; 1053 } 1054 1055 ds->base.hw_version = get_unaligned_le32(&buf[24]); 1056 ds->base.fw_version = get_unaligned_le32(&buf[28]); 1057 1058 /* Update version is some kind of feature version. It is distinct from 1059 * the firmware version as there can be many different variations of a 1060 * controller over time with the same physical shell, but with different 1061 * PCBs and other internal changes. The update version (internal name) is 1062 * used as a means to detect what features are available and change behavior. 1063 * Note: the version is different between DualSense and DualSense Edge. 1064 */ 1065 ds->update_version = get_unaligned_le16(&buf[44]); 1066 1067 err_free: 1068 kfree(buf); 1069 return ret; 1070 } 1071 1072 static int dualsense_get_mac_address(struct dualsense *ds) 1073 { 1074 uint8_t *buf; 1075 int ret = 0; 1076 1077 buf = kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL); 1078 if (!buf) 1079 return -ENOMEM; 1080 1081 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_PAIRING_INFO, buf, 1082 DS_FEATURE_REPORT_PAIRING_INFO_SIZE, true); 1083 if (ret) { 1084 hid_err(ds->base.hdev, "Failed to retrieve DualSense pairing info: %d\n", ret); 1085 goto err_free; 1086 } 1087 1088 memcpy(ds->base.mac_address, &buf[1], sizeof(ds->base.mac_address)); 1089 1090 err_free: 1091 kfree(buf); 1092 return ret; 1093 } 1094 1095 static int dualsense_lightbar_set_brightness(struct led_classdev *cdev, 1096 enum led_brightness brightness) 1097 { 1098 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev); 1099 struct dualsense *ds = container_of(mc_cdev, struct dualsense, lightbar); 1100 uint8_t red, green, blue; 1101 1102 led_mc_calc_color_components(mc_cdev, brightness); 1103 red = mc_cdev->subled_info[0].brightness; 1104 green = mc_cdev->subled_info[1].brightness; 1105 blue = mc_cdev->subled_info[2].brightness; 1106 1107 dualsense_set_lightbar(ds, red, green, blue); 1108 return 0; 1109 } 1110 1111 static enum led_brightness dualsense_player_led_get_brightness(struct led_classdev *led) 1112 { 1113 struct hid_device *hdev = to_hid_device(led->dev->parent); 1114 struct dualsense *ds = hid_get_drvdata(hdev); 1115 1116 return !!(ds->player_leds_state & BIT(led - ds->player_leds)); 1117 } 1118 1119 static int dualsense_player_led_set_brightness(struct led_classdev *led, enum led_brightness value) 1120 { 1121 struct hid_device *hdev = to_hid_device(led->dev->parent); 1122 struct dualsense *ds = hid_get_drvdata(hdev); 1123 unsigned long flags; 1124 unsigned int led_index; 1125 1126 spin_lock_irqsave(&ds->base.lock, flags); 1127 1128 led_index = led - ds->player_leds; 1129 if (value == LED_OFF) 1130 ds->player_leds_state &= ~BIT(led_index); 1131 else 1132 ds->player_leds_state |= BIT(led_index); 1133 1134 ds->update_player_leds = true; 1135 spin_unlock_irqrestore(&ds->base.lock, flags); 1136 1137 dualsense_schedule_work(ds); 1138 1139 return 0; 1140 } 1141 1142 static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp, 1143 void *buf) 1144 { 1145 struct hid_device *hdev = ds->base.hdev; 1146 1147 if (hdev->bus == BUS_BLUETOOTH) { 1148 struct dualsense_output_report_bt *bt = buf; 1149 1150 memset(bt, 0, sizeof(*bt)); 1151 bt->report_id = DS_OUTPUT_REPORT_BT; 1152 bt->tag = DS_OUTPUT_TAG; /* Tag must be set. Exact meaning is unclear. */ 1153 1154 /* 1155 * Highest 4-bit is a sequence number, which needs to be increased 1156 * every report. Lowest 4-bit is tag and can be zero for now. 1157 */ 1158 bt->seq_tag = (ds->output_seq << 4) | 0x0; 1159 if (++ds->output_seq == 16) 1160 ds->output_seq = 0; 1161 1162 rp->data = buf; 1163 rp->len = sizeof(*bt); 1164 rp->bt = bt; 1165 rp->usb = NULL; 1166 rp->common = &bt->common; 1167 } else { /* USB */ 1168 struct dualsense_output_report_usb *usb = buf; 1169 1170 memset(usb, 0, sizeof(*usb)); 1171 usb->report_id = DS_OUTPUT_REPORT_USB; 1172 1173 rp->data = buf; 1174 rp->len = sizeof(*usb); 1175 rp->bt = NULL; 1176 rp->usb = usb; 1177 rp->common = &usb->common; 1178 } 1179 } 1180 1181 static inline void dualsense_schedule_work(struct dualsense *ds) 1182 { 1183 unsigned long flags; 1184 1185 spin_lock_irqsave(&ds->base.lock, flags); 1186 if (ds->output_worker_initialized) 1187 schedule_work(&ds->output_worker); 1188 spin_unlock_irqrestore(&ds->base.lock, flags); 1189 } 1190 1191 /* 1192 * Helper function to send DualSense output reports. Applies a CRC at the end of a report 1193 * for Bluetooth reports. 1194 */ 1195 static void dualsense_send_output_report(struct dualsense *ds, 1196 struct dualsense_output_report *report) 1197 { 1198 struct hid_device *hdev = ds->base.hdev; 1199 1200 /* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */ 1201 if (report->bt) { 1202 uint32_t crc; 1203 uint8_t seed = PS_OUTPUT_CRC32_SEED; 1204 1205 crc = crc32_le(0xFFFFFFFF, &seed, 1); 1206 crc = ~crc32_le(crc, report->data, report->len - 4); 1207 1208 report->bt->crc32 = cpu_to_le32(crc); 1209 } 1210 1211 hid_hw_output_report(hdev, report->data, report->len); 1212 } 1213 1214 static void dualsense_output_worker(struct work_struct *work) 1215 { 1216 struct dualsense *ds = container_of(work, struct dualsense, output_worker); 1217 struct dualsense_output_report report; 1218 struct dualsense_output_report_common *common; 1219 unsigned long flags; 1220 1221 dualsense_init_output_report(ds, &report, ds->output_report_dmabuf); 1222 common = report.common; 1223 1224 spin_lock_irqsave(&ds->base.lock, flags); 1225 1226 if (ds->update_rumble) { 1227 /* Select classic rumble style haptics and enable it. */ 1228 common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT; 1229 if (ds->use_vibration_v2) 1230 common->valid_flag2 |= DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2; 1231 else 1232 common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION; 1233 common->motor_left = ds->motor_left; 1234 common->motor_right = ds->motor_right; 1235 ds->update_rumble = false; 1236 } 1237 1238 if (ds->update_lightbar) { 1239 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE; 1240 common->lightbar_red = ds->lightbar_red; 1241 common->lightbar_green = ds->lightbar_green; 1242 common->lightbar_blue = ds->lightbar_blue; 1243 1244 ds->update_lightbar = false; 1245 } 1246 1247 if (ds->update_player_leds) { 1248 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE; 1249 common->player_leds = ds->player_leds_state; 1250 1251 ds->update_player_leds = false; 1252 } 1253 1254 if (ds->update_mic_mute) { 1255 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE; 1256 common->mute_button_led = ds->mic_muted; 1257 1258 if (ds->mic_muted) { 1259 /* Disable microphone */ 1260 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE; 1261 common->power_save_control |= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE; 1262 } else { 1263 /* Enable microphone */ 1264 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE; 1265 common->power_save_control &= ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE; 1266 } 1267 1268 ds->update_mic_mute = false; 1269 } 1270 1271 spin_unlock_irqrestore(&ds->base.lock, flags); 1272 1273 dualsense_send_output_report(ds, &report); 1274 } 1275 1276 static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *report, 1277 u8 *data, int size) 1278 { 1279 struct hid_device *hdev = ps_dev->hdev; 1280 struct dualsense *ds = container_of(ps_dev, struct dualsense, base); 1281 struct dualsense_input_report *ds_report; 1282 uint8_t battery_data, battery_capacity, charging_status, value; 1283 int battery_status; 1284 uint32_t sensor_timestamp; 1285 bool btn_mic_state; 1286 unsigned long flags; 1287 int i; 1288 1289 /* 1290 * DualSense in USB uses the full HID report for reportID 1, but 1291 * Bluetooth uses a minimal HID report for reportID 1 and reports 1292 * the full report using reportID 49. 1293 */ 1294 if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB && 1295 size == DS_INPUT_REPORT_USB_SIZE) { 1296 ds_report = (struct dualsense_input_report *)&data[1]; 1297 } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT && 1298 size == DS_INPUT_REPORT_BT_SIZE) { 1299 /* Last 4 bytes of input report contain crc32 */ 1300 uint32_t report_crc = get_unaligned_le32(&data[size - 4]); 1301 1302 if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) { 1303 hid_err(hdev, "DualSense input CRC's check failed\n"); 1304 return -EILSEQ; 1305 } 1306 1307 ds_report = (struct dualsense_input_report *)&data[2]; 1308 } else { 1309 hid_err(hdev, "Unhandled reportID=%d\n", report->id); 1310 return -1; 1311 } 1312 1313 input_report_abs(ds->gamepad, ABS_X, ds_report->x); 1314 input_report_abs(ds->gamepad, ABS_Y, ds_report->y); 1315 input_report_abs(ds->gamepad, ABS_RX, ds_report->rx); 1316 input_report_abs(ds->gamepad, ABS_RY, ds_report->ry); 1317 input_report_abs(ds->gamepad, ABS_Z, ds_report->z); 1318 input_report_abs(ds->gamepad, ABS_RZ, ds_report->rz); 1319 1320 value = ds_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH; 1321 if (value >= ARRAY_SIZE(ps_gamepad_hat_mapping)) 1322 value = 8; /* center */ 1323 input_report_abs(ds->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x); 1324 input_report_abs(ds->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y); 1325 1326 input_report_key(ds->gamepad, BTN_WEST, ds_report->buttons[0] & DS_BUTTONS0_SQUARE); 1327 input_report_key(ds->gamepad, BTN_SOUTH, ds_report->buttons[0] & DS_BUTTONS0_CROSS); 1328 input_report_key(ds->gamepad, BTN_EAST, ds_report->buttons[0] & DS_BUTTONS0_CIRCLE); 1329 input_report_key(ds->gamepad, BTN_NORTH, ds_report->buttons[0] & DS_BUTTONS0_TRIANGLE); 1330 input_report_key(ds->gamepad, BTN_TL, ds_report->buttons[1] & DS_BUTTONS1_L1); 1331 input_report_key(ds->gamepad, BTN_TR, ds_report->buttons[1] & DS_BUTTONS1_R1); 1332 input_report_key(ds->gamepad, BTN_TL2, ds_report->buttons[1] & DS_BUTTONS1_L2); 1333 input_report_key(ds->gamepad, BTN_TR2, ds_report->buttons[1] & DS_BUTTONS1_R2); 1334 input_report_key(ds->gamepad, BTN_SELECT, ds_report->buttons[1] & DS_BUTTONS1_CREATE); 1335 input_report_key(ds->gamepad, BTN_START, ds_report->buttons[1] & DS_BUTTONS1_OPTIONS); 1336 input_report_key(ds->gamepad, BTN_THUMBL, ds_report->buttons[1] & DS_BUTTONS1_L3); 1337 input_report_key(ds->gamepad, BTN_THUMBR, ds_report->buttons[1] & DS_BUTTONS1_R3); 1338 input_report_key(ds->gamepad, BTN_MODE, ds_report->buttons[2] & DS_BUTTONS2_PS_HOME); 1339 input_sync(ds->gamepad); 1340 1341 /* 1342 * The DualSense has an internal microphone, which can be muted through a mute button 1343 * on the device. The driver is expected to read the button state and program the device 1344 * to mute/unmute audio at the hardware level. 1345 */ 1346 btn_mic_state = !!(ds_report->buttons[2] & DS_BUTTONS2_MIC_MUTE); 1347 if (btn_mic_state && !ds->last_btn_mic_state) { 1348 spin_lock_irqsave(&ps_dev->lock, flags); 1349 ds->update_mic_mute = true; 1350 ds->mic_muted = !ds->mic_muted; /* toggle */ 1351 spin_unlock_irqrestore(&ps_dev->lock, flags); 1352 1353 /* Schedule updating of microphone state at hardware level. */ 1354 dualsense_schedule_work(ds); 1355 } 1356 ds->last_btn_mic_state = btn_mic_state; 1357 1358 /* Parse and calibrate gyroscope data. */ 1359 for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) { 1360 int raw_data = (short)le16_to_cpu(ds_report->gyro[i]); 1361 int calib_data = mult_frac(ds->gyro_calib_data[i].sens_numer, 1362 raw_data, ds->gyro_calib_data[i].sens_denom); 1363 1364 input_report_abs(ds->sensors, ds->gyro_calib_data[i].abs_code, calib_data); 1365 } 1366 1367 /* Parse and calibrate accelerometer data. */ 1368 for (i = 0; i < ARRAY_SIZE(ds_report->accel); i++) { 1369 int raw_data = (short)le16_to_cpu(ds_report->accel[i]); 1370 int calib_data = mult_frac(ds->accel_calib_data[i].sens_numer, 1371 raw_data - ds->accel_calib_data[i].bias, 1372 ds->accel_calib_data[i].sens_denom); 1373 1374 input_report_abs(ds->sensors, ds->accel_calib_data[i].abs_code, calib_data); 1375 } 1376 1377 /* Convert timestamp (in 0.33us unit) to timestamp_us */ 1378 sensor_timestamp = le32_to_cpu(ds_report->sensor_timestamp); 1379 if (!ds->sensor_timestamp_initialized) { 1380 ds->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp, 3); 1381 ds->sensor_timestamp_initialized = true; 1382 } else { 1383 uint32_t delta; 1384 1385 if (ds->prev_sensor_timestamp > sensor_timestamp) 1386 delta = (U32_MAX - ds->prev_sensor_timestamp + sensor_timestamp + 1); 1387 else 1388 delta = sensor_timestamp - ds->prev_sensor_timestamp; 1389 ds->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta, 3); 1390 } 1391 ds->prev_sensor_timestamp = sensor_timestamp; 1392 input_event(ds->sensors, EV_MSC, MSC_TIMESTAMP, ds->sensor_timestamp_us); 1393 input_sync(ds->sensors); 1394 1395 for (i = 0; i < ARRAY_SIZE(ds_report->points); i++) { 1396 struct dualsense_touch_point *point = &ds_report->points[i]; 1397 bool active = (point->contact & DS_TOUCH_POINT_INACTIVE) ? false : true; 1398 1399 input_mt_slot(ds->touchpad, i); 1400 input_mt_report_slot_state(ds->touchpad, MT_TOOL_FINGER, active); 1401 1402 if (active) { 1403 int x = (point->x_hi << 8) | point->x_lo; 1404 int y = (point->y_hi << 4) | point->y_lo; 1405 1406 input_report_abs(ds->touchpad, ABS_MT_POSITION_X, x); 1407 input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, y); 1408 } 1409 } 1410 input_mt_sync_frame(ds->touchpad); 1411 input_report_key(ds->touchpad, BTN_LEFT, ds_report->buttons[2] & DS_BUTTONS2_TOUCHPAD); 1412 input_sync(ds->touchpad); 1413 1414 battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY; 1415 charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT; 1416 1417 switch (charging_status) { 1418 case 0x0: 1419 /* 1420 * Each unit of battery data corresponds to 10% 1421 * 0 = 0-9%, 1 = 10-19%, .. and 10 = 100% 1422 */ 1423 battery_capacity = min(battery_data * 10 + 5, 100); 1424 battery_status = POWER_SUPPLY_STATUS_DISCHARGING; 1425 break; 1426 case 0x1: 1427 battery_capacity = min(battery_data * 10 + 5, 100); 1428 battery_status = POWER_SUPPLY_STATUS_CHARGING; 1429 break; 1430 case 0x2: 1431 battery_capacity = 100; 1432 battery_status = POWER_SUPPLY_STATUS_FULL; 1433 break; 1434 case 0xa: /* voltage or temperature out of range */ 1435 case 0xb: /* temperature error */ 1436 battery_capacity = 0; 1437 battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1438 break; 1439 case 0xf: /* charging error */ 1440 default: 1441 battery_capacity = 0; 1442 battery_status = POWER_SUPPLY_STATUS_UNKNOWN; 1443 } 1444 1445 spin_lock_irqsave(&ps_dev->lock, flags); 1446 ps_dev->battery_capacity = battery_capacity; 1447 ps_dev->battery_status = battery_status; 1448 spin_unlock_irqrestore(&ps_dev->lock, flags); 1449 1450 return 0; 1451 } 1452 1453 static int dualsense_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect) 1454 { 1455 struct hid_device *hdev = input_get_drvdata(dev); 1456 struct dualsense *ds = hid_get_drvdata(hdev); 1457 unsigned long flags; 1458 1459 if (effect->type != FF_RUMBLE) 1460 return 0; 1461 1462 spin_lock_irqsave(&ds->base.lock, flags); 1463 ds->update_rumble = true; 1464 ds->motor_left = effect->u.rumble.strong_magnitude / 256; 1465 ds->motor_right = effect->u.rumble.weak_magnitude / 256; 1466 spin_unlock_irqrestore(&ds->base.lock, flags); 1467 1468 dualsense_schedule_work(ds); 1469 return 0; 1470 } 1471 1472 static void dualsense_remove(struct ps_device *ps_dev) 1473 { 1474 struct dualsense *ds = container_of(ps_dev, struct dualsense, base); 1475 unsigned long flags; 1476 1477 spin_lock_irqsave(&ds->base.lock, flags); 1478 ds->output_worker_initialized = false; 1479 spin_unlock_irqrestore(&ds->base.lock, flags); 1480 1481 cancel_work_sync(&ds->output_worker); 1482 } 1483 1484 static int dualsense_reset_leds(struct dualsense *ds) 1485 { 1486 struct dualsense_output_report report; 1487 uint8_t *buf; 1488 1489 buf = kzalloc(sizeof(struct dualsense_output_report_bt), GFP_KERNEL); 1490 if (!buf) 1491 return -ENOMEM; 1492 1493 dualsense_init_output_report(ds, &report, buf); 1494 /* 1495 * On Bluetooth the DualSense outputs an animation on the lightbar 1496 * during startup and maintains a color afterwards. We need to explicitly 1497 * reconfigure the lightbar before we can do any programming later on. 1498 * In USB the lightbar is not on by default, but redoing the setup there 1499 * doesn't hurt. 1500 */ 1501 report.common->valid_flag2 = DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE; 1502 report.common->lightbar_setup = DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT; /* Fade light out. */ 1503 dualsense_send_output_report(ds, &report); 1504 1505 kfree(buf); 1506 return 0; 1507 } 1508 1509 static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue) 1510 { 1511 unsigned long flags; 1512 1513 spin_lock_irqsave(&ds->base.lock, flags); 1514 ds->update_lightbar = true; 1515 ds->lightbar_red = red; 1516 ds->lightbar_green = green; 1517 ds->lightbar_blue = blue; 1518 spin_unlock_irqrestore(&ds->base.lock, flags); 1519 1520 dualsense_schedule_work(ds); 1521 } 1522 1523 static void dualsense_set_player_leds(struct dualsense *ds) 1524 { 1525 /* 1526 * The DualSense controller has a row of 5 LEDs used for player ids. 1527 * Behavior on the PlayStation 5 console is to center the player id 1528 * across the LEDs, so e.g. player 1 would be "--x--" with x being 'on'. 1529 * Follow a similar mapping here. 1530 */ 1531 static const int player_ids[5] = { 1532 BIT(2), 1533 BIT(3) | BIT(1), 1534 BIT(4) | BIT(2) | BIT(0), 1535 BIT(4) | BIT(3) | BIT(1) | BIT(0), 1536 BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0) 1537 }; 1538 1539 uint8_t player_id = ds->base.player_id % ARRAY_SIZE(player_ids); 1540 1541 ds->update_player_leds = true; 1542 ds->player_leds_state = player_ids[player_id]; 1543 dualsense_schedule_work(ds); 1544 } 1545 1546 static struct ps_device *dualsense_create(struct hid_device *hdev) 1547 { 1548 struct dualsense *ds; 1549 struct ps_device *ps_dev; 1550 uint8_t max_output_report_size; 1551 int i, ret; 1552 1553 static const struct ps_led_info player_leds_info[] = { 1554 { LED_FUNCTION_PLAYER1, "white", 1, dualsense_player_led_get_brightness, 1555 dualsense_player_led_set_brightness }, 1556 { LED_FUNCTION_PLAYER2, "white", 1, dualsense_player_led_get_brightness, 1557 dualsense_player_led_set_brightness }, 1558 { LED_FUNCTION_PLAYER3, "white", 1, dualsense_player_led_get_brightness, 1559 dualsense_player_led_set_brightness }, 1560 { LED_FUNCTION_PLAYER4, "white", 1, dualsense_player_led_get_brightness, 1561 dualsense_player_led_set_brightness }, 1562 { LED_FUNCTION_PLAYER5, "white", 1, dualsense_player_led_get_brightness, 1563 dualsense_player_led_set_brightness } 1564 }; 1565 1566 ds = devm_kzalloc(&hdev->dev, sizeof(*ds), GFP_KERNEL); 1567 if (!ds) 1568 return ERR_PTR(-ENOMEM); 1569 1570 /* 1571 * Patch version to allow userspace to distinguish between 1572 * hid-generic vs hid-playstation axis and button mapping. 1573 */ 1574 hdev->version |= HID_PLAYSTATION_VERSION_PATCH; 1575 1576 ps_dev = &ds->base; 1577 ps_dev->hdev = hdev; 1578 spin_lock_init(&ps_dev->lock); 1579 ps_dev->battery_capacity = 100; /* initial value until parse_report. */ 1580 ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN; 1581 ps_dev->parse_report = dualsense_parse_report; 1582 ps_dev->remove = dualsense_remove; 1583 INIT_WORK(&ds->output_worker, dualsense_output_worker); 1584 ds->output_worker_initialized = true; 1585 hid_set_drvdata(hdev, ds); 1586 1587 max_output_report_size = sizeof(struct dualsense_output_report_bt); 1588 ds->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL); 1589 if (!ds->output_report_dmabuf) 1590 return ERR_PTR(-ENOMEM); 1591 1592 ret = dualsense_get_mac_address(ds); 1593 if (ret) { 1594 hid_err(hdev, "Failed to get MAC address from DualSense\n"); 1595 return ERR_PTR(ret); 1596 } 1597 snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address); 1598 1599 ret = dualsense_get_firmware_info(ds); 1600 if (ret) { 1601 hid_err(hdev, "Failed to get firmware info from DualSense\n"); 1602 return ERR_PTR(ret); 1603 } 1604 1605 /* Original DualSense firmware simulated classic controller rumble through 1606 * its new haptics hardware. It felt different from classic rumble users 1607 * were used to. Since then new firmwares were introduced to change behavior 1608 * and make this new 'v2' behavior default on PlayStation and other platforms. 1609 * The original DualSense requires a new enough firmware as bundled with PS5 1610 * software released in 2021. DualSense edge supports it out of the box. 1611 * Both devices also support the old mode, but it is not really used. 1612 */ 1613 if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER) { 1614 /* Feature version 2.21 introduced new vibration method. */ 1615 ds->use_vibration_v2 = ds->update_version >= DS_FEATURE_VERSION(2, 21); 1616 } else if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER_2) { 1617 ds->use_vibration_v2 = true; 1618 } 1619 1620 ret = ps_devices_list_add(ps_dev); 1621 if (ret) 1622 return ERR_PTR(ret); 1623 1624 ret = dualsense_get_calibration_data(ds); 1625 if (ret) { 1626 hid_err(hdev, "Failed to get calibration data from DualSense\n"); 1627 goto err; 1628 } 1629 1630 ds->gamepad = ps_gamepad_create(hdev, dualsense_play_effect); 1631 if (IS_ERR(ds->gamepad)) { 1632 ret = PTR_ERR(ds->gamepad); 1633 goto err; 1634 } 1635 /* Use gamepad input device name as primary device name for e.g. LEDs */ 1636 ps_dev->input_dev_name = dev_name(&ds->gamepad->dev); 1637 1638 ds->sensors = ps_sensors_create(hdev, DS_ACC_RANGE, DS_ACC_RES_PER_G, 1639 DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S); 1640 if (IS_ERR(ds->sensors)) { 1641 ret = PTR_ERR(ds->sensors); 1642 goto err; 1643 } 1644 1645 ds->touchpad = ps_touchpad_create(hdev, DS_TOUCHPAD_WIDTH, DS_TOUCHPAD_HEIGHT, 2); 1646 if (IS_ERR(ds->touchpad)) { 1647 ret = PTR_ERR(ds->touchpad); 1648 goto err; 1649 } 1650 1651 ret = ps_device_register_battery(ps_dev); 1652 if (ret) 1653 goto err; 1654 1655 /* 1656 * The hardware may have control over the LEDs (e.g. in Bluetooth on startup). 1657 * Reset the LEDs (lightbar, mute, player leds), so we can control them 1658 * from software. 1659 */ 1660 ret = dualsense_reset_leds(ds); 1661 if (ret) 1662 goto err; 1663 1664 ret = ps_lightbar_register(ps_dev, &ds->lightbar, dualsense_lightbar_set_brightness); 1665 if (ret) 1666 goto err; 1667 1668 /* Set default lightbar color. */ 1669 dualsense_set_lightbar(ds, 0, 0, 128); /* blue */ 1670 1671 for (i = 0; i < ARRAY_SIZE(player_leds_info); i++) { 1672 const struct ps_led_info *led_info = &player_leds_info[i]; 1673 1674 ret = ps_led_register(ps_dev, &ds->player_leds[i], led_info); 1675 if (ret < 0) 1676 goto err; 1677 } 1678 1679 ret = ps_device_set_player_id(ps_dev); 1680 if (ret) { 1681 hid_err(hdev, "Failed to assign player id for DualSense: %d\n", ret); 1682 goto err; 1683 } 1684 1685 /* Set player LEDs to our player id. */ 1686 dualsense_set_player_leds(ds); 1687 1688 /* 1689 * Reporting hardware and firmware is important as there are frequent updates, which 1690 * can change behavior. 1691 */ 1692 hid_info(hdev, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n", 1693 ds->base.hw_version, ds->base.fw_version); 1694 1695 return &ds->base; 1696 1697 err: 1698 ps_devices_list_remove(ps_dev); 1699 return ERR_PTR(ret); 1700 } 1701 1702 static void dualshock4_dongle_calibration_work(struct work_struct *work) 1703 { 1704 struct dualshock4 *ds4 = container_of(work, struct dualshock4, dongle_hotplug_worker); 1705 unsigned long flags; 1706 enum dualshock4_dongle_state dongle_state; 1707 int ret; 1708 1709 ret = dualshock4_get_calibration_data(ds4); 1710 if (ret < 0) { 1711 /* This call is very unlikely to fail for the dongle. When it 1712 * fails we are probably in a very bad state, so mark the 1713 * dongle as disabled. We will re-enable the dongle if a new 1714 * DS4 hotplug is detect from sony_raw_event as any issues 1715 * are likely resolved then (the dongle is quite stupid). 1716 */ 1717 hid_err(ds4->base.hdev, "DualShock 4 USB dongle: calibration failed, disabling device\n"); 1718 dongle_state = DONGLE_DISABLED; 1719 } else { 1720 hid_info(ds4->base.hdev, "DualShock 4 USB dongle: calibration completed\n"); 1721 dongle_state = DONGLE_CONNECTED; 1722 } 1723 1724 spin_lock_irqsave(&ds4->base.lock, flags); 1725 ds4->dongle_state = dongle_state; 1726 spin_unlock_irqrestore(&ds4->base.lock, flags); 1727 } 1728 1729 static int dualshock4_get_calibration_data(struct dualshock4 *ds4) 1730 { 1731 struct hid_device *hdev = ds4->base.hdev; 1732 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus; 1733 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus; 1734 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus; 1735 short gyro_speed_plus, gyro_speed_minus; 1736 short acc_x_plus, acc_x_minus; 1737 short acc_y_plus, acc_y_minus; 1738 short acc_z_plus, acc_z_minus; 1739 int speed_2x; 1740 int range_2g; 1741 int ret = 0; 1742 uint8_t *buf; 1743 1744 if (ds4->base.hdev->bus == BUS_USB) { 1745 int retries; 1746 1747 buf = kzalloc(DS4_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL); 1748 if (!buf) 1749 return -ENOMEM; 1750 1751 /* We should normally receive the feature report data we asked 1752 * for, but hidraw applications such as Steam can issue feature 1753 * reports as well. In particular for Dongle reconnects, Steam 1754 * and this function are competing resulting in often receiving 1755 * data for a different HID report, so retry a few times. 1756 */ 1757 for (retries = 0; retries < 3; retries++) { 1758 ret = ps_get_report(hdev, DS4_FEATURE_REPORT_CALIBRATION, buf, 1759 DS4_FEATURE_REPORT_CALIBRATION_SIZE, true); 1760 if (ret) { 1761 if (retries < 2) { 1762 hid_warn(hdev, "Retrying DualShock 4 get calibration report (0x02) request\n"); 1763 continue; 1764 } 1765 1766 hid_err(hdev, "Failed to retrieve DualShock4 calibration info: %d\n", ret); 1767 ret = -EILSEQ; 1768 goto err_free; 1769 } else { 1770 break; 1771 } 1772 } 1773 } else { /* Bluetooth */ 1774 buf = kzalloc(DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE, GFP_KERNEL); 1775 if (!buf) 1776 return -ENOMEM; 1777 1778 ret = ps_get_report(hdev, DS4_FEATURE_REPORT_CALIBRATION_BT, buf, 1779 DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE, true); 1780 if (ret) { 1781 hid_err(hdev, "Failed to retrieve DualShock4 calibration info: %d\n", ret); 1782 goto err_free; 1783 } 1784 } 1785 1786 gyro_pitch_bias = get_unaligned_le16(&buf[1]); 1787 gyro_yaw_bias = get_unaligned_le16(&buf[3]); 1788 gyro_roll_bias = get_unaligned_le16(&buf[5]); 1789 if (ds4->base.hdev->bus == BUS_USB) { 1790 gyro_pitch_plus = get_unaligned_le16(&buf[7]); 1791 gyro_pitch_minus = get_unaligned_le16(&buf[9]); 1792 gyro_yaw_plus = get_unaligned_le16(&buf[11]); 1793 gyro_yaw_minus = get_unaligned_le16(&buf[13]); 1794 gyro_roll_plus = get_unaligned_le16(&buf[15]); 1795 gyro_roll_minus = get_unaligned_le16(&buf[17]); 1796 } else { 1797 /* BT + Dongle */ 1798 gyro_pitch_plus = get_unaligned_le16(&buf[7]); 1799 gyro_yaw_plus = get_unaligned_le16(&buf[9]); 1800 gyro_roll_plus = get_unaligned_le16(&buf[11]); 1801 gyro_pitch_minus = get_unaligned_le16(&buf[13]); 1802 gyro_yaw_minus = get_unaligned_le16(&buf[15]); 1803 gyro_roll_minus = get_unaligned_le16(&buf[17]); 1804 } 1805 gyro_speed_plus = get_unaligned_le16(&buf[19]); 1806 gyro_speed_minus = get_unaligned_le16(&buf[21]); 1807 acc_x_plus = get_unaligned_le16(&buf[23]); 1808 acc_x_minus = get_unaligned_le16(&buf[25]); 1809 acc_y_plus = get_unaligned_le16(&buf[27]); 1810 acc_y_minus = get_unaligned_le16(&buf[29]); 1811 acc_z_plus = get_unaligned_le16(&buf[31]); 1812 acc_z_minus = get_unaligned_le16(&buf[33]); 1813 1814 /* 1815 * Set gyroscope calibration and normalization parameters. 1816 * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s. 1817 */ 1818 speed_2x = (gyro_speed_plus + gyro_speed_minus); 1819 ds4->gyro_calib_data[0].abs_code = ABS_RX; 1820 ds4->gyro_calib_data[0].bias = 0; 1821 ds4->gyro_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; 1822 ds4->gyro_calib_data[0].sens_denom = abs(gyro_pitch_plus - gyro_pitch_bias) + 1823 abs(gyro_pitch_minus - gyro_pitch_bias); 1824 1825 ds4->gyro_calib_data[1].abs_code = ABS_RY; 1826 ds4->gyro_calib_data[1].bias = 0; 1827 ds4->gyro_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; 1828 ds4->gyro_calib_data[1].sens_denom = abs(gyro_yaw_plus - gyro_yaw_bias) + 1829 abs(gyro_yaw_minus - gyro_yaw_bias); 1830 1831 ds4->gyro_calib_data[2].abs_code = ABS_RZ; 1832 ds4->gyro_calib_data[2].bias = 0; 1833 ds4->gyro_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; 1834 ds4->gyro_calib_data[2].sens_denom = abs(gyro_roll_plus - gyro_roll_bias) + 1835 abs(gyro_roll_minus - gyro_roll_bias); 1836 1837 /* 1838 * Set accelerometer calibration and normalization parameters. 1839 * Data values will be normalized to 1/DS4_ACC_RES_PER_G g. 1840 */ 1841 range_2g = acc_x_plus - acc_x_minus; 1842 ds4->accel_calib_data[0].abs_code = ABS_X; 1843 ds4->accel_calib_data[0].bias = acc_x_plus - range_2g / 2; 1844 ds4->accel_calib_data[0].sens_numer = 2*DS4_ACC_RES_PER_G; 1845 ds4->accel_calib_data[0].sens_denom = range_2g; 1846 1847 range_2g = acc_y_plus - acc_y_minus; 1848 ds4->accel_calib_data[1].abs_code = ABS_Y; 1849 ds4->accel_calib_data[1].bias = acc_y_plus - range_2g / 2; 1850 ds4->accel_calib_data[1].sens_numer = 2*DS4_ACC_RES_PER_G; 1851 ds4->accel_calib_data[1].sens_denom = range_2g; 1852 1853 range_2g = acc_z_plus - acc_z_minus; 1854 ds4->accel_calib_data[2].abs_code = ABS_Z; 1855 ds4->accel_calib_data[2].bias = acc_z_plus - range_2g / 2; 1856 ds4->accel_calib_data[2].sens_numer = 2*DS4_ACC_RES_PER_G; 1857 ds4->accel_calib_data[2].sens_denom = range_2g; 1858 1859 err_free: 1860 kfree(buf); 1861 return ret; 1862 } 1863 1864 static int dualshock4_get_firmware_info(struct dualshock4 *ds4) 1865 { 1866 uint8_t *buf; 1867 int ret; 1868 1869 buf = kzalloc(DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL); 1870 if (!buf) 1871 return -ENOMEM; 1872 1873 /* Note USB and BT support the same feature report, but this report 1874 * lacks CRC support, so must be disabled in ps_get_report. 1875 */ 1876 ret = ps_get_report(ds4->base.hdev, DS4_FEATURE_REPORT_FIRMWARE_INFO, buf, 1877 DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE, false); 1878 if (ret) { 1879 hid_err(ds4->base.hdev, "Failed to retrieve DualShock4 firmware info: %d\n", ret); 1880 goto err_free; 1881 } 1882 1883 ds4->base.hw_version = get_unaligned_le16(&buf[35]); 1884 ds4->base.fw_version = get_unaligned_le16(&buf[41]); 1885 1886 err_free: 1887 kfree(buf); 1888 return ret; 1889 } 1890 1891 static int dualshock4_get_mac_address(struct dualshock4 *ds4) 1892 { 1893 struct hid_device *hdev = ds4->base.hdev; 1894 uint8_t *buf; 1895 int ret = 0; 1896 1897 if (hdev->bus == BUS_USB) { 1898 buf = kzalloc(DS4_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL); 1899 if (!buf) 1900 return -ENOMEM; 1901 1902 ret = ps_get_report(hdev, DS4_FEATURE_REPORT_PAIRING_INFO, buf, 1903 DS4_FEATURE_REPORT_PAIRING_INFO_SIZE, false); 1904 if (ret) { 1905 hid_err(hdev, "Failed to retrieve DualShock4 pairing info: %d\n", ret); 1906 goto err_free; 1907 } 1908 1909 memcpy(ds4->base.mac_address, &buf[1], sizeof(ds4->base.mac_address)); 1910 } else { 1911 /* Rely on HIDP for Bluetooth */ 1912 if (strlen(hdev->uniq) != 17) 1913 return -EINVAL; 1914 1915 ret = sscanf(hdev->uniq, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 1916 &ds4->base.mac_address[5], &ds4->base.mac_address[4], 1917 &ds4->base.mac_address[3], &ds4->base.mac_address[2], 1918 &ds4->base.mac_address[1], &ds4->base.mac_address[0]); 1919 1920 if (ret != sizeof(ds4->base.mac_address)) 1921 return -EINVAL; 1922 1923 return 0; 1924 } 1925 1926 err_free: 1927 kfree(buf); 1928 return ret; 1929 } 1930 1931 static enum led_brightness dualshock4_led_get_brightness(struct led_classdev *led) 1932 { 1933 struct hid_device *hdev = to_hid_device(led->dev->parent); 1934 struct dualshock4 *ds4 = hid_get_drvdata(hdev); 1935 unsigned int led_index; 1936 1937 led_index = led - ds4->lightbar_leds; 1938 switch (led_index) { 1939 case 0: 1940 return ds4->lightbar_red; 1941 case 1: 1942 return ds4->lightbar_green; 1943 case 2: 1944 return ds4->lightbar_blue; 1945 case 3: 1946 return ds4->lightbar_enabled; 1947 } 1948 1949 return -1; 1950 } 1951 1952 static int dualshock4_led_set_blink(struct led_classdev *led, unsigned long *delay_on, 1953 unsigned long *delay_off) 1954 { 1955 struct hid_device *hdev = to_hid_device(led->dev->parent); 1956 struct dualshock4 *ds4 = hid_get_drvdata(hdev); 1957 unsigned long flags; 1958 1959 spin_lock_irqsave(&ds4->base.lock, flags); 1960 1961 if (!*delay_on && !*delay_off) { 1962 /* Default to 1 Hz (50 centiseconds on, 50 centiseconds off). */ 1963 ds4->lightbar_blink_on = 50; 1964 ds4->lightbar_blink_off = 50; 1965 } else { 1966 /* Blink delays in centiseconds. */ 1967 ds4->lightbar_blink_on = min_t(unsigned long, *delay_on/10, DS4_LIGHTBAR_MAX_BLINK); 1968 ds4->lightbar_blink_off = min_t(unsigned long, *delay_off/10, DS4_LIGHTBAR_MAX_BLINK); 1969 } 1970 1971 ds4->update_lightbar_blink = true; 1972 1973 spin_unlock_irqrestore(&ds4->base.lock, flags); 1974 1975 dualshock4_schedule_work(ds4); 1976 1977 *delay_on = ds4->lightbar_blink_on; 1978 *delay_off = ds4->lightbar_blink_off; 1979 1980 return 0; 1981 } 1982 1983 static int dualshock4_led_set_brightness(struct led_classdev *led, enum led_brightness value) 1984 { 1985 struct hid_device *hdev = to_hid_device(led->dev->parent); 1986 struct dualshock4 *ds4 = hid_get_drvdata(hdev); 1987 unsigned long flags; 1988 unsigned int led_index; 1989 1990 spin_lock_irqsave(&ds4->base.lock, flags); 1991 1992 led_index = led - ds4->lightbar_leds; 1993 switch (led_index) { 1994 case 0: 1995 ds4->lightbar_red = value; 1996 break; 1997 case 1: 1998 ds4->lightbar_green = value; 1999 break; 2000 case 2: 2001 ds4->lightbar_blue = value; 2002 break; 2003 case 3: 2004 ds4->lightbar_enabled = !!value; 2005 } 2006 2007 ds4->update_lightbar = true; 2008 2009 spin_unlock_irqrestore(&ds4->base.lock, flags); 2010 2011 dualshock4_schedule_work(ds4); 2012 2013 return 0; 2014 } 2015 2016 static void dualshock4_init_output_report(struct dualshock4 *ds4, 2017 struct dualshock4_output_report *rp, void *buf) 2018 { 2019 struct hid_device *hdev = ds4->base.hdev; 2020 2021 if (hdev->bus == BUS_BLUETOOTH) { 2022 struct dualshock4_output_report_bt *bt = buf; 2023 2024 memset(bt, 0, sizeof(*bt)); 2025 bt->report_id = DS4_OUTPUT_REPORT_BT; 2026 2027 rp->data = buf; 2028 rp->len = sizeof(*bt); 2029 rp->bt = bt; 2030 rp->usb = NULL; 2031 rp->common = &bt->common; 2032 } else { /* USB */ 2033 struct dualshock4_output_report_usb *usb = buf; 2034 2035 memset(usb, 0, sizeof(*usb)); 2036 usb->report_id = DS4_OUTPUT_REPORT_USB; 2037 2038 rp->data = buf; 2039 rp->len = sizeof(*usb); 2040 rp->bt = NULL; 2041 rp->usb = usb; 2042 rp->common = &usb->common; 2043 } 2044 } 2045 2046 static void dualshock4_output_worker(struct work_struct *work) 2047 { 2048 struct dualshock4 *ds4 = container_of(work, struct dualshock4, output_worker); 2049 struct dualshock4_output_report report; 2050 struct dualshock4_output_report_common *common; 2051 unsigned long flags; 2052 2053 dualshock4_init_output_report(ds4, &report, ds4->output_report_dmabuf); 2054 common = report.common; 2055 2056 spin_lock_irqsave(&ds4->base.lock, flags); 2057 2058 if (ds4->update_rumble) { 2059 /* Select classic rumble style haptics and enable it. */ 2060 common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_MOTOR; 2061 common->motor_left = ds4->motor_left; 2062 common->motor_right = ds4->motor_right; 2063 ds4->update_rumble = false; 2064 } 2065 2066 if (ds4->update_lightbar) { 2067 common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED; 2068 /* Comptabile behavior with hid-sony, which used a dummy global LED to 2069 * allow enabling/disabling the lightbar. The global LED maps to 2070 * lightbar_enabled. 2071 */ 2072 common->lightbar_red = ds4->lightbar_enabled ? ds4->lightbar_red : 0; 2073 common->lightbar_green = ds4->lightbar_enabled ? ds4->lightbar_green : 0; 2074 common->lightbar_blue = ds4->lightbar_enabled ? ds4->lightbar_blue : 0; 2075 ds4->update_lightbar = false; 2076 } 2077 2078 if (ds4->update_lightbar_blink) { 2079 common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED_BLINK; 2080 common->lightbar_blink_on = ds4->lightbar_blink_on; 2081 common->lightbar_blink_off = ds4->lightbar_blink_off; 2082 ds4->update_lightbar_blink = false; 2083 } 2084 2085 spin_unlock_irqrestore(&ds4->base.lock, flags); 2086 2087 /* Bluetooth packets need additional flags as well as a CRC in the last 4 bytes. */ 2088 if (report.bt) { 2089 uint32_t crc; 2090 uint8_t seed = PS_OUTPUT_CRC32_SEED; 2091 2092 /* Hardware control flags need to set to let the device know 2093 * there is HID data as well as CRC. 2094 */ 2095 report.bt->hw_control = DS4_OUTPUT_HWCTL_HID | DS4_OUTPUT_HWCTL_CRC32; 2096 2097 if (ds4->update_bt_poll_interval) { 2098 report.bt->hw_control |= ds4->bt_poll_interval; 2099 ds4->update_bt_poll_interval = false; 2100 } 2101 2102 crc = crc32_le(0xFFFFFFFF, &seed, 1); 2103 crc = ~crc32_le(crc, report.data, report.len - 4); 2104 2105 report.bt->crc32 = cpu_to_le32(crc); 2106 } 2107 2108 hid_hw_output_report(ds4->base.hdev, report.data, report.len); 2109 } 2110 2111 static int dualshock4_parse_report(struct ps_device *ps_dev, struct hid_report *report, 2112 u8 *data, int size) 2113 { 2114 struct hid_device *hdev = ps_dev->hdev; 2115 struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base); 2116 struct dualshock4_input_report_common *ds4_report; 2117 struct dualshock4_touch_report *touch_reports; 2118 uint8_t battery_capacity, num_touch_reports, value; 2119 int battery_status, i, j; 2120 uint16_t sensor_timestamp; 2121 unsigned long flags; 2122 2123 /* 2124 * DualShock4 in USB uses the full HID report for reportID 1, but 2125 * Bluetooth uses a minimal HID report for reportID 1 and reports 2126 * the full report using reportID 17. 2127 */ 2128 if (hdev->bus == BUS_USB && report->id == DS4_INPUT_REPORT_USB && 2129 size == DS4_INPUT_REPORT_USB_SIZE) { 2130 struct dualshock4_input_report_usb *usb = (struct dualshock4_input_report_usb *)data; 2131 2132 ds4_report = &usb->common; 2133 num_touch_reports = usb->num_touch_reports; 2134 touch_reports = usb->touch_reports; 2135 } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS4_INPUT_REPORT_BT && 2136 size == DS4_INPUT_REPORT_BT_SIZE) { 2137 struct dualshock4_input_report_bt *bt = (struct dualshock4_input_report_bt *)data; 2138 uint32_t report_crc = get_unaligned_le32(&bt->crc32); 2139 2140 /* Last 4 bytes of input report contains CRC. */ 2141 if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) { 2142 hid_err(hdev, "DualShock4 input CRC's check failed\n"); 2143 return -EILSEQ; 2144 } 2145 2146 ds4_report = &bt->common; 2147 num_touch_reports = bt->num_touch_reports; 2148 touch_reports = bt->touch_reports; 2149 } else { 2150 hid_err(hdev, "Unhandled reportID=%d\n", report->id); 2151 return -1; 2152 } 2153 2154 input_report_abs(ds4->gamepad, ABS_X, ds4_report->x); 2155 input_report_abs(ds4->gamepad, ABS_Y, ds4_report->y); 2156 input_report_abs(ds4->gamepad, ABS_RX, ds4_report->rx); 2157 input_report_abs(ds4->gamepad, ABS_RY, ds4_report->ry); 2158 input_report_abs(ds4->gamepad, ABS_Z, ds4_report->z); 2159 input_report_abs(ds4->gamepad, ABS_RZ, ds4_report->rz); 2160 2161 value = ds4_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH; 2162 if (value >= ARRAY_SIZE(ps_gamepad_hat_mapping)) 2163 value = 8; /* center */ 2164 input_report_abs(ds4->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x); 2165 input_report_abs(ds4->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y); 2166 2167 input_report_key(ds4->gamepad, BTN_WEST, ds4_report->buttons[0] & DS_BUTTONS0_SQUARE); 2168 input_report_key(ds4->gamepad, BTN_SOUTH, ds4_report->buttons[0] & DS_BUTTONS0_CROSS); 2169 input_report_key(ds4->gamepad, BTN_EAST, ds4_report->buttons[0] & DS_BUTTONS0_CIRCLE); 2170 input_report_key(ds4->gamepad, BTN_NORTH, ds4_report->buttons[0] & DS_BUTTONS0_TRIANGLE); 2171 input_report_key(ds4->gamepad, BTN_TL, ds4_report->buttons[1] & DS_BUTTONS1_L1); 2172 input_report_key(ds4->gamepad, BTN_TR, ds4_report->buttons[1] & DS_BUTTONS1_R1); 2173 input_report_key(ds4->gamepad, BTN_TL2, ds4_report->buttons[1] & DS_BUTTONS1_L2); 2174 input_report_key(ds4->gamepad, BTN_TR2, ds4_report->buttons[1] & DS_BUTTONS1_R2); 2175 input_report_key(ds4->gamepad, BTN_SELECT, ds4_report->buttons[1] & DS_BUTTONS1_CREATE); 2176 input_report_key(ds4->gamepad, BTN_START, ds4_report->buttons[1] & DS_BUTTONS1_OPTIONS); 2177 input_report_key(ds4->gamepad, BTN_THUMBL, ds4_report->buttons[1] & DS_BUTTONS1_L3); 2178 input_report_key(ds4->gamepad, BTN_THUMBR, ds4_report->buttons[1] & DS_BUTTONS1_R3); 2179 input_report_key(ds4->gamepad, BTN_MODE, ds4_report->buttons[2] & DS_BUTTONS2_PS_HOME); 2180 input_sync(ds4->gamepad); 2181 2182 /* Parse and calibrate gyroscope data. */ 2183 for (i = 0; i < ARRAY_SIZE(ds4_report->gyro); i++) { 2184 int raw_data = (short)le16_to_cpu(ds4_report->gyro[i]); 2185 int calib_data = mult_frac(ds4->gyro_calib_data[i].sens_numer, 2186 raw_data, ds4->gyro_calib_data[i].sens_denom); 2187 2188 input_report_abs(ds4->sensors, ds4->gyro_calib_data[i].abs_code, calib_data); 2189 } 2190 2191 /* Parse and calibrate accelerometer data. */ 2192 for (i = 0; i < ARRAY_SIZE(ds4_report->accel); i++) { 2193 int raw_data = (short)le16_to_cpu(ds4_report->accel[i]); 2194 int calib_data = mult_frac(ds4->accel_calib_data[i].sens_numer, 2195 raw_data - ds4->accel_calib_data[i].bias, 2196 ds4->accel_calib_data[i].sens_denom); 2197 2198 input_report_abs(ds4->sensors, ds4->accel_calib_data[i].abs_code, calib_data); 2199 } 2200 2201 /* Convert timestamp (in 5.33us unit) to timestamp_us */ 2202 sensor_timestamp = le16_to_cpu(ds4_report->sensor_timestamp); 2203 if (!ds4->sensor_timestamp_initialized) { 2204 ds4->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp*16, 3); 2205 ds4->sensor_timestamp_initialized = true; 2206 } else { 2207 uint16_t delta; 2208 2209 if (ds4->prev_sensor_timestamp > sensor_timestamp) 2210 delta = (U16_MAX - ds4->prev_sensor_timestamp + sensor_timestamp + 1); 2211 else 2212 delta = sensor_timestamp - ds4->prev_sensor_timestamp; 2213 ds4->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta*16, 3); 2214 } 2215 ds4->prev_sensor_timestamp = sensor_timestamp; 2216 input_event(ds4->sensors, EV_MSC, MSC_TIMESTAMP, ds4->sensor_timestamp_us); 2217 input_sync(ds4->sensors); 2218 2219 for (i = 0; i < num_touch_reports; i++) { 2220 struct dualshock4_touch_report *touch_report = &touch_reports[i]; 2221 2222 for (j = 0; j < ARRAY_SIZE(touch_report->points); j++) { 2223 struct dualshock4_touch_point *point = &touch_report->points[j]; 2224 bool active = (point->contact & DS4_TOUCH_POINT_INACTIVE) ? false : true; 2225 2226 input_mt_slot(ds4->touchpad, j); 2227 input_mt_report_slot_state(ds4->touchpad, MT_TOOL_FINGER, active); 2228 2229 if (active) { 2230 int x = (point->x_hi << 8) | point->x_lo; 2231 int y = (point->y_hi << 4) | point->y_lo; 2232 2233 input_report_abs(ds4->touchpad, ABS_MT_POSITION_X, x); 2234 input_report_abs(ds4->touchpad, ABS_MT_POSITION_Y, y); 2235 } 2236 } 2237 input_mt_sync_frame(ds4->touchpad); 2238 input_sync(ds4->touchpad); 2239 } 2240 input_report_key(ds4->touchpad, BTN_LEFT, ds4_report->buttons[2] & DS_BUTTONS2_TOUCHPAD); 2241 2242 /* 2243 * Interpretation of the battery_capacity data depends on the cable state. 2244 * When no cable is connected (bit4 is 0): 2245 * - 0:10: percentage in units of 10%. 2246 * When a cable is plugged in: 2247 * - 0-10: percentage in units of 10%. 2248 * - 11: battery is full 2249 * - 14: not charging due to Voltage or temperature error 2250 * - 15: charge error 2251 */ 2252 if (ds4_report->status[0] & DS4_STATUS0_CABLE_STATE) { 2253 uint8_t battery_data = ds4_report->status[0] & DS4_STATUS0_BATTERY_CAPACITY; 2254 2255 if (battery_data < 10) { 2256 /* Take the mid-point for each battery capacity value, 2257 * because on the hardware side 0 = 0-9%, 1=10-19%, etc. 2258 * This matches official platform behavior, which does 2259 * the same. 2260 */ 2261 battery_capacity = battery_data * 10 + 5; 2262 battery_status = POWER_SUPPLY_STATUS_CHARGING; 2263 } else if (battery_data == 10) { 2264 battery_capacity = 100; 2265 battery_status = POWER_SUPPLY_STATUS_CHARGING; 2266 } else if (battery_data == DS4_BATTERY_STATUS_FULL) { 2267 battery_capacity = 100; 2268 battery_status = POWER_SUPPLY_STATUS_FULL; 2269 } else { /* 14, 15 and undefined values */ 2270 battery_capacity = 0; 2271 battery_status = POWER_SUPPLY_STATUS_UNKNOWN; 2272 } 2273 } else { 2274 uint8_t battery_data = ds4_report->status[0] & DS4_STATUS0_BATTERY_CAPACITY; 2275 2276 if (battery_data < 10) 2277 battery_capacity = battery_data * 10 + 5; 2278 else /* 10 */ 2279 battery_capacity = 100; 2280 2281 battery_status = POWER_SUPPLY_STATUS_DISCHARGING; 2282 } 2283 2284 spin_lock_irqsave(&ps_dev->lock, flags); 2285 ps_dev->battery_capacity = battery_capacity; 2286 ps_dev->battery_status = battery_status; 2287 spin_unlock_irqrestore(&ps_dev->lock, flags); 2288 2289 return 0; 2290 } 2291 2292 static int dualshock4_dongle_parse_report(struct ps_device *ps_dev, struct hid_report *report, 2293 u8 *data, int size) 2294 { 2295 struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base); 2296 bool connected = false; 2297 2298 /* The dongle reports data using the main USB report (0x1) no matter whether a controller 2299 * is connected with mostly zeros. The report does contain dongle status, which we use to 2300 * determine if a controller is connected and if so we forward to the regular DualShock4 2301 * parsing code. 2302 */ 2303 if (data[0] == DS4_INPUT_REPORT_USB && size == DS4_INPUT_REPORT_USB_SIZE) { 2304 struct dualshock4_input_report_common *ds4_report = (struct dualshock4_input_report_common *)&data[1]; 2305 unsigned long flags; 2306 2307 connected = ds4_report->status[1] & DS4_STATUS1_DONGLE_STATE ? false : true; 2308 2309 if (ds4->dongle_state == DONGLE_DISCONNECTED && connected) { 2310 hid_info(ps_dev->hdev, "DualShock 4 USB dongle: controller connected\n"); 2311 2312 dualshock4_set_default_lightbar_colors(ds4); 2313 2314 spin_lock_irqsave(&ps_dev->lock, flags); 2315 ds4->dongle_state = DONGLE_CALIBRATING; 2316 spin_unlock_irqrestore(&ps_dev->lock, flags); 2317 2318 schedule_work(&ds4->dongle_hotplug_worker); 2319 2320 /* Don't process the report since we don't have 2321 * calibration data, but let hidraw have it anyway. 2322 */ 2323 return 0; 2324 } else if ((ds4->dongle_state == DONGLE_CONNECTED || 2325 ds4->dongle_state == DONGLE_DISABLED) && !connected) { 2326 hid_info(ps_dev->hdev, "DualShock 4 USB dongle: controller disconnected\n"); 2327 2328 spin_lock_irqsave(&ps_dev->lock, flags); 2329 ds4->dongle_state = DONGLE_DISCONNECTED; 2330 spin_unlock_irqrestore(&ps_dev->lock, flags); 2331 2332 /* Return 0, so hidraw can get the report. */ 2333 return 0; 2334 } else if (ds4->dongle_state == DONGLE_CALIBRATING || 2335 ds4->dongle_state == DONGLE_DISABLED || 2336 ds4->dongle_state == DONGLE_DISCONNECTED) { 2337 /* Return 0, so hidraw can get the report. */ 2338 return 0; 2339 } 2340 } 2341 2342 if (connected) 2343 return dualshock4_parse_report(ps_dev, report, data, size); 2344 2345 return 0; 2346 } 2347 2348 static int dualshock4_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect) 2349 { 2350 struct hid_device *hdev = input_get_drvdata(dev); 2351 struct dualshock4 *ds4 = hid_get_drvdata(hdev); 2352 unsigned long flags; 2353 2354 if (effect->type != FF_RUMBLE) 2355 return 0; 2356 2357 spin_lock_irqsave(&ds4->base.lock, flags); 2358 ds4->update_rumble = true; 2359 ds4->motor_left = effect->u.rumble.strong_magnitude / 256; 2360 ds4->motor_right = effect->u.rumble.weak_magnitude / 256; 2361 spin_unlock_irqrestore(&ds4->base.lock, flags); 2362 2363 dualshock4_schedule_work(ds4); 2364 return 0; 2365 } 2366 2367 static void dualshock4_remove(struct ps_device *ps_dev) 2368 { 2369 struct dualshock4 *ds4 = container_of(ps_dev, struct dualshock4, base); 2370 unsigned long flags; 2371 2372 spin_lock_irqsave(&ds4->base.lock, flags); 2373 ds4->output_worker_initialized = false; 2374 spin_unlock_irqrestore(&ds4->base.lock, flags); 2375 2376 cancel_work_sync(&ds4->output_worker); 2377 2378 if (ps_dev->hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE) 2379 cancel_work_sync(&ds4->dongle_hotplug_worker); 2380 } 2381 2382 static inline void dualshock4_schedule_work(struct dualshock4 *ds4) 2383 { 2384 unsigned long flags; 2385 2386 spin_lock_irqsave(&ds4->base.lock, flags); 2387 if (ds4->output_worker_initialized) 2388 schedule_work(&ds4->output_worker); 2389 spin_unlock_irqrestore(&ds4->base.lock, flags); 2390 } 2391 2392 static void dualshock4_set_bt_poll_interval(struct dualshock4 *ds4, uint8_t interval) 2393 { 2394 ds4->bt_poll_interval = interval; 2395 ds4->update_bt_poll_interval = true; 2396 dualshock4_schedule_work(ds4); 2397 } 2398 2399 /* Set default lightbar color based on player. */ 2400 static void dualshock4_set_default_lightbar_colors(struct dualshock4 *ds4) 2401 { 2402 /* Use same player colors as PlayStation 4. 2403 * Array of colors is in RGB. 2404 */ 2405 static const int player_colors[4][3] = { 2406 { 0x00, 0x00, 0x40 }, /* Blue */ 2407 { 0x40, 0x00, 0x00 }, /* Red */ 2408 { 0x00, 0x40, 0x00 }, /* Green */ 2409 { 0x20, 0x00, 0x20 } /* Pink */ 2410 }; 2411 2412 uint8_t player_id = ds4->base.player_id % ARRAY_SIZE(player_colors); 2413 2414 ds4->lightbar_enabled = true; 2415 ds4->lightbar_red = player_colors[player_id][0]; 2416 ds4->lightbar_green = player_colors[player_id][1]; 2417 ds4->lightbar_blue = player_colors[player_id][2]; 2418 2419 ds4->update_lightbar = true; 2420 dualshock4_schedule_work(ds4); 2421 } 2422 2423 static struct ps_device *dualshock4_create(struct hid_device *hdev) 2424 { 2425 struct dualshock4 *ds4; 2426 struct ps_device *ps_dev; 2427 uint8_t max_output_report_size; 2428 int i, ret; 2429 2430 /* The DualShock4 has an RGB lightbar, which the original hid-sony driver 2431 * exposed as a set of 4 LEDs for the 3 color channels and a global control. 2432 * Ideally this should have used the multi-color LED class, which didn't exist 2433 * yet. In addition the driver used a naming scheme not compliant with the LED 2434 * naming spec by using "<mac_address>:<color>", which contained many colons. 2435 * We use a more compliant by using "<device_name>:<color>" name now. Ideally 2436 * would have been "<device_name>:<color>:indicator", but that would break 2437 * existing applications (e.g. Android). Nothing matches against MAC address. 2438 */ 2439 static const struct ps_led_info lightbar_leds_info[] = { 2440 { NULL, "red", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, 2441 { NULL, "green", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, 2442 { NULL, "blue", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, 2443 { NULL, "global", 1, dualshock4_led_get_brightness, dualshock4_led_set_brightness, 2444 dualshock4_led_set_blink }, 2445 }; 2446 2447 ds4 = devm_kzalloc(&hdev->dev, sizeof(*ds4), GFP_KERNEL); 2448 if (!ds4) 2449 return ERR_PTR(-ENOMEM); 2450 2451 /* 2452 * Patch version to allow userspace to distinguish between 2453 * hid-generic vs hid-playstation axis and button mapping. 2454 */ 2455 hdev->version |= HID_PLAYSTATION_VERSION_PATCH; 2456 2457 ps_dev = &ds4->base; 2458 ps_dev->hdev = hdev; 2459 spin_lock_init(&ps_dev->lock); 2460 ps_dev->battery_capacity = 100; /* initial value until parse_report. */ 2461 ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN; 2462 ps_dev->parse_report = dualshock4_parse_report; 2463 ps_dev->remove = dualshock4_remove; 2464 INIT_WORK(&ds4->output_worker, dualshock4_output_worker); 2465 ds4->output_worker_initialized = true; 2466 hid_set_drvdata(hdev, ds4); 2467 2468 max_output_report_size = sizeof(struct dualshock4_output_report_bt); 2469 ds4->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL); 2470 if (!ds4->output_report_dmabuf) 2471 return ERR_PTR(-ENOMEM); 2472 2473 if (hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE) { 2474 ds4->dongle_state = DONGLE_DISCONNECTED; 2475 INIT_WORK(&ds4->dongle_hotplug_worker, dualshock4_dongle_calibration_work); 2476 2477 /* Override parse report for dongle specific hotplug handling. */ 2478 ps_dev->parse_report = dualshock4_dongle_parse_report; 2479 } 2480 2481 ret = dualshock4_get_mac_address(ds4); 2482 if (ret) { 2483 hid_err(hdev, "Failed to get MAC address from DualShock4\n"); 2484 return ERR_PTR(ret); 2485 } 2486 snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds4->base.mac_address); 2487 2488 ret = dualshock4_get_firmware_info(ds4); 2489 if (ret) { 2490 hid_err(hdev, "Failed to get firmware info from DualShock4\n"); 2491 return ERR_PTR(ret); 2492 } 2493 2494 ret = ps_devices_list_add(ps_dev); 2495 if (ret) 2496 return ERR_PTR(ret); 2497 2498 ret = dualshock4_get_calibration_data(ds4); 2499 if (ret) { 2500 hid_err(hdev, "Failed to get calibration data from DualShock4\n"); 2501 goto err; 2502 } 2503 2504 ds4->gamepad = ps_gamepad_create(hdev, dualshock4_play_effect); 2505 if (IS_ERR(ds4->gamepad)) { 2506 ret = PTR_ERR(ds4->gamepad); 2507 goto err; 2508 } 2509 2510 /* Use gamepad input device name as primary device name for e.g. LEDs */ 2511 ps_dev->input_dev_name = dev_name(&ds4->gamepad->dev); 2512 2513 ds4->sensors = ps_sensors_create(hdev, DS4_ACC_RANGE, DS4_ACC_RES_PER_G, 2514 DS4_GYRO_RANGE, DS4_GYRO_RES_PER_DEG_S); 2515 if (IS_ERR(ds4->sensors)) { 2516 ret = PTR_ERR(ds4->sensors); 2517 goto err; 2518 } 2519 2520 ds4->touchpad = ps_touchpad_create(hdev, DS4_TOUCHPAD_WIDTH, DS4_TOUCHPAD_HEIGHT, 2); 2521 if (IS_ERR(ds4->touchpad)) { 2522 ret = PTR_ERR(ds4->touchpad); 2523 goto err; 2524 } 2525 2526 ret = ps_device_register_battery(ps_dev); 2527 if (ret) 2528 goto err; 2529 2530 for (i = 0; i < ARRAY_SIZE(lightbar_leds_info); i++) { 2531 const struct ps_led_info *led_info = &lightbar_leds_info[i]; 2532 2533 ret = ps_led_register(ps_dev, &ds4->lightbar_leds[i], led_info); 2534 if (ret < 0) 2535 goto err; 2536 } 2537 2538 dualshock4_set_bt_poll_interval(ds4, DS4_BT_DEFAULT_POLL_INTERVAL_MS); 2539 2540 ret = ps_device_set_player_id(ps_dev); 2541 if (ret) { 2542 hid_err(hdev, "Failed to assign player id for DualShock4: %d\n", ret); 2543 goto err; 2544 } 2545 2546 dualshock4_set_default_lightbar_colors(ds4); 2547 2548 /* 2549 * Reporting hardware and firmware is important as there are frequent updates, which 2550 * can change behavior. 2551 */ 2552 hid_info(hdev, "Registered DualShock4 controller hw_version=0x%08x fw_version=0x%08x\n", 2553 ds4->base.hw_version, ds4->base.fw_version); 2554 return &ds4->base; 2555 2556 err: 2557 ps_devices_list_remove(ps_dev); 2558 return ERR_PTR(ret); 2559 } 2560 2561 static int ps_raw_event(struct hid_device *hdev, struct hid_report *report, 2562 u8 *data, int size) 2563 { 2564 struct ps_device *dev = hid_get_drvdata(hdev); 2565 2566 if (dev && dev->parse_report) 2567 return dev->parse_report(dev, report, data, size); 2568 2569 return 0; 2570 } 2571 2572 static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id) 2573 { 2574 struct ps_device *dev; 2575 int ret; 2576 2577 ret = hid_parse(hdev); 2578 if (ret) { 2579 hid_err(hdev, "Parse failed\n"); 2580 return ret; 2581 } 2582 2583 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); 2584 if (ret) { 2585 hid_err(hdev, "Failed to start HID device\n"); 2586 return ret; 2587 } 2588 2589 ret = hid_hw_open(hdev); 2590 if (ret) { 2591 hid_err(hdev, "Failed to open HID device\n"); 2592 goto err_stop; 2593 } 2594 2595 if (hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER || 2596 hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER_2 || 2597 hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE) { 2598 dev = dualshock4_create(hdev); 2599 if (IS_ERR(dev)) { 2600 hid_err(hdev, "Failed to create dualshock4.\n"); 2601 ret = PTR_ERR(dev); 2602 goto err_close; 2603 } 2604 } else if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER || 2605 hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER_2) { 2606 dev = dualsense_create(hdev); 2607 if (IS_ERR(dev)) { 2608 hid_err(hdev, "Failed to create dualsense.\n"); 2609 ret = PTR_ERR(dev); 2610 goto err_close; 2611 } 2612 } 2613 2614 return ret; 2615 2616 err_close: 2617 hid_hw_close(hdev); 2618 err_stop: 2619 hid_hw_stop(hdev); 2620 return ret; 2621 } 2622 2623 static void ps_remove(struct hid_device *hdev) 2624 { 2625 struct ps_device *dev = hid_get_drvdata(hdev); 2626 2627 ps_devices_list_remove(dev); 2628 ps_device_release_player_id(dev); 2629 2630 if (dev->remove) 2631 dev->remove(dev); 2632 2633 hid_hw_close(hdev); 2634 hid_hw_stop(hdev); 2635 } 2636 2637 static const struct hid_device_id ps_devices[] = { 2638 /* Sony DualShock 4 controllers for PS4 */ 2639 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER) }, 2640 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER) }, 2641 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2) }, 2642 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2) }, 2643 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE) }, 2644 /* Sony DualSense controllers for PS5 */ 2645 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) }, 2646 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) }, 2647 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER_2) }, 2648 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER_2) }, 2649 { } 2650 }; 2651 MODULE_DEVICE_TABLE(hid, ps_devices); 2652 2653 static struct hid_driver ps_driver = { 2654 .name = "playstation", 2655 .id_table = ps_devices, 2656 .probe = ps_probe, 2657 .remove = ps_remove, 2658 .raw_event = ps_raw_event, 2659 .driver = { 2660 .dev_groups = ps_device_groups, 2661 }, 2662 }; 2663 2664 static int __init ps_init(void) 2665 { 2666 return hid_register_driver(&ps_driver); 2667 } 2668 2669 static void __exit ps_exit(void) 2670 { 2671 hid_unregister_driver(&ps_driver); 2672 ida_destroy(&ps_player_id_allocator); 2673 } 2674 2675 module_init(ps_init); 2676 module_exit(ps_exit); 2677 2678 MODULE_AUTHOR("Sony Interactive Entertainment"); 2679 MODULE_DESCRIPTION("HID Driver for PlayStation peripherals."); 2680 MODULE_LICENSE("GPL"); 2681