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