Skip to contentSkip to conversation

xpad: Overhaul device data for wireless devices

PATCH Superseded Sanjay Govind 4 participants 9 messages Apr 4 – Apr 8

Subsystems (2)

v2 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xpad: Overhaul device data for wireless devices

Xbox 360 wireless controllers expose information in the link and capabilities reports.

Extract and use the vendor id for wireless controllers, and use the subtype to build a nicer device name and product id.

Some xbox 360 controllers put a vid and pid into the stick capability data, so check if this was done, and pull the vid, pid and revision from there.

---
v2: Delay marking device as present until after capabilities or timeout

 drivers/input/joystick/xpad.c | 162 ++++++++++++++++++++++++++++++++--
 1 file changed, 155 insertions(+), 7 deletions(-)
drivers/input/joystick/xpad.c +155 -7
index bf4accf3f581..c35512c7c199 100644
@@ -68,6 +68,7 @@
 #include <linux/slab.h>
 #include <linux/stat.h>
 #include <linux/module.h>
+#include <linux/unaligned.h>
 #include <linux/usb/input.h>
 #include <linux/usb/quirks.h>
 
@@ -94,6 +95,22 @@
 #define XTYPE_XBOXONE     3
 #define XTYPE_UNKNOWN     4
 
+#define FLAG_FORCE_FEEDBACK	0x01
+
+#define SUBTYPE_GAMEPAD			 0x01
+#define SUBTYPE_WHEEL			 0x02
+#define SUBTYPE_ARCADE_STICK	 0x03
+#define SUBTYPE_FLIGHT_SICK		 0x04
+#define SUBTYPE_DANCE_PAD		 0x05
+#define SUBTYPE_GUITAR			 0x06
+#define SUBTYPE_GUITAR_ALTERNATE 0x07
+#define SUBTYPE_DRUM_KIT		 0x08
+#define SUBTYPE_GUITAR_BASS		 0x0B
+#define SUBTYPE_RB_KEYBOARD		 0x0F
+#define SUBTYPE_ARCADE_PAD		 0x13
+#define SUBTYPE_TURNTABLE		 0x17
+#define SUBTYPE_PRO_GUITAR		 0x19
+
 /* Send power-off packet to xpad360w after holding the mode button for this many
  * seconds
  */
@@ -795,8 +812,13 @@ struct usb_xpad {
 	int xtype;			/* type of xbox device */
 	int packet_type;		/* type of the extended packet */
 	int pad_nr;			/* the order x360 pads were attached */
+	u8 sub_type;
+	u16 flags;
+	u16 wireless_vid;
+	u16 wireless_pid;
+	u16 wireless_version;
 	const char *name;		/* name of the device */
-	struct work_struct work;	/* init/remove device from callback */
+	struct delayed_work work;	/* init/remove device from callback */
 	time64_t mode_btn_down_ts;
 	bool delay_init;		/* init packets should be delayed */
 	bool delayed_init_done;
@@ -807,6 +829,8 @@ static void xpad_deinit_input(struct usb_xpad *xpad);
 static int xpad_start_input(struct usb_xpad *xpad);
 static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num);
 static void xpad360w_poweroff_controller(struct usb_xpad *xpad);
+static int xpad_inquiry_pad_capabilities(struct usb_xpad *xpad);
+
 
 /*
  *	xpad_process_packet
@@ -980,7 +1004,7 @@ static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
 
 static void xpad_presence_work(struct work_struct *work)
 {
-	struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
+	struct usb_xpad *xpad = container_of(work, struct usb_xpad, work.work);
 	int error;
 
 	if (xpad->pad_present) {
@@ -1028,10 +1052,60 @@ static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned cha
 
 		if (xpad->pad_present != present) {
 			xpad->pad_present = present;
-			schedule_work(&xpad->work);
+			if (present) {
+				/*
+				 * Delay marking device as present, so we can make sure
+				 * we have received all the information from the capabilities
+				 * report. Some devices don't send one, so the delay
+				 * guarantees that these devices are still initialized.
+				 */
+				schedule_delayed_work(&xpad->work, msecs_to_jiffies(500));
+			} else {
+				schedule_delayed_work(&xpad->work, 0);
+			}
 		}
 	}
 
