Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'for-3.15/sony' into for-3.15/hid-core-ll-transport-cleanup

+703 -68
+6 -4
drivers/hid/Kconfig
··· 617 617 Support for Samsung InfraRed remote control or keyboards. 618 618 619 619 config HID_SONY 620 - tristate "Sony PS2/3 accessories" 620 + tristate "Sony PS2/3/4 accessories" 621 621 depends on USB_HID 622 622 depends on NEW_LEDS 623 623 depends on LEDS_CLASS 624 + select POWER_SUPPLY 624 625 ---help--- 625 626 Support for 626 627 627 628 * Sony PS3 6-axis controllers 629 + * Sony PS4 DualShock 4 controllers 628 630 * Buzz controllers 629 631 * Sony PS3 Blue-ray Disk Remote Control (Bluetooth) 630 632 * Logitech Harmony adapter for Sony Playstation 3 (Bluetooth) 631 633 632 634 config SONY_FF 633 - bool "Sony PS2/3 accessories force feedback support" 635 + bool "Sony PS2/3/4 accessories force feedback support" 634 636 depends on HID_SONY 635 637 select INPUT_FF_MEMLESS 636 638 ---help--- 637 - Say Y here if you have a Sony PS2/3 accessory and want to enable force 638 - feedback support for it. 639 + Say Y here if you have a Sony PS2/3/4 accessory and want to enable 640 + force feedback support for it. 639 641 640 642 config HID_SPEEDLINK 641 643 tristate "Speedlink VAD Cezanne mouse support"
+694 -64
drivers/hid/hid-sony.c
··· 17 17 * any later version. 18 18 */ 19 19 20 - /* NOTE: in order for the Sony PS3 BD Remote Control to be found by 20 + /* 21 + * NOTE: in order for the Sony PS3 BD Remote Control to be found by 21 22 * a Bluetooth host, the key combination Start+Enter has to be kept pressed 22 23 * for about 7 seconds with the Bluetooth Host Controller in discovering mode. 23 24 * ··· 31 30 #include <linux/slab.h> 32 31 #include <linux/usb.h> 33 32 #include <linux/leds.h> 33 + #include <linux/power_supply.h> 34 + #include <linux/spinlock.h> 35 + #include <linux/list.h> 36 + #include <linux/input/mt.h> 34 37 35 38 #include "hid-ids.h" 36 39 ··· 46 41 #define DUALSHOCK4_CONTROLLER_USB BIT(5) 47 42 #define DUALSHOCK4_CONTROLLER_BT BIT(6) 48 43 49 - #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB) 44 + #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT) 45 + #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\ 46 + DUALSHOCK4_CONTROLLER_BT) 47 + #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\ 48 + DUALSHOCK4_CONTROLLER) 49 + #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER) 50 + #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER) 50 51 51 52 #define MAX_LEDS 4 52 53 ··· 84 73 0xb1, 0x02, 0xc0, 0xc0, 85 74 }; 86 75 87 - /* The default descriptor doesn't provide mapping for the accelerometers 76 + /* 77 + * The default descriptor doesn't provide mapping for the accelerometers 88 78 * or orientation sensors. This fixed descriptor maps the accelerometers 89 79 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors 90 80 * to usage values 0x43, 0x44 and 0x45. ··· 344 332 0xC0 /* End Collection */ 345 333 }; 346 334 335 + /* 336 + * The default behavior of the Dualshock 4 is to send reports using report 337 + * type 1 when running over Bluetooth. However, as soon as it receives a 338 + * report of type 17 to set the LEDs or rumble it starts returning it's state 339 + * in report 17 instead of 1. Since report 17 is undefined in the default HID 340 + * descriptor the button and axis definitions must be moved to report 17 or 341 + * the HID layer won't process the received input once a report is sent. 342 + */ 343 + static u8 dualshock4_bt_rdesc[] = { 344 + 0x05, 0x01, /* Usage Page (Desktop), */ 345 + 0x09, 0x05, /* Usage (Gamepad), */ 346 + 0xA1, 0x01, /* Collection (Application), */ 347 + 0x85, 0x01, /* Report ID (1), */ 348 + 0x75, 0x08, /* Report Size (8), */ 349 + 0x95, 0x0A, /* Report Count (9), */ 350 + 0x81, 0x02, /* Input (Variable), */ 351 + 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */ 352 + 0x85, 0x02, /* Report ID (2), */ 353 + 0x09, 0x24, /* Usage (24h), */ 354 + 0x95, 0x24, /* Report Count (36), */ 355 + 0xB1, 0x02, /* Feature (Variable), */ 356 + 0x85, 0xA3, /* Report ID (163), */ 357 + 0x09, 0x25, /* Usage (25h), */ 358 + 0x95, 0x30, /* Report Count (48), */ 359 + 0xB1, 0x02, /* Feature (Variable), */ 360 + 0x85, 0x05, /* Report ID (5), */ 361 + 0x09, 0x26, /* Usage (26h), */ 362 + 0x95, 0x28, /* Report Count (40), */ 363 + 0xB1, 0x02, /* Feature (Variable), */ 364 + 0x85, 0x06, /* Report ID (6), */ 365 + 0x09, 0x27, /* Usage (27h), */ 366 + 0x95, 0x34, /* Report Count (52), */ 367 + 0xB1, 0x02, /* Feature (Variable), */ 368 + 0x85, 0x07, /* Report ID (7), */ 369 + 0x09, 0x28, /* Usage (28h), */ 370 + 0x95, 0x30, /* Report Count (48), */ 371 + 0xB1, 0x02, /* Feature (Variable), */ 372 + 0x85, 0x08, /* Report ID (8), */ 373 + 0x09, 0x29, /* Usage (29h), */ 374 + 0x95, 0x2F, /* Report Count (47), */ 375 + 0xB1, 0x02, /* Feature (Variable), */ 376 + 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */ 377 + 0x85, 0x03, /* Report ID (3), */ 378 + 0x09, 0x21, /* Usage (21h), */ 379 + 0x95, 0x26, /* Report Count (38), */ 380 + 0xB1, 0x02, /* Feature (Variable), */ 381 + 0x85, 0x04, /* Report ID (4), */ 382 + 0x09, 0x22, /* Usage (22h), */ 383 + 0x95, 0x2E, /* Report Count (46), */ 384 + 0xB1, 0x02, /* Feature (Variable), */ 385 + 0x85, 0xF0, /* Report ID (240), */ 386 + 0x09, 0x47, /* Usage (47h), */ 387 + 0x95, 0x3F, /* Report Count (63), */ 388 + 0xB1, 0x02, /* Feature (Variable), */ 389 + 0x85, 0xF1, /* Report ID (241), */ 390 + 0x09, 0x48, /* Usage (48h), */ 391 + 0x95, 0x3F, /* Report Count (63), */ 392 + 0xB1, 0x02, /* Feature (Variable), */ 393 + 0x85, 0xF2, /* Report ID (242), */ 394 + 0x09, 0x49, /* Usage (49h), */ 395 + 0x95, 0x0F, /* Report Count (15), */ 396 + 0xB1, 0x02, /* Feature (Variable), */ 397 + 0x85, 0x11, /* Report ID (17), */ 398 + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 399 + 0x09, 0x20, /* Usage (20h), */ 400 + 0x95, 0x02, /* Report Count (2), */ 401 + 0x81, 0x02, /* Input (Variable), */ 402 + 0x05, 0x01, /* Usage Page (Desktop), */ 403 + 0x09, 0x30, /* Usage (X), */ 404 + 0x09, 0x31, /* Usage (Y), */ 405 + 0x09, 0x32, /* Usage (Z), */ 406 + 0x09, 0x35, /* Usage (Rz), */ 407 + 0x15, 0x00, /* Logical Minimum (0), */ 408 + 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 409 + 0x75, 0x08, /* Report Size (8), */ 410 + 0x95, 0x04, /* Report Count (4), */ 411 + 0x81, 0x02, /* Input (Variable), */ 412 + 0x09, 0x39, /* Usage (Hat Switch), */ 413 + 0x15, 0x00, /* Logical Minimum (0), */ 414 + 0x25, 0x07, /* Logical Maximum (7), */ 415 + 0x75, 0x04, /* Report Size (4), */ 416 + 0x95, 0x01, /* Report Count (1), */ 417 + 0x81, 0x42, /* Input (Variable, Null State), */ 418 + 0x05, 0x09, /* Usage Page (Button), */ 419 + 0x19, 0x01, /* Usage Minimum (01h), */ 420 + 0x29, 0x0E, /* Usage Maximum (0Eh), */ 421 + 0x15, 0x00, /* Logical Minimum (0), */ 422 + 0x25, 0x01, /* Logical Maximum (1), */ 423 + 0x75, 0x01, /* Report Size (1), */ 424 + 0x95, 0x0E, /* Report Count (14), */ 425 + 0x81, 0x02, /* Input (Variable), */ 426 + 0x75, 0x06, /* Report Size (6), */ 427 + 0x95, 0x01, /* Report Count (1), */ 428 + 0x81, 0x01, /* Input (Constant), */ 429 + 0x05, 0x01, /* Usage Page (Desktop), */ 430 + 0x09, 0x33, /* Usage (Rx), */ 431 + 0x09, 0x34, /* Usage (Ry), */ 432 + 0x15, 0x00, /* Logical Minimum (0), */ 433 + 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 434 + 0x75, 0x08, /* Report Size (8), */ 435 + 0x95, 0x02, /* Report Count (2), */ 436 + 0x81, 0x02, /* Input (Variable), */ 437 + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 438 + 0x09, 0x20, /* Usage (20h), */ 439 + 0x95, 0x03, /* Report Count (3), */ 440 + 0x81, 0x02, /* Input (Variable), */ 441 + 0x05, 0x01, /* Usage Page (Desktop), */ 442 + 0x19, 0x40, /* Usage Minimum (40h), */ 443 + 0x29, 0x42, /* Usage Maximum (42h), */ 444 + 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */ 445 + 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */ 446 + 0x75, 0x10, /* Report Size (16), */ 447 + 0x95, 0x03, /* Report Count (3), */ 448 + 0x81, 0x02, /* Input (Variable), */ 449 + 0x19, 0x43, /* Usage Minimum (43h), */ 450 + 0x29, 0x45, /* Usage Maximum (45h), */ 451 + 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */ 452 + 0x26, 0x00, 0x40, /* Logical Maximum (16384), */ 453 + 0x95, 0x03, /* Report Count (3), */ 454 + 0x81, 0x02, /* Input (Variable), */ 455 + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 456 + 0x09, 0x20, /* Usage (20h), */ 457 + 0x15, 0x00, /* Logical Minimum (0), */ 458 + 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 459 + 0x75, 0x08, /* Report Size (8), */ 460 + 0x95, 0x31, /* Report Count (51), */ 461 + 0x81, 0x02, /* Input (Variable), */ 462 + 0x09, 0x21, /* Usage (21h), */ 463 + 0x75, 0x08, /* Report Size (8), */ 464 + 0x95, 0x4D, /* Report Count (77), */ 465 + 0x91, 0x02, /* Output (Variable), */ 466 + 0x85, 0x12, /* Report ID (18), */ 467 + 0x09, 0x22, /* Usage (22h), */ 468 + 0x95, 0x8D, /* Report Count (141), */ 469 + 0x81, 0x02, /* Input (Variable), */ 470 + 0x09, 0x23, /* Usage (23h), */ 471 + 0x91, 0x02, /* Output (Variable), */ 472 + 0x85, 0x13, /* Report ID (19), */ 473 + 0x09, 0x24, /* Usage (24h), */ 474 + 0x95, 0xCD, /* Report Count (205), */ 475 + 0x81, 0x02, /* Input (Variable), */ 476 + 0x09, 0x25, /* Usage (25h), */ 477 + 0x91, 0x02, /* Output (Variable), */ 478 + 0x85, 0x14, /* Report ID (20), */ 479 + 0x09, 0x26, /* Usage (26h), */ 480 + 0x96, 0x0D, 0x01, /* Report Count (269), */ 481 + 0x81, 0x02, /* Input (Variable), */ 482 + 0x09, 0x27, /* Usage (27h), */ 483 + 0x91, 0x02, /* Output (Variable), */ 484 + 0x85, 0x15, /* Report ID (21), */ 485 + 0x09, 0x28, /* Usage (28h), */ 486 + 0x96, 0x4D, 0x01, /* Report Count (333), */ 487 + 0x81, 0x02, /* Input (Variable), */ 488 + 0x09, 0x29, /* Usage (29h), */ 489 + 0x91, 0x02, /* Output (Variable), */ 490 + 0x85, 0x16, /* Report ID (22), */ 491 + 0x09, 0x2A, /* Usage (2Ah), */ 492 + 0x96, 0x8D, 0x01, /* Report Count (397), */ 493 + 0x81, 0x02, /* Input (Variable), */ 494 + 0x09, 0x2B, /* Usage (2Bh), */ 495 + 0x91, 0x02, /* Output (Variable), */ 496 + 0x85, 0x17, /* Report ID (23), */ 497 + 0x09, 0x2C, /* Usage (2Ch), */ 498 + 0x96, 0xCD, 0x01, /* Report Count (461), */ 499 + 0x81, 0x02, /* Input (Variable), */ 500 + 0x09, 0x2D, /* Usage (2Dh), */ 501 + 0x91, 0x02, /* Output (Variable), */ 502 + 0x85, 0x18, /* Report ID (24), */ 503 + 0x09, 0x2E, /* Usage (2Eh), */ 504 + 0x96, 0x0D, 0x02, /* Report Count (525), */ 505 + 0x81, 0x02, /* Input (Variable), */ 506 + 0x09, 0x2F, /* Usage (2Fh), */ 507 + 0x91, 0x02, /* Output (Variable), */ 508 + 0x85, 0x19, /* Report ID (25), */ 509 + 0x09, 0x30, /* Usage (30h), */ 510 + 0x96, 0x22, 0x02, /* Report Count (546), */ 511 + 0x81, 0x02, /* Input (Variable), */ 512 + 0x09, 0x31, /* Usage (31h), */ 513 + 0x91, 0x02, /* Output (Variable), */ 514 + 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */ 515 + 0x85, 0x82, /* Report ID (130), */ 516 + 0x09, 0x22, /* Usage (22h), */ 517 + 0x95, 0x3F, /* Report Count (63), */ 518 + 0xB1, 0x02, /* Feature (Variable), */ 519 + 0x85, 0x83, /* Report ID (131), */ 520 + 0x09, 0x23, /* Usage (23h), */ 521 + 0xB1, 0x02, /* Feature (Variable), */ 522 + 0x85, 0x84, /* Report ID (132), */ 523 + 0x09, 0x24, /* Usage (24h), */ 524 + 0xB1, 0x02, /* Feature (Variable), */ 525 + 0x85, 0x90, /* Report ID (144), */ 526 + 0x09, 0x30, /* Usage (30h), */ 527 + 0xB1, 0x02, /* Feature (Variable), */ 528 + 0x85, 0x91, /* Report ID (145), */ 529 + 0x09, 0x31, /* Usage (31h), */ 530 + 0xB1, 0x02, /* Feature (Variable), */ 531 + 0x85, 0x92, /* Report ID (146), */ 532 + 0x09, 0x32, /* Usage (32h), */ 533 + 0xB1, 0x02, /* Feature (Variable), */ 534 + 0x85, 0x93, /* Report ID (147), */ 535 + 0x09, 0x33, /* Usage (33h), */ 536 + 0xB1, 0x02, /* Feature (Variable), */ 537 + 0x85, 0xA0, /* Report ID (160), */ 538 + 0x09, 0x40, /* Usage (40h), */ 539 + 0xB1, 0x02, /* Feature (Variable), */ 540 + 0x85, 0xA4, /* Report ID (164), */ 541 + 0x09, 0x44, /* Usage (44h), */ 542 + 0xB1, 0x02, /* Feature (Variable), */ 543 + 0xC0 /* End Collection */ 544 + }; 545 + 347 546 static __u8 ps3remote_rdesc[] = { 348 547 0x05, 0x01, /* GUsagePage Generic Desktop */ 349 548 0x09, 0x05, /* LUsage 0x05 [Game Pad] */ ··· 672 449 }; 673 450 674 451 static const unsigned int buzz_keymap[] = { 675 - /* The controller has 4 remote buzzers, each with one LED and 5 452 + /* 453 + * The controller has 4 remote buzzers, each with one LED and 5 676 454 * buttons. 677 455 * 678 456 * We use the mapping chosen by the controller, which is: ··· 711 487 [20] = BTN_TRIGGER_HAPPY20, 712 488 }; 713 489 490 + static enum power_supply_property sony_battery_props[] = { 491 + POWER_SUPPLY_PROP_PRESENT, 492 + POWER_SUPPLY_PROP_CAPACITY, 493 + POWER_SUPPLY_PROP_SCOPE, 494 + POWER_SUPPLY_PROP_STATUS, 495 + }; 496 + 497 + static spinlock_t sony_dev_list_lock; 498 + static LIST_HEAD(sony_device_list); 499 + 714 500 struct sony_sc { 501 + spinlock_t lock; 502 + struct list_head list_node; 715 503 struct hid_device *hdev; 716 504 struct led_classdev *leds[MAX_LEDS]; 717 - struct hid_report *output_report; 718 505 unsigned long quirks; 719 506 struct work_struct state_worker; 507 + struct power_supply battery; 720 508 721 509 #ifdef CONFIG_SONY_FF 722 510 __u8 left; 723 511 __u8 right; 724 512 #endif 725 513 514 + __u8 mac_address[6]; 515 + __u8 worker_initialized; 516 + __u8 cable_state; 517 + __u8 battery_charging; 518 + __u8 battery_capacity; 726 519 __u8 led_state[MAX_LEDS]; 727 520 __u8 led_count; 728 521 }; ··· 817 576 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n"); 818 577 rdesc = dualshock4_usb_rdesc; 819 578 *rsize = sizeof(dualshock4_usb_rdesc); 579 + } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) { 580 + hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n"); 581 + rdesc = dualshock4_bt_rdesc; 582 + *rsize = sizeof(dualshock4_bt_rdesc); 820 583 } 821 584 822 585 /* The HID descriptor exposed over BT has a trailing zero byte */ ··· 844 599 return rdesc; 845 600 } 846 601 602 + static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size) 603 + { 604 + static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 }; 605 + unsigned long flags; 606 + __u8 cable_state, battery_capacity, battery_charging; 607 + 608 + /* 609 + * The sixaxis is charging if the battery value is 0xee 610 + * and it is fully charged if the value is 0xef. 611 + * It does not report the actual level while charging so it 612 + * is set to 100% while charging is in progress. 613 + */ 614 + if (rd[30] >= 0xee) { 615 + battery_capacity = 100; 616 + battery_charging = !(rd[30] & 0x01); 617 + } else { 618 + __u8 index = rd[30] <= 5 ? rd[30] : 5; 619 + battery_capacity = sixaxis_battery_capacity[index]; 620 + battery_charging = 0; 621 + } 622 + cable_state = !((rd[31] >> 4) & 0x01); 623 + 624 + spin_lock_irqsave(&sc->lock, flags); 625 + sc->cable_state = cable_state; 626 + sc->battery_capacity = battery_capacity; 627 + sc->battery_charging = battery_charging; 628 + spin_unlock_irqrestore(&sc->lock, flags); 629 + } 630 + 631 + static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size) 632 + { 633 + struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, 634 + struct hid_input, list); 635 + struct input_dev *input_dev = hidinput->input; 636 + unsigned long flags; 637 + int n, offset; 638 + __u8 cable_state, battery_capacity, battery_charging; 639 + 640 + /* 641 + * Battery and touchpad data starts at byte 30 in the USB report and 642 + * 32 in Bluetooth report. 643 + */ 644 + offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32; 645 + 646 + /* 647 + * The lower 4 bits of byte 30 contain the battery level 648 + * and the 5th bit contains the USB cable state. 649 + */ 650 + cable_state = (rd[offset] >> 4) & 0x01; 651 + battery_capacity = rd[offset] & 0x0F; 652 + 653 + /* 654 + * When a USB power source is connected the battery level ranges from 655 + * 0 to 10, and when running on battery power it ranges from 0 to 9. 656 + * A battery level above 10 when plugged in means charge completed. 657 + */ 658 + if (!cable_state || battery_capacity > 10) 659 + battery_charging = 0; 660 + else 661 + battery_charging = 1; 662 + 663 + if (!cable_state) 664 + battery_capacity++; 665 + if (battery_capacity > 10) 666 + battery_capacity = 10; 667 + 668 + battery_capacity *= 10; 669 + 670 + spin_lock_irqsave(&sc->lock, flags); 671 + sc->cable_state = cable_state; 672 + sc->battery_capacity = battery_capacity; 673 + sc->battery_charging = battery_charging; 674 + spin_unlock_irqrestore(&sc->lock, flags); 675 + 676 + offset += 5; 677 + 678 + /* 679 + * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB 680 + * and 37 on Bluetooth. 681 + * The first 7 bits of the first byte is a counter and bit 8 is a touch 682 + * indicator that is 0 when pressed and 1 when not pressed. 683 + * The next 3 bytes are two 12 bit touch coordinates, X and Y. 684 + * The data for the second touch is in the same format and immediatly 685 + * follows the data for the first. 686 + */ 687 + for (n = 0; n < 2; n++) { 688 + __u16 x, y; 689 + 690 + x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8); 691 + y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4); 692 + 693 + input_mt_slot(input_dev, n); 694 + input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 695 + !(rd[offset] >> 7)); 696 + input_report_abs(input_dev, ABS_MT_POSITION_X, x); 697 + input_report_abs(input_dev, ABS_MT_POSITION_Y, y); 698 + 699 + offset += 4; 700 + } 701 + } 702 + 847 703 static int sony_raw_event(struct hid_device *hdev, struct hid_report *report, 848 704 __u8 *rd, int size) 849 705 { 850 706 struct sony_sc *sc = hid_get_drvdata(hdev); 851 707 852 - /* Sixaxis HID report has acclerometers/gyro with MSByte first, this 708 + /* 709 + * Sixaxis HID report has acclerometers/gyro with MSByte first, this 853 710 * has to be BYTE_SWAPPED before passing up to joystick interface 854 711 */ 855 - if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) && 856 - rd[0] == 0x01 && size == 49) { 712 + if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) { 857 713 swap(rd[41], rd[42]); 858 714 swap(rd[43], rd[44]); 859 715 swap(rd[45], rd[46]); 860 716 swap(rd[47], rd[48]); 717 + 718 + sixaxis_parse_report(sc, rd, size); 719 + } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 && 720 + size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) 721 + && rd[0] == 0x11 && size == 78)) { 722 + dualshock4_parse_report(sc, rd, size); 861 723 } 862 724 863 725 return 0; ··· 1076 724 HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 1077 725 } 1078 726 727 + /* 728 + * Requesting feature report 0x02 in Bluetooth mode changes the state of the 729 + * controller so that it sends full input reports of type 0x11. 730 + */ 731 + static int dualshock4_set_operational_bt(struct hid_device *hdev) 732 + { 733 + __u8 buf[37] = { 0 }; 734 + 735 + return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf), 736 + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 737 + } 738 + 1079 739 static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds) 1080 740 { 1081 741 struct list_head *report_list = ··· 1115 751 1116 752 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) { 1117 753 buzz_set_leds(hdev, leds); 1118 - } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) || 1119 - (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) { 754 + } else { 1120 755 for (n = 0; n < count; n++) 1121 756 drv_data->led_state[n] = leds[n]; 1122 757 schedule_work(&drv_data->state_worker); ··· 1155 792 struct sony_sc *drv_data; 1156 793 1157 794 int n; 1158 - int on = 0; 1159 795 1160 796 drv_data = hid_get_drvdata(hdev); 1161 797 if (!drv_data) { ··· 1163 801 } 1164 802 1165 803 for (n = 0; n < drv_data->led_count; n++) { 1166 - if (led == drv_data->leds[n]) { 1167 - on = !!(drv_data->led_state[n]); 1168 - break; 1169 - } 804 + if (led == drv_data->leds[n]) 805 + return drv_data->led_state[n]; 1170 806 } 1171 807 1172 - return on ? LED_FULL : LED_OFF; 808 + return LED_OFF; 1173 809 } 1174 810 1175 811 static void sony_leds_remove(struct hid_device *hdev) ··· 1217 857 /* Validate expected report characteristics. */ 1218 858 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7)) 1219 859 return -ENODEV; 1220 - } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) { 860 + } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER) { 1221 861 drv_data->led_count = 3; 1222 862 max_brightness = 255; 1223 863 use_colors = 1; ··· 1231 871 name_fmt = "%s::sony%d"; 1232 872 } 1233 873 1234 - /* Clear LEDs as we have no way of reading their initial state. This is 874 + /* 875 + * Clear LEDs as we have no way of reading their initial state. This is 1235 876 * only relevant if the driver is loaded after somebody actively set the 1236 - * LEDs to on */ 877 + * LEDs to on 878 + */ 1237 879 sony_set_leds(hdev, initial_values, drv_data->led_count); 1238 880 1239 881 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1; ··· 1305 943 buf[10] |= sc->led_state[2] << 3; 1306 944 buf[10] |= sc->led_state[3] << 4; 1307 945 1308 - hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT); 946 + if (sc->quirks & SIXAXIS_CONTROLLER_USB) 947 + hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT); 948 + else 949 + hid_hw_raw_request(sc->hdev, 0x01, buf, sizeof(buf), 950 + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); 1309 951 } 1310 952 1311 953 static void dualshock4_state_worker(struct work_struct *work) 1312 954 { 1313 955 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker); 1314 956 struct hid_device *hdev = sc->hdev; 1315 - struct hid_report *report = sc->output_report; 1316 - __s32 *value = report->field[0]->value; 957 + int offset; 1317 958 1318 - value[0] = 0x03; 959 + __u8 buf[78] = { 0 }; 960 + 961 + if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { 962 + buf[0] = 0x05; 963 + buf[1] = 0x03; 964 + offset = 4; 965 + } else { 966 + buf[0] = 0x11; 967 + buf[1] = 0xB0; 968 + buf[3] = 0x0F; 969 + offset = 6; 970 + } 1319 971 1320 972 #ifdef CONFIG_SONY_FF 1321 - value[3] = sc->right; 1322 - value[4] = sc->left; 973 + buf[offset++] = sc->right; 974 + buf[offset++] = sc->left; 975 + #else 976 + offset += 2; 1323 977 #endif 1324 978 1325 - value[5] = sc->led_state[0]; 1326 - value[6] = sc->led_state[1]; 1327 - value[7] = sc->led_state[2]; 979 + buf[offset++] = sc->led_state[0]; 980 + buf[offset++] = sc->led_state[1]; 981 + buf[offset++] = sc->led_state[2]; 1328 982 1329 - hid_hw_request(hdev, report, HID_REQ_SET_REPORT); 983 + if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) 984 + hid_hw_output_report(hdev, buf, 32); 985 + else 986 + hid_hw_raw_request(hdev, 0x11, buf, 78, 987 + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); 1330 988 } 1331 989 1332 990 #ifdef CONFIG_SONY_FF ··· 1376 994 return input_ff_create_memless(input_dev, NULL, sony_play_effect); 1377 995 } 1378 996 1379 - static void sony_destroy_ff(struct hid_device *hdev) 1380 - { 1381 - struct sony_sc *sc = hid_get_drvdata(hdev); 1382 - 1383 - cancel_work_sync(&sc->state_worker); 1384 - } 1385 - 1386 997 #else 1387 998 static int sony_init_ff(struct hid_device *hdev) 1388 999 { 1389 1000 return 0; 1390 1001 } 1391 1002 1392 - static void sony_destroy_ff(struct hid_device *hdev) 1393 - { 1394 - } 1395 1003 #endif 1396 1004 1397 - static int sony_set_output_report(struct sony_sc *sc, int req_id, int req_size) 1005 + static int sony_battery_get_property(struct power_supply *psy, 1006 + enum power_supply_property psp, 1007 + union power_supply_propval *val) 1398 1008 { 1399 - struct list_head *head, *list; 1400 - struct hid_report *report; 1009 + struct sony_sc *sc = container_of(psy, struct sony_sc, battery); 1010 + unsigned long flags; 1011 + int ret = 0; 1012 + u8 battery_charging, battery_capacity, cable_state; 1013 + 1014 + spin_lock_irqsave(&sc->lock, flags); 1015 + battery_charging = sc->battery_charging; 1016 + battery_capacity = sc->battery_capacity; 1017 + cable_state = sc->cable_state; 1018 + spin_unlock_irqrestore(&sc->lock, flags); 1019 + 1020 + switch (psp) { 1021 + case POWER_SUPPLY_PROP_PRESENT: 1022 + val->intval = 1; 1023 + break; 1024 + case POWER_SUPPLY_PROP_SCOPE: 1025 + val->intval = POWER_SUPPLY_SCOPE_DEVICE; 1026 + break; 1027 + case POWER_SUPPLY_PROP_CAPACITY: 1028 + val->intval = battery_capacity; 1029 + break; 1030 + case POWER_SUPPLY_PROP_STATUS: 1031 + if (battery_charging) 1032 + val->intval = POWER_SUPPLY_STATUS_CHARGING; 1033 + else 1034 + if (battery_capacity == 100 && cable_state) 1035 + val->intval = POWER_SUPPLY_STATUS_FULL; 1036 + else 1037 + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 1038 + break; 1039 + default: 1040 + ret = -EINVAL; 1041 + break; 1042 + } 1043 + return ret; 1044 + } 1045 + 1046 + static int sony_battery_probe(struct sony_sc *sc) 1047 + { 1048 + static atomic_t power_id_seq = ATOMIC_INIT(0); 1049 + unsigned long power_id; 1401 1050 struct hid_device *hdev = sc->hdev; 1051 + int ret; 1402 1052 1403 - list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list; 1053 + /* 1054 + * Set the default battery level to 100% to avoid low battery warnings 1055 + * if the battery is polled before the first device report is received. 1056 + */ 1057 + sc->battery_capacity = 100; 1404 1058 1405 - list_for_each(head, list) { 1406 - report = list_entry(head, struct hid_report, list); 1059 + power_id = (unsigned long)atomic_inc_return(&power_id_seq); 1407 1060 1408 - if (report->id == req_id) { 1409 - if (report->size < req_size) { 1410 - hid_err(hdev, "Output report 0x%02x (%i bits) is smaller than requested size (%i bits)\n", 1411 - req_id, report->size, req_size); 1412 - return -EINVAL; 1413 - } 1414 - sc->output_report = report; 1415 - return 0; 1061 + sc->battery.properties = sony_battery_props; 1062 + sc->battery.num_properties = ARRAY_SIZE(sony_battery_props); 1063 + sc->battery.get_property = sony_battery_get_property; 1064 + sc->battery.type = POWER_SUPPLY_TYPE_BATTERY; 1065 + sc->battery.use_for_apm = 0; 1066 + sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu", 1067 + power_id); 1068 + if (!sc->battery.name) 1069 + return -ENOMEM; 1070 + 1071 + ret = power_supply_register(&hdev->dev, &sc->battery); 1072 + if (ret) { 1073 + hid_err(hdev, "Unable to register battery device\n"); 1074 + goto err_free; 1075 + } 1076 + 1077 + power_supply_powers(&sc->battery, &hdev->dev); 1078 + return 0; 1079 + 1080 + err_free: 1081 + kfree(sc->battery.name); 1082 + sc->battery.name = NULL; 1083 + return ret; 1084 + } 1085 + 1086 + static void sony_battery_remove(struct sony_sc *sc) 1087 + { 1088 + if (!sc->battery.name) 1089 + return; 1090 + 1091 + power_supply_unregister(&sc->battery); 1092 + kfree(sc->battery.name); 1093 + sc->battery.name = NULL; 1094 + } 1095 + 1096 + static int sony_register_touchpad(struct sony_sc *sc, int touch_count, 1097 + int w, int h) 1098 + { 1099 + struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, 1100 + struct hid_input, list); 1101 + struct input_dev *input_dev = hidinput->input; 1102 + int ret; 1103 + 1104 + ret = input_mt_init_slots(input_dev, touch_count, 0); 1105 + if (ret < 0) { 1106 + hid_err(sc->hdev, "Unable to initialize multi-touch slots\n"); 1107 + return ret; 1108 + } 1109 + 1110 + input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0); 1111 + input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0); 1112 + 1113 + return 0; 1114 + } 1115 + 1116 + /* 1117 + * If a controller is plugged in via USB while already connected via Bluetooth 1118 + * it will show up as two devices. A global list of connected controllers and 1119 + * their MAC addresses is maintained to ensure that a device is only connected 1120 + * once. 1121 + */ 1122 + static int sony_check_add_dev_list(struct sony_sc *sc) 1123 + { 1124 + struct sony_sc *entry; 1125 + unsigned long flags; 1126 + int ret; 1127 + 1128 + spin_lock_irqsave(&sony_dev_list_lock, flags); 1129 + 1130 + list_for_each_entry(entry, &sony_device_list, list_node) { 1131 + ret = memcmp(sc->mac_address, entry->mac_address, 1132 + sizeof(sc->mac_address)); 1133 + if (!ret) { 1134 + ret = -EEXIST; 1135 + hid_info(sc->hdev, "controller with MAC address %pMR already connected\n", 1136 + sc->mac_address); 1137 + goto unlock; 1416 1138 } 1417 1139 } 1418 1140 1419 - hid_err(hdev, "Unable to locate output report 0x%02x\n", req_id); 1141 + ret = 0; 1142 + list_add(&(sc->list_node), &sony_device_list); 1420 1143 1421 - return -EINVAL; 1144 + unlock: 1145 + spin_unlock_irqrestore(&sony_dev_list_lock, flags); 1146 + return ret; 1422 1147 } 1148 + 1149 + static void sony_remove_dev_list(struct sony_sc *sc) 1150 + { 1151 + unsigned long flags; 1152 + 1153 + if (sc->list_node.next) { 1154 + spin_lock_irqsave(&sony_dev_list_lock, flags); 1155 + list_del(&(sc->list_node)); 1156 + spin_unlock_irqrestore(&sony_dev_list_lock, flags); 1157 + } 1158 + } 1159 + 1160 + static int sony_get_bt_devaddr(struct sony_sc *sc) 1161 + { 1162 + int ret; 1163 + 1164 + /* HIDP stores the device MAC address as a string in the uniq field. */ 1165 + ret = strlen(sc->hdev->uniq); 1166 + if (ret != 17) 1167 + return -EINVAL; 1168 + 1169 + ret = sscanf(sc->hdev->uniq, 1170 + "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 1171 + &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3], 1172 + &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]); 1173 + 1174 + if (ret != 6) 1175 + return -EINVAL; 1176 + 1177 + return 0; 1178 + } 1179 + 1180 + static int sony_check_add(struct sony_sc *sc) 1181 + { 1182 + int n, ret; 1183 + 1184 + if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) || 1185 + (sc->quirks & SIXAXIS_CONTROLLER_BT)) { 1186 + /* 1187 + * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC 1188 + * address from the uniq string where HIDP stores it. 1189 + * As uniq cannot be guaranteed to be a MAC address in all cases 1190 + * a failure of this function should not prevent the connection. 1191 + */ 1192 + if (sony_get_bt_devaddr(sc) < 0) { 1193 + hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n"); 1194 + return 0; 1195 + } 1196 + } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { 1197 + __u8 buf[7]; 1198 + 1199 + /* 1200 + * The MAC address of a DS4 controller connected via USB can be 1201 + * retrieved with feature report 0x81. The address begins at 1202 + * offset 1. 1203 + */ 1204 + ret = hid_hw_raw_request(sc->hdev, 0x81, buf, sizeof(buf), 1205 + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 1206 + 1207 + if (ret != 7) { 1208 + hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n"); 1209 + return ret < 0 ? ret : -EINVAL; 1210 + } 1211 + 1212 + memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address)); 1213 + } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) { 1214 + __u8 buf[18]; 1215 + 1216 + /* 1217 + * The MAC address of a Sixaxis controller connected via USB can 1218 + * be retrieved with feature report 0xf2. The address begins at 1219 + * offset 4. 1220 + */ 1221 + ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, sizeof(buf), 1222 + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 1223 + 1224 + if (ret != 18) { 1225 + hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n"); 1226 + return ret < 0 ? ret : -EINVAL; 1227 + } 1228 + 1229 + /* 1230 + * The Sixaxis device MAC in the report is big-endian and must 1231 + * be byte-swapped. 1232 + */ 1233 + for (n = 0; n < 6; n++) 1234 + sc->mac_address[5-n] = buf[4+n]; 1235 + } else { 1236 + return 0; 1237 + } 1238 + 1239 + return sony_check_add_dev_list(sc); 1240 + } 1241 + 1423 1242 1424 1243 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) 1425 1244 { ··· 1661 1078 if (sc->quirks & SIXAXIS_CONTROLLER_USB) { 1662 1079 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report; 1663 1080 ret = sixaxis_set_operational_usb(hdev); 1081 + sc->worker_initialized = 1; 1664 1082 INIT_WORK(&sc->state_worker, sixaxis_state_worker); 1665 - } 1666 - else if (sc->quirks & SIXAXIS_CONTROLLER_BT) 1083 + } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) { 1667 1084 ret = sixaxis_set_operational_bt(hdev); 1668 - else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { 1669 - /* Report 5 (31 bytes) is used to send data to the controller via USB */ 1670 - ret = sony_set_output_report(sc, 0x05, 248); 1085 + sc->worker_initialized = 1; 1086 + INIT_WORK(&sc->state_worker, sixaxis_state_worker); 1087 + } else if (sc->quirks & DUALSHOCK4_CONTROLLER) { 1088 + if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) { 1089 + ret = dualshock4_set_operational_bt(hdev); 1090 + if (ret < 0) { 1091 + hid_err(hdev, "failed to set the Dualshock 4 operational mode\n"); 1092 + goto err_stop; 1093 + } 1094 + } 1095 + /* 1096 + * The Dualshock 4 touchpad supports 2 touches and has a 1097 + * resolution of 1920x940. 1098 + */ 1099 + ret = sony_register_touchpad(sc, 2, 1920, 940); 1671 1100 if (ret < 0) 1672 1101 goto err_stop; 1673 1102 1103 + sc->worker_initialized = 1; 1674 1104 INIT_WORK(&sc->state_worker, dualshock4_state_worker); 1675 1105 } else { 1676 1106 ret = 0; 1677 1107 } 1678 1108 1109 + if (ret < 0) 1110 + goto err_stop; 1111 + 1112 + ret = sony_check_add(sc); 1679 1113 if (ret < 0) 1680 1114 goto err_stop; 1681 1115 ··· 1702 1102 goto err_stop; 1703 1103 } 1704 1104 1705 - ret = sony_init_ff(hdev); 1706 - if (ret < 0) 1707 - goto err_stop; 1105 + if (sc->quirks & SONY_BATTERY_SUPPORT) { 1106 + ret = sony_battery_probe(sc); 1107 + if (ret < 0) 1108 + goto err_stop; 1109 + 1110 + /* Open the device to receive reports with battery info */ 1111 + ret = hid_hw_open(hdev); 1112 + if (ret < 0) { 1113 + hid_err(hdev, "hw open failed\n"); 1114 + goto err_stop; 1115 + } 1116 + } 1117 + 1118 + if (sc->quirks & SONY_FF_SUPPORT) { 1119 + ret = sony_init_ff(hdev); 1120 + if (ret < 0) 1121 + goto err_close; 1122 + } 1708 1123 1709 1124 return 0; 1125 + err_close: 1126 + hid_hw_close(hdev); 1710 1127 err_stop: 1711 1128 if (sc->quirks & SONY_LED_SUPPORT) 1712 1129 sony_leds_remove(hdev); 1130 + if (sc->quirks & SONY_BATTERY_SUPPORT) 1131 + sony_battery_remove(sc); 1132 + if (sc->worker_initialized) 1133 + cancel_work_sync(&sc->state_worker); 1134 + sony_remove_dev_list(sc); 1713 1135 hid_hw_stop(hdev); 1714 1136 return ret; 1715 1137 } ··· 1743 1121 if (sc->quirks & SONY_LED_SUPPORT) 1744 1122 sony_leds_remove(hdev); 1745 1123 1746 - sony_destroy_ff(hdev); 1124 + if (sc->quirks & SONY_BATTERY_SUPPORT) { 1125 + hid_hw_close(hdev); 1126 + sony_battery_remove(sc); 1127 + } 1128 + 1129 + if (sc->worker_initialized) 1130 + cancel_work_sync(&sc->state_worker); 1131 + 1132 + sony_remove_dev_list(sc); 1747 1133 1748 1134 hid_hw_stop(hdev); 1749 1135 }
+3
net/bluetooth/hidp/core.c
··· 767 767 snprintf(hid->phys, sizeof(hid->phys), "%pMR", 768 768 &l2cap_pi(session->ctrl_sock->sk)->chan->src); 769 769 770 + /* NOTE: Some device modules depend on the dst address being stored in 771 + * uniq. Please be aware of this before making changes to this behavior. 772 + */ 770 773 snprintf(hid->uniq, sizeof(hid->uniq), "%pMR", 771 774 &l2cap_pi(session->ctrl_sock->sk)->chan->dst); 772 775