1 /* ZD1211 USB-WLAN driver for Linux
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>
6 * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
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.
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.
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
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27 #include <linux/jiffies.h>
28 #include <net/ieee80211_radiotap.h>
35 struct zd_reg_alpha2_map
{
40 static struct zd_reg_alpha2_map reg_alpha2_map
[] = {
41 { ZD_REGDOMAIN_FCC
, "US" },
42 { ZD_REGDOMAIN_IC
, "CA" },
43 { ZD_REGDOMAIN_ETSI
, "DE" }, /* Generic ETSI, use most restrictive */
44 { ZD_REGDOMAIN_JAPAN
, "JP" },
45 { ZD_REGDOMAIN_JAPAN_2
, "JP" },
46 { ZD_REGDOMAIN_JAPAN_3
, "JP" },
47 { ZD_REGDOMAIN_SPAIN
, "ES" },
48 { ZD_REGDOMAIN_FRANCE
, "FR" },
51 /* This table contains the hardware specific values for the modulation rates. */
52 static const struct ieee80211_rate zd_rates
[] = {
54 .hw_value
= ZD_CCK_RATE_1M
, },
56 .hw_value
= ZD_CCK_RATE_2M
,
57 .hw_value_short
= ZD_CCK_RATE_2M
| ZD_CCK_PREA_SHORT
,
58 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
60 .hw_value
= ZD_CCK_RATE_5_5M
,
61 .hw_value_short
= ZD_CCK_RATE_5_5M
| ZD_CCK_PREA_SHORT
,
62 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
64 .hw_value
= ZD_CCK_RATE_11M
,
65 .hw_value_short
= ZD_CCK_RATE_11M
| ZD_CCK_PREA_SHORT
,
66 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
68 .hw_value
= ZD_OFDM_RATE_6M
,
71 .hw_value
= ZD_OFDM_RATE_9M
,
74 .hw_value
= ZD_OFDM_RATE_12M
,
77 .hw_value
= ZD_OFDM_RATE_18M
,
80 .hw_value
= ZD_OFDM_RATE_24M
,
83 .hw_value
= ZD_OFDM_RATE_36M
,
86 .hw_value
= ZD_OFDM_RATE_48M
,
89 .hw_value
= ZD_OFDM_RATE_54M
,
94 * Zydas retry rates table. Each line is listed in the same order as
95 * in zd_rates[] and contains all the rate used when a packet is sent
96 * starting with a given rates. Let's consider an example :
98 * "11 Mbits : 4, 3, 2, 1, 0" means :
99 * - packet is sent using 4 different rates
100 * - 1st rate is index 3 (ie 11 Mbits)
101 * - 2nd rate is index 2 (ie 5.5 Mbits)
102 * - 3rd rate is index 1 (ie 2 Mbits)
103 * - 4th rate is index 0 (ie 1 Mbits)
106 static const struct tx_retry_rate zd_retry_rates
[] = {
107 { /* 1 Mbits */ 1, { 0 }},
108 { /* 2 Mbits */ 2, { 1, 0 }},
109 { /* 5.5 Mbits */ 3, { 2, 1, 0 }},
110 { /* 11 Mbits */ 4, { 3, 2, 1, 0 }},
111 { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }},
112 { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}},
113 { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }},
114 { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }},
115 { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }},
116 { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }},
117 { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
118 { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
121 static const struct ieee80211_channel zd_channels
[] = {
122 { .center_freq
= 2412, .hw_value
= 1 },
123 { .center_freq
= 2417, .hw_value
= 2 },
124 { .center_freq
= 2422, .hw_value
= 3 },
125 { .center_freq
= 2427, .hw_value
= 4 },
126 { .center_freq
= 2432, .hw_value
= 5 },
127 { .center_freq
= 2437, .hw_value
= 6 },
128 { .center_freq
= 2442, .hw_value
= 7 },
129 { .center_freq
= 2447, .hw_value
= 8 },
130 { .center_freq
= 2452, .hw_value
= 9 },
131 { .center_freq
= 2457, .hw_value
= 10 },
132 { .center_freq
= 2462, .hw_value
= 11 },
133 { .center_freq
= 2467, .hw_value
= 12 },
134 { .center_freq
= 2472, .hw_value
= 13 },
135 { .center_freq
= 2484, .hw_value
= 14 },
138 static void housekeeping_init(struct zd_mac
*mac
);
139 static void housekeeping_enable(struct zd_mac
*mac
);
140 static void housekeeping_disable(struct zd_mac
*mac
);
141 static void beacon_init(struct zd_mac
*mac
);
142 static void beacon_enable(struct zd_mac
*mac
);
143 static void beacon_disable(struct zd_mac
*mac
);
145 static int zd_reg2alpha2(u8 regdomain
, char *alpha2
)
148 struct zd_reg_alpha2_map
*reg_map
;
149 for (i
= 0; i
< ARRAY_SIZE(reg_alpha2_map
); i
++) {
150 reg_map
= ®_alpha2_map
[i
];
151 if (regdomain
== reg_map
->reg
) {
152 alpha2
[0] = reg_map
->alpha2
[0];
153 alpha2
[1] = reg_map
->alpha2
[1];
160 int zd_mac_preinit_hw(struct ieee80211_hw
*hw
)
164 struct zd_mac
*mac
= zd_hw_mac(hw
);
166 r
= zd_chip_read_mac_addr_fw(&mac
->chip
, addr
);
170 SET_IEEE80211_PERM_ADDR(hw
, addr
);
175 int zd_mac_init_hw(struct ieee80211_hw
*hw
)
178 struct zd_mac
*mac
= zd_hw_mac(hw
);
179 struct zd_chip
*chip
= &mac
->chip
;
181 u8 default_regdomain
;
183 r
= zd_chip_enable_int(chip
);
186 r
= zd_chip_init_hw(chip
);
190 ZD_ASSERT(!irqs_disabled());
192 r
= zd_read_regdomain(chip
, &default_regdomain
);
195 spin_lock_irq(&mac
->lock
);
196 mac
->regdomain
= mac
->default_regdomain
= default_regdomain
;
197 spin_unlock_irq(&mac
->lock
);
199 /* We must inform the device that we are doing encryption/decryption in
200 * software at the moment. */
201 r
= zd_set_encryption_type(chip
, ENC_SNIFFER
);
205 r
= zd_reg2alpha2(mac
->regdomain
, alpha2
);
209 r
= regulatory_hint(hw
->wiphy
, alpha2
);
211 zd_chip_disable_int(chip
);
216 void zd_mac_clear(struct zd_mac
*mac
)
218 flush_workqueue(zd_workqueue
);
219 zd_chip_clear(&mac
->chip
);
220 ZD_ASSERT(!spin_is_locked(&mac
->lock
));
221 ZD_MEMCLEAR(mac
, sizeof(struct zd_mac
));
224 static int set_rx_filter(struct zd_mac
*mac
)
227 u32 filter
= STA_RX_FILTER
;
229 spin_lock_irqsave(&mac
->lock
, flags
);
231 filter
|= RX_FILTER_CTRL
;
232 spin_unlock_irqrestore(&mac
->lock
, flags
);
234 return zd_iowrite32(&mac
->chip
, CR_RX_FILTER
, filter
);
237 static int set_mac_and_bssid(struct zd_mac
*mac
)
244 r
= zd_write_mac_addr(&mac
->chip
, mac
->vif
->addr
);
248 /* Vendor driver after setting MAC either sets BSSID for AP or
249 * filter for other modes.
251 if (mac
->type
!= NL80211_IFTYPE_AP
)
252 return set_rx_filter(mac
);
254 return zd_write_bssid(&mac
->chip
, mac
->vif
->addr
);
257 static int set_mc_hash(struct zd_mac
*mac
)
259 struct zd_mc_hash hash
;
261 return zd_chip_set_multicast_hash(&mac
->chip
, &hash
);
264 static int zd_op_start(struct ieee80211_hw
*hw
)
266 struct zd_mac
*mac
= zd_hw_mac(hw
);
267 struct zd_chip
*chip
= &mac
->chip
;
268 struct zd_usb
*usb
= &chip
->usb
;
271 if (!usb
->initialized
) {
272 r
= zd_usb_init_hw(usb
);
277 r
= zd_chip_enable_int(chip
);
281 r
= zd_chip_set_basic_rates(chip
, CR_RATES_80211B
| CR_RATES_80211G
);
284 r
= set_rx_filter(mac
);
287 r
= set_mc_hash(mac
);
290 r
= zd_chip_switch_radio_on(chip
);
293 r
= zd_chip_enable_rxtx(chip
);
296 r
= zd_chip_enable_hwint(chip
);
300 housekeeping_enable(mac
);
302 set_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
305 zd_chip_disable_rxtx(chip
);
307 zd_chip_switch_radio_off(chip
);
309 zd_chip_disable_int(chip
);
314 static void zd_op_stop(struct ieee80211_hw
*hw
)
316 struct zd_mac
*mac
= zd_hw_mac(hw
);
317 struct zd_chip
*chip
= &mac
->chip
;
319 struct sk_buff_head
*ack_wait_queue
= &mac
->ack_wait_queue
;
321 clear_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
323 /* The order here deliberately is a little different from the open()
324 * method, since we need to make sure there is no opportunity for RX
325 * frames to be processed by mac80211 after we have stopped it.
328 zd_chip_disable_rxtx(chip
);
330 housekeeping_disable(mac
);
331 flush_workqueue(zd_workqueue
);
333 zd_chip_disable_hwint(chip
);
334 zd_chip_switch_radio_off(chip
);
335 zd_chip_disable_int(chip
);
338 while ((skb
= skb_dequeue(ack_wait_queue
)))
339 dev_kfree_skb_any(skb
);
343 * zd_mac_tx_status - reports tx status of a packet if required
344 * @hw - a &struct ieee80211_hw pointer
346 * @flags: extra flags to set in the TX status info
347 * @ackssi: ACK signal strength
348 * @success - True for successful transmission of the frame
350 * This information calls ieee80211_tx_status_irqsafe() if required by the
351 * control information. It copies the control information into the status
354 * If no status information has been requested, the skb is freed.
356 static void zd_mac_tx_status(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
357 int ackssi
, struct tx_status
*tx_status
)
359 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
361 int success
= 1, retry
= 1;
363 const struct tx_retry_rate
*retries
;
365 ieee80211_tx_info_clear_status(info
);
368 success
= !tx_status
->failure
;
369 retry
= tx_status
->retry
+ success
;
374 info
->flags
|= IEEE80211_TX_STAT_ACK
;
377 info
->flags
&= ~IEEE80211_TX_STAT_ACK
;
380 first_idx
= info
->status
.rates
[0].idx
;
381 ZD_ASSERT(0<=first_idx
&& first_idx
<ARRAY_SIZE(zd_retry_rates
));
382 retries
= &zd_retry_rates
[first_idx
];
383 ZD_ASSERT(1 <= retry
&& retry
<= retries
->count
);
385 info
->status
.rates
[0].idx
= retries
->rate
[0];
386 info
->status
.rates
[0].count
= 1; // (retry > 1 ? 2 : 1);
388 for (i
=1; i
<IEEE80211_TX_MAX_RATES
-1 && i
<retry
; i
++) {
389 info
->status
.rates
[i
].idx
= retries
->rate
[i
];
390 info
->status
.rates
[i
].count
= 1; // ((i==retry-1) && success ? 1:2);
392 for (; i
<IEEE80211_TX_MAX_RATES
&& i
<retry
; i
++) {
393 info
->status
.rates
[i
].idx
= retries
->rate
[retry
- 1];
394 info
->status
.rates
[i
].count
= 1; // (success ? 1:2);
396 if (i
<IEEE80211_TX_MAX_RATES
)
397 info
->status
.rates
[i
].idx
= -1; /* terminate */
399 info
->status
.ack_signal
= ackssi
;
400 ieee80211_tx_status_irqsafe(hw
, skb
);
404 * zd_mac_tx_failed - callback for failed frames
405 * @dev: the mac80211 wireless device
407 * This function is called if a frame couldn't be successfully
408 * transferred. The first frame from the tx queue, will be selected and
409 * reported as error to the upper layers.
411 void zd_mac_tx_failed(struct urb
*urb
)
413 struct ieee80211_hw
* hw
= zd_usb_to_hw(urb
->context
);
414 struct zd_mac
*mac
= zd_hw_mac(hw
);
415 struct sk_buff_head
*q
= &mac
->ack_wait_queue
;
417 struct tx_status
*tx_status
= (struct tx_status
*)urb
->transfer_buffer
;
419 int success
= !tx_status
->failure
;
420 int retry
= tx_status
->retry
+ success
;
424 q
= &mac
->ack_wait_queue
;
425 spin_lock_irqsave(&q
->lock
, flags
);
427 skb_queue_walk(q
, skb
) {
428 struct ieee80211_hdr
*tx_hdr
;
429 struct ieee80211_tx_info
*info
;
430 int first_idx
, final_idx
;
431 const struct tx_retry_rate
*retries
;
436 /* if the hardware reports a failure and we had a 802.11 ACK
437 * pending, then we skip the first skb when searching for a
439 if (tx_status
->failure
&& mac
->ack_pending
&&
440 skb_queue_is_first(q
, skb
)) {
444 tx_hdr
= (struct ieee80211_hdr
*)skb
->data
;
446 /* we skip all frames not matching the reported destination */
447 if (unlikely(memcmp(tx_hdr
->addr1
, tx_status
->mac
, ETH_ALEN
))) {
451 /* we skip all frames not matching the reported final rate */
453 info
= IEEE80211_SKB_CB(skb
);
454 first_idx
= info
->status
.rates
[0].idx
;
455 ZD_ASSERT(0<=first_idx
&& first_idx
<ARRAY_SIZE(zd_retry_rates
));
456 retries
= &zd_retry_rates
[first_idx
];
457 if (retry
<= 0 || retry
> retries
->count
)
460 final_idx
= retries
->rate
[retry
- 1];
461 final_rate
= zd_rates
[final_idx
].hw_value
;
463 if (final_rate
!= tx_status
->rate
) {
472 for (i
=1; i
<=position
; i
++) {
473 skb
= __skb_dequeue(q
);
474 zd_mac_tx_status(hw
, skb
,
475 mac
->ack_pending
? mac
->ack_signal
: 0,
476 i
== position
? tx_status
: NULL
);
477 mac
->ack_pending
= 0;
481 spin_unlock_irqrestore(&q
->lock
, flags
);
485 * zd_mac_tx_to_dev - callback for USB layer
486 * @skb: a &sk_buff pointer
487 * @error: error value, 0 if transmission successful
489 * Informs the MAC layer that the frame has successfully transferred to the
490 * device. If an ACK is required and the transfer to the device has been
491 * successful, the packets are put on the @ack_wait_queue with
492 * the control set removed.
494 void zd_mac_tx_to_dev(struct sk_buff
*skb
, int error
)
496 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
497 struct ieee80211_hw
*hw
= info
->rate_driver_data
[0];
498 struct zd_mac
*mac
= zd_hw_mac(hw
);
500 ieee80211_tx_info_clear_status(info
);
502 skb_pull(skb
, sizeof(struct zd_ctrlset
));
503 if (unlikely(error
||
504 (info
->flags
& IEEE80211_TX_CTL_NO_ACK
))) {
506 * FIXME : do we need to fill in anything ?
508 ieee80211_tx_status_irqsafe(hw
, skb
);
510 struct sk_buff_head
*q
= &mac
->ack_wait_queue
;
512 skb_queue_tail(q
, skb
);
513 while (skb_queue_len(q
) > ZD_MAC_MAX_ACK_WAITERS
) {
514 zd_mac_tx_status(hw
, skb_dequeue(q
),
515 mac
->ack_pending
? mac
->ack_signal
: 0,
517 mac
->ack_pending
= 0;
522 static int zd_calc_tx_length_us(u8
*service
, u8 zd_rate
, u16 tx_length
)
524 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
525 * the zd-rate values.
527 static const u8 rate_divisor
[] = {
528 [ZD_PURE_RATE(ZD_CCK_RATE_1M
)] = 1,
529 [ZD_PURE_RATE(ZD_CCK_RATE_2M
)] = 2,
530 /* Bits must be doubled. */
531 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M
)] = 11,
532 [ZD_PURE_RATE(ZD_CCK_RATE_11M
)] = 11,
533 [ZD_PURE_RATE(ZD_OFDM_RATE_6M
)] = 6,
534 [ZD_PURE_RATE(ZD_OFDM_RATE_9M
)] = 9,
535 [ZD_PURE_RATE(ZD_OFDM_RATE_12M
)] = 12,
536 [ZD_PURE_RATE(ZD_OFDM_RATE_18M
)] = 18,
537 [ZD_PURE_RATE(ZD_OFDM_RATE_24M
)] = 24,
538 [ZD_PURE_RATE(ZD_OFDM_RATE_36M
)] = 36,
539 [ZD_PURE_RATE(ZD_OFDM_RATE_48M
)] = 48,
540 [ZD_PURE_RATE(ZD_OFDM_RATE_54M
)] = 54,
543 u32 bits
= (u32
)tx_length
* 8;
546 divisor
= rate_divisor
[ZD_PURE_RATE(zd_rate
)];
551 case ZD_CCK_RATE_5_5M
:
552 bits
= (2*bits
) + 10; /* round up to the next integer */
554 case ZD_CCK_RATE_11M
:
557 *service
&= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION
;
558 if (0 < t
&& t
<= 3) {
559 *service
|= ZD_PLCP_SERVICE_LENGTH_EXTENSION
;
562 bits
+= 10; /* round up to the next integer */
569 static void cs_set_control(struct zd_mac
*mac
, struct zd_ctrlset
*cs
,
570 struct ieee80211_hdr
*header
,
571 struct ieee80211_tx_info
*info
)
575 * - if backoff needed, enable bit 0
576 * - if burst (backoff not needed) disable bit 0
582 if (info
->flags
& IEEE80211_TX_CTL_FIRST_FRAGMENT
)
583 cs
->control
|= ZD_CS_NEED_RANDOM_BACKOFF
;
585 /* No ACK expected (multicast, etc.) */
586 if (info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
587 cs
->control
|= ZD_CS_NO_ACK
;
590 if (ieee80211_is_pspoll(header
->frame_control
))
591 cs
->control
|= ZD_CS_PS_POLL_FRAME
;
593 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_USE_RTS_CTS
)
594 cs
->control
|= ZD_CS_RTS
;
596 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
)
597 cs
->control
|= ZD_CS_SELF_CTS
;
599 /* FIXME: Management frame? */
602 static int zd_mac_config_beacon(struct ieee80211_hw
*hw
, struct sk_buff
*beacon
)
604 struct zd_mac
*mac
= zd_hw_mac(hw
);
607 /* 4 more bytes for tail CRC */
608 u32 full_len
= beacon
->len
+ 4;
609 unsigned long end_jiffies
, message_jiffies
;
611 mutex_lock(&mac
->chip
.mutex
);
613 r
= zd_iowrite32_locked(&mac
->chip
, 0, CR_BCN_FIFO_SEMAPHORE
);
616 r
= zd_ioread32_locked(&mac
->chip
, &tmp
, CR_BCN_FIFO_SEMAPHORE
);
620 end_jiffies
= jiffies
+ HZ
/ 2; /*~500ms*/
621 message_jiffies
= jiffies
+ HZ
/ 10; /*~100ms*/
623 r
= zd_ioread32_locked(&mac
->chip
, &tmp
, CR_BCN_FIFO_SEMAPHORE
);
626 if (time_is_before_eq_jiffies(message_jiffies
)) {
627 message_jiffies
= jiffies
+ HZ
/ 10;
628 dev_err(zd_mac_dev(mac
),
629 "CR_BCN_FIFO_SEMAPHORE not ready\n");
630 if (time_is_before_eq_jiffies(end_jiffies
)) {
631 dev_err(zd_mac_dev(mac
),
632 "Giving up beacon config.\n");
640 r
= zd_iowrite32_locked(&mac
->chip
, full_len
- 1, CR_BCN_FIFO
);
643 if (zd_chip_is_zd1211b(&mac
->chip
)) {
644 r
= zd_iowrite32_locked(&mac
->chip
, full_len
- 1,
650 for (j
= 0 ; j
< beacon
->len
; j
++) {
651 r
= zd_iowrite32_locked(&mac
->chip
, *((u8
*)(beacon
->data
+ j
)),
657 for (j
= 0; j
< 4; j
++) {
658 r
= zd_iowrite32_locked(&mac
->chip
, 0x0, CR_BCN_FIFO
);
665 * Try very hard to release device beacon semaphore, as otherwise
666 * device/driver can be left in unusable state.
668 end_jiffies
= jiffies
+ HZ
/ 2; /*~500ms*/
669 ret
= zd_iowrite32_locked(&mac
->chip
, 1, CR_BCN_FIFO_SEMAPHORE
);
671 if (time_is_before_eq_jiffies(end_jiffies
)) {
677 ret
= zd_iowrite32_locked(&mac
->chip
, 1, CR_BCN_FIFO_SEMAPHORE
);
681 dev_err(zd_mac_dev(mac
), "Could not release "
682 "CR_BCN_FIFO_SEMAPHORE!\n");
683 if (r
< 0 || ret
< 0) {
689 /* 802.11b/g 2.4G CCK 1Mb
690 * 802.11a, not yet implemented, uses different values (see GPL vendor
693 r
= zd_iowrite32_locked(&mac
->chip
, 0x00000400 | (full_len
<< 19),
696 mutex_unlock(&mac
->chip
.mutex
);
700 static int fill_ctrlset(struct zd_mac
*mac
,
704 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
705 unsigned int frag_len
= skb
->len
+ FCS_LEN
;
706 unsigned int packet_length
;
707 struct ieee80211_rate
*txrate
;
708 struct zd_ctrlset
*cs
= (struct zd_ctrlset
*)
709 skb_push(skb
, sizeof(struct zd_ctrlset
));
710 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
712 ZD_ASSERT(frag_len
<= 0xffff);
714 txrate
= ieee80211_get_tx_rate(mac
->hw
, info
);
716 cs
->modulation
= txrate
->hw_value
;
717 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_USE_SHORT_PREAMBLE
)
718 cs
->modulation
= txrate
->hw_value_short
;
720 cs
->tx_length
= cpu_to_le16(frag_len
);
722 cs_set_control(mac
, cs
, hdr
, info
);
724 packet_length
= frag_len
+ sizeof(struct zd_ctrlset
) + 10;
725 ZD_ASSERT(packet_length
<= 0xffff);
726 /* ZD1211B: Computing the length difference this way, gives us
727 * flexibility to compute the packet length.
729 cs
->packet_length
= cpu_to_le16(zd_chip_is_zd1211b(&mac
->chip
) ?
730 packet_length
- frag_len
: packet_length
);
734 * - transmit frame length in microseconds
735 * - seems to be derived from frame length
736 * - see Cal_Us_Service() in zdinlinef.h
737 * - if macp->bTxBurstEnable is enabled, then multiply by 4
738 * - bTxBurstEnable is never set in the vendor driver
741 * - "for PLCP configuration"
742 * - always 0 except in some situations at 802.11b 11M
743 * - see line 53 of zdinlinef.h
746 r
= zd_calc_tx_length_us(&cs
->service
, ZD_RATE(cs
->modulation
),
747 le16_to_cpu(cs
->tx_length
));
750 cs
->current_length
= cpu_to_le16(r
);
751 cs
->next_frame_length
= 0;
757 * zd_op_tx - transmits a network frame to the device
759 * @dev: mac80211 hardware device
760 * @skb: socket buffer
761 * @control: the control structure
763 * This function transmit an IEEE 802.11 network frame to the device. The
764 * control block of the skbuff will be initialized. If necessary the incoming
765 * mac80211 queues will be stopped.
767 static int zd_op_tx(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
769 struct zd_mac
*mac
= zd_hw_mac(hw
);
770 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
773 r
= fill_ctrlset(mac
, skb
);
777 info
->rate_driver_data
[0] = hw
;
779 r
= zd_usb_tx(&mac
->chip
.usb
, skb
);
790 * filter_ack - filters incoming packets for acknowledgements
791 * @dev: the mac80211 device
792 * @rx_hdr: received header
793 * @stats: the status for the received packet
795 * This functions looks for ACK packets and tries to match them with the
796 * frames in the tx queue. If a match is found the frame will be dequeued and
797 * the upper layers is informed about the successful transmission. If
798 * mac80211 queues have been stopped and the number of frames still to be
799 * transmitted is low the queues will be opened again.
801 * Returns 1 if the frame was an ACK, 0 if it was ignored.
803 static int filter_ack(struct ieee80211_hw
*hw
, struct ieee80211_hdr
*rx_hdr
,
804 struct ieee80211_rx_status
*stats
)
806 struct zd_mac
*mac
= zd_hw_mac(hw
);
808 struct sk_buff_head
*q
;
813 if (!ieee80211_is_ack(rx_hdr
->frame_control
))
816 q
= &mac
->ack_wait_queue
;
817 spin_lock_irqsave(&q
->lock
, flags
);
818 skb_queue_walk(q
, skb
) {
819 struct ieee80211_hdr
*tx_hdr
;
823 if (mac
->ack_pending
&& skb_queue_is_first(q
, skb
))
826 tx_hdr
= (struct ieee80211_hdr
*)skb
->data
;
827 if (likely(!memcmp(tx_hdr
->addr2
, rx_hdr
->addr1
, ETH_ALEN
)))
835 for (i
=1; i
<position
; i
++) {
836 skb
= __skb_dequeue(q
);
837 zd_mac_tx_status(hw
, skb
,
838 mac
->ack_pending
? mac
->ack_signal
: 0,
840 mac
->ack_pending
= 0;
843 mac
->ack_pending
= 1;
844 mac
->ack_signal
= stats
->signal
;
846 /* Prevent pending tx-packet on AP-mode */
847 if (mac
->type
== NL80211_IFTYPE_AP
) {
848 skb
= __skb_dequeue(q
);
849 zd_mac_tx_status(hw
, skb
, mac
->ack_signal
, NULL
);
850 mac
->ack_pending
= 0;
854 spin_unlock_irqrestore(&q
->lock
, flags
);
858 int zd_mac_rx(struct ieee80211_hw
*hw
, const u8
*buffer
, unsigned int length
)
860 struct zd_mac
*mac
= zd_hw_mac(hw
);
861 struct ieee80211_rx_status stats
;
862 const struct rx_status
*status
;
870 if (length
< ZD_PLCP_HEADER_SIZE
+ 10 /* IEEE80211_1ADDR_LEN */ +
871 FCS_LEN
+ sizeof(struct rx_status
))
874 memset(&stats
, 0, sizeof(stats
));
876 /* Note about pass_failed_fcs and pass_ctrl access below:
877 * mac locking intentionally omitted here, as this is the only unlocked
878 * reader and the only writer is configure_filter. Plus, if there were
879 * any races accessing these variables, it wouldn't really matter.
880 * If mac80211 ever provides a way for us to access filter flags
881 * from outside configure_filter, we could improve on this. Also, this
882 * situation may change once we implement some kind of DMA-into-skb
885 /* Caller has to ensure that length >= sizeof(struct rx_status). */
886 status
= (struct rx_status
*)
887 (buffer
+ (length
- sizeof(struct rx_status
)));
888 if (status
->frame_status
& ZD_RX_ERROR
) {
889 if (mac
->pass_failed_fcs
&&
890 (status
->frame_status
& ZD_RX_CRC32_ERROR
)) {
891 stats
.flag
|= RX_FLAG_FAILED_FCS_CRC
;
898 stats
.freq
= zd_channels
[_zd_chip_get_channel(&mac
->chip
) - 1].center_freq
;
899 stats
.band
= IEEE80211_BAND_2GHZ
;
900 stats
.signal
= status
->signal_strength
;
902 rate
= zd_rx_rate(buffer
, status
);
904 /* todo: return index in the big switches in zd_rx_rate instead */
905 for (i
= 0; i
< mac
->band
.n_bitrates
; i
++)
906 if (rate
== mac
->band
.bitrates
[i
].hw_value
)
909 length
-= ZD_PLCP_HEADER_SIZE
+ sizeof(struct rx_status
);
910 buffer
+= ZD_PLCP_HEADER_SIZE
;
912 /* Except for bad frames, filter each frame to see if it is an ACK, in
913 * which case our internal TX tracking is updated. Normally we then
914 * bail here as there's no need to pass ACKs on up to the stack, but
915 * there is also the case where the stack has requested us to pass
916 * control frames on up (pass_ctrl) which we must consider. */
918 filter_ack(hw
, (struct ieee80211_hdr
*)buffer
, &stats
)
922 fc
= get_unaligned((__le16
*)buffer
);
923 need_padding
= ieee80211_is_data_qos(fc
) ^ ieee80211_has_a4(fc
);
925 skb
= dev_alloc_skb(length
+ (need_padding
? 2 : 0));
929 /* Make sure the payload data is 4 byte aligned. */
933 /* FIXME : could we avoid this big memcpy ? */
934 memcpy(skb_put(skb
, length
), buffer
, length
);
936 memcpy(IEEE80211_SKB_RXCB(skb
), &stats
, sizeof(stats
));
937 ieee80211_rx_irqsafe(hw
, skb
);
941 static int zd_op_add_interface(struct ieee80211_hw
*hw
,
942 struct ieee80211_vif
*vif
)
944 struct zd_mac
*mac
= zd_hw_mac(hw
);
946 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
947 if (mac
->type
!= NL80211_IFTYPE_UNSPECIFIED
)
951 case NL80211_IFTYPE_MONITOR
:
952 case NL80211_IFTYPE_MESH_POINT
:
953 case NL80211_IFTYPE_STATION
:
954 case NL80211_IFTYPE_ADHOC
:
955 mac
->type
= vif
->type
;
963 return set_mac_and_bssid(mac
);
966 static void zd_op_remove_interface(struct ieee80211_hw
*hw
,
967 struct ieee80211_vif
*vif
)
969 struct zd_mac
*mac
= zd_hw_mac(hw
);
970 mac
->type
= NL80211_IFTYPE_UNSPECIFIED
;
972 zd_set_beacon_interval(&mac
->chip
, 0, 0, NL80211_IFTYPE_UNSPECIFIED
);
973 zd_write_mac_addr(&mac
->chip
, NULL
);
976 static int zd_op_config(struct ieee80211_hw
*hw
, u32 changed
)
978 struct zd_mac
*mac
= zd_hw_mac(hw
);
979 struct ieee80211_conf
*conf
= &hw
->conf
;
981 return zd_chip_set_channel(&mac
->chip
, conf
->channel
->hw_value
);
984 static void zd_beacon_done(struct zd_mac
*mac
)
986 struct sk_buff
*skb
, *beacon
;
988 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
990 if (!mac
->vif
|| mac
->vif
->type
!= NL80211_IFTYPE_AP
)
994 * Send out buffered broad- and multicast frames.
996 while (!ieee80211_queue_stopped(mac
->hw
, 0)) {
997 skb
= ieee80211_get_buffered_bc(mac
->hw
, mac
->vif
);
1000 zd_op_tx(mac
->hw
, skb
);
1004 * Fetch next beacon so that tim_count is updated.
1006 beacon
= ieee80211_beacon_get(mac
->hw
, mac
->vif
);
1008 zd_mac_config_beacon(mac
->hw
, beacon
);
1012 spin_lock_irq(&mac
->lock
);
1013 mac
->beacon
.last_update
= jiffies
;
1014 spin_unlock_irq(&mac
->lock
);
1017 static void zd_process_intr(struct work_struct
*work
)
1020 unsigned long flags
;
1021 struct zd_mac
*mac
= container_of(work
, struct zd_mac
, process_intr
);
1023 spin_lock_irqsave(&mac
->lock
, flags
);
1024 int_status
= le16_to_cpu(*(__le16
*)(mac
->intr_buffer
+ 4));
1025 spin_unlock_irqrestore(&mac
->lock
, flags
);
1027 if (int_status
& INT_CFG_NEXT_BCN
) {
1028 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1029 zd_beacon_done(mac
);
1031 dev_dbg_f(zd_mac_dev(mac
), "Unsupported interrupt\n");
1034 zd_chip_enable_hwint(&mac
->chip
);
1038 static u64
zd_op_prepare_multicast(struct ieee80211_hw
*hw
,
1039 struct netdev_hw_addr_list
*mc_list
)
1041 struct zd_mac
*mac
= zd_hw_mac(hw
);
1042 struct zd_mc_hash hash
;
1043 struct netdev_hw_addr
*ha
;
1047 netdev_hw_addr_list_for_each(ha
, mc_list
) {
1048 dev_dbg_f(zd_mac_dev(mac
), "mc addr %pM\n", ha
->addr
);
1049 zd_mc_add_addr(&hash
, ha
->addr
);
1052 return hash
.low
| ((u64
)hash
.high
<< 32);
1055 #define SUPPORTED_FIF_FLAGS \
1056 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1057 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1058 static void zd_op_configure_filter(struct ieee80211_hw
*hw
,
1059 unsigned int changed_flags
,
1060 unsigned int *new_flags
,
1063 struct zd_mc_hash hash
= {
1065 .high
= multicast
>> 32,
1067 struct zd_mac
*mac
= zd_hw_mac(hw
);
1068 unsigned long flags
;
1071 /* Only deal with supported flags */
1072 changed_flags
&= SUPPORTED_FIF_FLAGS
;
1073 *new_flags
&= SUPPORTED_FIF_FLAGS
;
1076 * If multicast parameter (as returned by zd_op_prepare_multicast)
1077 * has changed, no bit in changed_flags is set. To handle this
1078 * situation, we do not return if changed_flags is 0. If we do so,
1079 * we will have some issue with IPv6 which uses multicast for link
1080 * layer address resolution.
1082 if (*new_flags
& (FIF_PROMISC_IN_BSS
| FIF_ALLMULTI
))
1083 zd_mc_add_all(&hash
);
1085 spin_lock_irqsave(&mac
->lock
, flags
);
1086 mac
->pass_failed_fcs
= !!(*new_flags
& FIF_FCSFAIL
);
1087 mac
->pass_ctrl
= !!(*new_flags
& FIF_CONTROL
);
1088 mac
->multicast_hash
= hash
;
1089 spin_unlock_irqrestore(&mac
->lock
, flags
);
1091 zd_chip_set_multicast_hash(&mac
->chip
, &hash
);
1093 if (changed_flags
& FIF_CONTROL
) {
1094 r
= set_rx_filter(mac
);
1096 dev_err(zd_mac_dev(mac
), "set_rx_filter error %d\n", r
);
1099 /* no handling required for FIF_OTHER_BSS as we don't currently
1100 * do BSSID filtering */
1101 /* FIXME: in future it would be nice to enable the probe response
1102 * filter (so that the driver doesn't see them) until
1103 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1104 * have to schedule work to enable prbresp reception, which might
1105 * happen too late. For now we'll just listen and forward them all the
1109 static void set_rts_cts(struct zd_mac
*mac
, unsigned int short_preamble
)
1111 mutex_lock(&mac
->chip
.mutex
);
1112 zd_chip_set_rts_cts_rate_locked(&mac
->chip
, short_preamble
);
1113 mutex_unlock(&mac
->chip
.mutex
);
1116 static void zd_op_bss_info_changed(struct ieee80211_hw
*hw
,
1117 struct ieee80211_vif
*vif
,
1118 struct ieee80211_bss_conf
*bss_conf
,
1121 struct zd_mac
*mac
= zd_hw_mac(hw
);
1124 dev_dbg_f(zd_mac_dev(mac
), "changes: %x\n", changes
);
1126 if (mac
->type
== NL80211_IFTYPE_MESH_POINT
||
1127 mac
->type
== NL80211_IFTYPE_ADHOC
) {
1129 if (changes
& BSS_CHANGED_BEACON
) {
1130 struct sk_buff
*beacon
= ieee80211_beacon_get(hw
, vif
);
1133 zd_chip_disable_hwint(&mac
->chip
);
1134 zd_mac_config_beacon(hw
, beacon
);
1135 zd_chip_enable_hwint(&mac
->chip
);
1140 if (changes
& BSS_CHANGED_BEACON_ENABLED
) {
1144 if (bss_conf
->enable_beacon
) {
1145 period
= bss_conf
->dtim_period
;
1146 interval
= bss_conf
->beacon_int
;
1149 spin_lock_irq(&mac
->lock
);
1150 mac
->beacon
.period
= period
;
1151 mac
->beacon
.interval
= interval
;
1152 mac
->beacon
.last_update
= jiffies
;
1153 spin_unlock_irq(&mac
->lock
);
1155 zd_set_beacon_interval(&mac
->chip
, interval
, period
,
1159 associated
= is_valid_ether_addr(bss_conf
->bssid
);
1161 spin_lock_irq(&mac
->lock
);
1162 mac
->associated
= associated
;
1163 spin_unlock_irq(&mac
->lock
);
1165 /* TODO: do hardware bssid filtering */
1167 if (changes
& BSS_CHANGED_ERP_PREAMBLE
) {
1168 spin_lock_irq(&mac
->lock
);
1169 mac
->short_preamble
= bss_conf
->use_short_preamble
;
1170 spin_unlock_irq(&mac
->lock
);
1172 set_rts_cts(mac
, bss_conf
->use_short_preamble
);
1176 static u64
zd_op_get_tsf(struct ieee80211_hw
*hw
)
1178 struct zd_mac
*mac
= zd_hw_mac(hw
);
1179 return zd_chip_get_tsf(&mac
->chip
);
1182 static const struct ieee80211_ops zd_ops
= {
1184 .start
= zd_op_start
,
1186 .add_interface
= zd_op_add_interface
,
1187 .remove_interface
= zd_op_remove_interface
,
1188 .config
= zd_op_config
,
1189 .prepare_multicast
= zd_op_prepare_multicast
,
1190 .configure_filter
= zd_op_configure_filter
,
1191 .bss_info_changed
= zd_op_bss_info_changed
,
1192 .get_tsf
= zd_op_get_tsf
,
1195 struct ieee80211_hw
*zd_mac_alloc_hw(struct usb_interface
*intf
)
1198 struct ieee80211_hw
*hw
;
1200 hw
= ieee80211_alloc_hw(sizeof(struct zd_mac
), &zd_ops
);
1202 dev_dbg_f(&intf
->dev
, "out of memory\n");
1206 mac
= zd_hw_mac(hw
);
1208 memset(mac
, 0, sizeof(*mac
));
1209 spin_lock_init(&mac
->lock
);
1212 mac
->type
= NL80211_IFTYPE_UNSPECIFIED
;
1214 memcpy(mac
->channels
, zd_channels
, sizeof(zd_channels
));
1215 memcpy(mac
->rates
, zd_rates
, sizeof(zd_rates
));
1216 mac
->band
.n_bitrates
= ARRAY_SIZE(zd_rates
);
1217 mac
->band
.bitrates
= mac
->rates
;
1218 mac
->band
.n_channels
= ARRAY_SIZE(zd_channels
);
1219 mac
->band
.channels
= mac
->channels
;
1221 hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = &mac
->band
;
1223 hw
->flags
= IEEE80211_HW_RX_INCLUDES_FCS
|
1224 IEEE80211_HW_SIGNAL_UNSPEC
|
1225 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING
;
1227 hw
->wiphy
->interface_modes
=
1228 BIT(NL80211_IFTYPE_MESH_POINT
) |
1229 BIT(NL80211_IFTYPE_STATION
) |
1230 BIT(NL80211_IFTYPE_ADHOC
);
1232 hw
->max_signal
= 100;
1234 hw
->extra_tx_headroom
= sizeof(struct zd_ctrlset
);
1237 * Tell mac80211 that we support multi rate retries
1239 hw
->max_rates
= IEEE80211_TX_MAX_RATES
;
1240 hw
->max_rate_tries
= 18; /* 9 rates * 2 retries/rate */
1242 skb_queue_head_init(&mac
->ack_wait_queue
);
1243 mac
->ack_pending
= 0;
1245 zd_chip_init(&mac
->chip
, hw
, intf
);
1246 housekeeping_init(mac
);
1248 INIT_WORK(&mac
->process_intr
, zd_process_intr
);
1250 SET_IEEE80211_DEV(hw
, &intf
->dev
);
1254 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1256 static void beacon_watchdog_handler(struct work_struct
*work
)
1258 struct zd_mac
*mac
=
1259 container_of(work
, struct zd_mac
, beacon
.watchdog_work
.work
);
1260 struct sk_buff
*beacon
;
1261 unsigned long timeout
;
1262 int interval
, period
;
1264 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1266 if (mac
->type
!= NL80211_IFTYPE_AP
|| !mac
->vif
)
1269 spin_lock_irq(&mac
->lock
);
1270 interval
= mac
->beacon
.interval
;
1271 period
= mac
->beacon
.period
;
1272 timeout
= mac
->beacon
.last_update
+ msecs_to_jiffies(interval
) + HZ
;
1273 spin_unlock_irq(&mac
->lock
);
1275 if (interval
> 0 && time_is_before_jiffies(timeout
)) {
1276 dev_dbg_f(zd_mac_dev(mac
), "beacon interrupt stalled, "
1278 "(interval: %d, dtim: %d)\n",
1281 zd_chip_disable_hwint(&mac
->chip
);
1283 beacon
= ieee80211_beacon_get(mac
->hw
, mac
->vif
);
1285 zd_mac_config_beacon(mac
->hw
, beacon
);
1289 zd_set_beacon_interval(&mac
->chip
, interval
, period
, mac
->type
);
1291 zd_chip_enable_hwint(&mac
->chip
);
1293 spin_lock_irq(&mac
->lock
);
1294 mac
->beacon
.last_update
= jiffies
;
1295 spin_unlock_irq(&mac
->lock
);
1299 queue_delayed_work(zd_workqueue
, &mac
->beacon
.watchdog_work
,
1300 BEACON_WATCHDOG_DELAY
);
1303 static void beacon_init(struct zd_mac
*mac
)
1305 INIT_DELAYED_WORK(&mac
->beacon
.watchdog_work
, beacon_watchdog_handler
);
1308 static void beacon_enable(struct zd_mac
*mac
)
1310 dev_dbg_f(zd_mac_dev(mac
), "\n");
1312 mac
->beacon
.last_update
= jiffies
;
1313 queue_delayed_work(zd_workqueue
, &mac
->beacon
.watchdog_work
,
1314 BEACON_WATCHDOG_DELAY
);
1317 static void beacon_disable(struct zd_mac
*mac
)
1319 dev_dbg_f(zd_mac_dev(mac
), "\n");
1320 cancel_delayed_work_sync(&mac
->beacon
.watchdog_work
);
1323 #define LINK_LED_WORK_DELAY HZ
1325 static void link_led_handler(struct work_struct
*work
)
1327 struct zd_mac
*mac
=
1328 container_of(work
, struct zd_mac
, housekeeping
.link_led_work
.work
);
1329 struct zd_chip
*chip
= &mac
->chip
;
1333 spin_lock_irq(&mac
->lock
);
1334 is_associated
= mac
->associated
;
1335 spin_unlock_irq(&mac
->lock
);
1337 r
= zd_chip_control_leds(chip
,
1338 is_associated
? ZD_LED_ASSOCIATED
: ZD_LED_SCANNING
);
1340 dev_dbg_f(zd_mac_dev(mac
), "zd_chip_control_leds error %d\n", r
);
1342 queue_delayed_work(zd_workqueue
, &mac
->housekeeping
.link_led_work
,
1343 LINK_LED_WORK_DELAY
);
1346 static void housekeeping_init(struct zd_mac
*mac
)
1348 INIT_DELAYED_WORK(&mac
->housekeeping
.link_led_work
, link_led_handler
);
1351 static void housekeeping_enable(struct zd_mac
*mac
)
1353 dev_dbg_f(zd_mac_dev(mac
), "\n");
1354 queue_delayed_work(zd_workqueue
, &mac
->housekeeping
.link_led_work
,
1358 static void housekeeping_disable(struct zd_mac
*mac
)
1360 dev_dbg_f(zd_mac_dev(mac
), "\n");
1361 cancel_delayed_work_sync(&mac
->housekeeping
.link_led_work
);
1362 zd_chip_control_leds(&mac
->chip
, ZD_LED_OFF
);