blob: 9585c1c1cc3669783653deb6841d14fe0c10b292 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -07002 * support.c - standard functions for the use of pnp protocol drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -06005 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
6 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/pnp.h>
12#include "base.h"
13
14/**
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070015 * pnp_is_active - Determines if a device is active based on its current
16 * resources
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * @dev: pointer to the desired PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070019int pnp_is_active(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060021 /*
22 * I don't think this is very reliable because pnp_disable_dev()
23 * only clears out auto-assigned resources.
24 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 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 Helgaas9dd78462007-07-26 10:41:20 -070027 pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1)
28 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 else
30 return 1;
31}
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033EXPORT_SYMBOL(pnp_is_active);
Bjorn Helgaas25eb8462008-04-28 16:33:53 -060034
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 */
39void 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 Harrison3fc95772008-05-14 16:05:49 -070054 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 Helgaas25eb8462008-04-28 16:33:53 -060058 str[7] = '\0';
59}
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060060
Bjorn Helgaas9fdee4e2008-06-27 16:56:55 -060061char *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 Helgaas81b5c752008-04-28 16:34:08 -060076void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc)
77{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060078 struct pnp_resource *pnp_res;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060079
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060080 if (list_empty(&dev->resources))
Bjorn Helgaas2f534322008-08-19 16:53:47 -060081 pnp_dbg(&dev->dev, "%s: no current resources\n", desc);
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060082 else {
83 pnp_dbg(&dev->dev, "%s: current resources:\n", desc);
84 list_for_each_entry(pnp_res, &dev->resources, list)
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -060085 pnp_dbg(&dev->dev, "%pr\n", &pnp_res->res);
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060086 }
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060087}
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060088
89char *pnp_option_priority_name(struct pnp_option *option)
90{
91 switch (pnp_option_priority(option)) {
92 case PNP_RES_PRIORITY_PREFERRED:
93 return "preferred";
94 case PNP_RES_PRIORITY_ACCEPTABLE:
95 return "acceptable";
96 case PNP_RES_PRIORITY_FUNCTIONAL:
97 return "functional";
98 }
99 return "invalid";
100}
101
102void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option)
103{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600104 char buf[128];
105 int len = 0, i;
106 struct pnp_port *port;
107 struct pnp_mem *mem;
108 struct pnp_irq *irq;
109 struct pnp_dma *dma;
110
111 if (pnp_option_is_dependent(option))
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700112 len += scnprintf(buf + len, sizeof(buf) - len,
113 " dependent set %d (%s) ",
114 pnp_option_set(option),
115 pnp_option_priority_name(option));
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600116 else
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700117 len += scnprintf(buf + len, sizeof(buf) - len,
118 " independent ");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600119
120 switch (option->type) {
121 case IORESOURCE_IO:
122 port = &option->u.port;
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700123 len += scnprintf(buf + len, sizeof(buf) - len, "io min %#llx "
124 "max %#llx align %lld size %lld flags %#x",
125 (unsigned long long) port->min,
126 (unsigned long long) port->max,
127 (unsigned long long) port->align,
128 (unsigned long long) port->size, port->flags);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600129 break;
130 case IORESOURCE_MEM:
131 mem = &option->u.mem;
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700132 len += scnprintf(buf + len, sizeof(buf) - len, "mem min %#llx "
133 "max %#llx align %lld size %lld flags %#x",
134 (unsigned long long) mem->min,
135 (unsigned long long) mem->max,
136 (unsigned long long) mem->align,
137 (unsigned long long) mem->size, mem->flags);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600138 break;
139 case IORESOURCE_IRQ:
140 irq = &option->u.irq;
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700141 len += scnprintf(buf + len, sizeof(buf) - len, "irq");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600142 if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700143 len += scnprintf(buf + len, sizeof(buf) - len,
144 " <none>");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600145 else {
146 for (i = 0; i < PNP_IRQ_NR; i++)
147 if (test_bit(i, irq->map.bits))
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700148 len += scnprintf(buf + len,
149 sizeof(buf) - len,
150 " %d", i);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600151 }
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700152 len += scnprintf(buf + len, sizeof(buf) - len, " flags %#x",
153 irq->flags);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600154 if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700155 len += scnprintf(buf + len, sizeof(buf) - len,
156 " (optional)");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600157 break;
158 case IORESOURCE_DMA:
159 dma = &option->u.dma;
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700160 len += scnprintf(buf + len, sizeof(buf) - len, "dma");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600161 if (!dma->map)
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700162 len += scnprintf(buf + len, sizeof(buf) - len,
163 " <none>");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600164 else {
165 for (i = 0; i < 8; i++)
166 if (dma->map & (1 << i))
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700167 len += scnprintf(buf + len,
168 sizeof(buf) - len,
169 " %d", i);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600170 }
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700171 len += scnprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) "
172 "flags %#x", dma->map, dma->flags);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600173 break;
174 }
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600175 pnp_dbg(&dev->dev, "%s\n", buf);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600176}