blob: 87bfd17b9c8c83cf6bb563dda303b2fe3352f6a9 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * This file contains the major functions in WLAN
3 * driver. It includes init, exit, open, close and main
4 * thread etc..
5 */
6
Holger Schuriga46c6412007-05-25 11:32:07 -04007#include <linux/moduleparam.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02008#include <linux/delay.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009#include <linux/etherdevice.h>
10#include <linux/netdevice.h>
11#include <linux/if_arp.h>
Dan Williamsfe336152007-08-02 11:32:25 -040012#include <linux/kthread.h>
Holger Schurig7919b892008-04-01 14:50:43 +020013#include <linux/kfifo.h>
David Woodhouse75bf45a2008-05-20 13:32:45 +010014#include <linux/stddef.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010015#include <linux/ieee80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020016#include <net/iw_handler.h>
Holger Schurigff9fc792009-10-06 16:31:54 +020017#include <net/cfg80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018
19#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020#include "decl.h"
21#include "dev.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020022#include "wext.h"
Holger Schurigff9fc792009-10-06 16:31:54 +020023#include "cfg.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020024#include "debugfs.h"
Holger Schurig245bf202008-04-02 16:27:42 +020025#include "scan.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020026#include "assoc.h"
Dan Williams6e66f032007-12-11 12:42:16 -050027#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020028
Dan Williams7856a542007-08-03 09:43:03 -040029#define DRIVER_RELEASE_VERSION "323.p0"
Holger Schurig10078322007-11-15 18:05:47 -050030const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
Dan Williams3ce40232007-05-10 23:03:07 -040031#ifdef DEBUG
32 "-dbg"
33#endif
34 "";
35
Holger Schuriga46c6412007-05-25 11:32:07 -040036
37/* Module parameters */
Holger Schurig10078322007-11-15 18:05:47 -050038unsigned int lbs_debug;
39EXPORT_SYMBOL_GPL(lbs_debug);
40module_param_named(libertas_debug, lbs_debug, int, 0644);
Holger Schuriga46c6412007-05-25 11:32:07 -040041
42
Holger Schurigf539f2e2008-03-26 13:22:11 +010043/* This global structure is used to send the confirm_sleep command as
44 * fast as possible down to the firmware. */
45struct cmd_confirm_sleep confirm_sleep;
46
47
Holger Schurig10078322007-11-15 18:05:47 -050048#define LBS_TX_PWR_DEFAULT 20 /*100mW */
49#define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
50#define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
51#define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
52#define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020053
54/* Format { channel, frequency (MHz), maxtxpower } */
55/* band: 'B/G', region: USA FCC/Canada IC */
56static struct chan_freq_power channel_freq_power_US_BG[] = {
Holger Schurig10078322007-11-15 18:05:47 -050057 {1, 2412, LBS_TX_PWR_US_DEFAULT},
58 {2, 2417, LBS_TX_PWR_US_DEFAULT},
59 {3, 2422, LBS_TX_PWR_US_DEFAULT},
60 {4, 2427, LBS_TX_PWR_US_DEFAULT},
61 {5, 2432, LBS_TX_PWR_US_DEFAULT},
62 {6, 2437, LBS_TX_PWR_US_DEFAULT},
63 {7, 2442, LBS_TX_PWR_US_DEFAULT},
64 {8, 2447, LBS_TX_PWR_US_DEFAULT},
65 {9, 2452, LBS_TX_PWR_US_DEFAULT},
66 {10, 2457, LBS_TX_PWR_US_DEFAULT},
67 {11, 2462, LBS_TX_PWR_US_DEFAULT}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020068};
69
70/* band: 'B/G', region: Europe ETSI */
71static struct chan_freq_power channel_freq_power_EU_BG[] = {
Holger Schurig10078322007-11-15 18:05:47 -050072 {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
73 {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
74 {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
75 {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
76 {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
77 {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
78 {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
79 {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
80 {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
81 {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
82 {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
83 {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
84 {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020085};
86
87/* band: 'B/G', region: Spain */
88static struct chan_freq_power channel_freq_power_SPN_BG[] = {
Holger Schurig10078322007-11-15 18:05:47 -050089 {10, 2457, LBS_TX_PWR_DEFAULT},
90 {11, 2462, LBS_TX_PWR_DEFAULT}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020091};
92
93/* band: 'B/G', region: France */
94static struct chan_freq_power channel_freq_power_FR_BG[] = {
Holger Schurig10078322007-11-15 18:05:47 -050095 {10, 2457, LBS_TX_PWR_FR_DEFAULT},
96 {11, 2462, LBS_TX_PWR_FR_DEFAULT},
97 {12, 2467, LBS_TX_PWR_FR_DEFAULT},
98 {13, 2472, LBS_TX_PWR_FR_DEFAULT}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020099};
100
101/* band: 'B/G', region: Japan */
102static struct chan_freq_power channel_freq_power_JPN_BG[] = {
Holger Schurig10078322007-11-15 18:05:47 -0500103 {1, 2412, LBS_TX_PWR_JP_DEFAULT},
104 {2, 2417, LBS_TX_PWR_JP_DEFAULT},
105 {3, 2422, LBS_TX_PWR_JP_DEFAULT},
106 {4, 2427, LBS_TX_PWR_JP_DEFAULT},
107 {5, 2432, LBS_TX_PWR_JP_DEFAULT},
108 {6, 2437, LBS_TX_PWR_JP_DEFAULT},
109 {7, 2442, LBS_TX_PWR_JP_DEFAULT},
110 {8, 2447, LBS_TX_PWR_JP_DEFAULT},
111 {9, 2452, LBS_TX_PWR_JP_DEFAULT},
112 {10, 2457, LBS_TX_PWR_JP_DEFAULT},
113 {11, 2462, LBS_TX_PWR_JP_DEFAULT},
114 {12, 2467, LBS_TX_PWR_JP_DEFAULT},
115 {13, 2472, LBS_TX_PWR_JP_DEFAULT},
116 {14, 2484, LBS_TX_PWR_JP_DEFAULT}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200117};
118
119/**
120 * the structure for channel, frequency and power
121 */
122struct region_cfp_table {
123 u8 region;
124 struct chan_freq_power *cfp_BG;
125 int cfp_no_BG;
126};
127
128/**
129 * the structure for the mapping between region and CFP
130 */
131static struct region_cfp_table region_cfp_table[] = {
132 {0x10, /*US FCC */
133 channel_freq_power_US_BG,
Denis Chengff8ac602007-09-02 18:30:18 +0800134 ARRAY_SIZE(channel_freq_power_US_BG),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200135 }
136 ,
137 {0x20, /*CANADA IC */
138 channel_freq_power_US_BG,
Denis Chengff8ac602007-09-02 18:30:18 +0800139 ARRAY_SIZE(channel_freq_power_US_BG),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200140 }
141 ,
142 {0x30, /*EU*/ channel_freq_power_EU_BG,
Denis Chengff8ac602007-09-02 18:30:18 +0800143 ARRAY_SIZE(channel_freq_power_EU_BG),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200144 }
145 ,
146 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
Denis Chengff8ac602007-09-02 18:30:18 +0800147 ARRAY_SIZE(channel_freq_power_SPN_BG),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148 }
149 ,
150 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
Denis Chengff8ac602007-09-02 18:30:18 +0800151 ARRAY_SIZE(channel_freq_power_FR_BG),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200152 }
153 ,
154 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
Denis Chengff8ac602007-09-02 18:30:18 +0800155 ARRAY_SIZE(channel_freq_power_JPN_BG),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200156 }
157 ,
158/*Add new region here */
159};
160
161/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162 * the table to keep region code
163 */
Holger Schurig10078322007-11-15 18:05:47 -0500164u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200165 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
166
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167/**
Dan Williams8c512762007-08-02 11:40:45 -0400168 * 802.11b/g supported bitrates (in 500Kb/s units)
169 */
Holger Schurig10078322007-11-15 18:05:47 -0500170u8 lbs_bg_rates[MAX_RATES] =
Dan Williams8c512762007-08-02 11:40:45 -0400171 { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
1720x00, 0x00 };
173
174/**
175 * FW rate table. FW refers to rates by their index in this table, not by the
176 * rate value itself. Values of 0x00 are
177 * reserved positions.
178 */
179static u8 fw_data_rates[MAX_RATES] =
180 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
181 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
182};
183
184/**
185 * @brief use index to get the data rate
186 *
187 * @param idx The index of data rate
188 * @return data rate or 0
189 */
Holger Schurig10078322007-11-15 18:05:47 -0500190u32 lbs_fw_index_to_data_rate(u8 idx)
Dan Williams8c512762007-08-02 11:40:45 -0400191{
192 if (idx >= sizeof(fw_data_rates))
193 idx = 0;
194 return fw_data_rates[idx];
195}
196
197/**
198 * @brief use rate to get the index
199 *
200 * @param rate data rate
201 * @return index or 0
202 */
Holger Schurig10078322007-11-15 18:05:47 -0500203u8 lbs_data_rate_to_fw_index(u32 rate)
Dan Williams8c512762007-08-02 11:40:45 -0400204{
205 u8 i;
206
207 if (!rate)
208 return 0;
209
210 for (i = 0; i < sizeof(fw_data_rates); i++) {
211 if (rate == fw_data_rates[i])
212 return i;
213 }
214 return 0;
215}
216
217/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200218 * Attributes exported through sysfs
219 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200220
221/**
Luis Carlos82fde742007-06-07 16:40:59 -0400222 * @brief Get function for sysfs attribute anycast_mask
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200223 */
Holger Schurig10078322007-11-15 18:05:47 -0500224static ssize_t lbs_anycast_get(struct device *dev,
Dan Williamsb59bb612007-06-18 11:50:43 -0400225 struct device_attribute *attr, char * buf)
226{
Kiran Divekarab65f642009-02-19 19:32:39 -0500227 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200228 struct cmd_ds_mesh_access mesh_access;
David Woodhouse301eacb2007-12-11 15:23:59 -0500229 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200230
231 memset(&mesh_access, 0, sizeof(mesh_access));
David Woodhouse301eacb2007-12-11 15:23:59 -0500232
233 ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
234 if (ret)
235 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236
Luis Carlos82fde742007-06-07 16:40:59 -0400237 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200238}
239
240/**
Luis Carlos82fde742007-06-07 16:40:59 -0400241 * @brief Set function for sysfs attribute anycast_mask
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200242 */
Holger Schurig10078322007-11-15 18:05:47 -0500243static ssize_t lbs_anycast_set(struct device *dev,
Dan Williamsb59bb612007-06-18 11:50:43 -0400244 struct device_attribute *attr, const char * buf, size_t count)
245{
Kiran Divekarab65f642009-02-19 19:32:39 -0500246 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200247 struct cmd_ds_mesh_access mesh_access;
David Woodhouse981f1872007-05-25 23:36:54 -0400248 uint32_t datum;
David Woodhouse301eacb2007-12-11 15:23:59 -0500249 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200250
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200251 memset(&mesh_access, 0, sizeof(mesh_access));
Luis Carlos82fde742007-06-07 16:40:59 -0400252 sscanf(buf, "%x", &datum);
David Woodhouse981f1872007-05-25 23:36:54 -0400253 mesh_access.data[0] = cpu_to_le32(datum);
254
David Woodhouse301eacb2007-12-11 15:23:59 -0500255 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
256 if (ret)
257 return ret;
258
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200259 return strlen(buf);
260}
261
Anna Neal6fb53252008-12-09 13:23:45 -0800262/**
263 * @brief Get function for sysfs attribute prb_rsp_limit
264 */
265static ssize_t lbs_prb_rsp_limit_get(struct device *dev,
266 struct device_attribute *attr, char *buf)
267{
Kiran Divekarab65f642009-02-19 19:32:39 -0500268 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
Anna Neal6fb53252008-12-09 13:23:45 -0800269 struct cmd_ds_mesh_access mesh_access;
270 int ret;
271 u32 retry_limit;
272
273 memset(&mesh_access, 0, sizeof(mesh_access));
274 mesh_access.data[0] = cpu_to_le32(CMD_ACT_GET);
275
276 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
277 &mesh_access);
278 if (ret)
279 return ret;
280
281 retry_limit = le32_to_cpu(mesh_access.data[1]);
282 return snprintf(buf, 10, "%d\n", retry_limit);
283}
284
285/**
286 * @brief Set function for sysfs attribute prb_rsp_limit
287 */
288static ssize_t lbs_prb_rsp_limit_set(struct device *dev,
289 struct device_attribute *attr, const char *buf, size_t count)
290{
Kiran Divekarab65f642009-02-19 19:32:39 -0500291 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
Anna Neal6fb53252008-12-09 13:23:45 -0800292 struct cmd_ds_mesh_access mesh_access;
293 int ret;
294 unsigned long retry_limit;
295
296 memset(&mesh_access, 0, sizeof(mesh_access));
297 mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
298
299 if (!strict_strtoul(buf, 10, &retry_limit))
300 return -ENOTSUPP;
301 if (retry_limit > 15)
302 return -ENOTSUPP;
303
304 mesh_access.data[1] = cpu_to_le32(retry_limit);
305
306 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
307 &mesh_access);
308 if (ret)
309 return ret;
310
311 return strlen(buf);
312}
313
David Woodhouse2fd6cfe2007-12-11 17:44:10 -0500314static int lbs_add_rtap(struct lbs_private *priv);
315static void lbs_remove_rtap(struct lbs_private *priv);
David Woodhouse23a397a2007-12-11 18:56:42 -0500316static int lbs_add_mesh(struct lbs_private *priv);
317static void lbs_remove_mesh(struct lbs_private *priv);
David Woodhouse7e226272007-12-14 22:53:41 -0500318
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400319
320/**
321 * Get function for sysfs attribute rtap
322 */
Holger Schurig10078322007-11-15 18:05:47 -0500323static ssize_t lbs_rtap_get(struct device *dev,
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400324 struct device_attribute *attr, char * buf)
325{
Kiran Divekarab65f642009-02-19 19:32:39 -0500326 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
David Woodhouseaa21c002007-12-08 20:04:36 +0000327 return snprintf(buf, 5, "0x%X\n", priv->monitormode);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400328}
329
330/**
331 * Set function for sysfs attribute rtap
332 */
Holger Schurig10078322007-11-15 18:05:47 -0500333static ssize_t lbs_rtap_set(struct device *dev,
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400334 struct device_attribute *attr, const char * buf, size_t count)
335{
336 int monitor_mode;
Kiran Divekarab65f642009-02-19 19:32:39 -0500337 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400338
339 sscanf(buf, "%x", &monitor_mode);
Holger Schurigc2b310a2008-03-26 09:57:14 +0100340 if (monitor_mode) {
341 if (priv->monitormode == monitor_mode)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400342 return strlen(buf);
Holger Schurigc2b310a2008-03-26 09:57:14 +0100343 if (!priv->monitormode) {
David Woodhouse8552855f2007-12-10 16:38:18 -0500344 if (priv->infra_open || priv->mesh_open)
345 return -EBUSY;
David Woodhouseaa21c002007-12-08 20:04:36 +0000346 if (priv->mode == IW_MODE_INFRA)
Dan Williams191bb402008-08-21 17:46:18 -0400347 lbs_cmd_80211_deauthenticate(priv,
348 priv->curbssparams.bssid,
349 WLAN_REASON_DEAUTH_LEAVING);
David Woodhouseaa21c002007-12-08 20:04:36 +0000350 else if (priv->mode == IW_MODE_ADHOC)
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400351 lbs_adhoc_stop(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500352 lbs_add_rtap(priv);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400353 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000354 priv->monitormode = monitor_mode;
Dan Williams3b72b012008-07-29 13:50:39 -0400355 } else {
Holger Schurigc2b310a2008-03-26 09:57:14 +0100356 if (!priv->monitormode)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400357 return strlen(buf);
Holger Schurigc2b310a2008-03-26 09:57:14 +0100358 priv->monitormode = 0;
Holger Schurig10078322007-11-15 18:05:47 -0500359 lbs_remove_rtap(priv);
David Woodhousef86a93e2007-12-08 19:46:19 +0000360
David Woodhouseaa21c002007-12-08 20:04:36 +0000361 if (priv->currenttxskb) {
362 dev_kfree_skb_any(priv->currenttxskb);
363 priv->currenttxskb = NULL;
David Woodhousef86a93e2007-12-08 19:46:19 +0000364 }
365
366 /* Wake queues, command thread, etc. */
367 lbs_host_to_card_done(priv);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400368 }
369
Holger Schurig10078322007-11-15 18:05:47 -0500370 lbs_prepare_and_send_command(priv,
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400371 CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
David Woodhouseaa21c002007-12-08 20:04:36 +0000372 CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400373 return strlen(buf);
374}
375
376/**
David Woodhouse23a397a2007-12-11 18:56:42 -0500377 * lbs_rtap attribute to be exported per ethX interface
378 * through sysfs (/sys/class/net/ethX/lbs_rtap)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400379 */
David Woodhouse23a397a2007-12-11 18:56:42 -0500380static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
381
382/**
383 * Get function for sysfs attribute mesh
384 */
385static ssize_t lbs_mesh_get(struct device *dev,
386 struct device_attribute *attr, char * buf)
387{
Kiran Divekarab65f642009-02-19 19:32:39 -0500388 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
David Woodhouse23a397a2007-12-11 18:56:42 -0500389 return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
390}
391
392/**
393 * Set function for sysfs attribute mesh
394 */
395static ssize_t lbs_mesh_set(struct device *dev,
396 struct device_attribute *attr, const char * buf, size_t count)
397{
Kiran Divekarab65f642009-02-19 19:32:39 -0500398 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
David Woodhouse23a397a2007-12-11 18:56:42 -0500399 int enable;
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700400 int ret, action = CMD_ACT_MESH_CONFIG_STOP;
David Woodhouse23a397a2007-12-11 18:56:42 -0500401
402 sscanf(buf, "%x", &enable);
403 enable = !!enable;
404 if (enable == !!priv->mesh_dev)
405 return count;
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700406 if (enable)
407 action = CMD_ACT_MESH_CONFIG_START;
408 ret = lbs_mesh_config(priv, action, priv->curbssparams.channel);
David Woodhouse23a397a2007-12-11 18:56:42 -0500409 if (ret)
410 return ret;
David Woodhouse7e226272007-12-14 22:53:41 -0500411
David Woodhouse23a397a2007-12-11 18:56:42 -0500412 if (enable)
413 lbs_add_mesh(priv);
414 else
415 lbs_remove_mesh(priv);
416
417 return count;
418}
419
420/**
421 * lbs_mesh attribute to be exported per ethX interface
422 * through sysfs (/sys/class/net/ethX/lbs_mesh)
423 */
424static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400425
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200426/**
Luis Carlos82fde742007-06-07 16:40:59 -0400427 * anycast_mask attribute to be exported per mshX interface
428 * through sysfs (/sys/class/net/mshX/anycast_mask)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429 */
Holger Schurig10078322007-11-15 18:05:47 -0500430static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200431
Anna Neal6fb53252008-12-09 13:23:45 -0800432/**
433 * prb_rsp_limit attribute to be exported per mshX interface
434 * through sysfs (/sys/class/net/mshX/prb_rsp_limit)
435 */
436static DEVICE_ATTR(prb_rsp_limit, 0644, lbs_prb_rsp_limit_get,
437 lbs_prb_rsp_limit_set);
438
Holger Schurig10078322007-11-15 18:05:47 -0500439static struct attribute *lbs_mesh_sysfs_entries[] = {
Luis Carlos Coboa9f38d02007-08-02 11:52:29 -0400440 &dev_attr_anycast_mask.attr,
Anna Neal6fb53252008-12-09 13:23:45 -0800441 &dev_attr_prb_rsp_limit.attr,
Luis Carlos Coboa9f38d02007-08-02 11:52:29 -0400442 NULL,
443};
444
Holger Schurig10078322007-11-15 18:05:47 -0500445static struct attribute_group lbs_mesh_attr_group = {
446 .attrs = lbs_mesh_sysfs_entries,
Luis Carlos Coboa9f38d02007-08-02 11:52:29 -0400447};
448
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449/**
David Woodhouse8552855f2007-12-10 16:38:18 -0500450 * @brief This function opens the ethX or mshX interface
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451 *
452 * @param dev A pointer to net_device structure
David Woodhouse8552855f2007-12-10 16:38:18 -0500453 * @return 0 or -EBUSY if monitor mode active
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454 */
Holger Schurig10078322007-11-15 18:05:47 -0500455static int lbs_dev_open(struct net_device *dev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456{
Kiran Divekarab65f642009-02-19 19:32:39 -0500457 struct lbs_private *priv = dev->ml_priv;
David Woodhouse8552855f2007-12-10 16:38:18 -0500458 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200459
Holger Schurig61d30022008-01-16 15:59:52 +0100460 lbs_deb_enter(LBS_DEB_NET);
461
David Woodhouse8552855f2007-12-10 16:38:18 -0500462 spin_lock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463
Holger Schurigc2b310a2008-03-26 09:57:14 +0100464 if (priv->monitormode) {
David Woodhouse8552855f2007-12-10 16:38:18 -0500465 ret = -EBUSY;
466 goto out;
Javier Cardona51d84f52007-05-25 12:06:56 -0400467 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200468
David Woodhouse8552855f2007-12-10 16:38:18 -0500469 if (dev == priv->mesh_dev) {
470 priv->mesh_open = 1;
471 priv->mesh_connect_status = LBS_CONNECTED;
472 netif_carrier_on(dev);
473 } else {
474 priv->infra_open = 1;
David Woodhouse7e226272007-12-14 22:53:41 -0500475
David Woodhouse8552855f2007-12-10 16:38:18 -0500476 if (priv->connect_status == LBS_CONNECTED)
477 netif_carrier_on(dev);
478 else
479 netif_carrier_off(dev);
480 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481
David Woodhouse8552855f2007-12-10 16:38:18 -0500482 if (!priv->tx_pending_len)
483 netif_wake_queue(dev);
484 out:
Brajesh Dave01d77d82007-11-20 17:44:14 -0500485
David Woodhouse8552855f2007-12-10 16:38:18 -0500486 spin_unlock_irq(&priv->driver_lock);
Holger Schurig61d30022008-01-16 15:59:52 +0100487 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
David Woodhouse8552855f2007-12-10 16:38:18 -0500488 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200489}
490
491/**
492 * @brief This function closes the mshX interface
493 *
494 * @param dev A pointer to net_device structure
495 * @return 0
496 */
David Woodhouse8552855f2007-12-10 16:38:18 -0500497static int lbs_mesh_stop(struct net_device *dev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498{
Wang Chen4ceb7b62008-09-05 11:28:47 +0800499 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500
Holger Schurig61d30022008-01-16 15:59:52 +0100501 lbs_deb_enter(LBS_DEB_MESH);
David Woodhouse8552855f2007-12-10 16:38:18 -0500502 spin_lock_irq(&priv->driver_lock);
503
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504 priv->mesh_open = 0;
David Woodhouse8552855f2007-12-10 16:38:18 -0500505 priv->mesh_connect_status = LBS_DISCONNECTED;
506
507 netif_stop_queue(dev);
508 netif_carrier_off(dev);
David Woodhouse7e226272007-12-14 22:53:41 -0500509
David Woodhouse8552855f2007-12-10 16:38:18 -0500510 spin_unlock_irq(&priv->driver_lock);
Holger Schurig61d30022008-01-16 15:59:52 +0100511
David Woodhouse75bf45a2008-05-20 13:32:45 +0100512 schedule_work(&priv->mcast_work);
513
Holger Schurig61d30022008-01-16 15:59:52 +0100514 lbs_deb_leave(LBS_DEB_MESH);
David Woodhouse8552855f2007-12-10 16:38:18 -0500515 return 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516}
517
518/**
519 * @brief This function closes the ethX interface
520 *
521 * @param dev A pointer to net_device structure
522 * @return 0
523 */
David Woodhouse8552855f2007-12-10 16:38:18 -0500524static int lbs_eth_stop(struct net_device *dev)
Holger Schurig78523da2007-05-25 11:49:19 -0400525{
Kiran Divekarab65f642009-02-19 19:32:39 -0500526 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200527
Holger Schurig61d30022008-01-16 15:59:52 +0100528 lbs_deb_enter(LBS_DEB_NET);
529
David Woodhouse8552855f2007-12-10 16:38:18 -0500530 spin_lock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531 priv->infra_open = 0;
David Woodhouse8552855f2007-12-10 16:38:18 -0500532 netif_stop_queue(dev);
David Woodhouse8552855f2007-12-10 16:38:18 -0500533 spin_unlock_irq(&priv->driver_lock);
Holger Schurig61d30022008-01-16 15:59:52 +0100534
David Woodhouse75bf45a2008-05-20 13:32:45 +0100535 schedule_work(&priv->mcast_work);
536
Holger Schurig61d30022008-01-16 15:59:52 +0100537 lbs_deb_leave(LBS_DEB_NET);
David Woodhouse8552855f2007-12-10 16:38:18 -0500538 return 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539}
540
Holger Schurig10078322007-11-15 18:05:47 -0500541static void lbs_tx_timeout(struct net_device *dev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200542{
Kiran Divekarab65f642009-02-19 19:32:39 -0500543 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544
Holger Schurig9012b282007-05-25 11:27:16 -0400545 lbs_deb_enter(LBS_DEB_TX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546
Holger Schurig9012b282007-05-25 11:27:16 -0400547 lbs_pr_err("tx watch dog timeout\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200548
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 dev->trans_start = jiffies;
550
Holger Schurig7919b892008-04-01 14:50:43 +0200551 if (priv->currenttxskb)
552 lbs_send_tx_feedback(priv, 0);
553
David Woodhousea63b22b2007-12-08 20:56:44 +0000554 /* XX: Shouldn't we also call into the hw-specific driver
555 to kick it somehow? */
556 lbs_host_to_card_done(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557
David Woodhouse354eca92007-12-17 19:22:40 -0500558 /* More often than not, this actually happens because the
559 firmware has crapped itself -- rather than just a very
560 busy medium. So send a harmless command, and if/when
561 _that_ times out, we'll kick it in the head. */
562 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
563 0, 0, NULL);
564
Holger Schurig9012b282007-05-25 11:27:16 -0400565 lbs_deb_leave(LBS_DEB_TX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200566}
567
David Woodhousee775ed72007-12-06 14:36:11 +0000568void lbs_host_to_card_done(struct lbs_private *priv)
569{
David Woodhousea63b22b2007-12-08 20:56:44 +0000570 unsigned long flags;
571
Holger Schurig61d30022008-01-16 15:59:52 +0100572 lbs_deb_enter(LBS_DEB_THREAD);
573
David Woodhousea63b22b2007-12-08 20:56:44 +0000574 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhousee775ed72007-12-06 14:36:11 +0000575
576 priv->dnld_sent = DNLD_RES_RECEIVED;
577
578 /* Wake main thread if commands are pending */
Amitkumar Karwar49125452009-09-30 20:04:38 -0700579 if (!priv->cur_cmd || priv->tx_pending_len > 0) {
580 if (!priv->wakeup_dev_required)
581 wake_up_interruptible(&priv->waitq);
582 }
David Woodhousee775ed72007-12-06 14:36:11 +0000583
David Woodhousea63b22b2007-12-08 20:56:44 +0000584 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig61d30022008-01-16 15:59:52 +0100585 lbs_deb_leave(LBS_DEB_THREAD);
David Woodhousee775ed72007-12-06 14:36:11 +0000586}
587EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
588
Holger Schurig10078322007-11-15 18:05:47 -0500589static int lbs_set_mac_address(struct net_device *dev, void *addr)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200590{
591 int ret = 0;
Kiran Divekarab65f642009-02-19 19:32:39 -0500592 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200593 struct sockaddr *phwaddr = addr;
Holger Schurig2af9f032008-03-26 09:58:32 +0100594 struct cmd_ds_802_11_mac_address cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595
Holger Schurig9012b282007-05-25 11:27:16 -0400596 lbs_deb_enter(LBS_DEB_NET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200597
Luis Carlos Cobo Rus0a0d08a2007-05-25 23:05:27 -0400598 /* In case it was called from the mesh device */
Holger Schurig2af9f032008-03-26 09:58:32 +0100599 dev = priv->dev;
Luis Carlos Cobo Rus0a0d08a2007-05-25 23:05:27 -0400600
Holger Schurig2af9f032008-03-26 09:58:32 +0100601 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
602 cmd.action = cpu_to_le16(CMD_ACT_SET);
603 memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200604
Holger Schurig2af9f032008-03-26 09:58:32 +0100605 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200606 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400607 lbs_deb_net("set MAC address failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608 goto done;
609 }
610
Holger Schurig2af9f032008-03-26 09:58:32 +0100611 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
612 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
Holger Schurig78523da2007-05-25 11:49:19 -0400613 if (priv->mesh_dev)
Holger Schurig2af9f032008-03-26 09:58:32 +0100614 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200615
616done:
Holger Schurig9012b282007-05-25 11:27:16 -0400617 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 return ret;
619}
620
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621
David Woodhouse75bf45a2008-05-20 13:32:45 +0100622static inline int mac_in_list(unsigned char *list, int list_len,
623 unsigned char *mac)
624{
625 while (list_len) {
626 if (!memcmp(list, mac, ETH_ALEN))
627 return 1;
628 list += ETH_ALEN;
629 list_len--;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630 }
David Woodhouse75bf45a2008-05-20 13:32:45 +0100631 return 0;
632}
633
634
635static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
636 struct net_device *dev, int nr_addrs)
637{
638 int i = nr_addrs;
639 struct dev_mc_list *mc_list;
David Woodhouse75bf45a2008-05-20 13:32:45 +0100640
641 if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
642 return nr_addrs;
643
David S. Millerb9e40852008-07-15 00:15:08 -0700644 netif_addr_lock_bh(dev);
David Woodhouse75bf45a2008-05-20 13:32:45 +0100645 for (mc_list = dev->mc_list; mc_list; mc_list = mc_list->next) {
646 if (mac_in_list(cmd->maclist, nr_addrs, mc_list->dmi_addr)) {
Johannes Berge1749612008-10-27 15:59:26 -0700647 lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
648 mc_list->dmi_addr);
David Woodhouse75bf45a2008-05-20 13:32:45 +0100649 continue;
650 }
651
652 if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
653 break;
654 memcpy(&cmd->maclist[6*i], mc_list->dmi_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700655 lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
656 mc_list->dmi_addr);
David Woodhouse75bf45a2008-05-20 13:32:45 +0100657 i++;
658 }
David S. Millerb9e40852008-07-15 00:15:08 -0700659 netif_addr_unlock_bh(dev);
David Woodhouse75bf45a2008-05-20 13:32:45 +0100660 if (mc_list)
661 return -EOVERFLOW;
662
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663 return i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200664}
665
David Woodhouse75bf45a2008-05-20 13:32:45 +0100666static void lbs_set_mcast_worker(struct work_struct *work)
667{
668 struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
669 struct cmd_ds_mac_multicast_adr mcast_cmd;
670 int dev_flags;
671 int nr_addrs;
672 int old_mac_control = priv->mac_control;
673
674 lbs_deb_enter(LBS_DEB_NET);
675
676 dev_flags = priv->dev->flags;
677 if (priv->mesh_dev)
678 dev_flags |= priv->mesh_dev->flags;
679
680 if (dev_flags & IFF_PROMISC) {
681 priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
682 priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
683 CMD_ACT_MAC_MULTICAST_ENABLE);
684 goto out_set_mac_control;
685 } else if (dev_flags & IFF_ALLMULTI) {
686 do_allmulti:
687 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
688 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
689 CMD_ACT_MAC_MULTICAST_ENABLE);
690 goto out_set_mac_control;
691 }
692
693 /* Once for priv->dev, again for priv->mesh_dev if it exists */
694 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
695 if (nr_addrs >= 0 && priv->mesh_dev)
696 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
697 if (nr_addrs < 0)
698 goto do_allmulti;
699
700 if (nr_addrs) {
701 int size = offsetof(struct cmd_ds_mac_multicast_adr,
702 maclist[6*nr_addrs]);
703
704 mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
705 mcast_cmd.hdr.size = cpu_to_le16(size);
706 mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
707
708 lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
709
710 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
711 } else
712 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
713
714 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
715 CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
716 out_set_mac_control:
717 if (priv->mac_control != old_mac_control)
718 lbs_set_mac_control(priv);
719
720 lbs_deb_leave(LBS_DEB_NET);
721}
722
Holger Schurig10078322007-11-15 18:05:47 -0500723static void lbs_set_multicast_list(struct net_device *dev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724{
Kiran Divekarab65f642009-02-19 19:32:39 -0500725 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726
David Woodhouse75bf45a2008-05-20 13:32:45 +0100727 schedule_work(&priv->mcast_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728}
729
730/**
Holger Schurig10078322007-11-15 18:05:47 -0500731 * @brief This function handles the major jobs in the LBS driver.
Holger Schurig9012b282007-05-25 11:27:16 -0400732 * It handles all events generated by firmware, RX data received
733 * from firmware and TX data sent from kernel.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734 *
Holger Schurig10078322007-11-15 18:05:47 -0500735 * @param data A pointer to lbs_thread structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736 * @return 0
737 */
Holger Schurig10078322007-11-15 18:05:47 -0500738static int lbs_thread(void *data)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739{
Dan Williamsfe336152007-08-02 11:32:25 -0400740 struct net_device *dev = data;
Kiran Divekarab65f642009-02-19 19:32:39 -0500741 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742 wait_queue_t wait;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743
Holger Schurig9012b282007-05-25 11:27:16 -0400744 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200745
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746 init_waitqueue_entry(&wait, current);
747
748 for (;;) {
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500749 int shouldsleep;
Holger Schurig7919b892008-04-01 14:50:43 +0200750 u8 resp_idx;
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500751
Holger Schurig7919b892008-04-01 14:50:43 +0200752 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
753 priv->currenttxskb, priv->dnld_sent);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754
Dan Williamsfe336152007-08-02 11:32:25 -0400755 add_wait_queue(&priv->waitq, &wait);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756 set_current_state(TASK_INTERRUPTIBLE);
David Woodhouseaa21c002007-12-08 20:04:36 +0000757 spin_lock_irq(&priv->driver_lock);
David Woodhouse59f3e4b2007-12-08 17:42:59 +0000758
David Woodhoused9f88702007-12-13 21:48:00 -0500759 if (kthread_should_stop())
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500760 shouldsleep = 0; /* Bye */
David Woodhoused9f88702007-12-13 21:48:00 -0500761 else if (priv->surpriseremoved)
762 shouldsleep = 1; /* We need to wait until we're _told_ to die */
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500763 else if (priv->psstate == PS_STATE_SLEEP)
764 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
David Woodhouse2a345092007-12-15 19:33:43 -0500765 else if (priv->cmd_timed_out)
766 shouldsleep = 0; /* Command timed out. Recover */
David Woodhouseb15152a2007-12-11 11:55:37 -0500767 else if (!priv->fw_ready)
768 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500769 else if (priv->dnld_sent)
770 shouldsleep = 1; /* Something is en route to the device already */
David Woodhouse2eb188a2007-12-09 23:54:27 -0500771 else if (priv->tx_pending_len > 0)
772 shouldsleep = 0; /* We've a packet to send */
Holger Schurigef707b82008-05-23 16:04:13 +0200773 else if (priv->resp_len[priv->resp_idx])
774 shouldsleep = 0; /* We have a command response */
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500775 else if (priv->cur_cmd)
776 shouldsleep = 1; /* Can't send a command; one already running */
Amitkumar Karwar49125452009-09-30 20:04:38 -0700777 else if (!list_empty(&priv->cmdpendingq) &&
778 !(priv->wakeup_dev_required))
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500779 shouldsleep = 0; /* We have a command to send */
Holger Schurig7919b892008-04-01 14:50:43 +0200780 else if (__kfifo_len(priv->event_fifo))
781 shouldsleep = 0; /* We have an event to process */
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500782 else
783 shouldsleep = 1; /* No command */
784
785 if (shouldsleep) {
Holger Schurig7919b892008-04-01 14:50:43 +0200786 lbs_deb_thread("sleeping, connect_status %d, "
Holger Schurig5f505d92008-04-30 10:50:07 +0200787 "psmode %d, psstate %d\n",
Holger Schurig7919b892008-04-01 14:50:43 +0200788 priv->connect_status,
789 priv->psmode, priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +0000790 spin_unlock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200791 schedule();
792 } else
David Woodhouseaa21c002007-12-08 20:04:36 +0000793 spin_unlock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200794
Holger Schurig7919b892008-04-01 14:50:43 +0200795 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
796 priv->currenttxskb, priv->dnld_sent);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797
798 set_current_state(TASK_RUNNING);
Dan Williamsfe336152007-08-02 11:32:25 -0400799 remove_wait_queue(&priv->waitq, &wait);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200800
Holger Schurig7919b892008-04-01 14:50:43 +0200801 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
802 priv->currenttxskb, priv->dnld_sent);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803
David Woodhoused9f88702007-12-13 21:48:00 -0500804 if (kthread_should_stop()) {
Holger Schurig7919b892008-04-01 14:50:43 +0200805 lbs_deb_thread("break from main thread\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200806 break;
807 }
808
David Woodhoused9f88702007-12-13 21:48:00 -0500809 if (priv->surpriseremoved) {
810 lbs_deb_thread("adapter removed; waiting to die...\n");
811 continue;
812 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813
Holger Schurig7919b892008-04-01 14:50:43 +0200814 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
815 priv->currenttxskb, priv->dnld_sent);
816
Holger Schurig7919b892008-04-01 14:50:43 +0200817 /* Process any pending command response */
Holger Schuriga01f5452008-06-04 11:10:40 +0200818 spin_lock_irq(&priv->driver_lock);
Holger Schurig7919b892008-04-01 14:50:43 +0200819 resp_idx = priv->resp_idx;
820 if (priv->resp_len[resp_idx]) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000821 spin_unlock_irq(&priv->driver_lock);
Holger Schurig7919b892008-04-01 14:50:43 +0200822 lbs_process_command_response(priv,
823 priv->resp_buf[resp_idx],
824 priv->resp_len[resp_idx]);
David Woodhouseaa21c002007-12-08 20:04:36 +0000825 spin_lock_irq(&priv->driver_lock);
Holger Schurig7919b892008-04-01 14:50:43 +0200826 priv->resp_len[resp_idx] = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200827 }
Holger Schurig7919b892008-04-01 14:50:43 +0200828 spin_unlock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829
Amitkumar Karwar49125452009-09-30 20:04:38 -0700830 /* Process hardware events, e.g. card removed, link lost */
831 spin_lock_irq(&priv->driver_lock);
832 while (__kfifo_len(priv->event_fifo)) {
833 u32 event;
834 __kfifo_get(priv->event_fifo, (unsigned char *) &event,
835 sizeof(event));
836 spin_unlock_irq(&priv->driver_lock);
837 lbs_process_event(priv, event);
838 spin_lock_irq(&priv->driver_lock);
839 }
840 spin_unlock_irq(&priv->driver_lock);
841
842 if (priv->wakeup_dev_required) {
843 lbs_deb_thread("Waking up device...\n");
844 /* Wake up device */
845 if (priv->exit_deep_sleep(priv))
846 lbs_deb_thread("Wakeup device failed\n");
847 continue;
848 }
849
Holger Schurig7919b892008-04-01 14:50:43 +0200850 /* command timeout stuff */
David Woodhouse2a345092007-12-15 19:33:43 -0500851 if (priv->cmd_timed_out && priv->cur_cmd) {
852 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
853
Holger Schurig57962f02008-05-14 16:30:28 +0200854 if (++priv->nr_retries > 3) {
855 lbs_pr_info("Excessive timeouts submitting "
856 "command 0x%04x\n",
857 le16_to_cpu(cmdnode->cmdbuf->command));
David Woodhouse2a345092007-12-15 19:33:43 -0500858 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
859 priv->nr_retries = 0;
Holger Schurigb43441a2008-05-23 10:07:56 +0200860 if (priv->reset_card)
David Woodhouseedf5dab2008-05-20 16:43:31 +0100861 priv->reset_card(priv);
David Woodhouse2a345092007-12-15 19:33:43 -0500862 } else {
863 priv->cur_cmd = NULL;
Brian Cavagnolo02969d22008-05-13 13:54:59 +0100864 priv->dnld_sent = DNLD_RES_RECEIVED;
Holger Schurig57962f02008-05-14 16:30:28 +0200865 lbs_pr_info("requeueing command 0x%04x due "
866 "to timeout (#%d)\n",
867 le16_to_cpu(cmdnode->cmdbuf->command),
868 priv->nr_retries);
David Woodhouse2a345092007-12-15 19:33:43 -0500869
870 /* Stick it back at the _top_ of the pending queue
871 for immediate resubmission */
872 list_add(&cmdnode->list, &priv->cmdpendingq);
873 }
874 }
875 priv->cmd_timed_out = 0;
876
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200878
David Woodhouseb15152a2007-12-11 11:55:37 -0500879 if (!priv->fw_ready)
880 continue;
881
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200882 /* Check if we need to confirm Sleep Request received previously */
David Woodhouseaa21c002007-12-08 20:04:36 +0000883 if (priv->psstate == PS_STATE_PRE_SLEEP &&
884 !priv->dnld_sent && !priv->cur_cmd) {
885 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig7919b892008-04-01 14:50:43 +0200886 lbs_deb_thread("pre-sleep, currenttxskb %p, "
887 "dnld_sent %d, cur_cmd %p\n",
888 priv->currenttxskb, priv->dnld_sent,
889 priv->cur_cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200890
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100891 lbs_ps_confirm_sleep(priv);
David Woodhouse59f3e4b2007-12-08 17:42:59 +0000892 } else {
893 /* workaround for firmware sending
894 * deauth/linkloss event immediately
895 * after sleep request; remove this
896 * after firmware fixes it
897 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000898 priv->psstate = PS_STATE_AWAKE;
Holger Schurig7919b892008-04-01 14:50:43 +0200899 lbs_pr_alert("ignore PS_SleepConfirm in "
900 "non-connected state\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200901 }
902 }
903
904 /* The PS state is changed during processing of Sleep Request
905 * event above
906 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000907 if ((priv->psstate == PS_STATE_SLEEP) ||
908 (priv->psstate == PS_STATE_PRE_SLEEP))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200909 continue;
910
Amitkumar Karwar49125452009-09-30 20:04:38 -0700911 if (priv->is_deep_sleep)
912 continue;
913
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200914 /* Execute the next command */
David Woodhouseaa21c002007-12-08 20:04:36 +0000915 if (!priv->dnld_sent && !priv->cur_cmd)
Holger Schurig10078322007-11-15 18:05:47 -0500916 lbs_execute_next_command(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200917
918 /* Wake-up command waiters which can't sleep in
Holger Schurig10078322007-11-15 18:05:47 -0500919 * lbs_prepare_and_send_command
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200920 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000921 if (!list_empty(&priv->cmdpendingq))
922 wake_up_all(&priv->cmd_pending);
David Woodhouse2eb188a2007-12-09 23:54:27 -0500923
924 spin_lock_irq(&priv->driver_lock);
925 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
926 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
927 priv->tx_pending_buf,
928 priv->tx_pending_len);
929 if (ret) {
930 lbs_deb_tx("host_to_card failed %d\n", ret);
931 priv->dnld_sent = DNLD_RES_RECEIVED;
932 }
933 priv->tx_pending_len = 0;
934 if (!priv->currenttxskb) {
935 /* We can wake the queues immediately if we aren't
936 waiting for TX feedback */
937 if (priv->connect_status == LBS_CONNECTED)
938 netif_wake_queue(priv->dev);
939 if (priv->mesh_dev &&
940 priv->mesh_connect_status == LBS_CONNECTED)
941 netif_wake_queue(priv->mesh_dev);
942 }
943 }
944 spin_unlock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200945 }
946
David Woodhouseaa21c002007-12-08 20:04:36 +0000947 del_timer(&priv->command_timer);
Amitkumar Karwar49125452009-09-30 20:04:38 -0700948 del_timer(&priv->auto_deepsleep_timer);
David Woodhouseaa21c002007-12-08 20:04:36 +0000949 wake_up_all(&priv->cmd_pending);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950
Holger Schurig9012b282007-05-25 11:27:16 -0400951 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952 return 0;
953}
954
David Woodhouseab25eca2007-12-12 17:38:56 -0500955static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
956 struct cmd_header *cmd)
957{
Holger Schurig61d30022008-01-16 15:59:52 +0100958 lbs_deb_enter(LBS_DEB_FW);
David Woodhouseab25eca2007-12-12 17:38:56 -0500959
960 netif_device_detach(priv->dev);
961 if (priv->mesh_dev)
962 netif_device_detach(priv->mesh_dev);
963
964 priv->fw_ready = 0;
Holger Schurig61d30022008-01-16 15:59:52 +0100965 lbs_deb_leave(LBS_DEB_FW);
David Woodhouseab25eca2007-12-12 17:38:56 -0500966 return 0;
967}
968
David Woodhouseab25eca2007-12-12 17:38:56 -0500969int lbs_suspend(struct lbs_private *priv)
970{
971 struct cmd_header cmd;
972 int ret;
973
Holger Schurig61d30022008-01-16 15:59:52 +0100974 lbs_deb_enter(LBS_DEB_FW);
975
David Woodhouse506e9022007-12-12 20:06:06 -0500976 if (priv->wol_criteria == 0xffffffff) {
977 lbs_pr_info("Suspend attempt without configuring wake params!\n");
978 return -EINVAL;
979 }
980
David Woodhouseab25eca2007-12-12 17:38:56 -0500981 memset(&cmd, 0, sizeof(cmd));
David Woodhouse7e226272007-12-14 22:53:41 -0500982
David Woodhouseab25eca2007-12-12 17:38:56 -0500983 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
984 sizeof(cmd), lbs_suspend_callback, 0);
985 if (ret)
986 lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
987
Holger Schurig61d30022008-01-16 15:59:52 +0100988 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
David Woodhouseab25eca2007-12-12 17:38:56 -0500989 return ret;
990}
991EXPORT_SYMBOL_GPL(lbs_suspend);
992
Holger Schuriga63e5cb2008-04-30 10:50:39 +0200993void lbs_resume(struct lbs_private *priv)
David Woodhouseab25eca2007-12-12 17:38:56 -0500994{
Holger Schurig61d30022008-01-16 15:59:52 +0100995 lbs_deb_enter(LBS_DEB_FW);
996
David Woodhouseab25eca2007-12-12 17:38:56 -0500997 priv->fw_ready = 1;
998
999 /* Firmware doesn't seem to give us RX packets any more
1000 until we send it some command. Might as well update */
1001 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1002 0, 0, NULL);
1003
1004 netif_device_attach(priv->dev);
1005 if (priv->mesh_dev)
1006 netif_device_attach(priv->mesh_dev);
1007
Holger Schurig61d30022008-01-16 15:59:52 +01001008 lbs_deb_leave(LBS_DEB_FW);
David Woodhouseab25eca2007-12-12 17:38:56 -05001009}
1010EXPORT_SYMBOL_GPL(lbs_resume);
1011
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001012/**
Colin McCabeb4836592009-01-02 19:00:22 -08001013 * @brief This function gets the HW spec from the firmware and sets
1014 * some basic parameters.
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001015 *
Holger Schurig69f90322007-11-23 15:43:44 +01001016 * @param priv A pointer to struct lbs_private structure
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001017 * @return 0 or -1
1018 */
Holger Schurig69f90322007-11-23 15:43:44 +01001019static int lbs_setup_firmware(struct lbs_private *priv)
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001020{
1021 int ret = -1;
Dan Williams87c8c722008-08-19 15:15:35 -04001022 s16 curlevel = 0, minlevel = 0, maxlevel = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001023
1024 lbs_deb_enter(LBS_DEB_FW);
1025
Dan Williams87c8c722008-08-19 15:15:35 -04001026 /* Read MAC address from firmware */
David Woodhouseaa21c002007-12-08 20:04:36 +00001027 memset(priv->current_addr, 0xff, ETH_ALEN);
Dan Williams6e66f032007-12-11 12:42:16 -05001028 ret = lbs_update_hw_spec(priv);
Holger Schurigef85ad52008-05-14 16:27:18 +02001029 if (ret)
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001030 goto done;
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001031
Dan Williams87c8c722008-08-19 15:15:35 -04001032 /* Read power levels if available */
1033 ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
1034 if (ret == 0) {
1035 priv->txpower_cur = curlevel;
1036 priv->txpower_min = minlevel;
1037 priv->txpower_max = maxlevel;
1038 }
1039
Holger Schurigd9e97782008-03-12 16:06:43 +01001040 lbs_set_mac_control(priv);
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001041done:
1042 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
1043 return ret;
1044}
1045
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001046/**
1047 * This function handles the timeout of command sending.
1048 * It will re-send the same command again.
1049 */
1050static void command_timer_fn(unsigned long data)
1051{
Holger Schurig69f90322007-11-23 15:43:44 +01001052 struct lbs_private *priv = (struct lbs_private *)data;
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001053 unsigned long flags;
1054
Holger Schurig61d30022008-01-16 15:59:52 +01001055 lbs_deb_enter(LBS_DEB_CMD);
David Woodhouseaa21c002007-12-08 20:04:36 +00001056 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001057
Holger Schurig57962f02008-05-14 16:30:28 +02001058 if (!priv->cur_cmd)
David Woodhouse2a345092007-12-15 19:33:43 -05001059 goto out;
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001060
Holger Schurig57962f02008-05-14 16:30:28 +02001061 lbs_pr_info("command 0x%04x timed out\n",
1062 le16_to_cpu(priv->cur_cmd->cmdbuf->command));
David Woodhouse2a345092007-12-15 19:33:43 -05001063
1064 priv->cmd_timed_out = 1;
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001065 wake_up_interruptible(&priv->waitq);
Holger Schurig61d30022008-01-16 15:59:52 +01001066out:
David Woodhouse2a345092007-12-15 19:33:43 -05001067 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig61d30022008-01-16 15:59:52 +01001068 lbs_deb_leave(LBS_DEB_CMD);
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001069}
1070
Amitkumar Karwar49125452009-09-30 20:04:38 -07001071/**
1072 * This function put the device back to deep sleep mode when timer expires
1073 * and no activity (command, event, data etc.) is detected.
1074 */
1075static void auto_deepsleep_timer_fn(unsigned long data)
1076{
1077 struct lbs_private *priv = (struct lbs_private *)data;
1078 int ret;
1079
1080 lbs_deb_enter(LBS_DEB_CMD);
1081
1082 if (priv->is_activity_detected) {
1083 priv->is_activity_detected = 0;
1084 } else {
1085 if (priv->is_auto_deep_sleep_enabled &&
1086 (!priv->wakeup_dev_required) &&
1087 (priv->connect_status != LBS_CONNECTED)) {
1088 lbs_deb_main("Entering auto deep sleep mode...\n");
1089 ret = lbs_prepare_and_send_command(priv,
1090 CMD_802_11_DEEP_SLEEP, 0,
1091 0, 0, NULL);
1092 }
1093 }
1094 mod_timer(&priv->auto_deepsleep_timer , jiffies +
1095 (priv->auto_deep_sleep_timeout * HZ)/1000);
1096 lbs_deb_leave(LBS_DEB_CMD);
1097}
1098
1099int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
1100{
1101 lbs_deb_enter(LBS_DEB_SDIO);
1102
1103 priv->is_auto_deep_sleep_enabled = 1;
1104 if (priv->is_deep_sleep)
1105 priv->wakeup_dev_required = 1;
1106 mod_timer(&priv->auto_deepsleep_timer ,
1107 jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
1108
1109 lbs_deb_leave(LBS_DEB_SDIO);
1110 return 0;
1111}
1112
1113int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
1114{
1115 lbs_deb_enter(LBS_DEB_SDIO);
1116
1117 priv->is_auto_deep_sleep_enabled = 0;
1118 priv->auto_deep_sleep_timeout = 0;
1119 del_timer(&priv->auto_deepsleep_timer);
1120
1121 lbs_deb_leave(LBS_DEB_SDIO);
1122 return 0;
1123}
1124
Holger Schurige2268682008-01-28 17:26:28 +01001125static void lbs_sync_channel_worker(struct work_struct *work)
1126{
1127 struct lbs_private *priv = container_of(work, struct lbs_private,
1128 sync_channel);
1129
1130 lbs_deb_enter(LBS_DEB_MAIN);
1131 if (lbs_update_channel(priv))
1132 lbs_pr_info("Channel synchronization failed.");
1133 lbs_deb_leave(LBS_DEB_MAIN);
1134}
1135
1136
Holger Schurig69f90322007-11-23 15:43:44 +01001137static int lbs_init_adapter(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -04001138{
Dan Williams954ee162007-08-20 11:43:25 -04001139 size_t bufsize;
1140 int i, ret = 0;
1141
Holger Schurig61d30022008-01-16 15:59:52 +01001142 lbs_deb_enter(LBS_DEB_MAIN);
1143
Dan Williams954ee162007-08-20 11:43:25 -04001144 /* Allocate buffer to store the BSSID list */
1145 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
David Woodhouseaa21c002007-12-08 20:04:36 +00001146 priv->networks = kzalloc(bufsize, GFP_KERNEL);
1147 if (!priv->networks) {
Dan Williams954ee162007-08-20 11:43:25 -04001148 lbs_pr_err("Out of memory allocating beacons\n");
1149 ret = -1;
1150 goto out;
1151 }
1152
1153 /* Initialize scan result lists */
David Woodhouseaa21c002007-12-08 20:04:36 +00001154 INIT_LIST_HEAD(&priv->network_free_list);
1155 INIT_LIST_HEAD(&priv->network_list);
Dan Williams954ee162007-08-20 11:43:25 -04001156 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001157 list_add_tail(&priv->networks[i].list,
1158 &priv->network_free_list);
Dan Williams954ee162007-08-20 11:43:25 -04001159 }
1160
David Woodhouseaa21c002007-12-08 20:04:36 +00001161 memset(priv->current_addr, 0xff, ETH_ALEN);
Dan Williams954ee162007-08-20 11:43:25 -04001162
David Woodhouseaa21c002007-12-08 20:04:36 +00001163 priv->connect_status = LBS_DISCONNECTED;
1164 priv->mesh_connect_status = LBS_DISCONNECTED;
1165 priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1166 priv->mode = IW_MODE_INFRA;
1167 priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
Holger Schurigd9e97782008-03-12 16:06:43 +01001168 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
Dan Williamsd5db2df2008-08-21 17:51:07 -04001169 priv->radio_on = 1;
Javier Cardona85319f92008-05-24 10:59:49 +01001170 priv->enablehwauto = 1;
David Woodhouseaa21c002007-12-08 20:04:36 +00001171 priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1172 priv->psmode = LBS802_11POWERMODECAM;
1173 priv->psstate = PS_STATE_FULL_POWER;
Amitkumar Karwar49125452009-09-30 20:04:38 -07001174 priv->is_deep_sleep = 0;
1175 priv->is_auto_deep_sleep_enabled = 0;
1176 priv->wakeup_dev_required = 0;
1177 init_waitqueue_head(&priv->ds_awake_q);
Dan Williams954ee162007-08-20 11:43:25 -04001178
David Woodhouseaa21c002007-12-08 20:04:36 +00001179 mutex_init(&priv->lock);
Dan Williams954ee162007-08-20 11:43:25 -04001180
David Woodhouseaa21c002007-12-08 20:04:36 +00001181 setup_timer(&priv->command_timer, command_timer_fn,
Holger Schurig61d30022008-01-16 15:59:52 +01001182 (unsigned long)priv);
Amitkumar Karwar49125452009-09-30 20:04:38 -07001183 setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
1184 (unsigned long)priv);
Dan Williams954ee162007-08-20 11:43:25 -04001185
David Woodhouseaa21c002007-12-08 20:04:36 +00001186 INIT_LIST_HEAD(&priv->cmdfreeq);
1187 INIT_LIST_HEAD(&priv->cmdpendingq);
Dan Williams954ee162007-08-20 11:43:25 -04001188
David Woodhouseaa21c002007-12-08 20:04:36 +00001189 spin_lock_init(&priv->driver_lock);
1190 init_waitqueue_head(&priv->cmd_pending);
Dan Williams954ee162007-08-20 11:43:25 -04001191
1192 /* Allocate the command buffers */
Holger Schurig10078322007-11-15 18:05:47 -05001193 if (lbs_allocate_cmd_buffer(priv)) {
Dan Williams954ee162007-08-20 11:43:25 -04001194 lbs_pr_err("Out of memory allocating command buffers\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001195 ret = -ENOMEM;
1196 goto out;
1197 }
1198 priv->resp_idx = 0;
1199 priv->resp_len[0] = priv->resp_len[1] = 0;
1200
1201 /* Create the event FIFO */
1202 priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
1203 if (IS_ERR(priv->event_fifo)) {
1204 lbs_pr_err("Out of memory allocating event FIFO buffer\n");
1205 ret = -ENOMEM;
1206 goto out;
Dan Williams954ee162007-08-20 11:43:25 -04001207 }
1208
1209out:
Holger Schurig61d30022008-01-16 15:59:52 +01001210 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1211
Dan Williams954ee162007-08-20 11:43:25 -04001212 return ret;
1213}
1214
Holger Schurig69f90322007-11-23 15:43:44 +01001215static void lbs_free_adapter(struct lbs_private *priv)
Holger Schurigac558ca2007-08-02 11:49:06 -04001216{
Holger Schurig61d30022008-01-16 15:59:52 +01001217 lbs_deb_enter(LBS_DEB_MAIN);
1218
Holger Schurig10078322007-11-15 18:05:47 -05001219 lbs_free_cmd_buffer(priv);
Holger Schurig7919b892008-04-01 14:50:43 +02001220 if (priv->event_fifo)
1221 kfifo_free(priv->event_fifo);
David Woodhouseaa21c002007-12-08 20:04:36 +00001222 del_timer(&priv->command_timer);
Amitkumar Karwar49125452009-09-30 20:04:38 -07001223 del_timer(&priv->auto_deepsleep_timer);
David Woodhouseaa21c002007-12-08 20:04:36 +00001224 kfree(priv->networks);
1225 priv->networks = NULL;
Holger Schurig61d30022008-01-16 15:59:52 +01001226
1227 lbs_deb_leave(LBS_DEB_MAIN);
Holger Schurigac558ca2007-08-02 11:49:06 -04001228}
1229
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001230static const struct net_device_ops lbs_netdev_ops = {
1231 .ndo_open = lbs_dev_open,
1232 .ndo_stop = lbs_eth_stop,
1233 .ndo_start_xmit = lbs_hard_start_xmit,
1234 .ndo_set_mac_address = lbs_set_mac_address,
1235 .ndo_tx_timeout = lbs_tx_timeout,
1236 .ndo_set_multicast_list = lbs_set_multicast_list,
1237 .ndo_change_mtu = eth_change_mtu,
1238 .ndo_validate_addr = eth_validate_addr,
1239};
1240
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001241/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001242 * @brief This function adds the card. it will probe the
Holger Schurig10078322007-11-15 18:05:47 -05001243 * card, allocate the lbs_priv and initialize the device.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001244 *
1245 * @param card A pointer to card
Holger Schurig69f90322007-11-23 15:43:44 +01001246 * @return A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001247 */
Holger Schurig69f90322007-11-23 15:43:44 +01001248struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001249{
Holger Schurigff9fc792009-10-06 16:31:54 +02001250 struct net_device *dev;
1251 struct wireless_dev *wdev;
Holger Schurig69f90322007-11-23 15:43:44 +01001252 struct lbs_private *priv = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001253
Holger Schurig61d30022008-01-16 15:59:52 +01001254 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001255
1256 /* Allocate an Ethernet device and register it */
Holger Schurigff9fc792009-10-06 16:31:54 +02001257 wdev = lbs_cfg_alloc(dmdev);
1258 if (IS_ERR(wdev)) {
1259 lbs_pr_err("cfg80211 init failed\n");
Dan Williams954ee162007-08-20 11:43:25 -04001260 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001261 }
Holger Schurigff9fc792009-10-06 16:31:54 +02001262 /* TODO? */
1263 wdev->iftype = NL80211_IFTYPE_STATION;
1264 priv = wdev_priv(wdev);
1265 priv->wdev = wdev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266
Holger Schurig10078322007-11-15 18:05:47 -05001267 if (lbs_init_adapter(priv)) {
Dan Williams954ee162007-08-20 11:43:25 -04001268 lbs_pr_err("failed to initialize adapter structure.\n");
Holger Schurigff9fc792009-10-06 16:31:54 +02001269 goto err_wdev;
Dan Williams954ee162007-08-20 11:43:25 -04001270 }
1271
Holger Schurigff9fc792009-10-06 16:31:54 +02001272 //TODO? dev = alloc_netdev_mq(0, "wlan%d", ether_setup, IWM_TX_QUEUES);
1273 dev = alloc_netdev(0, "wlan%d", ether_setup);
1274 if (!dev) {
1275 dev_err(dmdev, "no memory for network device instance\n");
1276 goto err_adapter;
1277 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001278
Holger Schurigff9fc792009-10-06 16:31:54 +02001279 dev->ieee80211_ptr = wdev;
1280 dev->ml_priv = priv;
1281 SET_NETDEV_DEV(dev, dmdev);
1282 wdev->netdev = dev;
1283 priv->dev = dev;
1284
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001285 dev->netdev_ops = &lbs_netdev_ops;
Holger Schurigfb3dddf2007-05-25 11:58:22 -04001286 dev->watchdog_timeo = 5 * HZ;
Holger Schurig10078322007-11-15 18:05:47 -05001287 dev->ethtool_ops = &lbs_ethtool_ops;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288#ifdef WIRELESS_EXT
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001289 dev->wireless_handlers = &lbs_handler_def;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001291 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001292
Holger Schurigff9fc792009-10-06 16:31:54 +02001293
1294 // TODO: kzalloc + iwm_init_default_profile(iwm, iwm->umac_profile); ??
1295
1296
1297 priv->card = card;
1298 priv->mesh_open = 0;
1299 priv->infra_open = 0;
1300
Dan Williams7732ca42007-05-25 13:13:25 -04001301
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001302 priv->rtap_net_dev = NULL;
Daniel Mackc00552c2009-08-11 16:09:34 +02001303 strcpy(dev->name, "wlan%d");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001304
Dan Williamsfe336152007-08-02 11:32:25 -04001305 lbs_deb_thread("Starting main thread...\n");
1306 init_waitqueue_head(&priv->waitq);
Holger Schurig10078322007-11-15 18:05:47 -05001307 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
Dan Williamsfe336152007-08-02 11:32:25 -04001308 if (IS_ERR(priv->main_thread)) {
1309 lbs_deb_thread("Error creating main thread.\n");
Holger Schurigff9fc792009-10-06 16:31:54 +02001310 goto err_ndev;
Dan Williamsfe336152007-08-02 11:32:25 -04001311 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001312
Holger Schurig10078322007-11-15 18:05:47 -05001313 priv->work_thread = create_singlethread_workqueue("lbs_worker");
1314 INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
1315 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
David Woodhouse75bf45a2008-05-20 13:32:45 +01001316 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
Holger Schurige2268682008-01-28 17:26:28 +01001317 INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001318
David Woodhouse23a397a2007-12-11 18:56:42 -05001319 sprintf(priv->mesh_ssid, "mesh");
1320 priv->mesh_ssid_len = 4;
1321
David Woodhouse506e9022007-12-12 20:06:06 -05001322 priv->wol_criteria = 0xffffffff;
1323 priv->wol_gpio = 0xff;
1324
Dan Williams954ee162007-08-20 11:43:25 -04001325 goto done;
1326
Holger Schurigff9fc792009-10-06 16:31:54 +02001327 err_ndev:
Dan Williams954ee162007-08-20 11:43:25 -04001328 free_netdev(dev);
Holger Schurigff9fc792009-10-06 16:31:54 +02001329
1330 err_adapter:
1331 lbs_free_adapter(priv);
1332
1333 err_wdev:
1334 lbs_cfg_free(priv);
1335
Dan Williams954ee162007-08-20 11:43:25 -04001336 priv = NULL;
1337
1338done:
Holger Schurig61d30022008-01-16 15:59:52 +01001339 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
Dan Williams954ee162007-08-20 11:43:25 -04001340 return priv;
1341}
Holger Schurig10078322007-11-15 18:05:47 -05001342EXPORT_SYMBOL_GPL(lbs_add_card);
Dan Williams954ee162007-08-20 11:43:25 -04001343
1344
Holger Schuriga63e5cb2008-04-30 10:50:39 +02001345void lbs_remove_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -04001346{
Dan Williams954ee162007-08-20 11:43:25 -04001347 struct net_device *dev = priv->dev;
1348 union iwreq_data wrqu;
1349
1350 lbs_deb_enter(LBS_DEB_MAIN);
1351
David Woodhouse23a397a2007-12-11 18:56:42 -05001352 lbs_remove_mesh(priv);
Holger Schurig10078322007-11-15 18:05:47 -05001353 lbs_remove_rtap(priv);
Dan Williams954ee162007-08-20 11:43:25 -04001354
1355 dev = priv->dev;
Dan Williams954ee162007-08-20 11:43:25 -04001356
Holger Schurig652e3f22008-04-30 10:51:15 +02001357 cancel_delayed_work_sync(&priv->scan_work);
1358 cancel_delayed_work_sync(&priv->assoc_work);
David Woodhouse75bf45a2008-05-20 13:32:45 +01001359 cancel_work_sync(&priv->mcast_work);
Dan Williams71b35f32008-09-08 16:34:40 -04001360
1361 /* worker thread destruction blocks on the in-flight command which
1362 * should have been cleared already in lbs_stop_card().
1363 */
1364 lbs_deb_main("destroying worker thread\n");
Dan Williams954ee162007-08-20 11:43:25 -04001365 destroy_workqueue(priv->work_thread);
Dan Williams71b35f32008-09-08 16:34:40 -04001366 lbs_deb_main("done destroying worker thread\n");
Dan Williams954ee162007-08-20 11:43:25 -04001367
David Woodhouseaa21c002007-12-08 20:04:36 +00001368 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1369 priv->psmode = LBS802_11POWERMODECAM;
Holger Schurig10078322007-11-15 18:05:47 -05001370 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001371 }
1372
Dan Williams954ee162007-08-20 11:43:25 -04001373 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1374 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1375 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1376
Amitkumar Karwar49125452009-09-30 20:04:38 -07001377 if (priv->is_deep_sleep) {
1378 priv->is_deep_sleep = 0;
1379 wake_up_interruptible(&priv->ds_awake_q);
1380 }
1381
Dan Williams954ee162007-08-20 11:43:25 -04001382 /* Stop the thread servicing the interrupts */
David Woodhouseaa21c002007-12-08 20:04:36 +00001383 priv->surpriseremoved = 1;
Dan Williams954ee162007-08-20 11:43:25 -04001384 kthread_stop(priv->main_thread);
1385
Holger Schurig10078322007-11-15 18:05:47 -05001386 lbs_free_adapter(priv);
Holger Schurigff9fc792009-10-06 16:31:54 +02001387 lbs_cfg_free(priv);
Dan Williams954ee162007-08-20 11:43:25 -04001388
1389 priv->dev = NULL;
1390 free_netdev(dev);
1391
1392 lbs_deb_leave(LBS_DEB_MAIN);
Dan Williams954ee162007-08-20 11:43:25 -04001393}
Holger Schurig10078322007-11-15 18:05:47 -05001394EXPORT_SYMBOL_GPL(lbs_remove_card);
Dan Williams954ee162007-08-20 11:43:25 -04001395
1396
Holger Schurig69f90322007-11-23 15:43:44 +01001397int lbs_start_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -04001398{
1399 struct net_device *dev = priv->dev;
1400 int ret = -1;
1401
1402 lbs_deb_enter(LBS_DEB_MAIN);
1403
1404 /* poke the firmware */
Holger Schurig10078322007-11-15 18:05:47 -05001405 ret = lbs_setup_firmware(priv);
Dan Williams954ee162007-08-20 11:43:25 -04001406 if (ret)
1407 goto done;
1408
1409 /* init 802.11d */
Holger Schurig10078322007-11-15 18:05:47 -05001410 lbs_init_11d(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001411
Holger Schurigff9fc792009-10-06 16:31:54 +02001412 if (lbs_cfg_register(priv)) {
1413 lbs_pr_err("cannot register device\n");
Dan Williams954ee162007-08-20 11:43:25 -04001414 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001415 }
David Woodhouse020f3d02007-12-12 23:29:13 -05001416
David Woodhouse020f3d02007-12-12 23:29:13 -05001417 lbs_update_channel(priv);
David Woodhouse020f3d02007-12-12 23:29:13 -05001418
Bing Zhao684d6b32009-03-25 09:51:16 -07001419 /* Check mesh FW version and appropriately send the mesh start
1420 * command
1421 */
1422 if (priv->mesh_fw_ver == MESH_FW_OLD) {
Holger Schurigc9d1be362008-01-16 15:57:44 +01001423 /* Enable mesh, if supported, and work out which TLV it uses.
1424 0x100 + 291 is an unofficial value used in 5.110.20.pXX
1425 0x100 + 37 is the official value used in 5.110.21.pXX
1426 but we check them in that order because 20.pXX doesn't
1427 give an error -- it just silently fails. */
1428
1429 /* 5.110.20.pXX firmware will fail the command if the channel
1430 doesn't match the existing channel. But only if the TLV
1431 is correct. If the channel is wrong, _BOTH_ versions will
1432 give an error to 0x100+291, and allow 0x100+37 to succeed.
1433 It's just that 5.110.20.pXX will not have done anything
1434 useful */
1435
Bing Zhao684d6b32009-03-25 09:51:16 -07001436 priv->mesh_tlv = TLV_TYPE_OLD_MESH_ID;
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001437 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1438 priv->curbssparams.channel)) {
Bing Zhao684d6b32009-03-25 09:51:16 -07001439 priv->mesh_tlv = TLV_TYPE_MESH_ID;
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001440 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1441 priv->curbssparams.channel))
Holger Schurigc9d1be362008-01-16 15:57:44 +01001442 priv->mesh_tlv = 0;
1443 }
Bing Zhao684d6b32009-03-25 09:51:16 -07001444 } else if (priv->mesh_fw_ver == MESH_FW_NEW) {
1445 /* 10.0.0.pXX new firmwares should succeed with TLV
1446 * 0x100+37; Do not invoke command with old TLV.
1447 */
1448 priv->mesh_tlv = TLV_TYPE_MESH_ID;
1449 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1450 priv->curbssparams.channel))
1451 priv->mesh_tlv = 0;
1452 }
1453 if (priv->mesh_tlv) {
1454 lbs_add_mesh(priv);
Holger Schurigc9d1be362008-01-16 15:57:44 +01001455
Bing Zhao684d6b32009-03-25 09:51:16 -07001456 if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
1457 lbs_pr_err("cannot register lbs_mesh attribute\n");
Dan Williams3b72b012008-07-29 13:50:39 -04001458
Bing Zhao684d6b32009-03-25 09:51:16 -07001459 /* While rtap isn't related to mesh, only mesh-enabled
1460 * firmware implements the rtap functionality via
1461 * CMD_802_11_MONITOR_MODE.
1462 */
1463 if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1464 lbs_pr_err("cannot register lbs_rtap attribute\n");
David Woodhouse020f3d02007-12-12 23:29:13 -05001465 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001466
Holger Schurig10078322007-11-15 18:05:47 -05001467 lbs_debugfs_init_one(priv, dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001468
Dan Williams954ee162007-08-20 11:43:25 -04001469 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001470
Dan Williams954ee162007-08-20 11:43:25 -04001471 ret = 0;
1472
Holger Schurig32a74b72007-05-25 12:04:31 -04001473done:
Dan Williams954ee162007-08-20 11:43:25 -04001474 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
Holger Schurig32a74b72007-05-25 12:04:31 -04001475 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001476}
Holger Schurig10078322007-11-15 18:05:47 -05001477EXPORT_SYMBOL_GPL(lbs_start_card);
Dan Williams954ee162007-08-20 11:43:25 -04001478
1479
Holger Schuriga63e5cb2008-04-30 10:50:39 +02001480void lbs_stop_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -04001481{
Julia Lawall43baa5b2009-01-09 10:23:10 +00001482 struct net_device *dev;
Dan Williams954ee162007-08-20 11:43:25 -04001483 struct cmd_ctrl_node *cmdnode;
1484 unsigned long flags;
1485
1486 lbs_deb_enter(LBS_DEB_MAIN);
1487
Holger Schurig652e3f22008-04-30 10:51:15 +02001488 if (!priv)
1489 goto out;
Julia Lawall43baa5b2009-01-09 10:23:10 +00001490 dev = priv->dev;
Holger Schurig652e3f22008-04-30 10:51:15 +02001491
Julia Lawall43baa5b2009-01-09 10:23:10 +00001492 netif_stop_queue(dev);
1493 netif_carrier_off(dev);
Dan Williams954ee162007-08-20 11:43:25 -04001494
Holger Schurig10078322007-11-15 18:05:47 -05001495 lbs_debugfs_remove_one(priv);
Javier Cardona15dbaac2008-05-17 21:01:24 -07001496 if (priv->mesh_tlv) {
David Woodhouse020f3d02007-12-12 23:29:13 -05001497 device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
Dan Williams3b72b012008-07-29 13:50:39 -04001498 device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
Javier Cardona15dbaac2008-05-17 21:01:24 -07001499 }
Dan Williams954ee162007-08-20 11:43:25 -04001500
Dan Williams71b35f32008-09-08 16:34:40 -04001501 /* Delete the timeout of the currently processing command */
Holger Schurig652e3f22008-04-30 10:51:15 +02001502 del_timer_sync(&priv->command_timer);
Amitkumar Karwar49125452009-09-30 20:04:38 -07001503 del_timer_sync(&priv->auto_deepsleep_timer);
Dan Williams71b35f32008-09-08 16:34:40 -04001504
1505 /* Flush pending command nodes */
David Woodhouseaa21c002007-12-08 20:04:36 +00001506 spin_lock_irqsave(&priv->driver_lock, flags);
Dan Williams71b35f32008-09-08 16:34:40 -04001507 lbs_deb_main("clearing pending commands\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001508 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
David Woodhouseae125bf2007-12-15 04:22:52 -05001509 cmdnode->result = -ENOENT;
Dan Williams954ee162007-08-20 11:43:25 -04001510 cmdnode->cmdwaitqwoken = 1;
1511 wake_up_interruptible(&cmdnode->cmdwait_q);
1512 }
Dan Williams71b35f32008-09-08 16:34:40 -04001513
1514 /* Flush the command the card is currently processing */
1515 if (priv->cur_cmd) {
1516 lbs_deb_main("clearing current command\n");
1517 priv->cur_cmd->result = -ENOENT;
1518 priv->cur_cmd->cmdwaitqwoken = 1;
1519 wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
1520 }
1521 lbs_deb_main("done clearing commands\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001522 spin_unlock_irqrestore(&priv->driver_lock, flags);
Dan Williams954ee162007-08-20 11:43:25 -04001523
1524 unregister_netdev(dev);
1525
Holger Schurig652e3f22008-04-30 10:51:15 +02001526out:
Holger Schuriga63e5cb2008-04-30 10:50:39 +02001527 lbs_deb_leave(LBS_DEB_MAIN);
Dan Williams954ee162007-08-20 11:43:25 -04001528}
Holger Schurig10078322007-11-15 18:05:47 -05001529EXPORT_SYMBOL_GPL(lbs_stop_card);
Holger Schurig084708b2007-05-25 12:37:58 -04001530
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001531
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001532static const struct net_device_ops mesh_netdev_ops = {
1533 .ndo_open = lbs_dev_open,
1534 .ndo_stop = lbs_mesh_stop,
1535 .ndo_start_xmit = lbs_hard_start_xmit,
1536 .ndo_set_mac_address = lbs_set_mac_address,
1537 .ndo_set_multicast_list = lbs_set_multicast_list,
1538};
1539
Holger Schurig78523da2007-05-25 11:49:19 -04001540/**
1541 * @brief This function adds mshX interface
1542 *
Holger Schurig69f90322007-11-23 15:43:44 +01001543 * @param priv A pointer to the struct lbs_private structure
Holger Schurig78523da2007-05-25 11:49:19 -04001544 * @return 0 if successful, -X otherwise
1545 */
David Woodhouse23a397a2007-12-11 18:56:42 -05001546static int lbs_add_mesh(struct lbs_private *priv)
Holger Schurig78523da2007-05-25 11:49:19 -04001547{
1548 struct net_device *mesh_dev = NULL;
1549 int ret = 0;
1550
1551 lbs_deb_enter(LBS_DEB_MESH);
1552
1553 /* Allocate a virtual mesh device */
1554 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1555 lbs_deb_mesh("init mshX device failed\n");
1556 ret = -ENOMEM;
1557 goto done;
1558 }
Wang Chen4ceb7b62008-09-05 11:28:47 +08001559 mesh_dev->ml_priv = priv;
Holger Schurig78523da2007-05-25 11:49:19 -04001560 priv->mesh_dev = mesh_dev;
1561
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001562 mesh_dev->netdev_ops = &mesh_netdev_ops;
Holger Schurig10078322007-11-15 18:05:47 -05001563 mesh_dev->ethtool_ops = &lbs_ethtool_ops;
Holger Schurig634b8f42007-05-25 13:05:16 -04001564 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1565 sizeof(priv->dev->dev_addr));
Holger Schurig78523da2007-05-25 11:49:19 -04001566
David Woodhouse23a397a2007-12-11 18:56:42 -05001567 SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
Dan Williams7732ca42007-05-25 13:13:25 -04001568
Holger Schurig78523da2007-05-25 11:49:19 -04001569#ifdef WIRELESS_EXT
Luis Carlos Cobo Rusf5e05b62007-05-25 23:08:34 -04001570 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
Holger Schurig78523da2007-05-25 11:49:19 -04001571#endif
David Woodhouse75bf45a2008-05-20 13:32:45 +01001572 mesh_dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Holger Schurig78523da2007-05-25 11:49:19 -04001573 /* Register virtual mesh interface */
1574 ret = register_netdev(mesh_dev);
1575 if (ret) {
1576 lbs_pr_err("cannot register mshX virtual interface\n");
1577 goto err_free;
1578 }
1579
Holger Schurig10078322007-11-15 18:05:47 -05001580 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
Holger Schurig78523da2007-05-25 11:49:19 -04001581 if (ret)
1582 goto err_unregister;
1583
Javier Cardona15dbaac2008-05-17 21:01:24 -07001584 lbs_persist_config_init(mesh_dev);
1585
Holger Schurig78523da2007-05-25 11:49:19 -04001586 /* Everything successful */
1587 ret = 0;
1588 goto done;
1589
Holger Schurig78523da2007-05-25 11:49:19 -04001590err_unregister:
1591 unregister_netdev(mesh_dev);
1592
1593err_free:
1594 free_netdev(mesh_dev);
1595
1596done:
1597 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1598 return ret;
1599}
Holger Schurig084708b2007-05-25 12:37:58 -04001600
David Woodhouse23a397a2007-12-11 18:56:42 -05001601static void lbs_remove_mesh(struct lbs_private *priv)
Holger Schurig78523da2007-05-25 11:49:19 -04001602{
1603 struct net_device *mesh_dev;
1604
Holger Schurig78523da2007-05-25 11:49:19 -04001605
1606 mesh_dev = priv->mesh_dev;
David Woodhouse23a397a2007-12-11 18:56:42 -05001607 if (!mesh_dev)
Holger Schurig61d30022008-01-16 15:59:52 +01001608 return;
Holger Schurig78523da2007-05-25 11:49:19 -04001609
Holger Schurig61d30022008-01-16 15:59:52 +01001610 lbs_deb_enter(LBS_DEB_MESH);
Holger Schurig78523da2007-05-25 11:49:19 -04001611 netif_stop_queue(mesh_dev);
Javier Cardona15dbaac2008-05-17 21:01:24 -07001612 netif_carrier_off(mesh_dev);
Holger Schurig10078322007-11-15 18:05:47 -05001613 sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
Javier Cardona15dbaac2008-05-17 21:01:24 -07001614 lbs_persist_config_remove(mesh_dev);
Holger Schurig78523da2007-05-25 11:49:19 -04001615 unregister_netdev(mesh_dev);
David Woodhouse23a397a2007-12-11 18:56:42 -05001616 priv->mesh_dev = NULL;
Holger Schurig78523da2007-05-25 11:49:19 -04001617 free_netdev(mesh_dev);
Holger Schurig61d30022008-01-16 15:59:52 +01001618 lbs_deb_leave(LBS_DEB_MESH);
Holger Schurig78523da2007-05-25 11:49:19 -04001619}
1620
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001621/**
1622 * @brief This function finds the CFP in
1623 * region_cfp_table based on region and band parameter.
1624 *
1625 * @param region The region code
1626 * @param band The band
1627 * @param cfp_no A pointer to CFP number
1628 * @return A pointer to CFP
1629 */
Holger Schurige98a88d2008-03-19 14:25:58 +01001630struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001631{
1632 int i, end;
1633
Holger Schurig9012b282007-05-25 11:27:16 -04001634 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001635
Denis Chengff8ac602007-09-02 18:30:18 +08001636 end = ARRAY_SIZE(region_cfp_table);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001637
1638 for (i = 0; i < end ; i++) {
Holger Schurig9012b282007-05-25 11:27:16 -04001639 lbs_deb_main("region_cfp_table[i].region=%d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001640 region_cfp_table[i].region);
1641 if (region_cfp_table[i].region == region) {
1642 *cfp_no = region_cfp_table[i].cfp_no_BG;
Holger Schurig9012b282007-05-25 11:27:16 -04001643 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001644 return region_cfp_table[i].cfp_BG;
1645 }
1646 }
1647
Holger Schurig9012b282007-05-25 11:27:16 -04001648 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001649 return NULL;
1650}
1651
Holger Schurig69f90322007-11-23 15:43:44 +01001652int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001653{
Holger Schurig9012b282007-05-25 11:27:16 -04001654 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001655 int i = 0;
1656
1657 struct chan_freq_power *cfp;
1658 int cfp_no;
1659
Holger Schurig9012b282007-05-25 11:27:16 -04001660 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001661
David Woodhouseaa21c002007-12-08 20:04:36 +00001662 memset(priv->region_channel, 0, sizeof(priv->region_channel));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001663
Holger Schurige98a88d2008-03-19 14:25:58 +01001664 cfp = lbs_get_region_cfp_table(region, &cfp_no);
Holger Schurig61d30022008-01-16 15:59:52 +01001665 if (cfp != NULL) {
1666 priv->region_channel[i].nrcfp = cfp_no;
1667 priv->region_channel[i].CFP = cfp;
1668 } else {
1669 lbs_deb_main("wrong region code %#x in band B/G\n",
1670 region);
1671 ret = -1;
1672 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001673 }
Holger Schurig61d30022008-01-16 15:59:52 +01001674 priv->region_channel[i].valid = 1;
1675 priv->region_channel[i].region = region;
1676 priv->region_channel[i].band = band;
1677 i++;
Holger Schurig9012b282007-05-25 11:27:16 -04001678out:
1679 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1680 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001681}
1682
Holger Schurig7919b892008-04-01 14:50:43 +02001683void lbs_queue_event(struct lbs_private *priv, u32 event)
1684{
1685 unsigned long flags;
1686
1687 lbs_deb_enter(LBS_DEB_THREAD);
1688 spin_lock_irqsave(&priv->driver_lock, flags);
1689
1690 if (priv->psstate == PS_STATE_SLEEP)
1691 priv->psstate = PS_STATE_AWAKE;
1692
1693 __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1694
1695 wake_up_interruptible(&priv->waitq);
1696
1697 spin_unlock_irqrestore(&priv->driver_lock, flags);
1698 lbs_deb_leave(LBS_DEB_THREAD);
1699}
1700EXPORT_SYMBOL_GPL(lbs_queue_event);
1701
1702void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001703{
Holger Schuriged37b512007-05-25 11:52:42 -04001704 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001705
David Woodhouse4f679492007-12-10 14:58:37 -05001706 if (priv->psstate == PS_STATE_SLEEP)
David Woodhouseaa21c002007-12-08 20:04:36 +00001707 priv->psstate = PS_STATE_AWAKE;
Holger Schurig7919b892008-04-01 14:50:43 +02001708
1709 /* Swap buffers by flipping the response index */
1710 BUG_ON(resp_idx > 1);
1711 priv->resp_idx = resp_idx;
1712
Dan Williamsfe336152007-08-02 11:32:25 -04001713 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001714
Holger Schuriged37b512007-05-25 11:52:42 -04001715 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001716}
Holger Schurig7919b892008-04-01 14:50:43 +02001717EXPORT_SYMBOL_GPL(lbs_notify_command_response);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001718
Andres Salomon4fb910f2007-11-20 17:43:45 -05001719static int __init lbs_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001720{
Holger Schurig9012b282007-05-25 11:27:16 -04001721 lbs_deb_enter(LBS_DEB_MAIN);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001722 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1723 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1724 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1725 confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
Holger Schurig10078322007-11-15 18:05:47 -05001726 lbs_debugfs_init();
Holger Schurig084708b2007-05-25 12:37:58 -04001727 lbs_deb_leave(LBS_DEB_MAIN);
1728 return 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001729}
1730
Andres Salomon4fb910f2007-11-20 17:43:45 -05001731static void __exit lbs_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001732{
Holger Schurig9012b282007-05-25 11:27:16 -04001733 lbs_deb_enter(LBS_DEB_MAIN);
Holger Schurig10078322007-11-15 18:05:47 -05001734 lbs_debugfs_remove();
Holger Schurig9012b282007-05-25 11:27:16 -04001735 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001736}
1737
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001738/*
1739 * rtap interface support fuctions
1740 */
1741
Holger Schurig10078322007-11-15 18:05:47 -05001742static int lbs_rtap_open(struct net_device *dev)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001743{
David Woodhouse8552855f2007-12-10 16:38:18 -05001744 /* Yes, _stop_ the queue. Because we don't support injection */
Holger Schurig61d30022008-01-16 15:59:52 +01001745 lbs_deb_enter(LBS_DEB_MAIN);
1746 netif_carrier_off(dev);
1747 netif_stop_queue(dev);
1748 lbs_deb_leave(LBS_DEB_LEAVE);
1749 return 0;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001750}
1751
Holger Schurig10078322007-11-15 18:05:47 -05001752static int lbs_rtap_stop(struct net_device *dev)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001753{
Holger Schurig61d30022008-01-16 15:59:52 +01001754 lbs_deb_enter(LBS_DEB_MAIN);
1755 lbs_deb_leave(LBS_DEB_MAIN);
1756 return 0;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001757}
1758
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00001759static netdev_tx_t lbs_rtap_hard_start_xmit(struct sk_buff *skb,
1760 struct net_device *dev)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001761{
Holger Schurig61d30022008-01-16 15:59:52 +01001762 netif_stop_queue(dev);
1763 return NETDEV_TX_BUSY;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001764}
1765
David Woodhouse2fd6cfe2007-12-11 17:44:10 -05001766static void lbs_remove_rtap(struct lbs_private *priv)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001767{
Holger Schurig61d30022008-01-16 15:59:52 +01001768 lbs_deb_enter(LBS_DEB_MAIN);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001769 if (priv->rtap_net_dev == NULL)
Holger Schurig5f505d92008-04-30 10:50:07 +02001770 goto out;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001771 unregister_netdev(priv->rtap_net_dev);
David Woodhoused9268fb2007-12-09 16:22:21 -05001772 free_netdev(priv->rtap_net_dev);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001773 priv->rtap_net_dev = NULL;
Holger Schurig5f505d92008-04-30 10:50:07 +02001774out:
Holger Schurig61d30022008-01-16 15:59:52 +01001775 lbs_deb_leave(LBS_DEB_MAIN);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001776}
1777
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001778static const struct net_device_ops rtap_netdev_ops = {
1779 .ndo_open = lbs_rtap_open,
1780 .ndo_stop = lbs_rtap_stop,
1781 .ndo_start_xmit = lbs_rtap_hard_start_xmit,
1782};
1783
David Woodhouse2fd6cfe2007-12-11 17:44:10 -05001784static int lbs_add_rtap(struct lbs_private *priv)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001785{
Holger Schurig61d30022008-01-16 15:59:52 +01001786 int ret = 0;
David Woodhoused9268fb2007-12-09 16:22:21 -05001787 struct net_device *rtap_dev;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001788
Holger Schurig61d30022008-01-16 15:59:52 +01001789 lbs_deb_enter(LBS_DEB_MAIN);
1790 if (priv->rtap_net_dev) {
1791 ret = -EPERM;
1792 goto out;
1793 }
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001794
David Woodhoused9268fb2007-12-09 16:22:21 -05001795 rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
Holger Schurig61d30022008-01-16 15:59:52 +01001796 if (rtap_dev == NULL) {
1797 ret = -ENOMEM;
1798 goto out;
1799 }
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001800
David Woodhouse121947c2007-12-09 19:54:11 -05001801 memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
David Woodhoused9268fb2007-12-09 16:22:21 -05001802 rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001803 rtap_dev->netdev_ops = &rtap_netdev_ops;
Wang Chen4ceb7b62008-09-05 11:28:47 +08001804 rtap_dev->ml_priv = priv;
Masakazu Mokuno229ce3a2008-05-14 14:16:50 +09001805 SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001806
Holger Schurig61d30022008-01-16 15:59:52 +01001807 ret = register_netdev(rtap_dev);
1808 if (ret) {
David Woodhoused9268fb2007-12-09 16:22:21 -05001809 free_netdev(rtap_dev);
Holger Schurig61d30022008-01-16 15:59:52 +01001810 goto out;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001811 }
David Woodhoused9268fb2007-12-09 16:22:21 -05001812 priv->rtap_net_dev = rtap_dev;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001813
Holger Schurig61d30022008-01-16 15:59:52 +01001814out:
1815 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1816 return ret;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001817}
1818
Holger Schurig10078322007-11-15 18:05:47 -05001819module_init(lbs_init_module);
1820module_exit(lbs_exit_module);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001821
Holger Schurig084708b2007-05-25 12:37:58 -04001822MODULE_DESCRIPTION("Libertas WLAN Driver Library");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001823MODULE_AUTHOR("Marvell International Ltd.");
1824MODULE_LICENSE("GPL");