superhyway.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * drivers/sh/superhyway/superhyway.c
  3. *
  4. * SuperHyway Bus Driver
  5. *
  6. * Copyright (C) 2004, 2005 Paul Mundt <lethal@linux-sh.org>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/device.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/list.h>
  18. #include <linux/superhyway.h>
  19. static int superhyway_devices;
  20. static struct device superhyway_bus_device = {
  21. .bus_id = "superhyway",
  22. };
  23. static void superhyway_device_release(struct device *dev)
  24. {
  25. kfree(to_superhyway_device(dev));
  26. }
  27. /**
  28. * superhyway_add_device - Add a SuperHyway module
  29. * @mod_id: Module ID (taken from MODULE.VCR.MOD_ID).
  30. * @base: Physical address where module is mapped.
  31. * @vcr: VCR value.
  32. *
  33. * This is responsible for adding a new SuperHyway module. This sets up a new
  34. * struct superhyway_device for the module being added. Each one of @mod_id,
  35. * @base, and @vcr are registered with the new device for further use
  36. * elsewhere.
  37. *
  38. * Devices are initially added in the order that they are scanned (from the
  39. * top-down of the memory map), and are assigned an ID based on the order that
  40. * they are added. Any manual addition of a module will thus get the ID after
  41. * the devices already discovered regardless of where it resides in memory.
  42. *
  43. * Further work can and should be done in superhyway_scan_bus(), to be sure
  44. * that any new modules are properly discovered and subsequently registered.
  45. */
  46. int superhyway_add_device(unsigned int mod_id, unsigned long base,
  47. unsigned long long vcr)
  48. {
  49. struct superhyway_device *dev;
  50. dev = kmalloc(sizeof(struct superhyway_device), GFP_KERNEL);
  51. if (!dev)
  52. return -ENOMEM;
  53. memset(dev, 0, sizeof(struct superhyway_device));
  54. dev->id.id = mod_id;
  55. sprintf(dev->name, "SuperHyway device %04x", dev->id.id);
  56. dev->vcr = *((struct vcr_info *)(&vcr));
  57. dev->resource.name = dev->name;
  58. dev->resource.start = base;
  59. dev->resource.end = dev->resource.start + 0x01000000;
  60. dev->dev.parent = &superhyway_bus_device;
  61. dev->dev.bus = &superhyway_bus_type;
  62. dev->dev.release = superhyway_device_release;
  63. sprintf(dev->dev.bus_id, "%02x", superhyway_devices);
  64. superhyway_devices++;
  65. return device_register(&dev->dev);
  66. }
  67. static int __init superhyway_init(void)
  68. {
  69. device_register(&superhyway_bus_device);
  70. return superhyway_scan_bus();
  71. }
  72. postcore_initcall(superhyway_init);
  73. static const struct superhyway_device_id *
  74. superhyway_match_id(const struct superhyway_device_id *ids,
  75. struct superhyway_device *dev)
  76. {
  77. while (ids->id) {
  78. if (ids->id == dev->id.id)
  79. return ids;
  80. ids++;
  81. }
  82. return NULL;
  83. }
  84. static int superhyway_device_probe(struct device *dev)
  85. {
  86. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  87. struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
  88. if (shyway_drv && shyway_drv->probe) {
  89. const struct superhyway_device_id *id;
  90. id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
  91. if (id)
  92. return shyway_drv->probe(shyway_dev, id);
  93. }
  94. return -ENODEV;
  95. }
  96. static int superhyway_device_remove(struct device *dev)
  97. {
  98. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  99. struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
  100. if (shyway_drv && shyway_drv->remove) {
  101. shyway_drv->remove(shyway_dev);
  102. return 0;
  103. }
  104. return -ENODEV;
  105. }
  106. /**
  107. * superhyway_register_driver - Register a new SuperHyway driver
  108. * @drv: SuperHyway driver to register.
  109. *
  110. * This registers the passed in @drv. Any devices matching the id table will
  111. * automatically be populated and handed off to the driver's specified probe
  112. * routine.
  113. */
  114. int superhyway_register_driver(struct superhyway_driver *drv)
  115. {
  116. drv->drv.name = drv->name;
  117. drv->drv.bus = &superhyway_bus_type;
  118. drv->drv.probe = superhyway_device_probe;
  119. drv->drv.remove = superhyway_device_remove;
  120. return driver_register(&drv->drv);
  121. }
  122. /**
  123. * superhyway_unregister_driver - Unregister a SuperHyway driver
  124. * @drv: SuperHyway driver to unregister.
  125. *
  126. * This cleans up after superhyway_register_driver(), and should be invoked in
  127. * the exit path of any module drivers.
  128. */
  129. void superhyway_unregister_driver(struct superhyway_driver *drv)
  130. {
  131. driver_unregister(&drv->drv);
  132. }
  133. static int superhyway_bus_match(struct device *dev, struct device_driver *drv)
  134. {
  135. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  136. struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
  137. const struct superhyway_device_id *ids = shyway_drv->id_table;
  138. if (!ids)
  139. return -EINVAL;
  140. if (superhyway_match_id(ids, shyway_dev))
  141. return 1;
  142. return -ENODEV;
  143. }
  144. struct bus_type superhyway_bus_type = {
  145. .name = "superhyway",
  146. .match = superhyway_bus_match,
  147. #ifdef CONFIG_SYSFS
  148. .dev_attrs = superhyway_dev_attrs,
  149. #endif
  150. };
  151. static int __init superhyway_bus_init(void)
  152. {
  153. return bus_register(&superhyway_bus_type);
  154. }
  155. static void __exit superhyway_bus_exit(void)
  156. {
  157. device_unregister(&superhyway_bus_device);
  158. bus_unregister(&superhyway_bus_type);
  159. }
  160. core_initcall(superhyway_bus_init);
  161. module_exit(superhyway_bus_exit);
  162. EXPORT_SYMBOL(superhyway_bus_type);
  163. EXPORT_SYMBOL(superhyway_add_device);
  164. EXPORT_SYMBOL(superhyway_register_driver);
  165. EXPORT_SYMBOL(superhyway_unregister_driver);
  166. MODULE_LICENSE("GPL");