blob: 17c95188bd11b9805486c7bb9ca6f308029cc7df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
3 *
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@suse.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6 *
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/errno.h>
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/pnp.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080014#include <linux/slab.h>
15#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "base.h"
17
18DECLARE_MUTEX(pnp_res_mutex);
19
20static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
21{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -070022 resource_size_t *start, *end;
23 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25 if (!dev || !rule)
26 return -EINVAL;
27
28 if (idx >= PNP_MAX_PORT) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070029 pnp_err
30 ("More than 4 ports is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 /* pretend we were successful so at least the manager won't try again */
32 return 1;
33 }
34
35 /* check if this resource has been manually set, if so skip */
36 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
37 return 1;
38
39 start = &dev->res.port_resource[idx].start;
40 end = &dev->res.port_resource[idx].end;
41 flags = &dev->res.port_resource[idx].flags;
42
43 /* set the initial values */
44 *flags |= rule->flags | IORESOURCE_IO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070045 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 if (!rule->size) {
48 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070049 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
51
52 *start = rule->min;
53 *end = *start + rule->size - 1;
54
55 /* run through until pnp_check_port is happy */
56 while (!pnp_check_port(dev, idx)) {
57 *start += rule->align;
58 *end = *start + rule->size - 1;
59 if (*start > rule->max || !rule->align)
60 return 0;
61 }
62 return 1;
63}
64
65static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
66{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -070067 resource_size_t *start, *end;
68 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 if (!dev || !rule)
71 return -EINVAL;
72
73 if (idx >= PNP_MAX_MEM) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070074 pnp_err
75 ("More than 8 mems is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* pretend we were successful so at least the manager won't try again */
77 return 1;
78 }
79
80 /* check if this resource has been manually set, if so skip */
81 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
82 return 1;
83
84 start = &dev->res.mem_resource[idx].start;
85 end = &dev->res.mem_resource[idx].end;
86 flags = &dev->res.mem_resource[idx].flags;
87
88 /* set the initial values */
89 *flags |= rule->flags | IORESOURCE_MEM;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070090 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* convert pnp flags to standard Linux flags */
93 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
94 *flags |= IORESOURCE_READONLY;
95 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
96 *flags |= IORESOURCE_CACHEABLE;
97 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
98 *flags |= IORESOURCE_RANGELENGTH;
99 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
100 *flags |= IORESOURCE_SHADOWABLE;
101
102 if (!rule->size) {
103 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700104 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
107 *start = rule->min;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700108 *end = *start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /* run through until pnp_check_mem is happy */
111 while (!pnp_check_mem(dev, idx)) {
112 *start += rule->align;
113 *end = *start + rule->size - 1;
114 if (*start > rule->max || !rule->align)
115 return 0;
116 }
117 return 1;
118}
119
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700120static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700122 resource_size_t *start, *end;
123 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int i;
125
126 /* IRQ priority: this table is good for i386 */
127 static unsigned short xtab[16] = {
128 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
129 };
130
131 if (!dev || !rule)
132 return -EINVAL;
133
134 if (idx >= PNP_MAX_IRQ) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700135 pnp_err
136 ("More than 2 irqs is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* pretend we were successful so at least the manager won't try again */
138 return 1;
139 }
140
141 /* check if this resource has been manually set, if so skip */
142 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
143 return 1;
144
145 start = &dev->res.irq_resource[idx].start;
146 end = &dev->res.irq_resource[idx].end;
147 flags = &dev->res.irq_resource[idx].flags;
148
149 /* set the initial values */
150 *flags |= rule->flags | IORESOURCE_IRQ;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700151 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
154 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700155 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
158 /* TBD: need check for >16 IRQ */
159 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
160 if (*start < PNP_IRQ_NR) {
161 *end = *start;
162 return 1;
163 }
164 for (i = 0; i < 16; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700165 if (test_bit(xtab[i], rule->map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 *start = *end = xtab[i];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700167 if (pnp_check_irq(dev, idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return 1;
169 }
170 }
171 return 0;
172}
173
174static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
175{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700176 resource_size_t *start, *end;
177 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int i;
179
180 /* DMA priority: this table is good for i386 */
181 static unsigned short xtab[8] = {
182 1, 3, 5, 6, 7, 0, 2, 4
183 };
184
185 if (!dev || !rule)
186 return -EINVAL;
187
188 if (idx >= PNP_MAX_DMA) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700189 pnp_err
190 ("More than 2 dmas is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* pretend we were successful so at least the manager won't try again */
192 return 1;
193 }
194
195 /* check if this resource has been manually set, if so skip */
196 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
197 return 1;
198
199 start = &dev->res.dma_resource[idx].start;
200 end = &dev->res.dma_resource[idx].end;
201 flags = &dev->res.dma_resource[idx].flags;
202
203 /* set the initial values */
204 *flags |= rule->flags | IORESOURCE_DMA;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700205 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 if (!rule->map) {
208 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700209 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700213 if (rule->map & (1 << xtab[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *start = *end = xtab[i];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700215 if (pnp_check_dma(dev, idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 1;
217 }
218 }
219 return 0;
220}
221
222/**
223 * pnp_init_resources - Resets a resource table to default values.
224 * @table: pointer to the desired resource table
225 *
226 */
227void pnp_init_resource_table(struct pnp_resource_table *table)
228{
229 int idx;
230 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
231 table->irq_resource[idx].name = NULL;
232 table->irq_resource[idx].start = -1;
233 table->irq_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700234 table->irq_resource[idx].flags =
235 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
238 table->dma_resource[idx].name = NULL;
239 table->dma_resource[idx].start = -1;
240 table->dma_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700241 table->dma_resource[idx].flags =
242 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
245 table->port_resource[idx].name = NULL;
246 table->port_resource[idx].start = 0;
247 table->port_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700248 table->port_resource[idx].flags =
249 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
252 table->mem_resource[idx].name = NULL;
253 table->mem_resource[idx].start = 0;
254 table->mem_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700255 table->mem_resource[idx].flags =
256 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258}
259
260/**
261 * pnp_clean_resources - clears resources that were not manually set
Martin Waitz67be2dd2005-05-01 08:59:26 -0700262 * @res: the resources to clean
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
264 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700265static void pnp_clean_resource_table(struct pnp_resource_table *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int idx;
268 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
269 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
270 continue;
271 res->irq_resource[idx].start = -1;
272 res->irq_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700273 res->irq_resource[idx].flags =
274 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
277 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
278 continue;
279 res->dma_resource[idx].start = -1;
280 res->dma_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700281 res->dma_resource[idx].flags =
282 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
285 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
286 continue;
287 res->port_resource[idx].start = 0;
288 res->port_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700289 res->port_resource[idx].flags =
290 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
293 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
294 continue;
295 res->mem_resource[idx].start = 0;
296 res->mem_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700297 res->mem_resource[idx].flags =
298 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
302/**
303 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
304 * @dev: pointer to the desired device
305 * @depnum: the dependent function number
306 *
307 * Only set depnum to 0 if the device does not have dependent options.
308 */
309static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
310{
311 struct pnp_port *port;
312 struct pnp_mem *mem;
313 struct pnp_irq *irq;
314 struct pnp_dma *dma;
315 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
316
317 if (!pnp_can_configure(dev))
318 return -ENODEV;
319
320 down(&pnp_res_mutex);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700321 pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (dev->independent) {
323 port = dev->independent->port;
324 mem = dev->independent->mem;
325 irq = dev->independent->irq;
326 dma = dev->independent->dma;
327 while (port) {
328 if (!pnp_assign_port(dev, port, nport))
329 goto fail;
330 nport++;
331 port = port->next;
332 }
333 while (mem) {
334 if (!pnp_assign_mem(dev, mem, nmem))
335 goto fail;
336 nmem++;
337 mem = mem->next;
338 }
339 while (irq) {
340 if (!pnp_assign_irq(dev, irq, nirq))
341 goto fail;
342 nirq++;
343 irq = irq->next;
344 }
345 while (dma) {
346 if (!pnp_assign_dma(dev, dma, ndma))
347 goto fail;
348 ndma++;
349 dma = dma->next;
350 }
351 }
352
353 if (depnum) {
354 struct pnp_option *dep;
355 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700356 for (i = 1, dep = dev->dependent; i < depnum;
357 i++, dep = dep->next)
358 if (!dep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto fail;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700360 port = dep->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 mem = dep->mem;
362 irq = dep->irq;
363 dma = dep->dma;
364 while (port) {
365 if (!pnp_assign_port(dev, port, nport))
366 goto fail;
367 nport++;
368 port = port->next;
369 }
370 while (mem) {
371 if (!pnp_assign_mem(dev, mem, nmem))
372 goto fail;
373 nmem++;
374 mem = mem->next;
375 }
376 while (irq) {
377 if (!pnp_assign_irq(dev, irq, nirq))
378 goto fail;
379 nirq++;
380 irq = irq->next;
381 }
382 while (dma) {
383 if (!pnp_assign_dma(dev, dma, ndma))
384 goto fail;
385 ndma++;
386 dma = dma->next;
387 }
388 } else if (dev->dependent)
389 goto fail;
390
391 up(&pnp_res_mutex);
392 return 1;
393
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700394 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 pnp_clean_resource_table(&dev->res);
396 up(&pnp_res_mutex);
397 return 0;
398}
399
400/**
401 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
402 * @dev: pointer to the desired device
403 * @res: pointer to the new resource config
Martin Waitz3d410882005-06-23 22:05:21 -0700404 * @mode: 0 or PNP_CONFIG_FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 *
406 * This function can be used by drivers that want to manually set thier resources.
407 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700408int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
409 int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700412 struct pnp_resource_table *bak;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (!dev || !res)
414 return -EINVAL;
415 if (!pnp_can_configure(dev))
416 return -ENODEV;
417 bak = pnp_alloc(sizeof(struct pnp_resource_table));
418 if (!bak)
419 return -ENOMEM;
420 *bak = dev->res;
421
422 down(&pnp_res_mutex);
423 dev->res = *res;
424 if (!(mode & PNP_CONFIG_FORCE)) {
425 for (i = 0; i < PNP_MAX_PORT; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700426 if (!pnp_check_port(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 goto fail;
428 }
429 for (i = 0; i < PNP_MAX_MEM; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700430 if (!pnp_check_mem(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto fail;
432 }
433 for (i = 0; i < PNP_MAX_IRQ; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700434 if (!pnp_check_irq(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 goto fail;
436 }
437 for (i = 0; i < PNP_MAX_DMA; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700438 if (!pnp_check_dma(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto fail;
440 }
441 }
442 up(&pnp_res_mutex);
443
444 kfree(bak);
445 return 0;
446
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700447 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 dev->res = *bak;
449 up(&pnp_res_mutex);
450 kfree(bak);
451 return -EINVAL;
452}
453
454/**
455 * pnp_auto_config_dev - automatically assigns resources to a device
456 * @dev: pointer to the desired device
457 *
458 */
459int pnp_auto_config_dev(struct pnp_dev *dev)
460{
461 struct pnp_option *dep;
462 int i = 1;
463
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700464 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -EINVAL;
466
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700467 if (!pnp_can_configure(dev)) {
468 pnp_dbg("Device %s does not support resource configuration.",
469 dev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENODEV;
471 }
472
473 if (!dev->dependent) {
474 if (pnp_assign_resources(dev, 0))
475 return 0;
476 } else {
477 dep = dev->dependent;
478 do {
479 if (pnp_assign_resources(dev, i))
480 return 0;
481 dep = dep->next;
482 i++;
483 } while (dep);
484 }
485
486 pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
487 return -EBUSY;
488}
489
490/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100491 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * @dev: pointer to the desired device
493 *
Pierre Ossman68094e32005-11-29 09:09:32 +0100494 * assumes that resources have alread been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100496
497int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!pnp_can_write(dev)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700500 pnp_dbg("Device %s does not support activation.",
501 dev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -EINVAL;
503 }
504
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700505 if (dev->protocol->set(dev, &dev->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
507 return -EIO;
508 }
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 pnp_info("Device %s activated.", dev->dev.bus_id);
511
Pierre Ossman68094e32005-11-29 09:09:32 +0100512 return 0;
513}
514
515/**
516 * pnp_stop_dev - low-level disable of the PnP device
517 * @dev: pointer to the desired device
518 *
519 * does not free resources
520 */
521
522int pnp_stop_dev(struct pnp_dev *dev)
523{
524 if (!pnp_can_disable(dev)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700525 pnp_dbg("Device %s does not support disabling.",
526 dev->dev.bus_id);
Pierre Ossman68094e32005-11-29 09:09:32 +0100527 return -EINVAL;
528 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700529 if (dev->protocol->disable(dev) < 0) {
Pierre Ossman68094e32005-11-29 09:09:32 +0100530 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
531 return -EIO;
532 }
533
534 pnp_info("Device %s disabled.", dev->dev.bus_id);
535
536 return 0;
537}
538
539/**
540 * pnp_activate_dev - activates a PnP device for use
541 * @dev: pointer to the desired device
542 *
543 * does not validate or set resources so be careful.
544 */
545int pnp_activate_dev(struct pnp_dev *dev)
546{
547 int error;
548
549 if (!dev)
550 return -EINVAL;
551 if (dev->active) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700552 return 0; /* the device is already active */
Pierre Ossman68094e32005-11-29 09:09:32 +0100553 }
554
555 /* ensure resources are allocated */
556 if (pnp_auto_config_dev(dev))
557 return -EBUSY;
558
559 error = pnp_start_dev(dev);
560 if (error)
561 return error;
562
563 dev->active = 1;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return 1;
566}
567
568/**
569 * pnp_disable_dev - disables device
570 * @dev: pointer to the desired device
571 *
572 * inform the correct pnp protocol so that resources can be used by other devices
573 */
574int pnp_disable_dev(struct pnp_dev *dev)
575{
Pierre Ossman68094e32005-11-29 09:09:32 +0100576 int error;
577
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700578 if (!dev)
579 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!dev->active) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700581 return 0; /* the device is already disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
Pierre Ossman68094e32005-11-29 09:09:32 +0100584 error = pnp_stop_dev(dev);
585 if (error)
586 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* release the resources so that other devices can use them */
591 down(&pnp_res_mutex);
592 pnp_clean_resource_table(&dev->res);
593 up(&pnp_res_mutex);
594
595 return 1;
596}
597
598/**
599 * pnp_resource_change - change one resource
600 * @resource: pointer to resource to be changed
601 * @start: start of region
602 * @size: size of region
603 *
604 */
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700605void pnp_resource_change(struct resource *resource, resource_size_t start,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700606 resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 if (resource == NULL)
609 return;
610 resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
611 resource->start = start;
612 resource->end = start + size - 1;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615EXPORT_SYMBOL(pnp_manual_config_dev);
Adrian Bunkb449f632005-11-07 01:01:48 -0800616#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617EXPORT_SYMBOL(pnp_auto_config_dev);
Adrian Bunkb449f632005-11-07 01:01:48 -0800618#endif
Pierre Ossman68094e32005-11-29 09:09:32 +0100619EXPORT_SYMBOL(pnp_start_dev);
620EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621EXPORT_SYMBOL(pnp_activate_dev);
622EXPORT_SYMBOL(pnp_disable_dev);
623EXPORT_SYMBOL(pnp_resource_change);
624EXPORT_SYMBOL(pnp_init_resource_table);