blob: 3bda513a6bd36cb92ea092ef3fd4ae13c7a6ad19 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/errno.h>
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/pnp.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080013#include <linux/slab.h>
14#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "base.h"
16
17DECLARE_MUTEX(pnp_res_mutex);
18
19static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
20{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -070021 resource_size_t *start, *end;
22 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24 if (!dev || !rule)
25 return -EINVAL;
26
27 if (idx >= PNP_MAX_PORT) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070028 pnp_err
29 ("More than 4 ports is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 /* pretend we were successful so at least the manager won't try again */
31 return 1;
32 }
33
34 /* check if this resource has been manually set, if so skip */
35 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
36 return 1;
37
38 start = &dev->res.port_resource[idx].start;
39 end = &dev->res.port_resource[idx].end;
40 flags = &dev->res.port_resource[idx].flags;
41
42 /* set the initial values */
43 *flags |= rule->flags | IORESOURCE_IO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070044 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (!rule->size) {
47 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070048 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50
51 *start = rule->min;
52 *end = *start + rule->size - 1;
53
54 /* run through until pnp_check_port is happy */
55 while (!pnp_check_port(dev, idx)) {
56 *start += rule->align;
57 *end = *start + rule->size - 1;
58 if (*start > rule->max || !rule->align)
59 return 0;
60 }
61 return 1;
62}
63
64static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
65{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -070066 resource_size_t *start, *end;
67 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if (!dev || !rule)
70 return -EINVAL;
71
72 if (idx >= PNP_MAX_MEM) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070073 pnp_err
74 ("More than 8 mems is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* pretend we were successful so at least the manager won't try again */
76 return 1;
77 }
78
79 /* check if this resource has been manually set, if so skip */
80 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
81 return 1;
82
83 start = &dev->res.mem_resource[idx].start;
84 end = &dev->res.mem_resource[idx].end;
85 flags = &dev->res.mem_resource[idx].flags;
86
87 /* set the initial values */
88 *flags |= rule->flags | IORESOURCE_MEM;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070089 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /* convert pnp flags to standard Linux flags */
92 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
93 *flags |= IORESOURCE_READONLY;
94 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
95 *flags |= IORESOURCE_CACHEABLE;
96 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
97 *flags |= IORESOURCE_RANGELENGTH;
98 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
99 *flags |= IORESOURCE_SHADOWABLE;
100
101 if (!rule->size) {
102 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700103 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
106 *start = rule->min;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700107 *end = *start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* run through until pnp_check_mem is happy */
110 while (!pnp_check_mem(dev, idx)) {
111 *start += rule->align;
112 *end = *start + rule->size - 1;
113 if (*start > rule->max || !rule->align)
114 return 0;
115 }
116 return 1;
117}
118
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700119static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700121 resource_size_t *start, *end;
122 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int i;
124
125 /* IRQ priority: this table is good for i386 */
126 static unsigned short xtab[16] = {
127 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
128 };
129
130 if (!dev || !rule)
131 return -EINVAL;
132
133 if (idx >= PNP_MAX_IRQ) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700134 pnp_err
135 ("More than 2 irqs is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* pretend we were successful so at least the manager won't try again */
137 return 1;
138 }
139
140 /* check if this resource has been manually set, if so skip */
141 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
142 return 1;
143
144 start = &dev->res.irq_resource[idx].start;
145 end = &dev->res.irq_resource[idx].end;
146 flags = &dev->res.irq_resource[idx].flags;
147
148 /* set the initial values */
149 *flags |= rule->flags | IORESOURCE_IRQ;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700150 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
153 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700154 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
157 /* TBD: need check for >16 IRQ */
158 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
159 if (*start < PNP_IRQ_NR) {
160 *end = *start;
161 return 1;
162 }
163 for (i = 0; i < 16; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700164 if (test_bit(xtab[i], rule->map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *start = *end = xtab[i];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700166 if (pnp_check_irq(dev, idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 1;
168 }
169 }
170 return 0;
171}
172
173static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
174{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700175 resource_size_t *start, *end;
176 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int i;
178
179 /* DMA priority: this table is good for i386 */
180 static unsigned short xtab[8] = {
181 1, 3, 5, 6, 7, 0, 2, 4
182 };
183
184 if (!dev || !rule)
185 return -EINVAL;
186
187 if (idx >= PNP_MAX_DMA) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700188 pnp_err
189 ("More than 2 dmas is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* pretend we were successful so at least the manager won't try again */
191 return 1;
192 }
193
194 /* check if this resource has been manually set, if so skip */
195 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
196 return 1;
197
198 start = &dev->res.dma_resource[idx].start;
199 end = &dev->res.dma_resource[idx].end;
200 flags = &dev->res.dma_resource[idx].flags;
201
202 /* set the initial values */
203 *flags |= rule->flags | IORESOURCE_DMA;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700204 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (!rule->map) {
207 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700208 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700212 if (rule->map & (1 << xtab[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *start = *end = xtab[i];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700214 if (pnp_check_dma(dev, idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 1;
216 }
217 }
218 return 0;
219}
220
221/**
222 * pnp_init_resources - Resets a resource table to default values.
223 * @table: pointer to the desired resource table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
225void pnp_init_resource_table(struct pnp_resource_table *table)
226{
227 int idx;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
230 table->irq_resource[idx].name = NULL;
231 table->irq_resource[idx].start = -1;
232 table->irq_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700233 table->irq_resource[idx].flags =
234 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
237 table->dma_resource[idx].name = NULL;
238 table->dma_resource[idx].start = -1;
239 table->dma_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700240 table->dma_resource[idx].flags =
241 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
244 table->port_resource[idx].name = NULL;
245 table->port_resource[idx].start = 0;
246 table->port_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700247 table->port_resource[idx].flags =
248 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
251 table->mem_resource[idx].name = NULL;
252 table->mem_resource[idx].start = 0;
253 table->mem_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700254 table->mem_resource[idx].flags =
255 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257}
258
259/**
260 * pnp_clean_resources - clears resources that were not manually set
Martin Waitz67be2dd2005-05-01 08:59:26 -0700261 * @res: the resources to clean
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700263static void pnp_clean_resource_table(struct pnp_resource_table *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 int idx;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
268 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
269 continue;
270 res->irq_resource[idx].start = -1;
271 res->irq_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700272 res->irq_resource[idx].flags =
273 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
276 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
277 continue;
278 res->dma_resource[idx].start = -1;
279 res->dma_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700280 res->dma_resource[idx].flags =
281 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
284 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
285 continue;
286 res->port_resource[idx].start = 0;
287 res->port_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700288 res->port_resource[idx].flags =
289 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
292 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
293 continue;
294 res->mem_resource[idx].start = 0;
295 res->mem_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700296 res->mem_resource[idx].flags =
297 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299}
300
301/**
302 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
303 * @dev: pointer to the desired device
304 * @depnum: the dependent function number
305 *
306 * Only set depnum to 0 if the device does not have dependent options.
307 */
308static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
309{
310 struct pnp_port *port;
311 struct pnp_mem *mem;
312 struct pnp_irq *irq;
313 struct pnp_dma *dma;
314 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
315
316 if (!pnp_can_configure(dev))
317 return -ENODEV;
318
319 down(&pnp_res_mutex);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700320 pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (dev->independent) {
322 port = dev->independent->port;
323 mem = dev->independent->mem;
324 irq = dev->independent->irq;
325 dma = dev->independent->dma;
326 while (port) {
327 if (!pnp_assign_port(dev, port, nport))
328 goto fail;
329 nport++;
330 port = port->next;
331 }
332 while (mem) {
333 if (!pnp_assign_mem(dev, mem, nmem))
334 goto fail;
335 nmem++;
336 mem = mem->next;
337 }
338 while (irq) {
339 if (!pnp_assign_irq(dev, irq, nirq))
340 goto fail;
341 nirq++;
342 irq = irq->next;
343 }
344 while (dma) {
345 if (!pnp_assign_dma(dev, dma, ndma))
346 goto fail;
347 ndma++;
348 dma = dma->next;
349 }
350 }
351
352 if (depnum) {
353 struct pnp_option *dep;
354 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700355 for (i = 1, dep = dev->dependent; i < depnum;
356 i++, dep = dep->next)
357 if (!dep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto fail;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700359 port = dep->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 mem = dep->mem;
361 irq = dep->irq;
362 dma = dep->dma;
363 while (port) {
364 if (!pnp_assign_port(dev, port, nport))
365 goto fail;
366 nport++;
367 port = port->next;
368 }
369 while (mem) {
370 if (!pnp_assign_mem(dev, mem, nmem))
371 goto fail;
372 nmem++;
373 mem = mem->next;
374 }
375 while (irq) {
376 if (!pnp_assign_irq(dev, irq, nirq))
377 goto fail;
378 nirq++;
379 irq = irq->next;
380 }
381 while (dma) {
382 if (!pnp_assign_dma(dev, dma, ndma))
383 goto fail;
384 ndma++;
385 dma = dma->next;
386 }
387 } else if (dev->dependent)
388 goto fail;
389
390 up(&pnp_res_mutex);
391 return 1;
392
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700393 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 pnp_clean_resource_table(&dev->res);
395 up(&pnp_res_mutex);
396 return 0;
397}
398
399/**
400 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
401 * @dev: pointer to the desired device
402 * @res: pointer to the new resource config
Martin Waitz3d410882005-06-23 22:05:21 -0700403 * @mode: 0 or PNP_CONFIG_FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 *
405 * This function can be used by drivers that want to manually set thier resources.
406 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700407int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
408 int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700411 struct pnp_resource_table *bak;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700412
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
458int pnp_auto_config_dev(struct pnp_dev *dev)
459{
460 struct pnp_option *dep;
461 int i = 1;
462
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700463 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EINVAL;
465
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700466 if (!pnp_can_configure(dev)) {
467 pnp_dbg("Device %s does not support resource configuration.",
468 dev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -ENODEV;
470 }
471
472 if (!dev->dependent) {
473 if (pnp_assign_resources(dev, 0))
474 return 0;
475 } else {
476 dep = dev->dependent;
477 do {
478 if (pnp_assign_resources(dev, i))
479 return 0;
480 dep = dep->next;
481 i++;
482 } while (dep);
483 }
484
485 pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
486 return -EBUSY;
487}
488
489/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100490 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * @dev: pointer to the desired device
492 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700493 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100495int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!pnp_can_write(dev)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700498 pnp_dbg("Device %s does not support activation.",
499 dev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -EINVAL;
501 }
502
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700503 if (dev->protocol->set(dev, &dev->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
505 return -EIO;
506 }
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 pnp_info("Device %s activated.", dev->dev.bus_id);
Pierre Ossman68094e32005-11-29 09:09:32 +0100509 return 0;
510}
511
512/**
513 * pnp_stop_dev - low-level disable of the PnP device
514 * @dev: pointer to the desired device
515 *
516 * does not free resources
517 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100518int pnp_stop_dev(struct pnp_dev *dev)
519{
520 if (!pnp_can_disable(dev)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700521 pnp_dbg("Device %s does not support disabling.",
522 dev->dev.bus_id);
Pierre Ossman68094e32005-11-29 09:09:32 +0100523 return -EINVAL;
524 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700525 if (dev->protocol->disable(dev) < 0) {
Pierre Ossman68094e32005-11-29 09:09:32 +0100526 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
527 return -EIO;
528 }
529
530 pnp_info("Device %s disabled.", dev->dev.bus_id);
Pierre Ossman68094e32005-11-29 09:09:32 +0100531 return 0;
532}
533
534/**
535 * pnp_activate_dev - activates a PnP device for use
536 * @dev: pointer to the desired device
537 *
538 * does not validate or set resources so be careful.
539 */
540int pnp_activate_dev(struct pnp_dev *dev)
541{
542 int error;
543
544 if (!dev)
545 return -EINVAL;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700546 if (dev->active)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700547 return 0; /* the device is already active */
Pierre Ossman68094e32005-11-29 09:09:32 +0100548
549 /* ensure resources are allocated */
550 if (pnp_auto_config_dev(dev))
551 return -EBUSY;
552
553 error = pnp_start_dev(dev);
554 if (error)
555 return error;
556
557 dev->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 1;
559}
560
561/**
562 * pnp_disable_dev - disables device
563 * @dev: pointer to the desired device
564 *
565 * inform the correct pnp protocol so that resources can be used by other devices
566 */
567int pnp_disable_dev(struct pnp_dev *dev)
568{
Pierre Ossman68094e32005-11-29 09:09:32 +0100569 int error;
570
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700571 if (!dev)
572 return -EINVAL;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700573 if (!dev->active)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700574 return 0; /* the device is already disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Pierre Ossman68094e32005-11-29 09:09:32 +0100576 error = pnp_stop_dev(dev);
577 if (error)
578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* release the resources so that other devices can use them */
583 down(&pnp_res_mutex);
584 pnp_clean_resource_table(&dev->res);
585 up(&pnp_res_mutex);
586
587 return 1;
588}
589
590/**
591 * pnp_resource_change - change one resource
592 * @resource: pointer to resource to be changed
593 * @start: start of region
594 * @size: size of region
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700596void pnp_resource_change(struct resource *resource, resource_size_t start,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700597 resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 if (resource == NULL)
600 return;
601 resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
602 resource->start = start;
603 resource->end = start + size - 1;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606EXPORT_SYMBOL(pnp_manual_config_dev);
Pierre Ossman68094e32005-11-29 09:09:32 +0100607EXPORT_SYMBOL(pnp_start_dev);
608EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609EXPORT_SYMBOL(pnp_activate_dev);
610EXPORT_SYMBOL(pnp_disable_dev);
611EXPORT_SYMBOL(pnp_resource_change);
612EXPORT_SYMBOL(pnp_init_resource_table);