cfctrl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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/stddef.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/slab.h>
  10. #include <net/caif/caif_layer.h>
  11. #include <net/caif/cfpkt.h>
  12. #include <net/caif/cfctrl.h>
  13. #define container_obj(layr) container_of(layr, struct cfctrl, serv.layer)
  14. #define UTILITY_NAME_LENGTH 16
  15. #define CFPKT_CTRL_PKT_LEN 20
  16. #ifdef CAIF_NO_LOOP
  17. static int handle_loop(struct cfctrl *ctrl,
  18. int cmd, struct cfpkt *pkt){
  19. return -1;
  20. }
  21. #else
  22. static int handle_loop(struct cfctrl *ctrl,
  23. int cmd, struct cfpkt *pkt);
  24. #endif
  25. static int cfctrl_recv(struct cflayer *layr, struct cfpkt *pkt);
  26. static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  27. int phyid);
  28. struct cflayer *cfctrl_create(void)
  29. {
  30. struct dev_info dev_info;
  31. struct cfctrl *this =
  32. kzalloc(sizeof(struct cfctrl), GFP_ATOMIC);
  33. if (!this)
  34. return NULL;
  35. caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
  36. memset(&dev_info, 0, sizeof(dev_info));
  37. dev_info.id = 0xff;
  38. cfsrvl_init(&this->serv, 0, &dev_info, false);
  39. atomic_set(&this->req_seq_no, 1);
  40. atomic_set(&this->rsp_seq_no, 1);
  41. this->serv.layer.receive = cfctrl_recv;
  42. sprintf(this->serv.layer.name, "ctrl");
  43. this->serv.layer.ctrlcmd = cfctrl_ctrlcmd;
  44. #ifndef CAIF_NO_LOOP
  45. spin_lock_init(&this->loop_linkid_lock);
  46. this->loop_linkid = 1;
  47. #endif
  48. spin_lock_init(&this->info_list_lock);
  49. INIT_LIST_HEAD(&this->list);
  50. return &this->serv.layer;
  51. }
  52. void cfctrl_remove(struct cflayer *layer)
  53. {
  54. struct cfctrl_request_info *p, *tmp;
  55. struct cfctrl *ctrl = container_obj(layer);
  56. spin_lock_bh(&ctrl->info_list_lock);
  57. list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
  58. list_del(&p->list);
  59. kfree(p);
  60. }
  61. spin_unlock_bh(&ctrl->info_list_lock);
  62. kfree(layer);
  63. }
  64. static bool param_eq(const struct cfctrl_link_param *p1,
  65. const struct cfctrl_link_param *p2)
  66. {
  67. bool eq =
  68. p1->linktype == p2->linktype &&
  69. p1->priority == p2->priority &&
  70. p1->phyid == p2->phyid &&
  71. p1->endpoint == p2->endpoint && p1->chtype == p2->chtype;
  72. if (!eq)
  73. return false;
  74. switch (p1->linktype) {
  75. case CFCTRL_SRV_VEI:
  76. return true;
  77. case CFCTRL_SRV_DATAGRAM:
  78. return p1->u.datagram.connid == p2->u.datagram.connid;
  79. case CFCTRL_SRV_RFM:
  80. return
  81. p1->u.rfm.connid == p2->u.rfm.connid &&
  82. strcmp(p1->u.rfm.volume, p2->u.rfm.volume) == 0;
  83. case CFCTRL_SRV_UTIL:
  84. return
  85. p1->u.utility.fifosize_kb == p2->u.utility.fifosize_kb
  86. && p1->u.utility.fifosize_bufs ==
  87. p2->u.utility.fifosize_bufs
  88. && strcmp(p1->u.utility.name, p2->u.utility.name) == 0
  89. && p1->u.utility.paramlen == p2->u.utility.paramlen
  90. && memcmp(p1->u.utility.params, p2->u.utility.params,
  91. p1->u.utility.paramlen) == 0;
  92. case CFCTRL_SRV_VIDEO:
  93. return p1->u.video.connid == p2->u.video.connid;
  94. case CFCTRL_SRV_DBG:
  95. return true;
  96. case CFCTRL_SRV_DECM:
  97. return false;
  98. default:
  99. return false;
  100. }
  101. return false;
  102. }
  103. static bool cfctrl_req_eq(const struct cfctrl_request_info *r1,
  104. const struct cfctrl_request_info *r2)
  105. {
  106. if (r1->cmd != r2->cmd)
  107. return false;
  108. if (r1->cmd == CFCTRL_CMD_LINK_SETUP)
  109. return param_eq(&r1->param, &r2->param);
  110. else
  111. return r1->channel_id == r2->channel_id;
  112. }
  113. /* Insert request at the end */
  114. static void cfctrl_insert_req(struct cfctrl *ctrl,
  115. struct cfctrl_request_info *req)
  116. {
  117. spin_lock_bh(&ctrl->info_list_lock);
  118. atomic_inc(&ctrl->req_seq_no);
  119. req->sequence_no = atomic_read(&ctrl->req_seq_no);
  120. list_add_tail(&req->list, &ctrl->list);
  121. spin_unlock_bh(&ctrl->info_list_lock);
  122. }
  123. /* Compare and remove request */
  124. static struct cfctrl_request_info *cfctrl_remove_req(struct cfctrl *ctrl,
  125. struct cfctrl_request_info *req)
  126. {
  127. struct cfctrl_request_info *p, *tmp, *first;
  128. first = list_first_entry(&ctrl->list, struct cfctrl_request_info, list);
  129. list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
  130. if (cfctrl_req_eq(req, p)) {
  131. if (p != first)
  132. pr_warn("Requests are not received in order\n");
  133. atomic_set(&ctrl->rsp_seq_no,
  134. p->sequence_no);
  135. list_del(&p->list);
  136. goto out;
  137. }
  138. }
  139. p = NULL;
  140. out:
  141. return p;
  142. }
  143. struct cfctrl_rsp *cfctrl_get_respfuncs(struct cflayer *layer)
  144. {
  145. struct cfctrl *this = container_obj(layer);
  146. return &this->res;
  147. }
  148. static void init_info(struct caif_payload_info *info, struct cfctrl *cfctrl)
  149. {
  150. info->hdr_len = 0;
  151. info->channel_id = cfctrl->serv.layer.id;
  152. info->dev_info = &cfctrl->serv.dev_info;
  153. }
  154. void cfctrl_enum_req(struct cflayer *layer, u8 physlinkid)
  155. {
  156. struct cfctrl *cfctrl = container_obj(layer);
  157. struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
  158. struct cflayer *dn = cfctrl->serv.layer.dn;
  159. if (!pkt)
  160. return;
  161. if (!dn) {
  162. pr_debug("not able to send enum request\n");
  163. return;
  164. }
  165. caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
  166. init_info(cfpkt_info(pkt), cfctrl);
  167. cfpkt_info(pkt)->dev_info->id = physlinkid;
  168. cfctrl->serv.dev_info.id = physlinkid;
  169. cfpkt_addbdy(pkt, CFCTRL_CMD_ENUM);
  170. cfpkt_addbdy(pkt, physlinkid);
  171. dn->transmit(dn, pkt);
  172. }
  173. int cfctrl_linkup_request(struct cflayer *layer,
  174. struct cfctrl_link_param *param,
  175. struct cflayer *user_layer)
  176. {
  177. struct cfctrl *cfctrl = container_obj(layer);
  178. u32 tmp32;
  179. u16 tmp16;
  180. u8 tmp8;
  181. struct cfctrl_request_info *req;
  182. int ret;
  183. char utility_name[16];
  184. struct cfpkt *pkt;
  185. struct cflayer *dn = cfctrl->serv.layer.dn;
  186. if (!dn) {
  187. pr_debug("not able to send linkup request\n");
  188. return -ENODEV;
  189. }
  190. if (cfctrl_cancel_req(layer, user_layer) > 0) {
  191. /* Slight Paranoia, check if already connecting */
  192. pr_err("Duplicate connect request for same client\n");
  193. WARN_ON(1);
  194. return -EALREADY;
  195. }
  196. pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
  197. if (!pkt)
  198. return -ENOMEM;
  199. cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP);
  200. cfpkt_addbdy(pkt, (param->chtype << 4) | param->linktype);
  201. cfpkt_addbdy(pkt, (param->priority << 3) | param->phyid);
  202. cfpkt_addbdy(pkt, param->endpoint & 0x03);
  203. switch (param->linktype) {
  204. case CFCTRL_SRV_VEI:
  205. break;
  206. case CFCTRL_SRV_VIDEO:
  207. cfpkt_addbdy(pkt, (u8) param->u.video.connid);
  208. break;
  209. case CFCTRL_SRV_DBG:
  210. break;
  211. case CFCTRL_SRV_DATAGRAM:
  212. tmp32 = cpu_to_le32(param->u.datagram.connid);
  213. cfpkt_add_body(pkt, &tmp32, 4);
  214. break;
  215. case CFCTRL_SRV_RFM:
  216. /* Construct a frame, convert DatagramConnectionID to network
  217. * format long and copy it out...
  218. */
  219. tmp32 = cpu_to_le32(param->u.rfm.connid);
  220. cfpkt_add_body(pkt, &tmp32, 4);
  221. /* Add volume name, including zero termination... */
  222. cfpkt_add_body(pkt, param->u.rfm.volume,
  223. strlen(param->u.rfm.volume) + 1);
  224. break;
  225. case CFCTRL_SRV_UTIL:
  226. tmp16 = cpu_to_le16(param->u.utility.fifosize_kb);
  227. cfpkt_add_body(pkt, &tmp16, 2);
  228. tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs);
  229. cfpkt_add_body(pkt, &tmp16, 2);
  230. memset(utility_name, 0, sizeof(utility_name));
  231. strncpy(utility_name, param->u.utility.name,
  232. UTILITY_NAME_LENGTH - 1);
  233. cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH);
  234. tmp8 = param->u.utility.paramlen;
  235. cfpkt_add_body(pkt, &tmp8, 1);
  236. cfpkt_add_body(pkt, param->u.utility.params,
  237. param->u.utility.paramlen);
  238. break;
  239. default:
  240. pr_warn("Request setup of bad link type = %d\n",
  241. param->linktype);
  242. return -EINVAL;
  243. }
  244. req = kzalloc(sizeof(*req), GFP_KERNEL);
  245. if (!req)
  246. return -ENOMEM;
  247. req->client_layer = user_layer;
  248. req->cmd = CFCTRL_CMD_LINK_SETUP;
  249. req->param = *param;
  250. cfctrl_insert_req(cfctrl, req);
  251. init_info(cfpkt_info(pkt), cfctrl);
  252. /*
  253. * NOTE:Always send linkup and linkdown request on the same
  254. * device as the payload. Otherwise old queued up payload
  255. * might arrive with the newly allocated channel ID.
  256. */
  257. cfpkt_info(pkt)->dev_info->id = param->phyid;
  258. ret =
  259. dn->transmit(dn, pkt);
  260. if (ret < 0) {
  261. int count;
  262. count = cfctrl_cancel_req(&cfctrl->serv.layer,
  263. user_layer);
  264. if (count != 1) {
  265. pr_err("Could not remove request (%d)", count);
  266. return -ENODEV;
  267. }
  268. }
  269. return 0;
  270. }
  271. int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid,
  272. struct cflayer *client)
  273. {
  274. int ret;
  275. struct cfctrl *cfctrl = container_obj(layer);
  276. struct cfpkt *pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
  277. struct cflayer *dn = cfctrl->serv.layer.dn;
  278. if (!pkt)
  279. return -ENOMEM;
  280. if (!dn) {
  281. pr_debug("not able to send link-down request\n");
  282. return -ENODEV;
  283. }
  284. cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY);
  285. cfpkt_addbdy(pkt, channelid);
  286. init_info(cfpkt_info(pkt), cfctrl);
  287. ret =
  288. dn->transmit(dn, pkt);
  289. #ifndef CAIF_NO_LOOP
  290. cfctrl->loop_linkused[channelid] = 0;
  291. #endif
  292. return ret;
  293. }
  294. int cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer)
  295. {
  296. struct cfctrl_request_info *p, *tmp;
  297. struct cfctrl *ctrl = container_obj(layr);
  298. int found = 0;
  299. spin_lock_bh(&ctrl->info_list_lock);
  300. list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
  301. if (p->client_layer == adap_layer) {
  302. list_del(&p->list);
  303. kfree(p);
  304. found++;
  305. }
  306. }
  307. spin_unlock_bh(&ctrl->info_list_lock);
  308. return found;
  309. }
  310. static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt)
  311. {
  312. u8 cmdrsp;
  313. u8 cmd;
  314. int ret = -1;
  315. u16 tmp16;
  316. u8 len;
  317. u8 param[255];
  318. u8 linkid;
  319. struct cfctrl *cfctrl = container_obj(layer);
  320. struct cfctrl_request_info rsp, *req;
  321. cfpkt_extr_head(pkt, &cmdrsp, 1);
  322. cmd = cmdrsp & CFCTRL_CMD_MASK;
  323. if (cmd != CFCTRL_CMD_LINK_ERR
  324. && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)
  325. && CFCTRL_ERR_BIT != (CFCTRL_ERR_BIT & cmdrsp)) {
  326. if (handle_loop(cfctrl, cmd, pkt) != 0)
  327. cmdrsp |= CFCTRL_ERR_BIT;
  328. }
  329. switch (cmd) {
  330. case CFCTRL_CMD_LINK_SETUP:
  331. {
  332. enum cfctrl_srv serv;
  333. enum cfctrl_srv servtype;
  334. u8 endpoint;
  335. u8 physlinkid;
  336. u8 prio;
  337. u8 tmp;
  338. u32 tmp32;
  339. u8 *cp;
  340. int i;
  341. struct cfctrl_link_param linkparam;
  342. memset(&linkparam, 0, sizeof(linkparam));
  343. cfpkt_extr_head(pkt, &tmp, 1);
  344. serv = tmp & CFCTRL_SRV_MASK;
  345. linkparam.linktype = serv;
  346. servtype = tmp >> 4;
  347. linkparam.chtype = servtype;
  348. cfpkt_extr_head(pkt, &tmp, 1);
  349. physlinkid = tmp & 0x07;
  350. prio = tmp >> 3;
  351. linkparam.priority = prio;
  352. linkparam.phyid = physlinkid;
  353. cfpkt_extr_head(pkt, &endpoint, 1);
  354. linkparam.endpoint = endpoint & 0x03;
  355. switch (serv) {
  356. case CFCTRL_SRV_VEI:
  357. case CFCTRL_SRV_DBG:
  358. if (CFCTRL_ERR_BIT & cmdrsp)
  359. break;
  360. /* Link ID */
  361. cfpkt_extr_head(pkt, &linkid, 1);
  362. break;
  363. case CFCTRL_SRV_VIDEO:
  364. cfpkt_extr_head(pkt, &tmp, 1);
  365. linkparam.u.video.connid = tmp;
  366. if (CFCTRL_ERR_BIT & cmdrsp)
  367. break;
  368. /* Link ID */
  369. cfpkt_extr_head(pkt, &linkid, 1);
  370. break;
  371. case CFCTRL_SRV_DATAGRAM:
  372. cfpkt_extr_head(pkt, &tmp32, 4);
  373. linkparam.u.datagram.connid =
  374. le32_to_cpu(tmp32);
  375. if (CFCTRL_ERR_BIT & cmdrsp)
  376. break;
  377. /* Link ID */
  378. cfpkt_extr_head(pkt, &linkid, 1);
  379. break;
  380. case CFCTRL_SRV_RFM:
  381. /* Construct a frame, convert
  382. * DatagramConnectionID
  383. * to network format long and copy it out...
  384. */
  385. cfpkt_extr_head(pkt, &tmp32, 4);
  386. linkparam.u.rfm.connid =
  387. le32_to_cpu(tmp32);
  388. cp = (u8 *) linkparam.u.rfm.volume;
  389. for (cfpkt_extr_head(pkt, &tmp, 1);
  390. cfpkt_more(pkt) && tmp != '\0';
  391. cfpkt_extr_head(pkt, &tmp, 1))
  392. *cp++ = tmp;
  393. *cp = '\0';
  394. if (CFCTRL_ERR_BIT & cmdrsp)
  395. break;
  396. /* Link ID */
  397. cfpkt_extr_head(pkt, &linkid, 1);
  398. break;
  399. case CFCTRL_SRV_UTIL:
  400. /* Construct a frame, convert
  401. * DatagramConnectionID
  402. * to network format long and copy it out...
  403. */
  404. /* Fifosize KB */
  405. cfpkt_extr_head(pkt, &tmp16, 2);
  406. linkparam.u.utility.fifosize_kb =
  407. le16_to_cpu(tmp16);
  408. /* Fifosize bufs */
  409. cfpkt_extr_head(pkt, &tmp16, 2);
  410. linkparam.u.utility.fifosize_bufs =
  411. le16_to_cpu(tmp16);
  412. /* name */
  413. cp = (u8 *) linkparam.u.utility.name;
  414. caif_assert(sizeof(linkparam.u.utility.name)
  415. >= UTILITY_NAME_LENGTH);
  416. for (i = 0;
  417. i < UTILITY_NAME_LENGTH
  418. && cfpkt_more(pkt); i++) {
  419. cfpkt_extr_head(pkt, &tmp, 1);
  420. *cp++ = tmp;
  421. }
  422. /* Length */
  423. cfpkt_extr_head(pkt, &len, 1);
  424. linkparam.u.utility.paramlen = len;
  425. /* Param Data */
  426. cp = linkparam.u.utility.params;
  427. while (cfpkt_more(pkt) && len--) {
  428. cfpkt_extr_head(pkt, &tmp, 1);
  429. *cp++ = tmp;
  430. }
  431. if (CFCTRL_ERR_BIT & cmdrsp)
  432. break;
  433. /* Link ID */
  434. cfpkt_extr_head(pkt, &linkid, 1);
  435. /* Length */
  436. cfpkt_extr_head(pkt, &len, 1);
  437. /* Param Data */
  438. cfpkt_extr_head(pkt, &param, len);
  439. break;
  440. default:
  441. pr_warn("Request setup, invalid type (%d)\n",
  442. serv);
  443. goto error;
  444. }
  445. rsp.cmd = cmd;
  446. rsp.param = linkparam;
  447. spin_lock_bh(&cfctrl->info_list_lock);
  448. req = cfctrl_remove_req(cfctrl, &rsp);
  449. if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
  450. cfpkt_erroneous(pkt)) {
  451. pr_err("Invalid O/E bit or parse error "
  452. "on CAIF control channel\n");
  453. cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
  454. 0,
  455. req ? req->client_layer
  456. : NULL);
  457. } else {
  458. cfctrl->res.linksetup_rsp(cfctrl->serv.
  459. layer.up, linkid,
  460. serv, physlinkid,
  461. req ? req->
  462. client_layer : NULL);
  463. }
  464. if (req != NULL)
  465. kfree(req);
  466. spin_unlock_bh(&cfctrl->info_list_lock);
  467. }
  468. break;
  469. case CFCTRL_CMD_LINK_DESTROY:
  470. cfpkt_extr_head(pkt, &linkid, 1);
  471. cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
  472. break;
  473. case CFCTRL_CMD_LINK_ERR:
  474. pr_err("Frame Error Indication received\n");
  475. cfctrl->res.linkerror_ind();
  476. break;
  477. case CFCTRL_CMD_ENUM:
  478. cfctrl->res.enum_rsp();
  479. break;
  480. case CFCTRL_CMD_SLEEP:
  481. cfctrl->res.sleep_rsp();
  482. break;
  483. case CFCTRL_CMD_WAKE:
  484. cfctrl->res.wake_rsp();
  485. break;
  486. case CFCTRL_CMD_LINK_RECONF:
  487. cfctrl->res.restart_rsp();
  488. break;
  489. case CFCTRL_CMD_RADIO_SET:
  490. cfctrl->res.radioset_rsp();
  491. break;
  492. default:
  493. pr_err("Unrecognized Control Frame\n");
  494. goto error;
  495. break;
  496. }
  497. ret = 0;
  498. error:
  499. cfpkt_destroy(pkt);
  500. return ret;
  501. }
  502. static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  503. int phyid)
  504. {
  505. struct cfctrl *this = container_obj(layr);
  506. switch (ctrl) {
  507. case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
  508. case CAIF_CTRLCMD_FLOW_OFF_IND:
  509. spin_lock_bh(&this->info_list_lock);
  510. if (!list_empty(&this->list))
  511. pr_debug("Received flow off in control layer\n");
  512. spin_unlock_bh(&this->info_list_lock);
  513. break;
  514. case _CAIF_CTRLCMD_PHYIF_DOWN_IND: {
  515. struct cfctrl_request_info *p, *tmp;
  516. /* Find all connect request and report failure */
  517. spin_lock_bh(&this->info_list_lock);
  518. list_for_each_entry_safe(p, tmp, &this->list, list) {
  519. if (p->param.phyid == phyid) {
  520. list_del(&p->list);
  521. p->client_layer->ctrlcmd(p->client_layer,
  522. CAIF_CTRLCMD_INIT_FAIL_RSP,
  523. phyid);
  524. kfree(p);
  525. }
  526. }
  527. spin_unlock_bh(&this->info_list_lock);
  528. break;
  529. }
  530. default:
  531. break;
  532. }
  533. }
  534. #ifndef CAIF_NO_LOOP
  535. static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
  536. {
  537. static int last_linkid;
  538. static int dec;
  539. u8 linkid, linktype, tmp;
  540. switch (cmd) {
  541. case CFCTRL_CMD_LINK_SETUP:
  542. spin_lock_bh(&ctrl->loop_linkid_lock);
  543. if (!dec) {
  544. for (linkid = last_linkid + 1; linkid < 254; linkid++)
  545. if (!ctrl->loop_linkused[linkid])
  546. goto found;
  547. }
  548. dec = 1;
  549. for (linkid = last_linkid - 1; linkid > 1; linkid--)
  550. if (!ctrl->loop_linkused[linkid])
  551. goto found;
  552. spin_unlock_bh(&ctrl->loop_linkid_lock);
  553. return -1;
  554. found:
  555. if (linkid < 10)
  556. dec = 0;
  557. if (!ctrl->loop_linkused[linkid])
  558. ctrl->loop_linkused[linkid] = 1;
  559. last_linkid = linkid;
  560. cfpkt_add_trail(pkt, &linkid, 1);
  561. spin_unlock_bh(&ctrl->loop_linkid_lock);
  562. cfpkt_peek_head(pkt, &linktype, 1);
  563. if (linktype == CFCTRL_SRV_UTIL) {
  564. tmp = 0x01;
  565. cfpkt_add_trail(pkt, &tmp, 1);
  566. cfpkt_add_trail(pkt, &tmp, 1);
  567. }
  568. break;
  569. case CFCTRL_CMD_LINK_DESTROY:
  570. spin_lock_bh(&ctrl->loop_linkid_lock);
  571. cfpkt_peek_head(pkt, &linkid, 1);
  572. ctrl->loop_linkused[linkid] = 0;
  573. spin_unlock_bh(&ctrl->loop_linkid_lock);
  574. break;
  575. default:
  576. break;
  577. }
  578. return 0;
  579. }
  580. #endif