+	/* Link report */
+	if (data[0] == 0x00 && data[1] == 0x0F) {
+		xpad->sub_type = data[25] & 0x7f;
+
+		/* Decode vendor id from link report */
+		xpad->wireless_vid = ((data[0x16] & 0xf) | data[0x18] << 4) << 8 | data[0x17];
+		/*
+		 * x360w controllers on windows put the subtype into the product
+		 * for wheels and gamepads, but it makes sense to do it for all
+		 * subtypes. This will be used if the capabilities report
+		 * doesn't provide us with a product id later.
+		 */
+		xpad->wireless_pid = 0x02a0 + xpad->sub_type;
+		xpad->wireless_version = 0;
+
+		if ((data[25] & 0x80) != 0)
+			xpad->flags |= FLAG_FORCE_FEEDBACK;
+
+		xpad_inquiry_pad_capabilities(xpad);
+	}
+
+	/* Capabilities report */
+	if (data[0] == 0x00 && data[1] == 0x05 && data[5] == 0x12) {
+		xpad->flags |= data[20];
+		/*
+		 * A bunch of vendors started putting vids and pids
+		 * into capabilities data because they can't be
+		 * retrieved by xinput easliy.
+		 * Not all of them do though, so check the vids match
+		 * before extracting that info.
+		 */
+		if (get_unaligned_le16(data + 10) == xpad->wireless_vid) {
+			xpad->wireless_pid = get_unaligned_le16(data + 12);
+			xpad->wireless_version = get_unaligned_le16(data + 14);
+		}
+		/* We got the capabilities report, so mark the device present now */
+		cancel_delayed_work(&xpad->work);
+		schedule_delayed_work(&xpad->work, 0);
+	}
+
 	/* Valid pad data */
 	if (data[1] != 0x1)
 		return;
@@ -1495,6 +1569,31 @@ static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
 	return xpad_try_sending_next_out_packet(xpad);
 }
 
+static int xpad_inquiry_pad_capabilities(struct usb_xpad *xpad)
+{
+	struct xpad_output_packet *packet =
+			&xpad->out_packets[XPAD_OUT_CMD_IDX];
+
+	guard(spinlock_irqsave)(&xpad->odata_lock);
+
+	packet->data[0] = 0x00;
+	packet->data[1] = 0x00;
+	packet->data[2] = 0x02;
+	packet->data[3] = 0x80;
+	packet->data[4] = 0x00;
+	packet->data[5] = 0x00;
+	packet->data[6] = 0x00;
+	packet->data[7] = 0x00;
+	packet->data[8] = 0x00;
+	packet->data[9] = 0x00;
+	packet->data[10] = 0x00;
+	packet->data[11] = 0x00;
+	packet->len = 12;
+	packet->pending = true;
+
+	return xpad_try_sending_next_out_packet(xpad);
+}
+
 static int xpad_start_xbox_one(struct usb_xpad *xpad)
 {
 	int error;
@@ -1893,7 +1992,7 @@ static void xpad360w_stop_input(struct usb_xpad *xpad)
 	usb_kill_urb(xpad->irq_in);
 
 	/* Make sure we are done with presence work if it was scheduled */
-	flush_work(&xpad->work);
+	flush_delayed_work(&xpad->work);
 }
 
 static int xpad_open(struct input_dev *dev)
