Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / drivers / net / wireless / p54 / fwio.c
1 /*
2  * Firmware I/O code for mac80211 Prism54 drivers
3  *
4  * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
5  * Copyright (c) 2007-2009, 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/slab.h>
20 #include <linux/firmware.h>
21 #include <linux/etherdevice.h>
22 #include <linux/export.h>
23
24 #include <net/mac80211.h>
25
26 #include "p54.h"
27 #include "eeprom.h"
28 #include "lmac.h"
29
30 int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
31 {
32         struct p54_common *priv = dev->priv;
33         struct exp_if *exp_if;
34         struct bootrec *bootrec;
35         u32 *data = (u32 *)fw->data;
36         u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
37         u8 *fw_version = NULL;
38         size_t len;
39         int i;
40         int maxlen;
41
42         if (priv->rx_start)
43                 return 0;
44
45         while (data < end_data && *data)
46                 data++;
47
48         while (data < end_data && !*data)
49                 data++;
50
51         bootrec = (struct bootrec *) data;
52
53         while (bootrec->data <= end_data && (bootrec->data +
54                (len = le32_to_cpu(bootrec->len))) <= end_data) {
55                 u32 code = le32_to_cpu(bootrec->code);
56                 switch (code) {
57                 case BR_CODE_COMPONENT_ID:
58                         priv->fw_interface = be32_to_cpup((__be32 *)
59                                              bootrec->data);
60                         switch (priv->fw_interface) {
61                         case FW_LM86:
62                         case FW_LM20:
63                         case FW_LM87: {
64                                 char *iftype = (char *)bootrec->data;
65                                 wiphy_info(priv->hw->wiphy,
66                                            "p54 detected a LM%c%c firmware\n",
67                                            iftype[2], iftype[3]);
68                                 break;
69                                 }
70                         case FW_FMAC:
71                         default:
72                                 wiphy_err(priv->hw->wiphy,
73                                           "unsupported firmware\n");
74                                 return -ENODEV;
75                         }
76                         break;
77                 case BR_CODE_COMPONENT_VERSION:
78                         /* 24 bytes should be enough for all firmwares */
79                         if (strnlen((unsigned char *) bootrec->data, 24) < 24)
80                                 fw_version = (unsigned char *) bootrec->data;
81                         break;
82                 case BR_CODE_DESCR: {
83                         struct bootrec_desc *desc =
84                                 (struct bootrec_desc *)bootrec->data;
85                         priv->rx_start = le32_to_cpu(desc->rx_start);
86                         /* FIXME add sanity checking */
87                         priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
88                         priv->headroom = desc->headroom;
89                         priv->tailroom = desc->tailroom;
90                         priv->privacy_caps = desc->privacy_caps;
91                         priv->rx_keycache_size = desc->rx_keycache_size;
92                         if (le32_to_cpu(bootrec->len) == 11)
93                                 priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
94                         else
95                                 priv->rx_mtu = (size_t)
96                                         0x620 - priv->tx_hdr_len;
97                         maxlen = priv->tx_hdr_len + /* USB devices */
98                                  sizeof(struct p54_rx_data) +
99                                  4 + /* rx alignment */
100                                  IEEE80211_MAX_FRAG_THRESHOLD;
101                         if (priv->rx_mtu > maxlen && PAGE_SIZE == 4096) {
102                                 printk(KERN_INFO "p54: rx_mtu reduced from %d "
103                                        "to %d\n", priv->rx_mtu, maxlen);
104                                 priv->rx_mtu = maxlen;
105                         }
106                         break;
107                         }
108                 case BR_CODE_EXPOSED_IF:
109                         exp_if = (struct exp_if *) bootrec->data;
110                         for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
111                                 if (exp_if[i].if_id == cpu_to_le16(IF_ID_LMAC))
112                                         priv->fw_var = le16_to_cpu(exp_if[i].variant);
113                         break;
114                 case BR_CODE_DEPENDENT_IF:
115                         break;
116                 case BR_CODE_END_OF_BRA:
117                 case LEGACY_BR_CODE_END_OF_BRA:
118                         end_data = NULL;
119                         break;
120                 default:
121                         break;
122                 }
123                 bootrec = (struct bootrec *)&bootrec->data[len];
124         }
125
126         if (fw_version) {
127                 wiphy_info(priv->hw->wiphy,
128                            "FW rev %s - Softmac protocol %x.%x\n",
129                            fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
130                 snprintf(dev->wiphy->fw_version, sizeof(dev->wiphy->fw_version),
131                                 "%s - %x.%x", fw_version,
132                                 priv->fw_var >> 8, priv->fw_var & 0xff);
133         }
134
135         if (priv->fw_var < 0x500)
136                 wiphy_info(priv->hw->wiphy,
137                            "you are using an obsolete firmware. "
138                            "visit http://wireless.kernel.org/en/users/Drivers/p54 "
139                            "and grab one for \"kernel >= 2.6.28\"!\n");
140
141         if (priv->fw_var >= 0x300) {
142                 /* Firmware supports QoS, use it! */
143
144                 if (priv->fw_var >= 0x500) {
145                         priv->tx_stats[P54_QUEUE_AC_VO].limit = 16;
146                         priv->tx_stats[P54_QUEUE_AC_VI].limit = 16;
147                         priv->tx_stats[P54_QUEUE_AC_BE].limit = 16;
148                         priv->tx_stats[P54_QUEUE_AC_BK].limit = 16;
149                 } else {
150                         priv->tx_stats[P54_QUEUE_AC_VO].limit = 3;
151                         priv->tx_stats[P54_QUEUE_AC_VI].limit = 4;
152                         priv->tx_stats[P54_QUEUE_AC_BE].limit = 3;
153                         priv->tx_stats[P54_QUEUE_AC_BK].limit = 2;
154                 }
155                 priv->hw->queues = P54_QUEUE_AC_NUM;
156         }
157
158         wiphy_info(priv->hw->wiphy,
159                    "cryptographic accelerator WEP:%s, TKIP:%s, CCMP:%s\n",
160                    (priv->privacy_caps & BR_DESC_PRIV_CAP_WEP) ? "YES" : "no",
161                    (priv->privacy_caps &
162                     (BR_DESC_PRIV_CAP_TKIP | BR_DESC_PRIV_CAP_MICHAEL))
163                    ? "YES" : "no",
164                    (priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)
165                    ? "YES" : "no");
166
167         if (priv->rx_keycache_size) {
168                 /*
169                  * NOTE:
170                  *
171                  * The firmware provides at most 255 (0 - 254) slots
172                  * for keys which are then used to offload decryption.
173                  * As a result the 255 entry (aka 0xff) can be used
174                  * safely by the driver to mark keys that didn't fit
175                  * into the full cache. This trick saves us from
176                  * keeping a extra list for uploaded keys.
177                  */
178
179                 priv->used_rxkeys = kzalloc(BITS_TO_LONGS(
180                         priv->rx_keycache_size), GFP_KERNEL);
181
182                 if (!priv->used_rxkeys)
183                         return -ENOMEM;
184         }
185
186         return 0;
187 }
188 EXPORT_SYMBOL_GPL(p54_parse_firmware);
189
190 static struct sk_buff *p54_alloc_skb(struct p54_common *priv, u16 hdr_flags,
191                                      u16 payload_len, u16 type, gfp_t memflags)
192 {
193         struct p54_hdr *hdr;
194         struct sk_buff *skb;
195         size_t frame_len = sizeof(*hdr) + payload_len;
196
197         if (frame_len > P54_MAX_CTRL_FRAME_LEN)
198                 return NULL;
199
200         if (unlikely(skb_queue_len(&priv->tx_pending) > 64))
201                 return NULL;
202
203         skb = __dev_alloc_skb(priv->tx_hdr_len + frame_len, memflags);
204         if (!skb)
205                 return NULL;
206         skb_reserve(skb, priv->tx_hdr_len);
207
208         hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
209         hdr->flags = cpu_to_le16(hdr_flags);
210         hdr->len = cpu_to_le16(payload_len);
211         hdr->type = cpu_to_le16(type);
212         hdr->tries = hdr->rts_tries = 0;
213         return skb;
214 }
215
216 int p54_download_eeprom(struct p54_common *priv, void *buf,
217                         u16 offset, u16 len)
218 {
219         struct p54_eeprom_lm86 *eeprom_hdr;
220         struct sk_buff *skb;
221         size_t eeprom_hdr_size;
222         int ret = 0;
223
224         if (priv->fw_var >= 0x509)
225                 eeprom_hdr_size = sizeof(*eeprom_hdr);
226         else
227                 eeprom_hdr_size = 0x4;
228
229         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL, eeprom_hdr_size +
230                             len, P54_CONTROL_TYPE_EEPROM_READBACK,
231                             GFP_KERNEL);
232         if (unlikely(!skb))
233                 return -ENOMEM;
234
235         mutex_lock(&priv->eeprom_mutex);
236         priv->eeprom = buf;
237         eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
238                 eeprom_hdr_size + len);
239
240         if (priv->fw_var < 0x509) {
241                 eeprom_hdr->v1.offset = cpu_to_le16(offset);
242                 eeprom_hdr->v1.len = cpu_to_le16(len);
243         } else {
244                 eeprom_hdr->v2.offset = cpu_to_le32(offset);
245                 eeprom_hdr->v2.len = cpu_to_le16(len);
246                 eeprom_hdr->v2.magic2 = 0xf;
247                 memcpy(eeprom_hdr->v2.magic, (const char *)"LOCK", 4);
248         }
249
250         p54_tx(priv, skb);
251
252         if (!wait_for_completion_interruptible_timeout(
253              &priv->eeprom_comp, HZ)) {
254                 wiphy_err(priv->hw->wiphy, "device does not respond!\n");
255                 ret = -EBUSY;
256         }
257         priv->eeprom = NULL;
258         mutex_unlock(&priv->eeprom_mutex);
259         return ret;
260 }
261
262 int p54_update_beacon_tim(struct p54_common *priv, u16 aid, bool set)
263 {
264         struct sk_buff *skb;
265         struct p54_tim *tim;
266
267         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*tim),
268                             P54_CONTROL_TYPE_TIM, GFP_ATOMIC);
269         if (unlikely(!skb))
270                 return -ENOMEM;
271
272         tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
273         tim->count = 1;
274         tim->entry[0] = cpu_to_le16(set ? (aid | 0x8000) : aid);
275         p54_tx(priv, skb);
276         return 0;
277 }
278
279 int p54_sta_unlock(struct p54_common *priv, u8 *addr)
280 {
281         struct sk_buff *skb;
282         struct p54_sta_unlock *sta;
283
284         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*sta),
285                             P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
286         if (unlikely(!skb))
287                 return -ENOMEM;
288
289         sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
290         memcpy(sta->addr, addr, ETH_ALEN);
291         p54_tx(priv, skb);
292         return 0;
293 }
294
295 int p54_tx_cancel(struct p54_common *priv, __le32 req_id)
296 {
297         struct sk_buff *skb;
298         struct p54_txcancel *cancel;
299         u32 _req_id = le32_to_cpu(req_id);
300
301         if (unlikely(_req_id < priv->rx_start || _req_id > priv->rx_end))
302                 return -EINVAL;
303
304         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*cancel),
305                             P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
306         if (unlikely(!skb))
307                 return -ENOMEM;
308
309         cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
310         cancel->req_id = req_id;
311         p54_tx(priv, skb);
312         return 0;
313 }
314
315 int p54_setup_mac(struct p54_common *priv)
316 {
317         struct sk_buff *skb;
318         struct p54_setup_mac *setup;
319         u16 mode;
320
321         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup),
322                             P54_CONTROL_TYPE_SETUP, GFP_ATOMIC);
323         if (!skb)
324                 return -ENOMEM;
325
326         setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
327         if (!(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) {
328                 switch (priv->mode) {
329                 case NL80211_IFTYPE_STATION:
330                         mode = P54_FILTER_TYPE_STATION;
331                         break;
332                 case NL80211_IFTYPE_AP:
333                         mode = P54_FILTER_TYPE_AP;
334                         break;
335                 case NL80211_IFTYPE_ADHOC:
336                 case NL80211_IFTYPE_MESH_POINT:
337                         mode = P54_FILTER_TYPE_IBSS;
338                         break;
339                 case NL80211_IFTYPE_MONITOR:
340                         mode = P54_FILTER_TYPE_PROMISCUOUS;
341                         break;
342                 default:
343                         mode = P54_FILTER_TYPE_HIBERNATE;
344                         break;
345                 }
346
347                 /*
348                  * "TRANSPARENT and PROMISCUOUS are mutually exclusive"
349                  * STSW45X0C LMAC API - page 12
350                  */
351                 if (((priv->filter_flags & FIF_PROMISC_IN_BSS) ||
352                      (priv->filter_flags & FIF_OTHER_BSS)) &&
353                     (mode != P54_FILTER_TYPE_PROMISCUOUS))
354                         mode |= P54_FILTER_TYPE_TRANSPARENT;
355         } else {
356                 mode = P54_FILTER_TYPE_HIBERNATE;
357         }
358
359         setup->mac_mode = cpu_to_le16(mode);
360         memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
361         memcpy(setup->bssid, priv->bssid, ETH_ALEN);
362         setup->rx_antenna = 2 & priv->rx_diversity_mask; /* automatic */
363         setup->rx_align = 0;
364         if (priv->fw_var < 0x500) {
365                 setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
366                 memset(setup->v1.rts_rates, 0, 8);
367                 setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
368                 setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
369                 setup->v1.rxhw = cpu_to_le16(priv->rxhw);
370                 setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
371                 setup->v1.unalloc0 = cpu_to_le16(0);
372         } else {
373                 setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
374                 setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
375                 setup->v2.rxhw = cpu_to_le16(priv->rxhw);
376                 setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
377                 setup->v2.truncate = cpu_to_le16(48896);
378                 setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
379                 setup->v2.sbss_offset = 0;
380                 setup->v2.mcast_window = 0;
381                 setup->v2.rx_rssi_threshold = 0;
382                 setup->v2.rx_ed_threshold = 0;
383                 setup->v2.ref_clock = cpu_to_le32(644245094);
384                 setup->v2.lpf_bandwidth = cpu_to_le16(65535);
385                 setup->v2.osc_start_delay = cpu_to_le16(65535);
386         }
387         p54_tx(priv, skb);
388         priv->phy_idle = mode == P54_FILTER_TYPE_HIBERNATE;
389         return 0;
390 }
391
392 int p54_scan(struct p54_common *priv, u16 mode, u16 dwell)
393 {
394         struct sk_buff *skb;
395         struct p54_hdr *hdr;
396         struct p54_scan_head *head;
397         struct p54_iq_autocal_entry *iq_autocal;
398         union p54_scan_body_union *body;
399         struct p54_scan_tail_rate *rate;
400         struct pda_rssi_cal_entry *rssi;
401         struct p54_rssi_db_entry *rssi_data;
402         unsigned int i;
403         void *entry;
404         __le16 freq = cpu_to_le16(priv->hw->conf.chandef.chan->center_freq);
405
406         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*head) +
407                             2 + sizeof(*iq_autocal) + sizeof(*body) +
408                             sizeof(*rate) + 2 * sizeof(*rssi),
409                             P54_CONTROL_TYPE_SCAN, GFP_ATOMIC);
410         if (!skb)
411                 return -ENOMEM;
412
413         head = (struct p54_scan_head *) skb_put(skb, sizeof(*head));
414         memset(head->scan_params, 0, sizeof(head->scan_params));
415         head->mode = cpu_to_le16(mode);
416         head->dwell = cpu_to_le16(dwell);
417         head->freq = freq;
418
419         if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
420                 __le16 *pa_power_points = (__le16 *) skb_put(skb, 2);
421                 *pa_power_points = cpu_to_le16(0x0c);
422         }
423
424         iq_autocal = (void *) skb_put(skb, sizeof(*iq_autocal));
425         for (i = 0; i < priv->iq_autocal_len; i++) {
426                 if (priv->iq_autocal[i].freq != freq)
427                         continue;
428
429                 memcpy(iq_autocal, &priv->iq_autocal[i].params,
430                        sizeof(struct p54_iq_autocal_entry));
431                 break;
432         }
433         if (i == priv->iq_autocal_len)
434                 goto err;
435
436         if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW)
437                 body = (void *) skb_put(skb, sizeof(body->longbow));
438         else
439                 body = (void *) skb_put(skb, sizeof(body->normal));
440
441         for (i = 0; i < priv->output_limit->entries; i++) {
442                 __le16 *entry_freq = (void *) (priv->output_limit->data +
443                                      priv->output_limit->entry_size * i);
444
445                 if (*entry_freq != freq)
446                         continue;
447
448                 if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
449                         memcpy(&body->longbow.power_limits,
450                                (void *) entry_freq + sizeof(__le16),
451                                priv->output_limit->entry_size);
452                 } else {
453                         struct pda_channel_output_limit *limits =
454                                (void *) entry_freq;
455
456                         body->normal.val_barker = 0x38;
457                         body->normal.val_bpsk = body->normal.dup_bpsk =
458                                 limits->val_bpsk;
459                         body->normal.val_qpsk = body->normal.dup_qpsk =
460                                 limits->val_qpsk;
461                         body->normal.val_16qam = body->normal.dup_16qam =
462                                 limits->val_16qam;
463                         body->normal.val_64qam = body->normal.dup_64qam =
464                                 limits->val_64qam;
465                 }
466                 break;
467         }
468         if (i == priv->output_limit->entries)
469                 goto err;
470
471         entry = (void *)(priv->curve_data->data + priv->curve_data->offset);
472         for (i = 0; i < priv->curve_data->entries; i++) {
473                 if (*((__le16 *)entry) != freq) {
474                         entry += priv->curve_data->entry_size;
475                         continue;
476                 }
477
478                 if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
479                         memcpy(&body->longbow.curve_data,
480                                 entry + sizeof(__le16),
481                                 priv->curve_data->entry_size);
482                 } else {
483                         struct p54_scan_body *chan = &body->normal;
484                         struct pda_pa_curve_data *curve_data =
485                                 (void *) priv->curve_data->data;
486
487                         entry += sizeof(__le16);
488                         chan->pa_points_per_curve = 8;
489                         memset(chan->curve_data, 0, sizeof(chan->curve_data));
490                         memcpy(chan->curve_data, entry,
491                                sizeof(struct p54_pa_curve_data_sample) *
492                                min((u8)8, curve_data->points_per_channel));
493                 }
494                 break;
495         }
496         if (i == priv->curve_data->entries)
497                 goto err;
498
499         if ((priv->fw_var >= 0x500) && (priv->fw_var < 0x509)) {
500                 rate = (void *) skb_put(skb, sizeof(*rate));
501                 rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
502                 for (i = 0; i < sizeof(rate->rts_rates); i++)
503                         rate->rts_rates[i] = i;
504         }
505
506         rssi = (struct pda_rssi_cal_entry *) skb_put(skb, sizeof(*rssi));
507         rssi_data = p54_rssi_find(priv, le16_to_cpu(freq));
508         rssi->mul = cpu_to_le16(rssi_data->mul);
509         rssi->add = cpu_to_le16(rssi_data->add);
510         if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
511                 /* Longbow frontend needs ever more */
512                 rssi = (void *) skb_put(skb, sizeof(*rssi));
513                 rssi->mul = cpu_to_le16(rssi_data->longbow_unkn);
514                 rssi->add = cpu_to_le16(rssi_data->longbow_unk2);
515         }
516
517         if (priv->fw_var >= 0x509) {
518                 rate = (void *) skb_put(skb, sizeof(*rate));
519                 rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
520                 for (i = 0; i < sizeof(rate->rts_rates); i++)
521                         rate->rts_rates[i] = i;
522         }
523
524         hdr = (struct p54_hdr *) skb->data;
525         hdr->len = cpu_to_le16(skb->len - sizeof(*hdr));
526
527         p54_tx(priv, skb);
528         priv->cur_rssi = rssi_data;
529         return 0;
530
531 err:
532         wiphy_err(priv->hw->wiphy, "frequency change to channel %d failed.\n",
533                   ieee80211_frequency_to_channel(
534                           priv->hw->conf.chandef.chan->center_freq));
535
536         dev_kfree_skb_any(skb);
537         return -EINVAL;
538 }
539
540 int p54_set_leds(struct p54_common *priv)
541 {
542         struct sk_buff *skb;
543         struct p54_led *led;
544
545         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led),
546                             P54_CONTROL_TYPE_LED, GFP_ATOMIC);
547         if (unlikely(!skb))
548                 return -ENOMEM;
549
550         led = (struct p54_led *) skb_put(skb, sizeof(*led));
551         led->flags = cpu_to_le16(0x0003);
552         led->mask[0] = led->mask[1] = cpu_to_le16(priv->softled_state);
553         led->delay[0] = cpu_to_le16(1);
554         led->delay[1] = cpu_to_le16(0);
555         p54_tx(priv, skb);
556         return 0;
557 }
558
559 int p54_set_edcf(struct p54_common *priv)
560 {
561         struct sk_buff *skb;
562         struct p54_edcf *edcf;
563         u8 rtd;
564
565         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf),
566                             P54_CONTROL_TYPE_DCFINIT, GFP_ATOMIC);
567         if (unlikely(!skb))
568                 return -ENOMEM;
569
570         edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
571         if (priv->use_short_slot) {
572                 edcf->slottime = 9;
573                 edcf->sifs = 0x10;
574                 edcf->eofpad = 0x00;
575         } else {
576                 edcf->slottime = 20;
577                 edcf->sifs = 0x0a;
578                 edcf->eofpad = 0x06;
579         }
580         /*
581          * calculate the extra round trip delay according to the
582          * formula from 802.11-2007 17.3.8.6.
583          */
584         rtd = 3 * priv->coverage_class;
585         edcf->slottime += rtd;
586         edcf->round_trip_delay = cpu_to_le16(rtd);
587         /* (see prism54/isl_oid.h for further details) */
588         edcf->frameburst = cpu_to_le16(0);
589         edcf->flags = 0;
590         memset(edcf->mapping, 0, sizeof(edcf->mapping));
591         memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
592         p54_tx(priv, skb);
593         return 0;
594 }
595
596 int p54_set_ps(struct p54_common *priv)
597 {
598         struct sk_buff *skb;
599         struct p54_psm *psm;
600         unsigned int i;
601         u16 mode;
602
603         if (priv->hw->conf.flags & IEEE80211_CONF_PS &&
604             !priv->powersave_override)
605                 mode = P54_PSM | P54_PSM_BEACON_TIMEOUT | P54_PSM_DTIM |
606                        P54_PSM_CHECKSUM | P54_PSM_MCBC;
607         else
608                 mode = P54_PSM_CAM;
609
610         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*psm),
611                             P54_CONTROL_TYPE_PSM, GFP_ATOMIC);
612         if (!skb)
613                 return -ENOMEM;
614
615         psm = (struct p54_psm *)skb_put(skb, sizeof(*psm));
616         psm->mode = cpu_to_le16(mode);
617         psm->aid = cpu_to_le16(priv->aid);
618         for (i = 0; i < ARRAY_SIZE(psm->intervals); i++) {
619                 psm->intervals[i].interval =
620                         cpu_to_le16(priv->hw->conf.listen_interval);
621                 psm->intervals[i].periods = cpu_to_le16(1);
622         }
623
624         psm->beacon_rssi_skip_max = 200;
625         psm->rssi_delta_threshold = 0;
626         psm->nr = 1;
627         psm->exclude[0] = WLAN_EID_TIM;
628
629         p54_tx(priv, skb);
630         priv->phy_ps = mode != P54_PSM_CAM;
631         return 0;
632 }
633
634 int p54_init_xbow_synth(struct p54_common *priv)
635 {
636         struct sk_buff *skb;
637         struct p54_xbow_synth *xbow;
638
639         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow),
640                             P54_CONTROL_TYPE_XBOW_SYNTH_CFG, GFP_KERNEL);
641         if (unlikely(!skb))
642                 return -ENOMEM;
643
644         xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
645         xbow->magic1 = cpu_to_le16(0x1);
646         xbow->magic2 = cpu_to_le16(0x2);
647         xbow->freq = cpu_to_le16(5390);
648         memset(xbow->padding, 0, sizeof(xbow->padding));
649         p54_tx(priv, skb);
650         return 0;
651 }
652
653 int p54_upload_key(struct p54_common *priv, u8 algo, int slot, u8 idx, u8 len,
654                    u8 *addr, u8* key)
655 {
656         struct sk_buff *skb;
657         struct p54_keycache *rxkey;
658
659         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*rxkey),
660                             P54_CONTROL_TYPE_RX_KEYCACHE, GFP_KERNEL);
661         if (unlikely(!skb))
662                 return -ENOMEM;
663
664         rxkey = (struct p54_keycache *)skb_put(skb, sizeof(*rxkey));
665         rxkey->entry = slot;
666         rxkey->key_id = idx;
667         rxkey->key_type = algo;
668         if (addr)
669                 memcpy(rxkey->mac, addr, ETH_ALEN);
670         else
671                 memset(rxkey->mac, ~0, ETH_ALEN);
672
673         switch (algo) {
674         case P54_CRYPTO_WEP:
675         case P54_CRYPTO_AESCCMP:
676                 rxkey->key_len = min_t(u8, 16, len);
677                 memcpy(rxkey->key, key, rxkey->key_len);
678                 break;
679
680         case P54_CRYPTO_TKIPMICHAEL:
681                 rxkey->key_len = 24;
682                 memcpy(rxkey->key, key, 16);
683                 memcpy(&(rxkey->key[16]), &(key
684                         [NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]), 8);
685                 break;
686
687         case P54_CRYPTO_NONE:
688                 rxkey->key_len = 0;
689                 memset(rxkey->key, 0, sizeof(rxkey->key));
690                 break;
691
692         default:
693                 wiphy_err(priv->hw->wiphy,
694                           "invalid cryptographic algorithm: %d\n", algo);
695                 dev_kfree_skb(skb);
696                 return -EINVAL;
697         }
698
699         p54_tx(priv, skb);
700         return 0;
701 }
702
703 int p54_fetch_statistics(struct p54_common *priv)
704 {
705         struct ieee80211_tx_info *txinfo;
706         struct p54_tx_info *p54info;
707         struct sk_buff *skb;
708
709         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL,
710                             sizeof(struct p54_statistics),
711                             P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
712         if (!skb)
713                 return -ENOMEM;
714
715         /*
716          * The statistic feedback causes some extra headaches here, if it
717          * is not to crash/corrupt the firmware data structures.
718          *
719          * Unlike all other Control Get OIDs we can not use helpers like
720          * skb_put to reserve the space for the data we're requesting.
721          * Instead the extra frame length -which will hold the results later-
722          * will only be told to the p54_assign_address, so that following
723          * frames won't be placed into the  allegedly empty area.
724          */
725         txinfo = IEEE80211_SKB_CB(skb);
726         p54info = (void *) txinfo->rate_driver_data;
727         p54info->extra_len = sizeof(struct p54_statistics);
728
729         p54_tx(priv, skb);
730         return 0;
731 }
732
733 int p54_set_groupfilter(struct p54_common *priv)
734 {
735         struct p54_group_address_table *grp;
736         struct sk_buff *skb;
737         bool on = false;
738
739         skb = p54_alloc_skb(priv, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*grp),
740                             P54_CONTROL_TYPE_GROUP_ADDRESS_TABLE, GFP_KERNEL);
741         if (!skb)
742                 return -ENOMEM;
743
744         grp = (struct p54_group_address_table *)skb_put(skb, sizeof(*grp));
745
746         on = !(priv->filter_flags & FIF_ALLMULTI) &&
747              (priv->mc_maclist_num > 0 &&
748               priv->mc_maclist_num <= MC_FILTER_ADDRESS_NUM);
749
750         if (on) {
751                 grp->filter_enable = cpu_to_le16(1);
752                 grp->num_address = cpu_to_le16(priv->mc_maclist_num);
753                 memcpy(grp->mac_list, priv->mc_maclist, sizeof(grp->mac_list));
754         } else {
755                 grp->filter_enable = cpu_to_le16(0);
756                 grp->num_address = cpu_to_le16(0);
757                 memset(grp->mac_list, 0, sizeof(grp->mac_list));
758         }
759
760         p54_tx(priv, skb);
761         return 0;
762 }