nfcsim.c 11 KB

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