2 * interface.c - contains everything related to the user interface
4 * Some code, especially possible resource dumping is based on isapnp_proc.c (c) Jaroslav Kysela <perex@perex.cz>
5 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
9 #include <linux/string.h>
10 #include <linux/errno.h>
11 #include <linux/list.h>
12 #include <linux/types.h>
13 #include <linux/pnp.h>
14 #include <linux/stat.h>
15 #include <linux/ctype.h>
16 #include <linux/slab.h>
17 #include <linux/mutex.h>
19 #include <asm/uaccess.h>
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 */
32 typedef struct pnp_info_buffer pnp_info_buffer_t;
34 static int pnp_printf(pnp_info_buffer_t * buffer, char *fmt, ...)
39 if (buffer->stop || buffer->error)
42 res = vsnprintf(buffer->curr, buffer->len - buffer->size, fmt, args);
44 if (buffer->size + res >= buffer->len) {
53 static void pnp_print_port(pnp_info_buffer_t * buffer, char *space,
54 struct pnp_port *port)
56 pnp_printf(buffer, "%sport %#llx-%#llx, align %#llx, size %#llx, "
57 "%i-bit address decoding\n", space,
58 (unsigned long long) port->min,
59 (unsigned long long) port->max,
60 port->align ? ((unsigned long long) port->align - 1) : 0,
61 (unsigned long long) port->size,
62 port->flags & IORESOURCE_IO_16BIT_ADDR ? 16 : 10);
65 static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space,
70 pnp_printf(buffer, "%sirq ", space);
71 for (i = 0; i < PNP_IRQ_NR; i++)
72 if (test_bit(i, irq->map.bits)) {
74 pnp_printf(buffer, ",");
79 pnp_printf(buffer, "2/9");
81 pnp_printf(buffer, "%i", i);
83 if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
84 pnp_printf(buffer, "<none>");
85 if (irq->flags & IORESOURCE_IRQ_HIGHEDGE)
86 pnp_printf(buffer, " High-Edge");
87 if (irq->flags & IORESOURCE_IRQ_LOWEDGE)
88 pnp_printf(buffer, " Low-Edge");
89 if (irq->flags & IORESOURCE_IRQ_HIGHLEVEL)
90 pnp_printf(buffer, " High-Level");
91 if (irq->flags & IORESOURCE_IRQ_LOWLEVEL)
92 pnp_printf(buffer, " Low-Level");
93 pnp_printf(buffer, "\n");
96 static void pnp_print_dma(pnp_info_buffer_t * buffer, char *space,
102 pnp_printf(buffer, "%sdma ", space);
103 for (i = 0; i < 8; i++)
104 if (dma->map & (1 << i)) {
106 pnp_printf(buffer, ",");
110 pnp_printf(buffer, "%i", i);
113 pnp_printf(buffer, "<none>");
114 switch (dma->flags & IORESOURCE_DMA_TYPE_MASK) {
115 case IORESOURCE_DMA_8BIT:
118 case IORESOURCE_DMA_8AND16BIT:
124 pnp_printf(buffer, " %s", s);
125 if (dma->flags & IORESOURCE_DMA_MASTER)
126 pnp_printf(buffer, " master");
127 if (dma->flags & IORESOURCE_DMA_BYTE)
128 pnp_printf(buffer, " byte-count");
129 if (dma->flags & IORESOURCE_DMA_WORD)
130 pnp_printf(buffer, " word-count");
131 switch (dma->flags & IORESOURCE_DMA_SPEED_MASK) {
132 case IORESOURCE_DMA_TYPEA:
135 case IORESOURCE_DMA_TYPEB:
138 case IORESOURCE_DMA_TYPEF:
145 pnp_printf(buffer, " %s\n", s);
148 static void pnp_print_mem(pnp_info_buffer_t * buffer, char *space,
153 pnp_printf(buffer, "%sMemory %#llx-%#llx, align %#llx, size %#llx",
154 space, (unsigned long long) mem->min,
155 (unsigned long long) mem->max,
156 (unsigned long long) mem->align,
157 (unsigned long long) mem->size);
158 if (mem->flags & IORESOURCE_MEM_WRITEABLE)
159 pnp_printf(buffer, ", writeable");
160 if (mem->flags & IORESOURCE_MEM_CACHEABLE)
161 pnp_printf(buffer, ", cacheable");
162 if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
163 pnp_printf(buffer, ", range-length");
164 if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
165 pnp_printf(buffer, ", shadowable");
166 if (mem->flags & IORESOURCE_MEM_EXPANSIONROM)
167 pnp_printf(buffer, ", expansion ROM");
168 switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
169 case IORESOURCE_MEM_8BIT:
172 case IORESOURCE_MEM_8AND16BIT:
175 case IORESOURCE_MEM_32BIT:
181 pnp_printf(buffer, ", %s\n", s);
184 static void pnp_print_option(pnp_info_buffer_t * buffer, char *space,
185 struct pnp_option *option, int dep)
188 struct pnp_port *port;
194 switch (option->priority) {
195 case PNP_RES_PRIORITY_PREFERRED:
198 case PNP_RES_PRIORITY_ACCEPTABLE:
201 case PNP_RES_PRIORITY_FUNCTIONAL:
207 pnp_printf(buffer, "Dependent: %02i - Priority %s\n", dep, s);
210 for (port = option->port; port; port = port->next)
211 pnp_print_port(buffer, space, port);
212 for (irq = option->irq; irq; irq = irq->next)
213 pnp_print_irq(buffer, space, irq);
214 for (dma = option->dma; dma; dma = dma->next)
215 pnp_print_dma(buffer, space, dma);
216 for (mem = option->mem; mem; mem = mem->next)
217 pnp_print_mem(buffer, space, mem);
220 static ssize_t pnp_show_options(struct device *dmdev,
221 struct device_attribute *attr, char *buf)
223 struct pnp_dev *dev = to_pnp_dev(dmdev);
224 pnp_info_buffer_t *buffer;
225 struct pnp_option *independent = dev->independent;
226 struct pnp_option *dependent = dev->dependent;
229 buffer = pnp_alloc(sizeof(pnp_info_buffer_t));
233 buffer->len = PAGE_SIZE;
234 buffer->buffer = buf;
235 buffer->curr = buffer->buffer;
237 pnp_print_option(buffer, "", independent, 0);
240 pnp_print_option(buffer, " ", dependent, dep);
241 dependent = dependent->next;
244 ret = (buffer->curr - buf);
249 static DEVICE_ATTR(options, S_IRUGO, pnp_show_options, NULL);
251 static ssize_t pnp_show_current_resources(struct device *dmdev,
252 struct device_attribute *attr,
255 struct pnp_dev *dev = to_pnp_dev(dmdev);
256 pnp_info_buffer_t *buffer;
257 struct pnp_resource *pnp_res;
258 struct resource *res;
264 buffer = pnp_alloc(sizeof(pnp_info_buffer_t));
268 buffer->len = PAGE_SIZE;
269 buffer->buffer = buf;
270 buffer->curr = buffer->buffer;
272 pnp_printf(buffer, "state = %s\n", dev->active ? "active" : "disabled");
274 list_for_each_entry(pnp_res, &dev->resources, list) {
277 pnp_printf(buffer, pnp_resource_type_name(res));
279 if (res->flags & IORESOURCE_DISABLED) {
280 pnp_printf(buffer, " disabled\n");
284 switch (pnp_resource_type(res)) {
287 pnp_printf(buffer, " %#llx-%#llx\n",
288 (unsigned long long) res->start,
289 (unsigned long long) res->end);
293 pnp_printf(buffer, " %lld\n",
294 (unsigned long long) res->start);
299 ret = (buffer->curr - buf);
304 static ssize_t pnp_set_current_resources(struct device *dmdev,
305 struct device_attribute *attr,
306 const char *ubuf, size_t count)
308 struct pnp_dev *dev = to_pnp_dev(dmdev);
309 char *buf = (void *)ubuf;
311 resource_size_t start, end;
313 if (dev->status & PNP_ATTACHED) {
315 dev_info(&dev->dev, "in use; can't configure\n");
319 while (isspace(*buf))
321 if (!strnicmp(buf, "disable", 7)) {
322 retval = pnp_disable_dev(dev);
325 if (!strnicmp(buf, "activate", 8)) {
326 retval = pnp_activate_dev(dev);
329 if (!strnicmp(buf, "fill", 4)) {
332 retval = pnp_auto_config_dev(dev);
335 if (!strnicmp(buf, "auto", 4)) {
338 pnp_init_resources(dev);
339 retval = pnp_auto_config_dev(dev);
342 if (!strnicmp(buf, "clear", 5)) {
345 pnp_init_resources(dev);
348 if (!strnicmp(buf, "get", 3)) {
349 mutex_lock(&pnp_res_mutex);
350 if (pnp_can_read(dev))
351 dev->protocol->get(dev);
352 mutex_unlock(&pnp_res_mutex);
355 if (!strnicmp(buf, "set", 3)) {
359 pnp_init_resources(dev);
360 mutex_lock(&pnp_res_mutex);
362 while (isspace(*buf))
364 if (!strnicmp(buf, "io", 2)) {
366 while (isspace(*buf))
368 start = simple_strtoul(buf, &buf, 0);
369 while (isspace(*buf))
373 while (isspace(*buf))
375 end = simple_strtoul(buf, &buf, 0);
378 pnp_add_io_resource(dev, start, end, 0);
381 if (!strnicmp(buf, "mem", 3)) {
383 while (isspace(*buf))
385 start = simple_strtoul(buf, &buf, 0);
386 while (isspace(*buf))
390 while (isspace(*buf))
392 end = simple_strtoul(buf, &buf, 0);
395 pnp_add_mem_resource(dev, start, end, 0);
398 if (!strnicmp(buf, "irq", 3)) {
400 while (isspace(*buf))
402 start = simple_strtoul(buf, &buf, 0);
403 pnp_add_irq_resource(dev, start, 0);
406 if (!strnicmp(buf, "dma", 3)) {
408 while (isspace(*buf))
410 start = simple_strtoul(buf, &buf, 0);
411 pnp_add_dma_resource(dev, start, 0);
416 mutex_unlock(&pnp_res_mutex);
426 static DEVICE_ATTR(resources, S_IRUGO | S_IWUSR,
427 pnp_show_current_resources, pnp_set_current_resources);
429 static ssize_t pnp_show_current_ids(struct device *dmdev,
430 struct device_attribute *attr, char *buf)
433 struct pnp_dev *dev = to_pnp_dev(dmdev);
434 struct pnp_id *pos = dev->id;
437 str += sprintf(str, "%s\n", pos->id);
443 static DEVICE_ATTR(id, S_IRUGO, pnp_show_current_ids, NULL);
445 int pnp_interface_attach_device(struct pnp_dev *dev)
447 int rc = device_create_file(&dev->dev, &dev_attr_options);
451 rc = device_create_file(&dev->dev, &dev_attr_resources);
454 rc = device_create_file(&dev->dev, &dev_attr_id);
461 device_remove_file(&dev->dev, &dev_attr_resources);
463 device_remove_file(&dev->dev, &dev_attr_options);