rsp.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  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. /* Handle NCI Response packets */
  35. static void nci_core_reset_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  36. {
  37. struct nci_core_reset_rsp *rsp = (void *) skb->data;
  38. pr_debug("status 0x%x\n", rsp->status);
  39. if (rsp->status == NCI_STATUS_OK) {
  40. ndev->nci_ver = rsp->nci_ver;
  41. pr_debug("nci_ver 0x%x, config_status 0x%x\n",
  42. rsp->nci_ver, rsp->config_status);
  43. }
  44. nci_req_complete(ndev, rsp->status);
  45. }
  46. static void nci_core_init_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  47. {
  48. struct nci_core_init_rsp_1 *rsp_1 = (void *) skb->data;
  49. struct nci_core_init_rsp_2 *rsp_2;
  50. pr_debug("status 0x%x\n", rsp_1->status);
  51. if (rsp_1->status != NCI_STATUS_OK)
  52. goto exit;
  53. ndev->nfcc_features = __le32_to_cpu(rsp_1->nfcc_features);
  54. ndev->num_supported_rf_interfaces = rsp_1->num_supported_rf_interfaces;
  55. if (ndev->num_supported_rf_interfaces >
  56. NCI_MAX_SUPPORTED_RF_INTERFACES) {
  57. ndev->num_supported_rf_interfaces =
  58. NCI_MAX_SUPPORTED_RF_INTERFACES;
  59. }
  60. memcpy(ndev->supported_rf_interfaces,
  61. rsp_1->supported_rf_interfaces,
  62. ndev->num_supported_rf_interfaces);
  63. rsp_2 = (void *) (skb->data + 6 + rsp_1->num_supported_rf_interfaces);
  64. ndev->max_logical_connections = rsp_2->max_logical_connections;
  65. ndev->max_routing_table_size =
  66. __le16_to_cpu(rsp_2->max_routing_table_size);
  67. ndev->max_ctrl_pkt_payload_len =
  68. rsp_2->max_ctrl_pkt_payload_len;
  69. ndev->max_size_for_large_params =
  70. __le16_to_cpu(rsp_2->max_size_for_large_params);
  71. ndev->manufact_id =
  72. rsp_2->manufact_id;
  73. ndev->manufact_specific_info =
  74. __le32_to_cpu(rsp_2->manufact_specific_info);
  75. pr_debug("nfcc_features 0x%x\n",
  76. ndev->nfcc_features);
  77. pr_debug("num_supported_rf_interfaces %d\n",
  78. ndev->num_supported_rf_interfaces);
  79. pr_debug("supported_rf_interfaces[0] 0x%x\n",
  80. ndev->supported_rf_interfaces[0]);
  81. pr_debug("supported_rf_interfaces[1] 0x%x\n",
  82. ndev->supported_rf_interfaces[1]);
  83. pr_debug("supported_rf_interfaces[2] 0x%x\n",
  84. ndev->supported_rf_interfaces[2]);
  85. pr_debug("supported_rf_interfaces[3] 0x%x\n",
  86. ndev->supported_rf_interfaces[3]);
  87. pr_debug("max_logical_connections %d\n",
  88. ndev->max_logical_connections);
  89. pr_debug("max_routing_table_size %d\n",
  90. ndev->max_routing_table_size);
  91. pr_debug("max_ctrl_pkt_payload_len %d\n",
  92. ndev->max_ctrl_pkt_payload_len);
  93. pr_debug("max_size_for_large_params %d\n",
  94. ndev->max_size_for_large_params);
  95. pr_debug("manufact_id 0x%x\n",
  96. ndev->manufact_id);
  97. pr_debug("manufact_specific_info 0x%x\n",
  98. ndev->manufact_specific_info);
  99. exit:
  100. nci_req_complete(ndev, rsp_1->status);
  101. }
  102. static void nci_core_set_config_rsp_packet(struct nci_dev *ndev,
  103. struct sk_buff *skb)
  104. {
  105. struct nci_core_set_config_rsp *rsp = (void *) skb->data;
  106. pr_debug("status 0x%x\n", rsp->status);
  107. nci_req_complete(ndev, rsp->status);
  108. }
  109. static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev,
  110. struct sk_buff *skb)
  111. {
  112. __u8 status = skb->data[0];
  113. pr_debug("status 0x%x\n", status);
  114. nci_req_complete(ndev, status);
  115. }
  116. static void nci_rf_disc_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  117. {
  118. __u8 status = skb->data[0];
  119. pr_debug("status 0x%x\n", status);
  120. if (status == NCI_STATUS_OK)
  121. atomic_set(&ndev->state, NCI_DISCOVERY);
  122. nci_req_complete(ndev, status);
  123. }
  124. static void nci_rf_disc_select_rsp_packet(struct nci_dev *ndev,
  125. struct sk_buff *skb)
  126. {
  127. __u8 status = skb->data[0];
  128. pr_debug("status 0x%x\n", status);
  129. /* Complete the request on intf_activated_ntf or generic_error_ntf */
  130. if (status != NCI_STATUS_OK)
  131. nci_req_complete(ndev, status);
  132. }
  133. static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
  134. struct sk_buff *skb)
  135. {
  136. __u8 status = skb->data[0];
  137. pr_debug("status 0x%x\n", status);
  138. /* If target was active, complete the request only in deactivate_ntf */
  139. if ((status != NCI_STATUS_OK) ||
  140. (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
  141. nci_clear_target_list(ndev);
  142. atomic_set(&ndev->state, NCI_IDLE);
  143. nci_req_complete(ndev, status);
  144. }
  145. }
  146. void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  147. {
  148. __u16 rsp_opcode = nci_opcode(skb->data);
  149. /* we got a rsp, stop the cmd timer */
  150. del_timer(&ndev->cmd_timer);
  151. pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  152. nci_pbf(skb->data),
  153. nci_opcode_gid(rsp_opcode),
  154. nci_opcode_oid(rsp_opcode),
  155. nci_plen(skb->data));
  156. /* strip the nci control header */
  157. skb_pull(skb, NCI_CTRL_HDR_SIZE);
  158. switch (rsp_opcode) {
  159. case NCI_OP_CORE_RESET_RSP:
  160. nci_core_reset_rsp_packet(ndev, skb);
  161. break;
  162. case NCI_OP_CORE_INIT_RSP:
  163. nci_core_init_rsp_packet(ndev, skb);
  164. break;
  165. case NCI_OP_CORE_SET_CONFIG_RSP:
  166. nci_core_set_config_rsp_packet(ndev, skb);
  167. break;
  168. case NCI_OP_RF_DISCOVER_MAP_RSP:
  169. nci_rf_disc_map_rsp_packet(ndev, skb);
  170. break;
  171. case NCI_OP_RF_DISCOVER_RSP:
  172. nci_rf_disc_rsp_packet(ndev, skb);
  173. break;
  174. case NCI_OP_RF_DISCOVER_SELECT_RSP:
  175. nci_rf_disc_select_rsp_packet(ndev, skb);
  176. break;
  177. case NCI_OP_RF_DEACTIVATE_RSP:
  178. nci_rf_deactivate_rsp_packet(ndev, skb);
  179. break;
  180. default:
  181. pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
  182. break;
  183. }
  184. kfree_skb(skb);
  185. /* trigger the next cmd */
  186. atomic_set(&ndev->cmd_cnt, 1);
  187. if (!skb_queue_empty(&ndev->cmd_q))
  188. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  189. }