nhmk/examples/chardev.c

163 lines
4.5 KiB
C
Raw Normal View History

2021-07-22 06:35:24 +08:00
/*
* chardev.c: Creates a read-only char device that says how many times
* you have read from the dev file
2021-07-22 06:35:24 +08:00
*/
#include <linux/atomic.h>
#include <linux/cdev.h>
2021-07-22 06:35:24 +08:00
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h> /* for sprintf() */
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/types.h>
#include <linux/uaccess.h> /* for get_user and put_user */
#include <asm/errno.h>
2021-07-22 06:35:24 +08:00
2021-08-07 18:29:39 +08:00
/* Prototypes - this would normally go in a .h file */
2021-07-22 06:35:24 +08:00
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char __user *, size_t,
loff_t *);
2021-07-22 06:35:24 +08:00
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
2021-07-22 06:35:24 +08:00
2021-08-07 18:29:39 +08:00
/* Global variables are declared as static, so are global within the file. */
2021-07-22 06:35:24 +08:00
static int major; /* major number assigned to our device driver */
enum {
CDEV_NOT_USED = 0,
CDEV_EXCLUSIVE_OPEN = 1,
};
/* Is device open? Used to prevent multiple access to device */
static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
static char msg[BUF_LEN + 1]; /* The msg the device will give when asked */
2021-07-22 06:35:24 +08:00
static struct class *cls;
2021-08-07 18:29:39 +08:00
static struct file_operations chardev_fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release,
};
2021-07-22 06:35:24 +08:00
static int __init chardev_init(void)
2021-07-22 06:35:24 +08:00
{
2021-08-07 18:29:39 +08:00
major = register_chrdev(0, DEVICE_NAME, &chardev_fops);
2021-07-22 06:35:24 +08:00
2021-08-07 18:29:39 +08:00
if (major < 0) {
pr_alert("Registering char device failed with %d\n", major);
return major;
2021-07-22 06:35:24 +08:00
}
2021-08-07 18:29:39 +08:00
pr_info("I was assigned major number %d.\n", major);
2021-07-22 06:35:24 +08:00
cls = class_create(THIS_MODULE, DEVICE_NAME);
2021-08-07 18:29:39 +08:00
device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
2021-07-22 06:35:24 +08:00
pr_info("Device created on /dev/%s\n", DEVICE_NAME);
return SUCCESS;
}
static void __exit chardev_exit(void)
2021-07-22 06:35:24 +08:00
{
2021-08-07 18:29:39 +08:00
device_destroy(cls, MKDEV(major, 0));
2021-07-22 06:35:24 +08:00
class_destroy(cls);
2021-08-07 18:29:39 +08:00
/* Unregister the device */
unregister_chrdev(major, DEVICE_NAME);
2021-07-22 06:35:24 +08:00
}
2021-08-07 18:29:39 +08:00
/* Methods */
2021-07-22 06:35:24 +08:00
2021-08-07 18:29:39 +08:00
/* Called when a process tries to open the device file, like
* "sudo cat /dev/chardev"
2021-07-22 06:35:24 +08:00
*/
static int device_open(struct inode *inode, struct file *file)
{
static int counter = 0;
if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
2021-07-22 06:35:24 +08:00
return -EBUSY;
sprintf(msg, "I already told you %d times Hello world!\n", counter++);
try_module_get(THIS_MODULE);
return SUCCESS;
}
2021-08-07 18:29:39 +08:00
/* Called when a process closes the device file. */
2021-07-22 06:35:24 +08:00
static int device_release(struct inode *inode, struct file *file)
{
/* We're now ready for our next caller */
atomic_set(&already_open, CDEV_NOT_USED);
2021-07-22 06:35:24 +08:00
2021-08-07 18:29:39 +08:00
/* Decrement the usage count, or else once you opened the file, you will
* never get rid of the module.
2021-07-22 06:35:24 +08:00
*/
module_put(THIS_MODULE);
return SUCCESS;
}
2021-08-07 18:29:39 +08:00
/* Called when a process, which already opened the dev file, attempts to
2021-07-22 06:35:24 +08:00
* read from it.
*/
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char __user *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t *offset)
2021-07-22 06:35:24 +08:00
{
2021-08-07 18:29:39 +08:00
/* Number of bytes actually written to the buffer */
2021-07-22 06:35:24 +08:00
int bytes_read = 0;
const char *msg_ptr = msg;
2021-07-22 06:35:24 +08:00
if (!*(msg_ptr + *offset)) { /* we are at the end of message */
*offset = 0; /* reset the offset */
return 0; /* signify end of file */
}
msg_ptr += *offset;
2021-07-22 06:35:24 +08:00
2021-08-07 18:29:39 +08:00
/* Actually put the data into the buffer */
while (length && *msg_ptr) {
/* The buffer is in the user data segment, not the kernel
2021-07-22 06:35:24 +08:00
* segment so "*" assignment won't work. We have to use
* put_user which copies data from the kernel data segment to
* the user data segment.
*/
2021-08-07 18:29:39 +08:00
put_user(*(msg_ptr++), buffer++);
2021-07-22 06:35:24 +08:00
length--;
bytes_read++;
}
*offset += bytes_read;
2021-08-07 18:29:39 +08:00
/* Most read functions return the number of bytes put into the buffer. */
2021-07-22 06:35:24 +08:00
return bytes_read;
}
2021-08-07 18:29:39 +08:00
/* Called when a process writes to dev file: echo "hi" > /dev/hello */
static ssize_t device_write(struct file *filp, const char __user *buff,
size_t len, loff_t *off)
2021-07-22 06:35:24 +08:00
{
2021-08-07 18:29:39 +08:00
pr_alert("Sorry, this operation is not supported.\n");
2021-07-22 06:35:24 +08:00
return -EINVAL;
}
module_init(chardev_init);
module_exit(chardev_exit);
2021-07-22 06:35:24 +08:00
MODULE_LICENSE("GPL");