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