1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ACPI Direct App Launch driver
4  *
5  * Copyright (C) 2024 Armin Wolf <[email protected]>
6  * Copyright (C) 2022 Arvid Norlander <[email protected]>
7  * Copyright (C) 2007-2010 Angelo Arrifano <[email protected]>
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 
39 struct 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  */
55 static const struct key_entry quickstart_keymap[] = {
56 	{ KE_KEY, 0x1, { KEY_UNKNOWN } },
57 	{ KE_END, 0 },
58 };
59 
button_id_show(struct device * dev,struct device_attribute * attr,char * buf)60 static 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 }
66 static DEVICE_ATTR_RO(button_id);
67 
68 static struct attribute *quickstart_attrs[] = {
69 	&dev_attr_button_id.attr,
70 	NULL
71 };
72 ATTRIBUTE_GROUPS(quickstart);
73 
quickstart_notify(acpi_handle handle,u32 event,void * context)74 static 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  */
quickstart_get_ghid(struct quickstart_data * data)98 static 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 
quickstart_notify_remove(void * context)147 static 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 
quickstart_mutex_destroy(void * data)157 static void quickstart_mutex_destroy(void *data)
158 {
159 	struct mutex *lock = data;
160 
161 	mutex_destroy(lock);
162 }
163 
quickstart_probe(struct platform_device * pdev)164 static int quickstart_probe(struct platform_device *pdev)
165 {
166 	struct quickstart_data *data;
167 	acpi_handle handle;
168 	acpi_status status;
169 	int ret;
170 
171 	handle = ACPI_HANDLE(&pdev->dev);
172 	if (!handle)
173 		return -ENODEV;
174 
175 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
176 	if (!data)
177 		return -ENOMEM;
178 
179 	data->dev = &pdev->dev;
180 	dev_set_drvdata(&pdev->dev, data);
181 
182 	mutex_init(&data->input_lock);
183 	ret = devm_add_action_or_reset(&pdev->dev, quickstart_mutex_destroy, &data->input_lock);
184 	if (ret < 0)
185 		return ret;
186 
187 	/*
188 	 * We have to initialize the device wakeup before evaluating GHID because
189 	 * doing so will notify the device if the button was used to wake the machine
190 	 * from S5.
191 	 */
192 	device_init_wakeup(&pdev->dev, true);
193 
194 	ret = quickstart_get_ghid(data);
195 	if (ret < 0)
196 		return ret;
197 
198 	data->input_device = devm_input_allocate_device(&pdev->dev);
199 	if (!data->input_device)
200 		return -ENOMEM;
201 
202 	ret = sparse_keymap_setup(data->input_device, quickstart_keymap, NULL);
203 	if (ret < 0)
204 		return ret;
205 
206 	snprintf(data->input_name, sizeof(data->input_name), "Quickstart Button %u", data->id);
207 	snprintf(data->phys, sizeof(data->phys), DRIVER_NAME "/input%u", data->id);
208 
209 	data->input_device->name = data->input_name;
210 	data->input_device->phys = data->phys;
211 	data->input_device->id.bustype = BUS_HOST;
212 
213 	ret = input_register_device(data->input_device);
214 	if (ret < 0)
215 		return ret;
216 
217 	status = acpi_install_notify_handler(handle, ACPI_DEVICE_NOTIFY, quickstart_notify, data);
218 	if (ACPI_FAILURE(status))
219 		return -EIO;
220 
221 	return devm_add_action_or_reset(&pdev->dev, quickstart_notify_remove, data);
222 }
223 
224 static const struct acpi_device_id quickstart_device_ids[] = {
225 	{ "PNP0C32" },
226 	{ }
227 };
228 MODULE_DEVICE_TABLE(acpi, quickstart_device_ids);
229 
230 static struct platform_driver quickstart_platform_driver = {
231 	.driver	= {
232 		.name = DRIVER_NAME,
233 		.dev_groups = quickstart_groups,
234 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
235 		.acpi_match_table = quickstart_device_ids,
236 	},
237 	.probe = quickstart_probe,
238 };
239 module_platform_driver(quickstart_platform_driver);
240 
241 MODULE_AUTHOR("Armin Wolf <[email protected]>");
242 MODULE_AUTHOR("Arvid Norlander <[email protected]>");
243 MODULE_AUTHOR("Angelo Arrifano");
244 MODULE_DESCRIPTION("ACPI Direct App Launch driver");
245 MODULE_LICENSE("GPL");
246