blob: d80b6e05e27d6274eea15b8855571ac300bb15ef [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);
Dan Williams8c512762007-08-02 11:40:45 -040060 lbs_deb_join("Tx datarate is currently 0x%X\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 Schurig9012b282007-05-25 11:27:16 -0400131 lbs_deb_enter(LBS_DEB_JOIN);
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:
153 lbs_deb_leave_args(LBS_DEB_JOIN, "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
Joe Perches0795af52007-10-03 17:59:30 -0700327 lbs_deb_join("AUTH_CMD: BSSID is : %s auth=0x%X\n",
328 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 Schurig9012b282007-05-25 11:27:16 -0400375 lbs_deb_enter(LBS_DEB_JOIN);
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);
Dan Williams8c512762007-08-02 11:40:45 -0400429 lbs_deb_join("ASSOC_CMD: num rates = %u\n", tmplen);
430
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);
467 lbs_deb_join("ASSOC_CMD: capability=%4X CAPINFO_MASK=%4X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400468 tmpcap, CAPINFO_MASK);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469
Holger Schurig9012b282007-05-25 11:27:16 -0400470done:
471 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 return ret;
473}
474
Holger Schurig69f90322007-11-23 15:43:44 +0100475int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
Dan Williamse76850d2007-05-25 17:09:41 -0400476 struct cmd_ds_command *cmd, void *pdata_buf)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477{
Holger Schurig69f90322007-11-23 15:43:44 +0100478 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200479 struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
480 int ret = 0;
481 int cmdappendsize = 0;
Dan Williamse76850d2007-05-25 17:09:41 -0400482 struct assoc_request * assoc_req = pdata_buf;
Dan Williams0c9ca692007-08-02 10:43:44 -0400483 u16 tmpcap = 0;
Dan Williams8c512762007-08-02 11:40:45 -0400484 size_t ratesize = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485
Holger Schurig9012b282007-05-25 11:27:16 -0400486 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200487
488 if (!adapter) {
489 ret = -1;
490 goto done;
491 }
492
Dan Williams0aef64d2007-08-02 11:31:18 -0400493 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 /*
496 * Fill in the parameters for 2 data structures:
497 * 1. cmd_ds_802_11_ad_hoc_start command
498 * 2. adapter->scantable[i]
499 *
500 * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
501 * probe delay, and cap info.
502 *
503 * Firmware will fill up beacon period, DTIM, Basic rates
504 * and operational rates.
505 */
506
Dan Williamsb44898e2007-08-02 11:18:40 -0400507 memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
508 memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509
Dan Williamsd8efea22007-05-28 23:54:55 -0400510 lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
511 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
512 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514 /* set the BSS type */
Dan Williams0aef64d2007-08-02 11:31:18 -0400515 adhs->bsstype = CMD_BSS_TYPE_IBSS;
Dan Williamse76850d2007-05-25 17:09:41 -0400516 adapter->mode = IW_MODE_ADHOC;
Brajesh Dave96287ac2007-11-20 17:44:28 -0500517 if (adapter->beacon_period == 0)
518 adapter->beacon_period = MRVDRV_BEACON_INTERVAL;
519 adhs->beaconperiod = cpu_to_le16(adapter->beacon_period);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520
521 /* set Physical param set */
522#define DS_PARA_IE_ID 3
523#define DS_PARA_IE_LEN 1
524
525 adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
526 adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
527
Dan Williamse76850d2007-05-25 17:09:41 -0400528 WARN_ON(!assoc_req->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529
Holger Schurig9012b282007-05-25 11:27:16 -0400530 lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400531 assoc_req->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532
Dan Williamse76850d2007-05-25 17:09:41 -0400533 adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534
535 /* set IBSS param set */
536#define IBSS_PARA_IE_ID 6
537#define IBSS_PARA_IE_LEN 2
538
539 adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
540 adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
Holger Schurigae596ce2007-08-02 13:10:05 -0400541 adhs->ssparamset.ibssparamset.atimwindow = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200542
543 /* set capability info */
Dan Williams0c9ca692007-08-02 10:43:44 -0400544 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williamse76850d2007-05-25 17:09:41 -0400545 if (assoc_req->secinfo.wep_enabled) {
Holger Schurig9012b282007-05-25 11:27:16 -0400546 lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400547 tmpcap |= WLAN_CAPABILITY_PRIVACY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200548 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400549 lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200550 }
Dan Williams0c9ca692007-08-02 10:43:44 -0400551 adhs->capability = cpu_to_le16(tmpcap);
552
553 /* probedelay */
Dan Williams0aef64d2007-08-02 11:31:18 -0400554 adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200555
Dan Williams8c512762007-08-02 11:40:45 -0400556 memset(adhs->rates, 0, sizeof(adhs->rates));
Holger Schurig10078322007-11-15 18:05:47 -0500557 ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
558 memcpy(adhs->rates, lbs_bg_rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200560 /* Copy the ad-hoc creating rates into Current BSS state structure */
Dan Williams8c512762007-08-02 11:40:45 -0400561 memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
562 memcpy(&adapter->curbssparams.rates, &adhs->rates, ratesize);
563
564 /* Set MSB on basic rates as the firmware requires, but _after_
565 * copying to current bss rates.
566 */
Holger Schurig10078322007-11-15 18:05:47 -0500567 lbs_set_basic_rate_flags(adhs->rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200568
Holger Schurig9012b282007-05-25 11:27:16 -0400569 lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
Dan Williams8c512762007-08-02 11:40:45 -0400570 adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571
Holger Schurig9012b282007-05-25 11:27:16 -0400572 lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573
Holger Schurig10078322007-11-15 18:05:47 -0500574 if (lbs_create_dnld_countryinfo_11d(priv)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400575 lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200576 ret = -1;
577 goto done;
578 }
579
David Woodhouse981f1872007-05-25 23:36:54 -0400580 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
581 S_DS_GEN + cmdappendsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200582
583 ret = 0;
584done:
Holger Schurig9012b282007-05-25 11:27:16 -0400585 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586 return ret;
587}
588
Holger Schurig69f90322007-11-23 15:43:44 +0100589int lbs_cmd_80211_ad_hoc_stop(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200590 struct cmd_ds_command *cmd)
591{
Dan Williams0aef64d2007-08-02 11:31:18 -0400592 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200593 cmd->size = cpu_to_le16(S_DS_GEN);
594
595 return 0;
596}
597
Holger Schurig69f90322007-11-23 15:43:44 +0100598int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200599 struct cmd_ds_command *cmd, void *pdata_buf)
600{
Holger Schurig69f90322007-11-23 15:43:44 +0100601 struct lbs_adapter *adapter = priv->adapter;
Dan Williams0c9ca692007-08-02 10:43:44 -0400602 struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
Dan Williamse76850d2007-05-25 17:09:41 -0400603 struct assoc_request * assoc_req = pdata_buf;
604 struct bss_descriptor *bss = &assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200605 int cmdappendsize = 0;
606 int ret = 0;
Dan Williams8c512762007-08-02 11:40:45 -0400607 u16 ratesize = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700608 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609
Holger Schurig9012b282007-05-25 11:27:16 -0400610 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611
Dan Williams0aef64d2007-08-02 11:31:18 -0400612 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613
Dan Williams0aef64d2007-08-02 11:31:18 -0400614 join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
Dan Williams0c9ca692007-08-02 10:43:44 -0400615 join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616
Dan Williams0c9ca692007-08-02 10:43:44 -0400617 memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
618 memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619
Dan Williams0c9ca692007-08-02 10:43:44 -0400620 memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
621 sizeof(union ieeetypes_phyparamset));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622
Dan Williams0c9ca692007-08-02 10:43:44 -0400623 memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
624 sizeof(union IEEEtypes_ssparamset));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200625
Dan Williams0c9ca692007-08-02 10:43:44 -0400626 join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400627 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
Dan Williams0c9ca692007-08-02 10:43:44 -0400628 bss->capability, CAPINFO_MASK);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629
630 /* information on BSSID descriptor passed to FW */
Dan Williamse76850d2007-05-25 17:09:41 -0400631 lbs_deb_join(
Joe Perches0795af52007-10-03 17:59:30 -0700632 "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
633 print_mac(mac, join_cmd->bss.bssid),
634 join_cmd->bss.ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200635
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200636 /* failtimeout */
Dan Williams0c9ca692007-08-02 10:43:44 -0400637 join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638
639 /* probedelay */
Dan Williams0aef64d2007-08-02 11:31:18 -0400640 join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200641
Dan Williamse76850d2007-05-25 17:09:41 -0400642 adapter->curbssparams.channel = bss->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643
Dan Williams8c512762007-08-02 11:40:45 -0400644 /* Copy Data rates from the rates recorded in scan response */
645 memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
646 ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
647 memcpy(join_cmd->bss.rates, bss->rates, ratesize);
648 if (get_common_rates(adapter, join_cmd->bss.rates, &ratesize)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400649 lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200650 ret = -1;
651 goto done;
652 }
653
Dan Williams8c512762007-08-02 11:40:45 -0400654 /* Copy the ad-hoc creating rates into Current BSS state structure */
655 memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
656 memcpy(&adapter->curbssparams.rates, join_cmd->bss.rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657
Dan Williams8c512762007-08-02 11:40:45 -0400658 /* Set MSB on basic rates as the firmware requires, but _after_
659 * copying to current bss rates.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200660 */
Holger Schurig10078322007-11-15 18:05:47 -0500661 lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662
Dan Williams0c9ca692007-08-02 10:43:44 -0400663 join_cmd->bss.ssparamset.ibssparamset.atimwindow =
Dan Williamse76850d2007-05-25 17:09:41 -0400664 cpu_to_le16(bss->atimwindow);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200665
Dan Williamse76850d2007-05-25 17:09:41 -0400666 if (assoc_req->secinfo.wep_enabled) {
Dan Williams0c9ca692007-08-02 10:43:44 -0400667 u16 tmp = le16_to_cpu(join_cmd->bss.capability);
668 tmp |= WLAN_CAPABILITY_PRIVACY;
669 join_cmd->bss.capability = cpu_to_le16(tmp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200670 }
671
Holger Schurig10078322007-11-15 18:05:47 -0500672 if (adapter->psmode == LBS802_11POWERMODEMAX_PSP) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200673 /* wake up first */
David Woodhouse981f1872007-05-25 23:36:54 -0400674 __le32 Localpsmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200675
Holger Schurig10078322007-11-15 18:05:47 -0500676 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
677 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400678 CMD_802_11_PS_MODE,
679 CMD_ACT_SET,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680 0, 0, &Localpsmode);
681
682 if (ret) {
683 ret = -1;
684 goto done;
685 }
686 }
687
Holger Schurig10078322007-11-15 18:05:47 -0500688 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689 ret = -1;
690 goto done;
691 }
692
David Woodhouse981f1872007-05-25 23:36:54 -0400693 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
694 S_DS_GEN + cmdappendsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695
Holger Schurig9012b282007-05-25 11:27:16 -0400696done:
697 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200698 return ret;
699}
700
Holger Schurig69f90322007-11-23 15:43:44 +0100701int lbs_ret_80211_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702 struct cmd_ds_command *resp)
703{
Holger Schurig69f90322007-11-23 15:43:44 +0100704 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705 int ret = 0;
706 union iwreq_data wrqu;
707 struct ieeetypes_assocrsp *passocrsp;
Dan Williamse76850d2007-05-25 17:09:41 -0400708 struct bss_descriptor * bss;
Dan Williamsc7fdf262007-08-02 13:20:29 -0400709 u16 status_code;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710
Holger Schurig9012b282007-05-25 11:27:16 -0400711 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200712
Dan Williamse76850d2007-05-25 17:09:41 -0400713 if (!adapter->in_progress_assoc_req) {
714 lbs_deb_join("ASSOC_RESP: no in-progress association request\n");
715 ret = -1;
716 goto done;
717 }
718 bss = &adapter->in_progress_assoc_req->bss;
719
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720 passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
721
Dan Williamsc7fdf262007-08-02 13:20:29 -0400722 /*
723 * Older FW versions map the IEEE 802.11 Status Code in the association
724 * response to the following values returned in passocrsp->statuscode:
725 *
726 * IEEE Status Code Marvell Status Code
727 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
728 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
729 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
730 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
731 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
732 * others -> 0x0003 ASSOC_RESULT_REFUSED
733 *
734 * Other response codes:
735 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
736 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
737 * association response from the AP)
738 */
739
740 status_code = le16_to_cpu(passocrsp->statuscode);
741 switch (status_code) {
742 case 0x00:
743 lbs_deb_join("ASSOC_RESP: Association succeeded\n");
744 break;
745 case 0x01:
746 lbs_deb_join("ASSOC_RESP: Association failed; invalid "
747 "parameters (status code %d)\n", status_code);
748 break;
749 case 0x02:
750 lbs_deb_join("ASSOC_RESP: Association failed; internal timer "
751 "expired while waiting for the AP (status code %d)"
752 "\n", status_code);
753 break;
754 case 0x03:
755 lbs_deb_join("ASSOC_RESP: Association failed; association "
756 "was refused by the AP (status code %d)\n",
757 status_code);
758 break;
759 case 0x04:
760 lbs_deb_join("ASSOC_RESP: Association failed; authentication "
761 "was refused by the AP (status code %d)\n",
762 status_code);
763 break;
764 default:
765 lbs_deb_join("ASSOC_RESP: Association failed; reason unknown "
766 "(status code %d)\n", status_code);
767 break;
768 }
769
770 if (status_code) {
Holger Schurig10078322007-11-15 18:05:47 -0500771 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200772 ret = -1;
773 goto done;
774 }
775
Holger Schurigece56192007-08-02 11:53:06 -0400776 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_RESP", (void *)&resp->params,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200777 le16_to_cpu(resp->size) - S_DS_GEN);
778
779 /* Send a Media Connected event, according to the Spec */
Holger Schurig10078322007-11-15 18:05:47 -0500780 adapter->connect_status = LBS_CONNECTED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200781
Dan Williamsd8efea22007-05-28 23:54:55 -0400782 lbs_deb_join("ASSOC_RESP: assocated to '%s'\n",
783 escape_essid(bss->ssid, bss->ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200784
Dan Williamse76850d2007-05-25 17:09:41 -0400785 /* Update current SSID and BSSID */
Dan Williamsd8efea22007-05-28 23:54:55 -0400786 memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
787 adapter->curbssparams.ssid_len = bss->ssid_len;
Dan Williamse76850d2007-05-25 17:09:41 -0400788 memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200789
Holger Schurig9012b282007-05-25 11:27:16 -0400790 lbs_deb_join("ASSOC_RESP: currentpacketfilter is %x\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200791 adapter->currentpacketfilter);
792
793 adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
794 adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
795
796 memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
797 memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
798 adapter->nextSNRNF = 0;
799 adapter->numSNRNF = 0;
800
Holger Schurig634b8f42007-05-25 13:05:16 -0400801 netif_carrier_on(priv->dev);
802 netif_wake_queue(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803
Javier Cardona51d84f52007-05-25 12:06:56 -0400804
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200805 memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
806 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Holger Schurig634b8f42007-05-25 13:05:16 -0400807 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200808
Holger Schurig9012b282007-05-25 11:27:16 -0400809done:
810 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811 return ret;
812}
813
Holger Schurig69f90322007-11-23 15:43:44 +0100814int lbs_ret_80211_disassociate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200815 struct cmd_ds_command *resp)
816{
Holger Schurig9012b282007-05-25 11:27:16 -0400817 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200818
Holger Schurig10078322007-11-15 18:05:47 -0500819 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200820
Holger Schurig9012b282007-05-25 11:27:16 -0400821 lbs_deb_leave(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200822 return 0;
823}
824
Holger Schurig69f90322007-11-23 15:43:44 +0100825int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200826 struct cmd_ds_command *resp)
827{
Holger Schurig69f90322007-11-23 15:43:44 +0100828 struct lbs_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829 int ret = 0;
830 u16 command = le16_to_cpu(resp->command);
831 u16 result = le16_to_cpu(resp->result);
832 struct cmd_ds_802_11_ad_hoc_result *padhocresult;
833 union iwreq_data wrqu;
Dan Williamse76850d2007-05-25 17:09:41 -0400834 struct bss_descriptor *bss;
Joe Perches0795af52007-10-03 17:59:30 -0700835 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200836
Holger Schurig9012b282007-05-25 11:27:16 -0400837 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200838
839 padhocresult = &resp->params.result;
840
Dan Williamse76850d2007-05-25 17:09:41 -0400841 lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
842 lbs_deb_join("ADHOC_RESP: command = %x\n", command);
843 lbs_deb_join("ADHOC_RESP: result = %x\n", result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200844
Dan Williamse76850d2007-05-25 17:09:41 -0400845 if (!adapter->in_progress_assoc_req) {
846 lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
847 ret = -1;
848 goto done;
849 }
850 bss = &adapter->in_progress_assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200851
852 /*
853 * Join result code 0 --> SUCCESS
854 */
855 if (result) {
Dan Williamse76850d2007-05-25 17:09:41 -0400856 lbs_deb_join("ADHOC_RESP: failed\n");
Holger Schurig10078322007-11-15 18:05:47 -0500857 if (adapter->connect_status == LBS_CONNECTED) {
858 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200859 }
Holger Schurig9012b282007-05-25 11:27:16 -0400860 ret = -1;
861 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200862 }
863
864 /*
865 * Now the join cmd should be successful
866 * If BSSID has changed use SSID to compare instead of BSSID
867 */
Dan Williamsd8efea22007-05-28 23:54:55 -0400868 lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
869 escape_essid(bss->ssid, bss->ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200870
871 /* Send a Media Connected event, according to the Spec */
Holger Schurig10078322007-11-15 18:05:47 -0500872 adapter->connect_status = LBS_CONNECTED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200873
Holger Schurig6b63cd02007-08-02 11:53:36 -0400874 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200875 /* Update the created network descriptor with the new BSSID */
Dan Williamsea8da922007-08-02 11:18:23 -0400876 memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877 }
878
879 /* Set the BSSID from the joined/started descriptor */
Dan Williamse76850d2007-05-25 17:09:41 -0400880 memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200881
882 /* Set the new SSID to current SSID */
Dan Williamsd8efea22007-05-28 23:54:55 -0400883 memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
884 adapter->curbssparams.ssid_len = bss->ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200885
Holger Schurig634b8f42007-05-25 13:05:16 -0400886 netif_carrier_on(priv->dev);
887 netif_wake_queue(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200888
889 memset(&wrqu, 0, sizeof(wrqu));
890 memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
891 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Holger Schurig634b8f42007-05-25 13:05:16 -0400892 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200893
Holger Schurig9012b282007-05-25 11:27:16 -0400894 lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
Dan Williamsef9a2642007-05-25 16:46:33 -0400895 lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
Joe Perches0795af52007-10-03 17:59:30 -0700896 lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
897 print_mac(mac, padhocresult->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200898
Holger Schurig9012b282007-05-25 11:27:16 -0400899done:
900 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200901 return ret;
902}
903
Holger Schurig69f90322007-11-23 15:43:44 +0100904int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200905 struct cmd_ds_command *resp)
906{
Holger Schurig9012b282007-05-25 11:27:16 -0400907 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200908
Holger Schurig10078322007-11-15 18:05:47 -0500909 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200910
Holger Schurig9012b282007-05-25 11:27:16 -0400911 lbs_deb_leave(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200912 return 0;
913}