OpenCloudOS-Kernel/drivers/sh/superhyway/superhyway.c

235 lines
5.9 KiB
C
Raw Normal View History

/*
* drivers/sh/superhyway/superhyway.c
*
* SuperHyway Bus Driver
*
* Copyright (C) 2004, 2005 Paul Mundt <lethal@linux-sh.org>
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/superhyway.h>
#include <linux/string.h>
#include <linux/slab.h>
static int superhyway_devices;
static struct device superhyway_bus_device = {
.init_name = "superhyway",
};
static void superhyway_device_release(struct device *dev)
{
struct superhyway_device *sdev = to_superhyway_device(dev);
kfree(sdev->resource);
kfree(sdev);
}
/**
* superhyway_add_device - Add a SuperHyway module
* @base: Physical address where module is mapped.
* @sdev: SuperHyway device to add, or NULL to allocate a new one.
* @bus: Bus where SuperHyway module resides.
*
* This is responsible for adding a new SuperHyway module. This sets up a new
* struct superhyway_device for the module being added if @sdev == NULL.
*
* Devices are initially added in the order that they are scanned (from the
* top-down of the memory map), and are assigned an ID based on the order that
* they are added. Any manual addition of a module will thus get the ID after
* the devices already discovered regardless of where it resides in memory.
*
* Further work can and should be done in superhyway_scan_bus(), to be sure
* that any new modules are properly discovered and subsequently registered.
*/
int superhyway_add_device(unsigned long base, struct superhyway_device *sdev,
struct superhyway_bus *bus)
{
struct superhyway_device *dev = sdev;
if (!dev) {
2007-07-19 16:49:03 +08:00
dev = kzalloc(sizeof(struct superhyway_device), GFP_KERNEL);
if (!dev)
return -ENOMEM;
}
dev->bus = bus;
superhyway_read_vcr(dev, base, &dev->vcr);
if (!dev->resource) {
dev->resource = kzalloc(sizeof(struct resource), GFP_KERNEL);
if (!dev->resource) {
kfree(dev);
return -ENOMEM;
}
dev->resource->name = dev->name;
dev->resource->start = base;
dev->resource->end = dev->resource->start + 0x01000000;
}
dev->dev.parent = &superhyway_bus_device;
dev->dev.bus = &superhyway_bus_type;
dev->dev.release = superhyway_device_release;
dev->id.id = dev->vcr.mod_id;
sprintf(dev->name, "SuperHyway device %04x", dev->id.id);
dev_set_name(&dev->dev, "%02x", superhyway_devices);
superhyway_devices++;
return device_register(&dev->dev);
}
int superhyway_add_devices(struct superhyway_bus *bus,
struct superhyway_device **devices,
int nr_devices)
{
int i, ret = 0;
for (i = 0; i < nr_devices; i++) {
struct superhyway_device *dev = devices[i];
ret |= superhyway_add_device(dev->resource[0].start, dev, bus);
}
return ret;
}
static int __init superhyway_init(void)
{
struct superhyway_bus *bus;
int ret;
ret = device_register(&superhyway_bus_device);
if (unlikely(ret))
return ret;
for (bus = superhyway_channels; bus->ops; bus++)
ret |= superhyway_scan_bus(bus);
return ret;
}
postcore_initcall(superhyway_init);
static const struct superhyway_device_id *
superhyway_match_id(const struct superhyway_device_id *ids,
struct superhyway_device *dev)
{
while (ids->id) {
if (ids->id == dev->id.id)
return ids;
ids++;
}
return NULL;
}
static int superhyway_device_probe(struct device *dev)
{
struct superhyway_device *shyway_dev = to_superhyway_device(dev);
struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
if (shyway_drv && shyway_drv->probe) {
const struct superhyway_device_id *id;
id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
if (id)
return shyway_drv->probe(shyway_dev, id);
}
return -ENODEV;
}
bus: Make remove callback return void The driver core ignores the return value of this callback because there is only little it can do when a device disappears. This is the final bit of a long lasting cleanup quest where several buses were converted to also return void from their remove callback. Additionally some resource leaks were fixed that were caused by drivers returning an error code in the expectation that the driver won't go away. With struct bus_type::remove returning void it's prevented that newly implemented buses return an ignored error code and so don't anticipate wrong expectations for driver authors. Reviewed-by: Tom Rix <trix@redhat.com> (For fpga) Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org> Reviewed-by: Cornelia Huck <cohuck@redhat.com> (For drivers/s390 and drivers/vfio) Acked-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk> (For ARM, Amba and related parts) Acked-by: Mark Brown <broonie@kernel.org> Acked-by: Chen-Yu Tsai <wens@csie.org> (for sunxi-rsb) Acked-by: Pali Rohár <pali@kernel.org> Acked-by: Mauro Carvalho Chehab <mchehab@kernel.org> (for media) Acked-by: Hans de Goede <hdegoede@redhat.com> (For drivers/platform) Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Acked-By: Vinod Koul <vkoul@kernel.org> Acked-by: Juergen Gross <jgross@suse.com> (For xen) Acked-by: Lee Jones <lee.jones@linaro.org> (For mfd) Acked-by: Johannes Thumshirn <jth@kernel.org> (For mcb) Acked-by: Johan Hovold <johan@kernel.org> Acked-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org> (For slimbus) Acked-by: Kirti Wankhede <kwankhede@nvidia.com> (For vfio) Acked-by: Maximilian Luz <luzmaximilian@gmail.com> Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com> (For ulpi and typec) Acked-by: Samuel Iglesias Gonsálvez <siglesias@igalia.com> (For ipack) Acked-by: Geoff Levand <geoff@infradead.org> (For ps3) Acked-by: Yehezkel Bernat <YehezkelShB@gmail.com> (For thunderbolt) Acked-by: Alexander Shishkin <alexander.shishkin@linux.intel.com> (For intel_th) Acked-by: Dominik Brodowski <linux@dominikbrodowski.net> (For pcmcia) Acked-by: Rafael J. Wysocki <rafael@kernel.org> (For ACPI) Acked-by: Bjorn Andersson <bjorn.andersson@linaro.org> (rpmsg and apr) Acked-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com> (For intel-ish-hid) Acked-by: Dan Williams <dan.j.williams@intel.com> (For CXL, DAX, and NVDIMM) Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com> (For isa) Acked-by: Stefan Richter <stefanr@s5r6.in-berlin.de> (For firewire) Acked-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> (For hid) Acked-by: Thorsten Scherer <t.scherer@eckelmann.de> (For siox) Acked-by: Sven Van Asbroeck <TheSven73@gmail.com> (For anybuss) Acked-by: Ulf Hansson <ulf.hansson@linaro.org> (For MMC) Acked-by: Wolfram Sang <wsa@kernel.org> # for I2C Acked-by: Sudeep Holla <sudeep.holla@arm.com> Acked-by: Geert Uytterhoeven <geert@linux-m68k.org> Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Acked-by: Finn Thain <fthain@linux-m68k.org> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Link: https://lore.kernel.org/r/20210713193522.1770306-6-u.kleine-koenig@pengutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-07-14 03:35:22 +08:00
static void superhyway_device_remove(struct device *dev)
{
struct superhyway_device *shyway_dev = to_superhyway_device(dev);
struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
if (shyway_drv->remove)
shyway_drv->remove(shyway_dev);
}
/**
* superhyway_register_driver - Register a new SuperHyway driver
* @drv: SuperHyway driver to register.
*
* This registers the passed in @drv. Any devices matching the id table will
* automatically be populated and handed off to the driver's specified probe
* routine.
*/
int superhyway_register_driver(struct superhyway_driver *drv)
{
drv->drv.name = drv->name;
drv->drv.bus = &superhyway_bus_type;
return driver_register(&drv->drv);
}
/**
* superhyway_unregister_driver - Unregister a SuperHyway driver
* @drv: SuperHyway driver to unregister.
*
* This cleans up after superhyway_register_driver(), and should be invoked in
* the exit path of any module drivers.
*/
void superhyway_unregister_driver(struct superhyway_driver *drv)
{
driver_unregister(&drv->drv);
}
static int superhyway_bus_match(struct device *dev, struct device_driver *drv)
{
struct superhyway_device *shyway_dev = to_superhyway_device(dev);
struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
const struct superhyway_device_id *ids = shyway_drv->id_table;
if (!ids)
return -EINVAL;
if (superhyway_match_id(ids, shyway_dev))
return 1;
return -ENODEV;
}
struct bus_type superhyway_bus_type = {
.name = "superhyway",
.match = superhyway_bus_match,
#ifdef CONFIG_SYSFS
.dev_groups = superhyway_dev_groups,
#endif
.probe = superhyway_device_probe,
.remove = superhyway_device_remove,
};
static int __init superhyway_bus_init(void)
{
return bus_register(&superhyway_bus_type);
}
static void __exit superhyway_bus_exit(void)
{
device_unregister(&superhyway_bus_device);
bus_unregister(&superhyway_bus_type);
}
core_initcall(superhyway_bus_init);
module_exit(superhyway_bus_exit);
EXPORT_SYMBOL(superhyway_bus_type);
EXPORT_SYMBOL(superhyway_add_device);
EXPORT_SYMBOL(superhyway_add_devices);
EXPORT_SYMBOL(superhyway_register_driver);
EXPORT_SYMBOL(superhyway_unregister_driver);
MODULE_LICENSE("GPL");