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 <sys/types.h>
30 #include <sys/stat.h>
31 #include <signal.h>
32 #include <time.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <dirent.h>
36 #include <ctype.h>
37 #include <cutils/properties.h>
38 #include <log/log.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "bt_hci_bdroid.h"
43 #include "bt_vendor_qcom.h"
44 #include <string.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     if (bt_vendor_cbacks)
138     {
139         /* Must free the RX event buffer */
140         bt_vendor_cbacks->dealloc(p_evt_buf);
141 
142         /* Once epilog process is done, must call callback to notify caller */
143         bt_vendor_cbacks->epilog_cb(BT_VND_OP_RESULT_SUCCESS);
144     }
145 }
146 
147 /*******************************************************************************
148 **
149 ** Function         hw_epilog_process
150 **
151 ** Description      Sample implementation of epilog process
152 **
153 ** Returns          None
154 **
155 *******************************************************************************/
hw_epilog_process(void)156 void hw_epilog_process(void)
157 {
158     HC_BT_HDR  *p_buf = NULL;
159     uint8_t     *p;
160 
161     ALOGI("hw_epilog_process");
162 
163     /* Sending a HCI_RESET */
164     if (bt_vendor_cbacks)
165     {
166         /* Must allocate command buffer via HC's alloc API */
167         p_buf = (HC_BT_HDR *) bt_vendor_cbacks->alloc(BT_HC_HDR_SIZE + \
168                                                        HCI_CMD_PREAMBLE_SIZE);
169     }
170 
171     if (p_buf)
172     {
173         p_buf->event = MSG_STACK_TO_HC_HCI_CMD;
174         p_buf->offset = 0;
175         p_buf->layer_specific = 0;
176         p_buf->len = HCI_CMD_PREAMBLE_SIZE;
177 
178         p = (uint8_t *) (p_buf + 1);
179         UINT16_TO_STREAM(p, HCI_RESET);
180         *p = 0; /* parameter length */
181 
182         /* Send command via HC's xmit_cb API */
183         bt_vendor_cbacks->xmit_cb(HCI_RESET, p_buf, hw_epilog_cback);
184     }
185     else
186     {
187         if (bt_vendor_cbacks)
188         {
189             ALOGE("vendor lib epilog process aborted [no buffer]");
190             bt_vendor_cbacks->epilog_cb(BT_VND_OP_RESULT_FAIL);
191         }
192     }
193 }
194 #endif // (HW_NEED_END_WITH_HCI_RESET == TRUE)
195