core.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "base.h"
  17. static LIST_HEAD(pnp_protocols);
  18. LIST_HEAD(pnp_global);
  19. DEFINE_SPINLOCK(pnp_lock);
  20. void *pnp_alloc(long size)
  21. {
  22. void *result;
  23. result = kmalloc(size, GFP_KERNEL);
  24. if (!result){
  25. printk(KERN_ERR "pnp: Out of Memory\n");
  26. return NULL;
  27. }
  28. memset(result, 0, size);
  29. return result;
  30. }
  31. /**
  32. * pnp_protocol_register - adds a pnp protocol to the pnp layer
  33. * @protocol: pointer to the corresponding pnp_protocol structure
  34. *
  35. * Ex protocols: ISAPNP, PNPBIOS, etc
  36. */
  37. int pnp_register_protocol(struct pnp_protocol *protocol)
  38. {
  39. int nodenum;
  40. struct list_head * pos;
  41. if (!protocol)
  42. return -EINVAL;
  43. INIT_LIST_HEAD(&protocol->devices);
  44. INIT_LIST_HEAD(&protocol->cards);
  45. nodenum = 0;
  46. spin_lock(&pnp_lock);
  47. /* assign the lowest unused number */
  48. list_for_each(pos,&pnp_protocols) {
  49. struct pnp_protocol * cur = to_pnp_protocol(pos);
  50. if (cur->number == nodenum){
  51. pos = &pnp_protocols;
  52. nodenum++;
  53. }
  54. }
  55. list_add_tail(&protocol->protocol_list, &pnp_protocols);
  56. spin_unlock(&pnp_lock);
  57. protocol->number = nodenum;
  58. sprintf(protocol->dev.bus_id, "pnp%d", nodenum);
  59. return device_register(&protocol->dev);
  60. }
  61. /**
  62. * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
  63. * @protocol: pointer to the corresponding pnp_protocol structure
  64. *
  65. */
  66. void pnp_unregister_protocol(struct pnp_protocol *protocol)
  67. {
  68. spin_lock(&pnp_lock);
  69. list_del(&protocol->protocol_list);
  70. spin_unlock(&pnp_lock);
  71. device_unregister(&protocol->dev);
  72. }
  73. static void pnp_free_ids(struct pnp_dev *dev)
  74. {
  75. struct pnp_id * id;
  76. struct pnp_id * next;
  77. if (!dev)
  78. return;
  79. id = dev->id;
  80. while (id) {
  81. next = id->next;
  82. kfree(id);
  83. id = next;
  84. }
  85. }
  86. static void pnp_release_device(struct device *dmdev)
  87. {
  88. struct pnp_dev * dev = to_pnp_dev(dmdev);
  89. pnp_free_option(dev->independent);
  90. pnp_free_option(dev->dependent);
  91. pnp_free_ids(dev);
  92. kfree(dev);
  93. }
  94. int __pnp_add_device(struct pnp_dev *dev)
  95. {
  96. int ret;
  97. pnp_fixup_device(dev);
  98. dev->dev.bus = &pnp_bus_type;
  99. dev->dev.release = &pnp_release_device;
  100. dev->status = PNP_READY;
  101. spin_lock(&pnp_lock);
  102. list_add_tail(&dev->global_list, &pnp_global);
  103. list_add_tail(&dev->protocol_list, &dev->protocol->devices);
  104. spin_unlock(&pnp_lock);
  105. ret = device_register(&dev->dev);
  106. if (ret == 0)
  107. pnp_interface_attach_device(dev);
  108. return ret;
  109. }
  110. /*
  111. * pnp_add_device - adds a pnp device to the pnp layer
  112. * @dev: pointer to dev to add
  113. *
  114. * adds to driver model, name database, fixups, interface, etc.
  115. */
  116. int pnp_add_device(struct pnp_dev *dev)
  117. {
  118. if (!dev || !dev->protocol || dev->card)
  119. return -EINVAL;
  120. dev->dev.parent = &dev->protocol->dev;
  121. sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number, dev->number);
  122. return __pnp_add_device(dev);
  123. }
  124. void __pnp_remove_device(struct pnp_dev *dev)
  125. {
  126. spin_lock(&pnp_lock);
  127. list_del(&dev->global_list);
  128. list_del(&dev->protocol_list);
  129. spin_unlock(&pnp_lock);
  130. device_unregister(&dev->dev);
  131. }
  132. /**
  133. * pnp_remove_device - removes a pnp device from the pnp layer
  134. * @dev: pointer to dev to add
  135. *
  136. * this function will free all mem used by dev
  137. */
  138. #if 0
  139. void pnp_remove_device(struct pnp_dev *dev)
  140. {
  141. if (!dev || dev->card)
  142. return;
  143. __pnp_remove_device(dev);
  144. }
  145. #endif /* 0 */
  146. static int __init pnp_init(void)
  147. {
  148. printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
  149. return bus_register(&pnp_bus_type);
  150. }
  151. subsys_initcall(pnp_init);
  152. #if 0
  153. EXPORT_SYMBOL(pnp_register_protocol);
  154. EXPORT_SYMBOL(pnp_unregister_protocol);
  155. EXPORT_SYMBOL(pnp_add_device);
  156. EXPORT_SYMBOL(pnp_remove_device);
  157. #endif /* 0 */