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