这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » linux注册字符设备和卸载字符设备函数

共2条 1/1 1 跳转至

linux注册字符设备和卸载字符设备函数

高工
2018-12-07 08:39:01     打赏

注册一个字符设备的方法是使用:

int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);

这里, 

major 是感兴趣的主编号, 

name 是驱动的名子(出现在 /proc/devices), 

fops 是缺省的file_operations 结构. 

一个对 register_chrdev 的调用为给定的主编号注册 0 - 255 的次编号, 并且为每

一个建立一个缺省的 cdev 结构. 使用这个接口的驱动必须准备好处理对所有 256 个次编号的 open

调用( 不管它们是否对应真实设备 ), 它们不能使用大于 255 的主或次编号.

若主设备号设为0 则内核自动分配主设备号


从系统中去除设备的正确的函数是:

int unregister_chrdev(unsigned int major, const char *name);

major 和 name 必须和传递给 register_chrdev 的相同, 否则调用会失败

例如

#define sddev_MAJOR 200


static const struct file_operations dev_fops={

    .owner  = THIS_MODULE,

    .open   = dev_open,

    .write  = dev_write,

    .read   = dev_read,

    .release = dev_release,


};


register_chrdev(sddev_MAJOR,"sddev",&dev_fops);

unregister_chrdev(sddev_MAJOR,"sddev");





管理员
2018-12-07 09:22:52     打赏
2楼

涨姿势  涨姿势


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]