Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Character device interface driver for Remoteproc framework.
0004  *
0005  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
0006  */
0007 
0008 #include <linux/cdev.h>
0009 #include <linux/compat.h>
0010 #include <linux/fs.h>
0011 #include <linux/module.h>
0012 #include <linux/remoteproc.h>
0013 #include <linux/uaccess.h>
0014 #include <uapi/linux/remoteproc_cdev.h>
0015 
0016 #include "remoteproc_internal.h"
0017 
0018 #define NUM_RPROC_DEVICES   64
0019 static dev_t rproc_major;
0020 
0021 static ssize_t rproc_cdev_write(struct file *filp, const char __user *buf, size_t len, loff_t *pos)
0022 {
0023     struct rproc *rproc = container_of(filp->f_inode->i_cdev, struct rproc, cdev);
0024     int ret = 0;
0025     char cmd[10];
0026 
0027     if (!len || len > sizeof(cmd))
0028         return -EINVAL;
0029 
0030     ret = copy_from_user(cmd, buf, len);
0031     if (ret)
0032         return -EFAULT;
0033 
0034     if (!strncmp(cmd, "start", len)) {
0035         ret = rproc_boot(rproc);
0036     } else if (!strncmp(cmd, "stop", len)) {
0037         ret = rproc_shutdown(rproc);
0038     } else if (!strncmp(cmd, "detach", len)) {
0039         ret = rproc_detach(rproc);
0040     } else {
0041         dev_err(&rproc->dev, "Unrecognized option\n");
0042         ret = -EINVAL;
0043     }
0044 
0045     return ret ? ret : len;
0046 }
0047 
0048 static long rproc_device_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
0049 {
0050     struct rproc *rproc = container_of(filp->f_inode->i_cdev, struct rproc, cdev);
0051     void __user *argp = (void __user *)arg;
0052     s32 param;
0053 
0054     switch (ioctl) {
0055     case RPROC_SET_SHUTDOWN_ON_RELEASE:
0056         if (copy_from_user(&param, argp, sizeof(s32)))
0057             return -EFAULT;
0058 
0059         rproc->cdev_put_on_release = !!param;
0060         break;
0061     case RPROC_GET_SHUTDOWN_ON_RELEASE:
0062         param = (s32)rproc->cdev_put_on_release;
0063         if (copy_to_user(argp, &param, sizeof(s32)))
0064             return -EFAULT;
0065 
0066         break;
0067     default:
0068         dev_err(&rproc->dev, "Unsupported ioctl\n");
0069         return -EINVAL;
0070     }
0071 
0072     return 0;
0073 }
0074 
0075 static int rproc_cdev_release(struct inode *inode, struct file *filp)
0076 {
0077     struct rproc *rproc = container_of(inode->i_cdev, struct rproc, cdev);
0078     int ret = 0;
0079 
0080     if (!rproc->cdev_put_on_release)
0081         return 0;
0082 
0083     if (rproc->state == RPROC_RUNNING)
0084         rproc_shutdown(rproc);
0085     else if (rproc->state == RPROC_ATTACHED)
0086         ret = rproc_detach(rproc);
0087 
0088     return ret;
0089 }
0090 
0091 static const struct file_operations rproc_fops = {
0092     .write = rproc_cdev_write,
0093     .unlocked_ioctl = rproc_device_ioctl,
0094     .compat_ioctl = compat_ptr_ioctl,
0095     .release = rproc_cdev_release,
0096 };
0097 
0098 int rproc_char_device_add(struct rproc *rproc)
0099 {
0100     int ret;
0101 
0102     cdev_init(&rproc->cdev, &rproc_fops);
0103     rproc->cdev.owner = THIS_MODULE;
0104 
0105     rproc->dev.devt = MKDEV(MAJOR(rproc_major), rproc->index);
0106     cdev_set_parent(&rproc->cdev, &rproc->dev.kobj);
0107     ret = cdev_add(&rproc->cdev, rproc->dev.devt, 1);
0108     if (ret < 0)
0109         dev_err(&rproc->dev, "Failed to add char dev for %s\n", rproc->name);
0110 
0111     return ret;
0112 }
0113 
0114 void rproc_char_device_remove(struct rproc *rproc)
0115 {
0116     cdev_del(&rproc->cdev);
0117 }
0118 
0119 void __init rproc_init_cdev(void)
0120 {
0121     int ret;
0122 
0123     ret = alloc_chrdev_region(&rproc_major, 0, NUM_RPROC_DEVICES, "remoteproc");
0124     if (ret < 0)
0125         pr_err("Failed to alloc rproc_cdev region, err %d\n", ret);
0126 }