libertas: make some functions void
[linux-2.6-block.git] / drivers / net / wireless / libertas / main.c
CommitLineData
876c9d3a
MT
1/**
2 * This file contains the major functions in WLAN
3 * driver. It includes init, exit, open, close and main
4 * thread etc..
5 */
6
a46c6410 7#include <linux/moduleparam.h>
876c9d3a 8#include <linux/delay.h>
876c9d3a
MT
9#include <linux/etherdevice.h>
10#include <linux/netdevice.h>
11#include <linux/if_arp.h>
fe336150 12#include <linux/kthread.h>
7919b89c 13#include <linux/kfifo.h>
876c9d3a
MT
14
15#include <net/iw_handler.h>
9012b28a 16#include <net/ieee80211.h>
876c9d3a
MT
17
18#include "host.h"
876c9d3a
MT
19#include "decl.h"
20#include "dev.h"
876c9d3a
MT
21#include "wext.h"
22#include "debugfs.h"
245bf20f 23#include "scan.h"
876c9d3a 24#include "assoc.h"
6e66f03f 25#include "cmd.h"
876c9d3a 26
7856a541 27#define DRIVER_RELEASE_VERSION "323.p0"
10078321 28const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
3ce40232
DW
29#ifdef DEBUG
30 "-dbg"
31#endif
32 "";
33
a46c6410
HS
34
35/* Module parameters */
10078321
HS
36unsigned int lbs_debug;
37EXPORT_SYMBOL_GPL(lbs_debug);
38module_param_named(libertas_debug, lbs_debug, int, 0644);
a46c6410
HS
39
40
f539f2ef
HS
41/* This global structure is used to send the confirm_sleep command as
42 * fast as possible down to the firmware. */
43struct cmd_confirm_sleep confirm_sleep;
44
45
10078321
HS
46#define LBS_TX_PWR_DEFAULT 20 /*100mW */
47#define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
48#define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
49#define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
50#define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
876c9d3a
MT
51
52/* Format { channel, frequency (MHz), maxtxpower } */
53/* band: 'B/G', region: USA FCC/Canada IC */
54static struct chan_freq_power channel_freq_power_US_BG[] = {
10078321
HS
55 {1, 2412, LBS_TX_PWR_US_DEFAULT},
56 {2, 2417, LBS_TX_PWR_US_DEFAULT},
57 {3, 2422, LBS_TX_PWR_US_DEFAULT},
58 {4, 2427, LBS_TX_PWR_US_DEFAULT},
59 {5, 2432, LBS_TX_PWR_US_DEFAULT},
60 {6, 2437, LBS_TX_PWR_US_DEFAULT},
61 {7, 2442, LBS_TX_PWR_US_DEFAULT},
62 {8, 2447, LBS_TX_PWR_US_DEFAULT},
63 {9, 2452, LBS_TX_PWR_US_DEFAULT},
64 {10, 2457, LBS_TX_PWR_US_DEFAULT},
65 {11, 2462, LBS_TX_PWR_US_DEFAULT}
876c9d3a
MT
66};
67
68/* band: 'B/G', region: Europe ETSI */
69static struct chan_freq_power channel_freq_power_EU_BG[] = {
10078321
HS
70 {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
71 {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
72 {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
73 {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
74 {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
75 {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
76 {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
77 {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
78 {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
79 {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
80 {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
81 {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
82 {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
876c9d3a
MT
83};
84
85/* band: 'B/G', region: Spain */
86static struct chan_freq_power channel_freq_power_SPN_BG[] = {
10078321
HS
87 {10, 2457, LBS_TX_PWR_DEFAULT},
88 {11, 2462, LBS_TX_PWR_DEFAULT}
876c9d3a
MT
89};
90
91/* band: 'B/G', region: France */
92static struct chan_freq_power channel_freq_power_FR_BG[] = {
10078321
HS
93 {10, 2457, LBS_TX_PWR_FR_DEFAULT},
94 {11, 2462, LBS_TX_PWR_FR_DEFAULT},
95 {12, 2467, LBS_TX_PWR_FR_DEFAULT},
96 {13, 2472, LBS_TX_PWR_FR_DEFAULT}
876c9d3a
MT
97};
98
99/* band: 'B/G', region: Japan */
100static struct chan_freq_power channel_freq_power_JPN_BG[] = {
10078321
HS
101 {1, 2412, LBS_TX_PWR_JP_DEFAULT},
102 {2, 2417, LBS_TX_PWR_JP_DEFAULT},
103 {3, 2422, LBS_TX_PWR_JP_DEFAULT},
104 {4, 2427, LBS_TX_PWR_JP_DEFAULT},
105 {5, 2432, LBS_TX_PWR_JP_DEFAULT},
106 {6, 2437, LBS_TX_PWR_JP_DEFAULT},
107 {7, 2442, LBS_TX_PWR_JP_DEFAULT},
108 {8, 2447, LBS_TX_PWR_JP_DEFAULT},
109 {9, 2452, LBS_TX_PWR_JP_DEFAULT},
110 {10, 2457, LBS_TX_PWR_JP_DEFAULT},
111 {11, 2462, LBS_TX_PWR_JP_DEFAULT},
112 {12, 2467, LBS_TX_PWR_JP_DEFAULT},
113 {13, 2472, LBS_TX_PWR_JP_DEFAULT},
114 {14, 2484, LBS_TX_PWR_JP_DEFAULT}
876c9d3a
MT
115};
116
117/**
118 * the structure for channel, frequency and power
119 */
120struct region_cfp_table {
121 u8 region;
122 struct chan_freq_power *cfp_BG;
123 int cfp_no_BG;
124};
125
126/**
127 * the structure for the mapping between region and CFP
128 */
129static struct region_cfp_table region_cfp_table[] = {
130 {0x10, /*US FCC */
131 channel_freq_power_US_BG,
ff8ac609 132 ARRAY_SIZE(channel_freq_power_US_BG),
876c9d3a
MT
133 }
134 ,
135 {0x20, /*CANADA IC */
136 channel_freq_power_US_BG,
ff8ac609 137 ARRAY_SIZE(channel_freq_power_US_BG),
876c9d3a
MT
138 }
139 ,
140 {0x30, /*EU*/ channel_freq_power_EU_BG,
ff8ac609 141 ARRAY_SIZE(channel_freq_power_EU_BG),
876c9d3a
MT
142 }
143 ,
144 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
ff8ac609 145 ARRAY_SIZE(channel_freq_power_SPN_BG),
876c9d3a
MT
146 }
147 ,
148 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
ff8ac609 149 ARRAY_SIZE(channel_freq_power_FR_BG),
876c9d3a
MT
150 }
151 ,
152 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
ff8ac609 153 ARRAY_SIZE(channel_freq_power_JPN_BG),
876c9d3a
MT
154 }
155 ,
156/*Add new region here */
157};
158
876c9d3a 159/**
8c512765 160 * the table to keep region code
876c9d3a 161 */
10078321 162u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
8c512765 163 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
876c9d3a
MT
164
165/**
8c512765 166 * 802.11b/g supported bitrates (in 500Kb/s units)
876c9d3a 167 */
10078321 168u8 lbs_bg_rates[MAX_RATES] =
8c512765
DW
169 { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
1700x00, 0x00 };
876c9d3a
MT
171
172/**
8c512765
DW
173 * FW rate table. FW refers to rates by their index in this table, not by the
174 * rate value itself. Values of 0x00 are
175 * reserved positions.
876c9d3a 176 */
8c512765
DW
177static u8 fw_data_rates[MAX_RATES] =
178 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
179 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
180};
876c9d3a 181
876c9d3a 182/**
8c512765
DW
183 * @brief use index to get the data rate
184 *
185 * @param idx The index of data rate
186 * @return data rate or 0
876c9d3a 187 */
10078321 188u32 lbs_fw_index_to_data_rate(u8 idx)
8c512765
DW
189{
190 if (idx >= sizeof(fw_data_rates))
191 idx = 0;
192 return fw_data_rates[idx];
193}
194
195/**
196 * @brief use rate to get the index
197 *
198 * @param rate data rate
199 * @return index or 0
200 */
10078321 201u8 lbs_data_rate_to_fw_index(u32 rate)
8c512765
DW
202{
203 u8 i;
204
205 if (!rate)
206 return 0;
207
208 for (i = 0; i < sizeof(fw_data_rates); i++) {
209 if (rate == fw_data_rates[i])
210 return i;
211 }
212 return 0;
213}
876c9d3a 214
876c9d3a
MT
215/**
216 * Attributes exported through sysfs
217 */
876c9d3a
MT
218
219/**
82fde74b 220 * @brief Get function for sysfs attribute anycast_mask
876c9d3a 221 */
10078321 222static ssize_t lbs_anycast_get(struct device *dev,
b59bb616
DW
223 struct device_attribute *attr, char * buf)
224{
301eacbf 225 struct lbs_private *priv = to_net_dev(dev)->priv;
876c9d3a 226 struct cmd_ds_mesh_access mesh_access;
301eacbf 227 int ret;
876c9d3a
MT
228
229 memset(&mesh_access, 0, sizeof(mesh_access));
301eacbf
DW
230
231 ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
232 if (ret)
233 return ret;
876c9d3a 234
82fde74b 235 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
876c9d3a
MT
236}
237
238/**
82fde74b 239 * @brief Set function for sysfs attribute anycast_mask
876c9d3a 240 */
10078321 241static ssize_t lbs_anycast_set(struct device *dev,
b59bb616
DW
242 struct device_attribute *attr, const char * buf, size_t count)
243{
301eacbf 244 struct lbs_private *priv = to_net_dev(dev)->priv;
876c9d3a 245 struct cmd_ds_mesh_access mesh_access;
981f187b 246 uint32_t datum;
301eacbf 247 int ret;
876c9d3a 248
876c9d3a 249 memset(&mesh_access, 0, sizeof(mesh_access));
82fde74b 250 sscanf(buf, "%x", &datum);
981f187b
DW
251 mesh_access.data[0] = cpu_to_le32(datum);
252
301eacbf
DW
253 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
254 if (ret)
255 return ret;
256
876c9d3a
MT
257 return strlen(buf);
258}
259
2fd6cfe3
DW
260static int lbs_add_rtap(struct lbs_private *priv);
261static void lbs_remove_rtap(struct lbs_private *priv);
23a397ac
DW
262static int lbs_add_mesh(struct lbs_private *priv);
263static void lbs_remove_mesh(struct lbs_private *priv);
7e226272 264
965f8bbc
LCC
265
266/**
267 * Get function for sysfs attribute rtap
268 */
10078321 269static ssize_t lbs_rtap_get(struct device *dev,
965f8bbc
LCC
270 struct device_attribute *attr, char * buf)
271{
e7deced0 272 struct lbs_private *priv = to_net_dev(dev)->priv;
aa21c004 273 return snprintf(buf, 5, "0x%X\n", priv->monitormode);
965f8bbc
LCC
274}
275
276/**
277 * Set function for sysfs attribute rtap
278 */
10078321 279static ssize_t lbs_rtap_set(struct device *dev,
965f8bbc
LCC
280 struct device_attribute *attr, const char * buf, size_t count)
281{
282 int monitor_mode;
e7deced0 283 struct lbs_private *priv = to_net_dev(dev)->priv;
965f8bbc
LCC
284
285 sscanf(buf, "%x", &monitor_mode);
c2b310a7
HS
286 if (monitor_mode) {
287 if (priv->monitormode == monitor_mode)
965f8bbc 288 return strlen(buf);
c2b310a7 289 if (!priv->monitormode) {
8552855f
DW
290 if (priv->infra_open || priv->mesh_open)
291 return -EBUSY;
aa21c004 292 if (priv->mode == IW_MODE_INFRA)
10078321 293 lbs_send_deauthentication(priv);
aa21c004 294 else if (priv->mode == IW_MODE_ADHOC)
10078321
HS
295 lbs_stop_adhoc_network(priv);
296 lbs_add_rtap(priv);
965f8bbc 297 }
aa21c004 298 priv->monitormode = monitor_mode;
965f8bbc
LCC
299 }
300
301 else {
c2b310a7 302 if (!priv->monitormode)
965f8bbc 303 return strlen(buf);
c2b310a7 304 priv->monitormode = 0;
10078321 305 lbs_remove_rtap(priv);
f86a93e1 306
aa21c004
DW
307 if (priv->currenttxskb) {
308 dev_kfree_skb_any(priv->currenttxskb);
309 priv->currenttxskb = NULL;
f86a93e1
DW
310 }
311
312 /* Wake queues, command thread, etc. */
313 lbs_host_to_card_done(priv);
965f8bbc
LCC
314 }
315
10078321 316 lbs_prepare_and_send_command(priv,
965f8bbc 317 CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
aa21c004 318 CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
965f8bbc
LCC
319 return strlen(buf);
320}
321
322/**
23a397ac
DW
323 * lbs_rtap attribute to be exported per ethX interface
324 * through sysfs (/sys/class/net/ethX/lbs_rtap)
965f8bbc 325 */
23a397ac
DW
326static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
327
328/**
329 * Get function for sysfs attribute mesh
330 */
331static ssize_t lbs_mesh_get(struct device *dev,
332 struct device_attribute *attr, char * buf)
333{
334 struct lbs_private *priv = to_net_dev(dev)->priv;
335 return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
336}
337
338/**
339 * Set function for sysfs attribute mesh
340 */
341static ssize_t lbs_mesh_set(struct device *dev,
342 struct device_attribute *attr, const char * buf, size_t count)
343{
344 struct lbs_private *priv = to_net_dev(dev)->priv;
345 int enable;
346 int ret;
347
348 sscanf(buf, "%x", &enable);
349 enable = !!enable;
350 if (enable == !!priv->mesh_dev)
351 return count;
352
86062134 353 ret = lbs_mesh_config(priv, enable, priv->curbssparams.channel);
23a397ac
DW
354 if (ret)
355 return ret;
7e226272 356
23a397ac
DW
357 if (enable)
358 lbs_add_mesh(priv);
359 else
360 lbs_remove_mesh(priv);
361
362 return count;
363}
364
365/**
366 * lbs_mesh attribute to be exported per ethX interface
367 * through sysfs (/sys/class/net/ethX/lbs_mesh)
368 */
369static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
965f8bbc 370
876c9d3a 371/**
82fde74b
LC
372 * anycast_mask attribute to be exported per mshX interface
373 * through sysfs (/sys/class/net/mshX/anycast_mask)
876c9d3a 374 */
10078321 375static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
876c9d3a 376
10078321 377static struct attribute *lbs_mesh_sysfs_entries[] = {
a9f38d02 378 &dev_attr_anycast_mask.attr,
a9f38d02
LCC
379 NULL,
380};
381
10078321
HS
382static struct attribute_group lbs_mesh_attr_group = {
383 .attrs = lbs_mesh_sysfs_entries,
a9f38d02
LCC
384};
385
876c9d3a 386/**
8552855f 387 * @brief This function opens the ethX or mshX interface
876c9d3a
MT
388 *
389 * @param dev A pointer to net_device structure
8552855f 390 * @return 0 or -EBUSY if monitor mode active
876c9d3a 391 */
10078321 392static int lbs_dev_open(struct net_device *dev)
876c9d3a 393{
8552855f
DW
394 struct lbs_private *priv = (struct lbs_private *) dev->priv ;
395 int ret = 0;
876c9d3a 396
61d30020
HS
397 lbs_deb_enter(LBS_DEB_NET);
398
8552855f 399 spin_lock_irq(&priv->driver_lock);
876c9d3a 400
c2b310a7 401 if (priv->monitormode) {
8552855f
DW
402 ret = -EBUSY;
403 goto out;
404 }
01d77d8d 405
8552855f
DW
406 if (dev == priv->mesh_dev) {
407 priv->mesh_open = 1;
408 priv->mesh_connect_status = LBS_CONNECTED;
409 netif_carrier_on(dev);
410 } else {
411 priv->infra_open = 1;
7e226272 412
8552855f
DW
413 if (priv->connect_status == LBS_CONNECTED)
414 netif_carrier_on(dev);
01d77d8d 415 else
8552855f 416 netif_carrier_off(dev);
51d84f50 417 }
876c9d3a 418
8552855f
DW
419 if (!priv->tx_pending_len)
420 netif_wake_queue(dev);
421 out:
876c9d3a 422
8552855f 423 spin_unlock_irq(&priv->driver_lock);
61d30020 424 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
8552855f 425 return ret;
876c9d3a
MT
426}
427
428/**
429 * @brief This function closes the mshX interface
430 *
431 * @param dev A pointer to net_device structure
432 * @return 0
433 */
8552855f 434static int lbs_mesh_stop(struct net_device *dev)
876c9d3a 435{
69f9032d 436 struct lbs_private *priv = (struct lbs_private *) (dev->priv);
876c9d3a 437
61d30020 438 lbs_deb_enter(LBS_DEB_MESH);
8552855f
DW
439 spin_lock_irq(&priv->driver_lock);
440
876c9d3a 441 priv->mesh_open = 0;
8552855f
DW
442 priv->mesh_connect_status = LBS_DISCONNECTED;
443
444 netif_stop_queue(dev);
445 netif_carrier_off(dev);
7e226272 446
8552855f 447 spin_unlock_irq(&priv->driver_lock);
61d30020
HS
448
449 lbs_deb_leave(LBS_DEB_MESH);
8552855f 450 return 0;
876c9d3a
MT
451}
452
453/**
454 * @brief This function closes the ethX interface
455 *
456 * @param dev A pointer to net_device structure
457 * @return 0
458 */
8552855f 459static int lbs_eth_stop(struct net_device *dev)
78523daa 460{
69f9032d 461 struct lbs_private *priv = (struct lbs_private *) dev->priv;
876c9d3a 462
61d30020 463 lbs_deb_enter(LBS_DEB_NET);
8552855f 464
61d30020 465 spin_lock_irq(&priv->driver_lock);
876c9d3a 466 priv->infra_open = 0;
8552855f 467 netif_stop_queue(dev);
8552855f 468 spin_unlock_irq(&priv->driver_lock);
61d30020
HS
469
470 lbs_deb_leave(LBS_DEB_NET);
8552855f 471 return 0;
876c9d3a
MT
472}
473
10078321 474static void lbs_tx_timeout(struct net_device *dev)
876c9d3a 475{
69f9032d 476 struct lbs_private *priv = (struct lbs_private *) dev->priv;
876c9d3a 477
9012b28a 478 lbs_deb_enter(LBS_DEB_TX);
876c9d3a 479
9012b28a 480 lbs_pr_err("tx watch dog timeout\n");
876c9d3a 481
876c9d3a
MT
482 dev->trans_start = jiffies;
483
7919b89c
HS
484 if (priv->currenttxskb)
485 lbs_send_tx_feedback(priv, 0);
486
a63b22bb
DW
487 /* XX: Shouldn't we also call into the hw-specific driver
488 to kick it somehow? */
489 lbs_host_to_card_done(priv);
876c9d3a 490
354eca98
DW
491 /* More often than not, this actually happens because the
492 firmware has crapped itself -- rather than just a very
493 busy medium. So send a harmless command, and if/when
494 _that_ times out, we'll kick it in the head. */
495 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
496 0, 0, NULL);
497
9012b28a 498 lbs_deb_leave(LBS_DEB_TX);
876c9d3a
MT
499}
500
e775ed7c
DW
501void lbs_host_to_card_done(struct lbs_private *priv)
502{
a63b22bb
DW
503 unsigned long flags;
504
61d30020
HS
505 lbs_deb_enter(LBS_DEB_THREAD);
506
a63b22bb 507 spin_lock_irqsave(&priv->driver_lock, flags);
e775ed7c
DW
508
509 priv->dnld_sent = DNLD_RES_RECEIVED;
510
511 /* Wake main thread if commands are pending */
a27b9f96 512 if (!priv->cur_cmd || priv->tx_pending_len > 0)
e775ed7c
DW
513 wake_up_interruptible(&priv->waitq);
514
a63b22bb 515 spin_unlock_irqrestore(&priv->driver_lock, flags);
61d30020 516 lbs_deb_leave(LBS_DEB_THREAD);
e775ed7c
DW
517}
518EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
519
876c9d3a
MT
520/**
521 * @brief This function returns the network statistics
522 *
69f9032d 523 * @param dev A pointer to struct lbs_private structure
876c9d3a
MT
524 * @return A pointer to net_device_stats structure
525 */
10078321 526static struct net_device_stats *lbs_get_stats(struct net_device *dev)
876c9d3a 527{
69f9032d 528 struct lbs_private *priv = (struct lbs_private *) dev->priv;
876c9d3a 529
61d30020 530 lbs_deb_enter(LBS_DEB_NET);
876c9d3a
MT
531 return &priv->stats;
532}
533
10078321 534static int lbs_set_mac_address(struct net_device *dev, void *addr)
876c9d3a
MT
535{
536 int ret = 0;
69f9032d 537 struct lbs_private *priv = (struct lbs_private *) dev->priv;
876c9d3a 538 struct sockaddr *phwaddr = addr;
2af9f039 539 struct cmd_ds_802_11_mac_address cmd;
876c9d3a 540
9012b28a 541 lbs_deb_enter(LBS_DEB_NET);
876c9d3a 542
0a0d08ac 543 /* In case it was called from the mesh device */
2af9f039 544 dev = priv->dev;
0a0d08ac 545
2af9f039
HS
546 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
547 cmd.action = cpu_to_le16(CMD_ACT_SET);
548 memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
876c9d3a 549
2af9f039 550 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
876c9d3a 551 if (ret) {
9012b28a 552 lbs_deb_net("set MAC address failed\n");
876c9d3a
MT
553 goto done;
554 }
555
2af9f039
HS
556 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
557 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
78523daa 558 if (priv->mesh_dev)
2af9f039 559 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
876c9d3a
MT
560
561done:
9012b28a 562 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
876c9d3a
MT
563 return ret;
564}
565
aa21c004 566static int lbs_copy_multicast_address(struct lbs_private *priv,
876c9d3a
MT
567 struct net_device *dev)
568{
569 int i = 0;
570 struct dev_mc_list *mcptr = dev->mc_list;
571
572 for (i = 0; i < dev->mc_count; i++) {
aa21c004 573 memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
876c9d3a
MT
574 mcptr = mcptr->next;
575 }
876c9d3a 576 return i;
876c9d3a
MT
577}
578
10078321 579static void lbs_set_multicast_list(struct net_device *dev)
876c9d3a 580{
69f9032d 581 struct lbs_private *priv = dev->priv;
d9e9778c 582 int old_mac_control;
0795af57 583 DECLARE_MAC_BUF(mac);
876c9d3a 584
9012b28a 585 lbs_deb_enter(LBS_DEB_NET);
876c9d3a 586
d9e9778c 587 old_mac_control = priv->mac_control;
876c9d3a
MT
588
589 if (dev->flags & IFF_PROMISC) {
9012b28a 590 lbs_deb_net("enable promiscuous mode\n");
d9e9778c 591 priv->mac_control |=
0aef64d7 592 CMD_ACT_MAC_PROMISCUOUS_ENABLE;
d9e9778c 593 priv->mac_control &=
0aef64d7
DW
594 ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
595 CMD_ACT_MAC_MULTICAST_ENABLE);
876c9d3a
MT
596 } else {
597 /* Multicast */
d9e9778c 598 priv->mac_control &=
0aef64d7 599 ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
876c9d3a
MT
600
601 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
602 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
9012b28a 603 lbs_deb_net( "enabling all multicast\n");
d9e9778c 604 priv->mac_control |=
0aef64d7 605 CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
d9e9778c 606 priv->mac_control &=
0aef64d7 607 ~CMD_ACT_MAC_MULTICAST_ENABLE;
876c9d3a 608 } else {
d9e9778c 609 priv->mac_control &=
0aef64d7 610 ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
876c9d3a
MT
611
612 if (!dev->mc_count) {
9012b28a
HS
613 lbs_deb_net("no multicast addresses, "
614 "disabling multicast\n");
d9e9778c 615 priv->mac_control &=
0aef64d7 616 ~CMD_ACT_MAC_MULTICAST_ENABLE;
876c9d3a
MT
617 } else {
618 int i;
619
d9e9778c 620 priv->mac_control |=
0aef64d7 621 CMD_ACT_MAC_MULTICAST_ENABLE;
876c9d3a 622
aa21c004
DW
623 priv->nr_of_multicastmacaddr =
624 lbs_copy_multicast_address(priv, dev);
876c9d3a 625
9012b28a 626 lbs_deb_net("multicast addresses: %d\n",
876c9d3a
MT
627 dev->mc_count);
628
629 for (i = 0; i < dev->mc_count; i++) {
61d30020 630 lbs_deb_net("Multicast address %d: %s\n",
0795af57 631 i, print_mac(mac,
aa21c004 632 priv->multicastlist[i]));
876c9d3a 633 }
9012b28a 634 /* send multicast addresses to firmware */
10078321 635 lbs_prepare_and_send_command(priv,
0aef64d7
DW
636 CMD_MAC_MULTICAST_ADR,
637 CMD_ACT_SET, 0, 0,
876c9d3a
MT
638 NULL);
639 }
640 }
641 }
642
d9e9778c
HS
643 if (priv->mac_control != old_mac_control)
644 lbs_set_mac_control(priv);
876c9d3a 645
9012b28a 646 lbs_deb_leave(LBS_DEB_NET);
876c9d3a
MT
647}
648
649/**
10078321 650 * @brief This function handles the major jobs in the LBS driver.
9012b28a
HS
651 * It handles all events generated by firmware, RX data received
652 * from firmware and TX data sent from kernel.
876c9d3a 653 *
10078321 654 * @param data A pointer to lbs_thread structure
876c9d3a
MT
655 * @return 0
656 */
10078321 657static int lbs_thread(void *data)
876c9d3a 658{
fe336150 659 struct net_device *dev = data;
69f9032d 660 struct lbs_private *priv = dev->priv;
876c9d3a 661 wait_queue_t wait;
876c9d3a 662
9012b28a 663 lbs_deb_enter(LBS_DEB_THREAD);
876c9d3a 664
876c9d3a
MT
665 init_waitqueue_entry(&wait, current);
666
667 for (;;) {
b8d40bc9 668 int shouldsleep;
7919b89c 669 u8 resp_idx;
b8d40bc9 670
7919b89c
HS
671 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
672 priv->currenttxskb, priv->dnld_sent);
876c9d3a 673
fe336150 674 add_wait_queue(&priv->waitq, &wait);
876c9d3a 675 set_current_state(TASK_INTERRUPTIBLE);
aa21c004 676 spin_lock_irq(&priv->driver_lock);
59f3e4bf 677
d9f88705 678 if (kthread_should_stop())
b8d40bc9 679 shouldsleep = 0; /* Bye */
d9f88705
DW
680 else if (priv->surpriseremoved)
681 shouldsleep = 1; /* We need to wait until we're _told_ to die */
b8d40bc9
DW
682 else if (priv->psstate == PS_STATE_SLEEP)
683 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
2a345099
DW
684 else if (priv->cmd_timed_out)
685 shouldsleep = 0; /* Command timed out. Recover */
b15152a4
DW
686 else if (!priv->fw_ready)
687 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
b8d40bc9
DW
688 else if (priv->dnld_sent)
689 shouldsleep = 1; /* Something is en route to the device already */
2eb188a1
DW
690 else if (priv->tx_pending_len > 0)
691 shouldsleep = 0; /* We've a packet to send */
b8d40bc9
DW
692 else if (priv->cur_cmd)
693 shouldsleep = 1; /* Can't send a command; one already running */
694 else if (!list_empty(&priv->cmdpendingq))
695 shouldsleep = 0; /* We have a command to send */
7919b89c
HS
696 else if (__kfifo_len(priv->event_fifo))
697 shouldsleep = 0; /* We have an event to process */
698 else if (priv->resp_len[priv->resp_idx])
699 shouldsleep = 0; /* We have a command response */
b8d40bc9
DW
700 else
701 shouldsleep = 1; /* No command */
702
703 if (shouldsleep) {
7919b89c 704 lbs_deb_thread("sleeping, connect_status %d, "
5f505d90 705 "psmode %d, psstate %d\n",
7919b89c
HS
706 priv->connect_status,
707 priv->psmode, priv->psstate);
aa21c004 708 spin_unlock_irq(&priv->driver_lock);
876c9d3a
MT
709 schedule();
710 } else
aa21c004 711 spin_unlock_irq(&priv->driver_lock);
876c9d3a 712
7919b89c
HS
713 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
714 priv->currenttxskb, priv->dnld_sent);
876c9d3a
MT
715
716 set_current_state(TASK_RUNNING);
fe336150 717 remove_wait_queue(&priv->waitq, &wait);
876c9d3a 718
7919b89c
HS
719 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
720 priv->currenttxskb, priv->dnld_sent);
876c9d3a 721
d9f88705 722 if (kthread_should_stop()) {
7919b89c 723 lbs_deb_thread("break from main thread\n");
876c9d3a
MT
724 break;
725 }
726
d9f88705
DW
727 if (priv->surpriseremoved) {
728 lbs_deb_thread("adapter removed; waiting to die...\n");
729 continue;
730 }
876c9d3a 731
7919b89c
HS
732 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
733 priv->currenttxskb, priv->dnld_sent);
59f3e4bf 734
7919b89c
HS
735 spin_lock_irq(&priv->driver_lock);
736 /* Process any pending command response */
737 resp_idx = priv->resp_idx;
738 if (priv->resp_len[resp_idx]) {
aa21c004 739 spin_unlock_irq(&priv->driver_lock);
7919b89c
HS
740 lbs_process_command_response(priv,
741 priv->resp_buf[resp_idx],
742 priv->resp_len[resp_idx]);
aa21c004 743 spin_lock_irq(&priv->driver_lock);
7919b89c 744 priv->resp_len[resp_idx] = 0;
876c9d3a 745 }
7919b89c 746 spin_unlock_irq(&priv->driver_lock);
876c9d3a 747
7919b89c 748 /* command timeout stuff */
2a345099
DW
749 if (priv->cmd_timed_out && priv->cur_cmd) {
750 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
751
752 if (++priv->nr_retries > 10) {
753 lbs_pr_info("Excessive timeouts submitting command %x\n",
754 le16_to_cpu(cmdnode->cmdbuf->command));
755 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
756 priv->nr_retries = 0;
757 } else {
758 priv->cur_cmd = NULL;
759 lbs_pr_info("requeueing command %x due to timeout (#%d)\n",
760 le16_to_cpu(cmdnode->cmdbuf->command), priv->nr_retries);
761
762 /* Stick it back at the _top_ of the pending queue
763 for immediate resubmission */
764 list_add(&cmdnode->list, &priv->cmdpendingq);
765 }
766 }
767 priv->cmd_timed_out = 0;
768
7919b89c
HS
769 /* Process hardware events, e.g. card removed, link lost */
770 spin_lock_irq(&priv->driver_lock);
771 while (__kfifo_len(priv->event_fifo)) {
772 u32 event;
876c9d3a 773
7919b89c
HS
774 __kfifo_get(priv->event_fifo, (unsigned char *) &event,
775 sizeof(event));
aa21c004 776 spin_unlock_irq(&priv->driver_lock);
7919b89c
HS
777 lbs_process_event(priv, event);
778 spin_lock_irq(&priv->driver_lock);
779 }
780 spin_unlock_irq(&priv->driver_lock);
876c9d3a 781
b15152a4
DW
782 if (!priv->fw_ready)
783 continue;
784
876c9d3a 785 /* Check if we need to confirm Sleep Request received previously */
aa21c004
DW
786 if (priv->psstate == PS_STATE_PRE_SLEEP &&
787 !priv->dnld_sent && !priv->cur_cmd) {
788 if (priv->connect_status == LBS_CONNECTED) {
7919b89c
HS
789 lbs_deb_thread("pre-sleep, currenttxskb %p, "
790 "dnld_sent %d, cur_cmd %p\n",
791 priv->currenttxskb, priv->dnld_sent,
792 priv->cur_cmd);
59f3e4bf 793
d4ff0ef6 794 lbs_ps_confirm_sleep(priv);
59f3e4bf
DW
795 } else {
796 /* workaround for firmware sending
797 * deauth/linkloss event immediately
798 * after sleep request; remove this
799 * after firmware fixes it
800 */
aa21c004 801 priv->psstate = PS_STATE_AWAKE;
7919b89c
HS
802 lbs_pr_alert("ignore PS_SleepConfirm in "
803 "non-connected state\n");
876c9d3a
MT
804 }
805 }
806
807 /* The PS state is changed during processing of Sleep Request
808 * event above
809 */
aa21c004
DW
810 if ((priv->psstate == PS_STATE_SLEEP) ||
811 (priv->psstate == PS_STATE_PRE_SLEEP))
876c9d3a
MT
812 continue;
813
814 /* Execute the next command */
aa21c004 815 if (!priv->dnld_sent && !priv->cur_cmd)
10078321 816 lbs_execute_next_command(priv);
876c9d3a
MT
817
818 /* Wake-up command waiters which can't sleep in
10078321 819 * lbs_prepare_and_send_command
876c9d3a 820 */
aa21c004
DW
821 if (!list_empty(&priv->cmdpendingq))
822 wake_up_all(&priv->cmd_pending);
2eb188a1
DW
823
824 spin_lock_irq(&priv->driver_lock);
825 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
826 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
827 priv->tx_pending_buf,
828 priv->tx_pending_len);
829 if (ret) {
830 lbs_deb_tx("host_to_card failed %d\n", ret);
831 priv->dnld_sent = DNLD_RES_RECEIVED;
832 }
833 priv->tx_pending_len = 0;
834 if (!priv->currenttxskb) {
835 /* We can wake the queues immediately if we aren't
836 waiting for TX feedback */
837 if (priv->connect_status == LBS_CONNECTED)
838 netif_wake_queue(priv->dev);
839 if (priv->mesh_dev &&
840 priv->mesh_connect_status == LBS_CONNECTED)
841 netif_wake_queue(priv->mesh_dev);
842 }
843 }
844 spin_unlock_irq(&priv->driver_lock);
876c9d3a
MT
845 }
846
aa21c004
DW
847 del_timer(&priv->command_timer);
848 wake_up_all(&priv->cmd_pending);
876c9d3a 849
9012b28a 850 lbs_deb_leave(LBS_DEB_THREAD);
876c9d3a
MT
851 return 0;
852}
853
ab25ecae
DW
854static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
855 struct cmd_header *cmd)
856{
61d30020 857 lbs_deb_enter(LBS_DEB_FW);
ab25ecae
DW
858
859 netif_device_detach(priv->dev);
860 if (priv->mesh_dev)
861 netif_device_detach(priv->mesh_dev);
862
863 priv->fw_ready = 0;
61d30020 864 lbs_deb_leave(LBS_DEB_FW);
ab25ecae
DW
865 return 0;
866}
867
ab25ecae
DW
868int lbs_suspend(struct lbs_private *priv)
869{
870 struct cmd_header cmd;
871 int ret;
872
61d30020
HS
873 lbs_deb_enter(LBS_DEB_FW);
874
506e9025
DW
875 if (priv->wol_criteria == 0xffffffff) {
876 lbs_pr_info("Suspend attempt without configuring wake params!\n");
877 return -EINVAL;
878 }
879
ab25ecae 880 memset(&cmd, 0, sizeof(cmd));
7e226272 881
ab25ecae
DW
882 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
883 sizeof(cmd), lbs_suspend_callback, 0);
884 if (ret)
885 lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
886
61d30020 887 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
ab25ecae
DW
888 return ret;
889}
890EXPORT_SYMBOL_GPL(lbs_suspend);
891
a63e5cb2 892void lbs_resume(struct lbs_private *priv)
ab25ecae 893{
61d30020
HS
894 lbs_deb_enter(LBS_DEB_FW);
895
ab25ecae
DW
896 priv->fw_ready = 1;
897
898 /* Firmware doesn't seem to give us RX packets any more
899 until we send it some command. Might as well update */
900 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
901 0, 0, NULL);
902
903 netif_device_attach(priv->dev);
904 if (priv->mesh_dev)
905 netif_device_attach(priv->mesh_dev);
906
61d30020 907 lbs_deb_leave(LBS_DEB_FW);
ab25ecae
DW
908}
909EXPORT_SYMBOL_GPL(lbs_resume);
910
1df4e8fe
HS
911/**
912 * @brief This function downloads firmware image, gets
913 * HW spec from firmware and set basic parameters to
914 * firmware.
915 *
69f9032d 916 * @param priv A pointer to struct lbs_private structure
1df4e8fe
HS
917 * @return 0 or -1
918 */
69f9032d 919static int lbs_setup_firmware(struct lbs_private *priv)
1df4e8fe
HS
920{
921 int ret = -1;
1df4e8fe
HS
922
923 lbs_deb_enter(LBS_DEB_FW);
924
1df4e8fe
HS
925 /*
926 * Read MAC address from HW
927 */
aa21c004 928 memset(priv->current_addr, 0xff, ETH_ALEN);
6e66f03f 929 ret = lbs_update_hw_spec(priv);
1df4e8fe
HS
930 if (ret) {
931 ret = -1;
932 goto done;
933 }
934
d9e9778c 935 lbs_set_mac_control(priv);
1df4e8fe 936
8e3c91bb
DW
937 ret = lbs_get_data_rate(priv);
938 if (ret < 0) {
1df4e8fe
HS
939 ret = -1;
940 goto done;
941 }
942
943 ret = 0;
944done:
945 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
946 return ret;
947}
948
1df4e8fe
HS
949/**
950 * This function handles the timeout of command sending.
951 * It will re-send the same command again.
952 */
953static void command_timer_fn(unsigned long data)
954{
69f9032d 955 struct lbs_private *priv = (struct lbs_private *)data;
1df4e8fe
HS
956 unsigned long flags;
957
61d30020 958 lbs_deb_enter(LBS_DEB_CMD);
2a345099 959 spin_lock_irqsave(&priv->driver_lock, flags);
1df4e8fe 960
2a345099
DW
961 if (!priv->cur_cmd) {
962 lbs_pr_info("Command timer expired; no pending command\n");
963 goto out;
1df4e8fe
HS
964 }
965
2a345099 966 lbs_pr_info("Command %x timed out\n", le16_to_cpu(priv->cur_cmd->cmdbuf->command));
1df4e8fe 967
2a345099 968 priv->cmd_timed_out = 1;
1df4e8fe 969 wake_up_interruptible(&priv->waitq);
61d30020 970out:
2a345099 971 spin_unlock_irqrestore(&priv->driver_lock, flags);
61d30020 972 lbs_deb_leave(LBS_DEB_CMD);
1df4e8fe
HS
973}
974
e226868e
HS
975static void lbs_sync_channel_worker(struct work_struct *work)
976{
977 struct lbs_private *priv = container_of(work, struct lbs_private,
978 sync_channel);
979
980 lbs_deb_enter(LBS_DEB_MAIN);
981 if (lbs_update_channel(priv))
982 lbs_pr_info("Channel synchronization failed.");
983 lbs_deb_leave(LBS_DEB_MAIN);
984}
985
986
69f9032d 987static int lbs_init_adapter(struct lbs_private *priv)
ac558ca2 988{
1df4e8fe 989 size_t bufsize;
954ee164 990 int i, ret = 0;
1df4e8fe 991
61d30020
HS
992 lbs_deb_enter(LBS_DEB_MAIN);
993
1df4e8fe
HS
994 /* Allocate buffer to store the BSSID list */
995 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
aa21c004
DW
996 priv->networks = kzalloc(bufsize, GFP_KERNEL);
997 if (!priv->networks) {
1df4e8fe 998 lbs_pr_err("Out of memory allocating beacons\n");
954ee164
DW
999 ret = -1;
1000 goto out;
1df4e8fe
HS
1001 }
1002
954ee164 1003 /* Initialize scan result lists */
aa21c004
DW
1004 INIT_LIST_HEAD(&priv->network_free_list);
1005 INIT_LIST_HEAD(&priv->network_list);
954ee164 1006 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
aa21c004
DW
1007 list_add_tail(&priv->networks[i].list,
1008 &priv->network_free_list);
954ee164 1009 }
1df4e8fe 1010
aa21c004 1011 memset(priv->current_addr, 0xff, ETH_ALEN);
1df4e8fe 1012
aa21c004
DW
1013 priv->connect_status = LBS_DISCONNECTED;
1014 priv->mesh_connect_status = LBS_DISCONNECTED;
1015 priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1016 priv->mode = IW_MODE_INFRA;
1017 priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
d9e9778c 1018 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
aa21c004
DW
1019 priv->radioon = RADIO_ON;
1020 priv->auto_rate = 1;
1021 priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1022 priv->psmode = LBS802_11POWERMODECAM;
1023 priv->psstate = PS_STATE_FULL_POWER;
1df4e8fe 1024
aa21c004 1025 mutex_init(&priv->lock);
1df4e8fe 1026
aa21c004 1027 setup_timer(&priv->command_timer, command_timer_fn,
61d30020 1028 (unsigned long)priv);
1df4e8fe 1029
aa21c004
DW
1030 INIT_LIST_HEAD(&priv->cmdfreeq);
1031 INIT_LIST_HEAD(&priv->cmdpendingq);
1df4e8fe 1032
aa21c004
DW
1033 spin_lock_init(&priv->driver_lock);
1034 init_waitqueue_head(&priv->cmd_pending);
1df4e8fe 1035
954ee164 1036 /* Allocate the command buffers */
10078321 1037 if (lbs_allocate_cmd_buffer(priv)) {
954ee164 1038 lbs_pr_err("Out of memory allocating command buffers\n");
7919b89c
HS
1039 ret = -ENOMEM;
1040 goto out;
1041 }
1042 priv->resp_idx = 0;
1043 priv->resp_len[0] = priv->resp_len[1] = 0;
1044
1045 /* Create the event FIFO */
1046 priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
1047 if (IS_ERR(priv->event_fifo)) {
1048 lbs_pr_err("Out of memory allocating event FIFO buffer\n");
1049 ret = -ENOMEM;
1050 goto out;
954ee164 1051 }
1df4e8fe 1052
954ee164 1053out:
61d30020
HS
1054 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1055
954ee164
DW
1056 return ret;
1057}
1df4e8fe 1058
69f9032d 1059static void lbs_free_adapter(struct lbs_private *priv)
954ee164 1060{
61d30020 1061 lbs_deb_enter(LBS_DEB_MAIN);
1df4e8fe 1062
61d30020 1063 lbs_free_cmd_buffer(priv);
7919b89c
HS
1064 if (priv->event_fifo)
1065 kfifo_free(priv->event_fifo);
aa21c004 1066 del_timer(&priv->command_timer);
aa21c004
DW
1067 kfree(priv->networks);
1068 priv->networks = NULL;
61d30020
HS
1069
1070 lbs_deb_leave(LBS_DEB_MAIN);
1df4e8fe
HS
1071}
1072
876c9d3a
MT
1073/**
1074 * @brief This function adds the card. it will probe the
10078321 1075 * card, allocate the lbs_priv and initialize the device.
876c9d3a
MT
1076 *
1077 * @param card A pointer to card
69f9032d 1078 * @return A pointer to struct lbs_private structure
876c9d3a 1079 */
69f9032d 1080struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
876c9d3a
MT
1081{
1082 struct net_device *dev = NULL;
69f9032d 1083 struct lbs_private *priv = NULL;
876c9d3a 1084
61d30020 1085 lbs_deb_enter(LBS_DEB_MAIN);
876c9d3a
MT
1086
1087 /* Allocate an Ethernet device and register it */
69f9032d
HS
1088 dev = alloc_etherdev(sizeof(struct lbs_private));
1089 if (!dev) {
9012b28a 1090 lbs_pr_err("init ethX device failed\n");
954ee164 1091 goto done;
876c9d3a 1092 }
64f104e8 1093 priv = dev->priv;
876c9d3a 1094
10078321 1095 if (lbs_init_adapter(priv)) {
954ee164
DW
1096 lbs_pr_err("failed to initialize adapter structure.\n");
1097 goto err_init_adapter;
1098 }
1099
634b8f49
HS
1100 priv->dev = dev;
1101 priv->card = card;
876c9d3a
MT
1102 priv->mesh_open = 0;
1103 priv->infra_open = 0;
876c9d3a 1104
876c9d3a 1105 /* Setup the OS Interface to our functions */
8552855f 1106 dev->open = lbs_dev_open;
198cefb9 1107 dev->hard_start_xmit = lbs_hard_start_xmit;
8552855f 1108 dev->stop = lbs_eth_stop;
10078321
HS
1109 dev->set_mac_address = lbs_set_mac_address;
1110 dev->tx_timeout = lbs_tx_timeout;
1111 dev->get_stats = lbs_get_stats;
fb3dddf2 1112 dev->watchdog_timeo = 5 * HZ;
10078321 1113 dev->ethtool_ops = &lbs_ethtool_ops;
876c9d3a 1114#ifdef WIRELESS_EXT
10078321 1115 dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
876c9d3a 1116#endif
876c9d3a 1117 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
10078321 1118 dev->set_multicast_list = lbs_set_multicast_list;
876c9d3a 1119
7732ca45
DW
1120 SET_NETDEV_DEV(dev, dmdev);
1121
965f8bbc 1122 priv->rtap_net_dev = NULL;
954ee164
DW
1123
1124 lbs_deb_thread("Starting main thread...\n");
1125 init_waitqueue_head(&priv->waitq);
10078321 1126 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
954ee164
DW
1127 if (IS_ERR(priv->main_thread)) {
1128 lbs_deb_thread("Error creating main thread.\n");
e7deced0 1129 goto err_init_adapter;
954ee164
DW
1130 }
1131
10078321
HS
1132 priv->work_thread = create_singlethread_workqueue("lbs_worker");
1133 INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
1134 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
e226868e 1135 INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
954ee164 1136
23a397ac
DW
1137 sprintf(priv->mesh_ssid, "mesh");
1138 priv->mesh_ssid_len = 4;
1139
506e9025
DW
1140 priv->wol_criteria = 0xffffffff;
1141 priv->wol_gpio = 0xff;
1142
32a74b7c
HS
1143 goto done;
1144
954ee164 1145err_init_adapter:
10078321 1146 lbs_free_adapter(priv);
32a74b7c 1147 free_netdev(dev);
6a812157 1148 priv = NULL;
954ee164 1149
32a74b7c 1150done:
61d30020 1151 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
32a74b7c
HS
1152 return priv;
1153}
10078321 1154EXPORT_SYMBOL_GPL(lbs_add_card);
32a74b7c 1155
954ee164 1156
a63e5cb2 1157void lbs_remove_card(struct lbs_private *priv)
32a74b7c 1158{
634b8f49 1159 struct net_device *dev = priv->dev;
954ee164 1160 union iwreq_data wrqu;
32a74b7c
HS
1161
1162 lbs_deb_enter(LBS_DEB_MAIN);
876c9d3a 1163
23a397ac 1164 lbs_remove_mesh(priv);
10078321 1165 lbs_remove_rtap(priv);
876c9d3a 1166
954ee164 1167 dev = priv->dev;
2afc0c5d 1168
954ee164
DW
1169 cancel_delayed_work(&priv->scan_work);
1170 cancel_delayed_work(&priv->assoc_work);
1171 destroy_workqueue(priv->work_thread);
876c9d3a 1172
aa21c004
DW
1173 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1174 priv->psmode = LBS802_11POWERMODECAM;
10078321 1175 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
876c9d3a
MT
1176 }
1177
954ee164
DW
1178 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1179 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1180 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1181
1182 /* Stop the thread servicing the interrupts */
aa21c004 1183 priv->surpriseremoved = 1;
954ee164
DW
1184 kthread_stop(priv->main_thread);
1185
10078321 1186 lbs_free_adapter(priv);
954ee164
DW
1187
1188 priv->dev = NULL;
1189 free_netdev(dev);
1190
1191 lbs_deb_leave(LBS_DEB_MAIN);
954ee164 1192}
10078321 1193EXPORT_SYMBOL_GPL(lbs_remove_card);
954ee164
DW
1194
1195
69f9032d 1196int lbs_start_card(struct lbs_private *priv)
954ee164
DW
1197{
1198 struct net_device *dev = priv->dev;
1199 int ret = -1;
1200
1201 lbs_deb_enter(LBS_DEB_MAIN);
1202
1203 /* poke the firmware */
10078321 1204 ret = lbs_setup_firmware(priv);
954ee164
DW
1205 if (ret)
1206 goto done;
1207
1208 /* init 802.11d */
10078321 1209 lbs_init_11d(priv);
876c9d3a
MT
1210
1211 if (register_netdev(dev)) {
9012b28a 1212 lbs_pr_err("cannot register ethX device\n");
954ee164 1213 goto done;
876c9d3a 1214 }
e7deced0
DW
1215 if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1216 lbs_pr_err("cannot register lbs_rtap attribute\n");
020f3d00 1217
020f3d00 1218 lbs_update_channel(priv);
020f3d00 1219
c9d1be36
HS
1220 /* 5.0.16p0 is known to NOT support any mesh */
1221 if (priv->fwrelease > 0x05001000) {
1222 /* Enable mesh, if supported, and work out which TLV it uses.
1223 0x100 + 291 is an unofficial value used in 5.110.20.pXX
1224 0x100 + 37 is the official value used in 5.110.21.pXX
1225 but we check them in that order because 20.pXX doesn't
1226 give an error -- it just silently fails. */
1227
1228 /* 5.110.20.pXX firmware will fail the command if the channel
1229 doesn't match the existing channel. But only if the TLV
1230 is correct. If the channel is wrong, _BOTH_ versions will
1231 give an error to 0x100+291, and allow 0x100+37 to succeed.
1232 It's just that 5.110.20.pXX will not have done anything
1233 useful */
1234
1235 priv->mesh_tlv = 0x100 + 291;
1236 if (lbs_mesh_config(priv, 1, priv->curbssparams.channel)) {
1237 priv->mesh_tlv = 0x100 + 37;
1238 if (lbs_mesh_config(priv, 1, priv->curbssparams.channel))
1239 priv->mesh_tlv = 0;
1240 }
1241 if (priv->mesh_tlv) {
1242 lbs_add_mesh(priv);
1243
1244 if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
1245 lbs_pr_err("cannot register lbs_mesh attribute\n");
1246 }
020f3d00 1247 }
876c9d3a 1248
10078321 1249 lbs_debugfs_init_one(priv, dev);
876c9d3a 1250
954ee164
DW
1251 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1252
32a74b7c 1253 ret = 0;
876c9d3a 1254
32a74b7c 1255done:
954ee164
DW
1256 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1257 return ret;
1258}
10078321 1259EXPORT_SYMBOL_GPL(lbs_start_card);
954ee164
DW
1260
1261
a63e5cb2 1262void lbs_stop_card(struct lbs_private *priv)
954ee164
DW
1263{
1264 struct net_device *dev = priv->dev;
954ee164
DW
1265 struct cmd_ctrl_node *cmdnode;
1266 unsigned long flags;
1267
1268 lbs_deb_enter(LBS_DEB_MAIN);
1269
1270 netif_stop_queue(priv->dev);
1271 netif_carrier_off(priv->dev);
1272
10078321 1273 lbs_debugfs_remove_one(priv);
10bca0d5 1274 device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
020f3d00
DW
1275 if (priv->mesh_tlv)
1276 device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
954ee164
DW
1277
1278 /* Flush pending command nodes */
aa21c004
DW
1279 spin_lock_irqsave(&priv->driver_lock, flags);
1280 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
ae125bf8 1281 cmdnode->result = -ENOENT;
954ee164
DW
1282 cmdnode->cmdwaitqwoken = 1;
1283 wake_up_interruptible(&cmdnode->cmdwait_q);
1284 }
aa21c004 1285 spin_unlock_irqrestore(&priv->driver_lock, flags);
954ee164
DW
1286
1287 unregister_netdev(dev);
1288
a63e5cb2 1289 lbs_deb_leave(LBS_DEB_MAIN);
876c9d3a 1290}
10078321 1291EXPORT_SYMBOL_GPL(lbs_stop_card);
084708b6 1292
876c9d3a 1293
78523daa
HS
1294/**
1295 * @brief This function adds mshX interface
1296 *
69f9032d 1297 * @param priv A pointer to the struct lbs_private structure
78523daa
HS
1298 * @return 0 if successful, -X otherwise
1299 */
23a397ac 1300static int lbs_add_mesh(struct lbs_private *priv)
78523daa
HS
1301{
1302 struct net_device *mesh_dev = NULL;
1303 int ret = 0;
1304
1305 lbs_deb_enter(LBS_DEB_MESH);
1306
1307 /* Allocate a virtual mesh device */
1308 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1309 lbs_deb_mesh("init mshX device failed\n");
1310 ret = -ENOMEM;
1311 goto done;
1312 }
1313 mesh_dev->priv = priv;
1314 priv->mesh_dev = mesh_dev;
1315
8552855f 1316 mesh_dev->open = lbs_dev_open;
198cefb9 1317 mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
8552855f 1318 mesh_dev->stop = lbs_mesh_stop;
10078321
HS
1319 mesh_dev->get_stats = lbs_get_stats;
1320 mesh_dev->set_mac_address = lbs_set_mac_address;
1321 mesh_dev->ethtool_ops = &lbs_ethtool_ops;
634b8f49
HS
1322 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1323 sizeof(priv->dev->dev_addr));
78523daa 1324
23a397ac 1325 SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
7732ca45 1326
78523daa 1327#ifdef WIRELESS_EXT
f5e05b69 1328 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
78523daa 1329#endif
78523daa
HS
1330 /* Register virtual mesh interface */
1331 ret = register_netdev(mesh_dev);
1332 if (ret) {
1333 lbs_pr_err("cannot register mshX virtual interface\n");
1334 goto err_free;
1335 }
1336
10078321 1337 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
78523daa
HS
1338 if (ret)
1339 goto err_unregister;
1340
1341 /* Everything successful */
1342 ret = 0;
1343 goto done;
1344
78523daa
HS
1345err_unregister:
1346 unregister_netdev(mesh_dev);
1347
1348err_free:
1349 free_netdev(mesh_dev);
1350
1351done:
1352 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1353 return ret;
1354}
084708b6 1355
23a397ac 1356static void lbs_remove_mesh(struct lbs_private *priv)
78523daa
HS
1357{
1358 struct net_device *mesh_dev;
1359
78523daa
HS
1360
1361 mesh_dev = priv->mesh_dev;
23a397ac 1362 if (!mesh_dev)
61d30020 1363 return;
78523daa 1364
61d30020 1365 lbs_deb_enter(LBS_DEB_MESH);
78523daa 1366 netif_stop_queue(mesh_dev);
51d84f50 1367 netif_carrier_off(priv->mesh_dev);
10078321 1368 sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
78523daa 1369 unregister_netdev(mesh_dev);
23a397ac 1370 priv->mesh_dev = NULL;
78523daa 1371 free_netdev(mesh_dev);
61d30020 1372 lbs_deb_leave(LBS_DEB_MESH);
78523daa
HS
1373}
1374
876c9d3a
MT
1375/**
1376 * @brief This function finds the CFP in
1377 * region_cfp_table based on region and band parameter.
1378 *
1379 * @param region The region code
1380 * @param band The band
1381 * @param cfp_no A pointer to CFP number
1382 * @return A pointer to CFP
1383 */
e98a88dd 1384struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
876c9d3a
MT
1385{
1386 int i, end;
1387
9012b28a 1388 lbs_deb_enter(LBS_DEB_MAIN);
876c9d3a 1389
ff8ac609 1390 end = ARRAY_SIZE(region_cfp_table);
876c9d3a
MT
1391
1392 for (i = 0; i < end ; i++) {
9012b28a 1393 lbs_deb_main("region_cfp_table[i].region=%d\n",
876c9d3a
MT
1394 region_cfp_table[i].region);
1395 if (region_cfp_table[i].region == region) {
1396 *cfp_no = region_cfp_table[i].cfp_no_BG;
9012b28a 1397 lbs_deb_leave(LBS_DEB_MAIN);
876c9d3a
MT
1398 return region_cfp_table[i].cfp_BG;
1399 }
1400 }
1401
9012b28a 1402 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
876c9d3a
MT
1403 return NULL;
1404}
1405
69f9032d 1406int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
876c9d3a 1407{
9012b28a 1408 int ret = 0;
876c9d3a
MT
1409 int i = 0;
1410
1411 struct chan_freq_power *cfp;
1412 int cfp_no;
1413
9012b28a 1414 lbs_deb_enter(LBS_DEB_MAIN);
876c9d3a 1415
aa21c004 1416 memset(priv->region_channel, 0, sizeof(priv->region_channel));
876c9d3a 1417
e98a88dd 1418 cfp = lbs_get_region_cfp_table(region, &cfp_no);
61d30020
HS
1419 if (cfp != NULL) {
1420 priv->region_channel[i].nrcfp = cfp_no;
1421 priv->region_channel[i].CFP = cfp;
1422 } else {
1423 lbs_deb_main("wrong region code %#x in band B/G\n",
1424 region);
1425 ret = -1;
1426 goto out;
876c9d3a 1427 }
61d30020
HS
1428 priv->region_channel[i].valid = 1;
1429 priv->region_channel[i].region = region;
1430 priv->region_channel[i].band = band;
1431 i++;
9012b28a
HS
1432out:
1433 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1434 return ret;
876c9d3a
MT
1435}
1436
7919b89c
HS
1437void lbs_queue_event(struct lbs_private *priv, u32 event)
1438{
1439 unsigned long flags;
1440
1441 lbs_deb_enter(LBS_DEB_THREAD);
1442 spin_lock_irqsave(&priv->driver_lock, flags);
1443
1444 if (priv->psstate == PS_STATE_SLEEP)
1445 priv->psstate = PS_STATE_AWAKE;
1446
1447 __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1448
1449 wake_up_interruptible(&priv->waitq);
1450
1451 spin_unlock_irqrestore(&priv->driver_lock, flags);
1452 lbs_deb_leave(LBS_DEB_THREAD);
1453}
1454EXPORT_SYMBOL_GPL(lbs_queue_event);
1455
1456void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
876c9d3a 1457{
ed37b516 1458 lbs_deb_enter(LBS_DEB_THREAD);
876c9d3a 1459
4f679496 1460 if (priv->psstate == PS_STATE_SLEEP)
aa21c004 1461 priv->psstate = PS_STATE_AWAKE;
7919b89c
HS
1462
1463 /* Swap buffers by flipping the response index */
1464 BUG_ON(resp_idx > 1);
1465 priv->resp_idx = resp_idx;
1466
fe336150 1467 wake_up_interruptible(&priv->waitq);
876c9d3a 1468
ed37b516 1469 lbs_deb_leave(LBS_DEB_THREAD);
876c9d3a 1470}
7919b89c 1471EXPORT_SYMBOL_GPL(lbs_notify_command_response);
876c9d3a 1472
4fb910fd 1473static int __init lbs_init_module(void)
876c9d3a 1474{
9012b28a 1475 lbs_deb_enter(LBS_DEB_MAIN);
f539f2ef
HS
1476 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1477 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1478 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1479 confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
10078321 1480 lbs_debugfs_init();
084708b6
HS
1481 lbs_deb_leave(LBS_DEB_MAIN);
1482 return 0;
876c9d3a
MT
1483}
1484
4fb910fd 1485static void __exit lbs_exit_module(void)
876c9d3a 1486{
9012b28a 1487 lbs_deb_enter(LBS_DEB_MAIN);
10078321 1488 lbs_debugfs_remove();
9012b28a 1489 lbs_deb_leave(LBS_DEB_MAIN);
876c9d3a
MT
1490}
1491
965f8bbc
LCC
1492/*
1493 * rtap interface support fuctions
1494 */
1495
10078321 1496static int lbs_rtap_open(struct net_device *dev)
965f8bbc 1497{
8552855f 1498 /* Yes, _stop_ the queue. Because we don't support injection */
61d30020
HS
1499 lbs_deb_enter(LBS_DEB_MAIN);
1500 netif_carrier_off(dev);
1501 netif_stop_queue(dev);
1502 lbs_deb_leave(LBS_DEB_LEAVE);
1503 return 0;
965f8bbc
LCC
1504}
1505
10078321 1506static int lbs_rtap_stop(struct net_device *dev)
965f8bbc 1507{
61d30020
HS
1508 lbs_deb_enter(LBS_DEB_MAIN);
1509 lbs_deb_leave(LBS_DEB_MAIN);
1510 return 0;
965f8bbc
LCC
1511}
1512
10078321 1513static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
965f8bbc 1514{
61d30020
HS
1515 netif_stop_queue(dev);
1516 return NETDEV_TX_BUSY;
965f8bbc
LCC
1517}
1518
10078321 1519static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
965f8bbc 1520{
69f9032d 1521 struct lbs_private *priv = dev->priv;
61d30020 1522 lbs_deb_enter(LBS_DEB_NET);
d9268fb9 1523 return &priv->stats;
965f8bbc
LCC
1524}
1525
1526
2fd6cfe3 1527static void lbs_remove_rtap(struct lbs_private *priv)
965f8bbc 1528{
61d30020 1529 lbs_deb_enter(LBS_DEB_MAIN);
965f8bbc 1530 if (priv->rtap_net_dev == NULL)
5f505d90 1531 goto out;
965f8bbc 1532 unregister_netdev(priv->rtap_net_dev);
d9268fb9 1533 free_netdev(priv->rtap_net_dev);
965f8bbc 1534 priv->rtap_net_dev = NULL;
5f505d90 1535out:
61d30020 1536 lbs_deb_leave(LBS_DEB_MAIN);
965f8bbc
LCC
1537}
1538
2fd6cfe3 1539static int lbs_add_rtap(struct lbs_private *priv)
965f8bbc 1540{
61d30020 1541 int ret = 0;
d9268fb9 1542 struct net_device *rtap_dev;
965f8bbc 1543
61d30020
HS
1544 lbs_deb_enter(LBS_DEB_MAIN);
1545 if (priv->rtap_net_dev) {
1546 ret = -EPERM;
1547 goto out;
1548 }
965f8bbc 1549
d9268fb9 1550 rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
61d30020
HS
1551 if (rtap_dev == NULL) {
1552 ret = -ENOMEM;
1553 goto out;
1554 }
965f8bbc 1555
121947c6 1556 memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
d9268fb9
DW
1557 rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
1558 rtap_dev->open = lbs_rtap_open;
1559 rtap_dev->stop = lbs_rtap_stop;
1560 rtap_dev->get_stats = lbs_rtap_get_stats;
1561 rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
1562 rtap_dev->set_multicast_list = lbs_set_multicast_list;
1563 rtap_dev->priv = priv;
965f8bbc 1564
61d30020
HS
1565 ret = register_netdev(rtap_dev);
1566 if (ret) {
d9268fb9 1567 free_netdev(rtap_dev);
61d30020 1568 goto out;
965f8bbc 1569 }
d9268fb9 1570 priv->rtap_net_dev = rtap_dev;
965f8bbc 1571
61d30020
HS
1572out:
1573 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1574 return ret;
965f8bbc
LCC
1575}
1576
04850a47
HS
1577#ifndef CONFIG_IEEE80211
1578const char *escape_essid(const char *essid, u8 essid_len)
1579{
1580 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
1581 const char *s = essid;
1582 char *d = escaped;
1583
1584 if (ieee80211_is_empty_essid(essid, essid_len)) {
1585 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
1586 return escaped;
1587 }
1588
1589 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
1590 while (essid_len--) {
1591 if (*s == '\0') {
1592 *d++ = '\\';
1593 *d++ = '0';
1594 s++;
1595 } else {
1596 *d++ = *s++;
1597 }
1598 }
1599 *d = '\0';
1600 return escaped;
1601}
1602#endif
965f8bbc 1603
10078321
HS
1604module_init(lbs_init_module);
1605module_exit(lbs_exit_module);
876c9d3a 1606
084708b6 1607MODULE_DESCRIPTION("Libertas WLAN Driver Library");
876c9d3a
MT
1608MODULE_AUTHOR("Marvell International Ltd.");
1609MODULE_LICENSE("GPL");