xref: /linux/drivers/pnp/support.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
307d4e9afSBjorn Helgaas  * support.c - standard functions for the use of pnp protocol drivers
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
61f32ca31SBjorn Helgaas  * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
71f32ca31SBjorn Helgaas  *	Bjorn Helgaas <bjorn.helgaas@hp.com>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/module.h>
111da177e4SLinus Torvalds #include <linux/ctype.h>
121da177e4SLinus Torvalds #include <linux/pnp.h>
131da177e4SLinus Torvalds #include "base.h"
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /**
1607d4e9afSBjorn Helgaas  * pnp_is_active - Determines if a device is active based on its current
1707d4e9afSBjorn Helgaas  *	resources
181da177e4SLinus Torvalds  * @dev: pointer to the desired PnP device
191da177e4SLinus Torvalds  */
pnp_is_active(struct pnp_dev * dev)201da177e4SLinus Torvalds int pnp_is_active(struct pnp_dev *dev)
211da177e4SLinus Torvalds {
22aee3ad81SBjorn Helgaas 	/*
23aee3ad81SBjorn Helgaas 	 * I don't think this is very reliable because pnp_disable_dev()
24aee3ad81SBjorn Helgaas 	 * only clears out auto-assigned resources.
25aee3ad81SBjorn Helgaas 	 */
261da177e4SLinus Torvalds 	if (!pnp_port_start(dev, 0) && pnp_port_len(dev, 0) <= 1 &&
271da177e4SLinus Torvalds 	    !pnp_mem_start(dev, 0) && pnp_mem_len(dev, 0) <= 1 &&
289dd78466SBjorn Helgaas 	    pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1)
291da177e4SLinus Torvalds 		return 0;
301da177e4SLinus Torvalds 	else
311da177e4SLinus Torvalds 		return 1;
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds EXPORT_SYMBOL(pnp_is_active);
3425eb8461SBjorn Helgaas 
3525eb8461SBjorn Helgaas /*
3625eb8461SBjorn Helgaas  * Functionally similar to acpi_ex_eisa_id_to_string(), but that's
3725eb8461SBjorn Helgaas  * buried in the ACPI CA, and we can't depend on it being present.
3825eb8461SBjorn Helgaas  */
pnp_eisa_id_to_string(u32 id,char * str)3925eb8461SBjorn Helgaas void pnp_eisa_id_to_string(u32 id, char *str)
4025eb8461SBjorn Helgaas {
4125eb8461SBjorn Helgaas 	id = be32_to_cpu(id);
4225eb8461SBjorn Helgaas 
4325eb8461SBjorn Helgaas 	/*
4425eb8461SBjorn Helgaas 	 * According to the specs, the first three characters are five-bit
4525eb8461SBjorn Helgaas 	 * compressed ASCII, and the left-over high order bit should be zero.
4625eb8461SBjorn Helgaas 	 * However, the Linux ISAPNP code historically used six bits for the
4725eb8461SBjorn Helgaas 	 * first character, and there seem to be IDs that depend on that,
4825eb8461SBjorn Helgaas 	 * e.g., "nEC8241" in the Linux 8250_pnp serial driver and the
4925eb8461SBjorn Helgaas 	 * FreeBSD sys/pc98/cbus/sio_cbus.c driver.
5025eb8461SBjorn Helgaas 	 */
5125eb8461SBjorn Helgaas 	str[0] = 'A' + ((id >> 26) & 0x3f) - 1;
5225eb8461SBjorn Helgaas 	str[1] = 'A' + ((id >> 21) & 0x1f) - 1;
5325eb8461SBjorn Helgaas 	str[2] = 'A' + ((id >> 16) & 0x1f) - 1;
543fc95772SHarvey Harrison 	str[3] = hex_asc_hi(id >> 8);
553fc95772SHarvey Harrison 	str[4] = hex_asc_lo(id >> 8);
563fc95772SHarvey Harrison 	str[5] = hex_asc_hi(id);
573fc95772SHarvey Harrison 	str[6] = hex_asc_lo(id);
5825eb8461SBjorn Helgaas 	str[7] = '\0';
5925eb8461SBjorn Helgaas }
6081b5c75fSBjorn Helgaas 
pnp_resource_type_name(struct resource * res)619fdee4e0SBjorn Helgaas char *pnp_resource_type_name(struct resource *res)
629fdee4e0SBjorn Helgaas {
639fdee4e0SBjorn Helgaas 	switch (pnp_resource_type(res)) {
649fdee4e0SBjorn Helgaas 	case IORESOURCE_IO:
659fdee4e0SBjorn Helgaas 		return "io";
669fdee4e0SBjorn Helgaas 	case IORESOURCE_MEM:
679fdee4e0SBjorn Helgaas 		return "mem";
689fdee4e0SBjorn Helgaas 	case IORESOURCE_IRQ:
699fdee4e0SBjorn Helgaas 		return "irq";
709fdee4e0SBjorn Helgaas 	case IORESOURCE_DMA:
719fdee4e0SBjorn Helgaas 		return "dma";
727e0e9c04SBjorn Helgaas 	case IORESOURCE_BUS:
737e0e9c04SBjorn Helgaas 		return "bus";
749fdee4e0SBjorn Helgaas 	}
757e0e9c04SBjorn Helgaas 	return "unknown";
769fdee4e0SBjorn Helgaas }
779fdee4e0SBjorn Helgaas 
dbg_pnp_show_resources(struct pnp_dev * dev,char * desc)7881b5c75fSBjorn Helgaas void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc)
7981b5c75fSBjorn Helgaas {
80aee3ad81SBjorn Helgaas 	struct pnp_resource *pnp_res;
8181b5c75fSBjorn Helgaas 
829a007b37SBjorn Helgaas 	if (list_empty(&dev->resources))
832f53432cSBjorn Helgaas 		pnp_dbg(&dev->dev, "%s: no current resources\n", desc);
849a007b37SBjorn Helgaas 	else {
852f53432cSBjorn Helgaas 		pnp_dbg(&dev->dev, "%s: current resources:\n", desc);
869a007b37SBjorn Helgaas 		list_for_each_entry(pnp_res, &dev->resources, list)
87c7dabef8SBjorn Helgaas 			pnp_dbg(&dev->dev, "%pr\n", &pnp_res->res);
8881b5c75fSBjorn Helgaas 	}
8981b5c75fSBjorn Helgaas }
901f32ca31SBjorn Helgaas 
pnp_option_priority_name(struct pnp_option * option)911f32ca31SBjorn Helgaas char *pnp_option_priority_name(struct pnp_option *option)
921f32ca31SBjorn Helgaas {
931f32ca31SBjorn Helgaas 	switch (pnp_option_priority(option)) {
941f32ca31SBjorn Helgaas 	case PNP_RES_PRIORITY_PREFERRED:
951f32ca31SBjorn Helgaas 		return "preferred";
961f32ca31SBjorn Helgaas 	case PNP_RES_PRIORITY_ACCEPTABLE:
971f32ca31SBjorn Helgaas 		return "acceptable";
981f32ca31SBjorn Helgaas 	case PNP_RES_PRIORITY_FUNCTIONAL:
991f32ca31SBjorn Helgaas 		return "functional";
1001f32ca31SBjorn Helgaas 	}
1011f32ca31SBjorn Helgaas 	return "invalid";
1021f32ca31SBjorn Helgaas }
1031f32ca31SBjorn Helgaas 
dbg_pnp_show_option(struct pnp_dev * dev,struct pnp_option * option)1041f32ca31SBjorn Helgaas void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option)
1051f32ca31SBjorn Helgaas {
1061f32ca31SBjorn Helgaas 	char buf[128];
1071f32ca31SBjorn Helgaas 	int len = 0, i;
1081f32ca31SBjorn Helgaas 	struct pnp_port *port;
1091f32ca31SBjorn Helgaas 	struct pnp_mem *mem;
1101f32ca31SBjorn Helgaas 	struct pnp_irq *irq;
1111f32ca31SBjorn Helgaas 	struct pnp_dma *dma;
1121f32ca31SBjorn Helgaas 
1131f32ca31SBjorn Helgaas 	if (pnp_option_is_dependent(option))
114ea44c1d6SBjorn Helgaas 		len += scnprintf(buf + len, sizeof(buf) - len,
1151f32ca31SBjorn Helgaas 				 "  dependent set %d (%s) ",
1161f32ca31SBjorn Helgaas 				 pnp_option_set(option),
1171f32ca31SBjorn Helgaas 				 pnp_option_priority_name(option));
1181f32ca31SBjorn Helgaas 	else
119ea44c1d6SBjorn Helgaas 		len += scnprintf(buf + len, sizeof(buf) - len,
120ea44c1d6SBjorn Helgaas 				 "  independent ");
1211f32ca31SBjorn Helgaas 
1221f32ca31SBjorn Helgaas 	switch (option->type) {
1231f32ca31SBjorn Helgaas 	case IORESOURCE_IO:
1241f32ca31SBjorn Helgaas 		port = &option->u.port;
125ea44c1d6SBjorn Helgaas 		len += scnprintf(buf + len, sizeof(buf) - len, "io  min %#llx "
1261f32ca31SBjorn Helgaas 				 "max %#llx align %lld size %lld flags %#x",
1271f32ca31SBjorn Helgaas 				 (unsigned long long) port->min,
1281f32ca31SBjorn Helgaas 				 (unsigned long long) port->max,
1291f32ca31SBjorn Helgaas 				 (unsigned long long) port->align,
1301f32ca31SBjorn Helgaas 				 (unsigned long long) port->size, port->flags);
1311f32ca31SBjorn Helgaas 		break;
1321f32ca31SBjorn Helgaas 	case IORESOURCE_MEM:
1331f32ca31SBjorn Helgaas 		mem = &option->u.mem;
134ea44c1d6SBjorn Helgaas 		len += scnprintf(buf + len, sizeof(buf) - len, "mem min %#llx "
1351f32ca31SBjorn Helgaas 				 "max %#llx align %lld size %lld flags %#x",
1361f32ca31SBjorn Helgaas 				 (unsigned long long) mem->min,
1371f32ca31SBjorn Helgaas 				 (unsigned long long) mem->max,
1381f32ca31SBjorn Helgaas 				 (unsigned long long) mem->align,
1391f32ca31SBjorn Helgaas 				 (unsigned long long) mem->size, mem->flags);
1401f32ca31SBjorn Helgaas 		break;
1411f32ca31SBjorn Helgaas 	case IORESOURCE_IRQ:
1421f32ca31SBjorn Helgaas 		irq = &option->u.irq;
143ea44c1d6SBjorn Helgaas 		len += scnprintf(buf + len, sizeof(buf) - len, "irq");
1441f32ca31SBjorn Helgaas 		if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
145ea44c1d6SBjorn Helgaas 			len += scnprintf(buf + len, sizeof(buf) - len,
1461f32ca31SBjorn Helgaas 					 " <none>");
1471f32ca31SBjorn Helgaas 		else {
1481f32ca31SBjorn Helgaas 			for (i = 0; i < PNP_IRQ_NR; i++)
1491f32ca31SBjorn Helgaas 				if (test_bit(i, irq->map.bits))
150ea44c1d6SBjorn Helgaas 					len += scnprintf(buf + len,
1511f32ca31SBjorn Helgaas 							 sizeof(buf) - len,
1521f32ca31SBjorn Helgaas 							 " %d", i);
1531f32ca31SBjorn Helgaas 		}
154ea44c1d6SBjorn Helgaas 		len += scnprintf(buf + len, sizeof(buf) - len, " flags %#x",
1551f32ca31SBjorn Helgaas 				 irq->flags);
1561f32ca31SBjorn Helgaas 		if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
157ea44c1d6SBjorn Helgaas 			len += scnprintf(buf + len, sizeof(buf) - len,
1581f32ca31SBjorn Helgaas 					 " (optional)");
1591f32ca31SBjorn Helgaas 		break;
1601f32ca31SBjorn Helgaas 	case IORESOURCE_DMA:
1611f32ca31SBjorn Helgaas 		dma = &option->u.dma;
162ea44c1d6SBjorn Helgaas 		len += scnprintf(buf + len, sizeof(buf) - len, "dma");
1631f32ca31SBjorn Helgaas 		if (!dma->map)
164ea44c1d6SBjorn Helgaas 			len += scnprintf(buf + len, sizeof(buf) - len,
1651f32ca31SBjorn Helgaas 					 " <none>");
1661f32ca31SBjorn Helgaas 		else {
1671f32ca31SBjorn Helgaas 			for (i = 0; i < 8; i++)
1681f32ca31SBjorn Helgaas 				if (dma->map & (1 << i))
169ea44c1d6SBjorn Helgaas 					len += scnprintf(buf + len,
1701f32ca31SBjorn Helgaas 							 sizeof(buf) - len,
1711f32ca31SBjorn Helgaas 							 " %d", i);
1721f32ca31SBjorn Helgaas 		}
173ea44c1d6SBjorn Helgaas 		len += scnprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) "
1741f32ca31SBjorn Helgaas 				 "flags %#x", dma->map, dma->flags);
1751f32ca31SBjorn Helgaas 		break;
1761f32ca31SBjorn Helgaas 	}
1772f53432cSBjorn Helgaas 	pnp_dbg(&dev->dev, "%s\n", buf);
1781f32ca31SBjorn Helgaas }
179