core.c 21 KB

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