1/* Copyright (c) 2012-2014, 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 IS_STR_END(c) ((c) == '\0' || (c) == '\n' || (c) == '\r')
57#define LENGTH(s) (sizeof(s) - 1)
58#define GPS_CHECK_NO_ERROR 0
59#define GPS_CHECK_NO_GPS_HW 1
60/* When system server is started, it uses 20 seconds as ActivityManager
61 * timeout. After that it sends SIGSTOP signal to process.
62 */
63#define QCA1530_DETECT_TIMEOUT 15
64#define QCA1530_DETECT_PRESENT "yes"
65#define QCA1530_DETECT_PROGRESS "detect"
66
67static unsigned int gTarget = (unsigned int)-1;
68
69static int read_a_line(const char * file_path, char * line, int line_size)
70{
71    FILE *fp;
72    int result = 0;
73
74    * line = '\0';
75    fp = fopen(file_path, "r" );
76    if( fp == NULL ) {
77        LOC_LOGE("open failed: %s: %s\n", file_path, strerror(errno));
78        result = -1;
79    } else {
80        int len;
81        fgets(line, line_size, fp);
82        len = strlen(line);
83        len = len < line_size - 1? len : line_size - 1;
84        line[len] = '\0';
85        LOC_LOGD("cat %s: %s", file_path, line);
86        fclose(fp);
87    }
88    return result;
89}
90
91/*!
92 * \brief Checks if QCA1530 is avalable.
93 *
94 * Function verifies if qca1530 SoC is configured on the device. The test is
95 * based on property value. For 1530 scenario, the value shall be one of the
96 * following: "yes", "no", "detect". All other values are treated equally to
97 * "no". When the value is "detect" the system waits for SoC detection to
98 * finish before returning result.
99 *
100 * \retval true - QCA1530 is available.
101 * \retval false - QCA1530 is not available.
102 */
103static bool is_qca1530(void)
104{
105    static const char qca1530_property_name[] = "sys.qca1530";
106    bool res = false;
107    int ret, i;
108    char buf[PROPERTY_VALUE_MAX];
109
110    memset(buf, 0, sizeof(buf));
111
112    for (i = 0; i < QCA1530_DETECT_TIMEOUT; ++i)
113    {
114        ret = property_get(qca1530_property_name, buf, NULL);
115        if (ret < 0)
116        {
117            LOC_LOGV( "qca1530: property %s is not accessible, ret=%d",
118                  qca1530_property_name,
119                  ret);
120
121            break;
122        }
123
124        LOC_LOGV( "qca1530: property %s is set to %s",
125                  qca1530_property_name,
126                  buf);
127
128        if (!memcmp(buf, QCA1530_DETECT_PRESENT,
129                    sizeof(QCA1530_DETECT_PRESENT)))
130        {
131            res = true;
132            break;
133        }
134        if (!memcmp(buf, QCA1530_DETECT_PROGRESS,
135                    sizeof(QCA1530_DETECT_PROGRESS)))
136        {
137            LOC_LOGV("qca1530: SoC detection is in progress.");
138            sleep(1);
139            continue;
140        }
141        break;
142    }
143
144    LOC_LOGD("qca1530: detected=%s", res ? "true" : "false");
145    return res;
146}
147
148/*The character array passed to this function should have length
149  of atleast PROPERTY_VALUE_MAX*/
150void loc_get_target_baseband(char *baseband, int array_length)
151{
152    if(baseband && (array_length >= PROPERTY_VALUE_MAX)) {
153        property_get("ro.baseband", baseband, "");
154        LOC_LOGD("%s:%d]: Baseband: %s\n", __func__, __LINE__, baseband);
155    }
156    else {
157        LOC_LOGE("%s:%d]: NULL parameter or array length less than PROPERTY_VALUE_MAX\n",
158                 __func__, __LINE__);
159    }
160}
161
162/*The character array passed to this function should have length
163  of atleast PROPERTY_VALUE_MAX*/
164void loc_get_platform_name(char *platform_name, int array_length)
165{
166    if(platform_name && (array_length >= PROPERTY_VALUE_MAX)) {
167        property_get("ro.board.platform", platform_name, "");
168        LOC_LOGD("%s:%d]: Target name: %s\n", __func__, __LINE__, platform_name);
169    }
170    else {
171        LOC_LOGE("%s:%d]: Null parameter or array length less than PROPERTY_VALUE_MAX\n",
172                 __func__, __LINE__);
173    }
174}
175
176unsigned int loc_get_target(void)
177{
178    if (gTarget != (unsigned int)-1)
179        return gTarget;
180
181    static const char hw_platform[]      = "/sys/devices/soc0/hw_platform";
182    static const char id[]               = "/sys/devices/soc0/soc_id";
183    static const char hw_platform_dep[]  =
184        "/sys/devices/system/soc/soc0/hw_platform";
185    static const char id_dep[]           = "/sys/devices/system/soc/soc0/id";
186    static const char mdm[]              = "/dev/mdm"; // No such file or directory
187
188    char rd_hw_platform[LINE_LEN];
189    char rd_id[LINE_LEN];
190    char rd_mdm[LINE_LEN];
191    char baseband[LINE_LEN];
192
193    if (is_qca1530()) {
194        gTarget = TARGET_QCA1530;
195        goto detected;
196    }
197
198    loc_get_target_baseband(baseband, sizeof(baseband));
199
200    if (!access(hw_platform, F_OK)) {
201        read_a_line(hw_platform, rd_hw_platform, LINE_LEN);
202    } else {
203        read_a_line(hw_platform_dep, rd_hw_platform, LINE_LEN);
204    }
205    if (!access(id, F_OK)) {
206        read_a_line(id, rd_id, LINE_LEN);
207    } else {
208        read_a_line(id_dep, rd_id, LINE_LEN);
209    }
210
211    if( !memcmp(baseband, STR_APQ, LENGTH(STR_APQ)) ){
212        if( !memcmp(rd_id, MPQ8064_ID_1, LENGTH(MPQ8064_ID_1))
213            && IS_STR_END(rd_id[LENGTH(MPQ8064_ID_1)]) )
214            gTarget = TARGET_MPQ;
215        else
216            gTarget = TARGET_APQ_SA;
217    }
218    else {
219        if( (!memcmp(rd_hw_platform, STR_LIQUID, LENGTH(STR_LIQUID))
220             && IS_STR_END(rd_hw_platform[LENGTH(STR_LIQUID)])) ||
221            (!memcmp(rd_hw_platform, STR_SURF,   LENGTH(STR_SURF))
222             && IS_STR_END(rd_hw_platform[LENGTH(STR_SURF)])) ||
223            (!memcmp(rd_hw_platform, STR_MTP,   LENGTH(STR_MTP))
224             && IS_STR_END(rd_hw_platform[LENGTH(STR_MTP)]))) {
225
226            if (!read_a_line( mdm, rd_mdm, LINE_LEN))
227                gTarget = TARGET_MDM;
228        }
229        else if( (!memcmp(rd_id, MSM8930_ID_1, LENGTH(MSM8930_ID_1))
230                   && IS_STR_END(rd_id[LENGTH(MSM8930_ID_1)])) ||
231                  (!memcmp(rd_id, MSM8930_ID_2, LENGTH(MSM8930_ID_2))
232                   && IS_STR_END(rd_id[LENGTH(MSM8930_ID_2)])) )
233             gTarget = TARGET_MSM_NO_SSC;
234        else
235             gTarget = TARGET_UNKNOWN;
236    }
237
238detected:
239    LOC_LOGD("HAL: %s returned %d", __FUNCTION__, gTarget);
240    return gTarget;
241}
242