misc 设备驱动

ARM 55浏览
#include <linux/kernel.h>
#include <linux/init.h>>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/io.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/uaccess.h>

#define WTBASE 0x10060000
#define WTCON 0x00
#define WTDAT 0x04
#define WTCNT 0x08
#define WTCLR 0x0C

//unsigned long __iomem *wtbase;    4个字节   +1
//void __iomem *wtbase;             1个字节   +4
char *wtbase;                     //1个字节   +4

int demo_open(struct inode *inode, struct file *file)
{
	printk ("%s,%dn",__func__,__LINE__);
	wtbase=ioremap(WTBASE,40);

	writel(readl(wtbase + WTCON) | (250<<8)|(3<<3),wtbase+WTCON); //如果是unsigned int  /long  这里是+1
//	writel(10000,wtbase+WTDAT);   //使用默认的
//	writel(10000,wtbase+WTCNT);   //使用默认的
//	writel(1<<2,wtcon);
	writel(readl(wtbase + WTCON)|(1<<5),wtbase+WTCON);

	printk ("%s,%dn",__func__,__LINE__);
	return 0;
}
int demo_release(struct inode *inode, struct file *file)
{
	iounmap(wtbase);
	printk ("%s,%dn",__func__,__LINE__);
	return 0;
}
ssize_t demo_read(struct file *file, char __user *user, size_t size, loff_t *loff)
{
	int ker_buf=0;
	ker_buf=readl(wtbase + WTCNT); //wtdat 是开始放入的值   wtcnt  是每次放入的值
	
	printk("%dn",ker_buf);
	if(copy_to_user(user,&ker_buf,sizeof(ker_buf)))
	{
		printk("copy_to_user failedn");
		//return -EAGAIN;
	}

//	printk ("%s,%dn",__func__,__LINE__);
	return 4;
}
ssize_t demo_write(struct file *file, const char __user *user, size_t size, loff_t *loff)
{
	printk ("%s,%dn",__func__,__LINE__);
	return 0;
}


const struct file_operations f_fops={
	.owner=THIS_MODULE,
	.open=demo_open,
	.release=demo_release,
	.read=demo_read,
	.write=demo_write,
};


struct miscdevice misc={
	.minor=255,
	.name="demo_misc",
	.fops=&f_fops,
};

static __init int demo_init(void)
{
	misc_register(&misc);
	printk("%s,%dn",__func__,__LINE__);
	return 0;
}

static __exit void demo_exit(void)
{
	misc_deregister(&misc);
	printk("%s,%dn",__func__,__LINE__);
}

module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

misc 设备驱动特点

1. misc设备共享一个主设备号,次设备号不同    register_chrdev_region

2. misc设备自动创建设备节点    class_create / device_create / class_device_create

3. misc设备驱动部分操作方法内核已经实现

4.misc设备驱动实现流程简单