rsp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. struct nci_conn_info *conn_info;
  119. __u8 status = skb->data[0];
  120. pr_debug("status 0x%x\n", status);
  121. if (status == NCI_STATUS_OK) {
  122. atomic_set(&ndev->state, NCI_DISCOVERY);
  123. conn_info = ndev->rf_conn_info;
  124. if (!conn_info) {
  125. conn_info = devm_kzalloc(&ndev->nfc_dev->dev,
  126. sizeof(struct nci_conn_info),
  127. GFP_KERNEL);
  128. if (!conn_info) {
  129. status = NCI_STATUS_REJECTED;
  130. goto exit;
  131. }
  132. conn_info->conn_id = NCI_STATIC_RF_CONN_ID;
  133. INIT_LIST_HEAD(&conn_info->list);
  134. list_add(&conn_info->list, &ndev->conn_info_list);
  135. ndev->rf_conn_info = conn_info;
  136. }
  137. }
  138. exit:
  139. nci_req_complete(ndev, status);
  140. }
  141. static void nci_rf_disc_select_rsp_packet(struct nci_dev *ndev,
  142. struct sk_buff *skb)
  143. {
  144. __u8 status = skb->data[0];
  145. pr_debug("status 0x%x\n", status);
  146. /* Complete the request on intf_activated_ntf or generic_error_ntf */
  147. if (status != NCI_STATUS_OK)
  148. nci_req_complete(ndev, status);
  149. }
  150. static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
  151. struct sk_buff *skb)
  152. {
  153. __u8 status = skb->data[0];
  154. pr_debug("status 0x%x\n", status);
  155. /* If target was active, complete the request only in deactivate_ntf */
  156. if ((status != NCI_STATUS_OK) ||
  157. (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
  158. nci_clear_target_list(ndev);
  159. atomic_set(&ndev->state, NCI_IDLE);
  160. nci_req_complete(ndev, status);
  161. }
  162. }
  163. static void nci_nfcee_discover_rsp_packet(struct nci_dev *ndev,
  164. struct sk_buff *skb)
  165. {
  166. struct nci_nfcee_discover_rsp *discover_rsp;
  167. if (skb->len != 2) {
  168. nci_req_complete(ndev, NCI_STATUS_NFCEE_PROTOCOL_ERROR);
  169. return;
  170. }
  171. discover_rsp = (struct nci_nfcee_discover_rsp *)skb->data;
  172. if (discover_rsp->status != NCI_STATUS_OK ||
  173. discover_rsp->num_nfcee == 0)
  174. nci_req_complete(ndev, discover_rsp->status);
  175. }
  176. static void nci_nfcee_mode_set_rsp_packet(struct nci_dev *ndev,
  177. struct sk_buff *skb)
  178. {
  179. __u8 status = skb->data[0];
  180. pr_debug("status 0x%x\n", status);
  181. nci_req_complete(ndev, status);
  182. }
  183. static void nci_core_conn_create_rsp_packet(struct nci_dev *ndev,
  184. struct sk_buff *skb)
  185. {
  186. __u8 status = skb->data[0];
  187. struct nci_conn_info *conn_info;
  188. struct nci_core_conn_create_rsp *rsp;
  189. pr_debug("status 0x%x\n", status);
  190. if (status == NCI_STATUS_OK) {
  191. rsp = (struct nci_core_conn_create_rsp *)skb->data;
  192. conn_info = devm_kzalloc(&ndev->nfc_dev->dev,
  193. sizeof(*conn_info), GFP_KERNEL);
  194. if (!conn_info) {
  195. status = NCI_STATUS_REJECTED;
  196. goto exit;
  197. }
  198. conn_info->id = ndev->cur_id;
  199. conn_info->conn_id = rsp->conn_id;
  200. /* Note: data_exchange_cb and data_exchange_cb_context need to
  201. * be specify out of nci_core_conn_create_rsp_packet
  202. */
  203. INIT_LIST_HEAD(&conn_info->list);
  204. list_add(&conn_info->list, &ndev->conn_info_list);
  205. if (ndev->cur_id == ndev->hci_dev->nfcee_id)
  206. ndev->hci_dev->conn_info = conn_info;
  207. conn_info->conn_id = rsp->conn_id;
  208. conn_info->max_pkt_payload_len = rsp->max_ctrl_pkt_payload_len;
  209. atomic_set(&conn_info->credits_cnt, rsp->credits_cnt);
  210. }
  211. exit:
  212. nci_req_complete(ndev, status);
  213. }
  214. static void nci_core_conn_close_rsp_packet(struct nci_dev *ndev,
  215. struct sk_buff *skb)
  216. {
  217. struct nci_conn_info *conn_info;
  218. __u8 status = skb->data[0];
  219. pr_debug("status 0x%x\n", status);
  220. if (status == NCI_STATUS_OK) {
  221. conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_id);
  222. if (conn_info) {
  223. list_del(&conn_info->list);
  224. devm_kfree(&ndev->nfc_dev->dev, conn_info);
  225. }
  226. }
  227. nci_req_complete(ndev, status);
  228. }
  229. void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  230. {
  231. __u16 rsp_opcode = nci_opcode(skb->data);
  232. /* we got a rsp, stop the cmd timer */
  233. del_timer(&ndev->cmd_timer);
  234. pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  235. nci_pbf(skb->data),
  236. nci_opcode_gid(rsp_opcode),
  237. nci_opcode_oid(rsp_opcode),
  238. nci_plen(skb->data));
  239. /* strip the nci control header */
  240. skb_pull(skb, NCI_CTRL_HDR_SIZE);
  241. if (nci_opcode_gid(rsp_opcode) == NCI_GID_PROPRIETARY) {
  242. if (nci_prop_rsp_packet(ndev, rsp_opcode, skb) == -ENOTSUPP) {
  243. pr_err("unsupported rsp opcode 0x%x\n",
  244. rsp_opcode);
  245. }
  246. goto end;
  247. }
  248. switch (rsp_opcode) {
  249. case NCI_OP_CORE_RESET_RSP:
  250. nci_core_reset_rsp_packet(ndev, skb);
  251. break;
  252. case NCI_OP_CORE_INIT_RSP:
  253. nci_core_init_rsp_packet(ndev, skb);
  254. break;
  255. case NCI_OP_CORE_SET_CONFIG_RSP:
  256. nci_core_set_config_rsp_packet(ndev, skb);
  257. break;
  258. case NCI_OP_CORE_CONN_CREATE_RSP:
  259. nci_core_conn_create_rsp_packet(ndev, skb);
  260. break;
  261. case NCI_OP_CORE_CONN_CLOSE_RSP:
  262. nci_core_conn_close_rsp_packet(ndev, skb);
  263. break;
  264. case NCI_OP_RF_DISCOVER_MAP_RSP:
  265. nci_rf_disc_map_rsp_packet(ndev, skb);
  266. break;
  267. case NCI_OP_RF_DISCOVER_RSP:
  268. nci_rf_disc_rsp_packet(ndev, skb);
  269. break;
  270. case NCI_OP_RF_DISCOVER_SELECT_RSP:
  271. nci_rf_disc_select_rsp_packet(ndev, skb);
  272. break;
  273. case NCI_OP_RF_DEACTIVATE_RSP:
  274. nci_rf_deactivate_rsp_packet(ndev, skb);
  275. break;
  276. case NCI_OP_NFCEE_DISCOVER_RSP:
  277. nci_nfcee_discover_rsp_packet(ndev, skb);
  278. break;
  279. case NCI_OP_NFCEE_MODE_SET_RSP:
  280. nci_nfcee_mode_set_rsp_packet(ndev, skb);
  281. break;
  282. default:
  283. pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
  284. break;
  285. }
  286. nci_core_rsp_packet(ndev, rsp_opcode, skb);
  287. end:
  288. kfree_skb(skb);
  289. /* trigger the next cmd */
  290. atomic_set(&ndev->cmd_cnt, 1);
  291. if (!skb_queue_empty(&ndev->cmd_q))
  292. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  293. }