ndlc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip
  3. *
  4. * Copyright (C) 2014-2015 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 "st-nci.h"
  21. #include "ndlc.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. ndlc->powered = 1;
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(ndlc_open);
  56. void ndlc_close(struct llt_ndlc *ndlc)
  57. {
  58. struct nci_mode_set_cmd cmd;
  59. cmd.cmd_type = ST_NCI_SET_NFC_MODE;
  60. cmd.mode = 0;
  61. /* toggle reset pin */
  62. ndlc->ops->enable(ndlc->phy_id);
  63. nci_prop_cmd(ndlc->ndev, ST_NCI_CORE_PROP,
  64. sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
  65. ndlc->powered = 0;
  66. ndlc->ops->disable(ndlc->phy_id);
  67. }
  68. EXPORT_SYMBOL(ndlc_close);
  69. int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb)
  70. {
  71. /* add ndlc header */
  72. u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO |
  73. PCB_FRAME_CRC_INFO_NOTPRESENT;
  74. *skb_push(skb, 1) = pcb;
  75. skb_queue_tail(&ndlc->send_q, skb);
  76. schedule_work(&ndlc->sm_work);
  77. return 0;
  78. }
  79. EXPORT_SYMBOL(ndlc_send);
  80. static void llt_ndlc_send_queue(struct llt_ndlc *ndlc)
  81. {
  82. struct sk_buff *skb;
  83. int r;
  84. unsigned long time_sent;
  85. if (ndlc->send_q.qlen)
  86. pr_debug("sendQlen=%d unackQlen=%d\n",
  87. ndlc->send_q.qlen, ndlc->ack_pending_q.qlen);
  88. while (ndlc->send_q.qlen) {
  89. skb = skb_dequeue(&ndlc->send_q);
  90. NDLC_DUMP_SKB("ndlc frame written", skb);
  91. r = ndlc->ops->write(ndlc->phy_id, skb);
  92. if (r < 0) {
  93. ndlc->hard_fault = r;
  94. break;
  95. }
  96. time_sent = jiffies;
  97. *(unsigned long *)skb->cb = time_sent;
  98. skb_queue_tail(&ndlc->ack_pending_q, skb);
  99. /* start timer t1 for ndlc aknowledge */
  100. ndlc->t1_active = true;
  101. mod_timer(&ndlc->t1_timer, time_sent +
  102. msecs_to_jiffies(NDLC_TIMER_T1));
  103. /* start timer t2 for chip availability */
  104. ndlc->t2_active = true;
  105. mod_timer(&ndlc->t2_timer, time_sent +
  106. msecs_to_jiffies(NDLC_TIMER_T2));
  107. }
  108. }
  109. static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc)
  110. {
  111. struct sk_buff *skb;
  112. u8 pcb;
  113. while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) {
  114. pcb = skb->data[0];
  115. switch (pcb & PCB_TYPE_MASK) {
  116. case PCB_TYPE_SUPERVISOR:
  117. skb->data[0] = (pcb & ~PCB_SUPERVISOR_RETRANSMIT_MASK) |
  118. PCB_SUPERVISOR_RETRANSMIT_YES;
  119. break;
  120. case PCB_TYPE_DATAFRAME:
  121. skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) |
  122. PCB_DATAFRAME_RETRANSMIT_YES;
  123. break;
  124. default:
  125. pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
  126. kfree_skb(skb);
  127. continue;
  128. }
  129. skb_queue_head(&ndlc->send_q, skb);
  130. }
  131. }
  132. static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc)
  133. {
  134. struct sk_buff *skb;
  135. u8 pcb;
  136. unsigned long time_sent;
  137. if (ndlc->rcv_q.qlen)
  138. pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen);
  139. while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) {
  140. pcb = skb->data[0];
  141. skb_pull(skb, 1);
  142. if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) {
  143. switch (pcb & PCB_SYNC_MASK) {
  144. case PCB_SYNC_ACK:
  145. skb = skb_dequeue(&ndlc->ack_pending_q);
  146. kfree_skb(skb);
  147. del_timer_sync(&ndlc->t1_timer);
  148. del_timer_sync(&ndlc->t2_timer);
  149. ndlc->t2_active = false;
  150. ndlc->t1_active = false;
  151. break;
  152. case PCB_SYNC_NACK:
  153. llt_ndlc_requeue_data_pending(ndlc);
  154. llt_ndlc_send_queue(ndlc);
  155. /* start timer t1 for ndlc aknowledge */
  156. time_sent = jiffies;
  157. ndlc->t1_active = true;
  158. mod_timer(&ndlc->t1_timer, time_sent +
  159. msecs_to_jiffies(NDLC_TIMER_T1));
  160. break;
  161. case PCB_SYNC_WAIT:
  162. time_sent = jiffies;
  163. ndlc->t1_active = true;
  164. mod_timer(&ndlc->t1_timer, time_sent +
  165. msecs_to_jiffies(NDLC_TIMER_T1_WAIT));
  166. break;
  167. default:
  168. kfree_skb(skb);
  169. break;
  170. }
  171. } else if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_DATAFRAME) {
  172. nci_recv_frame(ndlc->ndev, skb);
  173. } else {
  174. kfree_skb(skb);
  175. }
  176. }
  177. }
  178. static void llt_ndlc_sm_work(struct work_struct *work)
  179. {
  180. struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work);
  181. llt_ndlc_send_queue(ndlc);
  182. llt_ndlc_rcv_queue(ndlc);
  183. if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) {
  184. pr_debug
  185. ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
  186. ndlc->t1_active = false;
  187. llt_ndlc_requeue_data_pending(ndlc);
  188. llt_ndlc_send_queue(ndlc);
  189. }
  190. if (ndlc->t2_active && timer_pending(&ndlc->t2_timer) == 0) {
  191. pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n");
  192. ndlc->t2_active = false;
  193. ndlc->t1_active = false;
  194. del_timer_sync(&ndlc->t1_timer);
  195. del_timer_sync(&ndlc->t2_timer);
  196. ndlc_close(ndlc);
  197. ndlc->hard_fault = -EREMOTEIO;
  198. }
  199. }
  200. void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb)
  201. {
  202. if (skb == NULL) {
  203. pr_err("NULL Frame -> link is dead\n");
  204. ndlc->hard_fault = -EREMOTEIO;
  205. ndlc_close(ndlc);
  206. } else {
  207. NDLC_DUMP_SKB("incoming frame", skb);
  208. skb_queue_tail(&ndlc->rcv_q, skb);
  209. }
  210. schedule_work(&ndlc->sm_work);
  211. }
  212. EXPORT_SYMBOL(ndlc_recv);
  213. static void ndlc_t1_timeout(unsigned long data)
  214. {
  215. struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
  216. pr_debug("\n");
  217. schedule_work(&ndlc->sm_work);
  218. }
  219. static void ndlc_t2_timeout(unsigned long data)
  220. {
  221. struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
  222. pr_debug("\n");
  223. schedule_work(&ndlc->sm_work);
  224. }
  225. int ndlc_probe(void *phy_id, struct nfc_phy_ops *phy_ops, struct device *dev,
  226. int phy_headroom, int phy_tailroom, struct llt_ndlc **ndlc_id,
  227. struct st_nci_se_status *se_status)
  228. {
  229. struct llt_ndlc *ndlc;
  230. ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL);
  231. if (!ndlc)
  232. return -ENOMEM;
  233. ndlc->ops = phy_ops;
  234. ndlc->phy_id = phy_id;
  235. ndlc->dev = dev;
  236. ndlc->powered = 0;
  237. *ndlc_id = ndlc;
  238. /* initialize timers */
  239. init_timer(&ndlc->t1_timer);
  240. ndlc->t1_timer.data = (unsigned long)ndlc;
  241. ndlc->t1_timer.function = ndlc_t1_timeout;
  242. init_timer(&ndlc->t2_timer);
  243. ndlc->t2_timer.data = (unsigned long)ndlc;
  244. ndlc->t2_timer.function = ndlc_t2_timeout;
  245. skb_queue_head_init(&ndlc->rcv_q);
  246. skb_queue_head_init(&ndlc->send_q);
  247. skb_queue_head_init(&ndlc->ack_pending_q);
  248. INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work);
  249. return st_nci_probe(ndlc, phy_headroom, phy_tailroom, se_status);
  250. }
  251. EXPORT_SYMBOL(ndlc_probe);
  252. void ndlc_remove(struct llt_ndlc *ndlc)
  253. {
  254. st_nci_remove(ndlc->ndev);
  255. /* cancel timers */
  256. del_timer_sync(&ndlc->t1_timer);
  257. del_timer_sync(&ndlc->t2_timer);
  258. ndlc->t2_active = false;
  259. ndlc->t1_active = false;
  260. skb_queue_purge(&ndlc->rcv_q);
  261. skb_queue_purge(&ndlc->send_q);
  262. }
  263. EXPORT_SYMBOL(ndlc_remove);