1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* ZD1211 USB-WLAN driver for Linux
4 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
5 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
6 * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
7 * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/slab.h>
13 #include <linux/usb.h>
14 #include <linux/jiffies.h>
15 #include <net/ieee80211_radiotap.h>
22 struct zd_reg_alpha2_map
{
27 static struct zd_reg_alpha2_map reg_alpha2_map
[] = {
28 { ZD_REGDOMAIN_FCC
, "US" },
29 { ZD_REGDOMAIN_IC
, "CA" },
30 { ZD_REGDOMAIN_ETSI
, "DE" }, /* Generic ETSI, use most restrictive */
31 { ZD_REGDOMAIN_JAPAN
, "JP" },
32 { ZD_REGDOMAIN_JAPAN_2
, "JP" },
33 { ZD_REGDOMAIN_JAPAN_3
, "JP" },
34 { ZD_REGDOMAIN_SPAIN
, "ES" },
35 { ZD_REGDOMAIN_FRANCE
, "FR" },
38 /* This table contains the hardware specific values for the modulation rates. */
39 static const struct ieee80211_rate zd_rates
[] = {
41 .hw_value
= ZD_CCK_RATE_1M
, },
43 .hw_value
= ZD_CCK_RATE_2M
,
44 .hw_value_short
= ZD_CCK_RATE_2M
| ZD_CCK_PREA_SHORT
,
45 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
47 .hw_value
= ZD_CCK_RATE_5_5M
,
48 .hw_value_short
= ZD_CCK_RATE_5_5M
| ZD_CCK_PREA_SHORT
,
49 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
51 .hw_value
= ZD_CCK_RATE_11M
,
52 .hw_value_short
= ZD_CCK_RATE_11M
| ZD_CCK_PREA_SHORT
,
53 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
55 .hw_value
= ZD_OFDM_RATE_6M
,
58 .hw_value
= ZD_OFDM_RATE_9M
,
61 .hw_value
= ZD_OFDM_RATE_12M
,
64 .hw_value
= ZD_OFDM_RATE_18M
,
67 .hw_value
= ZD_OFDM_RATE_24M
,
70 .hw_value
= ZD_OFDM_RATE_36M
,
73 .hw_value
= ZD_OFDM_RATE_48M
,
76 .hw_value
= ZD_OFDM_RATE_54M
,
81 * Zydas retry rates table. Each line is listed in the same order as
82 * in zd_rates[] and contains all the rate used when a packet is sent
83 * starting with a given rates. Let's consider an example :
85 * "11 Mbits : 4, 3, 2, 1, 0" means :
86 * - packet is sent using 4 different rates
87 * - 1st rate is index 3 (ie 11 Mbits)
88 * - 2nd rate is index 2 (ie 5.5 Mbits)
89 * - 3rd rate is index 1 (ie 2 Mbits)
90 * - 4th rate is index 0 (ie 1 Mbits)
93 static const struct tx_retry_rate zd_retry_rates
[] = {
94 { /* 1 Mbits */ 1, { 0 }},
95 { /* 2 Mbits */ 2, { 1, 0 }},
96 { /* 5.5 Mbits */ 3, { 2, 1, 0 }},
97 { /* 11 Mbits */ 4, { 3, 2, 1, 0 }},
98 { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }},
99 { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}},
100 { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }},
101 { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }},
102 { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }},
103 { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }},
104 { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
105 { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
108 static const struct ieee80211_channel zd_channels
[] = {
109 { .center_freq
= 2412, .hw_value
= 1 },
110 { .center_freq
= 2417, .hw_value
= 2 },
111 { .center_freq
= 2422, .hw_value
= 3 },
112 { .center_freq
= 2427, .hw_value
= 4 },
113 { .center_freq
= 2432, .hw_value
= 5 },
114 { .center_freq
= 2437, .hw_value
= 6 },
115 { .center_freq
= 2442, .hw_value
= 7 },
116 { .center_freq
= 2447, .hw_value
= 8 },
117 { .center_freq
= 2452, .hw_value
= 9 },
118 { .center_freq
= 2457, .hw_value
= 10 },
119 { .center_freq
= 2462, .hw_value
= 11 },
120 { .center_freq
= 2467, .hw_value
= 12 },
121 { .center_freq
= 2472, .hw_value
= 13 },
122 { .center_freq
= 2484, .hw_value
= 14 },
125 static void housekeeping_init(struct zd_mac
*mac
);
126 static void housekeeping_enable(struct zd_mac
*mac
);
127 static void housekeeping_disable(struct zd_mac
*mac
);
128 static void beacon_init(struct zd_mac
*mac
);
129 static void beacon_enable(struct zd_mac
*mac
);
130 static void beacon_disable(struct zd_mac
*mac
);
131 static void set_rts_cts(struct zd_mac
*mac
, unsigned int short_preamble
);
132 static int zd_mac_config_beacon(struct ieee80211_hw
*hw
,
133 struct sk_buff
*beacon
, bool in_intr
);
135 static int zd_reg2alpha2(u8 regdomain
, char *alpha2
)
138 struct zd_reg_alpha2_map
*reg_map
;
139 for (i
= 0; i
< ARRAY_SIZE(reg_alpha2_map
); i
++) {
140 reg_map
= ®_alpha2_map
[i
];
141 if (regdomain
== reg_map
->reg
) {
142 alpha2
[0] = reg_map
->alpha2
[0];
143 alpha2
[1] = reg_map
->alpha2
[1];
150 static int zd_check_signal(struct ieee80211_hw
*hw
, int signal
)
152 struct zd_mac
*mac
= zd_hw_mac(hw
);
154 dev_dbg_f_cond(zd_mac_dev(mac
), signal
< 0 || signal
> 100,
155 "%s: signal value from device not in range 0..100, "
156 "but %d.\n", __func__
, signal
);
160 else if (signal
> 100)
166 int zd_mac_preinit_hw(struct ieee80211_hw
*hw
)
170 struct zd_mac
*mac
= zd_hw_mac(hw
);
172 r
= zd_chip_read_mac_addr_fw(&mac
->chip
, addr
);
176 SET_IEEE80211_PERM_ADDR(hw
, addr
);
181 int zd_mac_init_hw(struct ieee80211_hw
*hw
)
184 struct zd_mac
*mac
= zd_hw_mac(hw
);
185 struct zd_chip
*chip
= &mac
->chip
;
187 u8 default_regdomain
;
189 r
= zd_chip_enable_int(chip
);
192 r
= zd_chip_init_hw(chip
);
196 ZD_ASSERT(!irqs_disabled());
198 r
= zd_read_regdomain(chip
, &default_regdomain
);
201 spin_lock_irq(&mac
->lock
);
202 mac
->regdomain
= mac
->default_regdomain
= default_regdomain
;
203 spin_unlock_irq(&mac
->lock
);
205 /* We must inform the device that we are doing encryption/decryption in
206 * software at the moment. */
207 r
= zd_set_encryption_type(chip
, ENC_SNIFFER
);
211 r
= zd_reg2alpha2(mac
->regdomain
, alpha2
);
215 r
= regulatory_hint(hw
->wiphy
, alpha2
);
217 zd_chip_disable_int(chip
);
222 void zd_mac_clear(struct zd_mac
*mac
)
224 flush_workqueue(zd_workqueue
);
225 zd_chip_clear(&mac
->chip
);
226 ZD_MEMCLEAR(mac
, sizeof(struct zd_mac
));
229 static int set_rx_filter(struct zd_mac
*mac
)
232 u32 filter
= STA_RX_FILTER
;
234 spin_lock_irqsave(&mac
->lock
, flags
);
236 filter
|= RX_FILTER_CTRL
;
237 spin_unlock_irqrestore(&mac
->lock
, flags
);
239 return zd_iowrite32(&mac
->chip
, CR_RX_FILTER
, filter
);
242 static int set_mac_and_bssid(struct zd_mac
*mac
)
249 r
= zd_write_mac_addr(&mac
->chip
, mac
->vif
->addr
);
253 /* Vendor driver after setting MAC either sets BSSID for AP or
254 * filter for other modes.
256 if (mac
->type
!= NL80211_IFTYPE_AP
)
257 return set_rx_filter(mac
);
259 return zd_write_bssid(&mac
->chip
, mac
->vif
->addr
);
262 static int set_mc_hash(struct zd_mac
*mac
)
264 struct zd_mc_hash hash
;
266 return zd_chip_set_multicast_hash(&mac
->chip
, &hash
);
269 int zd_op_start(struct ieee80211_hw
*hw
)
271 struct zd_mac
*mac
= zd_hw_mac(hw
);
272 struct zd_chip
*chip
= &mac
->chip
;
273 struct zd_usb
*usb
= &chip
->usb
;
276 if (!usb
->initialized
) {
277 r
= zd_usb_init_hw(usb
);
282 r
= zd_chip_enable_int(chip
);
286 r
= zd_chip_set_basic_rates(chip
, CR_RATES_80211B
| CR_RATES_80211G
);
289 r
= set_rx_filter(mac
);
292 r
= set_mc_hash(mac
);
296 /* Wait after setting the multicast hash table and powering on
297 * the radio otherwise interface bring up will fail. This matches
298 * what the vendor driver did.
302 r
= zd_chip_switch_radio_on(chip
);
304 dev_err(zd_chip_dev(chip
),
305 "%s: failed to set radio on\n", __func__
);
308 r
= zd_chip_enable_rxtx(chip
);
311 r
= zd_chip_enable_hwint(chip
);
315 housekeeping_enable(mac
);
317 set_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
320 zd_chip_disable_rxtx(chip
);
322 zd_chip_switch_radio_off(chip
);
324 zd_chip_disable_int(chip
);
329 void zd_op_stop(struct ieee80211_hw
*hw
)
331 struct zd_mac
*mac
= zd_hw_mac(hw
);
332 struct zd_chip
*chip
= &mac
->chip
;
334 struct sk_buff_head
*ack_wait_queue
= &mac
->ack_wait_queue
;
336 clear_bit(ZD_DEVICE_RUNNING
, &mac
->flags
);
338 /* The order here deliberately is a little different from the open()
339 * method, since we need to make sure there is no opportunity for RX
340 * frames to be processed by mac80211 after we have stopped it.
343 zd_chip_disable_rxtx(chip
);
345 housekeeping_disable(mac
);
346 flush_workqueue(zd_workqueue
);
348 zd_chip_disable_hwint(chip
);
349 zd_chip_switch_radio_off(chip
);
350 zd_chip_disable_int(chip
);
353 while ((skb
= skb_dequeue(ack_wait_queue
)))
354 dev_kfree_skb_any(skb
);
357 int zd_restore_settings(struct zd_mac
*mac
)
359 struct sk_buff
*beacon
;
360 struct zd_mc_hash multicast_hash
;
361 unsigned int short_preamble
;
362 int r
, beacon_interval
, beacon_period
;
365 dev_dbg_f(zd_mac_dev(mac
), "\n");
367 spin_lock_irq(&mac
->lock
);
368 multicast_hash
= mac
->multicast_hash
;
369 short_preamble
= mac
->short_preamble
;
370 beacon_interval
= mac
->beacon
.interval
;
371 beacon_period
= mac
->beacon
.period
;
372 channel
= mac
->channel
;
373 spin_unlock_irq(&mac
->lock
);
375 r
= set_mac_and_bssid(mac
);
377 dev_dbg_f(zd_mac_dev(mac
), "set_mac_and_bssid failed, %d\n", r
);
381 r
= zd_chip_set_channel(&mac
->chip
, channel
);
383 dev_dbg_f(zd_mac_dev(mac
), "zd_chip_set_channel failed, %d\n",
388 set_rts_cts(mac
, short_preamble
);
390 r
= zd_chip_set_multicast_hash(&mac
->chip
, &multicast_hash
);
392 dev_dbg_f(zd_mac_dev(mac
),
393 "zd_chip_set_multicast_hash failed, %d\n", r
);
397 if (mac
->type
== NL80211_IFTYPE_MESH_POINT
||
398 mac
->type
== NL80211_IFTYPE_ADHOC
||
399 mac
->type
== NL80211_IFTYPE_AP
) {
400 if (mac
->vif
!= NULL
) {
401 beacon
= ieee80211_beacon_get(mac
->hw
, mac
->vif
);
403 zd_mac_config_beacon(mac
->hw
, beacon
, false);
406 zd_set_beacon_interval(&mac
->chip
, beacon_interval
,
407 beacon_period
, mac
->type
);
409 spin_lock_irq(&mac
->lock
);
410 mac
->beacon
.last_update
= jiffies
;
411 spin_unlock_irq(&mac
->lock
);
418 * zd_mac_tx_status - reports tx status of a packet if required
419 * @hw - a &struct ieee80211_hw pointer
421 * @flags: extra flags to set in the TX status info
422 * @ackssi: ACK signal strength
423 * @success - True for successful transmission of the frame
425 * This information calls ieee80211_tx_status_irqsafe() if required by the
426 * control information. It copies the control information into the status
429 * If no status information has been requested, the skb is freed.
431 static void zd_mac_tx_status(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
432 int ackssi
, struct tx_status
*tx_status
)
434 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
436 int success
= 1, retry
= 1;
438 const struct tx_retry_rate
*retries
;
440 ieee80211_tx_info_clear_status(info
);
443 success
= !tx_status
->failure
;
444 retry
= tx_status
->retry
+ success
;
449 info
->flags
|= IEEE80211_TX_STAT_ACK
;
452 info
->flags
&= ~IEEE80211_TX_STAT_ACK
;
455 first_idx
= info
->status
.rates
[0].idx
;
456 ZD_ASSERT(0<=first_idx
&& first_idx
<ARRAY_SIZE(zd_retry_rates
));
457 retries
= &zd_retry_rates
[first_idx
];
458 ZD_ASSERT(1 <= retry
&& retry
<= retries
->count
);
460 info
->status
.rates
[0].idx
= retries
->rate
[0];
461 info
->status
.rates
[0].count
= 1; // (retry > 1 ? 2 : 1);
463 for (i
=1; i
<IEEE80211_TX_MAX_RATES
-1 && i
<retry
; i
++) {
464 info
->status
.rates
[i
].idx
= retries
->rate
[i
];
465 info
->status
.rates
[i
].count
= 1; // ((i==retry-1) && success ? 1:2);
467 for (; i
<IEEE80211_TX_MAX_RATES
&& i
<retry
; i
++) {
468 info
->status
.rates
[i
].idx
= retries
->rate
[retry
- 1];
469 info
->status
.rates
[i
].count
= 1; // (success ? 1:2);
471 if (i
<IEEE80211_TX_MAX_RATES
)
472 info
->status
.rates
[i
].idx
= -1; /* terminate */
474 info
->status
.ack_signal
= zd_check_signal(hw
, ackssi
);
475 ieee80211_tx_status_irqsafe(hw
, skb
);
479 * zd_mac_tx_failed - callback for failed frames
480 * @dev: the mac80211 wireless device
482 * This function is called if a frame couldn't be successfully
483 * transferred. The first frame from the tx queue, will be selected and
484 * reported as error to the upper layers.
486 void zd_mac_tx_failed(struct urb
*urb
)
488 struct ieee80211_hw
* hw
= zd_usb_to_hw(urb
->context
);
489 struct zd_mac
*mac
= zd_hw_mac(hw
);
490 struct sk_buff_head
*q
= &mac
->ack_wait_queue
;
492 struct tx_status
*tx_status
= (struct tx_status
*)urb
->transfer_buffer
;
494 int success
= !tx_status
->failure
;
495 int retry
= tx_status
->retry
+ success
;
499 spin_lock_irqsave(&q
->lock
, flags
);
501 skb_queue_walk(q
, skb
) {
502 struct ieee80211_hdr
*tx_hdr
;
503 struct ieee80211_tx_info
*info
;
504 int first_idx
, final_idx
;
505 const struct tx_retry_rate
*retries
;
510 /* if the hardware reports a failure and we had a 802.11 ACK
511 * pending, then we skip the first skb when searching for a
513 if (tx_status
->failure
&& mac
->ack_pending
&&
514 skb_queue_is_first(q
, skb
)) {
518 tx_hdr
= (struct ieee80211_hdr
*)skb
->data
;
520 /* we skip all frames not matching the reported destination */
521 if (unlikely(!ether_addr_equal(tx_hdr
->addr1
, tx_status
->mac
)))
524 /* we skip all frames not matching the reported final rate */
526 info
= IEEE80211_SKB_CB(skb
);
527 first_idx
= info
->status
.rates
[0].idx
;
528 ZD_ASSERT(0<=first_idx
&& first_idx
<ARRAY_SIZE(zd_retry_rates
));
529 retries
= &zd_retry_rates
[first_idx
];
530 if (retry
<= 0 || retry
> retries
->count
)
533 final_idx
= retries
->rate
[retry
- 1];
534 final_rate
= zd_rates
[final_idx
].hw_value
;
536 if (final_rate
!= tx_status
->rate
) {
545 for (i
=1; i
<=position
; i
++) {
546 skb
= __skb_dequeue(q
);
547 zd_mac_tx_status(hw
, skb
,
548 mac
->ack_pending
? mac
->ack_signal
: 0,
549 i
== position
? tx_status
: NULL
);
550 mac
->ack_pending
= 0;
554 spin_unlock_irqrestore(&q
->lock
, flags
);
558 * zd_mac_tx_to_dev - callback for USB layer
559 * @skb: a &sk_buff pointer
560 * @error: error value, 0 if transmission successful
562 * Informs the MAC layer that the frame has successfully transferred to the
563 * device. If an ACK is required and the transfer to the device has been
564 * successful, the packets are put on the @ack_wait_queue with
565 * the control set removed.
567 void zd_mac_tx_to_dev(struct sk_buff
*skb
, int error
)
569 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
570 struct ieee80211_hw
*hw
= info
->rate_driver_data
[0];
571 struct zd_mac
*mac
= zd_hw_mac(hw
);
573 ieee80211_tx_info_clear_status(info
);
575 skb_pull(skb
, sizeof(struct zd_ctrlset
));
576 if (unlikely(error
||
577 (info
->flags
& IEEE80211_TX_CTL_NO_ACK
))) {
579 * FIXME : do we need to fill in anything ?
581 ieee80211_tx_status_irqsafe(hw
, skb
);
583 struct sk_buff_head
*q
= &mac
->ack_wait_queue
;
585 skb_queue_tail(q
, skb
);
586 while (skb_queue_len(q
) > ZD_MAC_MAX_ACK_WAITERS
) {
587 zd_mac_tx_status(hw
, skb_dequeue(q
),
588 mac
->ack_pending
? mac
->ack_signal
: 0,
590 mac
->ack_pending
= 0;
595 static int zd_calc_tx_length_us(u8
*service
, u8 zd_rate
, u16 tx_length
)
597 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
598 * the zd-rate values.
600 static const u8 rate_divisor
[] = {
601 [ZD_PURE_RATE(ZD_CCK_RATE_1M
)] = 1,
602 [ZD_PURE_RATE(ZD_CCK_RATE_2M
)] = 2,
603 /* Bits must be doubled. */
604 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M
)] = 11,
605 [ZD_PURE_RATE(ZD_CCK_RATE_11M
)] = 11,
606 [ZD_PURE_RATE(ZD_OFDM_RATE_6M
)] = 6,
607 [ZD_PURE_RATE(ZD_OFDM_RATE_9M
)] = 9,
608 [ZD_PURE_RATE(ZD_OFDM_RATE_12M
)] = 12,
609 [ZD_PURE_RATE(ZD_OFDM_RATE_18M
)] = 18,
610 [ZD_PURE_RATE(ZD_OFDM_RATE_24M
)] = 24,
611 [ZD_PURE_RATE(ZD_OFDM_RATE_36M
)] = 36,
612 [ZD_PURE_RATE(ZD_OFDM_RATE_48M
)] = 48,
613 [ZD_PURE_RATE(ZD_OFDM_RATE_54M
)] = 54,
616 u32 bits
= (u32
)tx_length
* 8;
619 divisor
= rate_divisor
[ZD_PURE_RATE(zd_rate
)];
624 case ZD_CCK_RATE_5_5M
:
625 bits
= (2*bits
) + 10; /* round up to the next integer */
627 case ZD_CCK_RATE_11M
:
630 *service
&= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION
;
631 if (0 < t
&& t
<= 3) {
632 *service
|= ZD_PLCP_SERVICE_LENGTH_EXTENSION
;
635 bits
+= 10; /* round up to the next integer */
642 static void cs_set_control(struct zd_mac
*mac
, struct zd_ctrlset
*cs
,
643 struct ieee80211_hdr
*header
,
644 struct ieee80211_tx_info
*info
)
648 * - if backoff needed, enable bit 0
649 * - if burst (backoff not needed) disable bit 0
655 if (info
->flags
& IEEE80211_TX_CTL_FIRST_FRAGMENT
)
656 cs
->control
|= ZD_CS_NEED_RANDOM_BACKOFF
;
658 /* No ACK expected (multicast, etc.) */
659 if (info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
660 cs
->control
|= ZD_CS_NO_ACK
;
663 if (ieee80211_is_pspoll(header
->frame_control
))
664 cs
->control
|= ZD_CS_PS_POLL_FRAME
;
666 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_USE_RTS_CTS
)
667 cs
->control
|= ZD_CS_RTS
;
669 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
)
670 cs
->control
|= ZD_CS_SELF_CTS
;
672 /* FIXME: Management frame? */
675 static bool zd_mac_match_cur_beacon(struct zd_mac
*mac
, struct sk_buff
*beacon
)
677 if (!mac
->beacon
.cur_beacon
)
680 if (mac
->beacon
.cur_beacon
->len
!= beacon
->len
)
683 return !memcmp(beacon
->data
, mac
->beacon
.cur_beacon
->data
, beacon
->len
);
686 static void zd_mac_free_cur_beacon_locked(struct zd_mac
*mac
)
688 ZD_ASSERT(mutex_is_locked(&mac
->chip
.mutex
));
690 kfree_skb(mac
->beacon
.cur_beacon
);
691 mac
->beacon
.cur_beacon
= NULL
;
694 static void zd_mac_free_cur_beacon(struct zd_mac
*mac
)
696 mutex_lock(&mac
->chip
.mutex
);
697 zd_mac_free_cur_beacon_locked(mac
);
698 mutex_unlock(&mac
->chip
.mutex
);
701 static int zd_mac_config_beacon(struct ieee80211_hw
*hw
, struct sk_buff
*beacon
,
704 struct zd_mac
*mac
= zd_hw_mac(hw
);
705 int r
, ret
, num_cmds
, req_pos
= 0;
707 /* 4 more bytes for tail CRC */
708 u32 full_len
= beacon
->len
+ 4;
709 unsigned long end_jiffies
, message_jiffies
;
710 struct zd_ioreq32
*ioreqs
;
712 mutex_lock(&mac
->chip
.mutex
);
714 /* Check if hw already has this beacon. */
715 if (zd_mac_match_cur_beacon(mac
, beacon
)) {
720 /* Alloc memory for full beacon write at once. */
721 num_cmds
= 1 + zd_chip_is_zd1211b(&mac
->chip
) + full_len
;
722 ioreqs
= kmalloc_array(num_cmds
, sizeof(struct zd_ioreq32
),
729 r
= zd_iowrite32_locked(&mac
->chip
, 0, CR_BCN_FIFO_SEMAPHORE
);
732 r
= zd_ioread32_locked(&mac
->chip
, &tmp
, CR_BCN_FIFO_SEMAPHORE
);
735 if (in_intr
&& tmp
& 0x2) {
740 end_jiffies
= jiffies
+ HZ
/ 2; /*~500ms*/
741 message_jiffies
= jiffies
+ HZ
/ 10; /*~100ms*/
743 r
= zd_ioread32_locked(&mac
->chip
, &tmp
, CR_BCN_FIFO_SEMAPHORE
);
746 if (time_is_before_eq_jiffies(message_jiffies
)) {
747 message_jiffies
= jiffies
+ HZ
/ 10;
748 dev_err(zd_mac_dev(mac
),
749 "CR_BCN_FIFO_SEMAPHORE not ready\n");
750 if (time_is_before_eq_jiffies(end_jiffies
)) {
751 dev_err(zd_mac_dev(mac
),
752 "Giving up beacon config.\n");
760 ioreqs
[req_pos
].addr
= CR_BCN_FIFO
;
761 ioreqs
[req_pos
].value
= full_len
- 1;
763 if (zd_chip_is_zd1211b(&mac
->chip
)) {
764 ioreqs
[req_pos
].addr
= CR_BCN_LENGTH
;
765 ioreqs
[req_pos
].value
= full_len
- 1;
769 for (j
= 0 ; j
< beacon
->len
; j
++) {
770 ioreqs
[req_pos
].addr
= CR_BCN_FIFO
;
771 ioreqs
[req_pos
].value
= *((u8
*)(beacon
->data
+ j
));
775 for (j
= 0; j
< 4; j
++) {
776 ioreqs
[req_pos
].addr
= CR_BCN_FIFO
;
777 ioreqs
[req_pos
].value
= 0x0;
781 BUG_ON(req_pos
!= num_cmds
);
783 r
= zd_iowrite32a_locked(&mac
->chip
, ioreqs
, num_cmds
);
787 * Try very hard to release device beacon semaphore, as otherwise
788 * device/driver can be left in unusable state.
790 end_jiffies
= jiffies
+ HZ
/ 2; /*~500ms*/
791 ret
= zd_iowrite32_locked(&mac
->chip
, 1, CR_BCN_FIFO_SEMAPHORE
);
793 if (in_intr
|| time_is_before_eq_jiffies(end_jiffies
)) {
799 ret
= zd_iowrite32_locked(&mac
->chip
, 1, CR_BCN_FIFO_SEMAPHORE
);
803 dev_err(zd_mac_dev(mac
), "Could not release "
804 "CR_BCN_FIFO_SEMAPHORE!\n");
805 if (r
< 0 || ret
< 0) {
809 /* We don't know if beacon was written successfully or not,
810 * so clear current. */
811 zd_mac_free_cur_beacon_locked(mac
);
816 /* Beacon has now been written successfully, update current. */
817 zd_mac_free_cur_beacon_locked(mac
);
818 mac
->beacon
.cur_beacon
= beacon
;
821 /* 802.11b/g 2.4G CCK 1Mb
822 * 802.11a, not yet implemented, uses different values (see GPL vendor
825 r
= zd_iowrite32_locked(&mac
->chip
, 0x00000400 | (full_len
<< 19),
831 mutex_unlock(&mac
->chip
.mutex
);
836 zd_mac_free_cur_beacon_locked(mac
);
839 mutex_unlock(&mac
->chip
.mutex
);
842 /* semaphore stuck, reset device to avoid fw freeze later */
843 dev_warn(zd_mac_dev(mac
), "CR_BCN_FIFO_SEMAPHORE stuck, "
844 "resetting device...");
845 usb_queue_reset_device(mac
->chip
.usb
.intf
);
850 static int fill_ctrlset(struct zd_mac
*mac
,
854 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
855 unsigned int frag_len
= skb
->len
+ FCS_LEN
;
856 unsigned int packet_length
;
857 struct ieee80211_rate
*txrate
;
858 struct zd_ctrlset
*cs
= skb_push(skb
, sizeof(struct zd_ctrlset
));
859 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
861 ZD_ASSERT(frag_len
<= 0xffff);
864 * Firmware computes the duration itself (for all frames except PSPoll)
865 * and needs the field set to 0 at input, otherwise firmware messes up
866 * duration_id and sets bits 14 and 15 on.
868 if (!ieee80211_is_pspoll(hdr
->frame_control
))
869 hdr
->duration_id
= 0;
871 txrate
= ieee80211_get_tx_rate(mac
->hw
, info
);
873 cs
->modulation
= txrate
->hw_value
;
874 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_USE_SHORT_PREAMBLE
)
875 cs
->modulation
= txrate
->hw_value_short
;
877 cs
->tx_length
= cpu_to_le16(frag_len
);
879 cs_set_control(mac
, cs
, hdr
, info
);
881 packet_length
= frag_len
+ sizeof(struct zd_ctrlset
) + 10;
882 ZD_ASSERT(packet_length
<= 0xffff);
883 /* ZD1211B: Computing the length difference this way, gives us
884 * flexibility to compute the packet length.
886 cs
->packet_length
= cpu_to_le16(zd_chip_is_zd1211b(&mac
->chip
) ?
887 packet_length
- frag_len
: packet_length
);
891 * - transmit frame length in microseconds
892 * - seems to be derived from frame length
893 * - see Cal_Us_Service() in zdinlinef.h
894 * - if macp->bTxBurstEnable is enabled, then multiply by 4
895 * - bTxBurstEnable is never set in the vendor driver
898 * - "for PLCP configuration"
899 * - always 0 except in some situations at 802.11b 11M
900 * - see line 53 of zdinlinef.h
903 r
= zd_calc_tx_length_us(&cs
->service
, ZD_RATE(cs
->modulation
),
904 le16_to_cpu(cs
->tx_length
));
907 cs
->current_length
= cpu_to_le16(r
);
908 cs
->next_frame_length
= 0;
914 * zd_op_tx - transmits a network frame to the device
916 * @dev: mac80211 hardware device
917 * @skb: socket buffer
918 * @control: the control structure
920 * This function transmit an IEEE 802.11 network frame to the device. The
921 * control block of the skbuff will be initialized. If necessary the incoming
922 * mac80211 queues will be stopped.
924 static void zd_op_tx(struct ieee80211_hw
*hw
,
925 struct ieee80211_tx_control
*control
,
928 struct zd_mac
*mac
= zd_hw_mac(hw
);
929 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
932 r
= fill_ctrlset(mac
, skb
);
936 info
->rate_driver_data
[0] = hw
;
938 r
= zd_usb_tx(&mac
->chip
.usb
, skb
);
948 * filter_ack - filters incoming packets for acknowledgements
949 * @dev: the mac80211 device
950 * @rx_hdr: received header
951 * @stats: the status for the received packet
953 * This functions looks for ACK packets and tries to match them with the
954 * frames in the tx queue. If a match is found the frame will be dequeued and
955 * the upper layers is informed about the successful transmission. If
956 * mac80211 queues have been stopped and the number of frames still to be
957 * transmitted is low the queues will be opened again.
959 * Returns 1 if the frame was an ACK, 0 if it was ignored.
961 static int filter_ack(struct ieee80211_hw
*hw
, struct ieee80211_hdr
*rx_hdr
,
962 struct ieee80211_rx_status
*stats
)
964 struct zd_mac
*mac
= zd_hw_mac(hw
);
966 struct sk_buff_head
*q
;
971 if (!ieee80211_is_ack(rx_hdr
->frame_control
))
974 q
= &mac
->ack_wait_queue
;
975 spin_lock_irqsave(&q
->lock
, flags
);
976 skb_queue_walk(q
, skb
) {
977 struct ieee80211_hdr
*tx_hdr
;
981 if (mac
->ack_pending
&& skb_queue_is_first(q
, skb
))
984 tx_hdr
= (struct ieee80211_hdr
*)skb
->data
;
985 if (likely(ether_addr_equal(tx_hdr
->addr2
, rx_hdr
->addr1
)))
993 for (i
=1; i
<position
; i
++) {
994 skb
= __skb_dequeue(q
);
995 zd_mac_tx_status(hw
, skb
,
996 mac
->ack_pending
? mac
->ack_signal
: 0,
998 mac
->ack_pending
= 0;
1001 mac
->ack_pending
= 1;
1002 mac
->ack_signal
= stats
->signal
;
1004 /* Prevent pending tx-packet on AP-mode */
1005 if (mac
->type
== NL80211_IFTYPE_AP
) {
1006 skb
= __skb_dequeue(q
);
1007 zd_mac_tx_status(hw
, skb
, mac
->ack_signal
, NULL
);
1008 mac
->ack_pending
= 0;
1012 spin_unlock_irqrestore(&q
->lock
, flags
);
1016 int zd_mac_rx(struct ieee80211_hw
*hw
, const u8
*buffer
, unsigned int length
)
1018 struct zd_mac
*mac
= zd_hw_mac(hw
);
1019 struct ieee80211_rx_status stats
;
1020 const struct rx_status
*status
;
1021 struct sk_buff
*skb
;
1028 if (length
< ZD_PLCP_HEADER_SIZE
+ 10 /* IEEE80211_1ADDR_LEN */ +
1029 FCS_LEN
+ sizeof(struct rx_status
))
1032 memset(&stats
, 0, sizeof(stats
));
1034 /* Note about pass_failed_fcs and pass_ctrl access below:
1035 * mac locking intentionally omitted here, as this is the only unlocked
1036 * reader and the only writer is configure_filter. Plus, if there were
1037 * any races accessing these variables, it wouldn't really matter.
1038 * If mac80211 ever provides a way for us to access filter flags
1039 * from outside configure_filter, we could improve on this. Also, this
1040 * situation may change once we implement some kind of DMA-into-skb
1043 /* Caller has to ensure that length >= sizeof(struct rx_status). */
1044 status
= (struct rx_status
*)
1045 (buffer
+ (length
- sizeof(struct rx_status
)));
1046 if (status
->frame_status
& ZD_RX_ERROR
) {
1047 if (mac
->pass_failed_fcs
&&
1048 (status
->frame_status
& ZD_RX_CRC32_ERROR
)) {
1049 stats
.flag
|= RX_FLAG_FAILED_FCS_CRC
;
1056 stats
.freq
= zd_channels
[_zd_chip_get_channel(&mac
->chip
) - 1].center_freq
;
1057 stats
.band
= NL80211_BAND_2GHZ
;
1058 stats
.signal
= zd_check_signal(hw
, status
->signal_strength
);
1060 rate
= zd_rx_rate(buffer
, status
);
1062 /* todo: return index in the big switches in zd_rx_rate instead */
1063 for (i
= 0; i
< mac
->band
.n_bitrates
; i
++)
1064 if (rate
== mac
->band
.bitrates
[i
].hw_value
)
1067 length
-= ZD_PLCP_HEADER_SIZE
+ sizeof(struct rx_status
);
1068 buffer
+= ZD_PLCP_HEADER_SIZE
;
1070 /* Except for bad frames, filter each frame to see if it is an ACK, in
1071 * which case our internal TX tracking is updated. Normally we then
1072 * bail here as there's no need to pass ACKs on up to the stack, but
1073 * there is also the case where the stack has requested us to pass
1074 * control frames on up (pass_ctrl) which we must consider. */
1076 filter_ack(hw
, (struct ieee80211_hdr
*)buffer
, &stats
)
1080 fc
= get_unaligned((__le16
*)buffer
);
1081 need_padding
= ieee80211_is_data_qos(fc
) ^ ieee80211_has_a4(fc
);
1083 skb
= dev_alloc_skb(length
+ (need_padding
? 2 : 0));
1087 /* Make sure the payload data is 4 byte aligned. */
1088 skb_reserve(skb
, 2);
1091 /* FIXME : could we avoid this big memcpy ? */
1092 skb_put_data(skb
, buffer
, length
);
1094 memcpy(IEEE80211_SKB_RXCB(skb
), &stats
, sizeof(stats
));
1095 ieee80211_rx_irqsafe(hw
, skb
);
1099 static int zd_op_add_interface(struct ieee80211_hw
*hw
,
1100 struct ieee80211_vif
*vif
)
1102 struct zd_mac
*mac
= zd_hw_mac(hw
);
1104 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
1105 if (mac
->type
!= NL80211_IFTYPE_UNSPECIFIED
)
1108 switch (vif
->type
) {
1109 case NL80211_IFTYPE_MONITOR
:
1110 case NL80211_IFTYPE_MESH_POINT
:
1111 case NL80211_IFTYPE_STATION
:
1112 case NL80211_IFTYPE_ADHOC
:
1113 case NL80211_IFTYPE_AP
:
1114 mac
->type
= vif
->type
;
1122 return set_mac_and_bssid(mac
);
1125 static void zd_op_remove_interface(struct ieee80211_hw
*hw
,
1126 struct ieee80211_vif
*vif
)
1128 struct zd_mac
*mac
= zd_hw_mac(hw
);
1129 mac
->type
= NL80211_IFTYPE_UNSPECIFIED
;
1131 zd_set_beacon_interval(&mac
->chip
, 0, 0, NL80211_IFTYPE_UNSPECIFIED
);
1132 zd_write_mac_addr(&mac
->chip
, NULL
);
1134 zd_mac_free_cur_beacon(mac
);
1137 static int zd_op_config(struct ieee80211_hw
*hw
, u32 changed
)
1139 struct zd_mac
*mac
= zd_hw_mac(hw
);
1140 struct ieee80211_conf
*conf
= &hw
->conf
;
1142 spin_lock_irq(&mac
->lock
);
1143 mac
->channel
= conf
->chandef
.chan
->hw_value
;
1144 spin_unlock_irq(&mac
->lock
);
1146 return zd_chip_set_channel(&mac
->chip
, conf
->chandef
.chan
->hw_value
);
1149 static void zd_beacon_done(struct zd_mac
*mac
)
1151 struct sk_buff
*skb
, *beacon
;
1153 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1155 if (!mac
->vif
|| mac
->vif
->type
!= NL80211_IFTYPE_AP
)
1159 * Send out buffered broad- and multicast frames.
1161 while (!ieee80211_queue_stopped(mac
->hw
, 0)) {
1162 skb
= ieee80211_get_buffered_bc(mac
->hw
, mac
->vif
);
1165 zd_op_tx(mac
->hw
, NULL
, skb
);
1169 * Fetch next beacon so that tim_count is updated.
1171 beacon
= ieee80211_beacon_get(mac
->hw
, mac
->vif
);
1173 zd_mac_config_beacon(mac
->hw
, beacon
, true);
1175 spin_lock_irq(&mac
->lock
);
1176 mac
->beacon
.last_update
= jiffies
;
1177 spin_unlock_irq(&mac
->lock
);
1180 static void zd_process_intr(struct work_struct
*work
)
1183 unsigned long flags
;
1184 struct zd_mac
*mac
= container_of(work
, struct zd_mac
, process_intr
);
1186 spin_lock_irqsave(&mac
->lock
, flags
);
1187 int_status
= le16_to_cpu(*(__le16
*)(mac
->intr_buffer
+ 4));
1188 spin_unlock_irqrestore(&mac
->lock
, flags
);
1190 if (int_status
& INT_CFG_NEXT_BCN
) {
1191 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1192 zd_beacon_done(mac
);
1194 dev_dbg_f(zd_mac_dev(mac
), "Unsupported interrupt\n");
1197 zd_chip_enable_hwint(&mac
->chip
);
1201 static u64
zd_op_prepare_multicast(struct ieee80211_hw
*hw
,
1202 struct netdev_hw_addr_list
*mc_list
)
1204 struct zd_mac
*mac
= zd_hw_mac(hw
);
1205 struct zd_mc_hash hash
;
1206 struct netdev_hw_addr
*ha
;
1210 netdev_hw_addr_list_for_each(ha
, mc_list
) {
1211 dev_dbg_f(zd_mac_dev(mac
), "mc addr %pM\n", ha
->addr
);
1212 zd_mc_add_addr(&hash
, ha
->addr
);
1215 return hash
.low
| ((u64
)hash
.high
<< 32);
1218 #define SUPPORTED_FIF_FLAGS \
1219 (FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1220 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1221 static void zd_op_configure_filter(struct ieee80211_hw
*hw
,
1222 unsigned int changed_flags
,
1223 unsigned int *new_flags
,
1226 struct zd_mc_hash hash
= {
1228 .high
= multicast
>> 32,
1230 struct zd_mac
*mac
= zd_hw_mac(hw
);
1231 unsigned long flags
;
1234 /* Only deal with supported flags */
1235 changed_flags
&= SUPPORTED_FIF_FLAGS
;
1236 *new_flags
&= SUPPORTED_FIF_FLAGS
;
1239 * If multicast parameter (as returned by zd_op_prepare_multicast)
1240 * has changed, no bit in changed_flags is set. To handle this
1241 * situation, we do not return if changed_flags is 0. If we do so,
1242 * we will have some issue with IPv6 which uses multicast for link
1243 * layer address resolution.
1245 if (*new_flags
& FIF_ALLMULTI
)
1246 zd_mc_add_all(&hash
);
1248 spin_lock_irqsave(&mac
->lock
, flags
);
1249 mac
->pass_failed_fcs
= !!(*new_flags
& FIF_FCSFAIL
);
1250 mac
->pass_ctrl
= !!(*new_flags
& FIF_CONTROL
);
1251 mac
->multicast_hash
= hash
;
1252 spin_unlock_irqrestore(&mac
->lock
, flags
);
1254 zd_chip_set_multicast_hash(&mac
->chip
, &hash
);
1256 if (changed_flags
& FIF_CONTROL
) {
1257 r
= set_rx_filter(mac
);
1259 dev_err(zd_mac_dev(mac
), "set_rx_filter error %d\n", r
);
1262 /* no handling required for FIF_OTHER_BSS as we don't currently
1263 * do BSSID filtering */
1264 /* FIXME: in future it would be nice to enable the probe response
1265 * filter (so that the driver doesn't see them) until
1266 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1267 * have to schedule work to enable prbresp reception, which might
1268 * happen too late. For now we'll just listen and forward them all the
1272 static void set_rts_cts(struct zd_mac
*mac
, unsigned int short_preamble
)
1274 mutex_lock(&mac
->chip
.mutex
);
1275 zd_chip_set_rts_cts_rate_locked(&mac
->chip
, short_preamble
);
1276 mutex_unlock(&mac
->chip
.mutex
);
1279 static void zd_op_bss_info_changed(struct ieee80211_hw
*hw
,
1280 struct ieee80211_vif
*vif
,
1281 struct ieee80211_bss_conf
*bss_conf
,
1284 struct zd_mac
*mac
= zd_hw_mac(hw
);
1287 dev_dbg_f(zd_mac_dev(mac
), "changes: %x\n", changes
);
1289 if (mac
->type
== NL80211_IFTYPE_MESH_POINT
||
1290 mac
->type
== NL80211_IFTYPE_ADHOC
||
1291 mac
->type
== NL80211_IFTYPE_AP
) {
1293 if (changes
& BSS_CHANGED_BEACON
) {
1294 struct sk_buff
*beacon
= ieee80211_beacon_get(hw
, vif
);
1297 zd_chip_disable_hwint(&mac
->chip
);
1298 zd_mac_config_beacon(hw
, beacon
, false);
1299 zd_chip_enable_hwint(&mac
->chip
);
1303 if (changes
& BSS_CHANGED_BEACON_ENABLED
) {
1307 if (bss_conf
->enable_beacon
) {
1308 period
= bss_conf
->dtim_period
;
1309 interval
= bss_conf
->beacon_int
;
1312 spin_lock_irq(&mac
->lock
);
1313 mac
->beacon
.period
= period
;
1314 mac
->beacon
.interval
= interval
;
1315 mac
->beacon
.last_update
= jiffies
;
1316 spin_unlock_irq(&mac
->lock
);
1318 zd_set_beacon_interval(&mac
->chip
, interval
, period
,
1322 associated
= is_valid_ether_addr(bss_conf
->bssid
);
1324 spin_lock_irq(&mac
->lock
);
1325 mac
->associated
= associated
;
1326 spin_unlock_irq(&mac
->lock
);
1328 /* TODO: do hardware bssid filtering */
1330 if (changes
& BSS_CHANGED_ERP_PREAMBLE
) {
1331 spin_lock_irq(&mac
->lock
);
1332 mac
->short_preamble
= bss_conf
->use_short_preamble
;
1333 spin_unlock_irq(&mac
->lock
);
1335 set_rts_cts(mac
, bss_conf
->use_short_preamble
);
1339 static u64
zd_op_get_tsf(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
)
1341 struct zd_mac
*mac
= zd_hw_mac(hw
);
1342 return zd_chip_get_tsf(&mac
->chip
);
1345 static const struct ieee80211_ops zd_ops
= {
1347 .start
= zd_op_start
,
1349 .add_interface
= zd_op_add_interface
,
1350 .remove_interface
= zd_op_remove_interface
,
1351 .config
= zd_op_config
,
1352 .prepare_multicast
= zd_op_prepare_multicast
,
1353 .configure_filter
= zd_op_configure_filter
,
1354 .bss_info_changed
= zd_op_bss_info_changed
,
1355 .get_tsf
= zd_op_get_tsf
,
1358 struct ieee80211_hw
*zd_mac_alloc_hw(struct usb_interface
*intf
)
1361 struct ieee80211_hw
*hw
;
1363 hw
= ieee80211_alloc_hw(sizeof(struct zd_mac
), &zd_ops
);
1365 dev_dbg_f(&intf
->dev
, "out of memory\n");
1369 mac
= zd_hw_mac(hw
);
1371 memset(mac
, 0, sizeof(*mac
));
1372 spin_lock_init(&mac
->lock
);
1375 mac
->type
= NL80211_IFTYPE_UNSPECIFIED
;
1377 memcpy(mac
->channels
, zd_channels
, sizeof(zd_channels
));
1378 memcpy(mac
->rates
, zd_rates
, sizeof(zd_rates
));
1379 mac
->band
.n_bitrates
= ARRAY_SIZE(zd_rates
);
1380 mac
->band
.bitrates
= mac
->rates
;
1381 mac
->band
.n_channels
= ARRAY_SIZE(zd_channels
);
1382 mac
->band
.channels
= mac
->channels
;
1384 hw
->wiphy
->bands
[NL80211_BAND_2GHZ
] = &mac
->band
;
1386 ieee80211_hw_set(hw
, MFP_CAPABLE
);
1387 ieee80211_hw_set(hw
, HOST_BROADCAST_PS_BUFFERING
);
1388 ieee80211_hw_set(hw
, RX_INCLUDES_FCS
);
1389 ieee80211_hw_set(hw
, SIGNAL_UNSPEC
);
1391 hw
->wiphy
->interface_modes
=
1392 BIT(NL80211_IFTYPE_MESH_POINT
) |
1393 BIT(NL80211_IFTYPE_STATION
) |
1394 BIT(NL80211_IFTYPE_ADHOC
) |
1395 BIT(NL80211_IFTYPE_AP
);
1397 wiphy_ext_feature_set(hw
->wiphy
, NL80211_EXT_FEATURE_CQM_RSSI_LIST
);
1399 hw
->max_signal
= 100;
1401 hw
->extra_tx_headroom
= sizeof(struct zd_ctrlset
);
1404 * Tell mac80211 that we support multi rate retries
1406 hw
->max_rates
= IEEE80211_TX_MAX_RATES
;
1407 hw
->max_rate_tries
= 18; /* 9 rates * 2 retries/rate */
1409 skb_queue_head_init(&mac
->ack_wait_queue
);
1410 mac
->ack_pending
= 0;
1412 zd_chip_init(&mac
->chip
, hw
, intf
);
1413 housekeeping_init(mac
);
1415 INIT_WORK(&mac
->process_intr
, zd_process_intr
);
1417 SET_IEEE80211_DEV(hw
, &intf
->dev
);
1421 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1423 static void beacon_watchdog_handler(struct work_struct
*work
)
1425 struct zd_mac
*mac
=
1426 container_of(work
, struct zd_mac
, beacon
.watchdog_work
.work
);
1427 struct sk_buff
*beacon
;
1428 unsigned long timeout
;
1429 int interval
, period
;
1431 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1433 if (mac
->type
!= NL80211_IFTYPE_AP
|| !mac
->vif
)
1436 spin_lock_irq(&mac
->lock
);
1437 interval
= mac
->beacon
.interval
;
1438 period
= mac
->beacon
.period
;
1439 timeout
= mac
->beacon
.last_update
+
1440 msecs_to_jiffies(interval
* 1024 / 1000) * 3;
1441 spin_unlock_irq(&mac
->lock
);
1443 if (interval
> 0 && time_is_before_jiffies(timeout
)) {
1444 dev_dbg_f(zd_mac_dev(mac
), "beacon interrupt stalled, "
1446 "(interval: %d, dtim: %d)\n",
1449 zd_chip_disable_hwint(&mac
->chip
);
1451 beacon
= ieee80211_beacon_get(mac
->hw
, mac
->vif
);
1453 zd_mac_free_cur_beacon(mac
);
1455 zd_mac_config_beacon(mac
->hw
, beacon
, false);
1458 zd_set_beacon_interval(&mac
->chip
, interval
, period
, mac
->type
);
1460 zd_chip_enable_hwint(&mac
->chip
);
1462 spin_lock_irq(&mac
->lock
);
1463 mac
->beacon
.last_update
= jiffies
;
1464 spin_unlock_irq(&mac
->lock
);
1468 queue_delayed_work(zd_workqueue
, &mac
->beacon
.watchdog_work
,
1469 BEACON_WATCHDOG_DELAY
);
1472 static void beacon_init(struct zd_mac
*mac
)
1474 INIT_DELAYED_WORK(&mac
->beacon
.watchdog_work
, beacon_watchdog_handler
);
1477 static void beacon_enable(struct zd_mac
*mac
)
1479 dev_dbg_f(zd_mac_dev(mac
), "\n");
1481 mac
->beacon
.last_update
= jiffies
;
1482 queue_delayed_work(zd_workqueue
, &mac
->beacon
.watchdog_work
,
1483 BEACON_WATCHDOG_DELAY
);
1486 static void beacon_disable(struct zd_mac
*mac
)
1488 dev_dbg_f(zd_mac_dev(mac
), "\n");
1489 cancel_delayed_work_sync(&mac
->beacon
.watchdog_work
);
1491 zd_mac_free_cur_beacon(mac
);
1494 #define LINK_LED_WORK_DELAY HZ
1496 static void link_led_handler(struct work_struct
*work
)
1498 struct zd_mac
*mac
=
1499 container_of(work
, struct zd_mac
, housekeeping
.link_led_work
.work
);
1500 struct zd_chip
*chip
= &mac
->chip
;
1504 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1507 spin_lock_irq(&mac
->lock
);
1508 is_associated
= mac
->associated
;
1509 spin_unlock_irq(&mac
->lock
);
1511 r
= zd_chip_control_leds(chip
,
1512 is_associated
? ZD_LED_ASSOCIATED
: ZD_LED_SCANNING
);
1514 dev_dbg_f(zd_mac_dev(mac
), "zd_chip_control_leds error %d\n", r
);
1517 queue_delayed_work(zd_workqueue
, &mac
->housekeeping
.link_led_work
,
1518 LINK_LED_WORK_DELAY
);
1521 static void housekeeping_init(struct zd_mac
*mac
)
1523 INIT_DELAYED_WORK(&mac
->housekeeping
.link_led_work
, link_led_handler
);
1526 static void housekeeping_enable(struct zd_mac
*mac
)
1528 dev_dbg_f(zd_mac_dev(mac
), "\n");
1529 queue_delayed_work(zd_workqueue
, &mac
->housekeeping
.link_led_work
,
1533 static void housekeeping_disable(struct zd_mac
*mac
)
1535 dev_dbg_f(zd_mac_dev(mac
), "\n");
1536 cancel_delayed_work_sync(&mac
->housekeeping
.link_led_work
);
1537 zd_chip_control_leds(&mac
->chip
, ZD_LED_OFF
);