blob: ff2fc315a89d894d84ded145ce18be3760170cd2 [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>
Roderick Colenbrander949aacc2021-02-16 16:56:09 -080012#include <linux/idr.h>
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080013#include <linux/input/mt.h>
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -070014#include <linux/leds.h>
15#include <linux/led-class-multicolor.h>
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080016#include <linux/module.h>
17
18#include <asm/unaligned.h>
19
20#include "hid-ids.h"
21
Roderick Colenbrander53f04e82021-02-07 13:49:01 -080022/* List of connected playstation devices. */
23static DEFINE_MUTEX(ps_devices_lock);
24static LIST_HEAD(ps_devices_list);
25
Roderick Colenbrander949aacc2021-02-16 16:56:09 -080026static DEFINE_IDA(ps_player_id_allocator);
27
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080028#define HID_PLAYSTATION_VERSION_PATCH 0x8000
29
30/* Base class for playstation devices. */
31struct ps_device {
Roderick Colenbrander53f04e82021-02-07 13:49:01 -080032 struct list_head list;
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080033 struct hid_device *hdev;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -080034 spinlock_t lock;
35
Roderick Colenbrander949aacc2021-02-16 16:56:09 -080036 uint32_t player_id;
37
Roderick Colenbranderd30bca42021-02-07 13:48:58 -080038 struct power_supply_desc battery_desc;
39 struct power_supply *battery;
40 uint8_t battery_capacity;
41 int battery_status;
42
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -070043 const char *input_dev_name; /* Name of primary input device. */
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -080044 uint8_t mac_address[6]; /* Note: stored in little endian order. */
Roderick Colenbrander0b25b552021-02-07 13:49:08 -080045 uint32_t hw_version;
46 uint32_t fw_version;
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080047
48 int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size);
49};
50
Roderick Colenbrander402987c2021-02-07 13:49:00 -080051/* Calibration data for playstation motion sensors. */
52struct ps_calibration_data {
53 int abs_code;
54 short bias;
55 int sens_numer;
56 int sens_denom;
57};
58
Roderick Colenbrander799b2b52021-02-07 13:49:02 -080059/* Seed values for DualShock4 / DualSense CRC32 for different report types. */
60#define PS_INPUT_CRC32_SEED 0xA1
Roderick Colenbrander51151092021-02-07 13:49:03 -080061#define PS_OUTPUT_CRC32_SEED 0xA2
Roderick Colenbrander799b2b52021-02-07 13:49:02 -080062#define PS_FEATURE_CRC32_SEED 0xA3
63
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080064#define DS_INPUT_REPORT_USB 0x01
65#define DS_INPUT_REPORT_USB_SIZE 64
Roderick Colenbrander799b2b52021-02-07 13:49:02 -080066#define DS_INPUT_REPORT_BT 0x31
67#define DS_INPUT_REPORT_BT_SIZE 78
Roderick Colenbrander51151092021-02-07 13:49:03 -080068#define DS_OUTPUT_REPORT_USB 0x02
69#define DS_OUTPUT_REPORT_USB_SIZE 63
70#define DS_OUTPUT_REPORT_BT 0x31
71#define DS_OUTPUT_REPORT_BT_SIZE 78
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080072
Roderick Colenbrander402987c2021-02-07 13:49:00 -080073#define DS_FEATURE_REPORT_CALIBRATION 0x05
74#define DS_FEATURE_REPORT_CALIBRATION_SIZE 41
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -080075#define DS_FEATURE_REPORT_PAIRING_INFO 0x09
76#define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20
Roderick Colenbrander0b25b552021-02-07 13:49:08 -080077#define DS_FEATURE_REPORT_FIRMWARE_INFO 0x20
78#define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE 64
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -080079
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080080/* Button masks for DualSense input report. */
81#define DS_BUTTONS0_HAT_SWITCH GENMASK(3, 0)
82#define DS_BUTTONS0_SQUARE BIT(4)
83#define DS_BUTTONS0_CROSS BIT(5)
84#define DS_BUTTONS0_CIRCLE BIT(6)
85#define DS_BUTTONS0_TRIANGLE BIT(7)
86#define DS_BUTTONS1_L1 BIT(0)
87#define DS_BUTTONS1_R1 BIT(1)
88#define DS_BUTTONS1_L2 BIT(2)
89#define DS_BUTTONS1_R2 BIT(3)
90#define DS_BUTTONS1_CREATE BIT(4)
91#define DS_BUTTONS1_OPTIONS BIT(5)
92#define DS_BUTTONS1_L3 BIT(6)
93#define DS_BUTTONS1_R3 BIT(7)
94#define DS_BUTTONS2_PS_HOME BIT(0)
95#define DS_BUTTONS2_TOUCHPAD BIT(1)
Roderick Colenbranderc26e48b2021-02-16 16:50:07 -080096#define DS_BUTTONS2_MIC_MUTE BIT(2)
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -080097
Roderick Colenbranderd30bca42021-02-07 13:48:58 -080098/* Status field of DualSense input report. */
99#define DS_STATUS_BATTERY_CAPACITY GENMASK(3, 0)
100#define DS_STATUS_CHARGING GENMASK(7, 4)
101#define DS_STATUS_CHARGING_SHIFT 4
102
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800103/*
104 * Status of a DualSense touch point contact.
105 * Contact IDs, with highest bit set are 'inactive'
106 * and any associated data is then invalid.
107 */
108#define DS_TOUCH_POINT_INACTIVE BIT(7)
109
Roderick Colenbrander51151092021-02-07 13:49:03 -0800110 /* Magic value required in tag field of Bluetooth output report. */
111#define DS_OUTPUT_TAG 0x10
112/* Flags for DualSense output report. */
113#define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0)
114#define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1)
Roderick Colenbranderc26e48b2021-02-16 16:50:07 -0800115#define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0)
116#define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1)
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -0800117#define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2)
118#define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3)
Roderick Colenbrander949aacc2021-02-16 16:56:09 -0800119#define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4)
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -0800120#define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1)
Roderick Colenbranderc26e48b2021-02-16 16:50:07 -0800121#define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4)
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -0800122#define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1)
Roderick Colenbrander51151092021-02-07 13:49:03 -0800123
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800124/* DualSense hardware limits */
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800125#define DS_ACC_RES_PER_G 8192
126#define DS_ACC_RANGE (4*DS_ACC_RES_PER_G)
127#define DS_GYRO_RES_PER_DEG_S 1024
128#define DS_GYRO_RANGE (2048*DS_GYRO_RES_PER_DEG_S)
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800129#define DS_TOUCHPAD_WIDTH 1920
130#define DS_TOUCHPAD_HEIGHT 1080
131
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800132struct dualsense {
133 struct ps_device base;
134 struct input_dev *gamepad;
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800135 struct input_dev *sensors;
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800136 struct input_dev *touchpad;
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800137
138 /* Calibration data for accelerometer and gyroscope. */
139 struct ps_calibration_data accel_calib_data[3];
140 struct ps_calibration_data gyro_calib_data[3];
141
142 /* Timestamp for sensor data */
143 bool sensor_timestamp_initialized;
144 uint32_t prev_sensor_timestamp;
145 uint32_t sensor_timestamp_us;
Roderick Colenbrander51151092021-02-07 13:49:03 -0800146
147 /* Compatible rumble state */
148 bool update_rumble;
149 uint8_t motor_left;
150 uint8_t motor_right;
151
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -0800152 /* RGB lightbar */
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -0700153 struct led_classdev_mc lightbar;
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -0800154 bool update_lightbar;
155 uint8_t lightbar_red;
156 uint8_t lightbar_green;
157 uint8_t lightbar_blue;
158
Roderick Colenbranderc26e48b2021-02-16 16:50:07 -0800159 /* Microphone */
160 bool update_mic_mute;
161 bool mic_muted;
162 bool last_btn_mic_state;
163
Roderick Colenbrander949aacc2021-02-16 16:56:09 -0800164 /* Player leds */
165 bool update_player_leds;
166 uint8_t player_leds_state;
167 struct led_classdev player_leds[5];
168
Roderick Colenbrander51151092021-02-07 13:49:03 -0800169 struct work_struct output_worker;
170 void *output_report_dmabuf;
171 uint8_t output_seq; /* Sequence number for output report. */
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800172};
173
174struct dualsense_touch_point {
175 uint8_t contact;
176 uint8_t x_lo;
177 uint8_t x_hi:4, y_lo:4;
178 uint8_t y_hi;
179} __packed;
180static_assert(sizeof(struct dualsense_touch_point) == 4);
181
182/* Main DualSense input report excluding any BT/USB specific headers. */
183struct dualsense_input_report {
184 uint8_t x, y;
185 uint8_t rx, ry;
186 uint8_t z, rz;
187 uint8_t seq_number;
188 uint8_t buttons[4];
189 uint8_t reserved[4];
190
191 /* Motion sensors */
192 __le16 gyro[3]; /* x, y, z */
193 __le16 accel[3]; /* x, y, z */
194 __le32 sensor_timestamp;
195 uint8_t reserved2;
196
197 /* Touchpad */
198 struct dualsense_touch_point points[2];
199
200 uint8_t reserved3[12];
201 uint8_t status;
202 uint8_t reserved4[10];
203} __packed;
204/* Common input report size shared equals the size of the USB report minus 1 byte for ReportID. */
205static_assert(sizeof(struct dualsense_input_report) == DS_INPUT_REPORT_USB_SIZE - 1);
206
Roderick Colenbrander51151092021-02-07 13:49:03 -0800207/* Common data between DualSense BT/USB main output report. */
208struct dualsense_output_report_common {
209 uint8_t valid_flag0;
210 uint8_t valid_flag1;
211
212 /* For DualShock 4 compatibility mode. */
213 uint8_t motor_right;
214 uint8_t motor_left;
215
216 /* Audio controls */
217 uint8_t reserved[4];
218 uint8_t mute_button_led;
219
220 uint8_t power_save_control;
221 uint8_t reserved2[28];
222
223 /* LEDs and lightbar */
224 uint8_t valid_flag2;
225 uint8_t reserved3[2];
226 uint8_t lightbar_setup;
227 uint8_t led_brightness;
228 uint8_t player_leds;
229 uint8_t lightbar_red;
230 uint8_t lightbar_green;
231 uint8_t lightbar_blue;
232} __packed;
233static_assert(sizeof(struct dualsense_output_report_common) == 47);
234
235struct dualsense_output_report_bt {
236 uint8_t report_id; /* 0x31 */
237 uint8_t seq_tag;
238 uint8_t tag;
239 struct dualsense_output_report_common common;
240 uint8_t reserved[24];
241 __le32 crc32;
242} __packed;
243static_assert(sizeof(struct dualsense_output_report_bt) == DS_OUTPUT_REPORT_BT_SIZE);
244
245struct dualsense_output_report_usb {
246 uint8_t report_id; /* 0x02 */
247 struct dualsense_output_report_common common;
248 uint8_t reserved[15];
249} __packed;
250static_assert(sizeof(struct dualsense_output_report_usb) == DS_OUTPUT_REPORT_USB_SIZE);
251
252/*
253 * The DualSense has a main output report used to control most features. It is
254 * largely the same between Bluetooth and USB except for different headers and CRC.
255 * This structure hide the differences between the two to simplify sending output reports.
256 */
257struct dualsense_output_report {
258 uint8_t *data; /* Start of data */
259 uint8_t len; /* Size of output report */
260
261 /* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
262 struct dualsense_output_report_bt *bt;
263 /* Points to USB data payload in case for a USB report else NULL. */
264 struct dualsense_output_report_usb *usb;
265 /* Points to common section of report, so past any headers. */
266 struct dualsense_output_report_common *common;
267};
268
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800269/*
270 * Common gamepad buttons across DualShock 3 / 4 and DualSense.
271 * Note: for device with a touchpad, touchpad button is not included
272 * as it will be part of the touchpad device.
273 */
274static const int ps_gamepad_buttons[] = {
275 BTN_WEST, /* Square */
276 BTN_NORTH, /* Triangle */
277 BTN_EAST, /* Circle */
278 BTN_SOUTH, /* Cross */
279 BTN_TL, /* L1 */
280 BTN_TR, /* R1 */
281 BTN_TL2, /* L2 */
282 BTN_TR2, /* R2 */
283 BTN_SELECT, /* Create (PS5) / Share (PS4) */
284 BTN_START, /* Option */
285 BTN_THUMBL, /* L3 */
286 BTN_THUMBR, /* R3 */
287 BTN_MODE, /* PS Home */
288};
289
290static const struct {int x; int y; } ps_gamepad_hat_mapping[] = {
291 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
292 {0, 0},
293};
294
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -0700295static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue);
296
Roderick Colenbrander53f04e82021-02-07 13:49:01 -0800297/*
298 * Add a new ps_device to ps_devices if it doesn't exist.
299 * Return error on duplicate device, which can happen if the same
300 * device is connected using both Bluetooth and USB.
301 */
302static int ps_devices_list_add(struct ps_device *dev)
303{
304 struct ps_device *entry;
305
306 mutex_lock(&ps_devices_lock);
307 list_for_each_entry(entry, &ps_devices_list, list) {
308 if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) {
309 hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n",
310 dev->mac_address);
311 mutex_unlock(&ps_devices_lock);
312 return -EEXIST;
313 }
314 }
315
316 list_add_tail(&dev->list, &ps_devices_list);
317 mutex_unlock(&ps_devices_lock);
318 return 0;
319}
320
321static int ps_devices_list_remove(struct ps_device *dev)
322{
323 mutex_lock(&ps_devices_lock);
324 list_del(&dev->list);
325 mutex_unlock(&ps_devices_lock);
326 return 0;
327}
328
Roderick Colenbrander949aacc2021-02-16 16:56:09 -0800329static int ps_device_set_player_id(struct ps_device *dev)
330{
331 int ret = ida_alloc(&ps_player_id_allocator, GFP_KERNEL);
332
333 if (ret < 0)
334 return ret;
335
336 dev->player_id = ret;
337 return 0;
338}
339
340static void ps_device_release_player_id(struct ps_device *dev)
341{
342 ida_free(&ps_player_id_allocator, dev->player_id);
343
344 dev->player_id = U32_MAX;
345}
346
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800347static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix)
348{
349 struct input_dev *input_dev;
350
351 input_dev = devm_input_allocate_device(&hdev->dev);
352 if (!input_dev)
353 return ERR_PTR(-ENOMEM);
354
355 input_dev->id.bustype = hdev->bus;
356 input_dev->id.vendor = hdev->vendor;
357 input_dev->id.product = hdev->product;
358 input_dev->id.version = hdev->version;
359 input_dev->uniq = hdev->uniq;
360
361 if (name_suffix) {
362 input_dev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name,
363 name_suffix);
364 if (!input_dev->name)
365 return ERR_PTR(-ENOMEM);
366 } else {
367 input_dev->name = hdev->name;
368 }
369
370 input_set_drvdata(input_dev, hdev);
371
372 return input_dev;
373}
374
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800375static enum power_supply_property ps_power_supply_props[] = {
376 POWER_SUPPLY_PROP_STATUS,
377 POWER_SUPPLY_PROP_PRESENT,
378 POWER_SUPPLY_PROP_CAPACITY,
379 POWER_SUPPLY_PROP_SCOPE,
380};
381
382static int ps_battery_get_property(struct power_supply *psy,
383 enum power_supply_property psp,
384 union power_supply_propval *val)
385{
386 struct ps_device *dev = power_supply_get_drvdata(psy);
387 uint8_t battery_capacity;
388 int battery_status;
389 unsigned long flags;
Roderick Colenbrander5fb52552021-02-11 22:41:00 -0800390 int ret = 0;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800391
392 spin_lock_irqsave(&dev->lock, flags);
393 battery_capacity = dev->battery_capacity;
394 battery_status = dev->battery_status;
395 spin_unlock_irqrestore(&dev->lock, flags);
396
397 switch (psp) {
398 case POWER_SUPPLY_PROP_STATUS:
399 val->intval = battery_status;
400 break;
401 case POWER_SUPPLY_PROP_PRESENT:
402 val->intval = 1;
403 break;
404 case POWER_SUPPLY_PROP_CAPACITY:
405 val->intval = battery_capacity;
406 break;
407 case POWER_SUPPLY_PROP_SCOPE:
408 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
409 break;
410 default:
411 ret = -EINVAL;
412 break;
413 }
414
Roderick Colenbrander5fb52552021-02-11 22:41:00 -0800415 return ret;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800416}
417
418static int ps_device_register_battery(struct ps_device *dev)
419{
420 struct power_supply *battery;
421 struct power_supply_config battery_cfg = { .drv_data = dev };
422 int ret;
423
424 dev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
425 dev->battery_desc.properties = ps_power_supply_props;
426 dev->battery_desc.num_properties = ARRAY_SIZE(ps_power_supply_props);
427 dev->battery_desc.get_property = ps_battery_get_property;
428 dev->battery_desc.name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
429 "ps-controller-battery-%pMR", dev->mac_address);
430 if (!dev->battery_desc.name)
431 return -ENOMEM;
432
433 battery = devm_power_supply_register(&dev->hdev->dev, &dev->battery_desc, &battery_cfg);
434 if (IS_ERR(battery)) {
435 ret = PTR_ERR(battery);
436 hid_err(dev->hdev, "Unable to register battery device: %d\n", ret);
437 return ret;
438 }
439 dev->battery = battery;
440
441 ret = power_supply_powers(dev->battery, &dev->hdev->dev);
442 if (ret) {
443 hid_err(dev->hdev, "Unable to activate battery device: %d\n", ret);
444 return ret;
445 }
446
447 return 0;
448}
449
Roderick Colenbrander799b2b52021-02-07 13:49:02 -0800450/* Compute crc32 of HID data and compare against expected CRC. */
451static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc)
452{
453 uint32_t crc;
454
455 crc = crc32_le(0xFFFFFFFF, &seed, 1);
456 crc = ~crc32_le(crc, data, len);
457
458 return crc == report_crc;
459}
460
Roderick Colenbrander51151092021-02-07 13:49:03 -0800461static struct input_dev *ps_gamepad_create(struct hid_device *hdev,
462 int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800463{
464 struct input_dev *gamepad;
465 unsigned int i;
466 int ret;
467
468 gamepad = ps_allocate_input_dev(hdev, NULL);
469 if (IS_ERR(gamepad))
470 return ERR_CAST(gamepad);
471
472 input_set_abs_params(gamepad, ABS_X, 0, 255, 0, 0);
473 input_set_abs_params(gamepad, ABS_Y, 0, 255, 0, 0);
474 input_set_abs_params(gamepad, ABS_Z, 0, 255, 0, 0);
475 input_set_abs_params(gamepad, ABS_RX, 0, 255, 0, 0);
476 input_set_abs_params(gamepad, ABS_RY, 0, 255, 0, 0);
477 input_set_abs_params(gamepad, ABS_RZ, 0, 255, 0, 0);
478
479 input_set_abs_params(gamepad, ABS_HAT0X, -1, 1, 0, 0);
480 input_set_abs_params(gamepad, ABS_HAT0Y, -1, 1, 0, 0);
481
482 for (i = 0; i < ARRAY_SIZE(ps_gamepad_buttons); i++)
483 input_set_capability(gamepad, EV_KEY, ps_gamepad_buttons[i]);
484
Roderick Colenbrander51151092021-02-07 13:49:03 -0800485#if IS_ENABLED(CONFIG_PLAYSTATION_FF)
486 if (play_effect) {
487 input_set_capability(gamepad, EV_FF, FF_RUMBLE);
488 input_ff_create_memless(gamepad, NULL, play_effect);
489 }
490#endif
491
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800492 ret = input_register_device(gamepad);
493 if (ret)
494 return ERR_PTR(ret);
495
496 return gamepad;
497}
498
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -0800499static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *buf, size_t size)
500{
501 int ret;
502
503 ret = hid_hw_raw_request(hdev, report_id, buf, size, HID_FEATURE_REPORT,
504 HID_REQ_GET_REPORT);
505 if (ret < 0) {
506 hid_err(hdev, "Failed to retrieve feature with reportID %d: %d\n", report_id, ret);
507 return ret;
508 }
509
510 if (ret != size) {
511 hid_err(hdev, "Invalid byte count transferred, expected %zu got %d\n", size, ret);
512 return -EINVAL;
513 }
514
515 if (buf[0] != report_id) {
516 hid_err(hdev, "Invalid reportID received, expected %d got %d\n", report_id, buf[0]);
517 return -EINVAL;
518 }
519
Roderick Colenbrander799b2b52021-02-07 13:49:02 -0800520 if (hdev->bus == BUS_BLUETOOTH) {
521 /* Last 4 bytes contains crc32. */
522 uint8_t crc_offset = size - 4;
523 uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]);
524
525 if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) {
526 hid_err(hdev, "CRC check failed for reportID=%d\n", report_id);
527 return -EILSEQ;
528 }
529 }
530
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -0800531 return 0;
532}
533
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -0700534/* Register a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */
535static int ps_lightbar_register(struct ps_device *ps_dev, struct led_classdev_mc *lightbar_mc_dev,
536 int (*brightness_set)(struct led_classdev *, enum led_brightness))
537{
538 struct hid_device *hdev = ps_dev->hdev;
539 struct mc_subled *mc_led_info;
540 struct led_classdev *led_cdev;
541 int ret;
542
543 mc_led_info = devm_kmalloc_array(&hdev->dev, 3, sizeof(*mc_led_info),
544 GFP_KERNEL | __GFP_ZERO);
545 if (!mc_led_info)
546 return -ENOMEM;
547
548 mc_led_info[0].color_index = LED_COLOR_ID_RED;
549 mc_led_info[1].color_index = LED_COLOR_ID_GREEN;
550 mc_led_info[2].color_index = LED_COLOR_ID_BLUE;
551
552 lightbar_mc_dev->subled_info = mc_led_info;
553 lightbar_mc_dev->num_colors = 3;
554
555 led_cdev = &lightbar_mc_dev->led_cdev;
556 led_cdev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s:rgb:indicator",
557 ps_dev->input_dev_name);
558 if (!led_cdev->name)
559 return -ENOMEM;
560 led_cdev->brightness = 255;
561 led_cdev->max_brightness = 255;
562 led_cdev->brightness_set_blocking = brightness_set;
563
564 ret = devm_led_classdev_multicolor_register(&hdev->dev, lightbar_mc_dev);
565 if (ret < 0) {
566 hid_err(hdev, "Cannot register multicolor LED device\n");
567 return ret;
568 }
569
570 return 0;
571}
572
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800573static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res,
574 int gyro_range, int gyro_res)
575{
576 struct input_dev *sensors;
577 int ret;
578
579 sensors = ps_allocate_input_dev(hdev, "Motion Sensors");
580 if (IS_ERR(sensors))
581 return ERR_CAST(sensors);
582
583 __set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit);
584 __set_bit(EV_MSC, sensors->evbit);
585 __set_bit(MSC_TIMESTAMP, sensors->mscbit);
586
587 /* Accelerometer */
588 input_set_abs_params(sensors, ABS_X, -accel_range, accel_range, 16, 0);
589 input_set_abs_params(sensors, ABS_Y, -accel_range, accel_range, 16, 0);
590 input_set_abs_params(sensors, ABS_Z, -accel_range, accel_range, 16, 0);
591 input_abs_set_res(sensors, ABS_X, accel_res);
592 input_abs_set_res(sensors, ABS_Y, accel_res);
593 input_abs_set_res(sensors, ABS_Z, accel_res);
594
595 /* Gyroscope */
596 input_set_abs_params(sensors, ABS_RX, -gyro_range, gyro_range, 16, 0);
597 input_set_abs_params(sensors, ABS_RY, -gyro_range, gyro_range, 16, 0);
598 input_set_abs_params(sensors, ABS_RZ, -gyro_range, gyro_range, 16, 0);
599 input_abs_set_res(sensors, ABS_RX, gyro_res);
600 input_abs_set_res(sensors, ABS_RY, gyro_res);
601 input_abs_set_res(sensors, ABS_RZ, gyro_res);
602
603 ret = input_register_device(sensors);
604 if (ret)
605 return ERR_PTR(ret);
606
607 return sensors;
608}
609
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800610static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width, int height,
611 unsigned int num_contacts)
612{
613 struct input_dev *touchpad;
614 int ret;
615
616 touchpad = ps_allocate_input_dev(hdev, "Touchpad");
617 if (IS_ERR(touchpad))
618 return ERR_CAST(touchpad);
619
620 /* Map button underneath touchpad to BTN_LEFT. */
621 input_set_capability(touchpad, EV_KEY, BTN_LEFT);
622 __set_bit(INPUT_PROP_BUTTONPAD, touchpad->propbit);
623
624 input_set_abs_params(touchpad, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
625 input_set_abs_params(touchpad, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
626
627 ret = input_mt_init_slots(touchpad, num_contacts, INPUT_MT_POINTER);
628 if (ret)
629 return ERR_PTR(ret);
630
631 ret = input_register_device(touchpad);
632 if (ret)
633 return ERR_PTR(ret);
634
635 return touchpad;
636}
637
Roderick Colenbrander0b25b552021-02-07 13:49:08 -0800638static ssize_t firmware_version_show(struct device *dev,
639 struct device_attribute
640 *attr, char *buf)
641{
642 struct hid_device *hdev = to_hid_device(dev);
643 struct ps_device *ps_dev = hid_get_drvdata(hdev);
644
645 return sysfs_emit(buf, "0x%08x\n", ps_dev->fw_version);
646}
647
648static DEVICE_ATTR_RO(firmware_version);
649
650static ssize_t hardware_version_show(struct device *dev,
651 struct device_attribute
652 *attr, char *buf)
653{
654 struct hid_device *hdev = to_hid_device(dev);
655 struct ps_device *ps_dev = hid_get_drvdata(hdev);
656
657 return sysfs_emit(buf, "0x%08x\n", ps_dev->hw_version);
658}
659
660static DEVICE_ATTR_RO(hardware_version);
661
662static struct attribute *ps_device_attributes[] = {
663 &dev_attr_firmware_version.attr,
664 &dev_attr_hardware_version.attr,
665 NULL
666};
667
668static const struct attribute_group ps_device_attribute_group = {
669 .attrs = ps_device_attributes,
670};
671
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800672static int dualsense_get_calibration_data(struct dualsense *ds)
673{
674 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
675 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
676 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
677 short gyro_speed_plus, gyro_speed_minus;
678 short acc_x_plus, acc_x_minus;
679 short acc_y_plus, acc_y_minus;
680 short acc_z_plus, acc_z_minus;
681 int speed_2x;
682 int range_2g;
683 int ret = 0;
684 uint8_t *buf;
685
686 buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL);
687 if (!buf)
688 return -ENOMEM;
689
690 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_CALIBRATION, buf,
691 DS_FEATURE_REPORT_CALIBRATION_SIZE);
692 if (ret) {
693 hid_err(ds->base.hdev, "Failed to retrieve DualSense calibration info: %d\n", ret);
694 goto err_free;
695 }
696
697 gyro_pitch_bias = get_unaligned_le16(&buf[1]);
698 gyro_yaw_bias = get_unaligned_le16(&buf[3]);
699 gyro_roll_bias = get_unaligned_le16(&buf[5]);
700 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
701 gyro_pitch_minus = get_unaligned_le16(&buf[9]);
702 gyro_yaw_plus = get_unaligned_le16(&buf[11]);
703 gyro_yaw_minus = get_unaligned_le16(&buf[13]);
704 gyro_roll_plus = get_unaligned_le16(&buf[15]);
705 gyro_roll_minus = get_unaligned_le16(&buf[17]);
706 gyro_speed_plus = get_unaligned_le16(&buf[19]);
707 gyro_speed_minus = get_unaligned_le16(&buf[21]);
708 acc_x_plus = get_unaligned_le16(&buf[23]);
709 acc_x_minus = get_unaligned_le16(&buf[25]);
710 acc_y_plus = get_unaligned_le16(&buf[27]);
711 acc_y_minus = get_unaligned_le16(&buf[29]);
712 acc_z_plus = get_unaligned_le16(&buf[31]);
713 acc_z_minus = get_unaligned_le16(&buf[33]);
714
715 /*
716 * Set gyroscope calibration and normalization parameters.
717 * Data values will be normalized to 1/DS_GYRO_RES_PER_DEG_S degree/s.
718 */
719 speed_2x = (gyro_speed_plus + gyro_speed_minus);
720 ds->gyro_calib_data[0].abs_code = ABS_RX;
721 ds->gyro_calib_data[0].bias = gyro_pitch_bias;
722 ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
723 ds->gyro_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
724
725 ds->gyro_calib_data[1].abs_code = ABS_RY;
726 ds->gyro_calib_data[1].bias = gyro_yaw_bias;
727 ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
728 ds->gyro_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
729
730 ds->gyro_calib_data[2].abs_code = ABS_RZ;
731 ds->gyro_calib_data[2].bias = gyro_roll_bias;
732 ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S;
733 ds->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
734
735 /*
736 * Set accelerometer calibration and normalization parameters.
737 * Data values will be normalized to 1/DS_ACC_RES_PER_G g.
738 */
739 range_2g = acc_x_plus - acc_x_minus;
740 ds->accel_calib_data[0].abs_code = ABS_X;
741 ds->accel_calib_data[0].bias = acc_x_plus - range_2g / 2;
742 ds->accel_calib_data[0].sens_numer = 2*DS_ACC_RES_PER_G;
743 ds->accel_calib_data[0].sens_denom = range_2g;
744
745 range_2g = acc_y_plus - acc_y_minus;
746 ds->accel_calib_data[1].abs_code = ABS_Y;
747 ds->accel_calib_data[1].bias = acc_y_plus - range_2g / 2;
748 ds->accel_calib_data[1].sens_numer = 2*DS_ACC_RES_PER_G;
749 ds->accel_calib_data[1].sens_denom = range_2g;
750
751 range_2g = acc_z_plus - acc_z_minus;
752 ds->accel_calib_data[2].abs_code = ABS_Z;
753 ds->accel_calib_data[2].bias = acc_z_plus - range_2g / 2;
754 ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G;
755 ds->accel_calib_data[2].sens_denom = range_2g;
756
757err_free:
758 kfree(buf);
759 return ret;
760}
761
Roderick Colenbrander0b25b552021-02-07 13:49:08 -0800762static int dualsense_get_firmware_info(struct dualsense *ds)
763{
764 uint8_t *buf;
765 int ret;
766
767 buf = kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL);
768 if (!buf)
769 return -ENOMEM;
770
771 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_FIRMWARE_INFO, buf,
772 DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE);
773 if (ret) {
774 hid_err(ds->base.hdev, "Failed to retrieve DualSense firmware info: %d\n", ret);
775 goto err_free;
776 }
777
778 ds->base.hw_version = get_unaligned_le32(&buf[24]);
779 ds->base.fw_version = get_unaligned_le32(&buf[28]);
780
781err_free:
782 kfree(buf);
783 return ret;
784}
785
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -0800786static int dualsense_get_mac_address(struct dualsense *ds)
787{
788 uint8_t *buf;
789 int ret = 0;
790
791 buf = kzalloc(DS_FEATURE_REPORT_PAIRING_INFO_SIZE, GFP_KERNEL);
792 if (!buf)
793 return -ENOMEM;
794
795 ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_PAIRING_INFO, buf,
796 DS_FEATURE_REPORT_PAIRING_INFO_SIZE);
797 if (ret) {
798 hid_err(ds->base.hdev, "Failed to retrieve DualSense pairing info: %d\n", ret);
799 goto err_free;
800 }
801
802 memcpy(ds->base.mac_address, &buf[1], sizeof(ds->base.mac_address));
803
804err_free:
805 kfree(buf);
806 return ret;
807}
808
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -0700809static int dualsense_lightbar_set_brightness(struct led_classdev *cdev,
810 enum led_brightness brightness)
811{
812 struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
813 struct dualsense *ds = container_of(mc_cdev, struct dualsense, lightbar);
814 uint8_t red, green, blue;
815
816 led_mc_calc_color_components(mc_cdev, brightness);
817 red = mc_cdev->subled_info[0].brightness;
818 green = mc_cdev->subled_info[1].brightness;
819 blue = mc_cdev->subled_info[2].brightness;
820
821 dualsense_set_lightbar(ds, red, green, blue);
822 return 0;
823}
824
Roderick Colenbrander51151092021-02-07 13:49:03 -0800825static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp,
826 void *buf)
827{
828 struct hid_device *hdev = ds->base.hdev;
829
830 if (hdev->bus == BUS_BLUETOOTH) {
831 struct dualsense_output_report_bt *bt = buf;
832
833 memset(bt, 0, sizeof(*bt));
834 bt->report_id = DS_OUTPUT_REPORT_BT;
835 bt->tag = DS_OUTPUT_TAG; /* Tag must be set. Exact meaning is unclear. */
836
837 /*
838 * Highest 4-bit is a sequence number, which needs to be increased
839 * every report. Lowest 4-bit is tag and can be zero for now.
840 */
841 bt->seq_tag = (ds->output_seq << 4) | 0x0;
842 if (++ds->output_seq == 16)
843 ds->output_seq = 0;
844
845 rp->data = buf;
846 rp->len = sizeof(*bt);
847 rp->bt = bt;
848 rp->usb = NULL;
849 rp->common = &bt->common;
850 } else { /* USB */
851 struct dualsense_output_report_usb *usb = buf;
852
853 memset(usb, 0, sizeof(*usb));
854 usb->report_id = DS_OUTPUT_REPORT_USB;
855
856 rp->data = buf;
857 rp->len = sizeof(*usb);
858 rp->bt = NULL;
859 rp->usb = usb;
860 rp->common = &usb->common;
861 }
862}
863
864/*
865 * Helper function to send DualSense output reports. Applies a CRC at the end of a report
866 * for Bluetooth reports.
867 */
868static void dualsense_send_output_report(struct dualsense *ds,
869 struct dualsense_output_report *report)
870{
871 struct hid_device *hdev = ds->base.hdev;
872
873 /* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */
874 if (report->bt) {
875 uint32_t crc;
876 uint8_t seed = PS_OUTPUT_CRC32_SEED;
877
878 crc = crc32_le(0xFFFFFFFF, &seed, 1);
879 crc = ~crc32_le(crc, report->data, report->len - 4);
880
881 report->bt->crc32 = cpu_to_le32(crc);
882 }
883
884 hid_hw_output_report(hdev, report->data, report->len);
885}
886
887static void dualsense_output_worker(struct work_struct *work)
888{
889 struct dualsense *ds = container_of(work, struct dualsense, output_worker);
890 struct dualsense_output_report report;
891 struct dualsense_output_report_common *common;
892 unsigned long flags;
893
894 dualsense_init_output_report(ds, &report, ds->output_report_dmabuf);
895 common = report.common;
896
897 spin_lock_irqsave(&ds->base.lock, flags);
898
899 if (ds->update_rumble) {
900 /* Select classic rumble style haptics and enable it. */
901 common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT;
902 common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION;
903 common->motor_left = ds->motor_left;
904 common->motor_right = ds->motor_right;
905 ds->update_rumble = false;
906 }
907
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -0800908 if (ds->update_lightbar) {
909 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE;
910 common->lightbar_red = ds->lightbar_red;
911 common->lightbar_green = ds->lightbar_green;
912 common->lightbar_blue = ds->lightbar_blue;
913
914 ds->update_lightbar = false;
915 }
916
Roderick Colenbrander949aacc2021-02-16 16:56:09 -0800917 if (ds->update_player_leds) {
918 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE;
919 common->player_leds = ds->player_leds_state;
920
921 ds->update_player_leds = false;
922 }
923
Roderick Colenbranderc26e48b2021-02-16 16:50:07 -0800924 if (ds->update_mic_mute) {
925 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE;
926 common->mute_button_led = ds->mic_muted;
927
928 if (ds->mic_muted) {
929 /* Disable microphone */
930 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
931 common->power_save_control |= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
932 } else {
933 /* Enable microphone */
934 common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
935 common->power_save_control &= ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
936 }
937
938 ds->update_mic_mute = false;
939 }
940
Roderick Colenbrander51151092021-02-07 13:49:03 -0800941 spin_unlock_irqrestore(&ds->base.lock, flags);
942
943 dualsense_send_output_report(ds, &report);
944}
945
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800946static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *report,
947 u8 *data, int size)
948{
949 struct hid_device *hdev = ps_dev->hdev;
950 struct dualsense *ds = container_of(ps_dev, struct dualsense, base);
951 struct dualsense_input_report *ds_report;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800952 uint8_t battery_data, battery_capacity, charging_status, value;
953 int battery_status;
Roderick Colenbrander402987c2021-02-07 13:49:00 -0800954 uint32_t sensor_timestamp;
Roderick Colenbranderc26e48b2021-02-16 16:50:07 -0800955 bool btn_mic_state;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -0800956 unsigned long flags;
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -0800957 int i;
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800958
959 /*
960 * DualSense in USB uses the full HID report for reportID 1, but
961 * Bluetooth uses a minimal HID report for reportID 1 and reports
962 * the full report using reportID 49.
963 */
964 if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB &&
965 size == DS_INPUT_REPORT_USB_SIZE) {
966 ds_report = (struct dualsense_input_report *)&data[1];
Roderick Colenbrander799b2b52021-02-07 13:49:02 -0800967 } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT &&
968 size == DS_INPUT_REPORT_BT_SIZE) {
969 /* Last 4 bytes of input report contain crc32 */
970 uint32_t report_crc = get_unaligned_le32(&data[size - 4]);
971
972 if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) {
973 hid_err(hdev, "DualSense input CRC's check failed\n");
974 return -EILSEQ;
975 }
976
977 ds_report = (struct dualsense_input_report *)&data[2];
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800978 } else {
979 hid_err(hdev, "Unhandled reportID=%d\n", report->id);
980 return -1;
981 }
982
983 input_report_abs(ds->gamepad, ABS_X, ds_report->x);
984 input_report_abs(ds->gamepad, ABS_Y, ds_report->y);
985 input_report_abs(ds->gamepad, ABS_RX, ds_report->rx);
986 input_report_abs(ds->gamepad, ABS_RY, ds_report->ry);
987 input_report_abs(ds->gamepad, ABS_Z, ds_report->z);
988 input_report_abs(ds->gamepad, ABS_RZ, ds_report->rz);
989
990 value = ds_report->buttons[0] & DS_BUTTONS0_HAT_SWITCH;
Colin Ian King50ab1ff2021-02-15 16:39:21 +0000991 if (value >= ARRAY_SIZE(ps_gamepad_hat_mapping))
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -0800992 value = 8; /* center */
993 input_report_abs(ds->gamepad, ABS_HAT0X, ps_gamepad_hat_mapping[value].x);
994 input_report_abs(ds->gamepad, ABS_HAT0Y, ps_gamepad_hat_mapping[value].y);
995
996 input_report_key(ds->gamepad, BTN_WEST, ds_report->buttons[0] & DS_BUTTONS0_SQUARE);
997 input_report_key(ds->gamepad, BTN_SOUTH, ds_report->buttons[0] & DS_BUTTONS0_CROSS);
998 input_report_key(ds->gamepad, BTN_EAST, ds_report->buttons[0] & DS_BUTTONS0_CIRCLE);
999 input_report_key(ds->gamepad, BTN_NORTH, ds_report->buttons[0] & DS_BUTTONS0_TRIANGLE);
1000 input_report_key(ds->gamepad, BTN_TL, ds_report->buttons[1] & DS_BUTTONS1_L1);
1001 input_report_key(ds->gamepad, BTN_TR, ds_report->buttons[1] & DS_BUTTONS1_R1);
1002 input_report_key(ds->gamepad, BTN_TL2, ds_report->buttons[1] & DS_BUTTONS1_L2);
1003 input_report_key(ds->gamepad, BTN_TR2, ds_report->buttons[1] & DS_BUTTONS1_R2);
1004 input_report_key(ds->gamepad, BTN_SELECT, ds_report->buttons[1] & DS_BUTTONS1_CREATE);
1005 input_report_key(ds->gamepad, BTN_START, ds_report->buttons[1] & DS_BUTTONS1_OPTIONS);
1006 input_report_key(ds->gamepad, BTN_THUMBL, ds_report->buttons[1] & DS_BUTTONS1_L3);
1007 input_report_key(ds->gamepad, BTN_THUMBR, ds_report->buttons[1] & DS_BUTTONS1_R3);
1008 input_report_key(ds->gamepad, BTN_MODE, ds_report->buttons[2] & DS_BUTTONS2_PS_HOME);
1009 input_sync(ds->gamepad);
1010
Roderick Colenbranderc26e48b2021-02-16 16:50:07 -08001011 /*
1012 * The DualSense has an internal microphone, which can be muted through a mute button
1013 * on the device. The driver is expected to read the button state and program the device
1014 * to mute/unmute audio at the hardware level.
1015 */
1016 btn_mic_state = !!(ds_report->buttons[2] & DS_BUTTONS2_MIC_MUTE);
1017 if (btn_mic_state && !ds->last_btn_mic_state) {
1018 spin_lock_irqsave(&ps_dev->lock, flags);
1019 ds->update_mic_mute = true;
1020 ds->mic_muted = !ds->mic_muted; /* toggle */
1021 spin_unlock_irqrestore(&ps_dev->lock, flags);
1022
1023 /* Schedule updating of microphone state at hardware level. */
1024 schedule_work(&ds->output_worker);
1025 }
1026 ds->last_btn_mic_state = btn_mic_state;
1027
Roderick Colenbrander402987c2021-02-07 13:49:00 -08001028 /* Parse and calibrate gyroscope data. */
1029 for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) {
1030 int raw_data = (short)le16_to_cpu(ds_report->gyro[i]);
1031 int calib_data = mult_frac(ds->gyro_calib_data[i].sens_numer,
1032 raw_data - ds->gyro_calib_data[i].bias,
1033 ds->gyro_calib_data[i].sens_denom);
1034
1035 input_report_abs(ds->sensors, ds->gyro_calib_data[i].abs_code, calib_data);
1036 }
1037
1038 /* Parse and calibrate accelerometer data. */
1039 for (i = 0; i < ARRAY_SIZE(ds_report->accel); i++) {
1040 int raw_data = (short)le16_to_cpu(ds_report->accel[i]);
1041 int calib_data = mult_frac(ds->accel_calib_data[i].sens_numer,
1042 raw_data - ds->accel_calib_data[i].bias,
1043 ds->accel_calib_data[i].sens_denom);
1044
1045 input_report_abs(ds->sensors, ds->accel_calib_data[i].abs_code, calib_data);
1046 }
1047
1048 /* Convert timestamp (in 0.33us unit) to timestamp_us */
1049 sensor_timestamp = le32_to_cpu(ds_report->sensor_timestamp);
1050 if (!ds->sensor_timestamp_initialized) {
1051 ds->sensor_timestamp_us = DIV_ROUND_CLOSEST(sensor_timestamp, 3);
1052 ds->sensor_timestamp_initialized = true;
1053 } else {
1054 uint32_t delta;
1055
1056 if (ds->prev_sensor_timestamp > sensor_timestamp)
1057 delta = (U32_MAX - ds->prev_sensor_timestamp + sensor_timestamp + 1);
1058 else
1059 delta = sensor_timestamp - ds->prev_sensor_timestamp;
1060 ds->sensor_timestamp_us += DIV_ROUND_CLOSEST(delta, 3);
1061 }
1062 ds->prev_sensor_timestamp = sensor_timestamp;
1063 input_event(ds->sensors, EV_MSC, MSC_TIMESTAMP, ds->sensor_timestamp_us);
1064 input_sync(ds->sensors);
1065
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -08001066 for (i = 0; i < ARRAY_SIZE(ds_report->points); i++) {
1067 struct dualsense_touch_point *point = &ds_report->points[i];
1068 bool active = (point->contact & DS_TOUCH_POINT_INACTIVE) ? false : true;
1069
1070 input_mt_slot(ds->touchpad, i);
1071 input_mt_report_slot_state(ds->touchpad, MT_TOOL_FINGER, active);
1072
1073 if (active) {
1074 int x = (point->x_hi << 8) | point->x_lo;
1075 int y = (point->y_hi << 4) | point->y_lo;
1076
1077 input_report_abs(ds->touchpad, ABS_MT_POSITION_X, x);
1078 input_report_abs(ds->touchpad, ABS_MT_POSITION_Y, y);
1079 }
1080 }
1081 input_mt_sync_frame(ds->touchpad);
1082 input_report_key(ds->touchpad, BTN_LEFT, ds_report->buttons[2] & DS_BUTTONS2_TOUCHPAD);
1083 input_sync(ds->touchpad);
1084
Roderick Colenbranderd30bca42021-02-07 13:48:58 -08001085 battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY;
1086 charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT;
1087
1088 switch (charging_status) {
1089 case 0x0:
1090 /*
1091 * Each unit of battery data corresponds to 10%
1092 * 0 = 0-9%, 1 = 10-19%, .. and 10 = 100%
1093 */
1094 battery_capacity = min(battery_data * 10 + 5, 100);
1095 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
1096 break;
1097 case 0x1:
1098 battery_capacity = min(battery_data * 10 + 5, 100);
1099 battery_status = POWER_SUPPLY_STATUS_CHARGING;
1100 break;
1101 case 0x2:
1102 battery_capacity = 100;
1103 battery_status = POWER_SUPPLY_STATUS_FULL;
1104 break;
1105 case 0xa: /* voltage or temperature out of range */
1106 case 0xb: /* temperature error */
1107 battery_capacity = 0;
1108 battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1109 break;
1110 case 0xf: /* charging error */
1111 default:
1112 battery_capacity = 0;
1113 battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
1114 }
1115
1116 spin_lock_irqsave(&ps_dev->lock, flags);
1117 ps_dev->battery_capacity = battery_capacity;
1118 ps_dev->battery_status = battery_status;
1119 spin_unlock_irqrestore(&ps_dev->lock, flags);
1120
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001121 return 0;
1122}
1123
Roderick Colenbrander51151092021-02-07 13:49:03 -08001124static int dualsense_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
1125{
1126 struct hid_device *hdev = input_get_drvdata(dev);
1127 struct dualsense *ds = hid_get_drvdata(hdev);
1128 unsigned long flags;
1129
1130 if (effect->type != FF_RUMBLE)
1131 return 0;
1132
1133 spin_lock_irqsave(&ds->base.lock, flags);
1134 ds->update_rumble = true;
1135 ds->motor_left = effect->u.rumble.strong_magnitude / 256;
1136 ds->motor_right = effect->u.rumble.weak_magnitude / 256;
1137 spin_unlock_irqrestore(&ds->base.lock, flags);
1138
1139 schedule_work(&ds->output_worker);
1140 return 0;
1141}
1142
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -08001143static int dualsense_reset_leds(struct dualsense *ds)
1144{
1145 struct dualsense_output_report report;
1146 uint8_t *buf;
1147
1148 buf = kzalloc(sizeof(struct dualsense_output_report_bt), GFP_KERNEL);
1149 if (!buf)
1150 return -ENOMEM;
1151
1152 dualsense_init_output_report(ds, &report, buf);
1153 /*
1154 * On Bluetooth the DualSense outputs an animation on the lightbar
1155 * during startup and maintains a color afterwards. We need to explicitly
1156 * reconfigure the lightbar before we can do any programming later on.
1157 * In USB the lightbar is not on by default, but redoing the setup there
1158 * doesn't hurt.
1159 */
1160 report.common->valid_flag2 = DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE;
1161 report.common->lightbar_setup = DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT; /* Fade light out. */
1162 dualsense_send_output_report(ds, &report);
1163
1164 kfree(buf);
1165 return 0;
1166}
1167
1168static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue)
1169{
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -07001170 unsigned long flags;
1171
1172 spin_lock_irqsave(&ds->base.lock, flags);
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -08001173 ds->update_lightbar = true;
1174 ds->lightbar_red = red;
1175 ds->lightbar_green = green;
1176 ds->lightbar_blue = blue;
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -07001177 spin_unlock_irqrestore(&ds->base.lock, flags);
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -08001178
1179 schedule_work(&ds->output_worker);
1180}
1181
Roderick Colenbrander949aacc2021-02-16 16:56:09 -08001182static void dualsense_set_player_leds(struct dualsense *ds)
1183{
1184 /*
1185 * The DualSense controller has a row of 5 LEDs used for player ids.
1186 * Behavior on the PlayStation 5 console is to center the player id
1187 * across the LEDs, so e.g. player 1 would be "--x--" with x being 'on'.
1188 * Follow a similar mapping here.
1189 */
1190 static const int player_ids[5] = {
1191 BIT(2),
1192 BIT(3) | BIT(1),
1193 BIT(4) | BIT(2) | BIT(0),
1194 BIT(4) | BIT(3) | BIT(1) | BIT(0),
1195 BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0)
1196 };
1197
1198 uint8_t player_id = ds->base.player_id % ARRAY_SIZE(player_ids);
1199
1200 ds->update_player_leds = true;
1201 ds->player_leds_state = player_ids[player_id];
1202 schedule_work(&ds->output_worker);
1203}
1204
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001205static struct ps_device *dualsense_create(struct hid_device *hdev)
1206{
1207 struct dualsense *ds;
Roderick Colenbranderd30bca42021-02-07 13:48:58 -08001208 struct ps_device *ps_dev;
Roderick Colenbrander51151092021-02-07 13:49:03 -08001209 uint8_t max_output_report_size;
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001210 int ret;
1211
1212 ds = devm_kzalloc(&hdev->dev, sizeof(*ds), GFP_KERNEL);
1213 if (!ds)
1214 return ERR_PTR(-ENOMEM);
1215
1216 /*
1217 * Patch version to allow userspace to distinguish between
1218 * hid-generic vs hid-playstation axis and button mapping.
1219 */
1220 hdev->version |= HID_PLAYSTATION_VERSION_PATCH;
1221
Roderick Colenbranderd30bca42021-02-07 13:48:58 -08001222 ps_dev = &ds->base;
1223 ps_dev->hdev = hdev;
1224 spin_lock_init(&ps_dev->lock);
1225 ps_dev->battery_capacity = 100; /* initial value until parse_report. */
1226 ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
1227 ps_dev->parse_report = dualsense_parse_report;
Roderick Colenbrander51151092021-02-07 13:49:03 -08001228 INIT_WORK(&ds->output_worker, dualsense_output_worker);
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001229 hid_set_drvdata(hdev, ds);
1230
Roderick Colenbrander51151092021-02-07 13:49:03 -08001231 max_output_report_size = sizeof(struct dualsense_output_report_bt);
1232 ds->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL);
1233 if (!ds->output_report_dmabuf)
1234 return ERR_PTR(-ENOMEM);
1235
Roderick Colenbranderb99dcef2021-02-07 13:48:57 -08001236 ret = dualsense_get_mac_address(ds);
1237 if (ret) {
1238 hid_err(hdev, "Failed to get MAC address from DualSense\n");
1239 return ERR_PTR(ret);
1240 }
1241 snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address);
1242
Roderick Colenbrander0b25b552021-02-07 13:49:08 -08001243 ret = dualsense_get_firmware_info(ds);
1244 if (ret) {
1245 hid_err(hdev, "Failed to get firmware info from DualSense\n");
1246 return ERR_PTR(ret);
1247 }
1248
Roderick Colenbrander53f04e82021-02-07 13:49:01 -08001249 ret = ps_devices_list_add(ps_dev);
1250 if (ret)
1251 return ERR_PTR(ret);
1252
Roderick Colenbrander402987c2021-02-07 13:49:00 -08001253 ret = dualsense_get_calibration_data(ds);
1254 if (ret) {
1255 hid_err(hdev, "Failed to get calibration data from DualSense\n");
1256 goto err;
1257 }
1258
Roderick Colenbrander51151092021-02-07 13:49:03 -08001259 ds->gamepad = ps_gamepad_create(hdev, dualsense_play_effect);
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001260 if (IS_ERR(ds->gamepad)) {
1261 ret = PTR_ERR(ds->gamepad);
1262 goto err;
1263 }
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -07001264 /* Use gamepad input device name as primary device name for e.g. LEDs */
1265 ps_dev->input_dev_name = dev_name(&ds->gamepad->dev);
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001266
Roderick Colenbrander402987c2021-02-07 13:49:00 -08001267 ds->sensors = ps_sensors_create(hdev, DS_ACC_RANGE, DS_ACC_RES_PER_G,
1268 DS_GYRO_RANGE, DS_GYRO_RES_PER_DEG_S);
1269 if (IS_ERR(ds->sensors)) {
1270 ret = PTR_ERR(ds->sensors);
1271 goto err;
1272 }
1273
Roderick Colenbranderf6bb05f2021-02-07 13:48:59 -08001274 ds->touchpad = ps_touchpad_create(hdev, DS_TOUCHPAD_WIDTH, DS_TOUCHPAD_HEIGHT, 2);
1275 if (IS_ERR(ds->touchpad)) {
1276 ret = PTR_ERR(ds->touchpad);
1277 goto err;
1278 }
1279
Roderick Colenbranderd30bca42021-02-07 13:48:58 -08001280 ret = ps_device_register_battery(ps_dev);
1281 if (ret)
1282 goto err;
1283
Roderick Colenbrander0b25b552021-02-07 13:49:08 -08001284 /*
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -08001285 * The hardware may have control over the LEDs (e.g. in Bluetooth on startup).
1286 * Reset the LEDs (lightbar, mute, player leds), so we can control them
1287 * from software.
1288 */
1289 ret = dualsense_reset_leds(ds);
1290 if (ret)
1291 goto err;
1292
Roderick Colenbranderfc97b4d2021-09-08 09:55:37 -07001293 ret = ps_lightbar_register(ps_dev, &ds->lightbar, dualsense_lightbar_set_brightness);
1294 if (ret)
1295 goto err;
1296
1297 /* Set default lightbar color. */
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -08001298 dualsense_set_lightbar(ds, 0, 0, 128); /* blue */
1299
Roderick Colenbrander949aacc2021-02-16 16:56:09 -08001300 ret = ps_device_set_player_id(ps_dev);
1301 if (ret) {
1302 hid_err(hdev, "Failed to assign player id for DualSense: %d\n", ret);
1303 goto err;
1304 }
1305
1306 /* Set player LEDs to our player id. */
1307 dualsense_set_player_leds(ds);
1308
Roderick Colenbrander8e5198a2021-02-16 14:26:35 -08001309 /*
Roderick Colenbrander0b25b552021-02-07 13:49:08 -08001310 * Reporting hardware and firmware is important as there are frequent updates, which
1311 * can change behavior.
1312 */
1313 hid_info(hdev, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n",
1314 ds->base.hw_version, ds->base.fw_version);
1315
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001316 return &ds->base;
1317
1318err:
Roderick Colenbrander53f04e82021-02-07 13:49:01 -08001319 ps_devices_list_remove(ps_dev);
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001320 return ERR_PTR(ret);
1321}
1322
1323static int ps_raw_event(struct hid_device *hdev, struct hid_report *report,
1324 u8 *data, int size)
1325{
1326 struct ps_device *dev = hid_get_drvdata(hdev);
1327
1328 if (dev && dev->parse_report)
1329 return dev->parse_report(dev, report, data, size);
1330
1331 return 0;
1332}
1333
1334static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id)
1335{
1336 struct ps_device *dev;
1337 int ret;
1338
1339 ret = hid_parse(hdev);
1340 if (ret) {
1341 hid_err(hdev, "Parse failed\n");
1342 return ret;
1343 }
1344
1345 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1346 if (ret) {
1347 hid_err(hdev, "Failed to start HID device\n");
1348 return ret;
1349 }
1350
1351 ret = hid_hw_open(hdev);
1352 if (ret) {
1353 hid_err(hdev, "Failed to open HID device\n");
1354 goto err_stop;
1355 }
1356
1357 if (hdev->product == USB_DEVICE_ID_SONY_PS5_CONTROLLER) {
1358 dev = dualsense_create(hdev);
1359 if (IS_ERR(dev)) {
1360 hid_err(hdev, "Failed to create dualsense.\n");
1361 ret = PTR_ERR(dev);
1362 goto err_close;
1363 }
1364 }
1365
Roderick Colenbrander0b25b552021-02-07 13:49:08 -08001366 ret = devm_device_add_group(&hdev->dev, &ps_device_attribute_group);
1367 if (ret) {
1368 hid_err(hdev, "Failed to register sysfs nodes.\n");
1369 goto err_close;
1370 }
1371
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001372 return ret;
1373
1374err_close:
1375 hid_hw_close(hdev);
1376err_stop:
1377 hid_hw_stop(hdev);
1378 return ret;
1379}
1380
1381static void ps_remove(struct hid_device *hdev)
1382{
Roderick Colenbrander53f04e82021-02-07 13:49:01 -08001383 struct ps_device *dev = hid_get_drvdata(hdev);
1384
1385 ps_devices_list_remove(dev);
Roderick Colenbrander949aacc2021-02-16 16:56:09 -08001386 ps_device_release_player_id(dev);
Roderick Colenbrander53f04e82021-02-07 13:49:01 -08001387
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001388 hid_hw_close(hdev);
1389 hid_hw_stop(hdev);
1390}
1391
1392static const struct hid_device_id ps_devices[] = {
Roderick Colenbrander799b2b52021-02-07 13:49:02 -08001393 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001394 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
1395 { }
1396};
1397MODULE_DEVICE_TABLE(hid, ps_devices);
1398
1399static struct hid_driver ps_driver = {
1400 .name = "playstation",
1401 .id_table = ps_devices,
1402 .probe = ps_probe,
1403 .remove = ps_remove,
1404 .raw_event = ps_raw_event,
1405};
1406
Roderick Colenbrander949aacc2021-02-16 16:56:09 -08001407static int __init ps_init(void)
1408{
1409 return hid_register_driver(&ps_driver);
1410}
1411
1412static void __exit ps_exit(void)
1413{
1414 hid_unregister_driver(&ps_driver);
1415 ida_destroy(&ps_player_id_allocator);
1416}
1417
1418module_init(ps_init);
1419module_exit(ps_exit);
Roderick Colenbranderbc2e15a2021-02-07 13:48:56 -08001420
1421MODULE_AUTHOR("Sony Interactive Entertainment");
1422MODULE_DESCRIPTION("HID Driver for PlayStation peripherals.");
1423MODULE_LICENSE("GPL");