aboutsummaryrefslogtreecommitdiffstats
path: root/net/caif
diff options
context:
space:
mode:
Diffstat (limited to 'net/caif')
-rw-r--r--net/caif/Kconfig48
-rw-r--r--net/caif/Makefile26
-rw-r--r--net/caif/caif_config_util.c87
-rw-r--r--net/caif/caif_dev.c417
-rw-r--r--net/caif/caif_socket.c1252
-rw-r--r--net/caif/cfcnfg.c470
-rw-r--r--net/caif/cfctrl.c692
-rw-r--r--net/caif/cfdbgl.c40
-rw-r--r--net/caif/cfdgml.c108
-rw-r--r--net/caif/cffrml.c151
-rw-r--r--net/caif/cfmuxl.c251
-rw-r--r--net/caif/cfpkt_skbuff.c571
-rw-r--r--net/caif/cfrfml.c108
-rw-r--r--net/caif/cfserl.c192
-rw-r--r--net/caif/cfsrvl.c192
-rw-r--r--net/caif/cfutill.c115
-rw-r--r--net/caif/cfveil.c107
-rw-r--r--net/caif/cfvidl.c65
-rw-r--r--net/caif/chnl_net.c467
19 files changed, 5359 insertions, 0 deletions
diff --git a/net/caif/Kconfig b/net/caif/Kconfig
new file mode 100644
index 000000000000..cd1daf6008bd
--- /dev/null
+++ b/net/caif/Kconfig
@@ -0,0 +1,48 @@
1#
2# CAIF net configurations
3#
4
5#menu "CAIF Support"
6comment "CAIF Support"
7menuconfig CAIF
8 tristate "Enable CAIF support"
9 select CRC_CCITT
10 default n
11 ---help---
12 The "Communication CPU to Application CPU Interface" (CAIF) is a packet
13 based connection-oriented MUX protocol developed by ST-Ericsson for use
14 with its modems. It is accessed from user space as sockets (PF_CAIF).
15
16 Say Y (or M) here if you build for a phone product (e.g. Android or
17 MeeGo ) that uses CAIF as transport, if unsure say N.
18
19 If you select to build it as module then CAIF_NETDEV also needs to be
20 built as modules. You will also need to say yes to any CAIF physical
21 devices that your platform requires.
22
23 See Documentation/networking/caif for a further explanation on how to
24 use and configure CAIF.
25
26if CAIF
27
28config CAIF_DEBUG
29 bool "Enable Debug"
30 default n
31 --- help ---
32 Enable the inclusion of debug code in the CAIF stack.
33 Be aware that doing this will impact performance.
34 If unsure say N.
35
36
37config CAIF_NETDEV
38 tristate "CAIF GPRS Network device"
39 default CAIF
40 ---help---
41 Say Y if you will be using a CAIF based GPRS network device.
42 This can be either built-in or a loadable module,
43 If you select to build it as a built-in then the main CAIF device must
44 also be a built-in.
45 If unsure say Y.
46
47endif
48#endmenu
diff --git a/net/caif/Makefile b/net/caif/Makefile
new file mode 100644
index 000000000000..34852af2595e
--- /dev/null
+++ b/net/caif/Makefile
@@ -0,0 +1,26 @@
1ifeq ($(CONFIG_CAIF_DEBUG),1)
2CAIF_DBG_FLAGS := -DDEBUG
3endif
4
5ccflags-y := $(CAIF_FLAGS) $(CAIF_DBG_FLAGS)
6
7caif-objs := caif_dev.o \
8 cfcnfg.o cfmuxl.o cfctrl.o \
9 cffrml.o cfveil.o cfdbgl.o\
10 cfserl.o cfdgml.o \
11 cfrfml.o cfvidl.o cfutill.o \
12 cfsrvl.o cfpkt_skbuff.o caif_config_util.o
13clean-dirs:= .tmp_versions
14
15clean-files:= \
16 Module.symvers \
17 modules.order \
18 *.cmd \
19 *.o \
20 *~
21
22obj-$(CONFIG_CAIF) += caif.o
23obj-$(CONFIG_CAIF_NETDEV) += chnl_net.o
24obj-$(CONFIG_CAIF) += caif_socket.o
25
26export-objs := caif.o
diff --git a/net/caif/caif_config_util.c b/net/caif/caif_config_util.c
new file mode 100644
index 000000000000..6f36580366f0
--- /dev/null
+++ b/net/caif/caif_config_util.c
@@ -0,0 +1,87 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <net/caif/cfctrl.h>
10#include <net/caif/cfcnfg.h>
11#include <net/caif/caif_dev.h>
12
13int connect_req_to_link_param(struct cfcnfg *cnfg,
14 struct caif_connect_request *s,
15 struct cfctrl_link_param *l)
16{
17 struct dev_info *dev_info;
18 enum cfcnfg_phy_preference pref;
19 memset(l, 0, sizeof(*l));
20 l->priority = s->priority;
21
22 if (s->link_name[0] != '\0')
23 l->phyid = cfcnfg_get_named(cnfg, s->link_name);
24 else {
25 switch (s->link_selector) {
26 case CAIF_LINK_HIGH_BANDW:
27 pref = CFPHYPREF_HIGH_BW;
28 break;
29 case CAIF_LINK_LOW_LATENCY:
30 pref = CFPHYPREF_LOW_LAT;
31 break;
32 default:
33 return -EINVAL;
34 }
35 dev_info = cfcnfg_get_phyid(cnfg, pref);
36 if (dev_info == NULL)
37 return -ENODEV;
38 l->phyid = dev_info->id;
39 }
40 switch (s->protocol) {
41 case CAIFPROTO_AT:
42 l->linktype = CFCTRL_SRV_VEI;
43 if (s->sockaddr.u.at.type == CAIF_ATTYPE_PLAIN)
44 l->chtype = 0x02;
45 else
46 l->chtype = s->sockaddr.u.at.type;
47 l->endpoint = 0x00;
48 break;
49 case CAIFPROTO_DATAGRAM:
50 l->linktype = CFCTRL_SRV_DATAGRAM;
51 l->chtype = 0x00;
52 l->u.datagram.connid = s->sockaddr.u.dgm.connection_id;
53 break;
54 case CAIFPROTO_DATAGRAM_LOOP:
55 l->linktype = CFCTRL_SRV_DATAGRAM;
56 l->chtype = 0x03;
57 l->endpoint = 0x00;
58 l->u.datagram.connid = s->sockaddr.u.dgm.connection_id;
59 break;
60 case CAIFPROTO_RFM:
61 l->linktype = CFCTRL_SRV_RFM;
62 l->u.datagram.connid = s->sockaddr.u.rfm.connection_id;
63 strncpy(l->u.rfm.volume, s->sockaddr.u.rfm.volume,
64 sizeof(l->u.rfm.volume)-1);
65 l->u.rfm.volume[sizeof(l->u.rfm.volume)-1] = 0;
66 break;
67 case CAIFPROTO_UTIL:
68 l->linktype = CFCTRL_SRV_UTIL;
69 l->endpoint = 0x00;
70 l->chtype = 0x00;
71 strncpy(l->u.utility.name, s->sockaddr.u.util.service,
72 sizeof(l->u.utility.name)-1);
73 l->u.utility.name[sizeof(l->u.utility.name)-1] = 0;
74 caif_assert(sizeof(l->u.utility.name) > 10);
75 l->u.utility.paramlen = s->param.size;
76 if (l->u.utility.paramlen > sizeof(l->u.utility.params))
77 l->u.utility.paramlen = sizeof(l->u.utility.params);
78
79 memcpy(l->u.utility.params, s->param.data,
80 l->u.utility.paramlen);
81
82 break;
83 default:
84 return -EINVAL;
85 }
86 return 0;
87}
diff --git a/net/caif/caif_dev.c b/net/caif/caif_dev.c
new file mode 100644
index 000000000000..e2b86f1f5a47
--- /dev/null
+++ b/net/caif/caif_dev.c
@@ -0,0 +1,417 @@
1/*
2 * CAIF Interface registration.
3 * Copyright (C) ST-Ericsson AB 2010
4 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
5 * License terms: GNU General Public License (GPL) version 2
6 *
7 * Borrowed heavily from file: pn_dev.c. Thanks to
8 * Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * and Sakari Ailus <sakari.ailus@nokia.com>
10 */
11
12#include <linux/version.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/if_arp.h>
16#include <linux/net.h>
17#include <linux/netdevice.h>
18#include <linux/skbuff.h>
19#include <linux/sched.h>
20#include <linux/wait.h>
21#include <net/netns/generic.h>
22#include <net/net_namespace.h>
23#include <net/pkt_sched.h>
24#include <net/caif/caif_device.h>
25#include <net/caif/caif_dev.h>
26#include <net/caif/caif_layer.h>
27#include <net/caif/cfpkt.h>
28#include <net/caif/cfcnfg.h>
29
30MODULE_LICENSE("GPL");
31#define TIMEOUT (HZ*5)
32
33/* Used for local tracking of the CAIF net devices */
34struct caif_device_entry {
35 struct cflayer layer;
36 struct list_head list;
37 atomic_t in_use;
38 atomic_t state;
39 u16 phyid;
40 struct net_device *netdev;
41 wait_queue_head_t event;
42};
43
44struct caif_device_entry_list {
45 struct list_head list;
46 /* Protects simulanous deletes in list */
47 spinlock_t lock;
48};
49
50struct caif_net {
51 struct caif_device_entry_list caifdevs;
52};
53
54static int caif_net_id;
55static struct cfcnfg *cfg;
56
57static struct caif_device_entry_list *caif_device_list(struct net *net)
58{
59 struct caif_net *caifn;
60 BUG_ON(!net);
61 caifn = net_generic(net, caif_net_id);
62 BUG_ON(!caifn);
63 return &caifn->caifdevs;
64}
65
66/* Allocate new CAIF device. */
67static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
68{
69 struct caif_device_entry_list *caifdevs;
70 struct caif_device_entry *caifd;
71 caifdevs = caif_device_list(dev_net(dev));
72 BUG_ON(!caifdevs);
73 caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
74 if (!caifd)
75 return NULL;
76 caifd->netdev = dev;
77 list_add(&caifd->list, &caifdevs->list);
78 init_waitqueue_head(&caifd->event);
79 return caifd;
80}
81
82static struct caif_device_entry *caif_get(struct net_device *dev)
83{
84 struct caif_device_entry_list *caifdevs =
85 caif_device_list(dev_net(dev));
86 struct caif_device_entry *caifd;
87 BUG_ON(!caifdevs);
88 list_for_each_entry(caifd, &caifdevs->list, list) {
89 if (caifd->netdev == dev)
90 return caifd;
91 }
92 return NULL;
93}
94
95static void caif_device_destroy(struct net_device *dev)
96{
97 struct caif_device_entry_list *caifdevs =
98 caif_device_list(dev_net(dev));
99 struct caif_device_entry *caifd;
100 ASSERT_RTNL();
101 if (dev->type != ARPHRD_CAIF)
102 return;
103
104 spin_lock_bh(&caifdevs->lock);
105 caifd = caif_get(dev);
106 if (caifd == NULL) {
107 spin_unlock_bh(&caifdevs->lock);
108 return;
109 }
110
111 list_del(&caifd->list);
112 spin_unlock_bh(&caifdevs->lock);
113
114 kfree(caifd);
115}
116
117static int transmit(struct cflayer *layer, struct cfpkt *pkt)
118{
119 struct caif_device_entry *caifd =
120 container_of(layer, struct caif_device_entry, layer);
121 struct sk_buff *skb, *skb2;
122 int ret = -EINVAL;
123 skb = cfpkt_tonative(pkt);
124 skb->dev = caifd->netdev;
125 /*
126 * Don't allow SKB to be destroyed upon error, but signal resend
127 * notification to clients. We can't rely on the return value as
128 * congestion (NET_XMIT_CN) sometimes drops the packet, sometimes don't.
129 */
130 if (netif_queue_stopped(caifd->netdev))
131 return -EAGAIN;
132 skb2 = skb_get(skb);
133
134 ret = dev_queue_xmit(skb2);
135
136 if (!ret)
137 kfree_skb(skb);
138 else
139 return -EAGAIN;
140
141 return 0;
142}
143
144static int modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
145{
146 struct caif_device_entry *caifd;
147 struct caif_dev_common *caifdev;
148 caifd = container_of(layr, struct caif_device_entry, layer);
149 caifdev = netdev_priv(caifd->netdev);
150 if (ctrl == _CAIF_MODEMCMD_PHYIF_USEFULL) {
151 atomic_set(&caifd->in_use, 1);
152 wake_up_interruptible(&caifd->event);
153
154 } else if (ctrl == _CAIF_MODEMCMD_PHYIF_USELESS) {
155 atomic_set(&caifd->in_use, 0);
156 wake_up_interruptible(&caifd->event);
157 }
158 return 0;
159}
160
161/*
162 * Stuff received packets to associated sockets.
163 * On error, returns non-zero and releases the skb.
164 */
165static int receive(struct sk_buff *skb, struct net_device *dev,
166 struct packet_type *pkttype, struct net_device *orig_dev)
167{
168 struct net *net;
169 struct cfpkt *pkt;
170 struct caif_device_entry *caifd;
171 net = dev_net(dev);
172 pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
173 caifd = caif_get(dev);
174 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd)
175 return NET_RX_DROP;
176
177 if (caifd->layer.up->receive(caifd->layer.up, pkt))
178 return NET_RX_DROP;
179
180 return 0;
181}
182
183static struct packet_type caif_packet_type __read_mostly = {
184 .type = cpu_to_be16(ETH_P_CAIF),
185 .func = receive,
186};
187
188static void dev_flowctrl(struct net_device *dev, int on)
189{
190 struct caif_device_entry *caifd = caif_get(dev);
191 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd)
192 return;
193
194 caifd->layer.up->ctrlcmd(caifd->layer.up,
195 on ?
196 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
197 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
198 caifd->layer.id);
199}
200
201/* notify Caif of device events */
202static int caif_device_notify(struct notifier_block *me, unsigned long what,
203 void *arg)
204{
205 struct net_device *dev = arg;
206 struct caif_device_entry *caifd = NULL;
207 struct caif_dev_common *caifdev;
208 enum cfcnfg_phy_preference pref;
209 int res = -EINVAL;
210 enum cfcnfg_phy_type phy_type;
211
212 if (dev->type != ARPHRD_CAIF)
213 return 0;
214
215 switch (what) {
216 case NETDEV_REGISTER:
217 pr_info("CAIF: %s():register %s\n", __func__, dev->name);
218 caifd = caif_device_alloc(dev);
219 if (caifd == NULL)
220 break;
221 caifdev = netdev_priv(dev);
222 caifdev->flowctrl = dev_flowctrl;
223 atomic_set(&caifd->state, what);
224 res = 0;
225 break;
226
227 case NETDEV_UP:
228 pr_info("CAIF: %s(): up %s\n", __func__, dev->name);
229 caifd = caif_get(dev);
230 if (caifd == NULL)
231 break;
232 caifdev = netdev_priv(dev);
233 if (atomic_read(&caifd->state) == NETDEV_UP) {
234 pr_info("CAIF: %s():%s already up\n",
235 __func__, dev->name);
236 break;
237 }
238 atomic_set(&caifd->state, what);
239 caifd->layer.transmit = transmit;
240 caifd->layer.modemcmd = modemcmd;
241
242 if (caifdev->use_frag)
243 phy_type = CFPHYTYPE_FRAG;
244 else
245 phy_type = CFPHYTYPE_CAIF;
246
247 switch (caifdev->link_select) {
248 case CAIF_LINK_HIGH_BANDW:
249 pref = CFPHYPREF_HIGH_BW;
250 break;
251 case CAIF_LINK_LOW_LATENCY:
252 pref = CFPHYPREF_LOW_LAT;
253 break;
254 default:
255 pref = CFPHYPREF_HIGH_BW;
256 break;
257 }
258
259 cfcnfg_add_phy_layer(get_caif_conf(),
260 phy_type,
261 dev,
262 &caifd->layer,
263 &caifd->phyid,
264 pref,
265 caifdev->use_fcs,
266 caifdev->use_stx);
267 strncpy(caifd->layer.name, dev->name,
268 sizeof(caifd->layer.name) - 1);
269 caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
270 break;
271
272 case NETDEV_GOING_DOWN:
273 caifd = caif_get(dev);
274 if (caifd == NULL)
275 break;
276 pr_info("CAIF: %s():going down %s\n", __func__, dev->name);
277
278 if (atomic_read(&caifd->state) == NETDEV_GOING_DOWN ||
279 atomic_read(&caifd->state) == NETDEV_DOWN)
280 break;
281
282 atomic_set(&caifd->state, what);
283 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd)
284 return -EINVAL;
285 caifd->layer.up->ctrlcmd(caifd->layer.up,
286 _CAIF_CTRLCMD_PHYIF_DOWN_IND,
287 caifd->layer.id);
288 res = wait_event_interruptible_timeout(caifd->event,
289 atomic_read(&caifd->in_use) == 0,
290 TIMEOUT);
291 break;
292
293 case NETDEV_DOWN:
294 caifd = caif_get(dev);
295 if (caifd == NULL)
296 break;
297 pr_info("CAIF: %s(): down %s\n", __func__, dev->name);
298 if (atomic_read(&caifd->in_use))
299 pr_warning("CAIF: %s(): "
300 "Unregistering an active CAIF device: %s\n",
301 __func__, dev->name);
302 cfcnfg_del_phy_layer(get_caif_conf(), &caifd->layer);
303 atomic_set(&caifd->state, what);
304 break;
305
306 case NETDEV_UNREGISTER:
307 caifd = caif_get(dev);
308 pr_info("CAIF: %s(): unregister %s\n", __func__, dev->name);
309 atomic_set(&caifd->state, what);
310 caif_device_destroy(dev);
311 break;
312 }
313 return 0;
314}
315
316static struct notifier_block caif_device_notifier = {
317 .notifier_call = caif_device_notify,
318 .priority = 0,
319};
320
321
322struct cfcnfg *get_caif_conf(void)
323{
324 return cfg;
325}
326EXPORT_SYMBOL(get_caif_conf);
327
328int caif_connect_client(struct caif_connect_request *conn_req,
329 struct cflayer *client_layer)
330{
331 struct cfctrl_link_param param;
332 int ret;
333 ret = connect_req_to_link_param(get_caif_conf(), conn_req, &param);
334 if (ret)
335 return ret;
336 /* Hook up the adaptation layer. */
337 return cfcnfg_add_adaptation_layer(get_caif_conf(),
338 &param, client_layer);
339}
340EXPORT_SYMBOL(caif_connect_client);
341
342int caif_disconnect_client(struct cflayer *adap_layer)
343{
344 return cfcnfg_disconn_adapt_layer(get_caif_conf(), adap_layer);
345}
346EXPORT_SYMBOL(caif_disconnect_client);
347
348void caif_release_client(struct cflayer *adap_layer)
349{
350 cfcnfg_release_adap_layer(adap_layer);
351}
352EXPORT_SYMBOL(caif_release_client);
353
354/* Per-namespace Caif devices handling */
355static int caif_init_net(struct net *net)
356{
357 struct caif_net *caifn = net_generic(net, caif_net_id);
358 INIT_LIST_HEAD(&caifn->caifdevs.list);
359 spin_lock_init(&caifn->caifdevs.lock);
360 return 0;
361}
362
363static void caif_exit_net(struct net *net)
364{
365 struct net_device *dev;
366 int res;
367 rtnl_lock();
368 for_each_netdev(net, dev) {
369 if (dev->type != ARPHRD_CAIF)
370 continue;
371 res = dev_close(dev);
372 caif_device_destroy(dev);
373 }
374 rtnl_unlock();
375}
376
377static struct pernet_operations caif_net_ops = {
378 .init = caif_init_net,
379 .exit = caif_exit_net,
380 .id = &caif_net_id,
381 .size = sizeof(struct caif_net),
382};
383
384/* Initialize Caif devices list */
385static int __init caif_device_init(void)
386{
387 int result;
388 cfg = cfcnfg_create();
389 if (!cfg) {
390 pr_warning("CAIF: %s(): can't create cfcnfg.\n", __func__);
391 goto err_cfcnfg_create_failed;
392 }
393 result = register_pernet_device(&caif_net_ops);
394
395 if (result) {
396 kfree(cfg);
397 cfg = NULL;
398 return result;
399 }
400 dev_add_pack(&caif_packet_type);
401 register_netdevice_notifier(&caif_device_notifier);
402
403 return result;
404err_cfcnfg_create_failed:
405 return -ENODEV;
406}
407
408static void __exit caif_device_exit(void)
409{
410 dev_remove_pack(&caif_packet_type);
411 unregister_pernet_device(&caif_net_ops);
412 unregister_netdevice_notifier(&caif_device_notifier);
413 cfcnfg_remove(cfg);
414}
415
416module_init(caif_device_init);
417module_exit(caif_device_exit);
diff --git a/net/caif/caif_socket.c b/net/caif/caif_socket.c
new file mode 100644
index 000000000000..c3a70c5c893a
--- /dev/null
+++ b/net/caif/caif_socket.c
@@ -0,0 +1,1252 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/fs.h>
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/sched.h>
11#include <linux/spinlock.h>
12#include <linux/mutex.h>
13#include <linux/list.h>
14#include <linux/wait.h>
15#include <linux/poll.h>
16#include <linux/tcp.h>
17#include <linux/uaccess.h>
18#include <linux/mutex.h>
19#include <linux/debugfs.h>
20#include <linux/caif/caif_socket.h>
21#include <asm/atomic.h>
22#include <net/sock.h>
23#include <net/tcp_states.h>
24#include <net/caif/caif_layer.h>
25#include <net/caif/caif_dev.h>
26#include <net/caif/cfpkt.h>
27
28MODULE_LICENSE("GPL");
29MODULE_ALIAS_NETPROTO(AF_CAIF);
30
31#define CAIF_DEF_SNDBUF (CAIF_MAX_PAYLOAD_SIZE*10)
32#define CAIF_DEF_RCVBUF (CAIF_MAX_PAYLOAD_SIZE*100)
33
34/*
35 * CAIF state is re-using the TCP socket states.
36 * caif_states stored in sk_state reflect the state as reported by
37 * the CAIF stack, while sk_socket->state is the state of the socket.
38 */
39enum caif_states {
40 CAIF_CONNECTED = TCP_ESTABLISHED,
41 CAIF_CONNECTING = TCP_SYN_SENT,
42 CAIF_DISCONNECTED = TCP_CLOSE
43};
44
45#define TX_FLOW_ON_BIT 1
46#define RX_FLOW_ON_BIT 2
47
48static struct dentry *debugfsdir;
49
50#ifdef CONFIG_DEBUG_FS
51struct debug_fs_counter {
52 atomic_t caif_nr_socks;
53 atomic_t num_connect_req;
54 atomic_t num_connect_resp;
55 atomic_t num_connect_fail_resp;
56 atomic_t num_disconnect;
57 atomic_t num_remote_shutdown_ind;
58 atomic_t num_tx_flow_off_ind;
59 atomic_t num_tx_flow_on_ind;
60 atomic_t num_rx_flow_off;
61 atomic_t num_rx_flow_on;
62};
63struct debug_fs_counter cnt;
64#define dbfs_atomic_inc(v) atomic_inc(v)
65#define dbfs_atomic_dec(v) atomic_dec(v)
66#else
67#define dbfs_atomic_inc(v)
68#define dbfs_atomic_dec(v)
69#endif
70
71struct caifsock {
72 struct sock sk; /* must be first member */
73 struct cflayer layer;
74 char name[CAIF_LAYER_NAME_SZ]; /* Used for debugging */
75 u32 flow_state;
76 struct caif_connect_request conn_req;
77 struct mutex readlock;
78 struct dentry *debugfs_socket_dir;
79};
80
81static int rx_flow_is_on(struct caifsock *cf_sk)
82{
83 return test_bit(RX_FLOW_ON_BIT,
84 (void *) &cf_sk->flow_state);
85}
86
87static int tx_flow_is_on(struct caifsock *cf_sk)
88{
89 return test_bit(TX_FLOW_ON_BIT,
90 (void *) &cf_sk->flow_state);
91}
92
93static void set_rx_flow_off(struct caifsock *cf_sk)
94{
95 clear_bit(RX_FLOW_ON_BIT,
96 (void *) &cf_sk->flow_state);
97}
98
99static void set_rx_flow_on(struct caifsock *cf_sk)
100{
101 set_bit(RX_FLOW_ON_BIT,
102 (void *) &cf_sk->flow_state);
103}
104
105static void set_tx_flow_off(struct caifsock *cf_sk)
106{
107 clear_bit(TX_FLOW_ON_BIT,
108 (void *) &cf_sk->flow_state);
109}
110
111static void set_tx_flow_on(struct caifsock *cf_sk)
112{
113 set_bit(TX_FLOW_ON_BIT,
114 (void *) &cf_sk->flow_state);
115}
116
117static void caif_read_lock(struct sock *sk)
118{
119 struct caifsock *cf_sk;
120 cf_sk = container_of(sk, struct caifsock, sk);
121 mutex_lock(&cf_sk->readlock);
122}
123
124static void caif_read_unlock(struct sock *sk)
125{
126 struct caifsock *cf_sk;
127 cf_sk = container_of(sk, struct caifsock, sk);
128 mutex_unlock(&cf_sk->readlock);
129}
130
131int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
132{
133 /* A quarter of full buffer is used a low water mark */
134 return cf_sk->sk.sk_rcvbuf / 4;
135}
136
137void caif_flow_ctrl(struct sock *sk, int mode)
138{
139 struct caifsock *cf_sk;
140 cf_sk = container_of(sk, struct caifsock, sk);
141 if (cf_sk->layer.dn)
142 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
143}
144
145/*
146 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
147 * not dropped, but CAIF is sending flow off instead.
148 */
149int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
150{
151 int err;
152 int skb_len;
153 unsigned long flags;
154 struct sk_buff_head *list = &sk->sk_receive_queue;
155 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
156
157 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
158 (unsigned)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
159 trace_printk("CAIF: %s():"
160 " sending flow OFF (queue len = %d %d)\n",
161 __func__,
162 atomic_read(&cf_sk->sk.sk_rmem_alloc),
163 sk_rcvbuf_lowwater(cf_sk));
164 set_rx_flow_off(cf_sk);
165 if (cf_sk->layer.dn)
166 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn,
167 CAIF_MODEMCMD_FLOW_OFF_REQ);
168 }
169
170 err = sk_filter(sk, skb);
171 if (err)
172 return err;
173 if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) {
174 set_rx_flow_off(cf_sk);
175 trace_printk("CAIF: %s():"
176 " sending flow OFF due to rmem_schedule\n",
177 __func__);
178 if (cf_sk->layer.dn)
179 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn,
180 CAIF_MODEMCMD_FLOW_OFF_REQ);
181 }
182 skb->dev = NULL;
183 skb_set_owner_r(skb, sk);
184 /* Cache the SKB length before we tack it onto the receive
185 * queue. Once it is added it no longer belongs to us and
186 * may be freed by other threads of control pulling packets
187 * from the queue.
188 */
189 skb_len = skb->len;
190 spin_lock_irqsave(&list->lock, flags);
191 if (!sock_flag(sk, SOCK_DEAD))
192 __skb_queue_tail(list, skb);
193 spin_unlock_irqrestore(&list->lock, flags);
194
195 if (!sock_flag(sk, SOCK_DEAD))
196 sk->sk_data_ready(sk, skb_len);
197 else
198 kfree_skb(skb);
199 return 0;
200}
201
202/* Packet Receive Callback function called from CAIF Stack */
203static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
204{
205 struct caifsock *cf_sk;
206 struct sk_buff *skb;
207
208 cf_sk = container_of(layr, struct caifsock, layer);
209 skb = cfpkt_tonative(pkt);
210
211 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
212 cfpkt_destroy(pkt);
213 return 0;
214 }
215 caif_queue_rcv_skb(&cf_sk->sk, skb);
216 return 0;
217}
218
219/* Packet Control Callback function called from CAIF */
220static void caif_ctrl_cb(struct cflayer *layr,
221 enum caif_ctrlcmd flow,
222 int phyid)
223{
224 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
225 switch (flow) {
226 case CAIF_CTRLCMD_FLOW_ON_IND:
227 /* OK from modem to start sending again */
228 dbfs_atomic_inc(&cnt.num_tx_flow_on_ind);
229 set_tx_flow_on(cf_sk);
230 cf_sk->sk.sk_state_change(&cf_sk->sk);
231 break;
232
233 case CAIF_CTRLCMD_FLOW_OFF_IND:
234 /* Modem asks us to shut up */
235 dbfs_atomic_inc(&cnt.num_tx_flow_off_ind);
236 set_tx_flow_off(cf_sk);
237 cf_sk->sk.sk_state_change(&cf_sk->sk);
238 break;
239
240 case CAIF_CTRLCMD_INIT_RSP:
241 /* We're now connected */
242 dbfs_atomic_inc(&cnt.num_connect_resp);
243 cf_sk->sk.sk_state = CAIF_CONNECTED;
244 set_tx_flow_on(cf_sk);
245 cf_sk->sk.sk_state_change(&cf_sk->sk);
246 break;
247
248 case CAIF_CTRLCMD_DEINIT_RSP:
249 /* We're now disconnected */
250 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
251 cf_sk->sk.sk_state_change(&cf_sk->sk);
252 cfcnfg_release_adap_layer(&cf_sk->layer);
253 break;
254
255 case CAIF_CTRLCMD_INIT_FAIL_RSP:
256 /* Connect request failed */
257 dbfs_atomic_inc(&cnt.num_connect_fail_resp);
258 cf_sk->sk.sk_err = ECONNREFUSED;
259 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
260 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
261 /*
262 * Socket "standards" seems to require POLLOUT to
263 * be set at connect failure.
264 */
265 set_tx_flow_on(cf_sk);
266 cf_sk->sk.sk_state_change(&cf_sk->sk);
267 break;
268
269 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
270 /* Modem has closed this connection, or device is down. */
271 dbfs_atomic_inc(&cnt.num_remote_shutdown_ind);
272 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
273 cf_sk->sk.sk_err = ECONNRESET;
274 set_rx_flow_on(cf_sk);
275 cf_sk->sk.sk_error_report(&cf_sk->sk);
276 break;
277
278 default:
279 pr_debug("CAIF: %s(): Unexpected flow command %d\n",
280 __func__, flow);
281 }
282}
283
284static void caif_check_flow_release(struct sock *sk)
285{
286 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
287
288 if (cf_sk->layer.dn == NULL || cf_sk->layer.dn->modemcmd == NULL)
289 return;
290 if (rx_flow_is_on(cf_sk))
291 return;
292
293 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
294 dbfs_atomic_inc(&cnt.num_rx_flow_on);
295 set_rx_flow_on(cf_sk);
296 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn,
297 CAIF_MODEMCMD_FLOW_ON_REQ);
298 }
299}
300/*
301 * Copied from sock.c:sock_queue_rcv_skb(), and added check that user buffer
302 * has sufficient size.
303 */
304
305static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
306 struct msghdr *m, size_t buf_len, int flags)
307
308{
309 struct sock *sk = sock->sk;
310 struct sk_buff *skb;
311 int ret = 0;
312 int len;
313
314 if (unlikely(!buf_len))
315 return -EINVAL;
316
317 skb = skb_recv_datagram(sk, flags, 0 , &ret);
318 if (!skb)
319 goto read_error;
320
321 len = skb->len;
322
323 if (skb && skb->len > buf_len && !(flags & MSG_PEEK)) {
324 len = buf_len;
325 /*
326 * Push skb back on receive queue if buffer too small.
327 * This has a built-in race where multi-threaded receive
328 * may get packet in wrong order, but multiple read does
329 * not really guarantee ordered delivery anyway.
330 * Let's optimize for speed without taking locks.
331 */
332
333 skb_queue_head(&sk->sk_receive_queue, skb);
334 ret = -EMSGSIZE;
335 goto read_error;
336 }
337
338 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, len);
339 if (ret)
340 goto read_error;
341
342 skb_free_datagram(sk, skb);
343
344 caif_check_flow_release(sk);
345
346 return len;
347
348read_error:
349 return ret;
350}
351
352
353/* Copied from unix_stream_wait_data, identical except for lock call. */
354static long caif_stream_data_wait(struct sock *sk, long timeo)
355{
356 DEFINE_WAIT(wait);
357 lock_sock(sk);
358
359 for (;;) {
360 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
361
362 if (!skb_queue_empty(&sk->sk_receive_queue) ||
363 sk->sk_err ||
364 sk->sk_state != CAIF_CONNECTED ||
365 sock_flag(sk, SOCK_DEAD) ||
366 (sk->sk_shutdown & RCV_SHUTDOWN) ||
367 signal_pending(current) ||
368 !timeo)
369 break;
370
371 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
372 release_sock(sk);
373 timeo = schedule_timeout(timeo);
374 lock_sock(sk);
375 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
376 }
377
378 finish_wait(sk_sleep(sk), &wait);
379 release_sock(sk);
380 return timeo;
381}
382
383
384/*
385 * Copied from unix_stream_recvmsg, but removed credit checks,
386 * changed locking calls, changed address handling.
387 */
388static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
389 struct msghdr *msg, size_t size,
390 int flags)
391{
392 struct sock *sk = sock->sk;
393 int copied = 0;
394 int target;
395 int err = 0;
396 long timeo;
397
398 err = -EOPNOTSUPP;
399 if (flags&MSG_OOB)
400 goto out;
401
402 msg->msg_namelen = 0;
403
404 /*
405 * Lock the socket to prevent queue disordering
406 * while sleeps in memcpy_tomsg
407 */
408 err = -EAGAIN;
409 if (sk->sk_state == CAIF_CONNECTING)
410 goto out;
411
412 caif_read_lock(sk);
413 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
414 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
415
416 do {
417 int chunk;
418 struct sk_buff *skb;
419
420 lock_sock(sk);
421 skb = skb_dequeue(&sk->sk_receive_queue);
422 caif_check_flow_release(sk);
423
424 if (skb == NULL) {
425 if (copied >= target)
426 goto unlock;
427 /*
428 * POSIX 1003.1g mandates this order.
429 */
430 err = sock_error(sk);
431 if (err)
432 goto unlock;
433 err = -ECONNRESET;
434 if (sk->sk_shutdown & RCV_SHUTDOWN)
435 goto unlock;
436
437 err = -EPIPE;
438 if (sk->sk_state != CAIF_CONNECTED)
439 goto unlock;
440 if (sock_flag(sk, SOCK_DEAD))
441 goto unlock;
442
443 release_sock(sk);
444
445 err = -EAGAIN;
446 if (!timeo)
447 break;
448
449 caif_read_unlock(sk);
450
451 timeo = caif_stream_data_wait(sk, timeo);
452
453 if (signal_pending(current)) {
454 err = sock_intr_errno(timeo);
455 goto out;
456 }
457 caif_read_lock(sk);
458 continue;
459unlock:
460 release_sock(sk);
461 break;
462 }
463 release_sock(sk);
464 chunk = min_t(unsigned int, skb->len, size);
465 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
466 skb_queue_head(&sk->sk_receive_queue, skb);
467 if (copied == 0)
468 copied = -EFAULT;
469 break;
470 }
471 copied += chunk;
472 size -= chunk;
473
474 /* Mark read part of skb as used */
475 if (!(flags & MSG_PEEK)) {
476 skb_pull(skb, chunk);
477
478 /* put the skb back if we didn't use it up. */
479 if (skb->len) {
480 skb_queue_head(&sk->sk_receive_queue, skb);
481 break;
482 }
483 kfree_skb(skb);
484
485 } else {
486 /*
487 * It is questionable, see note in unix_dgram_recvmsg.
488 */
489 /* put message back and return */
490 skb_queue_head(&sk->sk_receive_queue, skb);
491 break;
492 }
493 } while (size);
494 caif_read_unlock(sk);
495
496out:
497 return copied ? : err;
498}
499
500/*
501 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
502 * CAIF flow-on and sock_writable.
503 */
504static long caif_wait_for_flow_on(struct caifsock *cf_sk,
505 int wait_writeable, long timeo, int *err)
506{
507 struct sock *sk = &cf_sk->sk;
508 DEFINE_WAIT(wait);
509 for (;;) {
510 *err = 0;
511 if (tx_flow_is_on(cf_sk) &&
512 (!wait_writeable || sock_writeable(&cf_sk->sk)))
513 break;
514 *err = -ETIMEDOUT;
515 if (!timeo)
516 break;
517 *err = -ERESTARTSYS;
518 if (signal_pending(current))
519 break;
520 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
521 *err = -ECONNRESET;
522 if (sk->sk_shutdown & SHUTDOWN_MASK)
523 break;
524 *err = -sk->sk_err;
525 if (sk->sk_err)
526 break;
527 *err = -EPIPE;
528 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
529 break;
530 timeo = schedule_timeout(timeo);
531 }
532 finish_wait(sk_sleep(sk), &wait);
533 return timeo;
534}
535
536/*
537 * Transmit a SKB. The device may temporarily request re-transmission
538 * by returning EAGAIN.
539 */
540static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
541 int noblock, long timeo)
542{
543 struct cfpkt *pkt;
544 int ret, loopcnt = 0;
545
546 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
547 memset(cfpkt_info(pkt), 0, sizeof(struct caif_payload_info));
548 do {
549
550 ret = -ETIMEDOUT;
551
552 /* Slight paranoia, probably not needed. */
553 if (unlikely(loopcnt++ > 1000)) {
554 pr_warning("CAIF: %s(): transmit retries failed,"
555 " error = %d\n", __func__, ret);
556 break;
557 }
558
559 if (cf_sk->layer.dn != NULL)
560 ret = cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
561 if (likely(ret >= 0))
562 break;
563 /* if transmit return -EAGAIN, then retry */
564 if (noblock && ret == -EAGAIN)
565 break;
566 timeo = caif_wait_for_flow_on(cf_sk, 0, timeo, &ret);
567 if (signal_pending(current)) {
568 ret = sock_intr_errno(timeo);
569 break;
570 }
571 if (ret)
572 break;
573 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
574 sock_flag(&cf_sk->sk, SOCK_DEAD) ||
575 (cf_sk->sk.sk_shutdown & RCV_SHUTDOWN)) {
576 ret = -EPIPE;
577 cf_sk->sk.sk_err = EPIPE;
578 break;
579 }
580 } while (ret == -EAGAIN);
581 return ret;
582}
583
584/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
585static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
586 struct msghdr *msg, size_t len)
587{
588 struct sock *sk = sock->sk;
589 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
590 int buffer_size;
591 int ret = 0;
592 struct sk_buff *skb = NULL;
593 int noblock;
594 long timeo;
595 caif_assert(cf_sk);
596 ret = sock_error(sk);
597 if (ret)
598 goto err;
599
600 ret = -EOPNOTSUPP;
601 if (msg->msg_flags&MSG_OOB)
602 goto err;
603
604 ret = -EOPNOTSUPP;
605 if (msg->msg_namelen)
606 goto err;
607
608 ret = -EINVAL;
609 if (unlikely(msg->msg_iov->iov_base == NULL))
610 goto err;
611 noblock = msg->msg_flags & MSG_DONTWAIT;
612
613 buffer_size = len + CAIF_NEEDED_HEADROOM + CAIF_NEEDED_TAILROOM;
614
615 ret = -EMSGSIZE;
616 if (buffer_size > CAIF_MAX_PAYLOAD_SIZE)
617 goto err;
618
619 timeo = sock_sndtimeo(sk, noblock);
620 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
621 1, timeo, &ret);
622
623 ret = -EPIPE;
624 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
625 sock_flag(sk, SOCK_DEAD) ||
626 (sk->sk_shutdown & RCV_SHUTDOWN))
627 goto err;
628
629 ret = -ENOMEM;
630 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
631 if (!skb)
632 goto err;
633 skb_reserve(skb, CAIF_NEEDED_HEADROOM);
634
635 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
636
637 if (ret)
638 goto err;
639 ret = transmit_skb(skb, cf_sk, noblock, timeo);
640 if (ret < 0)
641 goto err;
642 return len;
643err:
644 kfree_skb(skb);
645 return ret;
646}
647
648/*
649 * Copied from unix_stream_sendmsg and adapted to CAIF:
650 * Changed removed permission handling and added waiting for flow on
651 * and other minor adaptations.
652 */
653static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
654 struct msghdr *msg, size_t len)
655{
656 struct sock *sk = sock->sk;
657 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
658 int err, size;
659 struct sk_buff *skb;
660 int sent = 0;
661 long timeo;
662
663 err = -EOPNOTSUPP;
664
665 if (unlikely(msg->msg_flags&MSG_OOB))
666 goto out_err;
667
668 if (unlikely(msg->msg_namelen))
669 goto out_err;
670
671 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
672 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
673
674 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
675 goto pipe_err;
676
677 while (sent < len) {
678
679 size = len-sent;
680
681 if (size > CAIF_MAX_PAYLOAD_SIZE)
682 size = CAIF_MAX_PAYLOAD_SIZE;
683
684 /* If size is more than half of sndbuf, chop up message */
685 if (size > ((sk->sk_sndbuf >> 1) - 64))
686 size = (sk->sk_sndbuf >> 1) - 64;
687
688 if (size > SKB_MAX_ALLOC)
689 size = SKB_MAX_ALLOC;
690
691 skb = sock_alloc_send_skb(sk,
692 size + CAIF_NEEDED_HEADROOM
693 + CAIF_NEEDED_TAILROOM,
694 msg->msg_flags&MSG_DONTWAIT,
695 &err);
696 if (skb == NULL)
697 goto out_err;
698
699 skb_reserve(skb, CAIF_NEEDED_HEADROOM);
700 /*
701 * If you pass two values to the sock_alloc_send_skb
702 * it tries to grab the large buffer with GFP_NOFS
703 * (which can fail easily), and if it fails grab the
704 * fallback size buffer which is under a page and will
705 * succeed. [Alan]
706 */
707 size = min_t(int, size, skb_tailroom(skb));
708
709 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
710 if (err) {
711 kfree_skb(skb);
712 goto out_err;
713 }
714 err = transmit_skb(skb, cf_sk,
715 msg->msg_flags&MSG_DONTWAIT, timeo);
716 if (err < 0) {
717 kfree_skb(skb);
718 goto pipe_err;
719 }
720 sent += size;
721 }
722
723 return sent;
724
725pipe_err:
726 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
727 send_sig(SIGPIPE, current, 0);
728 err = -EPIPE;
729out_err:
730 return sent ? : err;
731}
732
733static int setsockopt(struct socket *sock,
734 int lvl, int opt, char __user *ov, unsigned int ol)
735{
736 struct sock *sk = sock->sk;
737 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
738 int prio, linksel;
739 struct ifreq ifreq;
740
741 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
742 return -ENOPROTOOPT;
743
744 switch (opt) {
745 case CAIFSO_LINK_SELECT:
746 if (ol < sizeof(int))
747 return -EINVAL;
748 if (lvl != SOL_CAIF)
749 goto bad_sol;
750 if (copy_from_user(&linksel, ov, sizeof(int)))
751 return -EINVAL;
752 lock_sock(&(cf_sk->sk));
753 cf_sk->conn_req.link_selector = linksel;
754 release_sock(&cf_sk->sk);
755 return 0;
756
757 case SO_PRIORITY:
758 if (lvl != SOL_SOCKET)
759 goto bad_sol;
760 if (ol < sizeof(int))
761 return -EINVAL;
762 if (copy_from_user(&prio, ov, sizeof(int)))
763 return -EINVAL;
764 lock_sock(&(cf_sk->sk));
765 cf_sk->conn_req.priority = prio;
766 release_sock(&cf_sk->sk);
767 return 0;
768
769 case SO_BINDTODEVICE:
770 if (lvl != SOL_SOCKET)
771 goto bad_sol;
772 if (ol < sizeof(struct ifreq))
773 return -EINVAL;
774 if (copy_from_user(&ifreq, ov, sizeof(ifreq)))
775 return -EFAULT;
776 lock_sock(&(cf_sk->sk));
777 strncpy(cf_sk->conn_req.link_name, ifreq.ifr_name,
778 sizeof(cf_sk->conn_req.link_name));
779 cf_sk->conn_req.link_name
780 [sizeof(cf_sk->conn_req.link_name)-1] = 0;
781 release_sock(&cf_sk->sk);
782 return 0;
783
784 case CAIFSO_REQ_PARAM:
785 if (lvl != SOL_CAIF)
786 goto bad_sol;
787 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
788 return -ENOPROTOOPT;
789 lock_sock(&(cf_sk->sk));
790 cf_sk->conn_req.param.size = ol;
791 if (ol > sizeof(cf_sk->conn_req.param.data) ||
792 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
793 release_sock(&cf_sk->sk);
794 return -EINVAL;
795 }
796 release_sock(&cf_sk->sk);
797 return 0;
798
799 default:
800 return -ENOPROTOOPT;
801 }
802
803 return 0;
804bad_sol:
805 return -ENOPROTOOPT;
806
807}
808
809/*
810 * caif_connect() - Connect a CAIF Socket
811 * Copied and modified af_irda.c:irda_connect().
812 *
813 * Note : by consulting "errno", the user space caller may learn the cause
814 * of the failure. Most of them are visible in the function, others may come
815 * from subroutines called and are listed here :
816 * o -EAFNOSUPPORT: bad socket family or type.
817 * o -ESOCKTNOSUPPORT: bad socket type or protocol
818 * o -EINVAL: bad socket address, or CAIF link type
819 * o -ECONNREFUSED: remote end refused the connection.
820 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
821 * o -EISCONN: already connected.
822 * o -ETIMEDOUT: Connection timed out (send timeout)
823 * o -ENODEV: No link layer to send request
824 * o -ECONNRESET: Received Shutdown indication or lost link layer
825 * o -ENOMEM: Out of memory
826 *
827 * State Strategy:
828 * o sk_state: holds the CAIF_* protocol state, it's updated by
829 * caif_ctrl_cb.
830 * o sock->state: holds the SS_* socket state and is updated by connect and
831 * disconnect.
832 */
833static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
834 int addr_len, int flags)
835{
836 struct sock *sk = sock->sk;
837 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
838 long timeo;
839 int err;
840 lock_sock(sk);
841
842 err = -EAFNOSUPPORT;
843 if (uaddr->sa_family != AF_CAIF)
844 goto out;
845
846 err = -ESOCKTNOSUPPORT;
847 if (unlikely(!(sk->sk_type == SOCK_STREAM &&
848 cf_sk->sk.sk_protocol == CAIFPROTO_AT) &&
849 sk->sk_type != SOCK_SEQPACKET))
850 goto out;
851 switch (sock->state) {
852 case SS_UNCONNECTED:
853 /* Normal case, a fresh connect */
854 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
855 break;
856 case SS_CONNECTING:
857 switch (sk->sk_state) {
858 case CAIF_CONNECTED:
859 sock->state = SS_CONNECTED;
860 err = -EISCONN;
861 goto out;
862 case CAIF_DISCONNECTED:
863 /* Reconnect allowed */
864 break;
865 case CAIF_CONNECTING:
866 err = -EALREADY;
867 if (flags & O_NONBLOCK)
868 goto out;
869 goto wait_connect;
870 }
871 break;
872 case SS_CONNECTED:
873 caif_assert(sk->sk_state == CAIF_CONNECTED ||
874 sk->sk_state == CAIF_DISCONNECTED);
875 if (sk->sk_shutdown & SHUTDOWN_MASK) {
876 /* Allow re-connect after SHUTDOWN_IND */
877 caif_disconnect_client(&cf_sk->layer);
878 break;
879 }
880 /* No reconnect on a seqpacket socket */
881 err = -EISCONN;
882 goto out;
883 case SS_DISCONNECTING:
884 case SS_FREE:
885 caif_assert(1); /*Should never happen */
886 break;
887 }
888 sk->sk_state = CAIF_DISCONNECTED;
889 sock->state = SS_UNCONNECTED;
890 sk_stream_kill_queues(&cf_sk->sk);
891
892 err = -EINVAL;
893 if (addr_len != sizeof(struct sockaddr_caif) ||
894 !uaddr)
895 goto out;
896
897 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
898 sizeof(struct sockaddr_caif));
899
900 /* Move to connecting socket, start sending Connect Requests */
901 sock->state = SS_CONNECTING;
902 sk->sk_state = CAIF_CONNECTING;
903
904 dbfs_atomic_inc(&cnt.num_connect_req);
905 cf_sk->layer.receive = caif_sktrecv_cb;
906 err = caif_connect_client(&cf_sk->conn_req,
907 &cf_sk->layer);
908 if (err < 0) {
909 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
910 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
911 goto out;
912 }
913
914 err = -EINPROGRESS;
915wait_connect:
916
917 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
918 goto out;
919
920 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
921
922 release_sock(sk);
923 err = wait_event_interruptible_timeout(*sk_sleep(sk),
924 sk->sk_state != CAIF_CONNECTING,
925 timeo);
926 lock_sock(sk);
927 if (err < 0)
928 goto out; /* -ERESTARTSYS */
929 if (err == 0 && sk->sk_state != CAIF_CONNECTED) {
930 err = -ETIMEDOUT;
931 goto out;
932 }
933
934 if (sk->sk_state != CAIF_CONNECTED) {
935 sock->state = SS_UNCONNECTED;
936 err = sock_error(sk);
937 if (!err)
938 err = -ECONNREFUSED;
939 goto out;
940 }
941 sock->state = SS_CONNECTED;
942 err = 0;
943out:
944 release_sock(sk);
945 return err;
946}
947
948
949/*
950 * caif_release() - Disconnect a CAIF Socket
951 * Copied and modified af_irda.c:irda_release().
952 */
953static int caif_release(struct socket *sock)
954{
955 struct sock *sk = sock->sk;
956 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
957 int res = 0;
958
959 if (!sk)
960 return 0;
961
962 set_tx_flow_off(cf_sk);
963
964 /*
965 * Ensure that packets are not queued after this point in time.
966 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
967 * this ensures no packets when sock is dead.
968 */
969 spin_lock(&sk->sk_receive_queue.lock);
970 sock_set_flag(sk, SOCK_DEAD);
971 spin_unlock(&sk->sk_receive_queue.lock);
972 sock->sk = NULL;
973
974 dbfs_atomic_inc(&cnt.num_disconnect);
975
976 if (cf_sk->debugfs_socket_dir != NULL)
977 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
978
979 lock_sock(&(cf_sk->sk));
980 sk->sk_state = CAIF_DISCONNECTED;
981 sk->sk_shutdown = SHUTDOWN_MASK;
982
983 if (cf_sk->sk.sk_socket->state == SS_CONNECTED ||
984 cf_sk->sk.sk_socket->state == SS_CONNECTING)
985 res = caif_disconnect_client(&cf_sk->layer);
986
987 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
988 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
989
990 sock_orphan(sk);
991 cf_sk->layer.dn = NULL;
992 sk_stream_kill_queues(&cf_sk->sk);
993 release_sock(sk);
994 sock_put(sk);
995 return res;
996}
997
998/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
999static unsigned int caif_poll(struct file *file,
1000 struct socket *sock, poll_table *wait)
1001{
1002 struct sock *sk = sock->sk;
1003 unsigned int mask;
1004 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1005
1006 sock_poll_wait(file, sk_sleep(sk), wait);
1007 mask = 0;
1008
1009 /* exceptional events? */
1010 if (sk->sk_err)
1011 mask |= POLLERR;
1012 if (sk->sk_shutdown == SHUTDOWN_MASK)
1013 mask |= POLLHUP;
1014 if (sk->sk_shutdown & RCV_SHUTDOWN)
1015 mask |= POLLRDHUP;
1016
1017 /* readable? */
1018 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1019 (sk->sk_shutdown & RCV_SHUTDOWN))
1020 mask |= POLLIN | POLLRDNORM;
1021
1022 /* Connection-based need to check for termination and startup */
1023 if (sk->sk_state == CAIF_DISCONNECTED)
1024 mask |= POLLHUP;
1025
1026 /*
1027 * we set writable also when the other side has shut down the
1028 * connection. This prevents stuck sockets.
1029 */
1030 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
1031 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1032
1033 return mask;
1034}
1035
1036static const struct proto_ops caif_seqpacket_ops = {
1037 .family = PF_CAIF,
1038 .owner = THIS_MODULE,
1039 .release = caif_release,
1040 .bind = sock_no_bind,
1041 .connect = caif_connect,
1042 .socketpair = sock_no_socketpair,
1043 .accept = sock_no_accept,
1044 .getname = sock_no_getname,
1045 .poll = caif_poll,
1046 .ioctl = sock_no_ioctl,
1047 .listen = sock_no_listen,
1048 .shutdown = sock_no_shutdown,
1049 .setsockopt = setsockopt,
1050 .getsockopt = sock_no_getsockopt,
1051 .sendmsg = caif_seqpkt_sendmsg,
1052 .recvmsg = caif_seqpkt_recvmsg,
1053 .mmap = sock_no_mmap,
1054 .sendpage = sock_no_sendpage,
1055};
1056
1057static const struct proto_ops caif_stream_ops = {
1058 .family = PF_CAIF,
1059 .owner = THIS_MODULE,
1060 .release = caif_release,
1061 .bind = sock_no_bind,
1062 .connect = caif_connect,
1063 .socketpair = sock_no_socketpair,
1064 .accept = sock_no_accept,
1065 .getname = sock_no_getname,
1066 .poll = caif_poll,
1067 .ioctl = sock_no_ioctl,
1068 .listen = sock_no_listen,
1069 .shutdown = sock_no_shutdown,
1070 .setsockopt = setsockopt,
1071 .getsockopt = sock_no_getsockopt,
1072 .sendmsg = caif_stream_sendmsg,
1073 .recvmsg = caif_stream_recvmsg,
1074 .mmap = sock_no_mmap,
1075 .sendpage = sock_no_sendpage,
1076};
1077
1078/* This function is called when a socket is finally destroyed. */
1079static void caif_sock_destructor(struct sock *sk)
1080{
1081 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1082 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1083 caif_assert(sk_unhashed(sk));
1084 caif_assert(!sk->sk_socket);
1085 if (!sock_flag(sk, SOCK_DEAD)) {
1086 pr_info("Attempt to release alive CAIF socket: %p\n", sk);
1087 return;
1088 }
1089 sk_stream_kill_queues(&cf_sk->sk);
1090 dbfs_atomic_dec(&cnt.caif_nr_socks);
1091}
1092
1093static int caif_create(struct net *net, struct socket *sock, int protocol,
1094 int kern)
1095{
1096 struct sock *sk = NULL;
1097 struct caifsock *cf_sk = NULL;
1098 static struct proto prot = {.name = "PF_CAIF",
1099 .owner = THIS_MODULE,
1100 .obj_size = sizeof(struct caifsock),
1101 };
1102
1103 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1104 return -EPERM;
1105 /*
1106 * The sock->type specifies the socket type to use.
1107 * The CAIF socket is a packet stream in the sense
1108 * that it is packet based. CAIF trusts the reliability
1109 * of the link, no resending is implemented.
1110 */
1111 if (sock->type == SOCK_SEQPACKET)
1112 sock->ops = &caif_seqpacket_ops;
1113 else if (sock->type == SOCK_STREAM)
1114 sock->ops = &caif_stream_ops;
1115 else
1116 return -ESOCKTNOSUPPORT;
1117
1118 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1119 return -EPROTONOSUPPORT;
1120 /*
1121 * Set the socket state to unconnected. The socket state
1122 * is really not used at all in the net/core or socket.c but the
1123 * initialization makes sure that sock->state is not uninitialized.
1124 */
1125 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1126 if (!sk)
1127 return -ENOMEM;
1128
1129 cf_sk = container_of(sk, struct caifsock, sk);
1130
1131 /* Store the protocol */
1132 sk->sk_protocol = (unsigned char) protocol;
1133
1134 /* Sendbuf dictates the amount of outbound packets not yet sent */
1135 sk->sk_sndbuf = CAIF_DEF_SNDBUF;
1136 sk->sk_rcvbuf = CAIF_DEF_RCVBUF;
1137
1138 /*
1139 * Lock in order to try to stop someone from opening the socket
1140 * too early.
1141 */
1142 lock_sock(&(cf_sk->sk));
1143
1144 /* Initialize the nozero default sock structure data. */
1145 sock_init_data(sock, sk);
1146 sk->sk_destruct = caif_sock_destructor;
1147
1148 mutex_init(&cf_sk->readlock); /* single task reading lock */
1149 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1150 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1151 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
1152
1153 set_tx_flow_off(cf_sk);
1154 set_rx_flow_on(cf_sk);
1155
1156 /* Set default options on configuration */
1157 cf_sk->conn_req.priority = CAIF_PRIO_NORMAL;
1158 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
1159 cf_sk->conn_req.protocol = protocol;
1160 /* Increase the number of sockets created. */
1161 dbfs_atomic_inc(&cnt.caif_nr_socks);
1162#ifdef CONFIG_DEBUG_FS
1163 if (!IS_ERR(debugfsdir)) {
1164 /* Fill in some information concerning the misc socket. */
1165 snprintf(cf_sk->name, sizeof(cf_sk->name), "cfsk%d",
1166 atomic_read(&cnt.caif_nr_socks));
1167
1168 cf_sk->debugfs_socket_dir =
1169 debugfs_create_dir(cf_sk->name, debugfsdir);
1170 debugfs_create_u32("sk_state", S_IRUSR | S_IWUSR,
1171 cf_sk->debugfs_socket_dir,
1172 (u32 *) &cf_sk->sk.sk_state);
1173 debugfs_create_u32("flow_state", S_IRUSR | S_IWUSR,
1174 cf_sk->debugfs_socket_dir, &cf_sk->flow_state);
1175 debugfs_create_u32("sk_rmem_alloc", S_IRUSR | S_IWUSR,
1176 cf_sk->debugfs_socket_dir,
1177 (u32 *) &cf_sk->sk.sk_rmem_alloc);
1178 debugfs_create_u32("sk_wmem_alloc", S_IRUSR | S_IWUSR,
1179 cf_sk->debugfs_socket_dir,
1180 (u32 *) &cf_sk->sk.sk_wmem_alloc);
1181 debugfs_create_u32("identity", S_IRUSR | S_IWUSR,
1182 cf_sk->debugfs_socket_dir,
1183 (u32 *) &cf_sk->layer.id);
1184 }
1185#endif
1186 release_sock(&cf_sk->sk);
1187 return 0;
1188}
1189
1190
1191static struct net_proto_family caif_family_ops = {
1192 .family = PF_CAIF,
1193 .create = caif_create,
1194 .owner = THIS_MODULE,
1195};
1196
1197int af_caif_init(void)
1198{
1199 int err = sock_register(&caif_family_ops);
1200 if (!err)
1201 return err;
1202 return 0;
1203}
1204
1205static int __init caif_sktinit_module(void)
1206{
1207#ifdef CONFIG_DEBUG_FS
1208 debugfsdir = debugfs_create_dir("caif_sk", NULL);
1209 if (!IS_ERR(debugfsdir)) {
1210 debugfs_create_u32("num_sockets", S_IRUSR | S_IWUSR,
1211 debugfsdir,
1212 (u32 *) &cnt.caif_nr_socks);
1213 debugfs_create_u32("num_connect_req", S_IRUSR | S_IWUSR,
1214 debugfsdir,
1215 (u32 *) &cnt.num_connect_req);
1216 debugfs_create_u32("num_connect_resp", S_IRUSR | S_IWUSR,
1217 debugfsdir,
1218 (u32 *) &cnt.num_connect_resp);
1219 debugfs_create_u32("num_connect_fail_resp", S_IRUSR | S_IWUSR,
1220 debugfsdir,
1221 (u32 *) &cnt.num_connect_fail_resp);
1222 debugfs_create_u32("num_disconnect", S_IRUSR | S_IWUSR,
1223 debugfsdir,
1224 (u32 *) &cnt.num_disconnect);
1225 debugfs_create_u32("num_remote_shutdown_ind",
1226 S_IRUSR | S_IWUSR, debugfsdir,
1227 (u32 *) &cnt.num_remote_shutdown_ind);
1228 debugfs_create_u32("num_tx_flow_off_ind", S_IRUSR | S_IWUSR,
1229 debugfsdir,
1230 (u32 *) &cnt.num_tx_flow_off_ind);
1231 debugfs_create_u32("num_tx_flow_on_ind", S_IRUSR | S_IWUSR,
1232 debugfsdir,
1233 (u32 *) &cnt.num_tx_flow_on_ind);
1234 debugfs_create_u32("num_rx_flow_off", S_IRUSR | S_IWUSR,
1235 debugfsdir,
1236 (u32 *) &cnt.num_rx_flow_off);
1237 debugfs_create_u32("num_rx_flow_on", S_IRUSR | S_IWUSR,
1238 debugfsdir,
1239 (u32 *) &cnt.num_rx_flow_on);
1240 }
1241#endif
1242 return af_caif_init();
1243}
1244
1245static void __exit caif_sktexit_module(void)
1246{
1247 sock_unregister(PF_CAIF);
1248 if (debugfsdir != NULL)
1249 debugfs_remove_recursive(debugfsdir);
1250}
1251module_init(caif_sktinit_module);
1252module_exit(caif_sktexit_module);
diff --git a/net/caif/cfcnfg.c b/net/caif/cfcnfg.c
new file mode 100644
index 000000000000..df43f264d9fb
--- /dev/null
+++ b/net/caif/cfcnfg.c
@@ -0,0 +1,470 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6#include <linux/kernel.h>
7#include <linux/stddef.h>
8#include <linux/slab.h>
9#include <net/caif/caif_layer.h>
10#include <net/caif/cfpkt.h>
11#include <net/caif/cfcnfg.h>
12#include <net/caif/cfctrl.h>
13#include <net/caif/cfmuxl.h>
14#include <net/caif/cffrml.h>
15#include <net/caif/cfserl.h>
16#include <net/caif/cfsrvl.h>
17
18#include <linux/module.h>
19#include <asm/atomic.h>
20
21#define MAX_PHY_LAYERS 7
22#define PHY_NAME_LEN 20
23
24#define container_obj(layr) container_of(layr, struct cfcnfg, layer)
25
26/* Information about CAIF physical interfaces held by Config Module in order
27 * to manage physical interfaces
28 */
29struct cfcnfg_phyinfo {
30 /* Pointer to the layer below the MUX (framing layer) */
31 struct cflayer *frm_layer;
32 /* Pointer to the lowest actual physical layer */
33 struct cflayer *phy_layer;
34 /* Unique identifier of the physical interface */
35 unsigned int id;
36 /* Preference of the physical in interface */
37 enum cfcnfg_phy_preference pref;
38
39 /* Reference count, number of channels using the device */
40 int phy_ref_count;
41
42 /* Information about the physical device */
43 struct dev_info dev_info;
44};
45
46struct cfcnfg {
47 struct cflayer layer;
48 struct cflayer *ctrl;
49 struct cflayer *mux;
50 u8 last_phyid;
51 struct cfcnfg_phyinfo phy_layers[MAX_PHY_LAYERS];
52};
53
54static void cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id,
55 enum cfctrl_srv serv, u8 phyid,
56 struct cflayer *adapt_layer);
57static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id);
58static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
59 struct cflayer *adapt_layer);
60static void cfctrl_resp_func(void);
61static void cfctrl_enum_resp(void);
62
63struct cfcnfg *cfcnfg_create(void)
64{
65 struct cfcnfg *this;
66 struct cfctrl_rsp *resp;
67 /* Initiate this layer */
68 this = kzalloc(sizeof(struct cfcnfg), GFP_ATOMIC);
69 if (!this) {
70 pr_warning("CAIF: %s(): Out of memory\n", __func__);
71 return NULL;
72 }
73 this->mux = cfmuxl_create();
74 if (!this->mux)
75 goto out_of_mem;
76 this->ctrl = cfctrl_create();
77 if (!this->ctrl)
78 goto out_of_mem;
79 /* Initiate response functions */
80 resp = cfctrl_get_respfuncs(this->ctrl);
81 resp->enum_rsp = cfctrl_enum_resp;
82 resp->linkerror_ind = cfctrl_resp_func;
83 resp->linkdestroy_rsp = cfcnfg_linkdestroy_rsp;
84 resp->sleep_rsp = cfctrl_resp_func;
85 resp->wake_rsp = cfctrl_resp_func;
86 resp->restart_rsp = cfctrl_resp_func;
87 resp->radioset_rsp = cfctrl_resp_func;
88 resp->linksetup_rsp = cfcnfg_linkup_rsp;
89 resp->reject_rsp = cfcnfg_reject_rsp;
90
91 this->last_phyid = 1;
92
93 cfmuxl_set_uplayer(this->mux, this->ctrl, 0);
94 layer_set_dn(this->ctrl, this->mux);
95 layer_set_up(this->ctrl, this);
96 return this;
97out_of_mem:
98 pr_warning("CAIF: %s(): Out of memory\n", __func__);
99 kfree(this->mux);
100 kfree(this->ctrl);
101 kfree(this);
102 return NULL;
103}
104EXPORT_SYMBOL(cfcnfg_create);
105
106void cfcnfg_remove(struct cfcnfg *cfg)
107{
108 if (cfg) {
109 kfree(cfg->mux);
110 kfree(cfg->ctrl);
111 kfree(cfg);
112 }
113}
114
115static void cfctrl_resp_func(void)
116{
117}
118
119static void cfctrl_enum_resp(void)
120{
121}
122
123struct dev_info *cfcnfg_get_phyid(struct cfcnfg *cnfg,
124 enum cfcnfg_phy_preference phy_pref)
125{
126 u16 i;
127
128 /* Try to match with specified preference */
129 for (i = 1; i < MAX_PHY_LAYERS; i++) {
130 if (cnfg->phy_layers[i].id == i &&
131 cnfg->phy_layers[i].pref == phy_pref &&
132 cnfg->phy_layers[i].frm_layer != NULL) {
133 caif_assert(cnfg->phy_layers != NULL);
134 caif_assert(cnfg->phy_layers[i].id == i);
135 return &cnfg->phy_layers[i].dev_info;
136 }
137 }
138 /* Otherwise just return something */
139 for (i = 1; i < MAX_PHY_LAYERS; i++) {
140 if (cnfg->phy_layers[i].id == i) {
141 caif_assert(cnfg->phy_layers != NULL);
142 caif_assert(cnfg->phy_layers[i].id == i);
143 return &cnfg->phy_layers[i].dev_info;
144 }
145 }
146
147 return NULL;
148}
149
150static struct cfcnfg_phyinfo *cfcnfg_get_phyinfo(struct cfcnfg *cnfg,
151 u8 phyid)
152{
153 int i;
154 /* Try to match with specified preference */
155 for (i = 0; i < MAX_PHY_LAYERS; i++)
156 if (cnfg->phy_layers[i].frm_layer != NULL &&
157 cnfg->phy_layers[i].id == phyid)
158 return &cnfg->phy_layers[i];
159 return NULL;
160}
161
162int cfcnfg_get_named(struct cfcnfg *cnfg, char *name)
163{
164 int i;
165
166 /* Try to match with specified name */
167 for (i = 0; i < MAX_PHY_LAYERS; i++) {
168 if (cnfg->phy_layers[i].frm_layer != NULL
169 && strcmp(cnfg->phy_layers[i].phy_layer->name,
170 name) == 0)
171 return cnfg->phy_layers[i].frm_layer->id;
172 }
173 return 0;
174}
175
176int cfcnfg_disconn_adapt_layer(struct cfcnfg *cnfg, struct cflayer *adap_layer)
177{
178 u8 channel_id = 0;
179 int ret = 0;
180 struct cflayer *servl = NULL;
181 struct cfcnfg_phyinfo *phyinfo = NULL;
182 u8 phyid = 0;
183 caif_assert(adap_layer != NULL);
184 channel_id = adap_layer->id;
185 if (adap_layer->dn == NULL || channel_id == 0) {
186 pr_err("CAIF: %s():adap_layer->id is 0\n", __func__);
187 ret = -ENOTCONN;
188 goto end;
189 }
190 servl = cfmuxl_remove_uplayer(cnfg->mux, channel_id);
191 if (servl == NULL)
192 goto end;
193 layer_set_up(servl, NULL);
194 ret = cfctrl_linkdown_req(cnfg->ctrl, channel_id, adap_layer);
195 if (servl == NULL) {
196 pr_err("CAIF: %s(): PROTOCOL ERROR "
197 "- Error removing service_layer Channel_Id(%d)",
198 __func__, channel_id);
199 ret = -EINVAL;
200 goto end;
201 }
202 caif_assert(channel_id == servl->id);
203 if (adap_layer->dn != NULL) {
204 phyid = cfsrvl_getphyid(adap_layer->dn);
205
206 phyinfo = cfcnfg_get_phyinfo(cnfg, phyid);
207 if (phyinfo == NULL) {
208 pr_warning("CAIF: %s(): "
209 "No interface to send disconnect to\n",
210 __func__);
211 ret = -ENODEV;
212 goto end;
213 }
214 if (phyinfo->id != phyid ||
215 phyinfo->phy_layer->id != phyid ||
216 phyinfo->frm_layer->id != phyid) {
217 pr_err("CAIF: %s(): "
218 "Inconsistency in phy registration\n",
219 __func__);
220 ret = -EINVAL;
221 goto end;
222 }
223 }
224 if (phyinfo != NULL && --phyinfo->phy_ref_count == 0 &&
225 phyinfo->phy_layer != NULL &&
226 phyinfo->phy_layer->modemcmd != NULL) {
227 phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
228 _CAIF_MODEMCMD_PHYIF_USELESS);
229 }
230end:
231 cfsrvl_put(servl);
232 cfctrl_cancel_req(cnfg->ctrl, adap_layer);
233 if (adap_layer->ctrlcmd != NULL)
234 adap_layer->ctrlcmd(adap_layer, CAIF_CTRLCMD_DEINIT_RSP, 0);
235 return ret;
236
237}
238EXPORT_SYMBOL(cfcnfg_disconn_adapt_layer);
239
240void cfcnfg_release_adap_layer(struct cflayer *adap_layer)
241{
242 if (adap_layer->dn)
243 cfsrvl_put(adap_layer->dn);
244}
245EXPORT_SYMBOL(cfcnfg_release_adap_layer);
246
247static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id)
248{
249}
250
251int cfcnfg_add_adaptation_layer(struct cfcnfg *cnfg,
252 struct cfctrl_link_param *param,
253 struct cflayer *adap_layer)
254{
255 struct cflayer *frml;
256 if (adap_layer == NULL) {
257 pr_err("CAIF: %s(): adap_layer is zero", __func__);
258 return -EINVAL;
259 }
260 if (adap_layer->receive == NULL) {
261 pr_err("CAIF: %s(): adap_layer->receive is NULL", __func__);
262 return -EINVAL;
263 }
264 if (adap_layer->ctrlcmd == NULL) {
265 pr_err("CAIF: %s(): adap_layer->ctrlcmd == NULL", __func__);
266 return -EINVAL;
267 }
268 frml = cnfg->phy_layers[param->phyid].frm_layer;
269 if (frml == NULL) {
270 pr_err("CAIF: %s(): Specified PHY type does not exist!",
271 __func__);
272 return -ENODEV;
273 }
274 caif_assert(param->phyid == cnfg->phy_layers[param->phyid].id);
275 caif_assert(cnfg->phy_layers[param->phyid].frm_layer->id ==
276 param->phyid);
277 caif_assert(cnfg->phy_layers[param->phyid].phy_layer->id ==
278 param->phyid);
279 /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */
280 cfctrl_enum_req(cnfg->ctrl, param->phyid);
281 return cfctrl_linkup_request(cnfg->ctrl, param, adap_layer);
282}
283EXPORT_SYMBOL(cfcnfg_add_adaptation_layer);
284
285static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
286 struct cflayer *adapt_layer)
287{
288 if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
289 adapt_layer->ctrlcmd(adapt_layer,
290 CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
291}
292
293static void
294cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv,
295 u8 phyid, struct cflayer *adapt_layer)
296{
297 struct cfcnfg *cnfg = container_obj(layer);
298 struct cflayer *servicel = NULL;
299 struct cfcnfg_phyinfo *phyinfo;
300 if (adapt_layer == NULL) {
301 pr_debug("CAIF: %s(): link setup response "
302 "but no client exist, send linkdown back\n",
303 __func__);
304 cfctrl_linkdown_req(cnfg->ctrl, channel_id, NULL);
305 return;
306 }
307
308 caif_assert(cnfg != NULL);
309 caif_assert(phyid != 0);
310 phyinfo = &cnfg->phy_layers[phyid];
311 caif_assert(phyinfo != NULL);
312 caif_assert(phyinfo->id == phyid);
313 caif_assert(phyinfo->phy_layer != NULL);
314 caif_assert(phyinfo->phy_layer->id == phyid);
315
316 if (phyinfo != NULL &&
317 phyinfo->phy_ref_count++ == 0 &&
318 phyinfo->phy_layer != NULL &&
319 phyinfo->phy_layer->modemcmd != NULL) {
320 caif_assert(phyinfo->phy_layer->id == phyid);
321 phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
322 _CAIF_MODEMCMD_PHYIF_USEFULL);
323
324 }
325 adapt_layer->id = channel_id;
326
327 switch (serv) {
328 case CFCTRL_SRV_VEI:
329 servicel = cfvei_create(channel_id, &phyinfo->dev_info);
330 break;
331 case CFCTRL_SRV_DATAGRAM:
332 servicel = cfdgml_create(channel_id, &phyinfo->dev_info);
333 break;
334 case CFCTRL_SRV_RFM:
335 servicel = cfrfml_create(channel_id, &phyinfo->dev_info);
336 break;
337 case CFCTRL_SRV_UTIL:
338 servicel = cfutill_create(channel_id, &phyinfo->dev_info);
339 break;
340 case CFCTRL_SRV_VIDEO:
341 servicel = cfvidl_create(channel_id, &phyinfo->dev_info);
342 break;
343 case CFCTRL_SRV_DBG:
344 servicel = cfdbgl_create(channel_id, &phyinfo->dev_info);
345 break;
346 default:
347 pr_err("CAIF: %s(): Protocol error. "
348 "Link setup response - unknown channel type\n",
349 __func__);
350 return;
351 }
352 if (!servicel) {
353 pr_warning("CAIF: %s(): Out of memory\n", __func__);
354 return;
355 }
356 layer_set_dn(servicel, cnfg->mux);
357 cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id);
358 layer_set_up(servicel, adapt_layer);
359 layer_set_dn(adapt_layer, servicel);
360 cfsrvl_get(servicel);
361 servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0);
362}
363
364void
365cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
366 void *dev, struct cflayer *phy_layer, u16 *phyid,
367 enum cfcnfg_phy_preference pref,
368 bool fcs, bool stx)
369{
370 struct cflayer *frml;
371 struct cflayer *phy_driver = NULL;
372 int i;
373
374
375 if (cnfg->phy_layers[cnfg->last_phyid].frm_layer == NULL) {
376 *phyid = cnfg->last_phyid;
377
378 /* range: * 1..(MAX_PHY_LAYERS-1) */
379 cnfg->last_phyid =
380 (cnfg->last_phyid % (MAX_PHY_LAYERS - 1)) + 1;
381 } else {
382 *phyid = 0;
383 for (i = 1; i < MAX_PHY_LAYERS; i++) {
384 if (cnfg->phy_layers[i].frm_layer == NULL) {
385 *phyid = i;
386 break;
387 }
388 }
389 }
390 if (*phyid == 0) {
391 pr_err("CAIF: %s(): No Available PHY ID\n", __func__);
392 return;
393 }
394
395 switch (phy_type) {
396 case CFPHYTYPE_FRAG:
397 phy_driver =
398 cfserl_create(CFPHYTYPE_FRAG, *phyid, stx);
399 if (!phy_driver) {
400 pr_warning("CAIF: %s(): Out of memory\n", __func__);
401 return;
402 }
403
404 break;
405 case CFPHYTYPE_CAIF:
406 phy_driver = NULL;
407 break;
408 default:
409 pr_err("CAIF: %s(): %d", __func__, phy_type);
410 return;
411 break;
412 }
413
414 phy_layer->id = *phyid;
415 cnfg->phy_layers[*phyid].pref = pref;
416 cnfg->phy_layers[*phyid].id = *phyid;
417 cnfg->phy_layers[*phyid].dev_info.id = *phyid;
418 cnfg->phy_layers[*phyid].dev_info.dev = dev;
419 cnfg->phy_layers[*phyid].phy_layer = phy_layer;
420 cnfg->phy_layers[*phyid].phy_ref_count = 0;
421 phy_layer->type = phy_type;
422 frml = cffrml_create(*phyid, fcs);
423 if (!frml) {
424 pr_warning("CAIF: %s(): Out of memory\n", __func__);
425 return;
426 }
427 cnfg->phy_layers[*phyid].frm_layer = frml;
428 cfmuxl_set_dnlayer(cnfg->mux, frml, *phyid);
429 layer_set_up(frml, cnfg->mux);
430
431 if (phy_driver != NULL) {
432 phy_driver->id = *phyid;
433 layer_set_dn(frml, phy_driver);
434 layer_set_up(phy_driver, frml);
435 layer_set_dn(phy_driver, phy_layer);
436 layer_set_up(phy_layer, phy_driver);
437 } else {
438 layer_set_dn(frml, phy_layer);
439 layer_set_up(phy_layer, frml);
440 }
441}
442EXPORT_SYMBOL(cfcnfg_add_phy_layer);
443
444int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer)
445{
446 struct cflayer *frml, *frml_dn;
447 u16 phyid;
448 phyid = phy_layer->id;
449 caif_assert(phyid == cnfg->phy_layers[phyid].id);
450 caif_assert(phy_layer == cnfg->phy_layers[phyid].phy_layer);
451 caif_assert(phy_layer->id == phyid);
452 caif_assert(cnfg->phy_layers[phyid].frm_layer->id == phyid);
453
454 memset(&cnfg->phy_layers[phy_layer->id], 0,
455 sizeof(struct cfcnfg_phyinfo));
456 frml = cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id);
457 frml_dn = frml->dn;
458 cffrml_set_uplayer(frml, NULL);
459 cffrml_set_dnlayer(frml, NULL);
460 kfree(frml);
461
462 if (phy_layer != frml_dn) {
463 layer_set_up(frml_dn, NULL);
464 layer_set_dn(frml_dn, NULL);
465 kfree(frml_dn);
466 }
467 layer_set_up(phy_layer, NULL);
468 return 0;
469}
470EXPORT_SYMBOL(cfcnfg_del_phy_layer);
diff --git a/net/caif/cfctrl.c b/net/caif/cfctrl.c
new file mode 100644
index 000000000000..0ffe1e1ce901
--- /dev/null
+++ b/net/caif/cfctrl.c
@@ -0,0 +1,692 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/stddef.h>
8#include <linux/spinlock.h>
9#include <linux/slab.h>
10#include <net/caif/caif_layer.h>
11#include <net/caif/cfpkt.h>
12#include <net/caif/cfctrl.h>
13
14#define container_obj(layr) container_of(layr, struct cfctrl, serv.layer)
15#define UTILITY_NAME_LENGTH 16
16#define CFPKT_CTRL_PKT_LEN 20
17
18
19#ifdef CAIF_NO_LOOP
20static int handle_loop(struct cfctrl *ctrl,
21 int cmd, struct cfpkt *pkt){
22 return CAIF_FAILURE;
23}
24#else
25static int handle_loop(struct cfctrl *ctrl,
26 int cmd, struct cfpkt *pkt);
27#endif
28static int cfctrl_recv(struct cflayer *layr, struct cfpkt *pkt);
29static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
30 int phyid);
31
32
33struct cflayer *cfctrl_create(void)
34{
35 struct dev_info dev_info;
36 struct cfctrl *this =
37 kmalloc(sizeof(struct cfctrl), GFP_ATOMIC);
38 if (!this) {
39 pr_warning("CAIF: %s(): Out of memory\n", __func__);
40 return NULL;
41 }
42 caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
43 memset(&dev_info, 0, sizeof(dev_info));
44 dev_info.id = 0xff;
45 memset(this, 0, sizeof(*this));
46 cfsrvl_init(&this->serv, 0, &dev_info);
47 spin_lock_init(&this->info_list_lock);
48 atomic_set(&this->req_seq_no, 1);
49 atomic_set(&this->rsp_seq_no, 1);
50 this->serv.layer.receive = cfctrl_recv;
51 sprintf(this->serv.layer.name, "ctrl");
52 this->serv.layer.ctrlcmd = cfctrl_ctrlcmd;
53 spin_lock_init(&this->loop_linkid_lock);
54 this->loop_linkid = 1;
55 return &this->serv.layer;
56}
57
58static bool param_eq(struct cfctrl_link_param *p1, struct cfctrl_link_param *p2)
59{
60 bool eq =
61 p1->linktype == p2->linktype &&
62 p1->priority == p2->priority &&
63 p1->phyid == p2->phyid &&
64 p1->endpoint == p2->endpoint && p1->chtype == p2->chtype;
65
66 if (!eq)
67 return false;
68
69 switch (p1->linktype) {
70 case CFCTRL_SRV_VEI:
71 return true;
72 case CFCTRL_SRV_DATAGRAM:
73 return p1->u.datagram.connid == p2->u.datagram.connid;
74 case CFCTRL_SRV_RFM:
75 return
76 p1->u.rfm.connid == p2->u.rfm.connid &&
77 strcmp(p1->u.rfm.volume, p2->u.rfm.volume) == 0;
78 case CFCTRL_SRV_UTIL:
79 return
80 p1->u.utility.fifosize_kb == p2->u.utility.fifosize_kb
81 && p1->u.utility.fifosize_bufs ==
82 p2->u.utility.fifosize_bufs
83 && strcmp(p1->u.utility.name, p2->u.utility.name) == 0
84 && p1->u.utility.paramlen == p2->u.utility.paramlen
85 && memcmp(p1->u.utility.params, p2->u.utility.params,
86 p1->u.utility.paramlen) == 0;
87
88 case CFCTRL_SRV_VIDEO:
89 return p1->u.video.connid == p2->u.video.connid;
90 case CFCTRL_SRV_DBG:
91 return true;
92 case CFCTRL_SRV_DECM:
93 return false;
94 default:
95 return false;
96 }
97 return false;
98}
99
100bool cfctrl_req_eq(struct cfctrl_request_info *r1,
101 struct cfctrl_request_info *r2)
102{
103 if (r1->cmd != r2->cmd)
104 return false;
105 if (r1->cmd == CFCTRL_CMD_LINK_SETUP)
106 return param_eq(&r1->param, &r2->param);
107 else
108 return r1->channel_id == r2->channel_id;
109}
110
111/* Insert request at the end */
112void cfctrl_insert_req(struct cfctrl *ctrl,
113 struct cfctrl_request_info *req)
114{
115 struct cfctrl_request_info *p;
116 spin_lock(&ctrl->info_list_lock);
117 req->next = NULL;
118 atomic_inc(&ctrl->req_seq_no);
119 req->sequence_no = atomic_read(&ctrl->req_seq_no);
120 if (ctrl->first_req == NULL) {
121 ctrl->first_req = req;
122 spin_unlock(&ctrl->info_list_lock);
123 return;
124 }
125 p = ctrl->first_req;
126 while (p->next != NULL)
127 p = p->next;
128 p->next = req;
129 spin_unlock(&ctrl->info_list_lock);
130}
131
132/* Compare and remove request */
133struct cfctrl_request_info *cfctrl_remove_req(struct cfctrl *ctrl,
134 struct cfctrl_request_info *req)
135{
136 struct cfctrl_request_info *p;
137 struct cfctrl_request_info *ret;
138
139 spin_lock(&ctrl->info_list_lock);
140 if (ctrl->first_req == NULL) {
141 spin_unlock(&ctrl->info_list_lock);
142 return NULL;
143 }
144
145 if (cfctrl_req_eq(req, ctrl->first_req)) {
146 ret = ctrl->first_req;
147 caif_assert(ctrl->first_req);
148 atomic_set(&ctrl->rsp_seq_no,
149 ctrl->first_req->sequence_no);
150 ctrl->first_req = ctrl->first_req->next;
151 spin_unlock(&ctrl->info_list_lock);
152 return ret;
153 }
154
155 p = ctrl->first_req;
156
157 while (p->next != NULL) {
158 if (cfctrl_req_eq(req, p->next)) {
159 pr_warning("CAIF: %s(): Requests are not "
160 "received in order\n",
161 __func__);
162 ret = p->next;
163 atomic_set(&ctrl->rsp_seq_no,
164 p->next->sequence_no);
165 p->next = p->next->next;
166 spin_unlock(&ctrl->info_list_lock);
167 return ret;
168 }
169 p = p->next;
170 }
171 spin_unlock(&ctrl->info_list_lock);
172
173 pr_warning("CAIF: %s(): Request does not match\n",
174 __func__);
175 return NULL;
176}
177
178struct cfctrl_rsp *cfctrl_get_respfuncs(struct cflayer *layer)
179{
180 struct cfctrl *this = container_obj(layer);
181 return &this->res;
182}
183
184void cfctrl_set_dnlayer(struct cflayer *this, struct cflayer *dn)
185{
186 this->dn = dn;
187}
188
189void cfctrl_set_uplayer(struct cflayer *this, struct cflayer *up)
190{
191 this->up = up;
192}
193
194static void init_info(struct caif_payload_info *info, struct cfctrl *cfctrl)
195{
196 info->hdr_len = 0;
197 info->channel_id = cfctrl->serv.layer.id;
198 info->dev_info = &cfctrl->serv.dev_info;
199}
200
201void cfctrl_enum_req(struct cflayer *layer, u8 physlinkid)
202{
203 struct cfctrl *cfctrl = container_obj(layer);
204 int ret;
205 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
206 if (!pkt) {
207 pr_warning("CAIF: %s(): Out of memory\n", __func__);
208 return;
209 }
210 caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
211 init_info(cfpkt_info(pkt), cfctrl);
212 cfpkt_info(pkt)->dev_info->id = physlinkid;
213 cfctrl->serv.dev_info.id = physlinkid;
214 cfpkt_addbdy(pkt, CFCTRL_CMD_ENUM);
215 cfpkt_addbdy(pkt, physlinkid);
216 ret =
217 cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
218 if (ret < 0) {
219 pr_err("CAIF: %s(): Could not transmit enum message\n",
220 __func__);
221 cfpkt_destroy(pkt);
222 }
223}
224
225int cfctrl_linkup_request(struct cflayer *layer,
226 struct cfctrl_link_param *param,
227 struct cflayer *user_layer)
228{
229 struct cfctrl *cfctrl = container_obj(layer);
230 u32 tmp32;
231 u16 tmp16;
232 u8 tmp8;
233 struct cfctrl_request_info *req;
234 int ret;
235 char utility_name[16];
236 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
237 if (!pkt) {
238 pr_warning("CAIF: %s(): Out of memory\n", __func__);
239 return -ENOMEM;
240 }
241 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP);
242 cfpkt_addbdy(pkt, (param->chtype << 4) + param->linktype);
243 cfpkt_addbdy(pkt, (param->priority << 3) + param->phyid);
244 cfpkt_addbdy(pkt, param->endpoint & 0x03);
245
246 switch (param->linktype) {
247 case CFCTRL_SRV_VEI:
248 break;
249 case CFCTRL_SRV_VIDEO:
250 cfpkt_addbdy(pkt, (u8) param->u.video.connid);
251 break;
252 case CFCTRL_SRV_DBG:
253 break;
254 case CFCTRL_SRV_DATAGRAM:
255 tmp32 = cpu_to_le32(param->u.datagram.connid);
256 cfpkt_add_body(pkt, &tmp32, 4);
257 break;
258 case CFCTRL_SRV_RFM:
259 /* Construct a frame, convert DatagramConnectionID to network
260 * format long and copy it out...
261 */
262 tmp32 = cpu_to_le32(param->u.rfm.connid);
263 cfpkt_add_body(pkt, &tmp32, 4);
264 /* Add volume name, including zero termination... */
265 cfpkt_add_body(pkt, param->u.rfm.volume,
266 strlen(param->u.rfm.volume) + 1);
267 break;
268 case CFCTRL_SRV_UTIL:
269 tmp16 = cpu_to_le16(param->u.utility.fifosize_kb);
270 cfpkt_add_body(pkt, &tmp16, 2);
271 tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs);
272 cfpkt_add_body(pkt, &tmp16, 2);
273 memset(utility_name, 0, sizeof(utility_name));
274 strncpy(utility_name, param->u.utility.name,
275 UTILITY_NAME_LENGTH - 1);
276 cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH);
277 tmp8 = param->u.utility.paramlen;
278 cfpkt_add_body(pkt, &tmp8, 1);
279 cfpkt_add_body(pkt, param->u.utility.params,
280 param->u.utility.paramlen);
281 break;
282 default:
283 pr_warning("CAIF: %s():Request setup of bad link type = %d\n",
284 __func__, param->linktype);
285 return -EINVAL;
286 }
287 req = kzalloc(sizeof(*req), GFP_KERNEL);
288 if (!req) {
289 pr_warning("CAIF: %s(): Out of memory\n", __func__);
290 return -ENOMEM;
291 }
292 req->client_layer = user_layer;
293 req->cmd = CFCTRL_CMD_LINK_SETUP;
294 req->param = *param;
295 cfctrl_insert_req(cfctrl, req);
296 init_info(cfpkt_info(pkt), cfctrl);
297 /*
298 * NOTE:Always send linkup and linkdown request on the same
299 * device as the payload. Otherwise old queued up payload
300 * might arrive with the newly allocated channel ID.
301 */
302 cfpkt_info(pkt)->dev_info->id = param->phyid;
303 ret =
304 cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
305 if (ret < 0) {
306 pr_err("CAIF: %s(): Could not transmit linksetup request\n",
307 __func__);
308 cfpkt_destroy(pkt);
309 return -ENODEV;
310 }
311 return 0;
312}
313
314int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid,
315 struct cflayer *client)
316{
317 int ret;
318 struct cfctrl *cfctrl = container_obj(layer);
319 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
320 if (!pkt) {
321 pr_warning("CAIF: %s(): Out of memory\n", __func__);
322 return -ENOMEM;
323 }
324 cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY);
325 cfpkt_addbdy(pkt, channelid);
326 init_info(cfpkt_info(pkt), cfctrl);
327 ret =
328 cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
329 if (ret < 0) {
330 pr_err("CAIF: %s(): Could not transmit link-down request\n",
331 __func__);
332 cfpkt_destroy(pkt);
333 }
334 return ret;
335}
336
337void cfctrl_sleep_req(struct cflayer *layer)
338{
339 int ret;
340 struct cfctrl *cfctrl = container_obj(layer);
341 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
342 if (!pkt) {
343 pr_warning("CAIF: %s(): Out of memory\n", __func__);
344 return;
345 }
346 cfpkt_addbdy(pkt, CFCTRL_CMD_SLEEP);
347 init_info(cfpkt_info(pkt), cfctrl);
348 ret =
349 cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
350 if (ret < 0)
351 cfpkt_destroy(pkt);
352}
353
354void cfctrl_wake_req(struct cflayer *layer)
355{
356 int ret;
357 struct cfctrl *cfctrl = container_obj(layer);
358 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
359 if (!pkt) {
360 pr_warning("CAIF: %s(): Out of memory\n", __func__);
361 return;
362 }
363 cfpkt_addbdy(pkt, CFCTRL_CMD_WAKE);
364 init_info(cfpkt_info(pkt), cfctrl);
365 ret =
366 cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
367 if (ret < 0)
368 cfpkt_destroy(pkt);
369}
370
371void cfctrl_getstartreason_req(struct cflayer *layer)
372{
373 int ret;
374 struct cfctrl *cfctrl = container_obj(layer);
375 struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
376 if (!pkt) {
377 pr_warning("CAIF: %s(): Out of memory\n", __func__);
378 return;
379 }
380 cfpkt_addbdy(pkt, CFCTRL_CMD_START_REASON);
381 init_info(cfpkt_info(pkt), cfctrl);
382 ret =
383 cfctrl->serv.layer.dn->transmit(cfctrl->serv.layer.dn, pkt);
384 if (ret < 0)
385 cfpkt_destroy(pkt);
386}
387
388
389void cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer)
390{
391 struct cfctrl_request_info *p, *req;
392 struct cfctrl *ctrl = container_obj(layr);
393 spin_lock(&ctrl->info_list_lock);
394
395 if (ctrl->first_req == NULL) {
396 spin_unlock(&ctrl->info_list_lock);
397 return;
398 }
399
400 if (ctrl->first_req->client_layer == adap_layer) {
401
402 req = ctrl->first_req;
403 ctrl->first_req = ctrl->first_req->next;
404 kfree(req);
405 }
406
407 p = ctrl->first_req;
408 while (p != NULL && p->next != NULL) {
409 if (p->next->client_layer == adap_layer) {
410
411 req = p->next;
412 p->next = p->next->next;
413 kfree(p->next);
414 }
415 p = p->next;
416 }
417
418 spin_unlock(&ctrl->info_list_lock);
419}
420
421static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt)
422{
423 u8 cmdrsp;
424 u8 cmd;
425 int ret = -1;
426 u16 tmp16;
427 u8 len;
428 u8 param[255];
429 u8 linkid;
430 struct cfctrl *cfctrl = container_obj(layer);
431 struct cfctrl_request_info rsp, *req;
432
433
434 cfpkt_extr_head(pkt, &cmdrsp, 1);
435 cmd = cmdrsp & CFCTRL_CMD_MASK;
436 if (cmd != CFCTRL_CMD_LINK_ERR
437 && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)) {
438 if (handle_loop(cfctrl, cmd, pkt) == CAIF_FAILURE)
439 cmdrsp |= CFCTRL_ERR_BIT;
440 }
441
442 switch (cmd) {
443 case CFCTRL_CMD_LINK_SETUP:
444 {
445 enum cfctrl_srv serv;
446 enum cfctrl_srv servtype;
447 u8 endpoint;
448 u8 physlinkid;
449 u8 prio;
450 u8 tmp;
451 u32 tmp32;
452 u8 *cp;
453 int i;
454 struct cfctrl_link_param linkparam;
455 memset(&linkparam, 0, sizeof(linkparam));
456
457 cfpkt_extr_head(pkt, &tmp, 1);
458
459 serv = tmp & CFCTRL_SRV_MASK;
460 linkparam.linktype = serv;
461
462 servtype = tmp >> 4;
463 linkparam.chtype = servtype;
464
465 cfpkt_extr_head(pkt, &tmp, 1);
466 physlinkid = tmp & 0x07;
467 prio = tmp >> 3;
468
469 linkparam.priority = prio;
470 linkparam.phyid = physlinkid;
471 cfpkt_extr_head(pkt, &endpoint, 1);
472 linkparam.endpoint = endpoint & 0x03;
473
474 switch (serv) {
475 case CFCTRL_SRV_VEI:
476 case CFCTRL_SRV_DBG:
477 if (CFCTRL_ERR_BIT & cmdrsp)
478 break;
479 /* Link ID */
480 cfpkt_extr_head(pkt, &linkid, 1);
481 break;
482 case CFCTRL_SRV_VIDEO:
483 cfpkt_extr_head(pkt, &tmp, 1);
484 linkparam.u.video.connid = tmp;
485 if (CFCTRL_ERR_BIT & cmdrsp)
486 break;
487 /* Link ID */
488 cfpkt_extr_head(pkt, &linkid, 1);
489 break;
490
491 case CFCTRL_SRV_DATAGRAM:
492 cfpkt_extr_head(pkt, &tmp32, 4);
493 linkparam.u.datagram.connid =
494 le32_to_cpu(tmp32);
495 if (CFCTRL_ERR_BIT & cmdrsp)
496 break;
497 /* Link ID */
498 cfpkt_extr_head(pkt, &linkid, 1);
499 break;
500 case CFCTRL_SRV_RFM:
501 /* Construct a frame, convert
502 * DatagramConnectionID
503 * to network format long and copy it out...
504 */
505 cfpkt_extr_head(pkt, &tmp32, 4);
506 linkparam.u.rfm.connid =
507 le32_to_cpu(tmp32);
508 cp = (u8 *) linkparam.u.rfm.volume;
509 for (cfpkt_extr_head(pkt, &tmp, 1);
510 cfpkt_more(pkt) && tmp != '\0';
511 cfpkt_extr_head(pkt, &tmp, 1))
512 *cp++ = tmp;
513 *cp = '\0';
514
515 if (CFCTRL_ERR_BIT & cmdrsp)
516 break;
517 /* Link ID */
518 cfpkt_extr_head(pkt, &linkid, 1);
519
520 break;
521 case CFCTRL_SRV_UTIL:
522 /* Construct a frame, convert
523 * DatagramConnectionID
524 * to network format long and copy it out...
525 */
526 /* Fifosize KB */
527 cfpkt_extr_head(pkt, &tmp16, 2);
528 linkparam.u.utility.fifosize_kb =
529 le16_to_cpu(tmp16);
530 /* Fifosize bufs */
531 cfpkt_extr_head(pkt, &tmp16, 2);
532 linkparam.u.utility.fifosize_bufs =
533 le16_to_cpu(tmp16);
534 /* name */
535 cp = (u8 *) linkparam.u.utility.name;
536 caif_assert(sizeof(linkparam.u.utility.name)
537 >= UTILITY_NAME_LENGTH);
538 for (i = 0;
539 i < UTILITY_NAME_LENGTH
540 && cfpkt_more(pkt); i++) {
541 cfpkt_extr_head(pkt, &tmp, 1);
542 *cp++ = tmp;
543 }
544 /* Length */
545 cfpkt_extr_head(pkt, &len, 1);
546 linkparam.u.utility.paramlen = len;
547 /* Param Data */
548 cp = linkparam.u.utility.params;
549 while (cfpkt_more(pkt) && len--) {
550 cfpkt_extr_head(pkt, &tmp, 1);
551 *cp++ = tmp;
552 }
553 if (CFCTRL_ERR_BIT & cmdrsp)
554 break;
555 /* Link ID */
556 cfpkt_extr_head(pkt, &linkid, 1);
557 /* Length */
558 cfpkt_extr_head(pkt, &len, 1);
559 /* Param Data */
560 cfpkt_extr_head(pkt, &param, len);
561 break;
562 default:
563 pr_warning("CAIF: %s(): Request setup "
564 "- invalid link type (%d)",
565 __func__, serv);
566 goto error;
567 }
568
569 rsp.cmd = cmd;
570 rsp.param = linkparam;
571 req = cfctrl_remove_req(cfctrl, &rsp);
572
573 if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
574 cfpkt_erroneous(pkt)) {
575 pr_err("CAIF: %s(): Invalid O/E bit or parse "
576 "error on CAIF control channel",
577 __func__);
578 cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
579 0,
580 req ? req->client_layer
581 : NULL);
582 } else {
583 cfctrl->res.linksetup_rsp(cfctrl->serv.
584 layer.up, linkid,
585 serv, physlinkid,
586 req ? req->
587 client_layer : NULL);
588 }
589
590 if (req != NULL)
591 kfree(req);
592 }
593 break;
594 case CFCTRL_CMD_LINK_DESTROY:
595 cfpkt_extr_head(pkt, &linkid, 1);
596 cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
597 break;
598 case CFCTRL_CMD_LINK_ERR:
599 pr_err("CAIF: %s(): Frame Error Indication received\n",
600 __func__);
601 cfctrl->res.linkerror_ind();
602 break;
603 case CFCTRL_CMD_ENUM:
604 cfctrl->res.enum_rsp();
605 break;
606 case CFCTRL_CMD_SLEEP:
607 cfctrl->res.sleep_rsp();
608 break;
609 case CFCTRL_CMD_WAKE:
610 cfctrl->res.wake_rsp();
611 break;
612 case CFCTRL_CMD_LINK_RECONF:
613 cfctrl->res.restart_rsp();
614 break;
615 case CFCTRL_CMD_RADIO_SET:
616 cfctrl->res.radioset_rsp();
617 break;
618 default:
619 pr_err("CAIF: %s(): Unrecognized Control Frame\n", __func__);
620 goto error;
621 break;
622 }
623 ret = 0;
624error:
625 cfpkt_destroy(pkt);
626 return ret;
627}
628
629static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
630 int phyid)
631{
632 struct cfctrl *this = container_obj(layr);
633 switch (ctrl) {
634 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
635 case CAIF_CTRLCMD_FLOW_OFF_IND:
636 spin_lock(&this->info_list_lock);
637 if (this->first_req != NULL) {
638 pr_debug("CAIF: %s(): Received flow off in "
639 "control layer", __func__);
640 }
641 spin_unlock(&this->info_list_lock);
642 break;
643 default:
644 break;
645 }
646}
647
648#ifndef CAIF_NO_LOOP
649static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
650{
651 static int last_linkid;
652 u8 linkid, linktype, tmp;
653 switch (cmd) {
654 case CFCTRL_CMD_LINK_SETUP:
655 spin_lock(&ctrl->loop_linkid_lock);
656 for (linkid = last_linkid + 1; linkid < 255; linkid++)
657 if (!ctrl->loop_linkused[linkid])
658 goto found;
659 for (linkid = last_linkid - 1; linkid > 0; linkid--)
660 if (!ctrl->loop_linkused[linkid])
661 goto found;
662 spin_unlock(&ctrl->loop_linkid_lock);
663 pr_err("CAIF: %s(): Out of link-ids\n", __func__);
664 return -EINVAL;
665found:
666 if (!ctrl->loop_linkused[linkid])
667 ctrl->loop_linkused[linkid] = 1;
668
669 last_linkid = linkid;
670
671 cfpkt_add_trail(pkt, &linkid, 1);
672 spin_unlock(&ctrl->loop_linkid_lock);
673 cfpkt_peek_head(pkt, &linktype, 1);
674 if (linktype == CFCTRL_SRV_UTIL) {
675 tmp = 0x01;
676 cfpkt_add_trail(pkt, &tmp, 1);
677 cfpkt_add_trail(pkt, &tmp, 1);
678 }
679 break;
680
681 case CFCTRL_CMD_LINK_DESTROY:
682 spin_lock(&ctrl->loop_linkid_lock);
683 cfpkt_peek_head(pkt, &linkid, 1);
684 ctrl->loop_linkused[linkid] = 0;
685 spin_unlock(&ctrl->loop_linkid_lock);
686 break;
687 default:
688 break;
689 }
690 return CAIF_SUCCESS;
691}
692#endif
diff --git a/net/caif/cfdbgl.c b/net/caif/cfdbgl.c
new file mode 100644
index 000000000000..ab6b6dc34cf8
--- /dev/null
+++ b/net/caif/cfdbgl.c
@@ -0,0 +1,40 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/stddef.h>
8#include <linux/slab.h>
9#include <net/caif/caif_layer.h>
10#include <net/caif/cfsrvl.h>
11#include <net/caif/cfpkt.h>
12
13static int cfdbgl_receive(struct cflayer *layr, struct cfpkt *pkt);
14static int cfdbgl_transmit(struct cflayer *layr, struct cfpkt *pkt);
15
16struct cflayer *cfdbgl_create(u8 channel_id, struct dev_info *dev_info)
17{
18 struct cfsrvl *dbg = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
19 if (!dbg) {
20 pr_warning("CAIF: %s(): Out of memory\n", __func__);
21 return NULL;
22 }
23 caif_assert(offsetof(struct cfsrvl, layer) == 0);
24 memset(dbg, 0, sizeof(struct cfsrvl));
25 cfsrvl_init(dbg, channel_id, dev_info);
26 dbg->layer.receive = cfdbgl_receive;
27 dbg->layer.transmit = cfdbgl_transmit;
28 snprintf(dbg->layer.name, CAIF_LAYER_NAME_SZ - 1, "dbg%d", channel_id);
29 return &dbg->layer;
30}
31
32static int cfdbgl_receive(struct cflayer *layr, struct cfpkt *pkt)
33{
34 return layr->up->receive(layr->up, pkt);
35}
36
37static int cfdbgl_transmit(struct cflayer *layr, struct cfpkt *pkt)
38{
39 return layr->dn->transmit(layr->dn, pkt);
40}
diff --git a/net/caif/cfdgml.c b/net/caif/cfdgml.c
new file mode 100644
index 000000000000..53194840ecb6
--- /dev/null
+++ b/net/caif/cfdgml.c
@@ -0,0 +1,108 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/stddef.h>
8#include <linux/spinlock.h>
9#include <linux/slab.h>
10#include <net/caif/caif_layer.h>
11#include <net/caif/cfsrvl.h>
12#include <net/caif/cfpkt.h>
13
14#define container_obj(layr) ((struct cfsrvl *) layr)
15
16#define DGM_CMD_BIT 0x80
17#define DGM_FLOW_OFF 0x81
18#define DGM_FLOW_ON 0x80
19#define DGM_CTRL_PKT_SIZE 1
20
21static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt);
22static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt);
23
24struct cflayer *cfdgml_create(u8 channel_id, struct dev_info *dev_info)
25{
26 struct cfsrvl *dgm = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
27 if (!dgm) {
28 pr_warning("CAIF: %s(): Out of memory\n", __func__);
29 return NULL;
30 }
31 caif_assert(offsetof(struct cfsrvl, layer) == 0);
32 memset(dgm, 0, sizeof(struct cfsrvl));
33 cfsrvl_init(dgm, channel_id, dev_info);
34 dgm->layer.receive = cfdgml_receive;
35 dgm->layer.transmit = cfdgml_transmit;
36 snprintf(dgm->layer.name, CAIF_LAYER_NAME_SZ - 1, "dgm%d", channel_id);
37 dgm->layer.name[CAIF_LAYER_NAME_SZ - 1] = '\0';
38 return &dgm->layer;
39}
40
41static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt)
42{
43 u8 cmd = -1;
44 u8 dgmhdr[3];
45 int ret;
46 caif_assert(layr->up != NULL);
47 caif_assert(layr->receive != NULL);
48 caif_assert(layr->ctrlcmd != NULL);
49
50 if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
51 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
52 cfpkt_destroy(pkt);
53 return -EPROTO;
54 }
55
56 if ((cmd & DGM_CMD_BIT) == 0) {
57 if (cfpkt_extr_head(pkt, &dgmhdr, 3) < 0) {
58 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
59 cfpkt_destroy(pkt);
60 return -EPROTO;
61 }
62 ret = layr->up->receive(layr->up, pkt);
63 return ret;
64 }
65
66 switch (cmd) {
67 case DGM_FLOW_OFF: /* FLOW OFF */
68 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
69 cfpkt_destroy(pkt);
70 return 0;
71 case DGM_FLOW_ON: /* FLOW ON */
72 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
73 cfpkt_destroy(pkt);
74 return 0;
75 default:
76 cfpkt_destroy(pkt);
77 pr_info("CAIF: %s(): Unknown datagram control %d (0x%x)\n",
78 __func__, cmd, cmd);
79 return -EPROTO;
80 }
81}
82
83static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt)
84{
85 u32 zero = 0;
86 struct caif_payload_info *info;
87 struct cfsrvl *service = container_obj(layr);
88 int ret;
89 if (!cfsrvl_ready(service, &ret))
90 return ret;
91
92 cfpkt_add_head(pkt, &zero, 4);
93
94 /* Add info for MUX-layer to route the packet out. */
95 info = cfpkt_info(pkt);
96 info->channel_id = service->layer.id;
97 /* To optimize alignment, we add up the size of CAIF header
98 * before payload.
99 */
100 info->hdr_len = 4;
101 info->dev_info = &service->dev_info;
102 ret = layr->dn->transmit(layr->dn, pkt);
103 if (ret < 0) {
104 u32 tmp32;
105 cfpkt_extr_head(pkt, &tmp32, 4);
106 }
107 return ret;
108}
diff --git a/net/caif/cffrml.c b/net/caif/cffrml.c
new file mode 100644
index 000000000000..e86a4ca3b217
--- /dev/null
+++ b/net/caif/cffrml.c
@@ -0,0 +1,151 @@
1/*
2 * CAIF Framing Layer.
3 *
4 * Copyright (C) ST-Ericsson AB 2010
5 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
6 * License terms: GNU General Public License (GPL) version 2
7 */
8
9#include <linux/stddef.h>
10#include <linux/spinlock.h>
11#include <linux/slab.h>
12#include <linux/crc-ccitt.h>
13#include <net/caif/caif_layer.h>
14#include <net/caif/cfpkt.h>
15#include <net/caif/cffrml.h>
16
17#define container_obj(layr) container_of(layr, struct cffrml, layer)
18
19struct cffrml {
20 struct cflayer layer;
21 bool dofcs; /* !< FCS active */
22};
23
24static int cffrml_receive(struct cflayer *layr, struct cfpkt *pkt);
25static int cffrml_transmit(struct cflayer *layr, struct cfpkt *pkt);
26static void cffrml_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
27 int phyid);
28
29static u32 cffrml_rcv_error;
30static u32 cffrml_rcv_checsum_error;
31struct cflayer *cffrml_create(u16 phyid, bool use_fcs)
32{
33 struct cffrml *this = kmalloc(sizeof(struct cffrml), GFP_ATOMIC);
34 if (!this) {
35 pr_warning("CAIF: %s(): Out of memory\n", __func__);
36 return NULL;
37 }
38 caif_assert(offsetof(struct cffrml, layer) == 0);
39
40 memset(this, 0, sizeof(struct cflayer));
41 this->layer.receive = cffrml_receive;
42 this->layer.transmit = cffrml_transmit;
43 this->layer.ctrlcmd = cffrml_ctrlcmd;
44 snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "frm%d", phyid);
45 this->dofcs = use_fcs;
46 this->layer.id = phyid;
47 return (struct cflayer *) this;
48}
49
50void cffrml_set_uplayer(struct cflayer *this, struct cflayer *up)
51{
52 this->up = up;
53}
54
55void cffrml_set_dnlayer(struct cflayer *this, struct cflayer *dn)
56{
57 this->dn = dn;
58}
59
60static u16 cffrml_checksum(u16 chks, void *buf, u16 len)
61{
62 /* FIXME: FCS should be moved to glue in order to use OS-Specific
63 * solutions
64 */
65 return crc_ccitt(chks, buf, len);
66}
67
68static int cffrml_receive(struct cflayer *layr, struct cfpkt *pkt)
69{
70 u16 tmp;
71 u16 len;
72 u16 hdrchks;
73 u16 pktchks;
74 struct cffrml *this;
75 this = container_obj(layr);
76
77 cfpkt_extr_head(pkt, &tmp, 2);
78 len = le16_to_cpu(tmp);
79
80 /* Subtract for FCS on length if FCS is not used. */
81 if (!this->dofcs)
82 len -= 2;
83
84 if (cfpkt_setlen(pkt, len) < 0) {
85 ++cffrml_rcv_error;
86 pr_err("CAIF: %s():Framing length error (%d)\n", __func__, len);
87 cfpkt_destroy(pkt);
88 return -EPROTO;
89 }
90 /*
91 * Don't do extract if FCS is false, rather do setlen - then we don't
92 * get a cache-miss.
93 */
94 if (this->dofcs) {
95 cfpkt_extr_trail(pkt, &tmp, 2);
96 hdrchks = le16_to_cpu(tmp);
97 pktchks = cfpkt_iterate(pkt, cffrml_checksum, 0xffff);
98 if (pktchks != hdrchks) {
99 cfpkt_add_trail(pkt, &tmp, 2);
100 ++cffrml_rcv_error;
101 ++cffrml_rcv_checsum_error;
102 pr_info("CAIF: %s(): Frame checksum error "
103 "(0x%x != 0x%x)\n", __func__, hdrchks, pktchks);
104 return -EILSEQ;
105 }
106 }
107 if (cfpkt_erroneous(pkt)) {
108 ++cffrml_rcv_error;
109 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
110 cfpkt_destroy(pkt);
111 return -EPROTO;
112 }
113 return layr->up->receive(layr->up, pkt);
114}
115
116static int cffrml_transmit(struct cflayer *layr, struct cfpkt *pkt)
117{
118 int tmp;
119 u16 chks;
120 u16 len;
121 int ret;
122 struct cffrml *this = container_obj(layr);
123 if (this->dofcs) {
124 chks = cfpkt_iterate(pkt, cffrml_checksum, 0xffff);
125 tmp = cpu_to_le16(chks);
126 cfpkt_add_trail(pkt, &tmp, 2);
127 } else {
128 cfpkt_pad_trail(pkt, 2);
129 }
130 len = cfpkt_getlen(pkt);
131 tmp = cpu_to_le16(len);
132 cfpkt_add_head(pkt, &tmp, 2);
133 cfpkt_info(pkt)->hdr_len += 2;
134 if (cfpkt_erroneous(pkt)) {
135 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
136 return -EPROTO;
137 }
138 ret = layr->dn->transmit(layr->dn, pkt);
139 if (ret < 0) {
140 /* Remove header on faulty packet. */
141 cfpkt_extr_head(pkt, &tmp, 2);
142 }
143 return ret;
144}
145
146static void cffrml_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
147 int phyid)
148{
149 if (layr->up->ctrlcmd)
150 layr->up->ctrlcmd(layr->up, ctrl, layr->id);
151}
diff --git a/net/caif/cfmuxl.c b/net/caif/cfmuxl.c
new file mode 100644
index 000000000000..7372f27f1d32
--- /dev/null
+++ b/net/caif/cfmuxl.c
@@ -0,0 +1,251 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6#include <linux/stddef.h>
7#include <linux/spinlock.h>
8#include <linux/slab.h>
9#include <net/caif/cfpkt.h>
10#include <net/caif/cfmuxl.h>
11#include <net/caif/cfsrvl.h>
12#include <net/caif/cffrml.h>
13
14#define container_obj(layr) container_of(layr, struct cfmuxl, layer)
15
16#define CAIF_CTRL_CHANNEL 0
17#define UP_CACHE_SIZE 8
18#define DN_CACHE_SIZE 8
19
20struct cfmuxl {
21 struct cflayer layer;
22 struct list_head srvl_list;
23 struct list_head frml_list;
24 struct cflayer *up_cache[UP_CACHE_SIZE];
25 struct cflayer *dn_cache[DN_CACHE_SIZE];
26 /*
27 * Set when inserting or removing downwards layers.
28 */
29 spinlock_t transmit_lock;
30
31 /*
32 * Set when inserting or removing upwards layers.
33 */
34 spinlock_t receive_lock;
35
36};
37
38static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt);
39static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt);
40static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
41 int phyid);
42static struct cflayer *get_up(struct cfmuxl *muxl, u16 id);
43
44struct cflayer *cfmuxl_create(void)
45{
46 struct cfmuxl *this = kmalloc(sizeof(struct cfmuxl), GFP_ATOMIC);
47 if (!this)
48 return NULL;
49 memset(this, 0, sizeof(*this));
50 this->layer.receive = cfmuxl_receive;
51 this->layer.transmit = cfmuxl_transmit;
52 this->layer.ctrlcmd = cfmuxl_ctrlcmd;
53 INIT_LIST_HEAD(&this->srvl_list);
54 INIT_LIST_HEAD(&this->frml_list);
55 spin_lock_init(&this->transmit_lock);
56 spin_lock_init(&this->receive_lock);
57 snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "mux");
58 return &this->layer;
59}
60
61int cfmuxl_set_uplayer(struct cflayer *layr, struct cflayer *up, u8 linkid)
62{
63 struct cfmuxl *muxl = container_obj(layr);
64 spin_lock(&muxl->receive_lock);
65 cfsrvl_get(up);
66 list_add(&up->node, &muxl->srvl_list);
67 spin_unlock(&muxl->receive_lock);
68 return 0;
69}
70
71bool cfmuxl_is_phy_inuse(struct cflayer *layr, u8 phyid)
72{
73 struct list_head *node;
74 struct cflayer *layer;
75 struct cfmuxl *muxl = container_obj(layr);
76 bool match = false;
77 spin_lock(&muxl->receive_lock);
78
79 list_for_each(node, &muxl->srvl_list) {
80 layer = list_entry(node, struct cflayer, node);
81 if (cfsrvl_phyid_match(layer, phyid)) {
82 match = true;
83 break;
84 }
85
86 }
87 spin_unlock(&muxl->receive_lock);
88 return match;
89}
90
91u8 cfmuxl_get_phyid(struct cflayer *layr, u8 channel_id)
92{
93 struct cflayer *up;
94 int phyid;
95 struct cfmuxl *muxl = container_obj(layr);
96 spin_lock(&muxl->receive_lock);
97 up = get_up(muxl, channel_id);
98 if (up != NULL)
99 phyid = cfsrvl_getphyid(up);
100 else
101 phyid = 0;
102 spin_unlock(&muxl->receive_lock);
103 return phyid;
104}
105
106int cfmuxl_set_dnlayer(struct cflayer *layr, struct cflayer *dn, u8 phyid)
107{
108 struct cfmuxl *muxl = (struct cfmuxl *) layr;
109 spin_lock(&muxl->transmit_lock);
110 list_add(&dn->node, &muxl->frml_list);
111 spin_unlock(&muxl->transmit_lock);
112 return 0;
113}
114
115static struct cflayer *get_from_id(struct list_head *list, u16 id)
116{
117 struct list_head *node;
118 struct cflayer *layer;
119 list_for_each(node, list) {
120 layer = list_entry(node, struct cflayer, node);
121 if (layer->id == id)
122 return layer;
123 }
124 return NULL;
125}
126
127struct cflayer *cfmuxl_remove_dnlayer(struct cflayer *layr, u8 phyid)
128{
129 struct cfmuxl *muxl = container_obj(layr);
130 struct cflayer *dn;
131 spin_lock(&muxl->transmit_lock);
132 memset(muxl->dn_cache, 0, sizeof(muxl->dn_cache));
133 dn = get_from_id(&muxl->frml_list, phyid);
134 if (dn == NULL) {
135 spin_unlock(&muxl->transmit_lock);
136 return NULL;
137 }
138 list_del(&dn->node);
139 caif_assert(dn != NULL);
140 spin_unlock(&muxl->transmit_lock);
141 return dn;
142}
143
144/* Invariant: lock is taken */
145static struct cflayer *get_up(struct cfmuxl *muxl, u16 id)
146{
147 struct cflayer *up;
148 int idx = id % UP_CACHE_SIZE;
149 up = muxl->up_cache[idx];
150 if (up == NULL || up->id != id) {
151 up = get_from_id(&muxl->srvl_list, id);
152 muxl->up_cache[idx] = up;
153 }
154 return up;
155}
156
157/* Invariant: lock is taken */
158static struct cflayer *get_dn(struct cfmuxl *muxl, struct dev_info *dev_info)
159{
160 struct cflayer *dn;
161 int idx = dev_info->id % DN_CACHE_SIZE;
162 dn = muxl->dn_cache[idx];
163 if (dn == NULL || dn->id != dev_info->id) {
164 dn = get_from_id(&muxl->frml_list, dev_info->id);
165 muxl->dn_cache[idx] = dn;
166 }
167 return dn;
168}
169
170struct cflayer *cfmuxl_remove_uplayer(struct cflayer *layr, u8 id)
171{
172 struct cflayer *up;
173 struct cfmuxl *muxl = container_obj(layr);
174 spin_lock(&muxl->receive_lock);
175 up = get_up(muxl, id);
176 if (up == NULL)
177 return NULL;
178 memset(muxl->up_cache, 0, sizeof(muxl->up_cache));
179 list_del(&up->node);
180 cfsrvl_put(up);
181 spin_unlock(&muxl->receive_lock);
182 return up;
183}
184
185static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt)
186{
187 int ret;
188 struct cfmuxl *muxl = container_obj(layr);
189 u8 id;
190 struct cflayer *up;
191 if (cfpkt_extr_head(pkt, &id, 1) < 0) {
192 pr_err("CAIF: %s(): erroneous Caif Packet\n", __func__);
193 cfpkt_destroy(pkt);
194 return -EPROTO;
195 }
196
197 spin_lock(&muxl->receive_lock);
198 up = get_up(muxl, id);
199 spin_unlock(&muxl->receive_lock);
200 if (up == NULL) {
201 pr_info("CAIF: %s():Received data on unknown link ID = %d "
202 "(0x%x) up == NULL", __func__, id, id);
203 cfpkt_destroy(pkt);
204 /*
205 * Don't return ERROR, since modem misbehaves and sends out
206 * flow on before linksetup response.
207 */
208 return /* CFGLU_EPROT; */ 0;
209 }
210 cfsrvl_get(up);
211 ret = up->receive(up, pkt);
212 cfsrvl_put(up);
213 return ret;
214}
215
216static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt)
217{
218 int ret;
219 struct cfmuxl *muxl = container_obj(layr);
220 u8 linkid;
221 struct cflayer *dn;
222 struct caif_payload_info *info = cfpkt_info(pkt);
223 dn = get_dn(muxl, cfpkt_info(pkt)->dev_info);
224 if (dn == NULL) {
225 pr_warning("CAIF: %s(): Send data on unknown phy "
226 "ID = %d (0x%x)\n",
227 __func__, info->dev_info->id, info->dev_info->id);
228 return -ENOTCONN;
229 }
230 info->hdr_len += 1;
231 linkid = info->channel_id;
232 cfpkt_add_head(pkt, &linkid, 1);
233 ret = dn->transmit(dn, pkt);
234 /* Remove MUX protocol header upon error. */
235 if (ret < 0)
236 cfpkt_extr_head(pkt, &linkid, 1);
237 return ret;
238}
239
240static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
241 int phyid)
242{
243 struct cfmuxl *muxl = container_obj(layr);
244 struct list_head *node;
245 struct cflayer *layer;
246 list_for_each(node, &muxl->srvl_list) {
247 layer = list_entry(node, struct cflayer, node);
248 if (cfsrvl_phyid_match(layer, phyid))
249 layer->ctrlcmd(layer, ctrl, phyid);
250 }
251}
diff --git a/net/caif/cfpkt_skbuff.c b/net/caif/cfpkt_skbuff.c
new file mode 100644
index 000000000000..83fff2ff6658
--- /dev/null
+++ b/net/caif/cfpkt_skbuff.c
@@ -0,0 +1,571 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/string.h>
8#include <linux/skbuff.h>
9#include <linux/hardirq.h>
10#include <net/caif/cfpkt.h>
11
12#define PKT_PREFIX CAIF_NEEDED_HEADROOM
13#define PKT_POSTFIX CAIF_NEEDED_TAILROOM
14#define PKT_LEN_WHEN_EXTENDING 128
15#define PKT_ERROR(pkt, errmsg) do { \
16 cfpkt_priv(pkt)->erronous = true; \
17 skb_reset_tail_pointer(&pkt->skb); \
18 pr_warning("CAIF: " errmsg);\
19 } while (0)
20
21struct cfpktq {
22 struct sk_buff_head head;
23 atomic_t count;
24 /* Lock protects count updates */
25 spinlock_t lock;
26};
27
28/*
29 * net/caif/ is generic and does not
30 * understand SKB, so we do this typecast
31 */
32struct cfpkt {
33 struct sk_buff skb;
34};
35
36/* Private data inside SKB */
37struct cfpkt_priv_data {
38 struct dev_info dev_info;
39 bool erronous;
40};
41
42inline struct cfpkt_priv_data *cfpkt_priv(struct cfpkt *pkt)
43{
44 return (struct cfpkt_priv_data *) pkt->skb.cb;
45}
46
47inline bool is_erronous(struct cfpkt *pkt)
48{
49 return cfpkt_priv(pkt)->erronous;
50}
51
52inline struct sk_buff *pkt_to_skb(struct cfpkt *pkt)
53{
54 return &pkt->skb;
55}
56
57inline struct cfpkt *skb_to_pkt(struct sk_buff *skb)
58{
59 return (struct cfpkt *) skb;
60}
61
62
63struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt)
64{
65 struct cfpkt *pkt = skb_to_pkt(nativepkt);
66 cfpkt_priv(pkt)->erronous = false;
67 return pkt;
68}
69EXPORT_SYMBOL(cfpkt_fromnative);
70
71void *cfpkt_tonative(struct cfpkt *pkt)
72{
73 return (void *) pkt;
74}
75EXPORT_SYMBOL(cfpkt_tonative);
76
77static struct cfpkt *cfpkt_create_pfx(u16 len, u16 pfx)
78{
79 struct sk_buff *skb;
80
81 if (likely(in_interrupt()))
82 skb = alloc_skb(len + pfx, GFP_ATOMIC);
83 else
84 skb = alloc_skb(len + pfx, GFP_KERNEL);
85
86 if (unlikely(skb == NULL))
87 return NULL;
88
89 skb_reserve(skb, pfx);
90 return skb_to_pkt(skb);
91}
92
93inline struct cfpkt *cfpkt_create(u16 len)
94{
95 return cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX);
96}
97EXPORT_SYMBOL(cfpkt_create);
98
99void cfpkt_destroy(struct cfpkt *pkt)
100{
101 struct sk_buff *skb = pkt_to_skb(pkt);
102 kfree_skb(skb);
103}
104EXPORT_SYMBOL(cfpkt_destroy);
105
106inline bool cfpkt_more(struct cfpkt *pkt)
107{
108 struct sk_buff *skb = pkt_to_skb(pkt);
109 return skb->len > 0;
110}
111EXPORT_SYMBOL(cfpkt_more);
112
113int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len)
114{
115 struct sk_buff *skb = pkt_to_skb(pkt);
116 if (skb_headlen(skb) >= len) {
117 memcpy(data, skb->data, len);
118 return 0;
119 }
120 return !cfpkt_extr_head(pkt, data, len) &&
121 !cfpkt_add_head(pkt, data, len);
122}
123EXPORT_SYMBOL(cfpkt_peek_head);
124
125int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len)
126{
127 struct sk_buff *skb = pkt_to_skb(pkt);
128 u8 *from;
129 if (unlikely(is_erronous(pkt)))
130 return -EPROTO;
131
132 if (unlikely(len > skb->len)) {
133 PKT_ERROR(pkt, "cfpkt_extr_head read beyond end of packet\n");
134 return -EPROTO;
135 }
136
137 if (unlikely(len > skb_headlen(skb))) {
138 if (unlikely(skb_linearize(skb) != 0)) {
139 PKT_ERROR(pkt, "cfpkt_extr_head linearize failed\n");
140 return -EPROTO;
141 }
142 }
143 from = skb_pull(skb, len);
144 from -= len;
145 memcpy(data, from, len);
146 return 0;
147}
148EXPORT_SYMBOL(cfpkt_extr_head);
149
150int cfpkt_extr_trail(struct cfpkt *pkt, void *dta, u16 len)
151{
152 struct sk_buff *skb = pkt_to_skb(pkt);
153 u8 *data = dta;
154 u8 *from;
155 if (unlikely(is_erronous(pkt)))
156 return -EPROTO;
157
158 if (unlikely(skb_linearize(skb) != 0)) {
159 PKT_ERROR(pkt, "cfpkt_extr_trail linearize failed\n");
160 return -EPROTO;
161 }
162 if (unlikely(skb->data + len > skb_tail_pointer(skb))) {
163 PKT_ERROR(pkt, "cfpkt_extr_trail read beyond end of packet\n");
164 return -EPROTO;
165 }
166 from = skb_tail_pointer(skb) - len;
167 skb_trim(skb, skb->len - len);
168 memcpy(data, from, len);
169 return 0;
170}
171EXPORT_SYMBOL(cfpkt_extr_trail);
172
173int cfpkt_pad_trail(struct cfpkt *pkt, u16 len)
174{
175 return cfpkt_add_body(pkt, NULL, len);
176}
177EXPORT_SYMBOL(cfpkt_pad_trail);
178
179int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len)
180{
181 struct sk_buff *skb = pkt_to_skb(pkt);
182 struct sk_buff *lastskb;
183 u8 *to;
184 u16 addlen = 0;
185
186
187 if (unlikely(is_erronous(pkt)))
188 return -EPROTO;
189
190 lastskb = skb;
191
192 /* Check whether we need to add space at the tail */
193 if (unlikely(skb_tailroom(skb) < len)) {
194 if (likely(len < PKT_LEN_WHEN_EXTENDING))
195 addlen = PKT_LEN_WHEN_EXTENDING;
196 else
197 addlen = len;
198 }
199
200 /* Check whether we need to change the SKB before writing to the tail */
201 if (unlikely((addlen > 0) || skb_cloned(skb) || skb_shared(skb))) {
202
203 /* Make sure data is writable */
204 if (unlikely(skb_cow_data(skb, addlen, &lastskb) < 0)) {
205 PKT_ERROR(pkt, "cfpkt_add_body: cow failed\n");
206 return -EPROTO;
207 }
208 /*
209 * Is the SKB non-linear after skb_cow_data()? If so, we are
210 * going to add data to the last SKB, so we need to adjust
211 * lengths of the top SKB.
212 */
213 if (lastskb != skb) {
214 pr_warning("CAIF: %s(): Packet is non-linear\n",
215 __func__);
216 skb->len += len;
217 skb->data_len += len;
218 }
219 }
220
221 /* All set to put the last SKB and optionally write data there. */
222 to = skb_put(lastskb, len);
223 if (likely(data))
224 memcpy(to, data, len);
225 return 0;
226}
227EXPORT_SYMBOL(cfpkt_add_body);
228
229inline int cfpkt_addbdy(struct cfpkt *pkt, u8 data)
230{
231 return cfpkt_add_body(pkt, &data, 1);
232}
233EXPORT_SYMBOL(cfpkt_addbdy);
234
235int cfpkt_add_head(struct cfpkt *pkt, const void *data2, u16 len)
236{
237 struct sk_buff *skb = pkt_to_skb(pkt);
238 struct sk_buff *lastskb;
239 u8 *to;
240 const u8 *data = data2;
241 if (unlikely(is_erronous(pkt)))
242 return -EPROTO;
243 if (unlikely(skb_headroom(skb) < len)) {
244 PKT_ERROR(pkt, "cfpkt_add_head: no headroom\n");
245 return -EPROTO;
246 }
247
248 /* Make sure data is writable */
249 if (unlikely(skb_cow_data(skb, 0, &lastskb) < 0)) {
250 PKT_ERROR(pkt, "cfpkt_add_head: cow failed\n");
251 return -EPROTO;
252 }
253
254 to = skb_push(skb, len);
255 memcpy(to, data, len);
256 return 0;
257}
258EXPORT_SYMBOL(cfpkt_add_head);
259
260inline int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len)
261{
262 return cfpkt_add_body(pkt, data, len);
263}
264EXPORT_SYMBOL(cfpkt_add_trail);
265
266inline u16 cfpkt_getlen(struct cfpkt *pkt)
267{
268 struct sk_buff *skb = pkt_to_skb(pkt);
269 return skb->len;
270}
271EXPORT_SYMBOL(cfpkt_getlen);
272
273inline u16 cfpkt_iterate(struct cfpkt *pkt,
274 u16 (*iter_func)(u16, void *, u16),
275 u16 data)
276{
277 /*
278 * Don't care about the performance hit of linearizing,
279 * Checksum should not be used on high-speed interfaces anyway.
280 */
281 if (unlikely(is_erronous(pkt)))
282 return -EPROTO;
283 if (unlikely(skb_linearize(&pkt->skb) != 0)) {
284 PKT_ERROR(pkt, "cfpkt_iterate: linearize failed\n");
285 return -EPROTO;
286 }
287 return iter_func(data, pkt->skb.data, cfpkt_getlen(pkt));
288}
289EXPORT_SYMBOL(cfpkt_iterate);
290
291int cfpkt_setlen(struct cfpkt *pkt, u16 len)
292{
293 struct sk_buff *skb = pkt_to_skb(pkt);
294
295
296 if (unlikely(is_erronous(pkt)))
297 return -EPROTO;
298
299 if (likely(len <= skb->len)) {
300 if (unlikely(skb->data_len))
301 ___pskb_trim(skb, len);
302 else
303 skb_trim(skb, len);
304
305 return cfpkt_getlen(pkt);
306 }
307
308 /* Need to expand SKB */
309 if (unlikely(!cfpkt_pad_trail(pkt, len - skb->len)))
310 PKT_ERROR(pkt, "cfpkt_setlen: skb_pad_trail failed\n");
311
312 return cfpkt_getlen(pkt);
313}
314EXPORT_SYMBOL(cfpkt_setlen);
315
316struct cfpkt *cfpkt_create_uplink(const unsigned char *data, unsigned int len)
317{
318 struct cfpkt *pkt = cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX);
319 if (unlikely(data != NULL))
320 cfpkt_add_body(pkt, data, len);
321 return pkt;
322}
323EXPORT_SYMBOL(cfpkt_create_uplink);
324
325struct cfpkt *cfpkt_append(struct cfpkt *dstpkt,
326 struct cfpkt *addpkt,
327 u16 expectlen)
328{
329 struct sk_buff *dst = pkt_to_skb(dstpkt);
330 struct sk_buff *add = pkt_to_skb(addpkt);
331 u16 addlen = skb_headlen(add);
332 u16 neededtailspace;
333 struct sk_buff *tmp;
334 u16 dstlen;
335 u16 createlen;
336 if (unlikely(is_erronous(dstpkt) || is_erronous(addpkt))) {
337 cfpkt_destroy(addpkt);
338 return dstpkt;
339 }
340 if (expectlen > addlen)
341 neededtailspace = expectlen;
342 else
343 neededtailspace = addlen;
344
345 if (dst->tail + neededtailspace > dst->end) {
346 /* Create a dumplicate of 'dst' with more tail space */
347 dstlen = skb_headlen(dst);
348 createlen = dstlen + neededtailspace;
349 tmp = pkt_to_skb(
350 cfpkt_create(createlen + PKT_PREFIX + PKT_POSTFIX));
351 if (!tmp)
352 return NULL;
353 skb_set_tail_pointer(tmp, dstlen);
354 tmp->len = dstlen;
355 memcpy(tmp->data, dst->data, dstlen);
356 cfpkt_destroy(dstpkt);
357 dst = tmp;
358 }
359 memcpy(skb_tail_pointer(dst), add->data, skb_headlen(add));
360 cfpkt_destroy(addpkt);
361 dst->tail += addlen;
362 dst->len += addlen;
363 return skb_to_pkt(dst);
364}
365EXPORT_SYMBOL(cfpkt_append);
366
367struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos)
368{
369 struct sk_buff *skb2;
370 struct sk_buff *skb = pkt_to_skb(pkt);
371 u8 *split = skb->data + pos;
372 u16 len2nd = skb_tail_pointer(skb) - split;
373
374 if (unlikely(is_erronous(pkt)))
375 return NULL;
376
377 if (skb->data + pos > skb_tail_pointer(skb)) {
378 PKT_ERROR(pkt,
379 "cfpkt_split: trying to split beyond end of packet");
380 return NULL;
381 }
382
383 /* Create a new packet for the second part of the data */
384 skb2 = pkt_to_skb(
385 cfpkt_create_pfx(len2nd + PKT_PREFIX + PKT_POSTFIX,
386 PKT_PREFIX));
387
388 if (skb2 == NULL)
389 return NULL;
390
391 /* Reduce the length of the original packet */
392 skb_set_tail_pointer(skb, pos);
393 skb->len = pos;
394
395 memcpy(skb2->data, split, len2nd);
396 skb2->tail += len2nd;
397 skb2->len += len2nd;
398 return skb_to_pkt(skb2);
399}
400EXPORT_SYMBOL(cfpkt_split);
401
402char *cfpkt_log_pkt(struct cfpkt *pkt, char *buf, int buflen)
403{
404 struct sk_buff *skb = pkt_to_skb(pkt);
405 char *p = buf;
406 int i;
407
408 /*
409 * Sanity check buffer length, it needs to be at least as large as
410 * the header info: ~=50+ bytes
411 */
412 if (buflen < 50)
413 return NULL;
414
415 snprintf(buf, buflen, "%s: pkt:%p len:%ld(%ld+%ld) {%ld,%ld} data: [",
416 is_erronous(pkt) ? "ERRONOUS-SKB" :
417 (skb->data_len != 0 ? "COMPLEX-SKB" : "SKB"),
418 skb,
419 (long) skb->len,
420 (long) (skb_tail_pointer(skb) - skb->data),
421 (long) skb->data_len,
422 (long) (skb->data - skb->head),
423 (long) (skb_tail_pointer(skb) - skb->head));
424 p = buf + strlen(buf);
425
426 for (i = 0; i < skb_tail_pointer(skb) - skb->data && i < 300; i++) {
427 if (p > buf + buflen - 10) {
428 sprintf(p, "...");
429 p = buf + strlen(buf);
430 break;
431 }
432 sprintf(p, "%02x,", skb->data[i]);
433 p = buf + strlen(buf);
434 }
435 sprintf(p, "]\n");
436 return buf;
437}
438EXPORT_SYMBOL(cfpkt_log_pkt);
439
440int cfpkt_raw_append(struct cfpkt *pkt, void **buf, unsigned int buflen)
441{
442 struct sk_buff *skb = pkt_to_skb(pkt);
443 struct sk_buff *lastskb;
444
445 caif_assert(buf != NULL);
446 if (unlikely(is_erronous(pkt)))
447 return -EPROTO;
448 /* Make sure SKB is writable */
449 if (unlikely(skb_cow_data(skb, 0, &lastskb) < 0)) {
450 PKT_ERROR(pkt, "cfpkt_raw_append: skb_cow_data failed\n");
451 return -EPROTO;
452 }
453
454 if (unlikely(skb_linearize(skb) != 0)) {
455 PKT_ERROR(pkt, "cfpkt_raw_append: linearize failed\n");
456 return -EPROTO;
457 }
458
459 if (unlikely(skb_tailroom(skb) < buflen)) {
460 PKT_ERROR(pkt, "cfpkt_raw_append: buffer too short - failed\n");
461 return -EPROTO;
462 }
463
464 *buf = skb_put(skb, buflen);
465 return 1;
466}
467EXPORT_SYMBOL(cfpkt_raw_append);
468
469int cfpkt_raw_extract(struct cfpkt *pkt, void **buf, unsigned int buflen)
470{
471 struct sk_buff *skb = pkt_to_skb(pkt);
472
473 caif_assert(buf != NULL);
474 if (unlikely(is_erronous(pkt)))
475 return -EPROTO;
476
477 if (unlikely(buflen > skb->len)) {
478 PKT_ERROR(pkt, "cfpkt_raw_extract: buflen too large "
479 "- failed\n");
480 return -EPROTO;
481 }
482
483 if (unlikely(buflen > skb_headlen(skb))) {
484 if (unlikely(skb_linearize(skb) != 0)) {
485 PKT_ERROR(pkt, "cfpkt_raw_extract: linearize failed\n");
486 return -EPROTO;
487 }
488 }
489
490 *buf = skb->data;
491 skb_pull(skb, buflen);
492
493 return 1;
494}
495EXPORT_SYMBOL(cfpkt_raw_extract);
496
497inline bool cfpkt_erroneous(struct cfpkt *pkt)
498{
499 return cfpkt_priv(pkt)->erronous;
500}
501EXPORT_SYMBOL(cfpkt_erroneous);
502
503struct cfpktq *cfpktq_create(void)
504{
505 struct cfpktq *q = kmalloc(sizeof(struct cfpktq), GFP_ATOMIC);
506 if (!q)
507 return NULL;
508 skb_queue_head_init(&q->head);
509 atomic_set(&q->count, 0);
510 spin_lock_init(&q->lock);
511 return q;
512}
513EXPORT_SYMBOL(cfpktq_create);
514
515void cfpkt_queue(struct cfpktq *pktq, struct cfpkt *pkt, unsigned short prio)
516{
517 atomic_inc(&pktq->count);
518 spin_lock(&pktq->lock);
519 skb_queue_tail(&pktq->head, pkt_to_skb(pkt));
520 spin_unlock(&pktq->lock);
521
522}
523EXPORT_SYMBOL(cfpkt_queue);
524
525struct cfpkt *cfpkt_qpeek(struct cfpktq *pktq)
526{
527 struct cfpkt *tmp;
528 spin_lock(&pktq->lock);
529 tmp = skb_to_pkt(skb_peek(&pktq->head));
530 spin_unlock(&pktq->lock);
531 return tmp;
532}
533EXPORT_SYMBOL(cfpkt_qpeek);
534
535struct cfpkt *cfpkt_dequeue(struct cfpktq *pktq)
536{
537 struct cfpkt *pkt;
538 spin_lock(&pktq->lock);
539 pkt = skb_to_pkt(skb_dequeue(&pktq->head));
540 if (pkt) {
541 atomic_dec(&pktq->count);
542 caif_assert(atomic_read(&pktq->count) >= 0);
543 }
544 spin_unlock(&pktq->lock);
545 return pkt;
546}
547EXPORT_SYMBOL(cfpkt_dequeue);
548
549int cfpkt_qcount(struct cfpktq *pktq)
550{
551 return atomic_read(&pktq->count);
552}
553EXPORT_SYMBOL(cfpkt_qcount);
554
555struct cfpkt *cfpkt_clone_release(struct cfpkt *pkt)
556{
557 struct cfpkt *clone;
558 clone = skb_to_pkt(skb_clone(pkt_to_skb(pkt), GFP_ATOMIC));
559 /* Free original packet. */
560 cfpkt_destroy(pkt);
561 if (!clone)
562 return NULL;
563 return clone;
564}
565EXPORT_SYMBOL(cfpkt_clone_release);
566
567struct caif_payload_info *cfpkt_info(struct cfpkt *pkt)
568{
569 return (struct caif_payload_info *)&pkt_to_skb(pkt)->cb;
570}
571EXPORT_SYMBOL(cfpkt_info);
diff --git a/net/caif/cfrfml.c b/net/caif/cfrfml.c
new file mode 100644
index 000000000000..cd2830fec935
--- /dev/null
+++ b/net/caif/cfrfml.c
@@ -0,0 +1,108 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/stddef.h>
8#include <linux/spinlock.h>
9#include <linux/slab.h>
10#include <net/caif/caif_layer.h>
11#include <net/caif/cfsrvl.h>
12#include <net/caif/cfpkt.h>
13
14#define container_obj(layr) container_of(layr, struct cfsrvl, layer)
15
16#define RFM_SEGMENTATION_BIT 0x01
17#define RFM_PAYLOAD 0x00
18#define RFM_CMD_BIT 0x80
19#define RFM_FLOW_OFF 0x81
20#define RFM_FLOW_ON 0x80
21#define RFM_SET_PIN 0x82
22#define RFM_CTRL_PKT_SIZE 1
23
24static int cfrfml_receive(struct cflayer *layr, struct cfpkt *pkt);
25static int cfrfml_transmit(struct cflayer *layr, struct cfpkt *pkt);
26static int cfservl_modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl);
27
28struct cflayer *cfrfml_create(u8 channel_id, struct dev_info *dev_info)
29{
30 struct cfsrvl *rfm = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
31 if (!rfm) {
32 pr_warning("CAIF: %s(): Out of memory\n", __func__);
33 return NULL;
34 }
35 caif_assert(offsetof(struct cfsrvl, layer) == 0);
36 memset(rfm, 0, sizeof(struct cfsrvl));
37 cfsrvl_init(rfm, channel_id, dev_info);
38 rfm->layer.modemcmd = cfservl_modemcmd;
39 rfm->layer.receive = cfrfml_receive;
40 rfm->layer.transmit = cfrfml_transmit;
41 snprintf(rfm->layer.name, CAIF_LAYER_NAME_SZ, "rfm%d", channel_id);
42 return &rfm->layer;
43}
44
45static int cfservl_modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
46{
47 return -EPROTO;
48}
49
50static int cfrfml_receive(struct cflayer *layr, struct cfpkt *pkt)
51{
52 u8 tmp;
53 bool segmented;
54 int ret;
55 caif_assert(layr->up != NULL);
56 caif_assert(layr->receive != NULL);
57
58 /*
59 * RFM is taking care of segmentation and stripping of
60 * segmentation bit.
61 */
62 if (cfpkt_extr_head(pkt, &tmp, 1) < 0) {
63 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
64 cfpkt_destroy(pkt);
65 return -EPROTO;
66 }
67 segmented = tmp & RFM_SEGMENTATION_BIT;
68 caif_assert(!segmented);
69
70 ret = layr->up->receive(layr->up, pkt);
71 return ret;
72}
73
74static int cfrfml_transmit(struct cflayer *layr, struct cfpkt *pkt)
75{
76 u8 tmp = 0;
77 int ret;
78 struct cfsrvl *service = container_obj(layr);
79
80 caif_assert(layr->dn != NULL);
81 caif_assert(layr->dn->transmit != NULL);
82
83 if (!cfsrvl_ready(service, &ret))
84 return ret;
85
86 if (!cfpkt_getlen(pkt) > CAIF_MAX_PAYLOAD_SIZE) {
87 pr_err("CAIF: %s():Packet too large - size=%d\n",
88 __func__, cfpkt_getlen(pkt));
89 return -EOVERFLOW;
90 }
91 if (cfpkt_add_head(pkt, &tmp, 1) < 0) {
92 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
93 return -EPROTO;
94 }
95
96 /* Add info for MUX-layer to route the packet out. */
97 cfpkt_info(pkt)->channel_id = service->layer.id;
98 /*
99 * To optimize alignment, we add up the size of CAIF header before
100 * payload.
101 */
102 cfpkt_info(pkt)->hdr_len = 1;
103 cfpkt_info(pkt)->dev_info = &service->dev_info;
104 ret = layr->dn->transmit(layr->dn, pkt);
105 if (ret < 0)
106 cfpkt_extr_head(pkt, &tmp, 1);
107 return ret;
108}
diff --git a/net/caif/cfserl.c b/net/caif/cfserl.c
new file mode 100644
index 000000000000..06029ea2da2f
--- /dev/null
+++ b/net/caif/cfserl.c
@@ -0,0 +1,192 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/stddef.h>
8#include <linux/spinlock.h>
9#include <linux/slab.h>
10#include <net/caif/caif_layer.h>
11#include <net/caif/cfpkt.h>
12#include <net/caif/cfserl.h>
13
14#define container_obj(layr) ((struct cfserl *) layr)
15
16#define CFSERL_STX 0x02
17#define CAIF_MINIUM_PACKET_SIZE 4
18struct cfserl {
19 struct cflayer layer;
20 struct cfpkt *incomplete_frm;
21 /* Protects parallel processing of incoming packets */
22 spinlock_t sync;
23 bool usestx;
24};
25#define STXLEN(layr) (layr->usestx ? 1 : 0)
26
27static int cfserl_receive(struct cflayer *layr, struct cfpkt *pkt);
28static int cfserl_transmit(struct cflayer *layr, struct cfpkt *pkt);
29static void cfserl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
30 int phyid);
31
32struct cflayer *cfserl_create(int type, int instance, bool use_stx)
33{
34 struct cfserl *this = kmalloc(sizeof(struct cfserl), GFP_ATOMIC);
35 if (!this) {
36 pr_warning("CAIF: %s(): Out of memory\n", __func__);
37 return NULL;
38 }
39 caif_assert(offsetof(struct cfserl, layer) == 0);
40 memset(this, 0, sizeof(struct cfserl));
41 this->layer.receive = cfserl_receive;
42 this->layer.transmit = cfserl_transmit;
43 this->layer.ctrlcmd = cfserl_ctrlcmd;
44 this->layer.type = type;
45 this->usestx = use_stx;
46 spin_lock_init(&this->sync);
47 snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "ser1");
48 return &this->layer;
49}
50
51static int cfserl_receive(struct cflayer *l, struct cfpkt *newpkt)
52{
53 struct cfserl *layr = container_obj(l);
54 u16 pkt_len;
55 struct cfpkt *pkt = NULL;
56 struct cfpkt *tail_pkt = NULL;
57 u8 tmp8;
58 u16 tmp;
59 u8 stx = CFSERL_STX;
60 int ret;
61 u16 expectlen = 0;
62 caif_assert(newpkt != NULL);
63 spin_lock(&layr->sync);
64
65 if (layr->incomplete_frm != NULL) {
66
67 layr->incomplete_frm =
68 cfpkt_append(layr->incomplete_frm, newpkt, expectlen);
69 pkt = layr->incomplete_frm;
70 } else {
71 pkt = newpkt;
72 }
73 layr->incomplete_frm = NULL;
74
75 do {
76 /* Search for STX at start of pkt if STX is used */
77 if (layr->usestx) {
78 cfpkt_extr_head(pkt, &tmp8, 1);
79 if (tmp8 != CFSERL_STX) {
80 while (cfpkt_more(pkt)
81 && tmp8 != CFSERL_STX) {
82 cfpkt_extr_head(pkt, &tmp8, 1);
83 }
84 if (!cfpkt_more(pkt)) {
85 cfpkt_destroy(pkt);
86 layr->incomplete_frm = NULL;
87 spin_unlock(&layr->sync);
88 return -EPROTO;
89 }
90 }
91 }
92
93 pkt_len = cfpkt_getlen(pkt);
94
95 /*
96 * pkt_len is the accumulated length of the packet data
97 * we have received so far.
98 * Exit if frame doesn't hold length.
99 */
100
101 if (pkt_len < 2) {
102 if (layr->usestx)
103 cfpkt_add_head(pkt, &stx, 1);
104 layr->incomplete_frm = pkt;
105 spin_unlock(&layr->sync);
106 return 0;
107 }
108
109 /*
110 * Find length of frame.
111 * expectlen is the length we need for a full frame.
112 */
113 cfpkt_peek_head(pkt, &tmp, 2);
114 expectlen = le16_to_cpu(tmp) + 2;
115 /*
116 * Frame error handling
117 */
118 if (expectlen < CAIF_MINIUM_PACKET_SIZE
119 || expectlen > CAIF_MAX_FRAMESIZE) {
120 if (!layr->usestx) {
121 if (pkt != NULL)
122 cfpkt_destroy(pkt);
123 layr->incomplete_frm = NULL;
124 expectlen = 0;
125 spin_unlock(&layr->sync);
126 return -EPROTO;
127 }
128 continue;
129 }
130
131 if (pkt_len < expectlen) {
132 /* Too little received data */
133 if (layr->usestx)
134 cfpkt_add_head(pkt, &stx, 1);
135 layr->incomplete_frm = pkt;
136 spin_unlock(&layr->sync);
137 return 0;
138 }
139
140 /*
141 * Enough data for at least one frame.
142 * Split the frame, if too long
143 */
144 if (pkt_len > expectlen)
145 tail_pkt = cfpkt_split(pkt, expectlen);
146 else
147 tail_pkt = NULL;
148
149 /* Send the first part of packet upwards.*/
150 spin_unlock(&layr->sync);
151 ret = layr->layer.up->receive(layr->layer.up, pkt);
152 spin_lock(&layr->sync);
153 if (ret == -EILSEQ) {
154 if (layr->usestx) {
155 if (tail_pkt != NULL)
156 pkt = cfpkt_append(pkt, tail_pkt, 0);
157
158 /* Start search for next STX if frame failed */
159 continue;
160 } else {
161 cfpkt_destroy(pkt);
162 pkt = NULL;
163 }
164 }
165
166 pkt = tail_pkt;
167
168 } while (pkt != NULL);
169
170 spin_unlock(&layr->sync);
171 return 0;
172}
173
174static int cfserl_transmit(struct cflayer *layer, struct cfpkt *newpkt)
175{
176 struct cfserl *layr = container_obj(layer);
177 int ret;
178 u8 tmp8 = CFSERL_STX;
179 if (layr->usestx)
180 cfpkt_add_head(newpkt, &tmp8, 1);
181 ret = layer->dn->transmit(layer->dn, newpkt);
182 if (ret < 0)
183 cfpkt_extr_head(newpkt, &tmp8, 1);
184
185 return ret;
186}
187
188static void cfserl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
189 int phyid)
190{
191 layr->up->ctrlcmd(layr->up, ctrl, phyid);
192}
diff --git a/net/caif/cfsrvl.c b/net/caif/cfsrvl.c
new file mode 100644
index 000000000000..aff31f34528f
--- /dev/null
+++ b/net/caif/cfsrvl.c
@@ -0,0 +1,192 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/errno.h>
10#include <linux/slab.h>
11#include <net/caif/caif_layer.h>
12#include <net/caif/cfsrvl.h>
13#include <net/caif/cfpkt.h>
14
15#define SRVL_CTRL_PKT_SIZE 1
16#define SRVL_FLOW_OFF 0x81
17#define SRVL_FLOW_ON 0x80
18#define SRVL_SET_PIN 0x82
19#define SRVL_CTRL_PKT_SIZE 1
20
21#define container_obj(layr) container_of(layr, struct cfsrvl, layer)
22
23static void cfservl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
24 int phyid)
25{
26 struct cfsrvl *service = container_obj(layr);
27 caif_assert(layr->up != NULL);
28 caif_assert(layr->up->ctrlcmd != NULL);
29 switch (ctrl) {
30 case CAIF_CTRLCMD_INIT_RSP:
31 service->open = true;
32 layr->up->ctrlcmd(layr->up, ctrl, phyid);
33 break;
34 case CAIF_CTRLCMD_DEINIT_RSP:
35 case CAIF_CTRLCMD_INIT_FAIL_RSP:
36 service->open = false;
37 layr->up->ctrlcmd(layr->up, ctrl, phyid);
38 break;
39 case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
40 if (phyid != service->dev_info.id)
41 break;
42 if (service->modem_flow_on)
43 layr->up->ctrlcmd(layr->up,
44 CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
45 service->phy_flow_on = false;
46 break;
47 case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND:
48 if (phyid != service->dev_info.id)
49 return;
50 if (service->modem_flow_on) {
51 layr->up->ctrlcmd(layr->up,
52 CAIF_CTRLCMD_FLOW_ON_IND,
53 phyid);
54 }
55 service->phy_flow_on = true;
56 break;
57 case CAIF_CTRLCMD_FLOW_OFF_IND:
58 if (service->phy_flow_on) {
59 layr->up->ctrlcmd(layr->up,
60 CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
61 }
62 service->modem_flow_on = false;
63 break;
64 case CAIF_CTRLCMD_FLOW_ON_IND:
65 if (service->phy_flow_on) {
66 layr->up->ctrlcmd(layr->up,
67 CAIF_CTRLCMD_FLOW_ON_IND, phyid);
68 }
69 service->modem_flow_on = true;
70 break;
71 case _CAIF_CTRLCMD_PHYIF_DOWN_IND:
72 /* In case interface is down, let's fake a remove shutdown */
73 layr->up->ctrlcmd(layr->up,
74 CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, phyid);
75 break;
76 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
77 layr->up->ctrlcmd(layr->up, ctrl, phyid);
78 break;
79 default:
80 pr_warning("CAIF: %s(): "
81 "Unexpected ctrl in cfsrvl (%d)\n", __func__, ctrl);
82 /* We have both modem and phy flow on, send flow on */
83 layr->up->ctrlcmd(layr->up, ctrl, phyid);
84 service->phy_flow_on = true;
85 break;
86 }
87}
88
89static int cfservl_modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
90{
91 struct cfsrvl *service = container_obj(layr);
92 caif_assert(layr != NULL);
93 caif_assert(layr->dn != NULL);
94 caif_assert(layr->dn->transmit != NULL);
95 switch (ctrl) {
96 case CAIF_MODEMCMD_FLOW_ON_REQ:
97 {
98 struct cfpkt *pkt;
99 struct caif_payload_info *info;
100 u8 flow_on = SRVL_FLOW_ON;
101 pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
102 if (!pkt) {
103 pr_warning("CAIF: %s(): Out of memory\n",
104 __func__);
105 return -ENOMEM;
106 }
107
108 if (cfpkt_add_head(pkt, &flow_on, 1) < 0) {
109 pr_err("CAIF: %s(): Packet is erroneous!\n",
110 __func__);
111 cfpkt_destroy(pkt);
112 return -EPROTO;
113 }
114 info = cfpkt_info(pkt);
115 info->channel_id = service->layer.id;
116 info->hdr_len = 1;
117 info->dev_info = &service->dev_info;
118 return layr->dn->transmit(layr->dn, pkt);
119 }
120 case CAIF_MODEMCMD_FLOW_OFF_REQ:
121 {
122 struct cfpkt *pkt;
123 struct caif_payload_info *info;
124 u8 flow_off = SRVL_FLOW_OFF;
125 pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
126 if (cfpkt_add_head(pkt, &flow_off, 1) < 0) {
127 pr_err("CAIF: %s(): Packet is erroneous!\n",
128 __func__);
129 cfpkt_destroy(pkt);
130 return -EPROTO;
131 }
132 info = cfpkt_info(pkt);
133 info->channel_id = service->layer.id;
134 info->hdr_len = 1;
135 info->dev_info = &service->dev_info;
136 return layr->dn->transmit(layr->dn, pkt);
137 }
138 default:
139 break;
140 }
141 return -EINVAL;
142}
143
144void cfservl_destroy(struct cflayer *layer)
145{
146 kfree(layer);
147}
148
149void cfsrvl_init(struct cfsrvl *service,
150 u8 channel_id,
151 struct dev_info *dev_info)
152{
153 caif_assert(offsetof(struct cfsrvl, layer) == 0);
154 service->open = false;
155 service->modem_flow_on = true;
156 service->phy_flow_on = true;
157 service->layer.id = channel_id;
158 service->layer.ctrlcmd = cfservl_ctrlcmd;
159 service->layer.modemcmd = cfservl_modemcmd;
160 service->dev_info = *dev_info;
161 kref_init(&service->ref);
162}
163
164void cfsrvl_release(struct kref *kref)
165{
166 struct cfsrvl *service = container_of(kref, struct cfsrvl, ref);
167 kfree(service);
168}
169
170bool cfsrvl_ready(struct cfsrvl *service, int *err)
171{
172 if (service->open && service->modem_flow_on && service->phy_flow_on)
173 return true;
174 if (!service->open) {
175 *err = -ENOTCONN;
176 return false;
177 }
178 caif_assert(!(service->modem_flow_on && service->phy_flow_on));
179 *err = -EAGAIN;
180 return false;
181}
182u8 cfsrvl_getphyid(struct cflayer *layer)
183{
184 struct cfsrvl *servl = container_obj(layer);
185 return servl->dev_info.id;
186}
187
188bool cfsrvl_phyid_match(struct cflayer *layer, int phyid)
189{
190 struct cfsrvl *servl = container_obj(layer);
191 return servl->dev_info.id == phyid;
192}
diff --git a/net/caif/cfutill.c b/net/caif/cfutill.c
new file mode 100644
index 000000000000..5fd2c9ea8b42
--- /dev/null
+++ b/net/caif/cfutill.c
@@ -0,0 +1,115 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/slab.h>
10#include <linux/errno.h>
11#include <net/caif/caif_layer.h>
12#include <net/caif/cfsrvl.h>
13#include <net/caif/cfpkt.h>
14
15#define container_obj(layr) ((struct cfsrvl *) layr)
16#define UTIL_PAYLOAD 0x00
17#define UTIL_CMD_BIT 0x80
18#define UTIL_REMOTE_SHUTDOWN 0x82
19#define UTIL_FLOW_OFF 0x81
20#define UTIL_FLOW_ON 0x80
21#define UTIL_CTRL_PKT_SIZE 1
22static int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt);
23static int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt);
24
25struct cflayer *cfutill_create(u8 channel_id, struct dev_info *dev_info)
26{
27 struct cfsrvl *util = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
28 if (!util) {
29 pr_warning("CAIF: %s(): Out of memory\n", __func__);
30 return NULL;
31 }
32 caif_assert(offsetof(struct cfsrvl, layer) == 0);
33 memset(util, 0, sizeof(struct cfsrvl));
34 cfsrvl_init(util, channel_id, dev_info);
35 util->layer.receive = cfutill_receive;
36 util->layer.transmit = cfutill_transmit;
37 snprintf(util->layer.name, CAIF_LAYER_NAME_SZ - 1, "util1");
38 return &util->layer;
39}
40
41static int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt)
42{
43 u8 cmd = -1;
44 struct cfsrvl *service = container_obj(layr);
45 caif_assert(layr != NULL);
46 caif_assert(layr->up != NULL);
47 caif_assert(layr->up->receive != NULL);
48 caif_assert(layr->up->ctrlcmd != NULL);
49 if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
50 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
51 cfpkt_destroy(pkt);
52 return -EPROTO;
53 }
54
55 switch (cmd) {
56 case UTIL_PAYLOAD:
57 return layr->up->receive(layr->up, pkt);
58 case UTIL_FLOW_OFF:
59 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
60 cfpkt_destroy(pkt);
61 return 0;
62 case UTIL_FLOW_ON:
63 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
64 cfpkt_destroy(pkt);
65 return 0;
66 case UTIL_REMOTE_SHUTDOWN: /* Remote Shutdown Request */
67 pr_err("CAIF: %s(): REMOTE SHUTDOWN REQUEST RECEIVED\n",
68 __func__);
69 layr->ctrlcmd(layr, CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, 0);
70 service->open = false;
71 cfpkt_destroy(pkt);
72 return 0;
73 default:
74 cfpkt_destroy(pkt);
75 pr_warning("CAIF: %s(): Unknown service control %d (0x%x)\n",
76 __func__, cmd, cmd);
77 return -EPROTO;
78 }
79}
80
81static int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt)
82{
83 u8 zero = 0;
84 struct caif_payload_info *info;
85 int ret;
86 struct cfsrvl *service = container_obj(layr);
87 caif_assert(layr != NULL);
88 caif_assert(layr->dn != NULL);
89 caif_assert(layr->dn->transmit != NULL);
90 if (!cfsrvl_ready(service, &ret))
91 return ret;
92
93 if (cfpkt_getlen(pkt) > CAIF_MAX_PAYLOAD_SIZE) {
94 pr_err("CAIF: %s(): packet too large size=%d\n",
95 __func__, cfpkt_getlen(pkt));
96 return -EOVERFLOW;
97 }
98
99 cfpkt_add_head(pkt, &zero, 1);
100 /* Add info for MUX-layer to route the packet out. */
101 info = cfpkt_info(pkt);
102 info->channel_id = service->layer.id;
103 /*
104 * To optimize alignment, we add up the size of CAIF header before
105 * payload.
106 */
107 info->hdr_len = 1;
108 info->dev_info = &service->dev_info;
109 ret = layr->dn->transmit(layr->dn, pkt);
110 if (ret < 0) {
111 u32 tmp32;
112 cfpkt_extr_head(pkt, &tmp32, 4);
113 }
114 return ret;
115}
diff --git a/net/caif/cfveil.c b/net/caif/cfveil.c
new file mode 100644
index 000000000000..0fd827f49491
--- /dev/null
+++ b/net/caif/cfveil.c
@@ -0,0 +1,107 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/stddef.h>
8#include <linux/slab.h>
9#include <net/caif/caif_layer.h>
10#include <net/caif/cfsrvl.h>
11#include <net/caif/cfpkt.h>
12
13#define VEI_PAYLOAD 0x00
14#define VEI_CMD_BIT 0x80
15#define VEI_FLOW_OFF 0x81
16#define VEI_FLOW_ON 0x80
17#define VEI_SET_PIN 0x82
18#define VEI_CTRL_PKT_SIZE 1
19#define container_obj(layr) container_of(layr, struct cfsrvl, layer)
20
21static int cfvei_receive(struct cflayer *layr, struct cfpkt *pkt);
22static int cfvei_transmit(struct cflayer *layr, struct cfpkt *pkt);
23
24struct cflayer *cfvei_create(u8 channel_id, struct dev_info *dev_info)
25{
26 struct cfsrvl *vei = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
27 if (!vei) {
28 pr_warning("CAIF: %s(): Out of memory\n", __func__);
29 return NULL;
30 }
31 caif_assert(offsetof(struct cfsrvl, layer) == 0);
32 memset(vei, 0, sizeof(struct cfsrvl));
33 cfsrvl_init(vei, channel_id, dev_info);
34 vei->layer.receive = cfvei_receive;
35 vei->layer.transmit = cfvei_transmit;
36 snprintf(vei->layer.name, CAIF_LAYER_NAME_SZ - 1, "vei%d", channel_id);
37 return &vei->layer;
38}
39
40static int cfvei_receive(struct cflayer *layr, struct cfpkt *pkt)
41{
42 u8 cmd;
43 int ret;
44 caif_assert(layr->up != NULL);
45 caif_assert(layr->receive != NULL);
46 caif_assert(layr->ctrlcmd != NULL);
47
48
49 if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
50 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
51 cfpkt_destroy(pkt);
52 return -EPROTO;
53 }
54 switch (cmd) {
55 case VEI_PAYLOAD:
56 ret = layr->up->receive(layr->up, pkt);
57 return ret;
58 case VEI_FLOW_OFF:
59 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
60 cfpkt_destroy(pkt);
61 return 0;
62 case VEI_FLOW_ON:
63 layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
64 cfpkt_destroy(pkt);
65 return 0;
66 case VEI_SET_PIN: /* SET RS232 PIN */
67 cfpkt_destroy(pkt);
68 return 0;
69 default: /* SET RS232 PIN */
70 pr_warning("CAIF: %s():Unknown VEI control packet %d (0x%x)!\n",
71 __func__, cmd, cmd);
72 cfpkt_destroy(pkt);
73 return -EPROTO;
74 }
75}
76
77static int cfvei_transmit(struct cflayer *layr, struct cfpkt *pkt)
78{
79 u8 tmp = 0;
80 struct caif_payload_info *info;
81 int ret;
82 struct cfsrvl *service = container_obj(layr);
83 if (!cfsrvl_ready(service, &ret))
84 return ret;
85 caif_assert(layr->dn != NULL);
86 caif_assert(layr->dn->transmit != NULL);
87 if (!cfpkt_getlen(pkt) > CAIF_MAX_PAYLOAD_SIZE) {
88 pr_warning("CAIF: %s(): Packet too large - size=%d\n",
89 __func__, cfpkt_getlen(pkt));
90 return -EOVERFLOW;
91 }
92
93 if (cfpkt_add_head(pkt, &tmp, 1) < 0) {
94 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
95 return -EPROTO;
96 }
97
98 /* Add info-> for MUX-layer to route the packet out. */
99 info = cfpkt_info(pkt);
100 info->channel_id = service->layer.id;
101 info->hdr_len = 1;
102 info->dev_info = &service->dev_info;
103 ret = layr->dn->transmit(layr->dn, pkt);
104 if (ret < 0)
105 cfpkt_extr_head(pkt, &tmp, 1);
106 return ret;
107}
diff --git a/net/caif/cfvidl.c b/net/caif/cfvidl.c
new file mode 100644
index 000000000000..89ad4ea239f1
--- /dev/null
+++ b/net/caif/cfvidl.c
@@ -0,0 +1,65 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
5 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/slab.h>
10#include <linux/errno.h>
11#include <net/caif/caif_layer.h>
12#include <net/caif/cfsrvl.h>
13#include <net/caif/cfpkt.h>
14
15#define container_obj(layr) ((struct cfsrvl *) layr)
16
17static int cfvidl_receive(struct cflayer *layr, struct cfpkt *pkt);
18static int cfvidl_transmit(struct cflayer *layr, struct cfpkt *pkt);
19
20struct cflayer *cfvidl_create(u8 channel_id, struct dev_info *dev_info)
21{
22 struct cfsrvl *vid = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
23 if (!vid) {
24 pr_warning("CAIF: %s(): Out of memory\n", __func__);
25 return NULL;
26 }
27 caif_assert(offsetof(struct cfsrvl, layer) == 0);
28
29 memset(vid, 0, sizeof(struct cfsrvl));
30 cfsrvl_init(vid, channel_id, dev_info);
31 vid->layer.receive = cfvidl_receive;
32 vid->layer.transmit = cfvidl_transmit;
33 snprintf(vid->layer.name, CAIF_LAYER_NAME_SZ - 1, "vid1");
34 return &vid->layer;
35}
36
37static int cfvidl_receive(struct cflayer *layr, struct cfpkt *pkt)
38{
39 u32 videoheader;
40 if (cfpkt_extr_head(pkt, &videoheader, 4) < 0) {
41 pr_err("CAIF: %s(): Packet is erroneous!\n", __func__);
42 cfpkt_destroy(pkt);
43 return -EPROTO;
44 }
45 return layr->up->receive(layr->up, pkt);
46}
47
48static int cfvidl_transmit(struct cflayer *layr, struct cfpkt *pkt)
49{
50 struct cfsrvl *service = container_obj(layr);
51 struct caif_payload_info *info;
52 u32 videoheader = 0;
53 int ret;
54 if (!cfsrvl_ready(service, &ret))
55 return ret;
56 cfpkt_add_head(pkt, &videoheader, 4);
57 /* Add info for MUX-layer to route the packet out */
58 info = cfpkt_info(pkt);
59 info->channel_id = service->layer.id;
60 info->dev_info = &service->dev_info;
61 ret = layr->dn->transmit(layr->dn, pkt);
62 if (ret < 0)
63 cfpkt_extr_head(pkt, &videoheader, 4);
64 return ret;
65}
diff --git a/net/caif/chnl_net.c b/net/caif/chnl_net.c
new file mode 100644
index 000000000000..610966abe2dc
--- /dev/null
+++ b/net/caif/chnl_net.c
@@ -0,0 +1,467 @@
1/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Authors: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * Daniel Martensson / Daniel.Martensson@stericsson.com
5 * License terms: GNU General Public License (GPL) version 2
6 */
7
8#include <linux/version.h>
9#include <linux/fs.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/netdevice.h>
13#include <linux/if_ether.h>
14#include <linux/moduleparam.h>
15#include <linux/ip.h>
16#include <linux/sched.h>
17#include <linux/sockios.h>
18#include <linux/caif/if_caif.h>
19#include <net/rtnetlink.h>
20#include <net/caif/caif_layer.h>
21#include <net/caif/cfcnfg.h>
22#include <net/caif/cfpkt.h>
23#include <net/caif/caif_dev.h>
24
25/* GPRS PDP connection has MTU to 1500 */
26#define SIZE_MTU 1500
27/* 5 sec. connect timeout */
28#define CONNECT_TIMEOUT (5 * HZ)
29#define CAIF_NET_DEFAULT_QUEUE_LEN 500
30
31#undef pr_debug
32#define pr_debug pr_warning
33
34/*This list is protected by the rtnl lock. */
35static LIST_HEAD(chnl_net_list);
36
37MODULE_LICENSE("GPL");
38MODULE_ALIAS_RTNL_LINK("caif");
39
40enum caif_states {
41 CAIF_CONNECTED = 1,
42 CAIF_CONNECTING,
43 CAIF_DISCONNECTED,
44 CAIF_SHUTDOWN
45};
46
47struct chnl_net {
48 struct cflayer chnl;
49 struct net_device_stats stats;
50 struct caif_connect_request conn_req;
51 struct list_head list_field;
52 struct net_device *netdev;
53 char name[256];
54 wait_queue_head_t netmgmt_wq;
55 /* Flow status to remember and control the transmission. */
56 bool flowenabled;
57 enum caif_states state;
58};
59
60static void robust_list_del(struct list_head *delete_node)
61{
62 struct list_head *list_node;
63 struct list_head *n;
64 ASSERT_RTNL();
65 list_for_each_safe(list_node, n, &chnl_net_list) {
66 if (list_node == delete_node) {
67 list_del(list_node);
68 return;
69 }
70 }
71 WARN_ON(1);
72}
73
74static int chnl_recv_cb(struct cflayer *layr, struct cfpkt *pkt)
75{
76 struct sk_buff *skb;
77 struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
78 int pktlen;
79 int err = 0;
80
81 priv = container_of(layr, struct chnl_net, chnl);
82
83 if (!priv)
84 return -EINVAL;
85
86 /* Get length of CAIF packet. */
87 pktlen = cfpkt_getlen(pkt);
88
89 skb = (struct sk_buff *) cfpkt_tonative(pkt);
90 /* Pass some minimum information and
91 * send the packet to the net stack.
92 */
93 skb->dev = priv->netdev;
94 skb->protocol = htons(ETH_P_IP);
95
96 /* If we change the header in loop mode, the checksum is corrupted. */
97 if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
98 skb->ip_summed = CHECKSUM_UNNECESSARY;
99 else
100 skb->ip_summed = CHECKSUM_NONE;
101
102 if (in_interrupt())
103 netif_rx(skb);
104 else
105 netif_rx_ni(skb);
106
107 /* Update statistics. */
108 priv->netdev->stats.rx_packets++;
109 priv->netdev->stats.rx_bytes += pktlen;
110
111 return err;
112}
113
114static int delete_device(struct chnl_net *dev)
115{
116 ASSERT_RTNL();
117 if (dev->netdev)
118 unregister_netdevice(dev->netdev);
119 return 0;
120}
121
122static void close_work(struct work_struct *work)
123{
124 struct chnl_net *dev = NULL;
125 struct list_head *list_node;
126 struct list_head *_tmp;
127 /* May be called with or without RTNL lock held */
128 int islocked = rtnl_is_locked();
129 if (!islocked)
130 rtnl_lock();
131 list_for_each_safe(list_node, _tmp, &chnl_net_list) {
132 dev = list_entry(list_node, struct chnl_net, list_field);
133 if (dev->state == CAIF_SHUTDOWN)
134 dev_close(dev->netdev);
135 }
136 if (!islocked)
137 rtnl_unlock();
138}
139static DECLARE_WORK(close_worker, close_work);
140
141static void chnl_flowctrl_cb(struct cflayer *layr, enum caif_ctrlcmd flow,
142 int phyid)
143{
144 struct chnl_net *priv = container_of(layr, struct chnl_net, chnl);
145 pr_debug("CAIF: %s(): NET flowctrl func called flow: %s\n",
146 __func__,
147 flow == CAIF_CTRLCMD_FLOW_ON_IND ? "ON" :
148 flow == CAIF_CTRLCMD_INIT_RSP ? "INIT" :
149 flow == CAIF_CTRLCMD_FLOW_OFF_IND ? "OFF" :
150 flow == CAIF_CTRLCMD_DEINIT_RSP ? "CLOSE/DEINIT" :
151 flow == CAIF_CTRLCMD_INIT_FAIL_RSP ? "OPEN_FAIL" :
152 flow == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND ?
153 "REMOTE_SHUTDOWN" : "UKNOWN CTRL COMMAND");
154
155
156
157 switch (flow) {
158 case CAIF_CTRLCMD_FLOW_OFF_IND:
159 priv->flowenabled = false;
160 netif_stop_queue(priv->netdev);
161 break;
162 case CAIF_CTRLCMD_DEINIT_RSP:
163 priv->state = CAIF_DISCONNECTED;
164 break;
165 case CAIF_CTRLCMD_INIT_FAIL_RSP:
166 priv->state = CAIF_DISCONNECTED;
167 wake_up_interruptible(&priv->netmgmt_wq);
168 break;
169 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
170 priv->state = CAIF_SHUTDOWN;
171 netif_tx_disable(priv->netdev);
172 schedule_work(&close_worker);
173 break;
174 case CAIF_CTRLCMD_FLOW_ON_IND:
175 priv->flowenabled = true;
176 netif_wake_queue(priv->netdev);
177 break;
178 case CAIF_CTRLCMD_INIT_RSP:
179 priv->state = CAIF_CONNECTED;
180 priv->flowenabled = true;
181 netif_wake_queue(priv->netdev);
182 wake_up_interruptible(&priv->netmgmt_wq);
183 break;
184 default:
185 break;
186 }
187}
188
189static int chnl_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
190{
191 struct chnl_net *priv;
192 struct cfpkt *pkt = NULL;
193 int len;
194 int result = -1;
195 /* Get our private data. */
196 priv = netdev_priv(dev);
197
198 if (skb->len > priv->netdev->mtu) {
199 pr_warning("CAIF: %s(): Size of skb exceeded MTU\n", __func__);
200 return -ENOSPC;
201 }
202
203 if (!priv->flowenabled) {
204 pr_debug("CAIF: %s(): dropping packets flow off\n", __func__);
205 return NETDEV_TX_BUSY;
206 }
207
208 if (priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP)
209 swap(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
210
211 /* Store original SKB length. */
212 len = skb->len;
213
214 pkt = cfpkt_fromnative(CAIF_DIR_OUT, (void *) skb);
215
216 /* Send the packet down the stack. */
217 result = priv->chnl.dn->transmit(priv->chnl.dn, pkt);
218 if (result) {
219 if (result == -EAGAIN)
220 result = NETDEV_TX_BUSY;
221 return result;
222 }
223
224 /* Update statistics. */
225 dev->stats.tx_packets++;
226 dev->stats.tx_bytes += len;
227
228 return NETDEV_TX_OK;
229}
230
231static int chnl_net_open(struct net_device *dev)
232{
233 struct chnl_net *priv = NULL;
234 int result = -1;
235 ASSERT_RTNL();
236 priv = netdev_priv(dev);
237 if (!priv) {
238 pr_debug("CAIF: %s(): chnl_net_open: no priv\n", __func__);
239 return -ENODEV;
240 }
241
242 if (priv->state != CAIF_CONNECTING) {
243 priv->state = CAIF_CONNECTING;
244 result = caif_connect_client(&priv->conn_req, &priv->chnl);
245 if (result != 0) {
246 priv->state = CAIF_DISCONNECTED;
247 pr_debug("CAIF: %s(): err: "
248 "Unable to register and open device,"
249 " Err:%d\n",
250 __func__,
251 result);
252 return result;
253 }
254 }
255
256 result = wait_event_interruptible_timeout(priv->netmgmt_wq,
257 priv->state != CAIF_CONNECTING,
258 CONNECT_TIMEOUT);
259
260 if (result == -ERESTARTSYS) {
261 pr_debug("CAIF: %s(): wait_event_interruptible"
262 " woken by a signal\n", __func__);
263 return -ERESTARTSYS;
264 }
265 if (result == 0) {
266 pr_debug("CAIF: %s(): connect timeout\n", __func__);
267 caif_disconnect_client(&priv->chnl);
268 priv->state = CAIF_DISCONNECTED;
269 pr_debug("CAIF: %s(): state disconnected\n", __func__);
270 return -ETIMEDOUT;
271 }
272
273 if (priv->state != CAIF_CONNECTED) {
274 pr_debug("CAIF: %s(): connect failed\n", __func__);
275 return -ECONNREFUSED;
276 }
277 pr_debug("CAIF: %s(): CAIF Netdevice connected\n", __func__);
278 return 0;
279}
280
281static int chnl_net_stop(struct net_device *dev)
282{
283 struct chnl_net *priv;
284
285 ASSERT_RTNL();
286 priv = netdev_priv(dev);
287 priv->state = CAIF_DISCONNECTED;
288 caif_disconnect_client(&priv->chnl);
289 return 0;
290}
291
292static int chnl_net_init(struct net_device *dev)
293{
294 struct chnl_net *priv;
295 ASSERT_RTNL();
296 priv = netdev_priv(dev);
297 strncpy(priv->name, dev->name, sizeof(priv->name));
298 return 0;
299}
300
301static void chnl_net_uninit(struct net_device *dev)
302{
303 struct chnl_net *priv;
304 ASSERT_RTNL();
305 priv = netdev_priv(dev);
306 robust_list_del(&priv->list_field);
307}
308
309static const struct net_device_ops netdev_ops = {
310 .ndo_open = chnl_net_open,
311 .ndo_stop = chnl_net_stop,
312 .ndo_init = chnl_net_init,
313 .ndo_uninit = chnl_net_uninit,
314 .ndo_start_xmit = chnl_net_start_xmit,
315};
316
317static void ipcaif_net_setup(struct net_device *dev)
318{
319 struct chnl_net *priv;
320 dev->netdev_ops = &netdev_ops;
321 dev->destructor = free_netdev;
322 dev->flags |= IFF_NOARP;
323 dev->flags |= IFF_POINTOPOINT;
324 dev->needed_headroom = CAIF_NEEDED_HEADROOM;
325 dev->needed_tailroom = CAIF_NEEDED_TAILROOM;
326 dev->mtu = SIZE_MTU;
327 dev->tx_queue_len = CAIF_NET_DEFAULT_QUEUE_LEN;
328
329 priv = netdev_priv(dev);
330 priv->chnl.receive = chnl_recv_cb;
331 priv->chnl.ctrlcmd = chnl_flowctrl_cb;
332 priv->netdev = dev;
333 priv->conn_req.protocol = CAIFPROTO_DATAGRAM;
334 priv->conn_req.link_selector = CAIF_LINK_HIGH_BANDW;
335 priv->conn_req.priority = CAIF_PRIO_LOW;
336 /* Insert illegal value */
337 priv->conn_req.sockaddr.u.dgm.connection_id = -1;
338 priv->flowenabled = false;
339
340 ASSERT_RTNL();
341 init_waitqueue_head(&priv->netmgmt_wq);
342 list_add(&priv->list_field, &chnl_net_list);
343}
344
345
346static int ipcaif_fill_info(struct sk_buff *skb, const struct net_device *dev)
347{
348 struct chnl_net *priv;
349 u8 loop;
350 priv = netdev_priv(dev);
351 NLA_PUT_U32(skb, IFLA_CAIF_IPV4_CONNID,
352 priv->conn_req.sockaddr.u.dgm.connection_id);
353 NLA_PUT_U32(skb, IFLA_CAIF_IPV6_CONNID,
354 priv->conn_req.sockaddr.u.dgm.connection_id);
355 loop = priv->conn_req.protocol == CAIFPROTO_DATAGRAM_LOOP;
356 NLA_PUT_U8(skb, IFLA_CAIF_LOOPBACK, loop);
357
358
359 return 0;
360nla_put_failure:
361 return -EMSGSIZE;
362
363}
364
365static void caif_netlink_parms(struct nlattr *data[],
366 struct caif_connect_request *conn_req)
367{
368 if (!data) {
369 pr_warning("CAIF: %s: no params data found\n", __func__);
370 return;
371 }
372 if (data[IFLA_CAIF_IPV4_CONNID])
373 conn_req->sockaddr.u.dgm.connection_id =
374 nla_get_u32(data[IFLA_CAIF_IPV4_CONNID]);
375 if (data[IFLA_CAIF_IPV6_CONNID])
376 conn_req->sockaddr.u.dgm.connection_id =
377 nla_get_u32(data[IFLA_CAIF_IPV6_CONNID]);
378 if (data[IFLA_CAIF_LOOPBACK]) {
379 if (nla_get_u8(data[IFLA_CAIF_LOOPBACK]))
380 conn_req->protocol = CAIFPROTO_DATAGRAM_LOOP;
381 else
382 conn_req->protocol = CAIFPROTO_DATAGRAM;
383 }
384}
385
386static int ipcaif_newlink(struct net *src_net, struct net_device *dev,
387 struct nlattr *tb[], struct nlattr *data[])
388{
389 int ret;
390 struct chnl_net *caifdev;
391 ASSERT_RTNL();
392 caifdev = netdev_priv(dev);
393 caif_netlink_parms(data, &caifdev->conn_req);
394 dev_net_set(caifdev->netdev, src_net);
395
396 ret = register_netdevice(dev);
397 if (ret)
398 pr_warning("CAIF: %s(): device rtml registration failed\n",
399 __func__);
400 return ret;
401}
402
403static int ipcaif_changelink(struct net_device *dev, struct nlattr *tb[],
404 struct nlattr *data[])
405{
406 struct chnl_net *caifdev;
407 ASSERT_RTNL();
408 caifdev = netdev_priv(dev);
409 caif_netlink_parms(data, &caifdev->conn_req);
410 netdev_state_change(dev);
411 return 0;
412}
413
414static size_t ipcaif_get_size(const struct net_device *dev)
415{
416 return
417 /* IFLA_CAIF_IPV4_CONNID */
418 nla_total_size(4) +
419 /* IFLA_CAIF_IPV6_CONNID */
420 nla_total_size(4) +
421 /* IFLA_CAIF_LOOPBACK */
422 nla_total_size(2) +
423 0;
424}
425
426static const struct nla_policy ipcaif_policy[IFLA_CAIF_MAX + 1] = {
427 [IFLA_CAIF_IPV4_CONNID] = { .type = NLA_U32 },
428 [IFLA_CAIF_IPV6_CONNID] = { .type = NLA_U32 },
429 [IFLA_CAIF_LOOPBACK] = { .type = NLA_U8 }
430};
431
432
433static struct rtnl_link_ops ipcaif_link_ops __read_mostly = {
434 .kind = "caif",
435 .priv_size = sizeof(struct chnl_net),
436 .setup = ipcaif_net_setup,
437 .maxtype = IFLA_CAIF_MAX,
438 .policy = ipcaif_policy,
439 .newlink = ipcaif_newlink,
440 .changelink = ipcaif_changelink,
441 .get_size = ipcaif_get_size,
442 .fill_info = ipcaif_fill_info,
443
444};
445
446static int __init chnl_init_module(void)
447{
448 return rtnl_link_register(&ipcaif_link_ops);
449}
450
451static void __exit chnl_exit_module(void)
452{
453 struct chnl_net *dev = NULL;
454 struct list_head *list_node;
455 struct list_head *_tmp;
456 rtnl_link_unregister(&ipcaif_link_ops);
457 rtnl_lock();
458 list_for_each_safe(list_node, _tmp, &chnl_net_list) {
459 dev = list_entry(list_node, struct chnl_net, list_field);
460 list_del(list_node);
461 delete_device(dev);
462 }
463 rtnl_unlock();
464}
465
466module_init(chnl_init_module);
467module_exit(chnl_exit_module);