hid-google-hammer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 <[email protected]>
  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 <asm/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 int 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. return 0;
  218. }
  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. #ifdef CONFIG_OF
  225. static const struct of_device_id cbas_ec_of_match[] = {
  226. { .compatible = "google,cros-cbas" },
  227. { },
  228. };
  229. MODULE_DEVICE_TABLE(of, cbas_ec_of_match);
  230. #endif
  231. static struct platform_driver cbas_ec_driver = {
  232. .probe = cbas_ec_probe,
  233. .remove = cbas_ec_remove,
  234. .driver = {
  235. .name = "cbas_ec",
  236. .acpi_match_table = ACPI_PTR(cbas_ec_acpi_ids),
  237. .of_match_table = of_match_ptr(cbas_ec_of_match),
  238. .pm = &cbas_ec_pm_ops,
  239. },
  240. };
  241. #define MAX_BRIGHTNESS 100
  242. struct hammer_kbd_leds {
  243. struct led_classdev cdev;
  244. struct hid_device *hdev;
  245. u8 buf[2] ____cacheline_aligned;
  246. };
  247. static int hammer_kbd_brightness_set_blocking(struct led_classdev *cdev,
  248. enum led_brightness br)
  249. {
  250. struct hammer_kbd_leds *led = container_of(cdev,
  251. struct hammer_kbd_leds,
  252. cdev);
  253. int ret;
  254. led->buf[0] = 0;
  255. led->buf[1] = br;
  256. /*
  257. * Request USB HID device to be in Full On mode, so that sending
  258. * hardware output report and hardware raw request won't fail.
  259. */
  260. ret = hid_hw_power(led->hdev, PM_HINT_FULLON);
  261. if (ret < 0) {
  262. hid_err(led->hdev, "failed: device not resumed %d\n", ret);
  263. return ret;
  264. }
  265. ret = hid_hw_output_report(led->hdev, led->buf, sizeof(led->buf));
  266. if (ret == -ENOSYS)
  267. ret = hid_hw_raw_request(led->hdev, 0, led->buf,
  268. sizeof(led->buf),
  269. HID_OUTPUT_REPORT,
  270. HID_REQ_SET_REPORT);
  271. if (ret < 0)
  272. hid_err(led->hdev, "failed to set keyboard backlight: %d\n",
  273. ret);
  274. /* Request USB HID device back to Normal Mode. */
  275. hid_hw_power(led->hdev, PM_HINT_NORMAL);
  276. return ret;
  277. }
  278. static int hammer_register_leds(struct hid_device *hdev)
  279. {
  280. struct hammer_kbd_leds *kbd_backlight;
  281. kbd_backlight = devm_kzalloc(&hdev->dev, sizeof(*kbd_backlight),
  282. GFP_KERNEL);
  283. if (!kbd_backlight)
  284. return -ENOMEM;
  285. kbd_backlight->hdev = hdev;
  286. kbd_backlight->cdev.name = "hammer::kbd_backlight";
  287. kbd_backlight->cdev.max_brightness = MAX_BRIGHTNESS;
  288. kbd_backlight->cdev.brightness_set_blocking =
  289. hammer_kbd_brightness_set_blocking;
  290. kbd_backlight->cdev.flags = LED_HW_PLUGGABLE;
  291. /* Set backlight to 0% initially. */
  292. hammer_kbd_brightness_set_blocking(&kbd_backlight->cdev, 0);
  293. return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
  294. }
  295. #define HID_UP_GOOGLEVENDOR 0xffd10000
  296. #define HID_VD_KBD_FOLDED 0x00000019
  297. #define HID_USAGE_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED)
  298. /* HID usage for keyboard backlight (Alphanumeric display brightness) */
  299. #define HID_AD_BRIGHTNESS 0x00140046
  300. static int hammer_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  301. struct hid_field *field,
  302. struct hid_usage *usage,
  303. unsigned long **bit, int *max)
  304. {
  305. if (usage->hid == HID_USAGE_KBD_FOLDED) {
  306. /*
  307. * We do not want to have this usage mapped as it will get
  308. * mixed in with "base attached" signal and delivered over
  309. * separate input device for tablet switch mode.
  310. */
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. static void hammer_folded_event(struct hid_device *hdev, bool folded)
  316. {
  317. unsigned long flags;
  318. spin_lock_irqsave(&cbas_ec_lock, flags);
  319. /*
  320. * If we are getting events from Whiskers that means that it
  321. * is attached to the lid.
  322. */
  323. cbas_ec.base_present = true;
  324. cbas_ec.base_folded = folded;
  325. hid_dbg(hdev, "%s: base: %d, folded: %d\n", __func__,
  326. cbas_ec.base_present, cbas_ec.base_folded);
  327. if (cbas_ec.input) {
  328. input_report_switch(cbas_ec.input, SW_TABLET_MODE, folded);
  329. input_sync(cbas_ec.input);
  330. }
  331. spin_unlock_irqrestore(&cbas_ec_lock, flags);
  332. }
  333. static int hammer_event(struct hid_device *hid, struct hid_field *field,
  334. struct hid_usage *usage, __s32 value)
  335. {
  336. if (usage->hid == HID_USAGE_KBD_FOLDED) {
  337. hammer_folded_event(hid, value);
  338. return 1; /* We handled this event */
  339. }
  340. return 0;
  341. }
  342. static bool hammer_has_usage(struct hid_device *hdev, unsigned int report_type,
  343. unsigned application, unsigned usage)
  344. {
  345. struct hid_report_enum *re = &hdev->report_enum[report_type];
  346. struct hid_report *report;
  347. int i, j;
  348. list_for_each_entry(report, &re->report_list, list) {
  349. if (report->application != application)
  350. continue;
  351. for (i = 0; i < report->maxfield; i++) {
  352. struct hid_field *field = report->field[i];
  353. for (j = 0; j < field->maxusage; j++)
  354. if (field->usage[j].hid == usage)
  355. return true;
  356. }
  357. }
  358. return false;
  359. }
  360. static bool hammer_has_folded_event(struct hid_device *hdev)
  361. {
  362. return hammer_has_usage(hdev, HID_INPUT_REPORT,
  363. HID_GD_KEYBOARD, HID_USAGE_KBD_FOLDED);
  364. }
  365. static bool hammer_has_backlight_control(struct hid_device *hdev)
  366. {
  367. return hammer_has_usage(hdev, HID_OUTPUT_REPORT,
  368. HID_GD_KEYBOARD, HID_AD_BRIGHTNESS);
  369. }
  370. static void hammer_get_folded_state(struct hid_device *hdev)
  371. {
  372. struct hid_report *report;
  373. char *buf;
  374. int len, rlen;
  375. int a;
  376. report = hdev->report_enum[HID_INPUT_REPORT].report_id_hash[0x0];
  377. if (!report || report->maxfield < 1)
  378. return;
  379. len = hid_report_len(report) + 1;
  380. buf = kmalloc(len, GFP_KERNEL);
  381. if (!buf)
  382. return;
  383. rlen = hid_hw_raw_request(hdev, report->id, buf, len, report->type, HID_REQ_GET_REPORT);
  384. if (rlen != len) {
  385. hid_warn(hdev, "Unable to read base folded state: %d (expected %d)\n", rlen, len);
  386. goto out;
  387. }
  388. for (a = 0; a < report->maxfield; a++) {
  389. struct hid_field *field = report->field[a];
  390. if (field->usage->hid == HID_USAGE_KBD_FOLDED) {
  391. u32 value = hid_field_extract(hdev, buf+1,
  392. field->report_offset, field->report_size);
  393. hammer_folded_event(hdev, value);
  394. break;
  395. }
  396. }
  397. out:
  398. kfree(buf);
  399. }
  400. static void hammer_stop(void *hdev)
  401. {
  402. hid_hw_stop(hdev);
  403. }
  404. static int hammer_probe(struct hid_device *hdev,
  405. const struct hid_device_id *id)
  406. {
  407. struct vivaldi_data *vdata;
  408. int error;
  409. vdata = devm_kzalloc(&hdev->dev, sizeof(*vdata), GFP_KERNEL);
  410. if (!vdata)
  411. return -ENOMEM;
  412. hid_set_drvdata(hdev, vdata);
  413. error = hid_parse(hdev);
  414. if (error)
  415. return error;
  416. error = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  417. if (error)
  418. return error;
  419. error = devm_add_action(&hdev->dev, hammer_stop, hdev);
  420. if (error)
  421. return error;
  422. /*
  423. * We always want to poll for, and handle tablet mode events from
  424. * devices that have folded usage, even when nobody has opened the input
  425. * device. This also prevents the hid core from dropping early tablet
  426. * mode events from the device.
  427. */
  428. if (hammer_has_folded_event(hdev)) {
  429. hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
  430. error = hid_hw_open(hdev);
  431. if (error)
  432. return error;
  433. hammer_get_folded_state(hdev);
  434. }
  435. if (hammer_has_backlight_control(hdev)) {
  436. error = hammer_register_leds(hdev);
  437. if (error)
  438. hid_warn(hdev,
  439. "Failed to register keyboard backlight: %d\n",
  440. error);
  441. }
  442. return 0;
  443. }
  444. static void hammer_remove(struct hid_device *hdev)
  445. {
  446. unsigned long flags;
  447. if (hammer_has_folded_event(hdev)) {
  448. hid_hw_close(hdev);
  449. /*
  450. * If we are disconnecting then most likely Whiskers is
  451. * being removed. Even if it is not removed, without proper
  452. * keyboard we should not stay in clamshell mode.
  453. *
  454. * The reason for doing it here and not waiting for signal
  455. * from EC, is that on some devices there are high leakage
  456. * on Whiskers pins and we do not detect disconnect reliably,
  457. * resulting in devices being stuck in clamshell mode.
  458. */
  459. spin_lock_irqsave(&cbas_ec_lock, flags);
  460. if (cbas_ec.input && cbas_ec.base_present) {
  461. input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1);
  462. input_sync(cbas_ec.input);
  463. }
  464. cbas_ec.base_present = false;
  465. spin_unlock_irqrestore(&cbas_ec_lock, flags);
  466. }
  467. /* Unregistering LEDs and stopping the hardware is done via devm */
  468. }
  469. static const struct hid_device_id hammer_devices[] = {
  470. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  471. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_DON) },
  472. { HID_DEVICE(BUS_USB, HID_GROUP_VIVALDI,
  473. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_EEL) },
  474. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  475. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_HAMMER) },
  476. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  477. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_JEWEL) },
  478. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  479. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MAGNEMITE) },
  480. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  481. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MASTERBALL) },
  482. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  483. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MOONBALL) },
  484. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  485. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) },
  486. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  487. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) },
  488. { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
  489. USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
  490. { }
  491. };
  492. MODULE_DEVICE_TABLE(hid, hammer_devices);
  493. static struct hid_driver hammer_driver = {
  494. .name = "hammer",
  495. .id_table = hammer_devices,
  496. .probe = hammer_probe,
  497. .remove = hammer_remove,
  498. .feature_mapping = vivaldi_feature_mapping,
  499. .input_mapping = hammer_input_mapping,
  500. .event = hammer_event,
  501. .driver = {
  502. .dev_groups = vivaldi_attribute_groups,
  503. },
  504. };
  505. static int __init hammer_init(void)
  506. {
  507. int error;
  508. error = platform_driver_register(&cbas_ec_driver);
  509. if (error)
  510. return error;
  511. error = hid_register_driver(&hammer_driver);
  512. if (error) {
  513. platform_driver_unregister(&cbas_ec_driver);
  514. return error;
  515. }
  516. return 0;
  517. }
  518. module_init(hammer_init);
  519. static void __exit hammer_exit(void)
  520. {
  521. hid_unregister_driver(&hammer_driver);
  522. platform_driver_unregister(&cbas_ec_driver);
  523. }
  524. module_exit(hammer_exit);
  525. MODULE_LICENSE("GPL");