hid-lenovo.c 25 KB

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