]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hid/hid-sony.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / drivers / hid / hid-sony.c
CommitLineData
bd28ce00 1/*
077147a3 2 * HID driver for Sony / PS2 / PS3 / PS4 BD devices.
bd28ce00
JS
3 *
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
bd28ce00 7 * Copyright (c) 2008 Jiri Slaby
078328da
JK
8 * Copyright (c) 2012 David Dillow <dave@thedillows.org>
9 * Copyright (c) 2006-2013 Jiri Kosina
f04d5140 10 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
c4425c8f 11 * Copyright (c) 2014-2016 Frank Praznik <frank.praznik@gmail.com>
bd28ce00
JS
12 */
13
14/*
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)
18 * any later version.
19 */
20
ad142b9e
FP
21/*
22 * NOTE: in order for the Sony PS3 BD Remote Control to be found by
078328da
JK
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.
25 *
26 * There will be no PIN request from the device.
27 */
28
bd28ce00
JS
29#include <linux/device.h>
30#include <linux/hid.h>
31#include <linux/module.h>
5a0e3ad6 32#include <linux/slab.h>
40e32ee6 33#include <linux/leds.h>
d902f472
FP
34#include <linux/power_supply.h>
35#include <linux/spinlock.h>
d2d782fc 36#include <linux/list.h>
8025087a 37#include <linux/idr.h>
e5606230 38#include <linux/input/mt.h>
49b9ca6c
RC
39#include <linux/crc32.h>
40#include <asm/unaligned.h>
bd28ce00
JS
41
42#include "hid-ids.h"
43
6c79c18c
FP
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)
8ab1676b
FP
49#define DUALSHOCK4_CONTROLLER_USB BIT(5)
50#define DUALSHOCK4_CONTROLLER_BT BIT(6)
35f436c3
RC
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)
cc6e0bbb 58
fee4e2d5 59#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
b3bca326 60#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
4545ee0a
SW
61#define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
62 NAVIGATION_CONTROLLER_BT)
68330d83 63#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
35f436c3
RC
64 DUALSHOCK4_CONTROLLER_BT | \
65 DUALSHOCK4_DONGLE)
fee4e2d5 66#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
4545ee0a
SW
67 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
68 NAVIGATION_CONTROLLER)
12e9a6d7 69#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
4545ee0a 70 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
c5e0c1c4
FP
71#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
72 MOTION_CONTROLLER)
0f398230
FP
73#define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\
74 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
60781cf4
FP
75
76#define MAX_LEDS 4
0a286ef2 77
e57a67da 78
c5e0c1c4 79/* PS/3 Motion controller */
1adf904e 80static u8 motion_rdesc[] = {
c5e0c1c4
FP
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), */
c5e0c1c4 86 0x75, 0x01, /* Report Size (1), */
8b2513c3 87 0x95, 0x15, /* Report Count (21), */
c5e0c1c4
FP
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), */
8b2513c3
SW
94 0x29, 0x15, /* Usage Maximum (15h), */
95 0x81, 0x02, /* Input (Variable), * Buttons */
96 0x95, 0x0B, /* Report Count (11), */
c5e0c1c4 97 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
8b2513c3 98 0x81, 0x03, /* Input (Constant, Variable), * Padding */
c5e0c1c4
FP
99 0x15, 0x00, /* Logical Minimum (0), */
100 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
101 0x05, 0x01, /* Usage Page (Desktop), */
c5e0c1c4
FP
102 0xA1, 0x00, /* Collection (Physical), */
103 0x75, 0x08, /* Report Size (8), */
8b2513c3 104 0x95, 0x01, /* Report Count (1), */
c5e0c1c4
FP
105 0x35, 0x00, /* Physical Minimum (0), */
106 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
107 0x09, 0x30, /* Usage (X), */
8b2513c3 108 0x81, 0x02, /* Input (Variable), * Trigger */
c5e0c1c4 109 0xC0, /* End Collection, */
8b2513c3
SW
110 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
111 0x75, 0x08, /* Report Size (8), */
112 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
c5e0c1c4 113 0x81, 0x02, /* Input (Variable), */
8b2513c3 114 0x05, 0x01, /* Usage Page (Desktop), */
c5e0c1c4 115 0x75, 0x10, /* Report Size (16), */
8b2513c3
SW
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), */
c5e0c1c4 127 0x09, 0x01, /* Usage (Pointer), */
8b2513c3
SW
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 */
c5e0c1c4 132 0x81, 0x02, /* Input (Variable), */
8b2513c3
SW
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), */
c5e0c1c4
FP
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 */
174};
175
1adf904e 176static u8 ps3remote_rdesc[] = {
078328da
JK
177 0x05, 0x01, /* GUsagePage Generic Desktop */
178 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
179 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
180
181 /* Use collection 1 for joypad buttons */
182 0xA1, 0x02, /* MCollection Logical (interrelated data) */
183
ef916ef5
AO
184 /*
185 * Ignore the 1st byte, maybe it is used for a controller
186 * number but it's not needed for correct operation
187 */
078328da
JK
188 0x75, 0x08, /* GReportSize 0x08 [8] */
189 0x95, 0x01, /* GReportCount 0x01 [1] */
190 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
191
ef916ef5
AO
192 /*
193 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
194 * buttons multiple keypresses are allowed
195 */
078328da
JK
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]) */
204
205 0xC0, /* MEndCollection */
206
207 /* Use collection 2 for remote control buttons */
208 0xA1, 0x02, /* MCollection Logical (interrelated data) */
209
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] */
218 0x80, /* MInput */
219
ef916ef5
AO
220 /*
221 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
222 * 0xff and 11th is for press indication
223 */
078328da
JK
224 0x75, 0x08, /* GReportSize 0x08 [8] */
225 0x95, 0x06, /* GReportCount 0x06 [6] */
226 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
227
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]) */
236
237 0xC0, /* MEndCollection */
238
239 0xC0 /* MEndCollection [Game Pad] */
240};
241
242static const unsigned int ps3remote_keymap_joypad_buttons[] = {
243 [0x01] = KEY_SELECT,
244 [0x02] = BTN_THUMBL, /* L3 */
245 [0x03] = BTN_THUMBR, /* R3 */
246 [0x04] = BTN_START,
247 [0x05] = KEY_UP,
248 [0x06] = KEY_RIGHT,
249 [0x07] = KEY_DOWN,
250 [0x08] = KEY_LEFT,
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 */
260 [0x14] = KEY_ENTER,
261};
262static const unsigned int ps3remote_keymap_remote_buttons[] = {
263 [0x00] = KEY_1,
264 [0x01] = KEY_2,
265 [0x02] = KEY_3,
266 [0x03] = KEY_4,
267 [0x04] = KEY_5,
268 [0x05] = KEY_6,
269 [0x06] = KEY_7,
270 [0x07] = KEY_8,
271 [0x08] = KEY_9,
272 [0x09] = KEY_0,
273 [0x0e] = KEY_ESC, /* return */
274 [0x0f] = KEY_CLEAR,
275 [0x16] = KEY_EJECTCD,
276 [0x1a] = KEY_MENU, /* top menu */
277 [0x28] = KEY_TIME,
278 [0x30] = KEY_PREVIOUS,
279 [0x31] = KEY_NEXT,
280 [0x32] = KEY_PLAY,
281 [0x33] = KEY_REWIND, /* scan back */
282 [0x34] = KEY_FORWARD, /* scan forward */
283 [0x38] = KEY_STOP,
284 [0x39] = KEY_PAUSE,
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,
289 [0x64] = KEY_AUDIO,
290 [0x65] = KEY_ANGLE,
291 [0x70] = KEY_INFO, /* display */
292 [0x80] = KEY_BLUE,
293 [0x81] = KEY_RED,
294 [0x82] = KEY_GREEN,
295 [0x83] = KEY_YELLOW,
296};
297
f04d5140 298static const unsigned int buzz_keymap[] = {
ad142b9e
FP
299 /*
300 * The controller has 4 remote buzzers, each with one LED and 5
f04d5140 301 * buttons.
09593e38 302 *
f04d5140
CL
303 * We use the mapping chosen by the controller, which is:
304 *
305 * Key Offset
306 * -------------------
307 * Buzz 1
308 * Blue 5
309 * Orange 4
310 * Green 3
311 * Yellow 2
312 *
313 * So, for example, the orange button on the third buzzer is mapped to
314 * BTN_TRIGGER_HAPPY14
315 */
09593e38
AO
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,
f04d5140
CL
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,
336};
337
b8f0970d
RC
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.
342 */
343static const unsigned int navigation_absmap[] = {
344 [0x30] = ABS_X,
345 [0x31] = ABS_Y,
346 [0x33] = ABS_Z, /* L2 */
347};
348
349/* Buttons not physically available on the device, but still available
350 * in the reports are explicitly set to 0 for documentation purposes.
351 */
352static const unsigned int navigation_keymap[] = {
353 [0x01] = 0, /* Select */
354 [0x02] = BTN_THUMBL, /* L3 */
355 [0x03] = 0, /* R3 */
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 */
362 [0x0a] = 0, /* R2 */
363 [0x0b] = BTN_TL, /* L1 */
364 [0x0c] = 0, /* R1 */
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 */
370};
371
e19a267b
RC
372static const unsigned int sixaxis_absmap[] = {
373 [0x30] = ABS_X,
374 [0x31] = ABS_Y,
375 [0x32] = ABS_RX, /* right stick X */
376 [0x35] = ABS_RY, /* right stick Y */
377};
378
379static 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 */
397};
398
9131f8cc
RC
399static const unsigned int ds4_absmap[] = {
400 [0x30] = ABS_X,
401 [0x31] = ABS_Y,
402 [0x32] = ABS_RX, /* right stick X */
403 [0x33] = ABS_Z, /* L2 */
404 [0x34] = ABS_RZ, /* R2 */
405 [0x35] = ABS_RY, /* right stick Y */
406};
407
408static 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 */
422};
423
d03ae2e1
RC
424static 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},
426 {0, 0}
427};
9131f8cc 428
d902f472
FP
429static 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,
434};
435
55d3b664 436struct sixaxis_led {
1adf904e
PM
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") */
439 u8 enabled;
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%) */
55d3b664
FP
442} __packed;
443
444struct sixaxis_rumble {
1adf904e
PM
445 u8 padding;
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 */
55d3b664
FP
450} __packed;
451
452struct sixaxis_output_report {
1adf904e 453 u8 report_id;
55d3b664 454 struct sixaxis_rumble rumble;
1adf904e
PM
455 u8 padding[4];
456 u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
55d3b664
FP
457 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
458 struct sixaxis_led _reserved; /* LED5, not actually soldered */
459} __packed;
460
461union sixaxis_output_report_01 {
462 struct sixaxis_output_report data;
1adf904e 463 u8 buf[36];
55d3b664
FP
464};
465
c5e0c1c4
FP
466struct motion_output_report_02 {
467 u8 type, zero;
468 u8 r, g, b;
469 u8 zero2;
470 u8 rumble;
471};
472
2c159de0 473#define DS4_FEATURE_REPORT_0x02_SIZE 37
55a07d62 474#define DS4_FEATURE_REPORT_0x05_SIZE 41
2c159de0 475#define DS4_FEATURE_REPORT_0x81_SIZE 7
49b9ca6c 476#define DS4_INPUT_REPORT_0x11_SIZE 78
2c159de0
RC
477#define DS4_OUTPUT_REPORT_0x05_SIZE 32
478#define DS4_OUTPUT_REPORT_0x11_SIZE 78
29b691a8 479#define SIXAXIS_REPORT_0xF2_SIZE 17
a85d67b5 480#define SIXAXIS_REPORT_0xF5_SIZE 8
41d2d425 481#define MOTION_REPORT_0x02_SIZE 49
9b2b5c9a 482
cdc1c021
RC
483/* Offsets relative to USB input report (0x1). Bluetooth (0x11) requires an
484 * additional +2.
485 */
d03ae2e1 486#define DS4_INPUT_REPORT_AXIS_OFFSET 1
ac797b95 487#define DS4_INPUT_REPORT_BUTTON_OFFSET 5
80786eb9 488#define DS4_INPUT_REPORT_TIMESTAMP_OFFSET 10
227c011b 489#define DS4_INPUT_REPORT_GYRO_X_OFFSET 13
cdc1c021
RC
490#define DS4_INPUT_REPORT_BATTERY_OFFSET 30
491#define DS4_INPUT_REPORT_TOUCHPAD_OFFSET 33
492
510c8b7c 493#define SENSOR_SUFFIX " Motion Sensors"
ac797b95
RC
494#define DS4_TOUCHPAD_SUFFIX " Touchpad"
495
77b499e7
RC
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
55a07d62
RC
499#define DS4_GYRO_RES_PER_DEG_S 1024
500#define DS4_ACC_RES_PER_G 8192
501
510c8b7c
RC
502#define SIXAXIS_INPUT_REPORT_ACC_X_OFFSET 41
503#define SIXAXIS_ACC_RES_PER_G 113
504
8b402c92 505static DEFINE_SPINLOCK(sony_dev_list_lock);
d2d782fc 506static LIST_HEAD(sony_device_list);
8025087a 507static DEFINE_IDA(sony_device_id_allocator);
d2d782fc 508
55a07d62
RC
509/* Used for calibration of DS4 accelerometer and gyro. */
510struct ds4_calibration_data {
511 int abs_code;
512 short bias;
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.
516 */
517 int sens_numer;
518 int sens_denom;
519};
520
f2f47c38
RC
521enum ds4_dongle_state {
522 DONGLE_DISCONNECTED,
523 DONGLE_CALIBRATING,
524 DONGLE_CONNECTED,
525 DONGLE_DISABLED
526};
527
b5322736 528enum sony_worker {
f2f47c38
RC
529 SONY_WORKER_STATE,
530 SONY_WORKER_HOTPLUG
b5322736
RC
531};
532
cc6e0bbb 533struct sony_sc {
d902f472 534 spinlock_t lock;
d2d782fc 535 struct list_head list_node;
0a286ef2 536 struct hid_device *hdev;
ac797b95 537 struct input_dev *touchpad;
227c011b 538 struct input_dev *sensor_dev;
60781cf4 539 struct led_classdev *leds[MAX_LEDS];
cc6e0bbb 540 unsigned long quirks;
f2f47c38 541 struct work_struct hotplug_worker;
0a286ef2 542 struct work_struct state_worker;
09593e38 543 void (*send_output_report)(struct sony_sc *);
297d716f
KK
544 struct power_supply *battery;
545 struct power_supply_desc battery_desc;
8025087a 546 int device_id;
1adf904e 547 u8 *output_report_dmabuf;
f04d5140 548
9f323b68 549#ifdef CONFIG_SONY_FF
1adf904e
PM
550 u8 left;
551 u8 right;
9f323b68
SE
552#endif
553
1adf904e 554 u8 mac_address[6];
f2f47c38 555 u8 hotplug_worker_initialized;
b5322736 556 u8 state_worker_initialized;
2a242932 557 u8 defer_initialization;
1adf904e
PM
558 u8 cable_state;
559 u8 battery_charging;
560 u8 battery_capacity;
561 u8 led_state[MAX_LEDS];
1adf904e
PM
562 u8 led_delay_on[MAX_LEDS];
563 u8 led_delay_off[MAX_LEDS];
564 u8 led_count;
80786eb9
RC
565
566 bool timestamp_initialized;
567 u16 prev_timestamp;
568 unsigned int timestamp_us;
569
77b499e7 570 u8 ds4_bt_poll_interval;
f2f47c38 571 enum ds4_dongle_state ds4_dongle_state;
55a07d62
RC
572 /* DS4 calibration data */
573 struct ds4_calibration_data ds4_calib_data[6];
cc6e0bbb
JK
574};
575
405182c2
RC
576static void sony_set_leds(struct sony_sc *sc);
577
b5322736
RC
578static inline void sony_schedule_work(struct sony_sc *sc,
579 enum sony_worker which)
2a242932 580{
b5322736
RC
581 switch (which) {
582 case SONY_WORKER_STATE:
583 if (!sc->defer_initialization)
584 schedule_work(&sc->state_worker);
f2f47c38
RC
585 break;
586 case SONY_WORKER_HOTPLUG:
587 if (sc->hotplug_worker_initialized)
588 schedule_work(&sc->hotplug_worker);
589 break;
b5322736 590 }
2a242932
FP
591}
592
77b499e7
RC
593static ssize_t ds4_show_poll_interval(struct device *dev,
594 struct device_attribute
595 *attr, char *buf)
c607fb8d 596{
77b499e7
RC
597 struct hid_device *hdev = to_hid_device(dev);
598 struct sony_sc *sc = hid_get_drvdata(hdev);
599
600 return snprintf(buf, PAGE_SIZE, "%i\n", sc->ds4_bt_poll_interval);
c607fb8d
AO
601}
602
77b499e7
RC
603static ssize_t ds4_store_poll_interval(struct device *dev,
604 struct device_attribute *attr,
605 const char *buf, size_t count)
c5e0c1c4 606{
77b499e7
RC
607 struct hid_device *hdev = to_hid_device(dev);
608 struct sony_sc *sc = hid_get_drvdata(hdev);
609 unsigned long flags;
610 u8 interval;
611
612 if (kstrtou8(buf, 0, &interval))
613 return -EINVAL;
614
615 if (interval > DS4_BT_MAX_POLL_INTERVAL_MS)
616 return -EINVAL;
617
618 spin_lock_irqsave(&sc->lock, flags);
619 sc->ds4_bt_poll_interval = interval;
620 spin_unlock_irqrestore(&sc->lock, flags);
621
622 sony_schedule_work(sc, SONY_WORKER_STATE);
623
624 return count;
c5e0c1c4
FP
625}
626
77b499e7
RC
627static DEVICE_ATTR(bt_poll_interval, 0644, ds4_show_poll_interval,
628 ds4_store_poll_interval);
629
630
c5e0c1c4 631static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
b2723eb7
SW
632 unsigned int *rsize)
633{
c5e0c1c4
FP
634 *rsize = sizeof(motion_rdesc);
635 return motion_rdesc;
b2723eb7
SW
636}
637
1adf904e 638static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
078328da
JK
639 unsigned int *rsize)
640{
641 *rsize = sizeof(ps3remote_rdesc);
642 return ps3remote_rdesc;
643}
644
645static 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)
648{
649 unsigned int key = usage->hid & HID_USAGE;
650
651 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
652 return -1;
653
654 switch (usage->collection_index) {
655 case 1:
656 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
657 return -1;
658
659 key = ps3remote_keymap_joypad_buttons[key];
660 if (!key)
661 return -1;
662 break;
663 case 2:
664 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
665 return -1;
666
667 key = ps3remote_keymap_remote_buttons[key];
668 if (!key)
669 return -1;
670 break;
671 default:
672 return -1;
673 }
674
675 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
676 return 1;
677}
678
b8f0970d
RC
679static 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)
682{
683 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
684 unsigned int key = usage->hid & HID_USAGE;
685
686 if (key >= ARRAY_SIZE(sixaxis_keymap))
687 return -1;
688
689 key = navigation_keymap[key];
690 if (!key)
691 return -1;
692
693 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
694 return 1;
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.
700 */
701 switch (usage->usage_index) {
702 case 8: /* L2 */
703 usage->hid = HID_GD_Z;
704 break;
705 default:
706 return -1;
707 }
708
709 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
710 return 1;
711 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
712 unsigned int abs = usage->hid & HID_USAGE;
713
714 if (abs >= ARRAY_SIZE(navigation_absmap))
715 return -1;
716
717 abs = navigation_absmap[abs];
718
719 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
720 return 1;
721 }
722
723 return -1;
724}
725
726
e19a267b
RC
727static 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)
730{
731 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
732 unsigned int key = usage->hid & HID_USAGE;
733
734 if (key >= ARRAY_SIZE(sixaxis_keymap))
735 return -1;
736
737 key = sixaxis_keymap[key];
738 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
739 return 1;
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.
746 */
747 switch (usage->usage_index) {
748 case 8: /* L2 */
749 usage->hid = HID_GD_Z;
750 break;
751 case 9: /* R2 */
752 usage->hid = HID_GD_RZ;
753 break;
754 default:
755 return -1;
756 }
757
758 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
759 return 1;
760 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
761 unsigned int abs = usage->hid & HID_USAGE;
762
763 if (abs >= ARRAY_SIZE(sixaxis_absmap))
764 return -1;
765
766 abs = sixaxis_absmap[abs];
767
768 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
769 return 1;
770 }
771
772 return -1;
773}
774
9131f8cc
RC
775static 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)
778{
779 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
780 unsigned int key = usage->hid & HID_USAGE;
781
782 if (key >= ARRAY_SIZE(ds4_keymap))
783 return -1;
784
785 key = ds4_keymap[key];
786 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
787 return 1;
788 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
789 unsigned int abs = usage->hid & HID_USAGE;
790
791 /* Let the HID parser deal with the HAT. */
792 if (usage->hid == HID_GD_HATSWITCH)
793 return 0;
794
795 if (abs >= ARRAY_SIZE(ds4_absmap))
796 return -1;
797
798 abs = ds4_absmap[abs];
799 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
800 return 1;
801 }
802
803 return 0;
804}
805
1adf904e 806static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
73e4008d 807 unsigned int *rsize)
cc6e0bbb
JK
808{
809 struct sony_sc *sc = hid_get_drvdata(hdev);
810
4ba1eeeb 811 if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
74500cc8
SM
812 return rdesc;
813
99d24902
FLVC
814 /*
815 * Some Sony RF receivers wrongly declare the mouse pointer as a
816 * a constant non-data variable.
817 */
818 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
819 /* usage page: generic desktop controls */
820 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
821 /* usage: mouse */
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) {
a4649184 825 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
99d24902 826 /* input: data, variable, relative */
cc6e0bbb
JK
827 rdesc[55] = 0x06;
828 }
61ab44be 829
c5e0c1c4
FP
830 if (sc->quirks & MOTION_CONTROLLER)
831 return motion_fixup(hdev, rdesc, rsize);
832
078328da
JK
833 if (sc->quirks & PS3REMOTE)
834 return ps3remote_fixup(hdev, rdesc, rsize);
835
73e4008d 836 return rdesc;
cc6e0bbb
JK
837}
838
1adf904e 839static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
d902f472 840{
1adf904e 841 static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
d902f472 842 unsigned long flags;
12e9a6d7 843 int offset;
1adf904e 844 u8 cable_state, battery_capacity, battery_charging;
d902f472 845
ad142b9e
FP
846 /*
847 * The sixaxis is charging if the battery value is 0xee
d902f472
FP
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.
851 */
12e9a6d7
SW
852 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
853
854 if (rd[offset] >= 0xee) {
d902f472 855 battery_capacity = 100;
12e9a6d7 856 battery_charging = !(rd[offset] & 0x01);
9fddd74a 857 cable_state = 1;
d902f472 858 } else {
1adf904e 859 u8 index = rd[offset] <= 5 ? rd[offset] : 5;
ac3c9a94 860 battery_capacity = sixaxis_battery_capacity[index];
d902f472 861 battery_charging = 0;
9fddd74a 862 cable_state = 0;
d902f472 863 }
d902f472
FP
864
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);
510c8b7c
RC
870
871 if (sc->quirks & SIXAXIS_CONTROLLER) {
872 int val;
873
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);
877
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);
881
882 val = 511 - ((rd[offset+3] << 8) | rd[offset+2]);
883 input_report_abs(sc->sensor_dev, ABS_Z, val);
884
885 input_sync(sc->sensor_dev);
886 }
d902f472
FP
887}
888
1adf904e 889static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
d902f472 890{
d03ae2e1
RC
891 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
892 struct hid_input, list);
893 struct input_dev *input_dev = hidinput->input;
d902f472 894 unsigned long flags;
cdc1c021 895 int n, m, offset, num_touch_data, max_touch_data;
1adf904e 896 u8 cable_state, battery_capacity, battery_charging;
80786eb9 897 u16 timestamp;
d902f472 898
cdc1c021 899 /* When using Bluetooth the header is 2 bytes longer, so skip these. */
35f436c3 900 int data_offset = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 2 : 0;
6c5f860d 901
ac797b95
RC
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);
905
d03ae2e1
RC
906 /*
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.
919 */
920 if (rd[0] == 17) {
921 int value;
922
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]);
928
929 value = rd[offset+4] & 0xf;
930 if (value > 7)
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);
934
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);
939
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);
948
949 input_report_key(input_dev, BTN_MODE, rd[offset+6] & 0x1);
950
951 input_report_abs(input_dev, ABS_Z, rd[offset+7]);
952 input_report_abs(input_dev, ABS_RZ, rd[offset+8]);
953
954 input_sync(input_dev);
955 }
956
80786eb9
RC
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;
963 } else {
964 u16 delta;
965
966 if (sc->prev_timestamp > timestamp)
967 delta = (U16_MAX - sc->prev_timestamp + timestamp + 1);
968 else
969 delta = timestamp - sc->prev_timestamp;
970 sc->timestamp_us += (delta * 16) / 3;
971 }
972 sc->prev_timestamp = timestamp;
973 input_event(sc->sensor_dev, EV_MSC, MSC_TIMESTAMP, sc->timestamp_us);
974
227c011b 975 offset = data_offset + DS4_INPUT_REPORT_GYRO_X_OFFSET;
55a07d62
RC
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];
980
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.
985 */
986 int calib_data = mult_frac(calib->sens_numer,
987 raw_data - calib->bias,
988 calib->sens_denom);
227c011b 989
55a07d62
RC
990 input_report_abs(sc->sensor_dev, calib->abs_code, calib_data);
991 offset += 2;
227c011b
RC
992 }
993 input_sync(sc->sensor_dev);
994
ad142b9e 995 /*
cdc1c021 996 * The lower 4 bits of byte 30 (or 32 for BT) contain the battery level
d902f472
FP
997 * and the 5th bit contains the USB cable state.
998 */
cdc1c021 999 offset = data_offset + DS4_INPUT_REPORT_BATTERY_OFFSET;
6c5f860d
FP
1000 cable_state = (rd[offset] >> 4) & 0x01;
1001 battery_capacity = rd[offset] & 0x0F;
d902f472 1002
ad142b9e
FP
1003 /*
1004 * When a USB power source is connected the battery level ranges from
6c5f860d
FP
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.
d902f472 1007 */
6c5f860d 1008 if (!cable_state || battery_capacity > 10)
d902f472
FP
1009 battery_charging = 0;
1010 else
1011 battery_charging = 1;
1012
6c5f860d
FP
1013 if (!cable_state)
1014 battery_capacity++;
d902f472 1015 if (battery_capacity > 10)
6c5f860d
FP
1016 battery_capacity = 10;
1017
d902f472
FP
1018 battery_capacity *= 10;
1019
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);
e5606230 1025
ad142b9e 1026 /*
cdc1c021
RC
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).
e5606230 1031 */
cdc1c021 1032 offset = data_offset + DS4_INPUT_REPORT_TOUCHPAD_OFFSET;
35f436c3 1033 max_touch_data = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 4 : 3;
cdc1c021
RC
1034 if (rd[offset] > 0 && rd[offset] <= max_touch_data)
1035 num_touch_data = rd[offset];
1036 else
1037 num_touch_data = 1;
1038 offset += 1;
e5606230 1039
cdc1c021
RC
1040 for (m = 0; m < num_touch_data; m++) {
1041 /* Skip past timestamp */
1042 offset += 1;
e5606230 1043
cdc1c021
RC
1044 /*
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
1047 * pressed.
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.
1051 */
1052 for (n = 0; n < 2; n++) {
1053 u16 x, y;
1054 bool active;
1055
1056 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1057 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1058
1059 active = !(rd[offset] >> 7);
ac797b95
RC
1060 input_mt_slot(sc->touchpad, n);
1061 input_mt_report_slot_state(sc->touchpad, MT_TOOL_FINGER, active);
cdc1c021
RC
1062
1063 if (active) {
ac797b95
RC
1064 input_report_abs(sc->touchpad, ABS_MT_POSITION_X, x);
1065 input_report_abs(sc->touchpad, ABS_MT_POSITION_Y, y);
cdc1c021
RC
1066 }
1067
1068 offset += 4;
1069 }
ac797b95
RC
1070 input_mt_sync_frame(sc->touchpad);
1071 input_sync(sc->touchpad);
e5606230 1072 }
d902f472
FP
1073}
1074
c9e4d877 1075static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1adf904e 1076 u8 *rd, int size)
c9e4d877
SW
1077{
1078 struct sony_sc *sc = hid_get_drvdata(hdev);
1079
ad142b9e
FP
1080 /*
1081 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
c9e4d877
SW
1082 * has to be BYTE_SWAPPED before passing up to joystick interface
1083 */
fee4e2d5 1084 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
8f5f0bc2
FP
1085 /*
1086 * When connected via Bluetooth the Sixaxis occasionally sends
1087 * a report with the second byte 0xff and the rest zeroed.
1088 *
1089 * This report does not reflect the actual state of the
1090 * controller must be ignored to avoid generating false input
1091 * events.
1092 */
1093 if (rd[1] == 0xff)
1094 return -EINVAL;
1095
c9e4d877
SW
1096 swap(rd[41], rd[42]);
1097 swap(rd[43], rd[44]);
1098 swap(rd[45], rd[46]);
1099 swap(rd[47], rd[48]);
d902f472 1100
12e9a6d7
SW
1101 sixaxis_parse_report(sc, rd, size);
1102 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
d902f472 1103 sixaxis_parse_report(sc, rd, size);
4545ee0a
SW
1104 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1105 size == 49) {
1106 sixaxis_parse_report(sc, rd, size);
35f436c3
RC
1107 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1108 size == 64) {
1109 dualshock4_parse_report(sc, rd, size);
1110 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && rd[0] == 0x11 &&
1111 size == 78)) {
1112 /* CRC check */
1113 u8 bthdr = 0xA1;
1114 u32 crc;
1115 u32 report_crc;
49b9ca6c 1116
35f436c3
RC
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",
1122 report_crc, crc);
1123 return -EILSEQ;
49b9ca6c 1124 }
405182c2 1125
35f436c3
RC
1126 dualshock4_parse_report(sc, rd, size);
1127 } else if ((sc->quirks & DUALSHOCK4_DONGLE) && rd[0] == 0x01 &&
1128 size == 64) {
f2f47c38
RC
1129 unsigned long flags;
1130 enum ds4_dongle_state dongle_state;
1131
405182c2
RC
1132 /*
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).
1136 */
35f436c3
RC
1137 bool connected = (rd[31] & 0x04) ? false : true;
1138
f2f47c38
RC
1139 spin_lock_irqsave(&sc->lock, flags);
1140 dongle_state = sc->ds4_dongle_state;
1141 spin_unlock_irqrestore(&sc->lock, flags);
1142
1143 /*
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.
1150 */
1151 if (dongle_state == DONGLE_DISCONNECTED && connected) {
35f436c3
RC
1152 hid_info(sc->hdev, "DualShock 4 USB dongle: controller connected\n");
1153 sony_set_leds(sc);
f2f47c38
RC
1154
1155 spin_lock_irqsave(&sc->lock, flags);
1156 sc->ds4_dongle_state = DONGLE_CALIBRATING;
1157 spin_unlock_irqrestore(&sc->lock, flags);
1158
1159 sony_schedule_work(sc, SONY_WORKER_HOTPLUG);
1160
1161 /* Don't process the report since we don't have
1162 * calibration data, but let hidraw have it anyway.
1163 */
1164 return 0;
1165 } else if ((dongle_state == DONGLE_CONNECTED ||
1166 dongle_state == DONGLE_DISABLED) && !connected) {
35f436c3 1167 hid_info(sc->hdev, "DualShock 4 USB dongle: controller disconnected\n");
f2f47c38
RC
1168
1169 spin_lock_irqsave(&sc->lock, flags);
1170 sc->ds4_dongle_state = DONGLE_DISCONNECTED;
1171 spin_unlock_irqrestore(&sc->lock, flags);
1172
35f436c3
RC
1173 /* Return 0, so hidraw can get the report. */
1174 return 0;
f2f47c38
RC
1175 } else if (dongle_state == DONGLE_CALIBRATING ||
1176 dongle_state == DONGLE_DISABLED ||
1177 dongle_state == DONGLE_DISCONNECTED) {
35f436c3
RC
1178 /* Return 0, so hidraw can get the report. */
1179 return 0;
405182c2
RC
1180 }
1181
d902f472 1182 dualshock4_parse_report(sc, rd, size);
c9e4d877
SW
1183 }
1184
2a242932
FP
1185 if (sc->defer_initialization) {
1186 sc->defer_initialization = 0;
b5322736 1187 sony_schedule_work(sc, SONY_WORKER_STATE);
2a242932
FP
1188 }
1189
c9e4d877
SW
1190 return 0;
1191}
1192
f04d5140
CL
1193static 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)
1196{
1197 struct sony_sc *sc = hid_get_drvdata(hdev);
1198
1199 if (sc->quirks & BUZZ_CONTROLLER) {
1200 unsigned int key = usage->hid & HID_USAGE;
1201
1202 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1203 return -1;
1204
1205 switch (usage->collection_index) {
1206 case 1:
1207 if (key >= ARRAY_SIZE(buzz_keymap))
1208 return -1;
1209
1210 key = buzz_keymap[key];
1211 if (!key)
1212 return -1;
1213 break;
1214 default:
1215 return -1;
1216 }
1217
1218 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1219 return 1;
1220 }
1221
078328da
JK
1222 if (sc->quirks & PS3REMOTE)
1223 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1224
b8f0970d
RC
1225 if (sc->quirks & NAVIGATION_CONTROLLER)
1226 return navigation_mapping(hdev, hi, field, usage, bit, max);
1227
e19a267b
RC
1228 if (sc->quirks & SIXAXIS_CONTROLLER)
1229 return sixaxis_mapping(hdev, hi, field, usage, bit, max);
9131f8cc
RC
1230
1231 if (sc->quirks & DUALSHOCK4_CONTROLLER)
1232 return ds4_mapping(hdev, hi, field, usage, bit, max);
1233
e19a267b 1234
6f498018
BT
1235 /* Let hid-core decide for the others */
1236 return 0;
f04d5140
CL
1237}
1238
ac797b95 1239static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
ce8efc3b
FP
1240 int w, int h)
1241{
ac797b95
RC
1242 size_t name_sz;
1243 char *name;
ce8efc3b
FP
1244 int ret;
1245
ac797b95
RC
1246 sc->touchpad = input_allocate_device();
1247 if (!sc->touchpad)
1248 return -ENOMEM;
1249
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;
1258
1259 /* Append a suffix to the controller name as there are various
1260 * DS4 compatible non-Sony devices with different names.
1261 */
1262 name_sz = strlen(sc->hdev->name) + sizeof(DS4_TOUCHPAD_SUFFIX);
1263 name = kzalloc(name_sz, GFP_KERNEL);
1264 if (!name) {
1265 ret = -ENOMEM;
1266 goto err;
1267 }
1268 snprintf(name, name_sz, "%s" DS4_TOUCHPAD_SUFFIX, sc->hdev->name);
1269 sc->touchpad->name = name;
1270
b9f7d245 1271 ret = input_mt_init_slots(sc->touchpad, touch_count, INPUT_MT_POINTER);
ce8efc3b 1272 if (ret < 0)
ac797b95
RC
1273 goto err;
1274
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);
ce8efc3b 1279
ac797b95
RC
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);
1282
1283 ret = input_register_device(sc->touchpad);
1284 if (ret < 0)
1285 goto err;
ce8efc3b
FP
1286
1287 return 0;
ac797b95
RC
1288
1289err:
1290 kfree(sc->touchpad->name);
1291 sc->touchpad->name = NULL;
1292
1293 input_free_device(sc->touchpad);
1294 sc->touchpad = NULL;
1295
1296 return ret;
ce8efc3b
FP
1297}
1298
ac797b95
RC
1299static void sony_unregister_touchpad(struct sony_sc *sc)
1300{
1301 if (!sc->touchpad)
1302 return;
1303
1304 kfree(sc->touchpad->name);
1305 sc->touchpad->name = NULL;
1306
1307 input_unregister_device(sc->touchpad);
1308 sc->touchpad = NULL;
1309}
ce8efc3b 1310
227c011b
RC
1311static int sony_register_sensors(struct sony_sc *sc)
1312{
1313 size_t name_sz;
1314 char *name;
1315 int ret;
55a07d62 1316 int range;
227c011b
RC
1317
1318 sc->sensor_dev = input_allocate_device();
1319 if (!sc->sensor_dev)
1320 return -ENOMEM;
1321
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;
1330
1331 /* Append a suffix to the controller name as there are various
1332 * DS4 compatible non-Sony devices with different names.
1333 */
510c8b7c 1334 name_sz = strlen(sc->hdev->name) + sizeof(SENSOR_SUFFIX);
227c011b
RC
1335 name = kzalloc(name_sz, GFP_KERNEL);
1336 if (!name) {
1337 ret = -ENOMEM;
1338 goto err;
1339 }
510c8b7c 1340 snprintf(name, name_sz, "%s" SENSOR_SUFFIX, sc->hdev->name);
227c011b
RC
1341 sc->sensor_dev->name = name;
1342
510c8b7c
RC
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.
1349 */
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);
1364
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);
1372
1373 __set_bit(EV_MSC, sc->sensor_dev->evbit);
1374 __set_bit(MSC_TIMESTAMP, sc->sensor_dev->mscbit);
1375 }
1376
227c011b
RC
1377 __set_bit(INPUT_PROP_ACCELEROMETER, sc->sensor_dev->propbit);
1378
1379 ret = input_register_device(sc->sensor_dev);
1380 if (ret < 0)
1381 goto err;
1382
1383 return 0;
1384
1385err:
1386 kfree(sc->sensor_dev->name);
1387 sc->sensor_dev->name = NULL;
1388
1389 input_free_device(sc->sensor_dev);
1390 sc->sensor_dev = NULL;
1391
1392 return ret;
1393}
1394
1395static void sony_unregister_sensors(struct sony_sc *sc)
1396{
1397 if (!sc->sensor_dev)
1398 return;
1399
1400 kfree(sc->sensor_dev->name);
1401 sc->sensor_dev->name = NULL;
1402
1403 input_unregister_device(sc->sensor_dev);
1404 sc->sensor_dev = NULL;
1405}
1406
1407
bd28ce00
JS
1408/*
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
1411 * events.
1412 */
816651a7 1413static int sixaxis_set_operational_usb(struct hid_device *hdev)
bd28ce00 1414{
a85d67b5
AO
1415 const int buf_size =
1416 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
1adf904e 1417 u8 *buf;
bd28ce00 1418 int ret;
bd28ce00 1419
2e701a35 1420 buf = kmalloc(buf_size, GFP_KERNEL);
bd28ce00
JS
1421 if (!buf)
1422 return -ENOMEM;
1423
a85d67b5
AO
1424 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1425 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
a7de9b86
LK
1426 if (ret < 0) {
1427 hid_err(hdev, "can't set operational mode: step 1\n");
1428 goto out;
1429 }
f204828a 1430
a7de9b86
LK
1431 /*
1432 * Some compatible controllers like the Speedlink Strike FX and
1433 * Gasia need another query plus an USB interrupt to get operational.
1434 */
a85d67b5
AO
1435 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1436 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
a7de9b86
LK
1437 if (ret < 0) {
1438 hid_err(hdev, "can't set operational mode: step 2\n");
1439 goto out;
1440 }
f204828a 1441
a7de9b86 1442 ret = hid_hw_output_report(hdev, buf, 1);
19f4c2ba
BT
1443 if (ret < 0) {
1444 hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
1445 ret = 0;
1446 }
bd28ce00 1447
a7de9b86 1448out:
bd28ce00
JS
1449 kfree(buf);
1450
1451 return ret;
1452}
1453
816651a7 1454static int sixaxis_set_operational_bt(struct hid_device *hdev)
f9ce7c28 1455{
1adf904e
PM
1456 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1457 u8 *buf;
9b2b5c9a
FP
1458 int ret;
1459
1460 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1461 if (!buf)
1462 return -ENOMEM;
1463
1464 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
b0dd72aa 1465 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
9b2b5c9a
FP
1466
1467 kfree(buf);
1468
1469 return ret;
f9ce7c28
BN
1470}
1471
ad142b9e 1472/*
55a07d62
RC
1473 * Request DS4 calibration data for the motion sensors.
1474 * For Bluetooth this also affects the operating mode (see below).
68330d83 1475 */
55a07d62 1476static int dualshock4_get_calibration_data(struct sony_sc *sc)
68330d83 1477{
1adf904e 1478 u8 *buf;
9b2b5c9a 1479 int ret;
55a07d62
RC
1480 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
1481 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
1482 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
1483 short gyro_speed_plus, gyro_speed_minus;
1484 short acc_x_plus, acc_x_minus;
1485 short acc_y_plus, acc_y_minus;
1486 short acc_z_plus, acc_z_minus;
1487 int speed_2x;
1488 int range_2g;
1489
1490 /* For Bluetooth we use a different request, which supports CRC.
1491 * Note: in Bluetooth mode feature report 0x02 also changes the state
1492 * of the controller, so that it sends input reports of type 0x11.
1493 */
35f436c3 1494 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
55a07d62
RC
1495 buf = kmalloc(DS4_FEATURE_REPORT_0x02_SIZE, GFP_KERNEL);
1496 if (!buf)
1497 return -ENOMEM;
68330d83 1498
55a07d62
RC
1499 ret = hid_hw_raw_request(sc->hdev, 0x02, buf,
1500 DS4_FEATURE_REPORT_0x02_SIZE,
1501 HID_FEATURE_REPORT,
1502 HID_REQ_GET_REPORT);
1503 if (ret < 0)
1504 goto err_stop;
1505 } else {
1506 u8 bthdr = 0xA3;
1507 u32 crc;
1508 u32 report_crc;
1509 int retries;
9b2b5c9a 1510
55a07d62
RC
1511 buf = kmalloc(DS4_FEATURE_REPORT_0x05_SIZE, GFP_KERNEL);
1512 if (!buf)
1513 return -ENOMEM;
9b2b5c9a 1514
55a07d62
RC
1515 for (retries = 0; retries < 3; retries++) {
1516 ret = hid_hw_raw_request(sc->hdev, 0x05, buf,
1517 DS4_FEATURE_REPORT_0x05_SIZE,
1518 HID_FEATURE_REPORT,
1519 HID_REQ_GET_REPORT);
1520 if (ret < 0)
1521 goto err_stop;
9b2b5c9a 1522
55a07d62
RC
1523 /* CRC check */
1524 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1525 crc = ~crc32_le(crc, buf, DS4_FEATURE_REPORT_0x05_SIZE-4);
1526 report_crc = get_unaligned_le32(&buf[DS4_FEATURE_REPORT_0x05_SIZE-4]);
1527 if (crc != report_crc) {
1528 hid_warn(sc->hdev, "DualShock 4 calibration report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1529 report_crc, crc);
1530 if (retries < 2) {
1531 hid_warn(sc->hdev, "Retrying DualShock 4 get calibration report request\n");
1532 continue;
1533 } else {
1534 ret = -EILSEQ;
1535 goto err_stop;
1536 }
1537 } else {
1538 break;
1539 }
1540 }
1541 }
9b2b5c9a 1542
55a07d62
RC
1543 gyro_pitch_bias = get_unaligned_le16(&buf[1]);
1544 gyro_yaw_bias = get_unaligned_le16(&buf[3]);
1545 gyro_roll_bias = get_unaligned_le16(&buf[5]);
1546 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1547 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
1548 gyro_pitch_minus = get_unaligned_le16(&buf[9]);
1549 gyro_yaw_plus = get_unaligned_le16(&buf[11]);
1550 gyro_yaw_minus = get_unaligned_le16(&buf[13]);
1551 gyro_roll_plus = get_unaligned_le16(&buf[15]);
1552 gyro_roll_minus = get_unaligned_le16(&buf[17]);
1553 } else {
35f436c3 1554 /* BT + Dongle */
55a07d62
RC
1555 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
1556 gyro_yaw_plus = get_unaligned_le16(&buf[9]);
1557 gyro_roll_plus = get_unaligned_le16(&buf[11]);
1558 gyro_pitch_minus = get_unaligned_le16(&buf[13]);
1559 gyro_yaw_minus = get_unaligned_le16(&buf[15]);
1560 gyro_roll_minus = get_unaligned_le16(&buf[17]);
1561 }
1562 gyro_speed_plus = get_unaligned_le16(&buf[19]);
1563 gyro_speed_minus = get_unaligned_le16(&buf[21]);
1564 acc_x_plus = get_unaligned_le16(&buf[23]);
1565 acc_x_minus = get_unaligned_le16(&buf[25]);
1566 acc_y_plus = get_unaligned_le16(&buf[27]);
1567 acc_y_minus = get_unaligned_le16(&buf[29]);
1568 acc_z_plus = get_unaligned_le16(&buf[31]);
1569 acc_z_minus = get_unaligned_le16(&buf[33]);
1570
1571 /* Set gyroscope calibration and normalization parameters.
1572 * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
1573 */
1574 speed_2x = (gyro_speed_plus + gyro_speed_minus);
1575 sc->ds4_calib_data[0].abs_code = ABS_RX;
1576 sc->ds4_calib_data[0].bias = gyro_pitch_bias;
1577 sc->ds4_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1578 sc->ds4_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
1579
1580 sc->ds4_calib_data[1].abs_code = ABS_RY;
1581 sc->ds4_calib_data[1].bias = gyro_yaw_bias;
1582 sc->ds4_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1583 sc->ds4_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
1584
1585 sc->ds4_calib_data[2].abs_code = ABS_RZ;
1586 sc->ds4_calib_data[2].bias = gyro_roll_bias;
1587 sc->ds4_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1588 sc->ds4_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
1589
1590 /* Set accelerometer calibration and normalization parameters.
1591 * Data values will be normalized to 1/DS4_ACC_RES_PER_G G.
1592 */
1593 range_2g = acc_x_plus - acc_x_minus;
1594 sc->ds4_calib_data[3].abs_code = ABS_X;
1595 sc->ds4_calib_data[3].bias = acc_x_plus - range_2g / 2;
1596 sc->ds4_calib_data[3].sens_numer = 2*DS4_ACC_RES_PER_G;
1597 sc->ds4_calib_data[3].sens_denom = range_2g;
1598
1599 range_2g = acc_y_plus - acc_y_minus;
1600 sc->ds4_calib_data[4].abs_code = ABS_Y;
1601 sc->ds4_calib_data[4].bias = acc_y_plus - range_2g / 2;
1602 sc->ds4_calib_data[4].sens_numer = 2*DS4_ACC_RES_PER_G;
1603 sc->ds4_calib_data[4].sens_denom = range_2g;
1604
1605 range_2g = acc_z_plus - acc_z_minus;
1606 sc->ds4_calib_data[5].abs_code = ABS_Z;
1607 sc->ds4_calib_data[5].bias = acc_z_plus - range_2g / 2;
1608 sc->ds4_calib_data[5].sens_numer = 2*DS4_ACC_RES_PER_G;
1609 sc->ds4_calib_data[5].sens_denom = range_2g;
1610
1611err_stop:
1612 kfree(buf);
9b2b5c9a 1613 return ret;
f9ce7c28
BN
1614}
1615
f2f47c38
RC
1616static void dualshock4_calibration_work(struct work_struct *work)
1617{
1618 struct sony_sc *sc = container_of(work, struct sony_sc, hotplug_worker);
1619 unsigned long flags;
1620 enum ds4_dongle_state dongle_state;
1621 int ret;
1622
1623 ret = dualshock4_get_calibration_data(sc);
1624 if (ret < 0) {
1625 /* This call is very unlikely to fail for the dongle. When it
1626 * fails we are probably in a very bad state, so mark the
1627 * dongle as disabled. We will re-enable the dongle if a new
1628 * DS4 hotplug is detect from sony_raw_event as any issues
1629 * are likely resolved then (the dongle is quite stupid).
1630 */
1631 hid_err(sc->hdev, "DualShock 4 USB dongle: calibration failed, disabling device\n");
1632 dongle_state = DONGLE_DISABLED;
1633 } else {
1634 hid_info(sc->hdev, "DualShock 4 USB dongle: calibration completed\n");
1635 dongle_state = DONGLE_CONNECTED;
1636 }
1637
1638 spin_lock_irqsave(&sc->lock, flags);
1639 sc->ds4_dongle_state = dongle_state;
1640 spin_unlock_irqrestore(&sc->lock, flags);
1641}
1642
221399b3 1643static void sixaxis_set_leds_from_id(struct sony_sc *sc)
8025087a 1644{
1adf904e 1645 static const u8 sixaxis_leds[10][4] = {
8025087a
FP
1646 { 0x01, 0x00, 0x00, 0x00 },
1647 { 0x00, 0x01, 0x00, 0x00 },
1648 { 0x00, 0x00, 0x01, 0x00 },
1649 { 0x00, 0x00, 0x00, 0x01 },
1650 { 0x01, 0x00, 0x00, 0x01 },
1651 { 0x00, 0x01, 0x00, 0x01 },
1652 { 0x00, 0x00, 0x01, 0x01 },
1653 { 0x01, 0x00, 0x01, 0x01 },
1654 { 0x00, 0x01, 0x01, 0x01 },
1655 { 0x01, 0x01, 0x01, 0x01 }
1656 };
1657
221399b3
FP
1658 int id = sc->device_id;
1659
1660 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
8025087a
FP
1661
1662 if (id < 0)
1663 return;
1664
1665 id %= 10;
221399b3 1666 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
8025087a
FP
1667}
1668
221399b3 1669static void dualshock4_set_leds_from_id(struct sony_sc *sc)
8025087a
FP
1670{
1671 /* The first 4 color/index entries match what the PS4 assigns */
1adf904e 1672 static const u8 color_code[7][3] = {
39254a13
RC
1673 /* Blue */ { 0x00, 0x00, 0x40 },
1674 /* Red */ { 0x40, 0x00, 0x00 },
1675 /* Green */ { 0x00, 0x40, 0x00 },
1676 /* Pink */ { 0x20, 0x00, 0x20 },
8025087a
FP
1677 /* Orange */ { 0x02, 0x01, 0x00 },
1678 /* Teal */ { 0x00, 0x01, 0x01 },
1679 /* White */ { 0x01, 0x01, 0x01 }
1680 };
1681
221399b3
FP
1682 int id = sc->device_id;
1683
1684 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
8025087a
FP
1685
1686 if (id < 0)
1687 return;
1688
1689 id %= 7;
221399b3 1690 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
8025087a
FP
1691}
1692
221399b3 1693static void buzz_set_leds(struct sony_sc *sc)
f04d5140 1694{
221399b3 1695 struct hid_device *hdev = sc->hdev;
f04d5140
CL
1696 struct list_head *report_list =
1697 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1698 struct hid_report *report = list_entry(report_list->next,
1699 struct hid_report, list);
1adf904e 1700 s32 *value = report->field[0]->value;
f04d5140 1701
221399b3
FP
1702 BUILD_BUG_ON(MAX_LEDS < 4);
1703
f04d5140 1704 value[0] = 0x00;
221399b3
FP
1705 value[1] = sc->led_state[0] ? 0xff : 0x00;
1706 value[2] = sc->led_state[1] ? 0xff : 0x00;
1707 value[3] = sc->led_state[2] ? 0xff : 0x00;
1708 value[4] = sc->led_state[3] ? 0xff : 0x00;
f04d5140
CL
1709 value[5] = 0x00;
1710 value[6] = 0x00;
1711 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1712}
1713
221399b3 1714static void sony_set_leds(struct sony_sc *sc)
0a286ef2 1715{
221399b3 1716 if (!(sc->quirks & BUZZ_CONTROLLER))
b5322736 1717 sony_schedule_work(sc, SONY_WORKER_STATE);
221399b3
FP
1718 else
1719 buzz_set_leds(sc);
0a286ef2
SE
1720}
1721
c5382519 1722static void sony_led_set_brightness(struct led_classdev *led,
f04d5140
CL
1723 enum led_brightness value)
1724{
1725 struct device *dev = led->dev->parent;
ee79a8f8 1726 struct hid_device *hdev = to_hid_device(dev);
f04d5140 1727 struct sony_sc *drv_data;
f04d5140
CL
1728
1729 int n;
b3ed458c 1730 int force_update;
f04d5140
CL
1731
1732 drv_data = hid_get_drvdata(hdev);
2251b85f 1733 if (!drv_data) {
f04d5140
CL
1734 hid_err(hdev, "No device data\n");
1735 return;
1736 }
f04d5140 1737
b3ed458c
FP
1738 /*
1739 * The Sixaxis on USB will override any LED settings sent to it
1740 * and keep flashing all of the LEDs until the PS button is pressed.
1741 * Updates, even if redundant, must be always be sent to the
1742 * controller to avoid having to toggle the state of an LED just to
1743 * stop the flashing later on.
1744 */
1745 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1746
60781cf4 1747 for (n = 0; n < drv_data->led_count; n++) {
b3ed458c
FP
1748 if (led == drv_data->leds[n] && (force_update ||
1749 (value != drv_data->led_state[n] ||
1750 drv_data->led_delay_on[n] ||
1751 drv_data->led_delay_off[n]))) {
1752
1753 drv_data->led_state[n] = value;
1754
1755 /* Setting the brightness stops the blinking */
1756 drv_data->led_delay_on[n] = 0;
1757 drv_data->led_delay_off[n] = 0;
1758
221399b3 1759 sony_set_leds(drv_data);
f04d5140
CL
1760 break;
1761 }
1762 }
1763}
1764
c5382519 1765static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
f04d5140
CL
1766{
1767 struct device *dev = led->dev->parent;
ee79a8f8 1768 struct hid_device *hdev = to_hid_device(dev);
f04d5140 1769 struct sony_sc *drv_data;
f04d5140
CL
1770
1771 int n;
f04d5140
CL
1772
1773 drv_data = hid_get_drvdata(hdev);
2251b85f 1774 if (!drv_data) {
f04d5140
CL
1775 hid_err(hdev, "No device data\n");
1776 return LED_OFF;
1777 }
f04d5140 1778
60781cf4 1779 for (n = 0; n < drv_data->led_count; n++) {
7db7504a
SW
1780 if (led == drv_data->leds[n])
1781 return drv_data->led_state[n];
f04d5140
CL
1782 }
1783
7db7504a 1784 return LED_OFF;
f04d5140 1785}
f04d5140 1786
b3ed458c
FP
1787static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1788 unsigned long *delay_off)
1789{
1790 struct device *dev = led->dev->parent;
ee79a8f8 1791 struct hid_device *hdev = to_hid_device(dev);
b3ed458c
FP
1792 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1793 int n;
1adf904e 1794 u8 new_on, new_off;
b3ed458c
FP
1795
1796 if (!drv_data) {
1797 hid_err(hdev, "No device data\n");
1798 return -EINVAL;
1799 }
1800
1801 /* Max delay is 255 deciseconds or 2550 milliseconds */
1802 if (*delay_on > 2550)
1803 *delay_on = 2550;
1804 if (*delay_off > 2550)
1805 *delay_off = 2550;
1806
1807 /* Blink at 1 Hz if both values are zero */
1808 if (!*delay_on && !*delay_off)
1809 *delay_on = *delay_off = 500;
1810
1811 new_on = *delay_on / 10;
1812 new_off = *delay_off / 10;
1813
1814 for (n = 0; n < drv_data->led_count; n++) {
1815 if (led == drv_data->leds[n])
1816 break;
1817 }
1818
1819 /* This LED is not registered on this device */
1820 if (n >= drv_data->led_count)
1821 return -EINVAL;
1822
1823 /* Don't schedule work if the values didn't change */
1824 if (new_on != drv_data->led_delay_on[n] ||
1825 new_off != drv_data->led_delay_off[n]) {
1826 drv_data->led_delay_on[n] = new_on;
1827 drv_data->led_delay_off[n] = new_off;
b5322736 1828 sony_schedule_work(drv_data, SONY_WORKER_STATE);
b3ed458c
FP
1829 }
1830
1831 return 0;
1832}
1833
fa57a810 1834static void sony_leds_remove(struct sony_sc *sc)
0a286ef2 1835{
0a286ef2
SE
1836 struct led_classdev *led;
1837 int n;
1838
fa57a810 1839 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
0a286ef2 1840
fa57a810
FP
1841 for (n = 0; n < sc->led_count; n++) {
1842 led = sc->leds[n];
1843 sc->leds[n] = NULL;
0a286ef2
SE
1844 if (!led)
1845 continue;
1846 led_classdev_unregister(led);
1847 kfree(led);
1848 }
60781cf4 1849
fa57a810 1850 sc->led_count = 0;
0a286ef2
SE
1851}
1852
fa57a810 1853static int sony_leds_init(struct sony_sc *sc)
f04d5140 1854{
fa57a810 1855 struct hid_device *hdev = sc->hdev;
40e32ee6 1856 int n, ret = 0;
b3ed458c 1857 int use_ds4_names;
40e32ee6
JK
1858 struct led_classdev *led;
1859 size_t name_sz;
1860 char *name;
0a286ef2
SE
1861 size_t name_len;
1862 const char *name_fmt;
b3ed458c
FP
1863 static const char * const ds4_name_str[] = { "red", "green", "blue",
1864 "global" };
1adf904e
PM
1865 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1866 u8 use_hw_blink[MAX_LEDS] = { 0 };
f04d5140 1867
fa57a810 1868 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
0a286ef2 1869
fa57a810
FP
1870 if (sc->quirks & BUZZ_CONTROLLER) {
1871 sc->led_count = 4;
b3ed458c 1872 use_ds4_names = 0;
0a286ef2
SE
1873 name_len = strlen("::buzz#");
1874 name_fmt = "%s::buzz%d";
1875 /* Validate expected report characteristics. */
1876 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1877 return -ENODEV;
fa57a810 1878 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
221399b3
FP
1879 dualshock4_set_leds_from_id(sc);
1880 sc->led_state[3] = 1;
b3ed458c
FP
1881 sc->led_count = 4;
1882 memset(max_brightness, 255, 3);
1883 use_hw_blink[3] = 1;
1884 use_ds4_names = 1;
61ebca93
FP
1885 name_len = 0;
1886 name_fmt = "%s:%s";
c5e0c1c4
FP
1887 } else if (sc->quirks & MOTION_CONTROLLER) {
1888 sc->led_count = 3;
1889 memset(max_brightness, 255, 3);
1890 use_ds4_names = 1;
1891 name_len = 0;
1892 name_fmt = "%s:%s";
4545ee0a 1893 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
1adf904e 1894 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
4545ee0a
SW
1895
1896 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1897 sc->led_count = 1;
1898 memset(use_hw_blink, 1, 4);
1899 use_ds4_names = 0;
1900 name_len = strlen("::sony#");
1901 name_fmt = "%s::sony%d";
60781cf4 1902 } else {
221399b3 1903 sixaxis_set_leds_from_id(sc);
fa57a810 1904 sc->led_count = 4;
b3ed458c
FP
1905 memset(use_hw_blink, 1, 4);
1906 use_ds4_names = 0;
61ebca93
FP
1907 name_len = strlen("::sony#");
1908 name_fmt = "%s::sony%d";
60781cf4
FP
1909 }
1910
ad142b9e
FP
1911 /*
1912 * Clear LEDs as we have no way of reading their initial state. This is
f04d5140 1913 * only relevant if the driver is loaded after somebody actively set the
ad142b9e
FP
1914 * LEDs to on
1915 */
221399b3 1916 sony_set_leds(sc);
f04d5140 1917
0a286ef2 1918 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
f04d5140 1919
fa57a810 1920 for (n = 0; n < sc->led_count; n++) {
61ebca93 1921
b3ed458c
FP
1922 if (use_ds4_names)
1923 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
61ebca93 1924
40e32ee6
JK
1925 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1926 if (!led) {
1927 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
8cd5fcda 1928 ret = -ENOMEM;
40e32ee6
JK
1929 goto error_leds;
1930 }
f04d5140 1931
40e32ee6 1932 name = (void *)(&led[1]);
b3ed458c
FP
1933 if (use_ds4_names)
1934 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1935 ds4_name_str[n]);
61ebca93
FP
1936 else
1937 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
40e32ee6 1938 led->name = name;
221399b3 1939 led->brightness = sc->led_state[n];
b3ed458c 1940 led->max_brightness = max_brightness[n];
765a1077 1941 led->flags = LED_CORE_SUSPENDRESUME;
c5382519
SE
1942 led->brightness_get = sony_led_get_brightness;
1943 led->brightness_set = sony_led_set_brightness;
f04d5140 1944
b3ed458c
FP
1945 if (use_hw_blink[n])
1946 led->blink_set = sony_led_blink_set;
1947
8025087a
FP
1948 sc->leds[n] = led;
1949
8cd5fcda
JL
1950 ret = led_classdev_register(&hdev->dev, led);
1951 if (ret) {
40e32ee6 1952 hid_err(hdev, "Failed to register LED %d\n", n);
8025087a 1953 sc->leds[n] = NULL;
40e32ee6
JK
1954 kfree(led);
1955 goto error_leds;
f04d5140
CL
1956 }
1957 }
f04d5140
CL
1958
1959 return ret;
1960
f04d5140 1961error_leds:
fa57a810 1962 sony_leds_remove(sc);
f04d5140 1963
f04d5140 1964 return ret;
f04d5140
CL
1965}
1966
d8aaccda 1967static void sixaxis_send_output_report(struct sony_sc *sc)
a08c22c0 1968{
9b2b5c9a 1969 static const union sixaxis_output_report_01 default_report = {
55d3b664
FP
1970 .buf = {
1971 0x01,
ad07b7a6 1972 0x01, 0xff, 0x00, 0xff, 0x00,
55d3b664
FP
1973 0x00, 0x00, 0x00, 0x00, 0x00,
1974 0xff, 0x27, 0x10, 0x00, 0x32,
1975 0xff, 0x27, 0x10, 0x00, 0x32,
1976 0xff, 0x27, 0x10, 0x00, 0x32,
1977 0xff, 0x27, 0x10, 0x00, 0x32,
1978 0x00, 0x00, 0x00, 0x00, 0x00
1979 }
a08c22c0 1980 };
9b2b5c9a
FP
1981 struct sixaxis_output_report *report =
1982 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1983 int n;
1984
1985 /* Initialize the report with default values */
1986 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
9f323b68 1987
0a286ef2 1988#ifdef CONFIG_SONY_FF
9b2b5c9a
FP
1989 report->rumble.right_motor_on = sc->right ? 1 : 0;
1990 report->rumble.left_motor_force = sc->left;
0a286ef2
SE
1991#endif
1992
9b2b5c9a
FP
1993 report->leds_bitmap |= sc->led_state[0] << 1;
1994 report->leds_bitmap |= sc->led_state[1] << 2;
1995 report->leds_bitmap |= sc->led_state[2] << 3;
1996 report->leds_bitmap |= sc->led_state[3] << 4;
9f323b68 1997
88f6576f 1998 /* Set flag for all leds off, required for 3rd party INTEC controller */
9b2b5c9a
FP
1999 if ((report->leds_bitmap & 0x1E) == 0)
2000 report->leds_bitmap |= 0x20;
88f6576f 2001
b3ed458c
FP
2002 /*
2003 * The LEDs in the report are indexed in reverse order to their
2004 * corresponding light on the controller.
2005 * Index 0 = LED 4, index 1 = LED 3, etc...
2006 *
2007 * In the case of both delay values being zero (blinking disabled) the
2008 * default report values should be used or the controller LED will be
2009 * always off.
2010 */
2011 for (n = 0; n < 4; n++) {
2012 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
9b2b5c9a
FP
2013 report->led[3 - n].duty_off = sc->led_delay_off[n];
2014 report->led[3 - n].duty_on = sc->led_delay_on[n];
b3ed458c
FP
2015 }
2016 }
2017
1adf904e 2018 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
9b2b5c9a
FP
2019 sizeof(struct sixaxis_output_report),
2020 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
9f323b68
SE
2021}
2022
d8aaccda 2023static void dualshock4_send_output_report(struct sony_sc *sc)
0bd88dd3 2024{
0da8ea65 2025 struct hid_device *hdev = sc->hdev;
1adf904e 2026 u8 *buf = sc->output_report_dmabuf;
48220237
FP
2027 int offset;
2028
c4425c8f 2029 /*
77b499e7
RC
2030 * NOTE: The lower 6 bits of buf[1] field of the Bluetooth report
2031 * control the interval at which Dualshock 4 reports data:
2032 * 0x00 - 1ms
2033 * 0x01 - 1ms
2034 * 0x02 - 2ms
2035 * 0x3E - 62ms
2036 * 0x3F - disabled
c4425c8f 2037 */
35f436c3 2038 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
2c159de0 2039 memset(buf, 0, DS4_OUTPUT_REPORT_0x05_SIZE);
fdcf105d 2040 buf[0] = 0x05;
5caceb06 2041 buf[1] = 0x07; /* blink + LEDs + motor */
fdcf105d
FP
2042 offset = 4;
2043 } else {
2c159de0 2044 memset(buf, 0, DS4_OUTPUT_REPORT_0x11_SIZE);
fdcf105d 2045 buf[0] = 0x11;
77b499e7 2046 buf[1] = 0xC0 /* HID + CRC */ | sc->ds4_bt_poll_interval;
5caceb06 2047 buf[3] = 0x07; /* blink + LEDs + motor */
fdcf105d
FP
2048 offset = 6;
2049 }
0bd88dd3
FP
2050
2051#ifdef CONFIG_SONY_FF
48220237
FP
2052 buf[offset++] = sc->right;
2053 buf[offset++] = sc->left;
2054#else
2055 offset += 2;
0bd88dd3
FP
2056#endif
2057
b3ed458c
FP
2058 /* LED 3 is the global control */
2059 if (sc->led_state[3]) {
2060 buf[offset++] = sc->led_state[0];
2061 buf[offset++] = sc->led_state[1];
2062 buf[offset++] = sc->led_state[2];
2063 } else {
2064 offset += 3;
2065 }
2066
2067 /* If both delay values are zero the DualShock 4 disables blinking. */
2068 buf[offset++] = sc->led_delay_on[3];
2069 buf[offset++] = sc->led_delay_off[3];
60781cf4 2070
35f436c3 2071 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
2c159de0 2072 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x05_SIZE);
e7ef53ad
RC
2073 else {
2074 /* CRC generation */
2075 u8 bthdr = 0xA2;
2076 u32 crc;
2077
2078 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
2079 crc = ~crc32_le(crc, buf, DS4_OUTPUT_REPORT_0x11_SIZE-4);
2080 put_unaligned_le32(crc, &buf[74]);
2081 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x11_SIZE);
2082 }
0bd88dd3
FP
2083}
2084
d8aaccda 2085static void motion_send_output_report(struct sony_sc *sc)
c5e0c1c4 2086{
c5e0c1c4
FP
2087 struct hid_device *hdev = sc->hdev;
2088 struct motion_output_report_02 *report =
2089 (struct motion_output_report_02 *)sc->output_report_dmabuf;
2090
41d2d425 2091 memset(report, 0, MOTION_REPORT_0x02_SIZE);
c5e0c1c4
FP
2092
2093 report->type = 0x02; /* set leds */
2094 report->r = sc->led_state[0];
2095 report->g = sc->led_state[1];
2096 report->b = sc->led_state[2];
2097
2098#ifdef CONFIG_SONY_FF
2099 report->rumble = max(sc->right, sc->left);
2100#endif
2101
1adf904e 2102 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
c5e0c1c4
FP
2103}
2104
decd946c
FP
2105static inline void sony_send_output_report(struct sony_sc *sc)
2106{
2107 if (sc->send_output_report)
2108 sc->send_output_report(sc);
2109}
2110
d8aaccda
FP
2111static void sony_state_worker(struct work_struct *work)
2112{
2113 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
ef916ef5 2114
d8aaccda
FP
2115 sc->send_output_report(sc);
2116}
2117
9b2b5c9a
FP
2118static int sony_allocate_output_report(struct sony_sc *sc)
2119{
4545ee0a
SW
2120 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2121 (sc->quirks & NAVIGATION_CONTROLLER))
9b2b5c9a
FP
2122 sc->output_report_dmabuf =
2123 kmalloc(sizeof(union sixaxis_output_report_01),
2124 GFP_KERNEL);
2125 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
2c159de0 2126 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE,
9b2b5c9a 2127 GFP_KERNEL);
35f436c3 2128 else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
2c159de0 2129 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE,
9b2b5c9a 2130 GFP_KERNEL);
c5e0c1c4 2131 else if (sc->quirks & MOTION_CONTROLLER)
41d2d425
SW
2132 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
2133 GFP_KERNEL);
9b2b5c9a
FP
2134 else
2135 return 0;
2136
2137 if (!sc->output_report_dmabuf)
2138 return -ENOMEM;
2139
2140 return 0;
2141}
2142
0a286ef2 2143#ifdef CONFIG_SONY_FF
9f323b68
SE
2144static int sony_play_effect(struct input_dev *dev, void *data,
2145 struct ff_effect *effect)
2146{
a08c22c0 2147 struct hid_device *hid = input_get_drvdata(dev);
9f323b68 2148 struct sony_sc *sc = hid_get_drvdata(hid);
a08c22c0
SE
2149
2150 if (effect->type != FF_RUMBLE)
2151 return 0;
2152
9f323b68 2153 sc->left = effect->u.rumble.strong_magnitude / 256;
0bd88dd3 2154 sc->right = effect->u.rumble.weak_magnitude / 256;
a08c22c0 2155
b5322736 2156 sony_schedule_work(sc, SONY_WORKER_STATE);
9f323b68 2157 return 0;
a08c22c0
SE
2158}
2159
fa57a810 2160static int sony_init_ff(struct sony_sc *sc)
a08c22c0 2161{
fa57a810 2162 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
a08c22c0
SE
2163 struct hid_input, list);
2164 struct input_dev *input_dev = hidinput->input;
2165
2166 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
2167 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
2168}
2169
2170#else
fa57a810 2171static int sony_init_ff(struct sony_sc *sc)
a08c22c0
SE
2172{
2173 return 0;
2174}
9f323b68 2175
a08c22c0
SE
2176#endif
2177
d902f472
FP
2178static int sony_battery_get_property(struct power_supply *psy,
2179 enum power_supply_property psp,
2180 union power_supply_propval *val)
2181{
297d716f 2182 struct sony_sc *sc = power_supply_get_drvdata(psy);
d902f472
FP
2183 unsigned long flags;
2184 int ret = 0;
2185 u8 battery_charging, battery_capacity, cable_state;
2186
2187 spin_lock_irqsave(&sc->lock, flags);
2188 battery_charging = sc->battery_charging;
2189 battery_capacity = sc->battery_capacity;
2190 cable_state = sc->cable_state;
2191 spin_unlock_irqrestore(&sc->lock, flags);
2192
2193 switch (psp) {
2194 case POWER_SUPPLY_PROP_PRESENT:
2195 val->intval = 1;
2196 break;
2197 case POWER_SUPPLY_PROP_SCOPE:
2198 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2199 break;
2200 case POWER_SUPPLY_PROP_CAPACITY:
2201 val->intval = battery_capacity;
2202 break;
2203 case POWER_SUPPLY_PROP_STATUS:
2204 if (battery_charging)
2205 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2206 else
2207 if (battery_capacity == 100 && cable_state)
2208 val->intval = POWER_SUPPLY_STATUS_FULL;
2209 else
2210 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2211 break;
2212 default:
2213 ret = -EINVAL;
2214 break;
2215 }
2216 return ret;
9f323b68
SE
2217}
2218
0f398230 2219static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
c4e1ddf2 2220{
0f398230
FP
2221 const char *battery_str_fmt = append_dev_id ?
2222 "sony_controller_battery_%pMR_%i" :
2223 "sony_controller_battery_%pMR";
297d716f 2224 struct power_supply_config psy_cfg = { .drv_data = sc, };
c4e1ddf2 2225 struct hid_device *hdev = sc->hdev;
d902f472 2226 int ret;
c4e1ddf2 2227
ad142b9e
FP
2228 /*
2229 * Set the default battery level to 100% to avoid low battery warnings
d9a293a9
FP
2230 * if the battery is polled before the first device report is received.
2231 */
2232 sc->battery_capacity = 100;
2233
297d716f
KK
2234 sc->battery_desc.properties = sony_battery_props;
2235 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2236 sc->battery_desc.get_property = sony_battery_get_property;
2237 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2238 sc->battery_desc.use_for_apm = 0;
0f398230
FP
2239 sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt,
2240 sc->mac_address, sc->device_id);
297d716f 2241 if (!sc->battery_desc.name)
d902f472 2242 return -ENOMEM;
c4e1ddf2 2243
297d716f
KK
2244 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2245 &psy_cfg);
2246 if (IS_ERR(sc->battery)) {
2247 ret = PTR_ERR(sc->battery);
d902f472
FP
2248 hid_err(hdev, "Unable to register battery device\n");
2249 goto err_free;
2250 }
c4e1ddf2 2251
297d716f 2252 power_supply_powers(sc->battery, &hdev->dev);
a08c22c0 2253 return 0;
d902f472
FP
2254
2255err_free:
297d716f
KK
2256 kfree(sc->battery_desc.name);
2257 sc->battery_desc.name = NULL;
d902f472 2258 return ret;
a08c22c0 2259}
9f323b68 2260
d902f472 2261static void sony_battery_remove(struct sony_sc *sc)
9f323b68 2262{
297d716f 2263 if (!sc->battery_desc.name)
d902f472
FP
2264 return;
2265
297d716f
KK
2266 power_supply_unregister(sc->battery);
2267 kfree(sc->battery_desc.name);
2268 sc->battery_desc.name = NULL;
9f323b68 2269}
a08c22c0 2270
d2d782fc
FP
2271/*
2272 * If a controller is plugged in via USB while already connected via Bluetooth
2273 * it will show up as two devices. A global list of connected controllers and
2274 * their MAC addresses is maintained to ensure that a device is only connected
2275 * once.
0f398230
FP
2276 *
2277 * Some USB-only devices masquerade as Sixaxis controllers and all have the
2278 * same dummy Bluetooth address, so a comparison of the connection type is
2279 * required. Devices are only rejected in the case where two devices have
2280 * matching Bluetooth addresses on different bus types.
d2d782fc 2281 */
0f398230
FP
2282static inline int sony_compare_connection_type(struct sony_sc *sc0,
2283 struct sony_sc *sc1)
2284{
2285 const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
2286 const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
2287
2288 return sc0_not_bt == sc1_not_bt;
2289}
2290
d2d782fc
FP
2291static int sony_check_add_dev_list(struct sony_sc *sc)
2292{
2293 struct sony_sc *entry;
2294 unsigned long flags;
2295 int ret;
2296
2297 spin_lock_irqsave(&sony_dev_list_lock, flags);
2298
2299 list_for_each_entry(entry, &sony_device_list, list_node) {
2300 ret = memcmp(sc->mac_address, entry->mac_address,
2301 sizeof(sc->mac_address));
2302 if (!ret) {
0f398230
FP
2303 if (sony_compare_connection_type(sc, entry)) {
2304 ret = 1;
2305 } else {
2306 ret = -EEXIST;
2307 hid_info(sc->hdev,
2308 "controller with MAC address %pMR already connected\n",
d2d782fc 2309 sc->mac_address);
0f398230 2310 }
d2d782fc 2311 goto unlock;
c4e1ddf2
FP
2312 }
2313 }
2314
d2d782fc
FP
2315 ret = 0;
2316 list_add(&(sc->list_node), &sony_device_list);
c4e1ddf2 2317
d2d782fc
FP
2318unlock:
2319 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2320 return ret;
2321}
2322
2323static void sony_remove_dev_list(struct sony_sc *sc)
2324{
2325 unsigned long flags;
c4e1ddf2 2326
d2d782fc
FP
2327 if (sc->list_node.next) {
2328 spin_lock_irqsave(&sony_dev_list_lock, flags);
2329 list_del(&(sc->list_node));
2330 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2331 }
c4e1ddf2
FP
2332}
2333
d2d782fc
FP
2334static int sony_get_bt_devaddr(struct sony_sc *sc)
2335{
2336 int ret;
2337
2338 /* HIDP stores the device MAC address as a string in the uniq field. */
2339 ret = strlen(sc->hdev->uniq);
2340 if (ret != 17)
2341 return -EINVAL;
2342
2343 ret = sscanf(sc->hdev->uniq,
2344 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2345 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2346 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2347
2348 if (ret != 6)
2349 return -EINVAL;
2350
2351 return 0;
2352}
2353
2354static int sony_check_add(struct sony_sc *sc)
2355{
1adf904e 2356 u8 *buf = NULL;
d2d782fc
FP
2357 int n, ret;
2358
2359 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
12e9a6d7 2360 (sc->quirks & MOTION_CONTROLLER_BT) ||
4545ee0a 2361 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
d2d782fc
FP
2362 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2363 /*
2364 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2365 * address from the uniq string where HIDP stores it.
2366 * As uniq cannot be guaranteed to be a MAC address in all cases
2367 * a failure of this function should not prevent the connection.
2368 */
2369 if (sony_get_bt_devaddr(sc) < 0) {
2370 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2371 return 0;
2372 }
35f436c3 2373 } else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
2c159de0 2374 buf = kmalloc(DS4_FEATURE_REPORT_0x81_SIZE, GFP_KERNEL);
9b2b5c9a
FP
2375 if (!buf)
2376 return -ENOMEM;
d2d782fc
FP
2377
2378 /*
2379 * The MAC address of a DS4 controller connected via USB can be
2380 * retrieved with feature report 0x81. The address begins at
2381 * offset 1.
2382 */
9b2b5c9a 2383 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2c159de0 2384 DS4_FEATURE_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
9b2b5c9a 2385 HID_REQ_GET_REPORT);
d2d782fc 2386
2c159de0 2387 if (ret != DS4_FEATURE_REPORT_0x81_SIZE) {
d2d782fc 2388 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
9b2b5c9a
FP
2389 ret = ret < 0 ? ret : -EINVAL;
2390 goto out_free;
d2d782fc
FP
2391 }
2392
2393 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
c70d5f70
RC
2394
2395 snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
2396 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2397 sc->mac_address[5], sc->mac_address[4],
2398 sc->mac_address[3], sc->mac_address[2],
2399 sc->mac_address[1], sc->mac_address[0]);
4545ee0a
SW
2400 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2401 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
9b2b5c9a
FP
2402 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2403 if (!buf)
2404 return -ENOMEM;
d2d782fc
FP
2405
2406 /*
2407 * The MAC address of a Sixaxis controller connected via USB can
2408 * be retrieved with feature report 0xf2. The address begins at
2409 * offset 4.
2410 */
9b2b5c9a
FP
2411 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2412 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2413 HID_REQ_GET_REPORT);
d2d782fc 2414
9b2b5c9a 2415 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
d2d782fc 2416 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
9b2b5c9a
FP
2417 ret = ret < 0 ? ret : -EINVAL;
2418 goto out_free;
d2d782fc
FP
2419 }
2420
2421 /*
2422 * The Sixaxis device MAC in the report is big-endian and must
2423 * be byte-swapped.
2424 */
2425 for (n = 0; n < 6; n++)
2426 sc->mac_address[5-n] = buf[4+n];
5a144be3
RC
2427
2428 snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
2429 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2430 sc->mac_address[5], sc->mac_address[4],
2431 sc->mac_address[3], sc->mac_address[2],
2432 sc->mac_address[1], sc->mac_address[0]);
d2d782fc
FP
2433 } else {
2434 return 0;
2435 }
2436
9b2b5c9a
FP
2437 ret = sony_check_add_dev_list(sc);
2438
2439out_free:
2440
2441 kfree(buf);
2442
2443 return ret;
d2d782fc
FP
2444}
2445
8025087a
FP
2446static int sony_set_device_id(struct sony_sc *sc)
2447{
2448 int ret;
2449
2450 /*
2451 * Only DualShock 4 or Sixaxis controllers get an id.
2452 * All others are set to -1.
2453 */
2454 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2455 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2456 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2457 GFP_KERNEL);
2458 if (ret < 0) {
2459 sc->device_id = -1;
2460 return ret;
2461 }
2462 sc->device_id = ret;
2463 } else {
2464 sc->device_id = -1;
2465 }
2466
2467 return 0;
2468}
2469
2470static void sony_release_device_id(struct sony_sc *sc)
2471{
2472 if (sc->device_id >= 0) {
2473 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2474 sc->device_id = -1;
2475 }
2476}
2477
d8aaccda 2478static inline void sony_init_output_report(struct sony_sc *sc,
09593e38 2479 void (*send_output_report)(struct sony_sc *))
46262047 2480{
d8aaccda
FP
2481 sc->send_output_report = send_output_report;
2482
b5322736 2483 if (!sc->state_worker_initialized)
d8aaccda 2484 INIT_WORK(&sc->state_worker, sony_state_worker);
46262047 2485
b5322736 2486 sc->state_worker_initialized = 1;
46262047
FP
2487}
2488
2489static inline void sony_cancel_work_sync(struct sony_sc *sc)
2490{
f2f47c38
RC
2491 if (sc->hotplug_worker_initialized)
2492 cancel_work_sync(&sc->hotplug_worker);
b5322736 2493 if (sc->state_worker_initialized)
46262047
FP
2494 cancel_work_sync(&sc->state_worker);
2495}
d2d782fc 2496
77b499e7 2497
e1bc84d0
RC
2498static int sony_input_configured(struct hid_device *hdev,
2499 struct hid_input *hidinput)
bd28ce00 2500{
e1bc84d0 2501 struct sony_sc *sc = hid_get_drvdata(hdev);
0f398230 2502 int append_dev_id;
e1bc84d0 2503 int ret;
bd28ce00 2504
131a8a9a 2505 ret = sony_set_device_id(sc);
9b2b5c9a 2506 if (ret < 0) {
131a8a9a 2507 hid_err(hdev, "failed to allocate the device id\n");
9b2b5c9a
FP
2508 goto err_stop;
2509 }
2510
df848bc0
RC
2511 ret = append_dev_id = sony_check_add(sc);
2512 if (ret < 0)
2513 goto err_stop;
2514
131a8a9a 2515 ret = sony_allocate_output_report(sc);
8025087a 2516 if (ret < 0) {
131a8a9a 2517 hid_err(hdev, "failed to allocate the output report buffer\n");
8025087a
FP
2518 goto err_stop;
2519 }
2520
510c8b7c 2521 if (sc->quirks & NAVIGATION_CONTROLLER_USB) {
e534a935
BT
2522 /*
2523 * The Sony Sixaxis does not handle HID Output Reports on the
2524 * Interrupt EP like it could, so we need to force HID Output
2525 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2526 *
2527 * There is also another issue about HID Output Reports via USB,
2528 * the Sixaxis does not want the report_id as part of the data
2529 * packet, so we have to discard buf[0] when sending the actual
2530 * control message, even for numbered reports, humpf!
2a242932
FP
2531 *
2532 * Additionally, the Sixaxis on USB isn't properly initialized
2533 * until the PS logo button is pressed and as such won't retain
2534 * any state set by an output report, so the initial
2535 * configuration report is deferred until the first input
2536 * report arrives.
e534a935
BT
2537 */
2538 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2539 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
2a242932 2540 sc->defer_initialization = 1;
80ecc48c 2541
816651a7 2542 ret = sixaxis_set_operational_usb(hdev);
80ecc48c
RC
2543 if (ret < 0) {
2544 hid_err(hdev, "Failed to set controller into operational mode\n");
2545 goto err_stop;
2546 }
2547
d8aaccda 2548 sony_init_output_report(sc, sixaxis_send_output_report);
510c8b7c
RC
2549 } else if (sc->quirks & NAVIGATION_CONTROLLER_BT) {
2550 /*
2551 * The Navigation controller wants output reports sent on the ctrl
2552 * endpoint when connected via Bluetooth.
2553 */
2554 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2555
2556 ret = sixaxis_set_operational_bt(hdev);
2557 if (ret < 0) {
2558 hid_err(hdev, "Failed to set controller into operational mode\n");
2559 goto err_stop;
2560 }
2561
d8aaccda 2562 sony_init_output_report(sc, sixaxis_send_output_report);
510c8b7c
RC
2563 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
2564 /*
2565 * The Sony Sixaxis does not handle HID Output Reports on the
2566 * Interrupt EP and the device only becomes active when the
2567 * PS button is pressed. See comment for Navigation controller
2568 * above for more details.
2569 */
2570 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2571 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
2572 sc->defer_initialization = 1;
2573
2574 ret = sixaxis_set_operational_usb(hdev);
2575 if (ret < 0) {
2576 hid_err(hdev, "Failed to set controller into operational mode\n");
2577 goto err_stop;
2578 }
2579
2580 ret = sony_register_sensors(sc);
2581 if (ret) {
2582 hid_err(sc->hdev,
2583 "Unable to initialize motion sensors: %d\n", ret);
2584 goto err_stop;
2585 }
2586
2587 sony_init_output_report(sc, sixaxis_send_output_report);
2588 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
2078b9bb
FP
2589 /*
2590 * The Sixaxis wants output reports sent on the ctrl endpoint
2591 * when connected via Bluetooth.
2592 */
2593 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
80ecc48c 2594
816651a7 2595 ret = sixaxis_set_operational_bt(hdev);
80ecc48c
RC
2596 if (ret < 0) {
2597 hid_err(hdev, "Failed to set controller into operational mode\n");
2598 goto err_stop;
2599 }
2600
510c8b7c
RC
2601 ret = sony_register_sensors(sc);
2602 if (ret) {
2603 hid_err(sc->hdev,
2604 "Unable to initialize motion sensors: %d\n", ret);
2605 goto err_stop;
2606 }
2607
d8aaccda 2608 sony_init_output_report(sc, sixaxis_send_output_report);
fee4e2d5 2609 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
55a07d62
RC
2610 ret = dualshock4_get_calibration_data(sc);
2611 if (ret < 0) {
2612 hid_err(hdev, "Failed to get calibration data from Dualshock 4\n");
2613 goto err_stop;
68330d83 2614 }
c4e1ddf2 2615
e1bc84d0
RC
2616 /*
2617 * The Dualshock 4 touchpad supports 2 touches and has a
2618 * resolution of 1920x942 (44.86 dots/mm).
2619 */
ac797b95 2620 ret = sony_register_touchpad(sc, 2, 1920, 942);
e1bc84d0
RC
2621 if (ret) {
2622 hid_err(sc->hdev,
2623 "Unable to initialize multi-touch slots: %d\n",
2624 ret);
2b6579d4 2625 goto err_stop;
e1bc84d0
RC
2626 }
2627
227c011b
RC
2628 ret = sony_register_sensors(sc);
2629 if (ret) {
2630 hid_err(sc->hdev,
2631 "Unable to initialize motion sensors: %d\n", ret);
2632 goto err_stop;
2633 }
2634
77b499e7
RC
2635 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
2636 sc->ds4_bt_poll_interval = DS4_BT_DEFAULT_POLL_INTERVAL_MS;
2637 ret = device_create_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
2638 if (ret)
2639 hid_warn(sc->hdev,
2640 "can't create sysfs bt_poll_interval attribute err: %d\n",
2641 ret);
2642 }
2643
f2f47c38
RC
2644 if (sc->quirks & DUALSHOCK4_DONGLE) {
2645 INIT_WORK(&sc->hotplug_worker, dualshock4_calibration_work);
2646 sc->hotplug_worker_initialized = 1;
2647 sc->ds4_dongle_state = DONGLE_DISCONNECTED;
2648 }
2649
d8aaccda 2650 sony_init_output_report(sc, dualshock4_send_output_report);
c5e0c1c4 2651 } else if (sc->quirks & MOTION_CONTROLLER) {
d8aaccda 2652 sony_init_output_report(sc, motion_send_output_report);
0bd88dd3
FP
2653 } else {
2654 ret = 0;
2655 }
f9ce7c28 2656
0a286ef2 2657 if (sc->quirks & SONY_LED_SUPPORT) {
fa57a810 2658 ret = sony_leds_init(sc);
0a286ef2
SE
2659 if (ret < 0)
2660 goto err_stop;
2661 }
2662
d902f472 2663 if (sc->quirks & SONY_BATTERY_SUPPORT) {
0f398230 2664 ret = sony_battery_probe(sc, append_dev_id);
d902f472
FP
2665 if (ret < 0)
2666 goto err_stop;
2667
2668 /* Open the device to receive reports with battery info */
2669 ret = hid_hw_open(hdev);
2670 if (ret < 0) {
2671 hid_err(hdev, "hw open failed\n");
2672 goto err_stop;
2673 }
2674 }
2675
c8de9dbb 2676 if (sc->quirks & SONY_FF_SUPPORT) {
fa57a810 2677 ret = sony_init_ff(sc);
c8de9dbb
FP
2678 if (ret < 0)
2679 goto err_close;
5f5750d2 2680 }
a08c22c0 2681
bd28ce00 2682 return 0;
d902f472
FP
2683err_close:
2684 hid_hw_close(hdev);
bd28ce00 2685err_stop:
77b499e7
RC
2686 /* Piggy back on the default ds4_bt_ poll_interval to determine
2687 * if we need to remove the file as we don't know for sure if we
2688 * executed that logic.
2689 */
2690 if (sc->ds4_bt_poll_interval)
2691 device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
0a286ef2 2692 if (sc->quirks & SONY_LED_SUPPORT)
fa57a810 2693 sony_leds_remove(sc);
d902f472
FP
2694 if (sc->quirks & SONY_BATTERY_SUPPORT)
2695 sony_battery_remove(sc);
a687c576
RC
2696 if (sc->touchpad)
2697 sony_unregister_touchpad(sc);
a676bdc4
RC
2698 if (sc->sensor_dev)
2699 sony_unregister_sensors(sc);
46262047 2700 sony_cancel_work_sync(sc);
9b2b5c9a 2701 kfree(sc->output_report_dmabuf);
d2d782fc 2702 sony_remove_dev_list(sc);
8025087a 2703 sony_release_device_id(sc);
bd28ce00 2704 hid_hw_stop(hdev);
bd28ce00
JS
2705 return ret;
2706}
2707
e1bc84d0
RC
2708static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2709{
2710 int ret;
2711 unsigned long quirks = id->driver_data;
2712 struct sony_sc *sc;
2713 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2714
2715 if (!strcmp(hdev->name, "FutureMax Dance Mat"))
2716 quirks |= FUTUREMAX_DANCE_MAT;
2717
2718 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2719 if (sc == NULL) {
2720 hid_err(hdev, "can't alloc sony descriptor\n");
2721 return -ENOMEM;
2722 }
2723
2724 spin_lock_init(&sc->lock);
2725
2726 sc->quirks = quirks;
2727 hid_set_drvdata(hdev, sc);
2728 sc->hdev = hdev;
2729
2730 ret = hid_parse(hdev);
2731 if (ret) {
2732 hid_err(hdev, "parse failed\n");
2733 return ret;
2734 }
2735
2736 if (sc->quirks & VAIO_RDESC_CONSTANT)
2737 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2738 else if (sc->quirks & SIXAXIS_CONTROLLER)
2739 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2740
e19a267b 2741 /* Patch the hw version on DS3/4 compatible devices, so applications can
9131f8cc
RC
2742 * distinguish between the default HID mappings and the mappings defined
2743 * by the Linux game controller spec. This is important for the SDL2
2744 * library, which has a game controller database, which uses device ids
2745 * in combination with version as a key.
2746 */
e19a267b 2747 if (sc->quirks & (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER))
9131f8cc
RC
2748 hdev->version |= 0x8000;
2749
e1bc84d0
RC
2750 ret = hid_hw_start(hdev, connect_mask);
2751 if (ret) {
2752 hid_err(hdev, "hw start failed\n");
2753 return ret;
2754 }
2755
4f967f6d
RC
2756 /* sony_input_configured can fail, but this doesn't result
2757 * in hid_hw_start failures (intended). Check whether
2758 * the HID layer claimed the device else fail.
2759 * We don't know the actual reason for the failure, most
2760 * likely it is due to EEXIST in case of double connection
2761 * of USB and Bluetooth, but could have been due to ENOMEM
2762 * or other reasons as well.
2763 */
2764 if (!(hdev->claimed & HID_CLAIMED_INPUT)) {
2765 hid_err(hdev, "failed to claim input\n");
2766 return -ENODEV;
2767 }
2768
e1bc84d0
RC
2769 return ret;
2770}
2771
cc6e0bbb
JK
2772static void sony_remove(struct hid_device *hdev)
2773{
f04d5140
CL
2774 struct sony_sc *sc = hid_get_drvdata(hdev);
2775
ac797b95
RC
2776 hid_hw_close(hdev);
2777
0a286ef2 2778 if (sc->quirks & SONY_LED_SUPPORT)
fa57a810 2779 sony_leds_remove(sc);
f04d5140 2780
ac797b95 2781 if (sc->quirks & SONY_BATTERY_SUPPORT)
d902f472 2782 sony_battery_remove(sc);
ac797b95
RC
2783
2784 if (sc->touchpad)
2785 sony_unregister_touchpad(sc);
d902f472 2786
227c011b
RC
2787 if (sc->sensor_dev)
2788 sony_unregister_sensors(sc);
2789
77b499e7
RC
2790 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
2791 device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
2792
46262047 2793 sony_cancel_work_sync(sc);
9f323b68 2794
9b2b5c9a
FP
2795 kfree(sc->output_report_dmabuf);
2796
d2d782fc 2797 sony_remove_dev_list(sc);
9f323b68 2798
8025087a
FP
2799 sony_release_device_id(sc);
2800
cc6e0bbb 2801 hid_hw_stop(hdev);
cc6e0bbb
JK
2802}
2803
decd946c
FP
2804#ifdef CONFIG_PM
2805
2806static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2807{
decd946c 2808#ifdef CONFIG_SONY_FF
decd946c 2809
765a1077
FP
2810 /* On suspend stop any running force-feedback events */
2811 if (SONY_FF_SUPPORT) {
2812 struct sony_sc *sc = hid_get_drvdata(hdev);
decd946c 2813
765a1077 2814 sc->left = sc->right = 0;
decd946c
FP
2815 sony_send_output_report(sc);
2816 }
2817
765a1077 2818#endif
decd946c
FP
2819 return 0;
2820}
2821
2822static int sony_resume(struct hid_device *hdev)
2823{
765a1077 2824 struct sony_sc *sc = hid_get_drvdata(hdev);
decd946c 2825
765a1077
FP
2826 /*
2827 * The Sixaxis and navigation controllers on USB need to be
2828 * reinitialized on resume or they won't behave properly.
2829 */
2830 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2831 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2832 sixaxis_set_operational_usb(sc->hdev);
2833 sc->defer_initialization = 1;
decd946c
FP
2834 }
2835
2836 return 0;
2837}
2838
2839#endif
2840
bd28ce00 2841static const struct hid_device_id sony_devices[] = {
816651a7
AO
2842 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2843 .driver_data = SIXAXIS_CONTROLLER_USB },
35dca5b4 2844 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
4545ee0a 2845 .driver_data = NAVIGATION_CONTROLLER_USB },
6eabaaa0 2846 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
4545ee0a 2847 .driver_data = NAVIGATION_CONTROLLER_BT },
c5e0c1c4 2848 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
b3bca326 2849 .driver_data = MOTION_CONTROLLER_USB },
a4afa854 2850 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
b3bca326 2851 .driver_data = MOTION_CONTROLLER_BT },
816651a7
AO
2852 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2853 .driver_data = SIXAXIS_CONTROLLER_BT },
cc6e0bbb
JK
2854 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2855 .driver_data = VAIO_RDESC_CONSTANT },
a4649184
FLVC
2856 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2857 .driver_data = VAIO_RDESC_CONSTANT },
ef916ef5
AO
2858 /*
2859 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2860 * Logitech joystick from the device descriptor.
2861 */
f04d5140
CL
2862 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2863 .driver_data = BUZZ_CONTROLLER },
2864 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2865 .driver_data = BUZZ_CONTROLLER },
078328da
JK
2866 /* PS3 BD Remote Control */
2867 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2868 .driver_data = PS3REMOTE },
2869 /* Logitech Harmony Adapter for PS3 */
2870 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2871 .driver_data = PS3REMOTE },
68a49e51
FP
2872 /* SMK-Link PS3 BD Remote Control */
2873 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2874 .driver_data = PS3REMOTE },
0bd88dd3
FP
2875 /* Sony Dualshock 4 controllers for PS4 */
2876 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
8ab1676b 2877 .driver_data = DUALSHOCK4_CONTROLLER_USB },
0bd88dd3 2878 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
8ab1676b 2879 .driver_data = DUALSHOCK4_CONTROLLER_BT },
cf1015d6
RC
2880 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2881 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2882 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2883 .driver_data = DUALSHOCK4_CONTROLLER_BT },
de66a1a0 2884 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
35f436c3 2885 .driver_data = DUALSHOCK4_DONGLE },
74500cc8
SM
2886 /* Nyko Core Controller for PS3 */
2887 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
2888 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
bd28ce00
JS
2889 { }
2890};
2891MODULE_DEVICE_TABLE(hid, sony_devices);
2892
2893static struct hid_driver sony_driver = {
ce8efc3b
FP
2894 .name = "sony",
2895 .id_table = sony_devices,
2896 .input_mapping = sony_mapping,
2897 .input_configured = sony_input_configured,
2898 .probe = sony_probe,
2899 .remove = sony_remove,
2900 .report_fixup = sony_report_fixup,
decd946c
FP
2901 .raw_event = sony_raw_event,
2902
2903#ifdef CONFIG_PM
2904 .suspend = sony_suspend,
2905 .resume = sony_resume,
2906 .reset_resume = sony_resume,
2907#endif
bd28ce00 2908};
8025087a
FP
2909
2910static int __init sony_init(void)
2911{
2912 dbg_hid("Sony:%s\n", __func__);
2913
2914 return hid_register_driver(&sony_driver);
2915}
2916
2917static void __exit sony_exit(void)
2918{
2919 dbg_hid("Sony:%s\n", __func__);
2920
8025087a 2921 hid_unregister_driver(&sony_driver);
6c40065f 2922 ida_destroy(&sony_device_id_allocator);
8025087a
FP
2923}
2924module_init(sony_init);
2925module_exit(sony_exit);
bd28ce00 2926
bd28ce00 2927MODULE_LICENSE("GPL");