blob: fd81a0f5222f3c3f7f3173c66539a8b15111eca0 [file] [log] [blame]
Luming Yu79cda7d2005-08-03 18:07:59 -04001/*
2 * hotkey.c - ACPI Hotkey Driver ($Revision: 0.2 $)
Luming Yufb9802f2005-03-18 18:03:45 -05003 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/proc_fs.h>
29#include <linux/sched.h>
30#include <linux/kmod.h>
31#include <linux/seq_file.h>
32#include <acpi/acpi_drivers.h>
33#include <acpi/acpi_bus.h>
34#include <asm/uaccess.h>
35
36#define HOTKEY_ACPI_VERSION "0.1"
37
38#define HOTKEY_PROC "hotkey"
39#define HOTKEY_EV_CONFIG "event_config"
40#define HOTKEY_PL_CONFIG "poll_config"
41#define HOTKEY_ACTION "action"
42#define HOTKEY_INFO "info"
43
44#define ACPI_HOTK_NAME "Generic Hotkey Driver"
45#define ACPI_HOTK_CLASS "Hotkey"
46#define ACPI_HOTK_DEVICE_NAME "Hotkey"
47#define ACPI_HOTK_HID "Unknown?"
48#define ACPI_HOTKEY_COMPONENT 0x20000000
49
50#define ACPI_HOTKEY_EVENT 0x1
51#define ACPI_HOTKEY_POLLING 0x2
52#define ACPI_UNDEFINED_EVENT 0xf
53
Luming Yu79cda7d2005-08-03 18:07:59 -040054#define RESULT_STR_LEN 80
Luming Yufb9802f2005-03-18 18:03:45 -050055
Luming Yu79cda7d2005-08-03 18:07:59 -040056#define ACTION_METHOD 0
57#define POLL_METHOD 1
Luming Yufb9802f2005-03-18 18:03:45 -050058
Luming Yu79cda7d2005-08-03 18:07:59 -040059#define IS_EVENT(e) ((e) <= 10000 && (e) >0)
60#define IS_POLL(e) ((e) > 10000)
61#define IS_OTHERS(e) ((e)<=0 || (e)>=20000)
Luming Yufb9802f2005-03-18 18:03:45 -050062#define _COMPONENT ACPI_HOTKEY_COMPONENT
63ACPI_MODULE_NAME("acpi_hotkey")
64
Len Brown4be44fc2005-08-05 00:44:28 -040065 MODULE_AUTHOR("luming.yu@intel.com");
Luming Yufb9802f2005-03-18 18:03:45 -050066MODULE_DESCRIPTION(ACPI_HOTK_NAME);
67MODULE_LICENSE("GPL");
68
69/* standardized internal hotkey number/event */
70enum {
71 /* Video Extension event */
72 HK_EVENT_CYCLE_OUTPUT_DEVICE = 0x80,
73 HK_EVENT_OUTPUT_DEVICE_STATUS_CHANGE,
74 HK_EVENT_CYCLE_DISPLAY_OUTPUT,
75 HK_EVENT_NEXT_DISPLAY_OUTPUT,
76 HK_EVENT_PREVIOUS_DISPLAY_OUTPUT,
77 HK_EVENT_CYCLE_BRIGHTNESS,
78 HK_EVENT_INCREASE_BRIGHTNESS,
79 HK_EVENT_DECREASE_BRIGHTNESS,
80 HK_EVENT_ZERO_BRIGHTNESS,
81 HK_EVENT_DISPLAY_DEVICE_OFF,
82
83 /* Snd Card event */
84 HK_EVENT_VOLUME_MUTE,
85 HK_EVENT_VOLUME_INCLREASE,
86 HK_EVENT_VOLUME_DECREASE,
87
88 /* running state control */
89 HK_EVENT_ENTERRING_S3,
90 HK_EVENT_ENTERRING_S4,
91 HK_EVENT_ENTERRING_S5,
92};
93
94/* procdir we use */
95static struct proc_dir_entry *hotkey_proc_dir;
96static struct proc_dir_entry *hotkey_config;
97static struct proc_dir_entry *hotkey_poll_config;
98static struct proc_dir_entry *hotkey_action;
99static struct proc_dir_entry *hotkey_info;
100
101/* linkage for all type of hotkey */
102struct acpi_hotkey_link {
103 struct list_head entries;
104 int hotkey_type; /* event or polling based hotkey */
105 int hotkey_standard_num; /* standardized hotkey(event) number */
106};
107
108/* event based hotkey */
109struct acpi_event_hotkey {
110 struct acpi_hotkey_link hotkey_link;
111 int flag;
112 acpi_handle bus_handle; /* bus to install notify handler */
113 int external_hotkey_num; /* external hotkey/event number */
114 acpi_handle action_handle; /* acpi handle attached aml action method */
115 char *action_method; /* action method */
116};
117
Luming Yu79cda7d2005-08-03 18:07:59 -0400118/*
Luming Yufb9802f2005-03-18 18:03:45 -0500119 * There are two ways to poll status
120 * 1. directy call read_xxx method, without any arguments passed in
121 * 2. call write_xxx method, with arguments passed in, you need
122 * the result is saved in acpi_polling_hotkey.poll_result.
123 * anthoer read command through polling interface.
124 *
125 */
126
127/* polling based hotkey */
128struct acpi_polling_hotkey {
129 struct acpi_hotkey_link hotkey_link;
130 int flag;
131 acpi_handle poll_handle; /* acpi handle attached polling method */
132 char *poll_method; /* poll method */
133 acpi_handle action_handle; /* acpi handle attached action method */
134 char *action_method; /* action method */
Luming Yu79cda7d2005-08-03 18:07:59 -0400135 union acpi_object *poll_result; /* polling_result */
Luming Yufb9802f2005-03-18 18:03:45 -0500136 struct proc_dir_entry *proc;
137};
138
139/* hotkey object union */
140union acpi_hotkey {
141 struct list_head entries;
142 struct acpi_hotkey_link link;
143 struct acpi_event_hotkey event_hotkey;
144 struct acpi_polling_hotkey poll_hotkey;
145};
146
147/* hotkey object list */
148struct acpi_hotkey_list {
149 struct list_head *entries;
150 int count;
151};
152
153static int auto_hotkey_add(struct acpi_device *device);
154static int auto_hotkey_remove(struct acpi_device *device, int type);
155
156static struct acpi_driver hotkey_driver = {
157 .name = ACPI_HOTK_NAME,
158 .class = ACPI_HOTK_CLASS,
159 .ids = ACPI_HOTK_HID,
160 .ops = {
161 .add = auto_hotkey_add,
162 .remove = auto_hotkey_remove,
163 },
164};
165
Luming Yu79cda7d2005-08-03 18:07:59 -0400166static void free_hotkey_device(union acpi_hotkey *key);
167static void free_hotkey_buffer(union acpi_hotkey *key);
168static void free_poll_hotkey_buffer(union acpi_hotkey *key);
Luming Yufb9802f2005-03-18 18:03:45 -0500169static int hotkey_open_config(struct inode *inode, struct file *file);
Luming Yu79cda7d2005-08-03 18:07:59 -0400170static int hotkey_poll_open_config(struct inode *inode, struct file *file);
Luming Yufb9802f2005-03-18 18:03:45 -0500171static ssize_t hotkey_write_config(struct file *file,
172 const char __user * buffer,
173 size_t count, loff_t * data);
Luming Yufb9802f2005-03-18 18:03:45 -0500174static int hotkey_info_open_fs(struct inode *inode, struct file *file);
175static int hotkey_action_open_fs(struct inode *inode, struct file *file);
176static ssize_t hotkey_execute_aml_method(struct file *file,
177 const char __user * buffer,
178 size_t count, loff_t * data);
179static int hotkey_config_seq_show(struct seq_file *seq, void *offset);
Luming Yu79cda7d2005-08-03 18:07:59 -0400180static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset);
Luming Yufb9802f2005-03-18 18:03:45 -0500181static int hotkey_polling_open_fs(struct inode *inode, struct file *file);
Luming Yu79cda7d2005-08-03 18:07:59 -0400182static union acpi_hotkey *get_hotkey_by_event(struct
Len Brown4be44fc2005-08-05 00:44:28 -0400183 acpi_hotkey_list
184 *hotkey_list, int event);
Luming Yufb9802f2005-03-18 18:03:45 -0500185
186/* event based config */
187static struct file_operations hotkey_config_fops = {
188 .open = hotkey_open_config,
189 .read = seq_read,
190 .write = hotkey_write_config,
191 .llseek = seq_lseek,
192 .release = single_release,
193};
194
195/* polling based config */
196static struct file_operations hotkey_poll_config_fops = {
Luming Yu79cda7d2005-08-03 18:07:59 -0400197 .open = hotkey_poll_open_config,
Luming Yufb9802f2005-03-18 18:03:45 -0500198 .read = seq_read,
Luming Yu79cda7d2005-08-03 18:07:59 -0400199 .write = hotkey_write_config,
Luming Yufb9802f2005-03-18 18:03:45 -0500200 .llseek = seq_lseek,
201 .release = single_release,
202};
203
204/* hotkey driver info */
205static struct file_operations hotkey_info_fops = {
206 .open = hotkey_info_open_fs,
207 .read = seq_read,
208 .llseek = seq_lseek,
209 .release = single_release,
210};
211
212/* action */
213static struct file_operations hotkey_action_fops = {
214 .open = hotkey_action_open_fs,
215 .read = seq_read,
216 .write = hotkey_execute_aml_method,
217 .llseek = seq_lseek,
218 .release = single_release,
219};
220
221/* polling results */
222static struct file_operations hotkey_polling_fops = {
223 .open = hotkey_polling_open_fs,
224 .read = seq_read,
225 .llseek = seq_lseek,
226 .release = single_release,
227};
228
229struct acpi_hotkey_list global_hotkey_list; /* link all ev or pl hotkey */
230struct list_head hotkey_entries; /* head of the list of hotkey_list */
231
232static int hotkey_info_seq_show(struct seq_file *seq, void *offset)
233{
Luming Yufb9802f2005-03-18 18:03:45 -0500234
Luming Yu79cda7d2005-08-03 18:07:59 -0400235 seq_printf(seq, "Hotkey generic driver ver: %s\n", HOTKEY_ACPI_VERSION);
Luming Yufb9802f2005-03-18 18:03:45 -0500236
Patrick Mocheld550d982006-06-27 00:41:40 -0400237 return 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500238}
239
240static int hotkey_info_open_fs(struct inode *inode, struct file *file)
241{
242 return single_open(file, hotkey_info_seq_show, PDE(inode)->data);
243}
244
245static char *format_result(union acpi_object *object)
246{
Luming Yu79cda7d2005-08-03 18:07:59 -0400247 char *buf = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400248
Luming Yu79cda7d2005-08-03 18:07:59 -0400249 buf = (char *)kmalloc(RESULT_STR_LEN, GFP_KERNEL);
250 if (buf)
251 memset(buf, 0, RESULT_STR_LEN);
252 else
253 goto do_fail;
Luming Yufb9802f2005-03-18 18:03:45 -0500254
255 /* Now, just support integer type */
256 if (object->type == ACPI_TYPE_INTEGER)
Luming Yu79cda7d2005-08-03 18:07:59 -0400257 sprintf(buf, "%d\n", (u32) object->integer.value);
Len Brown4be44fc2005-08-05 00:44:28 -0400258 do_fail:
Luming Yu79cda7d2005-08-03 18:07:59 -0400259 return (buf);
Luming Yufb9802f2005-03-18 18:03:45 -0500260}
261
262static int hotkey_polling_seq_show(struct seq_file *seq, void *offset)
263{
264 struct acpi_polling_hotkey *poll_hotkey =
265 (struct acpi_polling_hotkey *)seq->private;
Luming Yu79cda7d2005-08-03 18:07:59 -0400266 char *buf;
Luming Yufb9802f2005-03-18 18:03:45 -0500267
Luming Yufb9802f2005-03-18 18:03:45 -0500268
Len Brown4be44fc2005-08-05 00:44:28 -0400269 if (poll_hotkey->poll_result) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400270 buf = format_result(poll_hotkey->poll_result);
Len Brown4be44fc2005-08-05 00:44:28 -0400271 if (buf)
Luming Yu79cda7d2005-08-03 18:07:59 -0400272 seq_printf(seq, "%s", buf);
273 kfree(buf);
274 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400275 return 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500276}
277
278static int hotkey_polling_open_fs(struct inode *inode, struct file *file)
279{
280 return single_open(file, hotkey_polling_seq_show, PDE(inode)->data);
281}
282
283static int hotkey_action_open_fs(struct inode *inode, struct file *file)
284{
285 return single_open(file, hotkey_info_seq_show, PDE(inode)->data);
286}
287
288/* Mapping external hotkey number to standardized hotkey event num */
289static int hotkey_get_internal_event(int event, struct acpi_hotkey_list *list)
290{
Luming Yu79cda7d2005-08-03 18:07:59 -0400291 struct list_head *entries;
292 int val = -1;
Luming Yufb9802f2005-03-18 18:03:45 -0500293
Luming Yufb9802f2005-03-18 18:03:45 -0500294
Luming Yu79cda7d2005-08-03 18:07:59 -0400295 list_for_each(entries, list->entries) {
Luming Yufb9802f2005-03-18 18:03:45 -0500296 union acpi_hotkey *key =
297 container_of(entries, union acpi_hotkey, entries);
298 if (key->link.hotkey_type == ACPI_HOTKEY_EVENT
Len Brown4be44fc2005-08-05 00:44:28 -0400299 && key->event_hotkey.external_hotkey_num == event) {
Luming Yufb9802f2005-03-18 18:03:45 -0500300 val = key->link.hotkey_standard_num;
Luming Yu79cda7d2005-08-03 18:07:59 -0400301 break;
302 }
Luming Yufb9802f2005-03-18 18:03:45 -0500303 }
304
Patrick Mocheld550d982006-06-27 00:41:40 -0400305 return val;
Luming Yufb9802f2005-03-18 18:03:45 -0500306}
307
308static void
309acpi_hotkey_notify_handler(acpi_handle handle, u32 event, void *data)
310{
311 struct acpi_device *device = NULL;
312 u32 internal_event;
313
Luming Yufb9802f2005-03-18 18:03:45 -0500314
315 if (acpi_bus_get_device(handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return;
Luming Yufb9802f2005-03-18 18:03:45 -0500317
318 internal_event = hotkey_get_internal_event(event, &global_hotkey_list);
Luming Yu79cda7d2005-08-03 18:07:59 -0400319 acpi_bus_generate_event(device, internal_event, 0);
Luming Yufb9802f2005-03-18 18:03:45 -0500320
Patrick Mocheld550d982006-06-27 00:41:40 -0400321 return;
Luming Yufb9802f2005-03-18 18:03:45 -0500322}
323
324/* Need to invent automatically hotkey add method */
325static int auto_hotkey_add(struct acpi_device *device)
326{
327 /* Implement me */
328 return 0;
329}
330
331/* Need to invent automatically hotkey remove method */
332static int auto_hotkey_remove(struct acpi_device *device, int type)
333{
334 /* Implement me */
335 return 0;
336}
337
338/* Create a proc file for each polling method */
339static int create_polling_proc(union acpi_hotkey *device)
340{
341 struct proc_dir_entry *proc;
Len Brown4be44fc2005-08-05 00:44:28 -0400342 char proc_name[80];
Andrew Morton8de7a632005-03-30 22:53:30 -0500343 mode_t mode;
Luming Yufb9802f2005-03-18 18:03:45 -0500344
Andrew Morton8de7a632005-03-30 22:53:30 -0500345 mode = S_IFREG | S_IRUGO | S_IWUGO;
Luming Yufb9802f2005-03-18 18:03:45 -0500346
Luming Yu79cda7d2005-08-03 18:07:59 -0400347 sprintf(proc_name, "%d", device->link.hotkey_standard_num);
348 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400349 strcat(proc_name, device->poll_hotkey.poll_method);
350 */
Luming Yu79cda7d2005-08-03 18:07:59 -0400351 proc = create_proc_entry(proc_name, mode, hotkey_proc_dir);
Luming Yufb9802f2005-03-18 18:03:45 -0500352
353 if (!proc) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400354 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -0500355 } else {
356 proc->proc_fops = &hotkey_polling_fops;
357 proc->owner = THIS_MODULE;
358 proc->data = device;
359 proc->uid = 0;
360 proc->gid = 0;
361 device->poll_hotkey.proc = proc;
362 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400363 return 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500364}
365
Luming Yufb9802f2005-03-18 18:03:45 -0500366static int hotkey_add(union acpi_hotkey *device)
367{
368 int status = 0;
369 struct acpi_device *dev = NULL;
370
Luming Yufb9802f2005-03-18 18:03:45 -0500371
372 if (device->link.hotkey_type == ACPI_HOTKEY_EVENT) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400373 acpi_bus_get_device(device->event_hotkey.bus_handle, &dev);
Luming Yufb9802f2005-03-18 18:03:45 -0500374 status = acpi_install_notify_handler(dev->handle,
Luming Yu79cda7d2005-08-03 18:07:59 -0400375 ACPI_DEVICE_NOTIFY,
Luming Yufb9802f2005-03-18 18:03:45 -0500376 acpi_hotkey_notify_handler,
Luming Yu79cda7d2005-08-03 18:07:59 -0400377 dev);
Luming Yufb9802f2005-03-18 18:03:45 -0500378 } else /* Add polling hotkey */
379 create_polling_proc(device);
380
381 global_hotkey_list.count++;
382
383 list_add_tail(&device->link.entries, global_hotkey_list.entries);
384
Patrick Mocheld550d982006-06-27 00:41:40 -0400385 return status;
Luming Yufb9802f2005-03-18 18:03:45 -0500386}
387
388static int hotkey_remove(union acpi_hotkey *device)
389{
390 struct list_head *entries, *next;
391
Luming Yufb9802f2005-03-18 18:03:45 -0500392
393 list_for_each_safe(entries, next, global_hotkey_list.entries) {
394 union acpi_hotkey *key =
395 container_of(entries, union acpi_hotkey, entries);
396 if (key->link.hotkey_standard_num ==
397 device->link.hotkey_standard_num) {
398 list_del(&key->link.entries);
Luming Yu79cda7d2005-08-03 18:07:59 -0400399 free_hotkey_device(key);
Luming Yufb9802f2005-03-18 18:03:45 -0500400 global_hotkey_list.count--;
401 break;
402 }
403 }
Luming Yu79cda7d2005-08-03 18:07:59 -0400404 kfree(device);
Patrick Mocheld550d982006-06-27 00:41:40 -0400405 return 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500406}
407
Len Brown4be44fc2005-08-05 00:44:28 -0400408static int hotkey_update(union acpi_hotkey *key)
Luming Yufb9802f2005-03-18 18:03:45 -0500409{
Luming Yu79cda7d2005-08-03 18:07:59 -0400410 struct list_head *entries;
Luming Yufb9802f2005-03-18 18:03:45 -0500411
Luming Yufb9802f2005-03-18 18:03:45 -0500412
Luming Yu79cda7d2005-08-03 18:07:59 -0400413 list_for_each(entries, global_hotkey_list.entries) {
Len Brown4be44fc2005-08-05 00:44:28 -0400414 union acpi_hotkey *tmp =
Luming Yufb9802f2005-03-18 18:03:45 -0500415 container_of(entries, union acpi_hotkey, entries);
Luming Yu79cda7d2005-08-03 18:07:59 -0400416 if (tmp->link.hotkey_standard_num ==
Luming Yufb9802f2005-03-18 18:03:45 -0500417 key->link.hotkey_standard_num) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400418 if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
419 free_hotkey_buffer(tmp);
420 tmp->event_hotkey.bus_handle =
Len Brown4be44fc2005-08-05 00:44:28 -0400421 key->event_hotkey.bus_handle;
Luming Yu79cda7d2005-08-03 18:07:59 -0400422 tmp->event_hotkey.external_hotkey_num =
Len Brown4be44fc2005-08-05 00:44:28 -0400423 key->event_hotkey.external_hotkey_num;
Luming Yu79cda7d2005-08-03 18:07:59 -0400424 tmp->event_hotkey.action_handle =
Len Brown4be44fc2005-08-05 00:44:28 -0400425 key->event_hotkey.action_handle;
Luming Yu79cda7d2005-08-03 18:07:59 -0400426 tmp->event_hotkey.action_method =
Len Brown4be44fc2005-08-05 00:44:28 -0400427 key->event_hotkey.action_method;
Luming Yu79cda7d2005-08-03 18:07:59 -0400428 kfree(key);
429 } else {
430 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400431 char proc_name[80];
Luming Yu79cda7d2005-08-03 18:07:59 -0400432
Len Brown4be44fc2005-08-05 00:44:28 -0400433 sprintf(proc_name, "%d", tmp->link.hotkey_standard_num);
434 strcat(proc_name, tmp->poll_hotkey.poll_method);
435 remove_proc_entry(proc_name,hotkey_proc_dir);
436 */
Luming Yu79cda7d2005-08-03 18:07:59 -0400437 free_poll_hotkey_buffer(tmp);
438 tmp->poll_hotkey.poll_handle =
Len Brown4be44fc2005-08-05 00:44:28 -0400439 key->poll_hotkey.poll_handle;
Luming Yu79cda7d2005-08-03 18:07:59 -0400440 tmp->poll_hotkey.poll_method =
Len Brown4be44fc2005-08-05 00:44:28 -0400441 key->poll_hotkey.poll_method;
Luming Yu79cda7d2005-08-03 18:07:59 -0400442 tmp->poll_hotkey.action_handle =
Len Brown4be44fc2005-08-05 00:44:28 -0400443 key->poll_hotkey.action_handle;
Luming Yu79cda7d2005-08-03 18:07:59 -0400444 tmp->poll_hotkey.action_method =
Len Brown4be44fc2005-08-05 00:44:28 -0400445 key->poll_hotkey.action_method;
Luming Yu79cda7d2005-08-03 18:07:59 -0400446 tmp->poll_hotkey.poll_result =
Len Brown4be44fc2005-08-05 00:44:28 -0400447 key->poll_hotkey.poll_result;
Luming Yu79cda7d2005-08-03 18:07:59 -0400448 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400449 create_polling_proc(tmp);
450 */
Luming Yu79cda7d2005-08-03 18:07:59 -0400451 kfree(key);
452 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400453 return 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500454 break;
455 }
456 }
457
Patrick Mocheld550d982006-06-27 00:41:40 -0400458 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -0500459}
460
461static void free_hotkey_device(union acpi_hotkey *key)
462{
463 struct acpi_device *dev;
Luming Yufb9802f2005-03-18 18:03:45 -0500464
Luming Yufb9802f2005-03-18 18:03:45 -0500465
466 if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400467 acpi_bus_get_device(key->event_hotkey.bus_handle, &dev);
Luming Yufb9802f2005-03-18 18:03:45 -0500468 if (dev->handle)
469 acpi_remove_notify_handler(dev->handle,
Luming Yu79cda7d2005-08-03 18:07:59 -0400470 ACPI_DEVICE_NOTIFY,
Luming Yufb9802f2005-03-18 18:03:45 -0500471 acpi_hotkey_notify_handler);
Luming Yu79cda7d2005-08-03 18:07:59 -0400472 free_hotkey_buffer(key);
473 } else {
Len Brown4be44fc2005-08-05 00:44:28 -0400474 char proc_name[80];
Luming Yu79cda7d2005-08-03 18:07:59 -0400475
476 sprintf(proc_name, "%d", key->link.hotkey_standard_num);
477 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400478 strcat(proc_name, key->poll_hotkey.poll_method);
479 */
480 remove_proc_entry(proc_name, hotkey_proc_dir);
Luming Yu79cda7d2005-08-03 18:07:59 -0400481 free_poll_hotkey_buffer(key);
482 }
Luming Yufb9802f2005-03-18 18:03:45 -0500483 kfree(key);
Patrick Mocheld550d982006-06-27 00:41:40 -0400484 return;
Luming Yufb9802f2005-03-18 18:03:45 -0500485}
486
Len Brown4be44fc2005-08-05 00:44:28 -0400487static void free_hotkey_buffer(union acpi_hotkey *key)
Luming Yu79cda7d2005-08-03 18:07:59 -0400488{
489 kfree(key->event_hotkey.action_method);
490}
491
Len Brown4be44fc2005-08-05 00:44:28 -0400492static void free_poll_hotkey_buffer(union acpi_hotkey *key)
Luming Yu79cda7d2005-08-03 18:07:59 -0400493{
494 kfree(key->poll_hotkey.action_method);
495 kfree(key->poll_hotkey.poll_method);
496 kfree(key->poll_hotkey.poll_result);
497}
Luming Yufb9802f2005-03-18 18:03:45 -0500498static int
499init_hotkey_device(union acpi_hotkey *key, char *bus_str, char *action_str,
500 char *method, int std_num, int external_num)
501{
Len Brown4be44fc2005-08-05 00:44:28 -0400502 acpi_handle tmp_handle;
Luming Yu79cda7d2005-08-03 18:07:59 -0400503 acpi_status status = AE_OK;
504
Luming Yufb9802f2005-03-18 18:03:45 -0500505
Len Brown4be44fc2005-08-05 00:44:28 -0400506 if (std_num < 0 || IS_POLL(std_num) || !key)
Luming Yu79cda7d2005-08-03 18:07:59 -0400507 goto do_fail;
508
Len Brown4be44fc2005-08-05 00:44:28 -0400509 if (!bus_str || !action_str || !method)
Luming Yu79cda7d2005-08-03 18:07:59 -0400510 goto do_fail;
511
Luming Yufb9802f2005-03-18 18:03:45 -0500512 key->link.hotkey_type = ACPI_HOTKEY_EVENT;
513 key->link.hotkey_standard_num = std_num;
514 key->event_hotkey.flag = 0;
Luming Yu79cda7d2005-08-03 18:07:59 -0400515 key->event_hotkey.action_method = method;
Luming Yufb9802f2005-03-18 18:03:45 -0500516
Len Brown4be44fc2005-08-05 00:44:28 -0400517 status =
518 acpi_get_handle(NULL, bus_str, &(key->event_hotkey.bus_handle));
519 if (ACPI_FAILURE(status))
Luming Yu79cda7d2005-08-03 18:07:59 -0400520 goto do_fail;
521 key->event_hotkey.external_hotkey_num = external_num;
Len Brown4be44fc2005-08-05 00:44:28 -0400522 status =
523 acpi_get_handle(NULL, action_str,
524 &(key->event_hotkey.action_handle));
525 if (ACPI_FAILURE(status))
Luming Yu79cda7d2005-08-03 18:07:59 -0400526 goto do_fail;
527 status = acpi_get_handle(key->event_hotkey.action_handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400528 method, &tmp_handle);
Luming Yu79cda7d2005-08-03 18:07:59 -0400529 if (ACPI_FAILURE(status))
530 goto do_fail;
Patrick Mocheld550d982006-06-27 00:41:40 -0400531 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400532 do_fail:
Patrick Mocheld550d982006-06-27 00:41:40 -0400533 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -0500534}
535
536static int
537init_poll_hotkey_device(union acpi_hotkey *key,
538 char *poll_str,
539 char *poll_method,
540 char *action_str, char *action_method, int std_num)
541{
Luming Yu79cda7d2005-08-03 18:07:59 -0400542 acpi_status status = AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400543 acpi_handle tmp_handle;
Luming Yu79cda7d2005-08-03 18:07:59 -0400544
Luming Yufb9802f2005-03-18 18:03:45 -0500545
Len Brown4be44fc2005-08-05 00:44:28 -0400546 if (std_num < 0 || IS_EVENT(std_num) || !key)
Luming Yu79cda7d2005-08-03 18:07:59 -0400547 goto do_fail;
548
Len Brown4be44fc2005-08-05 00:44:28 -0400549 if (!poll_str || !poll_method || !action_str || !action_method)
Luming Yu79cda7d2005-08-03 18:07:59 -0400550 goto do_fail;
551
Luming Yufb9802f2005-03-18 18:03:45 -0500552 key->link.hotkey_type = ACPI_HOTKEY_POLLING;
553 key->link.hotkey_standard_num = std_num;
554 key->poll_hotkey.flag = 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500555 key->poll_hotkey.poll_method = poll_method;
Luming Yu79cda7d2005-08-03 18:07:59 -0400556 key->poll_hotkey.action_method = action_method;
557
Len Brown4be44fc2005-08-05 00:44:28 -0400558 status =
559 acpi_get_handle(NULL, poll_str, &(key->poll_hotkey.poll_handle));
560 if (ACPI_FAILURE(status))
Luming Yu79cda7d2005-08-03 18:07:59 -0400561 goto do_fail;
562 status = acpi_get_handle(key->poll_hotkey.poll_handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400563 poll_method, &tmp_handle);
564 if (ACPI_FAILURE(status))
565 goto do_fail;
566 status =
567 acpi_get_handle(NULL, action_str,
568 &(key->poll_hotkey.action_handle));
Luming Yu79cda7d2005-08-03 18:07:59 -0400569 if (ACPI_FAILURE(status))
570 goto do_fail;
571 status = acpi_get_handle(key->poll_hotkey.action_handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400572 action_method, &tmp_handle);
Luming Yu79cda7d2005-08-03 18:07:59 -0400573 if (ACPI_FAILURE(status))
574 goto do_fail;
Luming Yufb9802f2005-03-18 18:03:45 -0500575 key->poll_hotkey.poll_result =
576 (union acpi_object *)kmalloc(sizeof(union acpi_object), GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400577 if (!key->poll_hotkey.poll_result)
Luming Yu79cda7d2005-08-03 18:07:59 -0400578 goto do_fail;
Patrick Mocheld550d982006-06-27 00:41:40 -0400579 return AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400580 do_fail:
Patrick Mocheld550d982006-06-27 00:41:40 -0400581 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -0500582}
583
Luming Yufb9802f2005-03-18 18:03:45 -0500584static int hotkey_open_config(struct inode *inode, struct file *file)
585{
Patrick Mocheld550d982006-06-27 00:41:40 -0400586 return (single_open
Luming Yufb9802f2005-03-18 18:03:45 -0500587 (file, hotkey_config_seq_show, PDE(inode)->data));
588}
589
Luming Yu79cda7d2005-08-03 18:07:59 -0400590static int hotkey_poll_open_config(struct inode *inode, struct file *file)
591{
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return (single_open
Luming Yu79cda7d2005-08-03 18:07:59 -0400593 (file, hotkey_poll_config_seq_show, PDE(inode)->data));
594}
595
Luming Yufb9802f2005-03-18 18:03:45 -0500596static int hotkey_config_seq_show(struct seq_file *seq, void *offset)
597{
598 struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
Luming Yu79cda7d2005-08-03 18:07:59 -0400599 struct list_head *entries;
Luming Yufb9802f2005-03-18 18:03:45 -0500600 char bus_name[ACPI_PATHNAME_MAX] = { 0 };
601 char action_name[ACPI_PATHNAME_MAX] = { 0 };
602 struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
603 struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };
604
Luming Yufb9802f2005-03-18 18:03:45 -0500605
Luming Yu79cda7d2005-08-03 18:07:59 -0400606 list_for_each(entries, hotkey_list->entries) {
Luming Yufb9802f2005-03-18 18:03:45 -0500607 union acpi_hotkey *key =
608 container_of(entries, union acpi_hotkey, entries);
609 if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
610 acpi_get_name(key->event_hotkey.bus_handle,
611 ACPI_NAME_TYPE_MAX, &bus);
612 acpi_get_name(key->event_hotkey.action_handle,
613 ACPI_NAME_TYPE_MAX, &act);
Luming Yu79cda7d2005-08-03 18:07:59 -0400614 seq_printf(seq, "%s:%s:%s:%d:%d\n", bus_name,
Luming Yufb9802f2005-03-18 18:03:45 -0500615 action_name,
616 key->event_hotkey.action_method,
617 key->link.hotkey_standard_num,
618 key->event_hotkey.external_hotkey_num);
Luming Yu79cda7d2005-08-03 18:07:59 -0400619 }
620 }
621 seq_puts(seq, "\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400622 return 0;
Luming Yu79cda7d2005-08-03 18:07:59 -0400623}
624
625static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset)
626{
627 struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
628 struct list_head *entries;
629 char bus_name[ACPI_PATHNAME_MAX] = { 0 };
630 char action_name[ACPI_PATHNAME_MAX] = { 0 };
631 struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
632 struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };
633
Luming Yu79cda7d2005-08-03 18:07:59 -0400634
635 list_for_each(entries, hotkey_list->entries) {
636 union acpi_hotkey *key =
637 container_of(entries, union acpi_hotkey, entries);
638 if (key->link.hotkey_type == ACPI_HOTKEY_POLLING) {
Luming Yufb9802f2005-03-18 18:03:45 -0500639 acpi_get_name(key->poll_hotkey.poll_handle,
640 ACPI_NAME_TYPE_MAX, &bus);
641 acpi_get_name(key->poll_hotkey.action_handle,
642 ACPI_NAME_TYPE_MAX, &act);
Luming Yu79cda7d2005-08-03 18:07:59 -0400643 seq_printf(seq, "%s:%s:%s:%s:%d\n", bus_name,
Luming Yufb9802f2005-03-18 18:03:45 -0500644 key->poll_hotkey.poll_method,
645 action_name,
646 key->poll_hotkey.action_method,
647 key->link.hotkey_standard_num);
648 }
649 }
650 seq_puts(seq, "\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400651 return 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500652}
653
654static int
655get_parms(char *config_record,
656 int *cmd,
Luming Yu79cda7d2005-08-03 18:07:59 -0400657 char **bus_handle,
658 char **bus_method,
659 char **action_handle,
660 char **method, int *internal_event_num, int *external_event_num)
Luming Yufb9802f2005-03-18 18:03:45 -0500661{
Luming Yu79cda7d2005-08-03 18:07:59 -0400662 char *tmp, *tmp1, count;
Luming Yufb9802f2005-03-18 18:03:45 -0500663
664 sscanf(config_record, "%d", cmd);
665
Len Brown4be44fc2005-08-05 00:44:28 -0400666 if (*cmd == 1) {
667 if (sscanf(config_record, "%d:%d", cmd, internal_event_num) !=
668 2)
Luming Yu79cda7d2005-08-03 18:07:59 -0400669 goto do_fail;
670 else
671 return (6);
672 }
Luming Yufb9802f2005-03-18 18:03:45 -0500673 tmp = strchr(config_record, ':');
Luming Yu79cda7d2005-08-03 18:07:59 -0400674 if (!tmp)
675 goto do_fail;
Luming Yufb9802f2005-03-18 18:03:45 -0500676 tmp++;
677 tmp1 = strchr(tmp, ':');
Luming Yu79cda7d2005-08-03 18:07:59 -0400678 if (!tmp1)
679 goto do_fail;
680
681 count = tmp1 - tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400682 *bus_handle = (char *)kmalloc(count + 1, GFP_KERNEL);
683 if (!*bus_handle)
Luming Yu79cda7d2005-08-03 18:07:59 -0400684 goto do_fail;
685 strncpy(*bus_handle, tmp, count);
686 *(*bus_handle + count) = 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500687
688 tmp = tmp1;
689 tmp++;
690 tmp1 = strchr(tmp, ':');
Luming Yu79cda7d2005-08-03 18:07:59 -0400691 if (!tmp1)
692 goto do_fail;
693 count = tmp1 - tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400694 *bus_method = (char *)kmalloc(count + 1, GFP_KERNEL);
695 if (!*bus_method)
Luming Yu79cda7d2005-08-03 18:07:59 -0400696 goto do_fail;
697 strncpy(*bus_method, tmp, count);
698 *(*bus_method + count) = 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500699
700 tmp = tmp1;
701 tmp++;
702 tmp1 = strchr(tmp, ':');
Luming Yu79cda7d2005-08-03 18:07:59 -0400703 if (!tmp1)
704 goto do_fail;
705 count = tmp1 - tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400706 *action_handle = (char *)kmalloc(count + 1, GFP_KERNEL);
Irwan Djajadi1fee9402006-01-20 15:28:00 -0500707 if (!*action_handle)
708 goto do_fail;
Luming Yu79cda7d2005-08-03 18:07:59 -0400709 strncpy(*action_handle, tmp, count);
710 *(*action_handle + count) = 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500711
712 tmp = tmp1;
713 tmp++;
714 tmp1 = strchr(tmp, ':');
Luming Yu79cda7d2005-08-03 18:07:59 -0400715 if (!tmp1)
716 goto do_fail;
717 count = tmp1 - tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400718 *method = (char *)kmalloc(count + 1, GFP_KERNEL);
719 if (!*method)
Luming Yu79cda7d2005-08-03 18:07:59 -0400720 goto do_fail;
721 strncpy(*method, tmp, count);
722 *(*method + count) = 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500723
Len Brown4be44fc2005-08-05 00:44:28 -0400724 if (sscanf(tmp1 + 1, "%d:%d", internal_event_num, external_event_num) <=
725 0)
Luming Yu79cda7d2005-08-03 18:07:59 -0400726 goto do_fail;
727
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return 6;
Len Brown4be44fc2005-08-05 00:44:28 -0400729 do_fail:
Patrick Mocheld550d982006-06-27 00:41:40 -0400730 return -1;
Luming Yufb9802f2005-03-18 18:03:45 -0500731}
732
733/* count is length for one input record */
734static ssize_t hotkey_write_config(struct file *file,
735 const char __user * buffer,
736 size_t count, loff_t * data)
737{
Luming Yu79cda7d2005-08-03 18:07:59 -0400738 char *config_record = NULL;
739 char *bus_handle = NULL;
740 char *bus_method = NULL;
741 char *action_handle = NULL;
742 char *method = NULL;
Luming Yufb9802f2005-03-18 18:03:45 -0500743 int cmd, internal_event_num, external_event_num;
744 int ret = 0;
745 union acpi_hotkey *key = NULL;
746
Luming Yufb9802f2005-03-18 18:03:45 -0500747
Len Brown4be44fc2005-08-05 00:44:28 -0400748 config_record = (char *)kmalloc(count + 1, GFP_KERNEL);
749 if (!config_record)
Patrick Mocheld550d982006-06-27 00:41:40 -0400750 return -ENOMEM;
Luming Yufb9802f2005-03-18 18:03:45 -0500751
752 if (copy_from_user(config_record, buffer, count)) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400753 kfree(config_record);
Len Brown64684632006-06-26 23:41:38 -0400754 printk(KERN_ERR PREFIX "Invalid data\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400755 return -EINVAL;
Luming Yufb9802f2005-03-18 18:03:45 -0500756 }
Luming Yu79cda7d2005-08-03 18:07:59 -0400757 config_record[count] = 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500758
759 ret = get_parms(config_record,
760 &cmd,
Luming Yu79cda7d2005-08-03 18:07:59 -0400761 &bus_handle,
762 &bus_method,
763 &action_handle,
764 &method, &internal_event_num, &external_event_num);
765
766 kfree(config_record);
Len Brown4be44fc2005-08-05 00:44:28 -0400767 if (IS_OTHERS(internal_event_num))
Luming Yu79cda7d2005-08-03 18:07:59 -0400768 goto do_fail;
Luming Yufb9802f2005-03-18 18:03:45 -0500769 if (ret != 6) {
Len Brown4be44fc2005-08-05 00:44:28 -0400770 do_fail:
Luming Yu79cda7d2005-08-03 18:07:59 -0400771 kfree(bus_handle);
772 kfree(bus_method);
773 kfree(action_handle);
774 kfree(method);
Len Brown64684632006-06-26 23:41:38 -0400775 printk(KERN_ERR PREFIX "Invalid data format ret=%d\n", ret);
Patrick Mocheld550d982006-06-27 00:41:40 -0400776 return -EINVAL;
Luming Yufb9802f2005-03-18 18:03:45 -0500777 }
778
779 key = kmalloc(sizeof(union acpi_hotkey), GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400780 if (!key)
Luming Yu79cda7d2005-08-03 18:07:59 -0400781 goto do_fail;
782 memset(key, 0, sizeof(union acpi_hotkey));
Len Brown4be44fc2005-08-05 00:44:28 -0400783 if (cmd == 1) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400784 union acpi_hotkey *tmp = NULL;
785 tmp = get_hotkey_by_event(&global_hotkey_list,
Len Brown4be44fc2005-08-05 00:44:28 -0400786 internal_event_num);
787 if (!tmp)
Len Brown64684632006-06-26 23:41:38 -0400788 printk(KERN_ERR PREFIX "Invalid key\n");
Luming Yu79cda7d2005-08-03 18:07:59 -0400789 else
790 memcpy(key, tmp, sizeof(union acpi_hotkey));
791 goto cont_cmd;
792 }
793 if (IS_EVENT(internal_event_num)) {
794 kfree(bus_method);
795 ret = init_hotkey_device(key, bus_handle, action_handle, method,
Len Brown4be44fc2005-08-05 00:44:28 -0400796 internal_event_num,
797 external_event_num);
Luming Yu79cda7d2005-08-03 18:07:59 -0400798 } else
799 ret = init_poll_hotkey_device(key, bus_handle, bus_method,
Len Brown4be44fc2005-08-05 00:44:28 -0400800 action_handle, method,
801 internal_event_num);
Luming Yu79cda7d2005-08-03 18:07:59 -0400802 if (ret) {
803 kfree(bus_handle);
804 kfree(action_handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400805 if (IS_EVENT(internal_event_num))
Luming Yu79cda7d2005-08-03 18:07:59 -0400806 free_hotkey_buffer(key);
807 else
808 free_poll_hotkey_buffer(key);
Luming Yufb9802f2005-03-18 18:03:45 -0500809 kfree(key);
Len Brown64684632006-06-26 23:41:38 -0400810 printk(KERN_ERR PREFIX "Invalid hotkey\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400811 return -EINVAL;
Luming Yufb9802f2005-03-18 18:03:45 -0500812 }
Luming Yu79cda7d2005-08-03 18:07:59 -0400813
Len Brown4be44fc2005-08-05 00:44:28 -0400814 cont_cmd:
Luming Yu79cda7d2005-08-03 18:07:59 -0400815 kfree(bus_handle);
816 kfree(action_handle);
817
Luming Yufb9802f2005-03-18 18:03:45 -0500818 switch (cmd) {
819 case 0:
Len Brown4be44fc2005-08-05 00:44:28 -0400820 if (get_hotkey_by_event
821 (&global_hotkey_list, key->link.hotkey_standard_num))
Luming Yu79cda7d2005-08-03 18:07:59 -0400822 goto fail_out;
823 else
824 hotkey_add(key);
Luming Yufb9802f2005-03-18 18:03:45 -0500825 break;
826 case 1:
827 hotkey_remove(key);
828 break;
829 case 2:
Len Brown4be44fc2005-08-05 00:44:28 -0400830 if (hotkey_update(key))
Luming Yu79cda7d2005-08-03 18:07:59 -0400831 goto fail_out;
Luming Yufb9802f2005-03-18 18:03:45 -0500832 break;
833 default:
Luming Yu79cda7d2005-08-03 18:07:59 -0400834 goto fail_out;
Luming Yufb9802f2005-03-18 18:03:45 -0500835 break;
836 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400837 return count;
Len Brown4be44fc2005-08-05 00:44:28 -0400838 fail_out:
839 if (IS_EVENT(internal_event_num))
Luming Yu79cda7d2005-08-03 18:07:59 -0400840 free_hotkey_buffer(key);
841 else
842 free_poll_hotkey_buffer(key);
843 kfree(key);
Len Brown64684632006-06-26 23:41:38 -0400844 printk(KERN_ERR PREFIX "invalid key\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400845 return -EINVAL;
Luming Yufb9802f2005-03-18 18:03:45 -0500846}
847
Luming Yu79cda7d2005-08-03 18:07:59 -0400848/*
Luming Yufb9802f2005-03-18 18:03:45 -0500849 * This function evaluates an ACPI method, given an int as parameter, the
850 * method is searched within the scope of the handle, can be NULL. The output
851 * of the method is written is output, which can also be NULL
852 *
853 * returns 1 if write is successful, 0 else.
854 */
855static int write_acpi_int(acpi_handle handle, const char *method, int val,
856 struct acpi_buffer *output)
857{
858 struct acpi_object_list params; /* list of input parameters (an int here) */
859 union acpi_object in_obj; /* the only param we use */
860 acpi_status status;
861
Luming Yufb9802f2005-03-18 18:03:45 -0500862 params.count = 1;
863 params.pointer = &in_obj;
864 in_obj.type = ACPI_TYPE_INTEGER;
865 in_obj.integer.value = val;
866
867 status = acpi_evaluate_object(handle, (char *)method, &params, output);
868
Patrick Mocheld550d982006-06-27 00:41:40 -0400869 return (status == AE_OK);
Luming Yufb9802f2005-03-18 18:03:45 -0500870}
871
Len Brown4be44fc2005-08-05 00:44:28 -0400872static int read_acpi_int(acpi_handle handle, const char *method,
873 union acpi_object *val)
Luming Yufb9802f2005-03-18 18:03:45 -0500874{
875 struct acpi_buffer output;
876 union acpi_object out_obj;
877 acpi_status status;
878
Luming Yufb9802f2005-03-18 18:03:45 -0500879 output.length = sizeof(out_obj);
880 output.pointer = &out_obj;
881
882 status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
Len Brown4be44fc2005-08-05 00:44:28 -0400883 if (val) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400884 val->integer.value = out_obj.integer.value;
885 val->type = out_obj.type;
886 } else
Len Brown64684632006-06-26 23:41:38 -0400887 printk(KERN_ERR PREFIX "null val pointer\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400888 return ((status == AE_OK)
Luming Yufb9802f2005-03-18 18:03:45 -0500889 && (out_obj.type == ACPI_TYPE_INTEGER));
890}
891
Luming Yu79cda7d2005-08-03 18:07:59 -0400892static union acpi_hotkey *get_hotkey_by_event(struct
Len Brown4be44fc2005-08-05 00:44:28 -0400893 acpi_hotkey_list
894 *hotkey_list, int event)
Luming Yufb9802f2005-03-18 18:03:45 -0500895{
Luming Yu79cda7d2005-08-03 18:07:59 -0400896 struct list_head *entries;
Luming Yufb9802f2005-03-18 18:03:45 -0500897
Luming Yu79cda7d2005-08-03 18:07:59 -0400898 list_for_each(entries, hotkey_list->entries) {
Luming Yufb9802f2005-03-18 18:03:45 -0500899 union acpi_hotkey *key =
900 container_of(entries, union acpi_hotkey, entries);
Luming Yu79cda7d2005-08-03 18:07:59 -0400901 if (key->link.hotkey_standard_num == event) {
Len Brown4be44fc2005-08-05 00:44:28 -0400902 return (key);
Luming Yufb9802f2005-03-18 18:03:45 -0500903 }
904 }
Len Brown4be44fc2005-08-05 00:44:28 -0400905 return (NULL);
Luming Yufb9802f2005-03-18 18:03:45 -0500906}
907
Luming Yu79cda7d2005-08-03 18:07:59 -0400908/*
Luming Yufb9802f2005-03-18 18:03:45 -0500909 * user call AML method interface:
910 * Call convention:
911 * echo "event_num: arg type : value"
912 * example: echo "1:1:30" > /proc/acpi/action
913 * Just support 1 integer arg passing to AML method
914 */
915
916static ssize_t hotkey_execute_aml_method(struct file *file,
917 const char __user * buffer,
918 size_t count, loff_t * data)
919{
920 struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
Luming Yu79cda7d2005-08-03 18:07:59 -0400921 char *arg;
Len Brown4be44fc2005-08-05 00:44:28 -0400922 int event, method_type, type, value;
Luming Yu79cda7d2005-08-03 18:07:59 -0400923 union acpi_hotkey *key;
Luming Yufb9802f2005-03-18 18:03:45 -0500924
Luming Yufb9802f2005-03-18 18:03:45 -0500925
Len Brown4be44fc2005-08-05 00:44:28 -0400926 arg = (char *)kmalloc(count + 1, GFP_KERNEL);
927 if (!arg)
Patrick Mocheld550d982006-06-27 00:41:40 -0400928 return -ENOMEM;
Len Brown4be44fc2005-08-05 00:44:28 -0400929 arg[count] = 0;
Luming Yufb9802f2005-03-18 18:03:45 -0500930
931 if (copy_from_user(arg, buffer, count)) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400932 kfree(arg);
Len Brown64684632006-06-26 23:41:38 -0400933 printk(KERN_ERR PREFIX "Invalid argument 2\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400934 return -EINVAL;
Luming Yufb9802f2005-03-18 18:03:45 -0500935 }
936
Len Brown4be44fc2005-08-05 00:44:28 -0400937 if (sscanf(arg, "%d:%d:%d:%d", &event, &method_type, &type, &value) !=
938 4) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400939 kfree(arg);
Len Brown64684632006-06-26 23:41:38 -0400940 printk(KERN_ERR PREFIX "Invalid argument 3\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400941 return -EINVAL;
Luming Yufb9802f2005-03-18 18:03:45 -0500942 }
Luming Yu79cda7d2005-08-03 18:07:59 -0400943 kfree(arg);
Luming Yufb9802f2005-03-18 18:03:45 -0500944 if (type == ACPI_TYPE_INTEGER) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400945 key = get_hotkey_by_event(hotkey_list, event);
Len Brown4be44fc2005-08-05 00:44:28 -0400946 if (!key)
Luming Yu79cda7d2005-08-03 18:07:59 -0400947 goto do_fail;
Luming Yufb9802f2005-03-18 18:03:45 -0500948 if (IS_EVENT(event))
Luming Yu79cda7d2005-08-03 18:07:59 -0400949 write_acpi_int(key->event_hotkey.action_handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400950 key->event_hotkey.action_method, value,
951 NULL);
Luming Yufb9802f2005-03-18 18:03:45 -0500952 else if (IS_POLL(event)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400953 if (method_type == POLL_METHOD)
Luming Yu79cda7d2005-08-03 18:07:59 -0400954 read_acpi_int(key->poll_hotkey.poll_handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400955 key->poll_hotkey.poll_method,
956 key->poll_hotkey.poll_result);
957 else if (method_type == ACTION_METHOD)
Luming Yu79cda7d2005-08-03 18:07:59 -0400958 write_acpi_int(key->poll_hotkey.action_handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400959 key->poll_hotkey.action_method,
960 value, NULL);
Luming Yu79cda7d2005-08-03 18:07:59 -0400961 else
962 goto do_fail;
963
Luming Yufb9802f2005-03-18 18:03:45 -0500964 }
965 } else {
Len Browncece9292006-06-26 23:04:31 -0400966 printk(KERN_WARNING "Not supported\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400967 return -EINVAL;
Luming Yufb9802f2005-03-18 18:03:45 -0500968 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400969 return count;
Len Brown4be44fc2005-08-05 00:44:28 -0400970 do_fail:
Patrick Mocheld550d982006-06-27 00:41:40 -0400971 return -EINVAL;
Luming Yu79cda7d2005-08-03 18:07:59 -0400972
Luming Yufb9802f2005-03-18 18:03:45 -0500973}
974
975static int __init hotkey_init(void)
976{
977 int result;
978 mode_t mode = S_IFREG | S_IRUGO | S_IWUGO;
979
Luming Yufb9802f2005-03-18 18:03:45 -0500980
981 if (acpi_disabled)
982 return -ENODEV;
983
984 if (acpi_specific_hotkey_enabled) {
985 printk("Using specific hotkey driver\n");
986 return -ENODEV;
987 }
988
989 hotkey_proc_dir = proc_mkdir(HOTKEY_PROC, acpi_root_dir);
990 if (!hotkey_proc_dir) {
Luming Yufb9802f2005-03-18 18:03:45 -0500991 return (-ENODEV);
992 }
993 hotkey_proc_dir->owner = THIS_MODULE;
994
995 hotkey_config =
996 create_proc_entry(HOTKEY_EV_CONFIG, mode, hotkey_proc_dir);
997 if (!hotkey_config) {
Luming Yu79cda7d2005-08-03 18:07:59 -0400998 goto do_fail1;
Luming Yufb9802f2005-03-18 18:03:45 -0500999 } else {
1000 hotkey_config->proc_fops = &hotkey_config_fops;
1001 hotkey_config->data = &global_hotkey_list;
1002 hotkey_config->owner = THIS_MODULE;
1003 hotkey_config->uid = 0;
1004 hotkey_config->gid = 0;
1005 }
1006
1007 hotkey_poll_config =
1008 create_proc_entry(HOTKEY_PL_CONFIG, mode, hotkey_proc_dir);
1009 if (!hotkey_poll_config) {
Luming Yu79cda7d2005-08-03 18:07:59 -04001010 goto do_fail2;
Luming Yufb9802f2005-03-18 18:03:45 -05001011 } else {
1012 hotkey_poll_config->proc_fops = &hotkey_poll_config_fops;
1013 hotkey_poll_config->data = &global_hotkey_list;
1014 hotkey_poll_config->owner = THIS_MODULE;
1015 hotkey_poll_config->uid = 0;
1016 hotkey_poll_config->gid = 0;
1017 }
1018
1019 hotkey_action = create_proc_entry(HOTKEY_ACTION, mode, hotkey_proc_dir);
1020 if (!hotkey_action) {
Luming Yu79cda7d2005-08-03 18:07:59 -04001021 goto do_fail3;
Luming Yufb9802f2005-03-18 18:03:45 -05001022 } else {
1023 hotkey_action->proc_fops = &hotkey_action_fops;
1024 hotkey_action->owner = THIS_MODULE;
1025 hotkey_action->uid = 0;
1026 hotkey_action->gid = 0;
1027 }
1028
1029 hotkey_info = create_proc_entry(HOTKEY_INFO, mode, hotkey_proc_dir);
1030 if (!hotkey_info) {
Luming Yu79cda7d2005-08-03 18:07:59 -04001031 goto do_fail4;
Luming Yufb9802f2005-03-18 18:03:45 -05001032 } else {
1033 hotkey_info->proc_fops = &hotkey_info_fops;
1034 hotkey_info->owner = THIS_MODULE;
1035 hotkey_info->uid = 0;
1036 hotkey_info->gid = 0;
1037 }
1038
1039 result = acpi_bus_register_driver(&hotkey_driver);
Luming Yu79cda7d2005-08-03 18:07:59 -04001040 if (result < 0)
1041 goto do_fail5;
Luming Yufb9802f2005-03-18 18:03:45 -05001042 global_hotkey_list.count = 0;
1043 global_hotkey_list.entries = &hotkey_entries;
1044
1045 INIT_LIST_HEAD(&hotkey_entries);
1046
1047 return (0);
Luming Yu79cda7d2005-08-03 18:07:59 -04001048
Len Brown4be44fc2005-08-05 00:44:28 -04001049 do_fail5:
Luming Yu79cda7d2005-08-03 18:07:59 -04001050 remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001051 do_fail4:
Luming Yu79cda7d2005-08-03 18:07:59 -04001052 remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001053 do_fail3:
Luming Yu79cda7d2005-08-03 18:07:59 -04001054 remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001055 do_fail2:
Luming Yu79cda7d2005-08-03 18:07:59 -04001056 remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001057 do_fail1:
Luming Yu79cda7d2005-08-03 18:07:59 -04001058 remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
1059 return (-ENODEV);
Luming Yufb9802f2005-03-18 18:03:45 -05001060}
1061
1062static void __exit hotkey_exit(void)
1063{
1064 struct list_head *entries, *next;
1065
Luming Yufb9802f2005-03-18 18:03:45 -05001066
1067 list_for_each_safe(entries, next, global_hotkey_list.entries) {
1068 union acpi_hotkey *key =
1069 container_of(entries, union acpi_hotkey, entries);
1070
1071 acpi_os_wait_events_complete(NULL);
1072 list_del(&key->link.entries);
1073 global_hotkey_list.count--;
1074 free_hotkey_device(key);
1075 }
1076 acpi_bus_unregister_driver(&hotkey_driver);
1077 remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
1078 remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
1079 remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
1080 remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
1081 remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
1082 return;
1083}
1084
1085module_init(hotkey_init);
1086module_exit(hotkey_exit);