Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * support.c - standard functions for the use of pnp protocol drivers
0004  *
0005  * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
0006  * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
0007  *  Bjorn Helgaas <bjorn.helgaas@hp.com>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/ctype.h>
0012 #include <linux/pnp.h>
0013 #include "base.h"
0014 
0015 /**
0016  * pnp_is_active - Determines if a device is active based on its current
0017  *  resources
0018  * @dev: pointer to the desired PnP device
0019  */
0020 int pnp_is_active(struct pnp_dev *dev)
0021 {
0022     /*
0023      * I don't think this is very reliable because pnp_disable_dev()
0024      * only clears out auto-assigned resources.
0025      */
0026     if (!pnp_port_start(dev, 0) && pnp_port_len(dev, 0) <= 1 &&
0027         !pnp_mem_start(dev, 0) && pnp_mem_len(dev, 0) <= 1 &&
0028         pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1)
0029         return 0;
0030     else
0031         return 1;
0032 }
0033 EXPORT_SYMBOL(pnp_is_active);
0034 
0035 /*
0036  * Functionally similar to acpi_ex_eisa_id_to_string(), but that's
0037  * buried in the ACPI CA, and we can't depend on it being present.
0038  */
0039 void pnp_eisa_id_to_string(u32 id, char *str)
0040 {
0041     id = be32_to_cpu(id);
0042 
0043     /*
0044      * According to the specs, the first three characters are five-bit
0045      * compressed ASCII, and the left-over high order bit should be zero.
0046      * However, the Linux ISAPNP code historically used six bits for the
0047      * first character, and there seem to be IDs that depend on that,
0048      * e.g., "nEC8241" in the Linux 8250_pnp serial driver and the
0049      * FreeBSD sys/pc98/cbus/sio_cbus.c driver.
0050      */
0051     str[0] = 'A' + ((id >> 26) & 0x3f) - 1;
0052     str[1] = 'A' + ((id >> 21) & 0x1f) - 1;
0053     str[2] = 'A' + ((id >> 16) & 0x1f) - 1;
0054     str[3] = hex_asc_hi(id >> 8);
0055     str[4] = hex_asc_lo(id >> 8);
0056     str[5] = hex_asc_hi(id);
0057     str[6] = hex_asc_lo(id);
0058     str[7] = '\0';
0059 }
0060 
0061 char *pnp_resource_type_name(struct resource *res)
0062 {
0063     switch (pnp_resource_type(res)) {
0064     case IORESOURCE_IO:
0065         return "io";
0066     case IORESOURCE_MEM:
0067         return "mem";
0068     case IORESOURCE_IRQ:
0069         return "irq";
0070     case IORESOURCE_DMA:
0071         return "dma";
0072     case IORESOURCE_BUS:
0073         return "bus";
0074     }
0075     return "unknown";
0076 }
0077 
0078 void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc)
0079 {
0080     struct pnp_resource *pnp_res;
0081 
0082     if (list_empty(&dev->resources))
0083         pnp_dbg(&dev->dev, "%s: no current resources\n", desc);
0084     else {
0085         pnp_dbg(&dev->dev, "%s: current resources:\n", desc);
0086         list_for_each_entry(pnp_res, &dev->resources, list)
0087             pnp_dbg(&dev->dev, "%pr\n", &pnp_res->res);
0088     }
0089 }
0090 
0091 char *pnp_option_priority_name(struct pnp_option *option)
0092 {
0093     switch (pnp_option_priority(option)) {
0094     case PNP_RES_PRIORITY_PREFERRED:
0095         return "preferred";
0096     case PNP_RES_PRIORITY_ACCEPTABLE:
0097         return "acceptable";
0098     case PNP_RES_PRIORITY_FUNCTIONAL:
0099         return "functional";
0100     }
0101     return "invalid";
0102 }
0103 
0104 void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option)
0105 {
0106     char buf[128];
0107     int len = 0, i;
0108     struct pnp_port *port;
0109     struct pnp_mem *mem;
0110     struct pnp_irq *irq;
0111     struct pnp_dma *dma;
0112 
0113     if (pnp_option_is_dependent(option))
0114         len += scnprintf(buf + len, sizeof(buf) - len,
0115                  "  dependent set %d (%s) ",
0116                  pnp_option_set(option),
0117                  pnp_option_priority_name(option));
0118     else
0119         len += scnprintf(buf + len, sizeof(buf) - len,
0120                  "  independent ");
0121 
0122     switch (option->type) {
0123     case IORESOURCE_IO:
0124         port = &option->u.port;
0125         len += scnprintf(buf + len, sizeof(buf) - len, "io  min %#llx "
0126                  "max %#llx align %lld size %lld flags %#x",
0127                  (unsigned long long) port->min,
0128                  (unsigned long long) port->max,
0129                  (unsigned long long) port->align,
0130                  (unsigned long long) port->size, port->flags);
0131         break;
0132     case IORESOURCE_MEM:
0133         mem = &option->u.mem;
0134         len += scnprintf(buf + len, sizeof(buf) - len, "mem min %#llx "
0135                  "max %#llx align %lld size %lld flags %#x",
0136                  (unsigned long long) mem->min,
0137                  (unsigned long long) mem->max,
0138                  (unsigned long long) mem->align,
0139                  (unsigned long long) mem->size, mem->flags);
0140         break;
0141     case IORESOURCE_IRQ:
0142         irq = &option->u.irq;
0143         len += scnprintf(buf + len, sizeof(buf) - len, "irq");
0144         if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
0145             len += scnprintf(buf + len, sizeof(buf) - len,
0146                      " <none>");
0147         else {
0148             for (i = 0; i < PNP_IRQ_NR; i++)
0149                 if (test_bit(i, irq->map.bits))
0150                     len += scnprintf(buf + len,
0151                              sizeof(buf) - len,
0152                              " %d", i);
0153         }
0154         len += scnprintf(buf + len, sizeof(buf) - len, " flags %#x",
0155                  irq->flags);
0156         if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
0157             len += scnprintf(buf + len, sizeof(buf) - len,
0158                      " (optional)");
0159         break;
0160     case IORESOURCE_DMA:
0161         dma = &option->u.dma;
0162         len += scnprintf(buf + len, sizeof(buf) - len, "dma");
0163         if (!dma->map)
0164             len += scnprintf(buf + len, sizeof(buf) - len,
0165                      " <none>");
0166         else {
0167             for (i = 0; i < 8; i++)
0168                 if (dma->map & (1 << i))
0169                     len += scnprintf(buf + len,
0170                              sizeof(buf) - len,
0171                              " %d", i);
0172         }
0173         len += scnprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) "
0174                  "flags %#x", dma->map, dma->flags);
0175         break;
0176     }
0177     pnp_dbg(&dev->dev, "%s\n", buf);
0178 }