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