main.c 28 KB

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