diff options
author | Jiri Kosina <jkosina@suse.cz> | 2014-03-14 10:30:16 -0400 |
---|---|---|
committer | Jiri Kosina <jkosina@suse.cz> | 2014-03-14 10:30:16 -0400 |
commit | 65ab2fc479fa8554e26d6d1726a4ebe6f29a73be (patch) | |
tree | a90294685f0e2a445397a6d9e7e85ac7949ebf1d /drivers/hid/hid-sony.c | |
parent | 866e4797b4e8100f5abcf2bfd3f7843dc40306cd (diff) | |
parent | dccf2f65e68d2f5b5e86ae6cf9796cf558694953 (diff) |
Merge branch 'for-3.15/sony' into for-3.15/hid-core-ll-transport-cleanup
Diffstat (limited to 'drivers/hid/hid-sony.c')
-rw-r--r-- | drivers/hid/hid-sony.c | 754 |
1 files changed, 692 insertions, 62 deletions
diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c index e3e89b6a41c2..b5fe65e70dc4 100644 --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c | |||
@@ -17,7 +17,8 @@ | |||
17 | * any later version. | 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 | * a Bluetooth host, the key combination Start+Enter has to be kept pressed | 22 | * a Bluetooth host, the key combination Start+Enter has to be kept pressed |
22 | * for about 7 seconds with the Bluetooth Host Controller in discovering mode. | 23 | * for about 7 seconds with the Bluetooth Host Controller in discovering mode. |
23 | * | 24 | * |
@@ -30,6 +31,10 @@ | |||
30 | #include <linux/slab.h> | 31 | #include <linux/slab.h> |
31 | #include <linux/usb.h> | 32 | #include <linux/usb.h> |
32 | #include <linux/leds.h> | 33 | #include <linux/leds.h> |
34 | #include <linux/power_supply.h> | ||
35 | #include <linux/spinlock.h> | ||
36 | #include <linux/list.h> | ||
37 | #include <linux/input/mt.h> | ||
33 | 38 | ||
34 | #include "hid-ids.h" | 39 | #include "hid-ids.h" |
35 | 40 | ||
@@ -41,7 +46,13 @@ | |||
41 | #define DUALSHOCK4_CONTROLLER_USB BIT(5) | 46 | #define DUALSHOCK4_CONTROLLER_USB BIT(5) |
42 | #define DUALSHOCK4_CONTROLLER_BT BIT(6) | 47 | #define DUALSHOCK4_CONTROLLER_BT BIT(6) |
43 | 48 | ||
44 | #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB) | 49 | #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT) |
50 | #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\ | ||
51 | DUALSHOCK4_CONTROLLER_BT) | ||
52 | #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\ | ||
53 | DUALSHOCK4_CONTROLLER) | ||
54 | #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER) | ||
55 | #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER) | ||
45 | 56 | ||
46 | #define MAX_LEDS 4 | 57 | #define MAX_LEDS 4 |
47 | 58 | ||
@@ -73,7 +84,8 @@ static const u8 sixaxis_rdesc_fixup2[] = { | |||
73 | 0xb1, 0x02, 0xc0, 0xc0, | 84 | 0xb1, 0x02, 0xc0, 0xc0, |
74 | }; | 85 | }; |
75 | 86 | ||
76 | /* The default descriptor doesn't provide mapping for the accelerometers | 87 | /* |
88 | * The default descriptor doesn't provide mapping for the accelerometers | ||
77 | * or orientation sensors. This fixed descriptor maps the accelerometers | 89 | * or orientation sensors. This fixed descriptor maps the accelerometers |
78 | * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors | 90 | * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors |
79 | * to usage values 0x43, 0x44 and 0x45. | 91 | * to usage values 0x43, 0x44 and 0x45. |
@@ -332,6 +344,217 @@ static u8 dualshock4_usb_rdesc[] = { | |||
332 | 0xC0 /* End Collection */ | 344 | 0xC0 /* End Collection */ |
333 | }; | 345 | }; |
334 | 346 | ||
347 | /* | ||
348 | * The default behavior of the Dualshock 4 is to send reports using report | ||
349 | * type 1 when running over Bluetooth. However, as soon as it receives a | ||
350 | * report of type 17 to set the LEDs or rumble it starts returning it's state | ||
351 | * in report 17 instead of 1. Since report 17 is undefined in the default HID | ||
352 | * descriptor the button and axis definitions must be moved to report 17 or | ||
353 | * the HID layer won't process the received input once a report is sent. | ||
354 | */ | ||
355 | static u8 dualshock4_bt_rdesc[] = { | ||
356 | 0x05, 0x01, /* Usage Page (Desktop), */ | ||
357 | 0x09, 0x05, /* Usage (Gamepad), */ | ||
358 | 0xA1, 0x01, /* Collection (Application), */ | ||
359 | 0x85, 0x01, /* Report ID (1), */ | ||
360 | 0x75, 0x08, /* Report Size (8), */ | ||
361 | 0x95, 0x0A, /* Report Count (9), */ | ||
362 | 0x81, 0x02, /* Input (Variable), */ | ||
363 | 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */ | ||
364 | 0x85, 0x02, /* Report ID (2), */ | ||
365 | 0x09, 0x24, /* Usage (24h), */ | ||
366 | 0x95, 0x24, /* Report Count (36), */ | ||
367 | 0xB1, 0x02, /* Feature (Variable), */ | ||
368 | 0x85, 0xA3, /* Report ID (163), */ | ||
369 | 0x09, 0x25, /* Usage (25h), */ | ||
370 | 0x95, 0x30, /* Report Count (48), */ | ||
371 | 0xB1, 0x02, /* Feature (Variable), */ | ||
372 | 0x85, 0x05, /* Report ID (5), */ | ||
373 | 0x09, 0x26, /* Usage (26h), */ | ||
374 | 0x95, 0x28, /* Report Count (40), */ | ||
375 | 0xB1, 0x02, /* Feature (Variable), */ | ||
376 | 0x85, 0x06, /* Report ID (6), */ | ||
377 | 0x09, 0x27, /* Usage (27h), */ | ||
378 | 0x95, 0x34, /* Report Count (52), */ | ||
379 | 0xB1, 0x02, /* Feature (Variable), */ | ||
380 | 0x85, 0x07, /* Report ID (7), */ | ||
381 | 0x09, 0x28, /* Usage (28h), */ | ||
382 | 0x95, 0x30, /* Report Count (48), */ | ||
383 | 0xB1, 0x02, /* Feature (Variable), */ | ||
384 | 0x85, 0x08, /* Report ID (8), */ | ||
385 | 0x09, 0x29, /* Usage (29h), */ | ||
386 | 0x95, 0x2F, /* Report Count (47), */ | ||
387 | 0xB1, 0x02, /* Feature (Variable), */ | ||
388 | 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */ | ||
389 | 0x85, 0x03, /* Report ID (3), */ | ||
390 | 0x09, 0x21, /* Usage (21h), */ | ||
391 | 0x95, 0x26, /* Report Count (38), */ | ||
392 | 0xB1, 0x02, /* Feature (Variable), */ | ||
393 | 0x85, 0x04, /* Report ID (4), */ | ||
394 | 0x09, 0x22, /* Usage (22h), */ | ||
395 | 0x95, 0x2E, /* Report Count (46), */ | ||
396 | 0xB1, 0x02, /* Feature (Variable), */ | ||
397 | 0x85, 0xF0, /* Report ID (240), */ | ||
398 | 0x09, 0x47, /* Usage (47h), */ | ||
399 | 0x95, 0x3F, /* Report Count (63), */ | ||
400 | 0xB1, 0x02, /* Feature (Variable), */ | ||
401 | 0x85, 0xF1, /* Report ID (241), */ | ||
402 | 0x09, 0x48, /* Usage (48h), */ | ||
403 | 0x95, 0x3F, /* Report Count (63), */ | ||
404 | 0xB1, 0x02, /* Feature (Variable), */ | ||
405 | 0x85, 0xF2, /* Report ID (242), */ | ||
406 | 0x09, 0x49, /* Usage (49h), */ | ||
407 | 0x95, 0x0F, /* Report Count (15), */ | ||
408 | 0xB1, 0x02, /* Feature (Variable), */ | ||
409 | 0x85, 0x11, /* Report ID (17), */ | ||
410 | 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ | ||
411 | 0x09, 0x20, /* Usage (20h), */ | ||
412 | 0x95, 0x02, /* Report Count (2), */ | ||
413 | 0x81, 0x02, /* Input (Variable), */ | ||
414 | 0x05, 0x01, /* Usage Page (Desktop), */ | ||
415 | 0x09, 0x30, /* Usage (X), */ | ||
416 | 0x09, 0x31, /* Usage (Y), */ | ||
417 | 0x09, 0x32, /* Usage (Z), */ | ||
418 | 0x09, 0x35, /* Usage (Rz), */ | ||
419 | 0x15, 0x00, /* Logical Minimum (0), */ | ||
420 | 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ | ||
421 | 0x75, 0x08, /* Report Size (8), */ | ||
422 | 0x95, 0x04, /* Report Count (4), */ | ||
423 | 0x81, 0x02, /* Input (Variable), */ | ||
424 | 0x09, 0x39, /* Usage (Hat Switch), */ | ||
425 | 0x15, 0x00, /* Logical Minimum (0), */ | ||
426 | 0x25, 0x07, /* Logical Maximum (7), */ | ||
427 | 0x75, 0x04, /* Report Size (4), */ | ||
428 | 0x95, 0x01, /* Report Count (1), */ | ||
429 | 0x81, 0x42, /* Input (Variable, Null State), */ | ||
430 | 0x05, 0x09, /* Usage Page (Button), */ | ||
431 | 0x19, 0x01, /* Usage Minimum (01h), */ | ||
432 | 0x29, 0x0E, /* Usage Maximum (0Eh), */ | ||
433 | 0x15, 0x00, /* Logical Minimum (0), */ | ||
434 | 0x25, 0x01, /* Logical Maximum (1), */ | ||
435 | 0x75, 0x01, /* Report Size (1), */ | ||
436 | 0x95, 0x0E, /* Report Count (14), */ | ||
437 | 0x81, 0x02, /* Input (Variable), */ | ||
438 | 0x75, 0x06, /* Report Size (6), */ | ||
439 | 0x95, 0x01, /* Report Count (1), */ | ||
440 | 0x81, 0x01, /* Input (Constant), */ | ||
441 | 0x05, 0x01, /* Usage Page (Desktop), */ | ||
442 | 0x09, 0x33, /* Usage (Rx), */ | ||
443 | 0x09, 0x34, /* Usage (Ry), */ | ||
444 | 0x15, 0x00, /* Logical Minimum (0), */ | ||
445 | 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ | ||
446 | 0x75, 0x08, /* Report Size (8), */ | ||
447 | 0x95, 0x02, /* Report Count (2), */ | ||
448 | 0x81, 0x02, /* Input (Variable), */ | ||
449 | 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ | ||
450 | 0x09, 0x20, /* Usage (20h), */ | ||
451 | 0x95, 0x03, /* Report Count (3), */ | ||
452 | 0x81, 0x02, /* Input (Variable), */ | ||
453 | 0x05, 0x01, /* Usage Page (Desktop), */ | ||
454 | 0x19, 0x40, /* Usage Minimum (40h), */ | ||
455 | 0x29, 0x42, /* Usage Maximum (42h), */ | ||
456 | 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */ | ||
457 | 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */ | ||
458 | 0x75, 0x10, /* Report Size (16), */ | ||
459 | 0x95, 0x03, /* Report Count (3), */ | ||
460 | 0x81, 0x02, /* Input (Variable), */ | ||
461 | 0x19, 0x43, /* Usage Minimum (43h), */ | ||
462 | 0x29, 0x45, /* Usage Maximum (45h), */ | ||
463 | 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */ | ||
464 | 0x26, 0x00, 0x40, /* Logical Maximum (16384), */ | ||
465 | 0x95, 0x03, /* Report Count (3), */ | ||
466 | 0x81, 0x02, /* Input (Variable), */ | ||
467 | 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ | ||
468 | 0x09, 0x20, /* Usage (20h), */ | ||
469 | 0x15, 0x00, /* Logical Minimum (0), */ | ||
470 | 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ | ||
471 | 0x75, 0x08, /* Report Size (8), */ | ||
472 | 0x95, 0x31, /* Report Count (51), */ | ||
473 | 0x81, 0x02, /* Input (Variable), */ | ||
474 | 0x09, 0x21, /* Usage (21h), */ | ||
475 | 0x75, 0x08, /* Report Size (8), */ | ||
476 | 0x95, 0x4D, /* Report Count (77), */ | ||
477 | 0x91, 0x02, /* Output (Variable), */ | ||
478 | 0x85, 0x12, /* Report ID (18), */ | ||
479 | 0x09, 0x22, /* Usage (22h), */ | ||
480 | 0x95, 0x8D, /* Report Count (141), */ | ||
481 | 0x81, 0x02, /* Input (Variable), */ | ||
482 | 0x09, 0x23, /* Usage (23h), */ | ||
483 | 0x91, 0x02, /* Output (Variable), */ | ||
484 | 0x85, 0x13, /* Report ID (19), */ | ||
485 | 0x09, 0x24, /* Usage (24h), */ | ||
486 | 0x95, 0xCD, /* Report Count (205), */ | ||
487 | 0x81, 0x02, /* Input (Variable), */ | ||
488 | 0x09, 0x25, /* Usage (25h), */ | ||
489 | 0x91, 0x02, /* Output (Variable), */ | ||
490 | 0x85, 0x14, /* Report ID (20), */ | ||
491 | 0x09, 0x26, /* Usage (26h), */ | ||
492 | 0x96, 0x0D, 0x01, /* Report Count (269), */ | ||
493 | 0x81, 0x02, /* Input (Variable), */ | ||
494 | 0x09, 0x27, /* Usage (27h), */ | ||
495 | 0x91, 0x02, /* Output (Variable), */ | ||
496 | 0x85, 0x15, /* Report ID (21), */ | ||
497 | 0x09, 0x28, /* Usage (28h), */ | ||
498 | 0x96, 0x4D, 0x01, /* Report Count (333), */ | ||
499 | 0x81, 0x02, /* Input (Variable), */ | ||
500 | 0x09, 0x29, /* Usage (29h), */ | ||
501 | 0x91, 0x02, /* Output (Variable), */ | ||
502 | 0x85, 0x16, /* Report ID (22), */ | ||
503 | 0x09, 0x2A, /* Usage (2Ah), */ | ||
504 | 0x96, 0x8D, 0x01, /* Report Count (397), */ | ||
505 | 0x81, 0x02, /* Input (Variable), */ | ||
506 | 0x09, 0x2B, /* Usage (2Bh), */ | ||
507 | 0x91, 0x02, /* Output (Variable), */ | ||
508 | 0x85, 0x17, /* Report ID (23), */ | ||
509 | 0x09, 0x2C, /* Usage (2Ch), */ | ||
510 | 0x96, 0xCD, 0x01, /* Report Count (461), */ | ||
511 | 0x81, 0x02, /* Input (Variable), */ | ||
512 | 0x09, 0x2D, /* Usage (2Dh), */ | ||
513 | 0x91, 0x02, /* Output (Variable), */ | ||
514 | 0x85, 0x18, /* Report ID (24), */ | ||
515 | 0x09, 0x2E, /* Usage (2Eh), */ | ||
516 | 0x96, 0x0D, 0x02, /* Report Count (525), */ | ||
517 | 0x81, 0x02, /* Input (Variable), */ | ||
518 | 0x09, 0x2F, /* Usage (2Fh), */ | ||
519 | 0x91, 0x02, /* Output (Variable), */ | ||
520 | 0x85, 0x19, /* Report ID (25), */ | ||
521 | 0x09, 0x30, /* Usage (30h), */ | ||
522 | 0x96, 0x22, 0x02, /* Report Count (546), */ | ||
523 | 0x81, 0x02, /* Input (Variable), */ | ||
524 | 0x09, 0x31, /* Usage (31h), */ | ||
525 | 0x91, 0x02, /* Output (Variable), */ | ||
526 | 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */ | ||
527 | 0x85, 0x82, /* Report ID (130), */ | ||
528 | 0x09, 0x22, /* Usage (22h), */ | ||
529 | 0x95, 0x3F, /* Report Count (63), */ | ||
530 | 0xB1, 0x02, /* Feature (Variable), */ | ||
531 | 0x85, 0x83, /* Report ID (131), */ | ||
532 | 0x09, 0x23, /* Usage (23h), */ | ||
533 | 0xB1, 0x02, /* Feature (Variable), */ | ||
534 | 0x85, 0x84, /* Report ID (132), */ | ||
535 | 0x09, 0x24, /* Usage (24h), */ | ||
536 | 0xB1, 0x02, /* Feature (Variable), */ | ||
537 | 0x85, 0x90, /* Report ID (144), */ | ||
538 | 0x09, 0x30, /* Usage (30h), */ | ||
539 | 0xB1, 0x02, /* Feature (Variable), */ | ||
540 | 0x85, 0x91, /* Report ID (145), */ | ||
541 | 0x09, 0x31, /* Usage (31h), */ | ||
542 | 0xB1, 0x02, /* Feature (Variable), */ | ||
543 | 0x85, 0x92, /* Report ID (146), */ | ||
544 | 0x09, 0x32, /* Usage (32h), */ | ||
545 | 0xB1, 0x02, /* Feature (Variable), */ | ||
546 | 0x85, 0x93, /* Report ID (147), */ | ||
547 | 0x09, 0x33, /* Usage (33h), */ | ||
548 | 0xB1, 0x02, /* Feature (Variable), */ | ||
549 | 0x85, 0xA0, /* Report ID (160), */ | ||
550 | 0x09, 0x40, /* Usage (40h), */ | ||
551 | 0xB1, 0x02, /* Feature (Variable), */ | ||
552 | 0x85, 0xA4, /* Report ID (164), */ | ||
553 | 0x09, 0x44, /* Usage (44h), */ | ||
554 | 0xB1, 0x02, /* Feature (Variable), */ | ||
555 | 0xC0 /* End Collection */ | ||
556 | }; | ||
557 | |||
335 | static __u8 ps3remote_rdesc[] = { | 558 | static __u8 ps3remote_rdesc[] = { |
336 | 0x05, 0x01, /* GUsagePage Generic Desktop */ | 559 | 0x05, 0x01, /* GUsagePage Generic Desktop */ |
337 | 0x09, 0x05, /* LUsage 0x05 [Game Pad] */ | 560 | 0x09, 0x05, /* LUsage 0x05 [Game Pad] */ |
@@ -449,7 +672,8 @@ static const unsigned int ps3remote_keymap_remote_buttons[] = { | |||
449 | }; | 672 | }; |
450 | 673 | ||
451 | static const unsigned int buzz_keymap[] = { | 674 | static const unsigned int buzz_keymap[] = { |
452 | /* The controller has 4 remote buzzers, each with one LED and 5 | 675 | /* |
676 | * The controller has 4 remote buzzers, each with one LED and 5 | ||
453 | * buttons. | 677 | * buttons. |
454 | * | 678 | * |
455 | * We use the mapping chosen by the controller, which is: | 679 | * We use the mapping chosen by the controller, which is: |
@@ -487,18 +711,35 @@ static const unsigned int buzz_keymap[] = { | |||
487 | [20] = BTN_TRIGGER_HAPPY20, | 711 | [20] = BTN_TRIGGER_HAPPY20, |
488 | }; | 712 | }; |
489 | 713 | ||
714 | static enum power_supply_property sony_battery_props[] = { | ||
715 | POWER_SUPPLY_PROP_PRESENT, | ||
716 | POWER_SUPPLY_PROP_CAPACITY, | ||
717 | POWER_SUPPLY_PROP_SCOPE, | ||
718 | POWER_SUPPLY_PROP_STATUS, | ||
719 | }; | ||
720 | |||
721 | static spinlock_t sony_dev_list_lock; | ||
722 | static LIST_HEAD(sony_device_list); | ||
723 | |||
490 | struct sony_sc { | 724 | struct sony_sc { |
725 | spinlock_t lock; | ||
726 | struct list_head list_node; | ||
491 | struct hid_device *hdev; | 727 | struct hid_device *hdev; |
492 | struct led_classdev *leds[MAX_LEDS]; | 728 | struct led_classdev *leds[MAX_LEDS]; |
493 | struct hid_report *output_report; | ||
494 | unsigned long quirks; | 729 | unsigned long quirks; |
495 | struct work_struct state_worker; | 730 | struct work_struct state_worker; |
731 | struct power_supply battery; | ||
496 | 732 | ||
497 | #ifdef CONFIG_SONY_FF | 733 | #ifdef CONFIG_SONY_FF |
498 | __u8 left; | 734 | __u8 left; |
499 | __u8 right; | 735 | __u8 right; |
500 | #endif | 736 | #endif |
501 | 737 | ||
738 | __u8 mac_address[6]; | ||
739 | __u8 worker_initialized; | ||
740 | __u8 cable_state; | ||
741 | __u8 battery_charging; | ||
742 | __u8 battery_capacity; | ||
502 | __u8 led_state[MAX_LEDS]; | 743 | __u8 led_state[MAX_LEDS]; |
503 | __u8 led_count; | 744 | __u8 led_count; |
504 | }; | 745 | }; |
@@ -576,6 +817,10 @@ static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc, | |||
576 | hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n"); | 817 | hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n"); |
577 | rdesc = dualshock4_usb_rdesc; | 818 | rdesc = dualshock4_usb_rdesc; |
578 | *rsize = sizeof(dualshock4_usb_rdesc); | 819 | *rsize = sizeof(dualshock4_usb_rdesc); |
820 | } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) { | ||
821 | hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n"); | ||
822 | rdesc = dualshock4_bt_rdesc; | ||
823 | *rsize = sizeof(dualshock4_bt_rdesc); | ||
579 | } | 824 | } |
580 | 825 | ||
581 | /* The HID descriptor exposed over BT has a trailing zero byte */ | 826 | /* The HID descriptor exposed over BT has a trailing zero byte */ |
@@ -599,20 +844,127 @@ static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc, | |||
599 | return rdesc; | 844 | return rdesc; |
600 | } | 845 | } |
601 | 846 | ||
847 | static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size) | ||
848 | { | ||
849 | static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 }; | ||
850 | unsigned long flags; | ||
851 | __u8 cable_state, battery_capacity, battery_charging; | ||
852 | |||
853 | /* | ||
854 | * The sixaxis is charging if the battery value is 0xee | ||
855 | * and it is fully charged if the value is 0xef. | ||
856 | * It does not report the actual level while charging so it | ||
857 | * is set to 100% while charging is in progress. | ||
858 | */ | ||
859 | if (rd[30] >= 0xee) { | ||
860 | battery_capacity = 100; | ||
861 | battery_charging = !(rd[30] & 0x01); | ||
862 | } else { | ||
863 | __u8 index = rd[30] <= 5 ? rd[30] : 5; | ||
864 | battery_capacity = sixaxis_battery_capacity[index]; | ||
865 | battery_charging = 0; | ||
866 | } | ||
867 | cable_state = !((rd[31] >> 4) & 0x01); | ||
868 | |||
869 | spin_lock_irqsave(&sc->lock, flags); | ||
870 | sc->cable_state = cable_state; | ||
871 | sc->battery_capacity = battery_capacity; | ||
872 | sc->battery_charging = battery_charging; | ||
873 | spin_unlock_irqrestore(&sc->lock, flags); | ||
874 | } | ||
875 | |||
876 | static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size) | ||
877 | { | ||
878 | struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, | ||
879 | struct hid_input, list); | ||
880 | struct input_dev *input_dev = hidinput->input; | ||
881 | unsigned long flags; | ||
882 | int n, offset; | ||
883 | __u8 cable_state, battery_capacity, battery_charging; | ||
884 | |||
885 | /* | ||
886 | * Battery and touchpad data starts at byte 30 in the USB report and | ||
887 | * 32 in Bluetooth report. | ||
888 | */ | ||
889 | offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32; | ||
890 | |||
891 | /* | ||
892 | * The lower 4 bits of byte 30 contain the battery level | ||
893 | * and the 5th bit contains the USB cable state. | ||
894 | */ | ||
895 | cable_state = (rd[offset] >> 4) & 0x01; | ||
896 | battery_capacity = rd[offset] & 0x0F; | ||
897 | |||
898 | /* | ||
899 | * When a USB power source is connected the battery level ranges from | ||
900 | * 0 to 10, and when running on battery power it ranges from 0 to 9. | ||
901 | * A battery level above 10 when plugged in means charge completed. | ||
902 | */ | ||
903 | if (!cable_state || battery_capacity > 10) | ||
904 | battery_charging = 0; | ||
905 | else | ||
906 | battery_charging = 1; | ||
907 | |||
908 | if (!cable_state) | ||
909 | battery_capacity++; | ||
910 | if (battery_capacity > 10) | ||
911 | battery_capacity = 10; | ||
912 | |||
913 | battery_capacity *= 10; | ||
914 | |||
915 | spin_lock_irqsave(&sc->lock, flags); | ||
916 | sc->cable_state = cable_state; | ||
917 | sc->battery_capacity = battery_capacity; | ||
918 | sc->battery_charging = battery_charging; | ||
919 | spin_unlock_irqrestore(&sc->lock, flags); | ||
920 | |||
921 | offset += 5; | ||
922 | |||
923 | /* | ||
924 | * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB | ||
925 | * and 37 on Bluetooth. | ||
926 | * The first 7 bits of the first byte is a counter and bit 8 is a touch | ||
927 | * indicator that is 0 when pressed and 1 when not pressed. | ||
928 | * The next 3 bytes are two 12 bit touch coordinates, X and Y. | ||
929 | * The data for the second touch is in the same format and immediatly | ||
930 | * follows the data for the first. | ||
931 | */ | ||
932 | for (n = 0; n < 2; n++) { | ||
933 | __u16 x, y; | ||
934 | |||
935 | x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8); | ||
936 | y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4); | ||
937 | |||
938 | input_mt_slot(input_dev, n); | ||
939 | input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, | ||
940 | !(rd[offset] >> 7)); | ||
941 | input_report_abs(input_dev, ABS_MT_POSITION_X, x); | ||
942 | input_report_abs(input_dev, ABS_MT_POSITION_Y, y); | ||
943 | |||
944 | offset += 4; | ||
945 | } | ||
946 | } | ||
947 | |||
602 | static int sony_raw_event(struct hid_device *hdev, struct hid_report *report, | 948 | static int sony_raw_event(struct hid_device *hdev, struct hid_report *report, |
603 | __u8 *rd, int size) | 949 | __u8 *rd, int size) |
604 | { | 950 | { |
605 | struct sony_sc *sc = hid_get_drvdata(hdev); | 951 | struct sony_sc *sc = hid_get_drvdata(hdev); |
606 | 952 | ||
607 | /* Sixaxis HID report has acclerometers/gyro with MSByte first, this | 953 | /* |
954 | * Sixaxis HID report has acclerometers/gyro with MSByte first, this | ||
608 | * has to be BYTE_SWAPPED before passing up to joystick interface | 955 | * has to be BYTE_SWAPPED before passing up to joystick interface |
609 | */ | 956 | */ |
610 | if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) && | 957 | if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) { |
611 | rd[0] == 0x01 && size == 49) { | ||
612 | swap(rd[41], rd[42]); | 958 | swap(rd[41], rd[42]); |
613 | swap(rd[43], rd[44]); | 959 | swap(rd[43], rd[44]); |
614 | swap(rd[45], rd[46]); | 960 | swap(rd[45], rd[46]); |
615 | swap(rd[47], rd[48]); | 961 | swap(rd[47], rd[48]); |
962 | |||
963 | sixaxis_parse_report(sc, rd, size); | ||
964 | } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 && | ||
965 | size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) | ||
966 | && rd[0] == 0x11 && size == 78)) { | ||
967 | dualshock4_parse_report(sc, rd, size); | ||
616 | } | 968 | } |
617 | 969 | ||
618 | return 0; | 970 | return 0; |
@@ -724,6 +1076,18 @@ static int sixaxis_set_operational_bt(struct hid_device *hdev) | |||
724 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT); | 1076 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT); |
725 | } | 1077 | } |
726 | 1078 | ||
1079 | /* | ||
1080 | * Requesting feature report 0x02 in Bluetooth mode changes the state of the | ||
1081 | * controller so that it sends full input reports of type 0x11. | ||
1082 | */ | ||
1083 | static int dualshock4_set_operational_bt(struct hid_device *hdev) | ||
1084 | { | ||
1085 | __u8 buf[37] = { 0 }; | ||
1086 | |||
1087 | return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf), | ||
1088 | HID_FEATURE_REPORT, HID_REQ_GET_REPORT); | ||
1089 | } | ||
1090 | |||
727 | static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds) | 1091 | static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds) |
728 | { | 1092 | { |
729 | struct list_head *report_list = | 1093 | struct list_head *report_list = |
@@ -751,8 +1115,7 @@ static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count) | |||
751 | 1115 | ||
752 | if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) { | 1116 | if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) { |
753 | buzz_set_leds(hdev, leds); | 1117 | buzz_set_leds(hdev, leds); |
754 | } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) || | 1118 | } else { |
755 | (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) { | ||
756 | for (n = 0; n < count; n++) | 1119 | for (n = 0; n < count; n++) |
757 | drv_data->led_state[n] = leds[n]; | 1120 | drv_data->led_state[n] = leds[n]; |
758 | schedule_work(&drv_data->state_worker); | 1121 | schedule_work(&drv_data->state_worker); |
@@ -792,7 +1155,6 @@ static enum led_brightness sony_led_get_brightness(struct led_classdev *led) | |||
792 | struct sony_sc *drv_data; | 1155 | struct sony_sc *drv_data; |
793 | 1156 | ||
794 | int n; | 1157 | int n; |
795 | int on = 0; | ||
796 | 1158 | ||
797 | drv_data = hid_get_drvdata(hdev); | 1159 | drv_data = hid_get_drvdata(hdev); |
798 | if (!drv_data) { | 1160 | if (!drv_data) { |
@@ -801,13 +1163,11 @@ static enum led_brightness sony_led_get_brightness(struct led_classdev *led) | |||
801 | } | 1163 | } |
802 | 1164 | ||
803 | for (n = 0; n < drv_data->led_count; n++) { | 1165 | for (n = 0; n < drv_data->led_count; n++) { |
804 | if (led == drv_data->leds[n]) { | 1166 | if (led == drv_data->leds[n]) |
805 | on = !!(drv_data->led_state[n]); | 1167 | return drv_data->led_state[n]; |
806 | break; | ||
807 | } | ||
808 | } | 1168 | } |
809 | 1169 | ||
810 | return on ? LED_FULL : LED_OFF; | 1170 | return LED_OFF; |
811 | } | 1171 | } |
812 | 1172 | ||
813 | static void sony_leds_remove(struct hid_device *hdev) | 1173 | static void sony_leds_remove(struct hid_device *hdev) |
@@ -857,7 +1217,7 @@ static int sony_leds_init(struct hid_device *hdev) | |||
857 | /* Validate expected report characteristics. */ | 1217 | /* Validate expected report characteristics. */ |
858 | if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7)) | 1218 | if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7)) |
859 | return -ENODEV; | 1219 | return -ENODEV; |
860 | } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) { | 1220 | } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER) { |
861 | drv_data->led_count = 3; | 1221 | drv_data->led_count = 3; |
862 | max_brightness = 255; | 1222 | max_brightness = 255; |
863 | use_colors = 1; | 1223 | use_colors = 1; |
@@ -871,9 +1231,11 @@ static int sony_leds_init(struct hid_device *hdev) | |||
871 | name_fmt = "%s::sony%d"; | 1231 | name_fmt = "%s::sony%d"; |
872 | } | 1232 | } |
873 | 1233 | ||
874 | /* Clear LEDs as we have no way of reading their initial state. This is | 1234 | /* |
1235 | * Clear LEDs as we have no way of reading their initial state. This is | ||
875 | * only relevant if the driver is loaded after somebody actively set the | 1236 | * only relevant if the driver is loaded after somebody actively set the |
876 | * LEDs to on */ | 1237 | * LEDs to on |
1238 | */ | ||
877 | sony_set_leds(hdev, initial_values, drv_data->led_count); | 1239 | sony_set_leds(hdev, initial_values, drv_data->led_count); |
878 | 1240 | ||
879 | name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1; | 1241 | name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1; |
@@ -943,28 +1305,48 @@ static void sixaxis_state_worker(struct work_struct *work) | |||
943 | buf[10] |= sc->led_state[2] << 3; | 1305 | buf[10] |= sc->led_state[2] << 3; |
944 | buf[10] |= sc->led_state[3] << 4; | 1306 | buf[10] |= sc->led_state[3] << 4; |
945 | 1307 | ||
946 | hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT); | 1308 | if (sc->quirks & SIXAXIS_CONTROLLER_USB) |
1309 | hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT); | ||
1310 | else | ||
1311 | hid_hw_raw_request(sc->hdev, 0x01, buf, sizeof(buf), | ||
1312 | HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); | ||
947 | } | 1313 | } |
948 | 1314 | ||
949 | static void dualshock4_state_worker(struct work_struct *work) | 1315 | static void dualshock4_state_worker(struct work_struct *work) |
950 | { | 1316 | { |
951 | struct sony_sc *sc = container_of(work, struct sony_sc, state_worker); | 1317 | struct sony_sc *sc = container_of(work, struct sony_sc, state_worker); |
952 | struct hid_device *hdev = sc->hdev; | 1318 | struct hid_device *hdev = sc->hdev; |
953 | struct hid_report *report = sc->output_report; | 1319 | int offset; |
954 | __s32 *value = report->field[0]->value; | 1320 | |
1321 | __u8 buf[78] = { 0 }; | ||
955 | 1322 | ||
956 | value[0] = 0x03; | 1323 | if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { |
1324 | buf[0] = 0x05; | ||
1325 | buf[1] = 0x03; | ||
1326 | offset = 4; | ||
1327 | } else { | ||
1328 | buf[0] = 0x11; | ||
1329 | buf[1] = 0xB0; | ||
1330 | buf[3] = 0x0F; | ||
1331 | offset = 6; | ||
1332 | } | ||
957 | 1333 | ||
958 | #ifdef CONFIG_SONY_FF | 1334 | #ifdef CONFIG_SONY_FF |
959 | value[3] = sc->right; | 1335 | buf[offset++] = sc->right; |
960 | value[4] = sc->left; | 1336 | buf[offset++] = sc->left; |
1337 | #else | ||
1338 | offset += 2; | ||
961 | #endif | 1339 | #endif |
962 | 1340 | ||
963 | value[5] = sc->led_state[0]; | 1341 | buf[offset++] = sc->led_state[0]; |
964 | value[6] = sc->led_state[1]; | 1342 | buf[offset++] = sc->led_state[1]; |
965 | value[7] = sc->led_state[2]; | 1343 | buf[offset++] = sc->led_state[2]; |
966 | 1344 | ||
967 | hid_hw_request(hdev, report, HID_REQ_SET_REPORT); | 1345 | if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) |
1346 | hid_hw_output_report(hdev, buf, 32); | ||
1347 | else | ||
1348 | hid_hw_raw_request(hdev, 0x11, buf, 78, | ||
1349 | HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); | ||
968 | } | 1350 | } |
969 | 1351 | ||
970 | #ifdef CONFIG_SONY_FF | 1352 | #ifdef CONFIG_SONY_FF |
@@ -994,51 +1376,252 @@ static int sony_init_ff(struct hid_device *hdev) | |||
994 | return input_ff_create_memless(input_dev, NULL, sony_play_effect); | 1376 | return input_ff_create_memless(input_dev, NULL, sony_play_effect); |
995 | } | 1377 | } |
996 | 1378 | ||
997 | static void sony_destroy_ff(struct hid_device *hdev) | 1379 | #else |
1380 | static int sony_init_ff(struct hid_device *hdev) | ||
998 | { | 1381 | { |
999 | struct sony_sc *sc = hid_get_drvdata(hdev); | 1382 | return 0; |
1383 | } | ||
1384 | |||
1385 | #endif | ||
1000 | 1386 | ||
1001 | cancel_work_sync(&sc->state_worker); | 1387 | static int sony_battery_get_property(struct power_supply *psy, |
1388 | enum power_supply_property psp, | ||
1389 | union power_supply_propval *val) | ||
1390 | { | ||
1391 | struct sony_sc *sc = container_of(psy, struct sony_sc, battery); | ||
1392 | unsigned long flags; | ||
1393 | int ret = 0; | ||
1394 | u8 battery_charging, battery_capacity, cable_state; | ||
1395 | |||
1396 | spin_lock_irqsave(&sc->lock, flags); | ||
1397 | battery_charging = sc->battery_charging; | ||
1398 | battery_capacity = sc->battery_capacity; | ||
1399 | cable_state = sc->cable_state; | ||
1400 | spin_unlock_irqrestore(&sc->lock, flags); | ||
1401 | |||
1402 | switch (psp) { | ||
1403 | case POWER_SUPPLY_PROP_PRESENT: | ||
1404 | val->intval = 1; | ||
1405 | break; | ||
1406 | case POWER_SUPPLY_PROP_SCOPE: | ||
1407 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; | ||
1408 | break; | ||
1409 | case POWER_SUPPLY_PROP_CAPACITY: | ||
1410 | val->intval = battery_capacity; | ||
1411 | break; | ||
1412 | case POWER_SUPPLY_PROP_STATUS: | ||
1413 | if (battery_charging) | ||
1414 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | ||
1415 | else | ||
1416 | if (battery_capacity == 100 && cable_state) | ||
1417 | val->intval = POWER_SUPPLY_STATUS_FULL; | ||
1418 | else | ||
1419 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | ||
1420 | break; | ||
1421 | default: | ||
1422 | ret = -EINVAL; | ||
1423 | break; | ||
1424 | } | ||
1425 | return ret; | ||
1002 | } | 1426 | } |
1003 | 1427 | ||
1004 | #else | 1428 | static int sony_battery_probe(struct sony_sc *sc) |
1005 | static int sony_init_ff(struct hid_device *hdev) | ||
1006 | { | 1429 | { |
1430 | static atomic_t power_id_seq = ATOMIC_INIT(0); | ||
1431 | unsigned long power_id; | ||
1432 | struct hid_device *hdev = sc->hdev; | ||
1433 | int ret; | ||
1434 | |||
1435 | /* | ||
1436 | * Set the default battery level to 100% to avoid low battery warnings | ||
1437 | * if the battery is polled before the first device report is received. | ||
1438 | */ | ||
1439 | sc->battery_capacity = 100; | ||
1440 | |||
1441 | power_id = (unsigned long)atomic_inc_return(&power_id_seq); | ||
1442 | |||
1443 | sc->battery.properties = sony_battery_props; | ||
1444 | sc->battery.num_properties = ARRAY_SIZE(sony_battery_props); | ||
1445 | sc->battery.get_property = sony_battery_get_property; | ||
1446 | sc->battery.type = POWER_SUPPLY_TYPE_BATTERY; | ||
1447 | sc->battery.use_for_apm = 0; | ||
1448 | sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu", | ||
1449 | power_id); | ||
1450 | if (!sc->battery.name) | ||
1451 | return -ENOMEM; | ||
1452 | |||
1453 | ret = power_supply_register(&hdev->dev, &sc->battery); | ||
1454 | if (ret) { | ||
1455 | hid_err(hdev, "Unable to register battery device\n"); | ||
1456 | goto err_free; | ||
1457 | } | ||
1458 | |||
1459 | power_supply_powers(&sc->battery, &hdev->dev); | ||
1007 | return 0; | 1460 | return 0; |
1461 | |||
1462 | err_free: | ||
1463 | kfree(sc->battery.name); | ||
1464 | sc->battery.name = NULL; | ||
1465 | return ret; | ||
1008 | } | 1466 | } |
1009 | 1467 | ||
1010 | static void sony_destroy_ff(struct hid_device *hdev) | 1468 | static void sony_battery_remove(struct sony_sc *sc) |
1011 | { | 1469 | { |
1470 | if (!sc->battery.name) | ||
1471 | return; | ||
1472 | |||
1473 | power_supply_unregister(&sc->battery); | ||
1474 | kfree(sc->battery.name); | ||
1475 | sc->battery.name = NULL; | ||
1012 | } | 1476 | } |
1013 | #endif | ||
1014 | 1477 | ||
1015 | static int sony_set_output_report(struct sony_sc *sc, int req_id, int req_size) | 1478 | static int sony_register_touchpad(struct sony_sc *sc, int touch_count, |
1479 | int w, int h) | ||
1016 | { | 1480 | { |
1017 | struct list_head *head, *list; | 1481 | struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, |
1018 | struct hid_report *report; | 1482 | struct hid_input, list); |
1019 | struct hid_device *hdev = sc->hdev; | 1483 | struct input_dev *input_dev = hidinput->input; |
1484 | int ret; | ||
1485 | |||
1486 | ret = input_mt_init_slots(input_dev, touch_count, 0); | ||
1487 | if (ret < 0) { | ||
1488 | hid_err(sc->hdev, "Unable to initialize multi-touch slots\n"); | ||
1489 | return ret; | ||
1490 | } | ||
1020 | 1491 | ||
1021 | list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list; | 1492 | input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0); |
1493 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0); | ||
1022 | 1494 | ||
1023 | list_for_each(head, list) { | 1495 | return 0; |
1024 | report = list_entry(head, struct hid_report, list); | 1496 | } |
1025 | 1497 | ||
1026 | if (report->id == req_id) { | 1498 | /* |
1027 | if (report->size < req_size) { | 1499 | * If a controller is plugged in via USB while already connected via Bluetooth |
1028 | hid_err(hdev, "Output report 0x%02x (%i bits) is smaller than requested size (%i bits)\n", | 1500 | * it will show up as two devices. A global list of connected controllers and |
1029 | req_id, report->size, req_size); | 1501 | * their MAC addresses is maintained to ensure that a device is only connected |
1030 | return -EINVAL; | 1502 | * once. |
1031 | } | 1503 | */ |
1032 | sc->output_report = report; | 1504 | static int sony_check_add_dev_list(struct sony_sc *sc) |
1033 | return 0; | 1505 | { |
1506 | struct sony_sc *entry; | ||
1507 | unsigned long flags; | ||
1508 | int ret; | ||
1509 | |||
1510 | spin_lock_irqsave(&sony_dev_list_lock, flags); | ||
1511 | |||
1512 | list_for_each_entry(entry, &sony_device_list, list_node) { | ||
1513 | ret = memcmp(sc->mac_address, entry->mac_address, | ||
1514 | sizeof(sc->mac_address)); | ||
1515 | if (!ret) { | ||
1516 | ret = -EEXIST; | ||
1517 | hid_info(sc->hdev, "controller with MAC address %pMR already connected\n", | ||
1518 | sc->mac_address); | ||
1519 | goto unlock; | ||
1034 | } | 1520 | } |
1035 | } | 1521 | } |
1036 | 1522 | ||
1037 | hid_err(hdev, "Unable to locate output report 0x%02x\n", req_id); | 1523 | ret = 0; |
1524 | list_add(&(sc->list_node), &sony_device_list); | ||
1038 | 1525 | ||
1039 | return -EINVAL; | 1526 | unlock: |
1527 | spin_unlock_irqrestore(&sony_dev_list_lock, flags); | ||
1528 | return ret; | ||
1529 | } | ||
1530 | |||
1531 | static void sony_remove_dev_list(struct sony_sc *sc) | ||
1532 | { | ||
1533 | unsigned long flags; | ||
1534 | |||
1535 | if (sc->list_node.next) { | ||
1536 | spin_lock_irqsave(&sony_dev_list_lock, flags); | ||
1537 | list_del(&(sc->list_node)); | ||
1538 | spin_unlock_irqrestore(&sony_dev_list_lock, flags); | ||
1539 | } | ||
1040 | } | 1540 | } |
1041 | 1541 | ||
1542 | static int sony_get_bt_devaddr(struct sony_sc *sc) | ||
1543 | { | ||
1544 | int ret; | ||
1545 | |||
1546 | /* HIDP stores the device MAC address as a string in the uniq field. */ | ||
1547 | ret = strlen(sc->hdev->uniq); | ||
1548 | if (ret != 17) | ||
1549 | return -EINVAL; | ||
1550 | |||
1551 | ret = sscanf(sc->hdev->uniq, | ||
1552 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", | ||
1553 | &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3], | ||
1554 | &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]); | ||
1555 | |||
1556 | if (ret != 6) | ||
1557 | return -EINVAL; | ||
1558 | |||
1559 | return 0; | ||
1560 | } | ||
1561 | |||
1562 | static int sony_check_add(struct sony_sc *sc) | ||
1563 | { | ||
1564 | int n, ret; | ||
1565 | |||
1566 | if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) || | ||
1567 | (sc->quirks & SIXAXIS_CONTROLLER_BT)) { | ||
1568 | /* | ||
1569 | * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC | ||
1570 | * address from the uniq string where HIDP stores it. | ||
1571 | * As uniq cannot be guaranteed to be a MAC address in all cases | ||
1572 | * a failure of this function should not prevent the connection. | ||
1573 | */ | ||
1574 | if (sony_get_bt_devaddr(sc) < 0) { | ||
1575 | hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n"); | ||
1576 | return 0; | ||
1577 | } | ||
1578 | } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { | ||
1579 | __u8 buf[7]; | ||
1580 | |||
1581 | /* | ||
1582 | * The MAC address of a DS4 controller connected via USB can be | ||
1583 | * retrieved with feature report 0x81. The address begins at | ||
1584 | * offset 1. | ||
1585 | */ | ||
1586 | ret = hid_hw_raw_request(sc->hdev, 0x81, buf, sizeof(buf), | ||
1587 | HID_FEATURE_REPORT, HID_REQ_GET_REPORT); | ||
1588 | |||
1589 | if (ret != 7) { | ||
1590 | hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n"); | ||
1591 | return ret < 0 ? ret : -EINVAL; | ||
1592 | } | ||
1593 | |||
1594 | memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address)); | ||
1595 | } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) { | ||
1596 | __u8 buf[18]; | ||
1597 | |||
1598 | /* | ||
1599 | * The MAC address of a Sixaxis controller connected via USB can | ||
1600 | * be retrieved with feature report 0xf2. The address begins at | ||
1601 | * offset 4. | ||
1602 | */ | ||
1603 | ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, sizeof(buf), | ||
1604 | HID_FEATURE_REPORT, HID_REQ_GET_REPORT); | ||
1605 | |||
1606 | if (ret != 18) { | ||
1607 | hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n"); | ||
1608 | return ret < 0 ? ret : -EINVAL; | ||
1609 | } | ||
1610 | |||
1611 | /* | ||
1612 | * The Sixaxis device MAC in the report is big-endian and must | ||
1613 | * be byte-swapped. | ||
1614 | */ | ||
1615 | for (n = 0; n < 6; n++) | ||
1616 | sc->mac_address[5-n] = buf[4+n]; | ||
1617 | } else { | ||
1618 | return 0; | ||
1619 | } | ||
1620 | |||
1621 | return sony_check_add_dev_list(sc); | ||
1622 | } | ||
1623 | |||
1624 | |||
1042 | static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) | 1625 | static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) |
1043 | { | 1626 | { |
1044 | int ret; | 1627 | int ret; |
@@ -1078,16 +1661,29 @@ static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) | |||
1078 | if (sc->quirks & SIXAXIS_CONTROLLER_USB) { | 1661 | if (sc->quirks & SIXAXIS_CONTROLLER_USB) { |
1079 | hdev->hid_output_raw_report = sixaxis_usb_output_raw_report; | 1662 | hdev->hid_output_raw_report = sixaxis_usb_output_raw_report; |
1080 | ret = sixaxis_set_operational_usb(hdev); | 1663 | ret = sixaxis_set_operational_usb(hdev); |
1664 | sc->worker_initialized = 1; | ||
1081 | INIT_WORK(&sc->state_worker, sixaxis_state_worker); | 1665 | INIT_WORK(&sc->state_worker, sixaxis_state_worker); |
1082 | } | 1666 | } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) { |
1083 | else if (sc->quirks & SIXAXIS_CONTROLLER_BT) | ||
1084 | ret = sixaxis_set_operational_bt(hdev); | 1667 | ret = sixaxis_set_operational_bt(hdev); |
1085 | else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { | 1668 | sc->worker_initialized = 1; |
1086 | /* Report 5 (31 bytes) is used to send data to the controller via USB */ | 1669 | INIT_WORK(&sc->state_worker, sixaxis_state_worker); |
1087 | ret = sony_set_output_report(sc, 0x05, 248); | 1670 | } else if (sc->quirks & DUALSHOCK4_CONTROLLER) { |
1671 | if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) { | ||
1672 | ret = dualshock4_set_operational_bt(hdev); | ||
1673 | if (ret < 0) { | ||
1674 | hid_err(hdev, "failed to set the Dualshock 4 operational mode\n"); | ||
1675 | goto err_stop; | ||
1676 | } | ||
1677 | } | ||
1678 | /* | ||
1679 | * The Dualshock 4 touchpad supports 2 touches and has a | ||
1680 | * resolution of 1920x940. | ||
1681 | */ | ||
1682 | ret = sony_register_touchpad(sc, 2, 1920, 940); | ||
1088 | if (ret < 0) | 1683 | if (ret < 0) |
1089 | goto err_stop; | 1684 | goto err_stop; |
1090 | 1685 | ||
1686 | sc->worker_initialized = 1; | ||
1091 | INIT_WORK(&sc->state_worker, dualshock4_state_worker); | 1687 | INIT_WORK(&sc->state_worker, dualshock4_state_worker); |
1092 | } else { | 1688 | } else { |
1093 | ret = 0; | 1689 | ret = 0; |
@@ -1096,20 +1692,46 @@ static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) | |||
1096 | if (ret < 0) | 1692 | if (ret < 0) |
1097 | goto err_stop; | 1693 | goto err_stop; |
1098 | 1694 | ||
1695 | ret = sony_check_add(sc); | ||
1696 | if (ret < 0) | ||
1697 | goto err_stop; | ||
1698 | |||
1099 | if (sc->quirks & SONY_LED_SUPPORT) { | 1699 | if (sc->quirks & SONY_LED_SUPPORT) { |
1100 | ret = sony_leds_init(hdev); | 1700 | ret = sony_leds_init(hdev); |
1101 | if (ret < 0) | 1701 | if (ret < 0) |
1102 | goto err_stop; | 1702 | goto err_stop; |
1103 | } | 1703 | } |
1104 | 1704 | ||
1105 | ret = sony_init_ff(hdev); | 1705 | if (sc->quirks & SONY_BATTERY_SUPPORT) { |
1106 | if (ret < 0) | 1706 | ret = sony_battery_probe(sc); |
1107 | goto err_stop; | 1707 | if (ret < 0) |
1708 | goto err_stop; | ||
1709 | |||
1710 | /* Open the device to receive reports with battery info */ | ||
1711 | ret = hid_hw_open(hdev); | ||
1712 | if (ret < 0) { | ||
1713 | hid_err(hdev, "hw open failed\n"); | ||
1714 | goto err_stop; | ||
1715 | } | ||
1716 | } | ||
1717 | |||
1718 | if (sc->quirks & SONY_FF_SUPPORT) { | ||
1719 | ret = sony_init_ff(hdev); | ||
1720 | if (ret < 0) | ||
1721 | goto err_close; | ||
1722 | } | ||
1108 | 1723 | ||
1109 | return 0; | 1724 | return 0; |
1725 | err_close: | ||
1726 | hid_hw_close(hdev); | ||
1110 | err_stop: | 1727 | err_stop: |
1111 | if (sc->quirks & SONY_LED_SUPPORT) | 1728 | if (sc->quirks & SONY_LED_SUPPORT) |
1112 | sony_leds_remove(hdev); | 1729 | sony_leds_remove(hdev); |
1730 | if (sc->quirks & SONY_BATTERY_SUPPORT) | ||
1731 | sony_battery_remove(sc); | ||
1732 | if (sc->worker_initialized) | ||
1733 | cancel_work_sync(&sc->state_worker); | ||
1734 | sony_remove_dev_list(sc); | ||
1113 | hid_hw_stop(hdev); | 1735 | hid_hw_stop(hdev); |
1114 | return ret; | 1736 | return ret; |
1115 | } | 1737 | } |
@@ -1121,7 +1743,15 @@ static void sony_remove(struct hid_device *hdev) | |||
1121 | if (sc->quirks & SONY_LED_SUPPORT) | 1743 | if (sc->quirks & SONY_LED_SUPPORT) |
1122 | sony_leds_remove(hdev); | 1744 | sony_leds_remove(hdev); |
1123 | 1745 | ||
1124 | sony_destroy_ff(hdev); | 1746 | if (sc->quirks & SONY_BATTERY_SUPPORT) { |
1747 | hid_hw_close(hdev); | ||
1748 | sony_battery_remove(sc); | ||
1749 | } | ||
1750 | |||
1751 | if (sc->worker_initialized) | ||
1752 | cancel_work_sync(&sc->state_worker); | ||
1753 | |||
1754 | sony_remove_dev_list(sc); | ||
1125 | 1755 | ||
1126 | hid_hw_stop(hdev); | 1756 | hid_hw_stop(hdev); |
1127 | } | 1757 | } |