1 // SPDX-License-Identifier: GPL-2.0
3 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
5 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
6 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
7 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/pnp.h>
16 #include <linux/bitmap.h>
17 #include <linux/mutex.h>
20 DEFINE_MUTEX(pnp_res_mutex);
22 static struct resource *pnp_find_resource(struct pnp_dev *dev,
27 struct resource *res = pnp_get_resource(dev, type, bar);
29 /* when the resource already exists, set its resource bits from rule */
31 res->flags &= ~IORESOURCE_BITS;
32 res->flags |= rule & IORESOURCE_BITS;
38 static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
40 struct resource *res, local_res;
42 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx);
44 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
45 "flags %#lx\n", idx, (unsigned long long) res->start,
46 (unsigned long long) res->end, res->flags);
51 res->flags = rule->flags | IORESOURCE_AUTO;
56 res->flags |= IORESOURCE_DISABLED;
57 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
61 res->start = rule->min;
62 res->end = res->start + rule->size - 1;
64 while (!pnp_check_port(dev, res)) {
65 res->start += rule->align;
66 res->end = res->start + rule->size - 1;
67 if (res->start > rule->max || !rule->align) {
68 pnp_dbg(&dev->dev, " couldn't assign io %d "
69 "(min %#llx max %#llx)\n", idx,
70 (unsigned long long) rule->min,
71 (unsigned long long) rule->max);
77 pnp_add_io_resource(dev, res->start, res->end, res->flags);
81 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
83 struct resource *res, local_res;
85 res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx);
87 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
88 "flags %#lx\n", idx, (unsigned long long) res->start,
89 (unsigned long long) res->end, res->flags);
94 res->flags = rule->flags | IORESOURCE_AUTO;
98 /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */
99 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
100 res->flags |= IORESOURCE_READONLY;
101 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
102 res->flags |= IORESOURCE_RANGELENGTH;
103 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
104 res->flags |= IORESOURCE_SHADOWABLE;
107 res->flags |= IORESOURCE_DISABLED;
108 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
112 res->start = rule->min;
113 res->end = res->start + rule->size - 1;
115 while (!pnp_check_mem(dev, res)) {
116 res->start += rule->align;
117 res->end = res->start + rule->size - 1;
118 if (res->start > rule->max || !rule->align) {
119 pnp_dbg(&dev->dev, " couldn't assign mem %d "
120 "(min %#llx max %#llx)\n", idx,
121 (unsigned long long) rule->min,
122 (unsigned long long) rule->max);
128 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
132 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
134 struct resource *res, local_res;
137 /* IRQ priority: this table is good for i386 */
138 static unsigned short xtab[16] = {
139 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
142 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
144 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
145 idx, (int) res->start, res->flags);
150 res->flags = rule->flags | IORESOURCE_AUTO;
154 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
155 res->flags |= IORESOURCE_DISABLED;
156 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
160 /* TBD: need check for >16 IRQ */
161 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
162 if (res->start < PNP_IRQ_NR) {
163 res->end = res->start;
166 for (i = 0; i < 16; i++) {
167 if (test_bit(xtab[i], rule->map.bits)) {
168 res->start = res->end = xtab[i];
169 if (pnp_check_irq(dev, res))
174 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
177 res->flags |= IORESOURCE_DISABLED;
178 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
182 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
186 pnp_add_irq_resource(dev, res->start, res->flags);
190 #ifdef CONFIG_ISA_DMA_API
191 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
193 struct resource *res, local_res;
196 /* DMA priority: this table is good for i386 */
197 static unsigned short xtab[8] = {
198 1, 3, 5, 6, 7, 0, 2, 4
201 res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
203 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
204 idx, (int) res->start, res->flags);
209 res->flags = rule->flags | IORESOURCE_AUTO;
214 res->flags |= IORESOURCE_DISABLED;
215 pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
219 for (i = 0; i < 8; i++) {
220 if (rule->map & (1 << xtab[i])) {
221 res->start = res->end = xtab[i];
222 if (pnp_check_dma(dev, res))
227 pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
231 pnp_add_dma_resource(dev, res->start, res->flags);
234 #endif /* CONFIG_ISA_DMA_API */
236 void pnp_init_resources(struct pnp_dev *dev)
238 pnp_free_resources(dev);
241 static void pnp_clean_resource_table(struct pnp_dev *dev)
243 struct pnp_resource *pnp_res, *tmp;
245 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
246 if (pnp_res->res.flags & IORESOURCE_AUTO)
247 pnp_free_resource(pnp_res);
252 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
253 * @dev: pointer to the desired device
254 * @set: the dependent function number
256 static int pnp_assign_resources(struct pnp_dev *dev, int set)
258 struct pnp_option *option;
259 int nport = 0, nmem = 0, nirq = 0;
260 int ndma __maybe_unused = 0;
263 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
264 mutex_lock(&pnp_res_mutex);
265 pnp_clean_resource_table(dev);
267 list_for_each_entry(option, &dev->options, list) {
268 if (pnp_option_is_dependent(option) &&
269 pnp_option_set(option) != set)
272 switch (option->type) {
274 ret = pnp_assign_port(dev, &option->u.port, nport++);
277 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
280 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
282 #ifdef CONFIG_ISA_DMA_API
284 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
295 mutex_unlock(&pnp_res_mutex);
297 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
298 pnp_clean_resource_table(dev);
300 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
305 * pnp_auto_config_dev - automatically assigns resources to a device
306 * @dev: pointer to the desired device
308 int pnp_auto_config_dev(struct pnp_dev *dev)
312 if (!pnp_can_configure(dev)) {
313 pnp_dbg(&dev->dev, "configuration not supported\n");
317 ret = pnp_assign_resources(dev, 0);
321 for (i = 1; i < dev->num_dependent_sets; i++) {
322 ret = pnp_assign_resources(dev, i);
327 dev_err(&dev->dev, "unable to assign resources\n");
332 * pnp_start_dev - low-level start of the PnP device
333 * @dev: pointer to the desired device
335 * assumes that resources have already been allocated
337 int pnp_start_dev(struct pnp_dev *dev)
339 if (!pnp_can_write(dev)) {
340 pnp_dbg(&dev->dev, "activation not supported\n");
344 dbg_pnp_show_resources(dev, "pnp_start_dev");
345 if (dev->protocol->set(dev) < 0) {
346 dev_err(&dev->dev, "activation failed\n");
350 dev_info(&dev->dev, "activated\n");
353 EXPORT_SYMBOL(pnp_start_dev);
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");
375 EXPORT_SYMBOL(pnp_stop_dev);
378 * pnp_activate_dev - activates a PnP device for use
379 * @dev: pointer to the desired device
381 * does not validate or set resources so be careful.
383 int pnp_activate_dev(struct pnp_dev *dev)
390 /* ensure resources are allocated */
391 if (pnp_auto_config_dev(dev))
394 error = pnp_start_dev(dev);
401 EXPORT_SYMBOL(pnp_activate_dev);
404 * pnp_disable_dev - disables device
405 * @dev: pointer to the desired device
407 * inform the correct pnp protocol so that resources can be used by other devices
409 int pnp_disable_dev(struct pnp_dev *dev)
416 error = pnp_stop_dev(dev);
422 /* release the resources so that other devices can use them */
423 mutex_lock(&pnp_res_mutex);
424 pnp_clean_resource_table(dev);
425 mutex_unlock(&pnp_res_mutex);
429 EXPORT_SYMBOL(pnp_disable_dev);