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