字符驱动

<linux/cdev.h>

struct cdev {
    struct kobject kobj; //内嵌的内核对象
    struct module *owner;//内核模块的对象指针
    const struct file_operations *ops;//向上提供的接口
    struct list_head list;//已经向内核注册的所有字符设备形成链表
    dev_t dev;//设备号
    unsigned int count;//次设备号个数
};

void cdev_init(struct cdev *, const struct file_operations *);//用于初始化cdev的成员,并建立cdev和file_operations之间的连接

struct cdev *cdev_alloc(void);//用于动态申请一个cdev内存

void cdev_put(struct cdev *p);//用向系统添加和删除一个cdev,完成字符设备的注册和注销

int cdev_add(struct cdev *, dev_t, unsigned); // 通常在字符设备驱动模块加载函数中调用

void cdev_del(struct cdev *); // 字符设备驱动模块卸载函数中调用

void cd_forget(struct inode *);//结构节点索引

举例
static int __init xxx_init(void)
{
    。。。
    cdev_init(&xxx_dev.cdev, &xxx_fops); // 初始化 cdev
    xxx_dev.cdev.owner = THIS_MODULE;
    MKDEV(xxx, 0);
    获取字符设备号(register_chrdev_region、alloc_chrdev_region)
    ret = cdev_add(&xxx_dev.cdev, xxx_dev_no, 1); //注册设备
    
}

比较老的方法
<linux/fs.h>
int register_chrdev(unsigned int major, const char *name,
                  const struct file_operations *fops)
void unregister_chrdev(unsigned int major, const char *name)

举例
static int __init xxx_init(void)
{
    register_chrdev(MAJOR, XXXname, &XXX_fops);    
}


                  


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值