KVM: x86: update KVM_SAVE_MSRS_BEGIN to correct value
[cascardo/linux.git] / drivers / staging / gdm72xx / netlink_k.c
1 /*
2  * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3  *
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.
7  *
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.
12  */
13
14 #include <linux/version.h>
15 #include <linux/module.h>
16 #include <linux/etherdevice.h>
17 #include <linux/netlink.h>
18 #include <asm/byteorder.h>
19 #include <net/sock.h>
20
21 #if !defined(NLMSG_HDRLEN)
22 #define NLMSG_HDRLEN     ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
23 #endif
24
25 #define ND_MAX_GROUP                    30
26 #define ND_IFINDEX_LEN                  sizeof(int)
27 #define ND_NLMSG_SPACE(len)             (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
28 #define ND_NLMSG_DATA(nlh) \
29         ((void *)((char *)NLMSG_DATA(nlh) + ND_IFINDEX_LEN))
30 #define ND_NLMSG_S_LEN(len)             (len+ND_IFINDEX_LEN)
31 #define ND_NLMSG_R_LEN(nlh)             (nlh->nlmsg_len-ND_IFINDEX_LEN)
32 #define ND_NLMSG_IFIDX(nlh)             NLMSG_DATA(nlh)
33 #define ND_MAX_MSG_LEN                  8096
34
35 #if defined(DEFINE_MUTEX)
36 static DEFINE_MUTEX(netlink_mutex);
37 #else
38 static struct semaphore netlink_mutex;
39 #define mutex_lock(x)           down(x)
40 #define mutex_unlock(x)         up(x)
41 #endif
42
43 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
44
45 static void netlink_rcv_cb(struct sk_buff *skb)
46 {
47         struct nlmsghdr *nlh;
48         struct net_device *dev;
49         u32 mlen;
50         void *msg;
51         int ifindex;
52
53         if (skb->len >= NLMSG_SPACE(0)) {
54                 nlh = (struct nlmsghdr *)skb->data;
55
56                 if (skb->len < nlh->nlmsg_len ||
57                 nlh->nlmsg_len > ND_MAX_MSG_LEN) {
58                         printk(KERN_ERR "Invalid length (%d,%d)\n", skb->len,
59                                 nlh->nlmsg_len);
60                         return;
61                 }
62
63                 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
64                 msg = ND_NLMSG_DATA(nlh);
65                 mlen = ND_NLMSG_R_LEN(nlh);
66
67                 if (rcv_cb) {
68                         dev = dev_get_by_index(&init_net, ifindex);
69                         if (dev) {
70                                 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
71                                 dev_put(dev);
72                         } else
73                                 printk(KERN_ERR "dev_get_by_index(%d) "
74                                         "is not found.\n", ifindex);
75                 } else
76                         printk(KERN_ERR "Unregistered Callback\n");
77         }
78 }
79
80 static void netlink_rcv(struct sk_buff *skb)
81 {
82         mutex_lock(&netlink_mutex);
83         netlink_rcv_cb(skb);
84         mutex_unlock(&netlink_mutex);
85 }
86
87 struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type,
88                                                 void *msg, int len))
89 {
90         struct sock *sock;
91         struct netlink_kernel_cfg cfg = {
92                 .input  = netlink_rcv,
93         };
94
95 #if !defined(DEFINE_MUTEX)
96         init_MUTEX(&netlink_mutex);
97 #endif
98
99         sock = netlink_kernel_create(&init_net, unit, THIS_MODULE, &cfg);
100
101         if (sock)
102                 rcv_cb = cb;
103
104         return sock;
105 }
106
107 void netlink_exit(struct sock *sock)
108 {
109         netlink_kernel_release(sock);
110 }
111
112 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
113 {
114         static u32 seq;
115         struct sk_buff *skb = NULL;
116         struct nlmsghdr *nlh;
117         int ret = 0;
118
119         if (group > ND_MAX_GROUP) {
120                 printk(KERN_ERR "Group %d is invalied.\n", group);
121                 printk(KERN_ERR "Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
122                 return -EINVAL;
123         }
124
125         skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
126         if (!skb) {
127                 printk(KERN_ERR "netlink_broadcast ret=%d\n", ret);
128                 return -ENOMEM;
129         }
130
131         seq++;
132         nlh = nlmsg_put(skb, 0, seq, type, len, 0);
133         if (!nlh) {
134                 kfree_skb(skb);
135                 return -EMSGSIZE;
136         }
137         memcpy(nlmsg_data(nlh), msg, len);
138
139         NETLINK_CB(skb).pid = 0;
140         NETLINK_CB(skb).dst_group = 0;
141
142         ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
143
144         if (!ret)
145                 return len;
146         else {
147                 if (ret != -ESRCH) {
148                         printk(KERN_ERR "netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
149                                 group, type, len, ret);
150                 }
151                 ret = 0;
152         }
153         return ret;
154 }