zd1211rw: port to mac80211
[deliverable/linux.git] / drivers / net / wireless / zd1211rw / zd_mac.c
1 /* zd_mac.c
2 *
3 * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
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>
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"
30 #include "zd_rf.h"
31
32 /* This table contains the hardware specific values for the modulation rates. */
33 static 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
75 static 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 };
105
106 static void housekeeping_init(struct zd_mac *mac);
107 static void housekeeping_enable(struct zd_mac *mac);
108 static void housekeeping_disable(struct zd_mac *mac);
109
110 int zd_mac_preinit_hw(struct ieee80211_hw *hw)
111 {
112 int r;
113 u8 addr[ETH_ALEN];
114 struct zd_mac *mac = zd_hw_mac(hw);
115
116 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
117 if (r)
118 return r;
119
120 SET_IEEE80211_PERM_ADDR(hw, addr);
121
122 return 0;
123 }
124
125 int zd_mac_init_hw(struct ieee80211_hw *hw)
126 {
127 int r;
128 struct zd_mac *mac = zd_hw_mac(hw);
129 struct zd_chip *chip = &mac->chip;
130 u8 default_regdomain;
131
132 r = zd_chip_enable_int(chip);
133 if (r)
134 goto out;
135 r = zd_chip_init_hw(chip);
136 if (r)
137 goto disable_int;
138
139 ZD_ASSERT(!irqs_disabled());
140
141 r = zd_read_regdomain(chip, &default_regdomain);
142 if (r)
143 goto disable_int;
144 spin_lock_irq(&mac->lock);
145 mac->regdomain = mac->default_regdomain = default_regdomain;
146 spin_unlock_irq(&mac->lock);
147
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);
151 if (r)
152 goto disable_int;
153
154 zd_geo_init(hw, mac->regdomain);
155
156 r = 0;
157 disable_int:
158 zd_chip_disable_int(chip);
159 out:
160 return r;
161 }
162
163 void zd_mac_clear(struct zd_mac *mac)
164 {
165 flush_workqueue(zd_workqueue);
166 zd_chip_clear(&mac->chip);
167 ZD_ASSERT(!spin_is_locked(&mac->lock));
168 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
169 }
170
171 static int set_rx_filter(struct zd_mac *mac)
172 {
173 unsigned long flags;
174 u32 filter = STA_RX_FILTER;
175
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);
182 }
183
184 static int set_mc_hash(struct zd_mac *mac)
185 {
186 struct zd_mc_hash hash;
187 zd_mc_clear(&hash);
188 return zd_chip_set_multicast_hash(&mac->chip, &hash);
189 }
190
191 static int zd_op_start(struct ieee80211_hw *hw)
192 {
193 struct zd_mac *mac = zd_hw_mac(hw);
194 struct zd_chip *chip = &mac->chip;
195 struct zd_usb *usb = &chip->usb;
196 int r;
197
198 if (!usb->initialized) {
199 r = zd_usb_init_hw(usb);
200 if (r)
201 goto out;
202 }
203
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;
211 r = set_rx_filter(mac);
212 if (r)
213 goto disable_int;
214 r = set_mc_hash(mac);
215 if (r)
216 goto disable_int;
217 r = zd_chip_switch_radio_on(chip);
218 if (r < 0)
219 goto disable_int;
220 r = zd_chip_enable_rxtx(chip);
221 if (r < 0)
222 goto disable_radio;
223 r = zd_chip_enable_hwint(chip);
224 if (r < 0)
225 goto disable_rxtx;
226
227 housekeeping_enable(mac);
228 return 0;
229 disable_rxtx:
230 zd_chip_disable_rxtx(chip);
231 disable_radio:
232 zd_chip_switch_radio_off(chip);
233 disable_int:
234 zd_chip_disable_int(chip);
235 out:
236 return r;
237 }
238
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 */
247 static 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 */
263 static void kfree_tx_skb(struct sk_buff *skb)
264 {
265 clear_tx_skb_control_block(skb);
266 dev_kfree_skb_any(skb);
267 }
268
269 static 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;
275
276 /* The order here deliberately is a little different from the open()
277 * method, since we need to make sure there is no opportunity for RX
278 * frames to be processed by mac80211 after we have stopped it.
279 */
280
281 zd_chip_disable_rxtx(chip);
282 housekeeping_disable(mac);
283 flush_workqueue(zd_workqueue);
284
285 zd_chip_disable_hwint(chip);
286 zd_chip_switch_radio_off(chip);
287 zd_chip_disable_int(chip);
288
289
290 while ((skb = skb_dequeue(ack_wait_queue)))
291 kfree_tx_skb(skb);
292 }
293
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 */
302 static 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));
316
317 return 0;
318 }
319
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 */
333 static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
334 struct ieee80211_tx_status *status,
335 bool success)
336 {
337 struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
338 skb->cb;
339
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);
346 }
347
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 */
356 void zd_mac_tx_failed(struct ieee80211_hw *hw)
357 {
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}};
361
362 skb = skb_dequeue(q);
363 if (skb == NULL)
364 return;
365 tx_status(hw, skb, &status, 0);
366 }
367
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 */
378 void 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);
391 } else {
392 struct sk_buff_head *q =
393 &zd_hw_mac(hw)->ack_wait_queue;
394
395 skb_queue_tail(q, skb);
396 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
397 zd_mac_tx_failed(hw);
398 }
399 } else {
400 kfree_tx_skb(skb);
401 }
402 }
403
404 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
405 {
406 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
407 * the zd-rate values.
408 */
409 static const u8 rate_divisor[] = {
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,
423 };
424
425 u32 bits = (u32)tx_length * 8;
426 u32 divisor;
427
428 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
429 if (divisor == 0)
430 return -EINVAL;
431
432 switch (zd_rate) {
433 case ZD_CCK_RATE_5_5M:
434 bits = (2*bits) + 10; /* round up to the next integer */
435 break;
436 case ZD_CCK_RATE_11M:
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
451 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
452 struct ieee80211_hdr *header, u32 flags)
453 {
454 u16 fctl = le16_to_cpu(header->frame_control);
455
456 /*
457 * CONTROL TODO:
458 * - if backoff needed, enable bit 0
459 * - if burst (backoff not needed) disable bit 0
460 */
461
462 cs->control = 0;
463
464 /* First fragment */
465 if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
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 */
473 if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
474 (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
475 cs->control |= ZD_CS_PS_POLL_FRAME;
476
477 if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
478 cs->control |= ZD_CS_RTS;
479
480 if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
481 cs->control |= ZD_CS_SELF_CTS;
482
483 /* FIXME: Management frame? */
484 }
485
486 static int fill_ctrlset(struct zd_mac *mac,
487 struct sk_buff *skb,
488 struct ieee80211_tx_control *control)
489 {
490 int r;
491 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
492 unsigned int frag_len = skb->len + FCS_LEN;
493 unsigned int packet_length;
494 struct zd_ctrlset *cs = (struct zd_ctrlset *)
495 skb_push(skb, sizeof(struct zd_ctrlset));
496
497 ZD_ASSERT(frag_len <= 0xffff);
498
499 cs->modulation = control->tx_rate;
500
501 cs->tx_length = cpu_to_le16(frag_len);
502
503 cs_set_control(mac, cs, hdr, control->flags);
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 */
510 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
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;
527 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
528 le16_to_cpu(cs->tx_length));
529 if (r < 0)
530 return r;
531 cs->current_length = cpu_to_le16(r);
532 cs->next_frame_length = 0;
533
534 return 0;
535 }
536
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 */
548 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
549 struct ieee80211_tx_control *control)
550 {
551 struct zd_mac *mac = zd_hw_mac(hw);
552 int r;
553
554 r = fill_ctrlset(mac, skb, control);
555 if (r)
556 return r;
557
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;
565 }
566 return 0;
567 }
568
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
574 *
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.
580 *
581 * Returns 1 if the frame was an ACK, 0 if it was ignored.
582 */
583 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
584 struct ieee80211_rx_status *stats)
585 {
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;
590
591 if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
592 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
593 return 0;
594
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 }
611 out:
612 spin_unlock_irqrestore(&q->lock, flags);
613 return 1;
614 }
615
616 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
617 {
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;
623
624 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
625 FCS_LEN + sizeof(struct rx_status))
626 return -EINVAL;
627
628 memset(&stats, 0, sizeof(stats));
629
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. */
638
639 /* Caller has to ensure that length >= sizeof(struct rx_status). */
640 status = (struct rx_status *)
641 (buffer + (length - sizeof(struct rx_status)));
642 if (status->frame_status & ZD_RX_ERROR) {
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;
649 }
650 }
651
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,
657 length - sizeof(struct rx_status),
658 status);
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;
673
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);
680 return 0;
681 }
682
683 static int zd_op_add_interface(struct ieee80211_hw *hw,
684 struct ieee80211_if_init_conf *conf)
685 {
686 struct zd_mac *mac = zd_hw_mac(hw);
687
688 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
689 if (mac->type != IEEE80211_IF_TYPE_INVALID)
690 return -EOPNOTSUPP;
691
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;
699 }
700
701 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
702 }
703
704 static 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 }
711
712 static 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 }
717
718 static 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);
722
723 spin_lock_irq(&mac->lock);
724 mac->associated = is_valid_ether_addr(conf->bssid);
725 spin_unlock_irq(&mac->lock);
726
727 /* TODO: do hardware bssid filtering */
728 return 0;
729 }
730
731 static void set_multicast_hash_handler(struct work_struct *work)
732 {
733 struct zd_mac *mac =
734 container_of(work, struct zd_mac, set_multicast_hash_work);
735 struct zd_mc_hash hash;
736
737 spin_lock_irq(&mac->lock);
738 hash = mac->multicast_hash;
739 spin_unlock_irq(&mac->lock);
740
741 zd_chip_set_multicast_hash(&mac->chip, &hash);
742 }
743
744 static void set_rx_filter_handler(struct work_struct *work)
745 {
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);
754 }
755
756 #define SUPPORTED_FIF_FLAGS \
757 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
758 FIF_OTHER_BSS)
759 static 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)
763 {
764 struct zd_mc_hash hash;
765 struct zd_mac *mac = zd_hw_mac(hw);
766 unsigned long flags;
767 int i;
768
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 }
793 }
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. */
813 }
814
815 static void set_rts_cts_work(struct work_struct *work)
816 {
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);
831 }
832
833 static void zd_op_erp_ie_changed(struct ieee80211_hw *hw, u8 changes,
834 int cts_protection, int preamble)
835 {
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 }
852 }
853
854 static 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
866 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
867 {
868 struct zd_mac *mac;
869 struct ieee80211_hw *hw;
870 int i;
871
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;
876 }
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 }
915 }
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;
925 }
926
927 #define LINK_LED_WORK_DELAY HZ
928
929 static void link_led_handler(struct work_struct *work)
930 {
931 struct zd_mac *mac =
932 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
933 struct zd_chip *chip = &mac->chip;
934 int is_associated;
935 int r;
936
937 spin_lock_irq(&mac->lock);
938 is_associated = mac->associated;
939 spin_unlock_irq(&mac->lock);
940
941 r = zd_chip_control_leds(chip,
942 is_associated ? LED_ASSOCIATED : LED_SCANNING);
943 if (r)
944 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
945
946 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
947 LINK_LED_WORK_DELAY);
948 }
949
950 static void housekeeping_init(struct zd_mac *mac)
951 {
952 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
953 }
954
955 static 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
962 static 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.052304 seconds and 5 git commands to generate.