blob: 5e43c471909958ab8e4eb4b4645ecf087c22b963 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
3 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 if (idx >= PNP_MAX_PORT) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070025 pnp_err
26 ("More than 4 ports is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 /* pretend we were successful so at least the manager won't try again */
28 return 1;
29 }
30
31 /* check if this resource has been manually set, if so skip */
32 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO))
33 return 1;
34
35 start = &dev->res.port_resource[idx].start;
36 end = &dev->res.port_resource[idx].end;
37 flags = &dev->res.port_resource[idx].flags;
38
39 /* set the initial values */
40 *flags |= rule->flags | IORESOURCE_IO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070041 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 if (!rule->size) {
44 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070045 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47
48 *start = rule->min;
49 *end = *start + rule->size - 1;
50
51 /* run through until pnp_check_port is happy */
52 while (!pnp_check_port(dev, idx)) {
53 *start += rule->align;
54 *end = *start + rule->size - 1;
55 if (*start > rule->max || !rule->align)
56 return 0;
57 }
58 return 1;
59}
60
61static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
62{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -070063 resource_size_t *start, *end;
64 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (idx >= PNP_MAX_MEM) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070067 pnp_err
68 ("More than 8 mems is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /* pretend we were successful so at least the manager won't try again */
70 return 1;
71 }
72
73 /* check if this resource has been manually set, if so skip */
74 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
75 return 1;
76
77 start = &dev->res.mem_resource[idx].start;
78 end = &dev->res.mem_resource[idx].end;
79 flags = &dev->res.mem_resource[idx].flags;
80
81 /* set the initial values */
82 *flags |= rule->flags | IORESOURCE_MEM;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070083 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /* convert pnp flags to standard Linux flags */
86 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
87 *flags |= IORESOURCE_READONLY;
88 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
89 *flags |= IORESOURCE_CACHEABLE;
90 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
91 *flags |= IORESOURCE_RANGELENGTH;
92 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
93 *flags |= IORESOURCE_SHADOWABLE;
94
95 if (!rule->size) {
96 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070097 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
100 *start = rule->min;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700101 *end = *start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* run through until pnp_check_mem is happy */
104 while (!pnp_check_mem(dev, idx)) {
105 *start += rule->align;
106 *end = *start + rule->size - 1;
107 if (*start > rule->max || !rule->align)
108 return 0;
109 }
110 return 1;
111}
112
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700113static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700115 resource_size_t *start, *end;
116 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int i;
118
119 /* IRQ priority: this table is good for i386 */
120 static unsigned short xtab[16] = {
121 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
122 };
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (idx >= PNP_MAX_IRQ) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700125 pnp_err
126 ("More than 2 irqs is incompatible with pnp specifications.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /* pretend we were successful so at least the manager won't try again */
128 return 1;
129 }
130
131 /* check if this resource has been manually set, if so skip */
132 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
133 return 1;
134
135 start = &dev->res.irq_resource[idx].start;
136 end = &dev->res.irq_resource[idx].end;
137 flags = &dev->res.irq_resource[idx].flags;
138
139 /* set the initial values */
140 *flags |= rule->flags | IORESOURCE_IRQ;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700141 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
144 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700145 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
148 /* TBD: need check for >16 IRQ */
149 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
150 if (*start < PNP_IRQ_NR) {
151 *end = *start;
152 return 1;
153 }
154 for (i = 0; i < 16; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700155 if (test_bit(xtab[i], rule->map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *start = *end = xtab[i];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700157 if (pnp_check_irq(dev, idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 1;
159 }
160 }
161 return 0;
162}
163
Jan Beulich7ef36392007-10-16 23:31:07 -0700164static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700166 resource_size_t *start, *end;
167 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int i;
169
170 /* DMA priority: this table is good for i386 */
171 static unsigned short xtab[8] = {
172 1, 3, 5, 6, 7, 0, 2, 4
173 };
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (idx >= PNP_MAX_DMA) {
Jan Beulich7ef36392007-10-16 23:31:07 -0700176 pnp_err("More than 2 dmas is incompatible with pnp "
177 "specifications.");
178 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180
181 /* check if this resource has been manually set, if so skip */
182 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
Jan Beulich7ef36392007-10-16 23:31:07 -0700183 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 start = &dev->res.dma_resource[idx].start;
186 end = &dev->res.dma_resource[idx].end;
187 flags = &dev->res.dma_resource[idx].flags;
188
189 /* set the initial values */
190 *flags |= rule->flags | IORESOURCE_DMA;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700191 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700194 if (rule->map & (1 << xtab[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *start = *end = xtab[i];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700196 if (pnp_check_dma(dev, idx))
Jan Beulich7ef36392007-10-16 23:31:07 -0700197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 }
Jan Beulich7ef36392007-10-16 23:31:07 -0700200#ifdef MAX_DMA_CHANNELS
201 *start = *end = MAX_DMA_CHANNELS;
202#endif
203 *flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/**
207 * pnp_init_resources - Resets a resource table to default values.
208 * @table: pointer to the desired resource table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210void pnp_init_resource_table(struct pnp_resource_table *table)
211{
212 int idx;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
215 table->irq_resource[idx].name = NULL;
216 table->irq_resource[idx].start = -1;
217 table->irq_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700218 table->irq_resource[idx].flags =
219 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
222 table->dma_resource[idx].name = NULL;
223 table->dma_resource[idx].start = -1;
224 table->dma_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700225 table->dma_resource[idx].flags =
226 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
229 table->port_resource[idx].name = NULL;
230 table->port_resource[idx].start = 0;
231 table->port_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700232 table->port_resource[idx].flags =
233 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
236 table->mem_resource[idx].name = NULL;
237 table->mem_resource[idx].start = 0;
238 table->mem_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700239 table->mem_resource[idx].flags =
240 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242}
243
244/**
245 * pnp_clean_resources - clears resources that were not manually set
Martin Waitz67be2dd2005-05-01 08:59:26 -0700246 * @res: the resources to clean
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700248static void pnp_clean_resource_table(struct pnp_resource_table *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 int idx;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
253 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
254 continue;
255 res->irq_resource[idx].start = -1;
256 res->irq_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700257 res->irq_resource[idx].flags =
258 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
261 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
262 continue;
263 res->dma_resource[idx].start = -1;
264 res->dma_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700265 res->dma_resource[idx].flags =
266 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
269 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
270 continue;
271 res->port_resource[idx].start = 0;
272 res->port_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700273 res->port_resource[idx].flags =
274 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
277 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
278 continue;
279 res->mem_resource[idx].start = 0;
280 res->mem_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700281 res->mem_resource[idx].flags =
282 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284}
285
286/**
287 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
288 * @dev: pointer to the desired device
289 * @depnum: the dependent function number
290 *
291 * Only set depnum to 0 if the device does not have dependent options.
292 */
293static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
294{
295 struct pnp_port *port;
296 struct pnp_mem *mem;
297 struct pnp_irq *irq;
298 struct pnp_dma *dma;
299 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
300
301 if (!pnp_can_configure(dev))
302 return -ENODEV;
303
304 down(&pnp_res_mutex);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700305 pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (dev->independent) {
307 port = dev->independent->port;
308 mem = dev->independent->mem;
309 irq = dev->independent->irq;
310 dma = dev->independent->dma;
311 while (port) {
312 if (!pnp_assign_port(dev, port, nport))
313 goto fail;
314 nport++;
315 port = port->next;
316 }
317 while (mem) {
318 if (!pnp_assign_mem(dev, mem, nmem))
319 goto fail;
320 nmem++;
321 mem = mem->next;
322 }
323 while (irq) {
324 if (!pnp_assign_irq(dev, irq, nirq))
325 goto fail;
326 nirq++;
327 irq = irq->next;
328 }
329 while (dma) {
Jan Beulich7ef36392007-10-16 23:31:07 -0700330 pnp_assign_dma(dev, dma, ndma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ndma++;
332 dma = dma->next;
333 }
334 }
335
336 if (depnum) {
337 struct pnp_option *dep;
338 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700339 for (i = 1, dep = dev->dependent; i < depnum;
340 i++, dep = dep->next)
341 if (!dep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto fail;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700343 port = dep->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 mem = dep->mem;
345 irq = dep->irq;
346 dma = dep->dma;
347 while (port) {
348 if (!pnp_assign_port(dev, port, nport))
349 goto fail;
350 nport++;
351 port = port->next;
352 }
353 while (mem) {
354 if (!pnp_assign_mem(dev, mem, nmem))
355 goto fail;
356 nmem++;
357 mem = mem->next;
358 }
359 while (irq) {
360 if (!pnp_assign_irq(dev, irq, nirq))
361 goto fail;
362 nirq++;
363 irq = irq->next;
364 }
365 while (dma) {
Jan Beulich7ef36392007-10-16 23:31:07 -0700366 pnp_assign_dma(dev, dma, ndma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ndma++;
368 dma = dma->next;
369 }
370 } else if (dev->dependent)
371 goto fail;
372
373 up(&pnp_res_mutex);
374 return 1;
375
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600376fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 pnp_clean_resource_table(&dev->res);
378 up(&pnp_res_mutex);
379 return 0;
380}
381
382/**
383 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
384 * @dev: pointer to the desired device
385 * @res: pointer to the new resource config
Martin Waitz3d410882005-06-23 22:05:21 -0700386 * @mode: 0 or PNP_CONFIG_FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 *
388 * This function can be used by drivers that want to manually set thier resources.
389 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700390int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
391 int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700394 struct pnp_resource_table *bak;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!pnp_can_configure(dev))
397 return -ENODEV;
398 bak = pnp_alloc(sizeof(struct pnp_resource_table));
399 if (!bak)
400 return -ENOMEM;
401 *bak = dev->res;
402
403 down(&pnp_res_mutex);
404 dev->res = *res;
405 if (!(mode & PNP_CONFIG_FORCE)) {
406 for (i = 0; i < PNP_MAX_PORT; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700407 if (!pnp_check_port(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto fail;
409 }
410 for (i = 0; i < PNP_MAX_MEM; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700411 if (!pnp_check_mem(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto fail;
413 }
414 for (i = 0; i < PNP_MAX_IRQ; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700415 if (!pnp_check_irq(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 goto fail;
417 }
418 for (i = 0; i < PNP_MAX_DMA; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700419 if (!pnp_check_dma(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto fail;
421 }
422 }
423 up(&pnp_res_mutex);
424
425 kfree(bak);
426 return 0;
427
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600428fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dev->res = *bak;
430 up(&pnp_res_mutex);
431 kfree(bak);
432 return -EINVAL;
433}
434
435/**
436 * pnp_auto_config_dev - automatically assigns resources to a device
437 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
439int pnp_auto_config_dev(struct pnp_dev *dev)
440{
441 struct pnp_option *dep;
442 int i = 1;
443
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700444 if (!pnp_can_configure(dev)) {
445 pnp_dbg("Device %s does not support resource configuration.",
446 dev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -ENODEV;
448 }
449
450 if (!dev->dependent) {
451 if (pnp_assign_resources(dev, 0))
452 return 0;
453 } else {
454 dep = dev->dependent;
455 do {
456 if (pnp_assign_resources(dev, i))
457 return 0;
458 dep = dep->next;
459 i++;
460 } while (dep);
461 }
462
463 pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
464 return -EBUSY;
465}
466
467/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100468 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * @dev: pointer to the desired device
470 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700471 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100473int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!pnp_can_write(dev)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700476 pnp_dbg("Device %s does not support activation.",
477 dev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EINVAL;
479 }
480
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700481 if (dev->protocol->set(dev, &dev->res) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 pnp_err("Failed to activate device %s.", dev->dev.bus_id);
483 return -EIO;
484 }
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 pnp_info("Device %s activated.", dev->dev.bus_id);
Pierre Ossman68094e32005-11-29 09:09:32 +0100487 return 0;
488}
489
490/**
491 * pnp_stop_dev - low-level disable of the PnP device
492 * @dev: pointer to the desired device
493 *
494 * does not free resources
495 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100496int pnp_stop_dev(struct pnp_dev *dev)
497{
498 if (!pnp_can_disable(dev)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700499 pnp_dbg("Device %s does not support disabling.",
500 dev->dev.bus_id);
Pierre Ossman68094e32005-11-29 09:09:32 +0100501 return -EINVAL;
502 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700503 if (dev->protocol->disable(dev) < 0) {
Pierre Ossman68094e32005-11-29 09:09:32 +0100504 pnp_err("Failed to disable device %s.", dev->dev.bus_id);
505 return -EIO;
506 }
507
508 pnp_info("Device %s disabled.", dev->dev.bus_id);
Pierre Ossman68094e32005-11-29 09:09:32 +0100509 return 0;
510}
511
512/**
513 * pnp_activate_dev - activates a PnP device for use
514 * @dev: pointer to the desired device
515 *
516 * does not validate or set resources so be careful.
517 */
518int pnp_activate_dev(struct pnp_dev *dev)
519{
520 int error;
521
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700522 if (dev->active)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700523 return 0; /* the device is already active */
Pierre Ossman68094e32005-11-29 09:09:32 +0100524
525 /* ensure resources are allocated */
526 if (pnp_auto_config_dev(dev))
527 return -EBUSY;
528
529 error = pnp_start_dev(dev);
530 if (error)
531 return error;
532
533 dev->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 1;
535}
536
537/**
538 * pnp_disable_dev - disables device
539 * @dev: pointer to the desired device
540 *
541 * inform the correct pnp protocol so that resources can be used by other devices
542 */
543int pnp_disable_dev(struct pnp_dev *dev)
544{
Pierre Ossman68094e32005-11-29 09:09:32 +0100545 int error;
546
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700547 if (!dev->active)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700548 return 0; /* the device is already disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Pierre Ossman68094e32005-11-29 09:09:32 +0100550 error = pnp_stop_dev(dev);
551 if (error)
552 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /* release the resources so that other devices can use them */
557 down(&pnp_res_mutex);
558 pnp_clean_resource_table(&dev->res);
559 up(&pnp_res_mutex);
560
561 return 1;
562}
563
564/**
565 * pnp_resource_change - change one resource
566 * @resource: pointer to resource to be changed
567 * @start: start of region
568 * @size: size of region
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700570void pnp_resource_change(struct resource *resource, resource_size_t start,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700571 resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
574 resource->start = start;
575 resource->end = start + size - 1;
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578EXPORT_SYMBOL(pnp_manual_config_dev);
Pierre Ossman68094e32005-11-29 09:09:32 +0100579EXPORT_SYMBOL(pnp_start_dev);
580EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581EXPORT_SYMBOL(pnp_activate_dev);
582EXPORT_SYMBOL(pnp_disable_dev);
583EXPORT_SYMBOL(pnp_resource_change);
584EXPORT_SYMBOL(pnp_init_resource_table);