1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 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 BLUEKITCHEN GMBH 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 34 * [email protected] 35 * 36 */ 37 38 /* 39 * btstack_chipset_csr.c 40 * 41 * Adapter to use CSR-based chipsets with BTstack 42 * SCO over HCI doesn't work over H4 connection and BTM805 module from Microchip Bluetooth Audio Developer Kit (CSR8811) 43 */ 44 45 #include "btstack_chipset_csr.h" 46 47 #include <stddef.h> /* NULL */ 48 #include <stdio.h> 49 #include <string.h> /* memcpy */ 50 51 #include "btstack_control.h" 52 #include "btstack_debug.h" 53 #include "btstack_util.h" 54 #include "hci_transport.h" 55 56 // minimal CSR init script to configure PSKEYs and activate them 57 static const uint8_t init_script[] = { 58 59 // Set ANA_Freq to 26MHz 60 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xfe, 0x01, 0x01, 0x00, 0x00, 0x00, 0x90, 0x65, 61 62 // Set HCI_NOP_DISABLE 63 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xf2, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 64 65 #ifdef HAVE_SCO_OVER_HCI 66 // Set HOSTIO_MAP_SCO_PCM to 0 67 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xab, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 68 // Set HOSTIO_MAP_SCO_CODEC to 0 69 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x03, 0x70, 0x00, 0x00, 0xb0, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 70 // Set ENABLE_SCO_STREAMS to 0 71 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xc9, 0x22, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 72 #endif 73 74 // Set UART baudrate to 115200 75 0x01, 0x00, 0xFC, 0x15, 0xc2, 0x02, 0x00, 0x0a, 0x00, 0x02, 0x00, 0x03, 0x70, 0x00, 0x00, 0xea, 0x01, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xc2, 76 77 // WarmReset 78 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x03, 0x0e, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 79 }; 80 static const uint16_t init_script_size = sizeof(init_script); 81 82 // 83 static uint32_t init_script_offset = 0; 84 static hci_transport_config_uart_t * hci_transport_config_uart = NULL; 85 86 static void chipset_init(const void * config){ 87 init_script_offset = 0; 88 hci_transport_config_uart = NULL; 89 // check for hci_transport_config_uart_t 90 if (!config) return; 91 if (((hci_transport_config_t*)config)->type != HCI_TRANSPORT_CONFIG_UART) return; 92 hci_transport_config_uart = (hci_transport_config_uart_t*) config; 93 } 94 95 // set requested baud rate 96 static void update_init_script_command(uint8_t *hci_cmd_buffer){ 97 uint16_t varid = little_endian_read_16(hci_cmd_buffer, 10); 98 if (varid != 0x7003) return; 99 uint16_t key = little_endian_read_16(hci_cmd_buffer, 14); 100 log_info("csr: pskey 0x%04x", key); 101 if (key != 0x01ea) return; 102 103 // check for baud rate 104 if (!hci_transport_config_uart) return; 105 uint32_t baudrate = hci_transport_config_uart->baudrate_main; 106 if (baudrate == 0){ 107 baudrate = hci_transport_config_uart->baudrate_init; 108 } 109 // uint32_t is stored as 2 x uint16_t with most important 16 bits first 110 little_endian_store_16(hci_cmd_buffer, 20, baudrate >> 16); 111 little_endian_store_16(hci_cmd_buffer, 22, baudrate & 0xffff); 112 } 113 114 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 115 116 if (init_script_offset >= init_script_size) { 117 return BTSTACK_CHIPSET_DONE; 118 } 119 120 // init script is stored with the HCI Command Packet Type 121 init_script_offset++; 122 // copy command header 123 memcpy(&hci_cmd_buffer[0], (uint8_t *) &init_script[init_script_offset], 3); 124 init_script_offset += 3; 125 int payload_len = hci_cmd_buffer[2]; 126 // copy command payload 127 memcpy(&hci_cmd_buffer[3], (uint8_t *) &init_script[init_script_offset], payload_len); 128 129 // support for on-the-fly configuration updates 130 update_init_script_command(hci_cmd_buffer); 131 132 init_script_offset += payload_len; 133 134 // support for warm boot command 135 uint16_t varid = little_endian_read_16(hci_cmd_buffer, 10); 136 if (varid == 0x4002){ 137 return BTSTACK_CHIPSET_WARMSTART_REQUIRED; 138 } 139 140 return BTSTACK_CHIPSET_VALID_COMMAND; 141 } 142 143 144 static const btstack_chipset_t btstack_chipset_bcm = { 145 "CSR", 146 chipset_init, 147 chipset_next_command, 148 NULL, // chipset_set_baudrate_command, 149 NULL, // chipset_set_bd_addr_command not supported or implemented 150 }; 151 152 // MARK: public API 153 const btstack_chipset_t * btstack_chipset_csr_instance(void){ 154 return &btstack_chipset_bcm; 155 } 156