/*
|
* Copyright (C) 2024 LingYun IoT System Studio
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
*
|
* A character skeleton driver example in linux kernel.
|
*/
|
|
#include <linux/module.h>
|
#include <linux/init.h>
|
#include <linux/kernel.h> /* printk() */
|
#include <linux/fs.h> /* everything... */
|
#include <linux/errno.h> /* error codes */
|
#include <linux/types.h> /* size_t */
|
#include <linux/cdev.h> /* cdev */
|
#include <linux/slab.h> /* kmalloc() */
|
#include <linux/version.h> /* kernel version code */
|
#include <linux/uaccess.h> /* copy_from/to_user() */
|
#include <linux/moduleparam.h>
|
|
/* device name and major number */
|
#define DEV_NAME "chrdev"
|
int dev_major = 0;
|
module_param(dev_major, int, S_IRUGO);
|
|
#define BUF_SIZE 1024
|
typedef struct chrdev_s
|
{
|
struct cdev cdev;
|
char *data; /* data buffer */
|
uint32_t size; /* data buffer size */
|
uint32_t bytes; /* data bytes in the buffer */
|
} chrdev_t;
|
|
static struct chrdev_s dev;
|
|
static ssize_t chrdev_read (struct file *file, char __user *buf, size_t count, loff_t *f_pos)
|
{
|
struct chrdev_s *dev = file->private_data;
|
ssize_t nbytes;
|
ssize_t rv = 0;
|
|
/* no data in buffer */
|
if( !dev->bytes )
|
return 0;
|
|
/* copy data to user space */
|
nbytes = count>dev->bytes ? dev->bytes : count;
|
if( copy_to_user(buf, dev->data, nbytes) )
|
{
|
rv = -EFAULT;
|
goto out;
|
}
|
|
/* update return value and data bytes in buffer */
|
rv = nbytes;
|
dev->bytes -= nbytes;
|
|
out:
|
return rv;
|
}
|
|
static ssize_t chrdev_write (struct file *file, const char __user *buf, size_t count, loff_t *f_pos)
|
{
|
struct chrdev_s *dev = file->private_data;
|
ssize_t nbytes;
|
ssize_t rv = 0;
|
|
/* no space left */
|
if( dev->bytes >= dev->size )
|
return -ENOSPC;
|
|
/* check copy data bytes */
|
if( dev->size - dev->bytes < count)
|
nbytes = dev->size - dev->bytes;
|
else
|
nbytes = count;
|
|
/* copy data from user space */
|
if( copy_from_user(&dev->data[dev->bytes], buf, nbytes) )
|
{
|
rv = -EFAULT;
|
goto out;
|
}
|
|
/* update return value and data bytes in buffer */
|
rv = nbytes;
|
dev->bytes += nbytes;
|
|
out:
|
return rv;
|
}
|
|
static int chrdev_open (struct inode *inode, struct file *file)
|
{
|
struct chrdev_s *dev; /* device struct address */
|
|
/* get the device struct address by container_of() */
|
dev = container_of(inode->i_cdev, struct chrdev_s, cdev);
|
|
/* save the device struct address for other methods */
|
file->private_data = dev;
|
|
return 0;
|
}
|
|
static int chrdev_close (struct inode *node, struct file *file)
|
{
|
return 0;
|
}
|
|
static struct file_operations chrdev_fops = {
|
.owner = THIS_MODULE,
|
.open = chrdev_open, /* open() implementation */
|
.read = chrdev_read, /* read() implementation */
|
.write = chrdev_write, /* write() implementation */
|
.release = chrdev_close, /* close() implementation */
|
};
|
|
static int __init chrdev_init(void)
|
{
|
dev_t devno;
|
int rv;
|
|
/* malloc and initial device read/write buffer */
|
dev.data = kmalloc(BUF_SIZE, GFP_KERNEL);
|
if( !dev.data )
|
{
|
printk(KERN_ERR " %s driver kmalloc() failed\n", DEV_NAME);
|
return -ENOMEM;
|
}
|
dev.size = BUF_SIZE;
|
dev.bytes = 0;
|
memset(dev.data, 0, dev.size);
|
|
/* allocate device number */
|
if(0 != dev_major)
|
{
|
devno = MKDEV(dev_major, 0);
|
rv = register_chrdev_region(devno, 1, DEV_NAME);
|
}
|
else
|
{
|
rv = alloc_chrdev_region(&devno, 0, 1, DEV_NAME);
|
dev_major = MAJOR(devno);
|
}
|
|
if(rv < 0)
|
{
|
printk(KERN_ERR "%s driver can't use major %d\n", DEV_NAME, dev_major);
|
return -ENODEV;
|
}
|
|
/* initialize cdev and setup fops */
|
cdev_init(&dev.cdev, &chrdev_fops);
|
dev.cdev.owner = THIS_MODULE;
|
|
/* register cdev to linux kernel */
|
rv = cdev_add(&dev.cdev, devno, 1);
|
if( rv )
|
{
|
rv = -ENODEV;
|
printk(KERN_ERR "%s driver regist failed, rv=%d\n", DEV_NAME, rv);
|
goto failed1;
|
}
|
|
printk(KERN_INFO "%s driver on major[%d] installed.\n", DEV_NAME, dev_major);
|
return 0;
|
|
failed1:
|
unregister_chrdev_region(devno, 1);
|
|
printk(KERN_ERR "%s driver installed failed.\n", DEV_NAME);
|
return rv;
|
}
|
|
static void __exit chrdev_exit(void)
|
{
|
cdev_del(&dev.cdev);
|
unregister_chrdev_region(MKDEV(dev_major,0), 1);
|
|
kfree(dev.data);
|
|
printk(KERN_INFO "%s driver removed!\n", DEV_NAME);
|
return;
|
}
|
|
module_init(chrdev_init);
|
module_exit(chrdev_exit);
|
|
MODULE_LICENSE("Dual BSD/GPL");
|
MODULE_AUTHOR("GuoWenxue <guowenxue@gmail.com>");
|