blob: 1adc83fdabb69497780f09d2dfeaa415a5d76e90 [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{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060022 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060024 res = pnp_get_resource(dev, IORESOURCE_IO, idx);
25 if (res) {
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060026 dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060027 "flags %#lx\n", idx, (unsigned long long) res->start,
28 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060029 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060030 }
31
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060032 res = &local_res;
33 res->flags = rule->flags | IORESOURCE_AUTO;
34 res->start = 0;
35 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060038 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060039 dev_dbg(&dev->dev, " io %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060040 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
42
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060043 res->start = rule->min;
44 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -060046 while (!pnp_check_port(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060047 res->start += rule->align;
48 res->end = res->start + rule->size - 1;
49 if (res->start > rule->max || !rule->align) {
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -060050 dev_dbg(&dev->dev, " couldn't assign io %d "
51 "(min %#llx max %#llx)\n", idx,
52 (unsigned long long) rule->min,
53 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060054 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060057
58__add:
59 pnp_add_io_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
64{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060065 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060067 res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
68 if (res) {
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060069 dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060070 "flags %#lx\n", idx, (unsigned long long) res->start,
71 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060072 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060073 }
74
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060075 res = &local_res;
76 res->flags = rule->flags | IORESOURCE_AUTO;
77 res->start = 0;
78 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060081 res->flags |= IORESOURCE_READONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060083 res->flags |= IORESOURCE_CACHEABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060085 res->flags |= IORESOURCE_RANGELENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060087 res->flags |= IORESOURCE_SHADOWABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060090 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060091 dev_dbg(&dev->dev, " mem %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060092 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060095 res->start = rule->min;
96 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -060098 while (!pnp_check_mem(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060099 res->start += rule->align;
100 res->end = res->start + rule->size - 1;
101 if (res->start > rule->max || !rule->align) {
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -0600102 dev_dbg(&dev->dev, " couldn't assign mem %d "
103 "(min %#llx max %#llx)\n", idx,
104 (unsigned long long) rule->min,
105 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600106 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600109
110__add:
111 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700115static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600117 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 int i;
119
120 /* IRQ priority: this table is good for i386 */
121 static unsigned short xtab[16] = {
122 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
123 };
124
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600125 res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
126 if (res) {
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600127 dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600128 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600129 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600130 }
131
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600132 res = &local_res;
133 res->flags = rule->flags | IORESOURCE_AUTO;
134 res->start = -1;
135 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600137 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600138 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600139 dev_dbg(&dev->dev, " irq %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600140 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
143 /* TBD: need check for >16 IRQ */
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600144 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600145 if (res->start < PNP_IRQ_NR) {
146 res->end = res->start;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600147 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149 for (i = 0; i < 16; i++) {
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600150 if (test_bit(xtab[i], rule->map.bits)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600151 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600152 if (pnp_check_irq(dev, res))
153 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600156 dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600157 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600158
159__add:
160 pnp_add_irq_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600164static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600166 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int i;
168
169 /* DMA priority: this table is good for i386 */
170 static unsigned short xtab[8] = {
171 1, 3, 5, 6, 7, 0, 2, 4
172 };
173
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600174 res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
175 if (res) {
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600176 dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600177 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600178 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600179 }
180
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600181 res = &local_res;
182 res->flags = rule->flags | IORESOURCE_AUTO;
183 res->start = -1;
184 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700187 if (rule->map & (1 << xtab[i])) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600188 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600189 if (pnp_check_dma(dev, res))
190 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192 }
Jan Beulich7ef36392007-10-16 23:31:07 -0700193#ifdef MAX_DMA_CHANNELS
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600194 res->start = res->end = MAX_DMA_CHANNELS;
Jan Beulich7ef36392007-10-16 23:31:07 -0700195#endif
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600196 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600197 dev_dbg(&dev->dev, " disable dma %d\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600198
199__add:
200 pnp_add_dma_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Bjorn Helgaas2cd1393092008-04-28 16:34:11 -0600204void pnp_init_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600206 pnp_free_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600209static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600211 struct pnp_resource *pnp_res, *tmp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700212
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600213 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
214 if (pnp_res->res.flags & IORESOURCE_AUTO)
215 pnp_free_resource(pnp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217}
218
219/**
220 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
221 * @dev: pointer to the desired device
222 * @depnum: the dependent function number
223 *
224 * Only set depnum to 0 if the device does not have dependent options.
225 */
226static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
227{
228 struct pnp_port *port;
229 struct pnp_mem *mem;
230 struct pnp_irq *irq;
231 struct pnp_dma *dma;
232 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
233
234 if (!pnp_can_configure(dev))
235 return -ENODEV;
236
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600237 dbg_pnp_show_resources(dev, "before pnp_assign_resources");
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800238 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600239 pnp_clean_resource_table(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (dev->independent) {
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600241 dev_dbg(&dev->dev, "assigning independent options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 port = dev->independent->port;
243 mem = dev->independent->mem;
244 irq = dev->independent->irq;
245 dma = dev->independent->dma;
246 while (port) {
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600247 if (pnp_assign_port(dev, port, nport) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto fail;
249 nport++;
250 port = port->next;
251 }
252 while (mem) {
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600253 if (pnp_assign_mem(dev, mem, nmem) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto fail;
255 nmem++;
256 mem = mem->next;
257 }
258 while (irq) {
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600259 if (pnp_assign_irq(dev, irq, nirq) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto fail;
261 nirq++;
262 irq = irq->next;
263 }
264 while (dma) {
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600265 if (pnp_assign_dma(dev, dma, ndma) < 0)
266 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 ndma++;
268 dma = dma->next;
269 }
270 }
271
272 if (depnum) {
273 struct pnp_option *dep;
274 int i;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600275
276 dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700277 for (i = 1, dep = dev->dependent; i < depnum;
278 i++, dep = dep->next)
279 if (!dep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto fail;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700281 port = dep->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 mem = dep->mem;
283 irq = dep->irq;
284 dma = dep->dma;
285 while (port) {
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600286 if (pnp_assign_port(dev, port, nport) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 goto fail;
288 nport++;
289 port = port->next;
290 }
291 while (mem) {
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600292 if (pnp_assign_mem(dev, mem, nmem) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goto fail;
294 nmem++;
295 mem = mem->next;
296 }
297 while (irq) {
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600298 if (pnp_assign_irq(dev, irq, nirq) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto fail;
300 nirq++;
301 irq = irq->next;
302 }
303 while (dma) {
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600304 if (pnp_assign_dma(dev, dma, ndma) < 0)
305 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ndma++;
307 dma = dma->next;
308 }
309 } else if (dev->dependent)
310 goto fail;
311
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800312 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600313 dbg_pnp_show_resources(dev, "after pnp_assign_resources");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 1;
315
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600316fail:
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600317 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800318 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600319 dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
321}
322
323/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * pnp_auto_config_dev - automatically assigns resources to a device
325 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
327int pnp_auto_config_dev(struct pnp_dev *dev)
328{
329 struct pnp_option *dep;
330 int i = 1;
331
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700332 if (!pnp_can_configure(dev)) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700333 dev_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -ENODEV;
335 }
336
337 if (!dev->dependent) {
338 if (pnp_assign_resources(dev, 0))
339 return 0;
340 } else {
341 dep = dev->dependent;
342 do {
343 if (pnp_assign_resources(dev, i))
344 return 0;
345 dep = dep->next;
346 i++;
347 } while (dep);
348 }
349
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700350 dev_err(&dev->dev, "unable to assign resources\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EBUSY;
352}
353
354/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100355 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * @dev: pointer to the desired device
357 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700358 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100360int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!pnp_can_write(dev)) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700363 dev_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -EINVAL;
365 }
366
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600367 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600368 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700369 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -EIO;
371 }
372
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700373 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100374 return 0;
375}
376
377/**
378 * pnp_stop_dev - low-level disable of the PnP device
379 * @dev: pointer to the desired device
380 *
381 * does not free resources
382 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100383int pnp_stop_dev(struct pnp_dev *dev)
384{
385 if (!pnp_can_disable(dev)) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700386 dev_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100387 return -EINVAL;
388 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700389 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700390 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100391 return -EIO;
392 }
393
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700394 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100395 return 0;
396}
397
398/**
399 * pnp_activate_dev - activates a PnP device for use
400 * @dev: pointer to the desired device
401 *
402 * does not validate or set resources so be careful.
403 */
404int pnp_activate_dev(struct pnp_dev *dev)
405{
406 int error;
407
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700408 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800409 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100410
411 /* ensure resources are allocated */
412 if (pnp_auto_config_dev(dev))
413 return -EBUSY;
414
415 error = pnp_start_dev(dev);
416 if (error)
417 return error;
418
419 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/**
424 * pnp_disable_dev - disables device
425 * @dev: pointer to the desired device
426 *
427 * inform the correct pnp protocol so that resources can be used by other devices
428 */
429int pnp_disable_dev(struct pnp_dev *dev)
430{
Pierre Ossman68094e32005-11-29 09:09:32 +0100431 int error;
432
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700433 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Pierre Ossman68094e32005-11-29 09:09:32 +0100436 error = pnp_stop_dev(dev);
437 if (error)
438 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800443 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600444 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800445 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Pierre Ossman68094e32005-11-29 09:09:32 +0100450EXPORT_SYMBOL(pnp_start_dev);
451EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452EXPORT_SYMBOL(pnp_activate_dev);
453EXPORT_SYMBOL(pnp_disable_dev);