button-hotplug.c 7.4 KB

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