ndlc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip
  3. *
  4. * Copyright (C) 2014 STMicroelectronics SAS. 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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/sched.h>
  19. #include <net/nfc/nci_core.h>
  20. #include "ndlc.h"
  21. #include "st21nfcb.h"
  22. #define NDLC_TIMER_T1 100
  23. #define NDLC_TIMER_T1_WAIT 400
  24. #define NDLC_TIMER_T2 1200
  25. #define PCB_TYPE_DATAFRAME 0x80
  26. #define PCB_TYPE_SUPERVISOR 0xc0
  27. #define PCB_TYPE_MASK PCB_TYPE_SUPERVISOR
  28. #define PCB_SYNC_ACK 0x20
  29. #define PCB_SYNC_NACK 0x10
  30. #define PCB_SYNC_WAIT 0x30
  31. #define PCB_SYNC_NOINFO 0x00
  32. #define PCB_SYNC_MASK PCB_SYNC_WAIT
  33. #define PCB_DATAFRAME_RETRANSMIT_YES 0x00
  34. #define PCB_DATAFRAME_RETRANSMIT_NO 0x04
  35. #define PCB_DATAFRAME_RETRANSMIT_MASK PCB_DATAFRAME_RETRANSMIT_NO
  36. #define PCB_SUPERVISOR_RETRANSMIT_YES 0x00
  37. #define PCB_SUPERVISOR_RETRANSMIT_NO 0x02
  38. #define PCB_SUPERVISOR_RETRANSMIT_MASK PCB_SUPERVISOR_RETRANSMIT_NO
  39. #define PCB_FRAME_CRC_INFO_PRESENT 0x08
  40. #define PCB_FRAME_CRC_INFO_NOTPRESENT 0x00
  41. #define PCB_FRAME_CRC_INFO_MASK PCB_FRAME_CRC_INFO_PRESENT
  42. #define NDLC_DUMP_SKB(info, skb) \
  43. do { \
  44. pr_debug("%s:\n", info); \
  45. print_hex_dump(KERN_DEBUG, "ndlc: ", DUMP_PREFIX_OFFSET, \
  46. 16, 1, skb->data, skb->len, 0); \
  47. } while (0)
  48. int ndlc_open(struct llt_ndlc *ndlc)
  49. {
  50. /* toggle reset pin */
  51. ndlc->ops->enable(ndlc->phy_id);
  52. return 0;
  53. }
  54. EXPORT_SYMBOL(ndlc_open);
  55. void ndlc_close(struct llt_ndlc *ndlc)
  56. {
  57. /* toggle reset pin */
  58. ndlc->ops->disable(ndlc->phy_id);
  59. }
  60. EXPORT_SYMBOL(ndlc_close);
  61. int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb)
  62. {
  63. /* add ndlc header */
  64. u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO |
  65. PCB_FRAME_CRC_INFO_NOTPRESENT;
  66. *skb_push(skb, 1) = pcb;
  67. skb_queue_tail(&ndlc->send_q, skb);
  68. schedule_work(&ndlc->sm_work);
  69. return 0;
  70. }
  71. EXPORT_SYMBOL(ndlc_send);
  72. static void llt_ndlc_send_queue(struct llt_ndlc *ndlc)
  73. {
  74. struct sk_buff *skb;
  75. int r;
  76. unsigned long time_sent;
  77. if (ndlc->send_q.qlen)
  78. pr_debug("sendQlen=%d unackQlen=%d\n",
  79. ndlc->send_q.qlen, ndlc->ack_pending_q.qlen);
  80. while (ndlc->send_q.qlen) {
  81. skb = skb_dequeue(&ndlc->send_q);
  82. NDLC_DUMP_SKB("ndlc frame written", skb);
  83. r = ndlc->ops->write(ndlc->phy_id, skb);
  84. if (r < 0) {
  85. ndlc->hard_fault = r;
  86. break;
  87. }
  88. time_sent = jiffies;
  89. *(unsigned long *)skb->cb = time_sent;
  90. skb_queue_tail(&ndlc->ack_pending_q, skb);
  91. /* start timer t1 for ndlc aknowledge */
  92. ndlc->t1_active = true;
  93. mod_timer(&ndlc->t1_timer, time_sent +
  94. msecs_to_jiffies(NDLC_TIMER_T1));
  95. /* start timer t2 for chip availability */
  96. ndlc->t2_active = true;
  97. mod_timer(&ndlc->t2_timer, time_sent +
  98. msecs_to_jiffies(NDLC_TIMER_T2));
  99. }
  100. }
  101. static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc)
  102. {
  103. struct sk_buff *skb;
  104. u8 pcb;
  105. while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) {
  106. pcb = skb->data[0];
  107. switch (pcb & PCB_TYPE_MASK) {
  108. case PCB_TYPE_SUPERVISOR:
  109. skb->data[0] = (pcb & ~PCB_SUPERVISOR_RETRANSMIT_MASK) |
  110. PCB_SUPERVISOR_RETRANSMIT_YES;
  111. break;
  112. case PCB_TYPE_DATAFRAME:
  113. skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) |
  114. PCB_DATAFRAME_RETRANSMIT_YES;
  115. break;
  116. default:
  117. pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
  118. kfree_skb(skb);
  119. break;
  120. }
  121. skb_queue_head(&ndlc->send_q, skb);
  122. }
  123. }
  124. static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc)
  125. {
  126. struct sk_buff *skb;
  127. u8 pcb;
  128. unsigned long time_sent;
  129. if (ndlc->rcv_q.qlen)
  130. pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen);
  131. while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) {
  132. pcb = skb->data[0];
  133. skb_pull(skb, 1);
  134. if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) {
  135. switch (pcb & PCB_SYNC_MASK) {
  136. case PCB_SYNC_ACK:
  137. del_timer_sync(&ndlc->t1_timer);
  138. del_timer_sync(&ndlc->t2_timer);
  139. ndlc->t2_active = false;
  140. ndlc->t1_active = false;
  141. break;
  142. case PCB_SYNC_NACK:
  143. llt_ndlc_requeue_data_pending(ndlc);
  144. llt_ndlc_send_queue(ndlc);
  145. /* start timer t1 for ndlc aknowledge */
  146. time_sent = jiffies;
  147. ndlc->t1_active = true;
  148. mod_timer(&ndlc->t1_timer, time_sent +
  149. msecs_to_jiffies(NDLC_TIMER_T1));
  150. break;
  151. case PCB_SYNC_WAIT:
  152. time_sent = jiffies;
  153. ndlc->t1_active = true;
  154. mod_timer(&ndlc->t1_timer, time_sent +
  155. msecs_to_jiffies(NDLC_TIMER_T1_WAIT));
  156. break;
  157. default:
  158. pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
  159. kfree_skb(skb);
  160. break;
  161. }
  162. } else {
  163. nci_recv_frame(ndlc->ndev, skb);
  164. }
  165. }
  166. }
  167. static void llt_ndlc_sm_work(struct work_struct *work)
  168. {
  169. struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work);
  170. llt_ndlc_send_queue(ndlc);
  171. llt_ndlc_rcv_queue(ndlc);
  172. if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) {
  173. pr_debug
  174. ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
  175. ndlc->t1_active = false;
  176. llt_ndlc_requeue_data_pending(ndlc);
  177. llt_ndlc_send_queue(ndlc);
  178. }
  179. if (ndlc->t2_active && timer_pending(&ndlc->t2_timer) == 0) {
  180. pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n");
  181. ndlc->t2_active = false;
  182. ndlc->t1_active = false;
  183. del_timer_sync(&ndlc->t1_timer);
  184. del_timer_sync(&ndlc->t2_timer);
  185. ndlc_close(ndlc);
  186. ndlc->hard_fault = -EREMOTEIO;
  187. }
  188. }
  189. void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb)
  190. {
  191. if (skb == NULL) {
  192. pr_err("NULL Frame -> link is dead\n");
  193. ndlc->hard_fault = -EREMOTEIO;
  194. ndlc_close(ndlc);
  195. } else {
  196. NDLC_DUMP_SKB("incoming frame", skb);
  197. skb_queue_tail(&ndlc->rcv_q, skb);
  198. }
  199. schedule_work(&ndlc->sm_work);
  200. }
  201. EXPORT_SYMBOL(ndlc_recv);
  202. static void ndlc_t1_timeout(unsigned long data)
  203. {
  204. struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
  205. pr_debug("\n");
  206. schedule_work(&ndlc->sm_work);
  207. }
  208. static void ndlc_t2_timeout(unsigned long data)
  209. {
  210. struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
  211. pr_debug("\n");
  212. schedule_work(&ndlc->sm_work);
  213. }
  214. int ndlc_probe(void *phy_id, struct nfc_phy_ops *phy_ops, struct device *dev,
  215. int phy_headroom, int phy_tailroom, struct llt_ndlc **ndlc_id)
  216. {
  217. struct llt_ndlc *ndlc;
  218. ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL);
  219. if (!ndlc) {
  220. nfc_err(dev, "Cannot allocate memory for ndlc.\n");
  221. return -ENOMEM;
  222. }
  223. ndlc->ops = phy_ops;
  224. ndlc->phy_id = phy_id;
  225. ndlc->dev = dev;
  226. *ndlc_id = ndlc;
  227. /* initialize timers */
  228. init_timer(&ndlc->t1_timer);
  229. ndlc->t1_timer.data = (unsigned long)ndlc;
  230. ndlc->t1_timer.function = ndlc_t1_timeout;
  231. init_timer(&ndlc->t2_timer);
  232. ndlc->t2_timer.data = (unsigned long)ndlc;
  233. ndlc->t2_timer.function = ndlc_t2_timeout;
  234. skb_queue_head_init(&ndlc->rcv_q);
  235. skb_queue_head_init(&ndlc->send_q);
  236. skb_queue_head_init(&ndlc->ack_pending_q);
  237. INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work);
  238. return st21nfcb_nci_probe(ndlc, phy_headroom, phy_tailroom);
  239. }
  240. EXPORT_SYMBOL(ndlc_probe);
  241. void ndlc_remove(struct llt_ndlc *ndlc)
  242. {
  243. /* cancel timers */
  244. del_timer_sync(&ndlc->t1_timer);
  245. del_timer_sync(&ndlc->t2_timer);
  246. ndlc->t2_active = false;
  247. ndlc->t1_active = false;
  248. skb_queue_purge(&ndlc->rcv_q);
  249. skb_queue_purge(&ndlc->send_q);
  250. st21nfcb_nci_remove(ndlc->ndev);
  251. kfree(ndlc);
  252. }
  253. EXPORT_SYMBOL(ndlc_remove);