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