3 * Common code for mac80211 Prism54 drivers
5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
6 * Copyright (c) 2007, Christian Lamparter <chunkeey@web.de>
8 * Based on the islsm (softmac prism54) driver, which is:
9 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/init.h>
17 #include <linux/firmware.h>
18 #include <linux/etherdevice.h>
20 #include <net/mac80211.h>
23 #include "p54common.h"
25 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
26 MODULE_DESCRIPTION("Softmac Prism54 common code");
27 MODULE_LICENSE("GPL");
28 MODULE_ALIAS("prism54common");
30 static struct ieee80211_rate p54_rates[] = {
31 { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
32 { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
33 { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
34 { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
35 { .bitrate = 60, .hw_value = 4, },
36 { .bitrate = 90, .hw_value = 5, },
37 { .bitrate = 120, .hw_value = 6, },
38 { .bitrate = 180, .hw_value = 7, },
39 { .bitrate = 240, .hw_value = 8, },
40 { .bitrate = 360, .hw_value = 9, },
41 { .bitrate = 480, .hw_value = 10, },
42 { .bitrate = 540, .hw_value = 11, },
45 static struct ieee80211_channel p54_channels[] = {
46 { .center_freq = 2412, .hw_value = 1, },
47 { .center_freq = 2417, .hw_value = 2, },
48 { .center_freq = 2422, .hw_value = 3, },
49 { .center_freq = 2427, .hw_value = 4, },
50 { .center_freq = 2432, .hw_value = 5, },
51 { .center_freq = 2437, .hw_value = 6, },
52 { .center_freq = 2442, .hw_value = 7, },
53 { .center_freq = 2447, .hw_value = 8, },
54 { .center_freq = 2452, .hw_value = 9, },
55 { .center_freq = 2457, .hw_value = 10, },
56 { .center_freq = 2462, .hw_value = 11, },
57 { .center_freq = 2467, .hw_value = 12, },
58 { .center_freq = 2472, .hw_value = 13, },
59 { .center_freq = 2484, .hw_value = 14, },
62 static struct ieee80211_supported_band band_2GHz = {
63 .channels = p54_channels,
64 .n_channels = ARRAY_SIZE(p54_channels),
65 .bitrates = p54_rates,
66 .n_bitrates = ARRAY_SIZE(p54_rates),
70 void p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
72 struct p54_common *priv = dev->priv;
73 struct bootrec_exp_if *exp_if;
74 struct bootrec *bootrec;
75 u32 *data = (u32 *)fw->data;
76 u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
77 u8 *fw_version = NULL;
84 while (data < end_data && *data)
87 while (data < end_data && !*data)
90 bootrec = (struct bootrec *) data;
92 while (bootrec->data <= end_data &&
93 (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
94 u32 code = le32_to_cpu(bootrec->code);
96 case BR_CODE_COMPONENT_ID:
97 switch (be32_to_cpu(*(__be32 *)bootrec->data)) {
99 printk(KERN_INFO "p54: FreeMAC firmware\n");
102 printk(KERN_INFO "p54: LM20 firmware\n");
105 printk(KERN_INFO "p54: LM86 firmware\n");
108 printk(KERN_INFO "p54: LM87 firmware - not supported yet!\n");
111 printk(KERN_INFO "p54: unknown firmware\n");
115 case BR_CODE_COMPONENT_VERSION:
116 /* 24 bytes should be enough for all firmwares */
117 if (strnlen((unsigned char*)bootrec->data, 24) < 24)
118 fw_version = (unsigned char*)bootrec->data;
121 priv->rx_start = le32_to_cpu(((__le32 *)bootrec->data)[1]);
122 /* FIXME add sanity checking */
123 priv->rx_end = le32_to_cpu(((__le32 *)bootrec->data)[2]) - 0x3500;
125 case BR_CODE_EXPOSED_IF:
126 exp_if = (struct bootrec_exp_if *) bootrec->data;
127 for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
128 if (exp_if[i].if_id == cpu_to_le16(0x1a))
129 priv->fw_var = le16_to_cpu(exp_if[i].variant);
131 case BR_CODE_DEPENDENT_IF:
133 case BR_CODE_END_OF_BRA:
134 case LEGACY_BR_CODE_END_OF_BRA:
140 bootrec = (struct bootrec *)&bootrec->data[len];
144 printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
145 fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
147 if (priv->fw_var >= 0x300) {
148 /* Firmware supports QoS, use it! */
149 priv->tx_stats[0].limit = 3;
150 priv->tx_stats[1].limit = 4;
151 priv->tx_stats[2].limit = 3;
152 priv->tx_stats[3].limit = 1;
156 EXPORT_SYMBOL_GPL(p54_parse_firmware);
158 static int p54_convert_rev0_to_rev1(struct ieee80211_hw *dev,
159 struct pda_pa_curve_data *curve_data)
161 struct p54_common *priv = dev->priv;
162 struct pda_pa_curve_data_sample_rev1 *rev1;
163 struct pda_pa_curve_data_sample_rev0 *rev0;
164 size_t cd_len = sizeof(*curve_data) +
165 (curve_data->points_per_channel*sizeof(*rev1) + 2) *
166 curve_data->channels;
168 void *source, *target;
170 priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
171 if (!priv->curve_data)
174 memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
175 source = curve_data->data;
176 target = priv->curve_data->data;
177 for (i = 0; i < curve_data->channels; i++) {
178 __le16 *freq = source;
179 source += sizeof(__le16);
180 *((__le16 *)target) = *freq;
181 target += sizeof(__le16);
182 for (j = 0; j < curve_data->points_per_channel; j++) {
186 rev1->rf_power = rev0->rf_power;
187 rev1->pa_detector = rev0->pa_detector;
188 rev1->data_64qam = rev0->pcv;
189 /* "invent" the points for the other modulations */
190 #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
191 rev1->data_16qam = SUB(rev0->pcv, 12);
192 rev1->data_qpsk = SUB(rev1->data_16qam, 12);
193 rev1->data_bpsk = SUB(rev1->data_qpsk, 12);
194 rev1->data_barker= SUB(rev1->data_bpsk, 14);
196 target += sizeof(*rev1);
197 source += sizeof(*rev0);
204 int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
206 struct p54_common *priv = dev->priv;
207 struct eeprom_pda_wrap *wrap = NULL;
208 struct pda_entry *entry;
209 unsigned int data_len, entry_len;
212 u8 *end = (u8 *)eeprom + len;
214 wrap = (struct eeprom_pda_wrap *) eeprom;
215 entry = (void *)wrap->data + le16_to_cpu(wrap->len);
217 /* verify that at least the entry length/code fits */
218 while ((u8 *)entry <= end - sizeof(*entry)) {
219 entry_len = le16_to_cpu(entry->len);
220 data_len = ((entry_len - 1) << 1);
222 /* abort if entry exceeds whole structure */
223 if ((u8 *)entry + sizeof(*entry) + data_len > end)
226 switch (le16_to_cpu(entry->code)) {
227 case PDR_MAC_ADDRESS:
228 SET_IEEE80211_PERM_ADDR(dev, entry->data);
230 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
236 if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
241 priv->output_limit = kmalloc(entry->data[1] *
242 sizeof(*priv->output_limit), GFP_KERNEL);
244 if (!priv->output_limit) {
249 memcpy(priv->output_limit, &entry->data[2],
250 entry->data[1]*sizeof(*priv->output_limit));
251 priv->output_limit_len = entry->data[1];
253 case PDR_PRISM_PA_CAL_CURVE_DATA:
254 if (data_len < sizeof(struct pda_pa_curve_data)) {
259 if (((struct pda_pa_curve_data *)entry->data)->cal_method_rev) {
260 priv->curve_data = kmalloc(data_len, GFP_KERNEL);
261 if (!priv->curve_data) {
266 memcpy(priv->curve_data, entry->data, data_len);
268 err = p54_convert_rev0_to_rev1(dev, (struct pda_pa_curve_data *)entry->data);
274 case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
275 priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
276 if (!priv->iq_autocal) {
281 memcpy(priv->iq_autocal, entry->data, data_len);
282 priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
284 case PDR_INTERFACE_LIST:
286 while ((u8 *)tmp < entry->data + data_len) {
287 struct bootrec_exp_if *exp_if = tmp;
288 if (le16_to_cpu(exp_if->if_id) == 0xF)
289 priv->rxhw = exp_if->variant & cpu_to_le16(0x07);
290 tmp += sizeof(struct bootrec_exp_if);
293 case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
294 priv->version = *(u8 *)(entry->data + 1);
297 /* make it overrun */
301 printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
302 le16_to_cpu(entry->code));
306 entry = (void *)entry + (entry_len + 1)*2;
309 if (!priv->iq_autocal || !priv->output_limit || !priv->curve_data) {
310 printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
318 if (priv->iq_autocal) {
319 kfree(priv->iq_autocal);
320 priv->iq_autocal = NULL;
323 if (priv->output_limit) {
324 kfree(priv->output_limit);
325 priv->output_limit = NULL;
328 if (priv->curve_data) {
329 kfree(priv->curve_data);
330 priv->curve_data = NULL;
333 printk(KERN_ERR "p54: eeprom parse failed!\n");
336 EXPORT_SYMBOL_GPL(p54_parse_eeprom);
338 void p54_fill_eeprom_readback(struct p54_control_hdr *hdr)
340 struct p54_eeprom_lm86 *eeprom_hdr;
342 hdr->magic1 = cpu_to_le16(0x8000);
343 hdr->len = cpu_to_le16(sizeof(*eeprom_hdr) + 0x2000);
344 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);
345 hdr->retry1 = hdr->retry2 = 0;
346 eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;
347 eeprom_hdr->offset = 0x0;
348 eeprom_hdr->len = cpu_to_le16(0x2000);
350 EXPORT_SYMBOL_GPL(p54_fill_eeprom_readback);
352 static void p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
354 struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
355 struct ieee80211_rx_status rx_status = {0};
356 u16 freq = le16_to_cpu(hdr->freq);
358 rx_status.signal = hdr->rssi;
360 rx_status.qual = (100 * hdr->rssi) / 127;
361 rx_status.rate_idx = hdr->rate & 0xf;
362 rx_status.freq = freq;
363 rx_status.band = IEEE80211_BAND_2GHZ;
364 rx_status.antenna = hdr->antenna;
365 rx_status.mactime = le64_to_cpu(hdr->timestamp);
366 rx_status.flag |= RX_FLAG_TSFT;
368 skb_pull(skb, sizeof(*hdr));
369 skb_trim(skb, le16_to_cpu(hdr->len));
371 ieee80211_rx_irqsafe(dev, skb, &rx_status);
374 static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
376 struct p54_common *priv = dev->priv;
379 for (i = 0; i < dev->queues; i++)
380 if (priv->tx_stats[i].len < priv->tx_stats[i].limit)
381 ieee80211_wake_queue(dev, i);
384 static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
386 struct p54_common *priv = dev->priv;
387 struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
388 struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
389 struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
390 u32 addr = le32_to_cpu(hdr->req_id) - 0x70;
391 struct memrecord *range = NULL;
393 u32 last_addr = priv->rx_start;
395 while (entry != (struct sk_buff *)&priv->tx_queue) {
396 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
397 range = (void *)info->driver_data;
398 if (range->start_addr == addr) {
399 struct p54_control_hdr *entry_hdr;
400 struct p54_tx_control_allocdata *entry_data;
403 if (entry->next != (struct sk_buff *)&priv->tx_queue) {
404 struct ieee80211_tx_info *ni;
405 struct memrecord *mr;
407 ni = IEEE80211_SKB_CB(entry->next);
408 mr = (struct memrecord *)ni->driver_data;
409 freed = mr->start_addr - last_addr;
411 freed = priv->rx_end - last_addr;
413 last_addr = range->end_addr;
414 __skb_unlink(entry, &priv->tx_queue);
415 memset(&info->status, 0, sizeof(info->status));
416 priv->tx_stats[skb_get_queue_mapping(skb)].len--;
417 entry_hdr = (struct p54_control_hdr *) entry->data;
418 entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
419 if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
420 pad = entry_data->align[0];
422 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
423 if (!(payload->status & 0x01))
424 info->flags |= IEEE80211_TX_STAT_ACK;
426 info->status.excessive_retries = 1;
428 info->status.retry_count = payload->retries - 1;
429 info->status.ack_signal = le16_to_cpu(payload->ack_rssi);
430 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
431 ieee80211_tx_status_irqsafe(dev, entry);
434 last_addr = range->end_addr;
438 if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
439 sizeof(struct p54_control_hdr))
440 p54_wake_free_queues(dev);
443 static void p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
445 struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
447 switch (le16_to_cpu(hdr->type)) {
448 case P54_CONTROL_TYPE_TXDONE:
449 p54_rx_frame_sent(dev, skb);
451 case P54_CONTROL_TYPE_BBP:
454 printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
455 wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
460 /* returns zero if skb can be reused */
461 int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
463 u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
467 p54_rx_data(dev, skb);
470 /* TODO: do something better... but then again, I've never seen this happen */
471 printk(KERN_ERR "%s: Received fault. Probably need to restart hardware now..\n",
472 wiphy_name(dev->wiphy));
475 p54_rx_control(dev, skb);
478 printk(KERN_ERR "%s: unknown frame RXed (0x%02x)\n",
479 wiphy_name(dev->wiphy), type);
484 EXPORT_SYMBOL_GPL(p54_rx);
487 * So, the firmware is somewhat stupid and doesn't know what places in its
488 * memory incoming data should go to. By poking around in the firmware, we
489 * can find some unused memory to upload our packets to. However, data that we
490 * want the card to TX needs to stay intact until the card has told us that
491 * it is done with it. This function finds empty places we can upload to and
492 * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
495 static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
496 struct p54_control_hdr *data, u32 len)
498 struct p54_common *priv = dev->priv;
499 struct sk_buff *entry = priv->tx_queue.next;
500 struct sk_buff *target_skb = NULL;
501 u32 last_addr = priv->rx_start;
502 u32 largest_hole = 0;
503 u32 target_addr = priv->rx_start;
506 len = (len + 0x170 + 3) & ~0x3; /* 0x70 headroom, 0x100 tailroom */
508 spin_lock_irqsave(&priv->tx_queue.lock, flags);
509 left = skb_queue_len(&priv->tx_queue);
512 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
513 struct memrecord *range = (void *)info->driver_data;
514 hole_size = range->start_addr - last_addr;
515 if (!target_skb && hole_size >= len) {
516 target_skb = entry->prev;
518 target_addr = last_addr;
520 largest_hole = max(largest_hole, hole_size);
521 last_addr = range->end_addr;
524 if (!target_skb && priv->rx_end - last_addr >= len) {
525 target_skb = priv->tx_queue.prev;
526 largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
527 if (!skb_queue_empty(&priv->tx_queue)) {
528 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(target_skb);
529 struct memrecord *range = (void *)info->driver_data;
530 target_addr = range->end_addr;
533 largest_hole = max(largest_hole, priv->rx_end - last_addr);
536 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
537 struct memrecord *range = (void *)info->driver_data;
538 range->start_addr = target_addr;
539 range->end_addr = target_addr + len;
540 __skb_queue_after(&priv->tx_queue, target_skb, skb);
541 if (largest_hole < IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
542 sizeof(struct p54_control_hdr))
543 ieee80211_stop_queues(dev);
545 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
547 data->req_id = cpu_to_le32(target_addr + 0x70);
550 static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
552 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
553 struct ieee80211_tx_queue_stats *current_queue;
554 struct p54_common *priv = dev->priv;
555 struct p54_control_hdr *hdr;
556 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
557 struct p54_tx_control_allocdata *txhdr;
561 current_queue = &priv->tx_stats[skb_get_queue_mapping(skb)];
562 if (unlikely(current_queue->len > current_queue->limit))
563 return NETDEV_TX_BUSY;
564 current_queue->len++;
565 current_queue->count++;
566 if (current_queue->len == current_queue->limit)
567 ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
569 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
572 txhdr = (struct p54_tx_control_allocdata *)
573 skb_push(skb, sizeof(*txhdr) + padding);
574 hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
577 hdr->magic1 = cpu_to_le16(0x4010);
579 hdr->magic1 = cpu_to_le16(0x0010);
580 hdr->len = cpu_to_le16(len);
581 hdr->type = (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 0 : cpu_to_le16(1);
582 hdr->retry1 = hdr->retry2 = info->control.retry_limit;
584 memset(txhdr->wep_key, 0x0, 16);
588 /* TODO: add support for alternate retry TX rates */
589 rate = ieee80211_get_tx_rate(dev, info)->hw_value;
590 if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE)
592 if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
594 else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
596 memset(txhdr->rateset, rate, 8);
597 txhdr->wep_key_present = 0;
598 txhdr->wep_key_len = 0;
599 txhdr->frame_type = cpu_to_le32(skb_get_queue_mapping(skb) + 4);
601 txhdr->antenna = (info->antenna_sel_tx == 0) ?
602 2 : info->antenna_sel_tx - 1;
603 txhdr->output_power = 0x7f; // HW Maximum
604 txhdr->magic5 = (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
605 0 : ((rate > 0x3) ? cpu_to_le32(0x33) : cpu_to_le32(0x23));
607 txhdr->align[0] = padding;
609 /* FIXME: The sequence that follows is needed for this driver to
610 * work with mac80211 since "mac80211: fix TX sequence numbers".
611 * As with the temporary code in rt2x00, changes will be needed
612 * to get proper sequence numbers on beacons. In addition, this
613 * patch places the sequence number in the hardware state, which
614 * limits us to a single virtual state.
616 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
617 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
619 ieee80211hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
620 ieee80211hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
622 /* modifies skb->cb and with it info, so must be last! */
623 p54_assign_address(dev, skb, hdr, skb->len);
625 priv->tx(dev, hdr, skb->len, 0);
629 static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
630 const u8 *dst, const u8 *src, u8 antenna,
631 u32 magic3, u32 magic8, u32 magic9)
633 struct p54_common *priv = dev->priv;
634 struct p54_control_hdr *hdr;
635 struct p54_tx_control_filter *filter;
637 hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
638 priv->tx_hdr_len, GFP_ATOMIC);
642 hdr = (void *)hdr + priv->tx_hdr_len;
644 filter = (struct p54_tx_control_filter *) hdr->data;
645 hdr->magic1 = cpu_to_le16(0x8001);
646 hdr->len = cpu_to_le16(sizeof(*filter));
647 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*filter));
648 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
650 filter->filter_type = cpu_to_le16(filter_type);
651 memcpy(filter->dst, dst, ETH_ALEN);
653 memset(filter->src, ~0, ETH_ALEN);
655 memcpy(filter->src, src, ETH_ALEN);
656 filter->antenna = antenna;
657 filter->magic3 = cpu_to_le32(magic3);
658 filter->rx_addr = cpu_to_le32(priv->rx_end);
659 filter->max_rx = cpu_to_le16(0x0620); /* FIXME: for usb ver 1.. maybe */
660 filter->rxhw = priv->rxhw;
661 filter->magic8 = cpu_to_le16(magic8);
662 filter->magic9 = cpu_to_le16(magic9);
664 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*filter), 1);
668 static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
670 struct p54_common *priv = dev->priv;
671 struct p54_control_hdr *hdr;
672 struct p54_tx_control_channel *chan;
674 size_t payload_len = sizeof(*chan) + sizeof(u32)*2 +
675 sizeof(*chan->curve_data) *
676 priv->curve_data->points_per_channel;
679 hdr = kzalloc(sizeof(*hdr) + payload_len +
680 priv->tx_hdr_len, GFP_KERNEL);
684 hdr = (void *)hdr + priv->tx_hdr_len;
686 chan = (struct p54_tx_control_channel *) hdr->data;
688 hdr->magic1 = cpu_to_le16(0x8001);
689 hdr->len = cpu_to_le16(sizeof(*chan));
690 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
691 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + payload_len);
693 chan->magic1 = cpu_to_le16(0x1);
694 chan->magic2 = cpu_to_le16(0x0);
696 for (i = 0; i < priv->iq_autocal_len; i++) {
697 if (priv->iq_autocal[i].freq != freq)
700 memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
701 sizeof(*priv->iq_autocal));
704 if (i == priv->iq_autocal_len)
707 for (i = 0; i < priv->output_limit_len; i++) {
708 if (priv->output_limit[i].freq != freq)
711 chan->val_barker = 0x38;
712 chan->val_bpsk = priv->output_limit[i].val_bpsk;
713 chan->val_qpsk = priv->output_limit[i].val_qpsk;
714 chan->val_16qam = priv->output_limit[i].val_16qam;
715 chan->val_64qam = priv->output_limit[i].val_64qam;
718 if (i == priv->output_limit_len)
721 chan->pa_points_per_curve = priv->curve_data->points_per_channel;
723 entry = priv->curve_data->data;
724 for (i = 0; i < priv->curve_data->channels; i++) {
725 if (*((__le16 *)entry) != freq) {
726 entry += sizeof(__le16);
727 entry += sizeof(struct pda_pa_curve_data_sample_rev1) *
728 chan->pa_points_per_curve;
732 entry += sizeof(__le16);
733 memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
734 chan->pa_points_per_curve);
738 memcpy(hdr->data + payload_len - 4, &chan->val_bpsk, 4);
740 priv->tx(dev, hdr, sizeof(*hdr) + payload_len, 1);
744 printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
749 static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
751 struct p54_common *priv = dev->priv;
752 struct p54_control_hdr *hdr;
753 struct p54_tx_control_led *led;
755 hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
756 priv->tx_hdr_len, GFP_KERNEL);
760 hdr = (void *)hdr + priv->tx_hdr_len;
761 hdr->magic1 = cpu_to_le16(0x8001);
762 hdr->len = cpu_to_le16(sizeof(*led));
763 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
764 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led));
766 led = (struct p54_tx_control_led *) hdr->data;
767 led->mode = cpu_to_le16(mode);
768 led->led_permanent = cpu_to_le16(link);
769 led->led_temporary = cpu_to_le16(act);
770 led->duration = cpu_to_le16(1000);
772 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
777 #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
779 queue.aifs = cpu_to_le16(ai_fs); \
780 queue.cwmin = cpu_to_le16(cw_min); \
781 queue.cwmax = cpu_to_le16(cw_max); \
782 queue.txop = cpu_to_le16(_txop); \
785 static void p54_init_vdcf(struct ieee80211_hw *dev)
787 struct p54_common *priv = dev->priv;
788 struct p54_control_hdr *hdr;
789 struct p54_tx_control_vdcf *vdcf;
791 /* all USB V1 adapters need a extra headroom */
792 hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
793 hdr->magic1 = cpu_to_le16(0x8001);
794 hdr->len = cpu_to_le16(sizeof(*vdcf));
795 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
796 hdr->req_id = cpu_to_le32(priv->rx_start);
798 vdcf = (struct p54_tx_control_vdcf *) hdr->data;
800 P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 47);
801 P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 94);
802 P54_SET_QUEUE(vdcf->queue[2], 0x0003, 0x000f, 0x03ff, 0);
803 P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0);
806 static void p54_set_vdcf(struct ieee80211_hw *dev)
808 struct p54_common *priv = dev->priv;
809 struct p54_control_hdr *hdr;
810 struct p54_tx_control_vdcf *vdcf;
812 hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
814 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf));
816 vdcf = (struct p54_tx_control_vdcf *) hdr->data;
818 if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
828 /* (see prism54/isl_oid.h for further details) */
829 vdcf->frameburst = cpu_to_le16(0);
831 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
834 static int p54_start(struct ieee80211_hw *dev)
836 struct p54_common *priv = dev->priv;
839 err = priv->open(dev);
841 priv->mode = IEEE80211_IF_TYPE_MNTR;
846 static void p54_stop(struct ieee80211_hw *dev)
848 struct p54_common *priv = dev->priv;
850 while ((skb = skb_dequeue(&priv->tx_queue)))
853 priv->mode = IEEE80211_IF_TYPE_INVALID;
856 static int p54_add_interface(struct ieee80211_hw *dev,
857 struct ieee80211_if_init_conf *conf)
859 struct p54_common *priv = dev->priv;
861 if (priv->mode != IEEE80211_IF_TYPE_MNTR)
864 switch (conf->type) {
865 case IEEE80211_IF_TYPE_STA:
866 priv->mode = conf->type;
872 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
874 p54_set_filter(dev, 0, priv->mac_addr, NULL, 0, 1, 0, 0xF642);
875 p54_set_filter(dev, 0, priv->mac_addr, NULL, 1, 0, 0, 0xF642);
877 switch (conf->type) {
878 case IEEE80211_IF_TYPE_STA:
879 p54_set_filter(dev, 1, priv->mac_addr, NULL, 0, 0x15F, 0x1F4, 0);
882 BUG(); /* impossible */
886 p54_set_leds(dev, 1, 0, 0);
891 static void p54_remove_interface(struct ieee80211_hw *dev,
892 struct ieee80211_if_init_conf *conf)
894 struct p54_common *priv = dev->priv;
895 priv->mode = IEEE80211_IF_TYPE_MNTR;
896 memset(priv->mac_addr, 0, ETH_ALEN);
897 p54_set_filter(dev, 0, priv->mac_addr, NULL, 2, 0, 0, 0);
900 static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
903 struct p54_common *priv = dev->priv;
905 mutex_lock(&priv->conf_mutex);
906 ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
908 mutex_unlock(&priv->conf_mutex);
912 static int p54_config_interface(struct ieee80211_hw *dev,
913 struct ieee80211_vif *vif,
914 struct ieee80211_if_conf *conf)
916 struct p54_common *priv = dev->priv;
918 mutex_lock(&priv->conf_mutex);
919 p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 0, 1, 0, 0xF642);
920 p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 2, 0, 0, 0);
921 p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
922 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
923 mutex_unlock(&priv->conf_mutex);
927 static void p54_configure_filter(struct ieee80211_hw *dev,
928 unsigned int changed_flags,
929 unsigned int *total_flags,
930 int mc_count, struct dev_mc_list *mclist)
932 struct p54_common *priv = dev->priv;
934 *total_flags &= FIF_BCN_PRBRESP_PROMISC;
936 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
937 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
938 p54_set_filter(dev, 0, priv->mac_addr,
941 p54_set_filter(dev, 0, priv->mac_addr,
942 priv->bssid, 2, 0, 0, 0);
946 static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
947 const struct ieee80211_tx_queue_params *params)
949 struct p54_common *priv = dev->priv;
950 struct p54_tx_control_vdcf *vdcf;
952 vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
953 ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
955 if ((params) && !(queue > 4)) {
956 P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
957 params->cw_min, params->cw_max, params->txop);
966 static int p54_get_stats(struct ieee80211_hw *dev,
967 struct ieee80211_low_level_stats *stats)
973 static int p54_get_tx_stats(struct ieee80211_hw *dev,
974 struct ieee80211_tx_queue_stats *stats)
976 struct p54_common *priv = dev->priv;
978 memcpy(stats, &priv->tx_stats, sizeof(stats[0]) * dev->queues);
983 static const struct ieee80211_ops p54_ops = {
987 .add_interface = p54_add_interface,
988 .remove_interface = p54_remove_interface,
989 .config = p54_config,
990 .config_interface = p54_config_interface,
991 .configure_filter = p54_configure_filter,
992 .conf_tx = p54_conf_tx,
993 .get_stats = p54_get_stats,
994 .get_tx_stats = p54_get_tx_stats
997 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
999 struct ieee80211_hw *dev;
1000 struct p54_common *priv;
1002 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
1007 priv->mode = IEEE80211_IF_TYPE_INVALID;
1008 skb_queue_head_init(&priv->tx_queue);
1009 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
1010 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
1011 IEEE80211_HW_RX_INCLUDES_FCS |
1012 IEEE80211_HW_SIGNAL_UNSPEC;
1013 dev->channel_change_time = 1000; /* TODO: find actual value */
1014 dev->max_signal = 127;
1016 priv->tx_stats[0].limit = 5;
1019 dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
1020 sizeof(struct p54_tx_control_allocdata);
1022 priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf) +
1023 priv->tx_hdr_len + sizeof(struct p54_control_hdr), GFP_KERNEL);
1025 if (!priv->cached_vdcf) {
1026 ieee80211_free_hw(dev);
1031 mutex_init(&priv->conf_mutex);
1035 EXPORT_SYMBOL_GPL(p54_init_common);
1037 void p54_free_common(struct ieee80211_hw *dev)
1039 struct p54_common *priv = dev->priv;
1040 kfree(priv->iq_autocal);
1041 kfree(priv->output_limit);
1042 kfree(priv->curve_data);
1043 kfree(priv->cached_vdcf);
1045 EXPORT_SYMBOL_GPL(p54_free_common);
1047 static int __init p54_init(void)
1052 static void __exit p54_exit(void)
1056 module_init(p54_init);
1057 module_exit(p54_exit);