xref: /linux/drivers/mcb/mcb-core.c (revision 803f1ca60d5c0107adfbce4e2d70488598b03a80)
13764e82eSJohannes Thumshirn /*
23764e82eSJohannes Thumshirn  * MEN Chameleon Bus.
33764e82eSJohannes Thumshirn  *
43764e82eSJohannes Thumshirn  * Copyright (C) 2013 MEN Mikroelektronik GmbH (www.men.de)
53764e82eSJohannes Thumshirn  * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
63764e82eSJohannes Thumshirn  *
73764e82eSJohannes Thumshirn  * This program is free software; you can redistribute it and/or modify it
83764e82eSJohannes Thumshirn  * under the terms of the GNU General Public License as published by the Free
93764e82eSJohannes Thumshirn  * Software Foundation; version 2 of the License.
103764e82eSJohannes Thumshirn  */
113764e82eSJohannes Thumshirn #include <linux/kernel.h>
123764e82eSJohannes Thumshirn #include <linux/module.h>
133764e82eSJohannes Thumshirn #include <linux/slab.h>
143764e82eSJohannes Thumshirn #include <linux/types.h>
153764e82eSJohannes Thumshirn #include <linux/idr.h>
163764e82eSJohannes Thumshirn #include <linux/mcb.h>
173764e82eSJohannes Thumshirn 
183764e82eSJohannes Thumshirn static DEFINE_IDA(mcb_ida);
193764e82eSJohannes Thumshirn 
203764e82eSJohannes Thumshirn static const struct mcb_device_id *mcb_match_id(const struct mcb_device_id *ids,
213764e82eSJohannes Thumshirn 						struct mcb_device *dev)
223764e82eSJohannes Thumshirn {
233764e82eSJohannes Thumshirn 	if (ids) {
243764e82eSJohannes Thumshirn 		while (ids->device) {
253764e82eSJohannes Thumshirn 			if (ids->device == dev->id)
263764e82eSJohannes Thumshirn 				return ids;
273764e82eSJohannes Thumshirn 			ids++;
283764e82eSJohannes Thumshirn 		}
293764e82eSJohannes Thumshirn 	}
303764e82eSJohannes Thumshirn 
313764e82eSJohannes Thumshirn 	return NULL;
323764e82eSJohannes Thumshirn }
333764e82eSJohannes Thumshirn 
343764e82eSJohannes Thumshirn static int mcb_match(struct device *dev, struct device_driver *drv)
353764e82eSJohannes Thumshirn {
363764e82eSJohannes Thumshirn 	struct mcb_driver *mdrv = to_mcb_driver(drv);
373764e82eSJohannes Thumshirn 	struct mcb_device *mdev = to_mcb_device(dev);
383764e82eSJohannes Thumshirn 	const struct mcb_device_id *found_id;
393764e82eSJohannes Thumshirn 
403764e82eSJohannes Thumshirn 	found_id = mcb_match_id(mdrv->id_table, mdev);
413764e82eSJohannes Thumshirn 	if (found_id)
423764e82eSJohannes Thumshirn 		return 1;
433764e82eSJohannes Thumshirn 
443764e82eSJohannes Thumshirn 	return 0;
453764e82eSJohannes Thumshirn }
463764e82eSJohannes Thumshirn 
473764e82eSJohannes Thumshirn static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env)
483764e82eSJohannes Thumshirn {
493764e82eSJohannes Thumshirn 	struct mcb_device *mdev = to_mcb_device(dev);
503764e82eSJohannes Thumshirn 	int ret;
513764e82eSJohannes Thumshirn 
523764e82eSJohannes Thumshirn 	ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id);
533764e82eSJohannes Thumshirn 	if (ret)
543764e82eSJohannes Thumshirn 		return -ENOMEM;
553764e82eSJohannes Thumshirn 
563764e82eSJohannes Thumshirn 	return 0;
573764e82eSJohannes Thumshirn }
583764e82eSJohannes Thumshirn 
593764e82eSJohannes Thumshirn static int mcb_probe(struct device *dev)
603764e82eSJohannes Thumshirn {
613764e82eSJohannes Thumshirn 	struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
623764e82eSJohannes Thumshirn 	struct mcb_device *mdev = to_mcb_device(dev);
633764e82eSJohannes Thumshirn 	const struct mcb_device_id *found_id;
643764e82eSJohannes Thumshirn 
653764e82eSJohannes Thumshirn 	found_id = mcb_match_id(mdrv->id_table, mdev);
663764e82eSJohannes Thumshirn 	if (!found_id)
673764e82eSJohannes Thumshirn 		return -ENODEV;
683764e82eSJohannes Thumshirn 
693764e82eSJohannes Thumshirn 	return mdrv->probe(mdev, found_id);
703764e82eSJohannes Thumshirn }
713764e82eSJohannes Thumshirn 
723764e82eSJohannes Thumshirn static int mcb_remove(struct device *dev)
733764e82eSJohannes Thumshirn {
743764e82eSJohannes Thumshirn 	struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
753764e82eSJohannes Thumshirn 	struct mcb_device *mdev = to_mcb_device(dev);
763764e82eSJohannes Thumshirn 
773764e82eSJohannes Thumshirn 	mdrv->remove(mdev);
783764e82eSJohannes Thumshirn 
793764e82eSJohannes Thumshirn 	put_device(&mdev->dev);
803764e82eSJohannes Thumshirn 
813764e82eSJohannes Thumshirn 	return 0;
823764e82eSJohannes Thumshirn }
833764e82eSJohannes Thumshirn 
843764e82eSJohannes Thumshirn static void mcb_shutdown(struct device *dev)
853764e82eSJohannes Thumshirn {
863764e82eSJohannes Thumshirn 	struct mcb_device *mdev = to_mcb_device(dev);
873764e82eSJohannes Thumshirn 	struct mcb_driver *mdrv = mdev->driver;
883764e82eSJohannes Thumshirn 
893764e82eSJohannes Thumshirn 	if (mdrv && mdrv->shutdown)
903764e82eSJohannes Thumshirn 		mdrv->shutdown(mdev);
913764e82eSJohannes Thumshirn }
923764e82eSJohannes Thumshirn 
93*803f1ca6SJohannes Thumshirn static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
94*803f1ca6SJohannes Thumshirn 			 char *buf)
95*803f1ca6SJohannes Thumshirn {
96*803f1ca6SJohannes Thumshirn 	struct mcb_bus *bus = to_mcb_bus(dev);
97*803f1ca6SJohannes Thumshirn 
98*803f1ca6SJohannes Thumshirn 	return scnprintf(buf, PAGE_SIZE, "%d\n", bus->revision);
99*803f1ca6SJohannes Thumshirn }
100*803f1ca6SJohannes Thumshirn static DEVICE_ATTR_RO(revision);
101*803f1ca6SJohannes Thumshirn 
102*803f1ca6SJohannes Thumshirn static ssize_t model_show(struct device *dev, struct device_attribute *attr,
103*803f1ca6SJohannes Thumshirn 			 char *buf)
104*803f1ca6SJohannes Thumshirn {
105*803f1ca6SJohannes Thumshirn 	struct mcb_bus *bus = to_mcb_bus(dev);
106*803f1ca6SJohannes Thumshirn 
107*803f1ca6SJohannes Thumshirn 	return scnprintf(buf, PAGE_SIZE, "%c\n", bus->model);
108*803f1ca6SJohannes Thumshirn }
109*803f1ca6SJohannes Thumshirn static DEVICE_ATTR_RO(model);
110*803f1ca6SJohannes Thumshirn 
111*803f1ca6SJohannes Thumshirn static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
112*803f1ca6SJohannes Thumshirn 			 char *buf)
113*803f1ca6SJohannes Thumshirn {
114*803f1ca6SJohannes Thumshirn 	struct mcb_bus *bus = to_mcb_bus(dev);
115*803f1ca6SJohannes Thumshirn 
116*803f1ca6SJohannes Thumshirn 	return scnprintf(buf, PAGE_SIZE, "%d\n", bus->minor);
117*803f1ca6SJohannes Thumshirn }
118*803f1ca6SJohannes Thumshirn static DEVICE_ATTR_RO(minor);
119*803f1ca6SJohannes Thumshirn 
120*803f1ca6SJohannes Thumshirn static ssize_t name_show(struct device *dev, struct device_attribute *attr,
121*803f1ca6SJohannes Thumshirn 			 char *buf)
122*803f1ca6SJohannes Thumshirn {
123*803f1ca6SJohannes Thumshirn 	struct mcb_bus *bus = to_mcb_bus(dev);
124*803f1ca6SJohannes Thumshirn 
125*803f1ca6SJohannes Thumshirn 	return scnprintf(buf, PAGE_SIZE, "%s\n", bus->name);
126*803f1ca6SJohannes Thumshirn }
127*803f1ca6SJohannes Thumshirn static DEVICE_ATTR_RO(name);
128*803f1ca6SJohannes Thumshirn 
129*803f1ca6SJohannes Thumshirn static struct attribute *mcb_bus_attrs[] = {
130*803f1ca6SJohannes Thumshirn 	&dev_attr_revision.attr,
131*803f1ca6SJohannes Thumshirn 	&dev_attr_model.attr,
132*803f1ca6SJohannes Thumshirn 	&dev_attr_minor.attr,
133*803f1ca6SJohannes Thumshirn 	&dev_attr_name.attr,
134*803f1ca6SJohannes Thumshirn 	NULL,
135*803f1ca6SJohannes Thumshirn };
136*803f1ca6SJohannes Thumshirn 
137*803f1ca6SJohannes Thumshirn static const struct attribute_group mcb_carrier_group = {
138*803f1ca6SJohannes Thumshirn 	.attrs = mcb_bus_attrs,
139*803f1ca6SJohannes Thumshirn };
140*803f1ca6SJohannes Thumshirn 
141*803f1ca6SJohannes Thumshirn static const struct attribute_group *mcb_carrier_groups[] = {
142*803f1ca6SJohannes Thumshirn 	&mcb_carrier_group,
143*803f1ca6SJohannes Thumshirn 	NULL,
144*803f1ca6SJohannes Thumshirn };
145*803f1ca6SJohannes Thumshirn 
146*803f1ca6SJohannes Thumshirn 
1473764e82eSJohannes Thumshirn static struct bus_type mcb_bus_type = {
1483764e82eSJohannes Thumshirn 	.name = "mcb",
1493764e82eSJohannes Thumshirn 	.match = mcb_match,
1503764e82eSJohannes Thumshirn 	.uevent = mcb_uevent,
1513764e82eSJohannes Thumshirn 	.probe = mcb_probe,
1523764e82eSJohannes Thumshirn 	.remove = mcb_remove,
1533764e82eSJohannes Thumshirn 	.shutdown = mcb_shutdown,
1543764e82eSJohannes Thumshirn };
1553764e82eSJohannes Thumshirn 
156*803f1ca6SJohannes Thumshirn static struct device_type mcb_carrier_device_type = {
157*803f1ca6SJohannes Thumshirn 	.name = "mcb-carrier",
158*803f1ca6SJohannes Thumshirn 	.groups = mcb_carrier_groups,
159*803f1ca6SJohannes Thumshirn };
160*803f1ca6SJohannes Thumshirn 
1613764e82eSJohannes Thumshirn /**
1623764e82eSJohannes Thumshirn  * __mcb_register_driver() - Register a @mcb_driver at the system
1633764e82eSJohannes Thumshirn  * @drv: The @mcb_driver
1643764e82eSJohannes Thumshirn  * @owner: The @mcb_driver's module
1653764e82eSJohannes Thumshirn  * @mod_name: The name of the @mcb_driver's module
1663764e82eSJohannes Thumshirn  *
1673764e82eSJohannes Thumshirn  * Register a @mcb_driver at the system. Perform some sanity checks, if
1683764e82eSJohannes Thumshirn  * the .probe and .remove methods are provided by the driver.
1693764e82eSJohannes Thumshirn  */
1703764e82eSJohannes Thumshirn int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
1713764e82eSJohannes Thumshirn 			const char *mod_name)
1723764e82eSJohannes Thumshirn {
1733764e82eSJohannes Thumshirn 	if (!drv->probe || !drv->remove)
1743764e82eSJohannes Thumshirn 		return -EINVAL;
1753764e82eSJohannes Thumshirn 
1763764e82eSJohannes Thumshirn 	drv->driver.owner = owner;
1773764e82eSJohannes Thumshirn 	drv->driver.bus = &mcb_bus_type;
1783764e82eSJohannes Thumshirn 	drv->driver.mod_name = mod_name;
1793764e82eSJohannes Thumshirn 
1803764e82eSJohannes Thumshirn 	return driver_register(&drv->driver);
1813764e82eSJohannes Thumshirn }
1823764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(__mcb_register_driver);
1833764e82eSJohannes Thumshirn 
1843764e82eSJohannes Thumshirn /**
1853764e82eSJohannes Thumshirn  * mcb_unregister_driver() - Unregister a @mcb_driver from the system
1863764e82eSJohannes Thumshirn  * @drv: The @mcb_driver
1873764e82eSJohannes Thumshirn  *
1883764e82eSJohannes Thumshirn  * Unregister a @mcb_driver from the system.
1893764e82eSJohannes Thumshirn  */
1903764e82eSJohannes Thumshirn void mcb_unregister_driver(struct mcb_driver *drv)
1913764e82eSJohannes Thumshirn {
1923764e82eSJohannes Thumshirn 	driver_unregister(&drv->driver);
1933764e82eSJohannes Thumshirn }
1943764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_unregister_driver);
1953764e82eSJohannes Thumshirn 
1963764e82eSJohannes Thumshirn static void mcb_release_dev(struct device *dev)
1973764e82eSJohannes Thumshirn {
1983764e82eSJohannes Thumshirn 	struct mcb_device *mdev = to_mcb_device(dev);
1993764e82eSJohannes Thumshirn 
2003764e82eSJohannes Thumshirn 	mcb_bus_put(mdev->bus);
2013764e82eSJohannes Thumshirn 	kfree(mdev);
2023764e82eSJohannes Thumshirn }
2033764e82eSJohannes Thumshirn 
2043764e82eSJohannes Thumshirn /**
2053764e82eSJohannes Thumshirn  * mcb_device_register() - Register a mcb_device
2063764e82eSJohannes Thumshirn  * @bus: The @mcb_bus of the device
2073764e82eSJohannes Thumshirn  * @dev: The @mcb_device
2083764e82eSJohannes Thumshirn  *
2093764e82eSJohannes Thumshirn  * Register a specific @mcb_device at a @mcb_bus and the system itself.
2103764e82eSJohannes Thumshirn  */
2113764e82eSJohannes Thumshirn int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
2123764e82eSJohannes Thumshirn {
2133764e82eSJohannes Thumshirn 	int ret;
2143764e82eSJohannes Thumshirn 	int device_id;
2153764e82eSJohannes Thumshirn 
2163764e82eSJohannes Thumshirn 	device_initialize(&dev->dev);
2173764e82eSJohannes Thumshirn 	dev->dev.bus = &mcb_bus_type;
2183764e82eSJohannes Thumshirn 	dev->dev.parent = bus->dev.parent;
2193764e82eSJohannes Thumshirn 	dev->dev.release = mcb_release_dev;
2203764e82eSJohannes Thumshirn 
2213764e82eSJohannes Thumshirn 	device_id = dev->id;
2223764e82eSJohannes Thumshirn 	dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
2233764e82eSJohannes Thumshirn 		bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
2243764e82eSJohannes Thumshirn 
2253764e82eSJohannes Thumshirn 	ret = device_add(&dev->dev);
2263764e82eSJohannes Thumshirn 	if (ret < 0) {
2273764e82eSJohannes Thumshirn 		pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
2283764e82eSJohannes Thumshirn 			device_id, bus->bus_nr, ret);
2293764e82eSJohannes Thumshirn 		goto out;
2303764e82eSJohannes Thumshirn 	}
2313764e82eSJohannes Thumshirn 
2323764e82eSJohannes Thumshirn 	return 0;
2333764e82eSJohannes Thumshirn 
2343764e82eSJohannes Thumshirn out:
2353764e82eSJohannes Thumshirn 
2363764e82eSJohannes Thumshirn 	return ret;
2373764e82eSJohannes Thumshirn }
2383764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_device_register);
2393764e82eSJohannes Thumshirn 
2403764e82eSJohannes Thumshirn /**
2413764e82eSJohannes Thumshirn  * mcb_alloc_bus() - Allocate a new @mcb_bus
2423764e82eSJohannes Thumshirn  *
2433764e82eSJohannes Thumshirn  * Allocate a new @mcb_bus.
2443764e82eSJohannes Thumshirn  */
2454ec65b77SJohannes Thumshirn struct mcb_bus *mcb_alloc_bus(struct device *carrier)
2463764e82eSJohannes Thumshirn {
2473764e82eSJohannes Thumshirn 	struct mcb_bus *bus;
2483764e82eSJohannes Thumshirn 	int bus_nr;
24918d28819SJohannes Thumshirn 	int rc;
2503764e82eSJohannes Thumshirn 
2513764e82eSJohannes Thumshirn 	bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
2523764e82eSJohannes Thumshirn 	if (!bus)
2534ec65b77SJohannes Thumshirn 		return ERR_PTR(-ENOMEM);
2543764e82eSJohannes Thumshirn 
2553764e82eSJohannes Thumshirn 	bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
2563764e82eSJohannes Thumshirn 	if (bus_nr < 0) {
25718d28819SJohannes Thumshirn 		rc = bus_nr;
25818d28819SJohannes Thumshirn 		goto err_free;
2593764e82eSJohannes Thumshirn 	}
2603764e82eSJohannes Thumshirn 
2613764e82eSJohannes Thumshirn 	bus->bus_nr = bus_nr;
2624ec65b77SJohannes Thumshirn 	bus->carrier = carrier;
26318d28819SJohannes Thumshirn 
26418d28819SJohannes Thumshirn 	device_initialize(&bus->dev);
26518d28819SJohannes Thumshirn 	bus->dev.parent = carrier;
26618d28819SJohannes Thumshirn 	bus->dev.bus = &mcb_bus_type;
267*803f1ca6SJohannes Thumshirn 	bus->dev.type = &mcb_carrier_device_type;
26818d28819SJohannes Thumshirn 
26918d28819SJohannes Thumshirn 	dev_set_name(&bus->dev, "mcb:%d", bus_nr);
27018d28819SJohannes Thumshirn 	rc = device_add(&bus->dev);
27118d28819SJohannes Thumshirn 	if (rc)
27218d28819SJohannes Thumshirn 		goto err_free;
27318d28819SJohannes Thumshirn 
2743764e82eSJohannes Thumshirn 	return bus;
27518d28819SJohannes Thumshirn err_free:
27618d28819SJohannes Thumshirn 	kfree(bus);
27718d28819SJohannes Thumshirn 	return ERR_PTR(rc);
2783764e82eSJohannes Thumshirn }
2793764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_alloc_bus);
2803764e82eSJohannes Thumshirn 
2813764e82eSJohannes Thumshirn static int __mcb_devices_unregister(struct device *dev, void *data)
2823764e82eSJohannes Thumshirn {
2833764e82eSJohannes Thumshirn 	device_unregister(dev);
2843764e82eSJohannes Thumshirn 	return 0;
2853764e82eSJohannes Thumshirn }
2863764e82eSJohannes Thumshirn 
2873764e82eSJohannes Thumshirn static void mcb_devices_unregister(struct mcb_bus *bus)
2883764e82eSJohannes Thumshirn {
2893764e82eSJohannes Thumshirn 	bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
2903764e82eSJohannes Thumshirn }
2913764e82eSJohannes Thumshirn /**
2923764e82eSJohannes Thumshirn  * mcb_release_bus() - Free a @mcb_bus
2933764e82eSJohannes Thumshirn  * @bus: The @mcb_bus to release
2943764e82eSJohannes Thumshirn  *
2953764e82eSJohannes Thumshirn  * Release an allocated @mcb_bus from the system.
2963764e82eSJohannes Thumshirn  */
2973764e82eSJohannes Thumshirn void mcb_release_bus(struct mcb_bus *bus)
2983764e82eSJohannes Thumshirn {
2993764e82eSJohannes Thumshirn 	mcb_devices_unregister(bus);
3003764e82eSJohannes Thumshirn 
3013764e82eSJohannes Thumshirn 	ida_simple_remove(&mcb_ida, bus->bus_nr);
3023764e82eSJohannes Thumshirn 
3033764e82eSJohannes Thumshirn 	kfree(bus);
3043764e82eSJohannes Thumshirn }
3053764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_release_bus);
3063764e82eSJohannes Thumshirn 
3073764e82eSJohannes Thumshirn /**
3083764e82eSJohannes Thumshirn  * mcb_bus_put() - Increment refcnt
3093764e82eSJohannes Thumshirn  * @bus: The @mcb_bus
3103764e82eSJohannes Thumshirn  *
3113764e82eSJohannes Thumshirn  * Get a @mcb_bus' ref
3123764e82eSJohannes Thumshirn  */
3133764e82eSJohannes Thumshirn struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
3143764e82eSJohannes Thumshirn {
3153764e82eSJohannes Thumshirn 	if (bus)
3163764e82eSJohannes Thumshirn 		get_device(&bus->dev);
3173764e82eSJohannes Thumshirn 
3183764e82eSJohannes Thumshirn 	return bus;
3193764e82eSJohannes Thumshirn }
3203764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_bus_get);
3213764e82eSJohannes Thumshirn 
3223764e82eSJohannes Thumshirn /**
3233764e82eSJohannes Thumshirn  * mcb_bus_put() - Decrement refcnt
3243764e82eSJohannes Thumshirn  * @bus: The @mcb_bus
3253764e82eSJohannes Thumshirn  *
3263764e82eSJohannes Thumshirn  * Release a @mcb_bus' ref
3273764e82eSJohannes Thumshirn  */
3283764e82eSJohannes Thumshirn void mcb_bus_put(struct mcb_bus *bus)
3293764e82eSJohannes Thumshirn {
3303764e82eSJohannes Thumshirn 	if (bus)
3313764e82eSJohannes Thumshirn 		put_device(&bus->dev);
3323764e82eSJohannes Thumshirn }
3333764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_bus_put);
3343764e82eSJohannes Thumshirn 
3353764e82eSJohannes Thumshirn /**
3363764e82eSJohannes Thumshirn  * mcb_alloc_dev() - Allocate a device
3373764e82eSJohannes Thumshirn  * @bus: The @mcb_bus the device is part of
3383764e82eSJohannes Thumshirn  *
3393764e82eSJohannes Thumshirn  * Allocate a @mcb_device and add bus.
3403764e82eSJohannes Thumshirn  */
3413764e82eSJohannes Thumshirn struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
3423764e82eSJohannes Thumshirn {
3433764e82eSJohannes Thumshirn 	struct mcb_device *dev;
3443764e82eSJohannes Thumshirn 
3453764e82eSJohannes Thumshirn 	dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
3463764e82eSJohannes Thumshirn 	if (!dev)
3473764e82eSJohannes Thumshirn 		return NULL;
3483764e82eSJohannes Thumshirn 
3493764e82eSJohannes Thumshirn 	INIT_LIST_HEAD(&dev->bus_list);
3503764e82eSJohannes Thumshirn 	dev->bus = bus;
3513764e82eSJohannes Thumshirn 
3523764e82eSJohannes Thumshirn 	return dev;
3533764e82eSJohannes Thumshirn }
3543764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_alloc_dev);
3553764e82eSJohannes Thumshirn 
3563764e82eSJohannes Thumshirn /**
3573764e82eSJohannes Thumshirn  * mcb_free_dev() - Free @mcb_device
3583764e82eSJohannes Thumshirn  * @dev: The device to free
3593764e82eSJohannes Thumshirn  *
3603764e82eSJohannes Thumshirn  * Free a @mcb_device
3613764e82eSJohannes Thumshirn  */
3623764e82eSJohannes Thumshirn void mcb_free_dev(struct mcb_device *dev)
3633764e82eSJohannes Thumshirn {
3643764e82eSJohannes Thumshirn 	kfree(dev);
3653764e82eSJohannes Thumshirn }
3663764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_free_dev);
3673764e82eSJohannes Thumshirn 
3683764e82eSJohannes Thumshirn static int __mcb_bus_add_devices(struct device *dev, void *data)
3693764e82eSJohannes Thumshirn {
3703764e82eSJohannes Thumshirn 	struct mcb_device *mdev = to_mcb_device(dev);
3713764e82eSJohannes Thumshirn 	int retval;
3723764e82eSJohannes Thumshirn 
3733764e82eSJohannes Thumshirn 	if (mdev->is_added)
3743764e82eSJohannes Thumshirn 		return 0;
3753764e82eSJohannes Thumshirn 
3763764e82eSJohannes Thumshirn 	retval = device_attach(dev);
3773764e82eSJohannes Thumshirn 	if (retval < 0)
3783764e82eSJohannes Thumshirn 		dev_err(dev, "Error adding device (%d)\n", retval);
3793764e82eSJohannes Thumshirn 
3803764e82eSJohannes Thumshirn 	mdev->is_added = true;
3813764e82eSJohannes Thumshirn 
3823764e82eSJohannes Thumshirn 	return 0;
3833764e82eSJohannes Thumshirn }
3843764e82eSJohannes Thumshirn 
3853764e82eSJohannes Thumshirn static int __mcb_bus_add_child(struct device *dev, void *data)
3863764e82eSJohannes Thumshirn {
3873764e82eSJohannes Thumshirn 	struct mcb_device *mdev = to_mcb_device(dev);
3883764e82eSJohannes Thumshirn 	struct mcb_bus *child;
3893764e82eSJohannes Thumshirn 
3903764e82eSJohannes Thumshirn 	BUG_ON(!mdev->is_added);
3913764e82eSJohannes Thumshirn 	child = mdev->subordinate;
3923764e82eSJohannes Thumshirn 
3933764e82eSJohannes Thumshirn 	if (child)
3943764e82eSJohannes Thumshirn 		mcb_bus_add_devices(child);
3953764e82eSJohannes Thumshirn 
3963764e82eSJohannes Thumshirn 	return 0;
3973764e82eSJohannes Thumshirn }
3983764e82eSJohannes Thumshirn 
3993764e82eSJohannes Thumshirn /**
4003764e82eSJohannes Thumshirn  * mcb_bus_add_devices() - Add devices in the bus' internal device list
4013764e82eSJohannes Thumshirn  * @bus: The @mcb_bus we add the devices
4023764e82eSJohannes Thumshirn  *
4033764e82eSJohannes Thumshirn  * Add devices in the bus' internal device list to the system.
4043764e82eSJohannes Thumshirn  */
4053764e82eSJohannes Thumshirn void mcb_bus_add_devices(const struct mcb_bus *bus)
4063764e82eSJohannes Thumshirn {
4073764e82eSJohannes Thumshirn 	bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
4083764e82eSJohannes Thumshirn 	bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
4093764e82eSJohannes Thumshirn 
4103764e82eSJohannes Thumshirn }
4113764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
4123764e82eSJohannes Thumshirn 
4133764e82eSJohannes Thumshirn /**
4143764e82eSJohannes Thumshirn  * mcb_request_mem() - Request memory
4153764e82eSJohannes Thumshirn  * @dev: The @mcb_device the memory is for
4163764e82eSJohannes Thumshirn  * @name: The name for the memory reference.
4173764e82eSJohannes Thumshirn  *
4183764e82eSJohannes Thumshirn  * Request memory for a @mcb_device. If @name is NULL the driver name will
4193764e82eSJohannes Thumshirn  * be used.
4203764e82eSJohannes Thumshirn  */
4213764e82eSJohannes Thumshirn struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
4223764e82eSJohannes Thumshirn {
4233764e82eSJohannes Thumshirn 	struct resource *mem;
4243764e82eSJohannes Thumshirn 	u32 size;
4253764e82eSJohannes Thumshirn 
4263764e82eSJohannes Thumshirn 	if (!name)
4273764e82eSJohannes Thumshirn 		name = dev->dev.driver->name;
4283764e82eSJohannes Thumshirn 
4293764e82eSJohannes Thumshirn 	size = resource_size(&dev->mem);
4303764e82eSJohannes Thumshirn 
4313764e82eSJohannes Thumshirn 	mem = request_mem_region(dev->mem.start, size, name);
4323764e82eSJohannes Thumshirn 	if (!mem)
4333764e82eSJohannes Thumshirn 		return ERR_PTR(-EBUSY);
4343764e82eSJohannes Thumshirn 
4353764e82eSJohannes Thumshirn 	return mem;
4363764e82eSJohannes Thumshirn }
4373764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_request_mem);
4383764e82eSJohannes Thumshirn 
4393764e82eSJohannes Thumshirn /**
4403764e82eSJohannes Thumshirn  * mcb_release_mem() - Release memory requested by device
4413764e82eSJohannes Thumshirn  * @dev: The @mcb_device that requested the memory
4423764e82eSJohannes Thumshirn  *
4433764e82eSJohannes Thumshirn  * Release memory that was prior requested via @mcb_request_mem().
4443764e82eSJohannes Thumshirn  */
4453764e82eSJohannes Thumshirn void mcb_release_mem(struct resource *mem)
4463764e82eSJohannes Thumshirn {
4473764e82eSJohannes Thumshirn 	u32 size;
4483764e82eSJohannes Thumshirn 
4493764e82eSJohannes Thumshirn 	size = resource_size(mem);
4503764e82eSJohannes Thumshirn 	release_mem_region(mem->start, size);
4513764e82eSJohannes Thumshirn }
4523764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_release_mem);
4533764e82eSJohannes Thumshirn 
4544ec65b77SJohannes Thumshirn static int __mcb_get_irq(struct mcb_device *dev)
4554ec65b77SJohannes Thumshirn {
4564ec65b77SJohannes Thumshirn 	struct resource *irq = &dev->irq;
4574ec65b77SJohannes Thumshirn 
4584ec65b77SJohannes Thumshirn 	return irq->start;
4594ec65b77SJohannes Thumshirn }
4604ec65b77SJohannes Thumshirn 
4613764e82eSJohannes Thumshirn /**
4623764e82eSJohannes Thumshirn  * mcb_get_irq() - Get device's IRQ number
4633764e82eSJohannes Thumshirn  * @dev: The @mcb_device the IRQ is for
4643764e82eSJohannes Thumshirn  *
4653764e82eSJohannes Thumshirn  * Get the IRQ number of a given @mcb_device.
4663764e82eSJohannes Thumshirn  */
4673764e82eSJohannes Thumshirn int mcb_get_irq(struct mcb_device *dev)
4683764e82eSJohannes Thumshirn {
4694ec65b77SJohannes Thumshirn 	struct mcb_bus *bus = dev->bus;
4703764e82eSJohannes Thumshirn 
4714ec65b77SJohannes Thumshirn 	if (bus->get_irq)
4724ec65b77SJohannes Thumshirn 		return bus->get_irq(dev);
4734ec65b77SJohannes Thumshirn 
4744ec65b77SJohannes Thumshirn 	return __mcb_get_irq(dev);
4753764e82eSJohannes Thumshirn }
4763764e82eSJohannes Thumshirn EXPORT_SYMBOL_GPL(mcb_get_irq);
4773764e82eSJohannes Thumshirn 
4783764e82eSJohannes Thumshirn static int mcb_init(void)
4793764e82eSJohannes Thumshirn {
4803764e82eSJohannes Thumshirn 	return bus_register(&mcb_bus_type);
4813764e82eSJohannes Thumshirn }
4823764e82eSJohannes Thumshirn 
4833764e82eSJohannes Thumshirn static void mcb_exit(void)
4843764e82eSJohannes Thumshirn {
485169883a6SJohannes Thumshirn 	ida_destroy(&mcb_ida);
4863764e82eSJohannes Thumshirn 	bus_unregister(&mcb_bus_type);
4873764e82eSJohannes Thumshirn }
4883764e82eSJohannes Thumshirn 
4893764e82eSJohannes Thumshirn /* mcb must be initialized after PCI but before the chameleon drivers.
4903764e82eSJohannes Thumshirn  * That means we must use some initcall between subsys_initcall and
4913764e82eSJohannes Thumshirn  * device_initcall.
4923764e82eSJohannes Thumshirn  */
4933764e82eSJohannes Thumshirn fs_initcall(mcb_init);
4943764e82eSJohannes Thumshirn module_exit(mcb_exit);
4953764e82eSJohannes Thumshirn 
4963764e82eSJohannes Thumshirn MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
4973764e82eSJohannes Thumshirn MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
4983764e82eSJohannes Thumshirn MODULE_LICENSE("GPL v2");
499