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
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>
18 #include <linux/module.h>
19 #include <asm/atomic.h>
21 #define MAX_PHY_LAYERS 7
22 #define PHY_NAME_LEN 20
24 #define container_obj(layr) container_of(layr, struct cfcnfg, layer)
25 #define RFM_FRAGMENT_SIZE 4030
27 /* Information about CAIF physical interfaces held by Config Module in order
28 * to manage physical interfaces
30 struct cfcnfg_phyinfo {
31 /* Pointer to the layer below the MUX (framing layer) */
32 struct cflayer *frm_layer;
33 /* Pointer to the lowest actual physical layer */
34 struct cflayer *phy_layer;
35 /* Unique identifier of the physical interface */
37 /* Preference of the physical in interface */
38 enum cfcnfg_phy_preference pref;
40 /* Reference count, number of channels using the device */
43 /* Information about the physical device */
44 struct dev_info dev_info;
52 struct cfcnfg_phyinfo phy_layers[MAX_PHY_LAYERS];
55 static void cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id,
56 enum cfctrl_srv serv, u8 phyid,
57 struct cflayer *adapt_layer);
58 static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id);
59 static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
60 struct cflayer *adapt_layer);
61 static void cfctrl_resp_func(void);
62 static void cfctrl_enum_resp(void);
64 struct cfcnfg *cfcnfg_create(void)
67 struct cfctrl_rsp *resp;
68 /* Initiate this layer */
69 this = kzalloc(sizeof(struct cfcnfg), GFP_ATOMIC);
71 pr_warning("CAIF: %s(): Out of memory\n", __func__);
74 this->mux = cfmuxl_create();
77 this->ctrl = cfctrl_create();
80 /* Initiate response functions */
81 resp = cfctrl_get_respfuncs(this->ctrl);
82 resp->enum_rsp = cfctrl_enum_resp;
83 resp->linkerror_ind = cfctrl_resp_func;
84 resp->linkdestroy_rsp = cfcnfg_linkdestroy_rsp;
85 resp->sleep_rsp = cfctrl_resp_func;
86 resp->wake_rsp = cfctrl_resp_func;
87 resp->restart_rsp = cfctrl_resp_func;
88 resp->radioset_rsp = cfctrl_resp_func;
89 resp->linksetup_rsp = cfcnfg_linkup_rsp;
90 resp->reject_rsp = cfcnfg_reject_rsp;
94 cfmuxl_set_uplayer(this->mux, this->ctrl, 0);
95 layer_set_dn(this->ctrl, this->mux);
96 layer_set_up(this->ctrl, this);
99 pr_warning("CAIF: %s(): Out of memory\n", __func__);
105 EXPORT_SYMBOL(cfcnfg_create);
107 void cfcnfg_remove(struct cfcnfg *cfg)
116 static void cfctrl_resp_func(void)
120 static void cfctrl_enum_resp(void)
124 struct dev_info *cfcnfg_get_phyid(struct cfcnfg *cnfg,
125 enum cfcnfg_phy_preference phy_pref)
129 /* Try to match with specified preference */
130 for (i = 1; i < MAX_PHY_LAYERS; i++) {
131 if (cnfg->phy_layers[i].id == i &&
132 cnfg->phy_layers[i].pref == phy_pref &&
133 cnfg->phy_layers[i].frm_layer != NULL) {
134 caif_assert(cnfg->phy_layers != NULL);
135 caif_assert(cnfg->phy_layers[i].id == i);
136 return &cnfg->phy_layers[i].dev_info;
139 /* Otherwise just return something */
140 for (i = 1; i < MAX_PHY_LAYERS; i++) {
141 if (cnfg->phy_layers[i].id == i) {
142 caif_assert(cnfg->phy_layers != NULL);
143 caif_assert(cnfg->phy_layers[i].id == i);
144 return &cnfg->phy_layers[i].dev_info;
151 static struct cfcnfg_phyinfo *cfcnfg_get_phyinfo(struct cfcnfg *cnfg,
155 /* Try to match with specified preference */
156 for (i = 0; i < MAX_PHY_LAYERS; i++)
157 if (cnfg->phy_layers[i].frm_layer != NULL &&
158 cnfg->phy_layers[i].id == phyid)
159 return &cnfg->phy_layers[i];
163 int cfcnfg_get_named(struct cfcnfg *cnfg, char *name)
167 /* Try to match with specified name */
168 for (i = 0; i < MAX_PHY_LAYERS; i++) {
169 if (cnfg->phy_layers[i].frm_layer != NULL
170 && strcmp(cnfg->phy_layers[i].phy_layer->name,
172 return cnfg->phy_layers[i].frm_layer->id;
177 int cfcnfg_disconn_adapt_layer(struct cfcnfg *cnfg, struct cflayer *adap_layer)
181 struct cflayer *servl = NULL;
182 struct cfcnfg_phyinfo *phyinfo = NULL;
184 caif_assert(adap_layer != NULL);
185 channel_id = adap_layer->id;
186 if (adap_layer->dn == NULL || channel_id == 0) {
187 pr_err("CAIF: %s():adap_layer->id is 0\n", __func__);
191 servl = cfmuxl_remove_uplayer(cnfg->mux, channel_id);
194 layer_set_up(servl, NULL);
195 ret = cfctrl_linkdown_req(cnfg->ctrl, channel_id, adap_layer);
197 pr_err("CAIF: %s(): PROTOCOL ERROR "
198 "- Error removing service_layer Channel_Id(%d)",
199 __func__, channel_id);
203 caif_assert(channel_id == servl->id);
204 if (adap_layer->dn != NULL) {
205 phyid = cfsrvl_getphyid(adap_layer->dn);
207 phyinfo = cfcnfg_get_phyinfo(cnfg, phyid);
208 if (phyinfo == NULL) {
209 pr_warning("CAIF: %s(): "
210 "No interface to send disconnect to\n",
215 if (phyinfo->id != phyid ||
216 phyinfo->phy_layer->id != phyid ||
217 phyinfo->frm_layer->id != phyid) {
218 pr_err("CAIF: %s(): "
219 "Inconsistency in phy registration\n",
225 if (phyinfo != NULL && --phyinfo->phy_ref_count == 0 &&
226 phyinfo->phy_layer != NULL &&
227 phyinfo->phy_layer->modemcmd != NULL) {
228 phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
229 _CAIF_MODEMCMD_PHYIF_USELESS);
233 cfctrl_cancel_req(cnfg->ctrl, adap_layer);
234 if (adap_layer->ctrlcmd != NULL)
235 adap_layer->ctrlcmd(adap_layer, CAIF_CTRLCMD_DEINIT_RSP, 0);
239 EXPORT_SYMBOL(cfcnfg_disconn_adapt_layer);
241 void cfcnfg_release_adap_layer(struct cflayer *adap_layer)
244 cfsrvl_put(adap_layer->dn);
246 EXPORT_SYMBOL(cfcnfg_release_adap_layer);
248 static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id)
252 int cfcnfg_add_adaptation_layer(struct cfcnfg *cnfg,
253 struct cfctrl_link_param *param,
254 struct cflayer *adap_layer)
256 struct cflayer *frml;
257 if (adap_layer == NULL) {
258 pr_err("CAIF: %s(): adap_layer is zero", __func__);
261 if (adap_layer->receive == NULL) {
262 pr_err("CAIF: %s(): adap_layer->receive is NULL", __func__);
265 if (adap_layer->ctrlcmd == NULL) {
266 pr_err("CAIF: %s(): adap_layer->ctrlcmd == NULL", __func__);
269 frml = cnfg->phy_layers[param->phyid].frm_layer;
271 pr_err("CAIF: %s(): Specified PHY type does not exist!",
275 caif_assert(param->phyid == cnfg->phy_layers[param->phyid].id);
276 caif_assert(cnfg->phy_layers[param->phyid].frm_layer->id ==
278 caif_assert(cnfg->phy_layers[param->phyid].phy_layer->id ==
280 /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */
281 cfctrl_enum_req(cnfg->ctrl, param->phyid);
282 return cfctrl_linkup_request(cnfg->ctrl, param, adap_layer);
284 EXPORT_SYMBOL(cfcnfg_add_adaptation_layer);
286 static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
287 struct cflayer *adapt_layer)
289 if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
290 adapt_layer->ctrlcmd(adapt_layer,
291 CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
295 cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv,
296 u8 phyid, struct cflayer *adapt_layer)
298 struct cfcnfg *cnfg = container_obj(layer);
299 struct cflayer *servicel = NULL;
300 struct cfcnfg_phyinfo *phyinfo;
301 if (adapt_layer == NULL) {
302 pr_debug("CAIF: %s(): link setup response "
303 "but no client exist, send linkdown back\n",
305 cfctrl_linkdown_req(cnfg->ctrl, channel_id, NULL);
309 caif_assert(cnfg != NULL);
310 caif_assert(phyid != 0);
311 phyinfo = &cnfg->phy_layers[phyid];
312 caif_assert(phyinfo->id == phyid);
313 caif_assert(phyinfo->phy_layer != NULL);
314 caif_assert(phyinfo->phy_layer->id == phyid);
316 phyinfo->phy_ref_count++;
317 if (phyinfo->phy_ref_count == 1 &&
318 phyinfo->phy_layer->modemcmd != NULL) {
319 phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
320 _CAIF_MODEMCMD_PHYIF_USEFULL);
322 adapt_layer->id = channel_id;
326 servicel = cfvei_create(channel_id, &phyinfo->dev_info);
328 case CFCTRL_SRV_DATAGRAM:
329 servicel = cfdgml_create(channel_id, &phyinfo->dev_info);
332 servicel = cfrfml_create(channel_id, &phyinfo->dev_info,
335 case CFCTRL_SRV_UTIL:
336 servicel = cfutill_create(channel_id, &phyinfo->dev_info);
338 case CFCTRL_SRV_VIDEO:
339 servicel = cfvidl_create(channel_id, &phyinfo->dev_info);
342 servicel = cfdbgl_create(channel_id, &phyinfo->dev_info);
345 pr_err("CAIF: %s(): Protocol error. "
346 "Link setup response - unknown channel type\n",
351 pr_warning("CAIF: %s(): Out of memory\n", __func__);
354 layer_set_dn(servicel, cnfg->mux);
355 cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id);
356 layer_set_up(servicel, adapt_layer);
357 layer_set_dn(adapt_layer, servicel);
358 cfsrvl_get(servicel);
359 servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0);
363 cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
364 void *dev, struct cflayer *phy_layer, u16 *phyid,
365 enum cfcnfg_phy_preference pref,
368 struct cflayer *frml;
369 struct cflayer *phy_driver = NULL;
373 if (cnfg->phy_layers[cnfg->last_phyid].frm_layer == NULL) {
374 *phyid = cnfg->last_phyid;
376 /* range: * 1..(MAX_PHY_LAYERS-1) */
378 (cnfg->last_phyid % (MAX_PHY_LAYERS - 1)) + 1;
381 for (i = 1; i < MAX_PHY_LAYERS; i++) {
382 if (cnfg->phy_layers[i].frm_layer == NULL) {
389 pr_err("CAIF: %s(): No Available PHY ID\n", __func__);
396 cfserl_create(CFPHYTYPE_FRAG, *phyid, stx);
398 pr_warning("CAIF: %s(): Out of memory\n", __func__);
407 pr_err("CAIF: %s(): %d", __func__, phy_type);
412 phy_layer->id = *phyid;
413 cnfg->phy_layers[*phyid].pref = pref;
414 cnfg->phy_layers[*phyid].id = *phyid;
415 cnfg->phy_layers[*phyid].dev_info.id = *phyid;
416 cnfg->phy_layers[*phyid].dev_info.dev = dev;
417 cnfg->phy_layers[*phyid].phy_layer = phy_layer;
418 cnfg->phy_layers[*phyid].phy_ref_count = 0;
419 phy_layer->type = phy_type;
420 frml = cffrml_create(*phyid, fcs);
422 pr_warning("CAIF: %s(): Out of memory\n", __func__);
425 cnfg->phy_layers[*phyid].frm_layer = frml;
426 cfmuxl_set_dnlayer(cnfg->mux, frml, *phyid);
427 layer_set_up(frml, cnfg->mux);
429 if (phy_driver != NULL) {
430 phy_driver->id = *phyid;
431 layer_set_dn(frml, phy_driver);
432 layer_set_up(phy_driver, frml);
433 layer_set_dn(phy_driver, phy_layer);
434 layer_set_up(phy_layer, phy_driver);
436 layer_set_dn(frml, phy_layer);
437 layer_set_up(phy_layer, frml);
440 EXPORT_SYMBOL(cfcnfg_add_phy_layer);
442 int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer)
444 struct cflayer *frml, *frml_dn;
446 phyid = phy_layer->id;
447 caif_assert(phyid == cnfg->phy_layers[phyid].id);
448 caif_assert(phy_layer == cnfg->phy_layers[phyid].phy_layer);
449 caif_assert(phy_layer->id == phyid);
450 caif_assert(cnfg->phy_layers[phyid].frm_layer->id == phyid);
452 memset(&cnfg->phy_layers[phy_layer->id], 0,
453 sizeof(struct cfcnfg_phyinfo));
454 frml = cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id);
456 cffrml_set_uplayer(frml, NULL);
457 cffrml_set_dnlayer(frml, NULL);
460 if (phy_layer != frml_dn) {
461 layer_set_up(frml_dn, NULL);
462 layer_set_dn(frml_dn, NULL);
465 layer_set_up(phy_layer, NULL);
468 EXPORT_SYMBOL(cfcnfg_del_phy_layer);