blob: 7ad3295752ef35371953705ad6b44cb5b6e3a6e1 [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>
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -06006 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/errno.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/pnp.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080015#include <linux/bitmap.h>
Daniel Walkerb3bd86e2008-02-06 01:40:04 -080016#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "base.h"
18
Daniel Walkerb3bd86e2008-02-06 01:40:04 -080019DEFINE_MUTEX(pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Witold Szczeponik13cde3b2012-12-15 01:02:08 +010021static struct resource *pnp_find_resource(struct pnp_dev *dev,
22 unsigned char rule,
23 unsigned long type,
24 unsigned int bar)
25{
26 struct resource *res = pnp_get_resource(dev, type, bar);
27
28 /* when the resource already exists, set its resource bits from rule */
29 if (res) {
30 res->flags &= ~IORESOURCE_BITS;
31 res->flags |= rule & IORESOURCE_BITS;
32 }
33
34 return res;
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
38{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060039 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Witold Szczeponik13cde3b2012-12-15 01:02:08 +010041 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060042 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060043 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060044 "flags %#lx\n", idx, (unsigned long long) res->start,
45 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060046 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060047 }
48
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060049 res = &local_res;
50 res->flags = rule->flags | IORESOURCE_AUTO;
51 res->start = 0;
52 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060055 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -060056 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060057 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060060 res->start = rule->min;
61 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -060063 while (!pnp_check_port(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060064 res->start += rule->align;
65 res->end = res->start + rule->size - 1;
66 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060067 pnp_dbg(&dev->dev, " couldn't assign io %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -060068 "(min %#llx max %#llx)\n", idx,
69 (unsigned long long) rule->min,
70 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060071 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060074
75__add:
76 pnp_add_io_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
81{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060082 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Witold Szczeponik13cde3b2012-12-15 01:02:08 +010084 res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060085 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060086 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060087 "flags %#lx\n", idx, (unsigned long long) res->start,
88 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060089 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060090 }
91
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060092 res = &local_res;
93 res->flags = rule->flags | IORESOURCE_AUTO;
94 res->start = 0;
95 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Witold Szczeponik13cde3b2012-12-15 01:02:08 +010097 /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060099 res->flags |= IORESOURCE_READONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600101 res->flags |= IORESOURCE_RANGELENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600103 res->flags |= IORESOURCE_SHADOWABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600106 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600107 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600108 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600111 res->start = rule->min;
112 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600114 while (!pnp_check_mem(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600115 res->start += rule->align;
116 res->end = res->start + rule->size - 1;
117 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600118 pnp_dbg(&dev->dev, " couldn't assign mem %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -0600119 "(min %#llx max %#llx)\n", idx,
120 (unsigned long long) rule->min,
121 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600122 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600125
126__add:
127 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700131static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600133 struct resource *res, local_res;
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
Witold Szczeponik13cde3b2012-12-15 01:02:08 +0100141 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600142 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600143 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600144 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600145 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600146 }
147
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600148 res = &local_res;
149 res->flags = rule->flags | IORESOURCE_AUTO;
150 res->start = -1;
151 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600153 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600154 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600155 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600156 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
159 /* TBD: need check for >16 IRQ */
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600160 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600161 if (res->start < PNP_IRQ_NR) {
162 res->end = res->start;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600163 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 for (i = 0; i < 16; i++) {
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600166 if (test_bit(xtab[i], rule->map.bits)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600167 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600168 if (pnp_check_irq(dev, res))
169 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600172
173 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
174 res->start = -1;
175 res->end = -1;
176 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600177 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600178 goto __add;
179 }
180
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600181 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600182 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600183
184__add:
185 pnp_add_irq_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
David Rientjes586f83e2011-03-22 16:34:56 -0700189#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600190static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600192 struct resource *res, local_res;
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
Witold Szczeponik13cde3b2012-12-15 01:02:08 +0100200 res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600201 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600202 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600203 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600204 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600205 }
206
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600207 res = &local_res;
208 res->flags = rule->flags | IORESOURCE_AUTO;
209 res->start = -1;
210 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
David Flaterbdf0eb32013-05-10 14:37:43 +0200212 if (!rule->map) {
213 res->flags |= IORESOURCE_DISABLED;
214 pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
215 goto __add;
216 }
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700219 if (rule->map & (1 << xtab[i])) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600220 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600221 if (pnp_check_dma(dev, res))
222 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
David Flaterbdf0eb32013-05-10 14:37:43 +0200225
226 pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
227 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600228
229__add:
230 pnp_add_dma_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
David Rientjes586f83e2011-03-22 16:34:56 -0700233#endif /* CONFIG_ISA_DMA_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Bjorn Helgaas2cd1393092008-04-28 16:34:11 -0600235void pnp_init_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600237 pnp_free_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600240static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600242 struct pnp_resource *pnp_res, *tmp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700243
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600244 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
245 if (pnp_res->res.flags & IORESOURCE_AUTO)
246 pnp_free_resource(pnp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248}
249
250/**
251 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
252 * @dev: pointer to the desired device
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600253 * @set: the dependent function number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600255static int pnp_assign_resources(struct pnp_dev *dev, int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600257 struct pnp_option *option;
David Rientjes586f83e2011-03-22 16:34:56 -0700258 int nport = 0, nmem = 0, nirq = 0;
259 int ndma __maybe_unused = 0;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600260 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600262 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800263 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600264 pnp_clean_resource_table(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600265
266 list_for_each_entry(option, &dev->options, list) {
267 if (pnp_option_is_dependent(option) &&
268 pnp_option_set(option) != set)
269 continue;
270
271 switch (option->type) {
272 case IORESOURCE_IO:
273 ret = pnp_assign_port(dev, &option->u.port, nport++);
274 break;
275 case IORESOURCE_MEM:
276 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
277 break;
278 case IORESOURCE_IRQ:
279 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
280 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700281#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600282 case IORESOURCE_DMA:
283 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
284 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700285#endif
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600286 default:
287 ret = -EINVAL;
288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600290 if (ret < 0)
291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800294 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600295 if (ret < 0) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600296 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600297 pnp_clean_resource_table(dev);
298 } else
299 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
300 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * pnp_auto_config_dev - automatically assigns resources to a device
305 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
307int pnp_auto_config_dev(struct pnp_dev *dev)
308{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600309 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700311 if (!pnp_can_configure(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600312 pnp_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -ENODEV;
314 }
315
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600316 ret = pnp_assign_resources(dev, 0);
317 if (ret == 0)
318 return 0;
319
320 for (i = 1; i < dev->num_dependent_sets; i++) {
321 ret = pnp_assign_resources(dev, i);
322 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700326 dev_err(&dev->dev, "unable to assign resources\n");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100331 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * @dev: pointer to the desired device
333 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700334 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100336int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!pnp_can_write(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600339 pnp_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -EINVAL;
341 }
342
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600343 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600344 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700345 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -EIO;
347 }
348
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700349 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100350 return 0;
351}
352
353/**
354 * pnp_stop_dev - low-level disable of the PnP device
355 * @dev: pointer to the desired device
356 *
357 * does not free resources
358 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100359int pnp_stop_dev(struct pnp_dev *dev)
360{
361 if (!pnp_can_disable(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600362 pnp_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100363 return -EINVAL;
364 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700365 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700366 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100367 return -EIO;
368 }
369
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700370 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100371 return 0;
372}
373
374/**
375 * pnp_activate_dev - activates a PnP device for use
376 * @dev: pointer to the desired device
377 *
378 * does not validate or set resources so be careful.
379 */
380int pnp_activate_dev(struct pnp_dev *dev)
381{
382 int error;
383
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700384 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800385 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100386
387 /* ensure resources are allocated */
388 if (pnp_auto_config_dev(dev))
389 return -EBUSY;
390
391 error = pnp_start_dev(dev);
392 if (error)
393 return error;
394
395 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399/**
400 * pnp_disable_dev - disables device
401 * @dev: pointer to the desired device
402 *
403 * inform the correct pnp protocol so that resources can be used by other devices
404 */
405int pnp_disable_dev(struct pnp_dev *dev)
406{
Pierre Ossman68094e32005-11-29 09:09:32 +0100407 int error;
408
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700409 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Pierre Ossman68094e32005-11-29 09:09:32 +0100412 error = pnp_stop_dev(dev);
413 if (error)
414 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800419 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600420 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800421 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Pierre Ossman68094e32005-11-29 09:09:32 +0100426EXPORT_SYMBOL(pnp_start_dev);
427EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428EXPORT_SYMBOL(pnp_activate_dev);
429EXPORT_SYMBOL(pnp_disable_dev);