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 37faa6c1f6SMatthias Ringwald /* 38faa6c1f6SMatthias Ringwald * bt_control_bcm.c 39faa6c1f6SMatthias Ringwald * 40faa6c1f6SMatthias Ringwald * Adapter to use Broadcom-based chipsets with BTstack 41faa6c1f6SMatthias Ringwald */ 42faa6c1f6SMatthias Ringwald 43faa6c1f6SMatthias Ringwald 44faa6c1f6SMatthias Ringwald #include "btstack_config.h" 45faa6c1f6SMatthias Ringwald 46faa6c1f6SMatthias Ringwald #include <stddef.h> /* NULL */ 47faa6c1f6SMatthias Ringwald #include <stdio.h> 48faa6c1f6SMatthias Ringwald #include <string.h> /* memcpy */ 49faa6c1f6SMatthias Ringwald 50faa6c1f6SMatthias Ringwald #include "btstack_control.h" 51faa6c1f6SMatthias Ringwald #include "btstack_debug.h" 52c0cdcfe7SMatthias Ringwald #include "btstack_chipset_bcm.h" 53faa6c1f6SMatthias Ringwald 54*64eccac7SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 55*64eccac7SMatthias Ringwald #include <ctype.h> 56*64eccac7SMatthias Ringwald #include <dirent.h> 57*64eccac7SMatthias Ringwald #include <fcntl.h> 58*64eccac7SMatthias Ringwald #include <unistd.h> 59*64eccac7SMatthias Ringwald #endif 60*64eccac7SMatthias Ringwald 61*64eccac7SMatthias Ringwald static int send_download_command; 62*64eccac7SMatthias Ringwald static uint32_t init_script_offset; 63*64eccac7SMatthias Ringwald 64*64eccac7SMatthias Ringwald // Embedded == non posix systems 65*64eccac7SMatthias Ringwald 66faa6c1f6SMatthias Ringwald // actual init script provided by separate bt_firmware_image.c from WICED SDK 67faa6c1f6SMatthias Ringwald extern const uint8_t brcm_patchram_buf[]; 68faa6c1f6SMatthias Ringwald extern const int brcm_patch_ram_length; 69faa6c1f6SMatthias Ringwald extern const char brcm_patch_version[]; 70faa6c1f6SMatthias Ringwald 71faa6c1f6SMatthias Ringwald // 72faa6c1f6SMatthias Ringwald // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c 73faa6c1f6SMatthias Ringwald static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){ 74faa6c1f6SMatthias Ringwald hci_cmd_buffer[0] = 0x18; 75faa6c1f6SMatthias Ringwald hci_cmd_buffer[1] = 0xfc; 76faa6c1f6SMatthias Ringwald hci_cmd_buffer[2] = 0x06; 77faa6c1f6SMatthias Ringwald hci_cmd_buffer[3] = 0x00; 78faa6c1f6SMatthias Ringwald hci_cmd_buffer[4] = 0x00; 79f8fbdce0SMatthias Ringwald little_endian_store_32(hci_cmd_buffer, 5, baudrate); 80faa6c1f6SMatthias Ringwald } 81faa6c1f6SMatthias Ringwald 82faa6c1f6SMatthias Ringwald // @note: bd addr has to be set after sending init script (it might just get re-set) 83faa6c1f6SMatthias Ringwald static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){ 84faa6c1f6SMatthias Ringwald hci_cmd_buffer[0] = 0x01; 85faa6c1f6SMatthias Ringwald hci_cmd_buffer[1] = 0xfc; 86faa6c1f6SMatthias Ringwald hci_cmd_buffer[2] = 0x06; 87a47028edSMatthias Ringwald reverse_bd_addr(addr, &hci_cmd_buffer[3]); 88faa6c1f6SMatthias Ringwald } 89faa6c1f6SMatthias Ringwald 90*64eccac7SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 91*64eccac7SMatthias Ringwald 92*64eccac7SMatthias Ringwald static const char * hcd_file_path; 93*64eccac7SMatthias Ringwald static const char * hcd_folder_path = "."; 94*64eccac7SMatthias Ringwald static int hcd_fd; 95*64eccac7SMatthias Ringwald static char matched_file[1000]; 96*64eccac7SMatthias Ringwald 97*64eccac7SMatthias Ringwald 98*64eccac7SMatthias Ringwald static void chipset_init(const void * config){ 99*64eccac7SMatthias Ringwald if (hcd_file_path){ 100*64eccac7SMatthias Ringwald log_info("chipset-bcm: init file %s", hcd_file_path); 101*64eccac7SMatthias Ringwald } else { 102*64eccac7SMatthias Ringwald log_info("chipset-bcm: init folder %s", hcd_folder_path); 103*64eccac7SMatthias Ringwald } 104*64eccac7SMatthias Ringwald send_download_command = 1; 105*64eccac7SMatthias Ringwald init_script_offset = 0; 106*64eccac7SMatthias Ringwald hcd_fd = -1; 107*64eccac7SMatthias Ringwald } 108*64eccac7SMatthias Ringwald 109*64eccac7SMatthias Ringwald static const uint8_t download_command[] = {0x2e, 0xfc, 0x00}; 110*64eccac7SMatthias Ringwald 111*64eccac7SMatthias Ringwald static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 112*64eccac7SMatthias Ringwald if (hcd_fd < 0){ 113*64eccac7SMatthias Ringwald log_info("chipset-bcm: open file %s", hcd_file_path); 114*64eccac7SMatthias Ringwald hcd_fd = open(hcd_file_path, O_RDONLY); 115*64eccac7SMatthias Ringwald if (hcd_fd < 0){ 116*64eccac7SMatthias Ringwald log_error("chipset-bcm: can't open file %s", hcd_file_path); 117*64eccac7SMatthias Ringwald return BTSTACK_CHIPSET_DONE; 118*64eccac7SMatthias Ringwald } 119*64eccac7SMatthias Ringwald } 120*64eccac7SMatthias Ringwald 121*64eccac7SMatthias Ringwald // send download firmware command 122*64eccac7SMatthias Ringwald if (send_download_command){ 123*64eccac7SMatthias Ringwald send_download_command = 0; 124*64eccac7SMatthias Ringwald memcpy(hci_cmd_buffer, download_command, sizeof(download_command)); 125*64eccac7SMatthias Ringwald return BTSTACK_CHIPSET_VALID_COMMAND; 126*64eccac7SMatthias Ringwald } 127*64eccac7SMatthias Ringwald 128*64eccac7SMatthias Ringwald // read next command, but skip download command 129*64eccac7SMatthias Ringwald do { 130*64eccac7SMatthias Ringwald // read command 131*64eccac7SMatthias Ringwald int res = read(hcd_fd, hci_cmd_buffer, 3); 132*64eccac7SMatthias Ringwald if (res == 0){ 133*64eccac7SMatthias Ringwald log_info("chipset-bcm: end of file, size %u", init_script_offset); 134*64eccac7SMatthias Ringwald close(hcd_fd); 135*64eccac7SMatthias Ringwald 136*64eccac7SMatthias Ringwald // wait for firmware patch to be applied - shorter delay possible 137*64eccac7SMatthias Ringwald sleep(1); 138*64eccac7SMatthias Ringwald 139*64eccac7SMatthias Ringwald return BTSTACK_CHIPSET_DONE; 140*64eccac7SMatthias Ringwald } 141*64eccac7SMatthias Ringwald if (res < 0){ 142*64eccac7SMatthias Ringwald log_error("chipset-bcm: read error at %u", init_script_offset); 143*64eccac7SMatthias Ringwald return BTSTACK_CHIPSET_DONE; 144*64eccac7SMatthias Ringwald } 145*64eccac7SMatthias Ringwald init_script_offset += 3; 146*64eccac7SMatthias Ringwald 147*64eccac7SMatthias Ringwald // read parameters 148*64eccac7SMatthias Ringwald int param_len = hci_cmd_buffer[2]; 149*64eccac7SMatthias Ringwald if (param_len){ 150*64eccac7SMatthias Ringwald res = read(hcd_fd, &hci_cmd_buffer[3], param_len); 151*64eccac7SMatthias Ringwald } 152*64eccac7SMatthias Ringwald if (res < 0){ 153*64eccac7SMatthias Ringwald log_error("chipset-bcm: read error at %u", init_script_offset); 154*64eccac7SMatthias Ringwald return BTSTACK_CHIPSET_DONE; 155*64eccac7SMatthias Ringwald } 156*64eccac7SMatthias Ringwald init_script_offset += param_len; 157*64eccac7SMatthias Ringwald 158*64eccac7SMatthias Ringwald } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 1); 159*64eccac7SMatthias Ringwald return BTSTACK_CHIPSET_VALID_COMMAND; 160*64eccac7SMatthias Ringwald } 161*64eccac7SMatthias Ringwald 162*64eccac7SMatthias Ringwald void btstack_chipset_bcm_set_hcd_file_path(const char * path){ 163*64eccac7SMatthias Ringwald hcd_file_path = path; 164*64eccac7SMatthias Ringwald } 165*64eccac7SMatthias Ringwald 166*64eccac7SMatthias Ringwald void btstack_chipset_bcm_set_hcd_folder_path(const char * path){ 167*64eccac7SMatthias Ringwald hcd_folder_path = path; 168*64eccac7SMatthias Ringwald } 169*64eccac7SMatthias Ringwald 170*64eccac7SMatthias Ringwald static int equal_ignore_case(const char *str1, const char *str2){ 171*64eccac7SMatthias Ringwald if (!str1 || !str2) return (1); 172*64eccac7SMatthias Ringwald int i = 0; 173*64eccac7SMatthias Ringwald while (1){ 174*64eccac7SMatthias Ringwald if (!str1[i] && !str2[i]) return 1; 175*64eccac7SMatthias Ringwald if (tolower(str1[i]) != tolower(str2[i])) return 0; 176*64eccac7SMatthias Ringwald if (!str1[i] || !str2[i]) return 0; 177*64eccac7SMatthias Ringwald i++; 178*64eccac7SMatthias Ringwald } 179*64eccac7SMatthias Ringwald } 180*64eccac7SMatthias Ringwald 181*64eccac7SMatthias Ringwald // assumption starts with BCM or bcm 182*64eccac7SMatthias Ringwald #define MAX_DEVICE_NAME_LEN 15 183*64eccac7SMatthias Ringwald void btstack_chipset_bcm_set_device_name(const char * device_name){ 184*64eccac7SMatthias Ringwald // ignore if file path already set 185*64eccac7SMatthias Ringwald if (hcd_file_path) { 186*64eccac7SMatthias Ringwald log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path); 187*64eccac7SMatthias Ringwald return; 188*64eccac7SMatthias Ringwald } 189*64eccac7SMatthias Ringwald // construct filename for long variant 190*64eccac7SMatthias Ringwald if (strlen(device_name) > MAX_DEVICE_NAME_LEN){ 191*64eccac7SMatthias Ringwald log_error("chipset-bcm: device name %s too long", device_name); 192*64eccac7SMatthias Ringwald return; 193*64eccac7SMatthias Ringwald } 194*64eccac7SMatthias Ringwald char filename_complete[MAX_DEVICE_NAME_LEN+5]; 195*64eccac7SMatthias Ringwald strcpy(filename_complete, device_name); 196*64eccac7SMatthias Ringwald strcat(filename_complete, ".hcd"); 197*64eccac7SMatthias Ringwald 198*64eccac7SMatthias Ringwald // construct short variant without revision info 199*64eccac7SMatthias Ringwald char filename_short[MAX_DEVICE_NAME_LEN+5]; 200*64eccac7SMatthias Ringwald strcpy(filename_short, device_name); 201*64eccac7SMatthias Ringwald int len = strlen(filename_short); 202*64eccac7SMatthias Ringwald while (len > 3){ 203*64eccac7SMatthias Ringwald char c = filename_short[len-1]; 204*64eccac7SMatthias Ringwald if (isdigit(c) == 0) break; 205*64eccac7SMatthias Ringwald len--; 206*64eccac7SMatthias Ringwald } 207*64eccac7SMatthias Ringwald if (len > 3){ 208*64eccac7SMatthias Ringwald filename_short[len-1] = 0; 209*64eccac7SMatthias Ringwald } 210*64eccac7SMatthias Ringwald strcat(filename_short, ".hcd"); 211*64eccac7SMatthias Ringwald log_info("chipset-bcm: looking for %s and %s", filename_short, filename_complete); 212*64eccac7SMatthias Ringwald 213*64eccac7SMatthias Ringwald // find in folder 214*64eccac7SMatthias Ringwald DIR *dirp = opendir(hcd_folder_path); 215*64eccac7SMatthias Ringwald int match_short = 0; 216*64eccac7SMatthias Ringwald int match_complete = 0; 217*64eccac7SMatthias Ringwald if (!dirp){ 218*64eccac7SMatthias Ringwald log_error("chipset-bcm: could not get directory for %s", hcd_folder_path); 219*64eccac7SMatthias Ringwald return; 220*64eccac7SMatthias Ringwald } 221*64eccac7SMatthias Ringwald while (1){ 222*64eccac7SMatthias Ringwald struct dirent *dp = readdir(dirp); 223*64eccac7SMatthias Ringwald if (!dp) break; 224*64eccac7SMatthias Ringwald if (equal_ignore_case(filename_complete, dp->d_name)){ 225*64eccac7SMatthias Ringwald match_complete = 1; 226*64eccac7SMatthias Ringwald continue; 227*64eccac7SMatthias Ringwald } 228*64eccac7SMatthias Ringwald if (equal_ignore_case(filename_short, dp->d_name)){ 229*64eccac7SMatthias Ringwald match_short = 1; 230*64eccac7SMatthias Ringwald } 231*64eccac7SMatthias Ringwald } 232*64eccac7SMatthias Ringwald closedir(dirp); 233*64eccac7SMatthias Ringwald if (match_complete){ 234*64eccac7SMatthias Ringwald sprintf(matched_file, "%s/%s", hcd_folder_path, filename_complete); 235*64eccac7SMatthias Ringwald hcd_file_path = matched_file; 236*64eccac7SMatthias Ringwald return; 237*64eccac7SMatthias Ringwald } 238*64eccac7SMatthias Ringwald if (match_short){ 239*64eccac7SMatthias Ringwald sprintf(matched_file, "%s/%s", hcd_folder_path, filename_short); 240*64eccac7SMatthias Ringwald hcd_file_path = matched_file; 241*64eccac7SMatthias Ringwald return; 242*64eccac7SMatthias Ringwald } 243*64eccac7SMatthias Ringwald log_error("chipset-bcm: could not find %s or %s, please provide .hcd file in %s", filename_complete, filename_short, hcd_folder_path); 244*64eccac7SMatthias Ringwald } 245*64eccac7SMatthias Ringwald 246*64eccac7SMatthias Ringwald #else 247*64eccac7SMatthias Ringwald 248*64eccac7SMatthias Ringwald static void chipset_init(const void * config){ 249*64eccac7SMatthias Ringwald log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length); 250*64eccac7SMatthias Ringwald init_script_offset = 0; 251*64eccac7SMatthias Ringwald send_download_command = 1; 252*64eccac7SMatthias Ringwald } 253*64eccac7SMatthias Ringwald 254faa6c1f6SMatthias Ringwald static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 255faa6c1f6SMatthias Ringwald // send download firmware command 256faa6c1f6SMatthias Ringwald if (send_download_command){ 257faa6c1f6SMatthias Ringwald send_download_command = 0; 258faa6c1f6SMatthias Ringwald hci_cmd_buffer[0] = 0x2e; 259faa6c1f6SMatthias Ringwald hci_cmd_buffer[1] = 0xfc; 260faa6c1f6SMatthias Ringwald hci_cmd_buffer[2] = 0x00; 261faa6c1f6SMatthias Ringwald return BTSTACK_CHIPSET_VALID_COMMAND; 262faa6c1f6SMatthias Ringwald } 263faa6c1f6SMatthias Ringwald 264faa6c1f6SMatthias Ringwald if (init_script_offset >= brcm_patch_ram_length) { 265faa6c1f6SMatthias Ringwald return BTSTACK_CHIPSET_DONE; 266faa6c1f6SMatthias Ringwald } 267faa6c1f6SMatthias Ringwald 268faa6c1f6SMatthias Ringwald int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2]; 269faa6c1f6SMatthias Ringwald memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len); 270faa6c1f6SMatthias Ringwald init_script_offset += cmd_len; 271faa6c1f6SMatthias Ringwald return BTSTACK_CHIPSET_VALID_COMMAND; 272faa6c1f6SMatthias Ringwald } 273*64eccac7SMatthias Ringwald #endif 274faa6c1f6SMatthias Ringwald 275faa6c1f6SMatthias Ringwald 276faa6c1f6SMatthias Ringwald static const btstack_chipset_t btstack_chipset_bcm = { 277faa6c1f6SMatthias Ringwald "BCM", 278faa6c1f6SMatthias Ringwald chipset_init, 279faa6c1f6SMatthias Ringwald chipset_next_command, 280faa6c1f6SMatthias Ringwald chipset_set_baudrate_command, 281faa6c1f6SMatthias Ringwald chipset_set_bd_addr_command, 282faa6c1f6SMatthias Ringwald }; 283faa6c1f6SMatthias Ringwald 284faa6c1f6SMatthias Ringwald // MARK: public API 285faa6c1f6SMatthias Ringwald const btstack_chipset_t * btstack_chipset_bcm_instance(void){ 286faa6c1f6SMatthias Ringwald return &btstack_chipset_bcm; 287faa6c1f6SMatthias Ringwald } 288