0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/module.h>
0011 #include <linux/input.h>
0012 #include <linux/slab.h>
0013 #include <linux/init.h>
0014 #include <linux/tty.h>
0015 #include <linux/delay.h>
0016 #include <linux/pm.h>
0017 #include <linux/apm-emulation.h>
0018
0019 static void system_power_event(unsigned int keycode)
0020 {
0021 switch (keycode) {
0022 case KEY_SUSPEND:
0023 apm_queue_event(APM_USER_SUSPEND);
0024 pr_info("Requesting system suspend...\n");
0025 break;
0026 default:
0027 break;
0028 }
0029 }
0030
0031 static void apmpower_event(struct input_handle *handle, unsigned int type,
0032 unsigned int code, int value)
0033 {
0034
0035 if (value != 1)
0036 return;
0037
0038 switch (type) {
0039 case EV_PWR:
0040 system_power_event(code);
0041 break;
0042
0043 default:
0044 break;
0045 }
0046 }
0047
0048 static int apmpower_connect(struct input_handler *handler,
0049 struct input_dev *dev,
0050 const struct input_device_id *id)
0051 {
0052 struct input_handle *handle;
0053 int error;
0054
0055 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
0056 if (!handle)
0057 return -ENOMEM;
0058
0059 handle->dev = dev;
0060 handle->handler = handler;
0061 handle->name = "apm-power";
0062
0063 error = input_register_handle(handle);
0064 if (error) {
0065 pr_err("Failed to register input power handler, error %d\n",
0066 error);
0067 kfree(handle);
0068 return error;
0069 }
0070
0071 error = input_open_device(handle);
0072 if (error) {
0073 pr_err("Failed to open input power device, error %d\n", error);
0074 input_unregister_handle(handle);
0075 kfree(handle);
0076 return error;
0077 }
0078
0079 return 0;
0080 }
0081
0082 static void apmpower_disconnect(struct input_handle *handle)
0083 {
0084 input_close_device(handle);
0085 input_unregister_handle(handle);
0086 kfree(handle);
0087 }
0088
0089 static const struct input_device_id apmpower_ids[] = {
0090 {
0091 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
0092 .evbit = { BIT_MASK(EV_PWR) },
0093 },
0094 { },
0095 };
0096
0097 MODULE_DEVICE_TABLE(input, apmpower_ids);
0098
0099 static struct input_handler apmpower_handler = {
0100 .event = apmpower_event,
0101 .connect = apmpower_connect,
0102 .disconnect = apmpower_disconnect,
0103 .name = "apm-power",
0104 .id_table = apmpower_ids,
0105 };
0106
0107 static int __init apmpower_init(void)
0108 {
0109 return input_register_handler(&apmpower_handler);
0110 }
0111
0112 static void __exit apmpower_exit(void)
0113 {
0114 input_unregister_handler(&apmpower_handler);
0115 }
0116
0117 module_init(apmpower_init);
0118 module_exit(apmpower_exit);
0119
0120 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
0121 MODULE_DESCRIPTION("Input Power Event -> APM Bridge");
0122 MODULE_LICENSE("GPL");