new driver for Valve Steam Controller
Subsystems (2)
v3 cover new driver for Valve Steam Controller
This patchset implements a driver for Valve Steam Controller, based on a reverse analysis by myself.
This is reroll v3, codenamed "lazy lizard". Changes from v2:
* Use RCU to do the dynamic connec/disconnect of wireless devices. Please, to anybody that knows their way around RCU, review.
* Remove entries in hid-quirks.c as they are no longer needed. This allows this module to be blacklisted without side effects.
* Do not bypass the virtual keyboard/mouse HID devices to avoid breaking existing use cases (lizard mode). A user-space tool to do that is linked.
* Fully separated axes for joystick and left-pad. As it happens, there are people with too many fingers.
* Add fuzz values for left/right pad axes, they are a little wiggly.
Notable changes from patchset v1:
* Remove references to USB. Now the interesting interfaces are selected by looking for the ones with feature reports.
* Feature reports buffers are allocated with hid_alloc_report_buf().
* Feature report length is checked, to avoid overflows in case of corrupt/malicius USB devices.
* Resolution added to the ABS axes.
* A lot of minor cleanups.
Rodrigo Rivas Costa (3):
HID: add driver for Valve Steam Controller
HID: steam: add serial number information.
HID: steam: add battery device.
drivers/hid/Kconfig | 8 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 4 + drivers/hid/hid-steam.c | 777 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 790 insertions(+) create mode 100644 drivers/hid/hid-steam.c
Signature · 1 line
2.16.2
PATCH v3 1/3 HID: add driver for Valve Steam Controller
There are two ways to connect the Steam Controller: directly to the USB or with the USB wireless adapter. Both methods are similar, but the wireless adapter can connect up to 4 devices at the same time.
The wired device will appear as 3 interfaces: a virtual mouse, a virtual keyboard and a custom HID device.
The wireless device will appear as 5 interfaces: a virtual keyboard and 4 custom HID devices, that will remain silent until a device is actually connected.
The custom HID device has a report descriptor with all vendor specific usages, so the hid-generic is not very useful. In a PC/SteamBox Valve Steam Client provices a software translation by using direct USB access and a creates a uinput virtual gamepad.
This driver was reverse engineered to provide direct kernel support in case you cannot, or do not want to, use Valve Steam Client. It disables the virtual keyboard and mouse, as they are not so useful when you have a working gamepad.
Working: buttons, axes, pads, wireless connect/disconnect.
TO-DO: Battery, force-feedback, accelerometer/gyro, led, beeper...
--- drivers/hid/Kconfig | 8 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 4 + drivers/hid/hid-steam.c | 539 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 552 insertions(+) create mode 100644 drivers/hid/hid-steam.c
drivers/hid/Kconfig +8 -0
@@ -823,6 +823,14 @@ config HID_SPEEDLINK ---help--- Support for Speedlink Vicious and Divine Cezanne mouse. +config HID_STEAM + tristate "Steam Controller support" + depends on HID + ---help--- + Say Y here if you have a Steam Controller if you want to use it + without running the Steam Client. It supports both the wired and + the wireless adaptor. + config HID_STEELSERIES tristate "Steelseries SRW-S1 steering wheel support" depends on HID
drivers/hid/Makefile +1 -0
@@ -95,6 +95,7 @@ obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o obj-$(CONFIG_HID_SONY) += hid-sony.o obj-$(CONFIG_HID_SPEEDLINK) += hid-speedlink.o +obj-$(CONFIG_HID_STEAM) += hid-steam.o obj-$(CONFIG_HID_STEELSERIES) += hid-steelseries.o obj-$(CONFIG_HID_SUNPLUS) += hid-sunplus.o obj-$(CONFIG_HID_GREENASIA) += hid-gaff.o
drivers/hid/hid-ids.h +4 -0
@@ -988,6 +988,10 @@#define USB_VENDOR_ID_STANTUM_SITRONIX 0x1403#define USB_DEVICE_ID_MTP_SITRONIX 0x5001 +#define USB_VENDOR_ID_VALVE 0x28de +#define USB_DEVICE_ID_STEAM_CONTROLLER 0x1102 +#define USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS 0x1142 +#define USB_VENDOR_ID_STEELSERIES 0x1038#define USB_DEVICE_ID_STEELSERIES_SRWS1 0x1410
drivers/hid/hid-steam.c new file +539 -0
539 lines not on this page · raw message
Signature · 1 line
2.16.2
PATCH v3 2/3 HID: steam: add serial number information.
In reply to Rodrigo Rivas CostaThis device has a feature report to send and receive commands. Use it to get the serial number and set the device's uniq value.
--- drivers/hid/hid-steam.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 3 deletions(-)
drivers/hid/hid-steam.c +102 -3
@@ -17,6 +17,8 @@#include <linux/hid.h> #include <linux/module.h> #include <linux/workqueue.h> +#include <linux/delay.h> +#include <linux/rcupdate.h> #include "hid-ids.h" MODULE_LICENSE ("GPL" );
@@ -40,8 +42,99 @@ struct steam_device {unsigned long quirks;struct work_struct work_connect;bool connected; +char serial_no[11 ]; }; +static int steam_recv_report (struct steam_device *steam, +u8 *data,int size) +{ +struct hid_report *r; +u8 *buf; +int ret; + + r = steam->hdev->report_enum[HID_FEATURE_REPORT ].report_id_hash[0 ]; +if (hid_report_len (r) <64 ) +return -EINVAL ; + buf =hid_alloc_report_buf (r,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +/* +* The report ID is always 0, so strip the first byte from the output. +* hid_report_len() is not counting the report ID, so +1 to the length +* or else we get a EOVERFLOW. We are safe from a buffer overflow +* because hid_alloc_report_buf() allocates +7 bytes. +*/ + ret =hid_hw_raw_request (steam->hdev,0x00 , + buf,hid_report_len (r) +1 , +HID_FEATURE_REPORT ,HID_REQ_GET_REPORT ); +if (ret >0 ) +memcpy (data, buf +1 ,min (size, ret -1 )); +kfree (buf); +return ret; +} + +static int steam_send_report (struct steam_device *steam, +u8 *cmd,int size) +{ +struct hid_report *r; +u8 *buf; +int retry; +int ret; + + r = steam->hdev->report_enum[HID_FEATURE_REPORT ].report_id_hash[0 ]; +if (hid_report_len (r) <64 ) +return -EINVAL ; + buf =hid_alloc_report_buf (r,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +/* The report ID is always 0 */ +memcpy (buf +1 , cmd, size); + +/* +* Sometimes the wireless controller fails with EPIPE +* when sending a feature report. +* Doing a HID_REQ_GET_REPORT and waiting for a while +* seems to fix that. +*/ +for (retry =0 ; retry <10 ; ++retry) { + ret =hid_hw_raw_request (steam->hdev,0 , + buf, size +1 , +HID_FEATURE_REPORT ,HID_REQ_SET_REPORT ); +if (ret != -EPIPE ) +break ; +steam_recv_report (steam,NULL ,0 ); +msleep (50 ); + } +kfree (buf); +if (ret <0 ) +hid_err (steam->hdev,"%s: error %d (%*ph)\n" , __func__, + ret, size, cmd); +return ret; +} + +static int steam_get_serial (struct steam_device *steam) +{ +/* +* Send: 0xae 0x15 0x01 +* Recv: 0xae 0x15 0x01 serialnumber (10 chars) +*/ +int ret; +u8 cmd[] = {0xae ,0x15 ,0x01 }; +u8 reply[14 ]; + + ret =steam_send_report (steam, cmd,sizeof (cmd)); +if (ret <0 ) +return ret; + ret =steam_recv_report (steam, reply,sizeof (reply)); +if (ret <0 ) +return ret; + reply[13 ] =0 ; +strcpy (steam->serial_no, reply +3 ); +return 0 ; +} +static int steam_input_open (struct input_dev *dev) {struct steam_device *steam =input_get_drvdata (dev);
@@ -70,7 +163,12 @@ static int steam_register(struct steam_device *steam)return -ENODEV ; } -hid_info (hdev,"Steam Controller connected" ); + ret =steam_get_serial (steam); +if (ret) +return ret; + +hid_info (hdev,"Steam Controller '%s' connected" , + steam->serial_no); input =input_allocate_device ();if (!input)
@@ -85,7 +183,7 @@ static int steam_register(struct steam_device *steam) "Wireless Steam Controller" :"Steam Controller" ; input->phys = hdev->phys; - input->uniq = hdev->uniq; + input->uniq = steam->serial_no; input->id.bustype = hdev->bus; input->id.vendor = hdev->vendor; input->id.product = hdev->product;
@@ -154,7 +252,8 @@ static void steam_unregister(struct steam_device *steam)if (input) {RCU_INIT_POINTER (steam->input,NULL );synchronize_rcu (); -hid_info (steam->hdev,"Steam Controller disconnected" ); +hid_info (steam->hdev,"Steam Controller '%s' disconnected" , + steam->serial_no);input_unregister_device (input); } }
Signature · 1 line
2.16.2
PATCH v3 3/3 HID: steam: add battery device.
In reply to Rodrigo Rivas CostaThe wireless Steam Controller is battery operated, so add the battery device and power information.
--- drivers/hid/hid-steam.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 1 deletion(-)
drivers/hid/hid-steam.c +140 -1
@@ -19,6 +19,7 @@#include <linux/workqueue.h> #include <linux/delay.h> #include <linux/rcupdate.h> +#include <linux/power_supply.h> #include "hid-ids.h" MODULE_LICENSE ("GPL" );
@@ -43,6 +44,10 @@ struct steam_device {struct work_struct work_connect;bool connected;char serial_no[11 ]; +struct power_supply_desc battery_desc; +struct power_supply __rcu *battery; +u8 battery_charge; +u16 voltage; }; static int steam_recv_report(struct steam_device *steam,
@@ -149,6 +154,85 @@ static void steam_input_close(struct input_dev *dev)hid_hw_close (steam->hdev); } +static enum power_supply_property steam_battery_props[] = { +POWER_SUPPLY_PROP_PRESENT , +POWER_SUPPLY_PROP_SCOPE , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CAPACITY , +}; + +static int steam_battery_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct steam_device *steam =power_supply_get_drvdata (psy); +unsigned long flags; +s16 volts; +u8 batt; +int ret =0 ; + +spin_lock_irqsave (&steam->lock, flags); + volts = steam->voltage; + batt = steam->battery_charge; +spin_unlock_irqrestore (&steam->lock, flags); + +switch (psp) { +case POWER_SUPPLY_PROP_PRESENT : + val->intval =1 ; +break ; +case POWER_SUPPLY_PROP_SCOPE : + val->intval =POWER_SUPPLY_SCOPE_DEVICE ; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = volts *1000 ;/* mV -> uV */ +break ; +case POWER_SUPPLY_PROP_CAPACITY : + val->intval = batt; +break ; +default : + ret = -EINVAL ; +break ; + } +return ret; +} + +static int steam_battery_register (struct steam_device *steam) +{ +struct power_supply *battery; +struct power_supply_config battery_cfg = { .drv_data = steam, }; +unsigned long flags; +int ret; + + steam->battery_desc.type =POWER_SUPPLY_TYPE_BATTERY ; + steam->battery_desc.properties = steam_battery_props; + steam->battery_desc.num_properties =ARRAY_SIZE (steam_battery_props); + steam->battery_desc.get_property = steam_battery_get_property; + steam->battery_desc.name =devm_kasprintf (&steam->hdev->dev, +GFP_KERNEL ,"steam-controller-%s-battery" , + steam->serial_no); +if (!steam->battery_desc.name) +return -ENOMEM ; + +/* avoid the warning of 0% battery while waiting for the first info */ +spin_lock_irqsave (&steam->lock, flags); + steam->voltage =3000 ; + steam->battery_charge =100 ; +spin_unlock_irqrestore (&steam->lock, flags); + + battery =power_supply_register (&steam->hdev->dev, + &steam->battery_desc, &battery_cfg); +if (IS_ERR (battery)) { + ret =PTR_ERR (battery); +hid_err (steam->hdev, +"%s:power_supply_register failed with error %d\n" , + __func__, ret); +return ret; + } +rcu_assign_pointer (steam->battery, battery); +power_supply_powers (battery, &steam->hdev->dev); +return 0 ; +} +static int steam_register (struct steam_device *steam) {struct hid_device *hdev = steam->hdev;
@@ -234,6 +318,10 @@ static int steam_register(struct steam_device *steam)rcu_assign_pointer (steam->input, input); +/* ignore battery errors, we can live without it */ +if (steam->quirks &STEAM_QUIRK_WIRELESS ) +steam_battery_register (steam); +return 0 ; input_register_fail:
@@ -244,11 +332,18 @@ static int steam_register(struct steam_device *steam)static void steam_unregister (struct steam_device *steam) {struct input_dev *input; +struct power_supply *battery;rcu_read_lock (); input =rcu_dereference (steam->input); + battery =rcu_dereference (steam->battery);rcu_read_unlock (); +if (battery) { +RCU_INIT_POINTER (steam->battery,NULL ); +synchronize_rcu (); +power_supply_unregister (battery); + }if (input) {RCU_INIT_POINTER (steam->input,NULL );synchronize_rcu ();
@@ -548,12 +643,44 @@ static void steam_do_input_event(struct steam_device *steam,input_sync (input); } +/* +* The size for this message payload is 11. +* The known values are: +* Offset| Type | Meaning +* -------+-------+--------------------------- +* 4-7 | u32 | sequence number +* 8-11 | -- | always 0 +* 12-13 | u16 | voltage (mV) +* 14 | u8 | battery percent +*/ +static void steam_do_battery_event (struct steam_device *steam, +struct power_supply *battery,u8 *data) +{ +unsigned long flags; + +s16 volts =le16_to_cpup ((__le16 *)(data +12 )); +u8 batt = data[14 ]; + +/* Creating the battery may have failed */ +rcu_read_lock (); + battery =rcu_dereference (steam->battery); +if (likely (battery)) { +spin_lock_irqsave (&steam->lock, flags); + steam->voltage = volts; + steam->battery_charge = batt; +spin_unlock_irqrestore (&steam->lock, flags); +power_supply_changed (battery); + } +rcu_read_unlock (); +} +static int steam_raw_event (struct hid_device *hdev,struct hid_report *report,u8 *data,int size) {struct steam_device *steam =hid_get_drvdata (hdev);struct input_dev *input; +struct power_supply *battery;if (!steam)return 0 ;
@@ -606,7 +733,19 @@ static int steam_raw_event(struct hid_device *hdev, }break ;case 0x04 : -/* TODO battery status */ +if (steam->quirks &STEAM_QUIRK_WIRELESS ) { +rcu_read_lock (); + battery =rcu_dereference (steam->battery); +if (likely (battery)) { +steam_do_battery_event (steam, battery, data); + }else { +dbg_hid ( +"%s: battery data without connect event\n" , + __func__); +steam_do_connect_event (steam, true); + } +rcu_read_unlock (); + }break ; }return 0 ;
Signature · 1 line
2.16.2
Hi Rodrigo,
Quoted 2 lines · Rodrigo Rivas Costa
This patchset implements a driver for Valve Steam Controller, based on a reverse analysis by myself.
To me, the code looks OK now. I haven't got the time to do a better review, so giving my:
However, before we include it, I'd like to have the ACK from Pierre-Loup and Clément. They should be more qualified than me to say if this will interfere with the official Steam client (I think we are good now, but that's just my opinion).
Cheers,
Benjamin
Quoted 37 lines
This is reroll v3, codenamed "lazy lizard". Changes from v2:
* Use RCU to do the dynamic connec/disconnect of wireless devices. Please, to anybody that knows their way around RCU, review.
* Remove entries inhid-quirks.cas they are no longer needed. This allows this module to be blacklisted without side effects.
* Do not bypass the virtual keyboard/mouse HID devices to avoid breaking existing use cases (lizard mode). A user-space tool to do that is linked.
* Fully separated axes for joystick and left-pad. As it happens, there are people with too many fingers.
* Add fuzz values for left/right pad axes, they are a little wiggly.Notable changes from patchset v1:
* Remove references to USB. Now the interesting interfaces are selected by looking for the ones with feature reports.
* Feature reports buffers are allocated withhid_alloc_report_buf().
* Feature report length is checked, to avoid overflows in case of corrupt/malicius USB devices.
* Resolution added to the ABS axes.
* A lot of minor cleanups.Rodrigo Rivas Costa (3):
HID: add driver for Valve Steam Controller
HID: steam: add serial number information.
HID: steam: add battery device.drivers/hid/Kconfig | 8 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 4 + drivers/hid/hid-steam.c | 777 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 790 insertions(+) create mode 100644 drivers/hid/hid-steam.c--
2.16.2
2018-02-26 10:50 GMT+01:00 Benjamin Tissoires <[email protected]>:
Quoted 7 lines
Hi Rodrigo,
Quoted 2 lines · Rodrigo Rivas Costa
This patchset implements a driver for Valve Steam Controller, based on a reverse analysis by myself.
Quoted 8 lines
To me, the code looks OK now. I haven't got the time to do a better review, so giving my:
However, before we include it, I'd like to have the ACK from Pierre-Loup and Clément. They should be more qualified than me to say if this will interfere with the official Steam client (I think we are good now, but that's just my opinion).
I am not qualified to speak about the steam client, it has been a long time since I have a look at how it uses the controller and I think it changed a lot.
I checked the code and I think you are not handling wireless connection correctly. If a wireless controller is already connected when the driver is loaded, you will not receive a connection event and forget to register the controller. You can send a 0xb4 request with empty parameters (0xb4, 0x00, ... ) to force the receiver to send its connection status (as the type 3 event you are already parsing). Since user-space program may also send this request, you should make sure the driver works if it receives a (dis)connected event when it is already (dis)connected.
Quoted 41 lines
Cheers,
BenjaminQuoted 37 lines
This is reroll v3, codenamed "lazy lizard". Changes from v2:
* Use RCU to do the dynamic connec/disconnect of wireless devices. Please, to anybody that knows their way around RCU, review.
* Remove entries inhid-quirks.cas they are no longer needed. This allows this module to be blacklisted without side effects.
* Do not bypass the virtual keyboard/mouse HID devices to avoid breaking existing use cases (lizard mode). A user-space tool to do that is linked.
* Fully separated axes for joystick and left-pad. As it happens, there are people with too many fingers.
* Add fuzz values for left/right pad axes, they are a little wiggly.Notable changes from patchset v1:
* Remove references to USB. Now the interesting interfaces are selected by looking for the ones with feature reports.
* Feature reports buffers are allocated withhid_alloc_report_buf().
* Feature report length is checked, to avoid overflows in case of corrupt/malicius USB devices.
* Resolution added to the ABS axes.
* A lot of minor cleanups.Rodrigo Rivas Costa (3):
HID: add driver for Valve Steam Controller
HID: steam: add serial number information.
HID: steam: add battery device.drivers/hid/Kconfig | 8 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 4 + drivers/hid/hid-steam.c | 777 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 790 insertions(+) create mode 100644 drivers/hid/hid-steam.c--
2.16.2
Quoted 21 lines · Clément VUCHENER
2018-02-26 10:50 GMT+01:00 Benjamin Tissoires <[email protected]>:
Quoted 7 lines
Hi Rodrigo,
Quoted 2 lines · Rodrigo Rivas Costa
This patchset implements a driver for Valve Steam Controller, based on a reverse analysis by myself.
Quoted 8 lines
To me, the code looks OK now. I haven't got the time to do a better review, so giving my:
However, before we include it, I'd like to have the ACK from Pierre-Loup and Clément. They should be more qualified than me to say if this will interfere with the official Steam client (I think we are good now, but that's just my opinion).
I am not qualified to speak about the steam client, it has been a long time since I have a look at how it uses the controller and I think it changed a lot.
Quoted 9 lines
I checked the code and I think you are not handling wireless connection correctly. If a wireless controller is already connected when the driver is loaded, you will not receive a connection event and forget to register the controller. You can send a
0xb4request with empty parameters (0xb4,0x00, ... ) to force the receiver to send its connection status (as the type 3 event you are already parsing). Since user-space program may also send this request, you should make sure the driver works if it receives a (dis)connected event when it is already (dis)connected.
You are partially right. If a wireless controller is already connected when the driver is loaded, it will not create the input device. But eventually it will receive a input or battery report. Then it will know that it is not yet registered and it will do it. Since the wireless device sends one battery status per second, that is not a big deal. That is in patch 3/3, though, without that you will have to touch a button to get the connection.
The redundant disconnection is handled properly, I think. It will just call steam_unregister() several times, but that will do nothing if the input device/battery is not created.
Anyway, I agree that the 0xb4 request is a much better solution, I didn't know about that command. I'll add it to the driver and check it from user-land to see if it breaks anything.
Thanks for the tip!
Rodrigo
Quoted 42 lines
Quoted 42 lines
Cheers,
BenjaminQuoted 37 lines
This is reroll v3, codenamed "lazy lizard". Changes from v2:
* Use RCU to do the dynamic connec/disconnect of wireless devices. Please, to anybody that knows their way around RCU, review.
* Remove entries inhid-quirks.cas they are no longer needed. This allows this module to be blacklisted without side effects.
* Do not bypass the virtual keyboard/mouse HID devices to avoid breaking existing use cases (lizard mode). A user-space tool to do that is linked.
* Fully separated axes for joystick and left-pad. As it happens, there are people with too many fingers.
* Add fuzz values for left/right pad axes, they are a little wiggly.Notable changes from patchset v1:
* Remove references to USB. Now the interesting interfaces are selected by looking for the ones with feature reports.
* Feature reports buffers are allocated withhid_alloc_report_buf().
* Feature report length is checked, to avoid overflows in case of corrupt/malicius USB devices.
* Resolution added to the ABS axes.
* A lot of minor cleanups.Rodrigo Rivas Costa (3):
HID: add driver for Valve Steam Controller
HID: steam: add serial number information.
HID: steam: add battery device.drivers/hid/Kconfig | 8 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 4 + drivers/hid/hid-steam.c | 777 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 790 insertions(+) create mode 100644 drivers/hid/hid-steam.c--
2.16.2