2017-11-07 21:58:50 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-07-24 22:11:49 +08:00
|
|
|
/*
|
2017-11-21 22:05:12 +08:00
|
|
|
* cdev.c - Character device component for Mostcore
|
2015-07-24 22:11:49 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/cdev.h>
|
2015-09-28 23:18:57 +08:00
|
|
|
#include <linux/poll.h>
|
2015-07-24 22:11:49 +08:00
|
|
|
#include <linux/kfifo.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/idr.h>
|
2020-03-10 21:02:40 +08:00
|
|
|
#include <linux/most.h>
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2018-05-08 17:45:06 +08:00
|
|
|
#define CHRDEV_REGION_SIZE 50
|
|
|
|
|
2017-11-21 22:05:20 +08:00
|
|
|
static struct cdev_component {
|
|
|
|
dev_t devno;
|
|
|
|
struct ida minor_id;
|
|
|
|
unsigned int major;
|
|
|
|
struct class *class;
|
2019-12-13 20:04:15 +08:00
|
|
|
struct most_component cc;
|
2017-11-21 22:05:20 +08:00
|
|
|
} comp;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel {
|
2015-07-24 22:11:49 +08:00
|
|
|
wait_queue_head_t wq;
|
2015-12-22 17:53:04 +08:00
|
|
|
spinlock_t unlink; /* synchronization lock to unlink channels */
|
2015-07-24 22:11:49 +08:00
|
|
|
struct cdev cdev;
|
|
|
|
struct device *dev;
|
|
|
|
struct mutex io_mutex;
|
|
|
|
struct most_interface *iface;
|
|
|
|
struct most_channel_config *cfg;
|
|
|
|
unsigned int channel_id;
|
|
|
|
dev_t devno;
|
2015-12-22 17:52:45 +08:00
|
|
|
size_t mbo_offs;
|
2015-07-24 22:11:49 +08:00
|
|
|
DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
|
2015-12-22 17:53:05 +08:00
|
|
|
int access_ref;
|
2015-07-24 22:11:49 +08:00
|
|
|
struct list_head list;
|
|
|
|
};
|
2015-10-21 23:50:45 +08:00
|
|
|
|
2017-11-21 22:05:08 +08:00
|
|
|
#define to_channel(d) container_of(d, struct comp_channel, cdev)
|
2021-03-29 17:40:15 +08:00
|
|
|
static LIST_HEAD(channel_list);
|
2021-03-29 17:40:14 +08:00
|
|
|
static DEFINE_SPINLOCK(ch_list_lock);
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2017-11-21 22:05:08 +08:00
|
|
|
static inline bool ch_has_mbo(struct comp_channel *c)
|
2016-01-12 21:00:03 +08:00
|
|
|
{
|
2017-11-21 22:05:20 +08:00
|
|
|
return channel_has_mbo(c->iface, c->channel_id, &comp.cc) > 0;
|
2016-01-12 21:00:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-08 17:45:08 +08:00
|
|
|
static inline struct mbo *ch_get_mbo(struct comp_channel *c, struct mbo **mbo)
|
2015-12-22 17:53:04 +08:00
|
|
|
{
|
2016-09-23 21:20:02 +08:00
|
|
|
if (!kfifo_peek(&c->fifo, mbo)) {
|
2017-11-21 22:05:20 +08:00
|
|
|
*mbo = most_get_mbo(c->iface, c->channel_id, &comp.cc);
|
2016-09-23 21:20:02 +08:00
|
|
|
if (*mbo)
|
|
|
|
kfifo_in(&c->fifo, mbo, 1);
|
|
|
|
}
|
2015-12-22 17:53:04 +08:00
|
|
|
return *mbo;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:05:08 +08:00
|
|
|
static struct comp_channel *get_channel(struct most_interface *iface, int id)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c, *tmp;
|
2015-07-24 22:11:49 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&ch_list_lock, flags);
|
2015-12-22 17:53:03 +08:00
|
|
|
list_for_each_entry_safe(c, tmp, &channel_list, list) {
|
|
|
|
if ((c->iface == iface) && (c->channel_id == id)) {
|
2020-06-22 16:48:15 +08:00
|
|
|
spin_unlock_irqrestore(&ch_list_lock, flags);
|
|
|
|
return c;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&ch_list_lock, flags);
|
2020-06-22 16:48:15 +08:00
|
|
|
return NULL;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:05:08 +08:00
|
|
|
static void stop_channel(struct comp_channel *c)
|
2015-12-22 17:53:01 +08:00
|
|
|
{
|
|
|
|
struct mbo *mbo;
|
|
|
|
|
|
|
|
while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1))
|
|
|
|
most_put_mbo(mbo);
|
2017-11-21 22:05:20 +08:00
|
|
|
most_stop_channel(c->iface, c->channel_id, &comp.cc);
|
2015-12-22 17:53:01 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:05:08 +08:00
|
|
|
static void destroy_cdev(struct comp_channel *c)
|
2015-12-22 17:53:01 +08:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2017-11-21 22:05:20 +08:00
|
|
|
device_destroy(comp.class, c->devno);
|
2015-12-22 17:53:01 +08:00
|
|
|
cdev_del(&c->cdev);
|
|
|
|
spin_lock_irqsave(&ch_list_lock, flags);
|
|
|
|
list_del(&c->list);
|
|
|
|
spin_unlock_irqrestore(&ch_list_lock, flags);
|
2017-04-07 21:38:37 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:05:08 +08:00
|
|
|
static void destroy_channel(struct comp_channel *c)
|
2017-04-07 21:38:37 +08:00
|
|
|
{
|
2017-11-21 22:05:20 +08:00
|
|
|
ida_simple_remove(&comp.minor_id, MINOR(c->devno));
|
2017-04-07 21:38:37 +08:00
|
|
|
kfifo_free(&c->fifo);
|
|
|
|
kfree(c);
|
2015-12-22 17:53:01 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 22:11:49 +08:00
|
|
|
/**
|
2017-11-21 22:05:19 +08:00
|
|
|
* comp_open - implements the syscall to open the device
|
2015-07-24 22:11:49 +08:00
|
|
|
* @inode: inode pointer
|
|
|
|
* @filp: file pointer
|
|
|
|
*
|
|
|
|
* This stores the channel pointer in the private data field of
|
|
|
|
* the file structure and activates the channel within the core.
|
|
|
|
*/
|
2017-11-21 22:05:19 +08:00
|
|
|
static int comp_open(struct inode *inode, struct file *filp)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c;
|
2015-07-24 22:11:49 +08:00
|
|
|
int ret;
|
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
c = to_channel(inode->i_cdev);
|
|
|
|
filp->private_data = c;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
if (((c->cfg->direction == MOST_CH_RX) &&
|
2015-10-21 23:50:48 +08:00
|
|
|
((filp->f_flags & O_ACCMODE) != O_RDONLY)) ||
|
2015-12-22 17:53:03 +08:00
|
|
|
((c->cfg->direction == MOST_CH_TX) &&
|
2015-07-24 22:11:49 +08:00
|
|
|
((filp->f_flags & O_ACCMODE) != O_WRONLY))) {
|
|
|
|
return -EACCES;
|
|
|
|
}
|
2015-12-22 17:53:04 +08:00
|
|
|
|
|
|
|
mutex_lock(&c->io_mutex);
|
|
|
|
if (!c->dev) {
|
|
|
|
mutex_unlock(&c->io_mutex);
|
2016-08-19 19:09:33 +08:00
|
|
|
return -ENODEV;
|
2015-12-22 17:53:04 +08:00
|
|
|
}
|
|
|
|
|
2015-12-22 17:53:05 +08:00
|
|
|
if (c->access_ref) {
|
2015-12-22 17:53:04 +08:00
|
|
|
mutex_unlock(&c->io_mutex);
|
2015-07-24 22:11:49 +08:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:53:06 +08:00
|
|
|
c->mbo_offs = 0;
|
2017-11-21 22:05:20 +08:00
|
|
|
ret = most_start_channel(c->iface, c->channel_id, &comp.cc);
|
2015-12-22 17:53:05 +08:00
|
|
|
if (!ret)
|
|
|
|
c->access_ref = 1;
|
2015-12-22 17:53:04 +08:00
|
|
|
mutex_unlock(&c->io_mutex);
|
2015-07-24 22:11:49 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-21 22:05:19 +08:00
|
|
|
* comp_close - implements the syscall to close the device
|
2015-07-24 22:11:49 +08:00
|
|
|
* @inode: inode pointer
|
|
|
|
* @filp: file pointer
|
|
|
|
*
|
|
|
|
* This stops the channel within the core.
|
|
|
|
*/
|
2017-11-21 22:05:19 +08:00
|
|
|
static int comp_close(struct inode *inode, struct file *filp)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c = to_channel(inode->i_cdev);
|
2015-12-22 17:53:03 +08:00
|
|
|
|
|
|
|
mutex_lock(&c->io_mutex);
|
2015-12-22 17:53:04 +08:00
|
|
|
spin_lock(&c->unlink);
|
2015-12-22 17:53:05 +08:00
|
|
|
c->access_ref = 0;
|
2015-12-22 17:53:04 +08:00
|
|
|
spin_unlock(&c->unlink);
|
|
|
|
if (c->dev) {
|
|
|
|
stop_channel(c);
|
2015-12-22 17:53:03 +08:00
|
|
|
mutex_unlock(&c->io_mutex);
|
2015-12-22 17:53:04 +08:00
|
|
|
} else {
|
|
|
|
mutex_unlock(&c->io_mutex);
|
2017-04-07 21:38:37 +08:00
|
|
|
destroy_channel(c);
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
2015-12-22 17:53:01 +08:00
|
|
|
return 0;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-21 22:05:19 +08:00
|
|
|
* comp_write - implements the syscall to write to the device
|
2015-07-24 22:11:49 +08:00
|
|
|
* @filp: file pointer
|
|
|
|
* @buf: pointer to user buffer
|
|
|
|
* @count: number of bytes to write
|
|
|
|
* @offset: offset from where to start writing
|
|
|
|
*/
|
2017-11-21 22:05:19 +08:00
|
|
|
static ssize_t comp_write(struct file *filp, const char __user *buf,
|
|
|
|
size_t count, loff_t *offset)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2015-12-22 17:53:07 +08:00
|
|
|
int ret;
|
2016-09-23 21:20:02 +08:00
|
|
|
size_t to_copy, left;
|
2015-12-22 17:53:04 +08:00
|
|
|
struct mbo *mbo = NULL;
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c = filp->private_data;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
mutex_lock(&c->io_mutex);
|
2015-12-22 17:53:04 +08:00
|
|
|
while (c->dev && !ch_get_mbo(c, &mbo)) {
|
2015-12-22 17:53:03 +08:00
|
|
|
mutex_unlock(&c->io_mutex);
|
2015-07-24 22:11:49 +08:00
|
|
|
|
|
|
|
if ((filp->f_flags & O_NONBLOCK))
|
|
|
|
return -EAGAIN;
|
2015-12-22 17:53:04 +08:00
|
|
|
if (wait_event_interruptible(c->wq, ch_has_mbo(c) || !c->dev))
|
2015-07-24 22:11:49 +08:00
|
|
|
return -ERESTARTSYS;
|
2015-12-22 17:53:04 +08:00
|
|
|
mutex_lock(&c->io_mutex);
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
if (unlikely(!c->dev)) {
|
2016-08-19 19:09:33 +08:00
|
|
|
ret = -ENODEV;
|
2015-12-22 17:53:07 +08:00
|
|
|
goto unlock;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 21:20:02 +08:00
|
|
|
to_copy = min(count, c->cfg->buffer_size - c->mbo_offs);
|
|
|
|
left = copy_from_user(mbo->virt_address + c->mbo_offs, buf, to_copy);
|
|
|
|
if (left == to_copy) {
|
2015-12-22 17:53:07 +08:00
|
|
|
ret = -EFAULT;
|
2016-09-23 21:20:02 +08:00
|
|
|
goto unlock;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 21:20:02 +08:00
|
|
|
c->mbo_offs += to_copy - left;
|
|
|
|
if (c->mbo_offs >= c->cfg->buffer_size ||
|
|
|
|
c->cfg->data_type == MOST_CH_CONTROL ||
|
|
|
|
c->cfg->data_type == MOST_CH_ASYNC) {
|
|
|
|
kfifo_skip(&c->fifo);
|
|
|
|
mbo->buffer_length = c->mbo_offs;
|
|
|
|
c->mbo_offs = 0;
|
|
|
|
most_submit_mbo(mbo);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = to_copy - left;
|
2015-12-22 17:53:07 +08:00
|
|
|
unlock:
|
2015-12-22 17:53:04 +08:00
|
|
|
mutex_unlock(&c->io_mutex);
|
2015-12-22 17:53:07 +08:00
|
|
|
return ret;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-21 22:05:19 +08:00
|
|
|
* comp_read - implements the syscall to read from the device
|
2015-07-24 22:11:49 +08:00
|
|
|
* @filp: file pointer
|
|
|
|
* @buf: pointer to user buffer
|
|
|
|
* @count: number of bytes to read
|
|
|
|
* @offset: offset from where to start reading
|
|
|
|
*/
|
|
|
|
static ssize_t
|
2017-11-21 22:05:19 +08:00
|
|
|
comp_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2015-12-22 17:52:45 +08:00
|
|
|
size_t to_copy, not_copied, copied;
|
2018-05-08 17:45:05 +08:00
|
|
|
struct mbo *mbo = NULL;
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c = filp->private_data;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:53:04 +08:00
|
|
|
mutex_lock(&c->io_mutex);
|
2015-12-22 17:53:06 +08:00
|
|
|
while (c->dev && !kfifo_peek(&c->fifo, &mbo)) {
|
2015-12-22 17:53:04 +08:00
|
|
|
mutex_unlock(&c->io_mutex);
|
2015-07-24 22:11:49 +08:00
|
|
|
if (filp->f_flags & O_NONBLOCK)
|
|
|
|
return -EAGAIN;
|
2015-12-22 17:53:03 +08:00
|
|
|
if (wait_event_interruptible(c->wq,
|
|
|
|
(!kfifo_is_empty(&c->fifo) ||
|
|
|
|
(!c->dev))))
|
2015-07-24 22:11:49 +08:00
|
|
|
return -ERESTARTSYS;
|
2015-12-22 17:53:04 +08:00
|
|
|
mutex_lock(&c->io_mutex);
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure we don't submit to gone devices */
|
2015-12-22 17:53:03 +08:00
|
|
|
if (unlikely(!c->dev)) {
|
|
|
|
mutex_unlock(&c->io_mutex);
|
2016-08-19 19:09:33 +08:00
|
|
|
return -ENODEV;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
2015-12-22 17:52:46 +08:00
|
|
|
to_copy = min_t(size_t,
|
|
|
|
count,
|
2015-12-22 17:53:03 +08:00
|
|
|
mbo->processed_length - c->mbo_offs);
|
2015-07-24 22:11:49 +08:00
|
|
|
|
|
|
|
not_copied = copy_to_user(buf,
|
2015-12-22 17:53:03 +08:00
|
|
|
mbo->virt_address + c->mbo_offs,
|
2015-12-22 17:52:43 +08:00
|
|
|
to_copy);
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:52:44 +08:00
|
|
|
copied = to_copy - not_copied;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
c->mbo_offs += copied;
|
|
|
|
if (c->mbo_offs >= mbo->processed_length) {
|
2015-12-22 17:53:06 +08:00
|
|
|
kfifo_skip(&c->fifo);
|
2015-07-24 22:11:49 +08:00
|
|
|
most_put_mbo(mbo);
|
2015-12-22 17:53:03 +08:00
|
|
|
c->mbo_offs = 0;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
2015-12-22 17:53:03 +08:00
|
|
|
mutex_unlock(&c->io_mutex);
|
2015-12-22 17:52:43 +08:00
|
|
|
return copied;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
Staging/IIO patches for 4.16-rc1
Here is the big Staging and IIO driver patches for 4.16-rc1.
There is the normal amount of new IIO drivers added, like all releases.
The networking IPX and the ncpfs filesystem are moved into the staging
tree, as they are on their way out of the kernel due to lack of use
anymore.
The visorbus subsystem finall has started moving out of the staging tree
to the "real" part of the kernel, and the most and fsl-mc codebases are
almost ready to move out, that will probably happen for 4.17-rc1 if all
goes well.
Other than that, there is a bunch of license header cleanups in the
tree, along with the normal amount of coding style churn that we all
know and love for this codebase. I also got frustrated at the
Meltdown/Spectre mess and took it out on the dgnc tty driver, deleting
huge chunks of it that were never even being used.
Full details of everything is in the shortlog.
All of these patches have been in linux-next for a while with no
reported issues.
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-----BEGIN PGP SIGNATURE-----
iG0EABECAC0WIQT0tgzFv3jCIUoxPcsxR9QN2y37KQUCWnLxoA8cZ3JlZ0Brcm9h
aC5jb20ACgkQMUfUDdst+yk4vgCgjeMlwhtar65DIticIRj626EFxiQAnjGmH8Kd
d9Xz2Piq8X47uSsC/6AE
=xxMT
-----END PGP SIGNATURE-----
Merge tag 'staging-4.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
Pull staging/IIO updates from Greg KH:
"Here is the big Staging and IIO driver patches for 4.16-rc1.
There is the normal amount of new IIO drivers added, like all
releases.
The networking IPX and the ncpfs filesystem are moved into the staging
tree, as they are on their way out of the kernel due to lack of use
anymore.
The visorbus subsystem finall has started moving out of the staging
tree to the "real" part of the kernel, and the most and fsl-mc
codebases are almost ready to move out, that will probably happen for
4.17-rc1 if all goes well.
Other than that, there is a bunch of license header cleanups in the
tree, along with the normal amount of coding style churn that we all
know and love for this codebase. I also got frustrated at the
Meltdown/Spectre mess and took it out on the dgnc tty driver, deleting
huge chunks of it that were never even being used.
Full details of everything is in the shortlog.
All of these patches have been in linux-next for a while with no
reported issues"
* tag 'staging-4.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (627 commits)
staging: rtlwifi: remove redundant initialization of 'cfg_cmd'
staging: rtl8723bs: remove a couple of redundant initializations
staging: comedi: reformat lines to 80 chars or less
staging: lustre: separate a connection destroy from free struct kib_conn
Staging: rtl8723bs: Use !x instead of NULL comparison
Staging: rtl8723bs: Remove dead code
Staging: rtl8723bs: Change names to conform to the kernel code
staging: ccree: Fix missing blank line after declaration
staging: rtl8188eu: remove redundant initialization of 'pwrcfgcmd'
staging: rtlwifi: remove unused RTLHALMAC_ST and RTLPHYDM_ST
staging: fbtft: remove unused FB_TFT_SSD1325 kconfig
staging: comedi: dt2811: remove redundant initialization of 'ns'
staging: wilc1000: fix alignments to match open parenthesis
staging: wilc1000: removed unnecessary defined enums typedef
staging: wilc1000: remove unnecessary use of parentheses
staging: rtl8192u: remove redundant initialization of 'timeout'
staging: sm750fb: fix CamelCase for dispSet var
staging: lustre: lnet/selftest: fix compile error on UP build
staging: rtl8723bs: hal_com_phycfg: Remove unneeded semicolons
staging: rts5208: Fix "seg_no" calculation in reset_ms_card()
...
2018-02-02 01:51:57 +08:00
|
|
|
static __poll_t comp_poll(struct file *filp, poll_table *wait)
|
2015-09-28 23:18:57 +08:00
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c = filp->private_data;
|
2017-07-03 18:39:46 +08:00
|
|
|
__poll_t mask = 0;
|
2015-09-28 23:18:57 +08:00
|
|
|
|
2015-10-28 22:14:35 +08:00
|
|
|
poll_wait(filp, &c->wq, wait);
|
2015-09-28 23:18:57 +08:00
|
|
|
|
2018-05-08 17:45:10 +08:00
|
|
|
mutex_lock(&c->io_mutex);
|
2015-09-28 23:18:57 +08:00
|
|
|
if (c->cfg->direction == MOST_CH_RX) {
|
2018-05-08 17:45:10 +08:00
|
|
|
if (!c->dev || !kfifo_is_empty(&c->fifo))
|
2018-02-12 06:34:03 +08:00
|
|
|
mask |= EPOLLIN | EPOLLRDNORM;
|
2015-09-28 23:18:57 +08:00
|
|
|
} else {
|
2018-05-08 17:45:10 +08:00
|
|
|
if (!c->dev || !kfifo_is_empty(&c->fifo) || ch_has_mbo(c))
|
2018-02-12 06:34:03 +08:00
|
|
|
mask |= EPOLLOUT | EPOLLWRNORM;
|
2015-09-28 23:18:57 +08:00
|
|
|
}
|
2018-05-08 17:45:10 +08:00
|
|
|
mutex_unlock(&c->io_mutex);
|
2015-09-28 23:18:57 +08:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2015-07-24 22:11:49 +08:00
|
|
|
/**
|
|
|
|
* Initialization of struct file_operations
|
|
|
|
*/
|
|
|
|
static const struct file_operations channel_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
2017-11-21 22:05:19 +08:00
|
|
|
.read = comp_read,
|
|
|
|
.write = comp_write,
|
|
|
|
.open = comp_open,
|
|
|
|
.release = comp_close,
|
|
|
|
.poll = comp_poll,
|
2015-07-24 22:11:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-11-21 22:05:19 +08:00
|
|
|
* comp_disconnect_channel - disconnect a channel
|
2015-07-24 22:11:49 +08:00
|
|
|
* @iface: pointer to interface instance
|
|
|
|
* @channel_id: channel index
|
|
|
|
*
|
|
|
|
* This frees allocated memory and removes the cdev that represents this
|
|
|
|
* channel in user space.
|
|
|
|
*/
|
2017-11-21 22:05:19 +08:00
|
|
|
static int comp_disconnect_channel(struct most_interface *iface, int channel_id)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
c = get_channel(iface, channel_id);
|
|
|
|
if (!c)
|
2020-06-22 16:48:14 +08:00
|
|
|
return -EINVAL;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
mutex_lock(&c->io_mutex);
|
2015-12-22 17:53:04 +08:00
|
|
|
spin_lock(&c->unlink);
|
2015-12-22 17:53:03 +08:00
|
|
|
c->dev = NULL;
|
2015-12-22 17:53:04 +08:00
|
|
|
spin_unlock(&c->unlink);
|
2017-04-07 21:38:37 +08:00
|
|
|
destroy_cdev(c);
|
2015-12-22 17:53:05 +08:00
|
|
|
if (c->access_ref) {
|
2015-12-22 17:53:04 +08:00
|
|
|
stop_channel(c);
|
|
|
|
wake_up_interruptible(&c->wq);
|
|
|
|
mutex_unlock(&c->io_mutex);
|
|
|
|
} else {
|
|
|
|
mutex_unlock(&c->io_mutex);
|
2017-04-07 21:38:37 +08:00
|
|
|
destroy_channel(c);
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-21 22:05:19 +08:00
|
|
|
* comp_rx_completion - completion handler for rx channels
|
2015-07-24 22:11:49 +08:00
|
|
|
* @mbo: pointer to buffer object that has completed
|
|
|
|
*
|
|
|
|
* This searches for the channel linked to this MBO and stores it in the local
|
|
|
|
* fifo buffer.
|
|
|
|
*/
|
2017-11-21 22:05:19 +08:00
|
|
|
static int comp_rx_completion(struct mbo *mbo)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
|
|
|
if (!mbo)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
c = get_channel(mbo->ifp, mbo->hdm_channel_id);
|
|
|
|
if (!c)
|
2020-06-22 16:48:14 +08:00
|
|
|
return -EINVAL;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:53:04 +08:00
|
|
|
spin_lock(&c->unlink);
|
2015-12-22 17:53:05 +08:00
|
|
|
if (!c->access_ref || !c->dev) {
|
2015-12-22 17:53:04 +08:00
|
|
|
spin_unlock(&c->unlink);
|
2016-08-19 19:09:33 +08:00
|
|
|
return -ENODEV;
|
2015-12-22 17:53:04 +08:00
|
|
|
}
|
2015-12-22 17:53:03 +08:00
|
|
|
kfifo_in(&c->fifo, &mbo, 1);
|
2015-12-22 17:53:04 +08:00
|
|
|
spin_unlock(&c->unlink);
|
2015-07-24 22:11:49 +08:00
|
|
|
#ifdef DEBUG_MESG
|
2015-12-22 17:53:03 +08:00
|
|
|
if (kfifo_is_full(&c->fifo))
|
2020-06-22 16:48:11 +08:00
|
|
|
dev_warn(c->dev, "Fifo is full\n");
|
2015-07-24 22:11:49 +08:00
|
|
|
#endif
|
2015-12-22 17:53:03 +08:00
|
|
|
wake_up_interruptible(&c->wq);
|
2015-07-24 22:11:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-21 22:05:19 +08:00
|
|
|
* comp_tx_completion - completion handler for tx channels
|
2015-07-24 22:11:49 +08:00
|
|
|
* @iface: pointer to interface instance
|
|
|
|
* @channel_id: channel index/ID
|
|
|
|
*
|
|
|
|
* This wakes sleeping processes in the wait-queue.
|
|
|
|
*/
|
2017-11-21 22:05:19 +08:00
|
|
|
static int comp_tx_completion(struct most_interface *iface, int channel_id)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2020-06-22 16:48:11 +08:00
|
|
|
c = get_channel(iface, channel_id);
|
|
|
|
if (!c)
|
2020-06-22 16:48:14 +08:00
|
|
|
return -EINVAL;
|
2020-06-22 16:48:11 +08:00
|
|
|
|
2015-07-24 22:11:49 +08:00
|
|
|
if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
|
2020-06-22 16:48:11 +08:00
|
|
|
dev_warn(c->dev, "Channel ID out of range\n");
|
2015-07-24 22:11:49 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
wake_up_interruptible(&c->wq);
|
2015-07-24 22:11:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-21 22:05:19 +08:00
|
|
|
* comp_probe - probe function of the driver module
|
2015-07-24 22:11:49 +08:00
|
|
|
* @iface: pointer to interface instance
|
|
|
|
* @channel_id: channel index/ID
|
|
|
|
* @cfg: pointer to actual channel configuration
|
|
|
|
* @name: name of the device to be created
|
|
|
|
*
|
|
|
|
* This allocates achannel object and creates the device node in /dev
|
|
|
|
*
|
|
|
|
* Returns 0 on success or error code otherwise.
|
|
|
|
*/
|
2017-11-21 22:05:19 +08:00
|
|
|
static int comp_probe(struct most_interface *iface, int channel_id,
|
2019-04-03 21:19:45 +08:00
|
|
|
struct most_channel_config *cfg, char *name, char *args)
|
2015-07-24 22:11:49 +08:00
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c;
|
2015-07-24 22:11:49 +08:00
|
|
|
unsigned long cl_flags;
|
|
|
|
int retval;
|
|
|
|
int current_minor;
|
|
|
|
|
2020-06-22 16:48:13 +08:00
|
|
|
if (!cfg || !name)
|
2015-07-24 22:11:49 +08:00
|
|
|
return -EINVAL;
|
2020-06-22 16:48:10 +08:00
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
c = get_channel(iface, channel_id);
|
|
|
|
if (c)
|
2015-07-24 22:11:49 +08:00
|
|
|
return -EEXIST;
|
|
|
|
|
2017-11-21 22:05:20 +08:00
|
|
|
current_minor = ida_simple_get(&comp.minor_id, 0, 0, GFP_KERNEL);
|
2015-07-24 22:11:49 +08:00
|
|
|
if (current_minor < 0)
|
|
|
|
return current_minor;
|
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
c = kzalloc(sizeof(*c), GFP_KERNEL);
|
|
|
|
if (!c) {
|
2015-07-24 22:11:49 +08:00
|
|
|
retval = -ENOMEM;
|
2018-09-21 17:28:51 +08:00
|
|
|
goto err_remove_ida;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:05:20 +08:00
|
|
|
c->devno = MKDEV(comp.major, current_minor);
|
2015-12-22 17:53:03 +08:00
|
|
|
cdev_init(&c->cdev, &channel_fops);
|
|
|
|
c->cdev.owner = THIS_MODULE;
|
2019-02-03 06:34:49 +08:00
|
|
|
retval = cdev_add(&c->cdev, c->devno, 1);
|
|
|
|
if (retval < 0)
|
|
|
|
goto err_free_c;
|
2015-12-22 17:53:03 +08:00
|
|
|
c->iface = iface;
|
|
|
|
c->cfg = cfg;
|
|
|
|
c->channel_id = channel_id;
|
2015-12-22 17:53:05 +08:00
|
|
|
c->access_ref = 0;
|
2015-12-22 17:53:04 +08:00
|
|
|
spin_lock_init(&c->unlink);
|
2015-12-22 17:53:03 +08:00
|
|
|
INIT_KFIFO(c->fifo);
|
|
|
|
retval = kfifo_alloc(&c->fifo, cfg->num_buffers, GFP_KERNEL);
|
2019-07-15 01:27:06 +08:00
|
|
|
if (retval)
|
2018-09-21 17:28:51 +08:00
|
|
|
goto err_del_cdev_and_free_channel;
|
2015-12-22 17:53:03 +08:00
|
|
|
init_waitqueue_head(&c->wq);
|
|
|
|
mutex_init(&c->io_mutex);
|
2015-07-24 22:11:49 +08:00
|
|
|
spin_lock_irqsave(&ch_list_lock, cl_flags);
|
2015-12-22 17:53:03 +08:00
|
|
|
list_add_tail(&c->list, &channel_list);
|
2015-07-24 22:11:49 +08:00
|
|
|
spin_unlock_irqrestore(&ch_list_lock, cl_flags);
|
2017-11-21 22:05:20 +08:00
|
|
|
c->dev = device_create(comp.class, NULL, c->devno, NULL, "%s", name);
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2016-02-09 01:25:34 +08:00
|
|
|
if (IS_ERR(c->dev)) {
|
|
|
|
retval = PTR_ERR(c->dev);
|
2018-09-21 17:28:51 +08:00
|
|
|
goto err_free_kfifo_and_del_list;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
2015-12-22 17:53:03 +08:00
|
|
|
kobject_uevent(&c->dev->kobj, KOBJ_ADD);
|
2015-07-24 22:11:49 +08:00
|
|
|
return 0;
|
|
|
|
|
2018-09-21 17:28:51 +08:00
|
|
|
err_free_kfifo_and_del_list:
|
2015-12-22 17:53:03 +08:00
|
|
|
kfifo_free(&c->fifo);
|
|
|
|
list_del(&c->list);
|
2018-09-21 17:28:51 +08:00
|
|
|
err_del_cdev_and_free_channel:
|
2015-12-22 17:53:03 +08:00
|
|
|
cdev_del(&c->cdev);
|
2019-02-03 06:34:49 +08:00
|
|
|
err_free_c:
|
2015-12-22 17:53:03 +08:00
|
|
|
kfree(c);
|
2018-09-21 17:28:51 +08:00
|
|
|
err_remove_ida:
|
2017-11-21 22:05:20 +08:00
|
|
|
ida_simple_remove(&comp.minor_id, current_minor);
|
2015-07-24 22:11:49 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:05:20 +08:00
|
|
|
static struct cdev_component comp = {
|
|
|
|
.cc = {
|
2019-11-09 00:21:08 +08:00
|
|
|
.mod = THIS_MODULE,
|
2017-11-21 22:05:20 +08:00
|
|
|
.name = "cdev",
|
|
|
|
.probe_channel = comp_probe,
|
|
|
|
.disconnect_channel = comp_disconnect_channel,
|
|
|
|
.rx_completion = comp_rx_completion,
|
|
|
|
.tx_completion = comp_tx_completion,
|
|
|
|
},
|
2015-07-24 22:11:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init mod_init(void)
|
|
|
|
{
|
2016-08-19 19:09:33 +08:00
|
|
|
int err;
|
|
|
|
|
2017-11-21 22:05:20 +08:00
|
|
|
comp.class = class_create(THIS_MODULE, "most_cdev");
|
2020-06-22 16:48:10 +08:00
|
|
|
if (IS_ERR(comp.class))
|
2017-11-21 22:05:20 +08:00
|
|
|
return PTR_ERR(comp.class);
|
|
|
|
|
|
|
|
ida_init(&comp.minor_id);
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2018-05-08 17:45:06 +08:00
|
|
|
err = alloc_chrdev_region(&comp.devno, 0, CHRDEV_REGION_SIZE, "cdev");
|
2016-08-19 19:09:34 +08:00
|
|
|
if (err < 0)
|
2016-08-22 22:46:24 +08:00
|
|
|
goto dest_ida;
|
2017-11-21 22:05:20 +08:00
|
|
|
comp.major = MAJOR(comp.devno);
|
|
|
|
err = most_register_component(&comp.cc);
|
2016-08-19 19:09:33 +08:00
|
|
|
if (err)
|
2017-11-21 22:05:20 +08:00
|
|
|
goto free_cdev;
|
2019-04-03 21:19:48 +08:00
|
|
|
err = most_register_configfs_subsys(&comp.cc);
|
|
|
|
if (err)
|
|
|
|
goto deregister_comp;
|
2015-07-24 22:11:49 +08:00
|
|
|
return 0;
|
|
|
|
|
2019-04-03 21:19:48 +08:00
|
|
|
deregister_comp:
|
|
|
|
most_deregister_component(&comp.cc);
|
2015-07-24 22:11:49 +08:00
|
|
|
free_cdev:
|
2018-05-08 17:45:06 +08:00
|
|
|
unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE);
|
2016-08-22 22:46:24 +08:00
|
|
|
dest_ida:
|
2017-11-21 22:05:20 +08:00
|
|
|
ida_destroy(&comp.minor_id);
|
|
|
|
class_destroy(comp.class);
|
2016-08-19 19:09:33 +08:00
|
|
|
return err;
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit mod_exit(void)
|
|
|
|
{
|
2017-11-21 22:05:08 +08:00
|
|
|
struct comp_channel *c, *tmp;
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2019-04-03 21:19:48 +08:00
|
|
|
most_deregister_configfs_subsys(&comp.cc);
|
2017-11-21 22:05:20 +08:00
|
|
|
most_deregister_component(&comp.cc);
|
2015-07-24 22:11:49 +08:00
|
|
|
|
2015-12-22 17:53:03 +08:00
|
|
|
list_for_each_entry_safe(c, tmp, &channel_list, list) {
|
|
|
|
destroy_cdev(c);
|
2017-04-07 21:38:37 +08:00
|
|
|
destroy_channel(c);
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
2019-04-25 03:23:43 +08:00
|
|
|
unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE);
|
2017-11-21 22:05:20 +08:00
|
|
|
ida_destroy(&comp.minor_id);
|
|
|
|
class_destroy(comp.class);
|
2015-07-24 22:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(mod_init);
|
|
|
|
module_exit(mod_exit);
|
|
|
|
MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|
2017-11-21 22:05:12 +08:00
|
|
|
MODULE_DESCRIPTION("character device component for mostcore");
|