carl9170 firmware: integrate firmware descriptors
[carl9170fw.git] / include / shared / fwdesc.h
1 /*
2  * Firmware Descriptor definitions
3  *
4  * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; see the file COPYING.  If not, see
17  * http://www.gnu.org/licenses/.
18  */
19
20 #ifndef __CARL9170_SHARED_FWDESC_H
21 #define __CARL9170_SHARED_FWDESC_H
22
23 /* NOTE: Don't mess with the order of the flags! */
24 enum carl9170fw_feature_list {
25         /* Always set */
26         CARL9170FW_DUMMY_FEATURE,
27
28         /*
29          * Indicates that this image has special boot block which prevents
30          * legacy drivers to drive the firmware.
31          */
32         CARL9170FW_MINIBOOT,
33
34         /* usb registers are initialized by the firmware */
35         CARL9170FW_USB_INIT_FIRMWARE,
36
37         /* command traps & notifications are send through EP2 */
38         CARL9170FW_USB_RESP_EP2,
39
40         /* usb download (app -> fw) stream */
41         CARL9170FW_USB_DOWN_STREAM,
42
43         /* usb upload (fw -> app) stream */
44         CARL9170FW_USB_UP_STREAM,
45
46         /* USB Watchdog */
47         CARL9170FW_USB_WATCHDOG,
48
49         /* unusable - reserved to flag non-functional debug firmwares */
50         CARL9170FW_UNUSABLE,
51
52         /* AR9170_CMD_RF_INIT, AR9170_CMD_FREQ_START, AR9170_CMD_FREQUENCY */
53         CARL9170FW_COMMAND_PHY,
54
55         /* AR9170_CMD_EKEY, AR9170_CMD_DKEY */
56         CARL9170FW_COMMAND_CAM,
57
58         /* Firmware has a software Content After Beacon Queueing mechanism */
59         CARL9170FW_WLANTX_CAB,
60
61         /* The firmware is capable of responding to incoming BAR frames */
62         CARL9170FW_HANDLE_BACK_REQ,
63
64         /* GPIO Interrupt | CARL9170_RSP_GPIO */
65         CARL9170FW_GPIO_INTERRUPT,
66
67         /* Firmware PSM support | CARL9170_CMD_PSM */
68         CARL9170FW_PSM,
69
70         /* KEEP LAST */
71         __CARL9170FW_FEATURE_NUM
72 };
73
74 #define OTUS_MAGIC      "OTAR"
75 #define MOTD_MAGIC      "MOTD"
76 #define FIX_MAGIC       "FIX\0"
77 #define DBG_MAGIC       "DBG\0"
78 #define CHK_MAGIC       "CHK\0"
79 #define LAST_MAGIC      "LAST"
80
81 #define CARL9170FW_SET_DAY(d) (((d) - 1) % 31)
82 #define CARL9170FW_SET_MONTH(m) ((((m) - 1) % 12) * 31)
83 #define CARL9170FW_SET_YEAR(y) (((y) - 10) * 372)
84
85 #define CARL9170FW_GET_DAY(d) (((d) % 31) + 1)
86 #define CARL9170FW_GET_MONTH(m) ((((m) / 31) % 12) + 1)
87 #define CARL9170FW_GET_YEAR(y) ((y) / 372 + 10)
88
89 struct carl9170fw_desc_head {
90         u8      magic[4];
91         __le16 length;
92         u8 min_ver;
93         u8 cur_ver;
94 } __packed;
95 #define CARL9170FW_DESC_HEAD_SIZE                       \
96         (sizeof(struct carl9170fw_desc_head))
97
98 #define CARL9170FW_OTUS_DESC_MIN_VER            3
99 #define CARL9170FW_OTUS_DESC_CUR_VER            3
100 struct carl9170fw_otus_desc {
101         struct carl9170fw_desc_head head;
102         __le32 feature_set;
103         __le32 fw_address;
104         __le32 bcn_addr;
105         __le16 bcn_len;
106         __le16 miniboot_size;
107         __le16 tx_frag_len;
108         __le16 rx_max_frame_len;
109         u8 tx_descs;
110         u8 cmd_bufs;
111         u8 api_ver;
112         u8 vif_num;
113 } __packed;
114 #define CARL9170FW_OTUS_DESC_SIZE                       \
115         (sizeof(struct carl9170fw_otus_desc))
116
117 #define CARL9170FW_MOTD_STRING_LEN                      24
118 #define CARL9170FW_MOTD_RELEASE_LEN                     20
119 #define CARL9170FW_MOTD_DESC_MIN_VER                    1
120 #define CARL9170FW_MOTD_DESC_CUR_VER                    2
121 struct carl9170fw_motd_desc {
122         struct carl9170fw_desc_head head;
123         __le32 fw_year_month_day;
124         char desc[CARL9170FW_MOTD_STRING_LEN];
125         char release[CARL9170FW_MOTD_RELEASE_LEN];
126 } __packed;
127 #define CARL9170FW_MOTD_DESC_SIZE                       \
128         (sizeof(struct carl9170fw_motd_desc))
129
130 #define CARL9170FW_FIX_DESC_MIN_VER                     1
131 #define CARL9170FW_FIX_DESC_CUR_VER                     2
132 struct carl9170fw_fix_entry {
133         __le32 address;
134         __le32 mask;
135         __le32 value;
136 } __packed;
137
138 struct carl9170fw_fix_desc {
139         struct carl9170fw_desc_head head;
140         struct carl9170fw_fix_entry data[0];
141 } __packed;
142 #define CARL9170FW_FIX_DESC_SIZE                        \
143         (sizeof(struct carl9170fw_fix_desc))
144
145 #define CARL9170FW_DBG_DESC_MIN_VER                     1
146 #define CARL9170FW_DBG_DESC_CUR_VER                     2
147 struct carl9170fw_dbg_desc {
148         struct carl9170fw_desc_head head;
149
150         __le32 bogoclock_addr;
151         __le32 counter_addr;
152         __le32 rx_total_addr;
153         __le32 rx_overrun_addr;
154
155         /* Put your debugging definitions here */
156 } __packed;
157 #define CARL9170FW_DBG_DESC_SIZE                        \
158         (sizeof(struct carl9170fw_dbg_desc))
159
160 #define CARL9170FW_CHK_DESC_MIN_VER                     1
161 #define CARL9170FW_CHK_DESC_CUR_VER                     2
162 struct carl9170fw_chk_desc {
163         struct carl9170fw_desc_head head;
164         __le32 fw_crc32;
165         __le32 hdr_crc32;
166 } __packed;
167 #define CARL9170FW_CHK_DESC_SIZE                        \
168         (sizeof(struct carl9170fw_chk_desc))
169
170 #define CARL9170FW_LAST_DESC_MIN_VER                    1
171 #define CARL9170FW_LAST_DESC_CUR_VER                    2
172 struct carl9170fw_last_desc {
173         struct carl9170fw_desc_head head;
174 } __packed;
175 #define CARL9170FW_LAST_DESC_SIZE                       \
176         (sizeof(struct carl9170fw_fix_desc))
177
178 #define CARL9170FW_DESC_MAX_LENGTH                      8192
179
180 #define CARL9170FW_FILL_DESC(_magic, _length, _min_ver, _cur_ver)       \
181         .head = {                                                       \
182                 .magic = _magic,                                        \
183                 .length = cpu_to_le16(_length),                         \
184                 .min_ver = _min_ver,                                    \
185                 .cur_ver = _cur_ver,                                    \
186         }
187
188 static inline void carl9170fw_fill_desc(struct carl9170fw_desc_head *head,
189                                          u8 magic[4], __le16 length,
190                                          u8 min_ver, u8 cur_ver)
191 {
192         head->magic[0] = magic[0];
193         head->magic[1] = magic[1];
194         head->magic[2] = magic[2];
195         head->magic[3] = magic[3];
196
197         head->length = length;
198         head->min_ver = min_ver;
199         head->cur_ver = cur_ver;
200 }
201
202 #define carl9170fw_for_each_hdr(desc, fw_desc)                          \
203         for (desc = fw_desc;                                            \
204              memcmp(desc->magic, LAST_MAGIC, 4) &&                      \
205              le16_to_cpu(desc->length) >= CARL9170FW_DESC_HEAD_SIZE &&  \
206              le16_to_cpu(desc->length) < CARL9170FW_DESC_MAX_LENGTH;    \
207              desc = (void *)((unsigned long)desc + le16_to_cpu(desc->length)))
208
209 #define CHECK_HDR_VERSION(head, _min_ver)                               \
210         (((head)->cur_ver < _min_ver) || ((head)->min_ver > _min_ver))  \
211
212 static inline bool carl9170fw_supports(__le32 list, u8 feature)
213 {
214         return le32_to_cpu(list) & BIT(feature);
215 }
216
217 static inline bool carl9170fw_desc_cmp(const struct carl9170fw_desc_head *head,
218                                        const u8 descid[4], u16 min_len,
219                                        u8 compatible_revision)
220 {
221         if (descid[0] == head->magic[0] && descid[1] == head->magic[1] &&
222             descid[2] == head->magic[2] && descid[3] == head->magic[3] &&
223             !CHECK_HDR_VERSION(head, compatible_revision) &&
224             (le16_to_cpu(head->length) >= min_len))
225                 return true;
226
227         return false;
228 }
229
230 #define CARL9170FW_MIN_SIZE     32
231 #define CARL9170FW_MAX_SIZE     16384
232
233 static inline bool carl9170fw_size_check(unsigned int len)
234 {
235         return (len <= CARL9170FW_MAX_SIZE && len >= CARL9170FW_MIN_SIZE);
236 }
237
238 #endif /* __CARL9170_SHARED_FWDESC_H */