LINUX interrupt learning notes

Source: Internet
Author: User
LINUX interrupt learning notes-general Linux technology-Linux programming and kernel information. For more information, see the following. 1. interrupted registration and release:

In To implement the interrupt registration interface:

Int request_irq (unsigned int irq,
Irqreturn_t (* handler) (int, void *,
Struct pt_regs *),
Unsigned long flags,
Const char * dev_name,
Void * dev_id );

Void free_irq (unsigned int irq, void * dev_id );

Function parameter description

Unsigned int irq: the interrupt number to be registered
Irqreturn_t (* handler) (int, void *, struct pt_regs *): The entry address of the interrupted service program.
Unsigned long flags: bit mask option related to interrupt management, which has three sets of values:
1. SA_INTERRUPT: a fast interrupt handler. When it is used, all other interrupts on the backend processor are disabled.
2. SA_SHIRQ: the interruption can be shared between devices.
3. SA_SAMPLE_RANDOM: This bit indicates that the interrupt can be contributed to/dev/random.
And the encryption pool used by/dev/urandom)
Const char * dev_name: Device description, indicating that the device is using this interrupt.

Void * dev_id: Used as a pointer to a shared disconnection. it is a unique identifier used to identify a device that is interrupted when it is released and possibly directed to its own private data zone ). This parameter is generally a pointer to the device data structure in a real driver. When an interrupt handler is called, it is passed to the void * dev_id of the interrupt handler. (This is my understanding.) If the interrupt is not shared, dev_id can be set to NULL, but it is a good idea to use this item to point to the device structure. we will see a practical application of dev_id in the "implement a processing" section.

Run the following command to view the interrupt number: "cat/proc/interrupts ".

/Proc/stat records several low-level statistics about system activity, including (but not limited to) the number of interruptions received since the system was started. each line of stat starts with a text string, which is the keyword of the line. The intr mark is what we are looking.

The first number is the total number of all interruptions, while the other one represents a single IRQ line, starting from 0. all counts are collected across all processors in the system. the Snapshot shows that the interrupt number 4 has been used once, although no installation is currently processed. if you are testing the drive request and releasing the interrupt in each open and close loop, you may find/proc/stat is more useful than/proc/interrupts.

The following is an interrupted service program that counts the interruption interval.

Irqreturn_t short_interrupt (int irq, void * dev_id, struct pt_regs * regs)
{
Static long mytime = 0;
Static int I = 0;
Struct net_device * dev = (struct net_device *) dev_id;

If (I = 0 ){
Mytime = jiffies;
} Else
If (I <20 ){
Mytime = jiffies-mytime;
Printk ("Request on IRQ % d time % d \ n", irq, mytime );
Mytime = jiffies;
Printk ("Interrupt on % s ----- % d \ n", dev-> name, dev-> irq );
}

I ++;
Return IRQ_HANDLED;
}
This function only counts the interval between two interruptions, in milliseconds.

Description of function parameters: int irq: the interrupt number is clearly passed here.

Void * dev_id: the ID number of the device. You can obtain the data structure of the device based on the ID number, and then obtain the information and related data of the device. The following describes how to extract network data.

Struct net_device * dev = (struct net_device *) dev_id; (the value of dev_id here is transmitted by the Macro when the registration is interrupted, and is the last parameter for registering the interrupt function. Note)

After that, you can use dev-> name; dev-> irq; to obtain information about the network device. Of course, some other work is required to extract the ip datagram.

Struct pt_regs * regs: it points to a data structure that stores the registers and statuses of the processor before interruption. It is mainly used for program debugging.



Return Value of the interrupt processing function: the return value of the interrupt program is a special type-irqreturn_t. However, the interrupt program returns only two-IRQ_NONE and IRQ_HANDLED values.

/* Irqreturn. h */

# Ifndef _ LINUX_IRQRETURN_H

# Define _ LINUX_IRQRETURN_H

Typedef int irqreturn_t;

/*

* For 2.4.x compatibility, 2.4.x can use

*

* Typedef void irqreturn_t;

* # Define IRQ_NONE

* # Define IRQ_HANDLED

* # Define IRQ_RETVAL (x)

*...... Here, I deleted some of the content that is closely related.

* To mix old-style and new-style irq handler returns.

*

* IRQ_NONE means we didn't handle it.

* After receiving the interrupt signal, the interrupt program finds that this is not the original interrupt signal specified during registration.

* Returned value

* IRQ_HANDLED means that we did have a valid interrupt and handled it.

* An accurate interrupt signal is received and processed correctly.

* IRQ_RETVAL (x) selects on the two depending on x being non-zero (for handled)

*/

# Define IRQ_NONE (0)

# Define IRQ_HANDLED (1)

# Define IRQ_RETVAL (x )! = 0) // This macro only returns 0 or non-0



# Endif

The above content is in linux/irqreturn. h. I have added some comments. I think it can explain the problem.

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.