virtual_ncidev.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtual NCI device simulation driver
  4. *
  5. * Copyright (C) 2020 Samsung Electronics
  6. * Bongsu Jeon <bongsu.jeon@samsung.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/mutex.h>
  12. #include <linux/wait.h>
  13. #include <net/nfc/nci_core.h>
  14. #define IOCTL_GET_NCIDEV_IDX 0
  15. #define VIRTUAL_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
  16. NFC_PROTO_MIFARE_MASK | \
  17. NFC_PROTO_FELICA_MASK | \
  18. NFC_PROTO_ISO14443_MASK | \
  19. NFC_PROTO_ISO14443_B_MASK | \
  20. NFC_PROTO_ISO15693_MASK)
  21. struct virtual_nci_dev {
  22. struct nci_dev *ndev;
  23. struct mutex mtx;
  24. struct sk_buff *send_buff;
  25. struct wait_queue_head wq;
  26. bool running;
  27. };
  28. static int virtual_nci_open(struct nci_dev *ndev)
  29. {
  30. struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
  31. vdev->running = true;
  32. return 0;
  33. }
  34. static int virtual_nci_close(struct nci_dev *ndev)
  35. {
  36. struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
  37. mutex_lock(&vdev->mtx);
  38. kfree_skb(vdev->send_buff);
  39. vdev->send_buff = NULL;
  40. vdev->running = false;
  41. mutex_unlock(&vdev->mtx);
  42. return 0;
  43. }
  44. static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
  45. {
  46. struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
  47. mutex_lock(&vdev->mtx);
  48. if (vdev->send_buff || !vdev->running) {
  49. mutex_unlock(&vdev->mtx);
  50. kfree_skb(skb);
  51. return -1;
  52. }
  53. vdev->send_buff = skb_copy(skb, GFP_KERNEL);
  54. if (!vdev->send_buff) {
  55. mutex_unlock(&vdev->mtx);
  56. kfree_skb(skb);
  57. return -1;
  58. }
  59. mutex_unlock(&vdev->mtx);
  60. wake_up_interruptible(&vdev->wq);
  61. consume_skb(skb);
  62. return 0;
  63. }
  64. static const struct nci_ops virtual_nci_ops = {
  65. .open = virtual_nci_open,
  66. .close = virtual_nci_close,
  67. .send = virtual_nci_send
  68. };
  69. static ssize_t virtual_ncidev_read(struct file *file, char __user *buf,
  70. size_t count, loff_t *ppos)
  71. {
  72. struct virtual_nci_dev *vdev = file->private_data;
  73. size_t actual_len;
  74. mutex_lock(&vdev->mtx);
  75. while (!vdev->send_buff) {
  76. mutex_unlock(&vdev->mtx);
  77. if (wait_event_interruptible(vdev->wq, vdev->send_buff))
  78. return -EFAULT;
  79. mutex_lock(&vdev->mtx);
  80. }
  81. actual_len = min_t(size_t, count, vdev->send_buff->len);
  82. if (copy_to_user(buf, vdev->send_buff->data, actual_len)) {
  83. mutex_unlock(&vdev->mtx);
  84. return -EFAULT;
  85. }
  86. skb_pull(vdev->send_buff, actual_len);
  87. if (vdev->send_buff->len == 0) {
  88. consume_skb(vdev->send_buff);
  89. vdev->send_buff = NULL;
  90. }
  91. mutex_unlock(&vdev->mtx);
  92. return actual_len;
  93. }
  94. static ssize_t virtual_ncidev_write(struct file *file,
  95. const char __user *buf,
  96. size_t count, loff_t *ppos)
  97. {
  98. struct virtual_nci_dev *vdev = file->private_data;
  99. struct sk_buff *skb;
  100. skb = alloc_skb(count, GFP_KERNEL);
  101. if (!skb)
  102. return -ENOMEM;
  103. if (copy_from_user(skb_put(skb, count), buf, count)) {
  104. kfree_skb(skb);
  105. return -EFAULT;
  106. }
  107. nci_recv_frame(vdev->ndev, skb);
  108. return count;
  109. }
  110. static int virtual_ncidev_open(struct inode *inode, struct file *file)
  111. {
  112. int ret = 0;
  113. struct virtual_nci_dev *vdev;
  114. vdev = kzalloc_obj(*vdev);
  115. if (!vdev)
  116. return -ENOMEM;
  117. vdev->ndev = nci_allocate_device(&virtual_nci_ops,
  118. VIRTUAL_NFC_PROTOCOLS, 0, 0);
  119. if (!vdev->ndev) {
  120. kfree(vdev);
  121. return -ENOMEM;
  122. }
  123. mutex_init(&vdev->mtx);
  124. init_waitqueue_head(&vdev->wq);
  125. file->private_data = vdev;
  126. nci_set_drvdata(vdev->ndev, vdev);
  127. ret = nci_register_device(vdev->ndev);
  128. if (ret < 0) {
  129. nci_free_device(vdev->ndev);
  130. mutex_destroy(&vdev->mtx);
  131. kfree(vdev);
  132. return ret;
  133. }
  134. return 0;
  135. }
  136. static int virtual_ncidev_close(struct inode *inode, struct file *file)
  137. {
  138. struct virtual_nci_dev *vdev = file->private_data;
  139. nci_unregister_device(vdev->ndev);
  140. nci_free_device(vdev->ndev);
  141. mutex_destroy(&vdev->mtx);
  142. kfree(vdev);
  143. return 0;
  144. }
  145. static long virtual_ncidev_ioctl(struct file *file, unsigned int cmd,
  146. unsigned long arg)
  147. {
  148. struct virtual_nci_dev *vdev = file->private_data;
  149. const struct nfc_dev *nfc_dev = vdev->ndev->nfc_dev;
  150. void __user *p = (void __user *)arg;
  151. if (cmd != IOCTL_GET_NCIDEV_IDX)
  152. return -ENOTTY;
  153. if (copy_to_user(p, &nfc_dev->idx, sizeof(nfc_dev->idx)))
  154. return -EFAULT;
  155. return 0;
  156. }
  157. static const struct file_operations virtual_ncidev_fops = {
  158. .owner = THIS_MODULE,
  159. .read = virtual_ncidev_read,
  160. .write = virtual_ncidev_write,
  161. .open = virtual_ncidev_open,
  162. .release = virtual_ncidev_close,
  163. .unlocked_ioctl = virtual_ncidev_ioctl
  164. };
  165. static struct miscdevice miscdev = {
  166. .minor = MISC_DYNAMIC_MINOR,
  167. .name = "virtual_nci",
  168. .fops = &virtual_ncidev_fops,
  169. .mode = 0600,
  170. };
  171. module_misc_device(miscdev);
  172. MODULE_LICENSE("GPL");
  173. MODULE_DESCRIPTION("Virtual NCI device simulation driver");
  174. MODULE_AUTHOR("Bongsu Jeon <bongsu.jeon@samsung.com>");