📄 eintdrv.c
字号:
/************************************** * S3C2410 外部中断2测试驱动程序 * $$$$ 运行在内核空间 * 此程序运行在创维特实验箱上 * 在中断服务程序中点亮或熄灭LED灯 * *************************************/#include <linux/kernel.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/errno.h> /* for -EBUSY */#include <linux/ioport.h> /* for verify_area */#include <linux/init.h> /* for module_init */#include <asm/io.h> /* 输入输出口的地址映射 */#include <asm/uaccess.h> /* 在/home/cvtech/jx2410/linux/include 下可以找到 put[get]_user()及copy_to[from]_user() */#include <asm-arm/arch-s3c2410/S3C2410.h>#include <asm-arm/arch-s3c2410/irqs.h>#include "eintdrv.h"#define DEVICE_NAME "int_dev"static int Device_Open = 0;int led_set = 0x0;volatile unsigned long *led_addr; //ioremap()需要这样定义static void eint2_handler(int irq, void *dev_id, struct pt_regs *regs){ printk("Extern irq 2 handled\n\n"); led_set = ~led_set; *led_addr = led_set; //输出到led灯}/************************************** * 当一个过程要从一个已经打开的设备文件读取 * 就一定会调用本函数 * *************************************/static int device_open(struct inode *inode, struct file *file){ int eint_irq; int ret_val; led_addr = ioremap(0x10000000,4); //将物理地址映射到led_addr printk(("'/dev/eint' device open\n")); //打印一句信息 if (Device_Open) return -EBUSY; //此语句是为了防止重复进入本子程序 Device_Open++; eint_irq = IRQ_EINT2; //获取外部中断号2的值 set_external_irq(eint_irq, EXT_FALLING_EDGE, GPIO_PULLUP_DIS); //set_external_irq(eint_irq, 2, GPIO_PULLUP_DIS); // 在home/cvtech/jx2410/linux/arch/arm/mach-s3c2410/irq.c可以找到定义 // 在 ret_val = request_irq(eint_irq, eint2_handler, 0, "s3c2410 eint 2", 0); // 在/home/cvtech/jx2410/linux/arch/alpha/kernel/irq.c可以找到到定义 if (ret_val < 0) { return ret_val; } return 0;}/************************************** * 当一个过程要关闭设备文件就一定会调用本函数 * 该函数无返回值,因为它不能失败 * *************************************/static int device_release(struct inode *inode, struct file *file){ printk(("'/dev/eint' device release\n")); /* 释放 irqs */ free_irq(IRQ_EINT2,NULL); /* 准备下一次调用 */ Device_Open --; return 0;}/************************************** * 模块声明:需要用到的函数的指针 * *************************************/struct file_operations Fops = { .open = device_open, .release = device_release, };// 初始化模块: 注册字符设备static int __init eint_module_init( void ){ int ret_val; // 注册字符设备 ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Fops); // 若值为负则表示错误 if (ret_val < 0) { printk ("%s failed with %d\n", "Sorry, registering the character device ",ret_val); return ret_val; } printk ("%s The major device number is %d.\n", "Registeration is a success", MAJOR_NUM); printk ("If you want to talk to the device driver,\n"); printk ("you'll have to create a device file. \n"); printk ("We suggest you use:\n"); printk ("mknod /dev/%s c %d 0\n", DEVICE_FILE_NAME,MAJOR_NUM); printk ("The device file name is important, because\n"); printk ("the ioctl program assumes that's the\n"); printk ("file you'll use.\n"); return 0;};// 卸载字符设备: rmmod命令将会调用本函数static void __exit eint_module_cleanup(void){ int ret; // 注销字符设备 ret = unregister_chrdev(MAJOR_NUM, DEVICE_NAME); // 如果有错则报告 if (ret < 0) printk("Error in module_unregister_chrdev: %d\n", ret); else printk("close the char device OK!\n");};module_init(eint_module_init); //insmod命令调用module_exit(eint_module_cleanup); //rmmod命令调用MODULE_LICENSE("GPL");MODULE_AUTHOR("Cvtech Co., Ltd <http://www.cvtech.com.cn>");MODULE_DESCRIPTION("EINT char driver");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -