static void __init dev_init(void)
{
- avl_init(&devices, avl_strcmp, false, NULL);
+ avl_init(&devices, avl_strcmp, true, NULL);
}
static void device_broadcast_event(struct device *dev, enum device_event ev)
device_broadcast_event(dev, DEV_EVENT_DOWN);
}
-int check_device_state(struct device *dev)
+int device_check_state(struct device *dev)
{
if (!dev->type->check_state)
return 0;
if (ret < 0)
return ret;
- check_device_state(dev);
+ device_check_state(dev);
return 0;
}
if (!create)
return NULL;
+ D(DEVICE, "Create simple device '%s'\n", name);
dev = calloc(1, sizeof(*dev));
device_init(dev, &simple_device_type, name);
return dev;
}
+static void
+device_delete(struct device *dev)
+{
+ if (!dev->avl.key)
+ return;
+
+ D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
+ avl_delete(&devices, &dev->avl);
+ dev->avl.key = NULL;
+}
+
void device_cleanup(struct device *dev)
{
struct device_user *dep, *tmp;
dep->cb(dep, DEV_EVENT_REMOVE);
}
- if (dev->avl.key)
- avl_delete(&devices, &dev->avl);
+ device_delete(dev);
}
void device_set_present(struct device *dev, bool state)
__device_free_unused(dev);
}
+void
+device_init_pending(void)
+{
+ struct device *dev, *tmp;
+
+ avl_for_each_element_safe(&devices, dev, avl, tmp) {
+ if (!dev->config_pending)
+ continue;
+
+ dev->type->config_init(dev);
+ dev->config_pending = false;
+ }
+}
+
enum dev_change_type
device_reload_config(struct device *dev, struct blob_attr *attr)
{
}
static enum dev_change_type
-device_check_config(struct device *dev, struct blob_attr *attr)
+device_check_config(struct device *dev, const struct device_type *type,
+ struct blob_attr *attr)
{
+ if (type != dev->type)
+ return DEV_CONFIG_RECREATE;
+
if (dev->type->reload)
return dev->type->reload(dev, attr);
device_set_present(odev, false);
list_for_each_entry_safe(dep, tmp, &odev->users, list) {
- list_move_tail(&dep->list, &dev->users);
- dep->dev = dev;
+ device_remove_user(dep);
+ device_add_user(dep, dev);
}
device_free(odev);
struct device *odev = NULL, *dev;
enum dev_change_type change;
+ D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
+ config = config_memdup(config);
+ if (!config)
+ return NULL;
+
odev = device_get(name, false);
if (odev) {
- change = device_check_config(odev, config);
+ change = device_check_config(odev, type, config);
switch (change) {
case DEV_CONFIG_APPLIED:
+ D(DEVICE, "Device '%s': config applied\n", odev->ifname);
free(odev->config);
- odev->config = config_memdup(config);
+ odev->config = config;
if (odev->present) {
device_set_present(odev, false);
device_set_present(odev, true);
}
- /* fall through */
+ free(config);
+ return odev;
case DEV_CONFIG_NO_CHANGE:
+ D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
+ free(config);
return odev;
case DEV_CONFIG_RECREATE:
+ D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
+ device_delete(odev);
break;
}
}
dev = type->create(config);
- dev->config = config_memdup(config);
+ if (!dev)
+ return NULL;
+
+ dev->config = config;
if (odev)
device_replace(dev, odev);
+ if (!config_init && dev->config_pending)
+ type->config_init(dev);
+
return dev;
}