linux Do-it-yourself 探测

探测也可以在驱动自身实现没有太大麻烦. 它是一个少有的驱动必须实现它自己的探测, 但是看它是如何工作的能够给出对这个过程的内部认识. 为此目的, short 模块进行 do- it-yourself 的 IRQ 线探测, 如果它使用 probe=2 加载.

这个机制与前面描述的相同: 使能所有未使用的中断, 接着等待并观察发生什么. 我们能 够, 然而, 利用我们对设备的知识. 常常地一个设备能够配置为使用一个 IRQ 号从 3 个 或者 4 个一套; 只探测这些 IRQ 使我们能够探测正确的一个, 不必测试所有的可能中断.

short 实现假定 3, 5, 7, 和 9 是唯一可能的 IRQ 值. 这些数实际上是一些并口设备允 许你选择的数.

下面的代码通过测试所有"可能的"中断并且查看发生的事情来探测中断. trials 数组列 出要尝试的中断, 以 0 作为结尾标志; tried 数组用来跟踪哪个处理实际上被这个驱动 注册.

int trials[] =

{

};


3, 5, 7, 9, 0

int tried[]  = {0, 0, 0, 0, 0}; int i, count = 0;

/*

*  install the probing handler for all possible lines. Remember

*  the result (0 for success, or -EBUSY) in order to only free

*  what has been acquired */ for (i = 0; trials[i]; i++)

tried[i] = request_irq(trials[i], short_probing,

SA_INTERRUPT, "short probe", NULL);

do

{

short_irq = 0; /* none got, yet */ outb_p(0x10,short_base+2); /* enable */ outb_p(0x00,short_base); outb_p(0xFF,short_base); /* toggle the bit */ outb_p(0x00,short_base+2); /* disable */ udelay(5); /* give it some time */

/* the value has been set by the handler */ if (short_irq == 0) { /* none of them? */

printk(KERN_INFO "short: no irq reported by probe\n");

}

/*

*  If more than one line has been activated, the result is

*  negative. We should service the interrupt (but the lpt port

*  doesn't need it) and loop over again. Do it at most 5 times

*/

} while (short_irq <=0 && count++ < 5);

/* end of loop, uninstall the handler */ for (i = 0; trials[i]; i++)

if (tried[i] == 0)

free_irq(trials[i], NULL);

if (short_irq < 0)

printk("short: probe failed %i times, giving up\n", count);

你可能事先不知道"可能的" IRQ 值是什么. 在这个情况, 你需要探测所有空闲的中断, 不是限制你自己在几个 trials[]. 为探测所有的中断, 你不得不从 IRQ 0 到 IRQ NR_IRQS-1 探测, 这里 NR_IRQS 在 <asm/irq.h> 中定义并且是独立于平台的.

现在我们只缺少探测处理自己了. 处理者的角色是更新 short_irq, 根据实际收到哪个中 断. short_irq 中的 0 值意味着"什么没有", 而一个负值意味着"模糊的". 这些值选择 来和 probe_irq_off 相一致并且允许同样的代码来调用任一种 short.c 中的探测.

irqreturn_t short_probing(int irq, void *dev_id, struct pt_regs *regs)

{

if (short_irq == 0) short_irq = irq;  /* found */

if (short_irq != irq) short_irq = -irq; /* ambiguous */ return IRQ_HANDLED;

}

处理的参数在后面描述. 知道 irq 是在处理的中断应当是足够的来理解刚刚展示的函数.

猜你喜欢

转载自www.cnblogs.com/fanweisheng/p/11142279.html