core.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * core.c - contains all core device and protocol registration functions
  3. *
  4. * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  5. *
  6. */
  7. #include <linux/pnp.h>
  8. #include <linux/types.h>
  9. #include <linux/list.h>
  10. #include <linux/device.h>
  11. #include <linux/module.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_SPINLOCK(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. /**
  39. * pnp_protocol_register - adds a pnp protocol to the pnp layer
  40. * @protocol: pointer to the corresponding pnp_protocol structure
  41. *
  42. * Ex protocols: ISAPNP, PNPBIOS, etc
  43. */
  44. int pnp_register_protocol(struct pnp_protocol *protocol)
  45. {
  46. int nodenum;
  47. struct list_head * pos;
  48. if (!protocol)
  49. return -EINVAL;
  50. INIT_LIST_HEAD(&protocol->devices);
  51. INIT_LIST_HEAD(&protocol->cards);
  52. nodenum = 0;
  53. spin_lock(&pnp_lock);
  54. /* assign the lowest unused number */
  55. list_for_each(pos,&pnp_protocols) {
  56. struct pnp_protocol * cur = to_pnp_protocol(pos);
  57. if (cur->number == nodenum){
  58. pos = &pnp_protocols;
  59. nodenum++;
  60. }
  61. }
  62. list_add_tail(&protocol->protocol_list, &pnp_protocols);
  63. spin_unlock(&pnp_lock);
  64. protocol->number = nodenum;
  65. sprintf(protocol->dev.bus_id, "pnp%d", nodenum);
  66. return device_register(&protocol->dev);
  67. }
  68. /**
  69. * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
  70. * @protocol: pointer to the corresponding pnp_protocol structure
  71. *
  72. */
  73. void pnp_unregister_protocol(struct pnp_protocol *protocol)
  74. {
  75. spin_lock(&pnp_lock);
  76. list_del(&protocol->protocol_list);
  77. spin_unlock(&pnp_lock);
  78. device_unregister(&protocol->dev);
  79. }
  80. static void pnp_free_ids(struct pnp_dev *dev)
  81. {
  82. struct pnp_id * id;
  83. struct pnp_id * next;
  84. if (!dev)
  85. return;
  86. id = dev->id;
  87. while (id) {
  88. next = id->next;
  89. kfree(id);
  90. id = next;
  91. }
  92. }
  93. static void pnp_release_device(struct device *dmdev)
  94. {
  95. struct pnp_dev * dev = to_pnp_dev(dmdev);
  96. pnp_free_option(dev->independent);
  97. pnp_free_option(dev->dependent);
  98. pnp_free_ids(dev);
  99. kfree(dev);
  100. }
  101. int __pnp_add_device(struct pnp_dev *dev)
  102. {
  103. int ret;
  104. pnp_fixup_device(dev);
  105. dev->dev.bus = &pnp_bus_type;
  106. dev->dev.dma_mask = &dev->dma_mask;
  107. dev->dma_mask = dev->dev.coherent_dma_mask = DMA_24BIT_MASK;
  108. dev->dev.release = &pnp_release_device;
  109. dev->status = PNP_READY;
  110. spin_lock(&pnp_lock);
  111. list_add_tail(&dev->global_list, &pnp_global);
  112. list_add_tail(&dev->protocol_list, &dev->protocol->devices);
  113. spin_unlock(&pnp_lock);
  114. ret = device_register(&dev->dev);
  115. if (ret == 0)
  116. pnp_interface_attach_device(dev);
  117. return ret;
  118. }
  119. /*
  120. * pnp_add_device - adds a pnp device to the pnp layer
  121. * @dev: pointer to dev to add
  122. *
  123. * adds to driver model, name database, fixups, interface, etc.
  124. */
  125. int pnp_add_device(struct pnp_dev *dev)
  126. {
  127. if (!dev || !dev->protocol || dev->card)
  128. return -EINVAL;
  129. dev->dev.parent = &dev->protocol->dev;
  130. sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number, dev->number);
  131. return __pnp_add_device(dev);
  132. }
  133. void __pnp_remove_device(struct pnp_dev *dev)
  134. {
  135. spin_lock(&pnp_lock);
  136. list_del(&dev->global_list);
  137. list_del(&dev->protocol_list);
  138. spin_unlock(&pnp_lock);
  139. device_unregister(&dev->dev);
  140. }
  141. /**
  142. * pnp_remove_device - removes a pnp device from the pnp layer
  143. * @dev: pointer to dev to add
  144. *
  145. * this function will free all mem used by dev
  146. */
  147. #if 0
  148. void pnp_remove_device(struct pnp_dev *dev)
  149. {
  150. if (!dev || dev->card)
  151. return;
  152. __pnp_remove_device(dev);
  153. }
  154. #endif /* 0 */
  155. static int __init pnp_init(void)
  156. {
  157. printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
  158. return bus_register(&pnp_bus_type);
  159. }
  160. subsys_initcall(pnp_init);
  161. #if 0
  162. EXPORT_SYMBOL(pnp_register_protocol);
  163. EXPORT_SYMBOL(pnp_unregister_protocol);
  164. EXPORT_SYMBOL(pnp_add_device);
  165. EXPORT_SYMBOL(pnp_remove_device);
  166. #endif /* 0 */