mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-05 15:40:34 +08:00
device: added register_class_devname() API
this allows drivers to register generic device names for a device class, with automatic class instance handling
This commit is contained in:
committed by
Lorenz Meier
parent
895dc3a2bb
commit
b2b9665e44
@@ -108,6 +108,42 @@ CDev::~CDev()
|
||||
unregister_driver(_devname);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user