diff options
author | Andrew Tridgell <tridge@samba.org> | 2013-12-07 22:43:54 +1100 |
---|---|---|
committer | Lorenz Meier <lm@inf.ethz.ch> | 2013-12-10 11:31:04 +0100 |
commit | b2b9665e4460ce25718509b7abb75c4b702c4c5f (patch) | |
tree | 259b860871b0c8dd98c1b39660d948443855d9f0 /src | |
parent | 895dc3a2bbf172c7a2095d3a815cdd13d1388816 (diff) | |
download | px4-firmware-b2b9665e4460ce25718509b7abb75c4b702c4c5f.tar.gz px4-firmware-b2b9665e4460ce25718509b7abb75c4b702c4c5f.tar.bz2 px4-firmware-b2b9665e4460ce25718509b7abb75c4b702c4c5f.zip |
device: added register_class_devname() API
this allows drivers to register generic device names for a device
class, with automatic class instance handling
Diffstat (limited to 'src')
-rw-r--r-- | src/drivers/device/cdev.cpp | 36 | ||||
-rw-r--r-- | src/drivers/device/device.h | 22 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/drivers/device/cdev.cpp b/src/drivers/device/cdev.cpp index 47ebcd40a..7954ce5ab 100644 --- a/src/drivers/device/cdev.cpp +++ b/src/drivers/device/cdev.cpp @@ -109,6 +109,42 @@ CDev::~CDev() } int +CDev::register_class_devname(const char *class_devname) +{ + if (class_devname == nullptr) { + return -EINVAL; + } + int class_instance = 0; + int ret = -ENOSPC; + while (class_instance < 4) { + if (class_instance == 0) { + ret = register_driver(class_devname, &fops, 0666, (void *)this); + if (ret == OK) break; + } else { + char name[32]; + snprintf(name, sizeof(name), "%s%u", class_devname, class_instance); + ret = register_driver(name, &fops, 0666, (void *)this); + if (ret == OK) break; + } + class_instance++; + } + if (class_instance == 4) + return ret; + return class_instance; +} + +int +CDev::unregister_class_devname(const char *class_devname, unsigned class_instance) +{ + if (class_instance > 0) { + char name[32]; + snprintf(name, sizeof(name), "%s%u", class_devname, class_instance); + return unregister_driver(name); + } + return unregister_driver(class_devname); +} + +int CDev::init() { // base class init first diff --git a/src/drivers/device/device.h b/src/drivers/device/device.h index a9ed5d77c..0235f6284 100644 --- a/src/drivers/device/device.h +++ b/src/drivers/device/device.h @@ -396,6 +396,25 @@ protected: */ virtual int close_last(struct file *filp); + /** + * Register a class device name, automatically adding device + * class instance suffix if need be. + * + * @param class_devname Device class name + * @return class_instamce Class instance created, or -errno on failure + */ + virtual int register_class_devname(const char *class_devname); + + /** + * Register a class device name, automatically adding device + * class instance suffix if need be. + * + * @param class_devname Device class name + * @param class_instance Device class instance from register_class_devname() + * @return OK on success, -errno otherwise + */ + virtual int unregister_class_devname(const char *class_devname, unsigned class_instance); + private: static const unsigned _max_pollwaiters = 8; @@ -488,4 +507,7 @@ private: } // namespace device +// class instance for primary driver of each class +#define CLASS_DEVICE_PRIMARY 0 + #endif /* _DEVICE_DEVICE_H */ |