Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 1 | /* |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 2 | * HID driver for Lenovo: |
| 3 | * - ThinkPad USB Keyboard with TrackPoint (tpkbd) |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 4 | * - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd) |
| 5 | * - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd) |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 6 | * |
| 7 | * Copyright (c) 2012 Bernhard Seibold |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 8 | * Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk> |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the Free |
| 14 | * Software Foundation; either version 2 of the License, or (at your option) |
| 15 | * any later version. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/sysfs.h> |
| 20 | #include <linux/device.h> |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 21 | #include <linux/hid.h> |
| 22 | #include <linux/input.h> |
| 23 | #include <linux/leds.h> |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 24 | |
| 25 | #include "hid-ids.h" |
| 26 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 27 | struct lenovo_drvdata_tpkbd { |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 28 | int led_state; |
| 29 | struct led_classdev led_mute; |
| 30 | struct led_classdev led_micmute; |
| 31 | int press_to_select; |
| 32 | int dragging; |
| 33 | int release_to_select; |
| 34 | int select_right; |
| 35 | int sensitivity; |
| 36 | int press_speed; |
| 37 | }; |
| 38 | |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 39 | struct lenovo_drvdata_cptkbd { |
| 40 | bool fn_lock; |
Jamie Lentin | e3cb0ac | 2014-12-16 21:26:45 +0000 | [diff] [blame] | 41 | int sensitivity; |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 44 | #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c)) |
| 45 | |
Benjamin Tissoires | 181a8b9 | 2015-05-01 16:22:45 -0400 | [diff] [blame] | 46 | static const __u8 lenovo_pro_dock_need_fixup_collection[] = { |
| 47 | 0x05, 0x88, /* Usage Page (Vendor Usage Page 0x88) */ |
| 48 | 0x09, 0x01, /* Usage (Vendor Usage 0x01) */ |
| 49 | 0xa1, 0x01, /* Collection (Application) */ |
| 50 | 0x85, 0x04, /* Report ID (4) */ |
| 51 | 0x19, 0x00, /* Usage Minimum (0) */ |
| 52 | 0x2a, 0xff, 0xff, /* Usage Maximum (65535) */ |
| 53 | }; |
| 54 | |
| 55 | static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
| 56 | unsigned int *rsize) |
| 57 | { |
| 58 | switch (hdev->product) { |
| 59 | case USB_DEVICE_ID_LENOVO_TPPRODOCK: |
| 60 | /* the fixups that need to be done: |
| 61 | * - get a reasonable usage max for the vendor collection |
| 62 | * 0x8801 from the report ID 4 |
| 63 | */ |
| 64 | if (*rsize >= 153 && |
| 65 | memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection, |
| 66 | sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) { |
| 67 | rdesc[151] = 0x01; |
| 68 | rdesc[152] = 0x00; |
| 69 | } |
| 70 | break; |
| 71 | } |
| 72 | return rdesc; |
| 73 | } |
| 74 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 75 | static int lenovo_input_mapping_tpkbd(struct hid_device *hdev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 76 | struct hid_input *hi, struct hid_field *field, |
| 77 | struct hid_usage *usage, unsigned long **bit, int *max) |
| 78 | { |
Benjamin Tissoires | 0c52183 | 2013-09-11 22:12:24 +0200 | [diff] [blame] | 79 | if (usage->hid == (HID_UP_BUTTON | 0x0010)) { |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 80 | /* This sub-device contains trackpoint, mark it */ |
Benjamin Tissoires | 0c52183 | 2013-09-11 22:12:24 +0200 | [diff] [blame] | 81 | hid_set_drvdata(hdev, (void *)1); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 82 | map_key_clear(KEY_MICMUTE); |
| 83 | return 1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 88 | static int lenovo_input_mapping_cptkbd(struct hid_device *hdev, |
| 89 | struct hid_input *hi, struct hid_field *field, |
| 90 | struct hid_usage *usage, unsigned long **bit, int *max) |
| 91 | { |
| 92 | /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */ |
| 93 | if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR || |
| 94 | (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) { |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 95 | switch (usage->hid & HID_USAGE) { |
| 96 | case 0x00f1: /* Fn-F4: Mic mute */ |
| 97 | map_key_clear(KEY_MICMUTE); |
| 98 | return 1; |
| 99 | case 0x00f2: /* Fn-F5: Brightness down */ |
| 100 | map_key_clear(KEY_BRIGHTNESSDOWN); |
| 101 | return 1; |
| 102 | case 0x00f3: /* Fn-F6: Brightness up */ |
| 103 | map_key_clear(KEY_BRIGHTNESSUP); |
| 104 | return 1; |
| 105 | case 0x00f4: /* Fn-F7: External display (projector) */ |
| 106 | map_key_clear(KEY_SWITCHVIDEOMODE); |
| 107 | return 1; |
| 108 | case 0x00f5: /* Fn-F8: Wireless */ |
| 109 | map_key_clear(KEY_WLAN); |
| 110 | return 1; |
| 111 | case 0x00f6: /* Fn-F9: Control panel */ |
| 112 | map_key_clear(KEY_CONFIG); |
| 113 | return 1; |
| 114 | case 0x00f8: /* Fn-F11: View open applications (3 boxes) */ |
| 115 | map_key_clear(KEY_SCALE); |
| 116 | return 1; |
Jamie Lentin | 5556eb1 | 2014-11-09 07:54:29 +0000 | [diff] [blame] | 117 | case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */ |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 118 | /* NB: This mapping is invented in raw_event below */ |
| 119 | map_key_clear(KEY_FILE); |
| 120 | return 1; |
Jamie Lentin | 5556eb1 | 2014-11-09 07:54:29 +0000 | [diff] [blame] | 121 | case 0x00fa: /* Fn-Esc: Fn-lock toggle */ |
| 122 | map_key_clear(KEY_FN_ESC); |
| 123 | return 1; |
Jamie Lentin | 94eefa2 | 2014-12-16 21:26:46 +0000 | [diff] [blame] | 124 | case 0x00fb: /* Middle mouse button (in native mode) */ |
| 125 | map_key_clear(BTN_MIDDLE); |
| 126 | return 1; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* Compatibility middle/wheel mappings should be ignored */ |
| 131 | if (usage->hid == HID_GD_WHEEL) |
| 132 | return -1; |
| 133 | if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON && |
| 134 | (usage->hid & HID_USAGE) == 0x003) |
| 135 | return -1; |
| 136 | if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER && |
| 137 | (usage->hid & HID_USAGE) == 0x238) |
| 138 | return -1; |
| 139 | |
| 140 | /* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */ |
| 141 | if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 || |
| 142 | (usage->hid & HID_USAGE_PAGE) == 0xffa10000) { |
| 143 | field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE; |
| 144 | field->logical_minimum = -127; |
| 145 | field->logical_maximum = 127; |
| 146 | |
| 147 | switch (usage->hid & HID_USAGE) { |
| 148 | case 0x0000: |
| 149 | hid_map_usage(hi, usage, bit, max, EV_REL, 0x06); |
| 150 | return 1; |
| 151 | case 0x0001: |
| 152 | hid_map_usage(hi, usage, bit, max, EV_REL, 0x08); |
| 153 | return 1; |
| 154 | default: |
| 155 | return -1; |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 162 | static int lenovo_input_mapping(struct hid_device *hdev, |
| 163 | struct hid_input *hi, struct hid_field *field, |
| 164 | struct hid_usage *usage, unsigned long **bit, int *max) |
| 165 | { |
| 166 | switch (hdev->product) { |
| 167 | case USB_DEVICE_ID_LENOVO_TPKBD: |
| 168 | return lenovo_input_mapping_tpkbd(hdev, hi, field, |
| 169 | usage, bit, max); |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 170 | case USB_DEVICE_ID_LENOVO_CUSBKBD: |
| 171 | case USB_DEVICE_ID_LENOVO_CBTKBD: |
| 172 | return lenovo_input_mapping_cptkbd(hdev, hi, field, |
| 173 | usage, bit, max); |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 174 | default: |
| 175 | return 0; |
| 176 | } |
| 177 | } |
| 178 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 179 | #undef map_key_clear |
| 180 | |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 181 | /* Send a config command to the keyboard */ |
| 182 | static int lenovo_send_cmd_cptkbd(struct hid_device *hdev, |
| 183 | unsigned char byte2, unsigned char byte3) |
| 184 | { |
| 185 | int ret; |
| 186 | unsigned char buf[] = {0x18, byte2, byte3}; |
| 187 | |
| 188 | switch (hdev->product) { |
| 189 | case USB_DEVICE_ID_LENOVO_CUSBKBD: |
| 190 | ret = hid_hw_raw_request(hdev, 0x13, buf, sizeof(buf), |
| 191 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT); |
| 192 | break; |
| 193 | case USB_DEVICE_ID_LENOVO_CBTKBD: |
| 194 | ret = hid_hw_output_report(hdev, buf, sizeof(buf)); |
| 195 | break; |
| 196 | default: |
| 197 | ret = -EINVAL; |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */ |
| 202 | } |
| 203 | |
| 204 | static void lenovo_features_set_cptkbd(struct hid_device *hdev) |
| 205 | { |
| 206 | int ret; |
| 207 | struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev); |
| 208 | |
| 209 | ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock); |
Jamie Lentin | e3cb0ac | 2014-12-16 21:26:45 +0000 | [diff] [blame] | 210 | ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity); |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 211 | if (ret) |
| 212 | hid_err(hdev, "Fn-lock setting failed: %d\n", ret); |
| 213 | } |
| 214 | |
| 215 | static ssize_t attr_fn_lock_show_cptkbd(struct device *dev, |
| 216 | struct device_attribute *attr, |
| 217 | char *buf) |
| 218 | { |
| 219 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
| 220 | struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev); |
| 221 | |
| 222 | return snprintf(buf, PAGE_SIZE, "%u\n", cptkbd_data->fn_lock); |
| 223 | } |
| 224 | |
| 225 | static ssize_t attr_fn_lock_store_cptkbd(struct device *dev, |
| 226 | struct device_attribute *attr, |
| 227 | const char *buf, |
| 228 | size_t count) |
| 229 | { |
| 230 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
| 231 | struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev); |
| 232 | int value; |
| 233 | |
| 234 | if (kstrtoint(buf, 10, &value)) |
| 235 | return -EINVAL; |
| 236 | if (value < 0 || value > 1) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | cptkbd_data->fn_lock = !!value; |
| 240 | lenovo_features_set_cptkbd(hdev); |
| 241 | |
| 242 | return count; |
| 243 | } |
| 244 | |
Jamie Lentin | e3cb0ac | 2014-12-16 21:26:45 +0000 | [diff] [blame] | 245 | static ssize_t attr_sensitivity_show_cptkbd(struct device *dev, |
| 246 | struct device_attribute *attr, |
| 247 | char *buf) |
| 248 | { |
| 249 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
| 250 | struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev); |
| 251 | |
| 252 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 253 | cptkbd_data->sensitivity); |
| 254 | } |
| 255 | |
| 256 | static ssize_t attr_sensitivity_store_cptkbd(struct device *dev, |
| 257 | struct device_attribute *attr, |
| 258 | const char *buf, |
| 259 | size_t count) |
| 260 | { |
| 261 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
| 262 | struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev); |
| 263 | int value; |
| 264 | |
| 265 | if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | cptkbd_data->sensitivity = value; |
| 269 | lenovo_features_set_cptkbd(hdev); |
| 270 | |
| 271 | return count; |
| 272 | } |
| 273 | |
| 274 | |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 275 | static struct device_attribute dev_attr_fn_lock_cptkbd = |
| 276 | __ATTR(fn_lock, S_IWUSR | S_IRUGO, |
| 277 | attr_fn_lock_show_cptkbd, |
| 278 | attr_fn_lock_store_cptkbd); |
| 279 | |
Jamie Lentin | e3cb0ac | 2014-12-16 21:26:45 +0000 | [diff] [blame] | 280 | static struct device_attribute dev_attr_sensitivity_cptkbd = |
| 281 | __ATTR(sensitivity, S_IWUSR | S_IRUGO, |
| 282 | attr_sensitivity_show_cptkbd, |
| 283 | attr_sensitivity_store_cptkbd); |
| 284 | |
| 285 | |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 286 | static struct attribute *lenovo_attributes_cptkbd[] = { |
| 287 | &dev_attr_fn_lock_cptkbd.attr, |
Jamie Lentin | e3cb0ac | 2014-12-16 21:26:45 +0000 | [diff] [blame] | 288 | &dev_attr_sensitivity_cptkbd.attr, |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 289 | NULL |
| 290 | }; |
| 291 | |
| 292 | static const struct attribute_group lenovo_attr_group_cptkbd = { |
| 293 | .attrs = lenovo_attributes_cptkbd, |
| 294 | }; |
| 295 | |
| 296 | static int lenovo_raw_event(struct hid_device *hdev, |
| 297 | struct hid_report *report, u8 *data, int size) |
| 298 | { |
| 299 | /* |
| 300 | * Compact USB keyboard's Fn-F12 report holds down many other keys, and |
| 301 | * its own key is outside the usage page range. Remove extra |
| 302 | * keypresses and remap to inside usage page. |
| 303 | */ |
| 304 | if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD |
| 305 | && size == 3 |
| 306 | && data[0] == 0x15 |
| 307 | && data[1] == 0x94 |
| 308 | && data[2] == 0x01)) { |
Jamie Lentin | 5556eb1 | 2014-11-09 07:54:29 +0000 | [diff] [blame] | 309 | data[1] = 0x00; |
| 310 | data[2] = 0x01; |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 316 | static int lenovo_features_set_tpkbd(struct hid_device *hdev) |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 317 | { |
| 318 | struct hid_report *report; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 319 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 320 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 321 | report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4]; |
| 322 | |
| 323 | report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02; |
| 324 | report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08; |
| 325 | report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20; |
| 326 | report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40; |
| 327 | report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver |
| 328 | report->field[2]->value[0] = data_pointer->sensitivity; |
| 329 | report->field[3]->value[0] = data_pointer->press_speed; |
| 330 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 331 | hid_hw_request(hdev, report, HID_REQ_SET_REPORT); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 335 | static ssize_t attr_press_to_select_show_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 336 | struct device_attribute *attr, |
| 337 | char *buf) |
| 338 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 339 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 340 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 341 | |
| 342 | return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select); |
| 343 | } |
| 344 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 345 | static ssize_t attr_press_to_select_store_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 346 | struct device_attribute *attr, |
| 347 | const char *buf, |
| 348 | size_t count) |
| 349 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 350 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 351 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 352 | int value; |
| 353 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 354 | if (kstrtoint(buf, 10, &value)) |
| 355 | return -EINVAL; |
| 356 | if (value < 0 || value > 1) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | data_pointer->press_to_select = value; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 360 | lenovo_features_set_tpkbd(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 361 | |
| 362 | return count; |
| 363 | } |
| 364 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 365 | static ssize_t attr_dragging_show_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 366 | struct device_attribute *attr, |
| 367 | char *buf) |
| 368 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 369 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 370 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 371 | |
| 372 | return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging); |
| 373 | } |
| 374 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 375 | static ssize_t attr_dragging_store_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 376 | struct device_attribute *attr, |
| 377 | const char *buf, |
| 378 | size_t count) |
| 379 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 380 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 381 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 382 | int value; |
| 383 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 384 | if (kstrtoint(buf, 10, &value)) |
| 385 | return -EINVAL; |
| 386 | if (value < 0 || value > 1) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | data_pointer->dragging = value; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 390 | lenovo_features_set_tpkbd(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 391 | |
| 392 | return count; |
| 393 | } |
| 394 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 395 | static ssize_t attr_release_to_select_show_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 396 | struct device_attribute *attr, |
| 397 | char *buf) |
| 398 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 399 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 400 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 401 | |
| 402 | return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select); |
| 403 | } |
| 404 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 405 | static ssize_t attr_release_to_select_store_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 406 | struct device_attribute *attr, |
| 407 | const char *buf, |
| 408 | size_t count) |
| 409 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 410 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 411 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 412 | int value; |
| 413 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 414 | if (kstrtoint(buf, 10, &value)) |
| 415 | return -EINVAL; |
| 416 | if (value < 0 || value > 1) |
| 417 | return -EINVAL; |
| 418 | |
| 419 | data_pointer->release_to_select = value; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 420 | lenovo_features_set_tpkbd(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 421 | |
| 422 | return count; |
| 423 | } |
| 424 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 425 | static ssize_t attr_select_right_show_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 426 | struct device_attribute *attr, |
| 427 | char *buf) |
| 428 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 429 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 430 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 431 | |
| 432 | return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right); |
| 433 | } |
| 434 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 435 | static ssize_t attr_select_right_store_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 436 | struct device_attribute *attr, |
| 437 | const char *buf, |
| 438 | size_t count) |
| 439 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 440 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 441 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 442 | int value; |
| 443 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 444 | if (kstrtoint(buf, 10, &value)) |
| 445 | return -EINVAL; |
| 446 | if (value < 0 || value > 1) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | data_pointer->select_right = value; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 450 | lenovo_features_set_tpkbd(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 451 | |
| 452 | return count; |
| 453 | } |
| 454 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 455 | static ssize_t attr_sensitivity_show_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 456 | struct device_attribute *attr, |
| 457 | char *buf) |
| 458 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 459 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 460 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 461 | |
| 462 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 463 | data_pointer->sensitivity); |
| 464 | } |
| 465 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 466 | static ssize_t attr_sensitivity_store_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 467 | struct device_attribute *attr, |
| 468 | const char *buf, |
| 469 | size_t count) |
| 470 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 471 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 472 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 473 | int value; |
| 474 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 475 | if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) |
| 476 | return -EINVAL; |
| 477 | |
| 478 | data_pointer->sensitivity = value; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 479 | lenovo_features_set_tpkbd(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 480 | |
| 481 | return count; |
| 482 | } |
| 483 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 484 | static ssize_t attr_press_speed_show_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 485 | struct device_attribute *attr, |
| 486 | char *buf) |
| 487 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 488 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 489 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 490 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 491 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 492 | data_pointer->press_speed); |
| 493 | } |
| 494 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 495 | static ssize_t attr_press_speed_store_tpkbd(struct device *dev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 496 | struct device_attribute *attr, |
| 497 | const char *buf, |
| 498 | size_t count) |
| 499 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 500 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 501 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 502 | int value; |
| 503 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 504 | if (kstrtoint(buf, 10, &value) || value < 1 || value > 255) |
| 505 | return -EINVAL; |
| 506 | |
| 507 | data_pointer->press_speed = value; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 508 | lenovo_features_set_tpkbd(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 509 | |
| 510 | return count; |
| 511 | } |
| 512 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 513 | static struct device_attribute dev_attr_press_to_select_tpkbd = |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 514 | __ATTR(press_to_select, S_IWUSR | S_IRUGO, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 515 | attr_press_to_select_show_tpkbd, |
| 516 | attr_press_to_select_store_tpkbd); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 517 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 518 | static struct device_attribute dev_attr_dragging_tpkbd = |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 519 | __ATTR(dragging, S_IWUSR | S_IRUGO, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 520 | attr_dragging_show_tpkbd, |
| 521 | attr_dragging_store_tpkbd); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 522 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 523 | static struct device_attribute dev_attr_release_to_select_tpkbd = |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 524 | __ATTR(release_to_select, S_IWUSR | S_IRUGO, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 525 | attr_release_to_select_show_tpkbd, |
| 526 | attr_release_to_select_store_tpkbd); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 527 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 528 | static struct device_attribute dev_attr_select_right_tpkbd = |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 529 | __ATTR(select_right, S_IWUSR | S_IRUGO, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 530 | attr_select_right_show_tpkbd, |
| 531 | attr_select_right_store_tpkbd); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 532 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 533 | static struct device_attribute dev_attr_sensitivity_tpkbd = |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 534 | __ATTR(sensitivity, S_IWUSR | S_IRUGO, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 535 | attr_sensitivity_show_tpkbd, |
| 536 | attr_sensitivity_store_tpkbd); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 537 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 538 | static struct device_attribute dev_attr_press_speed_tpkbd = |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 539 | __ATTR(press_speed, S_IWUSR | S_IRUGO, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 540 | attr_press_speed_show_tpkbd, |
| 541 | attr_press_speed_store_tpkbd); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 542 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 543 | static struct attribute *lenovo_attributes_tpkbd[] = { |
| 544 | &dev_attr_press_to_select_tpkbd.attr, |
| 545 | &dev_attr_dragging_tpkbd.attr, |
| 546 | &dev_attr_release_to_select_tpkbd.attr, |
| 547 | &dev_attr_select_right_tpkbd.attr, |
| 548 | &dev_attr_sensitivity_tpkbd.attr, |
| 549 | &dev_attr_press_speed_tpkbd.attr, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 550 | NULL |
| 551 | }; |
| 552 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 553 | static const struct attribute_group lenovo_attr_group_tpkbd = { |
| 554 | .attrs = lenovo_attributes_tpkbd, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 555 | }; |
| 556 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 557 | static enum led_brightness lenovo_led_brightness_get_tpkbd( |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 558 | struct led_classdev *led_cdev) |
| 559 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 560 | struct device *dev = led_cdev->dev->parent; |
| 561 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 562 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 563 | int led_nr = 0; |
| 564 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 565 | if (led_cdev == &data_pointer->led_micmute) |
| 566 | led_nr = 1; |
| 567 | |
| 568 | return data_pointer->led_state & (1 << led_nr) |
| 569 | ? LED_FULL |
| 570 | : LED_OFF; |
| 571 | } |
| 572 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 573 | static void lenovo_led_brightness_set_tpkbd(struct led_classdev *led_cdev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 574 | enum led_brightness value) |
| 575 | { |
Axel Lin | 832fbea | 2012-09-13 14:12:08 +0800 | [diff] [blame] | 576 | struct device *dev = led_cdev->dev->parent; |
| 577 | struct hid_device *hdev = container_of(dev, struct hid_device, dev); |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 578 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 579 | struct hid_report *report; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 580 | int led_nr = 0; |
| 581 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 582 | if (led_cdev == &data_pointer->led_micmute) |
| 583 | led_nr = 1; |
| 584 | |
| 585 | if (value == LED_OFF) |
| 586 | data_pointer->led_state &= ~(1 << led_nr); |
| 587 | else |
| 588 | data_pointer->led_state |= 1 << led_nr; |
| 589 | |
| 590 | report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3]; |
| 591 | report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1; |
| 592 | report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1; |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 593 | hid_hw_request(hdev, report, HID_REQ_SET_REPORT); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 596 | static int lenovo_probe_tpkbd(struct hid_device *hdev) |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 597 | { |
| 598 | struct device *dev = &hdev->dev; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 599 | struct lenovo_drvdata_tpkbd *data_pointer; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 600 | size_t name_sz = strlen(dev_name(dev)) + 16; |
| 601 | char *name_mute, *name_micmute; |
Benjamin Tissoires | 01ab35f | 2013-09-11 22:12:23 +0200 | [diff] [blame] | 602 | int i; |
Jamie Lentin | e0a6aad | 2014-07-26 15:42:27 +0100 | [diff] [blame] | 603 | int ret; |
Kees Cook | 0a9cd0a | 2013-09-11 21:56:55 +0200 | [diff] [blame] | 604 | |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 605 | /* |
| 606 | * Only register extra settings against subdevice where input_mapping |
| 607 | * set drvdata to 1, i.e. the trackpoint. |
| 608 | */ |
| 609 | if (!hid_get_drvdata(hdev)) |
| 610 | return 0; |
| 611 | |
| 612 | hid_set_drvdata(hdev, NULL); |
| 613 | |
Kees Cook | 0a9cd0a | 2013-09-11 21:56:55 +0200 | [diff] [blame] | 614 | /* Validate required reports. */ |
| 615 | for (i = 0; i < 4; i++) { |
| 616 | if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1)) |
| 617 | return -ENODEV; |
| 618 | } |
| 619 | if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2)) |
| 620 | return -ENODEV; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 621 | |
Jamie Lentin | e0a6aad | 2014-07-26 15:42:27 +0100 | [diff] [blame] | 622 | ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd); |
| 623 | if (ret) |
| 624 | hid_warn(hdev, "Could not create sysfs group: %d\n", ret); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 625 | |
Benjamin Tissoires | 01ab35f | 2013-09-11 22:12:23 +0200 | [diff] [blame] | 626 | data_pointer = devm_kzalloc(&hdev->dev, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 627 | sizeof(struct lenovo_drvdata_tpkbd), |
Benjamin Tissoires | 01ab35f | 2013-09-11 22:12:23 +0200 | [diff] [blame] | 628 | GFP_KERNEL); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 629 | if (data_pointer == NULL) { |
| 630 | hid_err(hdev, "Could not allocate memory for driver data\n"); |
Alexey Khoroshilov | b721260 | 2015-05-29 03:39:31 +0300 | [diff] [blame] | 631 | ret = -ENOMEM; |
| 632 | goto err; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | // set same default values as windows driver |
| 636 | data_pointer->sensitivity = 0xa0; |
| 637 | data_pointer->press_speed = 0x38; |
| 638 | |
Benjamin Tissoires | 01ab35f | 2013-09-11 22:12:23 +0200 | [diff] [blame] | 639 | name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL); |
| 640 | name_micmute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL); |
| 641 | if (name_mute == NULL || name_micmute == NULL) { |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 642 | hid_err(hdev, "Could not allocate memory for led data\n"); |
Alexey Khoroshilov | b721260 | 2015-05-29 03:39:31 +0300 | [diff] [blame] | 643 | ret = -ENOMEM; |
| 644 | goto err; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 645 | } |
| 646 | snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(dev)); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 647 | snprintf(name_micmute, name_sz, "%s:amber:micmute", dev_name(dev)); |
| 648 | |
| 649 | hid_set_drvdata(hdev, data_pointer); |
| 650 | |
| 651 | data_pointer->led_mute.name = name_mute; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 652 | data_pointer->led_mute.brightness_get = lenovo_led_brightness_get_tpkbd; |
| 653 | data_pointer->led_mute.brightness_set = lenovo_led_brightness_set_tpkbd; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 654 | data_pointer->led_mute.dev = dev; |
| 655 | led_classdev_register(dev, &data_pointer->led_mute); |
| 656 | |
| 657 | data_pointer->led_micmute.name = name_micmute; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 658 | data_pointer->led_micmute.brightness_get = |
| 659 | lenovo_led_brightness_get_tpkbd; |
| 660 | data_pointer->led_micmute.brightness_set = |
| 661 | lenovo_led_brightness_set_tpkbd; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 662 | data_pointer->led_micmute.dev = dev; |
| 663 | led_classdev_register(dev, &data_pointer->led_micmute); |
| 664 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 665 | lenovo_features_set_tpkbd(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 666 | |
| 667 | return 0; |
Alexey Khoroshilov | b721260 | 2015-05-29 03:39:31 +0300 | [diff] [blame] | 668 | err: |
| 669 | sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd); |
| 670 | return ret; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 673 | static int lenovo_probe_cptkbd(struct hid_device *hdev) |
| 674 | { |
| 675 | int ret; |
| 676 | struct lenovo_drvdata_cptkbd *cptkbd_data; |
| 677 | |
| 678 | /* All the custom action happens on the USBMOUSE device for USB */ |
| 679 | if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD |
| 680 | && hdev->type != HID_TYPE_USBMOUSE) { |
| 681 | hid_dbg(hdev, "Ignoring keyboard half of device\n"); |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | cptkbd_data = devm_kzalloc(&hdev->dev, |
| 686 | sizeof(*cptkbd_data), |
| 687 | GFP_KERNEL); |
| 688 | if (cptkbd_data == NULL) { |
| 689 | hid_err(hdev, "can't alloc keyboard descriptor\n"); |
| 690 | return -ENOMEM; |
| 691 | } |
| 692 | hid_set_drvdata(hdev, cptkbd_data); |
| 693 | |
| 694 | /* |
| 695 | * Tell the keyboard a driver understands it, and turn F7, F9, F11 into |
| 696 | * regular keys |
| 697 | */ |
| 698 | ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03); |
| 699 | if (ret) |
| 700 | hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret); |
| 701 | |
Jamie Lentin | 94eefa2 | 2014-12-16 21:26:46 +0000 | [diff] [blame] | 702 | /* Switch middle button to native mode */ |
| 703 | ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01); |
| 704 | if (ret) |
| 705 | hid_warn(hdev, "Failed to switch middle button: %d\n", ret); |
| 706 | |
Jamie Lentin | e3cb0ac | 2014-12-16 21:26:45 +0000 | [diff] [blame] | 707 | /* Set keyboard settings to known state */ |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 708 | cptkbd_data->fn_lock = true; |
Jamie Lentin | e3cb0ac | 2014-12-16 21:26:45 +0000 | [diff] [blame] | 709 | cptkbd_data->sensitivity = 0x05; |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 710 | lenovo_features_set_cptkbd(hdev); |
| 711 | |
| 712 | ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd); |
| 713 | if (ret) |
| 714 | hid_warn(hdev, "Could not create sysfs group: %d\n", ret); |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 719 | static int lenovo_probe(struct hid_device *hdev, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 720 | const struct hid_device_id *id) |
| 721 | { |
| 722 | int ret; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 723 | |
| 724 | ret = hid_parse(hdev); |
| 725 | if (ret) { |
| 726 | hid_err(hdev, "hid_parse failed\n"); |
Benjamin Tissoires | 0ccdd9e | 2013-09-11 21:56:59 +0200 | [diff] [blame] | 727 | goto err; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 731 | if (ret) { |
| 732 | hid_err(hdev, "hid_hw_start failed\n"); |
Benjamin Tissoires | 0ccdd9e | 2013-09-11 21:56:59 +0200 | [diff] [blame] | 733 | goto err; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 736 | switch (hdev->product) { |
| 737 | case USB_DEVICE_ID_LENOVO_TPKBD: |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 738 | ret = lenovo_probe_tpkbd(hdev); |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 739 | break; |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 740 | case USB_DEVICE_ID_LENOVO_CUSBKBD: |
| 741 | case USB_DEVICE_ID_LENOVO_CBTKBD: |
| 742 | ret = lenovo_probe_cptkbd(hdev); |
| 743 | break; |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 744 | default: |
| 745 | ret = 0; |
| 746 | break; |
Benjamin Tissoires | 0ccdd9e | 2013-09-11 21:56:59 +0200 | [diff] [blame] | 747 | } |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 748 | if (ret) |
| 749 | goto err_hid; |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 750 | |
| 751 | return 0; |
Benjamin Tissoires | 0ccdd9e | 2013-09-11 21:56:59 +0200 | [diff] [blame] | 752 | err_hid: |
| 753 | hid_hw_stop(hdev); |
| 754 | err: |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 755 | return ret; |
| 756 | } |
| 757 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 758 | static void lenovo_remove_tpkbd(struct hid_device *hdev) |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 759 | { |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 760 | struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 761 | |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 762 | /* |
| 763 | * Only the trackpoint half of the keyboard has drvdata and stuff that |
| 764 | * needs unregistering. |
| 765 | */ |
| 766 | if (data_pointer == NULL) |
| 767 | return; |
| 768 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 769 | sysfs_remove_group(&hdev->dev.kobj, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 770 | &lenovo_attr_group_tpkbd); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 771 | |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 772 | led_classdev_unregister(&data_pointer->led_micmute); |
| 773 | led_classdev_unregister(&data_pointer->led_mute); |
| 774 | |
| 775 | hid_set_drvdata(hdev, NULL); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 778 | static void lenovo_remove_cptkbd(struct hid_device *hdev) |
| 779 | { |
| 780 | sysfs_remove_group(&hdev->dev.kobj, |
| 781 | &lenovo_attr_group_cptkbd); |
| 782 | } |
| 783 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 784 | static void lenovo_remove(struct hid_device *hdev) |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 785 | { |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 786 | switch (hdev->product) { |
| 787 | case USB_DEVICE_ID_LENOVO_TPKBD: |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 788 | lenovo_remove_tpkbd(hdev); |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 789 | break; |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 790 | case USB_DEVICE_ID_LENOVO_CUSBKBD: |
| 791 | case USB_DEVICE_ID_LENOVO_CBTKBD: |
| 792 | lenovo_remove_cptkbd(hdev); |
| 793 | break; |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 794 | } |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 795 | |
| 796 | hid_hw_stop(hdev); |
| 797 | } |
| 798 | |
Andreas Fleig | d92189e | 2015-04-23 10:25:58 +0200 | [diff] [blame] | 799 | static void lenovo_input_configured(struct hid_device *hdev, |
| 800 | struct hid_input *hi) |
| 801 | { |
| 802 | switch (hdev->product) { |
| 803 | case USB_DEVICE_ID_LENOVO_TPKBD: |
| 804 | case USB_DEVICE_ID_LENOVO_CUSBKBD: |
| 805 | case USB_DEVICE_ID_LENOVO_CBTKBD: |
| 806 | if (test_bit(EV_REL, hi->input->evbit)) { |
| 807 | /* set only for trackpoint device */ |
| 808 | __set_bit(INPUT_PROP_POINTER, hi->input->propbit); |
| 809 | __set_bit(INPUT_PROP_POINTING_STICK, |
| 810 | hi->input->propbit); |
| 811 | } |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 817 | static const struct hid_device_id lenovo_devices[] = { |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 818 | { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) }, |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 819 | { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) }, |
| 820 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) }, |
Benjamin Tissoires | 181a8b9 | 2015-05-01 16:22:45 -0400 | [diff] [blame] | 821 | { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) }, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 822 | { } |
| 823 | }; |
| 824 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 825 | MODULE_DEVICE_TABLE(hid, lenovo_devices); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 826 | |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 827 | static struct hid_driver lenovo_driver = { |
| 828 | .name = "lenovo", |
| 829 | .id_table = lenovo_devices, |
Andreas Fleig | d92189e | 2015-04-23 10:25:58 +0200 | [diff] [blame] | 830 | .input_configured = lenovo_input_configured, |
Jamie Lentin | 6a5b414 | 2014-07-23 23:30:46 +0100 | [diff] [blame] | 831 | .input_mapping = lenovo_input_mapping, |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 832 | .probe = lenovo_probe, |
| 833 | .remove = lenovo_remove, |
Jamie Lentin | f3d4ff0 | 2014-07-23 23:30:48 +0100 | [diff] [blame] | 834 | .raw_event = lenovo_raw_event, |
Benjamin Tissoires | 181a8b9 | 2015-05-01 16:22:45 -0400 | [diff] [blame] | 835 | .report_fixup = lenovo_report_fixup, |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 836 | }; |
Jamie Lentin | 94723bf | 2014-07-23 23:30:45 +0100 | [diff] [blame] | 837 | module_hid_driver(lenovo_driver); |
Bernhard Seibold | c1dcad2d | 2012-02-15 13:40:43 +0100 | [diff] [blame] | 838 | |
| 839 | MODULE_LICENSE("GPL"); |