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