2 * HID driver for Sony / PS2 / PS3 / PS4 BD devices.
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>
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)
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.
26 * There will be no PIN request from the device.
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>
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)
52 #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
53 #define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
54 #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
55 DUALSHOCK4_CONTROLLER_BT)
56 #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
57 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER)
58 #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
60 #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
66 * The Sixaxis reports both digital and analog values for each button on the
67 * controller except for Start, Select and the PS button. The controller ends
68 * up reporting 27 axes which causes them to spill over into the multi-touch
69 * axis values. Additionally, the controller only has 20 actual, physical axes
70 * so there are several unused axes in between the used ones.
72 static __u8 sixaxis_rdesc
[] = {
73 0x05, 0x01, /* Usage Page (Desktop), */
74 0x09, 0x04, /* Usage (Joystick), */
75 0xA1, 0x01, /* Collection (Application), */
76 0xA1, 0x02, /* Collection (Logical), */
77 0x85, 0x01, /* Report ID (1), */
78 0x75, 0x08, /* Report Size (8), */
79 0x95, 0x01, /* Report Count (1), */
80 0x15, 0x00, /* Logical Minimum (0), */
81 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
82 0x81, 0x03, /* Input (Constant, Variable), */
83 0x75, 0x01, /* Report Size (1), */
84 0x95, 0x13, /* Report Count (19), */
85 0x15, 0x00, /* Logical Minimum (0), */
86 0x25, 0x01, /* Logical Maximum (1), */
87 0x35, 0x00, /* Physical Minimum (0), */
88 0x45, 0x01, /* Physical Maximum (1), */
89 0x05, 0x09, /* Usage Page (Button), */
90 0x19, 0x01, /* Usage Minimum (01h), */
91 0x29, 0x13, /* Usage Maximum (13h), */
92 0x81, 0x02, /* Input (Variable), */
93 0x75, 0x01, /* Report Size (1), */
94 0x95, 0x0D, /* Report Count (13), */
95 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
96 0x81, 0x03, /* Input (Constant, Variable), */
97 0x15, 0x00, /* Logical Minimum (0), */
98 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
99 0x05, 0x01, /* Usage Page (Desktop), */
100 0x09, 0x01, /* Usage (Pointer), */
101 0xA1, 0x00, /* Collection (Physical), */
102 0x75, 0x08, /* Report Size (8), */
103 0x95, 0x04, /* Report Count (4), */
104 0x35, 0x00, /* Physical Minimum (0), */
105 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
106 0x09, 0x30, /* Usage (X), */
107 0x09, 0x31, /* Usage (Y), */
108 0x09, 0x32, /* Usage (Z), */
109 0x09, 0x35, /* Usage (Rz), */
110 0x81, 0x02, /* Input (Variable), */
111 0xC0, /* End Collection, */
112 0x05, 0x01, /* Usage Page (Desktop), */
113 0x95, 0x13, /* Report Count (19), */
114 0x09, 0x01, /* Usage (Pointer), */
115 0x81, 0x02, /* Input (Variable), */
116 0x95, 0x0C, /* Report Count (12), */
117 0x81, 0x01, /* Input (Constant), */
118 0x75, 0x10, /* Report Size (16), */
119 0x95, 0x04, /* Report Count (4), */
120 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
121 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
122 0x09, 0x01, /* Usage (Pointer), */
123 0x81, 0x02, /* Input (Variable), */
124 0xC0, /* End Collection, */
125 0xA1, 0x02, /* Collection (Logical), */
126 0x85, 0x02, /* Report ID (2), */
127 0x75, 0x08, /* Report Size (8), */
128 0x95, 0x30, /* Report Count (48), */
129 0x09, 0x01, /* Usage (Pointer), */
130 0xB1, 0x02, /* Feature (Variable), */
131 0xC0, /* End Collection, */
132 0xA1, 0x02, /* Collection (Logical), */
133 0x85, 0xEE, /* Report ID (238), */
134 0x75, 0x08, /* Report Size (8), */
135 0x95, 0x30, /* Report Count (48), */
136 0x09, 0x01, /* Usage (Pointer), */
137 0xB1, 0x02, /* Feature (Variable), */
138 0xC0, /* End Collection, */
139 0xA1, 0x02, /* Collection (Logical), */
140 0x85, 0xEF, /* Report ID (239), */
141 0x75, 0x08, /* Report Size (8), */
142 0x95, 0x30, /* Report Count (48), */
143 0x09, 0x01, /* Usage (Pointer), */
144 0xB1, 0x02, /* Feature (Variable), */
145 0xC0, /* End Collection, */
146 0xC0 /* End Collection */
149 /* PS/3 Motion controller */
150 static __u8 motion_rdesc
[] = {
151 0x05, 0x01, /* Usage Page (Desktop), */
152 0x09, 0x04, /* Usage (Joystick), */
153 0xA1, 0x01, /* Collection (Application), */
154 0xA1, 0x02, /* Collection (Logical), */
155 0x85, 0x01, /* Report ID (1), */
156 0x75, 0x08, /* Report Size (8), */
157 0x95, 0x01, /* Report Count (1), */
158 0x15, 0x00, /* Logical Minimum (0), */
159 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
160 0x81, 0x03, /* Input (Constant, Variable), */
161 0x75, 0x01, /* Report Size (1), */
162 0x95, 0x13, /* Report Count (19), */
163 0x15, 0x00, /* Logical Minimum (0), */
164 0x25, 0x01, /* Logical Maximum (1), */
165 0x35, 0x00, /* Physical Minimum (0), */
166 0x45, 0x01, /* Physical Maximum (1), */
167 0x05, 0x09, /* Usage Page (Button), */
168 0x19, 0x01, /* Usage Minimum (01h), */
169 0x29, 0x13, /* Usage Maximum (13h), */
170 0x81, 0x02, /* Input (Variable), */
171 0x75, 0x01, /* Report Size (1), */
172 0x95, 0x0D, /* Report Count (13), */
173 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
174 0x81, 0x03, /* Input (Constant, Variable), */
175 0x15, 0x00, /* Logical Minimum (0), */
176 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
177 0x05, 0x01, /* Usage Page (Desktop), */
178 0x09, 0x01, /* Usage (Pointer), */
179 0xA1, 0x00, /* Collection (Physical), */
180 0x75, 0x08, /* Report Size (8), */
181 0x95, 0x04, /* Report Count (4), */
182 0x35, 0x00, /* Physical Minimum (0), */
183 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
184 0x09, 0x30, /* Usage (X), */
185 0x09, 0x31, /* Usage (Y), */
186 0x09, 0x32, /* Usage (Z), */
187 0x09, 0x35, /* Usage (Rz), */
188 0x81, 0x02, /* Input (Variable), */
189 0xC0, /* End Collection, */
190 0x05, 0x01, /* Usage Page (Desktop), */
191 0x95, 0x13, /* Report Count (19), */
192 0x09, 0x01, /* Usage (Pointer), */
193 0x81, 0x02, /* Input (Variable), */
194 0x95, 0x0C, /* Report Count (12), */
195 0x81, 0x01, /* Input (Constant), */
196 0x75, 0x10, /* Report Size (16), */
197 0x95, 0x04, /* Report Count (4), */
198 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
199 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
200 0x09, 0x01, /* Usage (Pointer), */
201 0x81, 0x02, /* Input (Variable), */
202 0xC0, /* End Collection, */
203 0xA1, 0x02, /* Collection (Logical), */
204 0x85, 0x02, /* Report ID (2), */
205 0x75, 0x08, /* Report Size (8), */
206 0x95, 0x30, /* Report Count (48), */
207 0x09, 0x01, /* Usage (Pointer), */
208 0xB1, 0x02, /* Feature (Variable), */
209 0xC0, /* End Collection, */
210 0xA1, 0x02, /* Collection (Logical), */
211 0x85, 0xEE, /* Report ID (238), */
212 0x75, 0x08, /* Report Size (8), */
213 0x95, 0x30, /* Report Count (48), */
214 0x09, 0x01, /* Usage (Pointer), */
215 0xB1, 0x02, /* Feature (Variable), */
216 0xC0, /* End Collection, */
217 0xA1, 0x02, /* Collection (Logical), */
218 0x85, 0xEF, /* Report ID (239), */
219 0x75, 0x08, /* Report Size (8), */
220 0x95, 0x30, /* Report Count (48), */
221 0x09, 0x01, /* Usage (Pointer), */
222 0xB1, 0x02, /* Feature (Variable), */
223 0xC0, /* End Collection, */
224 0xC0 /* End Collection */
229 * The default descriptor doesn't provide mapping for the accelerometers
230 * or orientation sensors. This fixed descriptor maps the accelerometers
231 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
232 * to usage values 0x43, 0x44 and 0x45.
234 static u8 dualshock4_usb_rdesc
[] = {
235 0x05, 0x01, /* Usage Page (Desktop), */
236 0x09, 0x05, /* Usage (Gamepad), */
237 0xA1, 0x01, /* Collection (Application), */
238 0x85, 0x01, /* Report ID (1), */
239 0x09, 0x30, /* Usage (X), */
240 0x09, 0x31, /* Usage (Y), */
241 0x09, 0x32, /* Usage (Z), */
242 0x09, 0x35, /* Usage (Rz), */
243 0x15, 0x00, /* Logical Minimum (0), */
244 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
245 0x75, 0x08, /* Report Size (8), */
246 0x95, 0x04, /* Report Count (4), */
247 0x81, 0x02, /* Input (Variable), */
248 0x09, 0x39, /* Usage (Hat Switch), */
249 0x15, 0x00, /* Logical Minimum (0), */
250 0x25, 0x07, /* Logical Maximum (7), */
251 0x35, 0x00, /* Physical Minimum (0), */
252 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
253 0x65, 0x14, /* Unit (Degrees), */
254 0x75, 0x04, /* Report Size (4), */
255 0x95, 0x01, /* Report Count (1), */
256 0x81, 0x42, /* Input (Variable, Null State), */
257 0x65, 0x00, /* Unit, */
258 0x05, 0x09, /* Usage Page (Button), */
259 0x19, 0x01, /* Usage Minimum (01h), */
260 0x29, 0x0E, /* Usage Maximum (0Eh), */
261 0x15, 0x00, /* Logical Minimum (0), */
262 0x25, 0x01, /* Logical Maximum (1), */
263 0x75, 0x01, /* Report Size (1), */
264 0x95, 0x0E, /* Report Count (14), */
265 0x81, 0x02, /* Input (Variable), */
266 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
267 0x09, 0x20, /* Usage (20h), */
268 0x75, 0x06, /* Report Size (6), */
269 0x95, 0x01, /* Report Count (1), */
270 0x15, 0x00, /* Logical Minimum (0), */
271 0x25, 0x3F, /* Logical Maximum (63), */
272 0x81, 0x02, /* Input (Variable), */
273 0x05, 0x01, /* Usage Page (Desktop), */
274 0x09, 0x33, /* Usage (Rx), */
275 0x09, 0x34, /* Usage (Ry), */
276 0x15, 0x00, /* Logical Minimum (0), */
277 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
278 0x75, 0x08, /* Report Size (8), */
279 0x95, 0x02, /* Report Count (2), */
280 0x81, 0x02, /* Input (Variable), */
281 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
282 0x09, 0x21, /* Usage (21h), */
283 0x95, 0x03, /* Report Count (3), */
284 0x81, 0x02, /* Input (Variable), */
285 0x05, 0x01, /* Usage Page (Desktop), */
286 0x19, 0x40, /* Usage Minimum (40h), */
287 0x29, 0x42, /* Usage Maximum (42h), */
288 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
289 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
290 0x75, 0x10, /* Report Size (16), */
291 0x95, 0x03, /* Report Count (3), */
292 0x81, 0x02, /* Input (Variable), */
293 0x19, 0x43, /* Usage Minimum (43h), */
294 0x29, 0x45, /* Usage Maximum (45h), */
295 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
296 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
297 0x95, 0x03, /* Report Count (3), */
298 0x81, 0x02, /* Input (Variable), */
299 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
300 0x09, 0x21, /* Usage (21h), */
301 0x15, 0x00, /* Logical Minimum (0), */
302 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
303 0x75, 0x08, /* Report Size (8), */
304 0x95, 0x27, /* Report Count (39), */
305 0x81, 0x02, /* Input (Variable), */
306 0x85, 0x05, /* Report ID (5), */
307 0x09, 0x22, /* Usage (22h), */
308 0x95, 0x1F, /* Report Count (31), */
309 0x91, 0x02, /* Output (Variable), */
310 0x85, 0x04, /* Report ID (4), */
311 0x09, 0x23, /* Usage (23h), */
312 0x95, 0x24, /* Report Count (36), */
313 0xB1, 0x02, /* Feature (Variable), */
314 0x85, 0x02, /* Report ID (2), */
315 0x09, 0x24, /* Usage (24h), */
316 0x95, 0x24, /* Report Count (36), */
317 0xB1, 0x02, /* Feature (Variable), */
318 0x85, 0x08, /* Report ID (8), */
319 0x09, 0x25, /* Usage (25h), */
320 0x95, 0x03, /* Report Count (3), */
321 0xB1, 0x02, /* Feature (Variable), */
322 0x85, 0x10, /* Report ID (16), */
323 0x09, 0x26, /* Usage (26h), */
324 0x95, 0x04, /* Report Count (4), */
325 0xB1, 0x02, /* Feature (Variable), */
326 0x85, 0x11, /* Report ID (17), */
327 0x09, 0x27, /* Usage (27h), */
328 0x95, 0x02, /* Report Count (2), */
329 0xB1, 0x02, /* Feature (Variable), */
330 0x85, 0x12, /* Report ID (18), */
331 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
332 0x09, 0x21, /* Usage (21h), */
333 0x95, 0x0F, /* Report Count (15), */
334 0xB1, 0x02, /* Feature (Variable), */
335 0x85, 0x13, /* Report ID (19), */
336 0x09, 0x22, /* Usage (22h), */
337 0x95, 0x16, /* Report Count (22), */
338 0xB1, 0x02, /* Feature (Variable), */
339 0x85, 0x14, /* Report ID (20), */
340 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
341 0x09, 0x20, /* Usage (20h), */
342 0x95, 0x10, /* Report Count (16), */
343 0xB1, 0x02, /* Feature (Variable), */
344 0x85, 0x15, /* Report ID (21), */
345 0x09, 0x21, /* Usage (21h), */
346 0x95, 0x2C, /* Report Count (44), */
347 0xB1, 0x02, /* Feature (Variable), */
348 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
349 0x85, 0x80, /* Report ID (128), */
350 0x09, 0x20, /* Usage (20h), */
351 0x95, 0x06, /* Report Count (6), */
352 0xB1, 0x02, /* Feature (Variable), */
353 0x85, 0x81, /* Report ID (129), */
354 0x09, 0x21, /* Usage (21h), */
355 0x95, 0x06, /* Report Count (6), */
356 0xB1, 0x02, /* Feature (Variable), */
357 0x85, 0x82, /* Report ID (130), */
358 0x09, 0x22, /* Usage (22h), */
359 0x95, 0x05, /* Report Count (5), */
360 0xB1, 0x02, /* Feature (Variable), */
361 0x85, 0x83, /* Report ID (131), */
362 0x09, 0x23, /* Usage (23h), */
363 0x95, 0x01, /* Report Count (1), */
364 0xB1, 0x02, /* Feature (Variable), */
365 0x85, 0x84, /* Report ID (132), */
366 0x09, 0x24, /* Usage (24h), */
367 0x95, 0x04, /* Report Count (4), */
368 0xB1, 0x02, /* Feature (Variable), */
369 0x85, 0x85, /* Report ID (133), */
370 0x09, 0x25, /* Usage (25h), */
371 0x95, 0x06, /* Report Count (6), */
372 0xB1, 0x02, /* Feature (Variable), */
373 0x85, 0x86, /* Report ID (134), */
374 0x09, 0x26, /* Usage (26h), */
375 0x95, 0x06, /* Report Count (6), */
376 0xB1, 0x02, /* Feature (Variable), */
377 0x85, 0x87, /* Report ID (135), */
378 0x09, 0x27, /* Usage (27h), */
379 0x95, 0x23, /* Report Count (35), */
380 0xB1, 0x02, /* Feature (Variable), */
381 0x85, 0x88, /* Report ID (136), */
382 0x09, 0x28, /* Usage (28h), */
383 0x95, 0x22, /* Report Count (34), */
384 0xB1, 0x02, /* Feature (Variable), */
385 0x85, 0x89, /* Report ID (137), */
386 0x09, 0x29, /* Usage (29h), */
387 0x95, 0x02, /* Report Count (2), */
388 0xB1, 0x02, /* Feature (Variable), */
389 0x85, 0x90, /* Report ID (144), */
390 0x09, 0x30, /* Usage (30h), */
391 0x95, 0x05, /* Report Count (5), */
392 0xB1, 0x02, /* Feature (Variable), */
393 0x85, 0x91, /* Report ID (145), */
394 0x09, 0x31, /* Usage (31h), */
395 0x95, 0x03, /* Report Count (3), */
396 0xB1, 0x02, /* Feature (Variable), */
397 0x85, 0x92, /* Report ID (146), */
398 0x09, 0x32, /* Usage (32h), */
399 0x95, 0x03, /* Report Count (3), */
400 0xB1, 0x02, /* Feature (Variable), */
401 0x85, 0x93, /* Report ID (147), */
402 0x09, 0x33, /* Usage (33h), */
403 0x95, 0x0C, /* Report Count (12), */
404 0xB1, 0x02, /* Feature (Variable), */
405 0x85, 0xA0, /* Report ID (160), */
406 0x09, 0x40, /* Usage (40h), */
407 0x95, 0x06, /* Report Count (6), */
408 0xB1, 0x02, /* Feature (Variable), */
409 0x85, 0xA1, /* Report ID (161), */
410 0x09, 0x41, /* Usage (41h), */
411 0x95, 0x01, /* Report Count (1), */
412 0xB1, 0x02, /* Feature (Variable), */
413 0x85, 0xA2, /* Report ID (162), */
414 0x09, 0x42, /* Usage (42h), */
415 0x95, 0x01, /* Report Count (1), */
416 0xB1, 0x02, /* Feature (Variable), */
417 0x85, 0xA3, /* Report ID (163), */
418 0x09, 0x43, /* Usage (43h), */
419 0x95, 0x30, /* Report Count (48), */
420 0xB1, 0x02, /* Feature (Variable), */
421 0x85, 0xA4, /* Report ID (164), */
422 0x09, 0x44, /* Usage (44h), */
423 0x95, 0x0D, /* Report Count (13), */
424 0xB1, 0x02, /* Feature (Variable), */
425 0x85, 0xA5, /* Report ID (165), */
426 0x09, 0x45, /* Usage (45h), */
427 0x95, 0x15, /* Report Count (21), */
428 0xB1, 0x02, /* Feature (Variable), */
429 0x85, 0xA6, /* Report ID (166), */
430 0x09, 0x46, /* Usage (46h), */
431 0x95, 0x15, /* Report Count (21), */
432 0xB1, 0x02, /* Feature (Variable), */
433 0x85, 0xF0, /* Report ID (240), */
434 0x09, 0x47, /* Usage (47h), */
435 0x95, 0x3F, /* Report Count (63), */
436 0xB1, 0x02, /* Feature (Variable), */
437 0x85, 0xF1, /* Report ID (241), */
438 0x09, 0x48, /* Usage (48h), */
439 0x95, 0x3F, /* Report Count (63), */
440 0xB1, 0x02, /* Feature (Variable), */
441 0x85, 0xF2, /* Report ID (242), */
442 0x09, 0x49, /* Usage (49h), */
443 0x95, 0x0F, /* Report Count (15), */
444 0xB1, 0x02, /* Feature (Variable), */
445 0x85, 0xA7, /* Report ID (167), */
446 0x09, 0x4A, /* Usage (4Ah), */
447 0x95, 0x01, /* Report Count (1), */
448 0xB1, 0x02, /* Feature (Variable), */
449 0x85, 0xA8, /* Report ID (168), */
450 0x09, 0x4B, /* Usage (4Bh), */
451 0x95, 0x01, /* Report Count (1), */
452 0xB1, 0x02, /* Feature (Variable), */
453 0x85, 0xA9, /* Report ID (169), */
454 0x09, 0x4C, /* Usage (4Ch), */
455 0x95, 0x08, /* Report Count (8), */
456 0xB1, 0x02, /* Feature (Variable), */
457 0x85, 0xAA, /* Report ID (170), */
458 0x09, 0x4E, /* Usage (4Eh), */
459 0x95, 0x01, /* Report Count (1), */
460 0xB1, 0x02, /* Feature (Variable), */
461 0x85, 0xAB, /* Report ID (171), */
462 0x09, 0x4F, /* Usage (4Fh), */
463 0x95, 0x39, /* Report Count (57), */
464 0xB1, 0x02, /* Feature (Variable), */
465 0x85, 0xAC, /* Report ID (172), */
466 0x09, 0x50, /* Usage (50h), */
467 0x95, 0x39, /* Report Count (57), */
468 0xB1, 0x02, /* Feature (Variable), */
469 0x85, 0xAD, /* Report ID (173), */
470 0x09, 0x51, /* Usage (51h), */
471 0x95, 0x0B, /* Report Count (11), */
472 0xB1, 0x02, /* Feature (Variable), */
473 0x85, 0xAE, /* Report ID (174), */
474 0x09, 0x52, /* Usage (52h), */
475 0x95, 0x01, /* Report Count (1), */
476 0xB1, 0x02, /* Feature (Variable), */
477 0x85, 0xAF, /* Report ID (175), */
478 0x09, 0x53, /* Usage (53h), */
479 0x95, 0x02, /* Report Count (2), */
480 0xB1, 0x02, /* Feature (Variable), */
481 0x85, 0xB0, /* Report ID (176), */
482 0x09, 0x54, /* Usage (54h), */
483 0x95, 0x3F, /* Report Count (63), */
484 0xB1, 0x02, /* Feature (Variable), */
485 0xC0 /* End Collection */
489 * The default behavior of the Dualshock 4 is to send reports using report
490 * type 1 when running over Bluetooth. However, when feature report 2 is
491 * requested during the controller initialization it starts sending input
492 * reports in report 17. Since report 17 is undefined in the default HID
493 * descriptor the button and axis definitions must be moved to report 17 or
494 * the HID layer won't process the received input.
496 static u8 dualshock4_bt_rdesc
[] = {
497 0x05, 0x01, /* Usage Page (Desktop), */
498 0x09, 0x05, /* Usage (Gamepad), */
499 0xA1, 0x01, /* Collection (Application), */
500 0x85, 0x01, /* Report ID (1), */
501 0x75, 0x08, /* Report Size (8), */
502 0x95, 0x0A, /* Report Count (9), */
503 0x81, 0x02, /* Input (Variable), */
504 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
505 0x85, 0x02, /* Report ID (2), */
506 0x09, 0x24, /* Usage (24h), */
507 0x95, 0x24, /* Report Count (36), */
508 0xB1, 0x02, /* Feature (Variable), */
509 0x85, 0xA3, /* Report ID (163), */
510 0x09, 0x25, /* Usage (25h), */
511 0x95, 0x30, /* Report Count (48), */
512 0xB1, 0x02, /* Feature (Variable), */
513 0x85, 0x05, /* Report ID (5), */
514 0x09, 0x26, /* Usage (26h), */
515 0x95, 0x28, /* Report Count (40), */
516 0xB1, 0x02, /* Feature (Variable), */
517 0x85, 0x06, /* Report ID (6), */
518 0x09, 0x27, /* Usage (27h), */
519 0x95, 0x34, /* Report Count (52), */
520 0xB1, 0x02, /* Feature (Variable), */
521 0x85, 0x07, /* Report ID (7), */
522 0x09, 0x28, /* Usage (28h), */
523 0x95, 0x30, /* Report Count (48), */
524 0xB1, 0x02, /* Feature (Variable), */
525 0x85, 0x08, /* Report ID (8), */
526 0x09, 0x29, /* Usage (29h), */
527 0x95, 0x2F, /* Report Count (47), */
528 0xB1, 0x02, /* Feature (Variable), */
529 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
530 0x85, 0x03, /* Report ID (3), */
531 0x09, 0x21, /* Usage (21h), */
532 0x95, 0x26, /* Report Count (38), */
533 0xB1, 0x02, /* Feature (Variable), */
534 0x85, 0x04, /* Report ID (4), */
535 0x09, 0x22, /* Usage (22h), */
536 0x95, 0x2E, /* Report Count (46), */
537 0xB1, 0x02, /* Feature (Variable), */
538 0x85, 0xF0, /* Report ID (240), */
539 0x09, 0x47, /* Usage (47h), */
540 0x95, 0x3F, /* Report Count (63), */
541 0xB1, 0x02, /* Feature (Variable), */
542 0x85, 0xF1, /* Report ID (241), */
543 0x09, 0x48, /* Usage (48h), */
544 0x95, 0x3F, /* Report Count (63), */
545 0xB1, 0x02, /* Feature (Variable), */
546 0x85, 0xF2, /* Report ID (242), */
547 0x09, 0x49, /* Usage (49h), */
548 0x95, 0x0F, /* Report Count (15), */
549 0xB1, 0x02, /* Feature (Variable), */
550 0x85, 0x11, /* Report ID (17), */
551 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
552 0x09, 0x20, /* Usage (20h), */
553 0x95, 0x02, /* Report Count (2), */
554 0x81, 0x02, /* Input (Variable), */
555 0x05, 0x01, /* Usage Page (Desktop), */
556 0x09, 0x30, /* Usage (X), */
557 0x09, 0x31, /* Usage (Y), */
558 0x09, 0x32, /* Usage (Z), */
559 0x09, 0x35, /* Usage (Rz), */
560 0x15, 0x00, /* Logical Minimum (0), */
561 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
562 0x75, 0x08, /* Report Size (8), */
563 0x95, 0x04, /* Report Count (4), */
564 0x81, 0x02, /* Input (Variable), */
565 0x09, 0x39, /* Usage (Hat Switch), */
566 0x15, 0x00, /* Logical Minimum (0), */
567 0x25, 0x07, /* Logical Maximum (7), */
568 0x75, 0x04, /* Report Size (4), */
569 0x95, 0x01, /* Report Count (1), */
570 0x81, 0x42, /* Input (Variable, Null State), */
571 0x05, 0x09, /* Usage Page (Button), */
572 0x19, 0x01, /* Usage Minimum (01h), */
573 0x29, 0x0E, /* Usage Maximum (0Eh), */
574 0x15, 0x00, /* Logical Minimum (0), */
575 0x25, 0x01, /* Logical Maximum (1), */
576 0x75, 0x01, /* Report Size (1), */
577 0x95, 0x0E, /* Report Count (14), */
578 0x81, 0x02, /* Input (Variable), */
579 0x75, 0x06, /* Report Size (6), */
580 0x95, 0x01, /* Report Count (1), */
581 0x81, 0x01, /* Input (Constant), */
582 0x05, 0x01, /* Usage Page (Desktop), */
583 0x09, 0x33, /* Usage (Rx), */
584 0x09, 0x34, /* Usage (Ry), */
585 0x15, 0x00, /* Logical Minimum (0), */
586 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
587 0x75, 0x08, /* Report Size (8), */
588 0x95, 0x02, /* Report Count (2), */
589 0x81, 0x02, /* Input (Variable), */
590 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
591 0x09, 0x20, /* Usage (20h), */
592 0x95, 0x03, /* Report Count (3), */
593 0x81, 0x02, /* Input (Variable), */
594 0x05, 0x01, /* Usage Page (Desktop), */
595 0x19, 0x40, /* Usage Minimum (40h), */
596 0x29, 0x42, /* Usage Maximum (42h), */
597 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
598 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
599 0x75, 0x10, /* Report Size (16), */
600 0x95, 0x03, /* Report Count (3), */
601 0x81, 0x02, /* Input (Variable), */
602 0x19, 0x43, /* Usage Minimum (43h), */
603 0x29, 0x45, /* Usage Maximum (45h), */
604 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
605 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
606 0x95, 0x03, /* Report Count (3), */
607 0x81, 0x02, /* Input (Variable), */
608 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
609 0x09, 0x20, /* Usage (20h), */
610 0x15, 0x00, /* Logical Minimum (0), */
611 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
612 0x75, 0x08, /* Report Size (8), */
613 0x95, 0x31, /* Report Count (51), */
614 0x81, 0x02, /* Input (Variable), */
615 0x09, 0x21, /* Usage (21h), */
616 0x75, 0x08, /* Report Size (8), */
617 0x95, 0x4D, /* Report Count (77), */
618 0x91, 0x02, /* Output (Variable), */
619 0x85, 0x12, /* Report ID (18), */
620 0x09, 0x22, /* Usage (22h), */
621 0x95, 0x8D, /* Report Count (141), */
622 0x81, 0x02, /* Input (Variable), */
623 0x09, 0x23, /* Usage (23h), */
624 0x91, 0x02, /* Output (Variable), */
625 0x85, 0x13, /* Report ID (19), */
626 0x09, 0x24, /* Usage (24h), */
627 0x95, 0xCD, /* Report Count (205), */
628 0x81, 0x02, /* Input (Variable), */
629 0x09, 0x25, /* Usage (25h), */
630 0x91, 0x02, /* Output (Variable), */
631 0x85, 0x14, /* Report ID (20), */
632 0x09, 0x26, /* Usage (26h), */
633 0x96, 0x0D, 0x01, /* Report Count (269), */
634 0x81, 0x02, /* Input (Variable), */
635 0x09, 0x27, /* Usage (27h), */
636 0x91, 0x02, /* Output (Variable), */
637 0x85, 0x15, /* Report ID (21), */
638 0x09, 0x28, /* Usage (28h), */
639 0x96, 0x4D, 0x01, /* Report Count (333), */
640 0x81, 0x02, /* Input (Variable), */
641 0x09, 0x29, /* Usage (29h), */
642 0x91, 0x02, /* Output (Variable), */
643 0x85, 0x16, /* Report ID (22), */
644 0x09, 0x2A, /* Usage (2Ah), */
645 0x96, 0x8D, 0x01, /* Report Count (397), */
646 0x81, 0x02, /* Input (Variable), */
647 0x09, 0x2B, /* Usage (2Bh), */
648 0x91, 0x02, /* Output (Variable), */
649 0x85, 0x17, /* Report ID (23), */
650 0x09, 0x2C, /* Usage (2Ch), */
651 0x96, 0xCD, 0x01, /* Report Count (461), */
652 0x81, 0x02, /* Input (Variable), */
653 0x09, 0x2D, /* Usage (2Dh), */
654 0x91, 0x02, /* Output (Variable), */
655 0x85, 0x18, /* Report ID (24), */
656 0x09, 0x2E, /* Usage (2Eh), */
657 0x96, 0x0D, 0x02, /* Report Count (525), */
658 0x81, 0x02, /* Input (Variable), */
659 0x09, 0x2F, /* Usage (2Fh), */
660 0x91, 0x02, /* Output (Variable), */
661 0x85, 0x19, /* Report ID (25), */
662 0x09, 0x30, /* Usage (30h), */
663 0x96, 0x22, 0x02, /* Report Count (546), */
664 0x81, 0x02, /* Input (Variable), */
665 0x09, 0x31, /* Usage (31h), */
666 0x91, 0x02, /* Output (Variable), */
667 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
668 0x85, 0x82, /* Report ID (130), */
669 0x09, 0x22, /* Usage (22h), */
670 0x95, 0x3F, /* Report Count (63), */
671 0xB1, 0x02, /* Feature (Variable), */
672 0x85, 0x83, /* Report ID (131), */
673 0x09, 0x23, /* Usage (23h), */
674 0xB1, 0x02, /* Feature (Variable), */
675 0x85, 0x84, /* Report ID (132), */
676 0x09, 0x24, /* Usage (24h), */
677 0xB1, 0x02, /* Feature (Variable), */
678 0x85, 0x90, /* Report ID (144), */
679 0x09, 0x30, /* Usage (30h), */
680 0xB1, 0x02, /* Feature (Variable), */
681 0x85, 0x91, /* Report ID (145), */
682 0x09, 0x31, /* Usage (31h), */
683 0xB1, 0x02, /* Feature (Variable), */
684 0x85, 0x92, /* Report ID (146), */
685 0x09, 0x32, /* Usage (32h), */
686 0xB1, 0x02, /* Feature (Variable), */
687 0x85, 0x93, /* Report ID (147), */
688 0x09, 0x33, /* Usage (33h), */
689 0xB1, 0x02, /* Feature (Variable), */
690 0x85, 0xA0, /* Report ID (160), */
691 0x09, 0x40, /* Usage (40h), */
692 0xB1, 0x02, /* Feature (Variable), */
693 0x85, 0xA4, /* Report ID (164), */
694 0x09, 0x44, /* Usage (44h), */
695 0xB1, 0x02, /* Feature (Variable), */
696 0xC0 /* End Collection */
699 static __u8 ps3remote_rdesc
[] = {
700 0x05, 0x01, /* GUsagePage Generic Desktop */
701 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
702 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
704 /* Use collection 1 for joypad buttons */
705 0xA1, 0x02, /* MCollection Logical (interrelated data) */
707 /* Ignore the 1st byte, maybe it is used for a controller
708 * number but it's not needed for correct operation */
709 0x75, 0x08, /* GReportSize 0x08 [8] */
710 0x95, 0x01, /* GReportCount 0x01 [1] */
711 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
713 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
714 * buttons multiple keypresses are allowed */
715 0x05, 0x09, /* GUsagePage Button */
716 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
717 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
718 0x14, /* GLogicalMinimum [0] */
719 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
720 0x75, 0x01, /* GReportSize 0x01 [1] */
721 0x95, 0x18, /* GReportCount 0x18 [24] */
722 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
724 0xC0, /* MEndCollection */
726 /* Use collection 2 for remote control buttons */
727 0xA1, 0x02, /* MCollection Logical (interrelated data) */
729 /* 5th byte is used for remote control buttons */
730 0x05, 0x09, /* GUsagePage Button */
731 0x18, /* LUsageMinimum [No button pressed] */
732 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
733 0x14, /* GLogicalMinimum [0] */
734 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
735 0x75, 0x08, /* GReportSize 0x08 [8] */
736 0x95, 0x01, /* GReportCount 0x01 [1] */
739 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
740 * 0xff and 11th is for press indication */
741 0x75, 0x08, /* GReportSize 0x08 [8] */
742 0x95, 0x06, /* GReportCount 0x06 [6] */
743 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
745 /* 12th byte is for battery strength */
746 0x05, 0x06, /* GUsagePage Generic Device Controls */
747 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
748 0x14, /* GLogicalMinimum [0] */
749 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
750 0x75, 0x08, /* GReportSize 0x08 [8] */
751 0x95, 0x01, /* GReportCount 0x01 [1] */
752 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
754 0xC0, /* MEndCollection */
756 0xC0 /* MEndCollection [Game Pad] */
759 static const unsigned int ps3remote_keymap_joypad_buttons
[] = {
761 [0x02] = BTN_THUMBL
, /* L3 */
762 [0x03] = BTN_THUMBR
, /* R3 */
768 [0x09] = BTN_TL2
, /* L2 */
769 [0x0a] = BTN_TR2
, /* R2 */
770 [0x0b] = BTN_TL
, /* L1 */
771 [0x0c] = BTN_TR
, /* R1 */
772 [0x0d] = KEY_OPTION
, /* options/triangle */
773 [0x0e] = KEY_BACK
, /* back/circle */
774 [0x0f] = BTN_0
, /* cross */
775 [0x10] = KEY_SCREEN
, /* view/square */
776 [0x11] = KEY_HOMEPAGE
, /* PS button */
779 static const unsigned int ps3remote_keymap_remote_buttons
[] = {
790 [0x0e] = KEY_ESC
, /* return */
792 [0x16] = KEY_EJECTCD
,
793 [0x1a] = KEY_MENU
, /* top menu */
795 [0x30] = KEY_PREVIOUS
,
798 [0x33] = KEY_REWIND
, /* scan back */
799 [0x34] = KEY_FORWARD
, /* scan forward */
802 [0x40] = KEY_CONTEXT_MENU
, /* pop up/menu */
803 [0x60] = KEY_FRAMEBACK
, /* slow/step back */
804 [0x61] = KEY_FRAMEFORWARD
, /* slow/step forward */
805 [0x63] = KEY_SUBTITLE
,
808 [0x70] = KEY_INFO
, /* display */
815 static const unsigned int buzz_keymap
[] = {
817 * The controller has 4 remote buzzers, each with one LED and 5
820 * We use the mapping chosen by the controller, which is:
823 * -------------------
830 * So, for example, the orange button on the third buzzer is mapped to
831 * BTN_TRIGGER_HAPPY14
833 [ 1] = BTN_TRIGGER_HAPPY1
,
834 [ 2] = BTN_TRIGGER_HAPPY2
,
835 [ 3] = BTN_TRIGGER_HAPPY3
,
836 [ 4] = BTN_TRIGGER_HAPPY4
,
837 [ 5] = BTN_TRIGGER_HAPPY5
,
838 [ 6] = BTN_TRIGGER_HAPPY6
,
839 [ 7] = BTN_TRIGGER_HAPPY7
,
840 [ 8] = BTN_TRIGGER_HAPPY8
,
841 [ 9] = BTN_TRIGGER_HAPPY9
,
842 [10] = BTN_TRIGGER_HAPPY10
,
843 [11] = BTN_TRIGGER_HAPPY11
,
844 [12] = BTN_TRIGGER_HAPPY12
,
845 [13] = BTN_TRIGGER_HAPPY13
,
846 [14] = BTN_TRIGGER_HAPPY14
,
847 [15] = BTN_TRIGGER_HAPPY15
,
848 [16] = BTN_TRIGGER_HAPPY16
,
849 [17] = BTN_TRIGGER_HAPPY17
,
850 [18] = BTN_TRIGGER_HAPPY18
,
851 [19] = BTN_TRIGGER_HAPPY19
,
852 [20] = BTN_TRIGGER_HAPPY20
,
855 static enum power_supply_property sony_battery_props
[] = {
856 POWER_SUPPLY_PROP_PRESENT
,
857 POWER_SUPPLY_PROP_CAPACITY
,
858 POWER_SUPPLY_PROP_SCOPE
,
859 POWER_SUPPLY_PROP_STATUS
,
863 __u8 time_enabled
; /* the total time the led is active (0xff means forever) */
864 __u8 duty_length
; /* how long a cycle is in deciseconds (0 means "really fast") */
866 __u8 duty_off
; /* % of duty_length the led is off (0xff means 100%) */
867 __u8 duty_on
; /* % of duty_length the led is on (0xff mean 100%) */
870 struct sixaxis_rumble
{
872 __u8 right_duration
; /* Right motor duration (0xff means forever) */
873 __u8 right_motor_on
; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
874 __u8 left_duration
; /* Left motor duration (0xff means forever) */
875 __u8 left_motor_force
; /* left (large) motor, supports force values from 0 to 255 */
878 struct sixaxis_output_report
{
880 struct sixaxis_rumble rumble
;
882 __u8 leds_bitmap
; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
883 struct sixaxis_led led
[4]; /* LEDx at (4 - x) */
884 struct sixaxis_led _reserved
; /* LED5, not actually soldered */
887 union sixaxis_output_report_01
{
888 struct sixaxis_output_report data
;
892 struct motion_output_report_02
{
899 #define DS4_REPORT_0x02_SIZE 37
900 #define DS4_REPORT_0x05_SIZE 32
901 #define DS4_REPORT_0x11_SIZE 78
902 #define DS4_REPORT_0x81_SIZE 7
903 #define SIXAXIS_REPORT_0xF2_SIZE 17
904 #define SIXAXIS_REPORT_0xF5_SIZE 8
906 static DEFINE_SPINLOCK(sony_dev_list_lock
);
907 static LIST_HEAD(sony_device_list
);
908 static DEFINE_IDA(sony_device_id_allocator
);
912 struct list_head list_node
;
913 struct hid_device
*hdev
;
914 struct led_classdev
*leds
[MAX_LEDS
];
915 unsigned long quirks
;
916 struct work_struct state_worker
;
917 struct power_supply
*battery
;
918 struct power_supply_desc battery_desc
;
920 __u8
*output_report_dmabuf
;
922 #ifdef CONFIG_SONY_FF
928 __u8 worker_initialized
;
930 __u8 battery_charging
;
931 __u8 battery_capacity
;
932 __u8 led_state
[MAX_LEDS
];
933 __u8 led_delay_on
[MAX_LEDS
];
934 __u8 led_delay_off
[MAX_LEDS
];
938 static __u8
*sixaxis_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
941 *rsize
= sizeof(sixaxis_rdesc
);
942 return sixaxis_rdesc
;
945 static u8
*motion_fixup(struct hid_device
*hdev
, u8
*rdesc
,
948 *rsize
= sizeof(motion_rdesc
);
952 static __u8
*ps3remote_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
955 *rsize
= sizeof(ps3remote_rdesc
);
956 return ps3remote_rdesc
;
959 static int ps3remote_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
960 struct hid_field
*field
, struct hid_usage
*usage
,
961 unsigned long **bit
, int *max
)
963 unsigned int key
= usage
->hid
& HID_USAGE
;
965 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_BUTTON
)
968 switch (usage
->collection_index
) {
970 if (key
>= ARRAY_SIZE(ps3remote_keymap_joypad_buttons
))
973 key
= ps3remote_keymap_joypad_buttons
[key
];
978 if (key
>= ARRAY_SIZE(ps3remote_keymap_remote_buttons
))
981 key
= ps3remote_keymap_remote_buttons
[key
];
989 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_KEY
, key
);
993 static __u8
*sony_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
996 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
999 * Some Sony RF receivers wrongly declare the mouse pointer as a
1000 * a constant non-data variable.
1002 if ((sc
->quirks
& VAIO_RDESC_CONSTANT
) && *rsize
>= 56 &&
1003 /* usage page: generic desktop controls */
1004 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1006 rdesc
[2] == 0x09 && rdesc
[3] == 0x02 &&
1007 /* input (usage page for x,y axes): constant, variable, relative */
1008 rdesc
[54] == 0x81 && rdesc
[55] == 0x07) {
1009 hid_info(hdev
, "Fixing up Sony RF Receiver report descriptor\n");
1010 /* input: data, variable, relative */
1015 * The default Dualshock 4 USB descriptor doesn't assign
1016 * the gyroscope values to corresponding axes so we need a
1019 if ((sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
) && *rsize
== 467) {
1020 hid_info(hdev
, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1021 rdesc
= dualshock4_usb_rdesc
;
1022 *rsize
= sizeof(dualshock4_usb_rdesc
);
1023 } else if ((sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
) && *rsize
== 357) {
1024 hid_info(hdev
, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1025 rdesc
= dualshock4_bt_rdesc
;
1026 *rsize
= sizeof(dualshock4_bt_rdesc
);
1029 if (sc
->quirks
& SIXAXIS_CONTROLLER
)
1030 return sixaxis_fixup(hdev
, rdesc
, rsize
);
1032 if (sc
->quirks
& MOTION_CONTROLLER
)
1033 return motion_fixup(hdev
, rdesc
, rsize
);
1035 if (sc
->quirks
& PS3REMOTE
)
1036 return ps3remote_fixup(hdev
, rdesc
, rsize
);
1041 static void sixaxis_parse_report(struct sony_sc
*sc
, __u8
*rd
, int size
)
1043 static const __u8 sixaxis_battery_capacity
[] = { 0, 1, 25, 50, 75, 100 };
1044 unsigned long flags
;
1046 __u8 cable_state
, battery_capacity
, battery_charging
;
1049 * The sixaxis is charging if the battery value is 0xee
1050 * and it is fully charged if the value is 0xef.
1051 * It does not report the actual level while charging so it
1052 * is set to 100% while charging is in progress.
1054 offset
= (sc
->quirks
& MOTION_CONTROLLER
) ? 12 : 30;
1056 if (rd
[offset
] >= 0xee) {
1057 battery_capacity
= 100;
1058 battery_charging
= !(rd
[offset
] & 0x01);
1061 __u8 index
= rd
[offset
] <= 5 ? rd
[offset
] : 5;
1062 battery_capacity
= sixaxis_battery_capacity
[index
];
1063 battery_charging
= 0;
1067 spin_lock_irqsave(&sc
->lock
, flags
);
1068 sc
->cable_state
= cable_state
;
1069 sc
->battery_capacity
= battery_capacity
;
1070 sc
->battery_charging
= battery_charging
;
1071 spin_unlock_irqrestore(&sc
->lock
, flags
);
1074 static void dualshock4_parse_report(struct sony_sc
*sc
, __u8
*rd
, int size
)
1076 struct hid_input
*hidinput
= list_entry(sc
->hdev
->inputs
.next
,
1077 struct hid_input
, list
);
1078 struct input_dev
*input_dev
= hidinput
->input
;
1079 unsigned long flags
;
1081 __u8 cable_state
, battery_capacity
, battery_charging
;
1084 * Battery and touchpad data starts at byte 30 in the USB report and
1085 * 32 in Bluetooth report.
1087 offset
= (sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
) ? 30 : 32;
1090 * The lower 4 bits of byte 30 contain the battery level
1091 * and the 5th bit contains the USB cable state.
1093 cable_state
= (rd
[offset
] >> 4) & 0x01;
1094 battery_capacity
= rd
[offset
] & 0x0F;
1097 * When a USB power source is connected the battery level ranges from
1098 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1099 * A battery level above 10 when plugged in means charge completed.
1101 if (!cable_state
|| battery_capacity
> 10)
1102 battery_charging
= 0;
1104 battery_charging
= 1;
1108 if (battery_capacity
> 10)
1109 battery_capacity
= 10;
1111 battery_capacity
*= 10;
1113 spin_lock_irqsave(&sc
->lock
, flags
);
1114 sc
->cable_state
= cable_state
;
1115 sc
->battery_capacity
= battery_capacity
;
1116 sc
->battery_charging
= battery_charging
;
1117 spin_unlock_irqrestore(&sc
->lock
, flags
);
1122 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
1123 * and 37 on Bluetooth.
1124 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1125 * indicator that is 0 when pressed and 1 when not pressed.
1126 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1127 * The data for the second touch is in the same format and immediatly
1128 * follows the data for the first.
1130 for (n
= 0; n
< 2; n
++) {
1133 x
= rd
[offset
+1] | ((rd
[offset
+2] & 0xF) << 8);
1134 y
= ((rd
[offset
+2] & 0xF0) >> 4) | (rd
[offset
+3] << 4);
1136 input_mt_slot(input_dev
, n
);
1137 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
,
1138 !(rd
[offset
] >> 7));
1139 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
1140 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
1146 static int sony_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1149 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
1152 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
1153 * has to be BYTE_SWAPPED before passing up to joystick interface
1155 if ((sc
->quirks
& SIXAXIS_CONTROLLER
) && rd
[0] == 0x01 && size
== 49) {
1156 swap(rd
[41], rd
[42]);
1157 swap(rd
[43], rd
[44]);
1158 swap(rd
[45], rd
[46]);
1159 swap(rd
[47], rd
[48]);
1161 sixaxis_parse_report(sc
, rd
, size
);
1162 } else if ((sc
->quirks
& MOTION_CONTROLLER_BT
) && rd
[0] == 0x01 && size
== 49) {
1163 sixaxis_parse_report(sc
, rd
, size
);
1164 } else if (((sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
) && rd
[0] == 0x01 &&
1165 size
== 64) || ((sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
)
1166 && rd
[0] == 0x11 && size
== 78)) {
1167 dualshock4_parse_report(sc
, rd
, size
);
1173 static int sony_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
1174 struct hid_field
*field
, struct hid_usage
*usage
,
1175 unsigned long **bit
, int *max
)
1177 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
1179 if (sc
->quirks
& BUZZ_CONTROLLER
) {
1180 unsigned int key
= usage
->hid
& HID_USAGE
;
1182 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_BUTTON
)
1185 switch (usage
->collection_index
) {
1187 if (key
>= ARRAY_SIZE(buzz_keymap
))
1190 key
= buzz_keymap
[key
];
1198 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_KEY
, key
);
1202 if (sc
->quirks
& PS3REMOTE
)
1203 return ps3remote_mapping(hdev
, hi
, field
, usage
, bit
, max
);
1205 /* Let hid-core decide for the others */
1209 static int sony_register_touchpad(struct hid_input
*hi
, int touch_count
,
1212 struct input_dev
*input_dev
= hi
->input
;
1215 ret
= input_mt_init_slots(input_dev
, touch_count
, 0);
1219 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, w
, 0, 0);
1220 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, h
, 0, 0);
1225 static void sony_input_configured(struct hid_device
*hdev
,
1226 struct hid_input
*hidinput
)
1228 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
1231 * The Dualshock 4 touchpad supports 2 touches and has a
1232 * resolution of 1920x942 (44.86 dots/mm).
1234 if (sc
->quirks
& DUALSHOCK4_CONTROLLER
) {
1235 if (sony_register_touchpad(hidinput
, 2, 1920, 942) != 0)
1237 "Unable to initialize multi-touch slots\n");
1242 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1243 * to "operational". Without this, the ps3 controller will not report any
1246 static int sixaxis_set_operational_usb(struct hid_device
*hdev
)
1248 const int buf_size
=
1249 max(SIXAXIS_REPORT_0xF2_SIZE
, SIXAXIS_REPORT_0xF5_SIZE
);
1253 buf
= kmalloc(buf_size
, GFP_KERNEL
);
1257 ret
= hid_hw_raw_request(hdev
, 0xf2, buf
, SIXAXIS_REPORT_0xF2_SIZE
,
1258 HID_FEATURE_REPORT
, HID_REQ_GET_REPORT
);
1260 hid_err(hdev
, "can't set operational mode: step 1\n");
1265 * Some compatible controllers like the Speedlink Strike FX and
1266 * Gasia need another query plus an USB interrupt to get operational.
1268 ret
= hid_hw_raw_request(hdev
, 0xf5, buf
, SIXAXIS_REPORT_0xF5_SIZE
,
1269 HID_FEATURE_REPORT
, HID_REQ_GET_REPORT
);
1271 hid_err(hdev
, "can't set operational mode: step 2\n");
1275 ret
= hid_hw_output_report(hdev
, buf
, 1);
1277 hid_err(hdev
, "can't set operational mode: step 3\n");
1285 static int sixaxis_set_operational_bt(struct hid_device
*hdev
)
1287 static const __u8 report
[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1291 buf
= kmemdup(report
, sizeof(report
), GFP_KERNEL
);
1295 ret
= hid_hw_raw_request(hdev
, buf
[0], buf
, sizeof(report
),
1296 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
1304 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
1305 * controller so that it sends full input reports of type 0x11.
1307 static int dualshock4_set_operational_bt(struct hid_device
*hdev
)
1312 buf
= kmalloc(DS4_REPORT_0x02_SIZE
, GFP_KERNEL
);
1316 ret
= hid_hw_raw_request(hdev
, 0x02, buf
, DS4_REPORT_0x02_SIZE
,
1317 HID_FEATURE_REPORT
, HID_REQ_GET_REPORT
);
1324 static void sixaxis_set_leds_from_id(struct sony_sc
*sc
)
1326 static const __u8 sixaxis_leds
[10][4] = {
1327 { 0x01, 0x00, 0x00, 0x00 },
1328 { 0x00, 0x01, 0x00, 0x00 },
1329 { 0x00, 0x00, 0x01, 0x00 },
1330 { 0x00, 0x00, 0x00, 0x01 },
1331 { 0x01, 0x00, 0x00, 0x01 },
1332 { 0x00, 0x01, 0x00, 0x01 },
1333 { 0x00, 0x00, 0x01, 0x01 },
1334 { 0x01, 0x00, 0x01, 0x01 },
1335 { 0x00, 0x01, 0x01, 0x01 },
1336 { 0x01, 0x01, 0x01, 0x01 }
1339 int id
= sc
->device_id
;
1341 BUILD_BUG_ON(MAX_LEDS
< ARRAY_SIZE(sixaxis_leds
[0]));
1347 memcpy(sc
->led_state
, sixaxis_leds
[id
], sizeof(sixaxis_leds
[id
]));
1350 static void dualshock4_set_leds_from_id(struct sony_sc
*sc
)
1352 /* The first 4 color/index entries match what the PS4 assigns */
1353 static const __u8 color_code
[7][3] = {
1354 /* Blue */ { 0x00, 0x00, 0x01 },
1355 /* Red */ { 0x01, 0x00, 0x00 },
1356 /* Green */ { 0x00, 0x01, 0x00 },
1357 /* Pink */ { 0x02, 0x00, 0x01 },
1358 /* Orange */ { 0x02, 0x01, 0x00 },
1359 /* Teal */ { 0x00, 0x01, 0x01 },
1360 /* White */ { 0x01, 0x01, 0x01 }
1363 int id
= sc
->device_id
;
1365 BUILD_BUG_ON(MAX_LEDS
< ARRAY_SIZE(color_code
[0]));
1371 memcpy(sc
->led_state
, color_code
[id
], sizeof(color_code
[id
]));
1374 static void buzz_set_leds(struct sony_sc
*sc
)
1376 struct hid_device
*hdev
= sc
->hdev
;
1377 struct list_head
*report_list
=
1378 &hdev
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
1379 struct hid_report
*report
= list_entry(report_list
->next
,
1380 struct hid_report
, list
);
1381 __s32
*value
= report
->field
[0]->value
;
1383 BUILD_BUG_ON(MAX_LEDS
< 4);
1386 value
[1] = sc
->led_state
[0] ? 0xff : 0x00;
1387 value
[2] = sc
->led_state
[1] ? 0xff : 0x00;
1388 value
[3] = sc
->led_state
[2] ? 0xff : 0x00;
1389 value
[4] = sc
->led_state
[3] ? 0xff : 0x00;
1392 hid_hw_request(hdev
, report
, HID_REQ_SET_REPORT
);
1395 static void sony_set_leds(struct sony_sc
*sc
)
1397 if (!(sc
->quirks
& BUZZ_CONTROLLER
))
1398 schedule_work(&sc
->state_worker
);
1403 static void sony_led_set_brightness(struct led_classdev
*led
,
1404 enum led_brightness value
)
1406 struct device
*dev
= led
->dev
->parent
;
1407 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
1408 struct sony_sc
*drv_data
;
1413 drv_data
= hid_get_drvdata(hdev
);
1415 hid_err(hdev
, "No device data\n");
1420 * The Sixaxis on USB will override any LED settings sent to it
1421 * and keep flashing all of the LEDs until the PS button is pressed.
1422 * Updates, even if redundant, must be always be sent to the
1423 * controller to avoid having to toggle the state of an LED just to
1424 * stop the flashing later on.
1426 force_update
= !!(drv_data
->quirks
& SIXAXIS_CONTROLLER_USB
);
1428 for (n
= 0; n
< drv_data
->led_count
; n
++) {
1429 if (led
== drv_data
->leds
[n
] && (force_update
||
1430 (value
!= drv_data
->led_state
[n
] ||
1431 drv_data
->led_delay_on
[n
] ||
1432 drv_data
->led_delay_off
[n
]))) {
1434 drv_data
->led_state
[n
] = value
;
1436 /* Setting the brightness stops the blinking */
1437 drv_data
->led_delay_on
[n
] = 0;
1438 drv_data
->led_delay_off
[n
] = 0;
1440 sony_set_leds(drv_data
);
1446 static enum led_brightness
sony_led_get_brightness(struct led_classdev
*led
)
1448 struct device
*dev
= led
->dev
->parent
;
1449 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
1450 struct sony_sc
*drv_data
;
1454 drv_data
= hid_get_drvdata(hdev
);
1456 hid_err(hdev
, "No device data\n");
1460 for (n
= 0; n
< drv_data
->led_count
; n
++) {
1461 if (led
== drv_data
->leds
[n
])
1462 return drv_data
->led_state
[n
];
1468 static int sony_led_blink_set(struct led_classdev
*led
, unsigned long *delay_on
,
1469 unsigned long *delay_off
)
1471 struct device
*dev
= led
->dev
->parent
;
1472 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
1473 struct sony_sc
*drv_data
= hid_get_drvdata(hdev
);
1475 __u8 new_on
, new_off
;
1478 hid_err(hdev
, "No device data\n");
1482 /* Max delay is 255 deciseconds or 2550 milliseconds */
1483 if (*delay_on
> 2550)
1485 if (*delay_off
> 2550)
1488 /* Blink at 1 Hz if both values are zero */
1489 if (!*delay_on
&& !*delay_off
)
1490 *delay_on
= *delay_off
= 500;
1492 new_on
= *delay_on
/ 10;
1493 new_off
= *delay_off
/ 10;
1495 for (n
= 0; n
< drv_data
->led_count
; n
++) {
1496 if (led
== drv_data
->leds
[n
])
1500 /* This LED is not registered on this device */
1501 if (n
>= drv_data
->led_count
)
1504 /* Don't schedule work if the values didn't change */
1505 if (new_on
!= drv_data
->led_delay_on
[n
] ||
1506 new_off
!= drv_data
->led_delay_off
[n
]) {
1507 drv_data
->led_delay_on
[n
] = new_on
;
1508 drv_data
->led_delay_off
[n
] = new_off
;
1509 schedule_work(&drv_data
->state_worker
);
1515 static void sony_leds_remove(struct sony_sc
*sc
)
1517 struct led_classdev
*led
;
1520 BUG_ON(!(sc
->quirks
& SONY_LED_SUPPORT
));
1522 for (n
= 0; n
< sc
->led_count
; n
++) {
1527 led_classdev_unregister(led
);
1534 static int sony_leds_init(struct sony_sc
*sc
)
1536 struct hid_device
*hdev
= sc
->hdev
;
1539 struct led_classdev
*led
;
1543 const char *name_fmt
;
1544 static const char * const ds4_name_str
[] = { "red", "green", "blue",
1546 __u8 max_brightness
[MAX_LEDS
] = { [0 ... (MAX_LEDS
- 1)] = 1 };
1547 __u8 use_hw_blink
[MAX_LEDS
] = { 0 };
1549 BUG_ON(!(sc
->quirks
& SONY_LED_SUPPORT
));
1551 if (sc
->quirks
& BUZZ_CONTROLLER
) {
1554 name_len
= strlen("::buzz#");
1555 name_fmt
= "%s::buzz%d";
1556 /* Validate expected report characteristics. */
1557 if (!hid_validate_values(hdev
, HID_OUTPUT_REPORT
, 0, 0, 7))
1559 } else if (sc
->quirks
& DUALSHOCK4_CONTROLLER
) {
1560 dualshock4_set_leds_from_id(sc
);
1561 sc
->led_state
[3] = 1;
1563 memset(max_brightness
, 255, 3);
1564 use_hw_blink
[3] = 1;
1568 } else if (sc
->quirks
& MOTION_CONTROLLER
) {
1570 memset(max_brightness
, 255, 3);
1575 sixaxis_set_leds_from_id(sc
);
1577 memset(use_hw_blink
, 1, 4);
1579 name_len
= strlen("::sony#");
1580 name_fmt
= "%s::sony%d";
1584 * Clear LEDs as we have no way of reading their initial state. This is
1585 * only relevant if the driver is loaded after somebody actively set the
1590 name_sz
= strlen(dev_name(&hdev
->dev
)) + name_len
+ 1;
1592 for (n
= 0; n
< sc
->led_count
; n
++) {
1595 name_sz
= strlen(dev_name(&hdev
->dev
)) + strlen(ds4_name_str
[n
]) + 2;
1597 led
= kzalloc(sizeof(struct led_classdev
) + name_sz
, GFP_KERNEL
);
1599 hid_err(hdev
, "Couldn't allocate memory for LED %d\n", n
);
1604 name
= (void *)(&led
[1]);
1606 snprintf(name
, name_sz
, name_fmt
, dev_name(&hdev
->dev
),
1609 snprintf(name
, name_sz
, name_fmt
, dev_name(&hdev
->dev
), n
+ 1);
1611 led
->brightness
= sc
->led_state
[n
];
1612 led
->max_brightness
= max_brightness
[n
];
1613 led
->brightness_get
= sony_led_get_brightness
;
1614 led
->brightness_set
= sony_led_set_brightness
;
1616 if (use_hw_blink
[n
])
1617 led
->blink_set
= sony_led_blink_set
;
1621 ret
= led_classdev_register(&hdev
->dev
, led
);
1623 hid_err(hdev
, "Failed to register LED %d\n", n
);
1633 sony_leds_remove(sc
);
1638 static void sixaxis_state_worker(struct work_struct
*work
)
1640 static const union sixaxis_output_report_01 default_report
= {
1643 0x00, 0xff, 0x00, 0xff, 0x00,
1644 0x00, 0x00, 0x00, 0x00, 0x00,
1645 0xff, 0x27, 0x10, 0x00, 0x32,
1646 0xff, 0x27, 0x10, 0x00, 0x32,
1647 0xff, 0x27, 0x10, 0x00, 0x32,
1648 0xff, 0x27, 0x10, 0x00, 0x32,
1649 0x00, 0x00, 0x00, 0x00, 0x00
1652 struct sony_sc
*sc
= container_of(work
, struct sony_sc
, state_worker
);
1653 struct sixaxis_output_report
*report
=
1654 (struct sixaxis_output_report
*)sc
->output_report_dmabuf
;
1657 /* Initialize the report with default values */
1658 memcpy(report
, &default_report
, sizeof(struct sixaxis_output_report
));
1660 #ifdef CONFIG_SONY_FF
1661 report
->rumble
.right_motor_on
= sc
->right
? 1 : 0;
1662 report
->rumble
.left_motor_force
= sc
->left
;
1665 report
->leds_bitmap
|= sc
->led_state
[0] << 1;
1666 report
->leds_bitmap
|= sc
->led_state
[1] << 2;
1667 report
->leds_bitmap
|= sc
->led_state
[2] << 3;
1668 report
->leds_bitmap
|= sc
->led_state
[3] << 4;
1670 /* Set flag for all leds off, required for 3rd party INTEC controller */
1671 if ((report
->leds_bitmap
& 0x1E) == 0)
1672 report
->leds_bitmap
|= 0x20;
1675 * The LEDs in the report are indexed in reverse order to their
1676 * corresponding light on the controller.
1677 * Index 0 = LED 4, index 1 = LED 3, etc...
1679 * In the case of both delay values being zero (blinking disabled) the
1680 * default report values should be used or the controller LED will be
1683 for (n
= 0; n
< 4; n
++) {
1684 if (sc
->led_delay_on
[n
] || sc
->led_delay_off
[n
]) {
1685 report
->led
[3 - n
].duty_off
= sc
->led_delay_off
[n
];
1686 report
->led
[3 - n
].duty_on
= sc
->led_delay_on
[n
];
1690 hid_hw_raw_request(sc
->hdev
, report
->report_id
, (__u8
*)report
,
1691 sizeof(struct sixaxis_output_report
),
1692 HID_OUTPUT_REPORT
, HID_REQ_SET_REPORT
);
1695 static void dualshock4_state_worker(struct work_struct
*work
)
1697 struct sony_sc
*sc
= container_of(work
, struct sony_sc
, state_worker
);
1698 struct hid_device
*hdev
= sc
->hdev
;
1699 __u8
*buf
= sc
->output_report_dmabuf
;
1702 if (sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
) {
1703 memset(buf
, 0, DS4_REPORT_0x05_SIZE
);
1708 memset(buf
, 0, DS4_REPORT_0x11_SIZE
);
1715 #ifdef CONFIG_SONY_FF
1716 buf
[offset
++] = sc
->right
;
1717 buf
[offset
++] = sc
->left
;
1722 /* LED 3 is the global control */
1723 if (sc
->led_state
[3]) {
1724 buf
[offset
++] = sc
->led_state
[0];
1725 buf
[offset
++] = sc
->led_state
[1];
1726 buf
[offset
++] = sc
->led_state
[2];
1731 /* If both delay values are zero the DualShock 4 disables blinking. */
1732 buf
[offset
++] = sc
->led_delay_on
[3];
1733 buf
[offset
++] = sc
->led_delay_off
[3];
1735 if (sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
)
1736 hid_hw_output_report(hdev
, buf
, DS4_REPORT_0x05_SIZE
);
1738 hid_hw_raw_request(hdev
, 0x11, buf
, DS4_REPORT_0x11_SIZE
,
1739 HID_OUTPUT_REPORT
, HID_REQ_SET_REPORT
);
1742 static void motion_state_worker(struct work_struct
*work
)
1744 struct sony_sc
*sc
= container_of(work
, struct sony_sc
, state_worker
);
1745 struct hid_device
*hdev
= sc
->hdev
;
1746 struct motion_output_report_02
*report
=
1747 (struct motion_output_report_02
*)sc
->output_report_dmabuf
;
1749 memset(report
, 0, sizeof(struct motion_output_report_02
));
1751 report
->type
= 0x02; /* set leds */
1752 report
->r
= sc
->led_state
[0];
1753 report
->g
= sc
->led_state
[1];
1754 report
->b
= sc
->led_state
[2];
1756 #ifdef CONFIG_SONY_FF
1757 report
->rumble
= max(sc
->right
, sc
->left
);
1760 hid_hw_output_report(hdev
, (__u8
*)report
,
1761 sizeof(struct motion_output_report_02
));
1764 static int sony_allocate_output_report(struct sony_sc
*sc
)
1766 if (sc
->quirks
& SIXAXIS_CONTROLLER
)
1767 sc
->output_report_dmabuf
=
1768 kmalloc(sizeof(union sixaxis_output_report_01
),
1770 else if (sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
)
1771 sc
->output_report_dmabuf
= kmalloc(DS4_REPORT_0x11_SIZE
,
1773 else if (sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
)
1774 sc
->output_report_dmabuf
= kmalloc(DS4_REPORT_0x05_SIZE
,
1776 else if (sc
->quirks
& MOTION_CONTROLLER
)
1777 sc
->output_report_dmabuf
=
1778 kmalloc(sizeof(struct motion_output_report_02
),
1783 if (!sc
->output_report_dmabuf
)
1789 #ifdef CONFIG_SONY_FF
1790 static int sony_play_effect(struct input_dev
*dev
, void *data
,
1791 struct ff_effect
*effect
)
1793 struct hid_device
*hid
= input_get_drvdata(dev
);
1794 struct sony_sc
*sc
= hid_get_drvdata(hid
);
1796 if (effect
->type
!= FF_RUMBLE
)
1799 sc
->left
= effect
->u
.rumble
.strong_magnitude
/ 256;
1800 sc
->right
= effect
->u
.rumble
.weak_magnitude
/ 256;
1802 schedule_work(&sc
->state_worker
);
1806 static int sony_init_ff(struct sony_sc
*sc
)
1808 struct hid_input
*hidinput
= list_entry(sc
->hdev
->inputs
.next
,
1809 struct hid_input
, list
);
1810 struct input_dev
*input_dev
= hidinput
->input
;
1812 input_set_capability(input_dev
, EV_FF
, FF_RUMBLE
);
1813 return input_ff_create_memless(input_dev
, NULL
, sony_play_effect
);
1817 static int sony_init_ff(struct sony_sc
*sc
)
1824 static int sony_battery_get_property(struct power_supply
*psy
,
1825 enum power_supply_property psp
,
1826 union power_supply_propval
*val
)
1828 struct sony_sc
*sc
= power_supply_get_drvdata(psy
);
1829 unsigned long flags
;
1831 u8 battery_charging
, battery_capacity
, cable_state
;
1833 spin_lock_irqsave(&sc
->lock
, flags
);
1834 battery_charging
= sc
->battery_charging
;
1835 battery_capacity
= sc
->battery_capacity
;
1836 cable_state
= sc
->cable_state
;
1837 spin_unlock_irqrestore(&sc
->lock
, flags
);
1840 case POWER_SUPPLY_PROP_PRESENT
:
1843 case POWER_SUPPLY_PROP_SCOPE
:
1844 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1846 case POWER_SUPPLY_PROP_CAPACITY
:
1847 val
->intval
= battery_capacity
;
1849 case POWER_SUPPLY_PROP_STATUS
:
1850 if (battery_charging
)
1851 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1853 if (battery_capacity
== 100 && cable_state
)
1854 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1856 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
1865 static int sony_battery_probe(struct sony_sc
*sc
)
1867 struct power_supply_config psy_cfg
= { .drv_data
= sc
, };
1868 struct hid_device
*hdev
= sc
->hdev
;
1872 * Set the default battery level to 100% to avoid low battery warnings
1873 * if the battery is polled before the first device report is received.
1875 sc
->battery_capacity
= 100;
1877 sc
->battery_desc
.properties
= sony_battery_props
;
1878 sc
->battery_desc
.num_properties
= ARRAY_SIZE(sony_battery_props
);
1879 sc
->battery_desc
.get_property
= sony_battery_get_property
;
1880 sc
->battery_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
1881 sc
->battery_desc
.use_for_apm
= 0;
1882 sc
->battery_desc
.name
= kasprintf(GFP_KERNEL
,
1883 "sony_controller_battery_%pMR",
1885 if (!sc
->battery_desc
.name
)
1888 sc
->battery
= power_supply_register(&hdev
->dev
, &sc
->battery_desc
,
1890 if (IS_ERR(sc
->battery
)) {
1891 ret
= PTR_ERR(sc
->battery
);
1892 hid_err(hdev
, "Unable to register battery device\n");
1896 power_supply_powers(sc
->battery
, &hdev
->dev
);
1900 kfree(sc
->battery_desc
.name
);
1901 sc
->battery_desc
.name
= NULL
;
1905 static void sony_battery_remove(struct sony_sc
*sc
)
1907 if (!sc
->battery_desc
.name
)
1910 power_supply_unregister(sc
->battery
);
1911 kfree(sc
->battery_desc
.name
);
1912 sc
->battery_desc
.name
= NULL
;
1916 * If a controller is plugged in via USB while already connected via Bluetooth
1917 * it will show up as two devices. A global list of connected controllers and
1918 * their MAC addresses is maintained to ensure that a device is only connected
1921 static int sony_check_add_dev_list(struct sony_sc
*sc
)
1923 struct sony_sc
*entry
;
1924 unsigned long flags
;
1927 spin_lock_irqsave(&sony_dev_list_lock
, flags
);
1929 list_for_each_entry(entry
, &sony_device_list
, list_node
) {
1930 ret
= memcmp(sc
->mac_address
, entry
->mac_address
,
1931 sizeof(sc
->mac_address
));
1934 hid_info(sc
->hdev
, "controller with MAC address %pMR already connected\n",
1941 list_add(&(sc
->list_node
), &sony_device_list
);
1944 spin_unlock_irqrestore(&sony_dev_list_lock
, flags
);
1948 static void sony_remove_dev_list(struct sony_sc
*sc
)
1950 unsigned long flags
;
1952 if (sc
->list_node
.next
) {
1953 spin_lock_irqsave(&sony_dev_list_lock
, flags
);
1954 list_del(&(sc
->list_node
));
1955 spin_unlock_irqrestore(&sony_dev_list_lock
, flags
);
1959 static int sony_get_bt_devaddr(struct sony_sc
*sc
)
1963 /* HIDP stores the device MAC address as a string in the uniq field. */
1964 ret
= strlen(sc
->hdev
->uniq
);
1968 ret
= sscanf(sc
->hdev
->uniq
,
1969 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1970 &sc
->mac_address
[5], &sc
->mac_address
[4], &sc
->mac_address
[3],
1971 &sc
->mac_address
[2], &sc
->mac_address
[1], &sc
->mac_address
[0]);
1979 static int sony_check_add(struct sony_sc
*sc
)
1984 if ((sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
) ||
1985 (sc
->quirks
& MOTION_CONTROLLER_BT
) ||
1986 (sc
->quirks
& SIXAXIS_CONTROLLER_BT
)) {
1988 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1989 * address from the uniq string where HIDP stores it.
1990 * As uniq cannot be guaranteed to be a MAC address in all cases
1991 * a failure of this function should not prevent the connection.
1993 if (sony_get_bt_devaddr(sc
) < 0) {
1994 hid_warn(sc
->hdev
, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1997 } else if (sc
->quirks
& DUALSHOCK4_CONTROLLER_USB
) {
1998 buf
= kmalloc(DS4_REPORT_0x81_SIZE
, GFP_KERNEL
);
2003 * The MAC address of a DS4 controller connected via USB can be
2004 * retrieved with feature report 0x81. The address begins at
2007 ret
= hid_hw_raw_request(sc
->hdev
, 0x81, buf
,
2008 DS4_REPORT_0x81_SIZE
, HID_FEATURE_REPORT
,
2009 HID_REQ_GET_REPORT
);
2011 if (ret
!= DS4_REPORT_0x81_SIZE
) {
2012 hid_err(sc
->hdev
, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
2013 ret
= ret
< 0 ? ret
: -EINVAL
;
2017 memcpy(sc
->mac_address
, &buf
[1], sizeof(sc
->mac_address
));
2018 } else if (sc
->quirks
& SIXAXIS_CONTROLLER_USB
) {
2019 buf
= kmalloc(SIXAXIS_REPORT_0xF2_SIZE
, GFP_KERNEL
);
2024 * The MAC address of a Sixaxis controller connected via USB can
2025 * be retrieved with feature report 0xf2. The address begins at
2028 ret
= hid_hw_raw_request(sc
->hdev
, 0xf2, buf
,
2029 SIXAXIS_REPORT_0xF2_SIZE
, HID_FEATURE_REPORT
,
2030 HID_REQ_GET_REPORT
);
2032 if (ret
!= SIXAXIS_REPORT_0xF2_SIZE
) {
2033 hid_err(sc
->hdev
, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
2034 ret
= ret
< 0 ? ret
: -EINVAL
;
2039 * The Sixaxis device MAC in the report is big-endian and must
2042 for (n
= 0; n
< 6; n
++)
2043 sc
->mac_address
[5-n
] = buf
[4+n
];
2048 ret
= sony_check_add_dev_list(sc
);
2057 static int sony_set_device_id(struct sony_sc
*sc
)
2062 * Only DualShock 4 or Sixaxis controllers get an id.
2063 * All others are set to -1.
2065 if ((sc
->quirks
& SIXAXIS_CONTROLLER
) ||
2066 (sc
->quirks
& DUALSHOCK4_CONTROLLER
)) {
2067 ret
= ida_simple_get(&sony_device_id_allocator
, 0, 0,
2073 sc
->device_id
= ret
;
2081 static void sony_release_device_id(struct sony_sc
*sc
)
2083 if (sc
->device_id
>= 0) {
2084 ida_simple_remove(&sony_device_id_allocator
, sc
->device_id
);
2089 static inline void sony_init_work(struct sony_sc
*sc
,
2090 void (*worker
)(struct work_struct
*))
2092 if (!sc
->worker_initialized
)
2093 INIT_WORK(&sc
->state_worker
, worker
);
2095 sc
->worker_initialized
= 1;
2098 static inline void sony_cancel_work_sync(struct sony_sc
*sc
)
2100 if (sc
->worker_initialized
)
2101 cancel_work_sync(&sc
->state_worker
);
2104 static int sony_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
2107 unsigned long quirks
= id
->driver_data
;
2109 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
2111 sc
= devm_kzalloc(&hdev
->dev
, sizeof(*sc
), GFP_KERNEL
);
2113 hid_err(hdev
, "can't alloc sony descriptor\n");
2117 spin_lock_init(&sc
->lock
);
2119 sc
->quirks
= quirks
;
2120 hid_set_drvdata(hdev
, sc
);
2123 ret
= hid_parse(hdev
);
2125 hid_err(hdev
, "parse failed\n");
2129 if (sc
->quirks
& VAIO_RDESC_CONSTANT
)
2130 connect_mask
|= HID_CONNECT_HIDDEV_FORCE
;
2131 else if (sc
->quirks
& SIXAXIS_CONTROLLER
)
2132 connect_mask
|= HID_CONNECT_HIDDEV_FORCE
;
2134 ret
= hid_hw_start(hdev
, connect_mask
);
2136 hid_err(hdev
, "hw start failed\n");
2140 ret
= sony_set_device_id(sc
);
2142 hid_err(hdev
, "failed to allocate the device id\n");
2146 ret
= sony_allocate_output_report(sc
);
2148 hid_err(hdev
, "failed to allocate the output report buffer\n");
2152 if (sc
->quirks
& SIXAXIS_CONTROLLER_USB
) {
2154 * The Sony Sixaxis does not handle HID Output Reports on the
2155 * Interrupt EP like it could, so we need to force HID Output
2156 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2158 * There is also another issue about HID Output Reports via USB,
2159 * the Sixaxis does not want the report_id as part of the data
2160 * packet, so we have to discard buf[0] when sending the actual
2161 * control message, even for numbered reports, humpf!
2163 hdev
->quirks
|= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP
;
2164 hdev
->quirks
|= HID_QUIRK_SKIP_OUTPUT_REPORT_ID
;
2165 ret
= sixaxis_set_operational_usb(hdev
);
2166 sony_init_work(sc
, sixaxis_state_worker
);
2167 } else if (sc
->quirks
& SIXAXIS_CONTROLLER_BT
) {
2169 * The Sixaxis wants output reports sent on the ctrl endpoint
2170 * when connected via Bluetooth.
2172 hdev
->quirks
|= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP
;
2173 ret
= sixaxis_set_operational_bt(hdev
);
2174 sony_init_work(sc
, sixaxis_state_worker
);
2175 } else if (sc
->quirks
& DUALSHOCK4_CONTROLLER
) {
2176 if (sc
->quirks
& DUALSHOCK4_CONTROLLER_BT
) {
2178 * The DualShock 4 wants output reports sent on the ctrl
2179 * endpoint when connected via Bluetooth.
2181 hdev
->quirks
|= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP
;
2182 ret
= dualshock4_set_operational_bt(hdev
);
2184 hid_err(hdev
, "failed to set the Dualshock 4 operational mode\n");
2189 sony_init_work(sc
, dualshock4_state_worker
);
2190 } else if (sc
->quirks
& MOTION_CONTROLLER
) {
2191 sony_init_work(sc
, motion_state_worker
);
2199 ret
= sony_check_add(sc
);
2203 if (sc
->quirks
& SONY_LED_SUPPORT
) {
2204 ret
= sony_leds_init(sc
);
2209 if (sc
->quirks
& SONY_BATTERY_SUPPORT
) {
2210 ret
= sony_battery_probe(sc
);
2214 /* Open the device to receive reports with battery info */
2215 ret
= hid_hw_open(hdev
);
2217 hid_err(hdev
, "hw open failed\n");
2222 if (sc
->quirks
& SONY_FF_SUPPORT
) {
2223 ret
= sony_init_ff(sc
);
2232 if (sc
->quirks
& SONY_LED_SUPPORT
)
2233 sony_leds_remove(sc
);
2234 if (sc
->quirks
& SONY_BATTERY_SUPPORT
)
2235 sony_battery_remove(sc
);
2236 sony_cancel_work_sync(sc
);
2237 kfree(sc
->output_report_dmabuf
);
2238 sony_remove_dev_list(sc
);
2239 sony_release_device_id(sc
);
2244 static void sony_remove(struct hid_device
*hdev
)
2246 struct sony_sc
*sc
= hid_get_drvdata(hdev
);
2248 if (sc
->quirks
& SONY_LED_SUPPORT
)
2249 sony_leds_remove(sc
);
2251 if (sc
->quirks
& SONY_BATTERY_SUPPORT
) {
2253 sony_battery_remove(sc
);
2256 sony_cancel_work_sync(sc
);
2258 kfree(sc
->output_report_dmabuf
);
2260 sony_remove_dev_list(sc
);
2262 sony_release_device_id(sc
);
2267 static const struct hid_device_id sony_devices
[] = {
2268 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS3_CONTROLLER
),
2269 .driver_data
= SIXAXIS_CONTROLLER_USB
},
2270 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER
),
2271 .driver_data
= SIXAXIS_CONTROLLER_USB
},
2272 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_MOTION_CONTROLLER
),
2273 .driver_data
= MOTION_CONTROLLER_USB
},
2274 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_MOTION_CONTROLLER
),
2275 .driver_data
= MOTION_CONTROLLER_BT
},
2276 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS3_CONTROLLER
),
2277 .driver_data
= SIXAXIS_CONTROLLER_BT
},
2278 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE
),
2279 .driver_data
= VAIO_RDESC_CONSTANT
},
2280 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE
),
2281 .driver_data
= VAIO_RDESC_CONSTANT
},
2282 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2283 * Logitech joystick from the device descriptor. */
2284 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER
),
2285 .driver_data
= BUZZ_CONTROLLER
},
2286 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER
),
2287 .driver_data
= BUZZ_CONTROLLER
},
2288 /* PS3 BD Remote Control */
2289 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS3_BDREMOTE
),
2290 .driver_data
= PS3REMOTE
},
2291 /* Logitech Harmony Adapter for PS3 */
2292 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, USB_DEVICE_ID_LOGITECH_HARMONY_PS3
),
2293 .driver_data
= PS3REMOTE
},
2294 /* SMK-Link PS3 BD Remote Control */
2295 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK
, USB_DEVICE_ID_SMK_PS3_BDREMOTE
),
2296 .driver_data
= PS3REMOTE
},
2297 /* Sony Dualshock 4 controllers for PS4 */
2298 { HID_USB_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER
),
2299 .driver_data
= DUALSHOCK4_CONTROLLER_USB
},
2300 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY
, USB_DEVICE_ID_SONY_PS4_CONTROLLER
),
2301 .driver_data
= DUALSHOCK4_CONTROLLER_BT
},
2304 MODULE_DEVICE_TABLE(hid
, sony_devices
);
2306 static struct hid_driver sony_driver
= {
2308 .id_table
= sony_devices
,
2309 .input_mapping
= sony_mapping
,
2310 .input_configured
= sony_input_configured
,
2311 .probe
= sony_probe
,
2312 .remove
= sony_remove
,
2313 .report_fixup
= sony_report_fixup
,
2314 .raw_event
= sony_raw_event
2317 static int __init
sony_init(void)
2319 dbg_hid("Sony:%s\n", __func__
);
2321 return hid_register_driver(&sony_driver
);
2324 static void __exit
sony_exit(void)
2326 dbg_hid("Sony:%s\n", __func__
);
2328 hid_unregister_driver(&sony_driver
);
2329 ida_destroy(&sony_device_id_allocator
);
2331 module_init(sony_init
);
2332 module_exit(sony_exit
);
2334 MODULE_LICENSE("GPL");