linux驱动 不创建dev,linux内核驱动知识三,自动创建设备文件devfs与udev

Linux2.4内核中devfs

创建设备目录:

devfs_handle_t devfs_mk_dir(devfs_handle_t

dir, const char *name, void info);

创建设备文件:

devfs_handle_t

devfs_register(devfs_handle_t dir, const char * name.

unsigned int flags, unsigned

int major,

unsigned int minor, umode_t mode,

void *ops, void *info) ;

在指定的目录中创建设备文件:dir目录名,为空表示在/dev/目录下创建;name文件名;flags创建标志;major主设备号;minor次设备号;mode创建模式;ops操作函数集;info通常为空

撤销设备文件:

void devfs_unregister(devfs_handlle_t de);

使用实例:

Static devfs_handle_t devfs_handle ;

Devfs_handle = devfs-register(NULL,

DEVICE_NAME, DEVFS_FL_DEFAULT, major, 0 , S_IFCHR | S_IRUSR | S_IWUSR,

&XXX_fops);

Devfs_unregister(devfs_handle);

此方法在linux 2.6任然被支持,但是是一种过时的做法

Linux2.6内核中udev

从linux 2.6.13开始,devfs不复存在,udev成为devfs的替代品。相比devfs,udev(mdev)存在于应用层。利用udev(mdev)来实现设备文件的自动创建 很简单,在驱动代码里调用class_create为该设备创建一个class,在为每个设备调用device_create创建对应的设备。

例1:

struct class *myclass;

static struct device

*dev;

myclass = class_create(THIS_MODULE,“my_device_driver”);

if (IS_ERR(my_class)) {

printk("Err:

failed in creating class.\n");

return ;

}

dev = device_create(myclass,

NULL, MKDEV(major_num, minor_num), NULL,“my_device”);

device_destroy(my_class,

MKDEV(major_num, minor_num));

class_destroy(my_class);

例2:

现在,好使的是device_create,从2.6.18开始可用

struct

device *device_create(struct class *class, struct device *parent,

dev_t devt, const char *fmt, ...)

从2.6.26起又多了一个参数drvdata:the data to be added to the device for callbacks不会用就给个NULL吧.

struct

device *device_create(struct class *class, struct device *parent,

dev_t devt, void *drvdata, const char *fmt, ...)

dev=device_create(usb_class, NULL, test_usb_devno, shortname);

//2.6.26以上// dev=device_create(usb_class, NULL,

test_usb_devno, NULL,shortname);

device_create( my_class, NULL, MKDEV(hello_major, 0),

"hello" "%d", 0 );

如果成功,它将会在/dev目录下产生/dev/hello0设备。

当驱动被加载时,udev(mdev)就会自动在/dev下创建my_device设备文件。

#include

// device_create()

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_create(struct

class        *cls,

struct class_device *parent,

dev_t

devt,

struct device       *device,

constchar

*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

PS:

2.6.15中的函数:

Class_device_create(struct class *cls,

struct class_device *parent, dev_t devt, struct device *device,

constchar *fmt, ...);

Class_devcie_destroy(struct class *class,

dev_t devt);

在2.6.27中变为:

Device_create(struct

class *class, struct device *parent,

dev_t devt, void *drvdata, const char

*fmt, ..);

Device_destroy(struct

class *class, dev_t devt);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值