core.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if (!protocol)
  48. return -EINVAL;
  49. INIT_LIST_HEAD(&protocol->devices);
  50. INIT_LIST_HEAD(&protocol->cards);
  51. nodenum = 0;
  52. spin_lock(&pnp_lock);
  53. /* assign the lowest unused number */
  54. list_for_each(pos, &pnp_protocols) {
  55. struct pnp_protocol *cur = to_pnp_protocol(pos);
  56. if (cur->number == nodenum) {
  57. pos = &pnp_protocols;
  58. nodenum++;
  59. }
  60. }
  61. list_add_tail(&protocol->protocol_list, &pnp_protocols);
  62. spin_unlock(&pnp_lock);
  63. protocol->number = nodenum;
  64. sprintf(protocol->dev.bus_id, "pnp%d", nodenum);
  65. return device_register(&protocol->dev);
  66. }
  67. /**
  68. * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
  69. * @protocol: pointer to the corresponding pnp_protocol structure
  70. */
  71. void pnp_unregister_protocol(struct pnp_protocol *protocol)
  72. {
  73. spin_lock(&pnp_lock);
  74. list_del(&protocol->protocol_list);
  75. spin_unlock(&pnp_lock);
  76. device_unregister(&protocol->dev);
  77. }
  78. static void pnp_free_ids(struct pnp_dev *dev)
  79. {
  80. struct pnp_id *id;
  81. struct pnp_id *next;
  82. if (!dev)
  83. return;
  84. id = dev->id;
  85. while (id) {
  86. next = id->next;
  87. kfree(id);
  88. id = next;
  89. }
  90. }
  91. static void pnp_release_device(struct device *dmdev)
  92. {
  93. struct pnp_dev *dev = to_pnp_dev(dmdev);
  94. pnp_free_option(dev->independent);
  95. pnp_free_option(dev->dependent);
  96. pnp_free_ids(dev);
  97. kfree(dev);
  98. }
  99. int __pnp_add_device(struct pnp_dev *dev)
  100. {
  101. int ret;
  102. pnp_fixup_device(dev);
  103. dev->dev.bus = &pnp_bus_type;
  104. dev->dev.dma_mask = &dev->dma_mask;
  105. dev->dma_mask = dev->dev.coherent_dma_mask = DMA_24BIT_MASK;
  106. dev->dev.release = &pnp_release_device;
  107. dev->status = PNP_READY;
  108. spin_lock(&pnp_lock);
  109. list_add_tail(&dev->global_list, &pnp_global);
  110. list_add_tail(&dev->protocol_list, &dev->protocol->devices);
  111. spin_unlock(&pnp_lock);
  112. ret = device_register(&dev->dev);
  113. if (ret == 0)
  114. pnp_interface_attach_device(dev);
  115. return ret;
  116. }
  117. /*
  118. * pnp_add_device - adds a pnp device to the pnp layer
  119. * @dev: pointer to dev to add
  120. *
  121. * adds to driver model, name database, fixups, interface, etc.
  122. */
  123. int pnp_add_device(struct pnp_dev *dev)
  124. {
  125. if (!dev || !dev->protocol || dev->card)
  126. return -EINVAL;
  127. dev->dev.parent = &dev->protocol->dev;
  128. sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number,
  129. dev->number);
  130. return __pnp_add_device(dev);
  131. }
  132. void __pnp_remove_device(struct pnp_dev *dev)
  133. {
  134. spin_lock(&pnp_lock);
  135. list_del(&dev->global_list);
  136. list_del(&dev->protocol_list);
  137. spin_unlock(&pnp_lock);
  138. device_unregister(&dev->dev);
  139. }
  140. static int __init pnp_init(void)
  141. {
  142. printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
  143. return bus_register(&pnp_bus_type);
  144. }
  145. subsys_initcall(pnp_init);