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, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 static bool length_check(struct dma_desc *desc)
38 volatile struct carl9170_tx_superframe *super = __get_super(desc);
40 if (unlikely(desc->totalLen < sizeof(struct carl9170_tx_superdesc)))
44 * check if the DMA is complete, or clipped.
46 * NB: The hardware aligns the descriptor length to
47 * a 4 byte boundary. This makes the direct comparison
48 * difficult, or unnecessary complex for a hot-path.
50 if (unlikely(super->s.len > desc->totalLen))
56 static void handle_download(void)
58 struct dma_desc *desc;
61 * Under normal conditions, all completed descs should have
62 * the AR9170_OWN_BITS_SE status flag set.
63 * However there seems to be a undocumented case where the flag
64 * is _SW ( handle_download_exception )
67 for_each_desc_not_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_HW) {
68 if (unlikely((length_check(desc) == false))) {
70 * There is no easy way of telling what was lost.
72 * Therefore we just reclaim the data.
73 * The driver has to have some sort frame
77 wlan_tx_complete(__get_super(desc), false);
78 dma_reclaim(&fw.pta.down_queue, desc);
85 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
86 xorl(AR9170_GPIO_REG_PORT_DATA, 2);
87 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
90 static void handle_upload(void)
92 struct dma_desc *desc;
94 for_each_desc_not_bits(desc, &fw.pta.up_queue, AR9170_OWN_BITS_HW) {
98 * DO NOT compare the descriptor addresses.
100 if (DESC_PAYLOAD(desc) == (void *) &dma_mem.reserved.rsp) {
101 fw.usb.int_desc = desc;
102 fw.usb.int_desc_available = 1;
104 dma_reclaim(&fw.wlan.rx_queue, desc);
105 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
109 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
110 xorl(AR9170_GPIO_REG_PORT_DATA, 2);
111 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
114 static void handle_download_exception(void)
116 struct dma_desc *desc, *target;
118 /* actually, the queue should be stopped by now? */
119 usb_stop_down_queue();
121 target = (void *)((get(AR9170_PTA_REG_DN_CURR_ADDRH) << 16) |
122 get(AR9170_PTA_REG_DN_CURR_ADDRL));
125 * Put "forgotten" packets from the head of the queue, back
126 * to the current position
128 __while_desc_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_HW) {
132 dma_reclaim(&fw.pta.down_queue,
133 dma_unlink_head(&fw.pta.down_queue));
136 __for_each_desc_continue(desc, &fw.pta.down_queue) {
137 if ((desc->status & AR9170_OWN_BITS) == AR9170_OWN_BITS_SW)
138 dma_fix_downqueue(desc);
142 usb_start_down_queue();
147 /* handle interrupts from DMA chip */
148 void handle_host_interface(void)
152 pta_int = get(AR9170_PTA_REG_INT_FLAG);
154 #define HANDLER(intr, flag, func) \
156 if ((intr & flag) != 0) { \
161 HANDLER(pta_int, AR9170_PTA_INT_FLAG_DN, handle_download);
163 HANDLER(pta_int, AR9170_PTA_INT_FLAG_UP, handle_upload);
165 /* This is just guesswork and MAGIC */
166 pta_int = get(AR9170_PTA_REG_DMA_STATUS);
167 HANDLER(pta_int, 0x1, handle_download_exception);
172 void handle_cmd(struct carl9170_rsp *resp)
174 struct carl9170_cmd *cmd = &dma_mem.reserved.cmd.cmd;
177 /* copies cmd, len and extra fields */
178 resp->hdr.len = cmd->hdr.len;
179 resp->hdr.cmd = cmd->hdr.cmd;
180 resp->hdr.ext = cmd->hdr.ext;
181 resp->hdr.seq |= cmd->hdr.seq;
183 switch (cmd->hdr.cmd & ~CARL9170_CMD_ASYNC_FLAG) {
184 case CARL9170_CMD_RREG:
185 for (i = 0; i < (cmd->hdr.len / 4); i++)
186 resp->rreg_res.vals[i] = get(cmd->rreg.regs[i]);
189 case CARL9170_CMD_WREG:
191 for (i = 0; i < (cmd->hdr.len / 8); i++)
192 set(cmd->wreg.regs[i].addr, cmd->wreg.regs[i].val);
195 case CARL9170_CMD_ECHO:
196 memcpy(resp->echo.vals, cmd->echo.vals, cmd->hdr.len);
199 case CARL9170_CMD_SWRST:
200 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
202 * Command has no payload, so the response
203 * has no payload either.
206 fw.wlan.mac_reset = CARL9170_MAC_RESET_FORCE;
207 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
210 case CARL9170_CMD_REBOOT:
217 case CARL9170_CMD_READ_TSF:
219 read_tsf((uint32_t *)resp->tsf.tsf);
222 case CARL9170_CMD_RX_FILTER:
224 fw.wlan.rx_filter = cmd->rx_filter.rx_filter;
227 case CARL9170_CMD_WOL:
231 case CARL9170_CMD_TALLY:
232 resp->hdr.len = sizeof(struct carl9170_tally_rsp);
233 memcpy(&resp->tally, &fw.tally, sizeof(struct carl9170_tally_rsp));
234 resp->tally.tick = fw.ticks_per_usec;
235 memset(&fw.tally, 0, sizeof(struct carl9170_tally_rsp));
238 case CARL9170_CMD_BCN_CTRL:
241 if (cmd->bcn_ctrl.mode & CARL9170_BCN_CTRL_CAB_TRIGGER) {
242 wlan_modify_beacon(cmd->bcn_ctrl.vif_id,
243 cmd->bcn_ctrl.bcn_addr, cmd->bcn_ctrl.bcn_len);
244 set(AR9170_MAC_REG_BCN_ADDR, cmd->bcn_ctrl.bcn_addr);
245 set(AR9170_MAC_REG_BCN_LENGTH, cmd->bcn_ctrl.bcn_len);
246 set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
248 wlan_cab_flush_queue(cmd->bcn_ctrl.vif_id);
249 fw.wlan.cab_flush_trigger[cmd->bcn_ctrl.vif_id] = CARL9170_CAB_TRIGGER_EMPTY;
253 #ifdef CONFIG_CARL9170FW_SECURITY_ENGINE
254 case CARL9170_CMD_EKEY:
256 set_key(&cmd->setkey);
259 case CARL9170_CMD_DKEY:
261 disable_key(&cmd->disablekey);
263 #endif /* CONFIG_CARL9170FW_SECURITY_ENGINE */
265 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
266 case CARL9170_CMD_FREQUENCY:
267 case CARL9170_CMD_RF_INIT:
271 case CARL9170_CMD_FREQ_START:
275 rf_notify_set_channel();
278 case CARL9170_CMD_PSM:
280 fw.phy.psm.state = le32_to_cpu(cmd->psm.state);
283 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
286 BUG("Unknown command %x\n", cmd->hdr.cmd);