]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices | |
3 | * | |
c1017a4c | 4 | * based on isapnp.c resource management (c) Jaroslav Kysela <[email protected]> |
1da177e4 | 5 | * Copyright 2003 Adam Belay <[email protected]> |
1f32ca31 BH |
6 | * Copyright (C) 2008 Hewlett-Packard Development Company, L.P. |
7 | * Bjorn Helgaas <[email protected]> | |
1da177e4 LT |
8 | */ |
9 | ||
1da177e4 LT |
10 | #include <linux/errno.h> |
11 | #include <linux/module.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/kernel.h> | |
1da177e4 | 14 | #include <linux/pnp.h> |
4e57b681 | 15 | #include <linux/bitmap.h> |
b3bd86e2 | 16 | #include <linux/mutex.h> |
1da177e4 LT |
17 | #include "base.h" |
18 | ||
b3bd86e2 | 19 | DEFINE_MUTEX(pnp_res_mutex); |
1da177e4 LT |
20 | |
21 | static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx) | |
22 | { | |
aee3ad81 | 23 | struct resource *res, local_res; |
1da177e4 | 24 | |
aee3ad81 BH |
25 | res = pnp_get_resource(dev, IORESOURCE_IO, idx); |
26 | if (res) { | |
2f53432c | 27 | pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx " |
28ccffcf BH |
28 | "flags %#lx\n", idx, (unsigned long long) res->start, |
29 | (unsigned long long) res->end, res->flags); | |
6e906f0e | 30 | return 0; |
81b5c75f BH |
31 | } |
32 | ||
aee3ad81 BH |
33 | res = &local_res; |
34 | res->flags = rule->flags | IORESOURCE_AUTO; | |
35 | res->start = 0; | |
36 | res->end = 0; | |
1da177e4 LT |
37 | |
38 | if (!rule->size) { | |
28ccffcf | 39 | res->flags |= IORESOURCE_DISABLED; |
2f53432c | 40 | pnp_dbg(&dev->dev, " io %d disabled\n", idx); |
aee3ad81 | 41 | goto __add; |
1da177e4 LT |
42 | } |
43 | ||
28ccffcf BH |
44 | res->start = rule->min; |
45 | res->end = res->start + rule->size - 1; | |
1da177e4 | 46 | |
f5d94ff0 | 47 | while (!pnp_check_port(dev, res)) { |
28ccffcf BH |
48 | res->start += rule->align; |
49 | res->end = res->start + rule->size - 1; | |
50 | if (res->start > rule->max || !rule->align) { | |
2f53432c | 51 | pnp_dbg(&dev->dev, " couldn't assign io %d " |
fcfb7ce3 BH |
52 | "(min %#llx max %#llx)\n", idx, |
53 | (unsigned long long) rule->min, | |
54 | (unsigned long long) rule->max); | |
6e906f0e | 55 | return -EBUSY; |
81b5c75f | 56 | } |
1da177e4 | 57 | } |
aee3ad81 BH |
58 | |
59 | __add: | |
60 | pnp_add_io_resource(dev, res->start, res->end, res->flags); | |
6e906f0e | 61 | return 0; |
1da177e4 LT |
62 | } |
63 | ||
64 | static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx) | |
65 | { | |
aee3ad81 | 66 | struct resource *res, local_res; |
1da177e4 | 67 | |
aee3ad81 BH |
68 | res = pnp_get_resource(dev, IORESOURCE_MEM, idx); |
69 | if (res) { | |
2f53432c | 70 | pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx " |
28ccffcf BH |
71 | "flags %#lx\n", idx, (unsigned long long) res->start, |
72 | (unsigned long long) res->end, res->flags); | |
6e906f0e | 73 | return 0; |
81b5c75f BH |
74 | } |
75 | ||
aee3ad81 BH |
76 | res = &local_res; |
77 | res->flags = rule->flags | IORESOURCE_AUTO; | |
78 | res->start = 0; | |
79 | res->end = 0; | |
1da177e4 | 80 | |
1da177e4 | 81 | if (!(rule->flags & IORESOURCE_MEM_WRITEABLE)) |
28ccffcf | 82 | res->flags |= IORESOURCE_READONLY; |
1da177e4 | 83 | if (rule->flags & IORESOURCE_MEM_CACHEABLE) |
28ccffcf | 84 | res->flags |= IORESOURCE_CACHEABLE; |
1da177e4 | 85 | if (rule->flags & IORESOURCE_MEM_RANGELENGTH) |
28ccffcf | 86 | res->flags |= IORESOURCE_RANGELENGTH; |
1da177e4 | 87 | if (rule->flags & IORESOURCE_MEM_SHADOWABLE) |
28ccffcf | 88 | res->flags |= IORESOURCE_SHADOWABLE; |
1da177e4 LT |
89 | |
90 | if (!rule->size) { | |
28ccffcf | 91 | res->flags |= IORESOURCE_DISABLED; |
2f53432c | 92 | pnp_dbg(&dev->dev, " mem %d disabled\n", idx); |
aee3ad81 | 93 | goto __add; |
1da177e4 LT |
94 | } |
95 | ||
28ccffcf BH |
96 | res->start = rule->min; |
97 | res->end = res->start + rule->size - 1; | |
1da177e4 | 98 | |
f5d94ff0 | 99 | while (!pnp_check_mem(dev, res)) { |
28ccffcf BH |
100 | res->start += rule->align; |
101 | res->end = res->start + rule->size - 1; | |
102 | if (res->start > rule->max || !rule->align) { | |
2f53432c | 103 | pnp_dbg(&dev->dev, " couldn't assign mem %d " |
fcfb7ce3 BH |
104 | "(min %#llx max %#llx)\n", idx, |
105 | (unsigned long long) rule->min, | |
106 | (unsigned long long) rule->max); | |
6e906f0e | 107 | return -EBUSY; |
81b5c75f | 108 | } |
1da177e4 | 109 | } |
aee3ad81 BH |
110 | |
111 | __add: | |
112 | pnp_add_mem_resource(dev, res->start, res->end, res->flags); | |
6e906f0e | 113 | return 0; |
1da177e4 LT |
114 | } |
115 | ||
9dd78466 | 116 | static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx) |
1da177e4 | 117 | { |
aee3ad81 | 118 | struct resource *res, local_res; |
1da177e4 LT |
119 | int i; |
120 | ||
121 | /* IRQ priority: this table is good for i386 */ | |
122 | static unsigned short xtab[16] = { | |
123 | 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2 | |
124 | }; | |
125 | ||
aee3ad81 BH |
126 | res = pnp_get_resource(dev, IORESOURCE_IRQ, idx); |
127 | if (res) { | |
2f53432c | 128 | pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n", |
28ccffcf | 129 | idx, (int) res->start, res->flags); |
6e906f0e | 130 | return 0; |
81b5c75f BH |
131 | } |
132 | ||
aee3ad81 BH |
133 | res = &local_res; |
134 | res->flags = rule->flags | IORESOURCE_AUTO; | |
135 | res->start = -1; | |
136 | res->end = -1; | |
1da177e4 | 137 | |
7aefff51 | 138 | if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) { |
28ccffcf | 139 | res->flags |= IORESOURCE_DISABLED; |
2f53432c | 140 | pnp_dbg(&dev->dev, " irq %d disabled\n", idx); |
aee3ad81 | 141 | goto __add; |
1da177e4 LT |
142 | } |
143 | ||
144 | /* TBD: need check for >16 IRQ */ | |
7aefff51 | 145 | res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16); |
28ccffcf BH |
146 | if (res->start < PNP_IRQ_NR) { |
147 | res->end = res->start; | |
aee3ad81 | 148 | goto __add; |
1da177e4 LT |
149 | } |
150 | for (i = 0; i < 16; i++) { | |
7aefff51 | 151 | if (test_bit(xtab[i], rule->map.bits)) { |
28ccffcf | 152 | res->start = res->end = xtab[i]; |
aee3ad81 BH |
153 | if (pnp_check_irq(dev, res)) |
154 | goto __add; | |
1da177e4 LT |
155 | } |
156 | } | |
d5ebde6e BH |
157 | |
158 | if (rule->flags & IORESOURCE_IRQ_OPTIONAL) { | |
159 | res->start = -1; | |
160 | res->end = -1; | |
161 | res->flags |= IORESOURCE_DISABLED; | |
2f53432c | 162 | pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx); |
d5ebde6e BH |
163 | goto __add; |
164 | } | |
165 | ||
2f53432c | 166 | pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx); |
6e906f0e | 167 | return -EBUSY; |
aee3ad81 BH |
168 | |
169 | __add: | |
170 | pnp_add_irq_resource(dev, res->start, res->flags); | |
6e906f0e | 171 | return 0; |
1da177e4 LT |
172 | } |
173 | ||
586f83e2 | 174 | #ifdef CONFIG_ISA_DMA_API |
6e906f0e | 175 | static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx) |
1da177e4 | 176 | { |
aee3ad81 | 177 | struct resource *res, local_res; |
1da177e4 LT |
178 | int i; |
179 | ||
180 | /* DMA priority: this table is good for i386 */ | |
181 | static unsigned short xtab[8] = { | |
182 | 1, 3, 5, 6, 7, 0, 2, 4 | |
183 | }; | |
184 | ||
aee3ad81 BH |
185 | res = pnp_get_resource(dev, IORESOURCE_DMA, idx); |
186 | if (res) { | |
2f53432c | 187 | pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n", |
28ccffcf | 188 | idx, (int) res->start, res->flags); |
6e906f0e | 189 | return 0; |
81b5c75f BH |
190 | } |
191 | ||
aee3ad81 BH |
192 | res = &local_res; |
193 | res->flags = rule->flags | IORESOURCE_AUTO; | |
194 | res->start = -1; | |
195 | res->end = -1; | |
1da177e4 | 196 | |
1da177e4 | 197 | for (i = 0; i < 8; i++) { |
9dd78466 | 198 | if (rule->map & (1 << xtab[i])) { |
28ccffcf | 199 | res->start = res->end = xtab[i]; |
aee3ad81 BH |
200 | if (pnp_check_dma(dev, res)) |
201 | goto __add; | |
1da177e4 LT |
202 | } |
203 | } | |
7ef36390 | 204 | #ifdef MAX_DMA_CHANNELS |
28ccffcf | 205 | res->start = res->end = MAX_DMA_CHANNELS; |
7ef36390 | 206 | #endif |
aee3ad81 | 207 | res->flags |= IORESOURCE_DISABLED; |
2f53432c | 208 | pnp_dbg(&dev->dev, " disable dma %d\n", idx); |
1da177e4 | 209 | |
aee3ad81 BH |
210 | __add: |
211 | pnp_add_dma_resource(dev, res->start, res->flags); | |
6e906f0e | 212 | return 0; |
d948a8da | 213 | } |
586f83e2 | 214 | #endif /* CONFIG_ISA_DMA_API */ |
d948a8da | 215 | |
2cd13930 | 216 | void pnp_init_resources(struct pnp_dev *dev) |
1da177e4 | 217 | { |
aee3ad81 | 218 | pnp_free_resources(dev); |
1da177e4 LT |
219 | } |
220 | ||
6969c7ed | 221 | static void pnp_clean_resource_table(struct pnp_dev *dev) |
1da177e4 | 222 | { |
aee3ad81 BH |
223 | struct pnp_resource *pnp_res, *tmp; |
224 | ||
225 | list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) { | |
226 | if (pnp_res->res.flags & IORESOURCE_AUTO) | |
227 | pnp_free_resource(pnp_res); | |
1da177e4 LT |
228 | } |
229 | } | |
230 | ||
231 | /** | |
232 | * pnp_assign_resources - assigns resources to the device based on the specified dependent number | |
233 | * @dev: pointer to the desired device | |
1f32ca31 | 234 | * @set: the dependent function number |
1da177e4 | 235 | */ |
1f32ca31 | 236 | static int pnp_assign_resources(struct pnp_dev *dev, int set) |
1da177e4 | 237 | { |
1f32ca31 | 238 | struct pnp_option *option; |
586f83e2 DR |
239 | int nport = 0, nmem = 0, nirq = 0; |
240 | int ndma __maybe_unused = 0; | |
1f32ca31 | 241 | int ret = 0; |
1da177e4 | 242 | |
2f53432c | 243 | pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set); |
b3bd86e2 | 244 | mutex_lock(&pnp_res_mutex); |
6969c7ed | 245 | pnp_clean_resource_table(dev); |
1da177e4 | 246 | |
1f32ca31 BH |
247 | list_for_each_entry(option, &dev->options, list) { |
248 | if (pnp_option_is_dependent(option) && | |
249 | pnp_option_set(option) != set) | |
250 | continue; | |
251 | ||
252 | switch (option->type) { | |
253 | case IORESOURCE_IO: | |
254 | ret = pnp_assign_port(dev, &option->u.port, nport++); | |
255 | break; | |
256 | case IORESOURCE_MEM: | |
257 | ret = pnp_assign_mem(dev, &option->u.mem, nmem++); | |
258 | break; | |
259 | case IORESOURCE_IRQ: | |
260 | ret = pnp_assign_irq(dev, &option->u.irq, nirq++); | |
261 | break; | |
586f83e2 | 262 | #ifdef CONFIG_ISA_DMA_API |
1f32ca31 BH |
263 | case IORESOURCE_DMA: |
264 | ret = pnp_assign_dma(dev, &option->u.dma, ndma++); | |
265 | break; | |
586f83e2 | 266 | #endif |
1f32ca31 BH |
267 | default: |
268 | ret = -EINVAL; | |
269 | break; | |
1da177e4 | 270 | } |
1f32ca31 BH |
271 | if (ret < 0) |
272 | break; | |
273 | } | |
1da177e4 | 274 | |
b3bd86e2 | 275 | mutex_unlock(&pnp_res_mutex); |
1f32ca31 | 276 | if (ret < 0) { |
2f53432c | 277 | pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret); |
1f32ca31 BH |
278 | pnp_clean_resource_table(dev); |
279 | } else | |
280 | dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded"); | |
281 | return ret; | |
1da177e4 LT |
282 | } |
283 | ||
1da177e4 LT |
284 | /** |
285 | * pnp_auto_config_dev - automatically assigns resources to a device | |
286 | * @dev: pointer to the desired device | |
1da177e4 LT |
287 | */ |
288 | int pnp_auto_config_dev(struct pnp_dev *dev) | |
289 | { | |
1f32ca31 | 290 | int i, ret; |
1da177e4 | 291 | |
9dd78466 | 292 | if (!pnp_can_configure(dev)) { |
2f53432c | 293 | pnp_dbg(&dev->dev, "configuration not supported\n"); |
1da177e4 LT |
294 | return -ENODEV; |
295 | } | |
296 | ||
1f32ca31 BH |
297 | ret = pnp_assign_resources(dev, 0); |
298 | if (ret == 0) | |
299 | return 0; | |
300 | ||
301 | for (i = 1; i < dev->num_dependent_sets; i++) { | |
302 | ret = pnp_assign_resources(dev, i); | |
303 | if (ret == 0) | |
1da177e4 | 304 | return 0; |
1da177e4 LT |
305 | } |
306 | ||
a05d0781 | 307 | dev_err(&dev->dev, "unable to assign resources\n"); |
1f32ca31 | 308 | return ret; |
1da177e4 LT |
309 | } |
310 | ||
68094e32 PO |
311 | /** |
312 | * pnp_start_dev - low-level start of the PnP device | |
313 | * @dev: pointer to the desired device | |
314 | * | |
07d4e9af | 315 | * assumes that resources have already been allocated |
68094e32 | 316 | */ |
68094e32 PO |
317 | int pnp_start_dev(struct pnp_dev *dev) |
318 | { | |
319 | if (!pnp_can_write(dev)) { | |
2f53432c | 320 | pnp_dbg(&dev->dev, "activation not supported\n"); |
68094e32 PO |
321 | return -EINVAL; |
322 | } | |
323 | ||
81b5c75f | 324 | dbg_pnp_show_resources(dev, "pnp_start_dev"); |
59284cb4 | 325 | if (dev->protocol->set(dev) < 0) { |
a05d0781 | 326 | dev_err(&dev->dev, "activation failed\n"); |
68094e32 PO |
327 | return -EIO; |
328 | } | |
329 | ||
a05d0781 | 330 | dev_info(&dev->dev, "activated\n"); |
68094e32 PO |
331 | return 0; |
332 | } | |
333 | ||
334 | /** | |
335 | * pnp_stop_dev - low-level disable of the PnP device | |
336 | * @dev: pointer to the desired device | |
337 | * | |
338 | * does not free resources | |
339 | */ | |
68094e32 PO |
340 | int pnp_stop_dev(struct pnp_dev *dev) |
341 | { | |
342 | if (!pnp_can_disable(dev)) { | |
2f53432c | 343 | pnp_dbg(&dev->dev, "disabling not supported\n"); |
68094e32 PO |
344 | return -EINVAL; |
345 | } | |
9dd78466 | 346 | if (dev->protocol->disable(dev) < 0) { |
a05d0781 | 347 | dev_err(&dev->dev, "disable failed\n"); |
68094e32 PO |
348 | return -EIO; |
349 | } | |
350 | ||
a05d0781 | 351 | dev_info(&dev->dev, "disabled\n"); |
68094e32 PO |
352 | return 0; |
353 | } | |
354 | ||
1da177e4 LT |
355 | /** |
356 | * pnp_activate_dev - activates a PnP device for use | |
357 | * @dev: pointer to the desired device | |
358 | * | |
359 | * does not validate or set resources so be careful. | |
360 | */ | |
361 | int pnp_activate_dev(struct pnp_dev *dev) | |
362 | { | |
68094e32 PO |
363 | int error; |
364 | ||
07d4e9af | 365 | if (dev->active) |
cc8259a6 | 366 | return 0; |
1da177e4 LT |
367 | |
368 | /* ensure resources are allocated */ | |
369 | if (pnp_auto_config_dev(dev)) | |
370 | return -EBUSY; | |
371 | ||
68094e32 PO |
372 | error = pnp_start_dev(dev); |
373 | if (error) | |
374 | return error; | |
1da177e4 LT |
375 | |
376 | dev->active = 1; | |
cc8259a6 | 377 | return 0; |
1da177e4 LT |
378 | } |
379 | ||
380 | /** | |
381 | * pnp_disable_dev - disables device | |
382 | * @dev: pointer to the desired device | |
383 | * | |
384 | * inform the correct pnp protocol so that resources can be used by other devices | |
385 | */ | |
386 | int pnp_disable_dev(struct pnp_dev *dev) | |
387 | { | |
68094e32 PO |
388 | int error; |
389 | ||
07d4e9af | 390 | if (!dev->active) |
cc8259a6 | 391 | return 0; |
1da177e4 | 392 | |
68094e32 PO |
393 | error = pnp_stop_dev(dev); |
394 | if (error) | |
395 | return error; | |
1da177e4 LT |
396 | |
397 | dev->active = 0; | |
1da177e4 LT |
398 | |
399 | /* release the resources so that other devices can use them */ | |
b3bd86e2 | 400 | mutex_lock(&pnp_res_mutex); |
6969c7ed | 401 | pnp_clean_resource_table(dev); |
b3bd86e2 | 402 | mutex_unlock(&pnp_res_mutex); |
1da177e4 | 403 | |
cc8259a6 | 404 | return 0; |
1da177e4 LT |
405 | } |
406 | ||
68094e32 PO |
407 | EXPORT_SYMBOL(pnp_start_dev); |
408 | EXPORT_SYMBOL(pnp_stop_dev); | |
1da177e4 LT |
409 | EXPORT_SYMBOL(pnp_activate_dev); |
410 | EXPORT_SYMBOL(pnp_disable_dev); |