ndlc.c 7.9 KB

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