hid-lenovo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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[] = {0x18, byte2, byte3};
  171. switch (hdev->product) {
  172. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  173. ret = hid_hw_raw_request(hdev, 0x13, buf, sizeof(buf),
  174. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  175. break;
  176. case USB_DEVICE_ID_LENOVO_CBTKBD:
  177. ret = hid_hw_output_report(hdev, buf, sizeof(buf));
  178. break;
  179. default:
  180. ret = -EINVAL;
  181. break;
  182. }
  183. return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
  184. }
  185. static void lenovo_features_set_cptkbd(struct hid_device *hdev)
  186. {
  187. int ret;
  188. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  189. ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
  190. if (ret)
  191. hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
  192. ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
  193. if (ret)
  194. hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
  195. }
  196. static ssize_t attr_fn_lock_show_cptkbd(struct device *dev,
  197. struct device_attribute *attr,
  198. char *buf)
  199. {
  200. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  201. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  202. return snprintf(buf, PAGE_SIZE, "%u\n", cptkbd_data->fn_lock);
  203. }
  204. static ssize_t attr_fn_lock_store_cptkbd(struct device *dev,
  205. struct device_attribute *attr,
  206. const char *buf,
  207. size_t count)
  208. {
  209. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  210. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  211. int value;
  212. if (kstrtoint(buf, 10, &value))
  213. return -EINVAL;
  214. if (value < 0 || value > 1)
  215. return -EINVAL;
  216. cptkbd_data->fn_lock = !!value;
  217. lenovo_features_set_cptkbd(hdev);
  218. return count;
  219. }
  220. static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
  221. struct device_attribute *attr,
  222. char *buf)
  223. {
  224. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  225. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  226. return snprintf(buf, PAGE_SIZE, "%u\n",
  227. cptkbd_data->sensitivity);
  228. }
  229. static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
  230. struct device_attribute *attr,
  231. const char *buf,
  232. size_t count)
  233. {
  234. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  235. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  236. int value;
  237. if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
  238. return -EINVAL;
  239. cptkbd_data->sensitivity = value;
  240. lenovo_features_set_cptkbd(hdev);
  241. return count;
  242. }
  243. static struct device_attribute dev_attr_fn_lock_cptkbd =
  244. __ATTR(fn_lock, S_IWUSR | S_IRUGO,
  245. attr_fn_lock_show_cptkbd,
  246. attr_fn_lock_store_cptkbd);
  247. static struct device_attribute dev_attr_sensitivity_cptkbd =
  248. __ATTR(sensitivity, S_IWUSR | S_IRUGO,
  249. attr_sensitivity_show_cptkbd,
  250. attr_sensitivity_store_cptkbd);
  251. static struct attribute *lenovo_attributes_cptkbd[] = {
  252. &dev_attr_fn_lock_cptkbd.attr,
  253. &dev_attr_sensitivity_cptkbd.attr,
  254. NULL
  255. };
  256. static const struct attribute_group lenovo_attr_group_cptkbd = {
  257. .attrs = lenovo_attributes_cptkbd,
  258. };
  259. static int lenovo_raw_event(struct hid_device *hdev,
  260. struct hid_report *report, u8 *data, int size)
  261. {
  262. /*
  263. * Compact USB keyboard's Fn-F12 report holds down many other keys, and
  264. * its own key is outside the usage page range. Remove extra
  265. * keypresses and remap to inside usage page.
  266. */
  267. if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
  268. && size == 3
  269. && data[0] == 0x15
  270. && data[1] == 0x94
  271. && data[2] == 0x01)) {
  272. data[1] = 0x00;
  273. data[2] = 0x01;
  274. }
  275. return 0;
  276. }
  277. static int lenovo_event_cptkbd(struct hid_device *hdev,
  278. struct hid_field *field, struct hid_usage *usage, __s32 value)
  279. {
  280. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  281. /* "wheel" scroll events */
  282. if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
  283. usage->code == REL_HWHEEL)) {
  284. /* Scroll events disable middle-click event */
  285. cptkbd_data->middlebutton_state = 2;
  286. return 0;
  287. }
  288. /* Middle click events */
  289. if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
  290. if (value == 1) {
  291. cptkbd_data->middlebutton_state = 1;
  292. } else if (value == 0) {
  293. if (cptkbd_data->middlebutton_state == 1) {
  294. /* No scrolling inbetween, send middle-click */
  295. input_event(field->hidinput->input,
  296. EV_KEY, BTN_MIDDLE, 1);
  297. input_sync(field->hidinput->input);
  298. input_event(field->hidinput->input,
  299. EV_KEY, BTN_MIDDLE, 0);
  300. input_sync(field->hidinput->input);
  301. }
  302. cptkbd_data->middlebutton_state = 0;
  303. }
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
  309. struct hid_usage *usage, __s32 value)
  310. {
  311. switch (hdev->product) {
  312. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  313. case USB_DEVICE_ID_LENOVO_CBTKBD:
  314. return lenovo_event_cptkbd(hdev, field, usage, value);
  315. default:
  316. return 0;
  317. }
  318. }
  319. static int lenovo_features_set_tpkbd(struct hid_device *hdev)
  320. {
  321. struct hid_report *report;
  322. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  323. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
  324. report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
  325. report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
  326. report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
  327. report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
  328. report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
  329. report->field[2]->value[0] = data_pointer->sensitivity;
  330. report->field[3]->value[0] = data_pointer->press_speed;
  331. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  332. return 0;
  333. }
  334. static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
  335. struct device_attribute *attr,
  336. char *buf)
  337. {
  338. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  339. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  340. return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
  341. }
  342. static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
  343. struct device_attribute *attr,
  344. const char *buf,
  345. size_t count)
  346. {
  347. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  348. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  349. int value;
  350. if (kstrtoint(buf, 10, &value))
  351. return -EINVAL;
  352. if (value < 0 || value > 1)
  353. return -EINVAL;
  354. data_pointer->press_to_select = value;
  355. lenovo_features_set_tpkbd(hdev);
  356. return count;
  357. }
  358. static ssize_t attr_dragging_show_tpkbd(struct device *dev,
  359. struct device_attribute *attr,
  360. char *buf)
  361. {
  362. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  363. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  364. return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
  365. }
  366. static ssize_t attr_dragging_store_tpkbd(struct device *dev,
  367. struct device_attribute *attr,
  368. const char *buf,
  369. size_t count)
  370. {
  371. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  372. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  373. int value;
  374. if (kstrtoint(buf, 10, &value))
  375. return -EINVAL;
  376. if (value < 0 || value > 1)
  377. return -EINVAL;
  378. data_pointer->dragging = value;
  379. lenovo_features_set_tpkbd(hdev);
  380. return count;
  381. }
  382. static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
  383. struct device_attribute *attr,
  384. char *buf)
  385. {
  386. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  387. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  388. return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
  389. }
  390. static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
  391. struct device_attribute *attr,
  392. const char *buf,
  393. size_t count)
  394. {
  395. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  396. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  397. int value;
  398. if (kstrtoint(buf, 10, &value))
  399. return -EINVAL;
  400. if (value < 0 || value > 1)
  401. return -EINVAL;
  402. data_pointer->release_to_select = value;
  403. lenovo_features_set_tpkbd(hdev);
  404. return count;
  405. }
  406. static ssize_t attr_select_right_show_tpkbd(struct device *dev,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  411. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  412. return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
  413. }
  414. static ssize_t attr_select_right_store_tpkbd(struct device *dev,
  415. struct device_attribute *attr,
  416. const char *buf,
  417. size_t count)
  418. {
  419. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  420. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  421. int value;
  422. if (kstrtoint(buf, 10, &value))
  423. return -EINVAL;
  424. if (value < 0 || value > 1)
  425. return -EINVAL;
  426. data_pointer->select_right = value;
  427. lenovo_features_set_tpkbd(hdev);
  428. return count;
  429. }
  430. static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
  431. struct device_attribute *attr,
  432. char *buf)
  433. {
  434. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  435. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  436. return snprintf(buf, PAGE_SIZE, "%u\n",
  437. data_pointer->sensitivity);
  438. }
  439. static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
  440. struct device_attribute *attr,
  441. const char *buf,
  442. size_t count)
  443. {
  444. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  445. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  446. int value;
  447. if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
  448. return -EINVAL;
  449. data_pointer->sensitivity = value;
  450. lenovo_features_set_tpkbd(hdev);
  451. return count;
  452. }
  453. static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
  454. struct device_attribute *attr,
  455. char *buf)
  456. {
  457. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  458. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  459. return snprintf(buf, PAGE_SIZE, "%u\n",
  460. data_pointer->press_speed);
  461. }
  462. static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
  463. struct device_attribute *attr,
  464. const char *buf,
  465. size_t count)
  466. {
  467. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  468. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  469. int value;
  470. if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
  471. return -EINVAL;
  472. data_pointer->press_speed = value;
  473. lenovo_features_set_tpkbd(hdev);
  474. return count;
  475. }
  476. static struct device_attribute dev_attr_press_to_select_tpkbd =
  477. __ATTR(press_to_select, S_IWUSR | S_IRUGO,
  478. attr_press_to_select_show_tpkbd,
  479. attr_press_to_select_store_tpkbd);
  480. static struct device_attribute dev_attr_dragging_tpkbd =
  481. __ATTR(dragging, S_IWUSR | S_IRUGO,
  482. attr_dragging_show_tpkbd,
  483. attr_dragging_store_tpkbd);
  484. static struct device_attribute dev_attr_release_to_select_tpkbd =
  485. __ATTR(release_to_select, S_IWUSR | S_IRUGO,
  486. attr_release_to_select_show_tpkbd,
  487. attr_release_to_select_store_tpkbd);
  488. static struct device_attribute dev_attr_select_right_tpkbd =
  489. __ATTR(select_right, S_IWUSR | S_IRUGO,
  490. attr_select_right_show_tpkbd,
  491. attr_select_right_store_tpkbd);
  492. static struct device_attribute dev_attr_sensitivity_tpkbd =
  493. __ATTR(sensitivity, S_IWUSR | S_IRUGO,
  494. attr_sensitivity_show_tpkbd,
  495. attr_sensitivity_store_tpkbd);
  496. static struct device_attribute dev_attr_press_speed_tpkbd =
  497. __ATTR(press_speed, S_IWUSR | S_IRUGO,
  498. attr_press_speed_show_tpkbd,
  499. attr_press_speed_store_tpkbd);
  500. static struct attribute *lenovo_attributes_tpkbd[] = {
  501. &dev_attr_press_to_select_tpkbd.attr,
  502. &dev_attr_dragging_tpkbd.attr,
  503. &dev_attr_release_to_select_tpkbd.attr,
  504. &dev_attr_select_right_tpkbd.attr,
  505. &dev_attr_sensitivity_tpkbd.attr,
  506. &dev_attr_press_speed_tpkbd.attr,
  507. NULL
  508. };
  509. static const struct attribute_group lenovo_attr_group_tpkbd = {
  510. .attrs = lenovo_attributes_tpkbd,
  511. };
  512. static enum led_brightness lenovo_led_brightness_get_tpkbd(
  513. struct led_classdev *led_cdev)
  514. {
  515. struct device *dev = led_cdev->dev->parent;
  516. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  517. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  518. int led_nr = 0;
  519. if (led_cdev == &data_pointer->led_micmute)
  520. led_nr = 1;
  521. return data_pointer->led_state & (1 << led_nr)
  522. ? LED_FULL
  523. : LED_OFF;
  524. }
  525. static void lenovo_led_brightness_set_tpkbd(struct led_classdev *led_cdev,
  526. enum led_brightness value)
  527. {
  528. struct device *dev = led_cdev->dev->parent;
  529. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  530. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  531. struct hid_report *report;
  532. int led_nr = 0;
  533. if (led_cdev == &data_pointer->led_micmute)
  534. led_nr = 1;
  535. if (value == LED_OFF)
  536. data_pointer->led_state &= ~(1 << led_nr);
  537. else
  538. data_pointer->led_state |= 1 << led_nr;
  539. report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
  540. report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
  541. report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
  542. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  543. }
  544. static int lenovo_probe_tpkbd(struct hid_device *hdev)
  545. {
  546. struct device *dev = &hdev->dev;
  547. struct lenovo_drvdata_tpkbd *data_pointer;
  548. size_t name_sz = strlen(dev_name(dev)) + 16;
  549. char *name_mute, *name_micmute;
  550. int i;
  551. int ret;
  552. /*
  553. * Only register extra settings against subdevice where input_mapping
  554. * set drvdata to 1, i.e. the trackpoint.
  555. */
  556. if (!hid_get_drvdata(hdev))
  557. return 0;
  558. hid_set_drvdata(hdev, NULL);
  559. /* Validate required reports. */
  560. for (i = 0; i < 4; i++) {
  561. if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
  562. return -ENODEV;
  563. }
  564. if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
  565. return -ENODEV;
  566. ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
  567. if (ret)
  568. hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
  569. data_pointer = devm_kzalloc(&hdev->dev,
  570. sizeof(struct lenovo_drvdata_tpkbd),
  571. GFP_KERNEL);
  572. if (data_pointer == NULL) {
  573. hid_err(hdev, "Could not allocate memory for driver data\n");
  574. ret = -ENOMEM;
  575. goto err;
  576. }
  577. // set same default values as windows driver
  578. data_pointer->sensitivity = 0xa0;
  579. data_pointer->press_speed = 0x38;
  580. name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
  581. name_micmute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
  582. if (name_mute == NULL || name_micmute == NULL) {
  583. hid_err(hdev, "Could not allocate memory for led data\n");
  584. ret = -ENOMEM;
  585. goto err;
  586. }
  587. snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(dev));
  588. snprintf(name_micmute, name_sz, "%s:amber:micmute", dev_name(dev));
  589. hid_set_drvdata(hdev, data_pointer);
  590. data_pointer->led_mute.name = name_mute;
  591. data_pointer->led_mute.brightness_get = lenovo_led_brightness_get_tpkbd;
  592. data_pointer->led_mute.brightness_set = lenovo_led_brightness_set_tpkbd;
  593. data_pointer->led_mute.dev = dev;
  594. ret = led_classdev_register(dev, &data_pointer->led_mute);
  595. if (ret < 0)
  596. goto err;
  597. data_pointer->led_micmute.name = name_micmute;
  598. data_pointer->led_micmute.brightness_get =
  599. lenovo_led_brightness_get_tpkbd;
  600. data_pointer->led_micmute.brightness_set =
  601. lenovo_led_brightness_set_tpkbd;
  602. data_pointer->led_micmute.dev = dev;
  603. ret = led_classdev_register(dev, &data_pointer->led_micmute);
  604. if (ret < 0) {
  605. led_classdev_unregister(&data_pointer->led_mute);
  606. goto err;
  607. }
  608. lenovo_features_set_tpkbd(hdev);
  609. return 0;
  610. err:
  611. sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
  612. return ret;
  613. }
  614. static int lenovo_probe_cptkbd(struct hid_device *hdev)
  615. {
  616. int ret;
  617. struct lenovo_drvdata_cptkbd *cptkbd_data;
  618. /* All the custom action happens on the USBMOUSE device for USB */
  619. if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
  620. && hdev->type != HID_TYPE_USBMOUSE) {
  621. hid_dbg(hdev, "Ignoring keyboard half of device\n");
  622. return 0;
  623. }
  624. cptkbd_data = devm_kzalloc(&hdev->dev,
  625. sizeof(*cptkbd_data),
  626. GFP_KERNEL);
  627. if (cptkbd_data == NULL) {
  628. hid_err(hdev, "can't alloc keyboard descriptor\n");
  629. return -ENOMEM;
  630. }
  631. hid_set_drvdata(hdev, cptkbd_data);
  632. /*
  633. * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
  634. * regular keys
  635. */
  636. ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
  637. if (ret)
  638. hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
  639. /* Switch middle button to native mode */
  640. ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
  641. if (ret)
  642. hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
  643. /* Set keyboard settings to known state */
  644. cptkbd_data->middlebutton_state = 0;
  645. cptkbd_data->fn_lock = true;
  646. cptkbd_data->sensitivity = 0x05;
  647. lenovo_features_set_cptkbd(hdev);
  648. ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
  649. if (ret)
  650. hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
  651. return 0;
  652. }
  653. static int lenovo_probe(struct hid_device *hdev,
  654. const struct hid_device_id *id)
  655. {
  656. int ret;
  657. ret = hid_parse(hdev);
  658. if (ret) {
  659. hid_err(hdev, "hid_parse failed\n");
  660. goto err;
  661. }
  662. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  663. if (ret) {
  664. hid_err(hdev, "hid_hw_start failed\n");
  665. goto err;
  666. }
  667. switch (hdev->product) {
  668. case USB_DEVICE_ID_LENOVO_TPKBD:
  669. ret = lenovo_probe_tpkbd(hdev);
  670. break;
  671. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  672. case USB_DEVICE_ID_LENOVO_CBTKBD:
  673. ret = lenovo_probe_cptkbd(hdev);
  674. break;
  675. default:
  676. ret = 0;
  677. break;
  678. }
  679. if (ret)
  680. goto err_hid;
  681. return 0;
  682. err_hid:
  683. hid_hw_stop(hdev);
  684. err:
  685. return ret;
  686. }
  687. static void lenovo_remove_tpkbd(struct hid_device *hdev)
  688. {
  689. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  690. /*
  691. * Only the trackpoint half of the keyboard has drvdata and stuff that
  692. * needs unregistering.
  693. */
  694. if (data_pointer == NULL)
  695. return;
  696. sysfs_remove_group(&hdev->dev.kobj,
  697. &lenovo_attr_group_tpkbd);
  698. led_classdev_unregister(&data_pointer->led_micmute);
  699. led_classdev_unregister(&data_pointer->led_mute);
  700. hid_set_drvdata(hdev, NULL);
  701. }
  702. static void lenovo_remove_cptkbd(struct hid_device *hdev)
  703. {
  704. sysfs_remove_group(&hdev->dev.kobj,
  705. &lenovo_attr_group_cptkbd);
  706. }
  707. static void lenovo_remove(struct hid_device *hdev)
  708. {
  709. switch (hdev->product) {
  710. case USB_DEVICE_ID_LENOVO_TPKBD:
  711. lenovo_remove_tpkbd(hdev);
  712. break;
  713. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  714. case USB_DEVICE_ID_LENOVO_CBTKBD:
  715. lenovo_remove_cptkbd(hdev);
  716. break;
  717. }
  718. hid_hw_stop(hdev);
  719. }
  720. static int lenovo_input_configured(struct hid_device *hdev,
  721. struct hid_input *hi)
  722. {
  723. switch (hdev->product) {
  724. case USB_DEVICE_ID_LENOVO_TPKBD:
  725. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  726. case USB_DEVICE_ID_LENOVO_CBTKBD:
  727. if (test_bit(EV_REL, hi->input->evbit)) {
  728. /* set only for trackpoint device */
  729. __set_bit(INPUT_PROP_POINTER, hi->input->propbit);
  730. __set_bit(INPUT_PROP_POINTING_STICK,
  731. hi->input->propbit);
  732. }
  733. break;
  734. }
  735. return 0;
  736. }
  737. static const struct hid_device_id lenovo_devices[] = {
  738. { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
  739. { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
  740. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
  741. { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
  742. { }
  743. };
  744. MODULE_DEVICE_TABLE(hid, lenovo_devices);
  745. static struct hid_driver lenovo_driver = {
  746. .name = "lenovo",
  747. .id_table = lenovo_devices,
  748. .input_configured = lenovo_input_configured,
  749. .input_mapping = lenovo_input_mapping,
  750. .probe = lenovo_probe,
  751. .remove = lenovo_remove,
  752. .raw_event = lenovo_raw_event,
  753. .event = lenovo_event,
  754. .report_fixup = lenovo_report_fixup,
  755. };
  756. module_hid_driver(lenovo_driver);
  757. MODULE_LICENSE("GPL");