blob: 95cebf0185decdd48134892b43f5d45928ab3515 [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_CACHEABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600101 res->flags |= IORESOURCE_CACHEABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600103 res->flags |= IORESOURCE_RANGELENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600105 res->flags |= IORESOURCE_SHADOWABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600108 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600109 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600110 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600113 res->start = rule->min;
114 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600116 while (!pnp_check_mem(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600117 res->start += rule->align;
118 res->end = res->start + rule->size - 1;
119 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600120 pnp_dbg(&dev->dev, " couldn't assign mem %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -0600121 "(min %#llx max %#llx)\n", idx,
122 (unsigned long long) rule->min,
123 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600124 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600127
128__add:
129 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700133static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600135 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int i;
137
138 /* IRQ priority: this table is good for i386 */
139 static unsigned short xtab[16] = {
140 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
141 };
142
Witold Szczeponik13cde3b2012-12-15 01:02:08 +0100143 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600144 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600145 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600146 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600147 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600148 }
149
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600150 res = &local_res;
151 res->flags = rule->flags | IORESOURCE_AUTO;
152 res->start = -1;
153 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600155 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600156 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600157 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600158 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161 /* TBD: need check for >16 IRQ */
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600162 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600163 if (res->start < PNP_IRQ_NR) {
164 res->end = res->start;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600165 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 for (i = 0; i < 16; i++) {
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600168 if (test_bit(xtab[i], rule->map.bits)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600169 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600170 if (pnp_check_irq(dev, res))
171 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 }
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600174
175 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
176 res->start = -1;
177 res->end = -1;
178 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600179 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600180 goto __add;
181 }
182
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600183 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600184 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600185
186__add:
187 pnp_add_irq_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
David Rientjes586f83e2011-03-22 16:34:56 -0700191#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600192static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600194 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int i;
196
197 /* DMA priority: this table is good for i386 */
198 static unsigned short xtab[8] = {
199 1, 3, 5, 6, 7, 0, 2, 4
200 };
201
Witold Szczeponik13cde3b2012-12-15 01:02:08 +0100202 res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600203 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600204 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600205 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600206 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600207 }
208
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600209 res = &local_res;
210 res->flags = rule->flags | IORESOURCE_AUTO;
211 res->start = -1;
212 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700215 if (rule->map & (1 << xtab[i])) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600216 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600217 if (pnp_check_dma(dev, res))
218 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 }
Jan Beulich7ef36392007-10-16 23:31:07 -0700221#ifdef MAX_DMA_CHANNELS
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600222 res->start = res->end = MAX_DMA_CHANNELS;
Jan Beulich7ef36392007-10-16 23:31:07 -0700223#endif
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600224 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600225 pnp_dbg(&dev->dev, " disable dma %d\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600226
227__add:
228 pnp_add_dma_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
David Rientjes586f83e2011-03-22 16:34:56 -0700231#endif /* CONFIG_ISA_DMA_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Bjorn Helgaas2cd1393092008-04-28 16:34:11 -0600233void pnp_init_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600235 pnp_free_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600238static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600240 struct pnp_resource *pnp_res, *tmp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700241
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600242 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
243 if (pnp_res->res.flags & IORESOURCE_AUTO)
244 pnp_free_resource(pnp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246}
247
248/**
249 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
250 * @dev: pointer to the desired device
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600251 * @set: the dependent function number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600253static int pnp_assign_resources(struct pnp_dev *dev, int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600255 struct pnp_option *option;
David Rientjes586f83e2011-03-22 16:34:56 -0700256 int nport = 0, nmem = 0, nirq = 0;
257 int ndma __maybe_unused = 0;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600258 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600260 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800261 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600262 pnp_clean_resource_table(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600263
264 list_for_each_entry(option, &dev->options, list) {
265 if (pnp_option_is_dependent(option) &&
266 pnp_option_set(option) != set)
267 continue;
268
269 switch (option->type) {
270 case IORESOURCE_IO:
271 ret = pnp_assign_port(dev, &option->u.port, nport++);
272 break;
273 case IORESOURCE_MEM:
274 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
275 break;
276 case IORESOURCE_IRQ:
277 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
278 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700279#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600280 case IORESOURCE_DMA:
281 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
282 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700283#endif
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600284 default:
285 ret = -EINVAL;
286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600288 if (ret < 0)
289 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800292 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600293 if (ret < 0) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600294 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600295 pnp_clean_resource_table(dev);
296 } else
297 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
298 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * pnp_auto_config_dev - automatically assigns resources to a device
303 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305int pnp_auto_config_dev(struct pnp_dev *dev)
306{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600307 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700309 if (!pnp_can_configure(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600310 pnp_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return -ENODEV;
312 }
313
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600314 ret = pnp_assign_resources(dev, 0);
315 if (ret == 0)
316 return 0;
317
318 for (i = 1; i < dev->num_dependent_sets; i++) {
319 ret = pnp_assign_resources(dev, i);
320 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700324 dev_err(&dev->dev, "unable to assign resources\n");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600325 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100329 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * @dev: pointer to the desired device
331 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700332 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100334int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!pnp_can_write(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600337 pnp_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return -EINVAL;
339 }
340
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600341 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600342 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700343 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EIO;
345 }
346
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700347 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100348 return 0;
349}
350
351/**
352 * pnp_stop_dev - low-level disable of the PnP device
353 * @dev: pointer to the desired device
354 *
355 * does not free resources
356 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100357int pnp_stop_dev(struct pnp_dev *dev)
358{
359 if (!pnp_can_disable(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600360 pnp_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100361 return -EINVAL;
362 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700363 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700364 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100365 return -EIO;
366 }
367
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700368 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100369 return 0;
370}
371
372/**
373 * pnp_activate_dev - activates a PnP device for use
374 * @dev: pointer to the desired device
375 *
376 * does not validate or set resources so be careful.
377 */
378int pnp_activate_dev(struct pnp_dev *dev)
379{
380 int error;
381
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700382 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800383 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100384
385 /* ensure resources are allocated */
386 if (pnp_auto_config_dev(dev))
387 return -EBUSY;
388
389 error = pnp_start_dev(dev);
390 if (error)
391 return error;
392
393 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/**
398 * pnp_disable_dev - disables device
399 * @dev: pointer to the desired device
400 *
401 * inform the correct pnp protocol so that resources can be used by other devices
402 */
403int pnp_disable_dev(struct pnp_dev *dev)
404{
Pierre Ossman68094e32005-11-29 09:09:32 +0100405 int error;
406
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700407 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Pierre Ossman68094e32005-11-29 09:09:32 +0100410 error = pnp_stop_dev(dev);
411 if (error)
412 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800417 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600418 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800419 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Pierre Ossman68094e32005-11-29 09:09:32 +0100424EXPORT_SYMBOL(pnp_start_dev);
425EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426EXPORT_SYMBOL(pnp_activate_dev);
427EXPORT_SYMBOL(pnp_disable_dev);