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