Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  DIO Driver Services
0003  *
0004  *  Copyright (C) 2004 Jochen Friedrich
0005  *
0006  *  Loosely based on drivers/pci/pci-driver.c and drivers/zorro/zorro-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/dio.h>
0016 
0017 
0018 /**
0019  *  dio_match_device - Tell if a DIO device structure has a matching DIO device id structure
0020  *  @ids: array of DIO device id structures to search in
0021  *  @d: the DIO device structure to match against
0022  *
0023  *  Used by a driver to check whether a DIO device present in the
0024  *  system is in its list of supported devices. Returns the matching
0025  *  dio_device_id structure or %NULL if there is no match.
0026  */
0027 
0028 static const struct dio_device_id *
0029 dio_match_device(const struct dio_device_id *ids,
0030            const struct dio_dev *d)
0031 {
0032     while (ids->id) {
0033         if (ids->id == DIO_WILDCARD)
0034             return ids;
0035         if (DIO_NEEDSSECID(ids->id & 0xff)) {
0036             if (ids->id == d->id)
0037                 return ids;
0038         } else {
0039             if ((ids->id & 0xff) == (d->id & 0xff))
0040                 return ids;
0041         }
0042         ids++;
0043     }
0044     return NULL;
0045 }
0046 
0047 static int dio_device_probe(struct device *dev)
0048 {
0049     int error = 0;
0050     struct dio_driver *drv = to_dio_driver(dev->driver);
0051     struct dio_dev *d = to_dio_dev(dev);
0052 
0053     if (!d->driver && drv->probe) {
0054         const struct dio_device_id *id;
0055 
0056         id = dio_match_device(drv->id_table, d);
0057         if (id)
0058             error = drv->probe(d, id);
0059         if (error >= 0) {
0060             d->driver = drv;
0061             error = 0;
0062         }
0063     }
0064     return error;
0065 }
0066 
0067 
0068 /**
0069  *  dio_register_driver - register a new DIO driver
0070  *  @drv: the driver structure to register
0071  *
0072  *  Adds the driver structure to the list of registered drivers
0073  *  Returns zero or a negative error value.
0074  */
0075 
0076 int dio_register_driver(struct dio_driver *drv)
0077 {
0078     /* initialize common driver fields */
0079     drv->driver.name = drv->name;
0080     drv->driver.bus = &dio_bus_type;
0081 
0082     /* register with core */
0083     return driver_register(&drv->driver);
0084 }
0085 
0086 
0087 /**
0088  *  dio_unregister_driver - unregister a DIO driver
0089  *  @drv: the driver structure to unregister
0090  *
0091  *  Deletes the driver structure from the list of registered DIO drivers,
0092  *  gives it a chance to clean up by calling its remove() function for
0093  *  each device it was responsible for, and marks those devices as
0094  *  driverless.
0095  */
0096 
0097 void dio_unregister_driver(struct dio_driver *drv)
0098 {
0099     driver_unregister(&drv->driver);
0100 }
0101 
0102 
0103 /**
0104  *  dio_bus_match - Tell if a DIO device structure has a matching DIO device id structure
0105  *  @dev: the DIO device structure to match against
0106  *  @drv: the &device_driver that points to the array of DIO device id structures to search
0107  *
0108  *  Used by the driver core to check whether a DIO device present in the
0109  *  system is in a driver's list of supported devices. Returns 1 if supported,
0110  *  and 0 if there is no match.
0111  */
0112 
0113 static int dio_bus_match(struct device *dev, struct device_driver *drv)
0114 {
0115     struct dio_dev *d = to_dio_dev(dev);
0116     struct dio_driver *dio_drv = to_dio_driver(drv);
0117     const struct dio_device_id *ids = dio_drv->id_table;
0118 
0119     if (!ids)
0120         return 0;
0121 
0122     return dio_match_device(ids, d) ? 1 : 0;
0123 }
0124 
0125 
0126 struct bus_type dio_bus_type = {
0127     .name   = "dio",
0128     .match  = dio_bus_match,
0129     .probe  = dio_device_probe,
0130 };
0131 
0132 
0133 static int __init dio_driver_init(void)
0134 {
0135     return bus_register(&dio_bus_type);
0136 }
0137 
0138 postcore_initcall(dio_driver_init);
0139 
0140 EXPORT_SYMBOL(dio_register_driver);
0141 EXPORT_SYMBOL(dio_unregister_driver);
0142 EXPORT_SYMBOL(dio_bus_type);