core.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <[email protected]>
  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. /**
  67. * pnp_unregister_protocol - removes a pnp protocol from the pnp layer
  68. * @protocol: pointer to the corresponding pnp_protocol structure
  69. */
  70. void pnp_unregister_protocol(struct pnp_protocol *protocol)
  71. {
  72. pnp_remove_protocol(protocol);
  73. device_unregister(&protocol->dev);
  74. }
  75. static void pnp_free_ids(struct pnp_dev *dev)
  76. {
  77. struct pnp_id *id;
  78. struct pnp_id *next;
  79. id = dev->id;
  80. while (id) {
  81. next = id->next;
  82. kfree(id);
  83. id = next;
  84. }
  85. }
  86. void pnp_free_resource(struct pnp_resource *pnp_res)
  87. {
  88. list_del(&pnp_res->list);
  89. kfree(pnp_res);
  90. }
  91. void pnp_free_resources(struct pnp_dev *dev)
  92. {
  93. struct pnp_resource *pnp_res, *tmp;
  94. list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
  95. pnp_free_resource(pnp_res);
  96. }
  97. }
  98. static void pnp_release_device(struct device *dmdev)
  99. {
  100. struct pnp_dev *dev = to_pnp_dev(dmdev);
  101. pnp_free_ids(dev);
  102. pnp_free_resources(dev);
  103. pnp_free_options(dev);
  104. kfree(dev);
  105. }
  106. struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id,
  107. const char *pnpid)
  108. {
  109. struct pnp_dev *dev;
  110. struct pnp_id *dev_id;
  111. dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
  112. if (!dev)
  113. return NULL;
  114. INIT_LIST_HEAD(&dev->resources);
  115. INIT_LIST_HEAD(&dev->options);
  116. dev->protocol = protocol;
  117. dev->number = id;
  118. dev->dma_mask = DMA_BIT_MASK(24);
  119. dev->dev.parent = &dev->protocol->dev;
  120. dev->dev.bus = &pnp_bus_type;
  121. dev->dev.dma_mask = &dev->dma_mask;
  122. dev->dev.coherent_dma_mask = dev->dma_mask;
  123. dev->dev.release = &pnp_release_device;
  124. dev_id = pnp_add_id(dev, pnpid);
  125. if (!dev_id) {
  126. kfree(dev);
  127. return NULL;
  128. }
  129. dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
  130. return dev;
  131. }
  132. static void pnp_delist_device(struct pnp_dev *dev)
  133. {
  134. mutex_lock(&pnp_lock);
  135. list_del(&dev->global_list);
  136. list_del(&dev->protocol_list);
  137. mutex_unlock(&pnp_lock);
  138. }
  139. int __pnp_add_device(struct pnp_dev *dev)
  140. {
  141. int ret;
  142. pnp_fixup_device(dev);
  143. dev->status = PNP_READY;
  144. mutex_lock(&pnp_lock);
  145. list_add_tail(&dev->global_list, &pnp_global);
  146. list_add_tail(&dev->protocol_list, &dev->protocol->devices);
  147. mutex_unlock(&pnp_lock);
  148. ret = device_register(&dev->dev);
  149. if (ret)
  150. pnp_delist_device(dev);
  151. else if (dev->protocol->can_wakeup)
  152. device_set_wakeup_capable(&dev->dev,
  153. dev->protocol->can_wakeup(dev));
  154. return ret;
  155. }
  156. /*
  157. * pnp_add_device - adds a pnp device to the pnp layer
  158. * @dev: pointer to dev to add
  159. *
  160. * adds to driver model, name database, fixups, interface, etc.
  161. */
  162. int pnp_add_device(struct pnp_dev *dev)
  163. {
  164. int ret;
  165. char buf[128];
  166. int len = 0;
  167. struct pnp_id *id;
  168. if (dev->card)
  169. return -EINVAL;
  170. ret = __pnp_add_device(dev);
  171. if (ret)
  172. return ret;
  173. buf[0] = '\0';
  174. for (id = dev->id; id; id = id->next)
  175. len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
  176. dev_dbg(&dev->dev, "%s device, IDs%s (%s)\n", dev->protocol->name, buf,
  177. dev->active ? "active" : "disabled");
  178. return 0;
  179. }
  180. void __pnp_remove_device(struct pnp_dev *dev)
  181. {
  182. pnp_delist_device(dev);
  183. device_unregister(&dev->dev);
  184. }
  185. static int __init pnp_init(void)
  186. {
  187. return bus_register(&pnp_bus_type);
  188. }
  189. subsys_initcall(pnp_init);
  190. int pnp_debug;
  191. #if defined(CONFIG_PNP_DEBUG_MESSAGES)
  192. module_param_named(debug, pnp_debug, int, 0644);
  193. #endif