- 论坛徽章:
- 0
|
创建字符设备 生成设备节点
2008年03月23日 星期日 18:10
使用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;
}
class_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);
class_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
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/107072/showart_2139616.html |
|