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/usb.h>
26 #include <linux/jiffies.h>
27 #include <net/ieee80211_radiotap.h>
34 struct zd_reg_alpha2_map {
39 static struct zd_reg_alpha2_map reg_alpha2_map[] = {
40 { ZD_REGDOMAIN_FCC, "US" },
41 { ZD_REGDOMAIN_IC, "CA" },
42 { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
43 { ZD_REGDOMAIN_JAPAN, "JP" },
44 { ZD_REGDOMAIN_JAPAN_ADD, "JP" },
45 { ZD_REGDOMAIN_SPAIN, "ES" },
46 { ZD_REGDOMAIN_FRANCE, "FR" },
49 /* This table contains the hardware specific values for the modulation rates. */
50 static const struct ieee80211_rate zd_rates[] = {
52 .hw_value = ZD_CCK_RATE_1M, },
54 .hw_value = ZD_CCK_RATE_2M,
55 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
56 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
58 .hw_value = ZD_CCK_RATE_5_5M,
59 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
60 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
62 .hw_value = ZD_CCK_RATE_11M,
63 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
64 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
66 .hw_value = ZD_OFDM_RATE_6M,
69 .hw_value = ZD_OFDM_RATE_9M,
72 .hw_value = ZD_OFDM_RATE_12M,
75 .hw_value = ZD_OFDM_RATE_18M,
78 .hw_value = ZD_OFDM_RATE_24M,
81 .hw_value = ZD_OFDM_RATE_36M,
84 .hw_value = ZD_OFDM_RATE_48M,
87 .hw_value = ZD_OFDM_RATE_54M,
91 static const struct ieee80211_channel zd_channels[] = {
92 { .center_freq = 2412, .hw_value = 1 },
93 { .center_freq = 2417, .hw_value = 2 },
94 { .center_freq = 2422, .hw_value = 3 },
95 { .center_freq = 2427, .hw_value = 4 },
96 { .center_freq = 2432, .hw_value = 5 },
97 { .center_freq = 2437, .hw_value = 6 },
98 { .center_freq = 2442, .hw_value = 7 },
99 { .center_freq = 2447, .hw_value = 8 },
100 { .center_freq = 2452, .hw_value = 9 },
101 { .center_freq = 2457, .hw_value = 10 },
102 { .center_freq = 2462, .hw_value = 11 },
103 { .center_freq = 2467, .hw_value = 12 },
104 { .center_freq = 2472, .hw_value = 13 },
105 { .center_freq = 2484, .hw_value = 14 },
108 static void housekeeping_init(struct zd_mac *mac);
109 static void housekeeping_enable(struct zd_mac *mac);
110 static void housekeeping_disable(struct zd_mac *mac);
112 static int zd_reg2alpha2(u8 regdomain, char *alpha2)
115 struct zd_reg_alpha2_map *reg_map;
116 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
117 reg_map = ®_alpha2_map[i];
118 if (regdomain == reg_map->reg) {
119 alpha2[0] = reg_map->alpha2[0];
120 alpha2[1] = reg_map->alpha2[1];
127 int zd_mac_preinit_hw(struct ieee80211_hw *hw)
131 struct zd_mac *mac = zd_hw_mac(hw);
133 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
137 SET_IEEE80211_PERM_ADDR(hw, addr);
142 int zd_mac_init_hw(struct ieee80211_hw *hw)
145 struct zd_mac *mac = zd_hw_mac(hw);
146 struct zd_chip *chip = &mac->chip;
148 u8 default_regdomain;
150 r = zd_chip_enable_int(chip);
153 r = zd_chip_init_hw(chip);
157 ZD_ASSERT(!irqs_disabled());
159 r = zd_read_regdomain(chip, &default_regdomain);
162 spin_lock_irq(&mac->lock);
163 mac->regdomain = mac->default_regdomain = default_regdomain;
164 spin_unlock_irq(&mac->lock);
166 /* We must inform the device that we are doing encryption/decryption in
167 * software at the moment. */
168 r = zd_set_encryption_type(chip, ENC_SNIFFER);
172 r = zd_reg2alpha2(mac->regdomain, alpha2);
174 regulatory_hint(hw->wiphy, alpha2);
178 zd_chip_disable_int(chip);
183 void zd_mac_clear(struct zd_mac *mac)
185 flush_workqueue(zd_workqueue);
186 zd_chip_clear(&mac->chip);
187 ZD_ASSERT(!spin_is_locked(&mac->lock));
188 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
191 static int set_rx_filter(struct zd_mac *mac)
194 u32 filter = STA_RX_FILTER;
196 spin_lock_irqsave(&mac->lock, flags);
198 filter |= RX_FILTER_CTRL;
199 spin_unlock_irqrestore(&mac->lock, flags);
201 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
204 static int set_mc_hash(struct zd_mac *mac)
206 struct zd_mc_hash hash;
208 return zd_chip_set_multicast_hash(&mac->chip, &hash);
211 static int zd_op_start(struct ieee80211_hw *hw)
213 struct zd_mac *mac = zd_hw_mac(hw);
214 struct zd_chip *chip = &mac->chip;
215 struct zd_usb *usb = &chip->usb;
218 if (!usb->initialized) {
219 r = zd_usb_init_hw(usb);
224 r = zd_chip_enable_int(chip);
228 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
231 r = set_rx_filter(mac);
234 r = set_mc_hash(mac);
237 r = zd_chip_switch_radio_on(chip);
240 r = zd_chip_enable_rxtx(chip);
243 r = zd_chip_enable_hwint(chip);
247 housekeeping_enable(mac);
250 zd_chip_disable_rxtx(chip);
252 zd_chip_switch_radio_off(chip);
254 zd_chip_disable_int(chip);
259 static void zd_op_stop(struct ieee80211_hw *hw)
261 struct zd_mac *mac = zd_hw_mac(hw);
262 struct zd_chip *chip = &mac->chip;
264 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
266 /* The order here deliberately is a little different from the open()
267 * method, since we need to make sure there is no opportunity for RX
268 * frames to be processed by mac80211 after we have stopped it.
271 zd_chip_disable_rxtx(chip);
272 housekeeping_disable(mac);
273 flush_workqueue(zd_workqueue);
275 zd_chip_disable_hwint(chip);
276 zd_chip_switch_radio_off(chip);
277 zd_chip_disable_int(chip);
280 while ((skb = skb_dequeue(ack_wait_queue)))
281 dev_kfree_skb_any(skb);
285 * tx_status - reports tx status of a packet if required
286 * @hw - a &struct ieee80211_hw pointer
288 * @flags: extra flags to set in the TX status info
289 * @ackssi: ACK signal strength
290 * @success - True for successfull transmission of the frame
292 * This information calls ieee80211_tx_status_irqsafe() if required by the
293 * control information. It copies the control information into the status
296 * If no status information has been requested, the skb is freed.
298 static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
299 int ackssi, bool success)
301 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
303 ieee80211_tx_info_clear_status(info);
306 info->flags |= IEEE80211_TX_STAT_ACK;
307 info->status.ack_signal = ackssi;
308 ieee80211_tx_status_irqsafe(hw, skb);
312 * zd_mac_tx_failed - callback for failed frames
313 * @dev: the mac80211 wireless device
315 * This function is called if a frame couldn't be succesfully be
316 * transferred. The first frame from the tx queue, will be selected and
317 * reported as error to the upper layers.
319 void zd_mac_tx_failed(struct ieee80211_hw *hw)
321 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
324 skb = skb_dequeue(q);
328 tx_status(hw, skb, 0, 0);
332 * zd_mac_tx_to_dev - callback for USB layer
333 * @skb: a &sk_buff pointer
334 * @error: error value, 0 if transmission successful
336 * Informs the MAC layer that the frame has successfully transferred to the
337 * device. If an ACK is required and the transfer to the device has been
338 * successful, the packets are put on the @ack_wait_queue with
339 * the control set removed.
341 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
343 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
344 struct ieee80211_hw *hw = info->rate_driver_data[0];
346 skb_pull(skb, sizeof(struct zd_ctrlset));
347 if (unlikely(error ||
348 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
349 tx_status(hw, skb, 0, !error);
351 struct sk_buff_head *q =
352 &zd_hw_mac(hw)->ack_wait_queue;
354 skb_queue_tail(q, skb);
355 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
356 zd_mac_tx_failed(hw);
360 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
362 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
363 * the zd-rate values.
365 static const u8 rate_divisor[] = {
366 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
367 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
368 /* Bits must be doubled. */
369 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
370 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
371 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
372 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
373 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
374 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
375 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
376 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
377 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
378 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
381 u32 bits = (u32)tx_length * 8;
384 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
389 case ZD_CCK_RATE_5_5M:
390 bits = (2*bits) + 10; /* round up to the next integer */
392 case ZD_CCK_RATE_11M:
395 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
396 if (0 < t && t <= 3) {
397 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
400 bits += 10; /* round up to the next integer */
407 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
408 struct ieee80211_hdr *header,
409 struct ieee80211_tx_info *info)
413 * - if backoff needed, enable bit 0
414 * - if burst (backoff not needed) disable bit 0
420 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
421 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
424 if (is_multicast_ether_addr(header->addr1))
425 cs->control |= ZD_CS_MULTICAST;
428 if (ieee80211_is_pspoll(header->frame_control))
429 cs->control |= ZD_CS_PS_POLL_FRAME;
431 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
432 cs->control |= ZD_CS_RTS;
434 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
435 cs->control |= ZD_CS_SELF_CTS;
437 /* FIXME: Management frame? */
440 static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
442 struct zd_mac *mac = zd_hw_mac(hw);
445 /* 4 more bytes for tail CRC */
446 u32 full_len = beacon->len + 4;
448 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
451 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
456 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
459 if ((++j % 100) == 0) {
460 printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
462 printk(KERN_ERR "Giving up beacon config.\n");
469 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
472 if (zd_chip_is_zd1211b(&mac->chip)) {
473 r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
478 for (j = 0 ; j < beacon->len; j++) {
479 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO,
480 *((u8 *)(beacon->data + j)));
485 for (j = 0; j < 4; j++) {
486 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
491 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
495 /* 802.11b/g 2.4G CCK 1Mb
496 * 802.11a, not yet implemented, uses different values (see GPL vendor
499 return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
503 static int fill_ctrlset(struct zd_mac *mac,
507 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
508 unsigned int frag_len = skb->len + FCS_LEN;
509 unsigned int packet_length;
510 struct ieee80211_rate *txrate;
511 struct zd_ctrlset *cs = (struct zd_ctrlset *)
512 skb_push(skb, sizeof(struct zd_ctrlset));
513 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
515 ZD_ASSERT(frag_len <= 0xffff);
517 txrate = ieee80211_get_tx_rate(mac->hw, info);
519 cs->modulation = txrate->hw_value;
520 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
521 cs->modulation = txrate->hw_value_short;
523 cs->tx_length = cpu_to_le16(frag_len);
525 cs_set_control(mac, cs, hdr, info);
527 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
528 ZD_ASSERT(packet_length <= 0xffff);
529 /* ZD1211B: Computing the length difference this way, gives us
530 * flexibility to compute the packet length.
532 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
533 packet_length - frag_len : packet_length);
537 * - transmit frame length in microseconds
538 * - seems to be derived from frame length
539 * - see Cal_Us_Service() in zdinlinef.h
540 * - if macp->bTxBurstEnable is enabled, then multiply by 4
541 * - bTxBurstEnable is never set in the vendor driver
544 * - "for PLCP configuration"
545 * - always 0 except in some situations at 802.11b 11M
546 * - see line 53 of zdinlinef.h
549 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
550 le16_to_cpu(cs->tx_length));
553 cs->current_length = cpu_to_le16(r);
554 cs->next_frame_length = 0;
560 * zd_op_tx - transmits a network frame to the device
562 * @dev: mac80211 hardware device
563 * @skb: socket buffer
564 * @control: the control structure
566 * This function transmit an IEEE 802.11 network frame to the device. The
567 * control block of the skbuff will be initialized. If necessary the incoming
568 * mac80211 queues will be stopped.
570 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
572 struct zd_mac *mac = zd_hw_mac(hw);
573 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
576 r = fill_ctrlset(mac, skb);
580 info->rate_driver_data[0] = hw;
582 r = zd_usb_tx(&mac->chip.usb, skb);
589 * filter_ack - filters incoming packets for acknowledgements
590 * @dev: the mac80211 device
591 * @rx_hdr: received header
592 * @stats: the status for the received packet
594 * This functions looks for ACK packets and tries to match them with the
595 * frames in the tx queue. If a match is found the frame will be dequeued and
596 * the upper layers is informed about the successful transmission. If
597 * mac80211 queues have been stopped and the number of frames still to be
598 * transmitted is low the queues will be opened again.
600 * Returns 1 if the frame was an ACK, 0 if it was ignored.
602 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
603 struct ieee80211_rx_status *stats)
606 struct sk_buff_head *q;
609 if (!ieee80211_is_ack(rx_hdr->frame_control))
612 q = &zd_hw_mac(hw)->ack_wait_queue;
613 spin_lock_irqsave(&q->lock, flags);
614 skb_queue_walk(q, skb) {
615 struct ieee80211_hdr *tx_hdr;
617 tx_hdr = (struct ieee80211_hdr *)skb->data;
618 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
620 __skb_unlink(skb, q);
621 tx_status(hw, skb, stats->signal, 1);
626 spin_unlock_irqrestore(&q->lock, flags);
630 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
632 struct zd_mac *mac = zd_hw_mac(hw);
633 struct ieee80211_rx_status stats;
634 const struct rx_status *status;
642 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
643 FCS_LEN + sizeof(struct rx_status))
646 memset(&stats, 0, sizeof(stats));
648 /* Note about pass_failed_fcs and pass_ctrl access below:
649 * mac locking intentionally omitted here, as this is the only unlocked
650 * reader and the only writer is configure_filter. Plus, if there were
651 * any races accessing these variables, it wouldn't really matter.
652 * If mac80211 ever provides a way for us to access filter flags
653 * from outside configure_filter, we could improve on this. Also, this
654 * situation may change once we implement some kind of DMA-into-skb
657 /* Caller has to ensure that length >= sizeof(struct rx_status). */
658 status = (struct rx_status *)
659 (buffer + (length - sizeof(struct rx_status)));
660 if (status->frame_status & ZD_RX_ERROR) {
661 if (mac->pass_failed_fcs &&
662 (status->frame_status & ZD_RX_CRC32_ERROR)) {
663 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
670 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
671 stats.band = IEEE80211_BAND_2GHZ;
672 stats.signal = status->signal_strength;
673 stats.qual = zd_rx_qual_percent(buffer,
674 length - sizeof(struct rx_status),
677 rate = zd_rx_rate(buffer, status);
679 /* todo: return index in the big switches in zd_rx_rate instead */
680 for (i = 0; i < mac->band.n_bitrates; i++)
681 if (rate == mac->band.bitrates[i].hw_value)
684 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
685 buffer += ZD_PLCP_HEADER_SIZE;
687 /* Except for bad frames, filter each frame to see if it is an ACK, in
688 * which case our internal TX tracking is updated. Normally we then
689 * bail here as there's no need to pass ACKs on up to the stack, but
690 * there is also the case where the stack has requested us to pass
691 * control frames on up (pass_ctrl) which we must consider. */
693 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
697 fc = *(__le16 *)buffer;
698 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
700 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
704 /* Make sure the the payload data is 4 byte aligned. */
708 memcpy(skb_put(skb, length), buffer, length);
710 ieee80211_rx_irqsafe(hw, skb, &stats);
714 static int zd_op_add_interface(struct ieee80211_hw *hw,
715 struct ieee80211_if_init_conf *conf)
717 struct zd_mac *mac = zd_hw_mac(hw);
719 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
720 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
723 switch (conf->type) {
724 case NL80211_IFTYPE_MONITOR:
725 case NL80211_IFTYPE_MESH_POINT:
726 case NL80211_IFTYPE_STATION:
727 case NL80211_IFTYPE_ADHOC:
728 mac->type = conf->type;
734 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
737 static void zd_op_remove_interface(struct ieee80211_hw *hw,
738 struct ieee80211_if_init_conf *conf)
740 struct zd_mac *mac = zd_hw_mac(hw);
741 mac->type = NL80211_IFTYPE_UNSPECIFIED;
742 zd_set_beacon_interval(&mac->chip, 0);
743 zd_write_mac_addr(&mac->chip, NULL);
746 static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
748 struct zd_mac *mac = zd_hw_mac(hw);
749 struct ieee80211_conf *conf = &hw->conf;
751 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
754 static int zd_op_config_interface(struct ieee80211_hw *hw,
755 struct ieee80211_vif *vif,
756 struct ieee80211_if_conf *conf)
758 struct zd_mac *mac = zd_hw_mac(hw);
762 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
763 mac->type == NL80211_IFTYPE_ADHOC) {
765 if (conf->changed & IEEE80211_IFCC_BEACON) {
766 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
770 r = zd_mac_config_beacon(hw, beacon);
773 r = zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
774 hw->conf.beacon_int);
780 associated = is_valid_ether_addr(conf->bssid);
782 spin_lock_irq(&mac->lock);
783 mac->associated = associated;
784 spin_unlock_irq(&mac->lock);
786 /* TODO: do hardware bssid filtering */
790 static void zd_process_intr(struct work_struct *work)
793 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
795 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
796 if (int_status & INT_CFG_NEXT_BCN) {
798 dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
800 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
802 zd_chip_enable_hwint(&mac->chip);
806 static void set_multicast_hash_handler(struct work_struct *work)
809 container_of(work, struct zd_mac, set_multicast_hash_work);
810 struct zd_mc_hash hash;
812 spin_lock_irq(&mac->lock);
813 hash = mac->multicast_hash;
814 spin_unlock_irq(&mac->lock);
816 zd_chip_set_multicast_hash(&mac->chip, &hash);
819 static void set_rx_filter_handler(struct work_struct *work)
822 container_of(work, struct zd_mac, set_rx_filter_work);
825 dev_dbg_f(zd_mac_dev(mac), "\n");
826 r = set_rx_filter(mac);
828 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
831 #define SUPPORTED_FIF_FLAGS \
832 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
833 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
834 static void zd_op_configure_filter(struct ieee80211_hw *hw,
835 unsigned int changed_flags,
836 unsigned int *new_flags,
837 int mc_count, struct dev_mc_list *mclist)
839 struct zd_mc_hash hash;
840 struct zd_mac *mac = zd_hw_mac(hw);
844 /* Only deal with supported flags */
845 changed_flags &= SUPPORTED_FIF_FLAGS;
846 *new_flags &= SUPPORTED_FIF_FLAGS;
848 /* changed_flags is always populated but this driver
849 * doesn't support all FIF flags so its possible we don't
850 * need to do anything */
854 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
855 zd_mc_add_all(&hash);
858 for (i = 0; i < mc_count; i++) {
861 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n",
863 zd_mc_add_addr(&hash, mclist->dmi_addr);
864 mclist = mclist->next;
868 spin_lock_irqsave(&mac->lock, flags);
869 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
870 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
871 mac->multicast_hash = hash;
872 spin_unlock_irqrestore(&mac->lock, flags);
873 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
875 if (changed_flags & FIF_CONTROL)
876 queue_work(zd_workqueue, &mac->set_rx_filter_work);
878 /* no handling required for FIF_OTHER_BSS as we don't currently
879 * do BSSID filtering */
880 /* FIXME: in future it would be nice to enable the probe response
881 * filter (so that the driver doesn't see them) until
882 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
883 * have to schedule work to enable prbresp reception, which might
884 * happen too late. For now we'll just listen and forward them all the
888 static void set_rts_cts_work(struct work_struct *work)
891 container_of(work, struct zd_mac, set_rts_cts_work);
893 unsigned int short_preamble;
895 mutex_lock(&mac->chip.mutex);
897 spin_lock_irqsave(&mac->lock, flags);
898 mac->updating_rts_rate = 0;
899 short_preamble = mac->short_preamble;
900 spin_unlock_irqrestore(&mac->lock, flags);
902 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
903 mutex_unlock(&mac->chip.mutex);
906 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
907 struct ieee80211_vif *vif,
908 struct ieee80211_bss_conf *bss_conf,
911 struct zd_mac *mac = zd_hw_mac(hw);
914 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
916 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
917 spin_lock_irqsave(&mac->lock, flags);
918 mac->short_preamble = bss_conf->use_short_preamble;
919 if (!mac->updating_rts_rate) {
920 mac->updating_rts_rate = 1;
921 /* FIXME: should disable TX here, until work has
922 * completed and RTS_CTS reg is updated */
923 queue_work(zd_workqueue, &mac->set_rts_cts_work);
925 spin_unlock_irqrestore(&mac->lock, flags);
929 static const struct ieee80211_ops zd_ops = {
931 .start = zd_op_start,
933 .add_interface = zd_op_add_interface,
934 .remove_interface = zd_op_remove_interface,
935 .config = zd_op_config,
936 .config_interface = zd_op_config_interface,
937 .configure_filter = zd_op_configure_filter,
938 .bss_info_changed = zd_op_bss_info_changed,
941 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
944 struct ieee80211_hw *hw;
946 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
948 dev_dbg_f(&intf->dev, "out of memory\n");
954 memset(mac, 0, sizeof(*mac));
955 spin_lock_init(&mac->lock);
958 mac->type = NL80211_IFTYPE_UNSPECIFIED;
960 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
961 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
962 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
963 mac->band.bitrates = mac->rates;
964 mac->band.n_channels = ARRAY_SIZE(zd_channels);
965 mac->band.channels = mac->channels;
967 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
969 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
970 IEEE80211_HW_SIGNAL_DB;
972 hw->wiphy->interface_modes =
973 BIT(NL80211_IFTYPE_MESH_POINT) |
974 BIT(NL80211_IFTYPE_STATION) |
975 BIT(NL80211_IFTYPE_ADHOC);
977 hw->max_signal = 100;
979 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
981 skb_queue_head_init(&mac->ack_wait_queue);
983 zd_chip_init(&mac->chip, hw, intf);
984 housekeeping_init(mac);
985 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
986 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
987 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
988 INIT_WORK(&mac->process_intr, zd_process_intr);
990 SET_IEEE80211_DEV(hw, &intf->dev);
994 #define LINK_LED_WORK_DELAY HZ
996 static void link_led_handler(struct work_struct *work)
999 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1000 struct zd_chip *chip = &mac->chip;
1004 spin_lock_irq(&mac->lock);
1005 is_associated = mac->associated;
1006 spin_unlock_irq(&mac->lock);
1008 r = zd_chip_control_leds(chip,
1009 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1011 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1013 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1014 LINK_LED_WORK_DELAY);
1017 static void housekeeping_init(struct zd_mac *mac)
1019 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1022 static void housekeeping_enable(struct zd_mac *mac)
1024 dev_dbg_f(zd_mac_dev(mac), "\n");
1025 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1029 static void housekeeping_disable(struct zd_mac *mac)
1031 dev_dbg_f(zd_mac_dev(mac), "\n");
1032 cancel_rearming_delayed_workqueue(zd_workqueue,
1033 &mac->housekeeping.link_led_work);
1034 zd_chip_control_leds(&mac->chip, LED_OFF);