drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / lib / klist.c
CommitLineData
9a19fea4 1/*
c3bb7fad 2 * klist.c - Routines for manipulating klists.
9a19fea4 3 *
c3bb7fad 4 * Copyright (C) 2005 Patrick Mochel
9a19fea4 5 *
c3bb7fad 6 * This file is released under the GPL v2.
9a19fea4 7 *
c3bb7fad
GKH
8 * This klist interface provides a couple of structures that wrap around
9 * struct list_head to provide explicit list "head" (struct klist) and list
10 * "node" (struct klist_node) objects. For struct klist, a spinlock is
11 * included that protects access to the actual list itself. struct
12 * klist_node provides a pointer to the klist that owns it and a kref
13 * reference count that indicates the number of current users of that node
14 * in the list.
9a19fea4 15 *
c3bb7fad
GKH
16 * The entire point is to provide an interface for iterating over a list
17 * that is safe and allows for modification of the list during the
18 * iteration (e.g. insertion and removal), including modification of the
19 * current node on the list.
9a19fea4 20 *
c3bb7fad
GKH
21 * It works using a 3rd object type - struct klist_iter - that is declared
22 * and initialized before an iteration. klist_next() is used to acquire the
23 * next element in the list. It returns NULL if there are no more items.
24 * Internally, that routine takes the klist's lock, decrements the
25 * reference count of the previous klist_node and increments the count of
26 * the next klist_node. It then drops the lock and returns.
9a19fea4 27 *
c3bb7fad
GKH
28 * There are primitives for adding and removing nodes to/from a klist.
29 * When deleting, klist_del() will simply decrement the reference count.
30 * Only when the count goes to 0 is the node removed from the list.
31 * klist_remove() will try to delete the node from the list and block until
32 * it is actually removed. This is useful for objects (like devices) that
33 * have been removed from the system and must be freed (but must wait until
34 * all accessors have finished).
9a19fea4
PM
35 */
36
37#include <linux/klist.h>
8bc3bcc9 38#include <linux/export.h>
210272a2 39#include <linux/sched.h>
9a19fea4 40
a1ed5b0c
TH
41/*
42 * Use the lowest bit of n_klist to mark deleted nodes and exclude
43 * dead ones from iteration.
44 */
45#define KNODE_DEAD 1LU
46#define KNODE_KLIST_MASK ~KNODE_DEAD
47
48static struct klist *knode_klist(struct klist_node *knode)
49{
50 return (struct klist *)
51 ((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
52}
53
54static bool knode_dead(struct klist_node *knode)
55{
56 return (unsigned long)knode->n_klist & KNODE_DEAD;
57}
58
59static void knode_set_klist(struct klist_node *knode, struct klist *klist)
60{
61 knode->n_klist = klist;
62 /* no knode deserves to start its life dead */
63 WARN_ON(knode_dead(knode));
64}
65
66static void knode_kill(struct klist_node *knode)
67{
68 /* and no knode should die twice ever either, see we're very humane */
69 WARN_ON(knode_dead(knode));
70 *(unsigned long *)&knode->n_klist |= KNODE_DEAD;
71}
9a19fea4
PM
72
73/**
c3bb7fad
GKH
74 * klist_init - Initialize a klist structure.
75 * @k: The klist we're initializing.
76 * @get: The get function for the embedding object (NULL if none)
77 * @put: The put function for the embedding object (NULL if none)
34bb61f9
JB
78 *
79 * Initialises the klist structure. If the klist_node structures are
80 * going to be embedded in refcounted objects (necessary for safe
81 * deletion) then the get/put arguments are used to initialise
82 * functions that take and release references on the embedding
83 * objects.
9a19fea4 84 */
c3bb7fad 85void klist_init(struct klist *k, void (*get)(struct klist_node *),
34bb61f9 86 void (*put)(struct klist_node *))
9a19fea4
PM
87{
88 INIT_LIST_HEAD(&k->k_list);
89 spin_lock_init(&k->k_lock);
34bb61f9
JB
90 k->get = get;
91 k->put = put;
9a19fea4 92}
9a19fea4
PM
93EXPORT_SYMBOL_GPL(klist_init);
94
c3bb7fad 95static void add_head(struct klist *k, struct klist_node *n)
9a19fea4
PM
96{
97 spin_lock(&k->k_lock);
98 list_add(&n->n_node, &k->k_list);
99 spin_unlock(&k->k_lock);
100}
101
c3bb7fad 102static void add_tail(struct klist *k, struct klist_node *n)
9a19fea4
PM
103{
104 spin_lock(&k->k_lock);
105 list_add_tail(&n->n_node, &k->k_list);
106 spin_unlock(&k->k_lock);
107}
108
c3bb7fad 109static void klist_node_init(struct klist *k, struct klist_node *n)
9a19fea4
PM
110{
111 INIT_LIST_HEAD(&n->n_node);
9a19fea4 112 kref_init(&n->n_ref);
a1ed5b0c 113 knode_set_klist(n, k);
34bb61f9
JB
114 if (k->get)
115 k->get(n);
9a19fea4
PM
116}
117
9a19fea4 118/**
c3bb7fad
GKH
119 * klist_add_head - Initialize a klist_node and add it to front.
120 * @n: node we're adding.
121 * @k: klist it's going on.
9a19fea4 122 */
c3bb7fad 123void klist_add_head(struct klist_node *n, struct klist *k)
9a19fea4
PM
124{
125 klist_node_init(k, n);
126 add_head(k, n);
127}
9a19fea4
PM
128EXPORT_SYMBOL_GPL(klist_add_head);
129
9a19fea4 130/**
c3bb7fad
GKH
131 * klist_add_tail - Initialize a klist_node and add it to back.
132 * @n: node we're adding.
133 * @k: klist it's going on.
9a19fea4 134 */
c3bb7fad 135void klist_add_tail(struct klist_node *n, struct klist *k)
9a19fea4
PM
136{
137 klist_node_init(k, n);
138 add_tail(k, n);
139}
9a19fea4
PM
140EXPORT_SYMBOL_GPL(klist_add_tail);
141
93dd4001
TH
142/**
143 * klist_add_after - Init a klist_node and add it after an existing node
144 * @n: node we're adding.
145 * @pos: node to put @n after
146 */
147void klist_add_after(struct klist_node *n, struct klist_node *pos)
148{
a1ed5b0c 149 struct klist *k = knode_klist(pos);
93dd4001
TH
150
151 klist_node_init(k, n);
152 spin_lock(&k->k_lock);
153 list_add(&n->n_node, &pos->n_node);
154 spin_unlock(&k->k_lock);
155}
156EXPORT_SYMBOL_GPL(klist_add_after);
157
158/**
159 * klist_add_before - Init a klist_node and add it before an existing node
160 * @n: node we're adding.
161 * @pos: node to put @n after
162 */
163void klist_add_before(struct klist_node *n, struct klist_node *pos)
164{
a1ed5b0c 165 struct klist *k = knode_klist(pos);
93dd4001
TH
166
167 klist_node_init(k, n);
168 spin_lock(&k->k_lock);
169 list_add_tail(&n->n_node, &pos->n_node);
170 spin_unlock(&k->k_lock);
171}
172EXPORT_SYMBOL_GPL(klist_add_before);
173
210272a2
MW
174struct klist_waiter {
175 struct list_head list;
176 struct klist_node *node;
177 struct task_struct *process;
178 int woken;
179};
180
181static DEFINE_SPINLOCK(klist_remove_lock);
182static LIST_HEAD(klist_remove_waiters);
183
c3bb7fad 184static void klist_release(struct kref *kref)
9a19fea4 185{
210272a2 186 struct klist_waiter *waiter, *tmp;
c3bb7fad 187 struct klist_node *n = container_of(kref, struct klist_node, n_ref);
7e9f4b2d 188
a1ed5b0c 189 WARN_ON(!knode_dead(n));
9a19fea4 190 list_del(&n->n_node);
210272a2
MW
191 spin_lock(&klist_remove_lock);
192 list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
193 if (waiter->node != n)
194 continue;
195
ac5a2962 196 list_del(&waiter->list);
210272a2
MW
197 waiter->woken = 1;
198 mb();
199 wake_up_process(waiter->process);
210272a2
MW
200 }
201 spin_unlock(&klist_remove_lock);
a1ed5b0c 202 knode_set_klist(n, NULL);
9a19fea4
PM
203}
204
c3bb7fad 205static int klist_dec_and_del(struct klist_node *n)
9a19fea4
PM
206{
207 return kref_put(&n->n_ref, klist_release);
208}
209
a1ed5b0c 210static void klist_put(struct klist_node *n, bool kill)
9a19fea4 211{
a1ed5b0c 212 struct klist *k = knode_klist(n);
7e9f4b2d 213 void (*put)(struct klist_node *) = k->put;
9a19fea4
PM
214
215 spin_lock(&k->k_lock);
a1ed5b0c
TH
216 if (kill)
217 knode_kill(n);
7e9f4b2d
AS
218 if (!klist_dec_and_del(n))
219 put = NULL;
9a19fea4 220 spin_unlock(&k->k_lock);
7e9f4b2d
AS
221 if (put)
222 put(n);
9a19fea4 223}
a1ed5b0c
TH
224
225/**
226 * klist_del - Decrement the reference count of node and try to remove.
227 * @n: node we're deleting.
228 */
229void klist_del(struct klist_node *n)
230{
231 klist_put(n, true);
232}
9a19fea4
PM
233EXPORT_SYMBOL_GPL(klist_del);
234
9a19fea4 235/**
c3bb7fad
GKH
236 * klist_remove - Decrement the refcount of node and wait for it to go away.
237 * @n: node we're removing.
9a19fea4 238 */
c3bb7fad 239void klist_remove(struct klist_node *n)
9a19fea4 240{
210272a2
MW
241 struct klist_waiter waiter;
242
243 waiter.node = n;
244 waiter.process = current;
245 waiter.woken = 0;
246 spin_lock(&klist_remove_lock);
247 list_add(&waiter.list, &klist_remove_waiters);
248 spin_unlock(&klist_remove_lock);
249
7e9f4b2d 250 klist_del(n);
210272a2
MW
251
252 for (;;) {
253 set_current_state(TASK_UNINTERRUPTIBLE);
254 if (waiter.woken)
255 break;
256 schedule();
257 }
258 __set_current_state(TASK_RUNNING);
9a19fea4 259}
9a19fea4
PM
260EXPORT_SYMBOL_GPL(klist_remove);
261
8b0c250b 262/**
c3bb7fad
GKH
263 * klist_node_attached - Say whether a node is bound to a list or not.
264 * @n: Node that we're testing.
8b0c250b 265 */
c3bb7fad 266int klist_node_attached(struct klist_node *n)
8b0c250b
PM
267{
268 return (n->n_klist != NULL);
269}
8b0c250b
PM
270EXPORT_SYMBOL_GPL(klist_node_attached);
271
9a19fea4 272/**
c3bb7fad
GKH
273 * klist_iter_init_node - Initialize a klist_iter structure.
274 * @k: klist we're iterating.
275 * @i: klist_iter we're filling.
276 * @n: node to start with.
9a19fea4 277 *
c3bb7fad
GKH
278 * Similar to klist_iter_init(), but starts the action off with @n,
279 * instead of with the list head.
9a19fea4 280 */
c3bb7fad
GKH
281void klist_iter_init_node(struct klist *k, struct klist_iter *i,
282 struct klist_node *n)
9a19fea4
PM
283{
284 i->i_klist = k;
00707985
JB
285 i->i_cur = NULL;
286 if (n && kref_get_unless_zero(&n->n_ref))
287 i->i_cur = n;
9a19fea4 288}
9a19fea4
PM
289EXPORT_SYMBOL_GPL(klist_iter_init_node);
290
9a19fea4 291/**
c3bb7fad
GKH
292 * klist_iter_init - Iniitalize a klist_iter structure.
293 * @k: klist we're iterating.
294 * @i: klist_iter structure we're filling.
9a19fea4 295 *
c3bb7fad 296 * Similar to klist_iter_init_node(), but start with the list head.
9a19fea4 297 */
c3bb7fad 298void klist_iter_init(struct klist *k, struct klist_iter *i)
9a19fea4
PM
299{
300 klist_iter_init_node(k, i, NULL);
301}
9a19fea4
PM
302EXPORT_SYMBOL_GPL(klist_iter_init);
303
9a19fea4 304/**
c3bb7fad
GKH
305 * klist_iter_exit - Finish a list iteration.
306 * @i: Iterator structure.
9a19fea4 307 *
c3bb7fad
GKH
308 * Must be called when done iterating over list, as it decrements the
309 * refcount of the current node. Necessary in case iteration exited before
310 * the end of the list was reached, and always good form.
9a19fea4 311 */
c3bb7fad 312void klist_iter_exit(struct klist_iter *i)
9a19fea4
PM
313{
314 if (i->i_cur) {
a1ed5b0c 315 klist_put(i->i_cur, false);
9a19fea4
PM
316 i->i_cur = NULL;
317 }
318}
9a19fea4
PM
319EXPORT_SYMBOL_GPL(klist_iter_exit);
320
c3bb7fad 321static struct klist_node *to_klist_node(struct list_head *n)
9a19fea4
PM
322{
323 return container_of(n, struct klist_node, n_node);
324}
325
9a19fea4 326/**
c3bb7fad
GKH
327 * klist_next - Ante up next node in list.
328 * @i: Iterator structure.
9a19fea4 329 *
c3bb7fad
GKH
330 * First grab list lock. Decrement the reference count of the previous
331 * node, if there was one. Grab the next node, increment its reference
332 * count, drop the lock, and return that next node.
9a19fea4 333 */
c3bb7fad 334struct klist_node *klist_next(struct klist_iter *i)
9a19fea4 335{
7e9f4b2d 336 void (*put)(struct klist_node *) = i->i_klist->put;
a1ed5b0c
TH
337 struct klist_node *last = i->i_cur;
338 struct klist_node *next;
9a19fea4
PM
339
340 spin_lock(&i->i_klist->k_lock);
a1ed5b0c
TH
341
342 if (last) {
343 next = to_klist_node(last->n_node.next);
344 if (!klist_dec_and_del(last))
7e9f4b2d 345 put = NULL;
9a19fea4 346 } else
a1ed5b0c 347 next = to_klist_node(i->i_klist->k_list.next);
9a19fea4 348
a1ed5b0c
TH
349 i->i_cur = NULL;
350 while (next != to_klist_node(&i->i_klist->k_list)) {
351 if (likely(!knode_dead(next))) {
352 kref_get(&next->n_ref);
353 i->i_cur = next;
354 break;
355 }
356 next = to_klist_node(next->n_node.next);
9a19fea4 357 }
a1ed5b0c 358
9a19fea4 359 spin_unlock(&i->i_klist->k_lock);
a1ed5b0c
TH
360
361 if (put && last)
362 put(last);
363 return i->i_cur;
9a19fea4 364}
9a19fea4 365EXPORT_SYMBOL_GPL(klist_next);