10 struct device_user dep;
12 device_state_cb set_state;
16 static void free_vlan_if(struct device *iface)
18 struct vlan_device *vldev;
20 vldev = container_of(iface, struct vlan_device, dev);
21 device_remove_user(&vldev->dep);
22 device_cleanup(&vldev->dev);
26 static int vlan_set_device_state(struct device *dev, bool up)
28 struct vlan_device *vldev;
31 vldev = container_of(dev, struct vlan_device, dev);
33 vldev->set_state(dev, false);
35 device_release(&vldev->dep);
39 ret = device_claim(&vldev->dep);
43 system_vlan_add(vldev->dep.dev, vldev->id);
44 ret = vldev->set_state(dev, true);
46 device_release(&vldev->dep);
51 static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
53 struct vlan_device *vldev;
55 vldev = container_of(dep, struct vlan_device, dep);
58 device_set_present(&vldev->dev, true);
60 case DEV_EVENT_REMOVE:
61 device_set_present(&vldev->dev, false);
68 static struct device *get_vlan_device(struct device *dev, int id, bool create)
70 static const struct device_type vlan_type = {
72 .config_params = &device_attr_list,
75 struct vlan_device *vldev;
76 struct device_user *dep;
78 /* look for an existing interface before creating a new one */
79 list_for_each_entry(dep, &dev->users, list) {
80 if (dep->cb != vlan_dev_cb)
83 vldev = container_of(dep, struct vlan_device, dep);
93 vldev = calloc(1, sizeof(*vldev));
94 snprintf(vldev->dev.ifname, IFNAMSIZ, "%s.%d", dev->ifname, id);
96 device_init(&vldev->dev, &vlan_type, NULL);
97 vldev->dev.default_config = true;
99 vldev->set_state = vldev->dev.set_state;
100 vldev->dev.set_state = vlan_set_device_state;
104 vldev->dep.cb = vlan_dev_cb;
105 device_add_user(&vldev->dep, dev);
110 static char *split_vlan(char *s)
123 struct device *get_vlan_device_chain(const char *ifname, bool create)
125 struct device *dev = NULL;
126 char *buf, *s, *next, *err = NULL;
129 buf = strdup(ifname);
134 dev = device_get(buf, create);
139 next = split_vlan(s);
140 id = strtoul(s, &err, 10);
144 dev = get_vlan_device(dev, id, create);