Skip to content

Commit 6770652

Browse files
parojdtor
authored andcommitted
Input: xpad - decipher xpadone packages with GIP defines
only renames, no functional changes. Some of the packets we send seem superfluous now. Unfortunately I dont have the hardware to verify whether they are. Signed-off-by: Pavel Rojtberg <rojtberg@gmail.com> Link: https://lore.kernel.org/r/20220913213133.584979-3-rojtberg@gmail.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent 90c9978 commit 6770652

1 file changed

Lines changed: 69 additions & 30 deletions

File tree

drivers/input/joystick/xpad.c

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,52 @@ struct xboxone_init_packet {
514514
.len = ARRAY_SIZE(_data), \
515515
}
516516

517+
/*
518+
* starting with xbox one, the game input protocol is used
519+
* magic numbers are taken from
520+
* - https://github.com/xpadneo/gip-dissector/blob/main/src/gip-dissector.lua
521+
* - https://github.com/medusalix/xone/blob/master/bus/protocol.c
522+
*/
523+
#define GIP_CMD_ACK 0x01
524+
#define GIP_CMD_IDENTIFY 0x04
525+
#define GIP_CMD_POWER 0x05
526+
#define GIP_CMD_AUTHENTICATE 0x06
527+
#define GIP_CMD_VIRTUAL_KEY 0x07
528+
#define GIP_CMD_RUMBLE 0x09
529+
#define GIP_CMD_LED 0x0a
530+
#define GIP_CMD_FIRMWARE 0x0c
531+
#define GIP_CMD_INPUT 0x20
532+
533+
#define GIP_SEQ0 0x00
534+
535+
#define GIP_OPT_ACK 0x10
536+
#define GIP_OPT_INTERNAL 0x20
537+
538+
/*
539+
* length of the command payload encoded with
540+
* https://en.wikipedia.org/wiki/LEB128
541+
* which is a no-op for N < 128
542+
*/
543+
#define GIP_PL_LEN(N) (N)
544+
545+
/*
546+
* payload specific defines
547+
*/
548+
#define GIP_PWR_ON 0x00
549+
#define GIP_LED_ON 0x01
550+
551+
#define GIP_MOTOR_R BIT(0)
552+
#define GIP_MOTOR_L BIT(1)
553+
#define GIP_MOTOR_RT BIT(2)
554+
#define GIP_MOTOR_LT BIT(3)
555+
#define GIP_MOTOR_ALL (GIP_MOTOR_R | GIP_MOTOR_L | GIP_MOTOR_RT | GIP_MOTOR_LT)
517556

