* [PATCH 1/5] rpmsg: smd: Reduce restrictions when finding channel @ 2016-10-08 4:23 ` Bjorn Andersson 0 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: Ohad Ben-Cohen, Bjorn Andersson Cc: Jonathan Corbet, Linus Walleij, Marek Novak, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm SMD channels are created by the remotes in "opening" state, but sometimes as we close and try to reopen them they linger in closing state. Following the search for a matching channel the create_ept() will verify that the channel is in a suitable state, so we can lax the restrictions of the search function to work around above difference in behaviour. Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- drivers/rpmsg/qcom_smd.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/drivers/rpmsg/qcom_smd.c b/drivers/rpmsg/qcom_smd.c index 06fef2b4c814..92efa74a0024 100644 --- a/drivers/rpmsg/qcom_smd.c +++ b/drivers/rpmsg/qcom_smd.c @@ -820,20 +820,13 @@ qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name) struct qcom_smd_channel *channel; struct qcom_smd_channel *ret = NULL; unsigned long flags; - unsigned state; spin_lock_irqsave(&edge->channels_lock, flags); list_for_each_entry(channel, &edge->channels, list) { - if (strcmp(channel->name, name)) - continue; - - state = GET_RX_CHANNEL_INFO(channel, state); - if (state != SMD_CHANNEL_OPENING && - state != SMD_CHANNEL_OPENED) - continue; - - ret = channel; - break; + if (!strcmp(channel->name, name)) { + ret = channel; + break; + } } spin_unlock_irqrestore(&edge->channels_lock, flags); -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 1/5] rpmsg: smd: Reduce restrictions when finding channel @ 2016-10-08 4:23 ` Bjorn Andersson 0 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: linux-arm-kernel SMD channels are created by the remotes in "opening" state, but sometimes as we close and try to reopen them they linger in closing state. Following the search for a matching channel the create_ept() will verify that the channel is in a suitable state, so we can lax the restrictions of the search function to work around above difference in behaviour. Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- drivers/rpmsg/qcom_smd.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/drivers/rpmsg/qcom_smd.c b/drivers/rpmsg/qcom_smd.c index 06fef2b4c814..92efa74a0024 100644 --- a/drivers/rpmsg/qcom_smd.c +++ b/drivers/rpmsg/qcom_smd.c @@ -820,20 +820,13 @@ qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name) struct qcom_smd_channel *channel; struct qcom_smd_channel *ret = NULL; unsigned long flags; - unsigned state; spin_lock_irqsave(&edge->channels_lock, flags); list_for_each_entry(channel, &edge->channels, list) { - if (strcmp(channel->name, name)) - continue; - - state = GET_RX_CHANNEL_INFO(channel, state); - if (state != SMD_CHANNEL_OPENING && - state != SMD_CHANNEL_OPENED) - continue; - - ret = channel; - break; + if (!strcmp(channel->name, name)) { + ret = channel; + break; + } } spin_unlock_irqrestore(&edge->channels_lock, flags); -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 2/5] rpmsg: Introduce a driver override mechanism 2016-10-08 4:23 ` Bjorn Andersson @ 2016-10-08 4:23 ` Bjorn Andersson -1 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: Ohad Ben-Cohen, Bjorn Andersson Cc: Jonathan Corbet, Linus Walleij, Marek Novak, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm Similar to other subsystems it's useful to provide a mechanism to force a specific driver match on a device, so introduce this. Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- drivers/rpmsg/rpmsg_core.c | 3 +++ include/linux/rpmsg.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/drivers/rpmsg/rpmsg_core.c b/drivers/rpmsg/rpmsg_core.c index b6ea9ffa7381..087d4db896c8 100644 --- a/drivers/rpmsg/rpmsg_core.c +++ b/drivers/rpmsg/rpmsg_core.c @@ -315,6 +315,9 @@ static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) const struct rpmsg_device_id *ids = rpdrv->id_table; unsigned int i; + if (rpdev->driver_override) + return !strcmp(rpdev->driver_override, drv->name); + if (ids) for (i = 0; ids[i].name[0]; i++) if (rpmsg_id_match(rpdev, &ids[i])) diff --git a/include/linux/rpmsg.h b/include/linux/rpmsg.h index 452d393cc8dd..7ad6c205f110 100644 --- a/include/linux/rpmsg.h +++ b/include/linux/rpmsg.h @@ -64,6 +64,7 @@ struct rpmsg_channel_info { * rpmsg_device - device that belong to the rpmsg bus * @dev: the device struct * @id: device id (used to match between rpmsg drivers and devices) + * @driver_override: driver name to force a match * @src: local address * @dst: destination address * @ept: the rpmsg endpoint of this channel @@ -72,6 +73,7 @@ struct rpmsg_channel_info { struct rpmsg_device { struct device dev; struct rpmsg_device_id id; + char *driver_override; u32 src; u32 dst; struct rpmsg_endpoint *ept; -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 2/5] rpmsg: Introduce a driver override mechanism @ 2016-10-08 4:23 ` Bjorn Andersson 0 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: linux-arm-kernel Similar to other subsystems it's useful to provide a mechanism to force a specific driver match on a device, so introduce this. Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- drivers/rpmsg/rpmsg_core.c | 3 +++ include/linux/rpmsg.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/drivers/rpmsg/rpmsg_core.c b/drivers/rpmsg/rpmsg_core.c index b6ea9ffa7381..087d4db896c8 100644 --- a/drivers/rpmsg/rpmsg_core.c +++ b/drivers/rpmsg/rpmsg_core.c @@ -315,6 +315,9 @@ static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) const struct rpmsg_device_id *ids = rpdrv->id_table; unsigned int i; + if (rpdev->driver_override) + return !strcmp(rpdev->driver_override, drv->name); + if (ids) for (i = 0; ids[i].name[0]; i++) if (rpmsg_id_match(rpdev, &ids[i])) diff --git a/include/linux/rpmsg.h b/include/linux/rpmsg.h index 452d393cc8dd..7ad6c205f110 100644 --- a/include/linux/rpmsg.h +++ b/include/linux/rpmsg.h @@ -64,6 +64,7 @@ struct rpmsg_channel_info { * rpmsg_device - device that belong to the rpmsg bus * @dev: the device struct * @id: device id (used to match between rpmsg drivers and devices) + * @driver_override: driver name to force a match * @src: local address * @dst: destination address * @ept: the rpmsg endpoint of this channel @@ -72,6 +73,7 @@ struct rpmsg_channel_info { struct rpmsg_device { struct device dev; struct rpmsg_device_id id; + char *driver_override; u32 src; u32 dst; struct rpmsg_endpoint *ept; -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 3/5] rpmsg: Support drivers without primary endpoint 2016-10-08 4:23 ` Bjorn Andersson @ 2016-10-08 4:23 ` Bjorn Andersson -1 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: Ohad Ben-Cohen, Bjorn Andersson Cc: Jonathan Corbet, Linus Walleij, Marek Novak, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm Some types of rpmsg drivers does not have a primary endpoint to tie their existence upon, but wishes to create and destroy endpoints dynamically, e.g. based on user interactions. Allow rpmsg drivers to omit a driver callback to signal this case and make the probe path not create a primary endpoint in this case. Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- drivers/rpmsg/rpmsg_core.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/drivers/rpmsg/rpmsg_core.c b/drivers/rpmsg/rpmsg_core.c index 087d4db896c8..7561941ba413 100644 --- a/drivers/rpmsg/rpmsg_core.c +++ b/drivers/rpmsg/rpmsg_core.c @@ -347,27 +347,30 @@ static int rpmsg_dev_probe(struct device *dev) struct rpmsg_device *rpdev = to_rpmsg_device(dev); struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver); struct rpmsg_channel_info chinfo = {}; - struct rpmsg_endpoint *ept; + struct rpmsg_endpoint *ept = NULL; int err; - strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); - chinfo.src = rpdev->src; - chinfo.dst = RPMSG_ADDR_ANY; + if (rpdrv->callback) { + strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); + chinfo.src = rpdev->src; + chinfo.dst = RPMSG_ADDR_ANY; - ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo); - if (!ept) { - dev_err(dev, "failed to create endpoint\n"); - err = -ENOMEM; - goto out; - } + ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo); + if (!ept) { + dev_err(dev, "failed to create endpoint\n"); + err = -ENOMEM; + goto out; + } - rpdev->ept = ept; - rpdev->src = ept->addr; + rpdev->ept = ept; + rpdev->src = ept->addr; + } err = rpdrv->probe(rpdev); if (err) { dev_err(dev, "%s: failed: %d\n", __func__, err); - rpmsg_destroy_ept(ept); + if (ept) + rpmsg_destroy_ept(ept); goto out; } @@ -388,7 +391,8 @@ static int rpmsg_dev_remove(struct device *dev) rpdrv->remove(rpdev); - rpmsg_destroy_ept(rpdev->ept); + if (rpdev->ept) + rpmsg_destroy_ept(rpdev->ept); return err; } -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 3/5] rpmsg: Support drivers without primary endpoint @ 2016-10-08 4:23 ` Bjorn Andersson 0 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: linux-arm-kernel Some types of rpmsg drivers does not have a primary endpoint to tie their existence upon, but wishes to create and destroy endpoints dynamically, e.g. based on user interactions. Allow rpmsg drivers to omit a driver callback to signal this case and make the probe path not create a primary endpoint in this case. Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- drivers/rpmsg/rpmsg_core.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/drivers/rpmsg/rpmsg_core.c b/drivers/rpmsg/rpmsg_core.c index 087d4db896c8..7561941ba413 100644 --- a/drivers/rpmsg/rpmsg_core.c +++ b/drivers/rpmsg/rpmsg_core.c @@ -347,27 +347,30 @@ static int rpmsg_dev_probe(struct device *dev) struct rpmsg_device *rpdev = to_rpmsg_device(dev); struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver); struct rpmsg_channel_info chinfo = {}; - struct rpmsg_endpoint *ept; + struct rpmsg_endpoint *ept = NULL; int err; - strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); - chinfo.src = rpdev->src; - chinfo.dst = RPMSG_ADDR_ANY; + if (rpdrv->callback) { + strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); + chinfo.src = rpdev->src; + chinfo.dst = RPMSG_ADDR_ANY; - ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo); - if (!ept) { - dev_err(dev, "failed to create endpoint\n"); - err = -ENOMEM; - goto out; - } + ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo); + if (!ept) { + dev_err(dev, "failed to create endpoint\n"); + err = -ENOMEM; + goto out; + } - rpdev->ept = ept; - rpdev->src = ept->addr; + rpdev->ept = ept; + rpdev->src = ept->addr; + } err = rpdrv->probe(rpdev); if (err) { dev_err(dev, "%s: failed: %d\n", __func__, err); - rpmsg_destroy_ept(ept); + if (ept) + rpmsg_destroy_ept(ept); goto out; } @@ -388,7 +391,8 @@ static int rpmsg_dev_remove(struct device *dev) rpdrv->remove(rpdev); - rpmsg_destroy_ept(rpdev->ept); + if (rpdev->ept) + rpmsg_destroy_ept(rpdev->ept); return err; } -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 4/5] rpmsg: Driver for user space endpoint interface 2016-10-08 4:23 ` Bjorn Andersson @ 2016-10-08 4:23 ` Bjorn Andersson -1 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: Ohad Ben-Cohen, Bjorn Andersson Cc: Jonathan Corbet, Linus Walleij, Marek Novak, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm This driver allows rpmsg instances to expose access to rpmsg endpoints to user space processes. It provides a control interface, allowing userspace to export endpoints and an endpoint interface for each exposed endpoint. The implementation is based on prior art by Texas Instrument, Google, PetaLogix and was derived from a FreeRTOS performance statistics driver written by Michal Simek. The control interface provides a "create endpoint" ioctl, which is fed a name, source and destination address. The three values are used to create the endpoint, in a backend-specific way, and a rpmsg endpoint device is created - with the three parameters are available in sysfs for udev usage. E.g. to create an endpoint device for one of the Qualcomm SMD channel related to DIAG one would issue: struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; int fd = open("/dev/rpmsg_ctrl0", O_RDWR); ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); Each created endpoint device shows up as an individual character device in /dev, allowing permission to be controlled on a per-endpoint basis. The rpmsg endpoint will be created and destroyed following the opening and closing of the endpoint device, allowing rpmsg backends to open and close the physical channel, if supported by the wire protocol. Cc: Marek Novak <marek.novak@nxp.com> Cc: Matteo Sartori <matteo.sartori@t3lab.it> Cc: Michal Simek <monstr@monstr.eu> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- Documentation/ioctl/ioctl-number.txt | 1 + drivers/rpmsg/Makefile | 2 +- drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ drivers/rpmsg/rpmsg_internal.h | 2 + include/uapi/linux/rpmsg.h | 35 +++ 5 files changed, 615 insertions(+), 1 deletion(-) create mode 100644 drivers/rpmsg/rpmsg_char.c create mode 100644 include/uapi/linux/rpmsg.h diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt index 81c7f2bb7daf..08244bea5048 100644 --- a/Documentation/ioctl/ioctl-number.txt +++ b/Documentation/ioctl/ioctl-number.txt @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> 0xB3 00 linux/mmc/ioctl.h 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> 0xC0 00-0F linux/usb/iowarrior.h 0xCA 00-0F uapi/misc/cxl.h 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile index ae9c9132cf76..5daf1209b77d 100644 --- a/drivers/rpmsg/Makefile +++ b/drivers/rpmsg/Makefile @@ -1,3 +1,3 @@ -obj-$(CONFIG_RPMSG) += rpmsg_core.o +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c new file mode 100644 index 000000000000..a398a63e8d44 --- /dev/null +++ b/drivers/rpmsg/rpmsg_char.c @@ -0,0 +1,576 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> + * Copyright (c) 2012, PetaLogix + * Copyright (c) 2011, Texas Instruments, Inc. + * Copyright (c) 2011, Google, Inc. + * + * Based on rpmsg performance statistics driver by Michal Simek, which in turn + * was based on TI & Google OMX rpmsg driver. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/rpmsg.h> +#include <linux/skbuff.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <uapi/linux/rpmsg.h> + +#include "rpmsg_internal.h" + +#define RPMSG_DEV_MAX 256 + +static dev_t rpmsg_major; +static struct class *rpmsg_class; + +static DEFINE_IDA(rpmsg_ctrl_ida); +static DEFINE_IDA(rpmsg_ept_ida); +static DEFINE_IDA(rpmsg_minor_ida); + +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev) + +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev) +#define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev) + +struct rpmsg_ctrldev { + struct rpmsg_device *rpdev; + struct cdev cdev; + struct device dev; +}; + +struct rpmsg_eptdev { + struct device dev; + struct cdev cdev; + + struct rpmsg_device *rpdev; + struct rpmsg_channel_info chinfo; + + struct mutex ept_lock; + struct rpmsg_endpoint *ept; + + spinlock_t queue_lock; + struct sk_buff_head queue; + wait_queue_head_t readq; +}; + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); + + +static int rpmsg_cdev_register(struct device *dev, + struct cdev *cdev, + const struct file_operations *fops, + dev_t *assigned_devt) +{ + dev_t devt; + int ret; + + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); + if (ret < 0) + return ret; + + devt = MKDEV(MAJOR(rpmsg_major), ret); + + cdev_init(cdev, fops); + cdev->owner = THIS_MODULE; + ret = cdev_add(cdev, devt, 1); + if (ret < 0) { + dev_err(dev, "cdev_add failed: %d\n", ret); + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); + return ret; + } + + *assigned_devt = devt; + return 0; +} + +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, + void *priv, u32 addr) +{ + struct rpmsg_eptdev *eptdev = priv; + struct sk_buff *skb; + + skb = alloc_skb(len, GFP_ATOMIC); + if (!skb) + return -ENOMEM; + + memcpy(skb_put(skb, len), buf, len); + + spin_lock(&eptdev->queue_lock); + skb_queue_tail(&eptdev->queue, skb); + spin_unlock(&eptdev->queue_lock); + + /* wake up any blocking processes, waiting for new data */ + wake_up_interruptible(&eptdev->readq); + + return 0; +} + +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) +{ + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct rpmsg_endpoint *ept; + struct rpmsg_device *rpdev = eptdev->rpdev; + struct device *dev = &eptdev->dev; + + get_device(dev); + + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); + if (!ept) { + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); + put_device(dev); + return -EINVAL; + } + + eptdev->ept = ept; + filp->private_data = eptdev; + + return 0; +} + +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) +{ + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct device *dev = &eptdev->dev; + struct sk_buff *skb; + + /* Close the endpoint, if it's not already destroyed by the parent */ + if (eptdev->ept) + rpmsg_destroy_ept(eptdev->ept); + + /* Discard all SKBs */ + while (!skb_queue_empty(&eptdev->queue)) { + skb = skb_dequeue(&eptdev->queue); + kfree_skb(skb); + } + + put_device(dev); + + return 0; +} + +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_eptdev *eptdev = fp->private_data; + + if (cmd != RPMSG_DESTROY_EPT_IOCTL) + return -EINVAL; + + return rpmsg_eptdev_destroy(eptdev); +} + +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + unsigned long flags; + struct sk_buff *skb; + int use; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + + /* Wait for data in the queue */ + if (skb_queue_empty(&eptdev->queue)) { + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + if (filp->f_flags & O_NONBLOCK) + return -EAGAIN; + + /* Wait until we get data or the endpoint goes away */ + if (wait_event_interruptible(eptdev->readq, + !skb_queue_empty(&eptdev->queue) || + !eptdev->ept)) + return -ERESTARTSYS; + + /* We lost the endpoint while waiting */ + if (!eptdev->ept) + return -EPIPE; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + } + + skb = skb_dequeue(&eptdev->queue); + if (!skb) + return -EFAULT; + + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + use = min_t(size_t, count, skb->len); + if (copy_to_user(buf, skb->data, use)) + use = -EFAULT; + + kfree_skb(skb); + + return use; +} + +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + void *kbuf; + int ret; + + kbuf = kzalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + if (copy_from_user(kbuf, buf, count)) { + ret = -EFAULT; + goto free_kbuf; + } + + if (mutex_lock_interruptible(&eptdev->ept_lock)) { + ret = -ERESTARTSYS; + goto free_kbuf; + } + + if (!eptdev->ept) { + ret = -EPIPE; + goto unlock_eptdev; + } + + if (filp->f_flags & O_NONBLOCK) + ret = rpmsg_trysend(eptdev->ept, kbuf, count); + else + ret = rpmsg_send(eptdev->ept, kbuf, count); + +unlock_eptdev: + mutex_unlock(&eptdev->ept_lock); + +free_kbuf: + kfree(kbuf); + return ret; +} + +static const struct file_operations rpmsg_eptdev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_eptdev_open, + .release = rpmsg_eptdev_release, + .read = rpmsg_eptdev_read, + .write = rpmsg_eptdev_write, + .unlocked_ioctl = rpmsg_eptdev_ioctl, +}; + +static ssize_t name_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%s\n", eptdev->chinfo.name); +} +static DEVICE_ATTR_RO(name); + +static ssize_t src_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.src); +} +static DEVICE_ATTR_RO(src); + +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.dst); +} +static DEVICE_ATTR_RO(dst); + +static struct attribute *rpmsg_eptdev_attrs[] = { + &dev_attr_name.attr, + &dev_attr_src.attr, + &dev_attr_dst.attr, + NULL +}; +ATTRIBUTE_GROUPS(rpmsg_eptdev); + +static void rpmsg_eptdev_release_device(struct device *dev) +{ + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); + kfree(eptdev); +} + +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, + struct rpmsg_channel_info chinfo) +{ + struct rpmsg_device *rpdev = ctrldev->rpdev; + struct rpmsg_eptdev *eptdev; + struct device *dev; + int ret; + int id; + + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); + if (!eptdev) + return -ENOMEM; + + eptdev->rpdev = rpdev; + eptdev->chinfo = chinfo; + + mutex_init(&eptdev->ept_lock); + spin_lock_init(&eptdev->queue_lock); + skb_queue_head_init(&eptdev->queue); + init_waitqueue_head(&eptdev->readq); + + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(eptdev); + return id; + } + + dev = &eptdev->dev; + device_initialize(dev); + dev->class = rpmsg_class; + dev->id = id; + dev->parent = &ctrldev->dev; + dev->release = rpmsg_eptdev_release_device; + dev->groups = rpmsg_eptdev_groups; + dev_set_name(dev, "rpmsg%d", id); + dev_set_drvdata(dev, eptdev); + + ret = rpmsg_cdev_register(dev, &eptdev->cdev, + &rpmsg_eptdev_fops, &dev->devt); + if (ret) { + dev_err(dev, "cdev_add failed: %d\n", ret); + goto out; + } + + ret = device_add(dev); + if (ret) { + dev_err(dev, "device_register failed: %d\n", ret); + goto out; + } + +out: + if (ret < 0) + put_device(dev); + + return ret; +} + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) +{ + struct rpmsg_endpoint *ept = eptdev->ept; + + mutex_lock(&eptdev->ept_lock); + eptdev->ept = NULL; + mutex_unlock(&eptdev->ept_lock); + + rpmsg_destroy_ept(ept); + + /* wake up any blocking processes */ + wake_up_interruptible(&eptdev->readq); + + cdev_del(&eptdev->cdev); + device_del(&eptdev->dev); + put_device(&eptdev->dev); + + return 0; +} + +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) +{ + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + get_device(&ctrldev->rpdev->dev); + filp->private_data = ctrldev; + + return 0; +} + +static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp) +{ + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + put_device(&ctrldev->rpdev->dev); + + return 0; +} + +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_ctrldev *ctrldev = fp->private_data; + void __user *argp = (void __user *)arg; + struct rpmsg_endpoint_info eptinfo; + struct rpmsg_channel_info chinfo; + + if (cmd != RPMSG_CREATE_EPT_IOCTL) + return -EINVAL; + + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) + return -EFAULT; + + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; + chinfo.src = eptinfo.src; + chinfo.dst = eptinfo.dst; + + return rpmsg_eptdev_create(ctrldev, chinfo); +}; + +static const struct file_operations rpmsg_ctrldev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_ctrldev_open, + .release = rpmsg_ctrldev_release, + .unlocked_ioctl = rpmsg_ctrldev_ioctl, +}; + +static void rpmsg_chrdev_release_device(struct device *dev) +{ + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); + + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); + cdev_del(&ctrldev->cdev); + kfree(ctrldev); +} + +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) +{ + struct rpmsg_ctrldev *ctrldev; + struct device *dev; + int ret; + int id; + + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); + if (!ctrldev) + return -ENOMEM; + + dev = &ctrldev->dev; + + ctrldev->rpdev = rpdev; + + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(ctrldev); + return id; + } + + device_initialize(dev); + dev->parent = &rpdev->dev; + dev->class = rpmsg_class; + dev->release = rpmsg_chrdev_release_device; + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); + + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, + &rpmsg_ctrldev_fops, &dev->devt); + if (ret < 0) { + put_device(dev); + return ret; + } + + ret = device_add(dev); + if (ret) { + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); + put_device(dev); + } + + dev_set_drvdata(&rpdev->dev, ctrldev); + + return ret; +} + +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) +{ + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + return rpmsg_eptdev_destroy(eptdev); +} + +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) +{ + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); + int ret; + + /* Destroy all endpoints */ + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); + if (ret) + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); + + device_del(&ctrldev->dev); + put_device(&ctrldev->dev); +} + +static struct rpmsg_driver rpmsg_chrdev_driver = { + .probe = rpmsg_chrdev_probe, + .remove = rpmsg_chrdev_remove, + .drv = { + .name = "rpmsg_chrdev", + }, +}; + +/** + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev + * @rpdev: prepared rpdev to be used for creating endpoints + * + * This function wraps rpmsg_register_device() preparing the rpdev for use as + * basis for the rpmsg chrdev. + */ +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) +{ + strcpy(rpdev->id.name, "rpmsg_chrdev"); + rpdev->driver_override = "rpmsg_chrdev"; + + return rpmsg_register_device(rpdev); +} +EXPORT_SYMBOL(rpmsg_chrdev_register_device); + +static int rpmsg_char_init(void) +{ + int ret; + + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); + if (ret < 0) { + pr_err("rpmsg: failed to allocate char dev region\n"); + return ret; + } + + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); + if (IS_ERR(rpmsg_class)) { + pr_err("failed to create rpmsg class\n"); + ret = PTR_ERR(rpmsg_class); + goto unregister_chrdev; + } + + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); + if (ret < 0) { + pr_err("rpmsgchr: failed to register rpmsg driver\n"); + goto destroy_class; + } + + return 0; + +destroy_class: + class_destroy(rpmsg_class); + +unregister_chrdev: + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); + + return ret; +} +postcore_initcall(rpmsg_char_init); + +static void rpmsg_chrdev_exit(void) +{ + unregister_rpmsg_driver(&rpmsg_chrdev_driver); + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); +} +module_exit(rpmsg_chrdev_exit); diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h index 8075a20f919b..53d300eacc1c 100644 --- a/drivers/rpmsg/rpmsg_internal.h +++ b/drivers/rpmsg/rpmsg_internal.h @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, struct device *rpmsg_find_device(struct device *parent, struct rpmsg_channel_info *chinfo); +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); + #endif diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h new file mode 100644 index 000000000000..dedc226e0d3f --- /dev/null +++ b/include/uapi/linux/rpmsg.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _UAPI_RPMSG_H_ +#define _UAPI_RPMSG_H_ + +#include <linux/ioctl.h> +#include <linux/types.h> + +/** + * struct rpmsg_endpoint_info - endpoint info representation + * @name: name of service + * @src: local address + * @dst: destination address + */ +struct rpmsg_endpoint_info { + char name[32]; + __u32 src; + __u32 dst; +}; + +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) + +#endif -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 4/5] rpmsg: Driver for user space endpoint interface @ 2016-10-08 4:23 ` Bjorn Andersson 0 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: linux-arm-kernel This driver allows rpmsg instances to expose access to rpmsg endpoints to user space processes. It provides a control interface, allowing userspace to export endpoints and an endpoint interface for each exposed endpoint. The implementation is based on prior art by Texas Instrument, Google, PetaLogix and was derived from a FreeRTOS performance statistics driver written by Michal Simek. The control interface provides a "create endpoint" ioctl, which is fed a name, source and destination address. The three values are used to create the endpoint, in a backend-specific way, and a rpmsg endpoint device is created - with the three parameters are available in sysfs for udev usage. E.g. to create an endpoint device for one of the Qualcomm SMD channel related to DIAG one would issue: struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; int fd = open("/dev/rpmsg_ctrl0", O_RDWR); ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); Each created endpoint device shows up as an individual character device in /dev, allowing permission to be controlled on a per-endpoint basis. The rpmsg endpoint will be created and destroyed following the opening and closing of the endpoint device, allowing rpmsg backends to open and close the physical channel, if supported by the wire protocol. Cc: Marek Novak <marek.novak@nxp.com> Cc: Matteo Sartori <matteo.sartori@t3lab.it> Cc: Michal Simek <monstr@monstr.eu> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- Documentation/ioctl/ioctl-number.txt | 1 + drivers/rpmsg/Makefile | 2 +- drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ drivers/rpmsg/rpmsg_internal.h | 2 + include/uapi/linux/rpmsg.h | 35 +++ 5 files changed, 615 insertions(+), 1 deletion(-) create mode 100644 drivers/rpmsg/rpmsg_char.c create mode 100644 include/uapi/linux/rpmsg.h diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt index 81c7f2bb7daf..08244bea5048 100644 --- a/Documentation/ioctl/ioctl-number.txt +++ b/Documentation/ioctl/ioctl-number.txt @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> 0xB3 00 linux/mmc/ioctl.h 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> 0xC0 00-0F linux/usb/iowarrior.h 0xCA 00-0F uapi/misc/cxl.h 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile index ae9c9132cf76..5daf1209b77d 100644 --- a/drivers/rpmsg/Makefile +++ b/drivers/rpmsg/Makefile @@ -1,3 +1,3 @@ -obj-$(CONFIG_RPMSG) += rpmsg_core.o +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c new file mode 100644 index 000000000000..a398a63e8d44 --- /dev/null +++ b/drivers/rpmsg/rpmsg_char.c @@ -0,0 +1,576 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> + * Copyright (c) 2012, PetaLogix + * Copyright (c) 2011, Texas Instruments, Inc. + * Copyright (c) 2011, Google, Inc. + * + * Based on rpmsg performance statistics driver by Michal Simek, which in turn + * was based on TI & Google OMX rpmsg driver. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/rpmsg.h> +#include <linux/skbuff.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <uapi/linux/rpmsg.h> + +#include "rpmsg_internal.h" + +#define RPMSG_DEV_MAX 256 + +static dev_t rpmsg_major; +static struct class *rpmsg_class; + +static DEFINE_IDA(rpmsg_ctrl_ida); +static DEFINE_IDA(rpmsg_ept_ida); +static DEFINE_IDA(rpmsg_minor_ida); + +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev) + +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev) +#define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev) + +struct rpmsg_ctrldev { + struct rpmsg_device *rpdev; + struct cdev cdev; + struct device dev; +}; + +struct rpmsg_eptdev { + struct device dev; + struct cdev cdev; + + struct rpmsg_device *rpdev; + struct rpmsg_channel_info chinfo; + + struct mutex ept_lock; + struct rpmsg_endpoint *ept; + + spinlock_t queue_lock; + struct sk_buff_head queue; + wait_queue_head_t readq; +}; + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); + + +static int rpmsg_cdev_register(struct device *dev, + struct cdev *cdev, + const struct file_operations *fops, + dev_t *assigned_devt) +{ + dev_t devt; + int ret; + + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); + if (ret < 0) + return ret; + + devt = MKDEV(MAJOR(rpmsg_major), ret); + + cdev_init(cdev, fops); + cdev->owner = THIS_MODULE; + ret = cdev_add(cdev, devt, 1); + if (ret < 0) { + dev_err(dev, "cdev_add failed: %d\n", ret); + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); + return ret; + } + + *assigned_devt = devt; + return 0; +} + +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, + void *priv, u32 addr) +{ + struct rpmsg_eptdev *eptdev = priv; + struct sk_buff *skb; + + skb = alloc_skb(len, GFP_ATOMIC); + if (!skb) + return -ENOMEM; + + memcpy(skb_put(skb, len), buf, len); + + spin_lock(&eptdev->queue_lock); + skb_queue_tail(&eptdev->queue, skb); + spin_unlock(&eptdev->queue_lock); + + /* wake up any blocking processes, waiting for new data */ + wake_up_interruptible(&eptdev->readq); + + return 0; +} + +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) +{ + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct rpmsg_endpoint *ept; + struct rpmsg_device *rpdev = eptdev->rpdev; + struct device *dev = &eptdev->dev; + + get_device(dev); + + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); + if (!ept) { + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); + put_device(dev); + return -EINVAL; + } + + eptdev->ept = ept; + filp->private_data = eptdev; + + return 0; +} + +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) +{ + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct device *dev = &eptdev->dev; + struct sk_buff *skb; + + /* Close the endpoint, if it's not already destroyed by the parent */ + if (eptdev->ept) + rpmsg_destroy_ept(eptdev->ept); + + /* Discard all SKBs */ + while (!skb_queue_empty(&eptdev->queue)) { + skb = skb_dequeue(&eptdev->queue); + kfree_skb(skb); + } + + put_device(dev); + + return 0; +} + +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_eptdev *eptdev = fp->private_data; + + if (cmd != RPMSG_DESTROY_EPT_IOCTL) + return -EINVAL; + + return rpmsg_eptdev_destroy(eptdev); +} + +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + unsigned long flags; + struct sk_buff *skb; + int use; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + + /* Wait for data in the queue */ + if (skb_queue_empty(&eptdev->queue)) { + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + if (filp->f_flags & O_NONBLOCK) + return -EAGAIN; + + /* Wait until we get data or the endpoint goes away */ + if (wait_event_interruptible(eptdev->readq, + !skb_queue_empty(&eptdev->queue) || + !eptdev->ept)) + return -ERESTARTSYS; + + /* We lost the endpoint while waiting */ + if (!eptdev->ept) + return -EPIPE; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + } + + skb = skb_dequeue(&eptdev->queue); + if (!skb) + return -EFAULT; + + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + use = min_t(size_t, count, skb->len); + if (copy_to_user(buf, skb->data, use)) + use = -EFAULT; + + kfree_skb(skb); + + return use; +} + +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + void *kbuf; + int ret; + + kbuf = kzalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + if (copy_from_user(kbuf, buf, count)) { + ret = -EFAULT; + goto free_kbuf; + } + + if (mutex_lock_interruptible(&eptdev->ept_lock)) { + ret = -ERESTARTSYS; + goto free_kbuf; + } + + if (!eptdev->ept) { + ret = -EPIPE; + goto unlock_eptdev; + } + + if (filp->f_flags & O_NONBLOCK) + ret = rpmsg_trysend(eptdev->ept, kbuf, count); + else + ret = rpmsg_send(eptdev->ept, kbuf, count); + +unlock_eptdev: + mutex_unlock(&eptdev->ept_lock); + +free_kbuf: + kfree(kbuf); + return ret; +} + +static const struct file_operations rpmsg_eptdev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_eptdev_open, + .release = rpmsg_eptdev_release, + .read = rpmsg_eptdev_read, + .write = rpmsg_eptdev_write, + .unlocked_ioctl = rpmsg_eptdev_ioctl, +}; + +static ssize_t name_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%s\n", eptdev->chinfo.name); +} +static DEVICE_ATTR_RO(name); + +static ssize_t src_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.src); +} +static DEVICE_ATTR_RO(src); + +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.dst); +} +static DEVICE_ATTR_RO(dst); + +static struct attribute *rpmsg_eptdev_attrs[] = { + &dev_attr_name.attr, + &dev_attr_src.attr, + &dev_attr_dst.attr, + NULL +}; +ATTRIBUTE_GROUPS(rpmsg_eptdev); + +static void rpmsg_eptdev_release_device(struct device *dev) +{ + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); + kfree(eptdev); +} + +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, + struct rpmsg_channel_info chinfo) +{ + struct rpmsg_device *rpdev = ctrldev->rpdev; + struct rpmsg_eptdev *eptdev; + struct device *dev; + int ret; + int id; + + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); + if (!eptdev) + return -ENOMEM; + + eptdev->rpdev = rpdev; + eptdev->chinfo = chinfo; + + mutex_init(&eptdev->ept_lock); + spin_lock_init(&eptdev->queue_lock); + skb_queue_head_init(&eptdev->queue); + init_waitqueue_head(&eptdev->readq); + + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(eptdev); + return id; + } + + dev = &eptdev->dev; + device_initialize(dev); + dev->class = rpmsg_class; + dev->id = id; + dev->parent = &ctrldev->dev; + dev->release = rpmsg_eptdev_release_device; + dev->groups = rpmsg_eptdev_groups; + dev_set_name(dev, "rpmsg%d", id); + dev_set_drvdata(dev, eptdev); + + ret = rpmsg_cdev_register(dev, &eptdev->cdev, + &rpmsg_eptdev_fops, &dev->devt); + if (ret) { + dev_err(dev, "cdev_add failed: %d\n", ret); + goto out; + } + + ret = device_add(dev); + if (ret) { + dev_err(dev, "device_register failed: %d\n", ret); + goto out; + } + +out: + if (ret < 0) + put_device(dev); + + return ret; +} + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) +{ + struct rpmsg_endpoint *ept = eptdev->ept; + + mutex_lock(&eptdev->ept_lock); + eptdev->ept = NULL; + mutex_unlock(&eptdev->ept_lock); + + rpmsg_destroy_ept(ept); + + /* wake up any blocking processes */ + wake_up_interruptible(&eptdev->readq); + + cdev_del(&eptdev->cdev); + device_del(&eptdev->dev); + put_device(&eptdev->dev); + + return 0; +} + +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) +{ + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + get_device(&ctrldev->rpdev->dev); + filp->private_data = ctrldev; + + return 0; +} + +static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp) +{ + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + put_device(&ctrldev->rpdev->dev); + + return 0; +} + +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_ctrldev *ctrldev = fp->private_data; + void __user *argp = (void __user *)arg; + struct rpmsg_endpoint_info eptinfo; + struct rpmsg_channel_info chinfo; + + if (cmd != RPMSG_CREATE_EPT_IOCTL) + return -EINVAL; + + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) + return -EFAULT; + + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; + chinfo.src = eptinfo.src; + chinfo.dst = eptinfo.dst; + + return rpmsg_eptdev_create(ctrldev, chinfo); +}; + +static const struct file_operations rpmsg_ctrldev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_ctrldev_open, + .release = rpmsg_ctrldev_release, + .unlocked_ioctl = rpmsg_ctrldev_ioctl, +}; + +static void rpmsg_chrdev_release_device(struct device *dev) +{ + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); + + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); + cdev_del(&ctrldev->cdev); + kfree(ctrldev); +} + +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) +{ + struct rpmsg_ctrldev *ctrldev; + struct device *dev; + int ret; + int id; + + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); + if (!ctrldev) + return -ENOMEM; + + dev = &ctrldev->dev; + + ctrldev->rpdev = rpdev; + + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(ctrldev); + return id; + } + + device_initialize(dev); + dev->parent = &rpdev->dev; + dev->class = rpmsg_class; + dev->release = rpmsg_chrdev_release_device; + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); + + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, + &rpmsg_ctrldev_fops, &dev->devt); + if (ret < 0) { + put_device(dev); + return ret; + } + + ret = device_add(dev); + if (ret) { + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); + put_device(dev); + } + + dev_set_drvdata(&rpdev->dev, ctrldev); + + return ret; +} + +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) +{ + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + return rpmsg_eptdev_destroy(eptdev); +} + +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) +{ + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); + int ret; + + /* Destroy all endpoints */ + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); + if (ret) + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); + + device_del(&ctrldev->dev); + put_device(&ctrldev->dev); +} + +static struct rpmsg_driver rpmsg_chrdev_driver = { + .probe = rpmsg_chrdev_probe, + .remove = rpmsg_chrdev_remove, + .drv = { + .name = "rpmsg_chrdev", + }, +}; + +/** + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev + * @rpdev: prepared rpdev to be used for creating endpoints + * + * This function wraps rpmsg_register_device() preparing the rpdev for use as + * basis for the rpmsg chrdev. + */ +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) +{ + strcpy(rpdev->id.name, "rpmsg_chrdev"); + rpdev->driver_override = "rpmsg_chrdev"; + + return rpmsg_register_device(rpdev); +} +EXPORT_SYMBOL(rpmsg_chrdev_register_device); + +static int rpmsg_char_init(void) +{ + int ret; + + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); + if (ret < 0) { + pr_err("rpmsg: failed to allocate char dev region\n"); + return ret; + } + + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); + if (IS_ERR(rpmsg_class)) { + pr_err("failed to create rpmsg class\n"); + ret = PTR_ERR(rpmsg_class); + goto unregister_chrdev; + } + + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); + if (ret < 0) { + pr_err("rpmsgchr: failed to register rpmsg driver\n"); + goto destroy_class; + } + + return 0; + +destroy_class: + class_destroy(rpmsg_class); + +unregister_chrdev: + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); + + return ret; +} +postcore_initcall(rpmsg_char_init); + +static void rpmsg_chrdev_exit(void) +{ + unregister_rpmsg_driver(&rpmsg_chrdev_driver); + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); +} +module_exit(rpmsg_chrdev_exit); diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h index 8075a20f919b..53d300eacc1c 100644 --- a/drivers/rpmsg/rpmsg_internal.h +++ b/drivers/rpmsg/rpmsg_internal.h @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, struct device *rpmsg_find_device(struct device *parent, struct rpmsg_channel_info *chinfo); +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); + #endif diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h new file mode 100644 index 000000000000..dedc226e0d3f --- /dev/null +++ b/include/uapi/linux/rpmsg.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _UAPI_RPMSG_H_ +#define _UAPI_RPMSG_H_ + +#include <linux/ioctl.h> +#include <linux/types.h> + +/** + * struct rpmsg_endpoint_info - endpoint info representation + * @name: name of service + * @src: local address + * @dst: destination address + */ +struct rpmsg_endpoint_info { + char name[32]; + __u32 src; + __u32 dst; +}; + +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) + +#endif -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* RE: [PATCH 4/5] rpmsg: Driver for user space endpoint interface 2016-10-08 4:23 ` Bjorn Andersson (?) (?) @ 2016-10-10 9:00 ` Marek Novak -1 siblings, 0 replies; 20+ messages in thread From: Marek Novak @ 2016-10-10 9:00 UTC (permalink / raw) To: Bjorn Andersson, Ohad Ben-Cohen Cc: Jonathan Corbet, Linus Walleij, Matteo Sartori, Michal -----Original Message----- From: Bjorn Andersson [mailto:bjorn.andersson@linaro.org] Sent: Saturday, October 08, 2016 6:23 AM To: Ohad Ben-Cohen <ohad@wizery.com>; Bjorn Andersson <bjorn.andersson@linaro.org> Cc: Jonathan Corbet <corbet@lwn.net>; Linus Walleij <linus.walleij@linaro.org>; Marek Novak <marek.novak@nxp.com>; Matteo Sartori <matteo.sartori@t3lab.it>; Michal Simek <monstr@monstr.eu>; linux-doc@vger.kernel.org; linux-kernel@vger.kernel.org; linux-remoteproc@vger.kernel.org; linux-arm-kernel@lists.infradead.org; linux-arm-msm@vger.kernel.org Subject: [PATCH 4/5] rpmsg: Driver for user space endpoint interface This driver allows rpmsg instances to expose access to rpmsg endpoints to user space processes. It provides a control interface, allowing userspace to export endpoints and an endpoint interface for each exposed endpoint. The implementation is based on prior art by Texas Instrument, Google, PetaLogix and was derived from a FreeRTOS performance statistics driver written by Michal Simek. The control interface provides a "create endpoint" ioctl, which is fed a name, source and destination address. The three values are used to create the endpoint, in a backend-specific way, and a rpmsg endpoint device is created - with the three parameters are available in sysfs for udev usage. E.g. to create an endpoint device for one of the Qualcomm SMD channel related to DIAG one would issue: struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; int fd = open("/dev/rpmsg_ctrl0", O_RDWR); ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); Each created endpoint device shows up as an individual character device in /dev, allowing permission to be controlled on a per-endpoint basis. The rpmsg endpoint will be created and destroyed following the opening and closing of the endpoint device, allowing rpmsg backends to open and close the physical channel, if supported by the wire protocol. Cc: Marek Novak <marek.novak@nxp.com> Cc: Matteo Sartori <matteo.sartori@t3lab.it> Cc: Michal Simek <monstr@monstr.eu> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- Documentation/ioctl/ioctl-number.txt | 1 + drivers/rpmsg/Makefile | 2 +- drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ drivers/rpmsg/rpmsg_internal.h | 2 + include/uapi/linux/rpmsg.h | 35 +++ 5 files changed, 615 insertions(+), 1 deletion(-) create mode 100644 drivers/rpmsg/rpmsg_char.c create mode 100644 include/uapi/linux/rpmsg.h diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt index 81c7f2bb7daf..08244bea5048 100644 --- a/Documentation/ioctl/ioctl-number.txt +++ b/Documentation/ioctl/ioctl-number.txt @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> 0xB3 00 linux/mmc/ioctl.h 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> 0xC0 00-0F linux/usb/iowarrior.h 0xCA 00-0F uapi/misc/cxl.h 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile index ae9c9132cf76..5daf1209b77d 100644 --- a/drivers/rpmsg/Makefile +++ b/drivers/rpmsg/Makefile @@ -1,3 +1,3 @@ -obj-$(CONFIG_RPMSG) += rpmsg_core.o +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c new file mode 100644 index 000000000000..a398a63e8d44 --- /dev/null +++ b/drivers/rpmsg/rpmsg_char.c @@ -0,0 +1,576 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> + * Copyright (c) 2012, PetaLogix + * Copyright (c) 2011, Texas Instruments, Inc. + * Copyright (c) 2011, Google, Inc. + * + * Based on rpmsg performance statistics driver by Michal Simek, which +in turn + * was based on TI & Google OMX rpmsg driver. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/rpmsg.h> +#include <linux/skbuff.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <uapi/linux/rpmsg.h> + +#include "rpmsg_internal.h" + +#define RPMSG_DEV_MAX 256 + +static dev_t rpmsg_major; +static struct class *rpmsg_class; + +static DEFINE_IDA(rpmsg_ctrl_ida); +static DEFINE_IDA(rpmsg_ept_ida); +static DEFINE_IDA(rpmsg_minor_ida); + +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct +rpmsg_eptdev, cdev) + +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, +dev) #define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct +rpmsg_ctrldev, cdev) + +struct rpmsg_ctrldev { + struct rpmsg_device *rpdev; + struct cdev cdev; + struct device dev; +}; + +struct rpmsg_eptdev { + struct device dev; + struct cdev cdev; + + struct rpmsg_device *rpdev; + struct rpmsg_channel_info chinfo; + + struct mutex ept_lock; + struct rpmsg_endpoint *ept; + + spinlock_t queue_lock; + struct sk_buff_head queue; + wait_queue_head_t readq; +}; + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); + + +static int rpmsg_cdev_register(struct device *dev, + struct cdev *cdev, + const struct file_operations *fops, + dev_t *assigned_devt) +{ + dev_t devt; + int ret; + + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); + if (ret < 0) + return ret; + + devt = MKDEV(MAJOR(rpmsg_major), ret); + + cdev_init(cdev, fops); + cdev->owner = THIS_MODULE; + ret = cdev_add(cdev, devt, 1); + if (ret < 0) { + dev_err(dev, "cdev_add failed: %d\n", ret); + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); + return ret; + } + + *assigned_devt = devt; + return 0; +} + +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, + void *priv, u32 addr) +{ + struct rpmsg_eptdev *eptdev = priv; + struct sk_buff *skb; + + skb = alloc_skb(len, GFP_ATOMIC); + if (!skb) + return -ENOMEM; + + memcpy(skb_put(skb, len), buf, len); + + spin_lock(&eptdev->queue_lock); + skb_queue_tail(&eptdev->queue, skb); + spin_unlock(&eptdev->queue_lock); + + /* wake up any blocking processes, waiting for new data */ + wake_up_interruptible(&eptdev->readq); + + return 0; +} + +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) { + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct rpmsg_endpoint *ept; + struct rpmsg_device *rpdev = eptdev->rpdev; + struct device *dev = &eptdev->dev; + + get_device(dev); + + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); + if (!ept) { + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); + put_device(dev); + return -EINVAL; + } + + eptdev->ept = ept; + filp->private_data = eptdev; + + return 0; +} + +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) +{ + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct device *dev = &eptdev->dev; + struct sk_buff *skb; + + /* Close the endpoint, if it's not already destroyed by the parent */ + if (eptdev->ept) + rpmsg_destroy_ept(eptdev->ept); + + /* Discard all SKBs */ + while (!skb_queue_empty(&eptdev->queue)) { + skb = skb_dequeue(&eptdev->queue); + kfree_skb(skb); + } + + put_device(dev); + + return 0; +} + +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_eptdev *eptdev = fp->private_data; + + if (cmd != RPMSG_DESTROY_EPT_IOCTL) + return -EINVAL; + + return rpmsg_eptdev_destroy(eptdev); +} + +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + unsigned long flags; + struct sk_buff *skb; + int use; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + + /* Wait for data in the queue */ + if (skb_queue_empty(&eptdev->queue)) { + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + if (filp->f_flags & O_NONBLOCK) + return -EAGAIN; + + /* Wait until we get data or the endpoint goes away */ + if (wait_event_interruptible(eptdev->readq, + !skb_queue_empty(&eptdev->queue) || + !eptdev->ept)) + return -ERESTARTSYS; + + /* We lost the endpoint while waiting */ + if (!eptdev->ept) + return -EPIPE; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + } + + skb = skb_dequeue(&eptdev->queue); + if (!skb) + return -EFAULT; + + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + use = min_t(size_t, count, skb->len); + if (copy_to_user(buf, skb->data, use)) + use = -EFAULT; + + kfree_skb(skb); + + return use; +} + +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + void *kbuf; + int ret; + + kbuf = kzalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + if (copy_from_user(kbuf, buf, count)) { + ret = -EFAULT; + goto free_kbuf; + } + + if (mutex_lock_interruptible(&eptdev->ept_lock)) { + ret = -ERESTARTSYS; + goto free_kbuf; + } + + if (!eptdev->ept) { + ret = -EPIPE; + goto unlock_eptdev; + } + + if (filp->f_flags & O_NONBLOCK) + ret = rpmsg_trysend(eptdev->ept, kbuf, count); + else + ret = rpmsg_send(eptdev->ept, kbuf, count); + +unlock_eptdev: + mutex_unlock(&eptdev->ept_lock); + +free_kbuf: + kfree(kbuf); + return ret; +} + +static const struct file_operations rpmsg_eptdev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_eptdev_open, + .release = rpmsg_eptdev_release, + .read = rpmsg_eptdev_read, + .write = rpmsg_eptdev_write, + .unlocked_ioctl = rpmsg_eptdev_ioctl, +}; + +static ssize_t name_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%s\n", eptdev->chinfo.name); } static +DEVICE_ATTR_RO(name); + +static ssize_t src_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.src); } static +DEVICE_ATTR_RO(src); + +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.dst); } static +DEVICE_ATTR_RO(dst); + +static struct attribute *rpmsg_eptdev_attrs[] = { + &dev_attr_name.attr, + &dev_attr_src.attr, + &dev_attr_dst.attr, + NULL +}; +ATTRIBUTE_GROUPS(rpmsg_eptdev); + +static void rpmsg_eptdev_release_device(struct device *dev) { + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); + kfree(eptdev); +} + +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, + struct rpmsg_channel_info chinfo) { + struct rpmsg_device *rpdev = ctrldev->rpdev; + struct rpmsg_eptdev *eptdev; + struct device *dev; + int ret; + int id; + + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); + if (!eptdev) + return -ENOMEM; + + eptdev->rpdev = rpdev; + eptdev->chinfo = chinfo; + + mutex_init(&eptdev->ept_lock); + spin_lock_init(&eptdev->queue_lock); + skb_queue_head_init(&eptdev->queue); + init_waitqueue_head(&eptdev->readq); + + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(eptdev); + return id; + } + + dev = &eptdev->dev; + device_initialize(dev); + dev->class = rpmsg_class; + dev->id = id; + dev->parent = &ctrldev->dev; + dev->release = rpmsg_eptdev_release_device; + dev->groups = rpmsg_eptdev_groups; + dev_set_name(dev, "rpmsg%d", id); + dev_set_drvdata(dev, eptdev); + + ret = rpmsg_cdev_register(dev, &eptdev->cdev, + &rpmsg_eptdev_fops, &dev->devt); + if (ret) { + dev_err(dev, "cdev_add failed: %d\n", ret); + goto out; + } + + ret = device_add(dev); + if (ret) { + dev_err(dev, "device_register failed: %d\n", ret); + goto out; + } + +out: + if (ret < 0) + put_device(dev); + + return ret; +} + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) { + struct rpmsg_endpoint *ept = eptdev->ept; + + mutex_lock(&eptdev->ept_lock); + eptdev->ept = NULL; + mutex_unlock(&eptdev->ept_lock); + + rpmsg_destroy_ept(ept); + + /* wake up any blocking processes */ + wake_up_interruptible(&eptdev->readq); + + cdev_del(&eptdev->cdev); + device_del(&eptdev->dev); + put_device(&eptdev->dev); + + return 0; +} + +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) { + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + get_device(&ctrldev->rpdev->dev); + filp->private_data = ctrldev; + + return 0; +} + +static int rpmsg_ctrldev_release(struct inode *inode, struct file +*filp) { + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + put_device(&ctrldev->rpdev->dev); + + return 0; +} + +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_ctrldev *ctrldev = fp->private_data; + void __user *argp = (void __user *)arg; + struct rpmsg_endpoint_info eptinfo; + struct rpmsg_channel_info chinfo; + + if (cmd != RPMSG_CREATE_EPT_IOCTL) + return -EINVAL; + + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) + return -EFAULT; + + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; + chinfo.src = eptinfo.src; + chinfo.dst = eptinfo.dst; + + return rpmsg_eptdev_create(ctrldev, chinfo); }; + +static const struct file_operations rpmsg_ctrldev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_ctrldev_open, + .release = rpmsg_ctrldev_release, + .unlocked_ioctl = rpmsg_ctrldev_ioctl, }; + +static void rpmsg_chrdev_release_device(struct device *dev) { + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); + + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); + cdev_del(&ctrldev->cdev); + kfree(ctrldev); +} + +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) { + struct rpmsg_ctrldev *ctrldev; + struct device *dev; + int ret; + int id; + + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); + if (!ctrldev) + return -ENOMEM; + + dev = &ctrldev->dev; + + ctrldev->rpdev = rpdev; + + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(ctrldev); + return id; + } + + device_initialize(dev); + dev->parent = &rpdev->dev; + dev->class = rpmsg_class; + dev->release = rpmsg_chrdev_release_device; + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); + + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, + &rpmsg_ctrldev_fops, &dev->devt); + if (ret < 0) { + put_device(dev); + return ret; + } + + ret = device_add(dev); + if (ret) { + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); + put_device(dev); + } + + dev_set_drvdata(&rpdev->dev, ctrldev); + + return ret; +} + +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) { + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + return rpmsg_eptdev_destroy(eptdev); +} + +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) { + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); + int ret; + + /* Destroy all endpoints */ + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); + if (ret) + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); + + device_del(&ctrldev->dev); + put_device(&ctrldev->dev); +} + +static struct rpmsg_driver rpmsg_chrdev_driver = { + .probe = rpmsg_chrdev_probe, + .remove = rpmsg_chrdev_remove, + .drv = { + .name = "rpmsg_chrdev", + }, +}; + +/** + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev + * @rpdev: prepared rpdev to be used for creating endpoints + * + * This function wraps rpmsg_register_device() preparing the rpdev for +use as + * basis for the rpmsg chrdev. + */ +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) { + strcpy(rpdev->id.name, "rpmsg_chrdev"); + rpdev->driver_override = "rpmsg_chrdev"; + + return rpmsg_register_device(rpdev); +} +EXPORT_SYMBOL(rpmsg_chrdev_register_device); + +static int rpmsg_char_init(void) +{ + int ret; + + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); + if (ret < 0) { + pr_err("rpmsg: failed to allocate char dev region\n"); + return ret; + } + + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); + if (IS_ERR(rpmsg_class)) { + pr_err("failed to create rpmsg class\n"); + ret = PTR_ERR(rpmsg_class); + goto unregister_chrdev; + } + + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); + if (ret < 0) { + pr_err("rpmsgchr: failed to register rpmsg driver\n"); + goto destroy_class; + } + + return 0; + +destroy_class: + class_destroy(rpmsg_class); + +unregister_chrdev: + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); + + return ret; +} +postcore_initcall(rpmsg_char_init); + +static void rpmsg_chrdev_exit(void) +{ + unregister_rpmsg_driver(&rpmsg_chrdev_driver); + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); } +module_exit(rpmsg_chrdev_exit); diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h index 8075a20f919b..53d300eacc1c 100644 --- a/drivers/rpmsg/rpmsg_internal.h +++ b/drivers/rpmsg/rpmsg_internal.h @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, struct device *rpmsg_find_device(struct device *parent, struct rpmsg_channel_info *chinfo); +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); + #endif diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h new file mode 100644 index 000000000000..dedc226e0d3f --- /dev/null +++ b/include/uapi/linux/rpmsg.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _UAPI_RPMSG_H_ +#define _UAPI_RPMSG_H_ + +#include <linux/ioctl.h> +#include <linux/types.h> + +/** + * struct rpmsg_endpoint_info - endpoint info representation + * @name: name of service + * @src: local address + * @dst: destination address + */ +struct rpmsg_endpoint_info { + char name[32]; + __u32 src; + __u32 dst; +}; + +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) + +#endif -- 2.5.0 ---------------------------------------- Hi Bjorn, Great patch! You managed to simplify what I was trying to accomplish with my patch (https://raw.githubusercontent.com/NXPmicro/rpmsg-sysfs/0aa1817545a765c200b1b2f9b6680a420dcf9171/rpmsg_sysfs_interface.patch ) I am looking forward for your patch being upstreamed! Did you place a pull request? Can you share with me/us a link to your fork, where the patch is applied? Thanks, Marek ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 4/5] rpmsg: Driver for user space endpoint interface @ 2016-10-10 9:00 ` Marek Novak 0 siblings, 0 replies; 20+ messages in thread From: Marek Novak @ 2016-10-10 9:00 UTC (permalink / raw) To: linux-arm-kernel -----Original Message----- From: Bjorn Andersson [mailto:bjorn.andersson at linaro.org] Sent: Saturday, October 08, 2016 6:23 AM To: Ohad Ben-Cohen <ohad@wizery.com>; Bjorn Andersson <bjorn.andersson@linaro.org> Cc: Jonathan Corbet <corbet@lwn.net>; Linus Walleij <linus.walleij@linaro.org>; Marek Novak <marek.novak@nxp.com>; Matteo Sartori <matteo.sartori@t3lab.it>; Michal Simek <monstr@monstr.eu>; linux-doc at vger.kernel.org; linux-kernel at vger.kernel.org; linux-remoteproc at vger.kernel.org; linux-arm-kernel at lists.infradead.org; linux-arm-msm at vger.kernel.org Subject: [PATCH 4/5] rpmsg: Driver for user space endpoint interface This driver allows rpmsg instances to expose access to rpmsg endpoints to user space processes. It provides a control interface, allowing userspace to export endpoints and an endpoint interface for each exposed endpoint. The implementation is based on prior art by Texas Instrument, Google, PetaLogix and was derived from a FreeRTOS performance statistics driver written by Michal Simek. The control interface provides a "create endpoint" ioctl, which is fed a name, source and destination address. The three values are used to create the endpoint, in a backend-specific way, and a rpmsg endpoint device is created - with the three parameters are available in sysfs for udev usage. E.g. to create an endpoint device for one of the Qualcomm SMD channel related to DIAG one would issue: struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; int fd = open("/dev/rpmsg_ctrl0", O_RDWR); ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); Each created endpoint device shows up as an individual character device in /dev, allowing permission to be controlled on a per-endpoint basis. The rpmsg endpoint will be created and destroyed following the opening and closing of the endpoint device, allowing rpmsg backends to open and close the physical channel, if supported by the wire protocol. Cc: Marek Novak <marek.novak@nxp.com> Cc: Matteo Sartori <matteo.sartori@t3lab.it> Cc: Michal Simek <monstr@monstr.eu> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- Documentation/ioctl/ioctl-number.txt | 1 + drivers/rpmsg/Makefile | 2 +- drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ drivers/rpmsg/rpmsg_internal.h | 2 + include/uapi/linux/rpmsg.h | 35 +++ 5 files changed, 615 insertions(+), 1 deletion(-) create mode 100644 drivers/rpmsg/rpmsg_char.c create mode 100644 include/uapi/linux/rpmsg.h diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt index 81c7f2bb7daf..08244bea5048 100644 --- a/Documentation/ioctl/ioctl-number.txt +++ b/Documentation/ioctl/ioctl-number.txt @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> 0xB3 00 linux/mmc/ioctl.h 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> 0xC0 00-0F linux/usb/iowarrior.h 0xCA 00-0F uapi/misc/cxl.h 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile index ae9c9132cf76..5daf1209b77d 100644 --- a/drivers/rpmsg/Makefile +++ b/drivers/rpmsg/Makefile @@ -1,3 +1,3 @@ -obj-$(CONFIG_RPMSG) += rpmsg_core.o +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c new file mode 100644 index 000000000000..a398a63e8d44 --- /dev/null +++ b/drivers/rpmsg/rpmsg_char.c @@ -0,0 +1,576 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> + * Copyright (c) 2012, PetaLogix + * Copyright (c) 2011, Texas Instruments, Inc. + * Copyright (c) 2011, Google, Inc. + * + * Based on rpmsg performance statistics driver by Michal Simek, which +in turn + * was based on TI & Google OMX rpmsg driver. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/rpmsg.h> +#include <linux/skbuff.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <uapi/linux/rpmsg.h> + +#include "rpmsg_internal.h" + +#define RPMSG_DEV_MAX 256 + +static dev_t rpmsg_major; +static struct class *rpmsg_class; + +static DEFINE_IDA(rpmsg_ctrl_ida); +static DEFINE_IDA(rpmsg_ept_ida); +static DEFINE_IDA(rpmsg_minor_ida); + +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct +rpmsg_eptdev, cdev) + +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, +dev) #define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct +rpmsg_ctrldev, cdev) + +struct rpmsg_ctrldev { + struct rpmsg_device *rpdev; + struct cdev cdev; + struct device dev; +}; + +struct rpmsg_eptdev { + struct device dev; + struct cdev cdev; + + struct rpmsg_device *rpdev; + struct rpmsg_channel_info chinfo; + + struct mutex ept_lock; + struct rpmsg_endpoint *ept; + + spinlock_t queue_lock; + struct sk_buff_head queue; + wait_queue_head_t readq; +}; + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); + + +static int rpmsg_cdev_register(struct device *dev, + struct cdev *cdev, + const struct file_operations *fops, + dev_t *assigned_devt) +{ + dev_t devt; + int ret; + + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); + if (ret < 0) + return ret; + + devt = MKDEV(MAJOR(rpmsg_major), ret); + + cdev_init(cdev, fops); + cdev->owner = THIS_MODULE; + ret = cdev_add(cdev, devt, 1); + if (ret < 0) { + dev_err(dev, "cdev_add failed: %d\n", ret); + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); + return ret; + } + + *assigned_devt = devt; + return 0; +} + +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, + void *priv, u32 addr) +{ + struct rpmsg_eptdev *eptdev = priv; + struct sk_buff *skb; + + skb = alloc_skb(len, GFP_ATOMIC); + if (!skb) + return -ENOMEM; + + memcpy(skb_put(skb, len), buf, len); + + spin_lock(&eptdev->queue_lock); + skb_queue_tail(&eptdev->queue, skb); + spin_unlock(&eptdev->queue_lock); + + /* wake up any blocking processes, waiting for new data */ + wake_up_interruptible(&eptdev->readq); + + return 0; +} + +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) { + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct rpmsg_endpoint *ept; + struct rpmsg_device *rpdev = eptdev->rpdev; + struct device *dev = &eptdev->dev; + + get_device(dev); + + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); + if (!ept) { + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); + put_device(dev); + return -EINVAL; + } + + eptdev->ept = ept; + filp->private_data = eptdev; + + return 0; +} + +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) +{ + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct device *dev = &eptdev->dev; + struct sk_buff *skb; + + /* Close the endpoint, if it's not already destroyed by the parent */ + if (eptdev->ept) + rpmsg_destroy_ept(eptdev->ept); + + /* Discard all SKBs */ + while (!skb_queue_empty(&eptdev->queue)) { + skb = skb_dequeue(&eptdev->queue); + kfree_skb(skb); + } + + put_device(dev); + + return 0; +} + +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_eptdev *eptdev = fp->private_data; + + if (cmd != RPMSG_DESTROY_EPT_IOCTL) + return -EINVAL; + + return rpmsg_eptdev_destroy(eptdev); +} + +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + unsigned long flags; + struct sk_buff *skb; + int use; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + + /* Wait for data in the queue */ + if (skb_queue_empty(&eptdev->queue)) { + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + if (filp->f_flags & O_NONBLOCK) + return -EAGAIN; + + /* Wait until we get data or the endpoint goes away */ + if (wait_event_interruptible(eptdev->readq, + !skb_queue_empty(&eptdev->queue) || + !eptdev->ept)) + return -ERESTARTSYS; + + /* We lost the endpoint while waiting */ + if (!eptdev->ept) + return -EPIPE; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + } + + skb = skb_dequeue(&eptdev->queue); + if (!skb) + return -EFAULT; + + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + use = min_t(size_t, count, skb->len); + if (copy_to_user(buf, skb->data, use)) + use = -EFAULT; + + kfree_skb(skb); + + return use; +} + +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + void *kbuf; + int ret; + + kbuf = kzalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + if (copy_from_user(kbuf, buf, count)) { + ret = -EFAULT; + goto free_kbuf; + } + + if (mutex_lock_interruptible(&eptdev->ept_lock)) { + ret = -ERESTARTSYS; + goto free_kbuf; + } + + if (!eptdev->ept) { + ret = -EPIPE; + goto unlock_eptdev; + } + + if (filp->f_flags & O_NONBLOCK) + ret = rpmsg_trysend(eptdev->ept, kbuf, count); + else + ret = rpmsg_send(eptdev->ept, kbuf, count); + +unlock_eptdev: + mutex_unlock(&eptdev->ept_lock); + +free_kbuf: + kfree(kbuf); + return ret; +} + +static const struct file_operations rpmsg_eptdev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_eptdev_open, + .release = rpmsg_eptdev_release, + .read = rpmsg_eptdev_read, + .write = rpmsg_eptdev_write, + .unlocked_ioctl = rpmsg_eptdev_ioctl, +}; + +static ssize_t name_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%s\n", eptdev->chinfo.name); } static +DEVICE_ATTR_RO(name); + +static ssize_t src_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.src); } static +DEVICE_ATTR_RO(src); + +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.dst); } static +DEVICE_ATTR_RO(dst); + +static struct attribute *rpmsg_eptdev_attrs[] = { + &dev_attr_name.attr, + &dev_attr_src.attr, + &dev_attr_dst.attr, + NULL +}; +ATTRIBUTE_GROUPS(rpmsg_eptdev); + +static void rpmsg_eptdev_release_device(struct device *dev) { + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); + kfree(eptdev); +} + +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, + struct rpmsg_channel_info chinfo) { + struct rpmsg_device *rpdev = ctrldev->rpdev; + struct rpmsg_eptdev *eptdev; + struct device *dev; + int ret; + int id; + + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); + if (!eptdev) + return -ENOMEM; + + eptdev->rpdev = rpdev; + eptdev->chinfo = chinfo; + + mutex_init(&eptdev->ept_lock); + spin_lock_init(&eptdev->queue_lock); + skb_queue_head_init(&eptdev->queue); + init_waitqueue_head(&eptdev->readq); + + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(eptdev); + return id; + } + + dev = &eptdev->dev; + device_initialize(dev); + dev->class = rpmsg_class; + dev->id = id; + dev->parent = &ctrldev->dev; + dev->release = rpmsg_eptdev_release_device; + dev->groups = rpmsg_eptdev_groups; + dev_set_name(dev, "rpmsg%d", id); + dev_set_drvdata(dev, eptdev); + + ret = rpmsg_cdev_register(dev, &eptdev->cdev, + &rpmsg_eptdev_fops, &dev->devt); + if (ret) { + dev_err(dev, "cdev_add failed: %d\n", ret); + goto out; + } + + ret = device_add(dev); + if (ret) { + dev_err(dev, "device_register failed: %d\n", ret); + goto out; + } + +out: + if (ret < 0) + put_device(dev); + + return ret; +} + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) { + struct rpmsg_endpoint *ept = eptdev->ept; + + mutex_lock(&eptdev->ept_lock); + eptdev->ept = NULL; + mutex_unlock(&eptdev->ept_lock); + + rpmsg_destroy_ept(ept); + + /* wake up any blocking processes */ + wake_up_interruptible(&eptdev->readq); + + cdev_del(&eptdev->cdev); + device_del(&eptdev->dev); + put_device(&eptdev->dev); + + return 0; +} + +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) { + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + get_device(&ctrldev->rpdev->dev); + filp->private_data = ctrldev; + + return 0; +} + +static int rpmsg_ctrldev_release(struct inode *inode, struct file +*filp) { + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + put_device(&ctrldev->rpdev->dev); + + return 0; +} + +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_ctrldev *ctrldev = fp->private_data; + void __user *argp = (void __user *)arg; + struct rpmsg_endpoint_info eptinfo; + struct rpmsg_channel_info chinfo; + + if (cmd != RPMSG_CREATE_EPT_IOCTL) + return -EINVAL; + + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) + return -EFAULT; + + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; + chinfo.src = eptinfo.src; + chinfo.dst = eptinfo.dst; + + return rpmsg_eptdev_create(ctrldev, chinfo); }; + +static const struct file_operations rpmsg_ctrldev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_ctrldev_open, + .release = rpmsg_ctrldev_release, + .unlocked_ioctl = rpmsg_ctrldev_ioctl, }; + +static void rpmsg_chrdev_release_device(struct device *dev) { + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); + + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); + cdev_del(&ctrldev->cdev); + kfree(ctrldev); +} + +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) { + struct rpmsg_ctrldev *ctrldev; + struct device *dev; + int ret; + int id; + + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); + if (!ctrldev) + return -ENOMEM; + + dev = &ctrldev->dev; + + ctrldev->rpdev = rpdev; + + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(ctrldev); + return id; + } + + device_initialize(dev); + dev->parent = &rpdev->dev; + dev->class = rpmsg_class; + dev->release = rpmsg_chrdev_release_device; + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); + + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, + &rpmsg_ctrldev_fops, &dev->devt); + if (ret < 0) { + put_device(dev); + return ret; + } + + ret = device_add(dev); + if (ret) { + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); + put_device(dev); + } + + dev_set_drvdata(&rpdev->dev, ctrldev); + + return ret; +} + +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) { + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + return rpmsg_eptdev_destroy(eptdev); +} + +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) { + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); + int ret; + + /* Destroy all endpoints */ + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); + if (ret) + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); + + device_del(&ctrldev->dev); + put_device(&ctrldev->dev); +} + +static struct rpmsg_driver rpmsg_chrdev_driver = { + .probe = rpmsg_chrdev_probe, + .remove = rpmsg_chrdev_remove, + .drv = { + .name = "rpmsg_chrdev", + }, +}; + +/** + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev + * @rpdev: prepared rpdev to be used for creating endpoints + * + * This function wraps rpmsg_register_device() preparing the rpdev for +use as + * basis for the rpmsg chrdev. + */ +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) { + strcpy(rpdev->id.name, "rpmsg_chrdev"); + rpdev->driver_override = "rpmsg_chrdev"; + + return rpmsg_register_device(rpdev); +} +EXPORT_SYMBOL(rpmsg_chrdev_register_device); + +static int rpmsg_char_init(void) +{ + int ret; + + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); + if (ret < 0) { + pr_err("rpmsg: failed to allocate char dev region\n"); + return ret; + } + + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); + if (IS_ERR(rpmsg_class)) { + pr_err("failed to create rpmsg class\n"); + ret = PTR_ERR(rpmsg_class); + goto unregister_chrdev; + } + + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); + if (ret < 0) { + pr_err("rpmsgchr: failed to register rpmsg driver\n"); + goto destroy_class; + } + + return 0; + +destroy_class: + class_destroy(rpmsg_class); + +unregister_chrdev: + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); + + return ret; +} +postcore_initcall(rpmsg_char_init); + +static void rpmsg_chrdev_exit(void) +{ + unregister_rpmsg_driver(&rpmsg_chrdev_driver); + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); } +module_exit(rpmsg_chrdev_exit); diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h index 8075a20f919b..53d300eacc1c 100644 --- a/drivers/rpmsg/rpmsg_internal.h +++ b/drivers/rpmsg/rpmsg_internal.h @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, struct device *rpmsg_find_device(struct device *parent, struct rpmsg_channel_info *chinfo); +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); + #endif diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h new file mode 100644 index 000000000000..dedc226e0d3f --- /dev/null +++ b/include/uapi/linux/rpmsg.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _UAPI_RPMSG_H_ +#define _UAPI_RPMSG_H_ + +#include <linux/ioctl.h> +#include <linux/types.h> + +/** + * struct rpmsg_endpoint_info - endpoint info representation + * @name: name of service + * @src: local address + * @dst: destination address + */ +struct rpmsg_endpoint_info { + char name[32]; + __u32 src; + __u32 dst; +}; + +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) + +#endif -- 2.5.0 ---------------------------------------- Hi Bjorn, Great patch! You managed to simplify what I was trying to accomplish with my patch (https://raw.githubusercontent.com/NXPmicro/rpmsg-sysfs/0aa1817545a765c200b1b2f9b6680a420dcf9171/rpmsg_sysfs_interface.patch ) I am looking forward for your patch being upstreamed! Did you place a pull request? Can you share with me/us a link to your fork, where the patch is applied? Thanks, Marek ^ permalink raw reply related [flat|nested] 20+ messages in thread
* RE: [PATCH 4/5] rpmsg: Driver for user space endpoint interface @ 2016-10-10 9:00 ` Marek Novak 0 siblings, 0 replies; 20+ messages in thread From: Marek Novak @ 2016-10-10 9:00 UTC (permalink / raw) To: Bjorn Andersson, Ohad Ben-Cohen Cc: Jonathan Corbet, Linus Walleij, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm -----Original Message----- From: Bjorn Andersson [mailto:bjorn.andersson@linaro.org] Sent: Saturday, October 08, 2016 6:23 AM To: Ohad Ben-Cohen <ohad@wizery.com>; Bjorn Andersson <bjorn.andersson@linaro.org> Cc: Jonathan Corbet <corbet@lwn.net>; Linus Walleij <linus.walleij@linaro.org>; Marek Novak <marek.novak@nxp.com>; Matteo Sartori <matteo.sartori@t3lab.it>; Michal Simek <monstr@monstr.eu>; linux-doc@vger.kernel.org; linux-kernel@vger.kernel.org; linux-remoteproc@vger.kernel.org; linux-arm-kernel@lists.infradead.org; linux-arm-msm@vger.kernel.org Subject: [PATCH 4/5] rpmsg: Driver for user space endpoint interface This driver allows rpmsg instances to expose access to rpmsg endpoints to user space processes. It provides a control interface, allowing userspace to export endpoints and an endpoint interface for each exposed endpoint. The implementation is based on prior art by Texas Instrument, Google, PetaLogix and was derived from a FreeRTOS performance statistics driver written by Michal Simek. The control interface provides a "create endpoint" ioctl, which is fed a name, source and destination address. The three values are used to create the endpoint, in a backend-specific way, and a rpmsg endpoint device is created - with the three parameters are available in sysfs for udev usage. E.g. to create an endpoint device for one of the Qualcomm SMD channel related to DIAG one would issue: struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; int fd = open("/dev/rpmsg_ctrl0", O_RDWR); ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); Each created endpoint device shows up as an individual character device in /dev, allowing permission to be controlled on a per-endpoint basis. The rpmsg endpoint will be created and destroyed following the opening and closing of the endpoint device, allowing rpmsg backends to open and close the physical channel, if supported by the wire protocol. Cc: Marek Novak <marek.novak@nxp.com> Cc: Matteo Sartori <matteo.sartori@t3lab.it> Cc: Michal Simek <monstr@monstr.eu> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- Documentation/ioctl/ioctl-number.txt | 1 + drivers/rpmsg/Makefile | 2 +- drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ drivers/rpmsg/rpmsg_internal.h | 2 + include/uapi/linux/rpmsg.h | 35 +++ 5 files changed, 615 insertions(+), 1 deletion(-) create mode 100644 drivers/rpmsg/rpmsg_char.c create mode 100644 include/uapi/linux/rpmsg.h diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt index 81c7f2bb7daf..08244bea5048 100644 --- a/Documentation/ioctl/ioctl-number.txt +++ b/Documentation/ioctl/ioctl-number.txt @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> 0xB3 00 linux/mmc/ioctl.h 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> 0xC0 00-0F linux/usb/iowarrior.h 0xCA 00-0F uapi/misc/cxl.h 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile index ae9c9132cf76..5daf1209b77d 100644 --- a/drivers/rpmsg/Makefile +++ b/drivers/rpmsg/Makefile @@ -1,3 +1,3 @@ -obj-$(CONFIG_RPMSG) += rpmsg_core.o +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c new file mode 100644 index 000000000000..a398a63e8d44 --- /dev/null +++ b/drivers/rpmsg/rpmsg_char.c @@ -0,0 +1,576 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> + * Copyright (c) 2012, PetaLogix + * Copyright (c) 2011, Texas Instruments, Inc. + * Copyright (c) 2011, Google, Inc. + * + * Based on rpmsg performance statistics driver by Michal Simek, which +in turn + * was based on TI & Google OMX rpmsg driver. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/rpmsg.h> +#include <linux/skbuff.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <uapi/linux/rpmsg.h> + +#include "rpmsg_internal.h" + +#define RPMSG_DEV_MAX 256 + +static dev_t rpmsg_major; +static struct class *rpmsg_class; + +static DEFINE_IDA(rpmsg_ctrl_ida); +static DEFINE_IDA(rpmsg_ept_ida); +static DEFINE_IDA(rpmsg_minor_ida); + +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct +rpmsg_eptdev, cdev) + +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, +dev) #define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct +rpmsg_ctrldev, cdev) + +struct rpmsg_ctrldev { + struct rpmsg_device *rpdev; + struct cdev cdev; + struct device dev; +}; + +struct rpmsg_eptdev { + struct device dev; + struct cdev cdev; + + struct rpmsg_device *rpdev; + struct rpmsg_channel_info chinfo; + + struct mutex ept_lock; + struct rpmsg_endpoint *ept; + + spinlock_t queue_lock; + struct sk_buff_head queue; + wait_queue_head_t readq; +}; + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); + + +static int rpmsg_cdev_register(struct device *dev, + struct cdev *cdev, + const struct file_operations *fops, + dev_t *assigned_devt) +{ + dev_t devt; + int ret; + + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); + if (ret < 0) + return ret; + + devt = MKDEV(MAJOR(rpmsg_major), ret); + + cdev_init(cdev, fops); + cdev->owner = THIS_MODULE; + ret = cdev_add(cdev, devt, 1); + if (ret < 0) { + dev_err(dev, "cdev_add failed: %d\n", ret); + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); + return ret; + } + + *assigned_devt = devt; + return 0; +} + +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, + void *priv, u32 addr) +{ + struct rpmsg_eptdev *eptdev = priv; + struct sk_buff *skb; + + skb = alloc_skb(len, GFP_ATOMIC); + if (!skb) + return -ENOMEM; + + memcpy(skb_put(skb, len), buf, len); + + spin_lock(&eptdev->queue_lock); + skb_queue_tail(&eptdev->queue, skb); + spin_unlock(&eptdev->queue_lock); + + /* wake up any blocking processes, waiting for new data */ + wake_up_interruptible(&eptdev->readq); + + return 0; +} + +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) { + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct rpmsg_endpoint *ept; + struct rpmsg_device *rpdev = eptdev->rpdev; + struct device *dev = &eptdev->dev; + + get_device(dev); + + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); + if (!ept) { + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); + put_device(dev); + return -EINVAL; + } + + eptdev->ept = ept; + filp->private_data = eptdev; + + return 0; +} + +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) +{ + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct device *dev = &eptdev->dev; + struct sk_buff *skb; + + /* Close the endpoint, if it's not already destroyed by the parent */ + if (eptdev->ept) + rpmsg_destroy_ept(eptdev->ept); + + /* Discard all SKBs */ + while (!skb_queue_empty(&eptdev->queue)) { + skb = skb_dequeue(&eptdev->queue); + kfree_skb(skb); + } + + put_device(dev); + + return 0; +} + +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_eptdev *eptdev = fp->private_data; + + if (cmd != RPMSG_DESTROY_EPT_IOCTL) + return -EINVAL; + + return rpmsg_eptdev_destroy(eptdev); +} + +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + unsigned long flags; + struct sk_buff *skb; + int use; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + + /* Wait for data in the queue */ + if (skb_queue_empty(&eptdev->queue)) { + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + if (filp->f_flags & O_NONBLOCK) + return -EAGAIN; + + /* Wait until we get data or the endpoint goes away */ + if (wait_event_interruptible(eptdev->readq, + !skb_queue_empty(&eptdev->queue) || + !eptdev->ept)) + return -ERESTARTSYS; + + /* We lost the endpoint while waiting */ + if (!eptdev->ept) + return -EPIPE; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + } + + skb = skb_dequeue(&eptdev->queue); + if (!skb) + return -EFAULT; + + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + use = min_t(size_t, count, skb->len); + if (copy_to_user(buf, skb->data, use)) + use = -EFAULT; + + kfree_skb(skb); + + return use; +} + +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + void *kbuf; + int ret; + + kbuf = kzalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + if (copy_from_user(kbuf, buf, count)) { + ret = -EFAULT; + goto free_kbuf; + } + + if (mutex_lock_interruptible(&eptdev->ept_lock)) { + ret = -ERESTARTSYS; + goto free_kbuf; + } + + if (!eptdev->ept) { + ret = -EPIPE; + goto unlock_eptdev; + } + + if (filp->f_flags & O_NONBLOCK) + ret = rpmsg_trysend(eptdev->ept, kbuf, count); + else + ret = rpmsg_send(eptdev->ept, kbuf, count); + +unlock_eptdev: + mutex_unlock(&eptdev->ept_lock); + +free_kbuf: + kfree(kbuf); + return ret; +} + +static const struct file_operations rpmsg_eptdev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_eptdev_open, + .release = rpmsg_eptdev_release, + .read = rpmsg_eptdev_read, + .write = rpmsg_eptdev_write, + .unlocked_ioctl = rpmsg_eptdev_ioctl, +}; + +static ssize_t name_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%s\n", eptdev->chinfo.name); } static +DEVICE_ATTR_RO(name); + +static ssize_t src_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.src); } static +DEVICE_ATTR_RO(src); + +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.dst); } static +DEVICE_ATTR_RO(dst); + +static struct attribute *rpmsg_eptdev_attrs[] = { + &dev_attr_name.attr, + &dev_attr_src.attr, + &dev_attr_dst.attr, + NULL +}; +ATTRIBUTE_GROUPS(rpmsg_eptdev); + +static void rpmsg_eptdev_release_device(struct device *dev) { + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); + kfree(eptdev); +} + +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, + struct rpmsg_channel_info chinfo) { + struct rpmsg_device *rpdev = ctrldev->rpdev; + struct rpmsg_eptdev *eptdev; + struct device *dev; + int ret; + int id; + + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); + if (!eptdev) + return -ENOMEM; + + eptdev->rpdev = rpdev; + eptdev->chinfo = chinfo; + + mutex_init(&eptdev->ept_lock); + spin_lock_init(&eptdev->queue_lock); + skb_queue_head_init(&eptdev->queue); + init_waitqueue_head(&eptdev->readq); + + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(eptdev); + return id; + } + + dev = &eptdev->dev; + device_initialize(dev); + dev->class = rpmsg_class; + dev->id = id; + dev->parent = &ctrldev->dev; + dev->release = rpmsg_eptdev_release_device; + dev->groups = rpmsg_eptdev_groups; + dev_set_name(dev, "rpmsg%d", id); + dev_set_drvdata(dev, eptdev); + + ret = rpmsg_cdev_register(dev, &eptdev->cdev, + &rpmsg_eptdev_fops, &dev->devt); + if (ret) { + dev_err(dev, "cdev_add failed: %d\n", ret); + goto out; + } + + ret = device_add(dev); + if (ret) { + dev_err(dev, "device_register failed: %d\n", ret); + goto out; + } + +out: + if (ret < 0) + put_device(dev); + + return ret; +} + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) { + struct rpmsg_endpoint *ept = eptdev->ept; + + mutex_lock(&eptdev->ept_lock); + eptdev->ept = NULL; + mutex_unlock(&eptdev->ept_lock); + + rpmsg_destroy_ept(ept); + + /* wake up any blocking processes */ + wake_up_interruptible(&eptdev->readq); + + cdev_del(&eptdev->cdev); + device_del(&eptdev->dev); + put_device(&eptdev->dev); + + return 0; +} + +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) { + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + get_device(&ctrldev->rpdev->dev); + filp->private_data = ctrldev; + + return 0; +} + +static int rpmsg_ctrldev_release(struct inode *inode, struct file +*filp) { + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + put_device(&ctrldev->rpdev->dev); + + return 0; +} + +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_ctrldev *ctrldev = fp->private_data; + void __user *argp = (void __user *)arg; + struct rpmsg_endpoint_info eptinfo; + struct rpmsg_channel_info chinfo; + + if (cmd != RPMSG_CREATE_EPT_IOCTL) + return -EINVAL; + + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) + return -EFAULT; + + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; + chinfo.src = eptinfo.src; + chinfo.dst = eptinfo.dst; + + return rpmsg_eptdev_create(ctrldev, chinfo); }; + +static const struct file_operations rpmsg_ctrldev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_ctrldev_open, + .release = rpmsg_ctrldev_release, + .unlocked_ioctl = rpmsg_ctrldev_ioctl, }; + +static void rpmsg_chrdev_release_device(struct device *dev) { + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); + + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); + cdev_del(&ctrldev->cdev); + kfree(ctrldev); +} + +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) { + struct rpmsg_ctrldev *ctrldev; + struct device *dev; + int ret; + int id; + + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); + if (!ctrldev) + return -ENOMEM; + + dev = &ctrldev->dev; + + ctrldev->rpdev = rpdev; + + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(ctrldev); + return id; + } + + device_initialize(dev); + dev->parent = &rpdev->dev; + dev->class = rpmsg_class; + dev->release = rpmsg_chrdev_release_device; + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); + + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, + &rpmsg_ctrldev_fops, &dev->devt); + if (ret < 0) { + put_device(dev); + return ret; + } + + ret = device_add(dev); + if (ret) { + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); + put_device(dev); + } + + dev_set_drvdata(&rpdev->dev, ctrldev); + + return ret; +} + +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) { + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + return rpmsg_eptdev_destroy(eptdev); +} + +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) { + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); + int ret; + + /* Destroy all endpoints */ + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); + if (ret) + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); + + device_del(&ctrldev->dev); + put_device(&ctrldev->dev); +} + +static struct rpmsg_driver rpmsg_chrdev_driver = { + .probe = rpmsg_chrdev_probe, + .remove = rpmsg_chrdev_remove, + .drv = { + .name = "rpmsg_chrdev", + }, +}; + +/** + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev + * @rpdev: prepared rpdev to be used for creating endpoints + * + * This function wraps rpmsg_register_device() preparing the rpdev for +use as + * basis for the rpmsg chrdev. + */ +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) { + strcpy(rpdev->id.name, "rpmsg_chrdev"); + rpdev->driver_override = "rpmsg_chrdev"; + + return rpmsg_register_device(rpdev); +} +EXPORT_SYMBOL(rpmsg_chrdev_register_device); + +static int rpmsg_char_init(void) +{ + int ret; + + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); + if (ret < 0) { + pr_err("rpmsg: failed to allocate char dev region\n"); + return ret; + } + + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); + if (IS_ERR(rpmsg_class)) { + pr_err("failed to create rpmsg class\n"); + ret = PTR_ERR(rpmsg_class); + goto unregister_chrdev; + } + + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); + if (ret < 0) { + pr_err("rpmsgchr: failed to register rpmsg driver\n"); + goto destroy_class; + } + + return 0; + +destroy_class: + class_destroy(rpmsg_class); + +unregister_chrdev: + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); + + return ret; +} +postcore_initcall(rpmsg_char_init); + +static void rpmsg_chrdev_exit(void) +{ + unregister_rpmsg_driver(&rpmsg_chrdev_driver); + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); } +module_exit(rpmsg_chrdev_exit); diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h index 8075a20f919b..53d300eacc1c 100644 --- a/drivers/rpmsg/rpmsg_internal.h +++ b/drivers/rpmsg/rpmsg_internal.h @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, struct device *rpmsg_find_device(struct device *parent, struct rpmsg_channel_info *chinfo); +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); + #endif diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h new file mode 100644 index 000000000000..dedc226e0d3f --- /dev/null +++ b/include/uapi/linux/rpmsg.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _UAPI_RPMSG_H_ +#define _UAPI_RPMSG_H_ + +#include <linux/ioctl.h> +#include <linux/types.h> + +/** + * struct rpmsg_endpoint_info - endpoint info representation + * @name: name of service + * @src: local address + * @dst: destination address + */ +struct rpmsg_endpoint_info { + char name[32]; + __u32 src; + __u32 dst; +}; + +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) + +#endif -- 2.5.0 ---------------------------------------- Hi Bjorn, Great patch! You managed to simplify what I was trying to accomplish with my patch (https://raw.githubusercontent.com/NXPmicro/rpmsg-sysfs/0aa1817545a765c200b1b2f9b6680a420dcf9171/rpmsg_sysfs_interface.patch ) I am looking forward for your patch being upstreamed! Did you place a pull request? Can you share with me/us a link to your fork, where the patch is applied? Thanks, Marek ^ permalink raw reply related [flat|nested] 20+ messages in thread
* RE: [PATCH 4/5] rpmsg: Driver for user space endpoint interface @ 2016-10-10 9:00 ` Marek Novak 0 siblings, 0 replies; 20+ messages in thread From: Marek Novak @ 2016-10-10 9:00 UTC (permalink / raw) To: Bjorn Andersson, Ohad Ben-Cohen Cc: Michal Simek, linux-doc, linux-arm-msm, Linus Walleij, Jonathan Corbet, linux-remoteproc, linux-kernel, Matteo Sartori, linux-arm-kernel -----Original Message----- From: Bjorn Andersson [mailto:bjorn.andersson@linaro.org] Sent: Saturday, October 08, 2016 6:23 AM To: Ohad Ben-Cohen <ohad@wizery.com>; Bjorn Andersson <bjorn.andersson@linaro.org> Cc: Jonathan Corbet <corbet@lwn.net>; Linus Walleij <linus.walleij@linaro.org>; Marek Novak <marek.novak@nxp.com>; Matteo Sartori <matteo.sartori@t3lab.it>; Michal Simek <monstr@monstr.eu>; linux-doc@vger.kernel.org; linux-kernel@vger.kernel.org; linux-remoteproc@vger.kernel.org; linux-arm-kernel@lists.infradead.org; linux-arm-msm@vger.kernel.org Subject: [PATCH 4/5] rpmsg: Driver for user space endpoint interface This driver allows rpmsg instances to expose access to rpmsg endpoints to user space processes. It provides a control interface, allowing userspace to export endpoints and an endpoint interface for each exposed endpoint. The implementation is based on prior art by Texas Instrument, Google, PetaLogix and was derived from a FreeRTOS performance statistics driver written by Michal Simek. The control interface provides a "create endpoint" ioctl, which is fed a name, source and destination address. The three values are used to create the endpoint, in a backend-specific way, and a rpmsg endpoint device is created - with the three parameters are available in sysfs for udev usage. E.g. to create an endpoint device for one of the Qualcomm SMD channel related to DIAG one would issue: struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; int fd = open("/dev/rpmsg_ctrl0", O_RDWR); ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); Each created endpoint device shows up as an individual character device in /dev, allowing permission to be controlled on a per-endpoint basis. The rpmsg endpoint will be created and destroyed following the opening and closing of the endpoint device, allowing rpmsg backends to open and close the physical channel, if supported by the wire protocol. Cc: Marek Novak <marek.novak@nxp.com> Cc: Matteo Sartori <matteo.sartori@t3lab.it> Cc: Michal Simek <monstr@monstr.eu> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- Documentation/ioctl/ioctl-number.txt | 1 + drivers/rpmsg/Makefile | 2 +- drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ drivers/rpmsg/rpmsg_internal.h | 2 + include/uapi/linux/rpmsg.h | 35 +++ 5 files changed, 615 insertions(+), 1 deletion(-) create mode 100644 drivers/rpmsg/rpmsg_char.c create mode 100644 include/uapi/linux/rpmsg.h diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt index 81c7f2bb7daf..08244bea5048 100644 --- a/Documentation/ioctl/ioctl-number.txt +++ b/Documentation/ioctl/ioctl-number.txt @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> 0xB3 00 linux/mmc/ioctl.h 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> 0xC0 00-0F linux/usb/iowarrior.h 0xCA 00-0F uapi/misc/cxl.h 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile index ae9c9132cf76..5daf1209b77d 100644 --- a/drivers/rpmsg/Makefile +++ b/drivers/rpmsg/Makefile @@ -1,3 +1,3 @@ -obj-$(CONFIG_RPMSG) += rpmsg_core.o +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c new file mode 100644 index 000000000000..a398a63e8d44 --- /dev/null +++ b/drivers/rpmsg/rpmsg_char.c @@ -0,0 +1,576 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> + * Copyright (c) 2012, PetaLogix + * Copyright (c) 2011, Texas Instruments, Inc. + * Copyright (c) 2011, Google, Inc. + * + * Based on rpmsg performance statistics driver by Michal Simek, which +in turn + * was based on TI & Google OMX rpmsg driver. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/rpmsg.h> +#include <linux/skbuff.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <uapi/linux/rpmsg.h> + +#include "rpmsg_internal.h" + +#define RPMSG_DEV_MAX 256 + +static dev_t rpmsg_major; +static struct class *rpmsg_class; + +static DEFINE_IDA(rpmsg_ctrl_ida); +static DEFINE_IDA(rpmsg_ept_ida); +static DEFINE_IDA(rpmsg_minor_ida); + +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct +rpmsg_eptdev, cdev) + +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, +dev) #define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct +rpmsg_ctrldev, cdev) + +struct rpmsg_ctrldev { + struct rpmsg_device *rpdev; + struct cdev cdev; + struct device dev; +}; + +struct rpmsg_eptdev { + struct device dev; + struct cdev cdev; + + struct rpmsg_device *rpdev; + struct rpmsg_channel_info chinfo; + + struct mutex ept_lock; + struct rpmsg_endpoint *ept; + + spinlock_t queue_lock; + struct sk_buff_head queue; + wait_queue_head_t readq; +}; + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); + + +static int rpmsg_cdev_register(struct device *dev, + struct cdev *cdev, + const struct file_operations *fops, + dev_t *assigned_devt) +{ + dev_t devt; + int ret; + + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); + if (ret < 0) + return ret; + + devt = MKDEV(MAJOR(rpmsg_major), ret); + + cdev_init(cdev, fops); + cdev->owner = THIS_MODULE; + ret = cdev_add(cdev, devt, 1); + if (ret < 0) { + dev_err(dev, "cdev_add failed: %d\n", ret); + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); + return ret; + } + + *assigned_devt = devt; + return 0; +} + +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, + void *priv, u32 addr) +{ + struct rpmsg_eptdev *eptdev = priv; + struct sk_buff *skb; + + skb = alloc_skb(len, GFP_ATOMIC); + if (!skb) + return -ENOMEM; + + memcpy(skb_put(skb, len), buf, len); + + spin_lock(&eptdev->queue_lock); + skb_queue_tail(&eptdev->queue, skb); + spin_unlock(&eptdev->queue_lock); + + /* wake up any blocking processes, waiting for new data */ + wake_up_interruptible(&eptdev->readq); + + return 0; +} + +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) { + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct rpmsg_endpoint *ept; + struct rpmsg_device *rpdev = eptdev->rpdev; + struct device *dev = &eptdev->dev; + + get_device(dev); + + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); + if (!ept) { + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); + put_device(dev); + return -EINVAL; + } + + eptdev->ept = ept; + filp->private_data = eptdev; + + return 0; +} + +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) +{ + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); + struct device *dev = &eptdev->dev; + struct sk_buff *skb; + + /* Close the endpoint, if it's not already destroyed by the parent */ + if (eptdev->ept) + rpmsg_destroy_ept(eptdev->ept); + + /* Discard all SKBs */ + while (!skb_queue_empty(&eptdev->queue)) { + skb = skb_dequeue(&eptdev->queue); + kfree_skb(skb); + } + + put_device(dev); + + return 0; +} + +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_eptdev *eptdev = fp->private_data; + + if (cmd != RPMSG_DESTROY_EPT_IOCTL) + return -EINVAL; + + return rpmsg_eptdev_destroy(eptdev); +} + +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + unsigned long flags; + struct sk_buff *skb; + int use; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + + /* Wait for data in the queue */ + if (skb_queue_empty(&eptdev->queue)) { + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + if (filp->f_flags & O_NONBLOCK) + return -EAGAIN; + + /* Wait until we get data or the endpoint goes away */ + if (wait_event_interruptible(eptdev->readq, + !skb_queue_empty(&eptdev->queue) || + !eptdev->ept)) + return -ERESTARTSYS; + + /* We lost the endpoint while waiting */ + if (!eptdev->ept) + return -EPIPE; + + spin_lock_irqsave(&eptdev->queue_lock, flags); + } + + skb = skb_dequeue(&eptdev->queue); + if (!skb) + return -EFAULT; + + spin_unlock_irqrestore(&eptdev->queue_lock, flags); + + use = min_t(size_t, count, skb->len); + if (copy_to_user(buf, skb->data, use)) + use = -EFAULT; + + kfree_skb(skb); + + return use; +} + +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, + size_t count, loff_t *f_pos) +{ + struct rpmsg_eptdev *eptdev = filp->private_data; + void *kbuf; + int ret; + + kbuf = kzalloc(count, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + if (copy_from_user(kbuf, buf, count)) { + ret = -EFAULT; + goto free_kbuf; + } + + if (mutex_lock_interruptible(&eptdev->ept_lock)) { + ret = -ERESTARTSYS; + goto free_kbuf; + } + + if (!eptdev->ept) { + ret = -EPIPE; + goto unlock_eptdev; + } + + if (filp->f_flags & O_NONBLOCK) + ret = rpmsg_trysend(eptdev->ept, kbuf, count); + else + ret = rpmsg_send(eptdev->ept, kbuf, count); + +unlock_eptdev: + mutex_unlock(&eptdev->ept_lock); + +free_kbuf: + kfree(kbuf); + return ret; +} + +static const struct file_operations rpmsg_eptdev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_eptdev_open, + .release = rpmsg_eptdev_release, + .read = rpmsg_eptdev_read, + .write = rpmsg_eptdev_write, + .unlocked_ioctl = rpmsg_eptdev_ioctl, +}; + +static ssize_t name_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%s\n", eptdev->chinfo.name); } static +DEVICE_ATTR_RO(name); + +static ssize_t src_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.src); } static +DEVICE_ATTR_RO(src); + +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); + + return sprintf(buf, "%d\n", eptdev->chinfo.dst); } static +DEVICE_ATTR_RO(dst); + +static struct attribute *rpmsg_eptdev_attrs[] = { + &dev_attr_name.attr, + &dev_attr_src.attr, + &dev_attr_dst.attr, + NULL +}; +ATTRIBUTE_GROUPS(rpmsg_eptdev); + +static void rpmsg_eptdev_release_device(struct device *dev) { + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); + kfree(eptdev); +} + +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, + struct rpmsg_channel_info chinfo) { + struct rpmsg_device *rpdev = ctrldev->rpdev; + struct rpmsg_eptdev *eptdev; + struct device *dev; + int ret; + int id; + + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); + if (!eptdev) + return -ENOMEM; + + eptdev->rpdev = rpdev; + eptdev->chinfo = chinfo; + + mutex_init(&eptdev->ept_lock); + spin_lock_init(&eptdev->queue_lock); + skb_queue_head_init(&eptdev->queue); + init_waitqueue_head(&eptdev->readq); + + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(eptdev); + return id; + } + + dev = &eptdev->dev; + device_initialize(dev); + dev->class = rpmsg_class; + dev->id = id; + dev->parent = &ctrldev->dev; + dev->release = rpmsg_eptdev_release_device; + dev->groups = rpmsg_eptdev_groups; + dev_set_name(dev, "rpmsg%d", id); + dev_set_drvdata(dev, eptdev); + + ret = rpmsg_cdev_register(dev, &eptdev->cdev, + &rpmsg_eptdev_fops, &dev->devt); + if (ret) { + dev_err(dev, "cdev_add failed: %d\n", ret); + goto out; + } + + ret = device_add(dev); + if (ret) { + dev_err(dev, "device_register failed: %d\n", ret); + goto out; + } + +out: + if (ret < 0) + put_device(dev); + + return ret; +} + +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) { + struct rpmsg_endpoint *ept = eptdev->ept; + + mutex_lock(&eptdev->ept_lock); + eptdev->ept = NULL; + mutex_unlock(&eptdev->ept_lock); + + rpmsg_destroy_ept(ept); + + /* wake up any blocking processes */ + wake_up_interruptible(&eptdev->readq); + + cdev_del(&eptdev->cdev); + device_del(&eptdev->dev); + put_device(&eptdev->dev); + + return 0; +} + +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) { + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + get_device(&ctrldev->rpdev->dev); + filp->private_data = ctrldev; + + return 0; +} + +static int rpmsg_ctrldev_release(struct inode *inode, struct file +*filp) { + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); + + put_device(&ctrldev->rpdev->dev); + + return 0; +} + +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, + unsigned long arg) +{ + struct rpmsg_ctrldev *ctrldev = fp->private_data; + void __user *argp = (void __user *)arg; + struct rpmsg_endpoint_info eptinfo; + struct rpmsg_channel_info chinfo; + + if (cmd != RPMSG_CREATE_EPT_IOCTL) + return -EINVAL; + + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) + return -EFAULT; + + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; + chinfo.src = eptinfo.src; + chinfo.dst = eptinfo.dst; + + return rpmsg_eptdev_create(ctrldev, chinfo); }; + +static const struct file_operations rpmsg_ctrldev_fops = { + .owner = THIS_MODULE, + .open = rpmsg_ctrldev_open, + .release = rpmsg_ctrldev_release, + .unlocked_ioctl = rpmsg_ctrldev_ioctl, }; + +static void rpmsg_chrdev_release_device(struct device *dev) { + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); + + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); + cdev_del(&ctrldev->cdev); + kfree(ctrldev); +} + +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) { + struct rpmsg_ctrldev *ctrldev; + struct device *dev; + int ret; + int id; + + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); + if (!ctrldev) + return -ENOMEM; + + dev = &ctrldev->dev; + + ctrldev->rpdev = rpdev; + + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); + if (id < 0) { + kfree(ctrldev); + return id; + } + + device_initialize(dev); + dev->parent = &rpdev->dev; + dev->class = rpmsg_class; + dev->release = rpmsg_chrdev_release_device; + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); + + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, + &rpmsg_ctrldev_fops, &dev->devt); + if (ret < 0) { + put_device(dev); + return ret; + } + + ret = device_add(dev); + if (ret) { + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); + put_device(dev); + } + + dev_set_drvdata(&rpdev->dev, ctrldev); + + return ret; +} + +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) { + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); + + return rpmsg_eptdev_destroy(eptdev); +} + +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) { + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); + int ret; + + /* Destroy all endpoints */ + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); + if (ret) + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); + + device_del(&ctrldev->dev); + put_device(&ctrldev->dev); +} + +static struct rpmsg_driver rpmsg_chrdev_driver = { + .probe = rpmsg_chrdev_probe, + .remove = rpmsg_chrdev_remove, + .drv = { + .name = "rpmsg_chrdev", + }, +}; + +/** + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev + * @rpdev: prepared rpdev to be used for creating endpoints + * + * This function wraps rpmsg_register_device() preparing the rpdev for +use as + * basis for the rpmsg chrdev. + */ +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) { + strcpy(rpdev->id.name, "rpmsg_chrdev"); + rpdev->driver_override = "rpmsg_chrdev"; + + return rpmsg_register_device(rpdev); +} +EXPORT_SYMBOL(rpmsg_chrdev_register_device); + +static int rpmsg_char_init(void) +{ + int ret; + + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); + if (ret < 0) { + pr_err("rpmsg: failed to allocate char dev region\n"); + return ret; + } + + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); + if (IS_ERR(rpmsg_class)) { + pr_err("failed to create rpmsg class\n"); + ret = PTR_ERR(rpmsg_class); + goto unregister_chrdev; + } + + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); + if (ret < 0) { + pr_err("rpmsgchr: failed to register rpmsg driver\n"); + goto destroy_class; + } + + return 0; + +destroy_class: + class_destroy(rpmsg_class); + +unregister_chrdev: + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); + + return ret; +} +postcore_initcall(rpmsg_char_init); + +static void rpmsg_chrdev_exit(void) +{ + unregister_rpmsg_driver(&rpmsg_chrdev_driver); + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); } +module_exit(rpmsg_chrdev_exit); diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h index 8075a20f919b..53d300eacc1c 100644 --- a/drivers/rpmsg/rpmsg_internal.h +++ b/drivers/rpmsg/rpmsg_internal.h @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, struct device *rpmsg_find_device(struct device *parent, struct rpmsg_channel_info *chinfo); +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); + #endif diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h new file mode 100644 index 000000000000..dedc226e0d3f --- /dev/null +++ b/include/uapi/linux/rpmsg.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016, Linaro Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _UAPI_RPMSG_H_ +#define _UAPI_RPMSG_H_ + +#include <linux/ioctl.h> +#include <linux/types.h> + +/** + * struct rpmsg_endpoint_info - endpoint info representation + * @name: name of service + * @src: local address + * @dst: destination address + */ +struct rpmsg_endpoint_info { + char name[32]; + __u32 src; + __u32 dst; +}; + +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) + +#endif -- 2.5.0 ---------------------------------------- Hi Bjorn, Great patch! You managed to simplify what I was trying to accomplish with my patch (https://raw.githubusercontent.com/NXPmicro/rpmsg-sysfs/0aa1817545a765c200b1b2f9b6680a420dcf9171/rpmsg_sysfs_interface.patch ) I am looking forward for your patch being upstreamed! Did you place a pull request? Can you share with me/us a link to your fork, where the patch is applied? Thanks, Marek ^ permalink raw reply related [flat|nested] 20+ messages in thread
* Re: [PATCH 4/5] rpmsg: Driver for user space endpoint interface 2016-10-08 4:23 ` Bjorn Andersson (?) @ 2016-10-11 7:46 ` loic pallardy -1 siblings, 0 replies; 20+ messages in thread From: loic pallardy @ 2016-10-11 7:46 UTC (permalink / raw) To: Bjorn Andersson, Ohad Ben-Cohen Cc: Jonathan Corbet, Linus Walleij, Marek Novak, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm On 10/08/2016 06:23 AM, Bjorn Andersson wrote: > This driver allows rpmsg instances to expose access to rpmsg endpoints > to user space processes. It provides a control interface, allowing > userspace to export endpoints and an endpoint interface for each exposed > endpoint. > > The implementation is based on prior art by Texas Instrument, Google, > PetaLogix and was derived from a FreeRTOS performance statistics driver > written by Michal Simek. > > The control interface provides a "create endpoint" ioctl, which is fed a > name, source and destination address. The three values are used to > create the endpoint, in a backend-specific way, and a rpmsg endpoint > device is created - with the three parameters are available in sysfs for > udev usage. > > E.g. to create an endpoint device for one of the Qualcomm SMD channel > related to DIAG one would issue: > > struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; > int fd = open("/dev/rpmsg_ctrl0", O_RDWR); > ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); > > Each created endpoint device shows up as an individual character device > in /dev, allowing permission to be controlled on a per-endpoint basis. > The rpmsg endpoint will be created and destroyed following the opening > and closing of the endpoint device, allowing rpmsg backends to open and > close the physical channel, if supported by the wire protocol. > > Cc: Marek Novak <marek.novak@nxp.com> > Cc: Matteo Sartori <matteo.sartori@t3lab.it> > Cc: Michal Simek <monstr@monstr.eu> > Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> > --- > Documentation/ioctl/ioctl-number.txt | 1 + > drivers/rpmsg/Makefile | 2 +- > drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ > drivers/rpmsg/rpmsg_internal.h | 2 + > include/uapi/linux/rpmsg.h | 35 +++ > 5 files changed, 615 insertions(+), 1 deletion(-) > create mode 100644 drivers/rpmsg/rpmsg_char.c > create mode 100644 include/uapi/linux/rpmsg.h > > diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt > index 81c7f2bb7daf..08244bea5048 100644 > --- a/Documentation/ioctl/ioctl-number.txt > +++ b/Documentation/ioctl/ioctl-number.txt > @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments > 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> > 0xB3 00 linux/mmc/ioctl.h > 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> > +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> > 0xC0 00-0F linux/usb/iowarrior.h > 0xCA 00-0F uapi/misc/cxl.h > 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h > diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile > index ae9c9132cf76..5daf1209b77d 100644 > --- a/drivers/rpmsg/Makefile > +++ b/drivers/rpmsg/Makefile > @@ -1,3 +1,3 @@ > -obj-$(CONFIG_RPMSG) += rpmsg_core.o > +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o Hi Bjorn, Could you please create a dedicated Kconfig entry for this new interface? This should be an option like i2C_dev. Regards, Loic > obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o > obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o > diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c > new file mode 100644 > index 000000000000..a398a63e8d44 > --- /dev/null > +++ b/drivers/rpmsg/rpmsg_char.c > @@ -0,0 +1,576 @@ > +/* > + * Copyright (c) 2016, Linaro Ltd. > + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> > + * Copyright (c) 2012, PetaLogix > + * Copyright (c) 2011, Texas Instruments, Inc. > + * Copyright (c) 2011, Google, Inc. > + * > + * Based on rpmsg performance statistics driver by Michal Simek, which in turn > + * was based on TI & Google OMX rpmsg driver. > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License version 2 and > + * only version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + */ > +#include <linux/cdev.h> > +#include <linux/device.h> > +#include <linux/fs.h> > +#include <linux/idr.h> > +#include <linux/kernel.h> > +#include <linux/module.h> > +#include <linux/rpmsg.h> > +#include <linux/skbuff.h> > +#include <linux/slab.h> > +#include <linux/uaccess.h> > +#include <uapi/linux/rpmsg.h> > + > +#include "rpmsg_internal.h" > + > +#define RPMSG_DEV_MAX 256 > + > +static dev_t rpmsg_major; > +static struct class *rpmsg_class; > + > +static DEFINE_IDA(rpmsg_ctrl_ida); > +static DEFINE_IDA(rpmsg_ept_ida); > +static DEFINE_IDA(rpmsg_minor_ida); > + > +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) > +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev) > + > +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev) > +#define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev) > + > +struct rpmsg_ctrldev { > + struct rpmsg_device *rpdev; > + struct cdev cdev; > + struct device dev; > +}; > + > +struct rpmsg_eptdev { > + struct device dev; > + struct cdev cdev; > + > + struct rpmsg_device *rpdev; > + struct rpmsg_channel_info chinfo; > + > + struct mutex ept_lock; > + struct rpmsg_endpoint *ept; > + > + spinlock_t queue_lock; > + struct sk_buff_head queue; > + wait_queue_head_t readq; > +}; > + > +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); > + > + > +static int rpmsg_cdev_register(struct device *dev, > + struct cdev *cdev, > + const struct file_operations *fops, > + dev_t *assigned_devt) > +{ > + dev_t devt; > + int ret; > + > + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); > + if (ret < 0) > + return ret; > + > + devt = MKDEV(MAJOR(rpmsg_major), ret); > + > + cdev_init(cdev, fops); > + cdev->owner = THIS_MODULE; > + ret = cdev_add(cdev, devt, 1); > + if (ret < 0) { > + dev_err(dev, "cdev_add failed: %d\n", ret); > + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); > + return ret; > + } > + > + *assigned_devt = devt; > + return 0; > +} > + > +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, > + void *priv, u32 addr) > +{ > + struct rpmsg_eptdev *eptdev = priv; > + struct sk_buff *skb; > + > + skb = alloc_skb(len, GFP_ATOMIC); > + if (!skb) > + return -ENOMEM; > + > + memcpy(skb_put(skb, len), buf, len); > + > + spin_lock(&eptdev->queue_lock); > + skb_queue_tail(&eptdev->queue, skb); > + spin_unlock(&eptdev->queue_lock); > + > + /* wake up any blocking processes, waiting for new data */ > + wake_up_interruptible(&eptdev->readq); > + > + return 0; > +} > + > +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); > + struct rpmsg_endpoint *ept; > + struct rpmsg_device *rpdev = eptdev->rpdev; > + struct device *dev = &eptdev->dev; > + > + get_device(dev); > + > + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); > + if (!ept) { > + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); > + put_device(dev); > + return -EINVAL; > + } > + > + eptdev->ept = ept; > + filp->private_data = eptdev; > + > + return 0; > +} > + > +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); > + struct device *dev = &eptdev->dev; > + struct sk_buff *skb; > + > + /* Close the endpoint, if it's not already destroyed by the parent */ > + if (eptdev->ept) > + rpmsg_destroy_ept(eptdev->ept); > + > + /* Discard all SKBs */ > + while (!skb_queue_empty(&eptdev->queue)) { > + skb = skb_dequeue(&eptdev->queue); > + kfree_skb(skb); > + } > + > + put_device(dev); > + > + return 0; > +} > + > +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, > + unsigned long arg) > +{ > + struct rpmsg_eptdev *eptdev = fp->private_data; > + > + if (cmd != RPMSG_DESTROY_EPT_IOCTL) > + return -EINVAL; > + > + return rpmsg_eptdev_destroy(eptdev); > +} > + > +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, > + size_t count, loff_t *f_pos) > +{ > + struct rpmsg_eptdev *eptdev = filp->private_data; > + unsigned long flags; > + struct sk_buff *skb; > + int use; > + > + spin_lock_irqsave(&eptdev->queue_lock, flags); > + > + /* Wait for data in the queue */ > + if (skb_queue_empty(&eptdev->queue)) { > + spin_unlock_irqrestore(&eptdev->queue_lock, flags); > + > + if (filp->f_flags & O_NONBLOCK) > + return -EAGAIN; > + > + /* Wait until we get data or the endpoint goes away */ > + if (wait_event_interruptible(eptdev->readq, > + !skb_queue_empty(&eptdev->queue) || > + !eptdev->ept)) > + return -ERESTARTSYS; > + > + /* We lost the endpoint while waiting */ > + if (!eptdev->ept) > + return -EPIPE; > + > + spin_lock_irqsave(&eptdev->queue_lock, flags); > + } > + > + skb = skb_dequeue(&eptdev->queue); > + if (!skb) > + return -EFAULT; > + > + spin_unlock_irqrestore(&eptdev->queue_lock, flags); > + > + use = min_t(size_t, count, skb->len); > + if (copy_to_user(buf, skb->data, use)) > + use = -EFAULT; > + > + kfree_skb(skb); > + > + return use; > +} > + > +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, > + size_t count, loff_t *f_pos) > +{ > + struct rpmsg_eptdev *eptdev = filp->private_data; > + void *kbuf; > + int ret; > + > + kbuf = kzalloc(count, GFP_KERNEL); > + if (!kbuf) > + return -ENOMEM; > + > + if (copy_from_user(kbuf, buf, count)) { > + ret = -EFAULT; > + goto free_kbuf; > + } > + > + if (mutex_lock_interruptible(&eptdev->ept_lock)) { > + ret = -ERESTARTSYS; > + goto free_kbuf; > + } > + > + if (!eptdev->ept) { > + ret = -EPIPE; > + goto unlock_eptdev; > + } > + > + if (filp->f_flags & O_NONBLOCK) > + ret = rpmsg_trysend(eptdev->ept, kbuf, count); > + else > + ret = rpmsg_send(eptdev->ept, kbuf, count); > + > +unlock_eptdev: > + mutex_unlock(&eptdev->ept_lock); > + > +free_kbuf: > + kfree(kbuf); > + return ret; > +} > + > +static const struct file_operations rpmsg_eptdev_fops = { > + .owner = THIS_MODULE, > + .open = rpmsg_eptdev_open, > + .release = rpmsg_eptdev_release, > + .read = rpmsg_eptdev_read, > + .write = rpmsg_eptdev_write, > + .unlocked_ioctl = rpmsg_eptdev_ioctl, > +}; > + > +static ssize_t name_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%s\n", eptdev->chinfo.name); > +} > +static DEVICE_ATTR_RO(name); > + > +static ssize_t src_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%d\n", eptdev->chinfo.src); > +} > +static DEVICE_ATTR_RO(src); > + > +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%d\n", eptdev->chinfo.dst); > +} > +static DEVICE_ATTR_RO(dst); > + > +static struct attribute *rpmsg_eptdev_attrs[] = { > + &dev_attr_name.attr, > + &dev_attr_src.attr, > + &dev_attr_dst.attr, > + NULL > +}; > +ATTRIBUTE_GROUPS(rpmsg_eptdev); > + > +static void rpmsg_eptdev_release_device(struct device *dev) > +{ > + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); > + > + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); > + kfree(eptdev); > +} > + > +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, > + struct rpmsg_channel_info chinfo) > +{ > + struct rpmsg_device *rpdev = ctrldev->rpdev; > + struct rpmsg_eptdev *eptdev; > + struct device *dev; > + int ret; > + int id; > + > + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); > + if (!eptdev) > + return -ENOMEM; > + > + eptdev->rpdev = rpdev; > + eptdev->chinfo = chinfo; > + > + mutex_init(&eptdev->ept_lock); > + spin_lock_init(&eptdev->queue_lock); > + skb_queue_head_init(&eptdev->queue); > + init_waitqueue_head(&eptdev->readq); > + > + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); > + if (id < 0) { > + kfree(eptdev); > + return id; > + } > + > + dev = &eptdev->dev; > + device_initialize(dev); > + dev->class = rpmsg_class; > + dev->id = id; > + dev->parent = &ctrldev->dev; > + dev->release = rpmsg_eptdev_release_device; > + dev->groups = rpmsg_eptdev_groups; > + dev_set_name(dev, "rpmsg%d", id); > + dev_set_drvdata(dev, eptdev); > + > + ret = rpmsg_cdev_register(dev, &eptdev->cdev, > + &rpmsg_eptdev_fops, &dev->devt); > + if (ret) { > + dev_err(dev, "cdev_add failed: %d\n", ret); > + goto out; > + } > + > + ret = device_add(dev); > + if (ret) { > + dev_err(dev, "device_register failed: %d\n", ret); > + goto out; > + } > + > +out: > + if (ret < 0) > + put_device(dev); > + > + return ret; > +} > + > +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) > +{ > + struct rpmsg_endpoint *ept = eptdev->ept; > + > + mutex_lock(&eptdev->ept_lock); > + eptdev->ept = NULL; > + mutex_unlock(&eptdev->ept_lock); > + > + rpmsg_destroy_ept(ept); > + > + /* wake up any blocking processes */ > + wake_up_interruptible(&eptdev->readq); > + > + cdev_del(&eptdev->cdev); > + device_del(&eptdev->dev); > + put_device(&eptdev->dev); > + > + return 0; > +} > + > +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); > + > + get_device(&ctrldev->rpdev->dev); > + filp->private_data = ctrldev; > + > + return 0; > +} > + > +static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); > + > + put_device(&ctrldev->rpdev->dev); > + > + return 0; > +} > + > +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, > + unsigned long arg) > +{ > + struct rpmsg_ctrldev *ctrldev = fp->private_data; > + void __user *argp = (void __user *)arg; > + struct rpmsg_endpoint_info eptinfo; > + struct rpmsg_channel_info chinfo; > + > + if (cmd != RPMSG_CREATE_EPT_IOCTL) > + return -EINVAL; > + > + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) > + return -EFAULT; > + > + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); > + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; > + chinfo.src = eptinfo.src; > + chinfo.dst = eptinfo.dst; > + > + return rpmsg_eptdev_create(ctrldev, chinfo); > +}; > + > +static const struct file_operations rpmsg_ctrldev_fops = { > + .owner = THIS_MODULE, > + .open = rpmsg_ctrldev_open, > + .release = rpmsg_ctrldev_release, > + .unlocked_ioctl = rpmsg_ctrldev_ioctl, > +}; > + > +static void rpmsg_chrdev_release_device(struct device *dev) > +{ > + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); > + > + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); > + cdev_del(&ctrldev->cdev); > + kfree(ctrldev); > +} > + > +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) > +{ > + struct rpmsg_ctrldev *ctrldev; > + struct device *dev; > + int ret; > + int id; > + > + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); > + if (!ctrldev) > + return -ENOMEM; > + > + dev = &ctrldev->dev; > + > + ctrldev->rpdev = rpdev; > + > + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); > + if (id < 0) { > + kfree(ctrldev); > + return id; > + } > + > + device_initialize(dev); > + dev->parent = &rpdev->dev; > + dev->class = rpmsg_class; > + dev->release = rpmsg_chrdev_release_device; > + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); > + > + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, > + &rpmsg_ctrldev_fops, &dev->devt); > + if (ret < 0) { > + put_device(dev); > + return ret; > + } > + > + ret = device_add(dev); > + if (ret) { > + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); > + put_device(dev); > + } > + > + dev_set_drvdata(&rpdev->dev, ctrldev); > + > + return ret; > +} > + > +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) > +{ > + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); > + > + return rpmsg_eptdev_destroy(eptdev); > +} > + > +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) > +{ > + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); > + int ret; > + > + /* Destroy all endpoints */ > + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); > + if (ret) > + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); > + > + device_del(&ctrldev->dev); > + put_device(&ctrldev->dev); > +} > + > +static struct rpmsg_driver rpmsg_chrdev_driver = { > + .probe = rpmsg_chrdev_probe, > + .remove = rpmsg_chrdev_remove, > + .drv = { > + .name = "rpmsg_chrdev", > + }, > +}; > + > +/** > + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev > + * @rpdev: prepared rpdev to be used for creating endpoints > + * > + * This function wraps rpmsg_register_device() preparing the rpdev for use as > + * basis for the rpmsg chrdev. > + */ > +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) > +{ > + strcpy(rpdev->id.name, "rpmsg_chrdev"); > + rpdev->driver_override = "rpmsg_chrdev"; > + > + return rpmsg_register_device(rpdev); > +} > +EXPORT_SYMBOL(rpmsg_chrdev_register_device); > + > +static int rpmsg_char_init(void) > +{ > + int ret; > + > + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); > + if (ret < 0) { > + pr_err("rpmsg: failed to allocate char dev region\n"); > + return ret; > + } > + > + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); > + if (IS_ERR(rpmsg_class)) { > + pr_err("failed to create rpmsg class\n"); > + ret = PTR_ERR(rpmsg_class); > + goto unregister_chrdev; > + } > + > + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); > + if (ret < 0) { > + pr_err("rpmsgchr: failed to register rpmsg driver\n"); > + goto destroy_class; > + } > + > + return 0; > + > +destroy_class: > + class_destroy(rpmsg_class); > + > +unregister_chrdev: > + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); > + > + return ret; > +} > +postcore_initcall(rpmsg_char_init); > + > +static void rpmsg_chrdev_exit(void) > +{ > + unregister_rpmsg_driver(&rpmsg_chrdev_driver); > + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); > +} > +module_exit(rpmsg_chrdev_exit); > diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h > index 8075a20f919b..53d300eacc1c 100644 > --- a/drivers/rpmsg/rpmsg_internal.h > +++ b/drivers/rpmsg/rpmsg_internal.h > @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, > struct device *rpmsg_find_device(struct device *parent, > struct rpmsg_channel_info *chinfo); > > +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); > + > #endif > diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h > new file mode 100644 > index 000000000000..dedc226e0d3f > --- /dev/null > +++ b/include/uapi/linux/rpmsg.h > @@ -0,0 +1,35 @@ > +/* > + * Copyright (c) 2016, Linaro Ltd. > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License version 2 and > + * only version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + */ > + > +#ifndef _UAPI_RPMSG_H_ > +#define _UAPI_RPMSG_H_ > + > +#include <linux/ioctl.h> > +#include <linux/types.h> > + > +/** > + * struct rpmsg_endpoint_info - endpoint info representation > + * @name: name of service > + * @src: local address > + * @dst: destination address > + */ > +struct rpmsg_endpoint_info { > + char name[32]; > + __u32 src; > + __u32 dst; > +}; > + > +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) > +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) > + > +#endif > ^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH 4/5] rpmsg: Driver for user space endpoint interface @ 2016-10-11 7:46 ` loic pallardy 0 siblings, 0 replies; 20+ messages in thread From: loic pallardy @ 2016-10-11 7:46 UTC (permalink / raw) To: linux-arm-kernel On 10/08/2016 06:23 AM, Bjorn Andersson wrote: > This driver allows rpmsg instances to expose access to rpmsg endpoints > to user space processes. It provides a control interface, allowing > userspace to export endpoints and an endpoint interface for each exposed > endpoint. > > The implementation is based on prior art by Texas Instrument, Google, > PetaLogix and was derived from a FreeRTOS performance statistics driver > written by Michal Simek. > > The control interface provides a "create endpoint" ioctl, which is fed a > name, source and destination address. The three values are used to > create the endpoint, in a backend-specific way, and a rpmsg endpoint > device is created - with the three parameters are available in sysfs for > udev usage. > > E.g. to create an endpoint device for one of the Qualcomm SMD channel > related to DIAG one would issue: > > struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; > int fd = open("/dev/rpmsg_ctrl0", O_RDWR); > ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); > > Each created endpoint device shows up as an individual character device > in /dev, allowing permission to be controlled on a per-endpoint basis. > The rpmsg endpoint will be created and destroyed following the opening > and closing of the endpoint device, allowing rpmsg backends to open and > close the physical channel, if supported by the wire protocol. > > Cc: Marek Novak <marek.novak@nxp.com> > Cc: Matteo Sartori <matteo.sartori@t3lab.it> > Cc: Michal Simek <monstr@monstr.eu> > Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> > --- > Documentation/ioctl/ioctl-number.txt | 1 + > drivers/rpmsg/Makefile | 2 +- > drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ > drivers/rpmsg/rpmsg_internal.h | 2 + > include/uapi/linux/rpmsg.h | 35 +++ > 5 files changed, 615 insertions(+), 1 deletion(-) > create mode 100644 drivers/rpmsg/rpmsg_char.c > create mode 100644 include/uapi/linux/rpmsg.h > > diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt > index 81c7f2bb7daf..08244bea5048 100644 > --- a/Documentation/ioctl/ioctl-number.txt > +++ b/Documentation/ioctl/ioctl-number.txt > @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments > 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> > 0xB3 00 linux/mmc/ioctl.h > 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> > +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> > 0xC0 00-0F linux/usb/iowarrior.h > 0xCA 00-0F uapi/misc/cxl.h > 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h > diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile > index ae9c9132cf76..5daf1209b77d 100644 > --- a/drivers/rpmsg/Makefile > +++ b/drivers/rpmsg/Makefile > @@ -1,3 +1,3 @@ > -obj-$(CONFIG_RPMSG) += rpmsg_core.o > +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o Hi Bjorn, Could you please create a dedicated Kconfig entry for this new interface? This should be an option like i2C_dev. Regards, Loic > obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o > obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o > diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c > new file mode 100644 > index 000000000000..a398a63e8d44 > --- /dev/null > +++ b/drivers/rpmsg/rpmsg_char.c > @@ -0,0 +1,576 @@ > +/* > + * Copyright (c) 2016, Linaro Ltd. > + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> > + * Copyright (c) 2012, PetaLogix > + * Copyright (c) 2011, Texas Instruments, Inc. > + * Copyright (c) 2011, Google, Inc. > + * > + * Based on rpmsg performance statistics driver by Michal Simek, which in turn > + * was based on TI & Google OMX rpmsg driver. > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License version 2 and > + * only version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + */ > +#include <linux/cdev.h> > +#include <linux/device.h> > +#include <linux/fs.h> > +#include <linux/idr.h> > +#include <linux/kernel.h> > +#include <linux/module.h> > +#include <linux/rpmsg.h> > +#include <linux/skbuff.h> > +#include <linux/slab.h> > +#include <linux/uaccess.h> > +#include <uapi/linux/rpmsg.h> > + > +#include "rpmsg_internal.h" > + > +#define RPMSG_DEV_MAX 256 > + > +static dev_t rpmsg_major; > +static struct class *rpmsg_class; > + > +static DEFINE_IDA(rpmsg_ctrl_ida); > +static DEFINE_IDA(rpmsg_ept_ida); > +static DEFINE_IDA(rpmsg_minor_ida); > + > +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) > +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev) > + > +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev) > +#define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev) > + > +struct rpmsg_ctrldev { > + struct rpmsg_device *rpdev; > + struct cdev cdev; > + struct device dev; > +}; > + > +struct rpmsg_eptdev { > + struct device dev; > + struct cdev cdev; > + > + struct rpmsg_device *rpdev; > + struct rpmsg_channel_info chinfo; > + > + struct mutex ept_lock; > + struct rpmsg_endpoint *ept; > + > + spinlock_t queue_lock; > + struct sk_buff_head queue; > + wait_queue_head_t readq; > +}; > + > +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); > + > + > +static int rpmsg_cdev_register(struct device *dev, > + struct cdev *cdev, > + const struct file_operations *fops, > + dev_t *assigned_devt) > +{ > + dev_t devt; > + int ret; > + > + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); > + if (ret < 0) > + return ret; > + > + devt = MKDEV(MAJOR(rpmsg_major), ret); > + > + cdev_init(cdev, fops); > + cdev->owner = THIS_MODULE; > + ret = cdev_add(cdev, devt, 1); > + if (ret < 0) { > + dev_err(dev, "cdev_add failed: %d\n", ret); > + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); > + return ret; > + } > + > + *assigned_devt = devt; > + return 0; > +} > + > +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, > + void *priv, u32 addr) > +{ > + struct rpmsg_eptdev *eptdev = priv; > + struct sk_buff *skb; > + > + skb = alloc_skb(len, GFP_ATOMIC); > + if (!skb) > + return -ENOMEM; > + > + memcpy(skb_put(skb, len), buf, len); > + > + spin_lock(&eptdev->queue_lock); > + skb_queue_tail(&eptdev->queue, skb); > + spin_unlock(&eptdev->queue_lock); > + > + /* wake up any blocking processes, waiting for new data */ > + wake_up_interruptible(&eptdev->readq); > + > + return 0; > +} > + > +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); > + struct rpmsg_endpoint *ept; > + struct rpmsg_device *rpdev = eptdev->rpdev; > + struct device *dev = &eptdev->dev; > + > + get_device(dev); > + > + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); > + if (!ept) { > + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); > + put_device(dev); > + return -EINVAL; > + } > + > + eptdev->ept = ept; > + filp->private_data = eptdev; > + > + return 0; > +} > + > +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); > + struct device *dev = &eptdev->dev; > + struct sk_buff *skb; > + > + /* Close the endpoint, if it's not already destroyed by the parent */ > + if (eptdev->ept) > + rpmsg_destroy_ept(eptdev->ept); > + > + /* Discard all SKBs */ > + while (!skb_queue_empty(&eptdev->queue)) { > + skb = skb_dequeue(&eptdev->queue); > + kfree_skb(skb); > + } > + > + put_device(dev); > + > + return 0; > +} > + > +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, > + unsigned long arg) > +{ > + struct rpmsg_eptdev *eptdev = fp->private_data; > + > + if (cmd != RPMSG_DESTROY_EPT_IOCTL) > + return -EINVAL; > + > + return rpmsg_eptdev_destroy(eptdev); > +} > + > +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, > + size_t count, loff_t *f_pos) > +{ > + struct rpmsg_eptdev *eptdev = filp->private_data; > + unsigned long flags; > + struct sk_buff *skb; > + int use; > + > + spin_lock_irqsave(&eptdev->queue_lock, flags); > + > + /* Wait for data in the queue */ > + if (skb_queue_empty(&eptdev->queue)) { > + spin_unlock_irqrestore(&eptdev->queue_lock, flags); > + > + if (filp->f_flags & O_NONBLOCK) > + return -EAGAIN; > + > + /* Wait until we get data or the endpoint goes away */ > + if (wait_event_interruptible(eptdev->readq, > + !skb_queue_empty(&eptdev->queue) || > + !eptdev->ept)) > + return -ERESTARTSYS; > + > + /* We lost the endpoint while waiting */ > + if (!eptdev->ept) > + return -EPIPE; > + > + spin_lock_irqsave(&eptdev->queue_lock, flags); > + } > + > + skb = skb_dequeue(&eptdev->queue); > + if (!skb) > + return -EFAULT; > + > + spin_unlock_irqrestore(&eptdev->queue_lock, flags); > + > + use = min_t(size_t, count, skb->len); > + if (copy_to_user(buf, skb->data, use)) > + use = -EFAULT; > + > + kfree_skb(skb); > + > + return use; > +} > + > +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, > + size_t count, loff_t *f_pos) > +{ > + struct rpmsg_eptdev *eptdev = filp->private_data; > + void *kbuf; > + int ret; > + > + kbuf = kzalloc(count, GFP_KERNEL); > + if (!kbuf) > + return -ENOMEM; > + > + if (copy_from_user(kbuf, buf, count)) { > + ret = -EFAULT; > + goto free_kbuf; > + } > + > + if (mutex_lock_interruptible(&eptdev->ept_lock)) { > + ret = -ERESTARTSYS; > + goto free_kbuf; > + } > + > + if (!eptdev->ept) { > + ret = -EPIPE; > + goto unlock_eptdev; > + } > + > + if (filp->f_flags & O_NONBLOCK) > + ret = rpmsg_trysend(eptdev->ept, kbuf, count); > + else > + ret = rpmsg_send(eptdev->ept, kbuf, count); > + > +unlock_eptdev: > + mutex_unlock(&eptdev->ept_lock); > + > +free_kbuf: > + kfree(kbuf); > + return ret; > +} > + > +static const struct file_operations rpmsg_eptdev_fops = { > + .owner = THIS_MODULE, > + .open = rpmsg_eptdev_open, > + .release = rpmsg_eptdev_release, > + .read = rpmsg_eptdev_read, > + .write = rpmsg_eptdev_write, > + .unlocked_ioctl = rpmsg_eptdev_ioctl, > +}; > + > +static ssize_t name_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%s\n", eptdev->chinfo.name); > +} > +static DEVICE_ATTR_RO(name); > + > +static ssize_t src_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%d\n", eptdev->chinfo.src); > +} > +static DEVICE_ATTR_RO(src); > + > +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%d\n", eptdev->chinfo.dst); > +} > +static DEVICE_ATTR_RO(dst); > + > +static struct attribute *rpmsg_eptdev_attrs[] = { > + &dev_attr_name.attr, > + &dev_attr_src.attr, > + &dev_attr_dst.attr, > + NULL > +}; > +ATTRIBUTE_GROUPS(rpmsg_eptdev); > + > +static void rpmsg_eptdev_release_device(struct device *dev) > +{ > + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); > + > + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); > + kfree(eptdev); > +} > + > +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, > + struct rpmsg_channel_info chinfo) > +{ > + struct rpmsg_device *rpdev = ctrldev->rpdev; > + struct rpmsg_eptdev *eptdev; > + struct device *dev; > + int ret; > + int id; > + > + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); > + if (!eptdev) > + return -ENOMEM; > + > + eptdev->rpdev = rpdev; > + eptdev->chinfo = chinfo; > + > + mutex_init(&eptdev->ept_lock); > + spin_lock_init(&eptdev->queue_lock); > + skb_queue_head_init(&eptdev->queue); > + init_waitqueue_head(&eptdev->readq); > + > + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); > + if (id < 0) { > + kfree(eptdev); > + return id; > + } > + > + dev = &eptdev->dev; > + device_initialize(dev); > + dev->class = rpmsg_class; > + dev->id = id; > + dev->parent = &ctrldev->dev; > + dev->release = rpmsg_eptdev_release_device; > + dev->groups = rpmsg_eptdev_groups; > + dev_set_name(dev, "rpmsg%d", id); > + dev_set_drvdata(dev, eptdev); > + > + ret = rpmsg_cdev_register(dev, &eptdev->cdev, > + &rpmsg_eptdev_fops, &dev->devt); > + if (ret) { > + dev_err(dev, "cdev_add failed: %d\n", ret); > + goto out; > + } > + > + ret = device_add(dev); > + if (ret) { > + dev_err(dev, "device_register failed: %d\n", ret); > + goto out; > + } > + > +out: > + if (ret < 0) > + put_device(dev); > + > + return ret; > +} > + > +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) > +{ > + struct rpmsg_endpoint *ept = eptdev->ept; > + > + mutex_lock(&eptdev->ept_lock); > + eptdev->ept = NULL; > + mutex_unlock(&eptdev->ept_lock); > + > + rpmsg_destroy_ept(ept); > + > + /* wake up any blocking processes */ > + wake_up_interruptible(&eptdev->readq); > + > + cdev_del(&eptdev->cdev); > + device_del(&eptdev->dev); > + put_device(&eptdev->dev); > + > + return 0; > +} > + > +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); > + > + get_device(&ctrldev->rpdev->dev); > + filp->private_data = ctrldev; > + > + return 0; > +} > + > +static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); > + > + put_device(&ctrldev->rpdev->dev); > + > + return 0; > +} > + > +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, > + unsigned long arg) > +{ > + struct rpmsg_ctrldev *ctrldev = fp->private_data; > + void __user *argp = (void __user *)arg; > + struct rpmsg_endpoint_info eptinfo; > + struct rpmsg_channel_info chinfo; > + > + if (cmd != RPMSG_CREATE_EPT_IOCTL) > + return -EINVAL; > + > + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) > + return -EFAULT; > + > + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); > + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; > + chinfo.src = eptinfo.src; > + chinfo.dst = eptinfo.dst; > + > + return rpmsg_eptdev_create(ctrldev, chinfo); > +}; > + > +static const struct file_operations rpmsg_ctrldev_fops = { > + .owner = THIS_MODULE, > + .open = rpmsg_ctrldev_open, > + .release = rpmsg_ctrldev_release, > + .unlocked_ioctl = rpmsg_ctrldev_ioctl, > +}; > + > +static void rpmsg_chrdev_release_device(struct device *dev) > +{ > + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); > + > + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); > + cdev_del(&ctrldev->cdev); > + kfree(ctrldev); > +} > + > +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) > +{ > + struct rpmsg_ctrldev *ctrldev; > + struct device *dev; > + int ret; > + int id; > + > + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); > + if (!ctrldev) > + return -ENOMEM; > + > + dev = &ctrldev->dev; > + > + ctrldev->rpdev = rpdev; > + > + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); > + if (id < 0) { > + kfree(ctrldev); > + return id; > + } > + > + device_initialize(dev); > + dev->parent = &rpdev->dev; > + dev->class = rpmsg_class; > + dev->release = rpmsg_chrdev_release_device; > + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); > + > + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, > + &rpmsg_ctrldev_fops, &dev->devt); > + if (ret < 0) { > + put_device(dev); > + return ret; > + } > + > + ret = device_add(dev); > + if (ret) { > + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); > + put_device(dev); > + } > + > + dev_set_drvdata(&rpdev->dev, ctrldev); > + > + return ret; > +} > + > +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) > +{ > + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); > + > + return rpmsg_eptdev_destroy(eptdev); > +} > + > +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) > +{ > + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); > + int ret; > + > + /* Destroy all endpoints */ > + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); > + if (ret) > + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); > + > + device_del(&ctrldev->dev); > + put_device(&ctrldev->dev); > +} > + > +static struct rpmsg_driver rpmsg_chrdev_driver = { > + .probe = rpmsg_chrdev_probe, > + .remove = rpmsg_chrdev_remove, > + .drv = { > + .name = "rpmsg_chrdev", > + }, > +}; > + > +/** > + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev > + * @rpdev: prepared rpdev to be used for creating endpoints > + * > + * This function wraps rpmsg_register_device() preparing the rpdev for use as > + * basis for the rpmsg chrdev. > + */ > +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) > +{ > + strcpy(rpdev->id.name, "rpmsg_chrdev"); > + rpdev->driver_override = "rpmsg_chrdev"; > + > + return rpmsg_register_device(rpdev); > +} > +EXPORT_SYMBOL(rpmsg_chrdev_register_device); > + > +static int rpmsg_char_init(void) > +{ > + int ret; > + > + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); > + if (ret < 0) { > + pr_err("rpmsg: failed to allocate char dev region\n"); > + return ret; > + } > + > + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); > + if (IS_ERR(rpmsg_class)) { > + pr_err("failed to create rpmsg class\n"); > + ret = PTR_ERR(rpmsg_class); > + goto unregister_chrdev; > + } > + > + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); > + if (ret < 0) { > + pr_err("rpmsgchr: failed to register rpmsg driver\n"); > + goto destroy_class; > + } > + > + return 0; > + > +destroy_class: > + class_destroy(rpmsg_class); > + > +unregister_chrdev: > + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); > + > + return ret; > +} > +postcore_initcall(rpmsg_char_init); > + > +static void rpmsg_chrdev_exit(void) > +{ > + unregister_rpmsg_driver(&rpmsg_chrdev_driver); > + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); > +} > +module_exit(rpmsg_chrdev_exit); > diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h > index 8075a20f919b..53d300eacc1c 100644 > --- a/drivers/rpmsg/rpmsg_internal.h > +++ b/drivers/rpmsg/rpmsg_internal.h > @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, > struct device *rpmsg_find_device(struct device *parent, > struct rpmsg_channel_info *chinfo); > > +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); > + > #endif > diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h > new file mode 100644 > index 000000000000..dedc226e0d3f > --- /dev/null > +++ b/include/uapi/linux/rpmsg.h > @@ -0,0 +1,35 @@ > +/* > + * Copyright (c) 2016, Linaro Ltd. > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License version 2 and > + * only version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + */ > + > +#ifndef _UAPI_RPMSG_H_ > +#define _UAPI_RPMSG_H_ > + > +#include <linux/ioctl.h> > +#include <linux/types.h> > + > +/** > + * struct rpmsg_endpoint_info - endpoint info representation > + * @name: name of service > + * @src: local address > + * @dst: destination address > + */ > +struct rpmsg_endpoint_info { > + char name[32]; > + __u32 src; > + __u32 dst; > +}; > + > +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) > +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) > + > +#endif > ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH 4/5] rpmsg: Driver for user space endpoint interface @ 2016-10-11 7:46 ` loic pallardy 0 siblings, 0 replies; 20+ messages in thread From: loic pallardy @ 2016-10-11 7:46 UTC (permalink / raw) To: Bjorn Andersson, Ohad Ben-Cohen Cc: Jonathan Corbet, Linus Walleij, Marek Novak, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm On 10/08/2016 06:23 AM, Bjorn Andersson wrote: > This driver allows rpmsg instances to expose access to rpmsg endpoints > to user space processes. It provides a control interface, allowing > userspace to export endpoints and an endpoint interface for each exposed > endpoint. > > The implementation is based on prior art by Texas Instrument, Google, > PetaLogix and was derived from a FreeRTOS performance statistics driver > written by Michal Simek. > > The control interface provides a "create endpoint" ioctl, which is fed a > name, source and destination address. The three values are used to > create the endpoint, in a backend-specific way, and a rpmsg endpoint > device is created - with the three parameters are available in sysfs for > udev usage. > > E.g. to create an endpoint device for one of the Qualcomm SMD channel > related to DIAG one would issue: > > struct rpmsg_endpoint_info info = { "DIAG_CNTL", 0, 0 }; > int fd = open("/dev/rpmsg_ctrl0", O_RDWR); > ioctl(fd, RPMSG_CREATE_EPT_IOCTL, &info); > > Each created endpoint device shows up as an individual character device > in /dev, allowing permission to be controlled on a per-endpoint basis. > The rpmsg endpoint will be created and destroyed following the opening > and closing of the endpoint device, allowing rpmsg backends to open and > close the physical channel, if supported by the wire protocol. > > Cc: Marek Novak <marek.novak@nxp.com> > Cc: Matteo Sartori <matteo.sartori@t3lab.it> > Cc: Michal Simek <monstr@monstr.eu> > Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> > --- > Documentation/ioctl/ioctl-number.txt | 1 + > drivers/rpmsg/Makefile | 2 +- > drivers/rpmsg/rpmsg_char.c | 576 +++++++++++++++++++++++++++++++++++ > drivers/rpmsg/rpmsg_internal.h | 2 + > include/uapi/linux/rpmsg.h | 35 +++ > 5 files changed, 615 insertions(+), 1 deletion(-) > create mode 100644 drivers/rpmsg/rpmsg_char.c > create mode 100644 include/uapi/linux/rpmsg.h > > diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt > index 81c7f2bb7daf..08244bea5048 100644 > --- a/Documentation/ioctl/ioctl-number.txt > +++ b/Documentation/ioctl/ioctl-number.txt > @@ -321,6 +321,7 @@ Code Seq#(hex) Include File Comments > 0xB1 00-1F PPPoX <mailto:mostrows@styx.uwaterloo.ca> > 0xB3 00 linux/mmc/ioctl.h > 0xB4 00-0F linux/gpio.h <mailto:linux-gpio@vger.kernel.org> > +0xB5 00-0F uapi/linux/rpmsg.h <mailto:linux-remoteproc@vger.kernel.org> > 0xC0 00-0F linux/usb/iowarrior.h > 0xCA 00-0F uapi/misc/cxl.h > 0xCA 80-8F uapi/scsi/cxlflash_ioctl.h > diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile > index ae9c9132cf76..5daf1209b77d 100644 > --- a/drivers/rpmsg/Makefile > +++ b/drivers/rpmsg/Makefile > @@ -1,3 +1,3 @@ > -obj-$(CONFIG_RPMSG) += rpmsg_core.o > +obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o Hi Bjorn, Could you please create a dedicated Kconfig entry for this new interface? This should be an option like i2C_dev. Regards, Loic > obj-$(CONFIG_RPMSG_QCOM_SMD) += qcom_smd.o > obj-$(CONFIG_RPMSG_VIRTIO) += virtio_rpmsg_bus.o > diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c > new file mode 100644 > index 000000000000..a398a63e8d44 > --- /dev/null > +++ b/drivers/rpmsg/rpmsg_char.c > @@ -0,0 +1,576 @@ > +/* > + * Copyright (c) 2016, Linaro Ltd. > + * Copyright (c) 2012, Michal Simek <monstr@monstr.eu> > + * Copyright (c) 2012, PetaLogix > + * Copyright (c) 2011, Texas Instruments, Inc. > + * Copyright (c) 2011, Google, Inc. > + * > + * Based on rpmsg performance statistics driver by Michal Simek, which in turn > + * was based on TI & Google OMX rpmsg driver. > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License version 2 and > + * only version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + */ > +#include <linux/cdev.h> > +#include <linux/device.h> > +#include <linux/fs.h> > +#include <linux/idr.h> > +#include <linux/kernel.h> > +#include <linux/module.h> > +#include <linux/rpmsg.h> > +#include <linux/skbuff.h> > +#include <linux/slab.h> > +#include <linux/uaccess.h> > +#include <uapi/linux/rpmsg.h> > + > +#include "rpmsg_internal.h" > + > +#define RPMSG_DEV_MAX 256 > + > +static dev_t rpmsg_major; > +static struct class *rpmsg_class; > + > +static DEFINE_IDA(rpmsg_ctrl_ida); > +static DEFINE_IDA(rpmsg_ept_ida); > +static DEFINE_IDA(rpmsg_minor_ida); > + > +#define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev) > +#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev) > + > +#define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev) > +#define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev) > + > +struct rpmsg_ctrldev { > + struct rpmsg_device *rpdev; > + struct cdev cdev; > + struct device dev; > +}; > + > +struct rpmsg_eptdev { > + struct device dev; > + struct cdev cdev; > + > + struct rpmsg_device *rpdev; > + struct rpmsg_channel_info chinfo; > + > + struct mutex ept_lock; > + struct rpmsg_endpoint *ept; > + > + spinlock_t queue_lock; > + struct sk_buff_head queue; > + wait_queue_head_t readq; > +}; > + > +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev); > + > + > +static int rpmsg_cdev_register(struct device *dev, > + struct cdev *cdev, > + const struct file_operations *fops, > + dev_t *assigned_devt) > +{ > + dev_t devt; > + int ret; > + > + ret = ida_simple_get(&rpmsg_minor_ida, 0, 0, GFP_KERNEL); > + if (ret < 0) > + return ret; > + > + devt = MKDEV(MAJOR(rpmsg_major), ret); > + > + cdev_init(cdev, fops); > + cdev->owner = THIS_MODULE; > + ret = cdev_add(cdev, devt, 1); > + if (ret < 0) { > + dev_err(dev, "cdev_add failed: %d\n", ret); > + ida_simple_remove(&rpmsg_minor_ida, MINOR(devt)); > + return ret; > + } > + > + *assigned_devt = devt; > + return 0; > +} > + > +static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, > + void *priv, u32 addr) > +{ > + struct rpmsg_eptdev *eptdev = priv; > + struct sk_buff *skb; > + > + skb = alloc_skb(len, GFP_ATOMIC); > + if (!skb) > + return -ENOMEM; > + > + memcpy(skb_put(skb, len), buf, len); > + > + spin_lock(&eptdev->queue_lock); > + skb_queue_tail(&eptdev->queue, skb); > + spin_unlock(&eptdev->queue_lock); > + > + /* wake up any blocking processes, waiting for new data */ > + wake_up_interruptible(&eptdev->readq); > + > + return 0; > +} > + > +static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); > + struct rpmsg_endpoint *ept; > + struct rpmsg_device *rpdev = eptdev->rpdev; > + struct device *dev = &eptdev->dev; > + > + get_device(dev); > + > + ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo); > + if (!ept) { > + dev_err(dev, "failed to open %s\n", eptdev->chinfo.name); > + put_device(dev); > + return -EINVAL; > + } > + > + eptdev->ept = ept; > + filp->private_data = eptdev; > + > + return 0; > +} > + > +static int rpmsg_eptdev_release(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev); > + struct device *dev = &eptdev->dev; > + struct sk_buff *skb; > + > + /* Close the endpoint, if it's not already destroyed by the parent */ > + if (eptdev->ept) > + rpmsg_destroy_ept(eptdev->ept); > + > + /* Discard all SKBs */ > + while (!skb_queue_empty(&eptdev->queue)) { > + skb = skb_dequeue(&eptdev->queue); > + kfree_skb(skb); > + } > + > + put_device(dev); > + > + return 0; > +} > + > +static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd, > + unsigned long arg) > +{ > + struct rpmsg_eptdev *eptdev = fp->private_data; > + > + if (cmd != RPMSG_DESTROY_EPT_IOCTL) > + return -EINVAL; > + > + return rpmsg_eptdev_destroy(eptdev); > +} > + > +static ssize_t rpmsg_eptdev_read(struct file *filp, char __user *buf, > + size_t count, loff_t *f_pos) > +{ > + struct rpmsg_eptdev *eptdev = filp->private_data; > + unsigned long flags; > + struct sk_buff *skb; > + int use; > + > + spin_lock_irqsave(&eptdev->queue_lock, flags); > + > + /* Wait for data in the queue */ > + if (skb_queue_empty(&eptdev->queue)) { > + spin_unlock_irqrestore(&eptdev->queue_lock, flags); > + > + if (filp->f_flags & O_NONBLOCK) > + return -EAGAIN; > + > + /* Wait until we get data or the endpoint goes away */ > + if (wait_event_interruptible(eptdev->readq, > + !skb_queue_empty(&eptdev->queue) || > + !eptdev->ept)) > + return -ERESTARTSYS; > + > + /* We lost the endpoint while waiting */ > + if (!eptdev->ept) > + return -EPIPE; > + > + spin_lock_irqsave(&eptdev->queue_lock, flags); > + } > + > + skb = skb_dequeue(&eptdev->queue); > + if (!skb) > + return -EFAULT; > + > + spin_unlock_irqrestore(&eptdev->queue_lock, flags); > + > + use = min_t(size_t, count, skb->len); > + if (copy_to_user(buf, skb->data, use)) > + use = -EFAULT; > + > + kfree_skb(skb); > + > + return use; > +} > + > +static ssize_t rpmsg_eptdev_write(struct file *filp, const char __user *buf, > + size_t count, loff_t *f_pos) > +{ > + struct rpmsg_eptdev *eptdev = filp->private_data; > + void *kbuf; > + int ret; > + > + kbuf = kzalloc(count, GFP_KERNEL); > + if (!kbuf) > + return -ENOMEM; > + > + if (copy_from_user(kbuf, buf, count)) { > + ret = -EFAULT; > + goto free_kbuf; > + } > + > + if (mutex_lock_interruptible(&eptdev->ept_lock)) { > + ret = -ERESTARTSYS; > + goto free_kbuf; > + } > + > + if (!eptdev->ept) { > + ret = -EPIPE; > + goto unlock_eptdev; > + } > + > + if (filp->f_flags & O_NONBLOCK) > + ret = rpmsg_trysend(eptdev->ept, kbuf, count); > + else > + ret = rpmsg_send(eptdev->ept, kbuf, count); > + > +unlock_eptdev: > + mutex_unlock(&eptdev->ept_lock); > + > +free_kbuf: > + kfree(kbuf); > + return ret; > +} > + > +static const struct file_operations rpmsg_eptdev_fops = { > + .owner = THIS_MODULE, > + .open = rpmsg_eptdev_open, > + .release = rpmsg_eptdev_release, > + .read = rpmsg_eptdev_read, > + .write = rpmsg_eptdev_write, > + .unlocked_ioctl = rpmsg_eptdev_ioctl, > +}; > + > +static ssize_t name_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%s\n", eptdev->chinfo.name); > +} > +static DEVICE_ATTR_RO(name); > + > +static ssize_t src_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%d\n", eptdev->chinfo.src); > +} > +static DEVICE_ATTR_RO(src); > + > +static ssize_t dst_show(struct device *dev, struct device_attribute *attr, > + char *buf) > +{ > + struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev); > + > + return sprintf(buf, "%d\n", eptdev->chinfo.dst); > +} > +static DEVICE_ATTR_RO(dst); > + > +static struct attribute *rpmsg_eptdev_attrs[] = { > + &dev_attr_name.attr, > + &dev_attr_src.attr, > + &dev_attr_dst.attr, > + NULL > +}; > +ATTRIBUTE_GROUPS(rpmsg_eptdev); > + > +static void rpmsg_eptdev_release_device(struct device *dev) > +{ > + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); > + > + ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); > + kfree(eptdev); > +} > + > +static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, > + struct rpmsg_channel_info chinfo) > +{ > + struct rpmsg_device *rpdev = ctrldev->rpdev; > + struct rpmsg_eptdev *eptdev; > + struct device *dev; > + int ret; > + int id; > + > + eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL); > + if (!eptdev) > + return -ENOMEM; > + > + eptdev->rpdev = rpdev; > + eptdev->chinfo = chinfo; > + > + mutex_init(&eptdev->ept_lock); > + spin_lock_init(&eptdev->queue_lock); > + skb_queue_head_init(&eptdev->queue); > + init_waitqueue_head(&eptdev->readq); > + > + id = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL); > + if (id < 0) { > + kfree(eptdev); > + return id; > + } > + > + dev = &eptdev->dev; > + device_initialize(dev); > + dev->class = rpmsg_class; > + dev->id = id; > + dev->parent = &ctrldev->dev; > + dev->release = rpmsg_eptdev_release_device; > + dev->groups = rpmsg_eptdev_groups; > + dev_set_name(dev, "rpmsg%d", id); > + dev_set_drvdata(dev, eptdev); > + > + ret = rpmsg_cdev_register(dev, &eptdev->cdev, > + &rpmsg_eptdev_fops, &dev->devt); > + if (ret) { > + dev_err(dev, "cdev_add failed: %d\n", ret); > + goto out; > + } > + > + ret = device_add(dev); > + if (ret) { > + dev_err(dev, "device_register failed: %d\n", ret); > + goto out; > + } > + > +out: > + if (ret < 0) > + put_device(dev); > + > + return ret; > +} > + > +static int rpmsg_eptdev_destroy(struct rpmsg_eptdev *eptdev) > +{ > + struct rpmsg_endpoint *ept = eptdev->ept; > + > + mutex_lock(&eptdev->ept_lock); > + eptdev->ept = NULL; > + mutex_unlock(&eptdev->ept_lock); > + > + rpmsg_destroy_ept(ept); > + > + /* wake up any blocking processes */ > + wake_up_interruptible(&eptdev->readq); > + > + cdev_del(&eptdev->cdev); > + device_del(&eptdev->dev); > + put_device(&eptdev->dev); > + > + return 0; > +} > + > +static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); > + > + get_device(&ctrldev->rpdev->dev); > + filp->private_data = ctrldev; > + > + return 0; > +} > + > +static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp) > +{ > + struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev); > + > + put_device(&ctrldev->rpdev->dev); > + > + return 0; > +} > + > +static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd, > + unsigned long arg) > +{ > + struct rpmsg_ctrldev *ctrldev = fp->private_data; > + void __user *argp = (void __user *)arg; > + struct rpmsg_endpoint_info eptinfo; > + struct rpmsg_channel_info chinfo; > + > + if (cmd != RPMSG_CREATE_EPT_IOCTL) > + return -EINVAL; > + > + if (copy_from_user(&eptinfo, argp, sizeof(eptinfo))) > + return -EFAULT; > + > + memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE); > + chinfo.name[RPMSG_NAME_SIZE-1] = '\0'; > + chinfo.src = eptinfo.src; > + chinfo.dst = eptinfo.dst; > + > + return rpmsg_eptdev_create(ctrldev, chinfo); > +}; > + > +static const struct file_operations rpmsg_ctrldev_fops = { > + .owner = THIS_MODULE, > + .open = rpmsg_ctrldev_open, > + .release = rpmsg_ctrldev_release, > + .unlocked_ioctl = rpmsg_ctrldev_ioctl, > +}; > + > +static void rpmsg_chrdev_release_device(struct device *dev) > +{ > + struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev); > + > + ida_simple_remove(&rpmsg_ctrl_ida, MINOR(dev->devt)); > + cdev_del(&ctrldev->cdev); > + kfree(ctrldev); > +} > + > +static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev) > +{ > + struct rpmsg_ctrldev *ctrldev; > + struct device *dev; > + int ret; > + int id; > + > + ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL); > + if (!ctrldev) > + return -ENOMEM; > + > + dev = &ctrldev->dev; > + > + ctrldev->rpdev = rpdev; > + > + id = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL); > + if (id < 0) { > + kfree(ctrldev); > + return id; > + } > + > + device_initialize(dev); > + dev->parent = &rpdev->dev; > + dev->class = rpmsg_class; > + dev->release = rpmsg_chrdev_release_device; > + dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", id); > + > + ret = rpmsg_cdev_register(dev, &ctrldev->cdev, > + &rpmsg_ctrldev_fops, &dev->devt); > + if (ret < 0) { > + put_device(dev); > + return ret; > + } > + > + ret = device_add(dev); > + if (ret) { > + dev_err(&rpdev->dev, "device_register failed: %d\n", ret); > + put_device(dev); > + } > + > + dev_set_drvdata(&rpdev->dev, ctrldev); > + > + return ret; > +} > + > +static int _rpmsg_eptdev_destroy(struct device *dev, void *data) > +{ > + struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); > + > + return rpmsg_eptdev_destroy(eptdev); > +} > + > +static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) > +{ > + struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); > + int ret; > + > + /* Destroy all endpoints */ > + ret = device_for_each_child(&ctrldev->dev, NULL, _rpmsg_eptdev_destroy); > + if (ret) > + dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); > + > + device_del(&ctrldev->dev); > + put_device(&ctrldev->dev); > +} > + > +static struct rpmsg_driver rpmsg_chrdev_driver = { > + .probe = rpmsg_chrdev_probe, > + .remove = rpmsg_chrdev_remove, > + .drv = { > + .name = "rpmsg_chrdev", > + }, > +}; > + > +/** > + * rpmsg_chrdev_register_device() - register chrdev device based on rpdev > + * @rpdev: prepared rpdev to be used for creating endpoints > + * > + * This function wraps rpmsg_register_device() preparing the rpdev for use as > + * basis for the rpmsg chrdev. > + */ > +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev) > +{ > + strcpy(rpdev->id.name, "rpmsg_chrdev"); > + rpdev->driver_override = "rpmsg_chrdev"; > + > + return rpmsg_register_device(rpdev); > +} > +EXPORT_SYMBOL(rpmsg_chrdev_register_device); > + > +static int rpmsg_char_init(void) > +{ > + int ret; > + > + ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg"); > + if (ret < 0) { > + pr_err("rpmsg: failed to allocate char dev region\n"); > + return ret; > + } > + > + rpmsg_class = class_create(THIS_MODULE, "rpmsg"); > + if (IS_ERR(rpmsg_class)) { > + pr_err("failed to create rpmsg class\n"); > + ret = PTR_ERR(rpmsg_class); > + goto unregister_chrdev; > + } > + > + ret = register_rpmsg_driver(&rpmsg_chrdev_driver); > + if (ret < 0) { > + pr_err("rpmsgchr: failed to register rpmsg driver\n"); > + goto destroy_class; > + } > + > + return 0; > + > +destroy_class: > + class_destroy(rpmsg_class); > + > +unregister_chrdev: > + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); > + > + return ret; > +} > +postcore_initcall(rpmsg_char_init); > + > +static void rpmsg_chrdev_exit(void) > +{ > + unregister_rpmsg_driver(&rpmsg_chrdev_driver); > + unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX); > +} > +module_exit(rpmsg_chrdev_exit); > diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h > index 8075a20f919b..53d300eacc1c 100644 > --- a/drivers/rpmsg/rpmsg_internal.h > +++ b/drivers/rpmsg/rpmsg_internal.h > @@ -79,4 +79,6 @@ int rpmsg_unregister_device(struct device *parent, > struct device *rpmsg_find_device(struct device *parent, > struct rpmsg_channel_info *chinfo); > > +int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev); > + > #endif > diff --git a/include/uapi/linux/rpmsg.h b/include/uapi/linux/rpmsg.h > new file mode 100644 > index 000000000000..dedc226e0d3f > --- /dev/null > +++ b/include/uapi/linux/rpmsg.h > @@ -0,0 +1,35 @@ > +/* > + * Copyright (c) 2016, Linaro Ltd. > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License version 2 and > + * only version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + */ > + > +#ifndef _UAPI_RPMSG_H_ > +#define _UAPI_RPMSG_H_ > + > +#include <linux/ioctl.h> > +#include <linux/types.h> > + > +/** > + * struct rpmsg_endpoint_info - endpoint info representation > + * @name: name of service > + * @src: local address > + * @dst: destination address > + */ > +struct rpmsg_endpoint_info { > + char name[32]; > + __u32 src; > + __u32 dst; > +}; > + > +#define RPMSG_CREATE_EPT_IOCTL _IOW(0xb5, 0x1, struct rpmsg_endpoint_info) > +#define RPMSG_DESTROY_EPT_IOCTL _IO(0xb5, 0x2) > + > +#endif > ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH 4/5] rpmsg: Driver for user space endpoint interface 2016-10-11 7:46 ` loic pallardy (?) @ 2016-10-11 16:36 ` Bjorn Andersson -1 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-11 16:36 UTC (permalink / raw) To: loic pallardy Cc: Ohad Ben-Cohen, Jonathan Corbet, Linus Walleij, Marek Novak, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm On Tue 11 Oct 00:46 PDT 2016, loic pallardy wrote: > On 10/08/2016 06:23 AM, Bjorn Andersson wrote: [..] > >diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile > >index ae9c9132cf76..5daf1209b77d 100644 > >--- a/drivers/rpmsg/Makefile > >+++ b/drivers/rpmsg/Makefile > >@@ -1,3 +1,3 @@ > >-obj-$(CONFIG_RPMSG) += rpmsg_core.o > >+obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o > Hi Bjorn, > > Could you please create a dedicated Kconfig entry for this new interface? > This should be an option like i2C_dev. > No problem, I'll do that. Regards, Bjorn ^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH 4/5] rpmsg: Driver for user space endpoint interface @ 2016-10-11 16:36 ` Bjorn Andersson 0 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-11 16:36 UTC (permalink / raw) To: linux-arm-kernel On Tue 11 Oct 00:46 PDT 2016, loic pallardy wrote: > On 10/08/2016 06:23 AM, Bjorn Andersson wrote: [..] > >diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile > >index ae9c9132cf76..5daf1209b77d 100644 > >--- a/drivers/rpmsg/Makefile > >+++ b/drivers/rpmsg/Makefile > >@@ -1,3 +1,3 @@ > >-obj-$(CONFIG_RPMSG) += rpmsg_core.o > >+obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o > Hi Bjorn, > > Could you please create a dedicated Kconfig entry for this new interface? > This should be an option like i2C_dev. > No problem, I'll do that. Regards, Bjorn ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH 4/5] rpmsg: Driver for user space endpoint interface @ 2016-10-11 16:36 ` Bjorn Andersson 0 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-11 16:36 UTC (permalink / raw) To: loic pallardy Cc: Ohad Ben-Cohen, Michal Simek, Jonathan Corbet, linux-arm-msm, Linus Walleij, linux-doc, linux-remoteproc, linux-kernel, Marek Novak, Matteo Sartori, linux-arm-kernel On Tue 11 Oct 00:46 PDT 2016, loic pallardy wrote: > On 10/08/2016 06:23 AM, Bjorn Andersson wrote: [..] > >diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile > >index ae9c9132cf76..5daf1209b77d 100644 > >--- a/drivers/rpmsg/Makefile > >+++ b/drivers/rpmsg/Makefile > >@@ -1,3 +1,3 @@ > >-obj-$(CONFIG_RPMSG) += rpmsg_core.o > >+obj-$(CONFIG_RPMSG) += rpmsg_core.o rpmsg_char.o > Hi Bjorn, > > Could you please create a dedicated Kconfig entry for this new interface? > This should be an option like i2C_dev. > No problem, I'll do that. Regards, Bjorn ^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH 5/5] rpmsg: smd: Register rpmsg user space interface for edges 2016-10-08 4:23 ` Bjorn Andersson @ 2016-10-08 4:23 ` Bjorn Andersson -1 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: Ohad Ben-Cohen, Bjorn Andersson Cc: Jonathan Corbet, Linus Walleij, Marek Novak, Matteo Sartori, Michal Simek, linux-doc, linux-kernel, linux-remoteproc, linux-arm-kernel, linux-arm-msm Create and register a rpmsg device for use with the rpmsg user space interface, allowing user space to access SMD channels. Also provide the "rpmsg_name" device attribute to expose the edge name in sysfs, allowing the user to write udev rules for specific rpmsg devices and their children. Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- drivers/rpmsg/qcom_smd.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/drivers/rpmsg/qcom_smd.c b/drivers/rpmsg/qcom_smd.c index 92efa74a0024..86856b2b558a 100644 --- a/drivers/rpmsg/qcom_smd.c +++ b/drivers/rpmsg/qcom_smd.c @@ -983,6 +983,20 @@ static int qcom_smd_create_device(struct qcom_smd_channel *channel) return rpmsg_register_device(rpdev); } +static int qcom_smd_create_chrdev(struct qcom_smd_edge *edge) +{ + struct qcom_smd_device *qsdev; + + qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL); + if (!qsdev) + return -ENOMEM; + + qsdev->edge = edge; + qsdev->rpdev.ops = &qcom_smd_device_ops; + qsdev->rpdev.dev.parent = &edge->dev; + return rpmsg_chrdev_register_device(&qsdev->rpdev); +} + /* * Allocate the qcom_smd_channel object for a newly found smd channel, * retrieving and validating the smem items involved. @@ -1284,6 +1298,21 @@ static void qcom_smd_edge_release(struct device *dev) kfree(edge); } +static ssize_t rpmsg_name_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct qcom_smd_edge *edge = to_smd_edge(dev); + + return sprintf(buf, "%s\n", edge->of_node->name); +} +static DEVICE_ATTR_RO(rpmsg_name); + +static struct attribute *qcom_smd_edge_attrs[] = { + &dev_attr_rpmsg_name.attr, + NULL +}; +ATTRIBUTE_GROUPS(qcom_smd_edge); + /** * qcom_smd_register_edge() - register an edge based on an device_node * @parent: parent device for the edge @@ -1305,6 +1334,7 @@ struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent, edge->dev.parent = parent; edge->dev.release = qcom_smd_edge_release; + edge->dev.groups = qcom_smd_edge_groups; dev_set_name(&edge->dev, "%s:%s", dev_name(parent), node->name); ret = device_register(&edge->dev); if (ret) { @@ -1318,6 +1348,12 @@ struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent, goto unregister_dev; } + ret = qcom_smd_create_chrdev(edge); + if (ret) { + dev_err(&edge->dev, "failed to register chrdev for edge\n"); + goto unregister_dev; + } + schedule_work(&edge->scan_work); return edge; -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH 5/5] rpmsg: smd: Register rpmsg user space interface for edges @ 2016-10-08 4:23 ` Bjorn Andersson 0 siblings, 0 replies; 20+ messages in thread From: Bjorn Andersson @ 2016-10-08 4:23 UTC (permalink / raw) To: linux-arm-kernel Create and register a rpmsg device for use with the rpmsg user space interface, allowing user space to access SMD channels. Also provide the "rpmsg_name" device attribute to expose the edge name in sysfs, allowing the user to write udev rules for specific rpmsg devices and their children. Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> --- drivers/rpmsg/qcom_smd.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/drivers/rpmsg/qcom_smd.c b/drivers/rpmsg/qcom_smd.c index 92efa74a0024..86856b2b558a 100644 --- a/drivers/rpmsg/qcom_smd.c +++ b/drivers/rpmsg/qcom_smd.c @@ -983,6 +983,20 @@ static int qcom_smd_create_device(struct qcom_smd_channel *channel) return rpmsg_register_device(rpdev); } +static int qcom_smd_create_chrdev(struct qcom_smd_edge *edge) +{ + struct qcom_smd_device *qsdev; + + qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL); + if (!qsdev) + return -ENOMEM; + + qsdev->edge = edge; + qsdev->rpdev.ops = &qcom_smd_device_ops; + qsdev->rpdev.dev.parent = &edge->dev; + return rpmsg_chrdev_register_device(&qsdev->rpdev); +} + /* * Allocate the qcom_smd_channel object for a newly found smd channel, * retrieving and validating the smem items involved. @@ -1284,6 +1298,21 @@ static void qcom_smd_edge_release(struct device *dev) kfree(edge); } +static ssize_t rpmsg_name_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct qcom_smd_edge *edge = to_smd_edge(dev); + + return sprintf(buf, "%s\n", edge->of_node->name); +} +static DEVICE_ATTR_RO(rpmsg_name); + +static struct attribute *qcom_smd_edge_attrs[] = { + &dev_attr_rpmsg_name.attr, + NULL +}; +ATTRIBUTE_GROUPS(qcom_smd_edge); + /** * qcom_smd_register_edge() - register an edge based on an device_node * @parent: parent device for the edge @@ -1305,6 +1334,7 @@ struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent, edge->dev.parent = parent; edge->dev.release = qcom_smd_edge_release; + edge->dev.groups = qcom_smd_edge_groups; dev_set_name(&edge->dev, "%s:%s", dev_name(parent), node->name); ret = device_register(&edge->dev); if (ret) { @@ -1318,6 +1348,12 @@ struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent, goto unregister_dev; } + ret = qcom_smd_create_chrdev(edge); + if (ret) { + dev_err(&edge->dev, "failed to register chrdev for edge\n"); + goto unregister_dev; + } + schedule_work(&edge->scan_work); return edge; -- 2.5.0 ^ permalink raw reply related [flat|nested] 20+ messages in thread
end of thread, other threads:[~2016-10-11 16:36 UTC | newest] Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2016-10-08 4:23 [PATCH 1/5] rpmsg: smd: Reduce restrictions when finding channel Bjorn Andersson 2016-10-08 4:23 ` Bjorn Andersson 2016-10-08 4:23 ` [PATCH 2/5] rpmsg: Introduce a driver override mechanism Bjorn Andersson 2016-10-08 4:23 ` Bjorn Andersson 2016-10-08 4:23 ` [PATCH 3/5] rpmsg: Support drivers without primary endpoint Bjorn Andersson 2016-10-08 4:23 ` Bjorn Andersson 2016-10-08 4:23 ` [PATCH 4/5] rpmsg: Driver for user space endpoint interface Bjorn Andersson 2016-10-08 4:23 ` Bjorn Andersson 2016-10-10 9:00 ` Marek Novak 2016-10-10 9:00 ` Marek Novak 2016-10-10 9:00 ` Marek Novak 2016-10-10 9:00 ` Marek Novak 2016-10-11 7:46 ` loic pallardy 2016-10-11 7:46 ` loic pallardy 2016-10-11 7:46 ` loic pallardy 2016-10-11 16:36 ` Bjorn Andersson 2016-10-11 16:36 ` Bjorn Andersson 2016-10-11 16:36 ` Bjorn Andersson 2016-10-08 4:23 ` [PATCH 5/5] rpmsg: smd: Register rpmsg user space interface for edges Bjorn Andersson 2016-10-08 4:23 ` Bjorn Andersson
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.