ntf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * The NFC Controller Interface is the communication protocol between an
  4. * NFC Controller (NFCC) and a Device Host (DH).
  5. *
  6. * Copyright (C) 2014 Marvell International Ltd.
  7. * Copyright (C) 2011 Texas Instruments, Inc.
  8. *
  9. * Written by Ilan Elias <ilane@ti.com>
  10. *
  11. * Acknowledgements:
  12. * This file is based on hci_event.c, which was written
  13. * by Maxim Krasnyansky.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  16. #include <linux/types.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/bitops.h>
  19. #include <linux/skbuff.h>
  20. #include "../nfc.h"
  21. #include <net/nfc/nci.h>
  22. #include <net/nfc/nci_core.h>
  23. #include <linux/nfc.h>
  24. /* Handle NCI Notification packets */
  25. static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
  26. struct sk_buff *skb)
  27. {
  28. struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
  29. struct nci_conn_info *conn_info;
  30. int i;
  31. pr_debug("num_entries %d\n", ntf->num_entries);
  32. if (ntf->num_entries > NCI_MAX_NUM_CONN)
  33. ntf->num_entries = NCI_MAX_NUM_CONN;
  34. /* update the credits */
  35. for (i = 0; i < ntf->num_entries; i++) {
  36. ntf->conn_entries[i].conn_id =
  37. nci_conn_id(&ntf->conn_entries[i].conn_id);
  38. pr_debug("entry[%d]: conn_id %d, credits %d\n",
  39. i, ntf->conn_entries[i].conn_id,
  40. ntf->conn_entries[i].credits);
  41. conn_info = nci_get_conn_info_by_conn_id(ndev,
  42. ntf->conn_entries[i].conn_id);
  43. if (!conn_info)
  44. return;
  45. atomic_add(ntf->conn_entries[i].credits,
  46. &conn_info->credits_cnt);
  47. }
  48. /* trigger the next tx */
  49. if (!skb_queue_empty(&ndev->tx_q))
  50. queue_work(ndev->tx_wq, &ndev->tx_work);
  51. }
  52. static void nci_core_generic_error_ntf_packet(struct nci_dev *ndev,
  53. struct sk_buff *skb)
  54. {
  55. __u8 status = skb->data[0];
  56. pr_debug("status 0x%x\n", status);
  57. if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
  58. /* Activation failed, so complete the request
  59. (the state remains the same) */
  60. nci_req_complete(ndev, status);
  61. }
  62. }
  63. static void nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
  64. struct sk_buff *skb)
  65. {
  66. struct nci_core_intf_error_ntf *ntf = (void *) skb->data;
  67. ntf->conn_id = nci_conn_id(&ntf->conn_id);
  68. pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
  69. /* complete the data exchange transaction, if exists */
  70. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  71. nci_data_exchange_complete(ndev, NULL, ntf->conn_id, -EIO);
  72. }
  73. static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
  74. struct rf_tech_specific_params_nfca_poll *nfca_poll,
  75. __u8 *data)
  76. {
  77. nfca_poll->sens_res = __le16_to_cpu(*((__le16 *)data));
  78. data += 2;
  79. nfca_poll->nfcid1_len = min_t(__u8, *data++, NFC_NFCID1_MAXSIZE);
  80. pr_debug("sens_res 0x%x, nfcid1_len %d\n",
  81. nfca_poll->sens_res, nfca_poll->nfcid1_len);
  82. memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
  83. data += nfca_poll->nfcid1_len;
  84. nfca_poll->sel_res_len = *data++;
  85. if (nfca_poll->sel_res_len != 0)
  86. nfca_poll->sel_res = *data++;
  87. pr_debug("sel_res_len %d, sel_res 0x%x\n",
  88. nfca_poll->sel_res_len,
  89. nfca_poll->sel_res);
  90. return data;
  91. }
  92. static __u8 *nci_extract_rf_params_nfcb_passive_poll(struct nci_dev *ndev,
  93. struct rf_tech_specific_params_nfcb_poll *nfcb_poll,
  94. __u8 *data)
  95. {
  96. nfcb_poll->sensb_res_len = min_t(__u8, *data++, NFC_SENSB_RES_MAXSIZE);
  97. pr_debug("sensb_res_len %d\n", nfcb_poll->sensb_res_len);
  98. memcpy(nfcb_poll->sensb_res, data, nfcb_poll->sensb_res_len);
  99. data += nfcb_poll->sensb_res_len;
  100. return data;
  101. }
  102. static __u8 *nci_extract_rf_params_nfcf_passive_poll(struct nci_dev *ndev,
  103. struct rf_tech_specific_params_nfcf_poll *nfcf_poll,
  104. __u8 *data)
  105. {
  106. nfcf_poll->bit_rate = *data++;
  107. nfcf_poll->sensf_res_len = min_t(__u8, *data++, NFC_SENSF_RES_MAXSIZE);
  108. pr_debug("bit_rate %d, sensf_res_len %d\n",
  109. nfcf_poll->bit_rate, nfcf_poll->sensf_res_len);
  110. memcpy(nfcf_poll->sensf_res, data, nfcf_poll->sensf_res_len);
  111. data += nfcf_poll->sensf_res_len;
  112. return data;
  113. }
  114. static __u8 *nci_extract_rf_params_nfcv_passive_poll(struct nci_dev *ndev,
  115. struct rf_tech_specific_params_nfcv_poll *nfcv_poll,
  116. __u8 *data)
  117. {
  118. ++data;
  119. nfcv_poll->dsfid = *data++;
  120. memcpy(nfcv_poll->uid, data, NFC_ISO15693_UID_MAXSIZE);
  121. data += NFC_ISO15693_UID_MAXSIZE;
  122. return data;
  123. }
  124. static __u8 *nci_extract_rf_params_nfcf_passive_listen(struct nci_dev *ndev,
  125. struct rf_tech_specific_params_nfcf_listen *nfcf_listen,
  126. __u8 *data)
  127. {
  128. nfcf_listen->local_nfcid2_len = min_t(__u8, *data++,
  129. NFC_NFCID2_MAXSIZE);
  130. memcpy(nfcf_listen->local_nfcid2, data, nfcf_listen->local_nfcid2_len);
  131. data += nfcf_listen->local_nfcid2_len;
  132. return data;
  133. }
  134. static __u32 nci_get_prop_rf_protocol(struct nci_dev *ndev, __u8 rf_protocol)
  135. {
  136. if (ndev->ops->get_rfprotocol)
  137. return ndev->ops->get_rfprotocol(ndev, rf_protocol);
  138. return 0;
  139. }
  140. static int nci_add_new_protocol(struct nci_dev *ndev,
  141. struct nfc_target *target,
  142. __u8 rf_protocol,
  143. __u8 rf_tech_and_mode,
  144. void *params)
  145. {
  146. struct rf_tech_specific_params_nfca_poll *nfca_poll;
  147. struct rf_tech_specific_params_nfcb_poll *nfcb_poll;
  148. struct rf_tech_specific_params_nfcf_poll *nfcf_poll;
  149. struct rf_tech_specific_params_nfcv_poll *nfcv_poll;
  150. __u32 protocol;
  151. if (rf_protocol == NCI_RF_PROTOCOL_T1T)
  152. protocol = NFC_PROTO_JEWEL_MASK;
  153. else if (rf_protocol == NCI_RF_PROTOCOL_T2T)
  154. protocol = NFC_PROTO_MIFARE_MASK;
  155. else if (rf_protocol == NCI_RF_PROTOCOL_ISO_DEP)
  156. if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE)
  157. protocol = NFC_PROTO_ISO14443_MASK;
  158. else
  159. protocol = NFC_PROTO_ISO14443_B_MASK;
  160. else if (rf_protocol == NCI_RF_PROTOCOL_T3T)
  161. protocol = NFC_PROTO_FELICA_MASK;
  162. else if (rf_protocol == NCI_RF_PROTOCOL_NFC_DEP)
  163. protocol = NFC_PROTO_NFC_DEP_MASK;
  164. else if (rf_protocol == NCI_RF_PROTOCOL_T5T)
  165. protocol = NFC_PROTO_ISO15693_MASK;
  166. else
  167. protocol = nci_get_prop_rf_protocol(ndev, rf_protocol);
  168. if (!(protocol & ndev->poll_prots)) {
  169. pr_err("the target found does not have the desired protocol\n");
  170. return -EPROTO;
  171. }
  172. if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE) {
  173. nfca_poll = (struct rf_tech_specific_params_nfca_poll *)params;
  174. target->sens_res = nfca_poll->sens_res;
  175. target->sel_res = nfca_poll->sel_res;
  176. target->nfcid1_len = nfca_poll->nfcid1_len;
  177. if (target->nfcid1_len > 0) {
  178. memcpy(target->nfcid1, nfca_poll->nfcid1,
  179. target->nfcid1_len);
  180. }
  181. } else if (rf_tech_and_mode == NCI_NFC_B_PASSIVE_POLL_MODE) {
  182. nfcb_poll = (struct rf_tech_specific_params_nfcb_poll *)params;
  183. target->sensb_res_len = nfcb_poll->sensb_res_len;
  184. if (target->sensb_res_len > 0) {
  185. memcpy(target->sensb_res, nfcb_poll->sensb_res,
  186. target->sensb_res_len);
  187. }
  188. } else if (rf_tech_and_mode == NCI_NFC_F_PASSIVE_POLL_MODE) {
  189. nfcf_poll = (struct rf_tech_specific_params_nfcf_poll *)params;
  190. target->sensf_res_len = nfcf_poll->sensf_res_len;
  191. if (target->sensf_res_len > 0) {
  192. memcpy(target->sensf_res, nfcf_poll->sensf_res,
  193. target->sensf_res_len);
  194. }
  195. } else if (rf_tech_and_mode == NCI_NFC_V_PASSIVE_POLL_MODE) {
  196. nfcv_poll = (struct rf_tech_specific_params_nfcv_poll *)params;
  197. target->is_iso15693 = 1;
  198. target->iso15693_dsfid = nfcv_poll->dsfid;
  199. memcpy(target->iso15693_uid, nfcv_poll->uid, NFC_ISO15693_UID_MAXSIZE);
  200. } else {
  201. pr_err("unsupported rf_tech_and_mode 0x%x\n", rf_tech_and_mode);
  202. return -EPROTO;
  203. }
  204. target->supported_protocols |= protocol;
  205. pr_debug("protocol 0x%x\n", protocol);
  206. return 0;
  207. }
  208. static void nci_add_new_target(struct nci_dev *ndev,
  209. struct nci_rf_discover_ntf *ntf)
  210. {
  211. struct nfc_target *target;
  212. int i, rc;
  213. for (i = 0; i < ndev->n_targets; i++) {
  214. target = &ndev->targets[i];
  215. if (target->logical_idx == ntf->rf_discovery_id) {
  216. /* This target already exists, add the new protocol */
  217. nci_add_new_protocol(ndev, target, ntf->rf_protocol,
  218. ntf->rf_tech_and_mode,
  219. &ntf->rf_tech_specific_params);
  220. return;
  221. }
  222. }
  223. /* This is a new target, check if we've enough room */
  224. if (ndev->n_targets == NCI_MAX_DISCOVERED_TARGETS) {
  225. pr_debug("not enough room, ignoring new target...\n");
  226. return;
  227. }
  228. target = &ndev->targets[ndev->n_targets];
  229. rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
  230. ntf->rf_tech_and_mode,
  231. &ntf->rf_tech_specific_params);
  232. if (!rc) {
  233. target->logical_idx = ntf->rf_discovery_id;
  234. ndev->n_targets++;
  235. pr_debug("logical idx %d, n_targets %d\n", target->logical_idx,
  236. ndev->n_targets);
  237. }
  238. }
  239. void nci_clear_target_list(struct nci_dev *ndev)
  240. {
  241. memset(ndev->targets, 0,
  242. (sizeof(struct nfc_target)*NCI_MAX_DISCOVERED_TARGETS));
  243. ndev->n_targets = 0;
  244. }
  245. static void nci_rf_discover_ntf_packet(struct nci_dev *ndev,
  246. struct sk_buff *skb)
  247. {
  248. struct nci_rf_discover_ntf ntf;
  249. __u8 *data = skb->data;
  250. bool add_target = true;
  251. ntf.rf_discovery_id = *data++;
  252. ntf.rf_protocol = *data++;
  253. ntf.rf_tech_and_mode = *data++;
  254. ntf.rf_tech_specific_params_len = *data++;
  255. pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
  256. pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
  257. pr_debug("rf_tech_and_mode 0x%x\n", ntf.rf_tech_and_mode);
  258. pr_debug("rf_tech_specific_params_len %d\n",
  259. ntf.rf_tech_specific_params_len);
  260. if (ntf.rf_tech_specific_params_len > 0) {
  261. switch (ntf.rf_tech_and_mode) {
  262. case NCI_NFC_A_PASSIVE_POLL_MODE:
  263. data = nci_extract_rf_params_nfca_passive_poll(ndev,
  264. &(ntf.rf_tech_specific_params.nfca_poll), data);
  265. break;
  266. case NCI_NFC_B_PASSIVE_POLL_MODE:
  267. data = nci_extract_rf_params_nfcb_passive_poll(ndev,
  268. &(ntf.rf_tech_specific_params.nfcb_poll), data);
  269. break;
  270. case NCI_NFC_F_PASSIVE_POLL_MODE:
  271. data = nci_extract_rf_params_nfcf_passive_poll(ndev,
  272. &(ntf.rf_tech_specific_params.nfcf_poll), data);
  273. break;
  274. case NCI_NFC_V_PASSIVE_POLL_MODE:
  275. data = nci_extract_rf_params_nfcv_passive_poll(ndev,
  276. &(ntf.rf_tech_specific_params.nfcv_poll), data);
  277. break;
  278. default:
  279. pr_err("unsupported rf_tech_and_mode 0x%x\n",
  280. ntf.rf_tech_and_mode);
  281. data += ntf.rf_tech_specific_params_len;
  282. add_target = false;
  283. }
  284. }
  285. ntf.ntf_type = *data++;
  286. pr_debug("ntf_type %d\n", ntf.ntf_type);
  287. if (add_target == true)
  288. nci_add_new_target(ndev, &ntf);
  289. if (ntf.ntf_type == NCI_DISCOVER_NTF_TYPE_MORE) {
  290. atomic_set(&ndev->state, NCI_W4_ALL_DISCOVERIES);
  291. } else {
  292. atomic_set(&ndev->state, NCI_W4_HOST_SELECT);
  293. nfc_targets_found(ndev->nfc_dev, ndev->targets,
  294. ndev->n_targets);
  295. }
  296. }
  297. static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
  298. struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
  299. {
  300. struct activation_params_nfca_poll_iso_dep *nfca_poll;
  301. struct activation_params_nfcb_poll_iso_dep *nfcb_poll;
  302. switch (ntf->activation_rf_tech_and_mode) {
  303. case NCI_NFC_A_PASSIVE_POLL_MODE:
  304. nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
  305. nfca_poll->rats_res_len = min_t(__u8, *data++, 20);
  306. pr_debug("rats_res_len %d\n", nfca_poll->rats_res_len);
  307. if (nfca_poll->rats_res_len > 0) {
  308. memcpy(nfca_poll->rats_res,
  309. data, nfca_poll->rats_res_len);
  310. }
  311. break;
  312. case NCI_NFC_B_PASSIVE_POLL_MODE:
  313. nfcb_poll = &ntf->activation_params.nfcb_poll_iso_dep;
  314. nfcb_poll->attrib_res_len = min_t(__u8, *data++, 50);
  315. pr_debug("attrib_res_len %d\n", nfcb_poll->attrib_res_len);
  316. if (nfcb_poll->attrib_res_len > 0) {
  317. memcpy(nfcb_poll->attrib_res,
  318. data, nfcb_poll->attrib_res_len);
  319. }
  320. break;
  321. default:
  322. pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
  323. ntf->activation_rf_tech_and_mode);
  324. return NCI_STATUS_RF_PROTOCOL_ERROR;
  325. }
  326. return NCI_STATUS_OK;
  327. }
  328. static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev,
  329. struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
  330. {
  331. struct activation_params_poll_nfc_dep *poll;
  332. struct activation_params_listen_nfc_dep *listen;
  333. switch (ntf->activation_rf_tech_and_mode) {
  334. case NCI_NFC_A_PASSIVE_POLL_MODE:
  335. case NCI_NFC_F_PASSIVE_POLL_MODE:
  336. poll = &ntf->activation_params.poll_nfc_dep;
  337. poll->atr_res_len = min_t(__u8, *data++,
  338. NFC_ATR_RES_MAXSIZE - 2);
  339. pr_debug("atr_res_len %d\n", poll->atr_res_len);
  340. if (poll->atr_res_len > 0)
  341. memcpy(poll->atr_res, data, poll->atr_res_len);
  342. break;
  343. case NCI_NFC_A_PASSIVE_LISTEN_MODE:
  344. case NCI_NFC_F_PASSIVE_LISTEN_MODE:
  345. listen = &ntf->activation_params.listen_nfc_dep;
  346. listen->atr_req_len = min_t(__u8, *data++,
  347. NFC_ATR_REQ_MAXSIZE - 2);
  348. pr_debug("atr_req_len %d\n", listen->atr_req_len);
  349. if (listen->atr_req_len > 0)
  350. memcpy(listen->atr_req, data, listen->atr_req_len);
  351. break;
  352. default:
  353. pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
  354. ntf->activation_rf_tech_and_mode);
  355. return NCI_STATUS_RF_PROTOCOL_ERROR;
  356. }
  357. return NCI_STATUS_OK;
  358. }
  359. static void nci_target_auto_activated(struct nci_dev *ndev,
  360. struct nci_rf_intf_activated_ntf *ntf)
  361. {
  362. struct nfc_target *target;
  363. int rc;
  364. target = &ndev->targets[ndev->n_targets];
  365. rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
  366. ntf->activation_rf_tech_and_mode,
  367. &ntf->rf_tech_specific_params);
  368. if (rc)
  369. return;
  370. target->logical_idx = ntf->rf_discovery_id;
  371. ndev->n_targets++;
  372. pr_debug("logical idx %d, n_targets %d\n",
  373. target->logical_idx, ndev->n_targets);
  374. nfc_targets_found(ndev->nfc_dev, ndev->targets, ndev->n_targets);
  375. }
  376. static int nci_store_general_bytes_nfc_dep(struct nci_dev *ndev,
  377. struct nci_rf_intf_activated_ntf *ntf)
  378. {
  379. ndev->remote_gb_len = 0;
  380. if (ntf->activation_params_len <= 0)
  381. return NCI_STATUS_OK;
  382. switch (ntf->activation_rf_tech_and_mode) {
  383. case NCI_NFC_A_PASSIVE_POLL_MODE:
  384. case NCI_NFC_F_PASSIVE_POLL_MODE:
  385. ndev->remote_gb_len = min_t(__u8,
  386. (ntf->activation_params.poll_nfc_dep.atr_res_len
  387. - NFC_ATR_RES_GT_OFFSET),
  388. NFC_ATR_RES_GB_MAXSIZE);
  389. memcpy(ndev->remote_gb,
  390. (ntf->activation_params.poll_nfc_dep.atr_res
  391. + NFC_ATR_RES_GT_OFFSET),
  392. ndev->remote_gb_len);
  393. break;
  394. case NCI_NFC_A_PASSIVE_LISTEN_MODE:
  395. case NCI_NFC_F_PASSIVE_LISTEN_MODE:
  396. ndev->remote_gb_len = min_t(__u8,
  397. (ntf->activation_params.listen_nfc_dep.atr_req_len
  398. - NFC_ATR_REQ_GT_OFFSET),
  399. NFC_ATR_REQ_GB_MAXSIZE);
  400. memcpy(ndev->remote_gb,
  401. (ntf->activation_params.listen_nfc_dep.atr_req
  402. + NFC_ATR_REQ_GT_OFFSET),
  403. ndev->remote_gb_len);
  404. break;
  405. default:
  406. pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
  407. ntf->activation_rf_tech_and_mode);
  408. return NCI_STATUS_RF_PROTOCOL_ERROR;
  409. }
  410. return NCI_STATUS_OK;
  411. }
  412. static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
  413. struct sk_buff *skb)
  414. {
  415. struct nci_conn_info *conn_info;
  416. struct nci_rf_intf_activated_ntf ntf;
  417. __u8 *data = skb->data;
  418. int err = NCI_STATUS_OK;
  419. ntf.rf_discovery_id = *data++;
  420. ntf.rf_interface = *data++;
  421. ntf.rf_protocol = *data++;
  422. ntf.activation_rf_tech_and_mode = *data++;
  423. ntf.max_data_pkt_payload_size = *data++;
  424. ntf.initial_num_credits = *data++;
  425. ntf.rf_tech_specific_params_len = *data++;
  426. pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
  427. pr_debug("rf_interface 0x%x\n", ntf.rf_interface);
  428. pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
  429. pr_debug("activation_rf_tech_and_mode 0x%x\n",
  430. ntf.activation_rf_tech_and_mode);
  431. pr_debug("max_data_pkt_payload_size 0x%x\n",
  432. ntf.max_data_pkt_payload_size);
  433. pr_debug("initial_num_credits 0x%x\n",
  434. ntf.initial_num_credits);
  435. pr_debug("rf_tech_specific_params_len %d\n",
  436. ntf.rf_tech_specific_params_len);
  437. /* If this contains a value of 0x00 (NFCEE Direct RF
  438. * Interface) then all following parameters SHALL contain a
  439. * value of 0 and SHALL be ignored.
  440. */
  441. if (ntf.rf_interface == NCI_RF_INTERFACE_NFCEE_DIRECT)
  442. goto listen;
  443. if (ntf.rf_tech_specific_params_len > 0) {
  444. switch (ntf.activation_rf_tech_and_mode) {
  445. case NCI_NFC_A_PASSIVE_POLL_MODE:
  446. data = nci_extract_rf_params_nfca_passive_poll(ndev,
  447. &(ntf.rf_tech_specific_params.nfca_poll), data);
  448. break;
  449. case NCI_NFC_B_PASSIVE_POLL_MODE:
  450. data = nci_extract_rf_params_nfcb_passive_poll(ndev,
  451. &(ntf.rf_tech_specific_params.nfcb_poll), data);
  452. break;
  453. case NCI_NFC_F_PASSIVE_POLL_MODE:
  454. data = nci_extract_rf_params_nfcf_passive_poll(ndev,
  455. &(ntf.rf_tech_specific_params.nfcf_poll), data);
  456. break;
  457. case NCI_NFC_V_PASSIVE_POLL_MODE:
  458. data = nci_extract_rf_params_nfcv_passive_poll(ndev,
  459. &(ntf.rf_tech_specific_params.nfcv_poll), data);
  460. break;
  461. case NCI_NFC_A_PASSIVE_LISTEN_MODE:
  462. /* no RF technology specific parameters */
  463. break;
  464. case NCI_NFC_F_PASSIVE_LISTEN_MODE:
  465. data = nci_extract_rf_params_nfcf_passive_listen(ndev,
  466. &(ntf.rf_tech_specific_params.nfcf_listen),
  467. data);
  468. break;
  469. default:
  470. pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
  471. ntf.activation_rf_tech_and_mode);
  472. err = NCI_STATUS_RF_PROTOCOL_ERROR;
  473. goto exit;
  474. }
  475. }
  476. ntf.data_exch_rf_tech_and_mode = *data++;
  477. ntf.data_exch_tx_bit_rate = *data++;
  478. ntf.data_exch_rx_bit_rate = *data++;
  479. ntf.activation_params_len = *data++;
  480. pr_debug("data_exch_rf_tech_and_mode 0x%x\n",
  481. ntf.data_exch_rf_tech_and_mode);
  482. pr_debug("data_exch_tx_bit_rate 0x%x\n", ntf.data_exch_tx_bit_rate);
  483. pr_debug("data_exch_rx_bit_rate 0x%x\n", ntf.data_exch_rx_bit_rate);
  484. pr_debug("activation_params_len %d\n", ntf.activation_params_len);
  485. if (ntf.activation_params_len > 0) {
  486. switch (ntf.rf_interface) {
  487. case NCI_RF_INTERFACE_ISO_DEP:
  488. err = nci_extract_activation_params_iso_dep(ndev,
  489. &ntf, data);
  490. break;
  491. case NCI_RF_INTERFACE_NFC_DEP:
  492. err = nci_extract_activation_params_nfc_dep(ndev,
  493. &ntf, data);
  494. break;
  495. case NCI_RF_INTERFACE_FRAME:
  496. /* no activation params */
  497. break;
  498. default:
  499. pr_err("unsupported rf_interface 0x%x\n",
  500. ntf.rf_interface);
  501. err = NCI_STATUS_RF_PROTOCOL_ERROR;
  502. break;
  503. }
  504. }
  505. exit:
  506. if (err == NCI_STATUS_OK) {
  507. conn_info = ndev->rf_conn_info;
  508. if (!conn_info)
  509. return;
  510. conn_info->max_pkt_payload_len = ntf.max_data_pkt_payload_size;
  511. conn_info->initial_num_credits = ntf.initial_num_credits;
  512. /* set the available credits to initial value */
  513. atomic_set(&conn_info->credits_cnt,
  514. conn_info->initial_num_credits);
  515. /* store general bytes to be reported later in dep_link_up */
  516. if (ntf.rf_interface == NCI_RF_INTERFACE_NFC_DEP) {
  517. err = nci_store_general_bytes_nfc_dep(ndev, &ntf);
  518. if (err != NCI_STATUS_OK)
  519. pr_err("unable to store general bytes\n");
  520. }
  521. }
  522. if (!(ntf.activation_rf_tech_and_mode & NCI_RF_TECH_MODE_LISTEN_MASK)) {
  523. /* Poll mode */
  524. if (atomic_read(&ndev->state) == NCI_DISCOVERY) {
  525. /* A single target was found and activated
  526. * automatically */
  527. atomic_set(&ndev->state, NCI_POLL_ACTIVE);
  528. if (err == NCI_STATUS_OK)
  529. nci_target_auto_activated(ndev, &ntf);
  530. } else { /* ndev->state == NCI_W4_HOST_SELECT */
  531. /* A selected target was activated, so complete the
  532. * request */
  533. atomic_set(&ndev->state, NCI_POLL_ACTIVE);
  534. nci_req_complete(ndev, err);
  535. }
  536. } else {
  537. listen:
  538. /* Listen mode */
  539. atomic_set(&ndev->state, NCI_LISTEN_ACTIVE);
  540. if (err == NCI_STATUS_OK &&
  541. ntf.rf_protocol == NCI_RF_PROTOCOL_NFC_DEP) {
  542. err = nfc_tm_activated(ndev->nfc_dev,
  543. NFC_PROTO_NFC_DEP_MASK,
  544. NFC_COMM_PASSIVE,
  545. ndev->remote_gb,
  546. ndev->remote_gb_len);
  547. if (err != NCI_STATUS_OK)
  548. pr_err("error when signaling tm activation\n");
  549. }
  550. }
  551. }
  552. static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
  553. struct sk_buff *skb)
  554. {
  555. struct nci_conn_info *conn_info;
  556. struct nci_rf_deactivate_ntf *ntf = (void *) skb->data;
  557. pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason);
  558. conn_info = ndev->rf_conn_info;
  559. if (!conn_info)
  560. return;
  561. /* drop tx data queue */
  562. skb_queue_purge(&ndev->tx_q);
  563. /* drop partial rx data packet */
  564. if (ndev->rx_data_reassembly) {
  565. kfree_skb(ndev->rx_data_reassembly);
  566. ndev->rx_data_reassembly = NULL;
  567. }
  568. /* complete the data exchange transaction, if exists */
  569. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  570. nci_data_exchange_complete(ndev, NULL, NCI_STATIC_RF_CONN_ID,
  571. -EIO);
  572. switch (ntf->type) {
  573. case NCI_DEACTIVATE_TYPE_IDLE_MODE:
  574. nci_clear_target_list(ndev);
  575. atomic_set(&ndev->state, NCI_IDLE);
  576. break;
  577. case NCI_DEACTIVATE_TYPE_SLEEP_MODE:
  578. case NCI_DEACTIVATE_TYPE_SLEEP_AF_MODE:
  579. atomic_set(&ndev->state, NCI_W4_HOST_SELECT);
  580. break;
  581. case NCI_DEACTIVATE_TYPE_DISCOVERY:
  582. nci_clear_target_list(ndev);
  583. atomic_set(&ndev->state, NCI_DISCOVERY);
  584. break;
  585. }
  586. nci_req_complete(ndev, NCI_STATUS_OK);
  587. }
  588. static void nci_nfcee_discover_ntf_packet(struct nci_dev *ndev,
  589. struct sk_buff *skb)
  590. {
  591. u8 status = NCI_STATUS_OK;
  592. struct nci_nfcee_discover_ntf *nfcee_ntf =
  593. (struct nci_nfcee_discover_ntf *)skb->data;
  594. pr_debug("\n");
  595. /* NFCForum NCI 9.2.1 HCI Network Specific Handling
  596. * If the NFCC supports the HCI Network, it SHALL return one,
  597. * and only one, NFCEE_DISCOVER_NTF with a Protocol type of
  598. * “HCI Access”, even if the HCI Network contains multiple NFCEEs.
  599. */
  600. ndev->hci_dev->nfcee_id = nfcee_ntf->nfcee_id;
  601. ndev->cur_params.id = nfcee_ntf->nfcee_id;
  602. nci_req_complete(ndev, status);
  603. }
  604. static void nci_nfcee_action_ntf_packet(struct nci_dev *ndev,
  605. struct sk_buff *skb)
  606. {
  607. pr_debug("\n");
  608. }
  609. void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
  610. {
  611. __u16 ntf_opcode = nci_opcode(skb->data);
  612. pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  613. nci_pbf(skb->data),
  614. nci_opcode_gid(ntf_opcode),
  615. nci_opcode_oid(ntf_opcode),
  616. nci_plen(skb->data));
  617. /* strip the nci control header */
  618. skb_pull(skb, NCI_CTRL_HDR_SIZE);
  619. if (nci_opcode_gid(ntf_opcode) == NCI_GID_PROPRIETARY) {
  620. if (nci_prop_ntf_packet(ndev, ntf_opcode, skb) == -ENOTSUPP) {
  621. pr_err("unsupported ntf opcode 0x%x\n",
  622. ntf_opcode);
  623. }
  624. goto end;
  625. }
  626. switch (ntf_opcode) {
  627. case NCI_OP_CORE_CONN_CREDITS_NTF:
  628. nci_core_conn_credits_ntf_packet(ndev, skb);
  629. break;
  630. case NCI_OP_CORE_GENERIC_ERROR_NTF:
  631. nci_core_generic_error_ntf_packet(ndev, skb);
  632. break;
  633. case NCI_OP_CORE_INTF_ERROR_NTF:
  634. nci_core_conn_intf_error_ntf_packet(ndev, skb);
  635. break;
  636. case NCI_OP_RF_DISCOVER_NTF:
  637. nci_rf_discover_ntf_packet(ndev, skb);
  638. break;
  639. case NCI_OP_RF_INTF_ACTIVATED_NTF:
  640. nci_rf_intf_activated_ntf_packet(ndev, skb);
  641. break;
  642. case NCI_OP_RF_DEACTIVATE_NTF:
  643. nci_rf_deactivate_ntf_packet(ndev, skb);
  644. break;
  645. case NCI_OP_NFCEE_DISCOVER_NTF:
  646. nci_nfcee_discover_ntf_packet(ndev, skb);
  647. break;
  648. case NCI_OP_RF_NFCEE_ACTION_NTF:
  649. nci_nfcee_action_ntf_packet(ndev, skb);
  650. break;
  651. default:
  652. pr_err("unknown ntf opcode 0x%x\n", ntf_opcode);
  653. break;
  654. }
  655. nci_core_ntf_packet(ndev, ntf_opcode, skb);
  656. end:
  657. kfree_skb(skb);
  658. }