1 /* 2 * Copyright (C) 2009-2012 by Matthias Ringwald 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at [email protected] 34 * 35 */ 36 37 /* 38 * bt_control_bcm.c 39 * 40 * Adapter to use Broadcom-based chipsets with BTstack 41 */ 42 43 44 #include "btstack_config.h" 45 46 #include <stddef.h> /* NULL */ 47 #include <stdio.h> 48 #include <string.h> /* memcpy */ 49 50 #include "btstack_control.h" 51 #include "btstack_debug.h" 52 #include "btstack_chipset_bcm.h" 53 54 #ifdef HAVE_POSIX_FILE_IO 55 #include <ctype.h> 56 #include <dirent.h> 57 #include <fcntl.h> 58 #include <unistd.h> 59 #endif 60 61 static int send_download_command; 62 static uint32_t init_script_offset; 63 64 // Embedded == non posix systems 65 66 // actual init script provided by separate bt_firmware_image.c from WICED SDK 67 extern const uint8_t brcm_patchram_buf[]; 68 extern const int brcm_patch_ram_length; 69 extern const char brcm_patch_version[]; 70 71 // 72 // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c 73 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){ 74 hci_cmd_buffer[0] = 0x18; 75 hci_cmd_buffer[1] = 0xfc; 76 hci_cmd_buffer[2] = 0x06; 77 hci_cmd_buffer[3] = 0x00; 78 hci_cmd_buffer[4] = 0x00; 79 little_endian_store_32(hci_cmd_buffer, 5, baudrate); 80 } 81 82 // @note: bd addr has to be set after sending init script (it might just get re-set) 83 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){ 84 hci_cmd_buffer[0] = 0x01; 85 hci_cmd_buffer[1] = 0xfc; 86 hci_cmd_buffer[2] = 0x06; 87 reverse_bd_addr(addr, &hci_cmd_buffer[3]); 88 } 89 90 #ifdef HAVE_POSIX_FILE_IO 91 92 static const char * hcd_file_path; 93 static const char * hcd_folder_path = "."; 94 static int hcd_fd; 95 static char matched_file[1000]; 96 97 98 static void chipset_init(const void * config){ 99 if (hcd_file_path){ 100 log_info("chipset-bcm: init file %s", hcd_file_path); 101 } else { 102 log_info("chipset-bcm: init folder %s", hcd_folder_path); 103 } 104 send_download_command = 1; 105 init_script_offset = 0; 106 hcd_fd = -1; 107 } 108 109 static const uint8_t download_command[] = {0x2e, 0xfc, 0x00}; 110 111 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 112 if (hcd_fd < 0){ 113 log_info("chipset-bcm: open file %s", hcd_file_path); 114 hcd_fd = open(hcd_file_path, O_RDONLY); 115 if (hcd_fd < 0){ 116 log_error("chipset-bcm: can't open file %s", hcd_file_path); 117 return BTSTACK_CHIPSET_DONE; 118 } 119 } 120 121 // send download firmware command 122 if (send_download_command){ 123 send_download_command = 0; 124 memcpy(hci_cmd_buffer, download_command, sizeof(download_command)); 125 return BTSTACK_CHIPSET_VALID_COMMAND; 126 } 127 128 // read next command, but skip download command 129 do { 130 // read command 131 int res = read(hcd_fd, hci_cmd_buffer, 3); 132 if (res == 0){ 133 log_info("chipset-bcm: end of file, size %u", init_script_offset); 134 close(hcd_fd); 135 136 // wait for firmware patch to be applied - shorter delay possible 137 sleep(1); 138 139 return BTSTACK_CHIPSET_DONE; 140 } 141 if (res < 0){ 142 log_error("chipset-bcm: read error at %u", init_script_offset); 143 return BTSTACK_CHIPSET_DONE; 144 } 145 init_script_offset += 3; 146 147 // read parameters 148 int param_len = hci_cmd_buffer[2]; 149 if (param_len){ 150 res = read(hcd_fd, &hci_cmd_buffer[3], param_len); 151 } 152 if (res < 0){ 153 log_error("chipset-bcm: read error at %u", init_script_offset); 154 return BTSTACK_CHIPSET_DONE; 155 } 156 init_script_offset += param_len; 157 158 } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 1); 159 return BTSTACK_CHIPSET_VALID_COMMAND; 160 } 161 162 void btstack_chipset_bcm_set_hcd_file_path(const char * path){ 163 hcd_file_path = path; 164 } 165 166 void btstack_chipset_bcm_set_hcd_folder_path(const char * path){ 167 hcd_folder_path = path; 168 } 169 170 static int equal_ignore_case(const char *str1, const char *str2){ 171 if (!str1 || !str2) return (1); 172 int i = 0; 173 while (1){ 174 if (!str1[i] && !str2[i]) return 1; 175 if (tolower(str1[i]) != tolower(str2[i])) return 0; 176 if (!str1[i] || !str2[i]) return 0; 177 i++; 178 } 179 } 180 181 // assumption starts with BCM or bcm 182 #define MAX_DEVICE_NAME_LEN 15 183 void btstack_chipset_bcm_set_device_name(const char * device_name){ 184 // ignore if file path already set 185 if (hcd_file_path) { 186 log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path); 187 return; 188 } 189 // construct filename for long variant 190 if (strlen(device_name) > MAX_DEVICE_NAME_LEN){ 191 log_error("chipset-bcm: device name %s too long", device_name); 192 return; 193 } 194 char filename_complete[MAX_DEVICE_NAME_LEN+5]; 195 strcpy(filename_complete, device_name); 196 strcat(filename_complete, ".hcd"); 197 198 // construct short variant without revision info 199 char filename_short[MAX_DEVICE_NAME_LEN+5]; 200 strcpy(filename_short, device_name); 201 int len = strlen(filename_short); 202 while (len > 3){ 203 char c = filename_short[len-1]; 204 if (isdigit(c) == 0) break; 205 len--; 206 } 207 if (len > 3){ 208 filename_short[len-1] = 0; 209 } 210 strcat(filename_short, ".hcd"); 211 log_info("chipset-bcm: looking for %s and %s", filename_short, filename_complete); 212 213 // find in folder 214 DIR *dirp = opendir(hcd_folder_path); 215 int match_short = 0; 216 int match_complete = 0; 217 if (!dirp){ 218 log_error("chipset-bcm: could not get directory for %s", hcd_folder_path); 219 return; 220 } 221 while (1){ 222 struct dirent *dp = readdir(dirp); 223 if (!dp) break; 224 if (equal_ignore_case(filename_complete, dp->d_name)){ 225 match_complete = 1; 226 continue; 227 } 228 if (equal_ignore_case(filename_short, dp->d_name)){ 229 match_short = 1; 230 } 231 } 232 closedir(dirp); 233 if (match_complete){ 234 sprintf(matched_file, "%s/%s", hcd_folder_path, filename_complete); 235 hcd_file_path = matched_file; 236 return; 237 } 238 if (match_short){ 239 sprintf(matched_file, "%s/%s", hcd_folder_path, filename_short); 240 hcd_file_path = matched_file; 241 return; 242 } 243 log_error("chipset-bcm: could not find %s or %s, please provide .hcd file in %s", filename_complete, filename_short, hcd_folder_path); 244 } 245 246 #else 247 248 static void chipset_init(const void * config){ 249 log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length); 250 init_script_offset = 0; 251 send_download_command = 1; 252 } 253 254 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 255 // send download firmware command 256 if (send_download_command){ 257 send_download_command = 0; 258 hci_cmd_buffer[0] = 0x2e; 259 hci_cmd_buffer[1] = 0xfc; 260 hci_cmd_buffer[2] = 0x00; 261 return BTSTACK_CHIPSET_VALID_COMMAND; 262 } 263 264 if (init_script_offset >= brcm_patch_ram_length) { 265 return BTSTACK_CHIPSET_DONE; 266 } 267 268 int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2]; 269 memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len); 270 init_script_offset += cmd_len; 271 return BTSTACK_CHIPSET_VALID_COMMAND; 272 } 273 #endif 274 275 276 static const btstack_chipset_t btstack_chipset_bcm = { 277 "BCM", 278 chipset_init, 279 chipset_next_command, 280 chipset_set_baudrate_command, 281 chipset_set_bd_addr_command, 282 }; 283 284 // MARK: public API 285 const btstack_chipset_t * btstack_chipset_bcm_instance(void){ 286 return &btstack_chipset_bcm; 287 } 288