2 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
8 #include <linux/errno.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/pnp.h>
13 #include <linux/slab.h>
14 #include <linux/bitmap.h>
15 #include <linux/mutex.h>
18 DEFINE_MUTEX(pnp_res_mutex
);
20 static int pnp_assign_port(struct pnp_dev
*dev
, struct pnp_port
*rule
, int idx
)
22 struct resource
*res
, local_res
;
24 res
= pnp_get_resource(dev
, IORESOURCE_IO
, idx
);
26 dev_dbg(&dev
->dev
, " io %d already set to %#llx-%#llx "
27 "flags %#lx\n", idx
, (unsigned long long) res
->start
,
28 (unsigned long long) res
->end
, res
->flags
);
33 res
->flags
= rule
->flags
| IORESOURCE_AUTO
;
38 res
->flags
|= IORESOURCE_DISABLED
;
39 dev_dbg(&dev
->dev
, " io %d disabled\n", idx
);
43 res
->start
= rule
->min
;
44 res
->end
= res
->start
+ rule
->size
- 1;
46 while (!pnp_check_port(dev
, res
)) {
47 res
->start
+= rule
->align
;
48 res
->end
= res
->start
+ rule
->size
- 1;
49 if (res
->start
> rule
->max
|| !rule
->align
) {
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
);
59 pnp_add_io_resource(dev
, res
->start
, res
->end
, res
->flags
);
63 static int pnp_assign_mem(struct pnp_dev
*dev
, struct pnp_mem
*rule
, int idx
)
65 struct resource
*res
, local_res
;
67 res
= pnp_get_resource(dev
, IORESOURCE_MEM
, idx
);
69 dev_dbg(&dev
->dev
, " mem %d already set to %#llx-%#llx "
70 "flags %#lx\n", idx
, (unsigned long long) res
->start
,
71 (unsigned long long) res
->end
, res
->flags
);
76 res
->flags
= rule
->flags
| IORESOURCE_AUTO
;
80 if (!(rule
->flags
& IORESOURCE_MEM_WRITEABLE
))
81 res
->flags
|= IORESOURCE_READONLY
;
82 if (rule
->flags
& IORESOURCE_MEM_CACHEABLE
)
83 res
->flags
|= IORESOURCE_CACHEABLE
;
84 if (rule
->flags
& IORESOURCE_MEM_RANGELENGTH
)
85 res
->flags
|= IORESOURCE_RANGELENGTH
;
86 if (rule
->flags
& IORESOURCE_MEM_SHADOWABLE
)
87 res
->flags
|= IORESOURCE_SHADOWABLE
;
90 res
->flags
|= IORESOURCE_DISABLED
;
91 dev_dbg(&dev
->dev
, " mem %d disabled\n", idx
);
95 res
->start
= rule
->min
;
96 res
->end
= res
->start
+ rule
->size
- 1;
98 while (!pnp_check_mem(dev
, res
)) {
99 res
->start
+= rule
->align
;
100 res
->end
= res
->start
+ rule
->size
- 1;
101 if (res
->start
> rule
->max
|| !rule
->align
) {
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
);
111 pnp_add_mem_resource(dev
, res
->start
, res
->end
, res
->flags
);
115 static int pnp_assign_irq(struct pnp_dev
*dev
, struct pnp_irq
*rule
, int idx
)
117 struct resource
*res
, local_res
;
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
125 res
= pnp_get_resource(dev
, IORESOURCE_IRQ
, idx
);
127 dev_dbg(&dev
->dev
, " irq %d already set to %d flags %#lx\n",
128 idx
, (int) res
->start
, res
->flags
);
133 res
->flags
= rule
->flags
| IORESOURCE_AUTO
;
137 if (bitmap_empty(rule
->map
.bits
, PNP_IRQ_NR
)) {
138 res
->flags
|= IORESOURCE_DISABLED
;
139 dev_dbg(&dev
->dev
, " irq %d disabled\n", idx
);
143 /* TBD: need check for >16 IRQ */
144 res
->start
= find_next_bit(rule
->map
.bits
, PNP_IRQ_NR
, 16);
145 if (res
->start
< PNP_IRQ_NR
) {
146 res
->end
= res
->start
;
149 for (i
= 0; i
< 16; i
++) {
150 if (test_bit(xtab
[i
], rule
->map
.bits
)) {
151 res
->start
= res
->end
= xtab
[i
];
152 if (pnp_check_irq(dev
, res
))
157 if (rule
->flags
& IORESOURCE_IRQ_OPTIONAL
) {
160 res
->flags
|= IORESOURCE_DISABLED
;
161 dev_dbg(&dev
->dev
, " irq %d disabled (optional)\n", idx
);
165 dev_dbg(&dev
->dev
, " couldn't assign irq %d\n", idx
);
169 pnp_add_irq_resource(dev
, res
->start
, res
->flags
);
173 static int pnp_assign_dma(struct pnp_dev
*dev
, struct pnp_dma
*rule
, int idx
)
175 struct resource
*res
, local_res
;
178 /* DMA priority: this table is good for i386 */
179 static unsigned short xtab
[8] = {
180 1, 3, 5, 6, 7, 0, 2, 4
183 res
= pnp_get_resource(dev
, IORESOURCE_DMA
, idx
);
185 dev_dbg(&dev
->dev
, " dma %d already set to %d flags %#lx\n",
186 idx
, (int) res
->start
, res
->flags
);
191 res
->flags
= rule
->flags
| IORESOURCE_AUTO
;
195 for (i
= 0; i
< 8; i
++) {
196 if (rule
->map
& (1 << xtab
[i
])) {
197 res
->start
= res
->end
= xtab
[i
];
198 if (pnp_check_dma(dev
, res
))
202 #ifdef MAX_DMA_CHANNELS
203 res
->start
= res
->end
= MAX_DMA_CHANNELS
;
205 res
->flags
|= IORESOURCE_DISABLED
;
206 dev_dbg(&dev
->dev
, " disable dma %d\n", idx
);
209 pnp_add_dma_resource(dev
, res
->start
, res
->flags
);
213 void pnp_init_resources(struct pnp_dev
*dev
)
215 pnp_free_resources(dev
);
218 static void pnp_clean_resource_table(struct pnp_dev
*dev
)
220 struct pnp_resource
*pnp_res
, *tmp
;
222 list_for_each_entry_safe(pnp_res
, tmp
, &dev
->resources
, list
) {
223 if (pnp_res
->res
.flags
& IORESOURCE_AUTO
)
224 pnp_free_resource(pnp_res
);
229 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
230 * @dev: pointer to the desired device
231 * @depnum: the dependent function number
233 * Only set depnum to 0 if the device does not have dependent options.
235 static int pnp_assign_resources(struct pnp_dev
*dev
, int depnum
)
237 struct pnp_port
*port
;
241 int nport
= 0, nmem
= 0, nirq
= 0, ndma
= 0;
243 dbg_pnp_show_resources(dev
, "before pnp_assign_resources");
244 mutex_lock(&pnp_res_mutex
);
245 pnp_clean_resource_table(dev
);
246 if (dev
->independent
) {
247 dev_dbg(&dev
->dev
, "assigning independent options\n");
248 port
= dev
->independent
->port
;
249 mem
= dev
->independent
->mem
;
250 irq
= dev
->independent
->irq
;
251 dma
= dev
->independent
->dma
;
253 if (pnp_assign_port(dev
, port
, nport
) < 0)
259 if (pnp_assign_mem(dev
, mem
, nmem
) < 0)
265 if (pnp_assign_irq(dev
, irq
, nirq
) < 0)
271 if (pnp_assign_dma(dev
, dma
, ndma
) < 0)
279 struct pnp_option
*dep
;
282 dev_dbg(&dev
->dev
, "assigning dependent option %d\n", depnum
);
283 for (i
= 1, dep
= dev
->dependent
; i
< depnum
;
284 i
++, dep
= dep
->next
)
292 if (pnp_assign_port(dev
, port
, nport
) < 0)
298 if (pnp_assign_mem(dev
, mem
, nmem
) < 0)
304 if (pnp_assign_irq(dev
, irq
, nirq
) < 0)
310 if (pnp_assign_dma(dev
, dma
, ndma
) < 0)
315 } else if (dev
->dependent
)
318 mutex_unlock(&pnp_res_mutex
);
319 dbg_pnp_show_resources(dev
, "after pnp_assign_resources");
323 pnp_clean_resource_table(dev
);
324 mutex_unlock(&pnp_res_mutex
);
325 dbg_pnp_show_resources(dev
, "after pnp_assign_resources (failed)");
330 * pnp_auto_config_dev - automatically assigns resources to a device
331 * @dev: pointer to the desired device
333 int pnp_auto_config_dev(struct pnp_dev
*dev
)
335 struct pnp_option
*dep
;
338 if (!pnp_can_configure(dev
)) {
339 dev_dbg(&dev
->dev
, "configuration not supported\n");
343 if (!dev
->dependent
) {
344 if (pnp_assign_resources(dev
, 0))
347 dep
= dev
->dependent
;
349 if (pnp_assign_resources(dev
, i
))
356 dev_err(&dev
->dev
, "unable to assign resources\n");
361 * pnp_start_dev - low-level start of the PnP device
362 * @dev: pointer to the desired device
364 * assumes that resources have already been allocated
366 int pnp_start_dev(struct pnp_dev
*dev
)
368 if (!pnp_can_write(dev
)) {
369 dev_dbg(&dev
->dev
, "activation not supported\n");
373 dbg_pnp_show_resources(dev
, "pnp_start_dev");
374 if (dev
->protocol
->set(dev
) < 0) {
375 dev_err(&dev
->dev
, "activation failed\n");
379 dev_info(&dev
->dev
, "activated\n");
384 * pnp_stop_dev - low-level disable of the PnP device
385 * @dev: pointer to the desired device
387 * does not free resources
389 int pnp_stop_dev(struct pnp_dev
*dev
)
391 if (!pnp_can_disable(dev
)) {
392 dev_dbg(&dev
->dev
, "disabling not supported\n");
395 if (dev
->protocol
->disable(dev
) < 0) {
396 dev_err(&dev
->dev
, "disable failed\n");
400 dev_info(&dev
->dev
, "disabled\n");
405 * pnp_activate_dev - activates a PnP device for use
406 * @dev: pointer to the desired device
408 * does not validate or set resources so be careful.
410 int pnp_activate_dev(struct pnp_dev
*dev
)
417 /* ensure resources are allocated */
418 if (pnp_auto_config_dev(dev
))
421 error
= pnp_start_dev(dev
);
430 * pnp_disable_dev - disables device
431 * @dev: pointer to the desired device
433 * inform the correct pnp protocol so that resources can be used by other devices
435 int pnp_disable_dev(struct pnp_dev
*dev
)
442 error
= pnp_stop_dev(dev
);
448 /* release the resources so that other devices can use them */
449 mutex_lock(&pnp_res_mutex
);
450 pnp_clean_resource_table(dev
);
451 mutex_unlock(&pnp_res_mutex
);
456 EXPORT_SYMBOL(pnp_start_dev
);
457 EXPORT_SYMBOL(pnp_stop_dev
);
458 EXPORT_SYMBOL(pnp_activate_dev
);
459 EXPORT_SYMBOL(pnp_disable_dev
);