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