xref: /linux/drivers/nfc/virtual_ncidev.c (revision 8675569d73cab15e5127d1f228afd3317cbeb5bf)
1e624e6c3SBongsu Jeon // SPDX-License-Identifier: GPL-2.0-or-later
2e624e6c3SBongsu Jeon /*
3e624e6c3SBongsu Jeon  * Virtual NCI device simulation driver
4e624e6c3SBongsu Jeon  *
5e624e6c3SBongsu Jeon  * Copyright (C) 2020 Samsung Electrnoics
6e624e6c3SBongsu Jeon  * Bongsu Jeon <bongsu.jeon@samsung.com>
7e624e6c3SBongsu Jeon  */
8e624e6c3SBongsu Jeon 
9e624e6c3SBongsu Jeon #include <linux/kernel.h>
10e624e6c3SBongsu Jeon #include <linux/module.h>
11e624e6c3SBongsu Jeon #include <linux/miscdevice.h>
12e624e6c3SBongsu Jeon #include <linux/mutex.h>
13*8675569dSBongsu Jeon #include <linux/wait.h>
14e624e6c3SBongsu Jeon #include <net/nfc/nci_core.h>
15e624e6c3SBongsu Jeon 
16e624e6c3SBongsu Jeon enum virtual_ncidev_mode {
17e624e6c3SBongsu Jeon 	virtual_ncidev_enabled,
18e624e6c3SBongsu Jeon 	virtual_ncidev_disabled,
19e624e6c3SBongsu Jeon 	virtual_ncidev_disabling,
20e624e6c3SBongsu Jeon };
21e624e6c3SBongsu Jeon 
22e624e6c3SBongsu Jeon #define IOCTL_GET_NCIDEV_IDX    0
23e624e6c3SBongsu Jeon #define VIRTUAL_NFC_PROTOCOLS	(NFC_PROTO_JEWEL_MASK | \
24e624e6c3SBongsu Jeon 				 NFC_PROTO_MIFARE_MASK | \
25e624e6c3SBongsu Jeon 				 NFC_PROTO_FELICA_MASK | \
26e624e6c3SBongsu Jeon 				 NFC_PROTO_ISO14443_MASK | \
27e624e6c3SBongsu Jeon 				 NFC_PROTO_ISO14443_B_MASK | \
28e624e6c3SBongsu Jeon 				 NFC_PROTO_ISO15693_MASK)
29e624e6c3SBongsu Jeon 
30e624e6c3SBongsu Jeon static enum virtual_ncidev_mode state;
31*8675569dSBongsu Jeon static DECLARE_WAIT_QUEUE_HEAD(wq);
32e624e6c3SBongsu Jeon static struct miscdevice miscdev;
33e624e6c3SBongsu Jeon static struct sk_buff *send_buff;
34e624e6c3SBongsu Jeon static struct nci_dev *ndev;
35e624e6c3SBongsu Jeon static DEFINE_MUTEX(nci_mutex);
36e624e6c3SBongsu Jeon 
37e624e6c3SBongsu Jeon static int virtual_nci_open(struct nci_dev *ndev)
38e624e6c3SBongsu Jeon {
39e624e6c3SBongsu Jeon 	return 0;
40e624e6c3SBongsu Jeon }
41e624e6c3SBongsu Jeon 
42e624e6c3SBongsu Jeon static int virtual_nci_close(struct nci_dev *ndev)
43e624e6c3SBongsu Jeon {
44e624e6c3SBongsu Jeon 	mutex_lock(&nci_mutex);
45e624e6c3SBongsu Jeon 	kfree_skb(send_buff);
46e624e6c3SBongsu Jeon 	send_buff = NULL;
47e624e6c3SBongsu Jeon 	mutex_unlock(&nci_mutex);
48e624e6c3SBongsu Jeon 
49e624e6c3SBongsu Jeon 	return 0;
50e624e6c3SBongsu Jeon }
51e624e6c3SBongsu Jeon 
52e624e6c3SBongsu Jeon static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
53e624e6c3SBongsu Jeon {
54e624e6c3SBongsu Jeon 	mutex_lock(&nci_mutex);
55e624e6c3SBongsu Jeon 	if (state != virtual_ncidev_enabled) {
56e624e6c3SBongsu Jeon 		mutex_unlock(&nci_mutex);
57e624e6c3SBongsu Jeon 		return 0;
58e624e6c3SBongsu Jeon 	}
59e624e6c3SBongsu Jeon 
60e624e6c3SBongsu Jeon 	if (send_buff) {
61e624e6c3SBongsu Jeon 		mutex_unlock(&nci_mutex);
62e624e6c3SBongsu Jeon 		return -1;
63e624e6c3SBongsu Jeon 	}
64e624e6c3SBongsu Jeon 	send_buff = skb_copy(skb, GFP_KERNEL);
65e624e6c3SBongsu Jeon 	mutex_unlock(&nci_mutex);
66*8675569dSBongsu Jeon 	wake_up_interruptible(&wq);
67e624e6c3SBongsu Jeon 
68e624e6c3SBongsu Jeon 	return 0;
69e624e6c3SBongsu Jeon }
70e624e6c3SBongsu Jeon 
71b9c28286SKrzysztof Kozlowski static const struct nci_ops virtual_nci_ops = {
72e624e6c3SBongsu Jeon 	.open = virtual_nci_open,
73e624e6c3SBongsu Jeon 	.close = virtual_nci_close,
74e624e6c3SBongsu Jeon 	.send = virtual_nci_send
75e624e6c3SBongsu Jeon };
76e624e6c3SBongsu Jeon 
77e624e6c3SBongsu Jeon static ssize_t virtual_ncidev_read(struct file *file, char __user *buf,
78e624e6c3SBongsu Jeon 				   size_t count, loff_t *ppos)
79e624e6c3SBongsu Jeon {
80e624e6c3SBongsu Jeon 	size_t actual_len;
81e624e6c3SBongsu Jeon 
82e624e6c3SBongsu Jeon 	mutex_lock(&nci_mutex);
83*8675569dSBongsu Jeon 	while (!send_buff) {
84e624e6c3SBongsu Jeon 		mutex_unlock(&nci_mutex);
85*8675569dSBongsu Jeon 		if (wait_event_interruptible(wq, send_buff))
86*8675569dSBongsu Jeon 			return -EFAULT;
87*8675569dSBongsu Jeon 		mutex_lock(&nci_mutex);
88e624e6c3SBongsu Jeon 	}
89e624e6c3SBongsu Jeon 
90e624e6c3SBongsu Jeon 	actual_len = min_t(size_t, count, send_buff->len);
91e624e6c3SBongsu Jeon 
92e624e6c3SBongsu Jeon 	if (copy_to_user(buf, send_buff->data, actual_len)) {
93e624e6c3SBongsu Jeon 		mutex_unlock(&nci_mutex);
94e624e6c3SBongsu Jeon 		return -EFAULT;
95e624e6c3SBongsu Jeon 	}
96e624e6c3SBongsu Jeon 
97e624e6c3SBongsu Jeon 	skb_pull(send_buff, actual_len);
98e624e6c3SBongsu Jeon 	if (send_buff->len == 0) {
99e624e6c3SBongsu Jeon 		consume_skb(send_buff);
100e624e6c3SBongsu Jeon 		send_buff = NULL;
101e624e6c3SBongsu Jeon 	}
102e624e6c3SBongsu Jeon 	mutex_unlock(&nci_mutex);
103e624e6c3SBongsu Jeon 
104e624e6c3SBongsu Jeon 	return actual_len;
105e624e6c3SBongsu Jeon }
106e624e6c3SBongsu Jeon 
107e624e6c3SBongsu Jeon static ssize_t virtual_ncidev_write(struct file *file,
108e624e6c3SBongsu Jeon 				    const char __user *buf,
109e624e6c3SBongsu Jeon 				    size_t count, loff_t *ppos)
110e624e6c3SBongsu Jeon {
111e624e6c3SBongsu Jeon 	struct sk_buff *skb;
112e624e6c3SBongsu Jeon 
113e624e6c3SBongsu Jeon 	skb = alloc_skb(count, GFP_KERNEL);
114e624e6c3SBongsu Jeon 	if (!skb)
115e624e6c3SBongsu Jeon 		return -ENOMEM;
116e624e6c3SBongsu Jeon 
117e624e6c3SBongsu Jeon 	if (copy_from_user(skb_put(skb, count), buf, count)) {
118e624e6c3SBongsu Jeon 		kfree_skb(skb);
119e624e6c3SBongsu Jeon 		return -EFAULT;
120e624e6c3SBongsu Jeon 	}
121e624e6c3SBongsu Jeon 
122e624e6c3SBongsu Jeon 	nci_recv_frame(ndev, skb);
123e624e6c3SBongsu Jeon 	return count;
124e624e6c3SBongsu Jeon }
125e624e6c3SBongsu Jeon 
126e624e6c3SBongsu Jeon static int virtual_ncidev_open(struct inode *inode, struct file *file)
127e624e6c3SBongsu Jeon {
128e624e6c3SBongsu Jeon 	int ret = 0;
129e624e6c3SBongsu Jeon 
130e624e6c3SBongsu Jeon 	mutex_lock(&nci_mutex);
131e624e6c3SBongsu Jeon 	if (state != virtual_ncidev_disabled) {
132e624e6c3SBongsu Jeon 		mutex_unlock(&nci_mutex);
133e624e6c3SBongsu Jeon 		return -EBUSY;
134e624e6c3SBongsu Jeon 	}
135e624e6c3SBongsu Jeon 
136e624e6c3SBongsu Jeon 	ndev = nci_allocate_device(&virtual_nci_ops, VIRTUAL_NFC_PROTOCOLS,
137e624e6c3SBongsu Jeon 				   0, 0);
138e624e6c3SBongsu Jeon 	if (!ndev) {
139e624e6c3SBongsu Jeon 		mutex_unlock(&nci_mutex);
140e624e6c3SBongsu Jeon 		return -ENOMEM;
141e624e6c3SBongsu Jeon 	}
142e624e6c3SBongsu Jeon 
143e624e6c3SBongsu Jeon 	ret = nci_register_device(ndev);
144e624e6c3SBongsu Jeon 	if (ret < 0) {
145e624e6c3SBongsu Jeon 		nci_free_device(ndev);
146e624e6c3SBongsu Jeon 		mutex_unlock(&nci_mutex);
147e624e6c3SBongsu Jeon 		return ret;
148e624e6c3SBongsu Jeon 	}
149e624e6c3SBongsu Jeon 	state = virtual_ncidev_enabled;
150e624e6c3SBongsu Jeon 	mutex_unlock(&nci_mutex);
151e624e6c3SBongsu Jeon 
152e624e6c3SBongsu Jeon 	return 0;
153e624e6c3SBongsu Jeon }
154e624e6c3SBongsu Jeon 
155e624e6c3SBongsu Jeon static int virtual_ncidev_close(struct inode *inode, struct file *file)
156e624e6c3SBongsu Jeon {
157e624e6c3SBongsu Jeon 	mutex_lock(&nci_mutex);
158e624e6c3SBongsu Jeon 
159e624e6c3SBongsu Jeon 	if (state == virtual_ncidev_enabled) {
160e624e6c3SBongsu Jeon 		state = virtual_ncidev_disabling;
161e624e6c3SBongsu Jeon 		mutex_unlock(&nci_mutex);
162e624e6c3SBongsu Jeon 
163e624e6c3SBongsu Jeon 		nci_unregister_device(ndev);
164e624e6c3SBongsu Jeon 		nci_free_device(ndev);
165e624e6c3SBongsu Jeon 
166e624e6c3SBongsu Jeon 		mutex_lock(&nci_mutex);
167e624e6c3SBongsu Jeon 	}
168e624e6c3SBongsu Jeon 
169e624e6c3SBongsu Jeon 	state = virtual_ncidev_disabled;
170e624e6c3SBongsu Jeon 	mutex_unlock(&nci_mutex);
171e624e6c3SBongsu Jeon 
172e624e6c3SBongsu Jeon 	return 0;
173e624e6c3SBongsu Jeon }
174e624e6c3SBongsu Jeon 
175e624e6c3SBongsu Jeon static long virtual_ncidev_ioctl(struct file *flip, unsigned int cmd,
176e624e6c3SBongsu Jeon 				 unsigned long arg)
177e624e6c3SBongsu Jeon {
17883428dbbSKrzysztof Kozlowski 	const struct nfc_dev *nfc_dev = ndev->nfc_dev;
179e624e6c3SBongsu Jeon 	void __user *p = (void __user *)arg;
180e624e6c3SBongsu Jeon 
181e624e6c3SBongsu Jeon 	if (cmd != IOCTL_GET_NCIDEV_IDX)
182e624e6c3SBongsu Jeon 		return -ENOTTY;
183e624e6c3SBongsu Jeon 
184e624e6c3SBongsu Jeon 	if (copy_to_user(p, &nfc_dev->idx, sizeof(nfc_dev->idx)))
185e624e6c3SBongsu Jeon 		return -EFAULT;
186e624e6c3SBongsu Jeon 
187e624e6c3SBongsu Jeon 	return 0;
188e624e6c3SBongsu Jeon }
189e624e6c3SBongsu Jeon 
190e624e6c3SBongsu Jeon static const struct file_operations virtual_ncidev_fops = {
191e624e6c3SBongsu Jeon 	.owner = THIS_MODULE,
192e624e6c3SBongsu Jeon 	.read = virtual_ncidev_read,
193e624e6c3SBongsu Jeon 	.write = virtual_ncidev_write,
194e624e6c3SBongsu Jeon 	.open = virtual_ncidev_open,
195e624e6c3SBongsu Jeon 	.release = virtual_ncidev_close,
196e624e6c3SBongsu Jeon 	.unlocked_ioctl = virtual_ncidev_ioctl
197e624e6c3SBongsu Jeon };
198e624e6c3SBongsu Jeon 
199e624e6c3SBongsu Jeon static int __init virtual_ncidev_init(void)
200e624e6c3SBongsu Jeon {
201e624e6c3SBongsu Jeon 	state = virtual_ncidev_disabled;
202e624e6c3SBongsu Jeon 	miscdev.minor = MISC_DYNAMIC_MINOR;
203e624e6c3SBongsu Jeon 	miscdev.name = "virtual_nci";
204e624e6c3SBongsu Jeon 	miscdev.fops = &virtual_ncidev_fops;
205e624e6c3SBongsu Jeon 	miscdev.mode = S_IALLUGO;
206e624e6c3SBongsu Jeon 
207e624e6c3SBongsu Jeon 	return misc_register(&miscdev);
208e624e6c3SBongsu Jeon }
209e624e6c3SBongsu Jeon 
210e624e6c3SBongsu Jeon static void __exit virtual_ncidev_exit(void)
211e624e6c3SBongsu Jeon {
212e624e6c3SBongsu Jeon 	misc_deregister(&miscdev);
213e624e6c3SBongsu Jeon }
214e624e6c3SBongsu Jeon 
215e624e6c3SBongsu Jeon module_init(virtual_ncidev_init);
216e624e6c3SBongsu Jeon module_exit(virtual_ncidev_exit);
217e624e6c3SBongsu Jeon 
218e624e6c3SBongsu Jeon MODULE_LICENSE("GPL");
219e624e6c3SBongsu Jeon MODULE_DESCRIPTION("Virtual NCI device simulation driver");
220e624e6c3SBongsu Jeon MODULE_AUTHOR("Bongsu Jeon <bongsu.jeon@samsung.com>");
221