blob: a518fdd4da0ab5a297d07f33099e702a088a06c1 [file] [log] [blame]
Sjur Braendelandc72dfae2010-03-30 13:56:25 +00001/*
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
Joe Perchesb31fa5b2010-09-05 21:31:11 +000012#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
13
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000014#include <linux/version.h>
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/if_arp.h>
18#include <linux/net.h>
19#include <linux/netdevice.h>
20#include <linux/skbuff.h>
21#include <linux/sched.h>
22#include <linux/wait.h>
23#include <net/netns/generic.h>
24#include <net/net_namespace.h>
25#include <net/pkt_sched.h>
26#include <net/caif/caif_device.h>
27#include <net/caif/caif_dev.h>
28#include <net/caif/caif_layer.h>
29#include <net/caif/cfpkt.h>
30#include <net/caif/cfcnfg.h>
31
32MODULE_LICENSE("GPL");
33#define TIMEOUT (HZ*5)
34
35/* Used for local tracking of the CAIF net devices */
36struct caif_device_entry {
37 struct cflayer layer;
38 struct list_head list;
39 atomic_t in_use;
40 atomic_t state;
41 u16 phyid;
42 struct net_device *netdev;
43 wait_queue_head_t event;
44};
45
46struct caif_device_entry_list {
47 struct list_head list;
48 /* Protects simulanous deletes in list */
49 spinlock_t lock;
50};
51
52struct caif_net {
53 struct caif_device_entry_list caifdevs;
54};
55
56static int caif_net_id;
57static struct cfcnfg *cfg;
58
59static struct caif_device_entry_list *caif_device_list(struct net *net)
60{
61 struct caif_net *caifn;
62 BUG_ON(!net);
63 caifn = net_generic(net, caif_net_id);
64 BUG_ON(!caifn);
65 return &caifn->caifdevs;
66}
67
68/* Allocate new CAIF device. */
69static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
70{
71 struct caif_device_entry_list *caifdevs;
72 struct caif_device_entry *caifd;
73 caifdevs = caif_device_list(dev_net(dev));
74 BUG_ON(!caifdevs);
75 caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
76 if (!caifd)
77 return NULL;
78 caifd->netdev = dev;
79 list_add(&caifd->list, &caifdevs->list);
80 init_waitqueue_head(&caifd->event);
81 return caifd;
82}
83
84static struct caif_device_entry *caif_get(struct net_device *dev)
85{
86 struct caif_device_entry_list *caifdevs =
87 caif_device_list(dev_net(dev));
88 struct caif_device_entry *caifd;
89 BUG_ON(!caifdevs);
90 list_for_each_entry(caifd, &caifdevs->list, list) {
91 if (caifd->netdev == dev)
92 return caifd;
93 }
94 return NULL;
95}
96
97static void caif_device_destroy(struct net_device *dev)
98{
99 struct caif_device_entry_list *caifdevs =
100 caif_device_list(dev_net(dev));
101 struct caif_device_entry *caifd;
102 ASSERT_RTNL();
103 if (dev->type != ARPHRD_CAIF)
104 return;
105
106 spin_lock_bh(&caifdevs->lock);
107 caifd = caif_get(dev);
108 if (caifd == NULL) {
109 spin_unlock_bh(&caifdevs->lock);
110 return;
111 }
112
113 list_del(&caifd->list);
114 spin_unlock_bh(&caifdevs->lock);
115
116 kfree(caifd);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000117}
118
119static int transmit(struct cflayer *layer, struct cfpkt *pkt)
120{
121 struct caif_device_entry *caifd =
122 container_of(layer, struct caif_device_entry, layer);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000123 struct sk_buff *skb;
124
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000125 skb = cfpkt_tonative(pkt);
126 skb->dev = caifd->netdev;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000127
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000128 dev_queue_xmit(skb);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000129
130 return 0;
131}
132
133static int modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
134{
135 struct caif_device_entry *caifd;
136 struct caif_dev_common *caifdev;
137 caifd = container_of(layr, struct caif_device_entry, layer);
138 caifdev = netdev_priv(caifd->netdev);
139 if (ctrl == _CAIF_MODEMCMD_PHYIF_USEFULL) {
140 atomic_set(&caifd->in_use, 1);
141 wake_up_interruptible(&caifd->event);
142
143 } else if (ctrl == _CAIF_MODEMCMD_PHYIF_USELESS) {
144 atomic_set(&caifd->in_use, 0);
145 wake_up_interruptible(&caifd->event);
146 }
147 return 0;
148}
149
150/*
151 * Stuff received packets to associated sockets.
152 * On error, returns non-zero and releases the skb.
153 */
154static int receive(struct sk_buff *skb, struct net_device *dev,
155 struct packet_type *pkttype, struct net_device *orig_dev)
156{
157 struct net *net;
158 struct cfpkt *pkt;
159 struct caif_device_entry *caifd;
160 net = dev_net(dev);
161 pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
162 caifd = caif_get(dev);
Sjur Braendelande5e03ce2010-09-21 11:44:45 +0000163 if (!caifd || !caifd->layer.up || !caifd->layer.up->receive)
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000164 return NET_RX_DROP;
165
166 if (caifd->layer.up->receive(caifd->layer.up, pkt))
167 return NET_RX_DROP;
168
169 return 0;
170}
171
172static struct packet_type caif_packet_type __read_mostly = {
173 .type = cpu_to_be16(ETH_P_CAIF),
174 .func = receive,
175};
176
177static void dev_flowctrl(struct net_device *dev, int on)
178{
179 struct caif_device_entry *caifd = caif_get(dev);
180 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd)
181 return;
182
183 caifd->layer.up->ctrlcmd(caifd->layer.up,
184 on ?
185 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
186 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
187 caifd->layer.id);
188}
189
190/* notify Caif of device events */
191static int caif_device_notify(struct notifier_block *me, unsigned long what,
192 void *arg)
193{
194 struct net_device *dev = arg;
195 struct caif_device_entry *caifd = NULL;
196 struct caif_dev_common *caifdev;
197 enum cfcnfg_phy_preference pref;
198 int res = -EINVAL;
199 enum cfcnfg_phy_type phy_type;
200
201 if (dev->type != ARPHRD_CAIF)
202 return 0;
203
204 switch (what) {
205 case NETDEV_REGISTER:
Joe Perchesf6c93222010-09-05 22:08:09 +0000206 netdev_info(dev, "register\n");
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000207 caifd = caif_device_alloc(dev);
208 if (caifd == NULL)
209 break;
210 caifdev = netdev_priv(dev);
211 caifdev->flowctrl = dev_flowctrl;
212 atomic_set(&caifd->state, what);
213 res = 0;
214 break;
215
216 case NETDEV_UP:
Joe Perchesf6c93222010-09-05 22:08:09 +0000217 netdev_info(dev, "up\n");
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000218 caifd = caif_get(dev);
219 if (caifd == NULL)
220 break;
221 caifdev = netdev_priv(dev);
222 if (atomic_read(&caifd->state) == NETDEV_UP) {
Joe Perchesf6c93222010-09-05 22:08:09 +0000223 netdev_info(dev, "already up\n");
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000224 break;
225 }
226 atomic_set(&caifd->state, what);
227 caifd->layer.transmit = transmit;
228 caifd->layer.modemcmd = modemcmd;
229
230 if (caifdev->use_frag)
231 phy_type = CFPHYTYPE_FRAG;
232 else
233 phy_type = CFPHYTYPE_CAIF;
234
235 switch (caifdev->link_select) {
236 case CAIF_LINK_HIGH_BANDW:
Sjur Braendeland2c485202010-04-28 08:54:40 +0000237 pref = CFPHYPREF_HIGH_BW;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000238 break;
239 case CAIF_LINK_LOW_LATENCY:
Sjur Braendeland2c485202010-04-28 08:54:40 +0000240 pref = CFPHYPREF_LOW_LAT;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000241 break;
242 default:
243 pref = CFPHYPREF_HIGH_BW;
244 break;
245 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000246 dev_hold(dev);
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000247 cfcnfg_add_phy_layer(cfg,
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000248 phy_type,
249 dev,
250 &caifd->layer,
251 &caifd->phyid,
252 pref,
253 caifdev->use_fcs,
254 caifdev->use_stx);
255 strncpy(caifd->layer.name, dev->name,
256 sizeof(caifd->layer.name) - 1);
257 caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
258 break;
259
260 case NETDEV_GOING_DOWN:
261 caifd = caif_get(dev);
262 if (caifd == NULL)
263 break;
Joe Perchesf6c93222010-09-05 22:08:09 +0000264 netdev_info(dev, "going down\n");
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000265
266 if (atomic_read(&caifd->state) == NETDEV_GOING_DOWN ||
267 atomic_read(&caifd->state) == NETDEV_DOWN)
268 break;
269
270 atomic_set(&caifd->state, what);
271 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd)
272 return -EINVAL;
273 caifd->layer.up->ctrlcmd(caifd->layer.up,
274 _CAIF_CTRLCMD_PHYIF_DOWN_IND,
275 caifd->layer.id);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000276 might_sleep();
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000277 res = wait_event_interruptible_timeout(caifd->event,
278 atomic_read(&caifd->in_use) == 0,
279 TIMEOUT);
280 break;
281
282 case NETDEV_DOWN:
283 caifd = caif_get(dev);
284 if (caifd == NULL)
285 break;
Joe Perchesf6c93222010-09-05 22:08:09 +0000286 netdev_info(dev, "down\n");
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000287 if (atomic_read(&caifd->in_use))
Joe Perchesf6c93222010-09-05 22:08:09 +0000288 netdev_warn(dev,
289 "Unregistering an active CAIF device\n");
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000290 cfcnfg_del_phy_layer(cfg, &caifd->layer);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000291 dev_put(dev);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000292 atomic_set(&caifd->state, what);
293 break;
294
295 case NETDEV_UNREGISTER:
296 caifd = caif_get(dev);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000297 if (caifd == NULL)
298 break;
Joe Perchesf6c93222010-09-05 22:08:09 +0000299 netdev_info(dev, "unregister\n");
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000300 atomic_set(&caifd->state, what);
301 caif_device_destroy(dev);
302 break;
303 }
304 return 0;
305}
306
307static struct notifier_block caif_device_notifier = {
308 .notifier_call = caif_device_notify,
309 .priority = 0,
310};
311
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000312int caif_connect_client(struct caif_connect_request *conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000313 struct cflayer *client_layer, int *ifindex,
314 int *headroom, int *tailroom)
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000315{
316 struct cfctrl_link_param param;
Sjur Braendelande539d832010-04-28 08:54:35 +0000317 int ret;
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000318
319 ret = caif_connect_req_to_link_param(cfg, conn_req, &param);
Sjur Braendelande539d832010-04-28 08:54:35 +0000320 if (ret)
321 return ret;
322 /* Hook up the adaptation layer. */
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000323 return cfcnfg_add_adaptation_layer(cfg, &param,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000324 client_layer, ifindex,
325 headroom, tailroom);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000326}
327EXPORT_SYMBOL(caif_connect_client);
328
329int caif_disconnect_client(struct cflayer *adap_layer)
330{
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000331 return cfcnfg_disconn_adapt_layer(cfg, adap_layer);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000332}
333EXPORT_SYMBOL(caif_disconnect_client);
334
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000335/* Per-namespace Caif devices handling */
336static int caif_init_net(struct net *net)
337{
338 struct caif_net *caifn = net_generic(net, caif_net_id);
339 INIT_LIST_HEAD(&caifn->caifdevs.list);
340 spin_lock_init(&caifn->caifdevs.lock);
341 return 0;
342}
343
344static void caif_exit_net(struct net *net)
345{
346 struct net_device *dev;
347 int res;
348 rtnl_lock();
349 for_each_netdev(net, dev) {
350 if (dev->type != ARPHRD_CAIF)
351 continue;
352 res = dev_close(dev);
353 caif_device_destroy(dev);
354 }
355 rtnl_unlock();
356}
357
358static struct pernet_operations caif_net_ops = {
359 .init = caif_init_net,
360 .exit = caif_exit_net,
361 .id = &caif_net_id,
362 .size = sizeof(struct caif_net),
363};
364
365/* Initialize Caif devices list */
366static int __init caif_device_init(void)
367{
368 int result;
369 cfg = cfcnfg_create();
370 if (!cfg) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000371 pr_warn("can't create cfcnfg\n");
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000372 goto err_cfcnfg_create_failed;
373 }
374 result = register_pernet_device(&caif_net_ops);
375
376 if (result) {
377 kfree(cfg);
378 cfg = NULL;
379 return result;
380 }
381 dev_add_pack(&caif_packet_type);
382 register_netdevice_notifier(&caif_device_notifier);
383
384 return result;
385err_cfcnfg_create_failed:
386 return -ENODEV;
387}
388
389static void __exit caif_device_exit(void)
390{
391 dev_remove_pack(&caif_packet_type);
392 unregister_pernet_device(&caif_net_ops);
393 unregister_netdevice_notifier(&caif_device_notifier);
394 cfcnfg_remove(cfg);
395}
396
397module_init(caif_device_init);
398module_exit(caif_device_exit);