blob: 2251dd7da06268df5afb939a83624c2071271b6b [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>
Daniel Walkerb3bd86e2008-02-06 01:40:04 -080015#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "base.h"
17
Daniel Walkerb3bd86e2008-02-06 01:40:04 -080018DEFINE_MUTEX(pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 if (idx >= PNP_MAX_PORT) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -070026 dev_err(&dev->dev, "too many I/O port resources\n");
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 start = &dev->res.port_resource[idx].start;
32 end = &dev->res.port_resource[idx].end;
33 flags = &dev->res.port_resource[idx].flags;
34
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060035 /* check if this resource has been manually set, if so skip */
36 if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO)) {
37 dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
38 "flags %#lx\n", idx, (unsigned long long) *start,
39 (unsigned long long) *end, *flags);
40 return 1;
41 }
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 /* 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 Helgaas81b5c752008-04-28 16:34:08 -060049 dev_dbg(&dev->dev, " io %d disabled\n", idx);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070050 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52
53 *start = rule->min;
54 *end = *start + rule->size - 1;
55
56 /* run through until pnp_check_port is happy */
57 while (!pnp_check_port(dev, idx)) {
58 *start += rule->align;
59 *end = *start + rule->size - 1;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060060 if (*start > rule->max || !rule->align) {
61 dev_dbg(&dev->dev, " couldn't assign io %d\n", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060065 dev_dbg(&dev->dev, " assign io %d %#llx-%#llx\n", idx,
66 (unsigned long long) *start, (unsigned long long) *end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return 1;
68}
69
70static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
71{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -070072 resource_size_t *start, *end;
73 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (idx >= PNP_MAX_MEM) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -070076 dev_err(&dev->dev, "too many memory resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* pretend we were successful so at least the manager won't try again */
78 return 1;
79 }
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 start = &dev->res.mem_resource[idx].start;
82 end = &dev->res.mem_resource[idx].end;
83 flags = &dev->res.mem_resource[idx].flags;
84
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060085 /* check if this resource has been manually set, if so skip */
86 if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO)) {
87 dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
88 "flags %#lx\n", idx, (unsigned long long) *start,
89 (unsigned long long) *end, *flags);
90 return 1;
91 }
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* set the initial values */
94 *flags |= rule->flags | IORESOURCE_MEM;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070095 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 /* convert pnp flags to standard Linux flags */
98 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
99 *flags |= IORESOURCE_READONLY;
100 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
101 *flags |= IORESOURCE_CACHEABLE;
102 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
103 *flags |= IORESOURCE_RANGELENGTH;
104 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
105 *flags |= IORESOURCE_SHADOWABLE;
106
107 if (!rule->size) {
108 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600109 dev_dbg(&dev->dev, " mem %d disabled\n", idx);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700110 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
113 *start = rule->min;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700114 *end = *start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /* run through until pnp_check_mem is happy */
117 while (!pnp_check_mem(dev, idx)) {
118 *start += rule->align;
119 *end = *start + rule->size - 1;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600120 if (*start > rule->max || !rule->align) {
121 dev_dbg(&dev->dev, " couldn't assign mem %d\n", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600125 dev_dbg(&dev->dev, " assign mem %d %#llx-%#llx\n", idx,
126 (unsigned long long) *start, (unsigned long long) *end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return 1;
128}
129
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700130static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700132 resource_size_t *start, *end;
133 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int i;
135
136 /* IRQ priority: this table is good for i386 */
137 static unsigned short xtab[16] = {
138 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
139 };
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (idx >= PNP_MAX_IRQ) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700142 dev_err(&dev->dev, "too many IRQ resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* pretend we were successful so at least the manager won't try again */
144 return 1;
145 }
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 start = &dev->res.irq_resource[idx].start;
148 end = &dev->res.irq_resource[idx].end;
149 flags = &dev->res.irq_resource[idx].flags;
150
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600151 /* check if this resource has been manually set, if so skip */
152 if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO)) {
153 dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
154 idx, (int) *start, *flags);
155 return 1;
156 }
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* set the initial values */
159 *flags |= rule->flags | IORESOURCE_IRQ;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700160 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
163 *flags |= IORESOURCE_DISABLED;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600164 dev_dbg(&dev->dev, " irq %d disabled\n", idx);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700165 return 1; /* skip disabled resource requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167
168 /* TBD: need check for >16 IRQ */
169 *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
170 if (*start < PNP_IRQ_NR) {
171 *end = *start;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600172 dev_dbg(&dev->dev, " assign irq %d %d\n", idx, (int) *start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 1;
174 }
175 for (i = 0; i < 16; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700176 if (test_bit(xtab[i], rule->map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 *start = *end = xtab[i];
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600178 if (pnp_check_irq(dev, idx)) {
179 dev_dbg(&dev->dev, " assign irq %d %d\n", idx,
180 (int) *start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 1;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 }
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600185 dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
187}
188
Jan Beulich7ef36392007-10-16 23:31:07 -0700189static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700191 resource_size_t *start, *end;
192 unsigned long *flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int i;
194
195 /* DMA priority: this table is good for i386 */
196 static unsigned short xtab[8] = {
197 1, 3, 5, 6, 7, 0, 2, 4
198 };
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (idx >= PNP_MAX_DMA) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700201 dev_err(&dev->dev, "too many DMA resources\n");
Jan Beulich7ef36392007-10-16 23:31:07 -0700202 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 start = &dev->res.dma_resource[idx].start;
206 end = &dev->res.dma_resource[idx].end;
207 flags = &dev->res.dma_resource[idx].flags;
208
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600209 /* check if this resource has been manually set, if so skip */
210 if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO)) {
211 dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
212 idx, (int) *start, *flags);
213 return;
214 }
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* set the initial values */
217 *flags |= rule->flags | IORESOURCE_DMA;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700218 *flags &= ~IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700221 if (rule->map & (1 << xtab[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *start = *end = xtab[i];
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600223 if (pnp_check_dma(dev, idx)) {
224 dev_dbg(&dev->dev, " assign dma %d %d\n", idx,
225 (int) *start);
Jan Beulich7ef36392007-10-16 23:31:07 -0700226 return;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
Jan Beulich7ef36392007-10-16 23:31:07 -0700230#ifdef MAX_DMA_CHANNELS
231 *start = *end = MAX_DMA_CHANNELS;
232#endif
233 *flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600234 dev_dbg(&dev->dev, " disable dma %d\n", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/**
238 * pnp_init_resources - Resets a resource table to default values.
239 * @table: pointer to the desired resource table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241void pnp_init_resource_table(struct pnp_resource_table *table)
242{
243 int idx;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
246 table->irq_resource[idx].name = NULL;
247 table->irq_resource[idx].start = -1;
248 table->irq_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700249 table->irq_resource[idx].flags =
250 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
253 table->dma_resource[idx].name = NULL;
254 table->dma_resource[idx].start = -1;
255 table->dma_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700256 table->dma_resource[idx].flags =
257 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
260 table->port_resource[idx].name = NULL;
261 table->port_resource[idx].start = 0;
262 table->port_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700263 table->port_resource[idx].flags =
264 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
267 table->mem_resource[idx].name = NULL;
268 table->mem_resource[idx].start = 0;
269 table->mem_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700270 table->mem_resource[idx].flags =
271 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273}
274
Bjorn Helgaasf4490002008-04-28 16:34:09 -0600275void pnp_init_resources(struct pnp_dev *dev)
276{
277 pnp_init_resource_table(&dev->res);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/**
281 * pnp_clean_resources - clears resources that were not manually set
Martin Waitz67be2dd2005-05-01 08:59:26 -0700282 * @res: the resources to clean
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600284static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600286 struct pnp_resource_table *res = &dev->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int idx;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
290 if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
291 continue;
292 res->irq_resource[idx].start = -1;
293 res->irq_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700294 res->irq_resource[idx].flags =
295 IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
298 if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
299 continue;
300 res->dma_resource[idx].start = -1;
301 res->dma_resource[idx].end = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700302 res->dma_resource[idx].flags =
303 IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
306 if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
307 continue;
308 res->port_resource[idx].start = 0;
309 res->port_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700310 res->port_resource[idx].flags =
311 IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
314 if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
315 continue;
316 res->mem_resource[idx].start = 0;
317 res->mem_resource[idx].end = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700318 res->mem_resource[idx].flags =
319 IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321}
322
323/**
324 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
325 * @dev: pointer to the desired device
326 * @depnum: the dependent function number
327 *
328 * Only set depnum to 0 if the device does not have dependent options.
329 */
330static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
331{
332 struct pnp_port *port;
333 struct pnp_mem *mem;
334 struct pnp_irq *irq;
335 struct pnp_dma *dma;
336 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
337
338 if (!pnp_can_configure(dev))
339 return -ENODEV;
340
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600341 dbg_pnp_show_resources(dev, "before pnp_assign_resources");
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800342 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600343 pnp_clean_resource_table(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (dev->independent) {
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600345 dev_dbg(&dev->dev, "assigning independent options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 port = dev->independent->port;
347 mem = dev->independent->mem;
348 irq = dev->independent->irq;
349 dma = dev->independent->dma;
350 while (port) {
351 if (!pnp_assign_port(dev, port, nport))
352 goto fail;
353 nport++;
354 port = port->next;
355 }
356 while (mem) {
357 if (!pnp_assign_mem(dev, mem, nmem))
358 goto fail;
359 nmem++;
360 mem = mem->next;
361 }
362 while (irq) {
363 if (!pnp_assign_irq(dev, irq, nirq))
364 goto fail;
365 nirq++;
366 irq = irq->next;
367 }
368 while (dma) {
Jan Beulich7ef36392007-10-16 23:31:07 -0700369 pnp_assign_dma(dev, dma, ndma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ndma++;
371 dma = dma->next;
372 }
373 }
374
375 if (depnum) {
376 struct pnp_option *dep;
377 int i;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600378
379 dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700380 for (i = 1, dep = dev->dependent; i < depnum;
381 i++, dep = dep->next)
382 if (!dep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto fail;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700384 port = dep->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 mem = dep->mem;
386 irq = dep->irq;
387 dma = dep->dma;
388 while (port) {
389 if (!pnp_assign_port(dev, port, nport))
390 goto fail;
391 nport++;
392 port = port->next;
393 }
394 while (mem) {
395 if (!pnp_assign_mem(dev, mem, nmem))
396 goto fail;
397 nmem++;
398 mem = mem->next;
399 }
400 while (irq) {
401 if (!pnp_assign_irq(dev, irq, nirq))
402 goto fail;
403 nirq++;
404 irq = irq->next;
405 }
406 while (dma) {
Jan Beulich7ef36392007-10-16 23:31:07 -0700407 pnp_assign_dma(dev, dma, ndma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ndma++;
409 dma = dma->next;
410 }
411 } else if (dev->dependent)
412 goto fail;
413
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800414 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600415 dbg_pnp_show_resources(dev, "after pnp_assign_resources");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 1;
417
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600418fail:
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600419 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800420 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600421 dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423}
424
425/**
426 * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
427 * @dev: pointer to the desired device
428 * @res: pointer to the new resource config
Martin Waitz3d410882005-06-23 22:05:21 -0700429 * @mode: 0 or PNP_CONFIG_FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 *
431 * This function can be used by drivers that want to manually set thier resources.
432 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700433int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
434 int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700437 struct pnp_resource_table *bak;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (!pnp_can_configure(dev))
440 return -ENODEV;
441 bak = pnp_alloc(sizeof(struct pnp_resource_table));
442 if (!bak)
443 return -ENOMEM;
444 *bak = dev->res;
445
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800446 mutex_lock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 dev->res = *res;
448 if (!(mode & PNP_CONFIG_FORCE)) {
449 for (i = 0; i < PNP_MAX_PORT; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700450 if (!pnp_check_port(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto fail;
452 }
453 for (i = 0; i < PNP_MAX_MEM; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700454 if (!pnp_check_mem(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto fail;
456 }
457 for (i = 0; i < PNP_MAX_IRQ; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700458 if (!pnp_check_irq(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 goto fail;
460 }
461 for (i = 0; i < PNP_MAX_DMA; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700462 if (!pnp_check_dma(dev, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto fail;
464 }
465 }
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800466 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 kfree(bak);
469 return 0;
470
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600471fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 dev->res = *bak;
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800473 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 kfree(bak);
475 return -EINVAL;
476}
477
478/**
479 * pnp_auto_config_dev - automatically assigns resources to a device
480 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
482int pnp_auto_config_dev(struct pnp_dev *dev)
483{
484 struct pnp_option *dep;
485 int i = 1;
486
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700487 if (!pnp_can_configure(dev)) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700488 dev_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -ENODEV;
490 }
491
492 if (!dev->dependent) {
493 if (pnp_assign_resources(dev, 0))
494 return 0;
495 } else {
496 dep = dev->dependent;
497 do {
498 if (pnp_assign_resources(dev, i))
499 return 0;
500 dep = dep->next;
501 i++;
502 } while (dep);
503 }
504
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700505 dev_err(&dev->dev, "unable to assign resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return -EBUSY;
507}
508
509/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100510 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * @dev: pointer to the desired device
512 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700513 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100515int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (!pnp_can_write(dev)) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700518 dev_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -EINVAL;
520 }
521
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600522 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600523 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700524 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EIO;
526 }
527
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700528 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100529 return 0;
530}
531
532/**
533 * pnp_stop_dev - low-level disable of the PnP device
534 * @dev: pointer to the desired device
535 *
536 * does not free resources
537 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100538int pnp_stop_dev(struct pnp_dev *dev)
539{
540 if (!pnp_can_disable(dev)) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700541 dev_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100542 return -EINVAL;
543 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700544 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700545 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100546 return -EIO;
547 }
548
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700549 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100550 return 0;
551}
552
553/**
554 * pnp_activate_dev - activates a PnP device for use
555 * @dev: pointer to the desired device
556 *
557 * does not validate or set resources so be careful.
558 */
559int pnp_activate_dev(struct pnp_dev *dev)
560{
561 int error;
562
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700563 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800564 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100565
566 /* ensure resources are allocated */
567 if (pnp_auto_config_dev(dev))
568 return -EBUSY;
569
570 error = pnp_start_dev(dev);
571 if (error)
572 return error;
573
574 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/**
579 * pnp_disable_dev - disables device
580 * @dev: pointer to the desired device
581 *
582 * inform the correct pnp protocol so that resources can be used by other devices
583 */
584int pnp_disable_dev(struct pnp_dev *dev)
585{
Pierre Ossman68094e32005-11-29 09:09:32 +0100586 int error;
587
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700588 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Pierre Ossman68094e32005-11-29 09:09:32 +0100591 error = pnp_stop_dev(dev);
592 if (error)
593 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800598 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600599 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800600 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/**
606 * pnp_resource_change - change one resource
607 * @resource: pointer to resource to be changed
608 * @start: start of region
609 * @size: size of region
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Greg Kroah-Hartmanb60ba832006-06-12 17:07:07 -0700611void pnp_resource_change(struct resource *resource, resource_size_t start,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700612 resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
615 resource->start = start;
616 resource->end = start + size - 1;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619EXPORT_SYMBOL(pnp_manual_config_dev);
Pierre Ossman68094e32005-11-29 09:09:32 +0100620EXPORT_SYMBOL(pnp_start_dev);
621EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622EXPORT_SYMBOL(pnp_activate_dev);
623EXPORT_SYMBOL(pnp_disable_dev);
624EXPORT_SYMBOL(pnp_resource_change);
625EXPORT_SYMBOL(pnp_init_resource_table);