rsp.c 6.1 KB

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