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
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
9 #include <linux/kernel.h>
10 #include <linux/stddef.h>
11 #include <linux/slab.h>
12 #include <linux/netdevice.h>
13 #include <linux/module.h>
14 #include <net/caif/caif_layer.h>
15 #include <net/caif/cfpkt.h>
16 #include <net/caif/cfcnfg.h>
17 #include <net/caif/cfctrl.h>
18 #include <net/caif/cfmuxl.h>
19 #include <net/caif/cffrml.h>
20 #include <net/caif/cfserl.h>
21 #include <net/caif/cfsrvl.h>
22 #include <net/caif/caif_dev.h>
24 #define container_obj(layr) container_of(layr, struct cfcnfg, layer)
26 /* Information about CAIF physical interfaces held by Config Module in order
27 * to manage physical interfaces
29 struct cfcnfg_phyinfo {
30 struct list_head node;
33 /* Pointer to the layer below the MUX (framing layer) */
34 struct cflayer *frm_layer;
35 /* Pointer to the lowest actual physical layer */
36 struct cflayer *phy_layer;
37 /* Unique identifier of the physical interface */
39 /* Preference of the physical in interface */
40 enum cfcnfg_phy_preference pref;
42 /* Information about the physical device */
43 struct dev_info dev_info;
48 /* Use Start of frame extension */
51 /* Use Start of frame checksum */
59 struct list_head phys;
63 static void cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id,
64 enum cfctrl_srv serv, u8 phyid,
65 struct cflayer *adapt_layer);
66 static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id);
67 static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
68 struct cflayer *adapt_layer);
69 static void cfctrl_resp_func(void);
70 static void cfctrl_enum_resp(void);
72 struct cfcnfg *cfcnfg_create(void)
75 struct cfctrl_rsp *resp;
79 /* Initiate this layer */
80 this = kzalloc(sizeof(struct cfcnfg), GFP_ATOMIC);
82 pr_warn("Out of memory\n");
85 this->mux = cfmuxl_create();
88 this->ctrl = cfctrl_create();
91 /* Initiate response functions */
92 resp = cfctrl_get_respfuncs(this->ctrl);
93 resp->enum_rsp = cfctrl_enum_resp;
94 resp->linkerror_ind = cfctrl_resp_func;
95 resp->linkdestroy_rsp = cfcnfg_linkdestroy_rsp;
96 resp->sleep_rsp = cfctrl_resp_func;
97 resp->wake_rsp = cfctrl_resp_func;
98 resp->restart_rsp = cfctrl_resp_func;
99 resp->radioset_rsp = cfctrl_resp_func;
100 resp->linksetup_rsp = cfcnfg_linkup_rsp;
101 resp->reject_rsp = cfcnfg_reject_rsp;
102 INIT_LIST_HEAD(&this->phys);
104 cfmuxl_set_uplayer(this->mux, this->ctrl, 0);
105 layer_set_dn(this->ctrl, this->mux);
106 layer_set_up(this->ctrl, this);
107 mutex_init(&this->lock);
111 pr_warn("Out of memory\n");
120 EXPORT_SYMBOL(cfcnfg_create);
122 void cfcnfg_remove(struct cfcnfg *cfg)
134 static void cfctrl_resp_func(void)
138 static struct cfcnfg_phyinfo *cfcnfg_get_phyinfo_rcu(struct cfcnfg *cnfg,
141 struct cfcnfg_phyinfo *phy;
143 list_for_each_entry_rcu(phy, &cnfg->phys, node)
144 if (phy->id == phyid)
149 static void cfctrl_enum_resp(void)
153 struct dev_info *cfcnfg_get_phyid(struct cfcnfg *cnfg,
154 enum cfcnfg_phy_preference phy_pref)
156 /* Try to match with specified preference */
157 struct cfcnfg_phyinfo *phy;
159 list_for_each_entry_rcu(phy, &cnfg->phys, node) {
160 if (phy->up && phy->pref == phy_pref &&
161 phy->frm_layer != NULL)
163 return &phy->dev_info;
166 /* Otherwise just return something */
167 list_for_each_entry_rcu(phy, &cnfg->phys, node)
169 return &phy->dev_info;
174 int cfcnfg_get_id_from_ifi(struct cfcnfg *cnfg, int ifi)
176 struct cfcnfg_phyinfo *phy;
178 list_for_each_entry_rcu(phy, &cnfg->phys, node)
179 if (phy->ifindex == ifi && phy->up)
184 int cfcnfg_disconn_adapt_layer(struct cfcnfg *cfg, struct cflayer *adap_layer)
188 struct cflayer *servl = NULL;
190 caif_assert(adap_layer != NULL);
192 channel_id = adap_layer->id;
193 if (adap_layer->dn == NULL || channel_id == 0) {
194 pr_err("adap_layer->dn == NULL or adap_layer->id is 0\n");
199 servl = cfmuxl_remove_uplayer(cfg->mux, channel_id);
201 pr_err("PROTOCOL ERROR - "
202 "Error removing service_layer Channel_Id(%d)",
208 ret = cfctrl_linkdown_req(cfg->ctrl, channel_id, adap_layer);
211 cfctrl_cancel_req(cfg->ctrl, adap_layer);
213 /* Do RCU sync before initiating cleanup */
215 if (adap_layer->ctrlcmd != NULL)
216 adap_layer->ctrlcmd(adap_layer, CAIF_CTRLCMD_DEINIT_RSP, 0);
220 EXPORT_SYMBOL(cfcnfg_disconn_adapt_layer);
222 void cfcnfg_release_adap_layer(struct cflayer *adap_layer)
225 cfsrvl_put(adap_layer->dn);
227 EXPORT_SYMBOL(cfcnfg_release_adap_layer);
229 static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id)
233 static const int protohead[CFCTRL_SRV_MASK] = {
234 [CFCTRL_SRV_VEI] = 4,
235 [CFCTRL_SRV_DATAGRAM] = 7,
236 [CFCTRL_SRV_UTIL] = 4,
237 [CFCTRL_SRV_RFM] = 3,
238 [CFCTRL_SRV_DBG] = 3,
241 int cfcnfg_add_adaptation_layer(struct cfcnfg *cnfg,
242 struct cfctrl_link_param *param,
243 struct cflayer *adap_layer,
248 struct cflayer *frml;
249 struct cfcnfg_phyinfo *phy;
253 phy = cfcnfg_get_phyinfo_rcu(cnfg, param->phyid);
260 if (adap_layer == NULL) {
261 pr_err("adap_layer is zero\n");
264 if (adap_layer->receive == NULL) {
265 pr_err("adap_layer->receive is NULL\n");
268 if (adap_layer->ctrlcmd == NULL) {
269 pr_err("adap_layer->ctrlcmd == NULL\n");
274 frml = phy->frm_layer;
276 pr_err("Specified PHY type does not exist!\n");
279 caif_assert(param->phyid == phy->id);
280 caif_assert(phy->frm_layer->id ==
282 caif_assert(phy->phy_layer->id ==
285 *ifindex = phy->ifindex;
288 protohead[param->linktype] + (phy->use_stx ? 1 : 0);
292 /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */
293 cfctrl_enum_req(cnfg->ctrl, param->phyid);
294 return cfctrl_linkup_request(cnfg->ctrl, param, adap_layer);
300 EXPORT_SYMBOL(cfcnfg_add_adaptation_layer);
302 static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
303 struct cflayer *adapt_layer)
305 if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
306 adapt_layer->ctrlcmd(adapt_layer,
307 CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
311 cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv,
312 u8 phyid, struct cflayer *adapt_layer)
314 struct cfcnfg *cnfg = container_obj(layer);
315 struct cflayer *servicel = NULL;
316 struct cfcnfg_phyinfo *phyinfo;
317 struct net_device *netdev;
321 if (adapt_layer == NULL) {
322 pr_debug("link setup response but no client exist,"
323 "send linkdown back\n");
324 cfctrl_linkdown_req(cnfg->ctrl, channel_id, NULL);
328 caif_assert(cnfg != NULL);
329 caif_assert(phyid != 0);
331 phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phyid);
332 if (phyinfo == NULL) {
333 pr_err("ERROR: Link Layer Device dissapeared"
334 "while connecting\n");
338 caif_assert(phyinfo != NULL);
339 caif_assert(phyinfo->id == phyid);
340 caif_assert(phyinfo->phy_layer != NULL);
341 caif_assert(phyinfo->phy_layer->id == phyid);
343 adapt_layer->id = channel_id;
347 servicel = cfvei_create(channel_id, &phyinfo->dev_info);
349 case CFCTRL_SRV_DATAGRAM:
350 servicel = cfdgml_create(channel_id,
354 netdev = phyinfo->dev_info.dev;
355 servicel = cfrfml_create(channel_id, &phyinfo->dev_info,
358 case CFCTRL_SRV_UTIL:
359 servicel = cfutill_create(channel_id, &phyinfo->dev_info);
361 case CFCTRL_SRV_VIDEO:
362 servicel = cfvidl_create(channel_id, &phyinfo->dev_info);
365 servicel = cfdbgl_create(channel_id, &phyinfo->dev_info);
368 pr_err("Protocol error. Link setup response "
369 "- unknown channel type\n");
373 pr_warn("Out of memory\n");
376 layer_set_dn(servicel, cnfg->mux);
377 cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id);
378 layer_set_up(servicel, adapt_layer);
379 layer_set_dn(adapt_layer, servicel);
383 servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0);
390 cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
391 struct net_device *dev, struct cflayer *phy_layer,
392 u16 *phy_id, enum cfcnfg_phy_preference pref,
395 struct cflayer *frml;
396 struct cflayer *phy_driver = NULL;
397 struct cfcnfg_phyinfo *phyinfo;
401 mutex_lock(&cnfg->lock);
403 /* CAIF protocol allow maximum 6 link-layers */
404 for (i = 0; i < 7; i++) {
405 phyid = (dev->ifindex + i) & 0x7;
408 if (cfcnfg_get_phyinfo_rcu(cnfg, phyid) == NULL)
411 pr_warn("Too many CAIF Link Layers (max 6)\n");
415 phyinfo = kzalloc(sizeof(struct cfcnfg_phyinfo), GFP_ATOMIC);
420 cfserl_create(CFPHYTYPE_FRAG, phyid, stx);
422 pr_warn("Out of memory\n");
432 phy_layer->id = phyid;
433 phyinfo->pref = pref;
435 phyinfo->dev_info.id = phyid;
436 phyinfo->dev_info.dev = dev;
437 phyinfo->phy_layer = phy_layer;
438 phyinfo->ifindex = dev->ifindex;
439 phyinfo->use_stx = stx;
440 phyinfo->use_fcs = fcs;
442 phy_layer->type = phy_type;
443 frml = cffrml_create(phyid, fcs);
446 pr_warn("Out of memory\n");
450 phyinfo->frm_layer = frml;
451 layer_set_up(frml, cnfg->mux);
453 if (phy_driver != NULL) {
454 phy_driver->id = phyid;
455 layer_set_dn(frml, phy_driver);
456 layer_set_up(phy_driver, frml);
457 layer_set_dn(phy_driver, phy_layer);
458 layer_set_up(phy_layer, phy_driver);
460 layer_set_dn(frml, phy_layer);
461 layer_set_up(phy_layer, frml);
464 list_add_rcu(&phyinfo->node, &cnfg->phys);
466 mutex_unlock(&cnfg->lock);
468 EXPORT_SYMBOL(cfcnfg_add_phy_layer);
470 int cfcnfg_set_phy_state(struct cfcnfg *cnfg, struct cflayer *phy_layer,
473 struct cfcnfg_phyinfo *phyinfo;
476 phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phy_layer->id);
477 if (phyinfo == NULL) {
482 if (phyinfo->up == up) {
489 cffrml_hold(phyinfo->frm_layer);
490 cfmuxl_set_dnlayer(cnfg->mux, phyinfo->frm_layer,
493 cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id);
494 cffrml_put(phyinfo->frm_layer);
500 EXPORT_SYMBOL(cfcnfg_set_phy_state);
502 int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer)
504 struct cflayer *frml, *frml_dn;
506 struct cfcnfg_phyinfo *phyinfo;
510 mutex_lock(&cnfg->lock);
512 phyid = phy_layer->id;
513 phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phyid);
517 caif_assert(phyid == phyinfo->id);
518 caif_assert(phy_layer == phyinfo->phy_layer);
519 caif_assert(phy_layer->id == phyid);
520 caif_assert(phyinfo->frm_layer->id == phyid);
522 /* Fail if reference count is not zero */
523 if (cffrml_refcnt_read(phyinfo->frm_layer) != 0) {
524 pr_info("Wait for device inuse\n");
525 mutex_unlock(&cnfg->lock);
529 list_del_rcu(&phyinfo->node);
532 frml = phyinfo->frm_layer;
534 cffrml_set_uplayer(frml, NULL);
535 cffrml_set_dnlayer(frml, NULL);
536 if (phy_layer != frml_dn) {
537 layer_set_up(frml_dn, NULL);
538 layer_set_dn(frml_dn, NULL);
540 layer_set_up(phy_layer, NULL);
544 if (phyinfo->phy_layer != frml_dn)
549 mutex_unlock(&cnfg->lock);
553 EXPORT_SYMBOL(cfcnfg_del_phy_layer);