zd1211rw: add beacon watchdog and setting HW beacon more failsafe
[deliverable/linux.git] / drivers / net / wireless / zd1211rw / zd_mac.c
1 /* ZD1211 USB-WLAN driver for Linux
2 *
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>
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
35 struct zd_reg_alpha2_map {
36 u32 reg;
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,
58 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
59 { .bitrate = 55,
60 .hw_value = ZD_CCK_RATE_5_5M,
61 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
62 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
63 { .bitrate = 110,
64 .hw_value = ZD_CCK_RATE_11M,
65 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
66 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
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
145 static int zd_reg2alpha2(u8 regdomain, char *alpha2)
146 {
147 unsigned int i;
148 struct zd_reg_alpha2_map *reg_map;
149 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
150 reg_map = &reg_alpha2_map[i];
151 if (regdomain == reg_map->reg) {
152 alpha2[0] = reg_map->alpha2[0];
153 alpha2[1] = reg_map->alpha2[1];
154 return 0;
155 }
156 }
157 return 1;
158 }
159
160 int zd_mac_preinit_hw(struct ieee80211_hw *hw)
161 {
162 int r;
163 u8 addr[ETH_ALEN];
164 struct zd_mac *mac = zd_hw_mac(hw);
165
166 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
167 if (r)
168 return r;
169
170 SET_IEEE80211_PERM_ADDR(hw, addr);
171
172 return 0;
173 }
174
175 int zd_mac_init_hw(struct ieee80211_hw *hw)
176 {
177 int r;
178 struct zd_mac *mac = zd_hw_mac(hw);
179 struct zd_chip *chip = &mac->chip;
180 char alpha2[2];
181 u8 default_regdomain;
182
183 r = zd_chip_enable_int(chip);
184 if (r)
185 goto out;
186 r = zd_chip_init_hw(chip);
187 if (r)
188 goto disable_int;
189
190 ZD_ASSERT(!irqs_disabled());
191
192 r = zd_read_regdomain(chip, &default_regdomain);
193 if (r)
194 goto disable_int;
195 spin_lock_irq(&mac->lock);
196 mac->regdomain = mac->default_regdomain = default_regdomain;
197 spin_unlock_irq(&mac->lock);
198
199 /* We must inform the device that we are doing encryption/decryption in
200 * software at the moment. */
201 r = zd_set_encryption_type(chip, ENC_SNIFFER);
202 if (r)
203 goto disable_int;
204
205 r = zd_reg2alpha2(mac->regdomain, alpha2);
206 if (r)
207 goto disable_int;
208
209 r = regulatory_hint(hw->wiphy, alpha2);
210 disable_int:
211 zd_chip_disable_int(chip);
212 out:
213 return r;
214 }
215
216 void zd_mac_clear(struct zd_mac *mac)
217 {
218 flush_workqueue(zd_workqueue);
219 zd_chip_clear(&mac->chip);
220 ZD_ASSERT(!spin_is_locked(&mac->lock));
221 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
222 }
223
224 static int set_rx_filter(struct zd_mac *mac)
225 {
226 unsigned long flags;
227 u32 filter = STA_RX_FILTER;
228
229 spin_lock_irqsave(&mac->lock, flags);
230 if (mac->pass_ctrl)
231 filter |= RX_FILTER_CTRL;
232 spin_unlock_irqrestore(&mac->lock, flags);
233
234 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
235 }
236
237 static int set_mac_and_bssid(struct zd_mac *mac)
238 {
239 int r;
240
241 if (!mac->vif)
242 return -1;
243
244 r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
245 if (r)
246 return r;
247
248 /* Vendor driver after setting MAC either sets BSSID for AP or
249 * filter for other modes.
250 */
251 if (mac->type != NL80211_IFTYPE_AP)
252 return set_rx_filter(mac);
253 else
254 return zd_write_bssid(&mac->chip, mac->vif->addr);
255 }
256
257 static int set_mc_hash(struct zd_mac *mac)
258 {
259 struct zd_mc_hash hash;
260 zd_mc_clear(&hash);
261 return zd_chip_set_multicast_hash(&mac->chip, &hash);
262 }
263
264 static int zd_op_start(struct ieee80211_hw *hw)
265 {
266 struct zd_mac *mac = zd_hw_mac(hw);
267 struct zd_chip *chip = &mac->chip;
268 struct zd_usb *usb = &chip->usb;
269 int r;
270
271 if (!usb->initialized) {
272 r = zd_usb_init_hw(usb);
273 if (r)
274 goto out;
275 }
276
277 r = zd_chip_enable_int(chip);
278 if (r < 0)
279 goto out;
280
281 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
282 if (r < 0)
283 goto disable_int;
284 r = set_rx_filter(mac);
285 if (r)
286 goto disable_int;
287 r = set_mc_hash(mac);
288 if (r)
289 goto disable_int;
290 r = zd_chip_switch_radio_on(chip);
291 if (r < 0)
292 goto disable_int;
293 r = zd_chip_enable_rxtx(chip);
294 if (r < 0)
295 goto disable_radio;
296 r = zd_chip_enable_hwint(chip);
297 if (r < 0)
298 goto disable_rxtx;
299
300 housekeeping_enable(mac);
301 beacon_enable(mac);
302 set_bit(ZD_DEVICE_RUNNING, &mac->flags);
303 return 0;
304 disable_rxtx:
305 zd_chip_disable_rxtx(chip);
306 disable_radio:
307 zd_chip_switch_radio_off(chip);
308 disable_int:
309 zd_chip_disable_int(chip);
310 out:
311 return r;
312 }
313
314 static void zd_op_stop(struct ieee80211_hw *hw)
315 {
316 struct zd_mac *mac = zd_hw_mac(hw);
317 struct zd_chip *chip = &mac->chip;
318 struct sk_buff *skb;
319 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
320
321 clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
322
323 /* The order here deliberately is a little different from the open()
324 * method, since we need to make sure there is no opportunity for RX
325 * frames to be processed by mac80211 after we have stopped it.
326 */
327
328 zd_chip_disable_rxtx(chip);
329 beacon_disable(mac);
330 housekeeping_disable(mac);
331 flush_workqueue(zd_workqueue);
332
333 zd_chip_disable_hwint(chip);
334 zd_chip_switch_radio_off(chip);
335 zd_chip_disable_int(chip);
336
337
338 while ((skb = skb_dequeue(ack_wait_queue)))
339 dev_kfree_skb_any(skb);
340 }
341
342 /**
343 * zd_mac_tx_status - reports tx status of a packet if required
344 * @hw - a &struct ieee80211_hw pointer
345 * @skb - a sk-buffer
346 * @flags: extra flags to set in the TX status info
347 * @ackssi: ACK signal strength
348 * @success - True for successful transmission of the frame
349 *
350 * This information calls ieee80211_tx_status_irqsafe() if required by the
351 * control information. It copies the control information into the status
352 * information.
353 *
354 * If no status information has been requested, the skb is freed.
355 */
356 static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
357 int ackssi, struct tx_status *tx_status)
358 {
359 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
360 int i;
361 int success = 1, retry = 1;
362 int first_idx;
363 const struct tx_retry_rate *retries;
364
365 ieee80211_tx_info_clear_status(info);
366
367 if (tx_status) {
368 success = !tx_status->failure;
369 retry = tx_status->retry + success;
370 }
371
372 if (success) {
373 /* success */
374 info->flags |= IEEE80211_TX_STAT_ACK;
375 } else {
376 /* failure */
377 info->flags &= ~IEEE80211_TX_STAT_ACK;
378 }
379
380 first_idx = info->status.rates[0].idx;
381 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
382 retries = &zd_retry_rates[first_idx];
383 ZD_ASSERT(1 <= retry && retry <= retries->count);
384
385 info->status.rates[0].idx = retries->rate[0];
386 info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
387
388 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
389 info->status.rates[i].idx = retries->rate[i];
390 info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
391 }
392 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
393 info->status.rates[i].idx = retries->rate[retry - 1];
394 info->status.rates[i].count = 1; // (success ? 1:2);
395 }
396 if (i<IEEE80211_TX_MAX_RATES)
397 info->status.rates[i].idx = -1; /* terminate */
398
399 info->status.ack_signal = ackssi;
400 ieee80211_tx_status_irqsafe(hw, skb);
401 }
402
403 /**
404 * zd_mac_tx_failed - callback for failed frames
405 * @dev: the mac80211 wireless device
406 *
407 * This function is called if a frame couldn't be successfully
408 * transferred. The first frame from the tx queue, will be selected and
409 * reported as error to the upper layers.
410 */
411 void zd_mac_tx_failed(struct urb *urb)
412 {
413 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
414 struct zd_mac *mac = zd_hw_mac(hw);
415 struct sk_buff_head *q = &mac->ack_wait_queue;
416 struct sk_buff *skb;
417 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
418 unsigned long flags;
419 int success = !tx_status->failure;
420 int retry = tx_status->retry + success;
421 int found = 0;
422 int i, position = 0;
423
424 q = &mac->ack_wait_queue;
425 spin_lock_irqsave(&q->lock, flags);
426
427 skb_queue_walk(q, skb) {
428 struct ieee80211_hdr *tx_hdr;
429 struct ieee80211_tx_info *info;
430 int first_idx, final_idx;
431 const struct tx_retry_rate *retries;
432 u8 final_rate;
433
434 position ++;
435
436 /* if the hardware reports a failure and we had a 802.11 ACK
437 * pending, then we skip the first skb when searching for a
438 * matching frame */
439 if (tx_status->failure && mac->ack_pending &&
440 skb_queue_is_first(q, skb)) {
441 continue;
442 }
443
444 tx_hdr = (struct ieee80211_hdr *)skb->data;
445
446 /* we skip all frames not matching the reported destination */
447 if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
448 continue;
449 }
450
451 /* we skip all frames not matching the reported final rate */
452
453 info = IEEE80211_SKB_CB(skb);
454 first_idx = info->status.rates[0].idx;
455 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
456 retries = &zd_retry_rates[first_idx];
457 if (retry <= 0 || retry > retries->count)
458 continue;
459
460 final_idx = retries->rate[retry - 1];
461 final_rate = zd_rates[final_idx].hw_value;
462
463 if (final_rate != tx_status->rate) {
464 continue;
465 }
466
467 found = 1;
468 break;
469 }
470
471 if (found) {
472 for (i=1; i<=position; i++) {
473 skb = __skb_dequeue(q);
474 zd_mac_tx_status(hw, skb,
475 mac->ack_pending ? mac->ack_signal : 0,
476 i == position ? tx_status : NULL);
477 mac->ack_pending = 0;
478 }
479 }
480
481 spin_unlock_irqrestore(&q->lock, flags);
482 }
483
484 /**
485 * zd_mac_tx_to_dev - callback for USB layer
486 * @skb: a &sk_buff pointer
487 * @error: error value, 0 if transmission successful
488 *
489 * Informs the MAC layer that the frame has successfully transferred to the
490 * device. If an ACK is required and the transfer to the device has been
491 * successful, the packets are put on the @ack_wait_queue with
492 * the control set removed.
493 */
494 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
495 {
496 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
497 struct ieee80211_hw *hw = info->rate_driver_data[0];
498 struct zd_mac *mac = zd_hw_mac(hw);
499
500 ieee80211_tx_info_clear_status(info);
501
502 skb_pull(skb, sizeof(struct zd_ctrlset));
503 if (unlikely(error ||
504 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
505 /*
506 * FIXME : do we need to fill in anything ?
507 */
508 ieee80211_tx_status_irqsafe(hw, skb);
509 } else {
510 struct sk_buff_head *q = &mac->ack_wait_queue;
511
512 skb_queue_tail(q, skb);
513 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
514 zd_mac_tx_status(hw, skb_dequeue(q),
515 mac->ack_pending ? mac->ack_signal : 0,
516 NULL);
517 mac->ack_pending = 0;
518 }
519 }
520 }
521
522 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
523 {
524 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
525 * the zd-rate values.
526 */
527 static const u8 rate_divisor[] = {
528 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
529 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
530 /* Bits must be doubled. */
531 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
532 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
533 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
534 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
535 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
536 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
537 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
538 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
539 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
540 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
541 };
542
543 u32 bits = (u32)tx_length * 8;
544 u32 divisor;
545
546 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
547 if (divisor == 0)
548 return -EINVAL;
549
550 switch (zd_rate) {
551 case ZD_CCK_RATE_5_5M:
552 bits = (2*bits) + 10; /* round up to the next integer */
553 break;
554 case ZD_CCK_RATE_11M:
555 if (service) {
556 u32 t = bits % 11;
557 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
558 if (0 < t && t <= 3) {
559 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
560 }
561 }
562 bits += 10; /* round up to the next integer */
563 break;
564 }
565
566 return bits/divisor;
567 }
568
569 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
570 struct ieee80211_hdr *header,
571 struct ieee80211_tx_info *info)
572 {
573 /*
574 * CONTROL TODO:
575 * - if backoff needed, enable bit 0
576 * - if burst (backoff not needed) disable bit 0
577 */
578
579 cs->control = 0;
580
581 /* First fragment */
582 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
583 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
584
585 /* No ACK expected (multicast, etc.) */
586 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
587 cs->control |= ZD_CS_NO_ACK;
588
589 /* PS-POLL */
590 if (ieee80211_is_pspoll(header->frame_control))
591 cs->control |= ZD_CS_PS_POLL_FRAME;
592
593 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
594 cs->control |= ZD_CS_RTS;
595
596 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
597 cs->control |= ZD_CS_SELF_CTS;
598
599 /* FIXME: Management frame? */
600 }
601
602 static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
603 {
604 struct zd_mac *mac = zd_hw_mac(hw);
605 int r, ret;
606 u32 tmp, j = 0;
607 /* 4 more bytes for tail CRC */
608 u32 full_len = beacon->len + 4;
609 unsigned long end_jiffies, message_jiffies;
610
611 mutex_lock(&mac->chip.mutex);
612
613 r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
614 if (r < 0)
615 goto out;
616 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
617 if (r < 0)
618 goto release_sema;
619
620 end_jiffies = jiffies + HZ / 2; /*~500ms*/
621 message_jiffies = jiffies + HZ / 10; /*~100ms*/
622 while (tmp & 0x2) {
623 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
624 if (r < 0)
625 goto release_sema;
626 if (time_is_before_eq_jiffies(message_jiffies)) {
627 message_jiffies = jiffies + HZ / 10;
628 dev_err(zd_mac_dev(mac),
629 "CR_BCN_FIFO_SEMAPHORE not ready\n");
630 if (time_is_before_eq_jiffies(end_jiffies)) {
631 dev_err(zd_mac_dev(mac),
632 "Giving up beacon config.\n");
633 r = -ETIMEDOUT;
634 goto release_sema;
635 }
636 }
637 msleep(20);
638 }
639
640 r = zd_iowrite32_locked(&mac->chip, full_len - 1, CR_BCN_FIFO);
641 if (r < 0)
642 goto release_sema;
643 if (zd_chip_is_zd1211b(&mac->chip)) {
644 r = zd_iowrite32_locked(&mac->chip, full_len - 1,
645 CR_BCN_LENGTH);
646 if (r < 0)
647 goto release_sema;
648 }
649
650 for (j = 0 ; j < beacon->len; j++) {
651 r = zd_iowrite32_locked(&mac->chip, *((u8 *)(beacon->data + j)),
652 CR_BCN_FIFO);
653 if (r < 0)
654 goto release_sema;
655 }
656
657 for (j = 0; j < 4; j++) {
658 r = zd_iowrite32_locked(&mac->chip, 0x0, CR_BCN_FIFO);
659 if (r < 0)
660 goto release_sema;
661 }
662
663 release_sema:
664 /*
665 * Try very hard to release device beacon semaphore, as otherwise
666 * device/driver can be left in unusable state.
667 */
668 end_jiffies = jiffies + HZ / 2; /*~500ms*/
669 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
670 while (ret < 0) {
671 if (time_is_before_eq_jiffies(end_jiffies)) {
672 ret = -ETIMEDOUT;
673 break;
674 }
675
676 msleep(20);
677 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
678 }
679
680 if (ret < 0)
681 dev_err(zd_mac_dev(mac), "Could not release "
682 "CR_BCN_FIFO_SEMAPHORE!\n");
683 if (r < 0 || ret < 0) {
684 if (r >= 0)
685 r = ret;
686 goto out;
687 }
688
689 /* 802.11b/g 2.4G CCK 1Mb
690 * 802.11a, not yet implemented, uses different values (see GPL vendor
691 * driver)
692 */
693 r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
694 CR_BCN_PLCP_CFG);
695 out:
696 mutex_unlock(&mac->chip.mutex);
697 return r;
698 }
699
700 static int fill_ctrlset(struct zd_mac *mac,
701 struct sk_buff *skb)
702 {
703 int r;
704 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
705 unsigned int frag_len = skb->len + FCS_LEN;
706 unsigned int packet_length;
707 struct ieee80211_rate *txrate;
708 struct zd_ctrlset *cs = (struct zd_ctrlset *)
709 skb_push(skb, sizeof(struct zd_ctrlset));
710 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
711
712 ZD_ASSERT(frag_len <= 0xffff);
713
714 txrate = ieee80211_get_tx_rate(mac->hw, info);
715
716 cs->modulation = txrate->hw_value;
717 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
718 cs->modulation = txrate->hw_value_short;
719
720 cs->tx_length = cpu_to_le16(frag_len);
721
722 cs_set_control(mac, cs, hdr, info);
723
724 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
725 ZD_ASSERT(packet_length <= 0xffff);
726 /* ZD1211B: Computing the length difference this way, gives us
727 * flexibility to compute the packet length.
728 */
729 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
730 packet_length - frag_len : packet_length);
731
732 /*
733 * CURRENT LENGTH:
734 * - transmit frame length in microseconds
735 * - seems to be derived from frame length
736 * - see Cal_Us_Service() in zdinlinef.h
737 * - if macp->bTxBurstEnable is enabled, then multiply by 4
738 * - bTxBurstEnable is never set in the vendor driver
739 *
740 * SERVICE:
741 * - "for PLCP configuration"
742 * - always 0 except in some situations at 802.11b 11M
743 * - see line 53 of zdinlinef.h
744 */
745 cs->service = 0;
746 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
747 le16_to_cpu(cs->tx_length));
748 if (r < 0)
749 return r;
750 cs->current_length = cpu_to_le16(r);
751 cs->next_frame_length = 0;
752
753 return 0;
754 }
755
756 /**
757 * zd_op_tx - transmits a network frame to the device
758 *
759 * @dev: mac80211 hardware device
760 * @skb: socket buffer
761 * @control: the control structure
762 *
763 * This function transmit an IEEE 802.11 network frame to the device. The
764 * control block of the skbuff will be initialized. If necessary the incoming
765 * mac80211 queues will be stopped.
766 */
767 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
768 {
769 struct zd_mac *mac = zd_hw_mac(hw);
770 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
771 int r;
772
773 r = fill_ctrlset(mac, skb);
774 if (r)
775 goto fail;
776
777 info->rate_driver_data[0] = hw;
778
779 r = zd_usb_tx(&mac->chip.usb, skb);
780 if (r)
781 goto fail;
782 return 0;
783
784 fail:
785 dev_kfree_skb(skb);
786 return 0;
787 }
788
789 /**
790 * filter_ack - filters incoming packets for acknowledgements
791 * @dev: the mac80211 device
792 * @rx_hdr: received header
793 * @stats: the status for the received packet
794 *
795 * This functions looks for ACK packets and tries to match them with the
796 * frames in the tx queue. If a match is found the frame will be dequeued and
797 * the upper layers is informed about the successful transmission. If
798 * mac80211 queues have been stopped and the number of frames still to be
799 * transmitted is low the queues will be opened again.
800 *
801 * Returns 1 if the frame was an ACK, 0 if it was ignored.
802 */
803 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
804 struct ieee80211_rx_status *stats)
805 {
806 struct zd_mac *mac = zd_hw_mac(hw);
807 struct sk_buff *skb;
808 struct sk_buff_head *q;
809 unsigned long flags;
810 int found = 0;
811 int i, position = 0;
812
813 if (!ieee80211_is_ack(rx_hdr->frame_control))
814 return 0;
815
816 q = &mac->ack_wait_queue;
817 spin_lock_irqsave(&q->lock, flags);
818 skb_queue_walk(q, skb) {
819 struct ieee80211_hdr *tx_hdr;
820
821 position ++;
822
823 if (mac->ack_pending && skb_queue_is_first(q, skb))
824 continue;
825
826 tx_hdr = (struct ieee80211_hdr *)skb->data;
827 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
828 {
829 found = 1;
830 break;
831 }
832 }
833
834 if (found) {
835 for (i=1; i<position; i++) {
836 skb = __skb_dequeue(q);
837 zd_mac_tx_status(hw, skb,
838 mac->ack_pending ? mac->ack_signal : 0,
839 NULL);
840 mac->ack_pending = 0;
841 }
842
843 mac->ack_pending = 1;
844 mac->ack_signal = stats->signal;
845
846 /* Prevent pending tx-packet on AP-mode */
847 if (mac->type == NL80211_IFTYPE_AP) {
848 skb = __skb_dequeue(q);
849 zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
850 mac->ack_pending = 0;
851 }
852 }
853
854 spin_unlock_irqrestore(&q->lock, flags);
855 return 1;
856 }
857
858 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
859 {
860 struct zd_mac *mac = zd_hw_mac(hw);
861 struct ieee80211_rx_status stats;
862 const struct rx_status *status;
863 struct sk_buff *skb;
864 int bad_frame = 0;
865 __le16 fc;
866 int need_padding;
867 int i;
868 u8 rate;
869
870 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
871 FCS_LEN + sizeof(struct rx_status))
872 return -EINVAL;
873
874 memset(&stats, 0, sizeof(stats));
875
876 /* Note about pass_failed_fcs and pass_ctrl access below:
877 * mac locking intentionally omitted here, as this is the only unlocked
878 * reader and the only writer is configure_filter. Plus, if there were
879 * any races accessing these variables, it wouldn't really matter.
880 * If mac80211 ever provides a way for us to access filter flags
881 * from outside configure_filter, we could improve on this. Also, this
882 * situation may change once we implement some kind of DMA-into-skb
883 * RX path. */
884
885 /* Caller has to ensure that length >= sizeof(struct rx_status). */
886 status = (struct rx_status *)
887 (buffer + (length - sizeof(struct rx_status)));
888 if (status->frame_status & ZD_RX_ERROR) {
889 if (mac->pass_failed_fcs &&
890 (status->frame_status & ZD_RX_CRC32_ERROR)) {
891 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
892 bad_frame = 1;
893 } else {
894 return -EINVAL;
895 }
896 }
897
898 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
899 stats.band = IEEE80211_BAND_2GHZ;
900 stats.signal = status->signal_strength;
901
902 rate = zd_rx_rate(buffer, status);
903
904 /* todo: return index in the big switches in zd_rx_rate instead */
905 for (i = 0; i < mac->band.n_bitrates; i++)
906 if (rate == mac->band.bitrates[i].hw_value)
907 stats.rate_idx = i;
908
909 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
910 buffer += ZD_PLCP_HEADER_SIZE;
911
912 /* Except for bad frames, filter each frame to see if it is an ACK, in
913 * which case our internal TX tracking is updated. Normally we then
914 * bail here as there's no need to pass ACKs on up to the stack, but
915 * there is also the case where the stack has requested us to pass
916 * control frames on up (pass_ctrl) which we must consider. */
917 if (!bad_frame &&
918 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
919 && !mac->pass_ctrl)
920 return 0;
921
922 fc = get_unaligned((__le16*)buffer);
923 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
924
925 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
926 if (skb == NULL)
927 return -ENOMEM;
928 if (need_padding) {
929 /* Make sure the payload data is 4 byte aligned. */
930 skb_reserve(skb, 2);
931 }
932
933 /* FIXME : could we avoid this big memcpy ? */
934 memcpy(skb_put(skb, length), buffer, length);
935
936 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
937 ieee80211_rx_irqsafe(hw, skb);
938 return 0;
939 }
940
941 static int zd_op_add_interface(struct ieee80211_hw *hw,
942 struct ieee80211_vif *vif)
943 {
944 struct zd_mac *mac = zd_hw_mac(hw);
945
946 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
947 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
948 return -EOPNOTSUPP;
949
950 switch (vif->type) {
951 case NL80211_IFTYPE_MONITOR:
952 case NL80211_IFTYPE_MESH_POINT:
953 case NL80211_IFTYPE_STATION:
954 case NL80211_IFTYPE_ADHOC:
955 mac->type = vif->type;
956 break;
957 default:
958 return -EOPNOTSUPP;
959 }
960
961 mac->vif = vif;
962
963 return set_mac_and_bssid(mac);
964 }
965
966 static void zd_op_remove_interface(struct ieee80211_hw *hw,
967 struct ieee80211_vif *vif)
968 {
969 struct zd_mac *mac = zd_hw_mac(hw);
970 mac->type = NL80211_IFTYPE_UNSPECIFIED;
971 mac->vif = NULL;
972 zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
973 zd_write_mac_addr(&mac->chip, NULL);
974 }
975
976 static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
977 {
978 struct zd_mac *mac = zd_hw_mac(hw);
979 struct ieee80211_conf *conf = &hw->conf;
980
981 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
982 }
983
984 static void zd_beacon_done(struct zd_mac *mac)
985 {
986 struct sk_buff *skb, *beacon;
987
988 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
989 return;
990 if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
991 return;
992
993 /*
994 * Send out buffered broad- and multicast frames.
995 */
996 while (!ieee80211_queue_stopped(mac->hw, 0)) {
997 skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
998 if (!skb)
999 break;
1000 zd_op_tx(mac->hw, skb);
1001 }
1002
1003 /*
1004 * Fetch next beacon so that tim_count is updated.
1005 */
1006 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1007 if (beacon) {
1008 zd_mac_config_beacon(mac->hw, beacon);
1009 kfree_skb(beacon);
1010 }
1011
1012 spin_lock_irq(&mac->lock);
1013 mac->beacon.last_update = jiffies;
1014 spin_unlock_irq(&mac->lock);
1015 }
1016
1017 static void zd_process_intr(struct work_struct *work)
1018 {
1019 u16 int_status;
1020 unsigned long flags;
1021 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1022
1023 spin_lock_irqsave(&mac->lock, flags);
1024 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1025 spin_unlock_irqrestore(&mac->lock, flags);
1026
1027 if (int_status & INT_CFG_NEXT_BCN) {
1028 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1029 zd_beacon_done(mac);
1030 } else {
1031 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
1032 }
1033
1034 zd_chip_enable_hwint(&mac->chip);
1035 }
1036
1037
1038 static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
1039 struct netdev_hw_addr_list *mc_list)
1040 {
1041 struct zd_mac *mac = zd_hw_mac(hw);
1042 struct zd_mc_hash hash;
1043 struct netdev_hw_addr *ha;
1044
1045 zd_mc_clear(&hash);
1046
1047 netdev_hw_addr_list_for_each(ha, mc_list) {
1048 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1049 zd_mc_add_addr(&hash, ha->addr);
1050 }
1051
1052 return hash.low | ((u64)hash.high << 32);
1053 }
1054
1055 #define SUPPORTED_FIF_FLAGS \
1056 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1057 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1058 static void zd_op_configure_filter(struct ieee80211_hw *hw,
1059 unsigned int changed_flags,
1060 unsigned int *new_flags,
1061 u64 multicast)
1062 {
1063 struct zd_mc_hash hash = {
1064 .low = multicast,
1065 .high = multicast >> 32,
1066 };
1067 struct zd_mac *mac = zd_hw_mac(hw);
1068 unsigned long flags;
1069 int r;
1070
1071 /* Only deal with supported flags */
1072 changed_flags &= SUPPORTED_FIF_FLAGS;
1073 *new_flags &= SUPPORTED_FIF_FLAGS;
1074
1075 /*
1076 * If multicast parameter (as returned by zd_op_prepare_multicast)
1077 * has changed, no bit in changed_flags is set. To handle this
1078 * situation, we do not return if changed_flags is 0. If we do so,
1079 * we will have some issue with IPv6 which uses multicast for link
1080 * layer address resolution.
1081 */
1082 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
1083 zd_mc_add_all(&hash);
1084
1085 spin_lock_irqsave(&mac->lock, flags);
1086 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1087 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1088 mac->multicast_hash = hash;
1089 spin_unlock_irqrestore(&mac->lock, flags);
1090
1091 zd_chip_set_multicast_hash(&mac->chip, &hash);
1092
1093 if (changed_flags & FIF_CONTROL) {
1094 r = set_rx_filter(mac);
1095 if (r)
1096 dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1097 }
1098
1099 /* no handling required for FIF_OTHER_BSS as we don't currently
1100 * do BSSID filtering */
1101 /* FIXME: in future it would be nice to enable the probe response
1102 * filter (so that the driver doesn't see them) until
1103 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1104 * have to schedule work to enable prbresp reception, which might
1105 * happen too late. For now we'll just listen and forward them all the
1106 * time. */
1107 }
1108
1109 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
1110 {
1111 mutex_lock(&mac->chip.mutex);
1112 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1113 mutex_unlock(&mac->chip.mutex);
1114 }
1115
1116 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1117 struct ieee80211_vif *vif,
1118 struct ieee80211_bss_conf *bss_conf,
1119 u32 changes)
1120 {
1121 struct zd_mac *mac = zd_hw_mac(hw);
1122 int associated;
1123
1124 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1125
1126 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1127 mac->type == NL80211_IFTYPE_ADHOC) {
1128 associated = true;
1129 if (changes & BSS_CHANGED_BEACON) {
1130 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1131
1132 if (beacon) {
1133 zd_chip_disable_hwint(&mac->chip);
1134 zd_mac_config_beacon(hw, beacon);
1135 zd_chip_enable_hwint(&mac->chip);
1136 kfree_skb(beacon);
1137 }
1138 }
1139
1140 if (changes & BSS_CHANGED_BEACON_ENABLED) {
1141 u16 interval = 0;
1142 u8 period = 0;
1143
1144 if (bss_conf->enable_beacon) {
1145 period = bss_conf->dtim_period;
1146 interval = bss_conf->beacon_int;
1147 }
1148
1149 spin_lock_irq(&mac->lock);
1150 mac->beacon.period = period;
1151 mac->beacon.interval = interval;
1152 mac->beacon.last_update = jiffies;
1153 spin_unlock_irq(&mac->lock);
1154
1155 zd_set_beacon_interval(&mac->chip, interval, period,
1156 mac->type);
1157 }
1158 } else
1159 associated = is_valid_ether_addr(bss_conf->bssid);
1160
1161 spin_lock_irq(&mac->lock);
1162 mac->associated = associated;
1163 spin_unlock_irq(&mac->lock);
1164
1165 /* TODO: do hardware bssid filtering */
1166
1167 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1168 spin_lock_irq(&mac->lock);
1169 mac->short_preamble = bss_conf->use_short_preamble;
1170 spin_unlock_irq(&mac->lock);
1171
1172 set_rts_cts(mac, bss_conf->use_short_preamble);
1173 }
1174 }
1175
1176 static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
1177 {
1178 struct zd_mac *mac = zd_hw_mac(hw);
1179 return zd_chip_get_tsf(&mac->chip);
1180 }
1181
1182 static const struct ieee80211_ops zd_ops = {
1183 .tx = zd_op_tx,
1184 .start = zd_op_start,
1185 .stop = zd_op_stop,
1186 .add_interface = zd_op_add_interface,
1187 .remove_interface = zd_op_remove_interface,
1188 .config = zd_op_config,
1189 .prepare_multicast = zd_op_prepare_multicast,
1190 .configure_filter = zd_op_configure_filter,
1191 .bss_info_changed = zd_op_bss_info_changed,
1192 .get_tsf = zd_op_get_tsf,
1193 };
1194
1195 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1196 {
1197 struct zd_mac *mac;
1198 struct ieee80211_hw *hw;
1199
1200 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1201 if (!hw) {
1202 dev_dbg_f(&intf->dev, "out of memory\n");
1203 return NULL;
1204 }
1205
1206 mac = zd_hw_mac(hw);
1207
1208 memset(mac, 0, sizeof(*mac));
1209 spin_lock_init(&mac->lock);
1210 mac->hw = hw;
1211
1212 mac->type = NL80211_IFTYPE_UNSPECIFIED;
1213
1214 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1215 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1216 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1217 mac->band.bitrates = mac->rates;
1218 mac->band.n_channels = ARRAY_SIZE(zd_channels);
1219 mac->band.channels = mac->channels;
1220
1221 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1222
1223 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1224 IEEE80211_HW_SIGNAL_UNSPEC |
1225 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
1226
1227 hw->wiphy->interface_modes =
1228 BIT(NL80211_IFTYPE_MESH_POINT) |
1229 BIT(NL80211_IFTYPE_STATION) |
1230 BIT(NL80211_IFTYPE_ADHOC);
1231
1232 hw->max_signal = 100;
1233 hw->queues = 1;
1234 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1235
1236 /*
1237 * Tell mac80211 that we support multi rate retries
1238 */
1239 hw->max_rates = IEEE80211_TX_MAX_RATES;
1240 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
1241
1242 skb_queue_head_init(&mac->ack_wait_queue);
1243 mac->ack_pending = 0;
1244
1245 zd_chip_init(&mac->chip, hw, intf);
1246 housekeeping_init(mac);
1247 beacon_init(mac);
1248 INIT_WORK(&mac->process_intr, zd_process_intr);
1249
1250 SET_IEEE80211_DEV(hw, &intf->dev);
1251 return hw;
1252 }
1253
1254 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1255
1256 static void beacon_watchdog_handler(struct work_struct *work)
1257 {
1258 struct zd_mac *mac =
1259 container_of(work, struct zd_mac, beacon.watchdog_work.work);
1260 struct sk_buff *beacon;
1261 unsigned long timeout;
1262 int interval, period;
1263
1264 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1265 goto rearm;
1266 if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1267 goto rearm;
1268
1269 spin_lock_irq(&mac->lock);
1270 interval = mac->beacon.interval;
1271 period = mac->beacon.period;
1272 timeout = mac->beacon.last_update + msecs_to_jiffies(interval) + HZ;
1273 spin_unlock_irq(&mac->lock);
1274
1275 if (interval > 0 && time_is_before_jiffies(timeout)) {
1276 dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1277 "restarting. "
1278 "(interval: %d, dtim: %d)\n",
1279 interval, period);
1280
1281 zd_chip_disable_hwint(&mac->chip);
1282
1283 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1284 if (beacon) {
1285 zd_mac_config_beacon(mac->hw, beacon);
1286 kfree_skb(beacon);
1287 }
1288
1289 zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1290
1291 zd_chip_enable_hwint(&mac->chip);
1292
1293 spin_lock_irq(&mac->lock);
1294 mac->beacon.last_update = jiffies;
1295 spin_unlock_irq(&mac->lock);
1296 }
1297
1298 rearm:
1299 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1300 BEACON_WATCHDOG_DELAY);
1301 }
1302
1303 static void beacon_init(struct zd_mac *mac)
1304 {
1305 INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1306 }
1307
1308 static void beacon_enable(struct zd_mac *mac)
1309 {
1310 dev_dbg_f(zd_mac_dev(mac), "\n");
1311
1312 mac->beacon.last_update = jiffies;
1313 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1314 BEACON_WATCHDOG_DELAY);
1315 }
1316
1317 static void beacon_disable(struct zd_mac *mac)
1318 {
1319 dev_dbg_f(zd_mac_dev(mac), "\n");
1320 cancel_delayed_work_sync(&mac->beacon.watchdog_work);
1321 }
1322
1323 #define LINK_LED_WORK_DELAY HZ
1324
1325 static void link_led_handler(struct work_struct *work)
1326 {
1327 struct zd_mac *mac =
1328 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1329 struct zd_chip *chip = &mac->chip;
1330 int is_associated;
1331 int r;
1332
1333 spin_lock_irq(&mac->lock);
1334 is_associated = mac->associated;
1335 spin_unlock_irq(&mac->lock);
1336
1337 r = zd_chip_control_leds(chip,
1338 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1339 if (r)
1340 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1341
1342 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1343 LINK_LED_WORK_DELAY);
1344 }
1345
1346 static void housekeeping_init(struct zd_mac *mac)
1347 {
1348 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1349 }
1350
1351 static void housekeeping_enable(struct zd_mac *mac)
1352 {
1353 dev_dbg_f(zd_mac_dev(mac), "\n");
1354 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1355 0);
1356 }
1357
1358 static void housekeeping_disable(struct zd_mac *mac)
1359 {
1360 dev_dbg_f(zd_mac_dev(mac), "\n");
1361 cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
1362 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
1363 }
This page took 0.060616 seconds and 5 git commands to generate.