1*5036d965SVladimir Kondratyev /*- 2*5036d965SVladimir Kondratyev * SPDX-License-Identifier: BSD-2-Clause 3*5036d965SVladimir Kondratyev * 4*5036d965SVladimir Kondratyev * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org> 5*5036d965SVladimir Kondratyev * Copyright (c) 2023 Future Crew LLC. 6*5036d965SVladimir Kondratyev * 7*5036d965SVladimir Kondratyev * Redistribution and use in source and binary forms, with or without 8*5036d965SVladimir Kondratyev * modification, are permitted provided that the following conditions 9*5036d965SVladimir Kondratyev * are met: 10*5036d965SVladimir Kondratyev * 1. Redistributions of source code must retain the above copyright 11*5036d965SVladimir Kondratyev * notice, this list of conditions and the following disclaimer. 12*5036d965SVladimir Kondratyev * 2. Redistributions in binary form must reproduce the above copyright 13*5036d965SVladimir Kondratyev * notice, this list of conditions and the following disclaimer in the 14*5036d965SVladimir Kondratyev * documentation and/or other materials provided with the distribution. 15*5036d965SVladimir Kondratyev * 16*5036d965SVladimir Kondratyev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17*5036d965SVladimir Kondratyev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*5036d965SVladimir Kondratyev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*5036d965SVladimir Kondratyev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20*5036d965SVladimir Kondratyev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*5036d965SVladimir Kondratyev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*5036d965SVladimir Kondratyev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*5036d965SVladimir Kondratyev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*5036d965SVladimir Kondratyev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*5036d965SVladimir Kondratyev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*5036d965SVladimir Kondratyev * SUCH DAMAGE. 27*5036d965SVladimir Kondratyev */ 28*5036d965SVladimir Kondratyev #ifndef __RTLBT_HW_H__ 29*5036d965SVladimir Kondratyev #define __RTLBT_HW_H__ 30*5036d965SVladimir Kondratyev 31*5036d965SVladimir Kondratyev #include <netgraph/bluetooth/include/ng_hci.h> 32*5036d965SVladimir Kondratyev 33*5036d965SVladimir Kondratyev /* USB control request (HCI command) structure */ 34*5036d965SVladimir Kondratyev struct rtlbt_hci_cmd { 35*5036d965SVladimir Kondratyev uint16_t opcode; 36*5036d965SVladimir Kondratyev uint8_t length; 37*5036d965SVladimir Kondratyev uint8_t data[]; 38*5036d965SVladimir Kondratyev } __attribute__ ((packed)); 39*5036d965SVladimir Kondratyev 40*5036d965SVladimir Kondratyev #define RTLBT_HCI_CMD_SIZE(cmd) \ 41*5036d965SVladimir Kondratyev ((cmd)->length + offsetof(struct rtlbt_hci_cmd, data)) 42*5036d965SVladimir Kondratyev 43*5036d965SVladimir Kondratyev /* USB interrupt transfer HCI event header structure */ 44*5036d965SVladimir Kondratyev struct rtlbt_hci_evhdr { 45*5036d965SVladimir Kondratyev uint8_t event; 46*5036d965SVladimir Kondratyev uint8_t length; 47*5036d965SVladimir Kondratyev } __attribute__ ((packed)); 48*5036d965SVladimir Kondratyev 49*5036d965SVladimir Kondratyev /* USB interrupt transfer (generic HCI event) structure */ 50*5036d965SVladimir Kondratyev struct rtlbt_hci_event { 51*5036d965SVladimir Kondratyev struct rtlbt_hci_evhdr header; 52*5036d965SVladimir Kondratyev uint8_t data[]; 53*5036d965SVladimir Kondratyev } __attribute__ ((packed)); 54*5036d965SVladimir Kondratyev 55*5036d965SVladimir Kondratyev /* USB interrupt transfer (HCI command completion event) structure */ 56*5036d965SVladimir Kondratyev struct rtlbt_hci_event_cmd_compl { 57*5036d965SVladimir Kondratyev struct rtlbt_hci_evhdr header; 58*5036d965SVladimir Kondratyev uint8_t numpkt; 59*5036d965SVladimir Kondratyev uint16_t opcode; 60*5036d965SVladimir Kondratyev uint8_t data[]; 61*5036d965SVladimir Kondratyev } __attribute__ ((packed)); 62*5036d965SVladimir Kondratyev 63*5036d965SVladimir Kondratyev #define RTLBT_HCI_EVT_COMPL_SIZE(payload) \ 64*5036d965SVladimir Kondratyev (offsetof(struct rtlbt_hci_event_cmd_compl, data) + sizeof(payload)) 65*5036d965SVladimir Kondratyev 66*5036d965SVladimir Kondratyev #define RTLBT_CONTROL_ENDPOINT_ADDR 0x00 67*5036d965SVladimir Kondratyev #define RTLBT_INTERRUPT_ENDPOINT_ADDR 0x81 68*5036d965SVladimir Kondratyev 69*5036d965SVladimir Kondratyev #define RTLBT_HCI_MAX_CMD_SIZE 256 70*5036d965SVladimir Kondratyev #define RTLBT_HCI_MAX_EVENT_SIZE 16 71*5036d965SVladimir Kondratyev 72*5036d965SVladimir Kondratyev #define RTLBT_MSEC2TS(msec) \ 73*5036d965SVladimir Kondratyev (struct timespec) { \ 74*5036d965SVladimir Kondratyev .tv_sec = (msec) / 1000, \ 75*5036d965SVladimir Kondratyev .tv_nsec = ((msec) % 1000) * 1000000 \ 76*5036d965SVladimir Kondratyev }; 77*5036d965SVladimir Kondratyev #define RTLBT_TS2MSEC(ts) ((ts).tv_sec * 1000 + (ts).tv_nsec / 1000000) 78*5036d965SVladimir Kondratyev #define RTLBT_HCI_CMD_TIMEOUT 2000 /* ms */ 79*5036d965SVladimir Kondratyev #define RTLBT_LOADCMPL_TIMEOUT 5000 /* ms */ 80*5036d965SVladimir Kondratyev 81*5036d965SVladimir Kondratyev #define RTLBT_MAX_CMD_DATA_LEN 252 82*5036d965SVladimir Kondratyev 83*5036d965SVladimir Kondratyev struct rtlbt_rom_ver_rp { 84*5036d965SVladimir Kondratyev uint8_t status; 85*5036d965SVladimir Kondratyev uint8_t version; 86*5036d965SVladimir Kondratyev } __attribute__ ((packed)); 87*5036d965SVladimir Kondratyev 88*5036d965SVladimir Kondratyev struct rtlbt_hci_dl_cmd { 89*5036d965SVladimir Kondratyev uint8_t index; 90*5036d965SVladimir Kondratyev uint8_t data[RTLBT_MAX_CMD_DATA_LEN]; 91*5036d965SVladimir Kondratyev } __attribute__ ((packed)); 92*5036d965SVladimir Kondratyev 93*5036d965SVladimir Kondratyev struct rtlbt_hci_dl_rp { 94*5036d965SVladimir Kondratyev uint8_t status; 95*5036d965SVladimir Kondratyev uint8_t index; 96*5036d965SVladimir Kondratyev } __attribute__ ((packed)); 97*5036d965SVladimir Kondratyev 98*5036d965SVladimir Kondratyev int rtlbt_read_local_ver(struct libusb_device_handle *hdl, 99*5036d965SVladimir Kondratyev ng_hci_read_local_ver_rp *ver); 100*5036d965SVladimir Kondratyev int rtlbt_read_rom_ver(struct libusb_device_handle *hdl, uint8_t *ver); 101*5036d965SVladimir Kondratyev int rtlbt_load_fwfile(struct libusb_device_handle *hdl, 102*5036d965SVladimir Kondratyev const struct rtlbt_firmware *fw); 103*5036d965SVladimir Kondratyev 104*5036d965SVladimir Kondratyev #endif 105