From 61e121047645122c47714fcda684d0ee67f444af Mon Sep 17 00:00:00 2001 From: Won Kang Date: Thu, 25 Jul 2013 03:36:17 +0900 Subject: staging: gdm7240: adding LTE USB driver GCT Semiconductor GDM7240 is 4G LTE chip. This driver supports GCT reference platform as a USB device. Signed-off-by: Won Kang Signed-off-by: Greg Kroah-Hartman --- drivers/staging/gdm724x/netlink_k.c | 161 ++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 drivers/staging/gdm724x/netlink_k.c (limited to 'drivers/staging/gdm724x/netlink_k.c') diff --git a/drivers/staging/gdm724x/netlink_k.c b/drivers/staging/gdm724x/netlink_k.c new file mode 100644 index 000000000000..9819bf014857 --- /dev/null +++ b/drivers/staging/gdm724x/netlink_k.c @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include + +#include "netlink_k.h" + +#if defined(DEFINE_MUTEX) +static DEFINE_MUTEX(netlink_mutex); +#else +static struct semaphore netlink_mutex; +#define mutex_lock(x) down(x) +#define mutex_unlock(x) up(x) +#endif + +#define ND_MAX_GROUP 30 +#define ND_IFINDEX_LEN sizeof(int) +#define ND_NLMSG_SPACE(len) (NLMSG_SPACE(len) + ND_IFINDEX_LEN) +#define ND_NLMSG_DATA(nlh) ((void *)((char *)NLMSG_DATA(nlh) + ND_IFINDEX_LEN)) +#define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN) +#define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN) +#define ND_NLMSG_IFIDX(nlh) NLMSG_DATA(nlh) +#define ND_MAX_MSG_LEN (1024 * 32) + +static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len); + +static void netlink_rcv_cb(struct sk_buff *skb) +{ + struct nlmsghdr *nlh; + struct net_device *dev; + u32 mlen; + void *msg; + int ifindex; + + if (!rcv_cb) { + printk(KERN_ERR "glte: nl cb - unregistered\n"); + return; + } + + if (skb->len < NLMSG_SPACE(0)) { + printk(KERN_ERR "glte: nl cb - invalid skb length\n"); + return; + } + + nlh = (struct nlmsghdr *)skb->data; + + if (skb->len < nlh->nlmsg_len || nlh->nlmsg_len > ND_MAX_MSG_LEN) { + printk(KERN_ERR "glte: nl cb - invalid length (%d,%d)\n", + skb->len, nlh->nlmsg_len); + return; + } + + memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN); + msg = ND_NLMSG_DATA(nlh); + mlen = ND_NLMSG_R_LEN(nlh); + + dev = dev_get_by_index(&init_net, ifindex); + if (dev) { + rcv_cb(dev, nlh->nlmsg_type, msg, mlen); + dev_put(dev); + } else { + printk(KERN_ERR "glte: nl cb - dev (%d) not found\n", ifindex); + } +} + +static void netlink_rcv(struct sk_buff *skb) +{ + mutex_lock(&netlink_mutex); + netlink_rcv_cb(skb); + mutex_unlock(&netlink_mutex); +} + +struct sock *netlink_init(int unit, + void (*cb)(struct net_device *dev, u16 type, void *msg, int len)) +{ + struct sock *sock; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0) + struct netlink_kernel_cfg cfg = { + .input = netlink_rcv, + }; +#endif + +#if !defined(DEFINE_MUTEX) + init_MUTEX(&netlink_mutex); +#endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0) + sock = netlink_kernel_create(&init_net, unit, 0, netlink_rcv, NULL, + THIS_MODULE); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) + sock = netlink_kernel_create(&init_net, unit, THIS_MODULE, &cfg); +#else + sock = netlink_kernel_create(&init_net, unit, &cfg); +#endif + + if (sock) + rcv_cb = cb; + + return sock; +} + +void netlink_exit(struct sock *sock) +{ + sock_release(sock->sk_socket); +} + +int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len) +{ + static u32 seq; + struct sk_buff *skb = NULL; + struct nlmsghdr *nlh; + int ret = 0; + + if (group > ND_MAX_GROUP) + return -EINVAL; + + if (!netlink_has_listeners(sock, group+1)) + return -ESRCH; + + skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC); + if (!skb) + return -ENOMEM; + + seq++; + + nlh = nlmsg_put(skb, 0, seq, type, len, 0); + memcpy(NLMSG_DATA(nlh), msg, len); +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) + NETLINK_CB(skb).pid = 0; +#else + NETLINK_CB(skb).portid = 0; +#endif + NETLINK_CB(skb).dst_group = 0; + + ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC); + if (!ret) + return len; + + if (ret != -ESRCH) + printk(KERN_ERR "glte: nl broadcast g=%d, t=%d, l=%d, r=%d\n", + group, type, len, ret); + else if (netlink_has_listeners(sock, group+1)) + return -EAGAIN; + + return ret; +} -- cgit v1.2.3