1 /*
2  * Copyright 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 
17 /******************************************************************************
18  *
19  *  Filename:      hardware.c
20  *
21  *  Description:   Contains controller-specific functions, like
22  *                      firmware patch download
23  *                      low power mode operations
24  *
25  ******************************************************************************/
26 
27 #define LOG_TAG "bt_vendor"
28 
29 #include "bt_hci_bdroid.h"
30 #include "bt_vendor_qcom.h"
31 #include <ctype.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <time.h>
41 #include <unistd.h>
42 
43 #include <cutils/properties.h>
44 #include <utils/Log.h>
45 #define MAX_CNT_RETRY 100
46 
hw_config(int nState)47 int hw_config(int nState)
48 {
49     char *szState[] = {"true", "false"};
50     char *szReqSt = NULL;
51     char szBtSocStatus[PROPERTY_VALUE_MAX] = {'\0', };
52 
53     if(nState == BT_VND_PWR_OFF)
54         szReqSt = szState[1];
55     else
56         szReqSt = szState[0];
57 
58     if((property_get("bluetooth.status", szBtSocStatus, "") <= 0))
59     {
60        if(nState == BT_VND_PWR_ON ) {
61           ALOGW("Hw_config: First Time BT on after boot.Starting hciattach daemon BTStatus=%s",szBtSocStatus);
62           if (property_set("bluetooth.hciattach", szReqSt) < 0)
63           {
64               ALOGE("Hw_config: Property Setting fail");
65               return -1;
66           }
67        }
68     } else if( !(strncmp(szBtSocStatus, "on", strlen("on")))) {
69           //BTSOC is already on
70           ALOGW("Hw_config: nState = %d", nState);
71     } else {
72           ALOGW("Hw_config: trigerring hciattach");
73           if (property_set("bluetooth.hciattach", szReqSt) < 0)
74           {
75               ALOGE("Hw_config: Property Setting fail");
76               return -1;
77           }
78     }
79 
80     return 0;
81 }
82 
readTrpState()83 int readTrpState()
84 {
85     char szBtStatus[PROPERTY_VALUE_MAX] = {0, };
86     if(property_get("bluetooth.status", szBtStatus, "") < 0){
87         ALOGE("Fail to get bluetooth status");
88         return FALSE;
89     }
90 
91     if(!strncmp(szBtStatus, "on", strlen("on"))){
92         ALOGI("bluetooth status is on");
93         return TRUE;
94     }
95     return FALSE;
96 }
97 
is_hw_ready()98 int is_hw_ready()
99 {
100     int i=0;
101     char szStatus[10] = {0,};
102 
103     for(i=MAX_CNT_RETRY; i>0; i--){
104        //TODO :: checking routine
105        if(readTrpState()==TRUE){
106            break;
107        }
108        usleep(50*1000);
109     }
110     return (i==0)? FALSE:TRUE;
111 }
112 
113 #if (HW_NEED_END_WITH_HCI_RESET == TRUE)
114 /*******************************************************************************
115 **
116 ** Function         hw_epilog_cback
117 **
118 ** Description      Callback function for Command Complete Events from HCI
119 **                  commands sent in epilog process.
120 **
121 ** Returns          None
122 **
123 *******************************************************************************/
hw_epilog_cback(void * p_mem)124 void hw_epilog_cback(void *p_mem)
125 {
126     HC_BT_HDR *p_evt_buf = (HC_BT_HDR *) p_mem;
127     char        *p_name, *p_tmp;
128     uint8_t     *p, status;
129     uint16_t    opcode;
130 
131     status = *((uint8_t *)(p_evt_buf + 1) + HCI_EVT_CMD_CMPL_STATUS_RET_BYTE);
132     p = (uint8_t *)(p_evt_buf + 1) + HCI_EVT_CMD_CMPL_OPCODE;
133     STREAM_TO_UINT16(opcode,p);
134 
135     ALOGI("%s Opcode:0x%04X Status: %d", __FUNCTION__, opcode, status);
136 
137     /* Must free the RX event buffer */
138     q.cb->dealloc(p_evt_buf);
139 
140     /* Once epilog process is done, must call callback to notify caller */
141     q.cb->epilog_cb(BT_VND_OP_RESULT_SUCCESS);
142 
143 }
144 
145 /*******************************************************************************
146 **
147 ** Function         hw_epilog_process
148 **
149 ** Description      Sample implementation of epilog process. This process is
150 **                  called with q_lock held and q.cb is assumed to be valid.
151 **
152 ** Returns          None
153 **
154 *******************************************************************************/
__hw_epilog_process(void)155 void __hw_epilog_process(void)
156 {
157     HC_BT_HDR  *p_buf = NULL;
158     uint8_t     *p;
159 
160     ALOGI("hw_epilog_process");
161 
162     /* Sending a HCI_RESET */
163     /* Must allocate command buffer via HC's alloc API */
164     p_buf = (HC_BT_HDR *) q.cb->alloc(BT_HC_HDR_SIZE + HCI_CMD_PREAMBLE_SIZE);
165     if (p_buf)
166     {
167         p_buf->event = MSG_STACK_TO_HC_HCI_CMD;
168         p_buf->offset = 0;
169         p_buf->layer_specific = 0;
170         p_buf->len = HCI_CMD_PREAMBLE_SIZE;
171 
172         p = (uint8_t *) (p_buf + 1);
173         UINT16_TO_STREAM(p, HCI_RESET);
174         *p = 0; /* parameter length */
175 
176         /* Send command via HC's xmit_cb API */
177         q.cb->xmit_cb(HCI_RESET, p_buf, hw_epilog_cback);
178     }
179     else
180     {
181         ALOGE("vendor lib epilog process aborted [no buffer]");
182         q.cb->epilog_cb(BT_VND_OP_RESULT_FAIL);
183     }
184 }
185 #endif // (HW_NEED_END_WITH_HCI_RESET == TRUE)
186