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, 0x01, /* Report Size (1), */
157 0x95, 0x15, /* Report Count (21), */
158 0x15, 0x00, /* Logical Minimum (0), */
159 0x25, 0x01, /* Logical Maximum (1), */
160 0x35, 0x00, /* Physical Minimum (0), */
161 0x45, 0x01, /* Physical Maximum (1), */
162 0x05, 0x09, /* Usage Page (Button), */
163 0x19, 0x01, /* Usage Minimum (01h), */
164 0x29, 0x15, /* Usage Maximum (15h), */
165 0x81, 0x02, /* Input (Variable), * Buttons */
166 0x95, 0x0B, /* Report Count (11), */
167 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
168 0x81, 0x03, /* Input (Constant, Variable), * Padding */
169 0x15, 0x00, /* Logical Minimum (0), */
170 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
171 0x05, 0x01, /* Usage Page (Desktop), */
172 0xA1, 0x00, /* Collection (Physical), */
173 0x75, 0x08, /* Report Size (8), */
174 0x95, 0x01, /* Report Count (1), */
175 0x35, 0x00, /* Physical Minimum (0), */
176 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
177 0x09, 0x30, /* Usage (X), */
178 0x81, 0x02, /* Input (Variable), * Trigger */
179 0xC0, /* End Collection, */
180 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
181 0x75, 0x08, /* Report Size (8), */
182 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
183 0x81, 0x02, /* Input (Variable), */
184 0x05, 0x01, /* Usage Page (Desktop), */
185 0x75, 0x10, /* Report Size (16), */
186 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
187 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
188 0x95, 0x03, /* Report Count (3), * 3x Accels */
189 0x09, 0x33, /* Usage (rX), */
190 0x09, 0x34, /* Usage (rY), */
191 0x09, 0x35, /* Usage (rZ), */
192 0x81, 0x02, /* Input (Variable), */
193 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
194 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
195 0x81, 0x02, /* Input (Variable), */
196 0x05, 0x01, /* Usage Page (Desktop), */
197 0x09, 0x01, /* Usage (Pointer), */
198 0x95, 0x03, /* Report Count (3), * 3x Gyros */
199 0x81, 0x02, /* Input (Variable), */
200 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
201 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
202 0x81, 0x02, /* Input (Variable), */
203 0x75, 0x0C, /* Report Size (12), */
204 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
205 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
206 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
207 0x81, 0x02, /* Input (Variable), */
208 0x75, 0x08, /* Report Size (8), */
209 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
210 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
211 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
212 0x81, 0x02, /* Input (Variable), */
213 0x75, 0x08, /* Report Size (8), */
214 0x95, 0x30, /* Report Count (48), */
215 0x09, 0x01, /* Usage (Pointer), */
216 0x91, 0x02, /* Output (Variable), */
217 0x75, 0x08, /* Report Size (8), */
218 0x95, 0x30, /* Report Count (48), */
219 0x09, 0x01, /* Usage (Pointer), */
220 0xB1, 0x02, /* Feature (Variable), */
221 0xC0, /* End Collection, */
222 0xA1, 0x02, /* Collection (Logical), */
223 0x85, 0x02, /* Report ID (2), */
224 0x75, 0x08, /* Report Size (8), */
225 0x95, 0x30, /* Report Count (48), */
226 0x09, 0x01, /* Usage (Pointer), */
227 0xB1, 0x02, /* Feature (Variable), */
228 0xC0, /* End Collection, */
229 0xA1, 0x02, /* Collection (Logical), */
230 0x85, 0xEE, /* Report ID (238), */
231 0x75, 0x08, /* Report Size (8), */
232 0x95, 0x30, /* Report Count (48), */
233 0x09, 0x01, /* Usage (Pointer), */
234 0xB1, 0x02, /* Feature (Variable), */
235 0xC0, /* End Collection, */
236 0xA1, 0x02, /* Collection (Logical), */
237 0x85, 0xEF, /* Report ID (239), */
238 0x75, 0x08, /* Report Size (8), */
239 0x95, 0x30, /* Report Count (48), */
240 0x09, 0x01, /* Usage (Pointer), */
241 0xB1, 0x02, /* Feature (Variable), */
242 0xC0, /* End Collection, */
243 0xC0 /* End Collection */
248 * The default descriptor doesn't provide mapping for the accelerometers
249 * or orientation sensors. This fixed descriptor maps the accelerometers
250 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
251 * to usage values 0x43, 0x44 and 0x45.
253 static u8 dualshock4_usb_rdesc[] = {
254 0x05, 0x01, /* Usage Page (Desktop), */
255 0x09, 0x05, /* Usage (Gamepad), */
256 0xA1, 0x01, /* Collection (Application), */
257 0x85, 0x01, /* Report ID (1), */
258 0x09, 0x30, /* Usage (X), */
259 0x09, 0x31, /* Usage (Y), */
260 0x09, 0x32, /* Usage (Z), */
261 0x09, 0x35, /* Usage (Rz), */
262 0x15, 0x00, /* Logical Minimum (0), */
263 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
264 0x75, 0x08, /* Report Size (8), */
265 0x95, 0x04, /* Report Count (4), */
266 0x81, 0x02, /* Input (Variable), */
267 0x09, 0x39, /* Usage (Hat Switch), */
268 0x15, 0x00, /* Logical Minimum (0), */
269 0x25, 0x07, /* Logical Maximum (7), */
270 0x35, 0x00, /* Physical Minimum (0), */
271 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
272 0x65, 0x14, /* Unit (Degrees), */
273 0x75, 0x04, /* Report Size (4), */
274 0x95, 0x01, /* Report Count (1), */
275 0x81, 0x42, /* Input (Variable, Null State), */
276 0x65, 0x00, /* Unit, */
277 0x05, 0x09, /* Usage Page (Button), */
278 0x19, 0x01, /* Usage Minimum (01h), */
279 0x29, 0x0E, /* Usage Maximum (0Eh), */
280 0x15, 0x00, /* Logical Minimum (0), */
281 0x25, 0x01, /* Logical Maximum (1), */
282 0x75, 0x01, /* Report Size (1), */
283 0x95, 0x0E, /* Report Count (14), */
284 0x81, 0x02, /* Input (Variable), */
285 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
286 0x09, 0x20, /* Usage (20h), */
287 0x75, 0x06, /* Report Size (6), */
288 0x95, 0x01, /* Report Count (1), */
289 0x15, 0x00, /* Logical Minimum (0), */
290 0x25, 0x3F, /* Logical Maximum (63), */
291 0x81, 0x02, /* Input (Variable), */
292 0x05, 0x01, /* Usage Page (Desktop), */
293 0x09, 0x33, /* Usage (Rx), */
294 0x09, 0x34, /* Usage (Ry), */
295 0x15, 0x00, /* Logical Minimum (0), */
296 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
297 0x75, 0x08, /* Report Size (8), */
298 0x95, 0x02, /* Report Count (2), */
299 0x81, 0x02, /* Input (Variable), */
300 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
301 0x09, 0x21, /* Usage (21h), */
302 0x95, 0x03, /* Report Count (3), */
303 0x81, 0x02, /* Input (Variable), */
304 0x05, 0x01, /* Usage Page (Desktop), */
305 0x19, 0x40, /* Usage Minimum (40h), */
306 0x29, 0x42, /* Usage Maximum (42h), */
307 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
308 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
309 0x75, 0x10, /* Report Size (16), */
310 0x95, 0x03, /* Report Count (3), */
311 0x81, 0x02, /* Input (Variable), */
312 0x19, 0x43, /* Usage Minimum (43h), */
313 0x29, 0x45, /* Usage Maximum (45h), */
314 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
315 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
316 0x95, 0x03, /* Report Count (3), */
317 0x81, 0x02, /* Input (Variable), */
318 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
319 0x09, 0x21, /* Usage (21h), */
320 0x15, 0x00, /* Logical Minimum (0), */
321 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
322 0x75, 0x08, /* Report Size (8), */
323 0x95, 0x27, /* Report Count (39), */
324 0x81, 0x02, /* Input (Variable), */
325 0x85, 0x05, /* Report ID (5), */
326 0x09, 0x22, /* Usage (22h), */
327 0x95, 0x1F, /* Report Count (31), */
328 0x91, 0x02, /* Output (Variable), */
329 0x85, 0x04, /* Report ID (4), */
330 0x09, 0x23, /* Usage (23h), */
331 0x95, 0x24, /* Report Count (36), */
332 0xB1, 0x02, /* Feature (Variable), */
333 0x85, 0x02, /* Report ID (2), */
334 0x09, 0x24, /* Usage (24h), */
335 0x95, 0x24, /* Report Count (36), */
336 0xB1, 0x02, /* Feature (Variable), */
337 0x85, 0x08, /* Report ID (8), */
338 0x09, 0x25, /* Usage (25h), */
339 0x95, 0x03, /* Report Count (3), */
340 0xB1, 0x02, /* Feature (Variable), */
341 0x85, 0x10, /* Report ID (16), */
342 0x09, 0x26, /* Usage (26h), */
343 0x95, 0x04, /* Report Count (4), */
344 0xB1, 0x02, /* Feature (Variable), */
345 0x85, 0x11, /* Report ID (17), */
346 0x09, 0x27, /* Usage (27h), */
347 0x95, 0x02, /* Report Count (2), */
348 0xB1, 0x02, /* Feature (Variable), */
349 0x85, 0x12, /* Report ID (18), */
350 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
351 0x09, 0x21, /* Usage (21h), */
352 0x95, 0x0F, /* Report Count (15), */
353 0xB1, 0x02, /* Feature (Variable), */
354 0x85, 0x13, /* Report ID (19), */
355 0x09, 0x22, /* Usage (22h), */
356 0x95, 0x16, /* Report Count (22), */
357 0xB1, 0x02, /* Feature (Variable), */
358 0x85, 0x14, /* Report ID (20), */
359 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
360 0x09, 0x20, /* Usage (20h), */
361 0x95, 0x10, /* Report Count (16), */
362 0xB1, 0x02, /* Feature (Variable), */
363 0x85, 0x15, /* Report ID (21), */
364 0x09, 0x21, /* Usage (21h), */
365 0x95, 0x2C, /* Report Count (44), */
366 0xB1, 0x02, /* Feature (Variable), */
367 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
368 0x85, 0x80, /* Report ID (128), */
369 0x09, 0x20, /* Usage (20h), */
370 0x95, 0x06, /* Report Count (6), */
371 0xB1, 0x02, /* Feature (Variable), */
372 0x85, 0x81, /* Report ID (129), */
373 0x09, 0x21, /* Usage (21h), */
374 0x95, 0x06, /* Report Count (6), */
375 0xB1, 0x02, /* Feature (Variable), */
376 0x85, 0x82, /* Report ID (130), */
377 0x09, 0x22, /* Usage (22h), */
378 0x95, 0x05, /* Report Count (5), */
379 0xB1, 0x02, /* Feature (Variable), */
380 0x85, 0x83, /* Report ID (131), */
381 0x09, 0x23, /* Usage (23h), */
382 0x95, 0x01, /* Report Count (1), */
383 0xB1, 0x02, /* Feature (Variable), */
384 0x85, 0x84, /* Report ID (132), */
385 0x09, 0x24, /* Usage (24h), */
386 0x95, 0x04, /* Report Count (4), */
387 0xB1, 0x02, /* Feature (Variable), */
388 0x85, 0x85, /* Report ID (133), */
389 0x09, 0x25, /* Usage (25h), */
390 0x95, 0x06, /* Report Count (6), */
391 0xB1, 0x02, /* Feature (Variable), */
392 0x85, 0x86, /* Report ID (134), */
393 0x09, 0x26, /* Usage (26h), */
394 0x95, 0x06, /* Report Count (6), */
395 0xB1, 0x02, /* Feature (Variable), */
396 0x85, 0x87, /* Report ID (135), */
397 0x09, 0x27, /* Usage (27h), */
398 0x95, 0x23, /* Report Count (35), */
399 0xB1, 0x02, /* Feature (Variable), */
400 0x85, 0x88, /* Report ID (136), */
401 0x09, 0x28, /* Usage (28h), */
402 0x95, 0x22, /* Report Count (34), */
403 0xB1, 0x02, /* Feature (Variable), */
404 0x85, 0x89, /* Report ID (137), */
405 0x09, 0x29, /* Usage (29h), */
406 0x95, 0x02, /* Report Count (2), */
407 0xB1, 0x02, /* Feature (Variable), */
408 0x85, 0x90, /* Report ID (144), */
409 0x09, 0x30, /* Usage (30h), */
410 0x95, 0x05, /* Report Count (5), */
411 0xB1, 0x02, /* Feature (Variable), */
412 0x85, 0x91, /* Report ID (145), */
413 0x09, 0x31, /* Usage (31h), */
414 0x95, 0x03, /* Report Count (3), */
415 0xB1, 0x02, /* Feature (Variable), */
416 0x85, 0x92, /* Report ID (146), */
417 0x09, 0x32, /* Usage (32h), */
418 0x95, 0x03, /* Report Count (3), */
419 0xB1, 0x02, /* Feature (Variable), */
420 0x85, 0x93, /* Report ID (147), */
421 0x09, 0x33, /* Usage (33h), */
422 0x95, 0x0C, /* Report Count (12), */
423 0xB1, 0x02, /* Feature (Variable), */
424 0x85, 0xA0, /* Report ID (160), */
425 0x09, 0x40, /* Usage (40h), */
426 0x95, 0x06, /* Report Count (6), */
427 0xB1, 0x02, /* Feature (Variable), */
428 0x85, 0xA1, /* Report ID (161), */
429 0x09, 0x41, /* Usage (41h), */
430 0x95, 0x01, /* Report Count (1), */
431 0xB1, 0x02, /* Feature (Variable), */
432 0x85, 0xA2, /* Report ID (162), */
433 0x09, 0x42, /* Usage (42h), */
434 0x95, 0x01, /* Report Count (1), */
435 0xB1, 0x02, /* Feature (Variable), */
436 0x85, 0xA3, /* Report ID (163), */
437 0x09, 0x43, /* Usage (43h), */
438 0x95, 0x30, /* Report Count (48), */
439 0xB1, 0x02, /* Feature (Variable), */
440 0x85, 0xA4, /* Report ID (164), */
441 0x09, 0x44, /* Usage (44h), */
442 0x95, 0x0D, /* Report Count (13), */
443 0xB1, 0x02, /* Feature (Variable), */
444 0x85, 0xA5, /* Report ID (165), */
445 0x09, 0x45, /* Usage (45h), */
446 0x95, 0x15, /* Report Count (21), */
447 0xB1, 0x02, /* Feature (Variable), */
448 0x85, 0xA6, /* Report ID (166), */
449 0x09, 0x46, /* Usage (46h), */
450 0x95, 0x15, /* Report Count (21), */
451 0xB1, 0x02, /* Feature (Variable), */
452 0x85, 0xF0, /* Report ID (240), */
453 0x09, 0x47, /* Usage (47h), */
454 0x95, 0x3F, /* Report Count (63), */
455 0xB1, 0x02, /* Feature (Variable), */
456 0x85, 0xF1, /* Report ID (241), */
457 0x09, 0x48, /* Usage (48h), */
458 0x95, 0x3F, /* Report Count (63), */
459 0xB1, 0x02, /* Feature (Variable), */
460 0x85, 0xF2, /* Report ID (242), */
461 0x09, 0x49, /* Usage (49h), */
462 0x95, 0x0F, /* Report Count (15), */
463 0xB1, 0x02, /* Feature (Variable), */
464 0x85, 0xA7, /* Report ID (167), */
465 0x09, 0x4A, /* Usage (4Ah), */
466 0x95, 0x01, /* Report Count (1), */
467 0xB1, 0x02, /* Feature (Variable), */
468 0x85, 0xA8, /* Report ID (168), */
469 0x09, 0x4B, /* Usage (4Bh), */
470 0x95, 0x01, /* Report Count (1), */
471 0xB1, 0x02, /* Feature (Variable), */
472 0x85, 0xA9, /* Report ID (169), */
473 0x09, 0x4C, /* Usage (4Ch), */
474 0x95, 0x08, /* Report Count (8), */
475 0xB1, 0x02, /* Feature (Variable), */
476 0x85, 0xAA, /* Report ID (170), */
477 0x09, 0x4E, /* Usage (4Eh), */
478 0x95, 0x01, /* Report Count (1), */
479 0xB1, 0x02, /* Feature (Variable), */
480 0x85, 0xAB, /* Report ID (171), */
481 0x09, 0x4F, /* Usage (4Fh), */
482 0x95, 0x39, /* Report Count (57), */
483 0xB1, 0x02, /* Feature (Variable), */
484 0x85, 0xAC, /* Report ID (172), */
485 0x09, 0x50, /* Usage (50h), */
486 0x95, 0x39, /* Report Count (57), */
487 0xB1, 0x02, /* Feature (Variable), */
488 0x85, 0xAD, /* Report ID (173), */
489 0x09, 0x51, /* Usage (51h), */
490 0x95, 0x0B, /* Report Count (11), */
491 0xB1, 0x02, /* Feature (Variable), */
492 0x85, 0xAE, /* Report ID (174), */
493 0x09, 0x52, /* Usage (52h), */
494 0x95, 0x01, /* Report Count (1), */
495 0xB1, 0x02, /* Feature (Variable), */
496 0x85, 0xAF, /* Report ID (175), */
497 0x09, 0x53, /* Usage (53h), */
498 0x95, 0x02, /* Report Count (2), */
499 0xB1, 0x02, /* Feature (Variable), */
500 0x85, 0xB0, /* Report ID (176), */
501 0x09, 0x54, /* Usage (54h), */
502 0x95, 0x3F, /* Report Count (63), */
503 0xB1, 0x02, /* Feature (Variable), */
504 0xC0 /* End Collection */
508 * The default behavior of the Dualshock 4 is to send reports using report
509 * type 1 when running over Bluetooth. However, when feature report 2 is
510 * requested during the controller initialization it starts sending input
511 * reports in report 17. Since report 17 is undefined in the default HID
512 * descriptor the button and axis definitions must be moved to report 17 or
513 * the HID layer won't process the received input.
515 static u8 dualshock4_bt_rdesc[] = {
516 0x05, 0x01, /* Usage Page (Desktop), */
517 0x09, 0x05, /* Usage (Gamepad), */
518 0xA1, 0x01, /* Collection (Application), */
519 0x85, 0x01, /* Report ID (1), */
520 0x75, 0x08, /* Report Size (8), */
521 0x95, 0x0A, /* Report Count (9), */
522 0x81, 0x02, /* Input (Variable), */
523 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
524 0x85, 0x02, /* Report ID (2), */
525 0x09, 0x24, /* Usage (24h), */
526 0x95, 0x24, /* Report Count (36), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0xA3, /* Report ID (163), */
529 0x09, 0x25, /* Usage (25h), */
530 0x95, 0x30, /* Report Count (48), */
531 0xB1, 0x02, /* Feature (Variable), */
532 0x85, 0x05, /* Report ID (5), */
533 0x09, 0x26, /* Usage (26h), */
534 0x95, 0x28, /* Report Count (40), */
535 0xB1, 0x02, /* Feature (Variable), */
536 0x85, 0x06, /* Report ID (6), */
537 0x09, 0x27, /* Usage (27h), */
538 0x95, 0x34, /* Report Count (52), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0x07, /* Report ID (7), */
541 0x09, 0x28, /* Usage (28h), */
542 0x95, 0x30, /* Report Count (48), */
543 0xB1, 0x02, /* Feature (Variable), */
544 0x85, 0x08, /* Report ID (8), */
545 0x09, 0x29, /* Usage (29h), */
546 0x95, 0x2F, /* Report Count (47), */
547 0xB1, 0x02, /* Feature (Variable), */
548 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
549 0x85, 0x03, /* Report ID (3), */
550 0x09, 0x21, /* Usage (21h), */
551 0x95, 0x26, /* Report Count (38), */
552 0xB1, 0x02, /* Feature (Variable), */
553 0x85, 0x04, /* Report ID (4), */
554 0x09, 0x22, /* Usage (22h), */
555 0x95, 0x2E, /* Report Count (46), */
556 0xB1, 0x02, /* Feature (Variable), */
557 0x85, 0xF0, /* Report ID (240), */
558 0x09, 0x47, /* Usage (47h), */
559 0x95, 0x3F, /* Report Count (63), */
560 0xB1, 0x02, /* Feature (Variable), */
561 0x85, 0xF1, /* Report ID (241), */
562 0x09, 0x48, /* Usage (48h), */
563 0x95, 0x3F, /* Report Count (63), */
564 0xB1, 0x02, /* Feature (Variable), */
565 0x85, 0xF2, /* Report ID (242), */
566 0x09, 0x49, /* Usage (49h), */
567 0x95, 0x0F, /* Report Count (15), */
568 0xB1, 0x02, /* Feature (Variable), */
569 0x85, 0x11, /* Report ID (17), */
570 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
571 0x09, 0x20, /* Usage (20h), */
572 0x95, 0x02, /* Report Count (2), */
573 0x81, 0x02, /* Input (Variable), */
574 0x05, 0x01, /* Usage Page (Desktop), */
575 0x09, 0x30, /* Usage (X), */
576 0x09, 0x31, /* Usage (Y), */
577 0x09, 0x32, /* Usage (Z), */
578 0x09, 0x35, /* Usage (Rz), */
579 0x15, 0x00, /* Logical Minimum (0), */
580 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
581 0x75, 0x08, /* Report Size (8), */
582 0x95, 0x04, /* Report Count (4), */
583 0x81, 0x02, /* Input (Variable), */
584 0x09, 0x39, /* Usage (Hat Switch), */
585 0x15, 0x00, /* Logical Minimum (0), */
586 0x25, 0x07, /* Logical Maximum (7), */
587 0x75, 0x04, /* Report Size (4), */
588 0x95, 0x01, /* Report Count (1), */
589 0x81, 0x42, /* Input (Variable, Null State), */
590 0x05, 0x09, /* Usage Page (Button), */
591 0x19, 0x01, /* Usage Minimum (01h), */
592 0x29, 0x0E, /* Usage Maximum (0Eh), */
593 0x15, 0x00, /* Logical Minimum (0), */
594 0x25, 0x01, /* Logical Maximum (1), */
595 0x75, 0x01, /* Report Size (1), */
596 0x95, 0x0E, /* Report Count (14), */
597 0x81, 0x02, /* Input (Variable), */
598 0x75, 0x06, /* Report Size (6), */
599 0x95, 0x01, /* Report Count (1), */
600 0x81, 0x01, /* Input (Constant), */
601 0x05, 0x01, /* Usage Page (Desktop), */
602 0x09, 0x33, /* Usage (Rx), */
603 0x09, 0x34, /* Usage (Ry), */
604 0x15, 0x00, /* Logical Minimum (0), */
605 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
606 0x75, 0x08, /* Report Size (8), */
607 0x95, 0x02, /* Report Count (2), */
608 0x81, 0x02, /* Input (Variable), */
609 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
610 0x09, 0x20, /* Usage (20h), */
611 0x95, 0x03, /* Report Count (3), */
612 0x81, 0x02, /* Input (Variable), */
613 0x05, 0x01, /* Usage Page (Desktop), */
614 0x19, 0x40, /* Usage Minimum (40h), */
615 0x29, 0x42, /* Usage Maximum (42h), */
616 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
617 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
618 0x75, 0x10, /* Report Size (16), */
619 0x95, 0x03, /* Report Count (3), */
620 0x81, 0x02, /* Input (Variable), */
621 0x19, 0x43, /* Usage Minimum (43h), */
622 0x29, 0x45, /* Usage Maximum (45h), */
623 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
624 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
625 0x95, 0x03, /* Report Count (3), */
626 0x81, 0x02, /* Input (Variable), */
627 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
628 0x09, 0x20, /* Usage (20h), */
629 0x15, 0x00, /* Logical Minimum (0), */
630 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
631 0x75, 0x08, /* Report Size (8), */
632 0x95, 0x31, /* Report Count (51), */
633 0x81, 0x02, /* Input (Variable), */
634 0x09, 0x21, /* Usage (21h), */
635 0x75, 0x08, /* Report Size (8), */
636 0x95, 0x4D, /* Report Count (77), */
637 0x91, 0x02, /* Output (Variable), */
638 0x85, 0x12, /* Report ID (18), */
639 0x09, 0x22, /* Usage (22h), */
640 0x95, 0x8D, /* Report Count (141), */
641 0x81, 0x02, /* Input (Variable), */
642 0x09, 0x23, /* Usage (23h), */
643 0x91, 0x02, /* Output (Variable), */
644 0x85, 0x13, /* Report ID (19), */
645 0x09, 0x24, /* Usage (24h), */
646 0x95, 0xCD, /* Report Count (205), */
647 0x81, 0x02, /* Input (Variable), */
648 0x09, 0x25, /* Usage (25h), */
649 0x91, 0x02, /* Output (Variable), */
650 0x85, 0x14, /* Report ID (20), */
651 0x09, 0x26, /* Usage (26h), */
652 0x96, 0x0D, 0x01, /* Report Count (269), */
653 0x81, 0x02, /* Input (Variable), */
654 0x09, 0x27, /* Usage (27h), */
655 0x91, 0x02, /* Output (Variable), */
656 0x85, 0x15, /* Report ID (21), */
657 0x09, 0x28, /* Usage (28h), */
658 0x96, 0x4D, 0x01, /* Report Count (333), */
659 0x81, 0x02, /* Input (Variable), */
660 0x09, 0x29, /* Usage (29h), */
661 0x91, 0x02, /* Output (Variable), */
662 0x85, 0x16, /* Report ID (22), */
663 0x09, 0x2A, /* Usage (2Ah), */
664 0x96, 0x8D, 0x01, /* Report Count (397), */
665 0x81, 0x02, /* Input (Variable), */
666 0x09, 0x2B, /* Usage (2Bh), */
667 0x91, 0x02, /* Output (Variable), */
668 0x85, 0x17, /* Report ID (23), */
669 0x09, 0x2C, /* Usage (2Ch), */
670 0x96, 0xCD, 0x01, /* Report Count (461), */
671 0x81, 0x02, /* Input (Variable), */
672 0x09, 0x2D, /* Usage (2Dh), */
673 0x91, 0x02, /* Output (Variable), */
674 0x85, 0x18, /* Report ID (24), */
675 0x09, 0x2E, /* Usage (2Eh), */
676 0x96, 0x0D, 0x02, /* Report Count (525), */
677 0x81, 0x02, /* Input (Variable), */
678 0x09, 0x2F, /* Usage (2Fh), */
679 0x91, 0x02, /* Output (Variable), */
680 0x85, 0x19, /* Report ID (25), */
681 0x09, 0x30, /* Usage (30h), */
682 0x96, 0x22, 0x02, /* Report Count (546), */
683 0x81, 0x02, /* Input (Variable), */
684 0x09, 0x31, /* Usage (31h), */
685 0x91, 0x02, /* Output (Variable), */
686 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
687 0x85, 0x82, /* Report ID (130), */
688 0x09, 0x22, /* Usage (22h), */
689 0x95, 0x3F, /* Report Count (63), */
690 0xB1, 0x02, /* Feature (Variable), */
691 0x85, 0x83, /* Report ID (131), */
692 0x09, 0x23, /* Usage (23h), */
693 0xB1, 0x02, /* Feature (Variable), */
694 0x85, 0x84, /* Report ID (132), */
695 0x09, 0x24, /* Usage (24h), */
696 0xB1, 0x02, /* Feature (Variable), */
697 0x85, 0x90, /* Report ID (144), */
698 0x09, 0x30, /* Usage (30h), */
699 0xB1, 0x02, /* Feature (Variable), */
700 0x85, 0x91, /* Report ID (145), */
701 0x09, 0x31, /* Usage (31h), */
702 0xB1, 0x02, /* Feature (Variable), */
703 0x85, 0x92, /* Report ID (146), */
704 0x09, 0x32, /* Usage (32h), */
705 0xB1, 0x02, /* Feature (Variable), */
706 0x85, 0x93, /* Report ID (147), */
707 0x09, 0x33, /* Usage (33h), */
708 0xB1, 0x02, /* Feature (Variable), */
709 0x85, 0xA0, /* Report ID (160), */
710 0x09, 0x40, /* Usage (40h), */
711 0xB1, 0x02, /* Feature (Variable), */
712 0x85, 0xA4, /* Report ID (164), */
713 0x09, 0x44, /* Usage (44h), */
714 0xB1, 0x02, /* Feature (Variable), */
715 0xC0 /* End Collection */
718 static __u8 ps3remote_rdesc[] = {
719 0x05, 0x01, /* GUsagePage Generic Desktop */
720 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
721 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
723 /* Use collection 1 for joypad buttons */
724 0xA1, 0x02, /* MCollection Logical (interrelated data) */
726 /* Ignore the 1st byte, maybe it is used for a controller
727 * number but it's not needed for correct operation */
728 0x75, 0x08, /* GReportSize 0x08 [8] */
729 0x95, 0x01, /* GReportCount 0x01 [1] */
730 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
732 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
733 * buttons multiple keypresses are allowed */
734 0x05, 0x09, /* GUsagePage Button */
735 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
736 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
737 0x14, /* GLogicalMinimum [0] */
738 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
739 0x75, 0x01, /* GReportSize 0x01 [1] */
740 0x95, 0x18, /* GReportCount 0x18 [24] */
741 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
743 0xC0, /* MEndCollection */
745 /* Use collection 2 for remote control buttons */
746 0xA1, 0x02, /* MCollection Logical (interrelated data) */
748 /* 5th byte is used for remote control buttons */
749 0x05, 0x09, /* GUsagePage Button */
750 0x18, /* LUsageMinimum [No button pressed] */
751 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
752 0x14, /* GLogicalMinimum [0] */
753 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
754 0x75, 0x08, /* GReportSize 0x08 [8] */
755 0x95, 0x01, /* GReportCount 0x01 [1] */
758 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
759 * 0xff and 11th is for press indication */
760 0x75, 0x08, /* GReportSize 0x08 [8] */
761 0x95, 0x06, /* GReportCount 0x06 [6] */
762 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
764 /* 12th byte is for battery strength */
765 0x05, 0x06, /* GUsagePage Generic Device Controls */
766 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
767 0x14, /* GLogicalMinimum [0] */
768 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
769 0x75, 0x08, /* GReportSize 0x08 [8] */
770 0x95, 0x01, /* GReportCount 0x01 [1] */
771 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
773 0xC0, /* MEndCollection */
775 0xC0 /* MEndCollection [Game Pad] */
778 static const unsigned int ps3remote_keymap_joypad_buttons[] = {
780 [0x02] = BTN_THUMBL, /* L3 */
781 [0x03] = BTN_THUMBR, /* R3 */
787 [0x09] = BTN_TL2, /* L2 */
788 [0x0a] = BTN_TR2, /* R2 */
789 [0x0b] = BTN_TL, /* L1 */
790 [0x0c] = BTN_TR, /* R1 */
791 [0x0d] = KEY_OPTION, /* options/triangle */
792 [0x0e] = KEY_BACK, /* back/circle */
793 [0x0f] = BTN_0, /* cross */
794 [0x10] = KEY_SCREEN, /* view/square */
795 [0x11] = KEY_HOMEPAGE, /* PS button */
798 static const unsigned int ps3remote_keymap_remote_buttons[] = {
809 [0x0e] = KEY_ESC, /* return */
811 [0x16] = KEY_EJECTCD,
812 [0x1a] = KEY_MENU, /* top menu */
814 [0x30] = KEY_PREVIOUS,
817 [0x33] = KEY_REWIND, /* scan back */
818 [0x34] = KEY_FORWARD, /* scan forward */
821 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
822 [0x60] = KEY_FRAMEBACK, /* slow/step back */
823 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
824 [0x63] = KEY_SUBTITLE,
827 [0x70] = KEY_INFO, /* display */
834 static const unsigned int buzz_keymap[] = {
836 * The controller has 4 remote buzzers, each with one LED and 5
839 * We use the mapping chosen by the controller, which is:
842 * -------------------
849 * So, for example, the orange button on the third buzzer is mapped to
850 * BTN_TRIGGER_HAPPY14
852 [ 1] = BTN_TRIGGER_HAPPY1,
853 [ 2] = BTN_TRIGGER_HAPPY2,
854 [ 3] = BTN_TRIGGER_HAPPY3,
855 [ 4] = BTN_TRIGGER_HAPPY4,
856 [ 5] = BTN_TRIGGER_HAPPY5,
857 [ 6] = BTN_TRIGGER_HAPPY6,
858 [ 7] = BTN_TRIGGER_HAPPY7,
859 [ 8] = BTN_TRIGGER_HAPPY8,
860 [ 9] = BTN_TRIGGER_HAPPY9,
861 [10] = BTN_TRIGGER_HAPPY10,
862 [11] = BTN_TRIGGER_HAPPY11,
863 [12] = BTN_TRIGGER_HAPPY12,
864 [13] = BTN_TRIGGER_HAPPY13,
865 [14] = BTN_TRIGGER_HAPPY14,
866 [15] = BTN_TRIGGER_HAPPY15,
867 [16] = BTN_TRIGGER_HAPPY16,
868 [17] = BTN_TRIGGER_HAPPY17,
869 [18] = BTN_TRIGGER_HAPPY18,
870 [19] = BTN_TRIGGER_HAPPY19,
871 [20] = BTN_TRIGGER_HAPPY20,
874 static enum power_supply_property sony_battery_props[] = {
875 POWER_SUPPLY_PROP_PRESENT,
876 POWER_SUPPLY_PROP_CAPACITY,
877 POWER_SUPPLY_PROP_SCOPE,
878 POWER_SUPPLY_PROP_STATUS,
882 __u8 time_enabled; /* the total time the led is active (0xff means forever) */
883 __u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
885 __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
886 __u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
889 struct sixaxis_rumble {
891 __u8 right_duration; /* Right motor duration (0xff means forever) */
892 __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
893 __u8 left_duration; /* Left motor duration (0xff means forever) */
894 __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
897 struct sixaxis_output_report {
899 struct sixaxis_rumble rumble;
901 __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
902 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
903 struct sixaxis_led _reserved; /* LED5, not actually soldered */
906 union sixaxis_output_report_01 {
907 struct sixaxis_output_report data;
911 struct motion_output_report_02 {
918 #define DS4_REPORT_0x02_SIZE 37
919 #define DS4_REPORT_0x05_SIZE 32
920 #define DS4_REPORT_0x11_SIZE 78
921 #define DS4_REPORT_0x81_SIZE 7
922 #define SIXAXIS_REPORT_0xF2_SIZE 17
923 #define SIXAXIS_REPORT_0xF5_SIZE 8
924 #define MOTION_REPORT_0x02_SIZE 49
926 static DEFINE_SPINLOCK(sony_dev_list_lock);
927 static LIST_HEAD(sony_device_list);
928 static DEFINE_IDA(sony_device_id_allocator);
932 struct list_head list_node;
933 struct hid_device *hdev;
934 struct led_classdev *leds[MAX_LEDS];
935 unsigned long quirks;
936 struct work_struct state_worker;
937 struct power_supply *battery;
938 struct power_supply_desc battery_desc;
940 __u8 *output_report_dmabuf;
942 #ifdef CONFIG_SONY_FF
948 __u8 worker_initialized;
950 __u8 battery_charging;
951 __u8 battery_capacity;
952 __u8 led_state[MAX_LEDS];
953 __u8 led_delay_on[MAX_LEDS];
954 __u8 led_delay_off[MAX_LEDS];
958 static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
961 *rsize = sizeof(sixaxis_rdesc);
962 return sixaxis_rdesc;
965 static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
968 *rsize = sizeof(motion_rdesc);
972 static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
975 *rsize = sizeof(ps3remote_rdesc);
976 return ps3remote_rdesc;
979 static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
980 struct hid_field *field, struct hid_usage *usage,
981 unsigned long **bit, int *max)
983 unsigned int key = usage->hid & HID_USAGE;
985 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
988 switch (usage->collection_index) {
990 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
993 key = ps3remote_keymap_joypad_buttons[key];
998 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
1001 key = ps3remote_keymap_remote_buttons[key];
1009 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1013 static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1014 unsigned int *rsize)
1016 struct sony_sc *sc = hid_get_drvdata(hdev);
1019 * Some Sony RF receivers wrongly declare the mouse pointer as a
1020 * a constant non-data variable.
1022 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1023 /* usage page: generic desktop controls */
1024 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1026 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1027 /* input (usage page for x,y axes): constant, variable, relative */
1028 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
1029 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
1030 /* input: data, variable, relative */
1035 * The default Dualshock 4 USB descriptor doesn't assign
1036 * the gyroscope values to corresponding axes so we need a
1039 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
1040 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1041 rdesc = dualshock4_usb_rdesc;
1042 *rsize = sizeof(dualshock4_usb_rdesc);
1043 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
1044 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1045 rdesc = dualshock4_bt_rdesc;
1046 *rsize = sizeof(dualshock4_bt_rdesc);
1049 if (sc->quirks & SIXAXIS_CONTROLLER)
1050 return sixaxis_fixup(hdev, rdesc, rsize);
1052 if (sc->quirks & MOTION_CONTROLLER)
1053 return motion_fixup(hdev, rdesc, rsize);
1055 if (sc->quirks & PS3REMOTE)
1056 return ps3remote_fixup(hdev, rdesc, rsize);
1061 static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1063 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
1064 unsigned long flags;
1066 __u8 cable_state, battery_capacity, battery_charging;
1069 * The sixaxis is charging if the battery value is 0xee
1070 * and it is fully charged if the value is 0xef.
1071 * It does not report the actual level while charging so it
1072 * is set to 100% while charging is in progress.
1074 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1076 if (rd[offset] >= 0xee) {
1077 battery_capacity = 100;
1078 battery_charging = !(rd[offset] & 0x01);
1081 __u8 index = rd[offset] <= 5 ? rd[offset] : 5;
1082 battery_capacity = sixaxis_battery_capacity[index];
1083 battery_charging = 0;
1087 spin_lock_irqsave(&sc->lock, flags);
1088 sc->cable_state = cable_state;
1089 sc->battery_capacity = battery_capacity;
1090 sc->battery_charging = battery_charging;
1091 spin_unlock_irqrestore(&sc->lock, flags);
1094 static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1096 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1097 struct hid_input, list);
1098 struct input_dev *input_dev = hidinput->input;
1099 unsigned long flags;
1101 __u8 cable_state, battery_capacity, battery_charging;
1104 * Battery and touchpad data starts at byte 30 in the USB report and
1105 * 32 in Bluetooth report.
1107 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1110 * The lower 4 bits of byte 30 contain the battery level
1111 * and the 5th bit contains the USB cable state.
1113 cable_state = (rd[offset] >> 4) & 0x01;
1114 battery_capacity = rd[offset] & 0x0F;
1117 * When a USB power source is connected the battery level ranges from
1118 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1119 * A battery level above 10 when plugged in means charge completed.
1121 if (!cable_state || battery_capacity > 10)
1122 battery_charging = 0;
1124 battery_charging = 1;
1128 if (battery_capacity > 10)
1129 battery_capacity = 10;
1131 battery_capacity *= 10;
1133 spin_lock_irqsave(&sc->lock, flags);
1134 sc->cable_state = cable_state;
1135 sc->battery_capacity = battery_capacity;
1136 sc->battery_charging = battery_charging;
1137 spin_unlock_irqrestore(&sc->lock, flags);
1142 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
1143 * and 37 on Bluetooth.
1144 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1145 * indicator that is 0 when pressed and 1 when not pressed.
1146 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1147 * The data for the second touch is in the same format and immediatly
1148 * follows the data for the first.
1150 for (n = 0; n < 2; n++) {
1153 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1154 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1156 input_mt_slot(input_dev, n);
1157 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1158 !(rd[offset] >> 7));
1159 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1160 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1166 static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1169 struct sony_sc *sc = hid_get_drvdata(hdev);
1172 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
1173 * has to be BYTE_SWAPPED before passing up to joystick interface
1175 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
1176 swap(rd[41], rd[42]);
1177 swap(rd[43], rd[44]);
1178 swap(rd[45], rd[46]);
1179 swap(rd[47], rd[48]);
1181 sixaxis_parse_report(sc, rd, size);
1182 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1183 sixaxis_parse_report(sc, rd, size);
1184 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1185 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1186 && rd[0] == 0x11 && size == 78)) {
1187 dualshock4_parse_report(sc, rd, size);
1193 static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1194 struct hid_field *field, struct hid_usage *usage,
1195 unsigned long **bit, int *max)
1197 struct sony_sc *sc = hid_get_drvdata(hdev);
1199 if (sc->quirks & BUZZ_CONTROLLER) {
1200 unsigned int key = usage->hid & HID_USAGE;
1202 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1205 switch (usage->collection_index) {
1207 if (key >= ARRAY_SIZE(buzz_keymap))
1210 key = buzz_keymap[key];
1218 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1222 if (sc->quirks & PS3REMOTE)
1223 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1225 /* Let hid-core decide for the others */
1229 static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1232 struct input_dev *input_dev = hi->input;
1235 ret = input_mt_init_slots(input_dev, touch_count, 0);
1239 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1240 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1245 static void sony_input_configured(struct hid_device *hdev,
1246 struct hid_input *hidinput)
1248 struct sony_sc *sc = hid_get_drvdata(hdev);
1251 * The Dualshock 4 touchpad supports 2 touches and has a
1252 * resolution of 1920x942 (44.86 dots/mm).
1254 if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1255 if (sony_register_touchpad(hidinput, 2, 1920, 942) != 0)
1257 "Unable to initialize multi-touch slots\n");
1262 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1263 * to "operational". Without this, the ps3 controller will not report any
1266 static int sixaxis_set_operational_usb(struct hid_device *hdev)
1268 const int buf_size =
1269 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
1273 buf = kmalloc(buf_size, GFP_KERNEL);
1277 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1278 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1280 hid_err(hdev, "can't set operational mode: step 1\n");
1285 * Some compatible controllers like the Speedlink Strike FX and
1286 * Gasia need another query plus an USB interrupt to get operational.
1288 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1289 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1291 hid_err(hdev, "can't set operational mode: step 2\n");
1295 ret = hid_hw_output_report(hdev, buf, 1);
1297 hid_err(hdev, "can't set operational mode: step 3\n");
1305 static int sixaxis_set_operational_bt(struct hid_device *hdev)
1307 static const __u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1311 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1315 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
1316 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
1324 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
1325 * controller so that it sends full input reports of type 0x11.
1327 static int dualshock4_set_operational_bt(struct hid_device *hdev)
1332 buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1336 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
1337 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1344 static void sixaxis_set_leds_from_id(struct sony_sc *sc)
1346 static const __u8 sixaxis_leds[10][4] = {
1347 { 0x01, 0x00, 0x00, 0x00 },
1348 { 0x00, 0x01, 0x00, 0x00 },
1349 { 0x00, 0x00, 0x01, 0x00 },
1350 { 0x00, 0x00, 0x00, 0x01 },
1351 { 0x01, 0x00, 0x00, 0x01 },
1352 { 0x00, 0x01, 0x00, 0x01 },
1353 { 0x00, 0x00, 0x01, 0x01 },
1354 { 0x01, 0x00, 0x01, 0x01 },
1355 { 0x00, 0x01, 0x01, 0x01 },
1356 { 0x01, 0x01, 0x01, 0x01 }
1359 int id = sc->device_id;
1361 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1367 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1370 static void dualshock4_set_leds_from_id(struct sony_sc *sc)
1372 /* The first 4 color/index entries match what the PS4 assigns */
1373 static const __u8 color_code[7][3] = {
1374 /* Blue */ { 0x00, 0x00, 0x01 },
1375 /* Red */ { 0x01, 0x00, 0x00 },
1376 /* Green */ { 0x00, 0x01, 0x00 },
1377 /* Pink */ { 0x02, 0x00, 0x01 },
1378 /* Orange */ { 0x02, 0x01, 0x00 },
1379 /* Teal */ { 0x00, 0x01, 0x01 },
1380 /* White */ { 0x01, 0x01, 0x01 }
1383 int id = sc->device_id;
1385 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1391 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
1394 static void buzz_set_leds(struct sony_sc *sc)
1396 struct hid_device *hdev = sc->hdev;
1397 struct list_head *report_list =
1398 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1399 struct hid_report *report = list_entry(report_list->next,
1400 struct hid_report, list);
1401 __s32 *value = report->field[0]->value;
1403 BUILD_BUG_ON(MAX_LEDS < 4);
1406 value[1] = sc->led_state[0] ? 0xff : 0x00;
1407 value[2] = sc->led_state[1] ? 0xff : 0x00;
1408 value[3] = sc->led_state[2] ? 0xff : 0x00;
1409 value[4] = sc->led_state[3] ? 0xff : 0x00;
1412 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1415 static void sony_set_leds(struct sony_sc *sc)
1417 if (!(sc->quirks & BUZZ_CONTROLLER))
1418 schedule_work(&sc->state_worker);
1423 static void sony_led_set_brightness(struct led_classdev *led,
1424 enum led_brightness value)
1426 struct device *dev = led->dev->parent;
1427 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1428 struct sony_sc *drv_data;
1433 drv_data = hid_get_drvdata(hdev);
1435 hid_err(hdev, "No device data\n");
1440 * The Sixaxis on USB will override any LED settings sent to it
1441 * and keep flashing all of the LEDs until the PS button is pressed.
1442 * Updates, even if redundant, must be always be sent to the
1443 * controller to avoid having to toggle the state of an LED just to
1444 * stop the flashing later on.
1446 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1448 for (n = 0; n < drv_data->led_count; n++) {
1449 if (led == drv_data->leds[n] && (force_update ||
1450 (value != drv_data->led_state[n] ||
1451 drv_data->led_delay_on[n] ||
1452 drv_data->led_delay_off[n]))) {
1454 drv_data->led_state[n] = value;
1456 /* Setting the brightness stops the blinking */
1457 drv_data->led_delay_on[n] = 0;
1458 drv_data->led_delay_off[n] = 0;
1460 sony_set_leds(drv_data);
1466 static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
1468 struct device *dev = led->dev->parent;
1469 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1470 struct sony_sc *drv_data;
1474 drv_data = hid_get_drvdata(hdev);
1476 hid_err(hdev, "No device data\n");
1480 for (n = 0; n < drv_data->led_count; n++) {
1481 if (led == drv_data->leds[n])
1482 return drv_data->led_state[n];
1488 static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1489 unsigned long *delay_off)
1491 struct device *dev = led->dev->parent;
1492 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1493 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1495 __u8 new_on, new_off;
1498 hid_err(hdev, "No device data\n");
1502 /* Max delay is 255 deciseconds or 2550 milliseconds */
1503 if (*delay_on > 2550)
1505 if (*delay_off > 2550)
1508 /* Blink at 1 Hz if both values are zero */
1509 if (!*delay_on && !*delay_off)
1510 *delay_on = *delay_off = 500;
1512 new_on = *delay_on / 10;
1513 new_off = *delay_off / 10;
1515 for (n = 0; n < drv_data->led_count; n++) {
1516 if (led == drv_data->leds[n])
1520 /* This LED is not registered on this device */
1521 if (n >= drv_data->led_count)
1524 /* Don't schedule work if the values didn't change */
1525 if (new_on != drv_data->led_delay_on[n] ||
1526 new_off != drv_data->led_delay_off[n]) {
1527 drv_data->led_delay_on[n] = new_on;
1528 drv_data->led_delay_off[n] = new_off;
1529 schedule_work(&drv_data->state_worker);
1535 static void sony_leds_remove(struct sony_sc *sc)
1537 struct led_classdev *led;
1540 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
1542 for (n = 0; n < sc->led_count; n++) {
1547 led_classdev_unregister(led);
1554 static int sony_leds_init(struct sony_sc *sc)
1556 struct hid_device *hdev = sc->hdev;
1559 struct led_classdev *led;
1563 const char *name_fmt;
1564 static const char * const ds4_name_str[] = { "red", "green", "blue",
1566 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1567 __u8 use_hw_blink[MAX_LEDS] = { 0 };
1569 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
1571 if (sc->quirks & BUZZ_CONTROLLER) {
1574 name_len = strlen("::buzz#");
1575 name_fmt = "%s::buzz%d";
1576 /* Validate expected report characteristics. */
1577 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1579 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1580 dualshock4_set_leds_from_id(sc);
1581 sc->led_state[3] = 1;
1583 memset(max_brightness, 255, 3);
1584 use_hw_blink[3] = 1;
1588 } else if (sc->quirks & MOTION_CONTROLLER) {
1590 memset(max_brightness, 255, 3);
1595 sixaxis_set_leds_from_id(sc);
1597 memset(use_hw_blink, 1, 4);
1599 name_len = strlen("::sony#");
1600 name_fmt = "%s::sony%d";
1604 * Clear LEDs as we have no way of reading their initial state. This is
1605 * only relevant if the driver is loaded after somebody actively set the
1610 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
1612 for (n = 0; n < sc->led_count; n++) {
1615 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
1617 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1619 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
1624 name = (void *)(&led[1]);
1626 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1629 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
1631 led->brightness = sc->led_state[n];
1632 led->max_brightness = max_brightness[n];
1633 led->brightness_get = sony_led_get_brightness;
1634 led->brightness_set = sony_led_set_brightness;
1636 if (use_hw_blink[n])
1637 led->blink_set = sony_led_blink_set;
1641 ret = led_classdev_register(&hdev->dev, led);
1643 hid_err(hdev, "Failed to register LED %d\n", n);
1653 sony_leds_remove(sc);
1658 static void sixaxis_state_worker(struct work_struct *work)
1660 static const union sixaxis_output_report_01 default_report = {
1663 0x00, 0xff, 0x00, 0xff, 0x00,
1664 0x00, 0x00, 0x00, 0x00, 0x00,
1665 0xff, 0x27, 0x10, 0x00, 0x32,
1666 0xff, 0x27, 0x10, 0x00, 0x32,
1667 0xff, 0x27, 0x10, 0x00, 0x32,
1668 0xff, 0x27, 0x10, 0x00, 0x32,
1669 0x00, 0x00, 0x00, 0x00, 0x00
1672 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1673 struct sixaxis_output_report *report =
1674 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1677 /* Initialize the report with default values */
1678 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
1680 #ifdef CONFIG_SONY_FF
1681 report->rumble.right_motor_on = sc->right ? 1 : 0;
1682 report->rumble.left_motor_force = sc->left;
1685 report->leds_bitmap |= sc->led_state[0] << 1;
1686 report->leds_bitmap |= sc->led_state[1] << 2;
1687 report->leds_bitmap |= sc->led_state[2] << 3;
1688 report->leds_bitmap |= sc->led_state[3] << 4;
1690 /* Set flag for all leds off, required for 3rd party INTEC controller */
1691 if ((report->leds_bitmap & 0x1E) == 0)
1692 report->leds_bitmap |= 0x20;
1695 * The LEDs in the report are indexed in reverse order to their
1696 * corresponding light on the controller.
1697 * Index 0 = LED 4, index 1 = LED 3, etc...
1699 * In the case of both delay values being zero (blinking disabled) the
1700 * default report values should be used or the controller LED will be
1703 for (n = 0; n < 4; n++) {
1704 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
1705 report->led[3 - n].duty_off = sc->led_delay_off[n];
1706 report->led[3 - n].duty_on = sc->led_delay_on[n];
1710 hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
1711 sizeof(struct sixaxis_output_report),
1712 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
1715 static void dualshock4_state_worker(struct work_struct *work)
1717 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1718 struct hid_device *hdev = sc->hdev;
1719 __u8 *buf = sc->output_report_dmabuf;
1722 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1723 memset(buf, 0, DS4_REPORT_0x05_SIZE);
1728 memset(buf, 0, DS4_REPORT_0x11_SIZE);
1735 #ifdef CONFIG_SONY_FF
1736 buf[offset++] = sc->right;
1737 buf[offset++] = sc->left;
1742 /* LED 3 is the global control */
1743 if (sc->led_state[3]) {
1744 buf[offset++] = sc->led_state[0];
1745 buf[offset++] = sc->led_state[1];
1746 buf[offset++] = sc->led_state[2];
1751 /* If both delay values are zero the DualShock 4 disables blinking. */
1752 buf[offset++] = sc->led_delay_on[3];
1753 buf[offset++] = sc->led_delay_off[3];
1755 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1756 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
1758 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
1759 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
1762 static void motion_state_worker(struct work_struct *work)
1764 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1765 struct hid_device *hdev = sc->hdev;
1766 struct motion_output_report_02 *report =
1767 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1769 memset(report, 0, MOTION_REPORT_0x02_SIZE);
1771 report->type = 0x02; /* set leds */
1772 report->r = sc->led_state[0];
1773 report->g = sc->led_state[1];
1774 report->b = sc->led_state[2];
1776 #ifdef CONFIG_SONY_FF
1777 report->rumble = max(sc->right, sc->left);
1780 hid_hw_output_report(hdev, (__u8 *)report, MOTION_REPORT_0x02_SIZE);
1783 static int sony_allocate_output_report(struct sony_sc *sc)
1785 if (sc->quirks & SIXAXIS_CONTROLLER)
1786 sc->output_report_dmabuf =
1787 kmalloc(sizeof(union sixaxis_output_report_01),
1789 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1790 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1792 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1793 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1795 else if (sc->quirks & MOTION_CONTROLLER)
1796 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
1801 if (!sc->output_report_dmabuf)
1807 #ifdef CONFIG_SONY_FF
1808 static int sony_play_effect(struct input_dev *dev, void *data,
1809 struct ff_effect *effect)
1811 struct hid_device *hid = input_get_drvdata(dev);
1812 struct sony_sc *sc = hid_get_drvdata(hid);
1814 if (effect->type != FF_RUMBLE)
1817 sc->left = effect->u.rumble.strong_magnitude / 256;
1818 sc->right = effect->u.rumble.weak_magnitude / 256;
1820 schedule_work(&sc->state_worker);
1824 static int sony_init_ff(struct sony_sc *sc)
1826 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1827 struct hid_input, list);
1828 struct input_dev *input_dev = hidinput->input;
1830 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1831 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1835 static int sony_init_ff(struct sony_sc *sc)
1842 static int sony_battery_get_property(struct power_supply *psy,
1843 enum power_supply_property psp,
1844 union power_supply_propval *val)
1846 struct sony_sc *sc = power_supply_get_drvdata(psy);
1847 unsigned long flags;
1849 u8 battery_charging, battery_capacity, cable_state;
1851 spin_lock_irqsave(&sc->lock, flags);
1852 battery_charging = sc->battery_charging;
1853 battery_capacity = sc->battery_capacity;
1854 cable_state = sc->cable_state;
1855 spin_unlock_irqrestore(&sc->lock, flags);
1858 case POWER_SUPPLY_PROP_PRESENT:
1861 case POWER_SUPPLY_PROP_SCOPE:
1862 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1864 case POWER_SUPPLY_PROP_CAPACITY:
1865 val->intval = battery_capacity;
1867 case POWER_SUPPLY_PROP_STATUS:
1868 if (battery_charging)
1869 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1871 if (battery_capacity == 100 && cable_state)
1872 val->intval = POWER_SUPPLY_STATUS_FULL;
1874 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1883 static int sony_battery_probe(struct sony_sc *sc)
1885 struct power_supply_config psy_cfg = { .drv_data = sc, };
1886 struct hid_device *hdev = sc->hdev;
1890 * Set the default battery level to 100% to avoid low battery warnings
1891 * if the battery is polled before the first device report is received.
1893 sc->battery_capacity = 100;
1895 sc->battery_desc.properties = sony_battery_props;
1896 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
1897 sc->battery_desc.get_property = sony_battery_get_property;
1898 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
1899 sc->battery_desc.use_for_apm = 0;
1900 sc->battery_desc.name = kasprintf(GFP_KERNEL,
1901 "sony_controller_battery_%pMR",
1903 if (!sc->battery_desc.name)
1906 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
1908 if (IS_ERR(sc->battery)) {
1909 ret = PTR_ERR(sc->battery);
1910 hid_err(hdev, "Unable to register battery device\n");
1914 power_supply_powers(sc->battery, &hdev->dev);
1918 kfree(sc->battery_desc.name);
1919 sc->battery_desc.name = NULL;
1923 static void sony_battery_remove(struct sony_sc *sc)
1925 if (!sc->battery_desc.name)
1928 power_supply_unregister(sc->battery);
1929 kfree(sc->battery_desc.name);
1930 sc->battery_desc.name = NULL;
1934 * If a controller is plugged in via USB while already connected via Bluetooth
1935 * it will show up as two devices. A global list of connected controllers and
1936 * their MAC addresses is maintained to ensure that a device is only connected
1939 static int sony_check_add_dev_list(struct sony_sc *sc)
1941 struct sony_sc *entry;
1942 unsigned long flags;
1945 spin_lock_irqsave(&sony_dev_list_lock, flags);
1947 list_for_each_entry(entry, &sony_device_list, list_node) {
1948 ret = memcmp(sc->mac_address, entry->mac_address,
1949 sizeof(sc->mac_address));
1952 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1959 list_add(&(sc->list_node), &sony_device_list);
1962 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1966 static void sony_remove_dev_list(struct sony_sc *sc)
1968 unsigned long flags;
1970 if (sc->list_node.next) {
1971 spin_lock_irqsave(&sony_dev_list_lock, flags);
1972 list_del(&(sc->list_node));
1973 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1977 static int sony_get_bt_devaddr(struct sony_sc *sc)
1981 /* HIDP stores the device MAC address as a string in the uniq field. */
1982 ret = strlen(sc->hdev->uniq);
1986 ret = sscanf(sc->hdev->uniq,
1987 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1988 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1989 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1997 static int sony_check_add(struct sony_sc *sc)
2002 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
2003 (sc->quirks & MOTION_CONTROLLER_BT) ||
2004 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2006 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2007 * address from the uniq string where HIDP stores it.
2008 * As uniq cannot be guaranteed to be a MAC address in all cases
2009 * a failure of this function should not prevent the connection.
2011 if (sony_get_bt_devaddr(sc) < 0) {
2012 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2015 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
2016 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
2021 * The MAC address of a DS4 controller connected via USB can be
2022 * retrieved with feature report 0x81. The address begins at
2025 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2026 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
2027 HID_REQ_GET_REPORT);
2029 if (ret != DS4_REPORT_0x81_SIZE) {
2030 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
2031 ret = ret < 0 ? ret : -EINVAL;
2035 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
2036 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
2037 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2042 * The MAC address of a Sixaxis controller connected via USB can
2043 * be retrieved with feature report 0xf2. The address begins at
2046 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2047 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2048 HID_REQ_GET_REPORT);
2050 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
2051 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
2052 ret = ret < 0 ? ret : -EINVAL;
2057 * The Sixaxis device MAC in the report is big-endian and must
2060 for (n = 0; n < 6; n++)
2061 sc->mac_address[5-n] = buf[4+n];
2066 ret = sony_check_add_dev_list(sc);
2075 static int sony_set_device_id(struct sony_sc *sc)
2080 * Only DualShock 4 or Sixaxis controllers get an id.
2081 * All others are set to -1.
2083 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2084 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2085 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2091 sc->device_id = ret;
2099 static void sony_release_device_id(struct sony_sc *sc)
2101 if (sc->device_id >= 0) {
2102 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2107 static inline void sony_init_work(struct sony_sc *sc,
2108 void (*worker)(struct work_struct *))
2110 if (!sc->worker_initialized)
2111 INIT_WORK(&sc->state_worker, worker);
2113 sc->worker_initialized = 1;
2116 static inline void sony_cancel_work_sync(struct sony_sc *sc)
2118 if (sc->worker_initialized)
2119 cancel_work_sync(&sc->state_worker);
2122 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2125 unsigned long quirks = id->driver_data;
2127 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2129 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2131 hid_err(hdev, "can't alloc sony descriptor\n");
2135 spin_lock_init(&sc->lock);
2137 sc->quirks = quirks;
2138 hid_set_drvdata(hdev, sc);
2141 ret = hid_parse(hdev);
2143 hid_err(hdev, "parse failed\n");
2147 if (sc->quirks & VAIO_RDESC_CONSTANT)
2148 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2149 else if (sc->quirks & SIXAXIS_CONTROLLER)
2150 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2152 ret = hid_hw_start(hdev, connect_mask);
2154 hid_err(hdev, "hw start failed\n");
2158 ret = sony_set_device_id(sc);
2160 hid_err(hdev, "failed to allocate the device id\n");
2164 ret = sony_allocate_output_report(sc);
2166 hid_err(hdev, "failed to allocate the output report buffer\n");
2170 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
2172 * The Sony Sixaxis does not handle HID Output Reports on the
2173 * Interrupt EP like it could, so we need to force HID Output
2174 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2176 * There is also another issue about HID Output Reports via USB,
2177 * the Sixaxis does not want the report_id as part of the data
2178 * packet, so we have to discard buf[0] when sending the actual
2179 * control message, even for numbered reports, humpf!
2181 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2182 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
2183 ret = sixaxis_set_operational_usb(hdev);
2184 sony_init_work(sc, sixaxis_state_worker);
2185 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
2187 * The Sixaxis wants output reports sent on the ctrl endpoint
2188 * when connected via Bluetooth.
2190 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2191 ret = sixaxis_set_operational_bt(hdev);
2192 sony_init_work(sc, sixaxis_state_worker);
2193 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
2194 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
2196 * The DualShock 4 wants output reports sent on the ctrl
2197 * endpoint when connected via Bluetooth.
2199 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2200 ret = dualshock4_set_operational_bt(hdev);
2202 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2207 sony_init_work(sc, dualshock4_state_worker);
2208 } else if (sc->quirks & MOTION_CONTROLLER) {
2209 sony_init_work(sc, motion_state_worker);
2217 ret = sony_check_add(sc);
2221 if (sc->quirks & SONY_LED_SUPPORT) {
2222 ret = sony_leds_init(sc);
2227 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2228 ret = sony_battery_probe(sc);
2232 /* Open the device to receive reports with battery info */
2233 ret = hid_hw_open(hdev);
2235 hid_err(hdev, "hw open failed\n");
2240 if (sc->quirks & SONY_FF_SUPPORT) {
2241 ret = sony_init_ff(sc);
2250 if (sc->quirks & SONY_LED_SUPPORT)
2251 sony_leds_remove(sc);
2252 if (sc->quirks & SONY_BATTERY_SUPPORT)
2253 sony_battery_remove(sc);
2254 sony_cancel_work_sync(sc);
2255 kfree(sc->output_report_dmabuf);
2256 sony_remove_dev_list(sc);
2257 sony_release_device_id(sc);
2262 static void sony_remove(struct hid_device *hdev)
2264 struct sony_sc *sc = hid_get_drvdata(hdev);
2266 if (sc->quirks & SONY_LED_SUPPORT)
2267 sony_leds_remove(sc);
2269 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2271 sony_battery_remove(sc);
2274 sony_cancel_work_sync(sc);
2276 kfree(sc->output_report_dmabuf);
2278 sony_remove_dev_list(sc);
2280 sony_release_device_id(sc);
2285 static const struct hid_device_id sony_devices[] = {
2286 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2287 .driver_data = SIXAXIS_CONTROLLER_USB },
2288 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2289 .driver_data = SIXAXIS_CONTROLLER_USB },
2290 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
2291 .driver_data = MOTION_CONTROLLER_USB },
2292 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
2293 .driver_data = MOTION_CONTROLLER_BT },
2294 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2295 .driver_data = SIXAXIS_CONTROLLER_BT },
2296 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2297 .driver_data = VAIO_RDESC_CONSTANT },
2298 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2299 .driver_data = VAIO_RDESC_CONSTANT },
2300 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2301 * Logitech joystick from the device descriptor. */
2302 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2303 .driver_data = BUZZ_CONTROLLER },
2304 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2305 .driver_data = BUZZ_CONTROLLER },
2306 /* PS3 BD Remote Control */
2307 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2308 .driver_data = PS3REMOTE },
2309 /* Logitech Harmony Adapter for PS3 */
2310 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2311 .driver_data = PS3REMOTE },
2312 /* SMK-Link PS3 BD Remote Control */
2313 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2314 .driver_data = PS3REMOTE },
2315 /* Sony Dualshock 4 controllers for PS4 */
2316 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
2317 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2318 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
2319 .driver_data = DUALSHOCK4_CONTROLLER_BT },
2322 MODULE_DEVICE_TABLE(hid, sony_devices);
2324 static struct hid_driver sony_driver = {
2326 .id_table = sony_devices,
2327 .input_mapping = sony_mapping,
2328 .input_configured = sony_input_configured,
2329 .probe = sony_probe,
2330 .remove = sony_remove,
2331 .report_fixup = sony_report_fixup,
2332 .raw_event = sony_raw_event
2335 static int __init sony_init(void)
2337 dbg_hid("Sony:%s\n", __func__);
2339 return hid_register_driver(&sony_driver);
2342 static void __exit sony_exit(void)
2344 dbg_hid("Sony:%s\n", __func__);
2346 hid_unregister_driver(&sony_driver);
2347 ida_destroy(&sony_device_id_allocator);
2349 module_init(sony_init);
2350 module_exit(sony_exit);
2352 MODULE_LICENSE("GPL");