support.c 4.8 KB

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