Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / qrtr / tun.c
blob15ce9b642b25f380bc7dba1cd1d4bb2ecd550ee0
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Linaro Ltd */
4 #include <linux/miscdevice.h>
5 #include <linux/module.h>
6 #include <linux/poll.h>
7 #include <linux/skbuff.h>
8 #include <linux/uaccess.h>
10 #include "qrtr.h"
12 struct qrtr_tun {
13 struct qrtr_endpoint ep;
15 struct sk_buff_head queue;
16 wait_queue_head_t readq;
19 static int qrtr_tun_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
21 struct qrtr_tun *tun = container_of(ep, struct qrtr_tun, ep);
23 skb_queue_tail(&tun->queue, skb);
25 /* wake up any blocking processes, waiting for new data */
26 wake_up_interruptible(&tun->readq);
28 return 0;
31 static int qrtr_tun_open(struct inode *inode, struct file *filp)
33 struct qrtr_tun *tun;
35 tun = kzalloc(sizeof(*tun), GFP_KERNEL);
36 if (!tun)
37 return -ENOMEM;
39 skb_queue_head_init(&tun->queue);
40 init_waitqueue_head(&tun->readq);
42 tun->ep.xmit = qrtr_tun_send;
44 filp->private_data = tun;
46 return qrtr_endpoint_register(&tun->ep, QRTR_EP_NID_AUTO);
49 static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to)
51 struct file *filp = iocb->ki_filp;
52 struct qrtr_tun *tun = filp->private_data;
53 struct sk_buff *skb;
54 int count;
56 while (!(skb = skb_dequeue(&tun->queue))) {
57 if (filp->f_flags & O_NONBLOCK)
58 return -EAGAIN;
60 /* Wait until we get data or the endpoint goes away */
61 if (wait_event_interruptible(tun->readq,
62 !skb_queue_empty(&tun->queue)))
63 return -ERESTARTSYS;
66 count = min_t(size_t, iov_iter_count(to), skb->len);
67 if (copy_to_iter(skb->data, count, to) != count)
68 count = -EFAULT;
70 kfree_skb(skb);
72 return count;
75 static ssize_t qrtr_tun_write_iter(struct kiocb *iocb, struct iov_iter *from)
77 struct file *filp = iocb->ki_filp;
78 struct qrtr_tun *tun = filp->private_data;
79 size_t len = iov_iter_count(from);
80 ssize_t ret;
81 void *kbuf;
83 kbuf = kzalloc(len, GFP_KERNEL);
84 if (!kbuf)
85 return -ENOMEM;
87 if (!copy_from_iter_full(kbuf, len, from)) {
88 kfree(kbuf);
89 return -EFAULT;
92 ret = qrtr_endpoint_post(&tun->ep, kbuf, len);
94 kfree(kbuf);
95 return ret < 0 ? ret : len;
98 static __poll_t qrtr_tun_poll(struct file *filp, poll_table *wait)
100 struct qrtr_tun *tun = filp->private_data;
101 __poll_t mask = 0;
103 poll_wait(filp, &tun->readq, wait);
105 if (!skb_queue_empty(&tun->queue))
106 mask |= EPOLLIN | EPOLLRDNORM;
108 return mask;
111 static int qrtr_tun_release(struct inode *inode, struct file *filp)
113 struct qrtr_tun *tun = filp->private_data;
115 qrtr_endpoint_unregister(&tun->ep);
117 /* Discard all SKBs */
118 skb_queue_purge(&tun->queue);
120 kfree(tun);
122 return 0;
125 static const struct file_operations qrtr_tun_ops = {
126 .owner = THIS_MODULE,
127 .open = qrtr_tun_open,
128 .poll = qrtr_tun_poll,
129 .read_iter = qrtr_tun_read_iter,
130 .write_iter = qrtr_tun_write_iter,
131 .release = qrtr_tun_release,
134 static struct miscdevice qrtr_tun_miscdev = {
135 MISC_DYNAMIC_MINOR,
136 "qrtr-tun",
137 &qrtr_tun_ops,
140 static int __init qrtr_tun_init(void)
142 int ret;
144 ret = misc_register(&qrtr_tun_miscdev);
145 if (ret)
146 pr_err("failed to register Qualcomm IPC Router tun device\n");
148 return ret;
151 static void __exit qrtr_tun_exit(void)
153 misc_deregister(&qrtr_tun_miscdev);
156 module_init(qrtr_tun_init);
157 module_exit(qrtr_tun_exit);
159 MODULE_DESCRIPTION("Qualcomm IPC Router TUN device");
160 MODULE_LICENSE("GPL v2");