core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*
  2. * The NFC Controller Interface is the communication protocol between an
  3. * NFC Controller (NFCC) and a Device Host (DH).
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2014 Marvell International Ltd.
  7. *
  8. * Written by Ilan Elias <ilane@ti.com>
  9. *
  10. * Acknowledgements:
  11. * This file is based on hci_core.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/module.h>
  29. #include <linux/types.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/completion.h>
  32. #include <linux/export.h>
  33. #include <linux/sched.h>
  34. #include <linux/bitops.h>
  35. #include <linux/skbuff.h>
  36. #include "../nfc.h"
  37. #include <net/nfc/nci.h>
  38. #include <net/nfc/nci_core.h>
  39. #include <linux/nfc.h>
  40. static void nci_cmd_work(struct work_struct *work);
  41. static void nci_rx_work(struct work_struct *work);
  42. static void nci_tx_work(struct work_struct *work);
  43. /* ---- NCI requests ---- */
  44. void nci_req_complete(struct nci_dev *ndev, int result)
  45. {
  46. if (ndev->req_status == NCI_REQ_PEND) {
  47. ndev->req_result = result;
  48. ndev->req_status = NCI_REQ_DONE;
  49. complete(&ndev->req_completion);
  50. }
  51. }
  52. static void nci_req_cancel(struct nci_dev *ndev, int err)
  53. {
  54. if (ndev->req_status == NCI_REQ_PEND) {
  55. ndev->req_result = err;
  56. ndev->req_status = NCI_REQ_CANCELED;
  57. complete(&ndev->req_completion);
  58. }
  59. }
  60. /* Execute request and wait for completion. */
  61. static int __nci_request(struct nci_dev *ndev,
  62. void (*req)(struct nci_dev *ndev, unsigned long opt),
  63. unsigned long opt, __u32 timeout)
  64. {
  65. int rc = 0;
  66. long completion_rc;
  67. ndev->req_status = NCI_REQ_PEND;
  68. reinit_completion(&ndev->req_completion);
  69. req(ndev, opt);
  70. completion_rc =
  71. wait_for_completion_interruptible_timeout(&ndev->req_completion,
  72. timeout);
  73. pr_debug("wait_for_completion return %ld\n", completion_rc);
  74. if (completion_rc > 0) {
  75. switch (ndev->req_status) {
  76. case NCI_REQ_DONE:
  77. rc = nci_to_errno(ndev->req_result);
  78. break;
  79. case NCI_REQ_CANCELED:
  80. rc = -ndev->req_result;
  81. break;
  82. default:
  83. rc = -ETIMEDOUT;
  84. break;
  85. }
  86. } else {
  87. pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
  88. completion_rc);
  89. rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
  90. }
  91. ndev->req_status = ndev->req_result = 0;
  92. return rc;
  93. }
  94. static inline int nci_request(struct nci_dev *ndev,
  95. void (*req)(struct nci_dev *ndev,
  96. unsigned long opt),
  97. unsigned long opt, __u32 timeout)
  98. {
  99. int rc;
  100. if (!test_bit(NCI_UP, &ndev->flags))
  101. return -ENETDOWN;
  102. /* Serialize all requests */
  103. mutex_lock(&ndev->req_lock);
  104. rc = __nci_request(ndev, req, opt, timeout);
  105. mutex_unlock(&ndev->req_lock);
  106. return rc;
  107. }
  108. static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
  109. {
  110. struct nci_core_reset_cmd cmd;
  111. cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
  112. nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
  113. }
  114. static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
  115. {
  116. nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
  117. }
  118. static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
  119. {
  120. struct nci_rf_disc_map_cmd cmd;
  121. struct disc_map_config *cfg = cmd.mapping_configs;
  122. __u8 *num = &cmd.num_mapping_configs;
  123. int i;
  124. /* set rf mapping configurations */
  125. *num = 0;
  126. /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
  127. for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
  128. if (ndev->supported_rf_interfaces[i] ==
  129. NCI_RF_INTERFACE_ISO_DEP) {
  130. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  131. cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
  132. NCI_DISC_MAP_MODE_LISTEN;
  133. cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
  134. (*num)++;
  135. } else if (ndev->supported_rf_interfaces[i] ==
  136. NCI_RF_INTERFACE_NFC_DEP) {
  137. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  138. cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
  139. NCI_DISC_MAP_MODE_LISTEN;
  140. cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
  141. (*num)++;
  142. }
  143. if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
  144. break;
  145. }
  146. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
  147. (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
  148. }
  149. struct nci_set_config_param {
  150. __u8 id;
  151. size_t len;
  152. __u8 *val;
  153. };
  154. static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
  155. {
  156. struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
  157. struct nci_core_set_config_cmd cmd;
  158. BUG_ON(param->len > NCI_MAX_PARAM_LEN);
  159. cmd.num_params = 1;
  160. cmd.param.id = param->id;
  161. cmd.param.len = param->len;
  162. memcpy(cmd.param.val, param->val, param->len);
  163. nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
  164. }
  165. struct nci_rf_discover_param {
  166. __u32 im_protocols;
  167. __u32 tm_protocols;
  168. };
  169. static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
  170. {
  171. struct nci_rf_discover_param *param =
  172. (struct nci_rf_discover_param *)opt;
  173. struct nci_rf_disc_cmd cmd;
  174. cmd.num_disc_configs = 0;
  175. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  176. (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
  177. param->im_protocols & NFC_PROTO_MIFARE_MASK ||
  178. param->im_protocols & NFC_PROTO_ISO14443_MASK ||
  179. param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  180. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  181. NCI_NFC_A_PASSIVE_POLL_MODE;
  182. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  183. cmd.num_disc_configs++;
  184. }
  185. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  186. (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
  187. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  188. NCI_NFC_B_PASSIVE_POLL_MODE;
  189. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  190. cmd.num_disc_configs++;
  191. }
  192. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  193. (param->im_protocols & NFC_PROTO_FELICA_MASK ||
  194. param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  195. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  196. NCI_NFC_F_PASSIVE_POLL_MODE;
  197. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  198. cmd.num_disc_configs++;
  199. }
  200. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  201. (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
  202. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  203. NCI_NFC_V_PASSIVE_POLL_MODE;
  204. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  205. cmd.num_disc_configs++;
  206. }
  207. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
  208. (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  209. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  210. NCI_NFC_A_PASSIVE_LISTEN_MODE;
  211. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  212. cmd.num_disc_configs++;
  213. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  214. NCI_NFC_F_PASSIVE_LISTEN_MODE;
  215. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  216. cmd.num_disc_configs++;
  217. }
  218. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
  219. (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
  220. &cmd);
  221. }
  222. struct nci_rf_discover_select_param {
  223. __u8 rf_discovery_id;
  224. __u8 rf_protocol;
  225. };
  226. static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
  227. {
  228. struct nci_rf_discover_select_param *param =
  229. (struct nci_rf_discover_select_param *)opt;
  230. struct nci_rf_discover_select_cmd cmd;
  231. cmd.rf_discovery_id = param->rf_discovery_id;
  232. cmd.rf_protocol = param->rf_protocol;
  233. switch (cmd.rf_protocol) {
  234. case NCI_RF_PROTOCOL_ISO_DEP:
  235. cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
  236. break;
  237. case NCI_RF_PROTOCOL_NFC_DEP:
  238. cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
  239. break;
  240. default:
  241. cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
  242. break;
  243. }
  244. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
  245. sizeof(struct nci_rf_discover_select_cmd), &cmd);
  246. }
  247. static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
  248. {
  249. struct nci_rf_deactivate_cmd cmd;
  250. cmd.type = opt;
  251. nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
  252. sizeof(struct nci_rf_deactivate_cmd), &cmd);
  253. }
  254. static int nci_open_device(struct nci_dev *ndev)
  255. {
  256. int rc = 0;
  257. mutex_lock(&ndev->req_lock);
  258. if (test_bit(NCI_UP, &ndev->flags)) {
  259. rc = -EALREADY;
  260. goto done;
  261. }
  262. if (ndev->ops->open(ndev)) {
  263. rc = -EIO;
  264. goto done;
  265. }
  266. atomic_set(&ndev->cmd_cnt, 1);
  267. set_bit(NCI_INIT, &ndev->flags);
  268. rc = __nci_request(ndev, nci_reset_req, 0,
  269. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  270. if (ndev->ops->setup)
  271. ndev->ops->setup(ndev);
  272. if (!rc) {
  273. rc = __nci_request(ndev, nci_init_req, 0,
  274. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  275. }
  276. if (!rc) {
  277. rc = __nci_request(ndev, nci_init_complete_req, 0,
  278. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  279. }
  280. clear_bit(NCI_INIT, &ndev->flags);
  281. if (!rc) {
  282. set_bit(NCI_UP, &ndev->flags);
  283. nci_clear_target_list(ndev);
  284. atomic_set(&ndev->state, NCI_IDLE);
  285. } else {
  286. /* Init failed, cleanup */
  287. skb_queue_purge(&ndev->cmd_q);
  288. skb_queue_purge(&ndev->rx_q);
  289. skb_queue_purge(&ndev->tx_q);
  290. ndev->ops->close(ndev);
  291. ndev->flags = 0;
  292. }
  293. done:
  294. mutex_unlock(&ndev->req_lock);
  295. return rc;
  296. }
  297. static int nci_close_device(struct nci_dev *ndev)
  298. {
  299. nci_req_cancel(ndev, ENODEV);
  300. mutex_lock(&ndev->req_lock);
  301. if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
  302. del_timer_sync(&ndev->cmd_timer);
  303. del_timer_sync(&ndev->data_timer);
  304. mutex_unlock(&ndev->req_lock);
  305. return 0;
  306. }
  307. /* Drop RX and TX queues */
  308. skb_queue_purge(&ndev->rx_q);
  309. skb_queue_purge(&ndev->tx_q);
  310. /* Flush RX and TX wq */
  311. flush_workqueue(ndev->rx_wq);
  312. flush_workqueue(ndev->tx_wq);
  313. /* Reset device */
  314. skb_queue_purge(&ndev->cmd_q);
  315. atomic_set(&ndev->cmd_cnt, 1);
  316. set_bit(NCI_INIT, &ndev->flags);
  317. __nci_request(ndev, nci_reset_req, 0,
  318. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  319. clear_bit(NCI_INIT, &ndev->flags);
  320. del_timer_sync(&ndev->cmd_timer);
  321. /* Flush cmd wq */
  322. flush_workqueue(ndev->cmd_wq);
  323. /* After this point our queues are empty
  324. * and no works are scheduled. */
  325. ndev->ops->close(ndev);
  326. /* Clear flags */
  327. ndev->flags = 0;
  328. mutex_unlock(&ndev->req_lock);
  329. return 0;
  330. }
  331. /* NCI command timer function */
  332. static void nci_cmd_timer(unsigned long arg)
  333. {
  334. struct nci_dev *ndev = (void *) arg;
  335. atomic_set(&ndev->cmd_cnt, 1);
  336. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  337. }
  338. /* NCI data exchange timer function */
  339. static void nci_data_timer(unsigned long arg)
  340. {
  341. struct nci_dev *ndev = (void *) arg;
  342. set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  343. queue_work(ndev->rx_wq, &ndev->rx_work);
  344. }
  345. static int nci_dev_up(struct nfc_dev *nfc_dev)
  346. {
  347. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  348. return nci_open_device(ndev);
  349. }
  350. static int nci_dev_down(struct nfc_dev *nfc_dev)
  351. {
  352. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  353. return nci_close_device(ndev);
  354. }
  355. int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
  356. {
  357. struct nci_set_config_param param;
  358. if (!val || !len)
  359. return 0;
  360. param.id = id;
  361. param.len = len;
  362. param.val = val;
  363. return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  364. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  365. }
  366. EXPORT_SYMBOL(nci_set_config);
  367. static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
  368. {
  369. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  370. struct nci_set_config_param param;
  371. int rc;
  372. param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
  373. if ((param.val == NULL) || (param.len == 0))
  374. return 0;
  375. if (param.len > NFC_MAX_GT_LEN)
  376. return -EINVAL;
  377. param.id = NCI_PN_ATR_REQ_GEN_BYTES;
  378. rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  379. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  380. if (rc)
  381. return rc;
  382. param.id = NCI_LN_ATR_RES_GEN_BYTES;
  383. return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  384. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  385. }
  386. static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
  387. {
  388. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  389. int rc;
  390. __u8 val;
  391. val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
  392. rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
  393. if (rc)
  394. return rc;
  395. val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
  396. rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
  397. if (rc)
  398. return rc;
  399. val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
  400. return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
  401. }
  402. static int nci_start_poll(struct nfc_dev *nfc_dev,
  403. __u32 im_protocols, __u32 tm_protocols)
  404. {
  405. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  406. struct nci_rf_discover_param param;
  407. int rc;
  408. if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
  409. (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
  410. pr_err("unable to start poll, since poll is already active\n");
  411. return -EBUSY;
  412. }
  413. if (ndev->target_active_prot) {
  414. pr_err("there is an active target\n");
  415. return -EBUSY;
  416. }
  417. if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
  418. (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
  419. pr_debug("target active or w4 select, implicitly deactivate\n");
  420. rc = nci_request(ndev, nci_rf_deactivate_req,
  421. NCI_DEACTIVATE_TYPE_IDLE_MODE,
  422. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  423. if (rc)
  424. return -EBUSY;
  425. }
  426. if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
  427. rc = nci_set_local_general_bytes(nfc_dev);
  428. if (rc) {
  429. pr_err("failed to set local general bytes\n");
  430. return rc;
  431. }
  432. }
  433. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  434. rc = nci_set_listen_parameters(nfc_dev);
  435. if (rc)
  436. pr_err("failed to set listen parameters\n");
  437. }
  438. param.im_protocols = im_protocols;
  439. param.tm_protocols = tm_protocols;
  440. rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
  441. msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
  442. if (!rc)
  443. ndev->poll_prots = im_protocols;
  444. return rc;
  445. }
  446. static void nci_stop_poll(struct nfc_dev *nfc_dev)
  447. {
  448. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  449. if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
  450. (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
  451. pr_err("unable to stop poll, since poll is not active\n");
  452. return;
  453. }
  454. nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
  455. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  456. }
  457. static int nci_activate_target(struct nfc_dev *nfc_dev,
  458. struct nfc_target *target, __u32 protocol)
  459. {
  460. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  461. struct nci_rf_discover_select_param param;
  462. struct nfc_target *nci_target = NULL;
  463. int i;
  464. int rc = 0;
  465. pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
  466. if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
  467. (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
  468. pr_err("there is no available target to activate\n");
  469. return -EINVAL;
  470. }
  471. if (ndev->target_active_prot) {
  472. pr_err("there is already an active target\n");
  473. return -EBUSY;
  474. }
  475. for (i = 0; i < ndev->n_targets; i++) {
  476. if (ndev->targets[i].idx == target->idx) {
  477. nci_target = &ndev->targets[i];
  478. break;
  479. }
  480. }
  481. if (!nci_target) {
  482. pr_err("unable to find the selected target\n");
  483. return -EINVAL;
  484. }
  485. if (!(nci_target->supported_protocols & (1 << protocol))) {
  486. pr_err("target does not support the requested protocol 0x%x\n",
  487. protocol);
  488. return -EINVAL;
  489. }
  490. if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
  491. param.rf_discovery_id = nci_target->logical_idx;
  492. if (protocol == NFC_PROTO_JEWEL)
  493. param.rf_protocol = NCI_RF_PROTOCOL_T1T;
  494. else if (protocol == NFC_PROTO_MIFARE)
  495. param.rf_protocol = NCI_RF_PROTOCOL_T2T;
  496. else if (protocol == NFC_PROTO_FELICA)
  497. param.rf_protocol = NCI_RF_PROTOCOL_T3T;
  498. else if (protocol == NFC_PROTO_ISO14443 ||
  499. protocol == NFC_PROTO_ISO14443_B)
  500. param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  501. else
  502. param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  503. rc = nci_request(ndev, nci_rf_discover_select_req,
  504. (unsigned long)&param,
  505. msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
  506. }
  507. if (!rc)
  508. ndev->target_active_prot = protocol;
  509. return rc;
  510. }
  511. static void nci_deactivate_target(struct nfc_dev *nfc_dev,
  512. struct nfc_target *target)
  513. {
  514. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  515. pr_debug("entry\n");
  516. if (!ndev->target_active_prot) {
  517. pr_err("unable to deactivate target, no active target\n");
  518. return;
  519. }
  520. ndev->target_active_prot = 0;
  521. if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
  522. nci_request(ndev, nci_rf_deactivate_req,
  523. NCI_DEACTIVATE_TYPE_SLEEP_MODE,
  524. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  525. }
  526. }
  527. static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  528. __u8 comm_mode, __u8 *gb, size_t gb_len)
  529. {
  530. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  531. int rc;
  532. pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
  533. rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
  534. if (rc)
  535. return rc;
  536. rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
  537. ndev->remote_gb_len);
  538. if (!rc)
  539. rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
  540. NFC_RF_INITIATOR);
  541. return rc;
  542. }
  543. static int nci_dep_link_down(struct nfc_dev *nfc_dev)
  544. {
  545. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  546. int rc;
  547. pr_debug("entry\n");
  548. if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
  549. nci_deactivate_target(nfc_dev, NULL);
  550. } else {
  551. if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
  552. atomic_read(&ndev->state) == NCI_DISCOVERY) {
  553. nci_request(ndev, nci_rf_deactivate_req, 0,
  554. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  555. }
  556. rc = nfc_tm_deactivated(nfc_dev);
  557. if (rc)
  558. pr_err("error when signaling tm deactivation\n");
  559. }
  560. return 0;
  561. }
  562. static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  563. struct sk_buff *skb,
  564. data_exchange_cb_t cb, void *cb_context)
  565. {
  566. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  567. int rc;
  568. pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
  569. if (!ndev->target_active_prot) {
  570. pr_err("unable to exchange data, no active target\n");
  571. return -EINVAL;
  572. }
  573. if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  574. return -EBUSY;
  575. /* store cb and context to be used on receiving data */
  576. ndev->data_exchange_cb = cb;
  577. ndev->data_exchange_cb_context = cb_context;
  578. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  579. if (rc)
  580. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  581. return rc;
  582. }
  583. static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  584. {
  585. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  586. int rc;
  587. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  588. if (rc)
  589. pr_err("unable to send data\n");
  590. return rc;
  591. }
  592. static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  593. {
  594. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  595. if (ndev->ops->enable_se)
  596. return ndev->ops->enable_se(ndev, se_idx);
  597. return 0;
  598. }
  599. static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  600. {
  601. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  602. if (ndev->ops->disable_se)
  603. return ndev->ops->disable_se(ndev, se_idx);
  604. return 0;
  605. }
  606. static int nci_discover_se(struct nfc_dev *nfc_dev)
  607. {
  608. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  609. if (ndev->ops->discover_se)
  610. return ndev->ops->discover_se(ndev);
  611. return 0;
  612. }
  613. static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
  614. u8 *apdu, size_t apdu_length,
  615. se_io_cb_t cb, void *cb_context)
  616. {
  617. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  618. if (ndev->ops->se_io)
  619. return ndev->ops->se_io(ndev, se_idx, apdu,
  620. apdu_length, cb, cb_context);
  621. return 0;
  622. }
  623. static struct nfc_ops nci_nfc_ops = {
  624. .dev_up = nci_dev_up,
  625. .dev_down = nci_dev_down,
  626. .start_poll = nci_start_poll,
  627. .stop_poll = nci_stop_poll,
  628. .dep_link_up = nci_dep_link_up,
  629. .dep_link_down = nci_dep_link_down,
  630. .activate_target = nci_activate_target,
  631. .deactivate_target = nci_deactivate_target,
  632. .im_transceive = nci_transceive,
  633. .tm_send = nci_tm_send,
  634. .enable_se = nci_enable_se,
  635. .disable_se = nci_disable_se,
  636. .discover_se = nci_discover_se,
  637. .se_io = nci_se_io,
  638. };
  639. /* ---- Interface to NCI drivers ---- */
  640. /**
  641. * nci_allocate_device - allocate a new nci device
  642. *
  643. * @ops: device operations
  644. * @supported_protocols: NFC protocols supported by the device
  645. */
  646. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  647. __u32 supported_protocols,
  648. int tx_headroom, int tx_tailroom)
  649. {
  650. struct nci_dev *ndev;
  651. pr_debug("supported_protocols 0x%x\n", supported_protocols);
  652. if (!ops->open || !ops->close || !ops->send)
  653. return NULL;
  654. if (!supported_protocols)
  655. return NULL;
  656. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  657. if (!ndev)
  658. return NULL;
  659. ndev->ops = ops;
  660. ndev->tx_headroom = tx_headroom;
  661. ndev->tx_tailroom = tx_tailroom;
  662. init_completion(&ndev->req_completion);
  663. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  664. supported_protocols,
  665. tx_headroom + NCI_DATA_HDR_SIZE,
  666. tx_tailroom);
  667. if (!ndev->nfc_dev)
  668. goto free_exit;
  669. nfc_set_drvdata(ndev->nfc_dev, ndev);
  670. return ndev;
  671. free_exit:
  672. kfree(ndev);
  673. return NULL;
  674. }
  675. EXPORT_SYMBOL(nci_allocate_device);
  676. /**
  677. * nci_free_device - deallocate nci device
  678. *
  679. * @ndev: The nci device to deallocate
  680. */
  681. void nci_free_device(struct nci_dev *ndev)
  682. {
  683. nfc_free_device(ndev->nfc_dev);
  684. kfree(ndev);
  685. }
  686. EXPORT_SYMBOL(nci_free_device);
  687. /**
  688. * nci_register_device - register a nci device in the nfc subsystem
  689. *
  690. * @dev: The nci device to register
  691. */
  692. int nci_register_device(struct nci_dev *ndev)
  693. {
  694. int rc;
  695. struct device *dev = &ndev->nfc_dev->dev;
  696. char name[32];
  697. ndev->flags = 0;
  698. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  699. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  700. ndev->cmd_wq = create_singlethread_workqueue(name);
  701. if (!ndev->cmd_wq) {
  702. rc = -ENOMEM;
  703. goto exit;
  704. }
  705. INIT_WORK(&ndev->rx_work, nci_rx_work);
  706. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  707. ndev->rx_wq = create_singlethread_workqueue(name);
  708. if (!ndev->rx_wq) {
  709. rc = -ENOMEM;
  710. goto destroy_cmd_wq_exit;
  711. }
  712. INIT_WORK(&ndev->tx_work, nci_tx_work);
  713. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  714. ndev->tx_wq = create_singlethread_workqueue(name);
  715. if (!ndev->tx_wq) {
  716. rc = -ENOMEM;
  717. goto destroy_rx_wq_exit;
  718. }
  719. skb_queue_head_init(&ndev->cmd_q);
  720. skb_queue_head_init(&ndev->rx_q);
  721. skb_queue_head_init(&ndev->tx_q);
  722. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  723. (unsigned long) ndev);
  724. setup_timer(&ndev->data_timer, nci_data_timer,
  725. (unsigned long) ndev);
  726. mutex_init(&ndev->req_lock);
  727. rc = nfc_register_device(ndev->nfc_dev);
  728. if (rc)
  729. goto destroy_rx_wq_exit;
  730. goto exit;
  731. destroy_rx_wq_exit:
  732. destroy_workqueue(ndev->rx_wq);
  733. destroy_cmd_wq_exit:
  734. destroy_workqueue(ndev->cmd_wq);
  735. exit:
  736. return rc;
  737. }
  738. EXPORT_SYMBOL(nci_register_device);
  739. /**
  740. * nci_unregister_device - unregister a nci device in the nfc subsystem
  741. *
  742. * @dev: The nci device to unregister
  743. */
  744. void nci_unregister_device(struct nci_dev *ndev)
  745. {
  746. nci_close_device(ndev);
  747. destroy_workqueue(ndev->cmd_wq);
  748. destroy_workqueue(ndev->rx_wq);
  749. destroy_workqueue(ndev->tx_wq);
  750. nfc_unregister_device(ndev->nfc_dev);
  751. }
  752. EXPORT_SYMBOL(nci_unregister_device);
  753. /**
  754. * nci_recv_frame - receive frame from NCI drivers
  755. *
  756. * @ndev: The nci device
  757. * @skb: The sk_buff to receive
  758. */
  759. int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  760. {
  761. pr_debug("len %d\n", skb->len);
  762. if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
  763. !test_bit(NCI_INIT, &ndev->flags))) {
  764. kfree_skb(skb);
  765. return -ENXIO;
  766. }
  767. /* Queue frame for rx worker thread */
  768. skb_queue_tail(&ndev->rx_q, skb);
  769. queue_work(ndev->rx_wq, &ndev->rx_work);
  770. return 0;
  771. }
  772. EXPORT_SYMBOL(nci_recv_frame);
  773. static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
  774. {
  775. pr_debug("len %d\n", skb->len);
  776. if (!ndev) {
  777. kfree_skb(skb);
  778. return -ENODEV;
  779. }
  780. /* Get rid of skb owner, prior to sending to the driver. */
  781. skb_orphan(skb);
  782. /* Send copy to sniffer */
  783. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  784. RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
  785. return ndev->ops->send(ndev, skb);
  786. }
  787. /* Send NCI command */
  788. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  789. {
  790. struct nci_ctrl_hdr *hdr;
  791. struct sk_buff *skb;
  792. pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
  793. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  794. if (!skb) {
  795. pr_err("no memory for command\n");
  796. return -ENOMEM;
  797. }
  798. hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
  799. hdr->gid = nci_opcode_gid(opcode);
  800. hdr->oid = nci_opcode_oid(opcode);
  801. hdr->plen = plen;
  802. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  803. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  804. if (plen)
  805. memcpy(skb_put(skb, plen), payload, plen);
  806. skb_queue_tail(&ndev->cmd_q, skb);
  807. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  808. return 0;
  809. }
  810. /* ---- NCI TX Data worker thread ---- */
  811. static void nci_tx_work(struct work_struct *work)
  812. {
  813. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  814. struct sk_buff *skb;
  815. pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
  816. /* Send queued tx data */
  817. while (atomic_read(&ndev->credits_cnt)) {
  818. skb = skb_dequeue(&ndev->tx_q);
  819. if (!skb)
  820. return;
  821. /* Check if data flow control is used */
  822. if (atomic_read(&ndev->credits_cnt) !=
  823. NCI_DATA_FLOW_CONTROL_NOT_USED)
  824. atomic_dec(&ndev->credits_cnt);
  825. pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  826. nci_pbf(skb->data),
  827. nci_conn_id(skb->data),
  828. nci_plen(skb->data));
  829. nci_send_frame(ndev, skb);
  830. mod_timer(&ndev->data_timer,
  831. jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
  832. }
  833. }
  834. /* ----- NCI RX worker thread (data & control) ----- */
  835. static void nci_rx_work(struct work_struct *work)
  836. {
  837. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  838. struct sk_buff *skb;
  839. while ((skb = skb_dequeue(&ndev->rx_q))) {
  840. /* Send copy to sniffer */
  841. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  842. RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
  843. /* Process frame */
  844. switch (nci_mt(skb->data)) {
  845. case NCI_MT_RSP_PKT:
  846. nci_rsp_packet(ndev, skb);
  847. break;
  848. case NCI_MT_NTF_PKT:
  849. nci_ntf_packet(ndev, skb);
  850. break;
  851. case NCI_MT_DATA_PKT:
  852. nci_rx_data_packet(ndev, skb);
  853. break;
  854. default:
  855. pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
  856. kfree_skb(skb);
  857. break;
  858. }
  859. }
  860. /* check if a data exchange timout has occurred */
  861. if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
  862. /* complete the data exchange transaction, if exists */
  863. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  864. nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
  865. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  866. }
  867. }
  868. /* ----- NCI TX CMD worker thread ----- */
  869. static void nci_cmd_work(struct work_struct *work)
  870. {
  871. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  872. struct sk_buff *skb;
  873. pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
  874. /* Send queued command */
  875. if (atomic_read(&ndev->cmd_cnt)) {
  876. skb = skb_dequeue(&ndev->cmd_q);
  877. if (!skb)
  878. return;
  879. atomic_dec(&ndev->cmd_cnt);
  880. pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  881. nci_pbf(skb->data),
  882. nci_opcode_gid(nci_opcode(skb->data)),
  883. nci_opcode_oid(nci_opcode(skb->data)),
  884. nci_plen(skb->data));
  885. nci_send_frame(ndev, skb);
  886. mod_timer(&ndev->cmd_timer,
  887. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  888. }
  889. }
  890. MODULE_LICENSE("GPL");