cfcnfg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. #include <linux/module.h>
  18. #include <asm/atomic.h>
  19. #define MAX_PHY_LAYERS 7
  20. #define PHY_NAME_LEN 20
  21. #define container_obj(layr) container_of(layr, struct cfcnfg, layer)
  22. #define RFM_FRAGMENT_SIZE 4030
  23. /* Information about CAIF physical interfaces held by Config Module in order
  24. * to manage physical interfaces
  25. */
  26. struct cfcnfg_phyinfo {
  27. /* Pointer to the layer below the MUX (framing layer) */
  28. struct cflayer *frm_layer;
  29. /* Pointer to the lowest actual physical layer */
  30. struct cflayer *phy_layer;
  31. /* Unique identifier of the physical interface */
  32. unsigned int id;
  33. /* Preference of the physical in interface */
  34. enum cfcnfg_phy_preference pref;
  35. /* Reference count, number of channels using the device */
  36. int phy_ref_count;
  37. /* Information about the physical device */
  38. struct dev_info dev_info;
  39. };
  40. struct cfcnfg {
  41. struct cflayer layer;
  42. struct cflayer *ctrl;
  43. struct cflayer *mux;
  44. u8 last_phyid;
  45. struct cfcnfg_phyinfo phy_layers[MAX_PHY_LAYERS];
  46. };
  47. static void cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id,
  48. enum cfctrl_srv serv, u8 phyid,
  49. struct cflayer *adapt_layer);
  50. static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id);
  51. static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
  52. struct cflayer *adapt_layer);
  53. static void cfctrl_resp_func(void);
  54. static void cfctrl_enum_resp(void);
  55. struct cfcnfg *cfcnfg_create(void)
  56. {
  57. struct cfcnfg *this;
  58. struct cfctrl_rsp *resp;
  59. /* Initiate this layer */
  60. this = kzalloc(sizeof(struct cfcnfg), GFP_ATOMIC);
  61. if (!this) {
  62. pr_warning("CAIF: %s(): Out of memory\n", __func__);
  63. return NULL;
  64. }
  65. this->mux = cfmuxl_create();
  66. if (!this->mux)
  67. goto out_of_mem;
  68. this->ctrl = cfctrl_create();
  69. if (!this->ctrl)
  70. goto out_of_mem;
  71. /* Initiate response functions */
  72. resp = cfctrl_get_respfuncs(this->ctrl);
  73. resp->enum_rsp = cfctrl_enum_resp;
  74. resp->linkerror_ind = cfctrl_resp_func;
  75. resp->linkdestroy_rsp = cfcnfg_linkdestroy_rsp;
  76. resp->sleep_rsp = cfctrl_resp_func;
  77. resp->wake_rsp = cfctrl_resp_func;
  78. resp->restart_rsp = cfctrl_resp_func;
  79. resp->radioset_rsp = cfctrl_resp_func;
  80. resp->linksetup_rsp = cfcnfg_linkup_rsp;
  81. resp->reject_rsp = cfcnfg_reject_rsp;
  82. this->last_phyid = 1;
  83. cfmuxl_set_uplayer(this->mux, this->ctrl, 0);
  84. layer_set_dn(this->ctrl, this->mux);
  85. layer_set_up(this->ctrl, this);
  86. return this;
  87. out_of_mem:
  88. pr_warning("CAIF: %s(): Out of memory\n", __func__);
  89. kfree(this->mux);
  90. kfree(this->ctrl);
  91. kfree(this);
  92. return NULL;
  93. }
  94. EXPORT_SYMBOL(cfcnfg_create);
  95. void cfcnfg_remove(struct cfcnfg *cfg)
  96. {
  97. if (cfg) {
  98. kfree(cfg->mux);
  99. kfree(cfg->ctrl);
  100. kfree(cfg);
  101. }
  102. }
  103. static void cfctrl_resp_func(void)
  104. {
  105. }
  106. static void cfctrl_enum_resp(void)
  107. {
  108. }
  109. struct dev_info *cfcnfg_get_phyid(struct cfcnfg *cnfg,
  110. enum cfcnfg_phy_preference phy_pref)
  111. {
  112. u16 i;
  113. /* Try to match with specified preference */
  114. for (i = 1; i < MAX_PHY_LAYERS; i++) {
  115. if (cnfg->phy_layers[i].id == i &&
  116. cnfg->phy_layers[i].pref == phy_pref &&
  117. cnfg->phy_layers[i].frm_layer != NULL) {
  118. caif_assert(cnfg->phy_layers != NULL);
  119. caif_assert(cnfg->phy_layers[i].id == i);
  120. return &cnfg->phy_layers[i].dev_info;
  121. }
  122. }
  123. /* Otherwise just return something */
  124. for (i = 1; i < MAX_PHY_LAYERS; i++) {
  125. if (cnfg->phy_layers[i].id == i) {
  126. caif_assert(cnfg->phy_layers != NULL);
  127. caif_assert(cnfg->phy_layers[i].id == i);
  128. return &cnfg->phy_layers[i].dev_info;
  129. }
  130. }
  131. return NULL;
  132. }
  133. static struct cfcnfg_phyinfo *cfcnfg_get_phyinfo(struct cfcnfg *cnfg,
  134. u8 phyid)
  135. {
  136. int i;
  137. /* Try to match with specified preference */
  138. for (i = 0; i < MAX_PHY_LAYERS; i++)
  139. if (cnfg->phy_layers[i].frm_layer != NULL &&
  140. cnfg->phy_layers[i].id == phyid)
  141. return &cnfg->phy_layers[i];
  142. return NULL;
  143. }
  144. int cfcnfg_get_named(struct cfcnfg *cnfg, char *name)
  145. {
  146. int i;
  147. /* Try to match with specified name */
  148. for (i = 0; i < MAX_PHY_LAYERS; i++) {
  149. if (cnfg->phy_layers[i].frm_layer != NULL
  150. && strcmp(cnfg->phy_layers[i].phy_layer->name,
  151. name) == 0)
  152. return cnfg->phy_layers[i].frm_layer->id;
  153. }
  154. return 0;
  155. }
  156. int cfcnfg_disconn_adapt_layer(struct cfcnfg *cnfg, struct cflayer *adap_layer)
  157. {
  158. u8 channel_id = 0;
  159. int ret = 0;
  160. struct cflayer *servl = NULL;
  161. struct cfcnfg_phyinfo *phyinfo = NULL;
  162. u8 phyid = 0;
  163. caif_assert(adap_layer != NULL);
  164. channel_id = adap_layer->id;
  165. if (adap_layer->dn == NULL || channel_id == 0) {
  166. pr_err("CAIF: %s():adap_layer->id is 0\n", __func__);
  167. ret = -ENOTCONN;
  168. goto end;
  169. }
  170. servl = cfmuxl_remove_uplayer(cnfg->mux, channel_id);
  171. if (servl == NULL)
  172. goto end;
  173. layer_set_up(servl, NULL);
  174. ret = cfctrl_linkdown_req(cnfg->ctrl, channel_id, adap_layer);
  175. if (servl == NULL) {
  176. pr_err("CAIF: %s(): PROTOCOL ERROR "
  177. "- Error removing service_layer Channel_Id(%d)",
  178. __func__, channel_id);
  179. ret = -EINVAL;
  180. goto end;
  181. }
  182. caif_assert(channel_id == servl->id);
  183. if (adap_layer->dn != NULL) {
  184. phyid = cfsrvl_getphyid(adap_layer->dn);
  185. phyinfo = cfcnfg_get_phyinfo(cnfg, phyid);
  186. if (phyinfo == NULL) {
  187. pr_warning("CAIF: %s(): "
  188. "No interface to send disconnect to\n",
  189. __func__);
  190. ret = -ENODEV;
  191. goto end;
  192. }
  193. if (phyinfo->id != phyid ||
  194. phyinfo->phy_layer->id != phyid ||
  195. phyinfo->frm_layer->id != phyid) {
  196. pr_err("CAIF: %s(): "
  197. "Inconsistency in phy registration\n",
  198. __func__);
  199. ret = -EINVAL;
  200. goto end;
  201. }
  202. }
  203. if (phyinfo != NULL && --phyinfo->phy_ref_count == 0 &&
  204. phyinfo->phy_layer != NULL &&
  205. phyinfo->phy_layer->modemcmd != NULL) {
  206. phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
  207. _CAIF_MODEMCMD_PHYIF_USELESS);
  208. }
  209. end:
  210. cfsrvl_put(servl);
  211. cfctrl_cancel_req(cnfg->ctrl, adap_layer);
  212. if (adap_layer->ctrlcmd != NULL)
  213. adap_layer->ctrlcmd(adap_layer, CAIF_CTRLCMD_DEINIT_RSP, 0);
  214. return ret;
  215. }
  216. EXPORT_SYMBOL(cfcnfg_disconn_adapt_layer);
  217. void cfcnfg_release_adap_layer(struct cflayer *adap_layer)
  218. {
  219. if (adap_layer->dn)
  220. cfsrvl_put(adap_layer->dn);
  221. }
  222. EXPORT_SYMBOL(cfcnfg_release_adap_layer);
  223. static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id)
  224. {
  225. }
  226. int cfcnfg_add_adaptation_layer(struct cfcnfg *cnfg,
  227. struct cfctrl_link_param *param,
  228. struct cflayer *adap_layer)
  229. {
  230. struct cflayer *frml;
  231. if (adap_layer == NULL) {
  232. pr_err("CAIF: %s(): adap_layer is zero", __func__);
  233. return -EINVAL;
  234. }
  235. if (adap_layer->receive == NULL) {
  236. pr_err("CAIF: %s(): adap_layer->receive is NULL", __func__);
  237. return -EINVAL;
  238. }
  239. if (adap_layer->ctrlcmd == NULL) {
  240. pr_err("CAIF: %s(): adap_layer->ctrlcmd == NULL", __func__);
  241. return -EINVAL;
  242. }
  243. frml = cnfg->phy_layers[param->phyid].frm_layer;
  244. if (frml == NULL) {
  245. pr_err("CAIF: %s(): Specified PHY type does not exist!",
  246. __func__);
  247. return -ENODEV;
  248. }
  249. caif_assert(param->phyid == cnfg->phy_layers[param->phyid].id);
  250. caif_assert(cnfg->phy_layers[param->phyid].frm_layer->id ==
  251. param->phyid);
  252. caif_assert(cnfg->phy_layers[param->phyid].phy_layer->id ==
  253. param->phyid);
  254. /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */
  255. cfctrl_enum_req(cnfg->ctrl, param->phyid);
  256. return cfctrl_linkup_request(cnfg->ctrl, param, adap_layer);
  257. }
  258. EXPORT_SYMBOL(cfcnfg_add_adaptation_layer);
  259. static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
  260. struct cflayer *adapt_layer)
  261. {
  262. if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
  263. adapt_layer->ctrlcmd(adapt_layer,
  264. CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
  265. }
  266. static void
  267. cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv,
  268. u8 phyid, struct cflayer *adapt_layer)
  269. {
  270. struct cfcnfg *cnfg = container_obj(layer);
  271. struct cflayer *servicel = NULL;
  272. struct cfcnfg_phyinfo *phyinfo;
  273. if (adapt_layer == NULL) {
  274. pr_debug("CAIF: %s(): link setup response "
  275. "but no client exist, send linkdown back\n",
  276. __func__);
  277. cfctrl_linkdown_req(cnfg->ctrl, channel_id, NULL);
  278. return;
  279. }
  280. caif_assert(cnfg != NULL);
  281. caif_assert(phyid != 0);
  282. phyinfo = &cnfg->phy_layers[phyid];
  283. caif_assert(phyinfo->id == phyid);
  284. caif_assert(phyinfo->phy_layer != NULL);
  285. caif_assert(phyinfo->phy_layer->id == phyid);
  286. phyinfo->phy_ref_count++;
  287. if (phyinfo->phy_ref_count == 1 &&
  288. phyinfo->phy_layer->modemcmd != NULL) {
  289. phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
  290. _CAIF_MODEMCMD_PHYIF_USEFULL);
  291. }
  292. adapt_layer->id = channel_id;
  293. switch (serv) {
  294. case CFCTRL_SRV_VEI:
  295. servicel = cfvei_create(channel_id, &phyinfo->dev_info);
  296. break;
  297. case CFCTRL_SRV_DATAGRAM:
  298. servicel = cfdgml_create(channel_id, &phyinfo->dev_info);
  299. break;
  300. case CFCTRL_SRV_RFM:
  301. servicel = cfrfml_create(channel_id, &phyinfo->dev_info,
  302. RFM_FRAGMENT_SIZE);
  303. break;
  304. case CFCTRL_SRV_UTIL:
  305. servicel = cfutill_create(channel_id, &phyinfo->dev_info);
  306. break;
  307. case CFCTRL_SRV_VIDEO:
  308. servicel = cfvidl_create(channel_id, &phyinfo->dev_info);
  309. break;
  310. case CFCTRL_SRV_DBG:
  311. servicel = cfdbgl_create(channel_id, &phyinfo->dev_info);
  312. break;
  313. default:
  314. pr_err("CAIF: %s(): Protocol error. "
  315. "Link setup response - unknown channel type\n",
  316. __func__);
  317. return;
  318. }
  319. if (!servicel) {
  320. pr_warning("CAIF: %s(): Out of memory\n", __func__);
  321. return;
  322. }
  323. layer_set_dn(servicel, cnfg->mux);
  324. cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id);
  325. layer_set_up(servicel, adapt_layer);
  326. layer_set_dn(adapt_layer, servicel);
  327. cfsrvl_get(servicel);
  328. servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0);
  329. }
  330. void
  331. cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
  332. void *dev, struct cflayer *phy_layer, u16 *phyid,
  333. enum cfcnfg_phy_preference pref,
  334. bool fcs, bool stx)
  335. {
  336. struct cflayer *frml;
  337. struct cflayer *phy_driver = NULL;
  338. int i;
  339. if (cnfg->phy_layers[cnfg->last_phyid].frm_layer == NULL) {
  340. *phyid = cnfg->last_phyid;
  341. /* range: * 1..(MAX_PHY_LAYERS-1) */
  342. cnfg->last_phyid =
  343. (cnfg->last_phyid % (MAX_PHY_LAYERS - 1)) + 1;
  344. } else {
  345. *phyid = 0;
  346. for (i = 1; i < MAX_PHY_LAYERS; i++) {
  347. if (cnfg->phy_layers[i].frm_layer == NULL) {
  348. *phyid = i;
  349. break;
  350. }
  351. }
  352. }
  353. if (*phyid == 0) {
  354. pr_err("CAIF: %s(): No Available PHY ID\n", __func__);
  355. return;
  356. }
  357. switch (phy_type) {
  358. case CFPHYTYPE_FRAG:
  359. phy_driver =
  360. cfserl_create(CFPHYTYPE_FRAG, *phyid, stx);
  361. if (!phy_driver) {
  362. pr_warning("CAIF: %s(): Out of memory\n", __func__);
  363. return;
  364. }
  365. break;
  366. case CFPHYTYPE_CAIF:
  367. phy_driver = NULL;
  368. break;
  369. default:
  370. pr_err("CAIF: %s(): %d", __func__, phy_type);
  371. return;
  372. break;
  373. }
  374. phy_layer->id = *phyid;
  375. cnfg->phy_layers[*phyid].pref = pref;
  376. cnfg->phy_layers[*phyid].id = *phyid;
  377. cnfg->phy_layers[*phyid].dev_info.id = *phyid;
  378. cnfg->phy_layers[*phyid].dev_info.dev = dev;
  379. cnfg->phy_layers[*phyid].phy_layer = phy_layer;
  380. cnfg->phy_layers[*phyid].phy_ref_count = 0;
  381. phy_layer->type = phy_type;
  382. frml = cffrml_create(*phyid, fcs);
  383. if (!frml) {
  384. pr_warning("CAIF: %s(): Out of memory\n", __func__);
  385. return;
  386. }
  387. cnfg->phy_layers[*phyid].frm_layer = frml;
  388. cfmuxl_set_dnlayer(cnfg->mux, frml, *phyid);
  389. layer_set_up(frml, cnfg->mux);
  390. if (phy_driver != NULL) {
  391. phy_driver->id = *phyid;
  392. layer_set_dn(frml, phy_driver);
  393. layer_set_up(phy_driver, frml);
  394. layer_set_dn(phy_driver, phy_layer);
  395. layer_set_up(phy_layer, phy_driver);
  396. } else {
  397. layer_set_dn(frml, phy_layer);
  398. layer_set_up(phy_layer, frml);
  399. }
  400. }
  401. EXPORT_SYMBOL(cfcnfg_add_phy_layer);
  402. int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer)
  403. {
  404. struct cflayer *frml, *frml_dn;
  405. u16 phyid;
  406. phyid = phy_layer->id;
  407. caif_assert(phyid == cnfg->phy_layers[phyid].id);
  408. caif_assert(phy_layer == cnfg->phy_layers[phyid].phy_layer);
  409. caif_assert(phy_layer->id == phyid);
  410. caif_assert(cnfg->phy_layers[phyid].frm_layer->id == phyid);
  411. memset(&cnfg->phy_layers[phy_layer->id], 0,
  412. sizeof(struct cfcnfg_phyinfo));
  413. frml = cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id);
  414. frml_dn = frml->dn;
  415. cffrml_set_uplayer(frml, NULL);
  416. cffrml_set_dnlayer(frml, NULL);
  417. kfree(frml);
  418. if (phy_layer != frml_dn) {
  419. layer_set_up(frml_dn, NULL);
  420. layer_set_dn(frml_dn, NULL);
  421. kfree(frml_dn);
  422. }
  423. layer_set_up(phy_layer, NULL);
  424. return 0;
  425. }
  426. EXPORT_SYMBOL(cfcnfg_del_phy_layer);