b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Button Hotplug driver |
| 3 | * |
| 4 | * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org> |
| 5 | * |
| 6 | * Based on the diag.c - GPIO interface driver for Broadcom boards |
| 7 | * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>, |
| 8 | * Copyright (C) 2006-2007 Felix Fietkau <nbd@nbd.name> |
| 9 | * Copyright (C) 2008 Andy Boyett <agb@openwrt.org> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 as published |
| 13 | * by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/version.h> |
| 18 | #include <linux/kmod.h> |
| 19 | #include <linux/input.h> |
| 20 | |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/netlink.h> |
| 24 | #include <linux/kobject.h> |
| 25 | |
| 26 | #define DRV_NAME "button-hotplug" |
| 27 | #define DRV_VERSION "0.4.1" |
| 28 | #define DRV_DESC "Button Hotplug driver" |
| 29 | |
| 30 | #define BH_SKB_SIZE 2048 |
| 31 | |
| 32 | #define PFX DRV_NAME ": " |
| 33 | |
| 34 | #undef BH_DEBUG |
| 35 | |
| 36 | #ifdef BH_DEBUG |
| 37 | #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args ) |
| 38 | #else |
| 39 | #define BH_DBG(fmt, args...) do {} while (0) |
| 40 | #endif |
| 41 | |
| 42 | #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args ) |
| 43 | |
| 44 | #ifndef BIT_MASK |
| 45 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) |
| 46 | #endif |
| 47 | |
| 48 | struct bh_priv { |
| 49 | unsigned long *seen; |
| 50 | struct input_handle handle; |
| 51 | }; |
| 52 | |
| 53 | struct bh_event { |
| 54 | const char *name; |
| 55 | char *action; |
| 56 | unsigned long seen; |
| 57 | |
| 58 | struct sk_buff *skb; |
| 59 | struct work_struct work; |
| 60 | }; |
| 61 | |
| 62 | struct bh_map { |
| 63 | unsigned int code; |
| 64 | const char *name; |
| 65 | }; |
| 66 | |
| 67 | extern u64 uevent_next_seqnum(void); |
| 68 | |
| 69 | #define BH_MAP(_code, _name) \ |
| 70 | { \ |
| 71 | .code = (_code), \ |
| 72 | .name = (_name), \ |
| 73 | } |
| 74 | |
| 75 | static struct bh_map button_map[] = { |
| 76 | BH_MAP(BTN_0, "BTN_0"), |
| 77 | BH_MAP(BTN_1, "BTN_1"), |
| 78 | BH_MAP(BTN_2, "BTN_2"), |
| 79 | BH_MAP(BTN_3, "BTN_3"), |
| 80 | BH_MAP(BTN_4, "BTN_4"), |
| 81 | BH_MAP(BTN_5, "BTN_5"), |
| 82 | BH_MAP(BTN_6, "BTN_6"), |
| 83 | BH_MAP(BTN_7, "BTN_7"), |
| 84 | BH_MAP(BTN_8, "BTN_8"), |
| 85 | BH_MAP(BTN_9, "BTN_9"), |
| 86 | BH_MAP(KEY_RESTART, "reset"), |
| 87 | BH_MAP(KEY_POWER, "power"), |
| 88 | BH_MAP(KEY_POWER2, "reboot"), |
| 89 | BH_MAP(KEY_RFKILL, "rfkill"), |
| 90 | BH_MAP(KEY_WPS_BUTTON, "wps"), |
| 91 | BH_MAP(KEY_WIMAX, "wwan"), |
| 92 | }; |
| 93 | |
| 94 | /* -------------------------------------------------------------------------*/ |
| 95 | |
| 96 | static int bh_event_add_var(struct bh_event *event, int argv, |
| 97 | const char *format, ...) |
| 98 | { |
| 99 | static char buf[128]; |
| 100 | char *s; |
| 101 | va_list args; |
| 102 | int len; |
| 103 | |
| 104 | if (argv) |
| 105 | return 0; |
| 106 | |
| 107 | va_start(args, format); |
| 108 | len = vsnprintf(buf, sizeof(buf), format, args); |
| 109 | va_end(args); |
| 110 | |
| 111 | if (len >= sizeof(buf)) { |
| 112 | BH_ERR("buffer size too small\n"); |
| 113 | WARN_ON(1); |
| 114 | return -ENOMEM; |
| 115 | } |
| 116 | |
| 117 | s = skb_put(event->skb, len + 1); |
| 118 | strcpy(s, buf); |
| 119 | |
| 120 | BH_DBG("added variable '%s'\n", s); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int button_hotplug_fill_event(struct bh_event *event) |
| 126 | { |
| 127 | int ret; |
| 128 | |
| 129 | ret = bh_event_add_var(event, 0, "HOME=%s", "/"); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | ret = bh_event_add_var(event, 0, "PATH=%s", |
| 134 | "/sbin:/bin:/usr/sbin:/usr/bin"); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
| 138 | ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button"); |
| 139 | if (ret) |
| 140 | return ret; |
| 141 | |
| 142 | ret = bh_event_add_var(event, 0, "ACTION=%s", event->action); |
| 143 | if (ret) |
| 144 | return ret; |
| 145 | |
| 146 | ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum()); |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static void button_hotplug_work(struct work_struct *work) |
| 160 | { |
| 161 | struct bh_event *event = container_of(work, struct bh_event, work); |
| 162 | int ret = 0; |
| 163 | |
| 164 | event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL); |
| 165 | if (!event->skb) |
| 166 | goto out_free_event; |
| 167 | |
| 168 | ret = bh_event_add_var(event, 0, "%s@", event->action); |
| 169 | if (ret) |
| 170 | goto out_free_skb; |
| 171 | |
| 172 | ret = button_hotplug_fill_event(event); |
| 173 | if (ret) |
| 174 | goto out_free_skb; |
| 175 | |
| 176 | NETLINK_CB(event->skb).dst_group = 1; |
| 177 | broadcast_uevent(event->skb, 0, 1, GFP_KERNEL); |
| 178 | |
| 179 | out_free_skb: |
| 180 | if (ret) { |
| 181 | BH_ERR("work error %d\n", ret); |
| 182 | kfree_skb(event->skb); |
| 183 | } |
| 184 | out_free_event: |
| 185 | kfree(event); |
| 186 | } |
| 187 | |
| 188 | static int button_hotplug_create_event(const char *name, unsigned long seen, |
| 189 | int pressed) |
| 190 | { |
| 191 | struct bh_event *event; |
| 192 | |
| 193 | BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n", |
| 194 | name, seen, pressed); |
| 195 | |
| 196 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 197 | if (!event) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | event->name = name; |
| 201 | event->seen = seen; |
| 202 | event->action = pressed ? "pressed" : "released"; |
| 203 | |
| 204 | INIT_WORK(&event->work, (void *)(void *)button_hotplug_work); |
| 205 | schedule_work(&event->work); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /* -------------------------------------------------------------------------*/ |
| 211 | |
| 212 | static int button_get_index(unsigned int code) |
| 213 | { |
| 214 | int i; |
| 215 | |
| 216 | for (i = 0; i < ARRAY_SIZE(button_map); i++) |
| 217 | if (button_map[i].code == code) |
| 218 | return i; |
| 219 | |
| 220 | return -1; |
| 221 | } |
| 222 | static void button_hotplug_event(struct input_handle *handle, |
| 223 | unsigned int type, unsigned int code, int value) |
| 224 | { |
| 225 | struct bh_priv *priv = handle->private; |
| 226 | unsigned long seen = jiffies; |
| 227 | int btn; |
| 228 | |
| 229 | BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value); |
| 230 | |
| 231 | if (type != EV_KEY) |
| 232 | return; |
| 233 | |
| 234 | btn = button_get_index(code); |
| 235 | if (btn < 0) |
| 236 | return; |
| 237 | |
| 238 | button_hotplug_create_event(button_map[btn].name, |
| 239 | (seen - priv->seen[btn]) / HZ, value); |
| 240 | priv->seen[btn] = seen; |
| 241 | } |
| 242 | |
| 243 | static int button_hotplug_connect(struct input_handler *handler, |
| 244 | struct input_dev *dev, const struct input_device_id *id) |
| 245 | { |
| 246 | struct bh_priv *priv; |
| 247 | int ret; |
| 248 | int i; |
| 249 | |
| 250 | for (i = 0; i < ARRAY_SIZE(button_map); i++) |
| 251 | if (test_bit(button_map[i].code, dev->keybit)) |
| 252 | break; |
| 253 | |
| 254 | if (i == ARRAY_SIZE(button_map)) |
| 255 | return -ENODEV; |
| 256 | |
| 257 | priv = kzalloc(sizeof(*priv) + |
| 258 | (sizeof(unsigned long) * ARRAY_SIZE(button_map)), |
| 259 | GFP_KERNEL); |
| 260 | if (!priv) |
| 261 | return -ENOMEM; |
| 262 | |
| 263 | priv->seen = (unsigned long *) &priv[1]; |
| 264 | priv->handle.private = priv; |
| 265 | priv->handle.dev = dev; |
| 266 | priv->handle.handler = handler; |
| 267 | priv->handle.name = DRV_NAME; |
| 268 | |
| 269 | ret = input_register_handle(&priv->handle); |
| 270 | if (ret) |
| 271 | goto err_free_priv; |
| 272 | |
| 273 | ret = input_open_device(&priv->handle); |
| 274 | if (ret) |
| 275 | goto err_unregister_handle; |
| 276 | |
| 277 | BH_DBG("connected to %s\n", dev->name); |
| 278 | |
| 279 | return 0; |
| 280 | |
| 281 | err_unregister_handle: |
| 282 | input_unregister_handle(&priv->handle); |
| 283 | |
| 284 | err_free_priv: |
| 285 | kfree(priv); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static void button_hotplug_disconnect(struct input_handle *handle) |
| 290 | { |
| 291 | struct bh_priv *priv = handle->private; |
| 292 | |
| 293 | input_close_device(handle); |
| 294 | input_unregister_handle(handle); |
| 295 | |
| 296 | kfree(priv); |
| 297 | } |
| 298 | |
| 299 | static const struct input_device_id button_hotplug_ids[] = { |
| 300 | { |
| 301 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT, |
| 302 | .evbit = { BIT_MASK(EV_KEY) }, |
| 303 | }, |
| 304 | { |
| 305 | /* Terminating entry */ |
| 306 | }, |
| 307 | }; |
| 308 | |
| 309 | MODULE_DEVICE_TABLE(input, button_hotplug_ids); |
| 310 | |
| 311 | static struct input_handler button_hotplug_handler = { |
| 312 | .event = button_hotplug_event, |
| 313 | .connect = button_hotplug_connect, |
| 314 | .disconnect = button_hotplug_disconnect, |
| 315 | .name = DRV_NAME, |
| 316 | .id_table = button_hotplug_ids, |
| 317 | }; |
| 318 | |
| 319 | /* -------------------------------------------------------------------------*/ |
| 320 | |
| 321 | static int __init button_hotplug_init(void) |
| 322 | { |
| 323 | int ret; |
| 324 | |
| 325 | printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n"); |
| 326 | ret = input_register_handler(&button_hotplug_handler); |
| 327 | if (ret) |
| 328 | BH_ERR("unable to register input handler\n"); |
| 329 | |
| 330 | return ret; |
| 331 | } |
| 332 | module_init(button_hotplug_init); |
| 333 | |
| 334 | static void __exit button_hotplug_exit(void) |
| 335 | { |
| 336 | input_unregister_handler(&button_hotplug_handler); |
| 337 | } |
| 338 | module_exit(button_hotplug_exit); |
| 339 | |
| 340 | MODULE_DESCRIPTION(DRV_DESC); |
| 341 | MODULE_VERSION(DRV_VERSION); |
| 342 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
| 343 | MODULE_LICENSE("GPL v2"); |
| 344 | |