GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / caif / cfcnfg.c
blob1129ef1fbf41242f8721ba7f982c1c1f0e8b17b1
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 <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 */
37 unsigned int id;
38 /* Preference of the physical in interface */
39 enum cfcnfg_phy_preference pref;
41 /* Reference count, number of channels using the device */
42 int phy_ref_count;
44 /* Information about the physical device */
45 struct dev_info dev_info;
47 /* Interface index */
48 int ifindex;
50 /* Use Start of frame extension */
51 bool use_stx;
53 /* Use Start of frame checksum */
54 bool use_fcs;
57 struct cfcnfg {
58 struct cflayer layer;
59 struct cflayer *ctrl;
60 struct cflayer *mux;
61 u8 last_phyid;
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)
76 struct cfcnfg *this;
77 struct cfctrl_rsp *resp;
78 /* Initiate this layer */
79 this = kzalloc(sizeof(struct cfcnfg), GFP_ATOMIC);
80 if (!this) {
81 pr_warning("CAIF: %s(): Out of memory\n", __func__);
82 return NULL;
84 this->mux = cfmuxl_create();
85 if (!this->mux)
86 goto out_of_mem;
87 this->ctrl = cfctrl_create();
88 if (!this->ctrl)
89 goto out_of_mem;
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);
107 return this;
108 out_of_mem:
109 pr_warning("CAIF: %s(): Out of memory\n", __func__);
110 kfree(this->mux);
111 kfree(this->ctrl);
112 kfree(this);
113 return NULL;
115 EXPORT_SYMBOL(cfcnfg_create);
117 void cfcnfg_remove(struct cfcnfg *cfg)
119 if (cfg) {
120 kfree(cfg->mux);
121 kfree(cfg->ctrl);
122 kfree(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)
137 u16 i;
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;
158 return NULL;
161 static struct cfcnfg_phyinfo *cfcnfg_get_phyinfo(struct cfcnfg *cnfg,
162 u8 phyid)
164 int i;
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];
170 return NULL;
173 int cfcnfg_get_named(struct cfcnfg *cnfg, char *name)
175 int i;
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,
181 name) == 0)
182 return cnfg->phy_layers[i].frm_layer->id;
184 return 0;
187 int cfcnfg_disconn_adapt_layer(struct cfcnfg *cnfg, struct cflayer *adap_layer)
189 u8 channel_id = 0;
190 int ret = 0;
191 struct cflayer *servl = NULL;
192 struct cfcnfg_phyinfo *phyinfo = NULL;
193 u8 phyid = 0;
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__);
198 ret = -ENOTCONN;
199 goto end;
201 servl = cfmuxl_remove_uplayer(cnfg->mux, channel_id);
202 if (servl == NULL)
203 goto end;
204 layer_set_up(servl, NULL);
205 ret = cfctrl_linkdown_req(cnfg->ctrl, channel_id, adap_layer);
206 if (servl == NULL) {
207 pr_err("CAIF: %s(): PROTOCOL ERROR "
208 "- Error removing service_layer Channel_Id(%d)",
209 __func__, channel_id);
210 ret = -EINVAL;
211 goto end;
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",
221 __func__);
222 ret = -ENODEV;
223 goto end;
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",
230 __func__);
231 ret = -EINVAL;
232 goto end;
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);
241 end:
242 cfsrvl_put(servl);
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);
246 return ret;
249 EXPORT_SYMBOL(cfcnfg_disconn_adapt_layer);
251 void cfcnfg_release_adap_layer(struct cflayer *adap_layer)
253 if (adap_layer->dn)
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,
273 int *ifindex,
274 int *proto_head,
275 int *proto_tail)
277 struct cflayer *frml;
278 if (adap_layer == NULL) {
279 pr_err("CAIF: %s(): adap_layer is zero", __func__);
280 return -EINVAL;
282 if (adap_layer->receive == NULL) {
283 pr_err("CAIF: %s(): adap_layer->receive is NULL", __func__);
284 return -EINVAL;
286 if (adap_layer->ctrlcmd == NULL) {
287 pr_err("CAIF: %s(): adap_layer->ctrlcmd == NULL", __func__);
288 return -EINVAL;
290 frml = cnfg->phy_layers[param->phyid].frm_layer;
291 if (frml == NULL) {
292 pr_err("CAIF: %s(): Specified PHY type does not exist!",
293 __func__);
294 return -ENODEV;
296 caif_assert(param->phyid == cnfg->phy_layers[param->phyid].id);
297 caif_assert(cnfg->phy_layers[param->phyid].frm_layer->id ==
298 param->phyid);
299 caif_assert(cnfg->phy_layers[param->phyid].phy_layer->id ==
300 param->phyid);
302 *ifindex = cnfg->phy_layers[param->phyid].ifindex;
303 *proto_head =
304 protohead[param->linktype]+
305 (cnfg->phy_layers[param->phyid].use_stx ? 1 : 0);
307 *proto_tail = 2;
309 cfctrl_enum_req(cnfg->ctrl, param->phyid);
310 return cfctrl_linkup_request(cnfg->ctrl, param, adap_layer);
312 EXPORT_SYMBOL(cfcnfg_add_adaptation_layer);
314 static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
315 struct cflayer *adapt_layer)
317 if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
318 adapt_layer->ctrlcmd(adapt_layer,
319 CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
322 static void
323 cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv,
324 u8 phyid, struct cflayer *adapt_layer)
326 struct cfcnfg *cnfg = container_obj(layer);
327 struct cflayer *servicel = NULL;
328 struct cfcnfg_phyinfo *phyinfo;
329 struct net_device *netdev;
331 if (adapt_layer == NULL) {
332 pr_debug("CAIF: %s(): link setup response "
333 "but no client exist, send linkdown back\n",
334 __func__);
335 cfctrl_linkdown_req(cnfg->ctrl, channel_id, NULL);
336 return;
339 caif_assert(cnfg != NULL);
340 caif_assert(phyid != 0);
341 phyinfo = &cnfg->phy_layers[phyid];
342 caif_assert(phyinfo->id == phyid);
343 caif_assert(phyinfo->phy_layer != NULL);
344 caif_assert(phyinfo->phy_layer->id == phyid);
346 phyinfo->phy_ref_count++;
347 if (phyinfo->phy_ref_count == 1 &&
348 phyinfo->phy_layer->modemcmd != NULL) {
349 phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
350 _CAIF_MODEMCMD_PHYIF_USEFULL);
352 adapt_layer->id = channel_id;
354 switch (serv) {
355 case CFCTRL_SRV_VEI:
356 servicel = cfvei_create(channel_id, &phyinfo->dev_info);
357 break;
358 case CFCTRL_SRV_DATAGRAM:
359 servicel = cfdgml_create(channel_id, &phyinfo->dev_info);
360 break;
361 case CFCTRL_SRV_RFM:
362 netdev = phyinfo->dev_info.dev;
363 servicel = cfrfml_create(channel_id, &phyinfo->dev_info,
364 netdev->mtu);
365 break;
366 case CFCTRL_SRV_UTIL:
367 servicel = cfutill_create(channel_id, &phyinfo->dev_info);
368 break;
369 case CFCTRL_SRV_VIDEO:
370 servicel = cfvidl_create(channel_id, &phyinfo->dev_info);
371 break;
372 case CFCTRL_SRV_DBG:
373 servicel = cfdbgl_create(channel_id, &phyinfo->dev_info);
374 break;
375 default:
376 pr_err("CAIF: %s(): Protocol error. "
377 "Link setup response - unknown channel type\n",
378 __func__);
379 return;
381 if (!servicel) {
382 pr_warning("CAIF: %s(): Out of memory\n", __func__);
383 return;
385 layer_set_dn(servicel, cnfg->mux);
386 cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id);
387 layer_set_up(servicel, adapt_layer);
388 layer_set_dn(adapt_layer, servicel);
389 cfsrvl_get(servicel);
390 servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0);
393 void
394 cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
395 struct net_device *dev, struct cflayer *phy_layer,
396 u16 *phyid, enum cfcnfg_phy_preference pref,
397 bool fcs, bool stx)
399 struct cflayer *frml;
400 struct cflayer *phy_driver = NULL;
401 int i;
404 if (cnfg->phy_layers[cnfg->last_phyid].frm_layer == NULL) {
405 *phyid = cnfg->last_phyid;
407 /* range: * 1..(MAX_PHY_LAYERS-1) */
408 cnfg->last_phyid =
409 (cnfg->last_phyid % (MAX_PHY_LAYERS - 1)) + 1;
410 } else {
411 *phyid = 0;
412 for (i = 1; i < MAX_PHY_LAYERS; i++) {
413 if (cnfg->phy_layers[i].frm_layer == NULL) {
414 *phyid = i;
415 break;
419 if (*phyid == 0) {
420 pr_err("CAIF: %s(): No Available PHY ID\n", __func__);
421 return;
424 switch (phy_type) {
425 case CFPHYTYPE_FRAG:
426 phy_driver =
427 cfserl_create(CFPHYTYPE_FRAG, *phyid, stx);
428 if (!phy_driver) {
429 pr_warning("CAIF: %s(): Out of memory\n", __func__);
430 return;
433 break;
434 case CFPHYTYPE_CAIF:
435 phy_driver = NULL;
436 break;
437 default:
438 pr_err("CAIF: %s(): %d", __func__, phy_type);
439 return;
440 break;
443 phy_layer->id = *phyid;
444 cnfg->phy_layers[*phyid].pref = pref;
445 cnfg->phy_layers[*phyid].id = *phyid;
446 cnfg->phy_layers[*phyid].dev_info.id = *phyid;
447 cnfg->phy_layers[*phyid].dev_info.dev = dev;
448 cnfg->phy_layers[*phyid].phy_layer = phy_layer;
449 cnfg->phy_layers[*phyid].phy_ref_count = 0;
450 cnfg->phy_layers[*phyid].ifindex = dev->ifindex;
451 cnfg->phy_layers[*phyid].use_stx = stx;
452 cnfg->phy_layers[*phyid].use_fcs = fcs;
454 phy_layer->type = phy_type;
455 frml = cffrml_create(*phyid, fcs);
456 if (!frml) {
457 pr_warning("CAIF: %s(): Out of memory\n", __func__);
458 return;
460 cnfg->phy_layers[*phyid].frm_layer = frml;
461 cfmuxl_set_dnlayer(cnfg->mux, frml, *phyid);
462 layer_set_up(frml, cnfg->mux);
464 if (phy_driver != NULL) {
465 phy_driver->id = *phyid;
466 layer_set_dn(frml, phy_driver);
467 layer_set_up(phy_driver, frml);
468 layer_set_dn(phy_driver, phy_layer);
469 layer_set_up(phy_layer, phy_driver);
470 } else {
471 layer_set_dn(frml, phy_layer);
472 layer_set_up(phy_layer, frml);
475 EXPORT_SYMBOL(cfcnfg_add_phy_layer);
477 int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer)
479 struct cflayer *frml, *frml_dn;
480 u16 phyid;
481 phyid = phy_layer->id;
482 caif_assert(phyid == cnfg->phy_layers[phyid].id);
483 caif_assert(phy_layer == cnfg->phy_layers[phyid].phy_layer);
484 caif_assert(phy_layer->id == phyid);
485 caif_assert(cnfg->phy_layers[phyid].frm_layer->id == phyid);
487 memset(&cnfg->phy_layers[phy_layer->id], 0,
488 sizeof(struct cfcnfg_phyinfo));
489 frml = cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id);
490 frml_dn = frml->dn;
491 cffrml_set_uplayer(frml, NULL);
492 cffrml_set_dnlayer(frml, NULL);
493 kfree(frml);
495 if (phy_layer != frml_dn) {
496 layer_set_up(frml_dn, NULL);
497 layer_set_dn(frml_dn, NULL);
498 kfree(frml_dn);
500 layer_set_up(phy_layer, NULL);
501 return 0;
503 EXPORT_SYMBOL(cfcnfg_del_phy_layer);