ntf.c 21 KB

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