llc_shdlc.c 18 KB

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