request_threaded_irq()解读

来源:互联网 发布:ios数据持久化方式 编辑:程序博客网 时间:2024/05/22 19:27

http://blog.csdn.net/lamdoc/article/details/7663049

1. 这个中断函数比较有意思,不仅定义了中断处理函数ads7846_hard_irq(),还会创建一个新线程,用来运行指定函数函数ads7846_irq()。具体应用:

[cpp] view plaincopy
  1. err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,  
  2.                    irq_flags, spi->dev.driver->name, ts);  
2. request_threaded_irq()函数,定义在/kernel/irq/manage.c中:

[cpp] view plaincopy
  1. /** 
  2.  *    request_threaded_irq - allocate an interrupt line 
  3.  *    @irq: Interrupt line to allocate 
  4.  *    @handler: Function to be called when the IRQ occurs. 
  5.  *         Primary handler for threaded interrupts 
  6.  *         If NULL and thread_fn != NULL the default 
  7.  *         primary handler is installed 
  8.  *    @thread_fn: Function called from the irq handler thread 
  9.  *         If NULL, no irq thread is created 
  10.  *    @irqflags: Interrupt type flags 
  11.  *    @devname: An ascii name for the claiming device 
  12.  *    @dev_id: A cookie passed back to the handler function 
  13.  * 
  14.  *    This call allocates interrupt resources and enables the 
  15.  *    interrupt line and IRQ handling. From the point this 
  16.  *    call is made your handler function may be invoked. Since 
  17.  *    your handler function must clear any interrupt the board 
  18.  *    raises, you must take care both to initialise your hardware 
  19.  *    and to set up the interrupt handler in the right order. 
  20.  * 
  21.  *    If you want to set up a threaded irq handler for your device 
  22.  *    then you need to supply @handler and @thread_fn. @handler ist 
  23.  *    still called in hard interrupt context and has to check 
  24.  *    whether the interrupt originates from the device. If yes it 
  25.  *    needs to disable the interrupt on the device and return 
  26.  *    IRQ_WAKE_THREAD which will wake up the handler thread and run 
  27.  *    @thread_fn. This split handler design is necessary to support 
  28.  *    shared interrupts. 
  29.  * 
  30.  *    Dev_id must be globally unique. Normally the address of the 
  31.  *    device data structure is used as the cookie. Since the handler 
  32.  *    receives this value it makes sense to use it. 
  33.  * 
  34.  *    If your interrupt is shared you must pass a non NULL dev_id 
  35.  *    as this is required when freeing the interrupt. 
  36.  * 
  37.  *    Flags: 
  38.  * 
  39.  *    IRQF_SHARED        Interrupt is shared 
  40.  *    IRQF_SAMPLE_RANDOM    The interrupt can be used for entropy 
  41.  *    IRQF_TRIGGER_*        Specify active edge(s) or level 
  42.  * 
  43.  */  
  44. int request_threaded_irq(unsigned int irq, irq_handler_t handler,  
  45.              irq_handler_t thread_fn, unsigned long irqflags,  
  46.              const char *devname, void *dev_id)  
  47. {  
  48.     struct irqaction *action;  
  49.     struct irq_desc *desc;  
  50.     int retval;  
  51.     /* 
  52.      * Sanity-check: shared interrupts must pass in a real dev-ID, 
  53.      * otherwise we'll have trouble later trying to figure out 
  54.      * which interrupt is which (messes up the interrupt freeing 
  55.      * logic etc). 
  56.      */  
  57.     if ((irqflags & IRQF_SHARED) && !dev_id)  
  58.         return -EINVAL;  
  59.     desc = irq_to_desc(irq);  
  60.     if (!desc)  
  61.         return -EINVAL;  
  62.     if (desc->status & IRQ_NOREQUEST)  
  63.         return -EINVAL;  
  64.     if (!handler) {  
  65.         if (!thread_fn)  
  66.             return -EINVAL;  
  67.         handler = irq_default_primary_handler;  
  68.     }  
  69.     action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);  //分配irqaction内存  
  70.     if (!action)  
  71.         return -ENOMEM;  
  72.     action->handler = handler;  
  73.     action->thread_fn = thread_fn;  
  74.     action->flags = irqflags;  
  75.     action->name = devname;  
  76.     action->dev_id = dev_id;  
  77.     chip_bus_lock(desc);  
  78.     retval = __setup_irq(irq, desc, action);    //锁上后,这句才是动作  
  79.     chip_bus_sync_unlock(desc);  
  80.     if (retval)  
  81.         kfree(action);  
  82. #ifdef CONFIG_DEBUG_SHIRQ  
  83.     if (!retval && (irqflags & IRQF_SHARED)) {  
  84.         /* 
  85.          * It's a shared IRQ -- the driver ought to be prepared for it 
  86.          * to happen immediately, so let's make sure.... 
  87.          * We disable the irq to make sure that a 'real' IRQ doesn't 
  88.          * run in parallel with our fake. 
  89.          */  
  90.         unsigned long flags;  
  91.         disable_irq(irq);  
  92.         local_irq_save(flags);  
  93.         handler(irq, dev_id);  
  94.         local_irq_restore(flags);  
  95.         enable_irq(irq);  
  96.     }  
  97. #endif  
  98.     return retval;  
  99. }  
  100. EXPORT_SYMBOL(request_threaded_irq);  

