button-hotplug.c 7.3 KB

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