ntf.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  28. #include <linux/types.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/bitops.h>
  31. #include <linux/skbuff.h>
  32. #include "../nfc.h"
  33. #include <net/nfc/nci.h>
  34. #include <net/nfc/nci_core.h>
  35. #include <linux/nfc.h>
  36. /* Handle NCI Notification packets */
  37. static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
  38. struct sk_buff *skb)
  39. {
  40. struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
  41. int i;
  42. pr_debug("num_entries %d\n", ntf->num_entries);
  43. if (ntf->num_entries > NCI_MAX_NUM_CONN)
  44. ntf->num_entries = NCI_MAX_NUM_CONN;
  45. /* update the credits */
  46. for (i = 0; i < ntf->num_entries; i++) {
  47. ntf->conn_entries[i].conn_id =
  48. nci_conn_id(&ntf->conn_entries[i].conn_id);
  49. pr_debug("entry[%d]: conn_id %d, credits %d\n",
  50. i, ntf->conn_entries[i].conn_id,
  51. ntf->conn_entries[i].credits);
  52. if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) {
  53. /* found static rf connection */
  54. atomic_add(ntf->conn_entries[i].credits,
  55. &ndev->credits_cnt);
  56. }
  57. }
  58. /* trigger the next tx */
  59. if (!skb_queue_empty(&ndev->tx_q))
  60. queue_work(ndev->tx_wq, &ndev->tx_work);
  61. }
  62. static void nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
  63. struct sk_buff *skb)
  64. {
  65. struct nci_core_intf_error_ntf *ntf = (void *) skb->data;
  66. ntf->conn_id = nci_conn_id(&ntf->conn_id);
  67. pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
  68. /* complete the data exchange transaction, if exists */
  69. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  70. nci_data_exchange_complete(ndev, NULL, -EIO);
  71. }
  72. static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
  73. struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
  74. {
  75. struct rf_tech_specific_params_nfca_poll *nfca_poll;
  76. nfca_poll = &ntf->rf_tech_specific_params.nfca_poll;
  77. nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data));
  78. data += 2;
  79. nfca_poll->nfcid1_len = *data++;
  80. pr_debug("sens_res 0x%x, nfcid1_len %d\n",
  81. nfca_poll->sens_res, nfca_poll->nfcid1_len);
  82. memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
  83. data += nfca_poll->nfcid1_len;
  84. nfca_poll->sel_res_len = *data++;
  85. if (nfca_poll->sel_res_len != 0)
  86. nfca_poll->sel_res = *data++;
  87. pr_debug("sel_res_len %d, sel_res 0x%x\n",
  88. nfca_poll->sel_res_len,
  89. nfca_poll->sel_res);
  90. return data;
  91. }
  92. static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
  93. struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
  94. {
  95. struct activation_params_nfca_poll_iso_dep *nfca_poll;
  96. switch (ntf->activation_rf_tech_and_mode) {
  97. case NCI_NFC_A_PASSIVE_POLL_MODE:
  98. nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
  99. nfca_poll->rats_res_len = *data++;
  100. if (nfca_poll->rats_res_len > 0) {
  101. memcpy(nfca_poll->rats_res,
  102. data,
  103. nfca_poll->rats_res_len);
  104. }
  105. break;
  106. default:
  107. pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
  108. ntf->activation_rf_tech_and_mode);
  109. return -EPROTO;
  110. }
  111. return 0;
  112. }
  113. static void nci_target_found(struct nci_dev *ndev,
  114. struct nci_rf_intf_activated_ntf *ntf)
  115. {
  116. struct nfc_target nfc_tgt;
  117. if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T) /* T2T MifareUL */
  118. nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK;
  119. else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP) /* 4A */
  120. nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK;
  121. else
  122. nfc_tgt.supported_protocols = 0;
  123. nfc_tgt.sens_res = ntf->rf_tech_specific_params.nfca_poll.sens_res;
  124. nfc_tgt.sel_res = ntf->rf_tech_specific_params.nfca_poll.sel_res;
  125. nfc_tgt.nfcid1_len = ntf->rf_tech_specific_params.nfca_poll.nfcid1_len;
  126. if (nfc_tgt.nfcid1_len > 0) {
  127. memcpy(nfc_tgt.nfcid1,
  128. ntf->rf_tech_specific_params.nfca_poll.nfcid1,
  129. nfc_tgt.nfcid1_len);
  130. }
  131. if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) {
  132. pr_debug("the target found does not have the desired protocol\n");
  133. return;
  134. }
  135. pr_debug("new target found, supported_protocols 0x%x\n",
  136. nfc_tgt.supported_protocols);
  137. ndev->target_available_prots = nfc_tgt.supported_protocols;
  138. ndev->max_data_pkt_payload_size = ntf->max_data_pkt_payload_size;
  139. ndev->initial_num_credits = ntf->initial_num_credits;
  140. /* set the available credits to initial value */
  141. atomic_set(&ndev->credits_cnt, ndev->initial_num_credits);
  142. nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1);
  143. }
  144. static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
  145. struct sk_buff *skb)
  146. {
  147. struct nci_rf_intf_activated_ntf ntf;
  148. __u8 *data = skb->data;
  149. int err = 0;
  150. clear_bit(NCI_DISCOVERY, &ndev->flags);
  151. set_bit(NCI_POLL_ACTIVE, &ndev->flags);
  152. ntf.rf_discovery_id = *data++;
  153. ntf.rf_interface = *data++;
  154. ntf.rf_protocol = *data++;
  155. ntf.activation_rf_tech_and_mode = *data++;
  156. ntf.max_data_pkt_payload_size = *data++;
  157. ntf.initial_num_credits = *data++;
  158. ntf.rf_tech_specific_params_len = *data++;
  159. pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
  160. pr_debug("rf_interface 0x%x\n", ntf.rf_interface);
  161. pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
  162. pr_debug("activation_rf_tech_and_mode 0x%x\n",
  163. ntf.activation_rf_tech_and_mode);
  164. pr_debug("max_data_pkt_payload_size 0x%x\n",
  165. ntf.max_data_pkt_payload_size);
  166. pr_debug("initial_num_credits 0x%x\n", ntf.initial_num_credits);
  167. pr_debug("rf_tech_specific_params_len %d\n",
  168. ntf.rf_tech_specific_params_len);
  169. if (ntf.rf_tech_specific_params_len > 0) {
  170. switch (ntf.activation_rf_tech_and_mode) {
  171. case NCI_NFC_A_PASSIVE_POLL_MODE:
  172. data = nci_extract_rf_params_nfca_passive_poll(ndev,
  173. &ntf, data);
  174. break;
  175. default:
  176. pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
  177. ntf.activation_rf_tech_and_mode);
  178. return;
  179. }
  180. }
  181. ntf.data_exch_rf_tech_and_mode = *data++;
  182. ntf.data_exch_tx_bit_rate = *data++;
  183. ntf.data_exch_rx_bit_rate = *data++;
  184. ntf.activation_params_len = *data++;
  185. pr_debug("data_exch_rf_tech_and_mode 0x%x\n",
  186. ntf.data_exch_rf_tech_and_mode);
  187. pr_debug("data_exch_tx_bit_rate 0x%x\n",
  188. ntf.data_exch_tx_bit_rate);
  189. pr_debug("data_exch_rx_bit_rate 0x%x\n",
  190. ntf.data_exch_rx_bit_rate);
  191. pr_debug("activation_params_len %d\n",
  192. ntf.activation_params_len);
  193. if (ntf.activation_params_len > 0) {
  194. switch (ntf.rf_interface) {
  195. case NCI_RF_INTERFACE_ISO_DEP:
  196. err = nci_extract_activation_params_iso_dep(ndev,
  197. &ntf, data);
  198. break;
  199. case NCI_RF_INTERFACE_FRAME:
  200. /* no activation params */
  201. break;
  202. default:
  203. pr_err("unsupported rf_interface 0x%x\n",
  204. ntf.rf_interface);
  205. return;
  206. }
  207. }
  208. if (!err)
  209. nci_target_found(ndev, &ntf);
  210. }
  211. static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
  212. struct sk_buff *skb)
  213. {
  214. struct nci_rf_deactivate_ntf *ntf = (void *) skb->data;
  215. pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason);
  216. clear_bit(NCI_POLL_ACTIVE, &ndev->flags);
  217. ndev->target_active_prot = 0;
  218. /* drop tx data queue */
  219. skb_queue_purge(&ndev->tx_q);
  220. /* drop partial rx data packet */
  221. if (ndev->rx_data_reassembly) {
  222. kfree_skb(ndev->rx_data_reassembly);
  223. ndev->rx_data_reassembly = 0;
  224. }
  225. /* complete the data exchange transaction, if exists */
  226. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  227. nci_data_exchange_complete(ndev, NULL, -EIO);
  228. }
  229. void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
  230. {
  231. __u16 ntf_opcode = nci_opcode(skb->data);
  232. pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  233. nci_pbf(skb->data),
  234. nci_opcode_gid(ntf_opcode),
  235. nci_opcode_oid(ntf_opcode),
  236. nci_plen(skb->data));
  237. /* strip the nci control header */
  238. skb_pull(skb, NCI_CTRL_HDR_SIZE);
  239. switch (ntf_opcode) {
  240. case NCI_OP_CORE_CONN_CREDITS_NTF:
  241. nci_core_conn_credits_ntf_packet(ndev, skb);
  242. break;
  243. case NCI_OP_CORE_INTF_ERROR_NTF:
  244. nci_core_conn_intf_error_ntf_packet(ndev, skb);
  245. break;
  246. case NCI_OP_RF_INTF_ACTIVATED_NTF:
  247. nci_rf_intf_activated_ntf_packet(ndev, skb);
  248. break;
  249. case NCI_OP_RF_DEACTIVATE_NTF:
  250. nci_rf_deactivate_ntf_packet(ndev, skb);
  251. break;
  252. default:
  253. pr_err("unknown ntf opcode 0x%x\n", ntf_opcode);
  254. break;
  255. }
  256. kfree_skb(skb);
  257. }