304 lines
6.9 KiB
C
Raw Normal View History

/**
* @file demo_char.c
* @author Rick Chan (cy187lion@sina.com)
* @brief Linux char driver demo.
* @version 0.1.0
* @date 2020-04-27
*
* @copyright Copyright (c) 2020
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/device.h>
MODULE_AUTHOR("Rick Chan");
MODULE_LICENSE("GPL");
#define DEMO_MAGIC 'R'
#define DEMO_IOC_SIZE 8
#define DEMO_CTL_IOC _IOC(_IOC_READ|_IOC_WRITE, DEMO_MAGIC, 0, DEMO_IOC_SIZE)
#define DEMO_CTL_IO _IO(DEMO_MAGIC, 1)
#define DEMO_CTL_IOR _IOR(DEMO_MAGIC, 2, uint16_t)
#define DEMO_CTL_IOW _IOW(DEMO_MAGIC, 3, int32_t)
#define DEMO_CTL_IOWR _IOWR(DEMO_MAGIC, 4, uint32_t)
#define DEMO_CTL_MAX 5
#define DEMO_MODULE_NAME "demo_char"
#define DEMO_DEV_CNT 2
// This is a test.
#define DEMO_DATA_SIZE 5
static int demo_major = 0;
struct demo_dev
{
struct cdev cdev;
struct device *dev;
// This is a test.
char demo_text[DEMO_DATA_SIZE];
};
struct class *class;
struct demo_dev* demo_devp;
static int demo_open(struct inode *inode, struct file* filp)
{
struct demo_dev *demo;
demo = container_of(inode->i_cdev, struct demo_dev, cdev);
filp->private_data = demo;
return 0;
}
static int demo_release(struct inode *inode, struct file* filp)
{
return 0;
}
static loff_t demo_llseek(struct file *filp, loff_t offset, int origin)
{
struct demo_dev *devp = filp->private_data;
loff_t ret;
(void)devp;
// This is a test.
switch(origin)
{
case 0: // 从文件开头开始偏移
if(offset<0)
{
ret = -EINVAL;
break;
}
if((unsigned int)offset>DEMO_DATA_SIZE)
{
ret = -EINVAL;
break;
}
filp->f_pos = (unsigned int)offset;
ret = filp->f_pos;
break;
case 1: // 从当前位置开始偏移
if((filp->f_pos+offset)>DEMO_DATA_SIZE)
{
ret = -EINVAL;
break;
}
if((filp->f_pos+offset)<0)
{
ret = -EINVAL;
break;
}
filp->f_pos += offset;
ret = filp->f_pos;
break;
default:
ret = -EINVAL;
}
return ret;
return filp->f_pos;
}
static ssize_t demo_read(struct file *filp, char __user *buffer, size_t count, loff_t *position)
{
struct demo_dev *devp = filp->private_data;
loff_t p = *position;
ssize_t ret = 0;
// This is a test.
// 分析和获取有效的读长度
if(DEMO_DATA_SIZE<=p) // 要读的偏移位置越界
return 0; // End of a file
if(DEMO_DATA_SIZE<(count+p)) // 要读的字节数太大
count = DEMO_DATA_SIZE-p;
if(copy_to_user((void*)buffer, &devp->demo_text[p], count))
ret = -EFAULT;
else
{
*position += count;
ret = count;
}
return ret;
}
static ssize_t demo_write(struct file *filp, const char __user *buffer, size_t count, loff_t *position)
{
struct demo_dev *devp = filp->private_data;
const char __user *p = buffer;
(void)devp;
return p-buffer;
}
static long demo_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct demo_dev *devp = filp->private_data;
(void)devp;
// 检测 cmd 合法性
if (DEMO_MAGIC!=_IOC_TYPE(cmd))
return -EINVAL;
if (DEMO_CTL_MAX<_IOC_NR(cmd))
return -EINVAL;
switch(cmd)
{
case DEMO_CTL_IOC:
{
uint8_t tmp[DEMO_IOC_SIZE];
copy_from_user(&tmp, (void*)arg, _IOC_SIZE(cmd));
copy_to_user((void*)arg, &tmp, _IOC_SIZE(cmd));
}
break;
case DEMO_CTL_IO:
break;
case DEMO_CTL_IOR:
{
uint16_t tmp = 0x55AA;
copy_to_user((void*)arg, &tmp, _IOC_SIZE(cmd));
}
break;
case DEMO_CTL_IOW:
{
int32_t tmp = 0;
copy_from_user(&tmp, (void*)arg, _IOC_SIZE(cmd));
}
break;
case DEMO_CTL_IOWR:
{
uint32_t tmp = 0;
copy_from_user(&tmp, (void*)arg, _IOC_SIZE(cmd));
copy_to_user((void*)arg, &tmp, _IOC_SIZE(cmd));
}
break;
default:
return -ENOIOCTLCMD;
}
return 0;
}
static int demo_mmap(struct file* filp, struct vm_area_struct* vma)
{
struct demo_dev *devp = filp->private_data;
(void)devp;
return 0;
}
static struct file_operations demo_fops = {
.owner = THIS_MODULE,
.open = demo_open,
.release = demo_release,
.llseek = demo_llseek,
.read = demo_read,
.write = demo_write,
.unlocked_ioctl = demo_ioctl,
.mmap = demo_mmap
};
static int demo_setup_cdev(struct demo_dev *devp, int index)
{
char name[16];
int err, devno = MKDEV(demo_major, index);
cdev_init(&devp->cdev, &demo_fops);
devp->cdev.owner = THIS_MODULE;
devp->cdev.ops = &demo_fops;
err = cdev_add(&devp->cdev, devno, 1);
if(err)
{
printk(KERN_ERR "demo add cdev:%d error:%d.\r\n", index, err);
goto out_cdev;
}
// 创建设备节点
memset(name, 0, 16);
sprintf(name, DEMO_MODULE_NAME"%d", index);
printk(KERN_INFO "demo new dev name:%s", name);
devp->dev = device_create(class, NULL, devp->cdev.dev, NULL, name);
// This is a test.
devp->demo_text[DEMO_DATA_SIZE-2] = '\n';
devp->demo_text[DEMO_DATA_SIZE-1] = 0;
sprintf(devp->demo_text, "%d", index);
return 0;
out_cdev:
cdev_del(&devp->cdev);
kfree(devp);
return err;
}
static int __init demo_init(void)
{
int err, i;
dev_t devno = MKDEV(demo_major, 0);
if(demo_major) // 使用固定主设备号
err = register_chrdev_region(devno, DEMO_DEV_CNT, DEMO_MODULE_NAME);
else // 动态分配主设备号
{
err = alloc_chrdev_region(&devno, 0, DEMO_DEV_CNT, DEMO_MODULE_NAME);
demo_major = MAJOR(devno);
}
if(err<0)
return err;
demo_devp = kzalloc(DEMO_DEV_CNT*sizeof(struct demo_dev), GFP_KERNEL);
if(!demo_devp)
{
err = -ENOMEM;
goto out;
}
// 创建设备类, 子设备属于同一个设备类
class = class_create(THIS_MODULE, DEMO_MODULE_NAME);
for(i=0; i<DEMO_DEV_CNT; i++)
{
err = demo_setup_cdev(&demo_devp[i], i);
if(err)
goto out_class;
}
return 0;
out_class:
class_destroy(class);
out:
unregister_chrdev_region(devno, DEMO_DEV_CNT);
return err;
}
static void demo_clean_cdev(struct demo_dev *devp)
{
device_destroy(class, devp->cdev.dev);
cdev_del(&devp->cdev);
}
static void __exit demo_exit(void)
{
int i;
for(i=0; i<DEMO_DEV_CNT; i++)
demo_clean_cdev(&demo_devp[i]);
class_destroy(class);
kfree(demo_devp);
unregister_chrdev_region(MKDEV(demo_major, 0), DEMO_DEV_CNT);
}
// insmod 时可传入参数
module_param(demo_major, int, S_IRUGO);
module_init(demo_init);
module_exit(demo_exit);