llc_shdlc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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/export.h>
  24. #include <linux/wait.h>
  25. #include <linux/slab.h>
  26. #include <linux/skbuff.h>
  27. #include "llc.h"
  28. enum shdlc_state {
  29. SHDLC_DISCONNECTED = 0,
  30. SHDLC_CONNECTING = 1,
  31. SHDLC_NEGOCIATING = 2,
  32. SHDLC_CONNECTED = 3
  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_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. if (shdlc->state == SHDLC_NEGOCIATING) {
  325. /* we sent RSET, but chip wants to negociate */
  326. if (skb->len > 0)
  327. w = skb->data[0];
  328. if (skb->len > 1)
  329. srej_support = skb->data[1] & 0x01 ? true :
  330. false;
  331. if ((w <= SHDLC_MAX_WINDOW) &&
  332. (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
  333. shdlc->w = w;
  334. shdlc->srej_support = srej_support;
  335. r = llc_shdlc_connect_send_ua(shdlc);
  336. llc_shdlc_connect_complete(shdlc, r);
  337. }
  338. } else if (shdlc->state == SHDLC_CONNECTED) {
  339. /*
  340. * Chip wants to reset link. This is unexpected and
  341. * unsupported.
  342. */
  343. shdlc->hard_fault = -ECONNRESET;
  344. }
  345. break;
  346. case U_FRAME_UA:
  347. if ((shdlc->state == SHDLC_CONNECTING &&
  348. shdlc->connect_tries > 0) ||
  349. (shdlc->state == SHDLC_NEGOCIATING))
  350. llc_shdlc_connect_complete(shdlc, 0);
  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. ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
  374. nr = control & SHDLC_CONTROL_NR_MASK;
  375. llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
  376. break;
  377. case SHDLC_CONTROL_HEAD_S:
  378. s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
  379. nr = control & SHDLC_CONTROL_NR_MASK;
  380. llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
  381. kfree_skb(skb);
  382. break;
  383. case SHDLC_CONTROL_HEAD_U:
  384. u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
  385. llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
  386. break;
  387. default:
  388. pr_err("UNKNOWN Control=%d\n", control);
  389. kfree_skb(skb);
  390. break;
  391. }
  392. }
  393. }
  394. static int llc_shdlc_w_used(int ns, int dnr)
  395. {
  396. int unack_count;
  397. if (dnr <= ns)
  398. unack_count = ns - dnr;
  399. else
  400. unack_count = 8 - dnr + ns;
  401. return unack_count;
  402. }
  403. /* Send frames according to algorithm at spec:10.8.1 */
  404. static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
  405. {
  406. struct sk_buff *skb;
  407. int r;
  408. unsigned long time_sent;
  409. if (shdlc->send_q.qlen)
  410. pr_debug
  411. ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
  412. shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
  413. shdlc->rnr == false ? "false" : "true",
  414. shdlc->w - llc_shdlc_w_used(shdlc->ns, shdlc->dnr),
  415. shdlc->ack_pending_q.qlen);
  416. while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
  417. (shdlc->rnr == false)) {
  418. if (shdlc->t1_active) {
  419. del_timer_sync(&shdlc->t1_timer);
  420. shdlc->t1_active = false;
  421. pr_debug("Stopped T1(send ack)\n");
  422. }
  423. skb = skb_dequeue(&shdlc->send_q);
  424. *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
  425. shdlc->nr;
  426. pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
  427. shdlc->nr);
  428. SHDLC_DUMP_SKB("shdlc frame written", skb);
  429. r = shdlc->xmit_to_drv(shdlc->hdev, skb);
  430. if (r < 0) {
  431. shdlc->hard_fault = r;
  432. break;
  433. }
  434. shdlc->ns = (shdlc->ns + 1) % 8;
  435. time_sent = jiffies;
  436. *(unsigned long *)skb->cb = time_sent;
  437. skb_queue_tail(&shdlc->ack_pending_q, skb);
  438. if (shdlc->t2_active == false) {
  439. shdlc->t2_active = true;
  440. mod_timer(&shdlc->t2_timer, time_sent +
  441. msecs_to_jiffies(SHDLC_T2_VALUE_MS));
  442. pr_debug("Started T2 (retransmit)\n");
  443. }
  444. }
  445. }
  446. static void llc_shdlc_connect_timeout(unsigned long data)
  447. {
  448. struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
  449. pr_debug("\n");
  450. queue_work(system_nrt_wq, &shdlc->sm_work);
  451. }
  452. static void llc_shdlc_t1_timeout(unsigned long data)
  453. {
  454. struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
  455. pr_debug("SoftIRQ: need to send ack\n");
  456. queue_work(system_nrt_wq, &shdlc->sm_work);
  457. }
  458. static void llc_shdlc_t2_timeout(unsigned long data)
  459. {
  460. struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
  461. pr_debug("SoftIRQ: need to retransmit\n");
  462. queue_work(system_nrt_wq, &shdlc->sm_work);
  463. }
  464. static void llc_shdlc_sm_work(struct work_struct *work)
  465. {
  466. struct llc_shdlc *shdlc = container_of(work, struct llc_shdlc, sm_work);
  467. int r;
  468. pr_debug("\n");
  469. mutex_lock(&shdlc->state_mutex);
  470. switch (shdlc->state) {
  471. case SHDLC_DISCONNECTED:
  472. skb_queue_purge(&shdlc->rcv_q);
  473. skb_queue_purge(&shdlc->send_q);
  474. skb_queue_purge(&shdlc->ack_pending_q);
  475. break;
  476. case SHDLC_CONNECTING:
  477. if (shdlc->hard_fault) {
  478. llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
  479. break;
  480. }
  481. if (shdlc->connect_tries++ < 5)
  482. r = llc_shdlc_connect_initiate(shdlc);
  483. else
  484. r = -ETIME;
  485. if (r < 0)
  486. llc_shdlc_connect_complete(shdlc, r);
  487. else {
  488. mod_timer(&shdlc->connect_timer, jiffies +
  489. msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
  490. shdlc->state = SHDLC_NEGOCIATING;
  491. }
  492. break;
  493. case SHDLC_NEGOCIATING:
  494. if (timer_pending(&shdlc->connect_timer) == 0) {
  495. shdlc->state = SHDLC_CONNECTING;
  496. queue_work(system_nrt_wq, &shdlc->sm_work);
  497. }
  498. llc_shdlc_handle_rcv_queue(shdlc);
  499. if (shdlc->hard_fault) {
  500. llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
  501. break;
  502. }
  503. break;
  504. case SHDLC_CONNECTED:
  505. llc_shdlc_handle_rcv_queue(shdlc);
  506. llc_shdlc_handle_send_queue(shdlc);
  507. if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
  508. pr_debug
  509. ("Handle T1(send ack) elapsed (T1 now inactive)\n");
  510. shdlc->t1_active = false;
  511. r = llc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
  512. shdlc->nr);
  513. if (r < 0)
  514. shdlc->hard_fault = r;
  515. }
  516. if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
  517. pr_debug
  518. ("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. }
  526. break;
  527. default:
  528. break;
  529. }
  530. mutex_unlock(&shdlc->state_mutex);
  531. }
  532. /*
  533. * Called from syscall context to establish shdlc link. Sleeps until
  534. * link is ready or failure.
  535. */
  536. static int llc_shdlc_connect(struct llc_shdlc *shdlc)
  537. {
  538. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
  539. pr_debug("\n");
  540. mutex_lock(&shdlc->state_mutex);
  541. shdlc->state = SHDLC_CONNECTING;
  542. shdlc->connect_wq = &connect_wq;
  543. shdlc->connect_tries = 0;
  544. shdlc->connect_result = 1;
  545. mutex_unlock(&shdlc->state_mutex);
  546. queue_work(system_nrt_wq, &shdlc->sm_work);
  547. wait_event(connect_wq, shdlc->connect_result != 1);
  548. return shdlc->connect_result;
  549. }
  550. static void llc_shdlc_disconnect(struct llc_shdlc *shdlc)
  551. {
  552. pr_debug("\n");
  553. mutex_lock(&shdlc->state_mutex);
  554. shdlc->state = SHDLC_DISCONNECTED;
  555. mutex_unlock(&shdlc->state_mutex);
  556. queue_work(system_nrt_wq, &shdlc->sm_work);
  557. }
  558. /*
  559. * Receive an incoming shdlc frame. Frame has already been crc-validated.
  560. * skb contains only LLC header and payload.
  561. * If skb == NULL, it is a notification that the link below is dead.
  562. */
  563. static void llc_shdlc_recv_frame(struct llc_shdlc *shdlc, struct sk_buff *skb)
  564. {
  565. if (skb == NULL) {
  566. pr_err("NULL Frame -> link is dead\n");
  567. shdlc->hard_fault = -EREMOTEIO;
  568. } else {
  569. SHDLC_DUMP_SKB("incoming frame", skb);
  570. skb_queue_tail(&shdlc->rcv_q, skb);
  571. }
  572. queue_work(system_nrt_wq, &shdlc->sm_work);
  573. }
  574. static void *llc_shdlc_init(struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
  575. rcv_to_hci_t rcv_to_hci, int tx_headroom,
  576. int tx_tailroom, int *rx_headroom, int *rx_tailroom,
  577. llc_failure_t llc_failure)
  578. {
  579. struct llc_shdlc *shdlc;
  580. *rx_headroom = SHDLC_LLC_HEAD_ROOM;
  581. *rx_tailroom = 0;
  582. shdlc = kzalloc(sizeof(struct llc_shdlc), GFP_KERNEL);
  583. if (shdlc == NULL)
  584. return NULL;
  585. mutex_init(&shdlc->state_mutex);
  586. shdlc->state = SHDLC_DISCONNECTED;
  587. init_timer(&shdlc->connect_timer);
  588. shdlc->connect_timer.data = (unsigned long)shdlc;
  589. shdlc->connect_timer.function = llc_shdlc_connect_timeout;
  590. init_timer(&shdlc->t1_timer);
  591. shdlc->t1_timer.data = (unsigned long)shdlc;
  592. shdlc->t1_timer.function = llc_shdlc_t1_timeout;
  593. init_timer(&shdlc->t2_timer);
  594. shdlc->t2_timer.data = (unsigned long)shdlc;
  595. shdlc->t2_timer.function = llc_shdlc_t2_timeout;
  596. shdlc->w = SHDLC_MAX_WINDOW;
  597. shdlc->srej_support = SHDLC_SREJ_SUPPORT;
  598. skb_queue_head_init(&shdlc->rcv_q);
  599. skb_queue_head_init(&shdlc->send_q);
  600. skb_queue_head_init(&shdlc->ack_pending_q);
  601. INIT_WORK(&shdlc->sm_work, llc_shdlc_sm_work);
  602. shdlc->hdev = hdev;
  603. shdlc->xmit_to_drv = xmit_to_drv;
  604. shdlc->rcv_to_hci = rcv_to_hci;
  605. shdlc->tx_headroom = tx_headroom;
  606. shdlc->tx_tailroom = tx_tailroom;
  607. shdlc->llc_failure = llc_failure;
  608. return shdlc;
  609. }
  610. static void llc_shdlc_deinit(struct nfc_llc *llc)
  611. {
  612. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  613. skb_queue_purge(&shdlc->rcv_q);
  614. skb_queue_purge(&shdlc->send_q);
  615. skb_queue_purge(&shdlc->ack_pending_q);
  616. kfree(shdlc);
  617. }
  618. static int llc_shdlc_start(struct nfc_llc *llc)
  619. {
  620. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  621. return llc_shdlc_connect(shdlc);
  622. }
  623. static int llc_shdlc_stop(struct nfc_llc *llc)
  624. {
  625. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  626. llc_shdlc_disconnect(shdlc);
  627. return 0;
  628. }
  629. static void llc_shdlc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
  630. {
  631. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  632. llc_shdlc_recv_frame(shdlc, skb);
  633. }
  634. static int llc_shdlc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
  635. {
  636. struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
  637. skb_queue_tail(&shdlc->send_q, skb);
  638. queue_work(system_nrt_wq, &shdlc->sm_work);
  639. return 0;
  640. }
  641. static struct nfc_llc_ops llc_shdlc_ops = {
  642. .init = llc_shdlc_init,
  643. .deinit = llc_shdlc_deinit,
  644. .start = llc_shdlc_start,
  645. .stop = llc_shdlc_stop,
  646. .rcv_from_drv = llc_shdlc_rcv_from_drv,
  647. .xmit_from_hci = llc_shdlc_xmit_from_hci,
  648. };
  649. int nfc_llc_shdlc_register()
  650. {
  651. return nfc_llc_register(LLC_SHDLC_NAME, &llc_shdlc_ops);
  652. }
  653. EXPORT_SYMBOL(nfc_llc_shdlc_register);