blob: 1550e6afb0537bfa4ef53d7e4041a9288be47e4e [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * Functions implementing wlan infrastructure and adhoc join routines,
3 * IOCTL handlers as well as command preperation and response routines
4 * for sending adhoc start, adhoc join, and association commands
5 * to the firmware.
6 */
7#include <linux/netdevice.h>
8#include <linux/if_arp.h>
9#include <linux/wireless.h>
Dan Williamse76850d2007-05-25 17:09:41 -040010#include <linux/etherdevice.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011
12#include <net/iw_handler.h>
13
14#include "host.h"
15#include "decl.h"
16#include "join.h"
17#include "dev.h"
Dan Williamse76850d2007-05-25 17:09:41 -040018#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020019
Luis Carlos Cobob37e5842007-08-02 13:15:40 -040020/* The firmware needs certain bits masked out of the beacon-derviced capability
21 * field when associating/joining to BSSs.
22 */
23#define CAPINFO_MASK (~(0xda00))
Dan Williams8c512762007-08-02 11:40:45 -040024
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020025/**
Dan Williams8c512762007-08-02 11:40:45 -040026 * @brief This function finds common rates between rate1 and card rates.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027 *
28 * It will fill common rates in rate1 as output if found.
29 *
30 * NOTE: Setting the MSB of the basic rates need to be taken
31 * care, either before or after calling this function
32 *
Holger Schurig69f90322007-11-23 15:43:44 +010033 * @param adapter A pointer to struct lbs_adapter structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020034 * @param rate1 the buffer which keeps input and output
Dan Williams8c512762007-08-02 11:40:45 -040035 * @param rate1_size the size of rate1 buffer; new size of buffer on return
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020036 *
37 * @return 0 or -1
38 */
Holger Schurig69f90322007-11-23 15:43:44 +010039static int get_common_rates(struct lbs_adapter *adapter,
40 u8 *rates,
41 u16 *rates_size)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020042{
Holger Schurig10078322007-11-15 18:05:47 -050043 u8 *card_rates = lbs_bg_rates;
44 size_t num_card_rates = sizeof(lbs_bg_rates);
Dan Williams8c512762007-08-02 11:40:45 -040045 int ret = 0, i, j;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020046 u8 tmp[30];
Dan Williams8c512762007-08-02 11:40:45 -040047 size_t tmp_size = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020048
Dan Williams8c512762007-08-02 11:40:45 -040049 /* For each rate in card_rates that exists in rate1, copy to tmp */
50 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
51 for (j = 0; rates[j] && (j < *rates_size); j++) {
52 if (rates[j] == card_rates[i])
53 tmp[tmp_size++] = card_rates[i];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020054 }
55 }
56
Holger Schurigece56192007-08-02 11:53:06 -040057 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
58 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
59 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
Holger Schurig91843462007-11-28 14:05:02 +010060 lbs_deb_join("TX data rate 0x%02x\n", adapter->cur_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020061
Dan Williams8c512762007-08-02 11:40:45 -040062 if (!adapter->auto_rate) {
63 for (i = 0; i < tmp_size; i++) {
64 if (tmp[i] == adapter->cur_rate)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020065 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020066 }
Dan Williams8c512762007-08-02 11:40:45 -040067 lbs_pr_alert("Previously set fixed data rate %#x isn't "
68 "compatible with the network.\n", adapter->cur_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020069 ret = -1;
70 goto done;
71 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020072 ret = 0;
Dan Williams8c512762007-08-02 11:40:45 -040073
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074done:
Dan Williams8c512762007-08-02 11:40:45 -040075 memset(rates, 0, *rates_size);
76 *rates_size = min_t(int, tmp_size, *rates_size);
77 memcpy(rates, tmp, *rates_size);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020078 return ret;
79}
80
Dan Williams8c512762007-08-02 11:40:45 -040081
82/**
83 * @brief Sets the MSB on basic rates as the firmware requires
84 *
85 * Scan through an array and set the MSB for basic data rates.
86 *
87 * @param rates buffer of data rates
88 * @param len size of buffer
89 */
Holger Schurig10078322007-11-15 18:05:47 -050090static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
Dan Williams8c512762007-08-02 11:40:45 -040091{
92 int i;
93
94 for (i = 0; i < len; i++) {
95 if (rates[i] == 0x02 || rates[i] == 0x04 ||
96 rates[i] == 0x0b || rates[i] == 0x16)
97 rates[i] |= 0x80;
98 }
99}
100
101/**
102 * @brief Unsets the MSB on basic rates
103 *
104 * Scan through an array and unset the MSB for basic data rates.
105 *
106 * @param rates buffer of data rates
107 * @param len size of buffer
108 */
Holger Schurig10078322007-11-15 18:05:47 -0500109void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
Dan Williams8c512762007-08-02 11:40:45 -0400110{
111 int i;
112
113 for (i = 0; i < len; i++)
114 rates[i] &= 0x7f;
115}
116
117
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200118/**
119 * @brief Associate to a specific BSS discovered in a scan
120 *
Holger Schurig69f90322007-11-23 15:43:44 +0100121 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200122 * @param pbssdesc Pointer to the BSS descriptor to associate with.
123 *
124 * @return 0-success, otherwise fail
125 */
Holger Schurig69f90322007-11-23 15:43:44 +0100126int lbs_associate(struct lbs_private *priv, struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200127{
Holger Schurig69f90322007-11-23 15:43:44 +0100128 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200129 int ret;
130
Holger Schurig91843462007-11-28 14:05:02 +0100131 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200132
Holger Schurig10078322007-11-15 18:05:47 -0500133 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
Dan Williams0aef64d2007-08-02 11:31:18 -0400134 0, CMD_OPTION_WAITFORRSP,
Dan Williamse76850d2007-05-25 17:09:41 -0400135 0, assoc_req->bss.bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136
Holger Schurig9012b282007-05-25 11:27:16 -0400137 if (ret)
138 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200139
140 /* set preamble to firmware */
Dan Williams0c9ca692007-08-02 10:43:44 -0400141 if ( (adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
142 && (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williams0aef64d2007-08-02 11:31:18 -0400143 adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200144 else
Dan Williams0aef64d2007-08-02 11:31:18 -0400145 adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200146
Holger Schurig10078322007-11-15 18:05:47 -0500147 lbs_set_radio_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148
Holger Schurig10078322007-11-15 18:05:47 -0500149 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
Dan Williams0aef64d2007-08-02 11:31:18 -0400150 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200151
Holger Schurig9012b282007-05-25 11:27:16 -0400152done:
Holger Schurig91843462007-11-28 14:05:02 +0100153 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200154 return ret;
155}
156
157/**
158 * @brief Start an Adhoc Network
159 *
Holger Schurig69f90322007-11-23 15:43:44 +0100160 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200161 * @param adhocssid The ssid of the Adhoc Network
162 * @return 0--success, -1--fail
163 */
Holger Schurig69f90322007-11-23 15:43:44 +0100164int lbs_start_adhoc_network(struct lbs_private *priv,
165 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200166{
Holger Schurig69f90322007-11-23 15:43:44 +0100167 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168 int ret = 0;
169
170 adapter->adhoccreate = 1;
171
Dan Williams0c9ca692007-08-02 10:43:44 -0400172 if (adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400173 lbs_deb_join("AdhocStart: Short preamble\n");
Dan Williams0aef64d2007-08-02 11:31:18 -0400174 adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
Dan Williams0c9ca692007-08-02 10:43:44 -0400175 } else {
176 lbs_deb_join("AdhocStart: Long preamble\n");
Dan Williams0aef64d2007-08-02 11:31:18 -0400177 adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200178 }
179
Holger Schurig10078322007-11-15 18:05:47 -0500180 lbs_set_radio_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200181
Dan Williamse76850d2007-05-25 17:09:41 -0400182 lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
183 lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200184
Holger Schurig10078322007-11-15 18:05:47 -0500185 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
Dan Williams0aef64d2007-08-02 11:31:18 -0400186 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200187
188 return ret;
189}
190
191/**
192 * @brief Join an adhoc network found in a previous scan
193 *
Holger Schurig69f90322007-11-23 15:43:44 +0100194 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 * @param pbssdesc Pointer to a BSS descriptor found in a previous scan
196 * to attempt to join
197 *
198 * @return 0--success, -1--fail
199 */
Holger Schurig69f90322007-11-23 15:43:44 +0100200int lbs_join_adhoc_network(struct lbs_private *priv,
201 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202{
Holger Schurig69f90322007-11-23 15:43:44 +0100203 struct lbs_adapter *adapter = priv->adapter;
Dan Williamse76850d2007-05-25 17:09:41 -0400204 struct bss_descriptor * bss = &assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200205 int ret = 0;
206
Dan Williamsd8efea22007-05-28 23:54:55 -0400207 lbs_deb_join("%s: Current SSID '%s', ssid length %u\n",
208 __func__,
209 escape_essid(adapter->curbssparams.ssid,
210 adapter->curbssparams.ssid_len),
211 adapter->curbssparams.ssid_len);
212 lbs_deb_join("%s: requested ssid '%s', ssid length %u\n",
213 __func__, escape_essid(bss->ssid, bss->ssid_len),
214 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200215
216 /* check if the requested SSID is already joined */
Dan Williams0edef212007-08-02 13:14:29 -0400217 if ( adapter->curbssparams.ssid_len
Holger Schurig10078322007-11-15 18:05:47 -0500218 && !lbs_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400219 adapter->curbssparams.ssid_len,
220 bss->ssid, bss->ssid_len)
Dan Williams0edef212007-08-02 13:14:29 -0400221 && (adapter->mode == IW_MODE_ADHOC)
Holger Schurig10078322007-11-15 18:05:47 -0500222 && (adapter->connect_status == LBS_CONNECTED)) {
Dan Williams0edef212007-08-02 13:14:29 -0400223 union iwreq_data wrqu;
224
225 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
226 "current, not attempting to re-join");
227
228 /* Send the re-association event though, because the association
229 * request really was successful, even if just a null-op.
230 */
231 memset(&wrqu, 0, sizeof(wrqu));
232 memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid,
233 ETH_ALEN);
234 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
235 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
236 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200237 }
238
Dan Williams0c9ca692007-08-02 10:43:44 -0400239 /* Use shortpreamble only when both creator and card supports
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200240 short preamble */
Dan Williams0c9ca692007-08-02 10:43:44 -0400241 if ( !(bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
242 || !(adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400243 lbs_deb_join("AdhocJoin: Long preamble\n");
Dan Williams0aef64d2007-08-02 11:31:18 -0400244 adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200245 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400246 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williams0aef64d2007-08-02 11:31:18 -0400247 adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200248 }
249
Holger Schurig10078322007-11-15 18:05:47 -0500250 lbs_set_radio_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200251
Dan Williamse76850d2007-05-25 17:09:41 -0400252 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
253 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254
255 adapter->adhoccreate = 0;
256
Holger Schurig10078322007-11-15 18:05:47 -0500257 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_JOIN,
Dan Williams0aef64d2007-08-02 11:31:18 -0400258 0, CMD_OPTION_WAITFORRSP,
Dan Williamse76850d2007-05-25 17:09:41 -0400259 OID_802_11_SSID, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200260
Dan Williams0edef212007-08-02 13:14:29 -0400261out:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200262 return ret;
263}
264
Holger Schurig69f90322007-11-23 15:43:44 +0100265int lbs_stop_adhoc_network(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266{
Holger Schurig10078322007-11-15 18:05:47 -0500267 return lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
Dan Williams0aef64d2007-08-02 11:31:18 -0400268 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200269}
270
271/**
272 * @brief Send Deauthentication Request
273 *
Holger Schurig69f90322007-11-23 15:43:44 +0100274 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200275 * @return 0--success, -1--fail
276 */
Holger Schurig69f90322007-11-23 15:43:44 +0100277int lbs_send_deauthentication(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278{
Holger Schurig10078322007-11-15 18:05:47 -0500279 return lbs_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
Dan Williams0aef64d2007-08-02 11:31:18 -0400280 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281}
282
283/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200284 * @brief This function prepares command of authenticate.
285 *
Holger Schurig69f90322007-11-23 15:43:44 +0100286 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200287 * @param cmd A pointer to cmd_ds_command structure
288 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
289 *
290 * @return 0 or -1
291 */
Holger Schurig69f90322007-11-23 15:43:44 +0100292int lbs_cmd_80211_authenticate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200293 struct cmd_ds_command *cmd,
294 void *pdata_buf)
295{
Holger Schurig69f90322007-11-23 15:43:44 +0100296 struct lbs_adapter *adapter = priv->adapter;
Dan Williams6affe782007-05-10 22:56:42 -0400297 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
298 int ret = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200299 u8 *bssid = pdata_buf;
Joe Perches0795af52007-10-03 17:59:30 -0700300 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200301
Dan Williams45f43de2007-05-25 22:58:55 -0400302 lbs_deb_enter(LBS_DEB_JOIN);
303
Dan Williams0aef64d2007-08-02 11:31:18 -0400304 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
Dan Williams6affe782007-05-10 22:56:42 -0400305 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
306 + S_DS_GEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307
Dan Williams6affe782007-05-10 22:56:42 -0400308 /* translate auth mode to 802.11 defined wire value */
309 switch (adapter->secinfo.auth_mode) {
310 case IW_AUTH_ALG_OPEN_SYSTEM:
311 pauthenticate->authtype = 0x00;
312 break;
313 case IW_AUTH_ALG_SHARED_KEY:
314 pauthenticate->authtype = 0x01;
315 break;
316 case IW_AUTH_ALG_LEAP:
317 pauthenticate->authtype = 0x80;
318 break;
319 default:
Holger Schurig9012b282007-05-25 11:27:16 -0400320 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
Dan Williams6affe782007-05-10 22:56:42 -0400321 adapter->secinfo.auth_mode);
322 goto out;
323 }
324
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200325 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
326
Holger Schurig91843462007-11-28 14:05:02 +0100327 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
Joe Perches0795af52007-10-03 17:59:30 -0700328 print_mac(mac, bssid), pauthenticate->authtype);
Dan Williams6affe782007-05-10 22:56:42 -0400329 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200330
Dan Williams6affe782007-05-10 22:56:42 -0400331out:
Dan Williams45f43de2007-05-25 22:58:55 -0400332 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Dan Williams6affe782007-05-10 22:56:42 -0400333 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200334}
335
Holger Schurig69f90322007-11-23 15:43:44 +0100336int lbs_cmd_80211_deauthenticate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337 struct cmd_ds_command *cmd)
338{
Holger Schurig69f90322007-11-23 15:43:44 +0100339 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200340 struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
341
Holger Schurig9012b282007-05-25 11:27:16 -0400342 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343
Dan Williams0aef64d2007-08-02 11:31:18 -0400344 cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
David Woodhouse981f1872007-05-25 23:36:54 -0400345 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346 S_DS_GEN);
347
348 /* set AP MAC address */
David Woodhouse981f1872007-05-25 23:36:54 -0400349 memmove(dauth->macaddr, adapter->curbssparams.bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350
351 /* Reason code 3 = Station is leaving */
352#define REASON_CODE_STA_LEAVING 3
353 dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
354
Holger Schurig9012b282007-05-25 11:27:16 -0400355 lbs_deb_leave(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356 return 0;
357}
358
Holger Schurig69f90322007-11-23 15:43:44 +0100359int lbs_cmd_80211_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200360 struct cmd_ds_command *cmd, void *pdata_buf)
361{
Holger Schurig69f90322007-11-23 15:43:44 +0100362 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
364 int ret = 0;
Dan Williamse76850d2007-05-25 17:09:41 -0400365 struct assoc_request * assoc_req = pdata_buf;
366 struct bss_descriptor * bss = &assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200367 u8 *pos;
David Woodhouse981f1872007-05-25 23:36:54 -0400368 u16 tmpcap, tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369 struct mrvlietypes_ssidparamset *ssid;
370 struct mrvlietypes_phyparamset *phy;
371 struct mrvlietypes_ssparamset *ss;
372 struct mrvlietypes_ratesparamset *rates;
373 struct mrvlietypes_rsnparamset *rsn;
374
Holger Schurig91843462007-11-28 14:05:02 +0100375 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377 pos = (u8 *) passo;
378
379 if (!adapter) {
380 ret = -1;
381 goto done;
382 }
383
Dan Williams0aef64d2007-08-02 11:31:18 -0400384 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200385
Dan Williamse76850d2007-05-25 17:09:41 -0400386 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200387 pos += sizeof(passo->peerstaaddr);
388
389 /* set the listen interval */
Holger Schurig0aabc0a52007-08-02 13:10:59 -0400390 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200391
Dan Williams0c9ca692007-08-02 10:43:44 -0400392 pos += sizeof(passo->capability);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393 pos += sizeof(passo->listeninterval);
394 pos += sizeof(passo->bcnperiod);
395 pos += sizeof(passo->dtimperiod);
396
397 ssid = (struct mrvlietypes_ssidparamset *) pos;
398 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
Dan Williamsd8efea22007-05-28 23:54:55 -0400399 tmplen = bss->ssid_len;
David Woodhouse707985b2007-05-25 23:41:16 -0400400 ssid->header.len = cpu_to_le16(tmplen);
Dan Williamsd8efea22007-05-28 23:54:55 -0400401 memcpy(ssid->ssid, bss->ssid, tmplen);
David Woodhouse707985b2007-05-25 23:41:16 -0400402 pos += sizeof(ssid->header) + tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200403
404 phy = (struct mrvlietypes_phyparamset *) pos;
405 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
David Woodhouse707985b2007-05-25 23:41:16 -0400406 tmplen = sizeof(phy->fh_ds.dsparamset);
407 phy->header.len = cpu_to_le16(tmplen);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200408 memcpy(&phy->fh_ds.dsparamset,
Dan Williamse76850d2007-05-25 17:09:41 -0400409 &bss->phyparamset.dsparamset.currentchan,
David Woodhouse707985b2007-05-25 23:41:16 -0400410 tmplen);
411 pos += sizeof(phy->header) + tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200412
413 ss = (struct mrvlietypes_ssparamset *) pos;
414 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
David Woodhouse707985b2007-05-25 23:41:16 -0400415 tmplen = sizeof(ss->cf_ibss.cfparamset);
416 ss->header.len = cpu_to_le16(tmplen);
417 pos += sizeof(ss->header) + tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200418
419 rates = (struct mrvlietypes_ratesparamset *) pos;
420 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
Dan Williams8c512762007-08-02 11:40:45 -0400421 memcpy(&rates->rates, &bss->rates, MAX_RATES);
422 tmplen = MAX_RATES;
423 if (get_common_rates(adapter, rates->rates, &tmplen)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200424 ret = -1;
425 goto done;
426 }
David Woodhouse981f1872007-05-25 23:36:54 -0400427 pos += sizeof(rates->header) + tmplen;
428 rates->header.len = cpu_to_le16(tmplen);
Holger Schurig91843462007-11-28 14:05:02 +0100429 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
Dan Williams8c512762007-08-02 11:40:45 -0400430
431 /* Copy the infra. association rates into Current BSS state structure */
432 memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
433 memcpy(&adapter->curbssparams.rates, &rates->rates, tmplen);
434
435 /* Set MSB on basic rates as the firmware requires, but _after_
436 * copying to current bss rates.
437 */
Holger Schurig10078322007-11-15 18:05:47 -0500438 lbs_set_basic_rate_flags(rates->rates, tmplen);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200439
Dan Williamse76850d2007-05-25 17:09:41 -0400440 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200441 rsn = (struct mrvlietypes_rsnparamset *) pos;
David Woodhouse981f1872007-05-25 23:36:54 -0400442 /* WPA_IE or WPA2_IE */
443 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
444 tmplen = (u16) assoc_req->wpa_ie[1];
445 rsn->header.len = cpu_to_le16(tmplen);
446 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
Holger Schurigece56192007-08-02 11:53:06 -0400447 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
David Woodhouse981f1872007-05-25 23:36:54 -0400448 sizeof(rsn->header) + tmplen);
449 pos += sizeof(rsn->header) + tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200450 }
451
452 /* update curbssparams */
David Woodhouse981f1872007-05-25 23:36:54 -0400453 adapter->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454
Holger Schurig10078322007-11-15 18:05:47 -0500455 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 ret = -1;
457 goto done;
458 }
459
460 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
461
Dan Williams0c9ca692007-08-02 10:43:44 -0400462 /* set the capability info */
463 tmpcap = (bss->capability & CAPINFO_MASK);
464 if (bss->mode == IW_MODE_INFRA)
465 tmpcap |= WLAN_CAPABILITY_ESS;
466 passo->capability = cpu_to_le16(tmpcap);
Holger Schurig91843462007-11-28 14:05:02 +0100467 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200468
Holger Schurig9012b282007-05-25 11:27:16 -0400469done:
Holger Schurig91843462007-11-28 14:05:02 +0100470 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200471 return ret;
472}
473
Holger Schurig69f90322007-11-23 15:43:44 +0100474int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
Dan Williamse76850d2007-05-25 17:09:41 -0400475 struct cmd_ds_command *cmd, void *pdata_buf)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476{
Holger Schurig69f90322007-11-23 15:43:44 +0100477 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478 struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
479 int ret = 0;
480 int cmdappendsize = 0;
Dan Williamse76850d2007-05-25 17:09:41 -0400481 struct assoc_request * assoc_req = pdata_buf;
Dan Williams0c9ca692007-08-02 10:43:44 -0400482 u16 tmpcap = 0;
Dan Williams8c512762007-08-02 11:40:45 -0400483 size_t ratesize = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484
Holger Schurig9012b282007-05-25 11:27:16 -0400485 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486
487 if (!adapter) {
488 ret = -1;
489 goto done;
490 }
491
Dan Williams0aef64d2007-08-02 11:31:18 -0400492 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 /*
495 * Fill in the parameters for 2 data structures:
496 * 1. cmd_ds_802_11_ad_hoc_start command
497 * 2. adapter->scantable[i]
498 *
499 * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
500 * probe delay, and cap info.
501 *
502 * Firmware will fill up beacon period, DTIM, Basic rates
503 * and operational rates.
504 */
505
Dan Williamsb44898e2007-08-02 11:18:40 -0400506 memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
507 memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200508
Dan Williamsd8efea22007-05-28 23:54:55 -0400509 lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
510 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
511 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513 /* set the BSS type */
Dan Williams0aef64d2007-08-02 11:31:18 -0400514 adhs->bsstype = CMD_BSS_TYPE_IBSS;
Dan Williamse76850d2007-05-25 17:09:41 -0400515 adapter->mode = IW_MODE_ADHOC;
Brajesh Dave96287ac2007-11-20 17:44:28 -0500516 if (adapter->beacon_period == 0)
517 adapter->beacon_period = MRVDRV_BEACON_INTERVAL;
518 adhs->beaconperiod = cpu_to_le16(adapter->beacon_period);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200519
520 /* set Physical param set */
521#define DS_PARA_IE_ID 3
522#define DS_PARA_IE_LEN 1
523
524 adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
525 adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
526
Dan Williamse76850d2007-05-25 17:09:41 -0400527 WARN_ON(!assoc_req->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528
Holger Schurig9012b282007-05-25 11:27:16 -0400529 lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400530 assoc_req->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531
Dan Williamse76850d2007-05-25 17:09:41 -0400532 adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533
534 /* set IBSS param set */
535#define IBSS_PARA_IE_ID 6
536#define IBSS_PARA_IE_LEN 2
537
538 adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
539 adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
Holger Schurigae596ce2007-08-02 13:10:05 -0400540 adhs->ssparamset.ibssparamset.atimwindow = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541
542 /* set capability info */
Dan Williams0c9ca692007-08-02 10:43:44 -0400543 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williamse76850d2007-05-25 17:09:41 -0400544 if (assoc_req->secinfo.wep_enabled) {
Holger Schurig9012b282007-05-25 11:27:16 -0400545 lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400546 tmpcap |= WLAN_CAPABILITY_PRIVACY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200547 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400548 lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 }
Dan Williams0c9ca692007-08-02 10:43:44 -0400550 adhs->capability = cpu_to_le16(tmpcap);
551
552 /* probedelay */
Dan Williams0aef64d2007-08-02 11:31:18 -0400553 adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554
Dan Williams8c512762007-08-02 11:40:45 -0400555 memset(adhs->rates, 0, sizeof(adhs->rates));
Holger Schurig10078322007-11-15 18:05:47 -0500556 ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
557 memcpy(adhs->rates, lbs_bg_rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200558
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 /* Copy the ad-hoc creating rates into Current BSS state structure */
Dan Williams8c512762007-08-02 11:40:45 -0400560 memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
561 memcpy(&adapter->curbssparams.rates, &adhs->rates, ratesize);
562
563 /* Set MSB on basic rates as the firmware requires, but _after_
564 * copying to current bss rates.
565 */
Holger Schurig10078322007-11-15 18:05:47 -0500566 lbs_set_basic_rate_flags(adhs->rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567
Holger Schurig9012b282007-05-25 11:27:16 -0400568 lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
Dan Williams8c512762007-08-02 11:40:45 -0400569 adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200570
Holger Schurig9012b282007-05-25 11:27:16 -0400571 lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572
Holger Schurig10078322007-11-15 18:05:47 -0500573 if (lbs_create_dnld_countryinfo_11d(priv)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400574 lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575 ret = -1;
576 goto done;
577 }
578
David Woodhouse981f1872007-05-25 23:36:54 -0400579 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
580 S_DS_GEN + cmdappendsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200581
582 ret = 0;
583done:
Holger Schurig9012b282007-05-25 11:27:16 -0400584 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585 return ret;
586}
587
Holger Schurig69f90322007-11-23 15:43:44 +0100588int lbs_cmd_80211_ad_hoc_stop(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200589 struct cmd_ds_command *cmd)
590{
Dan Williams0aef64d2007-08-02 11:31:18 -0400591 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592 cmd->size = cpu_to_le16(S_DS_GEN);
593
594 return 0;
595}
596
Holger Schurig69f90322007-11-23 15:43:44 +0100597int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200598 struct cmd_ds_command *cmd, void *pdata_buf)
599{
Holger Schurig69f90322007-11-23 15:43:44 +0100600 struct lbs_adapter *adapter = priv->adapter;
Dan Williams0c9ca692007-08-02 10:43:44 -0400601 struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
Dan Williamse76850d2007-05-25 17:09:41 -0400602 struct assoc_request * assoc_req = pdata_buf;
603 struct bss_descriptor *bss = &assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200604 int cmdappendsize = 0;
605 int ret = 0;
Dan Williams8c512762007-08-02 11:40:45 -0400606 u16 ratesize = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700607 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608
Holger Schurig9012b282007-05-25 11:27:16 -0400609 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610
Dan Williams0aef64d2007-08-02 11:31:18 -0400611 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200612
Dan Williams0aef64d2007-08-02 11:31:18 -0400613 join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
Dan Williams0c9ca692007-08-02 10:43:44 -0400614 join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200615
Dan Williams0c9ca692007-08-02 10:43:44 -0400616 memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
617 memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618
Dan Williams0c9ca692007-08-02 10:43:44 -0400619 memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
620 sizeof(union ieeetypes_phyparamset));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621
Dan Williams0c9ca692007-08-02 10:43:44 -0400622 memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
623 sizeof(union IEEEtypes_ssparamset));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624
Dan Williams0c9ca692007-08-02 10:43:44 -0400625 join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400626 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
Dan Williams0c9ca692007-08-02 10:43:44 -0400627 bss->capability, CAPINFO_MASK);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200628
629 /* information on BSSID descriptor passed to FW */
Dan Williamse76850d2007-05-25 17:09:41 -0400630 lbs_deb_join(
Joe Perches0795af52007-10-03 17:59:30 -0700631 "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
632 print_mac(mac, join_cmd->bss.bssid),
633 join_cmd->bss.ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200635 /* failtimeout */
Dan Williams0c9ca692007-08-02 10:43:44 -0400636 join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200637
638 /* probedelay */
Dan Williams0aef64d2007-08-02 11:31:18 -0400639 join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640
Dan Williamse76850d2007-05-25 17:09:41 -0400641 adapter->curbssparams.channel = bss->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200642
Dan Williams8c512762007-08-02 11:40:45 -0400643 /* Copy Data rates from the rates recorded in scan response */
644 memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
645 ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
646 memcpy(join_cmd->bss.rates, bss->rates, ratesize);
647 if (get_common_rates(adapter, join_cmd->bss.rates, &ratesize)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400648 lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200649 ret = -1;
650 goto done;
651 }
652
Dan Williams8c512762007-08-02 11:40:45 -0400653 /* Copy the ad-hoc creating rates into Current BSS state structure */
654 memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
655 memcpy(&adapter->curbssparams.rates, join_cmd->bss.rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200656
Dan Williams8c512762007-08-02 11:40:45 -0400657 /* Set MSB on basic rates as the firmware requires, but _after_
658 * copying to current bss rates.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659 */
Holger Schurig10078322007-11-15 18:05:47 -0500660 lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661
Dan Williams0c9ca692007-08-02 10:43:44 -0400662 join_cmd->bss.ssparamset.ibssparamset.atimwindow =
Dan Williamse76850d2007-05-25 17:09:41 -0400663 cpu_to_le16(bss->atimwindow);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200664
Dan Williamse76850d2007-05-25 17:09:41 -0400665 if (assoc_req->secinfo.wep_enabled) {
Dan Williams0c9ca692007-08-02 10:43:44 -0400666 u16 tmp = le16_to_cpu(join_cmd->bss.capability);
667 tmp |= WLAN_CAPABILITY_PRIVACY;
668 join_cmd->bss.capability = cpu_to_le16(tmp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200669 }
670
Holger Schurig10078322007-11-15 18:05:47 -0500671 if (adapter->psmode == LBS802_11POWERMODEMAX_PSP) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200672 /* wake up first */
David Woodhouse981f1872007-05-25 23:36:54 -0400673 __le32 Localpsmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200674
Holger Schurig10078322007-11-15 18:05:47 -0500675 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
676 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400677 CMD_802_11_PS_MODE,
678 CMD_ACT_SET,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200679 0, 0, &Localpsmode);
680
681 if (ret) {
682 ret = -1;
683 goto done;
684 }
685 }
686
Holger Schurig10078322007-11-15 18:05:47 -0500687 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688 ret = -1;
689 goto done;
690 }
691
David Woodhouse981f1872007-05-25 23:36:54 -0400692 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
693 S_DS_GEN + cmdappendsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694
Holger Schurig9012b282007-05-25 11:27:16 -0400695done:
696 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 return ret;
698}
699
Holger Schurig69f90322007-11-23 15:43:44 +0100700int lbs_ret_80211_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701 struct cmd_ds_command *resp)
702{
Holger Schurig69f90322007-11-23 15:43:44 +0100703 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704 int ret = 0;
705 union iwreq_data wrqu;
706 struct ieeetypes_assocrsp *passocrsp;
Dan Williamse76850d2007-05-25 17:09:41 -0400707 struct bss_descriptor * bss;
Dan Williamsc7fdf262007-08-02 13:20:29 -0400708 u16 status_code;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709
Holger Schurig91843462007-11-28 14:05:02 +0100710 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711
Dan Williamse76850d2007-05-25 17:09:41 -0400712 if (!adapter->in_progress_assoc_req) {
Holger Schurig91843462007-11-28 14:05:02 +0100713 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400714 ret = -1;
715 goto done;
716 }
717 bss = &adapter->in_progress_assoc_req->bss;
718
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719 passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
720
Dan Williamsc7fdf262007-08-02 13:20:29 -0400721 /*
722 * Older FW versions map the IEEE 802.11 Status Code in the association
723 * response to the following values returned in passocrsp->statuscode:
724 *
725 * IEEE Status Code Marvell Status Code
726 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
727 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
728 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
729 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
730 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
731 * others -> 0x0003 ASSOC_RESULT_REFUSED
732 *
733 * Other response codes:
734 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
735 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
736 * association response from the AP)
737 */
738
739 status_code = le16_to_cpu(passocrsp->statuscode);
740 switch (status_code) {
741 case 0x00:
Dan Williamsc7fdf262007-08-02 13:20:29 -0400742 break;
743 case 0x01:
Holger Schurig91843462007-11-28 14:05:02 +0100744 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
Dan Williamsc7fdf262007-08-02 13:20:29 -0400745 break;
746 case 0x02:
Holger Schurig91843462007-11-28 14:05:02 +0100747 lbs_deb_assoc("ASSOC_RESP: internal timer "
748 "expired while waiting for the AP\n");
Dan Williamsc7fdf262007-08-02 13:20:29 -0400749 break;
750 case 0x03:
Holger Schurig91843462007-11-28 14:05:02 +0100751 lbs_deb_assoc("ASSOC_RESP: association "
752 "refused by AP\n");
Dan Williamsc7fdf262007-08-02 13:20:29 -0400753 break;
754 case 0x04:
Holger Schurig91843462007-11-28 14:05:02 +0100755 lbs_deb_assoc("ASSOC_RESP: authentication "
756 "refused by AP\n");
Dan Williamsc7fdf262007-08-02 13:20:29 -0400757 break;
758 default:
Holger Schurig91843462007-11-28 14:05:02 +0100759 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
760 " unknown\n", status_code);
Dan Williamsc7fdf262007-08-02 13:20:29 -0400761 break;
762 }
763
764 if (status_code) {
Holger Schurig10078322007-11-15 18:05:47 -0500765 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200766 ret = -1;
767 goto done;
768 }
769
Holger Schurig91843462007-11-28 14:05:02 +0100770 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200771 le16_to_cpu(resp->size) - S_DS_GEN);
772
773 /* Send a Media Connected event, according to the Spec */
Holger Schurig10078322007-11-15 18:05:47 -0500774 adapter->connect_status = LBS_CONNECTED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200775
Dan Williamse76850d2007-05-25 17:09:41 -0400776 /* Update current SSID and BSSID */
Dan Williamsd8efea22007-05-28 23:54:55 -0400777 memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
778 adapter->curbssparams.ssid_len = bss->ssid_len;
Dan Williamse76850d2007-05-25 17:09:41 -0400779 memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200780
Holger Schurig91843462007-11-28 14:05:02 +0100781 lbs_deb_assoc("ASSOC_RESP: currentpacketfilter is 0x%x\n",
782 adapter->currentpacketfilter);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200783
784 adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
785 adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
786
787 memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
788 memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
789 adapter->nextSNRNF = 0;
790 adapter->numSNRNF = 0;
791
Holger Schurig634b8f42007-05-25 13:05:16 -0400792 netif_carrier_on(priv->dev);
793 netif_wake_queue(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200794
Javier Cardona51d84f52007-05-25 12:06:56 -0400795
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200796 memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
797 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Holger Schurig634b8f42007-05-25 13:05:16 -0400798 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200799
Holger Schurig9012b282007-05-25 11:27:16 -0400800done:
Holger Schurig91843462007-11-28 14:05:02 +0100801 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200802 return ret;
803}
804
Holger Schurig69f90322007-11-23 15:43:44 +0100805int lbs_ret_80211_disassociate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200806 struct cmd_ds_command *resp)
807{
Holger Schurig9012b282007-05-25 11:27:16 -0400808 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200809
Holger Schurig10078322007-11-15 18:05:47 -0500810 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811
Holger Schurig9012b282007-05-25 11:27:16 -0400812 lbs_deb_leave(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813 return 0;
814}
815
Holger Schurig69f90322007-11-23 15:43:44 +0100816int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200817 struct cmd_ds_command *resp)
818{
Holger Schurig69f90322007-11-23 15:43:44 +0100819 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200820 int ret = 0;
821 u16 command = le16_to_cpu(resp->command);
822 u16 result = le16_to_cpu(resp->result);
823 struct cmd_ds_802_11_ad_hoc_result *padhocresult;
824 union iwreq_data wrqu;
Dan Williamse76850d2007-05-25 17:09:41 -0400825 struct bss_descriptor *bss;
Joe Perches0795af52007-10-03 17:59:30 -0700826 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200827
Holger Schurig9012b282007-05-25 11:27:16 -0400828 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829
830 padhocresult = &resp->params.result;
831
Dan Williamse76850d2007-05-25 17:09:41 -0400832 lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
833 lbs_deb_join("ADHOC_RESP: command = %x\n", command);
834 lbs_deb_join("ADHOC_RESP: result = %x\n", result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200835
Dan Williamse76850d2007-05-25 17:09:41 -0400836 if (!adapter->in_progress_assoc_req) {
837 lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
838 ret = -1;
839 goto done;
840 }
841 bss = &adapter->in_progress_assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200842
843 /*
844 * Join result code 0 --> SUCCESS
845 */
846 if (result) {
Dan Williamse76850d2007-05-25 17:09:41 -0400847 lbs_deb_join("ADHOC_RESP: failed\n");
Holger Schurig10078322007-11-15 18:05:47 -0500848 if (adapter->connect_status == LBS_CONNECTED) {
849 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200850 }
Holger Schurig9012b282007-05-25 11:27:16 -0400851 ret = -1;
852 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200853 }
854
855 /*
856 * Now the join cmd should be successful
857 * If BSSID has changed use SSID to compare instead of BSSID
858 */
Dan Williamsd8efea22007-05-28 23:54:55 -0400859 lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
860 escape_essid(bss->ssid, bss->ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200861
862 /* Send a Media Connected event, according to the Spec */
Holger Schurig10078322007-11-15 18:05:47 -0500863 adapter->connect_status = LBS_CONNECTED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200864
Holger Schurig6b63cd02007-08-02 11:53:36 -0400865 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200866 /* Update the created network descriptor with the new BSSID */
Dan Williamsea8da922007-08-02 11:18:23 -0400867 memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200868 }
869
870 /* Set the BSSID from the joined/started descriptor */
Dan Williamse76850d2007-05-25 17:09:41 -0400871 memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200872
873 /* Set the new SSID to current SSID */
Dan Williamsd8efea22007-05-28 23:54:55 -0400874 memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
875 adapter->curbssparams.ssid_len = bss->ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200876
Holger Schurig634b8f42007-05-25 13:05:16 -0400877 netif_carrier_on(priv->dev);
878 netif_wake_queue(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200879
880 memset(&wrqu, 0, sizeof(wrqu));
881 memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
882 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Holger Schurig634b8f42007-05-25 13:05:16 -0400883 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200884
Holger Schurig9012b282007-05-25 11:27:16 -0400885 lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
Dan Williamsef9a2642007-05-25 16:46:33 -0400886 lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
Joe Perches0795af52007-10-03 17:59:30 -0700887 lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
888 print_mac(mac, padhocresult->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200889
Holger Schurig9012b282007-05-25 11:27:16 -0400890done:
891 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200892 return ret;
893}
894
Holger Schurig69f90322007-11-23 15:43:44 +0100895int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200896 struct cmd_ds_command *resp)
897{
Holger Schurig9012b282007-05-25 11:27:16 -0400898 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200899
Holger Schurig10078322007-11-15 18:05:47 -0500900 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200901
Holger Schurig9012b282007-05-25 11:27:16 -0400902 lbs_deb_leave(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200903 return 0;
904}