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 ...
随机推荐
- 20小时掌握网站开发(免费精品htmlcss视频教程)
自己最近研发了一套新的htmlcss教程,并进行了授课实施,视频教程百度云下载链接如下: 视频及案例源码下载地址 本套教程视频需要安装屏幕录像专家软件才能观看,屏幕录像专家下载地址如下: 屏幕录像专家 ...
- informix 通过ADO或ODBC连接提取数据时出现中文乱码的解决方法
最近在做一个项目,是对INFORMIX数据库的数据进行大数据分析,INFORMIX数据库数据有上亿条,没有linux的Root权限和informix数据的生产权限,只能读取.客户要求结果显示在内网wi ...
- 高通处理器手机 解锁Bootloader 教程
目前很多手机都需要解锁Bootloader之后才能进行刷机操作 本篇教程教你如何傻瓜式解锁Bootloader 首先需要在设置-关于手机 找到版本号(个别手机可能是内核版本号,甚至其他) 然后 快 ...
- 省市区县的sql语句——省
/*SQLyog v10.2 MySQL - 5.5.48 : Database - 省市县****************************************************** ...
- 删除git上已经提交的文件
1.先查看有哪些文件可以删除,但是不真执行删除 git rm -r -n job-executor-common/target/* -r 递归移除目录 -n 加上这个参数,执行命令时,是不会删除任何 ...
- dubbo之结果缓存
结果缓存,用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用户加缓存的工作量. lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存. threadlocal 当前线程缓存,比如 ...
- C++程序设计实验安排
2016-2017第二学期C++程序设计的实验时间与地点安排如下表,请大家根据时间按时来上机实验.另外,因为原来安排在4.1的实验因为调休补周一的课,因此挪至周五.另外第4次周六的课,考虑有一些同学有 ...
- jquery radio、 checkbox、 select 操作
转载:http://www.haorooms.com/post/checkandselect $("input[id^='code']");//id属性以code开始的所有inpu ...
- php多进程防止出现僵尸进程
对于用PHP进行多进程并发编程,不可避免要遇到僵尸进程的问题. 僵尸进程是指的父进程已经退出,而该进程dead之后没有进程接受,就成为僵尸进程(zombie)进程.任何进程在退出前(使用exit退出) ...
- vue-属性传值 props
props属性传值 1.传具体的值 string(字符串) number(数值) boolean(布尔) 2.传一个引用 array(数组) object(对象) ----传引用----- 代码 ...