| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2012 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 17 | #define LOG_TAG "libsuspend" | 
|  | 18 |  | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 19 | #include <stdbool.h> | 
|  | 20 |  | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 21 | #include <log/log.h> | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <suspend/autosuspend.h> | 
|  | 24 |  | 
|  | 25 | #include "autosuspend_ops.h" | 
|  | 26 |  | 
|  | 27 | static struct autosuspend_ops *autosuspend_ops; | 
|  | 28 | static bool autosuspend_enabled; | 
|  | 29 | static bool autosuspend_inited; | 
|  | 30 |  | 
|  | 31 | static int autosuspend_init(void) | 
|  | 32 | { | 
|  | 33 | if (autosuspend_inited) { | 
|  | 34 | return 0; | 
|  | 35 | } | 
|  | 36 |  | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 37 | autosuspend_ops = autosuspend_earlysuspend_init(); | 
|  | 38 | if (autosuspend_ops) { | 
|  | 39 | goto out; | 
|  | 40 | } | 
|  | 41 |  | 
| Todd Poynor | 6f26891 | 2014-03-04 13:11:21 -0800 | [diff] [blame] | 42 | /* Remove autosleep so userspace can manager suspend/resume and keep stats */ | 
|  | 43 | #if 0 | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 44 | autosuspend_ops = autosuspend_autosleep_init(); | 
|  | 45 | if (autosuspend_ops) { | 
|  | 46 | goto out; | 
|  | 47 | } | 
| Todd Poynor | 6f26891 | 2014-03-04 13:11:21 -0800 | [diff] [blame] | 48 | #endif | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 49 |  | 
|  | 50 | autosuspend_ops = autosuspend_wakeup_count_init(); | 
|  | 51 | if (autosuspend_ops) { | 
|  | 52 | goto out; | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | if (!autosuspend_ops) { | 
|  | 56 | ALOGE("failed to initialize autosuspend\n"); | 
|  | 57 | return -1; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | out: | 
| Kyle Russell | a26b4ca | 2012-11-19 16:29:58 -0500 | [diff] [blame] | 61 | autosuspend_inited = true; | 
|  | 62 |  | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 63 | ALOGV("autosuspend initialized\n"); | 
|  | 64 | return 0; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | int autosuspend_enable(void) | 
|  | 68 | { | 
|  | 69 | int ret; | 
|  | 70 |  | 
|  | 71 | ret = autosuspend_init(); | 
|  | 72 | if (ret) { | 
|  | 73 | return ret; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | ALOGV("autosuspend_enable\n"); | 
|  | 77 |  | 
|  | 78 | if (autosuspend_enabled) { | 
|  | 79 | return 0; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | ret = autosuspend_ops->enable(); | 
|  | 83 | if (ret) { | 
|  | 84 | return ret; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | autosuspend_enabled = true; | 
|  | 88 | return 0; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | int autosuspend_disable(void) | 
|  | 92 | { | 
|  | 93 | int ret; | 
|  | 94 |  | 
|  | 95 | ret = autosuspend_init(); | 
|  | 96 | if (ret) { | 
|  | 97 | return ret; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | ALOGV("autosuspend_disable\n"); | 
|  | 101 |  | 
|  | 102 | if (!autosuspend_enabled) { | 
|  | 103 | return 0; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | ret = autosuspend_ops->disable(); | 
|  | 107 | if (ret) { | 
|  | 108 | return ret; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | autosuspend_enabled = false; | 
|  | 112 | return 0; | 
|  | 113 | } |