免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1060 | 回复: 0
打印 上一主题 下一主题

创建字符设备的三种方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-06 12:02 |只看该作者 |倒序浏览
将创建字符设备的三种方法记录一下,以便以后参考.
1. 使用早期的register_chardev()方法
#include
#include
#include
#include
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
static int major;
static int Device_open = 0;
static char msg[BUF_LEN];
static char *msg_ptr;
static struct file_operations fops =
{
        .read = device_read,
        .write = device_write,
        .open = device_open,
        .release = device_release,
};
int init_module(void)
{
        major = register_chrdev(0, DEVICE_NAME, &fops);
        if (major
void cleanup_module(void)
{
        int ret = unregister_chrdev(major, DEVICE_NAME);
        if (ret
static int device_open(struct inode* inode, struct file* file)
{
        static int counter = 0;
        if (Device_open)
        {
                return -EBUSY;
        }
        Device_open++;
        sprintf(msg, "I already told you %d times hello world\nDevice_open=%d\n" , counter++, Device_open);
        msg_ptr = msg;
        try_module_get(THIS_MODULE);
        return SUCCESS;
}
static int device_release(struct inode* inode, struct file* file)
{
        Device_open--;
        module_put(THIS_MODULE);
        return 0;
}
static ssize_t device_read(struct file* filp, char *buffer, size_t length, loff_t *offset)
{
        int bytes_read = 0;
        if (*msg_ptr == '\0')
                return 0;
        printk(KERN_ALERT "length=%d\n", length);
        while (length && *msg_ptr)
        {
                put_user(*(msg_ptr++), buffer++);
                length--;
                bytes_read++;
        }
        return bytes_read;
}
static ssize_t device_write(struct file* filp, const char *buff, size_t len, loff_t *off)
{
        printk(KERN_ALERT "Sorry, this operation isn't supported\n");
        return -EINVAL;
}
2. 使用cdev的方法
#include
#include
#include
#include
#include
#include
#include
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
static int major;
static int Device_open = 0;
static char msg[BUF_LEN];
static char *msg_ptr;
static struct cdev *my_cdev;
static struct file_operations fops =
{
        .read = device_read,
        .write = device_write,
        .open = device_open,
        .release = device_release,
};
int init_module(void)
{
        int err;
        dev_t devid ;
        alloc_chrdev_region(&devid, 0, 1, "chardev");
        major = MAJOR(devid);
        my_cdev = cdev_alloc();
        cdev_init(my_cdev, &fops);
        err = cdev_add(my_cdev, devid, 1);
        if (err)
        {
                printk(KERN_INFO "I was assigned major number %d.\n", major);
                return -1;
        }
        printk("major number is %d\n", MAJOR(devid));
        return SUCCESS;
}
void cleanup_module(void)
{
        cdev_del(my_cdev);
        printk("cleanup done\n");
}
static int device_open(struct inode* inode, struct file* file)
{
        static int counter = 0;
        if (Device_open)
        {
                return -EBUSY;
        }
        Device_open++;
        sprintf(msg, "I already told you %d times hello world\nDevice_open=%d\n", counter++, Device_open);
        msg_ptr = msg;
        try_module_get(THIS_MODULE);
        return SUCCESS;
}
static int device_release(struct inode* inode, struct file* file)
{
        Device_open--;
        module_put(THIS_MODULE);
        return 0;
}
static ssize_t device_read(struct file* filp, char *buffer, size_t length, loff_t *offset)
{
        int bytes_read = 0;
        if (*msg_ptr == '\0')
                return 0;
        printk(KERN_ALERT "length=%d\n", length);
        while (length && *msg_ptr)
        {
                put_user(*(msg_ptr++), buffer++);
                length--;
                bytes_read++;
        }
        return bytes_read;
}
static ssize_t device_write(struct file* filp, const char *buff, size_t len, loff_t *off)
{
        printk(KERN_ALERT "Sorry, this operation isn't supported\n");
        return -EINVAL;
}
3. 使用udev在/dev/下动态生成设备文件的方式
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");                      /*此处如果不加的话加载的时候会出错*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
static int major;
static int Device_open = 0;
static char msg[BUF_LEN];
static char *msg_ptr;
static struct cdev *my_cdev;
static struct class *my_class;
dev_t devid ;
static struct file_operations fops =
{
        .read = device_read,
        .write = device_write,
        .open = device_open,
        .release = device_release,
};
int init_module(void)
{
        int err;
        alloc_chrdev_region(&devid, 0, 1, "chardev");
        major = MAJOR(devid);
        my_cdev = cdev_alloc();
        cdev_init(my_cdev, &fops);
        my_cdev->owner = THIS_MODULE;
        err = cdev_add(my_cdev, devid, 1);
        if (err)
        {
                printk(KERN_INFO "I was assigned major number %d.\n", major);
                return -1;
        }
        my_class = class_create(THIS_MODULE, "chardev_class1");
        if (IS_ERR(my_class))
        {
                printk(KERN_INFO "create class error\n");
                return -1;
        }
        class_device_create(my_class, NULL, devid, NULL, "chardev" "%d", MINOR(devid));
        printk("major number is %d\n", MAJOR(devid));
        return SUCCESS;
}
void cleanup_module(void)
{
        cdev_del(my_cdev);
        class_device_destroy(my_class,  devid);
        class_destroy(my_class);
        printk("cleanup done\n");
}
static int device_open(struct inode* inode, struct file* file)
{
        static int counter = 0;
        if (Device_open)
        {
                return -EBUSY;
        }
        Device_open++;
        sprintf(msg, "I already told you %d times hello world\nDevice_open=%d\n", counter++, Device_open);
        msg_ptr = msg;
        try_module_get(THIS_MODULE);
        return SUCCESS;
}
static int device_release(struct inode* inode, struct file* file)
{
        Device_open--;
        module_put(THIS_MODULE);
        return 0;
}
static ssize_t device_read(struct file* filp, char *buffer, size_t length, loff_t *offset)
{
        int bytes_read = 0;
        if (*msg_ptr == '\0')
                return 0;
        printk(KERN_ALERT "length=%d\n", length);
        while (length && *msg_ptr)
        {
                put_user(*(msg_ptr++), buffer++);
                length--;
                bytes_read++;
        }
        return bytes_read;
}
static ssize_t device_write(struct file* filp, const char *buff, size_t len, loff_t *off)
{
        printk(KERN_ALERT "Sorry, this operation isn't supported\n");
        return -EINVAL;
}



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/36290/showart_415402.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP