2 * carl9170 firmware - used by the ar9170 wireless device
4 * Host interface routines
6 * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7 * Copyright (c) 2007-2009 Atheros Communications, Inc.
8 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
9 * Copyright 2009-2012 Christian Lamparter <chunkeey@googlemail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; If not, see <http://www.gnu.org/licenses/>.
35 static bool length_check(struct dma_desc *desc)
37 volatile struct carl9170_tx_superframe *super = __get_super(desc);
39 if (unlikely(desc->totalLen < sizeof(struct carl9170_tx_superdesc)))
43 * check if the DMA is complete, or clipped.
45 * NB: The hardware aligns the descriptor length to
46 * a 4 byte boundary. This makes the direct comparison
47 * difficult, or unnecessary complex for a hot-path.
49 if (unlikely(super->s.len > desc->totalLen))
55 static void handle_download(void)
57 struct dma_desc *desc;
60 * Under normal conditions, all completed descs should have
61 * the AR9170_OWN_BITS_SE status flag set.
62 * However there seems to be a undocumented case where the flag
63 * is _SW ( handle_download_exception )
66 for_each_desc_not_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_HW) {
67 if (unlikely((length_check(desc) == false))) {
69 * There is no easy way of telling what was lost.
71 * Therefore we just reclaim the data.
72 * The driver has to have some sort frame
76 wlan_tx_complete(__get_super(desc), false);
77 dma_reclaim(&fw.pta.down_queue, desc);
84 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
85 xorl(AR9170_GPIO_REG_PORT_DATA, 2);
86 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
89 static void handle_upload(void)
91 struct dma_desc *desc;
93 for_each_desc_not_bits(desc, &fw.pta.up_queue, AR9170_OWN_BITS_HW) {
97 * DO NOT compare the descriptor addresses.
99 if (DESC_PAYLOAD(desc) == (void *) &dma_mem.reserved.rsp) {
100 fw.usb.int_desc = desc;
101 fw.usb.int_desc_available = 1;
103 dma_reclaim(&fw.wlan.rx_queue, desc);
104 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
108 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
109 xorl(AR9170_GPIO_REG_PORT_DATA, 2);
110 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
113 static void handle_download_exception(void)
115 struct dma_desc *desc, *target;
117 /* actually, the queue should be stopped by now? */
118 usb_stop_down_queue();
120 target = (void *)((get(AR9170_PTA_REG_DN_CURR_ADDRH) << 16) |
121 get(AR9170_PTA_REG_DN_CURR_ADDRL));
124 * Put "forgotten" packets from the head of the queue, back
125 * to the current position
127 __while_desc_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_HW) {
131 dma_reclaim(&fw.pta.down_queue,
132 dma_unlink_head(&fw.pta.down_queue));
135 __for_each_desc_continue(desc, &fw.pta.down_queue) {
136 if ((desc->status & AR9170_OWN_BITS) == AR9170_OWN_BITS_SW)
137 dma_fix_downqueue(desc);
141 usb_start_down_queue();
146 /* handle interrupts from DMA chip */
147 void handle_host_interface(void)
151 pta_int = get(AR9170_PTA_REG_INT_FLAG);
153 #define HANDLER(intr, flag, func) \
155 if ((intr & flag) != 0) { \
160 HANDLER(pta_int, AR9170_PTA_INT_FLAG_DN, handle_download);
162 HANDLER(pta_int, AR9170_PTA_INT_FLAG_UP, handle_upload);
164 /* This is just guesswork and MAGIC */
165 pta_int = get(AR9170_PTA_REG_DMA_STATUS);
166 HANDLER(pta_int, 0x1, handle_download_exception);
171 void handle_cmd(struct carl9170_rsp *resp)
173 struct carl9170_cmd *cmd = &dma_mem.reserved.cmd.cmd;
176 /* copies cmd, len and extra fields */
177 resp->hdr.len = cmd->hdr.len;
178 resp->hdr.cmd = cmd->hdr.cmd;
179 resp->hdr.ext = cmd->hdr.ext;
180 resp->hdr.seq |= cmd->hdr.seq;
182 switch (cmd->hdr.cmd & ~CARL9170_CMD_ASYNC_FLAG) {
183 case CARL9170_CMD_RREG:
184 for (i = 0; i < (cmd->hdr.len / 4); i++)
185 resp->rreg_res.vals[i] = get(cmd->rreg.regs[i]);
188 case CARL9170_CMD_WREG:
190 for (i = 0; i < (cmd->hdr.len / 8); i++)
191 set(cmd->wreg.regs[i].addr, cmd->wreg.regs[i].val);
194 case CARL9170_CMD_ECHO:
195 memcpy(resp->echo.vals, cmd->echo.vals, cmd->hdr.len);
198 case CARL9170_CMD_SWRST:
199 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
201 * Command has no payload, so the response
202 * has no payload either.
205 fw.wlan.mac_reset = CARL9170_MAC_RESET_FORCE;
206 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
209 case CARL9170_CMD_REBOOT:
216 case CARL9170_CMD_READ_TSF:
218 read_tsf((uint32_t *)resp->tsf.tsf);
221 case CARL9170_CMD_RX_FILTER:
223 fw.wlan.rx_filter = cmd->rx_filter.rx_filter;
226 case CARL9170_CMD_WOL:
230 case CARL9170_CMD_TALLY:
231 resp->hdr.len = sizeof(struct carl9170_tally_rsp);
232 memcpy(&resp->tally, &fw.tally, sizeof(struct carl9170_tally_rsp));
233 resp->tally.tick = fw.ticks_per_usec;
234 memset(&fw.tally, 0, sizeof(struct carl9170_tally_rsp));
237 case CARL9170_CMD_WREGB:
239 for (i = 0; i < MIN(cmd->wregb.count, cmd->hdr.len - 8); i++)
240 setb(cmd->wregb.addr + i, cmd->wregb.val[i]);
243 case CARL9170_CMD_BCN_CTRL:
246 if (cmd->bcn_ctrl.mode & CARL9170_BCN_CTRL_CAB_TRIGGER) {
247 wlan_modify_beacon(cmd->bcn_ctrl.vif_id,
248 cmd->bcn_ctrl.bcn_addr, cmd->bcn_ctrl.bcn_len);
249 set(AR9170_MAC_REG_BCN_ADDR, cmd->bcn_ctrl.bcn_addr);
250 set(AR9170_MAC_REG_BCN_LENGTH, cmd->bcn_ctrl.bcn_len);
251 set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
253 wlan_cab_flush_queue(cmd->bcn_ctrl.vif_id);
254 fw.wlan.cab_flush_trigger[cmd->bcn_ctrl.vif_id] = CARL9170_CAB_TRIGGER_EMPTY;
258 #ifdef CONFIG_CARL9170FW_SECURITY_ENGINE
259 case CARL9170_CMD_EKEY:
261 set_key(&cmd->setkey);
264 case CARL9170_CMD_DKEY:
266 disable_key(&cmd->disablekey);
268 #endif /* CONFIG_CARL9170FW_SECURITY_ENGINE */
270 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
271 case CARL9170_CMD_FREQUENCY:
272 case CARL9170_CMD_RF_INIT:
276 case CARL9170_CMD_FREQ_START:
280 rf_notify_set_channel();
283 case CARL9170_CMD_PSM:
285 fw.phy.psm.state = le32_to_cpu(cmd->psm.state);
288 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
291 BUG("Unknown command %x\n", cmd->hdr.cmd);