最近我利用網上的一個並口驅動程序,增加自己需要的內容,利用並口電壓來產生中斷,但是出現了問題,困擾了好久得不到解決,所以放上來求大家幫助。
代碼奉上:
[cpp]
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/interrupt.h>
#include <asm/io.h> /* outb */
#define MODULE_VERSION "1.0"
#define MODULE_NAME "interrupt_latency_x86"
int interruptcount = 0;
struct timeval tv1, tv2; /* do_gettimeofday fills these */
#define SPPDATAPORT 0x378
#define SPPSTATUSPORT (SPPDATAPORT + 1)
#define SPPCONTROLPORT (SPPDATAPORT + 2)
#define SSPINTERRUPTENABLE 0x10
#define INTERRUPT 7
static struct proc_dir_entry *interrupt_latency_file;
/*
* function interrupt_interrupt_latency
* This function is the interrupt handler for interrupt 7. It sets the tv2
* structure using do_gettimeofday. It then deasserts D7.
*/
void interrupt_interrupt_latency(int irq, void *dev_id, struct pt_regs *regs)
{
do_gettimeofday(&tv2);
outb(0x00,SPPDATAPORT); /* deassert the interrupt signal */
interruptcount++;
}
/*
* function proc_read_interrupt_latency
* The kernel executes this function when a read operation occurs on
* /proc/interrupt_latency. This function sets the tv1 structure. It asserts
* D7 which should immediately cause interrupt 7 to occur. The handler
* records tv2 and deasserts D7. This function returns the time differential
* between tv2 and tv1.
*/
static int proc_read_interrupt_latency(char *page, char **start, off_t off,int count, int *eof, void *data)
{
int len;
do_gettimeofday(&tv1);
outb(0x80,SPPDATAPORT); /* assert the interrupt signal */
len = sprintf(page, "Start %9i.%06i\nFinish %9i.%06i\nLatency %17i\n\Count %19i\n",(int)tv1.tv_sec, (int)tv1.tv_usec,
(int)tv2.tv_sec, (int)tv2.tv_usec, (int)(tv2.tv_usec - tv1.tv_usec),interruptcount);
return len;
}
/*
* function init_interrupt_latency
* This function creates the /proc directory entry interrupt_latency. It
* also configures the parallel port then requests interrupt 7 from Linux.
*/
static int __init init_interrupt_latency(void)
{
int rv = 0;
interrupt_latency_file = create_proc_entry("interrupt_latency", 0444, NULL);
if(interrupt_latency_file == NULL){
return -ENOMEM;
}
interrupt_latency_file->data = NULL;
interrupt_latency_file->read_proc = &proc_read_interrupt_latency;
interrupt_latency_file->write_proc = NULL;
/* request interrupt from linux */
rv = request_irq(INTERRUPT, interrupt_interrupt_latency, 0, "interrupt_latency",NULL);
printk("rv value is %d",rv);
if(rv){
printk("Can't get interrupt %d\n", INTERRUPT);
/* remove the proc entry on error */
remove_proc_entry("interrupt_latency", NULL);
printk("i am in rv remove\n");
}
/* enable parallel port interrupt generation */
outb(SSPINTERRUPTENABLE,SPPCONTROLPORT);
/* deassert the interrupt signal */
outb(0x00,SPPDATAPORT);
/* everything initialized */
printk(KERN_INFO "%s %s initialized\n",MODULE_NAME, MODULE_VERSION);
return 0;
}
/*
* function cleanup_interrupt_latency
* This function frees interrupt 7 then removes the /proc directory entry
* interrupt_latency.
*/
static void __exit cleanup_interrupt_latency(void)
{
/* disable parallel port interrupt reporting */
outb(0x00,SPPCONTROLPORT);
/* free the interrupt */
free_irq(INTERRUPT,NULL);
remove_proc_entry("interrupt_latency", NULL);
printk(KERN_INFO "%s %s removed\n", MODULE_NAME, MODULE_VERSION);
}
module_init(init_interrupt_latency);
module_exit(cleanup_interrupt_latency);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZXL");
MODULE_DESCRIPTION("interrupt_latency proc module");
EXPORT_NO_SYMBOLS;
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/interrupt.h>
#include <asm/io.h> /* outb */
#define MODULE_VERSION "1.0"
#define MODULE_NAME "interrupt_latency_x86"
int interruptcount = 0;
struct timeval tv1, tv2; /* do_gettimeofday fills these */
#define SPPDATAPORT 0x378
#define SPPSTATUSPORT (SPPDATAPORT + 1)
#define SPPCONTROLPORT (SPPDATAPORT + 2)
#define SSPINTERRUPTENABLE 0x10
#define INTERRUPT 7
static struct proc_dir_entry *interrupt_latency_file;
/*
* function interrupt_interrupt_latency
* This function is the interrupt handler for interrupt 7. It sets the tv2
* structure using do_gettimeofday. It then deasserts D7.
*/
void interrupt_interrupt_latency(int irq, void *dev_id, struct pt_regs *regs)
{
do_gettimeofday(&tv2);
outb(0x00,SPPDATAPORT); /* deassert the interrupt signal */
interruptcount++;
}
/*
* function proc_read_interrupt_latency
* The kernel executes this function when a read operation occurs on
* /proc/interrupt_latency. This function sets the tv1 structure. It asserts
* D7 which should immediately cause interrupt 7 to occur. The handler
* records tv2 and deasserts D7. This function returns the time differential
* between tv2 and tv1.
*/
static int proc_read_interrupt_latency(char *page, char **start, off_t off,int count, int *eof, void *data)
{
int len;
do_gettimeofday(&tv1);
outb(0x80,SPPDATAPORT); /* assert the interrupt signal */
len = sprintf(page, "Start %9i.%06i\nFinish %9i.%06i\nLatency %17i\n\Count %19i\n",(int)tv1.tv_sec, (int)tv1.tv_usec,
(int)tv2.tv_sec, (int)tv2.tv_usec, (int)(tv2.tv_usec - tv1.tv_usec),interruptcount);
return len;
}
/*
* function init_interrupt_latency
* This function creates the /proc directory entry interrupt_latency. It
* also configures the parallel port then requests interrupt 7 from Linux.
*/
static int __init init_interrupt_latency(void)
{
int rv = 0;
interrupt_latency_file = create_proc_entry("interrupt_latency", 0444, NULL);
if(interrupt_latency_file == NULL){
return -ENOMEM;
}
interrupt_latency_file->data = NULL;
interrupt_latency_file->read_proc = &proc_read_interrupt_latency;
interrupt_latency_file->write_proc = NULL;
/* request interrupt from linux */
rv = request_irq(INTERRUPT, interrupt_interrupt_latency, 0, "interrupt_latency",NULL);
printk("rv value is %d",rv);
if(rv){
printk("Can't get interrupt %d\n", INTERRUPT);
/* remove the proc entry on error */
remove_proc_entry("interrupt_latency", NULL);
printk("i am in rv remove\n");
}
/* enable parallel port interrupt generation */
outb(SSPINTERRUPTENABLE,SPPCONTROLPORT);
/* deassert the interrupt signal */
outb(0x00,SPPDATAPORT);
/* everything initialized */
printk(KERN_INFO "%s %s initialized\n",MODULE_NAME, MODULE_VERSION);
return 0;
}
/*
* function cleanup_interrupt_latency
* This function frees interrupt 7 then removes the /proc directory entry
* interrupt_latency.
*/
static void __exit cleanup_interrupt_latency(void)
{
/* disable parallel port interrupt reporting */
outb(0x00,SPPCONTROLPORT);
/* free the interrupt */
free_irq(INTERRUPT,NULL);
remove_proc_entry("interrupt_latency", NULL);
printk(KERN_INFO "%s %s removed\n", MODULE_NAME, MODULE_VERSION);
}
module_init(init_interrupt_latency);
module_exit(cleanup_interrupt_latency);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZXL");
MODULE_DESCRIPTION("interrupt_latency proc module");
EXPORT_NO_SYMBOLS;Makefile:
[cpp] view plaincopyprint?obj-m := interrupt_latency_x86.o
KDIR := /lib/modules/2.6.33.7.2-rt30/build
PWD := $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.o *.ko *.mod.c Modules.symvers modules.order
obj-m := interrupt_latency_x86.o
KDIR := /lib/modules/2.6.33.7.2-rt30/build
PWD := $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.o *.ko *.mod.c Modules.symvers modules.order 編譯成功後,insmod,dmesg後均正常,但是cat /proc/interrupt_latency後,就只有tv1的開始時間,沒有結束時間,也就是說中斷沒有被觸發,但是代碼中已經在pin口寫高地址了,不知道什麼問題,有待解決!!!
PS:我是在i386的系統上運行的.