閱讀934 返回首頁    go 阿裏雲 go 技術社區[雲棲]


一個有問題的驅動程序

最近我利用網上的一個並口驅動程序,增加自己需要的內容,利用並口電壓來產生中斷,但是出現了問題,困擾了好久得不到解決,所以放上來求大家幫助。

代碼奉上:

#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:

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的係統上運行的.

修改:

ps1.要把並口的pin9和pin10用線連起來,這是目前能改進的地方,但是發現還是不行啊。。。。4.26

ps2.在係統設置中,把parport打開,卸載了係統自帶的parport程序,但是還是不能觸發中斷!。。。5.2

ps3.最近做調試,發現並口電壓不能“拉低”,不知道什麼原因?。。。5.8

最後更新:2017-04-03 19:06:48

  上一篇:go 細說windows的異常處理和實現——結構化異常
  下一篇:go iOS網絡編程-iCloud鍵值數據存儲編程實例