blob: 00fd3577b98575abb218972a4e35d1d62f6366c2 [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/slab.h>
16#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
22static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
23{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060024 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060026 res = pnp_get_resource(dev, IORESOURCE_IO, idx);
27 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060028 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060029 "flags %#lx\n", idx, (unsigned long long) res->start,
30 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060031 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060032 }
33
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060034 res = &local_res;
35 res->flags = rule->flags | IORESOURCE_AUTO;
36 res->start = 0;
37 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060040 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -060041 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060042 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
44
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060045 res->start = rule->min;
46 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -060048 while (!pnp_check_port(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060049 res->start += rule->align;
50 res->end = res->start + rule->size - 1;
51 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060052 pnp_dbg(&dev->dev, " couldn't assign io %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -060053 "(min %#llx max %#llx)\n", idx,
54 (unsigned long long) rule->min,
55 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060056 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060059
60__add:
61 pnp_add_io_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
66{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060067 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060069 res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
70 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -060071 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060072 "flags %#lx\n", idx, (unsigned long long) res->start,
73 (unsigned long long) res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -060074 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060075 }
76
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060077 res = &local_res;
78 res->flags = rule->flags | IORESOURCE_AUTO;
79 res->start = 0;
80 res->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060083 res->flags |= IORESOURCE_READONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060085 res->flags |= IORESOURCE_CACHEABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060087 res->flags |= IORESOURCE_RANGELENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060089 res->flags |= IORESOURCE_SHADOWABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (!rule->size) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060092 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -060093 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060094 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -060097 res->start = rule->min;
98 res->end = res->start + rule->size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Bjorn Helgaasf5d94ff2008-04-28 16:34:22 -0600100 while (!pnp_check_mem(dev, res)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600101 res->start += rule->align;
102 res->end = res->start + rule->size - 1;
103 if (res->start > rule->max || !rule->align) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600104 pnp_dbg(&dev->dev, " couldn't assign mem %d "
Bjorn Helgaasfcfb7ce2008-06-27 16:57:07 -0600105 "(min %#llx max %#llx)\n", idx,
106 (unsigned long long) rule->min,
107 (unsigned long long) rule->max);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600108 return -EBUSY;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600111
112__add:
113 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700117static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600119 struct resource *res, local_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int i;
121
122 /* IRQ priority: this table is good for i386 */
123 static unsigned short xtab[16] = {
124 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
125 };
126
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600127 res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
128 if (res) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600129 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600130 idx, (int) res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600131 return 0;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600132 }
133
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600134 res = &local_res;
135 res->flags = rule->flags | IORESOURCE_AUTO;
136 res->start = -1;
137 res->end = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600139 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600140 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600141 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600142 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
145 /* TBD: need check for >16 IRQ */
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600146 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600147 if (res->start < PNP_IRQ_NR) {
148 res->end = res->start;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600149 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 for (i = 0; i < 16; i++) {
Bjorn Helgaas7aefff52008-06-27 16:57:05 -0600152 if (test_bit(xtab[i], rule->map.bits)) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600153 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600154 if (pnp_check_irq(dev, res))
155 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600158
159 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
160 res->start = -1;
161 res->end = -1;
162 res->flags |= IORESOURCE_DISABLED;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600163 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
Bjorn Helgaasd5ebde62008-06-27 16:57:14 -0600164 goto __add;
165 }
166
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600167 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600168 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600169
170__add:
171 pnp_add_irq_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
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}
214
Bjorn Helgaas2cd1393092008-04-28 16:34:11 -0600215void pnp_init_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600217 pnp_free_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600220static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600222 struct pnp_resource *pnp_res, *tmp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700223
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600224 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
225 if (pnp_res->res.flags & IORESOURCE_AUTO)
226 pnp_free_resource(pnp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228}
229
230/**
231 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
232 * @dev: pointer to the desired device
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600233 * @set: the dependent function number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600235static int pnp_assign_resources(struct pnp_dev *dev, int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600237 struct pnp_option *option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600239 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600241 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800242 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600243 pnp_clean_resource_table(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600244
245 list_for_each_entry(option, &dev->options, list) {
246 if (pnp_option_is_dependent(option) &&
247 pnp_option_set(option) != set)
248 continue;
249
250 switch (option->type) {
251 case IORESOURCE_IO:
252 ret = pnp_assign_port(dev, &option->u.port, nport++);
253 break;
254 case IORESOURCE_MEM:
255 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
256 break;
257 case IORESOURCE_IRQ:
258 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
259 break;
260 case IORESOURCE_DMA:
261 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
262 break;
263 default:
264 ret = -EINVAL;
265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600267 if (ret < 0)
268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800271 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600272 if (ret < 0) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600273 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600274 pnp_clean_resource_table(dev);
275 } else
276 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * pnp_auto_config_dev - automatically assigns resources to a device
282 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284int pnp_auto_config_dev(struct pnp_dev *dev)
285{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600286 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700288 if (!pnp_can_configure(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600289 pnp_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -ENODEV;
291 }
292
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600293 ret = pnp_assign_resources(dev, 0);
294 if (ret == 0)
295 return 0;
296
297 for (i = 1; i < dev->num_dependent_sets; i++) {
298 ret = pnp_assign_resources(dev, i);
299 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700303 dev_err(&dev->dev, "unable to assign resources\n");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600304 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100308 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * @dev: pointer to the desired device
310 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700311 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100313int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!pnp_can_write(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600316 pnp_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return -EINVAL;
318 }
319
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600320 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600321 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700322 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return -EIO;
324 }
325
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700326 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100327 return 0;
328}
329
330/**
331 * pnp_stop_dev - low-level disable of the PnP device
332 * @dev: pointer to the desired device
333 *
334 * does not free resources
335 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100336int pnp_stop_dev(struct pnp_dev *dev)
337{
338 if (!pnp_can_disable(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600339 pnp_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100340 return -EINVAL;
341 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700342 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700343 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100344 return -EIO;
345 }
346
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700347 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100348 return 0;
349}
350
351/**
352 * pnp_activate_dev - activates a PnP device for use
353 * @dev: pointer to the desired device
354 *
355 * does not validate or set resources so be careful.
356 */
357int pnp_activate_dev(struct pnp_dev *dev)
358{
359 int error;
360
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700361 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800362 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100363
364 /* ensure resources are allocated */
365 if (pnp_auto_config_dev(dev))
366 return -EBUSY;
367
368 error = pnp_start_dev(dev);
369 if (error)
370 return error;
371
372 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/**
377 * pnp_disable_dev - disables device
378 * @dev: pointer to the desired device
379 *
380 * inform the correct pnp protocol so that resources can be used by other devices
381 */
382int pnp_disable_dev(struct pnp_dev *dev)
383{
Pierre Ossman68094e32005-11-29 09:09:32 +0100384 int error;
385
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700386 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Pierre Ossman68094e32005-11-29 09:09:32 +0100389 error = pnp_stop_dev(dev);
390 if (error)
391 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800396 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600397 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800398 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Pierre Ossman68094e32005-11-29 09:09:32 +0100403EXPORT_SYMBOL(pnp_start_dev);
404EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405EXPORT_SYMBOL(pnp_activate_dev);
406EXPORT_SYMBOL(pnp_disable_dev);