blob: 0a15664eef1c65f2c9670f24ab03af0a604122c6 [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
21static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
22{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060023 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060025 res = pnp_get_resource(dev, IORESOURCE_IO, idx);
26 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060027 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060028 "flags %#lx\n", idx, (unsigned long long) res->start,
29 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060030 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060031 }
32
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060033 res = &local_res;
34 res->flags = rule->flags | IORESOURCE_AUTO;
35 res->start = 0;
36 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060039 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -060040 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060041 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060044 res->start = rule->min;
45 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -060047 while (!pnp_check_port(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060048 res->start += rule->align;
49 res->end = res->start + rule->size - 1;
50 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060051 pnp_dbg(&dev->dev, " couldn't assign io %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -060052 "(min %#llx max %#llx)\n", idx,
53 (unsigned long long) rule->min,
54 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060055 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060058
59__add:
60 pnp_add_io_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
65{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060066 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060068 res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
69 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060070 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060071 "flags %#lx\n", idx, (unsigned long long) res->start,
72 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060073 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060074 }
75
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060076 res = &local_res;
77 res->flags = rule->flags | IORESOURCE_AUTO;
78 res->start = 0;
79 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060082 res->flags |= IORESOURCE_READONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060084 res->flags |= IORESOURCE_CACHEABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060086 res->flags |= IORESOURCE_RANGELENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060088 res->flags |= IORESOURCE_SHADOWABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060091 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -060092 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060093 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060096 res->start = rule->min;
97 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -060099 while (!pnp_check_mem(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600100 res->start += rule->align;
101 res->end = res->start + rule->size - 1;
102 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600103 pnp_dbg(&dev->dev, " couldn't assign mem %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -0600104 "(min %#llx max %#llx)\n", idx,
105 (unsigned long long) rule->min,
106 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600107 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600110
111__add:
112 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700116static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600118 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int i;
120
121 /* IRQ priority: this table is good for i386 */
122 static unsigned short xtab[16] = {
123 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
124 };
125
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600126 res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
127 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600128 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600129 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600130 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600131 }
132
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600133 res = &local_res;
134 res->flags = rule->flags | IORESOURCE_AUTO;
135 res->start = -1;
136 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600138 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600139 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600140 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600141 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
144 /* TBD: need check for >16 IRQ */
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600145 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600146 if (res->start < PNP_IRQ_NR) {
147 res->end = res->start;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600148 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 for (i = 0; i < 16; i++) {
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600151 if (test_bit(xtab[i], rule->map.bits)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600152 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600153 if (pnp_check_irq(dev, res))
154 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 }
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600157
158 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
159 res->start = -1;
160 res->end = -1;
161 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600162 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600163 goto __add;
164 }
165
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600166 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600167 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600168
169__add:
170 pnp_add_irq_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600174static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600176 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int i;
178
179 /* DMA priority: this table is good for i386 */
180 static unsigned short xtab[8] = {
181 1, 3, 5, 6, 7, 0, 2, 4
182 };
183
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600184 res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
185 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600186 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600187 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600188 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600189 }
190
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600191 res = &local_res;
192 res->flags = rule->flags | IORESOURCE_AUTO;
193 res->start = -1;
194 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700197 if (rule->map & (1 << xtab[i])) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600198 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600199 if (pnp_check_dma(dev, res))
200 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 }
Jan Beulich7ef36392007-10-16 23:31:07 -0700203#ifdef MAX_DMA_CHANNELS
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600204 res->start = res->end = MAX_DMA_CHANNELS;
Jan Beulich7ef36392007-10-16 23:31:07 -0700205#endif
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600206 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600207 pnp_dbg(&dev->dev, " disable dma %d\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600208
209__add:
210 pnp_add_dma_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Bjorn Helgaas2cd1393092008-04-28 16:34:11 -0600214void pnp_init_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600216 pnp_free_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600219static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600221 struct pnp_resource *pnp_res, *tmp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700222
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600223 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
224 if (pnp_res->res.flags & IORESOURCE_AUTO)
225 pnp_free_resource(pnp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227}
228
229/**
230 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
231 * @dev: pointer to the desired device
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600232 * @set: the dependent function number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600234static int pnp_assign_resources(struct pnp_dev *dev, int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600236 struct pnp_option *option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600238 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600240 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800241 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600242 pnp_clean_resource_table(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600243
244 list_for_each_entry(option, &dev->options, list) {
245 if (pnp_option_is_dependent(option) &&
246 pnp_option_set(option) != set)
247 continue;
248
249 switch (option->type) {
250 case IORESOURCE_IO:
251 ret = pnp_assign_port(dev, &option->u.port, nport++);
252 break;
253 case IORESOURCE_MEM:
254 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
255 break;
256 case IORESOURCE_IRQ:
257 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
258 break;
259 case IORESOURCE_DMA:
260 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
261 break;
262 default:
263 ret = -EINVAL;
264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600266 if (ret < 0)
267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800270 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600271 if (ret < 0) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600272 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600273 pnp_clean_resource_table(dev);
274 } else
275 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
276 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * pnp_auto_config_dev - automatically assigns resources to a device
281 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
283int pnp_auto_config_dev(struct pnp_dev *dev)
284{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600285 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700287 if (!pnp_can_configure(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600288 pnp_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -ENODEV;
290 }
291
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600292 ret = pnp_assign_resources(dev, 0);
293 if (ret == 0)
294 return 0;
295
296 for (i = 1; i < dev->num_dependent_sets; i++) {
297 ret = pnp_assign_resources(dev, i);
298 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700302 dev_err(&dev->dev, "unable to assign resources\n");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100307 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * @dev: pointer to the desired device
309 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700310 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100312int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!pnp_can_write(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600315 pnp_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EINVAL;
317 }
318
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600319 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600320 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700321 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -EIO;
323 }
324
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700325 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100326 return 0;
327}
328
329/**
330 * pnp_stop_dev - low-level disable of the PnP device
331 * @dev: pointer to the desired device
332 *
333 * does not free resources
334 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100335int pnp_stop_dev(struct pnp_dev *dev)
336{
337 if (!pnp_can_disable(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600338 pnp_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100339 return -EINVAL;
340 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700341 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700342 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100343 return -EIO;
344 }
345
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700346 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100347 return 0;
348}
349
350/**
351 * pnp_activate_dev - activates a PnP device for use
352 * @dev: pointer to the desired device
353 *
354 * does not validate or set resources so be careful.
355 */
356int pnp_activate_dev(struct pnp_dev *dev)
357{
358 int error;
359
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700360 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800361 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100362
363 /* ensure resources are allocated */
364 if (pnp_auto_config_dev(dev))
365 return -EBUSY;
366
367 error = pnp_start_dev(dev);
368 if (error)
369 return error;
370
371 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/**
376 * pnp_disable_dev - disables device
377 * @dev: pointer to the desired device
378 *
379 * inform the correct pnp protocol so that resources can be used by other devices
380 */
381int pnp_disable_dev(struct pnp_dev *dev)
382{
Pierre Ossman68094e32005-11-29 09:09:32 +0100383 int error;
384
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700385 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Pierre Ossman68094e32005-11-29 09:09:32 +0100388 error = pnp_stop_dev(dev);
389 if (error)
390 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800395 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600396 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800397 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Pierre Ossman68094e32005-11-29 09:09:32 +0100402EXPORT_SYMBOL(pnp_start_dev);
403EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404EXPORT_SYMBOL(pnp_activate_dev);
405EXPORT_SYMBOL(pnp_disable_dev);