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>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/pnp.h>
15 #include <linux/slab.h>
16 #include <linux/bitmap.h>
17 #include <linux/mutex.h>
20 DEFINE_MUTEX(pnp_res_mutex
);
22 static int pnp_assign_port(struct pnp_dev
*dev
, struct pnp_port
*rule
, int idx
)
24 struct resource
*res
, local_res
;
26 res
= pnp_get_resource(dev
, IORESOURCE_IO
, idx
);
28 dev_dbg(&dev
->dev
, " io %d already set to %#llx-%#llx "
29 "flags %#lx\n", idx
, (unsigned long long) res
->start
,
30 (unsigned long long) res
->end
, res
->flags
);
35 res
->flags
= rule
->flags
| IORESOURCE_AUTO
;
40 res
->flags
|= IORESOURCE_DISABLED
;
41 dev_dbg(&dev
->dev
, " io %d disabled\n", idx
);
45 res
->start
= rule
->min
;
46 res
->end
= res
->start
+ rule
->size
- 1;
48 while (!pnp_check_port(dev
, res
)) {
49 res
->start
+= rule
->align
;
50 res
->end
= res
->start
+ rule
->size
- 1;
51 if (res
->start
> rule
->max
|| !rule
->align
) {
52 dev_dbg(&dev
->dev
, " couldn't assign io %d "
53 "(min %#llx max %#llx)\n", idx
,
54 (unsigned long long) rule
->min
,
55 (unsigned long long) rule
->max
);
61 pnp_add_io_resource(dev
, res
->start
, res
->end
, res
->flags
);
65 static int pnp_assign_mem(struct pnp_dev
*dev
, struct pnp_mem
*rule
, int idx
)
67 struct resource
*res
, local_res
;
69 res
= pnp_get_resource(dev
, IORESOURCE_MEM
, idx
);
71 dev_dbg(&dev
->dev
, " mem %d already set to %#llx-%#llx "
72 "flags %#lx\n", idx
, (unsigned long long) res
->start
,
73 (unsigned long long) res
->end
, res
->flags
);
78 res
->flags
= rule
->flags
| IORESOURCE_AUTO
;
82 if (!(rule
->flags
& IORESOURCE_MEM_WRITEABLE
))
83 res
->flags
|= IORESOURCE_READONLY
;
84 if (rule
->flags
& IORESOURCE_MEM_CACHEABLE
)
85 res
->flags
|= IORESOURCE_CACHEABLE
;
86 if (rule
->flags
& IORESOURCE_MEM_RANGELENGTH
)
87 res
->flags
|= IORESOURCE_RANGELENGTH
;
88 if (rule
->flags
& IORESOURCE_MEM_SHADOWABLE
)
89 res
->flags
|= IORESOURCE_SHADOWABLE
;
92 res
->flags
|= IORESOURCE_DISABLED
;
93 dev_dbg(&dev
->dev
, " mem %d disabled\n", idx
);
97 res
->start
= rule
->min
;
98 res
->end
= res
->start
+ rule
->size
- 1;
100 while (!pnp_check_mem(dev
, res
)) {
101 res
->start
+= rule
->align
;
102 res
->end
= res
->start
+ rule
->size
- 1;
103 if (res
->start
> rule
->max
|| !rule
->align
) {
104 dev_dbg(&dev
->dev
, " couldn't assign mem %d "
105 "(min %#llx max %#llx)\n", idx
,
106 (unsigned long long) rule
->min
,
107 (unsigned long long) rule
->max
);
113 pnp_add_mem_resource(dev
, res
->start
, res
->end
, res
->flags
);
117 static int pnp_assign_irq(struct pnp_dev
*dev
, struct pnp_irq
*rule
, int idx
)
119 struct resource
*res
, local_res
;
122 /* IRQ priority: this table is good for i386 */
123 static unsigned short xtab
[16] = {
124 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
127 res
= pnp_get_resource(dev
, IORESOURCE_IRQ
, idx
);
129 dev_dbg(&dev
->dev
, " irq %d already set to %d flags %#lx\n",
130 idx
, (int) res
->start
, res
->flags
);
135 res
->flags
= rule
->flags
| IORESOURCE_AUTO
;
139 if (bitmap_empty(rule
->map
.bits
, PNP_IRQ_NR
)) {
140 res
->flags
|= IORESOURCE_DISABLED
;
141 dev_dbg(&dev
->dev
, " irq %d disabled\n", idx
);
145 /* TBD: need check for >16 IRQ */
146 res
->start
= find_next_bit(rule
->map
.bits
, PNP_IRQ_NR
, 16);
147 if (res
->start
< PNP_IRQ_NR
) {
148 res
->end
= res
->start
;
151 for (i
= 0; i
< 16; i
++) {
152 if (test_bit(xtab
[i
], rule
->map
.bits
)) {
153 res
->start
= res
->end
= xtab
[i
];
154 if (pnp_check_irq(dev
, res
))
159 if (rule
->flags
& IORESOURCE_IRQ_OPTIONAL
) {
162 res
->flags
|= IORESOURCE_DISABLED
;
163 dev_dbg(&dev
->dev
, " irq %d disabled (optional)\n", idx
);
167 dev_dbg(&dev
->dev
, " couldn't assign irq %d\n", idx
);
171 pnp_add_irq_resource(dev
, res
->start
, res
->flags
);
175 static int pnp_assign_dma(struct pnp_dev
*dev
, struct pnp_dma
*rule
, int idx
)
177 struct resource
*res
, local_res
;
180 /* DMA priority: this table is good for i386 */
181 static unsigned short xtab
[8] = {
182 1, 3, 5, 6, 7, 0, 2, 4
185 res
= pnp_get_resource(dev
, IORESOURCE_DMA
, idx
);
187 dev_dbg(&dev
->dev
, " dma %d already set to %d flags %#lx\n",
188 idx
, (int) res
->start
, res
->flags
);
193 res
->flags
= rule
->flags
| IORESOURCE_AUTO
;
197 for (i
= 0; i
< 8; i
++) {
198 if (rule
->map
& (1 << xtab
[i
])) {
199 res
->start
= res
->end
= xtab
[i
];
200 if (pnp_check_dma(dev
, res
))
204 #ifdef MAX_DMA_CHANNELS
205 res
->start
= res
->end
= MAX_DMA_CHANNELS
;
207 res
->flags
|= IORESOURCE_DISABLED
;
208 dev_dbg(&dev
->dev
, " disable dma %d\n", idx
);
211 pnp_add_dma_resource(dev
, res
->start
, res
->flags
);
215 void pnp_init_resources(struct pnp_dev
*dev
)
217 pnp_free_resources(dev
);
220 static void pnp_clean_resource_table(struct pnp_dev
*dev
)
222 struct pnp_resource
*pnp_res
, *tmp
;
224 list_for_each_entry_safe(pnp_res
, tmp
, &dev
->resources
, list
) {
225 if (pnp_res
->res
.flags
& IORESOURCE_AUTO
)
226 pnp_free_resource(pnp_res
);
231 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
232 * @dev: pointer to the desired device
233 * @set: the dependent function number
235 static int pnp_assign_resources(struct pnp_dev
*dev
, int set
)
237 struct pnp_option
*option
;
238 int nport
= 0, nmem
= 0, nirq
= 0, ndma
= 0;
241 dev_dbg(&dev
->dev
, "pnp_assign_resources, try dependent set %d\n", set
);
242 mutex_lock(&pnp_res_mutex
);
243 pnp_clean_resource_table(dev
);
245 list_for_each_entry(option
, &dev
->options
, list
) {
246 if (pnp_option_is_dependent(option
) &&
247 pnp_option_set(option
) != set
)
250 switch (option
->type
) {
252 ret
= pnp_assign_port(dev
, &option
->u
.port
, nport
++);
255 ret
= pnp_assign_mem(dev
, &option
->u
.mem
, nmem
++);
258 ret
= pnp_assign_irq(dev
, &option
->u
.irq
, nirq
++);
261 ret
= pnp_assign_dma(dev
, &option
->u
.dma
, ndma
++);
271 mutex_unlock(&pnp_res_mutex
);
273 dev_dbg(&dev
->dev
, "pnp_assign_resources failed (%d)\n", ret
);
274 pnp_clean_resource_table(dev
);
276 dbg_pnp_show_resources(dev
, "pnp_assign_resources succeeded");
281 * pnp_auto_config_dev - automatically assigns resources to a device
282 * @dev: pointer to the desired device
284 int pnp_auto_config_dev(struct pnp_dev
*dev
)
288 if (!pnp_can_configure(dev
)) {
289 dev_dbg(&dev
->dev
, "configuration not supported\n");
293 ret
= pnp_assign_resources(dev
, 0);
297 for (i
= 1; i
< dev
->num_dependent_sets
; i
++) {
298 ret
= pnp_assign_resources(dev
, i
);
303 dev_err(&dev
->dev
, "unable to assign resources\n");
308 * pnp_start_dev - low-level start of the PnP device
309 * @dev: pointer to the desired device
311 * assumes that resources have already been allocated
313 int pnp_start_dev(struct pnp_dev
*dev
)
315 if (!pnp_can_write(dev
)) {
316 dev_dbg(&dev
->dev
, "activation not supported\n");
320 dbg_pnp_show_resources(dev
, "pnp_start_dev");
321 if (dev
->protocol
->set(dev
) < 0) {
322 dev_err(&dev
->dev
, "activation failed\n");
326 dev_info(&dev
->dev
, "activated\n");
331 * pnp_stop_dev - low-level disable of the PnP device
332 * @dev: pointer to the desired device
334 * does not free resources
336 int pnp_stop_dev(struct pnp_dev
*dev
)
338 if (!pnp_can_disable(dev
)) {
339 dev_dbg(&dev
->dev
, "disabling not supported\n");
342 if (dev
->protocol
->disable(dev
) < 0) {
343 dev_err(&dev
->dev
, "disable failed\n");
347 dev_info(&dev
->dev
, "disabled\n");
352 * pnp_activate_dev - activates a PnP device for use
353 * @dev: pointer to the desired device
355 * does not validate or set resources so be careful.
357 int pnp_activate_dev(struct pnp_dev
*dev
)
364 /* ensure resources are allocated */
365 if (pnp_auto_config_dev(dev
))
368 error
= pnp_start_dev(dev
);
377 * pnp_disable_dev - disables device
378 * @dev: pointer to the desired device
380 * inform the correct pnp protocol so that resources can be used by other devices
382 int pnp_disable_dev(struct pnp_dev
*dev
)
389 error
= pnp_stop_dev(dev
);
395 /* release the resources so that other devices can use them */
396 mutex_lock(&pnp_res_mutex
);
397 pnp_clean_resource_table(dev
);
398 mutex_unlock(&pnp_res_mutex
);
403 EXPORT_SYMBOL(pnp_start_dev
);
404 EXPORT_SYMBOL(pnp_stop_dev
);
405 EXPORT_SYMBOL(pnp_activate_dev
);
406 EXPORT_SYMBOL(pnp_disable_dev
);