Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 *  Zorro Driver Services
  3 *
  4 *  Copyright (C) 2003 Geert Uytterhoeven
  5 *
  6 *  Loosely based on drivers/pci/pci-driver.c
  7 *
  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
 10 *  for more details.
 11 */
 12
 13#include <linux/init.h>
 14#include <linux/module.h>
 15#include <linux/zorro.h>
 16
 
 
 17
 18    /**
 19     *  zorro_match_device - Tell if a Zorro device structure has a matching
 20     *                       Zorro device id structure
 21     *  @ids: array of Zorro device id structures to search in
 22     *  @dev: the Zorro device structure to match against
 23     *
 24     *  Used by a driver to check whether a Zorro device present in the
 25     *  system is in its list of supported devices. Returns the matching
 26     *  zorro_device_id structure or %NULL if there is no match.
 27     */
 28
 29const struct zorro_device_id *
 30zorro_match_device(const struct zorro_device_id *ids,
 31		   const struct zorro_dev *z)
 32{
 33	while (ids->id) {
 34		if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
 35			return ids;
 36		ids++;
 37	}
 38	return NULL;
 39}
 
 40
 41
 42static int zorro_device_probe(struct device *dev)
 43{
 44	int error = 0;
 45	struct zorro_driver *drv = to_zorro_driver(dev->driver);
 46	struct zorro_dev *z = to_zorro_dev(dev);
 47
 48	if (!z->driver && drv->probe) {
 49		const struct zorro_device_id *id;
 50
 51		id = zorro_match_device(drv->id_table, z);
 52		if (id)
 53			error = drv->probe(z, id);
 54		if (error >= 0) {
 55			z->driver = drv;
 56			error = 0;
 57		}
 58	}
 59	return error;
 60}
 61
 62
 63static int zorro_device_remove(struct device *dev)
 64{
 65	struct zorro_dev *z = to_zorro_dev(dev);
 66	struct zorro_driver *drv = to_zorro_driver(dev->driver);
 67
 68	if (drv) {
 69		if (drv->remove)
 70			drv->remove(z);
 71		z->driver = NULL;
 72	}
 73	return 0;
 74}
 75
 76
 77    /**
 78     *  zorro_register_driver - register a new Zorro driver
 79     *  @drv: the driver structure to register
 80     *
 81     *  Adds the driver structure to the list of registered drivers
 82     *  Returns zero or a negative error value.
 83     */
 84
 85int zorro_register_driver(struct zorro_driver *drv)
 86{
 87	/* initialize common driver fields */
 88	drv->driver.name = drv->name;
 89	drv->driver.bus = &zorro_bus_type;
 90
 91	/* register with core */
 92	return driver_register(&drv->driver);
 93}
 
 94
 95
 96    /**
 97     *  zorro_unregister_driver - unregister a zorro driver
 98     *  @drv: the driver structure to unregister
 99     *
100     *  Deletes the driver structure from the list of registered Zorro drivers,
101     *  gives it a chance to clean up by calling its remove() function for
102     *  each device it was responsible for, and marks those devices as
103     *  driverless.
104     */
105
106void zorro_unregister_driver(struct zorro_driver *drv)
107{
108	driver_unregister(&drv->driver);
109}
 
110
111
112    /**
113     *  zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
114     *                    device id structure
115     *  @ids: array of Zorro device id structures to search in
116     *  @dev: the Zorro device structure to match against
117     *
118     *  Used by a driver to check whether a Zorro device present in the
119     *  system is in its list of supported devices.Returns the matching
120     *  zorro_device_id structure or %NULL if there is no match.
121     */
122
123static int zorro_bus_match(struct device *dev, struct device_driver *drv)
124{
125	struct zorro_dev *z = to_zorro_dev(dev);
126	struct zorro_driver *zorro_drv = to_zorro_driver(drv);
127	const struct zorro_device_id *ids = zorro_drv->id_table;
128
129	if (!ids)
130		return 0;
131
132	while (ids->id) {
133		if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
134			return 1;
135		ids++;
136	}
137	return 0;
138}
139
140static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
141{
142#ifdef CONFIG_HOTPLUG
143	struct zorro_dev *z;
144
145	if (!dev)
146		return -ENODEV;
147
148	z = to_zorro_dev(dev);
149	if (!z)
150		return -ENODEV;
151
152	if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
153	    add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
154	    add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
155	    add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
156		return -ENOMEM;
157
158	return 0;
159#else /* !CONFIG_HOTPLUG */
160	return -ENODEV;
161#endif /* !CONFIG_HOTPLUG */
162}
163
164struct bus_type zorro_bus_type = {
165	.name	= "zorro",
166	.match	= zorro_bus_match,
167	.uevent	= zorro_uevent,
168	.probe	= zorro_device_probe,
169	.remove	= zorro_device_remove,
 
 
170};
 
