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 <linux/netdevice.h>
10 #include <net/caif/caif_layer.h>
11 #include <net/caif/cfpkt.h>
12 #include <net/caif/cfcnfg.h>
13 #include <net/caif/cfctrl.h>
14 #include <net/caif/cfmuxl.h>
15 #include <net/caif/cffrml.h>
16 #include <net/caif/cfserl.h>
17 #include <net/caif/cfsrvl.h>
19 #include <linux/module.h>
20 #include <asm/atomic.h>
22 #define MAX_PHY_LAYERS 7
23 #define PHY_NAME_LEN 20
25 #define container_obj(layr) container_of(layr, struct cfcnfg, layer)
26 #define RFM_FRAGMENT_SIZE 4030
28 /* Information about CAIF physical interfaces held by Config Module in order
29 * to manage physical interfaces
31 struct cfcnfg_phyinfo
{
32 /* Pointer to the layer below the MUX (framing layer) */
33 struct cflayer
*frm_layer
;
34 /* Pointer to the lowest actual physical layer */
35 struct cflayer
*phy_layer
;
36 /* Unique identifier of the physical interface */
38 /* Preference of the physical in interface */
39 enum cfcnfg_phy_preference pref
;
41 /* Reference count, number of channels using the device */
44 /* Information about the physical device */
45 struct dev_info dev_info
;
50 /* Use Start of frame extension */
53 /* Use Start of frame checksum */
62 struct cfcnfg_phyinfo phy_layers
[MAX_PHY_LAYERS
];
65 static void cfcnfg_linkup_rsp(struct cflayer
*layer
, u8 channel_id
,
66 enum cfctrl_srv serv
, u8 phyid
,
67 struct cflayer
*adapt_layer
);
68 static void cfcnfg_linkdestroy_rsp(struct cflayer
*layer
, u8 channel_id
);
69 static void cfcnfg_reject_rsp(struct cflayer
*layer
, u8 channel_id
,
70 struct cflayer
*adapt_layer
);
71 static void cfctrl_resp_func(void);
72 static void cfctrl_enum_resp(void);
74 struct cfcnfg
*cfcnfg_create(void)
77 struct cfctrl_rsp
*resp
;
78 /* Initiate this layer */
79 this = kzalloc(sizeof(struct cfcnfg
), GFP_ATOMIC
);
81 pr_warning("CAIF: %s(): Out of memory\n", __func__
);
84 this->mux
= cfmuxl_create();
87 this->ctrl
= cfctrl_create();
90 /* Initiate response functions */
91 resp
= cfctrl_get_respfuncs(this->ctrl
);
92 resp
->enum_rsp
= cfctrl_enum_resp
;
93 resp
->linkerror_ind
= cfctrl_resp_func
;
94 resp
->linkdestroy_rsp
= cfcnfg_linkdestroy_rsp
;
95 resp
->sleep_rsp
= cfctrl_resp_func
;
96 resp
->wake_rsp
= cfctrl_resp_func
;
97 resp
->restart_rsp
= cfctrl_resp_func
;
98 resp
->radioset_rsp
= cfctrl_resp_func
;
99 resp
->linksetup_rsp
= cfcnfg_linkup_rsp
;
100 resp
->reject_rsp
= cfcnfg_reject_rsp
;
102 this->last_phyid
= 1;
104 cfmuxl_set_uplayer(this->mux
, this->ctrl
, 0);
105 layer_set_dn(this->ctrl
, this->mux
);
106 layer_set_up(this->ctrl
, this);
109 pr_warning("CAIF: %s(): Out of memory\n", __func__
);
115 EXPORT_SYMBOL(cfcnfg_create
);
117 void cfcnfg_remove(struct cfcnfg
*cfg
)
126 static void cfctrl_resp_func(void)
130 static void cfctrl_enum_resp(void)
134 struct dev_info
*cfcnfg_get_phyid(struct cfcnfg
*cnfg
,
135 enum cfcnfg_phy_preference phy_pref
)
139 /* Try to match with specified preference */
140 for (i
= 1; i
< MAX_PHY_LAYERS
; i
++) {
141 if (cnfg
->phy_layers
[i
].id
== i
&&
142 cnfg
->phy_layers
[i
].pref
== phy_pref
&&
143 cnfg
->phy_layers
[i
].frm_layer
!= NULL
) {
144 caif_assert(cnfg
->phy_layers
!= NULL
);
145 caif_assert(cnfg
->phy_layers
[i
].id
== i
);
146 return &cnfg
->phy_layers
[i
].dev_info
;
149 /* Otherwise just return something */
150 for (i
= 1; i
< MAX_PHY_LAYERS
; i
++) {
151 if (cnfg
->phy_layers
[i
].id
== i
) {
152 caif_assert(cnfg
->phy_layers
!= NULL
);
153 caif_assert(cnfg
->phy_layers
[i
].id
== i
);
154 return &cnfg
->phy_layers
[i
].dev_info
;
161 static struct cfcnfg_phyinfo
*cfcnfg_get_phyinfo(struct cfcnfg
*cnfg
,
165 /* Try to match with specified preference */
166 for (i
= 0; i
< MAX_PHY_LAYERS
; i
++)
167 if (cnfg
->phy_layers
[i
].frm_layer
!= NULL
&&
168 cnfg
->phy_layers
[i
].id
== phyid
)
169 return &cnfg
->phy_layers
[i
];
173 int cfcnfg_get_named(struct cfcnfg
*cnfg
, char *name
)
177 /* Try to match with specified name */
178 for (i
= 0; i
< MAX_PHY_LAYERS
; i
++) {
179 if (cnfg
->phy_layers
[i
].frm_layer
!= NULL
180 && strcmp(cnfg
->phy_layers
[i
].phy_layer
->name
,
182 return cnfg
->phy_layers
[i
].frm_layer
->id
;
187 int cfcnfg_disconn_adapt_layer(struct cfcnfg
*cnfg
, struct cflayer
*adap_layer
)
191 struct cflayer
*servl
= NULL
;
192 struct cfcnfg_phyinfo
*phyinfo
= NULL
;
194 caif_assert(adap_layer
!= NULL
);
195 channel_id
= adap_layer
->id
;
196 if (adap_layer
->dn
== NULL
|| channel_id
== 0) {
197 pr_err("CAIF: %s():adap_layer->id is 0\n", __func__
);
201 servl
= cfmuxl_remove_uplayer(cnfg
->mux
, channel_id
);
204 layer_set_up(servl
, NULL
);
205 ret
= cfctrl_linkdown_req(cnfg
->ctrl
, channel_id
, adap_layer
);
207 pr_err("CAIF: %s(): PROTOCOL ERROR "
208 "- Error removing service_layer Channel_Id(%d)",
209 __func__
, channel_id
);
213 caif_assert(channel_id
== servl
->id
);
214 if (adap_layer
->dn
!= NULL
) {
215 phyid
= cfsrvl_getphyid(adap_layer
->dn
);
217 phyinfo
= cfcnfg_get_phyinfo(cnfg
, phyid
);
218 if (phyinfo
== NULL
) {
219 pr_warning("CAIF: %s(): "
220 "No interface to send disconnect to\n",
225 if (phyinfo
->id
!= phyid
||
226 phyinfo
->phy_layer
->id
!= phyid
||
227 phyinfo
->frm_layer
->id
!= phyid
) {
228 pr_err("CAIF: %s(): "
229 "Inconsistency in phy registration\n",
235 if (phyinfo
!= NULL
&& --phyinfo
->phy_ref_count
== 0 &&
236 phyinfo
->phy_layer
!= NULL
&&
237 phyinfo
->phy_layer
->modemcmd
!= NULL
) {
238 phyinfo
->phy_layer
->modemcmd(phyinfo
->phy_layer
,
239 _CAIF_MODEMCMD_PHYIF_USELESS
);
243 cfctrl_cancel_req(cnfg
->ctrl
, adap_layer
);
244 if (adap_layer
->ctrlcmd
!= NULL
)
245 adap_layer
->ctrlcmd(adap_layer
, CAIF_CTRLCMD_DEINIT_RSP
, 0);
249 EXPORT_SYMBOL(cfcnfg_disconn_adapt_layer
);
251 void cfcnfg_release_adap_layer(struct cflayer
*adap_layer
)
254 cfsrvl_put(adap_layer
->dn
);
256 EXPORT_SYMBOL(cfcnfg_release_adap_layer
);
258 static void cfcnfg_linkdestroy_rsp(struct cflayer
*layer
, u8 channel_id
)
262 int protohead
[CFCTRL_SRV_MASK
] = {
263 [CFCTRL_SRV_VEI
] = 4,
264 [CFCTRL_SRV_DATAGRAM
] = 7,
265 [CFCTRL_SRV_UTIL
] = 4,
266 [CFCTRL_SRV_RFM
] = 3,
267 [CFCTRL_SRV_DBG
] = 3,
270 int cfcnfg_add_adaptation_layer(struct cfcnfg
*cnfg
,
271 struct cfctrl_link_param
*param
,
272 struct cflayer
*adap_layer
,
277 struct cflayer
*frml
;
278 if (adap_layer
== NULL
) {
279 pr_err("CAIF: %s(): adap_layer is zero", __func__
);
282 if (adap_layer
->receive
== NULL
) {
283 pr_err("CAIF: %s(): adap_layer->receive is NULL", __func__
);
286 if (adap_layer
->ctrlcmd
== NULL
) {
287 pr_err("CAIF: %s(): adap_layer->ctrlcmd == NULL", __func__
);
290 frml
= cnfg
->phy_layers
[param
->phyid
].frm_layer
;
292 pr_err("CAIF: %s(): Specified PHY type does not exist!",
296 caif_assert(param
->phyid
== cnfg
->phy_layers
[param
->phyid
].id
);
297 caif_assert(cnfg
->phy_layers
[param
->phyid
].frm_layer
->id
==
299 caif_assert(cnfg
->phy_layers
[param
->phyid
].phy_layer
->id
==
302 *ifindex
= cnfg
->phy_layers
[param
->phyid
].ifindex
;
304 protohead
[param
->linktype
]+
305 (cnfg
->phy_layers
[param
->phyid
].use_stx
? 1 : 0);
309 /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */
310 cfctrl_enum_req(cnfg
->ctrl
, param
->phyid
);
311 return cfctrl_linkup_request(cnfg
->ctrl
, param
, adap_layer
);
313 EXPORT_SYMBOL(cfcnfg_add_adaptation_layer
);
315 static void cfcnfg_reject_rsp(struct cflayer
*layer
, u8 channel_id
,
316 struct cflayer
*adapt_layer
)
318 if (adapt_layer
!= NULL
&& adapt_layer
->ctrlcmd
!= NULL
)
319 adapt_layer
->ctrlcmd(adapt_layer
,
320 CAIF_CTRLCMD_INIT_FAIL_RSP
, 0);
324 cfcnfg_linkup_rsp(struct cflayer
*layer
, u8 channel_id
, enum cfctrl_srv serv
,
325 u8 phyid
, struct cflayer
*adapt_layer
)
327 struct cfcnfg
*cnfg
= container_obj(layer
);
328 struct cflayer
*servicel
= NULL
;
329 struct cfcnfg_phyinfo
*phyinfo
;
330 struct net_device
*netdev
;
332 if (adapt_layer
== NULL
) {
333 pr_debug("CAIF: %s(): link setup response "
334 "but no client exist, send linkdown back\n",
336 cfctrl_linkdown_req(cnfg
->ctrl
, channel_id
, NULL
);
340 caif_assert(cnfg
!= NULL
);
341 caif_assert(phyid
!= 0);
342 phyinfo
= &cnfg
->phy_layers
[phyid
];
343 caif_assert(phyinfo
->id
== phyid
);
344 caif_assert(phyinfo
->phy_layer
!= NULL
);
345 caif_assert(phyinfo
->phy_layer
->id
== phyid
);
347 phyinfo
->phy_ref_count
++;
348 if (phyinfo
->phy_ref_count
== 1 &&
349 phyinfo
->phy_layer
->modemcmd
!= NULL
) {
350 phyinfo
->phy_layer
->modemcmd(phyinfo
->phy_layer
,
351 _CAIF_MODEMCMD_PHYIF_USEFULL
);
353 adapt_layer
->id
= channel_id
;
357 servicel
= cfvei_create(channel_id
, &phyinfo
->dev_info
);
359 case CFCTRL_SRV_DATAGRAM
:
360 servicel
= cfdgml_create(channel_id
, &phyinfo
->dev_info
);
363 netdev
= phyinfo
->dev_info
.dev
;
364 servicel
= cfrfml_create(channel_id
, &phyinfo
->dev_info
,
367 case CFCTRL_SRV_UTIL
:
368 servicel
= cfutill_create(channel_id
, &phyinfo
->dev_info
);
370 case CFCTRL_SRV_VIDEO
:
371 servicel
= cfvidl_create(channel_id
, &phyinfo
->dev_info
);
374 servicel
= cfdbgl_create(channel_id
, &phyinfo
->dev_info
);
377 pr_err("CAIF: %s(): Protocol error. "
378 "Link setup response - unknown channel type\n",
383 pr_warning("CAIF: %s(): Out of memory\n", __func__
);
386 layer_set_dn(servicel
, cnfg
->mux
);
387 cfmuxl_set_uplayer(cnfg
->mux
, servicel
, channel_id
);
388 layer_set_up(servicel
, adapt_layer
);
389 layer_set_dn(adapt_layer
, servicel
);
390 cfsrvl_get(servicel
);
391 servicel
->ctrlcmd(servicel
, CAIF_CTRLCMD_INIT_RSP
, 0);
395 cfcnfg_add_phy_layer(struct cfcnfg
*cnfg
, enum cfcnfg_phy_type phy_type
,
396 struct net_device
*dev
, struct cflayer
*phy_layer
,
397 u16
*phyid
, enum cfcnfg_phy_preference pref
,
400 struct cflayer
*frml
;
401 struct cflayer
*phy_driver
= NULL
;
405 if (cnfg
->phy_layers
[cnfg
->last_phyid
].frm_layer
== NULL
) {
406 *phyid
= cnfg
->last_phyid
;
408 /* range: * 1..(MAX_PHY_LAYERS-1) */
410 (cnfg
->last_phyid
% (MAX_PHY_LAYERS
- 1)) + 1;
413 for (i
= 1; i
< MAX_PHY_LAYERS
; i
++) {
414 if (cnfg
->phy_layers
[i
].frm_layer
== NULL
) {
421 pr_err("CAIF: %s(): No Available PHY ID\n", __func__
);
428 cfserl_create(CFPHYTYPE_FRAG
, *phyid
, stx
);
430 pr_warning("CAIF: %s(): Out of memory\n", __func__
);
439 pr_err("CAIF: %s(): %d", __func__
, phy_type
);
444 phy_layer
->id
= *phyid
;
445 cnfg
->phy_layers
[*phyid
].pref
= pref
;
446 cnfg
->phy_layers
[*phyid
].id
= *phyid
;
447 cnfg
->phy_layers
[*phyid
].dev_info
.id
= *phyid
;
448 cnfg
->phy_layers
[*phyid
].dev_info
.dev
= dev
;
449 cnfg
->phy_layers
[*phyid
].phy_layer
= phy_layer
;
450 cnfg
->phy_layers
[*phyid
].phy_ref_count
= 0;
451 cnfg
->phy_layers
[*phyid
].ifindex
= dev
->ifindex
;
452 cnfg
->phy_layers
[*phyid
].use_stx
= stx
;
453 cnfg
->phy_layers
[*phyid
].use_fcs
= fcs
;
455 phy_layer
->type
= phy_type
;
456 frml
= cffrml_create(*phyid
, fcs
);
458 pr_warning("CAIF: %s(): Out of memory\n", __func__
);
461 cnfg
->phy_layers
[*phyid
].frm_layer
= frml
;
462 cfmuxl_set_dnlayer(cnfg
->mux
, frml
, *phyid
);
463 layer_set_up(frml
, cnfg
->mux
);
465 if (phy_driver
!= NULL
) {
466 phy_driver
->id
= *phyid
;
467 layer_set_dn(frml
, phy_driver
);
468 layer_set_up(phy_driver
, frml
);
469 layer_set_dn(phy_driver
, phy_layer
);
470 layer_set_up(phy_layer
, phy_driver
);
472 layer_set_dn(frml
, phy_layer
);
473 layer_set_up(phy_layer
, frml
);
476 EXPORT_SYMBOL(cfcnfg_add_phy_layer
);
478 int cfcnfg_del_phy_layer(struct cfcnfg
*cnfg
, struct cflayer
*phy_layer
)
480 struct cflayer
*frml
, *frml_dn
;
482 phyid
= phy_layer
->id
;
483 caif_assert(phyid
== cnfg
->phy_layers
[phyid
].id
);
484 caif_assert(phy_layer
== cnfg
->phy_layers
[phyid
].phy_layer
);
485 caif_assert(phy_layer
->id
== phyid
);
486 caif_assert(cnfg
->phy_layers
[phyid
].frm_layer
->id
== phyid
);
488 memset(&cnfg
->phy_layers
[phy_layer
->id
], 0,
489 sizeof(struct cfcnfg_phyinfo
));
490 frml
= cfmuxl_remove_dnlayer(cnfg
->mux
, phy_layer
->id
);
492 cffrml_set_uplayer(frml
, NULL
);
493 cffrml_set_dnlayer(frml
, NULL
);
496 if (phy_layer
!= frml_dn
) {
497 layer_set_up(frml_dn
, NULL
);
498 layer_set_dn(frml_dn
, NULL
);
501 layer_set_up(phy_layer
, NULL
);
504 EXPORT_SYMBOL(cfcnfg_del_phy_layer
);