]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/caif/cfcnfg.c
caif: Add reference counting to service layer
[net-next-2.6.git] / net / caif / cfcnfg.c
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 <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>
17
18 #include <linux/module.h>
19 #include <asm/atomic.h>
20
21 #define MAX_PHY_LAYERS 7
22 #define PHY_NAME_LEN 20
23
24 #define container_obj(layr) container_of(layr, struct cfcnfg, layer)
25
26 /* Information about CAIF physical interfaces held by Config Module in order
27  * to manage physical interfaces
28  */
29 struct cfcnfg_phyinfo {
30         /* Pointer to the layer below the MUX (framing layer) */
31         struct cflayer *frm_layer;
32         /* Pointer to the lowest actual physical layer */
33         struct cflayer *phy_layer;
34         /* Unique identifier of the physical interface */
35         unsigned int id;
36         /* Preference of the physical in interface */
37         enum cfcnfg_phy_preference pref;
38
39         /* Reference count, number of channels using the device */
40         int phy_ref_count;
41
42         /* Information about the physical device */
43         struct dev_info dev_info;
44 };
45
46 struct cfcnfg {
47         struct cflayer layer;
48         struct cflayer *ctrl;
49         struct cflayer *mux;
50         u8 last_phyid;
51         struct cfcnfg_phyinfo phy_layers[MAX_PHY_LAYERS];
52 };
53
54 static void cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id,
55                              enum cfctrl_srv serv, u8 phyid,
56                              struct cflayer *adapt_layer);
57 static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id,
58                                   struct cflayer *client_layer);
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);
63
64 struct cfcnfg *cfcnfg_create(void)
65 {
66         struct cfcnfg *this;
67         struct cfctrl_rsp *resp;
68         /* Initiate this layer */
69         this = kmalloc(sizeof(struct cfcnfg), GFP_ATOMIC);
70         if (!this) {
71                 pr_warning("CAIF: %s(): Out of memory\n", __func__);
72                 return NULL;
73         }
74         memset(this, 0, sizeof(struct cfcnfg));
75         this->mux = cfmuxl_create();
76         if (!this->mux)
77                 goto out_of_mem;
78         this->ctrl = cfctrl_create();
79         if (!this->ctrl)
80                 goto out_of_mem;
81         /* Initiate response functions */
82         resp = cfctrl_get_respfuncs(this->ctrl);
83         resp->enum_rsp = cfctrl_enum_resp;
84         resp->linkerror_ind = cfctrl_resp_func;
85         resp->linkdestroy_rsp = cfcnfg_linkdestroy_rsp;
86         resp->sleep_rsp = cfctrl_resp_func;
87         resp->wake_rsp = cfctrl_resp_func;
88         resp->restart_rsp = cfctrl_resp_func;
89         resp->radioset_rsp = cfctrl_resp_func;
90         resp->linksetup_rsp = cfcnfg_linkup_rsp;
91         resp->reject_rsp = cfcnfg_reject_rsp;
92
93         this->last_phyid = 1;
94
95         cfmuxl_set_uplayer(this->mux, this->ctrl, 0);
96         layer_set_dn(this->ctrl, this->mux);
97         layer_set_up(this->ctrl, this);
98         return this;
99 out_of_mem:
100         pr_warning("CAIF: %s(): Out of memory\n", __func__);
101         kfree(this->mux);
102         kfree(this->ctrl);
103         kfree(this);
104         return NULL;
105 }
106 EXPORT_SYMBOL(cfcnfg_create);
107
108 void cfcnfg_remove(struct cfcnfg *cfg)
109 {
110         if (cfg) {
111                 kfree(cfg->mux);
112                 kfree(cfg->ctrl);
113                 kfree(cfg);
114         }
115 }
116
117 static void cfctrl_resp_func(void)
118 {
119 }
120
121 static void cfctrl_enum_resp(void)
122 {
123 }
124
125 struct dev_info *cfcnfg_get_phyid(struct cfcnfg *cnfg,
126                                   enum cfcnfg_phy_preference phy_pref)
127 {
128         u16 i;
129
130         /* Try to match with specified preference */
131         for (i = 1; i < MAX_PHY_LAYERS; i++) {
132                 if (cnfg->phy_layers[i].id == i &&
133                      cnfg->phy_layers[i].pref == phy_pref &&
134                      cnfg->phy_layers[i].frm_layer != NULL) {
135                         caif_assert(cnfg->phy_layers != NULL);
136                         caif_assert(cnfg->phy_layers[i].id == i);
137                         return &cnfg->phy_layers[i].dev_info;
138                 }
139         }
140         /* Otherwise just return something */
141         for (i = 1; i < MAX_PHY_LAYERS; i++) {
142                 if (cnfg->phy_layers[i].id == i) {
143                         caif_assert(cnfg->phy_layers != NULL);
144                         caif_assert(cnfg->phy_layers[i].id == i);
145                         return &cnfg->phy_layers[i].dev_info;
146                 }
147         }
148
149         return NULL;
150 }
151
152 static struct cfcnfg_phyinfo *cfcnfg_get_phyinfo(struct cfcnfg *cnfg,
153                                                         u8 phyid)
154 {
155         int i;
156         /* Try to match with specified preference */
157         for (i = 0; i < MAX_PHY_LAYERS; i++)
158                 if (cnfg->phy_layers[i].frm_layer != NULL &&
159                     cnfg->phy_layers[i].id == phyid)
160                         return &cnfg->phy_layers[i];
161         return NULL;
162 }
163
164 int cfcnfg_get_named(struct cfcnfg *cnfg, char *name)
165 {
166         int i;
167
168         /* Try to match with specified name */
169         for (i = 0; i < MAX_PHY_LAYERS; i++) {
170                 if (cnfg->phy_layers[i].frm_layer != NULL
171                     && strcmp(cnfg->phy_layers[i].phy_layer->name,
172                               name) == 0)
173                         return cnfg->phy_layers[i].frm_layer->id;
174         }
175         return 0;
176 }
177
178 /*
179  * NOTE: What happens on destroy failure:
180  *       1a) No response - Too early
181  *            This will not happen because enumerate has already
182  *            completed.
183  *       1b) No response - FATAL
184  *            Not handled, but this should be a CAIF PROTOCOL ERROR
185  *            Modem error, response is really expected -  this
186  *            case is not really handled.
187  *       2) O/E-bit indicate error
188  *            Ignored - this link is destroyed anyway.
189  *       3) Not able to match on request
190  *            Not handled, but this should be a CAIF PROTOCOL ERROR
191  *       4) Link-Error - (no response)
192  *            Not handled, but this should be a CAIF PROTOCOL ERROR
193  */
194 int cfcnfg_disconn_adapt_layer(struct cfcnfg *cnfg, struct cflayer *adap_layer)
195 {
196         u8 channel_id = 0;
197         int ret = 0;
198         struct cfcnfg_phyinfo *phyinfo = NULL;
199         u8 phyid = 0;
200
201         caif_assert(adap_layer != NULL);
202         channel_id = adap_layer->id;
203         if (channel_id == 0) {
204                 pr_err("CAIF: %s():adap_layer->id is 0\n", __func__);
205                 ret = -ENOTCONN;
206                 goto end;
207         }
208
209         if (adap_layer->dn == NULL) {
210                 pr_err("CAIF: %s():adap_layer->dn is NULL\n", __func__);
211                 ret = -ENODEV;
212                 goto end;
213         }
214
215         if (adap_layer->dn != NULL)
216                 phyid = cfsrvl_getphyid(adap_layer->dn);
217
218         phyinfo = cfcnfg_get_phyinfo(cnfg, phyid);
219         if (phyinfo == NULL) {
220                 pr_warning("CAIF: %s(): No interface to send disconnect to\n",
221                            __func__);
222                 ret = -ENODEV;
223                 goto end;
224         }
225
226         if (phyinfo->id != phyid
227                 || phyinfo->phy_layer->id != phyid
228                 || phyinfo->frm_layer->id != phyid) {
229
230                 pr_err("CAIF: %s(): Inconsistency in phy registration\n",
231                         __func__);
232                 ret = -EINVAL;
233                 goto end;
234         }
235
236         ret = cfctrl_linkdown_req(cnfg->ctrl, channel_id, adap_layer);
237
238 end:
239         if (phyinfo != NULL && --phyinfo->phy_ref_count == 0 &&
240                 phyinfo->phy_layer != NULL &&
241                 phyinfo->phy_layer->modemcmd != NULL) {
242                 phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
243                                              _CAIF_MODEMCMD_PHYIF_USELESS);
244         }
245         return ret;
246
247 }
248 EXPORT_SYMBOL(cfcnfg_disconn_adapt_layer);
249
250 void cfcnfg_release_adap_layer(struct cflayer *adap_layer)
251 {
252         if (adap_layer->dn)
253                 cfsrvl_put(adap_layer->dn);
254 }
255 EXPORT_SYMBOL(cfcnfg_release_adap_layer);
256
257 static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id,
258                                   struct cflayer *client_layer)
259 {
260         struct cfcnfg *cnfg = container_obj(layer);
261         struct cflayer *servl;
262
263         /*
264          * 1) Remove service from the MUX layer. The MUX must
265          *    guarante that no more payload sent "upwards" (receive)
266          */
267         servl = cfmuxl_remove_uplayer(cnfg->mux, channel_id);
268
269         if (servl == NULL) {
270                 pr_err("CAIF: %s(): PROTOCOL ERROR "
271                        "- Error removing service_layer Channel_Id(%d)",
272                         __func__, channel_id);
273                 return;
274         }
275         caif_assert(channel_id == servl->id);
276
277         if (servl != client_layer && servl->up != client_layer) {
278                 pr_err("CAIF: %s(): Error removing service_layer "
279                        "Channel_Id(%d) %p %p",
280                         __func__, channel_id, (void *) servl,
281                         (void *) client_layer);
282                 return;
283         }
284
285         /*
286          * 2) DEINIT_RSP must guarantee that no more packets are transmitted
287          *    from client (adap_layer) when it returns.
288          */
289
290         if (servl->ctrlcmd == NULL) {
291                 pr_err("CAIF: %s(): Error servl->ctrlcmd == NULL", __func__);
292                 return;
293         }
294
295         servl->ctrlcmd(servl, CAIF_CTRLCMD_DEINIT_RSP, 0);
296
297         /* 3) It is now safe to destroy the service layer. */
298         cfservl_destroy(servl);
299 }
300
301 /*
302  * NOTE: What happens on linksetup failure:
303  *       1a) No response - Too early
304  *            This will not happen because enumerate is secured
305  *            before using interface.
306  *       1b) No response - FATAL
307  *            Not handled, but this should be a CAIF PROTOCOL ERROR
308  *            Modem error, response is really expected -  this case is
309  *            not really handled.
310  *       2) O/E-bit indicate error
311  *            Handled in cnfg_reject_rsp
312  *       3) Not able to match on request
313  *            Not handled, but this should be a CAIF PROTOCOL ERROR
314  *       4) Link-Error - (no response)
315  *            Not handled, but this should be a CAIF PROTOCOL ERROR
316  */
317
318 int
319 cfcnfg_add_adaptation_layer(struct cfcnfg *cnfg,
320                                 struct cfctrl_link_param *param,
321                                 struct cflayer *adap_layer)
322 {
323         struct cflayer *frml;
324         if (adap_layer == NULL) {
325                 pr_err("CAIF: %s(): adap_layer is zero", __func__);
326                 return -EINVAL;
327         }
328         if (adap_layer->receive == NULL) {
329                 pr_err("CAIF: %s(): adap_layer->receive is NULL", __func__);
330                 return -EINVAL;
331         }
332         if (adap_layer->ctrlcmd == NULL) {
333                 pr_err("CAIF: %s(): adap_layer->ctrlcmd == NULL", __func__);
334                 return -EINVAL;
335         }
336         frml = cnfg->phy_layers[param->phyid].frm_layer;
337         if (frml == NULL) {
338                 pr_err("CAIF: %s(): Specified PHY type does not exist!",
339                         __func__);
340                 return -ENODEV;
341         }
342         caif_assert(param->phyid == cnfg->phy_layers[param->phyid].id);
343         caif_assert(cnfg->phy_layers[param->phyid].frm_layer->id ==
344                      param->phyid);
345         caif_assert(cnfg->phy_layers[param->phyid].phy_layer->id ==
346                      param->phyid);
347         /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */
348         cfctrl_enum_req(cnfg->ctrl, param->phyid);
349         cfctrl_linkup_request(cnfg->ctrl, param, adap_layer);
350         return 0;
351 }
352 EXPORT_SYMBOL(cfcnfg_add_adaptation_layer);
353
354 static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
355                              struct cflayer *adapt_layer)
356 {
357         if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
358                 adapt_layer->ctrlcmd(adapt_layer,
359                                      CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
360 }
361
362 static void
363 cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv,
364                  u8 phyid, struct cflayer *adapt_layer)
365 {
366         struct cfcnfg *cnfg = container_obj(layer);
367         struct cflayer *servicel = NULL;
368         struct cfcnfg_phyinfo *phyinfo;
369         if (adapt_layer == NULL) {
370                 pr_err("CAIF: %s(): PROTOCOL ERROR "
371                         "- LinkUp Request/Response did not match\n", __func__);
372                 return;
373         }
374
375         caif_assert(cnfg != NULL);
376         caif_assert(phyid != 0);
377         phyinfo = &cnfg->phy_layers[phyid];
378         caif_assert(phyinfo != NULL);
379         caif_assert(phyinfo->id == phyid);
380         caif_assert(phyinfo->phy_layer != NULL);
381         caif_assert(phyinfo->phy_layer->id == phyid);
382
383         if (phyinfo != NULL &&
384             phyinfo->phy_ref_count++ == 0 &&
385             phyinfo->phy_layer != NULL &&
386             phyinfo->phy_layer->modemcmd != NULL) {
387                 caif_assert(phyinfo->phy_layer->id == phyid);
388                 phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
389                                              _CAIF_MODEMCMD_PHYIF_USEFULL);
390
391         }
392         adapt_layer->id = channel_id;
393
394         switch (serv) {
395         case CFCTRL_SRV_VEI:
396                 servicel = cfvei_create(channel_id, &phyinfo->dev_info);
397                 break;
398         case CFCTRL_SRV_DATAGRAM:
399                 servicel = cfdgml_create(channel_id, &phyinfo->dev_info);
400                 break;
401         case CFCTRL_SRV_RFM:
402                 servicel = cfrfml_create(channel_id, &phyinfo->dev_info);
403                 break;
404         case CFCTRL_SRV_UTIL:
405                 servicel = cfutill_create(channel_id, &phyinfo->dev_info);
406                 break;
407         case CFCTRL_SRV_VIDEO:
408                 servicel = cfvidl_create(channel_id, &phyinfo->dev_info);
409                 break;
410         case CFCTRL_SRV_DBG:
411                 servicel = cfdbgl_create(channel_id, &phyinfo->dev_info);
412                 break;
413         default:
414                 pr_err("CAIF: %s(): Protocol error. "
415                         "Link setup response - unknown channel type\n",
416                         __func__);
417                 return;
418         }
419         if (!servicel) {
420                 pr_warning("CAIF: %s(): Out of memory\n", __func__);
421                 return;
422         }
423         layer_set_dn(servicel, cnfg->mux);
424         cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id);
425         layer_set_up(servicel, adapt_layer);
426         layer_set_dn(adapt_layer, servicel);
427         servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0);
428 }
429
430 void
431 cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
432                      void *dev, struct cflayer *phy_layer, u16 *phyid,
433                      enum cfcnfg_phy_preference pref,
434                      bool fcs, bool stx)
435 {
436         struct cflayer *frml;
437         struct cflayer *phy_driver = NULL;
438         int i;
439
440
441         if (cnfg->phy_layers[cnfg->last_phyid].frm_layer == NULL) {
442                 *phyid = cnfg->last_phyid;
443
444                 /* range: * 1..(MAX_PHY_LAYERS-1) */
445                 cnfg->last_phyid =
446                     (cnfg->last_phyid % (MAX_PHY_LAYERS - 1)) + 1;
447         } else {
448                 *phyid = 0;
449                 for (i = 1; i < MAX_PHY_LAYERS; i++) {
450                         if (cnfg->phy_layers[i].frm_layer == NULL) {
451                                 *phyid = i;
452                                 break;
453                         }
454                 }
455         }
456         if (*phyid == 0) {
457                 pr_err("CAIF: %s(): No Available PHY ID\n", __func__);
458                 return;
459         }
460
461         switch (phy_type) {
462         case CFPHYTYPE_FRAG:
463                 phy_driver =
464                     cfserl_create(CFPHYTYPE_FRAG, *phyid, stx);
465                 if (!phy_driver) {
466                         pr_warning("CAIF: %s(): Out of memory\n", __func__);
467                         return;
468                 }
469
470                 break;
471         case CFPHYTYPE_CAIF:
472                 phy_driver = NULL;
473                 break;
474         default:
475                 pr_err("CAIF: %s(): %d", __func__, phy_type);
476                 return;
477                 break;
478         }
479
480         phy_layer->id = *phyid;
481         cnfg->phy_layers[*phyid].pref = pref;
482         cnfg->phy_layers[*phyid].id = *phyid;
483         cnfg->phy_layers[*phyid].dev_info.id = *phyid;
484         cnfg->phy_layers[*phyid].dev_info.dev = dev;
485         cnfg->phy_layers[*phyid].phy_layer = phy_layer;
486         cnfg->phy_layers[*phyid].phy_ref_count = 0;
487         phy_layer->type = phy_type;
488         frml = cffrml_create(*phyid, fcs);
489         if (!frml) {
490                 pr_warning("CAIF: %s(): Out of memory\n", __func__);
491                 return;
492         }
493         cnfg->phy_layers[*phyid].frm_layer = frml;
494         cfmuxl_set_dnlayer(cnfg->mux, frml, *phyid);
495         layer_set_up(frml, cnfg->mux);
496
497         if (phy_driver != NULL) {
498                 phy_driver->id = *phyid;
499                 layer_set_dn(frml, phy_driver);
500                 layer_set_up(phy_driver, frml);
501                 layer_set_dn(phy_driver, phy_layer);
502                 layer_set_up(phy_layer, phy_driver);
503         } else {
504                 layer_set_dn(frml, phy_layer);
505                 layer_set_up(phy_layer, frml);
506         }
507 }
508 EXPORT_SYMBOL(cfcnfg_add_phy_layer);
509
510 int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer)
511 {
512         struct cflayer *frml, *frml_dn;
513         u16 phyid;
514         phyid = phy_layer->id;
515         caif_assert(phyid == cnfg->phy_layers[phyid].id);
516         caif_assert(phy_layer == cnfg->phy_layers[phyid].phy_layer);
517         caif_assert(phy_layer->id == phyid);
518         caif_assert(cnfg->phy_layers[phyid].frm_layer->id == phyid);
519
520         memset(&cnfg->phy_layers[phy_layer->id], 0,
521                sizeof(struct cfcnfg_phyinfo));
522         frml = cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id);
523         frml_dn = frml->dn;
524         cffrml_set_uplayer(frml, NULL);
525         cffrml_set_dnlayer(frml, NULL);
526         kfree(frml);
527
528         if (phy_layer != frml_dn) {
529                 layer_set_up(frml_dn, NULL);
530                 layer_set_dn(frml_dn, NULL);
531                 kfree(frml_dn);
532         }
533         layer_set_up(phy_layer, NULL);
534         return 0;
535 }
536 EXPORT_SYMBOL(cfcnfg_del_phy_layer);