Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * HID driver for Sony DualSense(TM) controller. |
| 4 | * |
| 5 | * Copyright (c) 2020 Sony Interactive Entertainment |
| 6 | */ |
| 7 | |
| 8 | #include <linux/bits.h> |
Roderick Colenbrander | 799b2b5 | 2021-02-07 13:49:02 -0800 | [diff] [blame^] | 9 | #include <linux/crc32.h> |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 10 | #include <linux/device.h> |
| 11 | #include <linux/hid.h> |
| 12 | #include <linux/input/mt.h> |
| 13 | #include <linux/module.h> |
| 14 | |
| 15 | #include <asm/unaligned.h> |
| 16 | |
| 17 | #include "hid-ids.h" |
| 18 | |
Roderick Colenbrander | 53f04e8 | 2021-02-07 13:49:01 -0800 | [diff] [blame] | 19 | /* List of connected playstation devices. */ |
| 20 | static DEFINE_MUTEX(ps_devices_lock); |
| 21 | static LIST_HEAD(ps_devices_list); |
| 22 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 23 | #define HID_PLAYSTATION_VERSION_PATCH 0x8000 |
| 24 | |
| 25 | /* Base class for playstation devices. */ |
| 26 | struct ps_device { |
Roderick Colenbrander | 53f04e8 | 2021-02-07 13:49:01 -0800 | [diff] [blame] | 27 | struct list_head list; |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 28 | struct hid_device *hdev; |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 29 | spinlock_t lock; |
| 30 | |
| 31 | struct power_supply_desc battery_desc; |
| 32 | struct power_supply *battery; |
| 33 | uint8_t battery_capacity; |
| 34 | int battery_status; |
| 35 | |
Roderick Colenbrander | b99dcef | 2021-02-07 13:48:57 -0800 | [diff] [blame] | 36 | uint8_t mac_address[6]; /* Note: stored in little endian order. */ |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 37 | |
| 38 | int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size); |
| 39 | }; |
| 40 | |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 41 | /* Calibration data for playstation motion sensors. */ |
| 42 | struct ps_calibration_data { |
| 43 | int abs_code; |
| 44 | short bias; |
| 45 | int sens_numer; |
| 46 | int sens_denom; |
| 47 | }; |
| 48 | |
Roderick Colenbrander | 799b2b5 | 2021-02-07 13:49:02 -0800 | [diff] [blame^] | 49 | /* Seed values for DualShock4 / DualSense CRC32 for different report types. */ |
| 50 | #define PS_INPUT_CRC32_SEED 0xA1 |
| 51 | #define PS_FEATURE_CRC32_SEED 0xA3 |
| 52 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 53 | #define DS_INPUT_REPORT_USB 0x01 |
| 54 | #define DS_INPUT_REPORT_USB_SIZE 64 |
Roderick Colenbrander | 799b2b5 | 2021-02-07 13:49:02 -0800 | [diff] [blame^] | 55 | #define DS_INPUT_REPORT_BT 0x31 |
| 56 | #define DS_INPUT_REPORT_BT_SIZE 78 |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 57 | |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 58 | #define DS_FEATURE_REPORT_CALIBRATION 0x05 |
| 59 | #define DS_FEATURE_REPORT_CALIBRATION_SIZE 41 |
Roderick Colenbrander | b99dcef | 2021-02-07 13:48:57 -0800 | [diff] [blame] | 60 | #define DS_FEATURE_REPORT_PAIRING_INFO 0x09 |
| 61 | #define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20 |
| 62 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 63 | /* Button masks for DualSense input report. */ |
| 64 | #define DS_BUTTONS0_HAT_SWITCH GENMASK(3, 0) |
| 65 | #define DS_BUTTONS0_SQUARE BIT(4) |
| 66 | #define DS_BUTTONS0_CROSS BIT(5) |
| 67 | #define DS_BUTTONS0_CIRCLE BIT(6) |
| 68 | #define DS_BUTTONS0_TRIANGLE BIT(7) |
| 69 | #define DS_BUTTONS1_L1 BIT(0) |
| 70 | #define DS_BUTTONS1_R1 BIT(1) |
| 71 | #define DS_BUTTONS1_L2 BIT(2) |
| 72 | #define DS_BUTTONS1_R2 BIT(3) |
| 73 | #define DS_BUTTONS1_CREATE BIT(4) |
| 74 | #define DS_BUTTONS1_OPTIONS BIT(5) |
| 75 | #define DS_BUTTONS1_L3 BIT(6) |
| 76 | #define DS_BUTTONS1_R3 BIT(7) |
| 77 | #define DS_BUTTONS2_PS_HOME BIT(0) |
| 78 | #define DS_BUTTONS2_TOUCHPAD BIT(1) |
| 79 | |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 80 | /* Status field of DualSense input report. */ |
| 81 | #define DS_STATUS_BATTERY_CAPACITY GENMASK(3, 0) |
| 82 | #define DS_STATUS_CHARGING GENMASK(7, 4) |
| 83 | #define DS_STATUS_CHARGING_SHIFT 4 |
| 84 | |
Roderick Colenbrander | f6bb05f | 2021-02-07 13:48:59 -0800 | [diff] [blame] | 85 | /* |
| 86 | * Status of a DualSense touch point contact. |
| 87 | * Contact IDs, with highest bit set are 'inactive' |
| 88 | * and any associated data is then invalid. |
| 89 | */ |
| 90 | #define DS_TOUCH_POINT_INACTIVE BIT(7) |
| 91 | |
| 92 | /* DualSense hardware limits */ |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 93 | #define DS_ACC_RES_PER_G 8192 |
| 94 | #define DS_ACC_RANGE (4*DS_ACC_RES_PER_G) |
| 95 | #define DS_GYRO_RES_PER_DEG_S 1024 |
| 96 | #define DS_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S) |
Roderick Colenbrander | f6bb05f | 2021-02-07 13:48:59 -0800 | [diff] [blame] | 97 | #define DS_TOUCHPAD_WIDTH 1920 |
| 98 | #define DS_TOUCHPAD_HEIGHT 1080 |
| 99 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 100 | struct dualsense { |
| 101 | struct ps_device base; |
| 102 | struct input_dev *gamepad; |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 103 | struct input_dev *sensors; |
Roderick Colenbrander | f6bb05f | 2021-02-07 13:48:59 -0800 | [diff] [blame] | 104 | struct input_dev *touchpad; |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 105 | |
| 106 | /* Calibration data for accelerometer and gyroscope. */ |
| 107 | struct ps_calibration_data accel_calib_data[3]; |
| 108 | struct ps_calibration_data gyro_calib_data[3]; |
| 109 | |
| 110 | /* Timestamp for sensor data */ |
| 111 | bool sensor_timestamp_initialized; |
| 112 | uint32_t prev_sensor_timestamp; |
| 113 | uint32_t sensor_timestamp_us; |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | struct dualsense_touch_point { |
| 117 | uint8_t contact; |
| 118 | uint8_t x_lo; |
| 119 | uint8_t x_hi:4, y_lo:4; |
| 120 | uint8_t y_hi; |
| 121 | } __packed; |
| 122 | static_assert(sizeof(struct dualsense_touch_point) == 4); |
| 123 | |
| 124 | /* Main DualSense input report excluding any BT/USB specific headers. */ |
| 125 | struct dualsense_input_report { |
| 126 | uint8_t x, y; |
| 127 | uint8_t rx, ry; |
| 128 | uint8_t z, rz; |
| 129 | uint8_t seq_number; |
| 130 | uint8_t buttons[4]; |
| 131 | uint8_t reserved[4]; |
| 132 | |
| 133 | /* Motion sensors */ |
| 134 | __le16 gyro[3]; /* x, y, z */ |
| 135 | __le16 accel[3]; /* x, y, z */ |
| 136 | __le32 sensor_timestamp; |
| 137 | uint8_t reserved2; |
| 138 | |
| 139 | /* Touchpad */ |
| 140 | struct dualsense_touch_point points[2]; |
| 141 | |
| 142 | uint8_t reserved3[12]; |
| 143 | uint8_t status; |
| 144 | uint8_t reserved4[10]; |
| 145 | } __packed; |
| 146 | /* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */ |
| 147 | static_assert(sizeof(struct dualsense_input_report) == DS_INPUT_REPORT_USB_SIZE - 1); |
| 148 | |
| 149 | /* |
| 150 | * Common gamepad buttons across DualShock 3 / 4 and DualSense. |
| 151 | * Note: for device with a touchpad, touchpad button is not included |
| 152 | * as it will be part of the touchpad device. |
| 153 | */ |
| 154 | static const int ps_gamepad_buttons[] = { |
| 155 | BTN_WEST, /* Square */ |
| 156 | BTN_NORTH, /* Triangle */ |
| 157 | BTN_EAST, /* Circle */ |
| 158 | BTN_SOUTH, /* Cross */ |
| 159 | BTN_TL, /* L1 */ |
| 160 | BTN_TR, /* R1 */ |
| 161 | BTN_TL2, /* L2 */ |
| 162 | BTN_TR2, /* R2 */ |
| 163 | BTN_SELECT, /* Create (PS5) / Share (PS4) */ |
| 164 | BTN_START, /* Option */ |
| 165 | BTN_THUMBL, /* L3 */ |
| 166 | BTN_THUMBR, /* R3 */ |
| 167 | BTN_MODE, /* PS Home */ |
| 168 | }; |
| 169 | |
| 170 | static const struct {int x; int y; } ps_gamepad_hat_mapping[] = { |
| 171 | {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, |
| 172 | {0, 0}, |
| 173 | }; |
| 174 | |
Roderick Colenbrander | 53f04e8 | 2021-02-07 13:49:01 -0800 | [diff] [blame] | 175 | /* |
| 176 | * Add a new ps_device to ps_devices if it doesn't exist. |
| 177 | * Return error on duplicate device, which can happen if the same |
| 178 | * device is connected using both Bluetooth and USB. |
| 179 | */ |
| 180 | static int ps_devices_list_add(struct ps_device *dev) |
| 181 | { |
| 182 | struct ps_device *entry; |
| 183 | |
| 184 | mutex_lock(&ps_devices_lock); |
| 185 | list_for_each_entry(entry, &ps_devices_list, list) { |
| 186 | if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) { |
| 187 | hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n", |
| 188 | dev->mac_address); |
| 189 | mutex_unlock(&ps_devices_lock); |
| 190 | return -EEXIST; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | list_add_tail(&dev->list, &ps_devices_list); |
| 195 | mutex_unlock(&ps_devices_lock); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int ps_devices_list_remove(struct ps_device *dev) |
| 200 | { |
| 201 | mutex_lock(&ps_devices_lock); |
| 202 | list_del(&dev->list); |
| 203 | mutex_unlock(&ps_devices_lock); |
| 204 | return 0; |
| 205 | } |
| 206 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 207 | static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix) |
| 208 | { |
| 209 | struct input_dev *input_dev; |
| 210 | |
| 211 | input_dev = devm_input_allocate_device(&hdev->dev); |
| 212 | if (!input_dev) |
| 213 | return ERR_PTR(-ENOMEM); |
| 214 | |
| 215 | input_dev->id.bustype = hdev->bus; |
| 216 | input_dev->id.vendor = hdev->vendor; |
| 217 | input_dev->id.product = hdev->product; |
| 218 | input_dev->id.version = hdev->version; |
| 219 | input_dev->uniq = hdev->uniq; |
| 220 | |
| 221 | if (name_suffix) { |
| 222 | input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name, |
| 223 | name_suffix); |
| 224 | if (!input_dev->name) |
| 225 | return ERR_PTR(-ENOMEM); |
| 226 | } else { |
| 227 | input_dev->name = hdev->name; |
| 228 | } |
| 229 | |
| 230 | input_set_drvdata(input_dev, hdev); |
| 231 | |
| 232 | return input_dev; |
| 233 | } |
| 234 | |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 235 | static enum power_supply_property ps_power_supply_props[] = { |
| 236 | POWER_SUPPLY_PROP_STATUS, |
| 237 | POWER_SUPPLY_PROP_PRESENT, |
| 238 | POWER_SUPPLY_PROP_CAPACITY, |
| 239 | POWER_SUPPLY_PROP_SCOPE, |
| 240 | }; |
| 241 | |
| 242 | static int ps_battery_get_property(struct power_supply *psy, |
| 243 | enum power_supply_property psp, |
| 244 | union power_supply_propval *val) |
| 245 | { |
| 246 | struct ps_device *dev = power_supply_get_drvdata(psy); |
| 247 | uint8_t battery_capacity; |
| 248 | int battery_status; |
| 249 | unsigned long flags; |
| 250 | int ret; |
| 251 | |
| 252 | spin_lock_irqsave(&dev->lock, flags); |
| 253 | battery_capacity = dev->battery_capacity; |
| 254 | battery_status = dev->battery_status; |
| 255 | spin_unlock_irqrestore(&dev->lock, flags); |
| 256 | |
| 257 | switch (psp) { |
| 258 | case POWER_SUPPLY_PROP_STATUS: |
| 259 | val->intval = battery_status; |
| 260 | break; |
| 261 | case POWER_SUPPLY_PROP_PRESENT: |
| 262 | val->intval = 1; |
| 263 | break; |
| 264 | case POWER_SUPPLY_PROP_CAPACITY: |
| 265 | val->intval = battery_capacity; |
| 266 | break; |
| 267 | case POWER_SUPPLY_PROP_SCOPE: |
| 268 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; |
| 269 | break; |
| 270 | default: |
| 271 | ret = -EINVAL; |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int ps_device_register_battery(struct ps_device *dev) |
| 279 | { |
| 280 | struct power_supply *battery; |
| 281 | struct power_supply_config battery_cfg = { .drv_data = dev }; |
| 282 | int ret; |
| 283 | |
| 284 | dev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; |
| 285 | dev->battery_desc.properties = ps_power_supply_props; |
| 286 | dev->battery_desc.num_properties = ARRAY_SIZE(ps_power_supply_props); |
| 287 | dev->battery_desc.get_property = ps_battery_get_property; |
| 288 | dev->battery_desc.name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL, |
| 289 | "ps-controller-battery-%pMR", dev->mac_address); |
| 290 | if (!dev->battery_desc.name) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | battery = devm_power_supply_register(&dev->hdev->dev, &dev->battery_desc, &battery_cfg); |
| 294 | if (IS_ERR(battery)) { |
| 295 | ret = PTR_ERR(battery); |
| 296 | hid_err(dev->hdev, "Unable to register battery device: %d\n", ret); |
| 297 | return ret; |
| 298 | } |
| 299 | dev->battery = battery; |
| 300 | |
| 301 | ret = power_supply_powers(dev->battery, &dev->hdev->dev); |
| 302 | if (ret) { |
| 303 | hid_err(dev->hdev, "Unable to activate battery device: %d\n", ret); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
Roderick Colenbrander | 799b2b5 | 2021-02-07 13:49:02 -0800 | [diff] [blame^] | 310 | /* Compute crc32 of HID data and compare against expected CRC. */ |
| 311 | static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc) |
| 312 | { |
| 313 | uint32_t crc; |
| 314 | |
| 315 | crc = crc32_le(0xFFFFFFFF, &seed, 1); |
| 316 | crc = ~crc32_le(crc, data, len); |
| 317 | |
| 318 | return crc == report_crc; |
| 319 | } |
| 320 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 321 | static struct input_dev *ps_gamepad_create(struct hid_device *hdev) |
| 322 | { |
| 323 | struct input_dev *gamepad; |
| 324 | unsigned int i; |
| 325 | int ret; |
| 326 | |
| 327 | gamepad = ps_allocate_input_dev(hdev, NULL); |
| 328 | if (IS_ERR(gamepad)) |
| 329 | return ERR_CAST(gamepad); |
| 330 | |
| 331 | input_set_abs_params(gamepad, ABS_X, 0, 255, 0, 0); |
| 332 | input_set_abs_params(gamepad, ABS_Y, 0, 255, 0, 0); |
| 333 | input_set_abs_params(gamepad, ABS_Z, 0, 255, 0, 0); |
| 334 | input_set_abs_params(gamepad, ABS_RX, 0, 255, 0, 0); |
| 335 | input_set_abs_params(gamepad, ABS_RY, 0, 255, 0, 0); |
| 336 | input_set_abs_params(gamepad, ABS_RZ, 0, 255, 0, 0); |
| 337 | |
| 338 | input_set_abs_params(gamepad, ABS_HAT0X, -1, 1, 0, 0); |
| 339 | input_set_abs_params(gamepad, ABS_HAT0Y, -1, 1, 0, 0); |
| 340 | |
| 341 | for (i = 0; i < ARRAY_SIZE(ps_gamepad_buttons); i++) |
| 342 | input_set_capability(gamepad, EV_KEY, ps_gamepad_buttons[i]); |
| 343 | |
| 344 | ret = input_register_device(gamepad); |
| 345 | if (ret) |
| 346 | return ERR_PTR(ret); |
| 347 | |
| 348 | return gamepad; |
| 349 | } |
| 350 | |
Roderick Colenbrander | b99dcef | 2021-02-07 13:48:57 -0800 | [diff] [blame] | 351 | static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *buf, size_t size) |
| 352 | { |
| 353 | int ret; |
| 354 | |
| 355 | ret = hid_hw_raw_request(hdev, report_id, buf, size, HID_FEATURE_REPORT, |
| 356 | HID_REQ_GET_REPORT); |
| 357 | if (ret < 0) { |
| 358 | hid_err(hdev, "Failed to retrieve feature with reportID %d: %d\n", report_id, ret); |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | if (ret != size) { |
| 363 | hid_err(hdev, "Invalid byte count transferred, expected %zu got %d\n", size, ret); |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
| 367 | if (buf[0] != report_id) { |
| 368 | hid_err(hdev, "Invalid reportID received, expected %d got %d\n", report_id, buf[0]); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | |
Roderick Colenbrander | 799b2b5 | 2021-02-07 13:49:02 -0800 | [diff] [blame^] | 372 | if (hdev->bus == BUS_BLUETOOTH) { |
| 373 | /* Last 4 bytes contains crc32. */ |
| 374 | uint8_t crc_offset = size - 4; |
| 375 | uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]); |
| 376 | |
| 377 | if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) { |
| 378 | hid_err(hdev, "CRC check failed for reportID=%d\n", report_id); |
| 379 | return -EILSEQ; |
| 380 | } |
| 381 | } |
| 382 | |
Roderick Colenbrander | b99dcef | 2021-02-07 13:48:57 -0800 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 386 | static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res, |
| 387 | int gyro_range, int gyro_res) |
| 388 | { |
| 389 | struct input_dev *sensors; |
| 390 | int ret; |
| 391 | |
| 392 | sensors = ps_allocate_input_dev(hdev, "Motion Sensors"); |
| 393 | if (IS_ERR(sensors)) |
| 394 | return ERR_CAST(sensors); |
| 395 | |
| 396 | __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit); |
| 397 | __set_bit(EV_MSC, sensors->evbit); |
| 398 | __set_bit(MSC_TIMESTAMP, sensors->mscbit); |
| 399 | |
| 400 | /* Accelerometer */ |
| 401 | input_set_abs_params(sensors, ABS_X, -accel_range, accel_range, 16, 0); |
| 402 | input_set_abs_params(sensors, ABS_Y, -accel_range, accel_range, 16, 0); |
| 403 | input_set_abs_params(sensors, ABS_Z, -accel_range, accel_range, 16, 0); |
| 404 | input_abs_set_res(sensors, ABS_X, accel_res); |
| 405 | input_abs_set_res(sensors, ABS_Y, accel_res); |
| 406 | input_abs_set_res(sensors, ABS_Z, accel_res); |
| 407 | |
| 408 | /* Gyroscope */ |
| 409 | input_set_abs_params(sensors, ABS_RX, -gyro_range, gyro_range, 16, 0); |
| 410 | input_set_abs_params(sensors, ABS_RY, -gyro_range, gyro_range, 16, 0); |
| 411 | input_set_abs_params(sensors, ABS_RZ, -gyro_range, gyro_range, 16, 0); |
| 412 | input_abs_set_res(sensors, ABS_RX, gyro_res); |
| 413 | input_abs_set_res(sensors, ABS_RY, gyro_res); |
| 414 | input_abs_set_res(sensors, ABS_RZ, gyro_res); |
| 415 | |
| 416 | ret = input_register_device(sensors); |
| 417 | if (ret) |
| 418 | return ERR_PTR(ret); |
| 419 | |
| 420 | return sensors; |
| 421 | } |
| 422 | |
Roderick Colenbrander | f6bb05f | 2021-02-07 13:48:59 -0800 | [diff] [blame] | 423 | static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, int height, |
| 424 | unsigned int num_contacts) |
| 425 | { |
| 426 | struct input_dev *touchpad; |
| 427 | int ret; |
| 428 | |
| 429 | touchpad = ps_allocate_input_dev(hdev, "Touchpad"); |
| 430 | if (IS_ERR(touchpad)) |
| 431 | return ERR_CAST(touchpad); |
| 432 | |
| 433 | /* Map button underneath touchpad to BTN_LEFT. */ |
| 434 | input_set_capability(touchpad, EV_KEY, BTN_LEFT); |
| 435 | __set_bit(INPUT_PROP_BUTTONPAD, touchpad->propbit); |
| 436 | |
| 437 | input_set_abs_params(touchpad, ABS_MT_POSITION_X, 0, width - 1, 0, 0); |
| 438 | input_set_abs_params(touchpad, ABS_MT_POSITION_Y, 0, height - 1, 0, 0); |
| 439 | |
| 440 | ret = input_mt_init_slots(touchpad, num_contacts, INPUT_MT_POINTER); |
| 441 | if (ret) |
| 442 | return ERR_PTR(ret); |
| 443 | |
| 444 | ret = input_register_device(touchpad); |
| 445 | if (ret) |
| 446 | return ERR_PTR(ret); |
| 447 | |
| 448 | return touchpad; |
| 449 | } |
| 450 | |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 451 | static int dualsense_get_calibration_data(struct dualsense *ds) |
| 452 | { |
| 453 | short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus; |
| 454 | short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus; |
| 455 | short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus; |
| 456 | short gyro_speed_plus, gyro_speed_minus; |
| 457 | short acc_x_plus, acc_x_minus; |
| 458 | short acc_y_plus, acc_y_minus; |
| 459 | short acc_z_plus, acc_z_minus; |
| 460 | int speed_2x; |
| 461 | int range_2g; |
| 462 | int ret = 0; |
| 463 | uint8_t *buf; |
| 464 | |
| 465 | buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL); |
| 466 | if (!buf) |
| 467 | return -ENOMEM; |
| 468 | |
| 469 | ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_CALIBRATION, buf, |
| 470 | DS_FEATURE_REPORT_CALIBRATION_SIZE); |
| 471 | if (ret) { |
| 472 | hid_err(ds->base.hdev, "Failed to retrieve DualSense calibration info: %d\n", ret); |
| 473 | goto err_free; |
| 474 | } |
| 475 | |
| 476 | gyro_pitch_bias = get_unaligned_le16(&buf[1]); |
| 477 | gyro_yaw_bias = get_unaligned_le16(&buf[3]); |
| 478 | gyro_roll_bias = get_unaligned_le16(&buf[5]); |
| 479 | gyro_pitch_plus = get_unaligned_le16(&buf[7]); |
| 480 | gyro_pitch_minus = get_unaligned_le16(&buf[9]); |
| 481 | gyro_yaw_plus = get_unaligned_le16(&buf[11]); |
| 482 | gyro_yaw_minus = get_unaligned_le16(&buf[13]); |
| 483 | gyro_roll_plus = get_unaligned_le16(&buf[15]); |
| 484 | gyro_roll_minus = get_unaligned_le16(&buf[17]); |
| 485 | gyro_speed_plus = get_unaligned_le16(&buf[19]); |
| 486 | gyro_speed_minus = get_unaligned_le16(&buf[21]); |
| 487 | acc_x_plus = get_unaligned_le16(&buf[23]); |
| 488 | acc_x_minus = get_unaligned_le16(&buf[25]); |
| 489 | acc_y_plus = get_unaligned_le16(&buf[27]); |
| 490 | acc_y_minus = get_unaligned_le16(&buf[29]); |
| 491 | acc_z_plus = get_unaligned_le16(&buf[31]); |
| 492 | acc_z_minus = get_unaligned_le16(&buf[33]); |
| 493 | |
| 494 | /* |
| 495 | * Set gyroscope calibration and normalization parameters. |
| 496 | * Data values will be normalized to 1/DS_GYRO_RES_PER_DEG_S degree/s. |
| 497 | */ |
| 498 | speed_2x = (gyro_speed_plus + gyro_speed_minus); |
| 499 | ds->gyro_calib_data[0].abs_code = ABS_RX; |
| 500 | ds->gyro_calib_data[0].bias = gyro_pitch_bias; |
| 501 | ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; |
| 502 | ds->gyro_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus; |
| 503 | |
| 504 | ds->gyro_calib_data[1].abs_code = ABS_RY; |
| 505 | ds->gyro_calib_data[1].bias = gyro_yaw_bias; |
| 506 | ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; |
| 507 | ds->gyro_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus; |
| 508 | |
| 509 | ds->gyro_calib_data[2].abs_code = ABS_RZ; |
| 510 | ds->gyro_calib_data[2].bias = gyro_roll_bias; |
| 511 | ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; |
| 512 | ds->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus; |
| 513 | |
| 514 | /* |
| 515 | * Set accelerometer calibration and normalization parameters. |
| 516 | * Data values will be normalized to 1/DS_ACC_RES_PER_G g. |
| 517 | */ |
| 518 | range_2g = acc_x_plus - acc_x_minus; |
| 519 | ds->accel_calib_data[0].abs_code = ABS_X; |
| 520 | ds->accel_calib_data[0].bias = acc_x_plus - range_2g / 2; |
| 521 | ds->accel_calib_data[0].sens_numer = 2*DS_ACC_RES_PER_G; |
| 522 | ds->accel_calib_data[0].sens_denom = range_2g; |
| 523 | |
| 524 | range_2g = acc_y_plus - acc_y_minus; |
| 525 | ds->accel_calib_data[1].abs_code = ABS_Y; |
| 526 | ds->accel_calib_data[1].bias = acc_y_plus - range_2g / 2; |
| 527 | ds->accel_calib_data[1].sens_numer = 2*DS_ACC_RES_PER_G; |
| 528 | ds->accel_calib_data[1].sens_denom = range_2g; |
| 529 | |
| 530 | range_2g = acc_z_plus - acc_z_minus; |
| 531 | ds->accel_calib_data[2].abs_code = ABS_Z; |
| 532 | ds->accel_calib_data[2].bias = acc_z_plus - range_2g / 2; |
| 533 | ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G; |
| 534 | ds->accel_calib_data[2].sens_denom = range_2g; |
| 535 | |
| 536 | err_free: |
| 537 | kfree(buf); |
| 538 | return ret; |
| 539 | } |
| 540 | |
Roderick Colenbrander | b99dcef | 2021-02-07 13:48:57 -0800 | [diff] [blame] | 541 | static int dualsense_get_mac_address(struct dualsense *ds) |
| 542 | { |
| 543 | uint8_t *buf; |
| 544 | int ret = 0; |
| 545 | |
| 546 | buf = kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL); |
| 547 | if (!buf) |
| 548 | return -ENOMEM; |
| 549 | |
| 550 | ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_PAIRING_INFO, buf, |
| 551 | DS_FEATURE_REPORT_PAIRING_INFO_SIZE); |
| 552 | if (ret) { |
| 553 | hid_err(ds->base.hdev, "Failed to retrieve DualSense pairing info: %d\n", ret); |
| 554 | goto err_free; |
| 555 | } |
| 556 | |
| 557 | memcpy(ds->base.mac_address, &buf[1], sizeof(ds->base.mac_address)); |
| 558 | |
| 559 | err_free: |
| 560 | kfree(buf); |
| 561 | return ret; |
| 562 | } |
| 563 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 564 | static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *report, |
| 565 | u8 *data, int size) |
| 566 | { |
| 567 | struct hid_device *hdev = ps_dev->hdev; |
| 568 | struct dualsense *ds = container_of(ps_dev, struct dualsense, base); |
| 569 | struct dualsense_input_report *ds_report; |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 570 | uint8_t battery_data, battery_capacity, charging_status, value; |
| 571 | int battery_status; |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 572 | uint32_t sensor_timestamp; |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 573 | unsigned long flags; |
Roderick Colenbrander | f6bb05f | 2021-02-07 13:48:59 -0800 | [diff] [blame] | 574 | int i; |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * DualSense in USB uses the full HID report for reportID 1, but |
| 578 | * Bluetooth uses a minimal HID report for reportID 1 and reports |
| 579 | * the full report using reportID 49. |
| 580 | */ |
| 581 | if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB && |
| 582 | size == DS_INPUT_REPORT_USB_SIZE) { |
| 583 | ds_report = (struct dualsense_input_report *)&data[1]; |
Roderick Colenbrander | 799b2b5 | 2021-02-07 13:49:02 -0800 | [diff] [blame^] | 584 | } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT && |
| 585 | size == DS_INPUT_REPORT_BT_SIZE) { |
| 586 | /* Last 4 bytes of input report contain crc32 */ |
| 587 | uint32_t report_crc = get_unaligned_le32(&data[size - 4]); |
| 588 | |
| 589 | if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) { |
| 590 | hid_err(hdev, "DualSense input CRC's check failed\n"); |
| 591 | return -EILSEQ; |
| 592 | } |
| 593 | |
| 594 | ds_report = (struct dualsense_input_report *)&data[2]; |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 595 | } else { |
| 596 | hid_err(hdev, "Unhandled reportID=%d\n", report->id); |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | input_report_abs(ds->gamepad, ABS_X, ds_report->x); |
| 601 | input_report_abs(ds->gamepad, ABS_Y, ds_report->y); |
| 602 | input_report_abs(ds->gamepad, ABS_RX, ds_report->rx); |
| 603 | input_report_abs(ds->gamepad, ABS_RY, ds_report->ry); |
| 604 | input_report_abs(ds->gamepad, ABS_Z, ds_report->z); |
| 605 | input_report_abs(ds->gamepad, ABS_RZ, ds_report->rz); |
| 606 | |
| 607 | value = ds_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH; |
| 608 | if (value > ARRAY_SIZE(ps_gamepad_hat_mapping)) |
| 609 | value = 8; /* center */ |
| 610 | input_report_abs(ds->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x); |
| 611 | input_report_abs(ds->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y); |
| 612 | |
| 613 | input_report_key(ds->gamepad, BTN_WEST, ds_report->buttons[0] & DS_BUTTONS0_SQUARE); |
| 614 | input_report_key(ds->gamepad, BTN_SOUTH, ds_report->buttons[0] & DS_BUTTONS0_CROSS); |
| 615 | input_report_key(ds->gamepad, BTN_EAST, ds_report->buttons[0] & DS_BUTTONS0_CIRCLE); |
| 616 | input_report_key(ds->gamepad, BTN_NORTH, ds_report->buttons[0] & DS_BUTTONS0_TRIANGLE); |
| 617 | input_report_key(ds->gamepad, BTN_TL, ds_report->buttons[1] & DS_BUTTONS1_L1); |
| 618 | input_report_key(ds->gamepad, BTN_TR, ds_report->buttons[1] & DS_BUTTONS1_R1); |
| 619 | input_report_key(ds->gamepad, BTN_TL2, ds_report->buttons[1] & DS_BUTTONS1_L2); |
| 620 | input_report_key(ds->gamepad, BTN_TR2, ds_report->buttons[1] & DS_BUTTONS1_R2); |
| 621 | input_report_key(ds->gamepad, BTN_SELECT, ds_report->buttons[1] & DS_BUTTONS1_CREATE); |
| 622 | input_report_key(ds->gamepad, BTN_START, ds_report->buttons[1] & DS_BUTTONS1_OPTIONS); |
| 623 | input_report_key(ds->gamepad, BTN_THUMBL, ds_report->buttons[1] & DS_BUTTONS1_L3); |
| 624 | input_report_key(ds->gamepad, BTN_THUMBR, ds_report->buttons[1] & DS_BUTTONS1_R3); |
| 625 | input_report_key(ds->gamepad, BTN_MODE, ds_report->buttons[2] & DS_BUTTONS2_PS_HOME); |
| 626 | input_sync(ds->gamepad); |
| 627 | |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 628 | /* Parse and calibrate gyroscope data. */ |
| 629 | for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) { |
| 630 | int raw_data = (short)le16_to_cpu(ds_report->gyro[i]); |
| 631 | int calib_data = mult_frac(ds->gyro_calib_data[i].sens_numer, |
| 632 | raw_data - ds->gyro_calib_data[i].bias, |
| 633 | ds->gyro_calib_data[i].sens_denom); |
| 634 | |
| 635 | input_report_abs(ds->sensors, ds->gyro_calib_data[i].abs_code, calib_data); |
| 636 | } |
| 637 | |
| 638 | /* Parse and calibrate accelerometer data. */ |
| 639 | for (i = 0; i < ARRAY_SIZE(ds_report->accel); i++) { |
| 640 | int raw_data = (short)le16_to_cpu(ds_report->accel[i]); |
| 641 | int calib_data = mult_frac(ds->accel_calib_data[i].sens_numer, |
| 642 | raw_data - ds->accel_calib_data[i].bias, |
| 643 | ds->accel_calib_data[i].sens_denom); |
| 644 | |
| 645 | input_report_abs(ds->sensors, ds->accel_calib_data[i].abs_code, calib_data); |
| 646 | } |
| 647 | |
| 648 | /* Convert timestamp (in 0.33us unit) to timestamp_us */ |
| 649 | sensor_timestamp = le32_to_cpu(ds_report->sensor_timestamp); |
| 650 | if (!ds->sensor_timestamp_initialized) { |
| 651 | ds->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp, 3); |
| 652 | ds->sensor_timestamp_initialized = true; |
| 653 | } else { |
| 654 | uint32_t delta; |
| 655 | |
| 656 | if (ds->prev_sensor_timestamp > sensor_timestamp) |
| 657 | delta = (U32_MAX - ds->prev_sensor_timestamp + sensor_timestamp + 1); |
| 658 | else |
| 659 | delta = sensor_timestamp - ds->prev_sensor_timestamp; |
| 660 | ds->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta, 3); |
| 661 | } |
| 662 | ds->prev_sensor_timestamp = sensor_timestamp; |
| 663 | input_event(ds->sensors, EV_MSC, MSC_TIMESTAMP, ds->sensor_timestamp_us); |
| 664 | input_sync(ds->sensors); |
| 665 | |
Roderick Colenbrander | f6bb05f | 2021-02-07 13:48:59 -0800 | [diff] [blame] | 666 | for (i = 0; i < ARRAY_SIZE(ds_report->points); i++) { |
| 667 | struct dualsense_touch_point *point = &ds_report->points[i]; |
| 668 | bool active = (point->contact & DS_TOUCH_POINT_INACTIVE) ? false : true; |
| 669 | |
| 670 | input_mt_slot(ds->touchpad, i); |
| 671 | input_mt_report_slot_state(ds->touchpad, MT_TOOL_FINGER, active); |
| 672 | |
| 673 | if (active) { |
| 674 | int x = (point->x_hi << 8) | point->x_lo; |
| 675 | int y = (point->y_hi << 4) | point->y_lo; |
| 676 | |
| 677 | input_report_abs(ds->touchpad, ABS_MT_POSITION_X, x); |
| 678 | input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, y); |
| 679 | } |
| 680 | } |
| 681 | input_mt_sync_frame(ds->touchpad); |
| 682 | input_report_key(ds->touchpad, BTN_LEFT, ds_report->buttons[2] & DS_BUTTONS2_TOUCHPAD); |
| 683 | input_sync(ds->touchpad); |
| 684 | |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 685 | battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY; |
| 686 | charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT; |
| 687 | |
| 688 | switch (charging_status) { |
| 689 | case 0x0: |
| 690 | /* |
| 691 | * Each unit of battery data corresponds to 10% |
| 692 | * 0 = 0-9%, 1 = 10-19%, .. and 10 = 100% |
| 693 | */ |
| 694 | battery_capacity = min(battery_data * 10 + 5, 100); |
| 695 | battery_status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 696 | break; |
| 697 | case 0x1: |
| 698 | battery_capacity = min(battery_data * 10 + 5, 100); |
| 699 | battery_status = POWER_SUPPLY_STATUS_CHARGING; |
| 700 | break; |
| 701 | case 0x2: |
| 702 | battery_capacity = 100; |
| 703 | battery_status = POWER_SUPPLY_STATUS_FULL; |
| 704 | break; |
| 705 | case 0xa: /* voltage or temperature out of range */ |
| 706 | case 0xb: /* temperature error */ |
| 707 | battery_capacity = 0; |
| 708 | battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 709 | break; |
| 710 | case 0xf: /* charging error */ |
| 711 | default: |
| 712 | battery_capacity = 0; |
| 713 | battery_status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 714 | } |
| 715 | |
| 716 | spin_lock_irqsave(&ps_dev->lock, flags); |
| 717 | ps_dev->battery_capacity = battery_capacity; |
| 718 | ps_dev->battery_status = battery_status; |
| 719 | spin_unlock_irqrestore(&ps_dev->lock, flags); |
| 720 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static struct ps_device *dualsense_create(struct hid_device *hdev) |
| 725 | { |
| 726 | struct dualsense *ds; |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 727 | struct ps_device *ps_dev; |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 728 | int ret; |
| 729 | |
| 730 | ds = devm_kzalloc(&hdev->dev, sizeof(*ds), GFP_KERNEL); |
| 731 | if (!ds) |
| 732 | return ERR_PTR(-ENOMEM); |
| 733 | |
| 734 | /* |
| 735 | * Patch version to allow userspace to distinguish between |
| 736 | * hid-generic vs hid-playstation axis and button mapping. |
| 737 | */ |
| 738 | hdev->version |= HID_PLAYSTATION_VERSION_PATCH; |
| 739 | |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 740 | ps_dev = &ds->base; |
| 741 | ps_dev->hdev = hdev; |
| 742 | spin_lock_init(&ps_dev->lock); |
| 743 | ps_dev->battery_capacity = 100; /* initial value until parse_report. */ |
| 744 | ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 745 | ps_dev->parse_report = dualsense_parse_report; |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 746 | hid_set_drvdata(hdev, ds); |
| 747 | |
Roderick Colenbrander | b99dcef | 2021-02-07 13:48:57 -0800 | [diff] [blame] | 748 | ret = dualsense_get_mac_address(ds); |
| 749 | if (ret) { |
| 750 | hid_err(hdev, "Failed to get MAC address from DualSense\n"); |
| 751 | return ERR_PTR(ret); |
| 752 | } |
| 753 | snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address); |
| 754 | |
Roderick Colenbrander | 53f04e8 | 2021-02-07 13:49:01 -0800 | [diff] [blame] | 755 | ret = ps_devices_list_add(ps_dev); |
| 756 | if (ret) |
| 757 | return ERR_PTR(ret); |
| 758 | |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 759 | ret = dualsense_get_calibration_data(ds); |
| 760 | if (ret) { |
| 761 | hid_err(hdev, "Failed to get calibration data from DualSense\n"); |
| 762 | goto err; |
| 763 | } |
| 764 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 765 | ds->gamepad = ps_gamepad_create(hdev); |
| 766 | if (IS_ERR(ds->gamepad)) { |
| 767 | ret = PTR_ERR(ds->gamepad); |
| 768 | goto err; |
| 769 | } |
| 770 | |
Roderick Colenbrander | 402987c | 2021-02-07 13:49:00 -0800 | [diff] [blame] | 771 | ds->sensors = ps_sensors_create(hdev, DS_ACC_RANGE, DS_ACC_RES_PER_G, |
| 772 | DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S); |
| 773 | if (IS_ERR(ds->sensors)) { |
| 774 | ret = PTR_ERR(ds->sensors); |
| 775 | goto err; |
| 776 | } |
| 777 | |
Roderick Colenbrander | f6bb05f | 2021-02-07 13:48:59 -0800 | [diff] [blame] | 778 | ds->touchpad = ps_touchpad_create(hdev, DS_TOUCHPAD_WIDTH, DS_TOUCHPAD_HEIGHT, 2); |
| 779 | if (IS_ERR(ds->touchpad)) { |
| 780 | ret = PTR_ERR(ds->touchpad); |
| 781 | goto err; |
| 782 | } |
| 783 | |
Roderick Colenbrander | d30bca4 | 2021-02-07 13:48:58 -0800 | [diff] [blame] | 784 | ret = ps_device_register_battery(ps_dev); |
| 785 | if (ret) |
| 786 | goto err; |
| 787 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 788 | return &ds->base; |
| 789 | |
| 790 | err: |
Roderick Colenbrander | 53f04e8 | 2021-02-07 13:49:01 -0800 | [diff] [blame] | 791 | ps_devices_list_remove(ps_dev); |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 792 | return ERR_PTR(ret); |
| 793 | } |
| 794 | |
| 795 | static int ps_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 796 | u8 *data, int size) |
| 797 | { |
| 798 | struct ps_device *dev = hid_get_drvdata(hdev); |
| 799 | |
| 800 | if (dev && dev->parse_report) |
| 801 | return dev->parse_report(dev, report, data, size); |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 807 | { |
| 808 | struct ps_device *dev; |
| 809 | int ret; |
| 810 | |
| 811 | ret = hid_parse(hdev); |
| 812 | if (ret) { |
| 813 | hid_err(hdev, "Parse failed\n"); |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 818 | if (ret) { |
| 819 | hid_err(hdev, "Failed to start HID device\n"); |
| 820 | return ret; |
| 821 | } |
| 822 | |
| 823 | ret = hid_hw_open(hdev); |
| 824 | if (ret) { |
| 825 | hid_err(hdev, "Failed to open HID device\n"); |
| 826 | goto err_stop; |
| 827 | } |
| 828 | |
| 829 | if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER) { |
| 830 | dev = dualsense_create(hdev); |
| 831 | if (IS_ERR(dev)) { |
| 832 | hid_err(hdev, "Failed to create dualsense.\n"); |
| 833 | ret = PTR_ERR(dev); |
| 834 | goto err_close; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | return ret; |
| 839 | |
| 840 | err_close: |
| 841 | hid_hw_close(hdev); |
| 842 | err_stop: |
| 843 | hid_hw_stop(hdev); |
| 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | static void ps_remove(struct hid_device *hdev) |
| 848 | { |
Roderick Colenbrander | 53f04e8 | 2021-02-07 13:49:01 -0800 | [diff] [blame] | 849 | struct ps_device *dev = hid_get_drvdata(hdev); |
| 850 | |
| 851 | ps_devices_list_remove(dev); |
| 852 | |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 853 | hid_hw_close(hdev); |
| 854 | hid_hw_stop(hdev); |
| 855 | } |
| 856 | |
| 857 | static const struct hid_device_id ps_devices[] = { |
Roderick Colenbrander | 799b2b5 | 2021-02-07 13:49:02 -0800 | [diff] [blame^] | 858 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) }, |
Roderick Colenbrander | bc2e15a | 2021-02-07 13:48:56 -0800 | [diff] [blame] | 859 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) }, |
| 860 | { } |
| 861 | }; |
| 862 | MODULE_DEVICE_TABLE(hid, ps_devices); |
| 863 | |
| 864 | static struct hid_driver ps_driver = { |
| 865 | .name = "playstation", |
| 866 | .id_table = ps_devices, |
| 867 | .probe = ps_probe, |
| 868 | .remove = ps_remove, |
| 869 | .raw_event = ps_raw_event, |
| 870 | }; |
| 871 | |
| 872 | module_hid_driver(ps_driver); |
| 873 | |
| 874 | MODULE_AUTHOR("Sony Interactive Entertainment"); |
| 875 | MODULE_DESCRIPTION("HID Driver for PlayStation peripherals."); |
| 876 | MODULE_LICENSE("GPL"); |