#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/slab.h>
#include <linux/device.h>
#define DEVICE_NAME "chrdev_test"
#define DEVICE_MAJOR 0
#define MINOR_BASE 0
#define MINOR_NUM 1
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("colorfulshark@hotmail.com");
static int major = DEVICE_MAJOR;
static int minor_base = MINOR_BASE;
static int minor_num = MINOR_NUM;
static dev_t devno;
static struct cdev *pcdev;
static struct class *test_class;
static struct device *test_device;
static struct file_operations fops = {
.owner = THIS_MODULE,
};
#if 0
module_param(major, int, S_IRWXU);
module_param(minor_base, int, S_IRWXU);
module_param(minor_num, int, S_IRWXU);
#endif
static void request_devno(void)
{
int ret;
if(major) {
printk(KERN_ALERT "alloc device number static\n");
devno = MKDEV(major, minor_base);
ret = register_chrdev_region(devno, minor_num, DEVICE_NAME);
} else {
printk(KERN_ALERT "alloc device number dynamic\n");
ret = alloc_chrdev_region(&devno, minor_base, minor_num, DEVICE_NAME);
printk(KERN_ALERT "alloc major : %d\n", MAJOR(devno));
}
if(ret < 0) {
printk(KERN_ALERT "register chrdev region failed\n");
}
}
static void free_devno(void)
{
unregister_chrdev_region(devno, minor_num);
}
static int add_cdev(void)
{
int error;
pcdev = cdev_alloc();
if(pcdev == NULL) {
goto err_no_mem;
}
cdev_init(pcdev, &fops);
pcdev->owner = THIS_MODULE;
error = cdev_add(pcdev, devno, minor_num);
if(error)
goto err_cdev_add;
printk(KERN_ALERT "success to add cdev\n");
return 0;
err_no_mem:
printk(KERN_ALERT "no enough memory\n");
free_devno();
return -ENOMEM;
err_cdev_add:
printk(KERN_ALERT "fail to add cdev\n");
free_devno();
return error;
}
static void del_cdev(void)
{
cdev_del(pcdev);
}
static void add_dev_node(void)
{
test_class = class_create(THIS_MODULE, "test_class");
test_device = device_create(test_class, NULL, devno, NULL, "chrdev_test");
}
static void del_dev_node(void)
{
device_destroy(test_class, devno);
class_destroy(test_class);
}
static int chrdev_init(void)
{
printk(KERN_ALERT "chrdev init\n");
request_devno();
add_cdev();
add_dev_node();
return 0;
}
static void chrdev_exit(void)
{
del_dev_node();
del_cdev();
free_devno();
printk(KERN_ALERT "chrdev exit\n");
}
module_init(chrdev_init);
module_exit(chrdev_exit);