1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/slab.h>
25#include <linux/usb.h>
26#include <linux/jiffies.h>
27#include <net/ieee80211_radiotap.h>
28
29#include "zd_def.h"
30#include "zd_chip.h"
31#include "zd_mac.h"
32#include "zd_rf.h"
33
34struct zd_reg_alpha2_map {
35 u32 reg;
36 char alpha2[2];
37};
38
39static struct zd_reg_alpha2_map reg_alpha2_map[] = {
40 { ZD_REGDOMAIN_FCC, "US" },
41 { ZD_REGDOMAIN_IC, "CA" },
42 { ZD_REGDOMAIN_ETSI, "DE" },
43 { ZD_REGDOMAIN_JAPAN, "JP" },
44 { ZD_REGDOMAIN_JAPAN_2, "JP" },
45 { ZD_REGDOMAIN_JAPAN_3, "JP" },
46 { ZD_REGDOMAIN_SPAIN, "ES" },
47 { ZD_REGDOMAIN_FRANCE, "FR" },
48};
49
50
51static const struct ieee80211_rate zd_rates[] = {
52 { .bitrate = 10,
53 .hw_value = ZD_CCK_RATE_1M, },
54 { .bitrate = 20,
55 .hw_value = ZD_CCK_RATE_2M,
56 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
57 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
58 { .bitrate = 55,
59 .hw_value = ZD_CCK_RATE_5_5M,
60 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
61 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
62 { .bitrate = 110,
63 .hw_value = ZD_CCK_RATE_11M,
64 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
65 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
66 { .bitrate = 60,
67 .hw_value = ZD_OFDM_RATE_6M,
68 .flags = 0 },
69 { .bitrate = 90,
70 .hw_value = ZD_OFDM_RATE_9M,
71 .flags = 0 },
72 { .bitrate = 120,
73 .hw_value = ZD_OFDM_RATE_12M,
74 .flags = 0 },
75 { .bitrate = 180,
76 .hw_value = ZD_OFDM_RATE_18M,
77 .flags = 0 },
78 { .bitrate = 240,
79 .hw_value = ZD_OFDM_RATE_24M,
80 .flags = 0 },
81 { .bitrate = 360,
82 .hw_value = ZD_OFDM_RATE_36M,
83 .flags = 0 },
84 { .bitrate = 480,
85 .hw_value = ZD_OFDM_RATE_48M,
86 .flags = 0 },
87 { .bitrate = 540,
88 .hw_value = ZD_OFDM_RATE_54M,
89 .flags = 0 },
90};
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105static const struct tx_retry_rate zd_retry_rates[] = {
106 { 1, { 0 }},
107 { 2, { 1, 0 }},
108 { 3, { 2, 1, 0 }},
109 { 4, { 3, 2, 1, 0 }},
110 { 5, { 4, 3, 2, 1, 0 }},
111 { 6, { 5, 4, 3, 2, 1, 0}},
112 { 5, { 6, 3, 2, 1, 0 }},
113 { 6, { 7, 6, 3, 2, 1, 0 }},
114 { 6, { 8, 6, 3, 2, 1, 0 }},
115 { 7, { 9, 8, 6, 3, 2, 1, 0 }},
116 { 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
117 { 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
118};
119
120static const struct ieee80211_channel zd_channels[] = {
121 { .center_freq = 2412, .hw_value = 1 },
122 { .center_freq = 2417, .hw_value = 2 },
123 { .center_freq = 2422, .hw_value = 3 },
124 { .center_freq = 2427, .hw_value = 4 },
125 { .center_freq = 2432, .hw_value = 5 },
126 { .center_freq = 2437, .hw_value = 6 },
127 { .center_freq = 2442, .hw_value = 7 },
128 { .center_freq = 2447, .hw_value = 8 },
129 { .center_freq = 2452, .hw_value = 9 },
130 { .center_freq = 2457, .hw_value = 10 },
131 { .center_freq = 2462, .hw_value = 11 },
132 { .center_freq = 2467, .hw_value = 12 },
133 { .center_freq = 2472, .hw_value = 13 },
134 { .center_freq = 2484, .hw_value = 14 },
135};
136
137static void housekeeping_init(struct zd_mac *mac);
138static void housekeeping_enable(struct zd_mac *mac);
139static void housekeeping_disable(struct zd_mac *mac);
140static void beacon_init(struct zd_mac *mac);
141static void beacon_enable(struct zd_mac *mac);
142static void beacon_disable(struct zd_mac *mac);
143static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
144static int zd_mac_config_beacon(struct ieee80211_hw *hw,
145 struct sk_buff *beacon, bool in_intr);
146
147static int zd_reg2alpha2(u8 regdomain, char *alpha2)
148{
149 unsigned int i;
150 struct zd_reg_alpha2_map *reg_map;
151 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
152 reg_map = ®_alpha2_map[i];
153 if (regdomain == reg_map->reg) {
154 alpha2[0] = reg_map->alpha2[0];
155 alpha2[1] = reg_map->alpha2[1];
156 return 0;
157 }
158 }
159 return 1;
160}
161
162static int zd_check_signal(struct ieee80211_hw *hw, int signal)
163{
164 struct zd_mac *mac = zd_hw_mac(hw);
165
166 dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100,
167 "%s: signal value from device not in range 0..100, "
168 "but %d.\n", __func__, signal);
169
170 if (signal < 0)
171 signal = 0;
172 else if (signal > 100)
173 signal = 100;
174
175 return signal;
176}
177
178int zd_mac_preinit_hw(struct ieee80211_hw *hw)
179{
180 int r;
181 u8 addr[ETH_ALEN];
182 struct zd_mac *mac = zd_hw_mac(hw);
183
184 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
185 if (r)
186 return r;
187
188 SET_IEEE80211_PERM_ADDR(hw, addr);
189
190 return 0;
191}
192
193int zd_mac_init_hw(struct ieee80211_hw *hw)
194{
195 int r;
196 struct zd_mac *mac = zd_hw_mac(hw);
197 struct zd_chip *chip = &mac->chip;
198 char alpha2[2];
199 u8 default_regdomain;
200
201 r = zd_chip_enable_int(chip);
202 if (r)
203 goto out;
204 r = zd_chip_init_hw(chip);
205 if (r)
206 goto disable_int;
207
208 ZD_ASSERT(!irqs_disabled());
209
210 r = zd_read_regdomain(chip, &default_regdomain);
211 if (r)
212 goto disable_int;
213 spin_lock_irq(&mac->lock);
214 mac->regdomain = mac->default_regdomain = default_regdomain;
215 spin_unlock_irq(&mac->lock);
216
217
218
219 r = zd_set_encryption_type(chip, ENC_SNIFFER);
220 if (r)
221 goto disable_int;
222
223 r = zd_reg2alpha2(mac->regdomain, alpha2);
224 if (r)
225 goto disable_int;
226
227 r = regulatory_hint(hw->wiphy, alpha2);
228disable_int:
229 zd_chip_disable_int(chip);
230out:
231 return r;
232}
233
234void zd_mac_clear(struct zd_mac *mac)
235{
236 flush_workqueue(zd_workqueue);
237 zd_chip_clear(&mac->chip);
238 ZD_ASSERT(!spin_is_locked(&mac->lock));
239 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
240}
241
242static int set_rx_filter(struct zd_mac *mac)
243{
244 unsigned long flags;
245 u32 filter = STA_RX_FILTER;
246
247 spin_lock_irqsave(&mac->lock, flags);
248 if (mac->pass_ctrl)
249 filter |= RX_FILTER_CTRL;
250 spin_unlock_irqrestore(&mac->lock, flags);
251
252 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
253}
254
255static int set_mac_and_bssid(struct zd_mac *mac)
256{
257 int r;
258
259 if (!mac->vif)
260 return -1;
261
262 r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
263 if (r)
264 return r;
265
266
267
268
269 if (mac->type != NL80211_IFTYPE_AP)
270 return set_rx_filter(mac);
271 else
272 return zd_write_bssid(&mac->chip, mac->vif->addr);
273}
274
275static int set_mc_hash(struct zd_mac *mac)
276{
277 struct zd_mc_hash hash;
278 zd_mc_clear(&hash);
279 return zd_chip_set_multicast_hash(&mac->chip, &hash);
280}
281
282int zd_op_start(struct ieee80211_hw *hw)
283{
284 struct zd_mac *mac = zd_hw_mac(hw);
285 struct zd_chip *chip = &mac->chip;
286 struct zd_usb *usb = &chip->usb;
287 int r;
288
289 if (!usb->initialized) {
290 r = zd_usb_init_hw(usb);
291 if (r)
292 goto out;
293 }
294
295 r = zd_chip_enable_int(chip);
296 if (r < 0)
297 goto out;
298
299 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
300 if (r < 0)
301 goto disable_int;
302 r = set_rx_filter(mac);
303 if (r)
304 goto disable_int;
305 r = set_mc_hash(mac);
306 if (r)
307 goto disable_int;
308
309
310
311
312
313 msleep(10);
314
315 r = zd_chip_switch_radio_on(chip);
316 if (r < 0) {
317 dev_err(zd_chip_dev(chip),
318 "%s: failed to set radio on\n", __func__);
319 goto disable_int;
320 }
321 r = zd_chip_enable_rxtx(chip);
322 if (r < 0)
323 goto disable_radio;
324 r = zd_chip_enable_hwint(chip);
325 if (r < 0)
326 goto disable_rxtx;
327
328 housekeeping_enable(mac);
329 beacon_enable(mac);
330 set_bit(ZD_DEVICE_RUNNING, &mac->flags);
331 return 0;
332disable_rxtx:
333 zd_chip_disable_rxtx(chip);
334disable_radio:
335 zd_chip_switch_radio_off(chip);
336disable_int:
337 zd_chip_disable_int(chip);
338out:
339 return r;
340}
341
342void zd_op_stop(struct ieee80211_hw *hw)
343{
344 struct zd_mac *mac = zd_hw_mac(hw);
345 struct zd_chip *chip = &mac->chip;
346 struct sk_buff *skb;
347 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
348
349 clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
350
351
352
353
354
355
356 zd_chip_disable_rxtx(chip);
357 beacon_disable(mac);
358 housekeeping_disable(mac);
359 flush_workqueue(zd_workqueue);
360
361 zd_chip_disable_hwint(chip);
362 zd_chip_switch_radio_off(chip);
363 zd_chip_disable_int(chip);
364
365
366 while ((skb = skb_dequeue(ack_wait_queue)))
367 dev_kfree_skb_any(skb);
368}
369
370int zd_restore_settings(struct zd_mac *mac)
371{
372 struct sk_buff *beacon;
373 struct zd_mc_hash multicast_hash;
374 unsigned int short_preamble;
375 int r, beacon_interval, beacon_period;
376 u8 channel;
377
378 dev_dbg_f(zd_mac_dev(mac), "\n");
379
380 spin_lock_irq(&mac->lock);
381 multicast_hash = mac->multicast_hash;
382 short_preamble = mac->short_preamble;
383 beacon_interval = mac->beacon.interval;
384 beacon_period = mac->beacon.period;
385 channel = mac->channel;
386 spin_unlock_irq(&mac->lock);
387
388 r = set_mac_and_bssid(mac);
389 if (r < 0) {
390 dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
391 return r;
392 }
393
394 r = zd_chip_set_channel(&mac->chip, channel);
395 if (r < 0) {
396 dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
397 r);
398 return r;
399 }
400
401 set_rts_cts(mac, short_preamble);
402
403 r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
404 if (r < 0) {
405 dev_dbg_f(zd_mac_dev(mac),
406 "zd_chip_set_multicast_hash failed, %d\n", r);
407 return r;
408 }
409
410 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
411 mac->type == NL80211_IFTYPE_ADHOC ||
412 mac->type == NL80211_IFTYPE_AP) {
413 if (mac->vif != NULL) {
414 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
415 if (beacon)
416 zd_mac_config_beacon(mac->hw, beacon, false);
417 }
418
419 zd_set_beacon_interval(&mac->chip, beacon_interval,
420 beacon_period, mac->type);
421
422 spin_lock_irq(&mac->lock);
423 mac->beacon.last_update = jiffies;
424 spin_unlock_irq(&mac->lock);
425 }
426
427 return 0;
428}
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
445 int ackssi, struct tx_status *tx_status)
446{
447 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
448 int i;
449 int success = 1, retry = 1;
450 int first_idx;
451 const struct tx_retry_rate *retries;
452
453 ieee80211_tx_info_clear_status(info);
454
455 if (tx_status) {
456 success = !tx_status->failure;
457 retry = tx_status->retry + success;
458 }
459
460 if (success) {
461
462 info->flags |= IEEE80211_TX_STAT_ACK;
463 } else {
464
465 info->flags &= ~IEEE80211_TX_STAT_ACK;
466 }
467
468 first_idx = info->status.rates[0].idx;
469 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
470 retries = &zd_retry_rates[first_idx];
471 ZD_ASSERT(1 <= retry && retry <= retries->count);
472
473 info->status.rates[0].idx = retries->rate[0];
474 info->status.rates[0].count = 1;
475
476 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
477 info->status.rates[i].idx = retries->rate[i];
478 info->status.rates[i].count = 1;
479 }
480 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
481 info->status.rates[i].idx = retries->rate[retry - 1];
482 info->status.rates[i].count = 1;
483 }
484 if (i<IEEE80211_TX_MAX_RATES)
485 info->status.rates[i].idx = -1;
486
487 info->status.ack_signal = zd_check_signal(hw, ackssi);
488 ieee80211_tx_status_irqsafe(hw, skb);
489}
490
491
492
493
494
495
496
497
498
499void zd_mac_tx_failed(struct urb *urb)
500{
501 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
502 struct zd_mac *mac = zd_hw_mac(hw);
503 struct sk_buff_head *q = &mac->ack_wait_queue;
504 struct sk_buff *skb;
505 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
506 unsigned long flags;
507 int success = !tx_status->failure;
508 int retry = tx_status->retry + success;
509 int found = 0;
510 int i, position = 0;
511
512 q = &mac->ack_wait_queue;
513 spin_lock_irqsave(&q->lock, flags);
514
515 skb_queue_walk(q, skb) {
516 struct ieee80211_hdr *tx_hdr;
517 struct ieee80211_tx_info *info;
518 int first_idx, final_idx;
519 const struct tx_retry_rate *retries;
520 u8 final_rate;
521
522 position ++;
523
524
525
526
527 if (tx_status->failure && mac->ack_pending &&
528 skb_queue_is_first(q, skb)) {
529 continue;
530 }
531
532 tx_hdr = (struct ieee80211_hdr *)skb->data;
533
534
535 if (unlikely(!ether_addr_equal(tx_hdr->addr1, tx_status->mac)))
536 continue;
537
538
539
540 info = IEEE80211_SKB_CB(skb);
541 first_idx = info->status.rates[0].idx;
542 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
543 retries = &zd_retry_rates[first_idx];
544 if (retry <= 0 || retry > retries->count)
545 continue;
546
547 final_idx = retries->rate[retry - 1];
548 final_rate = zd_rates[final_idx].hw_value;
549
550 if (final_rate != tx_status->rate) {
551 continue;
552 }
553
554 found = 1;
555 break;
556 }
557
558 if (found) {
559 for (i=1; i<=position; i++) {
560 skb = __skb_dequeue(q);
561 zd_mac_tx_status(hw, skb,
562 mac->ack_pending ? mac->ack_signal : 0,
563 i == position ? tx_status : NULL);
564 mac->ack_pending = 0;
565 }
566 }
567
568 spin_unlock_irqrestore(&q->lock, flags);
569}
570
571
572
573
574
575
576
577
578
579
580
581void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
582{
583 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
584 struct ieee80211_hw *hw = info->rate_driver_data[0];
585 struct zd_mac *mac = zd_hw_mac(hw);
586
587 ieee80211_tx_info_clear_status(info);
588
589 skb_pull(skb, sizeof(struct zd_ctrlset));
590 if (unlikely(error ||
591 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
592
593
594
595 ieee80211_tx_status_irqsafe(hw, skb);
596 } else {
597 struct sk_buff_head *q = &mac->ack_wait_queue;
598
599 skb_queue_tail(q, skb);
600 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
601 zd_mac_tx_status(hw, skb_dequeue(q),
602 mac->ack_pending ? mac->ack_signal : 0,
603 NULL);
604 mac->ack_pending = 0;
605 }
606 }
607}
608
609static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
610{
611
612
613
614 static const u8 rate_divisor[] = {
615 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
616 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
617
618 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
619 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
620 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
621 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
622 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
623 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
624 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
625 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
626 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
627 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
628 };
629
630 u32 bits = (u32)tx_length * 8;
631 u32 divisor;
632
633 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
634 if (divisor == 0)
635 return -EINVAL;
636
637 switch (zd_rate) {
638 case ZD_CCK_RATE_5_5M:
639 bits = (2*bits) + 10;
640 break;
641 case ZD_CCK_RATE_11M:
642 if (service) {
643 u32 t = bits % 11;
644 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
645 if (0 < t && t <= 3) {
646 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
647 }
648 }
649 bits += 10;
650 break;
651 }
652
653 return bits/divisor;
654}
655
656static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
657 struct ieee80211_hdr *header,
658 struct ieee80211_tx_info *info)
659{
660
661
662
663
664
665
666 cs->control = 0;
667
668
669 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
670 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
671
672
673 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
674 cs->control |= ZD_CS_NO_ACK;
675
676
677 if (ieee80211_is_pspoll(header->frame_control))
678 cs->control |= ZD_CS_PS_POLL_FRAME;
679
680 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
681 cs->control |= ZD_CS_RTS;
682
683 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
684 cs->control |= ZD_CS_SELF_CTS;
685
686
687}
688
689static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
690{
691 if (!mac->beacon.cur_beacon)
692 return false;
693
694 if (mac->beacon.cur_beacon->len != beacon->len)
695 return false;
696
697 return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
698}
699
700static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
701{
702 ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
703
704 kfree_skb(mac->beacon.cur_beacon);
705 mac->beacon.cur_beacon = NULL;
706}
707
708static void zd_mac_free_cur_beacon(struct zd_mac *mac)
709{
710 mutex_lock(&mac->chip.mutex);
711 zd_mac_free_cur_beacon_locked(mac);
712 mutex_unlock(&mac->chip.mutex);
713}
714
715static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
716 bool in_intr)
717{
718 struct zd_mac *mac = zd_hw_mac(hw);
719 int r, ret, num_cmds, req_pos = 0;
720 u32 tmp, j = 0;
721
722 u32 full_len = beacon->len + 4;
723 unsigned long end_jiffies, message_jiffies;
724 struct zd_ioreq32 *ioreqs;
725
726 mutex_lock(&mac->chip.mutex);
727
728
729 if (zd_mac_match_cur_beacon(mac, beacon)) {
730 r = 0;
731 goto out_nofree;
732 }
733
734
735 num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
736 ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
737 if (!ioreqs) {
738 r = -ENOMEM;
739 goto out_nofree;
740 }
741
742 r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
743 if (r < 0)
744 goto out;
745 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
746 if (r < 0)
747 goto release_sema;
748 if (in_intr && tmp & 0x2) {
749 r = -EBUSY;
750 goto release_sema;
751 }
752
753 end_jiffies = jiffies + HZ / 2;
754 message_jiffies = jiffies + HZ / 10;
755 while (tmp & 0x2) {
756 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
757 if (r < 0)
758 goto release_sema;
759 if (time_is_before_eq_jiffies(message_jiffies)) {
760 message_jiffies = jiffies + HZ / 10;
761 dev_err(zd_mac_dev(mac),
762 "CR_BCN_FIFO_SEMAPHORE not ready\n");
763 if (time_is_before_eq_jiffies(end_jiffies)) {
764 dev_err(zd_mac_dev(mac),
765 "Giving up beacon config.\n");
766 r = -ETIMEDOUT;
767 goto reset_device;
768 }
769 }
770 msleep(20);
771 }
772
773 ioreqs[req_pos].addr = CR_BCN_FIFO;
774 ioreqs[req_pos].value = full_len - 1;
775 req_pos++;
776 if (zd_chip_is_zd1211b(&mac->chip)) {
777 ioreqs[req_pos].addr = CR_BCN_LENGTH;
778 ioreqs[req_pos].value = full_len - 1;
779 req_pos++;
780 }
781
782 for (j = 0 ; j < beacon->len; j++) {
783 ioreqs[req_pos].addr = CR_BCN_FIFO;
784 ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
785 req_pos++;
786 }
787
788 for (j = 0; j < 4; j++) {
789 ioreqs[req_pos].addr = CR_BCN_FIFO;
790 ioreqs[req_pos].value = 0x0;
791 req_pos++;
792 }
793
794 BUG_ON(req_pos != num_cmds);
795
796 r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
797
798release_sema:
799
800
801
802
803 end_jiffies = jiffies + HZ / 2;
804 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
805 while (ret < 0) {
806 if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
807 ret = -ETIMEDOUT;
808 break;
809 }
810
811 msleep(20);
812 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
813 }
814
815 if (ret < 0)
816 dev_err(zd_mac_dev(mac), "Could not release "
817 "CR_BCN_FIFO_SEMAPHORE!\n");
818 if (r < 0 || ret < 0) {
819 if (r >= 0)
820 r = ret;
821
822
823
824 zd_mac_free_cur_beacon_locked(mac);
825
826 goto out;
827 }
828
829
830 zd_mac_free_cur_beacon_locked(mac);
831 mac->beacon.cur_beacon = beacon;
832 beacon = NULL;
833
834
835
836
837
838 r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
839 CR_BCN_PLCP_CFG);
840out:
841 kfree(ioreqs);
842out_nofree:
843 kfree_skb(beacon);
844 mutex_unlock(&mac->chip.mutex);
845
846 return r;
847
848reset_device:
849 zd_mac_free_cur_beacon_locked(mac);
850 kfree_skb(beacon);
851
852 mutex_unlock(&mac->chip.mutex);
853 kfree(ioreqs);
854
855
856 dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
857 "resetting device...");
858 usb_queue_reset_device(mac->chip.usb.intf);
859
860 return r;
861}
862
863static int fill_ctrlset(struct zd_mac *mac,
864 struct sk_buff *skb)
865{
866 int r;
867 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
868 unsigned int frag_len = skb->len + FCS_LEN;
869 unsigned int packet_length;
870 struct ieee80211_rate *txrate;
871 struct zd_ctrlset *cs = (struct zd_ctrlset *)
872 skb_push(skb, sizeof(struct zd_ctrlset));
873 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
874
875 ZD_ASSERT(frag_len <= 0xffff);
876
877
878
879
880
881
882 if (!ieee80211_is_pspoll(hdr->frame_control))
883 hdr->duration_id = 0;
884
885 txrate = ieee80211_get_tx_rate(mac->hw, info);
886
887 cs->modulation = txrate->hw_value;
888 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
889 cs->modulation = txrate->hw_value_short;
890
891 cs->tx_length = cpu_to_le16(frag_len);
892
893 cs_set_control(mac, cs, hdr, info);
894
895 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
896 ZD_ASSERT(packet_length <= 0xffff);
897
898
899
900 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
901 packet_length - frag_len : packet_length);
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916 cs->service = 0;
917 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
918 le16_to_cpu(cs->tx_length));
919 if (r < 0)
920 return r;
921 cs->current_length = cpu_to_le16(r);
922 cs->next_frame_length = 0;
923
924 return 0;
925}
926
927
928
929
930
931
932
933
934
935
936
937
938static void zd_op_tx(struct ieee80211_hw *hw,
939 struct ieee80211_tx_control *control,
940 struct sk_buff *skb)
941{
942 struct zd_mac *mac = zd_hw_mac(hw);
943 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
944 int r;
945
946 r = fill_ctrlset(mac, skb);
947 if (r)
948 goto fail;
949
950 info->rate_driver_data[0] = hw;
951
952 r = zd_usb_tx(&mac->chip.usb, skb);
953 if (r)
954 goto fail;
955 return;
956
957fail:
958 dev_kfree_skb(skb);
959}
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
976 struct ieee80211_rx_status *stats)
977{
978 struct zd_mac *mac = zd_hw_mac(hw);
979 struct sk_buff *skb;
980 struct sk_buff_head *q;
981 unsigned long flags;
982 int found = 0;
983 int i, position = 0;
984
985 if (!ieee80211_is_ack(rx_hdr->frame_control))
986 return 0;
987
988 q = &mac->ack_wait_queue;
989 spin_lock_irqsave(&q->lock, flags);
990 skb_queue_walk(q, skb) {
991 struct ieee80211_hdr *tx_hdr;
992
993 position ++;
994
995 if (mac->ack_pending && skb_queue_is_first(q, skb))
996 continue;
997
998 tx_hdr = (struct ieee80211_hdr *)skb->data;
999 if (likely(ether_addr_equal(tx_hdr->addr2, rx_hdr->addr1)))
1000 {
1001 found = 1;
1002 break;
1003 }
1004 }
1005
1006 if (found) {
1007 for (i=1; i<position; i++) {
1008 skb = __skb_dequeue(q);
1009 zd_mac_tx_status(hw, skb,
1010 mac->ack_pending ? mac->ack_signal : 0,
1011 NULL);
1012 mac->ack_pending = 0;
1013 }
1014
1015 mac->ack_pending = 1;
1016 mac->ack_signal = stats->signal;
1017
1018
1019 if (mac->type == NL80211_IFTYPE_AP) {
1020 skb = __skb_dequeue(q);
1021 zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
1022 mac->ack_pending = 0;
1023 }
1024 }
1025
1026 spin_unlock_irqrestore(&q->lock, flags);
1027 return 1;
1028}
1029
1030int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
1031{
1032 struct zd_mac *mac = zd_hw_mac(hw);
1033 struct ieee80211_rx_status stats;
1034 const struct rx_status *status;
1035 struct sk_buff *skb;
1036 int bad_frame = 0;
1037 __le16 fc;
1038 int need_padding;
1039 int i;
1040 u8 rate;
1041
1042 if (length < ZD_PLCP_HEADER_SIZE + 10 +
1043 FCS_LEN + sizeof(struct rx_status))
1044 return -EINVAL;
1045
1046 memset(&stats, 0, sizeof(stats));
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058 status = (struct rx_status *)
1059 (buffer + (length - sizeof(struct rx_status)));
1060 if (status->frame_status & ZD_RX_ERROR) {
1061 if (mac->pass_failed_fcs &&
1062 (status->frame_status & ZD_RX_CRC32_ERROR)) {
1063 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
1064 bad_frame = 1;
1065 } else {
1066 return -EINVAL;
1067 }
1068 }
1069
1070 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
1071 stats.band = NL80211_BAND_2GHZ;
1072 stats.signal = zd_check_signal(hw, status->signal_strength);
1073
1074 rate = zd_rx_rate(buffer, status);
1075
1076
1077 for (i = 0; i < mac->band.n_bitrates; i++)
1078 if (rate == mac->band.bitrates[i].hw_value)
1079 stats.rate_idx = i;
1080
1081 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
1082 buffer += ZD_PLCP_HEADER_SIZE;
1083
1084
1085
1086
1087
1088
1089 if (!bad_frame &&
1090 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
1091 && !mac->pass_ctrl)
1092 return 0;
1093
1094 fc = get_unaligned((__le16*)buffer);
1095 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
1096
1097 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
1098 if (skb == NULL)
1099 return -ENOMEM;
1100 if (need_padding) {
1101
1102 skb_reserve(skb, 2);
1103 }
1104
1105
1106 memcpy(skb_put(skb, length), buffer, length);
1107
1108 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
1109 ieee80211_rx_irqsafe(hw, skb);
1110 return 0;
1111}
1112
1113static int zd_op_add_interface(struct ieee80211_hw *hw,
1114 struct ieee80211_vif *vif)
1115{
1116 struct zd_mac *mac = zd_hw_mac(hw);
1117
1118
1119 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
1120 return -EOPNOTSUPP;
1121
1122 switch (vif->type) {
1123 case NL80211_IFTYPE_MONITOR:
1124 case NL80211_IFTYPE_MESH_POINT:
1125 case NL80211_IFTYPE_STATION:
1126 case NL80211_IFTYPE_ADHOC:
1127 case NL80211_IFTYPE_AP:
1128 mac->type = vif->type;
1129 break;
1130 default:
1131 return -EOPNOTSUPP;
1132 }
1133
1134 mac->vif = vif;
1135
1136 return set_mac_and_bssid(mac);
1137}
1138
1139static void zd_op_remove_interface(struct ieee80211_hw *hw,
1140 struct ieee80211_vif *vif)
1141{
1142 struct zd_mac *mac = zd_hw_mac(hw);
1143 mac->type = NL80211_IFTYPE_UNSPECIFIED;
1144 mac->vif = NULL;
1145 zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
1146 zd_write_mac_addr(&mac->chip, NULL);
1147
1148 zd_mac_free_cur_beacon(mac);
1149}
1150
1151static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
1152{
1153 struct zd_mac *mac = zd_hw_mac(hw);
1154 struct ieee80211_conf *conf = &hw->conf;
1155
1156 spin_lock_irq(&mac->lock);
1157 mac->channel = conf->chandef.chan->hw_value;
1158 spin_unlock_irq(&mac->lock);
1159
1160 return zd_chip_set_channel(&mac->chip, conf->chandef.chan->hw_value);
1161}
1162
1163static void zd_beacon_done(struct zd_mac *mac)
1164{
1165 struct sk_buff *skb, *beacon;
1166
1167 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1168 return;
1169 if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1170 return;
1171
1172
1173
1174
1175 while (!ieee80211_queue_stopped(mac->hw, 0)) {
1176 skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1177 if (!skb)
1178 break;
1179 zd_op_tx(mac->hw, NULL, skb);
1180 }
1181
1182
1183
1184
1185 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1186 if (beacon)
1187 zd_mac_config_beacon(mac->hw, beacon, true);
1188
1189 spin_lock_irq(&mac->lock);
1190 mac->beacon.last_update = jiffies;
1191 spin_unlock_irq(&mac->lock);
1192}
1193
1194static void zd_process_intr(struct work_struct *work)
1195{
1196 u16 int_status;
1197 unsigned long flags;
1198 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1199
1200 spin_lock_irqsave(&mac->lock, flags);
1201 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1202 spin_unlock_irqrestore(&mac->lock, flags);
1203
1204 if (int_status & INT_CFG_NEXT_BCN) {
1205
1206 zd_beacon_done(mac);
1207 } else {
1208 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
1209 }
1210
1211 zd_chip_enable_hwint(&mac->chip);
1212}
1213
1214
1215static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
1216 struct netdev_hw_addr_list *mc_list)
1217{
1218 struct zd_mac *mac = zd_hw_mac(hw);
1219 struct zd_mc_hash hash;
1220 struct netdev_hw_addr *ha;
1221
1222 zd_mc_clear(&hash);
1223
1224 netdev_hw_addr_list_for_each(ha, mc_list) {
1225 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1226 zd_mc_add_addr(&hash, ha->addr);
1227 }
1228
1229 return hash.low | ((u64)hash.high << 32);
1230}
1231
1232#define SUPPORTED_FIF_FLAGS \
1233 (FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1234 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1235static void zd_op_configure_filter(struct ieee80211_hw *hw,
1236 unsigned int changed_flags,
1237 unsigned int *new_flags,
1238 u64 multicast)
1239{
1240 struct zd_mc_hash hash = {
1241 .low = multicast,
1242 .high = multicast >> 32,
1243 };
1244 struct zd_mac *mac = zd_hw_mac(hw);
1245 unsigned long flags;
1246 int r;
1247
1248
1249 changed_flags &= SUPPORTED_FIF_FLAGS;
1250 *new_flags &= SUPPORTED_FIF_FLAGS;
1251
1252
1253
1254
1255
1256
1257
1258
1259 if (*new_flags & FIF_ALLMULTI)
1260 zd_mc_add_all(&hash);
1261
1262 spin_lock_irqsave(&mac->lock, flags);
1263 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1264 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1265 mac->multicast_hash = hash;
1266 spin_unlock_irqrestore(&mac->lock, flags);
1267
1268 zd_chip_set_multicast_hash(&mac->chip, &hash);
1269
1270 if (changed_flags & FIF_CONTROL) {
1271 r = set_rx_filter(mac);
1272 if (r)
1273 dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1274 }
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284}
1285
1286static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
1287{
1288 mutex_lock(&mac->chip.mutex);
1289 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1290 mutex_unlock(&mac->chip.mutex);
1291}
1292
1293static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1294 struct ieee80211_vif *vif,
1295 struct ieee80211_bss_conf *bss_conf,
1296 u32 changes)
1297{
1298 struct zd_mac *mac = zd_hw_mac(hw);
1299 int associated;
1300
1301 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1302
1303 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1304 mac->type == NL80211_IFTYPE_ADHOC ||
1305 mac->type == NL80211_IFTYPE_AP) {
1306 associated = true;
1307 if (changes & BSS_CHANGED_BEACON) {
1308 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1309
1310 if (beacon) {
1311 zd_chip_disable_hwint(&mac->chip);
1312 zd_mac_config_beacon(hw, beacon, false);
1313 zd_chip_enable_hwint(&mac->chip);
1314 }
1315 }
1316
1317 if (changes & BSS_CHANGED_BEACON_ENABLED) {
1318 u16 interval = 0;
1319 u8 period = 0;
1320
1321 if (bss_conf->enable_beacon) {
1322 period = bss_conf->dtim_period;
1323 interval = bss_conf->beacon_int;
1324 }
1325
1326 spin_lock_irq(&mac->lock);
1327 mac->beacon.period = period;
1328 mac->beacon.interval = interval;
1329 mac->beacon.last_update = jiffies;
1330 spin_unlock_irq(&mac->lock);
1331
1332 zd_set_beacon_interval(&mac->chip, interval, period,
1333 mac->type);
1334 }
1335 } else
1336 associated = is_valid_ether_addr(bss_conf->bssid);
1337
1338 spin_lock_irq(&mac->lock);
1339 mac->associated = associated;
1340 spin_unlock_irq(&mac->lock);
1341
1342
1343
1344 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1345 spin_lock_irq(&mac->lock);
1346 mac->short_preamble = bss_conf->use_short_preamble;
1347 spin_unlock_irq(&mac->lock);
1348
1349 set_rts_cts(mac, bss_conf->use_short_preamble);
1350 }
1351}
1352
1353static u64 zd_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1354{
1355 struct zd_mac *mac = zd_hw_mac(hw);
1356 return zd_chip_get_tsf(&mac->chip);
1357}
1358
1359static const struct ieee80211_ops zd_ops = {
1360 .tx = zd_op_tx,
1361 .start = zd_op_start,
1362 .stop = zd_op_stop,
1363 .add_interface = zd_op_add_interface,
1364 .remove_interface = zd_op_remove_interface,
1365 .config = zd_op_config,
1366 .prepare_multicast = zd_op_prepare_multicast,
1367 .configure_filter = zd_op_configure_filter,
1368 .bss_info_changed = zd_op_bss_info_changed,
1369 .get_tsf = zd_op_get_tsf,
1370};
1371
1372struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1373{
1374 struct zd_mac *mac;
1375 struct ieee80211_hw *hw;
1376
1377 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1378 if (!hw) {
1379 dev_dbg_f(&intf->dev, "out of memory\n");
1380 return NULL;
1381 }
1382
1383 mac = zd_hw_mac(hw);
1384
1385 memset(mac, 0, sizeof(*mac));
1386 spin_lock_init(&mac->lock);
1387 mac->hw = hw;
1388
1389 mac->type = NL80211_IFTYPE_UNSPECIFIED;
1390
1391 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1392 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1393 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1394 mac->band.bitrates = mac->rates;
1395 mac->band.n_channels = ARRAY_SIZE(zd_channels);
1396 mac->band.channels = mac->channels;
1397
1398 hw->wiphy->bands[NL80211_BAND_2GHZ] = &mac->band;
1399
1400 ieee80211_hw_set(hw, MFP_CAPABLE);
1401 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
1402 ieee80211_hw_set(hw, RX_INCLUDES_FCS);
1403 ieee80211_hw_set(hw, SIGNAL_UNSPEC);
1404
1405 hw->wiphy->interface_modes =
1406 BIT(NL80211_IFTYPE_MESH_POINT) |
1407 BIT(NL80211_IFTYPE_STATION) |
1408 BIT(NL80211_IFTYPE_ADHOC) |
1409 BIT(NL80211_IFTYPE_AP);
1410
1411 hw->max_signal = 100;
1412 hw->queues = 1;
1413 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1414
1415
1416
1417
1418 hw->max_rates = IEEE80211_TX_MAX_RATES;
1419 hw->max_rate_tries = 18;
1420
1421 skb_queue_head_init(&mac->ack_wait_queue);
1422 mac->ack_pending = 0;
1423
1424 zd_chip_init(&mac->chip, hw, intf);
1425 housekeeping_init(mac);
1426 beacon_init(mac);
1427 INIT_WORK(&mac->process_intr, zd_process_intr);
1428
1429 SET_IEEE80211_DEV(hw, &intf->dev);
1430 return hw;
1431}
1432
1433#define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1434
1435static void beacon_watchdog_handler(struct work_struct *work)
1436{
1437 struct zd_mac *mac =
1438 container_of(work, struct zd_mac, beacon.watchdog_work.work);
1439 struct sk_buff *beacon;
1440 unsigned long timeout;
1441 int interval, period;
1442
1443 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1444 goto rearm;
1445 if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1446 goto rearm;
1447
1448 spin_lock_irq(&mac->lock);
1449 interval = mac->beacon.interval;
1450 period = mac->beacon.period;
1451 timeout = mac->beacon.last_update +
1452 msecs_to_jiffies(interval * 1024 / 1000) * 3;
1453 spin_unlock_irq(&mac->lock);
1454
1455 if (interval > 0 && time_is_before_jiffies(timeout)) {
1456 dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1457 "restarting. "
1458 "(interval: %d, dtim: %d)\n",
1459 interval, period);
1460
1461 zd_chip_disable_hwint(&mac->chip);
1462
1463 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1464 if (beacon) {
1465 zd_mac_free_cur_beacon(mac);
1466
1467 zd_mac_config_beacon(mac->hw, beacon, false);
1468 }
1469
1470 zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1471
1472 zd_chip_enable_hwint(&mac->chip);
1473
1474 spin_lock_irq(&mac->lock);
1475 mac->beacon.last_update = jiffies;
1476 spin_unlock_irq(&mac->lock);
1477 }
1478
1479rearm:
1480 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1481 BEACON_WATCHDOG_DELAY);
1482}
1483
1484static void beacon_init(struct zd_mac *mac)
1485{
1486 INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1487}
1488
1489static void beacon_enable(struct zd_mac *mac)
1490{
1491 dev_dbg_f(zd_mac_dev(mac), "\n");
1492
1493 mac->beacon.last_update = jiffies;
1494 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1495 BEACON_WATCHDOG_DELAY);
1496}
1497
1498static void beacon_disable(struct zd_mac *mac)
1499{
1500 dev_dbg_f(zd_mac_dev(mac), "\n");
1501 cancel_delayed_work_sync(&mac->beacon.watchdog_work);
1502
1503 zd_mac_free_cur_beacon(mac);
1504}
1505
1506#define LINK_LED_WORK_DELAY HZ
1507
1508static void link_led_handler(struct work_struct *work)
1509{
1510 struct zd_mac *mac =
1511 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1512 struct zd_chip *chip = &mac->chip;
1513 int is_associated;
1514 int r;
1515
1516 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1517 goto requeue;
1518
1519 spin_lock_irq(&mac->lock);
1520 is_associated = mac->associated;
1521 spin_unlock_irq(&mac->lock);
1522
1523 r = zd_chip_control_leds(chip,
1524 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1525 if (r)
1526 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1527
1528requeue:
1529 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1530 LINK_LED_WORK_DELAY);
1531}
1532
1533static void housekeeping_init(struct zd_mac *mac)
1534{
1535 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1536}
1537
1538static void housekeeping_enable(struct zd_mac *mac)
1539{
1540 dev_dbg_f(zd_mac_dev(mac), "\n");
1541 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1542 0);
1543}
1544
1545static void housekeeping_disable(struct zd_mac *mac)
1546{
1547 dev_dbg_f(zd_mac_dev(mac), "\n");
1548 cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
1549 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
1550}
1551