新闻  |   论坛  |   博客  |   在线研讨会
Linux-2.6.21 S3c6400中断剖析之二 (原创)-上海嵌入式索漫科技培训教材
xiajiashan | 2012-10-10 14:30:21    阅读:1198   发布文章

作者:下家山(请尊重原创,转载请注明)  http://www.xiajiashan.com  2.2 调用的第二个函数setup_irq

setup_irq的函数原形为

int setup_irq(unsigned int irq, struct irqaction *new)

{}(见kernel/irq/manage.c)

这个函数做的事情就多了:

下面逐句解释:

/*

 * Internal function to register an irqaction - typically used to

 * allocate special interrupts that are part of the architecture.

 */

int setup_irq(unsigned int irq, struct irqaction *new)

{

       struct irq_desc *desc = irq_desc + irq;/* irq_desc是一个结构体数组,是一个全局变量,定义在include/linux/irq.h中,其初始化在后面会讲到*/

       struct irqaction *old, **p;

       const char *old_name = NULL;

       unsigned long flags;

       int shared = 0;

 

       if (irq >= NR_IRQS) /* 定义在/include/asm/irq.h 初始值为128*/

              return -EINVAL;

 

       if (desc->chip == &no_irq_chip)

              return -ENOSYS;

       /*

        * Some drivers like serial.c use request_irq() heavily,

        * so we have to be careful not to interfere with a

        * running system.

        */

       if (new->flags & IRQF_SAMPLE_RANDOM) {

              /*

               * This function might sleep, we want to call it first,

               * outside of the atomic block.

               * Yes, this might clear the entropy pool if the wrong

               * driver is attempted to be loaded, without actually

               * installing a new handler, but is this really a problem,

               * only the sysadmin is able to do this.

               */

              rand_initialize_irq(irq); /*当你在注册驱动时加入了IRQF_SAMPLE_RANDOM标志,则调用此函数,这个函数是内核引入的一个通过中断产生随机数的机制,毛德操写的《Linux内核源代码情景分析上册》P209解释的很清楚*/

       }

 

       /*

        * The following block of code has to be executed atomically

        */

       spin_lock_irqsave(&desc->lock, flags);/*对于中断请求队列的操作是一个原子性操作,不允许其他操作打扰,故加锁,使CPU进入临界区*/

       p = &desc->action;

       old = *p;

       if (old) {/*一般*p应为空(初始化时为空,后面会讲到),因为是第一次注册这个中断(我们大部分情况中断是不去共享的,也就是一个中断不会注册两次),故这里就不熬述了*/

              /*

               * Can't share interrupts unless both agree to and are

               * the same type (level, edge, polarity). So both flag

               * fields must have IRQF_SHARED set and the bits which

               * set the trigger type must match.

               */

              if (!((old->flags & new->flags) & IRQF_SHARED) ||

                  ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {

                     old_name = old->name;

                     goto mismatch;

              }

 

#if defined(CONFIG_IRQ_PER_CPU)

              /* All handlers must agree on per-cpuness */

              if ((old->flags & IRQF_PERCPU) !=

                  (new->flags & IRQF_PERCPU))

                     goto mismatch;

#endif

 

              /* add new interrupt at end of irq queue */

              do {

                     p = &old->next;

                     old = *p;

              } while (old);

              shared = 1;

       }

 

       *p = new;                            /*把新的irq放到队列里*/

#if defined(CONFIG_IRQ_PER_CPU)

       if (new->flags & IRQF_PERCPU)

              desc->status |= IRQ_PER_CPU;

#endif

       /* Exclude IRQ from balancing */

       if (new->flags & IRQF_NOBALANCING)

              desc->status |= IRQ_NO_BALANCING;

 

       if (!shared) {

              irq_chip_set_defaults(desc->chip);/*函数原型见/kernel/irq/chip.c,主要设置几个重要的函数指针,后面专门做解释*/

 

              /* Setup the type (level, edge polarity) if configured: */

              if (new->flags & IRQF_TRIGGER_MASK) {/*注册新irq时如果标志位带触发条件,则执行,我们平常注册一个中断时只会带SA_INTERRUPT和SA_SHIRQ标志,触发条件有专门的函数实现*/

                     if (desc->chip && desc->chip->set_type)

                            desc->chip->set_type(irq,

                                          new->flags & IRQF_TRIGGER_MASK);

                     else

                            /*

                             * IRQF_TRIGGER_* but the PIC does not support

                             * multiple flow-types?

                             */

                            printk(KERN_WARNING "No IRQF_TRIGGER set_type "

                                   "function for IRQ %d (%s)\n", irq,

                                   desc->chip ? desc->chip->name :

                                   "unknown");

              } else

                     compat_irq_chip_set_default_handler(desc);/*检查 新irq对应desc结构体的handle_irq*/

 

              desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |

                              IRQ_INPROGRESS);/*确信这三个位不会置位,其定义见/include/linux/irq.h */

              /*在kernel启动初始化irq_desc 结构体时对desc->status 的设置值为IRQ_NOPROBE | IRQ_NOAUTOEN,故下面的if语句为真 */

              if (!(desc->status & IRQ_NOAUTOEN)) {

                     desc->depth = 0;

                     desc->status &= ~IRQ_DISABLED;/*当然要清这位*/

                     if (desc->chip->startup)/* 在上面已调用irq_chip_set_defaults(desc->chip);,其中就注册了desc->chip->startup 函数指针,见/kernel/irq/chip.c*/

                            desc->chip->startup(irq);/*会调用desc->chip->unmask ,而desc->chip->unmask最终会调用到s3c_irq_unmask,见/arch/arm/plat-s3c24xx/irq-pl192.c ,后面会专门讲*/

                     else

                            desc->chip->enable(irq);

              } else

                     /* Undo nested disables: */

                     desc->depth = 1;

       }

       /* Reset broken irq detection when installing new handler */

       desc->irq_count = 0;

       desc->irqs_unhandled = 0;

       spin_unlock_irqrestore(&desc->lock, flags);

 

       new->irq = irq;

       register_irq_proc(irq);

       new->dir = NULL;

       register_handler_proc(irq, new);

/* register_irq_proc(irq);  register_handler_proc(irq, new);这两句主要是做在文件系统中建立一个与此irq对应的文件相关的事情*/

       return 0;

 

mismatch:

#ifdef CONFIG_DEBUG_SHIRQ

       if (!(new->flags & IRQF_PROBE_SHARED)) {

              printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);

              if (old_name)

                     printk(KERN_ERR "current handler: %s\n", old_name);

              dump_stack();

       }

#endif

       spin_unlock_irqrestore(&desc->lock, flags);

       return -EBUSY;

}

2009-2-13   下家山     写于上海.漕河泾

                                   有什么问题可以给我邮件ximenpiaoxue4016@sina.com或加我群198204885

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
02年接触ARM和ucos,开发过有线和无线图像报警器,IPCamera,人脸识别系统,OCR识别系统,指纹识别系统,05年开始从事Linux及Rtems下WiFi,camera,Ethernet等驱动开发工作,专做嵌入式linux培训,致力于把我十年来的研发经验传授给每一个学员,招人的可以找我,ximenpiaoxue4016@sina.com
推荐文章
最近访客