hotkey.c 27.8 KB
Newer Older
1 2
/*
 *  hotkey.c - ACPI Hotkey Driver ($Revision: 0.2 $)
L
Luming Yu 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 *
 *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/kmod.h>
#include <linux/seq_file.h>
#include <acpi/acpi_drivers.h>
#include <acpi/acpi_bus.h>
#include <asm/uaccess.h>

#define HOTKEY_ACPI_VERSION "0.1"

#define HOTKEY_PROC "hotkey"
#define HOTKEY_EV_CONFIG    "event_config"
#define HOTKEY_PL_CONFIG    "poll_config"
#define HOTKEY_ACTION   "action"
#define HOTKEY_INFO "info"

#define ACPI_HOTK_NAME          "Generic Hotkey Driver"
#define ACPI_HOTK_CLASS         "Hotkey"
#define ACPI_HOTK_DEVICE_NAME   "Hotkey"
#define ACPI_HOTK_HID           "Unknown?"
#define ACPI_HOTKEY_COMPONENT   0x20000000

#define ACPI_HOTKEY_EVENT   0x1
#define ACPI_HOTKEY_POLLING 0x2
#define ACPI_UNDEFINED_EVENT    0xf

54
#define RESULT_STR_LEN	    80
L
Luming Yu 已提交
55

56 57
#define ACTION_METHOD	0
#define POLL_METHOD	1
L
Luming Yu 已提交
58

59 60 61
#define IS_EVENT(e)       	((e) <= 10000 && (e) >0)
#define IS_POLL(e)      	((e) > 10000)
#define IS_OTHERS(e)		((e)<=0 || (e)>=20000)
L
Luming Yu 已提交
62 63 64
#define _COMPONENT              ACPI_HOTKEY_COMPONENT
ACPI_MODULE_NAME("acpi_hotkey")

L
Len Brown 已提交
65
    MODULE_AUTHOR("luming.yu@intel.com");
L
Luming Yu 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
MODULE_DESCRIPTION(ACPI_HOTK_NAME);
MODULE_LICENSE("GPL");

/*  standardized internal hotkey number/event  */
enum {
	/* Video Extension event */
	HK_EVENT_CYCLE_OUTPUT_DEVICE = 0x80,
	HK_EVENT_OUTPUT_DEVICE_STATUS_CHANGE,
	HK_EVENT_CYCLE_DISPLAY_OUTPUT,
	HK_EVENT_NEXT_DISPLAY_OUTPUT,
	HK_EVENT_PREVIOUS_DISPLAY_OUTPUT,
	HK_EVENT_CYCLE_BRIGHTNESS,
	HK_EVENT_INCREASE_BRIGHTNESS,
	HK_EVENT_DECREASE_BRIGHTNESS,
	HK_EVENT_ZERO_BRIGHTNESS,
	HK_EVENT_DISPLAY_DEVICE_OFF,

	/* Snd Card event */
	HK_EVENT_VOLUME_MUTE,
	HK_EVENT_VOLUME_INCLREASE,
	HK_EVENT_VOLUME_DECREASE,

	/* running state control */
	HK_EVENT_ENTERRING_S3,
	HK_EVENT_ENTERRING_S4,
	HK_EVENT_ENTERRING_S5,
};

/*  procdir we use */
static struct proc_dir_entry *hotkey_proc_dir;
static struct proc_dir_entry *hotkey_config;
static struct proc_dir_entry *hotkey_poll_config;
static struct proc_dir_entry *hotkey_action;
static struct proc_dir_entry *hotkey_info;

/* linkage for all type of hotkey */
struct acpi_hotkey_link {
	struct list_head entries;
	int hotkey_type;	/* event or polling based hotkey  */
	int hotkey_standard_num;	/* standardized hotkey(event) number */
};

/* event based hotkey */
struct acpi_event_hotkey {
	struct acpi_hotkey_link hotkey_link;
	int flag;
	acpi_handle bus_handle;	/* bus to install notify handler */
	int external_hotkey_num;	/* external hotkey/event number */
	acpi_handle action_handle;	/* acpi handle attached aml action method */
	char *action_method;	/* action method */
};

118
/*
L
Luming Yu 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
 * There are two ways to poll status
 * 1. directy call read_xxx method, without any arguments passed in
 * 2. call write_xxx method, with arguments passed in, you need
 * the result is saved in acpi_polling_hotkey.poll_result.
 * anthoer read command through polling interface.
 *
 */

/* polling based hotkey */
struct acpi_polling_hotkey {
	struct acpi_hotkey_link hotkey_link;
	int flag;
	acpi_handle poll_handle;	/* acpi handle attached polling method */
	char *poll_method;	/* poll method */
	acpi_handle action_handle;	/* acpi handle attached action method */
	char *action_method;	/* action method */
135
	union acpi_object *poll_result;	/* polling_result */
L
Luming Yu 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	struct proc_dir_entry *proc;
};

/* hotkey object union */
union acpi_hotkey {
	struct list_head entries;
	struct acpi_hotkey_link link;
	struct acpi_event_hotkey event_hotkey;
	struct acpi_polling_hotkey poll_hotkey;
};

/* hotkey object list */
struct acpi_hotkey_list {
	struct list_head *entries;
	int count;
};

static int auto_hotkey_add(struct acpi_device *device);
static int auto_hotkey_remove(struct acpi_device *device, int type);

