闲下来的时候看看书
分类:
2012-09-21 20:05:09
创建设备目录:
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任然被支持,但是是一种过时的做法
Linux 2.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
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,
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
PS:
2.6.15中的函数:
Class_device_create(struct class *cls, struct class_device *parent, dev_t devt, struct device *device, const char *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);