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