上次写了一个LED的驱动程序,今天又在此基础上写了一个按键查询的驱动,使用了class结构体,在加载驱动时自动创建了设备文件,很方便
驱动代码如下
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/cdev.h>
#define MYMAJOR 201
#define MYCNT 1
static struct class *seconddrv_class;
static dev_t mydev;
static struct cdev mycdev;
volatile unsigned long *gph2con;
volatile unsigned long *gph2dat;
volatile unsigned long *gph3con;
volatile unsigned long *gph3dat;
static int second_drv_open(struct inode *inode, struct file *file)
{
/* 配置GPH2_3为输入引脚 */
*gph2con &= ~(0xf<<(3*4));
/* 配置GPH3_0,GPH3_1,GPH3_2,GPH3_3为输入引脚 */
*gph3con &= ~((0xf<<(0*4)) | (0xf<<(1*4)) | (0xf<<(2*4)) | (0xf<<(3*4)));
return 0;
}
ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
/* 返回5个引脚的电平 */
unsigned char key_vals[5];
int regval;
if (size != sizeof(key_vals))
return -EINVAL;
/* 读GPH2_3 */
regval = *gph2dat;
key_vals[0] = (regval & (1<<3)) ? 1 : 0;
/* 读GPH3_0,GPH3_1,GPH3_2,GPH3_3 */
regval = *gph3dat;
key_vals[1] = (regval & (1<<0)) ? 1 : 0;
key_vals[2] = (regval & (1<<1)) ? 1 : 0;
key_vals[3] = (regval & (1<<2)) ? 1 : 0;
key_vals[4] = (regval & (1<<3)) ? 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)
{
int ret = 0;
mydev = MKDEV(MYMAJOR,0);
ret = register_chrdev_region(mydev, MYCNT,"yangyun");
if(ret){
printk(KERN_ERR "Unable to register minor\n");
return -EINVAL;
}
printk(KERN_INFO "register_chrdev_region success\n");
cdev_init(&mycdev,&sencod_drv_fops);
ret = cdev_add(&mycdev,mydev,MYCNT);
if(ret){
printk(KERN_ERR "Unable to cdev_add\n");
return -EINVAL;
}
printk(KERN_INFO "cdev_add success\n");
seconddrv_class = class_create(THIS_MODULE, "second_drv");
device_create(seconddrv_class, NULL, MKDEV(MYMAJOR, 0), NULL, "buttons"); /* /dev/buttons */
gph2con = (volatile unsigned long *)ioremap(0xe0200c40, 16);
gph2dat = gph2con + 1;
gph3con = (volatile unsigned long *)ioremap(0xE0200C60, 16);
gph3dat = gph3con + 1;
return 0;
}
static void second_drv_exit(void)
{
cdev_del(&mycdev);
unregister_chrdev_region(mydev, MYCNT);
device_destroy(seconddrv_class, MKDEV(MYMAJOR, 0));
class_destroy(seconddrv_class);
iounmap(gph2con);
iounmap(gph3con);
}
module_init(second_drv_init);
module_exit(second_drv_exit);
MODULE_LICENSE("GPL");
测试代码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* seconddrvtest
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_vals[5];
int cnt = 0;
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
while (1)
{
read(fd, key_vals, sizeof(key_vals));
if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3] || !key_vals[4])
{
printf("%04d key pressed: %d %d %d %d %d\n", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3], key_vals[4]);
}
}
return 0;
}