main.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NXP Wireless LAN device driver: major functions
  4. *
  5. * Copyright 2011-2020 NXP
  6. */
  7. #include <linux/suspend.h>
  8. #include "main.h"
  9. #include "wmm.h"
  10. #include "cfg80211.h"
  11. #include "11n.h"
  12. #define VERSION "1.0"
  13. #define MFG_FIRMWARE "mwifiex_mfg.bin"
  14. static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
  15. module_param(debug_mask, uint, 0);
  16. MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
  17. const char driver_version[] = "mwifiex " VERSION " (%s) ";
  18. static char *cal_data_cfg;
  19. module_param(cal_data_cfg, charp, 0);
  20. static unsigned short driver_mode;
  21. module_param(driver_mode, ushort, 0);
  22. MODULE_PARM_DESC(driver_mode,
  23. "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
  24. bool mfg_mode;
  25. module_param(mfg_mode, bool, 0);
  26. MODULE_PARM_DESC(mfg_mode, "manufacturing mode enable:1, disable:0");
  27. bool aggr_ctrl;
  28. module_param(aggr_ctrl, bool, 0000);
  29. MODULE_PARM_DESC(aggr_ctrl, "usb tx aggregation enable:1, disable:0");
  30. const u16 mwifiex_1d_to_wmm_queue[8] = { 1, 0, 0, 1, 2, 2, 3, 3 };
  31. /*
  32. * This function registers the device and performs all the necessary
  33. * initializations.
  34. *
  35. * The following initialization operations are performed -
  36. * - Allocate adapter structure
  37. * - Save interface specific operations table in adapter
  38. * - Call interface specific initialization routine
  39. * - Allocate private structures
  40. * - Set default adapter structure parameters
  41. * - Initialize locks
  42. *
  43. * In case of any errors during inittialization, this function also ensures
  44. * proper cleanup before exiting.
  45. */
  46. static int mwifiex_register(void *card, struct device *dev,
  47. struct mwifiex_if_ops *if_ops, void **padapter)
  48. {
  49. struct mwifiex_adapter *adapter;
  50. int i;
  51. adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
  52. if (!adapter)
  53. return -ENOMEM;
  54. *padapter = adapter;
  55. adapter->dev = dev;
  56. adapter->card = card;
  57. /* Save interface specific operations in adapter */
  58. memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
  59. adapter->debug_mask = debug_mask;
  60. /* card specific initialization has been deferred until now .. */
  61. if (adapter->if_ops.init_if)
  62. if (adapter->if_ops.init_if(adapter))
  63. goto error;
  64. adapter->priv_num = 0;
  65. for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
  66. /* Allocate memory for private structure */
  67. adapter->priv[i] =
  68. kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
  69. if (!adapter->priv[i])
  70. goto error;
  71. adapter->priv[i]->adapter = adapter;
  72. adapter->priv_num++;
  73. }
  74. mwifiex_init_lock_list(adapter);
  75. timer_setup(&adapter->cmd_timer, mwifiex_cmd_timeout_func, 0);
  76. return 0;
  77. error:
  78. mwifiex_dbg(adapter, ERROR,
  79. "info: leave mwifiex_register with error\n");
  80. for (i = 0; i < adapter->priv_num; i++)
  81. kfree(adapter->priv[i]);
  82. kfree(adapter);
  83. return -1;
  84. }
  85. /*
  86. * This function unregisters the device and performs all the necessary
  87. * cleanups.
  88. *
  89. * The following cleanup operations are performed -
  90. * - Free the timers
  91. * - Free beacon buffers
  92. * - Free private structures
  93. * - Free adapter structure
  94. */
  95. static int mwifiex_unregister(struct mwifiex_adapter *adapter)
  96. {
  97. s32 i;
  98. if (adapter->if_ops.cleanup_if)
  99. adapter->if_ops.cleanup_if(adapter);
  100. del_timer_sync(&adapter->cmd_timer);
  101. /* Free private structures */
  102. for (i = 0; i < adapter->priv_num; i++) {
  103. if (adapter->priv[i]) {
  104. mwifiex_free_curr_bcn(adapter->priv[i]);
  105. kfree(adapter->priv[i]);
  106. }
  107. }
  108. if (adapter->nd_info) {
  109. for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
  110. kfree(adapter->nd_info->matches[i]);
  111. kfree(adapter->nd_info);
  112. adapter->nd_info = NULL;
  113. }
  114. kfree(adapter->regd);
  115. kfree(adapter);
  116. return 0;
  117. }
  118. void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
  119. {
  120. unsigned long flags;
  121. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  122. if (adapter->mwifiex_processing) {
  123. adapter->more_task_flag = true;
  124. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  125. } else {
  126. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  127. queue_work(adapter->workqueue, &adapter->main_work);
  128. }
  129. }
  130. EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
  131. static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
  132. {
  133. spin_lock_bh(&adapter->rx_proc_lock);
  134. if (adapter->rx_processing) {
  135. spin_unlock_bh(&adapter->rx_proc_lock);
  136. } else {
  137. spin_unlock_bh(&adapter->rx_proc_lock);
  138. queue_work(adapter->rx_workqueue, &adapter->rx_work);
  139. }
  140. }
  141. static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
  142. {
  143. struct sk_buff *skb;
  144. struct mwifiex_rxinfo *rx_info;
  145. spin_lock_bh(&adapter->rx_proc_lock);
  146. if (adapter->rx_processing || adapter->rx_locked) {
  147. spin_unlock_bh(&adapter->rx_proc_lock);
  148. goto exit_rx_proc;
  149. } else {
  150. adapter->rx_processing = true;
  151. spin_unlock_bh(&adapter->rx_proc_lock);
  152. }
  153. /* Check for Rx data */
  154. while ((skb = skb_dequeue(&adapter->rx_data_q))) {
  155. atomic_dec(&adapter->rx_pending);
  156. if ((adapter->delay_main_work ||
  157. adapter->iface_type == MWIFIEX_USB) &&
  158. (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
  159. if (adapter->if_ops.submit_rem_rx_urbs)
  160. adapter->if_ops.submit_rem_rx_urbs(adapter);
  161. adapter->delay_main_work = false;
  162. mwifiex_queue_main_work(adapter);
  163. }
  164. rx_info = MWIFIEX_SKB_RXCB(skb);
  165. if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
  166. if (adapter->if_ops.deaggr_pkt)
  167. adapter->if_ops.deaggr_pkt(adapter, skb);
  168. dev_kfree_skb_any(skb);
  169. } else {
  170. mwifiex_handle_rx_packet(adapter, skb);
  171. }
  172. }
  173. spin_lock_bh(&adapter->rx_proc_lock);
  174. adapter->rx_processing = false;
  175. spin_unlock_bh(&adapter->rx_proc_lock);
  176. exit_rx_proc:
  177. return 0;
  178. }
  179. static void maybe_quirk_fw_disable_ds(struct mwifiex_adapter *adapter)
  180. {
  181. struct mwifiex_private *priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
  182. struct mwifiex_ver_ext ver_ext;
  183. if (test_and_set_bit(MWIFIEX_IS_REQUESTING_FW_VEREXT, &adapter->work_flags))
  184. return;
  185. memset(&ver_ext, 0, sizeof(ver_ext));
  186. ver_ext.version_str_sel = 1;
  187. if (mwifiex_send_cmd(priv, HostCmd_CMD_VERSION_EXT,
  188. HostCmd_ACT_GEN_GET, 0, &ver_ext, false)) {
  189. mwifiex_dbg(priv->adapter, MSG,
  190. "Checking hardware revision failed.\n");
  191. }
  192. }
  193. /*
  194. * The main process.
  195. *
  196. * This function is the main procedure of the driver and handles various driver
  197. * operations. It runs in a loop and provides the core functionalities.
  198. *
  199. * The main responsibilities of this function are -
  200. * - Ensure concurrency control
  201. * - Handle pending interrupts and call interrupt handlers
  202. * - Wake up the card if required
  203. * - Handle command responses and call response handlers
  204. * - Handle events and call event handlers
  205. * - Execute pending commands
  206. * - Transmit pending data packets
  207. */
  208. int mwifiex_main_process(struct mwifiex_adapter *adapter)
  209. {
  210. int ret = 0;
  211. unsigned long flags;
  212. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  213. /* Check if already processing */
  214. if (adapter->mwifiex_processing || adapter->main_locked) {
  215. adapter->more_task_flag = true;
  216. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  217. return 0;
  218. } else {
  219. adapter->mwifiex_processing = true;
  220. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  221. }
  222. process_start:
  223. do {
  224. if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
  225. break;
  226. /* For non-USB interfaces, If we process interrupts first, it
  227. * would increase RX pending even further. Avoid this by
  228. * checking if rx_pending has crossed high threshold and
  229. * schedule rx work queue and then process interrupts.
  230. * For USB interface, there are no interrupts. We already have
  231. * HIGH_RX_PENDING check in usb.c
  232. */
  233. if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
  234. adapter->iface_type != MWIFIEX_USB) {
  235. adapter->delay_main_work = true;
  236. mwifiex_queue_rx_work(adapter);
  237. break;
  238. }
  239. /* Handle pending interrupt if any */
  240. if (adapter->int_status) {
  241. if (adapter->hs_activated)
  242. mwifiex_process_hs_config(adapter);
  243. if (adapter->if_ops.process_int_status)
  244. adapter->if_ops.process_int_status(adapter);
  245. }
  246. if (adapter->rx_work_enabled && adapter->data_received)
  247. mwifiex_queue_rx_work(adapter);
  248. /* Need to wake up the card ? */
  249. if ((adapter->ps_state == PS_STATE_SLEEP) &&
  250. (adapter->pm_wakeup_card_req &&
  251. !adapter->pm_wakeup_fw_try) &&
  252. (is_command_pending(adapter) ||
  253. !skb_queue_empty(&adapter->tx_data_q) ||
  254. !mwifiex_bypass_txlist_empty(adapter) ||
  255. !mwifiex_wmm_lists_empty(adapter))) {
  256. adapter->pm_wakeup_fw_try = true;
  257. mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
  258. adapter->if_ops.wakeup(adapter);
  259. continue;
  260. }
  261. if (IS_CARD_RX_RCVD(adapter)) {
  262. adapter->data_received = false;
  263. adapter->pm_wakeup_fw_try = false;
  264. del_timer(&adapter->wakeup_timer);
  265. if (adapter->ps_state == PS_STATE_SLEEP)
  266. adapter->ps_state = PS_STATE_AWAKE;
  267. } else {
  268. /* We have tried to wakeup the card already */
  269. if (adapter->pm_wakeup_fw_try)
  270. break;
  271. if (adapter->ps_state == PS_STATE_PRE_SLEEP)
  272. mwifiex_check_ps_cond(adapter);
  273. if (adapter->ps_state != PS_STATE_AWAKE)
  274. break;
  275. if (adapter->tx_lock_flag) {
  276. if (adapter->iface_type == MWIFIEX_USB) {
  277. if (!adapter->usb_mc_setup)
  278. break;
  279. } else
  280. break;
  281. }
  282. if ((!adapter->scan_chan_gap_enabled &&
  283. adapter->scan_processing) || adapter->data_sent ||
  284. mwifiex_is_tdls_chan_switching
  285. (mwifiex_get_priv(adapter,
  286. MWIFIEX_BSS_ROLE_STA)) ||
  287. (mwifiex_wmm_lists_empty(adapter) &&
  288. mwifiex_bypass_txlist_empty(adapter) &&
  289. skb_queue_empty(&adapter->tx_data_q))) {
  290. if (adapter->cmd_sent || adapter->curr_cmd ||
  291. !mwifiex_is_send_cmd_allowed
  292. (mwifiex_get_priv(adapter,
  293. MWIFIEX_BSS_ROLE_STA)) ||
  294. (!is_command_pending(adapter)))
  295. break;
  296. }
  297. }
  298. /* Check for event */
  299. if (adapter->event_received) {
  300. adapter->event_received = false;
  301. mwifiex_process_event(adapter);
  302. }
  303. /* Check for Cmd Resp */
  304. if (adapter->cmd_resp_received) {
  305. adapter->cmd_resp_received = false;
  306. mwifiex_process_cmdresp(adapter);
  307. /* call mwifiex back when init_fw is done */
  308. if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
  309. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  310. mwifiex_init_fw_complete(adapter);
  311. maybe_quirk_fw_disable_ds(adapter);
  312. }
  313. }
  314. /* Check if we need to confirm Sleep Request
  315. received previously */
  316. if (adapter->ps_state == PS_STATE_PRE_SLEEP)
  317. mwifiex_check_ps_cond(adapter);
  318. /* * The ps_state may have been changed during processing of
  319. * Sleep Request event.
  320. */
  321. if ((adapter->ps_state == PS_STATE_SLEEP) ||
  322. (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
  323. (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
  324. continue;
  325. }
  326. if (adapter->tx_lock_flag) {
  327. if (adapter->iface_type == MWIFIEX_USB) {
  328. if (!adapter->usb_mc_setup)
  329. continue;
  330. } else
  331. continue;
  332. }
  333. if (!adapter->cmd_sent && !adapter->curr_cmd &&
  334. mwifiex_is_send_cmd_allowed
  335. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
  336. if (mwifiex_exec_next_cmd(adapter) == -1) {
  337. ret = -1;
  338. break;
  339. }
  340. }
  341. /** If USB Multi channel setup ongoing,
  342. * wait for ready to tx data.
  343. */
  344. if (adapter->iface_type == MWIFIEX_USB &&
  345. adapter->usb_mc_setup)
  346. continue;
  347. if ((adapter->scan_chan_gap_enabled ||
  348. !adapter->scan_processing) &&
  349. !adapter->data_sent &&
  350. !skb_queue_empty(&adapter->tx_data_q)) {
  351. if (adapter->hs_activated_manually) {
  352. mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
  353. MWIFIEX_ASYNC_CMD);
  354. adapter->hs_activated_manually = false;
  355. }
  356. mwifiex_process_tx_queue(adapter);
  357. if (adapter->hs_activated) {
  358. clear_bit(MWIFIEX_IS_HS_CONFIGURED,
  359. &adapter->work_flags);
  360. mwifiex_hs_activated_event
  361. (mwifiex_get_priv
  362. (adapter, MWIFIEX_BSS_ROLE_ANY),
  363. false);
  364. }
  365. }
  366. if ((adapter->scan_chan_gap_enabled ||
  367. !adapter->scan_processing) &&
  368. !adapter->data_sent &&
  369. !mwifiex_bypass_txlist_empty(adapter) &&
  370. !mwifiex_is_tdls_chan_switching
  371. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
  372. if (adapter->hs_activated_manually) {
  373. mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
  374. MWIFIEX_ASYNC_CMD);
  375. adapter->hs_activated_manually = false;
  376. }
  377. mwifiex_process_bypass_tx(adapter);
  378. if (adapter->hs_activated) {
  379. clear_bit(MWIFIEX_IS_HS_CONFIGURED,
  380. &adapter->work_flags);
  381. mwifiex_hs_activated_event
  382. (mwifiex_get_priv
  383. (adapter, MWIFIEX_BSS_ROLE_ANY),
  384. false);
  385. }
  386. }
  387. if ((adapter->scan_chan_gap_enabled ||
  388. !adapter->scan_processing) &&
  389. !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
  390. !mwifiex_is_tdls_chan_switching
  391. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
  392. if (adapter->hs_activated_manually) {
  393. mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
  394. MWIFIEX_ASYNC_CMD);
  395. adapter->hs_activated_manually = false;
  396. }
  397. mwifiex_wmm_process_tx(adapter);
  398. if (adapter->hs_activated) {
  399. clear_bit(MWIFIEX_IS_HS_CONFIGURED,
  400. &adapter->work_flags);
  401. mwifiex_hs_activated_event
  402. (mwifiex_get_priv
  403. (adapter, MWIFIEX_BSS_ROLE_ANY),
  404. false);
  405. }
  406. }
  407. if (adapter->delay_null_pkt && !adapter->cmd_sent &&
  408. !adapter->curr_cmd && !is_command_pending(adapter) &&
  409. (mwifiex_wmm_lists_empty(adapter) &&
  410. mwifiex_bypass_txlist_empty(adapter) &&
  411. skb_queue_empty(&adapter->tx_data_q))) {
  412. if (!mwifiex_send_null_packet
  413. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  414. MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
  415. MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
  416. adapter->delay_null_pkt = false;
  417. adapter->ps_state = PS_STATE_SLEEP;
  418. }
  419. break;
  420. }
  421. } while (true);
  422. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  423. if (adapter->more_task_flag) {
  424. adapter->more_task_flag = false;
  425. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  426. goto process_start;
  427. }
  428. adapter->mwifiex_processing = false;
  429. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  430. return ret;
  431. }
  432. EXPORT_SYMBOL_GPL(mwifiex_main_process);
  433. /*
  434. * This function frees the adapter structure.
  435. *
  436. * Additionally, this closes the netlink socket, frees the timers
  437. * and private structures.
  438. */
  439. static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
  440. {
  441. if (!adapter) {
  442. pr_err("%s: adapter is NULL\n", __func__);
  443. return;
  444. }
  445. mwifiex_unregister(adapter);
  446. pr_debug("info: %s: free adapter\n", __func__);
  447. }
  448. /*
  449. * This function cancels all works in the queue and destroys
  450. * the main workqueue.
  451. */
  452. static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
  453. {
  454. if (adapter->workqueue) {
  455. destroy_workqueue(adapter->workqueue);
  456. adapter->workqueue = NULL;
  457. }
  458. if (adapter->rx_workqueue) {
  459. destroy_workqueue(adapter->rx_workqueue);
  460. adapter->rx_workqueue = NULL;
  461. }
  462. }
  463. /*
  464. * This function gets firmware and initializes it.
  465. *
  466. * The main initialization steps followed are -
  467. * - Download the correct firmware to card
  468. * - Issue the init commands to firmware
  469. */
  470. static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context)
  471. {
  472. int ret;
  473. char fmt[64];
  474. struct mwifiex_adapter *adapter = context;
  475. struct mwifiex_fw_image fw;
  476. bool init_failed = false;
  477. struct wireless_dev *wdev;
  478. struct completion *fw_done = adapter->fw_done;
  479. if (!firmware) {
  480. mwifiex_dbg(adapter, ERROR,
  481. "Failed to get firmware %s\n", adapter->fw_name);
  482. goto err_dnld_fw;
  483. }
  484. memset(&fw, 0, sizeof(struct mwifiex_fw_image));
  485. adapter->firmware = firmware;
  486. fw.fw_buf = (u8 *) adapter->firmware->data;
  487. fw.fw_len = adapter->firmware->size;
  488. if (adapter->if_ops.dnld_fw) {
  489. ret = adapter->if_ops.dnld_fw(adapter, &fw);
  490. } else {
  491. ret = mwifiex_dnld_fw(adapter, &fw);
  492. }
  493. if (ret == -1)
  494. goto err_dnld_fw;
  495. mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
  496. if (cal_data_cfg) {
  497. if ((request_firmware(&adapter->cal_data, cal_data_cfg,
  498. adapter->dev)) < 0)
  499. mwifiex_dbg(adapter, ERROR,
  500. "Cal data request_firmware() failed\n");
  501. }
  502. /* enable host interrupt after fw dnld is successful */
  503. if (adapter->if_ops.enable_int) {
  504. if (adapter->if_ops.enable_int(adapter))
  505. goto err_dnld_fw;
  506. }
  507. adapter->init_wait_q_woken = false;
  508. ret = mwifiex_init_fw(adapter);
  509. if (ret == -1) {
  510. goto err_init_fw;
  511. } else if (!ret) {
  512. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  513. goto done;
  514. }
  515. /* Wait for mwifiex_init to complete */
  516. if (!adapter->mfg_mode) {
  517. wait_event_interruptible(adapter->init_wait_q,
  518. adapter->init_wait_q_woken);
  519. if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
  520. goto err_init_fw;
  521. }
  522. if (!adapter->wiphy) {
  523. if (mwifiex_register_cfg80211(adapter)) {
  524. mwifiex_dbg(adapter, ERROR,
  525. "cannot register with cfg80211\n");
  526. goto err_init_fw;
  527. }
  528. }
  529. if (mwifiex_init_channel_scan_gap(adapter)) {
  530. mwifiex_dbg(adapter, ERROR,
  531. "could not init channel stats table\n");
  532. goto err_init_chan_scan;
  533. }
  534. if (driver_mode) {
  535. driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
  536. driver_mode |= MWIFIEX_DRIVER_MODE_STA;
  537. }
  538. rtnl_lock();
  539. wiphy_lock(adapter->wiphy);
  540. /* Create station interface by default */
  541. wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
  542. NL80211_IFTYPE_STATION, NULL);
  543. if (IS_ERR(wdev)) {
  544. mwifiex_dbg(adapter, ERROR,
  545. "cannot create default STA interface\n");
  546. wiphy_unlock(adapter->wiphy);
  547. rtnl_unlock();
  548. goto err_add_intf;
  549. }
  550. if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
  551. wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
  552. NL80211_IFTYPE_AP, NULL);
  553. if (IS_ERR(wdev)) {
  554. mwifiex_dbg(adapter, ERROR,
  555. "cannot create AP interface\n");
  556. wiphy_unlock(adapter->wiphy);
  557. rtnl_unlock();
  558. goto err_add_intf;
  559. }
  560. }
  561. if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
  562. wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
  563. NL80211_IFTYPE_P2P_CLIENT, NULL);
  564. if (IS_ERR(wdev)) {
  565. mwifiex_dbg(adapter, ERROR,
  566. "cannot create p2p client interface\n");
  567. wiphy_unlock(adapter->wiphy);
  568. rtnl_unlock();
  569. goto err_add_intf;
  570. }
  571. }
  572. wiphy_unlock(adapter->wiphy);
  573. rtnl_unlock();
  574. mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
  575. mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
  576. adapter->is_up = true;
  577. goto done;
  578. err_add_intf:
  579. vfree(adapter->chan_stats);
  580. err_init_chan_scan:
  581. wiphy_unregister(adapter->wiphy);
  582. wiphy_free(adapter->wiphy);
  583. err_init_fw:
  584. if (adapter->if_ops.disable_int)
  585. adapter->if_ops.disable_int(adapter);
  586. err_dnld_fw:
  587. mwifiex_dbg(adapter, ERROR,
  588. "info: %s: unregister device\n", __func__);
  589. if (adapter->if_ops.unregister_dev)
  590. adapter->if_ops.unregister_dev(adapter);
  591. set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
  592. mwifiex_terminate_workqueue(adapter);
  593. if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
  594. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  595. mwifiex_shutdown_drv(adapter);
  596. mwifiex_free_cmd_buffers(adapter);
  597. }
  598. init_failed = true;
  599. done:
  600. if (adapter->cal_data) {
  601. release_firmware(adapter->cal_data);
  602. adapter->cal_data = NULL;
  603. }
  604. if (adapter->firmware) {
  605. release_firmware(adapter->firmware);
  606. adapter->firmware = NULL;
  607. }
  608. if (init_failed) {
  609. if (adapter->irq_wakeup >= 0)
  610. device_init_wakeup(adapter->dev, false);
  611. mwifiex_free_adapter(adapter);
  612. }
  613. /* Tell all current and future waiters we're finished */
  614. complete_all(fw_done);
  615. return init_failed ? -EIO : 0;
  616. }
  617. static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
  618. {
  619. _mwifiex_fw_dpc(firmware, context);
  620. }
  621. /*
  622. * This function gets the firmware and (if called asynchronously) kicks off the
  623. * HW init when done.
  624. */
  625. static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter,
  626. bool req_fw_nowait)
  627. {
  628. int ret;
  629. /* Override default firmware with manufacturing one if
  630. * manufacturing mode is enabled
  631. */
  632. if (mfg_mode) {
  633. if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
  634. sizeof(adapter->fw_name)) >=
  635. sizeof(adapter->fw_name)) {
  636. pr_err("%s: fw_name too long!\n", __func__);
  637. return -1;
  638. }
  639. }
  640. if (req_fw_nowait) {
  641. ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
  642. adapter->dev, GFP_KERNEL, adapter,
  643. mwifiex_fw_dpc);
  644. } else {
  645. ret = request_firmware(&adapter->firmware,
  646. adapter->fw_name,
  647. adapter->dev);
  648. }
  649. if (ret < 0)
  650. mwifiex_dbg(adapter, ERROR, "request_firmware%s error %d\n",
  651. req_fw_nowait ? "_nowait" : "", ret);
  652. return ret;
  653. }
  654. /*
  655. * CFG802.11 network device handler for open.
  656. *
  657. * Starts the data queue.
  658. */
  659. static int
  660. mwifiex_open(struct net_device *dev)
  661. {
  662. netif_carrier_off(dev);
  663. return 0;
  664. }
  665. /*
  666. * CFG802.11 network device handler for close.
  667. */
  668. static int
  669. mwifiex_close(struct net_device *dev)
  670. {
  671. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  672. if (priv->scan_request) {
  673. struct cfg80211_scan_info info = {
  674. .aborted = true,
  675. };
  676. mwifiex_dbg(priv->adapter, INFO,
  677. "aborting scan on ndo_stop\n");
  678. cfg80211_scan_done(priv->scan_request, &info);
  679. priv->scan_request = NULL;
  680. priv->scan_aborting = true;
  681. }
  682. if (priv->sched_scanning) {
  683. mwifiex_dbg(priv->adapter, INFO,
  684. "aborting bgscan on ndo_stop\n");
  685. mwifiex_stop_bg_scan(priv);
  686. cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
  687. }
  688. return 0;
  689. }
  690. static bool
  691. mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
  692. struct sk_buff *skb)
  693. {
  694. struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
  695. if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
  696. mwifiex_is_skb_mgmt_frame(skb) ||
  697. (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
  698. ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
  699. (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
  700. mwifiex_dbg(priv->adapter, DATA,
  701. "bypass txqueue; eth type %#x, mgmt %d\n",
  702. ntohs(eth_hdr->h_proto),
  703. mwifiex_is_skb_mgmt_frame(skb));
  704. return true;
  705. }
  706. return false;
  707. }
  708. /*
  709. * Add buffer into wmm tx queue and queue work to transmit it.
  710. */
  711. int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
  712. {
  713. struct netdev_queue *txq;
  714. int index = mwifiex_1d_to_wmm_queue[skb->priority];
  715. if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
  716. txq = netdev_get_tx_queue(priv->netdev, index);
  717. if (!netif_tx_queue_stopped(txq)) {
  718. netif_tx_stop_queue(txq);
  719. mwifiex_dbg(priv->adapter, DATA,
  720. "stop queue: %d\n", index);
  721. }
  722. }
  723. if (mwifiex_bypass_tx_queue(priv, skb)) {
  724. atomic_inc(&priv->adapter->tx_pending);
  725. atomic_inc(&priv->adapter->bypass_tx_pending);
  726. mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
  727. } else {
  728. atomic_inc(&priv->adapter->tx_pending);
  729. mwifiex_wmm_add_buf_txqueue(priv, skb);
  730. }
  731. mwifiex_queue_main_work(priv->adapter);
  732. return 0;
  733. }
  734. struct sk_buff *
  735. mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
  736. struct sk_buff *skb, u8 flag, u64 *cookie)
  737. {
  738. struct sk_buff *orig_skb = skb;
  739. struct mwifiex_txinfo *tx_info, *orig_tx_info;
  740. skb = skb_clone(skb, GFP_ATOMIC);
  741. if (skb) {
  742. int id;
  743. spin_lock_bh(&priv->ack_status_lock);
  744. id = idr_alloc(&priv->ack_status_frames, orig_skb,
  745. 1, 0x10, GFP_ATOMIC);
  746. spin_unlock_bh(&priv->ack_status_lock);
  747. if (id >= 0) {
  748. tx_info = MWIFIEX_SKB_TXCB(skb);
  749. tx_info->ack_frame_id = id;
  750. tx_info->flags |= flag;
  751. orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
  752. orig_tx_info->ack_frame_id = id;
  753. orig_tx_info->flags |= flag;
  754. if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
  755. orig_tx_info->cookie = *cookie;
  756. } else if (skb_shared(skb)) {
  757. kfree_skb(orig_skb);
  758. } else {
  759. kfree_skb(skb);
  760. skb = orig_skb;
  761. }
  762. } else {
  763. /* couldn't clone -- lose tx status ... */
  764. skb = orig_skb;
  765. }
  766. return skb;
  767. }
  768. /*
  769. * CFG802.11 network device handler for data transmission.
  770. */
  771. static netdev_tx_t
  772. mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  773. {
  774. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  775. struct sk_buff *new_skb;
  776. struct mwifiex_txinfo *tx_info;
  777. bool multicast;
  778. mwifiex_dbg(priv->adapter, DATA,
  779. "data: %lu BSS(%d-%d): Data <= kernel\n",
  780. jiffies, priv->bss_type, priv->bss_num);
  781. if (test_bit(MWIFIEX_SURPRISE_REMOVED, &priv->adapter->work_flags)) {
  782. kfree_skb(skb);
  783. priv->stats.tx_dropped++;
  784. return 0;
  785. }
  786. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  787. mwifiex_dbg(priv->adapter, ERROR,
  788. "Tx: bad skb len %d\n", skb->len);
  789. kfree_skb(skb);
  790. priv->stats.tx_dropped++;
  791. return 0;
  792. }
  793. if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
  794. mwifiex_dbg(priv->adapter, DATA,
  795. "data: Tx: insufficient skb headroom %d\n",
  796. skb_headroom(skb));
  797. /* Insufficient skb headroom - allocate a new skb */
  798. new_skb =
  799. skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
  800. if (unlikely(!new_skb)) {
  801. mwifiex_dbg(priv->adapter, ERROR,
  802. "Tx: cannot alloca new_skb\n");
  803. kfree_skb(skb);
  804. priv->stats.tx_dropped++;
  805. return 0;
  806. }
  807. kfree_skb(skb);
  808. skb = new_skb;
  809. mwifiex_dbg(priv->adapter, INFO,
  810. "info: new skb headroomd %d\n",
  811. skb_headroom(skb));
  812. }
  813. tx_info = MWIFIEX_SKB_TXCB(skb);
  814. memset(tx_info, 0, sizeof(*tx_info));
  815. tx_info->bss_num = priv->bss_num;
  816. tx_info->bss_type = priv->bss_type;
  817. tx_info->pkt_len = skb->len;
  818. multicast = is_multicast_ether_addr(skb->data);
  819. if (unlikely(!multicast && skb->sk &&
  820. skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
  821. priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
  822. skb = mwifiex_clone_skb_for_tx_status(priv,
  823. skb,
  824. MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
  825. /* Record the current time the packet was queued; used to
  826. * determine the amount of time the packet was queued in
  827. * the driver before it was sent to the firmware.
  828. * The delay is then sent along with the packet to the
  829. * firmware for aggregate delay calculation for stats and
  830. * MSDU lifetime expiry.
  831. */
  832. __net_timestamp(skb);
  833. if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
  834. priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
  835. !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
  836. if (priv->adapter->auto_tdls && priv->check_tdls_tx)
  837. mwifiex_tdls_check_tx(priv, skb);
  838. }
  839. mwifiex_queue_tx_pkt(priv, skb);
  840. return 0;
  841. }
  842. int mwifiex_set_mac_address(struct mwifiex_private *priv,
  843. struct net_device *dev, bool external,
  844. u8 *new_mac)
  845. {
  846. int ret;
  847. u64 mac_addr, old_mac_addr;
  848. old_mac_addr = ether_addr_to_u64(priv->curr_addr);
  849. if (external) {
  850. mac_addr = ether_addr_to_u64(new_mac);
  851. } else {
  852. /* Internal mac address change */
  853. if (priv->bss_type == MWIFIEX_BSS_TYPE_ANY)
  854. return -EOPNOTSUPP;
  855. mac_addr = old_mac_addr;
  856. if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P) {
  857. mac_addr |= BIT_ULL(MWIFIEX_MAC_LOCAL_ADMIN_BIT);
  858. mac_addr += priv->bss_num;
  859. } else if (priv->adapter->priv[0] != priv) {
  860. /* Set mac address based on bss_type/bss_num */
  861. mac_addr ^= BIT_ULL(priv->bss_type + 8);
  862. mac_addr += priv->bss_num;
  863. }
  864. }
  865. u64_to_ether_addr(mac_addr, priv->curr_addr);
  866. /* Send request to firmware */
  867. ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
  868. HostCmd_ACT_GEN_SET, 0, NULL, true);
  869. if (ret) {
  870. u64_to_ether_addr(old_mac_addr, priv->curr_addr);
  871. mwifiex_dbg(priv->adapter, ERROR,
  872. "set mac address failed: ret=%d\n", ret);
  873. return ret;
  874. }
  875. eth_hw_addr_set(dev, priv->curr_addr);
  876. return 0;
  877. }
  878. /* CFG802.11 network device handler for setting MAC address.
  879. */
  880. static int
  881. mwifiex_ndo_set_mac_address(struct net_device *dev, void *addr)
  882. {
  883. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  884. struct sockaddr *hw_addr = addr;
  885. return mwifiex_set_mac_address(priv, dev, true, hw_addr->sa_data);
  886. }
  887. /*
  888. * CFG802.11 network device handler for setting multicast list.
  889. */
  890. static void mwifiex_set_multicast_list(struct net_device *dev)
  891. {
  892. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  893. struct mwifiex_multicast_list mcast_list;
  894. if (dev->flags & IFF_PROMISC) {
  895. mcast_list.mode = MWIFIEX_PROMISC_MODE;
  896. } else if (dev->flags & IFF_ALLMULTI ||
  897. netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
  898. mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
  899. } else {
  900. mcast_list.mode = MWIFIEX_MULTICAST_MODE;
  901. mcast_list.num_multicast_addr =
  902. mwifiex_copy_mcast_addr(&mcast_list, dev);
  903. }
  904. mwifiex_request_set_multicast_list(priv, &mcast_list);
  905. }
  906. /*
  907. * CFG802.11 network device handler for transmission timeout.
  908. */
  909. static void
  910. mwifiex_tx_timeout(struct net_device *dev, unsigned int txqueue)
  911. {
  912. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  913. priv->num_tx_timeout++;
  914. priv->tx_timeout_cnt++;
  915. mwifiex_dbg(priv->adapter, ERROR,
  916. "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
  917. jiffies, priv->tx_timeout_cnt, priv->bss_type,
  918. priv->bss_num);
  919. mwifiex_set_trans_start(dev);
  920. if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
  921. priv->adapter->if_ops.card_reset) {
  922. mwifiex_dbg(priv->adapter, ERROR,
  923. "tx_timeout_cnt exceeds threshold.\t"
  924. "Triggering card reset!\n");
  925. priv->adapter->if_ops.card_reset(priv->adapter);
  926. }
  927. }
  928. void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
  929. {
  930. struct usb_card_rec *card = adapter->card;
  931. struct mwifiex_private *priv;
  932. u16 tx_buf_size;
  933. int i, ret;
  934. card->mc_resync_flag = true;
  935. for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
  936. if (atomic_read(&card->port[i].tx_data_urb_pending)) {
  937. mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
  938. return;
  939. }
  940. }
  941. card->mc_resync_flag = false;
  942. tx_buf_size = 0xffff;
  943. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  944. ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
  945. HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
  946. if (ret)
  947. mwifiex_dbg(adapter, ERROR,
  948. "send reconfig tx buf size cmd err\n");
  949. }
  950. EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
  951. void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
  952. {
  953. /* Dump all the memory data into single file, a userspace script will
  954. * be used to split all the memory data to multiple files
  955. */
  956. mwifiex_dbg(adapter, MSG,
  957. "== mwifiex dump information to /sys/class/devcoredump start\n");
  958. dev_coredumpv(adapter->dev, adapter->devdump_data, adapter->devdump_len,
  959. GFP_KERNEL);
  960. mwifiex_dbg(adapter, MSG,
  961. "== mwifiex dump information to /sys/class/devcoredump end\n");
  962. /* Device dump data will be freed in device coredump release function
  963. * after 5 min. Here reset adapter->devdump_data and ->devdump_len
  964. * to avoid it been accidentally reused.
  965. */
  966. adapter->devdump_data = NULL;
  967. adapter->devdump_len = 0;
  968. }
  969. EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
  970. void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
  971. {
  972. char *p;
  973. char drv_version[64];
  974. struct usb_card_rec *cardp;
  975. struct sdio_mmc_card *sdio_card;
  976. struct mwifiex_private *priv;
  977. int i, idx;
  978. struct netdev_queue *txq;
  979. struct mwifiex_debug_info *debug_info;
  980. mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
  981. p = adapter->devdump_data;
  982. strcpy(p, "========Start dump driverinfo========\n");
  983. p += strlen("========Start dump driverinfo========\n");
  984. p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
  985. mwifiex_drv_get_driver_version(adapter, drv_version,
  986. sizeof(drv_version) - 1);
  987. p += sprintf(p, "driver_version = %s\n", drv_version);
  988. if (adapter->iface_type == MWIFIEX_USB) {
  989. cardp = (struct usb_card_rec *)adapter->card;
  990. p += sprintf(p, "tx_cmd_urb_pending = %d\n",
  991. atomic_read(&cardp->tx_cmd_urb_pending));
  992. p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
  993. atomic_read(&cardp->port[0].tx_data_urb_pending));
  994. p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
  995. atomic_read(&cardp->port[1].tx_data_urb_pending));
  996. p += sprintf(p, "rx_cmd_urb_pending = %d\n",
  997. atomic_read(&cardp->rx_cmd_urb_pending));
  998. p += sprintf(p, "rx_data_urb_pending = %d\n",
  999. atomic_read(&cardp->rx_data_urb_pending));
  1000. }
  1001. p += sprintf(p, "tx_pending = %d\n",
  1002. atomic_read(&adapter->tx_pending));
  1003. p += sprintf(p, "rx_pending = %d\n",
  1004. atomic_read(&adapter->rx_pending));
  1005. if (adapter->iface_type == MWIFIEX_SDIO) {
  1006. sdio_card = (struct sdio_mmc_card *)adapter->card;
  1007. p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
  1008. sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
  1009. p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
  1010. sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
  1011. }
  1012. for (i = 0; i < adapter->priv_num; i++) {
  1013. if (!adapter->priv[i] || !adapter->priv[i]->netdev)
  1014. continue;
  1015. priv = adapter->priv[i];
  1016. p += sprintf(p, "\n[interface : \"%s\"]\n",
  1017. priv->netdev->name);
  1018. p += sprintf(p, "wmm_tx_pending[0] = %d\n",
  1019. atomic_read(&priv->wmm_tx_pending[0]));
  1020. p += sprintf(p, "wmm_tx_pending[1] = %d\n",
  1021. atomic_read(&priv->wmm_tx_pending[1]));
  1022. p += sprintf(p, "wmm_tx_pending[2] = %d\n",
  1023. atomic_read(&priv->wmm_tx_pending[2]));
  1024. p += sprintf(p, "wmm_tx_pending[3] = %d\n",
  1025. atomic_read(&priv->wmm_tx_pending[3]));
  1026. p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
  1027. "Disconnected" : "Connected");
  1028. p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
  1029. ? "on" : "off"));
  1030. for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
  1031. txq = netdev_get_tx_queue(priv->netdev, idx);
  1032. p += sprintf(p, "tx queue %d:%s ", idx,
  1033. netif_tx_queue_stopped(txq) ?
  1034. "stopped" : "started");
  1035. }
  1036. p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
  1037. priv->netdev->name, priv->num_tx_timeout);
  1038. }
  1039. if (adapter->iface_type == MWIFIEX_SDIO ||
  1040. adapter->iface_type == MWIFIEX_PCIE) {
  1041. p += sprintf(p, "\n=== %s register dump===\n",
  1042. adapter->iface_type == MWIFIEX_SDIO ?
  1043. "SDIO" : "PCIE");
  1044. if (adapter->if_ops.reg_dump)
  1045. p += adapter->if_ops.reg_dump(adapter, p);
  1046. }
  1047. p += sprintf(p, "\n=== more debug information\n");
  1048. debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
  1049. if (debug_info) {
  1050. for (i = 0; i < adapter->priv_num; i++) {
  1051. if (!adapter->priv[i] || !adapter->priv[i]->netdev)
  1052. continue;
  1053. priv = adapter->priv[i];
  1054. mwifiex_get_debug_info(priv, debug_info);
  1055. p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
  1056. break;
  1057. }
  1058. kfree(debug_info);
  1059. }
  1060. strcpy(p, "\n========End dump========\n");
  1061. p += strlen("\n========End dump========\n");
  1062. mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
  1063. adapter->devdump_len = p - (char *)adapter->devdump_data;
  1064. }
  1065. EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
  1066. void mwifiex_prepare_fw_dump_info(struct mwifiex_adapter *adapter)
  1067. {
  1068. u8 idx;
  1069. char *fw_dump_ptr;
  1070. u32 dump_len = 0;
  1071. for (idx = 0; idx < adapter->num_mem_types; idx++) {
  1072. struct memory_type_mapping *entry =
  1073. &adapter->mem_type_mapping_tbl[idx];
  1074. if (entry->mem_ptr) {
  1075. dump_len += (strlen("========Start dump ") +
  1076. strlen(entry->mem_name) +
  1077. strlen("========\n") +
  1078. (entry->mem_size + 1) +
  1079. strlen("\n========End dump========\n"));
  1080. }
  1081. }
  1082. if (dump_len + 1 + adapter->devdump_len > MWIFIEX_FW_DUMP_SIZE) {
  1083. /* Realloc in case buffer overflow */
  1084. fw_dump_ptr = vzalloc(dump_len + 1 + adapter->devdump_len);
  1085. mwifiex_dbg(adapter, MSG, "Realloc device dump data.\n");
  1086. if (!fw_dump_ptr) {
  1087. vfree(adapter->devdump_data);
  1088. mwifiex_dbg(adapter, ERROR,
  1089. "vzalloc devdump data failure!\n");
  1090. return;
  1091. }
  1092. memmove(fw_dump_ptr, adapter->devdump_data,
  1093. adapter->devdump_len);
  1094. vfree(adapter->devdump_data);
  1095. adapter->devdump_data = fw_dump_ptr;
  1096. }
  1097. fw_dump_ptr = (char *)adapter->devdump_data + adapter->devdump_len;
  1098. for (idx = 0; idx < adapter->num_mem_types; idx++) {
  1099. struct memory_type_mapping *entry =
  1100. &adapter->mem_type_mapping_tbl[idx];
  1101. if (entry->mem_ptr) {
  1102. strcpy(fw_dump_ptr, "========Start dump ");
  1103. fw_dump_ptr += strlen("========Start dump ");
  1104. strcpy(fw_dump_ptr, entry->mem_name);
  1105. fw_dump_ptr += strlen(entry->mem_name);
  1106. strcpy(fw_dump_ptr, "========\n");
  1107. fw_dump_ptr += strlen("========\n");
  1108. memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
  1109. fw_dump_ptr += entry->mem_size;
  1110. strcpy(fw_dump_ptr, "\n========End dump========\n");
  1111. fw_dump_ptr += strlen("\n========End dump========\n");
  1112. }
  1113. }
  1114. adapter->devdump_len = fw_dump_ptr - (char *)adapter->devdump_data;
  1115. for (idx = 0; idx < adapter->num_mem_types; idx++) {
  1116. struct memory_type_mapping *entry =
  1117. &adapter->mem_type_mapping_tbl[idx];
  1118. vfree(entry->mem_ptr);
  1119. entry->mem_ptr = NULL;
  1120. entry->mem_size = 0;
  1121. }
  1122. }
  1123. EXPORT_SYMBOL_GPL(mwifiex_prepare_fw_dump_info);
  1124. /*
  1125. * CFG802.11 network device handler for statistics retrieval.
  1126. */
  1127. static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
  1128. {
  1129. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  1130. return &priv->stats;
  1131. }
  1132. static u16
  1133. mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
  1134. struct net_device *sb_dev)
  1135. {
  1136. skb->priority = cfg80211_classify8021d(skb, NULL);
  1137. return mwifiex_1d_to_wmm_queue[skb->priority];
  1138. }
  1139. /* Network device handlers */
  1140. static const struct net_device_ops mwifiex_netdev_ops = {
  1141. .ndo_open = mwifiex_open,
  1142. .ndo_stop = mwifiex_close,
  1143. .ndo_start_xmit = mwifiex_hard_start_xmit,
  1144. .ndo_set_mac_address = mwifiex_ndo_set_mac_address,
  1145. .ndo_validate_addr = eth_validate_addr,
  1146. .ndo_tx_timeout = mwifiex_tx_timeout,
  1147. .ndo_get_stats = mwifiex_get_stats,
  1148. .ndo_set_rx_mode = mwifiex_set_multicast_list,
  1149. .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
  1150. };
  1151. /*
  1152. * This function initializes the private structure parameters.
  1153. *
  1154. * The following wait queues are initialized -
  1155. * - IOCTL wait queue
  1156. * - Command wait queue
  1157. * - Statistics wait queue
  1158. *
  1159. * ...and the following default parameters are set -
  1160. * - Current key index : Set to 0
  1161. * - Rate index : Set to auto
  1162. * - Media connected : Set to disconnected
  1163. * - Adhoc link sensed : Set to false
  1164. * - Nick name : Set to null
  1165. * - Number of Tx timeout : Set to 0
  1166. * - Device address : Set to current address
  1167. * - Rx histogram statistc : Set to 0
  1168. *
  1169. * In addition, the CFG80211 work queue is also created.
  1170. */
  1171. void mwifiex_init_priv_params(struct mwifiex_private *priv,
  1172. struct net_device *dev)
  1173. {
  1174. dev->netdev_ops = &mwifiex_netdev_ops;
  1175. dev->needs_free_netdev = true;
  1176. /* Initialize private structure */
  1177. priv->current_key_index = 0;
  1178. priv->media_connected = false;
  1179. memset(priv->mgmt_ie, 0,
  1180. sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
  1181. priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
  1182. priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
  1183. priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
  1184. priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
  1185. priv->num_tx_timeout = 0;
  1186. if (is_valid_ether_addr(dev->dev_addr))
  1187. ether_addr_copy(priv->curr_addr, dev->dev_addr);
  1188. else
  1189. ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
  1190. if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
  1191. GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
  1192. priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
  1193. if (priv->hist_data)
  1194. mwifiex_hist_data_reset(priv);
  1195. }
  1196. }
  1197. /*
  1198. * This function check if command is pending.
  1199. */
  1200. int is_command_pending(struct mwifiex_adapter *adapter)
  1201. {
  1202. int is_cmd_pend_q_empty;
  1203. spin_lock_bh(&adapter->cmd_pending_q_lock);
  1204. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  1205. spin_unlock_bh(&adapter->cmd_pending_q_lock);
  1206. return !is_cmd_pend_q_empty;
  1207. }
  1208. /*
  1209. * This is the RX work queue function.
  1210. *
  1211. * It handles the RX operations.
  1212. */
  1213. static void mwifiex_rx_work_queue(struct work_struct *work)
  1214. {
  1215. struct mwifiex_adapter *adapter =
  1216. container_of(work, struct mwifiex_adapter, rx_work);
  1217. if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
  1218. return;
  1219. mwifiex_process_rx(adapter);
  1220. }
  1221. /*
  1222. * This is the main work queue function.
  1223. *
  1224. * It handles the main process, which in turn handles the complete
  1225. * driver operations.
  1226. */
  1227. static void mwifiex_main_work_queue(struct work_struct *work)
  1228. {
  1229. struct mwifiex_adapter *adapter =
  1230. container_of(work, struct mwifiex_adapter, main_work);
  1231. if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
  1232. return;
  1233. mwifiex_main_process(adapter);
  1234. }
  1235. /* Common teardown code used for both device removal and reset */
  1236. static void mwifiex_uninit_sw(struct mwifiex_adapter *adapter)
  1237. {
  1238. struct mwifiex_private *priv;
  1239. int i;
  1240. /* We can no longer handle interrupts once we start doing the teardown
  1241. * below.
  1242. */
  1243. if (adapter->if_ops.disable_int)
  1244. adapter->if_ops.disable_int(adapter);
  1245. set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
  1246. mwifiex_terminate_workqueue(adapter);
  1247. adapter->int_status = 0;
  1248. /* Stop data */
  1249. for (i = 0; i < adapter->priv_num; i++) {
  1250. priv = adapter->priv[i];
  1251. if (priv && priv->netdev) {
  1252. mwifiex_stop_net_dev_queue(priv->netdev, adapter);
  1253. if (netif_carrier_ok(priv->netdev))
  1254. netif_carrier_off(priv->netdev);
  1255. netif_device_detach(priv->netdev);
  1256. }
  1257. }
  1258. mwifiex_dbg(adapter, CMD, "cmd: calling mwifiex_shutdown_drv...\n");
  1259. mwifiex_shutdown_drv(adapter);
  1260. mwifiex_dbg(adapter, CMD, "cmd: mwifiex_shutdown_drv done\n");
  1261. if (atomic_read(&adapter->rx_pending) ||
  1262. atomic_read(&adapter->tx_pending) ||
  1263. atomic_read(&adapter->cmd_pending)) {
  1264. mwifiex_dbg(adapter, ERROR,
  1265. "rx_pending=%d, tx_pending=%d,\t"
  1266. "cmd_pending=%d\n",
  1267. atomic_read(&adapter->rx_pending),
  1268. atomic_read(&adapter->tx_pending),
  1269. atomic_read(&adapter->cmd_pending));
  1270. }
  1271. for (i = 0; i < adapter->priv_num; i++) {
  1272. priv = adapter->priv[i];
  1273. if (!priv)
  1274. continue;
  1275. rtnl_lock();
  1276. if (priv->netdev &&
  1277. priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
  1278. /*
  1279. * Close the netdev now, because if we do it later, the
  1280. * netdev notifiers will need to acquire the wiphy lock
  1281. * again --> deadlock.
  1282. */
  1283. dev_close(priv->wdev.netdev);
  1284. wiphy_lock(adapter->wiphy);
  1285. mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
  1286. wiphy_unlock(adapter->wiphy);
  1287. }
  1288. rtnl_unlock();
  1289. }
  1290. wiphy_unregister(adapter->wiphy);
  1291. wiphy_free(adapter->wiphy);
  1292. adapter->wiphy = NULL;
  1293. vfree(adapter->chan_stats);
  1294. mwifiex_free_cmd_buffers(adapter);
  1295. }
  1296. /*
  1297. * This function can be used for shutting down the adapter SW.
  1298. */
  1299. int mwifiex_shutdown_sw(struct mwifiex_adapter *adapter)
  1300. {
  1301. struct mwifiex_private *priv;
  1302. if (!adapter)
  1303. return 0;
  1304. wait_for_completion(adapter->fw_done);
  1305. /* Caller should ensure we aren't suspending while this happens */
  1306. reinit_completion(adapter->fw_done);
  1307. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  1308. mwifiex_deauthenticate(priv, NULL);
  1309. mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
  1310. mwifiex_uninit_sw(adapter);
  1311. adapter->is_up = false;
  1312. if (adapter->if_ops.down_dev)
  1313. adapter->if_ops.down_dev(adapter);
  1314. return 0;
  1315. }
  1316. EXPORT_SYMBOL_GPL(mwifiex_shutdown_sw);
  1317. /* This function can be used for reinitting the adapter SW. Required
  1318. * code is extracted from mwifiex_add_card()
  1319. */
  1320. int
  1321. mwifiex_reinit_sw(struct mwifiex_adapter *adapter)
  1322. {
  1323. int ret;
  1324. mwifiex_init_lock_list(adapter);
  1325. if (adapter->if_ops.up_dev)
  1326. adapter->if_ops.up_dev(adapter);
  1327. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  1328. clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
  1329. init_waitqueue_head(&adapter->init_wait_q);
  1330. clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
  1331. adapter->hs_activated = false;
  1332. clear_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags);
  1333. init_waitqueue_head(&adapter->hs_activate_wait_q);
  1334. init_waitqueue_head(&adapter->cmd_wait_q.wait);
  1335. adapter->cmd_wait_q.status = 0;
  1336. adapter->scan_wait_q_woken = false;
  1337. if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
  1338. adapter->rx_work_enabled = true;
  1339. adapter->workqueue =
  1340. alloc_workqueue("MWIFIEX_WORK_QUEUE",
  1341. WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  1342. if (!adapter->workqueue)
  1343. goto err_kmalloc;
  1344. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  1345. if (adapter->rx_work_enabled) {
  1346. adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
  1347. WQ_HIGHPRI |
  1348. WQ_MEM_RECLAIM |
  1349. WQ_UNBOUND, 1);
  1350. if (!adapter->rx_workqueue)
  1351. goto err_kmalloc;
  1352. INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
  1353. }
  1354. /* Register the device. Fill up the private data structure with
  1355. * relevant information from the card. Some code extracted from
  1356. * mwifiex_register_dev()
  1357. */
  1358. mwifiex_dbg(adapter, INFO, "%s, mwifiex_init_hw_fw()...\n", __func__);
  1359. if (mwifiex_init_hw_fw(adapter, false)) {
  1360. mwifiex_dbg(adapter, ERROR,
  1361. "%s: firmware init failed\n", __func__);
  1362. goto err_init_fw;
  1363. }
  1364. /* _mwifiex_fw_dpc() does its own cleanup */
  1365. ret = _mwifiex_fw_dpc(adapter->firmware, adapter);
  1366. if (ret) {
  1367. pr_err("Failed to bring up adapter: %d\n", ret);
  1368. return ret;
  1369. }
  1370. mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
  1371. return 0;
  1372. err_init_fw:
  1373. mwifiex_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
  1374. if (adapter->if_ops.unregister_dev)
  1375. adapter->if_ops.unregister_dev(adapter);
  1376. err_kmalloc:
  1377. set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
  1378. mwifiex_terminate_workqueue(adapter);
  1379. if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
  1380. mwifiex_dbg(adapter, ERROR,
  1381. "info: %s: shutdown mwifiex\n", __func__);
  1382. mwifiex_shutdown_drv(adapter);
  1383. mwifiex_free_cmd_buffers(adapter);
  1384. }
  1385. complete_all(adapter->fw_done);
  1386. mwifiex_dbg(adapter, INFO, "%s, error\n", __func__);
  1387. return -1;
  1388. }
  1389. EXPORT_SYMBOL_GPL(mwifiex_reinit_sw);
  1390. static irqreturn_t mwifiex_irq_wakeup_handler(int irq, void *priv)
  1391. {
  1392. struct mwifiex_adapter *adapter = priv;
  1393. dev_dbg(adapter->dev, "%s: wake by wifi", __func__);
  1394. adapter->wake_by_wifi = true;
  1395. disable_irq_nosync(irq);
  1396. /* Notify PM core we are wakeup source */
  1397. pm_wakeup_event(adapter->dev, 0);
  1398. pm_system_wakeup();
  1399. return IRQ_HANDLED;
  1400. }
  1401. static void mwifiex_probe_of(struct mwifiex_adapter *adapter)
  1402. {
  1403. int ret;
  1404. struct device *dev = adapter->dev;
  1405. if (!dev->of_node)
  1406. goto err_exit;
  1407. adapter->dt_node = dev->of_node;
  1408. adapter->irq_wakeup = irq_of_parse_and_map(adapter->dt_node, 0);
  1409. if (!adapter->irq_wakeup) {
  1410. dev_dbg(dev, "fail to parse irq_wakeup from device tree\n");
  1411. goto err_exit;
  1412. }
  1413. ret = devm_request_irq(dev, adapter->irq_wakeup,
  1414. mwifiex_irq_wakeup_handler, IRQF_TRIGGER_LOW,
  1415. "wifi_wake", adapter);
  1416. if (ret) {
  1417. dev_err(dev, "Failed to request irq_wakeup %d (%d)\n",
  1418. adapter->irq_wakeup, ret);
  1419. goto err_exit;
  1420. }
  1421. disable_irq(adapter->irq_wakeup);
  1422. if (device_init_wakeup(dev, true)) {
  1423. dev_err(dev, "fail to init wakeup for mwifiex\n");
  1424. goto err_exit;
  1425. }
  1426. return;
  1427. err_exit:
  1428. adapter->irq_wakeup = -1;
  1429. }
  1430. /*
  1431. * This function adds the card.
  1432. *
  1433. * This function follows the following major steps to set up the device -
  1434. * - Initialize software. This includes probing the card, registering
  1435. * the interface operations table, and allocating/initializing the
  1436. * adapter structure
  1437. * - Set up the netlink socket
  1438. * - Create and start the main work queue
  1439. * - Register the device
  1440. * - Initialize firmware and hardware
  1441. * - Add logical interfaces
  1442. */
  1443. int
  1444. mwifiex_add_card(void *card, struct completion *fw_done,
  1445. struct mwifiex_if_ops *if_ops, u8 iface_type,
  1446. struct device *dev)
  1447. {
  1448. struct mwifiex_adapter *adapter;
  1449. if (mwifiex_register(card, dev, if_ops, (void **)&adapter)) {
  1450. pr_err("%s: software init failed\n", __func__);
  1451. goto err_init_sw;
  1452. }
  1453. mwifiex_probe_of(adapter);
  1454. adapter->iface_type = iface_type;
  1455. adapter->fw_done = fw_done;
  1456. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  1457. clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
  1458. init_waitqueue_head(&adapter->init_wait_q);
  1459. clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
  1460. adapter->hs_activated = false;
  1461. init_waitqueue_head(&adapter->hs_activate_wait_q);
  1462. init_waitqueue_head(&adapter->cmd_wait_q.wait);
  1463. adapter->cmd_wait_q.status = 0;
  1464. adapter->scan_wait_q_woken = false;
  1465. if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
  1466. adapter->rx_work_enabled = true;
  1467. adapter->workqueue =
  1468. alloc_workqueue("MWIFIEX_WORK_QUEUE",
  1469. WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  1470. if (!adapter->workqueue)
  1471. goto err_kmalloc;
  1472. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  1473. if (adapter->rx_work_enabled) {
  1474. adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
  1475. WQ_HIGHPRI |
  1476. WQ_MEM_RECLAIM |
  1477. WQ_UNBOUND, 1);
  1478. if (!adapter->rx_workqueue)
  1479. goto err_kmalloc;
  1480. INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
  1481. }
  1482. /* Register the device. Fill up the private data structure with relevant
  1483. information from the card. */
  1484. if (adapter->if_ops.register_dev(adapter)) {
  1485. pr_err("%s: failed to register mwifiex device\n", __func__);
  1486. goto err_registerdev;
  1487. }
  1488. if (mwifiex_init_hw_fw(adapter, true)) {
  1489. pr_err("%s: firmware init failed\n", __func__);
  1490. goto err_init_fw;
  1491. }
  1492. return 0;
  1493. err_init_fw:
  1494. pr_debug("info: %s: unregister device\n", __func__);
  1495. if (adapter->if_ops.unregister_dev)
  1496. adapter->if_ops.unregister_dev(adapter);
  1497. err_registerdev:
  1498. set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
  1499. mwifiex_terminate_workqueue(adapter);
  1500. if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
  1501. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  1502. mwifiex_shutdown_drv(adapter);
  1503. mwifiex_free_cmd_buffers(adapter);
  1504. }
  1505. err_kmalloc:
  1506. if (adapter->irq_wakeup >= 0)
  1507. device_init_wakeup(adapter->dev, false);
  1508. mwifiex_free_adapter(adapter);
  1509. err_init_sw:
  1510. return -1;
  1511. }
  1512. EXPORT_SYMBOL_GPL(mwifiex_add_card);
  1513. /*
  1514. * This function removes the card.
  1515. *
  1516. * This function follows the following major steps to remove the device -
  1517. * - Stop data traffic
  1518. * - Shutdown firmware
  1519. * - Remove the logical interfaces
  1520. * - Terminate the work queue
  1521. * - Unregister the device
  1522. * - Free the adapter structure
  1523. */
  1524. int mwifiex_remove_card(struct mwifiex_adapter *adapter)
  1525. {
  1526. if (!adapter)
  1527. return 0;
  1528. if (adapter->is_up)
  1529. mwifiex_uninit_sw(adapter);
  1530. if (adapter->irq_wakeup >= 0)
  1531. device_init_wakeup(adapter->dev, false);
  1532. /* Unregister device */
  1533. mwifiex_dbg(adapter, INFO,
  1534. "info: unregister device\n");
  1535. if (adapter->if_ops.unregister_dev)
  1536. adapter->if_ops.unregister_dev(adapter);
  1537. /* Free adapter structure */
  1538. mwifiex_dbg(adapter, INFO,
  1539. "info: free adapter\n");
  1540. mwifiex_free_adapter(adapter);
  1541. return 0;
  1542. }
  1543. EXPORT_SYMBOL_GPL(mwifiex_remove_card);
  1544. void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
  1545. const char *fmt, ...)
  1546. {
  1547. struct va_format vaf;
  1548. va_list args;
  1549. if (!(adapter->debug_mask & mask))
  1550. return;
  1551. va_start(args, fmt);
  1552. vaf.fmt = fmt;
  1553. vaf.va = &args;
  1554. if (adapter->dev)
  1555. dev_info(adapter->dev, "%pV", &vaf);
  1556. else
  1557. pr_info("%pV", &vaf);
  1558. va_end(args);
  1559. }
  1560. EXPORT_SYMBOL_GPL(_mwifiex_dbg);
  1561. /*
  1562. * This function initializes the module.
  1563. *
  1564. * The debug FS is also initialized if configured.
  1565. */
  1566. static int
  1567. mwifiex_init_module(void)
  1568. {
  1569. #ifdef CONFIG_DEBUG_FS
  1570. mwifiex_debugfs_init();
  1571. #endif
  1572. return 0;
  1573. }
  1574. /*
  1575. * This function cleans up the module.
  1576. *
  1577. * The debug FS is removed if available.
  1578. */
  1579. static void
  1580. mwifiex_cleanup_module(void)
  1581. {
  1582. #ifdef CONFIG_DEBUG_FS
  1583. mwifiex_debugfs_remove();
  1584. #endif
  1585. }
  1586. module_init(mwifiex_init_module);
  1587. module_exit(mwifiex_cleanup_module);
  1588. MODULE_AUTHOR("Marvell International Ltd.");
  1589. MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
  1590. MODULE_VERSION(VERSION);
  1591. MODULE_LICENSE("GPL v2");