ntf.c 23 KB

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