aboutsummaryrefslogtreecommitdiffstats
path: root/package/kernel/button-hotplug/src/button-hotplug.c
blob: 41fdf3a256280336dd09fe0b0860c574c666c45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#
# Copyright (C) 2008 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

UCI_VERSION=0.7
UCI_RELEASE=5

PKG_NAME:=uci
PKG_VERSION:=$(UCI_VERSION)$(if $(UCI_RELEASE),.$(UCI_RELEASE))
PKG_RELEASE:=1

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=http://downloads.openwrt.org/sources
PKG_MD5SUM:=ed34c5ef606a90da4aba03ce6d22eeb9

include $(INCLUDE_DIR)/package.mk

# set to 1 to enable debugging
DEBUG=

define Package/libuci
  SECTION:=libs
  CATEGORY:=Libraries
  TITLE:=C library for the Unified Configuration Interface (UCI)
endef

define Package/uci
  SECTION:=base
  CATEGORY:=Base system
  DEPENDS:=+libuci
  TITLE:=Utility for the Unified Configuration Interface (UCI)
endef

define Package/ucitrigger
  SECTION:=base
  CATEGORY:=Base system
  DEPENDS:=+libuci-lua +lua
  TITLE:=Automatic triggers for applying system config changes
endef

define Package/libuci-lua
  SECTION=libs
  CATEGORY=Libraries
  DEPENDS:=+libuci +liblua
  TITLE:=Lua plugin for UCI
endef

define Build/Configure
endef

TARGET_CFLAGS += $(FPIC)
UCI_MAKEOPTS = \
		$(TARGET_CONFIGURE_OPTS) \
		COPTS="$(TARGET_CFLAGS)" \
		DEBUG="$(DEBUG)" \
		VERSION="$(UCI_VERSION)" \
		CPPFLAGS="-I$(PKG_BUILD_DIR) $(TARGET_CPPFLAGS)" \
		OS="Linux"

# work around a nasty gcc bug
ifneq ($(CONFIG_GCC_VERSION_4_2_4),)
  UCI_MAKEOPTS += WOPTS=""
endif

define Build/Compile
	$(MAKE) -C $(PKG_BUILD_DIR) $(UCI_MAKEOPTS)
	$(MAKE) -C $(PKG_BUILD_DIR)/lua $(UCI_MAKEOPTS)
	$(MAKE) -C $(PKG_BUILD_DIR)/trigger $(UCI_MAKEOPTS) \
		LIBS="$(TARGET_LDFLAGS) -L$(PKG_BUILD_DIR) -luci -llua -lcrypt -lm"
endef

define Package/libuci/install
	$(INSTALL_DIR) $(1)/lib
	$(CP) $(PKG_BUILD_DIR)/libuci.so* $(1)/lib/
endef

define Package/libuci-lua/install
	$(INSTALL_DIR) $(1)/usr/lib/lua
	$(CP) $(PKG_BUILD_DIR)/lua/uci.so $(1)/usr/lib/lua/
endef

