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 int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
23 struct resource *res, local_res;
25 res = pnp_get_resource(dev, IORESOURCE_IO, idx);
27 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
28 "flags %#lx\n", idx, (unsigned long long) res->start,
29 (unsigned long long) res->end, res->flags);
34 res->flags = rule->flags | IORESOURCE_AUTO;
39 res->flags |= IORESOURCE_DISABLED;
40 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
44 res->start = rule->min;
45 res->end = res->start + rule->size - 1;
47 while (!pnp_check_port(dev, res)) {
48 res->start += rule->align;
49 res->end = res->start + rule->size - 1;
50 if (res->start > rule->max || !rule->align) {
51 pnp_dbg(&dev->dev, " couldn't assign io %d "
52 "(min %#llx max %#llx)\n", idx,
53 (unsigned long long) rule->min,
54 (unsigned long long) rule->max);
60 pnp_add_io_resource(dev, res->start, res->end, res->flags);
64 static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
66 struct resource *res, local_res;
68 res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
70 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
71 "flags %#lx\n", idx, (unsigned long long) res->start,
72 (unsigned long long) res->end, res->flags);
77 res->flags = rule->flags | IORESOURCE_AUTO;
81 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
82 res->flags |= IORESOURCE_READONLY;
83 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
84 res->flags |= IORESOURCE_CACHEABLE;
85 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
86 res->flags |= IORESOURCE_RANGELENGTH;
87 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
88 res->flags |= IORESOURCE_SHADOWABLE;
91 res->flags |= IORESOURCE_DISABLED;
92 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
96 res->start = rule->min;
97 res->end = res->start + rule->size - 1;
99 while (!pnp_check_mem(dev, res)) {
100 res->start += rule->align;
101 res->end = res->start + rule->size - 1;
102 if (res->start > rule->max || !rule->align) {
103 pnp_dbg(&dev->dev, " couldn't assign mem %d "
104 "(min %#llx max %#llx)\n", idx,
105 (unsigned long long) rule->min,
106 (unsigned long long) rule->max);
112 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
116 static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
118 struct resource *res, local_res;
121 /* IRQ priority: this table is good for i386 */
122 static unsigned short xtab[16] = {
123 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
126 res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
128 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
129 idx, (int) res->start, res->flags);
134 res->flags = rule->flags | IORESOURCE_AUTO;
138 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
139 res->flags |= IORESOURCE_DISABLED;
140 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
144 /* TBD: need check for >16 IRQ */
145 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
146 if (res->start < PNP_IRQ_NR) {
147 res->end = res->start;
150 for (i = 0; i < 16; i++) {
151 if (test_bit(xtab[i], rule->map.bits)) {
152 res->start = res->end = xtab[i];
153 if (pnp_check_irq(dev, res))
158 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
161 res->flags |= IORESOURCE_DISABLED;
162 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
166 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
170 pnp_add_irq_resource(dev, res->start, res->flags);
174 static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
176 struct resource *res, local_res;
179 /* DMA priority: this table is good for i386 */
180 static unsigned short xtab[8] = {
181 1, 3, 5, 6, 7, 0, 2, 4
184 res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
186 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
187 idx, (int) res->start, res->flags);
192 res->flags = rule->flags | IORESOURCE_AUTO;
196 for (i = 0; i < 8; i++) {
197 if (rule->map & (1 << xtab[i])) {
198 res->start = res->end = xtab[i];
199 if (pnp_check_dma(dev, res))
203 #ifdef MAX_DMA_CHANNELS
204 res->start = res->end = MAX_DMA_CHANNELS;
206 res->flags |= IORESOURCE_DISABLED;
207 pnp_dbg(&dev->dev, " disable dma %d\n", idx);
210 pnp_add_dma_resource(dev, res->start, res->flags);
214 void pnp_init_resources(struct pnp_dev *dev)
216 pnp_free_resources(dev);
219 static void pnp_clean_resource_table(struct pnp_dev *dev)
221 struct pnp_resource *pnp_res, *tmp;
223 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
224 if (pnp_res->res.flags & IORESOURCE_AUTO)
225 pnp_free_resource(pnp_res);
230 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
231 * @dev: pointer to the desired device
232 * @set: the dependent function number
234 static int pnp_assign_resources(struct pnp_dev *dev, int set)
236 struct pnp_option *option;
237 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
240 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
241 mutex_lock(&pnp_res_mutex);
242 pnp_clean_resource_table(dev);
244 list_for_each_entry(option, &dev->options, list) {
245 if (pnp_option_is_dependent(option) &&
246 pnp_option_set(option) != set)
249 switch (option->type) {
251 ret = pnp_assign_port(dev, &option->u.port, nport++);
254 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
257 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
260 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
270 mutex_unlock(&pnp_res_mutex);
272 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
273 pnp_clean_resource_table(dev);
275 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
280 * pnp_auto_config_dev - automatically assigns resources to a device
281 * @dev: pointer to the desired device
283 int pnp_auto_config_dev(struct pnp_dev *dev)
287 if (!pnp_can_configure(dev)) {
288 pnp_dbg(&dev->dev, "configuration not supported\n");
292 ret = pnp_assign_resources(dev, 0);
296 for (i = 1; i < dev->num_dependent_sets; i++) {
297 ret = pnp_assign_resources(dev, i);
302 dev_err(&dev->dev, "unable to assign resources\n");
307 * pnp_start_dev - low-level start of the PnP device
308 * @dev: pointer to the desired device
310 * assumes that resources have already been allocated
312 int pnp_start_dev(struct pnp_dev *dev)
314 if (!pnp_can_write(dev)) {
315 pnp_dbg(&dev->dev, "activation not supported\n");
319 dbg_pnp_show_resources(dev, "pnp_start_dev");
320 if (dev->protocol->set(dev) < 0) {
321 dev_err(&dev->dev, "activation failed\n");
325 dev_info(&dev->dev, "activated\n");
330 * pnp_stop_dev - low-level disable of the PnP device
331 * @dev: pointer to the desired device
333 * does not free resources
335 int pnp_stop_dev(struct pnp_dev *dev)
337 if (!pnp_can_disable(dev)) {
338 pnp_dbg(&dev->dev, "disabling not supported\n");
341 if (dev->protocol->disable(dev) < 0) {
342 dev_err(&dev->dev, "disable failed\n");
346 dev_info(&dev->dev, "disabled\n");
351 * pnp_activate_dev - activates a PnP device for use
352 * @dev: pointer to the desired device
354 * does not validate or set resources so be careful.
356 int pnp_activate_dev(struct pnp_dev *dev)
363 /* ensure resources are allocated */
364 if (pnp_auto_config_dev(dev))
367 error = pnp_start_dev(dev);
376 * pnp_disable_dev - disables device
377 * @dev: pointer to the desired device
379 * inform the correct pnp protocol so that resources can be used by other devices
381 int pnp_disable_dev(struct pnp_dev *dev)
388 error = pnp_stop_dev(dev);
394 /* release the resources so that other devices can use them */
395 mutex_lock(&pnp_res_mutex);
396 pnp_clean_resource_table(dev);
397 mutex_unlock(&pnp_res_mutex);
402 EXPORT_SYMBOL(pnp_start_dev);
403 EXPORT_SYMBOL(pnp_stop_dev);
404 EXPORT_SYMBOL(pnp_activate_dev);
405 EXPORT_SYMBOL(pnp_disable_dev);