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. .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 = kmalloc(sizeof(struct superhyway_device), GFP_KERNEL);
  54. if (!dev)
  55. return -ENOMEM;
  56. memset(dev, 0, sizeof(struct superhyway_device));
  57. }
  58. dev->bus = bus;
  59. superhyway_read_vcr(dev, base, &dev->vcr);
  60. if (!dev->resource) {
  61. dev->resource = kmalloc(sizeof(struct resource), GFP_KERNEL);
  62. if (!dev->resource) {
  63. kfree(dev);
  64. return -ENOMEM;
  65. }
  66. dev->resource->name = dev->name;
  67. dev->resource->start = base;
  68. dev->resource->end = dev->resource->start + 0x01000000;
  69. }
  70. dev->dev.parent = &superhyway_bus_device;
  71. dev->dev.bus = &superhyway_bus_type;
  72. dev->dev.release = superhyway_device_release;
  73. dev->id.id = dev->vcr.mod_id;
  74. sprintf(dev->name, "SuperHyway device %04x", dev->id.id);
  75. sprintf(dev->dev.bus_id, "%02x", superhyway_devices);
  76. superhyway_devices++;
  77. return device_register(&dev->dev);
  78. }
  79. int superhyway_add_devices(struct superhyway_bus *bus,
  80. struct superhyway_device **devices,
  81. int nr_devices)
  82. {
  83. int i, ret = 0;
  84. for (i = 0; i < nr_devices; i++) {
  85. struct superhyway_device *dev = devices[i];
  86. ret |= superhyway_add_device(dev->resource[0].start, dev, bus);
  87. }
  88. return ret;
  89. }
  90. static int __init superhyway_init(void)
  91. {
  92. struct superhyway_bus *bus;
  93. int ret = 0;
  94. device_register(&superhyway_bus_device);
  95. for (bus = superhyway_channels; bus->ops; bus++)
  96. ret |= superhyway_scan_bus(bus);
  97. return ret;
  98. }
  99. postcore_initcall(superhyway_init);
  100. static const struct superhyway_device_id *
  101. superhyway_match_id(const struct superhyway_device_id *ids,
  102. struct superhyway_device *dev)
  103. {
  104. while (ids->id) {
  105. if (ids->id == dev->id.id)
  106. return ids;
  107. ids++;
  108. }
  109. return NULL;
  110. }
  111. static int superhyway_device_probe(struct device *dev)
  112. {
  113. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  114. struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
  115. if (shyway_drv && shyway_drv->probe) {
  116. const struct superhyway_device_id *id;
  117. id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
  118. if (id)
  119. return shyway_drv->probe(shyway_dev, id);
  120. }
  121. return -ENODEV;
  122. }
  123. static int superhyway_device_remove(struct device *dev)
  124. {
  125. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  126. struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
  127. if (shyway_drv && shyway_drv->remove) {
  128. shyway_drv->remove(shyway_dev);
  129. return 0;
  130. }
  131. return -ENODEV;
  132. }
  133. /**
  134. * superhyway_register_driver - Register a new SuperHyway driver
  135. * @drv: SuperHyway driver to register.
  136. *
  137. * This registers the passed in @drv. Any devices matching the id table will
  138. * automatically be populated and handed off to the driver's specified probe
  139. * routine.
  140. */
  141. int superhyway_register_driver(struct superhyway_driver *drv)
  142. {
  143. drv->drv.name = drv->name;
  144. drv->drv.bus = &superhyway_bus_type;
  145. return driver_register(&drv->drv);
  146. }
  147. /**
  148. * superhyway_unregister_driver - Unregister a SuperHyway driver
  149. * @drv: SuperHyway driver to unregister.
  150. *
  151. * This cleans up after superhyway_register_driver(), and should be invoked in
  152. * the exit path of any module drivers.
  153. */
  154. void superhyway_unregister_driver(struct superhyway_driver *drv)
  155. {
  156. driver_unregister(&drv->drv);
  157. }
  158. static int superhyway_bus_match(struct device *dev, struct device_driver *drv)
  159. {
  160. struct superhyway_device *shyway_dev = to_superhyway_device(dev);
  161. struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
  162. const struct superhyway_device_id *ids = shyway_drv->id_table;
  163. if (!ids)
  164. return -EINVAL;
  165. if (superhyway_match_id(ids, shyway_dev))
  166. return 1;
  167. return -ENODEV;
  168. }
  169. struct bus_type superhyway_bus_type = {
  170. .name = "superhyway",
  171. .match = superhyway_bus_match,
  172. #ifdef CONFIG_SYSFS
  173. .dev_attrs = superhyway_dev_attrs,
  174. #endif
  175. .probe = superhyway_device_probe,
  176. .remove = superhyway_device_remove,
  177. };
  178. static int __init superhyway_bus_init(void)
  179. {
  180. return bus_register(&superhyway_bus_type);
  181. }
  182. static void __exit superhyway_bus_exit(void)
  183. {
  184. device_unregister(&superhyway_bus_device);
  185. bus_unregister(&superhyway_bus_type);
  186. }
  187. core_initcall(superhyway_bus_init);
  188. module_exit(superhyway_bus_exit);
  189. EXPORT_SYMBOL(superhyway_bus_type);
  190. EXPORT_SYMBOL(superhyway_add_device);
  191. EXPORT_SYMBOL(superhyway_add_devices);
  192. EXPORT_SYMBOL(superhyway_register_driver);
  193. EXPORT_SYMBOL(superhyway_unregister_driver);
  194. MODULE_LICENSE("GPL");