c079b3371e13c7759d315530826fdb9ff5408b89
[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         /* unusable - reserved to flag non-functional debug firmwares */
29         CARL9170FW_UNUSABLE,
30
31         /* AR9170_CMD_RF_INIT, AR9170_CMD_FREQ_START, AR9170_CMD_FREQUENCY */
32         CARL9170FW_COMMAND_PHY,
33
34         /* AR9170_CMD_EKEY, AR9170_CMD_DKEY */
35         CARL9170FW_COMMAND_CAM,
36
37         /* Firmware has a software Content After Beacon Queueing mechanism */
38         CARL9170FW_WLANTX_CAB,
39
40         /* The firmware is capable of responding to incoming BAR frames */
41         CARL9170FW_HANDLE_BACK_REQ,
42
43         /* GPIO Interrupt | CARL9170_RSP_GPIO */
44         CARL9170FW_GPIO_INTERRUPT,
45
46         /* Firmware PSM support | CARL9170_CMD_PSM */
47         CARL9170FW_PSM,
48
49         /* KEEP LAST */
50         __CARL9170FW_FEATURE_NUM
51 };
52
53 enum carl9170fw_usb_feature_list {
54         /* Always set */
55         CARL9170FW_USB_DUMMY_FEATURE,
56
57         /*
58          * Indicates that this image has special boot block which prevents
59          * legacy drivers to drive the firmware.
60          */
61         CARL9170FW_USB_MINIBOOT,
62
63         /* usb registers are initialized by the firmware */
64         CARL9170FW_USB_INIT_FIRMWARE,
65
66         /* command traps & notifications are send through EP2 */
67         CARL9170FW_USB_RESP_EP2,
68
69         /* usb download (app -> fw) stream */
70         CARL9170FW_USB_DOWN_STREAM,
71
72         /* usb upload (fw -> app) stream */
73         CARL9170FW_USB_UP_STREAM,
74
75         /* USB Watchdog */
76         CARL9170FW_USB_WATCHDOG,
77
78         /* KEEP LAST */
79         __CARL9170FW_USB_FEATURE_NUM
80 };
81
82 #define OTUS_MAGIC      "OTAR"
83 #define USB_MAGIC       "USB\0"
84 #define MOTD_MAGIC      "MOTD"
85 #define FIX_MAGIC       "FIX\0"
86 #define DBG_MAGIC       "DBG\0"
87 #define CHK_MAGIC       "CHK\0"
88 #define LAST_MAGIC      "LAST"
89
90 #define CARL9170FW_SET_DAY(d) (((d) - 1) % 31)
91 #define CARL9170FW_SET_MONTH(m) ((((m) - 1) % 12) * 31)
92 #define CARL9170FW_SET_YEAR(y) (((y) - 10) * 372)
93
94 #define CARL9170FW_GET_DAY(d) (((d) % 31) + 1)
95 #define CARL9170FW_GET_MONTH(m) ((((m) / 31) % 12) + 1)
96 #define CARL9170FW_GET_YEAR(y) ((y) / 372 + 10)
97
98 struct carl9170fw_desc_head {
99         u8      magic[4];
100         __le16 length;
101         u8 min_ver;
102         u8 cur_ver;
103 } __packed;
104 #define CARL9170FW_DESC_HEAD_SIZE                       \
105         (sizeof(struct carl9170fw_desc_head))
106
107 #define CARL9170FW_OTUS_DESC_MIN_VER            2
108 #define CARL9170FW_OTUS_DESC_CUR_VER            2
109 struct carl9170fw_otus_desc {
110         struct carl9170fw_desc_head head;
111         __le32 fw_feature_set;
112         __le32 bcn_addr;
113         __le16 bcn_len;
114         u8 api_ver;
115         u8 vif_num;
116 } __packed;
117 #define CARL9170FW_OTUS_DESC_SIZE                       \
118         (sizeof(struct carl9170fw_otus_desc))
119
120 #define CARL9170FW_USB_DESC_MIN_VER             2
121 #define CARL9170FW_USB_DESC_CUR_VER             2
122 struct carl9170fw_usb_desc {
123         struct carl9170fw_desc_head head;
124         __le32 usb_feature_set;
125         __le32 fw_address;
126         __le16 tx_frag_len;
127         __le16 rx_max_frame_len;
128         __le16 miniboot_size;
129         u8 tx_descs;
130         u8 free;
131 } __packed;
132 #define CARL9170FW_USB_DESC_SIZE                        \
133         (sizeof(struct carl9170fw_usb_desc))
134
135 #define CARL9170FW_MOTD_STRING_LEN                      24
136 #define CARL9170FW_MOTD_RELEASE_LEN                     20
137 #define CARL9170FW_MOTD_DESC_MIN_VER                    1
138 #define CARL9170FW_MOTD_DESC_CUR_VER                    2
139 struct carl9170fw_motd_desc {
140         struct carl9170fw_desc_head head;
141         __le32 fw_year_month_day;
142         char desc[CARL9170FW_MOTD_STRING_LEN];
143         char release[CARL9170FW_MOTD_RELEASE_LEN];
144 } __packed;
145 #define CARL9170FW_MOTD_DESC_SIZE                       \
146         (sizeof(struct carl9170fw_motd_desc))
147
148 #define CARL9170FW_FIX_DESC_MIN_VER                     1
149 #define CARL9170FW_FIX_DESC_CUR_VER                     2
150 struct carl9170fw_fix_entry {
151         __le32 address;
152         __le32 mask;
153         __le32 value;
154 } __packed;
155
156 struct carl9170fw_fix_desc {
157         struct carl9170fw_desc_head head;
158         struct carl9170fw_fix_entry data[0];
159 } __packed;
160 #define CARL9170FW_FIX_DESC_SIZE                        \
161         (sizeof(struct carl9170fw_fix_desc))
162
163 #define CARL9170FW_DBG_DESC_MIN_VER                     1
164 #define CARL9170FW_DBG_DESC_CUR_VER                     2
165 struct carl9170fw_dbg_desc {
166         struct carl9170fw_desc_head head;
167
168         __le32 bogoclock_addr;
169         __le32 counter_addr;
170         __le32 rx_total_addr;
171         __le32 rx_overrun_addr;
172
173         /* Put your debugging definitions here */
174 } __packed;
175 #define CARL9170FW_DBG_DESC_SIZE                        \
176         (sizeof(struct carl9170fw_dbg_desc))
177
178 #define CARL9170FW_CHK_DESC_MIN_VER                     1
179 #define CARL9170FW_CHK_DESC_CUR_VER                     2
180 struct carl9170fw_chk_desc {
181         struct carl9170fw_desc_head head;
182         __le32 fw_crc32;
183         __le32 hdr_crc32;
184 } __packed;
185 #define CARL9170FW_CHK_DESC_SIZE                        \
186         (sizeof(struct carl9170fw_chk_desc))
187
188 #define CARL9170FW_LAST_DESC_MIN_VER                    1
189 #define CARL9170FW_LAST_DESC_CUR_VER                    2
190 struct carl9170fw_last_desc {
191         struct carl9170fw_desc_head head;
192 } __packed;
193 #define CARL9170FW_LAST_DESC_SIZE                       \
194         (sizeof(struct carl9170fw_fix_desc))
195
196 #define CARL9170FW_DESC_MAX_LENGTH                      8192
197
198 #define CARL9170FW_FILL_DESC(_magic, _length, _min_ver, _cur_ver)       \
199         .head = {                                                       \
200                 .magic = _magic,                                        \
201                 .length = cpu_to_le16(_length),                         \
202                 .min_ver = _min_ver,                                    \
203                 .cur_ver = _cur_ver,                                    \
204         }
205
206 static inline void carl9170fw_fill_desc(struct carl9170fw_desc_head *head,
207                                          u8 magic[4], __le16 length,
208                                          u8 min_ver, u8 cur_ver)
209 {
210         head->magic[0] = magic[0];
211         head->magic[1] = magic[1];
212         head->magic[2] = magic[2];
213         head->magic[3] = magic[3];
214
215         head->length = length;
216         head->min_ver = min_ver;
217         head->cur_ver = cur_ver;
218 }
219
220 #define carl9170fw_for_each_hdr(desc, fw_desc)                          \
221         for (desc = fw_desc;                                            \
222              memcmp(desc->magic, LAST_MAGIC, 4) &&                      \
223              le16_to_cpu(desc->length) >= CARL9170FW_DESC_HEAD_SIZE &&  \
224              le16_to_cpu(desc->length) < CARL9170FW_DESC_MAX_LENGTH;    \
225              desc = (void *)((unsigned long)desc + le16_to_cpu(desc->length)))
226
227 #define CHECK_HDR_VERSION(head, _min_ver)                               \
228         (((head)->cur_ver < _min_ver) || ((head)->min_ver > _min_ver))  \
229
230 static inline bool carl9170fw_supports(__le32 list, u8 feature)
231 {
232         return le32_to_cpu(list) & BIT(feature);
233 }
234
235 static inline bool carl9170fw_desc_cmp(const struct carl9170fw_desc_head *head,
236                                        const u8 descid[4], u16 min_len,
237                                        u8 compatible_revision)
238 {
239         if (descid[0] == head->magic[0] && descid[1] == head->magic[1] &&
240             descid[2] == head->magic[2] && descid[3] == head->magic[3] &&
241             !CHECK_HDR_VERSION(head, compatible_revision) &&
242             (le16_to_cpu(head->length) >= min_len))
243                 return true;
244
245         return false;
246 }
247
248 #define CARL9170FW_MIN_SIZE     32
249 #define CARL9170FW_MAX_SIZE     16384
250
251 static inline bool carl9170fw_size_check(unsigned int len)
252 {
253         return (len <= CARL9170FW_MAX_SIZE && len >= CARL9170FW_MIN_SIZE);
254 }
255
256 #endif /* __CARL9170_SHARED_FWDESC_H */