core.c 5.1 KB

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