llc_shdlc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * shdlc Link Layer Control
  3. *
  4. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
  19. #include <linux/types.h>
  20. #include <linux/sched.h>
  21. #include <linux/wait.h>
  22. #include <linux/slab.h>
  23. #include <linux/skbuff.h>
  24. #include "llc.h"
  25. enum shdlc_state {
  26. SHDLC_DISCONNECTED = 0,
  27. SHDLC_CONNECTING = 1,
  28. SHDLC_NEGOTIATING = 2,
  29. SHDLC_HALF_CONNECTED = 3,
  30. SHDLC_CONNECTED = 4
  31. };
  32. struct llc_shdlc {
  33. struct nfc_hci_dev *hdev;
  34. xmit_to_drv_t xmit_to_drv;
  35. rcv_to_hci_t rcv_to_hci;
  36. struct mutex state_mutex;
  37. enum shdlc_state state;
  38. int hard_fault;
  39. wait_queue_head_t *connect_wq;
  40. int connect_tries;
  41. int connect_result;
  42. struct timer_list connect_timer;/* aka T3 in spec 10.6.1 */
  43. u8 w; /* window size */
  44. bool srej_support;
  45. struct timer_list t1_timer; /* send ack timeout */
  46. bool t1_active;
  47. struct timer_list t2_timer; /* guard/retransmit timeout */
  48. bool t2_active;
  49. int ns; /* next seq num for send */
  50. int nr; /* next expected seq num for receive */
  51. int dnr; /* oldest sent unacked seq num */
  52. struct sk_buff_head rcv_q;
  53. struct sk_buff_head send_q;
  54. bool rnr; /* other side is not ready to receive */
  55. struct sk_buff_head ack_pending_q;
  56. struct work_struct sm_work;
  57. int tx_headroom;
  58. int tx_tailroom;
  59. llc_failure_t llc_failure;
  60. };
  61. #define SHDLC_LLC_HEAD_ROOM 2
  62. #define SHDLC_MAX_WINDOW 4
  63. #define SHDLC_SREJ_SUPPORT false
  64. #define SHDLC_CONTROL_HEAD_MASK 0xe0
  65. #define SHDLC_CONTROL_HEAD_I 0x80
  66. #define SHDLC_CONTROL_HEAD_I2 0xa0
  67. #define SHDLC_CONTROL_HEAD_S 0xc0
  68. #define SHDLC_CONTROL_HEAD_U 0xe0
  69. #define SHDLC_CONTROL_NS_MASK 0x38
  70. #define SHDLC_CONTROL_NR_MASK 0x07
  71. #define SHDLC_CONTROL_TYPE_MASK 0x18
  72. #define SHDLC_CONTROL_M_MASK 0x1f
  73. enum sframe_type {
  74. S_FRAME_RR = 0x00,
  75. S_FRAME_REJ = 0x01,
  76. S_FRAME_RNR = 0x02,
  77. S_FRAME_SREJ = 0x03
  78. };
  79. enum uframe_modifier {
  80. U_FRAME_UA = 0x06,
  81. U_FRAME_RSET = 0x19
  82. };
  83. #define SHDLC_CONNECT_VALUE_MS 5
  84. #define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
  85. #define SHDLC_T2_VALUE_MS 300
  86. #define SHDLC_DUMP_SKB(info, skb) \
  87. do { \
  88. pr_debug("%s:\n", info); \
  89. print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
  90. 16, 1, skb->data, skb->len, 0); \
  91. } while (0)
  92. /* checks x < y <= z modulo 8 */
  93. static bool llc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
  94. {
  95. if (x < z)
  96. return ((x < y) && (y <= z)) ? true : false;
  97. else
  98. return ((y > x) || (y <= z)) ? true : false;
  99. }
  100. /* checks x <= y < z modulo 8 */
  101. static bool llc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
  102. {
  103. if (x <= z)
  104. return ((x <= y) && (y < z)) ? true : false;
  105. else /* x > z -> z+8 > x */
  106. return ((y >= x) || (y < z)) ? true : false;
  107. }
  108. static struct sk_buff *llc_shdlc_alloc_skb(struct llc_shdlc *shdlc,
  109. int payload_len)
  110. {
  111. struct sk_buff *skb;
  112. skb = alloc_skb(shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM +
  113. shdlc->tx_tailroom + payload_len, GFP_KERNEL);
  114. if (skb)
  115. skb_reserve(skb, shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM);
  116. return skb;
  117. }
  118. /* immediately sends an S frame. */
  119. static int llc_shdlc_send_s_frame(struct llc_shdlc *shdlc,
  120. enum sframe_type sframe_type, int nr)
  121. {
  122. int r;
  123. struct sk_buff *skb;
  124. pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
  125. skb = llc_shdlc_alloc_skb(shdlc, 0);
  126. if (skb == NULL)
  127. return -ENOMEM;
  128. *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
  129. r = shdlc->xmit_to_drv(shdlc->hdev, skb);
  130. kfree_skb(skb);
  131. return r;
  132. }
  133. /* immediately sends an U frame. skb may contain optional payload */
  134. static int llc_shdlc_send_u_frame(struct llc_shdlc *shdlc,
  135. struct sk_buff *skb,
  136. enum uframe_modifier uframe_modifier)
  137. {
  138. int r;
  139. pr_debug("uframe_modifier=%d\n", uframe_modifier);
  140. *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
  141. r = shdlc->xmit_to_drv(shdlc->hdev, skb);
  142. kfree_skb(skb);
  143. return r;
  144. }
  145. /*
  146. * Free ack_pending frames until y_nr - 1, and reset t2 according to
  147. * the remaining oldest ack_pending frame sent time
  148. */
  149. static void llc_shdlc_reset_t2(struct llc_shdlc *shdlc, int y_nr)
  150. {
  151. struct sk_buff *skb;
  152. int dnr = shdlc->dnr; /* MUST initially be < y_nr */
  153. pr_debug("release ack pending up to frame %d excluded\n", y_nr);
  154. while (dnr != y_nr) {
  155. pr_debug("release ack pending frame %d\n", dnr);
  156. skb = skb_dequeue(&shdlc->ack_pending_q);
  157. kfree_skb(skb);
  158. dnr = (dnr + 1) % 8;
  159. }
  160. if (skb_queue_empty(&shdlc->ack_pending_q)) {
  161. if (shdlc->t2_active) {
  162. del_timer_sync(&shdlc->t2_timer);
  163. shdlc->t2_active = false;
  164. pr_debug
  165. ("All sent frames acked. Stopped T2(retransmit)\n");
  166. }
  167. } else {
  168. skb = skb_peek(&shdlc->ack_pending_q);
  169. mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
  170. msecs_to_jiffies(SHDLC_T2_VALUE_MS));
  171. shdlc->t2_active = true;
  172. pr_debug
  173. ("Start T2(retransmit) for remaining unacked sent frames\n");
  174. }
  175. }
  176. /*
  177. * Receive validated frames from lower layer. skb contains HCI payload only.
  178. * Handle according to algorithm at spec:10.8.2
  179. */
  180. static void llc_shdlc_rcv_i_frame(struct llc_shdlc *shdlc,
  181. struct sk_buff *skb, int ns, int nr)
  182. {
  183. int x_ns = ns;
  184. int y_nr = nr;
  185. pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
  186. if (shdlc->state != SHDLC_CONNECTED)
  187. goto exit;
  188. if (x_ns != shdlc->nr) {
  189. llc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
  190. goto exit;
  191. }
  192. if (shdlc->t1_active == false) {
  193. shdlc->t1_active = true;
  194. mod_timer(&shdlc->t1_timer, jiffies +
  195. msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
  196. pr_debug("(re)Start T1(send ack)\n");
  197. }
  198. if (skb->len) {
  199. shdlc->rcv_to_hci(shdlc->hdev, skb);
  200. skb = NULL;
  201. }
  202. shdlc->nr = (shdlc->nr + 1) % 8;
  203. if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
  204. llc_shdlc_reset_t2(shdlc, y_nr);
  205. shdlc->dnr = y_nr;
  206. }
  207. exit:
  208. kfree_skb(skb);
  209. }
  210. static void llc_shdlc_rcv_ack(struct llc_shdlc *shdlc, int y_nr)
  211. {
  212. pr_debug("remote acked up to frame %d excluded\n", y_nr);
  213. if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
  214. llc_shdlc_reset_t2(shdlc, y_nr);
  215. shdlc->dnr = y_nr;
  216. }
  217. }
  218. static void llc_shdlc_requeue_ack_pending(struct llc_shdlc *shdlc)
  219. {
  220. struct sk_buff *skb;
  221. pr_debug("ns reset to %d\n", shdlc->dnr);
  222. while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
  223. skb_pull(skb, 1); /* remove control field */
  224. skb_queue_head(&shdlc->send_q, skb);
  225. }
  226. shdlc->ns = shdlc->dnr;
  227. }
  228. static void llc_shdlc_rcv_rej(struct llc_shdlc *shdlc, int y_nr)
  229. {
  230. struct sk_buff *skb;
  231. pr_debug("remote asks retransmission from frame %d\n", y_nr);
  232. if (llc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
  233. if (shdlc->t2_active) {
  234. del_timer_sync(&shdlc->t2_timer);
  235. shdlc->t2_active = false;
  236. pr_debug("Stopped T2(retransmit)\n");
  237. }
  238. if (shdlc->dnr != y_nr) {
  239. while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
  240. skb = skb_dequeue(&shdlc->ack_pending_q);
  241. kfree_skb(skb);
  242. }
  243. }
  244. llc_shdlc_requeue_ack_pending(shdlc);
  245. }
  246. }
  247. /* See spec RR:10.8.3 REJ:10.8.4 */
  248. static void llc_shdlc_rcv_s_frame(struct llc_shdlc *shdlc,
  249. enum sframe_type s_frame_type, int nr)
  250. {
  251. struct sk_buff *skb;
  252. if (shdlc->state != SHDLC_CONNECTED)
  253. return;
  254. switch (s_frame_type) {
  255. case S_FRAME_RR:
  256. llc_shdlc_rcv_ack(shdlc, nr);
  257. if (shdlc->rnr == true) { /* see SHDLC 10.7.7 */
  258. shdlc->rnr = false;
  259. if (shdlc->send_q.qlen == 0) {
  260. skb = llc_shdlc_alloc_skb(shdlc, 0);
  261. if (skb)
  262. skb_queue_tail(&shdlc->send_q, skb);
  263. }
  264. }
  265. break;
  266. case S_FRAME_REJ:
  267. llc_shdlc_rcv_rej(shdlc, nr);
  268. break;
  269. case S_FRAME_RNR:
  270. llc_shdlc_rcv_ack(shdlc, nr);
  271. shdlc->rnr = true;
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. static void llc_shdlc_connect_complete(struct llc_shdlc *shdlc, int r)
  278. {
  279. pr_debug("result=%d\n", r);
  280. del_timer_sync(&shdlc->connect_timer);
  281. if (r == 0) {
  282. shdlc->ns = 0;
  283. shdlc->nr = 0;
  284. shdlc->dnr = 0;
  285. shdlc->state = SHDLC_HALF_CONNECTED;
  286. } else {
  287. shdlc->state = SHDLC_DISCONNECTED;
  288. }
  289. shdlc->connect_result = r;
  290. wake_up(shdlc->connect_wq);
  291. }
  292. static int llc_shdlc_connect_initiate(struct llc_shdlc *shdlc)
  293. {
  294. struct sk_buff *skb;
  295. pr_debug("\n");
  296. skb = llc_shdlc_alloc_skb(shdlc, 2);
  297. if (skb == NULL)
  298. return -ENOMEM;
  299. *skb_put(skb, 1) = SHDLC_MAX_WINDOW;
  300. *skb_put(skb, 1) = SHDLC_SREJ_SUPPORT ? 1 : 0;
  301. return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
  302. }
  303. static int llc_shdlc_connect_send_ua(struct llc_shdlc *shdlc)
  304. {
  305. struct sk_buff *skb;
  306. pr_debug("\n");
  307. skb = llc_shdlc_alloc_skb(shdlc, 0);
  308. if (skb == NULL)
  309. return -ENOMEM;
  310. return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
  311. }
  312. static void llc_shdlc_rcv_u_frame(struct llc_shdlc *shdlc,
  313. struct sk_buff *skb,
  314. enum uframe_modifier u_frame_modifier)
  315. {
  316. u8 w = SHDLC_MAX_WINDOW;
  317. bool srej_support = SHDLC_SREJ_SUPPORT;
  318. int r;
  319. pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
  320. switch (u_frame_modifier) {
  321. case U_FRAME_RSET:
  322. switch (shdlc->state) {
  323. case SHDLC_NEGOTIATING:
  324. case SHDLC_CONNECTING:
  325. /*
  326. * We sent RSET, but chip wants to negociate or we
  327. * got RSET before we managed to send out our.
  328. */
  329. if (skb->len > 0)
  330. w = skb->data[0];
  331. if (skb->len > 1)
  332. srej_support = skb->data[1] & 0x01 ? true :
  333. false;
  334. if ((w <= SHDLC_MAX_WINDOW) &&
  335. (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
  336. shdlc->w = w;
  337. shdlc->srej_support = srej_support;
  338. r = llc_shdlc_connect_send_ua(shdlc);
  339. llc_shdlc_connect_complete(shdlc, r);
  340. }
  341. break;
  342. case SHDLC_HALF_CONNECTED:
  343. /*
  344. * Chip resent RSET due to its timeout - Ignote it
  345. * as we already sent UA.
  346. */
  347. break;
  348. case SHDLC_CONNECTED:
  349. /*
  350. * Chip wants to reset link. This is unexpected and
  351. * unsupported.
  352. */
  353. shdlc->hard_fault = -ECONNRESET;
  354. break;
  355. default:
  356. break;
  357. }
  358. break;
  359. case U_FRAME_UA:
  360. if ((shdlc->state == SHDLC_CONNECTING &&
  361. shdlc->connect_tries > 0) ||
  362. (shdlc->state == SHDLC_NEGOTIATING)) {
  363. llc_shdlc_connect_complete(shdlc, 0);
  364. shdlc->state = SHDLC_CONNECTED;
  365. }
  366. break;
  367. default:
  368. break;
  369. }
  370. kfree_skb(skb);
  371. }
  372. static void llc_shdlc_handle_rcv_queue(struct llc_shdlc *shdlc)
  373. {
  374. struct sk_buff *skb;
  375. u8 control;
  376. int nr;
  377. int ns;
  378. enum sframe_type s_frame_type;
  379. enum uframe_modifier u_frame_modifier;
  380. if (shdlc->rcv_q.qlen)
  381. pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
  382. while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
  383. control = skb->data[0];
  384. skb_pull(skb, 1);
  385. switch (control & SHDLC_CONTROL_HEAD_MASK) {
  386. case SHDLC_CONTROL_HEAD_I:
  387. case SHDLC_CONTROL_HEAD_I2:
  388. if (shdlc->state == SHDLC_HALF_CONNECTED)
  389. shdlc->state = SHDLC_CONNECTED;
  390. ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
  391. nr = control & SHDLC_CONTROL_NR_MASK;
  392. llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
  393. break;
  394. case SHDLC_CONTROL_HEAD_S:
  395. if (shdlc->state == SHDLC_HALF_CONNECTED)
  396. shdlc->state = SHDLC_CONNECTED;
  397. s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
  398. nr = control & SHDLC_CONTROL_NR_MASK;
  399. llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
  400. kfree_skb(skb);
  401. break;
  402. case SHDLC_CONTROL_HEAD_U:
  403. u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
  404. llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
  405. break;
  406. default:
  407. pr_err("UNKNOWN Control=%d\n", control);
  408. kfree_skb(skb);
  409. break;
  410. }
  411. }
  412. }
  413. static int llc_shdlc_w_used(int ns, int dnr)
  414. {
  415. int unack_count;
  416. if (dnr <= ns)
  417. unack_count = ns - dnr;
  418. else
  419. unack_count = 8 - dnr + ns;
  420. return unack_count;
  421. }
  422. /* Send frames according to algorithm at spec:10.8.1 */
  423. static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
  424. {
  425. struct sk_buff *skb;
  426. int r;
  427. unsigned long time_sent;
  428. if (shdlc->send_q.qlen)
  429. pr_debug
  430. ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
  431. shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
  432. shdlc->rnr == false ? "false" : "true",
  433. shdlc->w - llc_shdlc_w_used(shdlc->ns, shdlc->dnr),
  434. shdlc->ack_pending_q.qlen);
  435. while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
  436. (shdlc->rnr == false)) {
  437. if (shdlc->t1_active) {
  438. del_timer_sync(&shdlc->t1_timer);
  439. shdlc->t1_active = false;
  440. pr_debug("Stopped T1(send ack)\n");
  441. }
  442. skb = skb_dequeue(&shdlc->send_q);
  443. *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
  444. shdlc->nr;
  445. pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
  446. shdlc->nr);
  447. SHDLC_DUMP_SKB("shdlc frame written", skb);
  448. r = shdlc->xmit_to_drv(shdlc->hdev, skb);
  449. if (r < 0) {
  450. shdlc->hard_fault = r;
  451. break;
  452. }
  453. shdlc->ns = (shdlc->ns + 1) % 8;
  454. time_sent = jiffies;
  455. *(unsigned long *)skb->cb = time_sent;
  456. skb_queue_tail(&shdlc->ack_pending_q, skb);
  457. if (shdlc->t2_active == false) {
  458. shdlc->t2_active = true;
  459. mod_timer(&shdlc->t2_timer, time_sent +
  460. msecs_to_jiffies(SHDLC_T2_VALUE_MS));
  461. pr_debug("Started T2 (retransmit)\n");
  462. }
  463. }
  464. }
  465. static void llc_shdlc_connect_timeout(unsigned long data)
  466. {
  467. struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
  468. pr_debug("\n");
  469. schedule_work(&shdlc->sm_work);
  470. }
  471. static void llc_shdlc_t1_timeout(unsigned long data)
  472. {
  473. struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
  474. pr_debug("SoftIRQ: need to send ack\n");
  475. schedule_work(&shdlc->sm_work);
  476. }
  477. static void llc_shdlc_t2_timeout(unsigned long data)
  478. {
  479. struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
  480. pr_debug("SoftIRQ: need to retransmit\n");
  481. schedule_work(&shdlc->sm_work);
  482. }
  483. static void llc_shdlc_sm_work(struct work_struct *work)
  484. {
  485. struct llc_shdlc *shdlc = container_of(work, struct llc_shdlc, sm_work);
  486. int r;
  487. pr_debug("\n");
  488. mutex_lock(&shdlc->state_mutex);
  489. switch (shdlc->state) {
  490. case SHDLC_DISCONNECTED:
  491. skb_queue_purge(&shdlc->rcv_q);
  492. skb_queue_purge(&shdlc->send_q);
  493. skb_queue_purge(&shdlc->ack_pending_q);
  494. break;
  495. case SHDLC_CONNECTING:
  496. if (shdlc->hard_fault) {
  497. llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
  498. break;
  499. }
  500. if (shdlc->connect_tries++ < 5)
  501. r = llc_shdlc_connect_initiate(shdlc);
  502. else
  503. r = -ETIME;
  504. if (r < 0) {
  505. llc_shdlc_connect_complete(shdlc, r);
  506. } else {
  507. mod_timer(&shdlc->connect_timer, jiffies +
  508. msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
  509. shdlc->state = SHDLC_NEGOTIATING;
  510. }
  511. break;
  512. case SHDLC_NEGOTIATING:
  513. if (timer_pending(&shdlc->connect_timer) == 0) {
  514. shdlc->state = SHDLC_CONNECTING;
  515. schedule_work(&shdlc->sm_work);
  516. }
  517. llc_shdlc_handle_rcv_queue(shdlc);
  518. if (shdlc->hard_fault) {
  519. llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
  520. break;
  521. }
  522. break;
  523. case SHDLC_HALF_CONNECTED:
  524. case SHDLC_CONNECTED:
  525. llc_shdlc_handle_rcv_queue(shdlc);
  526. llc_shdlc_handle_send_queue(shdlc);
  527. if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
  528. pr_debug
  529. ("Handle T1(send ack) elapsed (T1 now inactive)\n");
  530. shdlc->t1_active = false;
  531. r = llc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
  532. shdlc->nr);
  533. if (r < 0)
  534. shdlc->hard_fault = r;
  535. }
  536. if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
  537. pr_debug
  538. ("Handle T2(retransmit) elapsed (T2 inactive)\n");
  539. shdlc->t2_active = false;
  540. llc_shdlc_requeue_ack_pending(shdlc);
  541. llc_shdlc_handle_send_queue(shdlc);
  542. }
  543. if (shdlc->hard_fault)
  544. shdlc->llc_failure(shdlc->hdev, shdlc->hard_fault);
  545. break;
  546. default:
  547. break;
  548. }
  549. mutex_unlock(&shdlc->state_mutex);
  550. }
  551. /*
  552. * Called from syscall context to establish shdlc link. Sleeps until
  553. * link is ready or failure.
  554. */
  555. static int llc_shdlc_connect(struct llc_shdlc *shdlc)
  556. {
  557. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
  558. pr_debug("\n");
  559. mutex_lock(&shdlc->state_mutex);
  560. shdlc->state = SHDLC_CONNECTING;
  561. shdlc->connect_wq = &connect_wq;
  562. shdlc->connect_tries = 0;
  563. shdlc->connect_result = 1;
  564. mutex_unlock(&shdlc->state_mutex);
  565. schedule_work(&shdlc->sm_work);
  566. wait_event(connect_wq, shdlc->connect_result != 1);
  567. return shdlc->connect_result;
  568. }
  569. static void llc_shdlc_disconnect(struct llc_shdlc *shdlc)
  570. {
  571. pr_debug("\n");
  572. mutex_lock(&shdlc->state_mutex);
  573. shdlc->state = SHDLC_DISCONNECTED;
  574. mutex_unlock(&shdlc->state_mutex);
  575. schedule_work(&shdlc->sm_work);
  576. }
  577. /*
  578. * Receive an incoming shdlc frame. Frame has already been crc-validated.
  579. * skb contains only LLC header and payload.
  580. * If skb == NULL, it is a notification that the link below is dead.
  581. */
  582. static void llc_shdlc_recv_frame(struct llc_shdlc *shdlc, struct sk_buff *skb)
  583. {
  584. if (skb == NULL) {
  585. pr_err("NULL Frame -> link is dead\n");
  586. shdlc->hard_fault = -EREMOTEIO;
  587. } else {
  588. SHDLC_DUMP_SKB("incoming frame", skb);
  589. skb_queue_tail(&shdlc->rcv_q, skb);
  590. }
  591. schedule_work(&shdlc->sm_work);
  592. }
  593. static void *llc_shdlc_init(struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
  594. rcv_to_hci_t rcv_to_hci, int tx_headroom,
  595. int tx_tailroom, int *rx_headroom, int *rx_tailroom,
  596. llc_failure_t llc_failure)
  597. {
  598. struct llc_shdlc *shdlc;
  599. *rx_headroom = SHDLC_LLC_HEAD_ROOM;
  600. *rx_tailroom = 0;
  601. shdlc = kzalloc(sizeof(struct llc_shdlc), GFP_KERNEL);
  602. if (shdlc == NULL)
  603. return NULL;
  604. mutex_init(&shdlc->state_mutex);
  605. shdlc->state = SHDLC_DISCONNECTED;
  606. init_timer(&shdlc->connect_timer);
  607. shdlc->connect_timer.data = (unsigned long)shdlc;
  608. shdlc->connect_timer.function = llc_shdlc_connect_timeout;
  609. init_timer(&shdlc->t1_timer);
  610. shdlc->t1_timer.data = (unsigned long)shdlc;
  611. shdlc->t1_timer.function = llc_shdlc_t1_timeout;
  612. init_timer(&shdlc->t2_timer);
  613. shdlc->t2_timer.data = (unsigned long)shdlc;
  614. shdlc->t2_timer.function = llc_shdlc_t2_timeout;
  615. shdlc->w = SHDLC_MAX_WINDOW;
  616. shdlc->srej_support = SHDLC_SREJ_SUPPORT;
  617. skb_queue_head_init(&shdlc->rcv_q);
  618. skb_queue_head_init(&shdlc->send_q);
  619. skb_queue_head_init(&shdlc->ack_pending_q);
  620. INIT_WORK(&shdlc->sm_work, llc_shdlc_sm_work);
  621. shdlc->hdev = hdev;
  622. shdlc->xmit_to_drv = xmit_to_drv;
  623. shdlc->rcv_to_hci = rcv_to_hci;
  624. shdlc->tx_headroom = tx_headroom;
  625. shdlc->tx_tailroom = tx_tailroom;
  626. shdlc->llc_failure = llc_failure;
  627. return shdlc;
  628. }
  629. static void llc_shdlc_deinit(struct nfc_llc *llc)
  630. {
  631. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  632. skb_queue_purge(&shdlc->rcv_q);
  633. skb_queue_purge(&shdlc->send_q);
  634. skb_queue_purge(&shdlc->ack_pending_q);
  635. kfree(shdlc);
  636. }
  637. static int llc_shdlc_start(struct nfc_llc *llc)
  638. {
  639. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  640. return llc_shdlc_connect(shdlc);
  641. }
  642. static int llc_shdlc_stop(struct nfc_llc *llc)
  643. {
  644. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  645. llc_shdlc_disconnect(shdlc);
  646. return 0;
  647. }
  648. static void llc_shdlc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
  649. {
  650. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  651. llc_shdlc_recv_frame(shdlc, skb);
  652. }
  653. static int llc_shdlc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
  654. {
  655. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  656. skb_queue_tail(&shdlc->send_q, skb);
  657. schedule_work(&shdlc->sm_work);
  658. return 0;
  659. }
  660. static struct nfc_llc_ops llc_shdlc_ops = {
  661. .init = llc_shdlc_init,
  662. .deinit = llc_shdlc_deinit,
  663. .start = llc_shdlc_start,
  664. .stop = llc_shdlc_stop,
  665. .rcv_from_drv = llc_shdlc_rcv_from_drv,
  666. .xmit_from_hci = llc_shdlc_xmit_from_hci,
  667. };
  668. int nfc_llc_shdlc_register(void)
  669. {
  670. return nfc_llc_register(LLC_SHDLC_NAME, &llc_shdlc_ops);
  671. }