power.c revision d9de16eb13b5a295b555bc29ec58c517139f736e
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#include <errno.h>
17#include <string.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <sys/socket.h>
21#include <sys/un.h>
22#include <fcntl.h>
23#include <dlfcn.h>
24
25#define LOG_TAG "PowerHAL"
26#include <utils/Log.h>
27
28#include <hardware/hardware.h>
29#include <hardware/power.h>
30#define BOOST_SOCKET       "/dev/socket/mpdecision/boost"
31
32static int client_sockfd;
33static struct sockaddr_un client_addr;
34
35static void power_init(struct power_module *module)
36{
37    ALOGI("%s", __func__);
38    client_sockfd = socket(PF_UNIX, SOCK_DGRAM, 0);
39    if (client_sockfd < 0) {
40        ALOGE("%s: failed to open: %s", __func__, strerror(errno));
41        return;
42    }
43    memset(&client_addr, 0, sizeof(struct sockaddr_un));
44    client_addr.sun_family = AF_UNIX;
45    snprintf(client_addr.sun_path, UNIX_PATH_MAX, BOOST_SOCKET);
46}
47
48static void touch_boost()
49{
50    int rc;
51
52    if (client_sockfd < 0) {
53        ALOGE("%s: touchboost socket not created", __func__);
54        return;
55    }
56
57    rc = sendto(client_sockfd, "1", 1, 0, (const struct sockaddr *)&client_addr, sizeof(struct sockaddr_un));
58    if (rc < 0) {
59        ALOGE("%s: failed to send: %s", __func__, strerror(errno));
60    }
61}
62
63static void core_boost(int on)
64{
65    int rc;
66
67    if (client_sockfd < 0) {
68        ALOGE("%s: touchboost socket not created", __func__);
69        return;
70    }
71
72    if (!on) {
73        rc = sendto(client_sockfd, "2", 1, 0, (const struct sockaddr *)&client_addr, sizeof(struct sockaddr_un));
74    } else {
75        rc = sendto(client_sockfd, "3", 1, 0, (const struct sockaddr *)&client_addr, sizeof(struct sockaddr_un));
76    }
77
78    if (rc < 0) {
79        ALOGE("%s: failed to send: %s", __func__, strerror(errno));
80    }
81}
82
83static void power_set_interactive(struct power_module *module, int on)
84{
85    ALOGV("%s %s", __func__, (on ? "ON" : "OFF"));
86    if (on) {
87        core_boost(1);
88        touch_boost();
89    } else {
90        core_boost(0);
91    }
92}
93
94static void power_hint(struct power_module *module, power_hint_t hint,
95                       void *data) {
96    switch (hint) {
97        case POWER_HINT_INTERACTION:
98            ALOGV("POWER_HINT_INTERACTION");
99            touch_boost();
100            break;
101#if 0
102        case POWER_HINT_VSYNC:
103            ALOGV("POWER_HINT_VSYNC %s", (data ? "ON" : "OFF"));
104            break;
105#endif
106        default:
107             break;
108    }
109}
110
111static struct hw_module_methods_t power_module_methods = {
112    .open = NULL,
113};
114
115struct power_module HAL_MODULE_INFO_SYM = {
116    .common = {
117        .tag = HARDWARE_MODULE_TAG,
118        .module_api_version = POWER_MODULE_API_VERSION_0_2,
119        .hal_api_version = HARDWARE_HAL_API_VERSION,
120        .id = POWER_HARDWARE_MODULE_ID,
121        .name = "Qualcomm Power HAL",
122        .author = "The Android Open Source Project",
123        .methods = &power_module_methods,
124    },
125
126    .init = power_init,
127    .setInteractive = power_set_interactive,
128    .powerHint = power_hint,
129};
130