hid-steam.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. #include <linux/device.h>
  36. #include <linux/input.h>
  37. #include <linux/hid.h>
  38. #include <linux/module.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/mutex.h>
  41. #include <linux/rcupdate.h>
  42. #include <linux/delay.h>
  43. #include <linux/power_supply.h>
  44. #include "hid-ids.h"
  45. MODULE_LICENSE("GPL");
  46. MODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>");
  47. static bool lizard_mode = true;
  48. static DEFINE_MUTEX(steam_devices_lock);
  49. static LIST_HEAD(steam_devices);
  50. #define STEAM_QUIRK_WIRELESS BIT(0)
  51. /* Touch pads are 40 mm in diameter and 65535 units */
  52. #define STEAM_PAD_RESOLUTION 1638
  53. /* Trigger runs are about 5 mm and 256 units */
  54. #define STEAM_TRIGGER_RESOLUTION 51
  55. /* Joystick runs are about 5 mm and 256 units */
  56. #define STEAM_JOYSTICK_RESOLUTION 51
  57. #define STEAM_PAD_FUZZ 256
  58. /*
  59. * Commands that can be sent in a feature report.
  60. * Thanks to Valve for some valuable hints.
  61. */
  62. #define STEAM_CMD_SET_MAPPINGS 0x80
  63. #define STEAM_CMD_CLEAR_MAPPINGS 0x81
  64. #define STEAM_CMD_GET_MAPPINGS 0x82
  65. #define STEAM_CMD_GET_ATTRIB 0x83
  66. #define STEAM_CMD_GET_ATTRIB_LABEL 0x84
  67. #define STEAM_CMD_DEFAULT_MAPPINGS 0x85
  68. #define STEAM_CMD_FACTORY_RESET 0x86
  69. #define STEAM_CMD_WRITE_REGISTER 0x87
  70. #define STEAM_CMD_CLEAR_REGISTER 0x88
  71. #define STEAM_CMD_READ_REGISTER 0x89
  72. #define STEAM_CMD_GET_REGISTER_LABEL 0x8a
  73. #define STEAM_CMD_GET_REGISTER_MAX 0x8b
  74. #define STEAM_CMD_GET_REGISTER_DEFAULT 0x8c
  75. #define STEAM_CMD_SET_MODE 0x8d
  76. #define STEAM_CMD_DEFAULT_MOUSE 0x8e
  77. #define STEAM_CMD_FORCEFEEDBAK 0x8f
  78. #define STEAM_CMD_REQUEST_COMM_STATUS 0xb4
  79. #define STEAM_CMD_GET_SERIAL 0xae
  80. /* Some useful register ids */
  81. #define STEAM_REG_LPAD_MODE 0x07
  82. #define STEAM_REG_RPAD_MODE 0x08
  83. #define STEAM_REG_RPAD_MARGIN 0x18
  84. #define STEAM_REG_LED 0x2d
  85. #define STEAM_REG_GYRO_MODE 0x30
  86. /* Raw event identifiers */
  87. #define STEAM_EV_INPUT_DATA 0x01
  88. #define STEAM_EV_CONNECT 0x03
  89. #define STEAM_EV_BATTERY 0x04
  90. /* Values for GYRO_MODE (bitmask) */
  91. #define STEAM_GYRO_MODE_OFF 0x0000
  92. #define STEAM_GYRO_MODE_STEERING 0x0001
  93. #define STEAM_GYRO_MODE_TILT 0x0002
  94. #define STEAM_GYRO_MODE_SEND_ORIENTATION 0x0004
  95. #define STEAM_GYRO_MODE_SEND_RAW_ACCEL 0x0008
  96. #define STEAM_GYRO_MODE_SEND_RAW_GYRO 0x0010
  97. /* Other random constants */
  98. #define STEAM_SERIAL_LEN 10
  99. struct steam_device {
  100. struct list_head list;
  101. spinlock_t lock;
  102. struct hid_device *hdev, *client_hdev;
  103. struct mutex mutex;
  104. bool client_opened;
  105. struct input_dev __rcu *input;
  106. unsigned long quirks;
  107. struct work_struct work_connect;
  108. bool connected;
  109. char serial_no[STEAM_SERIAL_LEN + 1];
  110. struct power_supply_desc battery_desc;
  111. struct power_supply __rcu *battery;
  112. u8 battery_charge;
  113. u16 voltage;
  114. };
  115. static int steam_recv_report(struct steam_device *steam,
  116. u8 *data, int size)
  117. {
  118. struct hid_report *r;
  119. u8 *buf;
  120. int ret;
  121. r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
  122. if (hid_report_len(r) < 64)
  123. return -EINVAL;
  124. buf = hid_alloc_report_buf(r, GFP_KERNEL);
  125. if (!buf)
  126. return -ENOMEM;
  127. /*
  128. * The report ID is always 0, so strip the first byte from the output.
  129. * hid_report_len() is not counting the report ID, so +1 to the length
  130. * or else we get a EOVERFLOW. We are safe from a buffer overflow
  131. * because hid_alloc_report_buf() allocates +7 bytes.
  132. */
  133. ret = hid_hw_raw_request(steam->hdev, 0x00,
  134. buf, hid_report_len(r) + 1,
  135. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  136. if (ret > 0)
  137. memcpy(data, buf + 1, min(size, ret - 1));
  138. kfree(buf);
  139. return ret;
  140. }
  141. static int steam_send_report(struct steam_device *steam,
  142. u8 *cmd, int size)
  143. {
  144. struct hid_report *r;
  145. u8 *buf;
  146. unsigned int retries = 50;
  147. int ret;
  148. r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
  149. if (hid_report_len(r) < 64)
  150. return -EINVAL;
  151. buf = hid_alloc_report_buf(r, GFP_KERNEL);
  152. if (!buf)
  153. return -ENOMEM;
  154. /* The report ID is always 0 */
  155. memcpy(buf + 1, cmd, size);
  156. /*
  157. * Sometimes the wireless controller fails with EPIPE
  158. * when sending a feature report.
  159. * Doing a HID_REQ_GET_REPORT and waiting for a while
  160. * seems to fix that.
  161. */
  162. do {
  163. ret = hid_hw_raw_request(steam->hdev, 0,
  164. buf, size + 1,
  165. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  166. if (ret != -EPIPE)
  167. break;
  168. msleep(20);
  169. } while (--retries);
  170. kfree(buf);
  171. if (ret < 0)
  172. hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
  173. ret, size, cmd);
  174. return ret;
  175. }
  176. static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
  177. {
  178. return steam_send_report(steam, &cmd, 1);
  179. }
  180. static int steam_write_registers(struct steam_device *steam,
  181. /* u8 reg, u16 val */...)
  182. {
  183. /* Send: 0x87 len (reg valLo valHi)* */
  184. u8 reg;
  185. u16 val;
  186. u8 cmd[64] = {STEAM_CMD_WRITE_REGISTER, 0x00};
  187. va_list args;
  188. va_start(args, steam);
  189. for (;;) {
  190. reg = va_arg(args, int);
  191. if (reg == 0)
  192. break;
  193. val = va_arg(args, int);
  194. cmd[cmd[1] + 2] = reg;
  195. cmd[cmd[1] + 3] = val & 0xff;
  196. cmd[cmd[1] + 4] = val >> 8;
  197. cmd[1] += 3;
  198. }
  199. va_end(args);
  200. return steam_send_report(steam, cmd, 2 + cmd[1]);
  201. }
  202. static int steam_get_serial(struct steam_device *steam)
  203. {
  204. /*
  205. * Send: 0xae 0x15 0x01
  206. * Recv: 0xae 0x15 0x01 serialnumber (10 chars)
  207. */
  208. int ret;
  209. u8 cmd[] = {STEAM_CMD_GET_SERIAL, 0x15, 0x01};
  210. u8 reply[3 + STEAM_SERIAL_LEN + 1];
  211. ret = steam_send_report(steam, cmd, sizeof(cmd));
  212. if (ret < 0)
  213. return ret;
  214. ret = steam_recv_report(steam, reply, sizeof(reply));
  215. if (ret < 0)
  216. return ret;
  217. if (reply[0] != 0xae || reply[1] != 0x15 || reply[2] != 0x01)
  218. return -EIO;
  219. reply[3 + STEAM_SERIAL_LEN] = 0;
  220. strlcpy(steam->serial_no, reply + 3, sizeof(steam->serial_no));
  221. return 0;
  222. }
  223. /*
  224. * This command requests the wireless adaptor to post an event
  225. * with the connection status. Useful if this driver is loaded when
  226. * the controller is already connected.
  227. */
  228. static inline int steam_request_conn_status(struct steam_device *steam)
  229. {
  230. return steam_send_report_byte(steam, STEAM_CMD_REQUEST_COMM_STATUS);
  231. }
  232. static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
  233. {
  234. if (enable) {
  235. /* enable esc, enter, cursors */
  236. steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MAPPINGS);
  237. /* enable mouse */
  238. steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MOUSE);
  239. steam_write_registers(steam,
  240. STEAM_REG_RPAD_MARGIN, 0x01, /* enable margin */
  241. 0);
  242. } else {
  243. /* disable esc, enter, cursor */
  244. steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
  245. steam_write_registers(steam,
  246. STEAM_REG_RPAD_MODE, 0x07, /* disable mouse */
  247. STEAM_REG_RPAD_MARGIN, 0x00, /* disable margin */
  248. 0);
  249. }
  250. }
  251. static int steam_input_open(struct input_dev *dev)
  252. {
  253. struct steam_device *steam = input_get_drvdata(dev);
  254. mutex_lock(&steam->mutex);
  255. if (!steam->client_opened && lizard_mode)
  256. steam_set_lizard_mode(steam, false);
  257. mutex_unlock(&steam->mutex);
  258. return 0;
  259. }
  260. static void steam_input_close(struct input_dev *dev)
  261. {
  262. struct steam_device *steam = input_get_drvdata(dev);
  263. mutex_lock(&steam->mutex);
  264. if (!steam->client_opened && lizard_mode)
  265. steam_set_lizard_mode(steam, true);
  266. mutex_unlock(&steam->mutex);
  267. }
  268. static enum power_supply_property steam_battery_props[] = {
  269. POWER_SUPPLY_PROP_PRESENT,
  270. POWER_SUPPLY_PROP_SCOPE,
  271. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  272. POWER_SUPPLY_PROP_CAPACITY,
  273. };
  274. static int steam_battery_get_property(struct power_supply *psy,
  275. enum power_supply_property psp,
  276. union power_supply_propval *val)
  277. {
  278. struct steam_device *steam = power_supply_get_drvdata(psy);
  279. unsigned long flags;
  280. s16 volts;
  281. u8 batt;
  282. int ret = 0;
  283. spin_lock_irqsave(&steam->lock, flags);
  284. volts = steam->voltage;
  285. batt = steam->battery_charge;
  286. spin_unlock_irqrestore(&steam->lock, flags);
  287. switch (psp) {
  288. case POWER_SUPPLY_PROP_PRESENT:
  289. val->intval = 1;
  290. break;
  291. case POWER_SUPPLY_PROP_SCOPE:
  292. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  293. break;
  294. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  295. val->intval = volts * 1000; /* mV -> uV */
  296. break;
  297. case POWER_SUPPLY_PROP_CAPACITY:
  298. val->intval = batt;
  299. break;
  300. default:
  301. ret = -EINVAL;
  302. break;
  303. }
  304. return ret;
  305. }
  306. static int steam_battery_register(struct steam_device *steam)
  307. {
  308. struct power_supply *battery;
  309. struct power_supply_config battery_cfg = { .drv_data = steam, };
  310. unsigned long flags;
  311. int ret;
  312. steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  313. steam->battery_desc.properties = steam_battery_props;
  314. steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
  315. steam->battery_desc.get_property = steam_battery_get_property;
  316. steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
  317. GFP_KERNEL, "steam-controller-%s-battery",
  318. steam->serial_no);
  319. if (!steam->battery_desc.name)
  320. return -ENOMEM;
  321. /* avoid the warning of 0% battery while waiting for the first info */
  322. spin_lock_irqsave(&steam->lock, flags);
  323. steam->voltage = 3000;
  324. steam->battery_charge = 100;
  325. spin_unlock_irqrestore(&steam->lock, flags);
  326. battery = power_supply_register(&steam->hdev->dev,
  327. &steam->battery_desc, &battery_cfg);
  328. if (IS_ERR(battery)) {
  329. ret = PTR_ERR(battery);
  330. hid_err(steam->hdev,
  331. "%s:power_supply_register failed with error %d\n",
  332. __func__, ret);
  333. return ret;
  334. }
  335. rcu_assign_pointer(steam->battery, battery);
  336. power_supply_powers(battery, &steam->hdev->dev);
  337. return 0;
  338. }
  339. static int steam_input_register(struct steam_device *steam)
  340. {
  341. struct hid_device *hdev = steam->hdev;
  342. struct input_dev *input;
  343. int ret;
  344. rcu_read_lock();
  345. input = rcu_dereference(steam->input);
  346. rcu_read_unlock();
  347. if (input) {
  348. dbg_hid("%s: already connected\n", __func__);
  349. return 0;
  350. }
  351. input = input_allocate_device();
  352. if (!input)
  353. return -ENOMEM;
  354. input_set_drvdata(input, steam);
  355. input->dev.parent = &hdev->dev;
  356. input->open = steam_input_open;
  357. input->close = steam_input_close;
  358. input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ?
  359. "Wireless Steam Controller" :
  360. "Steam Controller";
  361. input->phys = hdev->phys;
  362. input->uniq = steam->serial_no;
  363. input->id.bustype = hdev->bus;
  364. input->id.vendor = hdev->vendor;
  365. input->id.product = hdev->product;
  366. input->id.version = hdev->version;
  367. input_set_capability(input, EV_KEY, BTN_TR2);
  368. input_set_capability(input, EV_KEY, BTN_TL2);
  369. input_set_capability(input, EV_KEY, BTN_TR);
  370. input_set_capability(input, EV_KEY, BTN_TL);
  371. input_set_capability(input, EV_KEY, BTN_Y);
  372. input_set_capability(input, EV_KEY, BTN_B);
  373. input_set_capability(input, EV_KEY, BTN_X);
  374. input_set_capability(input, EV_KEY, BTN_A);
  375. input_set_capability(input, EV_KEY, BTN_DPAD_UP);
  376. input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
  377. input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
  378. input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
  379. input_set_capability(input, EV_KEY, BTN_SELECT);
  380. input_set_capability(input, EV_KEY, BTN_MODE);
  381. input_set_capability(input, EV_KEY, BTN_START);
  382. input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
  383. input_set_capability(input, EV_KEY, BTN_GEAR_UP);
  384. input_set_capability(input, EV_KEY, BTN_THUMBR);
  385. input_set_capability(input, EV_KEY, BTN_THUMBL);
  386. input_set_capability(input, EV_KEY, BTN_THUMB);
  387. input_set_capability(input, EV_KEY, BTN_THUMB2);
  388. input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
  389. input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
  390. input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
  391. input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
  392. input_set_abs_params(input, ABS_RX, -32767, 32767,
  393. STEAM_PAD_FUZZ, 0);
  394. input_set_abs_params(input, ABS_RY, -32767, 32767,
  395. STEAM_PAD_FUZZ, 0);
  396. input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
  397. STEAM_PAD_FUZZ, 0);
  398. input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
  399. STEAM_PAD_FUZZ, 0);
  400. input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
  401. input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
  402. input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
  403. input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
  404. input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
  405. input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
  406. input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
  407. input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
  408. ret = input_register_device(input);
  409. if (ret)
  410. goto input_register_fail;
  411. rcu_assign_pointer(steam->input, input);
  412. return 0;
  413. input_register_fail:
  414. input_free_device(input);
  415. return ret;
  416. }
  417. static void steam_input_unregister(struct steam_device *steam)
  418. {
  419. struct input_dev *input;
  420. rcu_read_lock();
  421. input = rcu_dereference(steam->input);
  422. rcu_read_unlock();
  423. if (!input)
  424. return;
  425. RCU_INIT_POINTER(steam->input, NULL);
  426. synchronize_rcu();
  427. input_unregister_device(input);
  428. }
  429. static void steam_battery_unregister(struct steam_device *steam)
  430. {
  431. struct power_supply *battery;
  432. rcu_read_lock();
  433. battery = rcu_dereference(steam->battery);
  434. rcu_read_unlock();
  435. if (!battery)
  436. return;
  437. RCU_INIT_POINTER(steam->battery, NULL);
  438. synchronize_rcu();
  439. power_supply_unregister(battery);
  440. }
  441. static int steam_register(struct steam_device *steam)
  442. {
  443. int ret;
  444. bool client_opened;
  445. /*
  446. * This function can be called several times in a row with the
  447. * wireless adaptor, without steam_unregister() between them, because
  448. * another client send a get_connection_status command, for example.
  449. * The battery and serial number are set just once per device.
  450. */
  451. if (!steam->serial_no[0]) {
  452. /*
  453. * Unlikely, but getting the serial could fail, and it is not so
  454. * important, so make up a serial number and go on.
  455. */
  456. mutex_lock(&steam->mutex);
  457. if (steam_get_serial(steam) < 0)
  458. strlcpy(steam->serial_no, "XXXXXXXXXX",
  459. sizeof(steam->serial_no));
  460. mutex_unlock(&steam->mutex);
  461. hid_info(steam->hdev, "Steam Controller '%s' connected",
  462. steam->serial_no);
  463. /* ignore battery errors, we can live without it */
  464. if (steam->quirks & STEAM_QUIRK_WIRELESS)
  465. steam_battery_register(steam);
  466. mutex_lock(&steam_devices_lock);
  467. if (list_empty(&steam->list))
  468. list_add(&steam->list, &steam_devices);
  469. mutex_unlock(&steam_devices_lock);
  470. }
  471. mutex_lock(&steam->mutex);
  472. client_opened = steam->client_opened;
  473. if (!client_opened)
  474. steam_set_lizard_mode(steam, lizard_mode);
  475. mutex_unlock(&steam->mutex);
  476. if (!client_opened)
  477. ret = steam_input_register(steam);
  478. else
  479. ret = 0;
  480. return ret;
  481. }
  482. static void steam_unregister(struct steam_device *steam)
  483. {
  484. steam_battery_unregister(steam);
  485. steam_input_unregister(steam);
  486. if (steam->serial_no[0]) {
  487. hid_info(steam->hdev, "Steam Controller '%s' disconnected",
  488. steam->serial_no);
  489. mutex_lock(&steam_devices_lock);
  490. list_del_init(&steam->list);
  491. mutex_unlock(&steam_devices_lock);
  492. steam->serial_no[0] = 0;
  493. }
  494. }
  495. static void steam_work_connect_cb(struct work_struct *work)
  496. {
  497. struct steam_device *steam = container_of(work, struct steam_device,
  498. work_connect);
  499. unsigned long flags;
  500. bool connected;
  501. int ret;
  502. spin_lock_irqsave(&steam->lock, flags);
  503. connected = steam->connected;
  504. spin_unlock_irqrestore(&steam->lock, flags);
  505. if (connected) {
  506. ret = steam_register(steam);
  507. if (ret) {
  508. hid_err(steam->hdev,
  509. "%s:steam_register failed with error %d\n",
  510. __func__, ret);
  511. }
  512. } else {
  513. steam_unregister(steam);
  514. }
  515. }
  516. static bool steam_is_valve_interface(struct hid_device *hdev)
  517. {
  518. struct hid_report_enum *rep_enum;
  519. /*
  520. * The wired device creates 3 interfaces:
  521. * 0: emulated mouse.
  522. * 1: emulated keyboard.
  523. * 2: the real game pad.
  524. * The wireless device creates 5 interfaces:
  525. * 0: emulated keyboard.
  526. * 1-4: slots where up to 4 real game pads will be connected to.
  527. * We know which one is the real gamepad interface because they are the
  528. * only ones with a feature report.
  529. */
  530. rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
  531. return !list_empty(&rep_enum->report_list);
  532. }
  533. static int steam_client_ll_parse(struct hid_device *hdev)
  534. {
  535. struct steam_device *steam = hdev->driver_data;
  536. return hid_parse_report(hdev, steam->hdev->dev_rdesc,
  537. steam->hdev->dev_rsize);
  538. }
  539. static int steam_client_ll_start(struct hid_device *hdev)
  540. {
  541. return 0;
  542. }
  543. static void steam_client_ll_stop(struct hid_device *hdev)
  544. {
  545. }
  546. static int steam_client_ll_open(struct hid_device *hdev)
  547. {
  548. struct steam_device *steam = hdev->driver_data;
  549. mutex_lock(&steam->mutex);
  550. steam->client_opened = true;
  551. mutex_unlock(&steam->mutex);
  552. steam_input_unregister(steam);
  553. return 0;
  554. }
  555. static void steam_client_ll_close(struct hid_device *hdev)
  556. {
  557. struct steam_device *steam = hdev->driver_data;
  558. unsigned long flags;
  559. bool connected;
  560. spin_lock_irqsave(&steam->lock, flags);
  561. connected = steam->connected;
  562. spin_unlock_irqrestore(&steam->lock, flags);
  563. mutex_lock(&steam->mutex);
  564. steam->client_opened = false;
  565. if (connected)
  566. steam_set_lizard_mode(steam, lizard_mode);
  567. mutex_unlock(&steam->mutex);
  568. if (connected)
  569. steam_input_register(steam);
  570. }
  571. static int steam_client_ll_raw_request(struct hid_device *hdev,
  572. unsigned char reportnum, u8 *buf,
  573. size_t count, unsigned char report_type,
  574. int reqtype)
  575. {
  576. struct steam_device *steam = hdev->driver_data;
  577. return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
  578. report_type, reqtype);
  579. }
  580. static struct hid_ll_driver steam_client_ll_driver = {
  581. .parse = steam_client_ll_parse,
  582. .start = steam_client_ll_start,
  583. .stop = steam_client_ll_stop,
  584. .open = steam_client_ll_open,
  585. .close = steam_client_ll_close,
  586. .raw_request = steam_client_ll_raw_request,
  587. };
  588. static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
  589. {
  590. struct hid_device *client_hdev;
  591. client_hdev = hid_allocate_device();
  592. if (IS_ERR(client_hdev))
  593. return client_hdev;
  594. client_hdev->ll_driver = &steam_client_ll_driver;
  595. client_hdev->dev.parent = hdev->dev.parent;
  596. client_hdev->bus = hdev->bus;
  597. client_hdev->vendor = hdev->vendor;
  598. client_hdev->product = hdev->product;
  599. client_hdev->version = hdev->version;
  600. client_hdev->type = hdev->type;
  601. client_hdev->country = hdev->country;
  602. strlcpy(client_hdev->name, hdev->name,
  603. sizeof(client_hdev->name));
  604. strlcpy(client_hdev->phys, hdev->phys,
  605. sizeof(client_hdev->phys));
  606. /*
  607. * Since we use the same device info than the real interface to
  608. * trick userspace, we will be calling steam_probe recursively.
  609. * We need to recognize the client interface somehow.
  610. */
  611. client_hdev->group = HID_GROUP_STEAM;
  612. return client_hdev;
  613. }
  614. static int steam_probe(struct hid_device *hdev,
  615. const struct hid_device_id *id)
  616. {
  617. struct steam_device *steam;
  618. int ret;
  619. ret = hid_parse(hdev);
  620. if (ret) {
  621. hid_err(hdev,
  622. "%s:parse of hid interface failed\n", __func__);
  623. return ret;
  624. }
  625. /*
  626. * The virtual client_dev is only used for hidraw.
  627. * Also avoid the recursive probe.
  628. */
  629. if (hdev->group == HID_GROUP_STEAM)
  630. return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  631. /*
  632. * The non-valve interfaces (mouse and keyboard emulation) are
  633. * connected without changes.
  634. */
  635. if (!steam_is_valve_interface(hdev))
  636. return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  637. steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
  638. if (!steam) {
  639. ret = -ENOMEM;
  640. goto steam_alloc_fail;
  641. }
  642. steam->hdev = hdev;
  643. hid_set_drvdata(hdev, steam);
  644. spin_lock_init(&steam->lock);
  645. mutex_init(&steam->mutex);
  646. steam->quirks = id->driver_data;
  647. INIT_WORK(&steam->work_connect, steam_work_connect_cb);
  648. INIT_LIST_HEAD(&steam->list);
  649. steam->client_hdev = steam_create_client_hid(hdev);
  650. if (IS_ERR(steam->client_hdev)) {
  651. ret = PTR_ERR(steam->client_hdev);
  652. goto client_hdev_fail;
  653. }
  654. steam->client_hdev->driver_data = steam;
  655. /*
  656. * With the real steam controller interface, do not connect hidraw.
  657. * Instead, create the client_hid and connect that.
  658. */
  659. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
  660. if (ret)
  661. goto hid_hw_start_fail;
  662. ret = hid_add_device(steam->client_hdev);
  663. if (ret)
  664. goto client_hdev_add_fail;
  665. ret = hid_hw_open(hdev);
  666. if (ret) {
  667. hid_err(hdev,
  668. "%s:hid_hw_open\n",
  669. __func__);
  670. goto hid_hw_open_fail;
  671. }
  672. if (steam->quirks & STEAM_QUIRK_WIRELESS) {
  673. hid_info(hdev, "Steam wireless receiver connected");
  674. /* If using a wireless adaptor ask for connection status */
  675. steam->connected = false;
  676. steam_request_conn_status(steam);
  677. } else {
  678. /* A wired connection is always present */
  679. steam->connected = true;
  680. ret = steam_register(steam);
  681. if (ret) {
  682. hid_err(hdev,
  683. "%s:steam_register failed with error %d\n",
  684. __func__, ret);
  685. goto input_register_fail;
  686. }
  687. }
  688. return 0;
  689. input_register_fail:
  690. hid_hw_open_fail:
  691. client_hdev_add_fail:
  692. hid_hw_stop(hdev);
  693. hid_hw_start_fail:
  694. hid_destroy_device(steam->client_hdev);
  695. client_hdev_fail:
  696. cancel_work_sync(&steam->work_connect);
  697. steam_alloc_fail:
  698. hid_err(hdev, "%s: failed with error %d\n",
  699. __func__, ret);
  700. return ret;
  701. }
  702. static void steam_remove(struct hid_device *hdev)
  703. {
  704. struct steam_device *steam = hid_get_drvdata(hdev);
  705. if (!steam || hdev->group == HID_GROUP_STEAM) {
  706. hid_hw_stop(hdev);
  707. return;
  708. }
  709. hid_destroy_device(steam->client_hdev);
  710. steam->client_opened = false;
  711. cancel_work_sync(&steam->work_connect);
  712. if (steam->quirks & STEAM_QUIRK_WIRELESS) {
  713. hid_info(hdev, "Steam wireless receiver disconnected");
  714. }
  715. hid_hw_close(hdev);
  716. hid_hw_stop(hdev);
  717. steam_unregister(steam);
  718. }
  719. static void steam_do_connect_event(struct steam_device *steam, bool connected)
  720. {
  721. unsigned long flags;
  722. bool changed;
  723. spin_lock_irqsave(&steam->lock, flags);
  724. changed = steam->connected != connected;
  725. steam->connected = connected;
  726. spin_unlock_irqrestore(&steam->lock, flags);
  727. if (changed && schedule_work(&steam->work_connect) == 0)
  728. dbg_hid("%s: connected=%d event already queued\n",
  729. __func__, connected);
  730. }
  731. /*
  732. * Some input data in the protocol has the opposite sign.
  733. * Clamp the values to 32767..-32767 so that the range is
  734. * symmetrical and can be negated safely.
  735. */
  736. static inline s16 steam_le16(u8 *data)
  737. {
  738. s16 x = (s16) le16_to_cpup((__le16 *)data);
  739. return x == -32768 ? -32767 : x;
  740. }
  741. /*
  742. * The size for this message payload is 60.
  743. * The known values are:
  744. * (* values are not sent through wireless)
  745. * (* accelerator/gyro is disabled by default)
  746. * Offset| Type | Mapped to |Meaning
  747. * -------+-------+-----------+--------------------------
  748. * 4-7 | u32 | -- | sequence number
  749. * 8-10 | 24bit | see below | buttons
  750. * 11 | u8 | ABS_HAT2Y | left trigger
  751. * 12 | u8 | ABS_HAT2X | right trigger
  752. * 13-15 | -- | -- | always 0
  753. * 16-17 | s16 | ABS_X/ABS_HAT0X | X value
  754. * 18-19 | s16 | ABS_Y/ABS_HAT0Y | Y value
  755. * 20-21 | s16 | ABS_RX | right-pad X value
  756. * 22-23 | s16 | ABS_RY | right-pad Y value
  757. * 24-25 | s16 | -- | * left trigger
  758. * 26-27 | s16 | -- | * right trigger
  759. * 28-29 | s16 | -- | * accelerometer X value
  760. * 30-31 | s16 | -- | * accelerometer Y value
  761. * 32-33 | s16 | -- | * accelerometer Z value
  762. * 34-35 | s16 | -- | gyro X value
  763. * 36-36 | s16 | -- | gyro Y value
  764. * 38-39 | s16 | -- | gyro Z value
  765. * 40-41 | s16 | -- | quaternion W value
  766. * 42-43 | s16 | -- | quaternion X value
  767. * 44-45 | s16 | -- | quaternion Y value
  768. * 46-47 | s16 | -- | quaternion Z value
  769. * 48-49 | -- | -- | always 0
  770. * 50-51 | s16 | -- | * left trigger (uncalibrated)
  771. * 52-53 | s16 | -- | * right trigger (uncalibrated)
  772. * 54-55 | s16 | -- | * joystick X value (uncalibrated)
  773. * 56-57 | s16 | -- | * joystick Y value (uncalibrated)
  774. * 58-59 | s16 | -- | * left-pad X value
  775. * 60-61 | s16 | -- | * left-pad Y value
  776. * 62-63 | u16 | -- | * battery voltage
  777. *
  778. * The buttons are:
  779. * Bit | Mapped to | Description
  780. * ------+------------+--------------------------------
  781. * 8.0 | BTN_TR2 | right trigger fully pressed
  782. * 8.1 | BTN_TL2 | left trigger fully pressed
  783. * 8.2 | BTN_TR | right shoulder
  784. * 8.3 | BTN_TL | left shoulder
  785. * 8.4 | BTN_Y | button Y
  786. * 8.5 | BTN_B | button B
  787. * 8.6 | BTN_X | button X
  788. * 8.7 | BTN_A | button A
  789. * 9.0 | BTN_DPAD_UP | lef-pad up
  790. * 9.1 | BTN_DPAD_RIGHT | lef-pad right
  791. * 9.2 | BTN_DPAD_LEFT | lef-pad left
  792. * 9.3 | BTN_DPAD_DOWN | lef-pad down
  793. * 9.4 | BTN_SELECT | menu left
  794. * 9.5 | BTN_MODE | steam logo
  795. * 9.6 | BTN_START | menu right
  796. * 9.7 | BTN_GEAR_DOWN | left back lever
  797. * 10.0 | BTN_GEAR_UP | right back lever
  798. * 10.1 | -- | left-pad clicked
  799. * 10.2 | BTN_THUMBR | right-pad clicked
  800. * 10.3 | BTN_THUMB | left-pad touched (but see explanation below)
  801. * 10.4 | BTN_THUMB2 | right-pad touched
  802. * 10.5 | -- | unknown
  803. * 10.6 | BTN_THUMBL | joystick clicked
  804. * 10.7 | -- | lpad_and_joy
  805. */
  806. static void steam_do_input_event(struct steam_device *steam,
  807. struct input_dev *input, u8 *data)
  808. {
  809. /* 24 bits of buttons */
  810. u8 b8, b9, b10;
  811. s16 x, y;
  812. bool lpad_touched, lpad_and_joy;
  813. b8 = data[8];
  814. b9 = data[9];
  815. b10 = data[10];
  816. input_report_abs(input, ABS_HAT2Y, data[11]);
  817. input_report_abs(input, ABS_HAT2X, data[12]);
  818. /*
  819. * These two bits tells how to interpret the values X and Y.
  820. * lpad_and_joy tells that the joystick and the lpad are used at the
  821. * same time.
  822. * lpad_touched tells whether X/Y are to be read as lpad coord or
  823. * joystick values.
  824. * (lpad_touched || lpad_and_joy) tells if the lpad is really touched.
  825. */
  826. lpad_touched = b10 & BIT(3);
  827. lpad_and_joy = b10 & BIT(7);
  828. x = steam_le16(data + 16);
  829. y = -steam_le16(data + 18);
  830. input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
  831. input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
  832. /* Check if joystick is centered */
  833. if (lpad_touched && !lpad_and_joy) {
  834. input_report_abs(input, ABS_X, 0);
  835. input_report_abs(input, ABS_Y, 0);
  836. }
  837. /* Check if lpad is untouched */
  838. if (!(lpad_touched || lpad_and_joy)) {
  839. input_report_abs(input, ABS_HAT0X, 0);
  840. input_report_abs(input, ABS_HAT0Y, 0);
  841. }
  842. input_report_abs(input, ABS_RX, steam_le16(data + 20));
  843. input_report_abs(input, ABS_RY, -steam_le16(data + 22));
  844. input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
  845. input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
  846. input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
  847. input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
  848. input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
  849. input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
  850. input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
  851. input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
  852. input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
  853. input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
  854. input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
  855. input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
  856. input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
  857. input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
  858. input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
  859. input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
  860. input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
  861. input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
  862. input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
  863. input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
  864. input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
  865. input_sync(input);
  866. }
  867. /*
  868. * The size for this message payload is 11.
  869. * The known values are:
  870. * Offset| Type | Meaning
  871. * -------+-------+---------------------------
  872. * 4-7 | u32 | sequence number
  873. * 8-11 | -- | always 0
  874. * 12-13 | u16 | voltage (mV)
  875. * 14 | u8 | battery percent
  876. */
  877. static void steam_do_battery_event(struct steam_device *steam,
  878. struct power_supply *battery, u8 *data)
  879. {
  880. unsigned long flags;
  881. s16 volts = steam_le16(data + 12);
  882. u8 batt = data[14];
  883. /* Creating the battery may have failed */
  884. rcu_read_lock();
  885. battery = rcu_dereference(steam->battery);
  886. if (likely(battery)) {
  887. spin_lock_irqsave(&steam->lock, flags);
  888. steam->voltage = volts;
  889. steam->battery_charge = batt;
  890. spin_unlock_irqrestore(&steam->lock, flags);
  891. power_supply_changed(battery);
  892. }
  893. rcu_read_unlock();
  894. }
  895. static int steam_raw_event(struct hid_device *hdev,
  896. struct hid_report *report, u8 *data,
  897. int size)
  898. {
  899. struct steam_device *steam = hid_get_drvdata(hdev);
  900. struct input_dev *input;
  901. struct power_supply *battery;
  902. if (!steam)
  903. return 0;
  904. if (steam->client_opened)
  905. hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
  906. data, size, 0);
  907. /*
  908. * All messages are size=64, all values little-endian.
  909. * The format is:
  910. * Offset| Meaning
  911. * -------+--------------------------------------------
  912. * 0-1 | always 0x01, 0x00, maybe protocol version?
  913. * 2 | type of message
  914. * 3 | length of the real payload (not checked)
  915. * 4-n | payload data, depends on the type
  916. *
  917. * There are these known types of message:
  918. * 0x01: input data (60 bytes)
  919. * 0x03: wireless connect/disconnect (1 byte)
  920. * 0x04: battery status (11 bytes)
  921. */
  922. if (size != 64 || data[0] != 1 || data[1] != 0)
  923. return 0;
  924. switch (data[2]) {
  925. case STEAM_EV_INPUT_DATA:
  926. if (steam->client_opened)
  927. return 0;
  928. rcu_read_lock();
  929. input = rcu_dereference(steam->input);
  930. if (likely(input))
  931. steam_do_input_event(steam, input, data);
  932. rcu_read_unlock();
  933. break;
  934. case STEAM_EV_CONNECT:
  935. /*
  936. * The payload of this event is a single byte:
  937. * 0x01: disconnected.
  938. * 0x02: connected.
  939. */
  940. switch (data[4]) {
  941. case 0x01:
  942. steam_do_connect_event(steam, false);
  943. break;
  944. case 0x02:
  945. steam_do_connect_event(steam, true);
  946. break;
  947. }
  948. break;
  949. case STEAM_EV_BATTERY:
  950. if (steam->quirks & STEAM_QUIRK_WIRELESS) {
  951. rcu_read_lock();
  952. battery = rcu_dereference(steam->battery);
  953. if (likely(battery)) {
  954. steam_do_battery_event(steam, battery, data);
  955. } else {
  956. dbg_hid(
  957. "%s: battery data without connect event\n",
  958. __func__);
  959. steam_do_connect_event(steam, true);
  960. }
  961. rcu_read_unlock();
  962. }
  963. break;
  964. }
  965. return 0;
  966. }
  967. static int steam_param_set_lizard_mode(const char *val,
  968. const struct kernel_param *kp)
  969. {
  970. struct steam_device *steam;
  971. int ret;
  972. ret = param_set_bool(val, kp);
  973. if (ret)
  974. return ret;
  975. mutex_lock(&steam_devices_lock);
  976. list_for_each_entry(steam, &steam_devices, list) {
  977. mutex_lock(&steam->mutex);
  978. if (!steam->client_opened)
  979. steam_set_lizard_mode(steam, lizard_mode);
  980. mutex_unlock(&steam->mutex);
  981. }
  982. mutex_unlock(&steam_devices_lock);
  983. return 0;
  984. }
  985. static const struct kernel_param_ops steam_lizard_mode_ops = {
  986. .set = steam_param_set_lizard_mode,
  987. .get = param_get_bool,
  988. };
  989. module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
  990. MODULE_PARM_DESC(lizard_mode,
  991. "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
  992. static const struct hid_device_id steam_controllers[] = {
  993. { /* Wired Steam Controller */
  994. HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
  995. USB_DEVICE_ID_STEAM_CONTROLLER)
  996. },
  997. { /* Wireless Steam Controller */
  998. HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
  999. USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
  1000. .driver_data = STEAM_QUIRK_WIRELESS
  1001. },
  1002. {}
  1003. };
  1004. MODULE_DEVICE_TABLE(hid, steam_controllers);
  1005. static struct hid_driver steam_controller_driver = {
  1006. .name = "hid-steam",
  1007. .id_table = steam_controllers,
  1008. .probe = steam_probe,
  1009. .remove = steam_remove,
  1010. .raw_event = steam_raw_event,
  1011. };
  1012. module_hid_driver(steam_controller_driver);