Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / staging / gdm724x / netlink_k.c
blob77fc64e28428f7f6586c3e840435add348c3936a
1 /*
2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/export.h>
17 #include <linux/etherdevice.h>
18 #include <linux/netlink.h>
19 #include <asm/byteorder.h>
20 #include <net/sock.h>
22 #include "netlink_k.h"
24 #if defined(DEFINE_MUTEX)
25 static DEFINE_MUTEX(netlink_mutex);
26 #else
27 static struct semaphore netlink_mutex;
28 #define mutex_lock(x) down(x)
29 #define mutex_unlock(x) up(x)
30 #endif
32 #define ND_MAX_GROUP 30
33 #define ND_IFINDEX_LEN sizeof(int)
34 #define ND_NLMSG_SPACE(len) (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
35 #define ND_NLMSG_DATA(nlh) ((void *)((char *)NLMSG_DATA(nlh) + ND_IFINDEX_LEN))
36 #define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN)
37 #define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN)
38 #define ND_NLMSG_IFIDX(nlh) NLMSG_DATA(nlh)
39 #define ND_MAX_MSG_LEN (1024 * 32)
41 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
43 static void netlink_rcv_cb(struct sk_buff *skb)
45 struct nlmsghdr *nlh;
46 struct net_device *dev;
47 u32 mlen;
48 void *msg;
49 int ifindex;
51 if (!rcv_cb) {
52 pr_err("nl cb - unregistered\n");
53 return;
56 if (skb->len < NLMSG_SPACE(0)) {
57 pr_err("nl cb - invalid skb length\n");
58 return;
61 nlh = (struct nlmsghdr *)skb->data;
63 if (skb->len < nlh->nlmsg_len || nlh->nlmsg_len > ND_MAX_MSG_LEN) {
64 pr_err("nl cb - invalid length (%d,%d)\n",
65 skb->len, nlh->nlmsg_len);
66 return;
69 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
70 msg = ND_NLMSG_DATA(nlh);
71 mlen = ND_NLMSG_R_LEN(nlh);
73 dev = dev_get_by_index(&init_net, ifindex);
74 if (dev) {
75 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
76 dev_put(dev);
77 } else {
78 pr_err("nl cb - dev (%d) not found\n", ifindex);
82 static void netlink_rcv(struct sk_buff *skb)
84 mutex_lock(&netlink_mutex);
85 netlink_rcv_cb(skb);
86 mutex_unlock(&netlink_mutex);
89 struct sock *netlink_init(int unit,
90 void (*cb)(struct net_device *dev, u16 type, void *msg, int len))
92 struct sock *sock;
93 struct netlink_kernel_cfg cfg = {
94 .input = netlink_rcv,
97 #if !defined(DEFINE_MUTEX)
98 init_MUTEX(&netlink_mutex);
99 #endif
101 sock = netlink_kernel_create(&init_net, unit, &cfg);
103 if (sock)
104 rcv_cb = cb;
106 return sock;
109 void netlink_exit(struct sock *sock)
111 sock_release(sock->sk_socket);
114 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
116 static u32 seq;
117 struct sk_buff *skb = NULL;
118 struct nlmsghdr *nlh;
119 int ret = 0;
121 if (group > ND_MAX_GROUP)
122 return -EINVAL;
124 if (!netlink_has_listeners(sock, group+1))
125 return -ESRCH;
127 skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
128 if (!skb)
129 return -ENOMEM;
131 seq++;
133 nlh = nlmsg_put(skb, 0, seq, type, len, 0);
134 memcpy(NLMSG_DATA(nlh), msg, len);
135 NETLINK_CB(skb).portid = 0;
136 NETLINK_CB(skb).dst_group = 0;
138 ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
139 if (!ret)
140 return len;
142 if (ret != -ESRCH)
143 pr_err("nl broadcast g=%d, t=%d, l=%d, r=%d\n",
144 group, type, len, ret);
145 else if (netlink_has_listeners(sock, group+1))
146 return -EAGAIN;
148 return ret;