2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) Samsung Electronics, 2016
18 * 2015 Vyacheslav Cherkashin <v.cherkashin@samsung.com>
23 #include <linux/slab.h>
24 #include <linux/atomic.h>
25 #include <kprobe/swap_kprobes_deps.h>
28 static atomic_t wsp_res_count;
30 static int wsp_tdata_get_id(void)
32 return atomic_inc_return(&wsp_res_count);
35 static struct wsp_tdata *g_tdata;
37 /* FIXME: get_tdata() need receive for each processes */
38 static struct wsp_tdata *get_tdata(void)
47 /* ============================================================================
49 * ============================================================================
52 struct list_head res_list;
58 static struct wsp_res *wsp_tdata_new_res(struct wsp_tdata *d, void *ptr,
63 res = kmalloc(sizeof(*res), GFP_ATOMIC);
65 INIT_LIST_HEAD(&res->list);
67 res->id = wsp_tdata_get_id();
73 list_add(&res->list, &d->res_list);
74 spin_unlock(&d->lock);
80 static void wsp_tdata_del_res(struct wsp_res *res)
86 static struct wsp_tdata *wsp_tdata_create(void)
90 d = kmalloc(sizeof(*d), GFP_ATOMIC);
92 INIT_LIST_HEAD(&d->res_list);
93 spin_lock_init(&d->lock);
100 static void wsp_tdata_destroy(struct wsp_tdata *d)
102 struct wsp_res *res, *n;
105 list_for_each_entry_safe(res, n, &d->res_list, list)
106 wsp_tdata_del_res(res);
107 spin_unlock(&d->lock);
112 static struct wsp_res *wsp_tdata_last_res(struct wsp_tdata *d)
117 res = list_first_entry_or_null(&d->res_list, struct wsp_res, list);
118 spin_unlock(&d->lock);
123 struct wsp_res *wsp_tdata_find_res(struct wsp_tdata *data, void *ptr,
128 list_for_each_entry(res, &data->res_list, list) {
129 if (res->type != type)
132 if (res->ptr == ptr) {
133 if (res->stat == WRS_ERR) {
134 pr_err("ERR: something went wrong\n");
149 /* ============================================================================
150 * = wsp_current_[get/set]_stat() =
151 * ============================================================================
153 enum tdata_stat wsp_current_get_stat(void)
161 pr_err("ERR: no current tdata\n");
166 void wsp_current_set_stat(enum tdata_stat stat)
174 pr_err("ERR: no current tdata\n");
181 /* ============================================================================
183 * ============================================================================
185 struct wsp_res *wsp_res_new(void *ptr, enum wsp_res_t type)
187 struct wsp_tdata *data;
191 pr_err("ERR: no current tdata\n");
196 return wsp_tdata_new_res(data, ptr, type);
199 void wsp_res_del(struct wsp_res *res)
201 wsp_tdata_del_res(res);
204 struct wsp_res *wsp_res_find(void *ptr, enum wsp_res_t type)
206 struct wsp_tdata *data;
210 pr_err("ERR: no current tdata\n");
214 return wsp_tdata_find_res(data, ptr, type);
217 struct wsp_res *wsp_res_last(void)
223 pr_err("ERR: no current tdata\n");
227 return wsp_tdata_last_res(d);
230 int wsp_res_stat_set_next(struct wsp_res *res, enum wsp_res_stat stat)
234 if (stat == WRS_WILL_REQ) {
241 if (stat == WRS_SOUP_REQ) {
249 if (stat == WRS_ADD_DATA || stat == WRS_FINISH) {
259 pr_err("ERR: set_next_stat from %d to %d [id=%d]\n",
260 res->stat, stat, res->id);
271 /* ============================================================================
273 * ============================================================================
275 int wsp_res_init(void)
277 g_tdata = wsp_tdata_create();
281 atomic_set(&wsp_res_count, 0);
286 void wsp_res_exit(void)
288 wsp_tdata_destroy(g_tdata);