PCI: use %pF instead of print_fn_descriptor_symbol() in quirks.c
[deliverable/linux.git] / drivers / pnp / interface.c
CommitLineData
1da177e4
LT
1/*
2 * interface.c - contains everything related to the user interface
3 *
c1017a4c 4 * Some code, especially possible resource dumping is based on isapnp_proc.c (c) Jaroslav Kysela <perex@perex.cz>
1da177e4 5 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
1f32ca31
BH
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
1da177e4
LT
8 */
9
10#include <linux/pnp.h>
11#include <linux/string.h>
12#include <linux/errno.h>
13#include <linux/list.h>
14#include <linux/types.h>
b3bd86e2 15#include <linux/pnp.h>
1da177e4
LT
16#include <linux/stat.h>
17#include <linux/ctype.h>
18#include <linux/slab.h>
b3bd86e2
DW
19#include <linux/mutex.h>
20
1da177e4
LT
21#include <asm/uaccess.h>
22
23#include "base.h"
24
25struct pnp_info_buffer {
26 char *buffer; /* pointer to begin of buffer */
27 char *curr; /* current position in buffer */
28 unsigned long size; /* current size */
29 unsigned long len; /* total length of buffer */
30 int stop; /* stop flag */
31 int error; /* error code */
32};
33
34typedef struct pnp_info_buffer pnp_info_buffer_t;
35
9dd78466 36static int pnp_printf(pnp_info_buffer_t * buffer, char *fmt, ...)
1da177e4
LT
37{
38 va_list args;
39 int res;
40
41 if (buffer->stop || buffer->error)
42 return 0;
43 va_start(args, fmt);
44 res = vsnprintf(buffer->curr, buffer->len - buffer->size, fmt, args);
45 va_end(args);
46 if (buffer->size + res >= buffer->len) {
47 buffer->stop = 1;
48 return 0;
49 }
50 buffer->curr += res;
51 buffer->size += res;
52 return res;
53}
54
9dd78466
BH
55static void pnp_print_port(pnp_info_buffer_t * buffer, char *space,
56 struct pnp_port *port)
1da177e4 57{
169aaffe
BH
58 pnp_printf(buffer, "%sport %#llx-%#llx, align %#llx, size %#llx, "
59 "%i-bit address decoding\n", space,
60 (unsigned long long) port->min,
61 (unsigned long long) port->max,
62 port->align ? ((unsigned long long) port->align - 1) : 0,
63 (unsigned long long) port->size,
08c9f262 64 port->flags & IORESOURCE_IO_16BIT_ADDR ? 16 : 10);
1da177e4
LT
65}
66
9dd78466
BH
67static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space,
68 struct pnp_irq *irq)
1da177e4
LT
69{
70 int first = 1, i;
71
72 pnp_printf(buffer, "%sirq ", space);
73 for (i = 0; i < PNP_IRQ_NR; i++)
7aefff51 74 if (test_bit(i, irq->map.bits)) {
1da177e4
LT
75 if (!first) {
76 pnp_printf(buffer, ",");
77 } else {
78 first = 0;
79 }
80 if (i == 2 || i == 9)
81 pnp_printf(buffer, "2/9");
82 else
83 pnp_printf(buffer, "%i", i);
84 }
7aefff51 85 if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
1da177e4
LT
86 pnp_printf(buffer, "<none>");
87 if (irq->flags & IORESOURCE_IRQ_HIGHEDGE)
88 pnp_printf(buffer, " High-Edge");
89 if (irq->flags & IORESOURCE_IRQ_LOWEDGE)
90 pnp_printf(buffer, " Low-Edge");
91 if (irq->flags & IORESOURCE_IRQ_HIGHLEVEL)
92 pnp_printf(buffer, " High-Level");
93 if (irq->flags & IORESOURCE_IRQ_LOWLEVEL)
94 pnp_printf(buffer, " Low-Level");
d5ebde6e
BH
95 if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
96 pnp_printf(buffer, " (optional)");
1da177e4
LT
97 pnp_printf(buffer, "\n");
98}
99
9dd78466
BH
100static void pnp_print_dma(pnp_info_buffer_t * buffer, char *space,
101 struct pnp_dma *dma)
1da177e4
LT
102{
103 int first = 1, i;
104 char *s;
105
106 pnp_printf(buffer, "%sdma ", space);
107 for (i = 0; i < 8; i++)
9dd78466 108 if (dma->map & (1 << i)) {
1da177e4
LT
109 if (!first) {
110 pnp_printf(buffer, ",");
111 } else {
112 first = 0;
113 }
114 pnp_printf(buffer, "%i", i);
115 }
116 if (!dma->map)
117 pnp_printf(buffer, "<none>");
118 switch (dma->flags & IORESOURCE_DMA_TYPE_MASK) {
119 case IORESOURCE_DMA_8BIT:
120 s = "8-bit";
121 break;
122 case IORESOURCE_DMA_8AND16BIT:
123 s = "8-bit&16-bit";
124 break;
125 default:
126 s = "16-bit";
127 }
128 pnp_printf(buffer, " %s", s);
129 if (dma->flags & IORESOURCE_DMA_MASTER)
130 pnp_printf(buffer, " master");
131 if (dma->flags & IORESOURCE_DMA_BYTE)
132 pnp_printf(buffer, " byte-count");
133 if (dma->flags & IORESOURCE_DMA_WORD)
134 pnp_printf(buffer, " word-count");
135 switch (dma->flags & IORESOURCE_DMA_SPEED_MASK) {
136 case IORESOURCE_DMA_TYPEA:
137 s = "type-A";
138 break;
139 case IORESOURCE_DMA_TYPEB:
140 s = "type-B";
141 break;
142 case IORESOURCE_DMA_TYPEF:
143 s = "type-F";
144 break;
145 default:
146 s = "compatible";
147 break;
148 }
149 pnp_printf(buffer, " %s\n", s);
150}
151
9dd78466
BH
152static void pnp_print_mem(pnp_info_buffer_t * buffer, char *space,
153 struct pnp_mem *mem)
1da177e4
LT
154{
155 char *s;
156
169aaffe
BH
157 pnp_printf(buffer, "%sMemory %#llx-%#llx, align %#llx, size %#llx",
158 space, (unsigned long long) mem->min,
159 (unsigned long long) mem->max,
160 (unsigned long long) mem->align,
161 (unsigned long long) mem->size);
1da177e4
LT
162 if (mem->flags & IORESOURCE_MEM_WRITEABLE)
163 pnp_printf(buffer, ", writeable");
164 if (mem->flags & IORESOURCE_MEM_CACHEABLE)
165 pnp_printf(buffer, ", cacheable");
166 if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
167 pnp_printf(buffer, ", range-length");
168 if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
169 pnp_printf(buffer, ", shadowable");
170 if (mem->flags & IORESOURCE_MEM_EXPANSIONROM)
171 pnp_printf(buffer, ", expansion ROM");
172 switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
173 case IORESOURCE_MEM_8BIT:
174 s = "8-bit";
175 break;
176 case IORESOURCE_MEM_8AND16BIT:
177 s = "8-bit&16-bit";
178 break;
179 case IORESOURCE_MEM_32BIT:
180 s = "32-bit";
181 break;
182 default:
183 s = "16-bit";
184 }
185 pnp_printf(buffer, ", %s\n", s);
186}
187
9dd78466 188static void pnp_print_option(pnp_info_buffer_t * buffer, char *space,
1f32ca31 189 struct pnp_option *option)
1da177e4 190{
1f32ca31
BH
191 switch (option->type) {
192 case IORESOURCE_IO:
193 pnp_print_port(buffer, space, &option->u.port);
194 break;
195 case IORESOURCE_MEM:
196 pnp_print_mem(buffer, space, &option->u.mem);
197 break;
198 case IORESOURCE_IRQ:
199 pnp_print_irq(buffer, space, &option->u.irq);
200 break;
201 case IORESOURCE_DMA:
202 pnp_print_dma(buffer, space, &option->u.dma);
203 break;
1da177e4 204 }
1da177e4
LT
205}
206
9dd78466
BH
207static ssize_t pnp_show_options(struct device *dmdev,
208 struct device_attribute *attr, char *buf)
1da177e4
LT
209{
210 struct pnp_dev *dev = to_pnp_dev(dmdev);
b72ee1f1 211 pnp_info_buffer_t *buffer;
1f32ca31
BH
212 struct pnp_option *option;
213 int ret, dep = 0, set = 0;
214 char *indent;
1da177e4 215
b72ee1f1 216 buffer = pnp_alloc(sizeof(pnp_info_buffer_t));
1da177e4
LT
217 if (!buffer)
218 return -ENOMEM;
219
220 buffer->len = PAGE_SIZE;
221 buffer->buffer = buf;
222 buffer->curr = buffer->buffer;
1da177e4 223
1f32ca31
BH
224 list_for_each_entry(option, &dev->options, list) {
225 if (pnp_option_is_dependent(option)) {
226 indent = " ";
227 if (!dep || pnp_option_set(option) != set) {
228 set = pnp_option_set(option);
229 dep = 1;
230 pnp_printf(buffer, "Dependent: %02i - "
231 "Priority %s\n", set,
232 pnp_option_priority_name(option));
233 }
234 } else {
235 dep = 0;
236 indent = "";
237 }
238 pnp_print_option(buffer, indent, option);
1da177e4 239 }
1f32ca31 240
1da177e4
LT
241 ret = (buffer->curr - buf);
242 kfree(buffer);
243 return ret;
244}
245
9dd78466
BH
246static ssize_t pnp_show_current_resources(struct device *dmdev,
247 struct device_attribute *attr,
248 char *buf)
1da177e4
LT
249{
250 struct pnp_dev *dev = to_pnp_dev(dmdev);
b72ee1f1 251 pnp_info_buffer_t *buffer;
f61ed7e3 252 struct pnp_resource *pnp_res;
95ab3669 253 struct resource *res;
f61ed7e3 254 int ret;
1da177e4
LT
255
256 if (!dev)
257 return -EINVAL;
258
b72ee1f1 259 buffer = pnp_alloc(sizeof(pnp_info_buffer_t));
1da177e4
LT
260 if (!buffer)
261 return -ENOMEM;
b72ee1f1 262
1da177e4
LT
263 buffer->len = PAGE_SIZE;
264 buffer->buffer = buf;
265 buffer->curr = buffer->buffer;
266
f61ed7e3 267 pnp_printf(buffer, "state = %s\n", dev->active ? "active" : "disabled");
1da177e4 268
f61ed7e3
BH
269 list_for_each_entry(pnp_res, &dev->resources, list) {
270 res = &pnp_res->res;
271
272 pnp_printf(buffer, pnp_resource_type_name(res));
273
274 if (res->flags & IORESOURCE_DISABLED) {
aee3ad81 275 pnp_printf(buffer, " disabled\n");
f61ed7e3
BH
276 continue;
277 }
278
279 switch (pnp_resource_type(res)) {
280 case IORESOURCE_IO:
281 case IORESOURCE_MEM:
282 pnp_printf(buffer, " %#llx-%#llx\n",
aee3ad81
BH
283 (unsigned long long) res->start,
284 (unsigned long long) res->end);
f61ed7e3
BH
285 break;
286 case IORESOURCE_IRQ:
287 case IORESOURCE_DMA:
aee3ad81
BH
288 pnp_printf(buffer, " %lld\n",
289 (unsigned long long) res->start);
f61ed7e3
BH
290 break;
291 }
1da177e4 292 }
f61ed7e3 293
1da177e4
LT
294 ret = (buffer->curr - buf);
295 kfree(buffer);
296 return ret;
297}
298
b72ee1f1
BH
299static ssize_t pnp_set_current_resources(struct device *dmdev,
300 struct device_attribute *attr,
301 const char *ubuf, size_t count)
1da177e4
LT
302{
303 struct pnp_dev *dev = to_pnp_dev(dmdev);
9dd78466
BH
304 char *buf = (void *)ubuf;
305 int retval = 0;
cc8c2e30 306 resource_size_t start, end;
1da177e4
LT
307
308 if (dev->status & PNP_ATTACHED) {
309 retval = -EBUSY;
a05d0781 310 dev_info(&dev->dev, "in use; can't configure\n");
1da177e4
LT
311 goto done;
312 }
313
314 while (isspace(*buf))
315 ++buf;
9dd78466 316 if (!strnicmp(buf, "disable", 7)) {
1da177e4
LT
317 retval = pnp_disable_dev(dev);
318 goto done;
319 }
9dd78466 320 if (!strnicmp(buf, "activate", 8)) {
1da177e4
LT
321 retval = pnp_activate_dev(dev);
322 goto done;
323 }
9dd78466 324 if (!strnicmp(buf, "fill", 4)) {
1da177e4
LT
325 if (dev->active)
326 goto done;
327 retval = pnp_auto_config_dev(dev);
328 goto done;
329 }
9dd78466 330 if (!strnicmp(buf, "auto", 4)) {
1da177e4
LT
331 if (dev->active)
332 goto done;
f4490002 333 pnp_init_resources(dev);
1da177e4
LT
334 retval = pnp_auto_config_dev(dev);
335 goto done;
336 }
9dd78466 337 if (!strnicmp(buf, "clear", 5)) {
1da177e4
LT
338 if (dev->active)
339 goto done;
f4490002 340 pnp_init_resources(dev);
1da177e4
LT
341 goto done;
342 }
9dd78466 343 if (!strnicmp(buf, "get", 3)) {
b3bd86e2 344 mutex_lock(&pnp_res_mutex);
1da177e4 345 if (pnp_can_read(dev))
59284cb4 346 dev->protocol->get(dev);
b3bd86e2 347 mutex_unlock(&pnp_res_mutex);
1da177e4
LT
348 goto done;
349 }
9dd78466 350 if (!strnicmp(buf, "set", 3)) {
1da177e4
LT
351 if (dev->active)
352 goto done;
353 buf += 3;
f4490002 354 pnp_init_resources(dev);
b3bd86e2 355 mutex_lock(&pnp_res_mutex);
1da177e4
LT
356 while (1) {
357 while (isspace(*buf))
358 ++buf;
9dd78466 359 if (!strnicmp(buf, "io", 2)) {
1da177e4
LT
360 buf += 2;
361 while (isspace(*buf))
362 ++buf;
cc8c2e30 363 start = simple_strtoul(buf, &buf, 0);
1da177e4
LT
364 while (isspace(*buf))
365 ++buf;
9dd78466 366 if (*buf == '-') {
1da177e4
LT
367 buf += 1;
368 while (isspace(*buf))
369 ++buf;
cc8c2e30 370 end = simple_strtoul(buf, &buf, 0);
1da177e4 371 } else
cc8c2e30 372 end = start;
87e4acf3 373 pnp_add_io_resource(dev, start, end, 0);
1da177e4
LT
374 continue;
375 }
9dd78466 376 if (!strnicmp(buf, "mem", 3)) {
1da177e4
LT
377 buf += 3;
378 while (isspace(*buf))
379 ++buf;
d6180f36 380 start = simple_strtoul(buf, &buf, 0);
1da177e4
LT
381 while (isspace(*buf))
382 ++buf;
9dd78466 383 if (*buf == '-') {
1da177e4
LT
384 buf += 1;
385 while (isspace(*buf))
386 ++buf;
d6180f36 387 end = simple_strtoul(buf, &buf, 0);
1da177e4 388 } else
d6180f36 389 end = start;
87e4acf3 390 pnp_add_mem_resource(dev, start, end, 0);
1da177e4
LT
391 continue;
392 }
9dd78466 393 if (!strnicmp(buf, "irq", 3)) {
1da177e4
LT
394 buf += 3;
395 while (isspace(*buf))
396 ++buf;
dbddd038 397 start = simple_strtoul(buf, &buf, 0);
87e4acf3 398 pnp_add_irq_resource(dev, start, 0);
1da177e4
LT
399 continue;
400 }
9dd78466 401 if (!strnicmp(buf, "dma", 3)) {
1da177e4
LT
402 buf += 3;
403 while (isspace(*buf))
404 ++buf;
dc16f5f2 405 start = simple_strtoul(buf, &buf, 0);
87e4acf3 406 pnp_add_dma_resource(dev, start, 0);
1da177e4
LT
407 continue;
408 }
409 break;
410 }
b3bd86e2 411 mutex_unlock(&pnp_res_mutex);
1da177e4
LT
412 goto done;
413 }
1e0aa9ad
BH
414
415done:
1da177e4
LT
416 if (retval < 0)
417 return retval;
418 return count;
419}
420
9dd78466
BH
421static ssize_t pnp_show_current_ids(struct device *dmdev,
422 struct device_attribute *attr, char *buf)
1da177e4
LT
423{
424 char *str = buf;
425 struct pnp_dev *dev = to_pnp_dev(dmdev);
9dd78466 426 struct pnp_id *pos = dev->id;
1da177e4
LT
427
428 while (pos) {
9dd78466 429 str += sprintf(str, "%s\n", pos->id);
1da177e4
LT
430 pos = pos->next;
431 }
432 return (str - buf);
433}
434
8a89efd1
DM
435struct device_attribute pnp_interface_attrs[] = {
436 __ATTR(resources, S_IRUGO | S_IWUSR,
437 pnp_show_current_resources,
438 pnp_set_current_resources),
439 __ATTR(options, S_IRUGO, pnp_show_options, NULL),
440 __ATTR(id, S_IRUGO, pnp_show_current_ids, NULL),
441 __ATTR_NULL,
442};
This page took 0.715345 seconds and 5 git commands to generate.