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