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