core.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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,
  108. const char *pnpid)
  109. {
  110. struct pnp_dev *dev;
  111. struct pnp_id *dev_id;
  112. dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
  113. if (!dev)
  114. return NULL;
  115. INIT_LIST_HEAD(&dev->resources);
  116. INIT_LIST_HEAD(&dev->options);
  117. dev->protocol = protocol;
  118. dev->number = id;
  119. dev->dma_mask = DMA_BIT_MASK(24);
  120. dev->dev.parent = &dev->protocol->dev;
  121. dev->dev.bus = &pnp_bus_type;
  122. dev->dev.dma_mask = &dev->dma_mask;
  123. dev->dev.coherent_dma_mask = dev->dma_mask;
  124. dev->dev.release = &pnp_release_device;
  125. dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
  126. dev_id = pnp_add_id(dev, pnpid);
  127. if (!dev_id) {
  128. kfree(dev);
  129. return NULL;
  130. }
  131. return dev;
  132. }
  133. int __pnp_add_device(struct pnp_dev *dev)
  134. {
  135. pnp_fixup_device(dev);
  136. dev->status = PNP_READY;
  137. spin_lock(&pnp_lock);
  138. list_add_tail(&dev->global_list, &pnp_global);
  139. list_add_tail(&dev->protocol_list, &dev->protocol->devices);
  140. spin_unlock(&pnp_lock);
  141. if (dev->protocol->can_wakeup)
  142. device_set_wakeup_capable(&dev->dev,
  143. dev->protocol->can_wakeup(dev));
  144. return device_register(&dev->dev);
  145. }
  146. /*
  147. * pnp_add_device - adds a pnp device to the pnp layer
  148. * @dev: pointer to dev to add
  149. *
  150. * adds to driver model, name database, fixups, interface, etc.
  151. */
  152. int pnp_add_device(struct pnp_dev *dev)
  153. {
  154. int ret;
  155. char buf[128];
  156. int len = 0;
  157. struct pnp_id *id;
  158. if (dev->card)
  159. return -EINVAL;
  160. ret = __pnp_add_device(dev);
  161. if (ret)
  162. return ret;
  163. buf[0] = '\0';
  164. for (id = dev->id; id; id = id->next)
  165. len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
  166. dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs%s (%s)\n",
  167. dev->protocol->name, buf,
  168. dev->active ? "active" : "disabled");
  169. return 0;
  170. }
  171. void __pnp_remove_device(struct pnp_dev *dev)
  172. {
  173. spin_lock(&pnp_lock);
  174. list_del(&dev->global_list);
  175. list_del(&dev->protocol_list);
  176. spin_unlock(&pnp_lock);
  177. device_unregister(&dev->dev);
  178. }
  179. static int __init pnp_init(void)
  180. {
  181. return bus_register(&pnp_bus_type);
  182. }
  183. subsys_initcall(pnp_init);
  184. int pnp_debug;
  185. #if defined(CONFIG_PNP_DEBUG_MESSAGES)
  186. module_param_named(debug, pnp_debug, int, 0644);
  187. #endif