• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500-V1.0.1.40_1.0.68/src/linux/linux-2.6/drivers/net/wireless/zd1211rw/
1/* zd_mac.c
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
20#include <linux/wireless.h>
21#include <linux/usb.h>
22#include <linux/jiffies.h>
23#include <net/ieee80211_radiotap.h>
24
25#include "zd_def.h"
26#include "zd_chip.h"
27#include "zd_mac.h"
28#include "zd_ieee80211.h"
29#include "zd_netdev.h"
30#include "zd_rf.h"
31#include "zd_util.h"
32
33static void ieee_init(struct ieee80211_device *ieee);
34static void softmac_init(struct ieee80211softmac_device *sm);
35static void set_rts_cts_work(struct work_struct *work);
36static void set_basic_rates_work(struct work_struct *work);
37
38static void housekeeping_init(struct zd_mac *mac);
39static void housekeeping_enable(struct zd_mac *mac);
40static void housekeeping_disable(struct zd_mac *mac);
41
42static void set_multicast_hash_handler(struct work_struct *work);
43
44static void do_rx(unsigned long mac_ptr);
45
46int zd_mac_init(struct zd_mac *mac,
47	        struct net_device *netdev,
48	        struct usb_interface *intf)
49{
50	struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
51
52	memset(mac, 0, sizeof(*mac));
53	spin_lock_init(&mac->lock);
54	mac->netdev = netdev;
55	INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
56	INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
57
58	skb_queue_head_init(&mac->rx_queue);
59	tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
60	tasklet_disable(&mac->rx_tasklet);
61
62	ieee_init(ieee);
63	softmac_init(ieee80211_priv(netdev));
64	zd_chip_init(&mac->chip, netdev, intf);
65	housekeeping_init(mac);
66	INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
67	return 0;
68}
69
70static int reset_channel(struct zd_mac *mac)
71{
72	int r;
73	unsigned long flags;
74	const struct channel_range *range;
75
76	spin_lock_irqsave(&mac->lock, flags);
77	range = zd_channel_range(mac->regdomain);
78	if (!range->start) {
79		r = -EINVAL;
80		goto out;
81	}
82	mac->requested_channel = range->start;
83	r = 0;
84out:
85	spin_unlock_irqrestore(&mac->lock, flags);
86	return r;
87}
88
89int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
90{
91	int r;
92	struct zd_chip *chip = &mac->chip;
93	u8 addr[ETH_ALEN];
94	u8 default_regdomain;
95
96	r = zd_chip_enable_int(chip);
97	if (r)
98		goto out;
99	r = zd_chip_init_hw(chip, device_type);
100	if (r)
101		goto disable_int;
102
103	zd_get_e2p_mac_addr(chip, addr);
104	r = zd_write_mac_addr(chip, addr);
105	if (r)
106		goto disable_int;
107	ZD_ASSERT(!irqs_disabled());
108	spin_lock_irq(&mac->lock);
109	memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
110	spin_unlock_irq(&mac->lock);
111
112	r = zd_read_regdomain(chip, &default_regdomain);
113	if (r)
114		goto disable_int;
115	if (!zd_regdomain_supported(default_regdomain)) {
116		dev_dbg_f(zd_mac_dev(mac),
117			  "Regulatory Domain %#04x is not supported.\n",
118		          default_regdomain);
119		r = -EINVAL;
120		goto disable_int;
121	}
122	spin_lock_irq(&mac->lock);
123	mac->regdomain = mac->default_regdomain = default_regdomain;
124	spin_unlock_irq(&mac->lock);
125	r = reset_channel(mac);
126	if (r)
127		goto disable_int;
128
129	/* We must inform the device that we are doing encryption/decryption in
130	 * software at the moment. */
131	r = zd_set_encryption_type(chip, ENC_SNIFFER);
132	if (r)
133		goto disable_int;
134
135	r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
136	if (r)
137		goto disable_int;
138
139	r = 0;
140disable_int:
141	zd_chip_disable_int(chip);
142out:
143	return r;
144}
145
146void zd_mac_clear(struct zd_mac *mac)
147{
148	flush_workqueue(zd_workqueue);
149	skb_queue_purge(&mac->rx_queue);
150	tasklet_kill(&mac->rx_tasklet);
151	zd_chip_clear(&mac->chip);
152	ZD_ASSERT(!spin_is_locked(&mac->lock));
153	ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
154}
155
156static int reset_mode(struct zd_mac *mac)
157{
158	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
159	u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
160	return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
161}
162
163int zd_mac_open(struct net_device *netdev)
164{
165	struct zd_mac *mac = zd_netdev_mac(netdev);
166	struct zd_chip *chip = &mac->chip;
167	int r;
168
169	tasklet_enable(&mac->rx_tasklet);
170
171	r = zd_chip_enable_int(chip);
172	if (r < 0)
173		goto out;
174
175	r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
176	if (r < 0)
177		goto disable_int;
178	r = reset_mode(mac);
179	if (r)
180		goto disable_int;
181	r = zd_chip_switch_radio_on(chip);
182	if (r < 0)
183		goto disable_int;
184	r = zd_chip_set_channel(chip, mac->requested_channel);
185	if (r < 0)
186		goto disable_radio;
187	r = zd_chip_enable_rx(chip);
188	if (r < 0)
189		goto disable_radio;
190	r = zd_chip_enable_hwint(chip);
191	if (r < 0)
192		goto disable_rx;
193
194	housekeeping_enable(mac);
195	ieee80211softmac_start(netdev);
196	return 0;
197disable_rx:
198	zd_chip_disable_rx(chip);
199disable_radio:
200	zd_chip_switch_radio_off(chip);
201disable_int:
202	zd_chip_disable_int(chip);
203out:
204	return r;
205}
206
207int zd_mac_stop(struct net_device *netdev)
208{
209	struct zd_mac *mac = zd_netdev_mac(netdev);
210	struct zd_chip *chip = &mac->chip;
211
212	netif_stop_queue(netdev);
213
214	/*
215	 * The order here deliberately is a little different from the open()
216	 * method, since we need to make sure there is no opportunity for RX
217	 * frames to be processed by softmac after we have stopped it.
218	 */
219
220	zd_chip_disable_rx(chip);
221	skb_queue_purge(&mac->rx_queue);
222	tasklet_disable(&mac->rx_tasklet);
223	housekeeping_disable(mac);
224	ieee80211softmac_stop(netdev);
225
226	/* Ensure no work items are running or queued from this point */
227	cancel_delayed_work(&mac->set_rts_cts_work);
228	cancel_delayed_work(&mac->set_basic_rates_work);
229	flush_workqueue(zd_workqueue);
230	mac->updating_rts_rate = 0;
231	mac->updating_basic_rates = 0;
232
233	zd_chip_disable_hwint(chip);
234	zd_chip_switch_radio_off(chip);
235	zd_chip_disable_int(chip);
236
237	return 0;
238}
239
240int zd_mac_set_mac_address(struct net_device *netdev, void *p)
241{
242	int r;
243	unsigned long flags;
244	struct sockaddr *addr = p;
245	struct zd_mac *mac = zd_netdev_mac(netdev);
246	struct zd_chip *chip = &mac->chip;
247
248	if (!is_valid_ether_addr(addr->sa_data))
249		return -EADDRNOTAVAIL;
250
251	dev_dbg_f(zd_mac_dev(mac),
252		  "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
253
254	r = zd_write_mac_addr(chip, addr->sa_data);
255	if (r)
256		return r;
257
258	spin_lock_irqsave(&mac->lock, flags);
259	memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
260	spin_unlock_irqrestore(&mac->lock, flags);
261
262	return 0;
263}
264
265static void set_multicast_hash_handler(struct work_struct *work)
266{
267	struct zd_mac *mac = container_of(work, struct zd_mac,
268					  set_multicast_hash_work);
269	struct zd_mc_hash hash;
270
271	spin_lock_irq(&mac->lock);
272	hash = mac->multicast_hash;
273	spin_unlock_irq(&mac->lock);
274
275	zd_chip_set_multicast_hash(&mac->chip, &hash);
276}
277
278void zd_mac_set_multicast_list(struct net_device *dev)
279{
280	struct zd_mc_hash hash;
281	struct zd_mac *mac = zd_netdev_mac(dev);
282	struct dev_mc_list *mc;
283	unsigned long flags;
284
285	if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
286		zd_mc_add_all(&hash);
287	} else {
288		zd_mc_clear(&hash);
289		for (mc = dev->mc_list; mc; mc = mc->next) {
290			dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
291				  MAC_ARG(mc->dmi_addr));
292			zd_mc_add_addr(&hash, mc->dmi_addr);
293		}
294	}
295
296	spin_lock_irqsave(&mac->lock, flags);
297	mac->multicast_hash = hash;
298	spin_unlock_irqrestore(&mac->lock, flags);
299	queue_work(zd_workqueue, &mac->set_multicast_hash_work);
300}
301
302int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
303{
304	int r;
305	u8 channel;
306
307	ZD_ASSERT(!irqs_disabled());
308	spin_lock_irq(&mac->lock);
309	if (regdomain == 0) {
310		regdomain = mac->default_regdomain;
311	}
312	if (!zd_regdomain_supported(regdomain)) {
313		spin_unlock_irq(&mac->lock);
314		return -EINVAL;
315	}
316	mac->regdomain = regdomain;
317	channel = mac->requested_channel;
318	spin_unlock_irq(&mac->lock);
319
320	r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
321	if (r)
322		return r;
323	if (!zd_regdomain_supports_channel(regdomain, channel)) {
324		r = reset_channel(mac);
325		if (r)
326			return r;
327	}
328
329	return 0;
330}
331
332u8 zd_mac_get_regdomain(struct zd_mac *mac)
333{
334	unsigned long flags;
335	u8 regdomain;
336
337	spin_lock_irqsave(&mac->lock, flags);
338	regdomain = mac->regdomain;
339	spin_unlock_irqrestore(&mac->lock, flags);
340	return regdomain;
341}
342
343/* Fallback to lowest rate, if rate is unknown. */
344static u8 rate_to_zd_rate(u8 rate)
345{
346	switch (rate) {
347	case IEEE80211_CCK_RATE_2MB:
348		return ZD_CCK_RATE_2M;
349	case IEEE80211_CCK_RATE_5MB:
350		return ZD_CCK_RATE_5_5M;
351	case IEEE80211_CCK_RATE_11MB:
352		return ZD_CCK_RATE_11M;
353	case IEEE80211_OFDM_RATE_6MB:
354		return ZD_OFDM_RATE_6M;
355	case IEEE80211_OFDM_RATE_9MB:
356		return ZD_OFDM_RATE_9M;
357	case IEEE80211_OFDM_RATE_12MB:
358		return ZD_OFDM_RATE_12M;
359	case IEEE80211_OFDM_RATE_18MB:
360		return ZD_OFDM_RATE_18M;
361	case IEEE80211_OFDM_RATE_24MB:
362		return ZD_OFDM_RATE_24M;
363	case IEEE80211_OFDM_RATE_36MB:
364		return ZD_OFDM_RATE_36M;
365	case IEEE80211_OFDM_RATE_48MB:
366		return ZD_OFDM_RATE_48M;
367	case IEEE80211_OFDM_RATE_54MB:
368		return ZD_OFDM_RATE_54M;
369	}
370	return ZD_CCK_RATE_1M;
371}
372
373static u16 rate_to_cr_rate(u8 rate)
374{
375	switch (rate) {
376	case IEEE80211_CCK_RATE_2MB:
377		return CR_RATE_1M;
378	case IEEE80211_CCK_RATE_5MB:
379		return CR_RATE_5_5M;
380	case IEEE80211_CCK_RATE_11MB:
381		return CR_RATE_11M;
382	case IEEE80211_OFDM_RATE_6MB:
383		return CR_RATE_6M;
384	case IEEE80211_OFDM_RATE_9MB:
385		return CR_RATE_9M;
386	case IEEE80211_OFDM_RATE_12MB:
387		return CR_RATE_12M;
388	case IEEE80211_OFDM_RATE_18MB:
389		return CR_RATE_18M;
390	case IEEE80211_OFDM_RATE_24MB:
391		return CR_RATE_24M;
392	case IEEE80211_OFDM_RATE_36MB:
393		return CR_RATE_36M;
394	case IEEE80211_OFDM_RATE_48MB:
395		return CR_RATE_48M;
396	case IEEE80211_OFDM_RATE_54MB:
397		return CR_RATE_54M;
398	}
399	return CR_RATE_1M;
400}
401
402static void try_enable_tx(struct zd_mac *mac)
403{
404	unsigned long flags;
405
406	spin_lock_irqsave(&mac->lock, flags);
407	if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
408		netif_wake_queue(mac->netdev);
409	spin_unlock_irqrestore(&mac->lock, flags);
410}
411
412static void set_rts_cts_work(struct work_struct *work)
413{
414	struct zd_mac *mac =
415		container_of(work, struct zd_mac, set_rts_cts_work.work);
416	unsigned long flags;
417	u8 rts_rate;
418	unsigned int short_preamble;
419
420	mutex_lock(&mac->chip.mutex);
421
422	spin_lock_irqsave(&mac->lock, flags);
423	mac->updating_rts_rate = 0;
424	rts_rate = mac->rts_rate;
425	short_preamble = mac->short_preamble;
426	spin_unlock_irqrestore(&mac->lock, flags);
427
428	zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
429	mutex_unlock(&mac->chip.mutex);
430
431	try_enable_tx(mac);
432}
433
434static void set_basic_rates_work(struct work_struct *work)
435{
436	struct zd_mac *mac =
437		container_of(work, struct zd_mac, set_basic_rates_work.work);
438	unsigned long flags;
439	u16 basic_rates;
440
441	mutex_lock(&mac->chip.mutex);
442
443	spin_lock_irqsave(&mac->lock, flags);
444	mac->updating_basic_rates = 0;
445	basic_rates = mac->basic_rates;
446	spin_unlock_irqrestore(&mac->lock, flags);
447
448	zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
449	mutex_unlock(&mac->chip.mutex);
450
451	try_enable_tx(mac);
452}
453
454static void bssinfo_change(struct net_device *netdev, u32 changes)
455{
456	struct zd_mac *mac = zd_netdev_mac(netdev);
457	struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
458	struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
459	int need_set_rts_cts = 0;
460	int need_set_rates = 0;
461	u16 basic_rates;
462	unsigned long flags;
463
464	dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
465
466	if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
467		spin_lock_irqsave(&mac->lock, flags);
468		mac->short_preamble = bssinfo->short_preamble;
469		spin_unlock_irqrestore(&mac->lock, flags);
470		need_set_rts_cts = 1;
471	}
472
473	if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
474		/* Set RTS rate to highest available basic rate */
475		u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
476			&bssinfo->supported_rates, 1);
477		hi_rate = rate_to_zd_rate(hi_rate);
478
479		spin_lock_irqsave(&mac->lock, flags);
480		if (hi_rate != mac->rts_rate) {
481			mac->rts_rate = hi_rate;
482			need_set_rts_cts = 1;
483		}
484		spin_unlock_irqrestore(&mac->lock, flags);
485
486		/* Set basic rates */
487		need_set_rates = 1;
488		if (bssinfo->supported_rates.count == 0) {
489			/* Allow the device to be flexible */
490			basic_rates = CR_RATES_80211B | CR_RATES_80211G;
491		} else {
492			int i = 0;
493			basic_rates = 0;
494
495			for (i = 0; i < bssinfo->supported_rates.count; i++) {
496				u16 rate = bssinfo->supported_rates.rates[i];
497				if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
498					continue;
499
500				rate &= ~IEEE80211_BASIC_RATE_MASK;
501				basic_rates |= rate_to_cr_rate(rate);
502			}
503		}
504		spin_lock_irqsave(&mac->lock, flags);
505		mac->basic_rates = basic_rates;
506		spin_unlock_irqrestore(&mac->lock, flags);
507	}
508
509	/* Schedule any changes we made above */
510
511	spin_lock_irqsave(&mac->lock, flags);
512	if (need_set_rts_cts && !mac->updating_rts_rate) {
513		mac->updating_rts_rate = 1;
514		netif_stop_queue(mac->netdev);
515		queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
516	}
517	if (need_set_rates && !mac->updating_basic_rates) {
518		mac->updating_basic_rates = 1;
519		netif_stop_queue(mac->netdev);
520		queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
521				   0);
522	}
523	spin_unlock_irqrestore(&mac->lock, flags);
524}
525
526static void set_channel(struct net_device *netdev, u8 channel)
527{
528	struct zd_mac *mac = zd_netdev_mac(netdev);
529
530	dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
531
532	zd_chip_set_channel(&mac->chip, channel);
533}
534
535int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
536{
537	unsigned long lock_flags;
538	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
539
540	if (ieee->iw_mode == IW_MODE_INFRA)
541		return -EPERM;
542
543	spin_lock_irqsave(&mac->lock, lock_flags);
544	if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
545		spin_unlock_irqrestore(&mac->lock, lock_flags);
546		return -EINVAL;
547	}
548	mac->requested_channel = channel;
549	spin_unlock_irqrestore(&mac->lock, lock_flags);
550	if (netif_running(mac->netdev))
551		return zd_chip_set_channel(&mac->chip, channel);
552	else
553		return 0;
554}
555
556u8 zd_mac_get_channel(struct zd_mac *mac)
557{
558	u8 channel = zd_chip_get_channel(&mac->chip);
559
560	dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
561	return channel;
562}
563
564/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
565static u8 zd_rate_typed(u8 zd_rate)
566{
567	static const u8 typed_rates[16] = {
568		[ZD_CCK_RATE_1M]	= ZD_CS_CCK|ZD_CCK_RATE_1M,
569		[ZD_CCK_RATE_2M]	= ZD_CS_CCK|ZD_CCK_RATE_2M,
570		[ZD_CCK_RATE_5_5M]	= ZD_CS_CCK|ZD_CCK_RATE_5_5M,
571		[ZD_CCK_RATE_11M]	= ZD_CS_CCK|ZD_CCK_RATE_11M,
572		[ZD_OFDM_RATE_6M]	= ZD_CS_OFDM|ZD_OFDM_RATE_6M,
573		[ZD_OFDM_RATE_9M]	= ZD_CS_OFDM|ZD_OFDM_RATE_9M,
574		[ZD_OFDM_RATE_12M]	= ZD_CS_OFDM|ZD_OFDM_RATE_12M,
575		[ZD_OFDM_RATE_18M]	= ZD_CS_OFDM|ZD_OFDM_RATE_18M,
576		[ZD_OFDM_RATE_24M]	= ZD_CS_OFDM|ZD_OFDM_RATE_24M,
577		[ZD_OFDM_RATE_36M]	= ZD_CS_OFDM|ZD_OFDM_RATE_36M,
578		[ZD_OFDM_RATE_48M]	= ZD_CS_OFDM|ZD_OFDM_RATE_48M,
579		[ZD_OFDM_RATE_54M]	= ZD_CS_OFDM|ZD_OFDM_RATE_54M,
580	};
581
582	ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
583	return typed_rates[zd_rate & ZD_CS_RATE_MASK];
584}
585
586int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
587{
588	struct ieee80211_device *ieee;
589
590	switch (mode) {
591	case IW_MODE_AUTO:
592	case IW_MODE_ADHOC:
593	case IW_MODE_INFRA:
594		mac->netdev->type = ARPHRD_ETHER;
595		break;
596	case IW_MODE_MONITOR:
597		mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
598		break;
599	default:
600		dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
601		return -EINVAL;
602	}
603
604	ieee = zd_mac_to_ieee80211(mac);
605	ZD_ASSERT(!irqs_disabled());
606	spin_lock_irq(&ieee->lock);
607	ieee->iw_mode = mode;
608	spin_unlock_irq(&ieee->lock);
609
610	if (netif_running(mac->netdev))
611		return reset_mode(mac);
612
613	return 0;
614}
615
616int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
617{
618	unsigned long flags;
619	struct ieee80211_device *ieee;
620
621	ieee = zd_mac_to_ieee80211(mac);
622	spin_lock_irqsave(&ieee->lock, flags);
623	*mode = ieee->iw_mode;
624	spin_unlock_irqrestore(&ieee->lock, flags);
625	return 0;
626}
627
628int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
629{
630	int i;
631	const struct channel_range *channel_range;
632	u8 regdomain;
633
634	memset(range, 0, sizeof(*range));
635
636	range->throughput = 27 * 1000 * 1000;
637
638	range->max_qual.qual = 100;
639	range->max_qual.level = 100;
640
641	range->avg_qual.qual = 71;
642	range->avg_qual.level = 80;
643
644	range->min_rts = 256;
645	range->max_rts = 2346;
646
647	range->min_frag = MIN_FRAG_THRESHOLD;
648	range->max_frag = MAX_FRAG_THRESHOLD;
649
650	range->max_encoding_tokens = WEP_KEYS;
651	range->num_encoding_sizes = 2;
652	range->encoding_size[0] = 5;
653	range->encoding_size[1] = WEP_KEY_LEN;
654
655	range->we_version_compiled = WIRELESS_EXT;
656	range->we_version_source = 20;
657
658	range->enc_capa = IW_ENC_CAPA_WPA |  IW_ENC_CAPA_WPA2 |
659			  IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
660
661	ZD_ASSERT(!irqs_disabled());
662	spin_lock_irq(&mac->lock);
663	regdomain = mac->regdomain;
664	spin_unlock_irq(&mac->lock);
665	channel_range = zd_channel_range(regdomain);
666
667	range->num_channels = channel_range->end - channel_range->start;
668	range->old_num_channels = range->num_channels;
669	range->num_frequency = range->num_channels;
670	range->old_num_frequency = range->num_frequency;
671
672	for (i = 0; i < range->num_frequency; i++) {
673		struct iw_freq *freq = &range->freq[i];
674		freq->i = channel_range->start + i;
675		zd_channel_to_freq(freq, freq->i);
676	}
677
678	return 0;
679}
680
681static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
682{
683	static const u8 rate_divisor[] = {
684		[ZD_CCK_RATE_1M]	=  1,
685		[ZD_CCK_RATE_2M]	=  2,
686		[ZD_CCK_RATE_5_5M]	= 11, /* bits must be doubled */
687		[ZD_CCK_RATE_11M]	= 11,
688		[ZD_OFDM_RATE_6M]	=  6,
689		[ZD_OFDM_RATE_9M]	=  9,
690		[ZD_OFDM_RATE_12M]	= 12,
691		[ZD_OFDM_RATE_18M]	= 18,
692		[ZD_OFDM_RATE_24M]	= 24,
693		[ZD_OFDM_RATE_36M]	= 36,
694		[ZD_OFDM_RATE_48M]	= 48,
695		[ZD_OFDM_RATE_54M]	= 54,
696	};
697
698	u32 bits = (u32)tx_length * 8;
699	u32 divisor;
700
701	divisor = rate_divisor[zd_rate];
702	if (divisor == 0)
703		return -EINVAL;
704
705	switch (zd_rate) {
706	case ZD_CCK_RATE_5_5M:
707		bits = (2*bits) + 10; /* round up to the next integer */
708		break;
709	case ZD_CCK_RATE_11M:
710		if (service) {
711			u32 t = bits % 11;
712			*service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
713			if (0 < t && t <= 3) {
714				*service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
715			}
716		}
717		bits += 10; /* round up to the next integer */
718		break;
719	}
720
721	return bits/divisor;
722}
723
724enum {
725	R2M_SHORT_PREAMBLE = 0x01,
726	R2M_11A		   = 0x02,
727};
728
729static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
730{
731	u8 modulation;
732
733	modulation = zd_rate_typed(zd_rate);
734	if (flags & R2M_SHORT_PREAMBLE) {
735		switch (ZD_CS_RATE(modulation)) {
736		case ZD_CCK_RATE_2M:
737		case ZD_CCK_RATE_5_5M:
738		case ZD_CCK_RATE_11M:
739			modulation |= ZD_CS_CCK_PREA_SHORT;
740			return modulation;
741		}
742	}
743	if (flags & R2M_11A) {
744		if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
745			modulation |= ZD_CS_OFDM_MODE_11A;
746	}
747	return modulation;
748}
749
750static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
751	                      struct ieee80211_hdr_4addr *hdr)
752{
753	struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
754	u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
755	u8 rate, zd_rate;
756	int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
757	int is_multicast = is_multicast_ether_addr(hdr->addr1);
758	int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
759		is_multicast, is_mgt);
760	int flags = 0;
761
762	rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
763
764	if (short_preamble)
765		flags |= R2M_SHORT_PREAMBLE;
766
767	zd_rate = rate_to_zd_rate(rate);
768	cs->modulation = zd_rate_to_modulation(zd_rate, flags);
769}
770
771static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
772	                   struct ieee80211_hdr_4addr *header)
773{
774	struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
775	unsigned int tx_length = le16_to_cpu(cs->tx_length);
776	u16 fctl = le16_to_cpu(header->frame_ctl);
777	u16 ftype = WLAN_FC_GET_TYPE(fctl);
778	u16 stype = WLAN_FC_GET_STYPE(fctl);
779
780	/*
781	 * CONTROL TODO:
782	 * - if backoff needed, enable bit 0
783	 * - if burst (backoff not needed) disable bit 0
784	 */
785
786	cs->control = 0;
787
788	/* First fragment */
789	if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
790		cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
791
792	/* Multicast */
793	if (is_multicast_ether_addr(header->addr1))
794		cs->control |= ZD_CS_MULTICAST;
795
796	/* PS-POLL */
797	if (stype == IEEE80211_STYPE_PSPOLL)
798		cs->control |= ZD_CS_PS_POLL_FRAME;
799
800	/* Unicast data frames over the threshold should have RTS */
801	if (!is_multicast_ether_addr(header->addr1) &&
802	    	ftype != IEEE80211_FTYPE_MGMT &&
803		    tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
804		cs->control |= ZD_CS_RTS;
805
806	/* Use CTS-to-self protection if required */
807	if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
808			ieee80211softmac_protection_needed(softmac)) {
809		cs->control &= ~ZD_CS_RTS;
810		cs->control |= ZD_CS_SELF_CTS;
811	}
812
813}
814
815static int fill_ctrlset(struct zd_mac *mac,
816	                struct ieee80211_txb *txb,
817			int frag_num)
818{
819	int r;
820	struct sk_buff *skb = txb->fragments[frag_num];
821	struct ieee80211_hdr_4addr *hdr =
822		(struct ieee80211_hdr_4addr *) skb->data;
823	unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
824	unsigned int next_frag_len;
825	unsigned int packet_length;
826	struct zd_ctrlset *cs = (struct zd_ctrlset *)
827		skb_push(skb, sizeof(struct zd_ctrlset));
828
829	if (frag_num+1  < txb->nr_frags) {
830		next_frag_len = txb->fragments[frag_num+1]->len +
831			        IEEE80211_FCS_LEN;
832	} else {
833		next_frag_len = 0;
834	}
835	ZD_ASSERT(frag_len <= 0xffff);
836	ZD_ASSERT(next_frag_len <= 0xffff);
837
838	cs_set_modulation(mac, cs, hdr);
839
840	cs->tx_length = cpu_to_le16(frag_len);
841
842	cs_set_control(mac, cs, hdr);
843
844	packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
845	ZD_ASSERT(packet_length <= 0xffff);
846	/* ZD1211B: Computing the length difference this way, gives us
847	 * flexibility to compute the packet length.
848	 */
849	cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
850			packet_length - frag_len : packet_length);
851
852	/*
853	 * CURRENT LENGTH:
854	 * - transmit frame length in microseconds
855	 * - seems to be derived from frame length
856	 * - see Cal_Us_Service() in zdinlinef.h
857	 * - if macp->bTxBurstEnable is enabled, then multiply by 4
858	 *  - bTxBurstEnable is never set in the vendor driver
859	 *
860	 * SERVICE:
861	 * - "for PLCP configuration"
862	 * - always 0 except in some situations at 802.11b 11M
863	 * - see line 53 of zdinlinef.h
864	 */
865	cs->service = 0;
866	r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
867		                 le16_to_cpu(cs->tx_length));
868	if (r < 0)
869		return r;
870	cs->current_length = cpu_to_le16(r);
871
872	if (next_frag_len == 0) {
873		cs->next_frame_length = 0;
874	} else {
875		r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
876			                 next_frag_len);
877		if (r < 0)
878			return r;
879		cs->next_frame_length = cpu_to_le16(r);
880	}
881
882	return 0;
883}
884
885static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
886{
887	int i, r;
888	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
889
890	for (i = 0; i < txb->nr_frags; i++) {
891		struct sk_buff *skb = txb->fragments[i];
892
893		r = fill_ctrlset(mac, txb, i);
894		if (r) {
895			ieee->stats.tx_dropped++;
896			return r;
897		}
898		r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
899		if (r) {
900			ieee->stats.tx_dropped++;
901			return r;
902		}
903	}
904
905	mac->netdev->trans_start = jiffies;
906
907	ieee80211_txb_free(txb);
908	return 0;
909}
910
911struct zd_rt_hdr {
912	struct ieee80211_radiotap_header rt_hdr;
913	u8  rt_flags;
914	u8  rt_rate;
915	u16 rt_channel;
916	u16 rt_chbitmask;
917} __attribute__((packed));
918
919static void fill_rt_header(void *buffer, struct zd_mac *mac,
920	                   const struct ieee80211_rx_stats *stats,
921			   const struct rx_status *status)
922{
923	struct zd_rt_hdr *hdr = buffer;
924
925	hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
926	hdr->rt_hdr.it_pad = 0;
927	hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
928	hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
929		                 (1 << IEEE80211_RADIOTAP_CHANNEL) |
930				 (1 << IEEE80211_RADIOTAP_RATE));
931
932	hdr->rt_flags = 0;
933	if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
934		hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
935
936	hdr->rt_rate = stats->rate / 5;
937
938	hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
939		                             _zd_chip_get_channel(&mac->chip)));
940	hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
941		((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
942		ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
943}
944
945/* Returns 1 if the data packet is for us and 0 otherwise. */
946static int is_data_packet_for_us(struct ieee80211_device *ieee,
947	                         struct ieee80211_hdr_4addr *hdr)
948{
949	struct net_device *netdev = ieee->dev;
950	u16 fc = le16_to_cpu(hdr->frame_ctl);
951
952	ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
953
954	switch (ieee->iw_mode) {
955	case IW_MODE_ADHOC:
956		if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
957		    compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
958			return 0;
959		break;
960	case IW_MODE_AUTO:
961	case IW_MODE_INFRA:
962		if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
963		    IEEE80211_FCTL_FROMDS ||
964		    compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
965			return 0;
966		break;
967	default:
968		ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
969		return 0;
970	}
971
972	return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
973	       (is_multicast_ether_addr(hdr->addr1) &&
974		compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
975	       (netdev->flags & IFF_PROMISC);
976}
977
978/* Filters received packets. The function returns 1 if the packet should be
979 * forwarded to ieee80211_rx(). If the packet should be ignored the function
980 * returns 0. If an invalid packet is found the function returns -EINVAL.
981 *
982 * The function calls ieee80211_rx_mgt() directly.
983 *
984 * It has been based on ieee80211_rx_any.
985 */
986static int filter_rx(struct ieee80211_device *ieee,
987	             const u8 *buffer, unsigned int length,
988		     struct ieee80211_rx_stats *stats)
989{
990	struct ieee80211_hdr_4addr *hdr;
991	u16 fc;
992
993	if (ieee->iw_mode == IW_MODE_MONITOR)
994		return 1;
995
996	hdr = (struct ieee80211_hdr_4addr *)buffer;
997	fc = le16_to_cpu(hdr->frame_ctl);
998	if ((fc & IEEE80211_FCTL_VERS) != 0)
999		return -EINVAL;
1000
1001	switch (WLAN_FC_GET_TYPE(fc)) {
1002	case IEEE80211_FTYPE_MGMT:
1003		if (length < sizeof(struct ieee80211_hdr_3addr))
1004			return -EINVAL;
1005		ieee80211_rx_mgt(ieee, hdr, stats);
1006		return 0;
1007	case IEEE80211_FTYPE_CTL:
1008		return 0;
1009	case IEEE80211_FTYPE_DATA:
1010		/* Ignore invalid short buffers */
1011		if (length < sizeof(struct ieee80211_hdr_3addr))
1012			return -EINVAL;
1013		return is_data_packet_for_us(ieee, hdr);
1014	}
1015
1016	return -EINVAL;
1017}
1018
1019static void update_qual_rssi(struct zd_mac *mac,
1020			     const u8 *buffer, unsigned int length,
1021			     u8 qual_percent, u8 rssi_percent)
1022{
1023	unsigned long flags;
1024	struct ieee80211_hdr_3addr *hdr;
1025	int i;
1026
1027	hdr = (struct ieee80211_hdr_3addr *)buffer;
1028	if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1029		return;
1030	if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
1031		return;
1032
1033	spin_lock_irqsave(&mac->lock, flags);
1034	i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1035	mac->qual_buffer[i] = qual_percent;
1036	mac->rssi_buffer[i] = rssi_percent;
1037	mac->stats_count++;
1038	spin_unlock_irqrestore(&mac->lock, flags);
1039}
1040
1041static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1042	                 const struct rx_status **pstatus,
1043		         struct zd_mac *mac,
1044			 const u8 *buffer, unsigned int length)
1045{
1046	const struct rx_status *status;
1047
1048	*pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1049	if (status->frame_status & ZD_RX_ERROR) {
1050		struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1051		ieee->stats.rx_errors++;
1052		if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1053			ieee->stats.rx_missed_errors++;
1054		else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1055			ieee->stats.rx_fifo_errors++;
1056		else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1057			ieee->ieee_stats.rx_discards_undecryptable++;
1058		else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1059			ieee->stats.rx_crc_errors++;
1060			ieee->ieee_stats.rx_fcs_errors++;
1061		}
1062		else if (status->frame_status & ZD_RX_CRC16_ERROR)
1063			ieee->stats.rx_crc_errors++;
1064		return -EINVAL;
1065	}
1066
1067	memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1068	stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1069		               + sizeof(struct rx_status));
1070	stats->freq = IEEE80211_24GHZ_BAND;
1071	stats->received_channel = _zd_chip_get_channel(&mac->chip);
1072	stats->rssi = zd_rx_strength_percent(status->signal_strength);
1073	stats->signal = zd_rx_qual_percent(buffer,
1074		                          length - sizeof(struct rx_status),
1075		                          status);
1076	stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1077	stats->rate = zd_rx_rate(buffer, status);
1078	if (stats->rate)
1079		stats->mask |= IEEE80211_STATMASK_RATE;
1080
1081	return 0;
1082}
1083
1084static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1085{
1086	int r;
1087	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1088	struct ieee80211_rx_stats stats;
1089	const struct rx_status *status;
1090
1091	if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1092	               IEEE80211_FCS_LEN + sizeof(struct rx_status))
1093	{
1094		ieee->stats.rx_errors++;
1095		ieee->stats.rx_length_errors++;
1096		goto free_skb;
1097	}
1098
1099	r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1100	if (r) {
1101		/* Only packets with rx errors are included here.
1102		 * The error stats have already been set in fill_rx_stats.
1103		 */
1104		goto free_skb;
1105	}
1106
1107	__skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1108	__skb_trim(skb, skb->len -
1109		        (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1110
1111	update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1112		         status->signal_strength);
1113
1114	r = filter_rx(ieee, skb->data, skb->len, &stats);
1115	if (r <= 0) {
1116		if (r < 0) {
1117			ieee->stats.rx_errors++;
1118			dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1119		}
1120		goto free_skb;
1121	}
1122
1123	if (ieee->iw_mode == IW_MODE_MONITOR)
1124		fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1125			       &stats, status);
1126
1127	r = ieee80211_rx(ieee, skb, &stats);
1128	if (r)
1129		return;
1130free_skb:
1131	/* We are always in a soft irq. */
1132	dev_kfree_skb(skb);
1133}
1134
1135static void do_rx(unsigned long mac_ptr)
1136{
1137	struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1138	struct sk_buff *skb;
1139
1140	while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1141		zd_mac_rx(mac, skb);
1142}
1143
1144int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1145{
1146	struct sk_buff *skb;
1147
1148	skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1149	if (!skb) {
1150		struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1151		dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1152		ieee->stats.rx_dropped++;
1153		return -ENOMEM;
1154	}
1155	skb_reserve(skb, sizeof(struct zd_rt_hdr));
1156	memcpy(__skb_put(skb, length), buffer, length);
1157	skb_queue_tail(&mac->rx_queue, skb);
1158	tasklet_schedule(&mac->rx_tasklet);
1159	return 0;
1160}
1161
1162static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1163		     int pri)
1164{
1165	return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1166}
1167
1168static void set_security(struct net_device *netdev,
1169			 struct ieee80211_security *sec)
1170{
1171	struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1172	struct ieee80211_security *secinfo = &ieee->sec;
1173	int keyidx;
1174
1175	dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1176
1177	for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1178		if (sec->flags & (1<<keyidx)) {
1179			secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1180			secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1181			memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1182			       SCM_KEY_LEN);
1183		}
1184
1185	if (sec->flags & SEC_ACTIVE_KEY) {
1186		secinfo->active_key = sec->active_key;
1187		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1188			"   .active_key = %d\n", sec->active_key);
1189	}
1190	if (sec->flags & SEC_UNICAST_GROUP) {
1191		secinfo->unicast_uses_group = sec->unicast_uses_group;
1192		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1193			"   .unicast_uses_group = %d\n",
1194			sec->unicast_uses_group);
1195	}
1196	if (sec->flags & SEC_LEVEL) {
1197		secinfo->level = sec->level;
1198		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1199			"   .level = %d\n", sec->level);
1200	}
1201	if (sec->flags & SEC_ENABLED) {
1202		secinfo->enabled = sec->enabled;
1203		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1204			"   .enabled = %d\n", sec->enabled);
1205	}
1206	if (sec->flags & SEC_ENCRYPT) {
1207		secinfo->encrypt = sec->encrypt;
1208		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1209			"   .encrypt = %d\n", sec->encrypt);
1210	}
1211	if (sec->flags & SEC_AUTH_MODE) {
1212		secinfo->auth_mode = sec->auth_mode;
1213		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1214			"   .auth_mode = %d\n", sec->auth_mode);
1215	}
1216}
1217
1218static void ieee_init(struct ieee80211_device *ieee)
1219{
1220	ieee->mode = IEEE_B | IEEE_G;
1221	ieee->freq_band = IEEE80211_24GHZ_BAND;
1222	ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1223	ieee->tx_headroom = sizeof(struct zd_ctrlset);
1224	ieee->set_security = set_security;
1225	ieee->hard_start_xmit = netdev_tx;
1226
1227	/* Software encryption/decryption for now */
1228	ieee->host_build_iv = 0;
1229	ieee->host_encrypt = 1;
1230	ieee->host_decrypt = 1;
1231
1232	ieee->iw_mode = IW_MODE_INFRA;
1233}
1234
1235static void softmac_init(struct ieee80211softmac_device *sm)
1236{
1237	sm->set_channel = set_channel;
1238	sm->bssinfo_change = bssinfo_change;
1239}
1240
1241struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1242{
1243	struct zd_mac *mac = zd_netdev_mac(ndev);
1244	struct iw_statistics *iw_stats = &mac->iw_stats;
1245	unsigned int i, count, qual_total, rssi_total;
1246
1247	memset(iw_stats, 0, sizeof(struct iw_statistics));
1248	/* We are not setting the status, because ieee->state is not updated
1249	 * at all and this driver doesn't track authentication state.
1250	 */
1251	spin_lock_irq(&mac->lock);
1252	count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1253		mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1254	qual_total = rssi_total = 0;
1255	for (i = 0; i < count; i++) {
1256		qual_total += mac->qual_buffer[i];
1257		rssi_total += mac->rssi_buffer[i];
1258	}
1259	spin_unlock_irq(&mac->lock);
1260	iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1261	if (count > 0) {
1262		iw_stats->qual.qual = qual_total / count;
1263		iw_stats->qual.level = rssi_total / count;
1264		iw_stats->qual.updated |=
1265			IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1266	} else {
1267		iw_stats->qual.updated |=
1268			IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1269	}
1270	/* TODO: update counter */
1271	return iw_stats;
1272}
1273
1274#define LINK_LED_WORK_DELAY HZ
1275
1276static void link_led_handler(struct work_struct *work)
1277{
1278	struct zd_mac *mac =
1279		container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1280	struct zd_chip *chip = &mac->chip;
1281	struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1282	int is_associated;
1283	int r;
1284
1285	spin_lock_irq(&mac->lock);
1286	is_associated = sm->associnfo.associated != 0;
1287	spin_unlock_irq(&mac->lock);
1288
1289	r = zd_chip_control_leds(chip,
1290		                 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1291	if (r)
1292		dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1293
1294	queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1295		           LINK_LED_WORK_DELAY);
1296}
1297
1298static void housekeeping_init(struct zd_mac *mac)
1299{
1300	INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1301}
1302
1303static void housekeeping_enable(struct zd_mac *mac)
1304{
1305	dev_dbg_f(zd_mac_dev(mac), "\n");
1306	queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1307			   0);
1308}
1309
1310static void housekeeping_disable(struct zd_mac *mac)
1311{
1312	dev_dbg_f(zd_mac_dev(mac), "\n");
1313	cancel_rearming_delayed_workqueue(zd_workqueue,
1314		&mac->housekeeping.link_led_work);
1315	zd_chip_control_leds(&mac->chip, LED_OFF);
1316}
1317