core.c 23 KB

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