^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Input Power Event -> APM Bridge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2007 Richard Purdie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/apm-emulation.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static void system_power_event(unsigned int keycode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) switch (keycode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) case KEY_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) apm_queue_event(APM_USER_SUSPEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) pr_info("Requesting system suspend...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void apmpower_event(struct input_handle *handle, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* only react on key down events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (value != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) case EV_PWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) system_power_event(code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int apmpower_connect(struct input_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const struct input_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct input_handle *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) handle->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) handle->handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) handle->name = "apm-power";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) error = input_register_handle(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_err("Failed to register input power handler, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) kfree(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) error = input_open_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pr_err("Failed to open input power device, error %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) input_unregister_handle(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) kfree(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static void apmpower_disconnect(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) input_close_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) input_unregister_handle(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) kfree(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static const struct input_device_id apmpower_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .evbit = { BIT_MASK(EV_PWR) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) MODULE_DEVICE_TABLE(input, apmpower_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static struct input_handler apmpower_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .event = apmpower_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .connect = apmpower_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .disconnect = apmpower_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .name = "apm-power",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .id_table = apmpower_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int __init apmpower_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return input_register_handler(&apmpower_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void __exit apmpower_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) input_unregister_handler(&apmpower_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) module_init(apmpower_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) module_exit(apmpower_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) MODULE_DESCRIPTION("Input Power Event -> APM Bridge");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) MODULE_LICENSE("GPL");