llc_shdlc.c 19 KB

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