main.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. /*
  2. * This file contains the major functions in WLAN
  3. * driver. It includes init, exit, open, close and main
  4. * thread etc..
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/moduleparam.h>
  8. #include <linux/delay.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/kthread.h>
  14. #include <linux/kfifo.h>
  15. #include <linux/slab.h>
  16. #include <net/cfg80211.h>
  17. #include "host.h"
  18. #include "decl.h"
  19. #include "dev.h"
  20. #include "cfg.h"
  21. #include "debugfs.h"
  22. #include "cmd.h"
  23. #include "mesh.h"
  24. #define DRIVER_RELEASE_VERSION "323.p0"
  25. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  26. #ifdef DEBUG
  27. "-dbg"
  28. #endif
  29. "";
  30. /* Module parameters */
  31. unsigned int lbs_debug;
  32. EXPORT_SYMBOL_GPL(lbs_debug);
  33. module_param_named(libertas_debug, lbs_debug, int, 0644);
  34. unsigned int lbs_disablemesh;
  35. EXPORT_SYMBOL_GPL(lbs_disablemesh);
  36. module_param_named(libertas_disablemesh, lbs_disablemesh, int, 0644);
  37. /*
  38. * This global structure is used to send the confirm_sleep command as
  39. * fast as possible down to the firmware.
  40. */
  41. struct cmd_confirm_sleep confirm_sleep;
  42. /*
  43. * the table to keep region code
  44. */
  45. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  46. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  47. /*
  48. * FW rate table. FW refers to rates by their index in this table, not by the
  49. * rate value itself. Values of 0x00 are
  50. * reserved positions.
  51. */
  52. static u8 fw_data_rates[MAX_RATES] =
  53. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  54. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  55. };
  56. /**
  57. * lbs_fw_index_to_data_rate - use index to get the data rate
  58. *
  59. * @idx: The index of data rate
  60. * returns: data rate or 0
  61. */
  62. u32 lbs_fw_index_to_data_rate(u8 idx)
  63. {
  64. if (idx >= sizeof(fw_data_rates))
  65. idx = 0;
  66. return fw_data_rates[idx];
  67. }
  68. /**
  69. * lbs_data_rate_to_fw_index - use rate to get the index
  70. *
  71. * @rate: data rate
  72. * returns: index or 0
  73. */
  74. u8 lbs_data_rate_to_fw_index(u32 rate)
  75. {
  76. u8 i;
  77. if (!rate)
  78. return 0;
  79. for (i = 0; i < sizeof(fw_data_rates); i++) {
  80. if (rate == fw_data_rates[i])
  81. return i;
  82. }
  83. return 0;
  84. }
  85. int lbs_start_iface(struct lbs_private *priv)
  86. {
  87. struct cmd_ds_802_11_mac_address cmd;
  88. int ret;
  89. if (priv->power_restore) {
  90. ret = priv->power_restore(priv);
  91. if (ret)
  92. return ret;
  93. }
  94. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  95. cmd.action = cpu_to_le16(CMD_ACT_SET);
  96. memcpy(cmd.macadd, priv->current_addr, ETH_ALEN);
  97. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  98. if (ret) {
  99. lbs_deb_net("set MAC address failed\n");
  100. goto err;
  101. }
  102. lbs_update_channel(priv);
  103. priv->iface_running = true;
  104. return 0;
  105. err:
  106. if (priv->power_save)
  107. priv->power_save(priv);
  108. return ret;
  109. }
  110. /**
  111. * lbs_dev_open - open the ethX interface
  112. *
  113. * @dev: A pointer to &net_device structure
  114. * returns: 0 or -EBUSY if monitor mode active
  115. */
  116. static int lbs_dev_open(struct net_device *dev)
  117. {
  118. struct lbs_private *priv = dev->ml_priv;
  119. int ret = 0;
  120. lbs_deb_enter(LBS_DEB_NET);
  121. if (!priv->iface_running) {
  122. ret = lbs_start_iface(priv);
  123. if (ret)
  124. goto out;
  125. }
  126. spin_lock_irq(&priv->driver_lock);
  127. netif_carrier_off(dev);
  128. if (!priv->tx_pending_len)
  129. netif_wake_queue(dev);
  130. spin_unlock_irq(&priv->driver_lock);
  131. out:
  132. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  133. return ret;
  134. }
  135. static bool lbs_command_queue_empty(struct lbs_private *priv)
  136. {
  137. unsigned long flags;
  138. bool ret;
  139. spin_lock_irqsave(&priv->driver_lock, flags);
  140. ret = priv->cur_cmd == NULL && list_empty(&priv->cmdpendingq);
  141. spin_unlock_irqrestore(&priv->driver_lock, flags);
  142. return ret;
  143. }
  144. int lbs_stop_iface(struct lbs_private *priv)
  145. {
  146. unsigned long flags;
  147. int ret = 0;
  148. lbs_deb_enter(LBS_DEB_MAIN);
  149. spin_lock_irqsave(&priv->driver_lock, flags);
  150. priv->iface_running = false;
  151. kfree_skb(priv->currenttxskb);
  152. priv->currenttxskb = NULL;
  153. priv->tx_pending_len = 0;
  154. spin_unlock_irqrestore(&priv->driver_lock, flags);
  155. cancel_work_sync(&priv->mcast_work);
  156. /* Disable command processing, and wait for all commands to complete */
  157. lbs_deb_main("waiting for commands to complete\n");
  158. wait_event(priv->waitq, lbs_command_queue_empty(priv));
  159. lbs_deb_main("all commands completed\n");
  160. if (priv->power_save)
  161. ret = priv->power_save(priv);
  162. lbs_deb_leave(LBS_DEB_MAIN);
  163. return ret;
  164. }
  165. /**
  166. * lbs_eth_stop - close the ethX interface
  167. *
  168. * @dev: A pointer to &net_device structure
  169. * returns: 0
  170. */
  171. static int lbs_eth_stop(struct net_device *dev)
  172. {
  173. struct lbs_private *priv = dev->ml_priv;
  174. lbs_deb_enter(LBS_DEB_NET);
  175. if (priv->connect_status == LBS_CONNECTED)
  176. lbs_disconnect(priv, WLAN_REASON_DEAUTH_LEAVING);
  177. spin_lock_irq(&priv->driver_lock);
  178. netif_stop_queue(dev);
  179. spin_unlock_irq(&priv->driver_lock);
  180. lbs_update_mcast(priv);
  181. cancel_delayed_work_sync(&priv->scan_work);
  182. if (priv->scan_req) {
  183. cfg80211_scan_done(priv->scan_req, false);
  184. priv->scan_req = NULL;
  185. }
  186. netif_carrier_off(priv->dev);
  187. if (!lbs_iface_active(priv))
  188. lbs_stop_iface(priv);
  189. lbs_deb_leave(LBS_DEB_NET);
  190. return 0;
  191. }
  192. void lbs_host_to_card_done(struct lbs_private *priv)
  193. {
  194. unsigned long flags;
  195. lbs_deb_enter(LBS_DEB_THREAD);
  196. spin_lock_irqsave(&priv->driver_lock, flags);
  197. priv->dnld_sent = DNLD_RES_RECEIVED;
  198. /* Wake main thread if commands are pending */
  199. if (!priv->cur_cmd || priv->tx_pending_len > 0) {
  200. if (!priv->wakeup_dev_required)
  201. wake_up(&priv->waitq);
  202. }
  203. spin_unlock_irqrestore(&priv->driver_lock, flags);
  204. lbs_deb_leave(LBS_DEB_THREAD);
  205. }
  206. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  207. int lbs_set_mac_address(struct net_device *dev, void *addr)
  208. {
  209. int ret = 0;
  210. struct lbs_private *priv = dev->ml_priv;
  211. struct sockaddr *phwaddr = addr;
  212. lbs_deb_enter(LBS_DEB_NET);
  213. /*
  214. * Can only set MAC address when all interfaces are down, to be written
  215. * to the hardware when one of them is brought up.
  216. */
  217. if (lbs_iface_active(priv))
  218. return -EBUSY;
  219. /* In case it was called from the mesh device */
  220. dev = priv->dev;
  221. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  222. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  223. if (priv->mesh_dev)
  224. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  225. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  226. return ret;
  227. }
  228. static 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--;
  236. }
  237. return 0;
  238. }
  239. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  240. struct net_device *dev, int nr_addrs)
  241. {
  242. int i = nr_addrs;
  243. struct netdev_hw_addr *ha;
  244. int cnt;
  245. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  246. return nr_addrs;
  247. netif_addr_lock_bh(dev);
  248. cnt = netdev_mc_count(dev);
  249. netdev_for_each_mc_addr(ha, dev) {
  250. if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
  251. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  252. ha->addr);
  253. cnt--;
  254. continue;
  255. }
  256. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  257. break;
  258. memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
  259. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  260. ha->addr);
  261. i++;
  262. cnt--;
  263. }
  264. netif_addr_unlock_bh(dev);
  265. if (cnt)
  266. return -EOVERFLOW;
  267. return i;
  268. }
  269. void lbs_update_mcast(struct lbs_private *priv)
  270. {
  271. struct cmd_ds_mac_multicast_adr mcast_cmd;
  272. int dev_flags = 0;
  273. int nr_addrs;
  274. int old_mac_control = priv->mac_control;
  275. lbs_deb_enter(LBS_DEB_NET);
  276. if (netif_running(priv->dev))
  277. dev_flags |= priv->dev->flags;
  278. if (priv->mesh_dev && netif_running(priv->mesh_dev))
  279. dev_flags |= priv->mesh_dev->flags;
  280. if (dev_flags & IFF_PROMISC) {
  281. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  282. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  283. CMD_ACT_MAC_MULTICAST_ENABLE);
  284. goto out_set_mac_control;
  285. } else if (dev_flags & IFF_ALLMULTI) {
  286. do_allmulti:
  287. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  288. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  289. CMD_ACT_MAC_MULTICAST_ENABLE);
  290. goto out_set_mac_control;
  291. }
  292. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  293. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  294. if (nr_addrs >= 0 && priv->mesh_dev)
  295. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  296. if (nr_addrs < 0)
  297. goto do_allmulti;
  298. if (nr_addrs) {
  299. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  300. maclist[6*nr_addrs]);
  301. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  302. mcast_cmd.hdr.size = cpu_to_le16(size);
  303. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  304. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  305. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  306. } else
  307. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  308. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  309. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  310. out_set_mac_control:
  311. if (priv->mac_control != old_mac_control)
  312. lbs_set_mac_control(priv);
  313. lbs_deb_leave(LBS_DEB_NET);
  314. }
  315. static void lbs_set_mcast_worker(struct work_struct *work)
  316. {
  317. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  318. lbs_update_mcast(priv);
  319. }
  320. void lbs_set_multicast_list(struct net_device *dev)
  321. {
  322. struct lbs_private *priv = dev->ml_priv;
  323. schedule_work(&priv->mcast_work);
  324. }
  325. /**
  326. * lbs_thread - handles the major jobs in the LBS driver.
  327. * It handles all events generated by firmware, RX data received
  328. * from firmware and TX data sent from kernel.
  329. *
  330. * @data: A pointer to &lbs_thread structure
  331. * returns: 0
  332. */
  333. static int lbs_thread(void *data)
  334. {
  335. struct net_device *dev = data;
  336. struct lbs_private *priv = dev->ml_priv;
  337. wait_queue_t wait;
  338. lbs_deb_enter(LBS_DEB_THREAD);
  339. init_waitqueue_entry(&wait, current);
  340. for (;;) {
  341. int shouldsleep;
  342. u8 resp_idx;
  343. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  344. priv->currenttxskb, priv->dnld_sent);
  345. add_wait_queue(&priv->waitq, &wait);
  346. set_current_state(TASK_INTERRUPTIBLE);
  347. spin_lock_irq(&priv->driver_lock);
  348. if (kthread_should_stop())
  349. shouldsleep = 0; /* Bye */
  350. else if (priv->surpriseremoved)
  351. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  352. else if (priv->psstate == PS_STATE_SLEEP)
  353. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  354. else if (priv->cmd_timed_out)
  355. shouldsleep = 0; /* Command timed out. Recover */
  356. else if (!priv->fw_ready)
  357. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  358. else if (priv->dnld_sent)
  359. shouldsleep = 1; /* Something is en route to the device already */
  360. else if (priv->tx_pending_len > 0)
  361. shouldsleep = 0; /* We've a packet to send */
  362. else if (priv->resp_len[priv->resp_idx])
  363. shouldsleep = 0; /* We have a command response */
  364. else if (priv->cur_cmd)
  365. shouldsleep = 1; /* Can't send a command; one already running */
  366. else if (!list_empty(&priv->cmdpendingq) &&
  367. !(priv->wakeup_dev_required))
  368. shouldsleep = 0; /* We have a command to send */
  369. else if (kfifo_len(&priv->event_fifo))
  370. shouldsleep = 0; /* We have an event to process */
  371. else
  372. shouldsleep = 1; /* No command */
  373. if (shouldsleep) {
  374. lbs_deb_thread("sleeping, connect_status %d, "
  375. "psmode %d, psstate %d\n",
  376. priv->connect_status,
  377. priv->psmode, priv->psstate);
  378. spin_unlock_irq(&priv->driver_lock);
  379. schedule();
  380. } else
  381. spin_unlock_irq(&priv->driver_lock);
  382. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  383. priv->currenttxskb, priv->dnld_sent);
  384. set_current_state(TASK_RUNNING);
  385. remove_wait_queue(&priv->waitq, &wait);
  386. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  387. priv->currenttxskb, priv->dnld_sent);
  388. if (kthread_should_stop()) {
  389. lbs_deb_thread("break from main thread\n");
  390. break;
  391. }
  392. if (priv->surpriseremoved) {
  393. lbs_deb_thread("adapter removed; waiting to die...\n");
  394. continue;
  395. }
  396. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  397. priv->currenttxskb, priv->dnld_sent);
  398. /* Process any pending command response */
  399. spin_lock_irq(&priv->driver_lock);
  400. resp_idx = priv->resp_idx;
  401. if (priv->resp_len[resp_idx]) {
  402. spin_unlock_irq(&priv->driver_lock);
  403. lbs_process_command_response(priv,
  404. priv->resp_buf[resp_idx],
  405. priv->resp_len[resp_idx]);
  406. spin_lock_irq(&priv->driver_lock);
  407. priv->resp_len[resp_idx] = 0;
  408. }
  409. spin_unlock_irq(&priv->driver_lock);
  410. /* Process hardware events, e.g. card removed, link lost */
  411. spin_lock_irq(&priv->driver_lock);
  412. while (kfifo_len(&priv->event_fifo)) {
  413. u32 event;
  414. if (kfifo_out(&priv->event_fifo,
  415. (unsigned char *) &event, sizeof(event)) !=
  416. sizeof(event))
  417. break;
  418. spin_unlock_irq(&priv->driver_lock);
  419. lbs_process_event(priv, event);
  420. spin_lock_irq(&priv->driver_lock);
  421. }
  422. spin_unlock_irq(&priv->driver_lock);
  423. if (priv->wakeup_dev_required) {
  424. lbs_deb_thread("Waking up device...\n");
  425. /* Wake up device */
  426. if (priv->exit_deep_sleep(priv))
  427. lbs_deb_thread("Wakeup device failed\n");
  428. continue;
  429. }
  430. /* command timeout stuff */
  431. if (priv->cmd_timed_out && priv->cur_cmd) {
  432. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  433. netdev_info(dev, "Timeout submitting command 0x%04x\n",
  434. le16_to_cpu(cmdnode->cmdbuf->command));
  435. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  436. if (priv->reset_card)
  437. priv->reset_card(priv);
  438. }
  439. priv->cmd_timed_out = 0;
  440. if (!priv->fw_ready)
  441. continue;
  442. /* Check if we need to confirm Sleep Request received previously */
  443. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  444. !priv->dnld_sent && !priv->cur_cmd) {
  445. if (priv->connect_status == LBS_CONNECTED) {
  446. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  447. "dnld_sent %d, cur_cmd %p\n",
  448. priv->currenttxskb, priv->dnld_sent,
  449. priv->cur_cmd);
  450. lbs_ps_confirm_sleep(priv);
  451. } else {
  452. /* workaround for firmware sending
  453. * deauth/linkloss event immediately
  454. * after sleep request; remove this
  455. * after firmware fixes it
  456. */
  457. priv->psstate = PS_STATE_AWAKE;
  458. netdev_alert(dev,
  459. "ignore PS_SleepConfirm in non-connected state\n");
  460. }
  461. }
  462. /* The PS state is changed during processing of Sleep Request
  463. * event above
  464. */
  465. if ((priv->psstate == PS_STATE_SLEEP) ||
  466. (priv->psstate == PS_STATE_PRE_SLEEP))
  467. continue;
  468. if (priv->is_deep_sleep)
  469. continue;
  470. /* Execute the next command */
  471. if (!priv->dnld_sent && !priv->cur_cmd)
  472. lbs_execute_next_command(priv);
  473. spin_lock_irq(&priv->driver_lock);
  474. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  475. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  476. priv->tx_pending_buf,
  477. priv->tx_pending_len);
  478. if (ret) {
  479. lbs_deb_tx("host_to_card failed %d\n", ret);
  480. priv->dnld_sent = DNLD_RES_RECEIVED;
  481. }
  482. priv->tx_pending_len = 0;
  483. if (!priv->currenttxskb) {
  484. /* We can wake the queues immediately if we aren't
  485. waiting for TX feedback */
  486. if (priv->connect_status == LBS_CONNECTED)
  487. netif_wake_queue(priv->dev);
  488. if (priv->mesh_dev &&
  489. netif_running(priv->mesh_dev))
  490. netif_wake_queue(priv->mesh_dev);
  491. }
  492. }
  493. spin_unlock_irq(&priv->driver_lock);
  494. }
  495. del_timer(&priv->command_timer);
  496. del_timer(&priv->auto_deepsleep_timer);
  497. lbs_deb_leave(LBS_DEB_THREAD);
  498. return 0;
  499. }
  500. /**
  501. * lbs_setup_firmware - gets the HW spec from the firmware and sets
  502. * some basic parameters
  503. *
  504. * @priv: A pointer to &struct lbs_private structure
  505. * returns: 0 or -1
  506. */
  507. static int lbs_setup_firmware(struct lbs_private *priv)
  508. {
  509. int ret = -1;
  510. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  511. lbs_deb_enter(LBS_DEB_FW);
  512. /* Read MAC address from firmware */
  513. memset(priv->current_addr, 0xff, ETH_ALEN);
  514. ret = lbs_update_hw_spec(priv);
  515. if (ret)
  516. goto done;
  517. /* Read power levels if available */
  518. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  519. if (ret == 0) {
  520. priv->txpower_cur = curlevel;
  521. priv->txpower_min = minlevel;
  522. priv->txpower_max = maxlevel;
  523. }
  524. /* Send cmd to FW to enable 11D function */
  525. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
  526. lbs_set_mac_control(priv);
  527. done:
  528. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  529. return ret;
  530. }
  531. int lbs_suspend(struct lbs_private *priv)
  532. {
  533. int ret;
  534. lbs_deb_enter(LBS_DEB_FW);
  535. if (priv->is_deep_sleep) {
  536. ret = lbs_set_deep_sleep(priv, 0);
  537. if (ret) {
  538. netdev_err(priv->dev,
  539. "deep sleep cancellation failed: %d\n", ret);
  540. return ret;
  541. }
  542. priv->deep_sleep_required = 1;
  543. }
  544. ret = lbs_set_host_sleep(priv, 1);
  545. netif_device_detach(priv->dev);
  546. if (priv->mesh_dev)
  547. netif_device_detach(priv->mesh_dev);
  548. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  549. return ret;
  550. }
  551. EXPORT_SYMBOL_GPL(lbs_suspend);
  552. int lbs_resume(struct lbs_private *priv)
  553. {
  554. int ret;
  555. lbs_deb_enter(LBS_DEB_FW);
  556. ret = lbs_set_host_sleep(priv, 0);
  557. netif_device_attach(priv->dev);
  558. if (priv->mesh_dev)
  559. netif_device_attach(priv->mesh_dev);
  560. if (priv->deep_sleep_required) {
  561. priv->deep_sleep_required = 0;
  562. ret = lbs_set_deep_sleep(priv, 1);
  563. if (ret)
  564. netdev_err(priv->dev,
  565. "deep sleep activation failed: %d\n", ret);
  566. }
  567. if (priv->setup_fw_on_resume)
  568. ret = lbs_setup_firmware(priv);
  569. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  570. return ret;
  571. }
  572. EXPORT_SYMBOL_GPL(lbs_resume);
  573. /**
  574. * lbs_cmd_timeout_handler - handles the timeout of command sending.
  575. * It will re-send the same command again.
  576. *
  577. * @data: &struct lbs_private pointer
  578. */
  579. static void lbs_cmd_timeout_handler(unsigned long data)
  580. {
  581. struct lbs_private *priv = (struct lbs_private *)data;
  582. unsigned long flags;
  583. lbs_deb_enter(LBS_DEB_CMD);
  584. spin_lock_irqsave(&priv->driver_lock, flags);
  585. if (!priv->cur_cmd)
  586. goto out;
  587. netdev_info(priv->dev, "command 0x%04x timed out\n",
  588. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  589. priv->cmd_timed_out = 1;
  590. /*
  591. * If the device didn't even acknowledge the command, reset the state
  592. * so that we don't block all future commands due to this one timeout.
  593. */
  594. if (priv->dnld_sent == DNLD_CMD_SENT)
  595. priv->dnld_sent = DNLD_RES_RECEIVED;
  596. wake_up(&priv->waitq);
  597. out:
  598. spin_unlock_irqrestore(&priv->driver_lock, flags);
  599. lbs_deb_leave(LBS_DEB_CMD);
  600. }
  601. /**
  602. * auto_deepsleep_timer_fn - put the device back to deep sleep mode when
  603. * timer expires and no activity (command, event, data etc.) is detected.
  604. * @data: &struct lbs_private pointer
  605. * returns: N/A
  606. */
  607. static void auto_deepsleep_timer_fn(unsigned long data)
  608. {
  609. struct lbs_private *priv = (struct lbs_private *)data;
  610. lbs_deb_enter(LBS_DEB_CMD);
  611. if (priv->is_activity_detected) {
  612. priv->is_activity_detected = 0;
  613. } else {
  614. if (priv->is_auto_deep_sleep_enabled &&
  615. (!priv->wakeup_dev_required) &&
  616. (priv->connect_status != LBS_CONNECTED)) {
  617. struct cmd_header cmd;
  618. lbs_deb_main("Entering auto deep sleep mode...\n");
  619. memset(&cmd, 0, sizeof(cmd));
  620. cmd.size = cpu_to_le16(sizeof(cmd));
  621. lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
  622. sizeof(cmd));
  623. }
  624. }
  625. mod_timer(&priv->auto_deepsleep_timer , jiffies +
  626. (priv->auto_deep_sleep_timeout * HZ)/1000);
  627. lbs_deb_leave(LBS_DEB_CMD);
  628. }
  629. int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
  630. {
  631. lbs_deb_enter(LBS_DEB_SDIO);
  632. priv->is_auto_deep_sleep_enabled = 1;
  633. if (priv->is_deep_sleep)
  634. priv->wakeup_dev_required = 1;
  635. mod_timer(&priv->auto_deepsleep_timer ,
  636. jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
  637. lbs_deb_leave(LBS_DEB_SDIO);
  638. return 0;
  639. }
  640. int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
  641. {
  642. lbs_deb_enter(LBS_DEB_SDIO);
  643. priv->is_auto_deep_sleep_enabled = 0;
  644. priv->auto_deep_sleep_timeout = 0;
  645. del_timer(&priv->auto_deepsleep_timer);
  646. lbs_deb_leave(LBS_DEB_SDIO);
  647. return 0;
  648. }
  649. static int lbs_init_adapter(struct lbs_private *priv)
  650. {
  651. int ret;
  652. lbs_deb_enter(LBS_DEB_MAIN);
  653. memset(priv->current_addr, 0xff, ETH_ALEN);
  654. priv->connect_status = LBS_DISCONNECTED;
  655. priv->channel = DEFAULT_AD_HOC_CHANNEL;
  656. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  657. priv->radio_on = 1;
  658. priv->psmode = LBS802_11POWERMODECAM;
  659. priv->psstate = PS_STATE_FULL_POWER;
  660. priv->is_deep_sleep = 0;
  661. priv->is_auto_deep_sleep_enabled = 0;
  662. priv->deep_sleep_required = 0;
  663. priv->wakeup_dev_required = 0;
  664. init_waitqueue_head(&priv->ds_awake_q);
  665. init_waitqueue_head(&priv->scan_q);
  666. priv->authtype_auto = 1;
  667. priv->is_host_sleep_configured = 0;
  668. priv->is_host_sleep_activated = 0;
  669. init_waitqueue_head(&priv->host_sleep_q);
  670. mutex_init(&priv->lock);
  671. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  672. (unsigned long)priv);
  673. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  674. (unsigned long)priv);
  675. INIT_LIST_HEAD(&priv->cmdfreeq);
  676. INIT_LIST_HEAD(&priv->cmdpendingq);
  677. spin_lock_init(&priv->driver_lock);
  678. /* Allocate the command buffers */
  679. if (lbs_allocate_cmd_buffer(priv)) {
  680. pr_err("Out of memory allocating command buffers\n");
  681. ret = -ENOMEM;
  682. goto out;
  683. }
  684. priv->resp_idx = 0;
  685. priv->resp_len[0] = priv->resp_len[1] = 0;
  686. /* Create the event FIFO */
  687. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  688. if (ret) {
  689. pr_err("Out of memory allocating event FIFO buffer\n");
  690. goto out;
  691. }
  692. out:
  693. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  694. return ret;
  695. }
  696. static void lbs_free_adapter(struct lbs_private *priv)
  697. {
  698. lbs_deb_enter(LBS_DEB_MAIN);
  699. lbs_free_cmd_buffer(priv);
  700. kfifo_free(&priv->event_fifo);
  701. del_timer(&priv->command_timer);
  702. del_timer(&priv->auto_deepsleep_timer);
  703. lbs_deb_leave(LBS_DEB_MAIN);
  704. }
  705. static const struct net_device_ops lbs_netdev_ops = {
  706. .ndo_open = lbs_dev_open,
  707. .ndo_stop = lbs_eth_stop,
  708. .ndo_start_xmit = lbs_hard_start_xmit,
  709. .ndo_set_mac_address = lbs_set_mac_address,
  710. .ndo_set_multicast_list = lbs_set_multicast_list,
  711. .ndo_change_mtu = eth_change_mtu,
  712. .ndo_validate_addr = eth_validate_addr,
  713. };
  714. /**
  715. * lbs_add_card - adds the card. It will probe the
  716. * card, allocate the lbs_priv and initialize the device.
  717. *
  718. * @card: A pointer to card
  719. * @dmdev: A pointer to &struct device
  720. * returns: A pointer to &struct lbs_private structure
  721. */
  722. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  723. {
  724. struct net_device *dev;
  725. struct wireless_dev *wdev;
  726. struct lbs_private *priv = NULL;
  727. lbs_deb_enter(LBS_DEB_MAIN);
  728. /* Allocate an Ethernet device and register it */
  729. wdev = lbs_cfg_alloc(dmdev);
  730. if (IS_ERR(wdev)) {
  731. pr_err("cfg80211 init failed\n");
  732. goto done;
  733. }
  734. wdev->iftype = NL80211_IFTYPE_STATION;
  735. priv = wdev_priv(wdev);
  736. priv->wdev = wdev;
  737. if (lbs_init_adapter(priv)) {
  738. pr_err("failed to initialize adapter structure\n");
  739. goto err_wdev;
  740. }
  741. dev = alloc_netdev(0, "wlan%d", ether_setup);
  742. if (!dev) {
  743. dev_err(dmdev, "no memory for network device instance\n");
  744. goto err_adapter;
  745. }
  746. dev->ieee80211_ptr = wdev;
  747. dev->ml_priv = priv;
  748. SET_NETDEV_DEV(dev, dmdev);
  749. wdev->netdev = dev;
  750. priv->dev = dev;
  751. dev->netdev_ops = &lbs_netdev_ops;
  752. dev->watchdog_timeo = 5 * HZ;
  753. dev->ethtool_ops = &lbs_ethtool_ops;
  754. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  755. priv->card = card;
  756. strcpy(dev->name, "wlan%d");
  757. lbs_deb_thread("Starting main thread...\n");
  758. init_waitqueue_head(&priv->waitq);
  759. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  760. if (IS_ERR(priv->main_thread)) {
  761. lbs_deb_thread("Error creating main thread.\n");
  762. goto err_ndev;
  763. }
  764. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  765. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  766. priv->wol_criteria = EHS_REMOVE_WAKEUP;
  767. priv->wol_gpio = 0xff;
  768. priv->wol_gap = 20;
  769. priv->ehs_remove_supported = true;
  770. goto done;
  771. err_ndev:
  772. free_netdev(dev);
  773. err_adapter:
  774. lbs_free_adapter(priv);
  775. err_wdev:
  776. lbs_cfg_free(priv);
  777. priv = NULL;
  778. done:
  779. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  780. return priv;
  781. }
  782. EXPORT_SYMBOL_GPL(lbs_add_card);
  783. void lbs_remove_card(struct lbs_private *priv)
  784. {
  785. struct net_device *dev = priv->dev;
  786. lbs_deb_enter(LBS_DEB_MAIN);
  787. lbs_remove_mesh(priv);
  788. lbs_scan_deinit(priv);
  789. /* worker thread destruction blocks on the in-flight command which
  790. * should have been cleared already in lbs_stop_card().
  791. */
  792. lbs_deb_main("destroying worker thread\n");
  793. destroy_workqueue(priv->work_thread);
  794. lbs_deb_main("done destroying worker thread\n");
  795. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  796. priv->psmode = LBS802_11POWERMODECAM;
  797. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
  798. }
  799. if (priv->is_deep_sleep) {
  800. priv->is_deep_sleep = 0;
  801. wake_up_interruptible(&priv->ds_awake_q);
  802. }
  803. priv->is_host_sleep_configured = 0;
  804. priv->is_host_sleep_activated = 0;
  805. wake_up_interruptible(&priv->host_sleep_q);
  806. /* Stop the thread servicing the interrupts */
  807. priv->surpriseremoved = 1;
  808. kthread_stop(priv->main_thread);
  809. lbs_free_adapter(priv);
  810. lbs_cfg_free(priv);
  811. free_netdev(dev);
  812. lbs_deb_leave(LBS_DEB_MAIN);
  813. }
  814. EXPORT_SYMBOL_GPL(lbs_remove_card);
  815. int lbs_rtap_supported(struct lbs_private *priv)
  816. {
  817. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  818. return 1;
  819. /* newer firmware use a capability mask */
  820. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  821. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  822. }
  823. int lbs_start_card(struct lbs_private *priv)
  824. {
  825. struct net_device *dev = priv->dev;
  826. int ret = -1;
  827. lbs_deb_enter(LBS_DEB_MAIN);
  828. /* poke the firmware */
  829. ret = lbs_setup_firmware(priv);
  830. if (ret)
  831. goto done;
  832. if (!lbs_disablemesh)
  833. lbs_init_mesh(priv);
  834. else
  835. pr_info("%s: mesh disabled\n", dev->name);
  836. if (lbs_cfg_register(priv)) {
  837. pr_err("cannot register device\n");
  838. goto done;
  839. }
  840. if (lbs_mesh_activated(priv))
  841. lbs_start_mesh(priv);
  842. lbs_debugfs_init_one(priv, dev);
  843. netdev_info(dev, "Marvell WLAN 802.11 adapter\n");
  844. ret = 0;
  845. done:
  846. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  847. return ret;
  848. }
  849. EXPORT_SYMBOL_GPL(lbs_start_card);
  850. void lbs_stop_card(struct lbs_private *priv)
  851. {
  852. struct net_device *dev;
  853. lbs_deb_enter(LBS_DEB_MAIN);
  854. if (!priv)
  855. goto out;
  856. dev = priv->dev;
  857. netif_stop_queue(dev);
  858. netif_carrier_off(dev);
  859. lbs_debugfs_remove_one(priv);
  860. lbs_deinit_mesh(priv);
  861. unregister_netdev(dev);
  862. out:
  863. lbs_deb_leave(LBS_DEB_MAIN);
  864. }
  865. EXPORT_SYMBOL_GPL(lbs_stop_card);
  866. void lbs_queue_event(struct lbs_private *priv, u32 event)
  867. {
  868. unsigned long flags;
  869. lbs_deb_enter(LBS_DEB_THREAD);
  870. spin_lock_irqsave(&priv->driver_lock, flags);
  871. if (priv->psstate == PS_STATE_SLEEP)
  872. priv->psstate = PS_STATE_AWAKE;
  873. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  874. wake_up(&priv->waitq);
  875. spin_unlock_irqrestore(&priv->driver_lock, flags);
  876. lbs_deb_leave(LBS_DEB_THREAD);
  877. }
  878. EXPORT_SYMBOL_GPL(lbs_queue_event);
  879. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  880. {
  881. lbs_deb_enter(LBS_DEB_THREAD);
  882. if (priv->psstate == PS_STATE_SLEEP)
  883. priv->psstate = PS_STATE_AWAKE;
  884. /* Swap buffers by flipping the response index */
  885. BUG_ON(resp_idx > 1);
  886. priv->resp_idx = resp_idx;
  887. wake_up(&priv->waitq);
  888. lbs_deb_leave(LBS_DEB_THREAD);
  889. }
  890. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  891. /**
  892. * lbs_get_firmware - Retrieves two-stage firmware
  893. *
  894. * @dev: A pointer to &device structure
  895. * @user_helper: User-defined helper firmware file
  896. * @user_mainfw: User-defined main firmware file
  897. * @card_model: Bus-specific card model ID used to filter firmware table
  898. * elements
  899. * @fw_table: Table of firmware file names and device model numbers
  900. * terminated by an entry with a NULL helper name
  901. * @helper: On success, the helper firmware; caller must free
  902. * @mainfw: On success, the main firmware; caller must free
  903. *
  904. * returns: 0 on success, non-zero on failure
  905. */
  906. int lbs_get_firmware(struct device *dev, const char *user_helper,
  907. const char *user_mainfw, u32 card_model,
  908. const struct lbs_fw_table *fw_table,
  909. const struct firmware **helper,
  910. const struct firmware **mainfw)
  911. {
  912. const struct lbs_fw_table *iter;
  913. int ret;
  914. BUG_ON(helper == NULL);
  915. BUG_ON(mainfw == NULL);
  916. /* Try user-specified firmware first */
  917. if (user_helper) {
  918. ret = request_firmware(helper, user_helper, dev);
  919. if (ret) {
  920. dev_err(dev, "couldn't find helper firmware %s\n",
  921. user_helper);
  922. goto fail;
  923. }
  924. }
  925. if (user_mainfw) {
  926. ret = request_firmware(mainfw, user_mainfw, dev);
  927. if (ret) {
  928. dev_err(dev, "couldn't find main firmware %s\n",
  929. user_mainfw);
  930. goto fail;
  931. }
  932. }
  933. if (*helper && *mainfw)
  934. return 0;
  935. /* Otherwise search for firmware to use. If neither the helper or
  936. * the main firmware were specified by the user, then we need to
  937. * make sure that found helper & main are from the same entry in
  938. * fw_table.
  939. */
  940. iter = fw_table;
  941. while (iter && iter->helper) {
  942. if (iter->model != card_model)
  943. goto next;
  944. if (*helper == NULL) {
  945. ret = request_firmware(helper, iter->helper, dev);
  946. if (ret)
  947. goto next;
  948. /* If the device has one-stage firmware (ie cf8305) and
  949. * we've got it then we don't need to bother with the
  950. * main firmware.
  951. */
  952. if (iter->fwname == NULL)
  953. return 0;
  954. }
  955. if (*mainfw == NULL) {
  956. ret = request_firmware(mainfw, iter->fwname, dev);
  957. if (ret && !user_helper) {
  958. /* Clear the helper if it wasn't user-specified
  959. * and the main firmware load failed, to ensure
  960. * we don't have mismatched firmware pairs.
  961. */
  962. release_firmware(*helper);
  963. *helper = NULL;
  964. }
  965. }
  966. if (*helper && *mainfw)
  967. return 0;
  968. next:
  969. iter++;
  970. }
  971. fail:
  972. /* Failed */
  973. if (*helper) {
  974. release_firmware(*helper);
  975. *helper = NULL;
  976. }
  977. if (*mainfw) {
  978. release_firmware(*mainfw);
  979. *mainfw = NULL;
  980. }
  981. return -ENOENT;
  982. }
  983. EXPORT_SYMBOL_GPL(lbs_get_firmware);
  984. static int __init lbs_init_module(void)
  985. {
  986. lbs_deb_enter(LBS_DEB_MAIN);
  987. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  988. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  989. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  990. confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
  991. lbs_debugfs_init();
  992. lbs_deb_leave(LBS_DEB_MAIN);
  993. return 0;
  994. }
  995. static void __exit lbs_exit_module(void)
  996. {
  997. lbs_deb_enter(LBS_DEB_MAIN);
  998. lbs_debugfs_remove();
  999. lbs_deb_leave(LBS_DEB_MAIN);
  1000. }
  1001. module_init(lbs_init_module);
  1002. module_exit(lbs_exit_module);
  1003. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1004. MODULE_AUTHOR("Marvell International Ltd.");
  1005. MODULE_LICENSE("GPL");