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