@@ -1965,8 +2064,57 @@ static int xpad_init_input(struct usb_xpad *xpad)
 	usb_to_input_id(xpad->udev, &input_dev->id);
 
 	if (xpad->xtype == XTYPE_XBOX360W) {
-		/* x360w controllers and the receiver have different ids */
-		input_dev->id.product = 0x02a1;
+		/* If the Link report has provided a vid, it won't be set to 1 */
+		if (xpad->wireless_vid != 1)
+			input_dev->id.vendor = xpad->wireless_vid;
+		if (xpad->wireless_version)
+			input_dev->id.version = xpad->wireless_version;
+		input_dev->id.product = xpad->wireless_pid;
+		switch (xpad->sub_type) {
+		case SUBTYPE_GAMEPAD:
+			input_dev->name = "Xbox 360 Wireless Controller";
+			break;
+		case SUBTYPE_WHEEL:
+			input_dev->name = "Xbox 360 Wireless Wheel";
+			break;
+		case SUBTYPE_ARCADE_STICK:
+			input_dev->name = "Xbox 360 Wireless Arcade Stick";
+			break;
+		case SUBTYPE_FLIGHT_SICK:
+			input_dev->name = "Xbox 360 Wireless Flight Stick";
+			break;
+		case SUBTYPE_DANCE_PAD:
+			input_dev->name = "Xbox 360 Wireless Dance Pad";
+			break;
+		case SUBTYPE_GUITAR:
+			input_dev->name = "Xbox 360 Wireless Guitar";
+			break;
+		case SUBTYPE_GUITAR_ALTERNATE:
+			input_dev->name = "Xbox 360 Wireless Alternate Guitar";
+			break;
+		case SUBTYPE_GUITAR_BASS:
+			input_dev->name = "Xbox 360 Wireless Bass Guitar";
+			break;
+		case SUBTYPE_DRUM_KIT:
+			/* Vendors used force feedback flag to differentiate these */
+			if (xpad->flags & FLAG_FORCE_FEEDBACK)
+				input_dev->name = "Xbox 360 Wireless Guitar Hero Drum Kit";
+			else
+				input_dev->name = "Xbox 360 Wireless Rock Band Drum Kit";
+			break;
+		case SUBTYPE_RB_KEYBOARD:
+			input_dev->name = "Xbox 360 Wireless Rock Band Keyboard";
+			break;
+		case SUBTYPE_ARCADE_PAD:
+			input_dev->name = "Xbox 360 Wireless Arcade Pad";
+			break;
+		case SUBTYPE_TURNTABLE:
+			input_dev->name = "Xbox 360 Wireless DJ Hero Turntable";
+			break;
+		case SUBTYPE_PRO_GUITAR:
+			input_dev->name = "Xbox 360 Wireless Rock Band Pro Guitar";
+			break;
+		}
 	}
 
 	input_dev->dev.parent = &xpad->intf->dev;
@@ -2106,7 +2254,7 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 		xpad->delay_init = true;
 
 	xpad->packet_type = PKT_XB;
