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