blob: 3b6f42155e210c791d95113a3a320e82e3505672 [file] [log] [blame]
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001// 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 Colenbrander799b2b52021-02-07 13:49:02 -08009#include <linux/crc32.h>
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080010#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 Colenbrander53f04e82021-02-07 13:49:01 -080019/* List of connected playstation devices. */
20static DEFINE_MUTEX(ps_devices_lock);
21static LIST_HEAD(ps_devices_list);
22
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080023#define HID_PLAYSTATION_VERSION_PATCH 0x8000
24
25/* Base class for playstation devices. */
26struct ps_device {
Roderick Colenbrander53f04e82021-02-07 13:49:01 -080027 struct list_head list;
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080028 struct hid_device *hdev;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -080029 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 Colenbranderb99dcef2021-02-07 13:48:57 -080036 uint8_t mac_address[6]; /* Note: stored in little endian order. */
Roderick Colenbrander0b25b552021-02-07 13:49:08 -080037 uint32_t hw_version;
38 uint32_t fw_version;
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080039
40 int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size);
41};
42
Roderick Colenbrander402987c2021-02-07 13:49:00 -080043/* Calibration data for playstation motion sensors. */
44struct ps_calibration_data {
45 int abs_code;
46 short bias;
47 int sens_numer;
48 int sens_denom;
49};
50
Roderick Colenbrander799b2b52021-02-07 13:49:02 -080051/* Seed values for DualShock4 / DualSense CRC32 for different report types. */
52#define PS_INPUT_CRC32_SEED 0xA1
Roderick Colenbrander51151092021-02-07 13:49:03 -080053#define PS_OUTPUT_CRC32_SEED 0xA2
Roderick Colenbrander799b2b52021-02-07 13:49:02 -080054#define PS_FEATURE_CRC32_SEED 0xA3
55
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080056#define DS_INPUT_REPORT_USB 0x01
57#define DS_INPUT_REPORT_USB_SIZE 64
Roderick Colenbrander799b2b52021-02-07 13:49:02 -080058#define DS_INPUT_REPORT_BT 0x31
59#define DS_INPUT_REPORT_BT_SIZE 78
Roderick Colenbrander51151092021-02-07 13:49:03 -080060#define DS_OUTPUT_REPORT_USB 0x02
61#define DS_OUTPUT_REPORT_USB_SIZE 63
62#define DS_OUTPUT_REPORT_BT 0x31
63#define DS_OUTPUT_REPORT_BT_SIZE 78
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080064
Roderick Colenbrander402987c2021-02-07 13:49:00 -080065#define DS_FEATURE_REPORT_CALIBRATION 0x05
66#define DS_FEATURE_REPORT_CALIBRATION_SIZE 41
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -080067#define DS_FEATURE_REPORT_PAIRING_INFO 0x09
68#define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20
Roderick Colenbrander0b25b552021-02-07 13:49:08 -080069#define DS_FEATURE_REPORT_FIRMWARE_INFO 0x20
70#define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE 64
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -080071
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080072/* Button masks for DualSense input report. */
73#define DS_BUTTONS0_HAT_SWITCH GENMASK(3, 0)
74#define DS_BUTTONS0_SQUARE BIT(4)
75#define DS_BUTTONS0_CROSS BIT(5)
76#define DS_BUTTONS0_CIRCLE BIT(6)
77#define DS_BUTTONS0_TRIANGLE BIT(7)
78#define DS_BUTTONS1_L1 BIT(0)
79#define DS_BUTTONS1_R1 BIT(1)
80#define DS_BUTTONS1_L2 BIT(2)
81#define DS_BUTTONS1_R2 BIT(3)
82#define DS_BUTTONS1_CREATE BIT(4)
83#define DS_BUTTONS1_OPTIONS BIT(5)
84#define DS_BUTTONS1_L3 BIT(6)
85#define DS_BUTTONS1_R3 BIT(7)
86#define DS_BUTTONS2_PS_HOME BIT(0)
87#define DS_BUTTONS2_TOUCHPAD BIT(1)
88
Roderick Colenbranderd30bca42021-02-07 13:48:58 -080089/* Status field of DualSense input report. */
90#define DS_STATUS_BATTERY_CAPACITY GENMASK(3, 0)
91#define DS_STATUS_CHARGING GENMASK(7, 4)
92#define DS_STATUS_CHARGING_SHIFT 4
93
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -080094/*
95 * Status of a DualSense touch point contact.
96 * Contact IDs, with highest bit set are 'inactive'
97 * and any associated data is then invalid.
98 */
99#define DS_TOUCH_POINT_INACTIVE BIT(7)
100
Roderick Colenbrander51151092021-02-07 13:49:03 -0800101 /* Magic value required in tag field of Bluetooth output report. */
102#define DS_OUTPUT_TAG 0x10
103/* Flags for DualSense output report. */
104#define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0)
105#define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1)
106
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800107/* DualSense hardware limits */
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800108#define DS_ACC_RES_PER_G 8192
109#define DS_ACC_RANGE (4*DS_ACC_RES_PER_G)
110#define DS_GYRO_RES_PER_DEG_S 1024
111#define DS_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S)
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800112#define DS_TOUCHPAD_WIDTH 1920
113#define DS_TOUCHPAD_HEIGHT 1080
114
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800115struct dualsense {
116 struct ps_device base;
117 struct input_dev *gamepad;
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800118 struct input_dev *sensors;
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800119 struct input_dev *touchpad;
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800120
121 /* Calibration data for accelerometer and gyroscope. */
122 struct ps_calibration_data accel_calib_data[3];
123 struct ps_calibration_data gyro_calib_data[3];
124
125 /* Timestamp for sensor data */
126 bool sensor_timestamp_initialized;
127 uint32_t prev_sensor_timestamp;
128 uint32_t sensor_timestamp_us;
Roderick Colenbrander51151092021-02-07 13:49:03 -0800129
130 /* Compatible rumble state */
131 bool update_rumble;
132 uint8_t motor_left;
133 uint8_t motor_right;
134
135 struct work_struct output_worker;
136 void *output_report_dmabuf;
137 uint8_t output_seq; /* Sequence number for output report. */
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800138};
139
140struct dualsense_touch_point {
141 uint8_t contact;
142 uint8_t x_lo;
143 uint8_t x_hi:4, y_lo:4;
144 uint8_t y_hi;
145} __packed;
146static_assert(sizeof(struct dualsense_touch_point) == 4);
147
148/* Main DualSense input report excluding any BT/USB specific headers. */
149struct dualsense_input_report {
150 uint8_t x, y;
151 uint8_t rx, ry;
152 uint8_t z, rz;
153 uint8_t seq_number;
154 uint8_t buttons[4];
155 uint8_t reserved[4];
156
157 /* Motion sensors */
158 __le16 gyro[3]; /* x, y, z */
159 __le16 accel[3]; /* x, y, z */
160 __le32 sensor_timestamp;
161 uint8_t reserved2;
162
163 /* Touchpad */
164 struct dualsense_touch_point points[2];
165
166 uint8_t reserved3[12];
167 uint8_t status;
168 uint8_t reserved4[10];
169} __packed;
170/* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */
171static_assert(sizeof(struct dualsense_input_report) == DS_INPUT_REPORT_USB_SIZE - 1);
172
Roderick Colenbrander51151092021-02-07 13:49:03 -0800173/* Common data between DualSense BT/USB main output report. */
174struct dualsense_output_report_common {
175 uint8_t valid_flag0;
176 uint8_t valid_flag1;
177
178 /* For DualShock 4 compatibility mode. */
179 uint8_t motor_right;
180 uint8_t motor_left;
181
182 /* Audio controls */
183 uint8_t reserved[4];
184 uint8_t mute_button_led;
185
186 uint8_t power_save_control;
187 uint8_t reserved2[28];
188
189 /* LEDs and lightbar */
190 uint8_t valid_flag2;
191 uint8_t reserved3[2];
192 uint8_t lightbar_setup;
193 uint8_t led_brightness;
194 uint8_t player_leds;
195 uint8_t lightbar_red;
196 uint8_t lightbar_green;
197 uint8_t lightbar_blue;
198} __packed;
199static_assert(sizeof(struct dualsense_output_report_common) == 47);
200
201struct dualsense_output_report_bt {
202 uint8_t report_id; /* 0x31 */
203 uint8_t seq_tag;
204 uint8_t tag;
205 struct dualsense_output_report_common common;
206 uint8_t reserved[24];
207 __le32 crc32;
208} __packed;
209static_assert(sizeof(struct dualsense_output_report_bt) == DS_OUTPUT_REPORT_BT_SIZE);
210
211struct dualsense_output_report_usb {
212 uint8_t report_id; /* 0x02 */
213 struct dualsense_output_report_common common;
214 uint8_t reserved[15];
215} __packed;
216static_assert(sizeof(struct dualsense_output_report_usb) == DS_OUTPUT_REPORT_USB_SIZE);
217
218/*
219 * The DualSense has a main output report used to control most features. It is
220 * largely the same between Bluetooth and USB except for different headers and CRC.
221 * This structure hide the differences between the two to simplify sending output reports.
222 */
223struct dualsense_output_report {
224 uint8_t *data; /* Start of data */
225 uint8_t len; /* Size of output report */
226
227 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
228 struct dualsense_output_report_bt *bt;
229 /* Points to USB data payload in case for a USB report else NULL. */
230 struct dualsense_output_report_usb *usb;
231 /* Points to common section of report, so past any headers. */
232 struct dualsense_output_report_common *common;
233};
234
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800235/*
236 * Common gamepad buttons across DualShock 3 / 4 and DualSense.
237 * Note: for device with a touchpad, touchpad button is not included
238 * as it will be part of the touchpad device.
239 */
240static const int ps_gamepad_buttons[] = {
241 BTN_WEST, /* Square */
242 BTN_NORTH, /* Triangle */
243 BTN_EAST, /* Circle */
244 BTN_SOUTH, /* Cross */
245 BTN_TL, /* L1 */
246 BTN_TR, /* R1 */
247 BTN_TL2, /* L2 */
248 BTN_TR2, /* R2 */
249 BTN_SELECT, /* Create (PS5) / Share (PS4) */
250 BTN_START, /* Option */
251 BTN_THUMBL, /* L3 */
252 BTN_THUMBR, /* R3 */
253 BTN_MODE, /* PS Home */
254};
255
256static const struct {int x; int y; } ps_gamepad_hat_mapping[] = {
257 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
258 {0, 0},
259};
260
Roderick Colenbrander53f04e82021-02-07 13:49:01 -0800261/*
262 * Add a new ps_device to ps_devices if it doesn't exist.
263 * Return error on duplicate device, which can happen if the same
264 * device is connected using both Bluetooth and USB.
265 */
266static int ps_devices_list_add(struct ps_device *dev)
267{
268 struct ps_device *entry;
269
270 mutex_lock(&ps_devices_lock);
271 list_for_each_entry(entry, &ps_devices_list, list) {
272 if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) {
273 hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n",
274 dev->mac_address);
275 mutex_unlock(&ps_devices_lock);
276 return -EEXIST;
277 }
278 }
279
280 list_add_tail(&dev->list, &ps_devices_list);
281 mutex_unlock(&ps_devices_lock);
282 return 0;
283}
284
285static int ps_devices_list_remove(struct ps_device *dev)
286{
287 mutex_lock(&ps_devices_lock);
288 list_del(&dev->list);
289 mutex_unlock(&ps_devices_lock);
290 return 0;
291}
292
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800293static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix)
294{
295 struct input_dev *input_dev;
296
297 input_dev = devm_input_allocate_device(&hdev->dev);
298 if (!input_dev)
299 return ERR_PTR(-ENOMEM);
300
301 input_dev->id.bustype = hdev->bus;
302 input_dev->id.vendor = hdev->vendor;
303 input_dev->id.product = hdev->product;
304 input_dev->id.version = hdev->version;
305 input_dev->uniq = hdev->uniq;
306
307 if (name_suffix) {
308 input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name,
309 name_suffix);
310 if (!input_dev->name)
311 return ERR_PTR(-ENOMEM);
312 } else {
313 input_dev->name = hdev->name;
314 }
315
316 input_set_drvdata(input_dev, hdev);
317
318 return input_dev;
319}
320
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800321static enum power_supply_property ps_power_supply_props[] = {
322 POWER_SUPPLY_PROP_STATUS,
323 POWER_SUPPLY_PROP_PRESENT,
324 POWER_SUPPLY_PROP_CAPACITY,
325 POWER_SUPPLY_PROP_SCOPE,
326};
327
328static int ps_battery_get_property(struct power_supply *psy,
329 enum power_supply_property psp,
330 union power_supply_propval *val)
331{
332 struct ps_device *dev = power_supply_get_drvdata(psy);
333 uint8_t battery_capacity;
334 int battery_status;
335 unsigned long flags;
Roderick Colenbrander5fb52552021-02-11 22:41:00 -0800336 int ret = 0;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800337
338 spin_lock_irqsave(&dev->lock, flags);
339 battery_capacity = dev->battery_capacity;
340 battery_status = dev->battery_status;
341 spin_unlock_irqrestore(&dev->lock, flags);
342
343 switch (psp) {
344 case POWER_SUPPLY_PROP_STATUS:
345 val->intval = battery_status;
346 break;
347 case POWER_SUPPLY_PROP_PRESENT:
348 val->intval = 1;
349 break;
350 case POWER_SUPPLY_PROP_CAPACITY:
351 val->intval = battery_capacity;
352 break;
353 case POWER_SUPPLY_PROP_SCOPE:
354 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
355 break;
356 default:
357 ret = -EINVAL;
358 break;
359 }
360
Roderick Colenbrander5fb52552021-02-11 22:41:00 -0800361 return ret;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800362}
363
364static int ps_device_register_battery(struct ps_device *dev)
365{
366 struct power_supply *battery;
367 struct power_supply_config battery_cfg = { .drv_data = dev };
368 int ret;
369
370 dev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
371 dev->battery_desc.properties = ps_power_supply_props;
372 dev->battery_desc.num_properties = ARRAY_SIZE(ps_power_supply_props);
373 dev->battery_desc.get_property = ps_battery_get_property;
374 dev->battery_desc.name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
375 "ps-controller-battery-%pMR", dev->mac_address);
376 if (!dev->battery_desc.name)
377 return -ENOMEM;
378
379 battery = devm_power_supply_register(&dev->hdev->dev, &dev->battery_desc, &battery_cfg);
380 if (IS_ERR(battery)) {
381 ret = PTR_ERR(battery);
382 hid_err(dev->hdev, "Unable to register battery device: %d\n", ret);
383 return ret;
384 }
385 dev->battery = battery;
386
387 ret = power_supply_powers(dev->battery, &dev->hdev->dev);
388 if (ret) {
389 hid_err(dev->hdev, "Unable to activate battery device: %d\n", ret);
390 return ret;
391 }
392
393 return 0;
394}
395
Roderick Colenbrander799b2b52021-02-07 13:49:02 -0800396/* Compute crc32 of HID data and compare against expected CRC. */
397static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc)
398{
399 uint32_t crc;
400
401 crc = crc32_le(0xFFFFFFFF, &seed, 1);
402 crc = ~crc32_le(crc, data, len);
403
404 return crc == report_crc;
405}
406
Roderick Colenbrander51151092021-02-07 13:49:03 -0800407static struct input_dev *ps_gamepad_create(struct hid_device *hdev,
408 int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800409{
410 struct input_dev *gamepad;
411 unsigned int i;
412 int ret;
413
414 gamepad = ps_allocate_input_dev(hdev, NULL);
415 if (IS_ERR(gamepad))
416 return ERR_CAST(gamepad);
417
418 input_set_abs_params(gamepad, ABS_X, 0, 255, 0, 0);
419 input_set_abs_params(gamepad, ABS_Y, 0, 255, 0, 0);
420 input_set_abs_params(gamepad, ABS_Z, 0, 255, 0, 0);
421 input_set_abs_params(gamepad, ABS_RX, 0, 255, 0, 0);
422 input_set_abs_params(gamepad, ABS_RY, 0, 255, 0, 0);
423 input_set_abs_params(gamepad, ABS_RZ, 0, 255, 0, 0);
424
425 input_set_abs_params(gamepad, ABS_HAT0X, -1, 1, 0, 0);
426 input_set_abs_params(gamepad, ABS_HAT0Y, -1, 1, 0, 0);
427
428 for (i = 0; i < ARRAY_SIZE(ps_gamepad_buttons); i++)
429 input_set_capability(gamepad, EV_KEY, ps_gamepad_buttons[i]);
430
Roderick Colenbrander51151092021-02-07 13:49:03 -0800431#if IS_ENABLED(CONFIG_PLAYSTATION_FF)
432 if (play_effect) {
433 input_set_capability(gamepad, EV_FF, FF_RUMBLE);
434 input_ff_create_memless(gamepad, NULL, play_effect);
435 }
436#endif
437
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800438 ret = input_register_device(gamepad);
439 if (ret)
440 return ERR_PTR(ret);
441
442 return gamepad;
443}
444
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -0800445static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *buf, size_t size)
446{
447 int ret;
448
449 ret = hid_hw_raw_request(hdev, report_id, buf, size, HID_FEATURE_REPORT,
450 HID_REQ_GET_REPORT);
451 if (ret < 0) {
452 hid_err(hdev, "Failed to retrieve feature with reportID %d: %d\n", report_id, ret);
453 return ret;
454 }
455
456 if (ret != size) {
457 hid_err(hdev, "Invalid byte count transferred, expected %zu got %d\n", size, ret);
458 return -EINVAL;
459 }
460
461 if (buf[0] != report_id) {
462 hid_err(hdev, "Invalid reportID received, expected %d got %d\n", report_id, buf[0]);
463 return -EINVAL;
464 }
465
Roderick Colenbrander799b2b52021-02-07 13:49:02 -0800466 if (hdev->bus == BUS_BLUETOOTH) {
467 /* Last 4 bytes contains crc32. */
468 uint8_t crc_offset = size - 4;
469 uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]);
470
471 if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) {
472 hid_err(hdev, "CRC check failed for reportID=%d\n", report_id);
473 return -EILSEQ;
474 }
475 }
476
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -0800477 return 0;
478}
479
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800480static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res,
481 int gyro_range, int gyro_res)
482{
483 struct input_dev *sensors;
484 int ret;
485
486 sensors = ps_allocate_input_dev(hdev, "Motion Sensors");
487 if (IS_ERR(sensors))
488 return ERR_CAST(sensors);
489
490 __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit);
491 __set_bit(EV_MSC, sensors->evbit);
492 __set_bit(MSC_TIMESTAMP, sensors->mscbit);
493
494 /* Accelerometer */
495 input_set_abs_params(sensors, ABS_X, -accel_range, accel_range, 16, 0);
496 input_set_abs_params(sensors, ABS_Y, -accel_range, accel_range, 16, 0);
497 input_set_abs_params(sensors, ABS_Z, -accel_range, accel_range, 16, 0);
498 input_abs_set_res(sensors, ABS_X, accel_res);
499 input_abs_set_res(sensors, ABS_Y, accel_res);
500 input_abs_set_res(sensors, ABS_Z, accel_res);
501
502 /* Gyroscope */
503 input_set_abs_params(sensors, ABS_RX, -gyro_range, gyro_range, 16, 0);
504 input_set_abs_params(sensors, ABS_RY, -gyro_range, gyro_range, 16, 0);
505 input_set_abs_params(sensors, ABS_RZ, -gyro_range, gyro_range, 16, 0);
506 input_abs_set_res(sensors, ABS_RX, gyro_res);
507 input_abs_set_res(sensors, ABS_RY, gyro_res);
508 input_abs_set_res(sensors, ABS_RZ, gyro_res);
509
510 ret = input_register_device(sensors);
511 if (ret)
512 return ERR_PTR(ret);
513
514 return sensors;
515}
516
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800517static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, int height,
518 unsigned int num_contacts)
519{
520 struct input_dev *touchpad;
521 int ret;
522
523 touchpad = ps_allocate_input_dev(hdev, "Touchpad");
524 if (IS_ERR(touchpad))
525 return ERR_CAST(touchpad);
526
527 /* Map button underneath touchpad to BTN_LEFT. */
528 input_set_capability(touchpad, EV_KEY, BTN_LEFT);
529 __set_bit(INPUT_PROP_BUTTONPAD, touchpad->propbit);
530
531 input_set_abs_params(touchpad, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
532 input_set_abs_params(touchpad, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
533
534 ret = input_mt_init_slots(touchpad, num_contacts, INPUT_MT_POINTER);
535 if (ret)
536 return ERR_PTR(ret);
537
538 ret = input_register_device(touchpad);
539 if (ret)
540 return ERR_PTR(ret);
541
542 return touchpad;
543}
544
Roderick Colenbrander0b25b552021-02-07 13:49:08 -0800545static ssize_t firmware_version_show(struct device *dev,
546 struct device_attribute
547 *attr, char *buf)
548{
549 struct hid_device *hdev = to_hid_device(dev);
550 struct ps_device *ps_dev = hid_get_drvdata(hdev);
551
552 return sysfs_emit(buf, "0x%08x\n", ps_dev->fw_version);
553}
554
555static DEVICE_ATTR_RO(firmware_version);
556
557static ssize_t hardware_version_show(struct device *dev,
558 struct device_attribute
559 *attr, char *buf)
560{
561 struct hid_device *hdev = to_hid_device(dev);
562 struct ps_device *ps_dev = hid_get_drvdata(hdev);
563
564 return sysfs_emit(buf, "0x%08x\n", ps_dev->hw_version);
565}
566
567static DEVICE_ATTR_RO(hardware_version);
568
569static struct attribute *ps_device_attributes[] = {
570 &dev_attr_firmware_version.attr,
571 &dev_attr_hardware_version.attr,
572 NULL
573};
574
575static const struct attribute_group ps_device_attribute_group = {
576 .attrs = ps_device_attributes,
577};
578
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800579static int dualsense_get_calibration_data(struct dualsense *ds)
580{
581 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
582 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
583 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
584 short gyro_speed_plus, gyro_speed_minus;
585 short acc_x_plus, acc_x_minus;
586 short acc_y_plus, acc_y_minus;
587 short acc_z_plus, acc_z_minus;
588 int speed_2x;
589 int range_2g;
590 int ret = 0;
591 uint8_t *buf;
592
593 buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL);
594 if (!buf)
595 return -ENOMEM;
596
597 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_CALIBRATION, buf,
598 DS_FEATURE_REPORT_CALIBRATION_SIZE);
599 if (ret) {
600 hid_err(ds->base.hdev, "Failed to retrieve DualSense calibration info: %d\n", ret);
601 goto err_free;
602 }
603
604 gyro_pitch_bias = get_unaligned_le16(&buf[1]);
605 gyro_yaw_bias = get_unaligned_le16(&buf[3]);
606 gyro_roll_bias = get_unaligned_le16(&buf[5]);
607 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
608 gyro_pitch_minus = get_unaligned_le16(&buf[9]);
609 gyro_yaw_plus = get_unaligned_le16(&buf[11]);
610 gyro_yaw_minus = get_unaligned_le16(&buf[13]);
611 gyro_roll_plus = get_unaligned_le16(&buf[15]);
612 gyro_roll_minus = get_unaligned_le16(&buf[17]);
613 gyro_speed_plus = get_unaligned_le16(&buf[19]);
614 gyro_speed_minus = get_unaligned_le16(&buf[21]);
615 acc_x_plus = get_unaligned_le16(&buf[23]);
616 acc_x_minus = get_unaligned_le16(&buf[25]);
617 acc_y_plus = get_unaligned_le16(&buf[27]);
618 acc_y_minus = get_unaligned_le16(&buf[29]);
619 acc_z_plus = get_unaligned_le16(&buf[31]);
620 acc_z_minus = get_unaligned_le16(&buf[33]);
621
622 /*
623 * Set gyroscope calibration and normalization parameters.
624 * Data values will be normalized to 1/DS_GYRO_RES_PER_DEG_S degree/s.
625 */
626 speed_2x = (gyro_speed_plus + gyro_speed_minus);
627 ds->gyro_calib_data[0].abs_code = ABS_RX;
628 ds->gyro_calib_data[0].bias = gyro_pitch_bias;
629 ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
630 ds->gyro_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
631
632 ds->gyro_calib_data[1].abs_code = ABS_RY;
633 ds->gyro_calib_data[1].bias = gyro_yaw_bias;
634 ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
635 ds->gyro_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
636
637 ds->gyro_calib_data[2].abs_code = ABS_RZ;
638 ds->gyro_calib_data[2].bias = gyro_roll_bias;
639 ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
640 ds->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
641
642 /*
643 * Set accelerometer calibration and normalization parameters.
644 * Data values will be normalized to 1/DS_ACC_RES_PER_G g.
645 */
646 range_2g = acc_x_plus - acc_x_minus;
647 ds->accel_calib_data[0].abs_code = ABS_X;
648 ds->accel_calib_data[0].bias = acc_x_plus - range_2g / 2;
649 ds->accel_calib_data[0].sens_numer = 2*DS_ACC_RES_PER_G;
650 ds->accel_calib_data[0].sens_denom = range_2g;
651
652 range_2g = acc_y_plus - acc_y_minus;
653 ds->accel_calib_data[1].abs_code = ABS_Y;
654 ds->accel_calib_data[1].bias = acc_y_plus - range_2g / 2;
655 ds->accel_calib_data[1].sens_numer = 2*DS_ACC_RES_PER_G;
656 ds->accel_calib_data[1].sens_denom = range_2g;
657
658 range_2g = acc_z_plus - acc_z_minus;
659 ds->accel_calib_data[2].abs_code = ABS_Z;
660 ds->accel_calib_data[2].bias = acc_z_plus - range_2g / 2;
661 ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G;
662 ds->accel_calib_data[2].sens_denom = range_2g;
663
664err_free:
665 kfree(buf);
666 return ret;
667}
668
Roderick Colenbrander0b25b552021-02-07 13:49:08 -0800669static int dualsense_get_firmware_info(struct dualsense *ds)
670{
671 uint8_t *buf;
672 int ret;
673
674 buf = kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL);
675 if (!buf)
676 return -ENOMEM;
677
678 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_FIRMWARE_INFO, buf,
679 DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE);
680 if (ret) {
681 hid_err(ds->base.hdev, "Failed to retrieve DualSense firmware info: %d\n", ret);
682 goto err_free;
683 }
684
685 ds->base.hw_version = get_unaligned_le32(&buf[24]);
686 ds->base.fw_version = get_unaligned_le32(&buf[28]);
687
688err_free:
689 kfree(buf);
690 return ret;
691}
692
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -0800693static int dualsense_get_mac_address(struct dualsense *ds)
694{
695 uint8_t *buf;
696 int ret = 0;
697
698 buf = kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL);
699 if (!buf)
700 return -ENOMEM;
701
702 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_PAIRING_INFO, buf,
703 DS_FEATURE_REPORT_PAIRING_INFO_SIZE);
704 if (ret) {
705 hid_err(ds->base.hdev, "Failed to retrieve DualSense pairing info: %d\n", ret);
706 goto err_free;
707 }
708
709 memcpy(ds->base.mac_address, &buf[1], sizeof(ds->base.mac_address));
710
711err_free:
712 kfree(buf);
713 return ret;
714}
715
Roderick Colenbrander51151092021-02-07 13:49:03 -0800716static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp,
717 void *buf)
718{
719 struct hid_device *hdev = ds->base.hdev;
720
721 if (hdev->bus == BUS_BLUETOOTH) {
722 struct dualsense_output_report_bt *bt = buf;
723
724 memset(bt, 0, sizeof(*bt));
725 bt->report_id = DS_OUTPUT_REPORT_BT;
726 bt->tag = DS_OUTPUT_TAG; /* Tag must be set. Exact meaning is unclear. */
727
728 /*
729 * Highest 4-bit is a sequence number, which needs to be increased
730 * every report. Lowest 4-bit is tag and can be zero for now.
731 */
732 bt->seq_tag = (ds->output_seq << 4) | 0x0;
733 if (++ds->output_seq == 16)
734 ds->output_seq = 0;
735
736 rp->data = buf;
737 rp->len = sizeof(*bt);
738 rp->bt = bt;
739 rp->usb = NULL;
740 rp->common = &bt->common;
741 } else { /* USB */
742 struct dualsense_output_report_usb *usb = buf;
743
744 memset(usb, 0, sizeof(*usb));
745 usb->report_id = DS_OUTPUT_REPORT_USB;
746
747 rp->data = buf;
748 rp->len = sizeof(*usb);
749 rp->bt = NULL;
750 rp->usb = usb;
751 rp->common = &usb->common;
752 }
753}
754
755/*
756 * Helper function to send DualSense output reports. Applies a CRC at the end of a report
757 * for Bluetooth reports.
758 */
759static void dualsense_send_output_report(struct dualsense *ds,
760 struct dualsense_output_report *report)
761{
762 struct hid_device *hdev = ds->base.hdev;
763
764 /* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */
765 if (report->bt) {
766 uint32_t crc;
767 uint8_t seed = PS_OUTPUT_CRC32_SEED;
768
769 crc = crc32_le(0xFFFFFFFF, &seed, 1);
770 crc = ~crc32_le(crc, report->data, report->len - 4);
771
772 report->bt->crc32 = cpu_to_le32(crc);
773 }
774
775 hid_hw_output_report(hdev, report->data, report->len);
776}
777
778static void dualsense_output_worker(struct work_struct *work)
779{
780 struct dualsense *ds = container_of(work, struct dualsense, output_worker);
781 struct dualsense_output_report report;
782 struct dualsense_output_report_common *common;
783 unsigned long flags;
784
785 dualsense_init_output_report(ds, &report, ds->output_report_dmabuf);
786 common = report.common;
787
788 spin_lock_irqsave(&ds->base.lock, flags);
789
790 if (ds->update_rumble) {
791 /* Select classic rumble style haptics and enable it. */
792 common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT;
793 common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION;
794 common->motor_left = ds->motor_left;
795 common->motor_right = ds->motor_right;
796 ds->update_rumble = false;
797 }
798
799 spin_unlock_irqrestore(&ds->base.lock, flags);
800
801 dualsense_send_output_report(ds, &report);
802}
803
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800804static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *report,
805 u8 *data, int size)
806{
807 struct hid_device *hdev = ps_dev->hdev;
808 struct dualsense *ds = container_of(ps_dev, struct dualsense, base);
809 struct dualsense_input_report *ds_report;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800810 uint8_t battery_data, battery_capacity, charging_status, value;
811 int battery_status;
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800812 uint32_t sensor_timestamp;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800813 unsigned long flags;
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800814 int i;
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800815
816 /*
817 * DualSense in USB uses the full HID report for reportID 1, but
818 * Bluetooth uses a minimal HID report for reportID 1 and reports
819 * the full report using reportID 49.
820 */
821 if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB &&
822 size == DS_INPUT_REPORT_USB_SIZE) {
823 ds_report = (struct dualsense_input_report *)&data[1];
Roderick Colenbrander799b2b52021-02-07 13:49:02 -0800824 } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT &&
825 size == DS_INPUT_REPORT_BT_SIZE) {
826 /* Last 4 bytes of input report contain crc32 */
827 uint32_t report_crc = get_unaligned_le32(&data[size - 4]);
828
829 if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) {
830 hid_err(hdev, "DualSense input CRC's check failed\n");
831 return -EILSEQ;
832 }
833
834 ds_report = (struct dualsense_input_report *)&data[2];
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800835 } else {
836 hid_err(hdev, "Unhandled reportID=%d\n", report->id);
837 return -1;
838 }
839
840 input_report_abs(ds->gamepad, ABS_X, ds_report->x);
841 input_report_abs(ds->gamepad, ABS_Y, ds_report->y);
842 input_report_abs(ds->gamepad, ABS_RX, ds_report->rx);
843 input_report_abs(ds->gamepad, ABS_RY, ds_report->ry);
844 input_report_abs(ds->gamepad, ABS_Z, ds_report->z);
845 input_report_abs(ds->gamepad, ABS_RZ, ds_report->rz);
846
847 value = ds_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH;
Colin Ian King50ab1ff2021-02-15 16:39:21 +0000848 if (value >= ARRAY_SIZE(ps_gamepad_hat_mapping))
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800849 value = 8; /* center */
850 input_report_abs(ds->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x);
851 input_report_abs(ds->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y);
852
853 input_report_key(ds->gamepad, BTN_WEST, ds_report->buttons[0] & DS_BUTTONS0_SQUARE);
854 input_report_key(ds->gamepad, BTN_SOUTH, ds_report->buttons[0] & DS_BUTTONS0_CROSS);
855 input_report_key(ds->gamepad, BTN_EAST, ds_report->buttons[0] & DS_BUTTONS0_CIRCLE);
856 input_report_key(ds->gamepad, BTN_NORTH, ds_report->buttons[0] & DS_BUTTONS0_TRIANGLE);
857 input_report_key(ds->gamepad, BTN_TL, ds_report->buttons[1] & DS_BUTTONS1_L1);
858 input_report_key(ds->gamepad, BTN_TR, ds_report->buttons[1] & DS_BUTTONS1_R1);
859 input_report_key(ds->gamepad, BTN_TL2, ds_report->buttons[1] & DS_BUTTONS1_L2);
860 input_report_key(ds->gamepad, BTN_TR2, ds_report->buttons[1] & DS_BUTTONS1_R2);
861 input_report_key(ds->gamepad, BTN_SELECT, ds_report->buttons[1] & DS_BUTTONS1_CREATE);
862 input_report_key(ds->gamepad, BTN_START, ds_report->buttons[1] & DS_BUTTONS1_OPTIONS);
863 input_report_key(ds->gamepad, BTN_THUMBL, ds_report->buttons[1] & DS_BUTTONS1_L3);
864 input_report_key(ds->gamepad, BTN_THUMBR, ds_report->buttons[1] & DS_BUTTONS1_R3);
865 input_report_key(ds->gamepad, BTN_MODE, ds_report->buttons[2] & DS_BUTTONS2_PS_HOME);
866 input_sync(ds->gamepad);
867
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800868 /* Parse and calibrate gyroscope data. */
869 for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) {
870 int raw_data = (short)le16_to_cpu(ds_report->gyro[i]);
871 int calib_data = mult_frac(ds->gyro_calib_data[i].sens_numer,
872 raw_data - ds->gyro_calib_data[i].bias,
873 ds->gyro_calib_data[i].sens_denom);
874
875 input_report_abs(ds->sensors, ds->gyro_calib_data[i].abs_code, calib_data);
876 }
877
878 /* Parse and calibrate accelerometer data. */
879 for (i = 0; i < ARRAY_SIZE(ds_report->accel); i++) {
880 int raw_data = (short)le16_to_cpu(ds_report->accel[i]);
881 int calib_data = mult_frac(ds->accel_calib_data[i].sens_numer,
882 raw_data - ds->accel_calib_data[i].bias,
883 ds->accel_calib_data[i].sens_denom);
884
885 input_report_abs(ds->sensors, ds->accel_calib_data[i].abs_code, calib_data);
886 }
887
888 /* Convert timestamp (in 0.33us unit) to timestamp_us */
889 sensor_timestamp = le32_to_cpu(ds_report->sensor_timestamp);
890 if (!ds->sensor_timestamp_initialized) {
891 ds->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp, 3);
892 ds->sensor_timestamp_initialized = true;
893 } else {
894 uint32_t delta;
895
896 if (ds->prev_sensor_timestamp > sensor_timestamp)
897 delta = (U32_MAX - ds->prev_sensor_timestamp + sensor_timestamp + 1);
898 else
899 delta = sensor_timestamp - ds->prev_sensor_timestamp;
900 ds->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta, 3);
901 }
902 ds->prev_sensor_timestamp = sensor_timestamp;
903 input_event(ds->sensors, EV_MSC, MSC_TIMESTAMP, ds->sensor_timestamp_us);
904 input_sync(ds->sensors);
905
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800906 for (i = 0; i < ARRAY_SIZE(ds_report->points); i++) {
907 struct dualsense_touch_point *point = &ds_report->points[i];
908 bool active = (point->contact & DS_TOUCH_POINT_INACTIVE) ? false : true;
909
910 input_mt_slot(ds->touchpad, i);
911 input_mt_report_slot_state(ds->touchpad, MT_TOOL_FINGER, active);
912
913 if (active) {
914 int x = (point->x_hi << 8) | point->x_lo;
915 int y = (point->y_hi << 4) | point->y_lo;
916
917 input_report_abs(ds->touchpad, ABS_MT_POSITION_X, x);
918 input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, y);
919 }
920 }
921 input_mt_sync_frame(ds->touchpad);
922 input_report_key(ds->touchpad, BTN_LEFT, ds_report->buttons[2] & DS_BUTTONS2_TOUCHPAD);
923 input_sync(ds->touchpad);
924
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800925 battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY;
926 charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT;
927
928 switch (charging_status) {
929 case 0x0:
930 /*
931 * Each unit of battery data corresponds to 10%
932 * 0 = 0-9%, 1 = 10-19%, .. and 10 = 100%
933 */
934 battery_capacity = min(battery_data * 10 + 5, 100);
935 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
936 break;
937 case 0x1:
938 battery_capacity = min(battery_data * 10 + 5, 100);
939 battery_status = POWER_SUPPLY_STATUS_CHARGING;
940 break;
941 case 0x2:
942 battery_capacity = 100;
943 battery_status = POWER_SUPPLY_STATUS_FULL;
944 break;
945 case 0xa: /* voltage or temperature out of range */
946 case 0xb: /* temperature error */
947 battery_capacity = 0;
948 battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
949 break;
950 case 0xf: /* charging error */
951 default:
952 battery_capacity = 0;
953 battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
954 }
955
956 spin_lock_irqsave(&ps_dev->lock, flags);
957 ps_dev->battery_capacity = battery_capacity;
958 ps_dev->battery_status = battery_status;
959 spin_unlock_irqrestore(&ps_dev->lock, flags);
960
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800961 return 0;
962}
963
Roderick Colenbrander51151092021-02-07 13:49:03 -0800964static int dualsense_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
965{
966 struct hid_device *hdev = input_get_drvdata(dev);
967 struct dualsense *ds = hid_get_drvdata(hdev);
968 unsigned long flags;
969
970 if (effect->type != FF_RUMBLE)
971 return 0;
972
973 spin_lock_irqsave(&ds->base.lock, flags);
974 ds->update_rumble = true;
975 ds->motor_left = effect->u.rumble.strong_magnitude / 256;
976 ds->motor_right = effect->u.rumble.weak_magnitude / 256;
977 spin_unlock_irqrestore(&ds->base.lock, flags);
978
979 schedule_work(&ds->output_worker);
980 return 0;
981}
982
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800983static struct ps_device *dualsense_create(struct hid_device *hdev)
984{
985 struct dualsense *ds;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800986 struct ps_device *ps_dev;
Roderick Colenbrander51151092021-02-07 13:49:03 -0800987 uint8_t max_output_report_size;
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800988 int ret;
989
990 ds = devm_kzalloc(&hdev->dev, sizeof(*ds), GFP_KERNEL);
991 if (!ds)
992 return ERR_PTR(-ENOMEM);
993
994 /*
995 * Patch version to allow userspace to distinguish between
996 * hid-generic vs hid-playstation axis and button mapping.
997 */
998 hdev->version |= HID_PLAYSTATION_VERSION_PATCH;
999
Roderick Colenbranderd30bca42021-02-07 13:48:58 -08001000 ps_dev = &ds->base;
1001 ps_dev->hdev = hdev;
1002 spin_lock_init(&ps_dev->lock);
1003 ps_dev->battery_capacity = 100; /* initial value until parse_report. */
1004 ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
1005 ps_dev->parse_report = dualsense_parse_report;
Roderick Colenbrander51151092021-02-07 13:49:03 -08001006 INIT_WORK(&ds->output_worker, dualsense_output_worker);
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001007 hid_set_drvdata(hdev, ds);
1008
Roderick Colenbrander51151092021-02-07 13:49:03 -08001009 max_output_report_size = sizeof(struct dualsense_output_report_bt);
1010 ds->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL);
1011 if (!ds->output_report_dmabuf)
1012 return ERR_PTR(-ENOMEM);
1013
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -08001014 ret = dualsense_get_mac_address(ds);
1015 if (ret) {
1016 hid_err(hdev, "Failed to get MAC address from DualSense\n");
1017 return ERR_PTR(ret);
1018 }
1019 snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address);
1020
Roderick Colenbrander0b25b552021-02-07 13:49:08 -08001021 ret = dualsense_get_firmware_info(ds);
1022 if (ret) {
1023 hid_err(hdev, "Failed to get firmware info from DualSense\n");
1024 return ERR_PTR(ret);
1025 }
1026
Roderick Colenbrander53f04e82021-02-07 13:49:01 -08001027 ret = ps_devices_list_add(ps_dev);
1028 if (ret)
1029 return ERR_PTR(ret);
1030
Roderick Colenbrander402987c2021-02-07 13:49:00 -08001031 ret = dualsense_get_calibration_data(ds);
1032 if (ret) {
1033 hid_err(hdev, "Failed to get calibration data from DualSense\n");
1034 goto err;
1035 }
1036
Roderick Colenbrander51151092021-02-07 13:49:03 -08001037 ds->gamepad = ps_gamepad_create(hdev, dualsense_play_effect);
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001038 if (IS_ERR(ds->gamepad)) {
1039 ret = PTR_ERR(ds->gamepad);
1040 goto err;
1041 }
1042
Roderick Colenbrander402987c2021-02-07 13:49:00 -08001043 ds->sensors = ps_sensors_create(hdev, DS_ACC_RANGE, DS_ACC_RES_PER_G,
1044 DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S);
1045 if (IS_ERR(ds->sensors)) {
1046 ret = PTR_ERR(ds->sensors);
1047 goto err;
1048 }
1049
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -08001050 ds->touchpad = ps_touchpad_create(hdev, DS_TOUCHPAD_WIDTH, DS_TOUCHPAD_HEIGHT, 2);
1051 if (IS_ERR(ds->touchpad)) {
1052 ret = PTR_ERR(ds->touchpad);
1053 goto err;
1054 }
1055
Roderick Colenbranderd30bca42021-02-07 13:48:58 -08001056 ret = ps_device_register_battery(ps_dev);
1057 if (ret)
1058 goto err;
1059
Roderick Colenbrander0b25b552021-02-07 13:49:08 -08001060 /*
1061 * Reporting hardware and firmware is important as there are frequent updates, which
1062 * can change behavior.
1063 */
1064 hid_info(hdev, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n",
1065 ds->base.hw_version, ds->base.fw_version);
1066
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001067 return &ds->base;
1068
1069err:
Roderick Colenbrander53f04e82021-02-07 13:49:01 -08001070 ps_devices_list_remove(ps_dev);
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001071 return ERR_PTR(ret);
1072}
1073
1074static int ps_raw_event(struct hid_device *hdev, struct hid_report *report,
1075 u8 *data, int size)
1076{
1077 struct ps_device *dev = hid_get_drvdata(hdev);
1078
1079 if (dev && dev->parse_report)
1080 return dev->parse_report(dev, report, data, size);
1081
1082 return 0;
1083}
1084
1085static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id)
1086{
1087 struct ps_device *dev;
1088 int ret;
1089
1090 ret = hid_parse(hdev);
1091 if (ret) {
1092 hid_err(hdev, "Parse failed\n");
1093 return ret;
1094 }
1095
1096 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1097 if (ret) {
1098 hid_err(hdev, "Failed to start HID device\n");
1099 return ret;
1100 }
1101
1102 ret = hid_hw_open(hdev);
1103 if (ret) {
1104 hid_err(hdev, "Failed to open HID device\n");
1105 goto err_stop;
1106 }
1107
1108 if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER) {
1109 dev = dualsense_create(hdev);
1110 if (IS_ERR(dev)) {
1111 hid_err(hdev, "Failed to create dualsense.\n");
1112 ret = PTR_ERR(dev);
1113 goto err_close;
1114 }
1115 }
1116
Roderick Colenbrander0b25b552021-02-07 13:49:08 -08001117 ret = devm_device_add_group(&hdev->dev, &ps_device_attribute_group);
1118 if (ret) {
1119 hid_err(hdev, "Failed to register sysfs nodes.\n");
1120 goto err_close;
1121 }
1122
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001123 return ret;
1124
1125err_close:
1126 hid_hw_close(hdev);
1127err_stop:
1128 hid_hw_stop(hdev);
1129 return ret;
1130}
1131
1132static void ps_remove(struct hid_device *hdev)
1133{
Roderick Colenbrander53f04e82021-02-07 13:49:01 -08001134 struct ps_device *dev = hid_get_drvdata(hdev);
1135
1136 ps_devices_list_remove(dev);
1137
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001138 hid_hw_close(hdev);
1139 hid_hw_stop(hdev);
1140}
1141
1142static const struct hid_device_id ps_devices[] = {
Roderick Colenbrander799b2b52021-02-07 13:49:02 -08001143 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001144 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
1145 { }
1146};
1147MODULE_DEVICE_TABLE(hid, ps_devices);
1148
1149static struct hid_driver ps_driver = {
1150 .name = "playstation",
1151 .id_table = ps_devices,
1152 .probe = ps_probe,
1153 .remove = ps_remove,
1154 .raw_event = ps_raw_event,
1155};
1156
1157module_hid_driver(ps_driver);
1158
1159MODULE_AUTHOR("Sony Interactive Entertainment");
1160MODULE_DESCRIPTION("HID Driver for PlayStation peripherals.");
1161MODULE_LICENSE("GPL");