core.c 21 KB

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