[PATCH] libertas: make association paths consistent
[linux-2.6] / drivers / net / wireless / libertas / fw.c
1 /**
2   * This file contains the initialization for FW and HW
3   */
4 #include <linux/firmware.h>
5
6 #include "host.h"
7 #include "defs.h"
8 #include "decl.h"
9 #include "dev.h"
10 #include "wext.h"
11 #include "if_usb.h"
12
13 /**
14  *  @brief This function checks the validity of Boot2/FW image.
15  *
16  *  @param data              pointer to image
17  *         len               image length
18  *  @return     0 or -1
19  */
20 static int check_fwfile_format(u8 *data, u32 totlen)
21 {
22         u8  bincmd, exit;
23         u32 blksize, offset, len;
24         int ret;
25
26         ret = 1;
27         exit = len = 0;
28
29         do {
30                 bincmd = *data;
31                 blksize = *(u32*)(data + offsetof(struct fwheader, datalength));
32                 switch (bincmd) {
33                 case FW_HAS_DATA_TO_RECV:
34                         offset = sizeof(struct fwheader) + blksize;
35                         data += offset;
36                         len += offset;
37                         if (len >= totlen)
38                                 exit = 1;
39                         break;
40                 case FW_HAS_LAST_BLOCK:
41                         exit = 1;
42                         ret = 0;
43                         break;
44                 default:
45                         exit = 1;
46                         break;
47                 }
48         } while (!exit);
49
50         if (ret)
51                 lbs_pr_err("firmware file format check FAIL\n");
52         else
53                 lbs_deb_fw("firmware file format check PASS\n");
54
55         return ret;
56 }
57
58 /**
59  *  @brief This function downloads firmware image, gets
60  *  HW spec from firmware and set basic parameters to
61  *  firmware.
62  *
63  *  @param priv    A pointer to wlan_private structure
64  *  @return        0 or -1
65  */
66 static int wlan_setup_station_hw(wlan_private * priv, char *fw_name)
67 {
68         int ret = -1;
69         wlan_adapter *adapter = priv->adapter;
70
71         lbs_deb_enter(LBS_DEB_FW);
72
73         if ((ret = request_firmware(&priv->firmware, fw_name,
74                                     priv->hotplug_device)) < 0) {
75                 lbs_pr_err("request_firmware() failed with %#x\n",
76                        ret);
77                 lbs_pr_err("firmware %s not found\n", fw_name);
78                 goto done;
79         }
80
81         if(check_fwfile_format(priv->firmware->data, priv->firmware->size)) {
82                 release_firmware(priv->firmware);
83                 goto done;
84         }
85
86         ret = priv->hw_prog_firmware(priv);
87
88         release_firmware(priv->firmware);
89
90         if (ret) {
91                 lbs_deb_fw("bootloader in invalid state\n");
92                 ret = -1;
93                 goto done;
94         }
95
96         /*
97          * Read MAC address from HW
98          */
99         memset(adapter->current_addr, 0xff, ETH_ALEN);
100
101         ret = libertas_prepare_and_send_command(priv, cmd_get_hw_spec,
102                                     0, cmd_option_waitforrsp, 0, NULL);
103
104         if (ret) {
105                 ret = -1;
106                 goto done;
107         }
108
109         libertas_set_mac_packet_filter(priv);
110
111         /* Get the supported Data rates */
112         ret = libertas_prepare_and_send_command(priv, cmd_802_11_data_rate,
113                                     cmd_act_get_tx_rate,
114                                     cmd_option_waitforrsp, 0, NULL);
115
116         if (ret) {
117                 ret = -1;
118                 goto done;
119         }
120
121         ret = 0;
122 done:
123         lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
124         return ret;
125 }
126
127 static int wlan_allocate_adapter(wlan_private * priv)
128 {
129         size_t bufsize;
130         wlan_adapter *adapter = priv->adapter;
131
132         /* Allocate buffer to store the BSSID list */
133         bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
134         adapter->networks = kzalloc(bufsize, GFP_KERNEL);
135         if (!adapter->networks) {
136                 lbs_pr_err("Out of memory allocating beacons\n");
137                 libertas_free_adapter(priv);
138                 return -ENOMEM;
139         }
140
141         /* Allocate the command buffers */
142         libertas_allocate_cmd_buffer(priv);
143
144         memset(&adapter->libertas_ps_confirm_sleep, 0, sizeof(struct PS_CMD_ConfirmSleep));
145         adapter->libertas_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
146         adapter->libertas_ps_confirm_sleep.command =
147             cpu_to_le16(cmd_802_11_ps_mode);
148         adapter->libertas_ps_confirm_sleep.size =
149             cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
150         adapter->libertas_ps_confirm_sleep.result = 0;
151         adapter->libertas_ps_confirm_sleep.action =
152             cpu_to_le16(cmd_subcmd_sleep_confirmed);
153
154         return 0;
155 }
156
157 static void wlan_init_adapter(wlan_private * priv)
158 {
159         wlan_adapter *adapter = priv->adapter;
160         int i;
161
162         adapter->scanprobes = 0;
163
164         adapter->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
165         adapter->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
166
167         /* ATIM params */
168         adapter->atimwindow = 0;
169
170         adapter->connect_status = libertas_disconnected;
171         memset(adapter->current_addr, 0xff, ETH_ALEN);
172
173         /* scan type */
174         adapter->scantype = cmd_scan_type_active;
175
176         /* scan mode */
177         adapter->scanmode = cmd_bss_type_any;
178
179         /* 802.11 specific */
180         adapter->secinfo.wep_enabled = 0;
181         for (i = 0; i < sizeof(adapter->wep_keys) / sizeof(adapter->wep_keys[0]);
182              i++)
183                 memset(&adapter->wep_keys[i], 0, sizeof(struct WLAN_802_11_KEY));
184         adapter->wep_tx_keyidx = 0;
185         adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
186         adapter->mode = IW_MODE_INFRA;
187
188         adapter->pending_assoc_req = NULL;
189         adapter->in_progress_assoc_req = NULL;
190
191         /* Initialize scan result lists */
192         INIT_LIST_HEAD(&adapter->network_free_list);
193         INIT_LIST_HEAD(&adapter->network_list);
194         for (i = 0; i < MAX_NETWORK_COUNT; i++) {
195                 list_add_tail(&adapter->networks[i].list,
196                               &adapter->network_free_list);
197         }
198
199         mutex_init(&adapter->lock);
200
201         adapter->prescan = 1;
202
203         memset(&adapter->curbssparams, 0, sizeof(adapter->curbssparams));
204
205         /* PnP and power profile */
206         adapter->surpriseremoved = 0;
207
208         adapter->currentpacketfilter =
209             cmd_act_mac_rx_on | cmd_act_mac_tx_on;
210
211         adapter->radioon = RADIO_ON;
212         adapter->txantenna = RF_ANTENNA_2;
213         adapter->rxantenna = RF_ANTENNA_AUTO;
214
215         adapter->is_datarate_auto = 1;
216         adapter->beaconperiod = MRVDRV_BEACON_INTERVAL;
217
218         // set default value of capinfo.
219 #define SHORT_PREAMBLE_ALLOWED          1
220         memset(&adapter->capinfo, 0, sizeof(adapter->capinfo));
221         adapter->capinfo.shortpreamble = SHORT_PREAMBLE_ALLOWED;
222
223         adapter->psmode = wlan802_11powermodecam;
224         adapter->multipledtim = MRVDRV_DEFAULT_MULTIPLE_DTIM;
225
226         adapter->listeninterval = MRVDRV_DEFAULT_LISTEN_INTERVAL;
227
228         adapter->psstate = PS_STATE_FULL_POWER;
229         adapter->needtowakeup = 0;
230         adapter->locallisteninterval = 0;       /* default value in firmware will be used */
231
232         adapter->datarate = 0;  // Initially indicate the rate as auto
233
234         adapter->adhoc_grate_enabled = 0;
235
236         adapter->intcounter = 0;
237
238         adapter->currenttxskb = NULL;
239         adapter->pkttxctrl = 0;
240
241         memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
242         adapter->tx_queue_idx = 0;
243         spin_lock_init(&adapter->txqueue_lock);
244
245         return;
246 }
247
248 static void command_timer_fn(unsigned long data);
249
250 int libertas_init_fw(wlan_private * priv, char *fw_name)
251 {
252         int ret = -1;
253         wlan_adapter *adapter = priv->adapter;
254
255         lbs_deb_enter(LBS_DEB_FW);
256
257         /* Allocate adapter structure */
258         if ((ret = wlan_allocate_adapter(priv)) != 0)
259                 goto done;
260
261         /* init adapter structure */
262         wlan_init_adapter(priv);
263
264         /* init timer etc. */
265         setup_timer(&adapter->command_timer, command_timer_fn,
266                         (unsigned long)priv);
267
268         /* download fimrware etc. */
269         if ((ret = wlan_setup_station_hw(priv, fw_name)) != 0) {
270                 del_timer_sync(&adapter->command_timer);
271                 goto done;
272         }
273
274         /* init 802.11d */
275         libertas_init_11d(priv);
276
277         ret = 0;
278 done:
279         lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
280         return ret;
281 }
282
283 void libertas_free_adapter(wlan_private * priv)
284 {
285         wlan_adapter *adapter = priv->adapter;
286
287         if (!adapter) {
288                 lbs_deb_fw("why double free adapter?\n");
289                 return;
290         }
291
292         lbs_deb_fw("free command buffer\n");
293         libertas_free_cmd_buffer(priv);
294
295         lbs_deb_fw("free command_timer\n");
296         del_timer(&adapter->command_timer);
297
298         lbs_deb_fw("free scan results table\n");
299         kfree(adapter->networks);
300         adapter->networks = NULL;
301
302         /* Free the adapter object itself */
303         lbs_deb_fw("free adapter\n");
304         kfree(adapter);
305         priv->adapter = NULL;
306 }
307
308 /**
309  *  This function handles the timeout of command sending.
310  *  It will re-send the same command again.
311  */
312 static void command_timer_fn(unsigned long data)
313 {
314         wlan_private *priv = (wlan_private *)data;
315         wlan_adapter *adapter = priv->adapter;
316         struct cmd_ctrl_node *ptempnode;
317         struct cmd_ds_command *cmd;
318         unsigned long flags;
319
320         ptempnode = adapter->cur_cmd;
321         if (ptempnode == NULL) {
322                 lbs_deb_fw("ptempnode empty\n");
323                 return;
324         }
325
326         cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
327         if (!cmd) {
328                 lbs_deb_fw("cmd is NULL\n");
329                 return;
330         }
331
332         lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
333
334         if (!adapter->fw_ready)
335                 return;
336
337         spin_lock_irqsave(&adapter->driver_lock, flags);
338         adapter->cur_cmd = NULL;
339         spin_unlock_irqrestore(&adapter->driver_lock, flags);
340
341         lbs_deb_fw("re-sending same command because of timeout\n");
342         libertas_queue_cmd(adapter, ptempnode, 0);
343
344         wake_up_interruptible(&priv->mainthread.waitq);
345
346         return;
347 }