blob: 650177a3c858ba13159bbed2dbc9148c66370ec3 [file] [log] [blame]
Richard Purdiee17bb1d2008-01-30 16:33:59 -05001/*
2 * Input Power Event -> APM Bridge
3 *
4 * Copyright (c) 2007 Richard Purdie
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
Joe Perchesda0c4902010-11-29 23:33:07 -080012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Richard Purdiee17bb1d2008-01-30 16:33:59 -050014#include <linux/module.h>
15#include <linux/input.h>
16#include <linux/slab.h>
17#include <linux/init.h>
18#include <linux/tty.h>
19#include <linux/delay.h>
20#include <linux/pm.h>
21#include <linux/apm-emulation.h>
22
23static void system_power_event(unsigned int keycode)
24{
25 switch (keycode) {
26 case KEY_SUSPEND:
27 apm_queue_event(APM_USER_SUSPEND);
Joe Perchesda0c4902010-11-29 23:33:07 -080028 pr_info("Requesting system suspend...\n");
Richard Purdiee17bb1d2008-01-30 16:33:59 -050029 break;
30 default:
31 break;
32 }
33}
34
35static void apmpower_event(struct input_handle *handle, unsigned int type,
Baodong Chen41091ad2012-07-29 22:33:03 -070036 unsigned int code, int value)
Richard Purdiee17bb1d2008-01-30 16:33:59 -050037{
38 /* only react on key down events */
39 if (value != 1)
40 return;
41
42 switch (type) {
43 case EV_PWR:
44 system_power_event(code);
45 break;
46
47 default:
48 break;
49 }
50}
51
52static int apmpower_connect(struct input_handler *handler,
53 struct input_dev *dev,
54 const struct input_device_id *id)
55{
56 struct input_handle *handle;
57 int error;
58
59 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
60 if (!handle)
61 return -ENOMEM;
62
63 handle->dev = dev;
64 handle->handler = handler;
65 handle->name = "apm-power";
66
Richard Purdiee17bb1d2008-01-30 16:33:59 -050067 error = input_register_handle(handle);
68 if (error) {
Joe Perchesda0c4902010-11-29 23:33:07 -080069 pr_err("Failed to register input power handler, error %d\n",
70 error);
Richard Purdiee17bb1d2008-01-30 16:33:59 -050071 kfree(handle);
72 return error;
73 }
74
75 error = input_open_device(handle);
76 if (error) {
Joe Perchesda0c4902010-11-29 23:33:07 -080077 pr_err("Failed to open input power device, error %d\n", error);
Richard Purdiee17bb1d2008-01-30 16:33:59 -050078 input_unregister_handle(handle);
79 kfree(handle);
80 return error;
81 }
82
83 return 0;
84}
85
Helge Deller481419e2008-03-24 11:02:06 -040086static void apmpower_disconnect(struct input_handle *handle)
Richard Purdiee17bb1d2008-01-30 16:33:59 -050087{
Richard Purdiee17bb1d2008-01-30 16:33:59 -050088 input_close_device(handle);
Helge Deller481419e2008-03-24 11:02:06 -040089 input_unregister_handle(handle);
Richard Purdiee17bb1d2008-01-30 16:33:59 -050090 kfree(handle);
91}
92
93static const struct input_device_id apmpower_ids[] = {
94 {
95 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
96 .evbit = { BIT_MASK(EV_PWR) },
97 },
98 { },
99};
100
101MODULE_DEVICE_TABLE(input, apmpower_ids);
102
103static struct input_handler apmpower_handler = {
104 .event = apmpower_event,
105 .connect = apmpower_connect,
106 .disconnect = apmpower_disconnect,
107 .name = "apm-power",
108 .id_table = apmpower_ids,
109};
110
111static int __init apmpower_init(void)
112{
113 return input_register_handler(&apmpower_handler);
114}
115
116static void __exit apmpower_exit(void)
117{
118 input_unregister_handler(&apmpower_handler);
119}
120
121module_init(apmpower_init);
122module_exit(apmpower_exit);
123
124MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
125MODULE_DESCRIPTION("Input Power Event -> APM Bridge");
126MODULE_LICENSE("GPL");