blob: 6d8248eac6e8eb90e63154da0d61bc3032504888 [file] [log] [blame]
Michael Wueff1a592007-09-25 18:11:01 -07001
2/*
3 * Common code for mac80211 Prism54 drivers
4 *
5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
6 * Copyright (c) 2007, Christian Lamparter <chunkeey@web.de>
7 *
8 * Based on the islsm (softmac prism54) driver, which is:
9 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/init.h>
17#include <linux/firmware.h>
18#include <linux/etherdevice.h>
19
20#include <net/mac80211.h>
21
22#include "p54.h"
23#include "p54common.h"
24
25MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
26MODULE_DESCRIPTION("Softmac Prism54 common code");
27MODULE_LICENSE("GPL");
28MODULE_ALIAS("prism54common");
29
Johannes Berg8318d782008-01-24 19:38:38 +010030static struct ieee80211_rate p54_rates[] = {
31 { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
32 { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
33 { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
34 { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
35 { .bitrate = 60, .hw_value = 4, },
36 { .bitrate = 90, .hw_value = 5, },
37 { .bitrate = 120, .hw_value = 6, },
38 { .bitrate = 180, .hw_value = 7, },
39 { .bitrate = 240, .hw_value = 8, },
40 { .bitrate = 360, .hw_value = 9, },
41 { .bitrate = 480, .hw_value = 10, },
42 { .bitrate = 540, .hw_value = 11, },
43};
44
45static struct ieee80211_channel p54_channels[] = {
46 { .center_freq = 2412, .hw_value = 1, },
47 { .center_freq = 2417, .hw_value = 2, },
48 { .center_freq = 2422, .hw_value = 3, },
49 { .center_freq = 2427, .hw_value = 4, },
50 { .center_freq = 2432, .hw_value = 5, },
51 { .center_freq = 2437, .hw_value = 6, },
52 { .center_freq = 2442, .hw_value = 7, },
53 { .center_freq = 2447, .hw_value = 8, },
54 { .center_freq = 2452, .hw_value = 9, },
55 { .center_freq = 2457, .hw_value = 10, },
56 { .center_freq = 2462, .hw_value = 11, },
57 { .center_freq = 2467, .hw_value = 12, },
58 { .center_freq = 2472, .hw_value = 13, },
59 { .center_freq = 2484, .hw_value = 14, },
60};
61
Johannes Bergc2976ab2008-02-20 12:08:12 +010062static struct ieee80211_supported_band band_2GHz = {
Johannes Berg8318d782008-01-24 19:38:38 +010063 .channels = p54_channels,
64 .n_channels = ARRAY_SIZE(p54_channels),
65 .bitrates = p54_rates,
66 .n_bitrates = ARRAY_SIZE(p54_rates),
67};
68
Christian Lamparter4e416a62008-09-01 22:48:41 +020069int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
Michael Wueff1a592007-09-25 18:11:01 -070070{
71 struct p54_common *priv = dev->priv;
72 struct bootrec_exp_if *exp_if;
73 struct bootrec *bootrec;
74 u32 *data = (u32 *)fw->data;
75 u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
76 u8 *fw_version = NULL;
77 size_t len;
78 int i;
79
80 if (priv->rx_start)
Christian Lamparter4e416a62008-09-01 22:48:41 +020081 return 0;
Michael Wueff1a592007-09-25 18:11:01 -070082
83 while (data < end_data && *data)
84 data++;
85
86 while (data < end_data && !*data)
87 data++;
88
89 bootrec = (struct bootrec *) data;
90
91 while (bootrec->data <= end_data &&
92 (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
93 u32 code = le32_to_cpu(bootrec->code);
94 switch (code) {
95 case BR_CODE_COMPONENT_ID:
Al Virodc73c622007-12-21 23:49:02 -050096 switch (be32_to_cpu(*(__be32 *)bootrec->data)) {
Michael Wueff1a592007-09-25 18:11:01 -070097 case FW_FMAC:
98 printk(KERN_INFO "p54: FreeMAC firmware\n");
99 break;
100 case FW_LM20:
101 printk(KERN_INFO "p54: LM20 firmware\n");
102 break;
103 case FW_LM86:
104 printk(KERN_INFO "p54: LM86 firmware\n");
105 break;
106 case FW_LM87:
107 printk(KERN_INFO "p54: LM87 firmware - not supported yet!\n");
108 break;
109 default:
110 printk(KERN_INFO "p54: unknown firmware\n");
111 break;
112 }
113 break;
114 case BR_CODE_COMPONENT_VERSION:
115 /* 24 bytes should be enough for all firmwares */
116 if (strnlen((unsigned char*)bootrec->data, 24) < 24)
117 fw_version = (unsigned char*)bootrec->data;
118 break;
Christian Lamparter4e416a62008-09-01 22:48:41 +0200119 case BR_CODE_DESCR: {
120 struct bootrec_desc *desc =
121 (struct bootrec_desc *)bootrec->data;
122 priv->rx_start = le32_to_cpu(desc->rx_start);
Michael Wueff1a592007-09-25 18:11:01 -0700123 /* FIXME add sanity checking */
Christian Lamparter4e416a62008-09-01 22:48:41 +0200124 priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
125 priv->headroom = desc->headroom;
126 priv->tailroom = desc->tailroom;
127 if (bootrec->len == 11)
128 priv->rx_mtu = (size_t) le16_to_cpu(
129 (__le16)bootrec->data[10]);
130 else
131 priv->rx_mtu = (size_t)
132 0x620 - priv->tx_hdr_len;
Michael Wueff1a592007-09-25 18:11:01 -0700133 break;
Christian Lamparter4e416a62008-09-01 22:48:41 +0200134 }
Michael Wueff1a592007-09-25 18:11:01 -0700135 case BR_CODE_EXPOSED_IF:
136 exp_if = (struct bootrec_exp_if *) bootrec->data;
137 for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
Al Virodc73c622007-12-21 23:49:02 -0500138 if (exp_if[i].if_id == cpu_to_le16(0x1a))
Michael Wueff1a592007-09-25 18:11:01 -0700139 priv->fw_var = le16_to_cpu(exp_if[i].variant);
140 break;
141 case BR_CODE_DEPENDENT_IF:
142 break;
143 case BR_CODE_END_OF_BRA:
144 case LEGACY_BR_CODE_END_OF_BRA:
145 end_data = NULL;
146 break;
147 default:
148 break;
149 }
150 bootrec = (struct bootrec *)&bootrec->data[len];
151 }
152
153 if (fw_version)
154 printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
155 fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
156
157 if (priv->fw_var >= 0x300) {
158 /* Firmware supports QoS, use it! */
Chr84df3ed2008-08-24 03:15:16 +0200159 priv->tx_stats[4].limit = 3;
160 priv->tx_stats[5].limit = 4;
161 priv->tx_stats[6].limit = 3;
162 priv->tx_stats[7].limit = 1;
Michael Wueff1a592007-09-25 18:11:01 -0700163 dev->queues = 4;
164 }
Christian Lamparter4e416a62008-09-01 22:48:41 +0200165
166 return 0;
Michael Wueff1a592007-09-25 18:11:01 -0700167}
168EXPORT_SYMBOL_GPL(p54_parse_firmware);
169
Christian Lamparter154e3af2008-08-23 22:15:25 +0200170static int p54_convert_rev0(struct ieee80211_hw *dev,
171 struct pda_pa_curve_data *curve_data)
Michael Wueff1a592007-09-25 18:11:01 -0700172{
173 struct p54_common *priv = dev->priv;
Christian Lamparter154e3af2008-08-23 22:15:25 +0200174 struct p54_pa_curve_data_sample *dst;
175 struct pda_pa_curve_data_sample_rev0 *src;
Michael Wueff1a592007-09-25 18:11:01 -0700176 size_t cd_len = sizeof(*curve_data) +
Christian Lamparter154e3af2008-08-23 22:15:25 +0200177 (curve_data->points_per_channel*sizeof(*dst) + 2) *
Michael Wueff1a592007-09-25 18:11:01 -0700178 curve_data->channels;
179 unsigned int i, j;
180 void *source, *target;
181
182 priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
183 if (!priv->curve_data)
184 return -ENOMEM;
185
186 memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
187 source = curve_data->data;
188 target = priv->curve_data->data;
189 for (i = 0; i < curve_data->channels; i++) {
190 __le16 *freq = source;
191 source += sizeof(__le16);
192 *((__le16 *)target) = *freq;
193 target += sizeof(__le16);
194 for (j = 0; j < curve_data->points_per_channel; j++) {
Christian Lamparter154e3af2008-08-23 22:15:25 +0200195 dst = target;
196 src = source;
Michael Wueff1a592007-09-25 18:11:01 -0700197
Christian Lamparter154e3af2008-08-23 22:15:25 +0200198 dst->rf_power = src->rf_power;
199 dst->pa_detector = src->pa_detector;
200 dst->data_64qam = src->pcv;
Michael Wueff1a592007-09-25 18:11:01 -0700201 /* "invent" the points for the other modulations */
202#define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
Christian Lamparter154e3af2008-08-23 22:15:25 +0200203 dst->data_16qam = SUB(src->pcv, 12);
204 dst->data_qpsk = SUB(dst->data_16qam, 12);
205 dst->data_bpsk = SUB(dst->data_qpsk, 12);
206 dst->data_barker = SUB(dst->data_bpsk, 14);
Michael Wueff1a592007-09-25 18:11:01 -0700207#undef SUB
Christian Lamparter154e3af2008-08-23 22:15:25 +0200208 target += sizeof(*dst);
209 source += sizeof(*src);
Michael Wueff1a592007-09-25 18:11:01 -0700210 }
211 }
212
213 return 0;
214}
215
Christian Lamparter154e3af2008-08-23 22:15:25 +0200216static int p54_convert_rev1(struct ieee80211_hw *dev,
217 struct pda_pa_curve_data *curve_data)
218{
219 struct p54_common *priv = dev->priv;
220 struct p54_pa_curve_data_sample *dst;
221 struct pda_pa_curve_data_sample_rev1 *src;
222 size_t cd_len = sizeof(*curve_data) +
223 (curve_data->points_per_channel*sizeof(*dst) + 2) *
224 curve_data->channels;
225 unsigned int i, j;
226 void *source, *target;
227
228 priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
229 if (!priv->curve_data)
230 return -ENOMEM;
231
232 memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
233 source = curve_data->data;
234 target = priv->curve_data->data;
235 for (i = 0; i < curve_data->channels; i++) {
236 __le16 *freq = source;
237 source += sizeof(__le16);
238 *((__le16 *)target) = *freq;
239 target += sizeof(__le16);
240 for (j = 0; j < curve_data->points_per_channel; j++) {
241 memcpy(target, source, sizeof(*src));
242
243 target += sizeof(*dst);
244 source += sizeof(*src);
245 }
246 source++;
247 }
248
249 return 0;
250}
251
Christian Lamparter7cb77072008-09-01 22:48:51 +0200252const char* p54_rf_chips[] = { "NULL", "Indigo?", "Duette",
253 "Frisbee", "Xbow", "Longbow" };
254
Michael Wueff1a592007-09-25 18:11:01 -0700255int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
256{
257 struct p54_common *priv = dev->priv;
258 struct eeprom_pda_wrap *wrap = NULL;
259 struct pda_entry *entry;
Michael Wueff1a592007-09-25 18:11:01 -0700260 unsigned int data_len, entry_len;
261 void *tmp;
262 int err;
Johannes Bergc2f2d3a2008-02-29 23:28:25 +0100263 u8 *end = (u8 *)eeprom + len;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200264 DECLARE_MAC_BUF(mac);
Michael Wueff1a592007-09-25 18:11:01 -0700265
266 wrap = (struct eeprom_pda_wrap *) eeprom;
Johannes Berg8c282932008-02-29 13:56:33 +0100267 entry = (void *)wrap->data + le16_to_cpu(wrap->len);
Johannes Bergc2f2d3a2008-02-29 23:28:25 +0100268
269 /* verify that at least the entry length/code fits */
270 while ((u8 *)entry <= end - sizeof(*entry)) {
Michael Wueff1a592007-09-25 18:11:01 -0700271 entry_len = le16_to_cpu(entry->len);
272 data_len = ((entry_len - 1) << 1);
Johannes Bergc2f2d3a2008-02-29 23:28:25 +0100273
274 /* abort if entry exceeds whole structure */
275 if ((u8 *)entry + sizeof(*entry) + data_len > end)
276 break;
277
Michael Wueff1a592007-09-25 18:11:01 -0700278 switch (le16_to_cpu(entry->code)) {
279 case PDR_MAC_ADDRESS:
280 SET_IEEE80211_PERM_ADDR(dev, entry->data);
281 break;
282 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
283 if (data_len < 2) {
284 err = -EINVAL;
285 goto err;
286 }
287
288 if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
289 err = -EINVAL;
290 goto err;
291 }
292
293 priv->output_limit = kmalloc(entry->data[1] *
294 sizeof(*priv->output_limit), GFP_KERNEL);
295
296 if (!priv->output_limit) {
297 err = -ENOMEM;
298 goto err;
299 }
300
301 memcpy(priv->output_limit, &entry->data[2],
302 entry->data[1]*sizeof(*priv->output_limit));
303 priv->output_limit_len = entry->data[1];
304 break;
Christian Lamparter154e3af2008-08-23 22:15:25 +0200305 case PDR_PRISM_PA_CAL_CURVE_DATA: {
306 struct pda_pa_curve_data *curve_data =
307 (struct pda_pa_curve_data *)entry->data;
308 if (data_len < sizeof(*curve_data)) {
Michael Wueff1a592007-09-25 18:11:01 -0700309 err = -EINVAL;
310 goto err;
311 }
312
Christian Lamparter154e3af2008-08-23 22:15:25 +0200313 switch (curve_data->cal_method_rev) {
314 case 0:
315 err = p54_convert_rev0(dev, curve_data);
316 break;
317 case 1:
318 err = p54_convert_rev1(dev, curve_data);
319 break;
320 default:
321 printk(KERN_ERR "p54: unknown curve data "
322 "revision %d\n",
323 curve_data->cal_method_rev);
324 err = -ENODEV;
325 break;
Michael Wueff1a592007-09-25 18:11:01 -0700326 }
Christian Lamparter154e3af2008-08-23 22:15:25 +0200327 if (err)
328 goto err;
Michael Wueff1a592007-09-25 18:11:01 -0700329
Christian Lamparter154e3af2008-08-23 22:15:25 +0200330 }
Michael Wueff1a592007-09-25 18:11:01 -0700331 case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
332 priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
333 if (!priv->iq_autocal) {
334 err = -ENOMEM;
335 goto err;
336 }
337
338 memcpy(priv->iq_autocal, entry->data, data_len);
339 priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
340 break;
341 case PDR_INTERFACE_LIST:
342 tmp = entry->data;
343 while ((u8 *)tmp < entry->data + data_len) {
344 struct bootrec_exp_if *exp_if = tmp;
345 if (le16_to_cpu(exp_if->if_id) == 0xF)
Christian Lamparter7cb77072008-09-01 22:48:51 +0200346 priv->rxhw = le16_to_cpu(exp_if->variant) & 0x07;
Michael Wueff1a592007-09-25 18:11:01 -0700347 tmp += sizeof(struct bootrec_exp_if);
348 }
349 break;
350 case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
351 priv->version = *(u8 *)(entry->data + 1);
352 break;
353 case PDR_END:
Johannes Bergc2f2d3a2008-02-29 23:28:25 +0100354 /* make it overrun */
355 entry_len = len;
Michael Wueff1a592007-09-25 18:11:01 -0700356 break;
Florian Fainelli58e30732008-02-25 17:51:53 +0100357 default:
358 printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
359 le16_to_cpu(entry->code));
360 break;
Michael Wueff1a592007-09-25 18:11:01 -0700361 }
362
363 entry = (void *)entry + (entry_len + 1)*2;
Michael Wueff1a592007-09-25 18:11:01 -0700364 }
365
366 if (!priv->iq_autocal || !priv->output_limit || !priv->curve_data) {
367 printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
368 err = -EINVAL;
369 goto err;
370 }
371
Christian Lamparter7cb77072008-09-01 22:48:51 +0200372 switch (priv->rxhw) {
373 case 4: /* XBow */
374 case 1: /* Indigo? */
375 case 2: /* Duette */
376 /* TODO: 5GHz initialization goes here */
377
378 case 3: /* Frisbee */
379 case 5: /* Longbow */
380 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
381 break;
382 default:
383 printk(KERN_ERR "%s: unsupported RF-Chip\n",
384 wiphy_name(dev->wiphy));
385 err = -EINVAL;
386 goto err;
387 }
388
389 if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
390 u8 perm_addr[ETH_ALEN];
391
392 printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
393 wiphy_name(dev->wiphy));
394 random_ether_addr(perm_addr);
395 SET_IEEE80211_PERM_ADDR(dev, perm_addr);
396 }
397
398 printk(KERN_INFO "%s: hwaddr %s, MAC:isl38%02x RF:%s\n",
399 wiphy_name(dev->wiphy),
400 print_mac(mac, dev->wiphy->perm_addr),
401 priv->version, p54_rf_chips[priv->rxhw]);
402
Michael Wueff1a592007-09-25 18:11:01 -0700403 return 0;
404
405 err:
406 if (priv->iq_autocal) {
407 kfree(priv->iq_autocal);
408 priv->iq_autocal = NULL;
409 }
410
411 if (priv->output_limit) {
412 kfree(priv->output_limit);
413 priv->output_limit = NULL;
414 }
415
416 if (priv->curve_data) {
417 kfree(priv->curve_data);
418 priv->curve_data = NULL;
419 }
420
421 printk(KERN_ERR "p54: eeprom parse failed!\n");
422 return err;
423}
424EXPORT_SYMBOL_GPL(p54_parse_eeprom);
425
Michael Wueff1a592007-09-25 18:11:01 -0700426static void p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
427{
428 struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
429 struct ieee80211_rx_status rx_status = {0};
430 u16 freq = le16_to_cpu(hdr->freq);
431
Bruno Randolf566bfe52008-05-08 19:15:40 +0200432 rx_status.signal = hdr->rssi;
Johannes Berg8318d782008-01-24 19:38:38 +0100433 /* XX correct? */
Larry.Finger@lwfinger.net18d72602008-06-30 10:39:49 -0500434 rx_status.qual = (100 * hdr->rssi) / 127;
Johannes Berg8318d782008-01-24 19:38:38 +0100435 rx_status.rate_idx = hdr->rate & 0xf;
Michael Wueff1a592007-09-25 18:11:01 -0700436 rx_status.freq = freq;
Johannes Berg8318d782008-01-24 19:38:38 +0100437 rx_status.band = IEEE80211_BAND_2GHZ;
Michael Wueff1a592007-09-25 18:11:01 -0700438 rx_status.antenna = hdr->antenna;
439 rx_status.mactime = le64_to_cpu(hdr->timestamp);
Johannes Berg03bffc12007-12-04 20:33:40 +0100440 rx_status.flag |= RX_FLAG_TSFT;
Michael Wueff1a592007-09-25 18:11:01 -0700441
442 skb_pull(skb, sizeof(*hdr));
443 skb_trim(skb, le16_to_cpu(hdr->len));
444
445 ieee80211_rx_irqsafe(dev, skb, &rx_status);
446}
447
448static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
449{
450 struct p54_common *priv = dev->priv;
451 int i;
452
Michael Wueff1a592007-09-25 18:11:01 -0700453 for (i = 0; i < dev->queues; i++)
Chr84df3ed2008-08-24 03:15:16 +0200454 if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
Michael Wueff1a592007-09-25 18:11:01 -0700455 ieee80211_wake_queue(dev, i);
456}
457
458static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
459{
460 struct p54_common *priv = dev->priv;
461 struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
462 struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
463 struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
Christian Lamparter4e416a62008-09-01 22:48:41 +0200464 u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
Michael Wueff1a592007-09-25 18:11:01 -0700465 struct memrecord *range = NULL;
466 u32 freed = 0;
467 u32 last_addr = priv->rx_start;
Chr031d10e2008-08-24 03:15:06 +0200468 unsigned long flags;
Michael Wueff1a592007-09-25 18:11:01 -0700469
Chr031d10e2008-08-24 03:15:06 +0200470 spin_lock_irqsave(&priv->tx_queue.lock, flags);
Michael Wueff1a592007-09-25 18:11:01 -0700471 while (entry != (struct sk_buff *)&priv->tx_queue) {
Johannes Berg552fe532008-05-30 21:07:15 +0200472 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
473 range = (void *)info->driver_data;
Michael Wueff1a592007-09-25 18:11:01 -0700474 if (range->start_addr == addr) {
Michael Wueff1a592007-09-25 18:11:01 -0700475 struct p54_control_hdr *entry_hdr;
476 struct p54_tx_control_allocdata *entry_data;
477 int pad = 0;
478
Johannes Berg552fe532008-05-30 21:07:15 +0200479 if (entry->next != (struct sk_buff *)&priv->tx_queue) {
480 struct ieee80211_tx_info *ni;
481 struct memrecord *mr;
482
483 ni = IEEE80211_SKB_CB(entry->next);
484 mr = (struct memrecord *)ni->driver_data;
485 freed = mr->start_addr - last_addr;
486 } else
Michael Wueff1a592007-09-25 18:11:01 -0700487 freed = priv->rx_end - last_addr;
488
489 last_addr = range->end_addr;
490 __skb_unlink(entry, &priv->tx_queue);
Chr031d10e2008-08-24 03:15:06 +0200491 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
492
Johannes Berge039fa42008-05-15 12:55:29 +0200493 memset(&info->status, 0, sizeof(info->status));
Michael Wueff1a592007-09-25 18:11:01 -0700494 entry_hdr = (struct p54_control_hdr *) entry->data;
495 entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
496 if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
497 pad = entry_data->align[0];
498
Chr84df3ed2008-08-24 03:15:16 +0200499 priv->tx_stats[entry_data->hw_queue].len--;
Johannes Berge039fa42008-05-15 12:55:29 +0200500 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Michael Wueff1a592007-09-25 18:11:01 -0700501 if (!(payload->status & 0x01))
Johannes Berge039fa42008-05-15 12:55:29 +0200502 info->flags |= IEEE80211_TX_STAT_ACK;
Michael Wueff1a592007-09-25 18:11:01 -0700503 else
Johannes Berge039fa42008-05-15 12:55:29 +0200504 info->status.excessive_retries = 1;
Michael Wueff1a592007-09-25 18:11:01 -0700505 }
Johannes Berge039fa42008-05-15 12:55:29 +0200506 info->status.retry_count = payload->retries - 1;
507 info->status.ack_signal = le16_to_cpu(payload->ack_rssi);
Michael Wueff1a592007-09-25 18:11:01 -0700508 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
Johannes Berge039fa42008-05-15 12:55:29 +0200509 ieee80211_tx_status_irqsafe(dev, entry);
Chr031d10e2008-08-24 03:15:06 +0200510 goto out;
Michael Wueff1a592007-09-25 18:11:01 -0700511 } else
512 last_addr = range->end_addr;
513 entry = entry->next;
514 }
Chr031d10e2008-08-24 03:15:06 +0200515 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
Michael Wueff1a592007-09-25 18:11:01 -0700516
Chr031d10e2008-08-24 03:15:06 +0200517out:
Michael Wueff1a592007-09-25 18:11:01 -0700518 if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
519 sizeof(struct p54_control_hdr))
520 p54_wake_free_queues(dev);
521}
522
Christian Lamparter7cb77072008-09-01 22:48:51 +0200523static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
524 struct sk_buff *skb)
525{
526 struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
527 struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
528 struct p54_common *priv = dev->priv;
529
530 if (!priv->eeprom)
531 return ;
532
533 memcpy(priv->eeprom, eeprom->data, eeprom->len);
534
535 complete(&priv->eeprom_comp);
536}
537
Michael Wueff1a592007-09-25 18:11:01 -0700538static void p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
539{
540 struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
541
542 switch (le16_to_cpu(hdr->type)) {
543 case P54_CONTROL_TYPE_TXDONE:
544 p54_rx_frame_sent(dev, skb);
545 break;
546 case P54_CONTROL_TYPE_BBP:
547 break;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200548 case P54_CONTROL_TYPE_EEPROM_READBACK:
549 p54_rx_eeprom_readback(dev, skb);
550 break;
Michael Wueff1a592007-09-25 18:11:01 -0700551 default:
552 printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
553 wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
554 break;
555 }
556}
557
558/* returns zero if skb can be reused */
559int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
560{
561 u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
562 switch (type) {
563 case 0x00:
564 case 0x01:
565 p54_rx_data(dev, skb);
566 return -1;
567 case 0x4d:
568 /* TODO: do something better... but then again, I've never seen this happen */
569 printk(KERN_ERR "%s: Received fault. Probably need to restart hardware now..\n",
570 wiphy_name(dev->wiphy));
571 break;
572 case 0x80:
573 p54_rx_control(dev, skb);
574 break;
575 default:
576 printk(KERN_ERR "%s: unknown frame RXed (0x%02x)\n",
577 wiphy_name(dev->wiphy), type);
578 break;
579 }
580 return 0;
581}
582EXPORT_SYMBOL_GPL(p54_rx);
583
584/*
585 * So, the firmware is somewhat stupid and doesn't know what places in its
586 * memory incoming data should go to. By poking around in the firmware, we
587 * can find some unused memory to upload our packets to. However, data that we
588 * want the card to TX needs to stay intact until the card has told us that
589 * it is done with it. This function finds empty places we can upload to and
590 * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
591 * allocated areas.
592 */
593static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
Johannes Berge039fa42008-05-15 12:55:29 +0200594 struct p54_control_hdr *data, u32 len)
Michael Wueff1a592007-09-25 18:11:01 -0700595{
596 struct p54_common *priv = dev->priv;
597 struct sk_buff *entry = priv->tx_queue.next;
598 struct sk_buff *target_skb = NULL;
Michael Wueff1a592007-09-25 18:11:01 -0700599 u32 last_addr = priv->rx_start;
600 u32 largest_hole = 0;
601 u32 target_addr = priv->rx_start;
602 unsigned long flags;
603 unsigned int left;
Christian Lamparter4e416a62008-09-01 22:48:41 +0200604 len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
Michael Wueff1a592007-09-25 18:11:01 -0700605
606 spin_lock_irqsave(&priv->tx_queue.lock, flags);
607 left = skb_queue_len(&priv->tx_queue);
608 while (left--) {
609 u32 hole_size;
Johannes Berge039fa42008-05-15 12:55:29 +0200610 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
611 struct memrecord *range = (void *)info->driver_data;
Michael Wueff1a592007-09-25 18:11:01 -0700612 hole_size = range->start_addr - last_addr;
613 if (!target_skb && hole_size >= len) {
614 target_skb = entry->prev;
615 hole_size -= len;
616 target_addr = last_addr;
617 }
618 largest_hole = max(largest_hole, hole_size);
619 last_addr = range->end_addr;
620 entry = entry->next;
621 }
622 if (!target_skb && priv->rx_end - last_addr >= len) {
623 target_skb = priv->tx_queue.prev;
624 largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
625 if (!skb_queue_empty(&priv->tx_queue)) {
Johannes Berge039fa42008-05-15 12:55:29 +0200626 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(target_skb);
627 struct memrecord *range = (void *)info->driver_data;
Michael Wueff1a592007-09-25 18:11:01 -0700628 target_addr = range->end_addr;
629 }
630 } else
631 largest_hole = max(largest_hole, priv->rx_end - last_addr);
632
633 if (skb) {
Johannes Berge039fa42008-05-15 12:55:29 +0200634 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
635 struct memrecord *range = (void *)info->driver_data;
Michael Wueff1a592007-09-25 18:11:01 -0700636 range->start_addr = target_addr;
637 range->end_addr = target_addr + len;
Michael Wueff1a592007-09-25 18:11:01 -0700638 __skb_queue_after(&priv->tx_queue, target_skb, skb);
Christian Lamparter4e416a62008-09-01 22:48:41 +0200639 if (largest_hole < priv->rx_mtu + priv->headroom +
640 priv->tailroom +
Michael Wueff1a592007-09-25 18:11:01 -0700641 sizeof(struct p54_control_hdr))
642 ieee80211_stop_queues(dev);
643 }
644 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
645
Christian Lamparter4e416a62008-09-01 22:48:41 +0200646 data->req_id = cpu_to_le32(target_addr + priv->headroom);
Michael Wueff1a592007-09-25 18:11:01 -0700647}
648
Christian Lamparter7cb77072008-09-01 22:48:51 +0200649int p54_read_eeprom(struct ieee80211_hw *dev)
650{
651 struct p54_common *priv = dev->priv;
652 struct p54_control_hdr *hdr = NULL;
653 struct p54_eeprom_lm86 *eeprom_hdr;
654 size_t eeprom_size = 0x2020, offset = 0, blocksize;
655 int ret = -ENOMEM;
656 void *eeprom = NULL;
657
658 hdr = (struct p54_control_hdr *)kzalloc(sizeof(*hdr) +
659 sizeof(*eeprom_hdr) + EEPROM_READBACK_LEN, GFP_KERNEL);
660 if (!hdr)
661 goto free;
662
663 priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
664 if (!priv->eeprom)
665 goto free;
666
667 eeprom = kzalloc(eeprom_size, GFP_KERNEL);
668 if (!eeprom)
669 goto free;
670
671 hdr->magic1 = cpu_to_le16(0x8000);
672 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);
673 hdr->retry1 = hdr->retry2 = 0;
674 eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;
675
676 while (eeprom_size) {
677 blocksize = min(eeprom_size, (size_t)EEPROM_READBACK_LEN);
678 hdr->len = cpu_to_le16(blocksize + sizeof(*eeprom_hdr));
679 eeprom_hdr->offset = cpu_to_le16(offset);
680 eeprom_hdr->len = cpu_to_le16(blocksize);
681 p54_assign_address(dev, NULL, hdr, hdr->len + sizeof(*hdr));
682 priv->tx(dev, hdr, hdr->len + sizeof(*hdr), 0);
683
684 if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
685 printk(KERN_ERR "%s: device does not respond!\n",
686 wiphy_name(dev->wiphy));
687 ret = -EBUSY;
688 goto free;
689 }
690
691 memcpy(eeprom + offset, priv->eeprom, blocksize);
692 offset += blocksize;
693 eeprom_size -= blocksize;
694 }
695
696 ret = p54_parse_eeprom(dev, eeprom, offset);
697free:
698 kfree(priv->eeprom);
699 priv->eeprom = NULL;
700 kfree(hdr);
701 kfree(eeprom);
702
703 return ret;
704}
705EXPORT_SYMBOL_GPL(p54_read_eeprom);
706
Johannes Berge039fa42008-05-15 12:55:29 +0200707static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
Michael Wueff1a592007-09-25 18:11:01 -0700708{
Johannes Berge039fa42008-05-15 12:55:29 +0200709 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg57ffc582008-04-29 17:18:59 +0200710 struct ieee80211_tx_queue_stats *current_queue;
Michael Wueff1a592007-09-25 18:11:01 -0700711 struct p54_common *priv = dev->priv;
712 struct p54_control_hdr *hdr;
Larry Fingereda0c002008-08-05 11:23:16 -0500713 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
Michael Wueff1a592007-09-25 18:11:01 -0700714 struct p54_tx_control_allocdata *txhdr;
Michael Wueff1a592007-09-25 18:11:01 -0700715 size_t padding, len;
716 u8 rate;
Christian Lamparteraaa15532008-08-09 19:20:47 -0500717 u8 cts_rate = 0x20;
Michael Wueff1a592007-09-25 18:11:01 -0700718
Chr84df3ed2008-08-24 03:15:16 +0200719 current_queue = &priv->tx_stats[skb_get_queue_mapping(skb) + 4];
Michael Wueff1a592007-09-25 18:11:01 -0700720 if (unlikely(current_queue->len > current_queue->limit))
721 return NETDEV_TX_BUSY;
722 current_queue->len++;
723 current_queue->count++;
724 if (current_queue->len == current_queue->limit)
Johannes Berge2530082008-05-17 00:57:14 +0200725 ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
Michael Wueff1a592007-09-25 18:11:01 -0700726
727 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
728 len = skb->len;
729
Michael Wueff1a592007-09-25 18:11:01 -0700730 txhdr = (struct p54_tx_control_allocdata *)
731 skb_push(skb, sizeof(*txhdr) + padding);
732 hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
733
734 if (padding)
735 hdr->magic1 = cpu_to_le16(0x4010);
736 else
737 hdr->magic1 = cpu_to_le16(0x0010);
738 hdr->len = cpu_to_le16(len);
Johannes Berge039fa42008-05-15 12:55:29 +0200739 hdr->type = (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 0 : cpu_to_le16(1);
740 hdr->retry1 = hdr->retry2 = info->control.retry_limit;
Michael Wueff1a592007-09-25 18:11:01 -0700741
Michael Wueff1a592007-09-25 18:11:01 -0700742 /* TODO: add support for alternate retry TX rates */
Johannes Berge039fa42008-05-15 12:55:29 +0200743 rate = ieee80211_get_tx_rate(dev, info)->hw_value;
Christian Lamparteraaa15532008-08-09 19:20:47 -0500744 if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE) {
Johannes Berg8318d782008-01-24 19:38:38 +0100745 rate |= 0x10;
Christian Lamparteraaa15532008-08-09 19:20:47 -0500746 cts_rate |= 0x10;
747 }
748 if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
Michael Wueff1a592007-09-25 18:11:01 -0700749 rate |= 0x40;
Christian Lamparteraaa15532008-08-09 19:20:47 -0500750 cts_rate |= ieee80211_get_rts_cts_rate(dev, info)->hw_value;
751 } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
Michael Wueff1a592007-09-25 18:11:01 -0700752 rate |= 0x20;
Christian Lamparteraaa15532008-08-09 19:20:47 -0500753 cts_rate |= ieee80211_get_rts_cts_rate(dev, info)->hw_value;
754 }
Michael Wueff1a592007-09-25 18:11:01 -0700755 memset(txhdr->rateset, rate, 8);
Christian Lamparteraaa15532008-08-09 19:20:47 -0500756 txhdr->key_type = 0;
757 txhdr->key_len = 0;
758 txhdr->hw_queue = skb_get_queue_mapping(skb) + 4;
759 txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
Johannes Berge039fa42008-05-15 12:55:29 +0200760 2 : info->antenna_sel_tx - 1;
Michael Wueff1a592007-09-25 18:11:01 -0700761 txhdr->output_power = 0x7f; // HW Maximum
Christian Lamparteraaa15532008-08-09 19:20:47 -0500762 txhdr->cts_rate = (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
763 0 : cts_rate;
Michael Wueff1a592007-09-25 18:11:01 -0700764 if (padding)
765 txhdr->align[0] = padding;
766
Larry Fingereda0c002008-08-05 11:23:16 -0500767 /* FIXME: The sequence that follows is needed for this driver to
768 * work with mac80211 since "mac80211: fix TX sequence numbers".
769 * As with the temporary code in rt2x00, changes will be needed
770 * to get proper sequence numbers on beacons. In addition, this
771 * patch places the sequence number in the hardware state, which
772 * limits us to a single virtual state.
773 */
774 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
775 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
776 priv->seqno += 0x10;
777 ieee80211hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
778 ieee80211hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
779 }
Johannes Berge039fa42008-05-15 12:55:29 +0200780 /* modifies skb->cb and with it info, so must be last! */
781 p54_assign_address(dev, skb, hdr, skb->len);
782
Michael Wueff1a592007-09-25 18:11:01 -0700783 priv->tx(dev, hdr, skb->len, 0);
784 return 0;
785}
786
787static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
788 const u8 *dst, const u8 *src, u8 antenna,
789 u32 magic3, u32 magic8, u32 magic9)
790{
791 struct p54_common *priv = dev->priv;
792 struct p54_control_hdr *hdr;
793 struct p54_tx_control_filter *filter;
794
795 hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
Michael Wuba8007c2007-10-13 20:35:05 -0400796 priv->tx_hdr_len, GFP_ATOMIC);
Michael Wueff1a592007-09-25 18:11:01 -0700797 if (!hdr)
798 return -ENOMEM;
799
800 hdr = (void *)hdr + priv->tx_hdr_len;
801
802 filter = (struct p54_tx_control_filter *) hdr->data;
803 hdr->magic1 = cpu_to_le16(0x8001);
804 hdr->len = cpu_to_le16(sizeof(*filter));
Johannes Berge039fa42008-05-15 12:55:29 +0200805 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*filter));
Michael Wueff1a592007-09-25 18:11:01 -0700806 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
807
808 filter->filter_type = cpu_to_le16(filter_type);
809 memcpy(filter->dst, dst, ETH_ALEN);
810 if (!src)
811 memset(filter->src, ~0, ETH_ALEN);
812 else
813 memcpy(filter->src, src, ETH_ALEN);
814 filter->antenna = antenna;
815 filter->magic3 = cpu_to_le32(magic3);
816 filter->rx_addr = cpu_to_le32(priv->rx_end);
Christian Lamparter4e416a62008-09-01 22:48:41 +0200817 filter->max_rx = cpu_to_le16(priv->rx_mtu);
Christian Lamparter7cb77072008-09-01 22:48:51 +0200818 filter->rxhw = cpu_to_le16(priv->rxhw);
Michael Wueff1a592007-09-25 18:11:01 -0700819 filter->magic8 = cpu_to_le16(magic8);
820 filter->magic9 = cpu_to_le16(magic9);
821
822 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*filter), 1);
823 return 0;
824}
825
826static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
827{
828 struct p54_common *priv = dev->priv;
829 struct p54_control_hdr *hdr;
830 struct p54_tx_control_channel *chan;
831 unsigned int i;
Michael Wueff1a592007-09-25 18:11:01 -0700832 void *entry;
833
Christian Lamparter154e3af2008-08-23 22:15:25 +0200834 hdr = kzalloc(sizeof(*hdr) + sizeof(*chan) +
Michael Wueff1a592007-09-25 18:11:01 -0700835 priv->tx_hdr_len, GFP_KERNEL);
836 if (!hdr)
837 return -ENOMEM;
838
839 hdr = (void *)hdr + priv->tx_hdr_len;
840
841 chan = (struct p54_tx_control_channel *) hdr->data;
842
843 hdr->magic1 = cpu_to_le16(0x8001);
844 hdr->len = cpu_to_le16(sizeof(*chan));
845 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
Christian Lamparter154e3af2008-08-23 22:15:25 +0200846 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*chan));
Michael Wueff1a592007-09-25 18:11:01 -0700847
Christian Lamparter154e3af2008-08-23 22:15:25 +0200848 chan->flags = cpu_to_le16(0x1);
849 chan->dwell = cpu_to_le16(0x0);
Michael Wueff1a592007-09-25 18:11:01 -0700850
851 for (i = 0; i < priv->iq_autocal_len; i++) {
852 if (priv->iq_autocal[i].freq != freq)
853 continue;
854
855 memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
856 sizeof(*priv->iq_autocal));
857 break;
858 }
859 if (i == priv->iq_autocal_len)
860 goto err;
861
862 for (i = 0; i < priv->output_limit_len; i++) {
863 if (priv->output_limit[i].freq != freq)
864 continue;
865
866 chan->val_barker = 0x38;
Christian Lamparter154e3af2008-08-23 22:15:25 +0200867 chan->val_bpsk = chan->dup_bpsk =
868 priv->output_limit[i].val_bpsk;
869 chan->val_qpsk = chan->dup_qpsk =
870 priv->output_limit[i].val_qpsk;
871 chan->val_16qam = chan->dup_16qam =
872 priv->output_limit[i].val_16qam;
873 chan->val_64qam = chan->dup_64qam =
874 priv->output_limit[i].val_64qam;
Michael Wueff1a592007-09-25 18:11:01 -0700875 break;
876 }
877 if (i == priv->output_limit_len)
878 goto err;
879
Michael Wueff1a592007-09-25 18:11:01 -0700880 entry = priv->curve_data->data;
881 for (i = 0; i < priv->curve_data->channels; i++) {
882 if (*((__le16 *)entry) != freq) {
883 entry += sizeof(__le16);
Christian Lamparter154e3af2008-08-23 22:15:25 +0200884 entry += sizeof(struct p54_pa_curve_data_sample) *
885 priv->curve_data->points_per_channel;
Michael Wueff1a592007-09-25 18:11:01 -0700886 continue;
887 }
888
889 entry += sizeof(__le16);
Christian Lamparter154e3af2008-08-23 22:15:25 +0200890 chan->pa_points_per_curve =
891 min(priv->curve_data->points_per_channel, (u8) 8);
892
Michael Wueff1a592007-09-25 18:11:01 -0700893 memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
894 chan->pa_points_per_curve);
895 break;
896 }
897
Christian Lamparter154e3af2008-08-23 22:15:25 +0200898 chan->rssical_mul = cpu_to_le16(130);
899 chan->rssical_add = cpu_to_le16(0xfe70); /* -400 */
Michael Wueff1a592007-09-25 18:11:01 -0700900
Christian Lamparter154e3af2008-08-23 22:15:25 +0200901 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*chan), 1);
Michael Wueff1a592007-09-25 18:11:01 -0700902 return 0;
903
904 err:
905 printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
906 kfree(hdr);
907 return -EINVAL;
908}
909
910static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
911{
912 struct p54_common *priv = dev->priv;
913 struct p54_control_hdr *hdr;
914 struct p54_tx_control_led *led;
915
916 hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
917 priv->tx_hdr_len, GFP_KERNEL);
918 if (!hdr)
919 return -ENOMEM;
920
921 hdr = (void *)hdr + priv->tx_hdr_len;
922 hdr->magic1 = cpu_to_le16(0x8001);
923 hdr->len = cpu_to_le16(sizeof(*led));
924 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
Johannes Berge039fa42008-05-15 12:55:29 +0200925 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led));
Michael Wueff1a592007-09-25 18:11:01 -0700926
927 led = (struct p54_tx_control_led *) hdr->data;
928 led->mode = cpu_to_le16(mode);
929 led->led_permanent = cpu_to_le16(link);
930 led->led_temporary = cpu_to_le16(act);
931 led->duration = cpu_to_le16(1000);
932
933 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
934
935 return 0;
936}
937
Johannes Berg3330d7b2008-02-10 16:49:38 +0100938#define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
Michael Wueff1a592007-09-25 18:11:01 -0700939do { \
940 queue.aifs = cpu_to_le16(ai_fs); \
941 queue.cwmin = cpu_to_le16(cw_min); \
942 queue.cwmax = cpu_to_le16(cw_max); \
Johannes Berg3330d7b2008-02-10 16:49:38 +0100943 queue.txop = cpu_to_le16(_txop); \
Michael Wueff1a592007-09-25 18:11:01 -0700944} while(0)
945
946static void p54_init_vdcf(struct ieee80211_hw *dev)
947{
948 struct p54_common *priv = dev->priv;
949 struct p54_control_hdr *hdr;
950 struct p54_tx_control_vdcf *vdcf;
951
952 /* all USB V1 adapters need a extra headroom */
953 hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
954 hdr->magic1 = cpu_to_le16(0x8001);
955 hdr->len = cpu_to_le16(sizeof(*vdcf));
956 hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
957 hdr->req_id = cpu_to_le32(priv->rx_start);
958
959 vdcf = (struct p54_tx_control_vdcf *) hdr->data;
960
Johannes Berg3330d7b2008-02-10 16:49:38 +0100961 P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 47);
962 P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 94);
Christian Lamparter5200e8c2008-02-12 14:02:06 +0100963 P54_SET_QUEUE(vdcf->queue[2], 0x0003, 0x000f, 0x03ff, 0);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100964 P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0);
Michael Wueff1a592007-09-25 18:11:01 -0700965}
966
967static void p54_set_vdcf(struct ieee80211_hw *dev)
968{
969 struct p54_common *priv = dev->priv;
970 struct p54_control_hdr *hdr;
971 struct p54_tx_control_vdcf *vdcf;
972
973 hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
974
Johannes Berge039fa42008-05-15 12:55:29 +0200975 p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf));
Michael Wueff1a592007-09-25 18:11:01 -0700976
977 vdcf = (struct p54_tx_control_vdcf *) hdr->data;
978
979 if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
980 vdcf->slottime = 9;
Christian Lamparter5423b2e2008-08-07 10:22:28 +0200981 vdcf->magic1 = 0x10;
982 vdcf->magic2 = 0x00;
Michael Wueff1a592007-09-25 18:11:01 -0700983 } else {
984 vdcf->slottime = 20;
985 vdcf->magic1 = 0x0a;
986 vdcf->magic2 = 0x06;
987 }
988
989 /* (see prism54/isl_oid.h for further details) */
990 vdcf->frameburst = cpu_to_le16(0);
991
992 priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
993}
994
Johannes Berg4150c572007-09-17 01:29:23 -0400995static int p54_start(struct ieee80211_hw *dev)
Michael Wueff1a592007-09-25 18:11:01 -0700996{
997 struct p54_common *priv = dev->priv;
998 int err;
999
Christian Lamparter69bbc7d2008-08-13 23:41:45 +02001000 if (!priv->cached_vdcf) {
1001 priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf)+
1002 priv->tx_hdr_len + sizeof(struct p54_control_hdr),
1003 GFP_KERNEL);
1004
1005 if (!priv->cached_vdcf)
1006 return -ENOMEM;
1007 }
1008
Johannes Berg4150c572007-09-17 01:29:23 -04001009 err = priv->open(dev);
1010 if (!err)
1011 priv->mode = IEEE80211_IF_TYPE_MNTR;
1012
Christian Lamparter69bbc7d2008-08-13 23:41:45 +02001013 p54_init_vdcf(dev);
1014
Johannes Berg4150c572007-09-17 01:29:23 -04001015 return err;
1016}
1017
1018static void p54_stop(struct ieee80211_hw *dev)
1019{
1020 struct p54_common *priv = dev->priv;
1021 struct sk_buff *skb;
Johannes Berge039fa42008-05-15 12:55:29 +02001022 while ((skb = skb_dequeue(&priv->tx_queue)))
Johannes Berg4150c572007-09-17 01:29:23 -04001023 kfree_skb(skb);
Johannes Berg4150c572007-09-17 01:29:23 -04001024 priv->stop(dev);
Johannes Berga2897552007-09-28 14:01:25 +02001025 priv->mode = IEEE80211_IF_TYPE_INVALID;
Johannes Berg4150c572007-09-17 01:29:23 -04001026}
1027
1028static int p54_add_interface(struct ieee80211_hw *dev,
1029 struct ieee80211_if_init_conf *conf)
1030{
1031 struct p54_common *priv = dev->priv;
1032
1033 if (priv->mode != IEEE80211_IF_TYPE_MNTR)
1034 return -EOPNOTSUPP;
Michael Wueff1a592007-09-25 18:11:01 -07001035
1036 switch (conf->type) {
1037 case IEEE80211_IF_TYPE_STA:
1038 priv->mode = conf->type;
1039 break;
1040 default:
1041 return -EOPNOTSUPP;
1042 }
1043
Johannes Berg4150c572007-09-17 01:29:23 -04001044 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
Michael Wueff1a592007-09-25 18:11:01 -07001045
1046 p54_set_filter(dev, 0, priv->mac_addr, NULL, 0, 1, 0, 0xF642);
1047 p54_set_filter(dev, 0, priv->mac_addr, NULL, 1, 0, 0, 0xF642);
Michael Wueff1a592007-09-25 18:11:01 -07001048
1049 switch (conf->type) {
1050 case IEEE80211_IF_TYPE_STA:
1051 p54_set_filter(dev, 1, priv->mac_addr, NULL, 0, 0x15F, 0x1F4, 0);
1052 break;
Johannes Berg4150c572007-09-17 01:29:23 -04001053 default:
1054 BUG(); /* impossible */
1055 break;
Michael Wueff1a592007-09-25 18:11:01 -07001056 }
1057
1058 p54_set_leds(dev, 1, 0, 0);
1059
1060 return 0;
1061}
1062
1063static void p54_remove_interface(struct ieee80211_hw *dev,
1064 struct ieee80211_if_init_conf *conf)
1065{
1066 struct p54_common *priv = dev->priv;
Johannes Berg4150c572007-09-17 01:29:23 -04001067 priv->mode = IEEE80211_IF_TYPE_MNTR;
1068 memset(priv->mac_addr, 0, ETH_ALEN);
1069 p54_set_filter(dev, 0, priv->mac_addr, NULL, 2, 0, 0, 0);
Michael Wueff1a592007-09-25 18:11:01 -07001070}
1071
1072static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
1073{
1074 int ret;
Larry Finger6041e2a2008-08-03 17:58:36 -05001075 struct p54_common *priv = dev->priv;
Michael Wueff1a592007-09-25 18:11:01 -07001076
Larry Finger6041e2a2008-08-03 17:58:36 -05001077 mutex_lock(&priv->conf_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01001078 ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
Michael Wueff1a592007-09-25 18:11:01 -07001079 p54_set_vdcf(dev);
Larry Finger6041e2a2008-08-03 17:58:36 -05001080 mutex_unlock(&priv->conf_mutex);
Michael Wueff1a592007-09-25 18:11:01 -07001081 return ret;
1082}
1083
Johannes Berg32bfd352007-12-19 01:31:26 +01001084static int p54_config_interface(struct ieee80211_hw *dev,
1085 struct ieee80211_vif *vif,
Michael Wueff1a592007-09-25 18:11:01 -07001086 struct ieee80211_if_conf *conf)
1087{
1088 struct p54_common *priv = dev->priv;
1089
Larry Finger6041e2a2008-08-03 17:58:36 -05001090 mutex_lock(&priv->conf_mutex);
Michael Wueff1a592007-09-25 18:11:01 -07001091 p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 0, 1, 0, 0xF642);
1092 p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 2, 0, 0, 0);
1093 p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
Johannes Berg4150c572007-09-17 01:29:23 -04001094 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
Larry Finger6041e2a2008-08-03 17:58:36 -05001095 mutex_unlock(&priv->conf_mutex);
Michael Wueff1a592007-09-25 18:11:01 -07001096 return 0;
1097}
1098
Johannes Berg4150c572007-09-17 01:29:23 -04001099static void p54_configure_filter(struct ieee80211_hw *dev,
1100 unsigned int changed_flags,
1101 unsigned int *total_flags,
1102 int mc_count, struct dev_mc_list *mclist)
1103{
1104 struct p54_common *priv = dev->priv;
1105
1106 *total_flags &= FIF_BCN_PRBRESP_PROMISC;
1107
1108 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
1109 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
1110 p54_set_filter(dev, 0, priv->mac_addr,
1111 NULL, 2, 0, 0, 0);
1112 else
1113 p54_set_filter(dev, 0, priv->mac_addr,
1114 priv->bssid, 2, 0, 0, 0);
1115 }
1116}
1117
Johannes Berge100bb62008-04-30 18:51:21 +02001118static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
Michael Wueff1a592007-09-25 18:11:01 -07001119 const struct ieee80211_tx_queue_params *params)
1120{
1121 struct p54_common *priv = dev->priv;
1122 struct p54_tx_control_vdcf *vdcf;
1123
1124 vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
1125 ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
1126
John W. Linville3df5ee62008-05-01 17:07:32 -04001127 if ((params) && !(queue > 4)) {
Michael Wueff1a592007-09-25 18:11:01 -07001128 P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
Johannes Berg3330d7b2008-02-10 16:49:38 +01001129 params->cw_min, params->cw_max, params->txop);
Michael Wueff1a592007-09-25 18:11:01 -07001130 } else
1131 return -EINVAL;
1132
1133 p54_set_vdcf(dev);
1134
1135 return 0;
1136}
1137
1138static int p54_get_stats(struct ieee80211_hw *dev,
1139 struct ieee80211_low_level_stats *stats)
1140{
1141 /* TODO */
1142 return 0;
1143}
1144
1145static int p54_get_tx_stats(struct ieee80211_hw *dev,
1146 struct ieee80211_tx_queue_stats *stats)
1147{
1148 struct p54_common *priv = dev->priv;
Michael Wueff1a592007-09-25 18:11:01 -07001149
Chr84df3ed2008-08-24 03:15:16 +02001150 memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
Michael Wueff1a592007-09-25 18:11:01 -07001151
1152 return 0;
1153}
1154
1155static const struct ieee80211_ops p54_ops = {
1156 .tx = p54_tx,
Johannes Berg4150c572007-09-17 01:29:23 -04001157 .start = p54_start,
1158 .stop = p54_stop,
Michael Wueff1a592007-09-25 18:11:01 -07001159 .add_interface = p54_add_interface,
1160 .remove_interface = p54_remove_interface,
1161 .config = p54_config,
1162 .config_interface = p54_config_interface,
Johannes Berg4150c572007-09-17 01:29:23 -04001163 .configure_filter = p54_configure_filter,
Michael Wueff1a592007-09-25 18:11:01 -07001164 .conf_tx = p54_conf_tx,
1165 .get_stats = p54_get_stats,
1166 .get_tx_stats = p54_get_tx_stats
1167};
1168
1169struct ieee80211_hw *p54_init_common(size_t priv_data_len)
1170{
1171 struct ieee80211_hw *dev;
1172 struct p54_common *priv;
Michael Wueff1a592007-09-25 18:11:01 -07001173
1174 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
1175 if (!dev)
1176 return NULL;
1177
1178 priv = dev->priv;
Johannes Berga2897552007-09-28 14:01:25 +02001179 priv->mode = IEEE80211_IF_TYPE_INVALID;
Michael Wueff1a592007-09-25 18:11:01 -07001180 skb_queue_head_init(&priv->tx_queue);
Michael Wueff1a592007-09-25 18:11:01 -07001181 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
Bruno Randolf566bfe52008-05-08 19:15:40 +02001182 IEEE80211_HW_RX_INCLUDES_FCS |
1183 IEEE80211_HW_SIGNAL_UNSPEC;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001184
1185 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1186
Michael Wueff1a592007-09-25 18:11:01 -07001187 dev->channel_change_time = 1000; /* TODO: find actual value */
Bruno Randolf566bfe52008-05-08 19:15:40 +02001188 dev->max_signal = 127;
Michael Wueff1a592007-09-25 18:11:01 -07001189
Chr84df3ed2008-08-24 03:15:16 +02001190 priv->tx_stats[0].limit = 1;
1191 priv->tx_stats[1].limit = 1;
1192 priv->tx_stats[2].limit = 1;
1193 priv->tx_stats[3].limit = 1;
1194 priv->tx_stats[4].limit = 5;
Michael Wueff1a592007-09-25 18:11:01 -07001195 dev->queues = 1;
Michael Wueff1a592007-09-25 18:11:01 -07001196 dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
1197 sizeof(struct p54_tx_control_allocdata);
1198
Larry Finger6041e2a2008-08-03 17:58:36 -05001199 mutex_init(&priv->conf_mutex);
Christian Lamparter7cb77072008-09-01 22:48:51 +02001200 init_completion(&priv->eeprom_comp);
Michael Wueff1a592007-09-25 18:11:01 -07001201
Michael Wueff1a592007-09-25 18:11:01 -07001202 return dev;
1203}
1204EXPORT_SYMBOL_GPL(p54_init_common);
1205
1206void p54_free_common(struct ieee80211_hw *dev)
1207{
1208 struct p54_common *priv = dev->priv;
1209 kfree(priv->iq_autocal);
1210 kfree(priv->output_limit);
1211 kfree(priv->curve_data);
1212 kfree(priv->cached_vdcf);
1213}
1214EXPORT_SYMBOL_GPL(p54_free_common);
1215
1216static int __init p54_init(void)
1217{
1218 return 0;
1219}
1220
1221static void __exit p54_exit(void)
1222{
1223}
1224
1225module_init(p54_init);
1226module_exit(p54_exit);