blob: 9357aa779048a241e6dafe6bbaa1058b9eabb89f [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
David Flaterbdf0eb32013-05-10 14:37:43 +0200214 if (!rule->map) {
215 res->flags |= IORESOURCE_DISABLED;
216 pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
217 goto __add;
218 }
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 for (i = 0; i < 8; i++) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700221 if (rule->map & (1 << xtab[i])) {
Bjorn Helgaas28ccffc2008-04-28 16:34:18 -0600222 res->start = res->end = xtab[i];
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600223 if (pnp_check_dma(dev, res))
224 goto __add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 }
David Flaterbdf0eb32013-05-10 14:37:43 +0200227
228 pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
229 return -EBUSY;
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600230
231__add:
232 pnp_add_dma_resource(dev, res->start, res->flags);
Bjorn Helgaas6e906f02008-06-27 16:57:09 -0600233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
David Rientjes586f83e2011-03-22 16:34:56 -0700235#endif /* CONFIG_ISA_DMA_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Bjorn Helgaas2cd1393092008-04-28 16:34:11 -0600237void pnp_init_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600239 pnp_free_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600242static void pnp_clean_resource_table(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600244 struct pnp_resource *pnp_res, *tmp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700245
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600246 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
247 if (pnp_res->res.flags & IORESOURCE_AUTO)
248 pnp_free_resource(pnp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250}
251
252/**
253 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
254 * @dev: pointer to the desired device
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600255 * @set: the dependent function number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600257static int pnp_assign_resources(struct pnp_dev *dev, int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600259 struct pnp_option *option;
David Rientjes586f83e2011-03-22 16:34:56 -0700260 int nport = 0, nmem = 0, nirq = 0;
261 int ndma __maybe_unused = 0;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600262 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600264 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800265 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600266 pnp_clean_resource_table(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600267
268 list_for_each_entry(option, &dev->options, list) {
269 if (pnp_option_is_dependent(option) &&
270 pnp_option_set(option) != set)
271 continue;
272
273 switch (option->type) {
274 case IORESOURCE_IO:
275 ret = pnp_assign_port(dev, &option->u.port, nport++);
276 break;
277 case IORESOURCE_MEM:
278 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
279 break;
280 case IORESOURCE_IRQ:
281 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
282 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700283#ifdef CONFIG_ISA_DMA_API
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600284 case IORESOURCE_DMA:
285 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
286 break;
David Rientjes586f83e2011-03-22 16:34:56 -0700287#endif
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600288 default:
289 ret = -EINVAL;
290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600292 if (ret < 0)
293 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800296 mutex_unlock(&pnp_res_mutex);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600297 if (ret < 0) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600298 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600299 pnp_clean_resource_table(dev);
300 } else
301 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * pnp_auto_config_dev - automatically assigns resources to a device
307 * @dev: pointer to the desired device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
309int pnp_auto_config_dev(struct pnp_dev *dev)
310{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600311 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700313 if (!pnp_can_configure(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600314 pnp_dbg(&dev->dev, "configuration not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -ENODEV;
316 }
317
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600318 ret = pnp_assign_resources(dev, 0);
319 if (ret == 0)
320 return 0;
321
322 for (i = 1; i < dev->num_dependent_sets; i++) {
323 ret = pnp_assign_resources(dev, i);
324 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700328 dev_err(&dev->dev, "unable to assign resources\n");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600329 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/**
Pierre Ossman68094e32005-11-29 09:09:32 +0100333 * pnp_start_dev - low-level start of the PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * @dev: pointer to the desired device
335 *
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700336 * assumes that resources have already been allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100338int pnp_start_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!pnp_can_write(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600341 pnp_dbg(&dev->dev, "activation not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return -EINVAL;
343 }
344
Bjorn Helgaas81b5c752008-04-28 16:34:08 -0600345 dbg_pnp_show_resources(dev, "pnp_start_dev");
Bjorn Helgaas59284cb2008-04-28 16:34:05 -0600346 if (dev->protocol->set(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700347 dev_err(&dev->dev, "activation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return -EIO;
349 }
350
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700351 dev_info(&dev->dev, "activated\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100352 return 0;
353}
354
355/**
356 * pnp_stop_dev - low-level disable of the PnP device
357 * @dev: pointer to the desired device
358 *
359 * does not free resources
360 */
Pierre Ossman68094e32005-11-29 09:09:32 +0100361int pnp_stop_dev(struct pnp_dev *dev)
362{
363 if (!pnp_can_disable(dev)) {
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600364 pnp_dbg(&dev->dev, "disabling not supported\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100365 return -EINVAL;
366 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700367 if (dev->protocol->disable(dev) < 0) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700368 dev_err(&dev->dev, "disable failed\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100369 return -EIO;
370 }
371
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700372 dev_info(&dev->dev, "disabled\n");
Pierre Ossman68094e32005-11-29 09:09:32 +0100373 return 0;
374}
375
376/**
377 * pnp_activate_dev - activates a PnP device for use
378 * @dev: pointer to the desired device
379 *
380 * does not validate or set resources so be careful.
381 */
382int pnp_activate_dev(struct pnp_dev *dev)
383{
384 int error;
385
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700386 if (dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800387 return 0;
Pierre Ossman68094e32005-11-29 09:09:32 +0100388
389 /* ensure resources are allocated */
390 if (pnp_auto_config_dev(dev))
391 return -EBUSY;
392
393 error = pnp_start_dev(dev);
394 if (error)
395 return error;
396
397 dev->active = 1;
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/**
402 * pnp_disable_dev - disables device
403 * @dev: pointer to the desired device
404 *
405 * inform the correct pnp protocol so that resources can be used by other devices
406 */
407int pnp_disable_dev(struct pnp_dev *dev)
408{
Pierre Ossman68094e32005-11-29 09:09:32 +0100409 int error;
410
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700411 if (!dev->active)
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Pierre Ossman68094e32005-11-29 09:09:32 +0100414 error = pnp_stop_dev(dev);
415 if (error)
416 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 dev->active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* release the resources so that other devices can use them */
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800421 mutex_lock(&pnp_res_mutex);
Bjorn Helgaas6969c7e2008-04-28 16:34:10 -0600422 pnp_clean_resource_table(dev);
Daniel Walkerb3bd86e2008-02-06 01:40:04 -0800423 mutex_unlock(&pnp_res_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Bjorn Helgaascc8259a2008-02-06 01:40:02 -0800425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Pierre Ossman68094e32005-11-29 09:09:32 +0100428EXPORT_SYMBOL(pnp_start_dev);
429EXPORT_SYMBOL(pnp_stop_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430EXPORT_SYMBOL(pnp_activate_dev);
431EXPORT_SYMBOL(pnp_disable_dev);