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 enum virtual_ncidev_mode {
17 virtual_ncidev_enabled,
18 virtual_ncidev_disabled,
19 virtual_ncidev_disabling,
22 #define IOCTL_GET_NCIDEV_IDX 0
23 #define VIRTUAL_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
24 NFC_PROTO_MIFARE_MASK | \
25 NFC_PROTO_FELICA_MASK | \
26 NFC_PROTO_ISO14443_MASK | \
27 NFC_PROTO_ISO14443_B_MASK | \
28 NFC_PROTO_ISO15693_MASK)
30 static enum virtual_ncidev_mode state;
31 static DECLARE_WAIT_QUEUE_HEAD(wq);
32 static struct miscdevice miscdev;
33 static struct sk_buff *send_buff;
34 static struct nci_dev *ndev;
35 static DEFINE_MUTEX(nci_mutex);
37 static int virtual_nci_open(struct nci_dev *ndev)
42 static int virtual_nci_close(struct nci_dev *ndev)
44 mutex_lock(&nci_mutex);
47 mutex_unlock(&nci_mutex);
52 static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
54 mutex_lock(&nci_mutex);
55 if (state != virtual_ncidev_enabled) {
56 mutex_unlock(&nci_mutex);
62 mutex_unlock(&nci_mutex);
66 send_buff = skb_copy(skb, GFP_KERNEL);
67 mutex_unlock(&nci_mutex);
68 wake_up_interruptible(&wq);
74 static const struct nci_ops virtual_nci_ops = {
75 .open = virtual_nci_open,
76 .close = virtual_nci_close,
77 .send = virtual_nci_send
80 static ssize_t virtual_ncidev_read(struct file *file, char __user *buf,
81 size_t count, loff_t *ppos)
85 mutex_lock(&nci_mutex);
87 mutex_unlock(&nci_mutex);
88 if (wait_event_interruptible(wq, send_buff))
90 mutex_lock(&nci_mutex);
93 actual_len = min_t(size_t, count, send_buff->len);
95 if (copy_to_user(buf, send_buff->data, actual_len)) {
96 mutex_unlock(&nci_mutex);
100 skb_pull(send_buff, actual_len);
101 if (send_buff->len == 0) {
102 consume_skb(send_buff);
105 mutex_unlock(&nci_mutex);
110 static ssize_t virtual_ncidev_write(struct file *file,
111 const char __user *buf,
112 size_t count, loff_t *ppos)
116 skb = alloc_skb(count, GFP_KERNEL);
120 if (copy_from_user(skb_put(skb, count), buf, count)) {
125 nci_recv_frame(ndev, skb);
129 static int virtual_ncidev_open(struct inode *inode, struct file *file)
133 mutex_lock(&nci_mutex);
134 if (state != virtual_ncidev_disabled) {
135 mutex_unlock(&nci_mutex);
139 ndev = nci_allocate_device(&virtual_nci_ops, VIRTUAL_NFC_PROTOCOLS,
142 mutex_unlock(&nci_mutex);
146 ret = nci_register_device(ndev);
148 nci_free_device(ndev);
149 mutex_unlock(&nci_mutex);
152 state = virtual_ncidev_enabled;
153 mutex_unlock(&nci_mutex);
158 static int virtual_ncidev_close(struct inode *inode, struct file *file)
160 mutex_lock(&nci_mutex);
162 if (state == virtual_ncidev_enabled) {
163 state = virtual_ncidev_disabling;
164 mutex_unlock(&nci_mutex);
166 nci_unregister_device(ndev);
167 nci_free_device(ndev);
169 mutex_lock(&nci_mutex);
172 state = virtual_ncidev_disabled;
173 mutex_unlock(&nci_mutex);
178 static long virtual_ncidev_ioctl(struct file *flip, unsigned int cmd,
181 const struct nfc_dev *nfc_dev = ndev->nfc_dev;
182 void __user *p = (void __user *)arg;
184 if (cmd != IOCTL_GET_NCIDEV_IDX)
187 if (copy_to_user(p, &nfc_dev->idx, sizeof(nfc_dev->idx)))
193 static const struct file_operations virtual_ncidev_fops = {
194 .owner = THIS_MODULE,
195 .read = virtual_ncidev_read,
196 .write = virtual_ncidev_write,
197 .open = virtual_ncidev_open,
198 .release = virtual_ncidev_close,
199 .unlocked_ioctl = virtual_ncidev_ioctl
202 static int __init virtual_ncidev_init(void)
204 state = virtual_ncidev_disabled;
205 miscdev.minor = MISC_DYNAMIC_MINOR;
206 miscdev.name = "virtual_nci";
207 miscdev.fops = &virtual_ncidev_fops;
208 miscdev.mode = S_IALLUGO;
210 return misc_register(&miscdev);
213 static void __exit virtual_ncidev_exit(void)
215 misc_deregister(&miscdev);
218 module_init(virtual_ncidev_init);
219 module_exit(virtual_ncidev_exit);
221 MODULE_LICENSE("GPL");
222 MODULE_DESCRIPTION("Virtual NCI device simulation driver");
223 MODULE_AUTHOR("Bongsu Jeon <bongsu.jeon@samsung.com>");