0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012
0013 #include <linux/device.h>
0014 #include <linux/input.h>
0015 #include <linux/usb.h>
0016 #include <linux/hid.h>
0017 #include <linux/module.h>
0018 #include <linux/slab.h>
0019 #include <linux/sched.h>
0020 #include <linux/sched/clock.h>
0021 #include <linux/kfifo.h>
0022 #include <linux/input/mt.h>
0023 #include <linux/workqueue.h>
0024 #include <linux/atomic.h>
0025 #include <linux/fixp-arith.h>
0026 #include <asm/unaligned.h>
0027 #include "usbhid/usbhid.h"
0028 #include "hid-ids.h"
0029
0030 MODULE_LICENSE("GPL");
0031 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
0032 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
0033
0034 static bool disable_raw_mode;
0035 module_param(disable_raw_mode, bool, 0644);
0036 MODULE_PARM_DESC(disable_raw_mode,
0037 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
0038
0039 static bool disable_tap_to_click;
0040 module_param(disable_tap_to_click, bool, 0644);
0041 MODULE_PARM_DESC(disable_tap_to_click,
0042 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
0043
0044 #define REPORT_ID_HIDPP_SHORT 0x10
0045 #define REPORT_ID_HIDPP_LONG 0x11
0046 #define REPORT_ID_HIDPP_VERY_LONG 0x12
0047
0048 #define HIDPP_REPORT_SHORT_LENGTH 7
0049 #define HIDPP_REPORT_LONG_LENGTH 20
0050 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
0051
0052 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
0053 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
0054 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
0055
0056 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
0057 #define HIDPP_SUB_ID_ROLLER 0x05
0058 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
0059 #define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
0060 #define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
0061
0062 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
0063 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
0064 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
0065 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
0066 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
0067
0068
0069
0070 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
0071 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
0072 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
0073 #define HIDPP_QUIRK_UNIFYING BIT(25)
0074 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
0075 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
0076 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
0077 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
0078 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
0079 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
0080
0081
0082 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
0083 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
0084
0085
0086 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
0087 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
0088 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
0089
0090 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
0091
0092 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
0093 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
0094 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
0095 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
0096 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
0097 #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
0098 #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
0099
0100 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 struct fap {
0125 u8 feature_index;
0126 u8 funcindex_clientid;
0127 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
0128 };
0129
0130 struct rap {
0131 u8 sub_id;
0132 u8 reg_address;
0133 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
0134 };
0135
0136 struct hidpp_report {
0137 u8 report_id;
0138 u8 device_index;
0139 union {
0140 struct fap fap;
0141 struct rap rap;
0142 u8 rawbytes[sizeof(struct fap)];
0143 };
0144 } __packed;
0145
0146 struct hidpp_battery {
0147 u8 feature_index;
0148 u8 solar_feature_index;
0149 u8 voltage_feature_index;
0150 struct power_supply_desc desc;
0151 struct power_supply *ps;
0152 char name[64];
0153 int status;
0154 int capacity;
0155 int level;
0156 int voltage;
0157 int charge_type;
0158 bool online;
0159 u8 supported_levels_1004;
0160 };
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 struct hidpp_scroll_counter {
0174 int wheel_multiplier;
0175 int remainder;
0176 int direction;
0177 unsigned long long last_time;
0178 };
0179
0180 struct hidpp_device {
0181 struct hid_device *hid_dev;
0182 struct input_dev *input;
0183 struct mutex send_mutex;
0184 void *send_receive_buf;
0185 char *name;
0186 wait_queue_head_t wait;
0187 int very_long_report_length;
0188 bool answer_available;
0189 u8 protocol_major;
0190 u8 protocol_minor;
0191
0192 void *private_data;
0193
0194 struct work_struct work;
0195 struct kfifo delayed_work_fifo;
0196 atomic_t connected;
0197 struct input_dev *delayed_input;
0198
0199 unsigned long quirks;
0200 unsigned long capabilities;
0201 u8 supported_reports;
0202
0203 struct hidpp_battery battery;
0204 struct hidpp_scroll_counter vertical_wheel_counter;
0205
0206 u8 wireless_feature_index;
0207 };
0208
0209
0210 #define HIDPP_ERROR 0x8f
0211 #define HIDPP_ERROR_SUCCESS 0x00
0212 #define HIDPP_ERROR_INVALID_SUBID 0x01
0213 #define HIDPP_ERROR_INVALID_ADRESS 0x02
0214 #define HIDPP_ERROR_INVALID_VALUE 0x03
0215 #define HIDPP_ERROR_CONNECT_FAIL 0x04
0216 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
0217 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
0218 #define HIDPP_ERROR_BUSY 0x07
0219 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
0220 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
0221 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
0222 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
0223 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
0224
0225 #define HIDPP20_ERROR 0xff
0226
0227 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
0228
0229 static int __hidpp_send_report(struct hid_device *hdev,
0230 struct hidpp_report *hidpp_report)
0231 {
0232 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
0233 int fields_count, ret;
0234
0235 switch (hidpp_report->report_id) {
0236 case REPORT_ID_HIDPP_SHORT:
0237 fields_count = HIDPP_REPORT_SHORT_LENGTH;
0238 break;
0239 case REPORT_ID_HIDPP_LONG:
0240 fields_count = HIDPP_REPORT_LONG_LENGTH;
0241 break;
0242 case REPORT_ID_HIDPP_VERY_LONG:
0243 fields_count = hidpp->very_long_report_length;
0244 break;
0245 default:
0246 return -ENODEV;
0247 }
0248
0249
0250
0251
0252
0253 hidpp_report->device_index = 0xff;
0254
0255 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
0256 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
0257 } else {
0258 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
0259 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
0260 HID_REQ_SET_REPORT);
0261 }
0262
0263 return ret == fields_count ? 0 : -1;
0264 }
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
0275 struct hidpp_report *message,
0276 struct hidpp_report *response)
0277 {
0278 int ret;
0279
0280 mutex_lock(&hidpp->send_mutex);
0281
0282 hidpp->send_receive_buf = response;
0283 hidpp->answer_available = false;
0284
0285
0286
0287
0288
0289 *response = *message;
0290
0291 ret = __hidpp_send_report(hidpp->hid_dev, message);
0292
0293 if (ret) {
0294 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
0295 memset(response, 0, sizeof(struct hidpp_report));
0296 goto exit;
0297 }
0298
0299 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
0300 5*HZ)) {
0301 dbg_hid("%s:timeout waiting for response\n", __func__);
0302 memset(response, 0, sizeof(struct hidpp_report));
0303 ret = -ETIMEDOUT;
0304 }
0305
0306 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
0307 response->rap.sub_id == HIDPP_ERROR) {
0308 ret = response->rap.params[1];
0309 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
0310 goto exit;
0311 }
0312
0313 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
0314 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
0315 response->fap.feature_index == HIDPP20_ERROR) {
0316 ret = response->fap.params[1];
0317 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
0318 goto exit;
0319 }
0320
0321 exit:
0322 mutex_unlock(&hidpp->send_mutex);
0323 return ret;
0324
0325 }
0326
0327 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
0328 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
0329 struct hidpp_report *response)
0330 {
0331 struct hidpp_report *message;
0332 int ret;
0333
0334 if (param_count > sizeof(message->fap.params))
0335 return -EINVAL;
0336
0337 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
0338 if (!message)
0339 return -ENOMEM;
0340
0341 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
0342 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
0343 else
0344 message->report_id = REPORT_ID_HIDPP_LONG;
0345 message->fap.feature_index = feat_index;
0346 message->fap.funcindex_clientid = funcindex_clientid;
0347 memcpy(&message->fap.params, params, param_count);
0348
0349 ret = hidpp_send_message_sync(hidpp, message, response);
0350 kfree(message);
0351 return ret;
0352 }
0353
0354 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
0355 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
0356 struct hidpp_report *response)
0357 {
0358 struct hidpp_report *message;
0359 int ret, max_count;
0360
0361
0362 if (report_id == REPORT_ID_HIDPP_SHORT &&
0363 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
0364 report_id = REPORT_ID_HIDPP_LONG;
0365
0366 switch (report_id) {
0367 case REPORT_ID_HIDPP_SHORT:
0368 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
0369 break;
0370 case REPORT_ID_HIDPP_LONG:
0371 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
0372 break;
0373 case REPORT_ID_HIDPP_VERY_LONG:
0374 max_count = hidpp_dev->very_long_report_length - 4;
0375 break;
0376 default:
0377 return -EINVAL;
0378 }
0379
0380 if (param_count > max_count)
0381 return -EINVAL;
0382
0383 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
0384 if (!message)
0385 return -ENOMEM;
0386 message->report_id = report_id;
0387 message->rap.sub_id = sub_id;
0388 message->rap.reg_address = reg_address;
0389 memcpy(&message->rap.params, params, param_count);
0390
0391 ret = hidpp_send_message_sync(hidpp_dev, message, response);
0392 kfree(message);
0393 return ret;
0394 }
0395
0396 static void delayed_work_cb(struct work_struct *work)
0397 {
0398 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
0399 work);
0400 hidpp_connect_event(hidpp);
0401 }
0402
0403 static inline bool hidpp_match_answer(struct hidpp_report *question,
0404 struct hidpp_report *answer)
0405 {
0406 return (answer->fap.feature_index == question->fap.feature_index) &&
0407 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
0408 }
0409
0410 static inline bool hidpp_match_error(struct hidpp_report *question,
0411 struct hidpp_report *answer)
0412 {
0413 return ((answer->rap.sub_id == HIDPP_ERROR) ||
0414 (answer->fap.feature_index == HIDPP20_ERROR)) &&
0415 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
0416 (answer->fap.params[0] == question->fap.funcindex_clientid);
0417 }
0418
0419 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
0420 struct hidpp_report *report)
0421 {
0422 return (hidpp->wireless_feature_index &&
0423 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
0424 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
0425 (report->rap.sub_id == 0x41));
0426 }
0427
0428
0429
0430
0431 static void hidpp_prefix_name(char **name, int name_length)
0432 {
0433 #define PREFIX_LENGTH 9
0434
0435 int new_length;
0436 char *new_name;
0437
0438 if (name_length > PREFIX_LENGTH &&
0439 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
0440
0441 return;
0442
0443 new_length = PREFIX_LENGTH + name_length;
0444 new_name = kzalloc(new_length, GFP_KERNEL);
0445 if (!new_name)
0446 return;
0447
0448 snprintf(new_name, new_length, "Logitech %s", *name);
0449
0450 kfree(*name);
0451
0452 *name = new_name;
0453 }
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
0471 struct hidpp_scroll_counter *counter,
0472 int hi_res_value)
0473 {
0474 int low_res_value, remainder, direction;
0475 unsigned long long now, previous;
0476
0477 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
0478 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
0479
0480 remainder = counter->remainder;
0481 direction = hi_res_value > 0 ? 1 : -1;
0482
0483 now = sched_clock();
0484 previous = counter->last_time;
0485 counter->last_time = now;
0486
0487
0488
0489
0490
0491
0492 if (now - previous > 1000000000 || direction != counter->direction)
0493 remainder = 0;
0494
0495 counter->direction = direction;
0496 remainder += hi_res_value;
0497
0498
0499
0500
0501
0502
0503 if (abs(remainder) >= 60) {
0504
0505
0506
0507
0508
0509 low_res_value = remainder / 120;
0510 if (low_res_value == 0)
0511 low_res_value = (hi_res_value > 0 ? 1 : -1);
0512 input_report_rel(input_dev, REL_WHEEL, low_res_value);
0513 remainder -= low_res_value * 120;
0514 }
0515 counter->remainder = remainder;
0516 }
0517
0518
0519
0520
0521
0522 #define HIDPP_SET_REGISTER 0x80
0523 #define HIDPP_GET_REGISTER 0x81
0524 #define HIDPP_SET_LONG_REGISTER 0x82
0525 #define HIDPP_GET_LONG_REGISTER 0x83
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
0537 u8 register_address, u8 byte, u8 mask, u8 value)
0538 {
0539 struct hidpp_report response;
0540 int ret;
0541 u8 params[3] = { 0 };
0542
0543 ret = hidpp_send_rap_command_sync(hidpp_dev,
0544 REPORT_ID_HIDPP_SHORT,
0545 HIDPP_GET_REGISTER,
0546 register_address,
0547 NULL, 0, &response);
0548 if (ret)
0549 return ret;
0550
0551 memcpy(params, response.rap.params, 3);
0552
0553 params[byte] &= ~mask;
0554 params[byte] |= value & mask;
0555
0556 return hidpp_send_rap_command_sync(hidpp_dev,
0557 REPORT_ID_HIDPP_SHORT,
0558 HIDPP_SET_REGISTER,
0559 register_address,
0560 params, 3, &response);
0561 }
0562
0563 #define HIDPP_REG_ENABLE_REPORTS 0x00
0564 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
0565 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
0566 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
0567 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
0568 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
0569
0570 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
0571 {
0572 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
0573 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
0574 }
0575
0576 #define HIDPP_REG_FEATURES 0x01
0577 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
0578 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
0579
0580
0581 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
0582 {
0583 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
0584 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
0585 }
0586
0587 #define HIDPP_REG_BATTERY_STATUS 0x07
0588
0589 static int hidpp10_battery_status_map_level(u8 param)
0590 {
0591 int level;
0592
0593 switch (param) {
0594 case 1 ... 2:
0595 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
0596 break;
0597 case 3 ... 4:
0598 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
0599 break;
0600 case 5 ... 6:
0601 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
0602 break;
0603 case 7:
0604 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
0605 break;
0606 default:
0607 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
0608 }
0609
0610 return level;
0611 }
0612
0613 static int hidpp10_battery_status_map_status(u8 param)
0614 {
0615 int status;
0616
0617 switch (param) {
0618 case 0x00:
0619
0620 status = POWER_SUPPLY_STATUS_DISCHARGING;
0621 break;
0622 case 0x21:
0623 case 0x24:
0624 case 0x25:
0625 status = POWER_SUPPLY_STATUS_CHARGING;
0626 break;
0627 case 0x26:
0628 case 0x22:
0629 status = POWER_SUPPLY_STATUS_FULL;
0630 break;
0631 case 0x20:
0632 status = POWER_SUPPLY_STATUS_UNKNOWN;
0633 break;
0634
0635
0636
0637
0638
0639 default:
0640 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
0641 break;
0642 }
0643
0644 return status;
0645 }
0646
0647 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
0648 {
0649 struct hidpp_report response;
0650 int ret, status;
0651
0652 ret = hidpp_send_rap_command_sync(hidpp,
0653 REPORT_ID_HIDPP_SHORT,
0654 HIDPP_GET_REGISTER,
0655 HIDPP_REG_BATTERY_STATUS,
0656 NULL, 0, &response);
0657 if (ret)
0658 return ret;
0659
0660 hidpp->battery.level =
0661 hidpp10_battery_status_map_level(response.rap.params[0]);
0662 status = hidpp10_battery_status_map_status(response.rap.params[1]);
0663 hidpp->battery.status = status;
0664
0665 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
0666 status == POWER_SUPPLY_STATUS_FULL;
0667
0668 return 0;
0669 }
0670
0671 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
0672
0673 static int hidpp10_battery_mileage_map_status(u8 param)
0674 {
0675 int status;
0676
0677 switch (param >> 6) {
0678 case 0x00:
0679
0680 status = POWER_SUPPLY_STATUS_DISCHARGING;
0681 break;
0682 case 0x01:
0683 status = POWER_SUPPLY_STATUS_CHARGING;
0684 break;
0685 case 0x02:
0686 status = POWER_SUPPLY_STATUS_FULL;
0687 break;
0688
0689
0690
0691 default:
0692 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
0693 break;
0694 }
0695
0696 return status;
0697 }
0698
0699 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
0700 {
0701 struct hidpp_report response;
0702 int ret, status;
0703
0704 ret = hidpp_send_rap_command_sync(hidpp,
0705 REPORT_ID_HIDPP_SHORT,
0706 HIDPP_GET_REGISTER,
0707 HIDPP_REG_BATTERY_MILEAGE,
0708 NULL, 0, &response);
0709 if (ret)
0710 return ret;
0711
0712 hidpp->battery.capacity = response.rap.params[0];
0713 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
0714 hidpp->battery.status = status;
0715
0716 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
0717 status == POWER_SUPPLY_STATUS_FULL;
0718
0719 return 0;
0720 }
0721
0722 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
0723 {
0724 struct hidpp_report *report = (struct hidpp_report *)data;
0725 int status, capacity, level;
0726 bool changed;
0727
0728 if (report->report_id != REPORT_ID_HIDPP_SHORT)
0729 return 0;
0730
0731 switch (report->rap.sub_id) {
0732 case HIDPP_REG_BATTERY_STATUS:
0733 capacity = hidpp->battery.capacity;
0734 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
0735 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
0736 break;
0737 case HIDPP_REG_BATTERY_MILEAGE:
0738 capacity = report->rap.params[0];
0739 level = hidpp->battery.level;
0740 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
0741 break;
0742 default:
0743 return 0;
0744 }
0745
0746 changed = capacity != hidpp->battery.capacity ||
0747 level != hidpp->battery.level ||
0748 status != hidpp->battery.status;
0749
0750
0751 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
0752 status == POWER_SUPPLY_STATUS_FULL;
0753
0754 if (changed) {
0755 hidpp->battery.level = level;
0756 hidpp->battery.status = status;
0757 if (hidpp->battery.ps)
0758 power_supply_changed(hidpp->battery.ps);
0759 }
0760
0761 return 0;
0762 }
0763
0764 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
0765 #define HIDPP_EXTENDED_PAIRING 0x30
0766 #define HIDPP_DEVICE_NAME 0x40
0767
0768 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
0769 {
0770 struct hidpp_report response;
0771 int ret;
0772 u8 params[1] = { HIDPP_DEVICE_NAME };
0773 char *name;
0774 int len;
0775
0776 ret = hidpp_send_rap_command_sync(hidpp_dev,
0777 REPORT_ID_HIDPP_SHORT,
0778 HIDPP_GET_LONG_REGISTER,
0779 HIDPP_REG_PAIRING_INFORMATION,
0780 params, 1, &response);
0781 if (ret)
0782 return NULL;
0783
0784 len = response.rap.params[1];
0785
0786 if (2 + len > sizeof(response.rap.params))
0787 return NULL;
0788
0789 if (len < 4)
0790 return NULL;
0791
0792 name = kzalloc(len + 1, GFP_KERNEL);
0793 if (!name)
0794 return NULL;
0795
0796 memcpy(name, &response.rap.params[2], len);
0797
0798
0799 hidpp_prefix_name(&name, len + 1);
0800
0801 return name;
0802 }
0803
0804 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
0805 {
0806 struct hidpp_report response;
0807 int ret;
0808 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
0809
0810 ret = hidpp_send_rap_command_sync(hidpp,
0811 REPORT_ID_HIDPP_SHORT,
0812 HIDPP_GET_LONG_REGISTER,
0813 HIDPP_REG_PAIRING_INFORMATION,
0814 params, 1, &response);
0815 if (ret)
0816 return ret;
0817
0818
0819
0820
0821
0822 *serial = *((u32 *)&response.rap.params[1]);
0823 return 0;
0824 }
0825
0826 static int hidpp_unifying_init(struct hidpp_device *hidpp)
0827 {
0828 struct hid_device *hdev = hidpp->hid_dev;
0829 const char *name;
0830 u32 serial;
0831 int ret;
0832
0833 ret = hidpp_unifying_get_serial(hidpp, &serial);
0834 if (ret)
0835 return ret;
0836
0837 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
0838 hdev->product, &serial);
0839 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
0840
0841 name = hidpp_unifying_get_name(hidpp);
0842 if (!name)
0843 return -EIO;
0844
0845 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
0846 dbg_hid("HID++ Unifying: Got name: %s\n", name);
0847
0848 kfree(name);
0849 return 0;
0850 }
0851
0852
0853
0854
0855
0856 #define HIDPP_PAGE_ROOT 0x0000
0857 #define HIDPP_PAGE_ROOT_IDX 0x00
0858
0859 #define CMD_ROOT_GET_FEATURE 0x01
0860 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
0861
0862 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
0863 u8 *feature_index, u8 *feature_type)
0864 {
0865 struct hidpp_report response;
0866 int ret;
0867 u8 params[2] = { feature >> 8, feature & 0x00FF };
0868
0869 ret = hidpp_send_fap_command_sync(hidpp,
0870 HIDPP_PAGE_ROOT_IDX,
0871 CMD_ROOT_GET_FEATURE,
0872 params, 2, &response);
0873 if (ret)
0874 return ret;
0875
0876 if (response.fap.params[0] == 0)
0877 return -ENOENT;
0878
0879 *feature_index = response.fap.params[0];
0880 *feature_type = response.fap.params[1];
0881
0882 return ret;
0883 }
0884
0885 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
0886 {
0887 const u8 ping_byte = 0x5a;
0888 u8 ping_data[3] = { 0, 0, ping_byte };
0889 struct hidpp_report response;
0890 int ret;
0891
0892 ret = hidpp_send_rap_command_sync(hidpp,
0893 REPORT_ID_HIDPP_SHORT,
0894 HIDPP_PAGE_ROOT_IDX,
0895 CMD_ROOT_GET_PROTOCOL_VERSION,
0896 ping_data, sizeof(ping_data), &response);
0897
0898 if (ret == HIDPP_ERROR_INVALID_SUBID) {
0899 hidpp->protocol_major = 1;
0900 hidpp->protocol_minor = 0;
0901 goto print_version;
0902 }
0903
0904
0905 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
0906 return -EIO;
0907
0908 if (ret > 0) {
0909 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
0910 __func__, ret);
0911 return -EPROTO;
0912 }
0913 if (ret)
0914 return ret;
0915
0916 if (response.rap.params[2] != ping_byte) {
0917 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
0918 __func__, response.rap.params[2], ping_byte);
0919 return -EPROTO;
0920 }
0921
0922 hidpp->protocol_major = response.rap.params[0];
0923 hidpp->protocol_minor = response.rap.params[1];
0924
0925 print_version:
0926 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
0927 hidpp->protocol_major, hidpp->protocol_minor);
0928 return 0;
0929 }
0930
0931
0932
0933
0934
0935 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
0936
0937 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
0938 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
0939 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
0940
0941 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
0942 u8 feature_index, u8 *nameLength)
0943 {
0944 struct hidpp_report response;
0945 int ret;
0946
0947 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
0948 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
0949
0950 if (ret > 0) {
0951 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
0952 __func__, ret);
0953 return -EPROTO;
0954 }
0955 if (ret)
0956 return ret;
0957
0958 *nameLength = response.fap.params[0];
0959
0960 return ret;
0961 }
0962
0963 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
0964 u8 feature_index, u8 char_index, char *device_name, int len_buf)
0965 {
0966 struct hidpp_report response;
0967 int ret, i;
0968 int count;
0969
0970 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
0971 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
0972 &response);
0973
0974 if (ret > 0) {
0975 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
0976 __func__, ret);
0977 return -EPROTO;
0978 }
0979 if (ret)
0980 return ret;
0981
0982 switch (response.report_id) {
0983 case REPORT_ID_HIDPP_VERY_LONG:
0984 count = hidpp->very_long_report_length - 4;
0985 break;
0986 case REPORT_ID_HIDPP_LONG:
0987 count = HIDPP_REPORT_LONG_LENGTH - 4;
0988 break;
0989 case REPORT_ID_HIDPP_SHORT:
0990 count = HIDPP_REPORT_SHORT_LENGTH - 4;
0991 break;
0992 default:
0993 return -EPROTO;
0994 }
0995
0996 if (len_buf < count)
0997 count = len_buf;
0998
0999 for (i = 0; i < count; i++)
1000 device_name[i] = response.fap.params[i];
1001
1002 return count;
1003 }
1004
1005 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1006 {
1007 u8 feature_type;
1008 u8 feature_index;
1009 u8 __name_length;
1010 char *name;
1011 unsigned index = 0;
1012 int ret;
1013
1014 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1015 &feature_index, &feature_type);
1016 if (ret)
1017 return NULL;
1018
1019 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1020 &__name_length);
1021 if (ret)
1022 return NULL;
1023
1024 name = kzalloc(__name_length + 1, GFP_KERNEL);
1025 if (!name)
1026 return NULL;
1027
1028 while (index < __name_length) {
1029 ret = hidpp_devicenametype_get_device_name(hidpp,
1030 feature_index, index, name + index,
1031 __name_length - index);
1032 if (ret <= 0) {
1033 kfree(name);
1034 return NULL;
1035 }
1036 index += ret;
1037 }
1038
1039
1040 hidpp_prefix_name(&name, __name_length + 1);
1041
1042 return name;
1043 }
1044
1045
1046
1047
1048
1049 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1050
1051 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1052 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1053
1054 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1055
1056 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1057 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1058 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1059
1060 static int hidpp_map_battery_level(int capacity)
1061 {
1062 if (capacity < 11)
1063 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1064
1065
1066
1067
1068 else if (capacity < 30)
1069 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1070 else if (capacity < 81)
1071 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1072 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1073 }
1074
1075 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1076 int *next_capacity,
1077 int *level)
1078 {
1079 int status;
1080
1081 *capacity = data[0];
1082 *next_capacity = data[1];
1083 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1084
1085
1086
1087
1088 switch (data[2]) {
1089 case 0:
1090 status = POWER_SUPPLY_STATUS_DISCHARGING;
1091 *level = hidpp_map_battery_level(*capacity);
1092 break;
1093 case 1:
1094 status = POWER_SUPPLY_STATUS_CHARGING;
1095 break;
1096 case 2:
1097 status = POWER_SUPPLY_STATUS_CHARGING;
1098 break;
1099 case 3:
1100 status = POWER_SUPPLY_STATUS_FULL;
1101 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1102 *capacity = 100;
1103 break;
1104 case 4:
1105 status = POWER_SUPPLY_STATUS_CHARGING;
1106 break;
1107
1108
1109
1110 default:
1111 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1112 break;
1113 }
1114
1115 return status;
1116 }
1117
1118 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1119 u8 feature_index,
1120 int *status,
1121 int *capacity,
1122 int *next_capacity,
1123 int *level)
1124 {
1125 struct hidpp_report response;
1126 int ret;
1127 u8 *params = (u8 *)response.fap.params;
1128
1129 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1130 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1131 NULL, 0, &response);
1132
1133 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1134 return -EIO;
1135 if (ret > 0) {
1136 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1137 __func__, ret);
1138 return -EPROTO;
1139 }
1140 if (ret)
1141 return ret;
1142
1143 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1144 next_capacity,
1145 level);
1146
1147 return 0;
1148 }
1149
1150 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1151 u8 feature_index)
1152 {
1153 struct hidpp_report response;
1154 int ret;
1155 u8 *params = (u8 *)response.fap.params;
1156 unsigned int level_count, flags;
1157
1158 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1159 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1160 NULL, 0, &response);
1161 if (ret > 0) {
1162 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1163 __func__, ret);
1164 return -EPROTO;
1165 }
1166 if (ret)
1167 return ret;
1168
1169 level_count = params[0];
1170 flags = params[1];
1171
1172 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1173 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1174 else
1175 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1176
1177 return 0;
1178 }
1179
1180 static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1181 {
1182 u8 feature_type;
1183 int ret;
1184 int status, capacity, next_capacity, level;
1185
1186 if (hidpp->battery.feature_index == 0xff) {
1187 ret = hidpp_root_get_feature(hidpp,
1188 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1189 &hidpp->battery.feature_index,
1190 &feature_type);
1191 if (ret)
1192 return ret;
1193 }
1194
1195 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1196 hidpp->battery.feature_index,
1197 &status, &capacity,
1198 &next_capacity, &level);
1199 if (ret)
1200 return ret;
1201
1202 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1203 hidpp->battery.feature_index);
1204 if (ret)
1205 return ret;
1206
1207 hidpp->battery.status = status;
1208 hidpp->battery.capacity = capacity;
1209 hidpp->battery.level = level;
1210
1211 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1212 status == POWER_SUPPLY_STATUS_FULL;
1213
1214 return 0;
1215 }
1216
1217 static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1218 u8 *data, int size)
1219 {
1220 struct hidpp_report *report = (struct hidpp_report *)data;
1221 int status, capacity, next_capacity, level;
1222 bool changed;
1223
1224 if (report->fap.feature_index != hidpp->battery.feature_index ||
1225 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1226 return 0;
1227
1228 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1229 &capacity,
1230 &next_capacity,
1231 &level);
1232
1233
1234 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1235 status == POWER_SUPPLY_STATUS_FULL;
1236
1237 changed = capacity != hidpp->battery.capacity ||
1238 level != hidpp->battery.level ||
1239 status != hidpp->battery.status;
1240
1241 if (changed) {
1242 hidpp->battery.level = level;
1243 hidpp->battery.capacity = capacity;
1244 hidpp->battery.status = status;
1245 if (hidpp->battery.ps)
1246 power_supply_changed(hidpp->battery.ps);
1247 }
1248
1249 return 0;
1250 }
1251
1252
1253
1254
1255
1256 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1257
1258 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1259
1260 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1261
1262 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1263 int *level, int *charge_type)
1264 {
1265 int status;
1266
1267 long flags = (long) data[2];
1268 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1269
1270 if (flags & 0x80)
1271 switch (flags & 0x07) {
1272 case 0:
1273 status = POWER_SUPPLY_STATUS_CHARGING;
1274 break;
1275 case 1:
1276 status = POWER_SUPPLY_STATUS_FULL;
1277 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1278 break;
1279 case 2:
1280 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1281 break;
1282 default:
1283 status = POWER_SUPPLY_STATUS_UNKNOWN;
1284 break;
1285 }
1286 else
1287 status = POWER_SUPPLY_STATUS_DISCHARGING;
1288
1289 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1290 if (test_bit(3, &flags)) {
1291 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1292 }
1293 if (test_bit(4, &flags)) {
1294 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1295 }
1296 if (test_bit(5, &flags)) {
1297 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1298 }
1299
1300 *voltage = get_unaligned_be16(data);
1301
1302 return status;
1303 }
1304
1305 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1306 u8 feature_index,
1307 int *status, int *voltage,
1308 int *level, int *charge_type)
1309 {
1310 struct hidpp_report response;
1311 int ret;
1312 u8 *params = (u8 *)response.fap.params;
1313
1314 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1315 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1316 NULL, 0, &response);
1317
1318 if (ret > 0) {
1319 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1320 __func__, ret);
1321 return -EPROTO;
1322 }
1323 if (ret)
1324 return ret;
1325
1326 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1327
1328 *status = hidpp20_battery_map_status_voltage(params, voltage,
1329 level, charge_type);
1330
1331 return 0;
1332 }
1333
1334 static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
1335 {
1336
1337
1338
1339
1340
1341 static const int voltages[] = {
1342 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
1343 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
1344 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
1345 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
1346 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
1347 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
1348 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
1349 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
1350 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
1351 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
1352 };
1353
1354 int i;
1355
1356 BUILD_BUG_ON(ARRAY_SIZE(voltages) != 100);
1357
1358 if (unlikely(voltage < 3500 || voltage >= 5000))
1359 hid_warn_once(hid_dev,
1360 "%s: possibly using the wrong voltage curve\n",
1361 __func__);
1362
1363 for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1364 if (voltage >= voltages[i])
1365 return ARRAY_SIZE(voltages) - i;
1366 }
1367
1368 return 0;
1369 }
1370
1371 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1372 {
1373 u8 feature_type;
1374 int ret;
1375 int status, voltage, level, charge_type;
1376
1377 if (hidpp->battery.voltage_feature_index == 0xff) {
1378 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1379 &hidpp->battery.voltage_feature_index,
1380 &feature_type);
1381 if (ret)
1382 return ret;
1383 }
1384
1385 ret = hidpp20_battery_get_battery_voltage(hidpp,
1386 hidpp->battery.voltage_feature_index,
1387 &status, &voltage, &level, &charge_type);
1388
1389 if (ret)
1390 return ret;
1391
1392 hidpp->battery.status = status;
1393 hidpp->battery.voltage = voltage;
1394 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1395 voltage);
1396 hidpp->battery.level = level;
1397 hidpp->battery.charge_type = charge_type;
1398 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1399
1400 return 0;
1401 }
1402
1403 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1404 u8 *data, int size)
1405 {
1406 struct hidpp_report *report = (struct hidpp_report *)data;
1407 int status, voltage, level, charge_type;
1408
1409 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1410 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1411 return 0;
1412
1413 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1414 &level, &charge_type);
1415
1416 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1417
1418 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1419 hidpp->battery.voltage = voltage;
1420 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1421 voltage);
1422 hidpp->battery.status = status;
1423 hidpp->battery.level = level;
1424 hidpp->battery.charge_type = charge_type;
1425 if (hidpp->battery.ps)
1426 power_supply_changed(hidpp->battery.ps);
1427 }
1428 return 0;
1429 }
1430
1431
1432
1433
1434
1435 #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1436
1437 #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1438 #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1439
1440 #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1441
1442 #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1443 #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1444 #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1445 #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1446
1447 #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1448 #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1449
1450 static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1451 u8 feature_index)
1452 {
1453 struct hidpp_report response;
1454 int ret;
1455 u8 *params = (u8 *)response.fap.params;
1456
1457 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1458 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1459
1460 return 0;
1461 }
1462
1463 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1464 CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1465 NULL, 0, &response);
1466
1467 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1468 return -EIO;
1469 if (ret > 0) {
1470 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1471 __func__, ret);
1472 return -EPROTO;
1473 }
1474 if (ret)
1475 return ret;
1476
1477
1478
1479
1480
1481
1482
1483
1484 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1485 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1486 hidpp->battery.supported_levels_1004 = 0;
1487 } else {
1488 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1489 hidpp->battery.supported_levels_1004 = params[0];
1490 }
1491
1492 return 0;
1493 }
1494
1495 static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1496 u8 charging_status,
1497 u8 external_power_status)
1498 {
1499 int status;
1500
1501 switch (charging_status) {
1502 case 0:
1503 status = POWER_SUPPLY_STATUS_DISCHARGING;
1504 break;
1505 case 1:
1506 case 2:
1507 status = POWER_SUPPLY_STATUS_CHARGING;
1508 break;
1509 case 3:
1510 status = POWER_SUPPLY_STATUS_FULL;
1511 break;
1512 case 4:
1513 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1514 hid_info(hidpp->hid_dev, "%s: charging error",
1515 hidpp->name);
1516 break;
1517 default:
1518 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1519 break;
1520 }
1521
1522 return status;
1523 }
1524
1525 static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1526 u8 battery_level)
1527 {
1528
1529 battery_level &= hidpp->battery.supported_levels_1004;
1530
1531 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1532 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1533 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1534 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1535 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1536 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1537 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1538 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1539
1540 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1541 }
1542
1543 static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1544 u8 feature_index,
1545 u8 *state_of_charge,
1546 int *status,
1547 int *level)
1548 {
1549 struct hidpp_report response;
1550 int ret;
1551 u8 *params = (u8 *)response.fap.params;
1552
1553 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1554 CMD_UNIFIED_BATTERY_GET_STATUS,
1555 NULL, 0, &response);
1556
1557 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1558 return -EIO;
1559 if (ret > 0) {
1560 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1561 __func__, ret);
1562 return -EPROTO;
1563 }
1564 if (ret)
1565 return ret;
1566
1567 *state_of_charge = params[0];
1568 *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1569 *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1570
1571 return 0;
1572 }
1573
1574 static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1575 {
1576 u8 feature_type;
1577 int ret;
1578 u8 state_of_charge;
1579 int status, level;
1580
1581 if (hidpp->battery.feature_index == 0xff) {
1582 ret = hidpp_root_get_feature(hidpp,
1583 HIDPP_PAGE_UNIFIED_BATTERY,
1584 &hidpp->battery.feature_index,
1585 &feature_type);
1586 if (ret)
1587 return ret;
1588 }
1589
1590 ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1591 hidpp->battery.feature_index);
1592 if (ret)
1593 return ret;
1594
1595 ret = hidpp20_unifiedbattery_get_status(hidpp,
1596 hidpp->battery.feature_index,
1597 &state_of_charge,
1598 &status,
1599 &level);
1600 if (ret)
1601 return ret;
1602
1603 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1604 hidpp->battery.capacity = state_of_charge;
1605 hidpp->battery.status = status;
1606 hidpp->battery.level = level;
1607 hidpp->battery.online = true;
1608
1609 return 0;
1610 }
1611
1612 static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1613 u8 *data, int size)
1614 {
1615 struct hidpp_report *report = (struct hidpp_report *)data;
1616 u8 *params = (u8 *)report->fap.params;
1617 int state_of_charge, status, level;
1618 bool changed;
1619
1620 if (report->fap.feature_index != hidpp->battery.feature_index ||
1621 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1622 return 0;
1623
1624 state_of_charge = params[0];
1625 status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1626 level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1627
1628 changed = status != hidpp->battery.status ||
1629 (state_of_charge != hidpp->battery.capacity &&
1630 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1631 (level != hidpp->battery.level &&
1632 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1633
1634 if (changed) {
1635 hidpp->battery.capacity = state_of_charge;
1636 hidpp->battery.status = status;
1637 hidpp->battery.level = level;
1638 if (hidpp->battery.ps)
1639 power_supply_changed(hidpp->battery.ps);
1640 }
1641
1642 return 0;
1643 }
1644
1645
1646
1647
1648
1649 static enum power_supply_property hidpp_battery_props[] = {
1650 POWER_SUPPLY_PROP_ONLINE,
1651 POWER_SUPPLY_PROP_STATUS,
1652 POWER_SUPPLY_PROP_SCOPE,
1653 POWER_SUPPLY_PROP_MODEL_NAME,
1654 POWER_SUPPLY_PROP_MANUFACTURER,
1655 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1656 0,
1657 0,
1658 0,
1659 };
1660
1661 static int hidpp_battery_get_property(struct power_supply *psy,
1662 enum power_supply_property psp,
1663 union power_supply_propval *val)
1664 {
1665 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1666 int ret = 0;
1667
1668 switch(psp) {
1669 case POWER_SUPPLY_PROP_STATUS:
1670 val->intval = hidpp->battery.status;
1671 break;
1672 case POWER_SUPPLY_PROP_CAPACITY:
1673 val->intval = hidpp->battery.capacity;
1674 break;
1675 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1676 val->intval = hidpp->battery.level;
1677 break;
1678 case POWER_SUPPLY_PROP_SCOPE:
1679 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1680 break;
1681 case POWER_SUPPLY_PROP_ONLINE:
1682 val->intval = hidpp->battery.online;
1683 break;
1684 case POWER_SUPPLY_PROP_MODEL_NAME:
1685 if (!strncmp(hidpp->name, "Logitech ", 9))
1686 val->strval = hidpp->name + 9;
1687 else
1688 val->strval = hidpp->name;
1689 break;
1690 case POWER_SUPPLY_PROP_MANUFACTURER:
1691 val->strval = "Logitech";
1692 break;
1693 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1694 val->strval = hidpp->hid_dev->uniq;
1695 break;
1696 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1697
1698 val->intval = hidpp->battery.voltage * 1000;
1699 break;
1700 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1701 val->intval = hidpp->battery.charge_type;
1702 break;
1703 default:
1704 ret = -EINVAL;
1705 break;
1706 }
1707
1708 return ret;
1709 }
1710
1711
1712
1713
1714 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1715
1716 static int hidpp_set_wireless_feature_index(struct hidpp_device *hidpp)
1717 {
1718 u8 feature_type;
1719 int ret;
1720
1721 ret = hidpp_root_get_feature(hidpp,
1722 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1723 &hidpp->wireless_feature_index,
1724 &feature_type);
1725
1726 return ret;
1727 }
1728
1729
1730
1731
1732
1733 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1734
1735 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1736
1737 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1738 bool enabled, u8 *multiplier)
1739 {
1740 u8 feature_index;
1741 u8 feature_type;
1742 int ret;
1743 u8 params[1];
1744 struct hidpp_report response;
1745
1746 ret = hidpp_root_get_feature(hidpp,
1747 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1748 &feature_index,
1749 &feature_type);
1750 if (ret)
1751 return ret;
1752
1753 params[0] = enabled ? BIT(0) : 0;
1754 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1755 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1756 params, sizeof(params), &response);
1757 if (ret)
1758 return ret;
1759 *multiplier = response.fap.params[1];
1760 return 0;
1761 }
1762
1763
1764
1765
1766
1767 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1768
1769 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1770 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1771
1772 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1773 u8 *multiplier)
1774 {
1775 u8 feature_index;
1776 u8 feature_type;
1777 int ret;
1778 struct hidpp_report response;
1779
1780 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1781 &feature_index, &feature_type);
1782 if (ret)
1783 goto return_default;
1784
1785 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1786 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1787 NULL, 0, &response);
1788 if (ret)
1789 goto return_default;
1790
1791 *multiplier = response.fap.params[0];
1792 return 0;
1793 return_default:
1794 hid_warn(hidpp->hid_dev,
1795 "Couldn't get wheel multiplier (error %d)\n", ret);
1796 return ret;
1797 }
1798
1799 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1800 bool high_resolution, bool use_hidpp)
1801 {
1802 u8 feature_index;
1803 u8 feature_type;
1804 int ret;
1805 u8 params[1];
1806 struct hidpp_report response;
1807
1808 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1809 &feature_index, &feature_type);
1810 if (ret)
1811 return ret;
1812
1813 params[0] = (invert ? BIT(2) : 0) |
1814 (high_resolution ? BIT(1) : 0) |
1815 (use_hidpp ? BIT(0) : 0);
1816
1817 return hidpp_send_fap_command_sync(hidpp, feature_index,
1818 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1819 params, sizeof(params), &response);
1820 }
1821
1822
1823
1824
1825
1826 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1827
1828 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1829
1830 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1831 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1832 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1833
1834 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1835 {
1836 struct hidpp_report response;
1837 u8 params[2] = { 1, 1 };
1838 u8 feature_type;
1839 int ret;
1840
1841 if (hidpp->battery.feature_index == 0xff) {
1842 ret = hidpp_root_get_feature(hidpp,
1843 HIDPP_PAGE_SOLAR_KEYBOARD,
1844 &hidpp->battery.solar_feature_index,
1845 &feature_type);
1846 if (ret)
1847 return ret;
1848 }
1849
1850 ret = hidpp_send_fap_command_sync(hidpp,
1851 hidpp->battery.solar_feature_index,
1852 CMD_SOLAR_SET_LIGHT_MEASURE,
1853 params, 2, &response);
1854 if (ret > 0) {
1855 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1856 __func__, ret);
1857 return -EPROTO;
1858 }
1859 if (ret)
1860 return ret;
1861
1862 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1863
1864 return 0;
1865 }
1866
1867 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1868 u8 *data, int size)
1869 {
1870 struct hidpp_report *report = (struct hidpp_report *)data;
1871 int capacity, lux, status;
1872 u8 function;
1873
1874 function = report->fap.funcindex_clientid;
1875
1876
1877 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1878 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1879 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1880 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1881 return 0;
1882
1883 capacity = report->fap.params[0];
1884
1885 switch (function) {
1886 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1887 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1888 if (lux > 200)
1889 status = POWER_SUPPLY_STATUS_CHARGING;
1890 else
1891 status = POWER_SUPPLY_STATUS_DISCHARGING;
1892 break;
1893 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1894 default:
1895 if (capacity < hidpp->battery.capacity)
1896 status = POWER_SUPPLY_STATUS_DISCHARGING;
1897 else
1898 status = POWER_SUPPLY_STATUS_CHARGING;
1899
1900 }
1901
1902 if (capacity == 100)
1903 status = POWER_SUPPLY_STATUS_FULL;
1904
1905 hidpp->battery.online = true;
1906 if (capacity != hidpp->battery.capacity ||
1907 status != hidpp->battery.status) {
1908 hidpp->battery.capacity = capacity;
1909 hidpp->battery.status = status;
1910 if (hidpp->battery.ps)
1911 power_supply_changed(hidpp->battery.ps);
1912 }
1913
1914 return 0;
1915 }
1916
1917
1918
1919
1920
1921 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1922
1923 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1924
1925 struct hidpp_touchpad_fw_items {
1926 uint8_t presence;
1927 uint8_t desired_state;
1928 uint8_t state;
1929 uint8_t persistent;
1930 };
1931
1932
1933
1934
1935
1936 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1937 u8 feature_index,
1938 struct hidpp_touchpad_fw_items *items)
1939 {
1940 struct hidpp_report response;
1941 int ret;
1942 u8 *params = (u8 *)response.fap.params;
1943
1944 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1945 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1946
1947 if (ret > 0) {
1948 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1949 __func__, ret);
1950 return -EPROTO;
1951 }
1952 if (ret)
1953 return ret;
1954
1955 items->presence = params[0];
1956 items->desired_state = params[1];
1957 items->state = params[2];
1958 items->persistent = params[3];
1959
1960 return 0;
1961 }
1962
1963
1964
1965
1966
1967 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1968
1969 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1970 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1971
1972 #define EVENT_TOUCHPAD_RAW_XY 0x00
1973
1974 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1975 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1976
1977 struct hidpp_touchpad_raw_info {
1978 u16 x_size;
1979 u16 y_size;
1980 u8 z_range;
1981 u8 area_range;
1982 u8 timestamp_unit;
1983 u8 maxcontacts;
1984 u8 origin;
1985 u16 res;
1986 };
1987
1988 struct hidpp_touchpad_raw_xy_finger {
1989 u8 contact_type;
1990 u8 contact_status;
1991 u16 x;
1992 u16 y;
1993 u8 z;
1994 u8 area;
1995 u8 finger_id;
1996 };
1997
1998 struct hidpp_touchpad_raw_xy {
1999 u16 timestamp;
2000 struct hidpp_touchpad_raw_xy_finger fingers[2];
2001 u8 spurious_flag;
2002 u8 end_of_frame;
2003 u8 finger_count;
2004 u8 button;
2005 };
2006
2007 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
2008 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
2009 {
2010 struct hidpp_report response;
2011 int ret;
2012 u8 *params = (u8 *)response.fap.params;
2013
2014 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2015 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
2016
2017 if (ret > 0) {
2018 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2019 __func__, ret);
2020 return -EPROTO;
2021 }
2022 if (ret)
2023 return ret;
2024
2025 raw_info->x_size = get_unaligned_be16(¶ms[0]);
2026 raw_info->y_size = get_unaligned_be16(¶ms[2]);
2027 raw_info->z_range = params[4];
2028 raw_info->area_range = params[5];
2029 raw_info->maxcontacts = params[7];
2030 raw_info->origin = params[8];
2031
2032 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
2033
2034 return ret;
2035 }
2036
2037 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
2038 u8 feature_index, bool send_raw_reports,
2039 bool sensor_enhanced_settings)
2040 {
2041 struct hidpp_report response;
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2053
2054 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2055 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
2056 }
2057
2058 static void hidpp_touchpad_touch_event(u8 *data,
2059 struct hidpp_touchpad_raw_xy_finger *finger)
2060 {
2061 u8 x_m = data[0] << 2;
2062 u8 y_m = data[2] << 2;
2063
2064 finger->x = x_m << 6 | data[1];
2065 finger->y = y_m << 6 | data[3];
2066
2067 finger->contact_type = data[0] >> 6;
2068 finger->contact_status = data[2] >> 6;
2069
2070 finger->z = data[4];
2071 finger->area = data[5];
2072 finger->finger_id = data[6] >> 4;
2073 }
2074
2075 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2076 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2077 {
2078 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2079 raw_xy->end_of_frame = data[8] & 0x01;
2080 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2081 raw_xy->finger_count = data[15] & 0x0f;
2082 raw_xy->button = (data[8] >> 2) & 0x01;
2083
2084 if (raw_xy->finger_count) {
2085 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2086 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2087 }
2088 }
2089
2090
2091
2092
2093
2094 #define HIDPP_FF_GET_INFO 0x01
2095 #define HIDPP_FF_RESET_ALL 0x11
2096 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2097 #define HIDPP_FF_SET_EFFECT_STATE 0x31
2098 #define HIDPP_FF_DESTROY_EFFECT 0x41
2099 #define HIDPP_FF_GET_APERTURE 0x51
2100 #define HIDPP_FF_SET_APERTURE 0x61
2101 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2102 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2103
2104 #define HIDPP_FF_EFFECT_STATE_GET 0x00
2105 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
2106 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2107 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2108
2109 #define HIDPP_FF_EFFECT_CONSTANT 0x00
2110 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2111 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2112 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2113 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2114 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2115 #define HIDPP_FF_EFFECT_SPRING 0x06
2116 #define HIDPP_FF_EFFECT_DAMPER 0x07
2117 #define HIDPP_FF_EFFECT_FRICTION 0x08
2118 #define HIDPP_FF_EFFECT_INERTIA 0x09
2119 #define HIDPP_FF_EFFECT_RAMP 0x0A
2120
2121 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
2122
2123 #define HIDPP_FF_EFFECTID_NONE -1
2124 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
2125 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2126
2127 #define HIDPP_FF_MAX_PARAMS 20
2128 #define HIDPP_FF_RESERVED_SLOTS 1
2129
2130 struct hidpp_ff_private_data {
2131 struct hidpp_device *hidpp;
2132 u8 feature_index;
2133 u8 version;
2134 u16 gain;
2135 s16 range;
2136 u8 slot_autocenter;
2137 u8 num_effects;
2138 int *effect_ids;
2139 struct workqueue_struct *wq;
2140 atomic_t workqueue_size;
2141 };
2142
2143 struct hidpp_ff_work_data {
2144 struct work_struct work;
2145 struct hidpp_ff_private_data *data;
2146 int effect_id;
2147 u8 command;
2148 u8 params[HIDPP_FF_MAX_PARAMS];
2149 u8 size;
2150 };
2151
2152 static const signed short hidpp_ff_effects[] = {
2153 FF_CONSTANT,
2154 FF_PERIODIC,
2155 FF_SINE,
2156 FF_SQUARE,
2157 FF_SAW_UP,
2158 FF_SAW_DOWN,
2159 FF_TRIANGLE,
2160 FF_SPRING,
2161 FF_DAMPER,
2162 FF_AUTOCENTER,
2163 FF_GAIN,
2164 -1
2165 };
2166
2167 static const signed short hidpp_ff_effects_v2[] = {
2168 FF_RAMP,
2169 FF_FRICTION,
2170 FF_INERTIA,
2171 -1
2172 };
2173
2174 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2175 HIDPP_FF_EFFECT_SPRING,
2176 HIDPP_FF_EFFECT_FRICTION,
2177 HIDPP_FF_EFFECT_DAMPER,
2178 HIDPP_FF_EFFECT_INERTIA
2179 };
2180
2181 static const char *HIDPP_FF_CONDITION_NAMES[] = {
2182 "spring",
2183 "friction",
2184 "damper",
2185 "inertia"
2186 };
2187
2188
2189 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2190 {
2191 int i;
2192
2193 for (i = 0; i < data->num_effects; i++)
2194 if (data->effect_ids[i] == effect_id)
2195 return i+1;
2196
2197 return 0;
2198 }
2199
2200 static void hidpp_ff_work_handler(struct work_struct *w)
2201 {
2202 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2203 struct hidpp_ff_private_data *data = wd->data;
2204 struct hidpp_report response;
2205 u8 slot;
2206 int ret;
2207
2208
2209 switch (wd->effect_id) {
2210 case HIDPP_FF_EFFECTID_AUTOCENTER:
2211 wd->params[0] = data->slot_autocenter;
2212 break;
2213 case HIDPP_FF_EFFECTID_NONE:
2214
2215 break;
2216 default:
2217
2218 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2219 break;
2220 }
2221
2222
2223 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2224 wd->command, wd->params, wd->size, &response);
2225
2226 if (ret) {
2227 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2228 goto out;
2229 }
2230
2231
2232 switch (wd->command) {
2233 case HIDPP_FF_DOWNLOAD_EFFECT:
2234 slot = response.fap.params[0];
2235 if (slot > 0 && slot <= data->num_effects) {
2236 if (wd->effect_id >= 0)
2237
2238 data->effect_ids[slot-1] = wd->effect_id;
2239 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2240
2241 data->slot_autocenter = slot;
2242 }
2243 break;
2244 case HIDPP_FF_DESTROY_EFFECT:
2245 if (wd->effect_id >= 0)
2246
2247 data->effect_ids[wd->params[0]-1] = -1;
2248 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2249
2250 data->slot_autocenter = 0;
2251 break;
2252 case HIDPP_FF_SET_GLOBAL_GAINS:
2253 data->gain = (wd->params[0] << 8) + wd->params[1];
2254 break;
2255 case HIDPP_FF_SET_APERTURE:
2256 data->range = (wd->params[0] << 8) + wd->params[1];
2257 break;
2258 default:
2259
2260 break;
2261 }
2262
2263 out:
2264 atomic_dec(&data->workqueue_size);
2265 kfree(wd);
2266 }
2267
2268 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2269 {
2270 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2271 int s;
2272
2273 if (!wd)
2274 return -ENOMEM;
2275
2276 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2277
2278 wd->data = data;
2279 wd->effect_id = effect_id;
2280 wd->command = command;
2281 wd->size = size;
2282 memcpy(wd->params, params, size);
2283
2284 s = atomic_inc_return(&data->workqueue_size);
2285 queue_work(data->wq, &wd->work);
2286
2287
2288 if (s >= 20 && s % 20 == 0)
2289 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2290
2291 return 0;
2292 }
2293
2294 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2295 {
2296 struct hidpp_ff_private_data *data = dev->ff->private;
2297 u8 params[20];
2298 u8 size;
2299 int force;
2300
2301
2302 params[2] = effect->replay.length >> 8;
2303 params[3] = effect->replay.length & 255;
2304 params[4] = effect->replay.delay >> 8;
2305 params[5] = effect->replay.delay & 255;
2306
2307 switch (effect->type) {
2308 case FF_CONSTANT:
2309 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2310 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2311 params[6] = force >> 8;
2312 params[7] = force & 255;
2313 params[8] = effect->u.constant.envelope.attack_level >> 7;
2314 params[9] = effect->u.constant.envelope.attack_length >> 8;
2315 params[10] = effect->u.constant.envelope.attack_length & 255;
2316 params[11] = effect->u.constant.envelope.fade_level >> 7;
2317 params[12] = effect->u.constant.envelope.fade_length >> 8;
2318 params[13] = effect->u.constant.envelope.fade_length & 255;
2319 size = 14;
2320 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2321 effect->u.constant.level,
2322 effect->direction, force);
2323 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2324 effect->u.constant.envelope.attack_level,
2325 effect->u.constant.envelope.attack_length,
2326 effect->u.constant.envelope.fade_level,
2327 effect->u.constant.envelope.fade_length);
2328 break;
2329 case FF_PERIODIC:
2330 {
2331 switch (effect->u.periodic.waveform) {
2332 case FF_SINE:
2333 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2334 break;
2335 case FF_SQUARE:
2336 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2337 break;
2338 case FF_SAW_UP:
2339 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2340 break;
2341 case FF_SAW_DOWN:
2342 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2343 break;
2344 case FF_TRIANGLE:
2345 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2346 break;
2347 default:
2348 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2349 return -EINVAL;
2350 }
2351 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2352 params[6] = effect->u.periodic.magnitude >> 8;
2353 params[7] = effect->u.periodic.magnitude & 255;
2354 params[8] = effect->u.periodic.offset >> 8;
2355 params[9] = effect->u.periodic.offset & 255;
2356 params[10] = effect->u.periodic.period >> 8;
2357 params[11] = effect->u.periodic.period & 255;
2358 params[12] = effect->u.periodic.phase >> 8;
2359 params[13] = effect->u.periodic.phase & 255;
2360 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2361 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2362 params[16] = effect->u.periodic.envelope.attack_length & 255;
2363 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2364 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2365 params[19] = effect->u.periodic.envelope.fade_length & 255;
2366 size = 20;
2367 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2368 effect->u.periodic.magnitude, effect->direction,
2369 effect->u.periodic.offset,
2370 effect->u.periodic.period,
2371 effect->u.periodic.phase);
2372 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2373 effect->u.periodic.envelope.attack_level,
2374 effect->u.periodic.envelope.attack_length,
2375 effect->u.periodic.envelope.fade_level,
2376 effect->u.periodic.envelope.fade_length);
2377 break;
2378 }
2379 case FF_RAMP:
2380 params[1] = HIDPP_FF_EFFECT_RAMP;
2381 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2382 params[6] = force >> 8;
2383 params[7] = force & 255;
2384 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2385 params[8] = force >> 8;
2386 params[9] = force & 255;
2387 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2388 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2389 params[12] = effect->u.ramp.envelope.attack_length & 255;
2390 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2391 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2392 params[15] = effect->u.ramp.envelope.fade_length & 255;
2393 size = 16;
2394 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2395 effect->u.ramp.start_level,
2396 effect->u.ramp.end_level,
2397 effect->direction, force);
2398 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2399 effect->u.ramp.envelope.attack_level,
2400 effect->u.ramp.envelope.attack_length,
2401 effect->u.ramp.envelope.fade_level,
2402 effect->u.ramp.envelope.fade_length);
2403 break;
2404 case FF_FRICTION:
2405 case FF_INERTIA:
2406 case FF_SPRING:
2407 case FF_DAMPER:
2408 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2409 params[6] = effect->u.condition[0].left_saturation >> 9;
2410 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2411 params[8] = effect->u.condition[0].left_coeff >> 8;
2412 params[9] = effect->u.condition[0].left_coeff & 255;
2413 params[10] = effect->u.condition[0].deadband >> 9;
2414 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2415 params[12] = effect->u.condition[0].center >> 8;
2416 params[13] = effect->u.condition[0].center & 255;
2417 params[14] = effect->u.condition[0].right_coeff >> 8;
2418 params[15] = effect->u.condition[0].right_coeff & 255;
2419 params[16] = effect->u.condition[0].right_saturation >> 9;
2420 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2421 size = 18;
2422 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2423 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2424 effect->u.condition[0].left_coeff,
2425 effect->u.condition[0].left_saturation,
2426 effect->u.condition[0].right_coeff,
2427 effect->u.condition[0].right_saturation);
2428 dbg_hid(" deadband=%d, center=%d\n",
2429 effect->u.condition[0].deadband,
2430 effect->u.condition[0].center);
2431 break;
2432 default:
2433 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2434 return -EINVAL;
2435 }
2436
2437 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2438 }
2439
2440 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2441 {
2442 struct hidpp_ff_private_data *data = dev->ff->private;
2443 u8 params[2];
2444
2445 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2446
2447 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2448
2449 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2450 }
2451
2452 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2453 {
2454 struct hidpp_ff_private_data *data = dev->ff->private;
2455 u8 slot = 0;
2456
2457 dbg_hid("Erasing effect %d.\n", effect_id);
2458
2459 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2460 }
2461
2462 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2463 {
2464 struct hidpp_ff_private_data *data = dev->ff->private;
2465 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2466
2467 dbg_hid("Setting autocenter to %d.\n", magnitude);
2468
2469
2470 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2471
2472 params[2] = params[3] = params[4] = params[5] = 0;
2473
2474 params[8] = params[14] = magnitude >> 11;
2475 params[9] = params[15] = (magnitude >> 3) & 255;
2476 params[6] = params[16] = magnitude >> 9;
2477 params[7] = params[17] = (magnitude >> 1) & 255;
2478
2479 params[10] = params[11] = params[12] = params[13] = 0;
2480
2481 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2482 }
2483
2484 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2485 {
2486 struct hidpp_ff_private_data *data = dev->ff->private;
2487 u8 params[4];
2488
2489 dbg_hid("Setting gain to %d.\n", gain);
2490
2491 params[0] = gain >> 8;
2492 params[1] = gain & 255;
2493 params[2] = 0;
2494 params[3] = 0;
2495
2496 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2497 }
2498
2499 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2500 {
2501 struct hid_device *hid = to_hid_device(dev);
2502 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2503 struct input_dev *idev = hidinput->input;
2504 struct hidpp_ff_private_data *data = idev->ff->private;
2505
2506 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2507 }
2508
2509 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2510 {
2511 struct hid_device *hid = to_hid_device(dev);
2512 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2513 struct input_dev *idev = hidinput->input;
2514 struct hidpp_ff_private_data *data = idev->ff->private;
2515 u8 params[2];
2516 int range = simple_strtoul(buf, NULL, 10);
2517
2518 range = clamp(range, 180, 900);
2519
2520 params[0] = range >> 8;
2521 params[1] = range & 0x00FF;
2522
2523 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2524
2525 return count;
2526 }
2527
2528 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2529
2530 static void hidpp_ff_destroy(struct ff_device *ff)
2531 {
2532 struct hidpp_ff_private_data *data = ff->private;
2533 struct hid_device *hid = data->hidpp->hid_dev;
2534
2535 hid_info(hid, "Unloading HID++ force feedback.\n");
2536
2537 device_remove_file(&hid->dev, &dev_attr_range);
2538 destroy_workqueue(data->wq);
2539 kfree(data->effect_ids);
2540 }
2541
2542 static int hidpp_ff_init(struct hidpp_device *hidpp,
2543 struct hidpp_ff_private_data *data)
2544 {
2545 struct hid_device *hid = hidpp->hid_dev;
2546 struct hid_input *hidinput;
2547 struct input_dev *dev;
2548 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2549 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2550 struct ff_device *ff;
2551 int error, j, num_slots = data->num_effects;
2552 u8 version;
2553
2554 if (list_empty(&hid->inputs)) {
2555 hid_err(hid, "no inputs found\n");
2556 return -ENODEV;
2557 }
2558 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2559 dev = hidinput->input;
2560
2561 if (!dev) {
2562 hid_err(hid, "Struct input_dev not set!\n");
2563 return -EINVAL;
2564 }
2565
2566
2567 version = bcdDevice & 255;
2568
2569
2570 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2571 set_bit(hidpp_ff_effects[j], dev->ffbit);
2572 if (version > 1)
2573 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2574 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2575
2576 error = input_ff_create(dev, num_slots);
2577
2578 if (error) {
2579 hid_err(dev, "Failed to create FF device!\n");
2580 return error;
2581 }
2582
2583
2584
2585
2586 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2587 if (!data)
2588 return -ENOMEM;
2589 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2590 if (!data->effect_ids) {
2591 kfree(data);
2592 return -ENOMEM;
2593 }
2594 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2595 if (!data->wq) {
2596 kfree(data->effect_ids);
2597 kfree(data);
2598 return -ENOMEM;
2599 }
2600
2601 data->hidpp = hidpp;
2602 data->version = version;
2603 for (j = 0; j < num_slots; j++)
2604 data->effect_ids[j] = -1;
2605
2606 ff = dev->ff;
2607 ff->private = data;
2608
2609 ff->upload = hidpp_ff_upload_effect;
2610 ff->erase = hidpp_ff_erase_effect;
2611 ff->playback = hidpp_ff_playback;
2612 ff->set_gain = hidpp_ff_set_gain;
2613 ff->set_autocenter = hidpp_ff_set_autocenter;
2614 ff->destroy = hidpp_ff_destroy;
2615
2616
2617 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2618 if (error)
2619 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2620
2621
2622 atomic_set(&data->workqueue_size, 0);
2623
2624 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2625 version);
2626
2627 return 0;
2628 }
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640 #define WTP_MANUAL_RESOLUTION 39
2641
2642 struct wtp_data {
2643 u16 x_size, y_size;
2644 u8 finger_count;
2645 u8 mt_feature_index;
2646 u8 button_feature_index;
2647 u8 maxcontacts;
2648 bool flip_y;
2649 unsigned int resolution;
2650 };
2651
2652 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2653 struct hid_field *field, struct hid_usage *usage,
2654 unsigned long **bit, int *max)
2655 {
2656 return -1;
2657 }
2658
2659 static void wtp_populate_input(struct hidpp_device *hidpp,
2660 struct input_dev *input_dev)
2661 {
2662 struct wtp_data *wd = hidpp->private_data;
2663
2664 __set_bit(EV_ABS, input_dev->evbit);
2665 __set_bit(EV_KEY, input_dev->evbit);
2666 __clear_bit(EV_REL, input_dev->evbit);
2667 __clear_bit(EV_LED, input_dev->evbit);
2668
2669 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2670 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2671 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2672 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2673
2674
2675 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2676
2677 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2678
2679 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2680 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2681 else
2682 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2683
2684 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2685 INPUT_MT_DROP_UNUSED);
2686 }
2687
2688 static void wtp_touch_event(struct hidpp_device *hidpp,
2689 struct hidpp_touchpad_raw_xy_finger *touch_report)
2690 {
2691 struct wtp_data *wd = hidpp->private_data;
2692 int slot;
2693
2694 if (!touch_report->finger_id || touch_report->contact_type)
2695
2696 return;
2697
2698 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2699
2700 input_mt_slot(hidpp->input, slot);
2701 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2702 touch_report->contact_status);
2703 if (touch_report->contact_status) {
2704 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2705 touch_report->x);
2706 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2707 wd->flip_y ? wd->y_size - touch_report->y :
2708 touch_report->y);
2709 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2710 touch_report->area);
2711 }
2712 }
2713
2714 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2715 struct hidpp_touchpad_raw_xy *raw)
2716 {
2717 int i;
2718
2719 for (i = 0; i < 2; i++)
2720 wtp_touch_event(hidpp, &(raw->fingers[i]));
2721
2722 if (raw->end_of_frame &&
2723 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2724 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2725
2726 if (raw->end_of_frame || raw->finger_count <= 2) {
2727 input_mt_sync_frame(hidpp->input);
2728 input_sync(hidpp->input);
2729 }
2730 }
2731
2732 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2733 {
2734 struct wtp_data *wd = hidpp->private_data;
2735 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2736 (data[7] >> 4) * (data[7] >> 4)) / 2;
2737 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2738 (data[13] >> 4) * (data[13] >> 4)) / 2;
2739 struct hidpp_touchpad_raw_xy raw = {
2740 .timestamp = data[1],
2741 .fingers = {
2742 {
2743 .contact_type = 0,
2744 .contact_status = !!data[7],
2745 .x = get_unaligned_le16(&data[3]),
2746 .y = get_unaligned_le16(&data[5]),
2747 .z = c1_area,
2748 .area = c1_area,
2749 .finger_id = data[2],
2750 }, {
2751 .contact_type = 0,
2752 .contact_status = !!data[13],
2753 .x = get_unaligned_le16(&data[9]),
2754 .y = get_unaligned_le16(&data[11]),
2755 .z = c2_area,
2756 .area = c2_area,
2757 .finger_id = data[8],
2758 }
2759 },
2760 .finger_count = wd->maxcontacts,
2761 .spurious_flag = 0,
2762 .end_of_frame = (data[0] >> 7) == 0,
2763 .button = data[0] & 0x01,
2764 };
2765
2766 wtp_send_raw_xy_event(hidpp, &raw);
2767
2768 return 1;
2769 }
2770
2771 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2772 {
2773 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2774 struct wtp_data *wd = hidpp->private_data;
2775 struct hidpp_report *report = (struct hidpp_report *)data;
2776 struct hidpp_touchpad_raw_xy raw;
2777
2778 if (!wd || !hidpp->input)
2779 return 1;
2780
2781 switch (data[0]) {
2782 case 0x02:
2783 if (size < 2) {
2784 hid_err(hdev, "Received HID report of bad size (%d)",
2785 size);
2786 return 1;
2787 }
2788 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2789 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2790 !!(data[1] & 0x01));
2791 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2792 !!(data[1] & 0x02));
2793 input_sync(hidpp->input);
2794 return 0;
2795 } else {
2796 if (size < 21)
2797 return 1;
2798 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2799 }
2800 case REPORT_ID_HIDPP_LONG:
2801
2802 if ((report->fap.feature_index != wd->mt_feature_index) ||
2803 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2804 return 1;
2805 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2806
2807 wtp_send_raw_xy_event(hidpp, &raw);
2808 return 0;
2809 }
2810
2811 return 0;
2812 }
2813
2814 static int wtp_get_config(struct hidpp_device *hidpp)
2815 {
2816 struct wtp_data *wd = hidpp->private_data;
2817 struct hidpp_touchpad_raw_info raw_info = {0};
2818 u8 feature_type;
2819 int ret;
2820
2821 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2822 &wd->mt_feature_index, &feature_type);
2823 if (ret)
2824
2825 return ret;
2826
2827 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2828 &raw_info);
2829 if (ret)
2830 return ret;
2831
2832 wd->x_size = raw_info.x_size;
2833 wd->y_size = raw_info.y_size;
2834 wd->maxcontacts = raw_info.maxcontacts;
2835 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2836 wd->resolution = raw_info.res;
2837 if (!wd->resolution)
2838 wd->resolution = WTP_MANUAL_RESOLUTION;
2839
2840 return 0;
2841 }
2842
2843 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2844 {
2845 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2846 struct wtp_data *wd;
2847
2848 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2849 GFP_KERNEL);
2850 if (!wd)
2851 return -ENOMEM;
2852
2853 hidpp->private_data = wd;
2854
2855 return 0;
2856 };
2857
2858 static int wtp_connect(struct hid_device *hdev, bool connected)
2859 {
2860 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2861 struct wtp_data *wd = hidpp->private_data;
2862 int ret;
2863
2864 if (!wd->x_size) {
2865 ret = wtp_get_config(hidpp);
2866 if (ret) {
2867 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2868 return ret;
2869 }
2870 }
2871
2872 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2873 true, true);
2874 }
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2910
2911
2912 #define M560_MOUSE_BTN_LEFT 0x01
2913 #define M560_MOUSE_BTN_RIGHT 0x02
2914 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2915 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2916
2917 #define M560_SUB_ID 0x0a
2918 #define M560_BUTTON_MODE_REGISTER 0x35
2919
2920 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2921 {
2922 struct hidpp_report response;
2923 struct hidpp_device *hidpp_dev;
2924
2925 hidpp_dev = hid_get_drvdata(hdev);
2926
2927 return hidpp_send_rap_command_sync(
2928 hidpp_dev,
2929 REPORT_ID_HIDPP_SHORT,
2930 M560_SUB_ID,
2931 M560_BUTTON_MODE_REGISTER,
2932 (u8 *)m560_config_parameter,
2933 sizeof(m560_config_parameter),
2934 &response
2935 );
2936 }
2937
2938 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2939 {
2940 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2941
2942
2943 if (!hidpp->input) {
2944 hid_err(hdev, "error in parameter\n");
2945 return -EINVAL;
2946 }
2947
2948 if (size < 7) {
2949 hid_err(hdev, "error in report\n");
2950 return 0;
2951 }
2952
2953 if (data[0] == REPORT_ID_HIDPP_LONG &&
2954 data[2] == M560_SUB_ID && data[6] == 0x00) {
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968 switch (data[5]) {
2969 case 0xaf:
2970 input_report_key(hidpp->input, BTN_MIDDLE, 1);
2971 break;
2972 case 0xb0:
2973 input_report_key(hidpp->input, BTN_FORWARD, 1);
2974 break;
2975 case 0xae:
2976 input_report_key(hidpp->input, BTN_BACK, 1);
2977 break;
2978 case 0x00:
2979 input_report_key(hidpp->input, BTN_BACK, 0);
2980 input_report_key(hidpp->input, BTN_FORWARD, 0);
2981 input_report_key(hidpp->input, BTN_MIDDLE, 0);
2982 break;
2983 default:
2984 hid_err(hdev, "error in report\n");
2985 return 0;
2986 }
2987 input_sync(hidpp->input);
2988
2989 } else if (data[0] == 0x02) {
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999 int v;
3000
3001 input_report_key(hidpp->input, BTN_LEFT,
3002 !!(data[1] & M560_MOUSE_BTN_LEFT));
3003 input_report_key(hidpp->input, BTN_RIGHT,
3004 !!(data[1] & M560_MOUSE_BTN_RIGHT));
3005
3006 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3007 input_report_rel(hidpp->input, REL_HWHEEL, -1);
3008 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3009 -120);
3010 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3011 input_report_rel(hidpp->input, REL_HWHEEL, 1);
3012 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3013 120);
3014 }
3015
3016 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
3017 input_report_rel(hidpp->input, REL_X, v);
3018
3019 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
3020 input_report_rel(hidpp->input, REL_Y, v);
3021
3022 v = hid_snto32(data[6], 8);
3023 if (v != 0)
3024 hidpp_scroll_counter_handle_scroll(hidpp->input,
3025 &hidpp->vertical_wheel_counter, v);
3026
3027 input_sync(hidpp->input);
3028 }
3029
3030 return 1;
3031 }
3032
3033 static void m560_populate_input(struct hidpp_device *hidpp,
3034 struct input_dev *input_dev)
3035 {
3036 __set_bit(EV_KEY, input_dev->evbit);
3037 __set_bit(BTN_MIDDLE, input_dev->keybit);
3038 __set_bit(BTN_RIGHT, input_dev->keybit);
3039 __set_bit(BTN_LEFT, input_dev->keybit);
3040 __set_bit(BTN_BACK, input_dev->keybit);
3041 __set_bit(BTN_FORWARD, input_dev->keybit);
3042
3043 __set_bit(EV_REL, input_dev->evbit);
3044 __set_bit(REL_X, input_dev->relbit);
3045 __set_bit(REL_Y, input_dev->relbit);
3046 __set_bit(REL_WHEEL, input_dev->relbit);
3047 __set_bit(REL_HWHEEL, input_dev->relbit);
3048 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3049 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3050 }
3051
3052 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3053 struct hid_field *field, struct hid_usage *usage,
3054 unsigned long **bit, int *max)
3055 {
3056 return -1;
3057 }
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073 struct k400_private_data {
3074 u8 feature_index;
3075 };
3076
3077 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3078 {
3079 struct k400_private_data *k400 = hidpp->private_data;
3080 struct hidpp_touchpad_fw_items items = {};
3081 int ret;
3082 u8 feature_type;
3083
3084 if (!k400->feature_index) {
3085 ret = hidpp_root_get_feature(hidpp,
3086 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3087 &k400->feature_index, &feature_type);
3088 if (ret)
3089
3090 return ret;
3091 }
3092
3093 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3094 if (ret)
3095 return ret;
3096
3097 return 0;
3098 }
3099
3100 static int k400_allocate(struct hid_device *hdev)
3101 {
3102 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3103 struct k400_private_data *k400;
3104
3105 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3106 GFP_KERNEL);
3107 if (!k400)
3108 return -ENOMEM;
3109
3110 hidpp->private_data = k400;
3111
3112 return 0;
3113 };
3114
3115 static int k400_connect(struct hid_device *hdev, bool connected)
3116 {
3117 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3118
3119 if (!disable_tap_to_click)
3120 return 0;
3121
3122 return k400_disable_tap_to_click(hidpp);
3123 }
3124
3125
3126
3127
3128
3129 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3130
3131 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3132 struct hidpp_ff_private_data *data)
3133 {
3134 struct hidpp_report response;
3135 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3136 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3137 };
3138 int ret;
3139
3140
3141
3142 dbg_hid("Setting autocenter to 0.\n");
3143 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3144 HIDPP_FF_DOWNLOAD_EFFECT,
3145 params, ARRAY_SIZE(params),
3146 &response);
3147 if (ret)
3148 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3149 else
3150 data->slot_autocenter = response.fap.params[0];
3151
3152 return ret;
3153 }
3154
3155 static int g920_get_config(struct hidpp_device *hidpp,
3156 struct hidpp_ff_private_data *data)
3157 {
3158 struct hidpp_report response;
3159 u8 feature_type;
3160 int ret;
3161
3162 memset(data, 0, sizeof(*data));
3163
3164
3165 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3166 &data->feature_index, &feature_type);
3167 if (ret)
3168 return ret;
3169
3170
3171 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3172 HIDPP_FF_GET_INFO,
3173 NULL, 0,
3174 &response);
3175 if (ret) {
3176 if (ret < 0)
3177 return ret;
3178 hid_err(hidpp->hid_dev,
3179 "%s: received protocol error 0x%02x\n", __func__, ret);
3180 return -EPROTO;
3181 }
3182
3183 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3184
3185
3186 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3187 HIDPP_FF_RESET_ALL,
3188 NULL, 0,
3189 &response);
3190 if (ret)
3191 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3192
3193 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3194 HIDPP_FF_GET_APERTURE,
3195 NULL, 0,
3196 &response);
3197 if (ret) {
3198 hid_warn(hidpp->hid_dev,
3199 "Failed to read range from device!\n");
3200 }
3201 data->range = ret ?
3202 900 : get_unaligned_be16(&response.fap.params[0]);
3203
3204
3205 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3206 HIDPP_FF_GET_GLOBAL_GAINS,
3207 NULL, 0,
3208 &response);
3209 if (ret)
3210 hid_warn(hidpp->hid_dev,
3211 "Failed to read gain values from device!\n");
3212 data->gain = ret ?
3213 0xffff : get_unaligned_be16(&response.fap.params[0]);
3214
3215
3216
3217 return g920_ff_set_autocenter(hidpp, data);
3218 }
3219
3220
3221
3222
3223 #define DINOVO_MINI_PRODUCT_ID 0xb30c
3224
3225 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3226 struct hid_field *field, struct hid_usage *usage,
3227 unsigned long **bit, int *max)
3228 {
3229 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3230 return 0;
3231
3232 switch (usage->hid & HID_USAGE) {
3233 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3234 default:
3235 return 0;
3236 }
3237 return 1;
3238 }
3239
3240
3241
3242
3243 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3244 {
3245 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3246 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3247 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3248 }
3249
3250 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3251 u8 *data, int size)
3252 {
3253 s8 value, hvalue;
3254
3255 if (!hidpp->input)
3256 return -EINVAL;
3257
3258 if (size < 7)
3259 return 0;
3260
3261 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3262 return 0;
3263
3264 value = data[3];
3265 hvalue = data[4];
3266
3267 input_report_rel(hidpp->input, REL_WHEEL, value);
3268 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3269 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3270 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3271 input_sync(hidpp->input);
3272
3273 return 1;
3274 }
3275
3276 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3277 struct input_dev *input_dev)
3278 {
3279 __set_bit(EV_REL, input_dev->evbit);
3280 __set_bit(REL_WHEEL, input_dev->relbit);
3281 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3282 __set_bit(REL_HWHEEL, input_dev->relbit);
3283 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3284 }
3285
3286
3287
3288
3289 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3290 {
3291 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3292 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3293 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3294 }
3295
3296 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3297 u8 *data, int size)
3298 {
3299 int i;
3300
3301 if (!hidpp->input)
3302 return -EINVAL;
3303
3304 if (size < 7)
3305 return 0;
3306
3307 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3308 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3309 return 0;
3310
3311
3312
3313
3314
3315
3316
3317 for (i = 0; i < 8; i++)
3318 input_report_key(hidpp->input, BTN_MOUSE + i,
3319 (data[3] & (1 << i)));
3320
3321
3322 for (i = 0; i < 8; i++)
3323 input_report_key(hidpp->input, BTN_MISC + i,
3324 (data[4] & (1 << i)));
3325
3326 input_sync(hidpp->input);
3327 return 1;
3328 }
3329
3330 static void hidpp10_extra_mouse_buttons_populate_input(
3331 struct hidpp_device *hidpp, struct input_dev *input_dev)
3332 {
3333
3334 __set_bit(BTN_0, input_dev->keybit);
3335 __set_bit(BTN_1, input_dev->keybit);
3336 __set_bit(BTN_2, input_dev->keybit);
3337 __set_bit(BTN_3, input_dev->keybit);
3338 __set_bit(BTN_4, input_dev->keybit);
3339 __set_bit(BTN_5, input_dev->keybit);
3340 __set_bit(BTN_6, input_dev->keybit);
3341 __set_bit(BTN_7, input_dev->keybit);
3342 }
3343
3344
3345
3346
3347
3348
3349 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3350 u8 *_rdesc, unsigned int *rsize)
3351 {
3352
3353 static const char consumer_rdesc_start[] = {
3354 0x05, 0x0C,
3355 0x09, 0x01,
3356 0xA1, 0x01,
3357 0x85, 0x03,
3358 0x75, 0x10,
3359 0x95, 0x02,
3360 0x15, 0x01,
3361 0x26, 0x00
3362 };
3363 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3364 unsigned int size;
3365
3366 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3367 size = *rsize - (consumer_rdesc - rdesc);
3368 if (consumer_rdesc && size >= 25) {
3369 consumer_rdesc[15] = 0x7f;
3370 consumer_rdesc[16] = 0x10;
3371 consumer_rdesc[20] = 0x7f;
3372 consumer_rdesc[21] = 0x10;
3373 }
3374 return _rdesc;
3375 }
3376
3377 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3378 {
3379 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3380 HIDPP_ENABLE_CONSUMER_REPORT,
3381 HIDPP_ENABLE_CONSUMER_REPORT);
3382 }
3383
3384 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3385 u8 *data, int size)
3386 {
3387 u8 consumer_report[5];
3388
3389 if (size < 7)
3390 return 0;
3391
3392 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3393 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3394 return 0;
3395
3396
3397
3398
3399
3400 consumer_report[0] = 0x03;
3401 memcpy(&consumer_report[1], &data[3], 4);
3402
3403 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3404 consumer_report, 5, 1);
3405
3406 return 1;
3407 }
3408
3409
3410
3411
3412
3413 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3414 {
3415 int ret;
3416 u8 multiplier = 1;
3417
3418 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
3419 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3420 if (ret == 0)
3421 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3422 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
3423 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3424 &multiplier);
3425 } else {
3426 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3427 multiplier = 8;
3428 }
3429 if (ret)
3430 return ret;
3431
3432 if (multiplier == 0)
3433 multiplier = 1;
3434
3435 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3436 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3437 return 0;
3438 }
3439
3440
3441
3442
3443
3444 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3445 unsigned int *rsize)
3446 {
3447 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3448
3449 if (!hidpp)
3450 return rdesc;
3451
3452
3453 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3454 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3455 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3456
3457 return rdesc;
3458 }
3459
3460 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3461 struct hid_field *field, struct hid_usage *usage,
3462 unsigned long **bit, int *max)
3463 {
3464 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3465
3466 if (!hidpp)
3467 return 0;
3468
3469 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3470 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3471 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3472 field->application != HID_GD_MOUSE)
3473 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3474
3475 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3476 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3477
3478 return 0;
3479 }
3480
3481 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3482 struct hid_field *field, struct hid_usage *usage,
3483 unsigned long **bit, int *max)
3484 {
3485 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3486
3487 if (!hidpp)
3488 return 0;
3489
3490
3491 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3492 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3493 usage->code == ABS_Y || usage->code == ABS_Z ||
3494 usage->code == ABS_RZ)) {
3495 field->application = HID_GD_MULTIAXIS;
3496 }
3497 }
3498
3499 return 0;
3500 }
3501
3502
3503 static void hidpp_populate_input(struct hidpp_device *hidpp,
3504 struct input_dev *input)
3505 {
3506 hidpp->input = input;
3507
3508 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3509 wtp_populate_input(hidpp, input);
3510 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3511 m560_populate_input(hidpp, input);
3512
3513 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3514 hidpp10_wheel_populate_input(hidpp, input);
3515
3516 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3517 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3518 }
3519
3520 static int hidpp_input_configured(struct hid_device *hdev,
3521 struct hid_input *hidinput)
3522 {
3523 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3524 struct input_dev *input = hidinput->input;
3525
3526 if (!hidpp)
3527 return 0;
3528
3529 hidpp_populate_input(hidpp, input);
3530
3531 return 0;
3532 }
3533
3534 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3535 int size)
3536 {
3537 struct hidpp_report *question = hidpp->send_receive_buf;
3538 struct hidpp_report *answer = hidpp->send_receive_buf;
3539 struct hidpp_report *report = (struct hidpp_report *)data;
3540 int ret;
3541
3542
3543
3544
3545
3546 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3547
3548
3549
3550
3551 if (hidpp_match_answer(question, report) ||
3552 hidpp_match_error(question, report)) {
3553 *answer = *report;
3554 hidpp->answer_available = true;
3555 wake_up(&hidpp->wait);
3556
3557
3558
3559
3560
3561
3562 return 1;
3563 }
3564 }
3565
3566 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3567 atomic_set(&hidpp->connected,
3568 !(report->rap.params[0] & (1 << 6)));
3569 if (schedule_work(&hidpp->work) == 0)
3570 dbg_hid("%s: connect event already queued\n", __func__);
3571 return 1;
3572 }
3573
3574 if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3575 data[0] == REPORT_ID_HIDPP_SHORT &&
3576 data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3577 (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3578 dev_err_ratelimited(&hidpp->hid_dev->dev,
3579 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3580 dev_err_ratelimited(&hidpp->hid_dev->dev,
3581 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3582 }
3583
3584 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3585 ret = hidpp20_battery_event_1000(hidpp, data, size);
3586 if (ret != 0)
3587 return ret;
3588 ret = hidpp20_battery_event_1004(hidpp, data, size);
3589 if (ret != 0)
3590 return ret;
3591 ret = hidpp_solar_battery_event(hidpp, data, size);
3592 if (ret != 0)
3593 return ret;
3594 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3595 if (ret != 0)
3596 return ret;
3597 }
3598
3599 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3600 ret = hidpp10_battery_event(hidpp, data, size);
3601 if (ret != 0)
3602 return ret;
3603 }
3604
3605 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3606 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3607 if (ret != 0)
3608 return ret;
3609 }
3610
3611 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3612 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3613 if (ret != 0)
3614 return ret;
3615 }
3616
3617 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3618 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3619 if (ret != 0)
3620 return ret;
3621 }
3622
3623 return 0;
3624 }
3625
3626 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3627 u8 *data, int size)
3628 {
3629 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3630 int ret = 0;
3631
3632 if (!hidpp)
3633 return 0;
3634
3635
3636 switch (data[0]) {
3637 case REPORT_ID_HIDPP_VERY_LONG:
3638 if (size != hidpp->very_long_report_length) {
3639 hid_err(hdev, "received hid++ report of bad size (%d)",
3640 size);
3641 return 1;
3642 }
3643 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3644 break;
3645 case REPORT_ID_HIDPP_LONG:
3646 if (size != HIDPP_REPORT_LONG_LENGTH) {
3647 hid_err(hdev, "received hid++ report of bad size (%d)",
3648 size);
3649 return 1;
3650 }
3651 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3652 break;
3653 case REPORT_ID_HIDPP_SHORT:
3654 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3655 hid_err(hdev, "received hid++ report of bad size (%d)",
3656 size);
3657 return 1;
3658 }
3659 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3660 break;
3661 }
3662
3663
3664
3665 if (ret != 0)
3666 return ret;
3667
3668 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3669 return wtp_raw_event(hdev, data, size);
3670 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3671 return m560_raw_event(hdev, data, size);
3672
3673 return 0;
3674 }
3675
3676 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3677 struct hid_usage *usage, __s32 value)
3678 {
3679
3680
3681
3682 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3683 struct hidpp_scroll_counter *counter;
3684
3685 if (!hidpp)
3686 return 0;
3687
3688 counter = &hidpp->vertical_wheel_counter;
3689
3690
3691
3692
3693
3694 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
3695 || hidpp->input == NULL || counter->wheel_multiplier == 0)
3696 return 0;
3697
3698 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3699 return 1;
3700 }
3701
3702 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3703 {
3704 static atomic_t battery_no = ATOMIC_INIT(0);
3705 struct power_supply_config cfg = { .drv_data = hidpp };
3706 struct power_supply_desc *desc = &hidpp->battery.desc;
3707 enum power_supply_property *battery_props;
3708 struct hidpp_battery *battery;
3709 unsigned int num_battery_props;
3710 unsigned long n;
3711 int ret;
3712
3713 if (hidpp->battery.ps)
3714 return 0;
3715
3716 hidpp->battery.feature_index = 0xff;
3717 hidpp->battery.solar_feature_index = 0xff;
3718 hidpp->battery.voltage_feature_index = 0xff;
3719
3720 if (hidpp->protocol_major >= 2) {
3721 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3722 ret = hidpp_solar_request_battery_event(hidpp);
3723 else {
3724
3725
3726
3727 ret = hidpp20_query_battery_info_1000(hidpp);
3728 if (ret)
3729 ret = hidpp20_query_battery_info_1004(hidpp);
3730 if (ret)
3731 ret = hidpp20_query_battery_voltage_info(hidpp);
3732 }
3733
3734 if (ret)
3735 return ret;
3736 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3737 } else {
3738 ret = hidpp10_query_battery_status(hidpp);
3739 if (ret) {
3740 ret = hidpp10_query_battery_mileage(hidpp);
3741 if (ret)
3742 return -ENOENT;
3743 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3744 } else {
3745 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3746 }
3747 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3748 }
3749
3750 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3751 hidpp_battery_props,
3752 sizeof(hidpp_battery_props),
3753 GFP_KERNEL);
3754 if (!battery_props)
3755 return -ENOMEM;
3756
3757 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3758
3759 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
3760 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
3761 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3762 battery_props[num_battery_props++] =
3763 POWER_SUPPLY_PROP_CAPACITY;
3764
3765 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3766 battery_props[num_battery_props++] =
3767 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3768
3769 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3770 battery_props[num_battery_props++] =
3771 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3772
3773 battery = &hidpp->battery;
3774
3775 n = atomic_inc_return(&battery_no) - 1;
3776 desc->properties = battery_props;
3777 desc->num_properties = num_battery_props;
3778 desc->get_property = hidpp_battery_get_property;
3779 sprintf(battery->name, "hidpp_battery_%ld", n);
3780 desc->name = battery->name;
3781 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3782 desc->use_for_apm = 0;
3783
3784 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3785 &battery->desc,
3786 &cfg);
3787 if (IS_ERR(battery->ps))
3788 return PTR_ERR(battery->ps);
3789
3790 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3791
3792 return ret;
3793 }
3794
3795 static void hidpp_overwrite_name(struct hid_device *hdev)
3796 {
3797 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3798 char *name;
3799
3800 if (hidpp->protocol_major < 2)
3801 return;
3802
3803 name = hidpp_get_device_name(hidpp);
3804
3805 if (!name) {
3806 hid_err(hdev, "unable to retrieve the name of the device");
3807 } else {
3808 dbg_hid("HID++: Got name: %s\n", name);
3809 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3810 }
3811
3812 kfree(name);
3813 }
3814
3815 static int hidpp_input_open(struct input_dev *dev)
3816 {
3817 struct hid_device *hid = input_get_drvdata(dev);
3818
3819 return hid_hw_open(hid);
3820 }
3821
3822 static void hidpp_input_close(struct input_dev *dev)
3823 {
3824 struct hid_device *hid = input_get_drvdata(dev);
3825
3826 hid_hw_close(hid);
3827 }
3828
3829 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3830 {
3831 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3832 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3833
3834 if (!input_dev)
3835 return NULL;
3836
3837 input_set_drvdata(input_dev, hdev);
3838 input_dev->open = hidpp_input_open;
3839 input_dev->close = hidpp_input_close;
3840
3841 input_dev->name = hidpp->name;
3842 input_dev->phys = hdev->phys;
3843 input_dev->uniq = hdev->uniq;
3844 input_dev->id.bustype = hdev->bus;
3845 input_dev->id.vendor = hdev->vendor;
3846 input_dev->id.product = hdev->product;
3847 input_dev->id.version = hdev->version;
3848 input_dev->dev.parent = &hdev->dev;
3849
3850 return input_dev;
3851 }
3852
3853 static void hidpp_connect_event(struct hidpp_device *hidpp)
3854 {
3855 struct hid_device *hdev = hidpp->hid_dev;
3856 int ret = 0;
3857 bool connected = atomic_read(&hidpp->connected);
3858 struct input_dev *input;
3859 char *name, *devm_name;
3860
3861 if (!connected) {
3862 if (hidpp->battery.ps) {
3863 hidpp->battery.online = false;
3864 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3865 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3866 power_supply_changed(hidpp->battery.ps);
3867 }
3868 return;
3869 }
3870
3871 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3872 ret = wtp_connect(hdev, connected);
3873 if (ret)
3874 return;
3875 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3876 ret = m560_send_config_command(hdev, connected);
3877 if (ret)
3878 return;
3879 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3880 ret = k400_connect(hdev, connected);
3881 if (ret)
3882 return;
3883 }
3884
3885 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3886 ret = hidpp10_wheel_connect(hidpp);
3887 if (ret)
3888 return;
3889 }
3890
3891 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3892 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3893 if (ret)
3894 return;
3895 }
3896
3897 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3898 ret = hidpp10_consumer_keys_connect(hidpp);
3899 if (ret)
3900 return;
3901 }
3902
3903
3904
3905 if (!hidpp->protocol_major) {
3906 ret = hidpp_root_get_protocol_version(hidpp);
3907 if (ret) {
3908 hid_err(hdev, "Can not get the protocol version.\n");
3909 return;
3910 }
3911 }
3912
3913 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3914 name = hidpp_get_device_name(hidpp);
3915 if (name) {
3916 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3917 "%s", name);
3918 kfree(name);
3919 if (!devm_name)
3920 return;
3921
3922 hidpp->name = devm_name;
3923 }
3924 }
3925
3926 hidpp_initialize_battery(hidpp);
3927
3928
3929 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3930 hidpp10_enable_battery_reporting(hidpp);
3931 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3932 hidpp10_query_battery_mileage(hidpp);
3933 else
3934 hidpp10_query_battery_status(hidpp);
3935 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3936 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3937 hidpp20_query_battery_voltage_info(hidpp);
3938 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
3939 hidpp20_query_battery_info_1004(hidpp);
3940 else
3941 hidpp20_query_battery_info_1000(hidpp);
3942 }
3943 if (hidpp->battery.ps)
3944 power_supply_changed(hidpp->battery.ps);
3945
3946 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3947 hi_res_scroll_enable(hidpp);
3948
3949 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3950
3951 return;
3952
3953 input = hidpp_allocate_input(hdev);
3954 if (!input) {
3955 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3956 return;
3957 }
3958
3959 hidpp_populate_input(hidpp, input);
3960
3961 ret = input_register_device(input);
3962 if (ret)
3963 input_free_device(input);
3964
3965 hidpp->delayed_input = input;
3966 }
3967
3968 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3969
3970 static struct attribute *sysfs_attrs[] = {
3971 &dev_attr_builtin_power_supply.attr,
3972 NULL
3973 };
3974
3975 static const struct attribute_group ps_attribute_group = {
3976 .attrs = sysfs_attrs
3977 };
3978
3979 static int hidpp_get_report_length(struct hid_device *hdev, int id)
3980 {
3981 struct hid_report_enum *re;
3982 struct hid_report *report;
3983
3984 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3985 report = re->report_id_hash[id];
3986 if (!report)
3987 return 0;
3988
3989 return report->field[0]->report_count + 1;
3990 }
3991
3992 static u8 hidpp_validate_device(struct hid_device *hdev)
3993 {
3994 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3995 int id, report_length;
3996 u8 supported_reports = 0;
3997
3998 id = REPORT_ID_HIDPP_SHORT;
3999 report_length = hidpp_get_report_length(hdev, id);
4000 if (report_length) {
4001 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4002 goto bad_device;
4003
4004 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4005 }
4006
4007 id = REPORT_ID_HIDPP_LONG;
4008 report_length = hidpp_get_report_length(hdev, id);
4009 if (report_length) {
4010 if (report_length < HIDPP_REPORT_LONG_LENGTH)
4011 goto bad_device;
4012
4013 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4014 }
4015
4016 id = REPORT_ID_HIDPP_VERY_LONG;
4017 report_length = hidpp_get_report_length(hdev, id);
4018 if (report_length) {
4019 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4020 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4021 goto bad_device;
4022
4023 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4024 hidpp->very_long_report_length = report_length;
4025 }
4026
4027 return supported_reports;
4028
4029 bad_device:
4030 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4031 return false;
4032 }
4033
4034 static bool hidpp_application_equals(struct hid_device *hdev,
4035 unsigned int application)
4036 {
4037 struct list_head *report_list;
4038 struct hid_report *report;
4039
4040 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4041 report = list_first_entry_or_null(report_list, struct hid_report, list);
4042 return report && report->application == application;
4043 }
4044
4045 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4046 {
4047 struct hidpp_device *hidpp;
4048 int ret;
4049 bool connected;
4050 unsigned int connect_mask = HID_CONNECT_DEFAULT;
4051 struct hidpp_ff_private_data data;
4052
4053
4054 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4055 if (!hidpp)
4056 return -ENOMEM;
4057
4058 hidpp->hid_dev = hdev;
4059 hidpp->name = hdev->name;
4060 hidpp->quirks = id->driver_data;
4061 hid_set_drvdata(hdev, hidpp);
4062
4063 ret = hid_parse(hdev);
4064 if (ret) {
4065 hid_err(hdev, "%s:parse failed\n", __func__);
4066 return ret;
4067 }
4068
4069
4070
4071
4072 hidpp->supported_reports = hidpp_validate_device(hdev);
4073
4074 if (!hidpp->supported_reports) {
4075 hid_set_drvdata(hdev, NULL);
4076 devm_kfree(&hdev->dev, hidpp);
4077 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4078 }
4079
4080 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4081 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
4082
4083 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4084 hidpp_application_equals(hdev, HID_GD_MOUSE))
4085 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4086 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4087
4088 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4089 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4090 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4091
4092 if (disable_raw_mode) {
4093 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
4094 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
4095 }
4096
4097 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4098 ret = wtp_allocate(hdev, id);
4099 if (ret)
4100 return ret;
4101 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4102 ret = k400_allocate(hdev);
4103 if (ret)
4104 return ret;
4105 }
4106
4107 INIT_WORK(&hidpp->work, delayed_work_cb);
4108 mutex_init(&hidpp->send_mutex);
4109 init_waitqueue_head(&hidpp->wait);
4110
4111
4112 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4113 if (ret)
4114 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4115 hdev->name);
4116
4117
4118
4119
4120
4121 ret = hid_hw_start(hdev, 0);
4122 if (ret) {
4123 hid_err(hdev, "hw start failed\n");
4124 goto hid_hw_start_fail;
4125 }
4126
4127 ret = hid_hw_open(hdev);
4128 if (ret < 0) {
4129 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4130 __func__, ret);
4131 goto hid_hw_open_fail;
4132 }
4133
4134
4135 hid_device_io_start(hdev);
4136
4137 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
4138 hidpp_unifying_init(hidpp);
4139
4140 connected = hidpp_root_get_protocol_version(hidpp) == 0;
4141 atomic_set(&hidpp->connected, connected);
4142 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
4143 if (!connected) {
4144 ret = -ENODEV;
4145 hid_err(hdev, "Device not connected");
4146 goto hid_hw_init_fail;
4147 }
4148
4149 hidpp_overwrite_name(hdev);
4150 }
4151
4152 if (connected && hidpp->protocol_major >= 2) {
4153 ret = hidpp_set_wireless_feature_index(hidpp);
4154 if (ret == -ENOENT)
4155 hidpp->wireless_feature_index = 0;
4156 else if (ret)
4157 goto hid_hw_init_fail;
4158 }
4159
4160 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
4161 ret = wtp_get_config(hidpp);
4162 if (ret)
4163 goto hid_hw_init_fail;
4164 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
4165 ret = g920_get_config(hidpp, &data);
4166 if (ret)
4167 goto hid_hw_init_fail;
4168 }
4169
4170 hidpp_connect_event(hidpp);
4171
4172
4173 hid_device_io_stop(hdev);
4174 hid_hw_close(hdev);
4175 hid_hw_stop(hdev);
4176
4177 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
4178 connect_mask &= ~HID_CONNECT_HIDINPUT;
4179
4180
4181 ret = hid_hw_start(hdev, connect_mask);
4182 if (ret) {
4183 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
4184 goto hid_hw_start_fail;
4185 }
4186
4187 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4188 ret = hidpp_ff_init(hidpp, &data);
4189 if (ret)
4190 hid_warn(hidpp->hid_dev,
4191 "Unable to initialize force feedback support, errno %d\n",
4192 ret);
4193 }
4194
4195 return ret;
4196
4197 hid_hw_init_fail:
4198 hid_hw_close(hdev);
4199 hid_hw_open_fail:
4200 hid_hw_stop(hdev);
4201 hid_hw_start_fail:
4202 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4203 cancel_work_sync(&hidpp->work);
4204 mutex_destroy(&hidpp->send_mutex);
4205 return ret;
4206 }
4207
4208 static void hidpp_remove(struct hid_device *hdev)
4209 {
4210 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4211
4212 if (!hidpp)
4213 return hid_hw_stop(hdev);
4214
4215 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4216
4217 hid_hw_stop(hdev);
4218 cancel_work_sync(&hidpp->work);
4219 mutex_destroy(&hidpp->send_mutex);
4220 }
4221
4222 #define LDJ_DEVICE(product) \
4223 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4224 USB_VENDOR_ID_LOGITECH, (product))
4225
4226 #define L27MHZ_DEVICE(product) \
4227 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4228 USB_VENDOR_ID_LOGITECH, (product))
4229
4230 static const struct hid_device_id hidpp_devices[] = {
4231 {
4232 LDJ_DEVICE(0x4011),
4233 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4234 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4235 {
4236 LDJ_DEVICE(0x4101),
4237 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4238 {
4239 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4240 USB_DEVICE_ID_LOGITECH_T651),
4241 .driver_data = HIDPP_QUIRK_CLASS_WTP },
4242 {
4243 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4244 {
4245 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4246 {
4247 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4248 {
4249 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4250 {
4251 LDJ_DEVICE(0x402d),
4252 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
4253 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4254 {
4255 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4256 {
4257 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4258 {
4259 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4260 {
4261 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4262 { LDJ_DEVICE(0x4072), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4263 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4264 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4265 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4266 {
4267 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4268 {
4269 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4270 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4271 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4272 {
4273 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4274 {
4275 LDJ_DEVICE(0x4082), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4276 {
4277 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4278 {
4279 LDJ_DEVICE(0x4024),
4280 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4281 {
4282 LDJ_DEVICE(0x4002),
4283 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4284 {
4285 LDJ_DEVICE(0xb305),
4286 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4287 {
4288 LDJ_DEVICE(0xb309),
4289 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4290 {
4291 LDJ_DEVICE(0xb30b),
4292 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4293
4294 { LDJ_DEVICE(HID_ANY_ID) },
4295
4296 {
4297 L27MHZ_DEVICE(0x0049),
4298 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4299 {
4300 L27MHZ_DEVICE(0x0057),
4301 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4302 {
4303 L27MHZ_DEVICE(0x005c),
4304 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4305 {
4306 L27MHZ_DEVICE(0x00fe),
4307 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4308
4309 { L27MHZ_DEVICE(HID_ANY_ID) },
4310
4311 {
4312 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4313 {
4314 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4315 {
4316 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4317 {
4318 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4319 {
4320 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4321 {
4322 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4323 {
4324 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4325 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4326 {
4327 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4328
4329 {
4330 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4331 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4332 {
4333 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4334 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4335 {
4336 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4337 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4338 {
4339 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4340 {
4341 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012),
4342 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4343 {
4344 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4345 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e),
4346 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4347 {
4348 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023),
4349 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4350 {}
4351 };
4352
4353 MODULE_DEVICE_TABLE(hid, hidpp_devices);
4354
4355 static const struct hid_usage_id hidpp_usages[] = {
4356 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4357 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4358 };
4359
4360 static struct hid_driver hidpp_driver = {
4361 .name = "logitech-hidpp-device",
4362 .id_table = hidpp_devices,
4363 .report_fixup = hidpp_report_fixup,
4364 .probe = hidpp_probe,
4365 .remove = hidpp_remove,
4366 .raw_event = hidpp_raw_event,
4367 .usage_table = hidpp_usages,
4368 .event = hidpp_event,
4369 .input_configured = hidpp_input_configured,
4370 .input_mapping = hidpp_input_mapping,
4371 .input_mapped = hidpp_input_mapped,
4372 };
4373
4374 module_hid_driver(hidpp_driver);