1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
58#define STEAM_PAD_RESOLUTION 1638
59
60#define STEAM_TRIGGER_RESOLUTION 51
61
62#define STEAM_JOYSTICK_RESOLUTION 51
63
64#define STEAM_PAD_FUZZ 256
65
66
67
68
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
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
97#define STEAM_EV_INPUT_DATA 0x01
98#define STEAM_EV_CONNECT 0x03
99#define STEAM_EV_BATTERY 0x04
100
101
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
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 (hid_report_len(r) < 64)
138 return -EINVAL;
139
140 buf = hid_alloc_report_buf(r, GFP_KERNEL);
141 if (!buf)
142 return -ENOMEM;
143
144
145
146
147
148
149
150 ret = hid_hw_raw_request(steam->hdev, 0x00,
151 buf, hid_report_len(r) + 1,
152 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
153 if (ret > 0)
154 memcpy(data, buf + 1, min(size, ret - 1));
155 kfree(buf);
156 return ret;
157}
158
159static int steam_send_report(struct steam_device *steam,
160 u8 *cmd, int size)
161{
162 struct hid_report *r;
163 u8 *buf;
164 unsigned int retries = 50;
165 int ret;
166
167 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
168 if (hid_report_len(r) < 64)
169 return -EINVAL;
170
171 buf = hid_alloc_report_buf(r, GFP_KERNEL);
172 if (!buf)
173 return -ENOMEM;
174
175
176 memcpy(buf + 1, cmd, size);
177
178
179
180
181
182
183
184 do {
185 ret = hid_hw_raw_request(steam->hdev, 0,
186 buf, size + 1,
187 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
188 if (ret != -EPIPE)
189 break;
190 msleep(20);
191 } while (--retries);
192
193 kfree(buf);
194 if (ret < 0)
195 hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
196 ret, size, cmd);
197 return ret;
198}
199
200static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
201{
202 return steam_send_report(steam, &cmd, 1);
203}
204
205static int steam_write_registers(struct steam_device *steam,
206 ...)
207{
208
209 u8 reg;
210 u16 val;
211 u8 cmd[64] = {STEAM_CMD_WRITE_REGISTER, 0x00};
212 va_list args;
213
214 va_start(args, steam);
215 for (;;) {
216 reg = va_arg(args, int);
217 if (reg == 0)
218 break;
219 val = va_arg(args, int);
220 cmd[cmd[1] + 2] = reg;
221 cmd[cmd[1] + 3] = val & 0xff;
222 cmd[cmd[1] + 4] = val >> 8;
223 cmd[1] += 3;
224 }
225 va_end(args);
226
227 return steam_send_report(steam, cmd, 2 + cmd[1]);
228}
229
230static int steam_get_serial(struct steam_device *steam)
231{
232
233
234
235
236 int ret;
237 u8 cmd[] = {STEAM_CMD_GET_SERIAL, 0x15, 0x01};
238 u8 reply[3 + STEAM_SERIAL_LEN + 1];
239
240 ret = steam_send_report(steam, cmd, sizeof(cmd));
241 if (ret < 0)
242 return ret;
243 ret = steam_recv_report(steam, reply, sizeof(reply));
244 if (ret < 0)
245 return ret;
246 if (reply[0] != 0xae || reply[1] != 0x15 || reply[2] != 0x01)
247 return -EIO;
248 reply[3 + STEAM_SERIAL_LEN] = 0;
249 strlcpy(steam->serial_no, reply + 3, sizeof(steam->serial_no));
250 return 0;
251}
252
253
254
255
256
257
258static inline int steam_request_conn_status(struct steam_device *steam)
259{
260 return steam_send_report_byte(steam, STEAM_CMD_REQUEST_COMM_STATUS);
261}
262
263static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
264{
265 if (enable) {
266
267 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MAPPINGS);
268
269 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MOUSE);
270 steam_write_registers(steam,
271 STEAM_REG_RPAD_MARGIN, 0x01,
272 0);
273 } else {
274
275 steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
276 steam_write_registers(steam,
277 STEAM_REG_RPAD_MODE, 0x07,
278 STEAM_REG_RPAD_MARGIN, 0x00,
279 0);
280 }
281}
282
283static int steam_input_open(struct input_dev *dev)
284{
285 struct steam_device *steam = input_get_drvdata(dev);
286
287 mutex_lock(&steam->mutex);
288 if (!steam->client_opened && lizard_mode)
289 steam_set_lizard_mode(steam, false);
290 mutex_unlock(&steam->mutex);
291 return 0;
292}
293
294static void steam_input_close(struct input_dev *dev)
295{
296 struct steam_device *steam = input_get_drvdata(dev);
297
298 mutex_lock(&steam->mutex);
299 if (!steam->client_opened && lizard_mode)
300 steam_set_lizard_mode(steam, true);
301 mutex_unlock(&steam->mutex);
302}
303
304static enum power_supply_property steam_battery_props[] = {
305 POWER_SUPPLY_PROP_PRESENT,
306 POWER_SUPPLY_PROP_SCOPE,
307 POWER_SUPPLY_PROP_VOLTAGE_NOW,
308 POWER_SUPPLY_PROP_CAPACITY,
309};
310
311static int steam_battery_get_property(struct power_supply *psy,
312 enum power_supply_property psp,
313 union power_supply_propval *val)
314{
315 struct steam_device *steam = power_supply_get_drvdata(psy);
316 unsigned long flags;
317 s16 volts;
318 u8 batt;
319 int ret = 0;
320
321 spin_lock_irqsave(&steam->lock, flags);
322 volts = steam->voltage;
323 batt = steam->battery_charge;
324 spin_unlock_irqrestore(&steam->lock, flags);
325
326 switch (psp) {
327 case POWER_SUPPLY_PROP_PRESENT:
328 val->intval = 1;
329 break;
330 case POWER_SUPPLY_PROP_SCOPE:
331 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
332 break;
333 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
334 val->intval = volts * 1000;
335 break;
336 case POWER_SUPPLY_PROP_CAPACITY:
337 val->intval = batt;
338 break;
339 default:
340 ret = -EINVAL;
341 break;
342 }
343 return ret;
344}
345
346static int steam_battery_register(struct steam_device *steam)
347{
348 struct power_supply *battery;
349 struct power_supply_config battery_cfg = { .drv_data = steam, };
350 unsigned long flags;
351 int ret;
352
353 steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
354 steam->battery_desc.properties = steam_battery_props;
355 steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
356 steam->battery_desc.get_property = steam_battery_get_property;
357 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
358 GFP_KERNEL, "steam-controller-%s-battery",
359 steam->serial_no);
360 if (!steam->battery_desc.name)
361 return -ENOMEM;
362
363
364 spin_lock_irqsave(&steam->lock, flags);
365 steam->voltage = 3000;
366 steam->battery_charge = 100;
367 spin_unlock_irqrestore(&steam->lock, flags);
368
369 battery = power_supply_register(&steam->hdev->dev,
370 &steam->battery_desc, &battery_cfg);
371 if (IS_ERR(battery)) {
372 ret = PTR_ERR(battery);
373 hid_err(steam->hdev,
374 "%s:power_supply_register failed with error %d\n",
375 __func__, ret);
376 return ret;
377 }
378 rcu_assign_pointer(steam->battery, battery);
379 power_supply_powers(battery, &steam->hdev->dev);
380 return 0;
381}
382
383static int steam_input_register(struct steam_device *steam)
384{
385 struct hid_device *hdev = steam->hdev;
386 struct input_dev *input;
387 int ret;
388
389 rcu_read_lock();
390 input = rcu_dereference(steam->input);
391 rcu_read_unlock();
392 if (input) {
393 dbg_hid("%s: already connected\n", __func__);
394 return 0;
395 }
396
397 input = input_allocate_device();
398 if (!input)
399 return -ENOMEM;
400
401 input_set_drvdata(input, steam);
402 input->dev.parent = &hdev->dev;
403 input->open = steam_input_open;
404 input->close = steam_input_close;
405
406 input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ?
407 "Wireless Steam Controller" :
408 "Steam Controller";
409 input->phys = hdev->phys;
410 input->uniq = steam->serial_no;
411 input->id.bustype = hdev->bus;
412 input->id.vendor = hdev->vendor;
413 input->id.product = hdev->product;
414 input->id.version = hdev->version;
415
416 input_set_capability(input, EV_KEY, BTN_TR2);
417 input_set_capability(input, EV_KEY, BTN_TL2);
418 input_set_capability(input, EV_KEY, BTN_TR);
419 input_set_capability(input, EV_KEY, BTN_TL);
420 input_set_capability(input, EV_KEY, BTN_Y);
421 input_set_capability(input, EV_KEY, BTN_B);
422 input_set_capability(input, EV_KEY, BTN_X);
423 input_set_capability(input, EV_KEY, BTN_A);
424 input_set_capability(input, EV_KEY, BTN_DPAD_UP);
425 input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
426 input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
427 input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
428 input_set_capability(input, EV_KEY, BTN_SELECT);
429 input_set_capability(input, EV_KEY, BTN_MODE);
430 input_set_capability(input, EV_KEY, BTN_START);
431 input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
432 input_set_capability(input, EV_KEY, BTN_GEAR_UP);
433 input_set_capability(input, EV_KEY, BTN_THUMBR);
434 input_set_capability(input, EV_KEY, BTN_THUMBL);
435 input_set_capability(input, EV_KEY, BTN_THUMB);
436 input_set_capability(input, EV_KEY, BTN_THUMB2);
437
438 input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
439 input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
440 input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
441 input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
442 input_set_abs_params(input, ABS_RX, -32767, 32767,
443 STEAM_PAD_FUZZ, 0);
444 input_set_abs_params(input, ABS_RY, -32767, 32767,
445 STEAM_PAD_FUZZ, 0);
446 input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
447 STEAM_PAD_FUZZ, 0);
448 input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
449 STEAM_PAD_FUZZ, 0);
450 input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
451 input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
452 input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
453 input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
454 input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
455 input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
456 input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
457 input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
458
459 ret = input_register_device(input);
460 if (ret)
461 goto input_register_fail;
462
463 rcu_assign_pointer(steam->input, input);
464 return 0;
465
466input_register_fail:
467 input_free_device(input);
468 return ret;
469}
470
471static void steam_input_unregister(struct steam_device *steam)
472{
473 struct input_dev *input;
474 rcu_read_lock();
475 input = rcu_dereference(steam->input);
476 rcu_read_unlock();
477 if (!input)
478 return;
479 RCU_INIT_POINTER(steam->input, NULL);
480 synchronize_rcu();
481 input_unregister_device(input);
482}
483
484static void steam_battery_unregister(struct steam_device *steam)
485{
486 struct power_supply *battery;
487
488 rcu_read_lock();
489 battery = rcu_dereference(steam->battery);
490 rcu_read_unlock();
491
492 if (!battery)
493 return;
494 RCU_INIT_POINTER(steam->battery, NULL);
495 synchronize_rcu();
496 power_supply_unregister(battery);
497}
498
499static int steam_register(struct steam_device *steam)
500{
501 int ret;
502 bool client_opened;
503
504
505
506
507
508
509
510 if (!steam->serial_no[0]) {
511
512
513
514
515 mutex_lock(&steam->mutex);
516 if (steam_get_serial(steam) < 0)
517 strlcpy(steam->serial_no, "XXXXXXXXXX",
518 sizeof(steam->serial_no));
519 mutex_unlock(&steam->mutex);
520
521 hid_info(steam->hdev, "Steam Controller '%s' connected",
522 steam->serial_no);
523
524
525 if (steam->quirks & STEAM_QUIRK_WIRELESS)
526 steam_battery_register(steam);
527
528 mutex_lock(&steam_devices_lock);
529 list_add(&steam->list, &steam_devices);
530 mutex_unlock(&steam_devices_lock);
531 }
532
533 mutex_lock(&steam->mutex);
534 client_opened = steam->client_opened;
535 if (!client_opened)
536 steam_set_lizard_mode(steam, lizard_mode);
537 mutex_unlock(&steam->mutex);
538
539 if (!client_opened)
540 ret = steam_input_register(steam);
541 else
542 ret = 0;
543
544 return ret;
545}
546
547static void steam_unregister(struct steam_device *steam)
548{
549 steam_battery_unregister(steam);
550 steam_input_unregister(steam);
551 if (steam->serial_no[0]) {
552 hid_info(steam->hdev, "Steam Controller '%s' disconnected",
553 steam->serial_no);
554 mutex_lock(&steam_devices_lock);
555 list_del(&steam->list);
556 mutex_unlock(&steam_devices_lock);
557 steam->serial_no[0] = 0;
558 }
559}
560
561static void steam_work_connect_cb(struct work_struct *work)
562{
563 struct steam_device *steam = container_of(work, struct steam_device,
564 work_connect);
565 unsigned long flags;
566 bool connected;
567 int ret;
568
569 spin_lock_irqsave(&steam->lock, flags);
570 connected = steam->connected;
571 spin_unlock_irqrestore(&steam->lock, flags);
572
573 if (connected) {
574 ret = steam_register(steam);
575 if (ret) {
576 hid_err(steam->hdev,
577 "%s:steam_register failed with error %d\n",
578 __func__, ret);
579 }
580 } else {
581 steam_unregister(steam);
582 }
583}
584
585static bool steam_is_valve_interface(struct hid_device *hdev)
586{
587 struct hid_report_enum *rep_enum;
588
589
590
591
592
593
594
595
596
597
598
599
600 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
601 return !list_empty(&rep_enum->report_list);
602}
603
604static int steam_client_ll_parse(struct hid_device *hdev)
605{
606 struct steam_device *steam = hdev->driver_data;
607
608 return hid_parse_report(hdev, steam->hdev->dev_rdesc,
609 steam->hdev->dev_rsize);
610}
611
612static int steam_client_ll_start(struct hid_device *hdev)
613{
614 return 0;
615}
616
617static void steam_client_ll_stop(struct hid_device *hdev)
618{
619}
620
621static int steam_client_ll_open(struct hid_device *hdev)
622{
623 struct steam_device *steam = hdev->driver_data;
624
625 mutex_lock(&steam->mutex);
626 steam->client_opened = true;
627 mutex_unlock(&steam->mutex);
628
629 steam_input_unregister(steam);
630
631 return 0;
632}
633
634static void steam_client_ll_close(struct hid_device *hdev)
635{
636 struct steam_device *steam = hdev->driver_data;
637
638 unsigned long flags;
639 bool connected;
640
641 spin_lock_irqsave(&steam->lock, flags);
642 connected = steam->connected;
643 spin_unlock_irqrestore(&steam->lock, flags);
644
645 mutex_lock(&steam->mutex);
646 steam->client_opened = false;
647 if (connected)
648 steam_set_lizard_mode(steam, lizard_mode);
649 mutex_unlock(&steam->mutex);
650
651 if (connected)
652 steam_input_register(steam);
653}
654
655static int steam_client_ll_raw_request(struct hid_device *hdev,
656 unsigned char reportnum, u8 *buf,
657 size_t count, unsigned char report_type,
658 int reqtype)
659{
660 struct steam_device *steam = hdev->driver_data;
661
662 return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
663 report_type, reqtype);
664}
665
666static struct hid_ll_driver steam_client_ll_driver = {
667 .parse = steam_client_ll_parse,
668 .start = steam_client_ll_start,
669 .stop = steam_client_ll_stop,
670 .open = steam_client_ll_open,
671 .close = steam_client_ll_close,
672 .raw_request = steam_client_ll_raw_request,
673};
674
675static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
676{
677 struct hid_device *client_hdev;
678
679 client_hdev = hid_allocate_device();
680 if (IS_ERR(client_hdev))
681 return client_hdev;
682
683 client_hdev->ll_driver = &steam_client_ll_driver;
684 client_hdev->dev.parent = hdev->dev.parent;
685 client_hdev->bus = hdev->bus;
686 client_hdev->vendor = hdev->vendor;
687 client_hdev->product = hdev->product;
688 client_hdev->version = hdev->version;
689 client_hdev->type = hdev->type;
690 client_hdev->country = hdev->country;
691 strlcpy(client_hdev->name, hdev->name,
692 sizeof(client_hdev->name));
693 strlcpy(client_hdev->phys, hdev->phys,
694 sizeof(client_hdev->phys));
695
696
697
698
699
700 client_hdev->group = HID_GROUP_STEAM;
701 return client_hdev;
702}
703
704static int steam_probe(struct hid_device *hdev,
705 const struct hid_device_id *id)
706{
707 struct steam_device *steam;
708 int ret;
709
710 ret = hid_parse(hdev);
711 if (ret) {
712 hid_err(hdev,
713 "%s:parse of hid interface failed\n", __func__);
714 return ret;
715 }
716
717
718
719
720
721 if (hdev->group == HID_GROUP_STEAM)
722 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
723
724
725
726
727 if (!steam_is_valve_interface(hdev))
728 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
729
730 steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
731 if (!steam) {
732 ret = -ENOMEM;
733 goto steam_alloc_fail;
734 }
735 steam->hdev = hdev;
736 hid_set_drvdata(hdev, steam);
737 spin_lock_init(&steam->lock);
738 mutex_init(&steam->mutex);
739 steam->quirks = id->driver_data;
740 INIT_WORK(&steam->work_connect, steam_work_connect_cb);
741
742 steam->client_hdev = steam_create_client_hid(hdev);
743 if (IS_ERR(steam->client_hdev)) {
744 ret = PTR_ERR(steam->client_hdev);
745 goto client_hdev_fail;
746 }
747 steam->client_hdev->driver_data = steam;
748
749
750
751
752
753 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
754 if (ret)
755 goto hid_hw_start_fail;
756
757 ret = hid_add_device(steam->client_hdev);
758 if (ret)
759 goto client_hdev_add_fail;
760
761 ret = hid_hw_open(hdev);
762 if (ret) {
763 hid_err(hdev,
764 "%s:hid_hw_open\n",
765 __func__);
766 goto hid_hw_open_fail;
767 }
768
769 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
770 hid_info(hdev, "Steam wireless receiver connected");
771 steam_request_conn_status(steam);
772 } else {
773 ret = steam_register(steam);
774 if (ret) {
775 hid_err(hdev,
776 "%s:steam_register failed with error %d\n",
777 __func__, ret);
778 goto input_register_fail;
779 }
780 }
781
782 return 0;
783
784input_register_fail:
785hid_hw_open_fail:
786client_hdev_add_fail:
787 hid_hw_stop(hdev);
788hid_hw_start_fail:
789 hid_destroy_device(steam->client_hdev);
790client_hdev_fail:
791 cancel_work_sync(&steam->work_connect);
792steam_alloc_fail:
793 hid_err(hdev, "%s: failed with error %d\n",
794 __func__, ret);
795 return ret;
796}
797
798static void steam_remove(struct hid_device *hdev)
799{
800 struct steam_device *steam = hid_get_drvdata(hdev);
801
802 if (!steam || hdev->group == HID_GROUP_STEAM) {
803 hid_hw_stop(hdev);
804 return;
805 }
806
807 hid_destroy_device(steam->client_hdev);
808 steam->client_opened = false;
809 cancel_work_sync(&steam->work_connect);
810 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
811 hid_info(hdev, "Steam wireless receiver disconnected");
812 }
813 hid_hw_close(hdev);
814 hid_hw_stop(hdev);
815 steam_unregister(steam);
816}
817
818static void steam_do_connect_event(struct steam_device *steam, bool connected)
819{
820 unsigned long flags;
821 bool changed;
822
823 spin_lock_irqsave(&steam->lock, flags);
824 changed = steam->connected != connected;
825 steam->connected = connected;
826 spin_unlock_irqrestore(&steam->lock, flags);
827
828 if (changed && schedule_work(&steam->work_connect) == 0)
829 dbg_hid("%s: connected=%d event already queued\n",
830 __func__, connected);
831}
832
833
834
835
836
837
838static inline s16 steam_le16(u8 *data)
839{
840 s16 x = (s16) le16_to_cpup((__le16 *)data);
841
842 return x == -32768 ? -32767 : x;
843}
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911static void steam_do_input_event(struct steam_device *steam,
912 struct input_dev *input, u8 *data)
913{
914
915 u8 b8, b9, b10;
916 s16 x, y;
917 bool lpad_touched, lpad_and_joy;
918
919 b8 = data[8];
920 b9 = data[9];
921 b10 = data[10];
922
923 input_report_abs(input, ABS_HAT2Y, data[11]);
924 input_report_abs(input, ABS_HAT2X, data[12]);
925
926
927
928
929
930
931
932
933
934 lpad_touched = b10 & BIT(3);
935 lpad_and_joy = b10 & BIT(7);
936 x = steam_le16(data + 16);
937 y = -steam_le16(data + 18);
938
939 input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
940 input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
941
942 if (lpad_touched && !lpad_and_joy) {
943 input_report_abs(input, ABS_X, 0);
944 input_report_abs(input, ABS_Y, 0);
945 }
946
947 if (!(lpad_touched || lpad_and_joy)) {
948 input_report_abs(input, ABS_HAT0X, 0);
949 input_report_abs(input, ABS_HAT0Y, 0);
950 }
951
952 input_report_abs(input, ABS_RX, steam_le16(data + 20));
953 input_report_abs(input, ABS_RY, -steam_le16(data + 22));
954
955 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
956 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
957 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
958 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
959 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
960 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
961 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
962 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
963 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
964 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
965 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
966 input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
967 input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
968 input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
969 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
970 input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
971 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
972 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
973 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
974 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
975 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
976
977 input_sync(input);
978}
979
980
981
982
983
984
985
986
987
988
989
990static void steam_do_battery_event(struct steam_device *steam,
991 struct power_supply *battery, u8 *data)
992{
993 unsigned long flags;
994
995 s16 volts = steam_le16(data + 12);
996 u8 batt = data[14];
997
998
999 rcu_read_lock();
1000 battery = rcu_dereference(steam->battery);
1001 if (likely(battery)) {
1002 spin_lock_irqsave(&steam->lock, flags);
1003 steam->voltage = volts;
1004 steam->battery_charge = batt;
1005 spin_unlock_irqrestore(&steam->lock, flags);
1006 power_supply_changed(battery);
1007 }
1008 rcu_read_unlock();
1009}
1010
1011static int steam_raw_event(struct hid_device *hdev,
1012 struct hid_report *report, u8 *data,
1013 int size)
1014{
1015 struct steam_device *steam = hid_get_drvdata(hdev);
1016 struct input_dev *input;
1017 struct power_supply *battery;
1018
1019 if (!steam)
1020 return 0;
1021
1022 if (steam->client_opened)
1023 hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
1024 data, size, 0);
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041 if (size != 64 || data[0] != 1 || data[1] != 0)
1042 return 0;
1043
1044 switch (data[2]) {
1045 case STEAM_EV_INPUT_DATA:
1046 if (steam->client_opened)
1047 return 0;
1048 rcu_read_lock();
1049 input = rcu_dereference(steam->input);
1050 if (likely(input))
1051 steam_do_input_event(steam, input, data);
1052 rcu_read_unlock();
1053 break;
1054 case STEAM_EV_CONNECT:
1055
1056
1057
1058
1059
1060 switch (data[4]) {
1061 case 0x01:
1062 steam_do_connect_event(steam, false);
1063 break;
1064 case 0x02:
1065 steam_do_connect_event(steam, true);
1066 break;
1067 }
1068 break;
1069 case STEAM_EV_BATTERY:
1070 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1071 rcu_read_lock();
1072 battery = rcu_dereference(steam->battery);
1073 if (likely(battery)) {
1074 steam_do_battery_event(steam, battery, data);
1075 } else {
1076 dbg_hid(
1077 "%s: battery data without connect event\n",
1078 __func__);
1079 steam_do_connect_event(steam, true);
1080 }
1081 rcu_read_unlock();
1082 }
1083 break;
1084 }
1085 return 0;
1086}
1087
1088static int steam_param_set_lizard_mode(const char *val,
1089 const struct kernel_param *kp)
1090{
1091 struct steam_device *steam;
1092 int ret;
1093
1094 ret = param_set_bool(val, kp);
1095 if (ret)
1096 return ret;
1097
1098 mutex_lock(&steam_devices_lock);
1099 list_for_each_entry(steam, &steam_devices, list) {
1100 mutex_lock(&steam->mutex);
1101 if (!steam->client_opened)
1102 steam_set_lizard_mode(steam, lizard_mode);
1103 mutex_unlock(&steam->mutex);
1104 }
1105 mutex_unlock(&steam_devices_lock);
1106 return 0;
1107}
1108
1109static const struct kernel_param_ops steam_lizard_mode_ops = {
1110 .set = steam_param_set_lizard_mode,
1111 .get = param_get_bool,
1112};
1113
1114module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
1115MODULE_PARM_DESC(lizard_mode,
1116 "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
1117
1118static const struct hid_device_id steam_controllers[] = {
1119 {
1120 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1121 USB_DEVICE_ID_STEAM_CONTROLLER)
1122 },
1123 {
1124 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1125 USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
1126 .driver_data = STEAM_QUIRK_WIRELESS
1127 },
1128 {}
1129};
1130
1131MODULE_DEVICE_TABLE(hid, steam_controllers);
1132
1133static struct hid_driver steam_controller_driver = {
1134 .name = "hid-steam",
1135 .id_table = steam_controllers,
1136 .probe = steam_probe,
1137 .remove = steam_remove,
1138 .raw_event = steam_raw_event,
1139};
1140
1141module_hid_driver(steam_controller_driver);
1142