static struct acpi_driver hotkey_driver = {
	.name = ACPI_HOTK_NAME,
	.class = ACPI_HOTK_CLASS,
	.ids = ACPI_HOTK_HID,
	.ops = {
		.add = auto_hotkey_add,
		.remove = auto_hotkey_remove,
		},
};

166 167 168
static void free_hotkey_device(union acpi_hotkey *key);
static void free_hotkey_buffer(union acpi_hotkey *key);
static void free_poll_hotkey_buffer(union acpi_hotkey *key);
L
Luming Yu 已提交
169
static int hotkey_open_config(struct inode *inode, struct file *file);
170
static int hotkey_poll_open_config(struct inode *inode, struct file *file);
L
Luming Yu 已提交
171 172 173 174 175 176 177 178 179
static ssize_t hotkey_write_config(struct file *file,
				   const char __user * buffer,
				   size_t count, loff_t * data);
static int hotkey_info_open_fs(struct inode *inode, struct file *file);
static int hotkey_action_open_fs(struct inode *inode, struct file *file);
static ssize_t hotkey_execute_aml_method(struct file *file,
					 const char __user * buffer,
					 size_t count, loff_t * data);
static int hotkey_config_seq_show(struct seq_file *seq, void *offset);
180
static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset);
L
Luming Yu 已提交
181
static int hotkey_polling_open_fs(struct inode *inode, struct file *file);
182
static union acpi_hotkey *get_hotkey_by_event(struct
L
Len Brown 已提交
183 184
					      acpi_hotkey_list
					      *hotkey_list, int event);
L
Luming Yu 已提交
185 186

/* event based config */
187
static const struct file_operations hotkey_config_fops = {
L
Luming Yu 已提交
188 189 190 191 192 193 194 195
	.open = hotkey_open_config,
	.read = seq_read,
	.write = hotkey_write_config,
	.llseek = seq_lseek,
	.release = single_release,
};

/* polling based config */
196
static const struct file_operations hotkey_poll_config_fops = {
197
	.open = hotkey_poll_open_config,
L
Luming Yu 已提交
198
	.read = seq_read,
199
	.write = hotkey_write_config,
L
Luming Yu 已提交
200 201 202 203 204
	.llseek = seq_lseek,
	.release = single_release,
};

/* hotkey driver info */
205
static const struct file_operations hotkey_info_fops = {
L
Luming Yu 已提交
206 207 208 209 210 211 212
	.open = hotkey_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

/* action */
213
static const struct file_operations hotkey_action_fops = {
L
Luming Yu 已提交
214 215 216 217 218 219 220 221
	.open = hotkey_action_open_fs,
	.read = seq_read,
	.write = hotkey_execute_aml_method,
	.llseek = seq_lseek,
	.release = single_release,
};

/* polling results */
222
static const struct file_operations hotkey_polling_fops = {
L
Luming Yu 已提交
223 224 225 226 227 228 229 230 231 232 233 234
	.open = hotkey_polling_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

struct acpi_hotkey_list global_hotkey_list;	/* link all ev or pl hotkey  */
struct list_head hotkey_entries;	/* head of the list of hotkey_list */

static int hotkey_info_seq_show(struct seq_file *seq, void *offset)
{

235
	seq_printf(seq, "Hotkey generic driver ver: %s\n", HOTKEY_ACPI_VERSION);
L
Luming Yu 已提交
236

237
	return 0;
L
Luming Yu 已提交
238 239 240 241 242 243 244 245 246
}

static int hotkey_info_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, hotkey_info_seq_show, PDE(inode)->data);
}

static char *format_result(union acpi_object *object)
{
247
	char *buf = NULL;
L
Len Brown 已提交
248

249 250 251 252 253
	buf = (char *)kmalloc(RESULT_STR_LEN, GFP_KERNEL);
	if (buf)
		memset(buf, 0, RESULT_STR_LEN);
	else
		goto do_fail;
L
Luming Yu 已提交
254 255 256

	/* Now, just support integer type */
	if (object->type == ACPI_TYPE_INTEGER)
257
		sprintf(buf, "%d\n", (u32) object->integer.value);
L
Len Brown 已提交
258
      do_fail:
259
	return (buf);
L
Luming Yu 已提交
260 261 262 263 264 265
}

static int hotkey_polling_seq_show(struct seq_file *seq, void *offset)
{
	struct acpi_polling_hotkey *poll_hotkey =
	    (struct acpi_polling_hotkey *)seq->private;
266
	char *buf;
L
Luming Yu 已提交
267 268


L
Len Brown 已提交
269
	if (poll_hotkey->poll_result) {
270
		buf = format_result(poll_hotkey->poll_result);
L
Len Brown 已提交
271
		if (buf)
272 273 274
			seq_printf(seq, "%s", buf);
		kfree(buf);
	}
275
	return 0;
L
Luming Yu 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
}

static int hotkey_polling_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, hotkey_polling_seq_show, PDE(inode)->data);
}

static int hotkey_action_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, hotkey_info_seq_show, PDE(inode)->data);
}

