blob: 124b336dd26507fa4b29e17a3711f51ab3fd6b9c [file] [log] [blame]
Venkat Chinta686c9e52018-01-20 14:33:25 -08001/* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
Sagar Gored79f95e2017-03-14 18:32:17 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
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.
11 */
12
13#include "cam_req_mgr_timer.h"
Jigarkumar Zala36ad7172017-07-18 19:52:14 -070014#include "cam_debug_util.h"
Sagar Gored79f95e2017-03-14 18:32:17 -070015
16void crm_timer_reset(struct cam_req_mgr_timer *crm_timer)
17{
18 if (!crm_timer)
19 return;
Jigarkumar Zala36ad7172017-07-18 19:52:14 -070020 CAM_DBG(CAM_CRM, "Starting timer to fire in %d ms. (jiffies=%lu)\n",
Sagar Gored79f95e2017-03-14 18:32:17 -070021 crm_timer->expires, jiffies);
22 mod_timer(&crm_timer->sys_timer,
23 (jiffies + msecs_to_jiffies(crm_timer->expires)));
24}
25
26void crm_timer_callback(unsigned long data)
27{
28 struct cam_req_mgr_timer *timer = (struct cam_req_mgr_timer *)data;
29
30 if (!timer) {
Jigarkumar Zala36ad7172017-07-18 19:52:14 -070031 CAM_ERR(CAM_CRM, "NULL timer");
Sagar Gored79f95e2017-03-14 18:32:17 -070032 return;
33 }
Jigarkumar Zala36ad7172017-07-18 19:52:14 -070034 CAM_DBG(CAM_CRM, "timer %pK parent %pK", timer, timer->parent);
Sagar Gored79f95e2017-03-14 18:32:17 -070035 crm_timer_reset(timer);
36}
37
38void crm_timer_modify(struct cam_req_mgr_timer *crm_timer,
39 int32_t expires)
40{
Jigarkumar Zala36ad7172017-07-18 19:52:14 -070041 CAM_DBG(CAM_CRM, "new time %d", expires);
Sagar Gored79f95e2017-03-14 18:32:17 -070042 if (crm_timer) {
43 crm_timer->expires = expires;
44 crm_timer_reset(crm_timer);
45 }
46}
47
48int crm_timer_init(struct cam_req_mgr_timer **timer,
49 int32_t expires, void *parent, void (*timer_cb)(unsigned long))
50{
51 int ret = 0;
52 struct cam_req_mgr_timer *crm_timer = NULL;
53
Jigarkumar Zala36ad7172017-07-18 19:52:14 -070054 CAM_DBG(CAM_CRM, "init timer %d %pK", expires, *timer);
Sagar Gored79f95e2017-03-14 18:32:17 -070055 if (*timer == NULL) {
Venkat Chinta686c9e52018-01-20 14:33:25 -080056 if (g_cam_req_mgr_timer_cachep) {
57 crm_timer = (struct cam_req_mgr_timer *)
58 kmem_cache_alloc(
59 g_cam_req_mgr_timer_cachep,
60 __GFP_ZERO | GFP_KERNEL);
61 if (!crm_timer) {
62 ret = -ENOMEM;
63 goto end;
64 }
65 }
66
67 else {
Sagar Gored79f95e2017-03-14 18:32:17 -070068 ret = -ENOMEM;
69 goto end;
70 }
71
72 if (timer_cb != NULL)
73 crm_timer->timer_cb = timer_cb;
74 else
75 crm_timer->timer_cb = crm_timer_callback;
76
77 crm_timer->expires = expires;
78 crm_timer->parent = parent;
79 setup_timer(&crm_timer->sys_timer,
80 crm_timer->timer_cb, (unsigned long)crm_timer);
81 crm_timer_reset(crm_timer);
82 *timer = crm_timer;
83 } else {
Jigarkumar Zala36ad7172017-07-18 19:52:14 -070084 CAM_WARN(CAM_CRM, "Timer already exists!!");
Sagar Gored79f95e2017-03-14 18:32:17 -070085 ret = -EINVAL;
86 }
87end:
88 return ret;
89}
90void crm_timer_exit(struct cam_req_mgr_timer **crm_timer)
91{
Venkat Chinta686c9e52018-01-20 14:33:25 -080092 CAM_DBG(CAM_CRM, "destroy timer %pK @ %pK", *crm_timer, crm_timer);
Sagar Gored79f95e2017-03-14 18:32:17 -070093 if (*crm_timer) {
Lynus Vaz4622d1a2017-12-01 20:12:32 +053094 del_timer_sync(&(*crm_timer)->sys_timer);
Venkat Chinta686c9e52018-01-20 14:33:25 -080095 if (g_cam_req_mgr_timer_cachep)
96 kmem_cache_free(g_cam_req_mgr_timer_cachep, *crm_timer);
Sagar Gored79f95e2017-03-14 18:32:17 -070097 *crm_timer = NULL;
98 }
99}
100