xref: /linux/drivers/regulator/event.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
116e5ac12SNaresh Solanki // SPDX-License-Identifier: GPL-2.0
216e5ac12SNaresh Solanki /*
316e5ac12SNaresh Solanki  * Regulator event over netlink
416e5ac12SNaresh Solanki  *
516e5ac12SNaresh Solanki  * Author: Naresh Solanki <Naresh.Solanki@9elements.com>
616e5ac12SNaresh Solanki  */
716e5ac12SNaresh Solanki 
816e5ac12SNaresh Solanki #include <regulator/regulator.h>
916e5ac12SNaresh Solanki #include <net/netlink.h>
1016e5ac12SNaresh Solanki #include <net/genetlink.h>
11*1cadc04cSNaresh Solanki #include <linux/atomic.h>
1216e5ac12SNaresh Solanki 
1316e5ac12SNaresh Solanki #include "regnl.h"
1416e5ac12SNaresh Solanki 
15*1cadc04cSNaresh Solanki static atomic_t reg_event_seqnum = ATOMIC_INIT(0);
1616e5ac12SNaresh Solanki 
1716e5ac12SNaresh Solanki static const struct genl_multicast_group reg_event_mcgrps[] = {
1816e5ac12SNaresh Solanki 	{ .name = REG_GENL_MCAST_GROUP_NAME, },
1916e5ac12SNaresh Solanki };
2016e5ac12SNaresh Solanki 
2116e5ac12SNaresh Solanki static struct genl_family reg_event_genl_family __ro_after_init = {
2216e5ac12SNaresh Solanki 	.module = THIS_MODULE,
2316e5ac12SNaresh Solanki 	.name = REG_GENL_FAMILY_NAME,
2416e5ac12SNaresh Solanki 	.version = REG_GENL_VERSION,
2516e5ac12SNaresh Solanki 	.maxattr = REG_GENL_ATTR_MAX,
2616e5ac12SNaresh Solanki 	.mcgrps = reg_event_mcgrps,
2716e5ac12SNaresh Solanki 	.n_mcgrps = ARRAY_SIZE(reg_event_mcgrps),
2816e5ac12SNaresh Solanki };
2916e5ac12SNaresh Solanki 
reg_generate_netlink_event(const char * reg_name,u64 event)3016e5ac12SNaresh Solanki int reg_generate_netlink_event(const char *reg_name, u64 event)
3116e5ac12SNaresh Solanki {
3216e5ac12SNaresh Solanki 	struct sk_buff *skb;
3316e5ac12SNaresh Solanki 	struct nlattr *attr;
3416e5ac12SNaresh Solanki 	struct reg_genl_event *edata;
3516e5ac12SNaresh Solanki 	void *msg_header;
3616e5ac12SNaresh Solanki 	int size;
3716e5ac12SNaresh Solanki 
3816e5ac12SNaresh Solanki 	/* allocate memory */
3916e5ac12SNaresh Solanki 	size = nla_total_size(sizeof(struct reg_genl_event)) +
4016e5ac12SNaresh Solanki 	    nla_total_size(0);
4116e5ac12SNaresh Solanki 
4216e5ac12SNaresh Solanki 	skb = genlmsg_new(size, GFP_ATOMIC);
4316e5ac12SNaresh Solanki 	if (!skb)
4416e5ac12SNaresh Solanki 		return -ENOMEM;
4516e5ac12SNaresh Solanki 
4616e5ac12SNaresh Solanki 	/* add the genetlink message header */
47*1cadc04cSNaresh Solanki 	msg_header = genlmsg_put(skb, 0, atomic_inc_return(&reg_event_seqnum),
48*1cadc04cSNaresh Solanki 				 &reg_event_genl_family, 0, REG_GENL_CMD_EVENT);
4916e5ac12SNaresh Solanki 	if (!msg_header) {
5016e5ac12SNaresh Solanki 		nlmsg_free(skb);
5116e5ac12SNaresh Solanki 		return -ENOMEM;
5216e5ac12SNaresh Solanki 	}
5316e5ac12SNaresh Solanki 
5416e5ac12SNaresh Solanki 	/* fill the data */
5516e5ac12SNaresh Solanki 	attr = nla_reserve(skb, REG_GENL_ATTR_EVENT, sizeof(struct reg_genl_event));
5616e5ac12SNaresh Solanki 	if (!attr) {
5716e5ac12SNaresh Solanki 		nlmsg_free(skb);
5816e5ac12SNaresh Solanki 		return -EINVAL;
5916e5ac12SNaresh Solanki 	}
6016e5ac12SNaresh Solanki 
6116e5ac12SNaresh Solanki 	edata = nla_data(attr);
6216e5ac12SNaresh Solanki 	memset(edata, 0, sizeof(struct reg_genl_event));
6316e5ac12SNaresh Solanki 
6416e5ac12SNaresh Solanki 	strscpy(edata->reg_name, reg_name, sizeof(edata->reg_name));
6516e5ac12SNaresh Solanki 	edata->event = event;
6616e5ac12SNaresh Solanki 
6716e5ac12SNaresh Solanki 	/* send multicast genetlink message */
6816e5ac12SNaresh Solanki 	genlmsg_end(skb, msg_header);
6916e5ac12SNaresh Solanki 	size = genlmsg_multicast(&reg_event_genl_family, skb, 0, 0, GFP_ATOMIC);
7016e5ac12SNaresh Solanki 
7116e5ac12SNaresh Solanki 	return size;
7216e5ac12SNaresh Solanki }
7316e5ac12SNaresh Solanki 
reg_event_genetlink_init(void)7416e5ac12SNaresh Solanki static int __init reg_event_genetlink_init(void)
7516e5ac12SNaresh Solanki {
7616e5ac12SNaresh Solanki 	return genl_register_family(&reg_event_genl_family);
7716e5ac12SNaresh Solanki }
7816e5ac12SNaresh Solanki 
reg_event_init(void)7916e5ac12SNaresh Solanki static int __init reg_event_init(void)
8016e5ac12SNaresh Solanki {
8116e5ac12SNaresh Solanki 	int error;
8216e5ac12SNaresh Solanki 
8316e5ac12SNaresh Solanki 	/* create genetlink for acpi event */
8416e5ac12SNaresh Solanki 	error = reg_event_genetlink_init();
8516e5ac12SNaresh Solanki 	if (error)
8616e5ac12SNaresh Solanki 		pr_warn("Failed to create genetlink family for reg event\n");
8716e5ac12SNaresh Solanki 
8816e5ac12SNaresh Solanki 	return 0;
8916e5ac12SNaresh Solanki }
9016e5ac12SNaresh Solanki 
9116e5ac12SNaresh Solanki fs_initcall(reg_event_init);
92