cfctrl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. strncpy(utility_name, param->u.utility.name,
  235. UTILITY_NAME_LENGTH - 1);
  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. u16 tmp16;
  322. u8 len;
  323. u8 param[255];
  324. u8 linkid;
  325. struct cfctrl *cfctrl = container_obj(layer);
  326. struct cfctrl_request_info rsp, *req;
  327. cfpkt_extr_head(pkt, &cmdrsp, 1);
  328. cmd = cmdrsp & CFCTRL_CMD_MASK;
  329. if (cmd != CFCTRL_CMD_LINK_ERR
  330. && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)
  331. && CFCTRL_ERR_BIT != (CFCTRL_ERR_BIT & cmdrsp)) {
  332. if (handle_loop(cfctrl, cmd, pkt) != 0)
  333. cmdrsp |= CFCTRL_ERR_BIT;
  334. }
  335. switch (cmd) {
  336. case CFCTRL_CMD_LINK_SETUP:
  337. {
  338. enum cfctrl_srv serv;
  339. enum cfctrl_srv servtype;
  340. u8 endpoint;
  341. u8 physlinkid;
  342. u8 prio;
  343. u8 tmp;
  344. u32 tmp32;
  345. u8 *cp;
  346. int i;
  347. struct cfctrl_link_param linkparam;
  348. memset(&linkparam, 0, sizeof(linkparam));
  349. cfpkt_extr_head(pkt, &tmp, 1);
  350. serv = tmp & CFCTRL_SRV_MASK;
  351. linkparam.linktype = serv;
  352. servtype = tmp >> 4;
  353. linkparam.chtype = servtype;
  354. cfpkt_extr_head(pkt, &tmp, 1);
  355. physlinkid = tmp & 0x07;
  356. prio = tmp >> 3;
  357. linkparam.priority = prio;
  358. linkparam.phyid = physlinkid;
  359. cfpkt_extr_head(pkt, &endpoint, 1);
  360. linkparam.endpoint = endpoint & 0x03;
  361. switch (serv) {
  362. case CFCTRL_SRV_VEI:
  363. case CFCTRL_SRV_DBG:
  364. if (CFCTRL_ERR_BIT & cmdrsp)
  365. break;
  366. /* Link ID */
  367. cfpkt_extr_head(pkt, &linkid, 1);
  368. break;
  369. case CFCTRL_SRV_VIDEO:
  370. cfpkt_extr_head(pkt, &tmp, 1);
  371. linkparam.u.video.connid = tmp;
  372. if (CFCTRL_ERR_BIT & cmdrsp)
  373. break;
  374. /* Link ID */
  375. cfpkt_extr_head(pkt, &linkid, 1);
  376. break;
  377. case CFCTRL_SRV_DATAGRAM:
  378. cfpkt_extr_head(pkt, &tmp32, 4);
  379. linkparam.u.datagram.connid =
  380. le32_to_cpu(tmp32);
  381. if (CFCTRL_ERR_BIT & cmdrsp)
  382. break;
  383. /* Link ID */
  384. cfpkt_extr_head(pkt, &linkid, 1);
  385. break;
  386. case CFCTRL_SRV_RFM:
  387. /* Construct a frame, convert
  388. * DatagramConnectionID
  389. * to network format long and copy it out...
  390. */
  391. cfpkt_extr_head(pkt, &tmp32, 4);
  392. linkparam.u.rfm.connid =
  393. le32_to_cpu(tmp32);
  394. cp = (u8 *) linkparam.u.rfm.volume;
  395. for (cfpkt_extr_head(pkt, &tmp, 1);
  396. cfpkt_more(pkt) && tmp != '\0';
  397. cfpkt_extr_head(pkt, &tmp, 1))
  398. *cp++ = tmp;
  399. *cp = '\0';
  400. if (CFCTRL_ERR_BIT & cmdrsp)
  401. break;
  402. /* Link ID */
  403. cfpkt_extr_head(pkt, &linkid, 1);
  404. break;
  405. case CFCTRL_SRV_UTIL:
  406. /* Construct a frame, convert
  407. * DatagramConnectionID
  408. * to network format long and copy it out...
  409. */
  410. /* Fifosize KB */
  411. cfpkt_extr_head(pkt, &tmp16, 2);
  412. linkparam.u.utility.fifosize_kb =
  413. le16_to_cpu(tmp16);
  414. /* Fifosize bufs */
  415. cfpkt_extr_head(pkt, &tmp16, 2);
  416. linkparam.u.utility.fifosize_bufs =
  417. le16_to_cpu(tmp16);
  418. /* name */
  419. cp = (u8 *) linkparam.u.utility.name;
  420. caif_assert(sizeof(linkparam.u.utility.name)
  421. >= UTILITY_NAME_LENGTH);
  422. for (i = 0;
  423. i < UTILITY_NAME_LENGTH
  424. && cfpkt_more(pkt); i++) {
  425. cfpkt_extr_head(pkt, &tmp, 1);
  426. *cp++ = tmp;
  427. }
  428. /* Length */
  429. cfpkt_extr_head(pkt, &len, 1);
  430. linkparam.u.utility.paramlen = len;
  431. /* Param Data */
  432. cp = linkparam.u.utility.params;
  433. while (cfpkt_more(pkt) && len--) {
  434. cfpkt_extr_head(pkt, &tmp, 1);
  435. *cp++ = tmp;
  436. }
  437. if (CFCTRL_ERR_BIT & cmdrsp)
  438. break;
  439. /* Link ID */
  440. cfpkt_extr_head(pkt, &linkid, 1);
  441. /* Length */
  442. cfpkt_extr_head(pkt, &len, 1);
  443. /* Param Data */
  444. cfpkt_extr_head(pkt, &param, len);
  445. break;
  446. default:
  447. pr_warn("Request setup, invalid type (%d)\n",
  448. serv);
  449. goto error;
  450. }
  451. rsp.cmd = cmd;
  452. rsp.param = linkparam;
  453. spin_lock_bh(&cfctrl->info_list_lock);
  454. req = cfctrl_remove_req(cfctrl, &rsp);
  455. if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
  456. cfpkt_erroneous(pkt)) {
  457. pr_err("Invalid O/E bit or parse error "
  458. "on CAIF control channel\n");
  459. cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
  460. 0,
  461. req ? req->client_layer
  462. : NULL);
  463. } else {
  464. cfctrl->res.linksetup_rsp(cfctrl->serv.
  465. layer.up, linkid,
  466. serv, physlinkid,
  467. req ? req->
  468. client_layer : NULL);
  469. }
  470. kfree(req);
  471. spin_unlock_bh(&cfctrl->info_list_lock);
  472. }
  473. break;
  474. case CFCTRL_CMD_LINK_DESTROY:
  475. cfpkt_extr_head(pkt, &linkid, 1);
  476. cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
  477. break;
  478. case CFCTRL_CMD_LINK_ERR:
  479. pr_err("Frame Error Indication received\n");
  480. cfctrl->res.linkerror_ind();
  481. break;
  482. case CFCTRL_CMD_ENUM:
  483. cfctrl->res.enum_rsp();
  484. break;
  485. case CFCTRL_CMD_SLEEP:
  486. cfctrl->res.sleep_rsp();
  487. break;
  488. case CFCTRL_CMD_WAKE:
  489. cfctrl->res.wake_rsp();
  490. break;
  491. case CFCTRL_CMD_LINK_RECONF:
  492. cfctrl->res.restart_rsp();
  493. break;
  494. case CFCTRL_CMD_RADIO_SET:
  495. cfctrl->res.radioset_rsp();
  496. break;
  497. default:
  498. pr_err("Unrecognized Control Frame\n");
  499. goto error;
  500. }
  501. ret = 0;
  502. error:
  503. cfpkt_destroy(pkt);
  504. return ret;
  505. }
  506. static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  507. int phyid)
  508. {
  509. struct cfctrl *this = container_obj(layr);
  510. switch (ctrl) {
  511. case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
  512. case CAIF_CTRLCMD_FLOW_OFF_IND:
  513. spin_lock_bh(&this->info_list_lock);
  514. if (!list_empty(&this->list))
  515. pr_debug("Received flow off in control layer\n");
  516. spin_unlock_bh(&this->info_list_lock);
  517. break;
  518. case _CAIF_CTRLCMD_PHYIF_DOWN_IND: {
  519. struct cfctrl_request_info *p, *tmp;
  520. /* Find all connect request and report failure */
  521. spin_lock_bh(&this->info_list_lock);
  522. list_for_each_entry_safe(p, tmp, &this->list, list) {
  523. if (p->param.phyid == phyid) {
  524. list_del(&p->list);
  525. p->client_layer->ctrlcmd(p->client_layer,
  526. CAIF_CTRLCMD_INIT_FAIL_RSP,
  527. phyid);
  528. kfree(p);
  529. }
  530. }
  531. spin_unlock_bh(&this->info_list_lock);
  532. break;
  533. }
  534. default:
  535. break;
  536. }
  537. }
  538. #ifndef CAIF_NO_LOOP
  539. static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
  540. {
  541. static int last_linkid;
  542. static int dec;
  543. u8 linkid, linktype, tmp;
  544. switch (cmd) {
  545. case CFCTRL_CMD_LINK_SETUP:
  546. spin_lock_bh(&ctrl->loop_linkid_lock);
  547. if (!dec) {
  548. for (linkid = last_linkid + 1; linkid < 254; linkid++)
  549. if (!ctrl->loop_linkused[linkid])
  550. goto found;
  551. }
  552. dec = 1;
  553. for (linkid = last_linkid - 1; linkid > 1; linkid--)
  554. if (!ctrl->loop_linkused[linkid])
  555. goto found;
  556. spin_unlock_bh(&ctrl->loop_linkid_lock);
  557. return -1;
  558. found:
  559. if (linkid < 10)
  560. dec = 0;
  561. if (!ctrl->loop_linkused[linkid])
  562. ctrl->loop_linkused[linkid] = 1;
  563. last_linkid = linkid;
  564. cfpkt_add_trail(pkt, &linkid, 1);
  565. spin_unlock_bh(&ctrl->loop_linkid_lock);
  566. cfpkt_peek_head(pkt, &linktype, 1);
  567. if (linktype == CFCTRL_SRV_UTIL) {
  568. tmp = 0x01;
  569. cfpkt_add_trail(pkt, &tmp, 1);
  570. cfpkt_add_trail(pkt, &tmp, 1);
  571. }
  572. break;
  573. case CFCTRL_CMD_LINK_DESTROY:
  574. spin_lock_bh(&ctrl->loop_linkid_lock);
  575. cfpkt_peek_head(pkt, &linkid, 1);
  576. ctrl->loop_linkused[linkid] = 0;
  577. spin_unlock_bh(&ctrl->loop_linkid_lock);
  578. break;
  579. default:
  580. break;
  581. }
  582. return 0;
  583. }
  584. #endif