support.c 4.8 KB

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