Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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