Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * HID driver for Valve Steam Controller
   4 *
   5 * Copyright (c) 2018 Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
 
   6 *
   7 * Supports both the wired and wireless interfaces.
   8 *
   9 * This controller has a builtin emulation of mouse and keyboard: the right pad
  10 * can be used as a mouse, the shoulder buttons are mouse buttons, A and B
  11 * buttons are ENTER and ESCAPE, and so on. This is implemented as additional
  12 * HID interfaces.
  13 *
  14 * This is known as the "lizard mode", because apparently lizards like to use
  15 * the computer from the coach, without a proper mouse and keyboard.
  16 *
  17 * This driver will disable the lizard mode when the input device is opened
  18 * and re-enable it when the input device is closed, so as not to break user
  19 * mode behaviour. The lizard_mode parameter can be used to change that.
  20 *
  21 * There are a few user space applications (notably Steam Client) that use
  22 * the hidraw interface directly to create input devices (XTest, uinput...).
  23 * In order to avoid breaking them this driver creates a layered hidraw device,
  24 * so it can detect when the client is running and then:
  25 *  - it will not send any command to the controller.
  26 *  - this input device will be removed, to avoid double input of the same
  27 *    user action.
  28 * When the client is closed, this input device will be created again.
  29 *
  30 * For additional functions, such as changing the right-pad margin or switching
  31 * the led, you can use the user-space tool at:
  32 *
  33 *   https://github.com/rodrigorc/steamctrl
  34 */
  35
  36#include <linux/device.h>
  37#include <linux/input.h>
  38#include <linux/hid.h>
  39#include <linux/module.h>
  40#include <linux/workqueue.h>
  41#include <linux/mutex.h>
  42#include <linux/rcupdate.h>
  43#include <linux/delay.h>
  44#include <linux/power_supply.h>
  45#include "hid-ids.h"
  46
 
  47MODULE_LICENSE("GPL");
  48MODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>");
  49
  50static bool lizard_mode = true;
  51
  52static DEFINE_MUTEX(steam_devices_lock);
  53static LIST_HEAD(steam_devices);
  54
  55#define STEAM_QUIRK_WIRELESS		BIT(0)
 
  56
  57/* Touch pads are 40 mm in diameter and 65535 units */
  58#define STEAM_PAD_RESOLUTION 1638
  59/* Trigger runs are about 5 mm and 256 units */
  60#define STEAM_TRIGGER_RESOLUTION 51
  61/* Joystick runs are about 5 mm and 256 units */
  62#define STEAM_JOYSTICK_RESOLUTION 51
 
 
 
 
 
 
 
 
 
 
 
 
  63
  64#define STEAM_PAD_FUZZ 256
  65
  66/*
  67 * Commands that can be sent in a feature report.
  68 * Thanks to Valve for some valuable hints.
  69 */
  70#define STEAM_CMD_SET_MAPPINGS		0x80
  71#define STEAM_CMD_CLEAR_MAPPINGS	0x81
  72#define STEAM_CMD_GET_MAPPINGS		0x82
  73#define STEAM_CMD_GET_ATTRIB		0x83
  74#define STEAM_CMD_GET_ATTRIB_LABEL	0x84
  75#define STEAM_CMD_DEFAULT_MAPPINGS	0x85
  76#define STEAM_CMD_FACTORY_RESET		0x86
  77#define STEAM_CMD_WRITE_REGISTER	0x87
  78#define STEAM_CMD_CLEAR_REGISTER	0x88
  79#define STEAM_CMD_READ_REGISTER		0x89
  80#define STEAM_CMD_GET_REGISTER_LABEL	0x8a
  81#define STEAM_CMD_GET_REGISTER_MAX	0x8b
  82#define STEAM_CMD_GET_REGISTER_DEFAULT	0x8c
  83#define STEAM_CMD_SET_MODE		0x8d
  84#define STEAM_CMD_DEFAULT_MOUSE		0x8e
  85#define STEAM_CMD_FORCEFEEDBAK		0x8f
  86#define STEAM_CMD_REQUEST_COMM_STATUS	0xb4
  87#define STEAM_CMD_GET_SERIAL		0xae
  88
  89/* Some useful register ids */
  90#define STEAM_REG_LPAD_MODE		0x07
  91#define STEAM_REG_RPAD_MODE		0x08
  92#define STEAM_REG_RPAD_MARGIN		0x18
  93#define STEAM_REG_LED			0x2d
  94#define STEAM_REG_GYRO_MODE		0x30
  95
  96/* Raw event identifiers */
  97#define STEAM_EV_INPUT_DATA		0x01
  98#define STEAM_EV_CONNECT		0x03
  99#define STEAM_EV_BATTERY		0x04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 100
 101/* Values for GYRO_MODE (bitmask) */
 102#define STEAM_GYRO_MODE_OFF		0x0000
 103#define STEAM_GYRO_MODE_STEERING	0x0001
 104#define STEAM_GYRO_MODE_TILT		0x0002
 105#define STEAM_GYRO_MODE_SEND_ORIENTATION	0x0004
 106#define STEAM_GYRO_MODE_SEND_RAW_ACCEL		0x0008
 107#define STEAM_GYRO_MODE_SEND_RAW_GYRO		0x0010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 108
 109/* Other random constants */
 110#define STEAM_SERIAL_LEN 10
 111
 112struct steam_device {
 113	struct list_head list;
 114	spinlock_t lock;
 115	struct hid_device *hdev, *client_hdev;
 116	struct mutex mutex;
 117	bool client_opened;
 118	struct input_dev __rcu *input;
 
 119	unsigned long quirks;
 120	struct work_struct work_connect;
 121	bool connected;
 122	char serial_no[STEAM_SERIAL_LEN + 1];
 123	struct power_supply_desc battery_desc;
 124	struct power_supply __rcu *battery;
 125	u8 battery_charge;
 126	u16 voltage;
 
 
 
 
 
 
 
 
 127};
 128
 129static int steam_recv_report(struct steam_device *steam,
 130		u8 *data, int size)
 131{
 132	struct hid_report *r;
 133	u8 *buf;
 134	int ret;
 135
 136	r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
 137	if (!r) {
 138		hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted -  nothing to read\n");
 139		return -EINVAL;
 140	}
 141
 142	if (hid_report_len(r) < 64)
 143		return -EINVAL;
 144
 145	buf = hid_alloc_report_buf(r, GFP_KERNEL);
 146	if (!buf)
 147		return -ENOMEM;
 148
 149	/*
 150	 * The report ID is always 0, so strip the first byte from the output.
 151	 * hid_report_len() is not counting the report ID, so +1 to the length
 152	 * or else we get a EOVERFLOW. We are safe from a buffer overflow
 153	 * because hid_alloc_report_buf() allocates +7 bytes.
 154	 */
 155	ret = hid_hw_raw_request(steam->hdev, 0x00,
 156			buf, hid_report_len(r) + 1,
 157			HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
 158	if (ret > 0)
 159		memcpy(data, buf + 1, min(size, ret - 1));
 160	kfree(buf);
 161	return ret;
 162}
 163
 164static int steam_send_report(struct steam_device *steam,
 165		u8 *cmd, int size)
 166{
 167	struct hid_report *r;
 168	u8 *buf;
 169	unsigned int retries = 50;
 170	int ret;
 171
 172	r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
 173	if (!r) {
 174		hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted -  nothing to read\n");
 175		return -EINVAL;
 176	}
 177
 178	if (hid_report_len(r) < 64)
 179		return -EINVAL;
 180
 181	buf = hid_alloc_report_buf(r, GFP_KERNEL);
 182	if (!buf)
 183		return -ENOMEM;
 184
 185	/* The report ID is always 0 */
 186	memcpy(buf + 1, cmd, size);
 187
 188	/*
 189	 * Sometimes the wireless controller fails with EPIPE
 190	 * when sending a feature report.
 191	 * Doing a HID_REQ_GET_REPORT and waiting for a while
 192	 * seems to fix that.
 193	 */
 194	do {
 195		ret = hid_hw_raw_request(steam->hdev, 0,
 196				buf, size + 1,
 197				HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
 198		if (ret != -EPIPE)
 199			break;
 200		msleep(20);
 201	} while (--retries);
 202
 203	kfree(buf);
 204	if (ret < 0)
 205		hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
 206				ret, size, cmd);
 207	return ret;
 208}
 209
 210static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
 211{
 212	return steam_send_report(steam, &cmd, 1);
 213}
 214
 215static int steam_write_registers(struct steam_device *steam,
 216		/* u8 reg, u16 val */...)
 217{
 218	/* Send: 0x87 len (reg valLo valHi)* */
 219	u8 reg;
 220	u16 val;
 221	u8 cmd[64] = {STEAM_CMD_WRITE_REGISTER, 0x00};
 
 222	va_list args;
 223
 224	va_start(args, steam);
 225	for (;;) {
 226		reg = va_arg(args, int);
 227		if (reg == 0)
 228			break;
 229		val = va_arg(args, int);
 230		cmd[cmd[1] + 2] = reg;
 231		cmd[cmd[1] + 3] = val & 0xff;
 232		cmd[cmd[1] + 4] = val >> 8;
 233		cmd[1] += 3;
 234	}
 235	va_end(args);
 236
 237	return steam_send_report(steam, cmd, 2 + cmd[1]);
 
 
 
 
 
 
 
 
 
 238}
 239
 240static int steam_get_serial(struct steam_device *steam)
 241{
 242	/*
 243	 * Send: 0xae 0x15 0x01
 244	 * Recv: 0xae 0x15 0x01 serialnumber (10 chars)
 245	 */
 246	int ret;
 247	u8 cmd[] = {STEAM_CMD_GET_SERIAL, 0x15, 0x01};
 248	u8 reply[3 + STEAM_SERIAL_LEN + 1];
 249
 
 250	ret = steam_send_report(steam, cmd, sizeof(cmd));
 251	if (ret < 0)
 252		return ret;
 253	ret = steam_recv_report(steam, reply, sizeof(reply));
 254	if (ret < 0)
 255		return ret;
 256	if (reply[0] != 0xae || reply[1] != 0x15 || reply[2] != 0x01)
 257		return -EIO;
 
 
 
 258	reply[3 + STEAM_SERIAL_LEN] = 0;
 259	strscpy(steam->serial_no, reply + 3, sizeof(steam->serial_no));
 260	return 0;
 
 
 261}
 262
 263/*
 264 * This command requests the wireless adaptor to post an event
 265 * with the connection status. Useful if this driver is loaded when
 266 * the controller is already connected.
 267 */
 268static inline int steam_request_conn_status(struct steam_device *steam)
 269{
 270	return steam_send_report_byte(steam, STEAM_CMD_REQUEST_COMM_STATUS);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 271}
 
 272
 273static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
 274{
 
 
 
 275	if (enable) {
 
 276		/* enable esc, enter, cursors */
 277		steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MAPPINGS);
 278		/* enable mouse */
 279		steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MOUSE);
 280		steam_write_registers(steam,
 281			STEAM_REG_RPAD_MARGIN, 0x01, /* enable margin */
 282			0);
 283	} else {
 
 284		/* disable esc, enter, cursor */
 285		steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
 286		steam_write_registers(steam,
 287			STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
 288			STEAM_REG_RPAD_MARGIN, 0x00, /* disable margin */
 289			0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 290	}
 291}
 292
 293static int steam_input_open(struct input_dev *dev)
 294{
 295	struct steam_device *steam = input_get_drvdata(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 296
 297	mutex_lock(&steam->mutex);
 298	if (!steam->client_opened && lizard_mode)
 299		steam_set_lizard_mode(steam, false);
 300	mutex_unlock(&steam->mutex);
 301	return 0;
 302}
 303
 304static void steam_input_close(struct input_dev *dev)
 305{
 306	struct steam_device *steam = input_get_drvdata(dev);
 
 
 307
 308	mutex_lock(&steam->mutex);
 309	if (!steam->client_opened && lizard_mode)
 310		steam_set_lizard_mode(steam, true);
 311	mutex_unlock(&steam->mutex);
 
 
 
 312}
 313
 314static enum power_supply_property steam_battery_props[] = {
 315	POWER_SUPPLY_PROP_PRESENT,
 316	POWER_SUPPLY_PROP_SCOPE,
 317	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 318	POWER_SUPPLY_PROP_CAPACITY,
 319};
 320
 321static int steam_battery_get_property(struct power_supply *psy,
 322				enum power_supply_property psp,
 323				union power_supply_propval *val)
 324{
 325	struct steam_device *steam = power_supply_get_drvdata(psy);
 326	unsigned long flags;
 327	s16 volts;
 328	u8 batt;
 329	int ret = 0;
 330
 331	spin_lock_irqsave(&steam->lock, flags);
 332	volts = steam->voltage;
 333	batt = steam->battery_charge;
 334	spin_unlock_irqrestore(&steam->lock, flags);
 335
 336	switch (psp) {
 337	case POWER_SUPPLY_PROP_PRESENT:
 338		val->intval = 1;
 339		break;
 340	case POWER_SUPPLY_PROP_SCOPE:
 341		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
 342		break;
 343	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 344		val->intval = volts * 1000; /* mV -> uV */
 345		break;
 346	case POWER_SUPPLY_PROP_CAPACITY:
 347		val->intval = batt;
 348		break;
 349	default:
 350		ret = -EINVAL;
 351		break;
 352	}
 353	return ret;
 354}
 355
 356static int steam_battery_register(struct steam_device *steam)
 357{
 358	struct power_supply *battery;
 359	struct power_supply_config battery_cfg = { .drv_data = steam, };
 360	unsigned long flags;
 361	int ret;
 362
 363	steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
 364	steam->battery_desc.properties = steam_battery_props;
 365	steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
 366	steam->battery_desc.get_property = steam_battery_get_property;
 367	steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
 368			GFP_KERNEL, "steam-controller-%s-battery",
 369			steam->serial_no);
 370	if (!steam->battery_desc.name)
 371		return -ENOMEM;
 372
 373	/* avoid the warning of 0% battery while waiting for the first info */
 374	spin_lock_irqsave(&steam->lock, flags);
 375	steam->voltage = 3000;
 376	steam->battery_charge = 100;
 377	spin_unlock_irqrestore(&steam->lock, flags);
 378
 379	battery = power_supply_register(&steam->hdev->dev,
 380			&steam->battery_desc, &battery_cfg);
 381	if (IS_ERR(battery)) {
 382		ret = PTR_ERR(battery);
 383		hid_err(steam->hdev,
 384				"%s:power_supply_register failed with error %d\n",
 385				__func__, ret);
 386		return ret;
 387	}
 388	rcu_assign_pointer(steam->battery, battery);
 389	power_supply_powers(battery, &steam->hdev->dev);
 390	return 0;
 391}
 392
 393static int steam_input_register(struct steam_device *steam)
 394{
 395	struct hid_device *hdev = steam->hdev;
 396	struct input_dev *input;
 397	int ret;
 398
 399	rcu_read_lock();
 400	input = rcu_dereference(steam->input);
 401	rcu_read_unlock();
 402	if (input) {
 403		dbg_hid("%s: already connected\n", __func__);
 404		return 0;
 405	}
 406
 407	input = input_allocate_device();
 408	if (!input)
 409		return -ENOMEM;
 410
 411	input_set_drvdata(input, steam);
 412	input->dev.parent = &hdev->dev;
 413	input->open = steam_input_open;
 414	input->close = steam_input_close;
 415
 416	input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ?
 417		"Wireless Steam Controller" :
 418		"Steam Controller";
 419	input->phys = hdev->phys;
 420	input->uniq = steam->serial_no;
 421	input->id.bustype = hdev->bus;
 422	input->id.vendor = hdev->vendor;
 423	input->id.product = hdev->product;
 424	input->id.version = hdev->version;
 425
 426	input_set_capability(input, EV_KEY, BTN_TR2);
 427	input_set_capability(input, EV_KEY, BTN_TL2);
 428	input_set_capability(input, EV_KEY, BTN_TR);
 429	input_set_capability(input, EV_KEY, BTN_TL);
 430	input_set_capability(input, EV_KEY, BTN_Y);
 431	input_set_capability(input, EV_KEY, BTN_B);
 432	input_set_capability(input, EV_KEY, BTN_X);
 433	input_set_capability(input, EV_KEY, BTN_A);
 434	input_set_capability(input, EV_KEY, BTN_DPAD_UP);
 435	input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
 436	input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
 437	input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
 438	input_set_capability(input, EV_KEY, BTN_SELECT);
 439	input_set_capability(input, EV_KEY, BTN_MODE);
 440	input_set_capability(input, EV_KEY, BTN_START);
 441	input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
 442	input_set_capability(input, EV_KEY, BTN_GEAR_UP);
 443	input_set_capability(input, EV_KEY, BTN_THUMBR);
 444	input_set_capability(input, EV_KEY, BTN_THUMBL);
 445	input_set_capability(input, EV_KEY, BTN_THUMB);
 446	input_set_capability(input, EV_KEY, BTN_THUMB2);
 
 
 
 
 
 
 
 
 
 
 447
 448	input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
 449	input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
 450	input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
 451	input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
 452	input_set_abs_params(input, ABS_RX, -32767, 32767,
 453			STEAM_PAD_FUZZ, 0);
 454	input_set_abs_params(input, ABS_RY, -32767, 32767,
 455			STEAM_PAD_FUZZ, 0);
 456	input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
 457			STEAM_PAD_FUZZ, 0);
 458	input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
 459			STEAM_PAD_FUZZ, 0);
 460	input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
 461	input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
 462	input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
 463	input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 464	input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
 465	input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
 466	input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
 467	input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
 
 
 
 
 
 
 
 468
 469	ret = input_register_device(input);
 470	if (ret)
 471		goto input_register_fail;
 472
 473	rcu_assign_pointer(steam->input, input);
 474	return 0;
 475
 476input_register_fail:
 477	input_free_device(input);
 478	return ret;
 479}
 480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 481static void steam_input_unregister(struct steam_device *steam)
 482{
 483	struct input_dev *input;
 484	rcu_read_lock();
 485	input = rcu_dereference(steam->input);
 486	rcu_read_unlock();
 487	if (!input)
 488		return;
 489	RCU_INIT_POINTER(steam->input, NULL);
 490	synchronize_rcu();
 491	input_unregister_device(input);
 492}
 493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 494static void steam_battery_unregister(struct steam_device *steam)
 495{
 496	struct power_supply *battery;
 497
 498	rcu_read_lock();
 499	battery = rcu_dereference(steam->battery);
 500	rcu_read_unlock();
 501
 502	if (!battery)
 503		return;
 504	RCU_INIT_POINTER(steam->battery, NULL);
 505	synchronize_rcu();
 506	power_supply_unregister(battery);
 507}
 508
 509static int steam_register(struct steam_device *steam)
 510{
 511	int ret;
 512	bool client_opened;
 
 513
 514	/*
 515	 * This function can be called several times in a row with the
 516	 * wireless adaptor, without steam_unregister() between them, because
 517	 * another client send a get_connection_status command, for example.
 518	 * The battery and serial number are set just once per device.
 519	 */
 520	if (!steam->serial_no[0]) {
 521		/*
 522		 * Unlikely, but getting the serial could fail, and it is not so
 523		 * important, so make up a serial number and go on.
 524		 */
 525		mutex_lock(&steam->mutex);
 526		if (steam_get_serial(steam) < 0)
 527			strscpy(steam->serial_no, "XXXXXXXXXX",
 528					sizeof(steam->serial_no));
 529		mutex_unlock(&steam->mutex);
 530
 531		hid_info(steam->hdev, "Steam Controller '%s' connected",
 532				steam->serial_no);
 533
 534		/* ignore battery errors, we can live without it */
 535		if (steam->quirks & STEAM_QUIRK_WIRELESS)
 536			steam_battery_register(steam);
 537
 538		mutex_lock(&steam_devices_lock);
 539		if (list_empty(&steam->list))
 540			list_add(&steam->list, &steam_devices);
 541		mutex_unlock(&steam_devices_lock);
 542	}
 543
 544	mutex_lock(&steam->mutex);
 545	client_opened = steam->client_opened;
 546	if (!client_opened)
 547		steam_set_lizard_mode(steam, lizard_mode);
 548	mutex_unlock(&steam->mutex);
 549
 550	if (!client_opened)
 
 551		ret = steam_input_register(steam);
 552	else
 553		ret = 0;
 
 
 
 
 
 554
 
 
 
 555	return ret;
 556}
 557
 558static void steam_unregister(struct steam_device *steam)
 559{
 560	steam_battery_unregister(steam);
 
 561	steam_input_unregister(steam);
 562	if (steam->serial_no[0]) {
 563		hid_info(steam->hdev, "Steam Controller '%s' disconnected",
 564				steam->serial_no);
 565		mutex_lock(&steam_devices_lock);
 566		list_del_init(&steam->list);
 567		mutex_unlock(&steam_devices_lock);
 568		steam->serial_no[0] = 0;
 569	}
 570}
 571
 572static void steam_work_connect_cb(struct work_struct *work)
 573{
 574	struct steam_device *steam = container_of(work, struct steam_device,
 575							work_connect);
 576	unsigned long flags;
 577	bool connected;
 578	int ret;
 579
 580	spin_lock_irqsave(&steam->lock, flags);
 581	connected = steam->connected;
 582	spin_unlock_irqrestore(&steam->lock, flags);
 583
 584	if (connected) {
 585		ret = steam_register(steam);
 586		if (ret) {
 587			hid_err(steam->hdev,
 588				"%s:steam_register failed with error %d\n",
 589				__func__, ret);
 590		}
 591	} else {
 592		steam_unregister(steam);
 593	}
 594}
 595
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 596static bool steam_is_valve_interface(struct hid_device *hdev)
 597{
 598	struct hid_report_enum *rep_enum;
 599
 600	/*
 601	 * The wired device creates 3 interfaces:
 602	 *  0: emulated mouse.
 603	 *  1: emulated keyboard.
 604	 *  2: the real game pad.
 605	 * The wireless device creates 5 interfaces:
 606	 *  0: emulated keyboard.
 607	 *  1-4: slots where up to 4 real game pads will be connected to.
 608	 * We know which one is the real gamepad interface because they are the
 609	 * only ones with a feature report.
 610	 */
 611	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
 612	return !list_empty(&rep_enum->report_list);
 613}
 614
 615static int steam_client_ll_parse(struct hid_device *hdev)
 616{
 617	struct steam_device *steam = hdev->driver_data;
 618
 619	return hid_parse_report(hdev, steam->hdev->dev_rdesc,
 620			steam->hdev->dev_rsize);
 621}
 622
 623static int steam_client_ll_start(struct hid_device *hdev)
 624{
 625	return 0;
 626}
 627
 628static void steam_client_ll_stop(struct hid_device *hdev)
 629{
 630}
 631
 632static int steam_client_ll_open(struct hid_device *hdev)
 633{
 634	struct steam_device *steam = hdev->driver_data;
 
 635
 636	mutex_lock(&steam->mutex);
 637	steam->client_opened = true;
 638	mutex_unlock(&steam->mutex);
 639
 640	steam_input_unregister(steam);
 641
 642	return 0;
 643}
 644
 645static void steam_client_ll_close(struct hid_device *hdev)
 646{
 647	struct steam_device *steam = hdev->driver_data;
 648
 649	unsigned long flags;
 650	bool connected;
 651
 652	spin_lock_irqsave(&steam->lock, flags);
 653	connected = steam->connected;
 
 654	spin_unlock_irqrestore(&steam->lock, flags);
 655
 656	mutex_lock(&steam->mutex);
 657	steam->client_opened = false;
 658	if (connected)
 659		steam_set_lizard_mode(steam, lizard_mode);
 660	mutex_unlock(&steam->mutex);
 661
 662	if (connected)
 663		steam_input_register(steam);
 664}
 665
 666static int steam_client_ll_raw_request(struct hid_device *hdev,
 667				unsigned char reportnum, u8 *buf,
 668				size_t count, unsigned char report_type,
 669				int reqtype)
 670{
 671	struct steam_device *steam = hdev->driver_data;
 672
 673	return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
 674			report_type, reqtype);
 675}
 676
 677static struct hid_ll_driver steam_client_ll_driver = {
 678	.parse = steam_client_ll_parse,
 679	.start = steam_client_ll_start,
 680	.stop = steam_client_ll_stop,
 681	.open = steam_client_ll_open,
 682	.close = steam_client_ll_close,
 683	.raw_request = steam_client_ll_raw_request,
 684};
 685
 686static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
 687{
 688	struct hid_device *client_hdev;
 689
 690	client_hdev = hid_allocate_device();
 691	if (IS_ERR(client_hdev))
 692		return client_hdev;
 693
 694	client_hdev->ll_driver = &steam_client_ll_driver;
 695	client_hdev->dev.parent = hdev->dev.parent;
 696	client_hdev->bus = hdev->bus;
 697	client_hdev->vendor = hdev->vendor;
 698	client_hdev->product = hdev->product;
 699	client_hdev->version = hdev->version;
 700	client_hdev->type = hdev->type;
 701	client_hdev->country = hdev->country;
 702	strscpy(client_hdev->name, hdev->name,
 703			sizeof(client_hdev->name));
 704	strscpy(client_hdev->phys, hdev->phys,
 705			sizeof(client_hdev->phys));
 706	/*
 707	 * Since we use the same device info than the real interface to
 708	 * trick userspace, we will be calling steam_probe recursively.
 709	 * We need to recognize the client interface somehow.
 710	 */
 711	client_hdev->group = HID_GROUP_STEAM;
 712	return client_hdev;
 713}
 714
 715static int steam_probe(struct hid_device *hdev,
 716				const struct hid_device_id *id)
 717{
 718	struct steam_device *steam;
 719	int ret;
 720
 721	ret = hid_parse(hdev);
 722	if (ret) {
 723		hid_err(hdev,
 724			"%s:parse of hid interface failed\n", __func__);
 725		return ret;
 726	}
 727
 728	/*
 729	 * The virtual client_dev is only used for hidraw.
 730	 * Also avoid the recursive probe.
 731	 */
 732	if (hdev->group == HID_GROUP_STEAM)
 733		return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
 734	/*
 735	 * The non-valve interfaces (mouse and keyboard emulation) are
 736	 * connected without changes.
 737	 */
 738	if (!steam_is_valve_interface(hdev))
 739		return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
 740
 741	steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
 742	if (!steam) {
 743		ret = -ENOMEM;
 744		goto steam_alloc_fail;
 745	}
 746	steam->hdev = hdev;
 747	hid_set_drvdata(hdev, steam);
 748	spin_lock_init(&steam->lock);
 749	mutex_init(&steam->mutex);
 750	steam->quirks = id->driver_data;
 751	INIT_WORK(&steam->work_connect, steam_work_connect_cb);
 
 752	INIT_LIST_HEAD(&steam->list);
 753
 754	steam->client_hdev = steam_create_client_hid(hdev);
 755	if (IS_ERR(steam->client_hdev)) {
 756		ret = PTR_ERR(steam->client_hdev);
 757		goto client_hdev_fail;
 758	}
 759	steam->client_hdev->driver_data = steam;
 760
 761	/*
 762	 * With the real steam controller interface, do not connect hidraw.
 763	 * Instead, create the client_hid and connect that.
 764	 */
 765	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
 766	if (ret)
 767		goto hid_hw_start_fail;
 768
 769	ret = hid_add_device(steam->client_hdev);
 770	if (ret)
 771		goto client_hdev_add_fail;
 772
 773	ret = hid_hw_open(hdev);
 774	if (ret) {
 775		hid_err(hdev,
 776			"%s:hid_hw_open\n",
 777			__func__);
 778		goto hid_hw_open_fail;
 779	}
 780
 781	if (steam->quirks & STEAM_QUIRK_WIRELESS) {
 782		hid_info(hdev, "Steam wireless receiver connected");
 783		/* If using a wireless adaptor ask for connection status */
 784		steam->connected = false;
 785		steam_request_conn_status(steam);
 786	} else {
 787		/* A wired connection is always present */
 788		steam->connected = true;
 789		ret = steam_register(steam);
 790		if (ret) {
 791			hid_err(hdev,
 792				"%s:steam_register failed with error %d\n",
 793				__func__, ret);
 794			goto input_register_fail;
 795		}
 796	}
 797
 
 
 
 
 
 
 
 
 
 
 
 798	return 0;
 799
 800input_register_fail:
 801hid_hw_open_fail:
 802client_hdev_add_fail:
 803	hid_hw_stop(hdev);
 804hid_hw_start_fail:
 805	hid_destroy_device(steam->client_hdev);
 806client_hdev_fail:
 
 
 
 
 
 
 
 807	cancel_work_sync(&steam->work_connect);
 808steam_alloc_fail:
 809	hid_err(hdev, "%s: failed with error %d\n",
 810			__func__, ret);
 
 811	return ret;
 812}
 813
 814static void steam_remove(struct hid_device *hdev)
 815{
 816	struct steam_device *steam = hid_get_drvdata(hdev);
 817
 818	if (!steam || hdev->group == HID_GROUP_STEAM) {
 819		hid_hw_stop(hdev);
 820		return;
 821	}
 822
 823	hid_destroy_device(steam->client_hdev);
 824	steam->client_opened = false;
 825	cancel_work_sync(&steam->work_connect);
 
 
 
 
 826	if (steam->quirks & STEAM_QUIRK_WIRELESS) {
 827		hid_info(hdev, "Steam wireless receiver disconnected");
 828	}
 829	hid_hw_close(hdev);
 830	hid_hw_stop(hdev);
 831	steam_unregister(steam);
 832}
 833
 834static void steam_do_connect_event(struct steam_device *steam, bool connected)
 835{
 836	unsigned long flags;
 837	bool changed;
 838
 839	spin_lock_irqsave(&steam->lock, flags);
 840	changed = steam->connected != connected;
 841	steam->connected = connected;
 842	spin_unlock_irqrestore(&steam->lock, flags);
 843
 844	if (changed && schedule_work(&steam->work_connect) == 0)
 845		dbg_hid("%s: connected=%d event already queued\n",
 846				__func__, connected);
 847}
 848
 849/*
 850 * Some input data in the protocol has the opposite sign.
 851 * Clamp the values to 32767..-32767 so that the range is
 852 * symmetrical and can be negated safely.
 853 */
 854static inline s16 steam_le16(u8 *data)
 855{
 856	s16 x = (s16) le16_to_cpup((__le16 *)data);
 857
 858	return x == -32768 ? -32767 : x;
 859}
 860
 861/*
 862 * The size for this message payload is 60.
 863 * The known values are:
 864 *  (* values are not sent through wireless)
 865 *  (* accelerator/gyro is disabled by default)
 866 *  Offset| Type  | Mapped to |Meaning
 867 * -------+-------+-----------+--------------------------
 868 *  4-7   | u32   | --        | sequence number
 869 *  8-10  | 24bit | see below | buttons
 870 *  11    | u8    | ABS_HAT2Y | left trigger
 871 *  12    | u8    | ABS_HAT2X | right trigger
 872 *  13-15 | --    | --        | always 0
 873 *  16-17 | s16   | ABS_X/ABS_HAT0X     | X value
 874 *  18-19 | s16   | ABS_Y/ABS_HAT0Y     | Y value
 875 *  20-21 | s16   | ABS_RX    | right-pad X value
 876 *  22-23 | s16   | ABS_RY    | right-pad Y value
 877 *  24-25 | s16   | --        | * left trigger
 878 *  26-27 | s16   | --        | * right trigger
 879 *  28-29 | s16   | --        | * accelerometer X value
 880 *  30-31 | s16   | --        | * accelerometer Y value
 881 *  32-33 | s16   | --        | * accelerometer Z value
 882 *  34-35 | s16   | --        | gyro X value
 883 *  36-36 | s16   | --        | gyro Y value
 884 *  38-39 | s16   | --        | gyro Z value
 885 *  40-41 | s16   | --        | quaternion W value
 886 *  42-43 | s16   | --        | quaternion X value
 887 *  44-45 | s16   | --        | quaternion Y value
 888 *  46-47 | s16   | --        | quaternion Z value
 889 *  48-49 | --    | --        | always 0
 890 *  50-51 | s16   | --        | * left trigger (uncalibrated)
 891 *  52-53 | s16   | --        | * right trigger (uncalibrated)
 892 *  54-55 | s16   | --        | * joystick X value (uncalibrated)
 893 *  56-57 | s16   | --        | * joystick Y value (uncalibrated)
 894 *  58-59 | s16   | --        | * left-pad X value
 895 *  60-61 | s16   | --        | * left-pad Y value
 896 *  62-63 | u16   | --        | * battery voltage
 897 *
 898 * The buttons are:
 899 *  Bit  | Mapped to  | Description
 900 * ------+------------+--------------------------------
 901 *  8.0  | BTN_TR2    | right trigger fully pressed
 902 *  8.1  | BTN_TL2    | left trigger fully pressed
 903 *  8.2  | BTN_TR     | right shoulder
 904 *  8.3  | BTN_TL     | left shoulder
 905 *  8.4  | BTN_Y      | button Y
 906 *  8.5  | BTN_B      | button B
 907 *  8.6  | BTN_X      | button X
 908 *  8.7  | BTN_A      | button A
 909 *  9.0  | BTN_DPAD_UP    | lef-pad up
 910 *  9.1  | BTN_DPAD_RIGHT | lef-pad right
 911 *  9.2  | BTN_DPAD_LEFT  | lef-pad left
 912 *  9.3  | BTN_DPAD_DOWN  | lef-pad down
 913 *  9.4  | BTN_SELECT | menu left
 914 *  9.5  | BTN_MODE   | steam logo
 915 *  9.6  | BTN_START  | menu right
 916 *  9.7  | BTN_GEAR_DOWN | left back lever
 917 * 10.0  | BTN_GEAR_UP   | right back lever
 918 * 10.1  | --         | left-pad clicked
 919 * 10.2  | BTN_THUMBR | right-pad clicked
 920 * 10.3  | BTN_THUMB  | left-pad touched (but see explanation below)
 921 * 10.4  | BTN_THUMB2 | right-pad touched
 922 * 10.5  | --         | unknown
 923 * 10.6  | BTN_THUMBL | joystick clicked
 924 * 10.7  | --         | lpad_and_joy
 925 */
 926
 927static void steam_do_input_event(struct steam_device *steam,
 928		struct input_dev *input, u8 *data)
 929{
 930	/* 24 bits of buttons */
 931	u8 b8, b9, b10;
 932	s16 x, y;
 933	bool lpad_touched, lpad_and_joy;
 934
 935	b8 = data[8];
 936	b9 = data[9];
 937	b10 = data[10];
 938
 939	input_report_abs(input, ABS_HAT2Y, data[11]);
 940	input_report_abs(input, ABS_HAT2X, data[12]);
 941
 942	/*
 943	 * These two bits tells how to interpret the values X and Y.
 944	 * lpad_and_joy tells that the joystick and the lpad are used at the
 945	 * same time.
 946	 * lpad_touched tells whether X/Y are to be read as lpad coord or
 947	 * joystick values.
 948	 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched.
 949	 */
 950	lpad_touched = b10 & BIT(3);
 951	lpad_and_joy = b10 & BIT(7);
 952	x = steam_le16(data + 16);
 953	y = -steam_le16(data + 18);
 954
 955	input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
 956	input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
 957	/* Check if joystick is centered */
 958	if (lpad_touched && !lpad_and_joy) {
 959		input_report_abs(input, ABS_X, 0);
 960		input_report_abs(input, ABS_Y, 0);
 961	}
 962	/* Check if lpad is untouched */
 963	if (!(lpad_touched || lpad_and_joy)) {
 964		input_report_abs(input, ABS_HAT0X, 0);
 965		input_report_abs(input, ABS_HAT0Y, 0);
 966	}
 967
 968	input_report_abs(input, ABS_RX, steam_le16(data + 20));
 969	input_report_abs(input, ABS_RY, -steam_le16(data + 22));
 970
 971	input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
 972	input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
 973	input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
 974	input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
 975	input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
 976	input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
 977	input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
 978	input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
 979	input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
 980	input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
 981	input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
 982	input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
 983	input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
 984	input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
 985	input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
 986	input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
 987	input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
 988	input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
 989	input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
 990	input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
 991	input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
 992
 993	input_sync(input);
 994}
 995
 996/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 997 * The size for this message payload is 11.
 998 * The known values are:
 999 *  Offset| Type  | Meaning
