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