shdlc.c 22 KB

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