llc_shdlc.c 19 KB

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