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