ath5k: HW code cleanup
[deliverable/linux.git] / drivers / net / wireless / zd1211rw / zd_mac.c
CommitLineData
66bb42fd 1/* ZD1211 USB-WLAN driver for Linux
459c51ad 2 *
66bb42fd
DD
3 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
4 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
5 * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
459c51ad 6 * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
e85d0918
DD
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
e85d0918
DD
25#include <linux/usb.h>
26#include <linux/jiffies.h>
27#include <net/ieee80211_radiotap.h>
28
29#include "zd_def.h"
30#include "zd_chip.h"
31#include "zd_mac.h"
32#include "zd_ieee80211.h"
e85d0918 33#include "zd_rf.h"
e85d0918 34
459c51ad
DD
35/* This table contains the hardware specific values for the modulation rates. */
36static const struct ieee80211_rate zd_rates[] = {
8318d78a
JB
37 { .bitrate = 10,
38 .hw_value = ZD_CCK_RATE_1M, },
39 { .bitrate = 20,
40 .hw_value = ZD_CCK_RATE_2M,
41 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
42 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
43 { .bitrate = 55,
44 .hw_value = ZD_CCK_RATE_5_5M,
45 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
46 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
47 { .bitrate = 110,
48 .hw_value = ZD_CCK_RATE_11M,
49 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
50 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
51 { .bitrate = 60,
52 .hw_value = ZD_OFDM_RATE_6M,
53 .flags = 0 },
54 { .bitrate = 90,
55 .hw_value = ZD_OFDM_RATE_9M,
56 .flags = 0 },
57 { .bitrate = 120,
58 .hw_value = ZD_OFDM_RATE_12M,
59 .flags = 0 },
60 { .bitrate = 180,
61 .hw_value = ZD_OFDM_RATE_18M,
62 .flags = 0 },
63 { .bitrate = 240,
64 .hw_value = ZD_OFDM_RATE_24M,
65 .flags = 0 },
66 { .bitrate = 360,
67 .hw_value = ZD_OFDM_RATE_36M,
68 .flags = 0 },
69 { .bitrate = 480,
70 .hw_value = ZD_OFDM_RATE_48M,
71 .flags = 0 },
72 { .bitrate = 540,
73 .hw_value = ZD_OFDM_RATE_54M,
74 .flags = 0 },
459c51ad
DD
75};
76
77static const struct ieee80211_channel zd_channels[] = {
8318d78a
JB
78 { .center_freq = 2412, .hw_value = 1 },
79 { .center_freq = 2417, .hw_value = 2 },
80 { .center_freq = 2422, .hw_value = 3 },
81 { .center_freq = 2427, .hw_value = 4 },
82 { .center_freq = 2432, .hw_value = 5 },
83 { .center_freq = 2437, .hw_value = 6 },
84 { .center_freq = 2442, .hw_value = 7 },
85 { .center_freq = 2447, .hw_value = 8 },
86 { .center_freq = 2452, .hw_value = 9 },
87 { .center_freq = 2457, .hw_value = 10 },
88 { .center_freq = 2462, .hw_value = 11 },
89 { .center_freq = 2467, .hw_value = 12 },
90 { .center_freq = 2472, .hw_value = 13 },
91 { .center_freq = 2484, .hw_value = 14 },
459c51ad 92};
e85d0918 93
583afd1e
UK
94static void housekeeping_init(struct zd_mac *mac);
95static void housekeeping_enable(struct zd_mac *mac);
96static void housekeeping_disable(struct zd_mac *mac);
97
459c51ad 98int zd_mac_preinit_hw(struct ieee80211_hw *hw)
e85d0918
DD
99{
100 int r;
e85d0918 101 u8 addr[ETH_ALEN];
459c51ad 102 struct zd_mac *mac = zd_hw_mac(hw);
74553aed
DD
103
104 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
105 if (r)
106 return r;
107
459c51ad
DD
108 SET_IEEE80211_PERM_ADDR(hw, addr);
109
74553aed
DD
110 return 0;
111}
112
459c51ad 113int zd_mac_init_hw(struct ieee80211_hw *hw)
74553aed
DD
114{
115 int r;
459c51ad 116 struct zd_mac *mac = zd_hw_mac(hw);
74553aed 117 struct zd_chip *chip = &mac->chip;
e85d0918
DD
118 u8 default_regdomain;
119
120 r = zd_chip_enable_int(chip);
121 if (r)
122 goto out;
74553aed 123 r = zd_chip_init_hw(chip);
e85d0918
DD
124 if (r)
125 goto disable_int;
126
e85d0918 127 ZD_ASSERT(!irqs_disabled());
e85d0918
DD
128
129 r = zd_read_regdomain(chip, &default_regdomain);
130 if (r)
131 goto disable_int;
e85d0918
DD
132 spin_lock_irq(&mac->lock);
133 mac->regdomain = mac->default_regdomain = default_regdomain;
134 spin_unlock_irq(&mac->lock);
e85d0918 135
40da08bc
DD
136 /* We must inform the device that we are doing encryption/decryption in
137 * software at the moment. */
138 r = zd_set_encryption_type(chip, ENC_SNIFFER);
e85d0918
DD
139 if (r)
140 goto disable_int;
141
459c51ad 142 zd_geo_init(hw, mac->regdomain);
e85d0918
DD
143
144 r = 0;
145disable_int:
146 zd_chip_disable_int(chip);
147out:
148 return r;
149}
150
151void zd_mac_clear(struct zd_mac *mac)
152{
9cdac965 153 flush_workqueue(zd_workqueue);
e85d0918 154 zd_chip_clear(&mac->chip);
c48cf125
UK
155 ZD_ASSERT(!spin_is_locked(&mac->lock));
156 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
e85d0918
DD
157}
158
c5691235 159static int set_rx_filter(struct zd_mac *mac)
e85d0918 160{
459c51ad
DD
161 unsigned long flags;
162 u32 filter = STA_RX_FILTER;
e85d0918 163
459c51ad
DD
164 spin_lock_irqsave(&mac->lock, flags);
165 if (mac->pass_ctrl)
166 filter |= RX_FILTER_CTRL;
167 spin_unlock_irqrestore(&mac->lock, flags);
168
169 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
c5691235
UK
170}
171
172static int set_mc_hash(struct zd_mac *mac)
173{
174 struct zd_mc_hash hash;
c5691235 175 zd_mc_clear(&hash);
c5691235
UK
176 return zd_chip_set_multicast_hash(&mac->chip, &hash);
177}
178
459c51ad 179static int zd_op_start(struct ieee80211_hw *hw)
e85d0918 180{
459c51ad 181 struct zd_mac *mac = zd_hw_mac(hw);
e85d0918 182 struct zd_chip *chip = &mac->chip;
74553aed 183 struct zd_usb *usb = &chip->usb;
e85d0918
DD
184 int r;
185
74553aed
DD
186 if (!usb->initialized) {
187 r = zd_usb_init_hw(usb);
188 if (r)
189 goto out;
190 }
191
e85d0918
DD
192 r = zd_chip_enable_int(chip);
193 if (r < 0)
194 goto out;
195
196 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
197 if (r < 0)
198 goto disable_int;
c5691235 199 r = set_rx_filter(mac);
c5691235
UK
200 if (r)
201 goto disable_int;
202 r = set_mc_hash(mac);
e85d0918
DD
203 if (r)
204 goto disable_int;
205 r = zd_chip_switch_radio_on(chip);
206 if (r < 0)
207 goto disable_int;
459c51ad 208 r = zd_chip_enable_rxtx(chip);
e85d0918
DD
209 if (r < 0)
210 goto disable_radio;
211 r = zd_chip_enable_hwint(chip);
212 if (r < 0)
459c51ad 213 goto disable_rxtx;
e85d0918 214
583afd1e 215 housekeeping_enable(mac);
e85d0918 216 return 0;
459c51ad
DD
217disable_rxtx:
218 zd_chip_disable_rxtx(chip);
e85d0918
DD
219disable_radio:
220 zd_chip_switch_radio_off(chip);
221disable_int:
222 zd_chip_disable_int(chip);
223out:
224 return r;
225}
226
459c51ad
DD
227static void zd_op_stop(struct ieee80211_hw *hw)
228{
229 struct zd_mac *mac = zd_hw_mac(hw);
230 struct zd_chip *chip = &mac->chip;
231 struct sk_buff *skb;
232 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
c9a4b35d 233
459c51ad 234 /* The order here deliberately is a little different from the open()
e85d0918 235 * method, since we need to make sure there is no opportunity for RX
459c51ad 236 * frames to be processed by mac80211 after we have stopped it.
e85d0918
DD
237 */
238
459c51ad 239 zd_chip_disable_rxtx(chip);
583afd1e 240 housekeeping_disable(mac);
b1382ede 241 flush_workqueue(zd_workqueue);
b1382ede 242
e85d0918
DD
243 zd_chip_disable_hwint(chip);
244 zd_chip_switch_radio_off(chip);
245 zd_chip_disable_int(chip);
246
e85d0918 247
459c51ad 248 while ((skb = skb_dequeue(ack_wait_queue)))
e039fa4a 249 dev_kfree_skb_any(skb);
e85d0918
DD
250}
251
459c51ad
DD
252/**
253 * tx_status - reports tx status of a packet if required
254 * @hw - a &struct ieee80211_hw pointer
255 * @skb - a sk-buffer
e039fa4a
JB
256 * @flags: extra flags to set in the TX status info
257 * @ackssi: ACK signal strength
459c51ad
DD
258 * @success - True for successfull transmission of the frame
259 *
260 * This information calls ieee80211_tx_status_irqsafe() if required by the
261 * control information. It copies the control information into the status
262 * information.
263 *
264 * If no status information has been requested, the skb is freed.
265 */
266static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
e039fa4a 267 u32 flags, int ackssi, bool success)
b1382ede 268{
e039fa4a
JB
269 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
270
271 memset(&info->status, 0, sizeof(info->status));
b1382ede 272
459c51ad 273 if (!success)
e039fa4a
JB
274 info->status.excessive_retries = 1;
275 info->flags |= flags;
276 info->status.ack_signal = ackssi;
277 ieee80211_tx_status_irqsafe(hw, skb);
b1382ede
DD
278}
279
459c51ad
DD
280/**
281 * zd_mac_tx_failed - callback for failed frames
282 * @dev: the mac80211 wireless device
283 *
284 * This function is called if a frame couldn't be succesfully be
285 * transferred. The first frame from the tx queue, will be selected and
286 * reported as error to the upper layers.
287 */
288void zd_mac_tx_failed(struct ieee80211_hw *hw)
b1382ede 289{
459c51ad
DD
290 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
291 struct sk_buff *skb;
b1382ede 292
459c51ad
DD
293 skb = skb_dequeue(q);
294 if (skb == NULL)
295 return;
5078ed50 296
e039fa4a 297 tx_status(hw, skb, 0, 0, 0);
b1382ede
DD
298}
299
459c51ad
DD
300/**
301 * zd_mac_tx_to_dev - callback for USB layer
302 * @skb: a &sk_buff pointer
303 * @error: error value, 0 if transmission successful
304 *
305 * Informs the MAC layer that the frame has successfully transferred to the
306 * device. If an ACK is required and the transfer to the device has been
307 * successful, the packets are put on the @ack_wait_queue with
308 * the control set removed.
309 */
310void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
311{
e039fa4a
JB
312 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
313 struct ieee80211_hw *hw = info->driver_data[0];
b1382ede 314
e039fa4a
JB
315 skb_pull(skb, sizeof(struct zd_ctrlset));
316 if (unlikely(error ||
317 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
318 tx_status(hw, skb, 0, 0, !error);
459c51ad 319 } else {
e039fa4a
JB
320 struct sk_buff_head *q =
321 &zd_hw_mac(hw)->ack_wait_queue;
322
323 skb_queue_tail(q, skb);
324 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
325 zd_mac_tx_failed(hw);
e85d0918 326 }
e85d0918
DD
327}
328
b1cd8416 329static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
e85d0918 330{
64f222cc 331 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
459c51ad
DD
332 * the zd-rate values.
333 */
e85d0918 334 static const u8 rate_divisor[] = {
459c51ad
DD
335 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
336 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
337 /* Bits must be doubled. */
338 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
339 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
340 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
341 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
342 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
343 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
344 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
345 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
346 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
347 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
e85d0918
DD
348 };
349
350 u32 bits = (u32)tx_length * 8;
351 u32 divisor;
352
64f222cc 353 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
e85d0918
DD
354 if (divisor == 0)
355 return -EINVAL;
356
b1cd8416
DD
357 switch (zd_rate) {
358 case ZD_CCK_RATE_5_5M:
e85d0918
DD
359 bits = (2*bits) + 10; /* round up to the next integer */
360 break;
b1cd8416 361 case ZD_CCK_RATE_11M:
e85d0918
DD
362 if (service) {
363 u32 t = bits % 11;
364 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
365 if (0 < t && t <= 3) {
366 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
367 }
368 }
369 bits += 10; /* round up to the next integer */
370 break;
371 }
372
373 return bits/divisor;
374}
375
e85d0918 376static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
459c51ad 377 struct ieee80211_hdr *header, u32 flags)
e85d0918 378{
e85d0918 379 /*
b1382ede 380 * CONTROL TODO:
e85d0918
DD
381 * - if backoff needed, enable bit 0
382 * - if burst (backoff not needed) disable bit 0
e85d0918
DD
383 */
384
385 cs->control = 0;
386
387 /* First fragment */
e039fa4a 388 if (flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
e85d0918
DD
389 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
390
391 /* Multicast */
392 if (is_multicast_ether_addr(header->addr1))
393 cs->control |= ZD_CS_MULTICAST;
394
395 /* PS-POLL */
85365820 396 if (ieee80211_is_pspoll(header->frame_control))
e85d0918
DD
397 cs->control |= ZD_CS_PS_POLL_FRAME;
398
e039fa4a 399 if (flags & IEEE80211_TX_CTL_USE_RTS_CTS)
b1382ede
DD
400 cs->control |= ZD_CS_RTS;
401
e039fa4a 402 if (flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
b1382ede 403 cs->control |= ZD_CS_SELF_CTS;
e85d0918
DD
404
405 /* FIXME: Management frame? */
406}
407
f2cae6c5 408static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
72e77a8a
LCC
409{
410 struct zd_mac *mac = zd_hw_mac(hw);
f2cae6c5 411 int r;
72e77a8a
LCC
412 u32 tmp, j = 0;
413 /* 4 more bytes for tail CRC */
414 u32 full_len = beacon->len + 4;
f2cae6c5
DD
415
416 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
417 if (r < 0)
418 return r;
419 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
420 if (r < 0)
421 return r;
422
72e77a8a 423 while (tmp & 0x2) {
f2cae6c5
DD
424 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
425 if (r < 0)
426 return r;
72e77a8a
LCC
427 if ((++j % 100) == 0) {
428 printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
429 if (j >= 500) {
430 printk(KERN_ERR "Giving up beacon config.\n");
f2cae6c5 431 return -ETIMEDOUT;
72e77a8a
LCC
432 }
433 }
434 msleep(1);
435 }
436
f2cae6c5
DD
437 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
438 if (r < 0)
439 return r;
440 if (zd_chip_is_zd1211b(&mac->chip)) {
441 r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
442 if (r < 0)
443 return r;
444 }
72e77a8a 445
f2cae6c5
DD
446 for (j = 0 ; j < beacon->len; j++) {
447 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO,
72e77a8a 448 *((u8 *)(beacon->data + j)));
f2cae6c5
DD
449 if (r < 0)
450 return r;
451 }
72e77a8a 452
f2cae6c5
DD
453 for (j = 0; j < 4; j++) {
454 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
455 if (r < 0)
456 return r;
457 }
458
459 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
460 if (r < 0)
461 return r;
72e77a8a 462
72e77a8a
LCC
463 /* 802.11b/g 2.4G CCK 1Mb
464 * 802.11a, not yet implemented, uses different values (see GPL vendor
465 * driver)
466 */
f2cae6c5 467 return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
72e77a8a
LCC
468 (full_len << 19));
469}
470
e85d0918 471static int fill_ctrlset(struct zd_mac *mac,
e039fa4a 472 struct sk_buff *skb)
e85d0918
DD
473{
474 int r;
459c51ad
DD
475 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
476 unsigned int frag_len = skb->len + FCS_LEN;
e85d0918 477 unsigned int packet_length;
2e92e6f2 478 struct ieee80211_rate *txrate;
e85d0918
DD
479 struct zd_ctrlset *cs = (struct zd_ctrlset *)
480 skb_push(skb, sizeof(struct zd_ctrlset));
e039fa4a 481 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e85d0918 482
e85d0918 483 ZD_ASSERT(frag_len <= 0xffff);
e85d0918 484
e039fa4a 485 txrate = ieee80211_get_tx_rate(mac->hw, info);
2e92e6f2
JB
486
487 cs->modulation = txrate->hw_value;
e039fa4a 488 if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE)
2e92e6f2 489 cs->modulation = txrate->hw_value_short;
e85d0918
DD
490
491 cs->tx_length = cpu_to_le16(frag_len);
492
e039fa4a 493 cs_set_control(mac, cs, hdr, info->flags);
e85d0918
DD
494
495 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
496 ZD_ASSERT(packet_length <= 0xffff);
497 /* ZD1211B: Computing the length difference this way, gives us
498 * flexibility to compute the packet length.
499 */
74553aed 500 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
e85d0918
DD
501 packet_length - frag_len : packet_length);
502
503 /*
504 * CURRENT LENGTH:
505 * - transmit frame length in microseconds
506 * - seems to be derived from frame length
507 * - see Cal_Us_Service() in zdinlinef.h
508 * - if macp->bTxBurstEnable is enabled, then multiply by 4
509 * - bTxBurstEnable is never set in the vendor driver
510 *
511 * SERVICE:
512 * - "for PLCP configuration"
513 * - always 0 except in some situations at 802.11b 11M
514 * - see line 53 of zdinlinef.h
515 */
516 cs->service = 0;
64f222cc 517 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
e85d0918
DD
518 le16_to_cpu(cs->tx_length));
519 if (r < 0)
520 return r;
521 cs->current_length = cpu_to_le16(r);
459c51ad 522 cs->next_frame_length = 0;
e85d0918
DD
523
524 return 0;
525}
526
459c51ad
DD
527/**
528 * zd_op_tx - transmits a network frame to the device
529 *
530 * @dev: mac80211 hardware device
531 * @skb: socket buffer
532 * @control: the control structure
533 *
534 * This function transmit an IEEE 802.11 network frame to the device. The
535 * control block of the skbuff will be initialized. If necessary the incoming
536 * mac80211 queues will be stopped.
537 */
e039fa4a 538static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
e85d0918 539{
459c51ad 540 struct zd_mac *mac = zd_hw_mac(hw);
e039fa4a 541 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
459c51ad 542 int r;
e85d0918 543
e039fa4a 544 r = fill_ctrlset(mac, skb);
459c51ad
DD
545 if (r)
546 return r;
e85d0918 547
e039fa4a
JB
548 info->driver_data[0] = hw;
549
459c51ad 550 r = zd_usb_tx(&mac->chip.usb, skb);
e039fa4a 551 if (r)
459c51ad 552 return r;
e85d0918
DD
553 return 0;
554}
555
459c51ad
DD
556/**
557 * filter_ack - filters incoming packets for acknowledgements
558 * @dev: the mac80211 device
559 * @rx_hdr: received header
560 * @stats: the status for the received packet
741fec53 561 *
459c51ad
DD
562 * This functions looks for ACK packets and tries to match them with the
563 * frames in the tx queue. If a match is found the frame will be dequeued and
564 * the upper layers is informed about the successful transmission. If
565 * mac80211 queues have been stopped and the number of frames still to be
566 * transmitted is low the queues will be opened again.
e85d0918 567 *
459c51ad 568 * Returns 1 if the frame was an ACK, 0 if it was ignored.
e85d0918 569 */
459c51ad
DD
570static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
571 struct ieee80211_rx_status *stats)
e85d0918 572{
459c51ad
DD
573 struct sk_buff *skb;
574 struct sk_buff_head *q;
575 unsigned long flags;
e85d0918 576
85365820 577 if (!ieee80211_is_ack(rx_hdr->frame_control))
e85d0918 578 return 0;
e85d0918 579
459c51ad
DD
580 q = &zd_hw_mac(hw)->ack_wait_queue;
581 spin_lock_irqsave(&q->lock, flags);
582 for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
583 struct ieee80211_hdr *tx_hdr;
584
585 tx_hdr = (struct ieee80211_hdr *)skb->data;
586 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
587 {
459c51ad 588 __skb_unlink(skb, q);
e039fa4a 589 tx_status(hw, skb, IEEE80211_TX_STAT_ACK, stats->signal, 1);
459c51ad
DD
590 goto out;
591 }
592 }
593out:
594 spin_unlock_irqrestore(&q->lock, flags);
595 return 1;
e85d0918
DD
596}
597
459c51ad 598int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
e85d0918 599{
459c51ad
DD
600 struct zd_mac *mac = zd_hw_mac(hw);
601 struct ieee80211_rx_status stats;
602 const struct rx_status *status;
603 struct sk_buff *skb;
604 int bad_frame = 0;
85365820
HH
605 __le16 fc;
606 int need_padding;
8318d78a
JB
607 int i;
608 u8 rate;
db888aed 609
459c51ad
DD
610 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
611 FCS_LEN + sizeof(struct rx_status))
612 return -EINVAL;
e85d0918 613
459c51ad 614 memset(&stats, 0, sizeof(stats));
e85d0918 615
459c51ad
DD
616 /* Note about pass_failed_fcs and pass_ctrl access below:
617 * mac locking intentionally omitted here, as this is the only unlocked
618 * reader and the only writer is configure_filter. Plus, if there were
619 * any races accessing these variables, it wouldn't really matter.
620 * If mac80211 ever provides a way for us to access filter flags
621 * from outside configure_filter, we could improve on this. Also, this
622 * situation may change once we implement some kind of DMA-into-skb
623 * RX path. */
e85d0918 624
459c51ad
DD
625 /* Caller has to ensure that length >= sizeof(struct rx_status). */
626 status = (struct rx_status *)
937a049d 627 (buffer + (length - sizeof(struct rx_status)));
e85d0918 628 if (status->frame_status & ZD_RX_ERROR) {
459c51ad
DD
629 if (mac->pass_failed_fcs &&
630 (status->frame_status & ZD_RX_CRC32_ERROR)) {
631 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
632 bad_frame = 1;
633 } else {
634 return -EINVAL;
22d3405f 635 }
e85d0918 636 }
22d3405f 637
8318d78a
JB
638 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
639 stats.band = IEEE80211_BAND_2GHZ;
566bfe5a
BR
640 stats.signal = status->signal_strength;
641 stats.qual = zd_rx_qual_percent(buffer,
e85d0918
DD
642 length - sizeof(struct rx_status),
643 status);
8318d78a
JB
644
645 rate = zd_rx_rate(buffer, status);
646
647 /* todo: return index in the big switches in zd_rx_rate instead */
648 for (i = 0; i < mac->band.n_bitrates; i++)
649 if (rate == mac->band.bitrates[i].hw_value)
650 stats.rate_idx = i;
459c51ad
DD
651
652 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
653 buffer += ZD_PLCP_HEADER_SIZE;
654
655 /* Except for bad frames, filter each frame to see if it is an ACK, in
656 * which case our internal TX tracking is updated. Normally we then
657 * bail here as there's no need to pass ACKs on up to the stack, but
658 * there is also the case where the stack has requested us to pass
659 * control frames on up (pass_ctrl) which we must consider. */
660 if (!bad_frame &&
661 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
662 && !mac->pass_ctrl)
663 return 0;
e85d0918 664
85365820
HH
665 fc = *(__le16 *)buffer;
666 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
9081728b
MB
667
668 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
459c51ad
DD
669 if (skb == NULL)
670 return -ENOMEM;
9081728b
MB
671 if (need_padding) {
672 /* Make sure the the payload data is 4 byte aligned. */
673 skb_reserve(skb, 2);
674 }
675
459c51ad
DD
676 memcpy(skb_put(skb, length), buffer, length);
677
678 ieee80211_rx_irqsafe(hw, skb, &stats);
e85d0918
DD
679 return 0;
680}
681
459c51ad
DD
682static int zd_op_add_interface(struct ieee80211_hw *hw,
683 struct ieee80211_if_init_conf *conf)
e85d0918 684{
459c51ad 685 struct zd_mac *mac = zd_hw_mac(hw);
e85d0918 686
459c51ad
DD
687 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
688 if (mac->type != IEEE80211_IF_TYPE_INVALID)
689 return -EOPNOTSUPP;
e85d0918 690
459c51ad
DD
691 switch (conf->type) {
692 case IEEE80211_IF_TYPE_MNTR:
72e77a8a 693 case IEEE80211_IF_TYPE_MESH_POINT:
459c51ad 694 case IEEE80211_IF_TYPE_STA:
84e6dc9a 695 case IEEE80211_IF_TYPE_IBSS:
459c51ad
DD
696 mac->type = conf->type;
697 break;
698 default:
699 return -EOPNOTSUPP;
4d1feabc 700 }
e85d0918 701
459c51ad
DD
702 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
703}
e85d0918 704
459c51ad
DD
705static void zd_op_remove_interface(struct ieee80211_hw *hw,
706 struct ieee80211_if_init_conf *conf)
707{
708 struct zd_mac *mac = zd_hw_mac(hw);
709 mac->type = IEEE80211_IF_TYPE_INVALID;
86229f0c 710 zd_set_beacon_interval(&mac->chip, 0);
459c51ad
DD
711 zd_write_mac_addr(&mac->chip, NULL);
712}
93137943 713
459c51ad
DD
714static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
715{
716 struct zd_mac *mac = zd_hw_mac(hw);
8318d78a 717 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
459c51ad 718}
db888aed 719
32bfd35d
JB
720static int zd_op_config_interface(struct ieee80211_hw *hw,
721 struct ieee80211_vif *vif,
459c51ad
DD
722 struct ieee80211_if_conf *conf)
723{
724 struct zd_mac *mac = zd_hw_mac(hw);
72e77a8a 725 int associated;
f2cae6c5 726 int r;
72e77a8a 727
84e6dc9a
BR
728 if (mac->type == IEEE80211_IF_TYPE_MESH_POINT ||
729 mac->type == IEEE80211_IF_TYPE_IBSS) {
72e77a8a 730 associated = true;
9d139c81
JB
731 if (conf->changed & IEEE80211_IFCC_BEACON) {
732 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
733
734 if (!beacon)
735 return -ENOMEM;
736 r = zd_mac_config_beacon(hw, beacon);
f2cae6c5
DD
737 if (r < 0)
738 return r;
739 r = zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
72e77a8a 740 hw->conf.beacon_int);
f2cae6c5
DD
741 if (r < 0)
742 return r;
9d139c81 743 kfree_skb(beacon);
72e77a8a
LCC
744 }
745 } else
746 associated = is_valid_ether_addr(conf->bssid);
e85d0918 747
459c51ad 748 spin_lock_irq(&mac->lock);
72e77a8a 749 mac->associated = associated;
459c51ad 750 spin_unlock_irq(&mac->lock);
e85d0918 751
459c51ad
DD
752 /* TODO: do hardware bssid filtering */
753 return 0;
4d1feabc
UK
754}
755
72e77a8a
LCC
756void zd_process_intr(struct work_struct *work)
757{
758 u16 int_status;
759 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
760
d63ddcec 761 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
72e77a8a
LCC
762 if (int_status & INT_CFG_NEXT_BCN) {
763 if (net_ratelimit())
764 dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
765 } else
766 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
767
768 zd_chip_enable_hwint(&mac->chip);
769}
770
771
459c51ad 772static void set_multicast_hash_handler(struct work_struct *work)
4d1feabc 773{
459c51ad
DD
774 struct zd_mac *mac =
775 container_of(work, struct zd_mac, set_multicast_hash_work);
776 struct zd_mc_hash hash;
4d1feabc 777
459c51ad
DD
778 spin_lock_irq(&mac->lock);
779 hash = mac->multicast_hash;
780 spin_unlock_irq(&mac->lock);
4d1feabc 781
459c51ad 782 zd_chip_set_multicast_hash(&mac->chip, &hash);
e85d0918
DD
783}
784
459c51ad 785static void set_rx_filter_handler(struct work_struct *work)
e85d0918 786{
459c51ad
DD
787 struct zd_mac *mac =
788 container_of(work, struct zd_mac, set_rx_filter_work);
789 int r;
790
791 dev_dbg_f(zd_mac_dev(mac), "\n");
792 r = set_rx_filter(mac);
793 if (r)
794 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
e85d0918
DD
795}
796
459c51ad
DD
797#define SUPPORTED_FIF_FLAGS \
798 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
2c1a1b12 799 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
459c51ad
DD
800static void zd_op_configure_filter(struct ieee80211_hw *hw,
801 unsigned int changed_flags,
802 unsigned int *new_flags,
803 int mc_count, struct dev_mc_list *mclist)
e85d0918 804{
459c51ad
DD
805 struct zd_mc_hash hash;
806 struct zd_mac *mac = zd_hw_mac(hw);
807 unsigned long flags;
808 int i;
e85d0918 809
459c51ad
DD
810 /* Only deal with supported flags */
811 changed_flags &= SUPPORTED_FIF_FLAGS;
812 *new_flags &= SUPPORTED_FIF_FLAGS;
813
814 /* changed_flags is always populated but this driver
815 * doesn't support all FIF flags so its possible we don't
816 * need to do anything */
817 if (!changed_flags)
818 return;
819
820 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
821 zd_mc_add_all(&hash);
822 } else {
823 DECLARE_MAC_BUF(macbuf);
824
825 zd_mc_clear(&hash);
826 for (i = 0; i < mc_count; i++) {
827 if (!mclist)
828 break;
829 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
830 print_mac(macbuf, mclist->dmi_addr));
831 zd_mc_add_addr(&hash, mclist->dmi_addr);
832 mclist = mclist->next;
833 }
e85d0918 834 }
459c51ad
DD
835
836 spin_lock_irqsave(&mac->lock, flags);
837 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
838 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
839 mac->multicast_hash = hash;
840 spin_unlock_irqrestore(&mac->lock, flags);
841 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
842
843 if (changed_flags & FIF_CONTROL)
844 queue_work(zd_workqueue, &mac->set_rx_filter_work);
845
846 /* no handling required for FIF_OTHER_BSS as we don't currently
847 * do BSSID filtering */
848 /* FIXME: in future it would be nice to enable the probe response
849 * filter (so that the driver doesn't see them) until
850 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
851 * have to schedule work to enable prbresp reception, which might
852 * happen too late. For now we'll just listen and forward them all the
853 * time. */
e85d0918
DD
854}
855
459c51ad 856static void set_rts_cts_work(struct work_struct *work)
e85d0918 857{
459c51ad
DD
858 struct zd_mac *mac =
859 container_of(work, struct zd_mac, set_rts_cts_work);
860 unsigned long flags;
861 unsigned int short_preamble;
862
863 mutex_lock(&mac->chip.mutex);
864
865 spin_lock_irqsave(&mac->lock, flags);
866 mac->updating_rts_rate = 0;
867 short_preamble = mac->short_preamble;
868 spin_unlock_irqrestore(&mac->lock, flags);
869
870 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
871 mutex_unlock(&mac->chip.mutex);
e85d0918
DD
872}
873
471b3efd
JB
874static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
875 struct ieee80211_vif *vif,
876 struct ieee80211_bss_conf *bss_conf,
877 u32 changes)
e85d0918 878{
459c51ad
DD
879 struct zd_mac *mac = zd_hw_mac(hw);
880 unsigned long flags;
881
882 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
883
471b3efd 884 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
459c51ad 885 spin_lock_irqsave(&mac->lock, flags);
471b3efd 886 mac->short_preamble = bss_conf->use_short_preamble;
459c51ad
DD
887 if (!mac->updating_rts_rate) {
888 mac->updating_rts_rate = 1;
889 /* FIXME: should disable TX here, until work has
890 * completed and RTS_CTS reg is updated */
891 queue_work(zd_workqueue, &mac->set_rts_cts_work);
892 }
893 spin_unlock_irqrestore(&mac->lock, flags);
894 }
e85d0918
DD
895}
896
459c51ad
DD
897static const struct ieee80211_ops zd_ops = {
898 .tx = zd_op_tx,
899 .start = zd_op_start,
900 .stop = zd_op_stop,
901 .add_interface = zd_op_add_interface,
902 .remove_interface = zd_op_remove_interface,
903 .config = zd_op_config,
904 .config_interface = zd_op_config_interface,
905 .configure_filter = zd_op_configure_filter,
471b3efd 906 .bss_info_changed = zd_op_bss_info_changed,
459c51ad
DD
907};
908
909struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
e85d0918 910{
459c51ad
DD
911 struct zd_mac *mac;
912 struct ieee80211_hw *hw;
e85d0918 913
459c51ad
DD
914 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
915 if (!hw) {
916 dev_dbg_f(&intf->dev, "out of memory\n");
917 return NULL;
db888aed 918 }
459c51ad
DD
919
920 mac = zd_hw_mac(hw);
921
922 memset(mac, 0, sizeof(*mac));
923 spin_lock_init(&mac->lock);
924 mac->hw = hw;
925
926 mac->type = IEEE80211_IF_TYPE_INVALID;
927
928 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
929 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
8318d78a
JB
930 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
931 mac->band.bitrates = mac->rates;
932 mac->band.n_channels = ARRAY_SIZE(zd_channels);
933 mac->band.channels = mac->channels;
934
935 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
936
72e77a8a 937 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
566bfe5a 938 IEEE80211_HW_SIGNAL_DB;
459c51ad 939
566bfe5a 940 hw->max_signal = 100;
459c51ad
DD
941 hw->queues = 1;
942 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
943
944 skb_queue_head_init(&mac->ack_wait_queue);
945
459c51ad
DD
946 zd_chip_init(&mac->chip, hw, intf);
947 housekeeping_init(mac);
948 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
949 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
950 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
72e77a8a 951 INIT_WORK(&mac->process_intr, zd_process_intr);
459c51ad
DD
952
953 SET_IEEE80211_DEV(hw, &intf->dev);
954 return hw;
e85d0918
DD
955}
956
583afd1e
UK
957#define LINK_LED_WORK_DELAY HZ
958
c4028958 959static void link_led_handler(struct work_struct *work)
583afd1e 960{
c4028958
DH
961 struct zd_mac *mac =
962 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
583afd1e 963 struct zd_chip *chip = &mac->chip;
583afd1e
UK
964 int is_associated;
965 int r;
966
967 spin_lock_irq(&mac->lock);
459c51ad 968 is_associated = mac->associated;
583afd1e
UK
969 spin_unlock_irq(&mac->lock);
970
971 r = zd_chip_control_leds(chip,
972 is_associated ? LED_ASSOCIATED : LED_SCANNING);
973 if (r)
459c51ad 974 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
583afd1e
UK
975
976 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
977 LINK_LED_WORK_DELAY);
978}
979
980static void housekeeping_init(struct zd_mac *mac)
981{
c4028958 982 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
583afd1e
UK
983}
984
985static void housekeeping_enable(struct zd_mac *mac)
986{
987 dev_dbg_f(zd_mac_dev(mac), "\n");
988 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
989 0);
990}
991
992static void housekeeping_disable(struct zd_mac *mac)
993{
994 dev_dbg_f(zd_mac_dev(mac), "\n");
995 cancel_rearming_delayed_workqueue(zd_workqueue,
996 &mac->housekeeping.link_led_work);
997 zd_chip_control_leds(&mac->chip, LED_OFF);
998}
This page took 1.52142 seconds and 5 git commands to generate.