virtual_ncidev.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtual NCI device simulation driver
  4. *
  5. * Copyright (C) 2020 Samsung Electrnoics
  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. if (strnlen(skb->data, count) != count) {
  108. kfree_skb(skb);
  109. return -EINVAL;
  110. }
  111. nci_recv_frame(vdev->ndev, skb);
  112. return count;
  113. }
  114. static int virtual_ncidev_open(struct inode *inode, struct file *file)
  115. {
  116. int ret = 0;
  117. struct virtual_nci_dev *vdev;
  118. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  119. if (!vdev)
  120. return -ENOMEM;
  121. vdev->ndev = nci_allocate_device(&virtual_nci_ops,
  122. VIRTUAL_NFC_PROTOCOLS, 0, 0);
  123. if (!vdev->ndev) {
  124. kfree(vdev);
  125. return -ENOMEM;
  126. }
  127. mutex_init(&vdev->mtx);
  128. init_waitqueue_head(&vdev->wq);
  129. file->private_data = vdev;
  130. nci_set_drvdata(vdev->ndev, vdev);
  131. ret = nci_register_device(vdev->ndev);
  132. if (ret < 0) {
  133. nci_free_device(vdev->ndev);
  134. mutex_destroy(&vdev->mtx);
  135. kfree(vdev);
  136. return ret;
  137. }
  138. return 0;
  139. }
  140. static int virtual_ncidev_close(struct inode *inode, struct file *file)
  141. {
  142. struct virtual_nci_dev *vdev = file->private_data;
  143. nci_unregister_device(vdev->ndev);
  144. nci_free_device(vdev->ndev);
  145. mutex_destroy(&vdev->mtx);
  146. kfree(vdev);
  147. return 0;
  148. }
  149. static long virtual_ncidev_ioctl(struct file *file, unsigned int cmd,
  150. unsigned long arg)
  151. {
  152. struct virtual_nci_dev *vdev = file->private_data;
  153. const struct nfc_dev *nfc_dev = vdev->ndev->nfc_dev;
  154. void __user *p = (void __user *)arg;
  155. if (cmd != IOCTL_GET_NCIDEV_IDX)
  156. return -ENOTTY;
  157. if (copy_to_user(p, &nfc_dev->idx, sizeof(nfc_dev->idx)))
  158. return -EFAULT;
  159. return 0;
  160. }
  161. static const struct file_operations virtual_ncidev_fops = {
  162. .owner = THIS_MODULE,
  163. .read = virtual_ncidev_read,
  164. .write = virtual_ncidev_write,
  165. .open = virtual_ncidev_open,
  166. .release = virtual_ncidev_close,
  167. .unlocked_ioctl = virtual_ncidev_ioctl
  168. };
  169. static struct miscdevice miscdev = {
  170. .minor = MISC_DYNAMIC_MINOR,
  171. .name = "virtual_nci",
  172. .fops = &virtual_ncidev_fops,
  173. .mode = 0600,
  174. };
  175. module_misc_device(miscdev);
  176. MODULE_LICENSE("GPL");
  177. MODULE_DESCRIPTION("Virtual NCI device simulation driver");
  178. MODULE_AUTHOR("Bongsu Jeon <bongsu.jeon@samsung.com>");