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