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