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