171
172
173static int __init zorro_driver_init(void)
174{
175	return bus_register(&zorro_bus_type);
176}
177
178postcore_initcall(zorro_driver_init);
179
180EXPORT_SYMBOL(zorro_match_device);
181EXPORT_SYMBOL(zorro_register_driver);
182EXPORT_SYMBOL(zorro_unregister_driver);
183EXPORT_SYMBOL(zorro_bus_type);
v5.4
  1/*
  2 *  Zorro Driver Services
  3 *
  4 *  Copyright (C) 2003 Geert Uytterhoeven
  5 *
  6 *  Loosely based on drivers/pci/pci-driver.c
  7 *
  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
 10 *  for more details.
 11 */
 12
 13#include <linux/init.h>
 14#include <linux/module.h>
 15#include <linux/zorro.h>
 16
 17#include "zorro.h"
 18
 19
 20    /**
 21     *  zorro_match_device - Tell if a Zorro device structure has a matching
 22     *                       Zorro device id structure
 23     *  @ids: array of Zorro device id structures to search in
 24     *  @dev: the Zorro device structure to match against
 25     *
 26     *  Used by a driver to check whether a Zorro device present in the
 27     *  system is in its list of supported devices. Returns the matching
 28     *  zorro_device_id structure or %NULL if there is no match.
 29     */
 30
 31const struct zorro_device_id *
 32zorro_match_device(const struct zorro_device_id *ids,
 33		   const struct zorro_dev *z)
 34{
 35	while (ids->id) {
 36		if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
 37			return ids;
 38		ids++;
 39	}
 40	return NULL;
 41}
 42EXPORT_SYMBOL(zorro_match_device);
 43
 44
 45static int zorro_device_probe(struct device *dev)
 46{
 47	int error = 0;
 48	struct zorro_driver *drv = to_zorro_driver(dev->driver);
 49	struct zorro_dev *z = to_zorro_dev(dev);
 50
 51	if (!z->driver && drv->probe) {
 52		const struct zorro_device_id *id;
 53
 54		id = zorro_match_device(drv->id_table, z);
 55		if (id)
 56			error = drv->probe(z, id);
 57		if (error >= 0) {
 58			z->driver = drv;
 59			error = 0;
 60		}
 61	}
 62	return error;
 63}
 64
 65
 66static int zorro_device_remove(struct device *dev)
 67{
 68	struct zorro_dev *z = to_zorro_dev(dev);
 69	struct zorro_driver *drv = to_zorro_driver(dev->driver);
 70
 71	if (drv) {
 72		if (drv->remove)
 73			drv->remove(z);
 74		z->driver = NULL;
 75	}
 76	return 0;
 77}
 78
 79
 80    /**
 81     *  zorro_register_driver - register a new Zorro driver
 82     *  @drv: the driver structure to register
 83     *
 84     *  Adds the driver structure to the list of registered drivers
 85     *  Returns zero or a negative error value.
 86     */
 87
 88int zorro_register_driver(struct zorro_driver *drv)
 89{
 90	/* initialize common driver fields */
 91	drv->driver.name = drv->name;
 92	drv->driver.bus = &zorro_bus_type;
 93
 94	/* register with core */
 95	return driver_register(&drv->driver);
 96}
 97EXPORT_SYMBOL(zorro_register_driver);
 98
 99
100    /**
101     *  zorro_unregister_driver - unregister a zorro driver
102     *  @drv: the driver structure to unregister
103     *
104     *  Deletes the driver structure from the list of registered Zorro drivers,
105     *  gives it a chance to clean up by calling its remove() function for
106     *  each device it was responsible for, and marks those devices as
107     *  driverless.
108     */
109
110void zorro_unregister_driver(struct zorro_driver *drv)
111{
112	driver_unregister(&drv->driver);
113}
114EXPORT_SYMBOL(zorro_unregister_driver);
115
116
117    /**
118     *  zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
119     *                    device id structure
120     *  @ids: array of Zorro device id structures to search in
121     *  @dev: the Zorro device structure to match against
122     *
123     *  Used by a driver to check whether a Zorro device present in the
124     *  system is in its list of supported devices.Returns the matching
125     *  zorro_device_id structure or %NULL if there is no match.
126     */
127
128static int zorro_bus_match(struct device *dev, struct device_driver *drv)
129{
130	struct zorro_dev *z = to_zorro_dev(dev);
131	struct zorro_driver *zorro_drv = to_zorro_driver(drv);
132	const struct zorro_device_id *ids = zorro_drv->id_table;
133
134	if (!ids)
135		return 0;
136
137	while (ids->id) {
138		if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
139			return 1;
140		ids++;
141	}
142	return 0;
143}
144
145static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
146{
 
147	struct zorro_dev *z;
148
149	if (!dev)
150		return -ENODEV;
151
152	z = to_zorro_dev(dev);
153	if (!z)
154		return -ENODEV;
155
156	if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
157	    add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
158	    add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
159	    add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
160		return -ENOMEM;
161
162	return 0;
 
 
 
163}
164
165struct bus_type zorro_bus_type = {
166	.name		= "zorro",
167	.dev_name	= "zorro",
168	.dev_groups	= zorro_device_attribute_groups,
169	.match		= zorro_bus_match,
170	.uevent		= zorro_uevent,
171	.probe		= zorro_device_probe,
172	.remove		= zorro_device_remove,
173};
174EXPORT_SYMBOL(zorro_bus_type);
175
176
177static int __init zorro_driver_init(void)
178{
179	return bus_register(&zorro_bus_type);
180}
181
182postcore_initcall(zorro_driver_init);
183