dtsi: rk312x-sdk: update rk818 battery node
[firefly-linux-kernel-4.4.55.git] / fs / sysfs / group.c
1 /*
2  * fs/sysfs/group.c - Operations for adding/removing multiple files at once.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released undert the GPL v2. 
8  *
9  */
10
11 #include <linux/kobject.h>
12 #include <linux/module.h>
13 #include <linux/dcache.h>
14 #include <linux/namei.h>
15 #include <linux/err.h>
16 #include "sysfs.h"
17
18
19 static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
20                          const struct attribute_group *grp)
21 {
22         struct attribute *const* attr;
23         int i;
24
25         for (i = 0, attr = grp->attrs; *attr; i++, attr++)
26                 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
27 }
28
29 static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
30                         const struct attribute_group *grp, int update)
31 {
32         struct attribute *const* attr;
33         int error = 0, i;
34
35         for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
36                 umode_t mode = 0;
37
38                 /* in update mode, we're changing the permissions or
39                  * visibility.  Do this by first removing then
40                  * re-adding (if required) the file */
41                 if (update)
42                         sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
43                 if (grp->is_visible) {
44                         mode = grp->is_visible(kobj, *attr, i);
45                         if (!mode)
46                                 continue;
47                 }
48                 error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR,
49                                             (*attr)->mode | mode);
50                 if (unlikely(error))
51                         break;
52         }
53         if (error)
54                 remove_files(dir_sd, kobj, grp);
55         return error;
56 }
57
58
59 static int internal_create_group(struct kobject *kobj, int update,
60                                  const struct attribute_group *grp)
61 {
62         struct sysfs_dirent *sd;
63         int error;
64
65         BUG_ON(!kobj || (!update && !kobj->sd));
66
67         /* Updates may happen before the object has been instantiated */
68         if (unlikely(update && !kobj->sd))
69                 return -EINVAL;
70         if (!grp->attrs) {
71                 WARN(1, "sysfs: attrs not set by subsystem for group: %s/%s\n",
72                         kobj->name, grp->name ? "" : grp->name);
73                 return -EINVAL;
74         }
75         if (grp->name) {
76                 error = sysfs_create_subdir(kobj, grp->name, &sd);
77                 if (error)
78                         return error;
79         } else
80                 sd = kobj->sd;
81         sysfs_get(sd);
82         error = create_files(sd, kobj, grp, update);
83         if (error) {
84                 if (grp->name)
85                         sysfs_remove_subdir(sd);
86         }
87         sysfs_put(sd);
88         return error;
89 }
90
91 /**
92  * sysfs_create_group - given a directory kobject, create an attribute group
93  * @kobj:       The kobject to create the group on
94  * @grp:        The attribute group to create
95  *
96  * This function creates a group for the first time.  It will explicitly
97  * warn and error if any of the attribute files being created already exist.
98  *
99  * Returns 0 on success or error.
100  */
101 int sysfs_create_group(struct kobject *kobj,
102                        const struct attribute_group *grp)
103 {
104         return internal_create_group(kobj, 0, grp);
105 }
106
107 /**
108  * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
109  * @kobj:       The kobject to create the group on
110  * @groups:     The attribute groups to create, NULL terminated
111  *
112  * This function creates a bunch of attribute groups.  If an error occurs when
113  * creating a group, all previously created groups will be removed, unwinding
114  * everything back to the original state when this function was called.
115  * It will explicitly warn and error if any of the attribute files being
116  * created already exist.
117  *
118  * Returns 0 on success or error code from sysfs_create_groups on error.
119  */
120 int sysfs_create_groups(struct kobject *kobj,
121                         const struct attribute_group **groups)
122 {
123         int error = 0;
124         int i;
125
126         if (!groups)
127                 return 0;
128
129         for (i = 0; groups[i]; i++) {
130                 error = sysfs_create_group(kobj, groups[i]);
131                 if (error) {
132                         while (--i >= 0)
133                                 sysfs_remove_group(kobj, groups[i]);
134                         break;
135                 }
136         }
137         return error;
138 }
139 EXPORT_SYMBOL_GPL(sysfs_create_groups);
140
141 /**
142  * sysfs_update_group - given a directory kobject, update an attribute group
143  * @kobj:       The kobject to update the group on
144  * @grp:        The attribute group to update
145  *
146  * This function updates an attribute group.  Unlike
147  * sysfs_create_group(), it will explicitly not warn or error if any
148  * of the attribute files being created already exist.  Furthermore,
149  * if the visibility of the files has changed through the is_visible()
150  * callback, it will update the permissions and add or remove the
151  * relevant files.
152  *
153  * The primary use for this function is to call it after making a change
154  * that affects group visibility.
155  *
156  * Returns 0 on success or error.
157  */
158 int sysfs_update_group(struct kobject *kobj,
159                        const struct attribute_group *grp)
160 {
161         return internal_create_group(kobj, 1, grp);
162 }
163
164
165
166 void sysfs_remove_group(struct kobject * kobj, 
167                         const struct attribute_group * grp)
168 {
169         struct sysfs_dirent *dir_sd = kobj->sd;
170         struct sysfs_dirent *sd;
171
172         if (grp->name) {
173                 sd = sysfs_get_dirent(dir_sd, NULL, grp->name);
174                 if (!sd) {
175                         WARN(!sd, KERN_WARNING "sysfs group %p not found for "
176                                 "kobject '%s'\n", grp, kobject_name(kobj));
177                         return;
178                 }
179         } else
180                 sd = sysfs_get(dir_sd);
181
182         remove_files(sd, kobj, grp);
183         if (grp->name)
184                 sysfs_remove_subdir(sd);
185
186         sysfs_put(sd);
187 }
188
189 /**
190  * sysfs_remove_groups - remove a list of groups
191  *
192  * kobj:        The kobject for the groups to be removed from
193  * groups:      NULL terminated list of groups to be removed
194  *
195  * If groups is not NULL, the all groups will be removed from the kobject
196  */
197 void sysfs_remove_groups(struct kobject *kobj,
198                          const struct attribute_group **groups)
199 {
200         int i;
201
202         if (!groups)
203                 return;
204         for (i = 0; groups[i]; i++)
205                 sysfs_remove_group(kobj, groups[i]);
206 }
207 EXPORT_SYMBOL_GPL(sysfs_remove_groups);
208
209 /**
210  * sysfs_merge_group - merge files into a pre-existing attribute group.
211  * @kobj:       The kobject containing the group.
212  * @grp:        The files to create and the attribute group they belong to.
213  *
214  * This function returns an error if the group doesn't exist or any of the
215  * files already exist in that group, in which case none of the new files
216  * are created.
217  */
218 int sysfs_merge_group(struct kobject *kobj,
219                        const struct attribute_group *grp)
220 {
221         struct sysfs_dirent *dir_sd;
222         int error = 0;
223         struct attribute *const *attr;
224         int i;
225
226         dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
227         if (!dir_sd)
228                 return -ENOENT;
229
230         for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
231                 error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
232         if (error) {
233                 while (--i >= 0)
234                         sysfs_hash_and_remove(dir_sd, NULL, (*--attr)->name);
235         }
236         sysfs_put(dir_sd);
237
238         return error;
239 }
240 EXPORT_SYMBOL_GPL(sysfs_merge_group);
241
242 /**
243  * sysfs_unmerge_group - remove files from a pre-existing attribute group.
244  * @kobj:       The kobject containing the group.
245  * @grp:        The files to remove and the attribute group they belong to.
246  */
247 void sysfs_unmerge_group(struct kobject *kobj,
248                        const struct attribute_group *grp)
249 {
250         struct sysfs_dirent *dir_sd;
251         struct attribute *const *attr;
252
253         dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
254         if (dir_sd) {
255                 for (attr = grp->attrs; *attr; ++attr)
256                         sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
257                 sysfs_put(dir_sd);
258         }
259 }
260 EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
261
262 /**
263  * sysfs_add_link_to_group - add a symlink to an attribute group.
264  * @kobj:       The kobject containing the group.
265  * @group_name: The name of the group.
266  * @target:     The target kobject of the symlink to create.
267  * @link_name:  The name of the symlink to create.
268  */
269 int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
270                             struct kobject *target, const char *link_name)
271 {
272         struct sysfs_dirent *dir_sd;
273         int error = 0;
274
275         dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name);
276         if (!dir_sd)
277                 return -ENOENT;
278
279         error = sysfs_create_link_sd(dir_sd, target, link_name);
280         sysfs_put(dir_sd);
281
282         return error;
283 }
284 EXPORT_SYMBOL_GPL(sysfs_add_link_to_group);
285
286 /**
287  * sysfs_remove_link_from_group - remove a symlink from an attribute group.
288  * @kobj:       The kobject containing the group.
289  * @group_name: The name of the group.
290  * @link_name:  The name of the symlink to remove.
291  */
292 void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
293                                   const char *link_name)
294 {
295         struct sysfs_dirent *dir_sd;
296
297         dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name);
298         if (dir_sd) {
299                 sysfs_hash_and_remove(dir_sd, NULL, link_name);
300                 sysfs_put(dir_sd);
301         }
302 }
303 EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);
304
305 EXPORT_SYMBOL_GPL(sysfs_create_group);
306 EXPORT_SYMBOL_GPL(sysfs_update_group);
307 EXPORT_SYMBOL_GPL(sysfs_remove_group);