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