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. unsigned 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. struct nci_core_reset_cmd cmd;
  108. cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
  109. nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
  110. }
  111. static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
  112. {
  113. nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
  114. }
  115. static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
  116. {
  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. /* set rf mapping configurations */
  122. *num = 0;
  123. /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
  124. for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
  125. if (ndev->supported_rf_interfaces[i] ==
  126. NCI_RF_INTERFACE_ISO_DEP) {
  127. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  128. cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
  129. cfg[*num].rf_interface_type = NCI_RF_INTERFACE_ISO_DEP;
  130. (*num)++;
  131. } else if (ndev->supported_rf_interfaces[i] ==
  132. NCI_RF_INTERFACE_NFC_DEP) {
  133. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  134. cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
  135. cfg[*num].rf_interface_type = NCI_RF_INTERFACE_NFC_DEP;
  136. (*num)++;
  137. }
  138. if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
  139. break;
  140. }
  141. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
  142. (1 + ((*num)*sizeof(struct disc_map_config))),
  143. &cmd);
  144. }
  145. static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
  146. {
  147. struct nci_rf_disc_cmd cmd;
  148. __u32 protocols = opt;
  149. cmd.num_disc_configs = 0;
  150. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  151. (protocols & NFC_PROTO_JEWEL_MASK
  152. || protocols & NFC_PROTO_MIFARE_MASK
  153. || protocols & NFC_PROTO_ISO14443_MASK
  154. || protocols & NFC_PROTO_NFC_DEP_MASK)) {
  155. cmd.disc_configs[cmd.num_disc_configs].type =
  156. NCI_DISCOVERY_TYPE_POLL_A_PASSIVE;
  157. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  158. cmd.num_disc_configs++;
  159. }
  160. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  161. (protocols & NFC_PROTO_ISO14443_MASK)) {
  162. cmd.disc_configs[cmd.num_disc_configs].type =
  163. NCI_DISCOVERY_TYPE_POLL_B_PASSIVE;
  164. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  165. cmd.num_disc_configs++;
  166. }
  167. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  168. (protocols & NFC_PROTO_FELICA_MASK
  169. || protocols & NFC_PROTO_NFC_DEP_MASK)) {
  170. cmd.disc_configs[cmd.num_disc_configs].type =
  171. NCI_DISCOVERY_TYPE_POLL_F_PASSIVE;
  172. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  173. cmd.num_disc_configs++;
  174. }
  175. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
  176. (1 + (cmd.num_disc_configs*sizeof(struct disc_config))),
  177. &cmd);
  178. }
  179. static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
  180. {
  181. struct nci_rf_deactivate_cmd cmd;
  182. cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
  183. nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
  184. sizeof(struct nci_rf_deactivate_cmd),
  185. &cmd);
  186. }
  187. static int nci_open_device(struct nci_dev *ndev)
  188. {
  189. int rc = 0;
  190. mutex_lock(&ndev->req_lock);
  191. if (test_bit(NCI_UP, &ndev->flags)) {
  192. rc = -EALREADY;
  193. goto done;
  194. }
  195. if (ndev->ops->open(ndev)) {
  196. rc = -EIO;
  197. goto done;
  198. }
  199. atomic_set(&ndev->cmd_cnt, 1);
  200. set_bit(NCI_INIT, &ndev->flags);
  201. rc = __nci_request(ndev, nci_reset_req, 0,
  202. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  203. if (!rc) {
  204. rc = __nci_request(ndev, nci_init_req, 0,
  205. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  206. }
  207. if (!rc) {
  208. rc = __nci_request(ndev, nci_init_complete_req, 0,
  209. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  210. }
  211. clear_bit(NCI_INIT, &ndev->flags);
  212. if (!rc) {
  213. set_bit(NCI_UP, &ndev->flags);
  214. } else {
  215. /* Init failed, cleanup */
  216. skb_queue_purge(&ndev->cmd_q);
  217. skb_queue_purge(&ndev->rx_q);
  218. skb_queue_purge(&ndev->tx_q);
  219. ndev->ops->close(ndev);
  220. ndev->flags = 0;
  221. }
  222. done:
  223. mutex_unlock(&ndev->req_lock);
  224. return rc;
  225. }
  226. static int nci_close_device(struct nci_dev *ndev)
  227. {
  228. nci_req_cancel(ndev, ENODEV);
  229. mutex_lock(&ndev->req_lock);
  230. if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
  231. del_timer_sync(&ndev->cmd_timer);
  232. mutex_unlock(&ndev->req_lock);
  233. return 0;
  234. }
  235. /* Drop RX and TX queues */
  236. skb_queue_purge(&ndev->rx_q);
  237. skb_queue_purge(&ndev->tx_q);
  238. /* Flush RX and TX wq */
  239. flush_workqueue(ndev->rx_wq);
  240. flush_workqueue(ndev->tx_wq);
  241. /* Reset device */
  242. skb_queue_purge(&ndev->cmd_q);
  243. atomic_set(&ndev->cmd_cnt, 1);
  244. set_bit(NCI_INIT, &ndev->flags);
  245. __nci_request(ndev, nci_reset_req, 0,
  246. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  247. clear_bit(NCI_INIT, &ndev->flags);
  248. /* Flush cmd wq */
  249. flush_workqueue(ndev->cmd_wq);
  250. /* After this point our queues are empty
  251. * and no works are scheduled. */
  252. ndev->ops->close(ndev);
  253. /* Clear flags */
  254. ndev->flags = 0;
  255. mutex_unlock(&ndev->req_lock);
  256. return 0;
  257. }
  258. /* NCI command timer function */
  259. static void nci_cmd_timer(unsigned long arg)
  260. {
  261. struct nci_dev *ndev = (void *) arg;
  262. nfc_dbg("entry");
  263. atomic_set(&ndev->cmd_cnt, 1);
  264. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  265. }
  266. static int nci_dev_up(struct nfc_dev *nfc_dev)
  267. {
  268. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  269. nfc_dbg("entry");
  270. return nci_open_device(ndev);
  271. }
  272. static int nci_dev_down(struct nfc_dev *nfc_dev)
  273. {
  274. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  275. nfc_dbg("entry");
  276. return nci_close_device(ndev);
  277. }
  278. static int nci_start_poll(struct nfc_dev *nfc_dev, __u32 protocols)
  279. {
  280. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  281. int rc;
  282. nfc_dbg("entry");
  283. if (test_bit(NCI_DISCOVERY, &ndev->flags)) {
  284. nfc_err("unable to start poll, since poll is already active");
  285. return -EBUSY;
  286. }
  287. if (ndev->target_active_prot) {
  288. nfc_err("there is an active target");
  289. return -EBUSY;
  290. }
  291. if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
  292. nfc_dbg("target is active, implicitly deactivate...");
  293. rc = nci_request(ndev, nci_rf_deactivate_req, 0,
  294. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  295. if (rc)
  296. return -EBUSY;
  297. }
  298. rc = nci_request(ndev, nci_rf_discover_req, protocols,
  299. msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
  300. if (!rc)
  301. ndev->poll_prots = protocols;
  302. return rc;
  303. }
  304. static void nci_stop_poll(struct nfc_dev *nfc_dev)
  305. {
  306. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  307. nfc_dbg("entry");
  308. if (!test_bit(NCI_DISCOVERY, &ndev->flags)) {
  309. nfc_err("unable to stop poll, since poll is not active");
  310. return;
  311. }
  312. nci_request(ndev, nci_rf_deactivate_req, 0,
  313. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  314. }
  315. static int nci_activate_target(struct nfc_dev *nfc_dev, __u32 target_idx,
  316. __u32 protocol)
  317. {
  318. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  319. nfc_dbg("entry, target_idx %d, protocol 0x%x", target_idx, protocol);
  320. if (!test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
  321. nfc_err("there is no available target to activate");
  322. return -EINVAL;
  323. }
  324. if (ndev->target_active_prot) {
  325. nfc_err("there is already an active target");
  326. return -EBUSY;
  327. }
  328. if (!(ndev->target_available_prots & (1 << protocol))) {
  329. nfc_err("target does not support the requested protocol 0x%x",
  330. protocol);
  331. return -EINVAL;
  332. }
  333. ndev->target_active_prot = protocol;
  334. ndev->target_available_prots = 0;
  335. return 0;
  336. }
  337. static void nci_deactivate_target(struct nfc_dev *nfc_dev, __u32 target_idx)
  338. {
  339. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  340. nfc_dbg("entry, target_idx %d", target_idx);
  341. if (!ndev->target_active_prot) {
  342. nfc_err("unable to deactivate target, no active target");
  343. return;
  344. }
  345. ndev->target_active_prot = 0;
  346. if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
  347. nci_request(ndev, nci_rf_deactivate_req, 0,
  348. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  349. }
  350. }
  351. static int nci_data_exchange(struct nfc_dev *nfc_dev, __u32 target_idx,
  352. struct sk_buff *skb,
  353. data_exchange_cb_t cb,
  354. void *cb_context)
  355. {
  356. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  357. int rc;
  358. nfc_dbg("entry, target_idx %d, len %d", target_idx, skb->len);
  359. if (!ndev->target_active_prot) {
  360. nfc_err("unable to exchange data, no active target");
  361. return -EINVAL;
  362. }
  363. if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  364. return -EBUSY;
  365. /* store cb and context to be used on receiving data */
  366. ndev->data_exchange_cb = cb;
  367. ndev->data_exchange_cb_context = cb_context;
  368. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  369. if (rc)
  370. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  371. return rc;
  372. }
  373. static struct nfc_ops nci_nfc_ops = {
  374. .dev_up = nci_dev_up,
  375. .dev_down = nci_dev_down,
  376. .start_poll = nci_start_poll,
  377. .stop_poll = nci_stop_poll,
  378. .activate_target = nci_activate_target,
  379. .deactivate_target = nci_deactivate_target,
  380. .data_exchange = nci_data_exchange,
  381. };
  382. /* ---- Interface to NCI drivers ---- */
  383. /**
  384. * nci_allocate_device - allocate a new nci device
  385. *
  386. * @ops: device operations
  387. * @supported_protocols: NFC protocols supported by the device
  388. */
  389. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  390. __u32 supported_protocols,
  391. int tx_headroom,
  392. int tx_tailroom)
  393. {
  394. struct nci_dev *ndev;
  395. nfc_dbg("entry, supported_protocols 0x%x", supported_protocols);
  396. if (!ops->open || !ops->close || !ops->send)
  397. return NULL;
  398. if (!supported_protocols)
  399. return NULL;
  400. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  401. if (!ndev)
  402. return NULL;
  403. ndev->ops = ops;
  404. ndev->tx_headroom = tx_headroom;
  405. ndev->tx_tailroom = tx_tailroom;
  406. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  407. supported_protocols,
  408. tx_headroom + NCI_DATA_HDR_SIZE,
  409. tx_tailroom);
  410. if (!ndev->nfc_dev)
  411. goto free_exit;
  412. nfc_set_drvdata(ndev->nfc_dev, ndev);
  413. return ndev;
  414. free_exit:
  415. kfree(ndev);
  416. return NULL;
  417. }
  418. EXPORT_SYMBOL(nci_allocate_device);
  419. /**
  420. * nci_free_device - deallocate nci device
  421. *
  422. * @ndev: The nci device to deallocate
  423. */
  424. void nci_free_device(struct nci_dev *ndev)
  425. {
  426. nfc_dbg("entry");
  427. nfc_free_device(ndev->nfc_dev);
  428. kfree(ndev);
  429. }
  430. EXPORT_SYMBOL(nci_free_device);
  431. /**
  432. * nci_register_device - register a nci device in the nfc subsystem
  433. *
  434. * @dev: The nci device to register
  435. */
  436. int nci_register_device(struct nci_dev *ndev)
  437. {
  438. int rc;
  439. struct device *dev = &ndev->nfc_dev->dev;
  440. char name[32];
  441. nfc_dbg("entry");
  442. rc = nfc_register_device(ndev->nfc_dev);
  443. if (rc)
  444. goto exit;
  445. ndev->flags = 0;
  446. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  447. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  448. ndev->cmd_wq = create_singlethread_workqueue(name);
  449. if (!ndev->cmd_wq) {
  450. rc = -ENOMEM;
  451. goto unreg_exit;
  452. }
  453. INIT_WORK(&ndev->rx_work, nci_rx_work);
  454. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  455. ndev->rx_wq = create_singlethread_workqueue(name);
  456. if (!ndev->rx_wq) {
  457. rc = -ENOMEM;
  458. goto destroy_cmd_wq_exit;
  459. }
  460. INIT_WORK(&ndev->tx_work, nci_tx_work);
  461. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  462. ndev->tx_wq = create_singlethread_workqueue(name);
  463. if (!ndev->tx_wq) {
  464. rc = -ENOMEM;
  465. goto destroy_rx_wq_exit;
  466. }
  467. skb_queue_head_init(&ndev->cmd_q);
  468. skb_queue_head_init(&ndev->rx_q);
  469. skb_queue_head_init(&ndev->tx_q);
  470. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  471. (unsigned long) ndev);
  472. mutex_init(&ndev->req_lock);
  473. goto exit;
  474. destroy_rx_wq_exit:
  475. destroy_workqueue(ndev->rx_wq);
  476. destroy_cmd_wq_exit:
  477. destroy_workqueue(ndev->cmd_wq);
  478. unreg_exit:
  479. nfc_unregister_device(ndev->nfc_dev);
  480. exit:
  481. return rc;
  482. }
  483. EXPORT_SYMBOL(nci_register_device);
  484. /**
  485. * nci_unregister_device - unregister a nci device in the nfc subsystem
  486. *
  487. * @dev: The nci device to unregister
  488. */
  489. void nci_unregister_device(struct nci_dev *ndev)
  490. {
  491. nfc_dbg("entry");
  492. nci_close_device(ndev);
  493. destroy_workqueue(ndev->cmd_wq);
  494. destroy_workqueue(ndev->rx_wq);
  495. destroy_workqueue(ndev->tx_wq);
  496. nfc_unregister_device(ndev->nfc_dev);
  497. }
  498. EXPORT_SYMBOL(nci_unregister_device);
  499. /**
  500. * nci_recv_frame - receive frame from NCI drivers
  501. *
  502. * @skb: The sk_buff to receive
  503. */
  504. int nci_recv_frame(struct sk_buff *skb)
  505. {
  506. struct nci_dev *ndev = (struct nci_dev *) skb->dev;
  507. nfc_dbg("entry, len %d", skb->len);
  508. if (!ndev || (!test_bit(NCI_UP, &ndev->flags)
  509. && !test_bit(NCI_INIT, &ndev->flags))) {
  510. kfree_skb(skb);
  511. return -ENXIO;
  512. }
  513. /* Queue frame for rx worker thread */
  514. skb_queue_tail(&ndev->rx_q, skb);
  515. queue_work(ndev->rx_wq, &ndev->rx_work);
  516. return 0;
  517. }
  518. EXPORT_SYMBOL(nci_recv_frame);
  519. static int nci_send_frame(struct sk_buff *skb)
  520. {
  521. struct nci_dev *ndev = (struct nci_dev *) skb->dev;
  522. nfc_dbg("entry, len %d", skb->len);
  523. if (!ndev) {
  524. kfree_skb(skb);
  525. return -ENODEV;
  526. }
  527. /* Get rid of skb owner, prior to sending to the driver. */
  528. skb_orphan(skb);
  529. return ndev->ops->send(skb);
  530. }
  531. /* Send NCI command */
  532. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  533. {
  534. struct nci_ctrl_hdr *hdr;
  535. struct sk_buff *skb;
  536. nfc_dbg("entry, opcode 0x%x, plen %d", opcode, plen);
  537. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  538. if (!skb) {
  539. nfc_err("no memory for command");
  540. return -ENOMEM;
  541. }
  542. hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
  543. hdr->gid = nci_opcode_gid(opcode);
  544. hdr->oid = nci_opcode_oid(opcode);
  545. hdr->plen = plen;
  546. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  547. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  548. if (plen)
  549. memcpy(skb_put(skb, plen), payload, plen);
  550. skb->dev = (void *) ndev;
  551. skb_queue_tail(&ndev->cmd_q, skb);
  552. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  553. return 0;
  554. }
  555. /* ---- NCI TX Data worker thread ---- */
  556. static void nci_tx_work(struct work_struct *work)
  557. {
  558. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  559. struct sk_buff *skb;
  560. nfc_dbg("entry, credits_cnt %d", atomic_read(&ndev->credits_cnt));
  561. /* Send queued tx data */
  562. while (atomic_read(&ndev->credits_cnt)) {
  563. skb = skb_dequeue(&ndev->tx_q);
  564. if (!skb)
  565. return;
  566. /* Check if data flow control is used */
  567. if (atomic_read(&ndev->credits_cnt) !=
  568. NCI_DATA_FLOW_CONTROL_NOT_USED)
  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. }