blob: 144055593ec81618a5f740f7ec7b3b3fb4b69d60 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
4 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02005 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -06007 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pnp.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080016#include <linux/bitmap.h>
Daniel Walkerb3bd86e2008-02-06 01:40:04 -080017#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "base.h"
19
Daniel Walkerb3bd86e2008-02-06 01:40:04 -080020DEFINE_MUTEX(pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Witold Szczeponik13cde3b2012-12-15 01:02:08 +010022static struct resource *pnp_find_resource(struct pnp_dev *dev,
23 unsigned char rule,
24 unsigned long type,
25 unsigned int bar)
26{
27 struct resource *res = pnp_get_resource(dev, type, bar);
28
29 /* when the resource already exists, set its resource bits from rule */
30 if (res) {
31 res->flags &= ~IORESOURCE_BITS;
32 res->flags |= rule & IORESOURCE_BITS;
33 }
34
35 return res;
36}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
39{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060040 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Witold Szczeponik13cde3b2012-12-15 01:02:08 +010042 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060043 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060044 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060045 "flags %#lx\n", idx, (unsigned long long) res->start,
46 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060047 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060048 }
49
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060050 res = &local_res;
51 res->flags = rule->flags | IORESOURCE_AUTO;
52 res->start = 0;
53 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060056 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -060057 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060058 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060061 res->start = rule->min;
62 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -060064 while (!pnp_check_port(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060065 res->start += rule->align;
66 res->end = res->start + rule->size - 1;
67 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060068 pnp_dbg(&dev->dev, " couldn't assign io %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -060069 "(min %#llx max %#llx)\n", idx,
70 (unsigned long long) rule->min,
71 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060072 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060075
76__add:
77 pnp_add_io_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
82{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060083 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Witold Szczeponik13cde3b2012-12-15 01:02:08 +010085 res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060086 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060087 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060088 "flags %#lx\n", idx, (unsigned long long) res->start,
89 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060090 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060091 }
92
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060093 res = &local_res;
94 res->flags = rule->flags | IORESOURCE_AUTO;
95 res->start = 0;
96 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Witold Szczeponik13cde3b2012-12-15 01:02:08 +010098 /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600100 res->flags |= IORESOURCE_READONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600102 res->flags |= IORESOURCE_RANGELENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600104 res->flags |= IORESOURCE_SHADOWABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600107 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600108 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600109 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600112 res->start = rule->min;
113 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600115 while (!pnp_check_mem(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600116 res->start += rule->align;
117 res->end = res->start + rule->size - 1;
118 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600119 pnp_dbg(&dev->dev, " couldn't assign mem %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -0600120 "(min %#llx max %#llx)\n", idx,
121 (unsigned long long) rule->min,
122 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600123 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600126
127__add:
128 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700132static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600134 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int i;
136
137 /* IRQ priority: this table is good for i386 */
138 static unsigned short xtab[16] = {
139 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
140 };
141
Witold Szczeponik13cde3b2012-12-15 01:02:08 +0100142 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600143 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600144 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600145 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600146 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600147 }
148
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600149 res = &local_res;
150 res->flags = rule->flags | IORESOURCE_AUTO;
151 res->start = -1;
152 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600154 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600155 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600156 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600157 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159
160 /* TBD: need check for >16 IRQ */
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600161 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600162 if (res->start < PNP_IRQ_NR) {
163 res->end = res->start;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600164 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 for (i = 0; i < 16; i++) {
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600167 if (test_bit(xtab[i], rule->map.bits)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600168 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600169 if (pnp_check_irq(dev, res))
170 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 }
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600173
174 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
175 res->start = -1;
176 res->end = -1;
177 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600178 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600179 goto __add;
180 }
181
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600182 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600183 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600184
185__add:
186 pnp_add_irq_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
David Rientjes586f83e2011-03-22 16:34:56 -0700190#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600191static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600193 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int i;
195
196 /* DMA priority: this table is good for i386 */
197 static unsigned short xtab[8] = {
198 1, 3, 5, 6, 7, 0, 2, 4
199 };
200
Witold Szczeponik13cde3b2012-12-15 01:02:08 +0100201 res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600202 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600203 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600204 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600205 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600206 }
207
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600208 res = &local_res;
209 res->flags = rule->flags | IORESOURCE_AUTO;
210 res->start = -1;
211 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
David Flaterbdf0eb32013-05-10 14:37:43 +0200213 if (!rule->map) {
214 res->flags |= IORESOURCE_DISABLED;
215 pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
216 goto __add;
217 }
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700220 if (rule->map & (1 << xtab[i])) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600221 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600222 if (pnp_check_dma(dev, res))
223 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225 }
David Flaterbdf0eb32013-05-10 14:37:43 +0200226
227 pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
228 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600229
230__add:
231 pnp_add_dma_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
David Rientjes586f83e2011-03-22 16:34:56 -0700234#endif /* CONFIG_ISA_DMA_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Bjorn Helgaas2cd1393092008-04-28 16:34:11 -0600236void pnp_init_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600238 pnp_free_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600241static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600243 struct pnp_resource *pnp_res, *tmp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700244
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600245 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
246 if (pnp_res->res.flags & IORESOURCE_AUTO)
247 pnp_free_resource(pnp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249}
250
251/**
252 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
253 * @dev: pointer to the desired device
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600254 * @set: the dependent function number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600256static int pnp_assign_resources(struct pnp_dev *dev, int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600258 struct pnp_option *option;
David Rientjes586f83e2011-03-22 16:34:56 -0700259 int nport = 0, nmem = 0, nirq = 0;
260 int ndma __maybe_unused = 0;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600261 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600263 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800264 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600265 pnp_clean_resource_table(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600266
267 list_for_each_entry(option, &dev->options, list) {
268 if (pnp_option_is_dependent(option) &&
269 pnp_option_set(option) != set)
270 continue;
271
272 switch (option->type) {
273 case IORESOURCE_IO:
274 ret = pnp_assign_port(dev, &option->u.port, nport++);
275 break;
276 case IORESOURCE_MEM:
277 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
278 break;
279 case IORESOURCE_IRQ:
280 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
281 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700282#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600283 case IORESOURCE_DMA:
284 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
285 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700286#endif
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600287 default:
288 ret = -EINVAL;
289 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600291 if (ret < 0)
292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800295 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600296 if (ret < 0) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600297 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600298 pnp_clean_resource_table(dev);
299 } else
300 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * pnp_auto_config_dev - automatically assigns resources to a device
306 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
308int pnp_auto_config_dev(struct pnp_dev *dev)
309{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600310 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700312 if (!pnp_can_configure(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600313 pnp_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -ENODEV;
315 }
316
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600317 ret = pnp_assign_resources(dev, 0);
318 if (ret == 0)
319 return 0;
320
321 for (i = 1; i < dev->num_dependent_sets; i++) {
322 ret = pnp_assign_resources(dev, i);
323 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700327 dev_err(&dev->dev, "unable to assign resources\n");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600328 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100332 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * @dev: pointer to the desired device
334 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700335 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100337int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!pnp_can_write(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600340 pnp_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EINVAL;
342 }
343
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600344 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600345 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700346 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -EIO;
348 }
349
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700350 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100351 return 0;
352}
353
354/**
355 * pnp_stop_dev - low-level disable of the PnP device
356 * @dev: pointer to the desired device
357 *
358 * does not free resources
359 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100360int pnp_stop_dev(struct pnp_dev *dev)
361{
362 if (!pnp_can_disable(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600363 pnp_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100364 return -EINVAL;
365 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700366 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700367 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100368 return -EIO;
369 }
370
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700371 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100372 return 0;
373}
374
375/**
376 * pnp_activate_dev - activates a PnP device for use
377 * @dev: pointer to the desired device
378 *
379 * does not validate or set resources so be careful.
380 */
381int pnp_activate_dev(struct pnp_dev *dev)
382{
383 int error;
384
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700385 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800386 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100387
388 /* ensure resources are allocated */
389 if (pnp_auto_config_dev(dev))
390 return -EBUSY;
391
392 error = pnp_start_dev(dev);
393 if (error)
394 return error;
395
396 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/**
401 * pnp_disable_dev - disables device
402 * @dev: pointer to the desired device
403 *
404 * inform the correct pnp protocol so that resources can be used by other devices
405 */
406int pnp_disable_dev(struct pnp_dev *dev)
407{
Pierre Ossman68094e32005-11-29 09:09:32 +0100408 int error;
409
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700410 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Pierre Ossman68094e32005-11-29 09:09:32 +0100413 error = pnp_stop_dev(dev);
414 if (error)
415 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800420 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600421 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800422 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Pierre Ossman68094e32005-11-29 09:09:32 +0100427EXPORT_SYMBOL(pnp_start_dev);
428EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429EXPORT_SYMBOL(pnp_activate_dev);
430EXPORT_SYMBOL(pnp_disable_dev);