3. __setup_irq()定义如下:

[cpp] view plaincopy
  1. /* 
  2.  * Internal function to register an irqaction - typically used to 
  3.  * allocate special interrupts that are part of the architecture. 
  4.  */  
  5. static int  
  6. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)  
  7. {  
  8.     struct irqaction *old, **old_ptr;  
  9.     const char *old_name = NULL;  
  10.     unsigned long flags;  
  11.     int nested, shared = 0;  
  12.     int ret;  
  13.     if (!desc)  
  14.         return -EINVAL;  
  15.     if (desc->irq_data.chip == &no_irq_chip)  
  16.         return -ENOSYS;  
  17.     /* 
  18.      * Some drivers like serial.c use request_irq() heavily, 
  19.      * so we have to be careful not to interfere with a 
  20.      * running system. 
  21.      */  
  22.     if (new->flags & IRQF_SAMPLE_RANDOM) {  
  23.         /* 
  24.          * This function might sleep, we want to call it first, 
  25.          * outside of the atomic block. 
  26.          * Yes, this might clear the entropy pool if the wrong 
  27.          * driver is attempted to be loaded, without actually 
  28.          * installing a new handler, but is this really a problem, 
  29.          * only the sysadmin is able to do this. 
  30.          */  
  31.         rand_initialize_irq(irq);  
  32.     }  
  33.     /* Oneshot interrupts are not allowed with shared */  
  34.     if ((new->flags & IRQF_ONESHOT) && (new->flags & IRQF_SHARED))  
  35.         return -EINVAL;  
  36.     /* 
  37.      * Check whether the interrupt nests into another interrupt 
  38.      * thread. 
  39.      */  
  40.     nested = desc->status & IRQ_NESTED_THREAD;  
  41.     if (nested) {  
  42.         if (!new->thread_fn)  
  43.             return -EINVAL;  
  44.         /* 
  45.          * Replace the primary handler which was provided from 
  46.          * the driver for non nested interrupt handling by the 
  47.          * dummy function which warns when called. 
  48.          */  
  49.         new->handler = irq_nested_primary_handler;  
  50.     }  
  51.     /* 
  52.      * Create a handler thread when a thread function is supplied 
  53.      * and the interrupt does not nest into another interrupt 
  54.      * thread. 
  55.      */  
  56.     if (new->thread_fn && !nested) {  
  57.         struct task_struct *t;  
  58.         t = kthread_create(irq_thread, new"irq/%d-%s", irq,   //kthread_create()就是创建新内核线程的动作了  
  59.                  new->name);  
  60.         if (IS_ERR(t))  
  61.             return PTR_ERR(t);  
  62.         /* 
  63.          * We keep the reference to the task struct even if 
  64.          * the thread dies to avoid that the interrupt code 
  65.          * references an already freed task_struct. 
  66.          */  
  67.         get_task_struct(t);  
  68.         new->thread = t;  
  69.     }  
  70.     /* 
  71.      * The following block of code has to be executed atomically 
  72.      */  
  73.     raw_spin_lock_irqsave(&desc->lock, flags);  
  74.     old_ptr = &desc->action;  
  75.     old = *old_ptr;  
  76.     if (old) {  
  77.         /* 
  78.          * Can't share interrupts unless both agree to and are 
  79.          * the same type (level, edge, polarity). So both flag 
  80.          * fields must have IRQF_SHARED set and the bits which 
  81.          * set the trigger type must match. 
  82.          */  
  83.         if (!((old->flags & new->flags) & IRQF_SHARED) ||  
  84.          ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {  
  85.             old_name = old->name;  
  86.             goto mismatch;  
  87.         }  
  88. #if defined(CONFIG_IRQ_PER_CPU)  
  89.         /* All handlers must agree on per-cpuness */  
  90.         if ((old->flags & IRQF_PERCPU) !=  
  91.          (new->flags & IRQF_PERCPU))  
  92.             goto mismatch;  
  93. #endif  
  94.         /* add new interrupt at end of irq queue */  
  95.         do {  
  96.             old_ptr = &old->next;  
  97.             old = *old_ptr;  
  98.         } while (old);  
  99.         shared = 1;  
  100.     }  
  101.     if (!shared) {  
  102.         irq_chip_set_defaults(desc->irq_data.chip);  
  103.         init_waitqueue_head(&desc->wait_for_threads);  
  104.         /* Setup the type (level, edge polarity) if configured: */  
  105.         if (new->flags & IRQF_TRIGGER_MASK) {  
  106.             ret = __irq_set_trigger(desc, irq,  
  107.                     new->flags & IRQF_TRIGGER_MASK);  
  108.             if (ret)  
  109.                 goto out_thread;  
  110.         } else  
  111.             compat_irq_chip_set_default_handler(desc);  
  112. #if defined(CONFIG_IRQ_PER_CPU)  
  113.         if (new->flags & IRQF_PERCPU)  
  114.             desc->status |= IRQ_PER_CPU;  
  115. #endif  
  116.         desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | IRQ_ONESHOT |  
  117.                  IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);  
  118.         if (new->flags & IRQF_ONESHOT)  
  119.             desc->status |= IRQ_ONESHOT;  
  120.         if (!(desc->status & IRQ_NOAUTOEN)) {  
  121.             desc->depth = 0;  
  122.             desc->status &= ~IRQ_DISABLED;  
  123.             desc->irq_data.chip->irq_startup(&desc->irq_data);  
  124.         } else  
  125.             /* Undo nested disables: */  
  126.             desc->depth = 1;  
  127.         /* Exclude IRQ from balancing if requested */  
  128.         if (new->flags & IRQF_NOBALANCING)  
  129.             desc->status |= IRQ_NO_BALANCING;  
  130.         /* Set default affinity mask once everything is setup */  
  131.         setup_affinity(irq, desc);  
  132.     } else if ((new->flags & IRQF_TRIGGER_MASK)  
  133.             && (new->flags & IRQF_TRIGGER_MASK)  
  134.                 != (desc->status & IRQ_TYPE_SENSE_MASK)) {  
  135.         /* hope the handler works with the actual trigger mode... */  
  136.         pr_warning("IRQ %d uses trigger mode %d; requested %d\n",  
  137.                 irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK),  
  138.                 (int)(new->flags & IRQF_TRIGGER_MASK));  
  139.     }  
  140.     new->irq = irq;  
  141.     *old_ptr = new;  
  142.     /* Reset broken irq detection when installing new handler */  
  143.     desc->irq_count = 0;  
  144.     desc->irqs_unhandled = 0;  
  145.     /* 
  146.      * Check whether we disabled the irq via the spurious handler 
  147.      * before. Reenable it and give it another chance. 
  148.      */  
  149.     if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) {  
  150.         desc->status &= ~IRQ_SPURIOUS_DISABLED;  
  151.         __enable_irq(desc, irq, false);  
  152.     }  
  153.     raw_spin_unlock_irqrestore(&desc->lock, flags);  
  154.     /* 
  155.      * Strictly no need to wake it up, but hung_task complains 
  156.      * when no hard interrupt wakes the thread up. 
  157.      */  
  158.     if (new->thread)  
  159.         wake_up_process(new->thread);  
  160.     register_irq_proc(irq, desc);  
  161.     new->dir = NULL;  
  162.     register_handler_proc(irq, new);  
  163.     return 0;  
  164. mismatch:  
  165. #ifdef CONFIG_DEBUG_SHIRQ  
  166.     if (!(new->flags & IRQF_PROBE_SHARED)) {  
  167.         printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);  
  168.         if (old_name)  
  169.             printk(KERN_ERR "current handler: %s\n", old_name);  
  170.         dump_stack();  
  171.     }  
  172. #endif  
  173.     ret = -EBUSY;  
  174. out_thread:  
  175.     raw_spin_unlock_irqrestore(&desc->lock, flags);  
  176.     if (new->thread) {  
  177.         struct task_struct *t = new->thread;  
  178.         new->thread = NULL;  
  179.         if (likely(!test_bit(IRQTF_DIED, &new->thread_flags)))  
  180.             kthread_stop(t);  
  181.         put_task_struct(t);  
  182.     }  
  183.     return ret;  
  184. }  

0 0
原创粉丝点击