cfcnfg.c 13 KB

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