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