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