hid-google-hammer.c 14 KB

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