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