Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2016, Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2012, Michal Simek <monstr@monstr.eu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2012, PetaLogix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2011, Texas Instruments, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (c) 2011, Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Based on rpmsg performance statistics driver by Michal Simek, which in turn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * was based on TI & Google OMX rpmsg driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/rpmsg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <uapi/linux/rpmsg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "rpmsg_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define RPMSG_DEV_MAX	(MINORMASK + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static dev_t rpmsg_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static struct class *rpmsg_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static DEFINE_IDA(rpmsg_ctrl_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static DEFINE_IDA(rpmsg_ept_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static DEFINE_IDA(rpmsg_minor_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * struct rpmsg_ctrldev - control device for instantiating endpoint devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * @rpdev:	underlaying rpmsg device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * @cdev:	cdev for the ctrl device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * @dev:	device for the ctrl device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) struct rpmsg_ctrldev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct rpmsg_device *rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct cdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * struct rpmsg_eptdev - endpoint device context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * @dev:	endpoint device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * @cdev:	cdev for the endpoint device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * @rpdev:	underlaying rpmsg device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * @chinfo:	info used to open the endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * @ept_lock:	synchronization of @ept modifications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * @ept:	rpmsg endpoint reference, when open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * @queue_lock:	synchronization of @queue operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * @queue:	incoming message queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * @readq:	wait object for incoming queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) struct rpmsg_eptdev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct cdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct rpmsg_device *rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct rpmsg_channel_info chinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct mutex ept_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct rpmsg_endpoint *ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	spinlock_t queue_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct sk_buff_head queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	wait_queue_head_t readq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static int rpmsg_eptdev_destroy(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	mutex_lock(&eptdev->ept_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (eptdev->ept) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		rpmsg_destroy_ept(eptdev->ept);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		eptdev->ept = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	mutex_unlock(&eptdev->ept_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/* wake up any blocked readers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	wake_up_interruptible(&eptdev->readq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	cdev_device_del(&eptdev->cdev, &eptdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	put_device(&eptdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			void *priv, u32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct rpmsg_eptdev *eptdev = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	skb = alloc_skb(len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	skb_put_data(skb, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	spin_lock(&eptdev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	skb_queue_tail(&eptdev->queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	spin_unlock(&eptdev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/* wake up any blocking processes, waiting for new data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	wake_up_interruptible(&eptdev->readq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct rpmsg_endpoint *ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct rpmsg_device *rpdev = eptdev->rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct device *dev = &eptdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (!ept) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	eptdev->ept = ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	filp->private_data = eptdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int rpmsg_eptdev_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	struct device *dev = &eptdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	/* Close the endpoint, if it's not already destroyed by the parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	mutex_lock(&eptdev->ept_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (eptdev->ept) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		rpmsg_destroy_ept(eptdev->ept);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		eptdev->ept = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	mutex_unlock(&eptdev->ept_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	/* Discard all SKBs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	skb_queue_purge(&eptdev->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static ssize_t rpmsg_eptdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct file *filp = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct rpmsg_eptdev *eptdev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	int use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (!eptdev->ept)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	spin_lock_irqsave(&eptdev->queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	/* Wait for data in the queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (skb_queue_empty(&eptdev->queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		spin_unlock_irqrestore(&eptdev->queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		if (filp->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		/* Wait until we get data or the endpoint goes away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (wait_event_interruptible(eptdev->readq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 					     !skb_queue_empty(&eptdev->queue) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 					     !eptdev->ept))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		/* We lost the endpoint while waiting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (!eptdev->ept)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		spin_lock_irqsave(&eptdev->queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	skb = skb_dequeue(&eptdev->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	spin_unlock_irqrestore(&eptdev->queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	use = min_t(size_t, iov_iter_count(to), skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (copy_to_iter(skb->data, use, to) != use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		use = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static ssize_t rpmsg_eptdev_write_iter(struct kiocb *iocb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				       struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct file *filp = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct rpmsg_eptdev *eptdev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	size_t len = iov_iter_count(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	void *kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	kbuf = kzalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (!kbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (!copy_from_iter_full(kbuf, len, from)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		goto free_kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (mutex_lock_interruptible(&eptdev->ept_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		goto free_kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (!eptdev->ept) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		ret = -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		goto unlock_eptdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (filp->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		ret = rpmsg_trysend(eptdev->ept, kbuf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		ret = rpmsg_send(eptdev->ept, kbuf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) unlock_eptdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	mutex_unlock(&eptdev->ept_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) free_kbuf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	kfree(kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return ret < 0 ? ret : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static __poll_t rpmsg_eptdev_poll(struct file *filp, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct rpmsg_eptdev *eptdev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	__poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (!eptdev->ept)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	poll_wait(filp, &eptdev->readq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (!skb_queue_empty(&eptdev->queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	mask |= rpmsg_poll(eptdev->ept, filp, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			       unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	struct rpmsg_eptdev *eptdev = fp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (cmd != RPMSG_DESTROY_EPT_IOCTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return rpmsg_eptdev_destroy(&eptdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static const struct file_operations rpmsg_eptdev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	.open = rpmsg_eptdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	.release = rpmsg_eptdev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	.read_iter = rpmsg_eptdev_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	.write_iter = rpmsg_eptdev_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.poll = rpmsg_eptdev_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	.unlocked_ioctl = rpmsg_eptdev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static ssize_t name_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return sprintf(buf, "%s\n", eptdev->chinfo.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static DEVICE_ATTR_RO(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static ssize_t src_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return sprintf(buf, "%d\n", eptdev->chinfo.src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static DEVICE_ATTR_RO(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static ssize_t dst_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return sprintf(buf, "%d\n", eptdev->chinfo.dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static DEVICE_ATTR_RO(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static struct attribute *rpmsg_eptdev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	&dev_attr_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	&dev_attr_src.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	&dev_attr_dst.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) ATTRIBUTE_GROUPS(rpmsg_eptdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static void rpmsg_eptdev_release_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	ida_simple_remove(&rpmsg_ept_ida, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	kfree(eptdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			       struct rpmsg_channel_info chinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	struct rpmsg_device *rpdev = ctrldev->rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	struct rpmsg_eptdev *eptdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (!eptdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	dev = &eptdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	eptdev->rpdev = rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	eptdev->chinfo = chinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	mutex_init(&eptdev->ept_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	spin_lock_init(&eptdev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	skb_queue_head_init(&eptdev->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	init_waitqueue_head(&eptdev->readq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	device_initialize(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	dev->class = rpmsg_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	dev->parent = &ctrldev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	dev->groups = rpmsg_eptdev_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	dev_set_drvdata(dev, eptdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	cdev_init(&eptdev->cdev, &rpmsg_eptdev_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	eptdev->cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		goto free_eptdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	ret = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		goto free_minor_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	dev->id = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	dev_set_name(dev, "rpmsg%d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	ret = cdev_device_add(&eptdev->cdev, &eptdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		goto free_ept_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/* We can now rely on the release function for cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	dev->release = rpmsg_eptdev_release_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) free_ept_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	ida_simple_remove(&rpmsg_ept_ida, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) free_minor_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) free_eptdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	kfree(eptdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	get_device(&ctrldev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	filp->private_data = ctrldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	put_device(&ctrldev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 				unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	struct rpmsg_ctrldev *ctrldev = fp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct rpmsg_endpoint_info eptinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct rpmsg_channel_info chinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (cmd != RPMSG_CREATE_EPT_IOCTL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (copy_from_user(&eptinfo, argp, sizeof(eptinfo)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	chinfo.name[RPMSG_NAME_SIZE-1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	chinfo.src = eptinfo.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	chinfo.dst = eptinfo.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	return rpmsg_eptdev_create(ctrldev, chinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static const struct file_operations rpmsg_ctrldev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	.open = rpmsg_ctrldev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	.release = rpmsg_ctrldev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	.unlocked_ioctl = rpmsg_ctrldev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	.compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) static void rpmsg_ctrldev_release_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	kfree(ctrldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct rpmsg_ctrldev *ctrldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (!ctrldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	ctrldev->rpdev = rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	dev = &ctrldev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	device_initialize(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	dev->parent = &rpdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	dev->class = rpmsg_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	cdev_init(&ctrldev->cdev, &rpmsg_ctrldev_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	ctrldev->cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		goto free_ctrldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	ret = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		goto free_minor_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	dev->id = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	ret = cdev_device_add(&ctrldev->cdev, &ctrldev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		goto free_ctrl_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	/* We can now rely on the release function for cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	dev->release = rpmsg_ctrldev_release_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	dev_set_drvdata(&rpdev->dev, ctrldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) free_ctrl_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) free_minor_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) free_ctrldev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	kfree(ctrldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	/* Destroy all endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	ret = device_for_each_child(&ctrldev->dev, NULL, rpmsg_eptdev_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	cdev_device_del(&ctrldev->cdev, &ctrldev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	put_device(&ctrldev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static struct rpmsg_driver rpmsg_chrdev_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	.probe = rpmsg_chrdev_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	.remove = rpmsg_chrdev_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	.drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		.name = "rpmsg_chrdev",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static int rpmsg_char_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		pr_err("rpmsg: failed to allocate char dev region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	rpmsg_class = class_create(THIS_MODULE, "rpmsg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	if (IS_ERR(rpmsg_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		pr_err("failed to create rpmsg class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		return PTR_ERR(rpmsg_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	ret = register_rpmsg_driver(&rpmsg_chrdev_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		pr_err("rpmsgchr: failed to register rpmsg driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		class_destroy(rpmsg_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) postcore_initcall(rpmsg_char_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) static void rpmsg_chrdev_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	unregister_rpmsg_driver(&rpmsg_chrdev_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	class_destroy(rpmsg_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) module_exit(rpmsg_chrdev_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) MODULE_ALIAS("rpmsg:rpmsg_chrdev");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) MODULE_LICENSE("GPL v2");