// SPDX-License-Identifier: GPL-2.0+ /* * HID driver for Google Hammer device. * * Copyright (c) 2017 Google Inc. * Author: Wei-Ning Huang */ /* * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. */ #include #include #include #include #include #include #include #include #include #include #include "hid-ids.h" /* * C(hrome)B(ase)A(ttached)S(witch) - switch exported by Chrome EC and reporting * state of the "Whiskers" base - attached or detached. Whiskers USB device also * reports position of the keyboard - folded or not. Combining base state and * position allows us to generate proper "Tablet mode" events. */ struct cbas_ec { struct device *dev; /* The platform device (EC) */ struct input_dev *input; bool base_present; bool base_folded; struct notifier_block notifier; }; static struct cbas_ec cbas_ec; static DEFINE_SPINLOCK(cbas_ec_lock); static DEFINE_MUTEX(cbas_ec_reglock); static bool cbas_parse_base_state(const void *data) { u32 switches = get_unaligned_le32(data); return !!(switches & BIT(EC_MKBP_BASE_ATTACHED)); } static int cbas_ec_query_base(struct cros_ec_device *ec_dev, bool get_state, bool *state) { struct ec_params_mkbp_info *params; struct cros_ec_command *msg; int ret; msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)), GFP_KERNEL); if (!msg) return -ENOMEM; msg->command = EC_CMD_MKBP_INFO; msg->version = 1; msg->outsize = sizeof(*params); msg->insize = sizeof(u32); params = (struct ec_params_mkbp_info *)msg->data; params->info_type = get_state ? EC_MKBP_INFO_CURRENT : EC_MKBP_INFO_SUPPORTED; params->event_type = EC_MKBP_EVENT_SWITCH; ret = cros_ec_cmd_xfer_status(ec_dev, msg); if (ret >= 0) { if (ret != sizeof(u32)) { dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n", ret, sizeof(u32)); ret = -EPROTO; } else { *state = cbas_parse_base_state(msg->data); ret = 0; } } kfree(msg); return ret; } static int cbas_ec_notify(struct notifier_block *nb, unsigned long queued_during_suspend, void *_notify) { struct cros_ec_device *ec = _notify; unsigned long flags; bool base_present; if (ec->event_data.event_type == EC_MKBP_EVENT_SWITCH) { base_present = cbas_parse_base_state( &ec->event_data.data.switches); dev_dbg(cbas_ec.dev, "%s: base: %d\n", __func__, base_present); if (device_may_wakeup(cbas_ec.dev) || !queued_during_suspend) { pm_wakeup_event(cbas_ec.dev, 0); spin_lock_irqsave(&cbas_ec_lock, flags); /* * While input layer dedupes the events, we do not want * to disrupt the state reported by the base by * overriding it with state reported by the LID. Only * report changes, as we assume that on attach the base * is not folded. */ if (base_present != cbas_ec.base_present) { input_report_switch(cbas_ec.input, SW_TABLET_MODE, !base_present); input_sync(cbas_ec.input); cbas_ec.base_present = base_present; } spin_unlock_irqrestore(&cbas_ec_lock, flags); } } return NOTIFY_OK; } static __maybe_unused int cbas_ec_resume(struct device *dev) { struct cros_ec_device *ec = dev_get_drvdata(dev->parent); bool base_present; int error; error = cbas_ec_query_base(ec, true, &base_present); if (error) { dev_warn(dev, "failed to fetch base state on resume: %d\n", error); } else { spin_lock_irq(&cbas_ec_lock); cbas_ec.base_present = base_present; /* * Only report if base is disconnected. If base is connected, * it will resend its state on resume, and we'll update it * in hammer_event(). */ if (!cbas_ec.base_present) { input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1); input_sync(cbas_ec.input); } spin_unlock_irq(&cbas_ec_lock); } return 0; } static SIMPLE_DEV_PM_OPS(cbas_ec_pm_ops, NULL, cbas_ec_resume); static void cbas_ec_set_input(struct input_dev *input) { /* Take the lock so hammer_event() does not race with us here */ spin_lock_irq(&cbas_ec_lock); cbas_ec.input = input; spin_unlock_irq(&cbas_ec_lock); } static int __cbas_ec_probe(struct platform_device *pdev) { struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); struct input_dev *input; bool base_supported; int error; error = cbas_ec_query_base(ec, false, &base_supported); if (error) return error; if (!base_supported) return -ENXIO; input = devm_input_allocate_device(&pdev->dev); if (!input) return -ENOMEM; input->name = "Whiskers Tablet Mode Switch"; input->id.bustype = BUS_HOST; input_set_capability(input, EV_SW, SW_TABLET_MODE); error = input_register_device(input); if (error) { dev_err(&pdev->dev, "cannot register input device: %d\n", error); return error; } /* Seed the state */ error = cbas_ec_query_base(ec, true, &cbas_ec.base_present); if (error) { dev_err(&pdev->dev, "cannot query base state: %d\n", error); return error; } if (!cbas_ec.base_present) cbas_ec.base_folded = false; dev_dbg(&pdev->dev, "%s: base: %d, folded: %d\n", __func__, cbas_ec.base_present, cbas_ec.base_folded); input_report_switch(input, SW_TABLET_MODE, !cbas_ec.base_present || cbas_ec.base_folded); cbas_ec_set_input(input); cbas_ec.dev = &pdev->dev; cbas_ec.notifier.notifier_call = cbas_ec_notify; error = blocking_notifier_chain_register(&ec->event_notifier, &cbas_ec.notifier); if (error) { dev_err(&pdev->dev, "cannot register notifier: %d\n", error); cbas_ec_set_input(NULL); return error; } device_init_wakeup(&pdev->dev, true); return 0; } static int cbas_ec_probe(struct platform_device *pdev) { int retval; mutex_lock(&cbas_ec_reglock); if (cbas_ec.input) { retval = -EBUSY; goto out; } retval = __cbas_ec_probe(pdev); out: mutex_unlock(&cbas_ec_reglock); return retval; } static int cbas_ec_remove(struct platform_device *pdev) { struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); mutex_lock(&cbas_ec_reglock); blocking_notifier_chain_unregister(&ec->event_notifier, &cbas_ec.notifier); cbas_ec_set_input(NULL); mutex_unlock(&cbas_ec_reglock); return 0; } static const struct acpi_device_id cbas_ec_acpi_ids[] = { { "GOOG000B", 0 }, { } }; MODULE_DEVICE_TABLE(acpi, cbas_ec_acpi_ids); #ifdef CONFIG_OF static const struct of_device_id cbas_ec_of_match[] = { { .compatible = "google,cros-cbas" }, { }, }; MODULE_DEVICE_TABLE(of, cbas_ec_of_match); #endif static struct platform_driver cbas_ec_driver = { .probe = cbas_ec_probe, .remove = cbas_ec_remove, .driver = { .name = "cbas_ec", .acpi_match_table = ACPI_PTR(cbas_ec_acpi_ids), .of_match_table = of_match_ptr(cbas_ec_of_match), .pm = &cbas_ec_pm_ops, }, }; #define MAX_BRIGHTNESS 100 struct hammer_kbd_leds { struct led_classdev cdev; struct hid_device *hdev; u8 buf[2] ____cacheline_aligned; }; static int hammer_kbd_brightness_set_blocking(struct led_classdev *cdev, enum led_brightness br) { struct hammer_kbd_leds *led = contain