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
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 kfree(to_superhyway_device(dev
));
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.
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
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.
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.
52 int superhyway_add_device(unsigned int mod_id
, unsigned long base
,
53 unsigned long long vcr
)
55 struct superhyway_device
*dev
;
57 dev
= kmalloc(sizeof(struct superhyway_device
), GFP_KERNEL
);
61 memset(dev
, 0, sizeof(struct superhyway_device
));
64 sprintf(dev
->name
, "SuperHyway device %04x", dev
->id
.id
);
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
;
74 sprintf(dev
->dev
.bus_id
, "%02x", superhyway_devices
);
78 return device_register(&dev
->dev
);
81 static int __init
superhyway_init(void)
83 device_register(&superhyway_bus_device
);
84 return superhyway_scan_bus();
87 postcore_initcall(superhyway_init
);
89 static const struct superhyway_device_id
*
90 superhyway_match_id(const struct superhyway_device_id
*ids
,
91 struct superhyway_device
*dev
)
94 if (ids
->id
== dev
->id
.id
)
103 static int superhyway_device_probe(struct device
*dev
)
105 struct superhyway_device
*shyway_dev
= to_superhyway_device(dev
);
106 struct superhyway_driver
*shyway_drv
= to_superhyway_driver(dev
->driver
);
108 if (shyway_drv
&& shyway_drv
->probe
) {
109 const struct superhyway_device_id
*id
;
111 id
= superhyway_match_id(shyway_drv
->id_table
, shyway_dev
);
113 return shyway_drv
->probe(shyway_dev
, id
);
119 static int superhyway_device_remove(struct device
*dev
)
121 struct superhyway_device
*shyway_dev
= to_superhyway_device(dev
);
122 struct superhyway_driver
*shyway_drv
= to_superhyway_driver(dev
->driver
);
124 if (shyway_drv
&& shyway_drv
->remove
) {
125 shyway_drv
->remove(shyway_dev
);
133 * superhyway_register_driver - Register a new SuperHyway driver
134 * @drv: SuperHyway driver to register.
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
140 int superhyway_register_driver(struct superhyway_driver
*drv
)
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
;
147 return driver_register(&drv
->drv
);
151 * superhyway_unregister_driver - Unregister a SuperHyway driver
152 * @drv: SuperHyway driver to unregister.
154 * This cleans up after superhyway_register_driver(), and should be invoked in
155 * the exit path of any module drivers.
157 void superhyway_unregister_driver(struct superhyway_driver
*drv
)
159 driver_unregister(&drv
->drv
);
162 static int superhyway_bus_match(struct device
*dev
, struct device_driver
*drv
)
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
;
170 if (superhyway_match_id(ids
, shyway_dev
))
176 struct bus_type superhyway_bus_type
= {
177 .name
= "superhyway",
178 .match
= superhyway_bus_match
,
180 .dev_attrs
= superhyway_dev_attrs
,
184 static int __init
superhyway_bus_init(void)
186 return bus_register(&superhyway_bus_type
);
189 static void __exit
superhyway_bus_exit(void)
191 device_unregister(&superhyway_bus_device
);
192 bus_unregister(&superhyway_bus_type
);
195 core_initcall(superhyway_bus_init
);
196 module_exit(superhyway_bus_exit
);
198 EXPORT_SYMBOL(superhyway_bus_type
);
199 EXPORT_SYMBOL(superhyway_add_device
);
200 EXPORT_SYMBOL(superhyway_register_driver
);
201 EXPORT_SYMBOL(superhyway_unregister_driver
);
203 MODULE_LICENSE("GPL");