]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/zd1211rw/zd_mac.c
cxgb3 - Add LRO support
[net-next-2.6.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
227/**
228 * clear_tx_skb_control_block - clears the control block of tx skbuffs
229 * @skb: a &struct sk_buff pointer
230 *
231 * This clears the control block of skbuff buffers, which were transmitted to
232 * the device. Notify that the function is not thread-safe, so prevent
233 * multiple calls.
234 */
235static void clear_tx_skb_control_block(struct sk_buff *skb)
236{
237 struct zd_tx_skb_control_block *cb =
238 (struct zd_tx_skb_control_block *)skb->cb;
239
240 kfree(cb->control);
241 cb->control = NULL;
242}
243
244/**
245 * kfree_tx_skb - frees a tx skbuff
246 * @skb: a &struct sk_buff pointer
247 *
248 * Frees the tx skbuff. Frees also the allocated control structure in the
249 * control block if necessary.
250 */
251static void kfree_tx_skb(struct sk_buff *skb)
e85d0918 252{
459c51ad
DD
253 clear_tx_skb_control_block(skb);
254 dev_kfree_skb_any(skb);
255}
e85d0918 256
459c51ad
DD
257static void zd_op_stop(struct ieee80211_hw *hw)
258{
259 struct zd_mac *mac = zd_hw_mac(hw);
260 struct zd_chip *chip = &mac->chip;
261 struct sk_buff *skb;
262 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
c9a4b35d 263
459c51ad 264 /* The order here deliberately is a little different from the open()
e85d0918 265 * method, since we need to make sure there is no opportunity for RX
459c51ad 266 * frames to be processed by mac80211 after we have stopped it.
e85d0918
DD
267 */
268
459c51ad 269 zd_chip_disable_rxtx(chip);
583afd1e 270 housekeeping_disable(mac);
b1382ede 271 flush_workqueue(zd_workqueue);
b1382ede 272
e85d0918
DD
273 zd_chip_disable_hwint(chip);
274 zd_chip_switch_radio_off(chip);
275 zd_chip_disable_int(chip);
276
e85d0918 277
459c51ad
DD
278 while ((skb = skb_dequeue(ack_wait_queue)))
279 kfree_tx_skb(skb);
9cdac965
UK
280}
281
459c51ad
DD
282/**
283 * init_tx_skb_control_block - initializes skb control block
284 * @skb: a &sk_buff pointer
285 * @dev: pointer to the mac80221 device
286 * @control: mac80211 tx control applying for the frame in @skb
287 *
288 * Initializes the control block of the skbuff to be transmitted.
289 */
290static int init_tx_skb_control_block(struct sk_buff *skb,
291 struct ieee80211_hw *hw,
292 struct ieee80211_tx_control *control)
293{
294 struct zd_tx_skb_control_block *cb =
295 (struct zd_tx_skb_control_block *)skb->cb;
296
297 ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb));
298 memset(cb, 0, sizeof(*cb));
299 cb->hw= hw;
300 cb->control = kmalloc(sizeof(*control), GFP_ATOMIC);
301 if (cb->control == NULL)
302 return -ENOMEM;
303 memcpy(cb->control, control, sizeof(*control));
e85d0918
DD
304
305 return 0;
306}
307
459c51ad
DD
308/**
309 * tx_status - reports tx status of a packet if required
310 * @hw - a &struct ieee80211_hw pointer
311 * @skb - a sk-buffer
312 * @status - the tx status of the packet without control information
313 * @success - True for successfull transmission of the frame
314 *
315 * This information calls ieee80211_tx_status_irqsafe() if required by the
316 * control information. It copies the control information into the status
317 * information.
318 *
319 * If no status information has been requested, the skb is freed.
320 */
321static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
322 struct ieee80211_tx_status *status,
323 bool success)
b1382ede 324{
459c51ad
DD
325 struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
326 skb->cb;
b1382ede 327
459c51ad
DD
328 ZD_ASSERT(cb->control != NULL);
329 memcpy(&status->control, cb->control, sizeof(status->control));
330 if (!success)
331 status->excessive_retries = 1;
332 clear_tx_skb_control_block(skb);
333 ieee80211_tx_status_irqsafe(hw, skb, status);
b1382ede
DD
334}
335
459c51ad
DD
336/**
337 * zd_mac_tx_failed - callback for failed frames
338 * @dev: the mac80211 wireless device
339 *
340 * This function is called if a frame couldn't be succesfully be
341 * transferred. The first frame from the tx queue, will be selected and
342 * reported as error to the upper layers.
343 */
344void zd_mac_tx_failed(struct ieee80211_hw *hw)
b1382ede 345{
459c51ad
DD
346 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
347 struct sk_buff *skb;
5078ed50 348 struct ieee80211_tx_status status;
b1382ede 349
459c51ad
DD
350 skb = skb_dequeue(q);
351 if (skb == NULL)
352 return;
5078ed50
JB
353
354 memset(&status, 0, sizeof(status));
355
459c51ad 356 tx_status(hw, skb, &status, 0);
b1382ede
DD
357}
358
459c51ad
DD
359/**
360 * zd_mac_tx_to_dev - callback for USB layer
361 * @skb: a &sk_buff pointer
362 * @error: error value, 0 if transmission successful
363 *
364 * Informs the MAC layer that the frame has successfully transferred to the
365 * device. If an ACK is required and the transfer to the device has been
366 * successful, the packets are put on the @ack_wait_queue with
367 * the control set removed.
368 */
369void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
370{
371 struct zd_tx_skb_control_block *cb =
372 (struct zd_tx_skb_control_block *)skb->cb;
373 struct ieee80211_hw *hw = cb->hw;
374
375 if (likely(cb->control)) {
376 skb_pull(skb, sizeof(struct zd_ctrlset));
377 if (unlikely(error ||
378 (cb->control->flags & IEEE80211_TXCTL_NO_ACK)))
379 {
5078ed50
JB
380 struct ieee80211_tx_status status;
381 memset(&status, 0, sizeof(status));
459c51ad 382 tx_status(hw, skb, &status, !error);
b1382ede 383 } else {
459c51ad
DD
384 struct sk_buff_head *q =
385 &zd_hw_mac(hw)->ack_wait_queue;
b1382ede 386
459c51ad
DD
387 skb_queue_tail(q, skb);
388 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
389 zd_mac_tx_failed(hw);
b1382ede 390 }
459c51ad
DD
391 } else {
392 kfree_tx_skb(skb);
e85d0918 393 }
e85d0918
DD
394}
395
b1cd8416 396static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
e85d0918 397{
64f222cc 398 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
459c51ad
DD
399 * the zd-rate values.
400 */
e85d0918 401 static const u8 rate_divisor[] = {
459c51ad
DD
402 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
403 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
404 /* Bits must be doubled. */
405 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
406 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
407 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
408 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
409 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
410 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
411 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
412 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
413 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
414 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
e85d0918
DD
415 };
416
417 u32 bits = (u32)tx_length * 8;
418 u32 divisor;
419
64f222cc 420 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
e85d0918
DD
421 if (divisor == 0)
422 return -EINVAL;
423
b1cd8416
DD
424 switch (zd_rate) {
425 case ZD_CCK_RATE_5_5M:
e85d0918
DD
426 bits = (2*bits) + 10; /* round up to the next integer */
427 break;
b1cd8416 428 case ZD_CCK_RATE_11M:
e85d0918
DD
429 if (service) {
430 u32 t = bits % 11;
431 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
432 if (0 < t && t <= 3) {
433 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
434 }
435 }
436 bits += 10; /* round up to the next integer */
437 break;
438 }
439
440 return bits/divisor;
441}
442
e85d0918 443static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
459c51ad 444 struct ieee80211_hdr *header, u32 flags)
e85d0918 445{
459c51ad 446 u16 fctl = le16_to_cpu(header->frame_control);
e85d0918
DD
447
448 /*
b1382ede 449 * CONTROL TODO:
e85d0918
DD
450 * - if backoff needed, enable bit 0
451 * - if burst (backoff not needed) disable bit 0
e85d0918
DD
452 */
453
454 cs->control = 0;
455
456 /* First fragment */
459c51ad 457 if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
e85d0918
DD
458 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
459
460 /* Multicast */
461 if (is_multicast_ether_addr(header->addr1))
462 cs->control |= ZD_CS_MULTICAST;
463
464 /* PS-POLL */
459c51ad
DD
465 if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
466 (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
e85d0918
DD
467 cs->control |= ZD_CS_PS_POLL_FRAME;
468
459c51ad 469 if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
b1382ede
DD
470 cs->control |= ZD_CS_RTS;
471
459c51ad 472 if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
b1382ede 473 cs->control |= ZD_CS_SELF_CTS;
e85d0918
DD
474
475 /* FIXME: Management frame? */
476}
477
72e77a8a
LCC
478void zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
479{
480 struct zd_mac *mac = zd_hw_mac(hw);
481 u32 tmp, j = 0;
482 /* 4 more bytes for tail CRC */
483 u32 full_len = beacon->len + 4;
484 zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
485 zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
486 while (tmp & 0x2) {
487 zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
488 if ((++j % 100) == 0) {
489 printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
490 if (j >= 500) {
491 printk(KERN_ERR "Giving up beacon config.\n");
492 return;
493 }
494 }
495 msleep(1);
496 }
497
498 zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
499 if (zd_chip_is_zd1211b(&mac->chip))
500 zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
501
502 for (j = 0 ; j < beacon->len; j++)
503 zd_iowrite32(&mac->chip, CR_BCN_FIFO,
504 *((u8 *)(beacon->data + j)));
505
506 for (j = 0; j < 4; j++)
507 zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
508
509 zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
510 /* 802.11b/g 2.4G CCK 1Mb
511 * 802.11a, not yet implemented, uses different values (see GPL vendor
512 * driver)
513 */
514 zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
515 (full_len << 19));
516}
517
e85d0918 518static int fill_ctrlset(struct zd_mac *mac,
459c51ad
DD
519 struct sk_buff *skb,
520 struct ieee80211_tx_control *control)
e85d0918
DD
521{
522 int r;
459c51ad
DD
523 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
524 unsigned int frag_len = skb->len + FCS_LEN;
e85d0918
DD
525 unsigned int packet_length;
526 struct zd_ctrlset *cs = (struct zd_ctrlset *)
527 skb_push(skb, sizeof(struct zd_ctrlset));
528
e85d0918 529 ZD_ASSERT(frag_len <= 0xffff);
e85d0918 530
8318d78a
JB
531 cs->modulation = control->tx_rate->hw_value;
532 if (control->flags & IEEE80211_TXCTL_SHORT_PREAMBLE)
533 cs->modulation = control->tx_rate->hw_value_short;
e85d0918
DD
534
535 cs->tx_length = cpu_to_le16(frag_len);
536
459c51ad 537 cs_set_control(mac, cs, hdr, control->flags);
e85d0918
DD
538
539 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
540 ZD_ASSERT(packet_length <= 0xffff);
541 /* ZD1211B: Computing the length difference this way, gives us
542 * flexibility to compute the packet length.
543 */
74553aed 544 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
e85d0918
DD
545 packet_length - frag_len : packet_length);
546
547 /*
548 * CURRENT LENGTH:
549 * - transmit frame length in microseconds
550 * - seems to be derived from frame length
551 * - see Cal_Us_Service() in zdinlinef.h
552 * - if macp->bTxBurstEnable is enabled, then multiply by 4
553 * - bTxBurstEnable is never set in the vendor driver
554 *
555 * SERVICE:
556 * - "for PLCP configuration"
557 * - always 0 except in some situations at 802.11b 11M
558 * - see line 53 of zdinlinef.h
559 */
560 cs->service = 0;
64f222cc 561 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
e85d0918
DD
562 le16_to_cpu(cs->tx_length));
563 if (r < 0)
564 return r;
565 cs->current_length = cpu_to_le16(r);
459c51ad 566 cs->next_frame_length = 0;
e85d0918
DD
567
568 return 0;
569}
570
459c51ad
DD
571/**
572 * zd_op_tx - transmits a network frame to the device
573 *
574 * @dev: mac80211 hardware device
575 * @skb: socket buffer
576 * @control: the control structure
577 *
578 * This function transmit an IEEE 802.11 network frame to the device. The
579 * control block of the skbuff will be initialized. If necessary the incoming
580 * mac80211 queues will be stopped.
581 */
582static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
583 struct ieee80211_tx_control *control)
e85d0918 584{
459c51ad
DD
585 struct zd_mac *mac = zd_hw_mac(hw);
586 int r;
e85d0918 587
459c51ad
DD
588 r = fill_ctrlset(mac, skb, control);
589 if (r)
590 return r;
e85d0918 591
459c51ad
DD
592 r = init_tx_skb_control_block(skb, hw, control);
593 if (r)
594 return r;
595 r = zd_usb_tx(&mac->chip.usb, skb);
596 if (r) {
597 clear_tx_skb_control_block(skb);
598 return r;
e85d0918 599 }
e85d0918
DD
600 return 0;
601}
602
459c51ad
DD
603/**
604 * filter_ack - filters incoming packets for acknowledgements
605 * @dev: the mac80211 device
606 * @rx_hdr: received header
607 * @stats: the status for the received packet
741fec53 608 *
459c51ad
DD
609 * This functions looks for ACK packets and tries to match them with the
610 * frames in the tx queue. If a match is found the frame will be dequeued and
611 * the upper layers is informed about the successful transmission. If
612 * mac80211 queues have been stopped and the number of frames still to be
613 * transmitted is low the queues will be opened again.
e85d0918 614 *
459c51ad 615 * Returns 1 if the frame was an ACK, 0 if it was ignored.
e85d0918 616 */
459c51ad
DD
617static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
618 struct ieee80211_rx_status *stats)
e85d0918 619{
459c51ad
DD
620 u16 fc = le16_to_cpu(rx_hdr->frame_control);
621 struct sk_buff *skb;
622 struct sk_buff_head *q;
623 unsigned long flags;
e85d0918 624
459c51ad
DD
625 if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
626 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
e85d0918 627 return 0;
e85d0918 628
459c51ad
DD
629 q = &zd_hw_mac(hw)->ack_wait_queue;
630 spin_lock_irqsave(&q->lock, flags);
631 for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
632 struct ieee80211_hdr *tx_hdr;
633
634 tx_hdr = (struct ieee80211_hdr *)skb->data;
635 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
636 {
5078ed50
JB
637 struct ieee80211_tx_status status;
638
639 memset(&status, 0, sizeof(status));
459c51ad 640 status.flags = IEEE80211_TX_STATUS_ACK;
566bfe5a 641 status.ack_signal = stats->signal;
459c51ad
DD
642 __skb_unlink(skb, q);
643 tx_status(hw, skb, &status, 1);
644 goto out;
645 }
646 }
647out:
648 spin_unlock_irqrestore(&q->lock, flags);
649 return 1;
e85d0918
DD
650}
651
459c51ad 652int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
e85d0918 653{
459c51ad
DD
654 struct zd_mac *mac = zd_hw_mac(hw);
655 struct ieee80211_rx_status stats;
656 const struct rx_status *status;
657 struct sk_buff *skb;
658 int bad_frame = 0;
9081728b
MB
659 u16 fc;
660 bool is_qos, is_4addr, need_padding;
8318d78a
JB
661 int i;
662 u8 rate;
db888aed 663
459c51ad
DD
664 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
665 FCS_LEN + sizeof(struct rx_status))
666 return -EINVAL;
e85d0918 667
459c51ad 668 memset(&stats, 0, sizeof(stats));
e85d0918 669
459c51ad
DD
670 /* Note about pass_failed_fcs and pass_ctrl access below:
671 * mac locking intentionally omitted here, as this is the only unlocked
672 * reader and the only writer is configure_filter. Plus, if there were
673 * any races accessing these variables, it wouldn't really matter.
674 * If mac80211 ever provides a way for us to access filter flags
675 * from outside configure_filter, we could improve on this. Also, this
676 * situation may change once we implement some kind of DMA-into-skb
677 * RX path. */
e85d0918 678
459c51ad
DD
679 /* Caller has to ensure that length >= sizeof(struct rx_status). */
680 status = (struct rx_status *)
937a049d 681 (buffer + (length - sizeof(struct rx_status)));
e85d0918 682 if (status->frame_status & ZD_RX_ERROR) {
459c51ad
DD
683 if (mac->pass_failed_fcs &&
684 (status->frame_status & ZD_RX_CRC32_ERROR)) {
685 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
686 bad_frame = 1;
687 } else {
688 return -EINVAL;
22d3405f 689 }
e85d0918 690 }
22d3405f 691
8318d78a
JB
692 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
693 stats.band = IEEE80211_BAND_2GHZ;
566bfe5a
BR
694 stats.signal = status->signal_strength;
695 stats.qual = zd_rx_qual_percent(buffer,
e85d0918
DD
696 length - sizeof(struct rx_status),
697 status);
8318d78a
JB
698
699 rate = zd_rx_rate(buffer, status);
700
701 /* todo: return index in the big switches in zd_rx_rate instead */
702 for (i = 0; i < mac->band.n_bitrates; i++)
703 if (rate == mac->band.bitrates[i].hw_value)
704 stats.rate_idx = i;
459c51ad
DD
705
706 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
707 buffer += ZD_PLCP_HEADER_SIZE;
708
709 /* Except for bad frames, filter each frame to see if it is an ACK, in
710 * which case our internal TX tracking is updated. Normally we then
711 * bail here as there's no need to pass ACKs on up to the stack, but
712 * there is also the case where the stack has requested us to pass
713 * control frames on up (pass_ctrl) which we must consider. */
714 if (!bad_frame &&
715 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
716 && !mac->pass_ctrl)
717 return 0;
e85d0918 718
9081728b
MB
719 fc = le16_to_cpu(*((__le16 *) buffer));
720
721 is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
722 ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA);
723 is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
724 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
725 need_padding = is_qos ^ is_4addr;
726
727 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
459c51ad
DD
728 if (skb == NULL)
729 return -ENOMEM;
9081728b
MB
730 if (need_padding) {
731 /* Make sure the the payload data is 4 byte aligned. */
732 skb_reserve(skb, 2);
733 }
734
459c51ad
DD
735 memcpy(skb_put(skb, length), buffer, length);
736
737 ieee80211_rx_irqsafe(hw, skb, &stats);
e85d0918
DD
738 return 0;
739}
740
459c51ad
DD
741static int zd_op_add_interface(struct ieee80211_hw *hw,
742 struct ieee80211_if_init_conf *conf)
e85d0918 743{
459c51ad 744 struct zd_mac *mac = zd_hw_mac(hw);
e85d0918 745
459c51ad
DD
746 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
747 if (mac->type != IEEE80211_IF_TYPE_INVALID)
748 return -EOPNOTSUPP;
e85d0918 749
459c51ad
DD
750 switch (conf->type) {
751 case IEEE80211_IF_TYPE_MNTR:
72e77a8a 752 case IEEE80211_IF_TYPE_MESH_POINT:
459c51ad 753 case IEEE80211_IF_TYPE_STA:
84e6dc9a 754 case IEEE80211_IF_TYPE_IBSS:
459c51ad
DD
755 mac->type = conf->type;
756 break;
757 default:
758 return -EOPNOTSUPP;
4d1feabc 759 }
e85d0918 760
459c51ad
DD
761 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
762}
e85d0918 763
459c51ad
DD
764static void zd_op_remove_interface(struct ieee80211_hw *hw,
765 struct ieee80211_if_init_conf *conf)
766{
767 struct zd_mac *mac = zd_hw_mac(hw);
768 mac->type = IEEE80211_IF_TYPE_INVALID;
769 zd_write_mac_addr(&mac->chip, NULL);
770}
93137943 771
459c51ad
DD
772static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
773{
774 struct zd_mac *mac = zd_hw_mac(hw);
8318d78a 775 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
459c51ad 776}
db888aed 777
32bfd35d
JB
778static int zd_op_config_interface(struct ieee80211_hw *hw,
779 struct ieee80211_vif *vif,
459c51ad
DD
780 struct ieee80211_if_conf *conf)
781{
782 struct zd_mac *mac = zd_hw_mac(hw);
72e77a8a
LCC
783 int associated;
784
84e6dc9a
BR
785 if (mac->type == IEEE80211_IF_TYPE_MESH_POINT ||
786 mac->type == IEEE80211_IF_TYPE_IBSS) {
72e77a8a
LCC
787 associated = true;
788 if (conf->beacon) {
789 zd_mac_config_beacon(hw, conf->beacon);
790 kfree_skb(conf->beacon);
791 zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
792 hw->conf.beacon_int);
793 }
794 } else
795 associated = is_valid_ether_addr(conf->bssid);
e85d0918 796
459c51ad 797 spin_lock_irq(&mac->lock);
72e77a8a 798 mac->associated = associated;
459c51ad 799 spin_unlock_irq(&mac->lock);
e85d0918 800
459c51ad
DD
801 /* TODO: do hardware bssid filtering */
802 return 0;
4d1feabc
UK
803}
804
72e77a8a
LCC
805void zd_process_intr(struct work_struct *work)
806{
807 u16 int_status;
808 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
809
810 int_status = le16_to_cpu(*(u16 *)(mac->intr_buffer+4));
811 if (int_status & INT_CFG_NEXT_BCN) {
812 if (net_ratelimit())
813 dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
814 } else
815 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
816
817 zd_chip_enable_hwint(&mac->chip);
818}
819
820
459c51ad 821static void set_multicast_hash_handler(struct work_struct *work)
4d1feabc 822{
459c51ad
DD
823 struct zd_mac *mac =
824 container_of(work, struct zd_mac, set_multicast_hash_work);
825 struct zd_mc_hash hash;
4d1feabc 826
459c51ad
DD
827 spin_lock_irq(&mac->lock);
828 hash = mac->multicast_hash;
829 spin_unlock_irq(&mac->lock);
4d1feabc 830
459c51ad 831 zd_chip_set_multicast_hash(&mac->chip, &hash);
e85d0918
DD
832}
833
459c51ad 834static void set_rx_filter_handler(struct work_struct *work)
e85d0918 835{
459c51ad
DD
836 struct zd_mac *mac =
837 container_of(work, struct zd_mac, set_rx_filter_work);
838 int r;
839
840 dev_dbg_f(zd_mac_dev(mac), "\n");
841 r = set_rx_filter(mac);
842 if (r)
843 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
e85d0918
DD
844}
845
459c51ad
DD
846#define SUPPORTED_FIF_FLAGS \
847 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
2c1a1b12 848 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
459c51ad
DD
849static void zd_op_configure_filter(struct ieee80211_hw *hw,
850 unsigned int changed_flags,
851 unsigned int *new_flags,
852 int mc_count, struct dev_mc_list *mclist)
e85d0918 853{
459c51ad
DD
854 struct zd_mc_hash hash;
855 struct zd_mac *mac = zd_hw_mac(hw);
856 unsigned long flags;
857 int i;
e85d0918 858
459c51ad
DD
859 /* Only deal with supported flags */
860 changed_flags &= SUPPORTED_FIF_FLAGS;
861 *new_flags &= SUPPORTED_FIF_FLAGS;
862
863 /* changed_flags is always populated but this driver
864 * doesn't support all FIF flags so its possible we don't
865 * need to do anything */
866 if (!changed_flags)
867 return;
868
869 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
870 zd_mc_add_all(&hash);
871 } else {
872 DECLARE_MAC_BUF(macbuf);
873
874 zd_mc_clear(&hash);
875 for (i = 0; i < mc_count; i++) {
876 if (!mclist)
877 break;
878 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
879 print_mac(macbuf, mclist->dmi_addr));
880 zd_mc_add_addr(&hash, mclist->dmi_addr);
881 mclist = mclist->next;
882 }
e85d0918 883 }
459c51ad
DD
884
885 spin_lock_irqsave(&mac->lock, flags);
886 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
887 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
888 mac->multicast_hash = hash;
889 spin_unlock_irqrestore(&mac->lock, flags);
890 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
891
892 if (changed_flags & FIF_CONTROL)
893 queue_work(zd_workqueue, &mac->set_rx_filter_work);
894
895 /* no handling required for FIF_OTHER_BSS as we don't currently
896 * do BSSID filtering */
897 /* FIXME: in future it would be nice to enable the probe response
898 * filter (so that the driver doesn't see them) until
899 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
900 * have to schedule work to enable prbresp reception, which might
901 * happen too late. For now we'll just listen and forward them all the
902 * time. */
e85d0918
DD
903}
904
459c51ad 905static void set_rts_cts_work(struct work_struct *work)
e85d0918 906{
459c51ad
DD
907 struct zd_mac *mac =
908 container_of(work, struct zd_mac, set_rts_cts_work);
909 unsigned long flags;
910 unsigned int short_preamble;
911
912 mutex_lock(&mac->chip.mutex);
913
914 spin_lock_irqsave(&mac->lock, flags);
915 mac->updating_rts_rate = 0;
916 short_preamble = mac->short_preamble;
917 spin_unlock_irqrestore(&mac->lock, flags);
918
919 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
920 mutex_unlock(&mac->chip.mutex);
e85d0918
DD
921}
922
471b3efd
JB
923static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
924 struct ieee80211_vif *vif,
925 struct ieee80211_bss_conf *bss_conf,
926 u32 changes)
e85d0918 927{
459c51ad
DD
928 struct zd_mac *mac = zd_hw_mac(hw);
929 unsigned long flags;
930
931 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
932
471b3efd 933 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
459c51ad 934 spin_lock_irqsave(&mac->lock, flags);
471b3efd 935 mac->short_preamble = bss_conf->use_short_preamble;
459c51ad
DD
936 if (!mac->updating_rts_rate) {
937 mac->updating_rts_rate = 1;
938 /* FIXME: should disable TX here, until work has
939 * completed and RTS_CTS reg is updated */
940 queue_work(zd_workqueue, &mac->set_rts_cts_work);
941 }
942 spin_unlock_irqrestore(&mac->lock, flags);
943 }
e85d0918
DD
944}
945
84e6dc9a
BR
946static int zd_op_beacon_update(struct ieee80211_hw *hw,
947 struct sk_buff *skb,
948 struct ieee80211_tx_control *ctl)
949{
950 struct zd_mac *mac = zd_hw_mac(hw);
951 zd_mac_config_beacon(hw, skb);
952 kfree_skb(skb);
953 zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
954 hw->conf.beacon_int);
955 return 0;
956}
957
459c51ad
DD
958static const struct ieee80211_ops zd_ops = {
959 .tx = zd_op_tx,
960 .start = zd_op_start,
961 .stop = zd_op_stop,
962 .add_interface = zd_op_add_interface,
963 .remove_interface = zd_op_remove_interface,
964 .config = zd_op_config,
965 .config_interface = zd_op_config_interface,
966 .configure_filter = zd_op_configure_filter,
471b3efd 967 .bss_info_changed = zd_op_bss_info_changed,
84e6dc9a 968 .beacon_update = zd_op_beacon_update,
459c51ad
DD
969};
970
971struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
e85d0918 972{
459c51ad
DD
973 struct zd_mac *mac;
974 struct ieee80211_hw *hw;
e85d0918 975
459c51ad
DD
976 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
977 if (!hw) {
978 dev_dbg_f(&intf->dev, "out of memory\n");
979 return NULL;
db888aed 980 }
459c51ad
DD
981
982 mac = zd_hw_mac(hw);
983
984 memset(mac, 0, sizeof(*mac));
985 spin_lock_init(&mac->lock);
986 mac->hw = hw;
987
988 mac->type = IEEE80211_IF_TYPE_INVALID;
989
990 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
991 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
8318d78a
JB
992 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
993 mac->band.bitrates = mac->rates;
994 mac->band.n_channels = ARRAY_SIZE(zd_channels);
995 mac->band.channels = mac->channels;
996
997 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
998
72e77a8a 999 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
566bfe5a
BR
1000 IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
1001 IEEE80211_HW_SIGNAL_DB;
459c51ad 1002
566bfe5a 1003 hw->max_signal = 100;
459c51ad
DD
1004 hw->queues = 1;
1005 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1006
1007 skb_queue_head_init(&mac->ack_wait_queue);
1008
459c51ad
DD
1009 zd_chip_init(&mac->chip, hw, intf);
1010 housekeeping_init(mac);
1011 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
1012 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
1013 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
72e77a8a 1014 INIT_WORK(&mac->process_intr, zd_process_intr);
459c51ad
DD
1015
1016 SET_IEEE80211_DEV(hw, &intf->dev);
1017 return hw;
e85d0918
DD
1018}
1019
583afd1e
UK
1020#define LINK_LED_WORK_DELAY HZ
1021
c4028958 1022static void link_led_handler(struct work_struct *work)
583afd1e 1023{
c4028958
DH
1024 struct zd_mac *mac =
1025 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
583afd1e 1026 struct zd_chip *chip = &mac->chip;
583afd1e
UK
1027 int is_associated;
1028 int r;
1029
1030 spin_lock_irq(&mac->lock);
459c51ad 1031 is_associated = mac->associated;
583afd1e
UK
1032 spin_unlock_irq(&mac->lock);
1033
1034 r = zd_chip_control_leds(chip,
1035 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1036 if (r)
459c51ad 1037 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
583afd1e
UK
1038
1039 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1040 LINK_LED_WORK_DELAY);
1041}
1042
1043static void housekeeping_init(struct zd_mac *mac)
1044{
c4028958 1045 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
583afd1e
UK
1046}
1047
1048static void housekeeping_enable(struct zd_mac *mac)
1049{
1050 dev_dbg_f(zd_mac_dev(mac), "\n");
1051 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1052 0);
1053}
1054
1055static void housekeeping_disable(struct zd_mac *mac)
1056{
1057 dev_dbg_f(zd_mac_dev(mac), "\n");
1058 cancel_rearming_delayed_workqueue(zd_workqueue,
1059 &mac->housekeeping.link_led_work);
1060 zd_chip_control_leds(&mac->chip, LED_OFF);
1061}