cfcnfg.c 13 KB

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