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