support.c 5.5 KB

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