Linux interrupt-kernel interrupt programming

Source: Internet
Author: User
Linux interrupt kernel programming preface after analyzing the basic principle of interrupt, you can write a kernel interrupt program to experience the following, or you can continue to learn more about Linux interrupt kernel programming.
Preface
After analyzing the basic principle of interrupt, you can write a kernel interrupt program to experience the following, or you can continue to learn more about the kernel interrupt execution process.
I. kernel interrupt program:
Let's take a look at the program:
Before reading the program, you should be familiar with how to program modules and understand the usage of module_pararm. If you are not familiar with it, you can see that module_param () and Linux kernel module programming are not explained here.
1. program interrupt. c
[C-sharp]View plaincopy
1/* 2 * file name: interrupt. c3 * atuthor: john 4 */5 # include 6 # include 7 # include 8 # include 9 10 MODULE_LICENSE ("GPL"); 11 static int irq; 12 char * interface; 13 static irqreturn_t myirq_handler (int irq, void * dev ); 1415 static int _ init myirq_init (void) 16 {17 printk ("the module is working! /N "); 18 printk (" the irq is ready for working! /N "); 19 if (request_irq (irq, myirq_handler, ir1__shared, interface, & irq )) {20 printk (KERN_ERR "% s interrrupt can't register % d IRQ/n", interface, irq); 21 return-EIO; 22} 23 printk ("% s request % d IRQ/n", interface, irq); 24 return 0; 25} 26 static irqreturn_t myirq_handler (int irq, void * dev) 27 {28 printk ("% d IRQ is working/n", irq); 29 return IRQ_NONE; 30} 31 static void _ exit myirq_exit (void) 32 {33 prin Tk ("the module is leaving! /N "); 34 printk (" the irq is bye! /N "); 35 free_irq (irq, & irq); 36 printk (" % s interrupt free % d IRQ/n ", interface, irq ); 37 38} 39 module_init (myirq_init); 0 module_exit (myirq_exit); 41 module_param (interface, charp, 0644); 42 module_param (irq, int, 0644); 43 [C-sharp]View plaincopy1 obj-m: = tiger. o 23 CURRENT_PATH: = $ (shell pwd) 4 VERSION_NUM: = $ (shell uname-r) 5 LINUX_PATH: =/usr/src/linux-headers-$ (VERSION_NUM) 67 8 all: 9 make-C $ (LINUX_PATH) M = $ (CURRENT_PATH) modules 10 clean: 11 make-C $ (LINUX_PATH) M = $ (CURRENT_PATH) clean [C-sharp]View plaincopy/** These flags used only by the kernel as part of the * irq handling routines. ** ir1__disabled-keep irqs disabled when calling the action handler * Your-irq is used to feed the random generator * ir1__shared-allow sharing the irq among several devices * reset-set by callers when they benchmark Ct sharing mismatches to occur * random-Flag to mark this interrupt as timer interrupt * random-Interrupt is per cpu * random-Flag to exclude this interrupt from irq balancing * random-response is used for polling (only the interrupt that is * registered first in an shared interrupt is considered for * performance reasons) */# define ir1__disabled 0x00000020 # define limit 0x00000040 # define ir1__shared 0x00000080 # define ir1__probe_shared 0x00000100 # define ir1__timer 0x00000200 # define ir1__percpu 0x00000400 # define IRQF_NOBALANCING 0x00000800 # define ir1__irqpoll 0x00001000
[C-sharp]View plaincopy113struct irqaction {114 irq_handler_t handler; 115 unsigned long flags; 116 const char * name; 117 void * dev_id; 118 struct irqaction * next; 119 int irq; 120 struct proc_dir_entry * dir; 121 irq_handler_t thread_fn; 122 struct task_struct * thread; 123 unsigned long thread_flags; 124}; 125 [C-sharp]View irq_desc {176 unsigned int irq; 177 struct timer_rand_state * failed; 178 unsigned int * kstat_irqs; 179 # ifdef CONFIG_INTR_REMAP 180 struct failed * failed; 181 # endif 182 handle handle_irq; 183 struct irq_chip * chip; 184 struct msi_desc * msi_desc; 185 void * handler_data; 186 void * chip_data; 187 struct irqaction * action;/* IRQ action list */188 unsigned int status; /* IRQ status */189 190 unsigned int depth;/* nested irq disables */191 unsigned int wake_depth;/* nested wake enables */192 unsigned int irq_count; /* For detecting broken IRQs */193 unsigned long last_unhandled;/* Aging timer for unhandled count */194 unsigned int irqs_unhandled; 195 raw_spinlock_t lock; 196 # ifdef always affinity; 198 const struct cpumask * affinity_hint; 199 unsigned int node; 200 # ifdef define cpumask_var_t pending_mask; 202 # endif203 # endif 204 atomic_t threads_active; 205 bytes wait_for_threads; 206 # ifdef explain struct proc_dir_entry * dir; 208 # endif209 const char * name; 210 }____ limit; 211 212 extern void limit (struct irq_desc * old_desc, 213 struct irq_desc * desc, int node); 214 extern void arch_free_chip_data (struct irq_desc * old_desc, struct irq_desc * desc); 215 216 # ifndef explain struct irq_desc [NR_IRQS]; [C-sharp]View plaincopy111struct irq_chip {112 const char * name; 113 unsigned int (* startup) (unsigned int irq); 114 void (* shutdown) (unsigned int irq ); 115 void (* enable) (unsigned int irq); 116 void (* disable) (unsigned int irq); 117 118 void (* ack) (unsigned int irq ); 119 void (* mask) (unsigned int irq); 120 void (* mask_ack) (unsigned int irq); 121 void (* unmask) (unsigned int irq ); 122 void (* eoi) (unsigned int irq); 123 124 void (* end) (unsigned int irq); 125 int (* set_affinity) (unsigned int irq, 126 const struct cpumask * dest); 127 int (* retrigger) (unsigned int irq); 128 int (* set_type) (unsigned int irq, unsigned int flow_type ); 129 int (* set_wake) (unsigned int irq, unsigned int on); 130131 void (* bus_lock) (unsigned int irq); 132 void (* bus_sync_unlock) (unsigned int irq ); 133 134/* Currently used only by UML, might disappear one day. */135 # ifdef CONFIG_IRQ_RELEASE_METHOD 136 void (* release) (unsigned int irq, void * dev_id); 137 # endif 138/* 139 * For compatibility, -> typename is copied into-> name. 140 * Will disappear.141 */142 const char * typename; 143}; 144
[C-sharp]View outputs (unsigned int irq, irq_handler_t handler, unsigned long flags, 136 const char * name, void * dev) 137 {138 return request_threaded_irq (irq, handler, NULL, flags, name, dev); 139} 140 [C-sharp]View plaincopy1006/** 1007 * request_threaded_irq-allocate an interrupt line1008 * @ irq: Interrupt line to allocate 1009 * @ handler: function to be called when the IRQ occurs.1010 * Primary handler for threaded interrupts 1011 * If NULL and thread_fn! = NULL the default1012 * primary handler is installed 1013 * @ thread_fn: Function called from the irq handler thread1014 * If NULL, no irq thread is created 1015 * @ irqflags: interrupt type flags1016 * @ devname: An ascii name for the claiming device 1017 * @ dev_id: A cookie passed back to the handler function1018 * 1019 * This call allocates interrupt resources and enables the1020 * interrupt line and IRQ handling. from the point this 1021 * call is made your handler function may be invoked. since1022 * your handler function must clear any interrupt the board 1023 * raises, you must take care both to initialise your hardware1024 * and to set up the interrupt handler in the right order. 1025*1026 * If you want to set up a threaded irq handler for your device 1027 * then you need to supply @ handler and @ thread_fn. @ handler ist1028 * still called in hard interrupt context and has to check 1029 * whether the interrupt originates from the device. if yes it1030 * needs to disable the interrupt on the device and return 1031 * IRQ_WAKE_THREAD which will wake up the handler thread and run1032 * @ thread_fn. this split handler design is necessary to support 1033 * shared interrupts.1034 * 1035 * Dev_id must be globally unique. normally the address of the1036 * device data structure is used as the cookie. since the handler 1037 * Since es this value it makes sense to use it.1038 * 1039 * If your interrupt is shared you must pass a non NULL dev_id1040 * as this is required when freeing the interrupt. 1041*1042 * Flags: 1043*1044 * ir1__shared Interrupt is shared 1045 * ir1__sample_random The interrupt can be used for entropy1046 * ir1__trigger _ * Specify active edge (s) or level 1047*1048 */ [C-sharp]View plaincopy1049int trim (unsigned int irq, callback handler, 1050 irq_handler_t thread_fn, unsigned long irqflags, 1051 const char * devname, void * dev_id) 1052 {1053 struct irqaction * action; 1054 struct irq_desc * desc; 1055 int retval; 10561057/* 1058 * Sanity-check: shared interrupts must pass in a real dev-ID, 1059 * otherwise we'll have trouble later trying to figure out 106 0 * which interrupt is which (messes up the interrupt freeing1061 * logic etc). 1062 */1063 if (irqflags & ir1__shared )&&! Dev_id) 1064 return-EINVAL; 1065 1066 desc = irq_to_desc (irq); 1067 if (! Desc) 1068 return-EINVAL; 1069 1070 if (desc-> status & IRQ_NOREQUEST) 1071 return-EINVAL; 10721073 if (! Handler) {1074 if (! Thread_fn) 1075 return-EINVAL; 1076 handler = irq_default_primary_handler; 1077} 10781079 action = kzarloc (sizeof (struct irqaction), GFP_KERNEL); 1080 if (! Action) 1081 return-ENOMEM; 10821083 action-> handler = handler; 1084 action-> thread_fn = thread_fn; 1085 action-> flags = irqflags; 1086 action-> name = devname; 1087 action-> dev_id = dev_id; 10881089 chip_bus_lock (irq, desc); 1090 retval = _ setup_irq (irq, desc, action); 1091 chip_bus_sync_unlock (irq, desc ); 10921093 if (retval) 1094 kfree (action); 1095 1096 # ifdef CONFIG_DEBUG_SHIRQ1097 if (! Retval & (irqflags & ir1__shared) {1098/* 1099 * It's a shared IRQ -- the driver ought to be prepared for it 1100 * to happen immediately, so let's make sure .... 1101 * We disable the irq to make sure that a 'real' IRQ doesn' t 1102 * run in parallel with our fake.1103 */1104 unsigned long flags; 1105 1106 disable_irq (irq ); 1107 local_irq_save (flags); 11081109 handler (irq, dev_id); 11101111 local_irq_restore (flags); 1112 enable_irq (irq); 1113} 1114 # endif1115 return retval; 1116} [C-sharp]View plaincopy993void free_irq (unsigned int irq, void * dev_id) 994 {995 struct irq_desc * desc = irq_to_desc (irq); 996997 if (! Desc) 998 return; 999 1000 chip_bus_lock (irq, desc); 1001 kfree (_ free_irq (irq, dev_id); 1002 chip_bus_sync_unlock (irq, desc); 1003}
Related Article

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.