Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / net / wireless / p54common.c
blob692d00d969876a927a3e200847b7888aa1316020
2 /*
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>
22 #include "p54.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 void p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
32 struct p54_common *priv = dev->priv;
33 struct bootrec_exp_if *exp_if;
34 struct bootrec *bootrec;
35 u32 *data = (u32 *)fw->data;
36 u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
37 u8 *fw_version = NULL;
38 size_t len;
39 int i;
41 if (priv->rx_start)
42 return;
44 while (data < end_data && *data)
45 data++;
47 while (data < end_data && !*data)
48 data++;
50 bootrec = (struct bootrec *) data;
52 while (bootrec->data <= end_data &&
53 (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
54 u32 code = le32_to_cpu(bootrec->code);
55 switch (code) {
56 case BR_CODE_COMPONENT_ID:
57 switch (be32_to_cpu(*(__be32 *)bootrec->data)) {
58 case FW_FMAC:
59 printk(KERN_INFO "p54: FreeMAC firmware\n");
60 break;
61 case FW_LM20:
62 printk(KERN_INFO "p54: LM20 firmware\n");
63 break;
64 case FW_LM86:
65 printk(KERN_INFO "p54: LM86 firmware\n");
66 break;
67 case FW_LM87:
68 printk(KERN_INFO "p54: LM87 firmware - not supported yet!\n");
69 break;
70 default:
71 printk(KERN_INFO "p54: unknown firmware\n");
72 break;
74 break;
75 case BR_CODE_COMPONENT_VERSION:
76 /* 24 bytes should be enough for all firmwares */
77 if (strnlen((unsigned char*)bootrec->data, 24) < 24)
78 fw_version = (unsigned char*)bootrec->data;
79 break;
80 case BR_CODE_DESCR:
81 priv->rx_start = le32_to_cpu(((__le32 *)bootrec->data)[1]);
82 /* FIXME add sanity checking */
83 priv->rx_end = le32_to_cpu(((__le32 *)bootrec->data)[2]) - 0x3500;
84 break;
85 case BR_CODE_EXPOSED_IF:
86 exp_if = (struct bootrec_exp_if *) bootrec->data;
87 for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
88 if (exp_if[i].if_id == cpu_to_le16(0x1a))
89 priv->fw_var = le16_to_cpu(exp_if[i].variant);
90 break;
91 case BR_CODE_DEPENDENT_IF:
92 break;
93 case BR_CODE_END_OF_BRA:
94 case LEGACY_BR_CODE_END_OF_BRA:
95 end_data = NULL;
96 break;
97 default:
98 break;
100 bootrec = (struct bootrec *)&bootrec->data[len];
103 if (fw_version)
104 printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
105 fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
107 if (priv->fw_var >= 0x300) {
108 /* Firmware supports QoS, use it! */
109 priv->tx_stats.data[0].limit = 3;
110 priv->tx_stats.data[1].limit = 4;
111 priv->tx_stats.data[2].limit = 3;
112 priv->tx_stats.data[3].limit = 1;
113 dev->queues = 4;
116 EXPORT_SYMBOL_GPL(p54_parse_firmware);
118 static int p54_convert_rev0_to_rev1(struct ieee80211_hw *dev,
119 struct pda_pa_curve_data *curve_data)
121 struct p54_common *priv = dev->priv;
122 struct pda_pa_curve_data_sample_rev1 *rev1;
123 struct pda_pa_curve_data_sample_rev0 *rev0;
124 size_t cd_len = sizeof(*curve_data) +
125 (curve_data->points_per_channel*sizeof(*rev1) + 2) *
126 curve_data->channels;
127 unsigned int i, j;
128 void *source, *target;
130 priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
131 if (!priv->curve_data)
132 return -ENOMEM;
134 memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
135 source = curve_data->data;
136 target = priv->curve_data->data;
137 for (i = 0; i < curve_data->channels; i++) {
138 __le16 *freq = source;
139 source += sizeof(__le16);
140 *((__le16 *)target) = *freq;
141 target += sizeof(__le16);
142 for (j = 0; j < curve_data->points_per_channel; j++) {
143 rev1 = target;
144 rev0 = source;
146 rev1->rf_power = rev0->rf_power;
147 rev1->pa_detector = rev0->pa_detector;
148 rev1->data_64qam = rev0->pcv;
149 /* "invent" the points for the other modulations */
150 #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
151 rev1->data_16qam = SUB(rev0->pcv, 12);
152 rev1->data_qpsk = SUB(rev1->data_16qam, 12);
153 rev1->data_bpsk = SUB(rev1->data_qpsk, 12);
154 rev1->data_barker= SUB(rev1->data_bpsk, 14);
155 #undef SUB
156 target += sizeof(*rev1);
157 source += sizeof(*rev0);
161 return 0;
164 int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
166 struct p54_common *priv = dev->priv;
167 struct eeprom_pda_wrap *wrap = NULL;
168 struct pda_entry *entry;
169 <<<<<<< HEAD:drivers/net/wireless/p54common.c
170 int i = 0;
171 =======
172 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/p54common.c
173 unsigned int data_len, entry_len;
174 void *tmp;
175 int err;
176 <<<<<<< HEAD:drivers/net/wireless/p54common.c
177 =======
178 u8 *end = (u8 *)eeprom + len;
179 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/p54common.c
181 wrap = (struct eeprom_pda_wrap *) eeprom;
182 <<<<<<< HEAD:drivers/net/wireless/p54common.c
183 entry = (void *)wrap->data + wrap->len;
184 i += 2;
185 i += le16_to_cpu(entry->len)*2;
186 while (i < len) {
187 =======
188 entry = (void *)wrap->data + le16_to_cpu(wrap->len);
190 /* verify that at least the entry length/code fits */
191 while ((u8 *)entry <= end - sizeof(*entry)) {
192 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/p54common.c
193 entry_len = le16_to_cpu(entry->len);
194 data_len = ((entry_len - 1) << 1);
195 <<<<<<< HEAD:drivers/net/wireless/p54common.c
196 =======
198 /* abort if entry exceeds whole structure */
199 if ((u8 *)entry + sizeof(*entry) + data_len > end)
200 break;
202 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/p54common.c
203 switch (le16_to_cpu(entry->code)) {
204 case PDR_MAC_ADDRESS:
205 SET_IEEE80211_PERM_ADDR(dev, entry->data);
206 break;
207 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
208 if (data_len < 2) {
209 err = -EINVAL;
210 goto err;
213 if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
214 err = -EINVAL;
215 goto err;
218 priv->output_limit = kmalloc(entry->data[1] *
219 sizeof(*priv->output_limit), GFP_KERNEL);
221 if (!priv->output_limit) {
222 err = -ENOMEM;
223 goto err;
226 memcpy(priv->output_limit, &entry->data[2],
227 entry->data[1]*sizeof(*priv->output_limit));
228 priv->output_limit_len = entry->data[1];
229 break;
230 case PDR_PRISM_PA_CAL_CURVE_DATA:
231 if (data_len < sizeof(struct pda_pa_curve_data)) {
232 err = -EINVAL;
233 goto err;
236 if (((struct pda_pa_curve_data *)entry->data)->cal_method_rev) {
237 priv->curve_data = kmalloc(data_len, GFP_KERNEL);
238 if (!priv->curve_data) {
239 err = -ENOMEM;
240 goto err;
243 memcpy(priv->curve_data, entry->data, data_len);
244 } else {
245 err = p54_convert_rev0_to_rev1(dev, (struct pda_pa_curve_data *)entry->data);
246 if (err)
247 goto err;
250 break;
251 case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
252 priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
253 if (!priv->iq_autocal) {
254 err = -ENOMEM;
255 goto err;
258 memcpy(priv->iq_autocal, entry->data, data_len);
259 priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
260 break;
261 case PDR_INTERFACE_LIST:
262 tmp = entry->data;
263 while ((u8 *)tmp < entry->data + data_len) {
264 struct bootrec_exp_if *exp_if = tmp;
265 if (le16_to_cpu(exp_if->if_id) == 0xF)
266 priv->rxhw = exp_if->variant & cpu_to_le16(0x07);
267 tmp += sizeof(struct bootrec_exp_if);
269 break;
270 case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
271 priv->version = *(u8 *)(entry->data + 1);
272 break;
273 case PDR_END:
274 <<<<<<< HEAD:drivers/net/wireless/p54common.c
275 i = len;
276 =======
277 /* make it overrun */
278 entry_len = len;
279 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/p54common.c
280 break;
283 entry = (void *)entry + (entry_len + 1)*2;
284 <<<<<<< HEAD:drivers/net/wireless/p54common.c
285 i += 2;
286 i += entry_len*2;
287 =======
288 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/p54common.c
291 if (!priv->iq_autocal || !priv->output_limit || !priv->curve_data) {
292 printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
293 err = -EINVAL;
294 goto err;
297 return 0;
299 err:
300 if (priv->iq_autocal) {
301 kfree(priv->iq_autocal);
302 priv->iq_autocal = NULL;
305 if (priv->output_limit) {
306 kfree(priv->output_limit);
307 priv->output_limit = NULL;
310 if (priv->curve_data) {
311 kfree(priv->curve_data);
312 priv->curve_data = NULL;
315 printk(KERN_ERR "p54: eeprom parse failed!\n");
316 return err;
318 EXPORT_SYMBOL_GPL(p54_parse_eeprom);
320 void p54_fill_eeprom_readback(struct p54_control_hdr *hdr)
322 struct p54_eeprom_lm86 *eeprom_hdr;
324 hdr->magic1 = cpu_to_le16(0x8000);
325 hdr->len = cpu_to_le16(sizeof(*eeprom_hdr) + 0x2000);
326 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);
327 hdr->retry1 = hdr->retry2 = 0;
328 eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;
329 eeprom_hdr->offset = 0x0;
330 eeprom_hdr->len = cpu_to_le16(0x2000);
332 EXPORT_SYMBOL_GPL(p54_fill_eeprom_readback);
334 static void p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
336 struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
337 struct ieee80211_rx_status rx_status = {0};
338 u16 freq = le16_to_cpu(hdr->freq);
340 rx_status.ssi = hdr->rssi;
341 rx_status.rate = hdr->rate & 0x1f; /* report short preambles & CCK too */
342 rx_status.channel = freq == 2484 ? 14 : (freq - 2407)/5;
343 rx_status.freq = freq;
344 rx_status.phymode = MODE_IEEE80211G;
345 rx_status.antenna = hdr->antenna;
346 rx_status.mactime = le64_to_cpu(hdr->timestamp);
347 rx_status.flag |= RX_FLAG_TSFT;
349 skb_pull(skb, sizeof(*hdr));
350 skb_trim(skb, le16_to_cpu(hdr->len));
352 ieee80211_rx_irqsafe(dev, skb, &rx_status);
355 static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
357 struct p54_common *priv = dev->priv;
358 int i;
360 /* ieee80211_start_queues is great if all queues are really empty.
361 * But, what if some are full? */
363 for (i = 0; i < dev->queues; i++)
364 if (priv->tx_stats.data[i].len < priv->tx_stats.data[i].limit)
365 ieee80211_wake_queue(dev, i);
368 static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
370 struct p54_common *priv = dev->priv;
371 struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
372 struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
373 struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
374 u32 addr = le32_to_cpu(hdr->req_id) - 0x70;
375 struct memrecord *range = NULL;
376 u32 freed = 0;
377 u32 last_addr = priv->rx_start;
379 while (entry != (struct sk_buff *)&priv->tx_queue) {
380 range = (struct memrecord *)&entry->cb;
381 if (range->start_addr == addr) {
382 struct ieee80211_tx_status status = {{0}};
383 struct p54_control_hdr *entry_hdr;
384 struct p54_tx_control_allocdata *entry_data;
385 int pad = 0;
387 if (entry->next != (struct sk_buff *)&priv->tx_queue)
388 freed = ((struct memrecord *)&entry->next->cb)->start_addr - last_addr;
389 else
390 freed = priv->rx_end - last_addr;
392 last_addr = range->end_addr;
393 __skb_unlink(entry, &priv->tx_queue);
394 if (!range->control) {
395 kfree_skb(entry);
396 break;
398 memcpy(&status.control, range->control,
399 sizeof(status.control));
400 kfree(range->control);
401 priv->tx_stats.data[status.control.queue].len--;
403 entry_hdr = (struct p54_control_hdr *) entry->data;
404 entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
405 if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
406 pad = entry_data->align[0];
408 if (!(status.control.flags & IEEE80211_TXCTL_NO_ACK)) {
409 if (!(payload->status & 0x01))
410 status.flags |= IEEE80211_TX_STATUS_ACK;
411 else
412 status.excessive_retries = 1;
414 status.retry_count = payload->retries - 1;
415 status.ack_signal = le16_to_cpu(payload->ack_rssi);
416 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
417 ieee80211_tx_status_irqsafe(dev, entry, &status);
418 break;
419 } else
420 last_addr = range->end_addr;
421 entry = entry->next;
424 if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
425 sizeof(struct p54_control_hdr))
426 p54_wake_free_queues(dev);
429 static void p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
431 struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
433 switch (le16_to_cpu(hdr->type)) {
434 case P54_CONTROL_TYPE_TXDONE:
435 p54_rx_frame_sent(dev, skb);
436 break;
437 case P54_CONTROL_TYPE_BBP:
438 break;
439 default:
440 printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
441 wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
442 break;
446 /* returns zero if skb can be reused */
447 int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
449 u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
450 switch (type) {
451 case 0x00:
452 case 0x01:
453 p54_rx_data(dev, skb);
454 return -1;
455 case 0x4d:
456 /* TODO: do something better... but then again, I've never seen this happen */
457 printk(KERN_ERR "%s: Received fault. Probably need to restart hardware now..\n",
458 wiphy_name(dev->wiphy));
459 break;
460 case 0x80:
461 p54_rx_control(dev, skb);
462 break;
463 default:
464 printk(KERN_ERR "%s: unknown frame RXed (0x%02x)\n",
465 wiphy_name(dev->wiphy), type);
466 break;
468 return 0;
470 EXPORT_SYMBOL_GPL(p54_rx);
473 * So, the firmware is somewhat stupid and doesn't know what places in its
474 * memory incoming data should go to. By poking around in the firmware, we
475 * can find some unused memory to upload our packets to. However, data that we
476 * want the card to TX needs to stay intact until the card has told us that
477 * it is done with it. This function finds empty places we can upload to and
478 * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
479 * allocated areas.
481 static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
482 struct p54_control_hdr *data, u32 len,
483 struct ieee80211_tx_control *control)
485 struct p54_common *priv = dev->priv;
486 struct sk_buff *entry = priv->tx_queue.next;
487 struct sk_buff *target_skb = NULL;
488 struct memrecord *range;
489 u32 last_addr = priv->rx_start;
490 u32 largest_hole = 0;
491 u32 target_addr = priv->rx_start;
492 unsigned long flags;
493 unsigned int left;
494 len = (len + 0x170 + 3) & ~0x3; /* 0x70 headroom, 0x100 tailroom */
496 spin_lock_irqsave(&priv->tx_queue.lock, flags);
497 left = skb_queue_len(&priv->tx_queue);
498 while (left--) {
499 u32 hole_size;
500 range = (struct memrecord *)&entry->cb;
501 hole_size = range->start_addr - last_addr;
502 if (!target_skb && hole_size >= len) {
503 target_skb = entry->prev;
504 hole_size -= len;
505 target_addr = last_addr;
507 largest_hole = max(largest_hole, hole_size);
508 last_addr = range->end_addr;
509 entry = entry->next;
511 if (!target_skb && priv->rx_end - last_addr >= len) {
512 target_skb = priv->tx_queue.prev;
513 largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
514 if (!skb_queue_empty(&priv->tx_queue)) {
515 range = (struct memrecord *)&target_skb->cb;
516 target_addr = range->end_addr;
518 } else
519 largest_hole = max(largest_hole, priv->rx_end - last_addr);
521 if (skb) {
522 range = (struct memrecord *)&skb->cb;
523 range->start_addr = target_addr;
524 range->end_addr = target_addr + len;
525 range->control = control;
526 __skb_queue_after(&priv->tx_queue, target_skb, skb);
527 if (largest_hole < IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
528 sizeof(struct p54_control_hdr))
529 ieee80211_stop_queues(dev);
531 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
533 data->req_id = cpu_to_le32(target_addr + 0x70);
536 static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb,
537 struct ieee80211_tx_control *control)
539 struct ieee80211_tx_queue_stats_data *current_queue;
540 struct p54_common *priv = dev->priv;
541 struct p54_control_hdr *hdr;
542 struct p54_tx_control_allocdata *txhdr;
543 struct ieee80211_tx_control *control_copy;
544 size_t padding, len;
545 u8 rate;
547 current_queue = &priv->tx_stats.data[control->queue];
548 if (unlikely(current_queue->len > current_queue->limit))
549 return NETDEV_TX_BUSY;
550 current_queue->len++;
551 current_queue->count++;
552 if (current_queue->len == current_queue->limit)
553 ieee80211_stop_queue(dev, control->queue);
555 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
556 len = skb->len;
558 control_copy = kmalloc(sizeof(*control), GFP_ATOMIC);
559 if (control_copy)
560 memcpy(control_copy, control, sizeof(*control));
562 txhdr = (struct p54_tx_control_allocdata *)
563 skb_push(skb, sizeof(*txhdr) + padding);
564 hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
566 if (padding)
567 hdr->magic1 = cpu_to_le16(0x4010);
568 else
569 hdr->magic1 = cpu_to_le16(0x0010);
570 hdr->len = cpu_to_le16(len);
571 hdr->type = (control->flags & IEEE80211_TXCTL_NO_ACK) ? 0 : cpu_to_le16(1);
572 hdr->retry1 = hdr->retry2 = control->retry_limit;
573 p54_assign_address(dev, skb, hdr, skb->len, control_copy);
575 memset(txhdr->wep_key, 0x0, 16);
576 txhdr->padding = 0;
577 txhdr->padding2 = 0;
579 /* TODO: add support for alternate retry TX rates */
580 rate = control->tx_rate;
581 if (control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
582 rate |= 0x40;
583 else if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
584 rate |= 0x20;
585 memset(txhdr->rateset, rate, 8);
586 txhdr->wep_key_present = 0;
587 txhdr->wep_key_len = 0;
588 txhdr->frame_type = cpu_to_le32(control->queue + 4);
589 txhdr->magic4 = 0;
590 txhdr->antenna = (control->antenna_sel_tx == 0) ?
591 2 : control->antenna_sel_tx - 1;
592 txhdr->output_power = 0x7f; // HW Maximum
593 txhdr->magic5 = (control->flags & IEEE80211_TXCTL_NO_ACK) ?
594 0 : ((rate > 0x3) ? cpu_to_le32(0x33) : cpu_to_le32(0x23));
595 if (padding)
596 txhdr->align[0] = padding;
598 priv->tx(dev, hdr, skb->len, 0);
599 return 0;
602 static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
603 const u8 *dst, const u8 *src, u8 antenna,
604 u32 magic3, u32 magic8, u32 magic9)
606 struct p54_common *priv = dev->priv;
607 struct p54_control_hdr *hdr;
608 struct p54_tx_control_filter *filter;
610 hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
611 priv->tx_hdr_len, GFP_ATOMIC);
612 if (!hdr)
613 return -ENOMEM;
615 hdr = (void *)hdr + priv->tx_hdr_len;
617 filter = (struct p54_tx_control_filter *) hdr->data;
618 hdr->magic1 = cpu_to_le16(0x8001);
619 hdr->len = cpu_to_le16(sizeof(*filter));
620 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*filter), NULL);
621 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
623 filter->filter_type = cpu_to_le16(filter_type);
624 memcpy(filter->dst, dst, ETH_ALEN);
625 if (!src)
626 memset(filter->src, ~0, ETH_ALEN);
627 else
628 memcpy(filter->src, src, ETH_ALEN);
629 filter->antenna = antenna;
630 filter->magic3 = cpu_to_le32(magic3);
631 filter->rx_addr = cpu_to_le32(priv->rx_end);
632 filter->max_rx = cpu_to_le16(0x0620); /* FIXME: for usb ver 1.. maybe */
633 filter->rxhw = priv->rxhw;
634 filter->magic8 = cpu_to_le16(magic8);
635 filter->magic9 = cpu_to_le16(magic9);
637 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*filter), 1);
638 return 0;
641 static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
643 struct p54_common *priv = dev->priv;
644 struct p54_control_hdr *hdr;
645 struct p54_tx_control_channel *chan;
646 unsigned int i;
647 size_t payload_len = sizeof(*chan) + sizeof(u32)*2 +
648 sizeof(*chan->curve_data) *
649 priv->curve_data->points_per_channel;
650 void *entry;
652 hdr = kzalloc(sizeof(*hdr) + payload_len +
653 priv->tx_hdr_len, GFP_KERNEL);
654 if (!hdr)
655 return -ENOMEM;
657 hdr = (void *)hdr + priv->tx_hdr_len;
659 chan = (struct p54_tx_control_channel *) hdr->data;
661 hdr->magic1 = cpu_to_le16(0x8001);
662 hdr->len = cpu_to_le16(sizeof(*chan));
663 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
664 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + payload_len, NULL);
666 chan->magic1 = cpu_to_le16(0x1);
667 chan->magic2 = cpu_to_le16(0x0);
669 for (i = 0; i < priv->iq_autocal_len; i++) {
670 if (priv->iq_autocal[i].freq != freq)
671 continue;
673 memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
674 sizeof(*priv->iq_autocal));
675 break;
677 if (i == priv->iq_autocal_len)
678 goto err;
680 for (i = 0; i < priv->output_limit_len; i++) {
681 if (priv->output_limit[i].freq != freq)
682 continue;
684 chan->val_barker = 0x38;
685 chan->val_bpsk = priv->output_limit[i].val_bpsk;
686 chan->val_qpsk = priv->output_limit[i].val_qpsk;
687 chan->val_16qam = priv->output_limit[i].val_16qam;
688 chan->val_64qam = priv->output_limit[i].val_64qam;
689 break;
691 if (i == priv->output_limit_len)
692 goto err;
694 chan->pa_points_per_curve = priv->curve_data->points_per_channel;
696 entry = priv->curve_data->data;
697 for (i = 0; i < priv->curve_data->channels; i++) {
698 if (*((__le16 *)entry) != freq) {
699 entry += sizeof(__le16);
700 entry += sizeof(struct pda_pa_curve_data_sample_rev1) *
701 chan->pa_points_per_curve;
702 continue;
705 entry += sizeof(__le16);
706 memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
707 chan->pa_points_per_curve);
708 break;
711 memcpy(hdr->data + payload_len - 4, &chan->val_bpsk, 4);
713 priv->tx(dev, hdr, sizeof(*hdr) + payload_len, 1);
714 return 0;
716 err:
717 printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
718 kfree(hdr);
719 return -EINVAL;
722 static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
724 struct p54_common *priv = dev->priv;
725 struct p54_control_hdr *hdr;
726 struct p54_tx_control_led *led;
728 hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
729 priv->tx_hdr_len, GFP_KERNEL);
730 if (!hdr)
731 return -ENOMEM;
733 hdr = (void *)hdr + priv->tx_hdr_len;
734 hdr->magic1 = cpu_to_le16(0x8001);
735 hdr->len = cpu_to_le16(sizeof(*led));
736 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
737 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led), NULL);
739 led = (struct p54_tx_control_led *) hdr->data;
740 led->mode = cpu_to_le16(mode);
741 led->led_permanent = cpu_to_le16(link);
742 led->led_temporary = cpu_to_le16(act);
743 led->duration = cpu_to_le16(1000);
745 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
747 return 0;
750 #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, burst) \
751 do { \
752 queue.aifs = cpu_to_le16(ai_fs); \
753 queue.cwmin = cpu_to_le16(cw_min); \
754 queue.cwmax = cpu_to_le16(cw_max); \
755 queue.txop = (burst == 0) ? \
756 0 : cpu_to_le16((burst * 100) / 32 + 1); \
757 } while(0)
759 static void p54_init_vdcf(struct ieee80211_hw *dev)
761 struct p54_common *priv = dev->priv;
762 struct p54_control_hdr *hdr;
763 struct p54_tx_control_vdcf *vdcf;
765 /* all USB V1 adapters need a extra headroom */
766 hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
767 hdr->magic1 = cpu_to_le16(0x8001);
768 hdr->len = cpu_to_le16(sizeof(*vdcf));
769 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
770 hdr->req_id = cpu_to_le32(priv->rx_start);
772 vdcf = (struct p54_tx_control_vdcf *) hdr->data;
774 P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 0x000f);
775 P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 0x001e);
776 P54_SET_QUEUE(vdcf->queue[2], 0x0002, 0x000f, 0x03ff, 0x0014);
777 P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0x0000);
780 static void p54_set_vdcf(struct ieee80211_hw *dev)
782 struct p54_common *priv = dev->priv;
783 struct p54_control_hdr *hdr;
784 struct p54_tx_control_vdcf *vdcf;
786 hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
788 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf), NULL);
790 vdcf = (struct p54_tx_control_vdcf *) hdr->data;
792 if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
793 vdcf->slottime = 9;
794 vdcf->magic1 = 0x00;
795 vdcf->magic2 = 0x10;
796 } else {
797 vdcf->slottime = 20;
798 vdcf->magic1 = 0x0a;
799 vdcf->magic2 = 0x06;
802 /* (see prism54/isl_oid.h for further details) */
803 vdcf->frameburst = cpu_to_le16(0);
805 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
808 static int p54_start(struct ieee80211_hw *dev)
810 struct p54_common *priv = dev->priv;
811 int err;
813 err = priv->open(dev);
814 if (!err)
815 priv->mode = IEEE80211_IF_TYPE_MNTR;
817 return err;
820 static void p54_stop(struct ieee80211_hw *dev)
822 struct p54_common *priv = dev->priv;
823 struct sk_buff *skb;
824 while ((skb = skb_dequeue(&priv->tx_queue))) {
825 struct memrecord *range = (struct memrecord *)&skb->cb;
826 if (range->control)
827 kfree(range->control);
828 kfree_skb(skb);
830 priv->stop(dev);
831 priv->mode = IEEE80211_IF_TYPE_INVALID;
834 static int p54_add_interface(struct ieee80211_hw *dev,
835 struct ieee80211_if_init_conf *conf)
837 struct p54_common *priv = dev->priv;
839 if (priv->mode != IEEE80211_IF_TYPE_MNTR)
840 return -EOPNOTSUPP;
842 switch (conf->type) {
843 case IEEE80211_IF_TYPE_STA:
844 priv->mode = conf->type;
845 break;
846 default:
847 return -EOPNOTSUPP;
850 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
852 p54_set_filter(dev, 0, priv->mac_addr, NULL, 0, 1, 0, 0xF642);
853 p54_set_filter(dev, 0, priv->mac_addr, NULL, 1, 0, 0, 0xF642);
855 switch (conf->type) {
856 case IEEE80211_IF_TYPE_STA:
857 p54_set_filter(dev, 1, priv->mac_addr, NULL, 0, 0x15F, 0x1F4, 0);
858 break;
859 default:
860 BUG(); /* impossible */
861 break;
864 p54_set_leds(dev, 1, 0, 0);
866 return 0;
869 static void p54_remove_interface(struct ieee80211_hw *dev,
870 struct ieee80211_if_init_conf *conf)
872 struct p54_common *priv = dev->priv;
873 priv->mode = IEEE80211_IF_TYPE_MNTR;
874 memset(priv->mac_addr, 0, ETH_ALEN);
875 p54_set_filter(dev, 0, priv->mac_addr, NULL, 2, 0, 0, 0);
878 static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
880 int ret;
882 ret = p54_set_freq(dev, cpu_to_le16(conf->freq));
883 p54_set_vdcf(dev);
884 return ret;
887 static int p54_config_interface(struct ieee80211_hw *dev,
888 struct ieee80211_vif *vif,
889 struct ieee80211_if_conf *conf)
891 struct p54_common *priv = dev->priv;
893 p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 0, 1, 0, 0xF642);
894 p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 2, 0, 0, 0);
895 p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
896 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
897 return 0;
900 static void p54_configure_filter(struct ieee80211_hw *dev,
901 unsigned int changed_flags,
902 unsigned int *total_flags,
903 int mc_count, struct dev_mc_list *mclist)
905 struct p54_common *priv = dev->priv;
907 *total_flags &= FIF_BCN_PRBRESP_PROMISC;
909 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
910 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
911 p54_set_filter(dev, 0, priv->mac_addr,
912 NULL, 2, 0, 0, 0);
913 else
914 p54_set_filter(dev, 0, priv->mac_addr,
915 priv->bssid, 2, 0, 0, 0);
919 static int p54_conf_tx(struct ieee80211_hw *dev, int queue,
920 const struct ieee80211_tx_queue_params *params)
922 struct p54_common *priv = dev->priv;
923 struct p54_tx_control_vdcf *vdcf;
925 vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
926 ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
928 if ((params) && !((queue < 0) || (queue > 4))) {
929 P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
930 params->cw_min, params->cw_max, params->burst_time);
931 } else
932 return -EINVAL;
934 p54_set_vdcf(dev);
936 return 0;
939 static int p54_get_stats(struct ieee80211_hw *dev,
940 struct ieee80211_low_level_stats *stats)
942 /* TODO */
943 return 0;
946 static int p54_get_tx_stats(struct ieee80211_hw *dev,
947 struct ieee80211_tx_queue_stats *stats)
949 struct p54_common *priv = dev->priv;
950 unsigned int i;
952 for (i = 0; i < dev->queues; i++)
953 memcpy(&stats->data[i], &priv->tx_stats.data[i],
954 sizeof(stats->data[i]));
956 return 0;
959 static const struct ieee80211_ops p54_ops = {
960 .tx = p54_tx,
961 .start = p54_start,
962 .stop = p54_stop,
963 .add_interface = p54_add_interface,
964 .remove_interface = p54_remove_interface,
965 .config = p54_config,
966 .config_interface = p54_config_interface,
967 .configure_filter = p54_configure_filter,
968 .conf_tx = p54_conf_tx,
969 .get_stats = p54_get_stats,
970 .get_tx_stats = p54_get_tx_stats
973 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
975 struct ieee80211_hw *dev;
976 struct p54_common *priv;
977 int i;
979 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
980 if (!dev)
981 return NULL;
983 priv = dev->priv;
984 priv->mode = IEEE80211_IF_TYPE_INVALID;
985 skb_queue_head_init(&priv->tx_queue);
986 memcpy(priv->channels, p54_channels, sizeof(p54_channels));
987 memcpy(priv->rates, p54_rates, sizeof(p54_rates));
988 priv->modes[1].mode = MODE_IEEE80211B;
989 priv->modes[1].num_rates = 4;
990 priv->modes[1].rates = priv->rates;
991 priv->modes[1].num_channels = ARRAY_SIZE(p54_channels);
992 priv->modes[1].channels = priv->channels;
993 priv->modes[0].mode = MODE_IEEE80211G;
994 priv->modes[0].num_rates = ARRAY_SIZE(p54_rates);
995 priv->modes[0].rates = priv->rates;
996 priv->modes[0].num_channels = ARRAY_SIZE(p54_channels);
997 priv->modes[0].channels = priv->channels;
998 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
999 IEEE80211_HW_RX_INCLUDES_FCS;
1000 dev->channel_change_time = 1000; /* TODO: find actual value */
1001 dev->max_rssi = 127;
1003 priv->tx_stats.data[0].limit = 5;
1004 dev->queues = 1;
1006 dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
1007 sizeof(struct p54_tx_control_allocdata);
1009 priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf) +
1010 priv->tx_hdr_len + sizeof(struct p54_control_hdr), GFP_KERNEL);
1012 if (!priv->cached_vdcf) {
1013 ieee80211_free_hw(dev);
1014 return NULL;
1017 p54_init_vdcf(dev);
1019 for (i = 0; i < 2; i++) {
1020 if (ieee80211_register_hwmode(dev, &priv->modes[i])) {
1021 kfree(priv->cached_vdcf);
1022 ieee80211_free_hw(dev);
1023 return NULL;
1027 return dev;
1029 EXPORT_SYMBOL_GPL(p54_init_common);
1031 void p54_free_common(struct ieee80211_hw *dev)
1033 struct p54_common *priv = dev->priv;
1034 kfree(priv->iq_autocal);
1035 kfree(priv->output_limit);
1036 kfree(priv->curve_data);
1037 kfree(priv->cached_vdcf);
1039 EXPORT_SYMBOL_GPL(p54_free_common);
1041 static int __init p54_init(void)
1043 return 0;
1046 static void __exit p54_exit(void)
1050 module_init(p54_init);
1051 module_exit(p54_exit);