-	INIT_WORK(&xpad->work, xpad_presence_work);
+	INIT_DELAYED_WORK(&xpad->work, xpad_presence_work);
 
 	if (xpad->xtype == XTYPE_UNKNOWN) {
 		if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
Signature · 1 line
2.53.0
maintainer
#1 lore raw

Hi Sanjay,

Quoted 9 lines · Sanjay Govind

Xbox 360 wireless controllers expose information in the link and capabilities reports.

Extract and use the vendor id for wireless controllers, and use the subtype to build a nicer device name and product id.

Some xbox 360 controllers put a vid and pid into the stick capability data, so check if this was done, and pull the vid, pid and revision from there.

Sashuko correctly identified issues areoud re-scheduling work that already completed, please see:

https://sashiko.dev/#/patchset/[email protected]

Quoted 80 lines

---
v2: Delay marking device as present until after capabilities or timeout

 drivers/input/joystick/xpad.c | 162 ++++++++++++++++++++++++++++++++--
 1 file changed, 155 insertions(+), 7 deletions(-)
drivers/input/joystick/xpad.c +26 -3
index bf4accf3f581..c35512c7c199 100644
@@ -68,6 +68,7 @@
 #include <linux/slab.h>
 #include <linux/stat.h>
 #include <linux/module.h>
+#include <linux/unaligned.h>
 #include <linux/usb/input.h>
 #include <linux/usb/quirks.h>
 
@@ -94,6 +95,22 @@
 #define XTYPE_XBOXONE     3
 #define XTYPE_UNKNOWN     4
 
+#define FLAG_FORCE_FEEDBACK	0x01
+
+#define SUBTYPE_GAMEPAD			 0x01
+#define SUBTYPE_WHEEL			 0x02
+#define SUBTYPE_ARCADE_STICK	 0x03
+#define SUBTYPE_FLIGHT_SICK		 0x04
+#define SUBTYPE_DANCE_PAD		 0x05
+#define SUBTYPE_GUITAR			 0x06
+#define SUBTYPE_GUITAR_ALTERNATE 0x07
+#define SUBTYPE_DRUM_KIT		 0x08
+#define SUBTYPE_GUITAR_BASS		 0x0B
+#define SUBTYPE_RB_KEYBOARD		 0x0F
+#define SUBTYPE_ARCADE_PAD		 0x13
+#define SUBTYPE_TURNTABLE		 0x17
+#define SUBTYPE_PRO_GUITAR		 0x19
+
 /* Send power-off packet to xpad360w after holding the mode button for this many
  * seconds
  */
@@ -795,8 +812,13 @@ struct usb_xpad {
 	int xtype;			/* type of xbox device */
 	int packet_type;		/* type of the extended packet */
 	int pad_nr;			/* the order x360 pads were attached */
+	u8 sub_type;
+	u16 flags;
+	u16 wireless_vid;
+	u16 wireless_pid;
+	u16 wireless_version;
 	const char *name;		/* name of the device */
-	struct work_struct work;	/* init/remove device from callback */
+	struct delayed_work work;	/* init/remove device from callback */
 	time64_t mode_btn_down_ts;
 	bool delay_init;		/* init packets should be delayed */
 	bool delayed_init_done;
@@ -807,6 +829,8 @@ static void xpad_deinit_input(struct usb_xpad *xpad);
 static int xpad_start_input(struct usb_xpad *xpad);
 static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num);
 static void xpad360w_poweroff_controller(struct usb_xpad *xpad);
+static int xpad_inquiry_pad_capabilities(struct usb_xpad *xpad);
+
 
 /*
  *	xpad_process_packet
@@ -980,7 +1004,7 @@ static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
 
 static void xpad_presence_work(struct work_struct *work)
 {
-	struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
+	struct usb_xpad *xpad = container_of(work, struct usb_xpad, work.work);
 	int error;
 
 	if (xpad->pad_present) {
@@ -1028,10 +1052,60 @@ static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned cha
 
 		if (xpad->pad_present != present) {
 			xpad->pad_present = present;
-			schedule_work(&xpad->work);

drivers/input/joystick/xpad.c static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned cha · line 1064

Quoted 10 lines
drivers/input/joystick/xpad.c +10 -0
+			if (present) {
+				/*
+				 * Delay marking device as present, so we can make sure
+				 * we have received all the information from the capabilities
+				 * report. Some devices don't send one, so the delay
+				 * guarantees that these devices are still initialized.
+				 */
+				schedule_delayed_work(&xpad->work, msecs_to_jiffies(500));
+			} else {
+				schedule_delayed_work(&xpad->work, 0);
			schedule_delayed_work(&xpad->work,
					      present ? msecs_to_jiffies(500) : 0);
Quoted 32 lines
drivers/input/joystick/xpad.c +29 -0
+			}
 		}
 	}
 
