core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. 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_rf_disc_map_cmd cmd;
  116. struct nci_core_conn_create_cmd conn_cmd;
  117. int i;
  118. /* create static rf connection */
  119. conn_cmd.target_handle = 0;
  120. conn_cmd.num_target_specific_params = 0;
  121. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, 2, &conn_cmd);
  122. /* set rf mapping configurations */
  123. cmd.num_mapping_configs = 0;
  124. /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
  125. for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
  126. if (ndev->supported_rf_interfaces[i] ==
  127. NCI_RF_INTERFACE_ISO_DEP) {
  128. cmd.mapping_configs[cmd.num_mapping_configs]
  129. .rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  130. cmd.mapping_configs[cmd.num_mapping_configs]
  131. .mode = NCI_DISC_MAP_MODE_BOTH;
  132. cmd.mapping_configs[cmd.num_mapping_configs]
  133. .rf_interface_type = NCI_RF_INTERFACE_ISO_DEP;
  134. cmd.num_mapping_configs++;
  135. } else if (ndev->supported_rf_interfaces[i] ==
  136. NCI_RF_INTERFACE_NFC_DEP) {
  137. cmd.mapping_configs[cmd.num_mapping_configs]
  138. .rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  139. cmd.mapping_configs[cmd.num_mapping_configs]
  140. .mode = NCI_DISC_MAP_MODE_BOTH;
  141. cmd.mapping_configs[cmd.num_mapping_configs]
  142. .rf_interface_type = NCI_RF_INTERFACE_NFC_DEP;
  143. cmd.num_mapping_configs++;
  144. }
  145. if (cmd.num_mapping_configs == NCI_MAX_NUM_MAPPING_CONFIGS)
  146. break;
  147. }
  148. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
  149. (1 + (cmd.num_mapping_configs*sizeof(struct disc_map_config))),
  150. &cmd);
  151. }
  152. static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
  153. {
  154. struct nci_rf_disc_cmd cmd;
  155. __u32 protocols = opt;
  156. cmd.num_disc_configs = 0;
  157. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  158. (protocols & NFC_PROTO_JEWEL_MASK
  159. || protocols & NFC_PROTO_MIFARE_MASK
  160. || protocols & NFC_PROTO_ISO14443_MASK
  161. || protocols & NFC_PROTO_NFC_DEP_MASK)) {
  162. cmd.disc_configs[cmd.num_disc_configs].type =
  163. NCI_DISCOVERY_TYPE_POLL_A_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_ISO14443_MASK)) {
  169. cmd.disc_configs[cmd.num_disc_configs].type =
  170. NCI_DISCOVERY_TYPE_POLL_B_PASSIVE;
  171. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  172. cmd.num_disc_configs++;
  173. }
  174. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  175. (protocols & NFC_PROTO_FELICA_MASK
  176. || protocols & NFC_PROTO_NFC_DEP_MASK)) {
  177. cmd.disc_configs[cmd.num_disc_configs].type =
  178. NCI_DISCOVERY_TYPE_POLL_F_PASSIVE;
  179. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  180. cmd.num_disc_configs++;
  181. }
  182. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
  183. (1 + (cmd.num_disc_configs*sizeof(struct disc_config))),
  184. &cmd);
  185. }
  186. static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
  187. {
  188. struct nci_rf_deactivate_cmd cmd;
  189. cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
  190. nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
  191. sizeof(struct nci_rf_deactivate_cmd),
  192. &cmd);
  193. }
  194. static int nci_open_device(struct nci_dev *ndev)
  195. {
  196. int rc = 0;
  197. mutex_lock(&ndev->req_lock);
  198. if (test_bit(NCI_UP, &ndev->flags)) {
  199. rc = -EALREADY;
  200. goto done;
  201. }
  202. if (ndev->ops->open(ndev)) {
  203. rc = -EIO;
  204. goto done;
  205. }
  206. atomic_set(&ndev->cmd_cnt, 1);
  207. set_bit(NCI_INIT, &ndev->flags);
  208. rc = __nci_request(ndev, nci_reset_req, 0,
  209. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  210. if (!rc) {
  211. rc = __nci_request(ndev, nci_init_req, 0,
  212. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  213. }
  214. if (!rc) {
  215. rc = __nci_request(ndev, nci_init_complete_req, 0,
  216. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  217. }
  218. clear_bit(NCI_INIT, &ndev->flags);
  219. if (!rc) {
  220. set_bit(NCI_UP, &ndev->flags);
  221. } else {
  222. /* Init failed, cleanup */
  223. skb_queue_purge(&ndev->cmd_q);
  224. skb_queue_purge(&ndev->rx_q);
  225. skb_queue_purge(&ndev->tx_q);
  226. ndev->ops->close(ndev);
  227. ndev->flags = 0;
  228. }
  229. done:
  230. mutex_unlock(&ndev->req_lock);
  231. return rc;
  232. }
  233. static int nci_close_device(struct nci_dev *ndev)
  234. {
  235. nci_req_cancel(ndev, ENODEV);
  236. mutex_lock(&ndev->req_lock);
  237. if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
  238. del_timer_sync(&ndev->cmd_timer);
  239. mutex_unlock(&ndev->req_lock);
  240. return 0;
  241. }
  242. /* Drop RX and TX queues */
  243. skb_queue_purge(&ndev->rx_q);
  244. skb_queue_purge(&ndev->tx_q);
  245. /* Flush RX and TX wq */
  246. flush_workqueue(ndev->rx_wq);
  247. flush_workqueue(ndev->tx_wq);
  248. /* Reset device */
  249. skb_queue_purge(&ndev->cmd_q);
  250. atomic_set(&ndev->cmd_cnt, 1);
  251. set_bit(NCI_INIT, &ndev->flags);
  252. __nci_request(ndev, nci_reset_req, 0,
  253. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  254. clear_bit(NCI_INIT, &ndev->flags);
  255. /* Flush cmd wq */
  256. flush_workqueue(ndev->cmd_wq);
  257. /* After this point our queues are empty
  258. * and no works are scheduled. */
  259. ndev->ops->close(ndev);
  260. /* Clear flags */
  261. ndev->flags = 0;
  262. mutex_unlock(&ndev->req_lock);
  263. return 0;
  264. }
  265. /* NCI command timer function */
  266. static void nci_cmd_timer(unsigned long arg)
  267. {
  268. struct nci_dev *ndev = (void *) arg;
  269. nfc_dbg("entry");
  270. atomic_set(&ndev->cmd_cnt, 1);
  271. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  272. }
  273. static int nci_dev_up(struct nfc_dev *nfc_dev)
  274. {
  275. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  276. nfc_dbg("entry");
  277. return nci_open_device(ndev);
  278. }
  279. static int nci_dev_down(struct nfc_dev *nfc_dev)
  280. {
  281. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  282. nfc_dbg("entry");
  283. return nci_close_device(ndev);
  284. }
  285. static int nci_start_poll(struct nfc_dev *nfc_dev, __u32 protocols)
  286. {
  287. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  288. int rc;
  289. nfc_dbg("entry");
  290. if (test_bit(NCI_DISCOVERY, &ndev->flags)) {
  291. nfc_err("unable to start poll, since poll is already active");
  292. return -EBUSY;
  293. }
  294. if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
  295. nfc_dbg("target already active, first 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. nfc_dbg("entry, target_idx %d, len %d", target_idx, skb->len);
  361. if (!ndev->target_active_prot) {
  362. nfc_err("unable to exchange data, no active target");
  363. return -EINVAL;
  364. }
  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. return nci_send_data(ndev, ndev->conn_id, skb);
  369. }
  370. static struct nfc_ops nci_nfc_ops = {
  371. .dev_up = nci_dev_up,
  372. .dev_down = nci_dev_down,
  373. .start_poll = nci_start_poll,
  374. .stop_poll = nci_stop_poll,
  375. .activate_target = nci_activate_target,
  376. .deactivate_target = nci_deactivate_target,
  377. .data_exchange = nci_data_exchange,
  378. };
  379. /* ---- Interface to NCI drivers ---- */
  380. /**
  381. * nci_allocate_device - allocate a new nci device
  382. *
  383. * @ops: device operations
  384. * @supported_protocols: NFC protocols supported by the device
  385. */
  386. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  387. __u32 supported_protocols,
  388. int tx_headroom,
  389. int tx_tailroom)
  390. {
  391. struct nci_dev *ndev = NULL;
  392. nfc_dbg("entry, supported_protocols 0x%x", supported_protocols);
  393. if (!ops->open || !ops->close || !ops->send)
  394. goto exit;
  395. if (!supported_protocols)
  396. goto exit;
  397. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  398. if (!ndev)
  399. goto exit;
  400. ndev->ops = ops;
  401. ndev->tx_headroom = tx_headroom;
  402. ndev->tx_tailroom = tx_tailroom;
  403. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  404. supported_protocols,
  405. tx_headroom + NCI_DATA_HDR_SIZE,
  406. tx_tailroom);
  407. if (!ndev->nfc_dev)
  408. goto free_exit;
  409. nfc_set_drvdata(ndev->nfc_dev, ndev);
  410. goto exit;
  411. free_exit:
  412. kfree(ndev);
  413. exit:
  414. return ndev;
  415. }
  416. EXPORT_SYMBOL(nci_allocate_device);
  417. /**
  418. * nci_free_device - deallocate nci device
  419. *
  420. * @ndev: The nci device to deallocate
  421. */
  422. void nci_free_device(struct nci_dev *ndev)
  423. {
  424. nfc_dbg("entry");
  425. nfc_free_device(ndev->nfc_dev);
  426. kfree(ndev);
  427. }
  428. EXPORT_SYMBOL(nci_free_device);
  429. /**
  430. * nci_register_device - register a nci device in the nfc subsystem
  431. *
  432. * @dev: The nci device to register
  433. */
  434. int nci_register_device(struct nci_dev *ndev)
  435. {
  436. int rc;
  437. struct device *dev = &ndev->nfc_dev->dev;
  438. char name[32];
  439. nfc_dbg("entry");
  440. rc = nfc_register_device(ndev->nfc_dev);
  441. if (rc)
  442. goto exit;
  443. ndev->flags = 0;
  444. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  445. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  446. ndev->cmd_wq = create_singlethread_workqueue(name);
  447. if (!ndev->cmd_wq) {
  448. rc = -ENOMEM;
  449. goto unreg_exit;
  450. }
  451. INIT_WORK(&ndev->rx_work, nci_rx_work);
  452. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  453. ndev->rx_wq = create_singlethread_workqueue(name);
  454. if (!ndev->rx_wq) {
  455. rc = -ENOMEM;
  456. goto destroy_cmd_wq_exit;
  457. }
  458. INIT_WORK(&ndev->tx_work, nci_tx_work);
  459. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  460. ndev->tx_wq = create_singlethread_workqueue(name);
  461. if (!ndev->tx_wq) {
  462. rc = -ENOMEM;
  463. goto destroy_rx_wq_exit;
  464. }
  465. skb_queue_head_init(&ndev->cmd_q);
  466. skb_queue_head_init(&ndev->rx_q);
  467. skb_queue_head_init(&ndev->tx_q);
  468. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  469. (unsigned long) ndev);
  470. mutex_init(&ndev->req_lock);
  471. goto exit;
  472. destroy_rx_wq_exit:
  473. destroy_workqueue(ndev->rx_wq);
  474. destroy_cmd_wq_exit:
  475. destroy_workqueue(ndev->cmd_wq);
  476. unreg_exit:
  477. nfc_unregister_device(ndev->nfc_dev);
  478. exit:
  479. return rc;
  480. }
  481. EXPORT_SYMBOL(nci_register_device);
  482. /**
  483. * nci_unregister_device - unregister a nci device in the nfc subsystem
  484. *
  485. * @dev: The nci device to unregister
  486. */
  487. void nci_unregister_device(struct nci_dev *ndev)
  488. {
  489. nfc_dbg("entry");
  490. nci_close_device(ndev);
  491. destroy_workqueue(ndev->cmd_wq);
  492. destroy_workqueue(ndev->rx_wq);
  493. destroy_workqueue(ndev->tx_wq);
  494. nfc_unregister_device(ndev->nfc_dev);
  495. }
  496. EXPORT_SYMBOL(nci_unregister_device);
  497. /**
  498. * nci_recv_frame - receive frame from NCI drivers
  499. *
  500. * @skb: The sk_buff to receive
  501. */
  502. int nci_recv_frame(struct sk_buff *skb)
  503. {
  504. struct nci_dev *ndev = (struct nci_dev *) skb->dev;
  505. nfc_dbg("entry, len %d", skb->len);
  506. if (!ndev || (!test_bit(NCI_UP, &ndev->flags)
  507. && !test_bit(NCI_INIT, &ndev->flags))) {
  508. kfree_skb(skb);
  509. return -ENXIO;
  510. }
  511. /* Queue frame for rx worker thread */
  512. skb_queue_tail(&ndev->rx_q, skb);
  513. queue_work(ndev->rx_wq, &ndev->rx_work);
  514. return 0;
  515. }
  516. EXPORT_SYMBOL(nci_recv_frame);
  517. static int nci_send_frame(struct sk_buff *skb)
  518. {
  519. struct nci_dev *ndev = (struct nci_dev *) skb->dev;
  520. nfc_dbg("entry, len %d", skb->len);
  521. if (!ndev) {
  522. kfree_skb(skb);
  523. return -ENODEV;
  524. }
  525. /* Get rid of skb owner, prior to sending to the driver. */
  526. skb_orphan(skb);
  527. return ndev->ops->send(skb);
  528. }
  529. /* Send NCI command */
  530. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  531. {
  532. struct nci_ctrl_hdr *hdr;
  533. struct sk_buff *skb;
  534. nfc_dbg("entry, opcode 0x%x, plen %d", opcode, plen);
  535. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  536. if (!skb) {
  537. nfc_err("no memory for command");
  538. return -ENOMEM;
  539. }
  540. hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
  541. hdr->gid = nci_opcode_gid(opcode);
  542. hdr->oid = nci_opcode_oid(opcode);
  543. hdr->plen = plen;
  544. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  545. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  546. if (plen)
  547. memcpy(skb_put(skb, plen), payload, plen);
  548. skb->dev = (void *) ndev;
  549. skb_queue_tail(&ndev->cmd_q, skb);
  550. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  551. return 0;
  552. }
  553. /* ---- NCI TX Data worker thread ---- */
  554. static void nci_tx_work(struct work_struct *work)
  555. {
  556. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  557. struct sk_buff *skb;
  558. nfc_dbg("entry, credits_cnt %d", atomic_read(&ndev->credits_cnt));
  559. /* Send queued tx data */
  560. while (atomic_read(&ndev->credits_cnt)) {
  561. skb = skb_dequeue(&ndev->tx_q);
  562. if (!skb)
  563. return;
  564. atomic_dec(&ndev->credits_cnt);
  565. nfc_dbg("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d",
  566. nci_pbf(skb->data),
  567. nci_conn_id(skb->data),
  568. nci_plen(skb->data));
  569. nci_send_frame(skb);
  570. }
  571. }
  572. /* ----- NCI RX worker thread (data & control) ----- */
  573. static void nci_rx_work(struct work_struct *work)
  574. {
  575. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  576. struct sk_buff *skb;
  577. while ((skb = skb_dequeue(&ndev->rx_q))) {
  578. /* Process frame */
  579. switch (nci_mt(skb->data)) {
  580. case NCI_MT_RSP_PKT:
  581. nci_rsp_packet(ndev, skb);
  582. break;
  583. case NCI_MT_NTF_PKT:
  584. nci_ntf_packet(ndev, skb);
  585. break;
  586. case NCI_MT_DATA_PKT:
  587. nci_rx_data_packet(ndev, skb);
  588. break;
  589. default:
  590. nfc_err("unknown MT 0x%x", nci_mt(skb->data));
  591. kfree_skb(skb);
  592. break;
  593. }
  594. }
  595. }
  596. /* ----- NCI TX CMD worker thread ----- */
  597. static void nci_cmd_work(struct work_struct *work)
  598. {
  599. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  600. struct sk_buff *skb;
  601. nfc_dbg("entry, cmd_cnt %d", atomic_read(&ndev->cmd_cnt));
  602. /* Send queued command */
  603. if (atomic_read(&ndev->cmd_cnt)) {
  604. skb = skb_dequeue(&ndev->cmd_q);
  605. if (!skb)
  606. return;
  607. atomic_dec(&ndev->cmd_cnt);
  608. nfc_dbg("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
  609. nci_pbf(skb->data),
  610. nci_opcode_gid(nci_opcode(skb->data)),
  611. nci_opcode_oid(nci_opcode(skb->data)),
  612. nci_plen(skb->data));
  613. nci_send_frame(skb);
  614. mod_timer(&ndev->cmd_timer,
  615. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  616. }
  617. }