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