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 <ctype.h> 59 #include <dirent.h> 60 #include <fcntl.h> 61 #include <unistd.h> 62 #endif 63 64 #ifdef _WIN32 65 #include <Windows.h> 66 #endif 67 68 // assert outgoing and incoming hci packet buffers can hold max hci command resp. event packet 69 #if HCI_OUTGOING_PACKET_BUFFER_SIZE < (HCI_CMD_HEADER_SIZE + 255) 70 #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." 71 #endif 72 #if HCI_INCOMING_PACKET_BUFFER_SIZE < (HCI_EVENT_HEADER_SIZE + 255) 73 #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." 74 #endif 75 76 static int send_download_command; 77 static uint32_t init_script_offset; 78 79 // Embedded == non posix systems 80 81 // actual init script provided by separate bt_firmware_image.c from WICED SDK 82 extern const uint8_t brcm_patchram_buf[]; 83 extern const int brcm_patch_ram_length; 84 extern const char brcm_patch_version[]; 85 86 // 87 // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c 88 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){ 89 hci_cmd_buffer[0] = 0x18; 90 hci_cmd_buffer[1] = 0xfc; 91 hci_cmd_buffer[2] = 0x06; 92 hci_cmd_buffer[3] = 0x00; 93 hci_cmd_buffer[4] = 0x00; 94 little_endian_store_32(hci_cmd_buffer, 5, baudrate); 95 } 96 97 // @note: bd addr has to be set after sending init script (it might just get re-set) 98 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){ 99 hci_cmd_buffer[0] = 0x01; 100 hci_cmd_buffer[1] = 0xfc; 101 hci_cmd_buffer[2] = 0x06; 102 reverse_bd_addr(addr, &hci_cmd_buffer[3]); 103 } 104 105 #ifdef HAVE_POSIX_FILE_IO 106 107 static const char * hcd_file_path; 108 static const char * hcd_folder_path = "."; 109 static int hcd_fd; 110 static char matched_file[1000]; 111 112 113 static void chipset_init(const void * config){ 114 if (hcd_file_path){ 115 log_info("chipset-bcm: init file %s", hcd_file_path); 116 } else { 117 log_info("chipset-bcm: init folder %s", hcd_folder_path); 118 } 119 send_download_command = 1; 120 init_script_offset = 0; 121 hcd_fd = -1; 122 } 123 124 static const uint8_t download_command[] = {0x2e, 0xfc, 0x00}; 125 126 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 127 if (hcd_fd < 0){ 128 log_info("chipset-bcm: open file %s", hcd_file_path); 129 hcd_fd = open(hcd_file_path, O_RDONLY); 130 if (hcd_fd < 0){ 131 log_error("chipset-bcm: can't open file %s", hcd_file_path); 132 return BTSTACK_CHIPSET_NO_INIT_SCRIPT; 133 } 134 } 135 136 // send download firmware command 137 if (send_download_command){ 138 send_download_command = 0; 139 memcpy(hci_cmd_buffer, download_command, sizeof(download_command)); 140 return BTSTACK_CHIPSET_VALID_COMMAND; 141 } 142 143 // read next command, but skip download command 144 do { 145 // read command 146 int res = read(hcd_fd, hci_cmd_buffer, 3); 147 if (res == 0){ 148 log_info("chipset-bcm: end of file, size %u", init_script_offset); 149 close(hcd_fd); 150 return BTSTACK_CHIPSET_DONE; 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 += 3; 157 158 // read parameters 159 int param_len = hci_cmd_buffer[2]; 160 if (param_len){ 161 res = read(hcd_fd, &hci_cmd_buffer[3], param_len); 162 } 163 if (res < 0){ 164 log_error("chipset-bcm: read error at %u", init_script_offset); 165 return BTSTACK_CHIPSET_DONE; 166 } 167 init_script_offset += param_len; 168 169 } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 0); 170 return BTSTACK_CHIPSET_VALID_COMMAND; 171 } 172 173 void btstack_chipset_bcm_set_hcd_file_path(const char * path){ 174 hcd_file_path = path; 175 } 176 177 void btstack_chipset_bcm_set_hcd_folder_path(const char * path){ 178 hcd_folder_path = path; 179 } 180 181 static int equal_ignore_case(const char *str1, const char *str2){ 182 if (!str1 || !str2) return (1); 183 int i = 0; 184 while (true){ 185 if (!str1[i] && !str2[i]) return 1; 186 if (tolower(str1[i]) != tolower(str2[i])) return 0; 187 if (!str1[i] || !str2[i]) return 0; 188 i++; 189 } 190 } 191 192 // assumption starts with BCM or bcm 193 #define MAX_DEVICE_NAME_LEN 15 194 void btstack_chipset_bcm_set_device_name(const char * device_name){ 195 // ignore if file path already set 196 if (hcd_file_path) { 197 log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path); 198 return; 199 } 200 // construct filename for long variant 201 if (strlen(device_name) > MAX_DEVICE_NAME_LEN){ 202 log_error("chipset-bcm: device name %s too long", device_name); 203 return; 204 } 205 char filename_complete[MAX_DEVICE_NAME_LEN+5]; 206 strcpy(filename_complete, device_name); 207 strcat(filename_complete, ".hcd"); 208 209 // construct short variant without revision info 210 char filename_short[MAX_DEVICE_NAME_LEN+5]; 211 strcpy(filename_short, device_name); 212 int len = strlen(filename_short); 213 while (len > 3){ 214 char c = filename_short[len-1]; 215 if (isdigit(c) == 0) break; 216 len--; 217 } 218 if (len > 3){ 219 filename_short[len-1] = 0; 220 } 221 strcat(filename_short, ".hcd"); 222 log_info("chipset-bcm: looking for %s and %s", filename_short, filename_complete); 223 224 // find in folder 225 DIR *dirp = opendir(hcd_folder_path); 226 int match_short = 0; 227 int match_complete = 0; 228 if (!dirp){ 229 log_error("chipset-bcm: could not get directory for %s", hcd_folder_path); 230 return; 231 } 232 while (true){ 233 struct dirent *dp = readdir(dirp); 234 if (!dp) break; 235 if (equal_ignore_case(filename_complete, dp->d_name)){ 236 match_complete = 1; 237 continue; 238 } 239 if (equal_ignore_case(filename_short, dp->d_name)){ 240 match_short = 1; 241 } 242 } 243 closedir(dirp); 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