linux 驱动开发 自动创建节点的问题
来源:互联网 发布:万国司考网络课程 编辑:程序博客网 时间:2024/05/16 14:18
自学驱动以来,一直都是在加载模块后采用手动创建节点,虽然这个过程比较简单,毕竟还是有点麻烦,尤其是在调试模块的时候。
#insmod module_name.ko
#mknod /dev/module_name c MAJOR MINOR
#
在2.4里设备文件采用的是devfs,在2.6里已经用udev取代devfs,为解决上面那样手动创建节点的麻烦,我们可以在程序里加上创建节点这项,如下:
以字符设备char_dev为例,在驱动初始化的代码里调用class_create为该设备创建一个class,再为每个设备调用 class_device_create创建对应的设备,这样的module被加载时,undev daemon就会自动在/dev下创建char_dev设备文件。大概方法如下:
struct class *myclass = class_create(THIS_MODULE, “char_dev”);
class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “char_dev”);
class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “char_dev”);
当然,在exit函数中要把创建的class移除:
class_destory(&xxx_dev->cdev);
class_device_desotry(my_class,MKDEV(major_num,0));
下面介绍下函数class_creat和class_device_creat的原型:
class_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
class_create - create a struct class structure
@owner: pointer to the module that is to "own" this struct class
@name: pointer to a string for the name of this class.
在/sys/class/下创建类目录
class_device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *class_device_creat(struct class *cls,
struct class_device *parent,
dev_t devt,
struct device *device,
const char *fmt, ...)
class_device_create - creates a class device and registers it with sysfs
@cls: pointer to the struct class that this device should be registered to.
@parent: pointer to the parent struct class_device of this new device, if any.
@devt: the dev_t for the char device to be added.
@device: a pointer to a struct device that is assiociated with this class device.
@fmt: string for the class device's name
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
class_create - create a struct class structure
@owner: pointer to the module that is to "own" this struct class
@name: pointer to a string for the name of this class.
在/sys/class/下创建类目录
class_device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *class_device_creat(struct class *cls,
struct class_device *parent,
dev_t devt,
struct device *device,
const char *fmt, ...)
class_device_create - creates a class device and registers it with sysfs
@cls: pointer to the struct class that this device should be registered to.
@parent: pointer to the parent struct class_device of this new device, if any.
@devt: the dev_t for the char device to be added.
@device: a pointer to a struct device that is assiociated with this class device.
@fmt: string for the class device's name
void class_destroy(struct class *cls);/*销毁/sys/class下的类 */
void class_device_destroy(struct class *cls, dev_t devt); /*销毁一个类设备*/
参数含义同上
补充:
在Linux2.6中,针对上面的这个问题不同的版本有些修改,使用前要先查看下/.../include/linux/device.h里的函数声明,如我用的是Linux2.6.29,里面就没有class_device_create函数,而直接使用device_create就可以了,而在之前的版本如Linux2.6.15,里面就要用class_device_create函数 。
- linux 驱动开发 自动创建节点的问题
- linux 驱动开发 自动创建节点的问题
- linux 驱动开发 自动创建节点的问题
- linux 驱动开发 自动创建节点的问题
- linux 驱动开发 自动创建节点的问题
- linux 驱动开发 自动创建节点的问题
- linux驱动开发之自动创建设备节点
- Linux驱动开发--自动创建设备文件节点
- linux驱动开发--字符设备:自动创建设备节点
- linux驱动开发之自动创建设备节点
- linux驱动开发--字符设备:自动创建设备节点
- linux驱动开发之字符设备--自动创建设备节点
- linux字符设备驱动之设备节点的自动创建
- Linux驱动中自动创建节点
- Linux驱动模块自动创建设备节点
- 【Linux驱动】自动创建设备节点
- linux驱动之自动创建设备节点
- Linux驱动开发之四-----LED改进测试(增加自动创建设备节点)
- Qt-embedded-linux-opensource-src-4.5.1移植到mini2440
- 移植QT4.5 Segmentation Fault报错解决方法和字体偏小的办法
- HTC超越iPhone 成美线上讨论最热门手机
- Wi-Fi®安全状况
- Spring学习总结9(基于Hibernate的事务管理)
- linux 驱动开发 自动创建节点的问题
- 捕食者物体跟踪算法:未来的计算机操作界面
- 利用pm.utils解决Ubuntu中Load/Unload Cycle Count问题(即ubuntu伤害硬盘)
- MonoDroid学习笔记(十)—— 使用MonoDroid打电话,发短信,发邮件。
- 复杂网络
- OTL介绍
- 正交函数系分解
- 完成端口模型示例
- NSCT采样