]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/acpi/button.c
ACPI: button: Refactor lid_init_state module parsing code
[linux.git] / drivers / acpi / button.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  button.c - ACPI Button Driver
4  *
5  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7  */
8
9 #define pr_fmt(fmt) "ACPI: button: " fmt
10
11 #include <linux/compiler.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/input.h>
19 #include <linux/slab.h>
20 #include <linux/acpi.h>
21 #include <linux/dmi.h>
22 #include <acpi/button.h>
23
24 #define PREFIX "ACPI: "
25
26 #define ACPI_BUTTON_CLASS               "button"
27 #define ACPI_BUTTON_FILE_INFO           "info"
28 #define ACPI_BUTTON_FILE_STATE          "state"
29 #define ACPI_BUTTON_TYPE_UNKNOWN        0x00
30 #define ACPI_BUTTON_NOTIFY_STATUS       0x80
31
32 #define ACPI_BUTTON_SUBCLASS_POWER      "power"
33 #define ACPI_BUTTON_HID_POWER           "PNP0C0C"
34 #define ACPI_BUTTON_DEVICE_NAME_POWER   "Power Button"
35 #define ACPI_BUTTON_TYPE_POWER          0x01
36
37 #define ACPI_BUTTON_SUBCLASS_SLEEP      "sleep"
38 #define ACPI_BUTTON_HID_SLEEP           "PNP0C0E"
39 #define ACPI_BUTTON_DEVICE_NAME_SLEEP   "Sleep Button"
40 #define ACPI_BUTTON_TYPE_SLEEP          0x03
41
42 #define ACPI_BUTTON_SUBCLASS_LID        "lid"
43 #define ACPI_BUTTON_HID_LID             "PNP0C0D"
44 #define ACPI_BUTTON_DEVICE_NAME_LID     "Lid Switch"
45 #define ACPI_BUTTON_TYPE_LID            0x05
46
47 enum {
48         ACPI_BUTTON_LID_INIT_IGNORE,
49         ACPI_BUTTON_LID_INIT_OPEN,
50         ACPI_BUTTON_LID_INIT_METHOD,
51 };
52
53 static const char * const lid_init_state_str[] = {
54         [ACPI_BUTTON_LID_INIT_IGNORE]           = "ignore",
55         [ACPI_BUTTON_LID_INIT_OPEN]             = "open",
56         [ACPI_BUTTON_LID_INIT_METHOD]           = "method",
57 };
58
59 #define _COMPONENT              ACPI_BUTTON_COMPONENT
60 ACPI_MODULE_NAME("button");
61
62 MODULE_AUTHOR("Paul Diefenbaugh");
63 MODULE_DESCRIPTION("ACPI Button Driver");
64 MODULE_LICENSE("GPL");
65
66 static const struct acpi_device_id button_device_ids[] = {
67         {ACPI_BUTTON_HID_LID,    0},
68         {ACPI_BUTTON_HID_SLEEP,  0},
69         {ACPI_BUTTON_HID_SLEEPF, 0},
70         {ACPI_BUTTON_HID_POWER,  0},
71         {ACPI_BUTTON_HID_POWERF, 0},
72         {"", 0},
73 };
74 MODULE_DEVICE_TABLE(acpi, button_device_ids);
75
76 /*
77  * Some devices which don't even have a lid in anyway have a broken _LID
78  * method (e.g. pointing to a floating gpio pin) causing spurious LID events.
79  */
80 static const struct dmi_system_id lid_blacklst[] = {
81         {
82                 /* GP-electronic T701 */
83                 .matches = {
84                         DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
85                         DMI_MATCH(DMI_PRODUCT_NAME, "T701"),
86                         DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"),
87                 },
88         },
89         {}
90 };
91
92 static int acpi_button_add(struct acpi_device *device);
93 static int acpi_button_remove(struct acpi_device *device);
94 static void acpi_button_notify(struct acpi_device *device, u32 event);
95
96 #ifdef CONFIG_PM_SLEEP
97 static int acpi_button_suspend(struct device *dev);
98 static int acpi_button_resume(struct device *dev);
99 #else
100 #define acpi_button_suspend NULL
101 #define acpi_button_resume NULL
102 #endif
103 static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
104
105 static struct acpi_driver acpi_button_driver = {
106         .name = "button",
107         .class = ACPI_BUTTON_CLASS,
108         .ids = button_device_ids,
109         .ops = {
110                 .add = acpi_button_add,
111                 .remove = acpi_button_remove,
112                 .notify = acpi_button_notify,
113         },
114         .drv.pm = &acpi_button_pm,
115 };
116
117 struct acpi_button {
118         unsigned int type;
119         struct input_dev *input;
120         char phys[32];                  /* for input device */
121         unsigned long pushed;
122         int last_state;
123         ktime_t last_time;
124         bool suspended;
125 };
126
127 static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
128 static struct acpi_device *lid_device;
129 static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
130
131 static unsigned long lid_report_interval __read_mostly = 500;
132 module_param(lid_report_interval, ulong, 0644);
133 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
134
135 /* --------------------------------------------------------------------------
136                               FS Interface (/proc)
137    -------------------------------------------------------------------------- */
138
139 static struct proc_dir_entry *acpi_button_dir;
140 static struct proc_dir_entry *acpi_lid_dir;
141
142 static int acpi_lid_evaluate_state(struct acpi_device *device)
143 {
144         unsigned long long lid_state;
145         acpi_status status;
146
147         status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
148         if (ACPI_FAILURE(status))
149                 return -ENODEV;
150
151         return lid_state ? 1 : 0;
152 }
153
154 static int acpi_lid_notify_state(struct acpi_device *device, int state)
155 {
156         struct acpi_button *button = acpi_driver_data(device);
157         int ret;
158         ktime_t next_report;
159         bool do_update;
160
161         /*
162          * In lid_init_state=ignore mode, if user opens/closes lid
163          * frequently with "open" missing, and "last_time" is also updated
164          * frequently, "close" cannot be delivered to the userspace.
165          * So "last_time" is only updated after a timeout or an actual
166          * switch.
167          */
168         if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
169             button->last_state != !!state)
170                 do_update = true;
171         else
172                 do_update = false;
173
174         next_report = ktime_add(button->last_time,
175                                 ms_to_ktime(lid_report_interval));
176         if (button->last_state == !!state &&
177             ktime_after(ktime_get(), next_report)) {
178                 /* Complain the buggy firmware */
179                 pr_warn_once("The lid device is not compliant to SW_LID.\n");
180
181                 /*
182                  * Send the unreliable complement switch event:
183                  *
184                  * On most platforms, the lid device is reliable. However
185                  * there are exceptions:
186                  * 1. Platforms returning initial lid state as "close" by
187                  *    default after booting/resuming:
188                  *     https://bugzilla.kernel.org/show_bug.cgi?id=89211
189                  *     https://bugzilla.kernel.org/show_bug.cgi?id=106151
190                  * 2. Platforms never reporting "open" events:
191                  *     https://bugzilla.kernel.org/show_bug.cgi?id=106941
192                  * On these buggy platforms, the usage model of the ACPI
193                  * lid device actually is:
194                  * 1. The initial returning value of _LID may not be
195                  *    reliable.
196                  * 2. The open event may not be reliable.
197                  * 3. The close event is reliable.
198                  *
199                  * But SW_LID is typed as input switch event, the input
200                  * layer checks if the event is redundant. Hence if the
201                  * state is not switched, the userspace cannot see this
202                  * platform triggered reliable event. By inserting a
203                  * complement switch event, it then is guaranteed that the
204                  * platform triggered reliable one can always be seen by
205                  * the userspace.
206                  */
207                 if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
208                         do_update = true;
209                         /*
210                          * Do generate complement switch event for "close"
211                          * as "close" is reliable and wrong "open" won't
212                          * trigger unexpected behaviors.
213                          * Do not generate complement switch event for
214                          * "open" as "open" is not reliable and wrong
215                          * "close" will trigger unexpected behaviors.
216                          */
217                         if (!state) {
218                                 input_report_switch(button->input,
219                                                     SW_LID, state);
220                                 input_sync(button->input);
221                         }
222                 }
223         }
224         /* Send the platform triggered reliable event */
225         if (do_update) {
226                 acpi_handle_debug(device->handle, "ACPI LID %s\n",
227                                   state ? "open" : "closed");
228                 input_report_switch(button->input, SW_LID, !state);
229                 input_sync(button->input);
230                 button->last_state = !!state;
231                 button->last_time = ktime_get();
232         }
233
234         ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
235         if (ret == NOTIFY_DONE)
236                 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
237                                                    device);
238         if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
239                 /*
240                  * It is also regarded as success if the notifier_chain
241                  * returns NOTIFY_OK or NOTIFY_DONE.
242                  */
243                 ret = 0;
244         }
245         return ret;
246 }
247
248 static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
249                                                      void *offset)
250 {
251         struct acpi_device *device = seq->private;
252         int state;
253
254         state = acpi_lid_evaluate_state(device);
255         seq_printf(seq, "state:      %s\n",
256                    state < 0 ? "unsupported" : (state ? "open" : "closed"));
257         return 0;
258 }
259
260 static int acpi_button_add_fs(struct acpi_device *device)
261 {
262         struct acpi_button *button = acpi_driver_data(device);
263         struct proc_dir_entry *entry = NULL;
264         int ret = 0;
265
266         /* procfs I/F for ACPI lid device only */
267         if (button->type != ACPI_BUTTON_TYPE_LID)
268                 return 0;
269
270         if (acpi_button_dir || acpi_lid_dir) {
271                 printk(KERN_ERR PREFIX "More than one Lid device found!\n");
272                 return -EEXIST;
273         }
274
275         /* create /proc/acpi/button */
276         acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
277         if (!acpi_button_dir)
278                 return -ENODEV;
279
280         /* create /proc/acpi/button/lid */
281         acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
282         if (!acpi_lid_dir) {
283                 ret = -ENODEV;
284                 goto remove_button_dir;
285         }
286
287         /* create /proc/acpi/button/lid/LID/ */
288         acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
289         if (!acpi_device_dir(device)) {
290                 ret = -ENODEV;
291                 goto remove_lid_dir;
292         }
293
294         /* create /proc/acpi/button/lid/LID/state */
295         entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
296                         acpi_device_dir(device), acpi_button_state_seq_show,
297                         device);
298         if (!entry) {
299                 ret = -ENODEV;
300                 goto remove_dev_dir;
301         }
302
303 done:
304         return ret;
305
306 remove_dev_dir:
307         remove_proc_entry(acpi_device_bid(device),
308                           acpi_lid_dir);
309         acpi_device_dir(device) = NULL;
310 remove_lid_dir:
311         remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
312         acpi_lid_dir = NULL;
313 remove_button_dir:
314         remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
315         acpi_button_dir = NULL;
316         goto done;
317 }
318
319 static int acpi_button_remove_fs(struct acpi_device *device)
320 {
321         struct acpi_button *button = acpi_driver_data(device);
322
323         if (button->type != ACPI_BUTTON_TYPE_LID)
324                 return 0;
325
326         remove_proc_entry(ACPI_BUTTON_FILE_STATE,
327                           acpi_device_dir(device));
328         remove_proc_entry(acpi_device_bid(device),
329                           acpi_lid_dir);
330         acpi_device_dir(device) = NULL;
331         remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
332         acpi_lid_dir = NULL;
333         remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
334         acpi_button_dir = NULL;
335
336         return 0;
337 }
338
339 /* --------------------------------------------------------------------------
340                                 Driver Interface
341    -------------------------------------------------------------------------- */
342 int acpi_lid_notifier_register(struct notifier_block *nb)
343 {
344         return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
345 }
346 EXPORT_SYMBOL(acpi_lid_notifier_register);
347
348 int acpi_lid_notifier_unregister(struct notifier_block *nb)
349 {
350         return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
351 }
352 EXPORT_SYMBOL(acpi_lid_notifier_unregister);
353
354 int acpi_lid_open(void)
355 {
356         if (!lid_device)
357                 return -ENODEV;
358
359         return acpi_lid_evaluate_state(lid_device);
360 }
361 EXPORT_SYMBOL(acpi_lid_open);
362
363 static int acpi_lid_update_state(struct acpi_device *device,
364                                  bool signal_wakeup)
365 {
366         int state;
367
368         state = acpi_lid_evaluate_state(device);
369         if (state < 0)
370                 return state;
371
372         if (state && signal_wakeup)
373                 acpi_pm_wakeup_event(&device->dev);
374
375         return acpi_lid_notify_state(device, state);
376 }
377
378 static void acpi_lid_initialize_state(struct acpi_device *device)
379 {
380         switch (lid_init_state) {
381         case ACPI_BUTTON_LID_INIT_OPEN:
382                 (void)acpi_lid_notify_state(device, 1);
383                 break;
384         case ACPI_BUTTON_LID_INIT_METHOD:
385                 (void)acpi_lid_update_state(device, false);
386                 break;
387         case ACPI_BUTTON_LID_INIT_IGNORE:
388         default:
389                 break;
390         }
391 }
392
393 static void acpi_button_notify(struct acpi_device *device, u32 event)
394 {
395         struct acpi_button *button = acpi_driver_data(device);
396         struct input_dev *input;
397         int users;
398
399         switch (event) {
400         case ACPI_FIXED_HARDWARE_EVENT:
401                 event = ACPI_BUTTON_NOTIFY_STATUS;
402                 /* fall through */
403         case ACPI_BUTTON_NOTIFY_STATUS:
404                 input = button->input;
405                 if (button->type == ACPI_BUTTON_TYPE_LID) {
406                         mutex_lock(&button->input->mutex);
407                         users = button->input->users;
408                         mutex_unlock(&button->input->mutex);
409                         if (users)
410                                 acpi_lid_update_state(device, true);
411                 } else {
412                         int keycode;
413
414                         acpi_pm_wakeup_event(&device->dev);
415                         if (button->suspended)
416                                 break;
417
418                         keycode = test_bit(KEY_SLEEP, input->keybit) ?
419                                                 KEY_SLEEP : KEY_POWER;
420                         input_report_key(input, keycode, 1);
421                         input_sync(input);
422                         input_report_key(input, keycode, 0);
423                         input_sync(input);
424
425                         acpi_bus_generate_netlink_event(
426                                         device->pnp.device_class,
427                                         dev_name(&device->dev),
428                                         event, ++button->pushed);
429                 }
430                 break;
431         default:
432                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
433                                   "Unsupported event [0x%x]\n", event));
434                 break;
435         }
436 }
437
438 #ifdef CONFIG_PM_SLEEP
439 static int acpi_button_suspend(struct device *dev)
440 {
441         struct acpi_device *device = to_acpi_device(dev);
442         struct acpi_button *button = acpi_driver_data(device);
443
444         button->suspended = true;
445         return 0;
446 }
447
448 static int acpi_button_resume(struct device *dev)
449 {
450         struct acpi_device *device = to_acpi_device(dev);
451         struct acpi_button *button = acpi_driver_data(device);
452
453         button->suspended = false;
454         if (button->type == ACPI_BUTTON_TYPE_LID && button->input->users) {
455                 button->last_state = !!acpi_lid_evaluate_state(device);
456                 button->last_time = ktime_get();
457                 acpi_lid_initialize_state(device);
458         }
459         return 0;
460 }
461 #endif
462
463 static int acpi_lid_input_open(struct input_dev *input)
464 {
465         struct acpi_device *device = input_get_drvdata(input);
466         struct acpi_button *button = acpi_driver_data(device);
467
468         button->last_state = !!acpi_lid_evaluate_state(device);
469         button->last_time = ktime_get();
470         acpi_lid_initialize_state(device);
471
472         return 0;
473 }
474
475 static int acpi_button_add(struct acpi_device *device)
476 {
477         struct acpi_button *button;
478         struct input_dev *input;
479         const char *hid = acpi_device_hid(device);
480         char *name, *class;
481         int error;
482
483         if (!strcmp(hid, ACPI_BUTTON_HID_LID) && dmi_check_system(lid_blacklst))
484                 return -ENODEV;
485
486         button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
487         if (!button)
488                 return -ENOMEM;
489
490         device->driver_data = button;
491
492         button->input = input = input_allocate_device();
493         if (!input) {
494                 error = -ENOMEM;
495                 goto err_free_button;
496         }
497
498         name = acpi_device_name(device);
499         class = acpi_device_class(device);
500
501         if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
502             !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
503                 button->type = ACPI_BUTTON_TYPE_POWER;
504                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
505                 sprintf(class, "%s/%s",
506                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
507         } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
508                    !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
509                 button->type = ACPI_BUTTON_TYPE_SLEEP;
510                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
511                 sprintf(class, "%s/%s",
512                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
513         } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
514                 button->type = ACPI_BUTTON_TYPE_LID;
515                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
516                 sprintf(class, "%s/%s",
517                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
518                 input->open = acpi_lid_input_open;
519         } else {
520                 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
521                 error = -ENODEV;
522                 goto err_free_input;
523         }
524
525         error = acpi_button_add_fs(device);
526         if (error)
527                 goto err_free_input;
528
529         snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
530
531         input->name = name;
532         input->phys = button->phys;
533         input->id.bustype = BUS_HOST;
534         input->id.product = button->type;
535         input->dev.parent = &device->dev;
536
537         switch (button->type) {
538         case ACPI_BUTTON_TYPE_POWER:
539                 input_set_capability(input, EV_KEY, KEY_POWER);
540                 break;
541
542         case ACPI_BUTTON_TYPE_SLEEP:
543                 input_set_capability(input, EV_KEY, KEY_SLEEP);
544                 break;
545
546         case ACPI_BUTTON_TYPE_LID:
547                 input_set_capability(input, EV_SW, SW_LID);
548                 break;
549         }
550
551         input_set_drvdata(input, device);
552         error = input_register_device(input);
553         if (error)
554                 goto err_remove_fs;
555         if (button->type == ACPI_BUTTON_TYPE_LID) {
556                 /*
557                  * This assumes there's only one lid device, or if there are
558                  * more we only care about the last one...
559                  */
560                 lid_device = device;
561         }
562
563         device_init_wakeup(&device->dev, true);
564         printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
565         return 0;
566
567  err_remove_fs:
568         acpi_button_remove_fs(device);
569  err_free_input:
570         input_free_device(input);
571  err_free_button:
572         kfree(button);
573         return error;
574 }
575
576 static int acpi_button_remove(struct acpi_device *device)
577 {
578         struct acpi_button *button = acpi_driver_data(device);
579
580         acpi_button_remove_fs(device);
581         input_unregister_device(button->input);
582         kfree(button);
583         return 0;
584 }
585
586 static int param_set_lid_init_state(const char *val,
587                                     const struct kernel_param *kp)
588 {
589         int i;
590
591         i = sysfs_match_string(lid_init_state_str, val);
592         if (i < 0)
593                 return i;
594
595         lid_init_state = i;
596         pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]);
597         return 0;
598 }
599
600 static int param_get_lid_init_state(char *buf, const struct kernel_param *kp)
601 {
602         int i, c = 0;
603
604         for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++)
605                 if (i == lid_init_state)
606                         c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]);
607                 else
608                         c += sprintf(buf + c, "%s ", lid_init_state_str[i]);
609
610         buf[c - 1] = '\n'; /* Replace the final space with a newline */
611
612         return c;
613 }
614
615 module_param_call(lid_init_state,
616                   param_set_lid_init_state, param_get_lid_init_state,
617                   NULL, 0644);
618 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
619
620 static int acpi_button_register_driver(struct acpi_driver *driver)
621 {
622         /*
623          * Modules such as nouveau.ko and i915.ko have a link time dependency
624          * on acpi_lid_open(), and would therefore not be loadable on ACPI
625          * capable kernels booted in non-ACPI mode if the return value of
626          * acpi_bus_register_driver() is returned from here with ACPI disabled
627          * when this driver is built as a module.
628          */
629         if (acpi_disabled)
630                 return 0;
631
632         return acpi_bus_register_driver(driver);
633 }
634
635 static void acpi_button_unregister_driver(struct acpi_driver *driver)
636 {
637         if (!acpi_disabled)
638                 acpi_bus_unregister_driver(driver);
639 }
640
641 module_driver(acpi_button_driver, acpi_button_register_driver,
642                acpi_button_unregister_driver);