Make ar9170 use common ath reg code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / ar9170 / main.c
blob8de0ff9f580b129928a484a78686b040babf7e5b
1 /*
2 * Atheros AR9170 driver
4 * mac80211 interaction code
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, Christian Lamparter <chunkeey@web.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, see
21 * http://www.gnu.org/licenses/.
23 * This file incorporates work covered by the following copyright and
24 * permission notice:
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
27 * Permission to use, copy, modify, and/or distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
31 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 #include <linux/init.h>
41 #include <linux/module.h>
42 #include <linux/etherdevice.h>
43 #include <net/mac80211.h>
44 #include "ar9170.h"
45 #include "hw.h"
46 #include "cmd.h"
48 static int modparam_nohwcrypt;
49 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
50 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
52 #define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
53 .bitrate = (_bitrate), \
54 .flags = (_flags), \
55 .hw_value = (_hw_rate) | (_txpidx) << 4, \
58 static struct ieee80211_rate __ar9170_ratetable[] = {
59 RATE(10, 0, 0, 0),
60 RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE),
61 RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE),
62 RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE),
63 RATE(60, 0xb, 0, 0),
64 RATE(90, 0xf, 0, 0),
65 RATE(120, 0xa, 0, 0),
66 RATE(180, 0xe, 0, 0),
67 RATE(240, 0x9, 0, 0),
68 RATE(360, 0xd, 1, 0),
69 RATE(480, 0x8, 2, 0),
70 RATE(540, 0xc, 3, 0),
72 #undef RATE
74 #define ar9170_g_ratetable (__ar9170_ratetable + 0)
75 #define ar9170_g_ratetable_size 12
76 #define ar9170_a_ratetable (__ar9170_ratetable + 4)
77 #define ar9170_a_ratetable_size 8
80 * NB: The hw_value is used as an index into the ar9170_phy_freq_params
81 * array in phy.c so that we don't have to do frequency lookups!
83 #define CHAN(_freq, _idx) { \
84 .center_freq = (_freq), \
85 .hw_value = (_idx), \
86 .max_power = 18, /* XXX */ \
89 static struct ieee80211_channel ar9170_2ghz_chantable[] = {
90 CHAN(2412, 0),
91 CHAN(2417, 1),
92 CHAN(2422, 2),
93 CHAN(2427, 3),
94 CHAN(2432, 4),
95 CHAN(2437, 5),
96 CHAN(2442, 6),
97 CHAN(2447, 7),
98 CHAN(2452, 8),
99 CHAN(2457, 9),
100 CHAN(2462, 10),
101 CHAN(2467, 11),
102 CHAN(2472, 12),
103 CHAN(2484, 13),
106 static struct ieee80211_channel ar9170_5ghz_chantable[] = {
107 CHAN(4920, 14),
108 CHAN(4940, 15),
109 CHAN(4960, 16),
110 CHAN(4980, 17),
111 CHAN(5040, 18),
112 CHAN(5060, 19),
113 CHAN(5080, 20),
114 CHAN(5180, 21),
115 CHAN(5200, 22),
116 CHAN(5220, 23),
117 CHAN(5240, 24),
118 CHAN(5260, 25),
119 CHAN(5280, 26),
120 CHAN(5300, 27),
121 CHAN(5320, 28),
122 CHAN(5500, 29),
123 CHAN(5520, 30),
124 CHAN(5540, 31),
125 CHAN(5560, 32),
126 CHAN(5580, 33),
127 CHAN(5600, 34),
128 CHAN(5620, 35),
129 CHAN(5640, 36),
130 CHAN(5660, 37),
131 CHAN(5680, 38),
132 CHAN(5700, 39),
133 CHAN(5745, 40),
134 CHAN(5765, 41),
135 CHAN(5785, 42),
136 CHAN(5805, 43),
137 CHAN(5825, 44),
138 CHAN(5170, 45),
139 CHAN(5190, 46),
140 CHAN(5210, 47),
141 CHAN(5230, 48),
143 #undef CHAN
145 static struct ieee80211_supported_band ar9170_band_2GHz = {
146 .channels = ar9170_2ghz_chantable,
147 .n_channels = ARRAY_SIZE(ar9170_2ghz_chantable),
148 .bitrates = ar9170_g_ratetable,
149 .n_bitrates = ar9170_g_ratetable_size,
152 #ifdef AR9170_QUEUE_DEBUG
154 * In case some wants works with AR9170's crazy tx_status queueing techniques.
155 * He might need this rather useful probing function.
157 * NOTE: caller must hold the queue's spinlock!
160 static void ar9170_print_txheader(struct ar9170 *ar, struct sk_buff *skb)
162 struct ar9170_tx_control *txc = (void *) skb->data;
163 struct ieee80211_hdr *hdr = (void *)txc->frame_data;
165 printk(KERN_DEBUG "%s: => FRAME [skb:%p, queue:%d, DA:[%pM] "
166 "mac_control:%04x, phy_control:%08x]\n",
167 wiphy_name(ar->hw->wiphy), skb, skb_get_queue_mapping(skb),
168 ieee80211_get_DA(hdr), le16_to_cpu(txc->mac_control),
169 le32_to_cpu(txc->phy_control));
172 static void ar9170_dump_station_tx_status_queue(struct ar9170 *ar,
173 struct sk_buff_head *queue)
175 struct sk_buff *skb;
176 int i = 0;
178 printk(KERN_DEBUG "---[ cut here ]---\n");
179 printk(KERN_DEBUG "%s: %d entries in tx_status queue.\n",
180 wiphy_name(ar->hw->wiphy), skb_queue_len(queue));
182 skb_queue_walk(queue, skb) {
183 struct ar9170_tx_control *txc = (void *) skb->data;
184 struct ieee80211_hdr *hdr = (void *)txc->frame_data;
186 printk(KERN_DEBUG "index:%d => \n", i);
187 ar9170_print_txheader(ar, skb);
189 printk(KERN_DEBUG "---[ end ]---\n");
191 #endif /* AR9170_QUEUE_DEBUG */
193 static struct ieee80211_supported_band ar9170_band_5GHz = {
194 .channels = ar9170_5ghz_chantable,
195 .n_channels = ARRAY_SIZE(ar9170_5ghz_chantable),
196 .bitrates = ar9170_a_ratetable,
197 .n_bitrates = ar9170_a_ratetable_size,
200 void ar9170_handle_tx_status(struct ar9170 *ar, struct sk_buff *skb,
201 bool valid_status, u16 tx_status)
203 struct ieee80211_tx_info *txinfo;
204 unsigned int retries = 0, queue = skb_get_queue_mapping(skb);
205 unsigned long flags;
207 spin_lock_irqsave(&ar->tx_stats_lock, flags);
208 ar->tx_stats[queue].len--;
209 if (ieee80211_queue_stopped(ar->hw, queue))
210 ieee80211_wake_queue(ar->hw, queue);
211 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
213 txinfo = IEEE80211_SKB_CB(skb);
214 ieee80211_tx_info_clear_status(txinfo);
216 switch (tx_status) {
217 case AR9170_TX_STATUS_RETRY:
218 retries = 2;
219 case AR9170_TX_STATUS_COMPLETE:
220 txinfo->flags |= IEEE80211_TX_STAT_ACK;
221 break;
223 case AR9170_TX_STATUS_FAILED:
224 retries = ar->hw->conf.long_frame_max_tx_count;
225 break;
227 default:
228 printk(KERN_ERR "%s: invalid tx_status response (%x).\n",
229 wiphy_name(ar->hw->wiphy), tx_status);
230 break;
233 if (valid_status)
234 txinfo->status.rates[0].count = retries + 1;
236 skb_pull(skb, sizeof(struct ar9170_tx_control));
237 ieee80211_tx_status_irqsafe(ar->hw, skb);
240 static struct sk_buff *ar9170_find_skb_in_queue(struct ar9170 *ar,
241 const u8 *mac,
242 const u32 queue,
243 struct sk_buff_head *q)
245 unsigned long flags;
246 struct sk_buff *skb;
248 spin_lock_irqsave(&q->lock, flags);
249 skb_queue_walk(q, skb) {
250 struct ar9170_tx_control *txc = (void *) skb->data;
251 struct ieee80211_hdr *hdr = (void *) txc->frame_data;
252 u32 txc_queue = (le32_to_cpu(txc->phy_control) &
253 AR9170_TX_PHY_QOS_MASK) >>
254 AR9170_TX_PHY_QOS_SHIFT;
256 if ((queue != txc_queue) ||
257 (compare_ether_addr(ieee80211_get_DA(hdr), mac)))
258 continue;
260 __skb_unlink(skb, q);
261 spin_unlock_irqrestore(&q->lock, flags);
262 return skb;
264 spin_unlock_irqrestore(&q->lock, flags);
265 return NULL;
268 static struct sk_buff *ar9170_find_queued_skb(struct ar9170 *ar, const u8 *mac,
269 const u32 queue)
271 struct ieee80211_sta *sta;
272 struct sk_buff *skb;
275 * Unfortunately, the firmware does not tell to which (queued) frame
276 * this transmission status report belongs to.
278 * So we have to make risky guesses - with the scarce information
279 * the firmware provided (-> destination MAC, and phy_control) -
280 * and hope that we picked the right one...
282 rcu_read_lock();
283 sta = ieee80211_find_sta(ar->hw, mac);
285 if (likely(sta)) {
286 struct ar9170_sta_info *sta_priv = (void *) sta->drv_priv;
287 skb = skb_dequeue(&sta_priv->tx_status[queue]);
288 rcu_read_unlock();
289 if (likely(skb))
290 return skb;
291 } else
292 rcu_read_unlock();
294 /* scan the waste queue for candidates */
295 skb = ar9170_find_skb_in_queue(ar, mac, queue,
296 &ar->global_tx_status_waste);
297 if (!skb) {
298 /* so it still _must_ be in the global list. */
299 skb = ar9170_find_skb_in_queue(ar, mac, queue,
300 &ar->global_tx_status);
303 #ifdef AR9170_QUEUE_DEBUG
304 if (unlikely((!skb) && net_ratelimit())) {
305 printk(KERN_ERR "%s: ESS:[%pM] does not have any "
306 "outstanding frames in this queue (%d).\n",
307 wiphy_name(ar->hw->wiphy), mac, queue);
309 #endif /* AR9170_QUEUE_DEBUG */
310 return skb;
314 * This worker tries to keep the global tx_status queue empty.
315 * So we can guarantee that incoming tx_status reports for
316 * unregistered stations are always synced with the actual
317 * frame - which we think - belongs to.
320 static void ar9170_tx_status_janitor(struct work_struct *work)
322 struct ar9170 *ar = container_of(work, struct ar9170,
323 tx_status_janitor.work);
324 struct sk_buff *skb;
326 if (unlikely(!IS_STARTED(ar)))
327 return ;
329 mutex_lock(&ar->mutex);
330 /* recycle the garbage back to mac80211... one by one. */
331 while ((skb = skb_dequeue(&ar->global_tx_status_waste))) {
332 #ifdef AR9170_QUEUE_DEBUG
333 printk(KERN_DEBUG "%s: dispose queued frame =>\n",
334 wiphy_name(ar->hw->wiphy));
335 ar9170_print_txheader(ar, skb);
336 #endif /* AR9170_QUEUE_DEBUG */
337 ar9170_handle_tx_status(ar, skb, false,
338 AR9170_TX_STATUS_FAILED);
341 while ((skb = skb_dequeue(&ar->global_tx_status))) {
342 #ifdef AR9170_QUEUE_DEBUG
343 printk(KERN_DEBUG "%s: moving frame into waste queue =>\n",
344 wiphy_name(ar->hw->wiphy));
346 ar9170_print_txheader(ar, skb);
347 #endif /* AR9170_QUEUE_DEBUG */
348 skb_queue_tail(&ar->global_tx_status_waste, skb);
351 /* recall the janitor in 100ms - if there's garbage in the can. */
352 if (skb_queue_len(&ar->global_tx_status_waste) > 0)
353 queue_delayed_work(ar->hw->workqueue, &ar->tx_status_janitor,
354 msecs_to_jiffies(100));
356 mutex_unlock(&ar->mutex);
359 static void ar9170_handle_command_response(struct ar9170 *ar,
360 void *buf, u32 len)
362 struct ar9170_cmd_response *cmd = (void *) buf;
364 if ((cmd->type & 0xc0) != 0xc0) {
365 ar->callback_cmd(ar, len, buf);
366 return;
369 /* hardware event handlers */
370 switch (cmd->type) {
371 case 0xc1: {
373 * TX status notification:
374 * bytes: 0c c1 XX YY M1 M2 M3 M4 M5 M6 R4 R3 R2 R1 S2 S1
376 * XX always 81
377 * YY always 00
378 * M1-M6 is the MAC address
379 * R1-R4 is the transmit rate
380 * S1-S2 is the transmit status
383 struct sk_buff *skb;
384 u32 queue = (le32_to_cpu(cmd->tx_status.rate) &
385 AR9170_TX_PHY_QOS_MASK) >> AR9170_TX_PHY_QOS_SHIFT;
387 skb = ar9170_find_queued_skb(ar, cmd->tx_status.dst, queue);
388 if (unlikely(!skb))
389 return ;
391 ar9170_handle_tx_status(ar, skb, true,
392 le16_to_cpu(cmd->tx_status.status));
393 break;
396 case 0xc0:
398 * pre-TBTT event
400 if (ar->vif && ar->vif->type == NL80211_IFTYPE_AP)
401 queue_work(ar->hw->workqueue, &ar->beacon_work);
402 break;
404 case 0xc2:
406 * (IBSS) beacon send notification
407 * bytes: 04 c2 XX YY B4 B3 B2 B1
409 * XX always 80
410 * YY always 00
411 * B1-B4 "should" be the number of send out beacons.
413 break;
415 case 0xc3:
416 /* End of Atim Window */
417 break;
419 case 0xc4:
420 case 0xc5:
421 /* BlockACK events */
422 break;
424 case 0xc6:
425 /* Watchdog Interrupt */
426 break;
428 case 0xc9:
429 /* retransmission issue / SIFS/EIFS collision ?! */
430 break;
432 default:
433 printk(KERN_INFO "received unhandled event %x\n", cmd->type);
434 print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE, buf, len);
435 break;
440 * If the frame alignment is right (or the kernel has
441 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
442 * is only a single MPDU in the USB frame, then we can
443 * submit to mac80211 the SKB directly. However, since
444 * there may be multiple packets in one SKB in stream
445 * mode, and we need to observe the proper ordering,
446 * this is non-trivial.
448 static void ar9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
450 struct sk_buff *skb;
451 struct ar9170_rx_head *head = (void *)buf;
452 struct ar9170_rx_tail *tail;
453 struct ieee80211_rx_status status;
454 int mpdu_len, i;
455 u8 error, antennas = 0, decrypt;
456 __le16 fc;
457 int reserved;
459 if (unlikely(!IS_STARTED(ar)))
460 return ;
462 /* Received MPDU */
463 mpdu_len = len;
464 mpdu_len -= sizeof(struct ar9170_rx_head);
465 mpdu_len -= sizeof(struct ar9170_rx_tail);
466 BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != 12);
467 BUILD_BUG_ON(sizeof(struct ar9170_rx_tail) != 24);
469 if (mpdu_len <= FCS_LEN)
470 return;
472 tail = (void *)(buf + sizeof(struct ar9170_rx_head) + mpdu_len);
474 for (i = 0; i < 3; i++)
475 if (tail->rssi[i] != 0x80)
476 antennas |= BIT(i);
478 /* post-process RSSI */
479 for (i = 0; i < 7; i++)
480 if (tail->rssi[i] & 0x80)
481 tail->rssi[i] = ((tail->rssi[i] & 0x7f) + 1) & 0x7f;
483 memset(&status, 0, sizeof(status));
485 status.band = ar->channel->band;
486 status.freq = ar->channel->center_freq;
487 status.signal = ar->noise[0] + tail->rssi_combined;
488 status.noise = ar->noise[0];
489 status.antenna = antennas;
491 switch (tail->status & AR9170_RX_STATUS_MODULATION_MASK) {
492 case AR9170_RX_STATUS_MODULATION_CCK:
493 if (tail->status & AR9170_RX_STATUS_SHORT_PREAMBLE)
494 status.flag |= RX_FLAG_SHORTPRE;
495 switch (head->plcp[0]) {
496 case 0x0a:
497 status.rate_idx = 0;
498 break;
499 case 0x14:
500 status.rate_idx = 1;
501 break;
502 case 0x37:
503 status.rate_idx = 2;
504 break;
505 case 0x6e:
506 status.rate_idx = 3;
507 break;
508 default:
509 if ((!ar->sniffer_enabled) && (net_ratelimit()))
510 printk(KERN_ERR "%s: invalid plcp cck rate "
511 "(%x).\n", wiphy_name(ar->hw->wiphy),
512 head->plcp[0]);
513 return;
515 break;
516 case AR9170_RX_STATUS_MODULATION_OFDM:
517 switch (head->plcp[0] & 0xF) {
518 case 0xB:
519 status.rate_idx = 0;
520 break;
521 case 0xF:
522 status.rate_idx = 1;
523 break;
524 case 0xA:
525 status.rate_idx = 2;
526 break;
527 case 0xE:
528 status.rate_idx = 3;
529 break;
530 case 0x9:
531 status.rate_idx = 4;
532 break;
533 case 0xD:
534 status.rate_idx = 5;
535 break;
536 case 0x8:
537 status.rate_idx = 6;
538 break;
539 case 0xC:
540 status.rate_idx = 7;
541 break;
542 default:
543 if ((!ar->sniffer_enabled) && (net_ratelimit()))
544 printk(KERN_ERR "%s: invalid plcp ofdm rate "
545 "(%x).\n", wiphy_name(ar->hw->wiphy),
546 head->plcp[0]);
547 return;
549 if (status.band == IEEE80211_BAND_2GHZ)
550 status.rate_idx += 4;
551 break;
552 case AR9170_RX_STATUS_MODULATION_HT:
553 case AR9170_RX_STATUS_MODULATION_DUPOFDM:
554 /* XXX */
556 if (net_ratelimit())
557 printk(KERN_ERR "%s: invalid modulation\n",
558 wiphy_name(ar->hw->wiphy));
559 return;
562 error = tail->error;
564 if (error & AR9170_RX_ERROR_MMIC) {
565 status.flag |= RX_FLAG_MMIC_ERROR;
566 error &= ~AR9170_RX_ERROR_MMIC;
569 if (error & AR9170_RX_ERROR_PLCP) {
570 status.flag |= RX_FLAG_FAILED_PLCP_CRC;
571 error &= ~AR9170_RX_ERROR_PLCP;
574 if (error & AR9170_RX_ERROR_FCS) {
575 status.flag |= RX_FLAG_FAILED_FCS_CRC;
576 error &= ~AR9170_RX_ERROR_FCS;
579 decrypt = ar9170_get_decrypt_type(tail);
580 if (!(decrypt & AR9170_RX_ENC_SOFTWARE) &&
581 decrypt != AR9170_ENC_ALG_NONE)
582 status.flag |= RX_FLAG_DECRYPTED;
584 /* ignore wrong RA errors */
585 error &= ~AR9170_RX_ERROR_WRONG_RA;
587 if (error & AR9170_RX_ERROR_DECRYPT) {
588 error &= ~AR9170_RX_ERROR_DECRYPT;
591 * Rx decryption is done in place,
592 * the original data is lost anyway.
594 return ;
597 /* drop any other error frames */
598 if ((error) && (net_ratelimit())) {
599 printk(KERN_DEBUG "%s: errors: %#x\n",
600 wiphy_name(ar->hw->wiphy), error);
601 return;
604 buf += sizeof(struct ar9170_rx_head);
605 fc = *(__le16 *)buf;
607 if (ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc))
608 reserved = 32 + 2;
609 else
610 reserved = 32;
612 skb = dev_alloc_skb(mpdu_len + reserved);
613 if (!skb)
614 return;
616 skb_reserve(skb, reserved);
617 memcpy(skb_put(skb, mpdu_len), buf, mpdu_len);
618 ieee80211_rx_irqsafe(ar->hw, skb, &status);
621 void ar9170_rx(struct ar9170 *ar, struct sk_buff *skb)
623 unsigned int i, tlen, resplen;
624 u8 *tbuf, *respbuf;
626 tbuf = skb->data;
627 tlen = skb->len;
629 while (tlen >= 4) {
630 int clen = tbuf[1] << 8 | tbuf[0];
631 int wlen = (clen + 3) & ~3;
634 * parse stream (if any)
636 if (tbuf[2] != 0 || tbuf[3] != 0x4e) {
637 printk(KERN_ERR "%s: missing tag!\n",
638 wiphy_name(ar->hw->wiphy));
639 return ;
641 if (wlen > tlen - 4) {
642 printk(KERN_ERR "%s: invalid RX (%d, %d, %d)\n",
643 wiphy_name(ar->hw->wiphy), clen, wlen, tlen);
644 print_hex_dump(KERN_DEBUG, "data: ",
645 DUMP_PREFIX_OFFSET,
646 16, 1, tbuf, tlen, true);
647 return ;
649 resplen = clen;
650 respbuf = tbuf + 4;
651 tbuf += wlen + 4;
652 tlen -= wlen + 4;
654 i = 0;
656 /* weird thing, but this is the same in the original driver */
657 while (resplen > 2 && i < 12 &&
658 respbuf[0] == 0xff && respbuf[1] == 0xff) {
659 i += 2;
660 resplen -= 2;
661 respbuf += 2;
664 if (resplen < 4)
665 continue;
667 /* found the 6 * 0xffff marker? */
668 if (i == 12)
669 ar9170_handle_command_response(ar, respbuf, resplen);
670 else
671 ar9170_handle_mpdu(ar, respbuf, resplen);
674 if (tlen)
675 printk(KERN_ERR "%s: buffer remains!\n",
676 wiphy_name(ar->hw->wiphy));
679 #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
680 do { \
681 queue.aifs = ai_fs; \
682 queue.cw_min = cwmin; \
683 queue.cw_max = cwmax; \
684 queue.txop = _txop; \
685 } while (0)
687 static int ar9170_op_start(struct ieee80211_hw *hw)
689 struct ar9170 *ar = hw->priv;
690 int err, i;
692 mutex_lock(&ar->mutex);
694 /* reinitialize queues statistics */
695 memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
696 for (i = 0; i < ARRAY_SIZE(ar->tx_stats); i++)
697 ar->tx_stats[i].limit = 8;
699 /* reset QoS defaults */
700 AR9170_FILL_QUEUE(ar->edcf[0], 3, 15, 1023, 0); /* BEST EFFORT*/
701 AR9170_FILL_QUEUE(ar->edcf[1], 7, 15, 1023, 0); /* BACKGROUND */
702 AR9170_FILL_QUEUE(ar->edcf[2], 2, 7, 15, 94); /* VIDEO */
703 AR9170_FILL_QUEUE(ar->edcf[3], 2, 3, 7, 47); /* VOICE */
704 AR9170_FILL_QUEUE(ar->edcf[4], 2, 3, 7, 0); /* SPECIAL */
706 err = ar->open(ar);
707 if (err)
708 goto out;
710 err = ar9170_init_mac(ar);
711 if (err)
712 goto out;
714 err = ar9170_set_qos(ar);
715 if (err)
716 goto out;
718 err = ar9170_init_phy(ar, IEEE80211_BAND_2GHZ);
719 if (err)
720 goto out;
722 err = ar9170_init_rf(ar);
723 if (err)
724 goto out;
726 /* start DMA */
727 err = ar9170_write_reg(ar, 0x1c3d30, 0x100);
728 if (err)
729 goto out;
731 ar->state = AR9170_STARTED;
733 out:
734 mutex_unlock(&ar->mutex);
735 return err;
738 static void ar9170_op_stop(struct ieee80211_hw *hw)
740 struct ar9170 *ar = hw->priv;
742 if (IS_STARTED(ar))
743 ar->state = AR9170_IDLE;
745 flush_workqueue(ar->hw->workqueue);
747 mutex_lock(&ar->mutex);
748 cancel_delayed_work_sync(&ar->tx_status_janitor);
749 cancel_work_sync(&ar->filter_config_work);
750 cancel_work_sync(&ar->beacon_work);
751 skb_queue_purge(&ar->global_tx_status_waste);
752 skb_queue_purge(&ar->global_tx_status);
754 if (IS_ACCEPTING_CMD(ar)) {
755 ar9170_set_leds_state(ar, 0);
757 /* stop DMA */
758 ar9170_write_reg(ar, 0x1c3d30, 0);
759 ar->stop(ar);
762 mutex_unlock(&ar->mutex);
765 int ar9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
767 struct ar9170 *ar = hw->priv;
768 struct ieee80211_hdr *hdr;
769 struct ar9170_tx_control *txc;
770 struct ieee80211_tx_info *info;
771 struct ieee80211_rate *rate = NULL;
772 struct ieee80211_tx_rate *txrate;
773 unsigned int queue = skb_get_queue_mapping(skb);
774 unsigned long flags = 0;
775 struct ar9170_sta_info *sta_info = NULL;
776 u32 power, chains;
777 u16 keytype = 0;
778 u16 len, icv = 0;
779 int err;
780 bool tx_status;
782 if (unlikely(!IS_STARTED(ar)))
783 goto err_free;
785 hdr = (void *)skb->data;
786 info = IEEE80211_SKB_CB(skb);
787 len = skb->len;
789 spin_lock_irqsave(&ar->tx_stats_lock, flags);
790 if (ar->tx_stats[queue].limit < ar->tx_stats[queue].len) {
791 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
792 return NETDEV_TX_OK;
795 ar->tx_stats[queue].len++;
796 ar->tx_stats[queue].count++;
797 if (ar->tx_stats[queue].limit == ar->tx_stats[queue].len)
798 ieee80211_stop_queue(hw, queue);
800 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
802 txc = (void *)skb_push(skb, sizeof(*txc));
804 tx_status = (((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) != 0) ||
805 ((info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) != 0));
807 if (info->control.hw_key) {
808 icv = info->control.hw_key->icv_len;
810 switch (info->control.hw_key->alg) {
811 case ALG_WEP:
812 keytype = AR9170_TX_MAC_ENCR_RC4;
813 break;
814 case ALG_TKIP:
815 keytype = AR9170_TX_MAC_ENCR_RC4;
816 break;
817 case ALG_CCMP:
818 keytype = AR9170_TX_MAC_ENCR_AES;
819 break;
820 default:
821 WARN_ON(1);
822 goto err_dequeue;
826 /* Length */
827 txc->length = cpu_to_le16(len + icv + 4);
829 txc->mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
830 AR9170_TX_MAC_BACKOFF);
831 txc->mac_control |= cpu_to_le16(ar9170_qos_hwmap[queue] <<
832 AR9170_TX_MAC_QOS_SHIFT);
833 txc->mac_control |= cpu_to_le16(keytype);
834 txc->phy_control = cpu_to_le32(0);
836 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
837 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
839 if (info->flags & IEEE80211_TX_CTL_AMPDU)
840 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_AGGR);
842 txrate = &info->control.rates[0];
844 if (txrate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
845 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
846 else if (txrate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
847 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
849 if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
850 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
852 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
853 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
855 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
856 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ);
857 /* this works because 40 MHz is 2 and dup is 3 */
858 if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
859 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP);
861 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
862 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
864 if (txrate->flags & IEEE80211_TX_RC_MCS) {
865 u32 r = txrate->idx;
866 u8 *txpower;
868 r <<= AR9170_TX_PHY_MCS_SHIFT;
869 if (WARN_ON(r & ~AR9170_TX_PHY_MCS_MASK))
870 goto err_dequeue;
871 txc->phy_control |= cpu_to_le32(r & AR9170_TX_PHY_MCS_MASK);
872 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
874 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
875 if (info->band == IEEE80211_BAND_5GHZ)
876 txpower = ar->power_5G_ht40;
877 else
878 txpower = ar->power_2G_ht40;
879 } else {
880 if (info->band == IEEE80211_BAND_5GHZ)
881 txpower = ar->power_5G_ht20;
882 else
883 txpower = ar->power_2G_ht20;
886 power = txpower[(txrate->idx) & 7];
887 } else {
888 u8 *txpower;
889 u32 mod;
890 u32 phyrate;
891 u8 idx = txrate->idx;
893 if (info->band != IEEE80211_BAND_2GHZ) {
894 idx += 4;
895 txpower = ar->power_5G_leg;
896 mod = AR9170_TX_PHY_MOD_OFDM;
897 } else {
898 if (idx < 4) {
899 txpower = ar->power_2G_cck;
900 mod = AR9170_TX_PHY_MOD_CCK;
901 } else {
902 mod = AR9170_TX_PHY_MOD_OFDM;
903 txpower = ar->power_2G_ofdm;
907 rate = &__ar9170_ratetable[idx];
909 phyrate = rate->hw_value & 0xF;
910 power = txpower[(rate->hw_value & 0x30) >> 4];
911 phyrate <<= AR9170_TX_PHY_MCS_SHIFT;
913 txc->phy_control |= cpu_to_le32(mod);
914 txc->phy_control |= cpu_to_le32(phyrate);
917 power <<= AR9170_TX_PHY_TX_PWR_SHIFT;
918 power &= AR9170_TX_PHY_TX_PWR_MASK;
919 txc->phy_control |= cpu_to_le32(power);
921 /* set TX chains */
922 if (ar->eeprom.tx_mask == 1) {
923 chains = AR9170_TX_PHY_TXCHAIN_1;
924 } else {
925 chains = AR9170_TX_PHY_TXCHAIN_2;
927 /* >= 36M legacy OFDM - use only one chain */
928 if (rate && rate->bitrate >= 360)
929 chains = AR9170_TX_PHY_TXCHAIN_1;
931 txc->phy_control |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_SHIFT);
933 if (tx_status) {
934 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE);
936 * WARNING:
937 * Putting the QoS queue bits into an unexplored territory is
938 * certainly not elegant.
940 * In my defense: This idea provides a reasonable way to
941 * smuggle valuable information to the tx_status callback.
942 * Also, the idea behind this bit-abuse came straight from
943 * the original driver code.
946 txc->phy_control |=
947 cpu_to_le32(queue << AR9170_TX_PHY_QOS_SHIFT);
949 if (info->control.sta) {
950 sta_info = (void *) info->control.sta->drv_priv;
951 skb_queue_tail(&sta_info->tx_status[queue], skb);
952 } else {
953 skb_queue_tail(&ar->global_tx_status, skb);
955 queue_delayed_work(ar->hw->workqueue,
956 &ar->tx_status_janitor,
957 msecs_to_jiffies(100));
961 err = ar->tx(ar, skb, tx_status, 0);
962 if (unlikely(tx_status && err)) {
963 if (info->control.sta)
964 skb_unlink(skb, &sta_info->tx_status[queue]);
965 else
966 skb_unlink(skb, &ar->global_tx_status);
969 return NETDEV_TX_OK;
971 err_dequeue:
972 spin_lock_irqsave(&ar->tx_stats_lock, flags);
973 ar->tx_stats[queue].len--;
974 ar->tx_stats[queue].count--;
975 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
977 err_free:
978 dev_kfree_skb(skb);
979 return NETDEV_TX_OK;
982 static int ar9170_op_add_interface(struct ieee80211_hw *hw,
983 struct ieee80211_if_init_conf *conf)
985 struct ar9170 *ar = hw->priv;
986 int err = 0;
988 mutex_lock(&ar->mutex);
990 if (ar->vif) {
991 err = -EBUSY;
992 goto unlock;
995 ar->vif = conf->vif;
996 memcpy(ar->mac_addr, conf->mac_addr, ETH_ALEN);
998 if (modparam_nohwcrypt || (ar->vif->type != NL80211_IFTYPE_STATION)) {
999 ar->rx_software_decryption = true;
1000 ar->disable_offload = true;
1003 ar->cur_filter = 0;
1004 ar->want_filter = AR9170_MAC_REG_FTF_DEFAULTS;
1005 err = ar9170_update_frame_filter(ar);
1006 if (err)
1007 goto unlock;
1009 err = ar9170_set_operating_mode(ar);
1011 unlock:
1012 mutex_unlock(&ar->mutex);
1013 return err;
1016 static void ar9170_op_remove_interface(struct ieee80211_hw *hw,
1017 struct ieee80211_if_init_conf *conf)
1019 struct ar9170 *ar = hw->priv;
1021 mutex_lock(&ar->mutex);
1022 ar->vif = NULL;
1023 ar->want_filter = 0;
1024 ar9170_update_frame_filter(ar);
1025 ar9170_set_beacon_timers(ar);
1026 dev_kfree_skb(ar->beacon);
1027 ar->beacon = NULL;
1028 ar->sniffer_enabled = false;
1029 ar->rx_software_decryption = false;
1030 ar9170_set_operating_mode(ar);
1031 mutex_unlock(&ar->mutex);
1034 static int ar9170_op_config(struct ieee80211_hw *hw, u32 changed)
1036 struct ar9170 *ar = hw->priv;
1037 int err = 0;
1039 mutex_lock(&ar->mutex);
1041 if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
1042 /* TODO */
1043 err = 0;
1046 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
1047 /* TODO */
1048 err = 0;
1051 if (changed & IEEE80211_CONF_CHANGE_PS) {
1052 /* TODO */
1053 err = 0;
1056 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1057 /* TODO */
1058 err = 0;
1061 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
1063 * is it long_frame_max_tx_count or short_frame_max_tx_count?
1066 err = ar9170_set_hwretry_limit(ar,
1067 ar->hw->conf.long_frame_max_tx_count);
1068 if (err)
1069 goto out;
1072 if (changed & IEEE80211_CONF_CHANGE_BEACON_INTERVAL) {
1073 err = ar9170_set_beacon_timers(ar);
1074 if (err)
1075 goto out;
1078 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1079 err = ar9170_set_channel(ar, hw->conf.channel,
1080 AR9170_RFI_NONE, AR9170_BW_20);
1081 if (err)
1082 goto out;
1083 /* adjust slot time for 5 GHz */
1084 if (hw->conf.channel->band == IEEE80211_BAND_5GHZ)
1085 err = ar9170_write_reg(ar, AR9170_MAC_REG_SLOT_TIME,
1086 9 << 10);
1089 out:
1090 mutex_unlock(&ar->mutex);
1091 return err;
1094 static int ar9170_op_config_interface(struct ieee80211_hw *hw,
1095 struct ieee80211_vif *vif,
1096 struct ieee80211_if_conf *conf)
1098 struct ar9170 *ar = hw->priv;
1099 int err = 0;
1101 mutex_lock(&ar->mutex);
1103 if (conf->changed & IEEE80211_IFCC_BSSID) {
1104 memcpy(ar->bssid, conf->bssid, ETH_ALEN);
1105 err = ar9170_set_operating_mode(ar);
1108 if (conf->changed & IEEE80211_IFCC_BEACON) {
1109 err = ar9170_update_beacon(ar);
1111 if (err)
1112 goto out;
1113 err = ar9170_set_beacon_timers(ar);
1116 out:
1117 mutex_unlock(&ar->mutex);
1118 return err;
1121 static void ar9170_set_filters(struct work_struct *work)
1123 struct ar9170 *ar = container_of(work, struct ar9170,
1124 filter_config_work);
1125 int err;
1127 if (unlikely(!IS_STARTED(ar)))
1128 return ;
1130 mutex_lock(&ar->mutex);
1131 if (ar->filter_changed & AR9170_FILTER_CHANGED_PROMISC) {
1132 err = ar9170_set_operating_mode(ar);
1133 if (err)
1134 goto unlock;
1137 if (ar->filter_changed & AR9170_FILTER_CHANGED_MULTICAST) {
1138 err = ar9170_update_multicast(ar);
1139 if (err)
1140 goto unlock;
1143 if (ar->filter_changed & AR9170_FILTER_CHANGED_FRAMEFILTER)
1144 err = ar9170_update_frame_filter(ar);
1146 unlock:
1147 mutex_unlock(&ar->mutex);
1150 static void ar9170_op_configure_filter(struct ieee80211_hw *hw,
1151 unsigned int changed_flags,
1152 unsigned int *new_flags,
1153 int mc_count, struct dev_mc_list *mclist)
1155 struct ar9170 *ar = hw->priv;
1157 /* mask supported flags */
1158 *new_flags &= FIF_ALLMULTI | FIF_CONTROL | FIF_BCN_PRBRESP_PROMISC |
1159 FIF_PROMISC_IN_BSS;
1162 * We can support more by setting the sniffer bit and
1163 * then checking the error flags, later.
1166 if (changed_flags & FIF_ALLMULTI) {
1167 if (*new_flags & FIF_ALLMULTI) {
1168 ar->want_mc_hash = ~0ULL;
1169 } else {
1170 u64 mchash;
1171 int i;
1173 /* always get broadcast frames */
1174 mchash = 1ULL << (0xff>>2);
1176 for (i = 0; i < mc_count; i++) {
1177 if (WARN_ON(!mclist))
1178 break;
1179 mchash |= 1ULL << (mclist->dmi_addr[5] >> 2);
1180 mclist = mclist->next;
1182 ar->want_mc_hash = mchash;
1184 ar->filter_changed |= AR9170_FILTER_CHANGED_MULTICAST;
1187 if (changed_flags & FIF_CONTROL) {
1188 u32 filter = AR9170_MAC_REG_FTF_PSPOLL |
1189 AR9170_MAC_REG_FTF_RTS |
1190 AR9170_MAC_REG_FTF_CTS |
1191 AR9170_MAC_REG_FTF_ACK |
1192 AR9170_MAC_REG_FTF_CFE |
1193 AR9170_MAC_REG_FTF_CFE_ACK;
1195 if (*new_flags & FIF_CONTROL)
1196 ar->want_filter = ar->cur_filter | filter;
1197 else
1198 ar->want_filter = ar->cur_filter & ~filter;
1200 ar->filter_changed |= AR9170_FILTER_CHANGED_FRAMEFILTER;
1203 if (changed_flags & FIF_PROMISC_IN_BSS) {
1204 ar->sniffer_enabled = ((*new_flags) & FIF_PROMISC_IN_BSS) != 0;
1205 ar->filter_changed |= AR9170_FILTER_CHANGED_PROMISC;
1208 if (likely(IS_STARTED(ar)))
1209 queue_work(ar->hw->workqueue, &ar->filter_config_work);
1212 static void ar9170_op_bss_info_changed(struct ieee80211_hw *hw,
1213 struct ieee80211_vif *vif,
1214 struct ieee80211_bss_conf *bss_conf,
1215 u32 changed)
1217 struct ar9170 *ar = hw->priv;
1218 int err = 0;
1220 mutex_lock(&ar->mutex);
1222 ar9170_regwrite_begin(ar);
1224 if (changed & BSS_CHANGED_ASSOC) {
1225 ar->state = bss_conf->assoc ? AR9170_ASSOCIATED : ar->state;
1227 #ifndef CONFIG_AR9170_LEDS
1228 /* enable assoc LED. */
1229 err = ar9170_set_leds_state(ar, bss_conf->assoc ? 2 : 0);
1230 #endif /* CONFIG_AR9170_LEDS */
1233 if (changed & BSS_CHANGED_HT) {
1234 /* TODO */
1235 err = 0;
1238 if (changed & BSS_CHANGED_ERP_SLOT) {
1239 u32 slottime = 20;
1241 if (bss_conf->use_short_slot)
1242 slottime = 9;
1244 ar9170_regwrite(AR9170_MAC_REG_SLOT_TIME, slottime << 10);
1247 if (changed & BSS_CHANGED_BASIC_RATES) {
1248 u32 cck, ofdm;
1250 if (hw->conf.channel->band == IEEE80211_BAND_5GHZ) {
1251 ofdm = bss_conf->basic_rates;
1252 cck = 0;
1253 } else {
1254 /* four cck rates */
1255 cck = bss_conf->basic_rates & 0xf;
1256 ofdm = bss_conf->basic_rates >> 4;
1258 ar9170_regwrite(AR9170_MAC_REG_BASIC_RATE,
1259 ofdm << 8 | cck);
1262 ar9170_regwrite_finish();
1263 err = ar9170_regwrite_result();
1264 mutex_unlock(&ar->mutex);
1267 static u64 ar9170_op_get_tsf(struct ieee80211_hw *hw)
1269 struct ar9170 *ar = hw->priv;
1270 int err;
1271 u32 tsf_low;
1272 u32 tsf_high;
1273 u64 tsf;
1275 mutex_lock(&ar->mutex);
1276 err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_L, &tsf_low);
1277 if (!err)
1278 err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_H, &tsf_high);
1279 mutex_unlock(&ar->mutex);
1281 if (WARN_ON(err))
1282 return 0;
1284 tsf = tsf_high;
1285 tsf = (tsf << 32) | tsf_low;
1286 return tsf;
1289 static int ar9170_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1290 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1291 struct ieee80211_key_conf *key)
1293 struct ar9170 *ar = hw->priv;
1294 int err = 0, i;
1295 u8 ktype;
1297 if ((!ar->vif) || (ar->disable_offload))
1298 return -EOPNOTSUPP;
1300 switch (key->alg) {
1301 case ALG_WEP:
1302 if (key->keylen == LEN_WEP40)
1303 ktype = AR9170_ENC_ALG_WEP64;
1304 else
1305 ktype = AR9170_ENC_ALG_WEP128;
1306 break;
1307 case ALG_TKIP:
1308 ktype = AR9170_ENC_ALG_TKIP;
1309 break;
1310 case ALG_CCMP:
1311 ktype = AR9170_ENC_ALG_AESCCMP;
1312 break;
1313 default:
1314 return -EOPNOTSUPP;
1317 mutex_lock(&ar->mutex);
1318 if (cmd == SET_KEY) {
1319 if (unlikely(!IS_STARTED(ar))) {
1320 err = -EOPNOTSUPP;
1321 goto out;
1324 /* group keys need all-zeroes address */
1325 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1326 sta = NULL;
1328 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
1329 for (i = 0; i < 64; i++)
1330 if (!(ar->usedkeys & BIT(i)))
1331 break;
1332 if (i == 64) {
1333 ar->rx_software_decryption = true;
1334 ar9170_set_operating_mode(ar);
1335 err = -ENOSPC;
1336 goto out;
1338 } else {
1339 i = 64 + key->keyidx;
1342 key->hw_key_idx = i;
1344 err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL, ktype, 0,
1345 key->key, min_t(u8, 16, key->keylen));
1346 if (err)
1347 goto out;
1349 if (key->alg == ALG_TKIP) {
1350 err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL,
1351 ktype, 1, key->key + 16, 16);
1352 if (err)
1353 goto out;
1356 * hardware is not capable generating the MMIC
1357 * for fragmented frames!
1359 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1362 if (i < 64)
1363 ar->usedkeys |= BIT(i);
1365 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1366 } else {
1367 if (unlikely(!IS_STARTED(ar))) {
1368 /* The device is gone... together with the key ;-) */
1369 err = 0;
1370 goto out;
1373 err = ar9170_disable_key(ar, key->hw_key_idx);
1374 if (err)
1375 goto out;
1377 if (key->hw_key_idx < 64) {
1378 ar->usedkeys &= ~BIT(key->hw_key_idx);
1379 } else {
1380 err = ar9170_upload_key(ar, key->hw_key_idx, NULL,
1381 AR9170_ENC_ALG_NONE, 0,
1382 NULL, 0);
1383 if (err)
1384 goto out;
1386 if (key->alg == ALG_TKIP) {
1387 err = ar9170_upload_key(ar, key->hw_key_idx,
1388 NULL,
1389 AR9170_ENC_ALG_NONE, 1,
1390 NULL, 0);
1391 if (err)
1392 goto out;
1398 ar9170_regwrite_begin(ar);
1399 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L, ar->usedkeys);
1400 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H, ar->usedkeys >> 32);
1401 ar9170_regwrite_finish();
1402 err = ar9170_regwrite_result();
1404 out:
1405 mutex_unlock(&ar->mutex);
1407 return err;
1410 static void ar9170_sta_notify(struct ieee80211_hw *hw,
1411 struct ieee80211_vif *vif,
1412 enum sta_notify_cmd cmd,
1413 struct ieee80211_sta *sta)
1415 struct ar9170 *ar = hw->priv;
1416 struct ar9170_sta_info *info = (void *) sta->drv_priv;
1417 struct sk_buff *skb;
1418 unsigned int i;
1420 switch (cmd) {
1421 case STA_NOTIFY_ADD:
1422 for (i = 0; i < ar->hw->queues; i++)
1423 skb_queue_head_init(&info->tx_status[i]);
1424 break;
1426 case STA_NOTIFY_REMOVE:
1429 * transfer all outstanding frames that need a tx_status
1430 * reports to the global tx_status queue
1433 for (i = 0; i < ar->hw->queues; i++) {
1434 while ((skb = skb_dequeue(&info->tx_status[i]))) {
1435 #ifdef AR9170_QUEUE_DEBUG
1436 printk(KERN_DEBUG "%s: queueing frame in "
1437 "global tx_status queue =>\n",
1438 wiphy_name(ar->hw->wiphy));
1440 ar9170_print_txheader(ar, skb);
1441 #endif /* AR9170_QUEUE_DEBUG */
1442 skb_queue_tail(&ar->global_tx_status, skb);
1445 queue_delayed_work(ar->hw->workqueue, &ar->tx_status_janitor,
1446 msecs_to_jiffies(100));
1447 break;
1449 default:
1450 break;
1454 static int ar9170_get_stats(struct ieee80211_hw *hw,
1455 struct ieee80211_low_level_stats *stats)
1457 struct ar9170 *ar = hw->priv;
1458 u32 val;
1459 int err;
1461 mutex_lock(&ar->mutex);
1462 err = ar9170_read_reg(ar, AR9170_MAC_REG_TX_RETRY, &val);
1463 ar->stats.dot11ACKFailureCount += val;
1465 memcpy(stats, &ar->stats, sizeof(*stats));
1466 mutex_unlock(&ar->mutex);
1468 return 0;
1471 static int ar9170_get_tx_stats(struct ieee80211_hw *hw,
1472 struct ieee80211_tx_queue_stats *tx_stats)
1474 struct ar9170 *ar = hw->priv;
1476 spin_lock_bh(&ar->tx_stats_lock);
1477 memcpy(tx_stats, ar->tx_stats, sizeof(tx_stats[0]) * hw->queues);
1478 spin_unlock_bh(&ar->tx_stats_lock);
1480 return 0;
1483 static int ar9170_conf_tx(struct ieee80211_hw *hw, u16 queue,
1484 const struct ieee80211_tx_queue_params *param)
1486 struct ar9170 *ar = hw->priv;
1487 int ret;
1489 mutex_lock(&ar->mutex);
1490 if ((param) && !(queue > ar->hw->queues)) {
1491 memcpy(&ar->edcf[ar9170_qos_hwmap[queue]],
1492 param, sizeof(*param));
1494 ret = ar9170_set_qos(ar);
1495 } else
1496 ret = -EINVAL;
1498 mutex_unlock(&ar->mutex);
1499 return ret;
1502 static const struct ieee80211_ops ar9170_ops = {
1503 .start = ar9170_op_start,
1504 .stop = ar9170_op_stop,
1505 .tx = ar9170_op_tx,
1506 .add_interface = ar9170_op_add_interface,
1507 .remove_interface = ar9170_op_remove_interface,
1508 .config = ar9170_op_config,
1509 .config_interface = ar9170_op_config_interface,
1510 .configure_filter = ar9170_op_configure_filter,
1511 .conf_tx = ar9170_conf_tx,
1512 .bss_info_changed = ar9170_op_bss_info_changed,
1513 .get_tsf = ar9170_op_get_tsf,
1514 .set_key = ar9170_set_key,
1515 .sta_notify = ar9170_sta_notify,
1516 .get_stats = ar9170_get_stats,
1517 .get_tx_stats = ar9170_get_tx_stats,
1520 void *ar9170_alloc(size_t priv_size)
1522 struct ieee80211_hw *hw;
1523 struct ar9170 *ar;
1524 int i;
1526 hw = ieee80211_alloc_hw(priv_size, &ar9170_ops);
1527 if (!hw)
1528 return ERR_PTR(-ENOMEM);
1530 ar = hw->priv;
1531 ar->hw = hw;
1533 mutex_init(&ar->mutex);
1534 spin_lock_init(&ar->cmdlock);
1535 spin_lock_init(&ar->tx_stats_lock);
1536 skb_queue_head_init(&ar->global_tx_status);
1537 skb_queue_head_init(&ar->global_tx_status_waste);
1538 INIT_WORK(&ar->filter_config_work, ar9170_set_filters);
1539 INIT_WORK(&ar->beacon_work, ar9170_new_beacon);
1540 INIT_DELAYED_WORK(&ar->tx_status_janitor, ar9170_tx_status_janitor);
1542 /* all hw supports 2.4 GHz, so set channel to 1 by default */
1543 ar->channel = &ar9170_2ghz_chantable[0];
1545 /* first part of wiphy init */
1546 ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1547 BIT(NL80211_IFTYPE_WDS) |
1548 BIT(NL80211_IFTYPE_ADHOC);
1549 ar->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
1550 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
1551 IEEE80211_HW_SIGNAL_DBM |
1552 IEEE80211_HW_NOISE_DBM;
1554 ar->hw->queues = __AR9170_NUM_TXQ;
1555 ar->hw->extra_tx_headroom = 8;
1556 ar->hw->sta_data_size = sizeof(struct ar9170_sta_info);
1558 ar->hw->max_rates = 1;
1559 ar->hw->max_rate_tries = 3;
1561 for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
1562 ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1564 return ar;
1567 static int ar9170_read_eeprom(struct ar9170 *ar)
1569 #define RW 8 /* number of words to read at once */
1570 #define RB (sizeof(u32) * RW)
1571 DECLARE_MAC_BUF(mbuf);
1572 u8 *eeprom = (void *)&ar->eeprom;
1573 u8 *addr = ar->eeprom.mac_address;
1574 __le32 offsets[RW];
1575 int i, j, err, bands = 0;
1577 BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
1579 BUILD_BUG_ON(RB > AR9170_MAX_CMD_LEN - 4);
1580 #ifndef __CHECKER__
1581 /* don't want to handle trailing remains */
1582 BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
1583 #endif
1585 for (i = 0; i < sizeof(ar->eeprom)/RB; i++) {
1586 for (j = 0; j < RW; j++)
1587 offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
1588 RB * i + 4 * j);
1590 err = ar->exec_cmd(ar, AR9170_CMD_RREG,
1591 RB, (u8 *) &offsets,
1592 RB, eeprom + RB * i);
1593 if (err)
1594 return err;
1597 #undef RW
1598 #undef RB
1600 if (ar->eeprom.length == cpu_to_le16(0xFFFF))
1601 return -ENODATA;
1603 if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
1604 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar9170_band_2GHz;
1605 bands++;
1607 if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
1608 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar9170_band_5GHz;
1609 bands++;
1612 * I measured this, a bandswitch takes roughly
1613 * 135 ms and a frequency switch about 80.
1615 * FIXME: measure these values again once EEPROM settings
1616 * are used, that will influence them!
1618 if (bands == 2)
1619 ar->hw->channel_change_time = 135 * 1000;
1620 else
1621 ar->hw->channel_change_time = 80 * 1000;
1623 ar->regulatory.current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
1624 ar->regulatory.current_rd_ext = le16_to_cpu(ar->eeprom.reg_domain[1]);
1626 /* second part of wiphy init */
1627 SET_IEEE80211_PERM_ADDR(ar->hw, addr);
1629 return bands ? 0 : -EINVAL;
1632 static int ar9170_reg_notifier(struct wiphy *wiphy,
1633 struct regulatory_request *request)
1635 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1636 struct ar9170 *ar = hw->priv;
1638 return ath_reg_notifier_apply(wiphy, request, &ar->regulatory);
1641 int ar9170_register(struct ar9170 *ar, struct device *pdev)
1643 int err;
1645 /* try to read EEPROM, init MAC addr */
1646 err = ar9170_read_eeprom(ar);
1647 if (err)
1648 goto err_out;
1650 err = ath_regd_init(&ar->regulatory, ar->hw->wiphy,
1651 ar9170_reg_notifier);
1653 err = ieee80211_register_hw(ar->hw);
1654 if (err)
1655 goto err_out;
1657 if (!ath_is_world_regd(&ar->regulatory))
1658 regulatory_hint(ar->hw->wiphy, ar->regulatory.alpha2);
1660 err = ar9170_init_leds(ar);
1661 if (err)
1662 goto err_unreg;
1664 #ifdef CONFIG_AR9170_LEDS
1665 err = ar9170_register_leds(ar);
1666 if (err)
1667 goto err_unreg;
1668 #endif /* CONFIG_AR9170_LEDS */
1670 dev_info(pdev, "Atheros AR9170 is registered as '%s'\n",
1671 wiphy_name(ar->hw->wiphy));
1673 return err;
1675 err_unreg:
1676 ieee80211_unregister_hw(ar->hw);
1678 err_out:
1679 return err;
1682 void ar9170_unregister(struct ar9170 *ar)
1684 #ifdef CONFIG_AR9170_LEDS
1685 ar9170_unregister_leds(ar);
1686 #endif /* CONFIG_AR9170_LEDS */
1688 ieee80211_unregister_hw(ar->hw);
1689 mutex_destroy(&ar->mutex);