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>
20 static int superhyway_devices
;
22 static struct device superhyway_bus_device
= {
23 .bus_id
= "superhyway",
26 static void superhyway_device_release(struct device
*dev
)
28 kfree(to_superhyway_device(dev
));
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.
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
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.
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.
50 int superhyway_add_device(unsigned int mod_id
, unsigned long base
,
51 unsigned long long vcr
)
53 struct superhyway_device
*dev
;
55 dev
= kmalloc(sizeof(struct superhyway_device
), GFP_KERNEL
);
59 memset(dev
, 0, sizeof(struct superhyway_device
));
62 sprintf(dev
->name
, "SuperHyway device %04x", dev
->id
.id
);
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
;
72 sprintf(dev
->dev
.bus_id
, "%02x", superhyway_devices
);
76 return device_register(&dev
->dev
);
79 static int __init
superhyway_init(void)
81 device_register(&superhyway_bus_device
);
82 return superhyway_scan_bus();
85 postcore_initcall(superhyway_init
);
87 static const struct superhyway_device_id
*
88 superhyway_match_id(const struct superhyway_device_id
*ids
,
89 struct superhyway_device
*dev
)
92 if (ids
->id
== dev
->id
.id
)
101 static int superhyway_device_probe(struct device
*dev
)
103 struct superhyway_device
*shyway_dev
= to_superhyway_device(dev
);
104 struct superhyway_driver
*shyway_drv
= to_superhyway_driver(dev
->driver
);
106 if (shyway_drv
&& shyway_drv
->probe
) {
107 const struct superhyway_device_id
*id
;
109 id
= superhyway_match_id(shyway_drv
->id_table
, shyway_dev
);
111 return shyway_drv
->probe(shyway_dev
, id
);
117 static int superhyway_device_remove(struct device
*dev
)
119 struct superhyway_device
*shyway_dev
= to_superhyway_device(dev
);
120 struct superhyway_driver
*shyway_drv
= to_superhyway_driver(dev
->driver
);
122 if (shyway_drv
&& shyway_drv
->remove
) {
123 shyway_drv
->remove(shyway_dev
);
131 * superhyway_register_driver - Register a new SuperHyway driver
132 * @drv: SuperHyway driver to register.
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
138 int superhyway_register_driver(struct superhyway_driver
*drv
)
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
;
145 return driver_register(&drv
->drv
);
149 * superhyway_unregister_driver - Unregister a SuperHyway driver
150 * @drv: SuperHyway driver to unregister.
152 * This cleans up after superhyway_register_driver(), and should be invoked in
153 * the exit path of any module drivers.
155 void superhyway_unregister_driver(struct superhyway_driver
*drv
)
157 driver_unregister(&drv
->drv
);
160 static int superhyway_bus_match(struct device
*dev
, struct device_driver
*drv
)
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
;
168 if (superhyway_match_id(ids
, shyway_dev
))
174 struct bus_type superhyway_bus_type
= {
175 .name
= "superhyway",
176 .match
= superhyway_bus_match
,
178 .dev_attrs
= superhyway_dev_attrs
,
182 static int __init
superhyway_bus_init(void)
184 return bus_register(&superhyway_bus_type
);
187 static void __exit
superhyway_bus_exit(void)
189 device_unregister(&superhyway_bus_device
);
190 bus_unregister(&superhyway_bus_type
);
193 core_initcall(superhyway_bus_init
);
194 module_exit(superhyway_bus_exit
);
196 EXPORT_SYMBOL(superhyway_bus_type
);
197 EXPORT_SYMBOL(superhyway_add_device
);
198 EXPORT_SYMBOL(superhyway_register_driver
);
199 EXPORT_SYMBOL(superhyway_unregister_driver
);
201 MODULE_LICENSE("GPL");