ntf.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. * Acknowledgements:
  10. * This file is based on hci_event.c, which was written
  11. * by Maxim Krasnyansky.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <linux/types.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/bitops.h>
  30. #include <linux/skbuff.h>
  31. #include "../nfc.h"
  32. #include <net/nfc/nci.h>
  33. #include <net/nfc/nci_core.h>
  34. #include <linux/nfc.h>
  35. /* Handle NCI Notification packets */
  36. static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
  37. struct sk_buff *skb)
  38. {
  39. struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
  40. int i;
  41. nfc_dbg("entry, num_entries %d", ntf->num_entries);
  42. if (ntf->num_entries > NCI_MAX_NUM_CONN)
  43. ntf->num_entries = NCI_MAX_NUM_CONN;
  44. /* update the credits */
  45. for (i = 0; i < ntf->num_entries; i++) {
  46. nfc_dbg("entry[%d]: conn_id %d, credits %d", i,
  47. ntf->conn_entries[i].conn_id,
  48. ntf->conn_entries[i].credits);
  49. if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) {
  50. /* found static rf connection */
  51. atomic_add(ntf->conn_entries[i].credits,
  52. &ndev->credits_cnt);
  53. }
  54. }
  55. /* trigger the next tx */
  56. if (!skb_queue_empty(&ndev->tx_q))
  57. queue_work(ndev->tx_wq, &ndev->tx_work);
  58. }
  59. static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
  60. struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
  61. {
  62. struct rf_tech_specific_params_nfca_poll *nfca_poll;
  63. nfca_poll = &ntf->rf_tech_specific_params.nfca_poll;
  64. nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data));
  65. data += 2;
  66. nfca_poll->nfcid1_len = *data++;
  67. nfc_dbg("sens_res 0x%x, nfcid1_len %d",
  68. nfca_poll->sens_res,
  69. nfca_poll->nfcid1_len);
  70. memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
  71. data += nfca_poll->nfcid1_len;
  72. nfca_poll->sel_res_len = *data++;
  73. if (nfca_poll->sel_res_len != 0)
  74. nfca_poll->sel_res = *data++;
  75. nfc_dbg("sel_res_len %d, sel_res 0x%x",
  76. nfca_poll->sel_res_len,
  77. nfca_poll->sel_res);
  78. return data;
  79. }
  80. static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
  81. struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
  82. {
  83. struct activation_params_nfca_poll_iso_dep *nfca_poll;
  84. switch (ntf->activation_rf_tech_and_mode) {
  85. case NCI_NFC_A_PASSIVE_POLL_MODE:
  86. nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
  87. nfca_poll->rats_res_len = *data++;
  88. if (nfca_poll->rats_res_len > 0) {
  89. memcpy(nfca_poll->rats_res,
  90. data,
  91. nfca_poll->rats_res_len);
  92. }
  93. break;
  94. default:
  95. nfc_err("unsupported activation_rf_tech_and_mode 0x%x",
  96. ntf->activation_rf_tech_and_mode);
  97. return -EPROTO;
  98. }
  99. return 0;
  100. }
  101. static void nci_target_found(struct nci_dev *ndev,
  102. struct nci_rf_intf_activated_ntf *ntf)
  103. {
  104. struct nfc_target nfc_tgt;
  105. if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T) /* T2T MifareUL */
  106. nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK;
  107. else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP) /* 4A */
  108. nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK;
  109. else
  110. nfc_tgt.supported_protocols = 0;
  111. nfc_tgt.sens_res = ntf->rf_tech_specific_params.nfca_poll.sens_res;
  112. nfc_tgt.sel_res = ntf->rf_tech_specific_params.nfca_poll.sel_res;
  113. if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) {
  114. nfc_dbg("the target found does not have the desired protocol");
  115. return;
  116. }
  117. nfc_dbg("new target found, supported_protocols 0x%x",
  118. nfc_tgt.supported_protocols);
  119. ndev->target_available_prots = nfc_tgt.supported_protocols;
  120. nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1);
  121. }
  122. static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
  123. struct sk_buff *skb)
  124. {
  125. struct nci_rf_intf_activated_ntf ntf;
  126. __u8 *data = skb->data;
  127. int err = 0;
  128. clear_bit(NCI_DISCOVERY, &ndev->flags);
  129. set_bit(NCI_POLL_ACTIVE, &ndev->flags);
  130. ntf.rf_discovery_id = *data++;
  131. ntf.rf_interface_type = *data++;
  132. ntf.rf_protocol = *data++;
  133. ntf.activation_rf_tech_and_mode = *data++;
  134. ntf.rf_tech_specific_params_len = *data++;
  135. nfc_dbg("rf_discovery_id %d", ntf.rf_discovery_id);
  136. nfc_dbg("rf_interface_type 0x%x", ntf.rf_interface_type);
  137. nfc_dbg("rf_protocol 0x%x", ntf.rf_protocol);
  138. nfc_dbg("activation_rf_tech_and_mode 0x%x",
  139. ntf.activation_rf_tech_and_mode);
  140. nfc_dbg("rf_tech_specific_params_len %d",
  141. ntf.rf_tech_specific_params_len);
  142. if (ntf.rf_tech_specific_params_len > 0) {
  143. switch (ntf.activation_rf_tech_and_mode) {
  144. case NCI_NFC_A_PASSIVE_POLL_MODE:
  145. data = nci_extract_rf_params_nfca_passive_poll(ndev,
  146. &ntf, data);
  147. break;
  148. default:
  149. nfc_err("unsupported activation_rf_tech_and_mode 0x%x",
  150. ntf.activation_rf_tech_and_mode);
  151. return;
  152. }
  153. }
  154. ntf.data_exch_rf_tech_and_mode = *data++;
  155. ntf.data_exch_tx_bit_rate = *data++;
  156. ntf.data_exch_rx_bit_rate = *data++;
  157. ntf.activation_params_len = *data++;
  158. nfc_dbg("data_exch_rf_tech_and_mode 0x%x",
  159. ntf.data_exch_rf_tech_and_mode);
  160. nfc_dbg("data_exch_tx_bit_rate 0x%x",
  161. ntf.data_exch_tx_bit_rate);
  162. nfc_dbg("data_exch_rx_bit_rate 0x%x",
  163. ntf.data_exch_rx_bit_rate);
  164. nfc_dbg("activation_params_len %d",
  165. ntf.activation_params_len);
  166. if (ntf.activation_params_len > 0) {
  167. switch (ntf.rf_interface_type) {
  168. case NCI_RF_INTERFACE_ISO_DEP:
  169. err = nci_extract_activation_params_iso_dep(ndev,
  170. &ntf, data);
  171. break;
  172. case NCI_RF_INTERFACE_FRAME:
  173. /* no activation params */
  174. break;
  175. default:
  176. nfc_err("unsupported rf_interface_type 0x%x",
  177. ntf.rf_interface_type);
  178. return;
  179. }
  180. }
  181. if (!err)
  182. nci_target_found(ndev, &ntf);
  183. }
  184. static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
  185. struct sk_buff *skb)
  186. {
  187. struct nci_rf_deactivate_ntf *ntf = (void *) skb->data;
  188. nfc_dbg("entry, type 0x%x, reason 0x%x", ntf->type, ntf->reason);
  189. clear_bit(NCI_POLL_ACTIVE, &ndev->flags);
  190. ndev->target_active_prot = 0;
  191. /* drop tx data queue */
  192. skb_queue_purge(&ndev->tx_q);
  193. /* drop partial rx data packet */
  194. if (ndev->rx_data_reassembly) {
  195. kfree_skb(ndev->rx_data_reassembly);
  196. ndev->rx_data_reassembly = 0;
  197. }
  198. /* set the available credits to initial value */
  199. atomic_set(&ndev->credits_cnt, ndev->initial_num_credits);
  200. /* complete the data exchange transaction, if exists */
  201. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  202. nci_data_exchange_complete(ndev, NULL, -EIO);
  203. }
  204. void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
  205. {
  206. __u16 ntf_opcode = nci_opcode(skb->data);
  207. nfc_dbg("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
  208. nci_pbf(skb->data),
  209. nci_opcode_gid(ntf_opcode),
  210. nci_opcode_oid(ntf_opcode),
  211. nci_plen(skb->data));
  212. /* strip the nci control header */
  213. skb_pull(skb, NCI_CTRL_HDR_SIZE);
  214. switch (ntf_opcode) {
  215. case NCI_OP_CORE_CONN_CREDITS_NTF:
  216. nci_core_conn_credits_ntf_packet(ndev, skb);
  217. break;
  218. case NCI_OP_RF_INTF_ACTIVATED_NTF:
  219. nci_rf_intf_activated_ntf_packet(ndev, skb);
  220. break;
  221. case NCI_OP_RF_DEACTIVATE_NTF:
  222. nci_rf_deactivate_ntf_packet(ndev, skb);
  223. break;
  224. default:
  225. nfc_err("unknown ntf opcode 0x%x", ntf_opcode);
  226. break;
  227. }
  228. kfree_skb(skb);
  229. }