]>
Commit | Line | Data |
---|---|---|
5ea81769 AV |
1 | #include <linux/module.h> |
2 | #include <linux/interrupt.h> | |
0af3678f | 3 | #include <linux/device.h> |
1aeb272c | 4 | #include <linux/gfp.h> |
2b5e7730 | 5 | #include <linux/irq.h> |
5ea81769 | 6 | |
1c3e3630 BG |
7 | #include "internals.h" |
8 | ||
5ea81769 AV |
9 | /* |
10 | * Device resource management aware IRQ request/free implementation. | |
11 | */ | |
12 | struct irq_devres { | |
13 | unsigned int irq; | |
14 | void *dev_id; | |
15 | }; | |
16 | ||
17 | static void devm_irq_release(struct device *dev, void *res) | |
18 | { | |
19 | struct irq_devres *this = res; | |
20 | ||
21 | free_irq(this->irq, this->dev_id); | |
22 | } | |
23 | ||
24 | static int devm_irq_match(struct device *dev, void *res, void *data) | |
25 | { | |
26 | struct irq_devres *this = res, *match = data; | |
27 | ||
28 | return this->irq == match->irq && this->dev_id == match->dev_id; | |
29 | } | |
30 | ||
31 | /** | |
935bd5b9 | 32 | * devm_request_threaded_irq - allocate an interrupt line for a managed device |
5ea81769 AV |
33 | * @dev: device to request interrupt for |
34 | * @irq: Interrupt line to allocate | |
35 | * @handler: Function to be called when the IRQ occurs | |
935bd5b9 AV |
36 | * @thread_fn: function to be called in a threaded interrupt context. NULL |
37 | * for devices which handle everything in @handler | |
5ea81769 | 38 | * @irqflags: Interrupt type flags |
899b5fbf | 39 | * @devname: An ascii name for the claiming device, dev_name(dev) if NULL |
5ea81769 AV |
40 | * @dev_id: A cookie passed back to the handler function |
41 | * | |
42 | * Except for the extra @dev argument, this function takes the | |
43 | * same arguments and performs the same function as | |
307b28b9 | 44 | * request_threaded_irq(). IRQs requested with this function will be |
5ea81769 AV |
45 | * automatically freed on driver detach. |
46 | * | |
47 | * If an IRQ allocated with this function needs to be freed | |
5c42dc70 | 48 | * separately, devm_free_irq() must be used. |
5ea81769 | 49 | */ |
935bd5b9 AV |
50 | int devm_request_threaded_irq(struct device *dev, unsigned int irq, |
51 | irq_handler_t handler, irq_handler_t thread_fn, | |
52 | unsigned long irqflags, const char *devname, | |
53 | void *dev_id) | |
5ea81769 AV |
54 | { |
55 | struct irq_devres *dr; | |
56 | int rc; | |
57 | ||
58 | dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres), | |
59 | GFP_KERNEL); | |
60 | if (!dr) | |
61 | return -ENOMEM; | |
62 | ||
899b5fbf HK |
63 | if (!devname) |
64 | devname = dev_name(dev); | |
65 | ||
935bd5b9 AV |
66 | rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname, |
67 | dev_id); | |
5ea81769 | 68 | if (rc) { |
7f30e49e | 69 | devres_free(dr); |
5ea81769 AV |
70 | return rc; |
71 | } | |
72 | ||
73 | dr->irq = irq; | |
74 | dr->dev_id = dev_id; | |
75 | devres_add(dev, dr); | |
76 | ||
77 | return 0; | |
78 | } | |
935bd5b9 | 79 | EXPORT_SYMBOL(devm_request_threaded_irq); |
5ea81769 | 80 | |
0668d306 SB |
81 | /** |
82 | * devm_request_any_context_irq - allocate an interrupt line for a managed device | |
83 | * @dev: device to request interrupt for | |
84 | * @irq: Interrupt line to allocate | |
85 | * @handler: Function to be called when the IRQ occurs | |
86 | * @thread_fn: function to be called in a threaded interrupt context. NULL | |
87 | * for devices which handle everything in @handler | |
88 | * @irqflags: Interrupt type flags | |
899b5fbf | 89 | * @devname: An ascii name for the claiming device, dev_name(dev) if NULL |
0668d306 SB |
90 | * @dev_id: A cookie passed back to the handler function |
91 | * | |
92 | * Except for the extra @dev argument, this function takes the | |
93 | * same arguments and performs the same function as | |
94 | * request_any_context_irq(). IRQs requested with this function will be | |
95 | * automatically freed on driver detach. | |
96 | * | |
97 | * If an IRQ allocated with this function needs to be freed | |
98 | * separately, devm_free_irq() must be used. | |
99 | */ | |
100 | int devm_request_any_context_irq(struct device *dev, unsigned int irq, | |
101 | irq_handler_t handler, unsigned long irqflags, | |
102 | const char *devname, void *dev_id) | |
103 | { | |
104 | struct irq_devres *dr; | |
105 | int rc; | |
106 | ||
107 | dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres), | |
108 | GFP_KERNEL); | |
109 | if (!dr) | |
110 | return -ENOMEM; | |
111 | ||
899b5fbf HK |
112 | if (!devname) |
113 | devname = dev_name(dev); | |
114 | ||
0668d306 | 115 | rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id); |
63781394 | 116 | if (rc < 0) { |
0668d306 SB |
117 | devres_free(dr); |
118 | return rc; | |
119 | } | |
120 | ||
121 | dr->irq = irq; | |
122 | dr->dev_id = dev_id; | |
123 | devres_add(dev, dr); | |
124 | ||
63781394 | 125 | return rc; |
0668d306 SB |
126 | } |
127 | EXPORT_SYMBOL(devm_request_any_context_irq); | |
128 | ||
5ea81769 AV |
129 | /** |
130 | * devm_free_irq - free an interrupt | |
131 | * @dev: device to free interrupt for | |
132 | * @irq: Interrupt line to free | |
133 | * @dev_id: Device identity to free | |
134 | * | |
135 | * Except for the extra @dev argument, this function takes the | |
136 | * same arguments and performs the same function as free_irq(). | |
137 | * This function instead of free_irq() should be used to manually | |
9ce8e498 | 138 | * free IRQs allocated with devm_request_irq(). |
5ea81769 AV |
139 | */ |
140 | void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id) | |
141 | { | |
142 | struct irq_devres match_data = { irq, dev_id }; | |
143 | ||
5ea81769 AV |
144 | WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match, |
145 | &match_data)); | |
ae891a1b | 146 | free_irq(irq, dev_id); |
5ea81769 AV |
147 | } |
148 | EXPORT_SYMBOL(devm_free_irq); | |
2b5e7730 BG |
149 | |
150 | struct irq_desc_devres { | |
151 | unsigned int from; | |
152 | unsigned int cnt; | |
153 | }; | |
154 | ||
155 | static void devm_irq_desc_release(struct device *dev, void *res) | |
156 | { | |
157 | struct irq_desc_devres *this = res; | |
158 | ||
159 | irq_free_descs(this->from, this->cnt); | |
160 | } | |
161 | ||
162 | /** | |
163 | * __devm_irq_alloc_descs - Allocate and initialize a range of irq descriptors | |
164 | * for a managed device | |
165 | * @dev: Device to allocate the descriptors for | |
166 | * @irq: Allocate for specific irq number if irq >= 0 | |
167 | * @from: Start the search from this irq number | |
168 | * @cnt: Number of consecutive irqs to allocate | |
169 | * @node: Preferred node on which the irq descriptor should be allocated | |
170 | * @owner: Owning module (can be NULL) | |
171 | * @affinity: Optional pointer to an affinity mask array of size @cnt | |
172 | * which hints where the irq descriptors should be allocated | |
173 | * and which default affinities to use | |
174 | * | |
175 | * Returns the first irq number or error code. | |
176 | * | |
177 | * Note: Use the provided wrappers (devm_irq_alloc_desc*) for simplicity. | |
178 | */ | |
179 | int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from, | |
180 | unsigned int cnt, int node, struct module *owner, | |
181 | const struct cpumask *affinity) | |
182 | { | |
183 | struct irq_desc_devres *dr; | |
184 | int base; | |
185 | ||
186 | dr = devres_alloc(devm_irq_desc_release, sizeof(*dr), GFP_KERNEL); | |
187 | if (!dr) | |
188 | return -ENOMEM; | |
189 | ||
190 | base = __irq_alloc_descs(irq, from, cnt, node, owner, affinity); | |
191 | if (base < 0) { | |
192 | devres_free(dr); | |
193 | return base; | |
194 | } | |
195 | ||
196 | dr->from = base; | |
197 | dr->cnt = cnt; | |
198 | devres_add(dev, dr); | |
199 | ||
200 | return base; | |
201 | } | |
202 | EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs); | |
1c3e3630 BG |
203 | |
204 | #ifdef CONFIG_GENERIC_IRQ_CHIP | |
205 | /** | |
206 | * devm_irq_alloc_generic_chip - Allocate and initialize a generic chip | |
207 | * for a managed device | |
208 | * @dev: Device to allocate the generic chip for | |
209 | * @name: Name of the irq chip | |
210 | * @num_ct: Number of irq_chip_type instances associated with this | |
211 | * @irq_base: Interrupt base nr for this chip | |
212 | * @reg_base: Register base address (virtual) | |
213 | * @handler: Default flow handler associated with this chip | |
214 | * | |
215 | * Returns an initialized irq_chip_generic structure. The chip defaults | |
216 | * to the primary (index 0) irq_chip_type and @handler | |
217 | */ | |
218 | struct irq_chip_generic * | |
219 | devm_irq_alloc_generic_chip(struct device *dev, const char *name, int num_ct, | |
220 | unsigned int irq_base, void __iomem *reg_base, | |
221 | irq_flow_handler_t handler) | |
222 | { | |
223 | struct irq_chip_generic *gc; | |
224 | unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type); | |
225 | ||
226 | gc = devm_kzalloc(dev, sz, GFP_KERNEL); | |
227 | if (gc) | |
228 | irq_init_generic_chip(gc, name, num_ct, | |
229 | irq_base, reg_base, handler); | |
230 | ||
231 | return gc; | |
232 | } | |
233 | EXPORT_SYMBOL_GPL(devm_irq_alloc_generic_chip); | |
234 | #endif /* CONFIG_GENERIC_IRQ_CHIP */ |