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)  * cdev.c - Character device component for Mostcore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kfifo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/most.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define CHRDEV_REGION_SIZE 50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static struct cdev_component {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	dev_t devno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct ida minor_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	unsigned int major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct most_component cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) } comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) struct comp_channel {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	wait_queue_head_t wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	spinlock_t unlink;	/* synchronization lock to unlink channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct cdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct mutex io_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct most_interface *iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct most_channel_config *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	unsigned int channel_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	dev_t devno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	size_t mbo_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int access_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define to_channel(d) container_of(d, struct comp_channel, cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static struct list_head channel_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static spinlock_t ch_list_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static inline bool ch_has_mbo(struct comp_channel *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	return channel_has_mbo(c->iface, c->channel_id, &comp.cc) > 0;
^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) static inline struct mbo *ch_get_mbo(struct comp_channel *c, struct mbo **mbo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!kfifo_peek(&c->fifo, mbo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		*mbo = most_get_mbo(c->iface, c->channel_id, &comp.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (*mbo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			kfifo_in(&c->fifo, mbo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return *mbo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static struct comp_channel *get_channel(struct most_interface *iface, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct comp_channel *c, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	spin_lock_irqsave(&ch_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	list_for_each_entry_safe(c, tmp, &channel_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		if ((c->iface == iface) && (c->channel_id == id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			spin_unlock_irqrestore(&ch_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	spin_unlock_irqrestore(&ch_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return NULL;
^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 void stop_channel(struct comp_channel *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct mbo *mbo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		most_put_mbo(mbo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	most_stop_channel(c->iface, c->channel_id, &comp.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static void destroy_cdev(struct comp_channel *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	device_destroy(comp.class, c->devno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	cdev_del(&c->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	spin_lock_irqsave(&ch_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	list_del(&c->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	spin_unlock_irqrestore(&ch_list_lock, flags);
^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 void destroy_channel(struct comp_channel *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ida_simple_remove(&comp.minor_id, MINOR(c->devno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	kfifo_free(&c->fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	kfree(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * comp_open - implements the syscall to open the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * @inode: inode pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * @filp: file pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * This stores the channel pointer in the private data field of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * the file structure and activates the channel within the core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int comp_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct comp_channel *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	c = to_channel(inode->i_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	filp->private_data = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (((c->cfg->direction == MOST_CH_RX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	     ((filp->f_flags & O_ACCMODE) != O_RDONLY)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	     ((c->cfg->direction == MOST_CH_TX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		((filp->f_flags & O_ACCMODE) != O_WRONLY))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	mutex_lock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (!c->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (c->access_ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	c->mbo_offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	ret = most_start_channel(c->iface, c->channel_id, &comp.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		c->access_ref = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * comp_close - implements the syscall to close the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * @inode: inode pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * @filp: file pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * This stops the channel within the core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int comp_close(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct comp_channel *c = to_channel(inode->i_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	mutex_lock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	spin_lock(&c->unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	c->access_ref = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	spin_unlock(&c->unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (c->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		stop_channel(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		destroy_channel(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * comp_write - implements the syscall to write to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * @filp: file pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * @buf: pointer to user buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * @count: number of bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * @offset: offset from where to start writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static ssize_t comp_write(struct file *filp, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			  size_t count, loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	size_t to_copy, left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct mbo *mbo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct comp_channel *c = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	mutex_lock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	while (c->dev && !ch_get_mbo(c, &mbo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		if ((filp->f_flags & O_NONBLOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		if (wait_event_interruptible(c->wq, ch_has_mbo(c) || !c->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		mutex_lock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (unlikely(!c->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	to_copy = min(count, c->cfg->buffer_size - c->mbo_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	left = copy_from_user(mbo->virt_address + c->mbo_offs, buf, to_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (left == to_copy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		goto unlock;
^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) 	c->mbo_offs += to_copy - left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (c->mbo_offs >= c->cfg->buffer_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	    c->cfg->data_type == MOST_CH_CONTROL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	    c->cfg->data_type == MOST_CH_ASYNC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		kfifo_skip(&c->fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		mbo->buffer_length = c->mbo_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		c->mbo_offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		most_submit_mbo(mbo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	ret = to_copy - left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * comp_read - implements the syscall to read from the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * @filp: file pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  * @buf: pointer to user buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * @count: number of bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * @offset: offset from where to start reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) comp_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	size_t to_copy, not_copied, copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct mbo *mbo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct comp_channel *c = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	mutex_lock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	while (c->dev && !kfifo_peek(&c->fifo, &mbo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if (filp->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (wait_event_interruptible(c->wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 					     (!kfifo_is_empty(&c->fifo) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 					      (!c->dev))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		mutex_lock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/* make sure we don't submit to gone devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (unlikely(!c->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	to_copy = min_t(size_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			mbo->processed_length - c->mbo_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	not_copied = copy_to_user(buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				  mbo->virt_address + c->mbo_offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				  to_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	copied = to_copy - not_copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	c->mbo_offs += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (c->mbo_offs >= mbo->processed_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		kfifo_skip(&c->fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		most_put_mbo(mbo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		c->mbo_offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static __poll_t comp_poll(struct file *filp, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct comp_channel *c = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	__poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	poll_wait(filp, &c->wq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	mutex_lock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (c->cfg->direction == MOST_CH_RX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		if (!c->dev || !kfifo_is_empty(&c->fifo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (!c->dev || !kfifo_is_empty(&c->fifo) || ch_has_mbo(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			mask |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * Initialization of struct file_operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static const struct file_operations channel_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	.read = comp_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	.write = comp_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	.open = comp_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	.release = comp_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	.poll = comp_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  * comp_disconnect_channel - disconnect a channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  * @iface: pointer to interface instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * @channel_id: channel index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  * This frees allocated memory and removes the cdev that represents this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  * channel in user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int comp_disconnect_channel(struct most_interface *iface, int channel_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct comp_channel *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	c = get_channel(iface, channel_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	mutex_lock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	spin_lock(&c->unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	c->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	spin_unlock(&c->unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	destroy_cdev(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (c->access_ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		stop_channel(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		wake_up_interruptible(&c->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		mutex_unlock(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		destroy_channel(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  * comp_rx_completion - completion handler for rx channels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * @mbo: pointer to buffer object that has completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  * This searches for the channel linked to this MBO and stores it in the local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  * fifo buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static int comp_rx_completion(struct mbo *mbo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	struct comp_channel *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (!mbo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	c = get_channel(mbo->ifp, mbo->hdm_channel_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	spin_lock(&c->unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (!c->access_ref || !c->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		spin_unlock(&c->unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	kfifo_in(&c->fifo, &mbo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	spin_unlock(&c->unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #ifdef DEBUG_MESG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (kfifo_is_full(&c->fifo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		dev_warn(c->dev, "Fifo is full\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	wake_up_interruptible(&c->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  * comp_tx_completion - completion handler for tx channels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)  * @iface: pointer to interface instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  * @channel_id: channel index/ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  * This wakes sleeping processes in the wait-queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) static int comp_tx_completion(struct most_interface *iface, int channel_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	struct comp_channel *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	c = get_channel(iface, channel_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		dev_warn(c->dev, "Channel ID out of range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	wake_up_interruptible(&c->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  * comp_probe - probe function of the driver module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  * @iface: pointer to interface instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  * @channel_id: channel index/ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  * @cfg: pointer to actual channel configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  * @name: name of the device to be created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * This allocates achannel object and creates the device node in /dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * Returns 0 on success or error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static int comp_probe(struct most_interface *iface, int channel_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		      struct most_channel_config *cfg, char *name, char *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	struct comp_channel *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	unsigned long cl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	int current_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (!cfg || !name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	c = get_channel(iface, channel_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	current_minor = ida_simple_get(&comp.minor_id, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (current_minor < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		return current_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	c = kzalloc(sizeof(*c), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (!c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		goto err_remove_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	c->devno = MKDEV(comp.major, current_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	cdev_init(&c->cdev, &channel_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	c->cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	retval = cdev_add(&c->cdev, c->devno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		goto err_free_c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	c->iface = iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	c->cfg = cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	c->channel_id = channel_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	c->access_ref = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	spin_lock_init(&c->unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	INIT_KFIFO(c->fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	retval = kfifo_alloc(&c->fifo, cfg->num_buffers, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		goto err_del_cdev_and_free_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	init_waitqueue_head(&c->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	mutex_init(&c->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	spin_lock_irqsave(&ch_list_lock, cl_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	list_add_tail(&c->list, &channel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	spin_unlock_irqrestore(&ch_list_lock, cl_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	c->dev = device_create(comp.class, NULL, c->devno, NULL, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (IS_ERR(c->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		retval = PTR_ERR(c->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		goto err_free_kfifo_and_del_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	kobject_uevent(&c->dev->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) err_free_kfifo_and_del_list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	kfifo_free(&c->fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	list_del(&c->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) err_del_cdev_and_free_channel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	cdev_del(&c->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) err_free_c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	kfree(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) err_remove_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	ida_simple_remove(&comp.minor_id, current_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static struct cdev_component comp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	.cc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		.mod = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		.name = "cdev",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		.probe_channel = comp_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		.disconnect_channel = comp_disconnect_channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		.rx_completion = comp_rx_completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		.tx_completion = comp_tx_completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) static int __init mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	comp.class = class_create(THIS_MODULE, "most_cdev");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (IS_ERR(comp.class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		return PTR_ERR(comp.class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	INIT_LIST_HEAD(&channel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	spin_lock_init(&ch_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	ida_init(&comp.minor_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	err = alloc_chrdev_region(&comp.devno, 0, CHRDEV_REGION_SIZE, "cdev");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		goto dest_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	comp.major = MAJOR(comp.devno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	err = most_register_component(&comp.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		goto free_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	err = most_register_configfs_subsys(&comp.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		goto deregister_comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) deregister_comp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	most_deregister_component(&comp.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) free_cdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) dest_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	ida_destroy(&comp.minor_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	class_destroy(comp.class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static void __exit mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	struct comp_channel *c, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	most_deregister_configfs_subsys(&comp.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	most_deregister_component(&comp.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	list_for_each_entry_safe(c, tmp, &channel_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		destroy_cdev(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		destroy_channel(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	ida_destroy(&comp.minor_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	class_destroy(comp.class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) module_init(mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) module_exit(mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) MODULE_DESCRIPTION("character device component for mostcore");