Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-microblaze.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 ACPI_BUTTON_CLASS               "button"
25 #define ACPI_BUTTON_FILE_STATE          "state"
26 #define ACPI_BUTTON_TYPE_UNKNOWN        0x00
27 #define ACPI_BUTTON_NOTIFY_STATUS       0x80
28
29 #define ACPI_BUTTON_SUBCLASS_POWER      "power"
30 #define ACPI_BUTTON_DEVICE_NAME_POWER   "Power Button"
31 #define ACPI_BUTTON_TYPE_POWER          0x01
32
33 #define ACPI_BUTTON_SUBCLASS_SLEEP      "sleep"
34 #define ACPI_BUTTON_DEVICE_NAME_SLEEP   "Sleep Button"
35 #define ACPI_BUTTON_TYPE_SLEEP          0x03
36
37 #define ACPI_BUTTON_SUBCLASS_LID        "lid"
38 #define ACPI_BUTTON_DEVICE_NAME_LID     "Lid Switch"
39 #define ACPI_BUTTON_TYPE_LID            0x05
40
41 enum {
42         ACPI_BUTTON_LID_INIT_IGNORE,
43         ACPI_BUTTON_LID_INIT_OPEN,
44         ACPI_BUTTON_LID_INIT_METHOD,
45         ACPI_BUTTON_LID_INIT_DISABLED,
46 };
47
48 static const char * const lid_init_state_str[] = {
49         [ACPI_BUTTON_LID_INIT_IGNORE]           = "ignore",
50         [ACPI_BUTTON_LID_INIT_OPEN]             = "open",
51         [ACPI_BUTTON_LID_INIT_METHOD]           = "method",
52         [ACPI_BUTTON_LID_INIT_DISABLED]         = "disabled",
53 };
54
55 MODULE_AUTHOR("Paul Diefenbaugh");
56 MODULE_DESCRIPTION("ACPI Button Driver");
57 MODULE_LICENSE("GPL");
58
59 static const struct acpi_device_id button_device_ids[] = {
60         {ACPI_BUTTON_HID_LID,    0},
61         {ACPI_BUTTON_HID_SLEEP,  0},
62         {ACPI_BUTTON_HID_SLEEPF, 0},
63         {ACPI_BUTTON_HID_POWER,  0},
64         {ACPI_BUTTON_HID_POWERF, 0},
65         {"", 0},
66 };
67 MODULE_DEVICE_TABLE(acpi, button_device_ids);
68
69 /* Please keep this list sorted alphabetically by vendor and model */
70 static const struct dmi_system_id dmi_lid_quirks[] = {
71         {
72                 /* GP-electronic T701, _LID method points to a floating GPIO */
73                 .matches = {
74                         DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
75                         DMI_MATCH(DMI_PRODUCT_NAME, "T701"),
76                         DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"),
77                 },
78                 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_DISABLED,
79         },
80         {
81                 /*
82                  * Medion Akoya E2215T, notification of the LID device only
83                  * happens on close, not on open and _LID always returns closed.
84                  */
85                 .matches = {
86                         DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
87                         DMI_MATCH(DMI_PRODUCT_NAME, "E2215T"),
88                 },
89                 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
90         },
91         {
92                 /*
93                  * Medion Akoya E2228T, notification of the LID device only
94                  * happens on close, not on open and _LID always returns closed.
95                  */
96                 .matches = {
97                         DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
98                         DMI_MATCH(DMI_PRODUCT_NAME, "E2228T"),
99                 },
100                 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
101         },
102         {
103                 /*
104                  * Razer Blade Stealth 13 late 2019, notification of the LID device
105                  * only happens on close, not on open and _LID always returns closed.
106                  */
107                 .matches = {
108                         DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
109                         DMI_MATCH(DMI_PRODUCT_NAME, "Razer Blade Stealth 13 Late 2019"),
110                 },
111                 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
112         },
113         {}
114 };
115
116 static int acpi_button_add(struct acpi_device *device);
117 static int acpi_button_remove(struct acpi_device *device);
118 static void acpi_button_notify(struct acpi_device *device, u32 event);
119
120 #ifdef CONFIG_PM_SLEEP
121 static int acpi_button_suspend(struct device *dev);
122 static int acpi_button_resume(struct device *dev);
123 #else
124 #define acpi_button_suspend NULL
125 #define acpi_button_resume NULL
126 #endif
127 static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
128
129 static struct acpi_driver acpi_button_driver = {
130         .name = "button",
131         .class = ACPI_BUTTON_CLASS,
132         .ids = button_device_ids,
133         .ops = {
134                 .add = acpi_button_add,
135                 .remove = acpi_button_remove,
136                 .notify = acpi_button_notify,
137         },
138         .drv.pm = &acpi_button_pm,
139 };
140
141 struct acpi_button {
142         unsigned int type;
143         struct input_dev *input;
144         char phys[32];                  /* for input device */
145         unsigned long pushed;
146         int last_state;
147         ktime_t last_time;
148         bool suspended;
149         bool lid_state_initialized;
150 };
151
152 static struct acpi_device *lid_device;
153 static long lid_init_state = -1;
154
155 static unsigned long lid_report_interval __read_mostly = 500;
156 module_param(lid_report_interval, ulong, 0644);
157 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
158
159 /* --------------------------------------------------------------------------
160                               FS Interface (/proc)
161    -------------------------------------------------------------------------- */
162
163 static struct proc_dir_entry *acpi_button_dir;
164 static struct proc_dir_entry *acpi_lid_dir;
165
166 static int acpi_lid_evaluate_state(struct acpi_device *device)
167 {
168         unsigned long long lid_state;
169         acpi_status status;
170
171         status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
172         if (ACPI_FAILURE(status))
173                 return -ENODEV;
174
175         return lid_state ? 1 : 0;
176 }
177
178 static int acpi_lid_notify_state(struct acpi_device *device, int state)
179 {
180         struct acpi_button *button = acpi_driver_data(device);
181         ktime_t next_report;
182         bool do_update;
183
184         /*
185          * In lid_init_state=ignore mode, if user opens/closes lid
186          * frequently with "open" missing, and "last_time" is also updated
187          * frequently, "close" cannot be delivered to the userspace.
188          * So "last_time" is only updated after a timeout or an actual
189          * switch.
190          */
191         if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
192             button->last_state != !!state)
193                 do_update = true;
194         else
195                 do_update = false;
196
197         next_report = ktime_add(button->last_time,
198                                 ms_to_ktime(lid_report_interval));
199         if (button->last_state == !!state &&
200             ktime_after(ktime_get(), next_report)) {
201                 /* Complain the buggy firmware */
202                 pr_warn_once("The lid device is not compliant to SW_LID.\n");
203
204                 /*
205                  * Send the unreliable complement switch event:
206                  *
207                  * On most platforms, the lid device is reliable. However
208                  * there are exceptions:
209                  * 1. Platforms returning initial lid state as "close" by
210                  *    default after booting/resuming:
211                  *     https://bugzilla.kernel.org/show_bug.cgi?id=89211
212                  *     https://bugzilla.kernel.org/show_bug.cgi?id=106151
213                  * 2. Platforms never reporting "open" events:
214                  *     https://bugzilla.kernel.org/show_bug.cgi?id=106941
215                  * On these buggy platforms, the usage model of the ACPI
216                  * lid device actually is:
217                  * 1. The initial returning value of _LID may not be
218                  *    reliable.
219                  * 2. The open event may not be reliable.
220                  * 3. The close event is reliable.
221                  *
222                  * But SW_LID is typed as input switch event, the input
223                  * layer checks if the event is redundant. Hence if the
224                  * state is not switched, the userspace cannot see this
225                  * platform triggered reliable event. By inserting a
226                  * complement switch event, it then is guaranteed that the
227                  * platform triggered reliable one can always be seen by
228                  * the userspace.
229                  */
230                 if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
231                         do_update = true;
232                         /*
233                          * Do generate complement switch event for "close"
234                          * as "close" is reliable and wrong "open" won't
235                          * trigger unexpected behaviors.
236                          * Do not generate complement switch event for
237                          * "open" as "open" is not reliable and wrong
238                          * "close" will trigger unexpected behaviors.
239                          */
240                         if (!state) {
241                                 input_report_switch(button->input,
242                                                     SW_LID, state);
243                                 input_sync(button->input);
244                         }
245                 }
246         }
247         /* Send the platform triggered reliable event */
248         if (do_update) {
249                 acpi_handle_debug(device->handle, "ACPI LID %s\n",
250                                   state ? "open" : "closed");
251                 input_report_switch(button->input, SW_LID, !state);
252                 input_sync(button->input);
253                 button->last_state = !!state;
254                 button->last_time = ktime_get();
255         }
256
257         return 0;
258 }
259
260 static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
261                                                      void *offset)
262 {
263         struct acpi_device *device = seq->private;
264         int state;
265
266         state = acpi_lid_evaluate_state(device);
267         seq_printf(seq, "state:      %s\n",
268                    state < 0 ? "unsupported" : (state ? "open" : "closed"));
269         return 0;
270 }
271
272 static int acpi_button_add_fs(struct acpi_device *device)
273 {
274         struct acpi_button *button = acpi_driver_data(device);
275         struct proc_dir_entry *entry = NULL;
276         int ret = 0;
277
278         /* procfs I/F for ACPI lid device only */
279         if (button->type != ACPI_BUTTON_TYPE_LID)
280                 return 0;
281
282         if (acpi_button_dir || acpi_lid_dir) {
283                 pr_info("More than one Lid device found!\n");
284                 return -EEXIST;
285         }
286
287         /* create /proc/acpi/button */
288         acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
289         if (!acpi_button_dir)
290                 return -ENODEV;
291
292         /* create /proc/acpi/button/lid */
293         acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
294         if (!acpi_lid_dir) {
295                 ret = -ENODEV;
296                 goto remove_button_dir;
297         }
298
299         /* create /proc/acpi/button/lid/LID/ */
300         acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
301         if (!acpi_device_dir(device)) {
302                 ret = -ENODEV;
303                 goto remove_lid_dir;
304         }
305
306         /* create /proc/acpi/button/lid/LID/state */
307         entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
308                         acpi_device_dir(device), acpi_button_state_seq_show,
309                         device);
310         if (!entry) {
311                 ret = -ENODEV;
312                 goto remove_dev_dir;
313         }
314
315 done:
316         return ret;
317
318 remove_dev_dir:
319         remove_proc_entry(acpi_device_bid(device),
320                           acpi_lid_dir);
321         acpi_device_dir(device) = NULL;
322 remove_lid_dir:
323         remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
324         acpi_lid_dir = NULL;
325 remove_button_dir:
326         remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
327         acpi_button_dir = NULL;
328         goto done;
329 }
330
331 static int acpi_button_remove_fs(struct acpi_device *device)
332 {
333         struct acpi_button *button = acpi_driver_data(device);
334
335         if (button->type != ACPI_BUTTON_TYPE_LID)
336                 return 0;
337
338         remove_proc_entry(ACPI_BUTTON_FILE_STATE,
339                           acpi_device_dir(device));
340         remove_proc_entry(acpi_device_bid(device),
341                           acpi_lid_dir);
342         acpi_device_dir(device) = NULL;
343         remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
344         acpi_lid_dir = NULL;
345         remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
346         acpi_button_dir = NULL;
347
348         return 0;
349 }
350
351 /* --------------------------------------------------------------------------
352                                 Driver Interface
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         struct acpi_button *button = acpi_driver_data(device);
381
382         switch (lid_init_state) {
383         case ACPI_BUTTON_LID_INIT_OPEN:
384                 (void)acpi_lid_notify_state(device, 1);
385                 break;
386         case ACPI_BUTTON_LID_INIT_METHOD:
387                 (void)acpi_lid_update_state(device, false);
388                 break;
389         case ACPI_BUTTON_LID_INIT_IGNORE:
390         default:
391                 break;
392         }
393
394         button->lid_state_initialized = true;
395 }
396
397 static void acpi_button_notify(struct acpi_device *device, u32 event)
398 {
399         struct acpi_button *button = acpi_driver_data(device);
400         struct input_dev *input;
401
402         switch (event) {
403         case ACPI_FIXED_HARDWARE_EVENT:
404                 event = ACPI_BUTTON_NOTIFY_STATUS;
405                 fallthrough;
406         case ACPI_BUTTON_NOTIFY_STATUS:
407                 input = button->input;
408                 if (button->type == ACPI_BUTTON_TYPE_LID) {
409                         if (button->lid_state_initialized)
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_handle_debug(device->handle, "Unsupported event [0x%x]\n",
433                                   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) {
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) &&
484              lid_init_state == ACPI_BUTTON_LID_INIT_DISABLED)
485                 return -ENODEV;
486
487         button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
488         if (!button)
489                 return -ENOMEM;
490
491         device->driver_data = button;
492
493         button->input = input = input_allocate_device();
494         if (!input) {
495                 error = -ENOMEM;
496                 goto err_free_button;
497         }
498
499         name = acpi_device_name(device);
500         class = acpi_device_class(device);
501
502         if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
503             !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
504                 button->type = ACPI_BUTTON_TYPE_POWER;
505                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
506                 sprintf(class, "%s/%s",
507                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
508         } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
509                    !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
510                 button->type = ACPI_BUTTON_TYPE_SLEEP;
511                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
512                 sprintf(class, "%s/%s",
513                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
514         } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
515                 button->type = ACPI_BUTTON_TYPE_LID;
516                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
517                 sprintf(class, "%s/%s",
518                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
519                 input->open = acpi_lid_input_open;
520         } else {
521                 pr_info("Unsupported hid [%s]\n", hid);
522                 error = -ENODEV;
523                 goto err_free_input;
524         }
525
526         error = acpi_button_add_fs(device);
527         if (error)
528                 goto err_free_input;
529
530         snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
531
532         input->name = name;
533         input->phys = button->phys;
534         input->id.bustype = BUS_HOST;
535         input->id.product = button->type;
536         input->dev.parent = &device->dev;
537
538         switch (button->type) {
539         case ACPI_BUTTON_TYPE_POWER:
540                 input_set_capability(input, EV_KEY, KEY_POWER);
541                 break;
542
543         case ACPI_BUTTON_TYPE_SLEEP:
544                 input_set_capability(input, EV_KEY, KEY_SLEEP);
545                 break;
546
547         case ACPI_BUTTON_TYPE_LID:
548                 input_set_capability(input, EV_SW, SW_LID);
549                 break;
550         }
551
552         input_set_drvdata(input, device);
553         error = input_register_device(input);
554         if (error)
555                 goto err_remove_fs;
556         if (button->type == ACPI_BUTTON_TYPE_LID) {
557                 /*
558                  * This assumes there's only one lid device, or if there are
559                  * more we only care about the last one...
560                  */
561                 lid_device = device;
562         }
563
564         device_init_wakeup(&device->dev, true);
565         pr_info("%s [%s]\n", name, acpi_device_bid(device));
566         return 0;
567
568  err_remove_fs:
569         acpi_button_remove_fs(device);
570  err_free_input:
571         input_free_device(input);
572  err_free_button:
573         kfree(button);
574         return error;
575 }
576
577 static int acpi_button_remove(struct acpi_device *device)
578 {
579         struct acpi_button *button = acpi_driver_data(device);
580
581         acpi_button_remove_fs(device);
582         input_unregister_device(button->input);
583         kfree(button);
584         return 0;
585 }
586
587 static int param_set_lid_init_state(const char *val,
588                                     const struct kernel_param *kp)
589 {
590         int i;
591
592         i = sysfs_match_string(lid_init_state_str, val);
593         if (i < 0)
594                 return i;
595
596         lid_init_state = i;
597         pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]);
598         return 0;
599 }
600
601 static int param_get_lid_init_state(char *buf, const struct kernel_param *kp)
602 {
603         int i, c = 0;
604
605         for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++)
606                 if (i == lid_init_state)
607                         c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]);
608                 else
609                         c += sprintf(buf + c, "%s ", lid_init_state_str[i]);
610
611         buf[c - 1] = '\n'; /* Replace the final space with a newline */
612
613         return c;
614 }
615
616 module_param_call(lid_init_state,
617                   param_set_lid_init_state, param_get_lid_init_state,
618                   NULL, 0644);
619 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
620
621 static int acpi_button_register_driver(struct acpi_driver *driver)
622 {
623         const struct dmi_system_id *dmi_id;
624
625         if (lid_init_state == -1) {
626                 dmi_id = dmi_first_match(dmi_lid_quirks);
627                 if (dmi_id)
628                         lid_init_state = (long)dmi_id->driver_data;
629                 else
630                         lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
631         }
632
633         /*
634          * Modules such as nouveau.ko and i915.ko have a link time dependency
635          * on acpi_lid_open(), and would therefore not be loadable on ACPI
636          * capable kernels booted in non-ACPI mode if the return value of
637          * acpi_bus_register_driver() is returned from here with ACPI disabled
638          * when this driver is built as a module.
639          */
640         if (acpi_disabled)
641                 return 0;
642
643         return acpi_bus_register_driver(driver);
644 }
645
646 static void acpi_button_unregister_driver(struct acpi_driver *driver)
647 {
648         if (!acpi_disabled)
649                 acpi_bus_unregister_driver(driver);
650 }
651
652 module_driver(acpi_button_driver, acpi_button_register_driver,
653                acpi_button_unregister_driver);