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