/* Mapping external hotkey number to standardized hotkey event num */
static int hotkey_get_internal_event(int event, struct acpi_hotkey_list *list)
{
291 292
	struct list_head *entries;
	int val = -1;
L
Luming Yu 已提交
293 294


295
	list_for_each(entries, list->entries) {
L
Luming Yu 已提交
296 297 298
		union acpi_hotkey *key =
		    container_of(entries, union acpi_hotkey, entries);
		if (key->link.hotkey_type == ACPI_HOTKEY_EVENT
L
Len Brown 已提交
299
		    && key->event_hotkey.external_hotkey_num == event) {
L
Luming Yu 已提交
300
			val = key->link.hotkey_standard_num;
301 302
			break;
		}
L
Luming Yu 已提交
303 304
	}

305
	return val;
L
Luming Yu 已提交
306 307 308 309 310 311 312 313 314 315
}

static void
acpi_hotkey_notify_handler(acpi_handle handle, u32 event, void *data)
{
	struct acpi_device *device = NULL;
	u32 internal_event;


	if (acpi_bus_get_device(handle, &device))
316
		return;
L
Luming Yu 已提交
317 318

	internal_event = hotkey_get_internal_event(event, &global_hotkey_list);
319
	acpi_bus_generate_event(device, internal_event, 0);
L
Luming Yu 已提交
320

321
	return;
L
Luming Yu 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
}

/* Need to invent automatically hotkey add method */
static int auto_hotkey_add(struct acpi_device *device)
{
	/* Implement me */
	return 0;
}

/* Need to invent automatically hotkey remove method */
static int auto_hotkey_remove(struct acpi_device *device, int type)
{
	/* Implement me */
	return 0;
}

/* Create a proc file for each polling method */
static int create_polling_proc(union acpi_hotkey *device)
{
	struct proc_dir_entry *proc;
L
Len Brown 已提交
342
	char proc_name[80];
343
	mode_t mode;
L
Luming Yu 已提交
344

345
	mode = S_IFREG | S_IRUGO | S_IWUGO;
L
Luming Yu 已提交
346

347 348
	sprintf(proc_name, "%d", device->link.hotkey_standard_num);
	/*
L
Len Brown 已提交
349 350
	   strcat(proc_name, device->poll_hotkey.poll_method);
	 */
351
	proc = create_proc_entry(proc_name, mode, hotkey_proc_dir);
L
Luming Yu 已提交
352 353

	if (!proc) {
354
		return -ENODEV;
L
Luming Yu 已提交
355 356 357 358 359 360 361 362
	} else {
		proc->proc_fops = &hotkey_polling_fops;
		proc->owner = THIS_MODULE;
		proc->data = device;
		proc->uid = 0;
		proc->gid = 0;
		device->poll_hotkey.proc = proc;
	}
363
	return 0;
L
Luming Yu 已提交
364 365 366 367 368 369 370 371 372
}

static int hotkey_add(union acpi_hotkey *device)
{
	int status = 0;
	struct acpi_device *dev = NULL;


	if (device->link.hotkey_type == ACPI_HOTKEY_EVENT) {
373
		acpi_bus_get_device(device->event_hotkey.bus_handle, &dev);
L
Luming Yu 已提交
374
		status = acpi_install_notify_handler(dev->handle,
375
						     ACPI_DEVICE_NOTIFY,
L
Luming Yu 已提交
376
						     acpi_hotkey_notify_handler,
377
						     dev);
L
Luming Yu 已提交
378 379 380 381 382 383 384
	} else			/* Add polling hotkey */
		create_polling_proc(device);

	global_hotkey_list.count++;

	list_add_tail(&device->link.entries, global_hotkey_list.entries);

385
	return status;
L
Luming Yu 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398
}

static int hotkey_remove(union acpi_hotkey *device)
{
	struct list_head *entries, *next;


	list_for_each_safe(entries, next, global_hotkey_list.entries) {
		union acpi_hotkey *key =
		    container_of(entries, union acpi_hotkey, entries);
		if (key->link.hotkey_standard_num ==
		    device->link.hotkey_standard_num) {
			list_del(&key->link.entries);
399
			free_hotkey_device(key);
L
Luming Yu 已提交
400 401 402 403
			global_hotkey_list.count--;
			break;
		}
	}
404
	kfree(device);
405
	return 0;
L
Luming Yu 已提交
406 407
}

L
Len Brown 已提交
408
static int hotkey_update(union acpi_hotkey *key)
L
Luming Yu 已提交
409
{
410
	struct list_head *entries;
L
Luming Yu 已提交
411 412


413
	list_for_each(entries, global_hotkey_list.entries) {
L
Len Brown 已提交
414
		union acpi_hotkey *tmp =
L
Luming Yu 已提交
415
		    container_of(entries, union acpi_hotkey, entries);
416
		if (tmp->link.hotkey_standard_num ==
L
Luming Yu 已提交
417
		    key->link.hotkey_standard_num) {
418 419 420
			if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
				free_hotkey_buffer(tmp);
				tmp->event_hotkey.bus_handle =
L
Len Brown 已提交
421
				    key->event_hotkey.bus_handle;
422
				tmp->event_hotkey.external_hotkey_num =
L
Len Brown 已提交
423
				    key->event_hotkey.external_hotkey_num;
424
				tmp->event_hotkey.action_handle =
L
Len Brown 已提交
425
				    key->event_hotkey.action_handle;
426
				tmp->event_hotkey.action_method =
L
Len Brown 已提交
427
				    key->event_hotkey.action_method;
428 429 430
				kfree(key);
			} else {
				/*
L
Len Brown 已提交
431
				   char  proc_name[80];
432

L
Len Brown 已提交
433 434 435 436
				   sprintf(proc_name, "%d", tmp->link.hotkey_standard_num);
				   strcat(proc_name, tmp->poll_hotkey.poll_method);
				   remove_proc_entry(proc_name,hotkey_proc_dir);
				 */
437 438
				free_poll_hotkey_buffer(tmp);
				tmp->poll_hotkey.poll_handle =
L
Len Brown 已提交
439
				    key->poll_hotkey.poll_handle;
440
				tmp->poll_hotkey.poll_method =
L
Len Brown 已提交
441
				    key->poll_hotkey.poll_method;
442
				tmp->poll_hotkey.action_handle =
L
Len Brown 已提交
443
				    key->poll_hotkey.action_handle;
444
				tmp->poll_hotkey.action_method =
L
Len Brown 已提交
445
				    key->poll_hotkey.action_method;
446
				tmp->poll_hotkey.poll_result =
L
Len Brown 已提交
447
				    key->poll_hotkey.poll_result;
448
				/*
L
Len Brown 已提交
449 450
				   create_polling_proc(tmp);
				 */
451 452
				kfree(key);
			}
453
			return 0;
L
Luming Yu 已提交
454 455 456 457
			break;
		}
	}

