cfctrl.c 16 KB

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