xref: /btstack/chipset/bcm/btstack_chipset_bcm.c (revision ced70f9bfeafe291ec597a3a9cc862e39e0da3ce)
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 #endif
66 
67 // assert outgoing and incoming hci packet buffers can hold max hci command resp. event packet
68 #if HCI_OUTGOING_PACKET_BUFFER_SIZE < (HCI_CMD_HEADER_SIZE + 255)
69 #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."
70 #endif
71 #if HCI_INCOMING_PACKET_BUFFER_SIZE < (HCI_EVENT_HEADER_SIZE + 255)
72 #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."
73 #endif
74 
75 static int send_download_command;
76 static uint32_t init_script_offset;
77 
78 // Embedded == non posix systems
79 
80 // actual init script provided by separate bt_firmware_image.c from WICED SDK
81 extern const uint8_t brcm_patchram_buf[];
82 extern const int     brcm_patch_ram_length;
83 extern const char    brcm_patch_version[];
84 
85 //
86 // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c
87 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
88     hci_cmd_buffer[0] = 0x18;
89     hci_cmd_buffer[1] = 0xfc;
90     hci_cmd_buffer[2] = 0x06;
91     hci_cmd_buffer[3] = 0x00;
92     hci_cmd_buffer[4] = 0x00;
93     little_endian_store_32(hci_cmd_buffer, 5, baudrate);
94 }
95 
96 // @note: bd addr has to be set after sending init script (it might just get re-set)
97 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){
98     hci_cmd_buffer[0] = 0x01;
99     hci_cmd_buffer[1] = 0xfc;
100     hci_cmd_buffer[2] = 0x06;
101     reverse_bd_addr(addr, &hci_cmd_buffer[3]);
102 }
103 
104 #ifdef HAVE_POSIX_FILE_IO
105 
106 static const char * hcd_file_path;
107 static const char * hcd_folder_path = ".";
108 static FILE * hcd_file;
109 static char matched_file[1000];
110 
111 
112 static void chipset_init(const void * config){
113     if (hcd_file_path){
114         log_info("chipset-bcm: init file %s", hcd_file_path);
115     } else {
116         log_info("chipset-bcm: init folder %s", hcd_folder_path);
117     }
118     send_download_command = 1;
119     init_script_offset = 0;
120     hcd_file = NULL;
121 }
122 
123 static const uint8_t download_command[] = {0x2e, 0xfc, 0x00};
124 
125 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
126     if (hcd_file == NULL){
127         log_info("chipset-bcm: open file %s", hcd_file_path);
128         hcd_file = fopen(hcd_file_path, "rb");
129         if (hcd_file == NULL){
130             log_error("chipset-bcm: can't open file %s", hcd_file_path);
131             return BTSTACK_CHIPSET_NO_INIT_SCRIPT;
132         }
133     }
134 
135     // send download firmware command
136     if (send_download_command){
137         send_download_command = 0;
138         memcpy(hci_cmd_buffer, download_command, sizeof(download_command));
139         return BTSTACK_CHIPSET_VALID_COMMAND;
140     }
141 
142     // read next command, but skip download command
143     do {
144         // read command
145         size_t bytes_read = fread(hci_cmd_buffer, 1, 3, hcd_file);
146         if (bytes_read < 3){
147             if (feof(hcd_file)) {
148                 log_info("chipset-bcm: end of file, size %u", init_script_offset);
149             } else {
150                 log_error("chipset-bcm: read error at %u", init_script_offset);
151             }
152             fclose(hcd_file);
153             hcd_file = NULL;
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             bytes_read = fread(&hci_cmd_buffer[3], 1, param_len, hcd_file);
162         }
163         if (bytes_read < param_len){
164             log_error("chipset-bcm: read error at %u", init_script_offset);
165             fclose(hcd_file);
166             hcd_file = NULL;
167             return BTSTACK_CHIPSET_DONE;
168         }
169         init_script_offset += param_len;
170 
171     } while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 0);
172     return BTSTACK_CHIPSET_VALID_COMMAND;
173 }
174 
175 void btstack_chipset_bcm_set_hcd_file_path(const char * path){
176     hcd_file_path = path;
177 }
178 
179 void btstack_chipset_bcm_set_hcd_folder_path(const char * path){
180     hcd_folder_path = path;
181 }
182 
183 void btstack_chipset_bcm_set_device_name(const char * device_name){
184     // ignore if file path already set
185     if (hcd_file_path) {
186         log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path);
187         return;
188     }
189 
190     // find in folder
191     tinydir_dir dir = { 0 };
192     int res = tinydir_open(&dir, hcd_folder_path);
193     if (res < 0){
194         log_error("chipset-bcm: could not get directory for %s", hcd_folder_path);
195         return;
196     }
197     uint16_t device_name_len = (uint16_t) strlen(device_name);
198     while (dir.has_next) {
199         tinydir_file file;
200         tinydir_readfile(&dir, &file);
201         tinydir_next(&dir);
202         // starts with $device_name?
203         if (strncasecmp(device_name, file.name, device_name_len) != 0) {
204             continue;
205         }
206         // long enough to have ".hcd" suffix
207         uint16_t filename_len = (uint16_t) strlen(file.name);
208         if (filename_len < 5) {
209             continue;
210         }
211         // check suffix
212         if (strncasecmp(".hcd", &file.name[filename_len - 4], device_name_len) == 0) {
213             btstack_strcpy(matched_file, sizeof(matched_file), hcd_folder_path);
214             btstack_strcat(matched_file, sizeof(matched_file), "/");
215             btstack_strcat(matched_file, sizeof(matched_file), file.name);
216             hcd_file_path = matched_file;
217             break;
218         }
219     }
220     tinydir_close(&dir);
221     if (hcd_file_path == NULL) {
222         log_error("chipset-bcm: could not find .hcd that starts with %s at path %s", device_name, hcd_folder_path);
223     }
224 }
225 
226 #else
227 
228 static void chipset_init(const void * config){
229     log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length);
230     init_script_offset = 0;
231     send_download_command = 1;
232 }
233 
234 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
235     // no initscript
236     if (brcm_patch_ram_length == 0) return BTSTACK_CHIPSET_NO_INIT_SCRIPT;
237 
238     // send download firmware command
239     if (send_download_command){
240         send_download_command = 0;
241         hci_cmd_buffer[0] = 0x2e;
242         hci_cmd_buffer[1] = 0xfc;
243         hci_cmd_buffer[2] = 0x00;
244         return BTSTACK_CHIPSET_VALID_COMMAND;
245     }
246 
247     if (init_script_offset >= brcm_patch_ram_length) {
248         return BTSTACK_CHIPSET_DONE;
249     }
250 
251     int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2];
252     memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len);
253     init_script_offset += cmd_len;
254     return BTSTACK_CHIPSET_VALID_COMMAND;
255 }
256 #endif
257 
258 
259 static btstack_chipset_t btstack_chipset_bcm = {
260     "BCM",
261     chipset_init,
262     chipset_next_command,
263     chipset_set_baudrate_command,
264     chipset_set_bd_addr_command,
265 };
266 
267 // MARK: public API
268 const btstack_chipset_t * btstack_chipset_bcm_instance(void){
269     return &btstack_chipset_bcm;
270 }
271 
272 /**
273  * @brief Enable init file - needed by btstack_chipset_bcm_download_firmware when using h5
274  * @param enabled
275  */
276 void btstack_chipset_bcm_enable_init_script(int enabled){
277     if (enabled){
278         btstack_chipset_bcm.next_command = &chipset_next_command;
279     } else {
280         btstack_chipset_bcm.next_command = NULL;
281     }
282 }
283