📄 small_keyboard.c
字号:
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <asm/irq.h>
#include <linux/types.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/kernel_stat.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/seq_file.h>#include <asm/system.h>#include <asm/page.h>#include <asm/nios.h>#include <asm/hardirq.h>
#define DEVICE_FILE_NAME "/dev/small_keyboard"#define DEVICE_NAME "small_keyboard"
#define MAJOR_NUM 251#define SUCCESS 0static int keyboard = 0;static int keyboard_value=-1;
unsigned long int Read(unsigned long int add)
{
unsigned char value;
unsigned long int *addr,data;
addr=(unsigned long int*)(add);
data=*addr;
value=(unsigned char)data;
return(value);
}
void Write(unsigned long int add,unsigned char data)
{
unsigned long int *addr;
unsigned char datain;
addr=(unsigned long int*)(add);
datain=(unsigned char)data;
*addr=datain;
}
static irqreturn_t gpio_irq_handler(int irq, void *dev_id,struct pt_regs *reg)
{
unsigned char value; /* unsigned char key[16]; int i;
printk("get the IRQ2\n");
value=Read(0x02010870);
printk("take off IRQ2\n");
value=Read(0x02010871); key[0]=0x8f; key[1]=0x1f; key[2]=0x5f; key[3]=0xf1; key[4]=0x2f; key[5]=0x6f; key[6]=0x9f; key[7]=0x3f; key[8]=0x7f; key[9]=0x0f; key[10]=0x4f; key[11]=0xf5; key[12]=0xf6; key[13]=0xf4; key[14]=0xf3; key[15]=0xf2; key[16]='\0';
for(i=0;i<16;) { if(value==key[i++]) break; } keyboard_value=i-1; printk("key=%d\n",keyboard_value);*/
printk("get the IRQ2\n");
value=Read(0x02010870);
printk("take off IRQ2\n");
value=Read(0x02010871);
printk("value is 0x%02lx.\n",value);
return 0;
}
/*
* Function 1:device_open
*/
static int keyboard_open(struct inode *inode, struct file *file)
{
if (keyboard)
return -EBUSY;
keyboard++;
try_module_get(THIS_MODULE);
return SUCCESS;
}
/*
* Function 2:device_release
*/
static int keyboard_release(struct inode *inode, struct file *file)
{
keyboard--;
free_irq(2, NULL);
module_put(THIS_MODULE);
return SUCCESS;
}
/*
* Function 3:device_read
*/
static ssize_t keyboard_read(struct file *file,char __user * buffer,size_t length,loff_t * offset)
{
int i=keyboard_value; keyboard_value=-1; printk("i=%d\n",i); return i;
}struct file_operations Keyboard_Fops =
{
.read = keyboard_read,
.open = keyboard_open,
.release = keyboard_release,
};
int __init ite_gpio_init(void)
{
int ret_val;
ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Keyboard_Fops);
if (ret_val < 0) {
printk(KERN_ALERT "%s failed with %d\n",
"Sorry, registering the character device ", ret_val);
return ret_val;
} if (request_irq(2, gpio_irq_handler, 0, DEVICE_NAME, NULL)) {
printk("IRQ2 request error!\n");
return 0;
}
printk("IRQ2 request success!\n");
printk(KERN_INFO "Device %s registered\n", DEVICE_FILE_NAME); return 0;
}
static void __exit ite_gpio_exit(void)
{
int ret;
ret = unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
if (ret < 0)
printk(KERN_ALERT "Error: unregister_chrdev: %d\n", ret);
}
module_init(ite_gpio_init);
module_exit(ite_gpio_exit);#define author "HLT of TCB: Department 2"
#define description "A driver for small keyboard device of the satellite project."
MODULE_AUTHOR(author);
MODULE_DESCRIPTION(description);
MODULE_SUPPORTED_DEVICE(DEVICE_NAME);
MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -