1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Virtual NCI device simulation driver
5 * Copyright (C) 2020 Samsung Electrnoics
6 * Bongsu Jeon <bongsu.jeon@samsung.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/miscdevice.h>
12 #include <linux/mutex.h>
13 #include <linux/wait.h>
14 #include <net/nfc/nci_core.h>
16 #define IOCTL_GET_NCIDEV_IDX 0
17 #define VIRTUAL_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
18 NFC_PROTO_MIFARE_MASK | \
19 NFC_PROTO_FELICA_MASK | \
20 NFC_PROTO_ISO14443_MASK | \
21 NFC_PROTO_ISO14443_B_MASK | \
22 NFC_PROTO_ISO15693_MASK)
24 struct virtual_nci_dev {
27 struct sk_buff *send_buff;
28 struct wait_queue_head wq;
31 static int virtual_nci_open(struct nci_dev *ndev)
36 static int virtual_nci_close(struct nci_dev *ndev)
38 struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
40 mutex_lock(&vdev->mtx);
41 kfree_skb(vdev->send_buff);
42 vdev->send_buff = NULL;
43 mutex_unlock(&vdev->mtx);
48 static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
50 struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
52 mutex_lock(&vdev->mtx);
53 if (vdev->send_buff) {
54 mutex_unlock(&vdev->mtx);
58 vdev->send_buff = skb_copy(skb, GFP_KERNEL);
59 if (!vdev->send_buff) {
60 mutex_unlock(&vdev->mtx);
64 mutex_unlock(&vdev->mtx);
65 wake_up_interruptible(&vdev->wq);
71 static const struct nci_ops virtual_nci_ops = {
72 .open = virtual_nci_open,
73 .close = virtual_nci_close,
74 .send = virtual_nci_send
77 static ssize_t virtual_ncidev_read(struct file *file, char __user *buf,
78 size_t count, loff_t *ppos)
80 struct virtual_nci_dev *vdev = file->private_data;
83 mutex_lock(&vdev->mtx);
84 while (!vdev->send_buff) {
85 mutex_unlock(&vdev->mtx);
86 if (wait_event_interruptible(vdev->wq, vdev->send_buff))
88 mutex_lock(&vdev->mtx);
91 actual_len = min_t(size_t, count, vdev->send_buff->len);
93 if (copy_to_user(buf, vdev->send_buff->data, actual_len)) {
94 mutex_unlock(&vdev->mtx);
98 skb_pull(vdev->send_buff, actual_len);
99 if (vdev->send_buff->len == 0) {
100 consume_skb(vdev->send_buff);
101 vdev->send_buff = NULL;
103 mutex_unlock(&vdev->mtx);
108 static ssize_t virtual_ncidev_write(struct file *file,
109 const char __user *buf,
110 size_t count, loff_t *ppos)
112 struct virtual_nci_dev *vdev = file->private_data;
115 skb = alloc_skb(count, GFP_KERNEL);
119 if (copy_from_user(skb_put(skb, count), buf, count)) {
124 nci_recv_frame(vdev->ndev, skb);
128 static int virtual_ncidev_open(struct inode *inode, struct file *file)
131 struct virtual_nci_dev *vdev;
133 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
136 vdev->ndev = nci_allocate_device(&virtual_nci_ops,
137 VIRTUAL_NFC_PROTOCOLS, 0, 0);
143 mutex_init(&vdev->mtx);
144 init_waitqueue_head(&vdev->wq);
145 file->private_data = vdev;
146 nci_set_drvdata(vdev->ndev, vdev);
148 ret = nci_register_device(vdev->ndev);
150 nci_free_device(vdev->ndev);
151 mutex_destroy(&vdev->mtx);
159 static int virtual_ncidev_close(struct inode *inode, struct file *file)
161 struct virtual_nci_dev *vdev = file->private_data;
163 nci_unregister_device(vdev->ndev);
164 nci_free_device(vdev->ndev);
165 mutex_destroy(&vdev->mtx);
171 static long virtual_ncidev_ioctl(struct file *file, unsigned int cmd,
174 struct virtual_nci_dev *vdev = file->private_data;
175 const struct nfc_dev *nfc_dev = vdev->ndev->nfc_dev;
176 void __user *p = (void __user *)arg;
178 if (cmd != IOCTL_GET_NCIDEV_IDX)
181 if (copy_to_user(p, &nfc_dev->idx, sizeof(nfc_dev->idx)))
187 static const struct file_operations virtual_ncidev_fops = {
188 .owner = THIS_MODULE,
189 .read = virtual_ncidev_read,
190 .write = virtual_ncidev_write,
191 .open = virtual_ncidev_open,
192 .release = virtual_ncidev_close,
193 .unlocked_ioctl = virtual_ncidev_ioctl
196 static struct miscdevice miscdev = {
197 .minor = MISC_DYNAMIC_MINOR,
198 .name = "virtual_nci",
199 .fops = &virtual_ncidev_fops,
203 module_misc_device(miscdev);
205 MODULE_LICENSE("GPL");
206 MODULE_DESCRIPTION("Virtual NCI device simulation driver");
207 MODULE_AUTHOR("Bongsu Jeon <bongsu.jeon@samsung.com>");