xref: /freebsd/usr.sbin/bluetooth/rtlbtfw/rtlbt_fw.h (revision 3e72d0b40040f94c4b99ab9dfa0a0e1b62dff397)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 Adrian Chadd <adrian@freebsd.org>
5  * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
6  * Copyright (c) 2023 Future Crew LLC.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef	__RTLBT_FW_H__
31 #define	__RTLBT_FW_H__
32 
33 #include <sys/queue.h>
34 #include <sys/queue_mergesort.h>
35 
36 #define	RTLBT_ROM_LMP_8703B	0x8703
37 #define	RTLBT_ROM_LMP_8723A	0x1200
38 #define	RTLBT_ROM_LMP_8723B	0x8723
39 #define	RTLBT_ROM_LMP_8821A	0x8821
40 #define	RTLBT_ROM_LMP_8761A	0x8761
41 #define	RTLBT_ROM_LMP_8822B	0x8822
42 #define	RTLBT_ROM_LMP_8852A	0x8852
43 #define	RTLBT_ROM_LMP_8851B	0x8851
44 #define	RTLBT_ROM_LMP_8922A	0x8922
45 
46 #define RTLBT_PATCH_SNIPPETS		0x01
47 #define RTLBT_PATCH_DUMMY_HEADER	0x02
48 #define RTLBT_PATCH_SECURITY_HEADER	0x03
49 
50 enum rtlbt_fw_type {
51 	RTLBT_FW_TYPE_UNKNOWN,
52 	RTLBT_FW_TYPE_V1,
53 	RTLBT_FW_TYPE_V2,
54 };
55 
56 struct rtlbt_id_table {
57 	uint16_t lmp_subversion;
58 	uint16_t hci_revision;
59 	uint8_t hci_version;
60 	uint8_t flags;
61 #define	RTLBT_IC_FLAG_SIMPLE	(0 << 1)
62 #define	RTLBT_IC_FLAG_CONFIG	(1 << 1)
63 #define	RTLBT_IC_FLAG_MSFT	(2 << 1)
64 	const char *fw_name;
65 	const char *fw_suffix;
66 };
67 
68 struct rtlbt_firmware {
69 	char *fwname;
70 	size_t len;
71 	unsigned char *buf;
72 };
73 
74 SLIST_HEAD(rtlbt_patch_list, rtlbt_patch_entry);
75 
76 struct rtlbt_patch_entry {
77 	SLIST_ENTRY(rtlbt_patch_entry) next;
78 	uint32_t opcode;
79 	uint32_t len;
80 	uint8_t prio;
81 	uint8_t *data;
82 };
83 
84 struct rtlbt_iov {
85 	uint8_t *data;
86 	uint32_t len;
87 };
88 
89 struct rtlbt_fw_header_v1 {
90 	uint8_t signature[8];
91 	uint32_t fw_version;
92 	uint16_t num_patches;
93 } __attribute__ ((packed));
94 
95 struct rtlbt_fw_header_v2 {
96 	uint8_t signature[8];
97 	uint8_t fw_version[8];
98 	uint32_t num_sections;
99 } __attribute__ ((packed));
100 
101 struct rtlbt_section {
102 	uint32_t opcode;
103 	uint32_t len;
104 	uint8_t data[];
105 } __attribute__ ((packed));
106 
107 struct rtlbt_sec_hdr {
108 	uint16_t num;
109 	uint16_t reserved;
110 } __attribute__ ((packed));
111 
112 struct rtlbt_subsec_hdr {
113 	uint8_t eco;
114 	uint8_t prio;
115 	uint8_t cb[2];
116 	uint32_t len;
117 } __attribute__ ((packed));
118 
119 struct rtlbt_subsec_security_hdr {
120 	uint8_t eco;
121 	uint8_t prio;
122 	uint8_t key_id;
123 	uint8_t reserved;
124 	uint32_t len;
125 } __attribute__ ((packed));
126 
127 int rtlbt_fw_read(struct rtlbt_firmware *fw, const char *fwname);
128 void rtlbt_fw_free(struct rtlbt_firmware *fw);
129 char *rtlbt_get_fwname(const char *fw_name, const char *prefix,
130     const char *suffix);
131 const struct rtlbt_id_table *rtlbt_get_ic(uint16_t lmp_subversion,
132     uint16_t hci_revision, uint8_t hci_version);
133 enum rtlbt_fw_type rtlbt_get_fw_type(struct rtlbt_firmware *fw,
134     uint16_t *fw_lmp_subversion);
135 int rtlbt_parse_fwfile_v1(struct rtlbt_firmware *fw, uint8_t rom_version);
136 int rtlbt_parse_fwfile_v2(struct rtlbt_firmware *fw, uint8_t rom_version,
137     uint8_t reg_id);
138 int rtlbt_append_fwfile(struct rtlbt_firmware *fw, struct rtlbt_firmware *opt);
139 
140 #endif
141