blob: ed57cf863b6981230b372810cfc8ef948acac738 [file] [log] [blame]
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001/*
2 * This file contains the major functions in WLAN
3 * driver. It includes init, exit, open, close and main
4 * thread etc..
5 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02006
Holger Schuriga46c6412007-05-25 11:32:07 -04007#include <linux/moduleparam.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02008#include <linux/delay.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009#include <linux/etherdevice.h>
10#include <linux/netdevice.h>
11#include <linux/if_arp.h>
Dan Williamsfe336152007-08-02 11:32:25 -040012#include <linux/kthread.h>
Holger Schurig7919b892008-04-01 14:50:43 +020013#include <linux/kfifo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Holger Schurigff9fc792009-10-06 16:31:54 +020015#include <net/cfg80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020016
17#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018#include "decl.h"
19#include "dev.h"
Holger Schurigff9fc792009-10-06 16:31:54 +020020#include "cfg.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020021#include "debugfs.h"
Dan Williams6e66f032007-12-11 12:42:16 -050022#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020023
Dan Williams7856a542007-08-03 09:43:03 -040024#define DRIVER_RELEASE_VERSION "323.p0"
Holger Schurig10078322007-11-15 18:05:47 -050025const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
Dan Williams3ce40232007-05-10 23:03:07 -040026#ifdef DEBUG
27 "-dbg"
28#endif
29 "";
30
Holger Schuriga46c6412007-05-25 11:32:07 -040031
32/* Module parameters */
Holger Schurig10078322007-11-15 18:05:47 -050033unsigned int lbs_debug;
34EXPORT_SYMBOL_GPL(lbs_debug);
35module_param_named(libertas_debug, lbs_debug, int, 0644);
Holger Schuriga46c6412007-05-25 11:32:07 -040036
37
Randy Dunlap8973a6e2011-04-26 15:25:29 -070038/*
39 * This global structure is used to send the confirm_sleep command as
40 * fast as possible down to the firmware.
41 */
Holger Schurigf539f2e2008-03-26 13:22:11 +010042struct cmd_confirm_sleep confirm_sleep;
43
44
Randy Dunlap8973a6e2011-04-26 15:25:29 -070045/*
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020046 * the table to keep region code
47 */
Holger Schurig10078322007-11-15 18:05:47 -050048u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020049 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
50
Randy Dunlap8973a6e2011-04-26 15:25:29 -070051/*
Dan Williams8c512762007-08-02 11:40:45 -040052 * FW rate table. FW refers to rates by their index in this table, not by the
53 * rate value itself. Values of 0x00 are
54 * reserved positions.
55 */
56static u8 fw_data_rates[MAX_RATES] =
57 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
58 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
59};
60
61/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070062 * lbs_fw_index_to_data_rate - use index to get the data rate
Dan Williams8c512762007-08-02 11:40:45 -040063 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070064 * @idx: The index of data rate
65 * returns: data rate or 0
Dan Williams8c512762007-08-02 11:40:45 -040066 */
Holger Schurig10078322007-11-15 18:05:47 -050067u32 lbs_fw_index_to_data_rate(u8 idx)
Dan Williams8c512762007-08-02 11:40:45 -040068{
69 if (idx >= sizeof(fw_data_rates))
70 idx = 0;
71 return fw_data_rates[idx];
72}
73
74/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070075 * lbs_data_rate_to_fw_index - use rate to get the index
Dan Williams8c512762007-08-02 11:40:45 -040076 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070077 * @rate: data rate
78 * returns: index or 0
Dan Williams8c512762007-08-02 11:40:45 -040079 */
Holger Schurig10078322007-11-15 18:05:47 -050080u8 lbs_data_rate_to_fw_index(u32 rate)
Dan Williams8c512762007-08-02 11:40:45 -040081{
82 u8 i;
83
84 if (!rate)
85 return 0;
86
87 for (i = 0; i < sizeof(fw_data_rates); i++) {
88 if (rate == fw_data_rates[i])
89 return i;
90 }
91 return 0;
92}
93
Anna Neal6fb53252008-12-09 13:23:45 -080094
David Woodhouse23a397a2007-12-11 18:56:42 -050095/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070096 * lbs_dev_open - open the ethX interface
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020097 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070098 * @dev: A pointer to &net_device structure
99 * returns: 0 or -EBUSY if monitor mode active
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200100 */
Holger Schurig10078322007-11-15 18:05:47 -0500101static int lbs_dev_open(struct net_device *dev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200102{
Kiran Divekarab65f642009-02-19 19:32:39 -0500103 struct lbs_private *priv = dev->ml_priv;
David Woodhouse85528552007-12-10 16:38:18 -0500104 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200105
Holger Schurig61d30022008-01-16 15:59:52 +0100106 lbs_deb_enter(LBS_DEB_NET);
107
David Woodhouse85528552007-12-10 16:38:18 -0500108 spin_lock_irq(&priv->driver_lock);
Daniel Drake2e301682010-11-04 21:21:52 +0000109 priv->stopping = false;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200110
Holger Schurige0e42da2009-11-25 13:10:15 +0100111 if (priv->connect_status == LBS_CONNECTED)
112 netif_carrier_on(dev);
113 else
114 netif_carrier_off(dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200115
David Woodhouse85528552007-12-10 16:38:18 -0500116 if (!priv->tx_pending_len)
117 netif_wake_queue(dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500118
David Woodhouse85528552007-12-10 16:38:18 -0500119 spin_unlock_irq(&priv->driver_lock);
Holger Schurig61d30022008-01-16 15:59:52 +0100120 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
David Woodhouse85528552007-12-10 16:38:18 -0500121 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200122}
123
124/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700125 * lbs_eth_stop - close the ethX interface
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200126 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700127 * @dev: A pointer to &net_device structure
128 * returns: 0
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200129 */
David Woodhouse85528552007-12-10 16:38:18 -0500130static int lbs_eth_stop(struct net_device *dev)
Holger Schurig78523da2007-05-25 11:49:19 -0400131{
Kiran Divekarab65f642009-02-19 19:32:39 -0500132 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200133
Holger Schurig61d30022008-01-16 15:59:52 +0100134 lbs_deb_enter(LBS_DEB_NET);
135
David Woodhouse85528552007-12-10 16:38:18 -0500136 spin_lock_irq(&priv->driver_lock);
Daniel Drake2e301682010-11-04 21:21:52 +0000137 priv->stopping = true;
David Woodhouse85528552007-12-10 16:38:18 -0500138 netif_stop_queue(dev);
David Woodhouse85528552007-12-10 16:38:18 -0500139 spin_unlock_irq(&priv->driver_lock);
Holger Schurig61d30022008-01-16 15:59:52 +0100140
David Woodhouse75bf45a2008-05-20 13:32:45 +0100141 schedule_work(&priv->mcast_work);
Daniel Drake2e301682010-11-04 21:21:52 +0000142 cancel_delayed_work_sync(&priv->scan_work);
143 if (priv->scan_req) {
144 cfg80211_scan_done(priv->scan_req, false);
145 priv->scan_req = NULL;
146 }
David Woodhouse75bf45a2008-05-20 13:32:45 +0100147
Holger Schurig61d30022008-01-16 15:59:52 +0100148 lbs_deb_leave(LBS_DEB_NET);
David Woodhouse85528552007-12-10 16:38:18 -0500149 return 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200150}
151
Holger Schurig10078322007-11-15 18:05:47 -0500152static void lbs_tx_timeout(struct net_device *dev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200153{
Kiran Divekarab65f642009-02-19 19:32:39 -0500154 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200155
Holger Schurig9012b282007-05-25 11:27:16 -0400156 lbs_deb_enter(LBS_DEB_TX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200157
Holger Schurig9012b282007-05-25 11:27:16 -0400158 lbs_pr_err("tx watch dog timeout\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700160 dev->trans_start = jiffies; /* prevent tx timeout */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200161
Holger Schurig7919b892008-04-01 14:50:43 +0200162 if (priv->currenttxskb)
163 lbs_send_tx_feedback(priv, 0);
164
David Woodhousea63b22b2007-12-08 20:56:44 +0000165 /* XX: Shouldn't we also call into the hw-specific driver
166 to kick it somehow? */
167 lbs_host_to_card_done(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168
Dan Williams9fb76632010-07-27 12:55:21 -0700169 /* FIXME: reset the card */
David Woodhouse354eca92007-12-17 19:22:40 -0500170
Holger Schurig9012b282007-05-25 11:27:16 -0400171 lbs_deb_leave(LBS_DEB_TX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200172}
173
David Woodhousee775ed72007-12-06 14:36:11 +0000174void lbs_host_to_card_done(struct lbs_private *priv)
175{
David Woodhousea63b22b2007-12-08 20:56:44 +0000176 unsigned long flags;
177
Holger Schurig61d30022008-01-16 15:59:52 +0100178 lbs_deb_enter(LBS_DEB_THREAD);
179
David Woodhousea63b22b2007-12-08 20:56:44 +0000180 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhousee775ed72007-12-06 14:36:11 +0000181
182 priv->dnld_sent = DNLD_RES_RECEIVED;
183
184 /* Wake main thread if commands are pending */
Amitkumar Karwar49125452009-09-30 20:04:38 -0700185 if (!priv->cur_cmd || priv->tx_pending_len > 0) {
186 if (!priv->wakeup_dev_required)
187 wake_up_interruptible(&priv->waitq);
188 }
David Woodhousee775ed72007-12-06 14:36:11 +0000189
David Woodhousea63b22b2007-12-08 20:56:44 +0000190 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig61d30022008-01-16 15:59:52 +0100191 lbs_deb_leave(LBS_DEB_THREAD);
David Woodhousee775ed72007-12-06 14:36:11 +0000192}
193EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
194
Holger Schurige0e42da2009-11-25 13:10:15 +0100195int lbs_set_mac_address(struct net_device *dev, void *addr)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200196{
197 int ret = 0;
Kiran Divekarab65f642009-02-19 19:32:39 -0500198 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200199 struct sockaddr *phwaddr = addr;
Holger Schurig2af9f032008-03-26 09:58:32 +0100200 struct cmd_ds_802_11_mac_address cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200201
Holger Schurig9012b282007-05-25 11:27:16 -0400202 lbs_deb_enter(LBS_DEB_NET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200203
Luis Carlos Cobo Rus0a0d08a2007-05-25 23:05:27 -0400204 /* In case it was called from the mesh device */
Holger Schurig2af9f032008-03-26 09:58:32 +0100205 dev = priv->dev;
Luis Carlos Cobo Rus0a0d08a2007-05-25 23:05:27 -0400206
Holger Schurig2af9f032008-03-26 09:58:32 +0100207 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
208 cmd.action = cpu_to_le16(CMD_ACT_SET);
209 memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200210
Holger Schurig2af9f032008-03-26 09:58:32 +0100211 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200212 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400213 lbs_deb_net("set MAC address failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200214 goto done;
215 }
216
Holger Schurig2af9f032008-03-26 09:58:32 +0100217 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
218 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
Holger Schurig78523da2007-05-25 11:49:19 -0400219 if (priv->mesh_dev)
Holger Schurig2af9f032008-03-26 09:58:32 +0100220 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200221
222done:
Holger Schurig9012b282007-05-25 11:27:16 -0400223 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200224 return ret;
225}
226
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200227
David Woodhouse75bf45a2008-05-20 13:32:45 +0100228static inline int mac_in_list(unsigned char *list, int list_len,
229 unsigned char *mac)
230{
231 while (list_len) {
232 if (!memcmp(list, mac, ETH_ALEN))
233 return 1;
234 list += ETH_ALEN;
235 list_len--;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236 }
David Woodhouse75bf45a2008-05-20 13:32:45 +0100237 return 0;
238}
239
240
241static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
242 struct net_device *dev, int nr_addrs)
243{
244 int i = nr_addrs;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000245 struct netdev_hw_addr *ha;
Jiri Pirko655ffee2010-02-27 07:35:45 +0000246 int cnt;
David Woodhouse75bf45a2008-05-20 13:32:45 +0100247
248 if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
249 return nr_addrs;
250
David S. Millerb9e40852008-07-15 00:15:08 -0700251 netif_addr_lock_bh(dev);
Jiri Pirko655ffee2010-02-27 07:35:45 +0000252 cnt = netdev_mc_count(dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000253 netdev_for_each_mc_addr(ha, dev) {
254 if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
Johannes Berge1749612008-10-27 15:59:26 -0700255 lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
Jiri Pirko22bedad2010-04-01 21:22:57 +0000256 ha->addr);
Jiri Pirko655ffee2010-02-27 07:35:45 +0000257 cnt--;
David Woodhouse75bf45a2008-05-20 13:32:45 +0100258 continue;
259 }
260
261 if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
262 break;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000263 memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700264 lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
Jiri Pirko22bedad2010-04-01 21:22:57 +0000265 ha->addr);
David Woodhouse75bf45a2008-05-20 13:32:45 +0100266 i++;
Jiri Pirko655ffee2010-02-27 07:35:45 +0000267 cnt--;
David Woodhouse75bf45a2008-05-20 13:32:45 +0100268 }
David S. Millerb9e40852008-07-15 00:15:08 -0700269 netif_addr_unlock_bh(dev);
Jiri Pirko655ffee2010-02-27 07:35:45 +0000270 if (cnt)
David Woodhouse75bf45a2008-05-20 13:32:45 +0100271 return -EOVERFLOW;
272
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200273 return i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274}
275
David Woodhouse75bf45a2008-05-20 13:32:45 +0100276static void lbs_set_mcast_worker(struct work_struct *work)
277{
278 struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
279 struct cmd_ds_mac_multicast_adr mcast_cmd;
280 int dev_flags;
281 int nr_addrs;
282 int old_mac_control = priv->mac_control;
283
284 lbs_deb_enter(LBS_DEB_NET);
285
286 dev_flags = priv->dev->flags;
287 if (priv->mesh_dev)
288 dev_flags |= priv->mesh_dev->flags;
289
290 if (dev_flags & IFF_PROMISC) {
291 priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
292 priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
293 CMD_ACT_MAC_MULTICAST_ENABLE);
294 goto out_set_mac_control;
295 } else if (dev_flags & IFF_ALLMULTI) {
296 do_allmulti:
297 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
298 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
299 CMD_ACT_MAC_MULTICAST_ENABLE);
300 goto out_set_mac_control;
301 }
302
303 /* Once for priv->dev, again for priv->mesh_dev if it exists */
304 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
305 if (nr_addrs >= 0 && priv->mesh_dev)
306 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
307 if (nr_addrs < 0)
308 goto do_allmulti;
309
310 if (nr_addrs) {
311 int size = offsetof(struct cmd_ds_mac_multicast_adr,
312 maclist[6*nr_addrs]);
313
314 mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
315 mcast_cmd.hdr.size = cpu_to_le16(size);
316 mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
317
318 lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
319
320 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
321 } else
322 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
323
324 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
325 CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
326 out_set_mac_control:
327 if (priv->mac_control != old_mac_control)
328 lbs_set_mac_control(priv);
329
330 lbs_deb_leave(LBS_DEB_NET);
331}
332
Holger Schurige0e42da2009-11-25 13:10:15 +0100333void lbs_set_multicast_list(struct net_device *dev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200334{
Kiran Divekarab65f642009-02-19 19:32:39 -0500335 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200336
David Woodhouse75bf45a2008-05-20 13:32:45 +0100337 schedule_work(&priv->mcast_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338}
339
340/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700341 * lbs_thread - handles the major jobs in the LBS driver.
Holger Schurig9012b282007-05-25 11:27:16 -0400342 * It handles all events generated by firmware, RX data received
343 * from firmware and TX data sent from kernel.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200344 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700345 * @data: A pointer to &lbs_thread structure
346 * returns: 0
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347 */
Holger Schurig10078322007-11-15 18:05:47 -0500348static int lbs_thread(void *data)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200349{
Dan Williamsfe336152007-08-02 11:32:25 -0400350 struct net_device *dev = data;
Kiran Divekarab65f642009-02-19 19:32:39 -0500351 struct lbs_private *priv = dev->ml_priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352 wait_queue_t wait;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200353
Holger Schurig9012b282007-05-25 11:27:16 -0400354 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200355
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356 init_waitqueue_entry(&wait, current);
357
358 for (;;) {
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500359 int shouldsleep;
Holger Schurig7919b892008-04-01 14:50:43 +0200360 u8 resp_idx;
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500361
Holger Schurig7919b892008-04-01 14:50:43 +0200362 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
363 priv->currenttxskb, priv->dnld_sent);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364
Dan Williamsfe336152007-08-02 11:32:25 -0400365 add_wait_queue(&priv->waitq, &wait);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366 set_current_state(TASK_INTERRUPTIBLE);
David Woodhouseaa21c002007-12-08 20:04:36 +0000367 spin_lock_irq(&priv->driver_lock);
David Woodhouse59f3e4b2007-12-08 17:42:59 +0000368
David Woodhoused9f88702007-12-13 21:48:00 -0500369 if (kthread_should_stop())
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500370 shouldsleep = 0; /* Bye */
David Woodhoused9f88702007-12-13 21:48:00 -0500371 else if (priv->surpriseremoved)
372 shouldsleep = 1; /* We need to wait until we're _told_ to die */
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500373 else if (priv->psstate == PS_STATE_SLEEP)
374 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
David Woodhouse2a345092007-12-15 19:33:43 -0500375 else if (priv->cmd_timed_out)
376 shouldsleep = 0; /* Command timed out. Recover */
David Woodhouseb15152a2007-12-11 11:55:37 -0500377 else if (!priv->fw_ready)
378 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500379 else if (priv->dnld_sent)
380 shouldsleep = 1; /* Something is en route to the device already */
David Woodhouse2eb188a2007-12-09 23:54:27 -0500381 else if (priv->tx_pending_len > 0)
382 shouldsleep = 0; /* We've a packet to send */
Holger Schurigef707b82008-05-23 16:04:13 +0200383 else if (priv->resp_len[priv->resp_idx])
384 shouldsleep = 0; /* We have a command response */
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500385 else if (priv->cur_cmd)
386 shouldsleep = 1; /* Can't send a command; one already running */
Amitkumar Karwar49125452009-09-30 20:04:38 -0700387 else if (!list_empty(&priv->cmdpendingq) &&
388 !(priv->wakeup_dev_required))
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500389 shouldsleep = 0; /* We have a command to send */
Stefani Seibolde64c0262009-12-21 14:37:28 -0800390 else if (kfifo_len(&priv->event_fifo))
Holger Schurig7919b892008-04-01 14:50:43 +0200391 shouldsleep = 0; /* We have an event to process */
David Woodhouseb8d40bc2007-12-09 23:44:43 -0500392 else
393 shouldsleep = 1; /* No command */
394
395 if (shouldsleep) {
Holger Schurig7919b892008-04-01 14:50:43 +0200396 lbs_deb_thread("sleeping, connect_status %d, "
Holger Schurig5f505d92008-04-30 10:50:07 +0200397 "psmode %d, psstate %d\n",
Holger Schurig7919b892008-04-01 14:50:43 +0200398 priv->connect_status,
399 priv->psmode, priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +0000400 spin_unlock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200401 schedule();
402 } else
David Woodhouseaa21c002007-12-08 20:04:36 +0000403 spin_unlock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200404
Holger Schurig7919b892008-04-01 14:50:43 +0200405 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
406 priv->currenttxskb, priv->dnld_sent);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200407
408 set_current_state(TASK_RUNNING);
Dan Williamsfe336152007-08-02 11:32:25 -0400409 remove_wait_queue(&priv->waitq, &wait);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410
Holger Schurig7919b892008-04-01 14:50:43 +0200411 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
412 priv->currenttxskb, priv->dnld_sent);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200413
David Woodhoused9f88702007-12-13 21:48:00 -0500414 if (kthread_should_stop()) {
Holger Schurig7919b892008-04-01 14:50:43 +0200415 lbs_deb_thread("break from main thread\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416 break;
417 }
418
David Woodhoused9f88702007-12-13 21:48:00 -0500419 if (priv->surpriseremoved) {
420 lbs_deb_thread("adapter removed; waiting to die...\n");
421 continue;
422 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423
Holger Schurig7919b892008-04-01 14:50:43 +0200424 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
425 priv->currenttxskb, priv->dnld_sent);
426
Holger Schurig7919b892008-04-01 14:50:43 +0200427 /* Process any pending command response */
Holger Schuriga01f5452008-06-04 11:10:40 +0200428 spin_lock_irq(&priv->driver_lock);
Holger Schurig7919b892008-04-01 14:50:43 +0200429 resp_idx = priv->resp_idx;
430 if (priv->resp_len[resp_idx]) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000431 spin_unlock_irq(&priv->driver_lock);
Holger Schurig7919b892008-04-01 14:50:43 +0200432 lbs_process_command_response(priv,
433 priv->resp_buf[resp_idx],
434 priv->resp_len[resp_idx]);
David Woodhouseaa21c002007-12-08 20:04:36 +0000435 spin_lock_irq(&priv->driver_lock);
Holger Schurig7919b892008-04-01 14:50:43 +0200436 priv->resp_len[resp_idx] = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437 }
Holger Schurig7919b892008-04-01 14:50:43 +0200438 spin_unlock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200439
Amitkumar Karwar49125452009-09-30 20:04:38 -0700440 /* Process hardware events, e.g. card removed, link lost */
441 spin_lock_irq(&priv->driver_lock);
Stefani Seibolde64c0262009-12-21 14:37:28 -0800442 while (kfifo_len(&priv->event_fifo)) {
Amitkumar Karwar49125452009-09-30 20:04:38 -0700443 u32 event;
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800444
Stefani Seibold9842c382009-12-21 14:37:29 -0800445 if (kfifo_out(&priv->event_fifo,
446 (unsigned char *) &event, sizeof(event)) !=
447 sizeof(event))
448 break;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700449 spin_unlock_irq(&priv->driver_lock);
450 lbs_process_event(priv, event);
451 spin_lock_irq(&priv->driver_lock);
452 }
453 spin_unlock_irq(&priv->driver_lock);
454
455 if (priv->wakeup_dev_required) {
456 lbs_deb_thread("Waking up device...\n");
457 /* Wake up device */
458 if (priv->exit_deep_sleep(priv))
459 lbs_deb_thread("Wakeup device failed\n");
460 continue;
461 }
462
Holger Schurig7919b892008-04-01 14:50:43 +0200463 /* command timeout stuff */
David Woodhouse2a345092007-12-15 19:33:43 -0500464 if (priv->cmd_timed_out && priv->cur_cmd) {
465 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
466
Holger Schurig40e6fa82010-02-04 14:37:45 +0100467 lbs_pr_info("Timeout submitting command 0x%04x\n",
468 le16_to_cpu(cmdnode->cmdbuf->command));
469 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
470 if (priv->reset_card)
471 priv->reset_card(priv);
David Woodhouse2a345092007-12-15 19:33:43 -0500472 }
473 priv->cmd_timed_out = 0;
474
David Woodhouseb15152a2007-12-11 11:55:37 -0500475 if (!priv->fw_ready)
476 continue;
477
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478 /* Check if we need to confirm Sleep Request received previously */
David Woodhouseaa21c002007-12-08 20:04:36 +0000479 if (priv->psstate == PS_STATE_PRE_SLEEP &&
480 !priv->dnld_sent && !priv->cur_cmd) {
481 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig7919b892008-04-01 14:50:43 +0200482 lbs_deb_thread("pre-sleep, currenttxskb %p, "
483 "dnld_sent %d, cur_cmd %p\n",
484 priv->currenttxskb, priv->dnld_sent,
485 priv->cur_cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100487 lbs_ps_confirm_sleep(priv);
David Woodhouse59f3e4b2007-12-08 17:42:59 +0000488 } else {
489 /* workaround for firmware sending
490 * deauth/linkloss event immediately
491 * after sleep request; remove this
492 * after firmware fixes it
493 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000494 priv->psstate = PS_STATE_AWAKE;
Holger Schurig7919b892008-04-01 14:50:43 +0200495 lbs_pr_alert("ignore PS_SleepConfirm in "
496 "non-connected state\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 }
498 }
499
500 /* The PS state is changed during processing of Sleep Request
501 * event above
502 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000503 if ((priv->psstate == PS_STATE_SLEEP) ||
504 (priv->psstate == PS_STATE_PRE_SLEEP))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200505 continue;
506
Amitkumar Karwar49125452009-09-30 20:04:38 -0700507 if (priv->is_deep_sleep)
508 continue;
509
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 /* Execute the next command */
David Woodhouseaa21c002007-12-08 20:04:36 +0000511 if (!priv->dnld_sent && !priv->cur_cmd)
Holger Schurig10078322007-11-15 18:05:47 -0500512 lbs_execute_next_command(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513
David Woodhouse2eb188a2007-12-09 23:54:27 -0500514 spin_lock_irq(&priv->driver_lock);
515 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
516 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
517 priv->tx_pending_buf,
518 priv->tx_pending_len);
519 if (ret) {
520 lbs_deb_tx("host_to_card failed %d\n", ret);
521 priv->dnld_sent = DNLD_RES_RECEIVED;
522 }
523 priv->tx_pending_len = 0;
524 if (!priv->currenttxskb) {
525 /* We can wake the queues immediately if we aren't
526 waiting for TX feedback */
527 if (priv->connect_status == LBS_CONNECTED)
528 netif_wake_queue(priv->dev);
529 if (priv->mesh_dev &&
Holger Schurig602114a2009-12-02 15:26:01 +0100530 lbs_mesh_connected(priv))
David Woodhouse2eb188a2007-12-09 23:54:27 -0500531 netif_wake_queue(priv->mesh_dev);
532 }
533 }
534 spin_unlock_irq(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535 }
536
David Woodhouseaa21c002007-12-08 20:04:36 +0000537 del_timer(&priv->command_timer);
Amitkumar Karwar49125452009-09-30 20:04:38 -0700538 del_timer(&priv->auto_deepsleep_timer);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539
Holger Schurig9012b282007-05-25 11:27:16 -0400540 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541 return 0;
542}
543
Vasily Khoruzhick75abde42011-01-21 22:44:49 +0200544/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700545 * lbs_setup_firmware - gets the HW spec from the firmware and sets
546 * some basic parameters
Vasily Khoruzhick75abde42011-01-21 22:44:49 +0200547 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700548 * @priv: A pointer to &struct lbs_private structure
549 * returns: 0 or -1
Vasily Khoruzhick75abde42011-01-21 22:44:49 +0200550 */
551static int lbs_setup_firmware(struct lbs_private *priv)
552{
553 int ret = -1;
554 s16 curlevel = 0, minlevel = 0, maxlevel = 0;
555
556 lbs_deb_enter(LBS_DEB_FW);
557
558 /* Read MAC address from firmware */
559 memset(priv->current_addr, 0xff, ETH_ALEN);
560 ret = lbs_update_hw_spec(priv);
561 if (ret)
562 goto done;
563
564 /* Read power levels if available */
565 ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
566 if (ret == 0) {
567 priv->txpower_cur = curlevel;
568 priv->txpower_min = minlevel;
569 priv->txpower_max = maxlevel;
570 }
571
572 /* Send cmd to FW to enable 11D function */
573 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
574
575 lbs_set_mac_control(priv);
576done:
577 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
578 return ret;
579}
580
David Woodhouseab25eca2007-12-12 17:38:56 -0500581int lbs_suspend(struct lbs_private *priv)
582{
David Woodhouseab25eca2007-12-12 17:38:56 -0500583 int ret;
584
Holger Schurig61d30022008-01-16 15:59:52 +0100585 lbs_deb_enter(LBS_DEB_FW);
586
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700587 if (priv->is_deep_sleep) {
588 ret = lbs_set_deep_sleep(priv, 0);
589 if (ret) {
590 lbs_pr_err("deep sleep cancellation failed: %d\n", ret);
591 return ret;
592 }
593 priv->deep_sleep_required = 1;
David Woodhouse506e9022007-12-12 20:06:06 -0500594 }
595
Amitkumar Karwar13118432010-07-08 06:43:48 +0530596 ret = lbs_set_host_sleep(priv, 1);
David Woodhouse7e226272007-12-14 22:53:41 -0500597
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700598 netif_device_detach(priv->dev);
599 if (priv->mesh_dev)
600 netif_device_detach(priv->mesh_dev);
David Woodhouseab25eca2007-12-12 17:38:56 -0500601
Holger Schurig61d30022008-01-16 15:59:52 +0100602 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
David Woodhouseab25eca2007-12-12 17:38:56 -0500603 return ret;
604}
605EXPORT_SYMBOL_GPL(lbs_suspend);
606
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700607int lbs_resume(struct lbs_private *priv)
David Woodhouseab25eca2007-12-12 17:38:56 -0500608{
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700609 int ret;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700610
Holger Schurig61d30022008-01-16 15:59:52 +0100611 lbs_deb_enter(LBS_DEB_FW);
612
Amitkumar Karwar13118432010-07-08 06:43:48 +0530613 ret = lbs_set_host_sleep(priv, 0);
David Woodhouseab25eca2007-12-12 17:38:56 -0500614
615 netif_device_attach(priv->dev);
616 if (priv->mesh_dev)
617 netif_device_attach(priv->mesh_dev);
618
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700619 if (priv->deep_sleep_required) {
620 priv->deep_sleep_required = 0;
621 ret = lbs_set_deep_sleep(priv, 1);
622 if (ret)
623 lbs_pr_err("deep sleep activation failed: %d\n", ret);
624 }
625
Vasily Khoruzhick75abde42011-01-21 22:44:49 +0200626 if (priv->setup_fw_on_resume)
627 ret = lbs_setup_firmware(priv);
628
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700629 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
630 return ret;
David Woodhouseab25eca2007-12-12 17:38:56 -0500631}
632EXPORT_SYMBOL_GPL(lbs_resume);
633
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700635 * lbs_cmd_timeout_handler - handles the timeout of command sending.
636 * It will re-send the same command again.
637 *
638 * @data: &struct lbs_private pointer
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400639 */
Holger Schurig40e6fa82010-02-04 14:37:45 +0100640static void lbs_cmd_timeout_handler(unsigned long data)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400641{
Holger Schurig69f90322007-11-23 15:43:44 +0100642 struct lbs_private *priv = (struct lbs_private *)data;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400643 unsigned long flags;
644
Holger Schurig61d30022008-01-16 15:59:52 +0100645 lbs_deb_enter(LBS_DEB_CMD);
David Woodhouseaa21c002007-12-08 20:04:36 +0000646 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400647
Holger Schurig57962f02008-05-14 16:30:28 +0200648 if (!priv->cur_cmd)
David Woodhouse2a345092007-12-15 19:33:43 -0500649 goto out;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400650
Holger Schurig57962f02008-05-14 16:30:28 +0200651 lbs_pr_info("command 0x%04x timed out\n",
652 le16_to_cpu(priv->cur_cmd->cmdbuf->command));
David Woodhouse2a345092007-12-15 19:33:43 -0500653
654 priv->cmd_timed_out = 1;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400655 wake_up_interruptible(&priv->waitq);
Holger Schurig61d30022008-01-16 15:59:52 +0100656out:
David Woodhouse2a345092007-12-15 19:33:43 -0500657 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig61d30022008-01-16 15:59:52 +0100658 lbs_deb_leave(LBS_DEB_CMD);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400659}
660
Amitkumar Karwar49125452009-09-30 20:04:38 -0700661/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700662 * auto_deepsleep_timer_fn - put the device back to deep sleep mode when
663 * timer expires and no activity (command, event, data etc.) is detected.
664 * @data: &struct lbs_private pointer
665 * returns: N/A
Amitkumar Karwar49125452009-09-30 20:04:38 -0700666 */
667static void auto_deepsleep_timer_fn(unsigned long data)
668{
669 struct lbs_private *priv = (struct lbs_private *)data;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700670
671 lbs_deb_enter(LBS_DEB_CMD);
672
673 if (priv->is_activity_detected) {
674 priv->is_activity_detected = 0;
675 } else {
676 if (priv->is_auto_deep_sleep_enabled &&
Dan Williams53800f52010-07-27 13:15:01 -0700677 (!priv->wakeup_dev_required) &&
678 (priv->connect_status != LBS_CONNECTED)) {
679 struct cmd_header cmd;
680
Amitkumar Karwar49125452009-09-30 20:04:38 -0700681 lbs_deb_main("Entering auto deep sleep mode...\n");
Dan Williams53800f52010-07-27 13:15:01 -0700682 memset(&cmd, 0, sizeof(cmd));
683 cmd.size = cpu_to_le16(sizeof(cmd));
684 lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
685 sizeof(cmd));
Amitkumar Karwar49125452009-09-30 20:04:38 -0700686 }
687 }
688 mod_timer(&priv->auto_deepsleep_timer , jiffies +
689 (priv->auto_deep_sleep_timeout * HZ)/1000);
690 lbs_deb_leave(LBS_DEB_CMD);
691}
692
693int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
694{
695 lbs_deb_enter(LBS_DEB_SDIO);
696
697 priv->is_auto_deep_sleep_enabled = 1;
698 if (priv->is_deep_sleep)
699 priv->wakeup_dev_required = 1;
700 mod_timer(&priv->auto_deepsleep_timer ,
701 jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
702
703 lbs_deb_leave(LBS_DEB_SDIO);
704 return 0;
705}
706
707int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
708{
709 lbs_deb_enter(LBS_DEB_SDIO);
710
711 priv->is_auto_deep_sleep_enabled = 0;
712 priv->auto_deep_sleep_timeout = 0;
713 del_timer(&priv->auto_deepsleep_timer);
714
715 lbs_deb_leave(LBS_DEB_SDIO);
716 return 0;
717}
718
Holger Schurig69f90322007-11-23 15:43:44 +0100719static int lbs_init_adapter(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -0400720{
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530721 int ret;
Dan Williams954ee162007-08-20 11:43:25 -0400722
Holger Schurig61d30022008-01-16 15:59:52 +0100723 lbs_deb_enter(LBS_DEB_MAIN);
724
David Woodhouseaa21c002007-12-08 20:04:36 +0000725 memset(priv->current_addr, 0xff, ETH_ALEN);
Dan Williams954ee162007-08-20 11:43:25 -0400726
David Woodhouseaa21c002007-12-08 20:04:36 +0000727 priv->connect_status = LBS_DISCONNECTED;
Holger Schurigc14951f2009-10-22 15:30:50 +0200728 priv->channel = DEFAULT_AD_HOC_CHANNEL;
Holger Schurigd9e97782008-03-12 16:06:43 +0100729 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400730 priv->radio_on = 1;
David Woodhouseaa21c002007-12-08 20:04:36 +0000731 priv->psmode = LBS802_11POWERMODECAM;
732 priv->psstate = PS_STATE_FULL_POWER;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700733 priv->is_deep_sleep = 0;
734 priv->is_auto_deep_sleep_enabled = 0;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700735 priv->deep_sleep_required = 0;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700736 priv->wakeup_dev_required = 0;
737 init_waitqueue_head(&priv->ds_awake_q);
Dan Williamscc026812010-08-04 00:43:47 -0500738 init_waitqueue_head(&priv->scan_q);
Amitkumar Karwar921ca032010-02-25 17:16:36 -0800739 priv->authtype_auto = 1;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700740 priv->is_host_sleep_configured = 0;
741 priv->is_host_sleep_activated = 0;
742 init_waitqueue_head(&priv->host_sleep_q);
David Woodhouseaa21c002007-12-08 20:04:36 +0000743 mutex_init(&priv->lock);
Dan Williams954ee162007-08-20 11:43:25 -0400744
Holger Schurig40e6fa82010-02-04 14:37:45 +0100745 setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
Holger Schurig61d30022008-01-16 15:59:52 +0100746 (unsigned long)priv);
Amitkumar Karwar49125452009-09-30 20:04:38 -0700747 setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
748 (unsigned long)priv);
Dan Williams954ee162007-08-20 11:43:25 -0400749
David Woodhouseaa21c002007-12-08 20:04:36 +0000750 INIT_LIST_HEAD(&priv->cmdfreeq);
751 INIT_LIST_HEAD(&priv->cmdpendingq);
Dan Williams954ee162007-08-20 11:43:25 -0400752
David Woodhouseaa21c002007-12-08 20:04:36 +0000753 spin_lock_init(&priv->driver_lock);
Dan Williams954ee162007-08-20 11:43:25 -0400754
755 /* Allocate the command buffers */
Holger Schurig10078322007-11-15 18:05:47 -0500756 if (lbs_allocate_cmd_buffer(priv)) {
Dan Williams954ee162007-08-20 11:43:25 -0400757 lbs_pr_err("Out of memory allocating command buffers\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200758 ret = -ENOMEM;
759 goto out;
760 }
761 priv->resp_idx = 0;
762 priv->resp_len[0] = priv->resp_len[1] = 0;
763
764 /* Create the event FIFO */
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800765 ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800766 if (ret) {
Holger Schurig7919b892008-04-01 14:50:43 +0200767 lbs_pr_err("Out of memory allocating event FIFO buffer\n");
Holger Schurig7919b892008-04-01 14:50:43 +0200768 goto out;
Dan Williams954ee162007-08-20 11:43:25 -0400769 }
770
771out:
Holger Schurig61d30022008-01-16 15:59:52 +0100772 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
773
Dan Williams954ee162007-08-20 11:43:25 -0400774 return ret;
775}
776
Holger Schurig69f90322007-11-23 15:43:44 +0100777static void lbs_free_adapter(struct lbs_private *priv)
Holger Schurigac558ca2007-08-02 11:49:06 -0400778{
Holger Schurig61d30022008-01-16 15:59:52 +0100779 lbs_deb_enter(LBS_DEB_MAIN);
780
Holger Schurig10078322007-11-15 18:05:47 -0500781 lbs_free_cmd_buffer(priv);
Stefani Seibold45465482009-12-21 14:37:26 -0800782 kfifo_free(&priv->event_fifo);
David Woodhouseaa21c002007-12-08 20:04:36 +0000783 del_timer(&priv->command_timer);
Amitkumar Karwar49125452009-09-30 20:04:38 -0700784 del_timer(&priv->auto_deepsleep_timer);
Holger Schurig61d30022008-01-16 15:59:52 +0100785
786 lbs_deb_leave(LBS_DEB_MAIN);
Holger Schurigac558ca2007-08-02 11:49:06 -0400787}
788
Stephen Hemmingerf02abf12009-03-20 19:36:37 +0000789static const struct net_device_ops lbs_netdev_ops = {
790 .ndo_open = lbs_dev_open,
791 .ndo_stop = lbs_eth_stop,
792 .ndo_start_xmit = lbs_hard_start_xmit,
793 .ndo_set_mac_address = lbs_set_mac_address,
794 .ndo_tx_timeout = lbs_tx_timeout,
795 .ndo_set_multicast_list = lbs_set_multicast_list,
796 .ndo_change_mtu = eth_change_mtu,
797 .ndo_validate_addr = eth_validate_addr,
798};
799
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400800/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700801 * lbs_add_card - adds the card. It will probe the
Holger Schurig10078322007-11-15 18:05:47 -0500802 * card, allocate the lbs_priv and initialize the device.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700804 * @card: A pointer to card
805 * @dmdev: A pointer to &struct device
806 * returns: A pointer to &struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807 */
Holger Schurig69f90322007-11-23 15:43:44 +0100808struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200809{
Holger Schurigff9fc792009-10-06 16:31:54 +0200810 struct net_device *dev;
811 struct wireless_dev *wdev;
Holger Schurig69f90322007-11-23 15:43:44 +0100812 struct lbs_private *priv = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813
Holger Schurig61d30022008-01-16 15:59:52 +0100814 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200815
816 /* Allocate an Ethernet device and register it */
Holger Schurigff9fc792009-10-06 16:31:54 +0200817 wdev = lbs_cfg_alloc(dmdev);
818 if (IS_ERR(wdev)) {
819 lbs_pr_err("cfg80211 init failed\n");
Dan Williams954ee162007-08-20 11:43:25 -0400820 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200821 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530822
Holger Schurigff9fc792009-10-06 16:31:54 +0200823 wdev->iftype = NL80211_IFTYPE_STATION;
824 priv = wdev_priv(wdev);
825 priv->wdev = wdev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200826
Holger Schurig10078322007-11-15 18:05:47 -0500827 if (lbs_init_adapter(priv)) {
Dan Williams954ee162007-08-20 11:43:25 -0400828 lbs_pr_err("failed to initialize adapter structure.\n");
Holger Schurigff9fc792009-10-06 16:31:54 +0200829 goto err_wdev;
Dan Williams954ee162007-08-20 11:43:25 -0400830 }
831
Holger Schurigff9fc792009-10-06 16:31:54 +0200832 dev = alloc_netdev(0, "wlan%d", ether_setup);
833 if (!dev) {
834 dev_err(dmdev, "no memory for network device instance\n");
835 goto err_adapter;
836 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200837
Holger Schurigff9fc792009-10-06 16:31:54 +0200838 dev->ieee80211_ptr = wdev;
839 dev->ml_priv = priv;
840 SET_NETDEV_DEV(dev, dmdev);
841 wdev->netdev = dev;
842 priv->dev = dev;
843
Stephen Hemmingerf02abf12009-03-20 19:36:37 +0000844 dev->netdev_ops = &lbs_netdev_ops;
Holger Schurigfb3dddf2007-05-25 11:58:22 -0400845 dev->watchdog_timeo = 5 * HZ;
Holger Schurig10078322007-11-15 18:05:47 -0500846 dev->ethtool_ops = &lbs_ethtool_ops;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200847 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200848
Holger Schurigff9fc792009-10-06 16:31:54 +0200849 priv->card = card;
Holger Schurigff9fc792009-10-06 16:31:54 +0200850
Daniel Mackc00552c2009-08-11 16:09:34 +0200851 strcpy(dev->name, "wlan%d");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200852
Dan Williamsfe336152007-08-02 11:32:25 -0400853 lbs_deb_thread("Starting main thread...\n");
854 init_waitqueue_head(&priv->waitq);
Holger Schurig10078322007-11-15 18:05:47 -0500855 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
Dan Williamsfe336152007-08-02 11:32:25 -0400856 if (IS_ERR(priv->main_thread)) {
857 lbs_deb_thread("Error creating main thread.\n");
Holger Schurigff9fc792009-10-06 16:31:54 +0200858 goto err_ndev;
Dan Williamsfe336152007-08-02 11:32:25 -0400859 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200860
Holger Schurig10078322007-11-15 18:05:47 -0500861 priv->work_thread = create_singlethread_workqueue("lbs_worker");
David Woodhouse75bf45a2008-05-20 13:32:45 +0100862 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
David Woodhouse23a397a2007-12-11 18:56:42 -0500863
Deepak Saxenaae63a332010-10-31 13:40:33 +0000864 priv->wol_criteria = EHS_REMOVE_WAKEUP;
David Woodhouse506e9022007-12-12 20:06:06 -0500865 priv->wol_gpio = 0xff;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700866 priv->wol_gap = 20;
Deepak Saxenaae63a332010-10-31 13:40:33 +0000867 priv->ehs_remove_supported = true;
David Woodhouse506e9022007-12-12 20:06:06 -0500868
Dan Williams954ee162007-08-20 11:43:25 -0400869 goto done;
870
Holger Schurigff9fc792009-10-06 16:31:54 +0200871 err_ndev:
Dan Williams954ee162007-08-20 11:43:25 -0400872 free_netdev(dev);
Holger Schurigff9fc792009-10-06 16:31:54 +0200873
874 err_adapter:
875 lbs_free_adapter(priv);
876
877 err_wdev:
878 lbs_cfg_free(priv);
879
Dan Williams954ee162007-08-20 11:43:25 -0400880 priv = NULL;
881
882done:
Holger Schurig61d30022008-01-16 15:59:52 +0100883 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
Dan Williams954ee162007-08-20 11:43:25 -0400884 return priv;
885}
Holger Schurig10078322007-11-15 18:05:47 -0500886EXPORT_SYMBOL_GPL(lbs_add_card);
Dan Williams954ee162007-08-20 11:43:25 -0400887
888
Holger Schuriga63e5cb2008-04-30 10:50:39 +0200889void lbs_remove_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -0400890{
Dan Williams954ee162007-08-20 11:43:25 -0400891 struct net_device *dev = priv->dev;
Dan Williams954ee162007-08-20 11:43:25 -0400892
893 lbs_deb_enter(LBS_DEB_MAIN);
894
David Woodhouse23a397a2007-12-11 18:56:42 -0500895 lbs_remove_mesh(priv);
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530896 lbs_scan_deinit(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400897
898 dev = priv->dev;
Dan Williams954ee162007-08-20 11:43:25 -0400899
David Woodhouse75bf45a2008-05-20 13:32:45 +0100900 cancel_work_sync(&priv->mcast_work);
Dan Williams71b35f32008-09-08 16:34:40 -0400901
902 /* worker thread destruction blocks on the in-flight command which
903 * should have been cleared already in lbs_stop_card().
904 */
905 lbs_deb_main("destroying worker thread\n");
Dan Williams954ee162007-08-20 11:43:25 -0400906 destroy_workqueue(priv->work_thread);
Dan Williams71b35f32008-09-08 16:34:40 -0400907 lbs_deb_main("done destroying worker thread\n");
Dan Williams954ee162007-08-20 11:43:25 -0400908
David Woodhouseaa21c002007-12-08 20:04:36 +0000909 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
910 priv->psmode = LBS802_11POWERMODECAM;
Dan Williams0bb64082010-07-27 13:08:08 -0700911 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200912 }
913
Amitkumar Karwar49125452009-09-30 20:04:38 -0700914 if (priv->is_deep_sleep) {
915 priv->is_deep_sleep = 0;
916 wake_up_interruptible(&priv->ds_awake_q);
917 }
918
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700919 priv->is_host_sleep_configured = 0;
920 priv->is_host_sleep_activated = 0;
921 wake_up_interruptible(&priv->host_sleep_q);
922
Dan Williams954ee162007-08-20 11:43:25 -0400923 /* Stop the thread servicing the interrupts */
David Woodhouseaa21c002007-12-08 20:04:36 +0000924 priv->surpriseremoved = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400925 kthread_stop(priv->main_thread);
926
Holger Schurig10078322007-11-15 18:05:47 -0500927 lbs_free_adapter(priv);
Holger Schurigff9fc792009-10-06 16:31:54 +0200928 lbs_cfg_free(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400929 free_netdev(dev);
930
931 lbs_deb_leave(LBS_DEB_MAIN);
Dan Williams954ee162007-08-20 11:43:25 -0400932}
Holger Schurig10078322007-11-15 18:05:47 -0500933EXPORT_SYMBOL_GPL(lbs_remove_card);
Dan Williams954ee162007-08-20 11:43:25 -0400934
935
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530936int lbs_rtap_supported(struct lbs_private *priv)
Holger Schurigcd744682009-12-02 15:25:59 +0100937{
938 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
939 return 1;
940
941 /* newer firmware use a capability mask */
942 return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
943 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
944}
945
946
Holger Schurig69f90322007-11-23 15:43:44 +0100947int lbs_start_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -0400948{
949 struct net_device *dev = priv->dev;
950 int ret = -1;
951
952 lbs_deb_enter(LBS_DEB_MAIN);
953
954 /* poke the firmware */
Holger Schurig10078322007-11-15 18:05:47 -0500955 ret = lbs_setup_firmware(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400956 if (ret)
957 goto done;
958
Holger Schurigff9fc792009-10-06 16:31:54 +0200959 if (lbs_cfg_register(priv)) {
960 lbs_pr_err("cannot register device\n");
Dan Williams954ee162007-08-20 11:43:25 -0400961 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200962 }
David Woodhouse020f3d02007-12-12 23:29:13 -0500963
David Woodhouse020f3d02007-12-12 23:29:13 -0500964 lbs_update_channel(priv);
David Woodhouse020f3d02007-12-12 23:29:13 -0500965
Holger Schurigcd744682009-12-02 15:25:59 +0100966 lbs_init_mesh(priv);
967
Holger Schurig10078322007-11-15 18:05:47 -0500968 lbs_debugfs_init_one(priv, dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200969
Dan Williams954ee162007-08-20 11:43:25 -0400970 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200971
Dan Williams954ee162007-08-20 11:43:25 -0400972 ret = 0;
973
Holger Schurig32a74b72007-05-25 12:04:31 -0400974done:
Dan Williams954ee162007-08-20 11:43:25 -0400975 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
Holger Schurig32a74b72007-05-25 12:04:31 -0400976 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200977}
Holger Schurig10078322007-11-15 18:05:47 -0500978EXPORT_SYMBOL_GPL(lbs_start_card);
Dan Williams954ee162007-08-20 11:43:25 -0400979
980
Holger Schuriga63e5cb2008-04-30 10:50:39 +0200981void lbs_stop_card(struct lbs_private *priv)
Dan Williams954ee162007-08-20 11:43:25 -0400982{
Julia Lawall43baa5b2009-01-09 10:23:10 +0000983 struct net_device *dev;
Dan Williams954ee162007-08-20 11:43:25 -0400984 struct cmd_ctrl_node *cmdnode;
985 unsigned long flags;
986
987 lbs_deb_enter(LBS_DEB_MAIN);
988
Holger Schurig652e3f22008-04-30 10:51:15 +0200989 if (!priv)
990 goto out;
Julia Lawall43baa5b2009-01-09 10:23:10 +0000991 dev = priv->dev;
Holger Schurig652e3f22008-04-30 10:51:15 +0200992
Julia Lawall43baa5b2009-01-09 10:23:10 +0000993 netif_stop_queue(dev);
994 netif_carrier_off(dev);
Dan Williams954ee162007-08-20 11:43:25 -0400995
Holger Schurig10078322007-11-15 18:05:47 -0500996 lbs_debugfs_remove_one(priv);
Holger Schurigcd744682009-12-02 15:25:59 +0100997 lbs_deinit_mesh(priv);
998
Dan Williams71b35f32008-09-08 16:34:40 -0400999 /* Delete the timeout of the currently processing command */
Holger Schurig652e3f22008-04-30 10:51:15 +02001000 del_timer_sync(&priv->command_timer);
Amitkumar Karwar49125452009-09-30 20:04:38 -07001001 del_timer_sync(&priv->auto_deepsleep_timer);
Dan Williams71b35f32008-09-08 16:34:40 -04001002
1003 /* Flush pending command nodes */
David Woodhouseaa21c002007-12-08 20:04:36 +00001004 spin_lock_irqsave(&priv->driver_lock, flags);
Dan Williams71b35f32008-09-08 16:34:40 -04001005 lbs_deb_main("clearing pending commands\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001006 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
David Woodhouseae125bf2007-12-15 04:22:52 -05001007 cmdnode->result = -ENOENT;
Dan Williams954ee162007-08-20 11:43:25 -04001008 cmdnode->cmdwaitqwoken = 1;
1009 wake_up_interruptible(&cmdnode->cmdwait_q);
1010 }
Dan Williams71b35f32008-09-08 16:34:40 -04001011
1012 /* Flush the command the card is currently processing */
1013 if (priv->cur_cmd) {
1014 lbs_deb_main("clearing current command\n");
1015 priv->cur_cmd->result = -ENOENT;
1016 priv->cur_cmd->cmdwaitqwoken = 1;
1017 wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
1018 }
1019 lbs_deb_main("done clearing commands\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001020 spin_unlock_irqrestore(&priv->driver_lock, flags);
Dan Williams954ee162007-08-20 11:43:25 -04001021
1022 unregister_netdev(dev);
1023
Holger Schurig652e3f22008-04-30 10:51:15 +02001024out:
Holger Schuriga63e5cb2008-04-30 10:50:39 +02001025 lbs_deb_leave(LBS_DEB_MAIN);
Dan Williams954ee162007-08-20 11:43:25 -04001026}
Holger Schurig10078322007-11-15 18:05:47 -05001027EXPORT_SYMBOL_GPL(lbs_stop_card);
Holger Schurig084708b2007-05-25 12:37:58 -04001028
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029
Holger Schurig7919b892008-04-01 14:50:43 +02001030void lbs_queue_event(struct lbs_private *priv, u32 event)
1031{
1032 unsigned long flags;
1033
1034 lbs_deb_enter(LBS_DEB_THREAD);
1035 spin_lock_irqsave(&priv->driver_lock, flags);
1036
1037 if (priv->psstate == PS_STATE_SLEEP)
1038 priv->psstate = PS_STATE_AWAKE;
1039
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001040 kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
Holger Schurig7919b892008-04-01 14:50:43 +02001041
1042 wake_up_interruptible(&priv->waitq);
1043
1044 spin_unlock_irqrestore(&priv->driver_lock, flags);
1045 lbs_deb_leave(LBS_DEB_THREAD);
1046}
1047EXPORT_SYMBOL_GPL(lbs_queue_event);
1048
1049void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050{
Holger Schuriged37b512007-05-25 11:52:42 -04001051 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001052
David Woodhouse4f679492007-12-10 14:58:37 -05001053 if (priv->psstate == PS_STATE_SLEEP)
David Woodhouseaa21c002007-12-08 20:04:36 +00001054 priv->psstate = PS_STATE_AWAKE;
Holger Schurig7919b892008-04-01 14:50:43 +02001055
1056 /* Swap buffers by flipping the response index */
1057 BUG_ON(resp_idx > 1);
1058 priv->resp_idx = resp_idx;
1059
Dan Williamsfe336152007-08-02 11:32:25 -04001060 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001061
Holger Schuriged37b512007-05-25 11:52:42 -04001062 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001063}
Holger Schurig7919b892008-04-01 14:50:43 +02001064EXPORT_SYMBOL_GPL(lbs_notify_command_response);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001065
Dan Williams72f7a662010-08-07 21:14:33 -05001066/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001067 * lbs_get_firmware - Retrieves two-stage firmware
Dan Williams72f7a662010-08-07 21:14:33 -05001068 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001069 * @dev: A pointer to &device structure
1070 * @user_helper: User-defined helper firmware file
1071 * @user_mainfw: User-defined main firmware file
1072 * @card_model: Bus-specific card model ID used to filter firmware table
1073 * elements
1074 * @fw_table: Table of firmware file names and device model numbers
1075 * terminated by an entry with a NULL helper name
1076 * @helper: On success, the helper firmware; caller must free
1077 * @mainfw: On success, the main firmware; caller must free
Dan Williams72f7a662010-08-07 21:14:33 -05001078 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001079 * returns: 0 on success, non-zero on failure
Dan Williams72f7a662010-08-07 21:14:33 -05001080 */
1081int lbs_get_firmware(struct device *dev, const char *user_helper,
1082 const char *user_mainfw, u32 card_model,
1083 const struct lbs_fw_table *fw_table,
1084 const struct firmware **helper,
1085 const struct firmware **mainfw)
1086{
1087 const struct lbs_fw_table *iter;
1088 int ret;
1089
1090 BUG_ON(helper == NULL);
1091 BUG_ON(mainfw == NULL);
1092
1093 /* Try user-specified firmware first */
1094 if (user_helper) {
1095 ret = request_firmware(helper, user_helper, dev);
1096 if (ret) {
1097 lbs_pr_err("couldn't find helper firmware %s",
1098 user_helper);
1099 goto fail;
1100 }
1101 }
1102 if (user_mainfw) {
1103 ret = request_firmware(mainfw, user_mainfw, dev);
1104 if (ret) {
1105 lbs_pr_err("couldn't find main firmware %s",
1106 user_mainfw);
1107 goto fail;
1108 }
1109 }
1110
1111 if (*helper && *mainfw)
1112 return 0;
1113
1114 /* Otherwise search for firmware to use. If neither the helper or
1115 * the main firmware were specified by the user, then we need to
1116 * make sure that found helper & main are from the same entry in
1117 * fw_table.
1118 */
1119 iter = fw_table;
1120 while (iter && iter->helper) {
1121 if (iter->model != card_model)
1122 goto next;
1123
1124 if (*helper == NULL) {
1125 ret = request_firmware(helper, iter->helper, dev);
1126 if (ret)
1127 goto next;
1128
1129 /* If the device has one-stage firmware (ie cf8305) and
1130 * we've got it then we don't need to bother with the
1131 * main firmware.
1132 */
1133 if (iter->fwname == NULL)
1134 return 0;
1135 }
1136
1137 if (*mainfw == NULL) {
1138 ret = request_firmware(mainfw, iter->fwname, dev);
1139 if (ret && !user_helper) {
1140 /* Clear the helper if it wasn't user-specified
1141 * and the main firmware load failed, to ensure
1142 * we don't have mismatched firmware pairs.
1143 */
1144 release_firmware(*helper);
1145 *helper = NULL;
1146 }
1147 }
1148
1149 if (*helper && *mainfw)
1150 return 0;
1151
1152 next:
1153 iter++;
1154 }
1155
1156 fail:
1157 /* Failed */
1158 if (*helper) {
1159 release_firmware(*helper);
1160 *helper = NULL;
1161 }
1162 if (*mainfw) {
1163 release_firmware(*mainfw);
1164 *mainfw = NULL;
1165 }
1166
1167 return -ENOENT;
1168}
1169EXPORT_SYMBOL_GPL(lbs_get_firmware);
1170
Andres Salomon4fb910f2007-11-20 17:43:45 -05001171static int __init lbs_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001172{
Holger Schurig9012b282007-05-25 11:27:16 -04001173 lbs_deb_enter(LBS_DEB_MAIN);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001174 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1175 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1176 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
Dan Williams0bb64082010-07-27 13:08:08 -07001177 confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
Holger Schurig10078322007-11-15 18:05:47 -05001178 lbs_debugfs_init();
Holger Schurig084708b2007-05-25 12:37:58 -04001179 lbs_deb_leave(LBS_DEB_MAIN);
1180 return 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001181}
1182
Andres Salomon4fb910f2007-11-20 17:43:45 -05001183static void __exit lbs_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001184{
Holger Schurig9012b282007-05-25 11:27:16 -04001185 lbs_deb_enter(LBS_DEB_MAIN);
Holger Schurig10078322007-11-15 18:05:47 -05001186 lbs_debugfs_remove();
Holger Schurig9012b282007-05-25 11:27:16 -04001187 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001188}
1189
Holger Schurig10078322007-11-15 18:05:47 -05001190module_init(lbs_init_module);
1191module_exit(lbs_exit_module);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192
Holger Schurig084708b2007-05-25 12:37:58 -04001193MODULE_DESCRIPTION("Libertas WLAN Driver Library");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001194MODULE_AUTHOR("Marvell International Ltd.");
1195MODULE_LICENSE("GPL");