458
	return -ENODEV;
L
Luming Yu 已提交
459 460 461 462 463 464 465 466
}

static void free_hotkey_device(union acpi_hotkey *key)
{
	struct acpi_device *dev;


	if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
467
		acpi_bus_get_device(key->event_hotkey.bus_handle, &dev);
L
Luming Yu 已提交
468 469
		if (dev->handle)
			acpi_remove_notify_handler(dev->handle,
470
						   ACPI_DEVICE_NOTIFY,
L
Luming Yu 已提交
471
						   acpi_hotkey_notify_handler);
472 473
		free_hotkey_buffer(key);
	} else {
L
Len Brown 已提交
474
		char proc_name[80];
475 476 477

		sprintf(proc_name, "%d", key->link.hotkey_standard_num);
		/*
L
Len Brown 已提交
478 479 480
		   strcat(proc_name, key->poll_hotkey.poll_method);
		 */
		remove_proc_entry(proc_name, hotkey_proc_dir);
481 482
		free_poll_hotkey_buffer(key);
	}
L
Luming Yu 已提交
483
	kfree(key);
484
	return;
L
Luming Yu 已提交
485 486
}

L
Len Brown 已提交
487
static void free_hotkey_buffer(union acpi_hotkey *key)
488 489 490 491
{
	kfree(key->event_hotkey.action_method);
}

L
Len Brown 已提交
492
static void free_poll_hotkey_buffer(union acpi_hotkey *key)
493 494 495 496 497
{
	kfree(key->poll_hotkey.action_method);
	kfree(key->poll_hotkey.poll_method);
	kfree(key->poll_hotkey.poll_result);
}
L
Luming Yu 已提交
498 499 500 501
static int
init_hotkey_device(union acpi_hotkey *key, char *bus_str, char *action_str,
		   char *method, int std_num, int external_num)
{
L
Len Brown 已提交
502
	acpi_handle tmp_handle;
503 504
	acpi_status status = AE_OK;

L
Luming Yu 已提交
505

L
Len Brown 已提交
506
	if (std_num < 0 || IS_POLL(std_num) || !key)
507 508
		goto do_fail;

L
Len Brown 已提交
509
	if (!bus_str || !action_str || !method)
510 511
		goto do_fail;

L
Luming Yu 已提交
512 513 514
	key->link.hotkey_type = ACPI_HOTKEY_EVENT;
	key->link.hotkey_standard_num = std_num;
	key->event_hotkey.flag = 0;
515
	key->event_hotkey.action_method = method;
L
Luming Yu 已提交
516

L
Len Brown 已提交
517 518 519
	status =
	    acpi_get_handle(NULL, bus_str, &(key->event_hotkey.bus_handle));
	if (ACPI_FAILURE(status))
520 521
		goto do_fail;
	key->event_hotkey.external_hotkey_num = external_num;
L
Len Brown 已提交
522 523 524 525
	status =
	    acpi_get_handle(NULL, action_str,
			    &(key->event_hotkey.action_handle));
	if (ACPI_FAILURE(status))
526 527
		goto do_fail;
	status = acpi_get_handle(key->event_hotkey.action_handle,
L
Len Brown 已提交
528
				 method, &tmp_handle);
529 530
	if (ACPI_FAILURE(status))
		goto do_fail;
531
	return AE_OK;
L
Len Brown 已提交
532
      do_fail:
533
	return -ENODEV;
L
Luming Yu 已提交
534 535 536 537 538 539 540 541
}

