xref: /btstack/chipset/bcm/btstack_chipset_bcm.c (revision f9f2075ceac5e9dc08e9abea437e43d733a3a0ea)
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 /*
38  *  bt_control_bcm.c
39  *
40  *  Adapter to use Broadcom-based chipsets with BTstack
41  */
42 
43 
44 #include "btstack_config.h"
45 
46 #include <stddef.h>   /* NULL */
47 #include <stdio.h>
48 #include <string.h>   /* memcpy */
49 
50 #include "btstack_control.h"
51 #include "btstack_debug.h"
52 #include "btstack_chipset_bcm.h"
53 
54 #ifdef HAVE_POSIX_FILE_IO
55 #include <ctype.h>
56 #include <dirent.h>
57 #include <fcntl.h>
58 #include <unistd.h>
59 #endif
60 
61 #ifdef _WIN32
62 #include <Windows.h>
63 #endif
64 
65 static int send_download_command;
66 static uint32_t init_script_offset;
67 
68 // Embedded == non posix systems
69 
70 // actual init script provided by separate bt_firmware_image.c from WICED SDK
71 extern const uint8_t brcm_patchram_buf[];
72 extern const int     brcm_patch_ram_length;
73 extern const char    brcm_patch_version[];
74 
75 //
76 // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c
77 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
78     hci_cmd_buffer[0] = 0x18;
79     hci_cmd_buffer[1] = 0xfc;
80     hci_cmd_buffer[2] = 0x06;
81     hci_cmd_buffer[3] = 0x00;
82     hci_cmd_buffer[4] = 0x00;
83     little_endian_store_32(hci_cmd_buffer, 5, baudrate);
84 }
85 
86 // @note: bd addr has to be set after sending init script (it might just get re-set)
87 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){
88     hci_cmd_buffer[0] = 0x01;
89     hci_cmd_buffer[1] = 0xfc;
90     hci_cmd_buffer[2] = 0x06;
91     reverse_bd_addr(addr, &hci_cmd_buffer[3]);
92 }
93 
94 #ifdef HAVE_POSIX_FILE_IO
95 
96 static const char * hcd_file_path;
97 static const char * hcd_folder_path = ".";
98 static int hcd_fd;
99 static char matched_file[1000];
100 
101 
102 static void chipset_init(const void * config){
103     if (hcd_file_path){
104         log_info("chipset-bcm: init file %s", hcd_file_path);
105     } else {
106         log_info("chipset-bcm: init folder %s", hcd_folder_path);
107     }
108     send_download_command = 1;
109     init_script_offset = 0;
110     hcd_fd = -1;
111 }
112 
113 static const uint8_t download_command[] = {0x2e, 0xfc, 0x00};
114 
115 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
116     if (hcd_fd < 0){
117         log_info("chipset-bcm: open file %s", hcd_file_path);
118         hcd_fd = open(hcd_file_path, O_RDONLY);
119         if (hcd_fd < 0){
120             log_error("chipset-bcm: can't open file %s", hcd_file_path);
121             return BTSTACK_CHIPSET_DONE;
122         }
123     }
124 
125     // send download firmware command
126     if (send_download_command){
127         send_download_command = 0;
128         memcpy(hci_cmd_buffer, download_command, sizeof(download_command));
129         return BTSTACK_CHIPSET_VALID_COMMAND;
130     }
131 
132     // read next command, but skip download command
133     do {
134         // read command
135         int res = read(hcd_fd, hci_cmd_buffer, 3);
136         if (res == 0){
137             log_info("chipset-bcm: end of file, size %u", init_script_offset);
138             close(hcd_fd);
139 
140             // TODO: should not be needed anymore - fixed for embedded below and tested on RedBear Duo
141 
142             // wait for firmware patch to be applied - shorter delay possible
143 #ifdef _WIN32
144             Sleep(1000);
145 #else
146             sleep(1);
147 #endif
148             return BTSTACK_CHIPSET_DONE;
149         }
150         if (res < 0){
151             log_error("chipset-bcm: read error at %u", init_script_offset);
152             return BTSTACK_CHIPSET_DONE;
153         }
154         init_script_offset += 3;
155 
156         // read parameters
157         int param_len = hci_cmd_buffer[2];
158         if (param_len){
159             res = read(hcd_fd, &hci_cmd_buffer[3], param_len);
160         }
161         if (res < 0){
162             log_error("chipset-bcm: read error at %u", init_script_offset);
163             return BTSTACK_CHIPSET_DONE;
164         }
165         init_script_offset += param_len;
166 
167     } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 1);
168     return BTSTACK_CHIPSET_VALID_COMMAND;
169 }
170 
171 void btstack_chipset_bcm_set_hcd_file_path(const char * path){
172     hcd_file_path = path;
173 }
174 
175 void btstack_chipset_bcm_set_hcd_folder_path(const char * path){
176     hcd_folder_path = path;
177 }
178 
179 static int equal_ignore_case(const char *str1, const char *str2){
180     if (!str1 || !str2) return (1);
181     int i = 0;
182     while (1){
183         if (!str1[i] && !str2[i]) return 1;
184         if (tolower(str1[i]) != tolower(str2[i])) return 0;
185         if (!str1[i] || !str2[i]) return 0;
186         i++;
187     }
188 }
189 
190 // assumption starts with BCM or bcm
191 #define MAX_DEVICE_NAME_LEN 15
192 void btstack_chipset_bcm_set_device_name(const char * device_name){
193     // ignore if file path already set
194     if (hcd_file_path) {
195         log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path);
196         return;
197     }
198     // construct filename for long variant
199     if (strlen(device_name) > MAX_DEVICE_NAME_LEN){
200         log_error("chipset-bcm: device name %s too long", device_name);
201         return;
202     }
203     char filename_complete[MAX_DEVICE_NAME_LEN+5];
204     strcpy(filename_complete, device_name);
205     strcat(filename_complete, ".hcd");
206 
207     // construct short variant without revision info
208     char filename_short[MAX_DEVICE_NAME_LEN+5];
209     strcpy(filename_short, device_name);
210     int len = strlen(filename_short);
211     while (len > 3){
212         char c = filename_short[len-1];
213         if (isdigit(c) == 0) break;
214         len--;
215     }
216     if (len > 3){
217         filename_short[len-1] = 0;
218     }
219     strcat(filename_short, ".hcd");
220     log_info("chipset-bcm: looking for %s and %s", filename_short, filename_complete);
221 
222     // find in folder
223     DIR *dirp = opendir(hcd_folder_path);
224     int match_short = 0;
225     int match_complete = 0;
226     if (!dirp){
227         log_error("chipset-bcm: could not get directory for %s", hcd_folder_path);
228         return;
229     }
230     while (1){
231         struct dirent *dp = readdir(dirp);
232         if (!dp) break;
233         if (equal_ignore_case(filename_complete, dp->d_name)){
234             match_complete = 1;
235             continue;
236         }
237         if (equal_ignore_case(filename_short, dp->d_name)){
238             match_short = 1;
239         }
240     }
241     closedir(dirp);
242     if (match_complete){
243         sprintf(matched_file, "%s/%s", hcd_folder_path, filename_complete);
244         hcd_file_path = matched_file;
245         return;
246     }
247     if (match_short){
248         sprintf(matched_file, "%s/%s", hcd_folder_path, filename_short);
249         hcd_file_path = matched_file;
250         return;
251     }
252     log_error("chipset-bcm: could not find %s or %s, please provide .hcd file in %s", filename_complete, filename_short, hcd_folder_path);
253 }
254 
255 #else
256 
257 static void chipset_init(const void * config){
258     log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length);
259     init_script_offset = 0;
260     send_download_command = 1;
261 }
262 
263 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
264     // send download firmware command
265     if (send_download_command){
266         send_download_command = 0;
267         hci_cmd_buffer[0] = 0x2e;
268         hci_cmd_buffer[1] = 0xfc;
269         hci_cmd_buffer[2] = 0x00;
270         return BTSTACK_CHIPSET_VALID_COMMAND;
271     }
272 
273     if (init_script_offset >= brcm_patch_ram_length) {
274 
275         // It takes up to 2 ms for the BCM to raise its RTS line
276         // If we send the next command right away, the raise of the RTS will fall happen during
277         // it and causing the next command to fail (at least on RedBear Duo with manual CTS/RTS)
278         //
279         // -> Work around implemented in hci.c
280 
281         return BTSTACK_CHIPSET_DONE;
282     }
283 
284     int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2];
285     memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len);
286     init_script_offset += cmd_len;
287     return BTSTACK_CHIPSET_VALID_COMMAND;
288 }
289 #endif
290 
291 
292 static const btstack_chipset_t btstack_chipset_bcm = {
293     "BCM",
294     chipset_init,
295     chipset_next_command,
296     chipset_set_baudrate_command,
297     chipset_set_bd_addr_command,
298 };
299 
300 // MARK: public API
301 const btstack_chipset_t * btstack_chipset_bcm_instance(void){
302     return &btstack_chipset_bcm;
303 }
304