Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  Zorro Driver Services
0003  *
0004  *  Copyright (C) 2003 Geert Uytterhoeven
0005  *
0006  *  Loosely based on drivers/pci/pci-driver.c
0007  *
0008  *  This file is subject to the terms and conditions of the GNU General Public
0009  *  License.  See the file COPYING in the main directory of this archive
0010  *  for more details.
0011  */
0012 
0013 #include <linux/init.h>
0014 #include <linux/module.h>
0015 #include <linux/zorro.h>
0016 
0017 #include "zorro.h"
0018 
0019 
0020     /**
0021      *  zorro_match_device - Tell if a Zorro device structure has a matching
0022      *                       Zorro device id structure
0023      *  @ids: array of Zorro device id structures to search in
0024      *  @dev: the Zorro device structure to match against
0025      *
0026      *  Used by a driver to check whether a Zorro device present in the
0027      *  system is in its list of supported devices. Returns the matching
0028      *  zorro_device_id structure or %NULL if there is no match.
0029      */
0030 
0031 static const struct zorro_device_id *
0032 zorro_match_device(const struct zorro_device_id *ids,
0033            const struct zorro_dev *z)
0034 {
0035     while (ids->id) {
0036         if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
0037             return ids;
0038         ids++;
0039     }
0040     return NULL;
0041 }
0042 
0043 
0044 static int zorro_device_probe(struct device *dev)
0045 {
0046     int error = 0;
0047     struct zorro_driver *drv = to_zorro_driver(dev->driver);
0048     struct zorro_dev *z = to_zorro_dev(dev);
0049 
0050     if (drv->probe) {
0051         const struct zorro_device_id *id;
0052 
0053         id = zorro_match_device(drv->id_table, z);
0054         if (id)
0055             error = drv->probe(z, id);
0056         if (error >= 0)
0057             error = 0;
0058     }
0059     return error;
0060 }
0061 
0062 
0063 static void zorro_device_remove(struct device *dev)
0064 {
0065     struct zorro_dev *z = to_zorro_dev(dev);
0066     struct zorro_driver *drv = to_zorro_driver(dev->driver);
0067 
0068     if (drv->remove)
0069         drv->remove(z);
0070 }
0071 
0072 
0073     /**
0074      *  zorro_register_driver - register a new Zorro driver
0075      *  @drv: the driver structure to register
0076      *
0077      *  Adds the driver structure to the list of registered drivers
0078      *  Returns zero or a negative error value.
0079      */
0080 
0081 int zorro_register_driver(struct zorro_driver *drv)
0082 {
0083     /* initialize common driver fields */
0084     drv->driver.name = drv->name;
0085     drv->driver.bus = &zorro_bus_type;
0086 
0087     /* register with core */
0088     return driver_register(&drv->driver);
0089 }
0090 EXPORT_SYMBOL(zorro_register_driver);
0091 
0092 
0093     /**
0094      *  zorro_unregister_driver - unregister a zorro driver
0095      *  @drv: the driver structure to unregister
0096      *
0097      *  Deletes the driver structure from the list of registered Zorro drivers,
0098      *  gives it a chance to clean up by calling its remove() function for
0099      *  each device it was responsible for, and marks those devices as
0100      *  driverless.
0101      */
0102 
0103 void zorro_unregister_driver(struct zorro_driver *drv)
0104 {
0105     driver_unregister(&drv->driver);
0106 }
0107 EXPORT_SYMBOL(zorro_unregister_driver);
0108 
0109 
0110     /**
0111      *  zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
0112      *                    device id structure
0113      *  @ids: array of Zorro device id structures to search in
0114      *  @dev: the Zorro device structure to match against
0115      *
0116      *  Used by the driver core to check whether a Zorro device present in the
0117      *  system is in a driver's list of supported devices.  Returns 1 if
0118      *  supported, and 0 if there is no match.
0119      */
0120 
0121 static int zorro_bus_match(struct device *dev, struct device_driver *drv)
0122 {
0123     struct zorro_dev *z = to_zorro_dev(dev);
0124     struct zorro_driver *zorro_drv = to_zorro_driver(drv);
0125     const struct zorro_device_id *ids = zorro_drv->id_table;
0126 
0127     if (!ids)
0128         return 0;
0129 
0130     return !!zorro_match_device(ids, z);
0131 }
0132 
0133 static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
0134 {
0135     struct zorro_dev *z;
0136 
0137     if (!dev)
0138         return -ENODEV;
0139 
0140     z = to_zorro_dev(dev);
0141     if (!z)
0142         return -ENODEV;
0143 
0144     if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
0145         add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
0146         add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
0147         add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
0148         return -ENOMEM;
0149 
0150     return 0;
0151 }
0152 
0153 struct bus_type zorro_bus_type = {
0154     .name       = "zorro",
0155     .dev_name   = "zorro",
0156     .dev_groups = zorro_device_attribute_groups,
0157     .match      = zorro_bus_match,
0158     .uevent     = zorro_uevent,
0159     .probe      = zorro_device_probe,
0160     .remove     = zorro_device_remove,
0161 };
0162 EXPORT_SYMBOL(zorro_bus_type);
0163 
0164 
0165 static int __init zorro_driver_init(void)
0166 {
0167     return bus_register(&zorro_bus_type);
0168 }
0169 
0170 postcore_initcall(zorro_driver_init);
0171