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/bitmap.h>
16 #include <linux/mutex.h>
19 DEFINE_MUTEX(pnp_res_mutex);
21 static struct resource *pnp_find_resource(struct pnp_dev *dev,
26 struct resource *res = pnp_get_resource(dev, type, bar);
28 /* when the resource already exists, set its resource bits from rule */
30 res->flags &= ~IORESOURCE_BITS;
31 res->flags |= rule & IORESOURCE_BITS;
37 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
39 struct resource *res, local_res;
41 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx);
43 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
44 "flags %#lx\n", idx, (unsigned long long) res->start,
45 (unsigned long long) res->end, res->flags);
50 res->flags = rule->flags | IORESOURCE_AUTO;
55 res->flags |= IORESOURCE_DISABLED;
56 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
60 res->start = rule->min;
61 res->end = res->start + rule->size - 1;
63 while (!pnp_check_port(dev, res)) {
64 res->start += rule->align;
65 res->end = res->start + rule->size - 1;
66 if (res->start > rule->max || !rule->align) {
67 pnp_dbg(&dev->dev, " couldn't assign io %d "
68 "(min %#llx max %#llx)\n", idx,
69 (unsigned long long) rule->min,
70 (unsigned long long) rule->max);
76 pnp_add_io_resource(dev, res->start, res->end, res->flags);
80 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
82 struct resource *res, local_res;
84 res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx);
86 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
87 "flags %#lx\n", idx, (unsigned long long) res->start,
88 (unsigned long long) res->end, res->flags);
93 res->flags = rule->flags | IORESOURCE_AUTO;
97 /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */
98 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
99 res->flags |= IORESOURCE_READONLY;
100 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
101 res->flags |= IORESOURCE_CACHEABLE;
102 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
103 res->flags |= IORESOURCE_RANGELENGTH;
104 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
105 res->flags |= IORESOURCE_SHADOWABLE;
108 res->flags |= IORESOURCE_DISABLED;
109 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
113 res->start = rule->min;
114 res->end = res->start + rule->size - 1;
116 while (!pnp_check_mem(dev, res)) {
117 res->start += rule->align;
118 res->end = res->start + rule->size - 1;
119 if (res->start > rule->max || !rule->align) {
120 pnp_dbg(&dev->dev, " couldn't assign mem %d "
121 "(min %#llx max %#llx)\n", idx,
122 (unsigned long long) rule->min,
123 (unsigned long long) rule->max);
129 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
133 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
135 struct resource *res, local_res;
138 /* IRQ priority: this table is good for i386 */
139 static unsigned short xtab[16] = {
140 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
143 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
145 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
146 idx, (int) res->start, res->flags);
151 res->flags = rule->flags | IORESOURCE_AUTO;
155 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
156 res->flags |= IORESOURCE_DISABLED;
157 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
161 /* TBD: need check for >16 IRQ */
162 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
163 if (res->start < PNP_IRQ_NR) {
164 res->end = res->start;
167 for (i = 0; i < 16; i++) {
168 if (test_bit(xtab[i], rule->map.bits)) {
169 res->start = res->end = xtab[i];
170 if (pnp_check_irq(dev, res))
175 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
178 res->flags |= IORESOURCE_DISABLED;
179 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
183 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
187 pnp_add_irq_resource(dev, res->start, res->flags);
191 #ifdef CONFIG_ISA_DMA_API
192 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
194 struct resource *res, local_res;
197 /* DMA priority: this table is good for i386 */
198 static unsigned short xtab[8] = {
199 1, 3, 5, 6, 7, 0, 2, 4
202 res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
204 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
205 idx, (int) res->start, res->flags);
210 res->flags = rule->flags | IORESOURCE_AUTO;
215 res->flags |= IORESOURCE_DISABLED;
216 pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
220 for (i = 0; i < 8; i++) {
221 if (rule->map & (1 << xtab[i])) {
222 res->start = res->end = xtab[i];
223 if (pnp_check_dma(dev, res))
228 pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
232 pnp_add_dma_resource(dev, res->start, res->flags);
235 #endif /* CONFIG_ISA_DMA_API */
237 void pnp_init_resources(struct pnp_dev *dev)
239 pnp_free_resources(dev);
242 static void pnp_clean_resource_table(struct pnp_dev *dev)
244 struct pnp_resource *pnp_res, *tmp;
246 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
247 if (pnp_res->res.flags & IORESOURCE_AUTO)
248 pnp_free_resource(pnp_res);
253 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
254 * @dev: pointer to the desired device
255 * @set: the dependent function number
257 static int pnp_assign_resources(struct pnp_dev *dev, int set)
259 struct pnp_option *option;
260 int nport = 0, nmem = 0, nirq = 0;
261 int ndma __maybe_unused = 0;
264 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
265 mutex_lock(&pnp_res_mutex);
266 pnp_clean_resource_table(dev);
268 list_for_each_entry(option, &dev->options, list) {
269 if (pnp_option_is_dependent(option) &&
270 pnp_option_set(option) != set)
273 switch (option->type) {
275 ret = pnp_assign_port(dev, &option->u.port, nport++);
278 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
281 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
283 #ifdef CONFIG_ISA_DMA_API
285 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
296 mutex_unlock(&pnp_res_mutex);
298 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
299 pnp_clean_resource_table(dev);
301 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
306 * pnp_auto_config_dev - automatically assigns resources to a device
307 * @dev: pointer to the desired device
309 int pnp_auto_config_dev(struct pnp_dev *dev)
313 if (!pnp_can_configure(dev)) {
314 pnp_dbg(&dev->dev, "configuration not supported\n");
318 ret = pnp_assign_resources(dev, 0);
322 for (i = 1; i < dev->num_dependent_sets; i++) {
323 ret = pnp_assign_resources(dev, i);
328 dev_err(&dev->dev, "unable to assign resources\n");
333 * pnp_start_dev - low-level start of the PnP device
334 * @dev: pointer to the desired device
336 * assumes that resources have already been allocated
338 int pnp_start_dev(struct pnp_dev *dev)
340 if (!pnp_can_write(dev)) {
341 pnp_dbg(&dev->dev, "activation not supported\n");
345 dbg_pnp_show_resources(dev, "pnp_start_dev");
346 if (dev->protocol->set(dev) < 0) {
347 dev_err(&dev->dev, "activation failed\n");
351 dev_info(&dev->dev, "activated\n");
356 * pnp_stop_dev - low-level disable of the PnP device
357 * @dev: pointer to the desired device
359 * does not free resources
361 int pnp_stop_dev(struct pnp_dev *dev)
363 if (!pnp_can_disable(dev)) {
364 pnp_dbg(&dev->dev, "disabling not supported\n");
367 if (dev->protocol->disable(dev) < 0) {
368 dev_err(&dev->dev, "disable failed\n");
372 dev_info(&dev->dev, "disabled\n");
377 * pnp_activate_dev - activates a PnP device for use
378 * @dev: pointer to the desired device
380 * does not validate or set resources so be careful.
382 int pnp_activate_dev(struct pnp_dev *dev)
389 /* ensure resources are allocated */
390 if (pnp_auto_config_dev(dev))
393 error = pnp_start_dev(dev);
402 * pnp_disable_dev - disables device
403 * @dev: pointer to the desired device
405 * inform the correct pnp protocol so that resources can be used by other devices
407 int pnp_disable_dev(struct pnp_dev *dev)
414 error = pnp_stop_dev(dev);
420 /* release the resources so that other devices can use them */
421 mutex_lock(&pnp_res_mutex);
422 pnp_clean_resource_table(dev);
423 mutex_unlock(&pnp_res_mutex);
428 EXPORT_SYMBOL(pnp_start_dev);
429 EXPORT_SYMBOL(pnp_stop_dev);
430 EXPORT_SYMBOL(pnp_activate_dev);
431 EXPORT_SYMBOL(pnp_disable_dev);