blob: 3e5026ff2ffa4df06209005d1cd8462ac5fe9f9a [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 *
David Woodhouseaa21c002007-12-08 20:04:36 +000033 * @param priv A pointer to struct lbs_private 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 */
David Woodhouseaa21c002007-12-08 20:04:36 +000039static int get_common_rates(struct lbs_private *priv,
Holger Schurig69f90322007-11-23 15:43:44 +010040 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);
David Woodhouseaa21c002007-12-08 20:04:36 +000060 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020061
David Woodhouseaa21c002007-12-08 20:04:36 +000062 if (!priv->auto_rate) {
Dan Williams8c512762007-08-02 11:40:45 -040063 for (i = 0; i < tmp_size; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +000064 if (tmp[i] == priv->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 "
David Woodhouseaa21c002007-12-08 20:04:36 +000068 "compatible with the network.\n", priv->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/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200102 * @brief Associate to a specific BSS discovered in a scan
103 *
Holger Schurig69f90322007-11-23 15:43:44 +0100104 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200105 * @param pbssdesc Pointer to the BSS descriptor to associate with.
106 *
107 * @return 0-success, otherwise fail
108 */
Holger Schurig69f90322007-11-23 15:43:44 +0100109int lbs_associate(struct lbs_private *priv, struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200110{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200111 int ret;
112
Holger Schurig91843462007-11-28 14:05:02 +0100113 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200114
Holger Schurig10078322007-11-15 18:05:47 -0500115 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
Dan Williams0aef64d2007-08-02 11:31:18 -0400116 0, CMD_OPTION_WAITFORRSP,
Dan Williamse76850d2007-05-25 17:09:41 -0400117 0, assoc_req->bss.bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200118
Holger Schurig9012b282007-05-25 11:27:16 -0400119 if (ret)
120 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200121
122 /* set preamble to firmware */
David Woodhouseaa21c002007-12-08 20:04:36 +0000123 if ( (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Dan Williams0c9ca692007-08-02 10:43:44 -0400124 && (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
David Woodhouseaa21c002007-12-08 20:04:36 +0000125 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200126 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000127 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200128
Holger Schurig10078322007-11-15 18:05:47 -0500129 lbs_set_radio_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200130
Holger Schurig10078322007-11-15 18:05:47 -0500131 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
Dan Williams0aef64d2007-08-02 11:31:18 -0400132 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200133
Holger Schurig9012b282007-05-25 11:27:16 -0400134done:
Holger Schurig91843462007-11-28 14:05:02 +0100135 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136 return ret;
137}
138
139/**
140 * @brief Start an Adhoc Network
141 *
Holger Schurig69f90322007-11-23 15:43:44 +0100142 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200143 * @param adhocssid The ssid of the Adhoc Network
144 * @return 0--success, -1--fail
145 */
Holger Schurig69f90322007-11-23 15:43:44 +0100146int lbs_start_adhoc_network(struct lbs_private *priv,
147 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200149 int ret = 0;
150
David Woodhouseaa21c002007-12-08 20:04:36 +0000151 priv->adhoccreate = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200152
David Woodhouseaa21c002007-12-08 20:04:36 +0000153 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400154 lbs_deb_join("AdhocStart: Short preamble\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000155 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
Dan Williams0c9ca692007-08-02 10:43:44 -0400156 } else {
157 lbs_deb_join("AdhocStart: Long preamble\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000158 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159 }
160
Holger Schurig10078322007-11-15 18:05:47 -0500161 lbs_set_radio_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162
Dan Williamse76850d2007-05-25 17:09:41 -0400163 lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
164 lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200165
Holger Schurig10078322007-11-15 18:05:47 -0500166 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
Dan Williams0aef64d2007-08-02 11:31:18 -0400167 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168
169 return ret;
170}
171
172/**
173 * @brief Join an adhoc network found in a previous scan
174 *
Holger Schurig69f90322007-11-23 15:43:44 +0100175 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200176 * @param pbssdesc Pointer to a BSS descriptor found in a previous scan
177 * to attempt to join
178 *
179 * @return 0--success, -1--fail
180 */
Holger Schurig69f90322007-11-23 15:43:44 +0100181int lbs_join_adhoc_network(struct lbs_private *priv,
182 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183{
Dan Williamse76850d2007-05-25 17:09:41 -0400184 struct bss_descriptor * bss = &assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185 int ret = 0;
186
Dan Williamsd8efea22007-05-28 23:54:55 -0400187 lbs_deb_join("%s: Current SSID '%s', ssid length %u\n",
188 __func__,
David Woodhouseaa21c002007-12-08 20:04:36 +0000189 escape_essid(priv->curbssparams.ssid,
190 priv->curbssparams.ssid_len),
191 priv->curbssparams.ssid_len);
Dan Williamsd8efea22007-05-28 23:54:55 -0400192 lbs_deb_join("%s: requested ssid '%s', ssid length %u\n",
193 __func__, escape_essid(bss->ssid, bss->ssid_len),
194 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195
196 /* check if the requested SSID is already joined */
David Woodhouseaa21c002007-12-08 20:04:36 +0000197 if ( priv->curbssparams.ssid_len
198 && !lbs_ssid_cmp(priv->curbssparams.ssid,
199 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -0400200 bss->ssid, bss->ssid_len)
David Woodhouseaa21c002007-12-08 20:04:36 +0000201 && (priv->mode == IW_MODE_ADHOC)
202 && (priv->connect_status == LBS_CONNECTED)) {
Dan Williams0edef212007-08-02 13:14:29 -0400203 union iwreq_data wrqu;
204
205 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
206 "current, not attempting to re-join");
207
208 /* Send the re-association event though, because the association
209 * request really was successful, even if just a null-op.
210 */
211 memset(&wrqu, 0, sizeof(wrqu));
David Woodhouseaa21c002007-12-08 20:04:36 +0000212 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
Dan Williams0edef212007-08-02 13:14:29 -0400213 ETH_ALEN);
214 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
215 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
216 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200217 }
218
Dan Williams0c9ca692007-08-02 10:43:44 -0400219 /* Use shortpreamble only when both creator and card supports
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200220 short preamble */
Dan Williams0c9ca692007-08-02 10:43:44 -0400221 if ( !(bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
David Woodhouseaa21c002007-12-08 20:04:36 +0000222 || !(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400223 lbs_deb_join("AdhocJoin: Long preamble\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000224 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200225 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400226 lbs_deb_join("AdhocJoin: Short preamble\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000227 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200228 }
229
Holger Schurig10078322007-11-15 18:05:47 -0500230 lbs_set_radio_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200231
Dan Williamse76850d2007-05-25 17:09:41 -0400232 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
233 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200234
David Woodhouseaa21c002007-12-08 20:04:36 +0000235 priv->adhoccreate = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236
Holger Schurig10078322007-11-15 18:05:47 -0500237 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_JOIN,
Dan Williams0aef64d2007-08-02 11:31:18 -0400238 0, CMD_OPTION_WAITFORRSP,
Dan Williamse76850d2007-05-25 17:09:41 -0400239 OID_802_11_SSID, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200240
Dan Williams0edef212007-08-02 13:14:29 -0400241out:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200242 return ret;
243}
244
Holger Schurig69f90322007-11-23 15:43:44 +0100245int lbs_stop_adhoc_network(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246{
Holger Schurig10078322007-11-15 18:05:47 -0500247 return lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
Dan Williams0aef64d2007-08-02 11:31:18 -0400248 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249}
250
251/**
252 * @brief Send Deauthentication Request
253 *
Holger Schurig69f90322007-11-23 15:43:44 +0100254 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255 * @return 0--success, -1--fail
256 */
Holger Schurig69f90322007-11-23 15:43:44 +0100257int lbs_send_deauthentication(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200258{
Holger Schurig10078322007-11-15 18:05:47 -0500259 return lbs_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
Dan Williams0aef64d2007-08-02 11:31:18 -0400260 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200261}
262
263/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200264 * @brief This function prepares command of authenticate.
265 *
Holger Schurig69f90322007-11-23 15:43:44 +0100266 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200267 * @param cmd A pointer to cmd_ds_command structure
268 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
269 *
270 * @return 0 or -1
271 */
Holger Schurig69f90322007-11-23 15:43:44 +0100272int lbs_cmd_80211_authenticate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200273 struct cmd_ds_command *cmd,
274 void *pdata_buf)
275{
Dan Williams6affe782007-05-10 22:56:42 -0400276 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
277 int ret = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278 u8 *bssid = pdata_buf;
Joe Perches0795af52007-10-03 17:59:30 -0700279 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200280
Dan Williams45f43de2007-05-25 22:58:55 -0400281 lbs_deb_enter(LBS_DEB_JOIN);
282
Dan Williams0aef64d2007-08-02 11:31:18 -0400283 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
Dan Williams6affe782007-05-10 22:56:42 -0400284 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
285 + S_DS_GEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200286
Dan Williams6affe782007-05-10 22:56:42 -0400287 /* translate auth mode to 802.11 defined wire value */
David Woodhouseaa21c002007-12-08 20:04:36 +0000288 switch (priv->secinfo.auth_mode) {
Dan Williams6affe782007-05-10 22:56:42 -0400289 case IW_AUTH_ALG_OPEN_SYSTEM:
290 pauthenticate->authtype = 0x00;
291 break;
292 case IW_AUTH_ALG_SHARED_KEY:
293 pauthenticate->authtype = 0x01;
294 break;
295 case IW_AUTH_ALG_LEAP:
296 pauthenticate->authtype = 0x80;
297 break;
298 default:
Holger Schurig9012b282007-05-25 11:27:16 -0400299 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
David Woodhouseaa21c002007-12-08 20:04:36 +0000300 priv->secinfo.auth_mode);
Dan Williams6affe782007-05-10 22:56:42 -0400301 goto out;
302 }
303
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
305
Holger Schurig91843462007-11-28 14:05:02 +0100306 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
Joe Perches0795af52007-10-03 17:59:30 -0700307 print_mac(mac, bssid), pauthenticate->authtype);
Dan Williams6affe782007-05-10 22:56:42 -0400308 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200309
Dan Williams6affe782007-05-10 22:56:42 -0400310out:
Dan Williams45f43de2007-05-25 22:58:55 -0400311 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Dan Williams6affe782007-05-10 22:56:42 -0400312 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200313}
314
Holger Schurig69f90322007-11-23 15:43:44 +0100315int lbs_cmd_80211_deauthenticate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200316 struct cmd_ds_command *cmd)
317{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318 struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
319
Holger Schurig9012b282007-05-25 11:27:16 -0400320 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200321
Dan Williams0aef64d2007-08-02 11:31:18 -0400322 cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
David Woodhouse981f1872007-05-25 23:36:54 -0400323 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200324 S_DS_GEN);
325
326 /* set AP MAC address */
David Woodhouseaa21c002007-12-08 20:04:36 +0000327 memmove(dauth->macaddr, priv->curbssparams.bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328
329 /* Reason code 3 = Station is leaving */
330#define REASON_CODE_STA_LEAVING 3
331 dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
332
Holger Schurig9012b282007-05-25 11:27:16 -0400333 lbs_deb_leave(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200334 return 0;
335}
336
Holger Schurig69f90322007-11-23 15:43:44 +0100337int lbs_cmd_80211_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338 struct cmd_ds_command *cmd, void *pdata_buf)
339{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200340 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
341 int ret = 0;
Dan Williamse76850d2007-05-25 17:09:41 -0400342 struct assoc_request * assoc_req = pdata_buf;
343 struct bss_descriptor * bss = &assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200344 u8 *pos;
David Woodhouse981f1872007-05-25 23:36:54 -0400345 u16 tmpcap, tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346 struct mrvlietypes_ssidparamset *ssid;
347 struct mrvlietypes_phyparamset *phy;
348 struct mrvlietypes_ssparamset *ss;
349 struct mrvlietypes_ratesparamset *rates;
350 struct mrvlietypes_rsnparamset *rsn;
351
Holger Schurig91843462007-11-28 14:05:02 +0100352 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200353
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354 pos = (u8 *) passo;
355
David Woodhouseaa21c002007-12-08 20:04:36 +0000356 if (!priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357 ret = -1;
358 goto done;
359 }
360
Dan Williams0aef64d2007-08-02 11:31:18 -0400361 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362
Dan Williamse76850d2007-05-25 17:09:41 -0400363 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364 pos += sizeof(passo->peerstaaddr);
365
366 /* set the listen interval */
Holger Schurig0aabc0a52007-08-02 13:10:59 -0400367 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368
Dan Williams0c9ca692007-08-02 10:43:44 -0400369 pos += sizeof(passo->capability);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 pos += sizeof(passo->listeninterval);
371 pos += sizeof(passo->bcnperiod);
372 pos += sizeof(passo->dtimperiod);
373
374 ssid = (struct mrvlietypes_ssidparamset *) pos;
375 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
Dan Williamsd8efea22007-05-28 23:54:55 -0400376 tmplen = bss->ssid_len;
David Woodhouse707985b2007-05-25 23:41:16 -0400377 ssid->header.len = cpu_to_le16(tmplen);
Dan Williamsd8efea22007-05-28 23:54:55 -0400378 memcpy(ssid->ssid, bss->ssid, tmplen);
David Woodhouse707985b2007-05-25 23:41:16 -0400379 pos += sizeof(ssid->header) + tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380
381 phy = (struct mrvlietypes_phyparamset *) pos;
382 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
David Woodhouse707985b2007-05-25 23:41:16 -0400383 tmplen = sizeof(phy->fh_ds.dsparamset);
384 phy->header.len = cpu_to_le16(tmplen);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200385 memcpy(&phy->fh_ds.dsparamset,
Dan Williamse76850d2007-05-25 17:09:41 -0400386 &bss->phyparamset.dsparamset.currentchan,
David Woodhouse707985b2007-05-25 23:41:16 -0400387 tmplen);
388 pos += sizeof(phy->header) + tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200389
390 ss = (struct mrvlietypes_ssparamset *) pos;
391 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
David Woodhouse707985b2007-05-25 23:41:16 -0400392 tmplen = sizeof(ss->cf_ibss.cfparamset);
393 ss->header.len = cpu_to_le16(tmplen);
394 pos += sizeof(ss->header) + tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200395
396 rates = (struct mrvlietypes_ratesparamset *) pos;
397 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
Dan Williams8c512762007-08-02 11:40:45 -0400398 memcpy(&rates->rates, &bss->rates, MAX_RATES);
399 tmplen = MAX_RATES;
David Woodhouseaa21c002007-12-08 20:04:36 +0000400 if (get_common_rates(priv, rates->rates, &tmplen)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200401 ret = -1;
402 goto done;
403 }
David Woodhouse981f1872007-05-25 23:36:54 -0400404 pos += sizeof(rates->header) + tmplen;
405 rates->header.len = cpu_to_le16(tmplen);
Holger Schurig91843462007-11-28 14:05:02 +0100406 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
Dan Williams8c512762007-08-02 11:40:45 -0400407
408 /* Copy the infra. association rates into Current BSS state structure */
David Woodhouseaa21c002007-12-08 20:04:36 +0000409 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
410 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
Dan Williams8c512762007-08-02 11:40:45 -0400411
412 /* Set MSB on basic rates as the firmware requires, but _after_
413 * copying to current bss rates.
414 */
Holger Schurig10078322007-11-15 18:05:47 -0500415 lbs_set_basic_rate_flags(rates->rates, tmplen);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416
Dan Williamse76850d2007-05-25 17:09:41 -0400417 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200418 rsn = (struct mrvlietypes_rsnparamset *) pos;
David Woodhouse981f1872007-05-25 23:36:54 -0400419 /* WPA_IE or WPA2_IE */
420 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
421 tmplen = (u16) assoc_req->wpa_ie[1];
422 rsn->header.len = cpu_to_le16(tmplen);
423 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
Holger Schurigece56192007-08-02 11:53:06 -0400424 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
David Woodhouse981f1872007-05-25 23:36:54 -0400425 sizeof(rsn->header) + tmplen);
426 pos += sizeof(rsn->header) + tmplen;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427 }
428
429 /* update curbssparams */
David Woodhouseaa21c002007-12-08 20:04:36 +0000430 priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200431
Holger Schurig10078322007-11-15 18:05:47 -0500432 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200433 ret = -1;
434 goto done;
435 }
436
437 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
438
Dan Williams0c9ca692007-08-02 10:43:44 -0400439 /* set the capability info */
440 tmpcap = (bss->capability & CAPINFO_MASK);
441 if (bss->mode == IW_MODE_INFRA)
442 tmpcap |= WLAN_CAPABILITY_ESS;
443 passo->capability = cpu_to_le16(tmpcap);
Holger Schurig91843462007-11-28 14:05:02 +0100444 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200445
Holger Schurig9012b282007-05-25 11:27:16 -0400446done:
Holger Schurig91843462007-11-28 14:05:02 +0100447 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448 return ret;
449}
450
Holger Schurig69f90322007-11-23 15:43:44 +0100451int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
Dan Williamse76850d2007-05-25 17:09:41 -0400452 struct cmd_ds_command *cmd, void *pdata_buf)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200453{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454 struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
455 int ret = 0;
456 int cmdappendsize = 0;
Dan Williamse76850d2007-05-25 17:09:41 -0400457 struct assoc_request * assoc_req = pdata_buf;
Dan Williams0c9ca692007-08-02 10:43:44 -0400458 u16 tmpcap = 0;
Dan Williams8c512762007-08-02 11:40:45 -0400459 size_t ratesize = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460
Holger Schurig9012b282007-05-25 11:27:16 -0400461 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462
David Woodhouseaa21c002007-12-08 20:04:36 +0000463 if (!priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464 ret = -1;
465 goto done;
466 }
467
Dan Williams0aef64d2007-08-02 11:31:18 -0400468 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200470 /*
471 * Fill in the parameters for 2 data structures:
472 * 1. cmd_ds_802_11_ad_hoc_start command
David Woodhouseaa21c002007-12-08 20:04:36 +0000473 * 2. priv->scantable[i]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474 *
475 * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
476 * probe delay, and cap info.
477 *
478 * Firmware will fill up beacon period, DTIM, Basic rates
479 * and operational rates.
480 */
481
Dan Williamsb44898e2007-08-02 11:18:40 -0400482 memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
483 memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484
Dan Williamsd8efea22007-05-28 23:54:55 -0400485 lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
486 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
487 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200488
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200489 /* set the BSS type */
Dan Williams0aef64d2007-08-02 11:31:18 -0400490 adhs->bsstype = CMD_BSS_TYPE_IBSS;
David Woodhouseaa21c002007-12-08 20:04:36 +0000491 priv->mode = IW_MODE_ADHOC;
492 if (priv->beacon_period == 0)
493 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
494 adhs->beaconperiod = cpu_to_le16(priv->beacon_period);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495
496 /* set Physical param set */
497#define DS_PARA_IE_ID 3
498#define DS_PARA_IE_LEN 1
499
500 adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
501 adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
502
Dan Williamse76850d2007-05-25 17:09:41 -0400503 WARN_ON(!assoc_req->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504
Holger Schurig9012b282007-05-25 11:27:16 -0400505 lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400506 assoc_req->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200507
Dan Williamse76850d2007-05-25 17:09:41 -0400508 adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509
510 /* set IBSS param set */
511#define IBSS_PARA_IE_ID 6
512#define IBSS_PARA_IE_LEN 2
513
514 adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
515 adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
Holger Schurigae596ce2007-08-02 13:10:05 -0400516 adhs->ssparamset.ibssparamset.atimwindow = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200517
518 /* set capability info */
Dan Williams0c9ca692007-08-02 10:43:44 -0400519 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williamse76850d2007-05-25 17:09:41 -0400520 if (assoc_req->secinfo.wep_enabled) {
Holger Schurig9012b282007-05-25 11:27:16 -0400521 lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400522 tmpcap |= WLAN_CAPABILITY_PRIVACY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400524 lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 }
Dan Williams0c9ca692007-08-02 10:43:44 -0400526 adhs->capability = cpu_to_le16(tmpcap);
527
528 /* probedelay */
Dan Williams0aef64d2007-08-02 11:31:18 -0400529 adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530
Dan Williams8c512762007-08-02 11:40:45 -0400531 memset(adhs->rates, 0, sizeof(adhs->rates));
Holger Schurig10078322007-11-15 18:05:47 -0500532 ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
533 memcpy(adhs->rates, lbs_bg_rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535 /* Copy the ad-hoc creating rates into Current BSS state structure */
David Woodhouseaa21c002007-12-08 20:04:36 +0000536 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
537 memcpy(&priv->curbssparams.rates, &adhs->rates, ratesize);
Dan Williams8c512762007-08-02 11:40:45 -0400538
539 /* Set MSB on basic rates as the firmware requires, but _after_
540 * copying to current bss rates.
541 */
Holger Schurig10078322007-11-15 18:05:47 -0500542 lbs_set_basic_rate_flags(adhs->rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200543
Holger Schurig9012b282007-05-25 11:27:16 -0400544 lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
Dan Williams8c512762007-08-02 11:40:45 -0400545 adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546
Holger Schurig9012b282007-05-25 11:27:16 -0400547 lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200548
Holger Schurig10078322007-11-15 18:05:47 -0500549 if (lbs_create_dnld_countryinfo_11d(priv)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400550 lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551 ret = -1;
552 goto done;
553 }
554
David Woodhouse981f1872007-05-25 23:36:54 -0400555 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
556 S_DS_GEN + cmdappendsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557
558 ret = 0;
559done:
Holger Schurig9012b282007-05-25 11:27:16 -0400560 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561 return ret;
562}
563
Holger Schurige98a88d2008-03-19 14:25:58 +0100564int lbs_cmd_80211_ad_hoc_stop(struct cmd_ds_command *cmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200565{
Dan Williams0aef64d2007-08-02 11:31:18 -0400566 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567 cmd->size = cpu_to_le16(S_DS_GEN);
568
569 return 0;
570}
571
Holger Schurig69f90322007-11-23 15:43:44 +0100572int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573 struct cmd_ds_command *cmd, void *pdata_buf)
574{
Dan Williams0c9ca692007-08-02 10:43:44 -0400575 struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
Dan Williamse76850d2007-05-25 17:09:41 -0400576 struct assoc_request * assoc_req = pdata_buf;
577 struct bss_descriptor *bss = &assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578 int cmdappendsize = 0;
579 int ret = 0;
Dan Williams8c512762007-08-02 11:40:45 -0400580 u16 ratesize = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700581 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200582
Holger Schurig9012b282007-05-25 11:27:16 -0400583 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200584
Dan Williams0aef64d2007-08-02 11:31:18 -0400585 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586
Dan Williams0aef64d2007-08-02 11:31:18 -0400587 join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
Dan Williams0c9ca692007-08-02 10:43:44 -0400588 join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200589
Dan Williams0c9ca692007-08-02 10:43:44 -0400590 memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
591 memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592
Dan Williams0c9ca692007-08-02 10:43:44 -0400593 memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
594 sizeof(union ieeetypes_phyparamset));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595
Dan Williams0c9ca692007-08-02 10:43:44 -0400596 memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
597 sizeof(union IEEEtypes_ssparamset));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200598
Dan Williams0c9ca692007-08-02 10:43:44 -0400599 join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400600 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
Dan Williams0c9ca692007-08-02 10:43:44 -0400601 bss->capability, CAPINFO_MASK);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602
603 /* information on BSSID descriptor passed to FW */
Dan Williamse76850d2007-05-25 17:09:41 -0400604 lbs_deb_join(
Joe Perches0795af52007-10-03 17:59:30 -0700605 "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
606 print_mac(mac, join_cmd->bss.bssid),
607 join_cmd->bss.ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 /* failtimeout */
Dan Williams0c9ca692007-08-02 10:43:44 -0400610 join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611
612 /* probedelay */
Dan Williams0aef64d2007-08-02 11:31:18 -0400613 join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614
David Woodhouseaa21c002007-12-08 20:04:36 +0000615 priv->curbssparams.channel = bss->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616
Dan Williams8c512762007-08-02 11:40:45 -0400617 /* Copy Data rates from the rates recorded in scan response */
618 memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
619 ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
620 memcpy(join_cmd->bss.rates, bss->rates, ratesize);
David Woodhouseaa21c002007-12-08 20:04:36 +0000621 if (get_common_rates(priv, join_cmd->bss.rates, &ratesize)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400622 lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623 ret = -1;
624 goto done;
625 }
626
Dan Williams8c512762007-08-02 11:40:45 -0400627 /* Copy the ad-hoc creating rates into Current BSS state structure */
David Woodhouseaa21c002007-12-08 20:04:36 +0000628 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
629 memcpy(&priv->curbssparams.rates, join_cmd->bss.rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630
Dan Williams8c512762007-08-02 11:40:45 -0400631 /* Set MSB on basic rates as the firmware requires, but _after_
632 * copying to current bss rates.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200633 */
Holger Schurig10078322007-11-15 18:05:47 -0500634 lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200635
Dan Williams0c9ca692007-08-02 10:43:44 -0400636 join_cmd->bss.ssparamset.ibssparamset.atimwindow =
Dan Williamse76850d2007-05-25 17:09:41 -0400637 cpu_to_le16(bss->atimwindow);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638
Dan Williamse76850d2007-05-25 17:09:41 -0400639 if (assoc_req->secinfo.wep_enabled) {
Dan Williams0c9ca692007-08-02 10:43:44 -0400640 u16 tmp = le16_to_cpu(join_cmd->bss.capability);
641 tmp |= WLAN_CAPABILITY_PRIVACY;
642 join_cmd->bss.capability = cpu_to_le16(tmp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643 }
644
David Woodhouseaa21c002007-12-08 20:04:36 +0000645 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200646 /* wake up first */
David Woodhouse981f1872007-05-25 23:36:54 -0400647 __le32 Localpsmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200648
Holger Schurig10078322007-11-15 18:05:47 -0500649 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
650 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400651 CMD_802_11_PS_MODE,
652 CMD_ACT_SET,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653 0, 0, &Localpsmode);
654
655 if (ret) {
656 ret = -1;
657 goto done;
658 }
659 }
660
Holger Schurig10078322007-11-15 18:05:47 -0500661 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662 ret = -1;
663 goto done;
664 }
665
David Woodhouse981f1872007-05-25 23:36:54 -0400666 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
667 S_DS_GEN + cmdappendsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200668
Holger Schurig9012b282007-05-25 11:27:16 -0400669done:
670 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200671 return ret;
672}
673
Holger Schurig69f90322007-11-23 15:43:44 +0100674int lbs_ret_80211_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200675 struct cmd_ds_command *resp)
676{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200677 int ret = 0;
678 union iwreq_data wrqu;
679 struct ieeetypes_assocrsp *passocrsp;
Dan Williamse76850d2007-05-25 17:09:41 -0400680 struct bss_descriptor * bss;
Dan Williamsc7fdf262007-08-02 13:20:29 -0400681 u16 status_code;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200682
Holger Schurig91843462007-11-28 14:05:02 +0100683 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200684
David Woodhouseaa21c002007-12-08 20:04:36 +0000685 if (!priv->in_progress_assoc_req) {
Holger Schurig91843462007-11-28 14:05:02 +0100686 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400687 ret = -1;
688 goto done;
689 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000690 bss = &priv->in_progress_assoc_req->bss;
Dan Williamse76850d2007-05-25 17:09:41 -0400691
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200692 passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
693
Dan Williamsc7fdf262007-08-02 13:20:29 -0400694 /*
695 * Older FW versions map the IEEE 802.11 Status Code in the association
696 * response to the following values returned in passocrsp->statuscode:
697 *
698 * IEEE Status Code Marvell Status Code
699 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
700 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
701 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
702 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
703 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
704 * others -> 0x0003 ASSOC_RESULT_REFUSED
705 *
706 * Other response codes:
707 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
708 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
709 * association response from the AP)
710 */
711
712 status_code = le16_to_cpu(passocrsp->statuscode);
713 switch (status_code) {
714 case 0x00:
Dan Williamsc7fdf262007-08-02 13:20:29 -0400715 break;
716 case 0x01:
Holger Schurig91843462007-11-28 14:05:02 +0100717 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
Dan Williamsc7fdf262007-08-02 13:20:29 -0400718 break;
719 case 0x02:
Holger Schurig91843462007-11-28 14:05:02 +0100720 lbs_deb_assoc("ASSOC_RESP: internal timer "
721 "expired while waiting for the AP\n");
Dan Williamsc7fdf262007-08-02 13:20:29 -0400722 break;
723 case 0x03:
Holger Schurig91843462007-11-28 14:05:02 +0100724 lbs_deb_assoc("ASSOC_RESP: association "
725 "refused by AP\n");
Dan Williamsc7fdf262007-08-02 13:20:29 -0400726 break;
727 case 0x04:
Holger Schurig91843462007-11-28 14:05:02 +0100728 lbs_deb_assoc("ASSOC_RESP: authentication "
729 "refused by AP\n");
Dan Williamsc7fdf262007-08-02 13:20:29 -0400730 break;
731 default:
Holger Schurig91843462007-11-28 14:05:02 +0100732 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
733 " unknown\n", status_code);
Dan Williamsc7fdf262007-08-02 13:20:29 -0400734 break;
735 }
736
737 if (status_code) {
Holger Schurig10078322007-11-15 18:05:47 -0500738 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739 ret = -1;
740 goto done;
741 }
742
Holger Schurig91843462007-11-28 14:05:02 +0100743 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200744 le16_to_cpu(resp->size) - S_DS_GEN);
745
746 /* Send a Media Connected event, according to the Spec */
David Woodhouseaa21c002007-12-08 20:04:36 +0000747 priv->connect_status = LBS_CONNECTED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748
Dan Williamse76850d2007-05-25 17:09:41 -0400749 /* Update current SSID and BSSID */
David Woodhouseaa21c002007-12-08 20:04:36 +0000750 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
751 priv->curbssparams.ssid_len = bss->ssid_len;
752 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753
David Woodhouseaa21c002007-12-08 20:04:36 +0000754 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
755 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756
David Woodhouseaa21c002007-12-08 20:04:36 +0000757 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
758 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
759 priv->nextSNRNF = 0;
760 priv->numSNRNF = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761
Holger Schurig634b8f42007-05-25 13:05:16 -0400762 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500763 if (!priv->tx_pending_len)
764 netif_wake_queue(priv->dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400765
David Woodhouseaa21c002007-12-08 20:04:36 +0000766 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200767 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Holger Schurig634b8f42007-05-25 13:05:16 -0400768 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200769
Holger Schurig9012b282007-05-25 11:27:16 -0400770done:
Holger Schurig91843462007-11-28 14:05:02 +0100771 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200772 return ret;
773}
774
Holger Schurige98a88d2008-03-19 14:25:58 +0100775int lbs_ret_80211_disassociate(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776{
Holger Schurig9012b282007-05-25 11:27:16 -0400777 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200778
Holger Schurig10078322007-11-15 18:05:47 -0500779 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200780
Holger Schurig9012b282007-05-25 11:27:16 -0400781 lbs_deb_leave(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200782 return 0;
783}
784
Holger Schurig69f90322007-11-23 15:43:44 +0100785int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200786 struct cmd_ds_command *resp)
787{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200788 int ret = 0;
789 u16 command = le16_to_cpu(resp->command);
790 u16 result = le16_to_cpu(resp->result);
791 struct cmd_ds_802_11_ad_hoc_result *padhocresult;
792 union iwreq_data wrqu;
Dan Williamse76850d2007-05-25 17:09:41 -0400793 struct bss_descriptor *bss;
Joe Perches0795af52007-10-03 17:59:30 -0700794 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200795
Holger Schurig9012b282007-05-25 11:27:16 -0400796 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797
798 padhocresult = &resp->params.result;
799
Dan Williamse76850d2007-05-25 17:09:41 -0400800 lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
801 lbs_deb_join("ADHOC_RESP: command = %x\n", command);
802 lbs_deb_join("ADHOC_RESP: result = %x\n", result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803
David Woodhouseaa21c002007-12-08 20:04:36 +0000804 if (!priv->in_progress_assoc_req) {
Dan Williamse76850d2007-05-25 17:09:41 -0400805 lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
806 ret = -1;
807 goto done;
808 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000809 bss = &priv->in_progress_assoc_req->bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200810
811 /*
812 * Join result code 0 --> SUCCESS
813 */
814 if (result) {
Dan Williamse76850d2007-05-25 17:09:41 -0400815 lbs_deb_join("ADHOC_RESP: failed\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000816 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig10078322007-11-15 18:05:47 -0500817 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200818 }
Holger Schurig9012b282007-05-25 11:27:16 -0400819 ret = -1;
820 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200821 }
822
823 /*
824 * Now the join cmd should be successful
825 * If BSSID has changed use SSID to compare instead of BSSID
826 */
Dan Williamsd8efea22007-05-28 23:54:55 -0400827 lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
828 escape_essid(bss->ssid, bss->ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829
830 /* Send a Media Connected event, according to the Spec */
David Woodhouseaa21c002007-12-08 20:04:36 +0000831 priv->connect_status = LBS_CONNECTED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200832
Holger Schurig6b63cd02007-08-02 11:53:36 -0400833 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200834 /* Update the created network descriptor with the new BSSID */
Dan Williamsea8da922007-08-02 11:18:23 -0400835 memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200836 }
837
838 /* Set the BSSID from the joined/started descriptor */
David Woodhouseaa21c002007-12-08 20:04:36 +0000839 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200840
841 /* Set the new SSID to current SSID */
David Woodhouseaa21c002007-12-08 20:04:36 +0000842 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
843 priv->curbssparams.ssid_len = bss->ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200844
Holger Schurig634b8f42007-05-25 13:05:16 -0400845 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500846 if (!priv->tx_pending_len)
847 netif_wake_queue(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200848
849 memset(&wrqu, 0, sizeof(wrqu));
David Woodhouseaa21c002007-12-08 20:04:36 +0000850 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200851 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Holger Schurig634b8f42007-05-25 13:05:16 -0400852 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200853
Holger Schurig9012b282007-05-25 11:27:16 -0400854 lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000855 lbs_deb_join("ADHOC_RESP: channel = %d\n", priv->curbssparams.channel);
Joe Perches0795af52007-10-03 17:59:30 -0700856 lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
857 print_mac(mac, padhocresult->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200858
Holger Schurig9012b282007-05-25 11:27:16 -0400859done:
860 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200861 return ret;
862}
863
Holger Schurige98a88d2008-03-19 14:25:58 +0100864int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200865{
Holger Schurig9012b282007-05-25 11:27:16 -0400866 lbs_deb_enter(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200867
Holger Schurig10078322007-11-15 18:05:47 -0500868 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200869
Holger Schurig9012b282007-05-25 11:27:16 -0400870 lbs_deb_leave(LBS_DEB_JOIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200871 return 0;
872}