Merge branch 'linus' into x86/x2apic
[linux-2.6] / drivers / pnp / support.c
1 /*
2  * support.c - standard functions for the use of pnp protocol drivers
3  *
4  * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
5  * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
6  *      Bjorn Helgaas <bjorn.helgaas@hp.com>
7  */
8
9 #include <linux/module.h>
10 #include <linux/ctype.h>
11 #include <linux/pnp.h>
12 #include "base.h"
13
14 /**
15  * pnp_is_active - Determines if a device is active based on its current
16  *      resources
17  * @dev: pointer to the desired PnP device
18  */
19 int pnp_is_active(struct pnp_dev *dev)
20 {
21         /*
22          * I don't think this is very reliable because pnp_disable_dev()
23          * only clears out auto-assigned resources.
24          */
25         if (!pnp_port_start(dev, 0) && pnp_port_len(dev, 0) <= 1 &&
26             !pnp_mem_start(dev, 0) && pnp_mem_len(dev, 0) <= 1 &&
27             pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1)
28                 return 0;
29         else
30                 return 1;
31 }
32
33 EXPORT_SYMBOL(pnp_is_active);
34
35 /*
36  * Functionally similar to acpi_ex_eisa_id_to_string(), but that's
37  * buried in the ACPI CA, and we can't depend on it being present.
38  */
39 void pnp_eisa_id_to_string(u32 id, char *str)
40 {
41         id = be32_to_cpu(id);
42
43         /*
44          * According to the specs, the first three characters are five-bit
45          * compressed ASCII, and the left-over high order bit should be zero.
46          * However, the Linux ISAPNP code historically used six bits for the
47          * first character, and there seem to be IDs that depend on that,
48          * e.g., "nEC8241" in the Linux 8250_pnp serial driver and the
49          * FreeBSD sys/pc98/cbus/sio_cbus.c driver.
50          */
51         str[0] = 'A' + ((id >> 26) & 0x3f) - 1;
52         str[1] = 'A' + ((id >> 21) & 0x1f) - 1;
53         str[2] = 'A' + ((id >> 16) & 0x1f) - 1;
54         str[3] = hex_asc_hi(id >> 8);
55         str[4] = hex_asc_lo(id >> 8);
56         str[5] = hex_asc_hi(id);
57         str[6] = hex_asc_lo(id);
58         str[7] = '\0';
59 }
60
61 char *pnp_resource_type_name(struct resource *res)
62 {
63         switch (pnp_resource_type(res)) {
64         case IORESOURCE_IO:
65                 return "io";
66         case IORESOURCE_MEM:
67                 return "mem";
68         case IORESOURCE_IRQ:
69                 return "irq";
70         case IORESOURCE_DMA:
71                 return "dma";
72         }
73         return NULL;
74 }
75
76 void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc)
77 {
78 #ifdef DEBUG
79         char buf[128];
80         int len = 0;
81         struct pnp_resource *pnp_res;
82         struct resource *res;
83
84         if (list_empty(&dev->resources)) {
85                 dev_dbg(&dev->dev, "%s: no current resources\n", desc);
86                 return;
87         }
88
89         dev_dbg(&dev->dev, "%s: current resources:\n", desc);
90         list_for_each_entry(pnp_res, &dev->resources, list) {
91                 res = &pnp_res->res;
92
93                 len += snprintf(buf + len, sizeof(buf) - len, "  %-3s ",
94                                 pnp_resource_type_name(res));
95
96                 if (res->flags & IORESOURCE_DISABLED) {
97                         dev_dbg(&dev->dev, "%sdisabled\n", buf);
98                         continue;
99                 }
100
101                 switch (pnp_resource_type(res)) {
102                 case IORESOURCE_IO:
103                 case IORESOURCE_MEM:
104                         len += snprintf(buf + len, sizeof(buf) - len,
105                                         "%#llx-%#llx flags %#lx",
106                                         (unsigned long long) res->start,
107                                         (unsigned long long) res->end,
108                                         res->flags);
109                         break;
110                 case IORESOURCE_IRQ:
111                 case IORESOURCE_DMA:
112                         len += snprintf(buf + len, sizeof(buf) - len,
113                                         "%lld flags %#lx",
114                                         (unsigned long long) res->start,
115                                         res->flags);
116                         break;
117                 }
118                 dev_dbg(&dev->dev, "%s\n", buf);
119         }
120 #endif
121 }
122
123 char *pnp_option_priority_name(struct pnp_option *option)
124 {
125         switch (pnp_option_priority(option)) {
126         case PNP_RES_PRIORITY_PREFERRED:
127                 return "preferred";
128         case PNP_RES_PRIORITY_ACCEPTABLE:
129                 return "acceptable";
130         case PNP_RES_PRIORITY_FUNCTIONAL:
131                 return "functional";
132         }
133         return "invalid";
134 }
135
136 void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option)
137 {
138 #ifdef DEBUG
139         char buf[128];
140         int len = 0, i;
141         struct pnp_port *port;
142         struct pnp_mem *mem;
143         struct pnp_irq *irq;
144         struct pnp_dma *dma;
145
146         if (pnp_option_is_dependent(option))
147                 len += snprintf(buf + len, sizeof(buf) - len,
148                                 "  dependent set %d (%s) ",
149                                 pnp_option_set(option),
150                                 pnp_option_priority_name(option));
151         else
152                 len += snprintf(buf + len, sizeof(buf) - len, "  independent ");
153
154         switch (option->type) {
155         case IORESOURCE_IO:
156                 port = &option->u.port;
157                 len += snprintf(buf + len, sizeof(buf) - len, "io  min %#llx "
158                                 "max %#llx align %lld size %lld flags %#x",
159                                 (unsigned long long) port->min,
160                                 (unsigned long long) port->max,
161                                 (unsigned long long) port->align,
162                                 (unsigned long long) port->size, port->flags);
163                 break;
164         case IORESOURCE_MEM:
165                 mem = &option->u.mem;
166                 len += snprintf(buf + len, sizeof(buf) - len, "mem min %#llx "
167                                 "max %#llx align %lld size %lld flags %#x",
168                                 (unsigned long long) mem->min,
169                                 (unsigned long long) mem->max,
170                                 (unsigned long long) mem->align,
171                                 (unsigned long long) mem->size, mem->flags);
172                 break;
173         case IORESOURCE_IRQ:
174                 irq = &option->u.irq;
175                 len += snprintf(buf + len, sizeof(buf) - len, "irq");
176                 if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
177                         len += snprintf(buf + len, sizeof(buf) - len,
178                                         " <none>");
179                 else {
180                         for (i = 0; i < PNP_IRQ_NR; i++)
181                                 if (test_bit(i, irq->map.bits))
182                                         len += snprintf(buf + len,
183                                                         sizeof(buf) - len,
184                                                         " %d", i);
185                 }
186                 len += snprintf(buf + len, sizeof(buf) - len, " flags %#x",
187                                 irq->flags);
188                 if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
189                         len += snprintf(buf + len, sizeof(buf) - len,
190                                         " (optional)");
191                 break;
192         case IORESOURCE_DMA:
193                 dma = &option->u.dma;
194                 len += snprintf(buf + len, sizeof(buf) - len, "dma");
195                 if (!dma->map)
196                         len += snprintf(buf + len, sizeof(buf) - len,
197                                         " <none>");
198                 else {
199                         for (i = 0; i < 8; i++)
200                                 if (dma->map & (1 << i))
201                                         len += snprintf(buf + len,
202                                                         sizeof(buf) - len,
203                                                         " %d", i);
204                 }
205                 len += snprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) "
206                                 "flags %#x", dma->map, dma->flags);
207                 break;
208         }
209         dev_dbg(&dev->dev, "%s\n", buf);
210 #endif
211 }