18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  DIO Driver Services
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *  Copyright (C) 2004 Jochen Friedrich
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Loosely based on drivers/pci/pci-driver.c and drivers/zorro/zorro-driver.c
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  This file is subject to the terms and conditions of the GNU General Public
98c2ecf20Sopenharmony_ci *  License.  See the file COPYING in the main directory of this archive
108c2ecf20Sopenharmony_ci *  for more details.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/dio.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/**
198c2ecf20Sopenharmony_ci *  dio_match_device - Tell if a DIO device structure has a matching DIO device id structure
208c2ecf20Sopenharmony_ci *  @ids: array of DIO device id structures to search in
218c2ecf20Sopenharmony_ci *  @d: the DIO device structure to match against
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci *  Used by a driver to check whether a DIO device present in the
248c2ecf20Sopenharmony_ci *  system is in its list of supported devices. Returns the matching
258c2ecf20Sopenharmony_ci *  dio_device_id structure or %NULL if there is no match.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic const struct dio_device_id *
298c2ecf20Sopenharmony_cidio_match_device(const struct dio_device_id *ids,
308c2ecf20Sopenharmony_ci		   const struct dio_dev *d)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	while (ids->id) {
338c2ecf20Sopenharmony_ci		if (ids->id == DIO_WILDCARD)
348c2ecf20Sopenharmony_ci			return ids;
358c2ecf20Sopenharmony_ci		if (DIO_NEEDSSECID(ids->id & 0xff)) {
368c2ecf20Sopenharmony_ci			if (ids->id == d->id)
378c2ecf20Sopenharmony_ci				return ids;
388c2ecf20Sopenharmony_ci		} else {
398c2ecf20Sopenharmony_ci			if ((ids->id & 0xff) == (d->id & 0xff))
408c2ecf20Sopenharmony_ci				return ids;
418c2ecf20Sopenharmony_ci		}
428c2ecf20Sopenharmony_ci		ids++;
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci	return NULL;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int dio_device_probe(struct device *dev)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	int error = 0;
508c2ecf20Sopenharmony_ci	struct dio_driver *drv = to_dio_driver(dev->driver);
518c2ecf20Sopenharmony_ci	struct dio_dev *d = to_dio_dev(dev);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (!d->driver && drv->probe) {
548c2ecf20Sopenharmony_ci		const struct dio_device_id *id;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		id = dio_match_device(drv->id_table, d);
578c2ecf20Sopenharmony_ci		if (id)
588c2ecf20Sopenharmony_ci			error = drv->probe(d, id);
598c2ecf20Sopenharmony_ci		if (error >= 0) {
608c2ecf20Sopenharmony_ci			d->driver = drv;
618c2ecf20Sopenharmony_ci			error = 0;
628c2ecf20Sopenharmony_ci		}
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci	return error;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/**
698c2ecf20Sopenharmony_ci *  dio_register_driver - register a new DIO driver
708c2ecf20Sopenharmony_ci *  @drv: the driver structure to register
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci *  Adds the driver structure to the list of registered drivers
738c2ecf20Sopenharmony_ci *  Returns zero or a negative error value.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciint dio_register_driver(struct dio_driver *drv)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	/* initialize common driver fields */
798c2ecf20Sopenharmony_ci	drv->driver.name = drv->name;
808c2ecf20Sopenharmony_ci	drv->driver.bus = &dio_bus_type;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/* register with core */
838c2ecf20Sopenharmony_ci	return driver_register(&drv->driver);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/**
888c2ecf20Sopenharmony_ci *  dio_unregister_driver - unregister a DIO driver
898c2ecf20Sopenharmony_ci *  @drv: the driver structure to unregister
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci *  Deletes the driver structure from the list of registered DIO drivers,
928c2ecf20Sopenharmony_ci *  gives it a chance to clean up by calling its remove() function for
938c2ecf20Sopenharmony_ci *  each device it was responsible for, and marks those devices as
948c2ecf20Sopenharmony_ci *  driverless.
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_civoid dio_unregister_driver(struct dio_driver *drv)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	driver_unregister(&drv->driver);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/**
1048c2ecf20Sopenharmony_ci *  dio_bus_match - Tell if a DIO device structure has a matching DIO device id structure
1058c2ecf20Sopenharmony_ci *  @dev: the DIO device structure to match against
1068c2ecf20Sopenharmony_ci *  @drv: the &device_driver that points to the array of DIO device id structures to search
1078c2ecf20Sopenharmony_ci *
1088c2ecf20Sopenharmony_ci *  Used by the driver core to check whether a DIO device present in the
1098c2ecf20Sopenharmony_ci *  system is in a driver's list of supported devices. Returns 1 if supported,
1108c2ecf20Sopenharmony_ci *  and 0 if there is no match.
1118c2ecf20Sopenharmony_ci */
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic int dio_bus_match(struct device *dev, struct device_driver *drv)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct dio_dev *d = to_dio_dev(dev);
1168c2ecf20Sopenharmony_ci	struct dio_driver *dio_drv = to_dio_driver(drv);
1178c2ecf20Sopenharmony_ci	const struct dio_device_id *ids = dio_drv->id_table;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (!ids)
1208c2ecf20Sopenharmony_ci		return 0;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	return dio_match_device(ids, d) ? 1 : 0;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistruct bus_type dio_bus_type = {
1278c2ecf20Sopenharmony_ci	.name	= "dio",
1288c2ecf20Sopenharmony_ci	.match	= dio_bus_match,
1298c2ecf20Sopenharmony_ci	.probe	= dio_device_probe,
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic int __init dio_driver_init(void)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return bus_register(&dio_bus_type);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cipostcore_initcall(dio_driver_init);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dio_register_driver);
1418c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dio_unregister_driver);
1428c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dio_bus_type);
143