core.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. sprintf(protocol->dev.bus_id, "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. static void pnp_release_device(struct device *dmdev)
  88. {
  89. struct pnp_dev *dev = to_pnp_dev(dmdev);
  90. pnp_free_option(dev->independent);
  91. pnp_free_option(dev->dependent);
  92. pnp_free_ids(dev);
  93. kfree(dev);
  94. }
  95. int __pnp_add_device(struct pnp_dev *dev)
  96. {
  97. int ret;
  98. pnp_fixup_device(dev);
  99. dev->dev.bus = &pnp_bus_type;
  100. dev->dev.dma_mask = &dev->dma_mask;
  101. dev->dma_mask = dev->dev.coherent_dma_mask = DMA_24BIT_MASK;
  102. dev->dev.release = &pnp_release_device;
  103. dev->status = PNP_READY;
  104. spin_lock(&pnp_lock);
  105. list_add_tail(&dev->global_list, &pnp_global);
  106. list_add_tail(&dev->protocol_list, &dev->protocol->devices);
  107. spin_unlock(&pnp_lock);
  108. ret = device_register(&dev->dev);
  109. if (ret)
  110. return ret;
  111. pnp_interface_attach_device(dev);
  112. return 0;
  113. }
  114. /*
  115. * pnp_add_device - adds a pnp device to the pnp layer
  116. * @dev: pointer to dev to add
  117. *
  118. * adds to driver model, name database, fixups, interface, etc.
  119. */
  120. int pnp_add_device(struct pnp_dev *dev)
  121. {
  122. int ret;
  123. if (dev->card)
  124. return -EINVAL;
  125. dev->dev.parent = &dev->protocol->dev;
  126. sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number,
  127. dev->number);
  128. ret = __pnp_add_device(dev);
  129. if (ret)
  130. return ret;
  131. #ifdef CONFIG_PNP_DEBUG
  132. {
  133. struct pnp_id *id;
  134. dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs",
  135. dev->protocol->name);
  136. for (id = dev->id; id; id = id->next)
  137. printk(" %s", id->id);
  138. printk(" (%s)\n", dev->active ? "active" : "disabled");
  139. }
  140. #endif
  141. return 0;
  142. }
  143. void __pnp_remove_device(struct pnp_dev *dev)
  144. {
  145. spin_lock(&pnp_lock);
  146. list_del(&dev->global_list);
  147. list_del(&dev->protocol_list);
  148. spin_unlock(&pnp_lock);
  149. device_unregister(&dev->dev);
  150. }
  151. static int __init pnp_init(void)
  152. {
  153. printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
  154. return bus_register(&pnp_bus_type);
  155. }
  156. subsys_initcall(pnp_init);