core.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. void *pnp_alloc(long size)
  30. {
  31. void *result;
  32. result = kzalloc(size, GFP_KERNEL);
  33. if (!result) {
  34. printk(KERN_ERR "pnp: Out of Memory\n");
  35. return NULL;
  36. }
  37. return result;
  38. }
  39. static void pnp_remove_protocol(struct pnp_protocol *protocol)
  40. {
  41. mutex_lock(&pnp_lock);
  42. list_del(&protocol->protocol_list);
  43. mutex_unlock(&pnp_lock);
  44. }
  45. /**
  46. * pnp_protocol_register - adds a pnp protocol to the pnp layer
  47. * @protocol: pointer to the corresponding pnp_protocol structure
  48. *
  49. * Ex protocols: ISAPNP, PNPBIOS, etc
  50. */
  51. int pnp_register_protocol(struct pnp_protocol *protocol)
  52. {
  53. struct list_head *pos;
  54. int nodenum, ret;
  55. INIT_LIST_HEAD(&protocol->devices);
  56. INIT_LIST_HEAD(&protocol->cards);
  57. nodenum = 0;
  58. mutex_lock(&pnp_lock);
  59. /* assign the lowest unused number */
  60. list_for_each(pos, &pnp_protocols) {
  61. struct pnp_protocol *cur = to_pnp_protocol(pos);
  62. if (cur->number == nodenum) {
  63. pos = &pnp_protocols;
  64. nodenum++;
  65. }
  66. }
  67. protocol->number = nodenum;
  68. dev_set_name(&protocol->dev, "pnp%d", nodenum);
  69. list_add_tail(&protocol->protocol_list, &pnp_protocols);
  70. mutex_unlock(&pnp_lock);
  71. ret = device_register(&protocol->dev);
  72. if (ret)
  73. pnp_remove_protocol(protocol);
  74. return ret;
  75. }
  76. /**
  77. * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
  78. * @protocol: pointer to the corresponding pnp_protocol structure
  79. */
  80. void pnp_unregister_protocol(struct pnp_protocol *protocol)
  81. {
  82. pnp_remove_protocol(protocol);
  83. device_unregister(&protocol->dev);
  84. }
  85. static void pnp_free_ids(struct pnp_dev *dev)
  86. {
  87. struct pnp_id *id;
  88. struct pnp_id *next;
  89. id = dev->id;
  90. while (id) {
  91. next = id->next;
  92. kfree(id);
  93. id = next;
  94. }
  95. }
  96. void pnp_free_resource(struct pnp_resource *pnp_res)
  97. {
  98. list_del(&pnp_res->list);
  99. kfree(pnp_res);
  100. }
  101. void pnp_free_resources(struct pnp_dev *dev)
  102. {
  103. struct pnp_resource *pnp_res, *tmp;
  104. list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
  105. pnp_free_resource(pnp_res);
  106. }
  107. }
  108. static void pnp_release_device(struct device *dmdev)
  109. {
  110. struct pnp_dev *dev = to_pnp_dev(dmdev);
  111. pnp_free_ids(dev);
  112. pnp_free_resources(dev);
  113. pnp_free_options(dev);
  114. kfree(dev);
  115. }
  116. struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id,
  117. const char *pnpid)
  118. {
  119. struct pnp_dev *dev;
  120. struct pnp_id *dev_id;
  121. dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
  122. if (!dev)
  123. return NULL;
  124. INIT_LIST_HEAD(&dev->resources);
  125. INIT_LIST_HEAD(&dev->options);
  126. dev->protocol = protocol;
  127. dev->number = id;
  128. dev->dma_mask = DMA_BIT_MASK(24);
  129. dev->dev.parent = &dev->protocol->dev;
  130. dev->dev.bus = &pnp_bus_type;
  131. dev->dev.dma_mask = &dev->dma_mask;
  132. dev->dev.coherent_dma_mask = dev->dma_mask;
  133. dev->dev.release = &pnp_release_device;
  134. dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
  135. dev_id = pnp_add_id(dev, pnpid);
  136. if (!dev_id) {
  137. kfree(dev);
  138. return NULL;
  139. }
  140. return dev;
  141. }
  142. static void pnp_delist_device(struct pnp_dev *dev)
  143. {
  144. mutex_lock(&pnp_lock);
  145. list_del(&dev->global_list);
  146. list_del(&dev->protocol_list);
  147. mutex_unlock(&pnp_lock);
  148. }
  149. int __pnp_add_device(struct pnp_dev *dev)
  150. {
  151. int ret;
  152. pnp_fixup_device(dev);
  153. dev->status = PNP_READY;
  154. mutex_lock(&pnp_lock);
  155. list_add_tail(&dev->global_list, &pnp_global);
  156. list_add_tail(&dev->protocol_list, &dev->protocol->devices);
  157. mutex_unlock(&pnp_lock);
  158. ret = device_register(&dev->dev);
  159. if (ret)
  160. pnp_delist_device(dev);
  161. else if (dev->protocol->can_wakeup)
  162. device_set_wakeup_capable(&dev->dev,
  163. dev->protocol->can_wakeup(dev));
  164. return ret;
  165. }
  166. /*
  167. * pnp_add_device - adds a pnp device to the pnp layer
  168. * @dev: pointer to dev to add
  169. *
  170. * adds to driver model, name database, fixups, interface, etc.
  171. */
  172. int pnp_add_device(struct pnp_dev *dev)
  173. {
  174. int ret;
  175. char buf[128];
  176. int len = 0;
  177. struct pnp_id *id;
  178. if (dev->card)
  179. return -EINVAL;
  180. ret = __pnp_add_device(dev);
  181. if (ret)
  182. return ret;
  183. buf[0] = '\0';
  184. for (id = dev->id; id; id = id->next)
  185. len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
  186. dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs%s (%s)\n",
  187. dev->protocol->name, buf,
  188. dev->active ? "active" : "disabled");
  189. return 0;
  190. }
  191. void __pnp_remove_device(struct pnp_dev *dev)
  192. {
  193. pnp_delist_device(dev);
  194. device_unregister(&dev->dev);
  195. }
  196. static int __init pnp_init(void)
  197. {
  198. return bus_register(&pnp_bus_type);
  199. }
  200. subsys_initcall(pnp_init);
  201. int pnp_debug;
  202. #if defined(CONFIG_PNP_DEBUG_MESSAGES)
  203. module_param_named(debug, pnp_debug, int, 0644);
  204. #endif