Interrupt module example

Source: Internet
Author: User

 


Application and release interruption:
Register an interrupt: The request_irq () function is used to register an interrupt handler, this is because the actual service interruption will wait until the initialization program of a specific device registers the service interruption program with the system through this function, it can be executed only when it is attached to an interrupt request queue. The source code is as follows:


536int request_irq (unsigned int IRQ, irq_handler_t handler,
537 unsigned long irqflags, const char * devname, void * dev_id)
538 {
539 struct irqaction * Action;/* create a structure and add it to the struct irqaction queue of the corresponding IRQ Number */
540 int retval;/* return value */
541
//... Omitted

554 if (irqflags & ir1__shared )&&! Dev_id)
555 return-einval;
556 if (IRQ> = nr_irqs)
557 return-einval;
558 if (irq_desc [IRQ]. Status & irq_norequest)
559 return-einval;
560 if (! Handler)
561 return-einval;
562
563 action = kmalloc (sizeof (struct irqaction), gfp_atomic );
564 if (! Action)
565 return-enomem;
566
567 action-> handler = handler;
568 action-> flags = irqflags;
569 cpus_clear (action-> mask );
570 action-> name = devname;
571 action-> next = NULL;
572 action-> dev_id = dev_id;
573
574 select_smp_affinity (IRQ );
575
576 # ifdef config_debug_shirq
577 if (irqflags & ir1__shared ){
//....

584 unsigned long flags;
585
586 local_irq_save (flags );
587 handler (IRQ, dev_id );
588 local_irq_restore (flags );
589}
590 # endif
591
592 retval = setup_irq (IRQ, action );
593 if (retval)
594 kfree (action );
595
596 return retval;
597}

Function parameters:
Unsigned int IRQ: the interrupt number. Some devices are pre-specified. Most of them are automatically obtained or dynamically determined.
Handler: pointer to the interrupt handler. When the interrupt occurs, the kernel calls the handler to point to the handler.
Irqflags: It can be 0, sa_interrupt, sa_sample_random, and sa_shirq.
Dev_name: name of the hardware that generates an interruption.
Dev_id: used to share the interrupt number.
Note: using this function may cause sleep, so it cannot be used in the interrupt context.
This function is equivalent to the constructor in C ++. If the constructor succeeds, 0 is returned. Otherwise, non-0 is returned.
The first five if statements are used for error determination. They are used in sequence:
1. If shared interrupt is used but dev_id is not provided, an error is returned.
2. The maximum interrupt number is exceeded.
3. the interrupt number has been used and is not shared.
4 handler is empty
5. Memory not applied
Next, initialize the action structure based on the parameters passed in by the function from row 3, and then register the irqaction struct of the interrupt to the irqactiona queue by calling setup_irq.

Release the void free () function. Original Form:
Void free_irq (unsigned int IRQ, void * dev_id );
Release is in two cases: when the process is not shared, the disconnection will be disabled while the process is released. When sharing, this function will only delete the interrupt handler specified by the dev_id parameter in the disconnection.
Note: This function should be called in the process context.
When the interrupt service program is registered, the handler parameter is the interrupt service program to be registered. This function is compiled by yourself and is defined as follows:
Static irqreturn_t my_handler (int irq, void * dev_id );
It is defined as static because the interrupt handler is not directly called by the code in other files.
Then let's look at the returned value irqreturn_t, which is actually int type, to be compatible with previous versions.
IRQ: the interrupt line number. This flag is useful before dev_id is available. It is only used when printing logs.
Dev_id: Unique identifier of a device. It identifies multiple devices that share the same interrupt handler.
The function returns two values to indicate whether the user has actually handled an interrupt. They are:
Irq_none: If the interrupt handler detects an interrupt and finds that the device corresponding to the interrupt handler is not the device specified during registration using the request_irq () function, this value is returned.
Irq_handler: this value is returned when the interrupt handler is correctly called and he detects that he is calling the corresponding device.
The following is an example:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>

static int irq;
static char *interface;

Module_param (interface, CHARP, 0644); // a macro In the 2.6 kernel, indicating that this parameter must be provided when a module is inserted.

Module_param (IRQ, Int, 0644 );
Int dev_id = 127; // device ID

Static irqreturn_t myinterrupt (int irq, void * dev_id) // interrupt the service program, written by myself

{
Static int COUNT = 0;
If (count <10 ){
Printk ("interrupt % d, % d/N", IRQ, * (int *) dev_id );
Count ++;
}
Return irq_none;
}

Static int _ init myirqtest_init (void)
{
Printk ("My module worked! /N ");
If (request_irq (IRQ, & myinterrupt, ir1__shared, interface, & dev_id) {// The registration is interrupted. The parameter meanings are described earlier.

Printk (kern_err "myirqtest: cannot register IRQ % d/N", IRQ );
Return-EIO;
}
Printk ("% s request on IRQ % d succeede dev_id is: % d/N", interface, IRQ, dev_id );
Return 0;
}
Static void _ exit myirqtest_exit (void)
{
Printk ("unloading my module./N ");
Free_irq (IRQ, & IRQ );
Printk ("freeing IRQ % d/N", IRQ );
}
Module_init (myirqtest_init );
Module_exit (myirqtest_exit );
Module_license ("GPL ");

The above example involves module programming, can refer to here: http://www.kerneltravel.net /? Page_id = 8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.