1000 * -------+-------+---------------------------
1001 *  4-7   | u32   | sequence number
1002 *  8-11  | --    | always 0
1003 *  12-13 | u16   | voltage (mV)
1004 *  14    | u8    | battery percent
1005 */
1006static void steam_do_battery_event(struct steam_device *steam,
1007		struct power_supply *battery, u8 *data)
1008{
1009	unsigned long flags;
1010
1011	s16 volts = steam_le16(data + 12);
1012	u8 batt = data[14];
1013
1014	/* Creating the battery may have failed */
1015	rcu_read_lock();
1016	battery = rcu_dereference(steam->battery);
1017	if (likely(battery)) {
1018		spin_lock_irqsave(&steam->lock, flags);
1019		steam->voltage = volts;
1020		steam->battery_charge = batt;
1021		spin_unlock_irqrestore(&steam->lock, flags);
1022		power_supply_changed(battery);
1023	}
1024	rcu_read_unlock();
1025}
1026
1027static int steam_raw_event(struct hid_device *hdev,
1028			struct hid_report *report, u8 *data,
1029			int size)
1030{
1031	struct steam_device *steam = hid_get_drvdata(hdev);
1032	struct input_dev *input;
 
1033	struct power_supply *battery;
1034
1035	if (!steam)
1036		return 0;
1037
1038	if (steam->client_opened)
1039		hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
1040				data, size, 0);
1041	/*
1042	 * All messages are size=64, all values little-endian.
1043	 * The format is:
1044	 *  Offset| Meaning
1045	 * -------+--------------------------------------------
1046	 *  0-1   | always 0x01, 0x00, maybe protocol version?
1047	 *  2     | type of message
1048	 *  3     | length of the real payload (not checked)
1049	 *  4-n   | payload data, depends on the type
1050	 *
1051	 * There are these known types of message:
1052	 *  0x01: input data (60 bytes)
1053	 *  0x03: wireless connect/disconnect (1 byte)
1054	 *  0x04: battery status (11 bytes)
 
1055	 */
1056
1057	if (size != 64 || data[0] != 1 || data[1] != 0)
1058		return 0;
1059
1060	switch (data[2]) {
1061	case STEAM_EV_INPUT_DATA:
1062		if (steam->client_opened)
1063			return 0;
1064		rcu_read_lock();
1065		input = rcu_dereference(steam->input);
1066		if (likely(input))
1067			steam_do_input_event(steam, input, data);
1068		rcu_read_unlock();
1069		break;
1070	case STEAM_EV_CONNECT:
 
 
 
 
 
 
 
 
 
 
 
 
1071		/*
1072		 * The payload of this event is a single byte:
1073		 *  0x01: disconnected.
1074		 *  0x02: connected.
1075		 */
1076		switch (data[4]) {
1077		case 0x01:
1078			steam_do_connect_event(steam, false);
1079			break;
1080		case 0x02:
1081			steam_do_connect_event(steam, true);
1082			break;
1083		}
1084		break;
1085	case STEAM_EV_BATTERY:
1086		if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1087			rcu_read_lock();
1088			battery = rcu_dereference(steam->battery);
1089			if (likely(battery)) {
1090				steam_do_battery_event(steam, battery, data);
1091			} else {
1092				dbg_hid(
1093					"%s: battery data without connect event\n",
1094					__func__);
1095				steam_do_connect_event(steam, true);
1096			}
1097			rcu_read_unlock();
1098		}
1099		break;
1100	}
1101	return 0;
1102}
1103
1104static int steam_param_set_lizard_mode(const char *val,
1105					const struct kernel_param *kp)
1106{
1107	struct steam_device *steam;
1108	int ret;
1109
1110	ret = param_set_bool(val, kp);
1111	if (ret)
1112		return ret;
1113
1114	mutex_lock(&steam_devices_lock);
1115	list_for_each_entry(steam, &steam_devices, list) {
1116		mutex_lock(&steam->mutex);
1117		if (!steam->client_opened)
1118			steam_set_lizard_mode(steam, lizard_mode);
1119		mutex_unlock(&steam->mutex);
1120	}
1121	mutex_unlock(&steam_devices_lock);
1122	return 0;
1123}
1124
1125static const struct kernel_param_ops steam_lizard_mode_ops = {
1126	.set	= steam_param_set_lizard_mode,
1127	.get	= param_get_bool,
1128};
1129
1130module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
1131MODULE_PARM_DESC(lizard_mode,
1132	"Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
1133
1134static const struct hid_device_id steam_controllers[] = {
1135	{ /* Wired Steam Controller */
1136	  HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1137		USB_DEVICE_ID_STEAM_CONTROLLER)
1138	},
1139	{ /* Wireless Steam Controller */
1140	  HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1141		USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
1142	  .driver_data = STEAM_QUIRK_WIRELESS
 
 
 
 
 
1143	},
1144	{}
1145};
1146
1147MODULE_DEVICE_TABLE(hid, steam_controllers);
1148
1149static struct hid_driver steam_controller_driver = {
1150	.name = "hid-steam",
1151	.id_table = steam_controllers,
1152	.probe = steam_probe,
1153	.remove = steam_remove,
1154	.raw_event = steam_raw_event,
1155};
1156
1157module_hid_driver(steam_controller_driver);
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * HID driver for Valve Steam Controller
   4 *
   5 * Copyright (c) 2018 Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
   6 * Copyright (c) 2022 Valve Software
   7 *
   8 * Supports both the wired and wireless interfaces.
   9 *
  10 * This controller has a builtin emulation of mouse and keyboard: the right pad
  11 * can be used as a mouse, the shoulder buttons are mouse buttons, A and B
  12 * buttons are ENTER and ESCAPE, and so on. This is implemented as additional
  13 * HID interfaces.
  14 *
  15 * This is known as the "lizard mode", because apparently lizards like to use
  16 * the computer from the coach, without a proper mouse and keyboard.
  17 *
  18 * This driver will disable the lizard mode when the input device is opened
  19 * and re-enable it when the input device is closed, so as not to break user
  20 * mode behaviour. The lizard_mode parameter can be used to change that.
  21 *
  22 * There are a few user space applications (notably Steam Client) that use
  23 * the hidraw interface directly to create input devices (XTest, uinput...).
  24 * In order to avoid breaking them this driver creates a layered hidraw device,
  25 * so it can detect when the client is running and then:
  26 *  - it will not send any command to the controller.
  27 *  - this input device will be removed, to avoid double input of the same
  28 *    user action.
  29 * When the client is closed, this input device will be created again.
  30 *
  31 * For additional functions, such as changing the right-pad margin or switching
  32 * the led, you can use the user-space tool at:
  33 *
  34 *   https://github.com/rodrigorc/steamctrl
  35 */
  36
  37#include <linux/device.h>
  38#include <linux/input.h>
  39#include <linux/hid.h>
  40#include <linux/module.h>
  41#include <linux/workqueue.h>
  42#include <linux/mutex.h>
  43#include <linux/rcupdate.h>
  44#include <linux/delay.h>
  45#include <linux/power_supply.h>
  46#include "hid-ids.h"
  47
  48MODULE_DESCRIPTION("HID driver for Valve Steam Controller");
  49MODULE_LICENSE("GPL");
  50MODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>");
  51
  52static bool lizard_mode = true;
  53
  54static DEFINE_MUTEX(steam_devices_lock);
  55static LIST_HEAD(steam_devices);
  56
  57#define STEAM_QUIRK_WIRELESS		BIT(0)
  58#define STEAM_QUIRK_DECK		BIT(1)
  59
  60/* Touch pads are 40 mm in diameter and 65535 units */
  61#define STEAM_PAD_RESOLUTION 1638
  62/* Trigger runs are about 5 mm and 256 units */
  63#define STEAM_TRIGGER_RESOLUTION 51
  64/* Joystick runs are about 5 mm and 256 units */
  65#define STEAM_JOYSTICK_RESOLUTION 51
  66/* Trigger runs are about 6 mm and 32768 units */
  67#define STEAM_DECK_TRIGGER_RESOLUTION 5461
  68/* Joystick runs are about 5 mm and 32768 units */
  69#define STEAM_DECK_JOYSTICK_RESOLUTION 6553
  70/* Accelerometer has 16 bit resolution and a range of +/- 2g */
  71#define STEAM_DECK_ACCEL_RES_PER_G 16384
  72#define STEAM_DECK_ACCEL_RANGE 32768
  73#define STEAM_DECK_ACCEL_FUZZ 32
  74/* Gyroscope has 16 bit resolution and a range of +/- 2000 dps */
  75#define STEAM_DECK_GYRO_RES_PER_DPS 16
  76#define STEAM_DECK_GYRO_RANGE 32768
  77#define STEAM_DECK_GYRO_FUZZ 1
  78
  79#define STEAM_PAD_FUZZ 256
  80
  81/*
  82 * Commands that can be sent in a feature report.
  83 * Thanks to Valve and SDL for the names.
  84 */
  85enum {
  86	ID_SET_DIGITAL_MAPPINGS		= 0x80,
  87	ID_CLEAR_DIGITAL_MAPPINGS	= 0x81,
  88	ID_GET_DIGITAL_MAPPINGS		= 0x82,
  89	ID_GET_ATTRIBUTES_VALUES	= 0x83,
  90	ID_GET_ATTRIBUTE_LABEL		= 0x84,
  91	ID_SET_DEFAULT_DIGITAL_MAPPINGS	= 0x85,
  92	ID_FACTORY_RESET		= 0x86,
  93	ID_SET_SETTINGS_VALUES		= 0x87,
  94	ID_CLEAR_SETTINGS_VALUES	= 0x88,
  95	ID_GET_SETTINGS_VALUES		= 0x89,
  96	ID_GET_SETTING_LABEL		= 0x8A,
  97	ID_GET_SETTINGS_MAXS		= 0x8B,
  98	ID_GET_SETTINGS_DEFAULTS	= 0x8C,
  99	ID_SET_CONTROLLER_MODE		= 0x8D,
 100	ID_LOAD_DEFAULT_SETTINGS	= 0x8E,
 101	ID_TRIGGER_HAPTIC_PULSE		= 0x8F,
 102	ID_TURN_OFF_CONTROLLER		= 0x9F,
 103
 104	ID_GET_DEVICE_INFO		= 0xA1,
 105
 106	ID_CALIBRATE_TRACKPADS		= 0xA7,
 107	ID_RESERVED_0			= 0xA8,
 108	ID_SET_SERIAL_NUMBER		= 0xA9,
 109	ID_GET_TRACKPAD_CALIBRATION	= 0xAA,
 110	ID_GET_TRACKPAD_FACTORY_CALIBRATION = 0xAB,
 111	ID_GET_TRACKPAD_RAW_DATA	= 0xAC,
 112	ID_ENABLE_PAIRING		= 0xAD,
 113	ID_GET_STRING_ATTRIBUTE		= 0xAE,
 114	ID_RADIO_ERASE_RECORDS		= 0xAF,
 115	ID_RADIO_WRITE_RECORD		= 0xB0,
 116	ID_SET_DONGLE_SETTING		= 0xB1,
 117	ID_DONGLE_DISCONNECT_DEVICE	= 0xB2,
 118	ID_DONGLE_COMMIT_DEVICE		= 0xB3,
 119	ID_DONGLE_GET_WIRELESS_STATE	= 0xB4,
 120	ID_CALIBRATE_GYRO		= 0xB5,
 121	ID_PLAY_AUDIO			= 0xB6,
 122	ID_AUDIO_UPDATE_START		= 0xB7,
 123	ID_AUDIO_UPDATE_DATA		= 0xB8,
 124	ID_AUDIO_UPDATE_COMPLETE	= 0xB9,
 125	ID_GET_CHIPID			= 0xBA,
 126
 127	ID_CALIBRATE_JOYSTICK		= 0xBF,
 128	ID_CALIBRATE_ANALOG_TRIGGERS	= 0xC0,
 129	ID_SET_AUDIO_MAPPING		= 0xC1,
 130	ID_CHECK_GYRO_FW_LOAD		= 0xC2,
 131	ID_CALIBRATE_ANALOG		= 0xC3,
 132	ID_DONGLE_GET_CONNECTED_SLOTS	= 0xC4,
 133
 134	ID_RESET_IMU			= 0xCE,
 135
 136	ID_TRIGGER_HAPTIC_CMD		= 0xEA,
 137	ID_TRIGGER_RUMBLE_CMD		= 0xEB,
 138};
 139
 140/* Settings IDs */
 141enum {
 142	/* 0 */
 143	SETTING_MOUSE_SENSITIVITY,
 144	SETTING_MOUSE_ACCELERATION,
 145	SETTING_TRACKBALL_ROTATION_ANGLE,
 146	SETTING_HAPTIC_INTENSITY_UNUSED,
 147	SETTING_LEFT_GAMEPAD_STICK_ENABLED,
 148	SETTING_RIGHT_GAMEPAD_STICK_ENABLED,
 149	SETTING_USB_DEBUG_MODE,
 150	SETTING_LEFT_TRACKPAD_MODE,
 151	SETTING_RIGHT_TRACKPAD_MODE,
 152	SETTING_MOUSE_POINTER_ENABLED,
 153
 154	/* 10 */
 155	SETTING_DPAD_DEADZONE,
 156	SETTING_MINIMUM_MOMENTUM_VEL,
 157	SETTING_MOMENTUM_DECAY_AMMOUNT,
 158	SETTING_TRACKPAD_RELATIVE_MODE_TICKS_PER_PIXEL,
 159	SETTING_HAPTIC_INCREMENT,
 160	SETTING_DPAD_ANGLE_SIN,
 161	SETTING_DPAD_ANGLE_COS,
 162	SETTING_MOMENTUM_VERTICAL_DIVISOR,
 163	SETTING_MOMENTUM_MAXIMUM_VELOCITY,
 164	SETTING_TRACKPAD_Z_ON,
 165
 166	/* 20 */
 167	SETTING_TRACKPAD_Z_OFF,
 168	SETTING_SENSITIVY_SCALE_AMMOUNT,
 169	SETTING_LEFT_TRACKPAD_SECONDARY_MODE,
 170	SETTING_RIGHT_TRACKPAD_SECONDARY_MODE,
 171	SETTING_SMOOTH_ABSOLUTE_MOUSE,
 172	SETTING_STEAMBUTTON_POWEROFF_TIME,
 173	SETTING_UNUSED_1,
 174	SETTING_TRACKPAD_OUTER_RADIUS,
 175	SETTING_TRACKPAD_Z_ON_LEFT,
 176	SETTING_TRACKPAD_Z_OFF_LEFT,
 177
 178	/* 30 */
 179	SETTING_TRACKPAD_OUTER_SPIN_VEL,
 180	SETTING_TRACKPAD_OUTER_SPIN_RADIUS,
 181	SETTING_TRACKPAD_OUTER_SPIN_HORIZONTAL_ONLY,
 182	SETTING_TRACKPAD_RELATIVE_MODE_DEADZONE,
 183	SETTING_TRACKPAD_RELATIVE_MODE_MAX_VEL,
 184	SETTING_TRACKPAD_RELATIVE_MODE_INVERT_Y,
 185	SETTING_TRACKPAD_DOUBLE_TAP_BEEP_ENABLED,
 186	SETTING_TRACKPAD_DOUBLE_TAP_BEEP_PERIOD,
 187	SETTING_TRACKPAD_DOUBLE_TAP_BEEP_COUNT,
 188	SETTING_TRACKPAD_OUTER_RADIUS_RELEASE_ON_TRANSITION,
 189
 190	/* 40 */
 191	SETTING_RADIAL_MODE_ANGLE,
 192	SETTING_HAPTIC_INTENSITY_MOUSE_MODE,
 193	SETTING_LEFT_DPAD_REQUIRES_CLICK,
 194	SETTING_RIGHT_DPAD_REQUIRES_CLICK,
 195	SETTING_LED_BASELINE_BRIGHTNESS,
 196	SETTING_LED_USER_BRIGHTNESS,
 197	SETTING_ENABLE_RAW_JOYSTICK,
 198	SETTING_ENABLE_FAST_SCAN,
 199	SETTING_IMU_MODE,
 200	SETTING_WIRELESS_PACKET_VERSION,
 201
 202	/* 50 */
 203	SETTING_SLEEP_INACTIVITY_TIMEOUT,
 204	SETTING_TRACKPAD_NOISE_THRESHOLD,
 205	SETTING_LEFT_TRACKPAD_CLICK_PRESSURE,
 206	SETTING_RIGHT_TRACKPAD_CLICK_PRESSURE,
 207	SETTING_LEFT_BUMPER_CLICK_PRESSURE,
 208	SETTING_RIGHT_BUMPER_CLICK_PRESSURE,
 209	SETTING_LEFT_GRIP_CLICK_PRESSURE,
 210	SETTING_RIGHT_GRIP_CLICK_PRESSURE,
 211	SETTING_LEFT_GRIP2_CLICK_PRESSURE,
 212	SETTING_RIGHT_GRIP2_CLICK_PRESSURE,
 213
 214	/* 60 */
 215	SETTING_PRESSURE_MODE,
 216	SETTING_CONTROLLER_TEST_MODE,
 217	SETTING_TRIGGER_MODE,
 218	SETTING_TRACKPAD_Z_THRESHOLD,
 219	SETTING_FRAME_RATE,
 220	SETTING_TRACKPAD_FILT_CTRL,
 221	SETTING_TRACKPAD_CLIP,
 222	SETTING_DEBUG_OUTPUT_SELECT,
 223	SETTING_TRIGGER_THRESHOLD_PERCENT,
 224	SETTING_TRACKPAD_FREQUENCY_HOPPING,
 225
 226	/* 70 */
 227	SETTING_HAPTICS_ENABLED,
 228	SETTING_STEAM_WATCHDOG_ENABLE,
 229	SETTING_TIMP_TOUCH_THRESHOLD_ON,
 230	SETTING_TIMP_TOUCH_THRESHOLD_OFF,
 231	SETTING_FREQ_HOPPING,
 232	SETTING_TEST_CONTROL,
 233	SETTING_HAPTIC_MASTER_GAIN_DB,
 234	SETTING_THUMB_TOUCH_THRESH,
 235	SETTING_DEVICE_POWER_STATUS,
 236	SETTING_HAPTIC_INTENSITY,
 237
 238	/* 80 */
 239	SETTING_STABILIZER_ENABLED,
 240	SETTING_TIMP_MODE_MTE,
 241};
 242
 243/* Input report identifiers */
 244enum
 245{
 246	ID_CONTROLLER_STATE = 1,
 247	ID_CONTROLLER_DEBUG = 2,
 248	ID_CONTROLLER_WIRELESS = 3,
 249	ID_CONTROLLER_STATUS = 4,
 250	ID_CONTROLLER_DEBUG2 = 5,
 251	ID_CONTROLLER_SECONDARY_STATE = 6,
 252	ID_CONTROLLER_BLE_STATE = 7,
 253	ID_CONTROLLER_DECK_STATE = 9
 254};
 255
 256/* String attribute identifiers */
 257enum {
 258	ATTRIB_STR_BOARD_SERIAL,
 259	ATTRIB_STR_UNIT_SERIAL,
 260};
 261
 262/* Values for GYRO_MODE (bitmask) */
 263enum {
 264	SETTING_GYRO_MODE_OFF			= 0,
 265	SETTING_GYRO_MODE_STEERING		= BIT(0),
 266	SETTING_GYRO_MODE_TILT			= BIT(1),
 267	SETTING_GYRO_MODE_SEND_ORIENTATION	= BIT(2),
 268	SETTING_GYRO_MODE_SEND_RAW_ACCEL	= BIT(3),
 269	SETTING_GYRO_MODE_SEND_RAW_GYRO		= BIT(4),
 270};
 271
 272/* Trackpad modes */
 273enum {
 274	TRACKPAD_ABSOLUTE_MOUSE,
 275	TRACKPAD_RELATIVE_MOUSE,
 276	TRACKPAD_DPAD_FOUR_WAY_DISCRETE,
 277	TRACKPAD_DPAD_FOUR_WAY_OVERLAP,
 278	TRACKPAD_DPAD_EIGHT_WAY,
 279	TRACKPAD_RADIAL_MODE,
 280	TRACKPAD_ABSOLUTE_DPAD,
 281	TRACKPAD_NONE,
 282	TRACKPAD_GESTURE_KEYBOARD,
 283};
 284
 285/* Pad identifiers for the deck */
 286#define STEAM_PAD_LEFT 0
 287#define STEAM_PAD_RIGHT 1
 288#define STEAM_PAD_BOTH 2
 289
 290/* Other random constants */
 291#define STEAM_SERIAL_LEN 0x15
 292
 293struct steam_device {
 294	struct list_head list;
 295	spinlock_t lock;
 296	struct hid_device *hdev, *client_hdev;
 297	struct mutex report_mutex;
 298	unsigned long client_opened;
 299	struct input_dev __rcu *input;
 300	struct input_dev __rcu *sensors;
 301	unsigned long quirks;
 302	struct work_struct work_connect;
 303	bool connected;
 304	char serial_no[STEAM_SERIAL_LEN + 1];
 305	struct power_supply_desc battery_desc;
 306	struct power_supply __rcu *battery;
 307	u8 battery_charge;
 308	u16 voltage;
 309	struct delayed_work mode_switch;
 310	bool did_mode_switch;
 311	bool gamepad_mode;
 312	struct work_struct rumble_work;
 313	u16 rumble_left;
 314	u16 rumble_right;
 315	unsigned int sensor_timestamp_us;
 316	struct work_struct unregister_work;
 317};
 318
 319static int steam_recv_report(struct steam_device *steam,
 320		u8 *data, int size)
 321{
 322	struct hid_report *r;
 323	u8 *buf;
 324	int ret;
 325
 326	r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
 327	if (!r) {
 328		hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted -  nothing to read\n");
 329		return -EINVAL;
 330	}
 331
 332	if (hid_report_len(r) < 64)
 333		return -EINVAL;
 334
 335	buf = hid_alloc_report_buf(r, GFP_KERNEL);
 336	if (!buf)
 337		return -ENOMEM;
 338
 339	/*
 340	 * The report ID is always 0, so strip the first byte from the output.
 341	 * hid_report_len() is not counting the report ID, so +1 to the length
 342	 * or else we get a EOVERFLOW. We are safe from a buffer overflow
 343	 * because hid_alloc_report_buf() allocates +7 bytes.
 344	 */
 345	ret = hid_hw_raw_request(steam->hdev, 0x00,
 346			buf, hid_report_len(r) + 1,
 347			HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
 348	if (ret > 0)
 349		memcpy(data, buf + 1, min(size, ret - 1));
 350	kfree(buf);
 351	return ret;
 352}
 353
 354static int steam_send_report(struct steam_device *steam,
 355		u8 *cmd, int size)
 356{
 357	struct hid_report *r;
 358	u8 *buf;
 359	unsigned int retries = 50;
 360	int ret;
 361
 362	r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
 363	if (!r) {
 364		hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted -  nothing to read\n");
 365		return -EINVAL;
 366	}
 367
 368	if (hid_report_len(r) < 64)
 369		return -EINVAL;
 370
 371	buf = hid_alloc_report_buf(r, GFP_KERNEL);
 372	if (!buf)
 373		return -ENOMEM;
 374
 375	/* The report ID is always 0 */
 376	memcpy(buf + 1, cmd, size);
 377
 378	/*
 379	 * Sometimes the wireless controller fails with EPIPE
 380	 * when sending a feature report.
 381	 * Doing a HID_REQ_GET_REPORT and waiting for a while
 382	 * seems to fix that.
 383	 */
 384	do {
 385		ret = hid_hw_raw_request(steam->hdev, 0,
 386				buf, max(size, 64) + 1,
 387				HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
 388		if (ret != -EPIPE)
 389			break;
 390		msleep(20);
 391	} while (--retries);
 392
 393	kfree(buf);
 394	if (ret < 0)
 395		hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
 396				ret, size, cmd);
 397	return ret;
 398}
 399
 400static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
 401{
 402	return steam_send_report(steam, &cmd, 1);
 403}
 404
 405static int steam_write_settings(struct steam_device *steam,
 406		/* u8 reg, u16 val */...)
 407{
 408	/* Send: 0x87 len (reg valLo valHi)* */
 409	u8 reg;
 410	u16 val;
 411	u8 cmd[64] = {ID_SET_SETTINGS_VALUES, 0x00};
 412	int ret;
 413	va_list args;
 414
 415	va_start(args, steam);
 416	for (;;) {
 417		reg = va_arg(args, int);
 418		if (reg == 0)
 419			break;
 420		val = va_arg(args, int);
 421		cmd[cmd[1] + 2] = reg;
 422		cmd[cmd[1] + 3] = val & 0xff;
 423		cmd[cmd[1] + 4] = val >> 8;
 424		cmd[1] += 3;
 425	}
 426	va_end(args);
 427
 428	ret = steam_send_report(steam, cmd, 2 + cmd[1]);
 429	if (ret < 0)
 430		return ret;
 431
 432	/*
 433	 * Sometimes a lingering report for this command can
 434	 * get read back instead of the last set report if
 435	 * this isn't explicitly queried
 436	 */
 437	return steam_recv_report(steam, cmd, 2 + cmd[1]);
 438}
 439
 440static int steam_get_serial(struct steam_device *steam)
 441{
 442	/*
 443	 * Send: 0xae 0x15 0x01
 444	 * Recv: 0xae 0x15 0x01 serialnumber
 445	 */
 446	int ret = 0;
 447	u8 cmd[] = {ID_GET_STRING_ATTRIBUTE, sizeof(steam->serial_no), ATTRIB_STR_UNIT_SERIAL};
 448	u8 reply[3 + STEAM_SERIAL_LEN + 1];
 449
 450	mutex_lock(&steam->report_mutex);
 451	ret = steam_send_report(steam, cmd, sizeof(cmd));
 452	if (ret < 0)
 453		goto out;
 454	ret = steam_recv_report(steam, reply, sizeof(reply));
 455	if (ret < 0)
 456		goto out;
 457	if (reply[0] != ID_GET_STRING_ATTRIBUTE || reply[1] < 1 ||
 458	    reply[1] > sizeof(steam->serial_no) || reply[2] != ATTRIB_STR_UNIT_SERIAL) {
 459		ret = -EIO;
 460		goto out;
 461	}
 462	reply[3 + STEAM_SERIAL_LEN] = 0;
 463	strscpy(steam->serial_no, reply + 3, reply[1]);
 464out:
 465	mutex_unlock(&steam->report_mutex);
 466	return ret;
 467}
 468
 469/*
 470 * This command requests the wireless adaptor to post an event
 471 * with the connection status. Useful if this driver is loaded when
 472 * the controller is already connected.
 473 */
 474static inline int steam_request_conn_status(struct steam_device *steam)
 475{
 476	int ret;
 477	mutex_lock(&steam->report_mutex);
 478	ret = steam_send_report_byte(steam, ID_DONGLE_GET_WIRELESS_STATE);
 479	mutex_unlock(&steam->report_mutex);
 480	return ret;
 481}
 482
 483/*
 484 * Send a haptic pulse to the trackpads
 485 * Duration and interval are measured in microseconds, count is the number
 486 * of pulses to send for duration time with interval microseconds between them
 487 * and gain is measured in decibels, ranging from -24 to +6
 488 */
 489static inline int steam_haptic_pulse(struct steam_device *steam, u8 pad,
 490				u16 duration, u16 interval, u16 count, u8 gain)
 491{
 492	int ret;
 493	u8 report[10] = {ID_TRIGGER_HAPTIC_PULSE, 8};
 494
 495	/* Left and right are swapped on this report for legacy reasons */
 496	if (pad < STEAM_PAD_BOTH)
 497		pad ^= 1;
 498
 499	report[2] = pad;
 500	report[3] = duration & 0xFF;
 501	report[4] = duration >> 8;
 502	report[5] = interval & 0xFF;
 503	report[6] = interval >> 8;
 504	report[7] = count & 0xFF;
 505	report[8] = count >> 8;
 506	report[9] = gain;
 507
 508	mutex_lock(&steam->report_mutex);
 509	ret = steam_send_report(steam, report, sizeof(report));
 510	mutex_unlock(&steam->report_mutex);
 511	return ret;
 512}
 513
 514static inline int steam_haptic_rumble(struct steam_device *steam,
 515				u16 intensity, u16 left_speed, u16 right_speed,
 516				u8 left_gain, u8 right_gain)
 517{
 518	int ret;
 519	u8 report[11] = {ID_TRIGGER_RUMBLE_CMD, 9};
 520
 521	report[3] = intensity & 0xFF;
 522	report[4] = intensity >> 8;
 523	report[5] = left_speed & 0xFF;
 524	report[6] = left_speed >> 8;
 525	report[7] = right_speed & 0xFF;
 526	report[8] = right_speed >> 8;
 527	report[9] = left_gain;
 528	report[10] = right_gain;
 529
 530	mutex_lock(&steam->report_mutex);
 531	ret = steam_send_report(steam, report, sizeof(report));
 532	mutex_unlock(&steam->report_mutex);
 533	return ret;
 534}
 535
 536static void steam_haptic_rumble_cb(struct work_struct *work)
 537{
 538	struct steam_device *steam = container_of(work, struct steam_device,
 539							rumble_work);
 540	steam_haptic_rumble(steam, 0, steam->rumble_left,
 541		steam->rumble_right, 2, 0);
 542}
 543
 544#ifdef CONFIG_STEAM_FF
 545static int steam_play_effect(struct input_dev *dev, void *data,
 546				struct ff_effect *effect)
 547{
 548	struct steam_device *steam = input_get_drvdata(dev);
 549
 550	steam->rumble_left = effect->u.rumble.strong_magnitude;
 551	steam->rumble_right = effect->u.rumble.weak_magnitude;
 552
 553	return schedule_work(&steam->rumble_work);
 554}
 555#endif
 556
 557static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
 558{
 559	if (steam->gamepad_mode)
 560		enable = false;
 561
 562	if (enable) {
 563		mutex_lock(&steam->report_mutex);
 564		/* enable esc, enter, cursors */
 565		steam_send_report_byte(steam, ID_SET_DEFAULT_DIGITAL_MAPPINGS);
 566		/* reset settings */
 567		steam_send_report_byte(steam, ID_LOAD_DEFAULT_SETTINGS);
 568		mutex_unlock(&steam->report_mutex);
 
 
 569	} else {
 570		mutex_lock(&steam->report_mutex);
 571		/* disable esc, enter, cursor */
 572		steam_send_report_byte(steam, ID_CLEAR_DIGITAL_MAPPINGS);
 573
 574		if (steam->quirks & STEAM_QUIRK_DECK) {
 575			steam_write_settings(steam,
 576				SETTING_LEFT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
 577				SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
 578				SETTING_LEFT_TRACKPAD_CLICK_PRESSURE, 0xFFFF, /* disable haptic click */
 579				SETTING_RIGHT_TRACKPAD_CLICK_PRESSURE, 0xFFFF, /* disable haptic click */
 580				SETTING_STEAM_WATCHDOG_ENABLE, 0, /* disable watchdog that tests if Steam is active */
 581				0);
 582			mutex_unlock(&steam->report_mutex);
 583		} else {
 584			steam_write_settings(steam,
 585				SETTING_LEFT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
 586				SETTING_RIGHT_TRACKPAD_MODE, TRACKPAD_NONE, /* disable mouse */
 587				0);
 588			mutex_unlock(&steam->report_mutex);
 589		}
 590	}
 591}
 592
 593static int steam_input_open(struct input_dev *dev)
 594{
 595	struct steam_device *steam = input_get_drvdata(dev);
 596	unsigned long flags;
 597	bool set_lizard_mode;
 598
 599	/*
 600	 * Disabling lizard mode automatically is only done on the Steam
 601	 * Controller. On the Steam Deck, this is toggled manually by holding
 602	 * the options button instead, handled by steam_mode_switch_cb.
 603	 */
 604	if (!(steam->quirks & STEAM_QUIRK_DECK)) {
 605		spin_lock_irqsave(&steam->lock, flags);
 606		set_lizard_mode = !steam->client_opened && lizard_mode;
 607		spin_unlock_irqrestore(&steam->lock, flags);
 608		if (set_lizard_mode)
 609			steam_set_lizard_mode(steam, false);
 610	}
 611
 
 
 
 
 612	return 0;
 613}
 614
 615static void steam_input_close(struct input_dev *dev)
 616{
 617	struct steam_device *steam = input_get_drvdata(dev);
 618	unsigned long flags;
 619	bool set_lizard_mode;
 620
 621	if (!(steam->quirks & STEAM_QUIRK_DECK)) {
 622		spin_lock_irqsave(&steam->lock, flags);
 623		set_lizard_mode = !steam->client_opened && lizard_mode;
 624		spin_unlock_irqrestore(&steam->lock, flags);
 625		if (set_lizard_mode)
 626			steam_set_lizard_mode(steam, true);
 627	}
 628}
 629
 630static enum power_supply_property steam_battery_props[] = {
 631	POWER_SUPPLY_PROP_PRESENT,
 632	POWER_SUPPLY_PROP_SCOPE,
 633	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 634	POWER_SUPPLY_PROP_CAPACITY,
 635};
 636
 637static int steam_battery_get_property(struct power_supply *psy,
 638				enum power_supply_property psp,
 639				union power_supply_propval *val)
 640{
 641	struct steam_device *steam = power_supply_get_drvdata(psy);
 642	unsigned long flags;
 643	s16 volts;
 644	u8 batt;
 645	int ret = 0;
 646
 647	spin_lock_irqsave(&steam->lock, flags);
 648	volts = steam->voltage;
 649	batt = steam->battery_charge;
 650	spin_unlock_irqrestore(&steam->lock, flags);
 651
 652	switch (psp) {
 653	case POWER_SUPPLY_PROP_PRESENT:
 654		val->intval = 1;
 655		break;
 656	case POWER_SUPPLY_PROP_SCOPE:
 657		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
 658		break;
 659	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 660		val->intval = volts * 1000; /* mV -> uV */
 661		break;
 662	case POWER_SUPPLY_PROP_CAPACITY:
 663		val->intval = batt;
 664		break;
 665	default:
 666		ret = -EINVAL;
 667		break;
 668	}
 669	return ret;
 670}
 671
 672static int steam_battery_register(struct steam_device *steam)
 673{
 674	struct power_supply *battery;
 675	struct power_supply_config battery_cfg = { .drv_data = steam, };
 676	unsigned long flags;
 677	int ret;
 678
 679	steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
 680	steam->battery_desc.properties = steam_battery_props;
 681	steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
 682	steam->battery_desc.get_property = steam_battery_get_property;
 683	steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
 684			GFP_KERNEL, "steam-controller-%s-battery",
 685			steam->serial_no);
 686	if (!steam->battery_desc.name)
 687		return -ENOMEM;
 688
 689	/* avoid the warning of 0% battery while waiting for the first info */
 690	spin_lock_irqsave(&steam->lock, flags);
 691	steam->voltage = 3000;
 692	steam->battery_charge = 100;
 693	spin_unlock_irqrestore(&steam->lock, flags);
 694
 695	battery = power_supply_register(&steam->hdev->dev,
 696			&steam->battery_desc, &battery_cfg);
 697	if (IS_ERR(battery)) {
 698		ret = PTR_ERR(battery);
 699		hid_err(steam->hdev,
 700				"%s:power_supply_register failed with error %d\n",
 701				__func__, ret);
 702		return ret;
 703	}
 704	rcu_assign_pointer(steam->battery, battery);
 705	power_supply_powers(battery, &steam->hdev->dev);
 706	return 0;
 707}
 708
 709static int steam_input_register(struct steam_device *steam)
 710{
 711	struct hid_device *hdev = steam->hdev;
 712	struct input_dev *input;
 713	int ret;
 714
 715	rcu_read_lock();
 716	input = rcu_dereference(steam->input);
 717	rcu_read_unlock();
 718	if (input) {
 719		dbg_hid("%s: already connected\n", __func__);
 720		return 0;
 721	}
 722
 723	input = input_allocate_device();
 724	if (!input)
 725		return -ENOMEM;
 726
 727	input_set_drvdata(input, steam);
 728	input->dev.parent = &hdev->dev;
 729	input->open = steam_input_open;
 730	input->close = steam_input_close;
 731
 732	input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ? "Wireless Steam Controller" :
 733		(steam->quirks & STEAM_QUIRK_DECK) ? "Steam Deck" :
 734		"Steam Controller";
 735	input->phys = hdev->phys;
 736	input->uniq = steam->serial_no;
 737	input->id.bustype = hdev->bus;
 738	input->id.vendor = hdev->vendor;
 739	input->id.product = hdev->product;
 740	input->id.version = hdev->version;
 741
 742	input_set_capability(input, EV_KEY, BTN_TR2);
 743	input_set_capability(input, EV_KEY, BTN_TL2);
 744	input_set_capability(input, EV_KEY, BTN_TR);
 745	input_set_capability(input, EV_KEY, BTN_TL);
 746	input_set_capability(input, EV_KEY, BTN_Y);
 747	input_set_capability(input, EV_KEY, BTN_B);
 748	input_set_capability(input, EV_KEY, BTN_X);
 749	input_set_capability(input, EV_KEY, BTN_A);
 750	input_set_capability(input, EV_KEY, BTN_DPAD_UP);
 751	input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
 752	input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
 753	input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
 754	input_set_capability(input, EV_KEY, BTN_SELECT);
 755	input_set_capability(input, EV_KEY, BTN_MODE);
 756	input_set_capability(input, EV_KEY, BTN_START);
 
 
 757	input_set_capability(input, EV_KEY, BTN_THUMBR);
 758	input_set_capability(input, EV_KEY, BTN_THUMBL);
 759	input_set_capability(input, EV_KEY, BTN_THUMB);
 760	input_set_capability(input, EV_KEY, BTN_THUMB2);
 761	if (steam->quirks & STEAM_QUIRK_DECK) {
 762		input_set_capability(input, EV_KEY, BTN_BASE);
 763		input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY1);
 764		input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY2);
 765		input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY3);
 766		input_set_capability(input, EV_KEY, BTN_TRIGGER_HAPPY4);
 767	} else {
 768		input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
 769		input_set_capability(input, EV_KEY, BTN_GEAR_UP);
 770	}
 771
 
 
 772	input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
 773	input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
 774
 
 
 
 775	input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
 776			STEAM_PAD_FUZZ, 0);
 777	input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
 778			STEAM_PAD_FUZZ, 0);
 779
 780	if (steam->quirks & STEAM_QUIRK_DECK) {
 781		input_set_abs_params(input, ABS_HAT2Y, 0, 32767, 0, 0);
 782		input_set_abs_params(input, ABS_HAT2X, 0, 32767, 0, 0);
 783
 784		input_set_abs_params(input, ABS_RX, -32767, 32767, 0, 0);
 785		input_set_abs_params(input, ABS_RY, -32767, 32767, 0, 0);
 786
 787		input_set_abs_params(input, ABS_HAT1X, -32767, 32767,
 788				STEAM_PAD_FUZZ, 0);
 789		input_set_abs_params(input, ABS_HAT1Y, -32767, 32767,
 790				STEAM_PAD_FUZZ, 0);
 791
 792		input_abs_set_res(input, ABS_X, STEAM_DECK_JOYSTICK_RESOLUTION);
 793		input_abs_set_res(input, ABS_Y, STEAM_DECK_JOYSTICK_RESOLUTION);
 794		input_abs_set_res(input, ABS_RX, STEAM_DECK_JOYSTICK_RESOLUTION);
 795		input_abs_set_res(input, ABS_RY, STEAM_DECK_JOYSTICK_RESOLUTION);
 796		input_abs_set_res(input, ABS_HAT1X, STEAM_PAD_RESOLUTION);
 797		input_abs_set_res(input, ABS_HAT1Y, STEAM_PAD_RESOLUTION);
 798		input_abs_set_res(input, ABS_HAT2Y, STEAM_DECK_TRIGGER_RESOLUTION);
 799		input_abs_set_res(input, ABS_HAT2X, STEAM_DECK_TRIGGER_RESOLUTION);
 800	} else {
 801		input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
 802		input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
 803
 804		input_set_abs_params(input, ABS_RX, -32767, 32767,
 805				STEAM_PAD_FUZZ, 0);
 806		input_set_abs_params(input, ABS_RY, -32767, 32767,
 807				STEAM_PAD_FUZZ, 0);
 808
 809		input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
 810		input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
 811		input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
 812		input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
 813		input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
 814		input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
 815	}
 816	input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
 817	input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
 818
 819#ifdef CONFIG_STEAM_FF
 820	if (steam->quirks & STEAM_QUIRK_DECK) {
 821		input_set_capability(input, EV_FF, FF_RUMBLE);
 822		ret = input_ff_create_memless(input, NULL, steam_play_effect);
 823		if (ret)
 824			goto input_register_fail;
 825	}
 826#endif
 827
 828	ret = input_register_device(input);
 829	if (ret)
 830		goto input_register_fail;
 831
 832	rcu_assign_pointer(steam->input, input);
 833	return 0;
 834
 835input_register_fail:
 836	input_free_device(input);
 837	return ret;
 838}
 839
 840static int steam_sensors_register(struct steam_device *steam)
 841{
 842	struct hid_device *hdev = steam->hdev;
 843	struct input_dev *sensors;
 844	int ret;
 845
 846	if (!(steam->quirks & STEAM_QUIRK_DECK))
 847		return 0;
 848
 849	rcu_read_lock();
 850	sensors = rcu_dereference(steam->sensors);
 851	rcu_read_unlock();
 852	if (sensors) {
 853		dbg_hid("%s: already connected\n", __func__);
 854		return 0;
 855	}
 856
 857	sensors = input_allocate_device();
 858	if (!sensors)
 859		return -ENOMEM;
 860
 861	input_set_drvdata(sensors, steam);
 862	sensors->dev.parent = &hdev->dev;
 863
 864	sensors->name = "Steam Deck Motion Sensors";
 865	sensors->phys = hdev->phys;
 866	sensors->uniq = steam->serial_no;
 867	sensors->id.bustype = hdev->bus;
 868	sensors->id.vendor = hdev->vendor;
 869	sensors->id.product = hdev->product;
 870	sensors->id.version = hdev->version;
 871
 872	__set_bit(INPUT_PROP_ACCELEROMETER, sensors->propbit);
 873	__set_bit(EV_MSC, sensors->evbit);
 874	__set_bit(MSC_TIMESTAMP, sensors->mscbit);
 875
 876	input_set_abs_params(sensors, ABS_X, -STEAM_DECK_ACCEL_RANGE,
 877			STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
 878	input_set_abs_params(sensors, ABS_Y, -STEAM_DECK_ACCEL_RANGE,
 879			STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
 880	input_set_abs_params(sensors, ABS_Z, -STEAM_DECK_ACCEL_RANGE,
 881			STEAM_DECK_ACCEL_RANGE, STEAM_DECK_ACCEL_FUZZ, 0);
 882	input_abs_set_res(sensors, ABS_X, STEAM_DECK_ACCEL_RES_PER_G);
 883	input_abs_set_res(sensors, ABS_Y, STEAM_DECK_ACCEL_RES_PER_G);
 884	input_abs_set_res(sensors, ABS_Z, STEAM_DECK_ACCEL_RES_PER_G);
 885
 886	input_set_abs_params(sensors, ABS_RX, -STEAM_DECK_GYRO_RANGE,
 887			STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
 888	input_set_abs_params(sensors, ABS_RY, -STEAM_DECK_GYRO_RANGE,
 889			STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
 890	input_set_abs_params(sensors, ABS_RZ, -STEAM_DECK_GYRO_RANGE,
 891			STEAM_DECK_GYRO_RANGE, STEAM_DECK_GYRO_FUZZ, 0);
 892	input_abs_set_res(sensors, ABS_RX, STEAM_DECK_GYRO_RES_PER_DPS);
 893	input_abs_set_res(sensors, ABS_RY, STEAM_DECK_GYRO_RES_PER_DPS);
 894	input_abs_set_res(sensors, ABS_RZ, STEAM_DECK_GYRO_RES_PER_DPS);
 895
 896	ret = input_register_device(sensors);
 897	if (ret)
 898		goto sensors_register_fail;
 899
 900	rcu_assign_pointer(steam->sensors, sensors);
 901	return 0;
 902
 903sensors_register_fail:
 904	input_free_device(sensors);
 905	return ret;
 906}
 907
 908static void steam_input_unregister(struct steam_device *steam)
 909{
 910	struct input_dev *input;
 911	rcu_read_lock();
 912	input = rcu_dereference(steam->input);
 913	rcu_read_unlock();
 914	if (!input)
 915		return;
 916	RCU_INIT_POINTER(steam->input, NULL);
 917	synchronize_rcu();
 918	input_unregister_device(input);
 919}
 920
 921static void steam_sensors_unregister(struct steam_device *steam)
 922{
 923	struct input_dev *sensors;
 924
 925	if (!(steam->quirks & STEAM_QUIRK_DECK))
 926		return;
 927
 928	rcu_read_lock();
 929	sensors = rcu_dereference(steam->sensors);
 930	rcu_read_unlock();
 931
 932	if (!sensors)
 933		return;
 934	RCU_INIT_POINTER(steam->sensors, NULL);
 935	synchronize_rcu();
 936	input_unregister_device(sensors);
 937}
 938
 939static void steam_battery_unregister(struct steam_device *steam)
 940{
 941	struct power_supply *battery;
 942
 943	rcu_read_lock();
 944	battery = rcu_dereference(steam->battery);
 945	rcu_read_unlock();
 946
 947	if (!battery)
 948		return;
 949	RCU_INIT_POINTER(steam->battery, NULL);
 950	synchronize_rcu();
 951	power_supply_unregister(battery);
 952}
 953
 954static int steam_register(struct steam_device *steam)
 955{
 956	int ret;
 957	unsigned long client_opened;
 958	unsigned long flags;
 959
 960	/*
 961	 * This function can be called several times in a row with the
 962	 * wireless adaptor, without steam_unregister() between them, because
 963	 * another client send a get_connection_status command, for example.
 964	 * The battery and serial number are set just once per device.
 965	 */
 966	if (!steam->serial_no[0]) {
 967		/*
 968		 * Unlikely, but getting the serial could fail, and it is not so
 969		 * important, so make up a serial number and go on.
 970		 */
 
 971		if (steam_get_serial(steam) < 0)
 972			strscpy(steam->serial_no, "XXXXXXXXXX",
 973					sizeof(steam->serial_no));
 
 974
 975		hid_info(steam->hdev, "Steam Controller '%s' connected",
 976				steam->serial_no);
 977
 978		/* ignore battery errors, we can live without it */
 979		if (steam->quirks & STEAM_QUIRK_WIRELESS)
 980			steam_battery_register(steam);
 981
 982		mutex_lock(&steam_devices_lock);
 983		if (list_empty(&steam->list))
 984			list_add(&steam->list, &steam_devices);
 985		mutex_unlock(&steam_devices_lock);
 986	}
 987
 988	spin_lock_irqsave(&steam->lock, flags);
 989	client_opened = steam->client_opened;
 990	spin_unlock_irqrestore(&steam->lock, flags);
 
 
 991
 992	if (!client_opened) {
 993		steam_set_lizard_mode(steam, lizard_mode);
 994		ret = steam_input_register(steam);
 995		if (ret != 0)
 996			goto steam_register_input_fail;
 997		ret = steam_sensors_register(steam);
 998		if (ret != 0)
 999			goto steam_register_sensors_fail;
1000	}
1001	return 0;
1002
1003steam_register_sensors_fail:
1004	steam_input_unregister(steam);
1005steam_register_input_fail:
1006	return ret;
1007}
1008
1009static void steam_unregister(struct steam_device *steam)
1010{
1011	steam_battery_unregister(steam);
1012	steam_sensors_unregister(steam);
1013	steam_input_unregister(steam);
1014	if (steam->serial_no[0]) {
1015		hid_info(steam->hdev, "Steam Controller '%s' disconnected",
1016				steam->serial_no);
1017		mutex_lock(&steam_devices_lock);
1018		list_del_init(&steam->list);
1019		mutex_unlock(&steam_devices_lock);
1020		steam->serial_no[0] = 0;
1021	}
1022}
1023
1024static void steam_work_connect_cb(struct work_struct *work)
1025{
1026	struct steam_device *steam = container_of(work, struct steam_device,
1027							work_connect);
1028	unsigned long flags;
1029	bool connected;
1030	int ret;
1031
1032	spin_lock_irqsave(&steam->lock, flags);
1033	connected = steam->connected;
1034	spin_unlock_irqrestore(&steam->lock, flags);
1035
1036	if (connected) {
1037		ret = steam_register(steam);
1038		if (ret) {
1039			hid_err(steam->hdev,
1040				"%s:steam_register failed with error %d\n",
1041				__func__, ret);
1042		}
1043	} else {
1044		steam_unregister(steam);
1045	}
1046}
1047
1048static void steam_mode_switch_cb(struct work_struct *work)
1049{
1050	struct steam_device *steam = container_of(to_delayed_work(work),
1051							struct steam_device, mode_switch);
1052	unsigned long flags;
1053	bool client_opened;
1054	steam->gamepad_mode = !steam->gamepad_mode;
1055	if (!lizard_mode)
1056		return;
1057
1058	if (steam->gamepad_mode)
1059		steam_set_lizard_mode(steam, false);
1060	else {
1061		spin_lock_irqsave(&steam->lock, flags);
1062		client_opened = steam->client_opened;
1063		spin_unlock_irqrestore(&steam->lock, flags);
1064		if (!client_opened)
1065			steam_set_lizard_mode(steam, lizard_mode);
1066	}
1067
1068	steam_haptic_pulse(steam, STEAM_PAD_RIGHT, 0x190, 0, 1, 0);
1069	if (steam->gamepad_mode) {
1070		steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x14D, 0x14D, 0x2D, 0);
1071	} else {
1072		steam_haptic_pulse(steam, STEAM_PAD_LEFT, 0x1F4, 0x1F4, 0x1E, 0);
1073	}
1074}
1075
1076static void steam_work_unregister_cb(struct work_struct *work)
1077{
1078	struct steam_device *steam = container_of(work, struct steam_device,
1079							unregister_work);
1080	unsigned long flags;
1081	bool connected;
1082	bool opened;
1083
1084	spin_lock_irqsave(&steam->lock, flags);
1085	opened = steam->client_opened;
1086	connected = steam->connected;
1087	spin_unlock_irqrestore(&steam->lock, flags);
1088
1089	if (connected) {
1090		if (opened) {
1091			steam_sensors_unregister(steam);
1092			steam_input_unregister(steam);
1093		} else {
1094			steam_set_lizard_mode(steam, lizard_mode);
1095			steam_input_register(steam);
1096			steam_sensors_register(steam);
1097		}
1098	}
1099}
1100
1101static bool steam_is_valve_interface(struct hid_device *hdev)
1102{
1103	struct hid_report_enum *rep_enum;
1104
1105	/*
1106	 * The wired device creates 3 interfaces:
1107	 *  0: emulated mouse.
1108	 *  1: emulated keyboard.
1109	 *  2: the real game pad.
1110	 * The wireless device creates 5 interfaces:
1111	 *  0: emulated keyboard.
1112	 *  1-4: slots where up to 4 real game pads will be connected to.
1113	 * We know which one is the real gamepad interface because they are the
1114	 * only ones with a feature report.
1115	 */
1116	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
1117	return !list_empty(&rep_enum->report_list);
1118}
1119
1120static int steam_client_ll_parse(struct hid_device *hdev)
1121{
1122	struct steam_device *steam = hdev->driver_data;
1123
1124	return hid_parse_report(hdev, steam->hdev->dev_rdesc,
1125			steam->hdev->dev_rsize);
1126}
1127
1128static int steam_client_ll_start(struct hid_device *hdev)
1129{
1130	return 0;
1131}
1132
1133static void steam_client_ll_stop(struct hid_device *hdev)
1134{
1135}
1136
1137static int steam_client_ll_open(struct hid_device *hdev)
1138{
1139	struct steam_device *steam = hdev->driver_data;
1140	unsigned long flags;
1141
1142	spin_lock_irqsave(&steam->lock, flags);
1143	steam->client_opened++;
1144	spin_unlock_irqrestore(&steam->lock, flags);
1145
1146	schedule_work(&steam->unregister_work);
1147
1148	return 0;
1149}
1150
1151static void steam_client_ll_close(struct hid_device *hdev)
1152{
1153	struct steam_device *steam = hdev->driver_data;
1154
1155	unsigned long flags;
1156	bool connected;
1157
1158	spin_lock_irqsave(&steam->lock, flags);
1159	steam->client_opened--;
1160	connected = steam->connected && !steam->client_opened;
1161	spin_unlock_irqrestore(&steam->lock, flags);
1162
1163	schedule_work(&steam->unregister_work);
 
 
 
 
 
 
 
1164}
1165
1166static int steam_client_ll_raw_request(struct hid_device *hdev,
1167				unsigned char reportnum, u8 *buf,
1168				size_t count, unsigned char report_type,
1169				int reqtype)
1170{
1171	struct steam_device *steam = hdev->driver_data;
1172
1173	return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
1174			report_type, reqtype);
1175}
1176
1177static const struct hid_ll_driver steam_client_ll_driver = {
1178	.parse = steam_client_ll_parse,
1179	.start = steam_client_ll_start,
1180	.stop = steam_client_ll_stop,
1181	.open = steam_client_ll_open,
1182	.close = steam_client_ll_close,
1183	.raw_request = steam_client_ll_raw_request,
1184};
1185
1186static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
1187{
1188	struct hid_device *client_hdev;
1189
1190	client_hdev = hid_allocate_device();
1191	if (IS_ERR(client_hdev))
1192		return client_hdev;
1193
1194	client_hdev->ll_driver = &steam_client_ll_driver;
1195	client_hdev->dev.parent = hdev->dev.parent;
1196	client_hdev->bus = hdev->bus;
1197	client_hdev->vendor = hdev->vendor;
1198	client_hdev->product = hdev->product;
1199	client_hdev->version = hdev->version;
1200	client_hdev->type = hdev->type;
1201	client_hdev->country = hdev->country;
1202	strscpy(client_hdev->name, hdev->name,
1203			sizeof(client_hdev->name));
1204	strscpy(client_hdev->phys, hdev->phys,
1205			sizeof(client_hdev->phys));
1206	/*
1207	 * Since we use the same device info than the real interface to
1208	 * trick userspace, we will be calling steam_probe recursively.
1209	 * We need to recognize the client interface somehow.
1210	 */
1211	client_hdev->group = HID_GROUP_STEAM;
1212	return client_hdev;
1213}
1214
1215static int steam_probe(struct hid_device *hdev,
1216				const struct hid_device_id *id)
1217{
1218	struct steam_device *steam;
1219	int ret;
1220
1221	ret = hid_parse(hdev);
1222	if (ret) {
1223		hid_err(hdev,
1224			"%s:parse of hid interface failed\n", __func__);
1225		return ret;
1226	}
1227
1228	/*
1229	 * The virtual client_dev is only used for hidraw.
1230	 * Also avoid the recursive probe.
1231	 */
1232	if (hdev->group == HID_GROUP_STEAM)
1233		return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1234	/*
1235	 * The non-valve interfaces (mouse and keyboard emulation) are
1236	 * connected without changes.
1237	 */
1238	if (!steam_is_valve_interface(hdev))
1239		return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1240
1241	steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
1242	if (!steam)
1243		return -ENOMEM;
1244
 
1245	steam->hdev = hdev;
1246	hid_set_drvdata(hdev, steam);
1247	spin_lock_init(&steam->lock);
1248	mutex_init(&steam->report_mutex);
1249	steam->quirks = id->driver_data;
1250	INIT_WORK(&steam->work_connect, steam_work_connect_cb);
1251	INIT_DELAYED_WORK(&steam->mode_switch, steam_mode_switch_cb);
1252	INIT_LIST_HEAD(&steam->list);
1253	INIT_WORK(&steam->rumble_work, steam_haptic_rumble_cb);
1254	steam->sensor_timestamp_us = 0;
1255	INIT_WORK(&steam->unregister_work, steam_work_unregister_cb);
 
 
 
 
1256
1257	/*
1258	 * With the real steam controller interface, do not connect hidraw.
1259	 * Instead, create the client_hid and connect that.
1260	 */
1261	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
1262	if (ret)
1263		goto err_cancel_work;
 
 
 
 
1264
1265	ret = hid_hw_open(hdev);
1266	if (ret) {
1267		hid_err(hdev,
1268			"%s:hid_hw_open\n",
1269			__func__);
1270		goto err_hw_stop;
1271	}
1272
1273	if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1274		hid_info(hdev, "Steam wireless receiver connected");
1275		/* If using a wireless adaptor ask for connection status */
1276		steam->connected = false;
1277		steam_request_conn_status(steam);
1278	} else {
1279		/* A wired connection is always present */
1280		steam->connected = true;
1281		ret = steam_register(steam);
1282		if (ret) {
1283			hid_err(hdev,
1284				"%s:steam_register failed with error %d\n",
1285				__func__, ret);
1286			goto err_hw_close;
1287		}
1288	}
1289
1290	steam->client_hdev = steam_create_client_hid(hdev);
1291	if (IS_ERR(steam->client_hdev)) {
1292		ret = PTR_ERR(steam->client_hdev);
1293		goto err_steam_unregister;
1294	}
1295	steam->client_hdev->driver_data = steam;
1296
1297	ret = hid_add_device(steam->client_hdev);
1298	if (ret)
1299		goto err_destroy;
1300
1301	return 0;
1302
1303err_destroy:
 
 
 
 
1304	hid_destroy_device(steam->client_hdev);
1305err_steam_unregister:
1306	if (steam->connected)
1307		steam_unregister(steam);
1308err_hw_close:
1309	hid_hw_close(hdev);
1310err_hw_stop:
1311	hid_hw_stop(hdev);
1312err_cancel_work:
1313	cancel_work_sync(&steam->work_connect);
1314	cancel_delayed_work_sync(&steam->mode_switch);
1315	cancel_work_sync(&steam->rumble_work);
1316	cancel_work_sync(&steam->unregister_work);
1317
1318	return ret;
1319}
1320
1321static void steam_remove(struct hid_device *hdev)
1322{
1323	struct steam_device *steam = hid_get_drvdata(hdev);
1324
1325	if (!steam || hdev->group == HID_GROUP_STEAM) {
1326		hid_hw_stop(hdev);
1327		return;
1328	}
1329
1330	hid_destroy_device(steam->client_hdev);
1331	cancel_delayed_work_sync(&steam->mode_switch);
1332	cancel_work_sync(&steam->work_connect);
1333	cancel_work_sync(&steam->rumble_work);
1334	cancel_work_sync(&steam->unregister_work);
1335	steam->client_hdev = NULL;
1336	steam->client_opened = 0;
1337	if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1338		hid_info(hdev, "Steam wireless receiver disconnected");
1339	}
1340	hid_hw_close(hdev);
1341	hid_hw_stop(hdev);
1342	steam_unregister(steam);
1343}
1344
1345static void steam_do_connect_event(struct steam_device *steam, bool connected)
1346{
1347	unsigned long flags;
1348	bool changed;
1349
1350	spin_lock_irqsave(&steam->lock, flags);
1351	changed = steam->connected != connected;
1352	steam->connected = connected;
1353	spin_unlock_irqrestore(&steam->lock, flags);
1354
1355	if (changed && schedule_work(&steam->work_connect) == 0)
1356		dbg_hid("%s: connected=%d event already queued\n",
1357				__func__, connected);
1358}
1359
1360/*
1361 * Some input data in the protocol has the opposite sign.
1362 * Clamp the values to 32767..-32767 so that the range is
1363 * symmetrical and can be negated safely.
1364 */
1365static inline s16 steam_le16(u8 *data)
1366{
1367	s16 x = (s16) le16_to_cpup((__le16 *)data);
1368
1369	return x == -32768 ? -32767 : x;
1370}
1371
1372/*
1373 * The size for this message payload is 60.
1374 * The known values are:
1375 *  (* values are not sent through wireless)
1376 *  (* accelerator/gyro is disabled by default)
1377 *  Offset| Type  | Mapped to |Meaning
1378 * -------+-------+-----------+--------------------------
1379 *  4-7   | u32   | --        | sequence number
1380 *  8-10  | 24bit | see below | buttons
1381 *  11    | u8    | ABS_HAT2Y | left trigger
1382 *  12    | u8    | ABS_HAT2X | right trigger
1383 *  13-15 | --    | --        | always 0
1384 *  16-17 | s16   | ABS_X/ABS_HAT0X     | X value
1385 *  18-19 | s16   | ABS_Y/ABS_HAT0Y     | Y value
1386 *  20-21 | s16   | ABS_RX    | right-pad X value
1387 *  22-23 | s16   | ABS_RY    | right-pad Y value
1388 *  24-25 | s16   | --        | * left trigger
1389 *  26-27 | s16   | --        | * right trigger
1390 *  28-29 | s16   | --        | * accelerometer X value
1391 *  30-31 | s16   | --        | * accelerometer Y value
1392 *  32-33 | s16   | --        | * accelerometer Z value
1393 *  34-35 | s16   | --        | gyro X value
1394 *  36-36 | s16   | --        | gyro Y value
1395 *  38-39 | s16   | --        | gyro Z value
1396 *  40-41 | s16   | --        | quaternion W value
1397 *  42-43 | s16   | --        | quaternion X value
1398 *  44-45 | s16   | --        | quaternion Y value
1399 *  46-47 | s16   | --        | quaternion Z value
1400 *  48-49 | --    | --        | always 0
1401 *  50-51 | s16   | --        | * left trigger (uncalibrated)
1402 *  52-53 | s16   | --        | * right trigger (uncalibrated)
1403 *  54-55 | s16   | --        | * joystick X value (uncalibrated)
1404 *  56-57 | s16   | --        | * joystick Y value (uncalibrated)
1405 *  58-59 | s16   | --        | * left-pad X value
1406 *  60-61 | s16   | --        | * left-pad Y value
1407 *  62-63 | u16   | --        | * battery voltage
1408 *
1409 * The buttons are:
1410 *  Bit  | Mapped to  | Description
1411 * ------+------------+--------------------------------
1412 *  8.0  | BTN_TR2    | right trigger fully pressed
1413 *  8.1  | BTN_TL2    | left trigger fully pressed
1414 *  8.2  | BTN_TR     | right shoulder
1415 *  8.3  | BTN_TL     | left shoulder
1416 *  8.4  | BTN_Y      | button Y
1417 *  8.5  | BTN_B      | button B
1418 *  8.6  | BTN_X      | button X
1419 *  8.7  | BTN_A      | button A
1420 *  9.0  | BTN_DPAD_UP    | left-pad up
1421 *  9.1  | BTN_DPAD_RIGHT | left-pad right
1422 *  9.2  | BTN_DPAD_LEFT  | left-pad left
1423 *  9.3  | BTN_DPAD_DOWN  | left-pad down
1424 *  9.4  | BTN_SELECT | menu left
1425 *  9.5  | BTN_MODE   | steam logo
1426 *  9.6  | BTN_START  | menu right
1427 *  9.7  | BTN_GEAR_DOWN | left back lever
1428 * 10.0  | BTN_GEAR_UP   | right back lever
1429 * 10.1  | --         | left-pad clicked
1430 * 10.2  | BTN_THUMBR | right-pad clicked
1431 * 10.3  | BTN_THUMB  | left-pad touched (but see explanation below)
1432 * 10.4  | BTN_THUMB2 | right-pad touched
1433 * 10.5  | --         | unknown
1434 * 10.6  | BTN_THUMBL | joystick clicked
1435 * 10.7  | --         | lpad_and_joy
1436 */
1437
1438static void steam_do_input_event(struct steam_device *steam,
1439		struct input_dev *input, u8 *data)
1440{
1441	/* 24 bits of buttons */
1442	u8 b8, b9, b10;
1443	s16 x, y;
1444	bool lpad_touched, lpad_and_joy;
1445
1446	b8 = data[8];
1447	b9 = data[9];
1448	b10 = data[10];
1449
1450	input_report_abs(input, ABS_HAT2Y, data[11]);
1451	input_report_abs(input, ABS_HAT2X, data[12]);
1452
1453	/*
1454	 * These two bits tells how to interpret the values X and Y.
1455	 * lpad_and_joy tells that the joystick and the lpad are used at the
1456	 * same time.
1457	 * lpad_touched tells whether X/Y are to be read as lpad coord or
1458	 * joystick values.
1459	 * (lpad_touched || lpad_and_joy) tells if the lpad is really touched.
1460	 */
1461	lpad_touched = b10 & BIT(3);
1462	lpad_and_joy = b10 & BIT(7);
1463	x = steam_le16(data + 16);
1464	y = -steam_le16(data + 18);
1465
1466	input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
1467	input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
1468	/* Check if joystick is centered */
1469	if (lpad_touched && !lpad_and_joy) {
1470		input_report_abs(input, ABS_X, 0);
1471		input_report_abs(input, ABS_Y, 0);
1472	}
1473	/* Check if lpad is untouched */
1474	if (!(lpad_touched || lpad_and_joy)) {
1475		input_report_abs(input, ABS_HAT0X, 0);
1476		input_report_abs(input, ABS_HAT0Y, 0);
1477	}
1478
1479	input_report_abs(input, ABS_RX, steam_le16(data + 20));
1480	input_report_abs(input, ABS_RY, -steam_le16(data + 22));
1481
1482	input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1483	input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1484	input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1485	input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1486	input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1487	input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1488	input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1489	input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1490	input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1491	input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1492	input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1493	input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
1494	input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
1495	input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
1496	input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1497	input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
1498	input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
1499	input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1500	input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1501	input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1502	input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1503
1504	input_sync(input);
1505}
1506
1507/*
1508 * The size for this message payload is 56.
1509 * The known values are:
1510 *  Offset| Type  | Mapped to |Meaning
1511 * -------+-------+-----------+--------------------------
1512 *  4-7   | u32   | --        | sequence number
1513 *  8-15  | u64   | see below | buttons
1514 *  16-17 | s16   | ABS_HAT0X | left-pad X value
1515 *  18-19 | s16   | ABS_HAT0Y | left-pad Y value
1516 *  20-21 | s16   | ABS_HAT1X | right-pad X value
1517 *  22-23 | s16   | ABS_HAT1Y | right-pad Y value
1518 *  24-25 | s16   | IMU ABS_X | accelerometer X value
1519 *  26-27 | s16   | IMU ABS_Z | accelerometer Y value
1520 *  28-29 | s16   | IMU ABS_Y | accelerometer Z value
1521 *  30-31 | s16   | IMU ABS_RX | gyro X value
1522 *  32-33 | s16   | IMU ABS_RZ | gyro Y value
1523 *  34-35 | s16   | IMU ABS_RY | gyro Z value
1524 *  36-37 | s16   | --        | quaternion W value
1525 *  38-39 | s16   | --        | quaternion X value
1526 *  40-41 | s16   | --        | quaternion Y value
1527 *  42-43 | s16   | --        | quaternion Z value
1528 *  44-45 | u16   | ABS_HAT2Y | left trigger (uncalibrated)
1529 *  46-47 | u16   | ABS_HAT2X | right trigger (uncalibrated)
1530 *  48-49 | s16   | ABS_X     | left joystick X
1531 *  50-51 | s16   | ABS_Y     | left joystick Y
1532 *  52-53 | s16   | ABS_RX    | right joystick X
1533 *  54-55 | s16   | ABS_RY    | right joystick Y
1534 *  56-57 | u16   | --        | left pad pressure
1535 *  58-59 | u16   | --        | right pad pressure
1536 *
1537 * The buttons are:
1538 *  Bit  | Mapped to  | Description
1539 * ------+------------+--------------------------------
1540 *  8.0  | BTN_TR2    | right trigger fully pressed
1541 *  8.1  | BTN_TL2    | left trigger fully pressed
1542 *  8.2  | BTN_TR     | right shoulder
1543 *  8.3  | BTN_TL     | left shoulder
1544 *  8.4  | BTN_Y      | button Y
1545 *  8.5  | BTN_B      | button B
1546 *  8.6  | BTN_X      | button X
1547 *  8.7  | BTN_A      | button A
1548 *  9.0  | BTN_DPAD_UP    | left-pad up
1549 *  9.1  | BTN_DPAD_RIGHT | left-pad right
1550 *  9.2  | BTN_DPAD_LEFT  | left-pad left
1551 *  9.3  | BTN_DPAD_DOWN  | left-pad down
1552 *  9.4  | BTN_SELECT | menu left
1553 *  9.5  | BTN_MODE   | steam logo
1554 *  9.6  | BTN_START  | menu right
1555 *  9.7  | BTN_TRIGGER_HAPPY3 | left bottom grip button
1556 *  10.0 | BTN_TRIGGER_HAPPY4 | right bottom grip button
1557 *  10.1 | BTN_THUMB  | left pad pressed
1558 *  10.2 | BTN_THUMB2 | right pad pressed
1559 *  10.3 | --         | left pad touched
1560 *  10.4 | --         | right pad touched
1561 *  10.5 | --         | unknown
1562 *  10.6 | BTN_THUMBL | left joystick clicked
1563 *  10.7 | --         | unknown
1564 *  11.0 | --         | unknown
1565 *  11.1 | --         | unknown
1566 *  11.2 | BTN_THUMBR | right joystick clicked
1567 *  11.3 | --         | unknown
1568 *  11.4 | --         | unknown
1569 *  11.5 | --         | unknown
1570 *  11.6 | --         | unknown
1571 *  11.7 | --         | unknown
1572 *  12.0 | --         | unknown
1573 *  12.1 | --         | unknown
1574 *  12.2 | --         | unknown
1575 *  12.3 | --         | unknown
1576 *  12.4 | --         | unknown
1577 *  12.5 | --         | unknown
1578 *  12.6 | --         | unknown
1579 *  12.7 | --         | unknown
1580 *  13.0 | --         | unknown
1581 *  13.1 | BTN_TRIGGER_HAPPY1 | left top grip button
1582 *  13.2 | BTN_TRIGGER_HAPPY2 | right top grip button
1583 *  13.3 | --         | unknown
1584 *  13.4 | --         | unknown
1585 *  13.5 | --         | unknown
1586 *  13.6 | --         | left joystick touched
1587 *  13.7 | --         | right joystick touched
1588 *  14.0 | --         | unknown
1589 *  14.1 | --         | unknown
1590 *  14.2 | BTN_BASE   | quick access button
1591 *  14.3 | --         | unknown
1592 *  14.4 | --         | unknown
1593 *  14.5 | --         | unknown
1594 *  14.6 | --         | unknown
1595 *  14.7 | --         | unknown
1596 *  15.0 | --         | unknown
1597 *  15.1 | --         | unknown
1598 *  15.2 | --         | unknown
1599 *  15.3 | --         | unknown
1600 *  15.4 | --         | unknown
1601 *  15.5 | --         | unknown
1602 *  15.6 | --         | unknown
1603 *  15.7 | --         | unknown
1604 */
1605static void steam_do_deck_input_event(struct steam_device *steam,
1606		struct input_dev *input, u8 *data)
1607{
1608	u8 b8, b9, b10, b11, b13, b14;
1609	bool lpad_touched, rpad_touched;
1610
1611	b8 = data[8];
1612	b9 = data[9];
1613	b10 = data[10];
1614	b11 = data[11];
1615	b13 = data[13];
1616	b14 = data[14];
1617
1618	if (!(b9 & BIT(6)) && steam->did_mode_switch) {
1619		steam->did_mode_switch = false;
1620		cancel_delayed_work(&steam->mode_switch);
1621	} else if (!steam->client_opened && (b9 & BIT(6)) && !steam->did_mode_switch) {
1622		steam->did_mode_switch = true;
1623		schedule_delayed_work(&steam->mode_switch, 45 * HZ / 100);
1624	}
1625
1626	if (!steam->gamepad_mode)
1627		return;
1628
1629	lpad_touched = b10 & BIT(3);
1630	rpad_touched = b10 & BIT(4);
1631
1632	if (lpad_touched) {
1633		input_report_abs(input, ABS_HAT0X, steam_le16(data + 16));
1634		input_report_abs(input, ABS_HAT0Y, steam_le16(data + 18));
1635	} else {
1636		input_report_abs(input, ABS_HAT0X, 0);
1637		input_report_abs(input, ABS_HAT0Y, 0);
1638	}
1639
1640	if (rpad_touched) {
1641		input_report_abs(input, ABS_HAT1X, steam_le16(data + 20));
1642		input_report_abs(input, ABS_HAT1Y, steam_le16(data + 22));
1643	} else {
1644		input_report_abs(input, ABS_HAT1X, 0);
1645		input_report_abs(input, ABS_HAT1Y, 0);
1646	}
1647
1648	input_report_abs(input, ABS_X, steam_le16(data + 48));
1649	input_report_abs(input, ABS_Y, -steam_le16(data + 50));
1650	input_report_abs(input, ABS_RX, steam_le16(data + 52));
1651	input_report_abs(input, ABS_RY, -steam_le16(data + 54));
1652
1653	input_report_abs(input, ABS_HAT2Y, steam_le16(data + 44));
1654	input_report_abs(input, ABS_HAT2X, steam_le16(data + 46));
1655
1656	input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
1657	input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
1658	input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
1659	input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
1660	input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
1661	input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
1662	input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
1663	input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
1664	input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
1665	input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
1666	input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
1667	input_event(input, EV_KEY, BTN_TRIGGER_HAPPY3, !!(b9 & BIT(7)));
1668	input_event(input, EV_KEY, BTN_TRIGGER_HAPPY4, !!(b10 & BIT(0)));
1669	input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
1670	input_event(input, EV_KEY, BTN_THUMBR, !!(b11 & BIT(2)));
1671	input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
1672	input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
1673	input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
1674	input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
1675	input_event(input, EV_KEY, BTN_THUMB, !!(b10 & BIT(1)));
1676	input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(2)));
1677	input_event(input, EV_KEY, BTN_TRIGGER_HAPPY1, !!(b13 & BIT(1)));
1678	input_event(input, EV_KEY, BTN_TRIGGER_HAPPY2, !!(b13 & BIT(2)));
1679	input_event(input, EV_KEY, BTN_BASE, !!(b14 & BIT(2)));
1680
1681	input_sync(input);
1682}
1683
1684static void steam_do_deck_sensors_event(struct steam_device *steam,
1685		struct input_dev *sensors, u8 *data)
1686{
1687	/*
1688	 * The deck input report is received every 4 ms on average,
1689	 * with a jitter of +/- 4 ms even though the USB descriptor claims
1690	 * that it uses 1 kHz.
1691	 * Since the HID report does not include a sensor timestamp,
1692	 * use a fixed increment here.
1693	 */
1694	steam->sensor_timestamp_us += 4000;
1695
1696	if (!steam->gamepad_mode)
1697		return;
1698
1699	input_event(sensors, EV_MSC, MSC_TIMESTAMP, steam->sensor_timestamp_us);
1700	input_report_abs(sensors, ABS_X, steam_le16(data + 24));
1701	input_report_abs(sensors, ABS_Z, -steam_le16(data + 26));
1702	input_report_abs(sensors, ABS_Y, steam_le16(data + 28));
1703	input_report_abs(sensors, ABS_RX, steam_le16(data + 30));
1704	input_report_abs(sensors, ABS_RZ, -steam_le16(data + 32));
1705	input_report_abs(sensors, ABS_RY, steam_le16(data + 34));
1706
1707	input_sync(sensors);
1708}
1709
1710/*
1711 * The size for this message payload is 11.
1712 * The known values are:
1713 *  Offset| Type  | Meaning
1714 * -------+-------+---------------------------
1715 *  4-7   | u32   | sequence number
1716 *  8-11  | --    | always 0
1717 *  12-13 | u16   | voltage (mV)
1718 *  14    | u8    | battery percent
1719 */
1720static void steam_do_battery_event(struct steam_device *steam,
1721		struct power_supply *battery, u8 *data)
1722{
1723	unsigned long flags;
1724
1725	s16 volts = steam_le16(data + 12);
1726	u8 batt = data[14];
1727
1728	/* Creating the battery may have failed */
1729	rcu_read_lock();
1730	battery = rcu_dereference(steam->battery);
1731	if (likely(battery)) {
1732		spin_lock_irqsave(&steam->lock, flags);
1733		steam->voltage = volts;
1734		steam->battery_charge = batt;
1735		spin_unlock_irqrestore(&steam->lock, flags);
1736		power_supply_changed(battery);
1737	}
1738	rcu_read_unlock();
1739}
1740
1741static int steam_raw_event(struct hid_device *hdev,
1742			struct hid_report *report, u8 *data,
1743			int size)
1744{
1745	struct steam_device *steam = hid_get_drvdata(hdev);
1746	struct input_dev *input;
1747	struct input_dev *sensors;
1748	struct power_supply *battery;
1749
1750	if (!steam)
1751		return 0;
1752
1753	if (steam->client_opened)
1754		hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
1755				data, size, 0);
1756	/*
1757	 * All messages are size=64, all values little-endian.
1758	 * The format is:
1759	 *  Offset| Meaning
1760	 * -------+--------------------------------------------
1761	 *  0-1   | always 0x01, 0x00, maybe protocol version?
1762	 *  2     | type of message
1763	 *  3     | length of the real payload (not checked)
1764	 *  4-n   | payload data, depends on the type
1765	 *
1766	 * There are these known types of message:
1767	 *  0x01: input data (60 bytes)
1768	 *  0x03: wireless connect/disconnect (1 byte)
1769	 *  0x04: battery status (11 bytes)
1770	 *  0x09: Steam Deck input data (56 bytes)
1771	 */
1772
1773	if (size != 64 || data[0] != 1 || data[1] != 0)
1774		return 0;
1775
1776	switch (data[2]) {
1777	case ID_CONTROLLER_STATE:
1778		if (steam->client_opened)
1779			return 0;
1780		rcu_read_lock();
1781		input = rcu_dereference(steam->input);
1782		if (likely(input))
1783			steam_do_input_event(steam, input, data);
1784		rcu_read_unlock();
1785		break;
1786	case ID_CONTROLLER_DECK_STATE:
1787		if (steam->client_opened)
1788			return 0;
1789		rcu_read_lock();
1790		input = rcu_dereference(steam->input);
1791		if (likely(input))
1792			steam_do_deck_input_event(steam, input, data);
1793		sensors = rcu_dereference(steam->sensors);
1794		if (likely(sensors))
1795			steam_do_deck_sensors_event(steam, sensors, data);
1796		rcu_read_unlock();
1797		break;
1798	case ID_CONTROLLER_WIRELESS:
1799		/*
1800		 * The payload of this event is a single byte:
1801		 *  0x01: disconnected.
1802		 *  0x02: connected.
1803		 */
1804		switch (data[4]) {
1805		case 0x01:
1806			steam_do_connect_event(steam, false);
1807			break;
1808		case 0x02:
1809			steam_do_connect_event(steam, true);
1810			break;
1811		}
1812		break;
1813	case ID_CONTROLLER_STATUS:
1814		if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1815			rcu_read_lock();
1816			battery = rcu_dereference(steam->battery);
1817			if (likely(battery)) {
1818				steam_do_battery_event(steam, battery, data);
1819			} else {
1820				dbg_hid(
1821					"%s: battery data without connect event\n",
1822					__func__);
1823				steam_do_connect_event(steam, true);
1824			}
1825			rcu_read_unlock();
1826		}
1827		break;
1828	}
1829	return 0;
1830}
1831
1832static int steam_param_set_lizard_mode(const char *val,
1833					const struct kernel_param *kp)
1834{
1835	struct steam_device *steam;
1836	int ret;
1837
1838	ret = param_set_bool(val, kp);
1839	if (ret)
1840		return ret;
1841
1842	mutex_lock(&steam_devices_lock);
1843	list_for_each_entry(steam, &steam_devices, list) {
 
1844		if (!steam->client_opened)
1845			steam_set_lizard_mode(steam, lizard_mode);
 
1846	}
1847	mutex_unlock(&steam_devices_lock);
1848	return 0;
1849}
1850
1851static const struct kernel_param_ops steam_lizard_mode_ops = {
1852	.set	= steam_param_set_lizard_mode,
1853	.get	= param_get_bool,
1854};
1855
1856module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
1857MODULE_PARM_DESC(lizard_mode,
1858	"Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
1859
1860static const struct hid_device_id steam_controllers[] = {
1861	{ /* Wired Steam Controller */
1862	  HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1863		USB_DEVICE_ID_STEAM_CONTROLLER)
1864	},
1865	{ /* Wireless Steam Controller */
1866	  HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1867		USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
1868	  .driver_data = STEAM_QUIRK_WIRELESS
1869	},
1870	{ /* Steam Deck */
1871	  HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1872		USB_DEVICE_ID_STEAM_DECK),
1873	  .driver_data = STEAM_QUIRK_DECK
1874	},
1875	{}
1876};
1877
1878MODULE_DEVICE_TABLE(hid, steam_controllers);
1879
1880static struct hid_driver steam_controller_driver = {
1881	.name = "hid-steam",
1882	.id_table = steam_controllers,
1883	.probe = steam_probe,
1884	.remove = steam_remove,
1885	.raw_event = steam_raw_event,
1886};
1887
1888module_hid_driver(steam_controller_driver);