2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/sys/dev/netif/acx/acxcmd.c,v 1.9 2007/03/30 12:46:58 sephe Exp $
37 #include <sys/param.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
46 #include <net/if_arp.h>
47 #include <net/if_media.h>
49 #include <netproto/802_11/ieee80211_var.h>
50 #include <netproto/802_11/ieee80211_radiotap.h>
54 #include <dev/netif/acx/if_acxreg.h>
55 #include <dev/netif/acx/if_acxvar.h>
56 #include <dev/netif/acx/acxcmd.h>
58 #define CMDPRM_WRITE_REGION_1(sc, r, rlen) \
59 bus_space_write_region_1((sc)->sc_mem2_bt, \
62 (const uint8_t *)(r), (rlen))
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))
69 * This will clear previous command's
70 * execution status too
72 #define CMD_WRITE_4(sc, val) \
73 bus_space_write_4((sc)->sc_mem2_bt, (sc)->sc_mem2_bh, \
75 #define CMD_READ_4(sc) \
76 bus_space_read_4((sc)->sc_mem2_bt, (sc)->sc_mem2_bh, (sc)->sc_cmd)
79 * acx command register layerout:
80 * upper 16bits are command execution status
81 * lower 16bits are command to be executed
83 #define ACX_CMD_STATUS_SHIFT 16
84 #define ACX_CMD_STATUS_OK 1
87 uint32_t radio_ofs; /* radio firmware offset */
88 uint32_t radio_len; /* radio firmware length */
92 uint8_t bssid[IEEE80211_ADDR_LEN];
93 uint16_t beacon_intvl;
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_ */
104 * non-data frame tx rate
106 #define ACX_NDATA_TXRATE_2 20 /* 2Mbits/s */
109 * non-data frame tx options
111 #define ACX_NDATA_TXOPT_PBCC 0x40
112 #define ACX_NDATA_TXOPT_OFDM 0x20
113 #define ACX_NDATA_TXOPT_SHORT_PREAMBLE 0x10
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)
121 acx_init_cmd_reg(struct acx_softc *sc)
123 sc->sc_cmd = CSR_READ_4(sc, ACXREG_CMD_REG_OFFSET);
124 sc->sc_cmd_param = sc->sc_cmd + ACX_CMD_REG_SIZE;
126 /* Clear command & status */
131 acx_join_bss(struct acx_softc *sc, uint8_t mode, struct ieee80211_node *ni,
132 struct ieee80211_channel *c)
134 uint8_t bj_buf[BSS_JOIN_BUFLEN];
135 struct ieee80211com *ic = &sc->sc_ic;
136 struct bss_join_hdr *bj;
139 bzero(bj_buf, sizeof(bj_buf));
140 bj = (struct bss_join_hdr *)bj_buf;
142 for (i = 0; i < IEEE80211_ADDR_LEN; ++i)
143 bj->bssid[i] = ni->ni_bssid[IEEE80211_ADDR_LEN - i - 1];
145 bj->beacon_intvl = htole16(ni->ni_intval);
147 sc->chip_set_bss_join_param(sc, bj->chip_spec, ic->ic_dtim_period);
149 bj->ndata_txrate = ACX_NDATA_TXRATE_2;
152 bj->channel = ieee80211_chan2ieee(ic, c);
153 bj->esslen = ni->ni_esslen;
154 bcopy(ni->ni_essid, bj->essid, ni->ni_esslen);
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);
162 acx_enable_txchan(struct acx_softc *sc, uint8_t chan)
164 return acx_exec_command(sc, ACXCMD_ENABLE_TXCHAN, &chan, sizeof(chan),
169 acx_enable_rxchan(struct acx_softc *sc, uint8_t chan)
171 return acx_exec_command(sc, ACXCMD_ENABLE_RXCHAN, &chan, sizeof(chan),
176 acx_get_conf(struct acx_softc *sc, uint16_t conf_id, void *conf,
179 struct acx_conf *confcom;
181 if (conf_len < sizeof(*confcom)) {
182 if_printf(&sc->sc_ic.ic_if, "%s configure data is too short\n",
188 confcom->conf_id = htole16(conf_id);
189 confcom->conf_data_len = htole16(conf_len - sizeof(*confcom));
191 return acx_exec_command(sc, ACXCMD_GET_CONF, confcom, sizeof(*confcom),
196 acx_set_conf(struct acx_softc *sc, uint16_t conf_id, void *conf,
199 struct acx_conf *confcom;
201 if (conf_len < sizeof(*confcom)) {
202 if_printf(&sc->sc_ic.ic_if, "%s configure data is too short\n",
208 confcom->conf_id = htole16(conf_id);
209 confcom->conf_data_len = htole16(conf_len - sizeof(*confcom));
211 return acx_exec_command(sc, ACXCMD_SET_CONF, conf, conf_len, NULL, 0);
215 acx_set_tmplt(struct acx_softc *sc, uint16_t cmd, void *tmplt,
220 if (tmplt_len < sizeof(*size)) {
221 if_printf(&sc->sc_ic.ic_if, "%s template is too short\n",
227 *size = htole16(tmplt_len - sizeof(*size));
229 return acx_exec_command(sc, cmd, tmplt, tmplt_len, NULL, 0);
233 acx_init_radio(struct acx_softc *sc, uint32_t radio_ofs, uint32_t radio_len)
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);
243 acx_exec_command(struct acx_softc *sc, uint16_t cmd, void *param,
244 uint16_t param_len, void *result, uint16_t result_len)
249 ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
251 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0) {
252 if_printf(&sc->sc_ic.ic_if, "cmd 0x%04x failed (base firmware "
259 if (param != NULL && param_len != 0) {
260 /* Set command param */
261 CMDPRM_WRITE_REGION_1(sc, param, param_len);
265 CMD_WRITE_4(sc, cmd);
268 CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_CMD_FINI);
269 DELAY(50); /* XXX maybe 100 */
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 */
277 #define CMDWAIT_RETRY_MAX 1000
278 for (i = 0; i < CMDWAIT_RETRY_MAX; ++i) {
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);
288 if (i == CMDWAIT_RETRY_MAX) {
289 if_printf(&sc->sc_ic.ic_if, "cmd %04x failed (timeout)\n", cmd);
293 #undef CMDWAIT_RETRY_MAX
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);
303 if (result != NULL && result_len != 0) {
304 /* Get command result */
305 CMDPRM_READ_REGION_1(sc, result, result_len);