support.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * support.c - standard functions for the use of pnp protocol drivers
  4. *
  5. * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
  6. * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
  7. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/ctype.h>
  11. #include <linux/hex.h>
  12. #include <linux/pnp.h>
  13. #include "base.h"
  14. /**
  15. * pnp_is_active - Determines if a device is active based on its current
  16. * resources
  17. * @dev: pointer to the desired PnP device
  18. */
  19. int pnp_is_active(struct pnp_dev *dev)
  20. {
  21. /*
  22. * I don't think this is very reliable because pnp_disable_dev()
  23. * only clears out auto-assigned resources.
  24. */
  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 &&
  27. pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1)
  28. return 0;
  29. else
  30. return 1;
  31. }
  32. EXPORT_SYMBOL(pnp_is_active);
  33. /*
  34. * Functionally similar to acpi_ex_eisa_id_to_string(), but that's
  35. * buried in the ACPI CA, and we can't depend on it being present.
  36. */
  37. void pnp_eisa_id_to_string(u32 id, char *str)
  38. {
  39. id = be32_to_cpu(id);
  40. /*
  41. * According to the specs, the first three characters are five-bit
  42. * compressed ASCII, and the left-over high order bit should be zero.
  43. * However, the Linux ISAPNP code historically used six bits for the
  44. * first character, and there seem to be IDs that depend on that,
  45. * e.g., "nEC8241" in the Linux 8250_pnp serial driver and the
  46. * FreeBSD sys/pc98/cbus/sio_cbus.c driver.
  47. */
  48. str[0] = 'A' + ((id >> 26) & 0x3f) - 1;
  49. str[1] = 'A' + ((id >> 21) & 0x1f) - 1;
  50. str[2] = 'A' + ((id >> 16) & 0x1f) - 1;
  51. str[3] = hex_asc_hi(id >> 8);
  52. str[4] = hex_asc_lo(id >> 8);
  53. str[5] = hex_asc_hi(id);
  54. str[6] = hex_asc_lo(id);
  55. str[7] = '\0';
  56. }
  57. char *pnp_resource_type_name(struct resource *res)
  58. {
  59. switch (pnp_resource_type(res)) {
  60. case IORESOURCE_IO:
  61. return "io";
  62. case IORESOURCE_MEM:
  63. return "mem";
  64. case IORESOURCE_IRQ:
  65. return "irq";
  66. case IORESOURCE_DMA:
  67. return "dma";
  68. case IORESOURCE_BUS:
  69. return "bus";
  70. }
  71. return "unknown";
  72. }
  73. void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc)
  74. {
  75. struct pnp_resource *pnp_res;
  76. if (list_empty(&dev->resources))
  77. pnp_dbg(&dev->dev, "%s: no current resources\n", desc);
  78. else {
  79. pnp_dbg(&dev->dev, "%s: current resources:\n", desc);
  80. list_for_each_entry(pnp_res, &dev->resources, list)
  81. pnp_dbg(&dev->dev, "%pr\n", &pnp_res->res);
  82. }
  83. }
  84. char *pnp_option_priority_name(struct pnp_option *option)
  85. {
  86. switch (pnp_option_priority(option)) {
  87. case PNP_RES_PRIORITY_PREFERRED:
  88. return "preferred";
  89. case PNP_RES_PRIORITY_ACCEPTABLE:
  90. return "acceptable";
  91. case PNP_RES_PRIORITY_FUNCTIONAL:
  92. return "functional";
  93. }
  94. return "invalid";
  95. }
  96. void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option)
  97. {
  98. char buf[128];
  99. int len = 0, i;
  100. struct pnp_port *port;
  101. struct pnp_mem *mem;
  102. struct pnp_irq *irq;
  103. struct pnp_dma *dma;
  104. if (pnp_option_is_dependent(option))
  105. len += scnprintf(buf + len, sizeof(buf) - len,
  106. " dependent set %d (%s) ",
  107. pnp_option_set(option),
  108. pnp_option_priority_name(option));
  109. else
  110. len += scnprintf(buf + len, sizeof(buf) - len,
  111. " independent ");
  112. switch (option->type) {
  113. case IORESOURCE_IO:
  114. port = &option->u.port;
  115. len += scnprintf(buf + len, sizeof(buf) - len, "io min %#llx "
  116. "max %#llx align %lld size %lld flags %#x",
  117. (unsigned long long) port->min,
  118. (unsigned long long) port->max,
  119. (unsigned long long) port->align,
  120. (unsigned long long) port->size, port->flags);
  121. break;
  122. case IORESOURCE_MEM:
  123. mem = &option->u.mem;
  124. len += scnprintf(buf + len, sizeof(buf) - len, "mem min %#llx "
  125. "max %#llx align %lld size %lld flags %#x",
  126. (unsigned long long) mem->min,
  127. (unsigned long long) mem->max,
  128. (unsigned long long) mem->align,
  129. (unsigned long long) mem->size, mem->flags);
  130. break;
  131. case IORESOURCE_IRQ:
  132. irq = &option->u.irq;
  133. len += scnprintf(buf + len, sizeof(buf) - len, "irq");
  134. if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
  135. len += scnprintf(buf + len, sizeof(buf) - len,
  136. " <none>");
  137. else {
  138. for (i = 0; i < PNP_IRQ_NR; i++)
  139. if (test_bit(i, irq->map.bits))
  140. len += scnprintf(buf + len,
  141. sizeof(buf) - len,
  142. " %d", i);
  143. }
  144. len += scnprintf(buf + len, sizeof(buf) - len, " flags %#x",
  145. irq->flags);
  146. if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
  147. len += scnprintf(buf + len, sizeof(buf) - len,
  148. " (optional)");
  149. break;
  150. case IORESOURCE_DMA:
  151. dma = &option->u.dma;
  152. len += scnprintf(buf + len, sizeof(buf) - len, "dma");
  153. if (!dma->map)
  154. len += scnprintf(buf + len, sizeof(buf) - len,
  155. " <none>");
  156. else {
  157. for (i = 0; i < 8; i++)
  158. if (dma->map & (1 << i))
  159. len += scnprintf(buf + len,
  160. sizeof(buf) - len,
  161. " %d", i);
  162. }
  163. len += scnprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) "
  164. "flags %#x", dma->map, dma->flags);
  165. break;
  166. }
  167. pnp_dbg(&dev->dev, "%s\n", buf);
  168. }