0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #include <linux/device.h>
0037 #include <linux/input.h>
0038 #include <linux/hid.h>
0039 #include <linux/module.h>
0040 #include <linux/workqueue.h>
0041 #include <linux/mutex.h>
0042 #include <linux/rcupdate.h>
0043 #include <linux/delay.h>
0044 #include <linux/power_supply.h>
0045 #include "hid-ids.h"
0046
0047 MODULE_LICENSE("GPL");
0048 MODULE_AUTHOR("Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>");
0049
0050 static bool lizard_mode = true;
0051
0052 static DEFINE_MUTEX(steam_devices_lock);
0053 static LIST_HEAD(steam_devices);
0054
0055 #define STEAM_QUIRK_WIRELESS BIT(0)
0056
0057
0058 #define STEAM_PAD_RESOLUTION 1638
0059
0060 #define STEAM_TRIGGER_RESOLUTION 51
0061
0062 #define STEAM_JOYSTICK_RESOLUTION 51
0063
0064 #define STEAM_PAD_FUZZ 256
0065
0066
0067
0068
0069
0070 #define STEAM_CMD_SET_MAPPINGS 0x80
0071 #define STEAM_CMD_CLEAR_MAPPINGS 0x81
0072 #define STEAM_CMD_GET_MAPPINGS 0x82
0073 #define STEAM_CMD_GET_ATTRIB 0x83
0074 #define STEAM_CMD_GET_ATTRIB_LABEL 0x84
0075 #define STEAM_CMD_DEFAULT_MAPPINGS 0x85
0076 #define STEAM_CMD_FACTORY_RESET 0x86
0077 #define STEAM_CMD_WRITE_REGISTER 0x87
0078 #define STEAM_CMD_CLEAR_REGISTER 0x88
0079 #define STEAM_CMD_READ_REGISTER 0x89
0080 #define STEAM_CMD_GET_REGISTER_LABEL 0x8a
0081 #define STEAM_CMD_GET_REGISTER_MAX 0x8b
0082 #define STEAM_CMD_GET_REGISTER_DEFAULT 0x8c
0083 #define STEAM_CMD_SET_MODE 0x8d
0084 #define STEAM_CMD_DEFAULT_MOUSE 0x8e
0085 #define STEAM_CMD_FORCEFEEDBAK 0x8f
0086 #define STEAM_CMD_REQUEST_COMM_STATUS 0xb4
0087 #define STEAM_CMD_GET_SERIAL 0xae
0088
0089
0090 #define STEAM_REG_LPAD_MODE 0x07
0091 #define STEAM_REG_RPAD_MODE 0x08
0092 #define STEAM_REG_RPAD_MARGIN 0x18
0093 #define STEAM_REG_LED 0x2d
0094 #define STEAM_REG_GYRO_MODE 0x30
0095
0096
0097 #define STEAM_EV_INPUT_DATA 0x01
0098 #define STEAM_EV_CONNECT 0x03
0099 #define STEAM_EV_BATTERY 0x04
0100
0101
0102 #define STEAM_GYRO_MODE_OFF 0x0000
0103 #define STEAM_GYRO_MODE_STEERING 0x0001
0104 #define STEAM_GYRO_MODE_TILT 0x0002
0105 #define STEAM_GYRO_MODE_SEND_ORIENTATION 0x0004
0106 #define STEAM_GYRO_MODE_SEND_RAW_ACCEL 0x0008
0107 #define STEAM_GYRO_MODE_SEND_RAW_GYRO 0x0010
0108
0109
0110 #define STEAM_SERIAL_LEN 10
0111
0112 struct steam_device {
0113 struct list_head list;
0114 spinlock_t lock;
0115 struct hid_device *hdev, *client_hdev;
0116 struct mutex mutex;
0117 bool client_opened;
0118 struct input_dev __rcu *input;
0119 unsigned long quirks;
0120 struct work_struct work_connect;
0121 bool connected;
0122 char serial_no[STEAM_SERIAL_LEN + 1];
0123 struct power_supply_desc battery_desc;
0124 struct power_supply __rcu *battery;
0125 u8 battery_charge;
0126 u16 voltage;
0127 };
0128
0129 static int steam_recv_report(struct steam_device *steam,
0130 u8 *data, int size)
0131 {
0132 struct hid_report *r;
0133 u8 *buf;
0134 int ret;
0135
0136 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
0137 if (!r) {
0138 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
0139 return -EINVAL;
0140 }
0141
0142 if (hid_report_len(r) < 64)
0143 return -EINVAL;
0144
0145 buf = hid_alloc_report_buf(r, GFP_KERNEL);
0146 if (!buf)
0147 return -ENOMEM;
0148
0149
0150
0151
0152
0153
0154
0155 ret = hid_hw_raw_request(steam->hdev, 0x00,
0156 buf, hid_report_len(r) + 1,
0157 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
0158 if (ret > 0)
0159 memcpy(data, buf + 1, min(size, ret - 1));
0160 kfree(buf);
0161 return ret;
0162 }
0163
0164 static int steam_send_report(struct steam_device *steam,
0165 u8 *cmd, int size)
0166 {
0167 struct hid_report *r;
0168 u8 *buf;
0169 unsigned int retries = 50;
0170 int ret;
0171
0172 r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0];
0173 if (!r) {
0174 hid_err(steam->hdev, "No HID_FEATURE_REPORT submitted - nothing to read\n");
0175 return -EINVAL;
0176 }
0177
0178 if (hid_report_len(r) < 64)
0179 return -EINVAL;
0180
0181 buf = hid_alloc_report_buf(r, GFP_KERNEL);
0182 if (!buf)
0183 return -ENOMEM;
0184
0185
0186 memcpy(buf + 1, cmd, size);
0187
0188
0189
0190
0191
0192
0193
0194 do {
0195 ret = hid_hw_raw_request(steam->hdev, 0,
0196 buf, size + 1,
0197 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
0198 if (ret != -EPIPE)
0199 break;
0200 msleep(20);
0201 } while (--retries);
0202
0203 kfree(buf);
0204 if (ret < 0)
0205 hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__,
0206 ret, size, cmd);
0207 return ret;
0208 }
0209
0210 static inline int steam_send_report_byte(struct steam_device *steam, u8 cmd)
0211 {
0212 return steam_send_report(steam, &cmd, 1);
0213 }
0214
0215 static int steam_write_registers(struct steam_device *steam,
0216 ...)
0217 {
0218
0219 u8 reg;
0220 u16 val;
0221 u8 cmd[64] = {STEAM_CMD_WRITE_REGISTER, 0x00};
0222 va_list args;
0223
0224 va_start(args, steam);
0225 for (;;) {
0226 reg = va_arg(args, int);
0227 if (reg == 0)
0228 break;
0229 val = va_arg(args, int);
0230 cmd[cmd[1] + 2] = reg;
0231 cmd[cmd[1] + 3] = val & 0xff;
0232 cmd[cmd[1] + 4] = val >> 8;
0233 cmd[1] += 3;
0234 }
0235 va_end(args);
0236
0237 return steam_send_report(steam, cmd, 2 + cmd[1]);
0238 }
0239
0240 static int steam_get_serial(struct steam_device *steam)
0241 {
0242
0243
0244
0245
0246 int ret;
0247 u8 cmd[] = {STEAM_CMD_GET_SERIAL, 0x15, 0x01};
0248 u8 reply[3 + STEAM_SERIAL_LEN + 1];
0249
0250 ret = steam_send_report(steam, cmd, sizeof(cmd));
0251 if (ret < 0)
0252 return ret;
0253 ret = steam_recv_report(steam, reply, sizeof(reply));
0254 if (ret < 0)
0255 return ret;
0256 if (reply[0] != 0xae || reply[1] != 0x15 || reply[2] != 0x01)
0257 return -EIO;
0258 reply[3 + STEAM_SERIAL_LEN] = 0;
0259 strlcpy(steam->serial_no, reply + 3, sizeof(steam->serial_no));
0260 return 0;
0261 }
0262
0263
0264
0265
0266
0267
0268 static inline int steam_request_conn_status(struct steam_device *steam)
0269 {
0270 return steam_send_report_byte(steam, STEAM_CMD_REQUEST_COMM_STATUS);
0271 }
0272
0273 static void steam_set_lizard_mode(struct steam_device *steam, bool enable)
0274 {
0275 if (enable) {
0276
0277 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MAPPINGS);
0278
0279 steam_send_report_byte(steam, STEAM_CMD_DEFAULT_MOUSE);
0280 steam_write_registers(steam,
0281 STEAM_REG_RPAD_MARGIN, 0x01,
0282 0);
0283 } else {
0284
0285 steam_send_report_byte(steam, STEAM_CMD_CLEAR_MAPPINGS);
0286 steam_write_registers(steam,
0287 STEAM_REG_RPAD_MODE, 0x07,
0288 STEAM_REG_RPAD_MARGIN, 0x00,
0289 0);
0290 }
0291 }
0292
0293 static int steam_input_open(struct input_dev *dev)
0294 {
0295 struct steam_device *steam = input_get_drvdata(dev);
0296
0297 mutex_lock(&steam->mutex);
0298 if (!steam->client_opened && lizard_mode)
0299 steam_set_lizard_mode(steam, false);
0300 mutex_unlock(&steam->mutex);
0301 return 0;
0302 }
0303
0304 static void steam_input_close(struct input_dev *dev)
0305 {
0306 struct steam_device *steam = input_get_drvdata(dev);
0307
0308 mutex_lock(&steam->mutex);
0309 if (!steam->client_opened && lizard_mode)
0310 steam_set_lizard_mode(steam, true);
0311 mutex_unlock(&steam->mutex);
0312 }
0313
0314 static enum power_supply_property steam_battery_props[] = {
0315 POWER_SUPPLY_PROP_PRESENT,
0316 POWER_SUPPLY_PROP_SCOPE,
0317 POWER_SUPPLY_PROP_VOLTAGE_NOW,
0318 POWER_SUPPLY_PROP_CAPACITY,
0319 };
0320
0321 static int steam_battery_get_property(struct power_supply *psy,
0322 enum power_supply_property psp,
0323 union power_supply_propval *val)
0324 {
0325 struct steam_device *steam = power_supply_get_drvdata(psy);
0326 unsigned long flags;
0327 s16 volts;
0328 u8 batt;
0329 int ret = 0;
0330
0331 spin_lock_irqsave(&steam->lock, flags);
0332 volts = steam->voltage;
0333 batt = steam->battery_charge;
0334 spin_unlock_irqrestore(&steam->lock, flags);
0335
0336 switch (psp) {
0337 case POWER_SUPPLY_PROP_PRESENT:
0338 val->intval = 1;
0339 break;
0340 case POWER_SUPPLY_PROP_SCOPE:
0341 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
0342 break;
0343 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0344 val->intval = volts * 1000;
0345 break;
0346 case POWER_SUPPLY_PROP_CAPACITY:
0347 val->intval = batt;
0348 break;
0349 default:
0350 ret = -EINVAL;
0351 break;
0352 }
0353 return ret;
0354 }
0355
0356 static int steam_battery_register(struct steam_device *steam)
0357 {
0358 struct power_supply *battery;
0359 struct power_supply_config battery_cfg = { .drv_data = steam, };
0360 unsigned long flags;
0361 int ret;
0362
0363 steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
0364 steam->battery_desc.properties = steam_battery_props;
0365 steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props);
0366 steam->battery_desc.get_property = steam_battery_get_property;
0367 steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev,
0368 GFP_KERNEL, "steam-controller-%s-battery",
0369 steam->serial_no);
0370 if (!steam->battery_desc.name)
0371 return -ENOMEM;
0372
0373
0374 spin_lock_irqsave(&steam->lock, flags);
0375 steam->voltage = 3000;
0376 steam->battery_charge = 100;
0377 spin_unlock_irqrestore(&steam->lock, flags);
0378
0379 battery = power_supply_register(&steam->hdev->dev,
0380 &steam->battery_desc, &battery_cfg);
0381 if (IS_ERR(battery)) {
0382 ret = PTR_ERR(battery);
0383 hid_err(steam->hdev,
0384 "%s:power_supply_register failed with error %d\n",
0385 __func__, ret);
0386 return ret;
0387 }
0388 rcu_assign_pointer(steam->battery, battery);
0389 power_supply_powers(battery, &steam->hdev->dev);
0390 return 0;
0391 }
0392
0393 static int steam_input_register(struct steam_device *steam)
0394 {
0395 struct hid_device *hdev = steam->hdev;
0396 struct input_dev *input;
0397 int ret;
0398
0399 rcu_read_lock();
0400 input = rcu_dereference(steam->input);
0401 rcu_read_unlock();
0402 if (input) {
0403 dbg_hid("%s: already connected\n", __func__);
0404 return 0;
0405 }
0406
0407 input = input_allocate_device();
0408 if (!input)
0409 return -ENOMEM;
0410
0411 input_set_drvdata(input, steam);
0412 input->dev.parent = &hdev->dev;
0413 input->open = steam_input_open;
0414 input->close = steam_input_close;
0415
0416 input->name = (steam->quirks & STEAM_QUIRK_WIRELESS) ?
0417 "Wireless Steam Controller" :
0418 "Steam Controller";
0419 input->phys = hdev->phys;
0420 input->uniq = steam->serial_no;
0421 input->id.bustype = hdev->bus;
0422 input->id.vendor = hdev->vendor;
0423 input->id.product = hdev->product;
0424 input->id.version = hdev->version;
0425
0426 input_set_capability(input, EV_KEY, BTN_TR2);
0427 input_set_capability(input, EV_KEY, BTN_TL2);
0428 input_set_capability(input, EV_KEY, BTN_TR);
0429 input_set_capability(input, EV_KEY, BTN_TL);
0430 input_set_capability(input, EV_KEY, BTN_Y);
0431 input_set_capability(input, EV_KEY, BTN_B);
0432 input_set_capability(input, EV_KEY, BTN_X);
0433 input_set_capability(input, EV_KEY, BTN_A);
0434 input_set_capability(input, EV_KEY, BTN_DPAD_UP);
0435 input_set_capability(input, EV_KEY, BTN_DPAD_RIGHT);
0436 input_set_capability(input, EV_KEY, BTN_DPAD_LEFT);
0437 input_set_capability(input, EV_KEY, BTN_DPAD_DOWN);
0438 input_set_capability(input, EV_KEY, BTN_SELECT);
0439 input_set_capability(input, EV_KEY, BTN_MODE);
0440 input_set_capability(input, EV_KEY, BTN_START);
0441 input_set_capability(input, EV_KEY, BTN_GEAR_DOWN);
0442 input_set_capability(input, EV_KEY, BTN_GEAR_UP);
0443 input_set_capability(input, EV_KEY, BTN_THUMBR);
0444 input_set_capability(input, EV_KEY, BTN_THUMBL);
0445 input_set_capability(input, EV_KEY, BTN_THUMB);
0446 input_set_capability(input, EV_KEY, BTN_THUMB2);
0447
0448 input_set_abs_params(input, ABS_HAT2Y, 0, 255, 0, 0);
0449 input_set_abs_params(input, ABS_HAT2X, 0, 255, 0, 0);
0450 input_set_abs_params(input, ABS_X, -32767, 32767, 0, 0);
0451 input_set_abs_params(input, ABS_Y, -32767, 32767, 0, 0);
0452 input_set_abs_params(input, ABS_RX, -32767, 32767,
0453 STEAM_PAD_FUZZ, 0);
0454 input_set_abs_params(input, ABS_RY, -32767, 32767,
0455 STEAM_PAD_FUZZ, 0);
0456 input_set_abs_params(input, ABS_HAT0X, -32767, 32767,
0457 STEAM_PAD_FUZZ, 0);
0458 input_set_abs_params(input, ABS_HAT0Y, -32767, 32767,
0459 STEAM_PAD_FUZZ, 0);
0460 input_abs_set_res(input, ABS_X, STEAM_JOYSTICK_RESOLUTION);
0461 input_abs_set_res(input, ABS_Y, STEAM_JOYSTICK_RESOLUTION);
0462 input_abs_set_res(input, ABS_RX, STEAM_PAD_RESOLUTION);
0463 input_abs_set_res(input, ABS_RY, STEAM_PAD_RESOLUTION);
0464 input_abs_set_res(input, ABS_HAT0X, STEAM_PAD_RESOLUTION);
0465 input_abs_set_res(input, ABS_HAT0Y, STEAM_PAD_RESOLUTION);
0466 input_abs_set_res(input, ABS_HAT2Y, STEAM_TRIGGER_RESOLUTION);
0467 input_abs_set_res(input, ABS_HAT2X, STEAM_TRIGGER_RESOLUTION);
0468
0469 ret = input_register_device(input);
0470 if (ret)
0471 goto input_register_fail;
0472
0473 rcu_assign_pointer(steam->input, input);
0474 return 0;
0475
0476 input_register_fail:
0477 input_free_device(input);
0478 return ret;
0479 }
0480
0481 static void steam_input_unregister(struct steam_device *steam)
0482 {
0483 struct input_dev *input;
0484 rcu_read_lock();
0485 input = rcu_dereference(steam->input);
0486 rcu_read_unlock();
0487 if (!input)
0488 return;
0489 RCU_INIT_POINTER(steam->input, NULL);
0490 synchronize_rcu();
0491 input_unregister_device(input);
0492 }
0493
0494 static void steam_battery_unregister(struct steam_device *steam)
0495 {
0496 struct power_supply *battery;
0497
0498 rcu_read_lock();
0499 battery = rcu_dereference(steam->battery);
0500 rcu_read_unlock();
0501
0502 if (!battery)
0503 return;
0504 RCU_INIT_POINTER(steam->battery, NULL);
0505 synchronize_rcu();
0506 power_supply_unregister(battery);
0507 }
0508
0509 static int steam_register(struct steam_device *steam)
0510 {
0511 int ret;
0512 bool client_opened;
0513
0514
0515
0516
0517
0518
0519
0520 if (!steam->serial_no[0]) {
0521
0522
0523
0524
0525 mutex_lock(&steam->mutex);
0526 if (steam_get_serial(steam) < 0)
0527 strlcpy(steam->serial_no, "XXXXXXXXXX",
0528 sizeof(steam->serial_no));
0529 mutex_unlock(&steam->mutex);
0530
0531 hid_info(steam->hdev, "Steam Controller '%s' connected",
0532 steam->serial_no);
0533
0534
0535 if (steam->quirks & STEAM_QUIRK_WIRELESS)
0536 steam_battery_register(steam);
0537
0538 mutex_lock(&steam_devices_lock);
0539 if (list_empty(&steam->list))
0540 list_add(&steam->list, &steam_devices);
0541 mutex_unlock(&steam_devices_lock);
0542 }
0543
0544 mutex_lock(&steam->mutex);
0545 client_opened = steam->client_opened;
0546 if (!client_opened)
0547 steam_set_lizard_mode(steam, lizard_mode);
0548 mutex_unlock(&steam->mutex);
0549
0550 if (!client_opened)
0551 ret = steam_input_register(steam);
0552 else
0553 ret = 0;
0554
0555 return ret;
0556 }
0557
0558 static void steam_unregister(struct steam_device *steam)
0559 {
0560 steam_battery_unregister(steam);
0561 steam_input_unregister(steam);
0562 if (steam->serial_no[0]) {
0563 hid_info(steam->hdev, "Steam Controller '%s' disconnected",
0564 steam->serial_no);
0565 mutex_lock(&steam_devices_lock);
0566 list_del_init(&steam->list);
0567 mutex_unlock(&steam_devices_lock);
0568 steam->serial_no[0] = 0;
0569 }
0570 }
0571
0572 static void steam_work_connect_cb(struct work_struct *work)
0573 {
0574 struct steam_device *steam = container_of(work, struct steam_device,
0575 work_connect);
0576 unsigned long flags;
0577 bool connected;
0578 int ret;
0579
0580 spin_lock_irqsave(&steam->lock, flags);
0581 connected = steam->connected;
0582 spin_unlock_irqrestore(&steam->lock, flags);
0583
0584 if (connected) {
0585 ret = steam_register(steam);
0586 if (ret) {
0587 hid_err(steam->hdev,
0588 "%s:steam_register failed with error %d\n",
0589 __func__, ret);
0590 }
0591 } else {
0592 steam_unregister(steam);
0593 }
0594 }
0595
0596 static bool steam_is_valve_interface(struct hid_device *hdev)
0597 {
0598 struct hid_report_enum *rep_enum;
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
0612 return !list_empty(&rep_enum->report_list);
0613 }
0614
0615 static int steam_client_ll_parse(struct hid_device *hdev)
0616 {
0617 struct steam_device *steam = hdev->driver_data;
0618
0619 return hid_parse_report(hdev, steam->hdev->dev_rdesc,
0620 steam->hdev->dev_rsize);
0621 }
0622
0623 static int steam_client_ll_start(struct hid_device *hdev)
0624 {
0625 return 0;
0626 }
0627
0628 static void steam_client_ll_stop(struct hid_device *hdev)
0629 {
0630 }
0631
0632 static int steam_client_ll_open(struct hid_device *hdev)
0633 {
0634 struct steam_device *steam = hdev->driver_data;
0635
0636 mutex_lock(&steam->mutex);
0637 steam->client_opened = true;
0638 mutex_unlock(&steam->mutex);
0639
0640 steam_input_unregister(steam);
0641
0642 return 0;
0643 }
0644
0645 static void steam_client_ll_close(struct hid_device *hdev)
0646 {
0647 struct steam_device *steam = hdev->driver_data;
0648
0649 unsigned long flags;
0650 bool connected;
0651
0652 spin_lock_irqsave(&steam->lock, flags);
0653 connected = steam->connected;
0654 spin_unlock_irqrestore(&steam->lock, flags);
0655
0656 mutex_lock(&steam->mutex);
0657 steam->client_opened = false;
0658 if (connected)
0659 steam_set_lizard_mode(steam, lizard_mode);
0660 mutex_unlock(&steam->mutex);
0661
0662 if (connected)
0663 steam_input_register(steam);
0664 }
0665
0666 static int steam_client_ll_raw_request(struct hid_device *hdev,
0667 unsigned char reportnum, u8 *buf,
0668 size_t count, unsigned char report_type,
0669 int reqtype)
0670 {
0671 struct steam_device *steam = hdev->driver_data;
0672
0673 return hid_hw_raw_request(steam->hdev, reportnum, buf, count,
0674 report_type, reqtype);
0675 }
0676
0677 static struct hid_ll_driver steam_client_ll_driver = {
0678 .parse = steam_client_ll_parse,
0679 .start = steam_client_ll_start,
0680 .stop = steam_client_ll_stop,
0681 .open = steam_client_ll_open,
0682 .close = steam_client_ll_close,
0683 .raw_request = steam_client_ll_raw_request,
0684 };
0685
0686 static struct hid_device *steam_create_client_hid(struct hid_device *hdev)
0687 {
0688 struct hid_device *client_hdev;
0689
0690 client_hdev = hid_allocate_device();
0691 if (IS_ERR(client_hdev))
0692 return client_hdev;
0693
0694 client_hdev->ll_driver = &steam_client_ll_driver;
0695 client_hdev->dev.parent = hdev->dev.parent;
0696 client_hdev->bus = hdev->bus;
0697 client_hdev->vendor = hdev->vendor;
0698 client_hdev->product = hdev->product;
0699 client_hdev->version = hdev->version;
0700 client_hdev->type = hdev->type;
0701 client_hdev->country = hdev->country;
0702 strlcpy(client_hdev->name, hdev->name,
0703 sizeof(client_hdev->name));
0704 strlcpy(client_hdev->phys, hdev->phys,
0705 sizeof(client_hdev->phys));
0706
0707
0708
0709
0710
0711 client_hdev->group = HID_GROUP_STEAM;
0712 return client_hdev;
0713 }
0714
0715 static int steam_probe(struct hid_device *hdev,
0716 const struct hid_device_id *id)
0717 {
0718 struct steam_device *steam;
0719 int ret;
0720
0721 ret = hid_parse(hdev);
0722 if (ret) {
0723 hid_err(hdev,
0724 "%s:parse of hid interface failed\n", __func__);
0725 return ret;
0726 }
0727
0728
0729
0730
0731
0732 if (hdev->group == HID_GROUP_STEAM)
0733 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
0734
0735
0736
0737
0738 if (!steam_is_valve_interface(hdev))
0739 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0740
0741 steam = devm_kzalloc(&hdev->dev, sizeof(*steam), GFP_KERNEL);
0742 if (!steam) {
0743 ret = -ENOMEM;
0744 goto steam_alloc_fail;
0745 }
0746 steam->hdev = hdev;
0747 hid_set_drvdata(hdev, steam);
0748 spin_lock_init(&steam->lock);
0749 mutex_init(&steam->mutex);
0750 steam->quirks = id->driver_data;
0751 INIT_WORK(&steam->work_connect, steam_work_connect_cb);
0752 INIT_LIST_HEAD(&steam->list);
0753
0754 steam->client_hdev = steam_create_client_hid(hdev);
0755 if (IS_ERR(steam->client_hdev)) {
0756 ret = PTR_ERR(steam->client_hdev);
0757 goto client_hdev_fail;
0758 }
0759 steam->client_hdev->driver_data = steam;
0760
0761
0762
0763
0764
0765 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDRAW);
0766 if (ret)
0767 goto hid_hw_start_fail;
0768
0769 ret = hid_add_device(steam->client_hdev);
0770 if (ret)
0771 goto client_hdev_add_fail;
0772
0773 ret = hid_hw_open(hdev);
0774 if (ret) {
0775 hid_err(hdev,
0776 "%s:hid_hw_open\n",
0777 __func__);
0778 goto hid_hw_open_fail;
0779 }
0780
0781 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
0782 hid_info(hdev, "Steam wireless receiver connected");
0783
0784 steam->connected = false;
0785 steam_request_conn_status(steam);
0786 } else {
0787
0788 steam->connected = true;
0789 ret = steam_register(steam);
0790 if (ret) {
0791 hid_err(hdev,
0792 "%s:steam_register failed with error %d\n",
0793 __func__, ret);
0794 goto input_register_fail;
0795 }
0796 }
0797
0798 return 0;
0799
0800 input_register_fail:
0801 hid_hw_open_fail:
0802 client_hdev_add_fail:
0803 hid_hw_stop(hdev);
0804 hid_hw_start_fail:
0805 hid_destroy_device(steam->client_hdev);
0806 client_hdev_fail:
0807 cancel_work_sync(&steam->work_connect);
0808 steam_alloc_fail:
0809 hid_err(hdev, "%s: failed with error %d\n",
0810 __func__, ret);
0811 return ret;
0812 }
0813
0814 static void steam_remove(struct hid_device *hdev)
0815 {
0816 struct steam_device *steam = hid_get_drvdata(hdev);
0817
0818 if (!steam || hdev->group == HID_GROUP_STEAM) {
0819 hid_hw_stop(hdev);
0820 return;
0821 }
0822
0823 hid_destroy_device(steam->client_hdev);
0824 steam->client_opened = false;
0825 cancel_work_sync(&steam->work_connect);
0826 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
0827 hid_info(hdev, "Steam wireless receiver disconnected");
0828 }
0829 hid_hw_close(hdev);
0830 hid_hw_stop(hdev);
0831 steam_unregister(steam);
0832 }
0833
0834 static void steam_do_connect_event(struct steam_device *steam, bool connected)
0835 {
0836 unsigned long flags;
0837 bool changed;
0838
0839 spin_lock_irqsave(&steam->lock, flags);
0840 changed = steam->connected != connected;
0841 steam->connected = connected;
0842 spin_unlock_irqrestore(&steam->lock, flags);
0843
0844 if (changed && schedule_work(&steam->work_connect) == 0)
0845 dbg_hid("%s: connected=%d event already queued\n",
0846 __func__, connected);
0847 }
0848
0849
0850
0851
0852
0853
0854 static inline s16 steam_le16(u8 *data)
0855 {
0856 s16 x = (s16) le16_to_cpup((__le16 *)data);
0857
0858 return x == -32768 ? -32767 : x;
0859 }
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927 static void steam_do_input_event(struct steam_device *steam,
0928 struct input_dev *input, u8 *data)
0929 {
0930
0931 u8 b8, b9, b10;
0932 s16 x, y;
0933 bool lpad_touched, lpad_and_joy;
0934
0935 b8 = data[8];
0936 b9 = data[9];
0937 b10 = data[10];
0938
0939 input_report_abs(input, ABS_HAT2Y, data[11]);
0940 input_report_abs(input, ABS_HAT2X, data[12]);
0941
0942
0943
0944
0945
0946
0947
0948
0949
0950 lpad_touched = b10 & BIT(3);
0951 lpad_and_joy = b10 & BIT(7);
0952 x = steam_le16(data + 16);
0953 y = -steam_le16(data + 18);
0954
0955 input_report_abs(input, lpad_touched ? ABS_HAT0X : ABS_X, x);
0956 input_report_abs(input, lpad_touched ? ABS_HAT0Y : ABS_Y, y);
0957
0958 if (lpad_touched && !lpad_and_joy) {
0959 input_report_abs(input, ABS_X, 0);
0960 input_report_abs(input, ABS_Y, 0);
0961 }
0962
0963 if (!(lpad_touched || lpad_and_joy)) {
0964 input_report_abs(input, ABS_HAT0X, 0);
0965 input_report_abs(input, ABS_HAT0Y, 0);
0966 }
0967
0968 input_report_abs(input, ABS_RX, steam_le16(data + 20));
0969 input_report_abs(input, ABS_RY, -steam_le16(data + 22));
0970
0971 input_event(input, EV_KEY, BTN_TR2, !!(b8 & BIT(0)));
0972 input_event(input, EV_KEY, BTN_TL2, !!(b8 & BIT(1)));
0973 input_event(input, EV_KEY, BTN_TR, !!(b8 & BIT(2)));
0974 input_event(input, EV_KEY, BTN_TL, !!(b8 & BIT(3)));
0975 input_event(input, EV_KEY, BTN_Y, !!(b8 & BIT(4)));
0976 input_event(input, EV_KEY, BTN_B, !!(b8 & BIT(5)));
0977 input_event(input, EV_KEY, BTN_X, !!(b8 & BIT(6)));
0978 input_event(input, EV_KEY, BTN_A, !!(b8 & BIT(7)));
0979 input_event(input, EV_KEY, BTN_SELECT, !!(b9 & BIT(4)));
0980 input_event(input, EV_KEY, BTN_MODE, !!(b9 & BIT(5)));
0981 input_event(input, EV_KEY, BTN_START, !!(b9 & BIT(6)));
0982 input_event(input, EV_KEY, BTN_GEAR_DOWN, !!(b9 & BIT(7)));
0983 input_event(input, EV_KEY, BTN_GEAR_UP, !!(b10 & BIT(0)));
0984 input_event(input, EV_KEY, BTN_THUMBR, !!(b10 & BIT(2)));
0985 input_event(input, EV_KEY, BTN_THUMBL, !!(b10 & BIT(6)));
0986 input_event(input, EV_KEY, BTN_THUMB, lpad_touched || lpad_and_joy);
0987 input_event(input, EV_KEY, BTN_THUMB2, !!(b10 & BIT(4)));
0988 input_event(input, EV_KEY, BTN_DPAD_UP, !!(b9 & BIT(0)));
0989 input_event(input, EV_KEY, BTN_DPAD_RIGHT, !!(b9 & BIT(1)));
0990 input_event(input, EV_KEY, BTN_DPAD_LEFT, !!(b9 & BIT(2)));
0991 input_event(input, EV_KEY, BTN_DPAD_DOWN, !!(b9 & BIT(3)));
0992
0993 input_sync(input);
0994 }
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006 static void steam_do_battery_event(struct steam_device *steam,
1007 struct power_supply *battery, u8 *data)
1008 {
1009 unsigned long flags;
1010
1011 s16 volts = steam_le16(data + 12);
1012 u8 batt = data[14];
1013
1014
1015 rcu_read_lock();
1016 battery = rcu_dereference(steam->battery);
1017 if (likely(battery)) {
1018 spin_lock_irqsave(&steam->lock, flags);
1019 steam->voltage = volts;
1020 steam->battery_charge = batt;
1021 spin_unlock_irqrestore(&steam->lock, flags);
1022 power_supply_changed(battery);
1023 }
1024 rcu_read_unlock();
1025 }
1026
1027 static int steam_raw_event(struct hid_device *hdev,
1028 struct hid_report *report, u8 *data,
1029 int size)
1030 {
1031 struct steam_device *steam = hid_get_drvdata(hdev);
1032 struct input_dev *input;
1033 struct power_supply *battery;
1034
1035 if (!steam)
1036 return 0;
1037
1038 if (steam->client_opened)
1039 hid_input_report(steam->client_hdev, HID_FEATURE_REPORT,
1040 data, size, 0);
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057 if (size != 64 || data[0] != 1 || data[1] != 0)
1058 return 0;
1059
1060 switch (data[2]) {
1061 case STEAM_EV_INPUT_DATA:
1062 if (steam->client_opened)
1063 return 0;
1064 rcu_read_lock();
1065 input = rcu_dereference(steam->input);
1066 if (likely(input))
1067 steam_do_input_event(steam, input, data);
1068 rcu_read_unlock();
1069 break;
1070 case STEAM_EV_CONNECT:
1071
1072
1073
1074
1075
1076 switch (data[4]) {
1077 case 0x01:
1078 steam_do_connect_event(steam, false);
1079 break;
1080 case 0x02:
1081 steam_do_connect_event(steam, true);
1082 break;
1083 }
1084 break;
1085 case STEAM_EV_BATTERY:
1086 if (steam->quirks & STEAM_QUIRK_WIRELESS) {
1087 rcu_read_lock();
1088 battery = rcu_dereference(steam->battery);
1089 if (likely(battery)) {
1090 steam_do_battery_event(steam, battery, data);
1091 } else {
1092 dbg_hid(
1093 "%s: battery data without connect event\n",
1094 __func__);
1095 steam_do_connect_event(steam, true);
1096 }
1097 rcu_read_unlock();
1098 }
1099 break;
1100 }
1101 return 0;
1102 }
1103
1104 static int steam_param_set_lizard_mode(const char *val,
1105 const struct kernel_param *kp)
1106 {
1107 struct steam_device *steam;
1108 int ret;
1109
1110 ret = param_set_bool(val, kp);
1111 if (ret)
1112 return ret;
1113
1114 mutex_lock(&steam_devices_lock);
1115 list_for_each_entry(steam, &steam_devices, list) {
1116 mutex_lock(&steam->mutex);
1117 if (!steam->client_opened)
1118 steam_set_lizard_mode(steam, lizard_mode);
1119 mutex_unlock(&steam->mutex);
1120 }
1121 mutex_unlock(&steam_devices_lock);
1122 return 0;
1123 }
1124
1125 static const struct kernel_param_ops steam_lizard_mode_ops = {
1126 .set = steam_param_set_lizard_mode,
1127 .get = param_get_bool,
1128 };
1129
1130 module_param_cb(lizard_mode, &steam_lizard_mode_ops, &lizard_mode, 0644);
1131 MODULE_PARM_DESC(lizard_mode,
1132 "Enable mouse and keyboard emulation (lizard mode) when the gamepad is not in use");
1133
1134 static const struct hid_device_id steam_controllers[] = {
1135 {
1136 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1137 USB_DEVICE_ID_STEAM_CONTROLLER)
1138 },
1139 {
1140 HID_USB_DEVICE(USB_VENDOR_ID_VALVE,
1141 USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS),
1142 .driver_data = STEAM_QUIRK_WIRELESS
1143 },
1144 {}
1145 };
1146
1147 MODULE_DEVICE_TABLE(hid, steam_controllers);
1148
1149 static struct hid_driver steam_controller_driver = {
1150 .name = "hid-steam",
1151 .id_table = steam_controllers,
1152 .probe = steam_probe,
1153 .remove = steam_remove,
1154 .raw_event = steam_raw_event,
1155 };
1156
1157 module_hid_driver(steam_controller_driver);