2 * HID driver for Sony / PS2 / PS3 / PS4 BD devices.
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2008 Jiri Slaby
8 * Copyright (c) 2012 David Dillow <dave@thedillows.org>
9 * Copyright (c) 2006-2013 Jiri Kosina
10 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
11 * Copyright (c) 2014-2016 Frank Praznik <frank.praznik@gmail.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at your option)
22 * NOTE: in order for the Sony PS3 BD Remote Control to be found by
23 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
24 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
26 * There will be no PIN request from the device.
29 #include <linux/device.h>
30 #include <linux/hid.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/leds.h>
34 #include <linux/power_supply.h>
35 #include <linux/spinlock.h>
36 #include <linux/list.h>
37 #include <linux/idr.h>
38 #include <linux/input/mt.h>
39 #include <linux/crc32.h>
40 #include <asm/unaligned.h>
44 #define VAIO_RDESC_CONSTANT BIT(0)
45 #define SIXAXIS_CONTROLLER_USB BIT(1)
46 #define SIXAXIS_CONTROLLER_BT BIT(2)
47 #define BUZZ_CONTROLLER BIT(3)
48 #define PS3REMOTE BIT(4)
49 #define DUALSHOCK4_CONTROLLER_USB BIT(5)
50 #define DUALSHOCK4_CONTROLLER_BT BIT(6)
51 #define DUALSHOCK4_DONGLE BIT(7)
52 #define MOTION_CONTROLLER_USB BIT(8)
53 #define MOTION_CONTROLLER_BT BIT(9)
54 #define NAVIGATION_CONTROLLER_USB BIT(10)
55 #define NAVIGATION_CONTROLLER_BT BIT(11)
56 #define SINO_LITE_CONTROLLER BIT(12)
57 #define FUTUREMAX_DANCE_MAT BIT(13)
59 #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
60 #define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
61 #define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
62 NAVIGATION_CONTROLLER_BT)
63 #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
64 DUALSHOCK4_CONTROLLER_BT | \
66 #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
67 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
68 NAVIGATION_CONTROLLER)
69 #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
70 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
71 #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
73 #define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\
74 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
79 /* PS/3 Motion controller */
80 static u8 motion_rdesc
[] = {
81 0x05, 0x01, /* Usage Page (Desktop), */
82 0x09, 0x04, /* Usage (Joystick), */
83 0xA1, 0x01, /* Collection (Application), */
84 0xA1, 0x02, /* Collection (Logical), */
85 0x85, 0x01, /* Report ID (1), */
86 0x75, 0x01, /* Report Size (1), */
87 0x95, 0x15, /* Report Count (21), */
88 0x15, 0x00, /* Logical Minimum (0), */
89 0x25, 0x01, /* Logical Maximum (1), */
90 0x35, 0x00, /* Physical Minimum (0), */
91 0x45, 0x01, /* Physical Maximum (1), */
92 0x05, 0x09, /* Usage Page (Button), */
93 0x19, 0x01, /* Usage Minimum (01h), */
94 0x29, 0x15, /* Usage Maximum (15h), */
95 0x81, 0x02, /* Input (Variable), * Buttons */
96 0x95, 0x0B, /* Report Count (11), */
97 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
98 0x81, 0x03, /* Input (Constant, Variable), * Padding */
99 0x15, 0x00, /* Logical Minimum (0), */
100 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
101 0x05, 0x01, /* Usage Page (Desktop), */
102 0xA1, 0x00, /* Collection (Physical), */
103 0x75, 0x08, /* Report Size (8), */
104 0x95, 0x01, /* Report Count (1), */
105 0x35, 0x00, /* Physical Minimum (0), */
106 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
107 0x09, 0x30, /* Usage (X), */
108 0x81, 0x02, /* Input (Variable), * Trigger */
109 0xC0, /* End Collection, */
110 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
111 0x75, 0x08, /* Report Size (8), */
112 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
113 0x81, 0x02, /* Input (Variable), */
114 0x05, 0x01, /* Usage Page (Desktop), */
115 0x75, 0x10, /* Report Size (16), */
116 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
117 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
118 0x95, 0x03, /* Report Count (3), * 3x Accels */
119 0x09, 0x33, /* Usage (rX), */
120 0x09, 0x34, /* Usage (rY), */
121 0x09, 0x35, /* Usage (rZ), */
122 0x81, 0x02, /* Input (Variable), */
123 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
124 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
125 0x81, 0x02, /* Input (Variable), */
126 0x05, 0x01, /* Usage Page (Desktop), */
127 0x09, 0x01, /* Usage (Pointer), */
128 0x95, 0x03, /* Report Count (3), * 3x Gyros */
129 0x81, 0x02, /* Input (Variable), */
130 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
131 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
132 0x81, 0x02, /* Input (Variable), */
133 0x75, 0x0C, /* Report Size (12), */
134 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
135 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
136 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
137 0x81, 0x02, /* Input (Variable), */
138 0x75, 0x08, /* Report Size (8), */
139 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
140 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
141 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
142 0x81, 0x02, /* Input (Variable), */
143 0x75, 0x08, /* Report Size (8), */
144 0x95, 0x30, /* Report Count (48), */
145 0x09, 0x01, /* Usage (Pointer), */
146 0x91, 0x02, /* Output (Variable), */
147 0x75, 0x08, /* Report Size (8), */
148 0x95, 0x30, /* Report Count (48), */
149 0x09, 0x01, /* Usage (Pointer), */
150 0xB1, 0x02, /* Feature (Variable), */
151 0xC0, /* End Collection, */
152 0xA1, 0x02, /* Collection (Logical), */
153 0x85, 0x02, /* Report ID (2), */
154 0x75, 0x08, /* Report Size (8), */
155 0x95, 0x30, /* Report Count (48), */
156 0x09, 0x01, /* Usage (Pointer), */
157 0xB1, 0x02, /* Feature (Variable), */
158 0xC0, /* End Collection, */
159 0xA1, 0x02, /* Collection (Logical), */
160 0x85, 0xEE, /* Report ID (238), */
161 0x75, 0x08, /* Report Size (8), */
162 0x95, 0x30, /* Report Count (48), */
163 0x09, 0x01, /* Usage (Pointer), */
164 0xB1, 0x02, /* Feature (Variable), */
165 0xC0, /* End Collection, */
166 0xA1, 0x02, /* Collection (Logical), */
167 0x85, 0xEF, /* Report ID (239), */
168 0x75, 0x08, /* Report Size (8), */
169 0x95, 0x30, /* Report Count (48), */
170 0x09, 0x01, /* Usage (Pointer), */
171 0xB1, 0x02, /* Feature (Variable), */
172 0xC0, /* End Collection, */
173 0xC0 /* End Collection */
176 static u8 ps3remote_rdesc
[] = {
177 0x05, 0x01, /* GUsagePage Generic Desktop */
178 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
179 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
181 /* Use collection 1 for joypad buttons */
182 0xA1, 0x02, /* MCollection Logical (interrelated data) */
185 * Ignore the 1st byte, maybe it is used for a controller
186 * number but it's not needed for correct operation
188 0x75, 0x08, /* GReportSize 0x08 [8] */
189 0x95, 0x01, /* GReportCount 0x01 [1] */
190 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
193 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
194 * buttons multiple keypresses are allowed
196 0x05, 0x09, /* GUsagePage Button */
197 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
198 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
199 0x14, /* GLogicalMinimum [0] */
200 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
201 0x75, 0x01, /* GReportSize 0x01 [1] */
202 0x95, 0x18, /* GReportCount 0x18 [24] */
203 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
205 0xC0, /* MEndCollection */
207 /* Use collection 2 for remote control buttons */
208 0xA1, 0x02, /* MCollection Logical (interrelated data) */
210 /* 5th byte is used for remote control buttons */
211 0x05, 0x09, /* GUsagePage Button */
212 0x18, /* LUsageMinimum [No button pressed] */
213 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
214 0x14, /* GLogicalMinimum [0] */
215 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
216 0x75, 0x08, /* GReportSize 0x08 [8] */
217 0x95, 0x01, /* GReportCount 0x01 [1] */
221 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
222 * 0xff and 11th is for press indication
224 0x75, 0x08, /* GReportSize 0x08 [8] */
225 0x95, 0x06, /* GReportCount 0x06 [6] */
226 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
228 /* 12th byte is for battery strength */
229 0x05, 0x06, /* GUsagePage Generic Device Controls */
230 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
231 0x14, /* GLogicalMinimum [0] */
232 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
233 0x75, 0x08, /* GReportSize 0x08 [8] */
234 0x95, 0x01, /* GReportCount 0x01 [1] */
235 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
237 0xC0, /* MEndCollection */
239 0xC0 /* MEndCollection [Game Pad] */
242 static const unsigned int ps3remote_keymap_joypad_buttons
[] = {
244 [0x02] = BTN_THUMBL
, /* L3 */
245 [0x03] = BTN_THUMBR
, /* R3 */
251 [0x09] = BTN_TL2
, /* L2 */
252 [0x0a] = BTN_TR2
, /* R2 */
253 [0x0b] = BTN_TL
, /* L1 */
254 [0x0c] = BTN_TR
, /* R1 */
255 [0x0d] = KEY_OPTION
, /* options/triangle */
256 [0x0e] = KEY_BACK
, /* back/circle */
257 [0x0f] = BTN_0
, /* cross */
258 [0x10] = KEY_SCREEN
, /* view/square */
259 [0x11] = KEY_HOMEPAGE
, /* PS button */
262 static const unsigned int ps3remote_keymap_remote_buttons
[] = {
273 [0x0e] = KEY_ESC
, /* return */
275 [0x16] = KEY_EJECTCD
,
276 [0x1a] = KEY_MENU
, /* top menu */
278 [0x30] = KEY_PREVIOUS
,
281 [0x33] = KEY_REWIND
, /* scan back */
282 [0x34] = KEY_FORWARD
, /* scan forward */
285 [0x40] = KEY_CONTEXT_MENU
, /* pop up/menu */
286 [0x60] = KEY_FRAMEBACK
, /* slow/step back */
287 [0x61] = KEY_FRAMEFORWARD
, /* slow/step forward */
288 [0x63] = KEY_SUBTITLE
,
291 [0x70] = KEY_INFO
, /* display */
298 static const unsigned int buzz_keymap
[] = {
300 * The controller has 4 remote buzzers, each with one LED and 5
303 * We use the mapping chosen by the controller, which is:
306 * -------------------
313 * So, for example, the orange button on the third buzzer is mapped to
314 * BTN_TRIGGER_HAPPY14
316 [1] = BTN_TRIGGER_HAPPY1
,
317 [2] = BTN_TRIGGER_HAPPY2
,
318 [3] = BTN_TRIGGER_HAPPY3
,
319 [4] = BTN_TRIGGER_HAPPY4
,
320 [5] = BTN_TRIGGER_HAPPY5
,
321 [6] = BTN_TRIGGER_HAPPY6
,
322 [7] = BTN_TRIGGER_HAPPY7
,
323 [8] = BTN_TRIGGER_HAPPY8
,
324 [9] = BTN_TRIGGER_HAPPY9
,
325 [10] = BTN_TRIGGER_HAPPY10
,
326 [11] = BTN_TRIGGER_HAPPY11
,
327 [12] = BTN_TRIGGER_HAPPY12
,
328 [13] = BTN_TRIGGER_HAPPY13
,
329 [14] = BTN_TRIGGER_HAPPY14
,
330 [15] = BTN_TRIGGER_HAPPY15
,
331 [16] = BTN_TRIGGER_HAPPY16
,
332 [17] = BTN_TRIGGER_HAPPY17
,
333 [18] = BTN_TRIGGER_HAPPY18
,
334 [19] = BTN_TRIGGER_HAPPY19
,
335 [20] = BTN_TRIGGER_HAPPY20
,
338 /* The Navigation controller is a partial DS3 and uses the same HID report
339 * and hence the same keymap indices, however not not all axes/buttons
340 * are physically present. We use the same axis and button mapping as
341 * the DS3, which uses the Linux gamepad spec.
343 static const unsigned int navigation_absmap
[] = {
346 [0x33] = ABS_Z
, /* L2 */
349 /* Buttons not physically available on the device, but still available
350 * in the reports are explicitly set to 0 for documentation purposes.
352 static const unsigned int navigation_keymap
[] = {
353 [0x01] = 0, /* Select */
354 [0x02] = BTN_THUMBL
, /* L3 */
356 [0x04] = 0, /* Start */
357 [0x05] = BTN_DPAD_UP
, /* Up */
358 [0x06] = BTN_DPAD_RIGHT
, /* Right */
359 [0x07] = BTN_DPAD_DOWN
, /* Down */
360 [0x08] = BTN_DPAD_LEFT
, /* Left */
361 [0x09] = BTN_TL2
, /* L2 */
363 [0x0b] = BTN_TL
, /* L1 */
365 [0x0d] = BTN_NORTH
, /* Triangle */
366 [0x0e] = BTN_EAST
, /* Circle */
367 [0x0f] = BTN_SOUTH
, /* Cross */
368 [0x10] = BTN_WEST
, /* Square */
369 [0x11] = BTN_MODE
, /* PS */
372 static const unsigned int sixaxis_absmap
[] = {
375 [0x32] = ABS_RX
, /* right stick X */
376 [0x35] = ABS_RY
, /* right stick Y */
379 static const unsigned int sixaxis_keymap
[] = {
380 [0x01] = BTN_SELECT
, /* Select */
381 [0x02] = BTN_THUMBL
, /* L3 */
382 [0x03] = BTN_THUMBR
, /* R3 */
383 [0x04] = BTN_START
, /* Start */
384 [0x05] = BTN_DPAD_UP
, /* Up */
385 [0x06] = BTN_DPAD_RIGHT
, /* Right */
386 [0x07] = BTN_DPAD_DOWN
, /* Down */
387 [0x08] = BTN_DPAD_LEFT
, /* Left */
388 [0x09] = BTN_TL2
, /* L2 */
389 [0x0a] = BTN_TR2
, /* R2 */
390 [0x0b] = BTN_TL
, /* L1 */
391 [0x0c] = BTN_TR
, /* R1 */
392 [0x0d] = BTN_NORTH
, /* Triangle */
393 [0x0e] = BTN_EAST
, /* Circle */
394 [0x0f] = BTN_SOUTH
, /* Cross */
395 [0x10] = BTN_WEST
, /* Square */
396 [0x11] = BTN_MODE
, /* PS */
399 static const unsigned int ds4_absmap
[] = {
402 [0x32] = ABS_RX
, /* right stick X */
403 [0x33] = ABS_Z
, /* L2 */
404 [0x34] = ABS_RZ
, /* R2 */
405 [0x35] = ABS_RY
, /* right stick Y */
408 static const unsigned int ds4_keymap
[] = {
409 [0x1] = BTN_WEST
, /* Square */
410 [0x2] = BTN_SOUTH
, /* Cross */
411 [0x3] = BTN_EAST
, /* Circle */
412 [0x4] = BTN_NORTH
, /* Triangle */
413 [0x5] = BTN_TL
, /* L1 */
414 [0x6] = BTN_TR
, /* R1 */
415 [0x7] = BTN_TL2
, /* L2 */
416 [0x8] = BTN_TR2
, /* R2 */
417 [0x9] = BTN_SELECT
, /* Share */
418 [0xa] = BTN_START
, /* Options */
419 [0xb] = BTN_THUMBL
, /* L3 */
420 [0xc] = BTN_THUMBR
, /* R3 */
421 [0xd] = BTN_MODE
, /* PS */
424 static const struct {int x
; int y
; } ds4_hat_mapping
[] = {
425 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
429 static enum power_supply_property sony_battery_props
[] = {
430 POWER_SUPPLY_PROP_PRESENT
,
431 POWER_SUPPLY_PROP_CAPACITY
,
432 POWER_SUPPLY_PROP_SCOPE
,
433 POWER_SUPPLY_PROP_STATUS
,
437 u8 time_enabled
; /* the total time the led is active (0xff means forever) */
438 u8 duty_length
; /* how long a cycle is in deciseconds (0 means "really fast") */
440 u8 duty_off
; /* % of duty_length the led is off (0xff means 100%) */
441 u8 duty_on
; /* % of duty_length the led is on (0xff mean 100%) */
444 struct sixaxis_rumble
{
446 u8 right_duration
; /* Right motor duration (0xff means forever) */
447 u8 right_motor_on
; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
448 u8 left_duration
; /* Left motor duration (0xff means forever) */
449 u8 left_motor_force
; /* left (large) motor, supports force values from 0 to 255 */
452 struct sixaxis_output_report
{
454 struct sixaxis_rumble rumble
;
456 u8 leds_bitmap
; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
457 struct sixaxis_led led
[4]; /* LEDx at (4 - x) */
458 struct sixaxis_led _reserved
; /* LED5, not actually soldered */
461 union sixaxis_output_report_01
{
462 struct sixaxis_output_report data
;
466 struct motion_output_report_02
{
473 #define DS4_FEATURE_REPORT_0x02_SIZE 37
474 #define DS4_FEATURE_REPORT_0x05_SIZE 41
475 #define DS4_FEATURE_REPORT_0x81_SIZE 7
476 #define DS4_INPUT_REPORT_0x11_SIZE 78
477 #define DS4_OUTPUT_REPORT_0x05_SIZE 32
478 #define DS4_OUTPUT_REPORT_0x11_SIZE 78
479 #define SIXAXIS_REPORT_0xF2_SIZE 17
480 #define SIXAXIS_REPORT_0xF5_SIZE 8
481 #define MOTION_REPORT_0x02_SIZE 49
483 /* Offsets relative to USB input report (0x1). Bluetooth (0x11) requires an
486 #define DS4_INPUT_REPORT_AXIS_OFFSET 1
487 #define DS4_INPUT_REPORT_BUTTON_OFFSET 5
488 #define DS4_INPUT_REPORT_TIMESTAMP_OFFSET 10
489 #define DS4_INPUT_REPORT_GYRO_X_OFFSET 13
490 #define DS4_INPUT_REPORT_BATTERY_OFFSET 30
491 #define DS4_INPUT_REPORT_TOUCHPAD_OFFSET 33
493 #define SENSOR_SUFFIX " Motion Sensors"
494 #define DS4_TOUCHPAD_SUFFIX " Touchpad"
496 /* Default to 4ms poll interval, which is same as USB (not adjustable). */
497 #define DS4_BT_DEFAULT_POLL_INTERVAL_MS 4
498 #define DS4_BT_MAX_POLL_INTERVAL_MS 62
499 #define DS4_GYRO_RES_PER_DEG_S 1024
500 #define DS4_ACC_RES_PER_G 8192
502 #define SIXAXIS_INPUT_REPORT_ACC_X_OFFSET 41
503 #define SIXAXIS_ACC_RES_PER_G 113
505 static DEFINE_SPINLOCK(sony_dev_list_lock
);
506 static LIST_HEAD(sony_device_list
);
507 static DEFINE_IDA(sony_device_id_allocator
);
509 /* Used for calibration of DS4 accelerometer and gyro. */
510 struct ds4_calibration_data
{
513 /* Calibration requires scaling against a sensitivity value, which is a
514 * float. Store sensitivity as a fraction to limit floating point
515 * calculations until final calibration.
521 enum ds4_dongle_state
{
535 struct list_head list_node
;
536 struct hid_device
*hdev
;
537 struct input_dev
*touchpad
;
538 struct input_dev
*sensor_dev
;
539 struct led_classdev
*leds
[MAX_LEDS
];
540 unsigned long quirks
;
541 struct work_struct hotplug_worker
;
542 struct work_struct state_worker
;
543 void (*send_output_report
)(struct sony_sc
*);
544 struct power_supply
*battery
;
545 struct power_supply_desc battery_desc
;
547 u8
*output_report_dmabuf
;
549 #ifdef CONFIG_SONY_FF
555 u8 hotplug_worker_initialized
;
556 u8 state_worker_initialized
;
557 u8 defer_initialization
;
561 u8 led_state
[MAX_LEDS
];
562 u8 led_delay_on
[MAX_LEDS
];
563 u8 led_delay_off
[MAX_LEDS
];
566 bool timestamp_initialized
;
568 unsigned int timestamp_us
;
570 u8 ds4_bt_poll_interval
;
571 enum ds4_dongle_state ds4_dongle_state
;
572 /* DS4 calibration data */
573 struct ds4_calibration_data ds4_calib_data
[6];
576 static void sony_set_leds(struct sony_sc
*sc
);
578 static inline void sony_schedule_work(struct sony_sc
*sc
,
579 enum sony_worker which
)
582 case SONY_WORKER_STATE
:
583 if (!sc
->defer_initialization
)
584 schedule_work(&sc
->state_worker
);
586 case SONY_WORKER_HOTPLUG
:
587 if (sc
->hotplug_worker_initialized
)
588 schedule_work(&sc
->hotplug_worker
);
593 static ssize_t
ds4_show_poll_interval(struct device
*dev
,
594 struct device_attribute
597 struct hid_device
*hdev
= to_hid_device(dev
);
598 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
600 return snprintf(buf
, PAGE_SIZE
, "%i\n", sc
->ds4_bt_poll_interval
);
603 static ssize_t
ds4_store_poll_interval(struct device
*dev
,
604 struct device_attribute
*attr
,
605 const char *buf
, size_t count
)
607 struct hid_device
*hdev
= to_hid_device(dev
);
608 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
612 if (kstrtou8(buf
, 0, &interval
))
615 if (interval
> DS4_BT_MAX_POLL_INTERVAL_MS
)
618 spin_lock_irqsave(&sc
->lock
, flags
);
619 sc
->ds4_bt_poll_interval
= interval
;
620 spin_unlock_irqrestore(&sc
->lock
, flags
);
622 sony_schedule_work(sc
, SONY_WORKER_STATE
);
627 static DEVICE_ATTR(bt_poll_interval
, 0644, ds4_show_poll_interval
,
628 ds4_store_poll_interval
);
631 static u8
*motion_fixup(struct hid_device
*hdev
, u8
*rdesc
,
634 *rsize
= sizeof(motion_rdesc
);
638 static u8
*ps3remote_fixup(struct hid_device
*hdev
, u8
*rdesc
,
641 *rsize
= sizeof(ps3remote_rdesc
);
642 return ps3remote_rdesc
;
645 static int ps3remote_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
646 struct hid_field
*field
, struct hid_usage
*usage
,
647 unsigned long **bit
, int *max
)
649 unsigned int key
= usage
->hid
& HID_USAGE
;
651 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_BUTTON
)
654 switch (usage
->collection_index
) {
656 if (key
>= ARRAY_SIZE(ps3remote_keymap_joypad_buttons
))
659 key
= ps3remote_keymap_joypad_buttons
[key
];
664 if (key
>= ARRAY_SIZE(ps3remote_keymap_remote_buttons
))
667 key
= ps3remote_keymap_remote_buttons
[key
];
675 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_KEY
, key
);
679 static int navigation_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
680 struct hid_field
*field
, struct hid_usage
*usage
,
681 unsigned long **bit
, int *max
)
683 if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
) {
684 unsigned int key
= usage
->hid
& HID_USAGE
;
686 if (key
>= ARRAY_SIZE(sixaxis_keymap
))
689 key
= navigation_keymap
[key
];
693 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_KEY
, key
);
695 } else if (usage
->hid
== HID_GD_POINTER
) {
696 /* See comment in sixaxis_mapping, basically the L2 (and R2)
697 * triggers are reported through GD Pointer.
698 * In addition we ignore any analog button 'axes' and only
699 * support digital buttons.
701 switch (usage
->usage_index
) {
703 usage
->hid
= HID_GD_Z
;
709 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_ABS
, usage
->hid
& 0xf);
711 } else if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_GENDESK
) {
712 unsigned int abs
= usage
->hid
& HID_USAGE
;
714 if (abs
>= ARRAY_SIZE(navigation_absmap
))
717 abs
= navigation_absmap
[abs
];
719 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_ABS
, abs
);
727 static int sixaxis_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
728 struct hid_field
*field
, struct hid_usage
*usage
,
729 unsigned long **bit
, int *max
)
731 if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
) {
732 unsigned int key
= usage
->hid
& HID_USAGE
;
734 if (key
>= ARRAY_SIZE(sixaxis_keymap
))
737 key
= sixaxis_keymap
[key
];
738 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_KEY
, key
);
740 } else if (usage
->hid
== HID_GD_POINTER
) {
741 /* The DS3 provides analog values for most buttons and even
742 * for HAT axes through GD Pointer. L2 and R2 are reported
743 * among these as well instead of as GD Z / RZ. Remap L2
744 * and R2 and ignore other analog 'button axes' as there is
745 * no good way for reporting them.
747 switch (usage
->usage_index
) {
749 usage
->hid
= HID_GD_Z
;
752 usage
->hid
= HID_GD_RZ
;
758 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_ABS
, usage
->hid
& 0xf);
760 } else if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_GENDESK
) {
761 unsigned int abs
= usage
->hid
& HID_USAGE
;
763 if (abs
>= ARRAY_SIZE(sixaxis_absmap
))
766 abs
= sixaxis_absmap
[abs
];
768 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_ABS
, abs
);
775 static int ds4_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
776 struct hid_field
*field
, struct hid_usage
*usage
,
777 unsigned long **bit
, int *max
)
779 if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
) {
780 unsigned int key
= usage
->hid
& HID_USAGE
;
782 if (key
>= ARRAY_SIZE(ds4_keymap
))
785 key
= ds4_keymap
[key
];
786 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_KEY
, key
);
788 } else if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_GENDESK
) {
789 unsigned int abs
= usage
->hid
& HID_USAGE
;
791 /* Let the HID parser deal with the HAT. */
792 if (usage
->hid
== HID_GD_HATSWITCH
)
795 if (abs
>= ARRAY_SIZE(ds4_absmap
))
798 abs
= ds4_absmap
[abs
];
799 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_ABS
, abs
);
806 static u8
*sony_report_fixup(struct hid_device
*hdev
, u8
*rdesc
,
809 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
811 if (sc
->quirks
& (SINO_LITE_CONTROLLER
| FUTUREMAX_DANCE_MAT
))
815 * Some Sony RF receivers wrongly declare the mouse pointer as a
816 * a constant non-data variable.
818 if ((sc
->quirks
& VAIO_RDESC_CONSTANT
) && *rsize
>= 56 &&
819 /* usage page: generic desktop controls */
820 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
822 rdesc
[2] == 0x09 && rdesc
[3] == 0x02 &&
823 /* input (usage page for x,y axes): constant, variable, relative */
824 rdesc
[54] == 0x81 && rdesc
[55] == 0x07) {
825 hid_info(hdev
, "Fixing up Sony RF Receiver report descriptor\n");
826 /* input: data, variable, relative */
830 if (sc
->quirks
& MOTION_CONTROLLER
)
831 return motion_fixup(hdev
, rdesc
, rsize
);
833 if (sc
->quirks
& PS3REMOTE
)
834 return ps3remote_fixup(hdev
, rdesc
, rsize
);
839 static void sixaxis_parse_report(struct sony_sc
*sc
, u8
*rd
, int size
)
841 static const u8 sixaxis_battery_capacity
[] = { 0, 1, 25, 50, 75, 100 };
844 u8 cable_state
, battery_capacity
, battery_charging
;
847 * The sixaxis is charging if the battery value is 0xee
848 * and it is fully charged if the value is 0xef.
849 * It does not report the actual level while charging so it
850 * is set to 100% while charging is in progress.
852 offset
= (sc
->quirks
& MOTION_CONTROLLER
) ? 12 : 30;
854 if (rd
[offset
] >= 0xee) {
855 battery_capacity
= 100;
856 battery_charging
= !(rd
[offset
] & 0x01);
859 u8 index
= rd
[offset
] <= 5 ? rd
[offset
] : 5;
860 battery_capacity
= sixaxis_battery_capacity
[index
];
861 battery_charging
= 0;
865 spin_lock_irqsave(&sc
->lock
, flags
);
866 sc
->cable_state
= cable_state
;
867 sc
->battery_capacity
= battery_capacity
;
868 sc
->battery_charging
= battery_charging
;
869 spin_unlock_irqrestore(&sc
->lock
, flags
);
871 if (sc
->quirks
& SIXAXIS_CONTROLLER
) {
874 offset
= SIXAXIS_INPUT_REPORT_ACC_X_OFFSET
;
875 val
= ((rd
[offset
+1] << 8) | rd
[offset
]) - 511;
876 input_report_abs(sc
->sensor_dev
, ABS_X
, val
);
878 /* Y and Z are swapped and inversed */
879 val
= 511 - ((rd
[offset
+5] << 8) | rd
[offset
+4]);
880 input_report_abs(sc
->sensor_dev
, ABS_Y
, val
);
882 val
= 511 - ((rd
[offset
+3] << 8) | rd
[offset
+2]);
883 input_report_abs(sc
->sensor_dev
, ABS_Z
, val
);
885 input_sync(sc
->sensor_dev
);
889 static void dualshock4_parse_report(struct sony_sc
*sc
, u8
*rd
, int size
)
891 struct hid_input
*hidinput
= list_entry(sc
->hdev
->inputs
.next
,
892 struct hid_input
, list
);
893 struct input_dev
*input_dev
= hidinput
->input
;
895 int n
, m
, offset
, num_touch_data
, max_touch_data
;
896 u8 cable_state
, battery_capacity
, battery_charging
;
899 /* When using Bluetooth the header is 2 bytes longer, so skip these. */
900 int data_offset
= (sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
) ? 2 : 0;
902 /* Second bit of third button byte is for the touchpad button. */
903 offset
= data_offset
+ DS4_INPUT_REPORT_BUTTON_OFFSET
;
904 input_report_key(sc
->touchpad
, BTN_LEFT
, rd
[offset
+2] & 0x2);
907 * The default behavior of the Dualshock 4 is to send reports using
908 * report type 1 when running over Bluetooth. However, when feature
909 * report 2 is requested during the controller initialization it starts
910 * sending input reports in report 17. Since report 17 is undefined
911 * in the default HID descriptor, the HID layer won't generate events.
912 * While it is possible (and this was done before) to fixup the HID
913 * descriptor to add this mapping, it was better to do this manually.
914 * The reason is there were various pieces software both open and closed
915 * source, relying on the descriptors to be the same across various
916 * operating systems. If the descriptors wouldn't match some
917 * applications e.g. games on Wine would not be able to function due
918 * to different descriptors, which such applications are not parsing.
923 offset
= data_offset
+ DS4_INPUT_REPORT_AXIS_OFFSET
;
924 input_report_abs(input_dev
, ABS_X
, rd
[offset
]);
925 input_report_abs(input_dev
, ABS_Y
, rd
[offset
+1]);
926 input_report_abs(input_dev
, ABS_RX
, rd
[offset
+2]);
927 input_report_abs(input_dev
, ABS_RY
, rd
[offset
+3]);
929 value
= rd
[offset
+4] & 0xf;
931 value
= 8; /* Center 0, 0 */
932 input_report_abs(input_dev
, ABS_HAT0X
, ds4_hat_mapping
[value
].x
);
933 input_report_abs(input_dev
, ABS_HAT0Y
, ds4_hat_mapping
[value
].y
);
935 input_report_key(input_dev
, BTN_WEST
, rd
[offset
+4] & 0x10);
936 input_report_key(input_dev
, BTN_SOUTH
, rd
[offset
+4] & 0x20);
937 input_report_key(input_dev
, BTN_EAST
, rd
[offset
+4] & 0x40);
938 input_report_key(input_dev
, BTN_NORTH
, rd
[offset
+4] & 0x80);
940 input_report_key(input_dev
, BTN_TL
, rd
[offset
+5] & 0x1);
941 input_report_key(input_dev
, BTN_TR
, rd
[offset
+5] & 0x2);
942 input_report_key(input_dev
, BTN_TL2
, rd
[offset
+5] & 0x4);
943 input_report_key(input_dev
, BTN_TR2
, rd
[offset
+5] & 0x8);
944 input_report_key(input_dev
, BTN_SELECT
, rd
[offset
+5] & 0x10);
945 input_report_key(input_dev
, BTN_START
, rd
[offset
+5] & 0x20);
946 input_report_key(input_dev
, BTN_THUMBL
, rd
[offset
+5] & 0x40);
947 input_report_key(input_dev
, BTN_THUMBR
, rd
[offset
+5] & 0x80);
949 input_report_key(input_dev
, BTN_MODE
, rd
[offset
+6] & 0x1);
951 input_report_abs(input_dev
, ABS_Z
, rd
[offset
+7]);
952 input_report_abs(input_dev
, ABS_RZ
, rd
[offset
+8]);
954 input_sync(input_dev
);
957 /* Convert timestamp (in 5.33us unit) to timestamp_us */
958 offset
= data_offset
+ DS4_INPUT_REPORT_TIMESTAMP_OFFSET
;
959 timestamp
= get_unaligned_le16(&rd
[offset
]);
960 if (!sc
->timestamp_initialized
) {
961 sc
->timestamp_us
= ((unsigned int)timestamp
* 16) / 3;
962 sc
->timestamp_initialized
= true;
966 if (sc
->prev_timestamp
> timestamp
)
967 delta
= (U16_MAX
- sc
->prev_timestamp
+ timestamp
+ 1);
969 delta
= timestamp
- sc
->prev_timestamp
;
970 sc
->timestamp_us
+= (delta
* 16) / 3;
972 sc
->prev_timestamp
= timestamp
;
973 input_event(sc
->sensor_dev
, EV_MSC
, MSC_TIMESTAMP
, sc
->timestamp_us
);
975 offset
= data_offset
+ DS4_INPUT_REPORT_GYRO_X_OFFSET
;
976 for (n
= 0; n
< 6; n
++) {
977 /* Store data in int for more precision during mult_frac. */
978 int raw_data
= (short)((rd
[offset
+1] << 8) | rd
[offset
]);
979 struct ds4_calibration_data
*calib
= &sc
->ds4_calib_data
[n
];
981 /* High precision is needed during calibration, but the
982 * calibrated values are within 32-bit.
983 * Note: we swap numerator 'x' and 'numer' in mult_frac for
984 * precision reasons so we don't need 64-bit.
986 int calib_data
= mult_frac(calib
->sens_numer
,
987 raw_data
- calib
->bias
,
990 input_report_abs(sc
->sensor_dev
, calib
->abs_code
, calib_data
);
993 input_sync(sc
->sensor_dev
);
996 * The lower 4 bits of byte 30 (or 32 for BT) contain the battery level
997 * and the 5th bit contains the USB cable state.
999 offset
= data_offset
+ DS4_INPUT_REPORT_BATTERY_OFFSET
;
1000 cable_state
= (rd
[offset
] >> 4) & 0x01;
1001 battery_capacity
= rd
[offset
] & 0x0F;
1004 * When a USB power source is connected the battery level ranges from
1005 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1006 * A battery level above 10 when plugged in means charge completed.
1008 if (!cable_state
|| battery_capacity
> 10)
1009 battery_charging
= 0;
1011 battery_charging
= 1;
1015 if (battery_capacity
> 10)
1016 battery_capacity
= 10;
1018 battery_capacity
*= 10;
1020 spin_lock_irqsave(&sc
->lock
, flags
);
1021 sc
->cable_state
= cable_state
;
1022 sc
->battery_capacity
= battery_capacity
;
1023 sc
->battery_charging
= battery_charging
;
1024 spin_unlock_irqrestore(&sc
->lock
, flags
);
1027 * The Dualshock 4 multi-touch trackpad data starts at offset 33 on USB
1028 * and 35 on Bluetooth.
1029 * The first byte indicates the number of touch data in the report.
1030 * Trackpad data starts 2 bytes later (e.g. 35 for USB).
1032 offset
= data_offset
+ DS4_INPUT_REPORT_TOUCHPAD_OFFSET
;
1033 max_touch_data
= (sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
) ? 4 : 3;
1034 if (rd
[offset
] > 0 && rd
[offset
] <= max_touch_data
)
1035 num_touch_data
= rd
[offset
];
1040 for (m
= 0; m
< num_touch_data
; m
++) {
1041 /* Skip past timestamp */
1045 * The first 7 bits of the first byte is a counter and bit 8 is
1046 * a touch indicator that is 0 when pressed and 1 when not
1048 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1049 * The data for the second touch is in the same format and
1050 * immediately follows the data for the first.
1052 for (n
= 0; n
< 2; n
++) {
1056 x
= rd
[offset
+1] | ((rd
[offset
+2] & 0xF) << 8);
1057 y
= ((rd
[offset
+2] & 0xF0) >> 4) | (rd
[offset
+3] << 4);
1059 active
= !(rd
[offset
] >> 7);
1060 input_mt_slot(sc
->touchpad
, n
);
1061 input_mt_report_slot_state(sc
->touchpad
, MT_TOOL_FINGER
, active
);
1064 input_report_abs(sc
->touchpad
, ABS_MT_POSITION_X
, x
);
1065 input_report_abs(sc
->touchpad
, ABS_MT_POSITION_Y
, y
);
1070 input_mt_sync_frame(sc
->touchpad
);
1071 input_sync(sc
->touchpad
);
1075 static int sony_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1078 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
1081 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
1082 * has to be BYTE_SWAPPED before passing up to joystick interface
1084 if ((sc
->quirks
& SIXAXIS_CONTROLLER
) && rd
[0] == 0x01 && size
== 49) {
1086 * When connected via Bluetooth the Sixaxis occasionally sends
1087 * a report with the second byte 0xff and the rest zeroed.
1089 * This report does not reflect the actual state of the
1090 * controller must be ignored to avoid generating false input
1096 swap(rd
[41], rd
[42]);
1097 swap(rd
[43], rd
[44]);
1098 swap(rd
[45], rd
[46]);
1099 swap(rd
[47], rd
[48]);
1101 sixaxis_parse_report(sc
, rd
, size
);
1102 } else if ((sc
->quirks
& MOTION_CONTROLLER_BT
) && rd
[0] == 0x01 && size
== 49) {
1103 sixaxis_parse_report(sc
, rd
, size
);
1104 } else if ((sc
->quirks
& NAVIGATION_CONTROLLER
) && rd
[0] == 0x01 &&
1106 sixaxis_parse_report(sc
, rd
, size
);
1107 } else if ((sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
) && rd
[0] == 0x01 &&
1109 dualshock4_parse_report(sc
, rd
, size
);
1110 } else if (((sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
) && rd
[0] == 0x11 &&
1117 crc
= crc32_le(0xFFFFFFFF, &bthdr
, 1);
1118 crc
= ~crc32_le(crc
, rd
, DS4_INPUT_REPORT_0x11_SIZE
-4);
1119 report_crc
= get_unaligned_le32(&rd
[DS4_INPUT_REPORT_0x11_SIZE
-4]);
1120 if (crc
!= report_crc
) {
1121 hid_dbg(sc
->hdev
, "DualShock 4 input report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1126 dualshock4_parse_report(sc
, rd
, size
);
1127 } else if ((sc
->quirks
& DUALSHOCK4_DONGLE
) && rd
[0] == 0x01 &&
1129 unsigned long flags
;
1130 enum ds4_dongle_state dongle_state
;
1133 * In the case of a DS4 USB dongle, bit[2] of byte 31 indicates
1134 * if a DS4 is actually connected (indicated by '0').
1135 * For non-dongle, this bit is always 0 (connected).
1137 bool connected
= (rd
[31] & 0x04) ? false : true;
1139 spin_lock_irqsave(&sc
->lock
, flags
);
1140 dongle_state
= sc
->ds4_dongle_state
;
1141 spin_unlock_irqrestore(&sc
->lock
, flags
);
1144 * The dongle always sends input reports even when no
1145 * DS4 is attached. When a DS4 is connected, we need to
1146 * obtain calibration data before we can use it.
1147 * The code below tracks dongle state and kicks of
1148 * calibration when needed and only allows us to process
1149 * input if a DS4 is actually connected.
1151 if (dongle_state
== DONGLE_DISCONNECTED
&& connected
) {
1152 hid_info(sc
->hdev
, "DualShock 4 USB dongle: controller connected\n");
1155 spin_lock_irqsave(&sc
->lock
, flags
);
1156 sc
->ds4_dongle_state
= DONGLE_CALIBRATING
;
1157 spin_unlock_irqrestore(&sc
->lock
, flags
);
1159 sony_schedule_work(sc
, SONY_WORKER_HOTPLUG
);
1161 /* Don't process the report since we don't have
1162 * calibration data, but let hidraw have it anyway.
1165 } else if ((dongle_state
== DONGLE_CONNECTED
||
1166 dongle_state
== DONGLE_DISABLED
) && !connected
) {
1167 hid_info(sc
->hdev
, "DualShock 4 USB dongle: controller disconnected\n");
1169 spin_lock_irqsave(&sc
->lock
, flags
);
1170 sc
->ds4_dongle_state
= DONGLE_DISCONNECTED
;
1171 spin_unlock_irqrestore(&sc
->lock
, flags
);
1173 /* Return 0, so hidraw can get the report. */
1175 } else if (dongle_state
== DONGLE_CALIBRATING
||
1176 dongle_state
== DONGLE_DISABLED
||
1177 dongle_state
== DONGLE_DISCONNECTED
) {
1178 /* Return 0, so hidraw can get the report. */
1182 dualshock4_parse_report(sc
, rd
, size
);
1185 if (sc
->defer_initialization
) {
1186 sc
->defer_initialization
= 0;
1187 sony_schedule_work(sc
, SONY_WORKER_STATE
);
1193 static int sony_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
1194 struct hid_field
*field
, struct hid_usage
*usage
,
1195 unsigned long **bit
, int *max
)
1197 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
1199 if (sc
->quirks
& BUZZ_CONTROLLER
) {
1200 unsigned int key
= usage
->hid
& HID_USAGE
;
1202 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_BUTTON
)
1205 switch (usage
->collection_index
) {
1207 if (key
>= ARRAY_SIZE(buzz_keymap
))
1210 key
= buzz_keymap
[key
];
1218 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_KEY
, key
);
1222 if (sc
->quirks
& PS3REMOTE
)
1223 return ps3remote_mapping(hdev
, hi
, field
, usage
, bit
, max
);
1225 if (sc
->quirks
& NAVIGATION_CONTROLLER
)
1226 return navigation_mapping(hdev
, hi
, field
, usage
, bit
, max
);
1228 if (sc
->quirks
& SIXAXIS_CONTROLLER
)
1229 return sixaxis_mapping(hdev
, hi
, field
, usage
, bit
, max
);
1231 if (sc
->quirks
& DUALSHOCK4_CONTROLLER
)
1232 return ds4_mapping(hdev
, hi
, field
, usage
, bit
, max
);
1235 /* Let hid-core decide for the others */
1239 static int sony_register_touchpad(struct sony_sc
*sc
, int touch_count
,
1246 sc
->touchpad
= input_allocate_device();
1250 input_set_drvdata(sc
->touchpad
, sc
);
1251 sc
->touchpad
->dev
.parent
= &sc
->hdev
->dev
;
1252 sc
->touchpad
->phys
= sc
->hdev
->phys
;
1253 sc
->touchpad
->uniq
= sc
->hdev
->uniq
;
1254 sc
->touchpad
->id
.bustype
= sc
->hdev
->bus
;
1255 sc
->touchpad
->id
.vendor
= sc
->hdev
->vendor
;
1256 sc
->touchpad
->id
.product
= sc
->hdev
->product
;
1257 sc
->touchpad
->id
.version
= sc
->hdev
->version
;
1259 /* Append a suffix to the controller name as there are various
1260 * DS4 compatible non-Sony devices with different names.
1262 name_sz
= strlen(sc
->hdev
->name
) + sizeof(DS4_TOUCHPAD_SUFFIX
);
1263 name
= kzalloc(name_sz
, GFP_KERNEL
);
1268 snprintf(name
, name_sz
, "%s" DS4_TOUCHPAD_SUFFIX
, sc
->hdev
->name
);
1269 sc
->touchpad
->name
= name
;
1271 ret
= input_mt_init_slots(sc
->touchpad
, touch_count
, INPUT_MT_POINTER
);
1275 /* We map the button underneath the touchpad to BTN_LEFT. */
1276 __set_bit(EV_KEY
, sc
->touchpad
->evbit
);
1277 __set_bit(BTN_LEFT
, sc
->touchpad
->keybit
);
1278 __set_bit(INPUT_PROP_BUTTONPAD
, sc
->touchpad
->propbit
);
1280 input_set_abs_params(sc
->touchpad
, ABS_MT_POSITION_X
, 0, w
, 0, 0);
1281 input_set_abs_params(sc
->touchpad
, ABS_MT_POSITION_Y
, 0, h
, 0, 0);
1283 ret
= input_register_device(sc
->touchpad
);
1290 kfree(sc
->touchpad
->name
);
1291 sc
->touchpad
->name
= NULL
;
1293 input_free_device(sc
->touchpad
);
1294 sc
->touchpad
= NULL
;
1299 static void sony_unregister_touchpad(struct sony_sc
*sc
)
1304 kfree(sc
->touchpad
->name
);
1305 sc
->touchpad
->name
= NULL
;
1307 input_unregister_device(sc
->touchpad
);
1308 sc
->touchpad
= NULL
;
1311 static int sony_register_sensors(struct sony_sc
*sc
)
1318 sc
->sensor_dev
= input_allocate_device();
1319 if (!sc
->sensor_dev
)
1322 input_set_drvdata(sc
->sensor_dev
, sc
);
1323 sc
->sensor_dev
->dev
.parent
= &sc
->hdev
->dev
;
1324 sc
->sensor_dev
->phys
= sc
->hdev
->phys
;
1325 sc
->sensor_dev
->uniq
= sc
->hdev
->uniq
;
1326 sc
->sensor_dev
->id
.bustype
= sc
->hdev
->bus
;
1327 sc
->sensor_dev
->id
.vendor
= sc
->hdev
->vendor
;
1328 sc
->sensor_dev
->id
.product
= sc
->hdev
->product
;
1329 sc
->sensor_dev
->id
.version
= sc
->hdev
->version
;
1331 /* Append a suffix to the controller name as there are various
1332 * DS4 compatible non-Sony devices with different names.
1334 name_sz
= strlen(sc
->hdev
->name
) + sizeof(SENSOR_SUFFIX
);
1335 name
= kzalloc(name_sz
, GFP_KERNEL
);
1340 snprintf(name
, name_sz
, "%s" SENSOR_SUFFIX
, sc
->hdev
->name
);
1341 sc
->sensor_dev
->name
= name
;
1343 if (sc
->quirks
& SIXAXIS_CONTROLLER
) {
1344 /* For the DS3 we only support the accelerometer, which works
1345 * quite well even without calibration. The device also has
1346 * a 1-axis gyro, but it is very difficult to manage from within
1347 * the driver even to get data, the sensor is inaccurate and
1348 * the behavior is very different between hardware revisions.
1350 input_set_abs_params(sc
->sensor_dev
, ABS_X
, -512, 511, 4, 0);
1351 input_set_abs_params(sc
->sensor_dev
, ABS_Y
, -512, 511, 4, 0);
1352 input_set_abs_params(sc
->sensor_dev
, ABS_Z
, -512, 511, 4, 0);
1353 input_abs_set_res(sc
->sensor_dev
, ABS_X
, SIXAXIS_ACC_RES_PER_G
);
1354 input_abs_set_res(sc
->sensor_dev
, ABS_Y
, SIXAXIS_ACC_RES_PER_G
);
1355 input_abs_set_res(sc
->sensor_dev
, ABS_Z
, SIXAXIS_ACC_RES_PER_G
);
1356 } else if (sc
->quirks
& DUALSHOCK4_CONTROLLER
) {
1357 range
= DS4_ACC_RES_PER_G
*4;
1358 input_set_abs_params(sc
->sensor_dev
, ABS_X
, -range
, range
, 16, 0);
1359 input_set_abs_params(sc
->sensor_dev
, ABS_Y
, -range
, range
, 16, 0);
1360 input_set_abs_params(sc
->sensor_dev
, ABS_Z
, -range
, range
, 16, 0);
1361 input_abs_set_res(sc
->sensor_dev
, ABS_X
, DS4_ACC_RES_PER_G
);
1362 input_abs_set_res(sc
->sensor_dev
, ABS_Y
, DS4_ACC_RES_PER_G
);
1363 input_abs_set_res(sc
->sensor_dev
, ABS_Z
, DS4_ACC_RES_PER_G
);
1365 range
= DS4_GYRO_RES_PER_DEG_S
*2048;
1366 input_set_abs_params(sc
->sensor_dev
, ABS_RX
, -range
, range
, 16, 0);
1367 input_set_abs_params(sc
->sensor_dev
, ABS_RY
, -range
, range
, 16, 0);
1368 input_set_abs_params(sc
->sensor_dev
, ABS_RZ
, -range
, range
, 16, 0);
1369 input_abs_set_res(sc
->sensor_dev
, ABS_RX
, DS4_GYRO_RES_PER_DEG_S
);
1370 input_abs_set_res(sc
->sensor_dev
, ABS_RY
, DS4_GYRO_RES_PER_DEG_S
);
1371 input_abs_set_res(sc
->sensor_dev
, ABS_RZ
, DS4_GYRO_RES_PER_DEG_S
);
1373 __set_bit(EV_MSC
, sc
->sensor_dev
->evbit
);
1374 __set_bit(MSC_TIMESTAMP
, sc
->sensor_dev
->mscbit
);
1377 __set_bit(INPUT_PROP_ACCELEROMETER
, sc
->sensor_dev
->propbit
);
1379 ret
= input_register_device(sc
->sensor_dev
);
1386 kfree(sc
->sensor_dev
->name
);
1387 sc
->sensor_dev
->name
= NULL
;
1389 input_free_device(sc
->sensor_dev
);
1390 sc
->sensor_dev
= NULL
;
1395 static void sony_unregister_sensors(struct sony_sc
*sc
)
1397 if (!sc
->sensor_dev
)
1400 kfree(sc
->sensor_dev
->name
);
1401 sc
->sensor_dev
->name
= NULL
;
1403 input_unregister_device(sc
->sensor_dev
);
1404 sc
->sensor_dev
= NULL
;
1409 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1410 * to "operational". Without this, the ps3 controller will not report any
1413 static int sixaxis_set_operational_usb(struct hid_device
*hdev
)
1415 const int buf_size
=
1416 max(SIXAXIS_REPORT_0xF2_SIZE
, SIXAXIS_REPORT_0xF5_SIZE
);
1420 buf
= kmalloc(buf_size
, GFP_KERNEL
);
1424 ret
= hid_hw_raw_request(hdev
, 0xf2, buf
, SIXAXIS_REPORT_0xF2_SIZE
,
1425 HID_FEATURE_REPORT
, HID_REQ_GET_REPORT
);
1427 hid_err(hdev
, "can't set operational mode: step 1\n");
1432 * Some compatible controllers like the Speedlink Strike FX and
1433 * Gasia need another query plus an USB interrupt to get operational.
1435 ret
= hid_hw_raw_request(hdev
, 0xf5, buf
, SIXAXIS_REPORT_0xF5_SIZE
,
1436 HID_FEATURE_REPORT
, HID_REQ_GET_REPORT
);
1438 hid_err(hdev
, "can't set operational mode: step 2\n");
1443 * But the USB interrupt would cause SHANWAN controllers to
1444 * start rumbling non-stop.
1446 if (strcmp(hdev
->name
, "SHANWAN PS3 GamePad")) {
1447 ret
= hid_hw_output_report(hdev
, buf
, 1);
1449 hid_info(hdev
, "can't set operational mode: step 3, ignoring\n");
1460 static int sixaxis_set_operational_bt(struct hid_device
*hdev
)
1462 static const u8 report
[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1466 buf
= kmemdup(report
, sizeof(report
), GFP_KERNEL
);
1470 ret
= hid_hw_raw_request(hdev
, buf
[0], buf
, sizeof(report
),
1471 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
1479 * Request DS4 calibration data for the motion sensors.
1480 * For Bluetooth this also affects the operating mode (see below).
1482 static int dualshock4_get_calibration_data(struct sony_sc
*sc
)
1486 short gyro_pitch_bias
, gyro_pitch_plus
, gyro_pitch_minus
;
1487 short gyro_yaw_bias
, gyro_yaw_plus
, gyro_yaw_minus
;
1488 short gyro_roll_bias
, gyro_roll_plus
, gyro_roll_minus
;
1489 short gyro_speed_plus
, gyro_speed_minus
;
1490 short acc_x_plus
, acc_x_minus
;
1491 short acc_y_plus
, acc_y_minus
;
1492 short acc_z_plus
, acc_z_minus
;
1496 /* For Bluetooth we use a different request, which supports CRC.
1497 * Note: in Bluetooth mode feature report 0x02 also changes the state
1498 * of the controller, so that it sends input reports of type 0x11.
1500 if (sc
->quirks
& (DUALSHOCK4_CONTROLLER_USB
| DUALSHOCK4_DONGLE
)) {
1501 buf
= kmalloc(DS4_FEATURE_REPORT_0x02_SIZE
, GFP_KERNEL
);
1505 ret
= hid_hw_raw_request(sc
->hdev
, 0x02, buf
,
1506 DS4_FEATURE_REPORT_0x02_SIZE
,
1508 HID_REQ_GET_REPORT
);
1517 buf
= kmalloc(DS4_FEATURE_REPORT_0x05_SIZE
, GFP_KERNEL
);
1521 for (retries
= 0; retries
< 3; retries
++) {
1522 ret
= hid_hw_raw_request(sc
->hdev
, 0x05, buf
,
1523 DS4_FEATURE_REPORT_0x05_SIZE
,
1525 HID_REQ_GET_REPORT
);
1530 crc
= crc32_le(0xFFFFFFFF, &bthdr
, 1);
1531 crc
= ~crc32_le(crc
, buf
, DS4_FEATURE_REPORT_0x05_SIZE
-4);
1532 report_crc
= get_unaligned_le32(&buf
[DS4_FEATURE_REPORT_0x05_SIZE
-4]);
1533 if (crc
!= report_crc
) {
1534 hid_warn(sc
->hdev
, "DualShock 4 calibration report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1537 hid_warn(sc
->hdev
, "Retrying DualShock 4 get calibration report request\n");
1549 gyro_pitch_bias
= get_unaligned_le16(&buf
[1]);
1550 gyro_yaw_bias
= get_unaligned_le16(&buf
[3]);
1551 gyro_roll_bias
= get_unaligned_le16(&buf
[5]);
1552 if (sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
) {
1553 gyro_pitch_plus
= get_unaligned_le16(&buf
[7]);
1554 gyro_pitch_minus
= get_unaligned_le16(&buf
[9]);
1555 gyro_yaw_plus
= get_unaligned_le16(&buf
[11]);
1556 gyro_yaw_minus
= get_unaligned_le16(&buf
[13]);
1557 gyro_roll_plus
= get_unaligned_le16(&buf
[15]);
1558 gyro_roll_minus
= get_unaligned_le16(&buf
[17]);
1561 gyro_pitch_plus
= get_unaligned_le16(&buf
[7]);
1562 gyro_yaw_plus
= get_unaligned_le16(&buf
[9]);
1563 gyro_roll_plus
= get_unaligned_le16(&buf
[11]);
1564 gyro_pitch_minus
= get_unaligned_le16(&buf
[13]);
1565 gyro_yaw_minus
= get_unaligned_le16(&buf
[15]);
1566 gyro_roll_minus
= get_unaligned_le16(&buf
[17]);
1568 gyro_speed_plus
= get_unaligned_le16(&buf
[19]);
1569 gyro_speed_minus
= get_unaligned_le16(&buf
[21]);
1570 acc_x_plus
= get_unaligned_le16(&buf
[23]);
1571 acc_x_minus
= get_unaligned_le16(&buf
[25]);
1572 acc_y_plus
= get_unaligned_le16(&buf
[27]);
1573 acc_y_minus
= get_unaligned_le16(&buf
[29]);
1574 acc_z_plus
= get_unaligned_le16(&buf
[31]);
1575 acc_z_minus
= get_unaligned_le16(&buf
[33]);
1577 /* Set gyroscope calibration and normalization parameters.
1578 * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
1580 speed_2x
= (gyro_speed_plus
+ gyro_speed_minus
);
1581 sc
->ds4_calib_data
[0].abs_code
= ABS_RX
;
1582 sc
->ds4_calib_data
[0].bias
= gyro_pitch_bias
;
1583 sc
->ds4_calib_data
[0].sens_numer
= speed_2x
*DS4_GYRO_RES_PER_DEG_S
;
1584 sc
->ds4_calib_data
[0].sens_denom
= gyro_pitch_plus
- gyro_pitch_minus
;
1586 sc
->ds4_calib_data
[1].abs_code
= ABS_RY
;
1587 sc
->ds4_calib_data
[1].bias
= gyro_yaw_bias
;
1588 sc
->ds4_calib_data
[1].sens_numer
= speed_2x
*DS4_GYRO_RES_PER_DEG_S
;
1589 sc
->ds4_calib_data
[1].sens_denom
= gyro_yaw_plus
- gyro_yaw_minus
;
1591 sc
->ds4_calib_data
[2].abs_code
= ABS_RZ
;
1592 sc
->ds4_calib_data
[2].bias
= gyro_roll_bias
;
1593 sc
->ds4_calib_data
[2].sens_numer
= speed_2x
*DS4_GYRO_RES_PER_DEG_S
;
1594 sc
->ds4_calib_data
[2].sens_denom
= gyro_roll_plus
- gyro_roll_minus
;
1596 /* Set accelerometer calibration and normalization parameters.
1597 * Data values will be normalized to 1/DS4_ACC_RES_PER_G G.
1599 range_2g
= acc_x_plus
- acc_x_minus
;
1600 sc
->ds4_calib_data
[3].abs_code
= ABS_X
;
1601 sc
->ds4_calib_data
[3].bias
= acc_x_plus
- range_2g
/ 2;
1602 sc
->ds4_calib_data
[3].sens_numer
= 2*DS4_ACC_RES_PER_G
;
1603 sc
->ds4_calib_data
[3].sens_denom
= range_2g
;
1605 range_2g
= acc_y_plus
- acc_y_minus
;
1606 sc
->ds4_calib_data
[4].abs_code
= ABS_Y
;
1607 sc
->ds4_calib_data
[4].bias
= acc_y_plus
- range_2g
/ 2;
1608 sc
->ds4_calib_data
[4].sens_numer
= 2*DS4_ACC_RES_PER_G
;
1609 sc
->ds4_calib_data
[4].sens_denom
= range_2g
;
1611 range_2g
= acc_z_plus
- acc_z_minus
;
1612 sc
->ds4_calib_data
[5].abs_code
= ABS_Z
;
1613 sc
->ds4_calib_data
[5].bias
= acc_z_plus
- range_2g
/ 2;
1614 sc
->ds4_calib_data
[5].sens_numer
= 2*DS4_ACC_RES_PER_G
;
1615 sc
->ds4_calib_data
[5].sens_denom
= range_2g
;
1622 static void dualshock4_calibration_work(struct work_struct
*work
)
1624 struct sony_sc
*sc
= container_of(work
, struct sony_sc
, hotplug_worker
);
1625 unsigned long flags
;
1626 enum ds4_dongle_state dongle_state
;
1629 ret
= dualshock4_get_calibration_data(sc
);
1631 /* This call is very unlikely to fail for the dongle. When it
1632 * fails we are probably in a very bad state, so mark the
1633 * dongle as disabled. We will re-enable the dongle if a new
1634 * DS4 hotplug is detect from sony_raw_event as any issues
1635 * are likely resolved then (the dongle is quite stupid).
1637 hid_err(sc
->hdev
, "DualShock 4 USB dongle: calibration failed, disabling device\n");
1638 dongle_state
= DONGLE_DISABLED
;
1640 hid_info(sc
->hdev
, "DualShock 4 USB dongle: calibration completed\n");
1641 dongle_state
= DONGLE_CONNECTED
;
1644 spin_lock_irqsave(&sc
->lock
, flags
);
1645 sc
->ds4_dongle_state
= dongle_state
;
1646 spin_unlock_irqrestore(&sc
->lock
, flags
);
1649 static void sixaxis_set_leds_from_id(struct sony_sc
*sc
)
1651 static const u8 sixaxis_leds
[10][4] = {
1652 { 0x01, 0x00, 0x00, 0x00 },
1653 { 0x00, 0x01, 0x00, 0x00 },
1654 { 0x00, 0x00, 0x01, 0x00 },
1655 { 0x00, 0x00, 0x00, 0x01 },
1656 { 0x01, 0x00, 0x00, 0x01 },
1657 { 0x00, 0x01, 0x00, 0x01 },
1658 { 0x00, 0x00, 0x01, 0x01 },
1659 { 0x01, 0x00, 0x01, 0x01 },
1660 { 0x00, 0x01, 0x01, 0x01 },
1661 { 0x01, 0x01, 0x01, 0x01 }
1664 int id
= sc
->device_id
;
1666 BUILD_BUG_ON(MAX_LEDS
< ARRAY_SIZE(sixaxis_leds
[0]));
1672 memcpy(sc
->led_state
, sixaxis_leds
[id
], sizeof(sixaxis_leds
[id
]));
1675 static void dualshock4_set_leds_from_id(struct sony_sc
*sc
)
1677 /* The first 4 color/index entries match what the PS4 assigns */
1678 static const u8 color_code
[7][3] = {
1679 /* Blue */ { 0x00, 0x00, 0x40 },
1680 /* Red */ { 0x40, 0x00, 0x00 },
1681 /* Green */ { 0x00, 0x40, 0x00 },
1682 /* Pink */ { 0x20, 0x00, 0x20 },
1683 /* Orange */ { 0x02, 0x01, 0x00 },
1684 /* Teal */ { 0x00, 0x01, 0x01 },
1685 /* White */ { 0x01, 0x01, 0x01 }
1688 int id
= sc
->device_id
;
1690 BUILD_BUG_ON(MAX_LEDS
< ARRAY_SIZE(color_code
[0]));
1696 memcpy(sc
->led_state
, color_code
[id
], sizeof(color_code
[id
]));
1699 static void buzz_set_leds(struct sony_sc
*sc
)
1701 struct hid_device
*hdev
= sc
->hdev
;
1702 struct list_head
*report_list
=
1703 &hdev
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
1704 struct hid_report
*report
= list_entry(report_list
->next
,
1705 struct hid_report
, list
);
1706 s32
*value
= report
->field
[0]->value
;
1708 BUILD_BUG_ON(MAX_LEDS
< 4);
1711 value
[1] = sc
->led_state
[0] ? 0xff : 0x00;
1712 value
[2] = sc
->led_state
[1] ? 0xff : 0x00;
1713 value
[3] = sc
->led_state
[2] ? 0xff : 0x00;
1714 value
[4] = sc
->led_state
[3] ? 0xff : 0x00;
1717 hid_hw_request(hdev
, report
, HID_REQ_SET_REPORT
);
1720 static void sony_set_leds(struct sony_sc
*sc
)
1722 if (!(sc
->quirks
& BUZZ_CONTROLLER
))
1723 sony_schedule_work(sc
, SONY_WORKER_STATE
);
1728 static void sony_led_set_brightness(struct led_classdev
*led
,
1729 enum led_brightness value
)
1731 struct device
*dev
= led
->dev
->parent
;
1732 struct hid_device
*hdev
= to_hid_device(dev
);
1733 struct sony_sc
*drv_data
;
1738 drv_data
= hid_get_drvdata(hdev
);
1740 hid_err(hdev
, "No device data\n");
1745 * The Sixaxis on USB will override any LED settings sent to it
1746 * and keep flashing all of the LEDs until the PS button is pressed.
1747 * Updates, even if redundant, must be always be sent to the
1748 * controller to avoid having to toggle the state of an LED just to
1749 * stop the flashing later on.
1751 force_update
= !!(drv_data
->quirks
& SIXAXIS_CONTROLLER_USB
);
1753 for (n
= 0; n
< drv_data
->led_count
; n
++) {
1754 if (led
== drv_data
->leds
[n
] && (force_update
||
1755 (value
!= drv_data
->led_state
[n
] ||
1756 drv_data
->led_delay_on
[n
] ||
1757 drv_data
->led_delay_off
[n
]))) {
1759 drv_data
->led_state
[n
] = value
;
1761 /* Setting the brightness stops the blinking */
1762 drv_data
->led_delay_on
[n
] = 0;
1763 drv_data
->led_delay_off
[n
] = 0;
1765 sony_set_leds(drv_data
);
1771 static enum led_brightness
sony_led_get_brightness(struct led_classdev
*led
)
1773 struct device
*dev
= led
->dev
->parent
;
1774 struct hid_device
*hdev
= to_hid_device(dev
);
1775 struct sony_sc
*drv_data
;
1779 drv_data
= hid_get_drvdata(hdev
);
1781 hid_err(hdev
, "No device data\n");
1785 for (n
= 0; n
< drv_data
->led_count
; n
++) {
1786 if (led
== drv_data
->leds
[n
])
1787 return drv_data
->led_state
[n
];
1793 static int sony_led_blink_set(struct led_classdev
*led
, unsigned long *delay_on
,
1794 unsigned long *delay_off
)
1796 struct device
*dev
= led
->dev
->parent
;
1797 struct hid_device
*hdev
= to_hid_device(dev
);
1798 struct sony_sc
*drv_data
= hid_get_drvdata(hdev
);
1803 hid_err(hdev
, "No device data\n");
1807 /* Max delay is 255 deciseconds or 2550 milliseconds */
1808 if (*delay_on
> 2550)
1810 if (*delay_off
> 2550)
1813 /* Blink at 1 Hz if both values are zero */
1814 if (!*delay_on
&& !*delay_off
)
1815 *delay_on
= *delay_off
= 500;
1817 new_on
= *delay_on
/ 10;
1818 new_off
= *delay_off
/ 10;
1820 for (n
= 0; n
< drv_data
->led_count
; n
++) {
1821 if (led
== drv_data
->leds
[n
])
1825 /* This LED is not registered on this device */
1826 if (n
>= drv_data
->led_count
)
1829 /* Don't schedule work if the values didn't change */
1830 if (new_on
!= drv_data
->led_delay_on
[n
] ||
1831 new_off
!= drv_data
->led_delay_off
[n
]) {
1832 drv_data
->led_delay_on
[n
] = new_on
;
1833 drv_data
->led_delay_off
[n
] = new_off
;
1834 sony_schedule_work(drv_data
, SONY_WORKER_STATE
);
1840 static void sony_leds_remove(struct sony_sc
*sc
)
1842 struct led_classdev
*led
;
1845 BUG_ON(!(sc
->quirks
& SONY_LED_SUPPORT
));
1847 for (n
= 0; n
< sc
->led_count
; n
++) {
1852 led_classdev_unregister(led
);
1859 static int sony_leds_init(struct sony_sc
*sc
)
1861 struct hid_device
*hdev
= sc
->hdev
;
1864 struct led_classdev
*led
;
1868 const char *name_fmt
;
1869 static const char * const ds4_name_str
[] = { "red", "green", "blue",
1871 u8 max_brightness
[MAX_LEDS
] = { [0 ... (MAX_LEDS
- 1)] = 1 };
1872 u8 use_hw_blink
[MAX_LEDS
] = { 0 };
1874 BUG_ON(!(sc
->quirks
& SONY_LED_SUPPORT
));
1876 if (sc
->quirks
& BUZZ_CONTROLLER
) {
1879 name_len
= strlen("::buzz#");
1880 name_fmt
= "%s::buzz%d";
1881 /* Validate expected report characteristics. */
1882 if (!hid_validate_values(hdev
, HID_OUTPUT_REPORT
, 0, 0, 7))
1884 } else if (sc
->quirks
& DUALSHOCK4_CONTROLLER
) {
1885 dualshock4_set_leds_from_id(sc
);
1886 sc
->led_state
[3] = 1;
1888 memset(max_brightness
, 255, 3);
1889 use_hw_blink
[3] = 1;
1893 } else if (sc
->quirks
& MOTION_CONTROLLER
) {
1895 memset(max_brightness
, 255, 3);
1899 } else if (sc
->quirks
& NAVIGATION_CONTROLLER
) {
1900 static const u8 navigation_leds
[4] = {0x01, 0x00, 0x00, 0x00};
1902 memcpy(sc
->led_state
, navigation_leds
, sizeof(navigation_leds
));
1904 memset(use_hw_blink
, 1, 4);
1906 name_len
= strlen("::sony#");
1907 name_fmt
= "%s::sony%d";
1909 sixaxis_set_leds_from_id(sc
);
1911 memset(use_hw_blink
, 1, 4);
1913 name_len
= strlen("::sony#");
1914 name_fmt
= "%s::sony%d";
1918 * Clear LEDs as we have no way of reading their initial state. This is
1919 * only relevant if the driver is loaded after somebody actively set the
1924 name_sz
= strlen(dev_name(&hdev
->dev
)) + name_len
+ 1;
1926 for (n
= 0; n
< sc
->led_count
; n
++) {
1929 name_sz
= strlen(dev_name(&hdev
->dev
)) + strlen(ds4_name_str
[n
]) + 2;
1931 led
= kzalloc(sizeof(struct led_classdev
) + name_sz
, GFP_KERNEL
);
1933 hid_err(hdev
, "Couldn't allocate memory for LED %d\n", n
);
1938 name
= (void *)(&led
[1]);
1940 snprintf(name
, name_sz
, name_fmt
, dev_name(&hdev
->dev
),
1943 snprintf(name
, name_sz
, name_fmt
, dev_name(&hdev
->dev
), n
+ 1);
1945 led
->brightness
= sc
->led_state
[n
];
1946 led
->max_brightness
= max_brightness
[n
];
1947 led
->flags
= LED_CORE_SUSPENDRESUME
;
1948 led
->brightness_get
= sony_led_get_brightness
;
1949 led
->brightness_set
= sony_led_set_brightness
;
1951 if (use_hw_blink
[n
])
1952 led
->blink_set
= sony_led_blink_set
;
1956 ret
= led_classdev_register(&hdev
->dev
, led
);
1958 hid_err(hdev
, "Failed to register LED %d\n", n
);
1968 sony_leds_remove(sc
);
1973 static void sixaxis_send_output_report(struct sony_sc
*sc
)
1975 static const union sixaxis_output_report_01 default_report
= {
1978 0x01, 0xff, 0x00, 0xff, 0x00,
1979 0x00, 0x00, 0x00, 0x00, 0x00,
1980 0xff, 0x27, 0x10, 0x00, 0x32,
1981 0xff, 0x27, 0x10, 0x00, 0x32,
1982 0xff, 0x27, 0x10, 0x00, 0x32,
1983 0xff, 0x27, 0x10, 0x00, 0x32,
1984 0x00, 0x00, 0x00, 0x00, 0x00
1987 struct sixaxis_output_report
*report
=
1988 (struct sixaxis_output_report
*)sc
->output_report_dmabuf
;
1991 /* Initialize the report with default values */
1992 memcpy(report
, &default_report
, sizeof(struct sixaxis_output_report
));
1994 #ifdef CONFIG_SONY_FF
1995 report
->rumble
.right_motor_on
= sc
->right
? 1 : 0;
1996 report
->rumble
.left_motor_force
= sc
->left
;
1999 report
->leds_bitmap
|= sc
->led_state
[0] << 1;
2000 report
->leds_bitmap
|= sc
->led_state
[1] << 2;
2001 report
->leds_bitmap
|= sc
->led_state
[2] << 3;
2002 report
->leds_bitmap
|= sc
->led_state
[3] << 4;
2004 /* Set flag for all leds off, required for 3rd party INTEC controller */
2005 if ((report
->leds_bitmap
& 0x1E) == 0)
2006 report
->leds_bitmap
|= 0x20;
2009 * The LEDs in the report are indexed in reverse order to their
2010 * corresponding light on the controller.
2011 * Index 0 = LED 4, index 1 = LED 3, etc...
2013 * In the case of both delay values being zero (blinking disabled) the
2014 * default report values should be used or the controller LED will be
2017 for (n
= 0; n
< 4; n
++) {
2018 if (sc
->led_delay_on
[n
] || sc
->led_delay_off
[n
]) {
2019 report
->led
[3 - n
].duty_off
= sc
->led_delay_off
[n
];
2020 report
->led
[3 - n
].duty_on
= sc
->led_delay_on
[n
];
2024 hid_hw_raw_request(sc
->hdev
, report
->report_id
, (u8
*)report
,
2025 sizeof(struct sixaxis_output_report
),
2026 HID_OUTPUT_REPORT
, HID_REQ_SET_REPORT
);
2029 static void dualshock4_send_output_report(struct sony_sc
*sc
)
2031 struct hid_device
*hdev
= sc
->hdev
;
2032 u8
*buf
= sc
->output_report_dmabuf
;
2036 * NOTE: The lower 6 bits of buf[1] field of the Bluetooth report
2037 * control the interval at which Dualshock 4 reports data:
2044 if (sc
->quirks
& (DUALSHOCK4_CONTROLLER_USB
| DUALSHOCK4_DONGLE
)) {
2045 memset(buf
, 0, DS4_OUTPUT_REPORT_0x05_SIZE
);
2047 buf
[1] = 0x07; /* blink + LEDs + motor */
2050 memset(buf
, 0, DS4_OUTPUT_REPORT_0x11_SIZE
);
2052 buf
[1] = 0xC0 /* HID + CRC */ | sc
->ds4_bt_poll_interval
;
2053 buf
[3] = 0x07; /* blink + LEDs + motor */
2057 #ifdef CONFIG_SONY_FF
2058 buf
[offset
++] = sc
->right
;
2059 buf
[offset
++] = sc
->left
;
2064 /* LED 3 is the global control */
2065 if (sc
->led_state
[3]) {
2066 buf
[offset
++] = sc
->led_state
[0];
2067 buf
[offset
++] = sc
->led_state
[1];
2068 buf
[offset
++] = sc
->led_state
[2];
2073 /* If both delay values are zero the DualShock 4 disables blinking. */
2074 buf
[offset
++] = sc
->led_delay_on
[3];
2075 buf
[offset
++] = sc
->led_delay_off
[3];
2077 if (sc
->quirks
& (DUALSHOCK4_CONTROLLER_USB
| DUALSHOCK4_DONGLE
))
2078 hid_hw_output_report(hdev
, buf
, DS4_OUTPUT_REPORT_0x05_SIZE
);
2080 /* CRC generation */
2084 crc
= crc32_le(0xFFFFFFFF, &bthdr
, 1);
2085 crc
= ~crc32_le(crc
, buf
, DS4_OUTPUT_REPORT_0x11_SIZE
-4);
2086 put_unaligned_le32(crc
, &buf
[74]);
2087 hid_hw_output_report(hdev
, buf
, DS4_OUTPUT_REPORT_0x11_SIZE
);
2091 static void motion_send_output_report(struct sony_sc
*sc
)
2093 struct hid_device
*hdev
= sc
->hdev
;
2094 struct motion_output_report_02
*report
=
2095 (struct motion_output_report_02
*)sc
->output_report_dmabuf
;
2097 memset(report
, 0, MOTION_REPORT_0x02_SIZE
);
2099 report
->type
= 0x02; /* set leds */
2100 report
->r
= sc
->led_state
[0];
2101 report
->g
= sc
->led_state
[1];
2102 report
->b
= sc
->led_state
[2];
2104 #ifdef CONFIG_SONY_FF
2105 report
->rumble
= max(sc
->right
, sc
->left
);
2108 hid_hw_output_report(hdev
, (u8
*)report
, MOTION_REPORT_0x02_SIZE
);
2111 static inline void sony_send_output_report(struct sony_sc
*sc
)
2113 if (sc
->send_output_report
)
2114 sc
->send_output_report(sc
);
2117 static void sony_state_worker(struct work_struct
*work
)
2119 struct sony_sc
*sc
= container_of(work
, struct sony_sc
, state_worker
);
2121 sc
->send_output_report(sc
);
2124 static int sony_allocate_output_report(struct sony_sc
*sc
)
2126 if ((sc
->quirks
& SIXAXIS_CONTROLLER
) ||
2127 (sc
->quirks
& NAVIGATION_CONTROLLER
))
2128 sc
->output_report_dmabuf
=
2129 kmalloc(sizeof(union sixaxis_output_report_01
),
2131 else if (sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
)
2132 sc
->output_report_dmabuf
= kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE
,
2134 else if (sc
->quirks
& (DUALSHOCK4_CONTROLLER_USB
| DUALSHOCK4_DONGLE
))
2135 sc
->output_report_dmabuf
= kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE
,
2137 else if (sc
->quirks
& MOTION_CONTROLLER
)
2138 sc
->output_report_dmabuf
= kmalloc(MOTION_REPORT_0x02_SIZE
,
2143 if (!sc
->output_report_dmabuf
)
2149 #ifdef CONFIG_SONY_FF
2150 static int sony_play_effect(struct input_dev
*dev
, void *data
,
2151 struct ff_effect
*effect
)
2153 struct hid_device
*hid
= input_get_drvdata(dev
);
2154 struct sony_sc
*sc
= hid_get_drvdata(hid
);
2156 if (effect
->type
!= FF_RUMBLE
)
2159 sc
->left
= effect
->u
.rumble
.strong_magnitude
/ 256;
2160 sc
->right
= effect
->u
.rumble
.weak_magnitude
/ 256;
2162 sony_schedule_work(sc
, SONY_WORKER_STATE
);
2166 static int sony_init_ff(struct sony_sc
*sc
)
2168 struct hid_input
*hidinput
= list_entry(sc
->hdev
->inputs
.next
,
2169 struct hid_input
, list
);
2170 struct input_dev
*input_dev
= hidinput
->input
;
2172 input_set_capability(input_dev
, EV_FF
, FF_RUMBLE
);
2173 return input_ff_create_memless(input_dev
, NULL
, sony_play_effect
);
2177 static int sony_init_ff(struct sony_sc
*sc
)
2184 static int sony_battery_get_property(struct power_supply
*psy
,
2185 enum power_supply_property psp
,
2186 union power_supply_propval
*val
)
2188 struct sony_sc
*sc
= power_supply_get_drvdata(psy
);
2189 unsigned long flags
;
2191 u8 battery_charging
, battery_capacity
, cable_state
;
2193 spin_lock_irqsave(&sc
->lock
, flags
);
2194 battery_charging
= sc
->battery_charging
;
2195 battery_capacity
= sc
->battery_capacity
;
2196 cable_state
= sc
->cable_state
;
2197 spin_unlock_irqrestore(&sc
->lock
, flags
);
2200 case POWER_SUPPLY_PROP_PRESENT
:
2203 case POWER_SUPPLY_PROP_SCOPE
:
2204 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
2206 case POWER_SUPPLY_PROP_CAPACITY
:
2207 val
->intval
= battery_capacity
;
2209 case POWER_SUPPLY_PROP_STATUS
:
2210 if (battery_charging
)
2211 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
2213 if (battery_capacity
== 100 && cable_state
)
2214 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
2216 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
2225 static int sony_battery_probe(struct sony_sc
*sc
, int append_dev_id
)
2227 const char *battery_str_fmt
= append_dev_id
?
2228 "sony_controller_battery_%pMR_%i" :
2229 "sony_controller_battery_%pMR";
2230 struct power_supply_config psy_cfg
= { .drv_data
= sc
, };
2231 struct hid_device
*hdev
= sc
->hdev
;
2235 * Set the default battery level to 100% to avoid low battery warnings
2236 * if the battery is polled before the first device report is received.
2238 sc
->battery_capacity
= 100;
2240 sc
->battery_desc
.properties
= sony_battery_props
;
2241 sc
->battery_desc
.num_properties
= ARRAY_SIZE(sony_battery_props
);
2242 sc
->battery_desc
.get_property
= sony_battery_get_property
;
2243 sc
->battery_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
2244 sc
->battery_desc
.use_for_apm
= 0;
2245 sc
->battery_desc
.name
= kasprintf(GFP_KERNEL
, battery_str_fmt
,
2246 sc
->mac_address
, sc
->device_id
);
2247 if (!sc
->battery_desc
.name
)
2250 sc
->battery
= power_supply_register(&hdev
->dev
, &sc
->battery_desc
,
2252 if (IS_ERR(sc
->battery
)) {
2253 ret
= PTR_ERR(sc
->battery
);
2254 hid_err(hdev
, "Unable to register battery device\n");
2258 power_supply_powers(sc
->battery
, &hdev
->dev
);
2262 kfree(sc
->battery_desc
.name
);
2263 sc
->battery_desc
.name
= NULL
;
2267 static void sony_battery_remove(struct sony_sc
*sc
)
2269 if (!sc
->battery_desc
.name
)
2272 power_supply_unregister(sc
->battery
);
2273 kfree(sc
->battery_desc
.name
);
2274 sc
->battery_desc
.name
= NULL
;
2278 * If a controller is plugged in via USB while already connected via Bluetooth
2279 * it will show up as two devices. A global list of connected controllers and
2280 * their MAC addresses is maintained to ensure that a device is only connected
2283 * Some USB-only devices masquerade as Sixaxis controllers and all have the
2284 * same dummy Bluetooth address, so a comparison of the connection type is
2285 * required. Devices are only rejected in the case where two devices have
2286 * matching Bluetooth addresses on different bus types.
2288 static inline int sony_compare_connection_type(struct sony_sc
*sc0
,
2289 struct sony_sc
*sc1
)
2291 const int sc0_not_bt
= !(sc0
->quirks
& SONY_BT_DEVICE
);
2292 const int sc1_not_bt
= !(sc1
->quirks
& SONY_BT_DEVICE
);
2294 return sc0_not_bt
== sc1_not_bt
;
2297 static int sony_check_add_dev_list(struct sony_sc
*sc
)
2299 struct sony_sc
*entry
;
2300 unsigned long flags
;
2303 spin_lock_irqsave(&sony_dev_list_lock
, flags
);
2305 list_for_each_entry(entry
, &sony_device_list
, list_node
) {
2306 ret
= memcmp(sc
->mac_address
, entry
->mac_address
,
2307 sizeof(sc
->mac_address
));
2309 if (sony_compare_connection_type(sc
, entry
)) {
2314 "controller with MAC address %pMR already connected\n",
2322 list_add(&(sc
->list_node
), &sony_device_list
);
2325 spin_unlock_irqrestore(&sony_dev_list_lock
, flags
);
2329 static void sony_remove_dev_list(struct sony_sc
*sc
)
2331 unsigned long flags
;
2333 if (sc
->list_node
.next
) {
2334 spin_lock_irqsave(&sony_dev_list_lock
, flags
);
2335 list_del(&(sc
->list_node
));
2336 spin_unlock_irqrestore(&sony_dev_list_lock
, flags
);
2340 static int sony_get_bt_devaddr(struct sony_sc
*sc
)
2344 /* HIDP stores the device MAC address as a string in the uniq field. */
2345 ret
= strlen(sc
->hdev
->uniq
);
2349 ret
= sscanf(sc
->hdev
->uniq
,
2350 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2351 &sc
->mac_address
[5], &sc
->mac_address
[4], &sc
->mac_address
[3],
2352 &sc
->mac_address
[2], &sc
->mac_address
[1], &sc
->mac_address
[0]);
2360 static int sony_check_add(struct sony_sc
*sc
)
2365 if ((sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
) ||
2366 (sc
->quirks
& MOTION_CONTROLLER_BT
) ||
2367 (sc
->quirks
& NAVIGATION_CONTROLLER_BT
) ||
2368 (sc
->quirks
& SIXAXIS_CONTROLLER_BT
)) {
2370 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2371 * address from the uniq string where HIDP stores it.
2372 * As uniq cannot be guaranteed to be a MAC address in all cases
2373 * a failure of this function should not prevent the connection.
2375 if (sony_get_bt_devaddr(sc
) < 0) {
2376 hid_warn(sc
->hdev
, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2379 } else if (sc
->quirks
& (DUALSHOCK4_CONTROLLER_USB
| DUALSHOCK4_DONGLE
)) {
2380 buf
= kmalloc(DS4_FEATURE_REPORT_0x81_SIZE
, GFP_KERNEL
);
2385 * The MAC address of a DS4 controller connected via USB can be
2386 * retrieved with feature report 0x81. The address begins at
2389 ret
= hid_hw_raw_request(sc
->hdev
, 0x81, buf
,
2390 DS4_FEATURE_REPORT_0x81_SIZE
, HID_FEATURE_REPORT
,
2391 HID_REQ_GET_REPORT
);
2393 if (ret
!= DS4_FEATURE_REPORT_0x81_SIZE
) {
2394 hid_err(sc
->hdev
, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
2395 ret
= ret
< 0 ? ret
: -EINVAL
;
2399 memcpy(sc
->mac_address
, &buf
[1], sizeof(sc
->mac_address
));
2401 snprintf(sc
->hdev
->uniq
, sizeof(sc
->hdev
->uniq
),
2402 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2403 sc
->mac_address
[5], sc
->mac_address
[4],
2404 sc
->mac_address
[3], sc
->mac_address
[2],
2405 sc
->mac_address
[1], sc
->mac_address
[0]);
2406 } else if ((sc
->quirks
& SIXAXIS_CONTROLLER_USB
) ||
2407 (sc
->quirks
& NAVIGATION_CONTROLLER_USB
)) {
2408 buf
= kmalloc(SIXAXIS_REPORT_0xF2_SIZE
, GFP_KERNEL
);
2413 * The MAC address of a Sixaxis controller connected via USB can
2414 * be retrieved with feature report 0xf2. The address begins at
2417 ret
= hid_hw_raw_request(sc
->hdev
, 0xf2, buf
,
2418 SIXAXIS_REPORT_0xF2_SIZE
, HID_FEATURE_REPORT
,
2419 HID_REQ_GET_REPORT
);
2421 if (ret
!= SIXAXIS_REPORT_0xF2_SIZE
) {
2422 hid_err(sc
->hdev
, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
2423 ret
= ret
< 0 ? ret
: -EINVAL
;
2428 * The Sixaxis device MAC in the report is big-endian and must
2431 for (n
= 0; n
< 6; n
++)
2432 sc
->mac_address
[5-n
] = buf
[4+n
];
2434 snprintf(sc
->hdev
->uniq
, sizeof(sc
->hdev
->uniq
),
2435 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2436 sc
->mac_address
[5], sc
->mac_address
[4],
2437 sc
->mac_address
[3], sc
->mac_address
[2],
2438 sc
->mac_address
[1], sc
->mac_address
[0]);
2443 ret
= sony_check_add_dev_list(sc
);
2452 static int sony_set_device_id(struct sony_sc
*sc
)
2457 * Only DualShock 4 or Sixaxis controllers get an id.
2458 * All others are set to -1.
2460 if ((sc
->quirks
& SIXAXIS_CONTROLLER
) ||
2461 (sc
->quirks
& DUALSHOCK4_CONTROLLER
)) {
2462 ret
= ida_simple_get(&sony_device_id_allocator
, 0, 0,
2468 sc
->device_id
= ret
;
2476 static void sony_release_device_id(struct sony_sc
*sc
)
2478 if (sc
->device_id
>= 0) {
2479 ida_simple_remove(&sony_device_id_allocator
, sc
->device_id
);
2484 static inline void sony_init_output_report(struct sony_sc
*sc
,
2485 void (*send_output_report
)(struct sony_sc
*))
2487 sc
->send_output_report
= send_output_report
;
2489 if (!sc
->state_worker_initialized
)
2490 INIT_WORK(&sc
->state_worker
, sony_state_worker
);
2492 sc
->state_worker_initialized
= 1;
2495 static inline void sony_cancel_work_sync(struct sony_sc
*sc
)
2497 if (sc
->hotplug_worker_initialized
)
2498 cancel_work_sync(&sc
->hotplug_worker
);
2499 if (sc
->state_worker_initialized
)
2500 cancel_work_sync(&sc
->state_worker
);
2504 static int sony_input_configured(struct hid_device
*hdev
,
2505 struct hid_input
*hidinput
)
2507 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
2511 ret
= sony_set_device_id(sc
);
2513 hid_err(hdev
, "failed to allocate the device id\n");
2517 ret
= append_dev_id
= sony_check_add(sc
);
2521 ret
= sony_allocate_output_report(sc
);
2523 hid_err(hdev
, "failed to allocate the output report buffer\n");
2527 if (sc
->quirks
& NAVIGATION_CONTROLLER_USB
) {
2529 * The Sony Sixaxis does not handle HID Output Reports on the
2530 * Interrupt EP like it could, so we need to force HID Output
2531 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2533 * There is also another issue about HID Output Reports via USB,
2534 * the Sixaxis does not want the report_id as part of the data
2535 * packet, so we have to discard buf[0] when sending the actual
2536 * control message, even for numbered reports, humpf!
2538 * Additionally, the Sixaxis on USB isn't properly initialized
2539 * until the PS logo button is pressed and as such won't retain
2540 * any state set by an output report, so the initial
2541 * configuration report is deferred until the first input
2544 hdev
->quirks
|= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP
;
2545 hdev
->quirks
|= HID_QUIRK_SKIP_OUTPUT_REPORT_ID
;
2546 sc
->defer_initialization
= 1;
2548 ret
= sixaxis_set_operational_usb(hdev
);
2550 hid_err(hdev
, "Failed to set controller into operational mode\n");
2554 sony_init_output_report(sc
, sixaxis_send_output_report
);
2555 } else if (sc
->quirks
& NAVIGATION_CONTROLLER_BT
) {
2557 * The Navigation controller wants output reports sent on the ctrl
2558 * endpoint when connected via Bluetooth.
2560 hdev
->quirks
|= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP
;
2562 ret
= sixaxis_set_operational_bt(hdev
);
2564 hid_err(hdev
, "Failed to set controller into operational mode\n");
2568 sony_init_output_report(sc
, sixaxis_send_output_report
);
2569 } else if (sc
->quirks
& SIXAXIS_CONTROLLER_USB
) {
2571 * The Sony Sixaxis does not handle HID Output Reports on the
2572 * Interrupt EP and the device only becomes active when the
2573 * PS button is pressed. See comment for Navigation controller
2574 * above for more details.
2576 hdev
->quirks
|= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP
;
2577 hdev
->quirks
|= HID_QUIRK_SKIP_OUTPUT_REPORT_ID
;
2578 sc
->defer_initialization
= 1;
2580 ret
= sixaxis_set_operational_usb(hdev
);
2582 hid_err(hdev
, "Failed to set controller into operational mode\n");
2586 ret
= sony_register_sensors(sc
);
2589 "Unable to initialize motion sensors: %d\n", ret
);
2593 sony_init_output_report(sc
, sixaxis_send_output_report
);
2594 } else if (sc
->quirks
& SIXAXIS_CONTROLLER_BT
) {
2596 * The Sixaxis wants output reports sent on the ctrl endpoint
2597 * when connected via Bluetooth.
2599 hdev
->quirks
|= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP
;
2601 ret
= sixaxis_set_operational_bt(hdev
);
2603 hid_err(hdev
, "Failed to set controller into operational mode\n");
2607 ret
= sony_register_sensors(sc
);
2610 "Unable to initialize motion sensors: %d\n", ret
);
2614 sony_init_output_report(sc
, sixaxis_send_output_report
);
2615 } else if (sc
->quirks
& DUALSHOCK4_CONTROLLER
) {
2616 ret
= dualshock4_get_calibration_data(sc
);
2618 hid_err(hdev
, "Failed to get calibration data from Dualshock 4\n");
2623 * The Dualshock 4 touchpad supports 2 touches and has a
2624 * resolution of 1920x942 (44.86 dots/mm).
2626 ret
= sony_register_touchpad(sc
, 2, 1920, 942);
2629 "Unable to initialize multi-touch slots: %d\n",
2634 ret
= sony_register_sensors(sc
);
2637 "Unable to initialize motion sensors: %d\n", ret
);
2641 if (sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
) {
2642 sc
->ds4_bt_poll_interval
= DS4_BT_DEFAULT_POLL_INTERVAL_MS
;
2643 ret
= device_create_file(&sc
->hdev
->dev
, &dev_attr_bt_poll_interval
);
2646 "can't create sysfs bt_poll_interval attribute err: %d\n",
2650 if (sc
->quirks
& DUALSHOCK4_DONGLE
) {
2651 INIT_WORK(&sc
->hotplug_worker
, dualshock4_calibration_work
);
2652 sc
->hotplug_worker_initialized
= 1;
2653 sc
->ds4_dongle_state
= DONGLE_DISCONNECTED
;
2656 sony_init_output_report(sc
, dualshock4_send_output_report
);
2657 } else if (sc
->quirks
& MOTION_CONTROLLER
) {
2658 sony_init_output_report(sc
, motion_send_output_report
);
2663 if (sc
->quirks
& SONY_LED_SUPPORT
) {
2664 ret
= sony_leds_init(sc
);
2669 if (sc
->quirks
& SONY_BATTERY_SUPPORT
) {
2670 ret
= sony_battery_probe(sc
, append_dev_id
);
2674 /* Open the device to receive reports with battery info */
2675 ret
= hid_hw_open(hdev
);
2677 hid_err(hdev
, "hw open failed\n");
2682 if (sc
->quirks
& SONY_FF_SUPPORT
) {
2683 ret
= sony_init_ff(sc
);
2692 /* Piggy back on the default ds4_bt_ poll_interval to determine
2693 * if we need to remove the file as we don't know for sure if we
2694 * executed that logic.
2696 if (sc
->ds4_bt_poll_interval
)
2697 device_remove_file(&sc
->hdev
->dev
, &dev_attr_bt_poll_interval
);
2698 if (sc
->quirks
& SONY_LED_SUPPORT
)
2699 sony_leds_remove(sc
);
2700 if (sc
->quirks
& SONY_BATTERY_SUPPORT
)
2701 sony_battery_remove(sc
);
2703 sony_unregister_touchpad(sc
);
2705 sony_unregister_sensors(sc
);
2706 sony_cancel_work_sync(sc
);
2707 kfree(sc
->output_report_dmabuf
);
2708 sony_remove_dev_list(sc
);
2709 sony_release_device_id(sc
);
2714 static int sony_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
2717 unsigned long quirks
= id
->driver_data
;
2719 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
2721 if (!strcmp(hdev
->name
, "FutureMax Dance Mat"))
2722 quirks
|= FUTUREMAX_DANCE_MAT
;
2724 sc
= devm_kzalloc(&hdev
->dev
, sizeof(*sc
), GFP_KERNEL
);
2726 hid_err(hdev
, "can't alloc sony descriptor\n");
2730 spin_lock_init(&sc
->lock
);
2732 sc
->quirks
= quirks
;
2733 hid_set_drvdata(hdev
, sc
);
2736 ret
= hid_parse(hdev
);
2738 hid_err(hdev
, "parse failed\n");
2742 if (sc
->quirks
& VAIO_RDESC_CONSTANT
)
2743 connect_mask
|= HID_CONNECT_HIDDEV_FORCE
;
2744 else if (sc
->quirks
& SIXAXIS_CONTROLLER
)
2745 connect_mask
|= HID_CONNECT_HIDDEV_FORCE
;
2747 /* Patch the hw version on DS3/4 compatible devices, so applications can
2748 * distinguish between the default HID mappings and the mappings defined
2749 * by the Linux game controller spec. This is important for the SDL2
2750 * library, which has a game controller database, which uses device ids
2751 * in combination with version as a key.
2753 if (sc
->quirks
& (SIXAXIS_CONTROLLER
| DUALSHOCK4_CONTROLLER
))
2754 hdev
->version
|= 0x8000;
2756 ret
= hid_hw_start(hdev
, connect_mask
);
2758 hid_err(hdev
, "hw start failed\n");
2762 /* sony_input_configured can fail, but this doesn't result
2763 * in hid_hw_start failures (intended). Check whether
2764 * the HID layer claimed the device else fail.
2765 * We don't know the actual reason for the failure, most
2766 * likely it is due to EEXIST in case of double connection
2767 * of USB and Bluetooth, but could have been due to ENOMEM
2768 * or other reasons as well.
2770 if (!(hdev
->claimed
& HID_CLAIMED_INPUT
)) {
2771 hid_err(hdev
, "failed to claim input\n");
2778 static void sony_remove(struct hid_device
*hdev
)
2780 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
2784 if (sc
->quirks
& SONY_LED_SUPPORT
)
2785 sony_leds_remove(sc
);
2787 if (sc
->quirks
& SONY_BATTERY_SUPPORT
)
2788 sony_battery_remove(sc
);
2791 sony_unregister_touchpad(sc
);
2794 sony_unregister_sensors(sc
);
2796 if (sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
)
2797 device_remove_file(&sc
->hdev
->dev
, &dev_attr_bt_poll_interval
);
2799 sony_cancel_work_sync(sc
);
2801 kfree(sc
->output_report_dmabuf
);
2803 sony_remove_dev_list(sc
);
2805 sony_release_device_id(sc
);
2812 static int sony_suspend(struct hid_device
*hdev
, pm_message_t message
)
2814 #ifdef CONFIG_SONY_FF
2816 /* On suspend stop any running force-feedback events */
2817 if (SONY_FF_SUPPORT
) {
2818 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
2820 sc
->left
= sc
->right
= 0;
2821 sony_send_output_report(sc
);
2828 static int sony_resume(struct hid_device
*hdev
)
2830 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
2833 * The Sixaxis and navigation controllers on USB need to be
2834 * reinitialized on resume or they won't behave properly.
2836 if ((sc
->quirks
& SIXAXIS_CONTROLLER_USB
) ||
2837 (sc
->quirks
& NAVIGATION_CONTROLLER_USB
)) {
2838 sixaxis_set_operational_usb(sc
->hdev
);
2839 sc
->defer_initialization
= 1;
2847 static const struct hid_device_id sony_devices
[] = {
2848 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS3_CONTROLLER
),
2849 .driver_data
= SIXAXIS_CONTROLLER_USB
},
2850 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER
),
2851 .driver_data
= NAVIGATION_CONTROLLER_USB
},
2852 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER
),
2853 .driver_data
= NAVIGATION_CONTROLLER_BT
},
2854 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_MOTION_CONTROLLER
),
2855 .driver_data
= MOTION_CONTROLLER_USB
},
2856 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_MOTION_CONTROLLER
),
2857 .driver_data
= MOTION_CONTROLLER_BT
},
2858 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS3_CONTROLLER
),
2859 .driver_data
= SIXAXIS_CONTROLLER_BT
},
2860 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE
),
2861 .driver_data
= VAIO_RDESC_CONSTANT
},
2862 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE
),
2863 .driver_data
= VAIO_RDESC_CONSTANT
},
2865 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2866 * Logitech joystick from the device descriptor.
2868 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER
),
2869 .driver_data
= BUZZ_CONTROLLER
},
2870 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER
),
2871 .driver_data
= BUZZ_CONTROLLER
},
2872 /* PS3 BD Remote Control */
2873 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS3_BDREMOTE
),
2874 .driver_data
= PS3REMOTE
},
2875 /* Logitech Harmony Adapter for PS3 */
2876 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, USB_DEVICE_ID_LOGITECH_HARMONY_PS3
),
2877 .driver_data
= PS3REMOTE
},
2878 /* SMK-Link PS3 BD Remote Control */
2879 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK
, USB_DEVICE_ID_SMK_PS3_BDREMOTE
),
2880 .driver_data
= PS3REMOTE
},
2881 /* Sony Dualshock 4 controllers for PS4 */
2882 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER
),
2883 .driver_data
= DUALSHOCK4_CONTROLLER_USB
},
2884 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER
),
2885 .driver_data
= DUALSHOCK4_CONTROLLER_BT
},
2886 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2
),
2887 .driver_data
= DUALSHOCK4_CONTROLLER_USB
},
2888 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2
),
2889 .driver_data
= DUALSHOCK4_CONTROLLER_BT
},
2890 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE
),
2891 .driver_data
= DUALSHOCK4_DONGLE
},
2892 /* Nyko Core Controller for PS3 */
2893 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE
, USB_DEVICE_ID_SINO_LITE_CONTROLLER
),
2894 .driver_data
= SIXAXIS_CONTROLLER_USB
| SINO_LITE_CONTROLLER
},
2897 MODULE_DEVICE_TABLE(hid
, sony_devices
);
2899 static struct hid_driver sony_driver
= {
2901 .id_table
= sony_devices
,
2902 .input_mapping
= sony_mapping
,
2903 .input_configured
= sony_input_configured
,
2904 .probe
= sony_probe
,
2905 .remove
= sony_remove
,
2906 .report_fixup
= sony_report_fixup
,
2907 .raw_event
= sony_raw_event
,
2910 .suspend
= sony_suspend
,
2911 .resume
= sony_resume
,
2912 .reset_resume
= sony_resume
,
2916 static int __init
sony_init(void)
2918 dbg_hid("Sony:%s\n", __func__
);
2920 return hid_register_driver(&sony_driver
);
2923 static void __exit
sony_exit(void)
2925 dbg_hid("Sony:%s\n", __func__
);
2927 hid_unregister_driver(&sony_driver
);
2928 ida_destroy(&sony_device_id_allocator
);
2930 module_init(sony_init
);
2931 module_exit(sony_exit
);
2933 MODULE_LICENSE("GPL");