misc主设备号是10,MISC_MAJOR,初始化和注册在drivers/char/misc.c中进行,实际上是通过register_chrdev完成的,可见misc实际上也是一个cdev字符设备。
-
static int __init misc_init(void)
-
{
-
int err;
-
-
#ifdef CONFIG_PROC_FS
-
proc_create("misc", 0, NULL, &misc_proc_fops);
-
#endif
-
misc_class = class_create(THIS_MODULE, "misc");
-
err = PTR_ERR(misc_class);
-
if (IS_ERR(misc_class))
-
goto fail_remove;
-
-
err = -EIO;
-
if (register_chrdev(MISC_MAJOR,"misc",&misc_fops))
-
goto fail_printk;
-
misc_class->devnode = misc_devnode;
-
return 0;
-
-
fail_printk:
-
printk("unable to get major %d for misc devices\n", MISC_MAJOR);
-
class_destroy(misc_class);
-
fail_remove:
-
remove_proc_entry("misc", NULL);
-
return err;
-
}
-
subsys_initcall(misc_init);
miscdevice,混杂设备,由device结构派生而来。
使用misc子系统注册的字符驱动。
-
#include <linux/init.h>
-
#include <linux/module.h>
-
#include <linux/fs.h>
-
#include <linux/miscdevice.h>
-
-
#define DEVICE_NAME "charmisc"
-
-
static int char_misc_open(struct inode *inode, struct file *file )
-
{
-
try_module_get(THIS_MODULE);
-
printk(KERN_INFO DEVICE_NAME "opened!\n");
-
-
return 0;
-
}
-
-
static int char_misc_release(struct inode *inode, struct file *file )
-
{
-
printk(KERN_INFO DEVICE_NAME "closed!\n");
-
-
module_put(THIS_MODULE);
-
return 0;
-
}
-
-
-
static ssize_t char_misc_read(struct file *file, char *buf,size_t count, loff_t *f_pos)
-
{
-
printk(KERN_INFO DEVICE_NAME "read method!\n");
-
-
return count;
-
}
-
-
static ssize_t char_misc_write(struct file *file, const char *buf, size_t count, loff_t *f_pos)
-
{
-
printk(KERN_INFO DEVICE_NAME "write method!\n");
-
-
return count;
-
}
-
-
static int char_misc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
-
{
-
printk(KERN_INFO DEVICE_NAME "ioctl method!\n");
-
-
return 0;
-
}
-
-
struct file_operations char_misc_fops = {
-
.owner = THIS_MODULE,
-
.read = char_misc_read,
-
.write = char_misc_write,
-
.open = char_misc_open,
-
.release = char_misc_release,
-
.ioctl = char_misc_ioctl
-
};
-
-
/* misc结构体 */
-
static struct miscdevice char_misc = {
-
.minor = MISC_DYNAMIC_MINOR,
-
.name = DEVICE_NAME,
-
.fops = &char_misc_fops,
-
};
-
-
static int __init char_misc_init(void)
-
{
-
int ret;
-
-
ret = misc_register(&char_misc); //注册misc设备
-
if (ret < 0) {
-
printk(KERN_ERR "misc_register error!\n");
-
return -1;
-
}
-
-
return 0;
-
}
-
-
static void __exit char_misc_exit(void)
-
{
-
misc_deregister(&char_misc); //卸载misc设备
-
}
-
-
module_init(char_misc_init);
-
module_exit(char_misc_exit);
-
-
MODULE_LICENSE("GPL");
-
MODULE_AUTHOR("Chenxibing,hibrian.chen@gmail.com");
使用方法:
#insmod char_misc.ko
#ls /dev/charmisc -l
crw------- 1 root root 10, 53 2011-01-16 21:19 /dev/charmisc
主设备号是misc的主设备号10,53是新注册设备的次设备号。
在/sys/class/misc/目录下创建了charmisc目录:
#ls /sys/class/misc/charmisc/
dev power subsystem uevent
#rmmod char_misc
/dev/charmisc设备消失
/sys/class/misc/charmisc/目录消失
阅读(954) | 评论(0) | 转发(0) |