shdlc.c 21 KB

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