static int
init_poll_hotkey_device(union acpi_hotkey *key,
			char *poll_str,
			char *poll_method,
			char *action_str, char *action_method, int std_num)
{
542
	acpi_status status = AE_OK;
L
Len Brown 已提交
543
	acpi_handle tmp_handle;
544

L
Luming Yu 已提交
545

L
Len Brown 已提交
546
	if (std_num < 0 || IS_EVENT(std_num) || !key)
547 548
		goto do_fail;

L
Len Brown 已提交
549
	if (!poll_str || !poll_method || !action_str || !action_method)
550 551
		goto do_fail;

L
Luming Yu 已提交
552 553 554 555
	key->link.hotkey_type = ACPI_HOTKEY_POLLING;
	key->link.hotkey_standard_num = std_num;
	key->poll_hotkey.flag = 0;
	key->poll_hotkey.poll_method = poll_method;
556 557
	key->poll_hotkey.action_method = action_method;

L
Len Brown 已提交
558 559 560
	status =
	    acpi_get_handle(NULL, poll_str, &(key->poll_hotkey.poll_handle));
	if (ACPI_FAILURE(status))
561 562
		goto do_fail;
	status = acpi_get_handle(key->poll_hotkey.poll_handle,
L
Len Brown 已提交
563 564 565 566 567 568
				 poll_method, &tmp_handle);
	if (ACPI_FAILURE(status))
		goto do_fail;
	status =
	    acpi_get_handle(NULL, action_str,
			    &(key->poll_hotkey.action_handle));
569 570 571
	if (ACPI_FAILURE(status))
		goto do_fail;
	status = acpi_get_handle(key->poll_hotkey.action_handle,
L
Len Brown 已提交
572
				 action_method, &tmp_handle);
573 574
	if (ACPI_FAILURE(status))
		goto do_fail;
L
Luming Yu 已提交
575 576
	key->poll_hotkey.poll_result =
	    (union acpi_object *)kmalloc(sizeof(union acpi_object), GFP_KERNEL);
L
Len Brown 已提交
577
	if (!key->poll_hotkey.poll_result)
578
		goto do_fail;
579
	return AE_OK;
L
Len Brown 已提交
580
      do_fail:
581
	return -ENODEV;
L
Luming Yu 已提交
582 583 584 585
}

static int hotkey_open_config(struct inode *inode, struct file *file)
{
586
	return (single_open
L
Luming Yu 已提交
587 588 589
		     (file, hotkey_config_seq_show, PDE(inode)->data));
}

590 591
static int hotkey_poll_open_config(struct inode *inode, struct file *file)
{
592
	return (single_open
593 594 595
		     (file, hotkey_poll_config_seq_show, PDE(inode)->data));
}

L
Luming Yu 已提交
596 597 598
static int hotkey_config_seq_show(struct seq_file *seq, void *offset)
{
	struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
599
	struct list_head *entries;
L
Luming Yu 已提交
600 601 602 603 604 605
	char bus_name[ACPI_PATHNAME_MAX] = { 0 };
	char action_name[ACPI_PATHNAME_MAX] = { 0 };
	struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
	struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };


606
	list_for_each(entries, hotkey_list->entries) {
L
Luming Yu 已提交
607 608 609 610 611 612 613
		union acpi_hotkey *key =
		    container_of(entries, union acpi_hotkey, entries);
		if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
			acpi_get_name(key->event_hotkey.bus_handle,
				      ACPI_NAME_TYPE_MAX, &bus);
			acpi_get_name(key->event_hotkey.action_handle,
				      ACPI_NAME_TYPE_MAX, &act);
614
			seq_printf(seq, "%s:%s:%s:%d:%d\n", bus_name,
L
Luming Yu 已提交
615 616 617 618
				   action_name,
				   key->event_hotkey.action_method,
				   key->link.hotkey_standard_num,
				   key->event_hotkey.external_hotkey_num);
619 620 621
		}
	}
	seq_puts(seq, "\n");
622
	return 0;
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
}

static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset)
{
	struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
	struct list_head *entries;
	char bus_name[ACPI_PATHNAME_MAX] = { 0 };
	char action_name[ACPI_PATHNAME_MAX] = { 0 };
	struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
	struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };


	list_for_each(entries, hotkey_list->entries) {
		union acpi_hotkey *key =
		    container_of(entries, union acpi_hotkey, entries);
		if (key->link.hotkey_type == ACPI_HOTKEY_POLLING) {
L
Luming Yu 已提交
639 640 641 642
			acpi_get_name(key->poll_hotkey.poll_handle,
				      ACPI_NAME_TYPE_MAX, &bus);
			acpi_get_name(key->poll_hotkey.action_handle,
				      ACPI_NAME_TYPE_MAX, &act);
643
			seq_printf(seq, "%s:%s:%s:%s:%d\n", bus_name,
L
Luming Yu 已提交
644 645 646 647 648 649 650
				   key->poll_hotkey.poll_method,
				   action_name,
				   key->poll_hotkey.action_method,
				   key->link.hotkey_standard_num);
		}
	}
	seq_puts(seq, "\n");
651
	return 0;
L
Luming Yu 已提交
652 653 654 655 656
}

static int
get_parms(char *config_record,
	  int *cmd,
657 658 659 660
	  char **bus_handle,
	  char **bus_method,
	  char **action_handle,
	  char **method, int *internal_event_num, int *external_event_num)
