File Index
Files and directories
Sign in
FILE

quickstart.c

File preview with syntax highlighting.

linux-6.19.14/drivers/platform/x86/quickstart.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * ACPI Direct App Launch driver
  4 *
  5 * Copyright (C) 2024 Armin Wolf <W_Armin@gmx.de>
  6 * Copyright (C) 2022 Arvid Norlander <lkml@vorapal.se>
  7 * Copyright (C) 2007-2010 Angelo Arrifano <miknix@gmail.com>
  8 *
  9 * Information gathered from disassembled dsdt and from here:
 10 * <https://archive.org/details/microsoft-acpi-dirapplaunch>
 11 */
 12
 13#include <linux/acpi.h>
 14#include <linux/device.h>
 15#include <linux/errno.h>
 16#include <linux/init.h>
 17#include <linux/input.h>
 18#include <linux/input/sparse-keymap.h>
 19#include <linux/mod_devicetable.h>
 20#include <linux/module.h>
 21#include <linux/mutex.h>
 22#include <linux/platform_device.h>
 23#include <linux/printk.h>
 24#include <linux/slab.h>
 25#include <linux/sysfs.h>
 26#include <linux/types.h>
 27
 28#include <linux/unaligned.h>
 29
 30#define DRIVER_NAME	"quickstart"
 31
 32/*
 33 * There will be two events:
 34 * 0x02 - Button was pressed while device was off/sleeping.
 35 * 0x80 - Button was pressed while device was up.
 36 */
 37#define QUICKSTART_EVENT_RUNTIME	0x80
 38
 39struct quickstart_data {
 40	struct device *dev;
 41	struct mutex input_lock;	/* Protects input sequence during notify */
 42	struct input_dev *input_device;
 43	char input_name[32];
 44	char phys[32];
 45	u32 id;
 46};
 47
 48/*
 49 * Knowing what these buttons do require system specific knowledge.
 50 * This could be done by matching on DMI data in a long quirk table.
 51 * However, it is easier to leave it up to user space to figure this out.
 52 *
 53 * Using for example udev hwdb the scancode 0x1 can be remapped suitably.
 54 */
 55static const struct key_entry quickstart_keymap[] = {
 56	{ KE_KEY, 0x1, { KEY_UNKNOWN } },
 57	{ KE_END, 0 },
 58};
 59
 60static ssize_t button_id_show(struct device *dev, struct device_attribute *attr, char *buf)
 61{
 62	struct quickstart_data *data = dev_get_drvdata(dev);
 63
 64	return sysfs_emit(buf, "%u\n", data->id);
 65}
 66static DEVICE_ATTR_RO(button_id);
 67
 68static struct attribute *quickstart_attrs[] = {
 69	&dev_attr_button_id.attr,
 70	NULL
 71};
 72ATTRIBUTE_GROUPS(quickstart);
 73
 74static void quickstart_notify(acpi_handle handle, u32 event, void *context)
 75{
 76	struct quickstart_data *data = context;
 77
 78	switch (event) {
 79	case QUICKSTART_EVENT_RUNTIME:
 80		mutex_lock(&data->input_lock);
 81		sparse_keymap_report_event(data->input_device, 0x1, 1, true);
 82		mutex_unlock(&data->input_lock);
 83
 84		acpi_bus_generate_netlink_event(DRIVER_NAME, dev_name(data->dev), event, 0);
 85		break;
 86	default:
 87		dev_err(data->dev, FW_INFO "Unexpected ACPI notify event (%u)\n", event);
 88		break;
 89	}
 90}
 91
 92/*
 93 * The GHID ACPI method is used to indicate the "role" of the button.
 94 * However, all the meanings of these values are vendor defined.
 95 *
 96 * We do however expose this value to user space.
 97 */
 98static int quickstart_get_ghid(struct quickstart_data *data)
 99{
100	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
101	acpi_handle handle = ACPI_HANDLE(data->dev);
102	union acpi_object *obj;
103	acpi_status status;
104	int ret = 0;
105
106	/*
107	 * This returns a buffer telling the button usage ID,
108	 * and triggers pending notify events (The ones before booting).
109	 */
110	status = acpi_evaluate_object_typed(handle, "GHID", NULL, &buffer, ACPI_TYPE_BUFFER);
111	if (ACPI_FAILURE(status))
112		return -EIO;
113
114	obj = buffer.pointer;
115	if (!obj)
116		return -ENODATA;
117
118	/*
119	 * Quoting the specification:
120	 * "The GHID method can return a BYTE, WORD, or DWORD.
121	 *  The value must be encoded in little-endian byte
122	 *  order (least significant byte first)."
123	 */
124	switch (obj->buffer.length) {
125	case 1:
126		data->id = obj->buffer.pointer[0];
127		break;
128	case 2:
129		data->id = get_unaligned_le16(obj->buffer.pointer);
130		break;
131	case 4:
132		data->id = get_unaligned_le32(obj->buffer.pointer);
133		break;
134	default:
135		dev_err(data->dev,
136			FW_BUG "GHID method returned buffer of unexpected length %u\n",
137			obj->buffer.length);
138		ret = -EIO;
139		break;
140	}
141
142	kfree(obj);
143
144	return ret;
145}
146
147static void quickstart_notify_remove(void *context)
148{
149	struct quickstart_data *data = context;
150	acpi_handle handle;
151
152	handle = ACPI_HANDLE(data->dev);
153
154	acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, quickstart_notify);
155}
156
157static int quickstart_probe(struct platform_device *pdev)
158{
159	struct quickstart_data *data;
160	acpi_handle handle;
161	acpi_status status;
162	int ret;
163
164	handle = ACPI_HANDLE(&pdev->dev);
165	if (!handle)
166		return -ENODEV;
167
168	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
169	if (!data)
170		return -ENOMEM;
171
172	data->dev = &pdev->dev;
173	dev_set_drvdata(&pdev->dev, data);
174
175	ret = devm_mutex_init(&pdev->dev, &data->input_lock);
176	if (ret < 0)
177		return ret;
178
179	/*
180	 * We have to initialize the device wakeup before evaluating GHID because
181	 * doing so will notify the device if the button was used to wake the machine
182	 * from S5.
183	 */
184	device_init_wakeup(&pdev->dev, true);
185
186	ret = quickstart_get_ghid(data);
187	if (ret < 0)
188		return ret;
189
190	data->input_device = devm_input_allocate_device(&pdev->dev);
191	if (!data->input_device)
192		return -ENOMEM;
193
194	ret = sparse_keymap_setup(data->input_device, quickstart_keymap, NULL);
195	if (ret < 0)
196		return ret;
197
198	snprintf(data->input_name, sizeof(data->input_name), "Quickstart Button %u", data->id);
199	snprintf(data->phys, sizeof(data->phys), DRIVER_NAME "/input%u", data->id);
200
201	data->input_device->name = data->input_name;
202	data->input_device->phys = data->phys;
203	data->input_device->id.bustype = BUS_HOST;
204
205	ret = input_register_device(data->input_device);
206	if (ret < 0)
207		return ret;
208
209	status = acpi_install_notify_handler(handle, ACPI_DEVICE_NOTIFY, quickstart_notify, data);
210	if (ACPI_FAILURE(status))
211		return -EIO;
212
213	return devm_add_action_or_reset(&pdev->dev, quickstart_notify_remove, data);
214}
215
216static const struct acpi_device_id quickstart_device_ids[] = {
217	{ "PNP0C32" },
218	{ }
219};
220MODULE_DEVICE_TABLE(acpi, quickstart_device_ids);
221
222static struct platform_driver quickstart_platform_driver = {
223	.driver	= {
224		.name = DRIVER_NAME,
225		.dev_groups = quickstart_groups,
226		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
227		.acpi_match_table = quickstart_device_ids,
228	},
229	.probe = quickstart_probe,
230};
231module_platform_driver(quickstart_platform_driver);
232
233MODULE_AUTHOR("Armin Wolf <W_Armin@gmx.de>");
234MODULE_AUTHOR("Arvid Norlander <lkml@vorpal.se>");
235MODULE_AUTHOR("Angelo Arrifano");
236MODULE_DESCRIPTION("ACPI Direct App Launch driver");
237MODULE_LICENSE("GPL");
~/ Read-only file preview.