#include
#include
#include
#include
#include
#include
//包含class_create, device_create, ......这些程序
#include
#include
#include
//2.6.39版本内核为此位置
//之前版本的内核位置可能为
//#include
//#include
#include
#include
//加入两个结构体,用来供内核自动创建主设备号
//主要就是在init和exit两个函数中加入了这两个结构体
static struct class *seconddrv_class;
static struct class_device*seconddrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
static int second_drv_open(struct inode *inode, struct file *file)
{
/* 配置GPF1,4,2,0为输入引脚 */
*gpfcon &= ~((0x3<
return 0;
}
ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
/* 返回4个引脚的电平 */
unsigned char key_vals[4];
int regval;
if (size != sizeof(key_vals))
return -EINVAL;
/* 读GPF1,4,2,0 */
regval = *gpfdat;
key_vals[0] = (regval & (1<<1)) ? 1 : 0;
key_vals[1] = (regval & (1<<4)) ? 1 : 0;
key_vals[2] = (regval & (1<<2)) ? 1 : 0;
key_vals[3] = (regval & (1<<0)) ? 1 : 0;
copy_to_user(buf, key_vals, sizeof(key_vals));
return sizeof(key_vals);
}
static struct file_operations sencod_drv_fops = {
.owner =THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = second_drv_open,
.read=second_drv_read,
};
int major;
static int second_drv_init(void)
{
major = register_chrdev(0, "second_drv", &sencod_drv_fops);
seconddrv_class = class_create(THIS_MODULE, "second_drv");
//2.6.39版本的内核,函数变化为
//class_device_create ======> device_create
seconddrv_class_dev = device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons");// /dev/buttons
//地址映射
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;//加的是一个unsigned long的长度,即4个字节,到了0x56000054
return 0;
}
static void second_drv_exit(void)
{
unregister_chrdev(major, "second_drv");
//2.6.39版本的内核,函数变化为
// class_device_unregister ======> device_unregister
device_unregister(seconddrv_class_dev);
class_destroy(seconddrv_class);
iounmap(gpfcon);
}
module_init(second_drv_init);
module_exit(second_drv_exit);
MODULE_LICENSE("GPL");