L
Luming Yu 已提交
661
{
662
	char *tmp, *tmp1, count;
L
Luming Yu 已提交
663 664 665

	sscanf(config_record, "%d", cmd);

L
Len Brown 已提交
666 667 668
	if (*cmd == 1) {
		if (sscanf(config_record, "%d:%d", cmd, internal_event_num) !=
		    2)
669 670 671 672
			goto do_fail;
		else
			return (6);
	}
L
Luming Yu 已提交
673
	tmp = strchr(config_record, ':');
674 675
	if (!tmp)
		goto do_fail;
L
Luming Yu 已提交
676 677
	tmp++;
	tmp1 = strchr(tmp, ':');
678 679 680 681
	if (!tmp1)
		goto do_fail;

	count = tmp1 - tmp;
L
Len Brown 已提交
682 683
	*bus_handle = (char *)kmalloc(count + 1, GFP_KERNEL);
	if (!*bus_handle)
684 685 686
		goto do_fail;
	strncpy(*bus_handle, tmp, count);
	*(*bus_handle + count) = 0;
L
Luming Yu 已提交
687 688 689 690

	tmp = tmp1;
	tmp++;
	tmp1 = strchr(tmp, ':');
691 692 693
	if (!tmp1)
		goto do_fail;
	count = tmp1 - tmp;
L
Len Brown 已提交
694 695
	*bus_method = (char *)kmalloc(count + 1, GFP_KERNEL);
	if (!*bus_method)
696 697 698
		goto do_fail;
	strncpy(*bus_method, tmp, count);
	*(*bus_method + count) = 0;
L
Luming Yu 已提交
699 700 701 702

	tmp = tmp1;
	tmp++;
	tmp1 = strchr(tmp, ':');
703 704 705
	if (!tmp1)
		goto do_fail;
	count = tmp1 - tmp;
L
Len Brown 已提交
706
	*action_handle = (char *)kmalloc(count + 1, GFP_KERNEL);
707 708
	if (!*action_handle)
		goto do_fail;
709 710
	strncpy(*action_handle, tmp, count);
	*(*action_handle + count) = 0;
L
Luming Yu 已提交
711 712 713 714

	tmp = tmp1;
	tmp++;
	tmp1 = strchr(tmp, ':');
715 716 717
	if (!tmp1)
		goto do_fail;
	count = tmp1 - tmp;
L
Len Brown 已提交
718 719
	*method = (char *)kmalloc(count + 1, GFP_KERNEL);
	if (!*method)
720 721 722 723
		goto do_fail;
	strncpy(*method, tmp, count);
	*(*method + count) = 0;

L
Len Brown 已提交
724 725
	if (sscanf(tmp1 + 1, "%d:%d", internal_event_num, external_event_num) <=
	    0)
726
		goto do_fail;
L
Luming Yu 已提交
727

728
	return 6;
L
Len Brown 已提交
729
      do_fail:
730
	return -1;
L
Luming Yu 已提交
731 732 733 734 735 736 737
}

/*  count is length for one input record */
static ssize_t hotkey_write_config(struct file *file,
				   const char __user * buffer,
				   size_t count, loff_t * data)
{
738 739 740 741 742
	char *config_record = NULL;
	char *bus_handle = NULL;
	char *bus_method = NULL;
	char *action_handle = NULL;
	char *method = NULL;
L
Luming Yu 已提交
743 744 745 746 747
	int cmd, internal_event_num, external_event_num;
	int ret = 0;
	union acpi_hotkey *key = NULL;


L
Len Brown 已提交
748 749
	config_record = (char *)kmalloc(count + 1, GFP_KERNEL);
	if (!config_record)
750
		return -ENOMEM;
L
Luming Yu 已提交
751 752

	if (copy_from_user(config_record, buffer, count)) {
753
		kfree(config_record);
754
		printk(KERN_ERR PREFIX "Invalid data\n");
755
		return -EINVAL;
L
Luming Yu 已提交
756
	}
757
	config_record[count] = 0;
L
Luming Yu 已提交
758 759 760

	ret = get_parms(config_record,
			&cmd,
761 762 763 764 765 766
			&bus_handle,
			&bus_method,
			&action_handle,
			&method, &internal_event_num, &external_event_num);

	kfree(config_record);
L
Len Brown 已提交
767
	if (IS_OTHERS(internal_event_num))
768
		goto do_fail;
L
Luming Yu 已提交
769
	if (ret != 6) {
L
Len Brown 已提交
770
	      do_fail:
771 772 773 774
		kfree(bus_handle);
		kfree(bus_method);
		kfree(action_handle);
		kfree(method);
775
		printk(KERN_ERR PREFIX "Invalid data format ret=%d\n", ret);
776
		return -EINVAL;
L
Luming Yu 已提交
777 778 779
	}

	key = kmalloc(sizeof(union acpi_hotkey), GFP_KERNEL);
L
Len Brown 已提交
780
	if (!key)
781 782
		goto do_fail;
	memset(key, 0, sizeof(union acpi_hotkey));
L
Len Brown 已提交
783
	if (cmd == 1) {
784 785
		union acpi_hotkey *tmp = NULL;
		tmp = get_hotkey_by_event(&global_hotkey_list,
L
Len Brown 已提交
786 787
					  internal_event_num);
		if (!tmp)
788
			printk(KERN_ERR PREFIX "Invalid key\n");
789 790 791 792 793 794 795
		else
			memcpy(key, tmp, sizeof(union acpi_hotkey));
		goto cont_cmd;
	}
	if (IS_EVENT(internal_event_num)) {
		kfree(bus_method);
		ret = init_hotkey_device(key, bus_handle, action_handle, method,
L
Len Brown 已提交
796 797
					 internal_event_num,
					 external_event_num);
798 799
	} else
		ret = init_poll_hotkey_device(key, bus_handle, bus_method,
L
Len Brown 已提交
800 801
					      action_handle, method,
					      internal_event_num);
802 803 804
	if (ret) {
		kfree(bus_handle);
		kfree(action_handle);
L
Len Brown 已提交
805
		if (IS_EVENT(internal_event_num))
806 807 808
			free_hotkey_buffer(key);
		else
			free_poll_hotkey_buffer(key);
L
Luming Yu 已提交
809
		kfree(key);
810
		printk(KERN_ERR PREFIX "Invalid hotkey\n");
811
		return -EINVAL;
L
Luming Yu 已提交
812 813
	}

L
Len Brown 已提交
814
      cont_cmd:
815 816
	kfree(bus_handle);
	kfree(action_handle);
L
Luming Yu 已提交
817 818 819

	switch (cmd) {
	case 0:
L
Len Brown 已提交
820 821
		if (get_hotkey_by_event
		    (&global_hotkey_list, key->link.hotkey_standard_num))
822 823 824
			goto fail_out;
		else
			hotkey_add(key);
L
Luming Yu 已提交
825 826 827 828 829
		break;
	case 1:
		hotkey_remove(key);
		break;
	case 2:
L
Len Brown 已提交
830
		if (hotkey_update(key))
831
			goto fail_out;
L
Luming Yu 已提交
832 833
		break;
	default:
834
		goto fail_out;
L
Luming Yu 已提交
835 836
		break;
	}
837
	return count;
L
Len Brown 已提交
838 839
      fail_out:
	if (IS_EVENT(internal_event_num))
840 841 842 843
		free_hotkey_buffer(key);
	else
		free_poll_hotkey_buffer(key);
	kfree(key);
844
	printk(KERN_ERR PREFIX "invalid key\n");
845
	return -EINVAL;
L
Luming Yu 已提交
846 847
}

