1faa6c1f6SMatthias Ringwald /*
2faa6c1f6SMatthias Ringwald * Copyright (C) 2009-2012 by Matthias Ringwald
3faa6c1f6SMatthias Ringwald *
4faa6c1f6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
5faa6c1f6SMatthias Ringwald * modification, are permitted provided that the following conditions
6faa6c1f6SMatthias Ringwald * are met:
7faa6c1f6SMatthias Ringwald *
8faa6c1f6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
9faa6c1f6SMatthias Ringwald * notice, this list of conditions and the following disclaimer.
10faa6c1f6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
11faa6c1f6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
12faa6c1f6SMatthias Ringwald * documentation and/or other materials provided with the distribution.
13faa6c1f6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
14faa6c1f6SMatthias Ringwald * contributors may be used to endorse or promote products derived
15faa6c1f6SMatthias Ringwald * from this software without specific prior written permission.
16faa6c1f6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
17faa6c1f6SMatthias Ringwald * personal benefit and not for any commercial purpose or for
18faa6c1f6SMatthias Ringwald * monetary gain.
19faa6c1f6SMatthias Ringwald *
20faa6c1f6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
21faa6c1f6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22faa6c1f6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23faa6c1f6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24faa6c1f6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25faa6c1f6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26faa6c1f6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27faa6c1f6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28faa6c1f6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29faa6c1f6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30faa6c1f6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31faa6c1f6SMatthias Ringwald * SUCH DAMAGE.
32faa6c1f6SMatthias Ringwald *
3351acb414SMatthias Ringwald * Please inquire about commercial licensing options at [email protected]
34faa6c1f6SMatthias Ringwald *
35faa6c1f6SMatthias Ringwald */
36faa6c1f6SMatthias Ringwald
37e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_chipset_bcm.c"
38ab2c6ae4SMatthias Ringwald
39faa6c1f6SMatthias Ringwald /*
40faa6c1f6SMatthias Ringwald * bt_control_bcm.c
41faa6c1f6SMatthias Ringwald *
42faa6c1f6SMatthias Ringwald * Adapter to use Broadcom-based chipsets with BTstack
43faa6c1f6SMatthias Ringwald */
44faa6c1f6SMatthias Ringwald
45faa6c1f6SMatthias Ringwald
46faa6c1f6SMatthias Ringwald #include "btstack_config.h"
47faa6c1f6SMatthias Ringwald
48faa6c1f6SMatthias Ringwald #include <stddef.h> /* NULL */
49faa6c1f6SMatthias Ringwald #include <stdio.h>
50faa6c1f6SMatthias Ringwald #include <string.h> /* memcpy */
51faa6c1f6SMatthias Ringwald
52faa6c1f6SMatthias Ringwald #include "btstack_control.h"
53faa6c1f6SMatthias Ringwald #include "btstack_debug.h"
54c0cdcfe7SMatthias Ringwald #include "btstack_chipset_bcm.h"
557a4d61a3SMatthias Ringwald #include "hci.h"
56faa6c1f6SMatthias Ringwald
5764eccac7SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
58761802ccSMatthias Ringwald #include "tinydir.h"
5964eccac7SMatthias Ringwald #include <ctype.h>
609ed5a9efSMatthias Ringwald #endif
619ed5a9efSMatthias Ringwald
6274f83314SMatthias Ringwald #ifdef _MSC_VER
6374f83314SMatthias Ringwald // ignore deprecated warning for fopen
6474f83314SMatthias Ringwald #pragma warning(disable : 4996)
65c9207d20SMatthias Ringwald
66c9207d20SMatthias Ringwald // map strncasecmp
67c9207d20SMatthias Ringwald #define strncasecmp _strnicmp
6874f83314SMatthias Ringwald #endif
6974f83314SMatthias Ringwald
707a4d61a3SMatthias Ringwald // assert outgoing and incoming hci packet buffers can hold max hci command resp. event packet
717a4d61a3SMatthias Ringwald #if HCI_OUTGOING_PACKET_BUFFER_SIZE < (HCI_CMD_HEADER_SIZE + 255)
727a4d61a3SMatthias Ringwald #error "HCI_OUTGOING_PACKET_BUFFER_SIZE to small. Outgoing HCI packet buffer to small for largest HCI Command packet. Please set HCI_ACL_PAYLOAD_SIZE to 258 or higher."
737a4d61a3SMatthias Ringwald #endif
746ffe3f3eSMatthias Ringwald #if HCI_INCOMING_PACKET_BUFFER_SIZE < (HCI_EVENT_HEADER_SIZE + 255)
757a4d61a3SMatthias Ringwald #error "HCI_INCOMING_PACKET_BUFFER_SIZE to small. Incoming HCI packet buffer to small for largest HCI Event packet. Please set HCI_ACL_PAYLOAD_SIZE to 257 or higher."
767a4d61a3SMatthias Ringwald #endif
777a4d61a3SMatthias Ringwald
7864eccac7SMatthias Ringwald static int send_download_command;
7964eccac7SMatthias Ringwald static uint32_t init_script_offset;
8064eccac7SMatthias Ringwald
8164eccac7SMatthias Ringwald // Embedded == non posix systems
8264eccac7SMatthias Ringwald
83faa6c1f6SMatthias Ringwald // actual init script provided by separate bt_firmware_image.c from WICED SDK
84faa6c1f6SMatthias Ringwald extern const uint8_t brcm_patchram_buf[];
85faa6c1f6SMatthias Ringwald extern const int brcm_patch_ram_length;
86faa6c1f6SMatthias Ringwald extern const char brcm_patch_version[];
87faa6c1f6SMatthias Ringwald
88faa6c1f6SMatthias Ringwald //
89faa6c1f6SMatthias Ringwald // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c
chipset_set_baudrate_command(uint32_t baudrate,uint8_t * hci_cmd_buffer)90faa6c1f6SMatthias Ringwald static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
91faa6c1f6SMatthias Ringwald hci_cmd_buffer[0] = 0x18;
92faa6c1f6SMatthias Ringwald hci_cmd_buffer[1] = 0xfc;
93faa6c1f6SMatthias Ringwald hci_cmd_buffer[2] = 0x06;
94faa6c1f6SMatthias Ringwald hci_cmd_buffer[3] = 0x00;
95faa6c1f6SMatthias Ringwald hci_cmd_buffer[4] = 0x00;
96f8fbdce0SMatthias Ringwald little_endian_store_32(hci_cmd_buffer, 5, baudrate);
97faa6c1f6SMatthias Ringwald }
98faa6c1f6SMatthias Ringwald
99faa6c1f6SMatthias Ringwald // @note: bd addr has to be set after sending init script (it might just get re-set)
chipset_set_bd_addr_command(bd_addr_t addr,uint8_t * hci_cmd_buffer)100faa6c1f6SMatthias Ringwald static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){
101faa6c1f6SMatthias Ringwald hci_cmd_buffer[0] = 0x01;
102faa6c1f6SMatthias Ringwald hci_cmd_buffer[1] = 0xfc;
103faa6c1f6SMatthias Ringwald hci_cmd_buffer[2] = 0x06;
104a47028edSMatthias Ringwald reverse_bd_addr(addr, &hci_cmd_buffer[3]);
105faa6c1f6SMatthias Ringwald }
106faa6c1f6SMatthias Ringwald
10764eccac7SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
10864eccac7SMatthias Ringwald
10964eccac7SMatthias Ringwald static const char * hcd_file_path;
11064eccac7SMatthias Ringwald static const char * hcd_folder_path = ".";
111bd68af28SMatthias Ringwald static FILE * hcd_file;
11264eccac7SMatthias Ringwald static char matched_file[1000];
11364eccac7SMatthias Ringwald
11464eccac7SMatthias Ringwald
chipset_init(const void * config)11564eccac7SMatthias Ringwald static void chipset_init(const void * config){
116*5449042eSMatthias Ringwald UNUSED(config);
11764eccac7SMatthias Ringwald if (hcd_file_path){
11864eccac7SMatthias Ringwald log_info("chipset-bcm: init file %s", hcd_file_path);
11964eccac7SMatthias Ringwald } else {
12064eccac7SMatthias Ringwald log_info("chipset-bcm: init folder %s", hcd_folder_path);
12164eccac7SMatthias Ringwald }
12264eccac7SMatthias Ringwald send_download_command = 1;
12364eccac7SMatthias Ringwald init_script_offset = 0;
124bd68af28SMatthias Ringwald hcd_file = NULL;
12564eccac7SMatthias Ringwald }
12664eccac7SMatthias Ringwald
12764eccac7SMatthias Ringwald static const uint8_t download_command[] = {0x2e, 0xfc, 0x00};
12864eccac7SMatthias Ringwald
chipset_next_command(uint8_t * hci_cmd_buffer)12964eccac7SMatthias Ringwald static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
130bd68af28SMatthias Ringwald if (hcd_file == NULL){
13164eccac7SMatthias Ringwald log_info("chipset-bcm: open file %s", hcd_file_path);
132bd68af28SMatthias Ringwald hcd_file = fopen(hcd_file_path, "rb");
133bd68af28SMatthias Ringwald if (hcd_file == NULL){
13464eccac7SMatthias Ringwald log_error("chipset-bcm: can't open file %s", hcd_file_path);
1351565be94SMatthias Ringwald return BTSTACK_CHIPSET_NO_INIT_SCRIPT;
13664eccac7SMatthias Ringwald }
13764eccac7SMatthias Ringwald }
13864eccac7SMatthias Ringwald
13964eccac7SMatthias Ringwald // send download firmware command
14064eccac7SMatthias Ringwald if (send_download_command){
14164eccac7SMatthias Ringwald send_download_command = 0;
14264eccac7SMatthias Ringwald memcpy(hci_cmd_buffer, download_command, sizeof(download_command));
14364eccac7SMatthias Ringwald return BTSTACK_CHIPSET_VALID_COMMAND;
14464eccac7SMatthias Ringwald }
14564eccac7SMatthias Ringwald
14664eccac7SMatthias Ringwald // read next command, but skip download command
14764eccac7SMatthias Ringwald do {
14864eccac7SMatthias Ringwald // read command
149bd68af28SMatthias Ringwald size_t bytes_read = fread(hci_cmd_buffer, 1, 3, hcd_file);
150bd68af28SMatthias Ringwald if (bytes_read < 3){
151bd68af28SMatthias Ringwald if (feof(hcd_file)) {
15264eccac7SMatthias Ringwald log_info("chipset-bcm: end of file, size %u", init_script_offset);
153bd68af28SMatthias Ringwald } else {
15464eccac7SMatthias Ringwald log_error("chipset-bcm: read error at %u", init_script_offset);
155bd68af28SMatthias Ringwald }
156bd68af28SMatthias Ringwald fclose(hcd_file);
157bd68af28SMatthias Ringwald hcd_file = NULL;
15864eccac7SMatthias Ringwald return BTSTACK_CHIPSET_DONE;
15964eccac7SMatthias Ringwald }
16064eccac7SMatthias Ringwald init_script_offset += 3;
16164eccac7SMatthias Ringwald
16264eccac7SMatthias Ringwald // read parameters
163*5449042eSMatthias Ringwald size_t param_len = hci_cmd_buffer[2];
16464eccac7SMatthias Ringwald if (param_len){
165bd68af28SMatthias Ringwald bytes_read = fread(&hci_cmd_buffer[3], 1, param_len, hcd_file);
16664eccac7SMatthias Ringwald }
167bd68af28SMatthias Ringwald if (bytes_read < param_len){
16864eccac7SMatthias Ringwald log_error("chipset-bcm: read error at %u", init_script_offset);
169bd68af28SMatthias Ringwald fclose(hcd_file);
170bd68af28SMatthias Ringwald hcd_file = NULL;
17164eccac7SMatthias Ringwald return BTSTACK_CHIPSET_DONE;
17264eccac7SMatthias Ringwald }
17364eccac7SMatthias Ringwald init_script_offset += param_len;
17464eccac7SMatthias Ringwald
1758db5aeedSMatthias Ringwald } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 0);
17664eccac7SMatthias Ringwald return BTSTACK_CHIPSET_VALID_COMMAND;
17764eccac7SMatthias Ringwald }
17864eccac7SMatthias Ringwald
btstack_chipset_bcm_set_hcd_file_path(const char * path)17964eccac7SMatthias Ringwald void btstack_chipset_bcm_set_hcd_file_path(const char * path){
18064eccac7SMatthias Ringwald hcd_file_path = path;
18164eccac7SMatthias Ringwald }
18264eccac7SMatthias Ringwald
btstack_chipset_bcm_set_hcd_folder_path(const char * path)18364eccac7SMatthias Ringwald void btstack_chipset_bcm_set_hcd_folder_path(const char * path){
18464eccac7SMatthias Ringwald hcd_folder_path = path;
18564eccac7SMatthias Ringwald }
18664eccac7SMatthias Ringwald
btstack_chipset_bcm_set_device_name(const char * device_name)18764eccac7SMatthias Ringwald void btstack_chipset_bcm_set_device_name(const char * device_name){
18864eccac7SMatthias Ringwald // ignore if file path already set
18964eccac7SMatthias Ringwald if (hcd_file_path) {
19064eccac7SMatthias Ringwald log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path);
19164eccac7SMatthias Ringwald return;
19264eccac7SMatthias Ringwald }
19364eccac7SMatthias Ringwald
19464eccac7SMatthias Ringwald // find in folder
1957d6c5059SMatthias Ringwald tinydir_dir dir = {};
196761802ccSMatthias Ringwald int res = tinydir_open(&dir, hcd_folder_path);
197761802ccSMatthias Ringwald if (res < 0){
19864eccac7SMatthias Ringwald log_error("chipset-bcm: could not get directory for %s", hcd_folder_path);
19964eccac7SMatthias Ringwald return;
20064eccac7SMatthias Ringwald }
201ab3cc223SMatthias Ringwald uint16_t device_name_len = (uint16_t) strlen(device_name);
202761802ccSMatthias Ringwald while (dir.has_next) {
203761802ccSMatthias Ringwald tinydir_file file;
204761802ccSMatthias Ringwald tinydir_readfile(&dir, &file);
205761802ccSMatthias Ringwald tinydir_next(&dir);
206ab3cc223SMatthias Ringwald // starts with $device_name?
207ab3cc223SMatthias Ringwald if (strncasecmp(device_name, file.name, device_name_len) != 0) {
20864eccac7SMatthias Ringwald continue;
20964eccac7SMatthias Ringwald }
210ab3cc223SMatthias Ringwald // long enough to have ".hcd" suffix
211ab3cc223SMatthias Ringwald uint16_t filename_len = (uint16_t) strlen(file.name);
212ab3cc223SMatthias Ringwald if (filename_len < 5) {
213ab3cc223SMatthias Ringwald continue;
214ab3cc223SMatthias Ringwald }
215ab3cc223SMatthias Ringwald // check suffix
216ab3cc223SMatthias Ringwald if (strncasecmp(".hcd", &file.name[filename_len - 4], device_name_len) == 0) {
217ab3cc223SMatthias Ringwald btstack_strcpy(matched_file, sizeof(matched_file), hcd_folder_path);
218ab3cc223SMatthias Ringwald btstack_strcat(matched_file, sizeof(matched_file), "/");
219ab3cc223SMatthias Ringwald btstack_strcat(matched_file, sizeof(matched_file), file.name);
220ab3cc223SMatthias Ringwald hcd_file_path = matched_file;
221ab3cc223SMatthias Ringwald break;
22264eccac7SMatthias Ringwald }
22364eccac7SMatthias Ringwald }
224761802ccSMatthias Ringwald tinydir_close(&dir);
225ab3cc223SMatthias Ringwald if (hcd_file_path == NULL) {
226ab3cc223SMatthias Ringwald log_error("chipset-bcm: could not find .hcd that starts with %s at path %s", device_name, hcd_folder_path);
22764eccac7SMatthias Ringwald }
22864eccac7SMatthias Ringwald }
22964eccac7SMatthias Ringwald
23064eccac7SMatthias Ringwald #else
23164eccac7SMatthias Ringwald
chipset_init(const void * config)23264eccac7SMatthias Ringwald static void chipset_init(const void * config){
233*5449042eSMatthias Ringwald UNUSED(config);
23464eccac7SMatthias Ringwald log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length);
23564eccac7SMatthias Ringwald init_script_offset = 0;
23664eccac7SMatthias Ringwald send_download_command = 1;
23764eccac7SMatthias Ringwald }
23864eccac7SMatthias Ringwald
chipset_next_command(uint8_t * hci_cmd_buffer)239faa6c1f6SMatthias Ringwald static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
2401565be94SMatthias Ringwald // no initscript
2411565be94SMatthias Ringwald if (brcm_patch_ram_length == 0) return BTSTACK_CHIPSET_NO_INIT_SCRIPT;
2421565be94SMatthias Ringwald
243faa6c1f6SMatthias Ringwald // send download firmware command
244faa6c1f6SMatthias Ringwald if (send_download_command){
245faa6c1f6SMatthias Ringwald send_download_command = 0;
246faa6c1f6SMatthias Ringwald hci_cmd_buffer[0] = 0x2e;
247faa6c1f6SMatthias Ringwald hci_cmd_buffer[1] = 0xfc;
248faa6c1f6SMatthias Ringwald hci_cmd_buffer[2] = 0x00;
249faa6c1f6SMatthias Ringwald return BTSTACK_CHIPSET_VALID_COMMAND;
250faa6c1f6SMatthias Ringwald }
251faa6c1f6SMatthias Ringwald
252faa6c1f6SMatthias Ringwald if (init_script_offset >= brcm_patch_ram_length) {
253faa6c1f6SMatthias Ringwald return BTSTACK_CHIPSET_DONE;
254faa6c1f6SMatthias Ringwald }
255faa6c1f6SMatthias Ringwald
256faa6c1f6SMatthias Ringwald int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2];
257faa6c1f6SMatthias Ringwald memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len);
258faa6c1f6SMatthias Ringwald init_script_offset += cmd_len;
259faa6c1f6SMatthias Ringwald return BTSTACK_CHIPSET_VALID_COMMAND;
260faa6c1f6SMatthias Ringwald }
26164eccac7SMatthias Ringwald #endif
262faa6c1f6SMatthias Ringwald
263faa6c1f6SMatthias Ringwald
264646a1850SMatthias Ringwald static btstack_chipset_t btstack_chipset_bcm = {
265faa6c1f6SMatthias Ringwald "BCM",
266faa6c1f6SMatthias Ringwald chipset_init,
267faa6c1f6SMatthias Ringwald chipset_next_command,
268faa6c1f6SMatthias Ringwald chipset_set_baudrate_command,
269faa6c1f6SMatthias Ringwald chipset_set_bd_addr_command,
270faa6c1f6SMatthias Ringwald };
271faa6c1f6SMatthias Ringwald
272faa6c1f6SMatthias Ringwald // MARK: public API
btstack_chipset_bcm_instance(void)273faa6c1f6SMatthias Ringwald const btstack_chipset_t * btstack_chipset_bcm_instance(void){
274faa6c1f6SMatthias Ringwald return &btstack_chipset_bcm;
275faa6c1f6SMatthias Ringwald }
276646a1850SMatthias Ringwald
277646a1850SMatthias Ringwald /**
278646a1850SMatthias Ringwald * @brief Enable init file - needed by btstack_chipset_bcm_download_firmware when using h5
279646a1850SMatthias Ringwald * @param enabled
280646a1850SMatthias Ringwald */
btstack_chipset_bcm_enable_init_script(int enabled)281646a1850SMatthias Ringwald void btstack_chipset_bcm_enable_init_script(int enabled){
282646a1850SMatthias Ringwald if (enabled){
283646a1850SMatthias Ringwald btstack_chipset_bcm.next_command = &chipset_next_command;
284646a1850SMatthias Ringwald } else {
285646a1850SMatthias Ringwald btstack_chipset_bcm.next_command = NULL;
286646a1850SMatthias Ringwald }
287646a1850SMatthias Ringwald }
288