data.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * The NFC Controller Interface is the communication protocol between an
  3. * NFC Controller (NFCC) and a Device Host (DH).
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2014 Marvell International Ltd.
  7. *
  8. * Written by Ilan Elias <ilane@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  24. #include <linux/types.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/wait.h>
  27. #include <linux/bitops.h>
  28. #include <linux/skbuff.h>
  29. #include "../nfc.h"
  30. #include <net/nfc/nci.h>
  31. #include <net/nfc/nci_core.h>
  32. #include <linux/nfc.h>
  33. /* Complete data exchange transaction and forward skb to nfc core */
  34. void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
  35. __u8 conn_id, int err)
  36. {
  37. struct nci_conn_info *conn_info;
  38. data_exchange_cb_t cb;
  39. void *cb_context;
  40. conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
  41. if (!conn_info) {
  42. kfree_skb(skb);
  43. goto exit;
  44. }
  45. cb = conn_info->data_exchange_cb;
  46. cb_context = conn_info->data_exchange_cb_context;
  47. pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
  48. /* data exchange is complete, stop the data timer */
  49. del_timer_sync(&ndev->data_timer);
  50. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  51. if (cb) {
  52. /* forward skb to nfc core */
  53. cb(cb_context, skb, err);
  54. } else if (skb) {
  55. pr_err("no rx callback, dropping rx data...\n");
  56. /* no waiting callback, free skb */
  57. kfree_skb(skb);
  58. }
  59. exit:
  60. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  61. }
  62. /* ----------------- NCI TX Data ----------------- */
  63. static inline void nci_push_data_hdr(struct nci_dev *ndev,
  64. __u8 conn_id,
  65. struct sk_buff *skb,
  66. __u8 pbf)
  67. {
  68. struct nci_data_hdr *hdr;
  69. int plen = skb->len;
  70. hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
  71. hdr->conn_id = conn_id;
  72. hdr->rfu = 0;
  73. hdr->plen = plen;
  74. nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
  75. nci_pbf_set((__u8 *)hdr, pbf);
  76. }
  77. int nci_conn_max_data_pkt_payload_size(struct nci_dev *ndev, __u8 conn_id)
  78. {
  79. struct nci_conn_info *conn_info;
  80. conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
  81. if (!conn_info)
  82. return -EPROTO;
  83. return conn_info->max_pkt_payload_len;
  84. }
  85. EXPORT_SYMBOL(nci_conn_max_data_pkt_payload_size);
  86. static int nci_queue_tx_data_frags(struct nci_dev *ndev,
  87. __u8 conn_id,
  88. struct sk_buff *skb) {
  89. struct nci_conn_info *conn_info;
  90. int total_len = skb->len;
  91. unsigned char *data = skb->data;
  92. unsigned long flags;
  93. struct sk_buff_head frags_q;
  94. struct sk_buff *skb_frag;
  95. int frag_len;
  96. int rc = 0;
  97. pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
  98. conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
  99. if (!conn_info) {
  100. rc = -EPROTO;
  101. goto free_exit;
  102. }
  103. __skb_queue_head_init(&frags_q);
  104. while (total_len) {
  105. frag_len =
  106. min_t(int, total_len, conn_info->max_pkt_payload_len);
  107. skb_frag = nci_skb_alloc(ndev,
  108. (NCI_DATA_HDR_SIZE + frag_len),
  109. GFP_KERNEL);
  110. if (skb_frag == NULL) {
  111. rc = -ENOMEM;
  112. goto free_exit;
  113. }
  114. skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
  115. /* first, copy the data */
  116. memcpy(skb_put(skb_frag, frag_len), data, frag_len);
  117. /* second, set the header */
  118. nci_push_data_hdr(ndev, conn_id, skb_frag,
  119. ((total_len == frag_len) ?
  120. (NCI_PBF_LAST) : (NCI_PBF_CONT)));
  121. __skb_queue_tail(&frags_q, skb_frag);
  122. data += frag_len;
  123. total_len -= frag_len;
  124. pr_debug("frag_len %d, remaining total_len %d\n",
  125. frag_len, total_len);
  126. }
  127. /* queue all fragments atomically */
  128. spin_lock_irqsave(&ndev->tx_q.lock, flags);
  129. while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
  130. __skb_queue_tail(&ndev->tx_q, skb_frag);
  131. spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
  132. /* free the original skb */
  133. kfree_skb(skb);
  134. goto exit;
  135. free_exit:
  136. while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
  137. kfree_skb(skb_frag);
  138. exit:
  139. return rc;
  140. }
  141. /* Send NCI data */
  142. int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
  143. {
  144. struct nci_conn_info *conn_info;
  145. int rc = 0;
  146. pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
  147. conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
  148. if (!conn_info) {
  149. rc = -EPROTO;
  150. goto free_exit;
  151. }
  152. /* check if the packet need to be fragmented */
  153. if (skb->len <= conn_info->max_pkt_payload_len) {
  154. /* no need to fragment packet */
  155. nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
  156. skb_queue_tail(&ndev->tx_q, skb);
  157. } else {
  158. /* fragment packet and queue the fragments */
  159. rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
  160. if (rc) {
  161. pr_err("failed to fragment tx data packet\n");
  162. goto free_exit;
  163. }
  164. }
  165. ndev->cur_conn_id = conn_id;
  166. queue_work(ndev->tx_wq, &ndev->tx_work);
  167. goto exit;
  168. free_exit:
  169. kfree_skb(skb);
  170. exit:
  171. return rc;
  172. }
  173. EXPORT_SYMBOL(nci_send_data);
  174. /* ----------------- NCI RX Data ----------------- */
  175. static void nci_add_rx_data_frag(struct nci_dev *ndev,
  176. struct sk_buff *skb,
  177. __u8 pbf, __u8 conn_id, __u8 status)
  178. {
  179. int reassembly_len;
  180. int err = 0;
  181. if (status) {
  182. err = status;
  183. goto exit;
  184. }
  185. if (ndev->rx_data_reassembly) {
  186. reassembly_len = ndev->rx_data_reassembly->len;
  187. /* first, make enough room for the already accumulated data */
  188. if (skb_cow_head(skb, reassembly_len)) {
  189. pr_err("error adding room for accumulated rx data\n");
  190. kfree_skb(skb);
  191. skb = NULL;
  192. kfree_skb(ndev->rx_data_reassembly);
  193. ndev->rx_data_reassembly = NULL;
  194. err = -ENOMEM;
  195. goto exit;
  196. }
  197. /* second, combine the two fragments */
  198. memcpy(skb_push(skb, reassembly_len),
  199. ndev->rx_data_reassembly->data,
  200. reassembly_len);
  201. /* third, free old reassembly */
  202. kfree_skb(ndev->rx_data_reassembly);
  203. ndev->rx_data_reassembly = NULL;
  204. }
  205. if (pbf == NCI_PBF_CONT) {
  206. /* need to wait for next fragment, store skb and exit */
  207. ndev->rx_data_reassembly = skb;
  208. return;
  209. }
  210. exit:
  211. if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
  212. /* Data received in Target mode, forward to nfc core */
  213. err = nfc_tm_data_received(ndev->nfc_dev, skb);
  214. if (err)
  215. pr_err("unable to handle received data\n");
  216. } else {
  217. nci_data_exchange_complete(ndev, skb, conn_id, err);
  218. }
  219. }
  220. /* Rx Data packet */
  221. void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
  222. {
  223. __u8 pbf = nci_pbf(skb->data);
  224. __u8 status = 0;
  225. __u8 conn_id = nci_conn_id(skb->data);
  226. struct nci_conn_info *conn_info;
  227. pr_debug("len %d\n", skb->len);
  228. pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  229. nci_pbf(skb->data),
  230. nci_conn_id(skb->data),
  231. nci_plen(skb->data));
  232. conn_info = nci_get_conn_info_by_conn_id(ndev, nci_conn_id(skb->data));
  233. if (!conn_info)
  234. return;
  235. /* strip the nci data header */
  236. skb_pull(skb, NCI_DATA_HDR_SIZE);
  237. if (ndev->target_active_prot == NFC_PROTO_MIFARE ||
  238. ndev->target_active_prot == NFC_PROTO_JEWEL ||
  239. ndev->target_active_prot == NFC_PROTO_FELICA ||
  240. ndev->target_active_prot == NFC_PROTO_ISO15693) {
  241. /* frame I/F => remove the status byte */
  242. pr_debug("frame I/F => remove the status byte\n");
  243. status = skb->data[skb->len - 1];
  244. skb_trim(skb, (skb->len - 1));
  245. }
  246. nci_add_rx_data_frag(ndev, skb, pbf, conn_id, nci_to_errno(status));
  247. }