848
/*
L
Luming Yu 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
 * This function evaluates an ACPI method, given an int as parameter, the
 * method is searched within the scope of the handle, can be NULL. The output
 * of the method is written is output, which can also be NULL
 *
 * returns 1 if write is successful, 0 else.
 */
static int write_acpi_int(acpi_handle handle, const char *method, int val,
			  struct acpi_buffer *output)
{
	struct acpi_object_list params;	/* list of input parameters (an int here) */
	union acpi_object in_obj;	/* the only param we use */
	acpi_status status;

	params.count = 1;
	params.pointer = &in_obj;
	in_obj.type = ACPI_TYPE_INTEGER;
	in_obj.integer.value = val;

	status = acpi_evaluate_object(handle, (char *)method, &params, output);

869
	return (status == AE_OK);
L
Luming Yu 已提交
870 871
}

L
Len Brown 已提交
872 873
static int read_acpi_int(acpi_handle handle, const char *method,
			 union acpi_object *val)
L
Luming Yu 已提交
874 875 876 877 878 879 880 881 882
{
	struct acpi_buffer output;
	union acpi_object out_obj;
	acpi_status status;

	output.length = sizeof(out_obj);
	output.pointer = &out_obj;

	status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
L
Len Brown 已提交
883
	if (val) {
884 885 886
		val->integer.value = out_obj.integer.value;
		val->type = out_obj.type;
	} else
887
		printk(KERN_ERR PREFIX "null val pointer\n");
888
	return ((status == AE_OK)
L
Luming Yu 已提交
889 890 891
		     && (out_obj.type == ACPI_TYPE_INTEGER));
}

892
static union acpi_hotkey *get_hotkey_by_event(struct
L
Len Brown 已提交
893 894
					      acpi_hotkey_list
					      *hotkey_list, int event)
L
Luming Yu 已提交
895
{
896
	struct list_head *entries;
L
Luming Yu 已提交
897

898
	list_for_each(entries, hotkey_list->entries) {
L
Luming Yu 已提交
899 900
		union acpi_hotkey *key =
		    container_of(entries, union acpi_hotkey, entries);
901
		if (key->link.hotkey_standard_num == event) {
L
Len Brown 已提交
902
			return (key);
L
Luming Yu 已提交
903 904
		}
	}
L
Len Brown 已提交
905
	return (NULL);
L
Luming Yu 已提交
906 907
}

908
/*
L
Luming Yu 已提交
909 910 911 912 913 914 915 916 917 918 919 920
 * user call AML method interface:
 * Call convention:
 * echo "event_num: arg type : value"
 * example: echo "1:1:30" > /proc/acpi/action
 * Just support 1 integer arg passing to AML method
 */

