1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2003-2009 Marcel Holtmann <marcel@holtmann.org> 6 * 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 * 22 */ 23 24 #include <termios.h> 25 #include <stdint.h> 26 #include <syslog.h> 27 28 #if __BYTE_ORDER == __LITTLE_ENDIAN 29 #define cpu_to_le16(d) (d) 30 #define cpu_to_le32(d) (d) 31 #define le16_to_cpu(d) (d) 32 #define le32_to_cpu(d) (d) 33 #elif __BYTE_ORDER == __BIG_ENDIAN 34 #define cpu_to_le16(d) bswap_16(d) 35 #define cpu_to_le32(d) bswap_32(d) 36 #define le16_to_cpu(d) bswap_16(d) 37 #define le32_to_cpu(d) bswap_32(d) 38 #else 39 #error "Unknown byte order" 40 #endif 41 42 #ifndef N_HCI 43 #define N_HCI 15 44 #endif 45 46 #define HCIUARTSETPROTO _IOW('U', 200, int) 47 #define HCIUARTGETPROTO _IOR('U', 201, int) 48 #define HCIUARTGETDEVICE _IOR('U', 202, int) 49 #define HCIUARTSETFLAGS _IOW('U', 203, int) 50 #define HCIUARTGETFLAGS _IOR('U', 204, int) 51 52 #define HCI_UART_H4 0 53 #define HCI_UART_BCSP 1 54 #define HCI_UART_3WIRE 2 55 #define HCI_UART_H4DS 3 56 #define HCI_UART_LL 4 57 #define HCI_UART_RAW_DEVICE 0 58 59 extern uint8_t DBG_ON; 60 61 /* #define SYSLOG */ 62 63 #define LOG_STR "Realtek Bluetooth" 64 #ifdef SYSLOG 65 #define RS_DBG(fmt, arg...) \ 66 do{ \ 67 if (DBG_ON) \ 68 syslog(LOG_DEBUG, "%s :" fmt "\n" , LOG_STR, ##arg); \ 69 }while(0) 70 71 #define RS_INFO(fmt, arg...) \ 72 do{ \ 73 syslog(LOG_INFO, "%s :" fmt "\n", LOG_STR, ##arg); \ 74 }while(0) 75 76 #define RS_WARN(fmt, arg...) \ 77 do{ \ 78 syslog(LOG_WARNING, "%s WARN: " fmt "\n", LOG_STR, ##arg); \ 79 }while(0) 80 81 #define RS_ERR(fmt, arg...) \ 82 do{ \ 83 syslog(LOG_ERR, "%s ERROR: " fmt "\n", LOG_STR, ##arg); \ 84 }while(0) 85 #else 86 #define RS_DBG(fmt, arg...) \ 87 do{ \ 88 if (DBG_ON) \ 89 printf("%s :" fmt "\n" , LOG_STR, ##arg); \ 90 }while(0) 91 92 #define RS_INFO(fmt, arg...) \ 93 do{ \ 94 printf("%s :" fmt "\n", LOG_STR, ##arg); \ 95 }while(0) 96 97 #define RS_WARN(fmt, arg...) \ 98 do{ \ 99 printf("%s WARN: " fmt "\n", LOG_STR, ##arg); \ 100 }while(0) 101 102 #define RS_ERR(fmt, arg...) \ 103 do{ \ 104 printf("%s ERROR: " fmt "\n", LOG_STR, ##arg); \ 105 }while(0) 106 #endif 107 108 typedef enum _H5_RX_STATE { 109 H5_W4_PKT_DELIMITER, 110 H5_W4_PKT_START, 111 H5_W4_HDR, 112 H5_W4_DATA, 113 H5_W4_CRC 114 } H5_RX_STATE; 115 116 typedef enum _H5_RX_ESC_STATE { 117 H5_ESCSTATE_NOESC, 118 H5_ESCSTATE_ESC 119 } H5_RX_ESC_STATE; 120 121 typedef enum _H5_LINK_STATE { 122 H5_SYNC, 123 H5_CONFIG, 124 H5_INIT, 125 H5_PATCH, 126 H5_HCI_RESET, 127 H5_ACTIVE 128 } H5_LINK_STATE; 129 130 struct patch_info; 131 typedef struct rtb_struct { 132 /* three wire releated */ 133 uint8_t rxseq_txack; /* expected rx seq number */ 134 uint8_t rxack; /* last packet that the peer ack'ed */ 135 uint8_t use_crc; 136 uint8_t is_txack_req; /* txack required */ 137 uint8_t msgq_txseq; /* next pkt seq */ 138 uint16_t message_crc; 139 uint32_t rx_count; /* expected pkts to recv */ 140 141 H5_RX_STATE rx_state; 142 H5_RX_ESC_STATE rx_esc_state; 143 H5_LINK_STATE link_estab_state; 144 145 struct sk_buff *rx_skb; 146 147 uint16_t num_of_cmd_sent; 148 uint16_t lmp_subver; 149 uint16_t hci_rev; 150 uint8_t hci_ver; 151 uint8_t eversion; 152 uint8_t chip_type; 153 uint8_t chip_ver; 154 155 uint32_t vendor_baud; 156 uint8_t dl_fw_flag; 157 int serial_fd; 158 uint32_t uart_flow_ctrl; 159 uint32_t parenb: 16; 160 uint32_t pareven: 16; 161 int final_speed; 162 int total_num; /* total pkt number */ 163 int tx_index; /* current sending pkt number */ 164 int rx_index; /* ack index from board */ 165 int fw_len; /* fw patch file len */ 166 int config_len; /* config patch file len */ 167 int total_len; /* fw & config extracted buf len */ 168 uint8_t *fw_buf; /* fw patch file buf */ 169 uint8_t *config_buf; /* config patch file buf */ 170 uint8_t *total_buf; /* fw & config extracted buf */ 171 #define CMD_STATE_UNKNOWN 0x00 172 #define CMD_STATE_SUCCESS 0x01 173 struct __cmd_state { 174 uint16_t opcode; 175 uint16_t state; 176 } cmd_state; 177 178 struct patch_info *patch_ent; 179 180 int proto; 181 int timerfd; 182 int epollfd; 183 } rtb_struct_t; 184 extern struct rtb_struct rtb_cfg; 185 int timeout_set(int fd, unsigned int msec); 186 int set_speed(int fd, struct termios *ti, int speed); 187 int rtb_init(int fd, int proto, int speed, struct termios *ti); 188 int rtb_post(int fd, int proto, struct termios *ti); 189 void util_hexdump(const uint8_t *buf, size_t len); 190