- === udev
- 在设备被发现时自动加载对应驱动模块。(热插拔,设备被发现时,自动创建dev文件,并加载驱动模块)
- 1. vim /udev/Makefile 修改 安装路径和编译器
- prefix = /nfsroot/
- CROSS_COMPILE = /usr/local/arm/3.4.1/bin/arm-linux-
-
- make
- make install
- 2. vim /nfsroot/etc/init.d/rcS 加上
- mount -t tmpfs tmpfs /dev
- /sbin/udevd -d
- /sbin/udevstart
- 开机自动挂载
- ------------------ src -------------------------
- init:
- struct cdev my_cdev;
- struct class *my_class;
- #define DUMMY_MAJOR 255
- #define DUMMY_MINOR 0
- dev_t devno = MKDEV(DUMMY_MAJOR, DUMMY_MINOR);
- cdev_init(&my_cdev, &f_ops);
- cdev_add(&my_cdev, MKDEV(DUMMY_MAJOR, DUMMY_MINOR), 1);
- my_class =class_create(THIS_MODULE, "my_class");
- class_device_create(my_class, MKDEV(DUMMY_MAJOR, DUMMY_MINOR), NULL,"my_dev");
- exit:
- cdev_del(&my_cdev);
- class_device_destroy(my_class, MKDEV(DUMMY_MAJOR, DUMMY_MINOR));
- class_destroy(my_class);
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(void)
- {
- int fd;
- fd = open("/dev/test_udev", O_RDWR);
- if(fd < 0)
- {
- printf("open failed\n");
- }
- return 0;
- }
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- dev_t devno;
- struct cdev my_cdev;
- struct class *my_class;
- int my_open(struct inode *nod, struct file *filp)
- {
- printk("
my_open\n" ); - return 0;
- }
- struct file_operations my_ops = {
- .open = my_open,
- };
- int init_test(void)
- {
- devno = MKDEV(253, 0);
- register_chrdev_region(devno, 1, "test udev");
- cdev_init(&my_cdev, &my_ops);
- //init
- my_class = class_create(THIS_MODULE, "test_udev_class");
- device_create(my_class, NULL, devno, NULL, "%s", "test_udev");
- cdev_add(&my_cdev, devno, 1);
- printk("hello udev\n");
- return 0;
- }
- void exit_test(void)
- {
- cdev_del(&my_cdev);
- device_destroy(my_class, devno);
- class_destroy(my_class);
- unregister_chrdev_region(devno, 1);
- printk("bye\n");
- }
- module_init(init_test);
- module_exit(exit_test);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Richard");
- MODULE_DESCRIPTION("this is first module");
- MODULE_VERSION("v0.1");