Files
linux-nv-oot/drivers/misc/ioctl_example.c
Bharat Nihalani 1267ce01fe misc: Add ioctl_example driver
This driver exposes simple IOCTL that can be used for Perf
measurement.

Bug 4294493

Change-Id: I7a6cba710d9589da41f29e50ea487d8f9597f2db
Signed-off-by: Bharat Nihalani <bnihalani@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nv-oot/+/2991717
(cherry picked from commit ece89cb9c58c76fe822e8e80c99f671114d1b156)
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nv-oot/+/3002117
GVS: Gerrit_Virtual_Submit <buildbot_gerritrpt@nvidia.com>
2023-10-25 16:58:51 -07:00

94 lines
2.0 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/* SPDX-FileCopyrightText: Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
/* Define IOCTL commands */
#define IOCTL_SET_VALUE _IOW('k', 1, int)
#define IOCTL_GET_VALUE _IOR('k', 2, int)
static int value;
static dev_t dev_num;
static struct cdev cdev;
static struct class *dev_class;
static int device_open(struct inode *inode, struct file *file)
{
pr_info("ioctl_example: Device opened\n");
return 0;
}
static int device_release(struct inode *inode, struct file *file)
{
pr_info("ioctl_example: Device closed\n");
return 0;
}
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret = 0;
switch (cmd) {
case IOCTL_SET_VALUE:
if (copy_from_user(&value, (int *)arg, sizeof(int)))
ret = -EFAULT;
break;
case IOCTL_GET_VALUE:
if (copy_to_user((int *)arg, &value, sizeof(int)))
ret = -EFAULT;
break;
default:
ret = -ENOTTY;
break;
}
return ret;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = device_open,
.release = device_release,
.unlocked_ioctl = device_ioctl,
};
static int __init ioctl_example_init(void)
{
/* Allocate major and minor numbers */
alloc_chrdev_region(&dev_num, 0, 1, "ioctl_example");
/* Initialize the character device structure */
cdev_init(&cdev, &fops);
cdev_add(&cdev, dev_num, 1);
/* Create a device class */
dev_class = class_create(THIS_MODULE, "ioctl_example");
device_create(dev_class, NULL, dev_num, NULL, "ioctl_example");
pr_info("ioctl_example: Module loaded\n");
return 0;
}
static void __exit ioctl_example_exit(void)
{
device_destroy(dev_class, dev_num);
class_destroy(dev_class);
cdev_del(&cdev);
unregister_chrdev_region(dev_num, 1);
pr_info("ioctl_example: Module unloaded\n");
}
module_init(ioctl_example_init);
module_exit(ioctl_example_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple IOCTL Example");