static ssize_t hotkey_execute_aml_method(struct file *file,
					 const char __user * buffer,
					 size_t count, loff_t * data)
{
	struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
921
	char *arg;
L
Len Brown 已提交
922
	int event, method_type, type, value;
923
	union acpi_hotkey *key;
L
Luming Yu 已提交
924 925


L
Len Brown 已提交
926 927
	arg = (char *)kmalloc(count + 1, GFP_KERNEL);
	if (!arg)
928
		return -ENOMEM;
L
Len Brown 已提交
929
	arg[count] = 0;
L
Luming Yu 已提交
930 931

	if (copy_from_user(arg, buffer, count)) {
932
		kfree(arg);
933
		printk(KERN_ERR PREFIX "Invalid argument 2\n");
934
		return -EINVAL;
L
Luming Yu 已提交
935 936
	}

L
Len Brown 已提交
937 938
	if (sscanf(arg, "%d:%d:%d:%d", &event, &method_type, &type, &value) !=
	    4) {
939
		kfree(arg);
940
		printk(KERN_ERR PREFIX "Invalid argument 3\n");
941
		return -EINVAL;
L
Luming Yu 已提交
942
	}
943
	kfree(arg);
L
Luming Yu 已提交
944
	if (type == ACPI_TYPE_INTEGER) {
945
		key = get_hotkey_by_event(hotkey_list, event);
L
Len Brown 已提交
946
		if (!key)
947
			goto do_fail;
L
Luming Yu 已提交
948
		if (IS_EVENT(event))
949
			write_acpi_int(key->event_hotkey.action_handle,
L
Len Brown 已提交
950 951
				       key->event_hotkey.action_method, value,
				       NULL);
L
Luming Yu 已提交
952
		else if (IS_POLL(event)) {
L
Len Brown 已提交
953
			if (method_type == POLL_METHOD)
954
				read_acpi_int(key->poll_hotkey.poll_handle,
L
Len Brown 已提交
955 956 957
					      key->poll_hotkey.poll_method,
					      key->poll_hotkey.poll_result);
			else if (method_type == ACTION_METHOD)
958
				write_acpi_int(key->poll_hotkey.action_handle,
L
Len Brown 已提交
959 960
					       key->poll_hotkey.action_method,
					       value, NULL);
961 962 963
			else
				goto do_fail;

L
Luming Yu 已提交
964 965
		}
	} else {
966
		printk(KERN_WARNING "Not supported\n");
967
		return -EINVAL;
L
Luming Yu 已提交
968
	}
969
	return count;
L
Len Brown 已提交
970
      do_fail:
971
	return -EINVAL;
972

L
Luming Yu 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
}

static int __init hotkey_init(void)
{
	int result;
	mode_t mode = S_IFREG | S_IRUGO | S_IWUGO;


	if (acpi_disabled)
		return -ENODEV;

	if (acpi_specific_hotkey_enabled) {
		printk("Using specific hotkey driver\n");
		return -ENODEV;
	}

	hotkey_proc_dir = proc_mkdir(HOTKEY_PROC, acpi_root_dir);
	if (!hotkey_proc_dir) {
		return (-ENODEV);
	}
	hotkey_proc_dir->owner = THIS_MODULE;

	hotkey_config =
	    create_proc_entry(HOTKEY_EV_CONFIG, mode, hotkey_proc_dir);
	if (!hotkey_config) {
998
		goto do_fail1;
L
Luming Yu 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
	} else {
		hotkey_config->proc_fops = &hotkey_config_fops;
		hotkey_config->data = &global_hotkey_list;
		hotkey_config->owner = THIS_MODULE;
		hotkey_config->uid = 0;
		hotkey_config->gid = 0;
	}

	hotkey_poll_config =
	    create_proc_entry(HOTKEY_PL_CONFIG, mode, hotkey_proc_dir);
	if (!hotkey_poll_config) {
1010
		goto do_fail2;
L
Luming Yu 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	} else {
		hotkey_poll_config->proc_fops = &hotkey_poll_config_fops;
		hotkey_poll_config->data = &global_hotkey_list;
		hotkey_poll_config->owner = THIS_MODULE;
		hotkey_poll_config->uid = 0;
		hotkey_poll_config->gid = 0;
	}

	hotkey_action = create_proc_entry(HOTKEY_ACTION, mode, hotkey_proc_dir);
	if (!hotkey_action) {
1021
		goto do_fail3;
L
Luming Yu 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030
	} else {
		hotkey_action->proc_fops = &hotkey_action_fops;
		hotkey_action->owner = THIS_MODULE;
		hotkey_action->uid = 0;
		hotkey_action->gid = 0;
	}

	hotkey_info = create_proc_entry(HOTKEY_INFO, mode, hotkey_proc_dir);
	if (!hotkey_info) {
1031
		goto do_fail4;
L
Luming Yu 已提交
1032 1033 1034 1035 1036 1037 1038 1039
	} else {
		hotkey_info->proc_fops = &hotkey_info_fops;
		hotkey_info->owner = THIS_MODULE;
		hotkey_info->uid = 0;
		hotkey_info->gid = 0;
	}

	result = acpi_bus_register_driver(&hotkey_driver);
1040 1041
	if (result < 0)
		goto do_fail5;
L
Luming Yu 已提交
1042 1043 1044 1045 1046 1047
	global_hotkey_list.count = 0;
	global_hotkey_list.entries = &hotkey_entries;

	INIT_LIST_HEAD(&hotkey_entries);

	return (0);
1048

L
Len Brown 已提交
1049
      do_fail5:
1050
	remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
L
Len Brown 已提交
1051
      do_fail4:
1052
	remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
L
Len Brown 已提交
1053
      do_fail3:
1054
	remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
L
Len Brown 已提交
1055
      do_fail2:
1056
	remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
L
Len Brown 已提交
1057
      do_fail1:
1058 1059
	remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
	return (-ENODEV);
L
Luming Yu 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
}

static void __exit hotkey_exit(void)
{
	struct list_head *entries, *next;


	list_for_each_safe(entries, next, global_hotkey_list.entries) {
		union acpi_hotkey *key =
		    container_of(entries, union acpi_hotkey, entries);

		acpi_os_wait_events_complete(NULL);
		list_del(&key->link.entries);
		global_hotkey_list.count--;
		free_hotkey_device(key);
	}
	acpi_bus_unregister_driver(&hotkey_driver);
	remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
	remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
	remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
	remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
	remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
	return;
}

module_init(hotkey_init);
module_exit(hotkey_exit);