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