zd1211rw: batch beacon config commands together
[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, num_cmds, req_pos = 0;
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 struct zd_ioreq32 *ioreqs;
611
612 /* Alloc memory for full beacon write at once. */
613 num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
614 ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
615 if (!ioreqs)
616 return -ENOMEM;
617
618 mutex_lock(&mac->chip.mutex);
619
620 r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
621 if (r < 0)
622 goto out;
623 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
624 if (r < 0)
625 goto release_sema;
626
627 end_jiffies = jiffies + HZ / 2; /*~500ms*/
628 message_jiffies = jiffies + HZ / 10; /*~100ms*/
629 while (tmp & 0x2) {
630 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
631 if (r < 0)
632 goto release_sema;
633 if (time_is_before_eq_jiffies(message_jiffies)) {
634 message_jiffies = jiffies + HZ / 10;
635 dev_err(zd_mac_dev(mac),
636 "CR_BCN_FIFO_SEMAPHORE not ready\n");
637 if (time_is_before_eq_jiffies(end_jiffies)) {
638 dev_err(zd_mac_dev(mac),
639 "Giving up beacon config.\n");
640 r = -ETIMEDOUT;
641 goto release_sema;
642 }
643 }
644 msleep(20);
645 }
646
647 ioreqs[req_pos].addr = CR_BCN_FIFO;
648 ioreqs[req_pos].value = full_len - 1;
649 req_pos++;
650 if (zd_chip_is_zd1211b(&mac->chip)) {
651 ioreqs[req_pos].addr = CR_BCN_LENGTH;
652 ioreqs[req_pos].value = full_len - 1;
653 req_pos++;
654 }
655
656 for (j = 0 ; j < beacon->len; j++) {
657 ioreqs[req_pos].addr = CR_BCN_FIFO;
658 ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
659 req_pos++;
660 }
661
662 for (j = 0; j < 4; j++) {
663 ioreqs[req_pos].addr = CR_BCN_FIFO;
664 ioreqs[req_pos].value = 0x0;
665 req_pos++;
666 }
667
668 BUG_ON(req_pos != num_cmds);
669
670 r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
671
672 release_sema:
673 /*
674 * Try very hard to release device beacon semaphore, as otherwise
675 * device/driver can be left in unusable state.
676 */
677 end_jiffies = jiffies + HZ / 2; /*~500ms*/
678 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
679 while (ret < 0) {
680 if (time_is_before_eq_jiffies(end_jiffies)) {
681 ret = -ETIMEDOUT;
682 break;
683 }
684
685 msleep(20);
686 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
687 }
688
689 if (ret < 0)
690 dev_err(zd_mac_dev(mac), "Could not release "
691 "CR_BCN_FIFO_SEMAPHORE!\n");
692 if (r < 0 || ret < 0) {
693 if (r >= 0)
694 r = ret;
695 goto out;
696 }
697
698 /* 802.11b/g 2.4G CCK 1Mb
699 * 802.11a, not yet implemented, uses different values (see GPL vendor
700 * driver)
701 */
702 r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
703 CR_BCN_PLCP_CFG);
704 out:
705 mutex_unlock(&mac->chip.mutex);
706 kfree(ioreqs);
707 return r;
708 }
709
710 static int fill_ctrlset(struct zd_mac *mac,
711 struct sk_buff *skb)
712 {
713 int r;
714 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
715 unsigned int frag_len = skb->len + FCS_LEN;
716 unsigned int packet_length;
717 struct ieee80211_rate *txrate;
718 struct zd_ctrlset *cs = (struct zd_ctrlset *)
719 skb_push(skb, sizeof(struct zd_ctrlset));
720 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
721
722 ZD_ASSERT(frag_len <= 0xffff);
723
724 txrate = ieee80211_get_tx_rate(mac->hw, info);
725
726 cs->modulation = txrate->hw_value;
727 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
728 cs->modulation = txrate->hw_value_short;
729
730 cs->tx_length = cpu_to_le16(frag_len);
731
732 cs_set_control(mac, cs, hdr, info);
733
734 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
735 ZD_ASSERT(packet_length <= 0xffff);
736 /* ZD1211B: Computing the length difference this way, gives us
737 * flexibility to compute the packet length.
738 */
739 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
740 packet_length - frag_len : packet_length);
741
742 /*
743 * CURRENT LENGTH:
744 * - transmit frame length in microseconds
745 * - seems to be derived from frame length
746 * - see Cal_Us_Service() in zdinlinef.h
747 * - if macp->bTxBurstEnable is enabled, then multiply by 4
748 * - bTxBurstEnable is never set in the vendor driver
749 *
750 * SERVICE:
751 * - "for PLCP configuration"
752 * - always 0 except in some situations at 802.11b 11M
753 * - see line 53 of zdinlinef.h
754 */
755 cs->service = 0;
756 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
757 le16_to_cpu(cs->tx_length));
758 if (r < 0)
759 return r;
760 cs->current_length = cpu_to_le16(r);
761 cs->next_frame_length = 0;
762
763 return 0;
764 }
765
766 /**
767 * zd_op_tx - transmits a network frame to the device
768 *
769 * @dev: mac80211 hardware device
770 * @skb: socket buffer
771 * @control: the control structure
772 *
773 * This function transmit an IEEE 802.11 network frame to the device. The
774 * control block of the skbuff will be initialized. If necessary the incoming
775 * mac80211 queues will be stopped.
776 */
777 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
778 {
779 struct zd_mac *mac = zd_hw_mac(hw);
780 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
781 int r;
782
783 r = fill_ctrlset(mac, skb);
784 if (r)
785 goto fail;
786
787 info->rate_driver_data[0] = hw;
788
789 r = zd_usb_tx(&mac->chip.usb, skb);
790 if (r)
791 goto fail;
792 return 0;
793
794 fail:
795 dev_kfree_skb(skb);
796 return 0;
797 }
798
799 /**
800 * filter_ack - filters incoming packets for acknowledgements
801 * @dev: the mac80211 device
802 * @rx_hdr: received header
803 * @stats: the status for the received packet
804 *
805 * This functions looks for ACK packets and tries to match them with the
806 * frames in the tx queue. If a match is found the frame will be dequeued and
807 * the upper layers is informed about the successful transmission. If
808 * mac80211 queues have been stopped and the number of frames still to be
809 * transmitted is low the queues will be opened again.
810 *
811 * Returns 1 if the frame was an ACK, 0 if it was ignored.
812 */
813 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
814 struct ieee80211_rx_status *stats)
815 {
816 struct zd_mac *mac = zd_hw_mac(hw);
817 struct sk_buff *skb;
818 struct sk_buff_head *q;
819 unsigned long flags;
820 int found = 0;
821 int i, position = 0;
822
823 if (!ieee80211_is_ack(rx_hdr->frame_control))
824 return 0;
825
826 q = &mac->ack_wait_queue;
827 spin_lock_irqsave(&q->lock, flags);
828 skb_queue_walk(q, skb) {
829 struct ieee80211_hdr *tx_hdr;
830
831 position ++;
832
833 if (mac->ack_pending && skb_queue_is_first(q, skb))
834 continue;
835
836 tx_hdr = (struct ieee80211_hdr *)skb->data;
837 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
838 {
839 found = 1;
840 break;
841 }
842 }
843
844 if (found) {
845 for (i=1; i<position; i++) {
846 skb = __skb_dequeue(q);
847 zd_mac_tx_status(hw, skb,
848 mac->ack_pending ? mac->ack_signal : 0,
849 NULL);
850 mac->ack_pending = 0;
851 }
852
853 mac->ack_pending = 1;
854 mac->ack_signal = stats->signal;
855
856 /* Prevent pending tx-packet on AP-mode */
857 if (mac->type == NL80211_IFTYPE_AP) {
858 skb = __skb_dequeue(q);
859 zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
860 mac->ack_pending = 0;
861 }
862 }
863
864 spin_unlock_irqrestore(&q->lock, flags);
865 return 1;
866 }
867
868 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
869 {
870 struct zd_mac *mac = zd_hw_mac(hw);
871 struct ieee80211_rx_status stats;
872 const struct rx_status *status;
873 struct sk_buff *skb;
874 int bad_frame = 0;
875 __le16 fc;
876 int need_padding;
877 int i;
878 u8 rate;
879
880 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
881 FCS_LEN + sizeof(struct rx_status))
882 return -EINVAL;
883
884 memset(&stats, 0, sizeof(stats));
885
886 /* Note about pass_failed_fcs and pass_ctrl access below:
887 * mac locking intentionally omitted here, as this is the only unlocked
888 * reader and the only writer is configure_filter. Plus, if there were
889 * any races accessing these variables, it wouldn't really matter.
890 * If mac80211 ever provides a way for us to access filter flags
891 * from outside configure_filter, we could improve on this. Also, this
892 * situation may change once we implement some kind of DMA-into-skb
893 * RX path. */
894
895 /* Caller has to ensure that length >= sizeof(struct rx_status). */
896 status = (struct rx_status *)
897 (buffer + (length - sizeof(struct rx_status)));
898 if (status->frame_status & ZD_RX_ERROR) {
899 if (mac->pass_failed_fcs &&
900 (status->frame_status & ZD_RX_CRC32_ERROR)) {
901 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
902 bad_frame = 1;
903 } else {
904 return -EINVAL;
905 }
906 }
907
908 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
909 stats.band = IEEE80211_BAND_2GHZ;
910 stats.signal = status->signal_strength;
911
912 rate = zd_rx_rate(buffer, status);
913
914 /* todo: return index in the big switches in zd_rx_rate instead */
915 for (i = 0; i < mac->band.n_bitrates; i++)
916 if (rate == mac->band.bitrates[i].hw_value)
917 stats.rate_idx = i;
918
919 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
920 buffer += ZD_PLCP_HEADER_SIZE;
921
922 /* Except for bad frames, filter each frame to see if it is an ACK, in
923 * which case our internal TX tracking is updated. Normally we then
924 * bail here as there's no need to pass ACKs on up to the stack, but
925 * there is also the case where the stack has requested us to pass
926 * control frames on up (pass_ctrl) which we must consider. */
927 if (!bad_frame &&
928 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
929 && !mac->pass_ctrl)
930 return 0;
931
932 fc = get_unaligned((__le16*)buffer);
933 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
934
935 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
936 if (skb == NULL)
937 return -ENOMEM;
938 if (need_padding) {
939 /* Make sure the payload data is 4 byte aligned. */
940 skb_reserve(skb, 2);
941 }
942
943 /* FIXME : could we avoid this big memcpy ? */
944 memcpy(skb_put(skb, length), buffer, length);
945
946 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
947 ieee80211_rx_irqsafe(hw, skb);
948 return 0;
949 }
950
951 static int zd_op_add_interface(struct ieee80211_hw *hw,
952 struct ieee80211_vif *vif)
953 {
954 struct zd_mac *mac = zd_hw_mac(hw);
955
956 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
957 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
958 return -EOPNOTSUPP;
959
960 switch (vif->type) {
961 case NL80211_IFTYPE_MONITOR:
962 case NL80211_IFTYPE_MESH_POINT:
963 case NL80211_IFTYPE_STATION:
964 case NL80211_IFTYPE_ADHOC:
965 mac->type = vif->type;
966 break;
967 default:
968 return -EOPNOTSUPP;
969 }
970
971 mac->vif = vif;
972
973 return set_mac_and_bssid(mac);
974 }
975
976 static void zd_op_remove_interface(struct ieee80211_hw *hw,
977 struct ieee80211_vif *vif)
978 {
979 struct zd_mac *mac = zd_hw_mac(hw);
980 mac->type = NL80211_IFTYPE_UNSPECIFIED;
981 mac->vif = NULL;
982 zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
983 zd_write_mac_addr(&mac->chip, NULL);
984 }
985
986 static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
987 {
988 struct zd_mac *mac = zd_hw_mac(hw);
989 struct ieee80211_conf *conf = &hw->conf;
990
991 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
992 }
993
994 static void zd_beacon_done(struct zd_mac *mac)
995 {
996 struct sk_buff *skb, *beacon;
997
998 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
999 return;
1000 if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1001 return;
1002
1003 /*
1004 * Send out buffered broad- and multicast frames.
1005 */
1006 while (!ieee80211_queue_stopped(mac->hw, 0)) {
1007 skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1008 if (!skb)
1009 break;
1010 zd_op_tx(mac->hw, skb);
1011 }
1012
1013 /*
1014 * Fetch next beacon so that tim_count is updated.
1015 */
1016 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1017 if (beacon) {
1018 zd_mac_config_beacon(mac->hw, beacon);
1019 kfree_skb(beacon);
1020 }
1021
1022 spin_lock_irq(&mac->lock);
1023 mac->beacon.last_update = jiffies;
1024 spin_unlock_irq(&mac->lock);
1025 }
1026
1027 static void zd_process_intr(struct work_struct *work)
1028 {
1029 u16 int_status;
1030 unsigned long flags;
1031 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1032
1033 spin_lock_irqsave(&mac->lock, flags);
1034 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1035 spin_unlock_irqrestore(&mac->lock, flags);
1036
1037 if (int_status & INT_CFG_NEXT_BCN) {
1038 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1039 zd_beacon_done(mac);
1040 } else {
1041 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
1042 }
1043
1044 zd_chip_enable_hwint(&mac->chip);
1045 }
1046
1047
1048 static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
1049 struct netdev_hw_addr_list *mc_list)
1050 {
1051 struct zd_mac *mac = zd_hw_mac(hw);
1052 struct zd_mc_hash hash;
1053 struct netdev_hw_addr *ha;
1054
1055 zd_mc_clear(&hash);
1056
1057 netdev_hw_addr_list_for_each(ha, mc_list) {
1058 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1059 zd_mc_add_addr(&hash, ha->addr);
1060 }
1061
1062 return hash.low | ((u64)hash.high << 32);
1063 }
1064
1065 #define SUPPORTED_FIF_FLAGS \
1066 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1067 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1068 static void zd_op_configure_filter(struct ieee80211_hw *hw,
1069 unsigned int changed_flags,
1070 unsigned int *new_flags,
1071 u64 multicast)
1072 {
1073 struct zd_mc_hash hash = {
1074 .low = multicast,
1075 .high = multicast >> 32,
1076 };
1077 struct zd_mac *mac = zd_hw_mac(hw);
1078 unsigned long flags;
1079 int r;
1080
1081 /* Only deal with supported flags */
1082 changed_flags &= SUPPORTED_FIF_FLAGS;
1083 *new_flags &= SUPPORTED_FIF_FLAGS;
1084
1085 /*
1086 * If multicast parameter (as returned by zd_op_prepare_multicast)
1087 * has changed, no bit in changed_flags is set. To handle this
1088 * situation, we do not return if changed_flags is 0. If we do so,
1089 * we will have some issue with IPv6 which uses multicast for link
1090 * layer address resolution.
1091 */
1092 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
1093 zd_mc_add_all(&hash);
1094
1095 spin_lock_irqsave(&mac->lock, flags);
1096 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1097 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1098 mac->multicast_hash = hash;
1099 spin_unlock_irqrestore(&mac->lock, flags);
1100
1101 zd_chip_set_multicast_hash(&mac->chip, &hash);
1102
1103 if (changed_flags & FIF_CONTROL) {
1104 r = set_rx_filter(mac);
1105 if (r)
1106 dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1107 }
1108
1109 /* no handling required for FIF_OTHER_BSS as we don't currently
1110 * do BSSID filtering */
1111 /* FIXME: in future it would be nice to enable the probe response
1112 * filter (so that the driver doesn't see them) until
1113 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1114 * have to schedule work to enable prbresp reception, which might
1115 * happen too late. For now we'll just listen and forward them all the
1116 * time. */
1117 }
1118
1119 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
1120 {
1121 mutex_lock(&mac->chip.mutex);
1122 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1123 mutex_unlock(&mac->chip.mutex);
1124 }
1125
1126 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1127 struct ieee80211_vif *vif,
1128 struct ieee80211_bss_conf *bss_conf,
1129 u32 changes)
1130 {
1131 struct zd_mac *mac = zd_hw_mac(hw);
1132 int associated;
1133
1134 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1135
1136 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1137 mac->type == NL80211_IFTYPE_ADHOC) {
1138 associated = true;
1139 if (changes & BSS_CHANGED_BEACON) {
1140 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1141
1142 if (beacon) {
1143 zd_chip_disable_hwint(&mac->chip);
1144 zd_mac_config_beacon(hw, beacon);
1145 zd_chip_enable_hwint(&mac->chip);
1146 kfree_skb(beacon);
1147 }
1148 }
1149
1150 if (changes & BSS_CHANGED_BEACON_ENABLED) {
1151 u16 interval = 0;
1152 u8 period = 0;
1153
1154 if (bss_conf->enable_beacon) {
1155 period = bss_conf->dtim_period;
1156 interval = bss_conf->beacon_int;
1157 }
1158
1159 spin_lock_irq(&mac->lock);
1160 mac->beacon.period = period;
1161 mac->beacon.interval = interval;
1162 mac->beacon.last_update = jiffies;
1163 spin_unlock_irq(&mac->lock);
1164
1165 zd_set_beacon_interval(&mac->chip, interval, period,
1166 mac->type);
1167 }
1168 } else
1169 associated = is_valid_ether_addr(bss_conf->bssid);
1170
1171 spin_lock_irq(&mac->lock);
1172 mac->associated = associated;
1173 spin_unlock_irq(&mac->lock);
1174
1175 /* TODO: do hardware bssid filtering */
1176
1177 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1178 spin_lock_irq(&mac->lock);
1179 mac->short_preamble = bss_conf->use_short_preamble;
1180 spin_unlock_irq(&mac->lock);
1181
1182 set_rts_cts(mac, bss_conf->use_short_preamble);
1183 }
1184 }
1185
1186 static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
1187 {
1188 struct zd_mac *mac = zd_hw_mac(hw);
1189 return zd_chip_get_tsf(&mac->chip);
1190 }
1191
1192 static const struct ieee80211_ops zd_ops = {
1193 .tx = zd_op_tx,
1194 .start = zd_op_start,
1195 .stop = zd_op_stop,
1196 .add_interface = zd_op_add_interface,
1197 .remove_interface = zd_op_remove_interface,
1198 .config = zd_op_config,
1199 .prepare_multicast = zd_op_prepare_multicast,
1200 .configure_filter = zd_op_configure_filter,
1201 .bss_info_changed = zd_op_bss_info_changed,
1202 .get_tsf = zd_op_get_tsf,
1203 };
1204
1205 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1206 {
1207 struct zd_mac *mac;
1208 struct ieee80211_hw *hw;
1209
1210 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1211 if (!hw) {
1212 dev_dbg_f(&intf->dev, "out of memory\n");
1213 return NULL;
1214 }
1215
1216 mac = zd_hw_mac(hw);
1217
1218 memset(mac, 0, sizeof(*mac));
1219 spin_lock_init(&mac->lock);
1220 mac->hw = hw;
1221
1222 mac->type = NL80211_IFTYPE_UNSPECIFIED;
1223
1224 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1225 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1226 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1227 mac->band.bitrates = mac->rates;
1228 mac->band.n_channels = ARRAY_SIZE(zd_channels);
1229 mac->band.channels = mac->channels;
1230
1231 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1232
1233 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1234 IEEE80211_HW_SIGNAL_UNSPEC |
1235 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
1236
1237 hw->wiphy->interface_modes =
1238 BIT(NL80211_IFTYPE_MESH_POINT) |
1239 BIT(NL80211_IFTYPE_STATION) |
1240 BIT(NL80211_IFTYPE_ADHOC);
1241
1242 hw->max_signal = 100;
1243 hw->queues = 1;
1244 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1245
1246 /*
1247 * Tell mac80211 that we support multi rate retries
1248 */
1249 hw->max_rates = IEEE80211_TX_MAX_RATES;
1250 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
1251
1252 skb_queue_head_init(&mac->ack_wait_queue);
1253 mac->ack_pending = 0;
1254
1255 zd_chip_init(&mac->chip, hw, intf);
1256 housekeeping_init(mac);
1257 beacon_init(mac);
1258 INIT_WORK(&mac->process_intr, zd_process_intr);
1259
1260 SET_IEEE80211_DEV(hw, &intf->dev);
1261 return hw;
1262 }
1263
1264 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1265
1266 static void beacon_watchdog_handler(struct work_struct *work)
1267 {
1268 struct zd_mac *mac =
1269 container_of(work, struct zd_mac, beacon.watchdog_work.work);
1270 struct sk_buff *beacon;
1271 unsigned long timeout;
1272 int interval, period;
1273
1274 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1275 goto rearm;
1276 if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1277 goto rearm;
1278
1279 spin_lock_irq(&mac->lock);
1280 interval = mac->beacon.interval;
1281 period = mac->beacon.period;
1282 timeout = mac->beacon.last_update + msecs_to_jiffies(interval) + HZ;
1283 spin_unlock_irq(&mac->lock);
1284
1285 if (interval > 0 && time_is_before_jiffies(timeout)) {
1286 dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1287 "restarting. "
1288 "(interval: %d, dtim: %d)\n",
1289 interval, period);
1290
1291 zd_chip_disable_hwint(&mac->chip);
1292
1293 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1294 if (beacon) {
1295 zd_mac_config_beacon(mac->hw, beacon);
1296 kfree_skb(beacon);
1297 }
1298
1299 zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1300
1301 zd_chip_enable_hwint(&mac->chip);
1302
1303 spin_lock_irq(&mac->lock);
1304 mac->beacon.last_update = jiffies;
1305 spin_unlock_irq(&mac->lock);
1306 }
1307
1308 rearm:
1309 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1310 BEACON_WATCHDOG_DELAY);
1311 }
1312
1313 static void beacon_init(struct zd_mac *mac)
1314 {
1315 INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1316 }
1317
1318 static void beacon_enable(struct zd_mac *mac)
1319 {
1320 dev_dbg_f(zd_mac_dev(mac), "\n");
1321
1322 mac->beacon.last_update = jiffies;
1323 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1324 BEACON_WATCHDOG_DELAY);
1325 }
1326
1327 static void beacon_disable(struct zd_mac *mac)
1328 {
1329 dev_dbg_f(zd_mac_dev(mac), "\n");
1330 cancel_delayed_work_sync(&mac->beacon.watchdog_work);
1331 }
1332
1333 #define LINK_LED_WORK_DELAY HZ
1334
1335 static void link_led_handler(struct work_struct *work)
1336 {
1337 struct zd_mac *mac =
1338 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1339 struct zd_chip *chip = &mac->chip;
1340 int is_associated;
1341 int r;
1342
1343 spin_lock_irq(&mac->lock);
1344 is_associated = mac->associated;
1345 spin_unlock_irq(&mac->lock);
1346
1347 r = zd_chip_control_leds(chip,
1348 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1349 if (r)
1350 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1351
1352 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1353 LINK_LED_WORK_DELAY);
1354 }
1355
1356 static void housekeeping_init(struct zd_mac *mac)
1357 {
1358 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1359 }
1360
1361 static void housekeeping_enable(struct zd_mac *mac)
1362 {
1363 dev_dbg_f(zd_mac_dev(mac), "\n");
1364 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1365 0);
1366 }
1367
1368 static void housekeeping_disable(struct zd_mac *mac)
1369 {
1370 dev_dbg_f(zd_mac_dev(mac), "\n");
1371 cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
1372 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
1373 }
This page took 0.098267 seconds and 5 git commands to generate.