518557
/*
519558
* This packet is required for all Xbox One pads with 2015
520559
* or later firmware installed (or present from the factory).
521560
*/
522-
static const u8 xboxone_fw2015_init[] = {
523-
0x05, 0x20, 0x00, 0x01, 0x00
561+
static const u8 xboxone_power_on[] = {
562+
GIP_CMD_POWER, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(1), GIP_PWR_ON
524563
};
525564

526565
/*
@@ -530,7 +569,7 @@ static const u8 xboxone_fw2015_init[] = {
530569
* Bluetooth mode.
531570
*/
532571
static const u8 xboxone_s_init[] = {
533-
0x05, 0x20, 0x00, 0x0f, 0x06
572+
GIP_CMD_POWER, GIP_OPT_INTERNAL, GIP_SEQ0, 0x0f, 0x06
534573
};
535574

536575
/*
@@ -547,36 +586,36 @@ static const u8 extra_input_packet_init[] = {
547586
* (0x0e6f:0x0165) to finish initialization and for Hori pads
548587
* (0x0f0d:0x0067) to make the analog sticks work.
549588
*/
550-
static const u8 xboxone_hori_init[] = {
551-
0x01, 0x20, 0x00, 0x09, 0x00, 0x04, 0x20, 0x3a,
552-
0x00, 0x00, 0x00, 0x80, 0x00
589+
static const u8 xboxone_hori_ack_id[] = {
590+
GIP_CMD_ACK, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(9),
591+
0x00, GIP_CMD_IDENTIFY, GIP_OPT_INTERNAL, 0x3a, 0x00, 0x00, 0x00, 0x80, 0x00
553592
};
554593

555594
/*
556595
* This packet is required for most (all?) of the PDP pads to start
557596
* sending input reports. These pads include: (0x0e6f:0x02ab),
558597
* (0x0e6f:0x02a4), (0x0e6f:0x02a6).
559598
*/
560-
static const u8 xboxone_pdp_init1[] = {
561-
0x0a, 0x20, 0x00, 0x03, 0x00, 0x01, 0x14
599+
static const u8 xboxone_pdp_led_on[] = {
600+
GIP_CMD_LED, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(3), 0x00, GIP_LED_ON, 0x14
562601
};
563602

564603
/*
565604
* This packet is required for most (all?) of the PDP pads to start
566605
* sending input reports. These pads include: (0x0e6f:0x02ab),
567606
* (0x0e6f:0x02a4), (0x0e6f:0x02a6).
568607
*/
569-
static const u8 xboxone_pdp_init2[] = {
570-
0x06, 0x20, 0x00, 0x02, 0x01, 0x00
608+
static const u8 xboxone_pdp_auth[] = {
609+
GIP_CMD_AUTHENTICATE, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(2), 0x01, 0x00
571610
};
572611

573612
/*
574613
* A specific rumble packet is required for some PowerA pads to start
575614
* sending input reports. One of those pads is (0x24c6:0x543a).
576615
*/
577616
static const u8 xboxone_rumblebegin_init[] = {
578-
0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
579-
0x1D, 0x1D, 0xFF, 0x00, 0x00
617+
GIP_CMD_RUMBLE, 0x00, GIP_SEQ0, GIP_PL_LEN(9),
618+
0x00, GIP_MOTOR_ALL, 0x00, 0x00, 0x1D, 0x1D, 0xFF, 0x00, 0x00
580619
};
581620

582621
/*
@@ -586,8 +625,8 @@ static const u8 xboxone_rumblebegin_init[] = {
586625
* spin up to enough speed to actually vibrate the gamepad.
587626
*/
588627
static const u8 xboxone_rumbleend_init[] = {
589-
0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
590-
0x00, 0x00, 0x00, 0x00, 0x00
628+
GIP_CMD_RUMBLE, 0x00, GIP_SEQ0, GIP_PL_LEN(9),
629+
0x00, GIP_MOTOR_ALL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
591630
};
592631

593632
/*
@@ -597,14 +636,14 @@ static const u8 xboxone_rumbleend_init[] = {
597636
* packet is going to be sent.
598637
*/
599638
static const struct xboxone_init_packet xboxone_init_packets[] = {
600-
XBOXONE_INIT_PKT(0x0e6f, 0x0165, xboxone_hori_init),
601-
XBOXONE_INIT_PKT(0x0f0d, 0x0067, xboxone_hori_init),
602-
XBOXONE_INIT_PKT(0x0000, 0x0000, xboxone_fw2015_init),
639+
XBOXONE_INIT_PKT(0x0e6f, 0x0165, xboxone_hori_ack_id),
640+
XBOXONE_INIT_PKT(0x0f0d, 0x0067, xboxone_hori_ack_id),
641+
XBOXONE_INIT_PKT(0x0000, 0x0000, xboxone_power_on),
603642
XBOXONE_INIT_PKT(0x045e, 0x02ea, xboxone_s_init),
604643
XBOXONE_INIT_PKT(0x045e, 0x0b00, xboxone_s_init),
605644
XBOXONE_INIT_PKT(0x045e, 0x0b00, extra_input_packet_init),
606-
XBOXONE_INIT_PKT(0x0e6f, 0x0000, xboxone_pdp_init1),
607-
XBOXONE_INIT_PKT(0x0e6f, 0x0000, xboxone_pdp_init2),
645+
XBOXONE_INIT_PKT(0x0e6f, 0x0000, xboxone_pdp_led_on),
646+
XBOXONE_INIT_PKT(0x0e6f, 0x0000, xboxone_pdp_auth),
608647
XBOXONE_INIT_PKT(0x24c6, 0x541a, xboxone_rumblebegin_init),
609648
XBOXONE_INIT_PKT(0x24c6, 0x542a, xboxone_rumblebegin_init),
610649
XBOXONE_INIT_PKT(0x24c6, 0x543a, xboxone_rumblebegin_init),
@@ -920,19 +959,19 @@ static void xpadone_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char
920959
bool do_sync = false;
921960

922961
/* the xbox button has its own special report */
923-
if (data[0] == 0X07) {
962+
if (data[0] == GIP_CMD_VIRTUAL_KEY) {
924963
/*
925964
* The Xbox One S controller requires these reports to be
926965
* acked otherwise it continues sending them forever and
927966
* won't report further mode button events.
928967
*/
929-
if (data[1] == 0x30)
968+
if (data[1] == (GIP_OPT_ACK | GIP_OPT_INTERNAL))
930969
xpadone_ack_mode_report(xpad, data[2]);
931970

932971
input_report_key(dev, BTN_MODE, data[4] & BIT(0));
933972

934973
do_sync = true;
935-
} else if (data[0] == 0X0C) {
974+
} else if (data[0] == GIP_CMD_FIRMWARE) {
936975
/* Some packet formats force us to use this separate to poll paddle inputs */
937976
if (xpad->packet_type == PKT_XBE2_FW_5_11) {
938977
/* Mute paddles if controller is in a custom profile slot
@@ -950,7 +989,7 @@ static void xpadone_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char
950989

951990
do_sync = true;
952991
}
953-
} else if (data[0] == 0X20) { /* The main valid packet type for inputs */
992+
} else if (data[0] == GIP_CMD_INPUT) { /* The main valid packet type for inputs */
954993
/* menu/view buttons */
955994
input_report_key(dev, BTN_START, data[4] & BIT(2));
956995
input_report_key(dev, BTN_SELECT, data[4] & BIT(3));
@@ -1363,8 +1402,8 @@ static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num)
13631402
struct xpad_output_packet *packet =
13641403
&xpad->out_packets[XPAD_OUT_CMD_IDX];
13651404
static const u8 mode_report_ack[] = {
1366-
0x01, 0x20, 0x00, 0x09, 0x00, 0x07, 0x20, 0x02,
1367-
0x00, 0x00, 0x00, 0x00, 0x00
1405+
GIP_CMD_ACK, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(9),
1406+
0x00, GIP_CMD_VIRTUAL_KEY, GIP_OPT_INTERNAL, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00
13681407
};
13691408

13701409
spin_lock_irqsave(&xpad->odata_lock, flags);
@@ -1442,14 +1481,14 @@ static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect
14421481
break;
14431482

14441483
case XTYPE_XBOXONE:
1445-
packet->data[0] = 0x09; /* activate rumble */
1484+
packet->data[0] = GIP_CMD_RUMBLE; /* activate rumble */
14461485
packet->data[1] = 0x00;
14471486
packet->data[2] = xpad->odata_serial++;
1448-
packet->data[3] = 0x09;
1487+
packet->data[3] = GIP_PL_LEN(9);
14491488
packet->data[4] = 0x00;
1450-
packet->data[5] = 0x0F;
1451-
packet->data[6] = 0x00;
1452-
packet->data[7] = 0x00;
1489+
packet->data[5] = GIP_MOTOR_ALL;
1490+
packet->data[6] = 0x00; /* left trigger */
1491+
packet->data[7] = 0x00; /* right trigger */
14531492
packet->data[8] = strong / 512; /* left actuator */
14541493
packet->data[9] = weak / 512; /* right actuator */
14551494
packet->data[10] = 0xFF; /* on period */

0 commit comments

Comments
 (0)