core.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * core.c - contains all core device and protocol registration functions
  4. *
  5. * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  6. */
  7. #include <linux/pnp.h>
  8. #include <linux/types.h>
  9. #include <linux/list.h>
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/init.h>
  14. #include <linux/string.h>
  15. #include <linux/slab.h>
  16. #include <linux/errno.h>
  17. #include <linux/dma-mapping.h>
  18. #include "base.h"
  19. static LIST_HEAD(pnp_protocols);
  20. LIST_HEAD(pnp_global);
  21. DEFINE_MUTEX(pnp_lock);
  22. /*
  23. * ACPI or PNPBIOS should tell us about all platform devices, so we can
  24. * skip some blind probes. ISAPNP typically enumerates only plug-in ISA
  25. * devices, not built-in things like COM ports.
  26. */
  27. int pnp_platform_devices;
  28. EXPORT_SYMBOL(pnp_platform_devices);
  29. static void pnp_remove_protocol(struct pnp_protocol *protocol)
  30. {
  31. mutex_lock(&pnp_lock);
  32. list_del(&protocol->protocol_list);
  33. mutex_unlock(&pnp_lock);
  34. }
  35. /**
  36. * pnp_register_protocol - adds a pnp protocol to the pnp layer
  37. * @protocol: pointer to the corresponding pnp_protocol structure
  38. *
  39. * Ex protocols: ISAPNP, PNPBIOS, etc
  40. */
  41. int pnp_register_protocol(struct pnp_protocol *protocol)
  42. {
  43. struct list_head *pos;
  44. int nodenum, ret;
  45. INIT_LIST_HEAD(&protocol->devices);
  46. INIT_LIST_HEAD(&protocol->cards);
  47. nodenum = 0;
  48. mutex_lock(&pnp_lock);
  49. /* assign the lowest unused number */
  50. list_for_each(pos, &pnp_protocols) {
  51. struct pnp_protocol *cur = to_pnp_protocol(pos);
  52. if (cur->number == nodenum) {
  53. pos = &pnp_protocols;
  54. nodenum++;
  55. }
  56. }
  57. protocol->number = nodenum;
  58. dev_set_name(&protocol->dev, "pnp%d", nodenum);
  59. list_add_tail(&protocol->protocol_list, &pnp_protocols);
  60. mutex_unlock(&pnp_lock);
  61. ret = device_register(&protocol->dev);
  62. if (ret)
  63. pnp_remove_protocol(protocol);
  64. return ret;
  65. }
  66. static void pnp_free_ids(struct pnp_dev *dev)
  67. {
  68. struct pnp_id *id;
  69. struct pnp_id *next;
  70. id = dev->id;
  71. while (id) {
  72. next = id->next;
  73. kfree(id);
  74. id = next;
  75. }
  76. }
  77. void pnp_free_resource(struct pnp_resource *pnp_res)
  78. {
  79. list_del(&pnp_res->list);
  80. kfree(pnp_res);
  81. }
  82. void pnp_free_resources(struct pnp_dev *dev)
  83. {
  84. struct pnp_resource *pnp_res, *tmp;
  85. list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
  86. pnp_free_resource(pnp_res);
  87. }
  88. }
  89. static void pnp_release_device(struct device *dmdev)
  90. {
  91. struct pnp_dev *dev = to_pnp_dev(dmdev);
  92. pnp_free_ids(dev);
  93. pnp_free_resources(dev);
  94. pnp_free_options(dev);
  95. kfree(dev);
  96. }
  97. struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id,
  98. const char *pnpid)
  99. {
  100. struct pnp_dev *dev;
  101. struct pnp_id *dev_id;
  102. dev = kzalloc_obj(struct pnp_dev);
  103. if (!dev)
  104. return NULL;
  105. INIT_LIST_HEAD(&dev->resources);
  106. INIT_LIST_HEAD(&dev->options);
  107. dev->protocol = protocol;
  108. dev->number = id;
  109. dev->dma_mask = DMA_BIT_MASK(24);
  110. dev->dev.parent = &dev->protocol->dev;
  111. dev->dev.bus = &pnp_bus_type;
  112. dev->dev.dma_mask = &dev->dma_mask;
  113. dev->dev.coherent_dma_mask = dev->dma_mask;
  114. dev->dev.release = &pnp_release_device;
  115. dev_id = pnp_add_id(dev, pnpid);
  116. if (!dev_id) {
  117. kfree(dev);
  118. return NULL;
  119. }
  120. dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
  121. return dev;
  122. }
  123. static void pnp_delist_device(struct pnp_dev *dev)
  124. {
  125. mutex_lock(&pnp_lock);
  126. list_del(&dev->global_list);
  127. list_del(&dev->protocol_list);
  128. mutex_unlock(&pnp_lock);
  129. }
  130. int __pnp_add_device(struct pnp_dev *dev)
  131. {
  132. int ret;
  133. pnp_fixup_device(dev);
  134. dev->status = PNP_READY;
  135. mutex_lock(&pnp_lock);
  136. list_add_tail(&dev->global_list, &pnp_global);
  137. list_add_tail(&dev->protocol_list, &dev->protocol->devices);
  138. mutex_unlock(&pnp_lock);
  139. ret = device_register(&dev->dev);
  140. if (ret)
  141. pnp_delist_device(dev);
  142. else if (dev->protocol->can_wakeup)
  143. device_set_wakeup_capable(&dev->dev,
  144. dev->protocol->can_wakeup(dev));
  145. return ret;
  146. }
  147. /*
  148. * pnp_add_device - adds a pnp device to the pnp layer
  149. * @dev: pointer to dev to add
  150. *
  151. * adds to driver model, name database, fixups, interface, etc.
  152. */
  153. int pnp_add_device(struct pnp_dev *dev)
  154. {
  155. int ret;
  156. char buf[128];
  157. int len = 0;
  158. struct pnp_id *id;
  159. if (dev->card)
  160. return -EINVAL;
  161. ret = __pnp_add_device(dev);
  162. if (ret)
  163. return ret;
  164. buf[0] = '\0';
  165. for (id = dev->id; id; id = id->next)
  166. len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
  167. dev_dbg(&dev->dev, "%s device, IDs%s (%s)\n", dev->protocol->name, buf,
  168. dev->active ? "active" : "disabled");
  169. return 0;
  170. }
  171. static int __init pnp_init(void)
  172. {
  173. return bus_register(&pnp_bus_type);
  174. }
  175. subsys_initcall(pnp_init);
  176. int pnp_debug;
  177. #if defined(CONFIG_PNP_DEBUG_MESSAGES)
  178. module_param_named(debug, pnp_debug, int, 0644);
  179. #endif