第二个驱动程序-tq2440

#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/device.h>


MODULE_LICENSE("GPL");

static struct class *seconddrv_class;
static struct class_device *seconddrv_class_dev;

volatile unsigned long *gpfcon;	 	//0x56000050 
volatile unsigned long *gpfdat; 		//0x56000054


int second_drv_open  (struct inode *inode, struct file *file)
{
	*gpfdat &= ~((0x3 < 0*2) | (0x3 < 1*2) | (0x23< 2*2) | (0x3 < 2*4)); 
	return 0;
}

ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	unsigned char key_value[4];
	int regval;

	if (size  < sizeof(key_value) )
	{
		return -EINVAL;
	}
	
	regval = *gpfdat;
	key_value[0] = (regval & (0x1 < 1))? 1:0;
	key_value[1] = (regval & (0x1 < 4))? 1:0;
	key_value[2] = (regval & (0x1 < 2))? 1:0;	
	key_value[2] = (regval & (0x1 < 0))? 1:0;

	copy_to_user( buf,  key_value,  sizeof(key_value) );
	
	return  0;
}

static struct file_operations second_drv_fops =
{
	.owner = THIS_MODULE,
	.open = second_drv_open,
	.read = second_drv_read,
};

int major;
static int second_drv_init()
{
	major = register_chrdev(0,"seconddrv", &second_drv_fops);
	
	seconddrv_class = class_create(THIS_MODULE, "seconddrv");
	if (IS_ERR(seconddrv_class))
		return PTR_ERR(seconddrv_class);

	seconddrv_class_dev = device_create(seconddrv_class, NULL, MKDEV(major,0), NULL, "buttons"); /* /dev/xyz */
	if (unlikely(IS_ERR(seconddrv_class_dev)))
		return PTR_ERR(seconddrv_class_dev);

	gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
	gpfdat = (volatile unsigned long *)ioremap(0x56000054,16);
}

static void second_drv_exit()
{
	unregister_chrdev(major,"seconddrv");
	device_unregister(seconddrv_class_dev);
	class_destroy(seconddrv_class);

	iounmap(gpfcon);
	iounmap(gpfdat);
}

module_init(second_drv_init);

module_exit(second_drv_exit);


MODULE_LICENSE("GPL");



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值