button-hotplug.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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_RFKILL, "rfkill"),
  73. BH_MAP(KEY_WPS_BUTTON, "wps"),
  74. BH_MAP(KEY_WIMAX, "wwan"),
  75. };
  76. /* -------------------------------------------------------------------------*/
  77. static int bh_event_add_var(struct bh_event *event, int argv,
  78. const char *format, ...)
  79. {
  80. static char buf[128];
  81. char *s;
  82. va_list args;
  83. int len;
  84. if (argv)
  85. return 0;
  86. va_start(args, format);
  87. len = vsnprintf(buf, sizeof(buf), format, args);
  88. va_end(args);
  89. if (len >= sizeof(buf)) {
  90. BH_ERR("buffer size too small\n");
  91. WARN_ON(1);
  92. return -ENOMEM;
  93. }
  94. s = skb_put(event->skb, len + 1);
  95. strcpy(s, buf);
  96. BH_DBG("added variable '%s'\n", s);
  97. return 0;
  98. }
  99. static int button_hotplug_fill_event(struct bh_event *event)
  100. {
  101. int ret;
  102. ret = bh_event_add_var(event, 0, "HOME=%s", "/");
  103. if (ret)
  104. return ret;
  105. ret = bh_event_add_var(event, 0, "PATH=%s",
  106. "/sbin:/bin:/usr/sbin:/usr/bin");
  107. if (ret)
  108. return ret;
  109. ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
  110. if (ret)
  111. return ret;
  112. ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
  113. if (ret)
  114. return ret;
  115. ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
  116. if (ret)
  117. return ret;
  118. ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
  119. if (ret)
  120. return ret;
  121. ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
  122. return ret;
  123. }
  124. static void button_hotplug_work(struct work_struct *work)
  125. {
  126. struct bh_event *event = container_of(work, struct bh_event, work);
  127. int ret = 0;
  128. event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
  129. if (!event->skb)
  130. goto out_free_event;
  131. ret = bh_event_add_var(event, 0, "%s@", event->action);
  132. if (ret)
  133. goto out_free_skb;
  134. ret = button_hotplug_fill_event(event);
  135. if (ret)
  136. goto out_free_skb;
  137. NETLINK_CB(event->skb).dst_group = 1;
  138. broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
  139. out_free_skb:
  140. if (ret) {
  141. BH_ERR("work error %d\n", ret);
  142. kfree_skb(event->skb);
  143. }
  144. out_free_event:
  145. kfree(event);
  146. }
  147. static int button_hotplug_create_event(const char *name, unsigned long seen,
  148. int pressed)
  149. {
  150. struct bh_event *event;
  151. BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
  152. name, seen, pressed);
  153. event = kzalloc(sizeof(*event), GFP_KERNEL);
  154. if (!event)
  155. return -ENOMEM;
  156. event->name = name;
  157. event->seen = seen;
  158. event->action = pressed ? "pressed" : "released";
  159. INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
  160. schedule_work(&event->work);
  161. return 0;
  162. }
  163. /* -------------------------------------------------------------------------*/
  164. static int button_get_index(unsigned int code)
  165. {
  166. int i;
  167. for (i = 0; i < ARRAY_SIZE(button_map); i++)
  168. if (button_map[i].code == code)
  169. return i;
  170. return -1;
  171. }
  172. static void button_hotplug_event(struct input_handle *handle,
  173. unsigned int type, unsigned int code, int value)
  174. {
  175. struct bh_priv *priv = handle->private;
  176. unsigned long seen = jiffies;
  177. int btn;
  178. BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);
  179. if (type != EV_KEY)
  180. return;
  181. btn = button_get_index(code);
  182. if (btn < 0)
  183. return;
  184. button_hotplug_create_event(button_map[btn].name,
  185. (seen - priv->seen[btn]) / HZ, value);
  186. priv->seen[btn] = seen;
  187. }
  188. static int button_hotplug_connect(struct input_handler *handler,
  189. struct input_dev *dev, const struct input_device_id *id)
  190. {
  191. struct bh_priv *priv;
  192. int ret;
  193. int i;
  194. for (i = 0; i < ARRAY_SIZE(button_map); i++)
  195. if (test_bit(button_map[i].code, dev->keybit))
  196. break;
  197. if (i == ARRAY_SIZE(button_map))
  198. return -ENODEV;
  199. priv = kzalloc(sizeof(*priv) +
  200. (sizeof(unsigned long) * ARRAY_SIZE(button_map)),
  201. GFP_KERNEL);
  202. if (!priv)
  203. return -ENOMEM;
  204. priv->seen = (unsigned long *) &priv[1];
  205. priv->handle.private = priv;
  206. priv->handle.dev = dev;
  207. priv->handle.handler = handler;
  208. priv->handle.name = DRV_NAME;
  209. ret = input_register_handle(&priv->handle);
  210. if (ret)
  211. goto err_free_priv;
  212. ret = input_open_device(&priv->handle);
  213. if (ret)
  214. goto err_unregister_handle;
  215. BH_DBG("connected to %s\n", dev->name);
  216. return 0;
  217. err_unregister_handle:
  218. input_unregister_handle(&priv->handle);
  219. err_free_priv:
  220. kfree(priv);
  221. return ret;
  222. }
  223. static void button_hotplug_disconnect(struct input_handle *handle)
  224. {
  225. struct bh_priv *priv = handle->private;
  226. input_close_device(handle);
  227. input_unregister_handle(handle);
  228. kfree(priv);
  229. }
  230. static const struct input_device_id button_hotplug_ids[] = {
  231. {
  232. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  233. .evbit = { BIT_MASK(EV_KEY) },
  234. },
  235. {
  236. /* Terminating entry */
  237. },
  238. };
  239. MODULE_DEVICE_TABLE(input, button_hotplug_ids);
  240. static struct input_handler button_hotplug_handler = {
  241. .event = button_hotplug_event,
  242. .connect = button_hotplug_connect,
  243. .disconnect = button_hotplug_disconnect,
  244. .name = DRV_NAME,
  245. .id_table = button_hotplug_ids,
  246. };
  247. /* -------------------------------------------------------------------------*/
  248. static int __init button_hotplug_init(void)
  249. {
  250. int ret;
  251. printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
  252. ret = input_register_handler(&button_hotplug_handler);
  253. if (ret)
  254. BH_ERR("unable to register input handler\n");
  255. return ret;
  256. }
  257. module_init(button_hotplug_init);
  258. static void __exit button_hotplug_exit(void)
  259. {
  260. input_unregister_handler(&button_hotplug_handler);
  261. }
  262. module_exit(button_hotplug_exit);
  263. MODULE_DESCRIPTION(DRV_DESC);
  264. MODULE_VERSION(DRV_VERSION);
  265. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  266. MODULE_LICENSE("GPL v2");