data.c 5.9 KB

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