]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hid/hid-sony.c
HID: sony: Fix memory issue when connecting device using both Bluetooth and USB
[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)
b3bca326
SW
51#define MOTION_CONTROLLER_USB BIT(7)
52#define MOTION_CONTROLLER_BT BIT(8)
4545ee0a
SW
53#define NAVIGATION_CONTROLLER_USB BIT(9)
54#define NAVIGATION_CONTROLLER_BT BIT(10)
74500cc8 55#define SINO_LITE_CONTROLLER BIT(11)
4ba1eeeb 56#define FUTUREMAX_DANCE_MAT BIT(12)
cc6e0bbb 57
fee4e2d5 58#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
b3bca326 59#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
4545ee0a
SW
60#define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
61 NAVIGATION_CONTROLLER_BT)
68330d83
FP
62#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
63 DUALSHOCK4_CONTROLLER_BT)
fee4e2d5 64#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
4545ee0a
SW
65 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
66 NAVIGATION_CONTROLLER)
12e9a6d7 67#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
4545ee0a 68 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
c5e0c1c4
FP
69#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
70 MOTION_CONTROLLER)
0f398230
FP
71#define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\
72 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
60781cf4
FP
73
74#define MAX_LEDS 4
0a286ef2 75
4c3e8298
FP
76/*
77 * The Sixaxis reports both digital and analog values for each button on the
78 * controller except for Start, Select and the PS button. The controller ends
79 * up reporting 27 axes which causes them to spill over into the multi-touch
80 * axis values. Additionally, the controller only has 20 actual, physical axes
81 * so there are several unused axes in between the used ones.
82 */
1adf904e 83static u8 sixaxis_rdesc[] = {
fb705a6d 84 0x05, 0x01, /* Usage Page (Desktop), */
4c3e8298 85 0x09, 0x04, /* Usage (Joystick), */
fb705a6d
AO
86 0xA1, 0x01, /* Collection (Application), */
87 0xA1, 0x02, /* Collection (Logical), */
88 0x85, 0x01, /* Report ID (1), */
89 0x75, 0x08, /* Report Size (8), */
90 0x95, 0x01, /* Report Count (1), */
91 0x15, 0x00, /* Logical Minimum (0), */
92 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
93 0x81, 0x03, /* Input (Constant, Variable), */
94 0x75, 0x01, /* Report Size (1), */
95 0x95, 0x13, /* Report Count (19), */
96 0x15, 0x00, /* Logical Minimum (0), */
97 0x25, 0x01, /* Logical Maximum (1), */
98 0x35, 0x00, /* Physical Minimum (0), */
99 0x45, 0x01, /* Physical Maximum (1), */
100 0x05, 0x09, /* Usage Page (Button), */
101 0x19, 0x01, /* Usage Minimum (01h), */
102 0x29, 0x13, /* Usage Maximum (13h), */
103 0x81, 0x02, /* Input (Variable), */
104 0x75, 0x01, /* Report Size (1), */
105 0x95, 0x0D, /* Report Count (13), */
106 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
107 0x81, 0x03, /* Input (Constant, Variable), */
108 0x15, 0x00, /* Logical Minimum (0), */
109 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
110 0x05, 0x01, /* Usage Page (Desktop), */
111 0x09, 0x01, /* Usage (Pointer), */
112 0xA1, 0x00, /* Collection (Physical), */
113 0x75, 0x08, /* Report Size (8), */
114 0x95, 0x04, /* Report Count (4), */
115 0x35, 0x00, /* Physical Minimum (0), */
116 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
117 0x09, 0x30, /* Usage (X), */
118 0x09, 0x31, /* Usage (Y), */
119 0x09, 0x32, /* Usage (Z), */
120 0x09, 0x35, /* Usage (Rz), */
121 0x81, 0x02, /* Input (Variable), */
122 0xC0, /* End Collection, */
123 0x05, 0x01, /* Usage Page (Desktop), */
124 0x95, 0x13, /* Report Count (19), */
125 0x09, 0x01, /* Usage (Pointer), */
126 0x81, 0x02, /* Input (Variable), */
127 0x95, 0x0C, /* Report Count (12), */
128 0x81, 0x01, /* Input (Constant), */
129 0x75, 0x10, /* Report Size (16), */
130 0x95, 0x04, /* Report Count (4), */
131 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
132 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
133 0x09, 0x01, /* Usage (Pointer), */
134 0x81, 0x02, /* Input (Variable), */
135 0xC0, /* End Collection, */
136 0xA1, 0x02, /* Collection (Logical), */
137 0x85, 0x02, /* Report ID (2), */
138 0x75, 0x08, /* Report Size (8), */
139 0x95, 0x30, /* Report Count (48), */
140 0x09, 0x01, /* Usage (Pointer), */
141 0xB1, 0x02, /* Feature (Variable), */
142 0xC0, /* End Collection, */
143 0xA1, 0x02, /* Collection (Logical), */
144 0x85, 0xEE, /* Report ID (238), */
145 0x75, 0x08, /* Report Size (8), */
146 0x95, 0x30, /* Report Count (48), */
147 0x09, 0x01, /* Usage (Pointer), */
148 0xB1, 0x02, /* Feature (Variable), */
149 0xC0, /* End Collection, */
150 0xA1, 0x02, /* Collection (Logical), */
151 0x85, 0xEF, /* Report ID (239), */
152 0x75, 0x08, /* Report Size (8), */
153 0x95, 0x30, /* Report Count (48), */
154 0x09, 0x01, /* Usage (Pointer), */
155 0xB1, 0x02, /* Feature (Variable), */
156 0xC0, /* End Collection, */
157 0xC0 /* End Collection */
e57a67da
MCC
158};
159
c5e0c1c4 160/* PS/3 Motion controller */
1adf904e 161static u8 motion_rdesc[] = {
c5e0c1c4
FP
162 0x05, 0x01, /* Usage Page (Desktop), */
163 0x09, 0x04, /* Usage (Joystick), */
164 0xA1, 0x01, /* Collection (Application), */
165 0xA1, 0x02, /* Collection (Logical), */
166 0x85, 0x01, /* Report ID (1), */
c5e0c1c4 167 0x75, 0x01, /* Report Size (1), */
8b2513c3 168 0x95, 0x15, /* Report Count (21), */
c5e0c1c4
FP
169 0x15, 0x00, /* Logical Minimum (0), */
170 0x25, 0x01, /* Logical Maximum (1), */
171 0x35, 0x00, /* Physical Minimum (0), */
172 0x45, 0x01, /* Physical Maximum (1), */
173 0x05, 0x09, /* Usage Page (Button), */
174 0x19, 0x01, /* Usage Minimum (01h), */
8b2513c3
SW
175 0x29, 0x15, /* Usage Maximum (15h), */
176 0x81, 0x02, /* Input (Variable), * Buttons */
177 0x95, 0x0B, /* Report Count (11), */
c5e0c1c4 178 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
8b2513c3 179 0x81, 0x03, /* Input (Constant, Variable), * Padding */
c5e0c1c4
FP
180 0x15, 0x00, /* Logical Minimum (0), */
181 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
182 0x05, 0x01, /* Usage Page (Desktop), */
c5e0c1c4
FP
183 0xA1, 0x00, /* Collection (Physical), */
184 0x75, 0x08, /* Report Size (8), */
8b2513c3 185 0x95, 0x01, /* Report Count (1), */
c5e0c1c4
FP
186 0x35, 0x00, /* Physical Minimum (0), */
187 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
188 0x09, 0x30, /* Usage (X), */
8b2513c3 189 0x81, 0x02, /* Input (Variable), * Trigger */
c5e0c1c4 190 0xC0, /* End Collection, */
8b2513c3
SW
191 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
192 0x75, 0x08, /* Report Size (8), */
193 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
c5e0c1c4 194 0x81, 0x02, /* Input (Variable), */
8b2513c3 195 0x05, 0x01, /* Usage Page (Desktop), */
c5e0c1c4 196 0x75, 0x10, /* Report Size (16), */
8b2513c3
SW
197 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
198 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
199 0x95, 0x03, /* Report Count (3), * 3x Accels */
200 0x09, 0x33, /* Usage (rX), */
201 0x09, 0x34, /* Usage (rY), */
202 0x09, 0x35, /* Usage (rZ), */
203 0x81, 0x02, /* Input (Variable), */
204 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
205 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
206 0x81, 0x02, /* Input (Variable), */
207 0x05, 0x01, /* Usage Page (Desktop), */
c5e0c1c4 208 0x09, 0x01, /* Usage (Pointer), */
8b2513c3
SW
209 0x95, 0x03, /* Report Count (3), * 3x Gyros */
210 0x81, 0x02, /* Input (Variable), */
211 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
212 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
c5e0c1c4 213 0x81, 0x02, /* Input (Variable), */
8b2513c3
SW
214 0x75, 0x0C, /* Report Size (12), */
215 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
216 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
217 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
218 0x81, 0x02, /* Input (Variable), */
219 0x75, 0x08, /* Report Size (8), */
220 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
221 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
222 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
223 0x81, 0x02, /* Input (Variable), */
224 0x75, 0x08, /* Report Size (8), */
225 0x95, 0x30, /* Report Count (48), */
226 0x09, 0x01, /* Usage (Pointer), */
227 0x91, 0x02, /* Output (Variable), */
228 0x75, 0x08, /* Report Size (8), */
229 0x95, 0x30, /* Report Count (48), */
230 0x09, 0x01, /* Usage (Pointer), */
231 0xB1, 0x02, /* Feature (Variable), */
c5e0c1c4
FP
232 0xC0, /* End Collection, */
233 0xA1, 0x02, /* Collection (Logical), */
234 0x85, 0x02, /* Report ID (2), */
235 0x75, 0x08, /* Report Size (8), */
236 0x95, 0x30, /* Report Count (48), */
237 0x09, 0x01, /* Usage (Pointer), */
238 0xB1, 0x02, /* Feature (Variable), */
239 0xC0, /* End Collection, */
240 0xA1, 0x02, /* Collection (Logical), */
241 0x85, 0xEE, /* Report ID (238), */
242 0x75, 0x08, /* Report Size (8), */
243 0x95, 0x30, /* Report Count (48), */
244 0x09, 0x01, /* Usage (Pointer), */
245 0xB1, 0x02, /* Feature (Variable), */
246 0xC0, /* End Collection, */
247 0xA1, 0x02, /* Collection (Logical), */
248 0x85, 0xEF, /* Report ID (239), */
249 0x75, 0x08, /* Report Size (8), */
250 0x95, 0x30, /* Report Count (48), */
251 0x09, 0x01, /* Usage (Pointer), */
252 0xB1, 0x02, /* Feature (Variable), */
253 0xC0, /* End Collection, */
254 0xC0 /* End Collection */
255};
256
b2723eb7 257/* PS/3 Navigation controller */
1adf904e 258static u8 navigation_rdesc[] = {
b2723eb7 259 0x05, 0x01, /* Usage Page (Desktop), */
d542176f 260 0x09, 0x04, /* Usage (Joystick), */
b2723eb7
SW
261 0xA1, 0x01, /* Collection (Application), */
262 0xA1, 0x02, /* Collection (Logical), */
263 0x85, 0x01, /* Report ID (1), */
264 0x75, 0x08, /* Report Size (8), */
265 0x95, 0x01, /* Report Count (1), */
266 0x15, 0x00, /* Logical Minimum (0), */
267 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
268 0x81, 0x03, /* Input (Constant, Variable), */
269 0x75, 0x01, /* Report Size (1), */
270 0x95, 0x13, /* Report Count (19), */
271 0x15, 0x00, /* Logical Minimum (0), */
272 0x25, 0x01, /* Logical Maximum (1), */
273 0x35, 0x00, /* Physical Minimum (0), */
274 0x45, 0x01, /* Physical Maximum (1), */
275 0x05, 0x09, /* Usage Page (Button), */
276 0x19, 0x01, /* Usage Minimum (01h), */
277 0x29, 0x13, /* Usage Maximum (13h), */
278 0x81, 0x02, /* Input (Variable), */
279 0x75, 0x01, /* Report Size (1), */
280 0x95, 0x0D, /* Report Count (13), */
281 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
282 0x81, 0x03, /* Input (Constant, Variable), */
283 0x15, 0x00, /* Logical Minimum (0), */
284 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
285 0x05, 0x01, /* Usage Page (Desktop), */
286 0x09, 0x01, /* Usage (Pointer), */
287 0xA1, 0x00, /* Collection (Physical), */
288 0x75, 0x08, /* Report Size (8), */
289 0x95, 0x02, /* Report Count (2), */
290 0x35, 0x00, /* Physical Minimum (0), */
291 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
292 0x09, 0x30, /* Usage (X), */
293 0x09, 0x31, /* Usage (Y), */
294 0x81, 0x02, /* Input (Variable), */
295 0xC0, /* End Collection, */
296 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
297 0x95, 0x06, /* Report Count (6), */
298 0x81, 0x03, /* Input (Constant, Variable), */
299 0x05, 0x01, /* Usage Page (Desktop), */
300 0x75, 0x08, /* Report Size (8), */
301 0x95, 0x05, /* Report Count (5), */
302 0x09, 0x01, /* Usage (Pointer), */
303 0x81, 0x02, /* Input (Variable), */
304 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
2259b5bb
SW
305 0x95, 0x01, /* Report Count (1), */
306 0x81, 0x02, /* Input (Variable), */
307 0x05, 0x01, /* Usage Page (Desktop), */
308 0x95, 0x01, /* Report Count (1), */
309 0x09, 0x01, /* Usage (Pointer), */
310 0x81, 0x02, /* Input (Variable), */
311 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
312 0x95, 0x1E, /* Report Count (24), */
b2723eb7
SW
313 0x81, 0x02, /* Input (Variable), */
314 0x75, 0x08, /* Report Size (8), */
315 0x95, 0x30, /* Report Count (48), */
316 0x09, 0x01, /* Usage (Pointer), */
317 0x91, 0x02, /* Output (Variable), */
318 0x75, 0x08, /* Report Size (8), */
319 0x95, 0x30, /* Report Count (48), */
320 0x09, 0x01, /* Usage (Pointer), */
321 0xB1, 0x02, /* Feature (Variable), */
322 0xC0, /* End Collection, */
323 0xA1, 0x02, /* Collection (Logical), */
324 0x85, 0x02, /* Report ID (2), */
325 0x75, 0x08, /* Report Size (8), */
326 0x95, 0x30, /* Report Count (48), */
327 0x09, 0x01, /* Usage (Pointer), */
328 0xB1, 0x02, /* Feature (Variable), */
329 0xC0, /* End Collection, */
330 0xA1, 0x02, /* Collection (Logical), */
331 0x85, 0xEE, /* Report ID (238), */
332 0x75, 0x08, /* Report Size (8), */
333 0x95, 0x30, /* Report Count (48), */
334 0x09, 0x01, /* Usage (Pointer), */
335 0xB1, 0x02, /* Feature (Variable), */
336 0xC0, /* End Collection, */
337 0xA1, 0x02, /* Collection (Logical), */
338 0x85, 0xEF, /* Report ID (239), */
339 0x75, 0x08, /* Report Size (8), */
340 0x95, 0x30, /* Report Count (48), */
341 0x09, 0x01, /* Usage (Pointer), */
342 0xB1, 0x02, /* Feature (Variable), */
343 0xC0, /* End Collection, */
344 0xC0 /* End Collection */
345};
c5e0c1c4 346
ad142b9e
FP
347/*
348 * The default descriptor doesn't provide mapping for the accelerometers
58d7027b
FP
349 * or orientation sensors. This fixed descriptor maps the accelerometers
350 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
351 * to usage values 0x43, 0x44 and 0x45.
352 */
ed19d8cf 353static u8 dualshock4_usb_rdesc[] = {
58d7027b
FP
354 0x05, 0x01, /* Usage Page (Desktop), */
355 0x09, 0x05, /* Usage (Gamepad), */
356 0xA1, 0x01, /* Collection (Application), */
357 0x85, 0x01, /* Report ID (1), */
358 0x09, 0x30, /* Usage (X), */
359 0x09, 0x31, /* Usage (Y), */
360 0x09, 0x32, /* Usage (Z), */
361 0x09, 0x35, /* Usage (Rz), */
362 0x15, 0x00, /* Logical Minimum (0), */
363 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
364 0x75, 0x08, /* Report Size (8), */
365 0x95, 0x04, /* Report Count (4), */
366 0x81, 0x02, /* Input (Variable), */
367 0x09, 0x39, /* Usage (Hat Switch), */
368 0x15, 0x00, /* Logical Minimum (0), */
369 0x25, 0x07, /* Logical Maximum (7), */
370 0x35, 0x00, /* Physical Minimum (0), */
371 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
372 0x65, 0x14, /* Unit (Degrees), */
373 0x75, 0x04, /* Report Size (4), */
374 0x95, 0x01, /* Report Count (1), */
375 0x81, 0x42, /* Input (Variable, Null State), */
376 0x65, 0x00, /* Unit, */
377 0x05, 0x09, /* Usage Page (Button), */
378 0x19, 0x01, /* Usage Minimum (01h), */
379 0x29, 0x0E, /* Usage Maximum (0Eh), */
380 0x15, 0x00, /* Logical Minimum (0), */
381 0x25, 0x01, /* Logical Maximum (1), */
382 0x75, 0x01, /* Report Size (1), */
383 0x95, 0x0E, /* Report Count (14), */
384 0x81, 0x02, /* Input (Variable), */
385 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
386 0x09, 0x20, /* Usage (20h), */
387 0x75, 0x06, /* Report Size (6), */
388 0x95, 0x01, /* Report Count (1), */
389 0x15, 0x00, /* Logical Minimum (0), */
fb291cbd 390 0x25, 0x3F, /* Logical Maximum (63), */
58d7027b
FP
391 0x81, 0x02, /* Input (Variable), */
392 0x05, 0x01, /* Usage Page (Desktop), */
393 0x09, 0x33, /* Usage (Rx), */
394 0x09, 0x34, /* Usage (Ry), */
395 0x15, 0x00, /* Logical Minimum (0), */
396 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
397 0x75, 0x08, /* Report Size (8), */
398 0x95, 0x02, /* Report Count (2), */
399 0x81, 0x02, /* Input (Variable), */
400 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
401 0x09, 0x21, /* Usage (21h), */
402 0x95, 0x03, /* Report Count (3), */
403 0x81, 0x02, /* Input (Variable), */
404 0x05, 0x01, /* Usage Page (Desktop), */
405 0x19, 0x40, /* Usage Minimum (40h), */
406 0x29, 0x42, /* Usage Maximum (42h), */
407 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
bdae9e0e 408 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
58d7027b
FP
409 0x75, 0x10, /* Report Size (16), */
410 0x95, 0x03, /* Report Count (3), */
411 0x81, 0x02, /* Input (Variable), */
412 0x19, 0x43, /* Usage Minimum (43h), */
413 0x29, 0x45, /* Usage Maximum (45h), */
bdae9e0e
RC
414 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
415 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
58d7027b
FP
416 0x95, 0x03, /* Report Count (3), */
417 0x81, 0x02, /* Input (Variable), */
418 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
419 0x09, 0x21, /* Usage (21h), */
420 0x15, 0x00, /* Logical Minimum (0), */
fb291cbd 421 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
58d7027b
FP
422 0x75, 0x08, /* Report Size (8), */
423 0x95, 0x27, /* Report Count (39), */
424 0x81, 0x02, /* Input (Variable), */
425 0x85, 0x05, /* Report ID (5), */
426 0x09, 0x22, /* Usage (22h), */
427 0x95, 0x1F, /* Report Count (31), */
428 0x91, 0x02, /* Output (Variable), */
429 0x85, 0x04, /* Report ID (4), */
430 0x09, 0x23, /* Usage (23h), */
431 0x95, 0x24, /* Report Count (36), */
432 0xB1, 0x02, /* Feature (Variable), */
433 0x85, 0x02, /* Report ID (2), */
434 0x09, 0x24, /* Usage (24h), */
435 0x95, 0x24, /* Report Count (36), */
436 0xB1, 0x02, /* Feature (Variable), */
437 0x85, 0x08, /* Report ID (8), */
438 0x09, 0x25, /* Usage (25h), */
439 0x95, 0x03, /* Report Count (3), */
440 0xB1, 0x02, /* Feature (Variable), */
441 0x85, 0x10, /* Report ID (16), */
442 0x09, 0x26, /* Usage (26h), */
443 0x95, 0x04, /* Report Count (4), */
444 0xB1, 0x02, /* Feature (Variable), */
445 0x85, 0x11, /* Report ID (17), */
446 0x09, 0x27, /* Usage (27h), */
447 0x95, 0x02, /* Report Count (2), */
448 0xB1, 0x02, /* Feature (Variable), */
449 0x85, 0x12, /* Report ID (18), */
450 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
451 0x09, 0x21, /* Usage (21h), */
452 0x95, 0x0F, /* Report Count (15), */
453 0xB1, 0x02, /* Feature (Variable), */
454 0x85, 0x13, /* Report ID (19), */
455 0x09, 0x22, /* Usage (22h), */
456 0x95, 0x16, /* Report Count (22), */
457 0xB1, 0x02, /* Feature (Variable), */
458 0x85, 0x14, /* Report ID (20), */
459 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
460 0x09, 0x20, /* Usage (20h), */
461 0x95, 0x10, /* Report Count (16), */
462 0xB1, 0x02, /* Feature (Variable), */
463 0x85, 0x15, /* Report ID (21), */
464 0x09, 0x21, /* Usage (21h), */
465 0x95, 0x2C, /* Report Count (44), */
466 0xB1, 0x02, /* Feature (Variable), */
467 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
468 0x85, 0x80, /* Report ID (128), */
469 0x09, 0x20, /* Usage (20h), */
470 0x95, 0x06, /* Report Count (6), */
471 0xB1, 0x02, /* Feature (Variable), */
472 0x85, 0x81, /* Report ID (129), */
473 0x09, 0x21, /* Usage (21h), */
474 0x95, 0x06, /* Report Count (6), */
475 0xB1, 0x02, /* Feature (Variable), */
476 0x85, 0x82, /* Report ID (130), */
477 0x09, 0x22, /* Usage (22h), */
478 0x95, 0x05, /* Report Count (5), */
479 0xB1, 0x02, /* Feature (Variable), */
480 0x85, 0x83, /* Report ID (131), */
481 0x09, 0x23, /* Usage (23h), */
482 0x95, 0x01, /* Report Count (1), */
483 0xB1, 0x02, /* Feature (Variable), */
484 0x85, 0x84, /* Report ID (132), */
485 0x09, 0x24, /* Usage (24h), */
486 0x95, 0x04, /* Report Count (4), */
487 0xB1, 0x02, /* Feature (Variable), */
488 0x85, 0x85, /* Report ID (133), */
489 0x09, 0x25, /* Usage (25h), */
490 0x95, 0x06, /* Report Count (6), */
491 0xB1, 0x02, /* Feature (Variable), */
492 0x85, 0x86, /* Report ID (134), */
493 0x09, 0x26, /* Usage (26h), */
494 0x95, 0x06, /* Report Count (6), */
495 0xB1, 0x02, /* Feature (Variable), */
496 0x85, 0x87, /* Report ID (135), */
497 0x09, 0x27, /* Usage (27h), */
498 0x95, 0x23, /* Report Count (35), */
499 0xB1, 0x02, /* Feature (Variable), */
500 0x85, 0x88, /* Report ID (136), */
501 0x09, 0x28, /* Usage (28h), */
502 0x95, 0x22, /* Report Count (34), */
503 0xB1, 0x02, /* Feature (Variable), */
504 0x85, 0x89, /* Report ID (137), */
505 0x09, 0x29, /* Usage (29h), */
506 0x95, 0x02, /* Report Count (2), */
507 0xB1, 0x02, /* Feature (Variable), */
508 0x85, 0x90, /* Report ID (144), */
509 0x09, 0x30, /* Usage (30h), */
510 0x95, 0x05, /* Report Count (5), */
511 0xB1, 0x02, /* Feature (Variable), */
512 0x85, 0x91, /* Report ID (145), */
513 0x09, 0x31, /* Usage (31h), */
514 0x95, 0x03, /* Report Count (3), */
515 0xB1, 0x02, /* Feature (Variable), */
516 0x85, 0x92, /* Report ID (146), */
517 0x09, 0x32, /* Usage (32h), */
518 0x95, 0x03, /* Report Count (3), */
519 0xB1, 0x02, /* Feature (Variable), */
520 0x85, 0x93, /* Report ID (147), */
521 0x09, 0x33, /* Usage (33h), */
522 0x95, 0x0C, /* Report Count (12), */
523 0xB1, 0x02, /* Feature (Variable), */
524 0x85, 0xA0, /* Report ID (160), */
525 0x09, 0x40, /* Usage (40h), */
526 0x95, 0x06, /* Report Count (6), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0xA1, /* Report ID (161), */
529 0x09, 0x41, /* Usage (41h), */
530 0x95, 0x01, /* Report Count (1), */
531 0xB1, 0x02, /* Feature (Variable), */
532 0x85, 0xA2, /* Report ID (162), */
533 0x09, 0x42, /* Usage (42h), */
534 0x95, 0x01, /* Report Count (1), */
535 0xB1, 0x02, /* Feature (Variable), */
536 0x85, 0xA3, /* Report ID (163), */
537 0x09, 0x43, /* Usage (43h), */
538 0x95, 0x30, /* Report Count (48), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0xA4, /* Report ID (164), */
541 0x09, 0x44, /* Usage (44h), */
542 0x95, 0x0D, /* Report Count (13), */
543 0xB1, 0x02, /* Feature (Variable), */
544 0x85, 0xA5, /* Report ID (165), */
545 0x09, 0x45, /* Usage (45h), */
546 0x95, 0x15, /* Report Count (21), */
547 0xB1, 0x02, /* Feature (Variable), */
548 0x85, 0xA6, /* Report ID (166), */
549 0x09, 0x46, /* Usage (46h), */
550 0x95, 0x15, /* Report Count (21), */
551 0xB1, 0x02, /* Feature (Variable), */
552 0x85, 0xF0, /* Report ID (240), */
553 0x09, 0x47, /* Usage (47h), */
554 0x95, 0x3F, /* Report Count (63), */
555 0xB1, 0x02, /* Feature (Variable), */
556 0x85, 0xF1, /* Report ID (241), */
557 0x09, 0x48, /* Usage (48h), */
558 0x95, 0x3F, /* Report Count (63), */
559 0xB1, 0x02, /* Feature (Variable), */
560 0x85, 0xF2, /* Report ID (242), */
561 0x09, 0x49, /* Usage (49h), */
562 0x95, 0x0F, /* Report Count (15), */
563 0xB1, 0x02, /* Feature (Variable), */
564 0x85, 0xA7, /* Report ID (167), */
565 0x09, 0x4A, /* Usage (4Ah), */
566 0x95, 0x01, /* Report Count (1), */
567 0xB1, 0x02, /* Feature (Variable), */
568 0x85, 0xA8, /* Report ID (168), */
569 0x09, 0x4B, /* Usage (4Bh), */
570 0x95, 0x01, /* Report Count (1), */
571 0xB1, 0x02, /* Feature (Variable), */
572 0x85, 0xA9, /* Report ID (169), */
573 0x09, 0x4C, /* Usage (4Ch), */
574 0x95, 0x08, /* Report Count (8), */
575 0xB1, 0x02, /* Feature (Variable), */
576 0x85, 0xAA, /* Report ID (170), */
577 0x09, 0x4E, /* Usage (4Eh), */
578 0x95, 0x01, /* Report Count (1), */
579 0xB1, 0x02, /* Feature (Variable), */
580 0x85, 0xAB, /* Report ID (171), */
581 0x09, 0x4F, /* Usage (4Fh), */
582 0x95, 0x39, /* Report Count (57), */
583 0xB1, 0x02, /* Feature (Variable), */
584 0x85, 0xAC, /* Report ID (172), */
585 0x09, 0x50, /* Usage (50h), */
586 0x95, 0x39, /* Report Count (57), */
587 0xB1, 0x02, /* Feature (Variable), */
588 0x85, 0xAD, /* Report ID (173), */
589 0x09, 0x51, /* Usage (51h), */
590 0x95, 0x0B, /* Report Count (11), */
591 0xB1, 0x02, /* Feature (Variable), */
592 0x85, 0xAE, /* Report ID (174), */
593 0x09, 0x52, /* Usage (52h), */
594 0x95, 0x01, /* Report Count (1), */
595 0xB1, 0x02, /* Feature (Variable), */
596 0x85, 0xAF, /* Report ID (175), */
597 0x09, 0x53, /* Usage (53h), */
598 0x95, 0x02, /* Report Count (2), */
599 0xB1, 0x02, /* Feature (Variable), */
600 0x85, 0xB0, /* Report ID (176), */
601 0x09, 0x54, /* Usage (54h), */
602 0x95, 0x3F, /* Report Count (63), */
603 0xB1, 0x02, /* Feature (Variable), */
604 0xC0 /* End Collection */
ed19d8cf
FP
605};
606
ad142b9e
FP
607/*
608 * The default behavior of the Dualshock 4 is to send reports using report
077147a3
FP
609 * type 1 when running over Bluetooth. However, when feature report 2 is
610 * requested during the controller initialization it starts sending input
611 * reports in report 17. Since report 17 is undefined in the default HID
d829674d 612 * descriptor the button and axis definitions must be moved to report 17 or
077147a3 613 * the HID layer won't process the received input.
d829674d
FP
614 */
615static u8 dualshock4_bt_rdesc[] = {
616 0x05, 0x01, /* Usage Page (Desktop), */
617 0x09, 0x05, /* Usage (Gamepad), */
618 0xA1, 0x01, /* Collection (Application), */
619 0x85, 0x01, /* Report ID (1), */
620 0x75, 0x08, /* Report Size (8), */
621 0x95, 0x0A, /* Report Count (9), */
622 0x81, 0x02, /* Input (Variable), */
623 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
624 0x85, 0x02, /* Report ID (2), */
625 0x09, 0x24, /* Usage (24h), */
626 0x95, 0x24, /* Report Count (36), */
627 0xB1, 0x02, /* Feature (Variable), */
628 0x85, 0xA3, /* Report ID (163), */
629 0x09, 0x25, /* Usage (25h), */
630 0x95, 0x30, /* Report Count (48), */
631 0xB1, 0x02, /* Feature (Variable), */
632 0x85, 0x05, /* Report ID (5), */
633 0x09, 0x26, /* Usage (26h), */
634 0x95, 0x28, /* Report Count (40), */
635 0xB1, 0x02, /* Feature (Variable), */
636 0x85, 0x06, /* Report ID (6), */
637 0x09, 0x27, /* Usage (27h), */
638 0x95, 0x34, /* Report Count (52), */
639 0xB1, 0x02, /* Feature (Variable), */
640 0x85, 0x07, /* Report ID (7), */
641 0x09, 0x28, /* Usage (28h), */
642 0x95, 0x30, /* Report Count (48), */
643 0xB1, 0x02, /* Feature (Variable), */
644 0x85, 0x08, /* Report ID (8), */
645 0x09, 0x29, /* Usage (29h), */
646 0x95, 0x2F, /* Report Count (47), */
647 0xB1, 0x02, /* Feature (Variable), */
648 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
649 0x85, 0x03, /* Report ID (3), */
650 0x09, 0x21, /* Usage (21h), */
651 0x95, 0x26, /* Report Count (38), */
652 0xB1, 0x02, /* Feature (Variable), */
653 0x85, 0x04, /* Report ID (4), */
654 0x09, 0x22, /* Usage (22h), */
655 0x95, 0x2E, /* Report Count (46), */
656 0xB1, 0x02, /* Feature (Variable), */
657 0x85, 0xF0, /* Report ID (240), */
658 0x09, 0x47, /* Usage (47h), */
659 0x95, 0x3F, /* Report Count (63), */
660 0xB1, 0x02, /* Feature (Variable), */
661 0x85, 0xF1, /* Report ID (241), */
662 0x09, 0x48, /* Usage (48h), */
663 0x95, 0x3F, /* Report Count (63), */
664 0xB1, 0x02, /* Feature (Variable), */
665 0x85, 0xF2, /* Report ID (242), */
666 0x09, 0x49, /* Usage (49h), */
667 0x95, 0x0F, /* Report Count (15), */
668 0xB1, 0x02, /* Feature (Variable), */
669 0x85, 0x11, /* Report ID (17), */
670 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
671 0x09, 0x20, /* Usage (20h), */
672 0x95, 0x02, /* Report Count (2), */
673 0x81, 0x02, /* Input (Variable), */
674 0x05, 0x01, /* Usage Page (Desktop), */
675 0x09, 0x30, /* Usage (X), */
676 0x09, 0x31, /* Usage (Y), */
677 0x09, 0x32, /* Usage (Z), */
678 0x09, 0x35, /* Usage (Rz), */
679 0x15, 0x00, /* Logical Minimum (0), */
680 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
681 0x75, 0x08, /* Report Size (8), */
682 0x95, 0x04, /* Report Count (4), */
683 0x81, 0x02, /* Input (Variable), */
684 0x09, 0x39, /* Usage (Hat Switch), */
685 0x15, 0x00, /* Logical Minimum (0), */
686 0x25, 0x07, /* Logical Maximum (7), */
687 0x75, 0x04, /* Report Size (4), */
688 0x95, 0x01, /* Report Count (1), */
689 0x81, 0x42, /* Input (Variable, Null State), */
690 0x05, 0x09, /* Usage Page (Button), */
691 0x19, 0x01, /* Usage Minimum (01h), */
692 0x29, 0x0E, /* Usage Maximum (0Eh), */
693 0x15, 0x00, /* Logical Minimum (0), */
694 0x25, 0x01, /* Logical Maximum (1), */
695 0x75, 0x01, /* Report Size (1), */
696 0x95, 0x0E, /* Report Count (14), */
697 0x81, 0x02, /* Input (Variable), */
698 0x75, 0x06, /* Report Size (6), */
699 0x95, 0x01, /* Report Count (1), */
700 0x81, 0x01, /* Input (Constant), */
701 0x05, 0x01, /* Usage Page (Desktop), */
702 0x09, 0x33, /* Usage (Rx), */
703 0x09, 0x34, /* Usage (Ry), */
704 0x15, 0x00, /* Logical Minimum (0), */
705 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
706 0x75, 0x08, /* Report Size (8), */
707 0x95, 0x02, /* Report Count (2), */
708 0x81, 0x02, /* Input (Variable), */
709 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
710 0x09, 0x20, /* Usage (20h), */
711 0x95, 0x03, /* Report Count (3), */
712 0x81, 0x02, /* Input (Variable), */
713 0x05, 0x01, /* Usage Page (Desktop), */
714 0x19, 0x40, /* Usage Minimum (40h), */
715 0x29, 0x42, /* Usage Maximum (42h), */
716 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
bdae9e0e 717 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
d829674d
FP
718 0x75, 0x10, /* Report Size (16), */
719 0x95, 0x03, /* Report Count (3), */
720 0x81, 0x02, /* Input (Variable), */
721 0x19, 0x43, /* Usage Minimum (43h), */
722 0x29, 0x45, /* Usage Maximum (45h), */
bdae9e0e
RC
723 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
724 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
d829674d
FP
725 0x95, 0x03, /* Report Count (3), */
726 0x81, 0x02, /* Input (Variable), */
727 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
728 0x09, 0x20, /* Usage (20h), */
729 0x15, 0x00, /* Logical Minimum (0), */
730 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
731 0x75, 0x08, /* Report Size (8), */
732 0x95, 0x31, /* Report Count (51), */
733 0x81, 0x02, /* Input (Variable), */
734 0x09, 0x21, /* Usage (21h), */
735 0x75, 0x08, /* Report Size (8), */
736 0x95, 0x4D, /* Report Count (77), */
737 0x91, 0x02, /* Output (Variable), */
738 0x85, 0x12, /* Report ID (18), */
739 0x09, 0x22, /* Usage (22h), */
740 0x95, 0x8D, /* Report Count (141), */
741 0x81, 0x02, /* Input (Variable), */
742 0x09, 0x23, /* Usage (23h), */
743 0x91, 0x02, /* Output (Variable), */
744 0x85, 0x13, /* Report ID (19), */
745 0x09, 0x24, /* Usage (24h), */
746 0x95, 0xCD, /* Report Count (205), */
747 0x81, 0x02, /* Input (Variable), */
748 0x09, 0x25, /* Usage (25h), */
749 0x91, 0x02, /* Output (Variable), */
750 0x85, 0x14, /* Report ID (20), */
751 0x09, 0x26, /* Usage (26h), */
752 0x96, 0x0D, 0x01, /* Report Count (269), */
753 0x81, 0x02, /* Input (Variable), */
754 0x09, 0x27, /* Usage (27h), */
755 0x91, 0x02, /* Output (Variable), */
756 0x85, 0x15, /* Report ID (21), */
757 0x09, 0x28, /* Usage (28h), */
758 0x96, 0x4D, 0x01, /* Report Count (333), */
759 0x81, 0x02, /* Input (Variable), */
760 0x09, 0x29, /* Usage (29h), */
761 0x91, 0x02, /* Output (Variable), */
762 0x85, 0x16, /* Report ID (22), */
763 0x09, 0x2A, /* Usage (2Ah), */
764 0x96, 0x8D, 0x01, /* Report Count (397), */
765 0x81, 0x02, /* Input (Variable), */
766 0x09, 0x2B, /* Usage (2Bh), */
767 0x91, 0x02, /* Output (Variable), */
768 0x85, 0x17, /* Report ID (23), */
769 0x09, 0x2C, /* Usage (2Ch), */
770 0x96, 0xCD, 0x01, /* Report Count (461), */
771 0x81, 0x02, /* Input (Variable), */
772 0x09, 0x2D, /* Usage (2Dh), */
773 0x91, 0x02, /* Output (Variable), */
774 0x85, 0x18, /* Report ID (24), */
775 0x09, 0x2E, /* Usage (2Eh), */
776 0x96, 0x0D, 0x02, /* Report Count (525), */
777 0x81, 0x02, /* Input (Variable), */
778 0x09, 0x2F, /* Usage (2Fh), */
779 0x91, 0x02, /* Output (Variable), */
780 0x85, 0x19, /* Report ID (25), */
781 0x09, 0x30, /* Usage (30h), */
782 0x96, 0x22, 0x02, /* Report Count (546), */
783 0x81, 0x02, /* Input (Variable), */
784 0x09, 0x31, /* Usage (31h), */
785 0x91, 0x02, /* Output (Variable), */
786 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
787 0x85, 0x82, /* Report ID (130), */
788 0x09, 0x22, /* Usage (22h), */
789 0x95, 0x3F, /* Report Count (63), */
790 0xB1, 0x02, /* Feature (Variable), */
791 0x85, 0x83, /* Report ID (131), */
792 0x09, 0x23, /* Usage (23h), */
793 0xB1, 0x02, /* Feature (Variable), */
794 0x85, 0x84, /* Report ID (132), */
795 0x09, 0x24, /* Usage (24h), */
796 0xB1, 0x02, /* Feature (Variable), */
797 0x85, 0x90, /* Report ID (144), */
798 0x09, 0x30, /* Usage (30h), */
799 0xB1, 0x02, /* Feature (Variable), */
800 0x85, 0x91, /* Report ID (145), */
801 0x09, 0x31, /* Usage (31h), */
802 0xB1, 0x02, /* Feature (Variable), */
803 0x85, 0x92, /* Report ID (146), */
804 0x09, 0x32, /* Usage (32h), */
805 0xB1, 0x02, /* Feature (Variable), */
806 0x85, 0x93, /* Report ID (147), */
807 0x09, 0x33, /* Usage (33h), */
808 0xB1, 0x02, /* Feature (Variable), */
809 0x85, 0xA0, /* Report ID (160), */
810 0x09, 0x40, /* Usage (40h), */
811 0xB1, 0x02, /* Feature (Variable), */
812 0x85, 0xA4, /* Report ID (164), */
813 0x09, 0x44, /* Usage (44h), */
814 0xB1, 0x02, /* Feature (Variable), */
815 0xC0 /* End Collection */
816};
817
1adf904e 818static u8 ps3remote_rdesc[] = {
078328da
JK
819 0x05, 0x01, /* GUsagePage Generic Desktop */
820 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
821 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
822
823 /* Use collection 1 for joypad buttons */
824 0xA1, 0x02, /* MCollection Logical (interrelated data) */
825
ef916ef5
AO
826 /*
827 * Ignore the 1st byte, maybe it is used for a controller
828 * number but it's not needed for correct operation
829 */
078328da
JK
830 0x75, 0x08, /* GReportSize 0x08 [8] */
831 0x95, 0x01, /* GReportCount 0x01 [1] */
832 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
833
ef916ef5
AO
834 /*
835 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
836 * buttons multiple keypresses are allowed
837 */
078328da
JK
838 0x05, 0x09, /* GUsagePage Button */
839 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
840 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
841 0x14, /* GLogicalMinimum [0] */
842 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
843 0x75, 0x01, /* GReportSize 0x01 [1] */
844 0x95, 0x18, /* GReportCount 0x18 [24] */
845 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
846
847 0xC0, /* MEndCollection */
848
849 /* Use collection 2 for remote control buttons */
850 0xA1, 0x02, /* MCollection Logical (interrelated data) */
851
852 /* 5th byte is used for remote control buttons */
853 0x05, 0x09, /* GUsagePage Button */
854 0x18, /* LUsageMinimum [No button pressed] */
855 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
856 0x14, /* GLogicalMinimum [0] */
857 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
858 0x75, 0x08, /* GReportSize 0x08 [8] */
859 0x95, 0x01, /* GReportCount 0x01 [1] */
860 0x80, /* MInput */
861
ef916ef5
AO
862 /*
863 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
864 * 0xff and 11th is for press indication
865 */
078328da
JK
866 0x75, 0x08, /* GReportSize 0x08 [8] */
867 0x95, 0x06, /* GReportCount 0x06 [6] */
868 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
869
870 /* 12th byte is for battery strength */
871 0x05, 0x06, /* GUsagePage Generic Device Controls */
872 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
873 0x14, /* GLogicalMinimum [0] */
874 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
875 0x75, 0x08, /* GReportSize 0x08 [8] */
876 0x95, 0x01, /* GReportCount 0x01 [1] */
877 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
878
879 0xC0, /* MEndCollection */
880
881 0xC0 /* MEndCollection [Game Pad] */
882};
883
884static const unsigned int ps3remote_keymap_joypad_buttons[] = {
885 [0x01] = KEY_SELECT,
886 [0x02] = BTN_THUMBL, /* L3 */
887 [0x03] = BTN_THUMBR, /* R3 */
888 [0x04] = BTN_START,
889 [0x05] = KEY_UP,
890 [0x06] = KEY_RIGHT,
891 [0x07] = KEY_DOWN,
892 [0x08] = KEY_LEFT,
893 [0x09] = BTN_TL2, /* L2 */
894 [0x0a] = BTN_TR2, /* R2 */
895 [0x0b] = BTN_TL, /* L1 */
896 [0x0c] = BTN_TR, /* R1 */
897 [0x0d] = KEY_OPTION, /* options/triangle */
898 [0x0e] = KEY_BACK, /* back/circle */
899 [0x0f] = BTN_0, /* cross */
900 [0x10] = KEY_SCREEN, /* view/square */
901 [0x11] = KEY_HOMEPAGE, /* PS button */
902 [0x14] = KEY_ENTER,
903};
904static const unsigned int ps3remote_keymap_remote_buttons[] = {
905 [0x00] = KEY_1,
906 [0x01] = KEY_2,
907 [0x02] = KEY_3,
908 [0x03] = KEY_4,
909 [0x04] = KEY_5,
910 [0x05] = KEY_6,
911 [0x06] = KEY_7,
912 [0x07] = KEY_8,
913 [0x08] = KEY_9,
914 [0x09] = KEY_0,
915 [0x0e] = KEY_ESC, /* return */
916 [0x0f] = KEY_CLEAR,
917 [0x16] = KEY_EJECTCD,
918 [0x1a] = KEY_MENU, /* top menu */
919 [0x28] = KEY_TIME,
920 [0x30] = KEY_PREVIOUS,
921 [0x31] = KEY_NEXT,
922 [0x32] = KEY_PLAY,
923 [0x33] = KEY_REWIND, /* scan back */
924 [0x34] = KEY_FORWARD, /* scan forward */
925 [0x38] = KEY_STOP,
926 [0x39] = KEY_PAUSE,
927 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
928 [0x60] = KEY_FRAMEBACK, /* slow/step back */
929 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
930 [0x63] = KEY_SUBTITLE,
931 [0x64] = KEY_AUDIO,
932 [0x65] = KEY_ANGLE,
933 [0x70] = KEY_INFO, /* display */
934 [0x80] = KEY_BLUE,
935 [0x81] = KEY_RED,
936 [0x82] = KEY_GREEN,
937 [0x83] = KEY_YELLOW,
938};
939
f04d5140 940static const unsigned int buzz_keymap[] = {
ad142b9e
FP
941 /*
942 * The controller has 4 remote buzzers, each with one LED and 5
f04d5140 943 * buttons.
09593e38 944 *
f04d5140
CL
945 * We use the mapping chosen by the controller, which is:
946 *
947 * Key Offset
948 * -------------------
949 * Buzz 1
950 * Blue 5
951 * Orange 4
952 * Green 3
953 * Yellow 2
954 *
955 * So, for example, the orange button on the third buzzer is mapped to
956 * BTN_TRIGGER_HAPPY14
957 */
09593e38
AO
958 [1] = BTN_TRIGGER_HAPPY1,
959 [2] = BTN_TRIGGER_HAPPY2,
960 [3] = BTN_TRIGGER_HAPPY3,
961 [4] = BTN_TRIGGER_HAPPY4,
962 [5] = BTN_TRIGGER_HAPPY5,
963 [6] = BTN_TRIGGER_HAPPY6,
964 [7] = BTN_TRIGGER_HAPPY7,
965 [8] = BTN_TRIGGER_HAPPY8,
966 [9] = BTN_TRIGGER_HAPPY9,
f04d5140
CL
967 [10] = BTN_TRIGGER_HAPPY10,
968 [11] = BTN_TRIGGER_HAPPY11,
969 [12] = BTN_TRIGGER_HAPPY12,
970 [13] = BTN_TRIGGER_HAPPY13,
971 [14] = BTN_TRIGGER_HAPPY14,
972 [15] = BTN_TRIGGER_HAPPY15,
973 [16] = BTN_TRIGGER_HAPPY16,
974 [17] = BTN_TRIGGER_HAPPY17,
975 [18] = BTN_TRIGGER_HAPPY18,
976 [19] = BTN_TRIGGER_HAPPY19,
977 [20] = BTN_TRIGGER_HAPPY20,
978};
979
d902f472
FP
980static enum power_supply_property sony_battery_props[] = {
981 POWER_SUPPLY_PROP_PRESENT,
982 POWER_SUPPLY_PROP_CAPACITY,
983 POWER_SUPPLY_PROP_SCOPE,
984 POWER_SUPPLY_PROP_STATUS,
985};
986
55d3b664 987struct sixaxis_led {
1adf904e
PM
988 u8 time_enabled; /* the total time the led is active (0xff means forever) */
989 u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
990 u8 enabled;
991 u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
992 u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
55d3b664
FP
993} __packed;
994
995struct sixaxis_rumble {
1adf904e
PM
996 u8 padding;
997 u8 right_duration; /* Right motor duration (0xff means forever) */
998 u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
999 u8 left_duration; /* Left motor duration (0xff means forever) */
1000 u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
55d3b664
FP
1001} __packed;
1002
1003struct sixaxis_output_report {
1adf904e 1004 u8 report_id;
55d3b664 1005 struct sixaxis_rumble rumble;
1adf904e
PM
1006 u8 padding[4];
1007 u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
55d3b664
FP
1008 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
1009 struct sixaxis_led _reserved; /* LED5, not actually soldered */
1010} __packed;
1011
1012union sixaxis_output_report_01 {
1013 struct sixaxis_output_report data;
1adf904e 1014 u8 buf[36];
55d3b664
FP
1015};
1016
c5e0c1c4
FP
1017struct motion_output_report_02 {
1018 u8 type, zero;
1019 u8 r, g, b;
1020 u8 zero2;
1021 u8 rumble;
1022};
1023
2c159de0
RC
1024#define DS4_FEATURE_REPORT_0x02_SIZE 37
1025#define DS4_FEATURE_REPORT_0x81_SIZE 7
49b9ca6c 1026#define DS4_INPUT_REPORT_0x11_SIZE 78
2c159de0
RC
1027#define DS4_OUTPUT_REPORT_0x05_SIZE 32
1028#define DS4_OUTPUT_REPORT_0x11_SIZE 78
29b691a8 1029#define SIXAXIS_REPORT_0xF2_SIZE 17
a85d67b5 1030#define SIXAXIS_REPORT_0xF5_SIZE 8
41d2d425 1031#define MOTION_REPORT_0x02_SIZE 49
9b2b5c9a 1032
cdc1c021
RC
1033/* Offsets relative to USB input report (0x1). Bluetooth (0x11) requires an
1034 * additional +2.
1035 */
1036#define DS4_INPUT_REPORT_BATTERY_OFFSET 30
1037#define DS4_INPUT_REPORT_TOUCHPAD_OFFSET 33
1038
8b402c92 1039static DEFINE_SPINLOCK(sony_dev_list_lock);
d2d782fc 1040static LIST_HEAD(sony_device_list);
8025087a 1041static DEFINE_IDA(sony_device_id_allocator);
d2d782fc 1042
cc6e0bbb 1043struct sony_sc {
d902f472 1044 spinlock_t lock;
d2d782fc 1045 struct list_head list_node;
0a286ef2 1046 struct hid_device *hdev;
60781cf4 1047 struct led_classdev *leds[MAX_LEDS];
cc6e0bbb 1048 unsigned long quirks;
0a286ef2 1049 struct work_struct state_worker;
09593e38 1050 void (*send_output_report)(struct sony_sc *);
297d716f
KK
1051 struct power_supply *battery;
1052 struct power_supply_desc battery_desc;
8025087a 1053 int device_id;
1adf904e 1054 u8 *output_report_dmabuf;
f04d5140 1055
9f323b68 1056#ifdef CONFIG_SONY_FF
1adf904e
PM
1057 u8 left;
1058 u8 right;
9f323b68
SE
1059#endif
1060
1adf904e
PM
1061 u8 mac_address[6];
1062 u8 worker_initialized;
2a242932 1063 u8 defer_initialization;
1adf904e
PM
1064 u8 cable_state;
1065 u8 battery_charging;
1066 u8 battery_capacity;
1067 u8 led_state[MAX_LEDS];
1068 u8 resume_led_state[MAX_LEDS];
1069 u8 led_delay_on[MAX_LEDS];
1070 u8 led_delay_off[MAX_LEDS];
1071 u8 led_count;
cc6e0bbb
JK
1072};
1073
2a242932
FP
1074static inline void sony_schedule_work(struct sony_sc *sc)
1075{
1076 if (!sc->defer_initialization)
1077 schedule_work(&sc->state_worker);
1078}
1079
1adf904e 1080static u8 *sixaxis_fixup(struct hid_device *hdev, u8 *rdesc,
c607fb8d
AO
1081 unsigned int *rsize)
1082{
1083 *rsize = sizeof(sixaxis_rdesc);
1084 return sixaxis_rdesc;
1085}
1086
c5e0c1c4
FP
1087static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
1088 unsigned int *rsize)
1089{
1090 *rsize = sizeof(motion_rdesc);
1091 return motion_rdesc;
1092}
1093
b2723eb7
SW
1094static u8 *navigation_fixup(struct hid_device *hdev, u8 *rdesc,
1095 unsigned int *rsize)
1096{
1097 *rsize = sizeof(navigation_rdesc);
1098 return navigation_rdesc;
1099}
1100
1adf904e 1101static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
078328da
JK
1102 unsigned int *rsize)
1103{
1104 *rsize = sizeof(ps3remote_rdesc);
1105 return ps3remote_rdesc;
1106}
1107
1108static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
1109 struct hid_field *field, struct hid_usage *usage,
1110 unsigned long **bit, int *max)
1111{
1112 unsigned int key = usage->hid & HID_USAGE;
1113
1114 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1115 return -1;
1116
1117 switch (usage->collection_index) {
1118 case 1:
1119 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
1120 return -1;
1121
1122 key = ps3remote_keymap_joypad_buttons[key];
1123 if (!key)
1124 return -1;
1125 break;
1126 case 2:
1127 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
1128 return -1;
1129
1130 key = ps3remote_keymap_remote_buttons[key];
1131 if (!key)
1132 return -1;
1133 break;
1134 default:
1135 return -1;
1136 }
1137
1138 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1139 return 1;
1140}
1141
1adf904e 1142static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
73e4008d 1143 unsigned int *rsize)
cc6e0bbb
JK
1144{
1145 struct sony_sc *sc = hid_get_drvdata(hdev);
1146
4ba1eeeb 1147 if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
74500cc8
SM
1148 return rdesc;
1149
99d24902
FLVC
1150 /*
1151 * Some Sony RF receivers wrongly declare the mouse pointer as a
1152 * a constant non-data variable.
1153 */
1154 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1155 /* usage page: generic desktop controls */
1156 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1157 /* usage: mouse */
1158 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1159 /* input (usage page for x,y axes): constant, variable, relative */
1160 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
a4649184 1161 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
99d24902 1162 /* input: data, variable, relative */
cc6e0bbb
JK
1163 rdesc[55] = 0x06;
1164 }
61ab44be 1165
ed19d8cf
FP
1166 /*
1167 * The default Dualshock 4 USB descriptor doesn't assign
1168 * the gyroscope values to corresponding axes so we need a
1169 * modified one.
1170 */
b71b5578 1171 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
ed19d8cf
FP
1172 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1173 rdesc = dualshock4_usb_rdesc;
1174 *rsize = sizeof(dualshock4_usb_rdesc);
b71b5578 1175 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
d829674d
FP
1176 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1177 rdesc = dualshock4_bt_rdesc;
1178 *rsize = sizeof(dualshock4_bt_rdesc);
ed19d8cf
FP
1179 }
1180
c607fb8d
AO
1181 if (sc->quirks & SIXAXIS_CONTROLLER)
1182 return sixaxis_fixup(hdev, rdesc, rsize);
078328da 1183
c5e0c1c4
FP
1184 if (sc->quirks & MOTION_CONTROLLER)
1185 return motion_fixup(hdev, rdesc, rsize);
1186
4545ee0a 1187 if (sc->quirks & NAVIGATION_CONTROLLER)
b2723eb7 1188 return navigation_fixup(hdev, rdesc, rsize);
4545ee0a 1189
078328da
JK
1190 if (sc->quirks & PS3REMOTE)
1191 return ps3remote_fixup(hdev, rdesc, rsize);
1192
73e4008d 1193 return rdesc;
cc6e0bbb
JK
1194}
1195
1adf904e 1196static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
d902f472 1197{
1adf904e 1198 static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
d902f472 1199 unsigned long flags;
12e9a6d7 1200 int offset;
1adf904e 1201 u8 cable_state, battery_capacity, battery_charging;
d902f472 1202
ad142b9e
FP
1203 /*
1204 * The sixaxis is charging if the battery value is 0xee
d902f472
FP
1205 * and it is fully charged if the value is 0xef.
1206 * It does not report the actual level while charging so it
1207 * is set to 100% while charging is in progress.
1208 */
12e9a6d7
SW
1209 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1210
1211 if (rd[offset] >= 0xee) {
d902f472 1212 battery_capacity = 100;
12e9a6d7 1213 battery_charging = !(rd[offset] & 0x01);
9fddd74a 1214 cable_state = 1;
d902f472 1215 } else {
1adf904e 1216 u8 index = rd[offset] <= 5 ? rd[offset] : 5;
ac3c9a94 1217 battery_capacity = sixaxis_battery_capacity[index];
d902f472 1218 battery_charging = 0;
9fddd74a 1219 cable_state = 0;
d902f472 1220 }
d902f472
FP
1221
1222 spin_lock_irqsave(&sc->lock, flags);
1223 sc->cable_state = cable_state;
1224 sc->battery_capacity = battery_capacity;
1225 sc->battery_charging = battery_charging;
1226 spin_unlock_irqrestore(&sc->lock, flags);
1227}
1228
1adf904e 1229static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
d902f472 1230{
e5606230
FP
1231 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1232 struct hid_input, list);
1233 struct input_dev *input_dev = hidinput->input;
d902f472 1234 unsigned long flags;
cdc1c021 1235 int n, m, offset, num_touch_data, max_touch_data;
1adf904e 1236 u8 cable_state, battery_capacity, battery_charging;
d902f472 1237
cdc1c021
RC
1238 /* When using Bluetooth the header is 2 bytes longer, so skip these. */
1239 int data_offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 0 : 2;
6c5f860d 1240
ad142b9e 1241 /*
cdc1c021 1242 * The lower 4 bits of byte 30 (or 32 for BT) contain the battery level
d902f472
FP
1243 * and the 5th bit contains the USB cable state.
1244 */
cdc1c021 1245 offset = data_offset + DS4_INPUT_REPORT_BATTERY_OFFSET;
6c5f860d
FP
1246 cable_state = (rd[offset] >> 4) & 0x01;
1247 battery_capacity = rd[offset] & 0x0F;
d902f472 1248
ad142b9e
FP
1249 /*
1250 * When a USB power source is connected the battery level ranges from
6c5f860d
FP
1251 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1252 * A battery level above 10 when plugged in means charge completed.
d902f472 1253 */
6c5f860d 1254 if (!cable_state || battery_capacity > 10)
d902f472
FP
1255 battery_charging = 0;
1256 else
1257 battery_charging = 1;
1258
6c5f860d
FP
1259 if (!cable_state)
1260 battery_capacity++;
d902f472 1261 if (battery_capacity > 10)
6c5f860d
FP
1262 battery_capacity = 10;
1263
d902f472
FP
1264 battery_capacity *= 10;
1265
1266 spin_lock_irqsave(&sc->lock, flags);
1267 sc->cable_state = cable_state;
1268 sc->battery_capacity = battery_capacity;
1269 sc->battery_charging = battery_charging;
1270 spin_unlock_irqrestore(&sc->lock, flags);
e5606230 1271
ad142b9e 1272 /*
cdc1c021
RC
1273 * The Dualshock 4 multi-touch trackpad data starts at offset 33 on USB
1274 * and 35 on Bluetooth.
1275 * The first byte indicates the number of touch data in the report.
1276 * Trackpad data starts 2 bytes later (e.g. 35 for USB).
e5606230 1277 */
cdc1c021
RC
1278 offset = data_offset + DS4_INPUT_REPORT_TOUCHPAD_OFFSET;
1279 max_touch_data = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 3 : 4;
1280 if (rd[offset] > 0 && rd[offset] <= max_touch_data)
1281 num_touch_data = rd[offset];
1282 else
1283 num_touch_data = 1;
1284 offset += 1;
e5606230 1285
cdc1c021
RC
1286 for (m = 0; m < num_touch_data; m++) {
1287 /* Skip past timestamp */
1288 offset += 1;
e5606230 1289
cdc1c021
RC
1290 /*
1291 * The first 7 bits of the first byte is a counter and bit 8 is
1292 * a touch indicator that is 0 when pressed and 1 when not
1293 * pressed.
1294 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1295 * The data for the second touch is in the same format and
1296 * immediately follows the data for the first.
1297 */
1298 for (n = 0; n < 2; n++) {
1299 u16 x, y;
1300 bool active;
1301
1302 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1303 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1304
1305 active = !(rd[offset] >> 7);
1306 input_mt_slot(input_dev, n);
1307 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, active);
1308
1309 if (active) {
1310 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1311 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1312 }
1313
1314 offset += 4;
1315 }
1316 input_mt_sync_frame(input_dev);
1317 input_sync(input_dev);
e5606230 1318 }
d902f472
FP
1319}
1320
c9e4d877 1321static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1adf904e 1322 u8 *rd, int size)
c9e4d877
SW
1323{
1324 struct sony_sc *sc = hid_get_drvdata(hdev);
1325
ad142b9e
FP
1326 /*
1327 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
c9e4d877
SW
1328 * has to be BYTE_SWAPPED before passing up to joystick interface
1329 */
fee4e2d5 1330 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
8f5f0bc2
FP
1331 /*
1332 * When connected via Bluetooth the Sixaxis occasionally sends
1333 * a report with the second byte 0xff and the rest zeroed.
1334 *
1335 * This report does not reflect the actual state of the
1336 * controller must be ignored to avoid generating false input
1337 * events.
1338 */
1339 if (rd[1] == 0xff)
1340 return -EINVAL;
1341
c9e4d877
SW
1342 swap(rd[41], rd[42]);
1343 swap(rd[43], rd[44]);
1344 swap(rd[45], rd[46]);
1345 swap(rd[47], rd[48]);
d902f472 1346
12e9a6d7
SW
1347 sixaxis_parse_report(sc, rd, size);
1348 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
d902f472 1349 sixaxis_parse_report(sc, rd, size);
4545ee0a
SW
1350 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1351 size == 49) {
1352 sixaxis_parse_report(sc, rd, size);
68330d83
FP
1353 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1354 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1355 && rd[0] == 0x11 && size == 78)) {
49b9ca6c
RC
1356 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
1357 /* CRC check */
1358 u8 bthdr = 0xA1;
1359 u32 crc;
1360 u32 report_crc;
1361
1362 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1363 crc = ~crc32_le(crc, rd, DS4_INPUT_REPORT_0x11_SIZE-4);
1364 report_crc = get_unaligned_le32(&rd[DS4_INPUT_REPORT_0x11_SIZE-4]);
1365 if (crc != report_crc) {
1366 hid_dbg(sc->hdev, "DualShock 4 input report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1367 report_crc, crc);
1368 return -EILSEQ;
1369 }
1370 }
d902f472 1371 dualshock4_parse_report(sc, rd, size);
c9e4d877
SW
1372 }
1373
2a242932
FP
1374 if (sc->defer_initialization) {
1375 sc->defer_initialization = 0;
1376 sony_schedule_work(sc);
1377 }
1378
c9e4d877
SW
1379 return 0;
1380}
1381
f04d5140
CL
1382static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1383 struct hid_field *field, struct hid_usage *usage,
1384 unsigned long **bit, int *max)
1385{
1386 struct sony_sc *sc = hid_get_drvdata(hdev);
1387
1388 if (sc->quirks & BUZZ_CONTROLLER) {
1389 unsigned int key = usage->hid & HID_USAGE;
1390
1391 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1392 return -1;
1393
1394 switch (usage->collection_index) {
1395 case 1:
1396 if (key >= ARRAY_SIZE(buzz_keymap))
1397 return -1;
1398
1399 key = buzz_keymap[key];
1400 if (!key)
1401 return -1;
1402 break;
1403 default:
1404 return -1;
1405 }
1406
1407 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1408 return 1;
1409 }
1410
078328da
JK
1411 if (sc->quirks & PS3REMOTE)
1412 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1413
6f498018
BT
1414 /* Let hid-core decide for the others */
1415 return 0;
f04d5140
CL
1416}
1417
ce8efc3b
FP
1418static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1419 int w, int h)
1420{
1421 struct input_dev *input_dev = hi->input;
1422 int ret;
1423
1424 ret = input_mt_init_slots(input_dev, touch_count, 0);
1425 if (ret < 0)
1426 return ret;
1427
1428 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1429 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1430
1431 return 0;
1432}
1433
ce8efc3b 1434
bd28ce00
JS
1435/*
1436 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1437 * to "operational". Without this, the ps3 controller will not report any
1438 * events.
1439 */
816651a7 1440static int sixaxis_set_operational_usb(struct hid_device *hdev)
bd28ce00 1441{
a85d67b5
AO
1442 const int buf_size =
1443 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
1adf904e 1444 u8 *buf;
bd28ce00 1445 int ret;
bd28ce00 1446
2e701a35 1447 buf = kmalloc(buf_size, GFP_KERNEL);
bd28ce00
JS
1448 if (!buf)
1449 return -ENOMEM;
1450
a85d67b5
AO
1451 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1452 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
a7de9b86
LK
1453 if (ret < 0) {
1454 hid_err(hdev, "can't set operational mode: step 1\n");
1455 goto out;
1456 }
f204828a 1457
a7de9b86
LK
1458 /*
1459 * Some compatible controllers like the Speedlink Strike FX and
1460 * Gasia need another query plus an USB interrupt to get operational.
1461 */
a85d67b5
AO
1462 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1463 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
a7de9b86
LK
1464 if (ret < 0) {
1465 hid_err(hdev, "can't set operational mode: step 2\n");
1466 goto out;
1467 }
f204828a 1468
a7de9b86 1469 ret = hid_hw_output_report(hdev, buf, 1);
19f4c2ba
BT
1470 if (ret < 0) {
1471 hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
1472 ret = 0;
1473 }
bd28ce00 1474
a7de9b86 1475out:
bd28ce00
JS
1476 kfree(buf);
1477
1478 return ret;
1479}
1480
816651a7 1481static int sixaxis_set_operational_bt(struct hid_device *hdev)
f9ce7c28 1482{
1adf904e
PM
1483 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1484 u8 *buf;
9b2b5c9a
FP
1485 int ret;
1486
1487 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1488 if (!buf)
1489 return -ENOMEM;
1490
1491 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
b0dd72aa 1492 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
9b2b5c9a
FP
1493
1494 kfree(buf);
1495
1496 return ret;
f9ce7c28
BN
1497}
1498
ad142b9e
FP
1499/*
1500 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
68330d83
FP
1501 * controller so that it sends full input reports of type 0x11.
1502 */
1503static int dualshock4_set_operational_bt(struct hid_device *hdev)
1504{
1adf904e 1505 u8 *buf;
9b2b5c9a 1506 int ret;
68330d83 1507
2c159de0 1508 buf = kmalloc(DS4_FEATURE_REPORT_0x02_SIZE, GFP_KERNEL);
9b2b5c9a
FP
1509 if (!buf)
1510 return -ENOMEM;
1511
2c159de0 1512 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_FEATURE_REPORT_0x02_SIZE,
68330d83 1513 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
9b2b5c9a
FP
1514
1515 kfree(buf);
1516
1517 return ret;
f9ce7c28
BN
1518}
1519
221399b3 1520static void sixaxis_set_leds_from_id(struct sony_sc *sc)
8025087a 1521{
1adf904e 1522 static const u8 sixaxis_leds[10][4] = {
8025087a
FP
1523 { 0x01, 0x00, 0x00, 0x00 },
1524 { 0x00, 0x01, 0x00, 0x00 },
1525 { 0x00, 0x00, 0x01, 0x00 },
1526 { 0x00, 0x00, 0x00, 0x01 },
1527 { 0x01, 0x00, 0x00, 0x01 },
1528 { 0x00, 0x01, 0x00, 0x01 },
1529 { 0x00, 0x00, 0x01, 0x01 },
1530 { 0x01, 0x00, 0x01, 0x01 },
1531 { 0x00, 0x01, 0x01, 0x01 },
1532 { 0x01, 0x01, 0x01, 0x01 }
1533 };
1534
221399b3
FP
1535 int id = sc->device_id;
1536
1537 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
8025087a
FP
1538
1539 if (id < 0)
1540 return;
1541
1542 id %= 10;
221399b3 1543 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
8025087a
FP
1544}
1545
221399b3 1546static void dualshock4_set_leds_from_id(struct sony_sc *sc)
8025087a
FP
1547{
1548 /* The first 4 color/index entries match what the PS4 assigns */
1adf904e 1549 static const u8 color_code[7][3] = {
8025087a
FP
1550 /* Blue */ { 0x00, 0x00, 0x01 },
1551 /* Red */ { 0x01, 0x00, 0x00 },
1552 /* Green */ { 0x00, 0x01, 0x00 },
1553 /* Pink */ { 0x02, 0x00, 0x01 },
1554 /* Orange */ { 0x02, 0x01, 0x00 },
1555 /* Teal */ { 0x00, 0x01, 0x01 },
1556 /* White */ { 0x01, 0x01, 0x01 }
1557 };
1558
221399b3
FP
1559 int id = sc->device_id;
1560
1561 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
8025087a
FP
1562
1563 if (id < 0)
1564 return;
1565
1566 id %= 7;
221399b3 1567 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
8025087a
FP
1568}
1569
221399b3 1570static void buzz_set_leds(struct sony_sc *sc)
f04d5140 1571{
221399b3 1572 struct hid_device *hdev = sc->hdev;
f04d5140
CL
1573 struct list_head *report_list =
1574 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1575 struct hid_report *report = list_entry(report_list->next,
1576 struct hid_report, list);
1adf904e 1577 s32 *value = report->field[0]->value;
f04d5140 1578
221399b3
FP
1579 BUILD_BUG_ON(MAX_LEDS < 4);
1580
f04d5140 1581 value[0] = 0x00;
221399b3
FP
1582 value[1] = sc->led_state[0] ? 0xff : 0x00;
1583 value[2] = sc->led_state[1] ? 0xff : 0x00;
1584 value[3] = sc->led_state[2] ? 0xff : 0x00;
1585 value[4] = sc->led_state[3] ? 0xff : 0x00;
f04d5140
CL
1586 value[5] = 0x00;
1587 value[6] = 0x00;
1588 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1589}
1590
221399b3 1591static void sony_set_leds(struct sony_sc *sc)
0a286ef2 1592{
221399b3 1593 if (!(sc->quirks & BUZZ_CONTROLLER))
2a242932 1594 sony_schedule_work(sc);
221399b3
FP
1595 else
1596 buzz_set_leds(sc);
0a286ef2
SE
1597}
1598
c5382519 1599static void sony_led_set_brightness(struct led_classdev *led,
f04d5140
CL
1600 enum led_brightness value)
1601{
1602 struct device *dev = led->dev->parent;
ee79a8f8 1603 struct hid_device *hdev = to_hid_device(dev);
f04d5140 1604 struct sony_sc *drv_data;
f04d5140
CL
1605
1606 int n;
b3ed458c 1607 int force_update;
f04d5140
CL
1608
1609 drv_data = hid_get_drvdata(hdev);
2251b85f 1610 if (!drv_data) {
f04d5140
CL
1611 hid_err(hdev, "No device data\n");
1612 return;
1613 }
f04d5140 1614
b3ed458c
FP
1615 /*
1616 * The Sixaxis on USB will override any LED settings sent to it
1617 * and keep flashing all of the LEDs until the PS button is pressed.
1618 * Updates, even if redundant, must be always be sent to the
1619 * controller to avoid having to toggle the state of an LED just to
1620 * stop the flashing later on.
1621 */
1622 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1623
60781cf4 1624 for (n = 0; n < drv_data->led_count; n++) {
b3ed458c
FP
1625 if (led == drv_data->leds[n] && (force_update ||
1626 (value != drv_data->led_state[n] ||
1627 drv_data->led_delay_on[n] ||
1628 drv_data->led_delay_off[n]))) {
1629
1630 drv_data->led_state[n] = value;
1631
1632 /* Setting the brightness stops the blinking */
1633 drv_data->led_delay_on[n] = 0;
1634 drv_data->led_delay_off[n] = 0;
1635
221399b3 1636 sony_set_leds(drv_data);
f04d5140
CL
1637 break;
1638 }
1639 }
1640}
1641
c5382519 1642static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
f04d5140
CL
1643{
1644 struct device *dev = led->dev->parent;
ee79a8f8 1645 struct hid_device *hdev = to_hid_device(dev);
f04d5140 1646 struct sony_sc *drv_data;
f04d5140
CL
1647
1648 int n;
f04d5140
CL
1649
1650 drv_data = hid_get_drvdata(hdev);
2251b85f 1651 if (!drv_data) {
f04d5140
CL
1652 hid_err(hdev, "No device data\n");
1653 return LED_OFF;
1654 }
f04d5140 1655
60781cf4 1656 for (n = 0; n < drv_data->led_count; n++) {
7db7504a
SW
1657 if (led == drv_data->leds[n])
1658 return drv_data->led_state[n];
f04d5140
CL
1659 }
1660
7db7504a 1661 return LED_OFF;
f04d5140 1662}
f04d5140 1663
b3ed458c
FP
1664static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1665 unsigned long *delay_off)
1666{
1667 struct device *dev = led->dev->parent;
ee79a8f8 1668 struct hid_device *hdev = to_hid_device(dev);
b3ed458c
FP
1669 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1670 int n;
1adf904e 1671 u8 new_on, new_off;
b3ed458c
FP
1672
1673 if (!drv_data) {
1674 hid_err(hdev, "No device data\n");
1675 return -EINVAL;
1676 }
1677
1678 /* Max delay is 255 deciseconds or 2550 milliseconds */
1679 if (*delay_on > 2550)
1680 *delay_on = 2550;
1681 if (*delay_off > 2550)
1682 *delay_off = 2550;
1683
1684 /* Blink at 1 Hz if both values are zero */
1685 if (!*delay_on && !*delay_off)
1686 *delay_on = *delay_off = 500;
1687
1688 new_on = *delay_on / 10;
1689 new_off = *delay_off / 10;
1690
1691 for (n = 0; n < drv_data->led_count; n++) {
1692 if (led == drv_data->leds[n])
1693 break;
1694 }
1695
1696 /* This LED is not registered on this device */
1697 if (n >= drv_data->led_count)
1698 return -EINVAL;
1699
1700 /* Don't schedule work if the values didn't change */
1701 if (new_on != drv_data->led_delay_on[n] ||
1702 new_off != drv_data->led_delay_off[n]) {
1703 drv_data->led_delay_on[n] = new_on;
1704 drv_data->led_delay_off[n] = new_off;
2a242932 1705 sony_schedule_work(drv_data);
b3ed458c
FP
1706 }
1707
1708 return 0;
1709}
1710
fa57a810 1711static void sony_leds_remove(struct sony_sc *sc)
0a286ef2 1712{
0a286ef2
SE
1713 struct led_classdev *led;
1714 int n;
1715
fa57a810 1716 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
0a286ef2 1717
fa57a810
FP
1718 for (n = 0; n < sc->led_count; n++) {
1719 led = sc->leds[n];
1720 sc->leds[n] = NULL;
0a286ef2
SE
1721 if (!led)
1722 continue;
1723 led_classdev_unregister(led);
1724 kfree(led);
1725 }
60781cf4 1726
fa57a810 1727 sc->led_count = 0;
0a286ef2
SE
1728}
1729
fa57a810 1730static int sony_leds_init(struct sony_sc *sc)
f04d5140 1731{
fa57a810 1732 struct hid_device *hdev = sc->hdev;
40e32ee6 1733 int n, ret = 0;
b3ed458c 1734 int use_ds4_names;
40e32ee6
JK
1735 struct led_classdev *led;
1736 size_t name_sz;
1737 char *name;
0a286ef2
SE
1738 size_t name_len;
1739 const char *name_fmt;
b3ed458c
FP
1740 static const char * const ds4_name_str[] = { "red", "green", "blue",
1741 "global" };
1adf904e
PM
1742 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1743 u8 use_hw_blink[MAX_LEDS] = { 0 };
f04d5140 1744
fa57a810 1745 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
0a286ef2 1746
fa57a810
FP
1747 if (sc->quirks & BUZZ_CONTROLLER) {
1748 sc->led_count = 4;
b3ed458c 1749 use_ds4_names = 0;
0a286ef2
SE
1750 name_len = strlen("::buzz#");
1751 name_fmt = "%s::buzz%d";
1752 /* Validate expected report characteristics. */
1753 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1754 return -ENODEV;
fa57a810 1755 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
221399b3
FP
1756 dualshock4_set_leds_from_id(sc);
1757 sc->led_state[3] = 1;
b3ed458c
FP
1758 sc->led_count = 4;
1759 memset(max_brightness, 255, 3);
1760 use_hw_blink[3] = 1;
1761 use_ds4_names = 1;
61ebca93
FP
1762 name_len = 0;
1763 name_fmt = "%s:%s";
c5e0c1c4
FP
1764 } else if (sc->quirks & MOTION_CONTROLLER) {
1765 sc->led_count = 3;
1766 memset(max_brightness, 255, 3);
1767 use_ds4_names = 1;
1768 name_len = 0;
1769 name_fmt = "%s:%s";
4545ee0a 1770 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
1adf904e 1771 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
4545ee0a
SW
1772
1773 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1774 sc->led_count = 1;
1775 memset(use_hw_blink, 1, 4);
1776 use_ds4_names = 0;
1777 name_len = strlen("::sony#");
1778 name_fmt = "%s::sony%d";
60781cf4 1779 } else {
221399b3 1780 sixaxis_set_leds_from_id(sc);
fa57a810 1781 sc->led_count = 4;
b3ed458c
FP
1782 memset(use_hw_blink, 1, 4);
1783 use_ds4_names = 0;
61ebca93
FP
1784 name_len = strlen("::sony#");
1785 name_fmt = "%s::sony%d";
60781cf4
FP
1786 }
1787
ad142b9e
FP
1788 /*
1789 * Clear LEDs as we have no way of reading their initial state. This is
f04d5140 1790 * only relevant if the driver is loaded after somebody actively set the
ad142b9e
FP
1791 * LEDs to on
1792 */
221399b3 1793 sony_set_leds(sc);
f04d5140 1794
0a286ef2 1795 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
f04d5140 1796
fa57a810 1797 for (n = 0; n < sc->led_count; n++) {
61ebca93 1798
b3ed458c
FP
1799 if (use_ds4_names)
1800 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
61ebca93 1801
40e32ee6
JK
1802 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1803 if (!led) {
1804 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
8cd5fcda 1805 ret = -ENOMEM;
40e32ee6
JK
1806 goto error_leds;
1807 }
f04d5140 1808
40e32ee6 1809 name = (void *)(&led[1]);
b3ed458c
FP
1810 if (use_ds4_names)
1811 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1812 ds4_name_str[n]);
61ebca93
FP
1813 else
1814 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
40e32ee6 1815 led->name = name;
221399b3 1816 led->brightness = sc->led_state[n];
b3ed458c 1817 led->max_brightness = max_brightness[n];
c5382519
SE
1818 led->brightness_get = sony_led_get_brightness;
1819 led->brightness_set = sony_led_set_brightness;
f04d5140 1820
b3ed458c
FP
1821 if (use_hw_blink[n])
1822 led->blink_set = sony_led_blink_set;
1823
8025087a
FP
1824 sc->leds[n] = led;
1825
8cd5fcda
JL
1826 ret = led_classdev_register(&hdev->dev, led);
1827 if (ret) {
40e32ee6 1828 hid_err(hdev, "Failed to register LED %d\n", n);
8025087a 1829 sc->leds[n] = NULL;
40e32ee6
JK
1830 kfree(led);
1831 goto error_leds;
f04d5140
CL
1832 }
1833 }
f04d5140
CL
1834
1835 return ret;
1836
f04d5140 1837error_leds:
fa57a810 1838 sony_leds_remove(sc);
f04d5140 1839
f04d5140 1840 return ret;
f04d5140
CL
1841}
1842
d8aaccda 1843static void sixaxis_send_output_report(struct sony_sc *sc)
a08c22c0 1844{
9b2b5c9a 1845 static const union sixaxis_output_report_01 default_report = {
55d3b664
FP
1846 .buf = {
1847 0x01,
ad07b7a6 1848 0x01, 0xff, 0x00, 0xff, 0x00,
55d3b664
FP
1849 0x00, 0x00, 0x00, 0x00, 0x00,
1850 0xff, 0x27, 0x10, 0x00, 0x32,
1851 0xff, 0x27, 0x10, 0x00, 0x32,
1852 0xff, 0x27, 0x10, 0x00, 0x32,
1853 0xff, 0x27, 0x10, 0x00, 0x32,
1854 0x00, 0x00, 0x00, 0x00, 0x00
1855 }
a08c22c0 1856 };
9b2b5c9a
FP
1857 struct sixaxis_output_report *report =
1858 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1859 int n;
1860
1861 /* Initialize the report with default values */
1862 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
9f323b68 1863
0a286ef2 1864#ifdef CONFIG_SONY_FF
9b2b5c9a
FP
1865 report->rumble.right_motor_on = sc->right ? 1 : 0;
1866 report->rumble.left_motor_force = sc->left;
0a286ef2
SE
1867#endif
1868
9b2b5c9a
FP
1869 report->leds_bitmap |= sc->led_state[0] << 1;
1870 report->leds_bitmap |= sc->led_state[1] << 2;
1871 report->leds_bitmap |= sc->led_state[2] << 3;
1872 report->leds_bitmap |= sc->led_state[3] << 4;
9f323b68 1873
88f6576f 1874 /* Set flag for all leds off, required for 3rd party INTEC controller */
9b2b5c9a
FP
1875 if ((report->leds_bitmap & 0x1E) == 0)
1876 report->leds_bitmap |= 0x20;
88f6576f 1877
b3ed458c
FP
1878 /*
1879 * The LEDs in the report are indexed in reverse order to their
1880 * corresponding light on the controller.
1881 * Index 0 = LED 4, index 1 = LED 3, etc...
1882 *
1883 * In the case of both delay values being zero (blinking disabled) the
1884 * default report values should be used or the controller LED will be
1885 * always off.
1886 */
1887 for (n = 0; n < 4; n++) {
1888 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
9b2b5c9a
FP
1889 report->led[3 - n].duty_off = sc->led_delay_off[n];
1890 report->led[3 - n].duty_on = sc->led_delay_on[n];
b3ed458c
FP
1891 }
1892 }
1893
1adf904e 1894 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
9b2b5c9a
FP
1895 sizeof(struct sixaxis_output_report),
1896 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
9f323b68
SE
1897}
1898
d8aaccda 1899static void dualshock4_send_output_report(struct sony_sc *sc)
0bd88dd3 1900{
0da8ea65 1901 struct hid_device *hdev = sc->hdev;
1adf904e 1902 u8 *buf = sc->output_report_dmabuf;
48220237
FP
1903 int offset;
1904
c4425c8f
FP
1905 /*
1906 * NOTE: The buf[1] field of the Bluetooth report controls
1907 * the Dualshock 4 reporting rate.
1908 *
1909 * Known values include:
1910 *
1911 * 0x80 - 1000hz (full speed)
1912 * 0xA0 - 31hz
1913 * 0xB0 - 20hz
1914 * 0xD0 - 66hz
1915 */
fdcf105d 1916 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
2c159de0 1917 memset(buf, 0, DS4_OUTPUT_REPORT_0x05_SIZE);
fdcf105d 1918 buf[0] = 0x05;
b3ed458c 1919 buf[1] = 0xFF;
fdcf105d
FP
1920 offset = 4;
1921 } else {
2c159de0 1922 memset(buf, 0, DS4_OUTPUT_REPORT_0x11_SIZE);
fdcf105d 1923 buf[0] = 0x11;
e7ef53ad 1924 buf[1] = 0xC0; /* HID + CRC */
fdcf105d
FP
1925 buf[3] = 0x0F;
1926 offset = 6;
1927 }
0bd88dd3
FP
1928
1929#ifdef CONFIG_SONY_FF
48220237
FP
1930 buf[offset++] = sc->right;
1931 buf[offset++] = sc->left;
1932#else
1933 offset += 2;
0bd88dd3
FP
1934#endif
1935
b3ed458c
FP
1936 /* LED 3 is the global control */
1937 if (sc->led_state[3]) {
1938 buf[offset++] = sc->led_state[0];
1939 buf[offset++] = sc->led_state[1];
1940 buf[offset++] = sc->led_state[2];
1941 } else {
1942 offset += 3;
1943 }
1944
1945 /* If both delay values are zero the DualShock 4 disables blinking. */
1946 buf[offset++] = sc->led_delay_on[3];
1947 buf[offset++] = sc->led_delay_off[3];
60781cf4 1948
fdcf105d 1949 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
2c159de0 1950 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x05_SIZE);
e7ef53ad
RC
1951 else {
1952 /* CRC generation */
1953 u8 bthdr = 0xA2;
1954 u32 crc;
1955
1956 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1957 crc = ~crc32_le(crc, buf, DS4_OUTPUT_REPORT_0x11_SIZE-4);
1958 put_unaligned_le32(crc, &buf[74]);
1959 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x11_SIZE);
1960 }
0bd88dd3
FP
1961}
1962
d8aaccda 1963static void motion_send_output_report(struct sony_sc *sc)
c5e0c1c4 1964{
c5e0c1c4
FP
1965 struct hid_device *hdev = sc->hdev;
1966 struct motion_output_report_02 *report =
1967 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1968
41d2d425 1969 memset(report, 0, MOTION_REPORT_0x02_SIZE);
c5e0c1c4
FP
1970
1971 report->type = 0x02; /* set leds */
1972 report->r = sc->led_state[0];
1973 report->g = sc->led_state[1];
1974 report->b = sc->led_state[2];
1975
1976#ifdef CONFIG_SONY_FF
1977 report->rumble = max(sc->right, sc->left);
1978#endif
1979
1adf904e 1980 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
c5e0c1c4
FP
1981}
1982
decd946c
FP
1983static inline void sony_send_output_report(struct sony_sc *sc)
1984{
1985 if (sc->send_output_report)
1986 sc->send_output_report(sc);
1987}
1988
d8aaccda
FP
1989static void sony_state_worker(struct work_struct *work)
1990{
1991 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
ef916ef5 1992
d8aaccda
FP
1993 sc->send_output_report(sc);
1994}
1995
9b2b5c9a
FP
1996static int sony_allocate_output_report(struct sony_sc *sc)
1997{
4545ee0a
SW
1998 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1999 (sc->quirks & NAVIGATION_CONTROLLER))
9b2b5c9a
FP
2000 sc->output_report_dmabuf =
2001 kmalloc(sizeof(union sixaxis_output_report_01),
2002 GFP_KERNEL);
2003 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
2c159de0 2004 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE,
9b2b5c9a
FP
2005 GFP_KERNEL);
2006 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
2c159de0 2007 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE,
9b2b5c9a 2008 GFP_KERNEL);
c5e0c1c4 2009 else if (sc->quirks & MOTION_CONTROLLER)
41d2d425
SW
2010 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
2011 GFP_KERNEL);
9b2b5c9a
FP
2012 else
2013 return 0;
2014
2015 if (!sc->output_report_dmabuf)
2016 return -ENOMEM;
2017
2018 return 0;
2019}
2020
0a286ef2 2021#ifdef CONFIG_SONY_FF
9f323b68
SE
2022static int sony_play_effect(struct input_dev *dev, void *data,
2023 struct ff_effect *effect)
2024{
a08c22c0 2025 struct hid_device *hid = input_get_drvdata(dev);
9f323b68 2026 struct sony_sc *sc = hid_get_drvdata(hid);
a08c22c0
SE
2027
2028 if (effect->type != FF_RUMBLE)
2029 return 0;
2030
9f323b68 2031 sc->left = effect->u.rumble.strong_magnitude / 256;
0bd88dd3 2032 sc->right = effect->u.rumble.weak_magnitude / 256;
a08c22c0 2033
2a242932 2034 sony_schedule_work(sc);
9f323b68 2035 return 0;
a08c22c0
SE
2036}
2037
fa57a810 2038static int sony_init_ff(struct sony_sc *sc)
a08c22c0 2039{
fa57a810 2040 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
a08c22c0
SE
2041 struct hid_input, list);
2042 struct input_dev *input_dev = hidinput->input;
2043
2044 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
2045 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
2046}
2047
2048#else
fa57a810 2049static int sony_init_ff(struct sony_sc *sc)
a08c22c0
SE
2050{
2051 return 0;
2052}
9f323b68 2053
a08c22c0
SE
2054#endif
2055
d902f472
FP
2056static int sony_battery_get_property(struct power_supply *psy,
2057 enum power_supply_property psp,
2058 union power_supply_propval *val)
2059{
297d716f 2060 struct sony_sc *sc = power_supply_get_drvdata(psy);
d902f472
FP
2061 unsigned long flags;
2062 int ret = 0;
2063 u8 battery_charging, battery_capacity, cable_state;
2064
2065 spin_lock_irqsave(&sc->lock, flags);
2066 battery_charging = sc->battery_charging;
2067 battery_capacity = sc->battery_capacity;
2068 cable_state = sc->cable_state;
2069 spin_unlock_irqrestore(&sc->lock, flags);
2070
2071 switch (psp) {
2072 case POWER_SUPPLY_PROP_PRESENT:
2073 val->intval = 1;
2074 break;
2075 case POWER_SUPPLY_PROP_SCOPE:
2076 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2077 break;
2078 case POWER_SUPPLY_PROP_CAPACITY:
2079 val->intval = battery_capacity;
2080 break;
2081 case POWER_SUPPLY_PROP_STATUS:
2082 if (battery_charging)
2083 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2084 else
2085 if (battery_capacity == 100 && cable_state)
2086 val->intval = POWER_SUPPLY_STATUS_FULL;
2087 else
2088 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2089 break;
2090 default:
2091 ret = -EINVAL;
2092 break;
2093 }
2094 return ret;
9f323b68
SE
2095}
2096
0f398230 2097static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
c4e1ddf2 2098{
0f398230
FP
2099 const char *battery_str_fmt = append_dev_id ?
2100 "sony_controller_battery_%pMR_%i" :
2101 "sony_controller_battery_%pMR";
297d716f 2102 struct power_supply_config psy_cfg = { .drv_data = sc, };
c4e1ddf2 2103 struct hid_device *hdev = sc->hdev;
d902f472 2104 int ret;
c4e1ddf2 2105
ad142b9e
FP
2106 /*
2107 * Set the default battery level to 100% to avoid low battery warnings
d9a293a9
FP
2108 * if the battery is polled before the first device report is received.
2109 */
2110 sc->battery_capacity = 100;
2111
297d716f
KK
2112 sc->battery_desc.properties = sony_battery_props;
2113 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2114 sc->battery_desc.get_property = sony_battery_get_property;
2115 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2116 sc->battery_desc.use_for_apm = 0;
0f398230
FP
2117 sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt,
2118 sc->mac_address, sc->device_id);
297d716f 2119 if (!sc->battery_desc.name)
d902f472 2120 return -ENOMEM;
c4e1ddf2 2121
297d716f
KK
2122 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2123 &psy_cfg);
2124 if (IS_ERR(sc->battery)) {
2125 ret = PTR_ERR(sc->battery);
d902f472
FP
2126 hid_err(hdev, "Unable to register battery device\n");
2127 goto err_free;
2128 }
c4e1ddf2 2129
297d716f 2130 power_supply_powers(sc->battery, &hdev->dev);
a08c22c0 2131 return 0;
d902f472
FP
2132
2133err_free:
297d716f
KK
2134 kfree(sc->battery_desc.name);
2135 sc->battery_desc.name = NULL;
d902f472 2136 return ret;
a08c22c0 2137}
9f323b68 2138
d902f472 2139static void sony_battery_remove(struct sony_sc *sc)
9f323b68 2140{
297d716f 2141 if (!sc->battery_desc.name)
d902f472
FP
2142 return;
2143
297d716f
KK
2144 power_supply_unregister(sc->battery);
2145 kfree(sc->battery_desc.name);
2146 sc->battery_desc.name = NULL;
9f323b68 2147}
a08c22c0 2148
d2d782fc
FP
2149/*
2150 * If a controller is plugged in via USB while already connected via Bluetooth
2151 * it will show up as two devices. A global list of connected controllers and
2152 * their MAC addresses is maintained to ensure that a device is only connected
2153 * once.
0f398230
FP
2154 *
2155 * Some USB-only devices masquerade as Sixaxis controllers and all have the
2156 * same dummy Bluetooth address, so a comparison of the connection type is
2157 * required. Devices are only rejected in the case where two devices have
2158 * matching Bluetooth addresses on different bus types.
d2d782fc 2159 */
0f398230
FP
2160static inline int sony_compare_connection_type(struct sony_sc *sc0,
2161 struct sony_sc *sc1)
2162{
2163 const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
2164 const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
2165
2166 return sc0_not_bt == sc1_not_bt;
2167}
2168
d2d782fc
FP
2169static int sony_check_add_dev_list(struct sony_sc *sc)
2170{
2171 struct sony_sc *entry;
2172 unsigned long flags;
2173 int ret;
2174
2175 spin_lock_irqsave(&sony_dev_list_lock, flags);
2176
2177 list_for_each_entry(entry, &sony_device_list, list_node) {
2178 ret = memcmp(sc->mac_address, entry->mac_address,
2179 sizeof(sc->mac_address));
2180 if (!ret) {
0f398230
FP
2181 if (sony_compare_connection_type(sc, entry)) {
2182 ret = 1;
2183 } else {
2184 ret = -EEXIST;
2185 hid_info(sc->hdev,
2186 "controller with MAC address %pMR already connected\n",
d2d782fc 2187 sc->mac_address);
0f398230 2188 }
d2d782fc 2189 goto unlock;
c4e1ddf2
FP
2190 }
2191 }
2192
d2d782fc
FP
2193 ret = 0;
2194 list_add(&(sc->list_node), &sony_device_list);
c4e1ddf2 2195
d2d782fc
FP
2196unlock:
2197 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2198 return ret;
2199}
2200
2201static void sony_remove_dev_list(struct sony_sc *sc)
2202{
2203 unsigned long flags;
c4e1ddf2 2204
d2d782fc
FP
2205 if (sc->list_node.next) {
2206 spin_lock_irqsave(&sony_dev_list_lock, flags);
2207 list_del(&(sc->list_node));
2208 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2209 }
c4e1ddf2
FP
2210}
2211
d2d782fc
FP
2212static int sony_get_bt_devaddr(struct sony_sc *sc)
2213{
2214 int ret;
2215
2216 /* HIDP stores the device MAC address as a string in the uniq field. */
2217 ret = strlen(sc->hdev->uniq);
2218 if (ret != 17)
2219 return -EINVAL;
2220
2221 ret = sscanf(sc->hdev->uniq,
2222 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2223 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2224 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2225
2226 if (ret != 6)
2227 return -EINVAL;
2228
2229 return 0;
2230}
2231
2232static int sony_check_add(struct sony_sc *sc)
2233{
1adf904e 2234 u8 *buf = NULL;
d2d782fc
FP
2235 int n, ret;
2236
2237 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
12e9a6d7 2238 (sc->quirks & MOTION_CONTROLLER_BT) ||
4545ee0a 2239 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
d2d782fc
FP
2240 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2241 /*
2242 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2243 * address from the uniq string where HIDP stores it.
2244 * As uniq cannot be guaranteed to be a MAC address in all cases
2245 * a failure of this function should not prevent the connection.
2246 */
2247 if (sony_get_bt_devaddr(sc) < 0) {
2248 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2249 return 0;
2250 }
2251 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
2c159de0 2252 buf = kmalloc(DS4_FEATURE_REPORT_0x81_SIZE, GFP_KERNEL);
9b2b5c9a
FP
2253 if (!buf)
2254 return -ENOMEM;
d2d782fc
FP
2255
2256 /*
2257 * The MAC address of a DS4 controller connected via USB can be
2258 * retrieved with feature report 0x81. The address begins at
2259 * offset 1.
2260 */
9b2b5c9a 2261 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2c159de0 2262 DS4_FEATURE_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
9b2b5c9a 2263 HID_REQ_GET_REPORT);
d2d782fc 2264
2c159de0 2265 if (ret != DS4_FEATURE_REPORT_0x81_SIZE) {
d2d782fc 2266 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
9b2b5c9a
FP
2267 ret = ret < 0 ? ret : -EINVAL;
2268 goto out_free;
d2d782fc
FP
2269 }
2270
2271 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
4545ee0a
SW
2272 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2273 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
9b2b5c9a
FP
2274 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2275 if (!buf)
2276 return -ENOMEM;
d2d782fc
FP
2277
2278 /*
2279 * The MAC address of a Sixaxis controller connected via USB can
2280 * be retrieved with feature report 0xf2. The address begins at
2281 * offset 4.
2282 */
9b2b5c9a
FP
2283 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2284 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2285 HID_REQ_GET_REPORT);
d2d782fc 2286
9b2b5c9a 2287 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
d2d782fc 2288 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
9b2b5c9a
FP
2289 ret = ret < 0 ? ret : -EINVAL;
2290 goto out_free;
d2d782fc
FP
2291 }
2292
2293 /*
2294 * The Sixaxis device MAC in the report is big-endian and must
2295 * be byte-swapped.
2296 */
2297 for (n = 0; n < 6; n++)
2298 sc->mac_address[5-n] = buf[4+n];
2299 } else {
2300 return 0;
2301 }
2302
9b2b5c9a
FP
2303 ret = sony_check_add_dev_list(sc);
2304
2305out_free:
2306
2307 kfree(buf);
2308
2309 return ret;
d2d782fc
FP
2310}
2311
8025087a
FP
2312static int sony_set_device_id(struct sony_sc *sc)
2313{
2314 int ret;
2315
2316 /*
2317 * Only DualShock 4 or Sixaxis controllers get an id.
2318 * All others are set to -1.
2319 */
2320 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2321 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2322 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2323 GFP_KERNEL);
2324 if (ret < 0) {
2325 sc->device_id = -1;
2326 return ret;
2327 }
2328 sc->device_id = ret;
2329 } else {
2330 sc->device_id = -1;
2331 }
2332
2333 return 0;
2334}
2335
2336static void sony_release_device_id(struct sony_sc *sc)
2337{
2338 if (sc->device_id >= 0) {
2339 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2340 sc->device_id = -1;
2341 }
2342}
2343
d8aaccda 2344static inline void sony_init_output_report(struct sony_sc *sc,
09593e38 2345 void (*send_output_report)(struct sony_sc *))
46262047 2346{
d8aaccda
FP
2347 sc->send_output_report = send_output_report;
2348
46262047 2349 if (!sc->worker_initialized)
d8aaccda 2350 INIT_WORK(&sc->state_worker, sony_state_worker);
46262047
FP
2351
2352 sc->worker_initialized = 1;
2353}
2354
2355static inline void sony_cancel_work_sync(struct sony_sc *sc)
2356{
2357 if (sc->worker_initialized)
2358 cancel_work_sync(&sc->state_worker);
2359}
d2d782fc 2360
e1bc84d0
RC
2361static int sony_input_configured(struct hid_device *hdev,
2362 struct hid_input *hidinput)
bd28ce00 2363{
e1bc84d0 2364 struct sony_sc *sc = hid_get_drvdata(hdev);
0f398230 2365 int append_dev_id;
e1bc84d0 2366 int ret;
bd28ce00 2367
131a8a9a 2368 ret = sony_set_device_id(sc);
9b2b5c9a 2369 if (ret < 0) {
131a8a9a 2370 hid_err(hdev, "failed to allocate the device id\n");
9b2b5c9a
FP
2371 goto err_stop;
2372 }
2373
131a8a9a 2374 ret = sony_allocate_output_report(sc);
8025087a 2375 if (ret < 0) {
131a8a9a 2376 hid_err(hdev, "failed to allocate the output report buffer\n");
8025087a
FP
2377 goto err_stop;
2378 }
2379
4545ee0a
SW
2380 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2381 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
e534a935
BT
2382 /*
2383 * The Sony Sixaxis does not handle HID Output Reports on the
2384 * Interrupt EP like it could, so we need to force HID Output
2385 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2386 *
2387 * There is also another issue about HID Output Reports via USB,
2388 * the Sixaxis does not want the report_id as part of the data
2389 * packet, so we have to discard buf[0] when sending the actual
2390 * control message, even for numbered reports, humpf!
2a242932
FP
2391 *
2392 * Additionally, the Sixaxis on USB isn't properly initialized
2393 * until the PS logo button is pressed and as such won't retain
2394 * any state set by an output report, so the initial
2395 * configuration report is deferred until the first input
2396 * report arrives.
e534a935
BT
2397 */
2398 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2399 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
2a242932 2400 sc->defer_initialization = 1;
816651a7 2401 ret = sixaxis_set_operational_usb(hdev);
d8aaccda 2402 sony_init_output_report(sc, sixaxis_send_output_report);
4545ee0a
SW
2403 } else if ((sc->quirks & SIXAXIS_CONTROLLER_BT) ||
2404 (sc->quirks & NAVIGATION_CONTROLLER_BT)) {
2078b9bb
FP
2405 /*
2406 * The Sixaxis wants output reports sent on the ctrl endpoint
2407 * when connected via Bluetooth.
2408 */
2409 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
816651a7 2410 ret = sixaxis_set_operational_bt(hdev);
d8aaccda 2411 sony_init_output_report(sc, sixaxis_send_output_report);
fee4e2d5 2412 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
68330d83
FP
2413 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
2414 ret = dualshock4_set_operational_bt(hdev);
2415 if (ret < 0) {
2416 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2417 goto err_stop;
2418 }
2419 }
c4e1ddf2 2420
e1bc84d0
RC
2421 /*
2422 * The Dualshock 4 touchpad supports 2 touches and has a
2423 * resolution of 1920x942 (44.86 dots/mm).
2424 */
2425 ret = sony_register_touchpad(hidinput, 2, 1920, 942);
2426 if (ret) {
2427 hid_err(sc->hdev,
2428 "Unable to initialize multi-touch slots: %d\n",
2429 ret);
2430 return ret;
2431 }
2432
d8aaccda 2433 sony_init_output_report(sc, dualshock4_send_output_report);
c5e0c1c4 2434 } else if (sc->quirks & MOTION_CONTROLLER) {
d8aaccda 2435 sony_init_output_report(sc, motion_send_output_report);
0bd88dd3
FP
2436 } else {
2437 ret = 0;
2438 }
f9ce7c28 2439
d2d782fc
FP
2440 if (ret < 0)
2441 goto err_stop;
2442
0f398230 2443 ret = append_dev_id = sony_check_add(sc);
4dfdc464 2444 if (ret < 0)
bd28ce00
JS
2445 goto err_stop;
2446
0a286ef2 2447 if (sc->quirks & SONY_LED_SUPPORT) {
fa57a810 2448 ret = sony_leds_init(sc);
0a286ef2
SE
2449 if (ret < 0)
2450 goto err_stop;
2451 }
2452
d902f472 2453 if (sc->quirks & SONY_BATTERY_SUPPORT) {
0f398230 2454 ret = sony_battery_probe(sc, append_dev_id);
d902f472
FP
2455 if (ret < 0)
2456 goto err_stop;
2457
2458 /* Open the device to receive reports with battery info */
2459 ret = hid_hw_open(hdev);
2460 if (ret < 0) {
2461 hid_err(hdev, "hw open failed\n");
2462 goto err_stop;
2463 }
2464 }
2465
c8de9dbb 2466 if (sc->quirks & SONY_FF_SUPPORT) {
fa57a810 2467 ret = sony_init_ff(sc);
c8de9dbb
FP
2468 if (ret < 0)
2469 goto err_close;
5f5750d2 2470 }
a08c22c0 2471
bd28ce00 2472 return 0;
d902f472
FP
2473err_close:
2474 hid_hw_close(hdev);
bd28ce00 2475err_stop:
0a286ef2 2476 if (sc->quirks & SONY_LED_SUPPORT)
fa57a810 2477 sony_leds_remove(sc);
d902f472
FP
2478 if (sc->quirks & SONY_BATTERY_SUPPORT)
2479 sony_battery_remove(sc);
46262047 2480 sony_cancel_work_sync(sc);
9b2b5c9a 2481 kfree(sc->output_report_dmabuf);
d2d782fc 2482 sony_remove_dev_list(sc);
8025087a 2483 sony_release_device_id(sc);
bd28ce00 2484 hid_hw_stop(hdev);
bd28ce00
JS
2485 return ret;
2486}
2487
e1bc84d0
RC
2488static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2489{
2490 int ret;
2491 unsigned long quirks = id->driver_data;
2492 struct sony_sc *sc;
2493 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2494
2495 if (!strcmp(hdev->name, "FutureMax Dance Mat"))
2496 quirks |= FUTUREMAX_DANCE_MAT;
2497
2498 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2499 if (sc == NULL) {
2500 hid_err(hdev, "can't alloc sony descriptor\n");
2501 return -ENOMEM;
2502 }
2503
2504 spin_lock_init(&sc->lock);
2505
2506 sc->quirks = quirks;
2507 hid_set_drvdata(hdev, sc);
2508 sc->hdev = hdev;
2509
2510 ret = hid_parse(hdev);
2511 if (ret) {
2512 hid_err(hdev, "parse failed\n");
2513 return ret;
2514 }
2515
2516 if (sc->quirks & VAIO_RDESC_CONSTANT)
2517 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2518 else if (sc->quirks & SIXAXIS_CONTROLLER)
2519 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2520
2521 ret = hid_hw_start(hdev, connect_mask);
2522 if (ret) {
2523 hid_err(hdev, "hw start failed\n");
2524 return ret;
2525 }
2526
4f967f6d
RC
2527 /* sony_input_configured can fail, but this doesn't result
2528 * in hid_hw_start failures (intended). Check whether
2529 * the HID layer claimed the device else fail.
2530 * We don't know the actual reason for the failure, most
2531 * likely it is due to EEXIST in case of double connection
2532 * of USB and Bluetooth, but could have been due to ENOMEM
2533 * or other reasons as well.
2534 */
2535 if (!(hdev->claimed & HID_CLAIMED_INPUT)) {
2536 hid_err(hdev, "failed to claim input\n");
2537 return -ENODEV;
2538 }
2539
e1bc84d0
RC
2540 return ret;
2541}
2542
cc6e0bbb
JK
2543static void sony_remove(struct hid_device *hdev)
2544{
f04d5140
CL
2545 struct sony_sc *sc = hid_get_drvdata(hdev);
2546
0a286ef2 2547 if (sc->quirks & SONY_LED_SUPPORT)
fa57a810 2548 sony_leds_remove(sc);
f04d5140 2549
d902f472
FP
2550 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2551 hid_hw_close(hdev);
2552 sony_battery_remove(sc);
2553 }
2554
46262047 2555 sony_cancel_work_sync(sc);
9f323b68 2556
9b2b5c9a
FP
2557 kfree(sc->output_report_dmabuf);
2558
d2d782fc 2559 sony_remove_dev_list(sc);
9f323b68 2560
8025087a
FP
2561 sony_release_device_id(sc);
2562
cc6e0bbb 2563 hid_hw_stop(hdev);
cc6e0bbb
JK
2564}
2565
decd946c
FP
2566#ifdef CONFIG_PM
2567
2568static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2569{
2570 /*
2571 * On suspend save the current LED state,
2572 * stop running force-feedback and blank the LEDS.
09593e38 2573 */
decd946c
FP
2574 if (SONY_LED_SUPPORT || SONY_FF_SUPPORT) {
2575 struct sony_sc *sc = hid_get_drvdata(hdev);
2576
2577#ifdef CONFIG_SONY_FF
2578 sc->left = sc->right = 0;
2579#endif
2580
2581 memcpy(sc->resume_led_state, sc->led_state,
2582 sizeof(sc->resume_led_state));
2583 memset(sc->led_state, 0, sizeof(sc->led_state));
2584
2585 sony_send_output_report(sc);
2586 }
2587
2588 return 0;
2589}
2590
2591static int sony_resume(struct hid_device *hdev)
2592{
2593 /* Restore the state of controller LEDs on resume */
2594 if (SONY_LED_SUPPORT) {
2595 struct sony_sc *sc = hid_get_drvdata(hdev);
2596
2597 memcpy(sc->led_state, sc->resume_led_state,
2598 sizeof(sc->led_state));
2599
2600 /*
2601 * The Sixaxis and navigation controllers on USB need to be
2602 * reinitialized on resume or they won't behave properly.
2603 */
2604 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2a242932 2605 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
decd946c 2606 sixaxis_set_operational_usb(sc->hdev);
2a242932
FP
2607 sc->defer_initialization = 1;
2608 }
decd946c
FP
2609
2610 sony_set_leds(sc);
2611 }
2612
2613 return 0;
2614}
2615
2616#endif
2617
bd28ce00 2618static const struct hid_device_id sony_devices[] = {
816651a7
AO
2619 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2620 .driver_data = SIXAXIS_CONTROLLER_USB },
35dca5b4 2621 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
4545ee0a 2622 .driver_data = NAVIGATION_CONTROLLER_USB },
6eabaaa0 2623 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
4545ee0a 2624 .driver_data = NAVIGATION_CONTROLLER_BT },
c5e0c1c4 2625 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
b3bca326 2626 .driver_data = MOTION_CONTROLLER_USB },
a4afa854 2627 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
b3bca326 2628 .driver_data = MOTION_CONTROLLER_BT },
816651a7
AO
2629 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2630 .driver_data = SIXAXIS_CONTROLLER_BT },
cc6e0bbb
JK
2631 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2632 .driver_data = VAIO_RDESC_CONSTANT },
a4649184
FLVC
2633 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2634 .driver_data = VAIO_RDESC_CONSTANT },
ef916ef5
AO
2635 /*
2636 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2637 * Logitech joystick from the device descriptor.
2638 */
f04d5140
CL
2639 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2640 .driver_data = BUZZ_CONTROLLER },
2641 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2642 .driver_data = BUZZ_CONTROLLER },
078328da
JK
2643 /* PS3 BD Remote Control */
2644 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2645 .driver_data = PS3REMOTE },
2646 /* Logitech Harmony Adapter for PS3 */
2647 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2648 .driver_data = PS3REMOTE },
68a49e51
FP
2649 /* SMK-Link PS3 BD Remote Control */
2650 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2651 .driver_data = PS3REMOTE },
0bd88dd3
FP
2652 /* Sony Dualshock 4 controllers for PS4 */
2653 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
8ab1676b 2654 .driver_data = DUALSHOCK4_CONTROLLER_USB },
0bd88dd3 2655 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
8ab1676b 2656 .driver_data = DUALSHOCK4_CONTROLLER_BT },
cf1015d6
RC
2657 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2658 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2659 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2660 .driver_data = DUALSHOCK4_CONTROLLER_BT },
74500cc8
SM
2661 /* Nyko Core Controller for PS3 */
2662 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
2663 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
bd28ce00
JS
2664 { }
2665};
2666MODULE_DEVICE_TABLE(hid, sony_devices);
2667
2668static struct hid_driver sony_driver = {
ce8efc3b
FP
2669 .name = "sony",
2670 .id_table = sony_devices,
2671 .input_mapping = sony_mapping,
2672 .input_configured = sony_input_configured,
2673 .probe = sony_probe,
2674 .remove = sony_remove,
2675 .report_fixup = sony_report_fixup,
decd946c
FP
2676 .raw_event = sony_raw_event,
2677
2678#ifdef CONFIG_PM
2679 .suspend = sony_suspend,
2680 .resume = sony_resume,
2681 .reset_resume = sony_resume,
2682#endif
bd28ce00 2683};
8025087a
FP
2684
2685static int __init sony_init(void)
2686{
2687 dbg_hid("Sony:%s\n", __func__);
2688
2689 return hid_register_driver(&sony_driver);
2690}
2691
2692static void __exit sony_exit(void)
2693{
2694 dbg_hid("Sony:%s\n", __func__);
2695
8025087a 2696 hid_unregister_driver(&sony_driver);
6c40065f 2697 ida_destroy(&sony_device_id_allocator);
8025087a
FP
2698}
2699module_init(sony_init);
2700module_exit(sony_exit);
bd28ce00 2701
bd28ce00 2702MODULE_LICENSE("GPL");