+	/* Link report */
+	if (data[0] == 0x00 && data[1] == 0x0F) {
+		xpad->sub_type = data[25] & 0x7f;
+
+		/* Decode vendor id from link report */
+		xpad->wireless_vid = ((data[0x16] & 0xf) | data[0x18] << 4) << 8 | data[0x17];
+		/*
+		 * x360w controllers on windows put the subtype into the product
+		 * for wheels and gamepads, but it makes sense to do it for all
+		 * subtypes. This will be used if the capabilities report
+		 * doesn't provide us with a product id later.
+		 */
+		xpad->wireless_pid = 0x02a0 + xpad->sub_type;
+		xpad->wireless_version = 0;
+
+		if ((data[25] & 0x80) != 0)
+			xpad->flags |= FLAG_FORCE_FEEDBACK;
+
+		xpad_inquiry_pad_capabilities(xpad);
+	}
+
+	/* Capabilities report */
+	if (data[0] == 0x00 && data[1] == 0x05 && data[5] == 0x12) {
+		xpad->flags |= data[20];
+		/*
+		 * A bunch of vendors started putting vids and pids
+		 * into capabilities data because they can't be
+		 * retrieved by xinput easliy.

drivers/input/joystick/xpad.c static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned cha · line 1106

Quoted 10 lines
drivers/input/joystick/xpad.c +10 -0
+		 * Not all of them do though, so check the vids match
+		 * before extracting that info.
+		 */
+		if (get_unaligned_le16(data + 10) == xpad->wireless_vid) {
+			xpad->wireless_pid = get_unaligned_le16(data + 12);
+			xpad->wireless_version = get_unaligned_le16(data + 14);
+		}
+		/* We got the capabilities report, so mark the device present now */
+		cancel_delayed_work(&xpad->work);
+		schedule_delayed_work(&xpad->work, 0);
		mod_delayed_work(&xpad->work, 0);
Quoted 38 lines
drivers/input/joystick/xpad.c +27 -0
+	}
+
 	/* Valid pad data */
 	if (data[1] != 0x1)
 		return;
@@ -1495,6 +1569,31 @@ static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
 	return xpad_try_sending_next_out_packet(xpad);
 }
 
+static int xpad_inquiry_pad_capabilities(struct usb_xpad *xpad)
+{
+	struct xpad_output_packet *packet =
+			&xpad->out_packets[XPAD_OUT_CMD_IDX];
+
+	guard(spinlock_irqsave)(&xpad->odata_lock);
+
+	packet->data[0] = 0x00;
+	packet->data[1] = 0x00;
+	packet->data[2] = 0x02;
+	packet->data[3] = 0x80;
+	packet->data[4] = 0x00;
+	packet->data[5] = 0x00;
+	packet->data[6] = 0x00;
+	packet->data[7] = 0x00;
+	packet->data[8] = 0x00;
+	packet->data[9] = 0x00;
+	packet->data[10] = 0x00;
+	packet->data[11] = 0x00;
+	packet->len = 12;
+	packet->pending = true;
+
+	return xpad_try_sending_next_out_packet(xpad);
+}
+
 static int xpad_start_xbox_one(struct usb_xpad *xpad)
 {
 	int error;

drivers/input/joystick/xpad.c static void xpad360w_stop_input(struct usb_xpad *xpad) · line 1995

Quoted 5 lines
drivers/input/joystick/xpad.c +1 -1
@@ -1893,7 +1992,7 @@ static void xpad360w_stop_input(struct usb_xpad *xpad)
 	usb_kill_urb(xpad->irq_in);
 
 	/* Make sure we are done with presence work if it was scheduled */
-	flush_work(&xpad->work);
+	flush_delayed_work(&xpad->work);

Maybe this should be disable_delayed_work() instead...

drivers/input/joystick/xpad.c static int xpad_init_input(struct usb_xpad *xpad) · line 2068

Quoted 11 lines
drivers/input/joystick/xpad.c +2 -2
 }
 
 static int xpad_open(struct input_dev *dev)
