blob: eaaccde8221013c4faec1ef932732d463139136f [file] [log] [blame]
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -08001/* drivers/input/keyreset.c
2 *
Daniel Rosenberg215234c2014-05-07 14:17:47 -07003 * Copyright (C) 2014 Google, Inc.
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -08004 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/input.h>
17#include <linux/keyreset.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/reboot.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/syscalls.h>
Daniel Rosenberg215234c2014-05-07 14:17:47 -070024#include <linux/keycombo.h>
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080025
26struct keyreset_state {
Daniel Rosenberg215234c2014-05-07 14:17:47 -070027 int restart_requested;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080028 int (*reset_fn)(void);
Daniel Rosenberg215234c2014-05-07 14:17:47 -070029 struct platform_device *pdev_child;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080030};
31
Daniel Rosenberg215234c2014-05-07 14:17:47 -070032static void do_restart(void)
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080033{
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080034 sys_sync();
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080035 kernel_restart(NULL);
36}
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080037
Daniel Rosenberg215234c2014-05-07 14:17:47 -070038static void do_reset_fn(void *priv)
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080039{
Daniel Rosenberg215234c2014-05-07 14:17:47 -070040 struct keyreset_state *state = priv;
41 if (state->restart_requested)
42 panic("keyboard reset failed, %d", state->restart_requested);
43 if (state->reset_fn) {
44 state->restart_requested = state->reset_fn();
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080045 } else {
Daniel Rosenberg215234c2014-05-07 14:17:47 -070046 pr_info("keyboard reset\n");
47 do_restart();
48 state->restart_requested = 1;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080049 }
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080050}
51
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080052static int keyreset_probe(struct platform_device *pdev)
53{
Daniel Rosenberg215234c2014-05-07 14:17:47 -070054 int ret = -ENOMEM;
55 struct keycombo_platform_data *pdata_child;
56 struct keyreset_platform_data *pdata = pdev->dev.platform_data;
57 int up_size = 0, down_size = 0, size;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080058 int key, *keyp;
59 struct keyreset_state *state;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080060
61 if (!pdata)
62 return -EINVAL;
Daniel Rosenberg215234c2014-05-07 14:17:47 -070063 state = devm_kzalloc(&pdev->dev, sizeof(*state), GFP_KERNEL);
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080064 if (!state)
65 return -ENOMEM;
66
Daniel Rosenberg215234c2014-05-07 14:17:47 -070067 state->pdev_child = platform_device_alloc(KEYCOMBO_NAME,
68 PLATFORM_DEVID_AUTO);
69 if (!state->pdev_child)
70 return -ENOMEM;
71 state->pdev_child->dev.parent = &pdev->dev;
72
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080073 keyp = pdata->keys_down;
74 while ((key = *keyp++)) {
75 if (key >= KEY_MAX)
76 continue;
Daniel Rosenberg215234c2014-05-07 14:17:47 -070077 down_size++;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080078 }
79 if (pdata->keys_up) {
80 keyp = pdata->keys_up;
81 while ((key = *keyp++)) {
82 if (key >= KEY_MAX)
83 continue;
Daniel Rosenberg215234c2014-05-07 14:17:47 -070084 up_size++;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -080085 }
86 }
Daniel Rosenberg215234c2014-05-07 14:17:47 -070087 size = sizeof(struct keycombo_platform_data)
88 + sizeof(int) * (down_size + 1);
89 pdata_child = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
90 if (!pdata_child)
91 goto error;
92 memcpy(pdata_child->keys_down, pdata->keys_down,
93 sizeof(int) * down_size);
94 if (up_size > 0) {
95 pdata_child->keys_up = devm_kzalloc(&pdev->dev, up_size + 1,
96 GFP_KERNEL);
97 if (!pdata_child->keys_up)
98 goto error;
99 memcpy(pdata_child->keys_up, pdata->keys_up,
100 sizeof(int) * up_size);
101 if (!pdata_child->keys_up)
102 goto error;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -0800103 }
Daniel Rosenberg215234c2014-05-07 14:17:47 -0700104 state->reset_fn = pdata->reset_fn;
105 pdata_child->key_down_fn = do_reset_fn;
106 pdata_child->priv = state;
107 pdata_child->key_down_delay = pdata->key_down_delay;
108 ret = platform_device_add_data(state->pdev_child, pdata_child, size);
109 if (ret)
110 goto error;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -0800111 platform_set_drvdata(pdev, state);
Daniel Rosenberg215234c2014-05-07 14:17:47 -0700112 return platform_device_add(state->pdev_child);
113error:
114 platform_device_put(state->pdev_child);
115 return ret;
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -0800116}
117
118int keyreset_remove(struct platform_device *pdev)
119{
120 struct keyreset_state *state = platform_get_drvdata(pdev);
Daniel Rosenberg215234c2014-05-07 14:17:47 -0700121 platform_device_put(state->pdev_child);
Arve Hjønnevåg6ca5fa02008-11-21 21:47:23 -0800122 return 0;
123}
124
125
126struct platform_driver keyreset_driver = {
127 .driver.name = KEYRESET_NAME,
128 .probe = keyreset_probe,
129 .remove = keyreset_remove,
130};
131
132static int __init keyreset_init(void)
133{
134 return platform_driver_register(&keyreset_driver);
135}
136
137static void __exit keyreset_exit(void)
138{
139 return platform_driver_unregister(&keyreset_driver);
140}
141
142module_init(keyreset_init);
143module_exit(keyreset_exit);