data.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. *
  7. * Written by Ilan Elias <ilane@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  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. int err)
  36. {
  37. data_exchange_cb_t cb = ndev->data_exchange_cb;
  38. void *cb_context = ndev->data_exchange_cb_context;
  39. pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
  40. /* data exchange is complete, stop the data timer */
  41. del_timer_sync(&ndev->data_timer);
  42. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  43. if (cb) {
  44. ndev->data_exchange_cb = NULL;
  45. ndev->data_exchange_cb_context = NULL;
  46. /* forward skb to nfc core */
  47. cb(cb_context, skb, err);
  48. } else if (skb) {
  49. pr_err("no rx callback, dropping rx data...\n");
  50. /* no waiting callback, free skb */
  51. kfree_skb(skb);
  52. }
  53. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  54. }
  55. /* ----------------- NCI TX Data ----------------- */
  56. static inline void nci_push_data_hdr(struct nci_dev *ndev,
  57. __u8 conn_id,
  58. struct sk_buff *skb,
  59. __u8 pbf)
  60. {
  61. struct nci_data_hdr *hdr;
  62. int plen = skb->len;
  63. hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
  64. hdr->conn_id = conn_id;
  65. hdr->rfu = 0;
  66. hdr->plen = plen;
  67. nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
  68. nci_pbf_set((__u8 *)hdr, pbf);
  69. }
  70. static int nci_queue_tx_data_frags(struct nci_dev *ndev,
  71. __u8 conn_id,
  72. struct sk_buff *skb) {
  73. int total_len = skb->len;
  74. unsigned char *data = skb->data;
  75. unsigned long flags;
  76. struct sk_buff_head frags_q;
  77. struct sk_buff *skb_frag;
  78. int frag_len;
  79. int rc = 0;
  80. pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
  81. __skb_queue_head_init(&frags_q);
  82. while (total_len) {
  83. frag_len =
  84. min_t(int, total_len, ndev->max_data_pkt_payload_size);
  85. skb_frag = nci_skb_alloc(ndev,
  86. (NCI_DATA_HDR_SIZE + frag_len),
  87. GFP_KERNEL);
  88. if (skb_frag == NULL) {
  89. rc = -ENOMEM;
  90. goto free_exit;
  91. }
  92. skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
  93. /* first, copy the data */
  94. memcpy(skb_put(skb_frag, frag_len), data, frag_len);
  95. /* second, set the header */
  96. nci_push_data_hdr(ndev, conn_id, skb_frag,
  97. ((total_len == frag_len) ?
  98. (NCI_PBF_LAST) : (NCI_PBF_CONT)));
  99. __skb_queue_tail(&frags_q, skb_frag);
  100. data += frag_len;
  101. total_len -= frag_len;
  102. pr_debug("frag_len %d, remaining total_len %d\n",
  103. frag_len, total_len);
  104. }
  105. /* queue all fragments atomically */
  106. spin_lock_irqsave(&ndev->tx_q.lock, flags);
  107. while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
  108. __skb_queue_tail(&ndev->tx_q, skb_frag);
  109. spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
  110. /* free the original skb */
  111. kfree_skb(skb);
  112. goto exit;
  113. free_exit:
  114. while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
  115. kfree_skb(skb_frag);
  116. exit:
  117. return rc;
  118. }
  119. /* Send NCI data */
  120. int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
  121. {
  122. int rc = 0;
  123. pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
  124. /* check if the packet need to be fragmented */
  125. if (skb->len <= ndev->max_data_pkt_payload_size) {
  126. /* no need to fragment packet */
  127. nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
  128. skb_queue_tail(&ndev->tx_q, skb);
  129. } else {
  130. /* fragment packet and queue the fragments */
  131. rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
  132. if (rc) {
  133. pr_err("failed to fragment tx data packet\n");
  134. goto free_exit;
  135. }
  136. }
  137. queue_work(ndev->tx_wq, &ndev->tx_work);
  138. goto exit;
  139. free_exit:
  140. kfree_skb(skb);
  141. exit:
  142. return rc;
  143. }
  144. /* ----------------- NCI RX Data ----------------- */
  145. static void nci_add_rx_data_frag(struct nci_dev *ndev,
  146. struct sk_buff *skb,
  147. __u8 pbf)
  148. {
  149. int reassembly_len;
  150. int err = 0;
  151. if (ndev->rx_data_reassembly) {
  152. reassembly_len = ndev->rx_data_reassembly->len;
  153. /* first, make enough room for the already accumulated data */
  154. if (skb_cow_head(skb, reassembly_len)) {
  155. pr_err("error adding room for accumulated rx data\n");
  156. kfree_skb(skb);
  157. skb = NULL;
  158. kfree_skb(ndev->rx_data_reassembly);
  159. ndev->rx_data_reassembly = NULL;
  160. err = -ENOMEM;
  161. goto exit;
  162. }
  163. /* second, combine the two fragments */
  164. memcpy(skb_push(skb, reassembly_len),
  165. ndev->rx_data_reassembly->data,
  166. reassembly_len);
  167. /* third, free old reassembly */
  168. kfree_skb(ndev->rx_data_reassembly);
  169. ndev->rx_data_reassembly = NULL;
  170. }
  171. if (pbf == NCI_PBF_CONT) {
  172. /* need to wait for next fragment, store skb and exit */
  173. ndev->rx_data_reassembly = skb;
  174. return;
  175. }
  176. exit:
  177. nci_data_exchange_complete(ndev, skb, err);
  178. }
  179. /* Rx Data packet */
  180. void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
  181. {
  182. __u8 pbf = nci_pbf(skb->data);
  183. pr_debug("len %d\n", skb->len);
  184. pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  185. nci_pbf(skb->data),
  186. nci_conn_id(skb->data),
  187. nci_plen(skb->data));
  188. /* strip the nci data header */
  189. skb_pull(skb, NCI_DATA_HDR_SIZE);
  190. if (ndev->target_active_prot == NFC_PROTO_MIFARE) {
  191. /* frame I/F => remove the status byte */
  192. pr_debug("NFC_PROTO_MIFARE => remove the status byte\n");
  193. skb_trim(skb, (skb->len - 1));
  194. }
  195. nci_add_rx_data_frag(ndev, skb, pbf);
  196. }