hid-google-hammer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * HID driver for Google Hammer device.
  4. *
  5. * Copyright (c) 2017 Google Inc.
  6. * Author: Wei-Ning Huang <wnhuang@google.com>
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/hid.h>
  16. #include <linux/input/vivaldi-fmap.h>
  17. #include <linux/leds.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_data/cros_ec_commands.h>
  21. #include <linux/platform_data/cros_ec_proto.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_wakeup.h>
  24. #include <linux/unaligned.h>
  25. #include "hid-ids.h"
  26. #include "hid-vivaldi-common.h"
  27. /*
  28. * C(hrome)B(ase)A(ttached)S(witch) - switch exported by Chrome EC and reporting
  29. * state of the "Whiskers" base - attached or detached. Whiskers USB device also
  30. * reports position of the keyboard - folded or not. Combining base state and
  31. * position allows us to generate proper "Tablet mode" events.
  32. */
  33. struct cbas_ec {
  34. struct device *dev; /* The platform device (EC) */
  35. struct input_dev *input;
  36. bool base_present;
  37. bool base_folded;
  38. struct notifier_block notifier;
  39. };
  40. static struct cbas_ec cbas_ec;
  41. static DEFINE_SPINLOCK(cbas_ec_lock);
  42. static DEFINE_MUTEX(cbas_ec_reglock);
  43. static bool cbas_parse_base_state(const void *data)
  44. {
  45. u32 switches = get_unaligned_le32(data);
  46. return !!(switches & BIT(EC_MKBP_BASE_ATTACHED));
  47. }
  48. static int cbas_ec_query_base(struct cros_ec_device *ec_dev, bool get_state,
  49. bool *state)
  50. {
  51. struct ec_params_mkbp_info *params;
  52. struct cros_ec_command *msg;
  53. int ret;
  54. msg = kzalloc(struct_size(msg, data, max(sizeof(u32), sizeof(*params))),
  55. GFP_KERNEL);
  56. if (!msg)
  57. return -ENOMEM;
  58. msg->command = EC_CMD_MKBP_INFO;
  59. msg->version = 1;
  60. msg->outsize = sizeof(*params);
  61. msg->insize = sizeof(u32);
  62. params = (struct ec_params_mkbp_info *)msg->data;
  63. params->info_type = get_state ?
  64. EC_MKBP_INFO_CURRENT : EC_MKBP_INFO_SUPPORTED;
  65. params->event_type = EC_MKBP_EVENT_SWITCH;
  66. ret = cros_ec_cmd_xfer_status(ec_dev, msg);
  67. if (ret >= 0) {
  68. if (ret != sizeof(u32)) {
  69. dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n",
  70. ret, sizeof(u32));
  71. ret = -EPROTO;
  72. } else {
  73. *state = cbas_parse_base_state(msg->data);
  74. ret = 0;
  75. }
  76. }
  77. kfree(msg);
  78. return ret;
  79. }
  80. static int cbas_ec_notify(struct notifier_block *nb,
  81. unsigned long queued_during_suspend,
  82. void *_notify)
  83. {
  84. struct cros_ec_device *ec = _notify;
  85. unsigned long flags;
  86. bool base_present;
  87. if (ec->event_data.event_type == EC_MKBP_EVENT_SWITCH) {
  88. base_present = cbas_parse_base_state(
  89. &ec->event_data.data.switches);
  90. dev_dbg(cbas_ec.dev,
  91. "%s: base: %d\n", __func__, base_present);
  92. if (device_may_wakeup(cbas_ec.dev) ||
  93. !queued_during_suspend) {
  94. pm_wakeup_event(cbas_ec.dev, 0);
  95. spin_lock_irqsave(&cbas_ec_lock, flags);
  96. /*
  97. * While input layer dedupes the events, we do not want
  98. * to disrupt the state reported by the base by
  99. * overriding it with state reported by the LID. Only
  100. * report changes, as we assume that on attach the base
  101. * is not folded.
  102. */
  103. if (base_present != cbas_ec.base_present) {
  104. input_report_switch(cbas_ec.input,
  105. SW_TABLET_MODE,
  106. !base_present);
  107. input_sync(cbas_ec.input);
  108. cbas_ec.base_present = base_present;
  109. }
  110. spin_unlock_irqrestore(&cbas_ec_lock, flags);
  111. }
  112. }
  113. return NOTIFY_OK;
  114. }
  115. static __maybe_unused int cbas_ec_resume(struct device *dev)
  116. {
  117. struct cros_ec_device *ec = dev_get_drvdata(dev->parent);
  118. bool base_present;
  119. int error;
  120. error = cbas_ec_query_base(ec, true, &base_present);
  121. if (error) {
  122. dev_warn(dev, "failed to fetch base state on resume: %d\n",
  123. error);
  124. } else {
  125. spin_lock_irq(&cbas_ec_lock);
  126. cbas_ec.base_present = base_present;
  127. /*
  128. * Only report if base is disconnected. If base is connected,
  129. * it will resend its state on resume, and we'll update it
  130. * in hammer_event().
  131. */
  132. if (!cbas_ec.base_present) {
  133. input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1);
  134. input_sync(cbas_ec.input);
  135. }
  136. spin_unlock_irq(&cbas_ec_lock);
  137. }
  138. return 0;
  139. }
  140. static SIMPLE_DEV_PM_OPS(cbas_ec_pm_ops, NULL, cbas_ec_resume);
  141. static void cbas_ec_set_input(struct input_dev *input)
  142. {
  143. /* Take the lock so hammer_event() does not race with us here */
  144. spin_lock_irq(&cbas_ec_lock);
  145. cbas_ec.input = input;
  146. spin_unlock_irq(&cbas_ec_lock);
  147. }
  148. static int __cbas_ec_probe(struct platform_device *pdev)
  149. {
  150. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  151. struct input_dev *input;
  152. bool base_supported;
  153. int error;
  154. error = cbas_ec_query_base(ec, false, &base_supported);
  155. if (error)
  156. return error;
  157. if (!base_supported)
  158. return -ENXIO;
  159. input = devm_input_allocate_device(&pdev->dev);
  160. if (!input)
  161. return -ENOMEM;
  162. input->name = "Whiskers Tablet Mode Switch";
  163. input->id.bustype = BUS_HOST;
  164. input_set_capability(input, EV_SW, SW_TABLET_MODE);
  165. error = input_register_device(input);
  166. if (error) {
  167. dev_err(&pdev->dev, "cannot register input device: %d\n",
  168. error);
  169. return error;
  170. }
  171. /* Seed the state */
  172. error = cbas_ec_query_base(ec, true, &cbas_ec.base_present);
  173. if (error) {
  174. dev_err(&pdev->dev, "cannot query base state: %d\n", error);
  175. return error;
  176. }
  177. if (!cbas_ec.base_present)
  178. cbas_ec.base_folded = false;
  179. dev_dbg(&pdev->dev, "%s: base: %d, folded: %d\n", __func__,
  180. cbas_ec.base_present, cbas_ec.base_folded);
  181. input_report_switch(input, SW_TABLET_MODE,
  182. !cbas_ec.base_present || cbas_ec.base_folded);
  183. cbas_ec_set_input(input);
  184. cbas_ec.dev = &pdev->dev;
  185. cbas_ec.notifier.notifier_call = cbas_ec_notify;
  186. error = blocking_notifier_chain_register(&ec->event_notifier,
  187. &cbas_ec.notifier);
  188. if (error) {
  189. dev_err(&pdev->dev, "cannot register notifier: %d\n", error);
  190. cbas_ec_set_input(NULL);
  191. return error;
  192. }
  193. device_init_wakeup(&pdev->dev, true);
  194. return 0;
  195. }
  196. static int cbas_ec_probe(struct platform_device *pdev)
  197. {
  198. int retval;
  199. mutex_lock(&cbas_ec_reglock);
  200. if (cbas_ec.input) {
  201. retval = -EBUSY;
  202. goto out;
  203. }
  204. retval = __cbas_ec_probe(pdev);
  205. out:
  206. mutex_unlock(&cbas_ec_reglock);
  207. return retval;
  208. }
  209. static void cbas_ec_remove(struct platform_device *pdev)
  210. {
  211. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  212. mutex_lock(&cbas_ec_reglock);
  213. blocking_notifier_chain_unregister(&ec->event_notifier,
  214. &cbas_ec.notifier);
  215. cbas_ec_set_input(NULL);
  216. mutex_unlock(&cbas_ec_reglock);
  217. }
  218. #ifdef CONFIG_ACPI
  219. static const struct acpi_device_id cbas_ec_acpi_ids[] = {
  220. { "GOOG000B", 0 },
  221. { }
  222. };
  223. MODULE_DEVICE_TABLE(acpi, cbas_ec_acpi_ids);
  224. #endif
  225. #ifdef CONFIG_OF
  226. static const struct of_device_id cbas_ec_of_match[] = {
  227. { .compatible = "google,cros-cbas" },
  228. { },
  229. };
  230. MODULE_DEVICE_TABLE(of, cbas_ec_of_match);
  231. #endif
  232. static struct platform_driver cbas_ec_driver = {
  233. .probe = cbas_ec_probe,
  234. .remove_new = cbas_ec_remove,
  235. .driver = {
  236. .name = "cbas_ec",
  237. .acpi_match_table = ACPI_PTR(cbas_ec_acpi_ids),
  238. .of_match_table = of_match_ptr(cbas_ec_of_match),
  239. .pm = &cbas_ec_pm_ops,
  240. },
  241. };
  242. #define MAX_BRIGHTNESS 100
  243. struct hammer_kbd_leds {
  244. struct led_classdev cdev;
  245. struct hid_device *hdev;
  246. u8 buf[2] ____cacheline_aligned;
  247. };
  248. static int hammer_kbd_brightness_set_blocking(struct led_classdev *cdev,
  249. enum led_brightness br)
  250. {
  251. struct hammer_kbd_leds *led = container_of(cdev,
  252. struct hammer_kbd_leds,
  253. cdev);
  254. int ret;
  255. led->buf[0] = 0;
  256. led->buf[1] = br;
  257. /*
  258. * Request USB HID device to be in Full On mode, so that sending
  259. * hardware output report and hardware raw request won't fail.
  260. */
  261. ret = hid_hw_power(led->hdev, PM_HINT_FULLON);
  262. if (ret < 0) {
  263. hid_err(led->hdev, "failed: device not resumed %d\n", ret);
  264. return ret;
  265. }
  266. ret = hid_hw_output_report(led->hdev, led->buf, sizeof(led->buf));
  267. if (ret == -ENOSYS)
  268. ret = hid_hw_raw_request(led->hdev, 0, led->buf,
  269. sizeof(led->buf),
  270. HID_OUTPUT_REPORT,
  271. HID_REQ_SET_REPORT);
  272. if (ret < 0)
  273. hid_err(led->hdev, "failed to set keyboard backlight: %d\n",
  274. ret);
  275. /* Request USB HID device back to Normal Mode. */
  276. hid_hw_power(led->hdev, PM_HINT_NORMAL);
  277. return ret;
  278. }
  279. static int hammer_register_leds(struct hid_device *hdev)
  280. {
  281. struct hammer_kbd_leds *kbd_backlight;
  282. kbd_backlight = devm_kzalloc(&hdev->dev, sizeof(*kbd_backlight),
  283. GFP_KERNEL);
  284. if (!kbd_backlight)
  285. return -ENOMEM;
  286. kbd_backlight->hdev = hdev;
  287. kbd_backlight->cdev.name = "hammer::kbd_backlight";
  288. kbd_backlight->cdev.max_brightness = MAX_BRIGHTNESS;
  289. kbd_backlight->cdev.brightness_set_blocking =
  290. hammer_kbd_brightness_set_blocking;
  291. kbd_backlight->cdev.flags = LED_HW_PLUGGABLE;
  292. /* Set backlight to 0% initially. */
  293. hammer_kbd_brightness_set_blocking(&kbd_backlight->cdev, 0);
  294. return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
  295. }
  296. #define HID_UP_GOOGLEVENDOR 0xffd10000
  297. #define HID_VD_KBD_FOLDED 0x00000019
  298. #define HID_USAGE_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED)
  299. /* HID usage for keyboard backlight (Alphanumeric display brightness) */
  300. #define HID_AD_BRIGHTNESS 0x00140046
  301. static int hammer_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  302. struct hid_field *field,
  303. struct hid_usage *usage,
  304. unsigned long **bit, int *max)
  305. {
  306. if (usage->hid == HID_USAGE_KBD_FOLDED) {
  307. /*
  308. * We do not want to have this usage mapped as it will get
  309. * mixed in with "base attached" signal and delivered over
  310. * separate input device for tablet switch mode.
  311. */
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. static void hammer_folded_event(struct hid_device *hdev, bool folded)
  317. {
  318. unsigned long flags;
  319. spin_lock_irqsave(&cbas_ec_lock, flags);
  320. /*
  321. * If we are getting events from Whiskers that means that it
  322. * is attached to the lid.
  323. */
  324. cbas_ec.base_present = true;
  325. cbas_ec.base_folded = folded;
  326. hid_dbg(hdev, "%s: base: %d, folded: %d\n", __func__,
  327. cbas_ec.base_present, cbas_ec.base_folded);
  328. if (cbas_ec.input) {
  329. input_report_switch(cbas_ec.input, SW_TABLET_MODE, folded);
  330. input_sync(cbas_ec.input);
  331. }
  332. spin_unlock_irqrestore(&cbas_ec_lock, flags);
  333. }
  334. static int hammer_event(struct hid_device *hid, struct hid_field *field,
  335. struct hid_usage *usage, __s32 value)
  336. {
  337. if (usage->hid == HID_USAGE_KBD_FOLDED) {
  338. hammer_folded_event(hid, value);
  339. return 1; /* We handled this event */
  340. }
  341. return 0;
  342. }
  343. static bool hammer_has_folded_event(struct hid_device *hdev)
  344. {
  345. return !!hid_find_field(hdev, HID_INPUT_REPORT,
  346. HID_GD_KEYBOARD, HID_USAGE_KBD_FOLDED);
  347. }
  348. static bool hammer_has_backlight_control(struct hid_device *hdev)
  349. {
  350. return !!hid_find_field(hdev, HID_OUTPUT_REPORT,
  351. HID_GD_KEYBOARD, HID_AD_BRIGHTNESS);
  352. }
  353. static void hammer_get_folded_state(struct hid_device *hdev)
  354. {
  355. struct hid_report *report;
  356. char *buf;
  357. int len, rlen;
  358. int a;
  359. report = hdev->report_enum[HID_INPUT_REPORT].report_id_hash[0x0];
  360. if (!report || report->maxfield < 1)
  361. return;
  362. len = hid_report_len(report) + 1;
  363. buf = kmalloc(len, GFP_KERNEL);
  364. if (!buf)
  365. return;
  366. rlen = hid_hw_raw_request(hdev, report->id, buf, len, report->type, HID_REQ_GET_REPORT);
  367. if (rlen != len) {
  368. hid_warn(hdev, "Unable to read base folded state: %d (expected %d)\n", rlen, len);
  369. goto out;
  370. }
  371. for (a = 0; a < report->maxfield; a++) {
  372. struct hid_field *field = report->field[a];
  373. if (field->usage->hid == HID_USAGE_KBD_FOLDED) {
  374. u32 value = hid_field_extract(hdev, buf+1,
  375. field->report_offset, field->report_size);
  376. hammer_folded_event(hdev, value);
  377. break;
  378. }
  379. }
  380. out:
  381. kfree(buf);
  382. }
  383. static void hammer_stop(void *hdev)
  384. {
  385. hid_hw_stop(hdev);
  386. }
  387. static int hammer_probe(struct hid_device *hdev,
  388. const struct hid_device_id *id)
  389. {
  390. struct vivaldi_data *vdata;
  391. int error;
  392. vdata = devm_kzalloc(&hdev->dev, sizeof(*vdata), GFP_KERNEL);
  393. if (!vdata)
  394. return -ENOMEM;
  395. hid_set_drvdata(hdev, vdata);
  396. error = hid_parse(hdev);
  397. if (error)
  398. return error;
  399. error = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  400. if (error)
  401. return error;
  402. error = devm_add_action(&hdev->dev, hammer_stop, hdev);
  403. if (error)
  404. return error;
  405. /*
  406. * We always want to poll for, and handle tablet mode events from
  407. * devices that have folded usage, even when nobody has opened the input
  408. * device. This also prevents the hid core from dropping early tablet
  409. * mode events from the device.
  410. */
  411. if (hammer_has_folded_event(hdev)) {
  412. hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
  413. error = hid_hw_open(hdev);
  414. if (error)
  415. return error;
  416. hammer_get_folded_state(hdev);
  417. }
  418. if (hammer_has_backlight_control(hdev)) {
  419. error = hammer_register_leds(hdev);
  420. if (error)
  421. hid_warn(hdev,
  422. "Failed to register keyboard backlight: %d\n",
  423. error);
  424. }
  425. return 0;
  426. }
  427. static void hammer_remove(struct hid_device *hdev)
  428. {
  429. unsigned long flags;
  430. if (hammer_has_folded_event(hdev)) {
  431. hid_hw_close(hdev);
  432. /*
  433. * If we are disconnecting then most likely Whiskers is
  434. * being removed. Even if it is not removed, without proper
  435. * keyboard we should not stay in clamshell mode.
  436. *
  437. * The reason for doing it here and not waiting for signal
  438. * from EC, is that on some devices there are high leakage
  439. * on Whiskers pins and we do not detect disconnect reliably,
  440. * resulting in devices being stuck in clamshell mode.
  441. */
  442. spin_lock_irqsave(&cbas_ec_lock, flags);
  443. if (cbas_ec.input && cbas_ec.base_present) {
  444. input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1);
  445. input_sync(cbas_ec.input);
  446. }
  447. cbas_ec.base_present = false;
  448. spin_unlock_irqrestore(&cbas_ec_lock, flags);
  449. }
  450. /* Unregistering LEDs and stopping the hardware is done via devm */
  451. }
  452. static const struct hid_device_id hammer_devices[] = {
  453. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  454. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_DON) },
  455. { HID_DEVICE(BUS_USB, HID_GROUP_VIVALDI,
  456. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_EEL) },
  457. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  458. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_HAMMER) },
  459. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  460. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_JEWEL) },
  461. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  462. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MAGNEMITE) },
  463. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  464. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MASTERBALL) },
  465. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  466. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MOONBALL) },
  467. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  468. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) },
  469. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  470. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) },
  471. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  472. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
  473. { }
  474. };
  475. MODULE_DEVICE_TABLE(hid, hammer_devices);
  476. static struct hid_driver hammer_driver = {
  477. .name = "hammer",
  478. .id_table = hammer_devices,
  479. .probe = hammer_probe,
  480. .remove = hammer_remove,
  481. .feature_mapping = vivaldi_feature_mapping,
  482. .input_mapping = hammer_input_mapping,
  483. .event = hammer_event,
  484. .driver = {
  485. .dev_groups = vivaldi_attribute_groups,
  486. },
  487. };
  488. static int __init hammer_init(void)
  489. {
  490. int error;
  491. error = platform_driver_register(&cbas_ec_driver);
  492. if (error)
  493. return error;
  494. error = hid_register_driver(&hammer_driver);
  495. if (error) {
  496. platform_driver_unregister(&cbas_ec_driver);
  497. return error;
  498. }
  499. return 0;
  500. }
  501. module_init(hammer_init);
  502. static void __exit hammer_exit(void)
  503. {
  504. hid_unregister_driver(&hammer_driver);
  505. platform_driver_unregister(&cbas_ec_driver);
  506. }
  507. module_exit(hammer_exit);
  508. MODULE_DESCRIPTION("HID driver for Google Hammer device.");
  509. MODULE_LICENSE("GPL");