xref: /btstack/chipset/bcm/btstack_chipset_bcm.c (revision 0561b2d8d5dba972c7daa57d5e677f7a1327edfd)
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 
56 #ifdef HAVE_POSIX_FILE_IO
57 #include <ctype.h>
58 #include <dirent.h>
59 #include <fcntl.h>
60 #include <unistd.h>
61 #endif
62 
63 #ifdef _WIN32
64 #include <Windows.h>
65 #endif
66 
67 static int send_download_command;
68 static uint32_t init_script_offset;
69 
70 // Embedded == non posix systems
71 
72 // actual init script provided by separate bt_firmware_image.c from WICED SDK
73 extern const uint8_t brcm_patchram_buf[];
74 extern const int     brcm_patch_ram_length;
75 extern const char    brcm_patch_version[];
76 
77 //
78 // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c
79 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
80     hci_cmd_buffer[0] = 0x18;
81     hci_cmd_buffer[1] = 0xfc;
82     hci_cmd_buffer[2] = 0x06;
83     hci_cmd_buffer[3] = 0x00;
84     hci_cmd_buffer[4] = 0x00;
85     little_endian_store_32(hci_cmd_buffer, 5, baudrate);
86 }
87 
88 // @note: bd addr has to be set after sending init script (it might just get re-set)
89 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){
90     hci_cmd_buffer[0] = 0x01;
91     hci_cmd_buffer[1] = 0xfc;
92     hci_cmd_buffer[2] = 0x06;
93     reverse_bd_addr(addr, &hci_cmd_buffer[3]);
94 }
95 
96 #ifdef HAVE_POSIX_FILE_IO
97 
98 static const char * hcd_file_path;
99 static const char * hcd_folder_path = ".";
100 static int hcd_fd;
101 static char matched_file[1000];
102 
103 
104 static void chipset_init(const void * config){
105     if (hcd_file_path){
106         log_info("chipset-bcm: init file %s", hcd_file_path);
107     } else {
108         log_info("chipset-bcm: init folder %s", hcd_folder_path);
109     }
110     send_download_command = 1;
111     init_script_offset = 0;
112     hcd_fd = -1;
113 }
114 
115 static const uint8_t download_command[] = {0x2e, 0xfc, 0x00};
116 
117 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
118     if (hcd_fd < 0){
119         log_info("chipset-bcm: open file %s", hcd_file_path);
120         hcd_fd = open(hcd_file_path, O_RDONLY);
121         if (hcd_fd < 0){
122             log_error("chipset-bcm: can't open file %s", hcd_file_path);
123             return BTSTACK_CHIPSET_NO_INIT_SCRIPT;
124         }
125     }
126 
127     // send download firmware command
128     if (send_download_command){
129         send_download_command = 0;
130         memcpy(hci_cmd_buffer, download_command, sizeof(download_command));
131         return BTSTACK_CHIPSET_VALID_COMMAND;
132     }
133 
134     // read next command, but skip download command
135     do {
136         // read command
137         int res = read(hcd_fd, hci_cmd_buffer, 3);
138         if (res == 0){
139             log_info("chipset-bcm: end of file, size %u", init_script_offset);
140             close(hcd_fd);
141 
142             // TODO: should not be needed anymore - fixed for embedded below and tested on RedBear Duo
143 
144             // wait for firmware patch to be applied - shorter delay possible
145 #ifdef _WIN32
146             Sleep(1000);
147 #else
148             sleep(1);
149 #endif
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 
280         // It takes up to 2 ms for the BCM to raise its RTS line
281         // If we send the next command right away, the raise of the RTS will fall happen during
282         // it and causing the next command to fail (at least on RedBear Duo with manual CTS/RTS)
283         //
284         // -> Work around implemented in hci.c
285 
286         return BTSTACK_CHIPSET_DONE;
287     }
288 
289     int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2];
290     memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len);
291     init_script_offset += cmd_len;
292     return BTSTACK_CHIPSET_VALID_COMMAND;
293 }
294 #endif
295 
296 
297 static btstack_chipset_t btstack_chipset_bcm = {
298     "BCM",
299     chipset_init,
300     chipset_next_command,
301     chipset_set_baudrate_command,
302     chipset_set_bd_addr_command,
303 };
304 
305 // MARK: public API
306 const btstack_chipset_t * btstack_chipset_bcm_instance(void){
307     return &btstack_chipset_bcm;
308 }
309 
310 /**
311  * @brief Enable init file - needed by btstack_chipset_bcm_download_firmware when using h5
312  * @param enabled
313  */
314 void btstack_chipset_bcm_enable_init_script(int enabled){
315     if (enabled){
316         btstack_chipset_bcm.next_command = &chipset_next_command;
317     } else {
318         btstack_chipset_bcm.next_command = NULL;
319     }
320 }
321