xref: /btstack/chipset/bcm/btstack_chipset_bcm.c (revision c8249f334b7fdc69ce713f0cc754723f04d6ca30)
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     if (hcd_file_path){
117         log_info("chipset-bcm: init file %s", hcd_file_path);
118     } else {
119         log_info("chipset-bcm: init folder %s", hcd_folder_path);
120     }
121     send_download_command = 1;
122     init_script_offset = 0;
123     hcd_file = NULL;
124 }
125 
126 static const uint8_t download_command[] = {0x2e, 0xfc, 0x00};
127 
128 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
129     if (hcd_file == NULL){
130         log_info("chipset-bcm: open file %s", hcd_file_path);
131         hcd_file = fopen(hcd_file_path, "rb");
132         if (hcd_file == NULL){
133             log_error("chipset-bcm: can't open file %s", hcd_file_path);
134             return BTSTACK_CHIPSET_NO_INIT_SCRIPT;
135         }
136     }
137 
138     // send download firmware command
139     if (send_download_command){
140         send_download_command = 0;
141         memcpy(hci_cmd_buffer, download_command, sizeof(download_command));
142         return BTSTACK_CHIPSET_VALID_COMMAND;
143     }
144 
145     // read next command, but skip download command
146     do {
147         // read command
148         size_t bytes_read = fread(hci_cmd_buffer, 1, 3, hcd_file);
149         if (bytes_read < 3){
150             if (feof(hcd_file)) {
151                 log_info("chipset-bcm: end of file, size %u", init_script_offset);
152             } else {
153                 log_error("chipset-bcm: read error at %u", init_script_offset);
154             }
155             fclose(hcd_file);
156             hcd_file = NULL;
157             return BTSTACK_CHIPSET_DONE;
158         }
159         init_script_offset += 3;
160 
161         // read parameters
162         int param_len = hci_cmd_buffer[2];
163         if (param_len){
164             bytes_read = fread(&hci_cmd_buffer[3], 1, param_len, hcd_file);
165         }
166         if (bytes_read < param_len){
167             log_error("chipset-bcm: read error at %u", init_script_offset);
168             fclose(hcd_file);
169             hcd_file = NULL;
170             return BTSTACK_CHIPSET_DONE;
171         }
172         init_script_offset += param_len;
173 
174     } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 0);
175     return BTSTACK_CHIPSET_VALID_COMMAND;
176 }
177 
178 void btstack_chipset_bcm_set_hcd_file_path(const char * path){
179     hcd_file_path = path;
180 }
181 
182 void btstack_chipset_bcm_set_hcd_folder_path(const char * path){
183     hcd_folder_path = path;
184 }
185 
186 void btstack_chipset_bcm_set_device_name(const char * device_name){
187     // ignore if file path already set
188     if (hcd_file_path) {
189         log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path);
190         return;
191     }
192 
193     // find in folder
194     tinydir_dir dir = {};
195     int res = tinydir_open(&dir, hcd_folder_path);
196     if (res < 0){
197         log_error("chipset-bcm: could not get directory for %s", hcd_folder_path);
198         return;
199     }
200     uint16_t device_name_len = (uint16_t) strlen(device_name);
201     while (dir.has_next) {
202         tinydir_file file;
203         tinydir_readfile(&dir, &file);
204         tinydir_next(&dir);
205         // starts with $device_name?
206         if (strncasecmp(device_name, file.name, device_name_len) != 0) {
207             continue;
208         }
209         // long enough to have ".hcd" suffix
210         uint16_t filename_len = (uint16_t) strlen(file.name);
211         if (filename_len < 5) {
212             continue;
213         }
214         // check suffix
215         if (strncasecmp(".hcd", &file.name[filename_len - 4], device_name_len) == 0) {
216             btstack_strcpy(matched_file, sizeof(matched_file), hcd_folder_path);
217             btstack_strcat(matched_file, sizeof(matched_file), "/");
218             btstack_strcat(matched_file, sizeof(matched_file), file.name);
219             hcd_file_path = matched_file;
220             break;
221         }
222     }
223     tinydir_close(&dir);
224     if (hcd_file_path == NULL) {
225         log_error("chipset-bcm: could not find .hcd that starts with %s at path %s", device_name, hcd_folder_path);
226     }
227 }
228 
229 #else
230 
231 static void chipset_init(const void * config){
232     log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length);
233     init_script_offset = 0;
234     send_download_command = 1;
235 }
236 
237 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
238     // no initscript
239     if (brcm_patch_ram_length == 0) return BTSTACK_CHIPSET_NO_INIT_SCRIPT;
240 
241     // send download firmware command
242     if (send_download_command){
243         send_download_command = 0;
244         hci_cmd_buffer[0] = 0x2e;
245         hci_cmd_buffer[1] = 0xfc;
246         hci_cmd_buffer[2] = 0x00;
247         return BTSTACK_CHIPSET_VALID_COMMAND;
248     }
249 
250     if (init_script_offset >= brcm_patch_ram_length) {
251         return BTSTACK_CHIPSET_DONE;
252     }
253 
254     int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2];
255     memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len);
256     init_script_offset += cmd_len;
257     return BTSTACK_CHIPSET_VALID_COMMAND;
258 }
259 #endif
260 
261 
262 static btstack_chipset_t btstack_chipset_bcm = {
263     "BCM",
264     chipset_init,
265     chipset_next_command,
266     chipset_set_baudrate_command,
267     chipset_set_bd_addr_command,
268 };
269 
270 // MARK: public API
271 const btstack_chipset_t * btstack_chipset_bcm_instance(void){
272     return &btstack_chipset_bcm;
273 }
274 
275 /**
276  * @brief Enable init file - needed by btstack_chipset_bcm_download_firmware when using h5
277  * @param enabled
278  */
279 void btstack_chipset_bcm_enable_init_script(int enabled){
280     if (enabled){
281         btstack_chipset_bcm.next_command = &chipset_next_command;
282     } else {
283         btstack_chipset_bcm.next_command = NULL;
284     }
285 }
286