1/* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <errno.h>
37#include <hardware/gps.h>
38#include <cutils/properties.h>
39#include "loc_target.h"
40#include "loc_log.h"
41#include "log_util.h"
42
43#define APQ8064_ID_1 "109"
44#define APQ8064_ID_2 "153"
45#define MPQ8064_ID_1 "130"
46#define MSM8930_ID_1 "142"
47#define MSM8930_ID_2 "116"
48#define APQ8030_ID_1 "157"
49#define APQ8074_ID_1 "184"
50
51#define LINE_LEN 100
52#define STR_LIQUID    "Liquid"
53#define STR_SURF      "Surf"
54#define STR_MTP       "MTP"
55#define STR_APQ       "apq"
56#define STR_AUTO      "auto"
57#define IS_STR_END(c) ((c) == '\0' || (c) == '\n' || (c) == '\r')
58#define LENGTH(s) (sizeof(s) - 1)
59#define GPS_CHECK_NO_ERROR 0
60#define GPS_CHECK_NO_GPS_HW 1
61/* When system server is started, it uses 20 seconds as ActivityManager
62 * timeout. After that it sends SIGSTOP signal to process.
63 */
64#define QCA1530_DETECT_TIMEOUT 15
65#define QCA1530_DETECT_PRESENT "yes"
66#define QCA1530_DETECT_PROGRESS "detect"
67
68static unsigned int gTarget = (unsigned int)-1;
69
70static int read_a_line(const char * file_path, char * line, int line_size)
71{
72    FILE *fp;
73    int result = 0;
74
75    * line = '\0';
76    fp = fopen(file_path, "r" );
77    if( fp == NULL ) {
78        LOC_LOGE("open failed: %s: %s\n", file_path, strerror(errno));
79        result = -1;
80    } else {
81        int len;
82        fgets(line, line_size, fp);
83        len = strlen(line);
84        len = len < line_size - 1? len : line_size - 1;
85        line[len] = '\0';
86        LOC_LOGD("cat %s: %s", file_path, line);
87        fclose(fp);
88    }
89    return result;
90}
91
92/*!
93 * \brief Checks if QCA1530 is avalable.
94 *
95 * Function verifies if qca1530 SoC is configured on the device. The test is
96 * based on property value. For 1530 scenario, the value shall be one of the
97 * following: "yes", "no", "detect". All other values are treated equally to
98 * "no". When the value is "detect" the system waits for SoC detection to
99 * finish before returning result.
100 *
101 * \retval true - QCA1530 is available.
102 * \retval false - QCA1530 is not available.
103 */
104static bool is_qca1530(void)
105{
106    static const char qca1530_property_name[] = "sys.qca1530";
107    bool res = false;
108    int ret, i;
109    char buf[PROPERTY_VALUE_MAX];
110
111    memset(buf, 0, sizeof(buf));
112
113    for (i = 0; i < QCA1530_DETECT_TIMEOUT; ++i)
114    {
115        ret = property_get(qca1530_property_name, buf, NULL);
116        if (ret < 0)
117        {
118            LOC_LOGV( "qca1530: property %s is not accessible, ret=%d",
119                  qca1530_property_name,
120                  ret);
121
122            break;
123        }
124
125        LOC_LOGV( "qca1530: property %s is set to %s",
126                  qca1530_property_name,
127                  buf);
128
129        if (!memcmp(buf, QCA1530_DETECT_PRESENT,
130                    sizeof(QCA1530_DETECT_PRESENT)))
131        {
132            res = true;
133            break;
134        }
135        if (!memcmp(buf, QCA1530_DETECT_PROGRESS,
136                    sizeof(QCA1530_DETECT_PROGRESS)))
137        {
138            LOC_LOGV("qca1530: SoC detection is in progress.");
139            sleep(1);
140            continue;
141        }
142        break;
143    }
144
145    LOC_LOGD("qca1530: detected=%s", res ? "true" : "false");
146    return res;
147}
148
149/*The character array passed to this function should have length
150  of atleast PROPERTY_VALUE_MAX*/
151void loc_get_target_baseband(char *baseband, int array_length)
152{
153    if(baseband && (array_length >= PROPERTY_VALUE_MAX)) {
154        property_get("ro.baseband", baseband, "");
155        LOC_LOGD("%s:%d]: Baseband: %s\n", __func__, __LINE__, baseband);
156    }
157    else {
158        LOC_LOGE("%s:%d]: NULL parameter or array length less than PROPERTY_VALUE_MAX\n",
159                 __func__, __LINE__);
160    }
161}
162
163/*The character array passed to this function should have length
164  of atleast PROPERTY_VALUE_MAX*/
165void loc_get_platform_name(char *platform_name, int array_length)
166{
167    if(platform_name && (array_length >= PROPERTY_VALUE_MAX)) {
168        property_get("ro.board.platform", platform_name, "");
169        LOC_LOGD("%s:%d]: Target name: %s\n", __func__, __LINE__, platform_name);
170    }
171    else {
172        LOC_LOGE("%s:%d]: Null parameter or array length less than PROPERTY_VALUE_MAX\n",
173                 __func__, __LINE__);
174    }
175}
176
177unsigned int loc_get_target(void)
178{
179    if (gTarget != (unsigned int)-1)
180        return gTarget;
181
182    static const char hw_platform[]      = "/sys/devices/soc0/hw_platform";
183    static const char id[]               = "/sys/devices/soc0/soc_id";
184    static const char hw_platform_dep[]  =
185        "/sys/devices/system/soc/soc0/hw_platform";
186    static const char id_dep[]           = "/sys/devices/system/soc/soc0/id";
187    static const char mdm[]              = "/dev/mdm"; // No such file or directory
188
189    char rd_hw_platform[LINE_LEN];
190    char rd_id[LINE_LEN];
191    char rd_mdm[LINE_LEN];
192    char baseband[LINE_LEN];
193
194    if (is_qca1530()) {
195        gTarget = TARGET_QCA1530;
196        goto detected;
197    }
198
199    loc_get_target_baseband(baseband, sizeof(baseband));
200
201    if (!access(hw_platform, F_OK)) {
202        read_a_line(hw_platform, rd_hw_platform, LINE_LEN);
203    } else {
204        read_a_line(hw_platform_dep, rd_hw_platform, LINE_LEN);
205    }
206    if (!access(id, F_OK)) {
207        read_a_line(id, rd_id, LINE_LEN);
208    } else {
209        read_a_line(id_dep, rd_id, LINE_LEN);
210    }
211    if( !memcmp(baseband, STR_AUTO, LENGTH(STR_AUTO)) )
212    {
213          gTarget = TARGET_AUTO;
214          goto detected;
215    }
216    if( !memcmp(baseband, STR_APQ, LENGTH(STR_APQ)) ){
217
218        if( !memcmp(rd_id, MPQ8064_ID_1, LENGTH(MPQ8064_ID_1))
219            && IS_STR_END(rd_id[LENGTH(MPQ8064_ID_1)]) )
220            gTarget = TARGET_MPQ;
221        else
222            gTarget = TARGET_APQ_SA;
223    }
224    else {
225        if( (!memcmp(rd_hw_platform, STR_LIQUID, LENGTH(STR_LIQUID))
226             && IS_STR_END(rd_hw_platform[LENGTH(STR_LIQUID)])) ||
227            (!memcmp(rd_hw_platform, STR_SURF,   LENGTH(STR_SURF))
228             && IS_STR_END(rd_hw_platform[LENGTH(STR_SURF)])) ||
229            (!memcmp(rd_hw_platform, STR_MTP,   LENGTH(STR_MTP))
230             && IS_STR_END(rd_hw_platform[LENGTH(STR_MTP)]))) {
231
232            if (!read_a_line( mdm, rd_mdm, LINE_LEN))
233                gTarget = TARGET_MDM;
234        }
235        else if( (!memcmp(rd_id, MSM8930_ID_1, LENGTH(MSM8930_ID_1))
236                   && IS_STR_END(rd_id[LENGTH(MSM8930_ID_1)])) ||
237                  (!memcmp(rd_id, MSM8930_ID_2, LENGTH(MSM8930_ID_2))
238                   && IS_STR_END(rd_id[LENGTH(MSM8930_ID_2)])) )
239             gTarget = TARGET_MSM_NO_SSC;
240        else
241             gTarget = TARGET_UNKNOWN;
242    }
243
244detected:
245    LOC_LOGD("HAL: %s returned %d", __FUNCTION__, gTarget);
246    return gTarget;
247}
248
249/*Reads the property ro.lean to identify if this is a lean target
250  Returns:
251  0 if not a lean and mean target
252  1 if this is a lean and mean target
253*/
254int loc_identify_lean_target()
255{
256    int ret = 0;
257    char lean_target[PROPERTY_VALUE_MAX];
258    property_get("ro.lean", lean_target, "");
259    LOC_LOGD("%s:%d]: lean target: %s\n", __func__, __LINE__, lean_target);
260    return !(strncmp(lean_target, "true", PROPERTY_VALUE_MAX));
261}
262