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