1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 *  Filename:      bt_hw.c
22 *
23 *  Description:   Bluedroid libbt-vendor callback functions
24 *
25 ******************************************************************************/
26
27#define LOG_TAG "bt_hw"
28
29#include <dlfcn.h>
30#include <utils/Log.h>
31#include <pthread.h>
32#include "bt_vendor_lib.h"
33#include "bt_hci_bdroid.h"
34#include "hci.h"
35#include "userial.h"
36
37/******************************************************************************
38**  Externs
39******************************************************************************/
40
41extern tHCI_IF *p_hci_if;
42extern uint8_t fwcfg_acked;
43void lpm_vnd_cback(uint8_t vnd_result);
44
45/******************************************************************************
46**  Variables
47******************************************************************************/
48
49bt_vendor_interface_t *bt_vnd_if=NULL;
50
51/******************************************************************************
52**  Functions
53******************************************************************************/
54
55/******************************************************************************
56**
57** Function         fwcfg_cb
58**
59** Description      HOST/CONTROLLER VENDOR LIB CALLBACK API - This function is
60**                  called when the libbt-vendor completed firmware
61**                  configuration process
62**
63** Returns          None
64**
65******************************************************************************/
66static void fwcfg_cb(bt_vendor_op_result_t result)
67{
68    bt_hc_postload_result_t status = (result == BT_VND_OP_RESULT_SUCCESS) ? \
69                                     BT_HC_PRELOAD_SUCCESS : BT_HC_PRELOAD_FAIL;
70
71    fwcfg_acked = TRUE;
72
73    if (bt_hc_cbacks)
74        bt_hc_cbacks->preload_cb(NULL, status);
75}
76
77/******************************************************************************
78**
79** Function         scocfg_cb
80**
81** Description      HOST/CONTROLLER VENDOR LIB CALLBACK API - This function is
82**                  called when the libbt-vendor completed vendor specific SCO
83**                  configuration process
84**
85** Returns          None
86**
87******************************************************************************/
88static void scocfg_cb(bt_vendor_op_result_t result)
89{
90    /* Continue rest of postload process*/
91    p_hci_if->get_acl_max_len();
92}
93
94/******************************************************************************
95**
96** Function         lpm_vnd_cb
97**
98** Description      HOST/CONTROLLER VENDOR LIB CALLBACK API - This function is
99**                  called back from the libbt-vendor to indicate the result of
100**                  previous LPM enable/disable request
101**
102** Returns          None
103**
104******************************************************************************/
105static void lpm_vnd_cb(bt_vendor_op_result_t result)
106{
107    uint8_t status = (result == BT_VND_OP_RESULT_SUCCESS) ? 0 : 1;
108
109    lpm_vnd_cback(status);
110}
111
112/******************************************************************************
113**
114** Function         alloc
115**
116** Description      HOST/CONTROLLER VENDOR LIB CALLOUT API - This function is
117**                  called from the libbt-vendor to request for data buffer
118**                  allocation
119**
120** Returns          NULL / pointer to allocated buffer
121**
122******************************************************************************/
123static void *alloc(int size)
124{
125    HC_BT_HDR *p_hdr = NULL;
126
127    if (bt_hc_cbacks)
128        p_hdr = (HC_BT_HDR *) bt_hc_cbacks->alloc(size);
129
130    return (p_hdr);
131}
132
133/******************************************************************************
134**
135** Function         dealloc
136**
137** Description      HOST/CONTROLLER VENDOR LIB CALLOUT API - This function is
138**                  called from the libbt-vendor to release the data buffer
139**                  allocated through the alloc call earlier
140**
141** Returns          None
142**
143******************************************************************************/
144static void dealloc(void *p_buf)
145{
146    HC_BT_HDR *p_hdr = (HC_BT_HDR *) p_buf;
147
148    if (bt_hc_cbacks)
149        bt_hc_cbacks->dealloc((TRANSAC) p_buf, (char *) (p_hdr+1));
150}
151
152/******************************************************************************
153**
154** Function         xmit_cb
155**
156** Description      HOST/CONTROLLER VEDNOR LIB CALLOUT API - This function is
157**                  called from the libbt-vendor in order to send a prepared
158**                  HCI command packet through HCI transport TX function.
159**
160** Returns          TRUE/FALSE
161**
162******************************************************************************/
163static uint8_t xmit_cb(uint16_t opcode, void *p_buf, tINT_CMD_CBACK p_cback)
164{
165    return p_hci_if->send_int_cmd(opcode, (HC_BT_HDR *)p_buf, p_cback);
166}
167
168/******************************************************************************
169**
170** Function         epilog_cb
171**
172** Description      HOST/CONTROLLER VENDOR LIB CALLBACK API - This function is
173**                  called back from the libbt-vendor to indicate the result of
174**                  previous epilog call.
175**
176** Returns          None
177**
178******************************************************************************/
179static void epilog_cb(bt_vendor_op_result_t result)
180{
181    bthc_signal_event(HC_EVENT_EXIT);
182}
183
184/*****************************************************************************
185**   The libbt-vendor Callback Functions Table
186*****************************************************************************/
187static const bt_vendor_callbacks_t vnd_callbacks = {
188    sizeof(bt_vendor_callbacks_t),
189    fwcfg_cb,
190    scocfg_cb,
191    lpm_vnd_cb,
192    alloc,
193    dealloc,
194    xmit_cb,
195    epilog_cb
196};
197
198/******************************************************************************
199**
200** Function         init_vnd_if
201**
202** Description      Initialize vendor lib interface
203**
204** Returns          None
205**
206******************************************************************************/
207void init_vnd_if(unsigned char *local_bdaddr)
208{
209    void *dlhandle;
210
211    dlhandle = dlopen("libbt-vendor.so", RTLD_NOW);
212    if (!dlhandle)
213    {
214        ALOGE("!!! Failed to load libbt-vendor.so !!!");
215        return;
216    }
217
218    bt_vnd_if = (bt_vendor_interface_t *) dlsym(dlhandle, "BLUETOOTH_VENDOR_LIB_INTERFACE");
219    if (!bt_vnd_if)
220    {
221        ALOGE("!!! Failed to get bt vendor interface !!!");
222        return;
223    }
224
225    bt_vnd_if->init(&vnd_callbacks, local_bdaddr);
226}
227
228