superhyway.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. static int superhyway_devices;
  22. static struct device superhyway_bus_device = {
  23. .init_name = "superhyway",
  24. };
  25. static void superhyway_device_release(struct device *dev)
  26. {
  27. struct superhyway_device *sdev = to_superhyway_device(dev);
  28. kfree(sdev->resource);
  29. kfree(sdev);
  30. }
  31. /**
  32. * superhyway_add_device - Add a SuperHyway module
  33. * @base: Physical address where module is mapped.
  34. * @sdev: SuperHyway device to add, or NULL to allocate a new one.
  35. * @bus: Bus where SuperHyway module resides.
  36. *
  37. * This is responsible for adding a new SuperHyway module. This sets up a new
  38. * struct superhyway_device for the module being added if @sdev == NULL.
  39. *
  40. * Devices are initially added in the order that they are scanned (from the
  41. * top-down of the memory map), and are assigned an ID based on the order that
  42. * they are added. Any manual addition of a module will thus get the ID after
  43. * the devices already discovered regardless of where it resides in memory.
  44. *
  45. * Further work can and should be done in superhyway_scan_bus(), to be sure
  46. * that any new modules are properly discovered and subsequently registered.
  47. */
  48. int superhyway_add_device(unsigned long base, struct superhyway_device *sdev,
  49. struct superhyway_bus *bus)
  50. {
  51. struct superhyway_device *dev = sdev;
  52. if (!dev) {
  53. dev = kzalloc(sizeof(struct superhyway_device), GFP_KERNEL);
  54. if (!dev)
  55. return -ENOMEM;
  56. }
  57. dev->bus = bus;
  58. superhyway_read_vcr(dev, base, &dev->vcr);
  59. if (!dev->resource) {
  60. dev->resource = kmalloc(sizeof(struct resource), GFP_KERNEL);
  61. if (!dev->resource) {
  62. kfree(dev);
  63. return -ENOMEM;
  64. }
  65. dev->resource->name = dev->name;
  66. dev->resource->start = base;
  67. dev->resource->end = dev->resource->start + 0x01000000;
  68. }
  69. dev->dev.parent = &superhyway_bus_device;
  70. dev->dev.bus = &superhyway_bus_type;
  71. dev->dev.release = superhyway_device_release;
  72. dev->id.id = dev->vcr.mod_id;
  73. sprintf(dev->name, "SuperHyway device %04x", dev->id.id);
  74. dev_set_name(&dev->dev, "%02x", superhyway_devices);
  75. superhyway_devices++;
  76. return device_register(&dev->dev);
  77. }
  78. int superhyway_add_devices(struct superhyway_bus *bus,
  79. struct superhyway_device **devices,
  80. int nr_devices)
  81. {
  82. int i, ret = 0;
  83. for (i = 0; i < nr_devices; i++) {
  84. struct superhyway_device *dev = devices[i];
  85. ret |= superhyway_add_device(dev->resource[0].start, dev, bus);
  86. }
  87. return ret;
  88. }
  89. static int __init superhyway_init(void)
  90. {
  91. struct superhyway_bus *bus;
  92. int ret;
  93. ret = device_register(&superhyway_bus_device);
  94. if (unlikely(ret))
  95. return ret;
  96. for (bus = superhyway_channels; bus->ops; bus++)
  97. ret |= superhyway_scan_bus(bus);
  98. return ret;
  99. }
  100. postcore_initcall(superhyway_init);
  101. static const struct superhyway_device_id *
  102. superhyway_match_id(const struct superhyway_device_id *ids,
  103. struct superhyway_device *dev)
  104. {
  105. while (ids->id) {
  106. if (ids->id == dev->id.id)
  107. return ids;
  108. ids++;
  109. }
  110. return NULL;
  111. }
  112. static int superhyway_device_probe(struct device *dev)
  113. {
  114. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  115. struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
  116. if (shyway_drv && shyway_drv->probe) {
  117. const struct superhyway_device_id *id;
  118. id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
  119. if (id)
  120. return shyway_drv->probe(shyway_dev, id);
  121. }
  122. return -ENODEV;
  123. }
  124. static int superhyway_device_remove(struct device *dev)
  125. {
  126. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  127. struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
  128. if (shyway_drv && shyway_drv->remove) {
  129. shyway_drv->remove(shyway_dev);
  130. return 0;
  131. }
  132. return -ENODEV;
  133. }
  134. /**
  135. * superhyway_register_driver - Register a new SuperHyway driver
  136. * @drv: SuperHyway driver to register.
  137. *
  138. * This registers the passed in @drv. Any devices matching the id table will
  139. * automatically be populated and handed off to the driver's specified probe
  140. * routine.
  141. */
  142. int superhyway_register_driver(struct superhyway_driver *drv)
  143. {
  144. drv->drv.name = drv->name;
  145. drv->drv.bus = &superhyway_bus_type;
  146. return driver_register(&drv->drv);
  147. }
  148. /**
  149. * superhyway_unregister_driver - Unregister a SuperHyway driver
  150. * @drv: SuperHyway driver to unregister.
  151. *
  152. * This cleans up after superhyway_register_driver(), and should be invoked in
  153. * the exit path of any module drivers.
  154. */
  155. void superhyway_unregister_driver(struct superhyway_driver *drv)
  156. {
  157. driver_unregister(&drv->drv);
  158. }
  159. static int superhyway_bus_match(struct device *dev, struct device_driver *drv)
  160. {
  161. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  162. struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
  163. const struct superhyway_device_id *ids = shyway_drv->id_table;
  164. if (!ids)
  165. return -EINVAL;
  166. if (superhyway_match_id(ids, shyway_dev))
  167. return 1;
  168. return -ENODEV;
  169. }
  170. struct bus_type superhyway_bus_type = {
  171. .name = "superhyway",
  172. .match = superhyway_bus_match,
  173. #ifdef CONFIG_SYSFS
  174. .dev_attrs = superhyway_dev_attrs,
  175. #endif
  176. .probe = superhyway_device_probe,
  177. .remove = superhyway_device_remove,
  178. };
  179. static int __init superhyway_bus_init(void)
  180. {
  181. return bus_register(&superhyway_bus_type);
  182. }
  183. static void __exit superhyway_bus_exit(void)
  184. {
  185. device_unregister(&superhyway_bus_device);
  186. bus_unregister(&superhyway_bus_type);
  187. }
  188. core_initcall(superhyway_bus_init);
  189. module_exit(superhyway_bus_exit);
  190. EXPORT_SYMBOL(superhyway_bus_type);
  191. EXPORT_SYMBOL(superhyway_add_device);
  192. EXPORT_SYMBOL(superhyway_add_devices);
  193. EXPORT_SYMBOL(superhyway_register_driver);
  194. EXPORT_SYMBOL(superhyway_unregister_driver);
  195. MODULE_LICENSE("GPL");