blob: b7363236cc539f99239779b161251bbfc77ce1c6 [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);
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -07001092 if (ret)
1093 lbs_pr_err("Enter Deep Sleep command failed\n");
Amitkumar Karwar49125452009-09-30 20:04:38 -07001094 }
1095 }
1096 mod_timer(&priv->auto_deepsleep_timer , jiffies +
1097 (priv->auto_deep_sleep_timeout * HZ)/1000);
1098 lbs_deb_leave(LBS_DEB_CMD);
1099}
1100
1101int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
1102{
1103 lbs_deb_enter(LBS_DEB_SDIO);
1104
1105 priv->is_auto_deep_sleep_enabled = 1;
1106 if (priv->is_deep_sleep)
1107 priv->wakeup_dev_required = 1;
1108 mod_timer(&priv->auto_deepsleep_timer ,
1109 jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
1110
1111 lbs_deb_leave(LBS_DEB_SDIO);
1112 return 0;
1113}
1114
1115int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
1116{
1117 lbs_deb_enter(LBS_DEB_SDIO);
1118
1119 priv->is_auto_deep_sleep_enabled = 0;
1120 priv->auto_deep_sleep_timeout = 0;
1121 del_timer(&priv->auto_deepsleep_timer);
1122
1123 lbs_deb_leave(LBS_DEB_SDIO);
1124 return 0;
1125}
1126
Holger Schurige2268682008-01-28 17:26:28 +01001127static void lbs_sync_channel_worker(struct work_struct *work)
1128{
1129 struct lbs_private *priv = container_of(work, struct lbs_private,
1130 sync_channel);
1131
1132 lbs_deb_enter(LBS_DEB_MAIN);
1133 if (lbs_update_channel(priv))
1134 lbs_pr_info("Channel synchronization failed.");
1135 lbs_deb_leave(LBS_DEB_MAIN);
1136}
1137
1138
Holger Schurig69f90322007-11-23 15:43:44 +01001139static int lbs_init_adapter(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -04001140{
Dan Williams954ee162007-08-20 11:43:25 -04001141 size_t bufsize;
1142 int i, ret = 0;
1143
Holger Schurig61d30022008-01-16 15:59:52 +01001144 lbs_deb_enter(LBS_DEB_MAIN);
1145
Dan Williams954ee162007-08-20 11:43:25 -04001146 /* Allocate buffer to store the BSSID list */
1147 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
David Woodhouseaa21c002007-12-08 20:04:36 +00001148 priv->networks = kzalloc(bufsize, GFP_KERNEL);
1149 if (!priv->networks) {
Dan Williams954ee162007-08-20 11:43:25 -04001150 lbs_pr_err("Out of memory allocating beacons\n");
1151 ret = -1;
1152 goto out;
1153 }
1154
1155 /* Initialize scan result lists */
David Woodhouseaa21c002007-12-08 20:04:36 +00001156 INIT_LIST_HEAD(&priv->network_free_list);
1157 INIT_LIST_HEAD(&priv->network_list);
Dan Williams954ee162007-08-20 11:43:25 -04001158 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001159 list_add_tail(&priv->networks[i].list,
1160 &priv->network_free_list);
Dan Williams954ee162007-08-20 11:43:25 -04001161 }
1162
David Woodhouseaa21c002007-12-08 20:04:36 +00001163 memset(priv->current_addr, 0xff, ETH_ALEN);
Dan Williams954ee162007-08-20 11:43:25 -04001164
David Woodhouseaa21c002007-12-08 20:04:36 +00001165 priv->connect_status = LBS_DISCONNECTED;
1166 priv->mesh_connect_status = LBS_DISCONNECTED;
1167 priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1168 priv->mode = IW_MODE_INFRA;
1169 priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
Holger Schurigd9e97782008-03-12 16:06:43 +01001170 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
Dan Williamsd5db2df2008-08-21 17:51:07 -04001171 priv->radio_on = 1;
Javier Cardona85319f92008-05-24 10:59:49 +01001172 priv->enablehwauto = 1;
David Woodhouseaa21c002007-12-08 20:04:36 +00001173 priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1174 priv->psmode = LBS802_11POWERMODECAM;
1175 priv->psstate = PS_STATE_FULL_POWER;
Amitkumar Karwar49125452009-09-30 20:04:38 -07001176 priv->is_deep_sleep = 0;
1177 priv->is_auto_deep_sleep_enabled = 0;
1178 priv->wakeup_dev_required = 0;
1179 init_waitqueue_head(&priv->ds_awake_q);
Dan Williams954ee162007-08-20 11:43:25 -04001180
David Woodhouseaa21c002007-12-08 20:04:36 +00001181 mutex_init(&priv->lock);
Dan Williams954ee162007-08-20 11:43:25 -04001182
David Woodhouseaa21c002007-12-08 20:04:36 +00001183 setup_timer(&priv->command_timer, command_timer_fn,
Holger Schurig61d30022008-01-16 15:59:52 +01001184 (unsigned long)priv);
Amitkumar Karwar49125452009-09-30 20:04:38 -07001185 setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
1186 (unsigned long)priv);
Dan Williams954ee162007-08-20 11:43:25 -04001187
David Woodhouseaa21c002007-12-08 20:04:36 +00001188 INIT_LIST_HEAD(&priv->cmdfreeq);
1189 INIT_LIST_HEAD(&priv->cmdpendingq);
Dan Williams954ee162007-08-20 11:43:25 -04001190
David Woodhouseaa21c002007-12-08 20:04:36 +00001191 spin_lock_init(&priv->driver_lock);
1192 init_waitqueue_head(&priv->cmd_pending);
Dan Williams954ee162007-08-20 11:43:25 -04001193
1194 /* Allocate the command buffers */
Holger Schurig10078322007-11-15 18:05:47 -05001195 if (lbs_allocate_cmd_buffer(priv)) {
Dan Williams954ee162007-08-20 11:43:25 -04001196 lbs_pr_err("Out of memory allocating command buffers\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001197 ret = -ENOMEM;
1198 goto out;
1199 }
1200 priv->resp_idx = 0;
1201 priv->resp_len[0] = priv->resp_len[1] = 0;
1202
1203 /* Create the event FIFO */
1204 priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
1205 if (IS_ERR(priv->event_fifo)) {
1206 lbs_pr_err("Out of memory allocating event FIFO buffer\n");
1207 ret = -ENOMEM;
1208 goto out;
Dan Williams954ee162007-08-20 11:43:25 -04001209 }
1210
1211out:
Holger Schurig61d30022008-01-16 15:59:52 +01001212 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1213
Dan Williams954ee162007-08-20 11:43:25 -04001214 return ret;
1215}
1216
Holger Schurig69f90322007-11-23 15:43:44 +01001217static void lbs_free_adapter(struct lbs_private *priv)
Holger Schurigac558ca2007-08-02 11:49:06 -04001218{
Holger Schurig61d30022008-01-16 15:59:52 +01001219 lbs_deb_enter(LBS_DEB_MAIN);
1220
Holger Schurig10078322007-11-15 18:05:47 -05001221 lbs_free_cmd_buffer(priv);
Holger Schurig7919b892008-04-01 14:50:43 +02001222 if (priv->event_fifo)
1223 kfifo_free(priv->event_fifo);
David Woodhouseaa21c002007-12-08 20:04:36 +00001224 del_timer(&priv->command_timer);
Amitkumar Karwar49125452009-09-30 20:04:38 -07001225 del_timer(&priv->auto_deepsleep_timer);
David Woodhouseaa21c002007-12-08 20:04:36 +00001226 kfree(priv->networks);
1227 priv->networks = NULL;
Holger Schurig61d30022008-01-16 15:59:52 +01001228
1229 lbs_deb_leave(LBS_DEB_MAIN);
Holger Schurigac558ca2007-08-02 11:49:06 -04001230}
1231
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001232static const struct net_device_ops lbs_netdev_ops = {
1233 .ndo_open = lbs_dev_open,
1234 .ndo_stop = lbs_eth_stop,
1235 .ndo_start_xmit = lbs_hard_start_xmit,
1236 .ndo_set_mac_address = lbs_set_mac_address,
1237 .ndo_tx_timeout = lbs_tx_timeout,
1238 .ndo_set_multicast_list = lbs_set_multicast_list,
1239 .ndo_change_mtu = eth_change_mtu,
1240 .ndo_validate_addr = eth_validate_addr,
1241};
1242
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001243/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001244 * @brief This function adds the card. it will probe the
Holger Schurig10078322007-11-15 18:05:47 -05001245 * card, allocate the lbs_priv and initialize the device.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001246 *
1247 * @param card A pointer to card
Holger Schurig69f90322007-11-23 15:43:44 +01001248 * @return A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001249 */
Holger Schurig69f90322007-11-23 15:43:44 +01001250struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001251{
Holger Schurigff9fc792009-10-06 16:31:54 +02001252 struct net_device *dev;
1253 struct wireless_dev *wdev;
Holger Schurig69f90322007-11-23 15:43:44 +01001254 struct lbs_private *priv = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001255
Holger Schurig61d30022008-01-16 15:59:52 +01001256 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001257
1258 /* Allocate an Ethernet device and register it */
Holger Schurigff9fc792009-10-06 16:31:54 +02001259 wdev = lbs_cfg_alloc(dmdev);
1260 if (IS_ERR(wdev)) {
1261 lbs_pr_err("cfg80211 init failed\n");
Dan Williams954ee162007-08-20 11:43:25 -04001262 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001263 }
Holger Schurigff9fc792009-10-06 16:31:54 +02001264 /* TODO? */
1265 wdev->iftype = NL80211_IFTYPE_STATION;
1266 priv = wdev_priv(wdev);
1267 priv->wdev = wdev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001268
Holger Schurig10078322007-11-15 18:05:47 -05001269 if (lbs_init_adapter(priv)) {
Dan Williams954ee162007-08-20 11:43:25 -04001270 lbs_pr_err("failed to initialize adapter structure.\n");
Holger Schurigff9fc792009-10-06 16:31:54 +02001271 goto err_wdev;
Dan Williams954ee162007-08-20 11:43:25 -04001272 }
1273
Holger Schurigff9fc792009-10-06 16:31:54 +02001274 //TODO? dev = alloc_netdev_mq(0, "wlan%d", ether_setup, IWM_TX_QUEUES);
1275 dev = alloc_netdev(0, "wlan%d", ether_setup);
1276 if (!dev) {
1277 dev_err(dmdev, "no memory for network device instance\n");
1278 goto err_adapter;
1279 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001280
Holger Schurigff9fc792009-10-06 16:31:54 +02001281 dev->ieee80211_ptr = wdev;
1282 dev->ml_priv = priv;
1283 SET_NETDEV_DEV(dev, dmdev);
1284 wdev->netdev = dev;
1285 priv->dev = dev;
1286
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001287 dev->netdev_ops = &lbs_netdev_ops;
Holger Schurigfb3dddf2007-05-25 11:58:22 -04001288 dev->watchdog_timeo = 5 * HZ;
Holger Schurig10078322007-11-15 18:05:47 -05001289 dev->ethtool_ops = &lbs_ethtool_ops;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290#ifdef WIRELESS_EXT
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001291 dev->wireless_handlers = &lbs_handler_def;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001292#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294
Holger Schurigff9fc792009-10-06 16:31:54 +02001295
1296 // TODO: kzalloc + iwm_init_default_profile(iwm, iwm->umac_profile); ??
1297
1298
1299 priv->card = card;
1300 priv->mesh_open = 0;
1301 priv->infra_open = 0;
1302
Dan Williams7732ca42007-05-25 13:13:25 -04001303
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001304 priv->rtap_net_dev = NULL;
Daniel Mackc00552c2009-08-11 16:09:34 +02001305 strcpy(dev->name, "wlan%d");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001306
Dan Williamsfe336152007-08-02 11:32:25 -04001307 lbs_deb_thread("Starting main thread...\n");
1308 init_waitqueue_head(&priv->waitq);
Holger Schurig10078322007-11-15 18:05:47 -05001309 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
Dan Williamsfe336152007-08-02 11:32:25 -04001310 if (IS_ERR(priv->main_thread)) {
1311 lbs_deb_thread("Error creating main thread.\n");
Holger Schurigff9fc792009-10-06 16:31:54 +02001312 goto err_ndev;
Dan Williamsfe336152007-08-02 11:32:25 -04001313 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001314
Holger Schurig10078322007-11-15 18:05:47 -05001315 priv->work_thread = create_singlethread_workqueue("lbs_worker");
1316 INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
1317 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
David Woodhouse75bf45a2008-05-20 13:32:45 +01001318 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
Holger Schurige2268682008-01-28 17:26:28 +01001319 INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001320
David Woodhouse23a397a2007-12-11 18:56:42 -05001321 sprintf(priv->mesh_ssid, "mesh");
1322 priv->mesh_ssid_len = 4;
1323
David Woodhouse506e9022007-12-12 20:06:06 -05001324 priv->wol_criteria = 0xffffffff;
1325 priv->wol_gpio = 0xff;
1326
Dan Williams954ee162007-08-20 11:43:25 -04001327 goto done;
1328
Holger Schurigff9fc792009-10-06 16:31:54 +02001329 err_ndev:
Dan Williams954ee162007-08-20 11:43:25 -04001330 free_netdev(dev);
Holger Schurigff9fc792009-10-06 16:31:54 +02001331
1332 err_adapter:
1333 lbs_free_adapter(priv);
1334
1335 err_wdev:
1336 lbs_cfg_free(priv);
1337
Dan Williams954ee162007-08-20 11:43:25 -04001338 priv = NULL;
1339
1340done:
Holger Schurig61d30022008-01-16 15:59:52 +01001341 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
Dan Williams954ee162007-08-20 11:43:25 -04001342 return priv;
1343}
Holger Schurig10078322007-11-15 18:05:47 -05001344EXPORT_SYMBOL_GPL(lbs_add_card);
Dan Williams954ee162007-08-20 11:43:25 -04001345
1346
Holger Schuriga63e5cb2008-04-30 10:50:39 +02001347void lbs_remove_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -04001348{
Dan Williams954ee162007-08-20 11:43:25 -04001349 struct net_device *dev = priv->dev;
1350 union iwreq_data wrqu;
1351
1352 lbs_deb_enter(LBS_DEB_MAIN);
1353
David Woodhouse23a397a2007-12-11 18:56:42 -05001354 lbs_remove_mesh(priv);
Holger Schurig10078322007-11-15 18:05:47 -05001355 lbs_remove_rtap(priv);
Dan Williams954ee162007-08-20 11:43:25 -04001356
1357 dev = priv->dev;
Dan Williams954ee162007-08-20 11:43:25 -04001358
Holger Schurig652e3f22008-04-30 10:51:15 +02001359 cancel_delayed_work_sync(&priv->scan_work);
1360 cancel_delayed_work_sync(&priv->assoc_work);
David Woodhouse75bf45a2008-05-20 13:32:45 +01001361 cancel_work_sync(&priv->mcast_work);
Dan Williams71b35f32008-09-08 16:34:40 -04001362
1363 /* worker thread destruction blocks on the in-flight command which
1364 * should have been cleared already in lbs_stop_card().
1365 */
1366 lbs_deb_main("destroying worker thread\n");
Dan Williams954ee162007-08-20 11:43:25 -04001367 destroy_workqueue(priv->work_thread);
Dan Williams71b35f32008-09-08 16:34:40 -04001368 lbs_deb_main("done destroying worker thread\n");
Dan Williams954ee162007-08-20 11:43:25 -04001369
David Woodhouseaa21c002007-12-08 20:04:36 +00001370 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1371 priv->psmode = LBS802_11POWERMODECAM;
Holger Schurig10078322007-11-15 18:05:47 -05001372 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001373 }
1374
Dan Williams954ee162007-08-20 11:43:25 -04001375 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1376 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1377 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1378
Amitkumar Karwar49125452009-09-30 20:04:38 -07001379 if (priv->is_deep_sleep) {
1380 priv->is_deep_sleep = 0;
1381 wake_up_interruptible(&priv->ds_awake_q);
1382 }
1383
Dan Williams954ee162007-08-20 11:43:25 -04001384 /* Stop the thread servicing the interrupts */
David Woodhouseaa21c002007-12-08 20:04:36 +00001385 priv->surpriseremoved = 1;
Dan Williams954ee162007-08-20 11:43:25 -04001386 kthread_stop(priv->main_thread);
1387
Holger Schurig10078322007-11-15 18:05:47 -05001388 lbs_free_adapter(priv);
Holger Schurigff9fc792009-10-06 16:31:54 +02001389 lbs_cfg_free(priv);
Dan Williams954ee162007-08-20 11:43:25 -04001390
1391 priv->dev = NULL;
1392 free_netdev(dev);
1393
1394 lbs_deb_leave(LBS_DEB_MAIN);
Dan Williams954ee162007-08-20 11:43:25 -04001395}
Holger Schurig10078322007-11-15 18:05:47 -05001396EXPORT_SYMBOL_GPL(lbs_remove_card);
Dan Williams954ee162007-08-20 11:43:25 -04001397
1398
Holger Schurig69f90322007-11-23 15:43:44 +01001399int lbs_start_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -04001400{
1401 struct net_device *dev = priv->dev;
1402 int ret = -1;
1403
1404 lbs_deb_enter(LBS_DEB_MAIN);
1405
1406 /* poke the firmware */
Holger Schurig10078322007-11-15 18:05:47 -05001407 ret = lbs_setup_firmware(priv);
Dan Williams954ee162007-08-20 11:43:25 -04001408 if (ret)
1409 goto done;
1410
1411 /* init 802.11d */
Holger Schurig10078322007-11-15 18:05:47 -05001412 lbs_init_11d(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001413
Holger Schurigff9fc792009-10-06 16:31:54 +02001414 if (lbs_cfg_register(priv)) {
1415 lbs_pr_err("cannot register device\n");
Dan Williams954ee162007-08-20 11:43:25 -04001416 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001417 }
David Woodhouse020f3d02007-12-12 23:29:13 -05001418
David Woodhouse020f3d02007-12-12 23:29:13 -05001419 lbs_update_channel(priv);
David Woodhouse020f3d02007-12-12 23:29:13 -05001420
Bing Zhao684d6b32009-03-25 09:51:16 -07001421 /* Check mesh FW version and appropriately send the mesh start
1422 * command
1423 */
1424 if (priv->mesh_fw_ver == MESH_FW_OLD) {
Holger Schurigc9d1be362008-01-16 15:57:44 +01001425 /* Enable mesh, if supported, and work out which TLV it uses.
1426 0x100 + 291 is an unofficial value used in 5.110.20.pXX
1427 0x100 + 37 is the official value used in 5.110.21.pXX
1428 but we check them in that order because 20.pXX doesn't
1429 give an error -- it just silently fails. */
1430
1431 /* 5.110.20.pXX firmware will fail the command if the channel
1432 doesn't match the existing channel. But only if the TLV
1433 is correct. If the channel is wrong, _BOTH_ versions will
1434 give an error to 0x100+291, and allow 0x100+37 to succeed.
1435 It's just that 5.110.20.pXX will not have done anything
1436 useful */
1437
Bing Zhao684d6b32009-03-25 09:51:16 -07001438 priv->mesh_tlv = TLV_TYPE_OLD_MESH_ID;
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001439 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1440 priv->curbssparams.channel)) {
Bing Zhao684d6b32009-03-25 09:51:16 -07001441 priv->mesh_tlv = TLV_TYPE_MESH_ID;
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001442 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1443 priv->curbssparams.channel))
Holger Schurigc9d1be362008-01-16 15:57:44 +01001444 priv->mesh_tlv = 0;
1445 }
Bing Zhao684d6b32009-03-25 09:51:16 -07001446 } else if (priv->mesh_fw_ver == MESH_FW_NEW) {
1447 /* 10.0.0.pXX new firmwares should succeed with TLV
1448 * 0x100+37; Do not invoke command with old TLV.
1449 */
1450 priv->mesh_tlv = TLV_TYPE_MESH_ID;
1451 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1452 priv->curbssparams.channel))
1453 priv->mesh_tlv = 0;
1454 }
1455 if (priv->mesh_tlv) {
1456 lbs_add_mesh(priv);
Holger Schurigc9d1be362008-01-16 15:57:44 +01001457
Bing Zhao684d6b32009-03-25 09:51:16 -07001458 if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
1459 lbs_pr_err("cannot register lbs_mesh attribute\n");
Dan Williams3b72b012008-07-29 13:50:39 -04001460
Bing Zhao684d6b32009-03-25 09:51:16 -07001461 /* While rtap isn't related to mesh, only mesh-enabled
1462 * firmware implements the rtap functionality via
1463 * CMD_802_11_MONITOR_MODE.
1464 */
1465 if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1466 lbs_pr_err("cannot register lbs_rtap attribute\n");
David Woodhouse020f3d02007-12-12 23:29:13 -05001467 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001468
Holger Schurig10078322007-11-15 18:05:47 -05001469 lbs_debugfs_init_one(priv, dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001470
Dan Williams954ee162007-08-20 11:43:25 -04001471 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001472
Dan Williams954ee162007-08-20 11:43:25 -04001473 ret = 0;
1474
Holger Schurig32a74b72007-05-25 12:04:31 -04001475done:
Dan Williams954ee162007-08-20 11:43:25 -04001476 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
Holger Schurig32a74b72007-05-25 12:04:31 -04001477 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001478}
Holger Schurig10078322007-11-15 18:05:47 -05001479EXPORT_SYMBOL_GPL(lbs_start_card);
Dan Williams954ee162007-08-20 11:43:25 -04001480
1481
Holger Schuriga63e5cb2008-04-30 10:50:39 +02001482void lbs_stop_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -04001483{
Julia Lawall43baa5b2009-01-09 10:23:10 +00001484 struct net_device *dev;
Dan Williams954ee162007-08-20 11:43:25 -04001485 struct cmd_ctrl_node *cmdnode;
1486 unsigned long flags;
1487
1488 lbs_deb_enter(LBS_DEB_MAIN);
1489
Holger Schurig652e3f22008-04-30 10:51:15 +02001490 if (!priv)
1491 goto out;
Julia Lawall43baa5b2009-01-09 10:23:10 +00001492 dev = priv->dev;
Holger Schurig652e3f22008-04-30 10:51:15 +02001493
Julia Lawall43baa5b2009-01-09 10:23:10 +00001494 netif_stop_queue(dev);
1495 netif_carrier_off(dev);
Dan Williams954ee162007-08-20 11:43:25 -04001496
Holger Schurig10078322007-11-15 18:05:47 -05001497 lbs_debugfs_remove_one(priv);
Javier Cardona15dbaac2008-05-17 21:01:24 -07001498 if (priv->mesh_tlv) {
David Woodhouse020f3d02007-12-12 23:29:13 -05001499 device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
Dan Williams3b72b012008-07-29 13:50:39 -04001500 device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
Javier Cardona15dbaac2008-05-17 21:01:24 -07001501 }
Dan Williams954ee162007-08-20 11:43:25 -04001502
Dan Williams71b35f32008-09-08 16:34:40 -04001503 /* Delete the timeout of the currently processing command */
Holger Schurig652e3f22008-04-30 10:51:15 +02001504 del_timer_sync(&priv->command_timer);
Amitkumar Karwar49125452009-09-30 20:04:38 -07001505 del_timer_sync(&priv->auto_deepsleep_timer);
Dan Williams71b35f32008-09-08 16:34:40 -04001506
1507 /* Flush pending command nodes */
David Woodhouseaa21c002007-12-08 20:04:36 +00001508 spin_lock_irqsave(&priv->driver_lock, flags);
Dan Williams71b35f32008-09-08 16:34:40 -04001509 lbs_deb_main("clearing pending commands\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001510 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
David Woodhouseae125bf2007-12-15 04:22:52 -05001511 cmdnode->result = -ENOENT;
Dan Williams954ee162007-08-20 11:43:25 -04001512 cmdnode->cmdwaitqwoken = 1;
1513 wake_up_interruptible(&cmdnode->cmdwait_q);
1514 }
Dan Williams71b35f32008-09-08 16:34:40 -04001515
1516 /* Flush the command the card is currently processing */
1517 if (priv->cur_cmd) {
1518 lbs_deb_main("clearing current command\n");
1519 priv->cur_cmd->result = -ENOENT;
1520 priv->cur_cmd->cmdwaitqwoken = 1;
1521 wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
1522 }
1523 lbs_deb_main("done clearing commands\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001524 spin_unlock_irqrestore(&priv->driver_lock, flags);
Dan Williams954ee162007-08-20 11:43:25 -04001525
1526 unregister_netdev(dev);
1527
Holger Schurig652e3f22008-04-30 10:51:15 +02001528out:
Holger Schuriga63e5cb2008-04-30 10:50:39 +02001529 lbs_deb_leave(LBS_DEB_MAIN);
Dan Williams954ee162007-08-20 11:43:25 -04001530}
Holger Schurig10078322007-11-15 18:05:47 -05001531EXPORT_SYMBOL_GPL(lbs_stop_card);
Holger Schurig084708b2007-05-25 12:37:58 -04001532
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001533
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001534static const struct net_device_ops mesh_netdev_ops = {
1535 .ndo_open = lbs_dev_open,
1536 .ndo_stop = lbs_mesh_stop,
1537 .ndo_start_xmit = lbs_hard_start_xmit,
1538 .ndo_set_mac_address = lbs_set_mac_address,
1539 .ndo_set_multicast_list = lbs_set_multicast_list,
1540};
1541
Holger Schurig78523da2007-05-25 11:49:19 -04001542/**
1543 * @brief This function adds mshX interface
1544 *
Holger Schurig69f90322007-11-23 15:43:44 +01001545 * @param priv A pointer to the struct lbs_private structure
Holger Schurig78523da2007-05-25 11:49:19 -04001546 * @return 0 if successful, -X otherwise
1547 */
David Woodhouse23a397a2007-12-11 18:56:42 -05001548static int lbs_add_mesh(struct lbs_private *priv)
Holger Schurig78523da2007-05-25 11:49:19 -04001549{
1550 struct net_device *mesh_dev = NULL;
1551 int ret = 0;
1552
1553 lbs_deb_enter(LBS_DEB_MESH);
1554
1555 /* Allocate a virtual mesh device */
1556 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1557 lbs_deb_mesh("init mshX device failed\n");
1558 ret = -ENOMEM;
1559 goto done;
1560 }
Wang Chen4ceb7b62008-09-05 11:28:47 +08001561 mesh_dev->ml_priv = priv;
Holger Schurig78523da2007-05-25 11:49:19 -04001562 priv->mesh_dev = mesh_dev;
1563
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001564 mesh_dev->netdev_ops = &mesh_netdev_ops;
Holger Schurig10078322007-11-15 18:05:47 -05001565 mesh_dev->ethtool_ops = &lbs_ethtool_ops;
Holger Schurig634b8f42007-05-25 13:05:16 -04001566 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1567 sizeof(priv->dev->dev_addr));
Holger Schurig78523da2007-05-25 11:49:19 -04001568
David Woodhouse23a397a2007-12-11 18:56:42 -05001569 SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
Dan Williams7732ca42007-05-25 13:13:25 -04001570
Holger Schurig78523da2007-05-25 11:49:19 -04001571#ifdef WIRELESS_EXT
Luis Carlos Cobo Rusf5e05b62007-05-25 23:08:34 -04001572 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
Holger Schurig78523da2007-05-25 11:49:19 -04001573#endif
David Woodhouse75bf45a2008-05-20 13:32:45 +01001574 mesh_dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Holger Schurig78523da2007-05-25 11:49:19 -04001575 /* Register virtual mesh interface */
1576 ret = register_netdev(mesh_dev);
1577 if (ret) {
1578 lbs_pr_err("cannot register mshX virtual interface\n");
1579 goto err_free;
1580 }
1581
Holger Schurig10078322007-11-15 18:05:47 -05001582 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
Holger Schurig78523da2007-05-25 11:49:19 -04001583 if (ret)
1584 goto err_unregister;
1585
Javier Cardona15dbaac2008-05-17 21:01:24 -07001586 lbs_persist_config_init(mesh_dev);
1587
Holger Schurig78523da2007-05-25 11:49:19 -04001588 /* Everything successful */
1589 ret = 0;
1590 goto done;
1591
Holger Schurig78523da2007-05-25 11:49:19 -04001592err_unregister:
1593 unregister_netdev(mesh_dev);
1594
1595err_free:
1596 free_netdev(mesh_dev);
1597
1598done:
1599 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1600 return ret;
1601}
Holger Schurig084708b2007-05-25 12:37:58 -04001602
David Woodhouse23a397a2007-12-11 18:56:42 -05001603static void lbs_remove_mesh(struct lbs_private *priv)
Holger Schurig78523da2007-05-25 11:49:19 -04001604{
1605 struct net_device *mesh_dev;
1606
Holger Schurig78523da2007-05-25 11:49:19 -04001607
1608 mesh_dev = priv->mesh_dev;
David Woodhouse23a397a2007-12-11 18:56:42 -05001609 if (!mesh_dev)
Holger Schurig61d30022008-01-16 15:59:52 +01001610 return;
Holger Schurig78523da2007-05-25 11:49:19 -04001611
Holger Schurig61d30022008-01-16 15:59:52 +01001612 lbs_deb_enter(LBS_DEB_MESH);
Holger Schurig78523da2007-05-25 11:49:19 -04001613 netif_stop_queue(mesh_dev);
Javier Cardona15dbaac2008-05-17 21:01:24 -07001614 netif_carrier_off(mesh_dev);
Holger Schurig10078322007-11-15 18:05:47 -05001615 sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
Javier Cardona15dbaac2008-05-17 21:01:24 -07001616 lbs_persist_config_remove(mesh_dev);
Holger Schurig78523da2007-05-25 11:49:19 -04001617 unregister_netdev(mesh_dev);
David Woodhouse23a397a2007-12-11 18:56:42 -05001618 priv->mesh_dev = NULL;
Holger Schurig78523da2007-05-25 11:49:19 -04001619 free_netdev(mesh_dev);
Holger Schurig61d30022008-01-16 15:59:52 +01001620 lbs_deb_leave(LBS_DEB_MESH);
Holger Schurig78523da2007-05-25 11:49:19 -04001621}
1622
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001623/**
1624 * @brief This function finds the CFP in
1625 * region_cfp_table based on region and band parameter.
1626 *
1627 * @param region The region code
1628 * @param band The band
1629 * @param cfp_no A pointer to CFP number
1630 * @return A pointer to CFP
1631 */
Holger Schurige98a88d2008-03-19 14:25:58 +01001632struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001633{
1634 int i, end;
1635
Holger Schurig9012b282007-05-25 11:27:16 -04001636 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001637
Denis Chengff8ac602007-09-02 18:30:18 +08001638 end = ARRAY_SIZE(region_cfp_table);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001639
1640 for (i = 0; i < end ; i++) {
Holger Schurig9012b282007-05-25 11:27:16 -04001641 lbs_deb_main("region_cfp_table[i].region=%d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001642 region_cfp_table[i].region);
1643 if (region_cfp_table[i].region == region) {
1644 *cfp_no = region_cfp_table[i].cfp_no_BG;
Holger Schurig9012b282007-05-25 11:27:16 -04001645 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001646 return region_cfp_table[i].cfp_BG;
1647 }
1648 }
1649
Holger Schurig9012b282007-05-25 11:27:16 -04001650 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001651 return NULL;
1652}
1653
Holger Schurig69f90322007-11-23 15:43:44 +01001654int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001655{
Holger Schurig9012b282007-05-25 11:27:16 -04001656 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001657 int i = 0;
1658
1659 struct chan_freq_power *cfp;
1660 int cfp_no;
1661
Holger Schurig9012b282007-05-25 11:27:16 -04001662 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001663
David Woodhouseaa21c002007-12-08 20:04:36 +00001664 memset(priv->region_channel, 0, sizeof(priv->region_channel));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001665
Holger Schurige98a88d2008-03-19 14:25:58 +01001666 cfp = lbs_get_region_cfp_table(region, &cfp_no);
Holger Schurig61d30022008-01-16 15:59:52 +01001667 if (cfp != NULL) {
1668 priv->region_channel[i].nrcfp = cfp_no;
1669 priv->region_channel[i].CFP = cfp;
1670 } else {
1671 lbs_deb_main("wrong region code %#x in band B/G\n",
1672 region);
1673 ret = -1;
1674 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001675 }
Holger Schurig61d30022008-01-16 15:59:52 +01001676 priv->region_channel[i].valid = 1;
1677 priv->region_channel[i].region = region;
1678 priv->region_channel[i].band = band;
1679 i++;
Holger Schurig9012b282007-05-25 11:27:16 -04001680out:
1681 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1682 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001683}
1684
Holger Schurig7919b892008-04-01 14:50:43 +02001685void lbs_queue_event(struct lbs_private *priv, u32 event)
1686{
1687 unsigned long flags;
1688
1689 lbs_deb_enter(LBS_DEB_THREAD);
1690 spin_lock_irqsave(&priv->driver_lock, flags);
1691
1692 if (priv->psstate == PS_STATE_SLEEP)
1693 priv->psstate = PS_STATE_AWAKE;
1694
1695 __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1696
1697 wake_up_interruptible(&priv->waitq);
1698
1699 spin_unlock_irqrestore(&priv->driver_lock, flags);
1700 lbs_deb_leave(LBS_DEB_THREAD);
1701}
1702EXPORT_SYMBOL_GPL(lbs_queue_event);
1703
1704void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001705{
Holger Schuriged37b512007-05-25 11:52:42 -04001706 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001707
David Woodhouse4f679492007-12-10 14:58:37 -05001708 if (priv->psstate == PS_STATE_SLEEP)
David Woodhouseaa21c002007-12-08 20:04:36 +00001709 priv->psstate = PS_STATE_AWAKE;
Holger Schurig7919b892008-04-01 14:50:43 +02001710
1711 /* Swap buffers by flipping the response index */
1712 BUG_ON(resp_idx > 1);
1713 priv->resp_idx = resp_idx;
1714
Dan Williamsfe336152007-08-02 11:32:25 -04001715 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001716
Holger Schuriged37b512007-05-25 11:52:42 -04001717 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001718}
Holger Schurig7919b892008-04-01 14:50:43 +02001719EXPORT_SYMBOL_GPL(lbs_notify_command_response);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001720
Andres Salomon4fb910f2007-11-20 17:43:45 -05001721static int __init lbs_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001722{
Holger Schurig9012b282007-05-25 11:27:16 -04001723 lbs_deb_enter(LBS_DEB_MAIN);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001724 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1725 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1726 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1727 confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
Holger Schurig10078322007-11-15 18:05:47 -05001728 lbs_debugfs_init();
Holger Schurig084708b2007-05-25 12:37:58 -04001729 lbs_deb_leave(LBS_DEB_MAIN);
1730 return 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001731}
1732
Andres Salomon4fb910f2007-11-20 17:43:45 -05001733static void __exit lbs_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001734{
Holger Schurig9012b282007-05-25 11:27:16 -04001735 lbs_deb_enter(LBS_DEB_MAIN);
Holger Schurig10078322007-11-15 18:05:47 -05001736 lbs_debugfs_remove();
Holger Schurig9012b282007-05-25 11:27:16 -04001737 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001738}
1739
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001740/*
1741 * rtap interface support fuctions
1742 */
1743
Holger Schurig10078322007-11-15 18:05:47 -05001744static int lbs_rtap_open(struct net_device *dev)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001745{
David Woodhouse8552855f2007-12-10 16:38:18 -05001746 /* Yes, _stop_ the queue. Because we don't support injection */
Holger Schurig61d30022008-01-16 15:59:52 +01001747 lbs_deb_enter(LBS_DEB_MAIN);
1748 netif_carrier_off(dev);
1749 netif_stop_queue(dev);
1750 lbs_deb_leave(LBS_DEB_LEAVE);
1751 return 0;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001752}
1753
Holger Schurig10078322007-11-15 18:05:47 -05001754static int lbs_rtap_stop(struct net_device *dev)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001755{
Holger Schurig61d30022008-01-16 15:59:52 +01001756 lbs_deb_enter(LBS_DEB_MAIN);
1757 lbs_deb_leave(LBS_DEB_MAIN);
1758 return 0;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001759}
1760
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00001761static netdev_tx_t lbs_rtap_hard_start_xmit(struct sk_buff *skb,
1762 struct net_device *dev)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001763{
Holger Schurig61d30022008-01-16 15:59:52 +01001764 netif_stop_queue(dev);
1765 return NETDEV_TX_BUSY;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001766}
1767
David Woodhouse2fd6cfe2007-12-11 17:44:10 -05001768static void lbs_remove_rtap(struct lbs_private *priv)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001769{
Holger Schurig61d30022008-01-16 15:59:52 +01001770 lbs_deb_enter(LBS_DEB_MAIN);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001771 if (priv->rtap_net_dev == NULL)
Holger Schurig5f505d92008-04-30 10:50:07 +02001772 goto out;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001773 unregister_netdev(priv->rtap_net_dev);
David Woodhoused9268fb2007-12-09 16:22:21 -05001774 free_netdev(priv->rtap_net_dev);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001775 priv->rtap_net_dev = NULL;
Holger Schurig5f505d92008-04-30 10:50:07 +02001776out:
Holger Schurig61d30022008-01-16 15:59:52 +01001777 lbs_deb_leave(LBS_DEB_MAIN);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001778}
1779
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001780static const struct net_device_ops rtap_netdev_ops = {
1781 .ndo_open = lbs_rtap_open,
1782 .ndo_stop = lbs_rtap_stop,
1783 .ndo_start_xmit = lbs_rtap_hard_start_xmit,
1784};
1785
David Woodhouse2fd6cfe2007-12-11 17:44:10 -05001786static int lbs_add_rtap(struct lbs_private *priv)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001787{
Holger Schurig61d30022008-01-16 15:59:52 +01001788 int ret = 0;
David Woodhoused9268fb2007-12-09 16:22:21 -05001789 struct net_device *rtap_dev;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001790
Holger Schurig61d30022008-01-16 15:59:52 +01001791 lbs_deb_enter(LBS_DEB_MAIN);
1792 if (priv->rtap_net_dev) {
1793 ret = -EPERM;
1794 goto out;
1795 }
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001796
David Woodhoused9268fb2007-12-09 16:22:21 -05001797 rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
Holger Schurig61d30022008-01-16 15:59:52 +01001798 if (rtap_dev == NULL) {
1799 ret = -ENOMEM;
1800 goto out;
1801 }
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001802
David Woodhouse121947c2007-12-09 19:54:11 -05001803 memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
David Woodhoused9268fb2007-12-09 16:22:21 -05001804 rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
Stephen Hemmingerf02abf12009-03-20 19:36:37 +00001805 rtap_dev->netdev_ops = &rtap_netdev_ops;
Wang Chen4ceb7b62008-09-05 11:28:47 +08001806 rtap_dev->ml_priv = priv;
Masakazu Mokuno229ce3a2008-05-14 14:16:50 +09001807 SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001808
Holger Schurig61d30022008-01-16 15:59:52 +01001809 ret = register_netdev(rtap_dev);
1810 if (ret) {
David Woodhoused9268fb2007-12-09 16:22:21 -05001811 free_netdev(rtap_dev);
Holger Schurig61d30022008-01-16 15:59:52 +01001812 goto out;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001813 }
David Woodhoused9268fb2007-12-09 16:22:21 -05001814 priv->rtap_net_dev = rtap_dev;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001815
Holger Schurig61d30022008-01-16 15:59:52 +01001816out:
1817 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1818 return ret;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001819}
1820
Holger Schurig10078322007-11-15 18:05:47 -05001821module_init(lbs_init_module);
1822module_exit(lbs_exit_module);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001823
Holger Schurig084708b2007-05-25 12:37:58 -04001824MODULE_DESCRIPTION("Libertas WLAN Driver Library");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001825MODULE_AUTHOR("Marvell International Ltd.");
1826MODULE_LICENSE("GPL");