hid-steam.c 32 KB

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