rsp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /* Handle NCI Response packets */
  36. static void nci_core_reset_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  37. {
  38. struct nci_core_reset_rsp *rsp = (void *) skb->data;
  39. pr_debug("status 0x%x\n", rsp->status);
  40. if (rsp->status == NCI_STATUS_OK) {
  41. ndev->nci_ver = rsp->nci_ver;
  42. pr_debug("nci_ver 0x%x, config_status 0x%x\n",
  43. rsp->nci_ver, rsp->config_status);
  44. }
  45. nci_req_complete(ndev, rsp->status);
  46. }
  47. static void nci_core_init_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  48. {
  49. struct nci_core_init_rsp_1 *rsp_1 = (void *) skb->data;
  50. struct nci_core_init_rsp_2 *rsp_2;
  51. pr_debug("status 0x%x\n", rsp_1->status);
  52. if (rsp_1->status != NCI_STATUS_OK)
  53. goto exit;
  54. ndev->nfcc_features = __le32_to_cpu(rsp_1->nfcc_features);
  55. ndev->num_supported_rf_interfaces = rsp_1->num_supported_rf_interfaces;
  56. if (ndev->num_supported_rf_interfaces >
  57. NCI_MAX_SUPPORTED_RF_INTERFACES) {
  58. ndev->num_supported_rf_interfaces =
  59. NCI_MAX_SUPPORTED_RF_INTERFACES;
  60. }
  61. memcpy(ndev->supported_rf_interfaces,
  62. rsp_1->supported_rf_interfaces,
  63. ndev->num_supported_rf_interfaces);
  64. rsp_2 = (void *) (skb->data + 6 + rsp_1->num_supported_rf_interfaces);
  65. ndev->max_logical_connections =
  66. rsp_2->max_logical_connections;
  67. ndev->max_routing_table_size =
  68. __le16_to_cpu(rsp_2->max_routing_table_size);
  69. ndev->max_ctrl_pkt_payload_len =
  70. rsp_2->max_ctrl_pkt_payload_len;
  71. ndev->max_size_for_large_params =
  72. __le16_to_cpu(rsp_2->max_size_for_large_params);
  73. ndev->manufact_id =
  74. rsp_2->manufact_id;
  75. ndev->manufact_specific_info =
  76. __le32_to_cpu(rsp_2->manufact_specific_info);
  77. pr_debug("nfcc_features 0x%x\n",
  78. ndev->nfcc_features);
  79. pr_debug("num_supported_rf_interfaces %d\n",
  80. ndev->num_supported_rf_interfaces);
  81. pr_debug("supported_rf_interfaces[0] 0x%x\n",
  82. ndev->supported_rf_interfaces[0]);
  83. pr_debug("supported_rf_interfaces[1] 0x%x\n",
  84. ndev->supported_rf_interfaces[1]);
  85. pr_debug("supported_rf_interfaces[2] 0x%x\n",
  86. ndev->supported_rf_interfaces[2]);
  87. pr_debug("supported_rf_interfaces[3] 0x%x\n",
  88. ndev->supported_rf_interfaces[3]);
  89. pr_debug("max_logical_connections %d\n",
  90. ndev->max_logical_connections);
  91. pr_debug("max_routing_table_size %d\n",
  92. ndev->max_routing_table_size);
  93. pr_debug("max_ctrl_pkt_payload_len %d\n",
  94. ndev->max_ctrl_pkt_payload_len);
  95. pr_debug("max_size_for_large_params %d\n",
  96. ndev->max_size_for_large_params);
  97. pr_debug("manufact_id 0x%x\n",
  98. ndev->manufact_id);
  99. pr_debug("manufact_specific_info 0x%x\n",
  100. ndev->manufact_specific_info);
  101. exit:
  102. nci_req_complete(ndev, rsp_1->status);
  103. }
  104. static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev,
  105. struct sk_buff *skb)
  106. {
  107. __u8 status = skb->data[0];
  108. pr_debug("status 0x%x\n", status);
  109. nci_req_complete(ndev, status);
  110. }
  111. static void nci_rf_disc_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  112. {
  113. __u8 status = skb->data[0];
  114. pr_debug("status 0x%x\n", status);
  115. if (status == NCI_STATUS_OK)
  116. set_bit(NCI_DISCOVERY, &ndev->flags);
  117. nci_req_complete(ndev, status);
  118. }
  119. static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
  120. struct sk_buff *skb)
  121. {
  122. __u8 status = skb->data[0];
  123. pr_debug("status 0x%x\n", status);
  124. clear_bit(NCI_DISCOVERY, &ndev->flags);
  125. nci_req_complete(ndev, status);
  126. }
  127. void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  128. {
  129. __u16 rsp_opcode = nci_opcode(skb->data);
  130. /* we got a rsp, stop the cmd timer */
  131. del_timer(&ndev->cmd_timer);
  132. pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  133. nci_pbf(skb->data),
  134. nci_opcode_gid(rsp_opcode),
  135. nci_opcode_oid(rsp_opcode),
  136. nci_plen(skb->data));
  137. /* strip the nci control header */
  138. skb_pull(skb, NCI_CTRL_HDR_SIZE);
  139. switch (rsp_opcode) {
  140. case NCI_OP_CORE_RESET_RSP:
  141. nci_core_reset_rsp_packet(ndev, skb);
  142. break;
  143. case NCI_OP_CORE_INIT_RSP:
  144. nci_core_init_rsp_packet(ndev, skb);
  145. break;
  146. case NCI_OP_RF_DISCOVER_MAP_RSP:
  147. nci_rf_disc_map_rsp_packet(ndev, skb);
  148. break;
  149. case NCI_OP_RF_DISCOVER_RSP:
  150. nci_rf_disc_rsp_packet(ndev, skb);
  151. break;
  152. case NCI_OP_RF_DEACTIVATE_RSP:
  153. nci_rf_deactivate_rsp_packet(ndev, skb);
  154. break;
  155. default:
  156. pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
  157. break;
  158. }
  159. kfree_skb(skb);
  160. /* trigger the next cmd */
  161. atomic_set(&ndev->cmd_cnt, 1);
  162. if (!skb_queue_empty(&ndev->cmd_q))
  163. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  164. }