hid-lenovo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. bool fn_lock;
  36. };
  37. #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
  38. static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
  39. struct hid_input *hi, struct hid_field *field,
  40. struct hid_usage *usage, unsigned long **bit, int *max)
  41. {
  42. if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
  43. /* This sub-device contains trackpoint, mark it */
  44. hid_set_drvdata(hdev, (void *)1);
  45. map_key_clear(KEY_MICMUTE);
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
  51. struct hid_input *hi, struct hid_field *field,
  52. struct hid_usage *usage, unsigned long **bit, int *max)
  53. {
  54. /* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
  55. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
  56. (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
  57. set_bit(EV_REP, hi->input->evbit);
  58. switch (usage->hid & HID_USAGE) {
  59. case 0x00f1: /* Fn-F4: Mic mute */
  60. map_key_clear(KEY_MICMUTE);
  61. return 1;
  62. case 0x00f2: /* Fn-F5: Brightness down */
  63. map_key_clear(KEY_BRIGHTNESSDOWN);
  64. return 1;
  65. case 0x00f3: /* Fn-F6: Brightness up */
  66. map_key_clear(KEY_BRIGHTNESSUP);
  67. return 1;
  68. case 0x00f4: /* Fn-F7: External display (projector) */
  69. map_key_clear(KEY_SWITCHVIDEOMODE);
  70. return 1;
  71. case 0x00f5: /* Fn-F8: Wireless */
  72. map_key_clear(KEY_WLAN);
  73. return 1;
  74. case 0x00f6: /* Fn-F9: Control panel */
  75. map_key_clear(KEY_CONFIG);
  76. return 1;
  77. case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
  78. map_key_clear(KEY_SCALE);
  79. return 1;
  80. case 0x00fa: /* Fn-Esc: Fn-lock toggle */
  81. map_key_clear(KEY_FN_ESC);
  82. return 1;
  83. case 0x00fb: /* Fn-F12: Open My computer (6 boxes) USB-only */
  84. /* NB: This mapping is invented in raw_event below */
  85. map_key_clear(KEY_FILE);
  86. return 1;
  87. }
  88. }
  89. return 0;
  90. }
  91. static int lenovo_input_mapping(struct hid_device *hdev,
  92. struct hid_input *hi, struct hid_field *field,
  93. struct hid_usage *usage, unsigned long **bit, int *max)
  94. {
  95. switch (hdev->product) {
  96. case USB_DEVICE_ID_LENOVO_TPKBD:
  97. return lenovo_input_mapping_tpkbd(hdev, hi, field,
  98. usage, bit, max);
  99. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  100. case USB_DEVICE_ID_LENOVO_CBTKBD:
  101. return lenovo_input_mapping_cptkbd(hdev, hi, field,
  102. usage, bit, max);
  103. default:
  104. return 0;
  105. }
  106. }
  107. #undef map_key_clear
  108. /* Send a config command to the keyboard */
  109. static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
  110. unsigned char byte2, unsigned char byte3)
  111. {
  112. int ret;
  113. unsigned char buf[] = {0x18, byte2, byte3};
  114. switch (hdev->product) {
  115. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  116. ret = hid_hw_raw_request(hdev, 0x13, buf, sizeof(buf),
  117. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  118. break;
  119. case USB_DEVICE_ID_LENOVO_CBTKBD:
  120. ret = hid_hw_output_report(hdev, buf, sizeof(buf));
  121. break;
  122. default:
  123. ret = -EINVAL;
  124. break;
  125. }
  126. return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
  127. }
  128. static void lenovo_features_set_cptkbd(struct hid_device *hdev)
  129. {
  130. int ret;
  131. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  132. ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
  133. if (ret)
  134. hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
  135. }
  136. static ssize_t attr_fn_lock_show_cptkbd(struct device *dev,
  137. struct device_attribute *attr,
  138. char *buf)
  139. {
  140. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  141. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  142. return snprintf(buf, PAGE_SIZE, "%u\n", cptkbd_data->fn_lock);
  143. }
  144. static ssize_t attr_fn_lock_store_cptkbd(struct device *dev,
  145. struct device_attribute *attr,
  146. const char *buf,
  147. size_t count)
  148. {
  149. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  150. struct lenovo_drvdata_cptkbd *cptkbd_data = hid_get_drvdata(hdev);
  151. int value;
  152. if (kstrtoint(buf, 10, &value))
  153. return -EINVAL;
  154. if (value < 0 || value > 1)
  155. return -EINVAL;
  156. cptkbd_data->fn_lock = !!value;
  157. lenovo_features_set_cptkbd(hdev);
  158. return count;
  159. }
  160. static struct device_attribute dev_attr_fn_lock_cptkbd =
  161. __ATTR(fn_lock, S_IWUSR | S_IRUGO,
  162. attr_fn_lock_show_cptkbd,
  163. attr_fn_lock_store_cptkbd);
  164. static struct attribute *lenovo_attributes_cptkbd[] = {
  165. &dev_attr_fn_lock_cptkbd.attr,
  166. NULL
  167. };
  168. static const struct attribute_group lenovo_attr_group_cptkbd = {
  169. .attrs = lenovo_attributes_cptkbd,
  170. };
  171. static int lenovo_raw_event(struct hid_device *hdev,
  172. struct hid_report *report, u8 *data, int size)
  173. {
  174. /*
  175. * Compact USB keyboard's Fn-F12 report holds down many other keys, and
  176. * its own key is outside the usage page range. Remove extra
  177. * keypresses and remap to inside usage page.
  178. */
  179. if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
  180. && size == 3
  181. && data[0] == 0x15
  182. && data[1] == 0x94
  183. && data[2] == 0x01)) {
  184. data[1] = 0x0;
  185. data[2] = 0x4;
  186. }
  187. return 0;
  188. }
  189. static int lenovo_features_set_tpkbd(struct hid_device *hdev)
  190. {
  191. struct hid_report *report;
  192. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  193. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
  194. report->field[0]->value[0] = data_pointer->press_to_select ? 0x01 : 0x02;
  195. report->field[0]->value[0] |= data_pointer->dragging ? 0x04 : 0x08;
  196. report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
  197. report->field[0]->value[0] |= data_pointer->select_right ? 0x80 : 0x40;
  198. report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
  199. report->field[2]->value[0] = data_pointer->sensitivity;
  200. report->field[3]->value[0] = data_pointer->press_speed;
  201. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  202. return 0;
  203. }
  204. static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
  205. struct device_attribute *attr,
  206. char *buf)
  207. {
  208. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  209. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  210. return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
  211. }
  212. static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
  213. struct device_attribute *attr,
  214. const char *buf,
  215. size_t count)
  216. {
  217. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  218. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  219. int value;
  220. if (kstrtoint(buf, 10, &value))
  221. return -EINVAL;
  222. if (value < 0 || value > 1)
  223. return -EINVAL;
  224. data_pointer->press_to_select = value;
  225. lenovo_features_set_tpkbd(hdev);
  226. return count;
  227. }
  228. static ssize_t attr_dragging_show_tpkbd(struct device *dev,
  229. struct device_attribute *attr,
  230. char *buf)
  231. {
  232. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  233. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  234. return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
  235. }
  236. static ssize_t attr_dragging_store_tpkbd(struct device *dev,
  237. struct device_attribute *attr,
  238. const char *buf,
  239. size_t count)
  240. {
  241. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  242. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  243. int value;
  244. if (kstrtoint(buf, 10, &value))
  245. return -EINVAL;
  246. if (value < 0 || value > 1)
  247. return -EINVAL;
  248. data_pointer->dragging = value;
  249. lenovo_features_set_tpkbd(hdev);
  250. return count;
  251. }
  252. static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
  253. struct device_attribute *attr,
  254. char *buf)
  255. {
  256. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  257. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  258. return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
  259. }
  260. static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
  261. struct device_attribute *attr,
  262. const char *buf,
  263. size_t count)
  264. {
  265. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  266. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  267. int value;
  268. if (kstrtoint(buf, 10, &value))
  269. return -EINVAL;
  270. if (value < 0 || value > 1)
  271. return -EINVAL;
  272. data_pointer->release_to_select = value;
  273. lenovo_features_set_tpkbd(hdev);
  274. return count;
  275. }
  276. static ssize_t attr_select_right_show_tpkbd(struct device *dev,
  277. struct device_attribute *attr,
  278. char *buf)
  279. {
  280. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  281. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  282. return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
  283. }
  284. static ssize_t attr_select_right_store_tpkbd(struct device *dev,
  285. struct device_attribute *attr,
  286. const char *buf,
  287. size_t count)
  288. {
  289. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  290. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  291. int value;
  292. if (kstrtoint(buf, 10, &value))
  293. return -EINVAL;
  294. if (value < 0 || value > 1)
  295. return -EINVAL;
  296. data_pointer->select_right = value;
  297. lenovo_features_set_tpkbd(hdev);
  298. return count;
  299. }
  300. static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
  301. struct device_attribute *attr,
  302. char *buf)
  303. {
  304. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  305. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  306. return snprintf(buf, PAGE_SIZE, "%u\n",
  307. data_pointer->sensitivity);
  308. }
  309. static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
  310. struct device_attribute *attr,
  311. const char *buf,
  312. size_t count)
  313. {
  314. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  315. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  316. int value;
  317. if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
  318. return -EINVAL;
  319. data_pointer->sensitivity = value;
  320. lenovo_features_set_tpkbd(hdev);
  321. return count;
  322. }
  323. static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
  324. struct device_attribute *attr,
  325. char *buf)
  326. {
  327. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  328. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  329. return snprintf(buf, PAGE_SIZE, "%u\n",
  330. data_pointer->press_speed);
  331. }
  332. static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
  333. struct device_attribute *attr,
  334. const char *buf,
  335. size_t count)
  336. {
  337. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  338. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  339. int value;
  340. if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
  341. return -EINVAL;
  342. data_pointer->press_speed = value;
  343. lenovo_features_set_tpkbd(hdev);
  344. return count;
  345. }
  346. static struct device_attribute dev_attr_press_to_select_tpkbd =
  347. __ATTR(press_to_select, S_IWUSR | S_IRUGO,
  348. attr_press_to_select_show_tpkbd,
  349. attr_press_to_select_store_tpkbd);
  350. static struct device_attribute dev_attr_dragging_tpkbd =
  351. __ATTR(dragging, S_IWUSR | S_IRUGO,
  352. attr_dragging_show_tpkbd,
  353. attr_dragging_store_tpkbd);
  354. static struct device_attribute dev_attr_release_to_select_tpkbd =
  355. __ATTR(release_to_select, S_IWUSR | S_IRUGO,
  356. attr_release_to_select_show_tpkbd,
  357. attr_release_to_select_store_tpkbd);
  358. static struct device_attribute dev_attr_select_right_tpkbd =
  359. __ATTR(select_right, S_IWUSR | S_IRUGO,
  360. attr_select_right_show_tpkbd,
  361. attr_select_right_store_tpkbd);
  362. static struct device_attribute dev_attr_sensitivity_tpkbd =
  363. __ATTR(sensitivity, S_IWUSR | S_IRUGO,
  364. attr_sensitivity_show_tpkbd,
  365. attr_sensitivity_store_tpkbd);
  366. static struct device_attribute dev_attr_press_speed_tpkbd =
  367. __ATTR(press_speed, S_IWUSR | S_IRUGO,
  368. attr_press_speed_show_tpkbd,
  369. attr_press_speed_store_tpkbd);
  370. static struct attribute *lenovo_attributes_tpkbd[] = {
  371. &dev_attr_press_to_select_tpkbd.attr,
  372. &dev_attr_dragging_tpkbd.attr,
  373. &dev_attr_release_to_select_tpkbd.attr,
  374. &dev_attr_select_right_tpkbd.attr,
  375. &dev_attr_sensitivity_tpkbd.attr,
  376. &dev_attr_press_speed_tpkbd.attr,
  377. NULL
  378. };
  379. static const struct attribute_group lenovo_attr_group_tpkbd = {
  380. .attrs = lenovo_attributes_tpkbd,
  381. };
  382. static enum led_brightness lenovo_led_brightness_get_tpkbd(
  383. struct led_classdev *led_cdev)
  384. {
  385. struct device *dev = led_cdev->dev->parent;
  386. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  387. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  388. int led_nr = 0;
  389. if (led_cdev == &data_pointer->led_micmute)
  390. led_nr = 1;
  391. return data_pointer->led_state & (1 << led_nr)
  392. ? LED_FULL
  393. : LED_OFF;
  394. }
  395. static void lenovo_led_brightness_set_tpkbd(struct led_classdev *led_cdev,
  396. enum led_brightness value)
  397. {
  398. struct device *dev = led_cdev->dev->parent;
  399. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  400. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  401. struct hid_report *report;
  402. int led_nr = 0;
  403. if (led_cdev == &data_pointer->led_micmute)
  404. led_nr = 1;
  405. if (value == LED_OFF)
  406. data_pointer->led_state &= ~(1 << led_nr);
  407. else
  408. data_pointer->led_state |= 1 << led_nr;
  409. report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
  410. report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
  411. report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
  412. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  413. }
  414. static int lenovo_probe_tpkbd(struct hid_device *hdev)
  415. {
  416. struct device *dev = &hdev->dev;
  417. struct lenovo_drvdata_tpkbd *data_pointer;
  418. size_t name_sz = strlen(dev_name(dev)) + 16;
  419. char *name_mute, *name_micmute;
  420. int i;
  421. int ret;
  422. /*
  423. * Only register extra settings against subdevice where input_mapping
  424. * set drvdata to 1, i.e. the trackpoint.
  425. */
  426. if (!hid_get_drvdata(hdev))
  427. return 0;
  428. hid_set_drvdata(hdev, NULL);
  429. /* Validate required reports. */
  430. for (i = 0; i < 4; i++) {
  431. if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
  432. return -ENODEV;
  433. }
  434. if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
  435. return -ENODEV;
  436. ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
  437. if (ret)
  438. hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
  439. data_pointer = devm_kzalloc(&hdev->dev,
  440. sizeof(struct lenovo_drvdata_tpkbd),
  441. GFP_KERNEL);
  442. if (data_pointer == NULL) {
  443. hid_err(hdev, "Could not allocate memory for driver data\n");
  444. return -ENOMEM;
  445. }
  446. // set same default values as windows driver
  447. data_pointer->sensitivity = 0xa0;
  448. data_pointer->press_speed = 0x38;
  449. name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
  450. name_micmute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
  451. if (name_mute == NULL || name_micmute == NULL) {
  452. hid_err(hdev, "Could not allocate memory for led data\n");
  453. return -ENOMEM;
  454. }
  455. snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(dev));
  456. snprintf(name_micmute, name_sz, "%s:amber:micmute", dev_name(dev));
  457. hid_set_drvdata(hdev, data_pointer);
  458. data_pointer->led_mute.name = name_mute;
  459. data_pointer->led_mute.brightness_get = lenovo_led_brightness_get_tpkbd;
  460. data_pointer->led_mute.brightness_set = lenovo_led_brightness_set_tpkbd;
  461. data_pointer->led_mute.dev = dev;
  462. led_classdev_register(dev, &data_pointer->led_mute);
  463. data_pointer->led_micmute.name = name_micmute;
  464. data_pointer->led_micmute.brightness_get =
  465. lenovo_led_brightness_get_tpkbd;
  466. data_pointer->led_micmute.brightness_set =
  467. lenovo_led_brightness_set_tpkbd;
  468. data_pointer->led_micmute.dev = dev;
  469. led_classdev_register(dev, &data_pointer->led_micmute);
  470. lenovo_features_set_tpkbd(hdev);
  471. return 0;
  472. }
  473. static int lenovo_probe_cptkbd(struct hid_device *hdev)
  474. {
  475. int ret;
  476. struct lenovo_drvdata_cptkbd *cptkbd_data;
  477. /* All the custom action happens on the USBMOUSE device for USB */
  478. if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
  479. && hdev->type != HID_TYPE_USBMOUSE) {
  480. hid_dbg(hdev, "Ignoring keyboard half of device\n");
  481. return 0;
  482. }
  483. cptkbd_data = devm_kzalloc(&hdev->dev,
  484. sizeof(*cptkbd_data),
  485. GFP_KERNEL);
  486. if (cptkbd_data == NULL) {
  487. hid_err(hdev, "can't alloc keyboard descriptor\n");
  488. return -ENOMEM;
  489. }
  490. hid_set_drvdata(hdev, cptkbd_data);
  491. /*
  492. * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
  493. * regular keys
  494. */
  495. ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
  496. if (ret)
  497. hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
  498. /* Turn Fn-Lock on by default */
  499. cptkbd_data->fn_lock = true;
  500. lenovo_features_set_cptkbd(hdev);
  501. ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
  502. if (ret)
  503. hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
  504. return 0;
  505. }
  506. static int lenovo_probe(struct hid_device *hdev,
  507. const struct hid_device_id *id)
  508. {
  509. int ret;
  510. ret = hid_parse(hdev);
  511. if (ret) {
  512. hid_err(hdev, "hid_parse failed\n");
  513. goto err;
  514. }
  515. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  516. if (ret) {
  517. hid_err(hdev, "hid_hw_start failed\n");
  518. goto err;
  519. }
  520. switch (hdev->product) {
  521. case USB_DEVICE_ID_LENOVO_TPKBD:
  522. ret = lenovo_probe_tpkbd(hdev);
  523. break;
  524. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  525. case USB_DEVICE_ID_LENOVO_CBTKBD:
  526. ret = lenovo_probe_cptkbd(hdev);
  527. break;
  528. default:
  529. ret = 0;
  530. break;
  531. }
  532. if (ret)
  533. goto err_hid;
  534. return 0;
  535. err_hid:
  536. hid_hw_stop(hdev);
  537. err:
  538. return ret;
  539. }
  540. static void lenovo_remove_tpkbd(struct hid_device *hdev)
  541. {
  542. struct lenovo_drvdata_tpkbd *data_pointer = hid_get_drvdata(hdev);
  543. /*
  544. * Only the trackpoint half of the keyboard has drvdata and stuff that
  545. * needs unregistering.
  546. */
  547. if (data_pointer == NULL)
  548. return;
  549. sysfs_remove_group(&hdev->dev.kobj,
  550. &lenovo_attr_group_tpkbd);
  551. led_classdev_unregister(&data_pointer->led_micmute);
  552. led_classdev_unregister(&data_pointer->led_mute);
  553. hid_set_drvdata(hdev, NULL);
  554. }
  555. static void lenovo_remove_cptkbd(struct hid_device *hdev)
  556. {
  557. sysfs_remove_group(&hdev->dev.kobj,
  558. &lenovo_attr_group_cptkbd);
  559. }
  560. static void lenovo_remove(struct hid_device *hdev)
  561. {
  562. switch (hdev->product) {
  563. case USB_DEVICE_ID_LENOVO_TPKBD:
  564. lenovo_remove_tpkbd(hdev);
  565. break;
  566. case USB_DEVICE_ID_LENOVO_CUSBKBD:
  567. case USB_DEVICE_ID_LENOVO_CBTKBD:
  568. lenovo_remove_cptkbd(hdev);
  569. break;
  570. }
  571. hid_hw_stop(hdev);
  572. }
  573. static const struct hid_device_id lenovo_devices[] = {
  574. { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
  575. { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
  576. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
  577. { }
  578. };
  579. MODULE_DEVICE_TABLE(hid, lenovo_devices);
  580. static struct hid_driver lenovo_driver = {
  581. .name = "lenovo",
  582. .id_table = lenovo_devices,
  583. .input_mapping = lenovo_input_mapping,
  584. .probe = lenovo_probe,
  585. .remove = lenovo_remove,
  586. .raw_event = lenovo_raw_event,
  587. };
  588. module_hid_driver(lenovo_driver);
  589. MODULE_LICENSE("GPL");