Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 2 | * support.c - standard functions for the use of pnp protocol drivers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2003 Adam Belay <ambx1@neo.rr.com> |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 5 | * Copyright (C) 2008 Hewlett-Packard Development Company, L.P. |
| 6 | * Bjorn Helgaas <bjorn.helgaas@hp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/pnp.h> |
| 12 | #include "base.h" |
| 13 | |
| 14 | /** |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 15 | * pnp_is_active - Determines if a device is active based on its current |
| 16 | * resources |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * @dev: pointer to the desired PnP device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 19 | int pnp_is_active(struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 21 | /* |
| 22 | * I don't think this is very reliable because pnp_disable_dev() |
| 23 | * only clears out auto-assigned resources. |
| 24 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 && |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 27 | pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1) |
| 28 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | else |
| 30 | return 1; |
| 31 | } |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | EXPORT_SYMBOL(pnp_is_active); |
Bjorn Helgaas | 25eb846 | 2008-04-28 16:33:53 -0600 | [diff] [blame] | 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; |
Harvey Harrison | 3fc9577 | 2008-05-14 16:05:49 -0700 | [diff] [blame] | 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); |
Bjorn Helgaas | 25eb846 | 2008-04-28 16:33:53 -0600 | [diff] [blame] | 58 | str[7] = '\0'; |
| 59 | } |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 60 | |
Bjorn Helgaas | 9fdee4e | 2008-06-27 16:56:55 -0600 | [diff] [blame] | 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 | |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 76 | void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc) |
| 77 | { |
| 78 | #ifdef DEBUG |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 79 | char buf[128]; |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 80 | int len; |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 81 | struct pnp_resource *pnp_res; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 82 | struct resource *res; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 83 | |
Bjorn Helgaas | 819beac | 2008-06-27 16:57:08 -0600 | [diff] [blame] | 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); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 90 | list_for_each_entry(pnp_res, &dev->resources, list) { |
| 91 | res = &pnp_res->res; |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 92 | len = 0; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 93 | |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 94 | len += scnprintf(buf + len, sizeof(buf) - len, " %-3s ", |
| 95 | pnp_resource_type_name(res)); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 96 | |
| 97 | if (res->flags & IORESOURCE_DISABLED) { |
| 98 | dev_dbg(&dev->dev, "%sdisabled\n", buf); |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | switch (pnp_resource_type(res)) { |
| 103 | case IORESOURCE_IO: |
| 104 | case IORESOURCE_MEM: |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 105 | len += scnprintf(buf + len, sizeof(buf) - len, |
| 106 | "%#llx-%#llx flags %#lx", |
| 107 | (unsigned long long) res->start, |
| 108 | (unsigned long long) res->end, |
| 109 | res->flags); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 110 | break; |
| 111 | case IORESOURCE_IRQ: |
| 112 | case IORESOURCE_DMA: |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 113 | len += scnprintf(buf + len, sizeof(buf) - len, |
| 114 | "%lld flags %#lx", |
| 115 | (unsigned long long) res->start, |
| 116 | res->flags); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 117 | break; |
| 118 | } |
| 119 | dev_dbg(&dev->dev, "%s\n", buf); |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 120 | } |
| 121 | #endif |
| 122 | } |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 123 | |
| 124 | char *pnp_option_priority_name(struct pnp_option *option) |
| 125 | { |
| 126 | switch (pnp_option_priority(option)) { |
| 127 | case PNP_RES_PRIORITY_PREFERRED: |
| 128 | return "preferred"; |
| 129 | case PNP_RES_PRIORITY_ACCEPTABLE: |
| 130 | return "acceptable"; |
| 131 | case PNP_RES_PRIORITY_FUNCTIONAL: |
| 132 | return "functional"; |
| 133 | } |
| 134 | return "invalid"; |
| 135 | } |
| 136 | |
| 137 | void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option) |
| 138 | { |
| 139 | #ifdef DEBUG |
| 140 | char buf[128]; |
| 141 | int len = 0, i; |
| 142 | struct pnp_port *port; |
| 143 | struct pnp_mem *mem; |
| 144 | struct pnp_irq *irq; |
| 145 | struct pnp_dma *dma; |
| 146 | |
| 147 | if (pnp_option_is_dependent(option)) |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 148 | len += scnprintf(buf + len, sizeof(buf) - len, |
| 149 | " dependent set %d (%s) ", |
| 150 | pnp_option_set(option), |
| 151 | pnp_option_priority_name(option)); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 152 | else |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 153 | len += scnprintf(buf + len, sizeof(buf) - len, |
| 154 | " independent "); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 155 | |
| 156 | switch (option->type) { |
| 157 | case IORESOURCE_IO: |
| 158 | port = &option->u.port; |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 159 | len += scnprintf(buf + len, sizeof(buf) - len, "io min %#llx " |
| 160 | "max %#llx align %lld size %lld flags %#x", |
| 161 | (unsigned long long) port->min, |
| 162 | (unsigned long long) port->max, |
| 163 | (unsigned long long) port->align, |
| 164 | (unsigned long long) port->size, port->flags); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 165 | break; |
| 166 | case IORESOURCE_MEM: |
| 167 | mem = &option->u.mem; |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 168 | len += scnprintf(buf + len, sizeof(buf) - len, "mem min %#llx " |
| 169 | "max %#llx align %lld size %lld flags %#x", |
| 170 | (unsigned long long) mem->min, |
| 171 | (unsigned long long) mem->max, |
| 172 | (unsigned long long) mem->align, |
| 173 | (unsigned long long) mem->size, mem->flags); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 174 | break; |
| 175 | case IORESOURCE_IRQ: |
| 176 | irq = &option->u.irq; |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 177 | len += scnprintf(buf + len, sizeof(buf) - len, "irq"); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 178 | if (bitmap_empty(irq->map.bits, PNP_IRQ_NR)) |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 179 | len += scnprintf(buf + len, sizeof(buf) - len, |
| 180 | " <none>"); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 181 | else { |
| 182 | for (i = 0; i < PNP_IRQ_NR; i++) |
| 183 | if (test_bit(i, irq->map.bits)) |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 184 | len += scnprintf(buf + len, |
| 185 | sizeof(buf) - len, |
| 186 | " %d", i); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 187 | } |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 188 | len += scnprintf(buf + len, sizeof(buf) - len, " flags %#x", |
| 189 | irq->flags); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 190 | if (irq->flags & IORESOURCE_IRQ_OPTIONAL) |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 191 | len += scnprintf(buf + len, sizeof(buf) - len, |
| 192 | " (optional)"); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 193 | break; |
| 194 | case IORESOURCE_DMA: |
| 195 | dma = &option->u.dma; |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 196 | len += scnprintf(buf + len, sizeof(buf) - len, "dma"); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 197 | if (!dma->map) |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 198 | len += scnprintf(buf + len, sizeof(buf) - len, |
| 199 | " <none>"); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 200 | else { |
| 201 | for (i = 0; i < 8; i++) |
| 202 | if (dma->map & (1 << i)) |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 203 | len += scnprintf(buf + len, |
| 204 | sizeof(buf) - len, |
| 205 | " %d", i); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 206 | } |
Bjorn Helgaas | ea44c1d | 2008-07-31 00:07:27 -0700 | [diff] [blame] | 207 | len += scnprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) " |
| 208 | "flags %#x", dma->map, dma->flags); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | dev_dbg(&dev->dev, "%s\n", buf); |
| 212 | #endif |
| 213 | } |