]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/acpi/button.c
ACPI: button: whitespace changes
[mirror_ubuntu-bionic-kernel.git] / drivers / acpi / button.c
CommitLineData
1da177e4 1/*
50a4da89 2 * button.c - ACPI Button Driver
1da177e4
LT
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
b34a8030
AS
29#include <linux/types.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
c0968f0e 32#include <linux/input.h>
1da177e4
LT
33#include <acpi/acpi_bus.h>
34#include <acpi/acpi_drivers.h>
35
1da177e4 36#define ACPI_BUTTON_CLASS "button"
b34a8030
AS
37#define ACPI_BUTTON_FILE_INFO "info"
38#define ACPI_BUTTON_FILE_STATE "state"
39#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
1da177e4
LT
40#define ACPI_BUTTON_NOTIFY_STATUS 0x80
41
42#define ACPI_BUTTON_SUBCLASS_POWER "power"
4be44fcd 43#define ACPI_BUTTON_HID_POWER "PNP0C0C"
1da177e4
LT
44#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
45#define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
46#define ACPI_BUTTON_TYPE_POWER 0x01
47#define ACPI_BUTTON_TYPE_POWERF 0x02
48
49#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
50#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
51#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
52#define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
53#define ACPI_BUTTON_TYPE_SLEEP 0x03
54#define ACPI_BUTTON_TYPE_SLEEPF 0x04
55
56#define ACPI_BUTTON_SUBCLASS_LID "lid"
57#define ACPI_BUTTON_HID_LID "PNP0C0D"
58#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
59#define ACPI_BUTTON_TYPE_LID 0x05
60
61#define _COMPONENT ACPI_BUTTON_COMPONENT
f52fd66d 62ACPI_MODULE_NAME("button");
1da177e4 63
c0968f0e 64MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 65MODULE_DESCRIPTION("ACPI Button Driver");
1da177e4
LT
66MODULE_LICENSE("GPL");
67
1ba90e3a
TR
68static const struct acpi_device_id button_device_ids[] = {
69 {ACPI_BUTTON_HID_LID, 0},
70 {ACPI_BUTTON_HID_SLEEP, 0},
71 {ACPI_BUTTON_HID_SLEEPF, 0},
72 {ACPI_BUTTON_HID_POWER, 0},
73 {ACPI_BUTTON_HID_POWERF, 0},
74 {"", 0},
75};
76MODULE_DEVICE_TABLE(acpi, button_device_ids);
77
4be44fcd
LB
78static int acpi_button_add(struct acpi_device *device);
79static int acpi_button_remove(struct acpi_device *device, int type);
23de5d9e 80static int acpi_button_resume(struct acpi_device *device);
373cfc36 81static void acpi_button_notify(struct acpi_device *device, u32 event);
b34a8030
AS
82static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
83static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
1da177e4
LT
84
85static struct acpi_driver acpi_button_driver = {
c2b6705b 86 .name = "button",
4be44fcd 87 .class = ACPI_BUTTON_CLASS,
1ba90e3a 88 .ids = button_device_ids,
4be44fcd
LB
89 .ops = {
90 .add = acpi_button_add,
23de5d9e 91 .resume = acpi_button_resume,
4be44fcd 92 .remove = acpi_button_remove,
373cfc36 93 .notify = acpi_button_notify,
c0968f0e 94 },
1da177e4
LT
95};
96
97struct acpi_button {
4be44fcd 98 struct acpi_device *device; /* Fixed button kludge */
c0968f0e
DT
99 unsigned int type;
100 struct input_dev *input;
101 char phys[32]; /* for input device */
4be44fcd 102 unsigned long pushed;
1da177e4
LT
103};
104
d7508032 105static const struct file_operations acpi_button_info_fops = {
cf7acfab 106 .owner = THIS_MODULE,
4be44fcd
LB
107 .open = acpi_button_info_open_fs,
108 .read = seq_read,
109 .llseek = seq_lseek,
110 .release = single_release,
b34a8030
AS
111};
112
d7508032 113static const struct file_operations acpi_button_state_fops = {
cf7acfab 114 .owner = THIS_MODULE,
4be44fcd
LB
115 .open = acpi_button_state_open_fs,
116 .read = seq_read,
117 .llseek = seq_lseek,
118 .release = single_release,
b34a8030 119};
4be44fcd 120
b34a8030
AS
121/* --------------------------------------------------------------------------
122 FS Interface (/proc)
123 -------------------------------------------------------------------------- */
124
4be44fcd 125static struct proc_dir_entry *acpi_button_dir;
b34a8030
AS
126
127static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
128{
c0968f0e 129 struct acpi_button *button = seq->private;
b34a8030
AS
130
131 if (!button || !button->device)
d550d98d 132 return 0;
b34a8030 133
4be44fcd
LB
134 seq_printf(seq, "type: %s\n",
135 acpi_device_name(button->device));
d550d98d 136 return 0;
b34a8030
AS
137}
138
139static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
140{
141 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
142}
4be44fcd 143
b34a8030
AS
144static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
145{
c0968f0e 146 struct acpi_button *button = seq->private;
4be44fcd 147 acpi_status status;
27663c58 148 unsigned long long state;
b34a8030 149
b34a8030 150 if (!button || !button->device)
d550d98d 151 return 0;
b34a8030 152
27b1d3e8 153 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
c0968f0e
DT
154 seq_printf(seq, "state: %s\n",
155 ACPI_FAILURE(status) ? "unsupported" :
156 (state ? "open" : "closed"));
d550d98d 157 return 0;
b34a8030
AS
158}
159
160static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
161{
162 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
163}
164
165static struct proc_dir_entry *acpi_power_dir;
166static struct proc_dir_entry *acpi_sleep_dir;
167static struct proc_dir_entry *acpi_lid_dir;
168
4be44fcd 169static int acpi_button_add_fs(struct acpi_device *device)
b34a8030 170{
4be44fcd 171 struct proc_dir_entry *entry = NULL;
c0968f0e 172 struct acpi_button *button;
b34a8030
AS
173
174 if (!device || !acpi_driver_data(device))
d550d98d 175 return -EINVAL;
b34a8030
AS
176
177 button = acpi_driver_data(device);
178
179 switch (button->type) {
180 case ACPI_BUTTON_TYPE_POWER:
181 case ACPI_BUTTON_TYPE_POWERF:
182 if (!acpi_power_dir)
4be44fcd
LB
183 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
184 acpi_button_dir);
b34a8030
AS
185 entry = acpi_power_dir;
186 break;
187 case ACPI_BUTTON_TYPE_SLEEP:
188 case ACPI_BUTTON_TYPE_SLEEPF:
189 if (!acpi_sleep_dir)
4be44fcd
LB
190 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
191 acpi_button_dir);
b34a8030
AS
192 entry = acpi_sleep_dir;
193 break;
194 case ACPI_BUTTON_TYPE_LID:
195 if (!acpi_lid_dir)
4be44fcd
LB
196 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
197 acpi_button_dir);
b34a8030
AS
198 entry = acpi_lid_dir;
199 break;
200 }
201
202 if (!entry)
d550d98d 203 return -ENODEV;
b34a8030
AS
204
205 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
206 if (!acpi_device_dir(device))
d550d98d 207 return -ENODEV;
b34a8030
AS
208
209 /* 'info' [R] */
cf7acfab
DL
210 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
211 S_IRUGO, acpi_device_dir(device),
212 &acpi_button_info_fops,
213 acpi_driver_data(device));
b34a8030 214 if (!entry)
d550d98d 215 return -ENODEV;
b34a8030
AS
216
217 /* show lid state [R] */
218 if (button->type == ACPI_BUTTON_TYPE_LID) {
cf7acfab
DL
219 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
220 S_IRUGO, acpi_device_dir(device),
221 &acpi_button_state_fops,
222 acpi_driver_data(device));
b34a8030 223 if (!entry)
a6fc6720 224 return -ENODEV;
b34a8030
AS
225 }
226
d550d98d 227 return 0;
b34a8030
AS
228}
229
4be44fcd 230static int acpi_button_remove_fs(struct acpi_device *device)
b34a8030 231{
c0968f0e 232 struct acpi_button *button = acpi_driver_data(device);
b34a8030 233
b34a8030
AS
234 if (acpi_device_dir(device)) {
235 if (button->type == ACPI_BUTTON_TYPE_LID)
236 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
4be44fcd 237 acpi_device_dir(device));
b34a8030 238 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
4be44fcd 239 acpi_device_dir(device));
b34a8030
AS
240
241 remove_proc_entry(acpi_device_bid(device),
4be44fcd 242 acpi_device_dir(device)->parent);
b34a8030
AS
243 acpi_device_dir(device) = NULL;
244 }
245
d550d98d 246 return 0;
b34a8030
AS
247}
248
1da177e4
LT
249/* --------------------------------------------------------------------------
250 Driver Interface
251 -------------------------------------------------------------------------- */
23de5d9e
AS
252static int acpi_lid_send_state(struct acpi_button *button)
253{
27663c58 254 unsigned long long state;
23de5d9e
AS
255 acpi_status status;
256
257 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
258 &state);
259 if (ACPI_FAILURE(status))
260 return -ENODEV;
50a4da89 261
23de5d9e
AS
262 /* input layer checks if event is redundant */
263 input_report_switch(button->input, SW_LID, !state);
df316e93 264 input_sync(button->input);
23de5d9e
AS
265 return 0;
266}
1da177e4 267
373cfc36 268static void acpi_button_notify(struct acpi_device *device, u32 event)
1da177e4 269{
373cfc36 270 struct acpi_button *button = acpi_driver_data(device);
c0968f0e 271 struct input_dev *input;
1da177e4
LT
272
273 if (!button || !button->device)
d550d98d 274 return;
1da177e4
LT
275
276 switch (event) {
373cfc36
BH
277 case ACPI_FIXED_HARDWARE_EVENT:
278 event = ACPI_BUTTON_NOTIFY_STATUS;
279 /* fall through */
1da177e4 280 case ACPI_BUTTON_NOTIFY_STATUS:
c0968f0e 281 input = button->input;
c0968f0e 282 if (button->type == ACPI_BUTTON_TYPE_LID) {
23de5d9e 283 acpi_lid_send_state(button);
c0968f0e
DT
284 } else {
285 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
286 KEY_SLEEP : KEY_POWER;
287
288 input_report_key(input, keycode, 1);
289 input_sync(input);
290 input_report_key(input, keycode, 0);
df316e93 291 input_sync(input);
c0968f0e 292 }
c0968f0e 293
14e04fb3 294 acpi_bus_generate_proc_event(button->device, event,
4be44fcd 295 ++button->pushed);
1da177e4
LT
296 break;
297 default:
298 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 299 "Unsupported event [0x%x]\n", event));
1da177e4
LT
300 break;
301 }
1da177e4
LT
302}
303
23de5d9e
AS
304static int acpi_button_resume(struct acpi_device *device)
305{
306 struct acpi_button *button;
50a4da89 307
23de5d9e
AS
308 if (!device)
309 return -EINVAL;
310 button = acpi_driver_data(device);
311 if (button && button->type == ACPI_BUTTON_TYPE_LID)
312 return acpi_lid_send_state(button);
313 return 0;
314}
315
c0968f0e
DT
316static int acpi_button_add(struct acpi_device *device)
317{
318 int error;
319 struct acpi_button *button;
320 struct input_dev *input;
1da177e4
LT
321
322 if (!device)
d550d98d 323 return -EINVAL;
1da177e4 324
c0968f0e 325 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
1da177e4 326 if (!button)
d550d98d 327 return -ENOMEM;
1da177e4
LT
328
329 button->device = device;
db89b4f0 330 device->driver_data = button;
1da177e4 331
c0968f0e
DT
332 button->input = input = input_allocate_device();
333 if (!input) {
334 error = -ENOMEM;
335 goto err_free_button;
336 }
337
1da177e4
LT
338 /*
339 * Determine the button type (via hid), as fixed-feature buttons
340 * need to be handled a bit differently than generic-space.
341 */
342 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
343 button->type = ACPI_BUTTON_TYPE_POWER;
4be44fcd
LB
344 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
345 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 346 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
4be44fcd 347 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
1da177e4
LT
348 button->type = ACPI_BUTTON_TYPE_POWERF;
349 strcpy(acpi_device_name(device),
4be44fcd
LB
350 ACPI_BUTTON_DEVICE_NAME_POWERF);
351 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 352 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
4be44fcd 353 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
1da177e4 354 button->type = ACPI_BUTTON_TYPE_SLEEP;
4be44fcd
LB
355 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
356 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 357 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
4be44fcd 358 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
1da177e4
LT
359 button->type = ACPI_BUTTON_TYPE_SLEEPF;
360 strcpy(acpi_device_name(device),
4be44fcd
LB
361 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
362 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 363 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
4be44fcd 364 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
1da177e4 365 button->type = ACPI_BUTTON_TYPE_LID;
4be44fcd
LB
366 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
367 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 368 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
4be44fcd 369 } else {
6468463a
LB
370 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
371 acpi_device_hid(device));
c0968f0e
DT
372 error = -ENODEV;
373 goto err_free_input;
1da177e4
LT
374 }
375
c0968f0e
DT
376 error = acpi_button_add_fs(device);
377 if (error)
378 goto err_free_input;
379
c0968f0e
DT
380 snprintf(button->phys, sizeof(button->phys),
381 "%s/button/input0", acpi_device_hid(device));
382
383 input->name = acpi_device_name(device);
384 input->phys = button->phys;
385 input->id.bustype = BUS_HOST;
386 input->id.product = button->type;
3b34e523 387 input->dev.parent = &device->dev;
b34a8030 388
1da177e4 389 switch (button->type) {
c0968f0e 390 case ACPI_BUTTON_TYPE_POWER:
1da177e4 391 case ACPI_BUTTON_TYPE_POWERF:
7b19ada2 392 input->evbit[0] = BIT_MASK(EV_KEY);
c0968f0e 393 set_bit(KEY_POWER, input->keybit);
1da177e4 394 break;
c0968f0e
DT
395
396 case ACPI_BUTTON_TYPE_SLEEP:
1da177e4 397 case ACPI_BUTTON_TYPE_SLEEPF:
7b19ada2 398 input->evbit[0] = BIT_MASK(EV_KEY);
c0968f0e 399 set_bit(KEY_SLEEP, input->keybit);
1da177e4 400 break;
c0968f0e
DT
401
402 case ACPI_BUTTON_TYPE_LID:
7b19ada2 403 input->evbit[0] = BIT_MASK(EV_SW);
c0968f0e 404 set_bit(SW_LID, input->swbit);
1da177e4
LT
405 break;
406 }
407
c0968f0e
DT
408 error = input_register_device(input);
409 if (error)
373cfc36 410 goto err_remove_fs;
23de5d9e
AS
411 if (button->type == ACPI_BUTTON_TYPE_LID)
412 acpi_lid_send_state(button);
1da177e4
LT
413
414 if (device->wakeup.flags.valid) {
415 /* Button's GPE is run-wake GPE */
4be44fcd
LB
416 acpi_set_gpe_type(device->wakeup.gpe_device,
417 device->wakeup.gpe_number,
418 ACPI_GPE_TYPE_WAKE_RUN);
419 acpi_enable_gpe(device->wakeup.gpe_device,
0b7084ac 420 device->wakeup.gpe_number);
1da177e4
LT
421 device->wakeup.state.enabled = 1;
422 }
423
4be44fcd
LB
424 printk(KERN_INFO PREFIX "%s [%s]\n",
425 acpi_device_name(device), acpi_device_bid(device));
c0968f0e 426 return 0;
1da177e4 427
c0968f0e
DT
428 err_remove_fs:
429 acpi_button_remove_fs(device);
430 err_free_input:
431 input_free_device(input);
432 err_free_button:
433 kfree(button);
434 return error;
1da177e4
LT
435}
436
4be44fcd 437static int acpi_button_remove(struct acpi_device *device, int type)
1da177e4 438{
c0968f0e 439 struct acpi_button *button;
1da177e4
LT
440
441 if (!device || !acpi_driver_data(device))
d550d98d 442 return -EINVAL;
1da177e4
LT
443
444 button = acpi_driver_data(device);
445
4be44fcd 446 acpi_button_remove_fs(device);
c0968f0e 447 input_unregister_device(button->input);
1da177e4 448 kfree(button);
d550d98d 449 return 0;
1da177e4
LT
450}
451
4be44fcd 452static int __init acpi_button_init(void)
1da177e4 453{
c0968f0e 454 int result;
1da177e4 455
b34a8030
AS
456 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
457 if (!acpi_button_dir)
d550d98d 458 return -ENODEV;
50a4da89 459
1da177e4
LT
460 result = acpi_bus_register_driver(&acpi_button_driver);
461 if (result < 0) {
b34a8030 462 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
d550d98d 463 return -ENODEV;
1da177e4
LT
464 }
465
d550d98d 466 return 0;
1da177e4
LT
467}
468
4be44fcd 469static void __exit acpi_button_exit(void)
1da177e4 470{
1da177e4
LT
471 acpi_bus_unregister_driver(&acpi_button_driver);
472
4be44fcd 473 if (acpi_power_dir)
b34a8030
AS
474 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
475 if (acpi_sleep_dir)
476 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
477 if (acpi_lid_dir)
478 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
479 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
1da177e4
LT
480}
481
1da177e4
LT
482module_init(acpi_button_init);
483module_exit(acpi_button_exit);