core.c 4.7 KB

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