blob: de92b58d4508e775a6a59e9816b719153be4aaaa [file] [log] [blame]
Michael Wueff1a592007-09-25 18:11:01 -07001/*
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>
Johannes Bergc12abae2008-10-14 16:56:51 +02006 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
Michael Wueff1a592007-09-25 18:11:01 -07007 *
John W. Linville27df6052008-10-22 16:41:55 -04008 * 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
Chr94834072008-10-29 22:39:50 +010012 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
Michael Wueff1a592007-09-25 18:11:01 -070013 *
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
28MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
29MODULE_DESCRIPTION("Softmac Prism54 common code");
30MODULE_LICENSE("GPL");
31MODULE_ALIAS("prism54common");
32
Christian Lamparter1b997532008-09-06 14:25:58 +020033static struct ieee80211_rate p54_bgrates[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010034 { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
35 { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
36 { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
37 { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
38 { .bitrate = 60, .hw_value = 4, },
39 { .bitrate = 90, .hw_value = 5, },
40 { .bitrate = 120, .hw_value = 6, },
41 { .bitrate = 180, .hw_value = 7, },
42 { .bitrate = 240, .hw_value = 8, },
43 { .bitrate = 360, .hw_value = 9, },
44 { .bitrate = 480, .hw_value = 10, },
45 { .bitrate = 540, .hw_value = 11, },
46};
47
Christian Lamparter1b997532008-09-06 14:25:58 +020048static struct ieee80211_channel p54_bgchannels[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010049 { .center_freq = 2412, .hw_value = 1, },
50 { .center_freq = 2417, .hw_value = 2, },
51 { .center_freq = 2422, .hw_value = 3, },
52 { .center_freq = 2427, .hw_value = 4, },
53 { .center_freq = 2432, .hw_value = 5, },
54 { .center_freq = 2437, .hw_value = 6, },
55 { .center_freq = 2442, .hw_value = 7, },
56 { .center_freq = 2447, .hw_value = 8, },
57 { .center_freq = 2452, .hw_value = 9, },
58 { .center_freq = 2457, .hw_value = 10, },
59 { .center_freq = 2462, .hw_value = 11, },
60 { .center_freq = 2467, .hw_value = 12, },
61 { .center_freq = 2472, .hw_value = 13, },
62 { .center_freq = 2484, .hw_value = 14, },
63};
64
Johannes Bergc2976ab2008-02-20 12:08:12 +010065static struct ieee80211_supported_band band_2GHz = {
Christian Lamparter1b997532008-09-06 14:25:58 +020066 .channels = p54_bgchannels,
67 .n_channels = ARRAY_SIZE(p54_bgchannels),
68 .bitrates = p54_bgrates,
69 .n_bitrates = ARRAY_SIZE(p54_bgrates),
70};
71
72static struct ieee80211_rate p54_arates[] = {
73 { .bitrate = 60, .hw_value = 4, },
74 { .bitrate = 90, .hw_value = 5, },
75 { .bitrate = 120, .hw_value = 6, },
76 { .bitrate = 180, .hw_value = 7, },
77 { .bitrate = 240, .hw_value = 8, },
78 { .bitrate = 360, .hw_value = 9, },
79 { .bitrate = 480, .hw_value = 10, },
80 { .bitrate = 540, .hw_value = 11, },
81};
82
83static struct ieee80211_channel p54_achannels[] = {
84 { .center_freq = 4920 },
85 { .center_freq = 4940 },
86 { .center_freq = 4960 },
87 { .center_freq = 4980 },
88 { .center_freq = 5040 },
89 { .center_freq = 5060 },
90 { .center_freq = 5080 },
91 { .center_freq = 5170 },
92 { .center_freq = 5180 },
93 { .center_freq = 5190 },
94 { .center_freq = 5200 },
95 { .center_freq = 5210 },
96 { .center_freq = 5220 },
97 { .center_freq = 5230 },
98 { .center_freq = 5240 },
99 { .center_freq = 5260 },
100 { .center_freq = 5280 },
101 { .center_freq = 5300 },
102 { .center_freq = 5320 },
103 { .center_freq = 5500 },
104 { .center_freq = 5520 },
105 { .center_freq = 5540 },
106 { .center_freq = 5560 },
107 { .center_freq = 5580 },
108 { .center_freq = 5600 },
109 { .center_freq = 5620 },
110 { .center_freq = 5640 },
111 { .center_freq = 5660 },
112 { .center_freq = 5680 },
113 { .center_freq = 5700 },
114 { .center_freq = 5745 },
115 { .center_freq = 5765 },
116 { .center_freq = 5785 },
117 { .center_freq = 5805 },
118 { .center_freq = 5825 },
119};
120
121static struct ieee80211_supported_band band_5GHz = {
122 .channels = p54_achannels,
123 .n_channels = ARRAY_SIZE(p54_achannels),
124 .bitrates = p54_arates,
125 .n_bitrates = ARRAY_SIZE(p54_arates),
Johannes Berg8318d782008-01-24 19:38:38 +0100126};
127
Christian Lamparter4e416a62008-09-01 22:48:41 +0200128int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
Michael Wueff1a592007-09-25 18:11:01 -0700129{
130 struct p54_common *priv = dev->priv;
131 struct bootrec_exp_if *exp_if;
132 struct bootrec *bootrec;
133 u32 *data = (u32 *)fw->data;
134 u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
135 u8 *fw_version = NULL;
136 size_t len;
137 int i;
138
139 if (priv->rx_start)
Christian Lamparter4e416a62008-09-01 22:48:41 +0200140 return 0;
Michael Wueff1a592007-09-25 18:11:01 -0700141
142 while (data < end_data && *data)
143 data++;
144
145 while (data < end_data && !*data)
146 data++;
147
148 bootrec = (struct bootrec *) data;
149
150 while (bootrec->data <= end_data &&
151 (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
152 u32 code = le32_to_cpu(bootrec->code);
153 switch (code) {
154 case BR_CODE_COMPONENT_ID:
Larry Finger1f1c0e32008-09-25 14:54:28 -0500155 priv->fw_interface = be32_to_cpup((__be32 *)
156 bootrec->data);
Christian Lamparter2b808482008-09-04 12:29:38 +0200157 switch (priv->fw_interface) {
Michael Wueff1a592007-09-25 18:11:01 -0700158 case FW_FMAC:
159 printk(KERN_INFO "p54: FreeMAC firmware\n");
160 break;
161 case FW_LM20:
162 printk(KERN_INFO "p54: LM20 firmware\n");
163 break;
164 case FW_LM86:
165 printk(KERN_INFO "p54: LM86 firmware\n");
166 break;
167 case FW_LM87:
Christian Lamparter2b808482008-09-04 12:29:38 +0200168 printk(KERN_INFO "p54: LM87 firmware\n");
Michael Wueff1a592007-09-25 18:11:01 -0700169 break;
170 default:
171 printk(KERN_INFO "p54: unknown firmware\n");
172 break;
173 }
174 break;
175 case BR_CODE_COMPONENT_VERSION:
176 /* 24 bytes should be enough for all firmwares */
177 if (strnlen((unsigned char*)bootrec->data, 24) < 24)
178 fw_version = (unsigned char*)bootrec->data;
179 break;
Christian Lamparter4e416a62008-09-01 22:48:41 +0200180 case BR_CODE_DESCR: {
181 struct bootrec_desc *desc =
182 (struct bootrec_desc *)bootrec->data;
183 priv->rx_start = le32_to_cpu(desc->rx_start);
Michael Wueff1a592007-09-25 18:11:01 -0700184 /* FIXME add sanity checking */
Christian Lamparter4e416a62008-09-01 22:48:41 +0200185 priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
186 priv->headroom = desc->headroom;
187 priv->tailroom = desc->tailroom;
Larry Finger1f1c0e32008-09-25 14:54:28 -0500188 if (le32_to_cpu(bootrec->len) == 11)
Larry Finger2e20cc32008-10-09 17:38:52 -0700189 priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
Christian Lamparter4e416a62008-09-01 22:48:41 +0200190 else
191 priv->rx_mtu = (size_t)
192 0x620 - priv->tx_hdr_len;
Michael Wueff1a592007-09-25 18:11:01 -0700193 break;
Christian Lamparter4e416a62008-09-01 22:48:41 +0200194 }
Michael Wueff1a592007-09-25 18:11:01 -0700195 case BR_CODE_EXPOSED_IF:
196 exp_if = (struct bootrec_exp_if *) bootrec->data;
197 for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
Al Virodc73c622007-12-21 23:49:02 -0500198 if (exp_if[i].if_id == cpu_to_le16(0x1a))
Michael Wueff1a592007-09-25 18:11:01 -0700199 priv->fw_var = le16_to_cpu(exp_if[i].variant);
200 break;
201 case BR_CODE_DEPENDENT_IF:
202 break;
203 case BR_CODE_END_OF_BRA:
204 case LEGACY_BR_CODE_END_OF_BRA:
205 end_data = NULL;
206 break;
207 default:
208 break;
209 }
210 bootrec = (struct bootrec *)&bootrec->data[len];
211 }
212
213 if (fw_version)
214 printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
215 fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
216
Christian Lamparter9a8675d2008-10-18 23:04:15 +0200217 if (priv->fw_var < 0x500)
218 printk(KERN_INFO "p54: you are using an obsolete firmware. "
219 "visit http://wireless.kernel.org/en/users/Drivers/p54 "
220 "and grab one for \"kernel >= 2.6.28\"!\n");
221
Michael Wueff1a592007-09-25 18:11:01 -0700222 if (priv->fw_var >= 0x300) {
223 /* Firmware supports QoS, use it! */
Christian Lamparter9e7f3f82008-10-18 23:18:01 +0200224 priv->tx_stats[4].limit = 3; /* AC_VO */
225 priv->tx_stats[5].limit = 4; /* AC_VI */
226 priv->tx_stats[6].limit = 3; /* AC_BE */
227 priv->tx_stats[7].limit = 2; /* AC_BK */
Michael Wueff1a592007-09-25 18:11:01 -0700228 dev->queues = 4;
229 }
Christian Lamparter4e416a62008-09-01 22:48:41 +0200230
231 return 0;
Michael Wueff1a592007-09-25 18:11:01 -0700232}
233EXPORT_SYMBOL_GPL(p54_parse_firmware);
234
Christian Lamparter154e3af2008-08-23 22:15:25 +0200235static int p54_convert_rev0(struct ieee80211_hw *dev,
236 struct pda_pa_curve_data *curve_data)
Michael Wueff1a592007-09-25 18:11:01 -0700237{
238 struct p54_common *priv = dev->priv;
Christian Lamparter154e3af2008-08-23 22:15:25 +0200239 struct p54_pa_curve_data_sample *dst;
240 struct pda_pa_curve_data_sample_rev0 *src;
Michael Wueff1a592007-09-25 18:11:01 -0700241 size_t cd_len = sizeof(*curve_data) +
Christian Lamparter154e3af2008-08-23 22:15:25 +0200242 (curve_data->points_per_channel*sizeof(*dst) + 2) *
Michael Wueff1a592007-09-25 18:11:01 -0700243 curve_data->channels;
244 unsigned int i, j;
245 void *source, *target;
246
247 priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
248 if (!priv->curve_data)
249 return -ENOMEM;
250
251 memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
252 source = curve_data->data;
253 target = priv->curve_data->data;
254 for (i = 0; i < curve_data->channels; i++) {
255 __le16 *freq = source;
256 source += sizeof(__le16);
257 *((__le16 *)target) = *freq;
258 target += sizeof(__le16);
259 for (j = 0; j < curve_data->points_per_channel; j++) {
Christian Lamparter154e3af2008-08-23 22:15:25 +0200260 dst = target;
261 src = source;
Michael Wueff1a592007-09-25 18:11:01 -0700262
Christian Lamparter154e3af2008-08-23 22:15:25 +0200263 dst->rf_power = src->rf_power;
264 dst->pa_detector = src->pa_detector;
265 dst->data_64qam = src->pcv;
Michael Wueff1a592007-09-25 18:11:01 -0700266 /* "invent" the points for the other modulations */
267#define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
Christian Lamparter154e3af2008-08-23 22:15:25 +0200268 dst->data_16qam = SUB(src->pcv, 12);
269 dst->data_qpsk = SUB(dst->data_16qam, 12);
270 dst->data_bpsk = SUB(dst->data_qpsk, 12);
271 dst->data_barker = SUB(dst->data_bpsk, 14);
Michael Wueff1a592007-09-25 18:11:01 -0700272#undef SUB
Christian Lamparter154e3af2008-08-23 22:15:25 +0200273 target += sizeof(*dst);
274 source += sizeof(*src);
Michael Wueff1a592007-09-25 18:11:01 -0700275 }
276 }
277
278 return 0;
279}
280
Christian Lamparter154e3af2008-08-23 22:15:25 +0200281static int p54_convert_rev1(struct ieee80211_hw *dev,
282 struct pda_pa_curve_data *curve_data)
283{
284 struct p54_common *priv = dev->priv;
285 struct p54_pa_curve_data_sample *dst;
286 struct pda_pa_curve_data_sample_rev1 *src;
287 size_t cd_len = sizeof(*curve_data) +
288 (curve_data->points_per_channel*sizeof(*dst) + 2) *
289 curve_data->channels;
290 unsigned int i, j;
291 void *source, *target;
292
293 priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
294 if (!priv->curve_data)
295 return -ENOMEM;
296
297 memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
298 source = curve_data->data;
299 target = priv->curve_data->data;
300 for (i = 0; i < curve_data->channels; i++) {
301 __le16 *freq = source;
302 source += sizeof(__le16);
303 *((__le16 *)target) = *freq;
304 target += sizeof(__le16);
305 for (j = 0; j < curve_data->points_per_channel; j++) {
306 memcpy(target, source, sizeof(*src));
307
308 target += sizeof(*dst);
309 source += sizeof(*src);
310 }
311 source++;
312 }
313
314 return 0;
315}
316
Christian Lamparter4cc683c2008-10-15 03:30:06 +0200317static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
318 "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
Christian Lamparter1b997532008-09-06 14:25:58 +0200319static int p54_init_xbow_synth(struct ieee80211_hw *dev);
Christian Lamparter7cb77072008-09-01 22:48:51 +0200320
Larry Finger1f1c0e32008-09-25 14:54:28 -0500321static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
Michael Wueff1a592007-09-25 18:11:01 -0700322{
323 struct p54_common *priv = dev->priv;
324 struct eeprom_pda_wrap *wrap = NULL;
325 struct pda_entry *entry;
Michael Wueff1a592007-09-25 18:11:01 -0700326 unsigned int data_len, entry_len;
327 void *tmp;
328 int err;
Johannes Bergc2f2d3a2008-02-29 23:28:25 +0100329 u8 *end = (u8 *)eeprom + len;
Christian Lamparterf2c2e252008-10-25 16:14:14 +0200330 u16 synth = 0;
Michael Wueff1a592007-09-25 18:11:01 -0700331
332 wrap = (struct eeprom_pda_wrap *) eeprom;
Johannes Berg8c282932008-02-29 13:56:33 +0100333 entry = (void *)wrap->data + le16_to_cpu(wrap->len);
Johannes Bergc2f2d3a2008-02-29 23:28:25 +0100334
335 /* verify that at least the entry length/code fits */
336 while ((u8 *)entry <= end - sizeof(*entry)) {
Michael Wueff1a592007-09-25 18:11:01 -0700337 entry_len = le16_to_cpu(entry->len);
338 data_len = ((entry_len - 1) << 1);
Johannes Bergc2f2d3a2008-02-29 23:28:25 +0100339
340 /* abort if entry exceeds whole structure */
341 if ((u8 *)entry + sizeof(*entry) + data_len > end)
342 break;
343
Michael Wueff1a592007-09-25 18:11:01 -0700344 switch (le16_to_cpu(entry->code)) {
345 case PDR_MAC_ADDRESS:
346 SET_IEEE80211_PERM_ADDR(dev, entry->data);
347 break;
348 case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
349 if (data_len < 2) {
350 err = -EINVAL;
351 goto err;
352 }
353
354 if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
355 err = -EINVAL;
356 goto err;
357 }
358
359 priv->output_limit = kmalloc(entry->data[1] *
360 sizeof(*priv->output_limit), GFP_KERNEL);
361
362 if (!priv->output_limit) {
363 err = -ENOMEM;
364 goto err;
365 }
366
367 memcpy(priv->output_limit, &entry->data[2],
368 entry->data[1]*sizeof(*priv->output_limit));
369 priv->output_limit_len = entry->data[1];
370 break;
Christian Lamparter154e3af2008-08-23 22:15:25 +0200371 case PDR_PRISM_PA_CAL_CURVE_DATA: {
372 struct pda_pa_curve_data *curve_data =
373 (struct pda_pa_curve_data *)entry->data;
374 if (data_len < sizeof(*curve_data)) {
Michael Wueff1a592007-09-25 18:11:01 -0700375 err = -EINVAL;
376 goto err;
377 }
378
Christian Lamparter154e3af2008-08-23 22:15:25 +0200379 switch (curve_data->cal_method_rev) {
380 case 0:
381 err = p54_convert_rev0(dev, curve_data);
382 break;
383 case 1:
384 err = p54_convert_rev1(dev, curve_data);
385 break;
386 default:
387 printk(KERN_ERR "p54: unknown curve data "
388 "revision %d\n",
389 curve_data->cal_method_rev);
390 err = -ENODEV;
391 break;
Michael Wueff1a592007-09-25 18:11:01 -0700392 }
Christian Lamparter154e3af2008-08-23 22:15:25 +0200393 if (err)
394 goto err;
Michael Wueff1a592007-09-25 18:11:01 -0700395
Christian Lamparter154e3af2008-08-23 22:15:25 +0200396 }
Michael Wueff1a592007-09-25 18:11:01 -0700397 case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
398 priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
399 if (!priv->iq_autocal) {
400 err = -ENOMEM;
401 goto err;
402 }
403
404 memcpy(priv->iq_autocal, entry->data, data_len);
405 priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
406 break;
407 case PDR_INTERFACE_LIST:
408 tmp = entry->data;
409 while ((u8 *)tmp < entry->data + data_len) {
410 struct bootrec_exp_if *exp_if = tmp;
Christian Lamparter4cc683c2008-10-15 03:30:06 +0200411 if (le16_to_cpu(exp_if->if_id) == 0xf)
412 synth = le16_to_cpu(exp_if->variant);
Michael Wueff1a592007-09-25 18:11:01 -0700413 tmp += sizeof(struct bootrec_exp_if);
414 }
415 break;
416 case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
417 priv->version = *(u8 *)(entry->data + 1);
418 break;
419 case PDR_END:
Johannes Bergc2f2d3a2008-02-29 23:28:25 +0100420 /* make it overrun */
421 entry_len = len;
Michael Wueff1a592007-09-25 18:11:01 -0700422 break;
Pavel Roskinc8034c42008-10-29 17:31:43 -0400423 case PDR_MANUFACTURING_PART_NUMBER:
424 case PDR_PDA_VERSION:
425 case PDR_NIC_SERIAL_NUMBER:
426 case PDR_REGULATORY_DOMAIN_LIST:
427 case PDR_TEMPERATURE_TYPE:
428 case PDR_PRISM_PCI_IDENTIFIER:
429 case PDR_COUNTRY_INFORMATION:
430 case PDR_OEM_NAME:
431 case PDR_PRODUCT_NAME:
432 case PDR_UTF8_OEM_NAME:
433 case PDR_UTF8_PRODUCT_NAME:
434 case PDR_COUNTRY_LIST:
435 case PDR_DEFAULT_COUNTRY:
436 case PDR_ANTENNA_GAIN:
437 case PDR_PRISM_INDIGO_PA_CALIBRATION_DATA:
438 case PDR_RSSI_LINEAR_APPROXIMATION:
439 case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
440 case PDR_REGULATORY_POWER_LIMITS:
441 case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
442 case PDR_RADIATED_TRANSMISSION_CORRECTION:
443 case PDR_PRISM_TX_IQ_CALIBRATION:
444 case PDR_BASEBAND_REGISTERS:
445 case PDR_PER_CHANNEL_BASEBAND_REGISTERS:
446 break;
Florian Fainelli58e30732008-02-25 17:51:53 +0100447 default:
448 printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
449 le16_to_cpu(entry->code));
450 break;
Michael Wueff1a592007-09-25 18:11:01 -0700451 }
452
453 entry = (void *)entry + (entry_len + 1)*2;
Michael Wueff1a592007-09-25 18:11:01 -0700454 }
455
Christian Lamparterf2c2e252008-10-25 16:14:14 +0200456 if (!synth || !priv->iq_autocal || !priv->output_limit ||
457 !priv->curve_data) {
Michael Wueff1a592007-09-25 18:11:01 -0700458 printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
459 err = -EINVAL;
460 goto err;
461 }
462
Christian Lamparter9e7f3f82008-10-18 23:18:01 +0200463 priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
Christian Lamparter4cc683c2008-10-15 03:30:06 +0200464 if (priv->rxhw == 4)
Christian Lamparter1b997532008-09-06 14:25:58 +0200465 p54_init_xbow_synth(dev);
Christian Lamparter9e7f3f82008-10-18 23:18:01 +0200466 if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
Christian Lamparter1b997532008-09-06 14:25:58 +0200467 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +0200468 if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
Christian Lamparter4cc683c2008-10-15 03:30:06 +0200469 dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200470
471 if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
472 u8 perm_addr[ETH_ALEN];
473
474 printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
475 wiphy_name(dev->wiphy));
476 random_ether_addr(perm_addr);
477 SET_IEEE80211_PERM_ADDR(dev, perm_addr);
478 }
479
Johannes Berge1749612008-10-27 15:59:26 -0700480 printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
Christian Lamparter7cb77072008-09-01 22:48:51 +0200481 wiphy_name(dev->wiphy),
Johannes Berge1749612008-10-27 15:59:26 -0700482 dev->wiphy->perm_addr,
Christian Lamparter7cb77072008-09-01 22:48:51 +0200483 priv->version, p54_rf_chips[priv->rxhw]);
484
Michael Wueff1a592007-09-25 18:11:01 -0700485 return 0;
486
487 err:
488 if (priv->iq_autocal) {
489 kfree(priv->iq_autocal);
490 priv->iq_autocal = NULL;
491 }
492
493 if (priv->output_limit) {
494 kfree(priv->output_limit);
495 priv->output_limit = NULL;
496 }
497
498 if (priv->curve_data) {
499 kfree(priv->curve_data);
500 priv->curve_data = NULL;
501 }
502
503 printk(KERN_ERR "p54: eeprom parse failed!\n");
504 return err;
505}
Michael Wueff1a592007-09-25 18:11:01 -0700506
Christian Lampartercc6de662008-09-06 02:56:23 +0200507static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
508{
509 /* TODO: get the rssi_add & rssi_mul data from the eeprom */
510 return ((rssi * 0x83) / 64 - 400) / 4;
511}
512
Christian Lamparter19c19d52008-09-03 22:25:25 +0200513static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
Michael Wueff1a592007-09-25 18:11:01 -0700514{
Christian Lampartera0db6632008-09-06 02:56:04 +0200515 struct p54_common *priv = dev->priv;
John W. Linville27df6052008-10-22 16:41:55 -0400516 struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
Michael Wueff1a592007-09-25 18:11:01 -0700517 struct ieee80211_rx_status rx_status = {0};
518 u16 freq = le16_to_cpu(hdr->freq);
Christian Lamparter19c19d52008-09-03 22:25:25 +0200519 size_t header_len = sizeof(*hdr);
Christian Lampartera0db6632008-09-06 02:56:04 +0200520 u32 tsf32;
Michael Wueff1a592007-09-25 18:11:01 -0700521
John W. Linville27df6052008-10-22 16:41:55 -0400522 if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) {
Christian Lamparter78d57eb2008-09-06 02:56:12 +0200523 if (priv->filter_flags & FIF_FCSFAIL)
524 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
525 else
526 return 0;
527 }
528
Christian Lampartercc6de662008-09-06 02:56:23 +0200529 rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
530 rx_status.noise = priv->noise;
Johannes Berg8318d782008-01-24 19:38:38 +0100531 /* XX correct? */
Larry.Finger@lwfinger.net18d72602008-06-30 10:39:49 -0500532 rx_status.qual = (100 * hdr->rssi) / 127;
Christian Lamparterffed7852008-11-14 19:41:22 +0100533 if (hdr->rate & 0x10)
534 rx_status.flag |= RX_FLAG_SHORTPRE;
Christian Lampartercf3e74c2008-09-30 23:36:00 +0200535 rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
536 hdr->rate : (hdr->rate - 4)) & 0xf;
Michael Wueff1a592007-09-25 18:11:01 -0700537 rx_status.freq = freq;
Christian Lampartercf3e74c2008-09-30 23:36:00 +0200538 rx_status.band = dev->conf.channel->band;
Michael Wueff1a592007-09-25 18:11:01 -0700539 rx_status.antenna = hdr->antenna;
Christian Lampartera0db6632008-09-06 02:56:04 +0200540
541 tsf32 = le32_to_cpu(hdr->tsf32);
542 if (tsf32 < priv->tsf_low32)
543 priv->tsf_high32++;
544 rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
545 priv->tsf_low32 = tsf32;
546
Johannes Berg03bffc12007-12-04 20:33:40 +0100547 rx_status.flag |= RX_FLAG_TSFT;
Michael Wueff1a592007-09-25 18:11:01 -0700548
John W. Linville27df6052008-10-22 16:41:55 -0400549 if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
Christian Lamparter19c19d52008-09-03 22:25:25 +0200550 header_len += hdr->align[0];
551
552 skb_pull(skb, header_len);
Michael Wueff1a592007-09-25 18:11:01 -0700553 skb_trim(skb, le16_to_cpu(hdr->len));
554
555 ieee80211_rx_irqsafe(dev, skb, &rx_status);
Christian Lamparter19c19d52008-09-03 22:25:25 +0200556
557 return -1;
Michael Wueff1a592007-09-25 18:11:01 -0700558}
559
560static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
561{
562 struct p54_common *priv = dev->priv;
563 int i;
564
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200565 if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
566 return ;
567
Michael Wueff1a592007-09-25 18:11:01 -0700568 for (i = 0; i < dev->queues; i++)
Chr84df3ed2008-08-24 03:15:16 +0200569 if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
Michael Wueff1a592007-09-25 18:11:01 -0700570 ieee80211_wake_queue(dev, i);
571}
572
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200573void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
574{
575 struct p54_common *priv = dev->priv;
576 struct ieee80211_tx_info *info;
577 struct memrecord *range;
578 unsigned long flags;
579 u32 freed = 0, last_addr = priv->rx_start;
580
Christian Lamparterffed7852008-11-14 19:41:22 +0100581 if (unlikely(!skb || !dev || !skb_queue_len(&priv->tx_queue)))
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200582 return;
583
584 spin_lock_irqsave(&priv->tx_queue.lock, flags);
585 info = IEEE80211_SKB_CB(skb);
586 range = (void *)info->rate_driver_data;
587 if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
588 struct ieee80211_tx_info *ni;
589 struct memrecord *mr;
590
591 ni = IEEE80211_SKB_CB(skb->prev);
592 mr = (struct memrecord *)ni->rate_driver_data;
593 last_addr = mr->end_addr;
594 }
595 if (skb->next != (struct sk_buff *)&priv->tx_queue) {
596 struct ieee80211_tx_info *ni;
597 struct memrecord *mr;
598
599 ni = IEEE80211_SKB_CB(skb->next);
600 mr = (struct memrecord *)ni->rate_driver_data;
601 freed = mr->start_addr - last_addr;
602 } else
603 freed = priv->rx_end - last_addr;
604 __skb_unlink(skb, &priv->tx_queue);
605 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
606 kfree_skb(skb);
607
John W. Linville27df6052008-10-22 16:41:55 -0400608 if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200609 IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
610 p54_wake_free_queues(dev);
611}
612EXPORT_SYMBOL_GPL(p54_free_skb);
613
Michael Wueff1a592007-09-25 18:11:01 -0700614static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
615{
616 struct p54_common *priv = dev->priv;
John W. Linville27df6052008-10-22 16:41:55 -0400617 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
618 struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
Michael Wueff1a592007-09-25 18:11:01 -0700619 struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
Christian Lamparter4e416a62008-09-01 22:48:41 +0200620 u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
Michael Wueff1a592007-09-25 18:11:01 -0700621 struct memrecord *range = NULL;
622 u32 freed = 0;
623 u32 last_addr = priv->rx_start;
Chr031d10e2008-08-24 03:15:06 +0200624 unsigned long flags;
Johannes Bergc12abae2008-10-14 16:56:51 +0200625 int count, idx;
Michael Wueff1a592007-09-25 18:11:01 -0700626
Chr031d10e2008-08-24 03:15:06 +0200627 spin_lock_irqsave(&priv->tx_queue.lock, flags);
Michael Wueff1a592007-09-25 18:11:01 -0700628 while (entry != (struct sk_buff *)&priv->tx_queue) {
Johannes Berg552fe532008-05-30 21:07:15 +0200629 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
John W. Linville27df6052008-10-22 16:41:55 -0400630 struct p54_hdr *entry_hdr;
631 struct p54_tx_data *entry_data;
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200632 int pad = 0;
633
Johannes Berge6a98542008-10-21 12:40:02 +0200634 range = (void *)info->rate_driver_data;
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200635 if (range->start_addr != addr) {
Michael Wueff1a592007-09-25 18:11:01 -0700636 last_addr = range->end_addr;
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200637 entry = entry->next;
638 continue;
639 }
Chr031d10e2008-08-24 03:15:06 +0200640
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200641 if (entry->next != (struct sk_buff *)&priv->tx_queue) {
642 struct ieee80211_tx_info *ni;
643 struct memrecord *mr;
Johannes Bergc12abae2008-10-14 16:56:51 +0200644
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200645 ni = IEEE80211_SKB_CB(entry->next);
646 mr = (struct memrecord *)ni->rate_driver_data;
647 freed = mr->start_addr - last_addr;
Michael Wueff1a592007-09-25 18:11:01 -0700648 } else
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200649 freed = priv->rx_end - last_addr;
650
651 last_addr = range->end_addr;
652 __skb_unlink(entry, &priv->tx_queue);
653 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
654
Christian Lamparterc772a082008-11-29 22:33:57 +0100655 entry_hdr = (struct p54_hdr *) entry->data;
656 entry_data = (struct p54_tx_data *) entry_hdr->data;
657 priv->tx_stats[entry_data->hw_queue].len--;
658
Christian Lampartere5ea92a2008-10-18 23:19:00 +0200659 if (unlikely(entry == priv->cached_beacon)) {
660 kfree_skb(entry);
661 priv->cached_beacon = NULL;
662 goto out;
663 }
664
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200665 /*
666 * Clear manually, ieee80211_tx_info_clear_status would
667 * clear the counts too and we need them.
668 */
669 memset(&info->status.ampdu_ack_len, 0,
670 sizeof(struct ieee80211_tx_info) -
671 offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
672 BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
673 status.ampdu_ack_len) != 23);
674
John W. Linville27df6052008-10-22 16:41:55 -0400675 if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200676 pad = entry_data->align[0];
677
678 /* walk through the rates array and adjust the counts */
John W. Linville27df6052008-10-22 16:41:55 -0400679 count = payload->tries;
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200680 for (idx = 0; idx < 4; idx++) {
681 if (count >= info->status.rates[idx].count) {
682 count -= info->status.rates[idx].count;
683 } else if (count > 0) {
684 info->status.rates[idx].count = count;
685 count = 0;
686 } else {
687 info->status.rates[idx].idx = -1;
688 info->status.rates[idx].count = 0;
689 }
690 }
691
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200692 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
693 (!payload->status))
694 info->flags |= IEEE80211_TX_STAT_ACK;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +0200695 if (payload->status & P54_TX_PSM_CANCELLED)
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200696 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
697 info->status.ack_signal = p54_rssi_to_dbm(dev,
John W. Linville27df6052008-10-22 16:41:55 -0400698 (int)payload->ack_rssi);
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200699 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
700 ieee80211_tx_status_irqsafe(dev, entry);
701 goto out;
Michael Wueff1a592007-09-25 18:11:01 -0700702 }
Chr031d10e2008-08-24 03:15:06 +0200703 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
Michael Wueff1a592007-09-25 18:11:01 -0700704
Chr031d10e2008-08-24 03:15:06 +0200705out:
John W. Linville27df6052008-10-22 16:41:55 -0400706 if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
Christian Lamparter9de5776f2008-10-15 04:05:51 +0200707 IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
Michael Wueff1a592007-09-25 18:11:01 -0700708 p54_wake_free_queues(dev);
709}
710
Christian Lamparter7cb77072008-09-01 22:48:51 +0200711static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
712 struct sk_buff *skb)
713{
John W. Linville27df6052008-10-22 16:41:55 -0400714 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200715 struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
716 struct p54_common *priv = dev->priv;
717
718 if (!priv->eeprom)
719 return ;
720
Larry Finger1f1c0e32008-09-25 14:54:28 -0500721 memcpy(priv->eeprom, eeprom->data, le16_to_cpu(eeprom->len));
Christian Lamparter7cb77072008-09-01 22:48:51 +0200722
723 complete(&priv->eeprom_comp);
724}
725
Christian Lampartercc6de662008-09-06 02:56:23 +0200726static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
727{
728 struct p54_common *priv = dev->priv;
John W. Linville27df6052008-10-22 16:41:55 -0400729 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
Christian Lampartercc6de662008-09-06 02:56:23 +0200730 struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
731 u32 tsf32 = le32_to_cpu(stats->tsf32);
732
733 if (tsf32 < priv->tsf_low32)
734 priv->tsf_high32++;
735 priv->tsf_low32 = tsf32;
736
737 priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
738 priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
739 priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
740
741 priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
742 complete(&priv->stats_comp);
743
744 mod_timer(&priv->stats_timer, jiffies + 5 * HZ);
745}
746
Christian Lampartere5ea92a2008-10-18 23:19:00 +0200747static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
748{
749 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
750 struct p54_trap *trap = (struct p54_trap *) hdr->data;
751 u16 event = le16_to_cpu(trap->event);
752 u16 freq = le16_to_cpu(trap->frequency);
753
754 switch (event) {
755 case P54_TRAP_BEACON_TX:
756 break;
757 case P54_TRAP_RADAR:
758 printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
759 wiphy_name(dev->wiphy), freq);
760 break;
761 case P54_TRAP_NO_BEACON:
762 break;
763 case P54_TRAP_SCAN:
764 break;
765 case P54_TRAP_TBTT:
766 break;
767 case P54_TRAP_TIMER:
768 break;
769 default:
770 printk(KERN_INFO "%s: received event:%x freq:%d\n",
771 wiphy_name(dev->wiphy), event, freq);
772 break;
773 }
774}
775
Christian Lamparter19c19d52008-09-03 22:25:25 +0200776static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
Michael Wueff1a592007-09-25 18:11:01 -0700777{
John W. Linville27df6052008-10-22 16:41:55 -0400778 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
Michael Wueff1a592007-09-25 18:11:01 -0700779
780 switch (le16_to_cpu(hdr->type)) {
781 case P54_CONTROL_TYPE_TXDONE:
782 p54_rx_frame_sent(dev, skb);
783 break;
Christian Lampartere5ea92a2008-10-18 23:19:00 +0200784 case P54_CONTROL_TYPE_TRAP:
785 p54_rx_trap(dev, skb);
786 break;
Michael Wueff1a592007-09-25 18:11:01 -0700787 case P54_CONTROL_TYPE_BBP:
788 break;
Christian Lampartercc6de662008-09-06 02:56:23 +0200789 case P54_CONTROL_TYPE_STAT_READBACK:
790 p54_rx_stats(dev, skb);
791 break;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200792 case P54_CONTROL_TYPE_EEPROM_READBACK:
793 p54_rx_eeprom_readback(dev, skb);
794 break;
Michael Wueff1a592007-09-25 18:11:01 -0700795 default:
796 printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
797 wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
798 break;
799 }
Christian Lamparter19c19d52008-09-03 22:25:25 +0200800
801 return 0;
Michael Wueff1a592007-09-25 18:11:01 -0700802}
803
804/* returns zero if skb can be reused */
805int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
806{
Christian Lamparter9e7f3f82008-10-18 23:18:01 +0200807 u16 type = le16_to_cpu(*((__le16 *)skb->data));
Christian Lamparter19c19d52008-09-03 22:25:25 +0200808
Christian Lamparter9e7f3f82008-10-18 23:18:01 +0200809 if (type & P54_HDR_FLAG_CONTROL)
Christian Lamparter19c19d52008-09-03 22:25:25 +0200810 return p54_rx_control(dev, skb);
811 else
812 return p54_rx_data(dev, skb);
Michael Wueff1a592007-09-25 18:11:01 -0700813}
814EXPORT_SYMBOL_GPL(p54_rx);
815
816/*
817 * So, the firmware is somewhat stupid and doesn't know what places in its
818 * memory incoming data should go to. By poking around in the firmware, we
819 * can find some unused memory to upload our packets to. However, data that we
820 * want the card to TX needs to stay intact until the card has told us that
821 * it is done with it. This function finds empty places we can upload to and
822 * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
823 * allocated areas.
824 */
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200825static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
John W. Linville27df6052008-10-22 16:41:55 -0400826 struct p54_hdr *data, u32 len)
Michael Wueff1a592007-09-25 18:11:01 -0700827{
828 struct p54_common *priv = dev->priv;
829 struct sk_buff *entry = priv->tx_queue.next;
830 struct sk_buff *target_skb = NULL;
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200831 struct ieee80211_tx_info *info;
832 struct memrecord *range;
Michael Wueff1a592007-09-25 18:11:01 -0700833 u32 last_addr = priv->rx_start;
834 u32 largest_hole = 0;
835 u32 target_addr = priv->rx_start;
836 unsigned long flags;
837 unsigned int left;
Christian Lamparter4e416a62008-09-01 22:48:41 +0200838 len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
Michael Wueff1a592007-09-25 18:11:01 -0700839
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200840 if (!skb)
841 return -EINVAL;
842
Michael Wueff1a592007-09-25 18:11:01 -0700843 spin_lock_irqsave(&priv->tx_queue.lock, flags);
844 left = skb_queue_len(&priv->tx_queue);
845 while (left--) {
846 u32 hole_size;
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200847 info = IEEE80211_SKB_CB(entry);
848 range = (void *)info->rate_driver_data;
Michael Wueff1a592007-09-25 18:11:01 -0700849 hole_size = range->start_addr - last_addr;
850 if (!target_skb && hole_size >= len) {
851 target_skb = entry->prev;
852 hole_size -= len;
853 target_addr = last_addr;
854 }
855 largest_hole = max(largest_hole, hole_size);
856 last_addr = range->end_addr;
857 entry = entry->next;
858 }
859 if (!target_skb && priv->rx_end - last_addr >= len) {
860 target_skb = priv->tx_queue.prev;
861 largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
862 if (!skb_queue_empty(&priv->tx_queue)) {
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200863 info = IEEE80211_SKB_CB(target_skb);
864 range = (void *)info->rate_driver_data;
Michael Wueff1a592007-09-25 18:11:01 -0700865 target_addr = range->end_addr;
866 }
867 } else
868 largest_hole = max(largest_hole, priv->rx_end - last_addr);
869
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200870 if (!target_skb) {
871 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
872 ieee80211_stop_queues(dev);
873 return -ENOMEM;
Michael Wueff1a592007-09-25 18:11:01 -0700874 }
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200875
876 info = IEEE80211_SKB_CB(skb);
877 range = (void *)info->rate_driver_data;
878 range->start_addr = target_addr;
879 range->end_addr = target_addr + len;
880 __skb_queue_after(&priv->tx_queue, target_skb, skb);
Michael Wueff1a592007-09-25 18:11:01 -0700881 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
882
John W. Linville27df6052008-10-22 16:41:55 -0400883 if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200884 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
885 ieee80211_stop_queues(dev);
886
Christian Lamparter4e416a62008-09-01 22:48:41 +0200887 data->req_id = cpu_to_le32(target_addr + priv->headroom);
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200888 return 0;
889}
890
891static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev,
892 u16 hdr_flags, u16 len, u16 type, gfp_t memflags)
893{
894 struct p54_common *priv = dev->priv;
John W. Linville27df6052008-10-22 16:41:55 -0400895 struct p54_hdr *hdr;
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200896 struct sk_buff *skb;
897
898 skb = __dev_alloc_skb(len + priv->tx_hdr_len, memflags);
899 if (!skb)
900 return NULL;
901 skb_reserve(skb, priv->tx_hdr_len);
902
John W. Linville27df6052008-10-22 16:41:55 -0400903 hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
904 hdr->flags = cpu_to_le16(hdr_flags);
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200905 hdr->len = cpu_to_le16(len - sizeof(*hdr));
906 hdr->type = cpu_to_le16(type);
John W. Linville27df6052008-10-22 16:41:55 -0400907 hdr->tries = hdr->rts_tries = 0;
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200908
909 if (unlikely(p54_assign_address(dev, skb, hdr, len))) {
910 kfree_skb(skb);
911 return NULL;
912 }
913 return skb;
Michael Wueff1a592007-09-25 18:11:01 -0700914}
915
Christian Lamparter7cb77072008-09-01 22:48:51 +0200916int p54_read_eeprom(struct ieee80211_hw *dev)
917{
918 struct p54_common *priv = dev->priv;
John W. Linville27df6052008-10-22 16:41:55 -0400919 struct p54_hdr *hdr = NULL;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200920 struct p54_eeprom_lm86 *eeprom_hdr;
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200921 struct sk_buff *skb;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200922 size_t eeprom_size = 0x2020, offset = 0, blocksize;
923 int ret = -ENOMEM;
924 void *eeprom = NULL;
925
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200926 skb = p54_alloc_skb(dev, 0x8000, sizeof(*hdr) + sizeof(*eeprom_hdr) +
927 EEPROM_READBACK_LEN,
928 P54_CONTROL_TYPE_EEPROM_READBACK, GFP_KERNEL);
929 if (!skb)
Christian Lamparter7cb77072008-09-01 22:48:51 +0200930 goto free;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200931 priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
932 if (!priv->eeprom)
933 goto free;
Christian Lamparter7cb77072008-09-01 22:48:51 +0200934 eeprom = kzalloc(eeprom_size, GFP_KERNEL);
935 if (!eeprom)
936 goto free;
937
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200938 eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
939 sizeof(*eeprom_hdr) + EEPROM_READBACK_LEN);
Christian Lamparter7cb77072008-09-01 22:48:51 +0200940
941 while (eeprom_size) {
942 blocksize = min(eeprom_size, (size_t)EEPROM_READBACK_LEN);
Christian Lamparter7cb77072008-09-01 22:48:51 +0200943 eeprom_hdr->offset = cpu_to_le16(offset);
944 eeprom_hdr->len = cpu_to_le16(blocksize);
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200945 priv->tx(dev, skb, 0);
Christian Lamparter7cb77072008-09-01 22:48:51 +0200946
947 if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
948 printk(KERN_ERR "%s: device does not respond!\n",
949 wiphy_name(dev->wiphy));
950 ret = -EBUSY;
951 goto free;
952 }
953
954 memcpy(eeprom + offset, priv->eeprom, blocksize);
955 offset += blocksize;
956 eeprom_size -= blocksize;
957 }
958
959 ret = p54_parse_eeprom(dev, eeprom, offset);
960free:
961 kfree(priv->eeprom);
962 priv->eeprom = NULL;
Christian Lamparterb92f30d2008-10-15 04:07:16 +0200963 p54_free_skb(dev, skb);
Christian Lamparter7cb77072008-09-01 22:48:51 +0200964 kfree(eeprom);
965
966 return ret;
967}
968EXPORT_SYMBOL_GPL(p54_read_eeprom);
969
Christian Lampartere5ea92a2008-10-18 23:19:00 +0200970static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
971 bool set)
972{
973 struct p54_common *priv = dev->priv;
974 struct sk_buff *skb;
975 struct p54_tim *tim;
976
977 skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
978 sizeof(struct p54_hdr) + sizeof(*tim),
979 P54_CONTROL_TYPE_TIM, GFP_KERNEL);
980 if (!skb)
981 return -ENOMEM;
982
983 tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
984 tim->count = 1;
985 tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
986 priv->tx(dev, skb, 1);
987 return 0;
988}
989
990static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
991{
992 struct p54_common *priv = dev->priv;
993 struct sk_buff *skb;
994 struct p54_sta_unlock *sta;
995
996 skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
997 sizeof(struct p54_hdr) + sizeof(*sta),
998 P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
999 if (!skb)
1000 return -ENOMEM;
1001
1002 sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
1003 memcpy(sta->addr, addr, ETH_ALEN);
1004 priv->tx(dev, skb, 1);
1005 return 0;
1006}
1007
Christian Lamparterc772a082008-11-29 22:33:57 +01001008static void p54_sta_notify_ps(struct ieee80211_hw *dev,
1009 enum sta_notify_ps_cmd notify_cmd,
1010 struct ieee80211_sta *sta)
1011{
1012 switch (notify_cmd) {
1013 case STA_NOTIFY_AWAKE:
1014 p54_sta_unlock(dev, sta->addr);
1015 break;
1016 default:
1017 break;
1018 }
1019}
1020
1021static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
1022 enum sta_notify_cmd notify_cmd,
1023 struct ieee80211_sta *sta)
1024{
1025 switch (notify_cmd) {
1026 case STA_NOTIFY_ADD:
1027 case STA_NOTIFY_REMOVE:
1028 /*
1029 * Notify the firmware that we don't want or we don't
1030 * need to buffer frames for this station anymore.
1031 */
1032
1033 p54_sta_unlock(dev, sta->addr);
1034 break;
1035 default:
1036 break;
1037 }
1038}
1039
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001040static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
1041{
1042 struct p54_common *priv = dev->priv;
1043 struct sk_buff *skb;
1044 struct p54_hdr *hdr;
1045 struct p54_txcancel *cancel;
1046
1047 skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
1048 sizeof(struct p54_hdr) + sizeof(*cancel),
1049 P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
1050 if (!skb)
1051 return -ENOMEM;
1052
1053 hdr = (void *)entry->data;
1054 cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
1055 cancel->req_id = hdr->req_id;
1056 priv->tx(dev, skb, 1);
1057 return 0;
1058}
1059
Christian Lamparter94585b02008-10-18 23:18:44 +02001060static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
1061 struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
1062 u16 *flags, u16 *aid)
1063{
1064 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1065 struct p54_common *priv = dev->priv;
1066 int ret = 0;
1067
1068 if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
1069 if (ieee80211_is_beacon(hdr->frame_control)) {
1070 *aid = 0;
1071 *queue = 0;
1072 *extra_len = IEEE80211_MAX_TIM_LEN;
1073 *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
1074 return 0;
1075 } else if (ieee80211_is_probe_resp(hdr->frame_control)) {
1076 *aid = 0;
1077 *queue = 2;
1078 *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
1079 P54_HDR_FLAG_DATA_OUT_NOCANCEL;
1080 return 0;
1081 } else {
1082 *queue = 2;
1083 ret = 0;
1084 }
1085 } else {
1086 *queue += 4;
1087 ret = 1;
1088 }
1089
1090 switch (priv->mode) {
1091 case NL80211_IFTYPE_STATION:
1092 *aid = 1;
1093 break;
1094 case NL80211_IFTYPE_AP:
1095 case NL80211_IFTYPE_ADHOC:
Christian Lamparterd131bb52008-11-15 17:02:31 +01001096 case NL80211_IFTYPE_MESH_POINT:
Christian Lamparter94585b02008-10-18 23:18:44 +02001097 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
1098 *aid = 0;
1099 *queue = 3;
1100 return 0;
1101 }
1102 if (info->control.sta)
1103 *aid = info->control.sta->aid;
1104 else
Christian Lamparterc772a082008-11-29 22:33:57 +01001105 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
Christian Lamparter94585b02008-10-18 23:18:44 +02001106 }
1107 return ret;
1108}
1109
Johannes Berge039fa42008-05-15 12:55:29 +02001110static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
Michael Wueff1a592007-09-25 18:11:01 -07001111{
Johannes Berge039fa42008-05-15 12:55:29 +02001112 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001113 struct ieee80211_tx_queue_stats *current_queue = NULL;
Michael Wueff1a592007-09-25 18:11:01 -07001114 struct p54_common *priv = dev->priv;
John W. Linville27df6052008-10-22 16:41:55 -04001115 struct p54_hdr *hdr;
1116 struct p54_tx_data *txhdr;
John W. Linvilledb4186c2008-10-31 15:53:12 -04001117 size_t padding, len, tim_len = 0;
Christian Lamparterc772a082008-11-29 22:33:57 +01001118 int i, j, ridx, ret;
Christian Lamparter94585b02008-10-18 23:18:44 +02001119 u16 hdr_flags = 0, aid = 0;
1120 u8 rate, queue;
Christian Lamparteraaa15532008-08-09 19:20:47 -05001121 u8 cts_rate = 0x20;
Johannes Berge6a98542008-10-21 12:40:02 +02001122 u8 rc_flags;
Johannes Bergc12abae2008-10-14 16:56:51 +02001123 u8 calculated_tries[4];
1124 u8 nrates = 0, nremaining = 8;
Michael Wueff1a592007-09-25 18:11:01 -07001125
Christian Lamparter94585b02008-10-18 23:18:44 +02001126 queue = skb_get_queue_mapping(skb);
1127
Christian Lamparterc772a082008-11-29 22:33:57 +01001128 ret = p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid);
1129 current_queue = &priv->tx_stats[queue];
1130 if (unlikely((current_queue->len > current_queue->limit) && ret))
1131 return NETDEV_TX_BUSY;
1132 current_queue->len++;
1133 current_queue->count++;
1134 if ((current_queue->len == current_queue->limit) && ret)
1135 ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
Michael Wueff1a592007-09-25 18:11:01 -07001136
1137 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
1138 len = skb->len;
1139
John W. Linville27df6052008-10-22 16:41:55 -04001140 txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
1141 hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
Michael Wueff1a592007-09-25 18:11:01 -07001142
1143 if (padding)
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001144 hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
Michael Wueff1a592007-09-25 18:11:01 -07001145 hdr->len = cpu_to_le16(len);
Christian Lamparter94585b02008-10-18 23:18:44 +02001146 hdr->type = cpu_to_le16(aid);
John W. Linville27df6052008-10-22 16:41:55 -04001147 hdr->rts_tries = info->control.rates[0].count;
Michael Wueff1a592007-09-25 18:11:01 -07001148
Johannes Bergc12abae2008-10-14 16:56:51 +02001149 /*
1150 * we register the rates in perfect order, and
1151 * RTS/CTS won't happen on 5 GHz
1152 */
1153 cts_rate = info->control.rts_cts_rate_idx;
1154
1155 memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
1156
1157 /* see how many rates got used */
1158 for (i = 0; i < 4; i++) {
1159 if (info->control.rates[i].idx < 0)
1160 break;
1161 nrates++;
Christian Lamparteraaa15532008-08-09 19:20:47 -05001162 }
Johannes Bergc12abae2008-10-14 16:56:51 +02001163
1164 /* limit tries to 8/nrates per rate */
1165 for (i = 0; i < nrates; i++) {
1166 /*
1167 * The magic expression here is equivalent to 8/nrates for
1168 * all values that matter, but avoids division and jumps.
1169 * Note that nrates can only take the values 1 through 4.
1170 */
1171 calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
1172 info->control.rates[i].count);
1173 nremaining -= calculated_tries[i];
Christian Lamparteraaa15532008-08-09 19:20:47 -05001174 }
Johannes Bergc12abae2008-10-14 16:56:51 +02001175
1176 /* if there are tries left, distribute from back to front */
1177 for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
1178 int tmp = info->control.rates[i].count - calculated_tries[i];
1179
1180 if (tmp <= 0)
1181 continue;
1182 /* RC requested more tries at this rate */
1183
1184 tmp = min_t(int, tmp, nremaining);
1185 calculated_tries[i] += tmp;
1186 nremaining -= tmp;
1187 }
1188
1189 ridx = 0;
1190 for (i = 0; i < nrates && ridx < 8; i++) {
1191 /* we register the rates in perfect order */
1192 rate = info->control.rates[i].idx;
1193 if (info->band == IEEE80211_BAND_5GHZ)
1194 rate += 4;
1195
1196 /* store the count we actually calculated for TX status */
1197 info->control.rates[i].count = calculated_tries[i];
1198
1199 rc_flags = info->control.rates[i].flags;
1200 if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
1201 rate |= 0x10;
1202 cts_rate |= 0x10;
1203 }
1204 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
1205 rate |= 0x40;
1206 else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1207 rate |= 0x20;
1208 for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
1209 txhdr->rateset[ridx] = rate;
1210 ridx++;
1211 }
1212 }
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001213
1214 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
1215 hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
1216
1217 /* TODO: enable bursting */
1218 hdr->flags = cpu_to_le16(hdr_flags);
John W. Linville27df6052008-10-22 16:41:55 -04001219 hdr->tries = ridx;
1220 txhdr->crypt_offset = 0;
1221 txhdr->rts_rate_idx = 0;
Christian Lamparteraaa15532008-08-09 19:20:47 -05001222 txhdr->key_type = 0;
1223 txhdr->key_len = 0;
Christian Lamparter94585b02008-10-18 23:18:44 +02001224 txhdr->hw_queue = queue;
Christian Lamparterffed7852008-11-14 19:41:22 +01001225 if (current_queue)
1226 txhdr->backlog = current_queue->len;
1227 else
1228 txhdr->backlog = 0;
John W. Linville27df6052008-10-22 16:41:55 -04001229 memset(txhdr->durations, 0, sizeof(txhdr->durations));
Christian Lamparteraaa15532008-08-09 19:20:47 -05001230 txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
Johannes Berge039fa42008-05-15 12:55:29 +02001231 2 : info->antenna_sel_tx - 1;
Christian Lamparter09adf282008-09-06 14:25:53 +02001232 txhdr->output_power = priv->output_power;
John W. Linville27df6052008-10-22 16:41:55 -04001233 txhdr->cts_rate = cts_rate;
Michael Wueff1a592007-09-25 18:11:01 -07001234 if (padding)
1235 txhdr->align[0] = padding;
1236
Johannes Berge039fa42008-05-15 12:55:29 +02001237 /* modifies skb->cb and with it info, so must be last! */
Christian Lamparter94585b02008-10-18 23:18:44 +02001238 if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len))) {
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001239 skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001240 if (current_queue) {
1241 current_queue->len--;
1242 current_queue->count--;
1243 }
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001244 return NETDEV_TX_BUSY;
1245 }
1246 priv->tx(dev, skb, 0);
Michael Wueff1a592007-09-25 18:11:01 -07001247 return 0;
1248}
1249
Christian Lamparter5e734442008-10-15 04:07:56 +02001250static int p54_setup_mac(struct ieee80211_hw *dev, u16 mode, const u8 *bssid)
Michael Wueff1a592007-09-25 18:11:01 -07001251{
1252 struct p54_common *priv = dev->priv;
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001253 struct sk_buff *skb;
Christian Lamparter5e734442008-10-15 04:07:56 +02001254 struct p54_setup_mac *setup;
Michael Wueff1a592007-09-25 18:11:01 -07001255
John W. Linville27df6052008-10-22 16:41:55 -04001256 skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup) +
1257 sizeof(struct p54_hdr), P54_CONTROL_TYPE_SETUP,
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001258 GFP_ATOMIC);
1259 if (!skb)
Michael Wueff1a592007-09-25 18:11:01 -07001260 return -ENOMEM;
1261
Christian Lamparter5e734442008-10-15 04:07:56 +02001262 setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
1263 priv->mac_mode = mode;
1264 setup->mac_mode = cpu_to_le16(mode);
1265 memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
Christian Lampartere0a58ea2008-09-03 22:25:20 +02001266 if (!bssid)
Christian Lamparter5e734442008-10-15 04:07:56 +02001267 memset(setup->bssid, ~0, ETH_ALEN);
Michael Wueff1a592007-09-25 18:11:01 -07001268 else
Christian Lamparter5e734442008-10-15 04:07:56 +02001269 memcpy(setup->bssid, bssid, ETH_ALEN);
1270 setup->rx_antenna = priv->rx_antenna;
Chr94834072008-10-29 22:39:50 +01001271 setup->rx_align = 0;
Christian Lamparter19c19d52008-09-03 22:25:25 +02001272 if (priv->fw_var < 0x500) {
Christian Lamparterced09572008-11-14 19:42:39 +01001273 setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
Chr94834072008-10-29 22:39:50 +01001274 memset(setup->v1.rts_rates, 0, 8);
Christian Lamparter5e734442008-10-15 04:07:56 +02001275 setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
1276 setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
1277 setup->v1.rxhw = cpu_to_le16(priv->rxhw);
Christian Lamparterced09572008-11-14 19:42:39 +01001278 setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
Christian Lamparter5e734442008-10-15 04:07:56 +02001279 setup->v1.unalloc0 = cpu_to_le16(0);
Christian Lamparter19c19d52008-09-03 22:25:25 +02001280 } else {
Christian Lamparter5e734442008-10-15 04:07:56 +02001281 setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
1282 setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
1283 setup->v2.rxhw = cpu_to_le16(priv->rxhw);
Christian Lamparterced09572008-11-14 19:42:39 +01001284 setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
Christian Lamparter5e734442008-10-15 04:07:56 +02001285 setup->v2.truncate = cpu_to_le16(48896);
Christian Lamparterced09572008-11-14 19:42:39 +01001286 setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
Christian Lamparter5e734442008-10-15 04:07:56 +02001287 setup->v2.sbss_offset = 0;
1288 setup->v2.mcast_window = 0;
1289 setup->v2.rx_rssi_threshold = 0;
1290 setup->v2.rx_ed_threshold = 0;
1291 setup->v2.ref_clock = cpu_to_le32(644245094);
1292 setup->v2.lpf_bandwidth = cpu_to_le16(65535);
1293 setup->v2.osc_start_delay = cpu_to_le16(65535);
Christian Lamparter19c19d52008-09-03 22:25:25 +02001294 }
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001295 priv->tx(dev, skb, 1);
Michael Wueff1a592007-09-25 18:11:01 -07001296 return 0;
1297}
1298
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001299static int p54_set_freq(struct ieee80211_hw *dev, u16 frequency)
Michael Wueff1a592007-09-25 18:11:01 -07001300{
1301 struct p54_common *priv = dev->priv;
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001302 struct sk_buff *skb;
John W. Linville27df6052008-10-22 16:41:55 -04001303 struct p54_scan *chan;
Michael Wueff1a592007-09-25 18:11:01 -07001304 unsigned int i;
Michael Wueff1a592007-09-25 18:11:01 -07001305 void *entry;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001306 __le16 freq = cpu_to_le16(frequency);
Michael Wueff1a592007-09-25 18:11:01 -07001307
John W. Linville27df6052008-10-22 16:41:55 -04001308 skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*chan) +
1309 sizeof(struct p54_hdr), P54_CONTROL_TYPE_SCAN,
1310 GFP_ATOMIC);
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001311 if (!skb)
Michael Wueff1a592007-09-25 18:11:01 -07001312 return -ENOMEM;
1313
John W. Linville27df6052008-10-22 16:41:55 -04001314 chan = (struct p54_scan *) skb_put(skb, sizeof(*chan));
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001315 memset(chan->padding1, 0, sizeof(chan->padding1));
John W. Linville27df6052008-10-22 16:41:55 -04001316 chan->mode = cpu_to_le16(P54_SCAN_EXIT);
Christian Lamparter154e3af2008-08-23 22:15:25 +02001317 chan->dwell = cpu_to_le16(0x0);
Michael Wueff1a592007-09-25 18:11:01 -07001318
1319 for (i = 0; i < priv->iq_autocal_len; i++) {
1320 if (priv->iq_autocal[i].freq != freq)
1321 continue;
1322
1323 memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
1324 sizeof(*priv->iq_autocal));
1325 break;
1326 }
1327 if (i == priv->iq_autocal_len)
1328 goto err;
1329
1330 for (i = 0; i < priv->output_limit_len; i++) {
1331 if (priv->output_limit[i].freq != freq)
1332 continue;
1333
1334 chan->val_barker = 0x38;
Christian Lamparter154e3af2008-08-23 22:15:25 +02001335 chan->val_bpsk = chan->dup_bpsk =
1336 priv->output_limit[i].val_bpsk;
1337 chan->val_qpsk = chan->dup_qpsk =
1338 priv->output_limit[i].val_qpsk;
1339 chan->val_16qam = chan->dup_16qam =
1340 priv->output_limit[i].val_16qam;
1341 chan->val_64qam = chan->dup_64qam =
1342 priv->output_limit[i].val_64qam;
Michael Wueff1a592007-09-25 18:11:01 -07001343 break;
1344 }
1345 if (i == priv->output_limit_len)
1346 goto err;
1347
Michael Wueff1a592007-09-25 18:11:01 -07001348 entry = priv->curve_data->data;
1349 for (i = 0; i < priv->curve_data->channels; i++) {
1350 if (*((__le16 *)entry) != freq) {
1351 entry += sizeof(__le16);
Christian Lamparter154e3af2008-08-23 22:15:25 +02001352 entry += sizeof(struct p54_pa_curve_data_sample) *
1353 priv->curve_data->points_per_channel;
Michael Wueff1a592007-09-25 18:11:01 -07001354 continue;
1355 }
1356
1357 entry += sizeof(__le16);
Chr94834072008-10-29 22:39:50 +01001358 chan->pa_points_per_curve = 8;
1359 memset(chan->curve_data, 0, sizeof(*chan->curve_data));
1360 memcpy(chan->curve_data, entry,
1361 sizeof(struct p54_pa_curve_data_sample) *
1362 min((u8)8, priv->curve_data->points_per_channel));
Michael Wueff1a592007-09-25 18:11:01 -07001363 break;
1364 }
1365
Christian Lamparter19c19d52008-09-03 22:25:25 +02001366 if (priv->fw_var < 0x500) {
Christian Lamparter19c19d52008-09-03 22:25:25 +02001367 chan->v1.rssical_mul = cpu_to_le16(130);
1368 chan->v1.rssical_add = cpu_to_le16(0xfe70);
1369 } else {
Christian Lamparter19c19d52008-09-03 22:25:25 +02001370 chan->v2.rssical_mul = cpu_to_le16(130);
1371 chan->v2.rssical_add = cpu_to_le16(0xfe70);
Christian Lamparterced09572008-11-14 19:42:39 +01001372 chan->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
John W. Linville27df6052008-10-22 16:41:55 -04001373 memset(chan->v2.rts_rates, 0, 8);
Christian Lamparter19c19d52008-09-03 22:25:25 +02001374 }
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001375 priv->tx(dev, skb, 1);
Michael Wueff1a592007-09-25 18:11:01 -07001376 return 0;
1377
1378 err:
1379 printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001380 kfree_skb(skb);
Michael Wueff1a592007-09-25 18:11:01 -07001381 return -EINVAL;
1382}
1383
1384static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
1385{
1386 struct p54_common *priv = dev->priv;
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001387 struct sk_buff *skb;
John W. Linville27df6052008-10-22 16:41:55 -04001388 struct p54_led *led;
Michael Wueff1a592007-09-25 18:11:01 -07001389
John W. Linville27df6052008-10-22 16:41:55 -04001390 skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led) +
1391 sizeof(struct p54_hdr), P54_CONTROL_TYPE_LED,
1392 GFP_ATOMIC);
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001393 if (!skb)
Michael Wueff1a592007-09-25 18:11:01 -07001394 return -ENOMEM;
1395
John W. Linville27df6052008-10-22 16:41:55 -04001396 led = (struct p54_led *)skb_put(skb, sizeof(*led));
Michael Wueff1a592007-09-25 18:11:01 -07001397 led->mode = cpu_to_le16(mode);
1398 led->led_permanent = cpu_to_le16(link);
1399 led->led_temporary = cpu_to_le16(act);
1400 led->duration = cpu_to_le16(1000);
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001401 priv->tx(dev, skb, 1);
Michael Wueff1a592007-09-25 18:11:01 -07001402 return 0;
1403}
1404
Johannes Berg3330d7be2008-02-10 16:49:38 +01001405#define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
Michael Wueff1a592007-09-25 18:11:01 -07001406do { \
1407 queue.aifs = cpu_to_le16(ai_fs); \
1408 queue.cwmin = cpu_to_le16(cw_min); \
1409 queue.cwmax = cpu_to_le16(cw_max); \
Johannes Berg3330d7be2008-02-10 16:49:38 +01001410 queue.txop = cpu_to_le16(_txop); \
Michael Wueff1a592007-09-25 18:11:01 -07001411} while(0)
1412
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001413static int p54_set_edcf(struct ieee80211_hw *dev)
Michael Wueff1a592007-09-25 18:11:01 -07001414{
1415 struct p54_common *priv = dev->priv;
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001416 struct sk_buff *skb;
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001417 struct p54_edcf *edcf;
Michael Wueff1a592007-09-25 18:11:01 -07001418
John W. Linville27df6052008-10-22 16:41:55 -04001419 skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf) +
1420 sizeof(struct p54_hdr), P54_CONTROL_TYPE_DCFINIT,
1421 GFP_ATOMIC);
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001422 if (!skb)
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001423 return -ENOMEM;
1424
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001425 edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
Christian Lamparter40333e42008-10-08 20:52:22 +02001426 if (priv->use_short_slot) {
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001427 edcf->slottime = 9;
1428 edcf->sifs = 0x10;
1429 edcf->eofpad = 0x00;
Michael Wueff1a592007-09-25 18:11:01 -07001430 } else {
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001431 edcf->slottime = 20;
1432 edcf->sifs = 0x0a;
1433 edcf->eofpad = 0x06;
Michael Wueff1a592007-09-25 18:11:01 -07001434 }
Michael Wueff1a592007-09-25 18:11:01 -07001435 /* (see prism54/isl_oid.h for further details) */
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001436 edcf->frameburst = cpu_to_le16(0);
1437 edcf->round_trip_delay = cpu_to_le16(0);
Chr94834072008-10-29 22:39:50 +01001438 edcf->flags = 0;
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001439 memset(edcf->mapping, 0, sizeof(edcf->mapping));
1440 memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001441 priv->tx(dev, skb, 1);
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001442 return 0;
Michael Wueff1a592007-09-25 18:11:01 -07001443}
1444
Christian Lamparter0f1be972008-10-15 03:56:20 +02001445static int p54_init_stats(struct ieee80211_hw *dev)
1446{
1447 struct p54_common *priv = dev->priv;
Christian Lamparter0f1be972008-10-15 03:56:20 +02001448
John W. Linville27df6052008-10-22 16:41:55 -04001449 priv->cached_stats = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
1450 sizeof(struct p54_hdr) + sizeof(struct p54_statistics),
1451 P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
Christian Lamparter0f1be972008-10-15 03:56:20 +02001452 if (!priv->cached_stats)
1453 return -ENOMEM;
1454
Christian Lamparter0f1be972008-10-15 03:56:20 +02001455 mod_timer(&priv->stats_timer, jiffies + HZ);
1456 return 0;
1457}
1458
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001459static int p54_beacon_tim(struct sk_buff *skb)
1460{
1461 /*
1462 * the good excuse for this mess is ... the firmware.
1463 * The dummy TIM MUST be at the end of the beacon frame,
1464 * because it'll be overwritten!
1465 */
1466
1467 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1468 u8 *pos, *end;
1469
1470 if (skb->len <= sizeof(mgmt)) {
1471 printk(KERN_ERR "p54: beacon is too short!\n");
1472 return -EINVAL;
1473 }
1474
1475 pos = (u8 *)mgmt->u.beacon.variable;
1476 end = skb->data + skb->len;
1477 while (pos < end) {
1478 if (pos + 2 + pos[1] > end) {
1479 printk(KERN_ERR "p54: parsing beacon failed\n");
1480 return -EINVAL;
1481 }
1482
1483 if (pos[0] == WLAN_EID_TIM) {
1484 u8 dtim_len = pos[1];
1485 u8 dtim_period = pos[3];
1486 u8 *next = pos + 2 + dtim_len;
1487
1488 if (dtim_len < 3) {
1489 printk(KERN_ERR "p54: invalid dtim len!\n");
1490 return -EINVAL;
1491 }
1492 memmove(pos, next, end - next);
1493
1494 if (dtim_len > 3)
1495 skb_trim(skb, skb->len - (dtim_len - 3));
1496
1497 pos = end - (dtim_len + 2);
1498
1499 /* add the dummy at the end */
1500 pos[0] = WLAN_EID_TIM;
1501 pos[1] = 3;
1502 pos[2] = 0;
1503 pos[3] = dtim_period;
1504 pos[4] = 0;
1505 return 0;
1506 }
1507 pos += 2 + pos[1];
1508 }
1509 return 0;
1510}
1511
1512static int p54_beacon_update(struct ieee80211_hw *dev,
1513 struct ieee80211_vif *vif)
1514{
1515 struct p54_common *priv = dev->priv;
1516 struct sk_buff *beacon;
1517 int ret;
1518
1519 if (priv->cached_beacon) {
1520 p54_tx_cancel(dev, priv->cached_beacon);
1521 /* wait for the last beacon the be freed */
1522 msleep(10);
1523 }
1524
1525 beacon = ieee80211_beacon_get(dev, vif);
1526 if (!beacon)
1527 return -ENOMEM;
1528 ret = p54_beacon_tim(beacon);
1529 if (ret)
1530 return ret;
1531 ret = p54_tx(dev, beacon);
1532 if (ret)
1533 return ret;
1534 priv->cached_beacon = beacon;
1535 priv->tsf_high32 = 0;
1536 priv->tsf_low32 = 0;
1537
1538 return 0;
1539}
1540
Johannes Berg4150c572007-09-17 01:29:23 -04001541static int p54_start(struct ieee80211_hw *dev)
Michael Wueff1a592007-09-25 18:11:01 -07001542{
1543 struct p54_common *priv = dev->priv;
1544 int err;
1545
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001546 mutex_lock(&priv->conf_mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04001547 err = priv->open(dev);
Christian Lamparter40db0b22008-11-16 12:20:32 +01001548 if (err)
1549 goto out;
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001550 P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
1551 P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
1552 P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
1553 P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
1554 err = p54_set_edcf(dev);
Christian Lamparter40db0b22008-11-16 12:20:32 +01001555 if (err)
1556 goto out;
1557 err = p54_init_stats(dev);
1558 if (err)
1559 goto out;
1560 err = p54_setup_mac(dev, P54_FILTER_TYPE_NONE, NULL);
1561 if (err)
1562 goto out;
1563 priv->mode = NL80211_IFTYPE_MONITOR;
Johannes Berg4150c572007-09-17 01:29:23 -04001564
Christian Lamparter40db0b22008-11-16 12:20:32 +01001565out:
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001566 mutex_unlock(&priv->conf_mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04001567 return err;
1568}
1569
1570static void p54_stop(struct ieee80211_hw *dev)
1571{
1572 struct p54_common *priv = dev->priv;
1573 struct sk_buff *skb;
Christian Lampartercc6de662008-09-06 02:56:23 +02001574
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001575 mutex_lock(&priv->conf_mutex);
Christian Lampartercc6de662008-09-06 02:56:23 +02001576 del_timer(&priv->stats_timer);
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001577 p54_free_skb(dev, priv->cached_stats);
Christian Lamparter0f1be972008-10-15 03:56:20 +02001578 priv->cached_stats = NULL;
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001579 if (priv->cached_beacon)
1580 p54_tx_cancel(dev, priv->cached_beacon);
1581
Johannes Berge039fa42008-05-15 12:55:29 +02001582 while ((skb = skb_dequeue(&priv->tx_queue)))
Johannes Berg4150c572007-09-17 01:29:23 -04001583 kfree_skb(skb);
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001584
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001585 priv->cached_beacon = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04001586 priv->stop(dev);
Christian Lampartera0db6632008-09-06 02:56:04 +02001587 priv->tsf_high32 = priv->tsf_low32 = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02001588 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001589 mutex_unlock(&priv->conf_mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04001590}
1591
1592static int p54_add_interface(struct ieee80211_hw *dev,
1593 struct ieee80211_if_init_conf *conf)
1594{
1595 struct p54_common *priv = dev->priv;
1596
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001597 mutex_lock(&priv->conf_mutex);
1598 if (priv->mode != NL80211_IFTYPE_MONITOR) {
1599 mutex_unlock(&priv->conf_mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04001600 return -EOPNOTSUPP;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001601 }
Michael Wueff1a592007-09-25 18:11:01 -07001602
1603 switch (conf->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001604 case NL80211_IFTYPE_STATION:
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001605 case NL80211_IFTYPE_ADHOC:
1606 case NL80211_IFTYPE_AP:
Christian Lamparterd131bb52008-11-15 17:02:31 +01001607 case NL80211_IFTYPE_MESH_POINT:
Michael Wueff1a592007-09-25 18:11:01 -07001608 priv->mode = conf->type;
1609 break;
1610 default:
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001611 mutex_unlock(&priv->conf_mutex);
Michael Wueff1a592007-09-25 18:11:01 -07001612 return -EOPNOTSUPP;
1613 }
1614
Johannes Berg4150c572007-09-17 01:29:23 -04001615 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
Michael Wueff1a592007-09-25 18:11:01 -07001616
John W. Linville27df6052008-10-22 16:41:55 -04001617 p54_setup_mac(dev, P54_FILTER_TYPE_NONE, NULL);
Michael Wueff1a592007-09-25 18:11:01 -07001618
1619 switch (conf->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001620 case NL80211_IFTYPE_STATION:
John W. Linville27df6052008-10-22 16:41:55 -04001621 p54_setup_mac(dev, P54_FILTER_TYPE_STATION, NULL);
Michael Wueff1a592007-09-25 18:11:01 -07001622 break;
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001623 case NL80211_IFTYPE_AP:
1624 p54_setup_mac(dev, P54_FILTER_TYPE_AP, priv->mac_addr);
1625 break;
1626 case NL80211_IFTYPE_ADHOC:
Christian Lamparterd131bb52008-11-15 17:02:31 +01001627 case NL80211_IFTYPE_MESH_POINT:
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001628 p54_setup_mac(dev, P54_FILTER_TYPE_IBSS, NULL);
1629 break;
Johannes Berg4150c572007-09-17 01:29:23 -04001630 default:
1631 BUG(); /* impossible */
1632 break;
Michael Wueff1a592007-09-25 18:11:01 -07001633 }
1634
1635 p54_set_leds(dev, 1, 0, 0);
1636
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001637 mutex_unlock(&priv->conf_mutex);
Michael Wueff1a592007-09-25 18:11:01 -07001638 return 0;
1639}
1640
1641static void p54_remove_interface(struct ieee80211_hw *dev,
1642 struct ieee80211_if_init_conf *conf)
1643{
1644 struct p54_common *priv = dev->priv;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001645
1646 mutex_lock(&priv->conf_mutex);
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001647 if (priv->cached_beacon)
1648 p54_tx_cancel(dev, priv->cached_beacon);
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001649 p54_setup_mac(dev, P54_FILTER_TYPE_NONE, NULL);
Johannes Berg05c914f2008-09-11 00:01:58 +02001650 priv->mode = NL80211_IFTYPE_MONITOR;
Johannes Berg4150c572007-09-17 01:29:23 -04001651 memset(priv->mac_addr, 0, ETH_ALEN);
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001652 mutex_unlock(&priv->conf_mutex);
Michael Wueff1a592007-09-25 18:11:01 -07001653}
1654
Johannes Berge8975582008-10-09 12:18:51 +02001655static int p54_config(struct ieee80211_hw *dev, u32 changed)
Michael Wueff1a592007-09-25 18:11:01 -07001656{
1657 int ret;
Larry Finger6041e2a2008-08-03 17:58:36 -05001658 struct p54_common *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001659 struct ieee80211_conf *conf = &dev->conf;
Michael Wueff1a592007-09-25 18:11:01 -07001660
Larry Finger6041e2a2008-08-03 17:58:36 -05001661 mutex_lock(&priv->conf_mutex);
Johannes Berg0f4ac382008-10-09 12:18:04 +02001662 priv->rx_antenna = 2; /* automatic */
Christian Lamparter09adf282008-09-06 14:25:53 +02001663 priv->output_power = conf->power_level << 2;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001664 ret = p54_set_freq(dev, conf->channel->center_freq);
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001665 if (!ret)
1666 ret = p54_set_edcf(dev);
Larry Finger6041e2a2008-08-03 17:58:36 -05001667 mutex_unlock(&priv->conf_mutex);
Michael Wueff1a592007-09-25 18:11:01 -07001668 return ret;
1669}
1670
Johannes Berg32bfd352007-12-19 01:31:26 +01001671static int p54_config_interface(struct ieee80211_hw *dev,
1672 struct ieee80211_vif *vif,
Michael Wueff1a592007-09-25 18:11:01 -07001673 struct ieee80211_if_conf *conf)
1674{
1675 struct p54_common *priv = dev->priv;
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001676 int ret = 0;
Michael Wueff1a592007-09-25 18:11:01 -07001677
Larry Finger6041e2a2008-08-03 17:58:36 -05001678 mutex_lock(&priv->conf_mutex);
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001679 switch (priv->mode) {
1680 case NL80211_IFTYPE_STATION:
1681 ret = p54_setup_mac(dev, P54_FILTER_TYPE_STATION, conf->bssid);
1682 if (ret)
1683 goto out;
1684 ret = p54_set_leds(dev, 1,
1685 !is_multicast_ether_addr(conf->bssid), 0);
1686 if (ret)
1687 goto out;
1688 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
1689 break;
1690 case NL80211_IFTYPE_AP:
1691 case NL80211_IFTYPE_ADHOC:
Christian Lamparterd131bb52008-11-15 17:02:31 +01001692 case NL80211_IFTYPE_MESH_POINT:
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001693 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
1694 ret = p54_set_freq(dev, dev->conf.channel->center_freq);
1695 if (ret)
1696 goto out;
1697 ret = p54_setup_mac(dev, priv->mac_mode, priv->bssid);
1698 if (ret)
1699 goto out;
1700 if (conf->changed & IEEE80211_IFCC_BEACON) {
1701 ret = p54_beacon_update(dev, vif);
1702 if (ret)
1703 goto out;
1704 ret = p54_set_edcf(dev);
1705 if (ret)
1706 goto out;
1707 }
1708 }
1709out:
Larry Finger6041e2a2008-08-03 17:58:36 -05001710 mutex_unlock(&priv->conf_mutex);
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001711 return ret;
Michael Wueff1a592007-09-25 18:11:01 -07001712}
1713
Johannes Berg4150c572007-09-17 01:29:23 -04001714static void p54_configure_filter(struct ieee80211_hw *dev,
1715 unsigned int changed_flags,
1716 unsigned int *total_flags,
1717 int mc_count, struct dev_mc_list *mclist)
1718{
1719 struct p54_common *priv = dev->priv;
1720
Christian Lamparter78d57eb2008-09-06 02:56:12 +02001721 *total_flags &= FIF_BCN_PRBRESP_PROMISC |
1722 FIF_PROMISC_IN_BSS |
1723 FIF_FCSFAIL;
1724
1725 priv->filter_flags = *total_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04001726
1727 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
1728 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
Christian Lamparter5e734442008-10-15 04:07:56 +02001729 p54_setup_mac(dev, priv->mac_mode, NULL);
Johannes Berg4150c572007-09-17 01:29:23 -04001730 else
Christian Lamparter5e734442008-10-15 04:07:56 +02001731 p54_setup_mac(dev, priv->mac_mode, priv->bssid);
Christian Lamparter78d57eb2008-09-06 02:56:12 +02001732 }
1733
1734 if (changed_flags & FIF_PROMISC_IN_BSS) {
1735 if (*total_flags & FIF_PROMISC_IN_BSS)
Christian Lamparter5e734442008-10-15 04:07:56 +02001736 p54_setup_mac(dev, priv->mac_mode | 0x8, NULL);
Christian Lamparter78d57eb2008-09-06 02:56:12 +02001737 else
Christian Lamparter5e734442008-10-15 04:07:56 +02001738 p54_setup_mac(dev, priv->mac_mode & ~0x8, priv->bssid);
Johannes Berg4150c572007-09-17 01:29:23 -04001739 }
1740}
1741
Johannes Berge100bb62008-04-30 18:51:21 +02001742static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
Michael Wueff1a592007-09-25 18:11:01 -07001743 const struct ieee80211_tx_queue_params *params)
1744{
1745 struct p54_common *priv = dev->priv;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001746 int ret;
Michael Wueff1a592007-09-25 18:11:01 -07001747
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001748 mutex_lock(&priv->conf_mutex);
John W. Linville3df5ee62008-05-01 17:07:32 -04001749 if ((params) && !(queue > 4)) {
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001750 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
Johannes Berg3330d7be2008-02-10 16:49:38 +01001751 params->cw_min, params->cw_max, params->txop);
Christian Lamparterb50563a2008-11-24 14:52:51 +01001752 ret = p54_set_edcf(dev);
Michael Wueff1a592007-09-25 18:11:01 -07001753 } else
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001754 ret = -EINVAL;
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001755 mutex_unlock(&priv->conf_mutex);
1756 return ret;
Michael Wueff1a592007-09-25 18:11:01 -07001757}
1758
Christian Lamparter1b997532008-09-06 14:25:58 +02001759static int p54_init_xbow_synth(struct ieee80211_hw *dev)
1760{
1761 struct p54_common *priv = dev->priv;
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001762 struct sk_buff *skb;
John W. Linville27df6052008-10-22 16:41:55 -04001763 struct p54_xbow_synth *xbow;
Christian Lamparter1b997532008-09-06 14:25:58 +02001764
John W. Linville27df6052008-10-22 16:41:55 -04001765 skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow) +
1766 sizeof(struct p54_hdr),
1767 P54_CONTROL_TYPE_XBOW_SYNTH_CFG,
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001768 GFP_KERNEL);
1769 if (!skb)
Christian Lamparter1b997532008-09-06 14:25:58 +02001770 return -ENOMEM;
1771
John W. Linville27df6052008-10-22 16:41:55 -04001772 xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
Christian Lamparter1b997532008-09-06 14:25:58 +02001773 xbow->magic1 = cpu_to_le16(0x1);
1774 xbow->magic2 = cpu_to_le16(0x2);
1775 xbow->freq = cpu_to_le16(5390);
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001776 memset(xbow->padding, 0, sizeof(xbow->padding));
1777 priv->tx(dev, skb, 1);
Christian Lamparter1b997532008-09-06 14:25:58 +02001778 return 0;
1779}
1780
Christian Lampartercc6de662008-09-06 02:56:23 +02001781static void p54_statistics_timer(unsigned long data)
1782{
1783 struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
1784 struct p54_common *priv = dev->priv;
Christian Lampartercc6de662008-09-06 02:56:23 +02001785
1786 BUG_ON(!priv->cached_stats);
Christian Lampartercc6de662008-09-06 02:56:23 +02001787
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001788 priv->tx(dev, priv->cached_stats, 0);
Christian Lampartercc6de662008-09-06 02:56:23 +02001789}
1790
Michael Wueff1a592007-09-25 18:11:01 -07001791static int p54_get_stats(struct ieee80211_hw *dev,
1792 struct ieee80211_low_level_stats *stats)
1793{
Christian Lampartercc6de662008-09-06 02:56:23 +02001794 struct p54_common *priv = dev->priv;
1795
1796 del_timer(&priv->stats_timer);
1797 p54_statistics_timer((unsigned long)dev);
1798
1799 if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
1800 printk(KERN_ERR "%s: device does not respond!\n",
1801 wiphy_name(dev->wiphy));
1802 return -EBUSY;
1803 }
1804
1805 memcpy(stats, &priv->stats, sizeof(*stats));
1806
Michael Wueff1a592007-09-25 18:11:01 -07001807 return 0;
1808}
1809
1810static int p54_get_tx_stats(struct ieee80211_hw *dev,
1811 struct ieee80211_tx_queue_stats *stats)
1812{
1813 struct p54_common *priv = dev->priv;
Michael Wueff1a592007-09-25 18:11:01 -07001814
Chr84df3ed2008-08-24 03:15:16 +02001815 memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
Michael Wueff1a592007-09-25 18:11:01 -07001816
1817 return 0;
1818}
1819
Christian Lamparter40333e42008-10-08 20:52:22 +02001820static void p54_bss_info_changed(struct ieee80211_hw *dev,
1821 struct ieee80211_vif *vif,
1822 struct ieee80211_bss_conf *info,
1823 u32 changed)
1824{
1825 struct p54_common *priv = dev->priv;
1826
1827 if (changed & BSS_CHANGED_ERP_SLOT) {
1828 priv->use_short_slot = info->use_short_slot;
Christian Lamparter0fdd7c52008-10-15 03:55:37 +02001829 p54_set_edcf(dev);
Christian Lamparter40333e42008-10-08 20:52:22 +02001830 }
Christian Lamparterced09572008-11-14 19:42:39 +01001831 if (changed & BSS_CHANGED_BASIC_RATES) {
1832 if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
1833 priv->basic_rate_mask = (info->basic_rates << 4);
1834 else
1835 priv->basic_rate_mask = info->basic_rates;
1836 p54_setup_mac(dev, priv->mac_mode, priv->bssid);
1837 if (priv->fw_var >= 0x500)
1838 p54_set_freq(dev, dev->conf.channel->center_freq);
1839 }
1840 if (changed & BSS_CHANGED_ASSOC) {
1841 if (info->assoc) {
1842 priv->aid = info->aid;
1843 priv->wakeup_timer = info->beacon_int *
1844 info->dtim_period * 5;
1845 p54_setup_mac(dev, priv->mac_mode, priv->bssid);
1846 }
1847 }
1848
Christian Lamparter40333e42008-10-08 20:52:22 +02001849}
1850
Michael Wueff1a592007-09-25 18:11:01 -07001851static const struct ieee80211_ops p54_ops = {
1852 .tx = p54_tx,
Johannes Berg4150c572007-09-17 01:29:23 -04001853 .start = p54_start,
1854 .stop = p54_stop,
Michael Wueff1a592007-09-25 18:11:01 -07001855 .add_interface = p54_add_interface,
1856 .remove_interface = p54_remove_interface,
Christian Lampartere5ea92a2008-10-18 23:19:00 +02001857 .set_tim = p54_set_tim,
Christian Lamparterc772a082008-11-29 22:33:57 +01001858 .sta_notify_ps = p54_sta_notify_ps,
1859 .sta_notify = p54_sta_notify,
Michael Wueff1a592007-09-25 18:11:01 -07001860 .config = p54_config,
1861 .config_interface = p54_config_interface,
Christian Lamparter40333e42008-10-08 20:52:22 +02001862 .bss_info_changed = p54_bss_info_changed,
Johannes Berg4150c572007-09-17 01:29:23 -04001863 .configure_filter = p54_configure_filter,
Michael Wueff1a592007-09-25 18:11:01 -07001864 .conf_tx = p54_conf_tx,
1865 .get_stats = p54_get_stats,
1866 .get_tx_stats = p54_get_tx_stats
1867};
1868
1869struct ieee80211_hw *p54_init_common(size_t priv_data_len)
1870{
1871 struct ieee80211_hw *dev;
1872 struct p54_common *priv;
Michael Wueff1a592007-09-25 18:11:01 -07001873
1874 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
1875 if (!dev)
1876 return NULL;
1877
1878 priv = dev->priv;
Johannes Berg05c914f2008-09-11 00:01:58 +02001879 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
Christian Lamparterced09572008-11-14 19:42:39 +01001880 priv->basic_rate_mask = 0x15f;
Michael Wueff1a592007-09-25 18:11:01 -07001881 skb_queue_head_init(&priv->tx_queue);
Christian Lamparter94585b02008-10-18 23:18:44 +02001882 dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Christian Lampartercc6de662008-09-06 02:56:23 +02001883 IEEE80211_HW_SIGNAL_DBM |
1884 IEEE80211_HW_NOISE_DBM;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001885
Christian Lamparterd131bb52008-11-15 17:02:31 +01001886 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1887 BIT(NL80211_IFTYPE_ADHOC) |
1888 BIT(NL80211_IFTYPE_AP) |
1889 BIT(NL80211_IFTYPE_MESH_POINT);
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001890
Michael Wueff1a592007-09-25 18:11:01 -07001891 dev->channel_change_time = 1000; /* TODO: find actual value */
Christian Lamparter9e7f3f82008-10-18 23:18:01 +02001892 priv->tx_stats[0].limit = 1; /* Beacon queue */
1893 priv->tx_stats[1].limit = 1; /* Probe queue for HW scan */
1894 priv->tx_stats[2].limit = 3; /* queue for MLMEs */
1895 priv->tx_stats[3].limit = 3; /* Broadcast / MC queue */
1896 priv->tx_stats[4].limit = 5; /* Data */
Michael Wueff1a592007-09-25 18:11:01 -07001897 dev->queues = 1;
Christian Lampartercc6de662008-09-06 02:56:23 +02001898 priv->noise = -94;
Johannes Bergc12abae2008-10-14 16:56:51 +02001899 /*
1900 * We support at most 8 tries no matter which rate they're at,
1901 * we cannot support max_rates * max_rate_tries as we set it
1902 * here, but setting it correctly to 4/2 or so would limit us
1903 * artificially if the RC algorithm wants just two rates, so
1904 * let's say 4/7, we'll redistribute it at TX time, see the
1905 * comments there.
1906 */
1907 dev->max_rates = 4;
1908 dev->max_rate_tries = 7;
John W. Linville27df6052008-10-22 16:41:55 -04001909 dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
1910 sizeof(struct p54_tx_data);
Michael Wueff1a592007-09-25 18:11:01 -07001911
Larry Finger6041e2a2008-08-03 17:58:36 -05001912 mutex_init(&priv->conf_mutex);
Christian Lamparter7cb77072008-09-01 22:48:51 +02001913 init_completion(&priv->eeprom_comp);
Christian Lampartercc6de662008-09-06 02:56:23 +02001914 init_completion(&priv->stats_comp);
1915 setup_timer(&priv->stats_timer, p54_statistics_timer,
1916 (unsigned long)dev);
Michael Wueff1a592007-09-25 18:11:01 -07001917
Michael Wueff1a592007-09-25 18:11:01 -07001918 return dev;
1919}
1920EXPORT_SYMBOL_GPL(p54_init_common);
1921
1922void p54_free_common(struct ieee80211_hw *dev)
1923{
1924 struct p54_common *priv = dev->priv;
Christian Lamparterb92f30d2008-10-15 04:07:16 +02001925 del_timer(&priv->stats_timer);
1926 kfree_skb(priv->cached_stats);
Michael Wueff1a592007-09-25 18:11:01 -07001927 kfree(priv->iq_autocal);
1928 kfree(priv->output_limit);
1929 kfree(priv->curve_data);
Michael Wueff1a592007-09-25 18:11:01 -07001930}
1931EXPORT_SYMBOL_GPL(p54_free_common);
1932
1933static int __init p54_init(void)
1934{
1935 return 0;
1936}
1937
1938static void __exit p54_exit(void)
1939{
1940}
1941
1942module_init(p54_init);
1943module_exit(p54_exit);