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