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 #define BTSTACK_FILE__ "btstack_chipset_bcm.c" 38 39 /* 40 * bt_control_bcm.c 41 * 42 * Adapter to use Broadcom-based chipsets with BTstack 43 */ 44 45 46 #include "btstack_config.h" 47 48 #include <stddef.h> /* NULL */ 49 #include <stdio.h> 50 #include <string.h> /* memcpy */ 51 52 #include "btstack_control.h" 53 #include "btstack_debug.h" 54 #include "btstack_chipset_bcm.h" 55 #include "hci.h" 56 57 #ifdef HAVE_POSIX_FILE_IO 58 #include "tinydir.h" 59 #include <ctype.h> 60 #endif 61 62 #ifdef _MSC_VER 63 // ignore deprecated warning for fopen 64 #pragma warning(disable : 4996) 65 #endif 66 67 // assert outgoing and incoming hci packet buffers can hold max hci command resp. event packet 68 #if HCI_OUTGOING_PACKET_BUFFER_SIZE < (HCI_CMD_HEADER_SIZE + 255) 69 #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." 70 #endif 71 #if HCI_INCOMING_PACKET_BUFFER_SIZE < (HCI_EVENT_HEADER_SIZE + 255) 72 #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." 73 #endif 74 75 static int send_download_command; 76 static uint32_t init_script_offset; 77 78 // Embedded == non posix systems 79 80 // actual init script provided by separate bt_firmware_image.c from WICED SDK 81 extern const uint8_t brcm_patchram_buf[]; 82 extern const int brcm_patch_ram_length; 83 extern const char brcm_patch_version[]; 84 85 // 86 // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c 87 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){ 88 hci_cmd_buffer[0] = 0x18; 89 hci_cmd_buffer[1] = 0xfc; 90 hci_cmd_buffer[2] = 0x06; 91 hci_cmd_buffer[3] = 0x00; 92 hci_cmd_buffer[4] = 0x00; 93 little_endian_store_32(hci_cmd_buffer, 5, baudrate); 94 } 95 96 // @note: bd addr has to be set after sending init script (it might just get re-set) 97 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){ 98 hci_cmd_buffer[0] = 0x01; 99 hci_cmd_buffer[1] = 0xfc; 100 hci_cmd_buffer[2] = 0x06; 101 reverse_bd_addr(addr, &hci_cmd_buffer[3]); 102 } 103 104 #ifdef HAVE_POSIX_FILE_IO 105 106 static const char * hcd_file_path; 107 static const char * hcd_folder_path = "."; 108 static FILE * hcd_file; 109 static char matched_file[1000]; 110 111 112 static void chipset_init(const void * config){ 113 if (hcd_file_path){ 114 log_info("chipset-bcm: init file %s", hcd_file_path); 115 } else { 116 log_info("chipset-bcm: init folder %s", hcd_folder_path); 117 } 118 send_download_command = 1; 119 init_script_offset = 0; 120 hcd_file = NULL; 121 } 122 123 static const uint8_t download_command[] = {0x2e, 0xfc, 0x00}; 124 125 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 126 if (hcd_file == NULL){ 127 log_info("chipset-bcm: open file %s", hcd_file_path); 128 hcd_file = fopen(hcd_file_path, "rb"); 129 if (hcd_file == NULL){ 130 log_error("chipset-bcm: can't open file %s", hcd_file_path); 131 return BTSTACK_CHIPSET_NO_INIT_SCRIPT; 132 } 133 } 134 135 // send download firmware command 136 if (send_download_command){ 137 send_download_command = 0; 138 memcpy(hci_cmd_buffer, download_command, sizeof(download_command)); 139 return BTSTACK_CHIPSET_VALID_COMMAND; 140 } 141 142 // read next command, but skip download command 143 do { 144 // read command 145 size_t bytes_read = fread(hci_cmd_buffer, 1, 3, hcd_file); 146 if (bytes_read < 3){ 147 if (feof(hcd_file)) { 148 log_info("chipset-bcm: end of file, size %u", init_script_offset); 149 } else { 150 log_error("chipset-bcm: read error at %u", init_script_offset); 151 } 152 fclose(hcd_file); 153 hcd_file = NULL; 154 return BTSTACK_CHIPSET_DONE; 155 } 156 init_script_offset += 3; 157 158 // read parameters 159 int param_len = hci_cmd_buffer[2]; 160 if (param_len){ 161 bytes_read = fread(&hci_cmd_buffer[3], 1, param_len, hcd_file); 162 } 163 if (bytes_read < param_len){ 164 log_error("chipset-bcm: read error at %u", init_script_offset); 165 fclose(hcd_file); 166 hcd_file = NULL; 167 return BTSTACK_CHIPSET_DONE; 168 } 169 init_script_offset += param_len; 170 171 } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 0); 172 return BTSTACK_CHIPSET_VALID_COMMAND; 173 } 174 175 void btstack_chipset_bcm_set_hcd_file_path(const char * path){ 176 hcd_file_path = path; 177 } 178 179 void btstack_chipset_bcm_set_hcd_folder_path(const char * path){ 180 hcd_folder_path = path; 181 } 182 183 static int equal_ignore_case(const char *str1, const char *str2){ 184 if (!str1 || !str2) return (1); 185 int i = 0; 186 while (true){ 187 if (!str1[i] && !str2[i]) return 1; 188 if (tolower(str1[i]) != tolower(str2[i])) return 0; 189 if (!str1[i] || !str2[i]) return 0; 190 i++; 191 } 192 } 193 194 // assumption starts with BCM or bcm 195 #define MAX_DEVICE_NAME_LEN 15 196 void btstack_chipset_bcm_set_device_name(const char * device_name){ 197 // ignore if file path already set 198 if (hcd_file_path) { 199 log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path); 200 return; 201 } 202 // construct filename for long variant 203 if (strlen(device_name) > MAX_DEVICE_NAME_LEN){ 204 log_error("chipset-bcm: device name %s too long", device_name); 205 return; 206 } 207 char filename_complete[MAX_DEVICE_NAME_LEN+5]; 208 strcpy(filename_complete, device_name); 209 strcat(filename_complete, ".hcd"); 210 211 // construct short variant without revision info 212 char filename_short[MAX_DEVICE_NAME_LEN+5]; 213 strcpy(filename_short, device_name); 214 uint16_t len = (uint16_t) strlen(filename_short); 215 while (len > 3){ 216 char c = filename_short[len-1]; 217 if (isdigit(c) == 0) break; 218 len--; 219 } 220 if (len > 3){ 221 filename_short[len-1] = 0; 222 } 223 strcat(filename_short, ".hcd"); 224 log_info("chipset-bcm: looking for %s and %s", filename_short, filename_complete); 225 226 // find in folder 227 tinydir_dir dir = { 0 }; 228 int res = tinydir_open(&dir, hcd_folder_path); 229 if (res < 0){ 230 log_error("chipset-bcm: could not get directory for %s", hcd_folder_path); 231 return; 232 } 233 234 int match_short = 0; 235 int match_complete = 0; 236 while (dir.has_next) { 237 tinydir_file file; 238 tinydir_readfile(&dir, &file); 239 tinydir_next(&dir); 240 if (equal_ignore_case(filename_complete, file.name)){ 241 match_complete = 1; 242 continue; 243 } 244 if (equal_ignore_case(filename_short, file.name)){ 245 match_short = 1; 246 } 247 } 248 tinydir_close(&dir); 249 if (match_complete){ 250 btstack_strcpy(matched_file, sizeof(matched_file), hcd_folder_path); 251 btstack_strcat(matched_file, sizeof(matched_file), "/"); 252 btstack_strcat(matched_file, sizeof(matched_file), filename_complete); 253 hcd_file_path = matched_file; 254 return; 255 } 256 if (match_short){ 257 btstack_strcpy(matched_file, sizeof(matched_file), hcd_folder_path); 258 btstack_strcat(matched_file, sizeof(matched_file), "/"); 259 btstack_strcat(matched_file, sizeof(matched_file), filename_short); 260 hcd_file_path = matched_file; 261 return; 262 } 263 log_error("chipset-bcm: could not find %s or %s, please provide .hcd file in %s", filename_complete, filename_short, hcd_folder_path); 264 } 265 266 #else 267 268 static void chipset_init(const void * config){ 269 log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length); 270 init_script_offset = 0; 271 send_download_command = 1; 272 } 273 274 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 275 // no initscript 276 if (brcm_patch_ram_length == 0) return BTSTACK_CHIPSET_NO_INIT_SCRIPT; 277 278 // send download firmware command 279 if (send_download_command){ 280 send_download_command = 0; 281 hci_cmd_buffer[0] = 0x2e; 282 hci_cmd_buffer[1] = 0xfc; 283 hci_cmd_buffer[2] = 0x00; 284 return BTSTACK_CHIPSET_VALID_COMMAND; 285 } 286 287 if (init_script_offset >= brcm_patch_ram_length) { 288 return BTSTACK_CHIPSET_DONE; 289 } 290 291 int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2]; 292 memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len); 293 init_script_offset += cmd_len; 294 return BTSTACK_CHIPSET_VALID_COMMAND; 295 } 296 #endif 297 298 299 static btstack_chipset_t btstack_chipset_bcm = { 300 "BCM", 301 chipset_init, 302 chipset_next_command, 303 chipset_set_baudrate_command, 304 chipset_set_bd_addr_command, 305 }; 306 307 // MARK: public API 308 const btstack_chipset_t * btstack_chipset_bcm_instance(void){ 309 return &btstack_chipset_bcm; 310 } 311 312 /** 313 * @brief Enable init file - needed by btstack_chipset_bcm_download_firmware when using h5 314 * @param enabled 315 */ 316 void btstack_chipset_bcm_enable_init_script(int enabled){ 317 if (enabled){ 318 btstack_chipset_bcm.next_command = &chipset_next_command; 319 } else { 320 btstack_chipset_bcm.next_command = NULL; 321 } 322 } 323