转自:https://blog.csdn.net/tiantao2012/article/details/52222462
在看cpufreq驱动的时候有遇到
这行code是啥意思呢?
subsys_interface_register(&cpufreq_interface);
1576 static struct subsys_interface cpufreq_interface = {
1577 .name = "cpufreq",
1578 .subsys = &cpu_subsys,
1579 .add_dev = cpufreq_add_dev,
1580 .remove_dev = cpufreq_remove_dev,
1581 };
我们看看subsys_interface_register的实现。
1125 int subsys_interface_register(struct subsys_interface *sif)
1126 {
1127 struct bus_type *subsys;
1128 struct subsys_dev_iter iter;
1129 struct device *dev;
1130
1131 if (!sif || !sif->subsys)
1132 return -ENODEV;
1133
1134 subsys = bus_get(sif->subsys);
1135 if (!subsys)
1136 return -EINVAL;
1137
1138 mutex_lock(&subsys->p->mutex);
1139 list_add_tail(&sif->node, &subsys->p->interfaces);
1140 if (sif->add_dev) {
1141 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1142 while ((dev = subsys_dev_iter_next(&iter)))
1143 sif->add_dev(dev, sif);
1144 subsys_dev_iter_exit(&iter);
1145 }
1146 mutex_unlock(&subsys->p->mutex);
1147
1148 return 0;
1149 }
1134行subsys就等于1578行的cpu_sybsys.
1140行的sif->add_dev 就等1579行的cpufreq_add_dev,所以肯定不为NULL。
1142行会遍历cpu_sybsys,为每一个cpu调用cpufreq_add_dev 方法。也就是有几个cpu,cpufreq_add_dev 方法就没调用几次。
然后是子系统的注销:
同理注销的接口如下:
1157 void subsys_interface_unregister(struct subsys_interface *sif)
1158 {
1159 struct bus_type *subsys;
1160 struct subsys_dev_iter iter;
1161 struct device *dev;
1162
1163 if (!sif || !sif->subsys)
1164 return;
1165
1166 subsys = sif->subsys;
1167
1168 mutex_lock(&subsys->p->mutex);
1169 list_del_init(&sif->node);
1170 if (sif->remove_dev) {
1171 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1172 while ((dev = subsys_dev_iter_next(&iter)))
1173 sif->remove_dev(dev, sif);
1174 subsys_dev_iter_exit(&iter);
1175 }
1176 mutex_unlock(&subsys->p->mutex);
1177
1178 bus_put(subsys);
1179 }
1180 EXPORT_SYMBOL_GPL(subsys_interface_unregister);