Loading...
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
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>
15
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)
23
24struct virtual_nci_dev {
25 struct nci_dev *ndev;
26 struct mutex mtx;
27 struct sk_buff *send_buff;
28 struct wait_queue_head wq;
29 bool running;
30};
31
32static int virtual_nci_open(struct nci_dev *ndev)
33{
34 struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
35
36 vdev->running = true;
37 return 0;
38}
39
40static int virtual_nci_close(struct nci_dev *ndev)
41{
42 struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
43
44 mutex_lock(&vdev->mtx);
45 kfree_skb(vdev->send_buff);
46 vdev->send_buff = NULL;
47 vdev->running = false;
48 mutex_unlock(&vdev->mtx);
49
50 return 0;
51}
52
53static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
54{
55 struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
56
57 mutex_lock(&vdev->mtx);
58 if (vdev->send_buff || !vdev->running) {
59 mutex_unlock(&vdev->mtx);
60 kfree_skb(skb);
61 return -1;
62 }
63 vdev->send_buff = skb_copy(skb, GFP_KERNEL);
64 if (!vdev->send_buff) {
65 mutex_unlock(&vdev->mtx);
66 kfree_skb(skb);
67 return -1;
68 }
69 mutex_unlock(&vdev->mtx);
70 wake_up_interruptible(&vdev->wq);
71 consume_skb(skb);
72
73 return 0;
74}
75
76static const struct nci_ops virtual_nci_ops = {
77 .open = virtual_nci_open,
78 .close = virtual_nci_close,
79 .send = virtual_nci_send
80};
81
82static ssize_t virtual_ncidev_read(struct file *file, char __user *buf,
83 size_t count, loff_t *ppos)
84{
85 struct virtual_nci_dev *vdev = file->private_data;
86 size_t actual_len;
87
88 mutex_lock(&vdev->mtx);
89 while (!vdev->send_buff) {
90 mutex_unlock(&vdev->mtx);
91 if (wait_event_interruptible(vdev->wq, vdev->send_buff))
92 return -EFAULT;
93 mutex_lock(&vdev->mtx);
94 }
95
96 actual_len = min_t(size_t, count, vdev->send_buff->len);
97
98 if (copy_to_user(buf, vdev->send_buff->data, actual_len)) {
99 mutex_unlock(&vdev->mtx);
100 return -EFAULT;
101 }
102
103 skb_pull(vdev->send_buff, actual_len);
104 if (vdev->send_buff->len == 0) {
105 consume_skb(vdev->send_buff);
106 vdev->send_buff = NULL;
107 }
108 mutex_unlock(&vdev->mtx);
109
110 return actual_len;
111}
112
113static ssize_t virtual_ncidev_write(struct file *file,
114 const char __user *buf,
115 size_t count, loff_t *ppos)
116{
117 struct virtual_nci_dev *vdev = file->private_data;
118 struct sk_buff *skb;
119
120 skb = alloc_skb(count, GFP_KERNEL);
121 if (!skb)
122 return -ENOMEM;
123
124 if (copy_from_user(skb_put(skb, count), buf, count)) {
125 kfree_skb(skb);
126 return -EFAULT;
127 }
128
129 nci_recv_frame(vdev->ndev, skb);
130 return count;
131}
132
133static int virtual_ncidev_open(struct inode *inode, struct file *file)
134{
135 int ret = 0;
136 struct virtual_nci_dev *vdev;
137
138 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
139 if (!vdev)
140 return -ENOMEM;
141 vdev->ndev = nci_allocate_device(&virtual_nci_ops,
142 VIRTUAL_NFC_PROTOCOLS, 0, 0);
143 if (!vdev->ndev) {
144 kfree(vdev);
145 return -ENOMEM;
146 }
147
148 mutex_init(&vdev->mtx);
149 init_waitqueue_head(&vdev->wq);
150 file->private_data = vdev;
151 nci_set_drvdata(vdev->ndev, vdev);
152
153 ret = nci_register_device(vdev->ndev);
154 if (ret < 0) {
155 nci_free_device(vdev->ndev);
156 mutex_destroy(&vdev->mtx);
157 kfree(vdev);
158 return ret;
159 }
160
161 return 0;
162}
163
164static int virtual_ncidev_close(struct inode *inode, struct file *file)
165{
166 struct virtual_nci_dev *vdev = file->private_data;
167
168 nci_unregister_device(vdev->ndev);
169 nci_free_device(vdev->ndev);
170 mutex_destroy(&vdev->mtx);
171 kfree(vdev);
172
173 return 0;
174}
175
176static long virtual_ncidev_ioctl(struct file *file, unsigned int cmd,
177 unsigned long arg)
178{
179 struct virtual_nci_dev *vdev = file->private_data;
180 const struct nfc_dev *nfc_dev = vdev->ndev->nfc_dev;
181 void __user *p = (void __user *)arg;
182
183 if (cmd != IOCTL_GET_NCIDEV_IDX)
184 return -ENOTTY;
185
186 if (copy_to_user(p, &nfc_dev->idx, sizeof(nfc_dev->idx)))
187 return -EFAULT;
188
189 return 0;
190}
191
192static const struct file_operations virtual_ncidev_fops = {
193 .owner = THIS_MODULE,
194 .read = virtual_ncidev_read,
195 .write = virtual_ncidev_write,
196 .open = virtual_ncidev_open,
197 .release = virtual_ncidev_close,
198 .unlocked_ioctl = virtual_ncidev_ioctl
199};
200
201static struct miscdevice miscdev = {
202 .minor = MISC_DYNAMIC_MINOR,
203 .name = "virtual_nci",
204 .fops = &virtual_ncidev_fops,
205 .mode = 0600,
206};
207
208module_misc_device(miscdev);
209
210MODULE_LICENSE("GPL");
211MODULE_DESCRIPTION("Virtual NCI device simulation driver");
212MODULE_AUTHOR("Bongsu Jeon <bongsu.jeon@samsung.com>");
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
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>
15
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)
23
24struct virtual_nci_dev {
25 struct nci_dev *ndev;
26 struct mutex mtx;
27 struct sk_buff *send_buff;
28 struct wait_queue_head wq;
29};
30
31static int virtual_nci_open(struct nci_dev *ndev)
32{
33 return 0;
34}
35
36static int virtual_nci_close(struct nci_dev *ndev)
37{
38 struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
39
40 mutex_lock(&vdev->mtx);
41 kfree_skb(vdev->send_buff);
42 vdev->send_buff = NULL;
43 mutex_unlock(&vdev->mtx);
44
45 return 0;
46}
47
48static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
49{
50 struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
51
52 mutex_lock(&vdev->mtx);
53 if (vdev->send_buff) {
54 mutex_unlock(&vdev->mtx);
55 kfree_skb(skb);
56 return -1;
57 }
58 vdev->send_buff = skb_copy(skb, GFP_KERNEL);
59 if (!vdev->send_buff) {
60 mutex_unlock(&vdev->mtx);
61 kfree_skb(skb);
62 return -1;
63 }
64 mutex_unlock(&vdev->mtx);
65 wake_up_interruptible(&vdev->wq);
66 consume_skb(skb);
67
68 return 0;
69}
70
71static const struct nci_ops virtual_nci_ops = {
72 .open = virtual_nci_open,
73 .close = virtual_nci_close,
74 .send = virtual_nci_send
75};
76
77static ssize_t virtual_ncidev_read(struct file *file, char __user *buf,
78 size_t count, loff_t *ppos)
79{
80 struct virtual_nci_dev *vdev = file->private_data;
81 size_t actual_len;
82
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))
87 return -EFAULT;
88 mutex_lock(&vdev->mtx);
89 }
90
91 actual_len = min_t(size_t, count, vdev->send_buff->len);
92
93 if (copy_to_user(buf, vdev->send_buff->data, actual_len)) {
94 mutex_unlock(&vdev->mtx);
95 return -EFAULT;
96 }
97
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;
102 }
103 mutex_unlock(&vdev->mtx);
104
105 return actual_len;
106}
107
108static ssize_t virtual_ncidev_write(struct file *file,
109 const char __user *buf,
110 size_t count, loff_t *ppos)
111{
112 struct virtual_nci_dev *vdev = file->private_data;
113 struct sk_buff *skb;
114
115 skb = alloc_skb(count, GFP_KERNEL);
116 if (!skb)
117 return -ENOMEM;
118
119 if (copy_from_user(skb_put(skb, count), buf, count)) {
120 kfree_skb(skb);
121 return -EFAULT;
122 }
123
124 nci_recv_frame(vdev->ndev, skb);
125 return count;
126}
127
128static int virtual_ncidev_open(struct inode *inode, struct file *file)
129{
130 int ret = 0;
131 struct virtual_nci_dev *vdev;
132
133 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
134 if (!vdev)
135 return -ENOMEM;
136 vdev->ndev = nci_allocate_device(&virtual_nci_ops,
137 VIRTUAL_NFC_PROTOCOLS, 0, 0);
138 if (!vdev->ndev) {
139 kfree(vdev);
140 return -ENOMEM;
141 }
142
143 mutex_init(&vdev->mtx);
144 init_waitqueue_head(&vdev->wq);
145 file->private_data = vdev;
146 nci_set_drvdata(vdev->ndev, vdev);
147
148 ret = nci_register_device(vdev->ndev);
149 if (ret < 0) {
150 nci_free_device(vdev->ndev);
151 mutex_destroy(&vdev->mtx);
152 kfree(vdev);
153 return ret;
154 }
155
156 return 0;
157}
158
159static int virtual_ncidev_close(struct inode *inode, struct file *file)
160{
161 struct virtual_nci_dev *vdev = file->private_data;
162
163 nci_unregister_device(vdev->ndev);
164 nci_free_device(vdev->ndev);
165 mutex_destroy(&vdev->mtx);
166 kfree(vdev);
167
168 return 0;
169}
170
171static long virtual_ncidev_ioctl(struct file *file, unsigned int cmd,
172 unsigned long arg)
173{
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;
177
178 if (cmd != IOCTL_GET_NCIDEV_IDX)
179 return -ENOTTY;
180
181 if (copy_to_user(p, &nfc_dev->idx, sizeof(nfc_dev->idx)))
182 return -EFAULT;
183
184 return 0;
185}
186
187static 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
194};
195
196static struct miscdevice miscdev = {
197 .minor = MISC_DYNAMIC_MINOR,
198 .name = "virtual_nci",
199 .fops = &virtual_ncidev_fops,
200 .mode = 0600,
201};
202
203static int __init virtual_ncidev_init(void)
204{
205 return misc_register(&miscdev);
206}
207
208static void __exit virtual_ncidev_exit(void)
209{
210 misc_deregister(&miscdev);
211}
212
213module_init(virtual_ncidev_init);
214module_exit(virtual_ncidev_exit);
215
216MODULE_LICENSE("GPL");
217MODULE_DESCRIPTION("Virtual NCI device simulation driver");
218MODULE_AUTHOR("Bongsu Jeon <bongsu.jeon@samsung.com>");