class_create()
struct class *myclass = class_create(THIS_MODULE, “my_device_driver”);
device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “my_device”);
这样的module被加载时,udev daemon就会自动在/dev下创建my_device设备文件。
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/下创建类目录
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
使用udev在/dev/下动态生成设备文件
create_chrdev.c
---------------------------------------------
#include //dev_t
#include //struct cdev
#include //alloc_chrdev_region()
#include //class_create()
dev_t devid;
static struct cdev *led_cdev;
static int led_Major = 0;
static int led_Minor = 0;
static struct class *led_class;
static struct file_operations led_fops = {
.owner = THIS_MODULE,
};
static int __init hello_init(void)
{
int err;
//初始化cdev
led_cdev = cdev_alloc();
cdev_init(led_cdev, &led_fops);
led_cdev->owner = THIS_MODULE;
//动态获取主设备号(dev_t devid中包含"主设备号"和"次设备号"信息)
alloc_chrdev_region(&devid, 66, 1, "led");
led_Major = MAJOR(devid);
led_Minor = MINOR(devid);
printk(KERN_INFO "I was assigned major number %d.\n", led_Major);
printk(KERN_INFO "I was assigned minor number %d.\n", led_Minor);
//注册字符设备 (1)
err = cdev_add(led_cdev, devid, 1);
if (err) {
printk(KERN_NOTICE "Error %d adding device\n", err);
return -1;
}
led_class = class_create(THIS_MODULE, "led_class1");
if (IS_ERR(led_class)) {
printk(KERN_INFO "create class error\n");
return -1;
}
device_create(led_class, NULL, devid, NULL, "led" "%d", MINOR(devid));
return 0;
}
static void __exit hello_exit(void)
{
unregister_chrdev_region(devid, 1);
cdev_del(led_cdev);
device_destroy(led_class, devid);
class_destroy(led_class);
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zengxiaolong ");
MODULE_DESCRIPTION("A sample driver");
MODULE_SUPPORTED_DEVICE("testdevice");
(1) cdev_add()执行后将会在/proc/devices文件中看到"252 led",但是在/dev/目录下没有生成相应的设备文件。直到class_device_create()执行后才会在/dev/目录下生成设备文件led66。而class_create()执行后会在/sys/class/目录下生成led_class1目录
Makefile
---------------------------------------------
all: default
obj-m += create_chrdev.o
default:
make -C /home/zxl/soft/kernel/linux-2.6.22 M=`pwd` modules
clean:
make -C /home/zxl/soft/kernel/linux-2.6.22 M=`pwd` clean