blob: e4f53d31191de35a44b3964fff4b1dcf5b486833 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -07003 * support.c - standard functions for the use of pnp protocol drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -06006 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/pnp.h>
13#include "base.h"
14
15/**
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070016 * pnp_is_active - Determines if a device is active based on its current
17 * resources
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * @dev: pointer to the desired PnP device
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070020int pnp_is_active(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060022 /*
23 * I don't think this is very reliable because pnp_disable_dev()
24 * only clears out auto-assigned resources.
25 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 if (!pnp_port_start(dev, 0) && pnp_port_len(dev, 0) <= 1 &&
27 !pnp_mem_start(dev, 0) && pnp_mem_len(dev, 0) <= 1 &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070028 pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1)
29 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 else
31 return 1;
32}
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034EXPORT_SYMBOL(pnp_is_active);
Bjorn Helgaas25eb8462008-04-28 16:33:53 -060035
36/*
37 * Functionally similar to acpi_ex_eisa_id_to_string(), but that's
38 * buried in the ACPI CA, and we can't depend on it being present.
39 */
40void pnp_eisa_id_to_string(u32 id, char *str)
41{
42 id = be32_to_cpu(id);
43
44 /*
45 * According to the specs, the first three characters are five-bit
46 * compressed ASCII, and the left-over high order bit should be zero.
47 * However, the Linux ISAPNP code historically used six bits for the
48 * first character, and there seem to be IDs that depend on that,
49 * e.g., "nEC8241" in the Linux 8250_pnp serial driver and the
50 * FreeBSD sys/pc98/cbus/sio_cbus.c driver.
51 */
52 str[0] = 'A' + ((id >> 26) & 0x3f) - 1;
53 str[1] = 'A' + ((id >> 21) & 0x1f) - 1;
54 str[2] = 'A' + ((id >> 16) & 0x1f) - 1;
Harvey Harrison3fc95772008-05-14 16:05:49 -070055 str[3] = hex_asc_hi(id >> 8);
56 str[4] = hex_asc_lo(id >> 8);
57 str[5] = hex_asc_hi(id);
58 str[6] = hex_asc_lo(id);
Bjorn Helgaas25eb8462008-04-28 16:33:53 -060059 str[7] = '\0';
60}
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060061
Bjorn Helgaas9fdee4e2008-06-27 16:56:55 -060062char *pnp_resource_type_name(struct resource *res)
63{
64 switch (pnp_resource_type(res)) {
65 case IORESOURCE_IO:
66 return "io";
67 case IORESOURCE_MEM:
68 return "mem";
69 case IORESOURCE_IRQ:
70 return "irq";
71 case IORESOURCE_DMA:
72 return "dma";
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -070073 case IORESOURCE_BUS:
74 return "bus";
Bjorn Helgaas9fdee4e2008-06-27 16:56:55 -060075 }
Bjorn Helgaas7e0e9c02010-03-05 10:47:57 -070076 return "unknown";
Bjorn Helgaas9fdee4e2008-06-27 16:56:55 -060077}
78
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060079void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc)
80{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060081 struct pnp_resource *pnp_res;
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060082
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060083 if (list_empty(&dev->resources))
Bjorn Helgaas2f534322008-08-19 16:53:47 -060084 pnp_dbg(&dev->dev, "%s: no current resources\n", desc);
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060085 else {
86 pnp_dbg(&dev->dev, "%s: current resources:\n", desc);
87 list_for_each_entry(pnp_res, &dev->resources, list)
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -060088 pnp_dbg(&dev->dev, "%pr\n", &pnp_res->res);
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060089 }
Bjorn Helgaas81b5c752008-04-28 16:34:08 -060090}
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -060091
92char *pnp_option_priority_name(struct pnp_option *option)
93{
94 switch (pnp_option_priority(option)) {
95 case PNP_RES_PRIORITY_PREFERRED:
96 return "preferred";
97 case PNP_RES_PRIORITY_ACCEPTABLE:
98 return "acceptable";
99 case PNP_RES_PRIORITY_FUNCTIONAL:
100 return "functional";
101 }
102 return "invalid";
103}
104
105void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option)
106{
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600107 char buf[128];
108 int len = 0, i;
109 struct pnp_port *port;
110 struct pnp_mem *mem;
111 struct pnp_irq *irq;
112 struct pnp_dma *dma;
113
114 if (pnp_option_is_dependent(option))
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700115 len += scnprintf(buf + len, sizeof(buf) - len,
116 " dependent set %d (%s) ",
117 pnp_option_set(option),
118 pnp_option_priority_name(option));
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600119 else
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700120 len += scnprintf(buf + len, sizeof(buf) - len,
121 " independent ");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600122
123 switch (option->type) {
124 case IORESOURCE_IO:
125 port = &option->u.port;
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700126 len += scnprintf(buf + len, sizeof(buf) - len, "io min %#llx "
127 "max %#llx align %lld size %lld flags %#x",
128 (unsigned long long) port->min,
129 (unsigned long long) port->max,
130 (unsigned long long) port->align,
131 (unsigned long long) port->size, port->flags);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600132 break;
133 case IORESOURCE_MEM:
134 mem = &option->u.mem;
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700135 len += scnprintf(buf + len, sizeof(buf) - len, "mem min %#llx "
136 "max %#llx align %lld size %lld flags %#x",
137 (unsigned long long) mem->min,
138 (unsigned long long) mem->max,
139 (unsigned long long) mem->align,
140 (unsigned long long) mem->size, mem->flags);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600141 break;
142 case IORESOURCE_IRQ:
143 irq = &option->u.irq;
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700144 len += scnprintf(buf + len, sizeof(buf) - len, "irq");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600145 if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700146 len += scnprintf(buf + len, sizeof(buf) - len,
147 " <none>");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600148 else {
149 for (i = 0; i < PNP_IRQ_NR; i++)
150 if (test_bit(i, irq->map.bits))
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700151 len += scnprintf(buf + len,
152 sizeof(buf) - len,
153 " %d", i);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600154 }
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700155 len += scnprintf(buf + len, sizeof(buf) - len, " flags %#x",
156 irq->flags);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600157 if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700158 len += scnprintf(buf + len, sizeof(buf) - len,
159 " (optional)");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600160 break;
161 case IORESOURCE_DMA:
162 dma = &option->u.dma;
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700163 len += scnprintf(buf + len, sizeof(buf) - len, "dma");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600164 if (!dma->map)
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700165 len += scnprintf(buf + len, sizeof(buf) - len,
166 " <none>");
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600167 else {
168 for (i = 0; i < 8; i++)
169 if (dma->map & (1 << i))
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700170 len += scnprintf(buf + len,
171 sizeof(buf) - len,
172 " %d", i);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600173 }
Bjorn Helgaasea44c1d2008-07-31 00:07:27 -0700174 len += scnprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) "
175 "flags %#x", dma->map, dma->flags);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600176 break;
177 }
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600178 pnp_dbg(&dev->dev, "%s\n", buf);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600179}