shdlc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. * TODO: Could it be possible that there are pending
  274. * executing commands that are waiting for connect to complete
  275. * before they can be carried? As connect is a blocking
  276. * operation, it would require that the userspace process can
  277. * send commands on the same device from a second thread before
  278. * the device is up. I don't think that is possible, is it?
  279. */
  280. }
  281. shdlc->connect_result = r;
  282. wake_up(shdlc->connect_wq);
  283. }
  284. static int nfc_shdlc_connect_initiate(struct nfc_shdlc *shdlc)
  285. {
  286. struct sk_buff *skb;
  287. pr_debug("\n");
  288. skb = nfc_shdlc_alloc_skb(shdlc, 2);
  289. if (skb == NULL)
  290. return -ENOMEM;
  291. *skb_put(skb, 1) = SHDLC_MAX_WINDOW;
  292. *skb_put(skb, 1) = SHDLC_SREJ_SUPPORT ? 1 : 0;
  293. return nfc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
  294. }
  295. static int nfc_shdlc_connect_send_ua(struct nfc_shdlc *shdlc)
  296. {
  297. struct sk_buff *skb;
  298. pr_debug("\n");
  299. skb = nfc_shdlc_alloc_skb(shdlc, 0);
  300. if (skb == NULL)
  301. return -ENOMEM;
  302. return nfc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
  303. }
  304. static void nfc_shdlc_rcv_u_frame(struct nfc_shdlc *shdlc,
  305. struct sk_buff *skb,
  306. enum uframe_modifier u_frame_modifier)
  307. {
  308. u8 w = SHDLC_MAX_WINDOW;
  309. bool srej_support = SHDLC_SREJ_SUPPORT;
  310. int r;
  311. pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
  312. switch (u_frame_modifier) {
  313. case U_FRAME_RSET:
  314. if (shdlc->state == SHDLC_NEGOCIATING) {
  315. /* we sent RSET, but chip wants to negociate */
  316. if (skb->len > 0)
  317. w = skb->data[0];
  318. if (skb->len > 1)
  319. srej_support = skb->data[1] & 0x01 ? true :
  320. false;
  321. if ((w <= SHDLC_MAX_WINDOW) &&
  322. (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
  323. shdlc->w = w;
  324. shdlc->srej_support = srej_support;
  325. r = nfc_shdlc_connect_send_ua(shdlc);
  326. nfc_shdlc_connect_complete(shdlc, r);
  327. }
  328. } else if (shdlc->state > SHDLC_NEGOCIATING) {
  329. /*
  330. * TODO: Chip wants to reset link
  331. * send ua, empty skb lists, reset counters
  332. * propagate info to HCI layer
  333. */
  334. }
  335. break;
  336. case U_FRAME_UA:
  337. if ((shdlc->state == SHDLC_CONNECTING &&
  338. shdlc->connect_tries > 0) ||
  339. (shdlc->state == SHDLC_NEGOCIATING))
  340. nfc_shdlc_connect_complete(shdlc, 0);
  341. break;
  342. default:
  343. break;
  344. }
  345. kfree_skb(skb);
  346. }
  347. static void nfc_shdlc_handle_rcv_queue(struct nfc_shdlc *shdlc)
  348. {
  349. struct sk_buff *skb;
  350. u8 control;
  351. int nr;
  352. int ns;
  353. enum sframe_type s_frame_type;
  354. enum uframe_modifier u_frame_modifier;
  355. if (shdlc->rcv_q.qlen)
  356. pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
  357. while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
  358. control = skb->data[0];
  359. skb_pull(skb, 1);
  360. switch (control & SHDLC_CONTROL_HEAD_MASK) {
  361. case SHDLC_CONTROL_HEAD_I:
  362. case SHDLC_CONTROL_HEAD_I2:
  363. ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
  364. nr = control & SHDLC_CONTROL_NR_MASK;
  365. nfc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
  366. break;
  367. case SHDLC_CONTROL_HEAD_S:
  368. s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
  369. nr = control & SHDLC_CONTROL_NR_MASK;
  370. nfc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
  371. kfree_skb(skb);
  372. break;
  373. case SHDLC_CONTROL_HEAD_U:
  374. u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
  375. nfc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
  376. break;
  377. default:
  378. pr_err("UNKNOWN Control=%d\n", control);
  379. kfree_skb(skb);
  380. break;
  381. }
  382. }
  383. }
  384. static int nfc_shdlc_w_used(int ns, int dnr)
  385. {
  386. int unack_count;
  387. if (dnr <= ns)
  388. unack_count = ns - dnr;
  389. else
  390. unack_count = 8 - dnr + ns;
  391. return unack_count;
  392. }
  393. /* Send frames according to algorithm at spec:10.8.1 */
  394. static void nfc_shdlc_handle_send_queue(struct nfc_shdlc *shdlc)
  395. {
  396. struct sk_buff *skb;
  397. int r;
  398. unsigned long time_sent;
  399. if (shdlc->send_q.qlen)
  400. pr_debug
  401. ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
  402. shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
  403. shdlc->rnr == false ? "false" : "true",
  404. shdlc->w - nfc_shdlc_w_used(shdlc->ns, shdlc->dnr),
  405. shdlc->ack_pending_q.qlen);
  406. while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
  407. (shdlc->rnr == false)) {
  408. if (shdlc->t1_active) {
  409. del_timer_sync(&shdlc->t1_timer);
  410. shdlc->t1_active = false;
  411. pr_debug("Stopped T1(send ack)\n");
  412. }
  413. skb = skb_dequeue(&shdlc->send_q);
  414. *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
  415. shdlc->nr;
  416. pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
  417. shdlc->nr);
  418. /* SHDLC_DUMP_SKB("shdlc frame written", skb); */
  419. nfc_shdlc_add_len_crc(skb);
  420. r = shdlc->ops->xmit(shdlc, skb);
  421. if (r < 0) {
  422. /*
  423. * TODO: Cannot send, shdlc machine is dead, we
  424. * must propagate the information up to HCI.
  425. */
  426. shdlc->hard_fault = r;
  427. break;
  428. }
  429. shdlc->ns = (shdlc->ns + 1) % 8;
  430. time_sent = jiffies;
  431. *(unsigned long *)skb->cb = time_sent;
  432. skb_queue_tail(&shdlc->ack_pending_q, skb);
  433. if (shdlc->t2_active == false) {
  434. shdlc->t2_active = true;
  435. mod_timer(&shdlc->t2_timer, time_sent +
  436. msecs_to_jiffies(SHDLC_T2_VALUE_MS));
  437. pr_debug("Started T2 (retransmit)\n");
  438. }
  439. }
  440. }
  441. static void nfc_shdlc_connect_timeout(unsigned long data)
  442. {
  443. struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
  444. pr_debug("\n");
  445. queue_work(shdlc->sm_wq, &shdlc->sm_work);
  446. }
  447. static void nfc_shdlc_t1_timeout(unsigned long data)
  448. {
  449. struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
  450. pr_debug("SoftIRQ: need to send ack\n");
  451. queue_work(shdlc->sm_wq, &shdlc->sm_work);
  452. }
  453. static void nfc_shdlc_t2_timeout(unsigned long data)
  454. {
  455. struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
  456. pr_debug("SoftIRQ: need to retransmit\n");
  457. queue_work(shdlc->sm_wq, &shdlc->sm_work);
  458. }
  459. static void nfc_shdlc_sm_work(struct work_struct *work)
  460. {
  461. struct nfc_shdlc *shdlc = container_of(work, struct nfc_shdlc, sm_work);
  462. int r;
  463. pr_debug("\n");
  464. mutex_lock(&shdlc->state_mutex);
  465. switch (shdlc->state) {
  466. case SHDLC_DISCONNECTED:
  467. skb_queue_purge(&shdlc->rcv_q);
  468. skb_queue_purge(&shdlc->send_q);
  469. skb_queue_purge(&shdlc->ack_pending_q);
  470. break;
  471. case SHDLC_CONNECTING:
  472. if (shdlc->connect_tries++ < 5)
  473. r = nfc_shdlc_connect_initiate(shdlc);
  474. else
  475. r = -ETIME;
  476. if (r < 0)
  477. nfc_shdlc_connect_complete(shdlc, r);
  478. else {
  479. mod_timer(&shdlc->connect_timer, jiffies +
  480. msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
  481. shdlc->state = SHDLC_NEGOCIATING;
  482. }
  483. break;
  484. case SHDLC_NEGOCIATING:
  485. if (timer_pending(&shdlc->connect_timer) == 0) {
  486. shdlc->state = SHDLC_CONNECTING;
  487. queue_work(shdlc->sm_wq, &shdlc->sm_work);
  488. }
  489. nfc_shdlc_handle_rcv_queue(shdlc);
  490. break;
  491. case SHDLC_CONNECTED:
  492. nfc_shdlc_handle_rcv_queue(shdlc);
  493. nfc_shdlc_handle_send_queue(shdlc);
  494. if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
  495. pr_debug
  496. ("Handle T1(send ack) elapsed (T1 now inactive)\n");
  497. shdlc->t1_active = false;
  498. r = nfc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
  499. shdlc->nr);
  500. if (r < 0)
  501. shdlc->hard_fault = r;
  502. }
  503. if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
  504. pr_debug
  505. ("Handle T2(retransmit) elapsed (T2 inactive)\n");
  506. shdlc->t2_active = false;
  507. nfc_shdlc_requeue_ack_pending(shdlc);
  508. nfc_shdlc_handle_send_queue(shdlc);
  509. }
  510. if (shdlc->hard_fault) {
  511. /*
  512. * TODO: Handle hard_fault that occured during
  513. * this invocation of the shdlc worker
  514. */
  515. }
  516. break;
  517. default:
  518. break;
  519. }
  520. mutex_unlock(&shdlc->state_mutex);
  521. }
  522. /*
  523. * Called from syscall context to establish shdlc link. Sleeps until
  524. * link is ready or failure.
  525. */
  526. static int nfc_shdlc_connect(struct nfc_shdlc *shdlc)
  527. {
  528. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
  529. pr_debug("\n");
  530. mutex_lock(&shdlc->state_mutex);
  531. shdlc->state = SHDLC_CONNECTING;
  532. shdlc->connect_wq = &connect_wq;
  533. shdlc->connect_tries = 0;
  534. shdlc->connect_result = 1;
  535. mutex_unlock(&shdlc->state_mutex);
  536. queue_work(shdlc->sm_wq, &shdlc->sm_work);
  537. wait_event(connect_wq, shdlc->connect_result != 1);
  538. return shdlc->connect_result;
  539. }
  540. static void nfc_shdlc_disconnect(struct nfc_shdlc *shdlc)
  541. {
  542. pr_debug("\n");
  543. mutex_lock(&shdlc->state_mutex);
  544. shdlc->state = SHDLC_DISCONNECTED;
  545. mutex_unlock(&shdlc->state_mutex);
  546. queue_work(shdlc->sm_wq, &shdlc->sm_work);
  547. }
  548. /*
  549. * Receive an incoming shdlc frame. Frame has already been crc-validated.
  550. * skb contains only LLC header and payload.
  551. * If skb == NULL, it is a notification that the link below is dead.
  552. */
  553. void nfc_shdlc_recv_frame(struct nfc_shdlc *shdlc, struct sk_buff *skb)
  554. {
  555. if (skb == NULL) {
  556. pr_err("NULL Frame -> link is dead\n");
  557. shdlc->hard_fault = -EREMOTEIO;
  558. } else {
  559. SHDLC_DUMP_SKB("incoming frame", skb);
  560. skb_queue_tail(&shdlc->rcv_q, skb);
  561. }
  562. queue_work(shdlc->sm_wq, &shdlc->sm_work);
  563. }
  564. EXPORT_SYMBOL(nfc_shdlc_recv_frame);
  565. static int nfc_shdlc_open(struct nfc_hci_dev *hdev)
  566. {
  567. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  568. int r;
  569. pr_debug("\n");
  570. if (shdlc->ops->open) {
  571. r = shdlc->ops->open(shdlc);
  572. if (r < 0)
  573. return r;
  574. }
  575. r = nfc_shdlc_connect(shdlc);
  576. if (r < 0 && shdlc->ops->close)
  577. shdlc->ops->close(shdlc);
  578. return r;
  579. }
  580. static void nfc_shdlc_close(struct nfc_hci_dev *hdev)
  581. {
  582. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  583. pr_debug("\n");
  584. nfc_shdlc_disconnect(shdlc);
  585. if (shdlc->ops->close)
  586. shdlc->ops->close(shdlc);
  587. }
  588. static int nfc_shdlc_hci_ready(struct nfc_hci_dev *hdev)
  589. {
  590. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  591. int r = 0;
  592. pr_debug("\n");
  593. if (shdlc->ops->hci_ready)
  594. r = shdlc->ops->hci_ready(shdlc);
  595. return r;
  596. }
  597. static int nfc_shdlc_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  598. {
  599. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  600. SHDLC_DUMP_SKB("queuing HCP packet to shdlc", skb);
  601. skb_queue_tail(&shdlc->send_q, skb);
  602. queue_work(shdlc->sm_wq, &shdlc->sm_work);
  603. return 0;
  604. }
  605. static int nfc_shdlc_start_poll(struct nfc_hci_dev *hdev, u32 protocols)
  606. {
  607. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  608. pr_debug("\n");
  609. if (shdlc->ops->start_poll)
  610. return shdlc->ops->start_poll(shdlc, protocols);
  611. return 0;
  612. }
  613. static int nfc_shdlc_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
  614. struct nfc_target *target)
  615. {
  616. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  617. if (shdlc->ops->target_from_gate)
  618. return shdlc->ops->target_from_gate(shdlc, gate, target);
  619. return -EPERM;
  620. }
  621. static int nfc_shdlc_complete_target_discovered(struct nfc_hci_dev *hdev,
  622. u8 gate,
  623. struct nfc_target *target)
  624. {
  625. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  626. pr_debug("\n");
  627. if (shdlc->ops->complete_target_discovered)
  628. return shdlc->ops->complete_target_discovered(shdlc, gate,
  629. target);
  630. return 0;
  631. }
  632. static int nfc_shdlc_data_exchange(struct nfc_hci_dev *hdev,
  633. struct nfc_target *target,
  634. struct sk_buff *skb,
  635. struct sk_buff **res_skb)
  636. {
  637. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  638. if (shdlc->ops->data_exchange)
  639. return shdlc->ops->data_exchange(shdlc, target, skb, res_skb);
  640. return -EPERM;
  641. }
  642. static int nfc_shdlc_check_presence(struct nfc_hci_dev *hdev,
  643. struct nfc_target *target)
  644. {
  645. struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
  646. if (shdlc->ops->check_presence)
  647. return shdlc->ops->check_presence(shdlc, target);
  648. return 0;
  649. }
  650. static struct nfc_hci_ops shdlc_ops = {
  651. .open = nfc_shdlc_open,
  652. .close = nfc_shdlc_close,
  653. .hci_ready = nfc_shdlc_hci_ready,
  654. .xmit = nfc_shdlc_xmit,
  655. .start_poll = nfc_shdlc_start_poll,
  656. .target_from_gate = nfc_shdlc_target_from_gate,
  657. .complete_target_discovered = nfc_shdlc_complete_target_discovered,
  658. .data_exchange = nfc_shdlc_data_exchange,
  659. .check_presence = nfc_shdlc_check_presence,
  660. };
  661. struct nfc_shdlc *nfc_shdlc_allocate(struct nfc_shdlc_ops *ops,
  662. struct nfc_hci_init_data *init_data,
  663. u32 protocols,
  664. int tx_headroom, int tx_tailroom,
  665. int max_link_payload, const char *devname)
  666. {
  667. struct nfc_shdlc *shdlc;
  668. int r;
  669. char name[32];
  670. if (ops->xmit == NULL)
  671. return NULL;
  672. shdlc = kzalloc(sizeof(struct nfc_shdlc), GFP_KERNEL);
  673. if (shdlc == NULL)
  674. return NULL;
  675. mutex_init(&shdlc->state_mutex);
  676. shdlc->ops = ops;
  677. shdlc->state = SHDLC_DISCONNECTED;
  678. init_timer(&shdlc->connect_timer);
  679. shdlc->connect_timer.data = (unsigned long)shdlc;
  680. shdlc->connect_timer.function = nfc_shdlc_connect_timeout;
  681. init_timer(&shdlc->t1_timer);
  682. shdlc->t1_timer.data = (unsigned long)shdlc;
  683. shdlc->t1_timer.function = nfc_shdlc_t1_timeout;
  684. init_timer(&shdlc->t2_timer);
  685. shdlc->t2_timer.data = (unsigned long)shdlc;
  686. shdlc->t2_timer.function = nfc_shdlc_t2_timeout;
  687. shdlc->w = SHDLC_MAX_WINDOW;
  688. shdlc->srej_support = SHDLC_SREJ_SUPPORT;
  689. skb_queue_head_init(&shdlc->rcv_q);
  690. skb_queue_head_init(&shdlc->send_q);
  691. skb_queue_head_init(&shdlc->ack_pending_q);
  692. INIT_WORK(&shdlc->sm_work, nfc_shdlc_sm_work);
  693. snprintf(name, sizeof(name), "%s_shdlc_sm_wq", devname);
  694. shdlc->sm_wq = alloc_workqueue(name, WQ_NON_REENTRANT | WQ_UNBOUND |
  695. WQ_MEM_RECLAIM, 1);
  696. if (shdlc->sm_wq == NULL)
  697. goto err_allocwq;
  698. shdlc->client_headroom = tx_headroom;
  699. shdlc->client_tailroom = tx_tailroom;
  700. shdlc->hdev = nfc_hci_allocate_device(&shdlc_ops, init_data, protocols,
  701. tx_headroom + SHDLC_LLC_HEAD_ROOM,
  702. tx_tailroom + SHDLC_LLC_TAIL_ROOM,
  703. max_link_payload);
  704. if (shdlc->hdev == NULL)
  705. goto err_allocdev;
  706. nfc_hci_set_clientdata(shdlc->hdev, shdlc);
  707. r = nfc_hci_register_device(shdlc->hdev);
  708. if (r < 0)
  709. goto err_regdev;
  710. return shdlc;
  711. err_regdev:
  712. nfc_hci_free_device(shdlc->hdev);
  713. err_allocdev:
  714. destroy_workqueue(shdlc->sm_wq);
  715. err_allocwq:
  716. kfree(shdlc);
  717. return NULL;
  718. }
  719. EXPORT_SYMBOL(nfc_shdlc_allocate);
  720. void nfc_shdlc_free(struct nfc_shdlc *shdlc)
  721. {
  722. pr_debug("\n");
  723. /* TODO: Check that this cannot be called while still in use */
  724. nfc_hci_unregister_device(shdlc->hdev);
  725. nfc_hci_free_device(shdlc->hdev);
  726. destroy_workqueue(shdlc->sm_wq);
  727. skb_queue_purge(&shdlc->rcv_q);
  728. skb_queue_purge(&shdlc->send_q);
  729. skb_queue_purge(&shdlc->ack_pending_q);
  730. kfree(shdlc);
  731. }
  732. EXPORT_SYMBOL(nfc_shdlc_free);
  733. void nfc_shdlc_set_clientdata(struct nfc_shdlc *shdlc, void *clientdata)
  734. {
  735. pr_debug("\n");
  736. shdlc->clientdata = clientdata;
  737. }
  738. EXPORT_SYMBOL(nfc_shdlc_set_clientdata);
  739. void *nfc_shdlc_get_clientdata(struct nfc_shdlc *shdlc)
  740. {
  741. return shdlc->clientdata;
  742. }
  743. EXPORT_SYMBOL(nfc_shdlc_get_clientdata);
  744. struct nfc_hci_dev *nfc_shdlc_get_hci_dev(struct nfc_shdlc *shdlc)
  745. {
  746. return shdlc->hdev;
  747. }
  748. EXPORT_SYMBOL(nfc_shdlc_get_hci_dev);