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 */ 103 static 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 */ 110 static const char ghl_ps4_magic_data[] = { 111 0x30, 0x02, 0x08, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00 112 }; 113 114 /* PS/3 Motion controller */ 115 static 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 211 static 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 277 static 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 }; 297 static 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 333 static 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 */ 378 static 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 */ 387 static 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 407 static 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 414 static 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 434 static const unsigned int rb4_absmap[] = { 435 [0x30] = ABS_X, 436 [0x31] = ABS_Y, 437 }; 438 439 static const unsigned int ps3_turntable_absmap[] = { 440 [0x32] = ABS_X, 441 [0x35] = ABS_Y, 442 }; 443 444 static 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 460 static 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 467 struct 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; 474 static_assert(sizeof(struct sixaxis_led) == 5); 475 476 struct 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; 483 static_assert(sizeof(struct sixaxis_rumble) == 5); 484 485 struct 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; 493 static_assert(sizeof(struct sixaxis_output_report) == 36); 494 495 union sixaxis_output_report_01 { 496 struct sixaxis_output_report data; 497 u8 buf[36]; 498 }; 499 static_assert(sizeof(union sixaxis_output_report_01) == 36); 500 501 struct motion_output_report_02 { 502 u8 type, zero; 503 u8 r, g, b; 504 u8 zero2; 505 u8 rumble; 506 }; 507 static_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 520 static DEFINE_SPINLOCK(sony_dev_list_lock); 521 static LIST_HEAD(sony_device_list); 522 static DEFINE_IDA(sony_device_id_allocator); 523 524 enum sony_worker { 525 SONY_WORKER_STATE 526 }; 527 528 struct 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 567 static void sony_set_leds(struct sony_sc *sc); 568 569 static 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 584 static 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 594 static 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 604 static 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 */ 642 static 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 669 static 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 700 static 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 718 static 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 733 static 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 755 static 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 762 static 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 769 static 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 803 static 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 851 static 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 899 static 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 949 static 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 998 static 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 1072 static 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 1089 static 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 1143 static 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 && size == 49) { 1173 sixaxis_parse_report(sc, rd, size); 1174 } else if ((sc->quirks & NSG_MRXU_REMOTE) && rd[0] == 0x02 && size >= 12) { 1175 nsg_mrxu_parse_report(sc, rd, size); 1176 return 1; 1177 } else if ((sc->quirks & RB4_GUITAR_PS4_USB) && rd[0] == 0x01 && size == 64) { 1178 rb4_ps4_guitar_parse_report(sc, rd, size); 1179 return 1; 1180 } else if ((sc->quirks & RB4_GUITAR_PS4_BT) && rd[0] == 0x01 && size == 78) { 1181 rb4_ps4_guitar_parse_report(sc, rd, size); 1182 return 1; 1183 } else if ((sc->quirks & RB4_GUITAR_PS5) && rd[0] == 0x01 && size == 64) { 1184 rb4_ps5_guitar_parse_report(sc, rd, size); 1185 return 1; 1186 } 1187 1188 /* Rock Band 3 PS3 Pro instruments set rd[24] to 0xE0 when they're 1189 * sending full reports, and 0x02 when only sending navigation. 1190 */ 1191 if ((sc->quirks & RB3_PRO_INSTRUMENT) && size >= 25 && rd[24] == 0x02) { 1192 /* Only attempt to enable full report every 8 seconds */ 1193 if (time_after(jiffies, sc->rb3_pro_poke_jiffies)) { 1194 sc->rb3_pro_poke_jiffies = jiffies + secs_to_jiffies(8); 1195 rb3_pro_instrument_enable_full_report(sc); 1196 } 1197 } 1198 1199 if (sc->defer_initialization) { 1200 sc->defer_initialization = 0; 1201 sony_schedule_work(sc, SONY_WORKER_STATE); 1202 } 1203 1204 return 0; 1205 } 1206 1207 static int sony_mapping(struct hid_device *hdev, struct hid_input *hi, 1208 struct hid_field *field, struct hid_usage *usage, 1209 unsigned long **bit, int *max) 1210 { 1211 struct sony_sc *sc = hid_get_drvdata(hdev); 1212 int ret; 1213 1214 if (sc->quirks & BUZZ_CONTROLLER) { 1215 unsigned int key = usage->hid & HID_USAGE; 1216 1217 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON) 1218 return -1; 1219 1220 switch (usage->collection_index) { 1221 case 1: 1222 if (key >= ARRAY_SIZE(buzz_keymap)) 1223 return -1; 1224 1225 key = buzz_keymap[key]; 1226 if (!key) 1227 return -1; 1228 break; 1229 default: 1230 return -1; 1231 } 1232 1233 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); 1234 return 1; 1235 } 1236 1237 if (sc->quirks & PS3REMOTE) 1238 return ps3remote_mapping(hdev, hi, field, usage, bit, max); 1239 1240 if (sc->quirks & NAVIGATION_CONTROLLER) 1241 return navigation_mapping(hdev, hi, field, usage, bit, max); 1242 1243 if (sc->quirks & SIXAXIS_CONTROLLER) 1244 return sixaxis_mapping(hdev, hi, field, usage, bit, max); 1245 1246 /* INSTRUMENT quirk is used as a base mapping for instruments */ 1247 if (sc->quirks & INSTRUMENT) { 1248 ret = instrument_mapping(hdev, hi, field, usage, bit, max); 1249 if (ret != 0) 1250 return ret; 1251 } 1252 1253 if (sc->quirks & GH_GUITAR_TILT) 1254 return gh_guitar_mapping(hdev, hi, field, usage, bit, max); 1255 1256 if (sc->quirks & DJH_TURNTABLE) 1257 return djh_turntable_mapping(hdev, hi, field, usage, bit, max); 1258 1259 if (sc->quirks & (RB4_GUITAR_PS4_USB | RB4_GUITAR_PS4_BT)) 1260 return rb4_guitar_mapping(hdev, hi, field, usage, bit, max); 1261 1262 if (sc->quirks & RB4_GUITAR_PS5) 1263 return rb4_guitar_mapping(hdev, hi, field, usage, bit, max); 1264 1265 /* Let hid-core decide for the others */ 1266 return 0; 1267 } 1268 1269 static int sony_register_touchpad(struct sony_sc *sc, int touch_count, 1270 int w, int h, int touch_major, int touch_minor, int orientation) 1271 { 1272 size_t name_sz; 1273 char *name; 1274 int ret; 1275 1276 sc->touchpad = devm_input_allocate_device(&sc->hdev->dev); 1277 if (!sc->touchpad) 1278 return -ENOMEM; 1279 1280 input_set_drvdata(sc->touchpad, sc); 1281 sc->touchpad->dev.parent = &sc->hdev->dev; 1282 sc->touchpad->phys = sc->hdev->phys; 1283 sc->touchpad->uniq = sc->hdev->uniq; 1284 sc->touchpad->id.bustype = sc->hdev->bus; 1285 sc->touchpad->id.vendor = sc->hdev->vendor; 1286 sc->touchpad->id.product = sc->hdev->product; 1287 sc->touchpad->id.version = sc->hdev->version; 1288 1289 /* This suffix was originally apended when hid-sony also 1290 * supported DS4 devices. The DS4 was implemented using multiple 1291 * evdev nodes and hence had the need to separete them out using 1292 * a suffix. Other devices which were added later like Sony TV remotes 1293 * inhirited this suffix. 1294 */ 1295 name_sz = strlen(sc->hdev->name) + sizeof(TOUCHPAD_SUFFIX); 1296 name = devm_kzalloc(&sc->hdev->dev, name_sz, GFP_KERNEL); 1297 if (!name) 1298 return -ENOMEM; 1299 snprintf(name, name_sz, "%s" TOUCHPAD_SUFFIX, sc->hdev->name); 1300 sc->touchpad->name = name; 1301 1302 /* We map the button underneath the touchpad to BTN_LEFT. */ 1303 __set_bit(EV_KEY, sc->touchpad->evbit); 1304 __set_bit(BTN_LEFT, sc->touchpad->keybit); 1305 __set_bit(INPUT_PROP_BUTTONPAD, sc->touchpad->propbit); 1306 1307 input_set_abs_params(sc->touchpad, ABS_MT_POSITION_X, 0, w, 0, 0); 1308 input_set_abs_params(sc->touchpad, ABS_MT_POSITION_Y, 0, h, 0, 0); 1309 1310 if (touch_major > 0) { 1311 input_set_abs_params(sc->touchpad, ABS_MT_TOUCH_MAJOR, 1312 0, touch_major, 0, 0); 1313 if (touch_minor > 0) 1314 input_set_abs_params(sc->touchpad, ABS_MT_TOUCH_MINOR, 1315 0, touch_minor, 0, 0); 1316 if (orientation > 0) 1317 input_set_abs_params(sc->touchpad, ABS_MT_ORIENTATION, 1318 0, orientation, 0, 0); 1319 } 1320 1321 if (sc->quirks & NSG_MRXU_REMOTE) 1322 __set_bit(EV_REL, sc->touchpad->evbit); 1323 1324 ret = input_mt_init_slots(sc->touchpad, touch_count, INPUT_MT_POINTER); 1325 if (ret < 0) 1326 return ret; 1327 1328 ret = input_register_device(sc->touchpad); 1329 if (ret < 0) 1330 return ret; 1331 1332 return 0; 1333 } 1334 1335 static int sony_register_sensors(struct sony_sc *sc) 1336 { 1337 size_t name_sz; 1338 char *name; 1339 int ret; 1340 1341 sc->sensor_dev = devm_input_allocate_device(&sc->hdev->dev); 1342 if (!sc->sensor_dev) 1343 return -ENOMEM; 1344 1345 input_set_drvdata(sc->sensor_dev, sc); 1346 sc->sensor_dev->dev.parent = &sc->hdev->dev; 1347 sc->sensor_dev->phys = sc->hdev->phys; 1348 sc->sensor_dev->uniq = sc->hdev->uniq; 1349 sc->sensor_dev->id.bustype = sc->hdev->bus; 1350 sc->sensor_dev->id.vendor = sc->hdev->vendor; 1351 sc->sensor_dev->id.product = sc->hdev->product; 1352 sc->sensor_dev->id.version = sc->hdev->version; 1353 1354 /* Append a suffix to the controller name as there are various 1355 * DS4 compatible non-Sony devices with different names. 1356 */ 1357 name_sz = strlen(sc->hdev->name) + sizeof(SENSOR_SUFFIX); 1358 name = devm_kzalloc(&sc->hdev->dev, name_sz, GFP_KERNEL); 1359 if (!name) 1360 return -ENOMEM; 1361 snprintf(name, name_sz, "%s" SENSOR_SUFFIX, sc->hdev->name); 1362 sc->sensor_dev->name = name; 1363 1364 if (sc->quirks & SIXAXIS_CONTROLLER) { 1365 /* For the DS3 we only support the accelerometer, which works 1366 * quite well even without calibration. The device also has 1367 * a 1-axis gyro, but it is very difficult to manage from within 1368 * the driver even to get data, the sensor is inaccurate and 1369 * the behavior is very different between hardware revisions. 1370 */ 1371 input_set_abs_params(sc->sensor_dev, ABS_X, -512, 511, 4, 0); 1372 input_set_abs_params(sc->sensor_dev, ABS_Y, -512, 511, 4, 0); 1373 input_set_abs_params(sc->sensor_dev, ABS_Z, -512, 511, 4, 0); 1374 input_abs_set_res(sc->sensor_dev, ABS_X, SIXAXIS_ACC_RES_PER_G); 1375 input_abs_set_res(sc->sensor_dev, ABS_Y, SIXAXIS_ACC_RES_PER_G); 1376 input_abs_set_res(sc->sensor_dev, ABS_Z, SIXAXIS_ACC_RES_PER_G); 1377 } 1378 1379 __set_bit(INPUT_PROP_ACCELEROMETER, sc->sensor_dev->propbit); 1380 1381 ret = input_register_device(sc->sensor_dev); 1382 if (ret < 0) 1383 return ret; 1384 1385 return 0; 1386 } 1387 1388 /* 1389 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller 1390 * to "operational". Without this, the ps3 controller will not report any 1391 * events. 1392 */ 1393 static int sixaxis_set_operational_usb(struct hid_device *hdev) 1394 { 1395 struct sony_sc *sc = hid_get_drvdata(hdev); 1396 const int buf_size = 1397 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE); 1398 u8 *buf; 1399 int ret; 1400 1401 buf = kmalloc(buf_size, GFP_KERNEL); 1402 if (!buf) 1403 return -ENOMEM; 1404 1405 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE, 1406 HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 1407 if (ret < 0) { 1408 hid_err(hdev, "can't set operational mode: step 1\n"); 1409 goto out; 1410 } 1411 1412 /* 1413 * Some compatible controllers like the Speedlink Strike FX and 1414 * Gasia need another query plus an USB interrupt to get operational. 1415 */ 1416 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE, 1417 HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 1418 if (ret < 0) { 1419 hid_err(hdev, "can't set operational mode: step 2\n"); 1420 goto out; 1421 } 1422 1423 /* 1424 * But the USB interrupt would cause SHANWAN controllers to 1425 * start rumbling non-stop, so skip step 3 for these controllers. 1426 */ 1427 if (sc->quirks & SHANWAN_GAMEPAD) 1428 goto out; 1429 1430 ret = hid_hw_output_report(hdev, buf, 1); 1431 if (ret < 0) { 1432 hid_info(hdev, "can't set operational mode: step 3, ignoring\n"); 1433 ret = 0; 1434 } 1435 1436 out: 1437 kfree(buf); 1438 1439 return ret; 1440 } 1441 1442 static int sixaxis_set_operational_bt(struct hid_device *hdev) 1443 { 1444 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 }; 1445 u8 *buf; 1446 int ret; 1447 1448 buf = kmemdup(report, sizeof(report), GFP_KERNEL); 1449 if (!buf) 1450 return -ENOMEM; 1451 1452 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report), 1453 HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 1454 1455 kfree(buf); 1456 1457 return ret; 1458 } 1459 1460 static void sixaxis_set_leds_from_id(struct sony_sc *sc) 1461 { 1462 static const u8 sixaxis_leds[10][4] = { 1463 { 0x01, 0x00, 0x00, 0x00 }, 1464 { 0x00, 0x01, 0x00, 0x00 }, 1465 { 0x00, 0x00, 0x01, 0x00 }, 1466 { 0x00, 0x00, 0x00, 0x01 }, 1467 { 0x01, 0x00, 0x00, 0x01 }, 1468 { 0x00, 0x01, 0x00, 0x01 }, 1469 { 0x00, 0x00, 0x01, 0x01 }, 1470 { 0x01, 0x00, 0x01, 0x01 }, 1471 { 0x00, 0x01, 0x01, 0x01 }, 1472 { 0x01, 0x01, 0x01, 0x01 } 1473 }; 1474 1475 int id = sc->device_id; 1476 1477 BUILD_BUG_ON(ARRAY_SIZE(sixaxis_leds[0]) > MAX_LEDS); 1478 1479 if (id < 0) 1480 return; 1481 1482 id %= 10; 1483 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id])); 1484 } 1485 1486 static void buzz_set_leds(struct sony_sc *sc) 1487 { 1488 struct hid_device *hdev = sc->hdev; 1489 struct list_head *report_list = 1490 &hdev->report_enum[HID_OUTPUT_REPORT].report_list; 1491 struct hid_report *report = list_entry(report_list->next, 1492 struct hid_report, list); 1493 s32 *value = report->field[0]->value; 1494 1495 BUILD_BUG_ON(4 > MAX_LEDS); 1496 1497 value[0] = 0x00; 1498 value[1] = sc->led_state[0] ? 0xff : 0x00; 1499 value[2] = sc->led_state[1] ? 0xff : 0x00; 1500 value[3] = sc->led_state[2] ? 0xff : 0x00; 1501 value[4] = sc->led_state[3] ? 0xff : 0x00; 1502 value[5] = 0x00; 1503 value[6] = 0x00; 1504 hid_hw_request(hdev, report, HID_REQ_SET_REPORT); 1505 } 1506 1507 static void sony_set_leds(struct sony_sc *sc) 1508 { 1509 if (!(sc->quirks & BUZZ_CONTROLLER)) 1510 sony_schedule_work(sc, SONY_WORKER_STATE); 1511 else 1512 buzz_set_leds(sc); 1513 } 1514 1515 static void sony_led_set_brightness(struct led_classdev *led, 1516 enum led_brightness value) 1517 { 1518 struct device *dev = led->dev->parent; 1519 struct hid_device *hdev = to_hid_device(dev); 1520 struct sony_sc *drv_data; 1521 1522 int n; 1523 int force_update; 1524 1525 drv_data = hid_get_drvdata(hdev); 1526 if (!drv_data) { 1527 hid_err(hdev, "No device data\n"); 1528 return; 1529 } 1530 1531 /* 1532 * The Sixaxis on USB will override any LED settings sent to it 1533 * and keep flashing all of the LEDs until the PS button is pressed. 1534 * Updates, even if redundant, must be always be sent to the 1535 * controller to avoid having to toggle the state of an LED just to 1536 * stop the flashing later on. 1537 */ 1538 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB); 1539 1540 for (n = 0; n < drv_data->led_count; n++) { 1541 if (led == drv_data->leds[n] && (force_update || 1542 (value != drv_data->led_state[n] || 1543 drv_data->led_delay_on[n] || 1544 drv_data->led_delay_off[n]))) { 1545 1546 drv_data->led_state[n] = value; 1547 1548 /* Setting the brightness stops the blinking */ 1549 drv_data->led_delay_on[n] = 0; 1550 drv_data->led_delay_off[n] = 0; 1551 1552 sony_set_leds(drv_data); 1553 break; 1554 } 1555 } 1556 } 1557 1558 static enum led_brightness sony_led_get_brightness(struct led_classdev *led) 1559 { 1560 struct device *dev = led->dev->parent; 1561 struct hid_device *hdev = to_hid_device(dev); 1562 struct sony_sc *drv_data; 1563 1564 int n; 1565 1566 drv_data = hid_get_drvdata(hdev); 1567 if (!drv_data) { 1568 hid_err(hdev, "No device data\n"); 1569 return LED_OFF; 1570 } 1571 1572 for (n = 0; n < drv_data->led_count; n++) { 1573 if (led == drv_data->leds[n]) 1574 return drv_data->led_state[n]; 1575 } 1576 1577 return LED_OFF; 1578 } 1579 1580 static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on, 1581 unsigned long *delay_off) 1582 { 1583 struct device *dev = led->dev->parent; 1584 struct hid_device *hdev = to_hid_device(dev); 1585 struct sony_sc *drv_data = hid_get_drvdata(hdev); 1586 int n; 1587 u8 new_on, new_off; 1588 1589 if (!drv_data) { 1590 hid_err(hdev, "No device data\n"); 1591 return -EINVAL; 1592 } 1593 1594 /* Max delay is 255 deciseconds or 2550 milliseconds */ 1595 if (*delay_on > 2550) 1596 *delay_on = 2550; 1597 if (*delay_off > 2550) 1598 *delay_off = 2550; 1599 1600 /* Blink at 1 Hz if both values are zero */ 1601 if (!*delay_on && !*delay_off) 1602 *delay_on = *delay_off = 500; 1603 1604 new_on = *delay_on / 10; 1605 new_off = *delay_off / 10; 1606 1607 for (n = 0; n < drv_data->led_count; n++) { 1608 if (led == drv_data->leds[n]) 1609 break; 1610 } 1611 1612 /* This LED is not registered on this device */ 1613 if (n >= drv_data->led_count) 1614 return -EINVAL; 1615 1616 /* Don't schedule work if the values didn't change */ 1617 if (new_on != drv_data->led_delay_on[n] || 1618 new_off != drv_data->led_delay_off[n]) { 1619 drv_data->led_delay_on[n] = new_on; 1620 drv_data->led_delay_off[n] = new_off; 1621 sony_schedule_work(drv_data, SONY_WORKER_STATE); 1622 } 1623 1624 return 0; 1625 } 1626 1627 static int sony_leds_init(struct sony_sc *sc) 1628 { 1629 struct hid_device *hdev = sc->hdev; 1630 int n, ret = 0; 1631 int use_color_names; 1632 struct led_classdev *led; 1633 size_t name_sz; 1634 char *name; 1635 size_t name_len; 1636 const char *name_fmt; 1637 static const char * const color_name_str[] = { "red", "green", "blue", 1638 "global" }; 1639 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 }; 1640 u8 use_hw_blink[MAX_LEDS] = { 0 }; 1641 1642 if (sc->quirks & BUZZ_CONTROLLER) { 1643 sc->led_count = 4; 1644 use_color_names = 0; 1645 name_len = strlen("::buzz#"); 1646 name_fmt = "%s::buzz%d"; 1647 /* Validate expected report characteristics. */ 1648 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7)) 1649 return -ENODEV; 1650 } else if (sc->quirks & MOTION_CONTROLLER) { 1651 sc->led_count = 3; 1652 memset(max_brightness, 255, 3); 1653 use_color_names = 1; 1654 name_len = 0; 1655 name_fmt = "%s:%s"; 1656 } else if (sc->quirks & NAVIGATION_CONTROLLER) { 1657 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00}; 1658 1659 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds)); 1660 sc->led_count = 1; 1661 memset(use_hw_blink, 1, 4); 1662 use_color_names = 0; 1663 name_len = strlen("::sony#"); 1664 name_fmt = "%s::sony%d"; 1665 } else { 1666 sixaxis_set_leds_from_id(sc); 1667 sc->led_count = 4; 1668 memset(use_hw_blink, 1, 4); 1669 use_color_names = 0; 1670 name_len = strlen("::sony#"); 1671 name_fmt = "%s::sony%d"; 1672 } 1673 1674 /* 1675 * Clear LEDs as we have no way of reading their initial state. This is 1676 * only relevant if the driver is loaded after somebody actively set the 1677 * LEDs to on 1678 */ 1679 sony_set_leds(sc); 1680 1681 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1; 1682 1683 for (n = 0; n < sc->led_count; n++) { 1684 1685 if (use_color_names) 1686 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_name_str[n]) + 2; 1687 1688 led = devm_kzalloc(&hdev->dev, sizeof(struct led_classdev) + name_sz, GFP_KERNEL); 1689 if (!led) 1690 return -ENOMEM; 1691 1692 name = (void *)(&led[1]); 1693 if (use_color_names) 1694 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_name_str[n]); 1695 else 1696 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1); 1697 led->name = name; 1698 led->brightness = sc->led_state[n]; 1699 led->max_brightness = max_brightness[n]; 1700 led->flags = LED_CORE_SUSPENDRESUME; 1701 led->brightness_get = sony_led_get_brightness; 1702 led->brightness_set = sony_led_set_brightness; 1703 1704 if (use_hw_blink[n]) 1705 led->blink_set = sony_led_blink_set; 1706 1707 sc->leds[n] = led; 1708 1709 ret = devm_led_classdev_register(&hdev->dev, led); 1710 if (ret) { 1711 hid_err(hdev, "Failed to register LED %d\n", n); 1712 return ret; 1713 } 1714 } 1715 1716 return 0; 1717 } 1718 1719 static void sixaxis_send_output_report(struct sony_sc *sc) 1720 { 1721 static const union sixaxis_output_report_01 default_report = { 1722 .buf = { 1723 0x01, 1724 0x01, 0xff, 0x00, 0xff, 0x00, 1725 0x00, 0x00, 0x00, 0x00, 0x00, 1726 0xff, 0x27, 0x10, 0x00, 0x32, 1727 0xff, 0x27, 0x10, 0x00, 0x32, 1728 0xff, 0x27, 0x10, 0x00, 0x32, 1729 0xff, 0x27, 0x10, 0x00, 0x32, 1730 0x00, 0x00, 0x00, 0x00, 0x00 1731 } 1732 }; 1733 struct sixaxis_output_report *report = 1734 (struct sixaxis_output_report *)sc->output_report_dmabuf; 1735 int n; 1736 1737 /* Initialize the report with default values */ 1738 memcpy(report, &default_report, sizeof(struct sixaxis_output_report)); 1739 1740 #ifdef CONFIG_SONY_FF 1741 report->rumble.right_motor_on = sc->right ? 1 : 0; 1742 report->rumble.left_motor_force = sc->left; 1743 #endif 1744 1745 report->leds_bitmap |= sc->led_state[0] << 1; 1746 report->leds_bitmap |= sc->led_state[1] << 2; 1747 report->leds_bitmap |= sc->led_state[2] << 3; 1748 report->leds_bitmap |= sc->led_state[3] << 4; 1749 1750 /* Set flag for all leds off, required for 3rd party INTEC controller */ 1751 if ((report->leds_bitmap & 0x1E) == 0) 1752 report->leds_bitmap |= 0x20; 1753 1754 /* 1755 * The LEDs in the report are indexed in reverse order to their 1756 * corresponding light on the controller. 1757 * Index 0 = LED 4, index 1 = LED 3, etc... 1758 * 1759 * In the case of both delay values being zero (blinking disabled) the 1760 * default report values should be used or the controller LED will be 1761 * always off. 1762 */ 1763 for (n = 0; n < 4; n++) { 1764 if (sc->led_delay_on[n] || sc->led_delay_off[n]) { 1765 report->led[3 - n].duty_off = sc->led_delay_off[n]; 1766 report->led[3 - n].duty_on = sc->led_delay_on[n]; 1767 } 1768 } 1769 1770 /* SHANWAN controllers require output reports via intr channel */ 1771 if (sc->quirks & SHANWAN_GAMEPAD) 1772 hid_hw_output_report(sc->hdev, (u8 *)report, 1773 sizeof(struct sixaxis_output_report)); 1774 else 1775 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report, 1776 sizeof(struct sixaxis_output_report), 1777 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); 1778 } 1779 1780 static void motion_send_output_report(struct sony_sc *sc) 1781 { 1782 struct hid_device *hdev = sc->hdev; 1783 struct motion_output_report_02 *report = 1784 (struct motion_output_report_02 *)sc->output_report_dmabuf; 1785 1786 memset(report, 0, MOTION_REPORT_0x02_SIZE); 1787 1788 report->type = 0x02; /* set leds */ 1789 report->r = sc->led_state[0]; 1790 report->g = sc->led_state[1]; 1791 report->b = sc->led_state[2]; 1792 1793 #ifdef CONFIG_SONY_FF 1794 report->rumble = max(sc->right, sc->left); 1795 #endif 1796 1797 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE); 1798 } 1799 1800 #ifdef CONFIG_SONY_FF 1801 static inline void sony_send_output_report(struct sony_sc *sc) 1802 { 1803 if (sc->send_output_report) 1804 sc->send_output_report(sc); 1805 } 1806 #endif 1807 1808 static void sony_state_worker(struct work_struct *work) 1809 { 1810 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker); 1811 1812 sc->send_output_report(sc); 1813 } 1814 1815 static int sony_allocate_output_report(struct sony_sc *sc) 1816 { 1817 if ((sc->quirks & SIXAXIS_CONTROLLER) || 1818 (sc->quirks & NAVIGATION_CONTROLLER)) 1819 sc->output_report_dmabuf = 1820 devm_kmalloc(&sc->hdev->dev, 1821 sizeof(union sixaxis_output_report_01), 1822 GFP_KERNEL); 1823 else if (sc->quirks & MOTION_CONTROLLER) 1824 sc->output_report_dmabuf = devm_kmalloc(&sc->hdev->dev, 1825 MOTION_REPORT_0x02_SIZE, 1826 GFP_KERNEL); 1827 else 1828 return 0; 1829 1830 if (!sc->output_report_dmabuf) 1831 return -ENOMEM; 1832 1833 return 0; 1834 } 1835 1836 #ifdef CONFIG_SONY_FF 1837 static int sony_play_effect(struct input_dev *dev, void *data, 1838 struct ff_effect *effect) 1839 { 1840 struct hid_device *hid = input_get_drvdata(dev); 1841 struct sony_sc *sc = hid_get_drvdata(hid); 1842 1843 if (effect->type != FF_RUMBLE) 1844 return 0; 1845 1846 sc->left = effect->u.rumble.strong_magnitude / 256; 1847 sc->right = effect->u.rumble.weak_magnitude / 256; 1848 1849 sony_schedule_work(sc, SONY_WORKER_STATE); 1850 return 0; 1851 } 1852 1853 static int sony_init_ff(struct sony_sc *sc) 1854 { 1855 struct hid_input *hidinput; 1856 struct input_dev *input_dev; 1857 1858 if (list_empty(&sc->hdev->inputs)) { 1859 hid_err(sc->hdev, "no inputs found\n"); 1860 return -ENODEV; 1861 } 1862 hidinput = list_entry(sc->hdev->inputs.next, struct hid_input, list); 1863 input_dev = hidinput->input; 1864 1865 input_set_capability(input_dev, EV_FF, FF_RUMBLE); 1866 return input_ff_create_memless(input_dev, NULL, sony_play_effect); 1867 } 1868 1869 #else 1870 static int sony_init_ff(struct sony_sc *sc) 1871 { 1872 return 0; 1873 } 1874 1875 #endif 1876 1877 static int sony_battery_get_property(struct power_supply *psy, 1878 enum power_supply_property psp, 1879 union power_supply_propval *val) 1880 { 1881 struct sony_sc *sc = power_supply_get_drvdata(psy); 1882 unsigned long flags; 1883 int ret = 0; 1884 u8 battery_capacity; 1885 int battery_status; 1886 1887 spin_lock_irqsave(&sc->lock, flags); 1888 battery_capacity = sc->battery_capacity; 1889 battery_status = sc->battery_status; 1890 spin_unlock_irqrestore(&sc->lock, flags); 1891 1892 switch (psp) { 1893 case POWER_SUPPLY_PROP_PRESENT: 1894 val->intval = 1; 1895 break; 1896 case POWER_SUPPLY_PROP_SCOPE: 1897 val->intval = POWER_SUPPLY_SCOPE_DEVICE; 1898 break; 1899 case POWER_SUPPLY_PROP_CAPACITY: 1900 val->intval = battery_capacity; 1901 break; 1902 case POWER_SUPPLY_PROP_STATUS: 1903 val->intval = battery_status; 1904 break; 1905 default: 1906 ret = -EINVAL; 1907 break; 1908 } 1909 return ret; 1910 } 1911 1912 static int sony_battery_probe(struct sony_sc *sc, int append_dev_id) 1913 { 1914 const char *battery_str_fmt = append_dev_id ? 1915 "sony_controller_battery_%pMR_%i" : 1916 "sony_controller_battery_%pMR"; 1917 struct power_supply_config psy_cfg = { .drv_data = sc, }; 1918 struct hid_device *hdev = sc->hdev; 1919 int ret; 1920 1921 /* 1922 * Set the default battery level to 100% to avoid low battery warnings 1923 * if the battery is polled before the first device report is received. 1924 */ 1925 sc->battery_capacity = 100; 1926 1927 sc->battery_desc.properties = sony_battery_props; 1928 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props); 1929 sc->battery_desc.get_property = sony_battery_get_property; 1930 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; 1931 sc->battery_desc.use_for_apm = 0; 1932 sc->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, 1933 battery_str_fmt, sc->mac_address, sc->device_id); 1934 if (!sc->battery_desc.name) 1935 return -ENOMEM; 1936 1937 sc->battery = devm_power_supply_register(&hdev->dev, &sc->battery_desc, 1938 &psy_cfg); 1939 if (IS_ERR(sc->battery)) { 1940 ret = PTR_ERR(sc->battery); 1941 hid_err(hdev, "Unable to register battery device\n"); 1942 return ret; 1943 } 1944 1945 power_supply_powers(sc->battery, &hdev->dev); 1946 return 0; 1947 } 1948 1949 /* 1950 * If a controller is plugged in via USB while already connected via Bluetooth 1951 * it will show up as two devices. A global list of connected controllers and 1952 * their MAC addresses is maintained to ensure that a device is only connected 1953 * once. 1954 * 1955 * Some USB-only devices masquerade as Sixaxis controllers and all have the 1956 * same dummy Bluetooth address, so a comparison of the connection type is 1957 * required. Devices are only rejected in the case where two devices have 1958 * matching Bluetooth addresses on different bus types. 1959 */ 1960 static inline int sony_compare_connection_type(struct sony_sc *sc0, 1961 struct sony_sc *sc1) 1962 { 1963 const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE); 1964 const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE); 1965 1966 return sc0_not_bt == sc1_not_bt; 1967 } 1968 1969 static int sony_check_add_dev_list(struct sony_sc *sc) 1970 { 1971 struct sony_sc *entry; 1972 unsigned long flags; 1973 int ret; 1974 1975 spin_lock_irqsave(&sony_dev_list_lock, flags); 1976 1977 list_for_each_entry(entry, &sony_device_list, list_node) { 1978 ret = memcmp(sc->mac_address, entry->mac_address, 1979 sizeof(sc->mac_address)); 1980 if (!ret) { 1981 if (sony_compare_connection_type(sc, entry)) { 1982 ret = 1; 1983 } else { 1984 ret = -EEXIST; 1985 hid_info(sc->hdev, 1986 "controller with MAC address %pMR already connected\n", 1987 sc->mac_address); 1988 } 1989 goto unlock; 1990 } 1991 } 1992 1993 ret = 0; 1994 list_add(&(sc->list_node), &sony_device_list); 1995 1996 unlock: 1997 spin_unlock_irqrestore(&sony_dev_list_lock, flags); 1998 return ret; 1999 } 2000 2001 static void sony_remove_dev_list(struct sony_sc *sc) 2002 { 2003 unsigned long flags; 2004 2005 if (sc->list_node.next) { 2006 spin_lock_irqsave(&sony_dev_list_lock, flags); 2007 list_del(&(sc->list_node)); 2008 spin_unlock_irqrestore(&sony_dev_list_lock, flags); 2009 } 2010 } 2011 2012 static int sony_get_bt_devaddr(struct sony_sc *sc) 2013 { 2014 int ret; 2015 2016 /* HIDP stores the device MAC address as a string in the uniq field. */ 2017 ret = strlen(sc->hdev->uniq); 2018 if (ret != 17) 2019 return -EINVAL; 2020 2021 ret = sscanf(sc->hdev->uniq, 2022 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 2023 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3], 2024 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]); 2025 2026 if (ret != 6) 2027 return -EINVAL; 2028 2029 return 0; 2030 } 2031 2032 static int sony_check_add(struct sony_sc *sc) 2033 { 2034 u8 *buf = NULL; 2035 int n, ret; 2036 2037 if ((sc->quirks & MOTION_CONTROLLER_BT) || 2038 (sc->quirks & NAVIGATION_CONTROLLER_BT) || 2039 (sc->quirks & SIXAXIS_CONTROLLER_BT)) { 2040 /* 2041 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC 2042 * address from the uniq string where HIDP stores it. 2043 * As uniq cannot be guaranteed to be a MAC address in all cases 2044 * a failure of this function should not prevent the connection. 2045 */ 2046 if (sony_get_bt_devaddr(sc) < 0) { 2047 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n"); 2048 return 0; 2049 } 2050 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) || 2051 (sc->quirks & NAVIGATION_CONTROLLER_USB)) { 2052 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL); 2053 if (!buf) 2054 return -ENOMEM; 2055 2056 /* 2057 * The MAC address of a Sixaxis controller connected via USB can 2058 * be retrieved with feature report 0xf2. The address begins at 2059 * offset 4. 2060 */ 2061 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, 2062 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT, 2063 HID_REQ_GET_REPORT); 2064 2065 if (ret != SIXAXIS_REPORT_0xF2_SIZE) { 2066 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n"); 2067 ret = ret < 0 ? ret : -EINVAL; 2068 goto out_free; 2069 } 2070 2071 /* 2072 * The Sixaxis device MAC in the report is big-endian and must 2073 * be byte-swapped. 2074 */ 2075 for (n = 0; n < 6; n++) 2076 sc->mac_address[5-n] = buf[4+n]; 2077 2078 snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq), 2079 "%pMR", sc->mac_address); 2080 } else { 2081 return 0; 2082 } 2083 2084 ret = sony_check_add_dev_list(sc); 2085 2086 out_free: 2087 2088 kfree(buf); 2089 2090 return ret; 2091 } 2092 2093 static int sony_set_device_id(struct sony_sc *sc) 2094 { 2095 int ret; 2096 2097 /* 2098 * Only Sixaxis controllers get an id. 2099 * All others are set to -1. 2100 */ 2101 if (sc->quirks & SIXAXIS_CONTROLLER) { 2102 ret = ida_alloc(&sony_device_id_allocator, GFP_KERNEL); 2103 if (ret < 0) { 2104 sc->device_id = -1; 2105 return ret; 2106 } 2107 sc->device_id = ret; 2108 } else { 2109 sc->device_id = -1; 2110 } 2111 2112 return 0; 2113 } 2114 2115 static void sony_release_device_id(struct sony_sc *sc) 2116 { 2117 if (sc->device_id >= 0) { 2118 ida_free(&sony_device_id_allocator, sc->device_id); 2119 sc->device_id = -1; 2120 } 2121 } 2122 2123 static inline void sony_init_output_report(struct sony_sc *sc, 2124 void (*send_output_report)(struct sony_sc *)) 2125 { 2126 sc->send_output_report = send_output_report; 2127 2128 if (!sc->state_worker_initialized) 2129 INIT_WORK(&sc->state_worker, sony_state_worker); 2130 2131 sc->state_worker_initialized = 1; 2132 } 2133 2134 static inline void sony_cancel_work_sync(struct sony_sc *sc) 2135 { 2136 unsigned long flags; 2137 2138 if (sc->state_worker_initialized) { 2139 spin_lock_irqsave(&sc->lock, flags); 2140 sc->state_worker_initialized = 0; 2141 spin_unlock_irqrestore(&sc->lock, flags); 2142 cancel_work_sync(&sc->state_worker); 2143 } 2144 } 2145 2146 static int sony_input_configured(struct hid_device *hdev, 2147 struct hid_input *hidinput) 2148 { 2149 struct sony_sc *sc = hid_get_drvdata(hdev); 2150 int append_dev_id; 2151 int ret; 2152 2153 ret = sony_set_device_id(sc); 2154 if (ret < 0) { 2155 hid_err(hdev, "failed to allocate the device id\n"); 2156 goto err_stop; 2157 } 2158 2159 ret = append_dev_id = sony_check_add(sc); 2160 if (ret < 0) 2161 goto err_stop; 2162 2163 ret = sony_allocate_output_report(sc); 2164 if (ret < 0) { 2165 hid_err(hdev, "failed to allocate the output report buffer\n"); 2166 goto err_stop; 2167 } 2168 2169 if (sc->quirks & NAVIGATION_CONTROLLER_USB) { 2170 /* 2171 * The Sony Sixaxis does not handle HID Output Reports on the 2172 * Interrupt EP like it could, so we need to force HID Output 2173 * Reports to use HID_REQ_SET_REPORT on the Control EP. 2174 * 2175 * There is also another issue about HID Output Reports via USB, 2176 * the Sixaxis does not want the report_id as part of the data 2177 * packet, so we have to discard buf[0] when sending the actual 2178 * control message, even for numbered reports, humpf! 2179 * 2180 * Additionally, the Sixaxis on USB isn't properly initialized 2181 * until the PS logo button is pressed and as such won't retain 2182 * any state set by an output report, so the initial 2183 * configuration report is deferred until the first input 2184 * report arrives. 2185 */ 2186 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; 2187 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID; 2188 sc->defer_initialization = 1; 2189 2190 ret = sixaxis_set_operational_usb(hdev); 2191 if (ret < 0) { 2192 hid_err(hdev, "Failed to set controller into operational mode\n"); 2193 goto err_stop; 2194 } 2195 2196 sony_init_output_report(sc, sixaxis_send_output_report); 2197 } else if (sc->quirks & NAVIGATION_CONTROLLER_BT) { 2198 /* 2199 * The Navigation controller wants output reports sent on the ctrl 2200 * endpoint when connected via Bluetooth. 2201 */ 2202 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; 2203 2204 ret = sixaxis_set_operational_bt(hdev); 2205 if (ret < 0) { 2206 hid_err(hdev, "Failed to set controller into operational mode\n"); 2207 goto err_stop; 2208 } 2209 2210 sony_init_output_report(sc, sixaxis_send_output_report); 2211 } else if (sc->quirks & RB3_PRO_INSTRUMENT) { 2212 /* 2213 * Rock Band 3 PS3 Pro Instruments also do not handle HID Output 2214 * Reports on the interrupt EP like they should, so we need to force 2215 * HID output reports to use HID_REQ_SET_REPORT on the Control EP. 2216 * 2217 * There is also another issue about HID Output Reports via USB, 2218 * these instruments do not want the report_id as part of the data 2219 * packet, so we have to discard buf[0] when sending the actual 2220 * control message, even for numbered reports. 2221 */ 2222 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; 2223 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID; 2224 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) { 2225 /* 2226 * The Sony Sixaxis does not handle HID Output Reports on the 2227 * Interrupt EP and the device only becomes active when the 2228 * PS button is pressed. See comment for Navigation controller 2229 * above for more details. 2230 */ 2231 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; 2232 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID; 2233 sc->defer_initialization = 1; 2234 2235 ret = sixaxis_set_operational_usb(hdev); 2236 if (ret < 0) { 2237 hid_err(hdev, "Failed to set controller into operational mode\n"); 2238 goto err_stop; 2239 } 2240 2241 ret = sony_register_sensors(sc); 2242 if (ret) { 2243 hid_err(sc->hdev, 2244 "Unable to initialize motion sensors: %d\n", ret); 2245 goto err_stop; 2246 } 2247 2248 sony_init_output_report(sc, sixaxis_send_output_report); 2249 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) { 2250 /* 2251 * The Sixaxis wants output reports sent on the ctrl endpoint 2252 * when connected via Bluetooth. 2253 */ 2254 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; 2255 2256 ret = sixaxis_set_operational_bt(hdev); 2257 if (ret < 0) { 2258 hid_err(hdev, "Failed to set controller into operational mode\n"); 2259 goto err_stop; 2260 } 2261 2262 ret = sony_register_sensors(sc); 2263 if (ret) { 2264 hid_err(sc->hdev, 2265 "Unable to initialize motion sensors: %d\n", ret); 2266 goto err_stop; 2267 } 2268 2269 sony_init_output_report(sc, sixaxis_send_output_report); 2270 } else if (sc->quirks & NSG_MRXU_REMOTE) { 2271 /* 2272 * The NSG-MRxU touchpad supports 2 touches and has a 2273 * resolution of 1667x1868 2274 */ 2275 ret = sony_register_touchpad(sc, 2, 2276 NSG_MRXU_MAX_X, NSG_MRXU_MAX_Y, 15, 15, 1); 2277 if (ret) { 2278 hid_err(sc->hdev, 2279 "Unable to initialize multi-touch slots: %d\n", 2280 ret); 2281 goto err_stop; 2282 } 2283 2284 } else if (sc->quirks & MOTION_CONTROLLER) { 2285 sony_init_output_report(sc, motion_send_output_report); 2286 } 2287 2288 if (sc->quirks & SONY_LED_SUPPORT) { 2289 ret = sony_leds_init(sc); 2290 if (ret < 0) 2291 goto err_stop; 2292 } 2293 2294 if (sc->quirks & SONY_BATTERY_SUPPORT) { 2295 ret = sony_battery_probe(sc, append_dev_id); 2296 if (ret < 0) 2297 goto err_stop; 2298 2299 /* Open the device to receive reports with battery info */ 2300 ret = hid_hw_open(hdev); 2301 if (ret < 0) { 2302 hid_err(hdev, "hw open failed\n"); 2303 goto err_stop; 2304 } 2305 } 2306 2307 if (sc->quirks & SONY_FF_SUPPORT) { 2308 ret = sony_init_ff(sc); 2309 if (ret < 0) 2310 goto err_close; 2311 } 2312 2313 sc->input_dev = hidinput->input; 2314 return 0; 2315 err_close: 2316 hid_hw_close(hdev); 2317 err_stop: 2318 sony_cancel_work_sync(sc); 2319 sony_remove_dev_list(sc); 2320 sony_release_device_id(sc); 2321 return ret; 2322 } 2323 2324 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) 2325 { 2326 int ret; 2327 unsigned long quirks = id->driver_data; 2328 struct sony_sc *sc; 2329 struct usb_device *usbdev; 2330 unsigned int connect_mask = HID_CONNECT_DEFAULT; 2331 2332 if (!strcmp(hdev->name, "FutureMax Dance Mat")) 2333 quirks |= FUTUREMAX_DANCE_MAT; 2334 2335 if (!strcmp(hdev->name, "SHANWAN PS3 GamePad") || 2336 !strcmp(hdev->name, "ShanWan PS(R) Ga`epad")) 2337 quirks |= SHANWAN_GAMEPAD; 2338 2339 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL); 2340 if (!sc) 2341 return -ENOMEM; 2342 2343 spin_lock_init(&sc->lock); 2344 2345 sc->quirks = quirks; 2346 hid_set_drvdata(hdev, sc); 2347 sc->hdev = hdev; 2348 2349 ret = hid_parse(hdev); 2350 if (ret) { 2351 hid_err(hdev, "parse failed\n"); 2352 return ret; 2353 } 2354 2355 if (sc->quirks & VAIO_RDESC_CONSTANT) 2356 connect_mask |= HID_CONNECT_HIDDEV_FORCE; 2357 else if (sc->quirks & SIXAXIS_CONTROLLER) 2358 connect_mask |= HID_CONNECT_HIDDEV_FORCE; 2359 2360 /* Patch the hw version on DS3 compatible devices, so applications can 2361 * distinguish between the default HID mappings and the mappings defined 2362 * by the Linux game controller spec. This is important for the SDL2 2363 * library, which has a game controller database, which uses device ids 2364 * in combination with version as a key. 2365 */ 2366 if (sc->quirks & SIXAXIS_CONTROLLER) 2367 hdev->version |= 0x8000; 2368 2369 ret = hid_hw_start(hdev, connect_mask); 2370 if (ret) { 2371 hid_err(hdev, "hw start failed\n"); 2372 return ret; 2373 } 2374 2375 /* sony_input_configured can fail, but this doesn't result 2376 * in hid_hw_start failures (intended). Check whether 2377 * the HID layer claimed the device else fail. 2378 * We don't know the actual reason for the failure, most 2379 * likely it is due to EEXIST in case of double connection 2380 * of USB and Bluetooth, but could have been due to ENOMEM 2381 * or other reasons as well. 2382 */ 2383 if (!(hdev->claimed & HID_CLAIMED_INPUT)) { 2384 hid_err(hdev, "failed to claim input\n"); 2385 ret = -ENODEV; 2386 goto err; 2387 } 2388 2389 if (sc->quirks & RB3_PRO_INSTRUMENT) 2390 sc->rb3_pro_poke_jiffies = 0; 2391 2392 if (sc->quirks & (GHL_GUITAR_PS3WIIU | GHL_GUITAR_PS4)) { 2393 if (!hid_is_usb(hdev)) { 2394 ret = -EINVAL; 2395 goto err; 2396 } 2397 2398 usbdev = to_usb_device(sc->hdev->dev.parent->parent); 2399 2400 sc->ghl_urb = usb_alloc_urb(0, GFP_ATOMIC); 2401 if (!sc->ghl_urb) { 2402 ret = -ENOMEM; 2403 goto err; 2404 } 2405 2406 if (sc->quirks & GHL_GUITAR_PS3WIIU) 2407 ret = ghl_init_urb(sc, usbdev, ghl_ps3wiiu_magic_data, 2408 ARRAY_SIZE(ghl_ps3wiiu_magic_data)); 2409 else if (sc->quirks & GHL_GUITAR_PS4) 2410 ret = ghl_init_urb(sc, usbdev, ghl_ps4_magic_data, 2411 ARRAY_SIZE(ghl_ps4_magic_data)); 2412 if (ret) { 2413 hid_err(hdev, "error preparing URB\n"); 2414 goto err; 2415 } 2416 2417 timer_setup(&sc->ghl_poke_timer, ghl_magic_poke, 0); 2418 mod_timer(&sc->ghl_poke_timer, 2419 jiffies + GHL_GUITAR_POKE_INTERVAL*HZ); 2420 } 2421 2422 return ret; 2423 2424 err: 2425 usb_free_urb(sc->ghl_urb); 2426 2427 hid_hw_stop(hdev); 2428 return ret; 2429 } 2430 2431 static void sony_remove(struct hid_device *hdev) 2432 { 2433 struct sony_sc *sc = hid_get_drvdata(hdev); 2434 2435 if (sc->quirks & (GHL_GUITAR_PS3WIIU | GHL_GUITAR_PS4)) { 2436 timer_delete_sync(&sc->ghl_poke_timer); 2437 usb_free_urb(sc->ghl_urb); 2438 } 2439 2440 hid_hw_close(hdev); 2441 2442 sony_cancel_work_sync(sc); 2443 2444 sony_remove_dev_list(sc); 2445 2446 sony_release_device_id(sc); 2447 2448 hid_hw_stop(hdev); 2449 } 2450 2451 2452 static int sony_suspend(struct hid_device *hdev, pm_message_t message) 2453 { 2454 #ifdef CONFIG_SONY_FF 2455 struct sony_sc *sc = hid_get_drvdata(hdev); 2456 2457 /* On suspend stop any running force-feedback events */ 2458 if (sc->quirks & SONY_FF_SUPPORT) { 2459 sc->left = sc->right = 0; 2460 sony_send_output_report(sc); 2461 } 2462 2463 #endif 2464 return 0; 2465 } 2466 2467 static int sony_resume(struct hid_device *hdev) 2468 { 2469 struct sony_sc *sc = hid_get_drvdata(hdev); 2470 2471 /* 2472 * The Sixaxis and navigation controllers on USB need to be 2473 * reinitialized on resume or they won't behave properly. 2474 */ 2475 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) || 2476 (sc->quirks & NAVIGATION_CONTROLLER_USB)) { 2477 sixaxis_set_operational_usb(sc->hdev); 2478 sc->defer_initialization = 1; 2479 } 2480 2481 return 0; 2482 } 2483 2484 static const struct hid_device_id sony_devices[] = { 2485 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER), 2486 .driver_data = SIXAXIS_CONTROLLER_USB }, 2487 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER), 2488 .driver_data = NAVIGATION_CONTROLLER_USB }, 2489 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER), 2490 .driver_data = NAVIGATION_CONTROLLER_BT }, 2491 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER), 2492 .driver_data = MOTION_CONTROLLER_USB }, 2493 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER), 2494 .driver_data = MOTION_CONTROLLER_BT }, 2495 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER), 2496 .driver_data = SIXAXIS_CONTROLLER_BT }, 2497 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE), 2498 .driver_data = VAIO_RDESC_CONSTANT }, 2499 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE), 2500 .driver_data = VAIO_RDESC_CONSTANT }, 2501 /* 2502 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as 2503 * Logitech joystick from the device descriptor. 2504 */ 2505 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER), 2506 .driver_data = BUZZ_CONTROLLER }, 2507 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER), 2508 .driver_data = BUZZ_CONTROLLER }, 2509 /* PS3 BD Remote Control */ 2510 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE), 2511 .driver_data = PS3REMOTE }, 2512 /* Logitech Harmony Adapter for PS3 */ 2513 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3), 2514 .driver_data = PS3REMOTE }, 2515 /* SMK-Link PS3 BD Remote Control */ 2516 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE), 2517 .driver_data = PS3REMOTE }, 2518 /* Nyko Core Controller for PS3 */ 2519 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER), 2520 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER }, 2521 /* SMK-Link NSG-MR5U Remote Control */ 2522 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_NSG_MR5U_REMOTE), 2523 .driver_data = NSG_MR5U_REMOTE_BT }, 2524 /* SMK-Link NSG-MR7U Remote Control */ 2525 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_NSG_MR7U_REMOTE), 2526 .driver_data = NSG_MR7U_REMOTE_BT }, 2527 /* Guitar Hero Live PS3 and Wii U guitar dongles */ 2528 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3WIIU_GHLIVE), 2529 .driver_data = GHL_GUITAR_PS3WIIU | GH_GUITAR_TILT | INSTRUMENT }, 2530 /* Guitar Hero PC Guitar Dongle */ 2531 { HID_USB_DEVICE(USB_VENDOR_ID_REDOCTANE, USB_DEVICE_ID_REDOCTANE_GUITAR_DONGLE), 2532 .driver_data = GH_GUITAR_TILT | INSTRUMENT }, 2533 /* Guitar Hero PS3 Guitar Dongle */ 2534 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_GH_GUITAR), 2535 .driver_data = GH_GUITAR_TILT | INSTRUMENT }, 2536 /* Guitar Hero PS3 Drum Dongle */ 2537 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_GH_DRUMS), 2538 .driver_data = INSTRUMENT }, 2539 /* DJ Hero PS3 Guitar Dongle */ 2540 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_DJH_TURNTABLE), 2541 .driver_data = DJH_TURNTABLE | INSTRUMENT }, 2542 /* Guitar Hero Live PS4 guitar dongles */ 2543 { HID_USB_DEVICE(USB_VENDOR_ID_REDOCTANE, USB_DEVICE_ID_REDOCTANE_PS4_GHLIVE_DONGLE), 2544 .driver_data = GHL_GUITAR_PS4 | GH_GUITAR_TILT | INSTRUMENT }, 2545 /* Rock Band 1 Wii instruments */ 2546 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB1_GUITAR), 2547 .driver_data = INSTRUMENT }, 2548 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB1_DRUMS), 2549 .driver_data = INSTRUMENT }, 2550 /* Rock Band 2 Wii instruments */ 2551 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB2_GUITAR), 2552 .driver_data = INSTRUMENT }, 2553 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB2_DRUMS), 2554 .driver_data = INSTRUMENT }, 2555 /* Rock Band 3 Wii instruments */ 2556 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB3_MPA_DRUMS_MODE), 2557 .driver_data = INSTRUMENT }, 2558 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB3_MUSTANG_GUITAR), 2559 .driver_data = INSTRUMENT }, 2560 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB3_MPA_MUSTANG_MODE), 2561 .driver_data = INSTRUMENT }, 2562 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB3_MPA_SQUIER_MODE), 2563 .driver_data = INSTRUMENT }, 2564 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB3_KEYBOARD), 2565 .driver_data = INSTRUMENT }, 2566 { HID_USB_DEVICE(USB_VENDOR_ID_HARMONIX, USB_DEVICE_ID_HARMONIX_WII_RB3_MPA_KEYBOARD_MODE), 2567 .driver_data = INSTRUMENT }, 2568 /* Rock Band 3 PS3 instruments */ 2569 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_RB_GUITAR), 2570 .driver_data = INSTRUMENT }, 2571 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_RB_DRUMS), 2572 .driver_data = INSTRUMENT }, 2573 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_RB3_MPA_DRUMS_MODE), 2574 .driver_data = INSTRUMENT }, 2575 /* Rock Band 3 PS3 Pro instruments */ 2576 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_RB3_MUSTANG_GUITAR), 2577 .driver_data = INSTRUMENT | RB3_PRO_INSTRUMENT }, 2578 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_RB3_MPA_MUSTANG_MODE), 2579 .driver_data = INSTRUMENT | RB3_PRO_INSTRUMENT }, 2580 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_RB3_MPA_SQUIER_MODE), 2581 .driver_data = INSTRUMENT | RB3_PRO_INSTRUMENT }, 2582 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_RB3_KEYBOARD), 2583 .driver_data = INSTRUMENT | RB3_PRO_INSTRUMENT }, 2584 { HID_USB_DEVICE(USB_VENDOR_ID_SONY_RHYTHM, USB_DEVICE_ID_SONY_PS3_RB3_MPA_KEYBOARD_MODE), 2585 .driver_data = INSTRUMENT | RB3_PRO_INSTRUMENT }, 2586 /* Rock Band 4 PS4 guitars */ 2587 { HID_USB_DEVICE(USB_VENDOR_ID_PDP, USB_DEVICE_ID_PDP_PS4_RIFFMASTER), 2588 .driver_data = RB4_GUITAR_PS4_USB | INSTRUMENT }, 2589 { HID_USB_DEVICE(USB_VENDOR_ID_CRKD, USB_DEVICE_ID_CRKD_PS4_GIBSON_SG), 2590 .driver_data = RB4_GUITAR_PS4_USB | INSTRUMENT }, 2591 { HID_USB_DEVICE(USB_VENDOR_ID_CRKD, USB_DEVICE_ID_CRKD_PS4_GIBSON_SG_DONGLE), 2592 .driver_data = RB4_GUITAR_PS4_USB | INSTRUMENT }, 2593 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_PDP, USB_DEVICE_ID_PDP_PS4_JAGUAR), 2594 .driver_data = RB4_GUITAR_PS4_BT | INSTRUMENT }, 2595 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MADCATZ, USB_DEVICE_ID_MADCATZ_PS4_STRATOCASTER), 2596 .driver_data = RB4_GUITAR_PS4_BT | INSTRUMENT }, 2597 /* Rock Band 4 PS5 guitars */ 2598 { HID_USB_DEVICE(USB_VENDOR_ID_PDP, USB_DEVICE_ID_PDP_PS5_RIFFMASTER), 2599 .driver_data = RB4_GUITAR_PS5 | INSTRUMENT }, 2600 { HID_USB_DEVICE(USB_VENDOR_ID_CRKD, USB_DEVICE_ID_CRKD_PS5_GIBSON_SG), 2601 .driver_data = RB4_GUITAR_PS5 | INSTRUMENT }, 2602 { HID_USB_DEVICE(USB_VENDOR_ID_CRKD, USB_DEVICE_ID_CRKD_PS5_GIBSON_SG_DONGLE), 2603 .driver_data = RB4_GUITAR_PS5 | INSTRUMENT }, 2604 { } 2605 }; 2606 MODULE_DEVICE_TABLE(hid, sony_devices); 2607 2608 static struct hid_driver sony_driver = { 2609 .name = "sony", 2610 .id_table = sony_devices, 2611 .input_mapping = sony_mapping, 2612 .input_configured = sony_input_configured, 2613 .probe = sony_probe, 2614 .remove = sony_remove, 2615 .report_fixup = sony_report_fixup, 2616 .raw_event = sony_raw_event, 2617 .suspend = pm_ptr(sony_suspend), 2618 .resume = pm_ptr(sony_resume), 2619 .reset_resume = pm_ptr(sony_resume), 2620 }; 2621 2622 static int __init sony_init(void) 2623 { 2624 dbg_hid("Sony:%s\n", __func__); 2625 2626 return hid_register_driver(&sony_driver); 2627 } 2628 2629 static void __exit sony_exit(void) 2630 { 2631 dbg_hid("Sony:%s\n", __func__); 2632 2633 hid_unregister_driver(&sony_driver); 2634 ida_destroy(&sony_device_id_allocator); 2635 } 2636 module_init(sony_init); 2637 module_exit(sony_exit); 2638 2639 MODULE_DESCRIPTION("HID driver for Sony / PS2 / PS3 BD / PS4 / PS5 devices"); 2640 MODULE_LICENSE("GPL"); 2641