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