zd1211rw: Add ID for Trendnet TEW-429UB A
[deliverable/linux.git] / drivers / net / wireless / zd1211rw / zd_mac.c
CommitLineData
e85d0918 1/* zd_mac.c
459c51ad
DD
2 *
3 * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
e85d0918
DD
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
e85d0918
DD
22#include <linux/usb.h>
23#include <linux/jiffies.h>
24#include <net/ieee80211_radiotap.h>
25
26#include "zd_def.h"
27#include "zd_chip.h"
28#include "zd_mac.h"
29#include "zd_ieee80211.h"
e85d0918 30#include "zd_rf.h"
e85d0918 31
459c51ad
DD
32/* This table contains the hardware specific values for the modulation rates. */
33static const struct ieee80211_rate zd_rates[] = {
34 { .rate = 10,
35 .val = ZD_CCK_RATE_1M,
36 .flags = IEEE80211_RATE_CCK },
37 { .rate = 20,
38 .val = ZD_CCK_RATE_2M,
39 .val2 = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
40 .flags = IEEE80211_RATE_CCK_2 },
41 { .rate = 55,
42 .val = ZD_CCK_RATE_5_5M,
43 .val2 = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
44 .flags = IEEE80211_RATE_CCK_2 },
45 { .rate = 110,
46 .val = ZD_CCK_RATE_11M,
47 .val2 = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
48 .flags = IEEE80211_RATE_CCK_2 },
49 { .rate = 60,
50 .val = ZD_OFDM_RATE_6M,
51 .flags = IEEE80211_RATE_OFDM },
52 { .rate = 90,
53 .val = ZD_OFDM_RATE_9M,
54 .flags = IEEE80211_RATE_OFDM },
55 { .rate = 120,
56 .val = ZD_OFDM_RATE_12M,
57 .flags = IEEE80211_RATE_OFDM },
58 { .rate = 180,
59 .val = ZD_OFDM_RATE_18M,
60 .flags = IEEE80211_RATE_OFDM },
61 { .rate = 240,
62 .val = ZD_OFDM_RATE_24M,
63 .flags = IEEE80211_RATE_OFDM },
64 { .rate = 360,
65 .val = ZD_OFDM_RATE_36M,
66 .flags = IEEE80211_RATE_OFDM },
67 { .rate = 480,
68 .val = ZD_OFDM_RATE_48M,
69 .flags = IEEE80211_RATE_OFDM },
70 { .rate = 540,
71 .val = ZD_OFDM_RATE_54M,
72 .flags = IEEE80211_RATE_OFDM },
73};
74
75static const struct ieee80211_channel zd_channels[] = {
76 { .chan = 1,
77 .freq = 2412},
78 { .chan = 2,
79 .freq = 2417},
80 { .chan = 3,
81 .freq = 2422},
82 { .chan = 4,
83 .freq = 2427},
84 { .chan = 5,
85 .freq = 2432},
86 { .chan = 6,
87 .freq = 2437},
88 { .chan = 7,
89 .freq = 2442},
90 { .chan = 8,
91 .freq = 2447},
92 { .chan = 9,
93 .freq = 2452},
94 { .chan = 10,
95 .freq = 2457},
96 { .chan = 11,
97 .freq = 2462},
98 { .chan = 12,
99 .freq = 2467},
100 { .chan = 13,
101 .freq = 2472},
102 { .chan = 14,
103 .freq = 2484}
104};
e85d0918 105
583afd1e
UK
106static void housekeeping_init(struct zd_mac *mac);
107static void housekeeping_enable(struct zd_mac *mac);
108static void housekeeping_disable(struct zd_mac *mac);
109
459c51ad 110int zd_mac_preinit_hw(struct ieee80211_hw *hw)
e85d0918
DD
111{
112 int r;
e85d0918 113 u8 addr[ETH_ALEN];
459c51ad 114 struct zd_mac *mac = zd_hw_mac(hw);
74553aed
DD
115
116 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
117 if (r)
118 return r;
119
459c51ad
DD
120 SET_IEEE80211_PERM_ADDR(hw, addr);
121
74553aed
DD
122 return 0;
123}
124
459c51ad 125int zd_mac_init_hw(struct ieee80211_hw *hw)
74553aed
DD
126{
127 int r;
459c51ad 128 struct zd_mac *mac = zd_hw_mac(hw);
74553aed 129 struct zd_chip *chip = &mac->chip;
e85d0918
DD
130 u8 default_regdomain;
131
132 r = zd_chip_enable_int(chip);
133 if (r)
134 goto out;
74553aed 135 r = zd_chip_init_hw(chip);
e85d0918
DD
136 if (r)
137 goto disable_int;
138
e85d0918 139 ZD_ASSERT(!irqs_disabled());
e85d0918
DD
140
141 r = zd_read_regdomain(chip, &default_regdomain);
142 if (r)
143 goto disable_int;
e85d0918
DD
144 spin_lock_irq(&mac->lock);
145 mac->regdomain = mac->default_regdomain = default_regdomain;
146 spin_unlock_irq(&mac->lock);
e85d0918 147
40da08bc
DD
148 /* We must inform the device that we are doing encryption/decryption in
149 * software at the moment. */
150 r = zd_set_encryption_type(chip, ENC_SNIFFER);
e85d0918
DD
151 if (r)
152 goto disable_int;
153
459c51ad 154 zd_geo_init(hw, mac->regdomain);
e85d0918
DD
155
156 r = 0;
157disable_int:
158 zd_chip_disable_int(chip);
159out:
160 return r;
161}
162
163void zd_mac_clear(struct zd_mac *mac)
164{
9cdac965 165 flush_workqueue(zd_workqueue);
e85d0918 166 zd_chip_clear(&mac->chip);
c48cf125
UK
167 ZD_ASSERT(!spin_is_locked(&mac->lock));
168 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
e85d0918
DD
169}
170
c5691235 171static int set_rx_filter(struct zd_mac *mac)
e85d0918 172{
459c51ad
DD
173 unsigned long flags;
174 u32 filter = STA_RX_FILTER;
e85d0918 175
459c51ad
DD
176 spin_lock_irqsave(&mac->lock, flags);
177 if (mac->pass_ctrl)
178 filter |= RX_FILTER_CTRL;
179 spin_unlock_irqrestore(&mac->lock, flags);
180
181 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
c5691235
UK
182}
183
184static int set_mc_hash(struct zd_mac *mac)
185{
186 struct zd_mc_hash hash;
c5691235 187 zd_mc_clear(&hash);
c5691235
UK
188 return zd_chip_set_multicast_hash(&mac->chip, &hash);
189}
190
459c51ad 191static int zd_op_start(struct ieee80211_hw *hw)
e85d0918 192{
459c51ad 193 struct zd_mac *mac = zd_hw_mac(hw);
e85d0918 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
e85d0918
DD
204 r = zd_chip_enable_int(chip);
205 if (r < 0)
206 goto out;
207
208 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
209 if (r < 0)
210 goto disable_int;
c5691235 211 r = set_rx_filter(mac);
c5691235
UK
212 if (r)
213 goto disable_int;
214 r = set_mc_hash(mac);
e85d0918
DD
215 if (r)
216 goto disable_int;
217 r = zd_chip_switch_radio_on(chip);
218 if (r < 0)
219 goto disable_int;
459c51ad 220 r = zd_chip_enable_rxtx(chip);
e85d0918
DD
221 if (r < 0)
222 goto disable_radio;
223 r = zd_chip_enable_hwint(chip);
224 if (r < 0)
459c51ad 225 goto disable_rxtx;
e85d0918 226
583afd1e 227 housekeeping_enable(mac);
e85d0918 228 return 0;
459c51ad
DD
229disable_rxtx:
230 zd_chip_disable_rxtx(chip);
e85d0918
DD
231disable_radio:
232 zd_chip_switch_radio_off(chip);
233disable_int:
234 zd_chip_disable_int(chip);
235out:
236 return r;
237}
238
459c51ad
DD
239/**
240 * clear_tx_skb_control_block - clears the control block of tx skbuffs
241 * @skb: a &struct sk_buff pointer
242 *
243 * This clears the control block of skbuff buffers, which were transmitted to
244 * the device. Notify that the function is not thread-safe, so prevent
245 * multiple calls.
246 */
247static void clear_tx_skb_control_block(struct sk_buff *skb)
248{
249 struct zd_tx_skb_control_block *cb =
250 (struct zd_tx_skb_control_block *)skb->cb;
251
252 kfree(cb->control);
253 cb->control = NULL;
254}
255
256/**
257 * kfree_tx_skb - frees a tx skbuff
258 * @skb: a &struct sk_buff pointer
259 *
260 * Frees the tx skbuff. Frees also the allocated control structure in the
261 * control block if necessary.
262 */
263static void kfree_tx_skb(struct sk_buff *skb)
e85d0918 264{
459c51ad
DD
265 clear_tx_skb_control_block(skb);
266 dev_kfree_skb_any(skb);
267}
e85d0918 268
459c51ad
DD
269static void zd_op_stop(struct ieee80211_hw *hw)
270{
271 struct zd_mac *mac = zd_hw_mac(hw);
272 struct zd_chip *chip = &mac->chip;
273 struct sk_buff *skb;
274 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
c9a4b35d 275
459c51ad 276 /* The order here deliberately is a little different from the open()
e85d0918 277 * method, since we need to make sure there is no opportunity for RX
459c51ad 278 * frames to be processed by mac80211 after we have stopped it.
e85d0918
DD
279 */
280
459c51ad 281 zd_chip_disable_rxtx(chip);
583afd1e 282 housekeeping_disable(mac);
b1382ede 283 flush_workqueue(zd_workqueue);
b1382ede 284
e85d0918
DD
285 zd_chip_disable_hwint(chip);
286 zd_chip_switch_radio_off(chip);
287 zd_chip_disable_int(chip);
288
e85d0918 289
459c51ad
DD
290 while ((skb = skb_dequeue(ack_wait_queue)))
291 kfree_tx_skb(skb);
9cdac965
UK
292}
293
459c51ad
DD
294/**
295 * init_tx_skb_control_block - initializes skb control block
296 * @skb: a &sk_buff pointer
297 * @dev: pointer to the mac80221 device
298 * @control: mac80211 tx control applying for the frame in @skb
299 *
300 * Initializes the control block of the skbuff to be transmitted.
301 */
302static int init_tx_skb_control_block(struct sk_buff *skb,
303 struct ieee80211_hw *hw,
304 struct ieee80211_tx_control *control)
305{
306 struct zd_tx_skb_control_block *cb =
307 (struct zd_tx_skb_control_block *)skb->cb;
308
309 ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb));
310 memset(cb, 0, sizeof(*cb));
311 cb->hw= hw;
312 cb->control = kmalloc(sizeof(*control), GFP_ATOMIC);
313 if (cb->control == NULL)
314 return -ENOMEM;
315 memcpy(cb->control, control, sizeof(*control));
e85d0918
DD
316
317 return 0;
318}
319
459c51ad
DD
320/**
321 * tx_status - reports tx status of a packet if required
322 * @hw - a &struct ieee80211_hw pointer
323 * @skb - a sk-buffer
324 * @status - the tx status of the packet without control information
325 * @success - True for successfull transmission of the frame
326 *
327 * This information calls ieee80211_tx_status_irqsafe() if required by the
328 * control information. It copies the control information into the status
329 * information.
330 *
331 * If no status information has been requested, the skb is freed.
332 */
333static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
334 struct ieee80211_tx_status *status,
335 bool success)
b1382ede 336{
459c51ad
DD
337 struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
338 skb->cb;
b1382ede 339
459c51ad
DD
340 ZD_ASSERT(cb->control != NULL);
341 memcpy(&status->control, cb->control, sizeof(status->control));
342 if (!success)
343 status->excessive_retries = 1;
344 clear_tx_skb_control_block(skb);
345 ieee80211_tx_status_irqsafe(hw, skb, status);
b1382ede
DD
346}
347
459c51ad
DD
348/**
349 * zd_mac_tx_failed - callback for failed frames
350 * @dev: the mac80211 wireless device
351 *
352 * This function is called if a frame couldn't be succesfully be
353 * transferred. The first frame from the tx queue, will be selected and
354 * reported as error to the upper layers.
355 */
356void zd_mac_tx_failed(struct ieee80211_hw *hw)
b1382ede 357{
459c51ad
DD
358 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
359 struct sk_buff *skb;
360 struct ieee80211_tx_status status = {{0}};
b1382ede 361
459c51ad
DD
362 skb = skb_dequeue(q);
363 if (skb == NULL)
364 return;
365 tx_status(hw, skb, &status, 0);
b1382ede
DD
366}
367
459c51ad
DD
368/**
369 * zd_mac_tx_to_dev - callback for USB layer
370 * @skb: a &sk_buff pointer
371 * @error: error value, 0 if transmission successful
372 *
373 * Informs the MAC layer that the frame has successfully transferred to the
374 * device. If an ACK is required and the transfer to the device has been
375 * successful, the packets are put on the @ack_wait_queue with
376 * the control set removed.
377 */
378void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
379{
380 struct zd_tx_skb_control_block *cb =
381 (struct zd_tx_skb_control_block *)skb->cb;
382 struct ieee80211_hw *hw = cb->hw;
383
384 if (likely(cb->control)) {
385 skb_pull(skb, sizeof(struct zd_ctrlset));
386 if (unlikely(error ||
387 (cb->control->flags & IEEE80211_TXCTL_NO_ACK)))
388 {
389 struct ieee80211_tx_status status = {{0}};
390 tx_status(hw, skb, &status, !error);
b1382ede 391 } else {
459c51ad
DD
392 struct sk_buff_head *q =
393 &zd_hw_mac(hw)->ack_wait_queue;
b1382ede 394
459c51ad
DD
395 skb_queue_tail(q, skb);
396 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
397 zd_mac_tx_failed(hw);
b1382ede 398 }
459c51ad
DD
399 } else {
400 kfree_tx_skb(skb);
e85d0918 401 }
e85d0918
DD
402}
403
b1cd8416 404static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
e85d0918 405{
64f222cc 406 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
459c51ad
DD
407 * the zd-rate values.
408 */
e85d0918 409 static const u8 rate_divisor[] = {
459c51ad
DD
410 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
411 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
412 /* Bits must be doubled. */
413 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
414 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
415 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
416 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
417 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
418 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
419 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
420 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
421 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
422 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
e85d0918
DD
423 };
424
425 u32 bits = (u32)tx_length * 8;
426 u32 divisor;
427
64f222cc 428 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
e85d0918
DD
429 if (divisor == 0)
430 return -EINVAL;
431
b1cd8416
DD
432 switch (zd_rate) {
433 case ZD_CCK_RATE_5_5M:
e85d0918
DD
434 bits = (2*bits) + 10; /* round up to the next integer */
435 break;
b1cd8416 436 case ZD_CCK_RATE_11M:
e85d0918
DD
437 if (service) {
438 u32 t = bits % 11;
439 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
440 if (0 < t && t <= 3) {
441 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
442 }
443 }
444 bits += 10; /* round up to the next integer */
445 break;
446 }
447
448 return bits/divisor;
449}
450
e85d0918 451static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
459c51ad 452 struct ieee80211_hdr *header, u32 flags)
e85d0918 453{
459c51ad 454 u16 fctl = le16_to_cpu(header->frame_control);
e85d0918
DD
455
456 /*
b1382ede 457 * CONTROL TODO:
e85d0918
DD
458 * - if backoff needed, enable bit 0
459 * - if burst (backoff not needed) disable bit 0
e85d0918
DD
460 */
461
462 cs->control = 0;
463
464 /* First fragment */
459c51ad 465 if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
e85d0918
DD
466 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
467
468 /* Multicast */
469 if (is_multicast_ether_addr(header->addr1))
470 cs->control |= ZD_CS_MULTICAST;
471
472 /* PS-POLL */
459c51ad
DD
473 if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
474 (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
e85d0918
DD
475 cs->control |= ZD_CS_PS_POLL_FRAME;
476
459c51ad 477 if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
b1382ede
DD
478 cs->control |= ZD_CS_RTS;
479
459c51ad 480 if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
b1382ede 481 cs->control |= ZD_CS_SELF_CTS;
e85d0918
DD
482
483 /* FIXME: Management frame? */
484}
485
486static int fill_ctrlset(struct zd_mac *mac,
459c51ad
DD
487 struct sk_buff *skb,
488 struct ieee80211_tx_control *control)
e85d0918
DD
489{
490 int r;
459c51ad
DD
491 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
492 unsigned int frag_len = skb->len + FCS_LEN;
e85d0918
DD
493 unsigned int packet_length;
494 struct zd_ctrlset *cs = (struct zd_ctrlset *)
495 skb_push(skb, sizeof(struct zd_ctrlset));
496
e85d0918 497 ZD_ASSERT(frag_len <= 0xffff);
e85d0918 498
459c51ad 499 cs->modulation = control->tx_rate;
e85d0918
DD
500
501 cs->tx_length = cpu_to_le16(frag_len);
502
459c51ad 503 cs_set_control(mac, cs, hdr, control->flags);
e85d0918
DD
504
505 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
506 ZD_ASSERT(packet_length <= 0xffff);
507 /* ZD1211B: Computing the length difference this way, gives us
508 * flexibility to compute the packet length.
509 */
74553aed 510 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
e85d0918
DD
511 packet_length - frag_len : packet_length);
512
513 /*
514 * CURRENT LENGTH:
515 * - transmit frame length in microseconds
516 * - seems to be derived from frame length
517 * - see Cal_Us_Service() in zdinlinef.h
518 * - if macp->bTxBurstEnable is enabled, then multiply by 4
519 * - bTxBurstEnable is never set in the vendor driver
520 *
521 * SERVICE:
522 * - "for PLCP configuration"
523 * - always 0 except in some situations at 802.11b 11M
524 * - see line 53 of zdinlinef.h
525 */
526 cs->service = 0;
64f222cc 527 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
e85d0918
DD
528 le16_to_cpu(cs->tx_length));
529 if (r < 0)
530 return r;
531 cs->current_length = cpu_to_le16(r);
459c51ad 532 cs->next_frame_length = 0;
e85d0918
DD
533
534 return 0;
535}
536
459c51ad
DD
537/**
538 * zd_op_tx - transmits a network frame to the device
539 *
540 * @dev: mac80211 hardware device
541 * @skb: socket buffer
542 * @control: the control structure
543 *
544 * This function transmit an IEEE 802.11 network frame to the device. The
545 * control block of the skbuff will be initialized. If necessary the incoming
546 * mac80211 queues will be stopped.
547 */
548static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
549 struct ieee80211_tx_control *control)
e85d0918 550{
459c51ad
DD
551 struct zd_mac *mac = zd_hw_mac(hw);
552 int r;
e85d0918 553
459c51ad
DD
554 r = fill_ctrlset(mac, skb, control);
555 if (r)
556 return r;
e85d0918 557
459c51ad
DD
558 r = init_tx_skb_control_block(skb, hw, control);
559 if (r)
560 return r;
561 r = zd_usb_tx(&mac->chip.usb, skb);
562 if (r) {
563 clear_tx_skb_control_block(skb);
564 return r;
e85d0918 565 }
e85d0918
DD
566 return 0;
567}
568
459c51ad
DD
569/**
570 * filter_ack - filters incoming packets for acknowledgements
571 * @dev: the mac80211 device
572 * @rx_hdr: received header
573 * @stats: the status for the received packet
741fec53 574 *
459c51ad
DD
575 * This functions looks for ACK packets and tries to match them with the
576 * frames in the tx queue. If a match is found the frame will be dequeued and
577 * the upper layers is informed about the successful transmission. If
578 * mac80211 queues have been stopped and the number of frames still to be
579 * transmitted is low the queues will be opened again.
e85d0918 580 *
459c51ad 581 * Returns 1 if the frame was an ACK, 0 if it was ignored.
e85d0918 582 */
459c51ad
DD
583static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
584 struct ieee80211_rx_status *stats)
e85d0918 585{
459c51ad
DD
586 u16 fc = le16_to_cpu(rx_hdr->frame_control);
587 struct sk_buff *skb;
588 struct sk_buff_head *q;
589 unsigned long flags;
e85d0918 590
459c51ad
DD
591 if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
592 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
e85d0918 593 return 0;
e85d0918 594
459c51ad
DD
595 q = &zd_hw_mac(hw)->ack_wait_queue;
596 spin_lock_irqsave(&q->lock, flags);
597 for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
598 struct ieee80211_hdr *tx_hdr;
599
600 tx_hdr = (struct ieee80211_hdr *)skb->data;
601 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
602 {
603 struct ieee80211_tx_status status = {{0}};
604 status.flags = IEEE80211_TX_STATUS_ACK;
605 status.ack_signal = stats->ssi;
606 __skb_unlink(skb, q);
607 tx_status(hw, skb, &status, 1);
608 goto out;
609 }
610 }
611out:
612 spin_unlock_irqrestore(&q->lock, flags);
613 return 1;
e85d0918
DD
614}
615
459c51ad 616int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
e85d0918 617{
459c51ad
DD
618 struct zd_mac *mac = zd_hw_mac(hw);
619 struct ieee80211_rx_status stats;
620 const struct rx_status *status;
621 struct sk_buff *skb;
622 int bad_frame = 0;
db888aed 623
459c51ad
DD
624 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
625 FCS_LEN + sizeof(struct rx_status))
626 return -EINVAL;
e85d0918 627
459c51ad 628 memset(&stats, 0, sizeof(stats));
e85d0918 629
459c51ad
DD
630 /* Note about pass_failed_fcs and pass_ctrl access below:
631 * mac locking intentionally omitted here, as this is the only unlocked
632 * reader and the only writer is configure_filter. Plus, if there were
633 * any races accessing these variables, it wouldn't really matter.
634 * If mac80211 ever provides a way for us to access filter flags
635 * from outside configure_filter, we could improve on this. Also, this
636 * situation may change once we implement some kind of DMA-into-skb
637 * RX path. */
e85d0918 638
459c51ad
DD
639 /* Caller has to ensure that length >= sizeof(struct rx_status). */
640 status = (struct rx_status *)
937a049d 641 (buffer + (length - sizeof(struct rx_status)));
e85d0918 642 if (status->frame_status & ZD_RX_ERROR) {
459c51ad
DD
643 if (mac->pass_failed_fcs &&
644 (status->frame_status & ZD_RX_CRC32_ERROR)) {
645 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
646 bad_frame = 1;
647 } else {
648 return -EINVAL;
22d3405f 649 }
e85d0918 650 }
22d3405f 651
459c51ad
DD
652 stats.channel = _zd_chip_get_channel(&mac->chip);
653 stats.freq = zd_channels[stats.channel - 1].freq;
654 stats.phymode = MODE_IEEE80211G;
655 stats.ssi = status->signal_strength;
656 stats.signal = zd_rx_qual_percent(buffer,
e85d0918
DD
657 length - sizeof(struct rx_status),
658 status);
459c51ad
DD
659 stats.rate = zd_rx_rate(buffer, status);
660
661 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
662 buffer += ZD_PLCP_HEADER_SIZE;
663
664 /* Except for bad frames, filter each frame to see if it is an ACK, in
665 * which case our internal TX tracking is updated. Normally we then
666 * bail here as there's no need to pass ACKs on up to the stack, but
667 * there is also the case where the stack has requested us to pass
668 * control frames on up (pass_ctrl) which we must consider. */
669 if (!bad_frame &&
670 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
671 && !mac->pass_ctrl)
672 return 0;
e85d0918 673
459c51ad
DD
674 skb = dev_alloc_skb(length);
675 if (skb == NULL)
676 return -ENOMEM;
677 memcpy(skb_put(skb, length), buffer, length);
678
679 ieee80211_rx_irqsafe(hw, skb, &stats);
e85d0918
DD
680 return 0;
681}
682
459c51ad
DD
683static int zd_op_add_interface(struct ieee80211_hw *hw,
684 struct ieee80211_if_init_conf *conf)
e85d0918 685{
459c51ad 686 struct zd_mac *mac = zd_hw_mac(hw);
e85d0918 687
459c51ad
DD
688 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
689 if (mac->type != IEEE80211_IF_TYPE_INVALID)
690 return -EOPNOTSUPP;
e85d0918 691
459c51ad
DD
692 switch (conf->type) {
693 case IEEE80211_IF_TYPE_MNTR:
694 case IEEE80211_IF_TYPE_STA:
695 mac->type = conf->type;
696 break;
697 default:
698 return -EOPNOTSUPP;
4d1feabc 699 }
e85d0918 700
459c51ad
DD
701 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
702}
e85d0918 703
459c51ad
DD
704static void zd_op_remove_interface(struct ieee80211_hw *hw,
705 struct ieee80211_if_init_conf *conf)
706{
707 struct zd_mac *mac = zd_hw_mac(hw);
708 mac->type = IEEE80211_IF_TYPE_INVALID;
709 zd_write_mac_addr(&mac->chip, NULL);
710}
93137943 711
459c51ad
DD
712static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
713{
714 struct zd_mac *mac = zd_hw_mac(hw);
715 return zd_chip_set_channel(&mac->chip, conf->channel);
716}
db888aed 717
459c51ad
DD
718static int zd_op_config_interface(struct ieee80211_hw *hw, int if_id,
719 struct ieee80211_if_conf *conf)
720{
721 struct zd_mac *mac = zd_hw_mac(hw);
e85d0918 722
459c51ad
DD
723 spin_lock_irq(&mac->lock);
724 mac->associated = is_valid_ether_addr(conf->bssid);
725 spin_unlock_irq(&mac->lock);
e85d0918 726
459c51ad
DD
727 /* TODO: do hardware bssid filtering */
728 return 0;
4d1feabc
UK
729}
730
459c51ad 731static void set_multicast_hash_handler(struct work_struct *work)
4d1feabc 732{
459c51ad
DD
733 struct zd_mac *mac =
734 container_of(work, struct zd_mac, set_multicast_hash_work);
735 struct zd_mc_hash hash;
4d1feabc 736
459c51ad
DD
737 spin_lock_irq(&mac->lock);
738 hash = mac->multicast_hash;
739 spin_unlock_irq(&mac->lock);
4d1feabc 740
459c51ad 741 zd_chip_set_multicast_hash(&mac->chip, &hash);
e85d0918
DD
742}
743
459c51ad 744static void set_rx_filter_handler(struct work_struct *work)
e85d0918 745{
459c51ad
DD
746 struct zd_mac *mac =
747 container_of(work, struct zd_mac, set_rx_filter_work);
748 int r;
749
750 dev_dbg_f(zd_mac_dev(mac), "\n");
751 r = set_rx_filter(mac);
752 if (r)
753 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
e85d0918
DD
754}
755
459c51ad
DD
756#define SUPPORTED_FIF_FLAGS \
757 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
758 FIF_OTHER_BSS)
759static void zd_op_configure_filter(struct ieee80211_hw *hw,
760 unsigned int changed_flags,
761 unsigned int *new_flags,
762 int mc_count, struct dev_mc_list *mclist)
e85d0918 763{
459c51ad
DD
764 struct zd_mc_hash hash;
765 struct zd_mac *mac = zd_hw_mac(hw);
766 unsigned long flags;
767 int i;
e85d0918 768
459c51ad
DD
769 /* Only deal with supported flags */
770 changed_flags &= SUPPORTED_FIF_FLAGS;
771 *new_flags &= SUPPORTED_FIF_FLAGS;
772
773 /* changed_flags is always populated but this driver
774 * doesn't support all FIF flags so its possible we don't
775 * need to do anything */
776 if (!changed_flags)
777 return;
778
779 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
780 zd_mc_add_all(&hash);
781 } else {
782 DECLARE_MAC_BUF(macbuf);
783
784 zd_mc_clear(&hash);
785 for (i = 0; i < mc_count; i++) {
786 if (!mclist)
787 break;
788 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
789 print_mac(macbuf, mclist->dmi_addr));
790 zd_mc_add_addr(&hash, mclist->dmi_addr);
791 mclist = mclist->next;
792 }
e85d0918 793 }
459c51ad
DD
794
795 spin_lock_irqsave(&mac->lock, flags);
796 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
797 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
798 mac->multicast_hash = hash;
799 spin_unlock_irqrestore(&mac->lock, flags);
800 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
801
802 if (changed_flags & FIF_CONTROL)
803 queue_work(zd_workqueue, &mac->set_rx_filter_work);
804
805 /* no handling required for FIF_OTHER_BSS as we don't currently
806 * do BSSID filtering */
807 /* FIXME: in future it would be nice to enable the probe response
808 * filter (so that the driver doesn't see them) until
809 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
810 * have to schedule work to enable prbresp reception, which might
811 * happen too late. For now we'll just listen and forward them all the
812 * time. */
e85d0918
DD
813}
814
459c51ad 815static void set_rts_cts_work(struct work_struct *work)
e85d0918 816{
459c51ad
DD
817 struct zd_mac *mac =
818 container_of(work, struct zd_mac, set_rts_cts_work);
819 unsigned long flags;
820 unsigned int short_preamble;
821
822 mutex_lock(&mac->chip.mutex);
823
824 spin_lock_irqsave(&mac->lock, flags);
825 mac->updating_rts_rate = 0;
826 short_preamble = mac->short_preamble;
827 spin_unlock_irqrestore(&mac->lock, flags);
828
829 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
830 mutex_unlock(&mac->chip.mutex);
e85d0918
DD
831}
832
459c51ad
DD
833static void zd_op_erp_ie_changed(struct ieee80211_hw *hw, u8 changes,
834 int cts_protection, int preamble)
e85d0918 835{
459c51ad
DD
836 struct zd_mac *mac = zd_hw_mac(hw);
837 unsigned long flags;
838
839 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
840
841 if (changes & IEEE80211_ERP_CHANGE_PREAMBLE) {
842 spin_lock_irqsave(&mac->lock, flags);
843 mac->short_preamble = !preamble;
844 if (!mac->updating_rts_rate) {
845 mac->updating_rts_rate = 1;
846 /* FIXME: should disable TX here, until work has
847 * completed and RTS_CTS reg is updated */
848 queue_work(zd_workqueue, &mac->set_rts_cts_work);
849 }
850 spin_unlock_irqrestore(&mac->lock, flags);
851 }
e85d0918
DD
852}
853
459c51ad
DD
854static const struct ieee80211_ops zd_ops = {
855 .tx = zd_op_tx,
856 .start = zd_op_start,
857 .stop = zd_op_stop,
858 .add_interface = zd_op_add_interface,
859 .remove_interface = zd_op_remove_interface,
860 .config = zd_op_config,
861 .config_interface = zd_op_config_interface,
862 .configure_filter = zd_op_configure_filter,
863 .erp_ie_changed = zd_op_erp_ie_changed,
864};
865
866struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
e85d0918 867{
459c51ad
DD
868 struct zd_mac *mac;
869 struct ieee80211_hw *hw;
870 int i;
e85d0918 871
459c51ad
DD
872 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
873 if (!hw) {
874 dev_dbg_f(&intf->dev, "out of memory\n");
875 return NULL;
db888aed 876 }
459c51ad
DD
877
878 mac = zd_hw_mac(hw);
879
880 memset(mac, 0, sizeof(*mac));
881 spin_lock_init(&mac->lock);
882 mac->hw = hw;
883
884 mac->type = IEEE80211_IF_TYPE_INVALID;
885
886 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
887 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
888 mac->modes[0].mode = MODE_IEEE80211G;
889 mac->modes[0].num_rates = ARRAY_SIZE(zd_rates);
890 mac->modes[0].rates = mac->rates;
891 mac->modes[0].num_channels = ARRAY_SIZE(zd_channels);
892 mac->modes[0].channels = mac->channels;
893 mac->modes[1].mode = MODE_IEEE80211B;
894 mac->modes[1].num_rates = 4;
895 mac->modes[1].rates = mac->rates;
896 mac->modes[1].num_channels = ARRAY_SIZE(zd_channels);
897 mac->modes[1].channels = mac->channels;
898
899 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
900 IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED;
901 hw->max_rssi = 100;
902 hw->max_signal = 100;
903
904 hw->queues = 1;
905 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
906
907 skb_queue_head_init(&mac->ack_wait_queue);
908
909 for (i = 0; i < 2; i++) {
910 if (ieee80211_register_hwmode(hw, &mac->modes[i])) {
911 dev_dbg_f(&intf->dev, "cannot register hwmode\n");
912 ieee80211_free_hw(hw);
913 return NULL;
914 }
db888aed 915 }
459c51ad
DD
916
917 zd_chip_init(&mac->chip, hw, intf);
918 housekeeping_init(mac);
919 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
920 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
921 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
922
923 SET_IEEE80211_DEV(hw, &intf->dev);
924 return hw;
e85d0918
DD
925}
926
583afd1e
UK
927#define LINK_LED_WORK_DELAY HZ
928
c4028958 929static void link_led_handler(struct work_struct *work)
583afd1e 930{
c4028958
DH
931 struct zd_mac *mac =
932 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
583afd1e 933 struct zd_chip *chip = &mac->chip;
583afd1e
UK
934 int is_associated;
935 int r;
936
937 spin_lock_irq(&mac->lock);
459c51ad 938 is_associated = mac->associated;
583afd1e
UK
939 spin_unlock_irq(&mac->lock);
940
941 r = zd_chip_control_leds(chip,
942 is_associated ? LED_ASSOCIATED : LED_SCANNING);
943 if (r)
459c51ad 944 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
583afd1e
UK
945
946 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
947 LINK_LED_WORK_DELAY);
948}
949
950static void housekeeping_init(struct zd_mac *mac)
951{
c4028958 952 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
583afd1e
UK
953}
954
955static void housekeeping_enable(struct zd_mac *mac)
956{
957 dev_dbg_f(zd_mac_dev(mac), "\n");
958 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
959 0);
960}
961
962static void housekeeping_disable(struct zd_mac *mac)
963{
964 dev_dbg_f(zd_mac_dev(mac), "\n");
965 cancel_rearming_delayed_workqueue(zd_workqueue,
966 &mac->housekeeping.link_led_work);
967 zd_chip_control_leds(&mac->chip, LED_OFF);
968}
This page took 0.437542 seconds and 5 git commands to generate.