superhyway.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. .bus_id = "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. sprintf(dev->dev.bus_id, "%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 = 0;
  93. device_register(&superhyway_bus_device);
  94. for (bus = superhyway_channels; bus->ops; bus++)
  95. ret |= superhyway_scan_bus(bus);
  96. return ret;
  97. }
  98. postcore_initcall(superhyway_init);
  99. static const struct superhyway_device_id *
  100. superhyway_match_id(const struct superhyway_device_id *ids,
  101. struct superhyway_device *dev)
  102. {
  103. while (ids->id) {
  104. if (ids->id == dev->id.id)
  105. return ids;
  106. ids++;
  107. }
  108. return NULL;
  109. }
  110. static int superhyway_device_probe(struct device *dev)
  111. {
  112. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  113. struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
  114. if (shyway_drv && shyway_drv->probe) {
  115. const struct superhyway_device_id *id;
  116. id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
  117. if (id)
  118. return shyway_drv->probe(shyway_dev, id);
  119. }
  120. return -ENODEV;
  121. }
  122. static int superhyway_device_remove(struct device *dev)
  123. {
  124. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  125. struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
  126. if (shyway_drv && shyway_drv->remove) {
  127. shyway_drv->remove(shyway_dev);
  128. return 0;
  129. }
  130. return -ENODEV;
  131. }
  132. /**
  133. * superhyway_register_driver - Register a new SuperHyway driver
  134. * @drv: SuperHyway driver to register.
  135. *
  136. * This registers the passed in @drv. Any devices matching the id table will
  137. * automatically be populated and handed off to the driver's specified probe
  138. * routine.
  139. */
  140. int superhyway_register_driver(struct superhyway_driver *drv)
  141. {
  142. drv->drv.name = drv->name;
  143. drv->drv.bus = &superhyway_bus_type;
  144. return driver_register(&drv->drv);
  145. }
  146. /**
  147. * superhyway_unregister_driver - Unregister a SuperHyway driver
  148. * @drv: SuperHyway driver to unregister.
  149. *
  150. * This cleans up after superhyway_register_driver(), and should be invoked in
  151. * the exit path of any module drivers.
  152. */
  153. void superhyway_unregister_driver(struct superhyway_driver *drv)
  154. {
  155. driver_unregister(&drv->drv);
  156. }
  157. static int superhyway_bus_match(struct device *dev, struct device_driver *drv)
  158. {
  159. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  160. struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
  161. const struct superhyway_device_id *ids = shyway_drv->id_table;
  162. if (!ids)
  163. return -EINVAL;
  164. if (superhyway_match_id(ids, shyway_dev))
  165. return 1;
  166. return -ENODEV;
  167. }
  168. struct bus_type superhyway_bus_type = {
  169. .name = "superhyway",
  170. .match = superhyway_bus_match,
  171. #ifdef CONFIG_SYSFS
  172. .dev_attrs = superhyway_dev_attrs,
  173. #endif
  174. .probe = superhyway_device_probe,
  175. .remove = superhyway_device_remove,
  176. };
  177. static int __init superhyway_bus_init(void)
  178. {
  179. return bus_register(&superhyway_bus_type);
  180. }
  181. static void __exit superhyway_bus_exit(void)
  182. {
  183. device_unregister(&superhyway_bus_device);
  184. bus_unregister(&superhyway_bus_type);
  185. }
  186. core_initcall(superhyway_bus_init);
  187. module_exit(superhyway_bus_exit);
  188. EXPORT_SYMBOL(superhyway_bus_type);
  189. EXPORT_SYMBOL(superhyway_add_device);
  190. EXPORT_SYMBOL(superhyway_add_devices);
  191. EXPORT_SYMBOL(superhyway_register_driver);
  192. EXPORT_SYMBOL(superhyway_unregister_driver);
  193. MODULE_LICENSE("GPL");