xref: /btstack/chipset/bcm/btstack_chipset_bcm.c (revision f12924e0a61c0582b548a1e70cea1bd59ba21f1d)
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             // wait for firmware patch to be applied - shorter delay possible
141 #ifdef _WIN32
142             Sleep(1000);
143 #else
144             sleep(1);
145 #endif
146             return BTSTACK_CHIPSET_DONE;
147         }
148         if (res < 0){
149             log_error("chipset-bcm: read error at %u", init_script_offset);
150             return BTSTACK_CHIPSET_DONE;
151         }
152         init_script_offset += 3;
153 
154         // read parameters
155         int param_len = hci_cmd_buffer[2];
156         if (param_len){
157             res = read(hcd_fd, &hci_cmd_buffer[3], param_len);
158         }
159         if (res < 0){
160             log_error("chipset-bcm: read error at %u", init_script_offset);
161             return BTSTACK_CHIPSET_DONE;
162         }
163         init_script_offset += param_len;
164 
165     } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 1);
166     return BTSTACK_CHIPSET_VALID_COMMAND;
167 }
168 
169 void btstack_chipset_bcm_set_hcd_file_path(const char * path){
170     hcd_file_path = path;
171 }
172 
173 void btstack_chipset_bcm_set_hcd_folder_path(const char * path){
174     hcd_folder_path = path;
175 }
176 
177 static int equal_ignore_case(const char *str1, const char *str2){
178     if (!str1 || !str2) return (1);
179     int i = 0;
180     while (1){
181         if (!str1[i] && !str2[i]) return 1;
182         if (tolower(str1[i]) != tolower(str2[i])) return 0;
183         if (!str1[i] || !str2[i]) return 0;
184         i++;
185     }
186 }
187 
188 // assumption starts with BCM or bcm
189 #define MAX_DEVICE_NAME_LEN 15
190 void btstack_chipset_bcm_set_device_name(const char * device_name){
191     // ignore if file path already set
192     if (hcd_file_path) {
193         log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path);
194         return;
195     }
196     // construct filename for long variant
197     if (strlen(device_name) > MAX_DEVICE_NAME_LEN){
198         log_error("chipset-bcm: device name %s too long", device_name);
199         return;
200     }
201     char filename_complete[MAX_DEVICE_NAME_LEN+5];
202     strcpy(filename_complete, device_name);
203     strcat(filename_complete, ".hcd");
204 
205     // construct short variant without revision info
206     char filename_short[MAX_DEVICE_NAME_LEN+5];
207     strcpy(filename_short, device_name);
208     int len = strlen(filename_short);
209     while (len > 3){
210         char c = filename_short[len-1];
211         if (isdigit(c) == 0) break;
212         len--;
213     }
214     if (len > 3){
215         filename_short[len-1] = 0;
216     }
217     strcat(filename_short, ".hcd");
218     log_info("chipset-bcm: looking for %s and %s", filename_short, filename_complete);
219 
220     // find in folder
221     DIR *dirp = opendir(hcd_folder_path);
222     int match_short = 0;
223     int match_complete = 0;
224     if (!dirp){
225         log_error("chipset-bcm: could not get directory for %s", hcd_folder_path);
226         return;
227     }
228     while (1){
229         struct dirent *dp = readdir(dirp);
230         if (!dp) break;
231         if (equal_ignore_case(filename_complete, dp->d_name)){
232             match_complete = 1;
233             continue;
234         }
235         if (equal_ignore_case(filename_short, dp->d_name)){
236             match_short = 1;
237         }
238     }
239     closedir(dirp);
240     if (match_complete){
241         sprintf(matched_file, "%s/%s", hcd_folder_path, filename_complete);
242         hcd_file_path = matched_file;
243         return;
244     }
245     if (match_short){
246         sprintf(matched_file, "%s/%s", hcd_folder_path, filename_short);
247         hcd_file_path = matched_file;
248         return;
249     }
250     log_error("chipset-bcm: could not find %s or %s, please provide .hcd file in %s", filename_complete, filename_short, hcd_folder_path);
251 }
252 
253 #else
254 
255 static void chipset_init(const void * config){
256     log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length);
257     init_script_offset = 0;
258     send_download_command = 1;
259 }
260 
261 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
262     // send download firmware command
263     if (send_download_command){
264         send_download_command = 0;
265         hci_cmd_buffer[0] = 0x2e;
266         hci_cmd_buffer[1] = 0xfc;
267         hci_cmd_buffer[2] = 0x00;
268         return BTSTACK_CHIPSET_VALID_COMMAND;
269     }
270 
271     if (init_script_offset >= brcm_patch_ram_length) {
272         return BTSTACK_CHIPSET_DONE;
273     }
274 
275     int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2];
276     memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len);
277     init_script_offset += cmd_len;
278     return BTSTACK_CHIPSET_VALID_COMMAND;
279 }
280 #endif
281 
282 
283 static const btstack_chipset_t btstack_chipset_bcm = {
284     "BCM",
285     chipset_init,
286     chipset_next_command,
287     chipset_set_baudrate_command,
288     chipset_set_bd_addr_command,
289 };
290 
291 // MARK: public API
292 const btstack_chipset_t * btstack_chipset_bcm_instance(void){
293     return &btstack_chipset_bcm;
294 }
295