cdev_init和register_chrdev区别
---
01:include/linux/fs.h
static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)
{
return __register_chrdev(major, , , name, fops);
}
fs/char_dev.c
/**
* __register_chrdev() - create and register a cdev occupying a range of minors
* @major: major device number or 0 for dynamic allocation
* @baseminor: first of the requested range of minor numbers
* @count: the number of minor numbers required
* @name: name of this range of devices
* @fops: file operations associated with this devices
*
* If @major == 0 this functions will dynamically allocate a major and return
* its number.
*
* If @major > 0 this function will attempt to reserve a device with the given
* major number and will return zero on success.
*
* Returns a -ve errno on failure.
*
* The name of this device has nothing to do with the name of the device in
* /dev. It only helps to keep track of the different owners of devices. If
* your module name has only one type of devices it's ok to use e.g. the name
* of the module here.
*/
int __register_chrdev(unsigned int major, unsigned int baseminor,
¦ ¦ unsigned int count, const char *name,
¦ ¦ const struct file_operations *fops)
{
struct char_device_struct *cd;
struct cdev *cdev;
int err = -ENOMEM; cd = __register_chrdev_region(major, baseminor, count, name);
if (IS_ERR(cd))
return PTR_ERR(cd); cdev = cdev_alloc();
if (!cdev)
goto out2; cdev->owner = fops->owner;
cdev->ops = fops;
kobject_set_name(&cdev->kobj, "%s", name); err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
if (err)
goto out; cd->cdev = cdev; return major ? : cd->major;
out:
kobject_put(&cdev->kobj);
out2:
kfree(__unregister_chrdev_region(cd->major, baseminor, count));
return err;
}
02:fs/char_dev.c
/**
* cdev_init() - initialize a cdev structure
* @cdev: the structure to initialize
* @fops: the file_operations for this device
*
* Initializes @cdev, remembering @fops, making it ready to add to the
* system with cdev_add().
*/
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
memset(cdev, , sizeof *cdev);
INIT_LIST_HEAD(&cdev->list);
kobject_init(&cdev->kobj, &ktype_cdev_default);
cdev->ops = fops;
}
---------------------------------使用--------------------------
static int flashlight_probe(struct platform_device *dev)
{
int ret = , err = ; logI("[flashlight_probe] start ~"); #ifdef ALLOC_DEVNO
ret = alloc_chrdev_region(&flashlight_devno, , , FLASHLIGHT_DEVNAME);
if (ret) {
logI("[flashlight_probe] alloc_chrdev_region fail: %d ~", ret);
goto flashlight_probe_error;
} else {
logI("[flashlight_probe] major: %d, minor: %d ~", MAJOR(flashlight_devno),
¦ ¦MINOR(flashlight_devno));
}
cdev_init(&flashlight_cdev, &flashlight_fops);
flashlight_cdev.owner = THIS_MODULE;
err = cdev_add(&flashlight_cdev, flashlight_devno, );
if (err) {
logI("[flashlight_probe] cdev_add fail: %d ~", err);
goto flashlight_probe_error;
}
#else
#define FLASHLIGHT_MAJOR 242
ret = register_chrdev(FLASHLIGHT_MAJOR, FLASHLIGHT_DEVNAME, &flashlight_fops);
if (ret != ) {
logI("[flashlight_probe] Unable to register chardev on major=%d (%d) ~",
¦ ¦FLASHLIGHT_MAJOR, ret);
return ret;
}
flashlight_devno = MKDEV(FLASHLIGHT_MAJOR, );
#endif flashlight_class = class_create(THIS_MODULE, "flashlightdrv");
if (IS_ERR(flashlight_class)) {
logI("[flashlight_probe] Unable to create class, err = %d ~",
¦ ¦(int)PTR_ERR(flashlight_class));
goto flashlight_probe_error;
}
flashlight_device =
¦ device_create(flashlight_class, NULL, flashlight_devno, NULL, FLASHLIGHT_DEVNAME);
if (NULL == flashlight_device) {
logI("[flashlight_probe] device_create fail ~");
goto flashlight_probe_error;
} /* initialize members */
spin_lock_init(&flashlight_private.lock);
init_waitqueue_head(&flashlight_private.read_wait);
/* init_MUTEX(&flashlight_private.sem); */
sema_init(&flashlight_private.sem, ); flashlight_gpio_init(dev);
logI("[flashlight_probe] Done ~");
return ; flashlight_probe_error:
#ifdef ALLOC_DEVNO
if (err == )
cdev_del(&flashlight_cdev);
if (ret == )
unregister_chrdev_region(flashlight_devno, );
#else
if (ret == )
unregister_chrdev(MAJOR(flashlight_devno), FLASHLIGHT_DEVNAME);
#endif
return -;
}
---
cdev_init和register_chrdev区别的更多相关文章
- register_chrdev、register_chrdev_region以及alloc_chrdev_region之间的区别
register_chrdev:Linux2.6.30之前所用,不用定义cdev:但 如果是register_chrdev 注册的话,这个时候,分配的次设备号,是从0~255,这样子的话,就分配的范围 ...
- misc_register、 register_chrdev 的区别总结
参考: http://longer.spaces.eepw.com.cn/articles/article/item/60415 http://imganquan.org/blog/?p=350 网上 ...
- 转:misc_register、 register_chrdev 的区别总结
杂项设备(misc device) 杂项设备也是在嵌入式系统中用得比较多的一种设备驱动.在 Linux 内核的include/linux目录下有Miscdevice.h文件,要把自己定义的misc d ...
- ⭐register_chrdev、register_chrdev_region以及alloc_chrdev_region之间的区别
register_chrdev:Linux2.6.30之前所用,不用定义cdev:但 如果是register_chrdev 注册的话,这个时候,分配的次设备号,是从0~255,这样子的话,就分配的范围 ...
- cdev_alloc与cdev_init区别
struct cdev *cdev_alloc(void) { struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL); if (p) { ...
- 【整理】--【字符设备】分配设备号register_chrdev_region()、alloc_chrdev_region() 和 register_chrdev()
(1) 分配设备编号,注册设备与注销设备的函数均在fs.h中声明,如下: extern int register_chrdev_region(dev_t,unsigned int,const char ...
- linux驱动---字符设备的注册register_chrdev说起
首先我们在注册函数里面调用了register_chrdev(MEM_MAJOR,"mem",&memory_fops),向内核注册了一个字符设备. 第一个参数是主设备号,0 ...
- udev和devfs的区别
devfs(设备文件系统)是由Linux2.4内核引入的,它的出现主要使得设备驱动程序能够自主管理自己的设备文件.具体来说,devfs具有如下优点: 可以通过程序在设备初始化时在/dev目录下创建设备 ...
- 字符设备之register_chrdev与register_chrdev_region(转)
之前写字符设备驱动,都是使用register_chrdev向内核注册驱动程序中构建的file_operations结构体,之后创建的设备文件,只要是主设备号相同(次设备号不同),则绑定的都是同一个fi ...
随机推荐
- Vue 函数
1.转换为大写字符 .toUpperCase() 2.字符串反转 this.message = this.message.split('').reverse().join('') 3.从index开 ...
- SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs
ORACLE-BASE - ALTER TABLE ... SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs ...
- Spring Boot (9) mybatis全注解化
ORM对比图 框架对比 Spring JDBC Spring Data Jpa Mybatis 性能 性能最好 性能最差 居中 代码量 多 少 多 学习成本 低 高 居中 推荐指数 ❤❤❤ ❤❤❤❤❤ ...
- # --with-http_sub_module模块
作用: http内容替换 语法 第一种语法: sub_filter string:要替换的内容 替换后的内容 这个模块只能替换第一个匹配的字符串,如果需要匹配全部替换,则用到下面的第三种语法配置 第二 ...
- iOS多线程——GCD篇
什么是GCD GCD是苹果对多线程编程做的一套新的抽象基于C语言层的API,结合Block简化了多线程的操作,使得我们对线程操作能够更加的安全高效. 在GCD出现之前Cocoa框架提供了NSObjec ...
- Django学习案例一(blog):六. 开发博客内容页面
目标:某条博客具体内容的展示,可返回博客主页面,可进行评论. 1. 编辑路由 一篇博客,要将其找出来,就需要有一个唯一的标识.Django 的模型中默认有一个唯一的且未自增长的主键,即 id 字段.我 ...
- 菜鸟使用 centOS 安装 redis 并放入service 启动 记录
1.下载redis: wget http://download.redis.io/releases/redis-2.8.17.tar.gz 若wget 不可用,请先安装wget yum install ...
- 《Linux程序设计》笔记(二)shell程序设计
1. 进程树形显示 ps -e f 2. 重定向 > 覆盖文件 >> 附加至文件 1> 标准输出 2> 标准错误输出 0 代表一个程序的标准输入 3. 程序可以在当前目录 ...
- java web设置全局context参数
先在生成的web.xml文件中配置全局参数变量(Parameter:参数) <web-app> <context-param> 设置parameter(参数)的识别名字为adm ...
- java操作Excel的poi的导出Excel表格
页面布局 点击导出用户:触发函数,直接访问后台 后台方法如下: public String export()throws Exception{ Connection con=null; try { c ...