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