support.c 5.5 KB

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