ntf.c 23 KB

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