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