nfcsim.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * NFC hardware simulation driver
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <linux/device.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/nfc.h>
  19. #include <net/nfc/nfc.h>
  20. #define DEV_ERR(_dev, fmt, args...) nfc_err(&_dev->nfc_dev->dev, \
  21. "%s: " fmt, __func__, ## args)
  22. #define DEV_DBG(_dev, fmt, args...) dev_dbg(&_dev->nfc_dev->dev, \
  23. "%s: " fmt, __func__, ## args)
  24. #define NFCSIM_VERSION "0.1"
  25. #define NFCSIM_POLL_NONE 0
  26. #define NFCSIM_POLL_INITIATOR 1
  27. #define NFCSIM_POLL_TARGET 2
  28. #define NFCSIM_POLL_DUAL (NFCSIM_POLL_INITIATOR | NFCSIM_POLL_TARGET)
  29. struct nfcsim {
  30. struct nfc_dev *nfc_dev;
  31. struct mutex lock;
  32. struct delayed_work recv_work;
  33. struct sk_buff *clone_skb;
  34. struct delayed_work poll_work;
  35. u8 polling_mode;
  36. u8 curr_polling_mode;
  37. u8 shutting_down;
  38. u8 up;
  39. u8 initiator;
  40. data_exchange_cb_t cb;
  41. void *cb_context;
  42. struct nfcsim *peer_dev;
  43. };
  44. static struct nfcsim *dev0;
  45. static struct nfcsim *dev1;
  46. static struct workqueue_struct *wq;
  47. static void nfcsim_cleanup_dev(struct nfcsim *dev, u8 shutdown)
  48. {
  49. DEV_DBG(dev, "shutdown=%d\n", shutdown);
  50. mutex_lock(&dev->lock);
  51. dev->polling_mode = NFCSIM_POLL_NONE;
  52. dev->shutting_down = shutdown;
  53. dev->cb = NULL;
  54. dev_kfree_skb(dev->clone_skb);
  55. dev->clone_skb = NULL;
  56. mutex_unlock(&dev->lock);
  57. cancel_delayed_work_sync(&dev->poll_work);
  58. cancel_delayed_work_sync(&dev->recv_work);
  59. }
  60. static int nfcsim_target_found(struct nfcsim *dev)
  61. {
  62. struct nfc_target nfc_tgt;
  63. DEV_DBG(dev, "\n");
  64. memset(&nfc_tgt, 0, sizeof(struct nfc_target));
  65. nfc_tgt.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  66. nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
  67. return 0;
  68. }
  69. static int nfcsim_dev_up(struct nfc_dev *nfc_dev)
  70. {
  71. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  72. DEV_DBG(dev, "\n");
  73. mutex_lock(&dev->lock);
  74. dev->up = 1;
  75. mutex_unlock(&dev->lock);
  76. return 0;
  77. }
  78. static int nfcsim_dev_down(struct nfc_dev *nfc_dev)
  79. {
  80. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  81. DEV_DBG(dev, "\n");
  82. mutex_lock(&dev->lock);
  83. dev->up = 0;
  84. mutex_unlock(&dev->lock);
  85. return 0;
  86. }
  87. static int nfcsim_dep_link_up(struct nfc_dev *nfc_dev,
  88. struct nfc_target *target,
  89. u8 comm_mode, u8 *gb, size_t gb_len)
  90. {
  91. int rc;
  92. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  93. struct nfcsim *peer = dev->peer_dev;
  94. u8 *remote_gb;
  95. size_t remote_gb_len;
  96. DEV_DBG(dev, "target_idx: %d, comm_mode: %d\n", target->idx, comm_mode);
  97. mutex_lock(&peer->lock);
  98. nfc_tm_activated(peer->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
  99. NFC_COMM_ACTIVE, gb, gb_len);
  100. remote_gb = nfc_get_local_general_bytes(peer->nfc_dev, &remote_gb_len);
  101. if (!remote_gb) {
  102. DEV_ERR(peer, "Can't get remote general bytes\n");
  103. mutex_unlock(&peer->lock);
  104. return -EINVAL;
  105. }
  106. mutex_unlock(&peer->lock);
  107. mutex_lock(&dev->lock);
  108. rc = nfc_set_remote_general_bytes(nfc_dev, remote_gb, remote_gb_len);
  109. if (rc) {
  110. DEV_ERR(dev, "Can't set remote general bytes\n");
  111. mutex_unlock(&dev->lock);
  112. return rc;
  113. }
  114. rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_ACTIVE,
  115. NFC_RF_INITIATOR);
  116. mutex_unlock(&dev->lock);
  117. return rc;
  118. }
  119. static int nfcsim_dep_link_down(struct nfc_dev *nfc_dev)
  120. {
  121. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  122. DEV_DBG(dev, "\n");
  123. nfcsim_cleanup_dev(dev, 0);
  124. return 0;
  125. }
  126. static int nfcsim_start_poll(struct nfc_dev *nfc_dev,
  127. u32 im_protocols, u32 tm_protocols)
  128. {
  129. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  130. int rc;
  131. mutex_lock(&dev->lock);
  132. if (dev->polling_mode != NFCSIM_POLL_NONE) {
  133. DEV_ERR(dev, "Already in polling mode\n");
  134. rc = -EBUSY;
  135. goto exit;
  136. }
  137. if (im_protocols & NFC_PROTO_NFC_DEP_MASK)
  138. dev->polling_mode |= NFCSIM_POLL_INITIATOR;
  139. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK)
  140. dev->polling_mode |= NFCSIM_POLL_TARGET;
  141. if (dev->polling_mode == NFCSIM_POLL_NONE) {
  142. DEV_ERR(dev, "Unsupported polling mode\n");
  143. rc = -EINVAL;
  144. goto exit;
  145. }
  146. dev->initiator = 0;
  147. dev->curr_polling_mode = NFCSIM_POLL_NONE;
  148. queue_delayed_work(wq, &dev->poll_work, 0);
  149. DEV_DBG(dev, "Start polling: im: 0x%X, tm: 0x%X\n", im_protocols,
  150. tm_protocols);
  151. rc = 0;
  152. exit:
  153. mutex_unlock(&dev->lock);
  154. return rc;
  155. }
  156. static void nfcsim_stop_poll(struct nfc_dev *nfc_dev)
  157. {
  158. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  159. DEV_DBG(dev, "Stop poll\n");
  160. mutex_lock(&dev->lock);
  161. dev->polling_mode = NFCSIM_POLL_NONE;
  162. mutex_unlock(&dev->lock);
  163. cancel_delayed_work_sync(&dev->poll_work);
  164. }
  165. static int nfcsim_activate_target(struct nfc_dev *nfc_dev,
  166. struct nfc_target *target, u32 protocol)
  167. {
  168. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  169. DEV_DBG(dev, "\n");
  170. return -ENOTSUPP;
  171. }
  172. static void nfcsim_deactivate_target(struct nfc_dev *nfc_dev,
  173. struct nfc_target *target, u8 mode)
  174. {
  175. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  176. DEV_DBG(dev, "\n");
  177. }
  178. static void nfcsim_wq_recv(struct work_struct *work)
  179. {
  180. struct nfcsim *dev = container_of(work, struct nfcsim,
  181. recv_work.work);
  182. mutex_lock(&dev->lock);
  183. if (dev->shutting_down || !dev->up || !dev->clone_skb) {
  184. dev_kfree_skb(dev->clone_skb);
  185. goto exit;
  186. }
  187. if (dev->initiator) {
  188. if (!dev->cb) {
  189. DEV_ERR(dev, "Null recv callback\n");
  190. dev_kfree_skb(dev->clone_skb);
  191. goto exit;
  192. }
  193. dev->cb(dev->cb_context, dev->clone_skb, 0);
  194. dev->cb = NULL;
  195. } else {
  196. nfc_tm_data_received(dev->nfc_dev, dev->clone_skb);
  197. }
  198. exit:
  199. dev->clone_skb = NULL;
  200. mutex_unlock(&dev->lock);
  201. }
  202. static int nfcsim_tx(struct nfc_dev *nfc_dev, struct nfc_target *target,
  203. struct sk_buff *skb, data_exchange_cb_t cb,
  204. void *cb_context)
  205. {
  206. struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
  207. struct nfcsim *peer = dev->peer_dev;
  208. int err;
  209. mutex_lock(&dev->lock);
  210. if (dev->shutting_down || !dev->up) {
  211. mutex_unlock(&dev->lock);
  212. err = -ENODEV;
  213. goto exit;
  214. }
  215. dev->cb = cb;
  216. dev->cb_context = cb_context;
  217. mutex_unlock(&dev->lock);
  218. mutex_lock(&peer->lock);
  219. peer->clone_skb = skb_clone(skb, GFP_KERNEL);
  220. if (!peer->clone_skb) {
  221. DEV_ERR(dev, "skb_clone failed\n");
  222. mutex_unlock(&peer->lock);
  223. err = -ENOMEM;
  224. goto exit;
  225. }
  226. /* This simulates an arbitrary transmission delay between the 2 devices.
  227. * If packet transmission occurs immediately between them, we have a
  228. * non-stop flow of several tens of thousands SYMM packets per second
  229. * and a burning cpu.
  230. *
  231. * TODO: Add support for a sysfs entry to control this delay.
  232. */
  233. queue_delayed_work(wq, &peer->recv_work, msecs_to_jiffies(5));
  234. mutex_unlock(&peer->lock);
  235. err = 0;
  236. exit:
  237. dev_kfree_skb(skb);
  238. return err;
  239. }
  240. static int nfcsim_im_transceive(struct nfc_dev *nfc_dev,
  241. struct nfc_target *target, struct sk_buff *skb,
  242. data_exchange_cb_t cb, void *cb_context)
  243. {
  244. return nfcsim_tx(nfc_dev, target, skb, cb, cb_context);
  245. }
  246. static int nfcsim_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  247. {
  248. return nfcsim_tx(nfc_dev, NULL, skb, NULL, NULL);
  249. }
  250. static struct nfc_ops nfcsim_nfc_ops = {
  251. .dev_up = nfcsim_dev_up,
  252. .dev_down = nfcsim_dev_down,
  253. .dep_link_up = nfcsim_dep_link_up,
  254. .dep_link_down = nfcsim_dep_link_down,
  255. .start_poll = nfcsim_start_poll,
  256. .stop_poll = nfcsim_stop_poll,
  257. .activate_target = nfcsim_activate_target,
  258. .deactivate_target = nfcsim_deactivate_target,
  259. .im_transceive = nfcsim_im_transceive,
  260. .tm_send = nfcsim_tm_send,
  261. };
  262. static void nfcsim_set_polling_mode(struct nfcsim *dev)
  263. {
  264. if (dev->polling_mode == NFCSIM_POLL_NONE) {
  265. dev->curr_polling_mode = NFCSIM_POLL_NONE;
  266. return;
  267. }
  268. if (dev->curr_polling_mode == NFCSIM_POLL_NONE) {
  269. if (dev->polling_mode & NFCSIM_POLL_INITIATOR)
  270. dev->curr_polling_mode = NFCSIM_POLL_INITIATOR;
  271. else
  272. dev->curr_polling_mode = NFCSIM_POLL_TARGET;
  273. return;
  274. }
  275. if (dev->polling_mode == NFCSIM_POLL_DUAL) {
  276. if (dev->curr_polling_mode == NFCSIM_POLL_TARGET)
  277. dev->curr_polling_mode = NFCSIM_POLL_INITIATOR;
  278. else
  279. dev->curr_polling_mode = NFCSIM_POLL_TARGET;
  280. }
  281. }
  282. static void nfcsim_wq_poll(struct work_struct *work)
  283. {
  284. struct nfcsim *dev = container_of(work, struct nfcsim, poll_work.work);
  285. struct nfcsim *peer = dev->peer_dev;
  286. /* These work items run on an ordered workqueue and are therefore
  287. * serialized. So we can take both mutexes without being dead locked.
  288. */
  289. mutex_lock(&dev->lock);
  290. mutex_lock(&peer->lock);
  291. nfcsim_set_polling_mode(dev);
  292. if (dev->curr_polling_mode == NFCSIM_POLL_NONE) {
  293. DEV_DBG(dev, "Not polling\n");
  294. goto unlock;
  295. }
  296. DEV_DBG(dev, "Polling as %s",
  297. dev->curr_polling_mode == NFCSIM_POLL_INITIATOR ?
  298. "initiator\n" : "target\n");
  299. if (dev->curr_polling_mode == NFCSIM_POLL_TARGET)
  300. goto sched_work;
  301. if (peer->curr_polling_mode == NFCSIM_POLL_TARGET) {
  302. peer->polling_mode = NFCSIM_POLL_NONE;
  303. dev->polling_mode = NFCSIM_POLL_NONE;
  304. dev->initiator = 1;
  305. nfcsim_target_found(dev);
  306. goto unlock;
  307. }
  308. sched_work:
  309. /* This defines the delay for an initiator to check if the other device
  310. * is polling in target mode.
  311. * If the device starts in dual mode polling, it switches between
  312. * initiator and target at every round.
  313. * Because the wq is ordered and only 1 work item is executed at a time,
  314. * we'll always have one device polling as initiator and the other as
  315. * target at some point, even if both are started in dual mode.
  316. */
  317. queue_delayed_work(wq, &dev->poll_work, msecs_to_jiffies(200));
  318. unlock:
  319. mutex_unlock(&peer->lock);
  320. mutex_unlock(&dev->lock);
  321. }
  322. static struct nfcsim *nfcsim_init_dev(void)
  323. {
  324. struct nfcsim *dev;
  325. int rc = -ENOMEM;
  326. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  327. if (dev == NULL)
  328. return ERR_PTR(-ENOMEM);
  329. mutex_init(&dev->lock);
  330. INIT_DELAYED_WORK(&dev->recv_work, nfcsim_wq_recv);
  331. INIT_DELAYED_WORK(&dev->poll_work, nfcsim_wq_poll);
  332. dev->nfc_dev = nfc_allocate_device(&nfcsim_nfc_ops,
  333. NFC_PROTO_NFC_DEP_MASK,
  334. 0, 0);
  335. if (!dev->nfc_dev)
  336. goto error;
  337. nfc_set_drvdata(dev->nfc_dev, dev);
  338. rc = nfc_register_device(dev->nfc_dev);
  339. if (rc)
  340. goto free_nfc_dev;
  341. return dev;
  342. free_nfc_dev:
  343. nfc_free_device(dev->nfc_dev);
  344. error:
  345. kfree(dev);
  346. return ERR_PTR(rc);
  347. }
  348. static void nfcsim_free_device(struct nfcsim *dev)
  349. {
  350. nfc_unregister_device(dev->nfc_dev);
  351. nfc_free_device(dev->nfc_dev);
  352. kfree(dev);
  353. }
  354. static int __init nfcsim_init(void)
  355. {
  356. int rc;
  357. /* We need an ordered wq to ensure that poll_work items are executed
  358. * one at a time.
  359. */
  360. wq = alloc_ordered_workqueue("nfcsim", 0);
  361. if (!wq) {
  362. rc = -ENOMEM;
  363. goto exit;
  364. }
  365. dev0 = nfcsim_init_dev();
  366. if (IS_ERR(dev0)) {
  367. rc = PTR_ERR(dev0);
  368. goto exit;
  369. }
  370. dev1 = nfcsim_init_dev();
  371. if (IS_ERR(dev1)) {
  372. kfree(dev0);
  373. rc = PTR_ERR(dev1);
  374. goto exit;
  375. }
  376. dev0->peer_dev = dev1;
  377. dev1->peer_dev = dev0;
  378. pr_debug("NFCsim " NFCSIM_VERSION " initialized\n");
  379. rc = 0;
  380. exit:
  381. if (rc)
  382. pr_err("Failed to initialize nfcsim driver (%d)\n",
  383. rc);
  384. return rc;
  385. }
  386. static void __exit nfcsim_exit(void)
  387. {
  388. nfcsim_cleanup_dev(dev0, 1);
  389. nfcsim_cleanup_dev(dev1, 1);
  390. nfcsim_free_device(dev0);
  391. nfcsim_free_device(dev1);
  392. destroy_workqueue(wq);
  393. }
  394. module_init(nfcsim_init);
  395. module_exit(nfcsim_exit);
  396. MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION);
  397. MODULE_VERSION(NFCSIM_VERSION);
  398. MODULE_LICENSE("GPL");