hid-lenovo.c 24 KB

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