core.c 18 KB

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