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