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