Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zd_mac.c
Go to the documentation of this file.
1 /* ZD1211 USB-WLAN driver for Linux
2  *
3  * Copyright (C) 2005-2007 Ulrich Kunitz <[email protected]>
4  * Copyright (C) 2006-2007 Daniel Drake <[email protected]>
5  * Copyright (C) 2006-2007 Michael Wu <[email protected]>
6  * Copyright (C) 2007-2008 Luis R. Rodriguez <[email protected]>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22 
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27 #include <linux/jiffies.h>
28 #include <net/ieee80211_radiotap.h>
29 
30 #include "zd_def.h"
31 #include "zd_chip.h"
32 #include "zd_mac.h"
33 #include "zd_rf.h"
34 
37  char alpha2[2];
38 };
39 
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" },
49 };
50 
51 /* This table contains the hardware specific values for the modulation rates. */
52 static const struct ieee80211_rate zd_rates[] = {
53  { .bitrate = 10,
54  .hw_value = ZD_CCK_RATE_1M, },
55  { .bitrate = 20,
56  .hw_value = ZD_CCK_RATE_2M,
57  .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
59  { .bitrate = 55,
60  .hw_value = ZD_CCK_RATE_5_5M,
61  .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
63  { .bitrate = 110,
64  .hw_value = ZD_CCK_RATE_11M,
65  .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
67  { .bitrate = 60,
68  .hw_value = ZD_OFDM_RATE_6M,
69  .flags = 0 },
70  { .bitrate = 90,
71  .hw_value = ZD_OFDM_RATE_9M,
72  .flags = 0 },
73  { .bitrate = 120,
74  .hw_value = ZD_OFDM_RATE_12M,
75  .flags = 0 },
76  { .bitrate = 180,
77  .hw_value = ZD_OFDM_RATE_18M,
78  .flags = 0 },
79  { .bitrate = 240,
80  .hw_value = ZD_OFDM_RATE_24M,
81  .flags = 0 },
82  { .bitrate = 360,
83  .hw_value = ZD_OFDM_RATE_36M,
84  .flags = 0 },
85  { .bitrate = 480,
86  .hw_value = ZD_OFDM_RATE_48M,
87  .flags = 0 },
88  { .bitrate = 540,
89  .hw_value = ZD_OFDM_RATE_54M,
90  .flags = 0 },
91 };
92 
93 /*
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 :
97  *
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)
104  */
105 
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 }}
119 };
120 
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 },
136 };
137 
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);
144 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
145 static int zd_mac_config_beacon(struct ieee80211_hw *hw,
146  struct sk_buff *beacon, bool in_intr);
147 
148 static int zd_reg2alpha2(u8 regdomain, char *alpha2)
149 {
150  unsigned int i;
151  struct zd_reg_alpha2_map *reg_map;
152  for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
153  reg_map = &reg_alpha2_map[i];
154  if (regdomain == reg_map->reg) {
155  alpha2[0] = reg_map->alpha2[0];
156  alpha2[1] = reg_map->alpha2[1];
157  return 0;
158  }
159  }
160  return 1;
161 }
162 
163 static int zd_check_signal(struct ieee80211_hw *hw, int signal)
164 {
165  struct zd_mac *mac = zd_hw_mac(hw);
166 
167  dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100,
168  "%s: signal value from device not in range 0..100, "
169  "but %d.\n", __func__, signal);
170 
171  if (signal < 0)
172  signal = 0;
173  else if (signal > 100)
174  signal = 100;
175 
176  return signal;
177 }
178 
180 {
181  int r;
182  u8 addr[ETH_ALEN];
183  struct zd_mac *mac = zd_hw_mac(hw);
184 
185  r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
186  if (r)
187  return r;
188 
189  SET_IEEE80211_PERM_ADDR(hw, addr);
190 
191  return 0;
192 }
193 
195 {
196  int r;
197  struct zd_mac *mac = zd_hw_mac(hw);
198  struct zd_chip *chip = &mac->chip;
199  char alpha2[2];
200  u8 default_regdomain;
201 
202  r = zd_chip_enable_int(chip);
203  if (r)
204  goto out;
205  r = zd_chip_init_hw(chip);
206  if (r)
207  goto disable_int;
208 
210 
211  r = zd_read_regdomain(chip, &default_regdomain);
212  if (r)
213  goto disable_int;
214  spin_lock_irq(&mac->lock);
215  mac->regdomain = mac->default_regdomain = default_regdomain;
216  spin_unlock_irq(&mac->lock);
217 
218  /* We must inform the device that we are doing encryption/decryption in
219  * software at the moment. */
220  r = zd_set_encryption_type(chip, ENC_SNIFFER);
221  if (r)
222  goto disable_int;
223 
224  r = zd_reg2alpha2(mac->regdomain, alpha2);
225  if (r)
226  goto disable_int;
227 
228  r = regulatory_hint(hw->wiphy, alpha2);
229 disable_int:
230  zd_chip_disable_int(chip);
231 out:
232  return r;
233 }
234 
235 void zd_mac_clear(struct zd_mac *mac)
236 {
238  zd_chip_clear(&mac->chip);
239  ZD_ASSERT(!spin_is_locked(&mac->lock));
240  ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
241 }
242 
243 static int set_rx_filter(struct zd_mac *mac)
244 {
245  unsigned long flags;
247 
248  spin_lock_irqsave(&mac->lock, flags);
249  if (mac->pass_ctrl)
250  filter |= RX_FILTER_CTRL;
251  spin_unlock_irqrestore(&mac->lock, flags);
252 
253  return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
254 }
255 
256 static int set_mac_and_bssid(struct zd_mac *mac)
257 {
258  int r;
259 
260  if (!mac->vif)
261  return -1;
262 
263  r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
264  if (r)
265  return r;
266 
267  /* Vendor driver after setting MAC either sets BSSID for AP or
268  * filter for other modes.
269  */
270  if (mac->type != NL80211_IFTYPE_AP)
271  return set_rx_filter(mac);
272  else
273  return zd_write_bssid(&mac->chip, mac->vif->addr);
274 }
275 
276 static int set_mc_hash(struct zd_mac *mac)
277 {
278  struct zd_mc_hash hash;
279  zd_mc_clear(&hash);
280  return zd_chip_set_multicast_hash(&mac->chip, &hash);
281 }
282 
283 int zd_op_start(struct ieee80211_hw *hw)
284 {
285  struct zd_mac *mac = zd_hw_mac(hw);
286  struct zd_chip *chip = &mac->chip;
287  struct zd_usb *usb = &chip->usb;
288  int r;
289 
290  if (!usb->initialized) {
291  r = zd_usb_init_hw(usb);
292  if (r)
293  goto out;
294  }
295 
296  r = zd_chip_enable_int(chip);
297  if (r < 0)
298  goto out;
299 
301  if (r < 0)
302  goto disable_int;
303  r = set_rx_filter(mac);
304  if (r)
305  goto disable_int;
306  r = set_mc_hash(mac);
307  if (r)
308  goto disable_int;
309 
310  /* Wait after setting the multicast hash table and powering on
311  * the radio otherwise interface bring up will fail. This matches
312  * what the vendor driver did.
313  */
314  msleep(10);
315 
316  r = zd_chip_switch_radio_on(chip);
317  if (r < 0) {
318  dev_err(zd_chip_dev(chip),
319  "%s: failed to set radio on\n", __func__);
320  goto disable_int;
321  }
322  r = zd_chip_enable_rxtx(chip);
323  if (r < 0)
324  goto disable_radio;
325  r = zd_chip_enable_hwint(chip);
326  if (r < 0)
327  goto disable_rxtx;
328 
329  housekeeping_enable(mac);
330  beacon_enable(mac);
332  return 0;
333 disable_rxtx:
334  zd_chip_disable_rxtx(chip);
335 disable_radio:
337 disable_int:
338  zd_chip_disable_int(chip);
339 out:
340  return r;
341 }
342 
343 void zd_op_stop(struct ieee80211_hw *hw)
344 {
345  struct zd_mac *mac = zd_hw_mac(hw);
346  struct zd_chip *chip = &mac->chip;
347  struct sk_buff *skb;
348  struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
349 
351 
352  /* The order here deliberately is a little different from the open()
353  * method, since we need to make sure there is no opportunity for RX
354  * frames to be processed by mac80211 after we have stopped it.
355  */
356 
357  zd_chip_disable_rxtx(chip);
358  beacon_disable(mac);
359  housekeeping_disable(mac);
361 
362  zd_chip_disable_hwint(chip);
364  zd_chip_disable_int(chip);
365 
366 
367  while ((skb = skb_dequeue(ack_wait_queue)))
368  dev_kfree_skb_any(skb);
369 }
370 
371 int zd_restore_settings(struct zd_mac *mac)
372 {
373  struct sk_buff *beacon;
374  struct zd_mc_hash multicast_hash;
375  unsigned int short_preamble;
376  int r, beacon_interval, beacon_period;
377  u8 channel;
378 
379  dev_dbg_f(zd_mac_dev(mac), "\n");
380 
381  spin_lock_irq(&mac->lock);
382  multicast_hash = mac->multicast_hash;
383  short_preamble = mac->short_preamble;
384  beacon_interval = mac->beacon.interval;
385  beacon_period = mac->beacon.period;
386  channel = mac->channel;
387  spin_unlock_irq(&mac->lock);
388 
389  r = set_mac_and_bssid(mac);
390  if (r < 0) {
391  dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
392  return r;
393  }
394 
395  r = zd_chip_set_channel(&mac->chip, channel);
396  if (r < 0) {
397  dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
398  r);
399  return r;
400  }
401 
402  set_rts_cts(mac, short_preamble);
403 
404  r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
405  if (r < 0) {
406  dev_dbg_f(zd_mac_dev(mac),
407  "zd_chip_set_multicast_hash failed, %d\n", r);
408  return r;
409  }
410 
411  if (mac->type == NL80211_IFTYPE_MESH_POINT ||
412  mac->type == NL80211_IFTYPE_ADHOC ||
413  mac->type == NL80211_IFTYPE_AP) {
414  if (mac->vif != NULL) {
415  beacon = ieee80211_beacon_get(mac->hw, mac->vif);
416  if (beacon)
417  zd_mac_config_beacon(mac->hw, beacon, false);
418  }
419 
420  zd_set_beacon_interval(&mac->chip, beacon_interval,
421  beacon_period, mac->type);
422 
423  spin_lock_irq(&mac->lock);
424  mac->beacon.last_update = jiffies;
425  spin_unlock_irq(&mac->lock);
426  }
427 
428  return 0;
429 }
430 
445 static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
446  int ackssi, struct tx_status *tx_status)
447 {
448  struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
449  int i;
450  int success = 1, retry = 1;
451  int first_idx;
452  const struct tx_retry_rate *retries;
453 
454  ieee80211_tx_info_clear_status(info);
455 
456  if (tx_status) {
457  success = !tx_status->failure;
458  retry = tx_status->retry + success;
459  }
460 
461  if (success) {
462  /* success */
463  info->flags |= IEEE80211_TX_STAT_ACK;
464  } else {
465  /* failure */
466  info->flags &= ~IEEE80211_TX_STAT_ACK;
467  }
468 
469  first_idx = info->status.rates[0].idx;
470  ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
471  retries = &zd_retry_rates[first_idx];
472  ZD_ASSERT(1 <= retry && retry <= retries->count);
473 
474  info->status.rates[0].idx = retries->rate[0];
475  info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
476 
477  for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
478  info->status.rates[i].idx = retries->rate[i];
479  info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
480  }
481  for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
482  info->status.rates[i].idx = retries->rate[retry - 1];
483  info->status.rates[i].count = 1; // (success ? 1:2);
484  }
486  info->status.rates[i].idx = -1; /* terminate */
487 
488  info->status.ack_signal = zd_check_signal(hw, ackssi);
490 }
491 
500 void zd_mac_tx_failed(struct urb *urb)
501 {
502  struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
503  struct zd_mac *mac = zd_hw_mac(hw);
504  struct sk_buff_head *q = &mac->ack_wait_queue;
505  struct sk_buff *skb;
506  struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
507  unsigned long flags;
508  int success = !tx_status->failure;
509  int retry = tx_status->retry + success;
510  int found = 0;
511  int i, position = 0;
512 
513  q = &mac->ack_wait_queue;
514  spin_lock_irqsave(&q->lock, flags);
515 
516  skb_queue_walk(q, skb) {
517  struct ieee80211_hdr *tx_hdr;
518  struct ieee80211_tx_info *info;
519  int first_idx, final_idx;
520  const struct tx_retry_rate *retries;
521  u8 final_rate;
522 
523  position ++;
524 
525  /* if the hardware reports a failure and we had a 802.11 ACK
526  * pending, then we skip the first skb when searching for a
527  * matching frame */
528  if (tx_status->failure && mac->ack_pending &&
529  skb_queue_is_first(q, skb)) {
530  continue;
531  }
532 
533  tx_hdr = (struct ieee80211_hdr *)skb->data;
534 
535  /* we skip all frames not matching the reported destination */
536  if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
537  continue;
538  }
539 
540  /* we skip all frames not matching the reported final rate */
541 
542  info = IEEE80211_SKB_CB(skb);
543  first_idx = info->status.rates[0].idx;
544  ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
545  retries = &zd_retry_rates[first_idx];
546  if (retry <= 0 || retry > retries->count)
547  continue;
548 
549  final_idx = retries->rate[retry - 1];
550  final_rate = zd_rates[final_idx].hw_value;
551 
552  if (final_rate != tx_status->rate) {
553  continue;
554  }
555 
556  found = 1;
557  break;
558  }
559 
560  if (found) {
561  for (i=1; i<=position; i++) {
562  skb = __skb_dequeue(q);
563  zd_mac_tx_status(hw, skb,
564  mac->ack_pending ? mac->ack_signal : 0,
565  i == position ? tx_status : NULL);
566  mac->ack_pending = 0;
567  }
568  }
569 
570  spin_unlock_irqrestore(&q->lock, flags);
571 }
572 
583 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
584 {
585  struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
586  struct ieee80211_hw *hw = info->rate_driver_data[0];
587  struct zd_mac *mac = zd_hw_mac(hw);
588 
589  ieee80211_tx_info_clear_status(info);
590 
591  skb_pull(skb, sizeof(struct zd_ctrlset));
592  if (unlikely(error ||
593  (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
594  /*
595  * FIXME : do we need to fill in anything ?
596  */
598  } else {
599  struct sk_buff_head *q = &mac->ack_wait_queue;
600 
601  skb_queue_tail(q, skb);
602  while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
603  zd_mac_tx_status(hw, skb_dequeue(q),
604  mac->ack_pending ? mac->ack_signal : 0,
605  NULL);
606  mac->ack_pending = 0;
607  }
608  }
609 }
610 
611 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
612 {
613  /* ZD_PURE_RATE() must be used to remove the modulation type flag of
614  * the zd-rate values.
615  */
616  static const u8 rate_divisor[] = {
619  /* Bits must be doubled. */
630  };
631 
632  u32 bits = (u32)tx_length * 8;
633  u32 divisor;
634 
635  divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
636  if (divisor == 0)
637  return -EINVAL;
638 
639  switch (zd_rate) {
640  case ZD_CCK_RATE_5_5M:
641  bits = (2*bits) + 10; /* round up to the next integer */
642  break;
643  case ZD_CCK_RATE_11M:
644  if (service) {
645  u32 t = bits % 11;
647  if (0 < t && t <= 3) {
649  }
650  }
651  bits += 10; /* round up to the next integer */
652  break;
653  }
654 
655  return bits/divisor;
656 }
657 
658 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
659  struct ieee80211_hdr *header,
660  struct ieee80211_tx_info *info)
661 {
662  /*
663  * CONTROL TODO:
664  * - if backoff needed, enable bit 0
665  * - if burst (backoff not needed) disable bit 0
666  */
667 
668  cs->control = 0;
669 
670  /* First fragment */
673 
674  /* No ACK expected (multicast, etc.) */
675  if (info->flags & IEEE80211_TX_CTL_NO_ACK)
676  cs->control |= ZD_CS_NO_ACK;
677 
678  /* PS-POLL */
679  if (ieee80211_is_pspoll(header->frame_control))
681 
682  if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
683  cs->control |= ZD_CS_RTS;
684 
685  if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
686  cs->control |= ZD_CS_SELF_CTS;
687 
688  /* FIXME: Management frame? */
689 }
690 
691 static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
692 {
693  if (!mac->beacon.cur_beacon)
694  return false;
695 
696  if (mac->beacon.cur_beacon->len != beacon->len)
697  return false;
698 
699  return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
700 }
701 
702 static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
703 {
704  ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
705 
706  kfree_skb(mac->beacon.cur_beacon);
707  mac->beacon.cur_beacon = NULL;
708 }
709 
710 static void zd_mac_free_cur_beacon(struct zd_mac *mac)
711 {
712  mutex_lock(&mac->chip.mutex);
713  zd_mac_free_cur_beacon_locked(mac);
714  mutex_unlock(&mac->chip.mutex);
715 }
716 
717 static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
718  bool in_intr)
719 {
720  struct zd_mac *mac = zd_hw_mac(hw);
721  int r, ret, num_cmds, req_pos = 0;
722  u32 tmp, j = 0;
723  /* 4 more bytes for tail CRC */
724  u32 full_len = beacon->len + 4;
725  unsigned long end_jiffies, message_jiffies;
726  struct zd_ioreq32 *ioreqs;
727 
728  mutex_lock(&mac->chip.mutex);
729 
730  /* Check if hw already has this beacon. */
731  if (zd_mac_match_cur_beacon(mac, beacon)) {
732  r = 0;
733  goto out_nofree;
734  }
735 
736  /* Alloc memory for full beacon write at once. */
737  num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
738  ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
739  if (!ioreqs) {
740  r = -ENOMEM;
741  goto out_nofree;
742  }
743 
744  r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
745  if (r < 0)
746  goto out;
747  r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
748  if (r < 0)
749  goto release_sema;
750  if (in_intr && tmp & 0x2) {
751  r = -EBUSY;
752  goto release_sema;
753  }
754 
755  end_jiffies = jiffies + HZ / 2; /*~500ms*/
756  message_jiffies = jiffies + HZ / 10; /*~100ms*/
757  while (tmp & 0x2) {
758  r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
759  if (r < 0)
760  goto release_sema;
761  if (time_is_before_eq_jiffies(message_jiffies)) {
762  message_jiffies = jiffies + HZ / 10;
763  dev_err(zd_mac_dev(mac),
764  "CR_BCN_FIFO_SEMAPHORE not ready\n");
765  if (time_is_before_eq_jiffies(end_jiffies)) {
766  dev_err(zd_mac_dev(mac),
767  "Giving up beacon config.\n");
768  r = -ETIMEDOUT;
769  goto reset_device;
770  }
771  }
772  msleep(20);
773  }
774 
775  ioreqs[req_pos].addr = CR_BCN_FIFO;
776  ioreqs[req_pos].value = full_len - 1;
777  req_pos++;
778  if (zd_chip_is_zd1211b(&mac->chip)) {
779  ioreqs[req_pos].addr = CR_BCN_LENGTH;
780  ioreqs[req_pos].value = full_len - 1;
781  req_pos++;
782  }
783 
784  for (j = 0 ; j < beacon->len; j++) {
785  ioreqs[req_pos].addr = CR_BCN_FIFO;
786  ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
787  req_pos++;
788  }
789 
790  for (j = 0; j < 4; j++) {
791  ioreqs[req_pos].addr = CR_BCN_FIFO;
792  ioreqs[req_pos].value = 0x0;
793  req_pos++;
794  }
795 
796  BUG_ON(req_pos != num_cmds);
797 
798  r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
799 
800 release_sema:
801  /*
802  * Try very hard to release device beacon semaphore, as otherwise
803  * device/driver can be left in unusable state.
804  */
805  end_jiffies = jiffies + HZ / 2; /*~500ms*/
806  ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
807  while (ret < 0) {
808  if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
809  ret = -ETIMEDOUT;
810  break;
811  }
812 
813  msleep(20);
814  ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
815  }
816 
817  if (ret < 0)
818  dev_err(zd_mac_dev(mac), "Could not release "
819  "CR_BCN_FIFO_SEMAPHORE!\n");
820  if (r < 0 || ret < 0) {
821  if (r >= 0)
822  r = ret;
823 
824  /* We don't know if beacon was written successfully or not,
825  * so clear current. */
826  zd_mac_free_cur_beacon_locked(mac);
827 
828  goto out;
829  }
830 
831  /* Beacon has now been written successfully, update current. */
832  zd_mac_free_cur_beacon_locked(mac);
833  mac->beacon.cur_beacon = beacon;
834  beacon = NULL;
835 
836  /* 802.11b/g 2.4G CCK 1Mb
837  * 802.11a, not yet implemented, uses different values (see GPL vendor
838  * driver)
839  */
840  r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
842 out:
843  kfree(ioreqs);
844 out_nofree:
845  kfree_skb(beacon);
846  mutex_unlock(&mac->chip.mutex);
847 
848  return r;
849 
850 reset_device:
851  zd_mac_free_cur_beacon_locked(mac);
852  kfree_skb(beacon);
853 
854  mutex_unlock(&mac->chip.mutex);
855  kfree(ioreqs);
856 
857  /* semaphore stuck, reset device to avoid fw freeze later */
858  dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
859  "resetting device...");
860  usb_queue_reset_device(mac->chip.usb.intf);
861 
862  return r;
863 }
864 
865 static int fill_ctrlset(struct zd_mac *mac,
866  struct sk_buff *skb)
867 {
868  int r;
869  struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
870  unsigned int frag_len = skb->len + FCS_LEN;
871  unsigned int packet_length;
872  struct ieee80211_rate *txrate;
873  struct zd_ctrlset *cs = (struct zd_ctrlset *)
874  skb_push(skb, sizeof(struct zd_ctrlset));
875  struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
876 
877  ZD_ASSERT(frag_len <= 0xffff);
878 
879  /*
880  * Firmware computes the duration itself (for all frames except PSPoll)
881  * and needs the field set to 0 at input, otherwise firmware messes up
882  * duration_id and sets bits 14 and 15 on.
883  */
884  if (!ieee80211_is_pspoll(hdr->frame_control))
885  hdr->duration_id = 0;
886 
887  txrate = ieee80211_get_tx_rate(mac->hw, info);
888 
889  cs->modulation = txrate->hw_value;
890  if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
891  cs->modulation = txrate->hw_value_short;
892 
893  cs->tx_length = cpu_to_le16(frag_len);
894 
895  cs_set_control(mac, cs, hdr, info);
896 
897  packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
898  ZD_ASSERT(packet_length <= 0xffff);
899  /* ZD1211B: Computing the length difference this way, gives us
900  * flexibility to compute the packet length.
901  */
902  cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
903  packet_length - frag_len : packet_length);
904 
905  /*
906  * CURRENT LENGTH:
907  * - transmit frame length in microseconds
908  * - seems to be derived from frame length
909  * - see Cal_Us_Service() in zdinlinef.h
910  * - if macp->bTxBurstEnable is enabled, then multiply by 4
911  * - bTxBurstEnable is never set in the vendor driver
912  *
913  * SERVICE:
914  * - "for PLCP configuration"
915  * - always 0 except in some situations at 802.11b 11M
916  * - see line 53 of zdinlinef.h
917  */
918  cs->service = 0;
919  r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
920  le16_to_cpu(cs->tx_length));
921  if (r < 0)
922  return r;
923  cs->current_length = cpu_to_le16(r);
924  cs->next_frame_length = 0;
925 
926  return 0;
927 }
928 
940 static void zd_op_tx(struct ieee80211_hw *hw,
942  struct sk_buff *skb)
943 {
944  struct zd_mac *mac = zd_hw_mac(hw);
945  struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
946  int r;
947 
948  r = fill_ctrlset(mac, skb);
949  if (r)
950  goto fail;
951 
952  info->rate_driver_data[0] = hw;
953 
954  r = zd_usb_tx(&mac->chip.usb, skb);
955  if (r)
956  goto fail;
957  return;
958 
959 fail:
960  dev_kfree_skb(skb);
961 }
962 
977 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
978  struct ieee80211_rx_status *stats)
979 {
980  struct zd_mac *mac = zd_hw_mac(hw);
981  struct sk_buff *skb;
982  struct sk_buff_head *q;
983  unsigned long flags;
984  int found = 0;
985  int i, position = 0;
986 
987  if (!ieee80211_is_ack(rx_hdr->frame_control))
988  return 0;
989 
990  q = &mac->ack_wait_queue;
991  spin_lock_irqsave(&q->lock, flags);
992  skb_queue_walk(q, skb) {
993  struct ieee80211_hdr *tx_hdr;
994 
995  position ++;
996 
997  if (mac->ack_pending && skb_queue_is_first(q, skb))
998  continue;
999 
1000  tx_hdr = (struct ieee80211_hdr *)skb->data;
1001  if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
1002  {
1003  found = 1;
1004  break;
1005  }
1006  }
1007 
1008  if (found) {
1009  for (i=1; i<position; i++) {
1010  skb = __skb_dequeue(q);
1011  zd_mac_tx_status(hw, skb,
1012  mac->ack_pending ? mac->ack_signal : 0,
1013  NULL);
1014  mac->ack_pending = 0;
1015  }
1016 
1017  mac->ack_pending = 1;
1018  mac->ack_signal = stats->signal;
1019 
1020  /* Prevent pending tx-packet on AP-mode */
1021  if (mac->type == NL80211_IFTYPE_AP) {
1022  skb = __skb_dequeue(q);
1023  zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
1024  mac->ack_pending = 0;
1025  }
1026  }
1027 
1028  spin_unlock_irqrestore(&q->lock, flags);
1029  return 1;
1030 }
1031 
1032 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
1033 {
1034  struct zd_mac *mac = zd_hw_mac(hw);
1035  struct ieee80211_rx_status stats;
1036  const struct rx_status *status;
1037  struct sk_buff *skb;
1038  int bad_frame = 0;
1039  __le16 fc;
1040  int need_padding;
1041  int i;
1042  u8 rate;
1043 
1044  if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
1045  FCS_LEN + sizeof(struct rx_status))
1046  return -EINVAL;
1047 
1048  memset(&stats, 0, sizeof(stats));
1049 
1050  /* Note about pass_failed_fcs and pass_ctrl access below:
1051  * mac locking intentionally omitted here, as this is the only unlocked
1052  * reader and the only writer is configure_filter. Plus, if there were
1053  * any races accessing these variables, it wouldn't really matter.
1054  * If mac80211 ever provides a way for us to access filter flags
1055  * from outside configure_filter, we could improve on this. Also, this
1056  * situation may change once we implement some kind of DMA-into-skb
1057  * RX path. */
1058 
1059  /* Caller has to ensure that length >= sizeof(struct rx_status). */
1060  status = (struct rx_status *)
1061  (buffer + (length - sizeof(struct rx_status)));
1062  if (status->frame_status & ZD_RX_ERROR) {
1063  if (mac->pass_failed_fcs &&
1064  (status->frame_status & ZD_RX_CRC32_ERROR)) {
1065  stats.flag |= RX_FLAG_FAILED_FCS_CRC;
1066  bad_frame = 1;
1067  } else {
1068  return -EINVAL;
1069  }
1070  }
1071 
1072  stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
1073  stats.band = IEEE80211_BAND_2GHZ;
1074  stats.signal = zd_check_signal(hw, status->signal_strength);
1075 
1076  rate = zd_rx_rate(buffer, status);
1077 
1078  /* todo: return index in the big switches in zd_rx_rate instead */
1079  for (i = 0; i < mac->band.n_bitrates; i++)
1080  if (rate == mac->band.bitrates[i].hw_value)
1081  stats.rate_idx = i;
1082 
1083  length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
1084  buffer += ZD_PLCP_HEADER_SIZE;
1085 
1086  /* Except for bad frames, filter each frame to see if it is an ACK, in
1087  * which case our internal TX tracking is updated. Normally we then
1088  * bail here as there's no need to pass ACKs on up to the stack, but
1089  * there is also the case where the stack has requested us to pass
1090  * control frames on up (pass_ctrl) which we must consider. */
1091  if (!bad_frame &&
1092  filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
1093  && !mac->pass_ctrl)
1094  return 0;
1095 
1096  fc = get_unaligned((__le16*)buffer);
1097  need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
1098 
1099  skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
1100  if (skb == NULL)
1101  return -ENOMEM;
1102  if (need_padding) {
1103  /* Make sure the payload data is 4 byte aligned. */
1104  skb_reserve(skb, 2);
1105  }
1106 
1107  /* FIXME : could we avoid this big memcpy ? */
1108  memcpy(skb_put(skb, length), buffer, length);
1109 
1110  memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
1111  ieee80211_rx_irqsafe(hw, skb);
1112  return 0;
1113 }
1114 
1115 static int zd_op_add_interface(struct ieee80211_hw *hw,
1116  struct ieee80211_vif *vif)
1117 {
1118  struct zd_mac *mac = zd_hw_mac(hw);
1119 
1120  /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
1121  if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
1122  return -EOPNOTSUPP;
1123 
1124  switch (vif->type) {
1128  case NL80211_IFTYPE_ADHOC:
1129  case NL80211_IFTYPE_AP:
1130  mac->type = vif->type;
1131  break;
1132  default:
1133  return -EOPNOTSUPP;
1134  }
1135 
1136  mac->vif = vif;
1137 
1138  return set_mac_and_bssid(mac);
1139 }
1140 
1141 static void zd_op_remove_interface(struct ieee80211_hw *hw,
1142  struct ieee80211_vif *vif)
1143 {
1144  struct zd_mac *mac = zd_hw_mac(hw);
1146  mac->vif = NULL;
1148  zd_write_mac_addr(&mac->chip, NULL);
1149 
1150  zd_mac_free_cur_beacon(mac);
1151 }
1152 
1153 static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
1154 {
1155  struct zd_mac *mac = zd_hw_mac(hw);
1156  struct ieee80211_conf *conf = &hw->conf;
1157 
1158  spin_lock_irq(&mac->lock);
1159  mac->channel = conf->channel->hw_value;
1160  spin_unlock_irq(&mac->lock);
1161 
1162  return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
1163 }
1164 
1165 static void zd_beacon_done(struct zd_mac *mac)
1166 {
1167  struct sk_buff *skb, *beacon;
1168 
1169  if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1170  return;
1171  if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1172  return;
1173 
1174  /*
1175  * Send out buffered broad- and multicast frames.
1176  */
1177  while (!ieee80211_queue_stopped(mac->hw, 0)) {
1178  skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1179  if (!skb)
1180  break;
1181  zd_op_tx(mac->hw, NULL, skb);
1182  }
1183 
1184  /*
1185  * Fetch next beacon so that tim_count is updated.
1186  */
1187  beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1188  if (beacon)
1189  zd_mac_config_beacon(mac->hw, beacon, true);
1190 
1191  spin_lock_irq(&mac->lock);
1192  mac->beacon.last_update = jiffies;
1193  spin_unlock_irq(&mac->lock);
1194 }
1195 
1196 static void zd_process_intr(struct work_struct *work)
1197 {
1198  u16 int_status;
1199  unsigned long flags;
1200  struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1201 
1202  spin_lock_irqsave(&mac->lock, flags);
1203  int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1204  spin_unlock_irqrestore(&mac->lock, flags);
1205 
1206  if (int_status & INT_CFG_NEXT_BCN) {
1207  /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1208  zd_beacon_done(mac);
1209  } else {
1210  dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
1211  }
1212 
1213  zd_chip_enable_hwint(&mac->chip);
1214 }
1215 
1216 
1217 static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
1218  struct netdev_hw_addr_list *mc_list)
1219 {
1220  struct zd_mac *mac = zd_hw_mac(hw);
1221  struct zd_mc_hash hash;
1222  struct netdev_hw_addr *ha;
1223 
1224  zd_mc_clear(&hash);
1225 
1226  netdev_hw_addr_list_for_each(ha, mc_list) {
1227  dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1228  zd_mc_add_addr(&hash, ha->addr);
1229  }
1230 
1231  return hash.low | ((u64)hash.high << 32);
1232 }
1233 
1234 #define SUPPORTED_FIF_FLAGS \
1235  (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1236  FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1237 static void zd_op_configure_filter(struct ieee80211_hw *hw,
1238  unsigned int changed_flags,
1239  unsigned int *new_flags,
1240  u64 multicast)
1241 {
1242  struct zd_mc_hash hash = {
1243  .low = multicast,
1244  .high = multicast >> 32,
1245  };
1246  struct zd_mac *mac = zd_hw_mac(hw);
1247  unsigned long flags;
1248  int r;
1249 
1250  /* Only deal with supported flags */
1251  changed_flags &= SUPPORTED_FIF_FLAGS;
1252  *new_flags &= SUPPORTED_FIF_FLAGS;
1253 
1254  /*
1255  * If multicast parameter (as returned by zd_op_prepare_multicast)
1256  * has changed, no bit in changed_flags is set. To handle this
1257  * situation, we do not return if changed_flags is 0. If we do so,
1258  * we will have some issue with IPv6 which uses multicast for link
1259  * layer address resolution.
1260  */
1261  if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
1262  zd_mc_add_all(&hash);
1263 
1264  spin_lock_irqsave(&mac->lock, flags);
1265  mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1266  mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1267  mac->multicast_hash = hash;
1268  spin_unlock_irqrestore(&mac->lock, flags);
1269 
1270  zd_chip_set_multicast_hash(&mac->chip, &hash);
1271 
1272  if (changed_flags & FIF_CONTROL) {
1273  r = set_rx_filter(mac);
1274  if (r)
1275  dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1276  }
1277 
1278  /* no handling required for FIF_OTHER_BSS as we don't currently
1279  * do BSSID filtering */
1280  /* FIXME: in future it would be nice to enable the probe response
1281  * filter (so that the driver doesn't see them) until
1282  * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1283  * have to schedule work to enable prbresp reception, which might
1284  * happen too late. For now we'll just listen and forward them all the
1285  * time. */
1286 }
1287 
1288 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
1289 {
1290  mutex_lock(&mac->chip.mutex);
1291  zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1292  mutex_unlock(&mac->chip.mutex);
1293 }
1294 
1295 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1296  struct ieee80211_vif *vif,
1297  struct ieee80211_bss_conf *bss_conf,
1298  u32 changes)
1299 {
1300  struct zd_mac *mac = zd_hw_mac(hw);
1301  int associated;
1302 
1303  dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1304 
1305  if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1306  mac->type == NL80211_IFTYPE_ADHOC ||
1307  mac->type == NL80211_IFTYPE_AP) {
1308  associated = true;
1309  if (changes & BSS_CHANGED_BEACON) {
1310  struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1311 
1312  if (beacon) {
1313  zd_chip_disable_hwint(&mac->chip);
1314  zd_mac_config_beacon(hw, beacon, false);
1315  zd_chip_enable_hwint(&mac->chip);
1316  }
1317  }
1318 
1319  if (changes & BSS_CHANGED_BEACON_ENABLED) {
1320  u16 interval = 0;
1321  u8 period = 0;
1322 
1323  if (bss_conf->enable_beacon) {
1324  period = bss_conf->dtim_period;
1325  interval = bss_conf->beacon_int;
1326  }
1327 
1328  spin_lock_irq(&mac->lock);
1329  mac->beacon.period = period;
1330  mac->beacon.interval = interval;
1331  mac->beacon.last_update = jiffies;
1332  spin_unlock_irq(&mac->lock);
1333 
1334  zd_set_beacon_interval(&mac->chip, interval, period,
1335  mac->type);
1336  }
1337  } else
1338  associated = is_valid_ether_addr(bss_conf->bssid);
1339 
1340  spin_lock_irq(&mac->lock);
1341  mac->associated = associated;
1342  spin_unlock_irq(&mac->lock);
1343 
1344  /* TODO: do hardware bssid filtering */
1345 
1346  if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1347  spin_lock_irq(&mac->lock);
1348  mac->short_preamble = bss_conf->use_short_preamble;
1349  spin_unlock_irq(&mac->lock);
1350 
1351  set_rts_cts(mac, bss_conf->use_short_preamble);
1352  }
1353 }
1354 
1355 static u64 zd_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1356 {
1357  struct zd_mac *mac = zd_hw_mac(hw);
1358  return zd_chip_get_tsf(&mac->chip);
1359 }
1360 
1361 static const struct ieee80211_ops zd_ops = {
1362  .tx = zd_op_tx,
1363  .start = zd_op_start,
1364  .stop = zd_op_stop,
1365  .add_interface = zd_op_add_interface,
1366  .remove_interface = zd_op_remove_interface,
1367  .config = zd_op_config,
1368  .prepare_multicast = zd_op_prepare_multicast,
1369  .configure_filter = zd_op_configure_filter,
1370  .bss_info_changed = zd_op_bss_info_changed,
1371  .get_tsf = zd_op_get_tsf,
1372 };
1373 
1375 {
1376  struct zd_mac *mac;
1377  struct ieee80211_hw *hw;
1378 
1379  hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1380  if (!hw) {
1381  dev_dbg_f(&intf->dev, "out of memory\n");
1382  return NULL;
1383  }
1384 
1385  mac = zd_hw_mac(hw);
1386 
1387  memset(mac, 0, sizeof(*mac));
1388  spin_lock_init(&mac->lock);
1389  mac->hw = hw;
1390 
1392 
1393  memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1394  memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1395  mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1396  mac->band.bitrates = mac->rates;
1397  mac->band.n_channels = ARRAY_SIZE(zd_channels);
1398  mac->band.channels = mac->channels;
1399 
1400  hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1401 
1406 
1407  hw->wiphy->interface_modes =
1412 
1413  hw->max_signal = 100;
1414  hw->queues = 1;
1415  hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1416 
1417  /*
1418  * Tell mac80211 that we support multi rate retries
1419  */
1421  hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
1422 
1423  skb_queue_head_init(&mac->ack_wait_queue);
1424  mac->ack_pending = 0;
1425 
1426  zd_chip_init(&mac->chip, hw, intf);
1427  housekeeping_init(mac);
1428  beacon_init(mac);
1429  INIT_WORK(&mac->process_intr, zd_process_intr);
1430 
1431  SET_IEEE80211_DEV(hw, &intf->dev);
1432  return hw;
1433 }
1434 
1435 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1436 
1437 static void beacon_watchdog_handler(struct work_struct *work)
1438 {
1439  struct zd_mac *mac =
1440  container_of(work, struct zd_mac, beacon.watchdog_work.work);
1441  struct sk_buff *beacon;
1442  unsigned long timeout;
1443  int interval, period;
1444 
1445  if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1446  goto rearm;
1447  if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1448  goto rearm;
1449 
1450  spin_lock_irq(&mac->lock);
1451  interval = mac->beacon.interval;
1452  period = mac->beacon.period;
1453  timeout = mac->beacon.last_update +
1454  msecs_to_jiffies(interval * 1024 / 1000) * 3;
1455  spin_unlock_irq(&mac->lock);
1456 
1457  if (interval > 0 && time_is_before_jiffies(timeout)) {
1458  dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1459  "restarting. "
1460  "(interval: %d, dtim: %d)\n",
1461  interval, period);
1462 
1463  zd_chip_disable_hwint(&mac->chip);
1464 
1465  beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1466  if (beacon) {
1467  zd_mac_free_cur_beacon(mac);
1468 
1469  zd_mac_config_beacon(mac->hw, beacon, false);
1470  }
1471 
1472  zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1473 
1474  zd_chip_enable_hwint(&mac->chip);
1475 
1476  spin_lock_irq(&mac->lock);
1477  mac->beacon.last_update = jiffies;
1478  spin_unlock_irq(&mac->lock);
1479  }
1480 
1481 rearm:
1482  queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1484 }
1485 
1486 static void beacon_init(struct zd_mac *mac)
1487 {
1488  INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1489 }
1490 
1491 static void beacon_enable(struct zd_mac *mac)
1492 {
1493  dev_dbg_f(zd_mac_dev(mac), "\n");
1494 
1495  mac->beacon.last_update = jiffies;
1496  queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1498 }
1499 
1500 static void beacon_disable(struct zd_mac *mac)
1501 {
1502  dev_dbg_f(zd_mac_dev(mac), "\n");
1503  cancel_delayed_work_sync(&mac->beacon.watchdog_work);
1504 
1505  zd_mac_free_cur_beacon(mac);
1506 }
1507 
1508 #define LINK_LED_WORK_DELAY HZ
1509 
1510 static void link_led_handler(struct work_struct *work)
1511 {
1512  struct zd_mac *mac =
1513  container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1514  struct zd_chip *chip = &mac->chip;
1515  int is_associated;
1516  int r;
1517 
1518  if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1519  goto requeue;
1520 
1521  spin_lock_irq(&mac->lock);
1522  is_associated = mac->associated;
1523  spin_unlock_irq(&mac->lock);
1524 
1525  r = zd_chip_control_leds(chip,
1526  is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1527  if (r)
1528  dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1529 
1530 requeue:
1531  queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1533 }
1534 
1535 static void housekeeping_init(struct zd_mac *mac)
1536 {
1537  INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1538 }
1539 
1540 static void housekeeping_enable(struct zd_mac *mac)
1541 {
1542  dev_dbg_f(zd_mac_dev(mac), "\n");
1543  queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1544  0);
1545 }
1546 
1547 static void housekeeping_disable(struct zd_mac *mac)
1548 {
1549  dev_dbg_f(zd_mac_dev(mac), "\n");
1550  cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
1552 }