24961102e28f060e93bef75a757185283bf8ac5a
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / kernel / swap / writer / event_filter.c
1 /**
2  * writer/event_filter.c
3  * @author Vyacheslav Cherkashin <v.cherkashin@samsung.com>
4  *
5  * @section LICENSE
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * @section COPYRIGHT
22  *
23  * Copyright (C) Samsung Electronics, 2013
24  *
25  * @section DESCRIPTION
26  *
27  * Events filter.
28  */
29
30
31 #include <linux/module.h>
32 #include <linux/list.h>
33 #include "event_filter.h"
34
35
36 static LIST_HEAD(filter_list);
37
38 static int func_none(struct task_struct *task)
39 {
40         return 1;
41 }
42
43 static struct ev_filter filter_none = {
44         .name = "all",
45         .filter = func_none
46 };
47
48 static struct ev_filter *filter_current = &filter_none;
49
50 int check_event(struct task_struct *task)
51 {
52         return filter_current->filter(task);
53 }
54 EXPORT_SYMBOL_GPL(check_event);
55
56 static struct ev_filter *event_filter_find(const char *name)
57 {
58         struct ev_filter *f, *tmp;
59
60         list_for_each_entry_safe(f, tmp, &filter_list, list) {
61                 if (strcmp(f->name, name) == 0)
62                         return f;
63         }
64
65         return NULL;
66 }
67
68 /**
69  * @brief Registers event filter.
70  *
71  * @param f Pointer to the event filter.
72  * @return 0 on success, error code on error.
73  */
74 int event_filter_register(struct ev_filter *f)
75 {
76         if (event_filter_find(f->name))
77                 return -EINVAL;
78
79         INIT_LIST_HEAD(&f->list);
80         list_add(&f->list, &filter_list);
81
82         return 0;
83 }
84 EXPORT_SYMBOL_GPL(event_filter_register);
85
86 /**
87  * @brief Unregisters event filter.
88  *
89  * @param f Pointer to the event filter.
90  * @return Void.
91  */
92 void event_filter_unregister(struct ev_filter *f)
93 {
94         struct ev_filter *filter, *tmp;
95
96         if (filter_current == f)
97                 filter_current = &filter_none;
98
99         list_for_each_entry_safe(filter, tmp, &filter_list, list) {
100                 if (filter == f) {
101                         list_del(&filter->list);
102                         break;
103                 }
104         }
105 }
106 EXPORT_SYMBOL_GPL(event_filter_unregister);
107
108 /**
109  * @brief Sets event filter by its name.
110  *
111  * @param name Filter name.
112  * @return 0 on success, error code on error.
113  */
114 int event_filter_set(const char *name)
115 {
116         struct ev_filter *f;
117
118         f = event_filter_find(name);
119         if (f == NULL)
120                 return -EINVAL;
121
122         filter_current = f;
123
124         return 0;
125 }
126 EXPORT_SYMBOL_GPL(event_filter_set);
127
128 /**
129  * @brief Gets filter name.
130  *
131  * @return Pointer to the filter name string.
132  */
133 const char *event_filter_get(void)
134 {
135         return filter_current->name;
136 }
137
138 /**
139  * @brief Runs specified callback for each filter in list.
140  *
141  * @param func Specified callback.
142  * @param data Pointer to the data passed to the callback.
143  * @return Void.
144  */
145 void event_filter_on_each(void (*func)(struct ev_filter *, void *),
146                           void *data)
147 {
148         struct ev_filter *f, *tmp;
149
150         list_for_each_entry_safe(f, tmp, &filter_list, list)
151                 func(f, data);
152 }
153
154 /**
155  * @brief Initializes event filter.
156  *
157  * @return Initialization result.
158  */
159 int event_filter_init(void)
160 {
161         return event_filter_register(&filter_none);
162 }
163
164 /**
165  * @brief Uninitializes event filter.
166  *
167  * @return Void.
168  */
169 void event_filter_exit(void)
170 {
171         event_filter_unregister(&filter_none);
172 }