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