18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * HID driver for Valve Steam Controller
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2018 Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Supports both the wired and wireless interfaces.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This controller has a builtin emulation of mouse and keyboard: the right pad
108c2ecf20Sopenharmony_ci * can be used as a mouse, the shoulder buttons are mouse buttons, A and B
118c2ecf20Sopenharmony_ci * buttons are ENTER and ESCAPE, and so on. This is implemented as additional
128c2ecf20Sopenharmony_ci * HID interfaces.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * This is known as the "lizard mode", because apparently lizards like to use
158c2ecf20Sopenharmony_ci * the computer from the coach, without a proper mouse and keyboard.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * This driver will disable the lizard mode when the input device is opened
188c2ecf20Sopenharmony_ci * and re-enable it when the input device is closed, so as not to break user
198c2ecf20Sopenharmony_ci * mode behaviour. The lizard_mode parameter can be used to change that.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * There are a few user space applications (notably Steam Client) that use
228c2ecf20Sopenharmony_ci * the hidraw interface directly to create input devices (XTest, uinput...).
238c2ecf20Sopenharmony_ci * In order to avoid breaking them this driver creates a layered hidraw device,
248c2ecf20Sopenharmony_ci * so it can detect when the client is running and then:
258c2ecf20Sopenharmony_ci *  - it will not send any command to the controller.
268c2ecf20Sopenharmony_ci *  - this input device will be removed, to avoid double input of the same
278c2ecf20Sopenharmony_ci *    user action.
288c2ecf20Sopenharmony_ci * When the client is closed, this input device will be created again.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * For additional functions, such as changing the right-pad margin or switching
318c2ecf20Sopenharmony_ci * the led, you can use the user-space tool at:
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci *   https://github.com/rodrigorc/steamctrl
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <linux/device.h>
378c2ecf20Sopenharmony_ci#include <linux/input.h>
388c2ecf20Sopenharmony_ci#include <linux/hid.h>
398c2ecf20Sopenharmony_ci#include <linux/module.h>
408c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
418c2ecf20Sopenharmony_ci#include <linux/mutex.h>
428c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
438c2ecf20Sopenharmony_ci#include <linux/delay.h>
448c2ecf20Sopenharmony_ci#include <linux/power_supply.h>
458c2ecf20Sopenharmony_ci#include "hid-ids.h"
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
488c2ecf20Sopenharmony_ciMODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>");
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic bool lizard_mode = true;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(steam_devices_lock);
538c2ecf20Sopenharmony_cistatic LIST_HEAD(steam_devices);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define STEAM_QUIRK_WIRELESS		BIT(0)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* Touch pads are 40 mm in diameter and 65535 units */
588c2ecf20Sopenharmony_ci#define STEAM_PAD_RESOLUTION 1638
598c2ecf20Sopenharmony_ci/* Trigger runs are about 5 mm and 256 units */
608c2ecf20Sopenharmony_ci#define STEAM_TRIGGER_RESOLUTION 51
618c2ecf20Sopenharmony_ci/* Joystick runs are about 5 mm and 256 units */
628c2ecf20Sopenharmony_ci#define STEAM_JOYSTICK_RESOLUTION 51
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define STEAM_PAD_FUZZ 256
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/*
678c2ecf20Sopenharmony_ci * Commands that can be sent in a feature report.
688c2ecf20Sopenharmony_ci * Thanks to Valve for some valuable hints.
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_ci#define STEAM_CMD_SET_MAPPINGS		0x80
718c2ecf20Sopenharmony_ci#define STEAM_CMD_CLEAR_MAPPINGS	0x81
728c2ecf20Sopenharmony_ci#define STEAM_CMD_GET_MAPPINGS		0x82
738c2ecf20Sopenharmony_ci#define STEAM_CMD_GET_ATTRIB		0x83
748c2ecf20Sopenharmony_ci#define STEAM_CMD_GET_ATTRIB_LABEL	0x84
758c2ecf20Sopenharmony_ci#define STEAM_CMD_DEFAULT_MAPPINGS	0x85
768c2ecf20Sopenharmony_ci#define STEAM_CMD_FACTORY_RESET		0x86
778c2ecf20Sopenharmony_ci#define STEAM_CMD_WRITE_REGISTER	0x87
788c2ecf20Sopenharmony_ci#define STEAM_CMD_CLEAR_REGISTER	0x88
798c2ecf20Sopenharmony_ci#define STEAM_CMD_READ_REGISTER		0x89
808c2ecf20Sopenharmony_ci#define STEAM_CMD_GET_REGISTER_LABEL	0x8a
818c2ecf20Sopenharmony_ci#define STEAM_CMD_GET_REGISTER_MAX	0x8b
828c2ecf20Sopenharmony_ci#define STEAM_CMD_GET_REGISTER_DEFAULT	0x8c
838c2ecf20Sopenharmony_ci#define STEAM_CMD_SET_MODE		0x8d
848c2ecf20Sopenharmony_ci#define STEAM_CMD_DEFAULT_MOUSE		0x8e
858c2ecf20Sopenharmony_ci#define STEAM_CMD_FORCEFEEDBAK		0x8f
868c2ecf20Sopenharmony_ci#define STEAM_CMD_REQUEST_COMM_STATUS	0xb4
878c2ecf20Sopenharmony_ci#define STEAM_CMD_GET_SERIAL		0xae
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* Some useful register ids */
908c2ecf20Sopenharmony_ci#define STEAM_REG_LPAD_MODE		0x07
918c2ecf20Sopenharmony_ci#define STEAM_REG_RPAD_MODE		0x08
928c2ecf20Sopenharmony_ci#define STEAM_REG_RPAD_MARGIN		0x18
938c2ecf20Sopenharmony_ci#define STEAM_REG_LED			0x2d
948c2ecf20Sopenharmony_ci#define STEAM_REG_GYRO_MODE		0x30
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* Raw event identifiers */
978c2ecf20Sopenharmony_ci#define STEAM_EV_INPUT_DATA		0x01
988c2ecf20Sopenharmony_ci#define STEAM_EV_CONNECT		0x03
998c2ecf20Sopenharmony_ci#define STEAM_EV_BATTERY		0x04
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* Values for GYRO_MODE (bitmask) */
1028c2ecf20Sopenharmony_ci#define STEAM_GYRO_MODE_OFF		0x0000
1038c2ecf20Sopenharmony_ci#define STEAM_GYRO_MODE_STEERING	0x0001
1048c2ecf20Sopenharmony_ci#define STEAM_GYRO_MODE_TILT		0x0002
1058c2ecf20Sopenharmony_ci#define STEAM_GYRO_MODE_SEND_ORIENTATION	0x0004
1068c2ecf20Sopenharmony_ci#define STEAM_GYRO_MODE_SEND_RAW_ACCEL		0x0008
1078c2ecf20Sopenharmony_ci#define STEAM_GYRO_MODE_SEND_RAW_GYRO		0x0010
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* Other random constants */
1108c2ecf20Sopenharmony_ci#define STEAM_SERIAL_LEN 10
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistruct steam_device {
1138c2ecf20Sopenharmony_ci	struct list_head list;
1148c2ecf20Sopenharmony_ci	spinlock_t lock;
1158c2ecf20Sopenharmony_ci	struct hid_device *hdev, *client_hdev;
1168c2ecf20Sopenharmony_ci	struct mutex mutex;
1178c2ecf20Sopenharmony_ci	bool client_opened;
1188c2ecf20Sopenharmony_ci	struct input_dev __rcu *input;
1198c2ecf20Sopenharmony_ci	unsigned long quirks;
1208c2ecf20Sopenharmony_ci	struct work_struct work_connect;
1218c2ecf20Sopenharmony_ci	bool connected;
1228c2ecf20Sopenharmony_ci	char serial_no[STEAM_SERIAL_LEN + 1];
1238c2ecf20Sopenharmony_ci	struct power_supply_desc battery_desc;
1248c2ecf20Sopenharmony_ci	struct power_supply __rcu *battery;
1258c2ecf20Sopenharmony_ci	u8 battery_charge;
1268c2ecf20Sopenharmony_ci	u16 voltage;
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic int steam_recv_report(struct steam_device *steam,
1308c2ecf20Sopenharmony_ci		u8 *data, int size)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct hid_report *r;
1338c2ecf20Sopenharmony_ci	u8 *buf;
1348c2ecf20Sopenharmony_ci	int ret;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
1378c2ecf20Sopenharmony_ci	if (!r) {
1388c2ecf20Sopenharmony_ci		hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted -  nothing to read\n");
1398c2ecf20Sopenharmony_ci		return -EINVAL;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (hid_report_len(r) < 64)
1438c2ecf20Sopenharmony_ci		return -EINVAL;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	buf = hid_alloc_report_buf(r, GFP_KERNEL);
1468c2ecf20Sopenharmony_ci	if (!buf)
1478c2ecf20Sopenharmony_ci		return -ENOMEM;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/*
1508c2ecf20Sopenharmony_ci	 * The report ID is always 0, so strip the first byte from the output.
1518c2ecf20Sopenharmony_ci	 * hid_report_len() is not counting the report ID, so +1 to the length
1528c2ecf20Sopenharmony_ci	 * or else we get a EOVERFLOW. We are safe from a buffer overflow
1538c2ecf20Sopenharmony_ci	 * because hid_alloc_report_buf() allocates +7 bytes.
1548c2ecf20Sopenharmony_ci	 */
1558c2ecf20Sopenharmony_ci	ret = hid_hw_raw_request(steam->hdev, 0x00,
1568c2ecf20Sopenharmony_ci			buf, hid_report_len(r) + 1,
1578c2ecf20Sopenharmony_ci			HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1588c2ecf20Sopenharmony_ci	if (ret > 0)
1598c2ecf20Sopenharmony_ci		memcpy(data, buf + 1, min(size, ret - 1));
1608c2ecf20Sopenharmony_ci	kfree(buf);
1618c2ecf20Sopenharmony_ci	return ret;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int steam_send_report(struct steam_device *steam,
1658c2ecf20Sopenharmony_ci		u8 *cmd, int size)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct hid_report *r;
1688c2ecf20Sopenharmony_ci	u8 *buf;
1698c2ecf20Sopenharmony_ci	unsigned int retries = 50;
1708c2ecf20Sopenharmony_ci	int ret;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
1738c2ecf20Sopenharmony_ci	if (!r) {
1748c2ecf20Sopenharmony_ci		hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted -  nothing to read\n");
1758c2ecf20Sopenharmony_ci		return -EINVAL;
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (hid_report_len(r) < 64)
1798c2ecf20Sopenharmony_ci		return -EINVAL;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	buf = hid_alloc_report_buf(r, GFP_KERNEL);
1828c2ecf20Sopenharmony_ci	if (!buf)
1838c2ecf20Sopenharmony_ci		return -ENOMEM;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* The report ID is always 0 */
1868c2ecf20Sopenharmony_ci	memcpy(buf + 1, cmd, size);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/*
1898c2ecf20Sopenharmony_ci	 * Sometimes the wireless controller fails with EPIPE
1908c2ecf20Sopenharmony_ci	 * when sending a feature report.
1918c2ecf20Sopenharmony_ci	 * Doing a HID_REQ_GET_REPORT and waiting for a while
1928c2ecf20Sopenharmony_ci	 * seems to fix that.
1938c2ecf20Sopenharmony_ci	 */
1948c2ecf20Sopenharmony_ci	do {
1958c2ecf20Sopenharmony_ci		ret = hid_hw_raw_request(steam->hdev, 0,
1968c2ecf20Sopenharmony_ci				buf, size + 1,
1978c2ecf20Sopenharmony_ci				HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
1988c2ecf20Sopenharmony_ci		if (ret != -EPIPE)
1998c2ecf20Sopenharmony_ci			break;
2008c2ecf20Sopenharmony_ci		msleep(20);
2018c2ecf20Sopenharmony_ci	} while (--retries);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	kfree(buf);
2048c2ecf20Sopenharmony_ci	if (ret < 0)
2058c2ecf20Sopenharmony_ci		hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
2068c2ecf20Sopenharmony_ci				ret, size, cmd);
2078c2ecf20Sopenharmony_ci	return ret;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	return steam_send_report(steam, &cmd, 1);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic int steam_write_registers(struct steam_device *steam,
2168c2ecf20Sopenharmony_ci		/* u8 reg, u16 val */...)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	/* Send: 0x87 len (reg valLo valHi)* */
2198c2ecf20Sopenharmony_ci	u8 reg;
2208c2ecf20Sopenharmony_ci	u16 val;
2218c2ecf20Sopenharmony_ci	u8 cmd[64] = {STEAM_CMD_WRITE_REGISTER, 0x00};
2228c2ecf20Sopenharmony_ci	va_list args;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	va_start(args, steam);
2258c2ecf20Sopenharmony_ci	for (;;) {
2268c2ecf20Sopenharmony_ci		reg = va_arg(args, int);
2278c2ecf20Sopenharmony_ci		if (reg == 0)
2288c2ecf20Sopenharmony_ci			break;
2298c2ecf20Sopenharmony_ci		val = va_arg(args, int);
2308c2ecf20Sopenharmony_ci		cmd[cmd[1] + 2] = reg;
2318c2ecf20Sopenharmony_ci		cmd[cmd[1] + 3] = val & 0xff;
2328c2ecf20Sopenharmony_ci		cmd[cmd[1] + 4] = val >> 8;
2338c2ecf20Sopenharmony_ci		cmd[1] += 3;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci	va_end(args);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	return steam_send_report(steam, cmd, 2 + cmd[1]);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic int steam_get_serial(struct steam_device *steam)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	/*
2438c2ecf20Sopenharmony_ci	 * Send: 0xae 0x15 0x01
2448c2ecf20Sopenharmony_ci	 * Recv: 0xae 0x15 0x01 serialnumber (10 chars)
2458c2ecf20Sopenharmony_ci	 */
2468c2ecf20Sopenharmony_ci	int ret;
2478c2ecf20Sopenharmony_ci	u8 cmd[] = {STEAM_CMD_GET_SERIAL, 0x15, 0x01};
2488c2ecf20Sopenharmony_ci	u8 reply[3 + STEAM_SERIAL_LEN + 1];
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	ret = steam_send_report(steam, cmd, sizeof(cmd));
2518c2ecf20Sopenharmony_ci	if (ret < 0)
2528c2ecf20Sopenharmony_ci		return ret;
2538c2ecf20Sopenharmony_ci	ret = steam_recv_report(steam, reply, sizeof(reply));
2548c2ecf20Sopenharmony_ci	if (ret < 0)
2558c2ecf20Sopenharmony_ci		return ret;
2568c2ecf20Sopenharmony_ci	if (reply[0] != 0xae || reply[1] != 0x15 || reply[2] != 0x01)
2578c2ecf20Sopenharmony_ci		return -EIO;
2588c2ecf20Sopenharmony_ci	reply[3 + STEAM_SERIAL_LEN] = 0;
2598c2ecf20Sopenharmony_ci	strlcpy(steam->serial_no, reply + 3, sizeof(steam->serial_no));
2608c2ecf20Sopenharmony_ci	return 0;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/*
2648c2ecf20Sopenharmony_ci * This command requests the wireless adaptor to post an event
2658c2ecf20Sopenharmony_ci * with the connection status. Useful if this driver is loaded when
2668c2ecf20Sopenharmony_ci * the controller is already connected.
2678c2ecf20Sopenharmony_ci */
2688c2ecf20Sopenharmony_cistatic inline int steam_request_conn_status(struct steam_device *steam)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	return steam_send_report_byte(steam, STEAM_CMD_REQUEST_COMM_STATUS);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void steam_set_lizard_mode(struct steam_device *steam, bool enable)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	if (enable) {
2768c2ecf20Sopenharmony_ci		/* enable esc, enter, cursors */
2778c2ecf20Sopenharmony_ci		steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MAPPINGS);
2788c2ecf20Sopenharmony_ci		/* enable mouse */
2798c2ecf20Sopenharmony_ci		steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MOUSE);
2808c2ecf20Sopenharmony_ci		steam_write_registers(steam,
2818c2ecf20Sopenharmony_ci			STEAM_REG_RPAD_MARGIN, 0x01, /* enable margin */
2828c2ecf20Sopenharmony_ci			0);
2838c2ecf20Sopenharmony_ci	} else {
2848c2ecf20Sopenharmony_ci		/* disable esc, enter, cursor */
2858c2ecf20Sopenharmony_ci		steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
2868c2ecf20Sopenharmony_ci		steam_write_registers(steam,
2878c2ecf20Sopenharmony_ci			STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
2888c2ecf20Sopenharmony_ci			STEAM_REG_RPAD_MARGIN, 0x00, /* disable margin */
2898c2ecf20Sopenharmony_ci			0);
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic int steam_input_open(struct input_dev *dev)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	struct steam_device *steam = input_get_drvdata(dev);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	mutex_lock(&steam->mutex);
2988c2ecf20Sopenharmony_ci	if (!steam->client_opened && lizard_mode)
2998c2ecf20Sopenharmony_ci		steam_set_lizard_mode(steam, false);
3008c2ecf20Sopenharmony_ci	mutex_unlock(&steam->mutex);
3018c2ecf20Sopenharmony_ci	return 0;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic void steam_input_close(struct input_dev *dev)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct steam_device *steam = input_get_drvdata(dev);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	mutex_lock(&steam->mutex);
3098c2ecf20Sopenharmony_ci	if (!steam->client_opened && lizard_mode)
3108c2ecf20Sopenharmony_ci		steam_set_lizard_mode(steam, true);
3118c2ecf20Sopenharmony_ci	mutex_unlock(&steam->mutex);
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic enum power_supply_property steam_battery_props[] = {
3158c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_PRESENT,
3168c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_SCOPE,
3178c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3188c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_CAPACITY,
3198c2ecf20Sopenharmony_ci};
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic int steam_battery_get_property(struct power_supply *psy,
3228c2ecf20Sopenharmony_ci				enum power_supply_property psp,
3238c2ecf20Sopenharmony_ci				union power_supply_propval *val)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	struct steam_device *steam = power_supply_get_drvdata(psy);
3268c2ecf20Sopenharmony_ci	unsigned long flags;
3278c2ecf20Sopenharmony_ci	s16 volts;
3288c2ecf20Sopenharmony_ci	u8 batt;
3298c2ecf20Sopenharmony_ci	int ret = 0;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	spin_lock_irqsave(&steam->lock, flags);
3328c2ecf20Sopenharmony_ci	volts = steam->voltage;
3338c2ecf20Sopenharmony_ci	batt = steam->battery_charge;
3348c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&steam->lock, flags);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	switch (psp) {
3378c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_PRESENT:
3388c2ecf20Sopenharmony_ci		val->intval = 1;
3398c2ecf20Sopenharmony_ci		break;
3408c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_SCOPE:
3418c2ecf20Sopenharmony_ci		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3428c2ecf20Sopenharmony_ci		break;
3438c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3448c2ecf20Sopenharmony_ci		val->intval = volts * 1000; /* mV -> uV */
3458c2ecf20Sopenharmony_ci		break;
3468c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_CAPACITY:
3478c2ecf20Sopenharmony_ci		val->intval = batt;
3488c2ecf20Sopenharmony_ci		break;
3498c2ecf20Sopenharmony_ci	default:
3508c2ecf20Sopenharmony_ci		ret = -EINVAL;
3518c2ecf20Sopenharmony_ci		break;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci	return ret;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic int steam_battery_register(struct steam_device *steam)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	struct power_supply *battery;
3598c2ecf20Sopenharmony_ci	struct power_supply_config battery_cfg = { .drv_data = steam, };
3608c2ecf20Sopenharmony_ci	unsigned long flags;
3618c2ecf20Sopenharmony_ci	int ret;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
3648c2ecf20Sopenharmony_ci	steam->battery_desc.properties = steam_battery_props;
3658c2ecf20Sopenharmony_ci	steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
3668c2ecf20Sopenharmony_ci	steam->battery_desc.get_property = steam_battery_get_property;
3678c2ecf20Sopenharmony_ci	steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
3688c2ecf20Sopenharmony_ci			GFP_KERNEL, "steam-controller-%s-battery",
3698c2ecf20Sopenharmony_ci			steam->serial_no);
3708c2ecf20Sopenharmony_ci	if (!steam->battery_desc.name)
3718c2ecf20Sopenharmony_ci		return -ENOMEM;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	/* avoid the warning of 0% battery while waiting for the first info */
3748c2ecf20Sopenharmony_ci	spin_lock_irqsave(&steam->lock, flags);
3758c2ecf20Sopenharmony_ci	steam->voltage = 3000;
3768c2ecf20Sopenharmony_ci	steam->battery_charge = 100;
3778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&steam->lock, flags);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	battery = power_supply_register(&steam->hdev->dev,
3808c2ecf20Sopenharmony_ci			&steam->battery_desc, &battery_cfg);
3818c2ecf20Sopenharmony_ci	if (IS_ERR(battery)) {
3828c2ecf20Sopenharmony_ci		ret = PTR_ERR(battery);
3838c2ecf20Sopenharmony_ci		hid_err(steam->hdev,
3848c2ecf20Sopenharmony_ci				"%s:power_supply_register failed with error %d\n",
3858c2ecf20Sopenharmony_ci				__func__, ret);
3868c2ecf20Sopenharmony_ci		return ret;
3878c2ecf20Sopenharmony_ci	}
3888c2ecf20Sopenharmony_ci	rcu_assign_pointer(steam->battery, battery);
3898c2ecf20Sopenharmony_ci	power_supply_powers(battery, &steam->hdev->dev);
3908c2ecf20Sopenharmony_ci	return 0;
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cistatic int steam_input_register(struct steam_device *steam)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct hid_device *hdev = steam->hdev;
3968c2ecf20Sopenharmony_ci	struct input_dev *input;
3978c2ecf20Sopenharmony_ci	int ret;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	rcu_read_lock();
4008c2ecf20Sopenharmony_ci	input = rcu_dereference(steam->input);
4018c2ecf20Sopenharmony_ci	rcu_read_unlock();
4028c2ecf20Sopenharmony_ci	if (input) {
4038c2ecf20Sopenharmony_ci		dbg_hid("%s: already connected\n", __func__);
4048c2ecf20Sopenharmony_ci		return 0;
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	input = input_allocate_device();
4088c2ecf20Sopenharmony_ci	if (!input)
4098c2ecf20Sopenharmony_ci		return -ENOMEM;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	input_set_drvdata(input, steam);
4128c2ecf20Sopenharmony_ci	input->dev.parent = &hdev->dev;
4138c2ecf20Sopenharmony_ci	input->open = steam_input_open;
4148c2ecf20Sopenharmony_ci	input->close = steam_input_close;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ?
4178c2ecf20Sopenharmony_ci		"Wireless Steam Controller" :
4188c2ecf20Sopenharmony_ci		"Steam Controller";
4198c2ecf20Sopenharmony_ci	input->phys = hdev->phys;
4208c2ecf20Sopenharmony_ci	input->uniq = steam->serial_no;
4218c2ecf20Sopenharmony_ci	input->id.bustype = hdev->bus;
4228c2ecf20Sopenharmony_ci	input->id.vendor = hdev->vendor;
4238c2ecf20Sopenharmony_ci	input->id.product = hdev->product;
4248c2ecf20Sopenharmony_ci	input->id.version = hdev->version;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_TR2);
4278c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_TL2);
4288c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_TR);
4298c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_TL);
4308c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_Y);
4318c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_B);
4328c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_X);
4338c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_A);
4348c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_DPAD_UP);
4358c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
4368c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
4378c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
4388c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_SELECT);
4398c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_MODE);
4408c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_START);
4418c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
4428c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_GEAR_UP);
4438c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_THUMBR);
4448c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_THUMBL);
4458c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_THUMB);
4468c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, BTN_THUMB2);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
4498c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
4508c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
4518c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
4528c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_RX, -32767, 32767,
4538c2ecf20Sopenharmony_ci			STEAM_PAD_FUZZ, 0);
4548c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_RY, -32767, 32767,
4558c2ecf20Sopenharmony_ci			STEAM_PAD_FUZZ, 0);
4568c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
4578c2ecf20Sopenharmony_ci			STEAM_PAD_FUZZ, 0);
4588c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
4598c2ecf20Sopenharmony_ci			STEAM_PAD_FUZZ, 0);
4608c2ecf20Sopenharmony_ci	input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
4618c2ecf20Sopenharmony_ci	input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
4628c2ecf20Sopenharmony_ci	input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
4638c2ecf20Sopenharmony_ci	input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
4648c2ecf20Sopenharmony_ci	input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
4658c2ecf20Sopenharmony_ci	input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
4668c2ecf20Sopenharmony_ci	input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
4678c2ecf20Sopenharmony_ci	input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	ret = input_register_device(input);
4708c2ecf20Sopenharmony_ci	if (ret)
4718c2ecf20Sopenharmony_ci		goto input_register_fail;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	rcu_assign_pointer(steam->input, input);
4748c2ecf20Sopenharmony_ci	return 0;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ciinput_register_fail:
4778c2ecf20Sopenharmony_ci	input_free_device(input);
4788c2ecf20Sopenharmony_ci	return ret;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic void steam_input_unregister(struct steam_device *steam)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	struct input_dev *input;
4848c2ecf20Sopenharmony_ci	rcu_read_lock();
4858c2ecf20Sopenharmony_ci	input = rcu_dereference(steam->input);
4868c2ecf20Sopenharmony_ci	rcu_read_unlock();
4878c2ecf20Sopenharmony_ci	if (!input)
4888c2ecf20Sopenharmony_ci		return;
4898c2ecf20Sopenharmony_ci	RCU_INIT_POINTER(steam->input, NULL);
4908c2ecf20Sopenharmony_ci	synchronize_rcu();
4918c2ecf20Sopenharmony_ci	input_unregister_device(input);
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic void steam_battery_unregister(struct steam_device *steam)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	struct power_supply *battery;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	rcu_read_lock();
4998c2ecf20Sopenharmony_ci	battery = rcu_dereference(steam->battery);
5008c2ecf20Sopenharmony_ci	rcu_read_unlock();
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	if (!battery)
5038c2ecf20Sopenharmony_ci		return;
5048c2ecf20Sopenharmony_ci	RCU_INIT_POINTER(steam->battery, NULL);
5058c2ecf20Sopenharmony_ci	synchronize_rcu();
5068c2ecf20Sopenharmony_ci	power_supply_unregister(battery);
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_cistatic int steam_register(struct steam_device *steam)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	int ret;
5128c2ecf20Sopenharmony_ci	bool client_opened;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	/*
5158c2ecf20Sopenharmony_ci	 * This function can be called several times in a row with the
5168c2ecf20Sopenharmony_ci	 * wireless adaptor, without steam_unregister() between them, because
5178c2ecf20Sopenharmony_ci	 * another client send a get_connection_status command, for example.
5188c2ecf20Sopenharmony_ci	 * The battery and serial number are set just once per device.
5198c2ecf20Sopenharmony_ci	 */
5208c2ecf20Sopenharmony_ci	if (!steam->serial_no[0]) {
5218c2ecf20Sopenharmony_ci		/*
5228c2ecf20Sopenharmony_ci		 * Unlikely, but getting the serial could fail, and it is not so
5238c2ecf20Sopenharmony_ci		 * important, so make up a serial number and go on.
5248c2ecf20Sopenharmony_ci		 */
5258c2ecf20Sopenharmony_ci		mutex_lock(&steam->mutex);
5268c2ecf20Sopenharmony_ci		if (steam_get_serial(steam) < 0)
5278c2ecf20Sopenharmony_ci			strlcpy(steam->serial_no, "XXXXXXXXXX",
5288c2ecf20Sopenharmony_ci					sizeof(steam->serial_no));
5298c2ecf20Sopenharmony_ci		mutex_unlock(&steam->mutex);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci		hid_info(steam->hdev, "Steam Controller '%s' connected",
5328c2ecf20Sopenharmony_ci				steam->serial_no);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		/* ignore battery errors, we can live without it */
5358c2ecf20Sopenharmony_ci		if (steam->quirks & STEAM_QUIRK_WIRELESS)
5368c2ecf20Sopenharmony_ci			steam_battery_register(steam);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci		mutex_lock(&steam_devices_lock);
5398c2ecf20Sopenharmony_ci		if (list_empty(&steam->list))
5408c2ecf20Sopenharmony_ci			list_add(&steam->list, &steam_devices);
5418c2ecf20Sopenharmony_ci		mutex_unlock(&steam_devices_lock);
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	mutex_lock(&steam->mutex);
5458c2ecf20Sopenharmony_ci	client_opened = steam->client_opened;
5468c2ecf20Sopenharmony_ci	if (!client_opened)
5478c2ecf20Sopenharmony_ci		steam_set_lizard_mode(steam, lizard_mode);
5488c2ecf20Sopenharmony_ci	mutex_unlock(&steam->mutex);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	if (!client_opened)
5518c2ecf20Sopenharmony_ci		ret = steam_input_register(steam);
5528c2ecf20Sopenharmony_ci	else
5538c2ecf20Sopenharmony_ci		ret = 0;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	return ret;
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic void steam_unregister(struct steam_device *steam)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	steam_battery_unregister(steam);
5618c2ecf20Sopenharmony_ci	steam_input_unregister(steam);
5628c2ecf20Sopenharmony_ci	if (steam->serial_no[0]) {
5638c2ecf20Sopenharmony_ci		hid_info(steam->hdev, "Steam Controller '%s' disconnected",
5648c2ecf20Sopenharmony_ci				steam->serial_no);
5658c2ecf20Sopenharmony_ci		mutex_lock(&steam_devices_lock);
5668c2ecf20Sopenharmony_ci		list_del_init(&steam->list);
5678c2ecf20Sopenharmony_ci		mutex_unlock(&steam_devices_lock);
5688c2ecf20Sopenharmony_ci		steam->serial_no[0] = 0;
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic void steam_work_connect_cb(struct work_struct *work)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	struct steam_device *steam = container_of(work, struct steam_device,
5758c2ecf20Sopenharmony_ci							work_connect);
5768c2ecf20Sopenharmony_ci	unsigned long flags;
5778c2ecf20Sopenharmony_ci	bool connected;
5788c2ecf20Sopenharmony_ci	int ret;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	spin_lock_irqsave(&steam->lock, flags);
5818c2ecf20Sopenharmony_ci	connected = steam->connected;
5828c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&steam->lock, flags);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	if (connected) {
5858c2ecf20Sopenharmony_ci		ret = steam_register(steam);
5868c2ecf20Sopenharmony_ci		if (ret) {
5878c2ecf20Sopenharmony_ci			hid_err(steam->hdev,
5888c2ecf20Sopenharmony_ci				"%s:steam_register failed with error %d\n",
5898c2ecf20Sopenharmony_ci				__func__, ret);
5908c2ecf20Sopenharmony_ci		}
5918c2ecf20Sopenharmony_ci	} else {
5928c2ecf20Sopenharmony_ci		steam_unregister(steam);
5938c2ecf20Sopenharmony_ci	}
5948c2ecf20Sopenharmony_ci}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_cistatic bool steam_is_valve_interface(struct hid_device *hdev)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	struct hid_report_enum *rep_enum;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	/*
6018c2ecf20Sopenharmony_ci	 * The wired device creates 3 interfaces:
6028c2ecf20Sopenharmony_ci	 *  0: emulated mouse.
6038c2ecf20Sopenharmony_ci	 *  1: emulated keyboard.
6048c2ecf20Sopenharmony_ci	 *  2: the real game pad.
6058c2ecf20Sopenharmony_ci	 * The wireless device creates 5 interfaces:
6068c2ecf20Sopenharmony_ci	 *  0: emulated keyboard.
6078c2ecf20Sopenharmony_ci	 *  1-4: slots where up to 4 real game pads will be connected to.
6088c2ecf20Sopenharmony_ci	 * We know which one is the real gamepad interface because they are the
6098c2ecf20Sopenharmony_ci	 * only ones with a feature report.
6108c2ecf20Sopenharmony_ci	 */
6118c2ecf20Sopenharmony_ci	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
6128c2ecf20Sopenharmony_ci	return !list_empty(&rep_enum->report_list);
6138c2ecf20Sopenharmony_ci}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_cistatic int steam_client_ll_parse(struct hid_device *hdev)
6168c2ecf20Sopenharmony_ci{
6178c2ecf20Sopenharmony_ci	struct steam_device *steam = hdev->driver_data;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	return hid_parse_report(hdev, steam->hdev->dev_rdesc,
6208c2ecf20Sopenharmony_ci			steam->hdev->dev_rsize);
6218c2ecf20Sopenharmony_ci}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_cistatic int steam_client_ll_start(struct hid_device *hdev)
6248c2ecf20Sopenharmony_ci{
6258c2ecf20Sopenharmony_ci	return 0;
6268c2ecf20Sopenharmony_ci}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_cistatic void steam_client_ll_stop(struct hid_device *hdev)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic int steam_client_ll_open(struct hid_device *hdev)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	struct steam_device *steam = hdev->driver_data;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	mutex_lock(&steam->mutex);
6378c2ecf20Sopenharmony_ci	steam->client_opened = true;
6388c2ecf20Sopenharmony_ci	mutex_unlock(&steam->mutex);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	steam_input_unregister(steam);
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	return 0;
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_cistatic void steam_client_ll_close(struct hid_device *hdev)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	struct steam_device *steam = hdev->driver_data;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	unsigned long flags;
6508c2ecf20Sopenharmony_ci	bool connected;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	spin_lock_irqsave(&steam->lock, flags);
6538c2ecf20Sopenharmony_ci	connected = steam->connected;
6548c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&steam->lock, flags);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	mutex_lock(&steam->mutex);
6578c2ecf20Sopenharmony_ci	steam->client_opened = false;
6588c2ecf20Sopenharmony_ci	if (connected)
6598c2ecf20Sopenharmony_ci		steam_set_lizard_mode(steam, lizard_mode);
6608c2ecf20Sopenharmony_ci	mutex_unlock(&steam->mutex);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	if (connected)
6638c2ecf20Sopenharmony_ci		steam_input_register(steam);
6648c2ecf20Sopenharmony_ci}
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_cistatic int steam_client_ll_raw_request(struct hid_device *hdev,
6678c2ecf20Sopenharmony_ci				unsigned char reportnum, u8 *buf,
6688c2ecf20Sopenharmony_ci				size_t count, unsigned char report_type,
6698c2ecf20Sopenharmony_ci				int reqtype)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	struct steam_device *steam = hdev->driver_data;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
6748c2ecf20Sopenharmony_ci			report_type, reqtype);
6758c2ecf20Sopenharmony_ci}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_cistatic struct hid_ll_driver steam_client_ll_driver = {
6788c2ecf20Sopenharmony_ci	.parse = steam_client_ll_parse,
6798c2ecf20Sopenharmony_ci	.start = steam_client_ll_start,
6808c2ecf20Sopenharmony_ci	.stop = steam_client_ll_stop,
6818c2ecf20Sopenharmony_ci	.open = steam_client_ll_open,
6828c2ecf20Sopenharmony_ci	.close = steam_client_ll_close,
6838c2ecf20Sopenharmony_ci	.raw_request = steam_client_ll_raw_request,
6848c2ecf20Sopenharmony_ci};
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_cistatic struct hid_device *steam_create_client_hid(struct hid_device *hdev)
6878c2ecf20Sopenharmony_ci{
6888c2ecf20Sopenharmony_ci	struct hid_device *client_hdev;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	client_hdev = hid_allocate_device();
6918c2ecf20Sopenharmony_ci	if (IS_ERR(client_hdev))
6928c2ecf20Sopenharmony_ci		return client_hdev;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	client_hdev->ll_driver = &steam_client_ll_driver;
6958c2ecf20Sopenharmony_ci	client_hdev->dev.parent = hdev->dev.parent;
6968c2ecf20Sopenharmony_ci	client_hdev->bus = hdev->bus;
6978c2ecf20Sopenharmony_ci	client_hdev->vendor = hdev->vendor;
6988c2ecf20Sopenharmony_ci	client_hdev->product = hdev->product;
6998c2ecf20Sopenharmony_ci	client_hdev->version = hdev->version;
7008c2ecf20Sopenharmony_ci	client_hdev->type = hdev->type;
7018c2ecf20Sopenharmony_ci	client_hdev->country = hdev->country;
7028c2ecf20Sopenharmony_ci	strlcpy(client_hdev->name, hdev->name,
7038c2ecf20Sopenharmony_ci			sizeof(client_hdev->name));
7048c2ecf20Sopenharmony_ci	strlcpy(client_hdev->phys, hdev->phys,
7058c2ecf20Sopenharmony_ci			sizeof(client_hdev->phys));
7068c2ecf20Sopenharmony_ci	/*
7078c2ecf20Sopenharmony_ci	 * Since we use the same device info than the real interface to
7088c2ecf20Sopenharmony_ci	 * trick userspace, we will be calling steam_probe recursively.
7098c2ecf20Sopenharmony_ci	 * We need to recognize the client interface somehow.
7108c2ecf20Sopenharmony_ci	 */
7118c2ecf20Sopenharmony_ci	client_hdev->group = HID_GROUP_STEAM;
7128c2ecf20Sopenharmony_ci	return client_hdev;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_cistatic int steam_probe(struct hid_device *hdev,
7168c2ecf20Sopenharmony_ci				const struct hid_device_id *id)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	struct steam_device *steam;
7198c2ecf20Sopenharmony_ci	int ret;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	ret = hid_parse(hdev);
7228c2ecf20Sopenharmony_ci	if (ret) {
7238c2ecf20Sopenharmony_ci		hid_err(hdev,
7248c2ecf20Sopenharmony_ci			"%s:parse of hid interface failed\n", __func__);
7258c2ecf20Sopenharmony_ci		return ret;
7268c2ecf20Sopenharmony_ci	}
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	/*
7298c2ecf20Sopenharmony_ci	 * The virtual client_dev is only used for hidraw.
7308c2ecf20Sopenharmony_ci	 * Also avoid the recursive probe.
7318c2ecf20Sopenharmony_ci	 */
7328c2ecf20Sopenharmony_ci	if (hdev->group == HID_GROUP_STEAM)
7338c2ecf20Sopenharmony_ci		return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
7348c2ecf20Sopenharmony_ci	/*
7358c2ecf20Sopenharmony_ci	 * The non-valve interfaces (mouse and keyboard emulation) are
7368c2ecf20Sopenharmony_ci	 * connected without changes.
7378c2ecf20Sopenharmony_ci	 */
7388c2ecf20Sopenharmony_ci	if (!steam_is_valve_interface(hdev))
7398c2ecf20Sopenharmony_ci		return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
7428c2ecf20Sopenharmony_ci	if (!steam) {
7438c2ecf20Sopenharmony_ci		ret = -ENOMEM;
7448c2ecf20Sopenharmony_ci		goto steam_alloc_fail;
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci	steam->hdev = hdev;
7478c2ecf20Sopenharmony_ci	hid_set_drvdata(hdev, steam);
7488c2ecf20Sopenharmony_ci	spin_lock_init(&steam->lock);
7498c2ecf20Sopenharmony_ci	mutex_init(&steam->mutex);
7508c2ecf20Sopenharmony_ci	steam->quirks = id->driver_data;
7518c2ecf20Sopenharmony_ci	INIT_WORK(&steam->work_connect, steam_work_connect_cb);
7528c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&steam->list);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	steam->client_hdev = steam_create_client_hid(hdev);
7558c2ecf20Sopenharmony_ci	if (IS_ERR(steam->client_hdev)) {
7568c2ecf20Sopenharmony_ci		ret = PTR_ERR(steam->client_hdev);
7578c2ecf20Sopenharmony_ci		goto client_hdev_fail;
7588c2ecf20Sopenharmony_ci	}
7598c2ecf20Sopenharmony_ci	steam->client_hdev->driver_data = steam;
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/*
7628c2ecf20Sopenharmony_ci	 * With the real steam controller interface, do not connect hidraw.
7638c2ecf20Sopenharmony_ci	 * Instead, create the client_hid and connect that.
7648c2ecf20Sopenharmony_ci	 */
7658c2ecf20Sopenharmony_ci	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
7668c2ecf20Sopenharmony_ci	if (ret)
7678c2ecf20Sopenharmony_ci		goto hid_hw_start_fail;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	ret = hid_add_device(steam->client_hdev);
7708c2ecf20Sopenharmony_ci	if (ret)
7718c2ecf20Sopenharmony_ci		goto client_hdev_add_fail;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	ret = hid_hw_open(hdev);
7748c2ecf20Sopenharmony_ci	if (ret) {
7758c2ecf20Sopenharmony_ci		hid_err(hdev,
7768c2ecf20Sopenharmony_ci			"%s:hid_hw_open\n",
7778c2ecf20Sopenharmony_ci			__func__);
7788c2ecf20Sopenharmony_ci		goto hid_hw_open_fail;
7798c2ecf20Sopenharmony_ci	}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	if (steam->quirks & STEAM_QUIRK_WIRELESS) {
7828c2ecf20Sopenharmony_ci		hid_info(hdev, "Steam wireless receiver connected");
7838c2ecf20Sopenharmony_ci		/* If using a wireless adaptor ask for connection status */
7848c2ecf20Sopenharmony_ci		steam->connected = false;
7858c2ecf20Sopenharmony_ci		steam_request_conn_status(steam);
7868c2ecf20Sopenharmony_ci	} else {
7878c2ecf20Sopenharmony_ci		/* A wired connection is always present */
7888c2ecf20Sopenharmony_ci		steam->connected = true;
7898c2ecf20Sopenharmony_ci		ret = steam_register(steam);
7908c2ecf20Sopenharmony_ci		if (ret) {
7918c2ecf20Sopenharmony_ci			hid_err(hdev,
7928c2ecf20Sopenharmony_ci				"%s:steam_register failed with error %d\n",
7938c2ecf20Sopenharmony_ci				__func__, ret);
7948c2ecf20Sopenharmony_ci			goto input_register_fail;
7958c2ecf20Sopenharmony_ci		}
7968c2ecf20Sopenharmony_ci	}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	return 0;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ciinput_register_fail:
8018c2ecf20Sopenharmony_cihid_hw_open_fail:
8028c2ecf20Sopenharmony_ciclient_hdev_add_fail:
8038c2ecf20Sopenharmony_ci	hid_hw_stop(hdev);
8048c2ecf20Sopenharmony_cihid_hw_start_fail:
8058c2ecf20Sopenharmony_ci	hid_destroy_device(steam->client_hdev);
8068c2ecf20Sopenharmony_ciclient_hdev_fail:
8078c2ecf20Sopenharmony_ci	cancel_work_sync(&steam->work_connect);
8088c2ecf20Sopenharmony_cisteam_alloc_fail:
8098c2ecf20Sopenharmony_ci	hid_err(hdev, "%s: failed with error %d\n",
8108c2ecf20Sopenharmony_ci			__func__, ret);
8118c2ecf20Sopenharmony_ci	return ret;
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_cistatic void steam_remove(struct hid_device *hdev)
8158c2ecf20Sopenharmony_ci{
8168c2ecf20Sopenharmony_ci	struct steam_device *steam = hid_get_drvdata(hdev);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	if (!steam || hdev->group == HID_GROUP_STEAM) {
8198c2ecf20Sopenharmony_ci		hid_hw_stop(hdev);
8208c2ecf20Sopenharmony_ci		return;
8218c2ecf20Sopenharmony_ci	}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	hid_destroy_device(steam->client_hdev);
8248c2ecf20Sopenharmony_ci	steam->client_opened = false;
8258c2ecf20Sopenharmony_ci	cancel_work_sync(&steam->work_connect);
8268c2ecf20Sopenharmony_ci	if (steam->quirks & STEAM_QUIRK_WIRELESS) {
8278c2ecf20Sopenharmony_ci		hid_info(hdev, "Steam wireless receiver disconnected");
8288c2ecf20Sopenharmony_ci	}
8298c2ecf20Sopenharmony_ci	hid_hw_close(hdev);
8308c2ecf20Sopenharmony_ci	hid_hw_stop(hdev);
8318c2ecf20Sopenharmony_ci	steam_unregister(steam);
8328c2ecf20Sopenharmony_ci}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_cistatic void steam_do_connect_event(struct steam_device *steam, bool connected)
8358c2ecf20Sopenharmony_ci{
8368c2ecf20Sopenharmony_ci	unsigned long flags;
8378c2ecf20Sopenharmony_ci	bool changed;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	spin_lock_irqsave(&steam->lock, flags);
8408c2ecf20Sopenharmony_ci	changed = steam->connected != connected;
8418c2ecf20Sopenharmony_ci	steam->connected = connected;
8428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&steam->lock, flags);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	if (changed && schedule_work(&steam->work_connect) == 0)
8458c2ecf20Sopenharmony_ci		dbg_hid("%s: connected=%d event already queued\n",
8468c2ecf20Sopenharmony_ci				__func__, connected);
8478c2ecf20Sopenharmony_ci}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci/*
8508c2ecf20Sopenharmony_ci * Some input data in the protocol has the opposite sign.
8518c2ecf20Sopenharmony_ci * Clamp the values to 32767..-32767 so that the range is
8528c2ecf20Sopenharmony_ci * symmetrical and can be negated safely.
8538c2ecf20Sopenharmony_ci */
8548c2ecf20Sopenharmony_cistatic inline s16 steam_le16(u8 *data)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	s16 x = (s16) le16_to_cpup((__le16 *)data);
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	return x == -32768 ? -32767 : x;
8598c2ecf20Sopenharmony_ci}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci/*
8628c2ecf20Sopenharmony_ci * The size for this message payload is 60.
8638c2ecf20Sopenharmony_ci * The known values are:
8648c2ecf20Sopenharmony_ci *  (* values are not sent through wireless)
8658c2ecf20Sopenharmony_ci *  (* accelerator/gyro is disabled by default)
8668c2ecf20Sopenharmony_ci *  Offset| Type  | Mapped to |Meaning
8678c2ecf20Sopenharmony_ci * -------+-------+-----------+--------------------------
8688c2ecf20Sopenharmony_ci *  4-7   | u32   | --        | sequence number
8698c2ecf20Sopenharmony_ci *  8-10  | 24bit | see below | buttons
8708c2ecf20Sopenharmony_ci *  11    | u8    | ABS_HAT2Y | left trigger
8718c2ecf20Sopenharmony_ci *  12    | u8    | ABS_HAT2X | right trigger
8728c2ecf20Sopenharmony_ci *  13-15 | --    | --        | always 0
8738c2ecf20Sopenharmony_ci *  16-17 | s16   | ABS_X/ABS_HAT0X     | X value
8748c2ecf20Sopenharmony_ci *  18-19 | s16   | ABS_Y/ABS_HAT0Y     | Y value
8758c2ecf20Sopenharmony_ci *  20-21 | s16   | ABS_RX    | right-pad X value
8768c2ecf20Sopenharmony_ci *  22-23 | s16   | ABS_RY    | right-pad Y value
8778c2ecf20Sopenharmony_ci *  24-25 | s16   | --        | * left trigger
8788c2ecf20Sopenharmony_ci *  26-27 | s16   | --        | * right trigger
8798c2ecf20Sopenharmony_ci *  28-29 | s16   | --        | * accelerometer X value
8808c2ecf20Sopenharmony_ci *  30-31 | s16   | --        | * accelerometer Y value
8818c2ecf20Sopenharmony_ci *  32-33 | s16   | --        | * accelerometer Z value
8828c2ecf20Sopenharmony_ci *  34-35 | s16   | --        | gyro X value
8838c2ecf20Sopenharmony_ci *  36-36 | s16   | --        | gyro Y value
8848c2ecf20Sopenharmony_ci *  38-39 | s16   | --        | gyro Z value
8858c2ecf20Sopenharmony_ci *  40-41 | s16   | --        | quaternion W value
8868c2ecf20Sopenharmony_ci *  42-43 | s16   | --        | quaternion X value
8878c2ecf20Sopenharmony_ci *  44-45 | s16   | --        | quaternion Y value
8888c2ecf20Sopenharmony_ci *  46-47 | s16   | --        | quaternion Z value
8898c2ecf20Sopenharmony_ci *  48-49 | --    | --        | always 0
8908c2ecf20Sopenharmony_ci *  50-51 | s16   | --        | * left trigger (uncalibrated)
8918c2ecf20Sopenharmony_ci *  52-53 | s16   | --        | * right trigger (uncalibrated)
8928c2ecf20Sopenharmony_ci *  54-55 | s16   | --        | * joystick X value (uncalibrated)
8938c2ecf20Sopenharmony_ci *  56-57 | s16   | --        | * joystick Y value (uncalibrated)
8948c2ecf20Sopenharmony_ci *  58-59 | s16   | --        | * left-pad X value
8958c2ecf20Sopenharmony_ci *  60-61 | s16   | --        | * left-pad Y value
8968c2ecf20Sopenharmony_ci *  62-63 | u16   | --        | * battery voltage
8978c2ecf20Sopenharmony_ci *
8988c2ecf20Sopenharmony_ci * The buttons are:
8998c2ecf20Sopenharmony_ci *  Bit  | Mapped to  | Description
9008c2ecf20Sopenharmony_ci * ------+------------+--------------------------------
9018c2ecf20Sopenharmony_ci *  8.0  | BTN_TR2    | right trigger fully pressed
9028c2ecf20Sopenharmony_ci *  8.1  | BTN_TL2    | left trigger fully pressed
9038c2ecf20Sopenharmony_ci *  8.2  | BTN_TR     | right shoulder
9048c2ecf20Sopenharmony_ci *  8.3  | BTN_TL     | left shoulder
9058c2ecf20Sopenharmony_ci *  8.4  | BTN_Y      | button Y
9068c2ecf20Sopenharmony_ci *  8.5  | BTN_B      | button B
9078c2ecf20Sopenharmony_ci *  8.6  | BTN_X      | button X
9088c2ecf20Sopenharmony_ci *  8.7  | BTN_A      | button A
9098c2ecf20Sopenharmony_ci *  9.0  | BTN_DPAD_UP    | lef-pad up
9108c2ecf20Sopenharmony_ci *  9.1  | BTN_DPAD_RIGHT | lef-pad right
9118c2ecf20Sopenharmony_ci *  9.2  | BTN_DPAD_LEFT  | lef-pad left
9128c2ecf20Sopenharmony_ci *  9.3  | BTN_DPAD_DOWN  | lef-pad down
9138c2ecf20Sopenharmony_ci *  9.4  | BTN_SELECT | menu left
9148c2ecf20Sopenharmony_ci *  9.5  | BTN_MODE   | steam logo
9158c2ecf20Sopenharmony_ci *  9.6  | BTN_START  | menu right
9168c2ecf20Sopenharmony_ci *  9.7  | BTN_GEAR_DOWN | left back lever
9178c2ecf20Sopenharmony_ci * 10.0  | BTN_GEAR_UP   | right back lever
9188c2ecf20Sopenharmony_ci * 10.1  | --         | left-pad clicked
9198c2ecf20Sopenharmony_ci * 10.2  | BTN_THUMBR | right-pad clicked
9208c2ecf20Sopenharmony_ci * 10.3  | BTN_THUMB  | left-pad touched (but see explanation below)
9218c2ecf20Sopenharmony_ci * 10.4  | BTN_THUMB2 | right-pad touched
9228c2ecf20Sopenharmony_ci * 10.5  | --         | unknown
9238c2ecf20Sopenharmony_ci * 10.6  | BTN_THUMBL | joystick clicked
9248c2ecf20Sopenharmony_ci * 10.7  | --         | lpad_and_joy
9258c2ecf20Sopenharmony_ci */
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_cistatic void steam_do_input_event(struct steam_device *steam,
9288c2ecf20Sopenharmony_ci		struct input_dev *input, u8 *data)
9298c2ecf20Sopenharmony_ci{
9308c2ecf20Sopenharmony_ci	/* 24 bits of buttons */
9318c2ecf20Sopenharmony_ci	u8 b8, b9, b10;
9328c2ecf20Sopenharmony_ci	s16 x, y;
9338c2ecf20Sopenharmony_ci	bool lpad_touched, lpad_and_joy;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	b8 = data[8];
9368c2ecf20Sopenharmony_ci	b9 = data[9];
9378c2ecf20Sopenharmony_ci	b10 = data[10];
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	input_report_abs(input, ABS_HAT2Y, data[11]);
9408c2ecf20Sopenharmony_ci	input_report_abs(input, ABS_HAT2X, data[12]);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	/*
9438c2ecf20Sopenharmony_ci	 * These two bits tells how to interpret the values X and Y.
9448c2ecf20Sopenharmony_ci	 * lpad_and_joy tells that the joystick and the lpad are used at the
9458c2ecf20Sopenharmony_ci	 * same time.
9468c2ecf20Sopenharmony_ci	 * lpad_touched tells whether X/Y are to be read as lpad coord or
9478c2ecf20Sopenharmony_ci	 * joystick values.
9488c2ecf20Sopenharmony_ci	 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched.
9498c2ecf20Sopenharmony_ci	 */
9508c2ecf20Sopenharmony_ci	lpad_touched = b10 & BIT(3);
9518c2ecf20Sopenharmony_ci	lpad_and_joy = b10 & BIT(7);
9528c2ecf20Sopenharmony_ci	x = steam_le16(data + 16);
9538c2ecf20Sopenharmony_ci	y = -steam_le16(data + 18);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
9568c2ecf20Sopenharmony_ci	input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
9578c2ecf20Sopenharmony_ci	/* Check if joystick is centered */
9588c2ecf20Sopenharmony_ci	if (lpad_touched && !lpad_and_joy) {
9598c2ecf20Sopenharmony_ci		input_report_abs(input, ABS_X, 0);
9608c2ecf20Sopenharmony_ci		input_report_abs(input, ABS_Y, 0);
9618c2ecf20Sopenharmony_ci	}
9628c2ecf20Sopenharmony_ci	/* Check if lpad is untouched */
9638c2ecf20Sopenharmony_ci	if (!(lpad_touched || lpad_and_joy)) {
9648c2ecf20Sopenharmony_ci		input_report_abs(input, ABS_HAT0X, 0);
9658c2ecf20Sopenharmony_ci		input_report_abs(input, ABS_HAT0Y, 0);
9668c2ecf20Sopenharmony_ci	}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	input_report_abs(input, ABS_RX, steam_le16(data + 20));
9698c2ecf20Sopenharmony_ci	input_report_abs(input, ABS_RY, -steam_le16(data + 22));
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
9728c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
9738c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
9748c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
9758c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
9768c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
9778c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
9788c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
9798c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
9808c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
9818c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
9828c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
9838c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
9848c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
9858c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
9868c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
9878c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
9888c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
9898c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
9908c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
9918c2ecf20Sopenharmony_ci	input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	input_sync(input);
9948c2ecf20Sopenharmony_ci}
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci/*
9978c2ecf20Sopenharmony_ci * The size for this message payload is 11.
9988c2ecf20Sopenharmony_ci * The known values are:
9998c2ecf20Sopenharmony_ci *  Offset| Type  | Meaning
10008c2ecf20Sopenharmony_ci * -------+-------+---------------------------
10018c2ecf20Sopenharmony_ci *  4-7   | u32   | sequence number
10028c2ecf20Sopenharmony_ci *  8-11  | --    | always 0
10038c2ecf20Sopenharmony_ci *  12-13 | u16   | voltage (mV)
10048c2ecf20Sopenharmony_ci *  14    | u8    | battery percent
10058c2ecf20Sopenharmony_ci */
10068c2ecf20Sopenharmony_cistatic void steam_do_battery_event(struct steam_device *steam,
10078c2ecf20Sopenharmony_ci		struct power_supply *battery, u8 *data)
10088c2ecf20Sopenharmony_ci{
10098c2ecf20Sopenharmony_ci	unsigned long flags;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	s16 volts = steam_le16(data + 12);
10128c2ecf20Sopenharmony_ci	u8 batt = data[14];
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	/* Creating the battery may have failed */
10158c2ecf20Sopenharmony_ci	rcu_read_lock();
10168c2ecf20Sopenharmony_ci	battery = rcu_dereference(steam->battery);
10178c2ecf20Sopenharmony_ci	if (likely(battery)) {
10188c2ecf20Sopenharmony_ci		spin_lock_irqsave(&steam->lock, flags);
10198c2ecf20Sopenharmony_ci		steam->voltage = volts;
10208c2ecf20Sopenharmony_ci		steam->battery_charge = batt;
10218c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&steam->lock, flags);
10228c2ecf20Sopenharmony_ci		power_supply_changed(battery);
10238c2ecf20Sopenharmony_ci	}
10248c2ecf20Sopenharmony_ci	rcu_read_unlock();
10258c2ecf20Sopenharmony_ci}
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_cistatic int steam_raw_event(struct hid_device *hdev,
10288c2ecf20Sopenharmony_ci			struct hid_report *report, u8 *data,
10298c2ecf20Sopenharmony_ci			int size)
10308c2ecf20Sopenharmony_ci{
10318c2ecf20Sopenharmony_ci	struct steam_device *steam = hid_get_drvdata(hdev);
10328c2ecf20Sopenharmony_ci	struct input_dev *input;
10338c2ecf20Sopenharmony_ci	struct power_supply *battery;
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	if (!steam)
10368c2ecf20Sopenharmony_ci		return 0;
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	if (steam->client_opened)
10398c2ecf20Sopenharmony_ci		hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
10408c2ecf20Sopenharmony_ci				data, size, 0);
10418c2ecf20Sopenharmony_ci	/*
10428c2ecf20Sopenharmony_ci	 * All messages are size=64, all values little-endian.
10438c2ecf20Sopenharmony_ci	 * The format is:
10448c2ecf20Sopenharmony_ci	 *  Offset| Meaning
10458c2ecf20Sopenharmony_ci	 * -------+--------------------------------------------
10468c2ecf20Sopenharmony_ci	 *  0-1   | always 0x01, 0x00, maybe protocol version?
10478c2ecf20Sopenharmony_ci	 *  2     | type of message
10488c2ecf20Sopenharmony_ci	 *  3     | length of the real payload (not checked)
10498c2ecf20Sopenharmony_ci	 *  4-n   | payload data, depends on the type
10508c2ecf20Sopenharmony_ci	 *
10518c2ecf20Sopenharmony_ci	 * There are these known types of message:
10528c2ecf20Sopenharmony_ci	 *  0x01: input data (60 bytes)
10538c2ecf20Sopenharmony_ci	 *  0x03: wireless connect/disconnect (1 byte)
10548c2ecf20Sopenharmony_ci	 *  0x04: battery status (11 bytes)
10558c2ecf20Sopenharmony_ci	 */
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	if (size != 64 || data[0] != 1 || data[1] != 0)
10588c2ecf20Sopenharmony_ci		return 0;
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	switch (data[2]) {
10618c2ecf20Sopenharmony_ci	case STEAM_EV_INPUT_DATA:
10628c2ecf20Sopenharmony_ci		if (steam->client_opened)
10638c2ecf20Sopenharmony_ci			return 0;
10648c2ecf20Sopenharmony_ci		rcu_read_lock();
10658c2ecf20Sopenharmony_ci		input = rcu_dereference(steam->input);
10668c2ecf20Sopenharmony_ci		if (likely(input))
10678c2ecf20Sopenharmony_ci			steam_do_input_event(steam, input, data);
10688c2ecf20Sopenharmony_ci		rcu_read_unlock();
10698c2ecf20Sopenharmony_ci		break;
10708c2ecf20Sopenharmony_ci	case STEAM_EV_CONNECT:
10718c2ecf20Sopenharmony_ci		/*
10728c2ecf20Sopenharmony_ci		 * The payload of this event is a single byte:
10738c2ecf20Sopenharmony_ci		 *  0x01: disconnected.
10748c2ecf20Sopenharmony_ci		 *  0x02: connected.
10758c2ecf20Sopenharmony_ci		 */
10768c2ecf20Sopenharmony_ci		switch (data[4]) {
10778c2ecf20Sopenharmony_ci		case 0x01:
10788c2ecf20Sopenharmony_ci			steam_do_connect_event(steam, false);
10798c2ecf20Sopenharmony_ci			break;
10808c2ecf20Sopenharmony_ci		case 0x02:
10818c2ecf20Sopenharmony_ci			steam_do_connect_event(steam, true);
10828c2ecf20Sopenharmony_ci			break;
10838c2ecf20Sopenharmony_ci		}
10848c2ecf20Sopenharmony_ci		break;
10858c2ecf20Sopenharmony_ci	case STEAM_EV_BATTERY:
10868c2ecf20Sopenharmony_ci		if (steam->quirks & STEAM_QUIRK_WIRELESS) {
10878c2ecf20Sopenharmony_ci			rcu_read_lock();
10888c2ecf20Sopenharmony_ci			battery = rcu_dereference(steam->battery);
10898c2ecf20Sopenharmony_ci			if (likely(battery)) {
10908c2ecf20Sopenharmony_ci				steam_do_battery_event(steam, battery, data);
10918c2ecf20Sopenharmony_ci			} else {
10928c2ecf20Sopenharmony_ci				dbg_hid(
10938c2ecf20Sopenharmony_ci					"%s: battery data without connect event\n",
10948c2ecf20Sopenharmony_ci					__func__);
10958c2ecf20Sopenharmony_ci				steam_do_connect_event(steam, true);
10968c2ecf20Sopenharmony_ci			}
10978c2ecf20Sopenharmony_ci			rcu_read_unlock();
10988c2ecf20Sopenharmony_ci		}
10998c2ecf20Sopenharmony_ci		break;
11008c2ecf20Sopenharmony_ci	}
11018c2ecf20Sopenharmony_ci	return 0;
11028c2ecf20Sopenharmony_ci}
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_cistatic int steam_param_set_lizard_mode(const char *val,
11058c2ecf20Sopenharmony_ci					const struct kernel_param *kp)
11068c2ecf20Sopenharmony_ci{
11078c2ecf20Sopenharmony_ci	struct steam_device *steam;
11088c2ecf20Sopenharmony_ci	int ret;
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	ret = param_set_bool(val, kp);
11118c2ecf20Sopenharmony_ci	if (ret)
11128c2ecf20Sopenharmony_ci		return ret;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	mutex_lock(&steam_devices_lock);
11158c2ecf20Sopenharmony_ci	list_for_each_entry(steam, &steam_devices, list) {
11168c2ecf20Sopenharmony_ci		mutex_lock(&steam->mutex);
11178c2ecf20Sopenharmony_ci		if (!steam->client_opened)
11188c2ecf20Sopenharmony_ci			steam_set_lizard_mode(steam, lizard_mode);
11198c2ecf20Sopenharmony_ci		mutex_unlock(&steam->mutex);
11208c2ecf20Sopenharmony_ci	}
11218c2ecf20Sopenharmony_ci	mutex_unlock(&steam_devices_lock);
11228c2ecf20Sopenharmony_ci	return 0;
11238c2ecf20Sopenharmony_ci}
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_cistatic const struct kernel_param_ops steam_lizard_mode_ops = {
11268c2ecf20Sopenharmony_ci	.set	= steam_param_set_lizard_mode,
11278c2ecf20Sopenharmony_ci	.get	= param_get_bool,
11288c2ecf20Sopenharmony_ci};
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_cimodule_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
11318c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lizard_mode,
11328c2ecf20Sopenharmony_ci	"Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_cistatic const struct hid_device_id steam_controllers[] = {
11358c2ecf20Sopenharmony_ci	{ /* Wired Steam Controller */
11368c2ecf20Sopenharmony_ci	  HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
11378c2ecf20Sopenharmony_ci		USB_DEVICE_ID_STEAM_CONTROLLER)
11388c2ecf20Sopenharmony_ci	},
11398c2ecf20Sopenharmony_ci	{ /* Wireless Steam Controller */
11408c2ecf20Sopenharmony_ci	  HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
11418c2ecf20Sopenharmony_ci		USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
11428c2ecf20Sopenharmony_ci	  .driver_data = STEAM_QUIRK_WIRELESS
11438c2ecf20Sopenharmony_ci	},
11448c2ecf20Sopenharmony_ci	{}
11458c2ecf20Sopenharmony_ci};
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, steam_controllers);
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_cistatic struct hid_driver steam_controller_driver = {
11508c2ecf20Sopenharmony_ci	.name = "hid-steam",
11518c2ecf20Sopenharmony_ci	.id_table = steam_controllers,
11528c2ecf20Sopenharmony_ci	.probe = steam_probe,
11538c2ecf20Sopenharmony_ci	.remove = steam_remove,
11548c2ecf20Sopenharmony_ci	.raw_event = steam_raw_event,
11558c2ecf20Sopenharmony_ci};
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_cimodule_hid_driver(steam_controller_driver);
1158