core.c 18 KB

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