PNP: remove unused interfaces using pnp_resource_table
[deliverable/linux.git] / drivers / pnp / manager.c
CommitLineData
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 18DEFINE_MUTEX(pnp_res_mutex);
1da177e4
LT
19
20static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
21{
b60ba834
GKH
22 resource_size_t *start, *end;
23 unsigned long *flags;
1da177e4 24
1da177e4 25 if (idx >= PNP_MAX_PORT) {
a05d0781 26 dev_err(&dev->dev, "too many I/O port resources\n");
1da177e4
LT
27 /* pretend we were successful so at least the manager won't try again */
28 return 1;
29 }
30
1da177e4
LT
31 start = &dev->res.port_resource[idx].start;
32 end = &dev->res.port_resource[idx].end;
33 flags = &dev->res.port_resource[idx].flags;
34
81b5c75f
BH
35 /* check if this resource has been manually set, if so skip */
36 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO)) {
37 dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
38 "flags %#lx\n", idx, (unsigned long long) *start,
39 (unsigned long long) *end, *flags);
40 return 1;
41 }
42
1da177e4
LT
43 /* set the initial values */
44 *flags |= rule->flags | IORESOURCE_IO;
9dd78466 45 *flags &= ~IORESOURCE_UNSET;
1da177e4
LT
46
47 if (!rule->size) {
48 *flags |= IORESOURCE_DISABLED;
81b5c75f 49 dev_dbg(&dev->dev, " io %d disabled\n", idx);
9dd78466 50 return 1; /* skip disabled resource requests */
1da177e4
LT
51 }
52
53 *start = rule->min;
54 *end = *start + rule->size - 1;
55
56 /* run through until pnp_check_port is happy */
57 while (!pnp_check_port(dev, idx)) {
58 *start += rule->align;
59 *end = *start + rule->size - 1;
81b5c75f
BH
60 if (*start > rule->max || !rule->align) {
61 dev_dbg(&dev->dev, " couldn't assign io %d\n", idx);
1da177e4 62 return 0;
81b5c75f 63 }
1da177e4 64 }
81b5c75f
BH
65 dev_dbg(&dev->dev, " assign io %d %#llx-%#llx\n", idx,
66 (unsigned long long) *start, (unsigned long long) *end);
1da177e4
LT
67 return 1;
68}
69
70static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
71{
b60ba834
GKH
72 resource_size_t *start, *end;
73 unsigned long *flags;
1da177e4 74
1da177e4 75 if (idx >= PNP_MAX_MEM) {
a05d0781 76 dev_err(&dev->dev, "too many memory resources\n");
1da177e4
LT
77 /* pretend we were successful so at least the manager won't try again */
78 return 1;
79 }
80
1da177e4
LT
81 start = &dev->res.mem_resource[idx].start;
82 end = &dev->res.mem_resource[idx].end;
83 flags = &dev->res.mem_resource[idx].flags;
84
81b5c75f
BH
85 /* check if this resource has been manually set, if so skip */
86 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO)) {
87 dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
88 "flags %#lx\n", idx, (unsigned long long) *start,
89 (unsigned long long) *end, *flags);
90 return 1;
91 }
92
1da177e4
LT
93 /* set the initial values */
94 *flags |= rule->flags | IORESOURCE_MEM;
9dd78466 95 *flags &= ~IORESOURCE_UNSET;
1da177e4
LT
96
97 /* convert pnp flags to standard Linux flags */
98 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
99 *flags |= IORESOURCE_READONLY;
100 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
101 *flags |= IORESOURCE_CACHEABLE;
102 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
103 *flags |= IORESOURCE_RANGELENGTH;
104 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
105 *flags |= IORESOURCE_SHADOWABLE;
106
107 if (!rule->size) {
108 *flags |= IORESOURCE_DISABLED;
81b5c75f 109 dev_dbg(&dev->dev, " mem %d disabled\n", idx);
9dd78466 110 return 1; /* skip disabled resource requests */
1da177e4
LT
111 }
112
113 *start = rule->min;
9dd78466 114 *end = *start + rule->size - 1;
1da177e4
LT
115
116 /* run through until pnp_check_mem is happy */
117 while (!pnp_check_mem(dev, idx)) {
118 *start += rule->align;
119 *end = *start + rule->size - 1;
81b5c75f
BH
120 if (*start > rule->max || !rule->align) {
121 dev_dbg(&dev->dev, " couldn't assign mem %d\n", idx);
1da177e4 122 return 0;
81b5c75f 123 }
1da177e4 124 }
81b5c75f
BH
125 dev_dbg(&dev->dev, " assign mem %d %#llx-%#llx\n", idx,
126 (unsigned long long) *start, (unsigned long long) *end);
1da177e4
LT
127 return 1;
128}
129
9dd78466 130static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
1da177e4 131{
b60ba834
GKH
132 resource_size_t *start, *end;
133 unsigned long *flags;
1da177e4
LT
134 int i;
135
136 /* IRQ priority: this table is good for i386 */
137 static unsigned short xtab[16] = {
138 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
139 };
140
1da177e4 141 if (idx >= PNP_MAX_IRQ) {
a05d0781 142 dev_err(&dev->dev, "too many IRQ resources\n");
1da177e4
LT
143 /* pretend we were successful so at least the manager won't try again */
144 return 1;
145 }
146
1da177e4
LT
147 start = &dev->res.irq_resource[idx].start;
148 end = &dev->res.irq_resource[idx].end;
149 flags = &dev->res.irq_resource[idx].flags;
150
81b5c75f
BH
151 /* check if this resource has been manually set, if so skip */
152 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO)) {
153 dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
154 idx, (int) *start, *flags);
155 return 1;
156 }
157
1da177e4
LT
158 /* set the initial values */
159 *flags |= rule->flags | IORESOURCE_IRQ;
9dd78466 160 *flags &= ~IORESOURCE_UNSET;
1da177e4
LT
161
162 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
163 *flags |= IORESOURCE_DISABLED;
81b5c75f 164 dev_dbg(&dev->dev, " irq %d disabled\n", idx);
9dd78466 165 return 1; /* skip disabled resource requests */
1da177e4
LT
166 }
167
168 /* TBD: need check for >16 IRQ */
169 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
170 if (*start < PNP_IRQ_NR) {
171 *end = *start;
81b5c75f 172 dev_dbg(&dev->dev, " assign irq %d %d\n", idx, (int) *start);
1da177e4
LT
173 return 1;
174 }
175 for (i = 0; i < 16; i++) {
9dd78466 176 if (test_bit(xtab[i], rule->map)) {
1da177e4 177 *start = *end = xtab[i];
81b5c75f
BH
178 if (pnp_check_irq(dev, idx)) {
179 dev_dbg(&dev->dev, " assign irq %d %d\n", idx,
180 (int) *start);
1da177e4 181 return 1;
81b5c75f 182 }
1da177e4
LT
183 }
184 }
81b5c75f 185 dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
1da177e4
LT
186 return 0;
187}
188
7ef36390 189static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
1da177e4 190{
b60ba834
GKH
191 resource_size_t *start, *end;
192 unsigned long *flags;
1da177e4
LT
193 int i;
194
195 /* DMA priority: this table is good for i386 */
196 static unsigned short xtab[8] = {
197 1, 3, 5, 6, 7, 0, 2, 4
198 };
199
1da177e4 200 if (idx >= PNP_MAX_DMA) {
a05d0781 201 dev_err(&dev->dev, "too many DMA resources\n");
7ef36390 202 return;
1da177e4
LT
203 }
204
1da177e4
LT
205 start = &dev->res.dma_resource[idx].start;
206 end = &dev->res.dma_resource[idx].end;
207 flags = &dev->res.dma_resource[idx].flags;
208
81b5c75f
BH
209 /* check if this resource has been manually set, if so skip */
210 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO)) {
211 dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
212 idx, (int) *start, *flags);
213 return;
214 }
215
1da177e4
LT
216 /* set the initial values */
217 *flags |= rule->flags | IORESOURCE_DMA;
9dd78466 218 *flags &= ~IORESOURCE_UNSET;
1da177e4 219
1da177e4 220 for (i = 0; i < 8; i++) {
9dd78466 221 if (rule->map & (1 << xtab[i])) {
1da177e4 222 *start = *end = xtab[i];
81b5c75f
BH
223 if (pnp_check_dma(dev, idx)) {
224 dev_dbg(&dev->dev, " assign dma %d %d\n", idx,
225 (int) *start);
7ef36390 226 return;
81b5c75f 227 }
1da177e4
LT
228 }
229 }
7ef36390
JB
230#ifdef MAX_DMA_CHANNELS
231 *start = *end = MAX_DMA_CHANNELS;
232#endif
233 *flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
81b5c75f 234 dev_dbg(&dev->dev, " disable dma %d\n", idx);
1da177e4
LT
235}
236
237/**
238 * pnp_init_resources - Resets a resource table to default values.
239 * @table: pointer to the desired resource table
1da177e4 240 */
2cd13930 241void pnp_init_resources(struct pnp_dev *dev)
1da177e4 242{
2cd13930 243 struct pnp_resource_table *table = &dev->res;
1da177e4 244 int idx;
07d4e9af 245
1da177e4
LT
246 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
247 table->irq_resource[idx].name = NULL;
248 table->irq_resource[idx].start = -1;
249 table->irq_resource[idx].end = -1;
9dd78466
BH
250 table->irq_resource[idx].flags =
251 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
1da177e4
LT
252 }
253 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
254 table->dma_resource[idx].name = NULL;
255 table->dma_resource[idx].start = -1;
256 table->dma_resource[idx].end = -1;
9dd78466
BH
257 table->dma_resource[idx].flags =
258 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
1da177e4
LT
259 }
260 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
261 table->port_resource[idx].name = NULL;
262 table->port_resource[idx].start = 0;
263 table->port_resource[idx].end = 0;
9dd78466
BH
264 table->port_resource[idx].flags =
265 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
1da177e4
LT
266 }
267 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
268 table->mem_resource[idx].name = NULL;
269 table->mem_resource[idx].start = 0;
270 table->mem_resource[idx].end = 0;
9dd78466
BH
271 table->mem_resource[idx].flags =
272 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
1da177e4
LT
273 }
274}
275
276/**
277 * pnp_clean_resources - clears resources that were not manually set
67be2dd1 278 * @res: the resources to clean
1da177e4 279 */
6969c7ed 280static void pnp_clean_resource_table(struct pnp_dev *dev)
1da177e4 281{
6969c7ed 282 struct pnp_resource_table *res = &dev->res;
1da177e4 283 int idx;
07d4e9af 284
1da177e4
LT
285 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
286 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
287 continue;
288 res->irq_resource[idx].start = -1;
289 res->irq_resource[idx].end = -1;
9dd78466
BH
290 res->irq_resource[idx].flags =
291 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
1da177e4
LT
292 }
293 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
294 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
295 continue;
296 res->dma_resource[idx].start = -1;
297 res->dma_resource[idx].end = -1;
9dd78466
BH
298 res->dma_resource[idx].flags =
299 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
1da177e4
LT
300 }
301 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
302 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
303 continue;
304 res->port_resource[idx].start = 0;
305 res->port_resource[idx].end = 0;
9dd78466
BH
306 res->port_resource[idx].flags =
307 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
1da177e4
LT
308 }
309 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
310 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
311 continue;
312 res->mem_resource[idx].start = 0;
313 res->mem_resource[idx].end = 0;
9dd78466
BH
314 res->mem_resource[idx].flags =
315 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
1da177e4
LT
316 }
317}
318
319/**
320 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
321 * @dev: pointer to the desired device
322 * @depnum: the dependent function number
323 *
324 * Only set depnum to 0 if the device does not have dependent options.
325 */
326static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
327{
328 struct pnp_port *port;
329 struct pnp_mem *mem;
330 struct pnp_irq *irq;
331 struct pnp_dma *dma;
332 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
333
334 if (!pnp_can_configure(dev))
335 return -ENODEV;
336
81b5c75f 337 dbg_pnp_show_resources(dev, "before pnp_assign_resources");
b3bd86e2 338 mutex_lock(&pnp_res_mutex);
6969c7ed 339 pnp_clean_resource_table(dev);
1da177e4 340 if (dev->independent) {
81b5c75f 341 dev_dbg(&dev->dev, "assigning independent options\n");
1da177e4
LT
342 port = dev->independent->port;
343 mem = dev->independent->mem;
344 irq = dev->independent->irq;
345 dma = dev->independent->dma;
346 while (port) {
347 if (!pnp_assign_port(dev, port, nport))
348 goto fail;
349 nport++;
350 port = port->next;
351 }
352 while (mem) {
353 if (!pnp_assign_mem(dev, mem, nmem))
354 goto fail;
355 nmem++;
356 mem = mem->next;
357 }
358 while (irq) {
359 if (!pnp_assign_irq(dev, irq, nirq))
360 goto fail;
361 nirq++;
362 irq = irq->next;
363 }
364 while (dma) {
7ef36390 365 pnp_assign_dma(dev, dma, ndma);
1da177e4
LT
366 ndma++;
367 dma = dma->next;
368 }
369 }
370
371 if (depnum) {
372 struct pnp_option *dep;
373 int i;
81b5c75f
BH
374
375 dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
9dd78466
BH
376 for (i = 1, dep = dev->dependent; i < depnum;
377 i++, dep = dep->next)
378 if (!dep)
1da177e4 379 goto fail;
9dd78466 380 port = dep->port;
1da177e4
LT
381 mem = dep->mem;
382 irq = dep->irq;
383 dma = dep->dma;
384 while (port) {
385 if (!pnp_assign_port(dev, port, nport))
386 goto fail;
387 nport++;
388 port = port->next;
389 }
390 while (mem) {
391 if (!pnp_assign_mem(dev, mem, nmem))
392 goto fail;
393 nmem++;
394 mem = mem->next;
395 }
396 while (irq) {
397 if (!pnp_assign_irq(dev, irq, nirq))
398 goto fail;
399 nirq++;
400 irq = irq->next;
401 }
402 while (dma) {
7ef36390 403 pnp_assign_dma(dev, dma, ndma);
1da177e4
LT
404 ndma++;
405 dma = dma->next;
406 }
407 } else if (dev->dependent)
408 goto fail;
409
b3bd86e2 410 mutex_unlock(&pnp_res_mutex);
81b5c75f 411 dbg_pnp_show_resources(dev, "after pnp_assign_resources");
1da177e4
LT
412 return 1;
413
1e0aa9ad 414fail:
6969c7ed 415 pnp_clean_resource_table(dev);
b3bd86e2 416 mutex_unlock(&pnp_res_mutex);
81b5c75f 417 dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
1da177e4
LT
418 return 0;
419}
420
1da177e4
LT
421/**
422 * pnp_auto_config_dev - automatically assigns resources to a device
423 * @dev: pointer to the desired device
1da177e4
LT
424 */
425int pnp_auto_config_dev(struct pnp_dev *dev)
426{
427 struct pnp_option *dep;
428 int i = 1;
429
9dd78466 430 if (!pnp_can_configure(dev)) {
a05d0781 431 dev_dbg(&dev->dev, "configuration not supported\n");
1da177e4
LT
432 return -ENODEV;
433 }
434
435 if (!dev->dependent) {
436 if (pnp_assign_resources(dev, 0))
437 return 0;
438 } else {
439 dep = dev->dependent;
440 do {
441 if (pnp_assign_resources(dev, i))
442 return 0;
443 dep = dep->next;
444 i++;
445 } while (dep);
446 }
447
a05d0781 448 dev_err(&dev->dev, "unable to assign resources\n");
1da177e4
LT
449 return -EBUSY;
450}
451
68094e32
PO
452/**
453 * pnp_start_dev - low-level start of the PnP device
454 * @dev: pointer to the desired device
455 *
07d4e9af 456 * assumes that resources have already been allocated
68094e32 457 */
68094e32
PO
458int pnp_start_dev(struct pnp_dev *dev)
459{
460 if (!pnp_can_write(dev)) {
a05d0781 461 dev_dbg(&dev->dev, "activation not supported\n");
68094e32
PO
462 return -EINVAL;
463 }
464
81b5c75f 465 dbg_pnp_show_resources(dev, "pnp_start_dev");
59284cb4 466 if (dev->protocol->set(dev) < 0) {
a05d0781 467 dev_err(&dev->dev, "activation failed\n");
68094e32
PO
468 return -EIO;
469 }
470
a05d0781 471 dev_info(&dev->dev, "activated\n");
68094e32
PO
472 return 0;
473}
474
475/**
476 * pnp_stop_dev - low-level disable of the PnP device
477 * @dev: pointer to the desired device
478 *
479 * does not free resources
480 */
68094e32
PO
481int pnp_stop_dev(struct pnp_dev *dev)
482{
483 if (!pnp_can_disable(dev)) {
a05d0781 484 dev_dbg(&dev->dev, "disabling not supported\n");
68094e32
PO
485 return -EINVAL;
486 }
9dd78466 487 if (dev->protocol->disable(dev) < 0) {
a05d0781 488 dev_err(&dev->dev, "disable failed\n");
68094e32
PO
489 return -EIO;
490 }
491
a05d0781 492 dev_info(&dev->dev, "disabled\n");
68094e32
PO
493 return 0;
494}
495
1da177e4
LT
496/**
497 * pnp_activate_dev - activates a PnP device for use
498 * @dev: pointer to the desired device
499 *
500 * does not validate or set resources so be careful.
501 */
502int pnp_activate_dev(struct pnp_dev *dev)
503{
68094e32
PO
504 int error;
505
07d4e9af 506 if (dev->active)
cc8259a6 507 return 0;
1da177e4
LT
508
509 /* ensure resources are allocated */
510 if (pnp_auto_config_dev(dev))
511 return -EBUSY;
512
68094e32
PO
513 error = pnp_start_dev(dev);
514 if (error)
515 return error;
1da177e4
LT
516
517 dev->active = 1;
cc8259a6 518 return 0;
1da177e4
LT
519}
520
521/**
522 * pnp_disable_dev - disables device
523 * @dev: pointer to the desired device
524 *
525 * inform the correct pnp protocol so that resources can be used by other devices
526 */
527int pnp_disable_dev(struct pnp_dev *dev)
528{
68094e32
PO
529 int error;
530
07d4e9af 531 if (!dev->active)
cc8259a6 532 return 0;
1da177e4 533
68094e32
PO
534 error = pnp_stop_dev(dev);
535 if (error)
536 return error;
1da177e4
LT
537
538 dev->active = 0;
1da177e4
LT
539
540 /* release the resources so that other devices can use them */
b3bd86e2 541 mutex_lock(&pnp_res_mutex);
6969c7ed 542 pnp_clean_resource_table(dev);
b3bd86e2 543 mutex_unlock(&pnp_res_mutex);
1da177e4 544
cc8259a6 545 return 0;
1da177e4
LT
546}
547
68094e32
PO
548EXPORT_SYMBOL(pnp_start_dev);
549EXPORT_SYMBOL(pnp_stop_dev);
1da177e4
LT
550EXPORT_SYMBOL(pnp_activate_dev);
551EXPORT_SYMBOL(pnp_disable_dev);
This page took 0.458562 seconds and 5 git commands to generate.