core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. *
  7. * Written by Ilan Elias <ilane@ti.com>
  8. *
  9. * Acknowledgements:
  10. * This file is based on hci_core.c, which was written
  11. * by Maxim Krasnyansky.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <linux/types.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/completion.h>
  30. #include <linux/export.h>
  31. #include <linux/sched.h>
  32. #include <linux/bitops.h>
  33. #include <linux/skbuff.h>
  34. #include "../nfc.h"
  35. #include <net/nfc/nci.h>
  36. #include <net/nfc/nci_core.h>
  37. #include <linux/nfc.h>
  38. static void nci_cmd_work(struct work_struct *work);
  39. static void nci_rx_work(struct work_struct *work);
  40. static void nci_tx_work(struct work_struct *work);
  41. /* ---- NCI requests ---- */
  42. void nci_req_complete(struct nci_dev *ndev, int result)
  43. {
  44. if (ndev->req_status == NCI_REQ_PEND) {
  45. ndev->req_result = result;
  46. ndev->req_status = NCI_REQ_DONE;
  47. complete(&ndev->req_completion);
  48. }
  49. }
  50. static void nci_req_cancel(struct nci_dev *ndev, int err)
  51. {
  52. if (ndev->req_status == NCI_REQ_PEND) {
  53. ndev->req_result = err;
  54. ndev->req_status = NCI_REQ_CANCELED;
  55. complete(&ndev->req_completion);
  56. }
  57. }
  58. /* Execute request and wait for completion. */
  59. static int __nci_request(struct nci_dev *ndev,
  60. void (*req)(struct nci_dev *ndev, unsigned long opt),
  61. unsigned long opt,
  62. __u32 timeout)
  63. {
  64. int rc = 0;
  65. unsigned long completion_rc;
  66. ndev->req_status = NCI_REQ_PEND;
  67. init_completion(&ndev->req_completion);
  68. req(ndev, opt);
  69. completion_rc = wait_for_completion_interruptible_timeout(
  70. &ndev->req_completion,
  71. timeout);
  72. nfc_dbg("wait_for_completion return %ld", completion_rc);
  73. if (completion_rc > 0) {
  74. switch (ndev->req_status) {
  75. case NCI_REQ_DONE:
  76. rc = nci_to_errno(ndev->req_result);
  77. break;
  78. case NCI_REQ_CANCELED:
  79. rc = -ndev->req_result;
  80. break;
  81. default:
  82. rc = -ETIMEDOUT;
  83. break;
  84. }
  85. } else {
  86. nfc_err("wait_for_completion_interruptible_timeout failed %ld",
  87. completion_rc);
  88. rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
  89. }
  90. ndev->req_status = ndev->req_result = 0;
  91. return rc;
  92. }
  93. static inline int nci_request(struct nci_dev *ndev,
  94. void (*req)(struct nci_dev *ndev, unsigned long opt),
  95. unsigned long opt, __u32 timeout)
  96. {
  97. int rc;
  98. if (!test_bit(NCI_UP, &ndev->flags))
  99. return -ENETDOWN;
  100. /* Serialize all requests */
  101. mutex_lock(&ndev->req_lock);
  102. rc = __nci_request(ndev, req, opt, timeout);
  103. mutex_unlock(&ndev->req_lock);
  104. return rc;
  105. }
  106. static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
  107. {
  108. nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 0, NULL);
  109. }
  110. static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
  111. {
  112. nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
  113. }
  114. static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
  115. {
  116. struct nci_core_conn_create_cmd conn_cmd;
  117. struct nci_rf_disc_map_cmd cmd;
  118. struct disc_map_config *cfg = cmd.mapping_configs;
  119. __u8 *num = &cmd.num_mapping_configs;
  120. int i;
  121. /* create static rf connection */
  122. conn_cmd.target_handle = 0;
  123. conn_cmd.num_target_specific_params = 0;
  124. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, 2, &conn_cmd);
  125. /* set rf mapping configurations */
  126. *num = 0;
  127. /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
  128. for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
  129. if (ndev->supported_rf_interfaces[i] ==
  130. NCI_RF_INTERFACE_ISO_DEP) {
  131. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  132. cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
  133. cfg[*num].rf_interface_type = 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_BOTH;
  139. cfg[*num].rf_interface_type = NCI_RF_INTERFACE_NFC_DEP;
  140. (*num)++;
  141. }
  142. if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
  143. break;
  144. }
  145. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
  146. (1 + ((*num)*sizeof(struct disc_map_config))),
  147. &cmd);
  148. }
  149. static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
  150. {
  151. struct nci_rf_disc_cmd cmd;
  152. __u32 protocols = opt;
  153. cmd.num_disc_configs = 0;
  154. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  155. (protocols & NFC_PROTO_JEWEL_MASK
  156. || protocols & NFC_PROTO_MIFARE_MASK
  157. || protocols & NFC_PROTO_ISO14443_MASK
  158. || protocols & NFC_PROTO_NFC_DEP_MASK)) {
  159. cmd.disc_configs[cmd.num_disc_configs].type =
  160. NCI_DISCOVERY_TYPE_POLL_A_PASSIVE;
  161. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  162. cmd.num_disc_configs++;
  163. }
  164. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  165. (protocols & NFC_PROTO_ISO14443_MASK)) {
  166. cmd.disc_configs[cmd.num_disc_configs].type =
  167. NCI_DISCOVERY_TYPE_POLL_B_PASSIVE;
  168. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  169. cmd.num_disc_configs++;
  170. }
  171. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  172. (protocols & NFC_PROTO_FELICA_MASK
  173. || protocols & NFC_PROTO_NFC_DEP_MASK)) {
  174. cmd.disc_configs[cmd.num_disc_configs].type =
  175. NCI_DISCOVERY_TYPE_POLL_F_PASSIVE;
  176. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  177. cmd.num_disc_configs++;
  178. }
  179. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
  180. (1 + (cmd.num_disc_configs*sizeof(struct disc_config))),
  181. &cmd);
  182. }
  183. static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
  184. {
  185. struct nci_rf_deactivate_cmd cmd;
  186. cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
  187. nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
  188. sizeof(struct nci_rf_deactivate_cmd),
  189. &cmd);
  190. }
  191. static int nci_open_device(struct nci_dev *ndev)
  192. {
  193. int rc = 0;
  194. mutex_lock(&ndev->req_lock);
  195. if (test_bit(NCI_UP, &ndev->flags)) {
  196. rc = -EALREADY;
  197. goto done;
  198. }
  199. if (ndev->ops->open(ndev)) {
  200. rc = -EIO;
  201. goto done;
  202. }
  203. atomic_set(&ndev->cmd_cnt, 1);
  204. set_bit(NCI_INIT, &ndev->flags);
  205. rc = __nci_request(ndev, nci_reset_req, 0,
  206. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  207. if (!rc) {
  208. rc = __nci_request(ndev, nci_init_req, 0,
  209. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  210. }
  211. if (!rc) {
  212. rc = __nci_request(ndev, nci_init_complete_req, 0,
  213. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  214. }
  215. clear_bit(NCI_INIT, &ndev->flags);
  216. if (!rc) {
  217. set_bit(NCI_UP, &ndev->flags);
  218. } else {
  219. /* Init failed, cleanup */
  220. skb_queue_purge(&ndev->cmd_q);
  221. skb_queue_purge(&ndev->rx_q);
  222. skb_queue_purge(&ndev->tx_q);
  223. ndev->ops->close(ndev);
  224. ndev->flags = 0;
  225. }
  226. done:
  227. mutex_unlock(&ndev->req_lock);
  228. return rc;
  229. }
  230. static int nci_close_device(struct nci_dev *ndev)
  231. {
  232. nci_req_cancel(ndev, ENODEV);
  233. mutex_lock(&ndev->req_lock);
  234. if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
  235. del_timer_sync(&ndev->cmd_timer);
  236. mutex_unlock(&ndev->req_lock);
  237. return 0;
  238. }
  239. /* Drop RX and TX queues */
  240. skb_queue_purge(&ndev->rx_q);
  241. skb_queue_purge(&ndev->tx_q);
  242. /* Flush RX and TX wq */
  243. flush_workqueue(ndev->rx_wq);
  244. flush_workqueue(ndev->tx_wq);
  245. /* Reset device */
  246. skb_queue_purge(&ndev->cmd_q);
  247. atomic_set(&ndev->cmd_cnt, 1);
  248. set_bit(NCI_INIT, &ndev->flags);
  249. __nci_request(ndev, nci_reset_req, 0,
  250. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  251. clear_bit(NCI_INIT, &ndev->flags);
  252. /* Flush cmd wq */
  253. flush_workqueue(ndev->cmd_wq);
  254. /* After this point our queues are empty
  255. * and no works are scheduled. */
  256. ndev->ops->close(ndev);
  257. /* Clear flags */
  258. ndev->flags = 0;
  259. mutex_unlock(&ndev->req_lock);
  260. return 0;
  261. }
  262. /* NCI command timer function */
  263. static void nci_cmd_timer(unsigned long arg)
  264. {
  265. struct nci_dev *ndev = (void *) arg;
  266. nfc_dbg("entry");
  267. atomic_set(&ndev->cmd_cnt, 1);
  268. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  269. }
  270. static int nci_dev_up(struct nfc_dev *nfc_dev)
  271. {
  272. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  273. nfc_dbg("entry");
  274. return nci_open_device(ndev);
  275. }
  276. static int nci_dev_down(struct nfc_dev *nfc_dev)
  277. {
  278. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  279. nfc_dbg("entry");
  280. return nci_close_device(ndev);
  281. }
  282. static int nci_start_poll(struct nfc_dev *nfc_dev, __u32 protocols)
  283. {
  284. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  285. int rc;
  286. nfc_dbg("entry");
  287. if (test_bit(NCI_DISCOVERY, &ndev->flags)) {
  288. nfc_err("unable to start poll, since poll is already active");
  289. return -EBUSY;
  290. }
  291. if (ndev->target_active_prot) {
  292. nfc_err("there is an active target");
  293. return -EBUSY;
  294. }
  295. if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
  296. nfc_dbg("target is active, implicitly deactivate...");
  297. rc = nci_request(ndev, nci_rf_deactivate_req, 0,
  298. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  299. if (rc)
  300. return -EBUSY;
  301. }
  302. rc = nci_request(ndev, nci_rf_discover_req, protocols,
  303. msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
  304. if (!rc)
  305. ndev->poll_prots = protocols;
  306. return rc;
  307. }
  308. static void nci_stop_poll(struct nfc_dev *nfc_dev)
  309. {
  310. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  311. nfc_dbg("entry");
  312. if (!test_bit(NCI_DISCOVERY, &ndev->flags)) {
  313. nfc_err("unable to stop poll, since poll is not active");
  314. return;
  315. }
  316. nci_request(ndev, nci_rf_deactivate_req, 0,
  317. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  318. }
  319. static int nci_activate_target(struct nfc_dev *nfc_dev, __u32 target_idx,
  320. __u32 protocol)
  321. {
  322. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  323. nfc_dbg("entry, target_idx %d, protocol 0x%x", target_idx, protocol);
  324. if (!test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
  325. nfc_err("there is no available target to activate");
  326. return -EINVAL;
  327. }
  328. if (ndev->target_active_prot) {
  329. nfc_err("there is already an active target");
  330. return -EBUSY;
  331. }
  332. if (!(ndev->target_available_prots & (1 << protocol))) {
  333. nfc_err("target does not support the requested protocol 0x%x",
  334. protocol);
  335. return -EINVAL;
  336. }
  337. ndev->target_active_prot = protocol;
  338. ndev->target_available_prots = 0;
  339. return 0;
  340. }
  341. static void nci_deactivate_target(struct nfc_dev *nfc_dev, __u32 target_idx)
  342. {
  343. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  344. nfc_dbg("entry, target_idx %d", target_idx);
  345. if (!ndev->target_active_prot) {
  346. nfc_err("unable to deactivate target, no active target");
  347. return;
  348. }
  349. ndev->target_active_prot = 0;
  350. if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
  351. nci_request(ndev, nci_rf_deactivate_req, 0,
  352. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  353. }
  354. }
  355. static int nci_data_exchange(struct nfc_dev *nfc_dev, __u32 target_idx,
  356. struct sk_buff *skb,
  357. data_exchange_cb_t cb,
  358. void *cb_context)
  359. {
  360. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  361. int rc;
  362. nfc_dbg("entry, target_idx %d, len %d", target_idx, skb->len);
  363. if (!ndev->target_active_prot) {
  364. nfc_err("unable to exchange data, no active target");
  365. return -EINVAL;
  366. }
  367. if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  368. return -EBUSY;
  369. /* store cb and context to be used on receiving data */
  370. ndev->data_exchange_cb = cb;
  371. ndev->data_exchange_cb_context = cb_context;
  372. rc = nci_send_data(ndev, ndev->conn_id, skb);
  373. if (rc)
  374. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  375. return rc;
  376. }
  377. static struct nfc_ops nci_nfc_ops = {
  378. .dev_up = nci_dev_up,
  379. .dev_down = nci_dev_down,
  380. .start_poll = nci_start_poll,
  381. .stop_poll = nci_stop_poll,
  382. .activate_target = nci_activate_target,
  383. .deactivate_target = nci_deactivate_target,
  384. .data_exchange = nci_data_exchange,
  385. };
  386. /* ---- Interface to NCI drivers ---- */
  387. /**
  388. * nci_allocate_device - allocate a new nci device
  389. *
  390. * @ops: device operations
  391. * @supported_protocols: NFC protocols supported by the device
  392. */
  393. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  394. __u32 supported_protocols,
  395. int tx_headroom,
  396. int tx_tailroom)
  397. {
  398. struct nci_dev *ndev;
  399. nfc_dbg("entry, supported_protocols 0x%x", supported_protocols);
  400. if (!ops->open || !ops->close || !ops->send)
  401. return NULL;
  402. if (!supported_protocols)
  403. return NULL;
  404. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  405. if (!ndev)
  406. return NULL;
  407. ndev->ops = ops;
  408. ndev->tx_headroom = tx_headroom;
  409. ndev->tx_tailroom = tx_tailroom;
  410. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  411. supported_protocols,
  412. tx_headroom + NCI_DATA_HDR_SIZE,
  413. tx_tailroom);
  414. if (!ndev->nfc_dev)
  415. goto free_exit;
  416. nfc_set_drvdata(ndev->nfc_dev, ndev);
  417. return ndev;
  418. free_exit:
  419. kfree(ndev);
  420. return NULL;
  421. }
  422. EXPORT_SYMBOL(nci_allocate_device);
  423. /**
  424. * nci_free_device - deallocate nci device
  425. *
  426. * @ndev: The nci device to deallocate
  427. */
  428. void nci_free_device(struct nci_dev *ndev)
  429. {
  430. nfc_dbg("entry");
  431. nfc_free_device(ndev->nfc_dev);
  432. kfree(ndev);
  433. }
  434. EXPORT_SYMBOL(nci_free_device);
  435. /**
  436. * nci_register_device - register a nci device in the nfc subsystem
  437. *
  438. * @dev: The nci device to register
  439. */
  440. int nci_register_device(struct nci_dev *ndev)
  441. {
  442. int rc;
  443. struct device *dev = &ndev->nfc_dev->dev;
  444. char name[32];
  445. nfc_dbg("entry");
  446. rc = nfc_register_device(ndev->nfc_dev);
  447. if (rc)
  448. goto exit;
  449. ndev->flags = 0;
  450. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  451. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  452. ndev->cmd_wq = create_singlethread_workqueue(name);
  453. if (!ndev->cmd_wq) {
  454. rc = -ENOMEM;
  455. goto unreg_exit;
  456. }
  457. INIT_WORK(&ndev->rx_work, nci_rx_work);
  458. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  459. ndev->rx_wq = create_singlethread_workqueue(name);
  460. if (!ndev->rx_wq) {
  461. rc = -ENOMEM;
  462. goto destroy_cmd_wq_exit;
  463. }
  464. INIT_WORK(&ndev->tx_work, nci_tx_work);
  465. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  466. ndev->tx_wq = create_singlethread_workqueue(name);
  467. if (!ndev->tx_wq) {
  468. rc = -ENOMEM;
  469. goto destroy_rx_wq_exit;
  470. }
  471. skb_queue_head_init(&ndev->cmd_q);
  472. skb_queue_head_init(&ndev->rx_q);
  473. skb_queue_head_init(&ndev->tx_q);
  474. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  475. (unsigned long) ndev);
  476. mutex_init(&ndev->req_lock);
  477. goto exit;
  478. destroy_rx_wq_exit:
  479. destroy_workqueue(ndev->rx_wq);
  480. destroy_cmd_wq_exit:
  481. destroy_workqueue(ndev->cmd_wq);
  482. unreg_exit:
  483. nfc_unregister_device(ndev->nfc_dev);
  484. exit:
  485. return rc;
  486. }
  487. EXPORT_SYMBOL(nci_register_device);
  488. /**
  489. * nci_unregister_device - unregister a nci device in the nfc subsystem
  490. *
  491. * @dev: The nci device to unregister
  492. */
  493. void nci_unregister_device(struct nci_dev *ndev)
  494. {
  495. nfc_dbg("entry");
  496. nci_close_device(ndev);
  497. destroy_workqueue(ndev->cmd_wq);
  498. destroy_workqueue(ndev->rx_wq);
  499. destroy_workqueue(ndev->tx_wq);
  500. nfc_unregister_device(ndev->nfc_dev);
  501. }
  502. EXPORT_SYMBOL(nci_unregister_device);
  503. /**
  504. * nci_recv_frame - receive frame from NCI drivers
  505. *
  506. * @skb: The sk_buff to receive
  507. */
  508. int nci_recv_frame(struct sk_buff *skb)
  509. {
  510. struct nci_dev *ndev = (struct nci_dev *) skb->dev;
  511. nfc_dbg("entry, len %d", skb->len);
  512. if (!ndev || (!test_bit(NCI_UP, &ndev->flags)
  513. && !test_bit(NCI_INIT, &ndev->flags))) {
  514. kfree_skb(skb);
  515. return -ENXIO;
  516. }
  517. /* Queue frame for rx worker thread */
  518. skb_queue_tail(&ndev->rx_q, skb);
  519. queue_work(ndev->rx_wq, &ndev->rx_work);
  520. return 0;
  521. }
  522. EXPORT_SYMBOL(nci_recv_frame);
  523. static int nci_send_frame(struct sk_buff *skb)
  524. {
  525. struct nci_dev *ndev = (struct nci_dev *) skb->dev;
  526. nfc_dbg("entry, len %d", skb->len);
  527. if (!ndev) {
  528. kfree_skb(skb);
  529. return -ENODEV;
  530. }
  531. /* Get rid of skb owner, prior to sending to the driver. */
  532. skb_orphan(skb);
  533. return ndev->ops->send(skb);
  534. }
  535. /* Send NCI command */
  536. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  537. {
  538. struct nci_ctrl_hdr *hdr;
  539. struct sk_buff *skb;
  540. nfc_dbg("entry, opcode 0x%x, plen %d", opcode, plen);
  541. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  542. if (!skb) {
  543. nfc_err("no memory for command");
  544. return -ENOMEM;
  545. }
  546. hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
  547. hdr->gid = nci_opcode_gid(opcode);
  548. hdr->oid = nci_opcode_oid(opcode);
  549. hdr->plen = plen;
  550. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  551. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  552. if (plen)
  553. memcpy(skb_put(skb, plen), payload, plen);
  554. skb->dev = (void *) ndev;
  555. skb_queue_tail(&ndev->cmd_q, skb);
  556. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  557. return 0;
  558. }
  559. /* ---- NCI TX Data worker thread ---- */
  560. static void nci_tx_work(struct work_struct *work)
  561. {
  562. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  563. struct sk_buff *skb;
  564. nfc_dbg("entry, credits_cnt %d", atomic_read(&ndev->credits_cnt));
  565. /* Send queued tx data */
  566. while (atomic_read(&ndev->credits_cnt)) {
  567. skb = skb_dequeue(&ndev->tx_q);
  568. if (!skb)
  569. return;
  570. atomic_dec(&ndev->credits_cnt);
  571. nfc_dbg("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d",
  572. nci_pbf(skb->data),
  573. nci_conn_id(skb->data),
  574. nci_plen(skb->data));
  575. nci_send_frame(skb);
  576. }
  577. }
  578. /* ----- NCI RX worker thread (data & control) ----- */
  579. static void nci_rx_work(struct work_struct *work)
  580. {
  581. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  582. struct sk_buff *skb;
  583. while ((skb = skb_dequeue(&ndev->rx_q))) {
  584. /* Process frame */
  585. switch (nci_mt(skb->data)) {
  586. case NCI_MT_RSP_PKT:
  587. nci_rsp_packet(ndev, skb);
  588. break;
  589. case NCI_MT_NTF_PKT:
  590. nci_ntf_packet(ndev, skb);
  591. break;
  592. case NCI_MT_DATA_PKT:
  593. nci_rx_data_packet(ndev, skb);
  594. break;
  595. default:
  596. nfc_err("unknown MT 0x%x", nci_mt(skb->data));
  597. kfree_skb(skb);
  598. break;
  599. }
  600. }
  601. }
  602. /* ----- NCI TX CMD worker thread ----- */
  603. static void nci_cmd_work(struct work_struct *work)
  604. {
  605. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  606. struct sk_buff *skb;
  607. nfc_dbg("entry, cmd_cnt %d", atomic_read(&ndev->cmd_cnt));
  608. /* Send queued command */
  609. if (atomic_read(&ndev->cmd_cnt)) {
  610. skb = skb_dequeue(&ndev->cmd_q);
  611. if (!skb)
  612. return;
  613. atomic_dec(&ndev->cmd_cnt);
  614. nfc_dbg("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
  615. nci_pbf(skb->data),
  616. nci_opcode_gid(nci_opcode(skb->data)),
  617. nci_opcode_oid(nci_opcode(skb->data)),
  618. nci_plen(skb->data));
  619. nci_send_frame(skb);
  620. mod_timer(&ndev->cmd_timer,
  621. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  622. }
  623. }