@@ -1965,8 +2064,57 @@ static int xpad_init_input(struct usb_xpad *xpad)
 	usb_to_input_id(xpad->udev, &input_dev->id);
 
 	if (xpad->xtype == XTYPE_XBOX360W) {
-		/* x360w controllers and the receiver have different ids */
-		input_dev->id.product = 0x02a1;
+		/* If the Link report has provided a vid, it won't be set to 1 */
+		if (xpad->wireless_vid != 1)

When will it be set to 1?

Thanks.

Signature · 1 line
Dmitry
#2 lore raw
Quoted 1 line · Dmitry Torokhov

When will it be set to 1?

The link report itself sets it to 1 in some cases, but i think i will just change this so that if we get a link report and the vid == 1, we set wireless_vid to 0, so we can rely on it being a non zero value instead

Quoted 1 line

Maybe this should be disable_delayed_work() instead...

For simplicity I'll just get rid of that, and we can just always wait for it to expire instead of trying to shortcut it if we get the capabilities report faster than 500ms.

Quoted 1 line

Can this also read stale data for the same reason if the packet is too short?

I'll add in length checks.

#3 lore raw
Quoted 1 line · Dmitry Torokhov

Sashuko correctly identified issues areoud re-scheduling work that

already completed, please see:

Is there a way to test a patch against sashiko without submitting it to the mailing list?

maintainer
#4 lore raw
Quoted 8 lines · Sanjay Govind
Quoted 1 line · Dmitry Torokhov

Sashuko correctly identified issues areoud re-scheduling work that

already completed, please see:

Is there a way to test a patch against sashiko without submitting it to the mailing list?

I think you'd have to spin up your own instance for that...

Signature · 1 line
Dmitry
#5 lore raw
Quoted 1 line · Dmitry Torokhov

I think you'd have to spin up your own instance for that...

makes sense

#6 lore raw
Quoted 7 lines · Sanjay Govind
Quoted 1 line · Dmitry Torokhov

I think you'd have to spin up your own instance for that...

makes sense

Hi Sanjay,
I have a workflow for running an llm and getting reviews locally. Perhaps I will post it to the mailing list soon. I can cc you. I have been out for the Easter holidays

Of course, you will need your own LLM and harness for that, Claude code/Antigravity/Copilot/Opencode, etc. I know they aren't particularly cheap.

Dmitry, sashiko looks great, I left a star. I could also cc you. I will also reference the prompts used in that as it claims some subsystem specific ones. From a quick glance it seems to might suffer from context bloat though. There are a lot of guideline files that could have higher information density.

Antheas

maintainer
#7 lore raw
Quoted 18 lines · Antheas Kapenekakis
Quoted 7 lines · Sanjay Govind
Quoted 1 line · Dmitry Torokhov

I think you'd have to spin up your own instance for that...

makes sense

Hi Sanjay,
I have a workflow for running an llm and getting reviews locally. Perhaps I will post it to the mailing list soon. I can cc you. I have been out for the Easter holidays

Of course, you will need your own LLM and harness for that, Claude code/Antigravity/Copilot/Opencode, etc. I know they aren't particularly cheap.

Quoted 5 lines

Dmitry, sashiko looks great, I left a star. I could also cc you. I will also reference the prompts used in that as it claims some subsystem specific ones. From a quick glance it seems to might suffer from context bloat though. There are a lot of guideline files that could have higher information density.

I am just a happy user of sashiko, I'll leave it to Roman to comment on the potential bloat concerns.

Thanks.

Signature · 1 line
Dmitry
#8 lore raw
Quoted 23 lines · Dmitry Torokhov
Quoted 18 lines · Antheas Kapenekakis
Quoted 8 lines · Sanjay Govind
Quoted 1 line · Dmitry Torokhov

I think you'd have to spin up your own instance for that...

makes sense

Hi Sanjay,
I have a workflow for running an llm and getting reviews locally. Perhaps I will post it to the mailing list soon. I can cc you. I have been out for the Easter holidays

Of course, you will need your own LLM and harness for that, Claude code/Antigravity/Copilot/Opencode, etc. I know they aren't particularly cheap.

Quoted 8 lines
Quoted 5 lines

Dmitry, sashiko looks great, I left a star. I could also cc you. I will also reference the prompts used in that as it claims some subsystem specific ones. From a quick glance it seems to might suffer from context bloat though. There are a lot of guideline files that could have higher information density.

I am just a happy user of sashiko, I'll leave it to Roman to comment on the potential bloat concerns.

All I can say is Sashiko is an open-source project and I’m happy to take any pull requests (including alternative prompts) which are improving the quality of reviews. I’m not attached to any particular approach to the prompting and happy to change it assuming there is data showing that some other approach is better.

Thanks!