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
);
605 int r
, ret
, num_cmds
, req_pos
= 0;
607 /* 4 more bytes for tail CRC */
608 u32 full_len
= beacon
->len
+ 4;
609 unsigned long end_jiffies
, message_jiffies
;
610 struct zd_ioreq32
*ioreqs
;
612 /* Alloc memory for full beacon write at once. */
613 num_cmds
= 1 + zd_chip_is_zd1211b(&mac
->chip
) + full_len
;
614 ioreqs
= kmalloc(num_cmds
* sizeof(struct zd_ioreq32
), GFP_KERNEL
);
618 mutex_lock(&mac
->chip
.mutex
);
620 r
= zd_iowrite32_locked(&mac
->chip
, 0, CR_BCN_FIFO_SEMAPHORE
);
623 r
= zd_ioread32_locked(&mac
->chip
, &tmp
, CR_BCN_FIFO_SEMAPHORE
);
627 end_jiffies
= jiffies
+ HZ
/ 2; /*~500ms*/
628 message_jiffies
= jiffies
+ HZ
/ 10; /*~100ms*/
630 r
= zd_ioread32_locked(&mac
->chip
, &tmp
, CR_BCN_FIFO_SEMAPHORE
);
633 if (time_is_before_eq_jiffies(message_jiffies
)) {
634 message_jiffies
= jiffies
+ HZ
/ 10;
635 dev_err(zd_mac_dev(mac
),
636 "CR_BCN_FIFO_SEMAPHORE not ready\n");
637 if (time_is_before_eq_jiffies(end_jiffies
)) {
638 dev_err(zd_mac_dev(mac
),
639 "Giving up beacon config.\n");
647 ioreqs
[req_pos
].addr
= CR_BCN_FIFO
;
648 ioreqs
[req_pos
].value
= full_len
- 1;
650 if (zd_chip_is_zd1211b(&mac
->chip
)) {
651 ioreqs
[req_pos
].addr
= CR_BCN_LENGTH
;
652 ioreqs
[req_pos
].value
= full_len
- 1;
656 for (j
= 0 ; j
< beacon
->len
; j
++) {
657 ioreqs
[req_pos
].addr
= CR_BCN_FIFO
;
658 ioreqs
[req_pos
].value
= *((u8
*)(beacon
->data
+ j
));
662 for (j
= 0; j
< 4; j
++) {
663 ioreqs
[req_pos
].addr
= CR_BCN_FIFO
;
664 ioreqs
[req_pos
].value
= 0x0;
668 BUG_ON(req_pos
!= num_cmds
);
670 r
= zd_iowrite32a_locked(&mac
->chip
, ioreqs
, num_cmds
);
674 * Try very hard to release device beacon semaphore, as otherwise
675 * device/driver can be left in unusable state.
677 end_jiffies
= jiffies
+ HZ
/ 2; /*~500ms*/
678 ret
= zd_iowrite32_locked(&mac
->chip
, 1, CR_BCN_FIFO_SEMAPHORE
);
680 if (time_is_before_eq_jiffies(end_jiffies
)) {
686 ret
= zd_iowrite32_locked(&mac
->chip
, 1, CR_BCN_FIFO_SEMAPHORE
);
690 dev_err(zd_mac_dev(mac
), "Could not release "
691 "CR_BCN_FIFO_SEMAPHORE!\n");
692 if (r
< 0 || ret
< 0) {
698 /* 802.11b/g 2.4G CCK 1Mb
699 * 802.11a, not yet implemented, uses different values (see GPL vendor
702 r
= zd_iowrite32_locked(&mac
->chip
, 0x00000400 | (full_len
<< 19),
705 mutex_unlock(&mac
->chip
.mutex
);
710 static int fill_ctrlset(struct zd_mac
*mac
,
714 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
715 unsigned int frag_len
= skb
->len
+ FCS_LEN
;
716 unsigned int packet_length
;
717 struct ieee80211_rate
*txrate
;
718 struct zd_ctrlset
*cs
= (struct zd_ctrlset
*)
719 skb_push(skb
, sizeof(struct zd_ctrlset
));
720 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
722 ZD_ASSERT(frag_len
<= 0xffff);
724 txrate
= ieee80211_get_tx_rate(mac
->hw
, info
);
726 cs
->modulation
= txrate
->hw_value
;
727 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_USE_SHORT_PREAMBLE
)
728 cs
->modulation
= txrate
->hw_value_short
;
730 cs
->tx_length
= cpu_to_le16(frag_len
);
732 cs_set_control(mac
, cs
, hdr
, info
);
734 packet_length
= frag_len
+ sizeof(struct zd_ctrlset
) + 10;
735 ZD_ASSERT(packet_length
<= 0xffff);
736 /* ZD1211B: Computing the length difference this way, gives us
737 * flexibility to compute the packet length.
739 cs
->packet_length
= cpu_to_le16(zd_chip_is_zd1211b(&mac
->chip
) ?
740 packet_length
- frag_len
: packet_length
);
744 * - transmit frame length in microseconds
745 * - seems to be derived from frame length
746 * - see Cal_Us_Service() in zdinlinef.h
747 * - if macp->bTxBurstEnable is enabled, then multiply by 4
748 * - bTxBurstEnable is never set in the vendor driver
751 * - "for PLCP configuration"
752 * - always 0 except in some situations at 802.11b 11M
753 * - see line 53 of zdinlinef.h
756 r
= zd_calc_tx_length_us(&cs
->service
, ZD_RATE(cs
->modulation
),
757 le16_to_cpu(cs
->tx_length
));
760 cs
->current_length
= cpu_to_le16(r
);
761 cs
->next_frame_length
= 0;
767 * zd_op_tx - transmits a network frame to the device
769 * @dev: mac80211 hardware device
770 * @skb: socket buffer
771 * @control: the control structure
773 * This function transmit an IEEE 802.11 network frame to the device. The
774 * control block of the skbuff will be initialized. If necessary the incoming
775 * mac80211 queues will be stopped.
777 static int zd_op_tx(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
779 struct zd_mac
*mac
= zd_hw_mac(hw
);
780 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
783 r
= fill_ctrlset(mac
, skb
);
787 info
->rate_driver_data
[0] = hw
;
789 r
= zd_usb_tx(&mac
->chip
.usb
, skb
);
800 * filter_ack - filters incoming packets for acknowledgements
801 * @dev: the mac80211 device
802 * @rx_hdr: received header
803 * @stats: the status for the received packet
805 * This functions looks for ACK packets and tries to match them with the
806 * frames in the tx queue. If a match is found the frame will be dequeued and
807 * the upper layers is informed about the successful transmission. If
808 * mac80211 queues have been stopped and the number of frames still to be
809 * transmitted is low the queues will be opened again.
811 * Returns 1 if the frame was an ACK, 0 if it was ignored.
813 static int filter_ack(struct ieee80211_hw
*hw
, struct ieee80211_hdr
*rx_hdr
,
814 struct ieee80211_rx_status
*stats
)
816 struct zd_mac
*mac
= zd_hw_mac(hw
);
818 struct sk_buff_head
*q
;
823 if (!ieee80211_is_ack(rx_hdr
->frame_control
))
826 q
= &mac
->ack_wait_queue
;
827 spin_lock_irqsave(&q
->lock
, flags
);
828 skb_queue_walk(q
, skb
) {
829 struct ieee80211_hdr
*tx_hdr
;
833 if (mac
->ack_pending
&& skb_queue_is_first(q
, skb
))
836 tx_hdr
= (struct ieee80211_hdr
*)skb
->data
;
837 if (likely(!memcmp(tx_hdr
->addr2
, rx_hdr
->addr1
, ETH_ALEN
)))
845 for (i
=1; i
<position
; i
++) {
846 skb
= __skb_dequeue(q
);
847 zd_mac_tx_status(hw
, skb
,
848 mac
->ack_pending
? mac
->ack_signal
: 0,
850 mac
->ack_pending
= 0;
853 mac
->ack_pending
= 1;
854 mac
->ack_signal
= stats
->signal
;
856 /* Prevent pending tx-packet on AP-mode */
857 if (mac
->type
== NL80211_IFTYPE_AP
) {
858 skb
= __skb_dequeue(q
);
859 zd_mac_tx_status(hw
, skb
, mac
->ack_signal
, NULL
);
860 mac
->ack_pending
= 0;
864 spin_unlock_irqrestore(&q
->lock
, flags
);
868 int zd_mac_rx(struct ieee80211_hw
*hw
, const u8
*buffer
, unsigned int length
)
870 struct zd_mac
*mac
= zd_hw_mac(hw
);
871 struct ieee80211_rx_status stats
;
872 const struct rx_status
*status
;
880 if (length
< ZD_PLCP_HEADER_SIZE
+ 10 /* IEEE80211_1ADDR_LEN */ +
881 FCS_LEN
+ sizeof(struct rx_status
))
884 memset(&stats
, 0, sizeof(stats
));
886 /* Note about pass_failed_fcs and pass_ctrl access below:
887 * mac locking intentionally omitted here, as this is the only unlocked
888 * reader and the only writer is configure_filter. Plus, if there were
889 * any races accessing these variables, it wouldn't really matter.
890 * If mac80211 ever provides a way for us to access filter flags
891 * from outside configure_filter, we could improve on this. Also, this
892 * situation may change once we implement some kind of DMA-into-skb
895 /* Caller has to ensure that length >= sizeof(struct rx_status). */
896 status
= (struct rx_status
*)
897 (buffer
+ (length
- sizeof(struct rx_status
)));
898 if (status
->frame_status
& ZD_RX_ERROR
) {
899 if (mac
->pass_failed_fcs
&&
900 (status
->frame_status
& ZD_RX_CRC32_ERROR
)) {
901 stats
.flag
|= RX_FLAG_FAILED_FCS_CRC
;
908 stats
.freq
= zd_channels
[_zd_chip_get_channel(&mac
->chip
) - 1].center_freq
;
909 stats
.band
= IEEE80211_BAND_2GHZ
;
910 stats
.signal
= status
->signal_strength
;
912 rate
= zd_rx_rate(buffer
, status
);
914 /* todo: return index in the big switches in zd_rx_rate instead */
915 for (i
= 0; i
< mac
->band
.n_bitrates
; i
++)
916 if (rate
== mac
->band
.bitrates
[i
].hw_value
)
919 length
-= ZD_PLCP_HEADER_SIZE
+ sizeof(struct rx_status
);
920 buffer
+= ZD_PLCP_HEADER_SIZE
;
922 /* Except for bad frames, filter each frame to see if it is an ACK, in
923 * which case our internal TX tracking is updated. Normally we then
924 * bail here as there's no need to pass ACKs on up to the stack, but
925 * there is also the case where the stack has requested us to pass
926 * control frames on up (pass_ctrl) which we must consider. */
928 filter_ack(hw
, (struct ieee80211_hdr
*)buffer
, &stats
)
932 fc
= get_unaligned((__le16
*)buffer
);
933 need_padding
= ieee80211_is_data_qos(fc
) ^ ieee80211_has_a4(fc
);
935 skb
= dev_alloc_skb(length
+ (need_padding
? 2 : 0));
939 /* Make sure the payload data is 4 byte aligned. */
943 /* FIXME : could we avoid this big memcpy ? */
944 memcpy(skb_put(skb
, length
), buffer
, length
);
946 memcpy(IEEE80211_SKB_RXCB(skb
), &stats
, sizeof(stats
));
947 ieee80211_rx_irqsafe(hw
, skb
);
951 static int zd_op_add_interface(struct ieee80211_hw
*hw
,
952 struct ieee80211_vif
*vif
)
954 struct zd_mac
*mac
= zd_hw_mac(hw
);
956 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
957 if (mac
->type
!= NL80211_IFTYPE_UNSPECIFIED
)
961 case NL80211_IFTYPE_MONITOR
:
962 case NL80211_IFTYPE_MESH_POINT
:
963 case NL80211_IFTYPE_STATION
:
964 case NL80211_IFTYPE_ADHOC
:
965 mac
->type
= vif
->type
;
973 return set_mac_and_bssid(mac
);
976 static void zd_op_remove_interface(struct ieee80211_hw
*hw
,
977 struct ieee80211_vif
*vif
)
979 struct zd_mac
*mac
= zd_hw_mac(hw
);
980 mac
->type
= NL80211_IFTYPE_UNSPECIFIED
;
982 zd_set_beacon_interval(&mac
->chip
, 0, 0, NL80211_IFTYPE_UNSPECIFIED
);
983 zd_write_mac_addr(&mac
->chip
, NULL
);
986 static int zd_op_config(struct ieee80211_hw
*hw
, u32 changed
)
988 struct zd_mac
*mac
= zd_hw_mac(hw
);
989 struct ieee80211_conf
*conf
= &hw
->conf
;
991 return zd_chip_set_channel(&mac
->chip
, conf
->channel
->hw_value
);
994 static void zd_beacon_done(struct zd_mac
*mac
)
996 struct sk_buff
*skb
, *beacon
;
998 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1000 if (!mac
->vif
|| mac
->vif
->type
!= NL80211_IFTYPE_AP
)
1004 * Send out buffered broad- and multicast frames.
1006 while (!ieee80211_queue_stopped(mac
->hw
, 0)) {
1007 skb
= ieee80211_get_buffered_bc(mac
->hw
, mac
->vif
);
1010 zd_op_tx(mac
->hw
, skb
);
1014 * Fetch next beacon so that tim_count is updated.
1016 beacon
= ieee80211_beacon_get(mac
->hw
, mac
->vif
);
1018 zd_mac_config_beacon(mac
->hw
, beacon
);
1022 spin_lock_irq(&mac
->lock
);
1023 mac
->beacon
.last_update
= jiffies
;
1024 spin_unlock_irq(&mac
->lock
);
1027 static void zd_process_intr(struct work_struct
*work
)
1030 unsigned long flags
;
1031 struct zd_mac
*mac
= container_of(work
, struct zd_mac
, process_intr
);
1033 spin_lock_irqsave(&mac
->lock
, flags
);
1034 int_status
= le16_to_cpu(*(__le16
*)(mac
->intr_buffer
+ 4));
1035 spin_unlock_irqrestore(&mac
->lock
, flags
);
1037 if (int_status
& INT_CFG_NEXT_BCN
) {
1038 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1039 zd_beacon_done(mac
);
1041 dev_dbg_f(zd_mac_dev(mac
), "Unsupported interrupt\n");
1044 zd_chip_enable_hwint(&mac
->chip
);
1048 static u64
zd_op_prepare_multicast(struct ieee80211_hw
*hw
,
1049 struct netdev_hw_addr_list
*mc_list
)
1051 struct zd_mac
*mac
= zd_hw_mac(hw
);
1052 struct zd_mc_hash hash
;
1053 struct netdev_hw_addr
*ha
;
1057 netdev_hw_addr_list_for_each(ha
, mc_list
) {
1058 dev_dbg_f(zd_mac_dev(mac
), "mc addr %pM\n", ha
->addr
);
1059 zd_mc_add_addr(&hash
, ha
->addr
);
1062 return hash
.low
| ((u64
)hash
.high
<< 32);
1065 #define SUPPORTED_FIF_FLAGS \
1066 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1067 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1068 static void zd_op_configure_filter(struct ieee80211_hw
*hw
,
1069 unsigned int changed_flags
,
1070 unsigned int *new_flags
,
1073 struct zd_mc_hash hash
= {
1075 .high
= multicast
>> 32,
1077 struct zd_mac
*mac
= zd_hw_mac(hw
);
1078 unsigned long flags
;
1081 /* Only deal with supported flags */
1082 changed_flags
&= SUPPORTED_FIF_FLAGS
;
1083 *new_flags
&= SUPPORTED_FIF_FLAGS
;
1086 * If multicast parameter (as returned by zd_op_prepare_multicast)
1087 * has changed, no bit in changed_flags is set. To handle this
1088 * situation, we do not return if changed_flags is 0. If we do so,
1089 * we will have some issue with IPv6 which uses multicast for link
1090 * layer address resolution.
1092 if (*new_flags
& (FIF_PROMISC_IN_BSS
| FIF_ALLMULTI
))
1093 zd_mc_add_all(&hash
);
1095 spin_lock_irqsave(&mac
->lock
, flags
);
1096 mac
->pass_failed_fcs
= !!(*new_flags
& FIF_FCSFAIL
);
1097 mac
->pass_ctrl
= !!(*new_flags
& FIF_CONTROL
);
1098 mac
->multicast_hash
= hash
;
1099 spin_unlock_irqrestore(&mac
->lock
, flags
);
1101 zd_chip_set_multicast_hash(&mac
->chip
, &hash
);
1103 if (changed_flags
& FIF_CONTROL
) {
1104 r
= set_rx_filter(mac
);
1106 dev_err(zd_mac_dev(mac
), "set_rx_filter error %d\n", r
);
1109 /* no handling required for FIF_OTHER_BSS as we don't currently
1110 * do BSSID filtering */
1111 /* FIXME: in future it would be nice to enable the probe response
1112 * filter (so that the driver doesn't see them) until
1113 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1114 * have to schedule work to enable prbresp reception, which might
1115 * happen too late. For now we'll just listen and forward them all the
1119 static void set_rts_cts(struct zd_mac
*mac
, unsigned int short_preamble
)
1121 mutex_lock(&mac
->chip
.mutex
);
1122 zd_chip_set_rts_cts_rate_locked(&mac
->chip
, short_preamble
);
1123 mutex_unlock(&mac
->chip
.mutex
);
1126 static void zd_op_bss_info_changed(struct ieee80211_hw
*hw
,
1127 struct ieee80211_vif
*vif
,
1128 struct ieee80211_bss_conf
*bss_conf
,
1131 struct zd_mac
*mac
= zd_hw_mac(hw
);
1134 dev_dbg_f(zd_mac_dev(mac
), "changes: %x\n", changes
);
1136 if (mac
->type
== NL80211_IFTYPE_MESH_POINT
||
1137 mac
->type
== NL80211_IFTYPE_ADHOC
) {
1139 if (changes
& BSS_CHANGED_BEACON
) {
1140 struct sk_buff
*beacon
= ieee80211_beacon_get(hw
, vif
);
1143 zd_chip_disable_hwint(&mac
->chip
);
1144 zd_mac_config_beacon(hw
, beacon
);
1145 zd_chip_enable_hwint(&mac
->chip
);
1150 if (changes
& BSS_CHANGED_BEACON_ENABLED
) {
1154 if (bss_conf
->enable_beacon
) {
1155 period
= bss_conf
->dtim_period
;
1156 interval
= bss_conf
->beacon_int
;
1159 spin_lock_irq(&mac
->lock
);
1160 mac
->beacon
.period
= period
;
1161 mac
->beacon
.interval
= interval
;
1162 mac
->beacon
.last_update
= jiffies
;
1163 spin_unlock_irq(&mac
->lock
);
1165 zd_set_beacon_interval(&mac
->chip
, interval
, period
,
1169 associated
= is_valid_ether_addr(bss_conf
->bssid
);
1171 spin_lock_irq(&mac
->lock
);
1172 mac
->associated
= associated
;
1173 spin_unlock_irq(&mac
->lock
);
1175 /* TODO: do hardware bssid filtering */
1177 if (changes
& BSS_CHANGED_ERP_PREAMBLE
) {
1178 spin_lock_irq(&mac
->lock
);
1179 mac
->short_preamble
= bss_conf
->use_short_preamble
;
1180 spin_unlock_irq(&mac
->lock
);
1182 set_rts_cts(mac
, bss_conf
->use_short_preamble
);
1186 static u64
zd_op_get_tsf(struct ieee80211_hw
*hw
)
1188 struct zd_mac
*mac
= zd_hw_mac(hw
);
1189 return zd_chip_get_tsf(&mac
->chip
);
1192 static const struct ieee80211_ops zd_ops
= {
1194 .start
= zd_op_start
,
1196 .add_interface
= zd_op_add_interface
,
1197 .remove_interface
= zd_op_remove_interface
,
1198 .config
= zd_op_config
,
1199 .prepare_multicast
= zd_op_prepare_multicast
,
1200 .configure_filter
= zd_op_configure_filter
,
1201 .bss_info_changed
= zd_op_bss_info_changed
,
1202 .get_tsf
= zd_op_get_tsf
,
1205 struct ieee80211_hw
*zd_mac_alloc_hw(struct usb_interface
*intf
)
1208 struct ieee80211_hw
*hw
;
1210 hw
= ieee80211_alloc_hw(sizeof(struct zd_mac
), &zd_ops
);
1212 dev_dbg_f(&intf
->dev
, "out of memory\n");
1216 mac
= zd_hw_mac(hw
);
1218 memset(mac
, 0, sizeof(*mac
));
1219 spin_lock_init(&mac
->lock
);
1222 mac
->type
= NL80211_IFTYPE_UNSPECIFIED
;
1224 memcpy(mac
->channels
, zd_channels
, sizeof(zd_channels
));
1225 memcpy(mac
->rates
, zd_rates
, sizeof(zd_rates
));
1226 mac
->band
.n_bitrates
= ARRAY_SIZE(zd_rates
);
1227 mac
->band
.bitrates
= mac
->rates
;
1228 mac
->band
.n_channels
= ARRAY_SIZE(zd_channels
);
1229 mac
->band
.channels
= mac
->channels
;
1231 hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = &mac
->band
;
1233 hw
->flags
= IEEE80211_HW_RX_INCLUDES_FCS
|
1234 IEEE80211_HW_SIGNAL_UNSPEC
|
1235 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING
;
1237 hw
->wiphy
->interface_modes
=
1238 BIT(NL80211_IFTYPE_MESH_POINT
) |
1239 BIT(NL80211_IFTYPE_STATION
) |
1240 BIT(NL80211_IFTYPE_ADHOC
);
1242 hw
->max_signal
= 100;
1244 hw
->extra_tx_headroom
= sizeof(struct zd_ctrlset
);
1247 * Tell mac80211 that we support multi rate retries
1249 hw
->max_rates
= IEEE80211_TX_MAX_RATES
;
1250 hw
->max_rate_tries
= 18; /* 9 rates * 2 retries/rate */
1252 skb_queue_head_init(&mac
->ack_wait_queue
);
1253 mac
->ack_pending
= 0;
1255 zd_chip_init(&mac
->chip
, hw
, intf
);
1256 housekeeping_init(mac
);
1258 INIT_WORK(&mac
->process_intr
, zd_process_intr
);
1260 SET_IEEE80211_DEV(hw
, &intf
->dev
);
1264 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1266 static void beacon_watchdog_handler(struct work_struct
*work
)
1268 struct zd_mac
*mac
=
1269 container_of(work
, struct zd_mac
, beacon
.watchdog_work
.work
);
1270 struct sk_buff
*beacon
;
1271 unsigned long timeout
;
1272 int interval
, period
;
1274 if (!test_bit(ZD_DEVICE_RUNNING
, &mac
->flags
))
1276 if (mac
->type
!= NL80211_IFTYPE_AP
|| !mac
->vif
)
1279 spin_lock_irq(&mac
->lock
);
1280 interval
= mac
->beacon
.interval
;
1281 period
= mac
->beacon
.period
;
1282 timeout
= mac
->beacon
.last_update
+ msecs_to_jiffies(interval
) + HZ
;
1283 spin_unlock_irq(&mac
->lock
);
1285 if (interval
> 0 && time_is_before_jiffies(timeout
)) {
1286 dev_dbg_f(zd_mac_dev(mac
), "beacon interrupt stalled, "
1288 "(interval: %d, dtim: %d)\n",
1291 zd_chip_disable_hwint(&mac
->chip
);
1293 beacon
= ieee80211_beacon_get(mac
->hw
, mac
->vif
);
1295 zd_mac_config_beacon(mac
->hw
, beacon
);
1299 zd_set_beacon_interval(&mac
->chip
, interval
, period
, mac
->type
);
1301 zd_chip_enable_hwint(&mac
->chip
);
1303 spin_lock_irq(&mac
->lock
);
1304 mac
->beacon
.last_update
= jiffies
;
1305 spin_unlock_irq(&mac
->lock
);
1309 queue_delayed_work(zd_workqueue
, &mac
->beacon
.watchdog_work
,
1310 BEACON_WATCHDOG_DELAY
);
1313 static void beacon_init(struct zd_mac
*mac
)
1315 INIT_DELAYED_WORK(&mac
->beacon
.watchdog_work
, beacon_watchdog_handler
);
1318 static void beacon_enable(struct zd_mac
*mac
)
1320 dev_dbg_f(zd_mac_dev(mac
), "\n");
1322 mac
->beacon
.last_update
= jiffies
;
1323 queue_delayed_work(zd_workqueue
, &mac
->beacon
.watchdog_work
,
1324 BEACON_WATCHDOG_DELAY
);
1327 static void beacon_disable(struct zd_mac
*mac
)
1329 dev_dbg_f(zd_mac_dev(mac
), "\n");
1330 cancel_delayed_work_sync(&mac
->beacon
.watchdog_work
);
1333 #define LINK_LED_WORK_DELAY HZ
1335 static void link_led_handler(struct work_struct
*work
)
1337 struct zd_mac
*mac
=
1338 container_of(work
, struct zd_mac
, housekeeping
.link_led_work
.work
);
1339 struct zd_chip
*chip
= &mac
->chip
;
1343 spin_lock_irq(&mac
->lock
);
1344 is_associated
= mac
->associated
;
1345 spin_unlock_irq(&mac
->lock
);
1347 r
= zd_chip_control_leds(chip
,
1348 is_associated
? ZD_LED_ASSOCIATED
: ZD_LED_SCANNING
);
1350 dev_dbg_f(zd_mac_dev(mac
), "zd_chip_control_leds error %d\n", r
);
1352 queue_delayed_work(zd_workqueue
, &mac
->housekeeping
.link_led_work
,
1353 LINK_LED_WORK_DELAY
);
1356 static void housekeeping_init(struct zd_mac
*mac
)
1358 INIT_DELAYED_WORK(&mac
->housekeeping
.link_led_work
, link_led_handler
);
1361 static void housekeeping_enable(struct zd_mac
*mac
)
1363 dev_dbg_f(zd_mac_dev(mac
), "\n");
1364 queue_delayed_work(zd_workqueue
, &mac
->housekeeping
.link_led_work
,
1368 static void housekeeping_disable(struct zd_mac
*mac
)
1370 dev_dbg_f(zd_mac_dev(mac
), "\n");
1371 cancel_delayed_work_sync(&mac
->housekeeping
.link_led_work
);
1372 zd_chip_control_leds(&mac
->chip
, ZD_LED_OFF
);