RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / sh / superhyway / superhyway.c
blob7d873b3b0513c03fb8fcfaa3e52b622e4cc4760f
1 /*
2 * drivers/sh/superhyway/superhyway.c
4 * SuperHyway Bus Driver
6 * Copyright (C) 2004, 2005 Paul Mundt <lethal@linux-sh.org>
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.
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>
22 static int superhyway_devices;
24 static struct device superhyway_bus_device = {
25 .bus_id = "superhyway",
28 static void superhyway_device_release(struct device *dev)
30 struct superhyway_device *sdev = to_superhyway_device(dev);
32 kfree(sdev->resource);
33 kfree(sdev);
36 /**
37 * superhyway_add_device - Add a SuperHyway module
38 * @base: Physical address where module is mapped.
39 * @sdev: SuperHyway device to add, or NULL to allocate a new one.
40 * @bus: Bus where SuperHyway module resides.
42 * This is responsible for adding a new SuperHyway module. This sets up a new
43 * struct superhyway_device for the module being added if @sdev == NULL.
45 * Devices are initially added in the order that they are scanned (from the
46 * top-down of the memory map), and are assigned an ID based on the order that
47 * they are added. Any manual addition of a module will thus get the ID after
48 * the devices already discovered regardless of where it resides in memory.
50 * Further work can and should be done in superhyway_scan_bus(), to be sure
51 * that any new modules are properly discovered and subsequently registered.
53 int superhyway_add_device(unsigned long base, struct superhyway_device *sdev,
54 struct superhyway_bus *bus)
56 struct superhyway_device *dev = sdev;
58 if (!dev) {
59 dev = kzalloc(sizeof(struct superhyway_device), GFP_KERNEL);
60 if (!dev)
61 return -ENOMEM;
65 dev->bus = bus;
66 superhyway_read_vcr(dev, base, &dev->vcr);
68 if (!dev->resource) {
69 dev->resource = kmalloc(sizeof(struct resource), GFP_KERNEL);
70 if (!dev->resource) {
71 kfree(dev);
72 return -ENOMEM;
75 dev->resource->name = dev->name;
76 dev->resource->start = base;
77 dev->resource->end = dev->resource->start + 0x01000000;
80 dev->dev.parent = &superhyway_bus_device;
81 dev->dev.bus = &superhyway_bus_type;
82 dev->dev.release = superhyway_device_release;
83 dev->id.id = dev->vcr.mod_id;
85 sprintf(dev->name, "SuperHyway device %04x", dev->id.id);
86 sprintf(dev->dev.bus_id, "%02x", superhyway_devices);
88 superhyway_devices++;
90 return device_register(&dev->dev);
93 int superhyway_add_devices(struct superhyway_bus *bus,
94 struct superhyway_device **devices,
95 int nr_devices)
97 int i, ret = 0;
99 for (i = 0; i < nr_devices; i++) {
100 struct superhyway_device *dev = devices[i];
101 ret |= superhyway_add_device(dev->resource[0].start, dev, bus);
104 return ret;
107 static int __init superhyway_init(void)
109 struct superhyway_bus *bus;
110 int ret = 0;
112 device_register(&superhyway_bus_device);
114 for (bus = superhyway_channels; bus->ops; bus++)
115 ret |= superhyway_scan_bus(bus);
117 return ret;
120 postcore_initcall(superhyway_init);
122 static const struct superhyway_device_id *
123 superhyway_match_id(const struct superhyway_device_id *ids,
124 struct superhyway_device *dev)
126 while (ids->id) {
127 if (ids->id == dev->id.id)
128 return ids;
130 ids++;
133 return NULL;
136 static int superhyway_device_probe(struct device *dev)
138 struct superhyway_device *shyway_dev = to_superhyway_device(dev);
139 struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
141 if (shyway_drv && shyway_drv->probe) {
142 const struct superhyway_device_id *id;
144 id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
145 if (id)
146 return shyway_drv->probe(shyway_dev, id);
149 return -ENODEV;
152 static int superhyway_device_remove(struct device *dev)
154 struct superhyway_device *shyway_dev = to_superhyway_device(dev);
155 struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
157 if (shyway_drv && shyway_drv->remove) {
158 shyway_drv->remove(shyway_dev);
159 return 0;
162 return -ENODEV;
166 * superhyway_register_driver - Register a new SuperHyway driver
167 * @drv: SuperHyway driver to register.
169 * This registers the passed in @drv. Any devices matching the id table will
170 * automatically be populated and handed off to the driver's specified probe
171 * routine.
173 int superhyway_register_driver(struct superhyway_driver *drv)
175 drv->drv.name = drv->name;
176 drv->drv.bus = &superhyway_bus_type;
178 return driver_register(&drv->drv);
182 * superhyway_unregister_driver - Unregister a SuperHyway driver
183 * @drv: SuperHyway driver to unregister.
185 * This cleans up after superhyway_register_driver(), and should be invoked in
186 * the exit path of any module drivers.
188 void superhyway_unregister_driver(struct superhyway_driver *drv)
190 driver_unregister(&drv->drv);
193 static int superhyway_bus_match(struct device *dev, struct device_driver *drv)
195 struct superhyway_device *shyway_dev = to_superhyway_device(dev);
196 struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
197 const struct superhyway_device_id *ids = shyway_drv->id_table;
199 if (!ids)
200 return -EINVAL;
201 if (superhyway_match_id(ids, shyway_dev))
202 return 1;
204 return -ENODEV;
207 struct bus_type superhyway_bus_type = {
208 .name = "superhyway",
209 .match = superhyway_bus_match,
210 #ifdef CONFIG_SYSFS
211 .dev_attrs = superhyway_dev_attrs,
212 #endif
213 .probe = superhyway_device_probe,
214 .remove = superhyway_device_remove,
217 static int __init superhyway_bus_init(void)
219 return bus_register(&superhyway_bus_type);
222 static void __exit superhyway_bus_exit(void)
224 device_unregister(&superhyway_bus_device);
225 bus_unregister(&superhyway_bus_type);
228 core_initcall(superhyway_bus_init);
229 module_exit(superhyway_bus_exit);
231 EXPORT_SYMBOL(superhyway_bus_type);
232 EXPORT_SYMBOL(superhyway_add_device);
233 EXPORT_SYMBOL(superhyway_add_devices);
234 EXPORT_SYMBOL(superhyway_register_driver);
235 EXPORT_SYMBOL(superhyway_unregister_driver);
237 MODULE_LICENSE("GPL");