define Package/ucitrigger/install
	$(INSTALL_DIR) $(1)/usr/lib/lua/uci $(1)/lib/config/trigger $(1)/usr/sbin
	$(INSTALL_DATA) ./trigger/lib/trigger.lua $(1)/usr/lib/lua/uci/
	$(INSTALL_DATA) ./trigger/modules/*.lua $(1)/lib/config/trigger/
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/trigger/uci_trigger.so $(1)/usr/lib/
	$(INSTALL_BIN) ./trigger/apply_config $(1)/usr/sbin/
endef

define Package/uci/install
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_DIR) $(1)/sbin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/uci $(1)/sbin/
	$(CP) ./files/* $(1)/
endef

define Build/InstallDev
	$(INSTALL_DIR) $(1)/usr/include
	$(CP) $(PKG_BUILD_DIR)/uci{,_config}.h $(1)/usr/include
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_BUILD_DIR)/libuci.so* $(1)/usr/lib
	$(CP) $(PKG_BUILD_DIR)/libuci.a $(1)/usr/lib
	$(CP) $(PKG_BUILD_DIR)/libucimap.a $(1)/usr/lib
endef

$(eval $(call BuildPackage,uci))
$(eval $(call BuildPackage,libuci))
$(eval $(call BuildPackage,libuci-lua))
$(eval $(call BuildPackage,ucitrigger))
fmt, DRV_NAME, ##args ) #else #define BH_DBG(fmt, args...) do {} while (0) #endif #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args ) #ifndef BIT_MASK #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) #endif struct bh_priv { unsigned long *seen; struct input_handle handle; }; struct bh_event { const char *name; char *action; unsigned long seen; struct sk_buff *skb; struct work_struct work; }; struct bh_map { unsigned int code; const char *name; }; extern u64 uevent_next_seqnum(void); #define BH_MAP(_code, _name) \ { \ .code = (_code), \ .name = (_name), \ } static struct bh_map button_map[] = { BH_MAP(BTN_0, "BTN_0"), BH_MAP(BTN_1, "BTN_1"), BH_MAP(BTN_2, "BTN_2"), BH_MAP(BTN_3, "BTN_3"), BH_MAP(BTN_4, "BTN_4"), BH_MAP(BTN_5, "BTN_5"), BH_MAP(BTN_6, "BTN_6"), BH_MAP(BTN_7, "BTN_7"), BH_MAP(BTN_8, "BTN_8"), BH_MAP(BTN_9, "BTN_9"), BH_MAP(KEY_RESTART, "reset"), BH_MAP(KEY_POWER, "power"), BH_MAP(KEY_RFKILL, "rfkill"), BH_MAP(KEY_WPS_BUTTON, "wps"), BH_MAP(KEY_WIMAX, "wwan"), }; /* -------------------------------------------------------------------------*/ static int bh_event_add_var(struct bh_event *event, int argv, const char *format, ...) { static char buf[128]; char *s; va_list args; int len; if (argv) return 0; va_start(args, format); len = vsnprintf(buf, sizeof(buf), format, args); va_end(args); if (len >= sizeof(buf)) { BH_ERR("buffer size too small\n"); WARN_ON(1); return -ENOMEM; } s = skb_put(event->skb, len + 1); strcpy(s, buf); BH_DBG("added variable '%s'\n", s); return 0; } static int button_hotplug_fill_event(struct bh_event *event) { int ret; ret = bh_event_add_var(event, 0, "HOME=%s", "/"); if (ret) return ret; ret = bh_event_add_var(event, 0, "PATH=%s", "/sbin:/bin:/usr/sbin:/usr/bin"); if (ret) return ret; ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button"); if (ret) return ret; ret = bh_event_add_var(event, 0, "ACTION=%s", event->action); if (ret) return ret; ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name); if (ret) return ret; ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen); if (ret) return ret; ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum()); return ret; } static void button_hotplug_work(struct work_struct *work) { struct bh_event *event = container_of(work, struct bh_event, work); int ret = 0; event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL); if (!event->skb) goto out_free_event; ret = bh_event_add_var(event, 0, "%s@", event->action); if (ret) goto out_free_skb; ret = button_hotplug_fill_event(event); if (ret) goto out_free_skb; NETLINK_CB(event->skb).dst_group = 1; broadcast_uevent(event->skb, 0, 1, GFP_KERNEL); out_free_skb: if (ret) { BH_ERR("work error %d\n", ret); kfree_skb(event->skb); } out_free_event: kfree(event); } static int button_hotplug_create_event(const char *name, unsigned long seen, int pressed) { struct bh_event *event; BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n", name, seen, pressed); event = kzalloc(sizeof(*event), GFP_KERNEL); if (!event) return -ENOMEM; event->name = name; event->seen = seen; event->action = pressed ? "pressed" : "released"; INIT_WORK(&event->work, (void *)(void *)button_hotplug_work); schedule_work(&event->work); return 0; } /* -------------------------------------------------------------------------*/ static int button_get_index(unsigned int code) { int i; for (i = 0; i < ARRAY_SIZE(button_map); i++) if (button_map[i].code == code) return i; return -1; } static void button_hotplug_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) { struct bh_priv *priv = handle->private; unsigned long seen = jiffies; int btn; BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value); if (type != EV_KEY) return; btn = button_get_index(code); if (btn < 0) return; button_hotplug_create_event(button_map[btn].name, (seen - priv->seen[btn]) / HZ, value); priv->seen[btn] = seen; } static int button_hotplug_connect(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id) { struct bh_priv *priv; int ret; int i; for (i = 0; i < ARRAY_SIZE(button_map); i++) if (test_bit(button_map[i].code, dev->keybit)) break; if (i == ARRAY_SIZE(button_map)) return -ENODEV; priv = kzalloc(sizeof(*priv) + (sizeof(unsigned long) * ARRAY_SIZE(button_map)), GFP_KERNEL); if (!priv) return -ENOMEM; priv->seen = (unsigned long *) &priv[1]; priv->handle.private = priv; priv->handle.dev = dev; priv->handle.handler = handler; priv->handle.name = DRV_NAME; ret = input_register_handle(&priv->handle); if (ret) goto err_free_priv; ret = input_open_device(&priv->handle); if (ret) goto err_unregister_handle; BH_DBG("connected to %s\n", dev->name); return 0; err_unregister_handle: input_unregister_handle(&priv->handle); err_free_priv: kfree(priv); return ret; } static void button_hotplug_disconnect(struct input_handle *handle) { struct bh_priv *priv = handle->private; input_close_device(handle); input_unregister_handle(handle); kfree(priv); } static const struct input_device_id button_hotplug_ids[] = { { .flags = INPUT_DEVICE_ID_MATCH_EVBIT, .evbit = { BIT_MASK(EV_KEY) }, }, { /* Terminating entry */ }, }; MODULE_DEVICE_TABLE(input, button_hotplug_ids); static struct input_handler button_hotplug_handler = { .event = button_hotplug_event, .connect = button_hotplug_connect, .disconnect = button_hotplug_disconnect, .name = DRV_NAME, .id_table = button_hotplug_ids, }; /* -------------------------------------------------------------------------*/ static int __init button_hotplug_init(void) { int ret; printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n"); ret = input_register_handler(&button_hotplug_handler); if (ret) BH_ERR("unable to register input handler\n"); return ret; } module_init(button_hotplug_init); static void __exit button_hotplug_exit(void) { input_unregister_handler(&button_hotplug_handler); } module_exit(button_hotplug_exit); MODULE_DESCRIPTION(DRV_DESC); MODULE_VERSION(DRV_VERSION); MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); MODULE_LICENSE("GPL v2");