9
0
Fork 0

driver: allow register device with specific id

if you specify id = 0 the next available id will be taken
otherwise fail if already registered

before if you specify one it will have use the next free id anyway

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
This commit is contained in:
Jean-Christophe PLAGNIOL-VILLARD 2010-09-12 19:19:08 +08:00
parent 55819d58d4
commit 427c608583
1 changed files with 22 additions and 4 deletions

View File

@ -56,14 +56,24 @@ struct device_d *get_device_by_name(const char *name)
return NULL;
}
struct device_d *get_device_by_name_id(const char *name, int id)
{
struct device_d *dev;
for_each_device(dev) {
if(!strcmp(dev->name, name) && id == dev->id)
return dev;
}
return NULL;
}
int get_free_deviceid(const char *name_template)
{
int i = 0;
char name[MAX_DRIVER_NAME + 3];
while (1) {
sprintf(name, "%s%d", name_template, i);
if (!get_device_by_name(name))
if (!get_device_by_name_id(name_template, i))
return i;
i++;
};
@ -95,7 +105,15 @@ int register_device(struct device_d *new_device)
{
struct driver_d *drv;
new_device->id = get_free_deviceid(new_device->name);
if (new_device->id == 0) {
new_device->id = get_free_deviceid(new_device->name);
} else {
if (get_device_by_name_id(new_device->name, new_device->id)) {
eprintf("register_device: already registered %s\n",
dev_name(new_device));
return -EINVAL;
}
}
debug ("register_device: %s\n",new_device->name);