完善字符设备驱动参考.
Signed-off-by: ithink.chan <chenyang@autoai.com>
This commit is contained in:
parent
fd2e512a16
commit
7263d9ebd4
@ -15,6 +15,10 @@
|
||||
#include <linux/uaccess.h>
|
||||
#include <linux/fs.h>
|
||||
|
||||
#include <linux/cdev.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/device.h>
|
||||
|
||||
MODULE_AUTHOR("Rick Chan");
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
@ -26,9 +30,25 @@ MODULE_LICENSE("GPL");
|
||||
#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"
|
||||
|
||||
static int demo_major = 0;
|
||||
|
||||
struct demo_dev
|
||||
{
|
||||
struct cdev cdev;
|
||||
struct class *uclass;
|
||||
struct device *udevice;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -39,23 +59,35 @@ static int demo_release(struct inode *inode, struct file* filp)
|
||||
|
||||
static loff_t demo_llseek(struct file *filp, loff_t offset, int origin)
|
||||
{
|
||||
struct demo_dev *devp = filp->private_data;
|
||||
|
||||
(void)devp;
|
||||
return filp->f_pos;
|
||||
}
|
||||
|
||||
static ssize_t demo_read(struct file *filp, char __user *buffer, size_t count, loff_t *offset)
|
||||
{
|
||||
struct demo_dev *devp = filp->private_data;
|
||||
const char __user *p = buffer;
|
||||
|
||||
(void)devp;
|
||||
return p-buffer;
|
||||
}
|
||||
|
||||
static ssize_t demo_write(struct file *filp, const char __user *buffer, size_t count, loff_t *offset)
|
||||
{
|
||||
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;
|
||||
@ -100,6 +132,9 @@ static long demo_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
||||
|
||||
static int demo_mmap(struct file* filp, struct vm_area_struct* vma)
|
||||
{
|
||||
struct demo_dev *devp = filp->private_data;
|
||||
(void)devp;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -114,22 +149,60 @@ static struct file_operations demo_fops = {
|
||||
.mmap = demo_mmap
|
||||
};
|
||||
|
||||
static struct miscdevice demo_dev = {
|
||||
.minor = MISC_DYNAMIC_MINOR, // Dynamically allocate minor.
|
||||
.name = "demo_char",
|
||||
.fops = &demo_fops,
|
||||
};
|
||||
|
||||
static int __init demo_init(void)
|
||||
{
|
||||
misc_register(&demo_dev);
|
||||
int result;
|
||||
dev_t devno = MKDEV(demo_major, 0);
|
||||
|
||||
if(demo_major)
|
||||
result = register_chrdev_region(devno, 1, DEMO_MODULE_NAME);
|
||||
else
|
||||
{
|
||||
result = alloc_chrdev_region(&devno, 0, 1, DEMO_MODULE_NAME);
|
||||
demo_major = MAJOR(devno);
|
||||
}
|
||||
|
||||
if(result < 0)
|
||||
return result;
|
||||
|
||||
demo_devp = kzalloc(sizeof(struct demo_dev), GFP_KERNEL);
|
||||
if (!demo_devp)
|
||||
{
|
||||
result = - ENOMEM;
|
||||
goto out_kmalloc;
|
||||
}
|
||||
|
||||
cdev_init(&demo_devp->cdev, &demo_fops);
|
||||
demo_devp->cdev.owner = THIS_MODULE;
|
||||
demo_devp->cdev.ops = &demo_fops;
|
||||
result = cdev_add(&demo_devp->cdev, devno, 1);
|
||||
if(result)
|
||||
{
|
||||
printk(KERN_NOTICE "Error %d.\r\n", result);
|
||||
goto out_cdev;
|
||||
}
|
||||
demo_devp->uclass = class_create(THIS_MODULE, DEMO_MODULE_NAME);
|
||||
demo_devp->udevice = device_create(demo_devp->uclass, NULL, demo_devp->cdev.dev, NULL, DEMO_MODULE_NAME"0");
|
||||
|
||||
return 0;
|
||||
out_cdev:
|
||||
cdev_del(&demo_devp->cdev);
|
||||
kfree(demo_devp);
|
||||
out_kmalloc:
|
||||
unregister_chrdev_region(devno, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void __exit demo_exit(void)
|
||||
{
|
||||
misc_deregister(&demo_dev);
|
||||
device_destroy(demo_devp->uclass, demo_devp->cdev.dev);
|
||||
class_destroy(demo_devp->uclass);
|
||||
cdev_del(&demo_devp->cdev);
|
||||
kfree(demo_devp);
|
||||
unregister_chrdev_region(MKDEV(demo_major, 0), 1);
|
||||
}
|
||||
|
||||
module_param(demo_major, int, S_IRUGO);
|
||||
|
||||
module_init(demo_init);
|
||||
module_exit(demo_exit);
|
||||
|
@ -15,6 +15,10 @@
|
||||
#include <linux/uaccess.h>
|
||||
#include <linux/fs.h>
|
||||
|
||||
#include <linux/cdev.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/device.h>
|
||||
|
||||
MODULE_AUTHOR("Rick Chan");
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
@ -26,9 +30,25 @@ MODULE_LICENSE("GPL");
|
||||
#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"
|
||||
|
||||
static int demo_major = 0;
|
||||
|
||||
struct demo_dev
|
||||
{
|
||||
struct cdev cdev;
|
||||
struct class *uclass;
|
||||
struct device *udevice;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -39,23 +59,35 @@ static int demo_release(struct inode *inode, struct file* filp)
|
||||
|
||||
static loff_t demo_llseek(struct file *filp, loff_t offset, int origin)
|
||||
{
|
||||
struct demo_dev *devp = filp->private_data;
|
||||
|
||||
(void)devp;
|
||||
return filp->f_pos;
|
||||
}
|
||||
|
||||
static ssize_t demo_read(struct file *filp, char __user *buffer, size_t count, loff_t *offset)
|
||||
{
|
||||
struct demo_dev *devp = filp->private_data;
|
||||
const char __user *p = buffer;
|
||||
|
||||
(void)devp;
|
||||
return p-buffer;
|
||||
}
|
||||
|
||||
static ssize_t demo_write(struct file *filp, const char __user *buffer, size_t count, loff_t *offset)
|
||||
{
|
||||
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;
|
||||
@ -100,6 +132,9 @@ static long demo_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
||||
|
||||
static int demo_mmap(struct file* filp, struct vm_area_struct* vma)
|
||||
{
|
||||
struct demo_dev *devp = filp->private_data;
|
||||
(void)devp;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -114,22 +149,60 @@ static struct file_operations demo_fops = {
|
||||
.mmap = demo_mmap
|
||||
};
|
||||
|
||||
static struct miscdevice demo_dev = {
|
||||
.minor = MISC_DYNAMIC_MINOR, // Dynamically allocate minor.
|
||||
.name = "demo_char",
|
||||
.fops = &demo_fops,
|
||||
};
|
||||
|
||||
static int __init demo_init(void)
|
||||
{
|
||||
misc_register(&demo_dev);
|
||||
int result;
|
||||
dev_t devno = MKDEV(demo_major, 0);
|
||||
|
||||
if(demo_major)
|
||||
result = register_chrdev_region(devno, 1, DEMO_MODULE_NAME);
|
||||
else
|
||||
{
|
||||
result = alloc_chrdev_region(&devno, 0, 1, DEMO_MODULE_NAME);
|
||||
demo_major = MAJOR(devno);
|
||||
}
|
||||
|
||||
if(result < 0)
|
||||
return result;
|
||||
|
||||
demo_devp = kzalloc(sizeof(struct demo_dev), GFP_KERNEL);
|
||||
if (!demo_devp)
|
||||
{
|
||||
result = - ENOMEM;
|
||||
goto out_kmalloc;
|
||||
}
|
||||
|
||||
cdev_init(&demo_devp->cdev, &demo_fops);
|
||||
demo_devp->cdev.owner = THIS_MODULE;
|
||||
demo_devp->cdev.ops = &demo_fops;
|
||||
result = cdev_add(&demo_devp->cdev, devno, 1);
|
||||
if(result)
|
||||
{
|
||||
printk(KERN_NOTICE "Error %d.\r\n", result);
|
||||
goto out_cdev;
|
||||
}
|
||||
demo_devp->uclass = class_create(THIS_MODULE, DEMO_MODULE_NAME);
|
||||
demo_devp->udevice = device_create(demo_devp->uclass, NULL, demo_devp->cdev.dev, NULL, DEMO_MODULE_NAME"0");
|
||||
|
||||
return 0;
|
||||
out_cdev:
|
||||
cdev_del(&demo_devp->cdev);
|
||||
kfree(demo_devp);
|
||||
out_kmalloc:
|
||||
unregister_chrdev_region(devno, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void __exit demo_exit(void)
|
||||
{
|
||||
misc_deregister(&demo_dev);
|
||||
device_destroy(demo_devp->uclass, demo_devp->cdev.dev);
|
||||
class_destroy(demo_devp->uclass);
|
||||
cdev_del(&demo_devp->cdev);
|
||||
kfree(demo_devp);
|
||||
unregister_chrdev_region(MKDEV(demo_major, 0), 1);
|
||||
}
|
||||
|
||||
module_param(demo_major, int, S_IRUGO);
|
||||
|
||||
module_init(demo_init);
|
||||
module_exit(demo_exit);
|
||||
|
Loading…
x
Reference in New Issue
Block a user