data.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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,
  35. struct sk_buff *skb,
  36. int err)
  37. {
  38. data_exchange_cb_t cb = ndev->data_exchange_cb;
  39. void *cb_context = ndev->data_exchange_cb_context;
  40. pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
  41. if (cb) {
  42. ndev->data_exchange_cb = NULL;
  43. ndev->data_exchange_cb_context = 0;
  44. /* forward skb to nfc core */
  45. cb(cb_context, skb, err);
  46. } else if (skb) {
  47. pr_err("no rx callback, dropping rx data...\n");
  48. /* no waiting callback, free skb */
  49. kfree_skb(skb);
  50. }
  51. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  52. }
  53. /* ----------------- NCI TX Data ----------------- */
  54. static inline void nci_push_data_hdr(struct nci_dev *ndev,
  55. __u8 conn_id,
  56. struct sk_buff *skb,
  57. __u8 pbf)
  58. {
  59. struct nci_data_hdr *hdr;
  60. int plen = skb->len;
  61. hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
  62. hdr->conn_id = conn_id;
  63. hdr->rfu = 0;
  64. hdr->plen = plen;
  65. nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
  66. nci_pbf_set((__u8 *)hdr, pbf);
  67. skb->dev = (void *) ndev;
  68. }
  69. static int nci_queue_tx_data_frags(struct nci_dev *ndev,
  70. __u8 conn_id,
  71. struct sk_buff *skb) {
  72. int total_len = skb->len;
  73. unsigned char *data = skb->data;
  74. unsigned long flags;
  75. struct sk_buff_head frags_q;
  76. struct sk_buff *skb_frag;
  77. int frag_len;
  78. int rc = 0;
  79. pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
  80. __skb_queue_head_init(&frags_q);
  81. while (total_len) {
  82. frag_len =
  83. min_t(int, total_len, ndev->max_data_pkt_payload_size);
  84. skb_frag = nci_skb_alloc(ndev,
  85. (NCI_DATA_HDR_SIZE + frag_len),
  86. GFP_KERNEL);
  87. if (skb_frag == NULL) {
  88. rc = -ENOMEM;
  89. goto free_exit;
  90. }
  91. skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
  92. /* first, copy the data */
  93. memcpy(skb_put(skb_frag, frag_len), data, frag_len);
  94. /* second, set the header */
  95. nci_push_data_hdr(ndev, conn_id, skb_frag,
  96. ((total_len == frag_len) ? (NCI_PBF_LAST) : (NCI_PBF_CONT)));
  97. __skb_queue_tail(&frags_q, skb_frag);
  98. data += frag_len;
  99. total_len -= frag_len;
  100. pr_debug("frag_len %d, remaining total_len %d\n",
  101. frag_len, total_len);
  102. }
  103. /* queue all fragments atomically */
  104. spin_lock_irqsave(&ndev->tx_q.lock, flags);
  105. while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
  106. __skb_queue_tail(&ndev->tx_q, skb_frag);
  107. spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
  108. /* free the original skb */
  109. kfree_skb(skb);
  110. goto exit;
  111. free_exit:
  112. while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
  113. kfree_skb(skb_frag);
  114. exit:
  115. return rc;
  116. }
  117. /* Send NCI data */
  118. int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
  119. {
  120. int rc = 0;
  121. pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
  122. /* check if the packet need to be fragmented */
  123. if (skb->len <= ndev->max_data_pkt_payload_size) {
  124. /* no need to fragment packet */
  125. nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
  126. skb_queue_tail(&ndev->tx_q, skb);
  127. } else {
  128. /* fragment packet and queue the fragments */
  129. rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
  130. if (rc) {
  131. pr_err("failed to fragment tx data packet\n");
  132. goto free_exit;
  133. }
  134. }
  135. queue_work(ndev->tx_wq, &ndev->tx_work);
  136. goto exit;
  137. free_exit:
  138. kfree_skb(skb);
  139. exit:
  140. return rc;
  141. }
  142. /* ----------------- NCI RX Data ----------------- */
  143. static void nci_add_rx_data_frag(struct nci_dev *ndev,
  144. struct sk_buff *skb,
  145. __u8 pbf)
  146. {
  147. int reassembly_len;
  148. int err = 0;
  149. if (ndev->rx_data_reassembly) {
  150. reassembly_len = ndev->rx_data_reassembly->len;
  151. /* first, make enough room for the already accumulated data */
  152. if (skb_cow_head(skb, reassembly_len)) {
  153. pr_err("error adding room for accumulated rx data\n");
  154. kfree_skb(skb);
  155. skb = 0;
  156. kfree_skb(ndev->rx_data_reassembly);
  157. ndev->rx_data_reassembly = 0;
  158. err = -ENOMEM;
  159. goto exit;
  160. }
  161. /* second, combine the two fragments */
  162. memcpy(skb_push(skb, reassembly_len),
  163. ndev->rx_data_reassembly->data,
  164. reassembly_len);
  165. /* third, free old reassembly */
  166. kfree_skb(ndev->rx_data_reassembly);
  167. ndev->rx_data_reassembly = 0;
  168. }
  169. if (pbf == NCI_PBF_CONT) {
  170. /* need to wait for next fragment, store skb and exit */
  171. ndev->rx_data_reassembly = skb;
  172. return;
  173. }
  174. exit:
  175. nci_data_exchange_complete(ndev, skb, err);
  176. }
  177. /* Rx Data packet */
  178. void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
  179. {
  180. __u8 pbf = nci_pbf(skb->data);
  181. pr_debug("len %d\n", skb->len);
  182. pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  183. nci_pbf(skb->data),
  184. nci_conn_id(skb->data),
  185. nci_plen(skb->data));
  186. /* strip the nci data header */
  187. skb_pull(skb, NCI_DATA_HDR_SIZE);
  188. if (ndev->target_active_prot == NFC_PROTO_MIFARE) {
  189. /* frame I/F => remove the status byte */
  190. pr_debug("NFC_PROTO_MIFARE => remove the status byte\n");
  191. skb_trim(skb, (skb->len - 1));
  192. }
  193. nci_add_rx_data_frag(ndev, skb, pbf);
  194. }