Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
4 *
5 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
6 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
7 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
9 */
10
11#include <linux/errno.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/pnp.h>
16#include <linux/bitmap.h>
17#include <linux/mutex.h>
18#include "base.h"
19
20DEFINE_MUTEX(pnp_res_mutex);
21
22static 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
38static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
39{
40 struct resource *res, local_res;
41
42 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx);
43 if (res) {
44 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
45 "flags %#lx\n", idx, (unsigned long long) res->start,
46 (unsigned long long) res->end, res->flags);
47 return 0;
48 }
49
50 res = &local_res;
51 res->flags = rule->flags | IORESOURCE_AUTO;
52 res->start = 0;
53 res->end = 0;
54
55 if (!rule->size) {
56 res->flags |= IORESOURCE_DISABLED;
57 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
58 goto __add;
59 }
60
61 res->start = rule->min;
62 res->end = res->start + rule->size - 1;
63
64 while (!pnp_check_port(dev, res)) {
65 res->start += rule->align;
66 res->end = res->start + rule->size - 1;
67 if (res->start > rule->max || !rule->align) {
68 pnp_dbg(&dev->dev, " couldn't assign io %d "
69 "(min %#llx max %#llx)\n", idx,
70 (unsigned long long) rule->min,
71 (unsigned long long) rule->max);
72 return -EBUSY;
73 }
74 }
75
76__add:
77 pnp_add_io_resource(dev, res->start, res->end, res->flags);
78 return 0;
79}
80
81static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
82{
83 struct resource *res, local_res;
84
85 res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx);
86 if (res) {
87 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
88 "flags %#lx\n", idx, (unsigned long long) res->start,
89 (unsigned long long) res->end, res->flags);
90 return 0;
91 }
92
93 res = &local_res;
94 res->flags = rule->flags | IORESOURCE_AUTO;
95 res->start = 0;
96 res->end = 0;
97
98 /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */
99 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
100 res->flags |= IORESOURCE_READONLY;
101 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
102 res->flags |= IORESOURCE_RANGELENGTH;
103 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
104 res->flags |= IORESOURCE_SHADOWABLE;
105
106 if (!rule->size) {
107 res->flags |= IORESOURCE_DISABLED;
108 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
109 goto __add;
110 }
111
112 res->start = rule->min;
113 res->end = res->start + rule->size - 1;
114
115 while (!pnp_check_mem(dev, res)) {
116 res->start += rule->align;
117 res->end = res->start + rule->size - 1;
118 if (res->start > rule->max || !rule->align) {
119 pnp_dbg(&dev->dev, " couldn't assign mem %d "
120 "(min %#llx max %#llx)\n", idx,
121 (unsigned long long) rule->min,
122 (unsigned long long) rule->max);
123 return -EBUSY;
124 }
125 }
126
127__add:
128 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
129 return 0;
130}
131
132static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
133{
134 struct resource *res, local_res;
135 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
142 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
143 if (res) {
144 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
145 idx, (int) res->start, res->flags);
146 return 0;
147 }
148
149 res = &local_res;
150 res->flags = rule->flags | IORESOURCE_AUTO;
151 res->start = -1;
152 res->end = -1;
153
154 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
155 res->flags |= IORESOURCE_DISABLED;
156 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
157 goto __add;
158 }
159
160 /* TBD: need check for >16 IRQ */
161 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
162 if (res->start < PNP_IRQ_NR) {
163 res->end = res->start;
164 goto __add;
165 }
166 for (i = 0; i < 16; i++) {
167 if (test_bit(xtab[i], rule->map.bits)) {
168 res->start = res->end = xtab[i];
169 if (pnp_check_irq(dev, res))
170 goto __add;
171 }
172 }
173
174 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
175 res->start = -1;
176 res->end = -1;
177 res->flags |= IORESOURCE_DISABLED;
178 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
179 goto __add;
180 }
181
182 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
183 return -EBUSY;
184
185__add:
186 pnp_add_irq_resource(dev, res->start, res->flags);
187 return 0;
188}
189
190#ifdef CONFIG_ISA_DMA_API
191static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
192{
193 struct resource *res, local_res;
194 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
201 res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
202 if (res) {
203 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
204 idx, (int) res->start, res->flags);
205 return 0;
206 }
207
208 res = &local_res;
209 res->flags = rule->flags | IORESOURCE_AUTO;
210 res->start = -1;
211 res->end = -1;
212
213 if (!rule->map) {
214 res->flags |= IORESOURCE_DISABLED;
215 pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
216 goto __add;
217 }
218
219 for (i = 0; i < 8; i++) {
220 if (rule->map & (1 << xtab[i])) {
221 res->start = res->end = xtab[i];
222 if (pnp_check_dma(dev, res))
223 goto __add;
224 }
225 }
226
227 pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
228 return -EBUSY;
229
230__add:
231 pnp_add_dma_resource(dev, res->start, res->flags);
232 return 0;
233}
234#endif /* CONFIG_ISA_DMA_API */
235
236void pnp_init_resources(struct pnp_dev *dev)
237{
238 pnp_free_resources(dev);
239}
240
241static void pnp_clean_resource_table(struct pnp_dev *dev)
242{
243 struct pnp_resource *pnp_res, *tmp;
244
245 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);
248 }
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
254 * @set: the dependent function number
255 */
256static int pnp_assign_resources(struct pnp_dev *dev, int set)
257{
258 struct pnp_option *option;
259 int nport = 0, nmem = 0, nirq = 0;
260 int ndma __maybe_unused = 0;
261 int ret = 0;
262
263 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
264 mutex_lock(&pnp_res_mutex);
265 pnp_clean_resource_table(dev);
266
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;
282#ifdef CONFIG_ISA_DMA_API
283 case IORESOURCE_DMA:
284 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
285 break;
286#endif
287 default:
288 ret = -EINVAL;
289 break;
290 }
291 if (ret < 0)
292 break;
293 }
294
295 mutex_unlock(&pnp_res_mutex);
296 if (ret < 0) {
297 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
298 pnp_clean_resource_table(dev);
299 } else
300 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
301 return ret;
302}
303
304/**
305 * pnp_auto_config_dev - automatically assigns resources to a device
306 * @dev: pointer to the desired device
307 */
308int pnp_auto_config_dev(struct pnp_dev *dev)
309{
310 int i, ret;
311
312 if (!pnp_can_configure(dev)) {
313 pnp_dbg(&dev->dev, "configuration not supported\n");
314 return -ENODEV;
315 }
316
317 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)
324 return 0;
325 }
326
327 dev_err(&dev->dev, "unable to assign resources\n");
328 return ret;
329}
330
331/**
332 * pnp_start_dev - low-level start of the PnP device
333 * @dev: pointer to the desired device
334 *
335 * assumes that resources have already been allocated
336 */
337int pnp_start_dev(struct pnp_dev *dev)
338{
339 if (!pnp_can_write(dev)) {
340 pnp_dbg(&dev->dev, "activation not supported\n");
341 return -EINVAL;
342 }
343
344 dbg_pnp_show_resources(dev, "pnp_start_dev");
345 if (dev->protocol->set(dev) < 0) {
346 dev_err(&dev->dev, "activation failed\n");
347 return -EIO;
348 }
349
350 dev_info(&dev->dev, "activated\n");
351 return 0;
352}
353EXPORT_SYMBOL(pnp_start_dev);
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 */
361int pnp_stop_dev(struct pnp_dev *dev)
362{
363 if (!pnp_can_disable(dev)) {
364 pnp_dbg(&dev->dev, "disabling not supported\n");
365 return -EINVAL;
366 }
367 if (dev->protocol->disable(dev) < 0) {
368 dev_err(&dev->dev, "disable failed\n");
369 return -EIO;
370 }
371
372 dev_info(&dev->dev, "disabled\n");
373 return 0;
374}
375EXPORT_SYMBOL(pnp_stop_dev);
376
377/**
378 * pnp_activate_dev - activates a PnP device for use
379 * @dev: pointer to the desired device
380 *
381 * does not validate or set resources so be careful.
382 */
383int pnp_activate_dev(struct pnp_dev *dev)
384{
385 int error;
386
387 if (dev->active)
388 return 0;
389
390 /* ensure resources are allocated */
391 if (pnp_auto_config_dev(dev))
392 return -EBUSY;
393
394 error = pnp_start_dev(dev);
395 if (error)
396 return error;
397
398 dev->active = 1;
399 return 0;
400}
401EXPORT_SYMBOL(pnp_activate_dev);
402
403/**
404 * pnp_disable_dev - disables device
405 * @dev: pointer to the desired device
406 *
407 * inform the correct pnp protocol so that resources can be used by other devices
408 */
409int pnp_disable_dev(struct pnp_dev *dev)
410{
411 int error;
412
413 if (!dev->active)
414 return 0;
415
416 error = pnp_stop_dev(dev);
417 if (error)
418 return error;
419
420 dev->active = 0;
421
422 /* release the resources so that other devices can use them */
423 mutex_lock(&pnp_res_mutex);
424 pnp_clean_resource_table(dev);
425 mutex_unlock(&pnp_res_mutex);
426
427 return 0;
428}
429EXPORT_SYMBOL(pnp_disable_dev);
1/*
2 * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
3 *
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
8 */
9
10#include <linux/errno.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/pnp.h>
15#include <linux/bitmap.h>
16#include <linux/mutex.h>
17#include "base.h"
18
19DEFINE_MUTEX(pnp_res_mutex);
20
21static 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
37static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
38{
39 struct resource *res, local_res;
40
41 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx);
42 if (res) {
43 pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
44 "flags %#lx\n", idx, (unsigned long long) res->start,
45 (unsigned long long) res->end, res->flags);
46 return 0;
47 }
48
49 res = &local_res;
50 res->flags = rule->flags | IORESOURCE_AUTO;
51 res->start = 0;
52 res->end = 0;
53
54 if (!rule->size) {
55 res->flags |= IORESOURCE_DISABLED;
56 pnp_dbg(&dev->dev, " io %d disabled\n", idx);
57 goto __add;
58 }
59
60 res->start = rule->min;
61 res->end = res->start + rule->size - 1;
62
63 while (!pnp_check_port(dev, res)) {
64 res->start += rule->align;
65 res->end = res->start + rule->size - 1;
66 if (res->start > rule->max || !rule->align) {
67 pnp_dbg(&dev->dev, " couldn't assign io %d "
68 "(min %#llx max %#llx)\n", idx,
69 (unsigned long long) rule->min,
70 (unsigned long long) rule->max);
71 return -EBUSY;
72 }
73 }
74
75__add:
76 pnp_add_io_resource(dev, res->start, res->end, res->flags);
77 return 0;
78}
79
80static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
81{
82 struct resource *res, local_res;
83
84 res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx);
85 if (res) {
86 pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
87 "flags %#lx\n", idx, (unsigned long long) res->start,
88 (unsigned long long) res->end, res->flags);
89 return 0;
90 }
91
92 res = &local_res;
93 res->flags = rule->flags | IORESOURCE_AUTO;
94 res->start = 0;
95 res->end = 0;
96
97 /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */
98 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
99 res->flags |= IORESOURCE_READONLY;
100 if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
101 res->flags |= IORESOURCE_RANGELENGTH;
102 if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
103 res->flags |= IORESOURCE_SHADOWABLE;
104
105 if (!rule->size) {
106 res->flags |= IORESOURCE_DISABLED;
107 pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
108 goto __add;
109 }
110
111 res->start = rule->min;
112 res->end = res->start + rule->size - 1;
113
114 while (!pnp_check_mem(dev, res)) {
115 res->start += rule->align;
116 res->end = res->start + rule->size - 1;
117 if (res->start > rule->max || !rule->align) {
118 pnp_dbg(&dev->dev, " couldn't assign mem %d "
119 "(min %#llx max %#llx)\n", idx,
120 (unsigned long long) rule->min,
121 (unsigned long long) rule->max);
122 return -EBUSY;
123 }
124 }
125
126__add:
127 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
128 return 0;
129}
130
131static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
132{
133 struct resource *res, local_res;
134 int i;
135
136 /* IRQ priority: this table is good for i386 */
137 static unsigned short xtab[16] = {
138 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
139 };
140
141 res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
142 if (res) {
143 pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
144 idx, (int) res->start, res->flags);
145 return 0;
146 }
147
148 res = &local_res;
149 res->flags = rule->flags | IORESOURCE_AUTO;
150 res->start = -1;
151 res->end = -1;
152
153 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
154 res->flags |= IORESOURCE_DISABLED;
155 pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
156 goto __add;
157 }
158
159 /* TBD: need check for >16 IRQ */
160 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
161 if (res->start < PNP_IRQ_NR) {
162 res->end = res->start;
163 goto __add;
164 }
165 for (i = 0; i < 16; i++) {
166 if (test_bit(xtab[i], rule->map.bits)) {
167 res->start = res->end = xtab[i];
168 if (pnp_check_irq(dev, res))
169 goto __add;
170 }
171 }
172
173 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
174 res->start = -1;
175 res->end = -1;
176 res->flags |= IORESOURCE_DISABLED;
177 pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
178 goto __add;
179 }
180
181 pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
182 return -EBUSY;
183
184__add:
185 pnp_add_irq_resource(dev, res->start, res->flags);
186 return 0;
187}
188
189#ifdef CONFIG_ISA_DMA_API
190static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
191{
192 struct resource *res, local_res;
193 int i;
194
195 /* DMA priority: this table is good for i386 */
196 static unsigned short xtab[8] = {
197 1, 3, 5, 6, 7, 0, 2, 4
198 };
199
200 res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
201 if (res) {
202 pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
203 idx, (int) res->start, res->flags);
204 return 0;
205 }
206
207 res = &local_res;
208 res->flags = rule->flags | IORESOURCE_AUTO;
209 res->start = -1;
210 res->end = -1;
211
212 if (!rule->map) {
213 res->flags |= IORESOURCE_DISABLED;
214 pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
215 goto __add;
216 }
217
218 for (i = 0; i < 8; i++) {
219 if (rule->map & (1 << xtab[i])) {
220 res->start = res->end = xtab[i];
221 if (pnp_check_dma(dev, res))
222 goto __add;
223 }
224 }
225
226 pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
227 return -EBUSY;
228
229__add:
230 pnp_add_dma_resource(dev, res->start, res->flags);
231 return 0;
232}
233#endif /* CONFIG_ISA_DMA_API */
234
235void pnp_init_resources(struct pnp_dev *dev)
236{
237 pnp_free_resources(dev);
238}
239
240static void pnp_clean_resource_table(struct pnp_dev *dev)
241{
242 struct pnp_resource *pnp_res, *tmp;
243
244 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
245 if (pnp_res->res.flags & IORESOURCE_AUTO)
246 pnp_free_resource(pnp_res);
247 }
248}
249
250/**
251 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
252 * @dev: pointer to the desired device
253 * @set: the dependent function number
254 */
255static int pnp_assign_resources(struct pnp_dev *dev, int set)
256{
257 struct pnp_option *option;
258 int nport = 0, nmem = 0, nirq = 0;
259 int ndma __maybe_unused = 0;
260 int ret = 0;
261
262 pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
263 mutex_lock(&pnp_res_mutex);
264 pnp_clean_resource_table(dev);
265
266 list_for_each_entry(option, &dev->options, list) {
267 if (pnp_option_is_dependent(option) &&
268 pnp_option_set(option) != set)
269 continue;
270
271 switch (option->type) {
272 case IORESOURCE_IO:
273 ret = pnp_assign_port(dev, &option->u.port, nport++);
274 break;
275 case IORESOURCE_MEM:
276 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
277 break;
278 case IORESOURCE_IRQ:
279 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
280 break;
281#ifdef CONFIG_ISA_DMA_API
282 case IORESOURCE_DMA:
283 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
284 break;
285#endif
286 default:
287 ret = -EINVAL;
288 break;
289 }
290 if (ret < 0)
291 break;
292 }
293
294 mutex_unlock(&pnp_res_mutex);
295 if (ret < 0) {
296 pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
297 pnp_clean_resource_table(dev);
298 } else
299 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
300 return ret;
301}
302
303/**
304 * pnp_auto_config_dev - automatically assigns resources to a device
305 * @dev: pointer to the desired device
306 */
307int pnp_auto_config_dev(struct pnp_dev *dev)
308{
309 int i, ret;
310
311 if (!pnp_can_configure(dev)) {
312 pnp_dbg(&dev->dev, "configuration not supported\n");
313 return -ENODEV;
314 }
315
316 ret = pnp_assign_resources(dev, 0);
317 if (ret == 0)
318 return 0;
319
320 for (i = 1; i < dev->num_dependent_sets; i++) {
321 ret = pnp_assign_resources(dev, i);
322 if (ret == 0)
323 return 0;
324 }
325
326 dev_err(&dev->dev, "unable to assign resources\n");
327 return ret;
328}
329
330/**
331 * pnp_start_dev - low-level start of the PnP device
332 * @dev: pointer to the desired device
333 *
334 * assumes that resources have already been allocated
335 */
336int pnp_start_dev(struct pnp_dev *dev)
337{
338 if (!pnp_can_write(dev)) {
339 pnp_dbg(&dev->dev, "activation not supported\n");
340 return -EINVAL;
341 }
342
343 dbg_pnp_show_resources(dev, "pnp_start_dev");
344 if (dev->protocol->set(dev) < 0) {
345 dev_err(&dev->dev, "activation failed\n");
346 return -EIO;
347 }
348
349 dev_info(&dev->dev, "activated\n");
350 return 0;
351}
352
353/**
354 * pnp_stop_dev - low-level disable of the PnP device
355 * @dev: pointer to the desired device
356 *
357 * does not free resources
358 */
359int pnp_stop_dev(struct pnp_dev *dev)
360{
361 if (!pnp_can_disable(dev)) {
362 pnp_dbg(&dev->dev, "disabling not supported\n");
363 return -EINVAL;
364 }
365 if (dev->protocol->disable(dev) < 0) {
366 dev_err(&dev->dev, "disable failed\n");
367 return -EIO;
368 }
369
370 dev_info(&dev->dev, "disabled\n");
371 return 0;
372}
373
374/**
375 * pnp_activate_dev - activates a PnP device for use
376 * @dev: pointer to the desired device
377 *
378 * does not validate or set resources so be careful.
379 */
380int pnp_activate_dev(struct pnp_dev *dev)
381{
382 int error;
383
384 if (dev->active)
385 return 0;
386
387 /* ensure resources are allocated */
388 if (pnp_auto_config_dev(dev))
389 return -EBUSY;
390
391 error = pnp_start_dev(dev);
392 if (error)
393 return error;
394
395 dev->active = 1;
396 return 0;
397}
398
399/**
400 * pnp_disable_dev - disables device
401 * @dev: pointer to the desired device
402 *
403 * inform the correct pnp protocol so that resources can be used by other devices
404 */
405int pnp_disable_dev(struct pnp_dev *dev)
406{
407 int error;
408
409 if (!dev->active)
410 return 0;
411
412 error = pnp_stop_dev(dev);
413 if (error)
414 return error;
415
416 dev->active = 0;
417
418 /* release the resources so that other devices can use them */
419 mutex_lock(&pnp_res_mutex);
420 pnp_clean_resource_table(dev);
421 mutex_unlock(&pnp_res_mutex);
422
423 return 0;
424}
425
426EXPORT_SYMBOL(pnp_start_dev);
427EXPORT_SYMBOL(pnp_stop_dev);
428EXPORT_SYMBOL(pnp_activate_dev);
429EXPORT_SYMBOL(pnp_disable_dev);