blob: ed9ce507149ab87efe8692b24379a82ab728184c [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
David Rientjes586f83e2011-03-22 16:34:56 -0700174#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600175static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600177 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int i;
179
180 /* DMA priority: this table is good for i386 */
181 static unsigned short xtab[8] = {
182 1, 3, 5, 6, 7, 0, 2, 4
183 };
184
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600185 res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
186 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600187 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600188 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600189 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600190 }
191
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600192 res = &local_res;
193 res->flags = rule->flags | IORESOURCE_AUTO;
194 res->start = -1;
195 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700198 if (rule->map & (1 << xtab[i])) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600199 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600200 if (pnp_check_dma(dev, res))
201 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 }
Jan Beulich7ef36392007-10-16 23:31:07 -0700204#ifdef MAX_DMA_CHANNELS
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600205 res->start = res->end = MAX_DMA_CHANNELS;
Jan Beulich7ef36392007-10-16 23:31:07 -0700206#endif
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600207 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600208 pnp_dbg(&dev->dev, " disable dma %d\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600209
210__add:
211 pnp_add_dma_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
David Rientjes586f83e2011-03-22 16:34:56 -0700214#endif /* CONFIG_ISA_DMA_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Bjorn Helgaas2cd1393092008-04-28 16:34:11 -0600216void pnp_init_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600218 pnp_free_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600221static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600223 struct pnp_resource *pnp_res, *tmp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700224
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600225 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
226 if (pnp_res->res.flags & IORESOURCE_AUTO)
227 pnp_free_resource(pnp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229}
230
231/**
232 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
233 * @dev: pointer to the desired device
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600234 * @set: the dependent function number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600236static int pnp_assign_resources(struct pnp_dev *dev, int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600238 struct pnp_option *option;
David Rientjes586f83e2011-03-22 16:34:56 -0700239 int nport = 0, nmem = 0, nirq = 0;
240 int ndma __maybe_unused = 0;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600241 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600243 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800244 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600245 pnp_clean_resource_table(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600246
247 list_for_each_entry(option, &dev->options, list) {
248 if (pnp_option_is_dependent(option) &&
249 pnp_option_set(option) != set)
250 continue;
251
252 switch (option->type) {
253 case IORESOURCE_IO:
254 ret = pnp_assign_port(dev, &option->u.port, nport++);
255 break;
256 case IORESOURCE_MEM:
257 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
258 break;
259 case IORESOURCE_IRQ:
260 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
261 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700262#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600263 case IORESOURCE_DMA:
264 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
265 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700266#endif
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600267 default:
268 ret = -EINVAL;
269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600271 if (ret < 0)
272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800275 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600276 if (ret < 0) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600277 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600278 pnp_clean_resource_table(dev);
279 } else
280 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * pnp_auto_config_dev - automatically assigns resources to a device
286 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288int pnp_auto_config_dev(struct pnp_dev *dev)
289{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600290 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700292 if (!pnp_can_configure(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600293 pnp_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -ENODEV;
295 }
296
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600297 ret = pnp_assign_resources(dev, 0);
298 if (ret == 0)
299 return 0;
300
301 for (i = 1; i < dev->num_dependent_sets; i++) {
302 ret = pnp_assign_resources(dev, i);
303 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700307 dev_err(&dev->dev, "unable to assign resources\n");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600308 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100312 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * @dev: pointer to the desired device
314 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700315 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100317int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!pnp_can_write(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600320 pnp_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -EINVAL;
322 }
323
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600324 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600325 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700326 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -EIO;
328 }
329
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700330 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100331 return 0;
332}
333
334/**
335 * pnp_stop_dev - low-level disable of the PnP device
336 * @dev: pointer to the desired device
337 *
338 * does not free resources
339 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100340int pnp_stop_dev(struct pnp_dev *dev)
341{
342 if (!pnp_can_disable(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600343 pnp_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100344 return -EINVAL;
345 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700346 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700347 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100348 return -EIO;
349 }
350
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700351 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100352 return 0;
353}
354
355/**
356 * pnp_activate_dev - activates a PnP device for use
357 * @dev: pointer to the desired device
358 *
359 * does not validate or set resources so be careful.
360 */
361int pnp_activate_dev(struct pnp_dev *dev)
362{
363 int error;
364
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700365 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800366 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100367
368 /* ensure resources are allocated */
369 if (pnp_auto_config_dev(dev))
370 return -EBUSY;
371
372 error = pnp_start_dev(dev);
373 if (error)
374 return error;
375
376 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/**
381 * pnp_disable_dev - disables device
382 * @dev: pointer to the desired device
383 *
384 * inform the correct pnp protocol so that resources can be used by other devices
385 */
386int pnp_disable_dev(struct pnp_dev *dev)
387{
Pierre Ossman68094e32005-11-29 09:09:32 +0100388 int error;
389
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700390 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Pierre Ossman68094e32005-11-29 09:09:32 +0100393 error = pnp_stop_dev(dev);
394 if (error)
395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800400 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600401 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800402 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Pierre Ossman68094e32005-11-29 09:09:32 +0100407EXPORT_SYMBOL(pnp_start_dev);
408EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409EXPORT_SYMBOL(pnp_activate_dev);
410EXPORT_SYMBOL(pnp_disable_dev);