Module: xenomai-3
Branch: wip/gpio
Commit: b697eb6b36b1b3dabe25b50a0217b24753670806
URL:    
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=b697eb6b36b1b3dabe25b50a0217b24753670806

Author: Philippe Gerum <r...@xenomai.org>
Date:   Wed Jun  1 11:18:22 2016 +0200

cobalt/rtdm: add base minor setting for named drivers

Some character/named drivers may want to set a non-zero base minor for
indexing their devices (e.g. GPIO banks).

To this end, the new .base_minor field can be set in device driver
descriptors. Most in-tree driver descriptors reside in static memory
which guarantees that such field is zero when unspecified.

CAUTION: existing out of tree drivers have to initialize .base_minor
explicitly if the descriptor does not reside on clean memory.

---

 include/cobalt/kernel/rtdm/driver.h |    2 ++
 kernel/cobalt/rtdm/device.c         |   19 +++++++++++++------
 kernel/drivers/udd/udd.c            |    1 +
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/include/cobalt/kernel/rtdm/driver.h 
b/include/cobalt/kernel/rtdm/driver.h
index 42f0b73..bcf6e54 100644
--- a/include/cobalt/kernel/rtdm/driver.h
+++ b/include/cobalt/kernel/rtdm/driver.h
@@ -280,6 +280,8 @@ struct rtdm_driver {
         * allocate a chrdev region for named devices.
         */
        int device_count;
+       /** Base minor for named devices. */
+       int base_minor;
        /** Reserved area */
        struct {
                union {
diff --git a/kernel/cobalt/rtdm/device.c b/kernel/cobalt/rtdm/device.c
index 3e6a46b..6c6299f 100644
--- a/kernel/cobalt/rtdm/device.c
+++ b/kernel/cobalt/rtdm/device.c
@@ -283,17 +283,23 @@ static int register_driver(struct rtdm_driver *drv)
                return -EINVAL;
        }
 
-       if (drv->device_count <= 0)
+       if (drv->device_count <= 0 ||
+           drv->device_count >= RTDM_MAX_MINOR)
+               return -EINVAL;
+
+       if (drv->base_minor < 0 ||
+           drv->base_minor >= RTDM_MAX_MINOR)
                return -EINVAL;
 
        if ((drv->device_flags & RTDM_NAMED_DEVICE) == 0)
                goto done;
 
-       ret = alloc_chrdev_region(&rdev, 0, drv->device_count,
+       ret = alloc_chrdev_region(&rdev, drv->base_minor, drv->device_count,
                                  drv->profile_info.name);
        if (ret) {
-               printk(XENO_WARNING "cannot allocate chrdev region %s[0..%d]\n",
-                      drv->profile_info.name, drv->device_count - 1);
+               printk(XENO_WARNING "cannot allocate chrdev region 
%s[%d..%d]\n",
+                      drv->profile_info.name, drv->base_minor,
+                      drv->base_minor + drv->device_count - 1);
                return ret;
        }
 
@@ -333,7 +339,7 @@ static void unregister_driver(struct rtdm_driver *drv)
 
        if (drv->device_flags & RTDM_NAMED_DEVICE) {
                cdev_del(&drv->named.cdev);
-               unregister_chrdev_region(MKDEV(drv->named.major, 0),
+               unregister_chrdev_region(MKDEV(drv->named.major, 
drv->base_minor),
                                         drv->device_count);
        }
 }
@@ -402,7 +408,8 @@ int rtdm_dev_register(struct rtdm_device *dev)
        if (drv->device_flags & RTDM_NAMED_DEVICE) {
                if (drv->device_flags & RTDM_FIXED_MINOR) {
                        minor = dev->minor;
-                       if (minor < 0 || minor >= drv->device_count) {
+                       if (minor < 0 ||
+                           minor >= drv->base_minor + drv->device_count) {
                                ret = -ENXIO;
                                goto fail;
                        }
diff --git a/kernel/drivers/udd/udd.c b/kernel/drivers/udd/udd.c
index 6f12235..e14ff0b 100644
--- a/kernel/drivers/udd/udd.c
+++ b/kernel/drivers/udd/udd.c
@@ -290,6 +290,7 @@ static inline int register_mapper(struct udd_device *udd)
                                  RTDM_SUBCLASS_GENERIC, 0);
        drv->device_flags = RTDM_NAMED_DEVICE|RTDM_FIXED_MINOR;
        drv->device_count = UDD_NR_MAPS;
+       drv->base_minor = 0;
        drv->ops = (struct rtdm_fd_ops){
                .open           =       mapper_open,
                .close          =       mapper_close,


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
https://xenomai.org/mailman/listinfo/xenomai-git

Reply via email to