rsp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 = NULL;
  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->dest_params = devm_kzalloc(&ndev->nfc_dev->dev,
  199. sizeof(struct dest_spec_params),
  200. GFP_KERNEL);
  201. if (!conn_info->dest_params) {
  202. status = NCI_STATUS_REJECTED;
  203. goto free_conn_info;
  204. }
  205. conn_info->dest_type = ndev->cur_dest_type;
  206. conn_info->dest_params->id = ndev->cur_params.id;
  207. conn_info->dest_params->protocol = ndev->cur_params.protocol;
  208. conn_info->conn_id = rsp->conn_id;
  209. /* Note: data_exchange_cb and data_exchange_cb_context need to
  210. * be specify out of nci_core_conn_create_rsp_packet
  211. */
  212. INIT_LIST_HEAD(&conn_info->list);
  213. list_add(&conn_info->list, &ndev->conn_info_list);
  214. if (ndev->cur_params.id == ndev->hci_dev->nfcee_id)
  215. ndev->hci_dev->conn_info = conn_info;
  216. conn_info->conn_id = rsp->conn_id;
  217. conn_info->max_pkt_payload_len = rsp->max_ctrl_pkt_payload_len;
  218. atomic_set(&conn_info->credits_cnt, rsp->credits_cnt);
  219. }
  220. free_conn_info:
  221. if (status == NCI_STATUS_REJECTED)
  222. devm_kfree(&ndev->nfc_dev->dev, conn_info);
  223. exit:
  224. nci_req_complete(ndev, status);
  225. }
  226. static void nci_core_conn_close_rsp_packet(struct nci_dev *ndev,
  227. struct sk_buff *skb)
  228. {
  229. struct nci_conn_info *conn_info;
  230. __u8 status = skb->data[0];
  231. pr_debug("status 0x%x\n", status);
  232. if (status == NCI_STATUS_OK) {
  233. conn_info = nci_get_conn_info_by_conn_id(ndev,
  234. ndev->cur_conn_id);
  235. if (conn_info) {
  236. list_del(&conn_info->list);
  237. devm_kfree(&ndev->nfc_dev->dev, conn_info);
  238. }
  239. }
  240. nci_req_complete(ndev, status);
  241. }
  242. void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
  243. {
  244. __u16 rsp_opcode = nci_opcode(skb->data);
  245. /* we got a rsp, stop the cmd timer */
  246. del_timer(&ndev->cmd_timer);
  247. pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  248. nci_pbf(skb->data),
  249. nci_opcode_gid(rsp_opcode),
  250. nci_opcode_oid(rsp_opcode),
  251. nci_plen(skb->data));
  252. /* strip the nci control header */
  253. skb_pull(skb, NCI_CTRL_HDR_SIZE);
  254. if (nci_opcode_gid(rsp_opcode) == NCI_GID_PROPRIETARY) {
  255. if (nci_prop_rsp_packet(ndev, rsp_opcode, skb) == -ENOTSUPP) {
  256. pr_err("unsupported rsp opcode 0x%x\n",
  257. rsp_opcode);
  258. }
  259. goto end;
  260. }
  261. switch (rsp_opcode) {
  262. case NCI_OP_CORE_RESET_RSP:
  263. nci_core_reset_rsp_packet(ndev, skb);
  264. break;
  265. case NCI_OP_CORE_INIT_RSP:
  266. nci_core_init_rsp_packet(ndev, skb);
  267. break;
  268. case NCI_OP_CORE_SET_CONFIG_RSP:
  269. nci_core_set_config_rsp_packet(ndev, skb);
  270. break;
  271. case NCI_OP_CORE_CONN_CREATE_RSP:
  272. nci_core_conn_create_rsp_packet(ndev, skb);
  273. break;
  274. case NCI_OP_CORE_CONN_CLOSE_RSP:
  275. nci_core_conn_close_rsp_packet(ndev, skb);
  276. break;
  277. case NCI_OP_RF_DISCOVER_MAP_RSP:
  278. nci_rf_disc_map_rsp_packet(ndev, skb);
  279. break;
  280. case NCI_OP_RF_DISCOVER_RSP:
  281. nci_rf_disc_rsp_packet(ndev, skb);
  282. break;
  283. case NCI_OP_RF_DISCOVER_SELECT_RSP:
  284. nci_rf_disc_select_rsp_packet(ndev, skb);
  285. break;
  286. case NCI_OP_RF_DEACTIVATE_RSP:
  287. nci_rf_deactivate_rsp_packet(ndev, skb);
  288. break;
  289. case NCI_OP_NFCEE_DISCOVER_RSP:
  290. nci_nfcee_discover_rsp_packet(ndev, skb);
  291. break;
  292. case NCI_OP_NFCEE_MODE_SET_RSP:
  293. nci_nfcee_mode_set_rsp_packet(ndev, skb);
  294. break;
  295. default:
  296. pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
  297. break;
  298. }
  299. nci_core_rsp_packet(ndev, rsp_opcode, skb);
  300. end:
  301. kfree_skb(skb);
  302. /* trigger the next cmd */
  303. atomic_set(&ndev->cmd_cnt, 1);
  304. if (!skb_queue_empty(&ndev->cmd_q))
  305. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  306. }