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