Allow "join bss command" argument's essid field to be IEEE80211_NWID_LEN long.
[dragonfly.git] / sys / dev / netif / acx / acxcmd.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
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  * 
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/dev/netif/acx/acxcmd.c,v 1.9 2007/03/30 12:46:58 sephe Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/rman.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/if_media.h>
48
49 #include <netproto/802_11/ieee80211_var.h>
50 #include <netproto/802_11/ieee80211_radiotap.h>
51
52 #define ACX_DEBUG
53
54 #include <dev/netif/acx/if_acxreg.h>
55 #include <dev/netif/acx/if_acxvar.h>
56 #include <dev/netif/acx/acxcmd.h>
57
58 #define CMDPRM_WRITE_REGION_1(sc, r, rlen)              \
59         bus_space_write_region_1((sc)->sc_mem2_bt,      \
60                                  (sc)->sc_mem2_bh,      \
61                                  (sc)->sc_cmd_param,    \
62                                  (const uint8_t *)(r), (rlen))
63
64 #define CMDPRM_READ_REGION_1(sc, r, rlen)                               \
65         bus_space_read_region_1((sc)->sc_mem2_bt, (sc)->sc_mem2_bh,     \
66                                 (sc)->sc_cmd_param, (uint8_t *)(r), (rlen))
67
68 /*
69  * This will clear previous command's
70  * execution status too
71  */
72 #define CMD_WRITE_4(sc, val)                                    \
73         bus_space_write_4((sc)->sc_mem2_bt, (sc)->sc_mem2_bh,   \
74                           (sc)->sc_cmd, (val))
75 #define CMD_READ_4(sc)          \
76         bus_space_read_4((sc)->sc_mem2_bt, (sc)->sc_mem2_bh, (sc)->sc_cmd)
77
78 /*
79  * acx command register layerout:
80  * upper 16bits are command execution status
81  * lower 16bits are command to be executed
82  */
83 #define ACX_CMD_STATUS_SHIFT    16
84 #define ACX_CMD_STATUS_OK       1
85
86 struct radio_init {
87         uint32_t        radio_ofs;      /* radio firmware offset */
88         uint32_t        radio_len;      /* radio firmware length */
89 } __packed;
90
91 struct bss_join_hdr {
92         uint8_t         bssid[IEEE80211_ADDR_LEN];
93         uint16_t        beacon_intvl;
94         uint8_t         chip_spec[3];
95         uint8_t         ndata_txrate;   /* see ACX_NDATA_TXRATE_ */
96         uint8_t         ndata_txopt;    /* see ACX_NDATA_TXOPT_ */
97         uint8_t         mode;           /* see ACX_MODE_ */
98         uint8_t         channel;
99         uint8_t         esslen;
100         char            essid[1];
101 } __packed;
102
103 /*
104  * non-data frame tx rate
105  */
106 #define ACX_NDATA_TXRATE_2              20      /* 2Mbits/s */
107
108 /*
109  * non-data frame tx options
110  */
111 #define ACX_NDATA_TXOPT_PBCC            0x40
112 #define ACX_NDATA_TXOPT_OFDM            0x20
113 #define ACX_NDATA_TXOPT_SHORT_PREAMBLE  0x10
114
115 #define BSS_JOIN_BUFLEN         \
116         (sizeof(struct bss_join_hdr) + IEEE80211_NWID_LEN)
117 #define BSS_JOIN_PARAM_SIZE(bj) \
118         (sizeof(struct bss_join_hdr) + (bj)->esslen)
119
120 void
121 acx_init_cmd_reg(struct acx_softc *sc)
122 {
123         sc->sc_cmd = CSR_READ_4(sc, ACXREG_CMD_REG_OFFSET);
124         sc->sc_cmd_param = sc->sc_cmd + ACX_CMD_REG_SIZE;
125
126         /* Clear command & status */
127         CMD_WRITE_4(sc, 0);
128 }
129
130 int
131 acx_join_bss(struct acx_softc *sc, uint8_t mode, struct ieee80211_node *ni,
132              struct ieee80211_channel *c)
133 {
134         uint8_t bj_buf[BSS_JOIN_BUFLEN];
135         struct ieee80211com *ic = &sc->sc_ic;
136         struct bss_join_hdr *bj;
137         int i;
138
139         bzero(bj_buf, sizeof(bj_buf));
140         bj = (struct bss_join_hdr *)bj_buf;
141
142         for (i = 0; i < IEEE80211_ADDR_LEN; ++i)
143                 bj->bssid[i] = ni->ni_bssid[IEEE80211_ADDR_LEN - i - 1];
144
145         bj->beacon_intvl = htole16(ni->ni_intval);
146
147         sc->chip_set_bss_join_param(sc, bj->chip_spec, ic->ic_dtim_period);
148
149         bj->ndata_txrate = ACX_NDATA_TXRATE_2;
150         bj->ndata_txopt = 0;
151         bj->mode = mode;
152         bj->channel = ieee80211_chan2ieee(ic, c);
153         bj->esslen = ni->ni_esslen;
154         bcopy(ni->ni_essid, bj->essid, ni->ni_esslen);
155
156         DPRINTF((&ic->ic_if, "join BSS/IBSS on channel %d\n", bj->channel));
157         return acx_exec_command(sc, ACXCMD_JOIN_BSS,
158                                 bj, BSS_JOIN_PARAM_SIZE(bj), NULL, 0);
159 }
160
161 int
162 acx_enable_txchan(struct acx_softc *sc, uint8_t chan)
163 {
164         return acx_exec_command(sc, ACXCMD_ENABLE_TXCHAN, &chan, sizeof(chan),
165                                 NULL, 0);
166 }
167
168 int
169 acx_enable_rxchan(struct acx_softc *sc, uint8_t chan)
170 {
171         return acx_exec_command(sc, ACXCMD_ENABLE_RXCHAN, &chan, sizeof(chan),
172                                 NULL, 0);
173 }
174
175 int
176 acx_get_conf(struct acx_softc *sc, uint16_t conf_id, void *conf,
177              uint16_t conf_len)
178 {
179         struct acx_conf *confcom;
180
181         if (conf_len < sizeof(*confcom)) {
182                 if_printf(&sc->sc_ic.ic_if, "%s configure data is too short\n",
183                           __func__);
184                 return 1;
185         }
186
187         confcom = conf;
188         confcom->conf_id = htole16(conf_id);
189         confcom->conf_data_len = htole16(conf_len - sizeof(*confcom));
190
191         return acx_exec_command(sc, ACXCMD_GET_CONF, confcom, sizeof(*confcom),
192                                 conf, conf_len);
193 }
194
195 int
196 acx_set_conf(struct acx_softc *sc, uint16_t conf_id, void *conf,
197              uint16_t conf_len)
198 {
199         struct acx_conf *confcom;
200
201         if (conf_len < sizeof(*confcom)) {
202                 if_printf(&sc->sc_ic.ic_if, "%s configure data is too short\n",
203                           __func__);
204                 return 1;
205         }
206
207         confcom = conf;
208         confcom->conf_id = htole16(conf_id);
209         confcom->conf_data_len = htole16(conf_len - sizeof(*confcom));
210
211         return acx_exec_command(sc, ACXCMD_SET_CONF, conf, conf_len, NULL, 0);
212 }
213
214 int
215 acx_set_tmplt(struct acx_softc *sc, uint16_t cmd, void *tmplt,
216               uint16_t tmplt_len)
217 {
218         uint16_t *size;
219
220         if (tmplt_len < sizeof(*size)) {
221                 if_printf(&sc->sc_ic.ic_if, "%s template is too short\n",
222                           __func__);
223                 return 1;
224         }
225
226         size = tmplt;
227         *size = htole16(tmplt_len - sizeof(*size));
228
229         return acx_exec_command(sc, cmd, tmplt, tmplt_len, NULL, 0);
230 }
231
232 int
233 acx_init_radio(struct acx_softc *sc, uint32_t radio_ofs, uint32_t radio_len)
234 {
235         struct radio_init r;
236
237         r.radio_ofs = htole32(radio_ofs);
238         r.radio_len = htole32(radio_len);
239         return acx_exec_command(sc, ACXCMD_INIT_RADIO, &r, sizeof(r), NULL, 0);
240 }
241
242 int
243 acx_exec_command(struct acx_softc *sc, uint16_t cmd, void *param,
244                  uint16_t param_len, void *result, uint16_t result_len)
245 {
246         uint16_t status;
247         int i, ret;
248
249         ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
250
251         if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0) {
252                 if_printf(&sc->sc_ic.ic_if, "cmd 0x%04x failed (base firmware "
253                           "not loaded)", cmd);
254                 return 1;
255         }
256
257         ret = 0;
258
259         if (param != NULL && param_len != 0) {
260                 /* Set command param */
261                 CMDPRM_WRITE_REGION_1(sc, param, param_len);
262         }
263
264         /* Set command */
265         CMD_WRITE_4(sc, cmd);
266
267         /* Exec command */
268         CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_CMD_FINI);
269         DELAY(50);      /* XXX maybe 100 */
270
271         /* Wait for command to complete */
272         if (cmd == ACXCMD_INIT_RADIO) {
273                 /* XXX radio initialization is extremely long */
274                 tsleep(&cmd, 0, "rdinit", (150 * hz) / 1000);   /* 150ms */
275         }
276
277 #define CMDWAIT_RETRY_MAX       1000
278         for (i = 0; i < CMDWAIT_RETRY_MAX; ++i) {
279                 uint16_t reg;
280
281                 reg = CSR_READ_2(sc, ACXREG_INTR_STATUS);
282                 if (reg & ACXRV_INTR_CMD_FINI) {
283                         CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_CMD_FINI);
284                         break;
285                 }
286                 DELAY(50);
287         }
288         if (i == CMDWAIT_RETRY_MAX) {
289                 if_printf(&sc->sc_ic.ic_if, "cmd %04x failed (timeout)\n", cmd);
290                 ret = 1;
291                 goto back;
292         }
293 #undef CMDWAIT_RETRY_MAX
294
295         /* Get command exec status */
296         status = (CMD_READ_4(sc) >> ACX_CMD_STATUS_SHIFT);
297         if (status != ACX_CMD_STATUS_OK) {
298                 if_printf(&sc->sc_ic.ic_if, "cmd %04x failed\n", cmd);
299                 ret = 1;
300                 goto back;
301         }
302
303         if (result != NULL && result_len != 0) {
304                 /* Get command result */
305                 CMDPRM_READ_REGION_1(sc, result, result_len);
306         }
307
308 back:
309         CMD_WRITE_4(sc, 0);
310         return ret;
311 }