Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / net / wireless / p54 / p54common.c
1 /*
2  * Common code for mac80211 Prism54 drivers
3  *
4  * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
5  * Copyright (c) 2007, Christian Lamparter <chunkeey@web.de>
6  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7  *
8  * Based on:
9  * - the islsm (softmac prism54) driver, which is:
10  *   Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
11  * - stlc45xx driver
12  *   Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18
19 #include <linux/init.h>
20 #include <linux/firmware.h>
21 #include <linux/etherdevice.h>
22
23 #include <net/mac80211.h>
24
25 #include "p54.h"
26 #include "p54common.h"
27
28 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
29 MODULE_DESCRIPTION("Softmac Prism54 common code");
30 MODULE_LICENSE("GPL");
31 MODULE_ALIAS("prism54common");
32
33 static struct ieee80211_rate p54_bgrates[] = {
34         { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
35         { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
36         { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
37         { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
38         { .bitrate = 60, .hw_value = 4, },
39         { .bitrate = 90, .hw_value = 5, },
40         { .bitrate = 120, .hw_value = 6, },
41         { .bitrate = 180, .hw_value = 7, },
42         { .bitrate = 240, .hw_value = 8, },
43         { .bitrate = 360, .hw_value = 9, },
44         { .bitrate = 480, .hw_value = 10, },
45         { .bitrate = 540, .hw_value = 11, },
46 };
47
48 static struct ieee80211_channel p54_bgchannels[] = {
49         { .center_freq = 2412, .hw_value = 1, },
50         { .center_freq = 2417, .hw_value = 2, },
51         { .center_freq = 2422, .hw_value = 3, },
52         { .center_freq = 2427, .hw_value = 4, },
53         { .center_freq = 2432, .hw_value = 5, },
54         { .center_freq = 2437, .hw_value = 6, },
55         { .center_freq = 2442, .hw_value = 7, },
56         { .center_freq = 2447, .hw_value = 8, },
57         { .center_freq = 2452, .hw_value = 9, },
58         { .center_freq = 2457, .hw_value = 10, },
59         { .center_freq = 2462, .hw_value = 11, },
60         { .center_freq = 2467, .hw_value = 12, },
61         { .center_freq = 2472, .hw_value = 13, },
62         { .center_freq = 2484, .hw_value = 14, },
63 };
64
65 static struct ieee80211_supported_band band_2GHz = {
66         .channels = p54_bgchannels,
67         .n_channels = ARRAY_SIZE(p54_bgchannels),
68         .bitrates = p54_bgrates,
69         .n_bitrates = ARRAY_SIZE(p54_bgrates),
70 };
71
72 static struct ieee80211_rate p54_arates[] = {
73         { .bitrate = 60, .hw_value = 4, },
74         { .bitrate = 90, .hw_value = 5, },
75         { .bitrate = 120, .hw_value = 6, },
76         { .bitrate = 180, .hw_value = 7, },
77         { .bitrate = 240, .hw_value = 8, },
78         { .bitrate = 360, .hw_value = 9, },
79         { .bitrate = 480, .hw_value = 10, },
80         { .bitrate = 540, .hw_value = 11, },
81 };
82
83 static struct ieee80211_channel p54_achannels[] = {
84         { .center_freq = 4920 },
85         { .center_freq = 4940 },
86         { .center_freq = 4960 },
87         { .center_freq = 4980 },
88         { .center_freq = 5040 },
89         { .center_freq = 5060 },
90         { .center_freq = 5080 },
91         { .center_freq = 5170 },
92         { .center_freq = 5180 },
93         { .center_freq = 5190 },
94         { .center_freq = 5200 },
95         { .center_freq = 5210 },
96         { .center_freq = 5220 },
97         { .center_freq = 5230 },
98         { .center_freq = 5240 },
99         { .center_freq = 5260 },
100         { .center_freq = 5280 },
101         { .center_freq = 5300 },
102         { .center_freq = 5320 },
103         { .center_freq = 5500 },
104         { .center_freq = 5520 },
105         { .center_freq = 5540 },
106         { .center_freq = 5560 },
107         { .center_freq = 5580 },
108         { .center_freq = 5600 },
109         { .center_freq = 5620 },
110         { .center_freq = 5640 },
111         { .center_freq = 5660 },
112         { .center_freq = 5680 },
113         { .center_freq = 5700 },
114         { .center_freq = 5745 },
115         { .center_freq = 5765 },
116         { .center_freq = 5785 },
117         { .center_freq = 5805 },
118         { .center_freq = 5825 },
119 };
120
121 static struct ieee80211_supported_band band_5GHz = {
122         .channels = p54_achannels,
123         .n_channels = ARRAY_SIZE(p54_achannels),
124         .bitrates = p54_arates,
125         .n_bitrates = ARRAY_SIZE(p54_arates),
126 };
127
128 int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
129 {
130         struct p54_common *priv = dev->priv;
131         struct bootrec_exp_if *exp_if;
132         struct bootrec *bootrec;
133         u32 *data = (u32 *)fw->data;
134         u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
135         u8 *fw_version = NULL;
136         size_t len;
137         int i;
138
139         if (priv->rx_start)
140                 return 0;
141
142         while (data < end_data && *data)
143                 data++;
144
145         while (data < end_data && !*data)
146                 data++;
147
148         bootrec = (struct bootrec *) data;
149
150         while (bootrec->data <= end_data &&
151                (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
152                 u32 code = le32_to_cpu(bootrec->code);
153                 switch (code) {
154                 case BR_CODE_COMPONENT_ID:
155                         priv->fw_interface = be32_to_cpup((__be32 *)
156                                              bootrec->data);
157                         switch (priv->fw_interface) {
158                         case FW_FMAC:
159                                 printk(KERN_INFO "p54: FreeMAC firmware\n");
160                                 break;
161                         case FW_LM20:
162                                 printk(KERN_INFO "p54: LM20 firmware\n");
163                                 break;
164                         case FW_LM86:
165                                 printk(KERN_INFO "p54: LM86 firmware\n");
166                                 break;
167                         case FW_LM87:
168                                 printk(KERN_INFO "p54: LM87 firmware\n");
169                                 break;
170                         default:
171                                 printk(KERN_INFO "p54: unknown firmware\n");
172                                 break;
173                         }
174                         break;
175                 case BR_CODE_COMPONENT_VERSION:
176                         /* 24 bytes should be enough for all firmwares */
177                         if (strnlen((unsigned char*)bootrec->data, 24) < 24)
178                                 fw_version = (unsigned char*)bootrec->data;
179                         break;
180                 case BR_CODE_DESCR: {
181                         struct bootrec_desc *desc =
182                                 (struct bootrec_desc *)bootrec->data;
183                         priv->rx_start = le32_to_cpu(desc->rx_start);
184                         /* FIXME add sanity checking */
185                         priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
186                         priv->headroom = desc->headroom;
187                         priv->tailroom = desc->tailroom;
188                         if (le32_to_cpu(bootrec->len) == 11)
189                                 priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
190                         else
191                                 priv->rx_mtu = (size_t)
192                                         0x620 - priv->tx_hdr_len;
193                         break;
194                         }
195                 case BR_CODE_EXPOSED_IF:
196                         exp_if = (struct bootrec_exp_if *) bootrec->data;
197                         for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
198                                 if (exp_if[i].if_id == cpu_to_le16(0x1a))
199                                         priv->fw_var = le16_to_cpu(exp_if[i].variant);
200                         break;
201                 case BR_CODE_DEPENDENT_IF:
202                         break;
203                 case BR_CODE_END_OF_BRA:
204                 case LEGACY_BR_CODE_END_OF_BRA:
205                         end_data = NULL;
206                         break;
207                 default:
208                         break;
209                 }
210                 bootrec = (struct bootrec *)&bootrec->data[len];
211         }
212
213         if (fw_version)
214                 printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
215                         fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
216
217         if (priv->fw_var < 0x500)
218                 printk(KERN_INFO "p54: you are using an obsolete firmware. "
219                        "visit http://wireless.kernel.org/en/users/Drivers/p54 "
220                        "and grab one for \"kernel >= 2.6.28\"!\n");
221
222         if (priv->fw_var >= 0x300) {
223                 /* Firmware supports QoS, use it! */
224                 priv->tx_stats[4].limit = 3;            /* AC_VO */
225                 priv->tx_stats[5].limit = 4;            /* AC_VI */
226                 priv->tx_stats[6].limit = 3;            /* AC_BE */
227                 priv->tx_stats[7].limit = 2;            /* AC_BK */
228                 dev->queues = 4;
229         }
230
231         return 0;
232 }
233 EXPORT_SYMBOL_GPL(p54_parse_firmware);
234
235 static int p54_convert_rev0(struct ieee80211_hw *dev,
236                             struct pda_pa_curve_data *curve_data)
237 {
238         struct p54_common *priv = dev->priv;
239         struct p54_pa_curve_data_sample *dst;
240         struct pda_pa_curve_data_sample_rev0 *src;
241         size_t cd_len = sizeof(*curve_data) +
242                 (curve_data->points_per_channel*sizeof(*dst) + 2) *
243                  curve_data->channels;
244         unsigned int i, j;
245         void *source, *target;
246
247         priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
248         if (!priv->curve_data)
249                 return -ENOMEM;
250
251         memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
252         source = curve_data->data;
253         target = priv->curve_data->data;
254         for (i = 0; i < curve_data->channels; i++) {
255                 __le16 *freq = source;
256                 source += sizeof(__le16);
257                 *((__le16 *)target) = *freq;
258                 target += sizeof(__le16);
259                 for (j = 0; j < curve_data->points_per_channel; j++) {
260                         dst = target;
261                         src = source;
262
263                         dst->rf_power = src->rf_power;
264                         dst->pa_detector = src->pa_detector;
265                         dst->data_64qam = src->pcv;
266                         /* "invent" the points for the other modulations */
267 #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
268                         dst->data_16qam = SUB(src->pcv, 12);
269                         dst->data_qpsk = SUB(dst->data_16qam, 12);
270                         dst->data_bpsk = SUB(dst->data_qpsk, 12);
271                         dst->data_barker = SUB(dst->data_bpsk, 14);
272 #undef SUB
273                         target += sizeof(*dst);
274                         source += sizeof(*src);
275                 }
276         }
277
278         return 0;
279 }
280
281 static int p54_convert_rev1(struct ieee80211_hw *dev,
282                             struct pda_pa_curve_data *curve_data)
283 {
284         struct p54_common *priv = dev->priv;
285         struct p54_pa_curve_data_sample *dst;
286         struct pda_pa_curve_data_sample_rev1 *src;
287         size_t cd_len = sizeof(*curve_data) +
288                 (curve_data->points_per_channel*sizeof(*dst) + 2) *
289                  curve_data->channels;
290         unsigned int i, j;
291         void *source, *target;
292
293         priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
294         if (!priv->curve_data)
295                 return -ENOMEM;
296
297         memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
298         source = curve_data->data;
299         target = priv->curve_data->data;
300         for (i = 0; i < curve_data->channels; i++) {
301                 __le16 *freq = source;
302                 source += sizeof(__le16);
303                 *((__le16 *)target) = *freq;
304                 target += sizeof(__le16);
305                 for (j = 0; j < curve_data->points_per_channel; j++) {
306                         memcpy(target, source, sizeof(*src));
307
308                         target += sizeof(*dst);
309                         source += sizeof(*src);
310                 }
311                 source++;
312         }
313
314         return 0;
315 }
316
317 static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
318                               "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
319 static int p54_init_xbow_synth(struct ieee80211_hw *dev);
320
321 static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
322 {
323         struct p54_common *priv = dev->priv;
324         struct eeprom_pda_wrap *wrap = NULL;
325         struct pda_entry *entry;
326         unsigned int data_len, entry_len;
327         void *tmp;
328         int err;
329         u8 *end = (u8 *)eeprom + len;
330         u16 synth = 0;
331
332         wrap = (struct eeprom_pda_wrap *) eeprom;
333         entry = (void *)wrap->data + le16_to_cpu(wrap->len);
334
335         /* verify that at least the entry length/code fits */
336         while ((u8 *)entry <= end - sizeof(*entry)) {
337                 entry_len = le16_to_cpu(entry->len);
338                 data_len = ((entry_len - 1) << 1);
339
340                 /* abort if entry exceeds whole structure */
341                 if ((u8 *)entry + sizeof(*entry) + data_len > end)
342                         break;
343
344                 switch (le16_to_cpu(entry->code)) {
345                 case PDR_MAC_ADDRESS:
346                         SET_IEEE80211_PERM_ADDR(dev, entry->data);
347                         break;
348                 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
349                         if (data_len < 2) {
350                                 err = -EINVAL;
351                                 goto err;
352                         }
353
354                         if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
355                                 err = -EINVAL;
356                                 goto err;
357                         }
358
359                         priv->output_limit = kmalloc(entry->data[1] *
360                                 sizeof(*priv->output_limit), GFP_KERNEL);
361
362                         if (!priv->output_limit) {
363                                 err = -ENOMEM;
364                                 goto err;
365                         }
366
367                         memcpy(priv->output_limit, &entry->data[2],
368                                entry->data[1]*sizeof(*priv->output_limit));
369                         priv->output_limit_len = entry->data[1];
370                         break;
371                 case PDR_PRISM_PA_CAL_CURVE_DATA: {
372                         struct pda_pa_curve_data *curve_data =
373                                 (struct pda_pa_curve_data *)entry->data;
374                         if (data_len < sizeof(*curve_data)) {
375                                 err = -EINVAL;
376                                 goto err;
377                         }
378
379                         switch (curve_data->cal_method_rev) {
380                         case 0:
381                                 err = p54_convert_rev0(dev, curve_data);
382                                 break;
383                         case 1:
384                                 err = p54_convert_rev1(dev, curve_data);
385                                 break;
386                         default:
387                                 printk(KERN_ERR "p54: unknown curve data "
388                                                 "revision %d\n",
389                                                 curve_data->cal_method_rev);
390                                 err = -ENODEV;
391                                 break;
392                         }
393                         if (err)
394                                 goto err;
395
396                 }
397                 case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
398                         priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
399                         if (!priv->iq_autocal) {
400                                 err = -ENOMEM;
401                                 goto err;
402                         }
403
404                         memcpy(priv->iq_autocal, entry->data, data_len);
405                         priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
406                         break;
407                 case PDR_INTERFACE_LIST:
408                         tmp = entry->data;
409                         while ((u8 *)tmp < entry->data + data_len) {
410                                 struct bootrec_exp_if *exp_if = tmp;
411                                 if (le16_to_cpu(exp_if->if_id) == 0xf)
412                                         synth = le16_to_cpu(exp_if->variant);
413                                 tmp += sizeof(struct bootrec_exp_if);
414                         }
415                         break;
416                 case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
417                         priv->version = *(u8 *)(entry->data + 1);
418                         break;
419                 case PDR_END:
420                         /* make it overrun */
421                         entry_len = len;
422                         break;
423                 case PDR_MANUFACTURING_PART_NUMBER:
424                 case PDR_PDA_VERSION:
425                 case PDR_NIC_SERIAL_NUMBER:
426                 case PDR_REGULATORY_DOMAIN_LIST:
427                 case PDR_TEMPERATURE_TYPE:
428                 case PDR_PRISM_PCI_IDENTIFIER:
429                 case PDR_COUNTRY_INFORMATION:
430                 case PDR_OEM_NAME:
431                 case PDR_PRODUCT_NAME:
432                 case PDR_UTF8_OEM_NAME:
433                 case PDR_UTF8_PRODUCT_NAME:
434                 case PDR_COUNTRY_LIST:
435                 case PDR_DEFAULT_COUNTRY:
436                 case PDR_ANTENNA_GAIN:
437                 case PDR_PRISM_INDIGO_PA_CALIBRATION_DATA:
438                 case PDR_RSSI_LINEAR_APPROXIMATION:
439                 case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
440                 case PDR_REGULATORY_POWER_LIMITS:
441                 case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
442                 case PDR_RADIATED_TRANSMISSION_CORRECTION:
443                 case PDR_PRISM_TX_IQ_CALIBRATION:
444                 case PDR_BASEBAND_REGISTERS:
445                 case PDR_PER_CHANNEL_BASEBAND_REGISTERS:
446                         break;
447                 default:
448                         printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
449                                 le16_to_cpu(entry->code));
450                         break;
451                 }
452
453                 entry = (void *)entry + (entry_len + 1)*2;
454         }
455
456         if (!synth || !priv->iq_autocal || !priv->output_limit ||
457             !priv->curve_data) {
458                 printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
459                 err = -EINVAL;
460                 goto err;
461         }
462
463         priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
464         if (priv->rxhw == 4)
465                 p54_init_xbow_synth(dev);
466         if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
467                 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
468         if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
469                 dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
470
471         if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
472                 u8 perm_addr[ETH_ALEN];
473
474                 printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
475                         wiphy_name(dev->wiphy));
476                 random_ether_addr(perm_addr);
477                 SET_IEEE80211_PERM_ADDR(dev, perm_addr);
478         }
479
480         printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
481                 wiphy_name(dev->wiphy),
482                 dev->wiphy->perm_addr,
483                 priv->version, p54_rf_chips[priv->rxhw]);
484
485         return 0;
486
487   err:
488         if (priv->iq_autocal) {
489                 kfree(priv->iq_autocal);
490                 priv->iq_autocal = NULL;
491         }
492
493         if (priv->output_limit) {
494                 kfree(priv->output_limit);
495                 priv->output_limit = NULL;
496         }
497
498         if (priv->curve_data) {
499                 kfree(priv->curve_data);
500                 priv->curve_data = NULL;
501         }
502
503         printk(KERN_ERR "p54: eeprom parse failed!\n");
504         return err;
505 }
506
507 static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
508 {
509         /* TODO: get the rssi_add & rssi_mul data from the eeprom */
510         return ((rssi * 0x83) / 64 - 400) / 4;
511 }
512
513 static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
514 {
515         struct p54_common *priv = dev->priv;
516         struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
517         struct ieee80211_rx_status rx_status = {0};
518         u16 freq = le16_to_cpu(hdr->freq);
519         size_t header_len = sizeof(*hdr);
520         u32 tsf32;
521
522         if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) {
523                 if (priv->filter_flags & FIF_FCSFAIL)
524                         rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
525                 else
526                         return 0;
527         }
528
529         rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
530         rx_status.noise = priv->noise;
531         /* XX correct? */
532         rx_status.qual = (100 * hdr->rssi) / 127;
533         if (hdr->rate & 0x10)
534                 rx_status.flag |= RX_FLAG_SHORTPRE;
535         rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
536                         hdr->rate : (hdr->rate - 4)) & 0xf;
537         rx_status.freq = freq;
538         rx_status.band =  dev->conf.channel->band;
539         rx_status.antenna = hdr->antenna;
540
541         tsf32 = le32_to_cpu(hdr->tsf32);
542         if (tsf32 < priv->tsf_low32)
543                 priv->tsf_high32++;
544         rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
545         priv->tsf_low32 = tsf32;
546
547         rx_status.flag |= RX_FLAG_TSFT;
548
549         if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
550                 header_len += hdr->align[0];
551
552         skb_pull(skb, header_len);
553         skb_trim(skb, le16_to_cpu(hdr->len));
554
555         ieee80211_rx_irqsafe(dev, skb, &rx_status);
556
557         return -1;
558 }
559
560 static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
561 {
562         struct p54_common *priv = dev->priv;
563         int i;
564
565         if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
566                 return ;
567
568         for (i = 0; i < dev->queues; i++)
569                 if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
570                         ieee80211_wake_queue(dev, i);
571 }
572
573 void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
574 {
575         struct p54_common *priv = dev->priv;
576         struct ieee80211_tx_info *info;
577         struct memrecord *range;
578         unsigned long flags;
579         u32 freed = 0, last_addr = priv->rx_start;
580
581         if (unlikely(!skb || !dev || !skb_queue_len(&priv->tx_queue)))
582                 return;
583
584         spin_lock_irqsave(&priv->tx_queue.lock, flags);
585         info = IEEE80211_SKB_CB(skb);
586         range = (void *)info->rate_driver_data;
587         if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
588                 struct ieee80211_tx_info *ni;
589                 struct memrecord *mr;
590
591                 ni = IEEE80211_SKB_CB(skb->prev);
592                 mr = (struct memrecord *)ni->rate_driver_data;
593                 last_addr = mr->end_addr;
594         }
595         if (skb->next != (struct sk_buff *)&priv->tx_queue) {
596                 struct ieee80211_tx_info *ni;
597                 struct memrecord *mr;
598
599                 ni = IEEE80211_SKB_CB(skb->next);
600                 mr = (struct memrecord *)ni->rate_driver_data;
601                 freed = mr->start_addr - last_addr;
602         } else
603                 freed = priv->rx_end - last_addr;
604         __skb_unlink(skb, &priv->tx_queue);
605         spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
606         kfree_skb(skb);
607
608         if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
609                      IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
610                 p54_wake_free_queues(dev);
611 }
612 EXPORT_SYMBOL_GPL(p54_free_skb);
613
614 static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
615 {
616         struct p54_common *priv = dev->priv;
617         struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
618         struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
619         struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
620         u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
621         struct memrecord *range = NULL;
622         u32 freed = 0;
623         u32 last_addr = priv->rx_start;
624         unsigned long flags;
625         int count, idx;
626
627         spin_lock_irqsave(&priv->tx_queue.lock, flags);
628         while (entry != (struct sk_buff *)&priv->tx_queue) {
629                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
630                 struct p54_hdr *entry_hdr;
631                 struct p54_tx_data *entry_data;
632                 int pad = 0;
633
634                 range = (void *)info->rate_driver_data;
635                 if (range->start_addr != addr) {
636                         last_addr = range->end_addr;
637                         entry = entry->next;
638                         continue;
639                 }
640
641                 if (entry->next != (struct sk_buff *)&priv->tx_queue) {
642                         struct ieee80211_tx_info *ni;
643                         struct memrecord *mr;
644
645                         ni = IEEE80211_SKB_CB(entry->next);
646                         mr = (struct memrecord *)ni->rate_driver_data;
647                         freed = mr->start_addr - last_addr;
648                 } else
649                         freed = priv->rx_end - last_addr;
650
651                 last_addr = range->end_addr;
652                 __skb_unlink(entry, &priv->tx_queue);
653                 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
654
655                 if (unlikely(entry == priv->cached_beacon)) {
656                         kfree_skb(entry);
657                         priv->cached_beacon = NULL;
658                         goto out;
659                 }
660
661                 /*
662                  * Clear manually, ieee80211_tx_info_clear_status would
663                  * clear the counts too and we need them.
664                  */
665                 memset(&info->status.ampdu_ack_len, 0,
666                        sizeof(struct ieee80211_tx_info) -
667                        offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
668                 BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
669                                       status.ampdu_ack_len) != 23);
670
671                 entry_hdr = (struct p54_hdr *) entry->data;
672                 entry_data = (struct p54_tx_data *) entry_hdr->data;
673                 if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
674                         pad = entry_data->align[0];
675
676                 /* walk through the rates array and adjust the counts */
677                 count = payload->tries;
678                 for (idx = 0; idx < 4; idx++) {
679                         if (count >= info->status.rates[idx].count) {
680                                 count -= info->status.rates[idx].count;
681                         } else if (count > 0) {
682                                 info->status.rates[idx].count = count;
683                                 count = 0;
684                         } else {
685                                 info->status.rates[idx].idx = -1;
686                                 info->status.rates[idx].count = 0;
687                         }
688                 }
689
690                 priv->tx_stats[entry_data->hw_queue].len--;
691                 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
692                      (!payload->status))
693                         info->flags |= IEEE80211_TX_STAT_ACK;
694                 if (payload->status & P54_TX_PSM_CANCELLED)
695                         info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
696                 info->status.ack_signal = p54_rssi_to_dbm(dev,
697                                 (int)payload->ack_rssi);
698                 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
699                 ieee80211_tx_status_irqsafe(dev, entry);
700                 goto out;
701         }
702         spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
703
704 out:
705         if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
706                      IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
707                 p54_wake_free_queues(dev);
708 }
709
710 static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
711                                    struct sk_buff *skb)
712 {
713         struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
714         struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
715         struct p54_common *priv = dev->priv;
716
717         if (!priv->eeprom)
718                 return ;
719
720         memcpy(priv->eeprom, eeprom->data, le16_to_cpu(eeprom->len));
721
722         complete(&priv->eeprom_comp);
723 }
724
725 static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
726 {
727         struct p54_common *priv = dev->priv;
728         struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
729         struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
730         u32 tsf32 = le32_to_cpu(stats->tsf32);
731
732         if (tsf32 < priv->tsf_low32)
733                 priv->tsf_high32++;
734         priv->tsf_low32 = tsf32;
735
736         priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
737         priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
738         priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
739
740         priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
741         complete(&priv->stats_comp);
742
743         mod_timer(&priv->stats_timer, jiffies + 5 * HZ);
744 }
745
746 static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
747 {
748         struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
749         struct p54_trap *trap = (struct p54_trap *) hdr->data;
750         u16 event = le16_to_cpu(trap->event);
751         u16 freq = le16_to_cpu(trap->frequency);
752
753         switch (event) {
754         case P54_TRAP_BEACON_TX:
755                 break;
756         case P54_TRAP_RADAR:
757                 printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
758                         wiphy_name(dev->wiphy), freq);
759                 break;
760         case P54_TRAP_NO_BEACON:
761                 break;
762         case P54_TRAP_SCAN:
763                 break;
764         case P54_TRAP_TBTT:
765                 break;
766         case P54_TRAP_TIMER:
767                 break;
768         default:
769                 printk(KERN_INFO "%s: received event:%x freq:%d\n",
770                        wiphy_name(dev->wiphy), event, freq);
771                 break;
772         }
773 }
774
775 static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
776 {
777         struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
778
779         switch (le16_to_cpu(hdr->type)) {
780         case P54_CONTROL_TYPE_TXDONE:
781                 p54_rx_frame_sent(dev, skb);
782                 break;
783         case P54_CONTROL_TYPE_TRAP:
784                 p54_rx_trap(dev, skb);
785                 break;
786         case P54_CONTROL_TYPE_BBP:
787                 break;
788         case P54_CONTROL_TYPE_STAT_READBACK:
789                 p54_rx_stats(dev, skb);
790                 break;
791         case P54_CONTROL_TYPE_EEPROM_READBACK:
792                 p54_rx_eeprom_readback(dev, skb);
793                 break;
794         default:
795                 printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
796                        wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
797                 break;
798         }
799
800         return 0;
801 }
802
803 /* returns zero if skb can be reused */
804 int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
805 {
806         u16 type = le16_to_cpu(*((__le16 *)skb->data));
807
808         if (type & P54_HDR_FLAG_CONTROL)
809                 return p54_rx_control(dev, skb);
810         else
811                 return p54_rx_data(dev, skb);
812 }
813 EXPORT_SYMBOL_GPL(p54_rx);
814
815 /*
816  * So, the firmware is somewhat stupid and doesn't know what places in its
817  * memory incoming data should go to. By poking around in the firmware, we
818  * can find some unused memory to upload our packets to. However, data that we
819  * want the card to TX needs to stay intact until the card has told us that
820  * it is done with it. This function finds empty places we can upload to and
821  * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
822  * allocated areas.
823  */
824 static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
825                                struct p54_hdr *data, u32 len)
826 {
827         struct p54_common *priv = dev->priv;
828         struct sk_buff *entry = priv->tx_queue.next;
829         struct sk_buff *target_skb = NULL;
830         struct ieee80211_tx_info *info;
831         struct memrecord *range;
832         u32 last_addr = priv->rx_start;
833         u32 largest_hole = 0;
834         u32 target_addr = priv->rx_start;
835         unsigned long flags;
836         unsigned int left;
837         len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
838
839         if (!skb)
840                 return -EINVAL;
841
842         spin_lock_irqsave(&priv->tx_queue.lock, flags);
843         left = skb_queue_len(&priv->tx_queue);
844         while (left--) {
845                 u32 hole_size;
846                 info = IEEE80211_SKB_CB(entry);
847                 range = (void *)info->rate_driver_data;
848                 hole_size = range->start_addr - last_addr;
849                 if (!target_skb && hole_size >= len) {
850                         target_skb = entry->prev;
851                         hole_size -= len;
852                         target_addr = last_addr;
853                 }
854                 largest_hole = max(largest_hole, hole_size);
855                 last_addr = range->end_addr;
856                 entry = entry->next;
857         }
858         if (!target_skb && priv->rx_end - last_addr >= len) {
859                 target_skb = priv->tx_queue.prev;
860                 largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
861                 if (!skb_queue_empty(&priv->tx_queue)) {
862                         info = IEEE80211_SKB_CB(target_skb);
863                         range = (void *)info->rate_driver_data;
864                         target_addr = range->end_addr;
865                 }
866         } else
867                 largest_hole = max(largest_hole, priv->rx_end - last_addr);
868
869         if (!target_skb) {
870                 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
871                 ieee80211_stop_queues(dev);
872                 return -ENOMEM;
873         }
874
875         info = IEEE80211_SKB_CB(skb);
876         range = (void *)info->rate_driver_data;
877         range->start_addr = target_addr;
878         range->end_addr = target_addr + len;
879         __skb_queue_after(&priv->tx_queue, target_skb, skb);
880         spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
881
882         if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
883                            48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
884                 ieee80211_stop_queues(dev);
885
886         data->req_id = cpu_to_le32(target_addr + priv->headroom);
887         return 0;
888 }
889
890 static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev,
891                 u16 hdr_flags, u16 len, u16 type, gfp_t memflags)
892 {
893         struct p54_common *priv = dev->priv;
894         struct p54_hdr *hdr;
895         struct sk_buff *skb;
896
897         skb = __dev_alloc_skb(len + priv->tx_hdr_len, memflags);
898         if (!skb)
899                 return NULL;
900         skb_reserve(skb, priv->tx_hdr_len);
901
902         hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
903         hdr->flags = cpu_to_le16(hdr_flags);
904         hdr->len = cpu_to_le16(len - sizeof(*hdr));
905         hdr->type = cpu_to_le16(type);
906         hdr->tries = hdr->rts_tries = 0;
907
908         if (unlikely(p54_assign_address(dev, skb, hdr, len))) {
909                 kfree_skb(skb);
910                 return NULL;
911         }
912         return skb;
913 }
914
915 int p54_read_eeprom(struct ieee80211_hw *dev)
916 {
917         struct p54_common *priv = dev->priv;
918         struct p54_hdr *hdr = NULL;
919         struct p54_eeprom_lm86 *eeprom_hdr;
920         struct sk_buff *skb;
921         size_t eeprom_size = 0x2020, offset = 0, blocksize;
922         int ret = -ENOMEM;
923         void *eeprom = NULL;
924
925         skb = p54_alloc_skb(dev, 0x8000, sizeof(*hdr) + sizeof(*eeprom_hdr) +
926                             EEPROM_READBACK_LEN,
927                             P54_CONTROL_TYPE_EEPROM_READBACK, GFP_KERNEL);
928         if (!skb)
929                 goto free;
930         priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
931         if (!priv->eeprom)
932                 goto free;
933         eeprom = kzalloc(eeprom_size, GFP_KERNEL);
934         if (!eeprom)
935                 goto free;
936
937         eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
938                      sizeof(*eeprom_hdr) + EEPROM_READBACK_LEN);
939
940         while (eeprom_size) {
941                 blocksize = min(eeprom_size, (size_t)EEPROM_READBACK_LEN);
942                 eeprom_hdr->offset = cpu_to_le16(offset);
943                 eeprom_hdr->len = cpu_to_le16(blocksize);
944                 priv->tx(dev, skb, 0);
945
946                 if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
947                         printk(KERN_ERR "%s: device does not respond!\n",
948                                 wiphy_name(dev->wiphy));
949                         ret = -EBUSY;
950                         goto free;
951                 }
952
953                 memcpy(eeprom + offset, priv->eeprom, blocksize);
954                 offset += blocksize;
955                 eeprom_size -= blocksize;
956         }
957
958         ret = p54_parse_eeprom(dev, eeprom, offset);
959 free:
960         kfree(priv->eeprom);
961         priv->eeprom = NULL;
962         p54_free_skb(dev, skb);
963         kfree(eeprom);
964
965         return ret;
966 }
967 EXPORT_SYMBOL_GPL(p54_read_eeprom);
968
969 static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
970                 bool set)
971 {
972         struct p54_common *priv = dev->priv;
973         struct sk_buff *skb;
974         struct p54_tim *tim;
975
976         skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
977                       sizeof(struct p54_hdr) + sizeof(*tim),
978                       P54_CONTROL_TYPE_TIM, GFP_KERNEL);
979         if (!skb)
980                 return -ENOMEM;
981
982         tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
983         tim->count = 1;
984         tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
985         priv->tx(dev, skb, 1);
986         return 0;
987 }
988
989 static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
990 {
991         struct p54_common *priv = dev->priv;
992         struct sk_buff *skb;
993         struct p54_sta_unlock *sta;
994
995         skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
996                 sizeof(struct p54_hdr) + sizeof(*sta),
997                 P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
998         if (!skb)
999                 return -ENOMEM;
1000
1001         sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
1002         memcpy(sta->addr, addr, ETH_ALEN);
1003         priv->tx(dev, skb, 1);
1004         return 0;
1005 }
1006
1007 static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
1008 {
1009         struct p54_common *priv = dev->priv;
1010         struct sk_buff *skb;
1011         struct p54_hdr *hdr;
1012         struct p54_txcancel *cancel;
1013
1014         skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
1015                 sizeof(struct p54_hdr) + sizeof(*cancel),
1016                 P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
1017         if (!skb)
1018                 return -ENOMEM;
1019
1020         hdr = (void *)entry->data;
1021         cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
1022         cancel->req_id = hdr->req_id;
1023         priv->tx(dev, skb, 1);
1024         return 0;
1025 }
1026
1027 static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
1028                 struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
1029                 u16 *flags, u16 *aid)
1030 {
1031         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1032         struct p54_common *priv = dev->priv;
1033         int ret = 0;
1034
1035         if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
1036                 if (ieee80211_is_beacon(hdr->frame_control)) {
1037                         *aid = 0;
1038                         *queue = 0;
1039                         *extra_len = IEEE80211_MAX_TIM_LEN;
1040                         *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
1041                         return 0;
1042                 } else if (ieee80211_is_probe_resp(hdr->frame_control)) {
1043                         *aid = 0;
1044                         *queue = 2;
1045                         *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
1046                                  P54_HDR_FLAG_DATA_OUT_NOCANCEL;
1047                         return 0;
1048                 } else {
1049                         *queue = 2;
1050                         ret = 0;
1051                 }
1052         } else {
1053                 *queue += 4;
1054                 ret = 1;
1055         }
1056
1057         switch (priv->mode) {
1058         case NL80211_IFTYPE_STATION:
1059                 *aid = 1;
1060                 break;
1061         case NL80211_IFTYPE_AP:
1062         case NL80211_IFTYPE_ADHOC:
1063         case NL80211_IFTYPE_MESH_POINT:
1064                 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
1065                         *aid = 0;
1066                         *queue = 3;
1067                         return 0;
1068                 }
1069                 if (info->control.sta)
1070                         *aid = info->control.sta->aid;
1071                 else
1072                         *flags = P54_HDR_FLAG_DATA_OUT_NOCANCEL;
1073         }
1074         return ret;
1075 }
1076
1077 static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
1078 {
1079         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1080         struct ieee80211_tx_queue_stats *current_queue = NULL;
1081         struct p54_common *priv = dev->priv;
1082         struct p54_hdr *hdr;
1083         struct p54_tx_data *txhdr;
1084         size_t padding, len, tim_len = 0;
1085         int i, j, ridx;
1086         u16 hdr_flags = 0, aid = 0;
1087         u8 rate, queue;
1088         u8 cts_rate = 0x20;
1089         u8 rc_flags;
1090         u8 calculated_tries[4];
1091         u8 nrates = 0, nremaining = 8;
1092
1093         queue = skb_get_queue_mapping(skb);
1094
1095         if (p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid)) {
1096                 current_queue = &priv->tx_stats[queue];
1097                 if (unlikely(current_queue->len > current_queue->limit))
1098                         return NETDEV_TX_BUSY;
1099                 current_queue->len++;
1100                 current_queue->count++;
1101                 if (current_queue->len == current_queue->limit)
1102                         ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
1103         }
1104
1105         padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
1106         len = skb->len;
1107
1108         if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT) {
1109                 if (info->control.sta)
1110                         if (p54_sta_unlock(dev, info->control.sta->addr)) {
1111                                 if (current_queue) {
1112                                         current_queue->len--;
1113                                         current_queue->count--;
1114                                 }
1115                                 return NETDEV_TX_BUSY;
1116                         }
1117         }
1118
1119         txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
1120         hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
1121
1122         if (padding)
1123                 hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
1124         hdr->len = cpu_to_le16(len);
1125         hdr->type = cpu_to_le16(aid);
1126         hdr->rts_tries = info->control.rates[0].count;
1127
1128         /*
1129          * we register the rates in perfect order, and
1130          * RTS/CTS won't happen on 5 GHz
1131          */
1132         cts_rate = info->control.rts_cts_rate_idx;
1133
1134         memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
1135
1136         /* see how many rates got used */
1137         for (i = 0; i < 4; i++) {
1138                 if (info->control.rates[i].idx < 0)
1139                         break;
1140                 nrates++;
1141         }
1142
1143         /* limit tries to 8/nrates per rate */
1144         for (i = 0; i < nrates; i++) {
1145                 /*
1146                  * The magic expression here is equivalent to 8/nrates for
1147                  * all values that matter, but avoids division and jumps.
1148                  * Note that nrates can only take the values 1 through 4.
1149                  */
1150                 calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
1151                                                  info->control.rates[i].count);
1152                 nremaining -= calculated_tries[i];
1153         }
1154
1155         /* if there are tries left, distribute from back to front */
1156         for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
1157                 int tmp = info->control.rates[i].count - calculated_tries[i];
1158
1159                 if (tmp <= 0)
1160                         continue;
1161                 /* RC requested more tries at this rate */
1162
1163                 tmp = min_t(int, tmp, nremaining);
1164                 calculated_tries[i] += tmp;
1165                 nremaining -= tmp;
1166         }
1167
1168         ridx = 0;
1169         for (i = 0; i < nrates && ridx < 8; i++) {
1170                 /* we register the rates in perfect order */
1171                 rate = info->control.rates[i].idx;
1172                 if (info->band == IEEE80211_BAND_5GHZ)
1173                         rate += 4;
1174
1175                 /* store the count we actually calculated for TX status */
1176                 info->control.rates[i].count = calculated_tries[i];
1177
1178                 rc_flags = info->control.rates[i].flags;
1179                 if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
1180                         rate |= 0x10;
1181                         cts_rate |= 0x10;
1182                 }
1183                 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
1184                         rate |= 0x40;
1185                 else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1186                         rate |= 0x20;
1187                 for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
1188                         txhdr->rateset[ridx] = rate;
1189                         ridx++;
1190                 }
1191         }
1192
1193         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
1194                 hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
1195
1196         /* TODO: enable bursting */
1197         hdr->flags = cpu_to_le16(hdr_flags);
1198         hdr->tries = ridx;
1199         txhdr->crypt_offset = 0;
1200         txhdr->rts_rate_idx = 0;
1201         txhdr->key_type = 0;
1202         txhdr->key_len = 0;
1203         txhdr->hw_queue = queue;
1204         if (current_queue)
1205                 txhdr->backlog = current_queue->len;
1206         else
1207                 txhdr->backlog = 0;
1208         memset(txhdr->durations, 0, sizeof(txhdr->durations));
1209         txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
1210                 2 : info->antenna_sel_tx - 1;
1211         txhdr->output_power = priv->output_power;
1212         txhdr->cts_rate = cts_rate;
1213         if (padding)
1214                 txhdr->align[0] = padding;
1215
1216         /* modifies skb->cb and with it info, so must be last! */
1217         if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len))) {
1218                 skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
1219                 if (current_queue) {
1220                         current_queue->len--;
1221                         current_queue->count--;
1222                 }
1223                 return NETDEV_TX_BUSY;
1224         }
1225         priv->tx(dev, skb, 0);
1226         return 0;
1227 }
1228
1229 static int p54_setup_mac(struct ieee80211_hw *dev, u16 mode, const u8 *bssid)
1230 {
1231         struct p54_common *priv = dev->priv;
1232         struct sk_buff *skb;
1233         struct p54_setup_mac *setup;
1234
1235         skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup) +
1236                             sizeof(struct p54_hdr), P54_CONTROL_TYPE_SETUP,
1237                             GFP_ATOMIC);
1238         if (!skb)
1239                 return -ENOMEM;
1240
1241         setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
1242         priv->mac_mode = mode;
1243         setup->mac_mode = cpu_to_le16(mode);
1244         memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
1245         if (!bssid)
1246                 memset(setup->bssid, ~0, ETH_ALEN);
1247         else
1248                 memcpy(setup->bssid, bssid, ETH_ALEN);
1249         setup->rx_antenna = priv->rx_antenna;
1250         setup->rx_align = 0;
1251         if (priv->fw_var < 0x500) {
1252                 setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
1253                 memset(setup->v1.rts_rates, 0, 8);
1254                 setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
1255                 setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
1256                 setup->v1.rxhw = cpu_to_le16(priv->rxhw);
1257                 setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
1258                 setup->v1.unalloc0 = cpu_to_le16(0);
1259         } else {
1260                 setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
1261                 setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
1262                 setup->v2.rxhw = cpu_to_le16(priv->rxhw);
1263                 setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
1264                 setup->v2.truncate = cpu_to_le16(48896);
1265                 setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
1266                 setup->v2.sbss_offset = 0;
1267                 setup->v2.mcast_window = 0;
1268                 setup->v2.rx_rssi_threshold = 0;
1269                 setup->v2.rx_ed_threshold = 0;
1270                 setup->v2.ref_clock = cpu_to_le32(644245094);
1271                 setup->v2.lpf_bandwidth = cpu_to_le16(65535);
1272                 setup->v2.osc_start_delay = cpu_to_le16(65535);
1273         }
1274         priv->tx(dev, skb, 1);
1275         return 0;
1276 }
1277
1278 static int p54_set_freq(struct ieee80211_hw *dev, u16 frequency)
1279 {
1280         struct p54_common *priv = dev->priv;
1281         struct sk_buff *skb;
1282         struct p54_scan *chan;
1283         unsigned int i;
1284         void *entry;
1285         __le16 freq = cpu_to_le16(frequency);
1286
1287         skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*chan) +
1288                             sizeof(struct p54_hdr), P54_CONTROL_TYPE_SCAN,
1289                             GFP_ATOMIC);
1290         if (!skb)
1291                 return -ENOMEM;
1292
1293         chan = (struct p54_scan *) skb_put(skb, sizeof(*chan));
1294         memset(chan->padding1, 0, sizeof(chan->padding1));
1295         chan->mode = cpu_to_le16(P54_SCAN_EXIT);
1296         chan->dwell = cpu_to_le16(0x0);
1297
1298         for (i = 0; i < priv->iq_autocal_len; i++) {
1299                 if (priv->iq_autocal[i].freq != freq)
1300                         continue;
1301
1302                 memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
1303                        sizeof(*priv->iq_autocal));
1304                 break;
1305         }
1306         if (i == priv->iq_autocal_len)
1307                 goto err;
1308
1309         for (i = 0; i < priv->output_limit_len; i++) {
1310                 if (priv->output_limit[i].freq != freq)
1311                         continue;
1312
1313                 chan->val_barker = 0x38;
1314                 chan->val_bpsk = chan->dup_bpsk =
1315                         priv->output_limit[i].val_bpsk;
1316                 chan->val_qpsk = chan->dup_qpsk =
1317                         priv->output_limit[i].val_qpsk;
1318                 chan->val_16qam = chan->dup_16qam =
1319                         priv->output_limit[i].val_16qam;
1320                 chan->val_64qam = chan->dup_64qam =
1321                         priv->output_limit[i].val_64qam;
1322                 break;
1323         }
1324         if (i == priv->output_limit_len)
1325                 goto err;
1326
1327         entry = priv->curve_data->data;
1328         for (i = 0; i < priv->curve_data->channels; i++) {
1329                 if (*((__le16 *)entry) != freq) {
1330                         entry += sizeof(__le16);
1331                         entry += sizeof(struct p54_pa_curve_data_sample) *
1332                                  priv->curve_data->points_per_channel;
1333                         continue;
1334                 }
1335
1336                 entry += sizeof(__le16);
1337                 chan->pa_points_per_curve = 8;
1338                 memset(chan->curve_data, 0, sizeof(*chan->curve_data));
1339                 memcpy(chan->curve_data, entry,
1340                        sizeof(struct p54_pa_curve_data_sample) *
1341                        min((u8)8, priv->curve_data->points_per_channel));
1342                 break;
1343         }
1344
1345         if (priv->fw_var < 0x500) {
1346                 chan->v1.rssical_mul = cpu_to_le16(130);
1347                 chan->v1.rssical_add = cpu_to_le16(0xfe70);
1348         } else {
1349                 chan->v2.rssical_mul = cpu_to_le16(130);
1350                 chan->v2.rssical_add = cpu_to_le16(0xfe70);
1351                 chan->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
1352                 memset(chan->v2.rts_rates, 0, 8);
1353         }
1354         priv->tx(dev, skb, 1);
1355         return 0;
1356
1357  err:
1358         printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
1359         kfree_skb(skb);
1360         return -EINVAL;
1361 }
1362
1363 static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
1364 {
1365         struct p54_common *priv = dev->priv;
1366         struct sk_buff *skb;
1367         struct p54_led *led;
1368
1369         skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led) +
1370                         sizeof(struct p54_hdr), P54_CONTROL_TYPE_LED,
1371                         GFP_ATOMIC);
1372         if (!skb)
1373                 return -ENOMEM;
1374
1375         led = (struct p54_led *)skb_put(skb, sizeof(*led));
1376         led->mode = cpu_to_le16(mode);
1377         led->led_permanent = cpu_to_le16(link);
1378         led->led_temporary = cpu_to_le16(act);
1379         led->duration = cpu_to_le16(1000);
1380         priv->tx(dev, skb, 1);
1381         return 0;
1382 }
1383
1384 #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop)      \
1385 do {                                                            \
1386         queue.aifs = cpu_to_le16(ai_fs);                        \
1387         queue.cwmin = cpu_to_le16(cw_min);                      \
1388         queue.cwmax = cpu_to_le16(cw_max);                      \
1389         queue.txop = cpu_to_le16(_txop);                        \
1390 } while(0)
1391
1392 static int p54_set_edcf(struct ieee80211_hw *dev)
1393 {
1394         struct p54_common *priv = dev->priv;
1395         struct sk_buff *skb;
1396         struct p54_edcf *edcf;
1397
1398         skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf) +
1399                         sizeof(struct p54_hdr), P54_CONTROL_TYPE_DCFINIT,
1400                         GFP_ATOMIC);
1401         if (!skb)
1402                 return -ENOMEM;
1403
1404         edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
1405         if (priv->use_short_slot) {
1406                 edcf->slottime = 9;
1407                 edcf->sifs = 0x10;
1408                 edcf->eofpad = 0x00;
1409         } else {
1410                 edcf->slottime = 20;
1411                 edcf->sifs = 0x0a;
1412                 edcf->eofpad = 0x06;
1413         }
1414         /* (see prism54/isl_oid.h for further details) */
1415         edcf->frameburst = cpu_to_le16(0);
1416         edcf->round_trip_delay = cpu_to_le16(0);
1417         edcf->flags = 0;
1418         memset(edcf->mapping, 0, sizeof(edcf->mapping));
1419         memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
1420         priv->tx(dev, skb, 1);
1421         return 0;
1422 }
1423
1424 static int p54_init_stats(struct ieee80211_hw *dev)
1425 {
1426         struct p54_common *priv = dev->priv;
1427
1428         priv->cached_stats = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
1429                         sizeof(struct p54_hdr) + sizeof(struct p54_statistics),
1430                         P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
1431         if (!priv->cached_stats)
1432                         return -ENOMEM;
1433
1434         mod_timer(&priv->stats_timer, jiffies + HZ);
1435         return 0;
1436 }
1437
1438 static int p54_beacon_tim(struct sk_buff *skb)
1439 {
1440         /*
1441          * the good excuse for this mess is ... the firmware.
1442          * The dummy TIM MUST be at the end of the beacon frame,
1443          * because it'll be overwritten!
1444          */
1445
1446         struct ieee80211_mgmt *mgmt = (void *)skb->data;
1447         u8 *pos, *end;
1448
1449         if (skb->len <= sizeof(mgmt)) {
1450                 printk(KERN_ERR "p54: beacon is too short!\n");
1451                 return -EINVAL;
1452         }
1453
1454         pos = (u8 *)mgmt->u.beacon.variable;
1455         end = skb->data + skb->len;
1456         while (pos < end) {
1457                 if (pos + 2 + pos[1] > end) {
1458                         printk(KERN_ERR "p54: parsing beacon failed\n");
1459                         return -EINVAL;
1460                 }
1461
1462                 if (pos[0] == WLAN_EID_TIM) {
1463                         u8 dtim_len = pos[1];
1464                         u8 dtim_period = pos[3];
1465                         u8 *next = pos + 2 + dtim_len;
1466
1467                         if (dtim_len < 3) {
1468                                 printk(KERN_ERR "p54: invalid dtim len!\n");
1469                                 return -EINVAL;
1470                         }
1471                         memmove(pos, next, end - next);
1472
1473                         if (dtim_len > 3)
1474                                 skb_trim(skb, skb->len - (dtim_len - 3));
1475
1476                         pos = end - (dtim_len + 2);
1477
1478                         /* add the dummy at the end */
1479                         pos[0] = WLAN_EID_TIM;
1480                         pos[1] = 3;
1481                         pos[2] = 0;
1482                         pos[3] = dtim_period;
1483                         pos[4] = 0;
1484                         return 0;
1485                 }
1486                 pos += 2 + pos[1];
1487         }
1488         return 0;
1489 }
1490
1491 static int p54_beacon_update(struct ieee80211_hw *dev,
1492                         struct ieee80211_vif *vif)
1493 {
1494         struct p54_common *priv = dev->priv;
1495         struct sk_buff *beacon;
1496         int ret;
1497
1498         if (priv->cached_beacon) {
1499                 p54_tx_cancel(dev, priv->cached_beacon);
1500                 /* wait for the last beacon the be freed */
1501                 msleep(10);
1502         }
1503
1504         beacon = ieee80211_beacon_get(dev, vif);
1505         if (!beacon)
1506                 return -ENOMEM;
1507         ret = p54_beacon_tim(beacon);
1508         if (ret)
1509                 return ret;
1510         ret = p54_tx(dev, beacon);
1511         if (ret)
1512                 return ret;
1513         priv->cached_beacon = beacon;
1514         priv->tsf_high32 = 0;
1515         priv->tsf_low32 = 0;
1516
1517         return 0;
1518 }
1519
1520 static int p54_start(struct ieee80211_hw *dev)
1521 {
1522         struct p54_common *priv = dev->priv;
1523         int err;
1524
1525         mutex_lock(&priv->conf_mutex);
1526         err = priv->open(dev);
1527         if (err)
1528                 goto out;
1529         P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
1530         P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
1531         P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
1532         P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
1533         err = p54_set_edcf(dev);
1534         if (err)
1535                 goto out;
1536         err = p54_init_stats(dev);
1537         if (err)
1538                 goto out;
1539         err = p54_setup_mac(dev, P54_FILTER_TYPE_NONE, NULL);
1540         if (err)
1541                 goto out;
1542         priv->mode = NL80211_IFTYPE_MONITOR;
1543
1544 out:
1545         mutex_unlock(&priv->conf_mutex);
1546         return err;
1547 }
1548
1549 static void p54_stop(struct ieee80211_hw *dev)
1550 {
1551         struct p54_common *priv = dev->priv;
1552         struct sk_buff *skb;
1553
1554         mutex_lock(&priv->conf_mutex);
1555         del_timer(&priv->stats_timer);
1556         p54_free_skb(dev, priv->cached_stats);
1557         priv->cached_stats = NULL;
1558         if (priv->cached_beacon)
1559                 p54_tx_cancel(dev, priv->cached_beacon);
1560
1561         while ((skb = skb_dequeue(&priv->tx_queue)))
1562                 kfree_skb(skb);
1563
1564         priv->cached_beacon = NULL;
1565         priv->stop(dev);
1566         priv->tsf_high32 = priv->tsf_low32 = 0;
1567         priv->mode = NL80211_IFTYPE_UNSPECIFIED;
1568         mutex_unlock(&priv->conf_mutex);
1569 }
1570
1571 static int p54_add_interface(struct ieee80211_hw *dev,
1572                              struct ieee80211_if_init_conf *conf)
1573 {
1574         struct p54_common *priv = dev->priv;
1575
1576         mutex_lock(&priv->conf_mutex);
1577         if (priv->mode != NL80211_IFTYPE_MONITOR) {
1578                 mutex_unlock(&priv->conf_mutex);
1579                 return -EOPNOTSUPP;
1580         }
1581
1582         switch (conf->type) {
1583         case NL80211_IFTYPE_STATION:
1584         case NL80211_IFTYPE_ADHOC:
1585         case NL80211_IFTYPE_AP:
1586         case NL80211_IFTYPE_MESH_POINT:
1587                 priv->mode = conf->type;
1588                 break;
1589         default:
1590                 mutex_unlock(&priv->conf_mutex);
1591                 return -EOPNOTSUPP;
1592         }
1593
1594         memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
1595
1596         p54_setup_mac(dev, P54_FILTER_TYPE_NONE, NULL);
1597
1598         switch (conf->type) {
1599         case NL80211_IFTYPE_STATION:
1600                 p54_setup_mac(dev, P54_FILTER_TYPE_STATION, NULL);
1601                 break;
1602         case NL80211_IFTYPE_AP:
1603                 p54_setup_mac(dev, P54_FILTER_TYPE_AP, priv->mac_addr);
1604                 break;
1605         case NL80211_IFTYPE_ADHOC:
1606         case NL80211_IFTYPE_MESH_POINT:
1607                 p54_setup_mac(dev, P54_FILTER_TYPE_IBSS, NULL);
1608                 break;
1609         default:
1610                 BUG();  /* impossible */
1611                 break;
1612         }
1613
1614         p54_set_leds(dev, 1, 0, 0);
1615
1616         mutex_unlock(&priv->conf_mutex);
1617         return 0;
1618 }
1619
1620 static void p54_remove_interface(struct ieee80211_hw *dev,
1621                                  struct ieee80211_if_init_conf *conf)
1622 {
1623         struct p54_common *priv = dev->priv;
1624
1625         mutex_lock(&priv->conf_mutex);
1626         if (priv->cached_beacon)
1627                 p54_tx_cancel(dev, priv->cached_beacon);
1628         p54_setup_mac(dev, P54_FILTER_TYPE_NONE, NULL);
1629         priv->mode = NL80211_IFTYPE_MONITOR;
1630         memset(priv->mac_addr, 0, ETH_ALEN);
1631         mutex_unlock(&priv->conf_mutex);
1632 }
1633
1634 static int p54_config(struct ieee80211_hw *dev, u32 changed)
1635 {
1636         int ret;
1637         struct p54_common *priv = dev->priv;
1638         struct ieee80211_conf *conf = &dev->conf;
1639
1640         mutex_lock(&priv->conf_mutex);
1641         priv->rx_antenna = 2; /* automatic */
1642         priv->output_power = conf->power_level << 2;
1643         ret = p54_set_freq(dev, conf->channel->center_freq);
1644         if (!ret)
1645                 ret = p54_set_edcf(dev);
1646         mutex_unlock(&priv->conf_mutex);
1647         return ret;
1648 }
1649
1650 static int p54_config_interface(struct ieee80211_hw *dev,
1651                                 struct ieee80211_vif *vif,
1652                                 struct ieee80211_if_conf *conf)
1653 {
1654         struct p54_common *priv = dev->priv;
1655         int ret = 0;
1656
1657         mutex_lock(&priv->conf_mutex);
1658         switch (priv->mode) {
1659         case NL80211_IFTYPE_STATION:
1660                 ret = p54_setup_mac(dev, P54_FILTER_TYPE_STATION, conf->bssid);
1661                 if (ret)
1662                         goto out;
1663                 ret = p54_set_leds(dev, 1,
1664                                    !is_multicast_ether_addr(conf->bssid), 0);
1665                 if (ret)
1666                         goto out;
1667                 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
1668                 break;
1669         case NL80211_IFTYPE_AP:
1670         case NL80211_IFTYPE_ADHOC:
1671         case NL80211_IFTYPE_MESH_POINT:
1672                 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
1673                 ret = p54_set_freq(dev, dev->conf.channel->center_freq);
1674                 if (ret)
1675                         goto out;
1676                 ret = p54_setup_mac(dev, priv->mac_mode, priv->bssid);
1677                 if (ret)
1678                         goto out;
1679                 if (conf->changed & IEEE80211_IFCC_BEACON) {
1680                         ret = p54_beacon_update(dev, vif);
1681                         if (ret)
1682                                 goto out;
1683                         ret = p54_set_edcf(dev);
1684                         if (ret)
1685                                 goto out;
1686                 }
1687         }
1688 out:
1689         mutex_unlock(&priv->conf_mutex);
1690         return ret;
1691 }
1692
1693 static void p54_configure_filter(struct ieee80211_hw *dev,
1694                                  unsigned int changed_flags,
1695                                  unsigned int *total_flags,
1696                                  int mc_count, struct dev_mc_list *mclist)
1697 {
1698         struct p54_common *priv = dev->priv;
1699
1700         *total_flags &= FIF_BCN_PRBRESP_PROMISC |
1701                         FIF_PROMISC_IN_BSS |
1702                         FIF_FCSFAIL;
1703
1704         priv->filter_flags = *total_flags;
1705
1706         if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
1707                 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
1708                         p54_setup_mac(dev, priv->mac_mode, NULL);
1709                 else
1710                         p54_setup_mac(dev, priv->mac_mode, priv->bssid);
1711         }
1712
1713         if (changed_flags & FIF_PROMISC_IN_BSS) {
1714                 if (*total_flags & FIF_PROMISC_IN_BSS)
1715                         p54_setup_mac(dev, priv->mac_mode | 0x8, NULL);
1716                 else
1717                         p54_setup_mac(dev, priv->mac_mode & ~0x8, priv->bssid);
1718         }
1719 }
1720
1721 static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
1722                        const struct ieee80211_tx_queue_params *params)
1723 {
1724         struct p54_common *priv = dev->priv;
1725         int ret;
1726
1727         mutex_lock(&priv->conf_mutex);
1728         if ((params) && !(queue > 4)) {
1729                 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
1730                         params->cw_min, params->cw_max, params->txop);
1731                 ret = p54_set_edcf(dev);
1732         } else
1733                 ret = -EINVAL;
1734         mutex_unlock(&priv->conf_mutex);
1735         return ret;
1736 }
1737
1738 static int p54_init_xbow_synth(struct ieee80211_hw *dev)
1739 {
1740         struct p54_common *priv = dev->priv;
1741         struct sk_buff *skb;
1742         struct p54_xbow_synth *xbow;
1743
1744         skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow) +
1745                             sizeof(struct p54_hdr),
1746                             P54_CONTROL_TYPE_XBOW_SYNTH_CFG,
1747                             GFP_KERNEL);
1748         if (!skb)
1749                 return -ENOMEM;
1750
1751         xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
1752         xbow->magic1 = cpu_to_le16(0x1);
1753         xbow->magic2 = cpu_to_le16(0x2);
1754         xbow->freq = cpu_to_le16(5390);
1755         memset(xbow->padding, 0, sizeof(xbow->padding));
1756         priv->tx(dev, skb, 1);
1757         return 0;
1758 }
1759
1760 static void p54_statistics_timer(unsigned long data)
1761 {
1762         struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
1763         struct p54_common *priv = dev->priv;
1764
1765         BUG_ON(!priv->cached_stats);
1766
1767         priv->tx(dev, priv->cached_stats, 0);
1768 }
1769
1770 static int p54_get_stats(struct ieee80211_hw *dev,
1771                          struct ieee80211_low_level_stats *stats)
1772 {
1773         struct p54_common *priv = dev->priv;
1774
1775         del_timer(&priv->stats_timer);
1776         p54_statistics_timer((unsigned long)dev);
1777
1778         if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
1779                 printk(KERN_ERR "%s: device does not respond!\n",
1780                         wiphy_name(dev->wiphy));
1781                 return -EBUSY;
1782         }
1783
1784         memcpy(stats, &priv->stats, sizeof(*stats));
1785
1786         return 0;
1787 }
1788
1789 static int p54_get_tx_stats(struct ieee80211_hw *dev,
1790                             struct ieee80211_tx_queue_stats *stats)
1791 {
1792         struct p54_common *priv = dev->priv;
1793
1794         memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
1795
1796         return 0;
1797 }
1798
1799 static void p54_bss_info_changed(struct ieee80211_hw *dev,
1800                                  struct ieee80211_vif *vif,
1801                                  struct ieee80211_bss_conf *info,
1802                                  u32 changed)
1803 {
1804         struct p54_common *priv = dev->priv;
1805
1806         if (changed & BSS_CHANGED_ERP_SLOT) {
1807                 priv->use_short_slot = info->use_short_slot;
1808                 p54_set_edcf(dev);
1809         }
1810         if (changed & BSS_CHANGED_BASIC_RATES) {
1811                 if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
1812                         priv->basic_rate_mask = (info->basic_rates << 4);
1813                 else
1814                         priv->basic_rate_mask = info->basic_rates;
1815                 p54_setup_mac(dev, priv->mac_mode, priv->bssid);
1816                 if (priv->fw_var >= 0x500)
1817                         p54_set_freq(dev, dev->conf.channel->center_freq);
1818         }
1819         if (changed & BSS_CHANGED_ASSOC) {
1820                 if (info->assoc) {
1821                         priv->aid = info->aid;
1822                         priv->wakeup_timer = info->beacon_int *
1823                                              info->dtim_period * 5;
1824                         p54_setup_mac(dev, priv->mac_mode, priv->bssid);
1825                 }
1826         }
1827
1828 }
1829
1830 static const struct ieee80211_ops p54_ops = {
1831         .tx                     = p54_tx,
1832         .start                  = p54_start,
1833         .stop                   = p54_stop,
1834         .add_interface          = p54_add_interface,
1835         .remove_interface       = p54_remove_interface,
1836         .set_tim                = p54_set_tim,
1837         .config                 = p54_config,
1838         .config_interface       = p54_config_interface,
1839         .bss_info_changed       = p54_bss_info_changed,
1840         .configure_filter       = p54_configure_filter,
1841         .conf_tx                = p54_conf_tx,
1842         .get_stats              = p54_get_stats,
1843         .get_tx_stats           = p54_get_tx_stats
1844 };
1845
1846 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
1847 {
1848         struct ieee80211_hw *dev;
1849         struct p54_common *priv;
1850
1851         dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
1852         if (!dev)
1853                 return NULL;
1854
1855         priv = dev->priv;
1856         priv->mode = NL80211_IFTYPE_UNSPECIFIED;
1857         priv->basic_rate_mask = 0x15f;
1858         skb_queue_head_init(&priv->tx_queue);
1859         dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1860                      IEEE80211_HW_SIGNAL_DBM |
1861                      IEEE80211_HW_NOISE_DBM;
1862
1863         dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1864                                       BIT(NL80211_IFTYPE_ADHOC) |
1865                                       BIT(NL80211_IFTYPE_AP) |
1866                                       BIT(NL80211_IFTYPE_MESH_POINT);
1867
1868         dev->channel_change_time = 1000;        /* TODO: find actual value */
1869         priv->tx_stats[0].limit = 1;            /* Beacon queue */
1870         priv->tx_stats[1].limit = 1;            /* Probe queue for HW scan */
1871         priv->tx_stats[2].limit = 3;            /* queue for MLMEs */
1872         priv->tx_stats[3].limit = 3;            /* Broadcast / MC queue */
1873         priv->tx_stats[4].limit = 5;            /* Data */
1874         dev->queues = 1;
1875         priv->noise = -94;
1876         /*
1877          * We support at most 8 tries no matter which rate they're at,
1878          * we cannot support max_rates * max_rate_tries as we set it
1879          * here, but setting it correctly to 4/2 or so would limit us
1880          * artificially if the RC algorithm wants just two rates, so
1881          * let's say 4/7, we'll redistribute it at TX time, see the
1882          * comments there.
1883          */
1884         dev->max_rates = 4;
1885         dev->max_rate_tries = 7;
1886         dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
1887                                  sizeof(struct p54_tx_data);
1888
1889         mutex_init(&priv->conf_mutex);
1890         init_completion(&priv->eeprom_comp);
1891         init_completion(&priv->stats_comp);
1892         setup_timer(&priv->stats_timer, p54_statistics_timer,
1893                 (unsigned long)dev);
1894
1895         return dev;
1896 }
1897 EXPORT_SYMBOL_GPL(p54_init_common);
1898
1899 void p54_free_common(struct ieee80211_hw *dev)
1900 {
1901         struct p54_common *priv = dev->priv;
1902         del_timer(&priv->stats_timer);
1903         kfree_skb(priv->cached_stats);
1904         kfree(priv->iq_autocal);
1905         kfree(priv->output_limit);
1906         kfree(priv->curve_data);
1907 }
1908 EXPORT_SYMBOL_GPL(p54_free_common);
1909
1910 static int __init p54_init(void)
1911 {
1912         return 0;
1913 }
1914
1915 static void __exit p54_exit(void)
1916 {
1917 }
1918
1919 module_init(p54_init);
1920 module_exit(p54_exit);