Venkat Chinta | 686c9e5 | 2018-01-20 14:33:25 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 2 | * |
| 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 Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 14 | #include "cam_debug_util.h" |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 15 | |
| 16 | void crm_timer_reset(struct cam_req_mgr_timer *crm_timer) |
| 17 | { |
| 18 | if (!crm_timer) |
| 19 | return; |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 20 | CAM_DBG(CAM_CRM, "Starting timer to fire in %d ms. (jiffies=%lu)\n", |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 21 | crm_timer->expires, jiffies); |
| 22 | mod_timer(&crm_timer->sys_timer, |
| 23 | (jiffies + msecs_to_jiffies(crm_timer->expires))); |
| 24 | } |
| 25 | |
| 26 | void 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 Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 31 | CAM_ERR(CAM_CRM, "NULL timer"); |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 32 | return; |
| 33 | } |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 34 | CAM_DBG(CAM_CRM, "timer %pK parent %pK", timer, timer->parent); |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 35 | crm_timer_reset(timer); |
| 36 | } |
| 37 | |
| 38 | void crm_timer_modify(struct cam_req_mgr_timer *crm_timer, |
| 39 | int32_t expires) |
| 40 | { |
Jigarkumar Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 41 | CAM_DBG(CAM_CRM, "new time %d", expires); |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 42 | if (crm_timer) { |
| 43 | crm_timer->expires = expires; |
| 44 | crm_timer_reset(crm_timer); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | int 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 Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 54 | CAM_DBG(CAM_CRM, "init timer %d %pK", expires, *timer); |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 55 | if (*timer == NULL) { |
Venkat Chinta | 686c9e5 | 2018-01-20 14:33:25 -0800 | [diff] [blame^] | 56 | 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 Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 68 | 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 Zala | 36ad717 | 2017-07-18 19:52:14 -0700 | [diff] [blame] | 84 | CAM_WARN(CAM_CRM, "Timer already exists!!"); |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 85 | ret = -EINVAL; |
| 86 | } |
| 87 | end: |
| 88 | return ret; |
| 89 | } |
| 90 | void crm_timer_exit(struct cam_req_mgr_timer **crm_timer) |
| 91 | { |
Venkat Chinta | 686c9e5 | 2018-01-20 14:33:25 -0800 | [diff] [blame^] | 92 | CAM_DBG(CAM_CRM, "destroy timer %pK @ %pK", *crm_timer, crm_timer); |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 93 | if (*crm_timer) { |
Lynus Vaz | 4622d1a | 2017-12-01 20:12:32 +0530 | [diff] [blame] | 94 | del_timer_sync(&(*crm_timer)->sys_timer); |
Venkat Chinta | 686c9e5 | 2018-01-20 14:33:25 -0800 | [diff] [blame^] | 95 | if (g_cam_req_mgr_timer_cachep) |
| 96 | kmem_cache_free(g_cam_req_mgr_timer_cachep, *crm_timer); |
Sagar Gore | d79f95e | 2017-03-14 18:32:17 -0700 | [diff] [blame] | 97 | *crm_timer = NULL; |
| 98 | } |
| 99 | } |
| 100 | |