ACPI: button: remove unnecessary null pointer checks
[GitHub/mt8127/android_kernel_alcatel_ttab.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 130
4be44fcd
LB
131 seq_printf(seq, "type: %s\n",
132 acpi_device_name(button->device));
d550d98d 133 return 0;
b34a8030
AS
134}
135
136static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
137{
138 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
139}
4be44fcd 140
b34a8030
AS
141static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
142{
c0968f0e 143 struct acpi_button *button = seq->private;
4be44fcd 144 acpi_status status;
27663c58 145 unsigned long long state;
b34a8030 146
27b1d3e8 147 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
c0968f0e
DT
148 seq_printf(seq, "state: %s\n",
149 ACPI_FAILURE(status) ? "unsupported" :
150 (state ? "open" : "closed"));
d550d98d 151 return 0;
b34a8030
AS
152}
153
154static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
155{
156 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
157}
158
159static struct proc_dir_entry *acpi_power_dir;
160static struct proc_dir_entry *acpi_sleep_dir;
161static struct proc_dir_entry *acpi_lid_dir;
162
4be44fcd 163static int acpi_button_add_fs(struct acpi_device *device)
b34a8030 164{
4be44fcd 165 struct proc_dir_entry *entry = NULL;
c0968f0e 166 struct acpi_button *button;
b34a8030 167
b34a8030
AS
168 button = acpi_driver_data(device);
169
170 switch (button->type) {
171 case ACPI_BUTTON_TYPE_POWER:
172 case ACPI_BUTTON_TYPE_POWERF:
173 if (!acpi_power_dir)
4be44fcd
LB
174 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
175 acpi_button_dir);
b34a8030
AS
176 entry = acpi_power_dir;
177 break;
178 case ACPI_BUTTON_TYPE_SLEEP:
179 case ACPI_BUTTON_TYPE_SLEEPF:
180 if (!acpi_sleep_dir)
4be44fcd
LB
181 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
182 acpi_button_dir);
b34a8030
AS
183 entry = acpi_sleep_dir;
184 break;
185 case ACPI_BUTTON_TYPE_LID:
186 if (!acpi_lid_dir)
4be44fcd
LB
187 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
188 acpi_button_dir);
b34a8030
AS
189 entry = acpi_lid_dir;
190 break;
191 }
192
193 if (!entry)
d550d98d 194 return -ENODEV;
b34a8030
AS
195
196 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
197 if (!acpi_device_dir(device))
d550d98d 198 return -ENODEV;
b34a8030
AS
199
200 /* 'info' [R] */
cf7acfab
DL
201 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
202 S_IRUGO, acpi_device_dir(device),
203 &acpi_button_info_fops,
204 acpi_driver_data(device));
b34a8030 205 if (!entry)
d550d98d 206 return -ENODEV;
b34a8030
AS
207
208 /* show lid state [R] */
209 if (button->type == ACPI_BUTTON_TYPE_LID) {
cf7acfab
DL
210 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
211 S_IRUGO, acpi_device_dir(device),
212 &acpi_button_state_fops,
213 acpi_driver_data(device));
b34a8030 214 if (!entry)
a6fc6720 215 return -ENODEV;
b34a8030
AS
216 }
217
d550d98d 218 return 0;
b34a8030
AS
219}
220
4be44fcd 221static int acpi_button_remove_fs(struct acpi_device *device)
b34a8030 222{
c0968f0e 223 struct acpi_button *button = acpi_driver_data(device);
b34a8030 224
b34a8030
AS
225 if (acpi_device_dir(device)) {
226 if (button->type == ACPI_BUTTON_TYPE_LID)
227 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
4be44fcd 228 acpi_device_dir(device));
b34a8030 229 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
4be44fcd 230 acpi_device_dir(device));
b34a8030
AS
231
232 remove_proc_entry(acpi_device_bid(device),
4be44fcd 233 acpi_device_dir(device)->parent);
b34a8030
AS
234 acpi_device_dir(device) = NULL;
235 }
236
d550d98d 237 return 0;
b34a8030
AS
238}
239
1da177e4
LT
240/* --------------------------------------------------------------------------
241 Driver Interface
242 -------------------------------------------------------------------------- */
23de5d9e
AS
243static int acpi_lid_send_state(struct acpi_button *button)
244{
27663c58 245 unsigned long long state;
23de5d9e
AS
246 acpi_status status;
247
248 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
249 &state);
250 if (ACPI_FAILURE(status))
251 return -ENODEV;
50a4da89 252
23de5d9e
AS
253 /* input layer checks if event is redundant */
254 input_report_switch(button->input, SW_LID, !state);
df316e93 255 input_sync(button->input);
23de5d9e
AS
256 return 0;
257}
1da177e4 258
373cfc36 259static void acpi_button_notify(struct acpi_device *device, u32 event)
1da177e4 260{
373cfc36 261 struct acpi_button *button = acpi_driver_data(device);
c0968f0e 262 struct input_dev *input;
1da177e4 263
1da177e4 264 switch (event) {
373cfc36
BH
265 case ACPI_FIXED_HARDWARE_EVENT:
266 event = ACPI_BUTTON_NOTIFY_STATUS;
267 /* fall through */
1da177e4 268 case ACPI_BUTTON_NOTIFY_STATUS:
c0968f0e 269 input = button->input;
c0968f0e 270 if (button->type == ACPI_BUTTON_TYPE_LID) {
23de5d9e 271 acpi_lid_send_state(button);
c0968f0e
DT
272 } else {
273 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
274 KEY_SLEEP : KEY_POWER;
275
276 input_report_key(input, keycode, 1);
277 input_sync(input);
278 input_report_key(input, keycode, 0);
df316e93 279 input_sync(input);
c0968f0e 280 }
c0968f0e 281
14e04fb3 282 acpi_bus_generate_proc_event(button->device, event,
4be44fcd 283 ++button->pushed);
1da177e4
LT
284 break;
285 default:
286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 287 "Unsupported event [0x%x]\n", event));
1da177e4
LT
288 break;
289 }
1da177e4
LT
290}
291
23de5d9e
AS
292static int acpi_button_resume(struct acpi_device *device)
293{
294 struct acpi_button *button;
50a4da89 295
23de5d9e 296 button = acpi_driver_data(device);
e2fb9754 297 if (button->type == ACPI_BUTTON_TYPE_LID)
23de5d9e
AS
298 return acpi_lid_send_state(button);
299 return 0;
300}
301
c0968f0e
DT
302static int acpi_button_add(struct acpi_device *device)
303{
304 int error;
305 struct acpi_button *button;
306 struct input_dev *input;
1da177e4 307
c0968f0e 308 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
1da177e4 309 if (!button)
d550d98d 310 return -ENOMEM;
1da177e4
LT
311
312 button->device = device;
db89b4f0 313 device->driver_data = button;
1da177e4 314
c0968f0e
DT
315 button->input = input = input_allocate_device();
316 if (!input) {
317 error = -ENOMEM;
318 goto err_free_button;
319 }
320
1da177e4
LT
321 /*
322 * Determine the button type (via hid), as fixed-feature buttons
323 * need to be handled a bit differently than generic-space.
324 */
325 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
326 button->type = ACPI_BUTTON_TYPE_POWER;
4be44fcd
LB
327 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
328 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 329 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
4be44fcd 330 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
1da177e4
LT
331 button->type = ACPI_BUTTON_TYPE_POWERF;
332 strcpy(acpi_device_name(device),
4be44fcd
LB
333 ACPI_BUTTON_DEVICE_NAME_POWERF);
334 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 335 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
4be44fcd 336 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
1da177e4 337 button->type = ACPI_BUTTON_TYPE_SLEEP;
4be44fcd
LB
338 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
339 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 340 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
4be44fcd 341 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
1da177e4
LT
342 button->type = ACPI_BUTTON_TYPE_SLEEPF;
343 strcpy(acpi_device_name(device),
4be44fcd
LB
344 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
345 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 346 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
4be44fcd 347 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
1da177e4 348 button->type = ACPI_BUTTON_TYPE_LID;
4be44fcd
LB
349 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
350 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 351 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
4be44fcd 352 } else {
6468463a
LB
353 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
354 acpi_device_hid(device));
c0968f0e
DT
355 error = -ENODEV;
356 goto err_free_input;
1da177e4
LT
357 }
358
c0968f0e
DT
359 error = acpi_button_add_fs(device);
360 if (error)
361 goto err_free_input;
362
c0968f0e
DT
363 snprintf(button->phys, sizeof(button->phys),
364 "%s/button/input0", acpi_device_hid(device));
365
366 input->name = acpi_device_name(device);
367 input->phys = button->phys;
368 input->id.bustype = BUS_HOST;
369 input->id.product = button->type;
3b34e523 370 input->dev.parent = &device->dev;
b34a8030 371
1da177e4 372 switch (button->type) {
c0968f0e 373 case ACPI_BUTTON_TYPE_POWER:
1da177e4 374 case ACPI_BUTTON_TYPE_POWERF:
7b19ada2 375 input->evbit[0] = BIT_MASK(EV_KEY);
c0968f0e 376 set_bit(KEY_POWER, input->keybit);
1da177e4 377 break;
c0968f0e
DT
378
379 case ACPI_BUTTON_TYPE_SLEEP:
1da177e4 380 case ACPI_BUTTON_TYPE_SLEEPF:
7b19ada2 381 input->evbit[0] = BIT_MASK(EV_KEY);
c0968f0e 382 set_bit(KEY_SLEEP, input->keybit);
1da177e4 383 break;
c0968f0e
DT
384
385 case ACPI_BUTTON_TYPE_LID:
7b19ada2 386 input->evbit[0] = BIT_MASK(EV_SW);
c0968f0e 387 set_bit(SW_LID, input->swbit);
1da177e4
LT
388 break;
389 }
390
c0968f0e
DT
391 error = input_register_device(input);
392 if (error)
373cfc36 393 goto err_remove_fs;
23de5d9e
AS
394 if (button->type == ACPI_BUTTON_TYPE_LID)
395 acpi_lid_send_state(button);
1da177e4
LT
396
397 if (device->wakeup.flags.valid) {
398 /* Button's GPE is run-wake GPE */
4be44fcd
LB
399 acpi_set_gpe_type(device->wakeup.gpe_device,
400 device->wakeup.gpe_number,
401 ACPI_GPE_TYPE_WAKE_RUN);
402 acpi_enable_gpe(device->wakeup.gpe_device,
0b7084ac 403 device->wakeup.gpe_number);
1da177e4
LT
404 device->wakeup.state.enabled = 1;
405 }
406
4be44fcd
LB
407 printk(KERN_INFO PREFIX "%s [%s]\n",
408 acpi_device_name(device), acpi_device_bid(device));
c0968f0e 409 return 0;
1da177e4 410
c0968f0e
DT
411 err_remove_fs:
412 acpi_button_remove_fs(device);
413 err_free_input:
414 input_free_device(input);
415 err_free_button:
416 kfree(button);
417 return error;
1da177e4
LT
418}
419
4be44fcd 420static int acpi_button_remove(struct acpi_device *device, int type)
1da177e4 421{
c0968f0e 422 struct acpi_button *button;
1da177e4 423
1da177e4
LT
424 button = acpi_driver_data(device);
425
4be44fcd 426 acpi_button_remove_fs(device);
c0968f0e 427 input_unregister_device(button->input);
1da177e4 428 kfree(button);
d550d98d 429 return 0;
1da177e4
LT
430}
431
4be44fcd 432static int __init acpi_button_init(void)
1da177e4 433{
c0968f0e 434 int result;
1da177e4 435
b34a8030
AS
436 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
437 if (!acpi_button_dir)
d550d98d 438 return -ENODEV;
50a4da89 439
1da177e4
LT
440 result = acpi_bus_register_driver(&acpi_button_driver);
441 if (result < 0) {
b34a8030 442 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
d550d98d 443 return -ENODEV;
1da177e4
LT
444 }
445
d550d98d 446 return 0;
1da177e4
LT
447}
448
4be44fcd 449static void __exit acpi_button_exit(void)
1da177e4 450{
1da177e4
LT
451 acpi_bus_unregister_driver(&acpi_button_driver);
452
4be44fcd 453 if (acpi_power_dir)
b34a8030
AS
454 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
455 if (acpi_sleep_dir)
456 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
457 if (acpi_lid_dir)
458 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
459 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
1da177e4
LT
460}
461
1da177e4
LT
462module_init(acpi_button_init);
463module_exit(acpi_button_exit);