support.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. }
  67. return NULL;
  68. }
  69. void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc)
  70. {
  71. char buf[128];
  72. int len;
  73. struct pnp_resource *pnp_res;
  74. struct resource *res;
  75. if (list_empty(&dev->resources)) {
  76. pnp_dbg(&dev->dev, "%s: no current resources\n", desc);
  77. return;
  78. }
  79. pnp_dbg(&dev->dev, "%s: current resources:\n", desc);
  80. list_for_each_entry(pnp_res, &dev->resources, list) {
  81. res = &pnp_res->res;
  82. len = 0;
  83. len += scnprintf(buf + len, sizeof(buf) - len, " %-3s ",
  84. pnp_resource_type_name(res));
  85. if (res->flags & IORESOURCE_DISABLED) {
  86. pnp_dbg(&dev->dev, "%sdisabled\n", buf);
  87. continue;
  88. }
  89. switch (pnp_resource_type(res)) {
  90. case IORESOURCE_IO:
  91. case IORESOURCE_MEM:
  92. len += scnprintf(buf + len, sizeof(buf) - len,
  93. "%#llx-%#llx flags %#lx",
  94. (unsigned long long) res->start,
  95. (unsigned long long) res->end,
  96. res->flags);
  97. break;
  98. case IORESOURCE_IRQ:
  99. case IORESOURCE_DMA:
  100. len += scnprintf(buf + len, sizeof(buf) - len,
  101. "%lld flags %#lx",
  102. (unsigned long long) res->start,
  103. res->flags);
  104. break;
  105. }
  106. pnp_dbg(&dev->dev, "%s\n", buf);
  107. }
  108. }
  109. char *pnp_option_priority_name(struct pnp_option *option)
  110. {
  111. switch (pnp_option_priority(option)) {
  112. case PNP_RES_PRIORITY_PREFERRED:
  113. return "preferred";
  114. case PNP_RES_PRIORITY_ACCEPTABLE:
  115. return "acceptable";
  116. case PNP_RES_PRIORITY_FUNCTIONAL:
  117. return "functional";
  118. }
  119. return "invalid";
  120. }
  121. void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option)
  122. {
  123. char buf[128];
  124. int len = 0, i;
  125. struct pnp_port *port;
  126. struct pnp_mem *mem;
  127. struct pnp_irq *irq;
  128. struct pnp_dma *dma;
  129. if (pnp_option_is_dependent(option))
  130. len += scnprintf(buf + len, sizeof(buf) - len,
  131. " dependent set %d (%s) ",
  132. pnp_option_set(option),
  133. pnp_option_priority_name(option));
  134. else
  135. len += scnprintf(buf + len, sizeof(buf) - len,
  136. " independent ");
  137. switch (option->type) {
  138. case IORESOURCE_IO:
  139. port = &option->u.port;
  140. len += scnprintf(buf + len, sizeof(buf) - len, "io min %#llx "
  141. "max %#llx align %lld size %lld flags %#x",
  142. (unsigned long long) port->min,
  143. (unsigned long long) port->max,
  144. (unsigned long long) port->align,
  145. (unsigned long long) port->size, port->flags);
  146. break;
  147. case IORESOURCE_MEM:
  148. mem = &option->u.mem;
  149. len += scnprintf(buf + len, sizeof(buf) - len, "mem min %#llx "
  150. "max %#llx align %lld size %lld flags %#x",
  151. (unsigned long long) mem->min,
  152. (unsigned long long) mem->max,
  153. (unsigned long long) mem->align,
  154. (unsigned long long) mem->size, mem->flags);
  155. break;
  156. case IORESOURCE_IRQ:
  157. irq = &option->u.irq;
  158. len += scnprintf(buf + len, sizeof(buf) - len, "irq");
  159. if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
  160. len += scnprintf(buf + len, sizeof(buf) - len,
  161. " <none>");
  162. else {
  163. for (i = 0; i < PNP_IRQ_NR; i++)
  164. if (test_bit(i, irq->map.bits))
  165. len += scnprintf(buf + len,
  166. sizeof(buf) - len,
  167. " %d", i);
  168. }
  169. len += scnprintf(buf + len, sizeof(buf) - len, " flags %#x",
  170. irq->flags);
  171. if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
  172. len += scnprintf(buf + len, sizeof(buf) - len,
  173. " (optional)");
  174. break;
  175. case IORESOURCE_DMA:
  176. dma = &option->u.dma;
  177. len += scnprintf(buf + len, sizeof(buf) - len, "dma");
  178. if (!dma->map)
  179. len += scnprintf(buf + len, sizeof(buf) - len,
  180. " <none>");
  181. else {
  182. for (i = 0; i < 8; i++)
  183. if (dma->map & (1 << i))
  184. len += scnprintf(buf + len,
  185. sizeof(buf) - len,
  186. " %d", i);
  187. }
  188. len += scnprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) "
  189. "flags %#x", dma->map, dma->flags);
  190. break;
  191. }
  192. pnp_dbg(&dev->dev, "%s\n", buf);
  193. }