cmd.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. /**
  2. * This file contains the handling of command.
  3. * It prepares command and sends it to firmware when it is ready.
  4. */
  5. #include <linux/kfifo.h>
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <linux/if_arp.h>
  9. #include "decl.h"
  10. #include "cfg.h"
  11. #include "cmd.h"
  12. #define CAL_NF(nf) ((s32)(-(s32)(nf)))
  13. #define CAL_RSSI(snr, nf) ((s32)((s32)(snr) + CAL_NF(nf)))
  14. static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
  15. /**
  16. * @brief Simple callback that copies response back into command
  17. *
  18. * @param priv A pointer to struct lbs_private structure
  19. * @param extra A pointer to the original command structure for which
  20. * 'resp' is a response
  21. * @param resp A pointer to the command response
  22. *
  23. * @return 0 on success, error on failure
  24. */
  25. int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
  26. struct cmd_header *resp)
  27. {
  28. struct cmd_header *buf = (void *)extra;
  29. uint16_t copy_len;
  30. copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
  31. memcpy(buf, resp, copy_len);
  32. return 0;
  33. }
  34. EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
  35. /**
  36. * @brief Simple callback that ignores the result. Use this if
  37. * you just want to send a command to the hardware, but don't
  38. * care for the result.
  39. *
  40. * @param priv ignored
  41. * @param extra ignored
  42. * @param resp ignored
  43. *
  44. * @return 0 for success
  45. */
  46. static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
  47. struct cmd_header *resp)
  48. {
  49. return 0;
  50. }
  51. /**
  52. * @brief Checks whether a command is allowed in Power Save mode
  53. *
  54. * @param command the command ID
  55. * @return 1 if allowed, 0 if not allowed
  56. */
  57. static u8 is_command_allowed_in_ps(u16 cmd)
  58. {
  59. switch (cmd) {
  60. case CMD_802_11_RSSI:
  61. return 1;
  62. case CMD_802_11_HOST_SLEEP_CFG:
  63. return 1;
  64. default:
  65. break;
  66. }
  67. return 0;
  68. }
  69. /**
  70. * @brief This function checks if the command is allowed.
  71. *
  72. * @param priv A pointer to lbs_private structure
  73. * @return allowed or not allowed.
  74. */
  75. static int lbs_is_cmd_allowed(struct lbs_private *priv)
  76. {
  77. int ret = 1;
  78. lbs_deb_enter(LBS_DEB_CMD);
  79. if (!priv->is_auto_deep_sleep_enabled) {
  80. if (priv->is_deep_sleep) {
  81. lbs_deb_cmd("command not allowed in deep sleep\n");
  82. ret = 0;
  83. }
  84. }
  85. lbs_deb_leave(LBS_DEB_CMD);
  86. return ret;
  87. }
  88. /**
  89. * @brief Updates the hardware details like MAC address and regulatory region
  90. *
  91. * @param priv A pointer to struct lbs_private structure
  92. *
  93. * @return 0 on success, error on failure
  94. */
  95. int lbs_update_hw_spec(struct lbs_private *priv)
  96. {
  97. struct cmd_ds_get_hw_spec cmd;
  98. int ret = -1;
  99. u32 i;
  100. lbs_deb_enter(LBS_DEB_CMD);
  101. memset(&cmd, 0, sizeof(cmd));
  102. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  103. memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
  104. ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
  105. if (ret)
  106. goto out;
  107. priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
  108. /* The firmware release is in an interesting format: the patch
  109. * level is in the most significant nibble ... so fix that: */
  110. priv->fwrelease = le32_to_cpu(cmd.fwrelease);
  111. priv->fwrelease = (priv->fwrelease << 8) |
  112. (priv->fwrelease >> 24 & 0xff);
  113. /* Some firmware capabilities:
  114. * CF card firmware 5.0.16p0: cap 0x00000303
  115. * USB dongle firmware 5.110.17p2: cap 0x00000303
  116. */
  117. lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
  118. cmd.permanentaddr,
  119. priv->fwrelease >> 24 & 0xff,
  120. priv->fwrelease >> 16 & 0xff,
  121. priv->fwrelease >> 8 & 0xff,
  122. priv->fwrelease & 0xff,
  123. priv->fwcapinfo);
  124. lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
  125. cmd.hwifversion, cmd.version);
  126. /* Clamp region code to 8-bit since FW spec indicates that it should
  127. * only ever be 8-bit, even though the field size is 16-bit. Some firmware
  128. * returns non-zero high 8 bits here.
  129. *
  130. * Firmware version 4.0.102 used in CF8381 has region code shifted. We
  131. * need to check for this problem and handle it properly.
  132. */
  133. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
  134. priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
  135. else
  136. priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
  137. for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
  138. /* use the region code to search for the index */
  139. if (priv->regioncode == lbs_region_code_to_index[i])
  140. break;
  141. }
  142. /* if it's unidentified region code, use the default (USA) */
  143. if (i >= MRVDRV_MAX_REGION_CODE) {
  144. priv->regioncode = 0x10;
  145. lbs_pr_info("unidentified region code; using the default (USA)\n");
  146. }
  147. if (priv->current_addr[0] == 0xff)
  148. memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
  149. memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
  150. if (priv->mesh_dev)
  151. memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
  152. out:
  153. lbs_deb_leave(LBS_DEB_CMD);
  154. return ret;
  155. }
  156. static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
  157. struct cmd_header *resp)
  158. {
  159. lbs_deb_enter(LBS_DEB_CMD);
  160. if (priv->is_host_sleep_activated) {
  161. priv->is_host_sleep_configured = 0;
  162. if (priv->psstate == PS_STATE_FULL_POWER) {
  163. priv->is_host_sleep_activated = 0;
  164. wake_up_interruptible(&priv->host_sleep_q);
  165. }
  166. } else {
  167. priv->is_host_sleep_configured = 1;
  168. }
  169. lbs_deb_leave(LBS_DEB_CMD);
  170. return 0;
  171. }
  172. int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
  173. struct wol_config *p_wol_config)
  174. {
  175. struct cmd_ds_host_sleep cmd_config;
  176. int ret;
  177. cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
  178. cmd_config.criteria = cpu_to_le32(criteria);
  179. cmd_config.gpio = priv->wol_gpio;
  180. cmd_config.gap = priv->wol_gap;
  181. if (p_wol_config != NULL)
  182. memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
  183. sizeof(struct wol_config));
  184. else
  185. cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
  186. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
  187. le16_to_cpu(cmd_config.hdr.size),
  188. lbs_ret_host_sleep_cfg, 0);
  189. if (!ret) {
  190. if (p_wol_config)
  191. memcpy((uint8_t *) p_wol_config,
  192. (uint8_t *)&cmd_config.wol_conf,
  193. sizeof(struct wol_config));
  194. } else {
  195. lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
  196. }
  197. return ret;
  198. }
  199. EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
  200. static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
  201. u16 cmd_action)
  202. {
  203. struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
  204. lbs_deb_enter(LBS_DEB_CMD);
  205. cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
  206. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
  207. sizeof(struct cmd_header));
  208. psm->action = cpu_to_le16(cmd_action);
  209. psm->multipledtim = 0;
  210. switch (cmd_action) {
  211. case CMD_SUBCMD_ENTER_PS:
  212. lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
  213. psm->locallisteninterval = 0;
  214. psm->nullpktinterval = 0;
  215. psm->multipledtim =
  216. cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
  217. break;
  218. case CMD_SUBCMD_EXIT_PS:
  219. lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
  220. break;
  221. case CMD_SUBCMD_SLEEP_CONFIRMED:
  222. lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
  223. break;
  224. default:
  225. break;
  226. }
  227. lbs_deb_leave(LBS_DEB_CMD);
  228. return 0;
  229. }
  230. int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
  231. struct sleep_params *sp)
  232. {
  233. struct cmd_ds_802_11_sleep_params cmd;
  234. int ret;
  235. lbs_deb_enter(LBS_DEB_CMD);
  236. if (cmd_action == CMD_ACT_GET) {
  237. memset(&cmd, 0, sizeof(cmd));
  238. } else {
  239. cmd.error = cpu_to_le16(sp->sp_error);
  240. cmd.offset = cpu_to_le16(sp->sp_offset);
  241. cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
  242. cmd.calcontrol = sp->sp_calcontrol;
  243. cmd.externalsleepclk = sp->sp_extsleepclk;
  244. cmd.reserved = cpu_to_le16(sp->sp_reserved);
  245. }
  246. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  247. cmd.action = cpu_to_le16(cmd_action);
  248. ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
  249. if (!ret) {
  250. lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
  251. "calcontrol 0x%x extsleepclk 0x%x\n",
  252. le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
  253. le16_to_cpu(cmd.stabletime), cmd.calcontrol,
  254. cmd.externalsleepclk);
  255. sp->sp_error = le16_to_cpu(cmd.error);
  256. sp->sp_offset = le16_to_cpu(cmd.offset);
  257. sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
  258. sp->sp_calcontrol = cmd.calcontrol;
  259. sp->sp_extsleepclk = cmd.externalsleepclk;
  260. sp->sp_reserved = le16_to_cpu(cmd.reserved);
  261. }
  262. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  263. return 0;
  264. }
  265. static int lbs_wait_for_ds_awake(struct lbs_private *priv)
  266. {
  267. int ret = 0;
  268. lbs_deb_enter(LBS_DEB_CMD);
  269. if (priv->is_deep_sleep) {
  270. if (!wait_event_interruptible_timeout(priv->ds_awake_q,
  271. !priv->is_deep_sleep, (10 * HZ))) {
  272. lbs_pr_err("ds_awake_q: timer expired\n");
  273. ret = -1;
  274. }
  275. }
  276. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  277. return ret;
  278. }
  279. int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
  280. {
  281. int ret = 0;
  282. lbs_deb_enter(LBS_DEB_CMD);
  283. if (deep_sleep) {
  284. if (priv->is_deep_sleep != 1) {
  285. lbs_deb_cmd("deep sleep: sleep\n");
  286. BUG_ON(!priv->enter_deep_sleep);
  287. ret = priv->enter_deep_sleep(priv);
  288. if (!ret) {
  289. netif_stop_queue(priv->dev);
  290. netif_carrier_off(priv->dev);
  291. }
  292. } else {
  293. lbs_pr_err("deep sleep: already enabled\n");
  294. }
  295. } else {
  296. if (priv->is_deep_sleep) {
  297. lbs_deb_cmd("deep sleep: wakeup\n");
  298. BUG_ON(!priv->exit_deep_sleep);
  299. ret = priv->exit_deep_sleep(priv);
  300. if (!ret) {
  301. ret = lbs_wait_for_ds_awake(priv);
  302. if (ret)
  303. lbs_pr_err("deep sleep: wakeup"
  304. "failed\n");
  305. }
  306. }
  307. }
  308. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  309. return ret;
  310. }
  311. static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
  312. unsigned long dummy,
  313. struct cmd_header *cmd)
  314. {
  315. lbs_deb_enter(LBS_DEB_FW);
  316. priv->is_host_sleep_activated = 1;
  317. wake_up_interruptible(&priv->host_sleep_q);
  318. lbs_deb_leave(LBS_DEB_FW);
  319. return 0;
  320. }
  321. int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
  322. {
  323. struct cmd_header cmd;
  324. int ret = 0;
  325. uint32_t criteria = EHS_REMOVE_WAKEUP;
  326. lbs_deb_enter(LBS_DEB_CMD);
  327. if (host_sleep) {
  328. if (priv->is_host_sleep_activated != 1) {
  329. memset(&cmd, 0, sizeof(cmd));
  330. ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
  331. (struct wol_config *)NULL);
  332. if (ret) {
  333. lbs_pr_info("Host sleep configuration failed: "
  334. "%d\n", ret);
  335. return ret;
  336. }
  337. if (priv->psstate == PS_STATE_FULL_POWER) {
  338. ret = __lbs_cmd(priv,
  339. CMD_802_11_HOST_SLEEP_ACTIVATE,
  340. &cmd,
  341. sizeof(cmd),
  342. lbs_ret_host_sleep_activate, 0);
  343. if (ret)
  344. lbs_pr_info("HOST_SLEEP_ACTIVATE "
  345. "failed: %d\n", ret);
  346. }
  347. if (!wait_event_interruptible_timeout(
  348. priv->host_sleep_q,
  349. priv->is_host_sleep_activated,
  350. (10 * HZ))) {
  351. lbs_pr_err("host_sleep_q: timer expired\n");
  352. ret = -1;
  353. }
  354. } else {
  355. lbs_pr_err("host sleep: already enabled\n");
  356. }
  357. } else {
  358. if (priv->is_host_sleep_activated)
  359. ret = lbs_host_sleep_cfg(priv, criteria,
  360. (struct wol_config *)NULL);
  361. }
  362. return ret;
  363. }
  364. /**
  365. * @brief Set an SNMP MIB value
  366. *
  367. * @param priv A pointer to struct lbs_private structure
  368. * @param oid The OID to set in the firmware
  369. * @param val Value to set the OID to
  370. *
  371. * @return 0 on success, error on failure
  372. */
  373. int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
  374. {
  375. struct cmd_ds_802_11_snmp_mib cmd;
  376. int ret;
  377. lbs_deb_enter(LBS_DEB_CMD);
  378. memset(&cmd, 0, sizeof (cmd));
  379. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  380. cmd.action = cpu_to_le16(CMD_ACT_SET);
  381. cmd.oid = cpu_to_le16((u16) oid);
  382. switch (oid) {
  383. case SNMP_MIB_OID_BSS_TYPE:
  384. cmd.bufsize = cpu_to_le16(sizeof(u8));
  385. cmd.value[0] = val;
  386. break;
  387. case SNMP_MIB_OID_11D_ENABLE:
  388. case SNMP_MIB_OID_FRAG_THRESHOLD:
  389. case SNMP_MIB_OID_RTS_THRESHOLD:
  390. case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
  391. case SNMP_MIB_OID_LONG_RETRY_LIMIT:
  392. cmd.bufsize = cpu_to_le16(sizeof(u16));
  393. *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
  394. break;
  395. default:
  396. lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
  397. ret = -EINVAL;
  398. goto out;
  399. }
  400. lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
  401. le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
  402. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  403. out:
  404. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  405. return ret;
  406. }
  407. /**
  408. * @brief Get an SNMP MIB value
  409. *
  410. * @param priv A pointer to struct lbs_private structure
  411. * @param oid The OID to retrieve from the firmware
  412. * @param out_val Location for the returned value
  413. *
  414. * @return 0 on success, error on failure
  415. */
  416. int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
  417. {
  418. struct cmd_ds_802_11_snmp_mib cmd;
  419. int ret;
  420. lbs_deb_enter(LBS_DEB_CMD);
  421. memset(&cmd, 0, sizeof (cmd));
  422. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  423. cmd.action = cpu_to_le16(CMD_ACT_GET);
  424. cmd.oid = cpu_to_le16(oid);
  425. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  426. if (ret)
  427. goto out;
  428. switch (le16_to_cpu(cmd.bufsize)) {
  429. case sizeof(u8):
  430. *out_val = cmd.value[0];
  431. break;
  432. case sizeof(u16):
  433. *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
  434. break;
  435. default:
  436. lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
  437. oid, le16_to_cpu(cmd.bufsize));
  438. break;
  439. }
  440. out:
  441. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  442. return ret;
  443. }
  444. /**
  445. * @brief Get the min, max, and current TX power
  446. *
  447. * @param priv A pointer to struct lbs_private structure
  448. * @param curlevel Current power level in dBm
  449. * @param minlevel Minimum supported power level in dBm (optional)
  450. * @param maxlevel Maximum supported power level in dBm (optional)
  451. *
  452. * @return 0 on success, error on failure
  453. */
  454. int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
  455. s16 *maxlevel)
  456. {
  457. struct cmd_ds_802_11_rf_tx_power cmd;
  458. int ret;
  459. lbs_deb_enter(LBS_DEB_CMD);
  460. memset(&cmd, 0, sizeof(cmd));
  461. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  462. cmd.action = cpu_to_le16(CMD_ACT_GET);
  463. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  464. if (ret == 0) {
  465. *curlevel = le16_to_cpu(cmd.curlevel);
  466. if (minlevel)
  467. *minlevel = cmd.minlevel;
  468. if (maxlevel)
  469. *maxlevel = cmd.maxlevel;
  470. }
  471. lbs_deb_leave(LBS_DEB_CMD);
  472. return ret;
  473. }
  474. /**
  475. * @brief Set the TX power
  476. *
  477. * @param priv A pointer to struct lbs_private structure
  478. * @param dbm The desired power level in dBm
  479. *
  480. * @return 0 on success, error on failure
  481. */
  482. int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
  483. {
  484. struct cmd_ds_802_11_rf_tx_power cmd;
  485. int ret;
  486. lbs_deb_enter(LBS_DEB_CMD);
  487. memset(&cmd, 0, sizeof(cmd));
  488. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  489. cmd.action = cpu_to_le16(CMD_ACT_SET);
  490. cmd.curlevel = cpu_to_le16(dbm);
  491. lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
  492. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  493. lbs_deb_leave(LBS_DEB_CMD);
  494. return ret;
  495. }
  496. /**
  497. * @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
  498. *
  499. * @param priv A pointer to struct lbs_private structure
  500. * @param enable 1 to enable monitor mode, 0 to disable
  501. *
  502. * @return 0 on success, error on failure
  503. */
  504. int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
  505. {
  506. struct cmd_ds_802_11_monitor_mode cmd;
  507. int ret;
  508. memset(&cmd, 0, sizeof(cmd));
  509. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  510. cmd.action = cpu_to_le16(CMD_ACT_SET);
  511. if (enable)
  512. cmd.mode = cpu_to_le16(0x1);
  513. lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
  514. ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
  515. if (ret == 0) {
  516. priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
  517. ARPHRD_ETHER;
  518. }
  519. lbs_deb_leave(LBS_DEB_CMD);
  520. return ret;
  521. }
  522. /**
  523. * @brief Get the radio channel
  524. *
  525. * @param priv A pointer to struct lbs_private structure
  526. *
  527. * @return The channel on success, error on failure
  528. */
  529. static int lbs_get_channel(struct lbs_private *priv)
  530. {
  531. struct cmd_ds_802_11_rf_channel cmd;
  532. int ret = 0;
  533. lbs_deb_enter(LBS_DEB_CMD);
  534. memset(&cmd, 0, sizeof(cmd));
  535. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  536. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
  537. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  538. if (ret)
  539. goto out;
  540. ret = le16_to_cpu(cmd.channel);
  541. lbs_deb_cmd("current radio channel is %d\n", ret);
  542. out:
  543. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  544. return ret;
  545. }
  546. int lbs_update_channel(struct lbs_private *priv)
  547. {
  548. int ret;
  549. /* the channel in f/w could be out of sync; get the current channel */
  550. lbs_deb_enter(LBS_DEB_ASSOC);
  551. ret = lbs_get_channel(priv);
  552. if (ret > 0) {
  553. priv->channel = ret;
  554. ret = 0;
  555. }
  556. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  557. return ret;
  558. }
  559. /**
  560. * @brief Set the radio channel
  561. *
  562. * @param priv A pointer to struct lbs_private structure
  563. * @param channel The desired channel, or 0 to clear a locked channel
  564. *
  565. * @return 0 on success, error on failure
  566. */
  567. int lbs_set_channel(struct lbs_private *priv, u8 channel)
  568. {
  569. struct cmd_ds_802_11_rf_channel cmd;
  570. #ifdef DEBUG
  571. u8 old_channel = priv->channel;
  572. #endif
  573. int ret = 0;
  574. lbs_deb_enter(LBS_DEB_CMD);
  575. memset(&cmd, 0, sizeof(cmd));
  576. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  577. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
  578. cmd.channel = cpu_to_le16(channel);
  579. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  580. if (ret)
  581. goto out;
  582. priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
  583. lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
  584. priv->channel);
  585. out:
  586. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  587. return ret;
  588. }
  589. /**
  590. * @brief Get current RSSI and noise floor
  591. *
  592. * @param priv A pointer to struct lbs_private structure
  593. * @param rssi On successful return, signal level in mBm
  594. *
  595. * @return The channel on success, error on failure
  596. */
  597. int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
  598. {
  599. struct cmd_ds_802_11_rssi cmd;
  600. int ret = 0;
  601. lbs_deb_enter(LBS_DEB_CMD);
  602. BUG_ON(rssi == NULL);
  603. BUG_ON(nf == NULL);
  604. memset(&cmd, 0, sizeof(cmd));
  605. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  606. /* Average SNR over last 8 beacons */
  607. cmd.n_or_snr = cpu_to_le16(8);
  608. ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
  609. if (ret == 0) {
  610. *nf = CAL_NF(le16_to_cpu(cmd.nf));
  611. *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
  612. }
  613. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  614. return ret;
  615. }
  616. /**
  617. * @brief Send regulatory and 802.11d domain information to the firmware
  618. *
  619. * @param priv pointer to struct lbs_private
  620. * @param request cfg80211 regulatory request structure
  621. * @param bands the device's supported bands and channels
  622. *
  623. * @return 0 on success, error code on failure
  624. */
  625. int lbs_set_11d_domain_info(struct lbs_private *priv,
  626. struct regulatory_request *request,
  627. struct ieee80211_supported_band **bands)
  628. {
  629. struct cmd_ds_802_11d_domain_info cmd;
  630. struct mrvl_ie_domain_param_set *domain = &cmd.domain;
  631. struct ieee80211_country_ie_triplet *t;
  632. enum ieee80211_band band;
  633. struct ieee80211_channel *ch;
  634. u8 num_triplet = 0;
  635. u8 num_parsed_chan = 0;
  636. u8 first_channel = 0, next_chan = 0, max_pwr = 0;
  637. u8 i, flag = 0;
  638. size_t triplet_size;
  639. int ret;
  640. lbs_deb_enter(LBS_DEB_11D);
  641. memset(&cmd, 0, sizeof(cmd));
  642. cmd.action = cpu_to_le16(CMD_ACT_SET);
  643. lbs_deb_11d("Setting country code '%c%c'\n",
  644. request->alpha2[0], request->alpha2[1]);
  645. domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
  646. /* Set country code */
  647. domain->country_code[0] = request->alpha2[0];
  648. domain->country_code[1] = request->alpha2[1];
  649. domain->country_code[2] = ' ';
  650. /* Now set up the channel triplets; firmware is somewhat picky here
  651. * and doesn't validate channel numbers and spans; hence it would
  652. * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39. Since
  653. * the last 3 aren't valid channels, the driver is responsible for
  654. * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
  655. * etc.
  656. */
  657. for (band = 0;
  658. (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
  659. band++) {
  660. if (!bands[band])
  661. continue;
  662. for (i = 0;
  663. (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
  664. i++) {
  665. ch = &bands[band]->channels[i];
  666. if (ch->flags & IEEE80211_CHAN_DISABLED)
  667. continue;
  668. if (!flag) {
  669. flag = 1;
  670. next_chan = first_channel = (u32) ch->hw_value;
  671. max_pwr = ch->max_power;
  672. num_parsed_chan = 1;
  673. continue;
  674. }
  675. if ((ch->hw_value == next_chan + 1) &&
  676. (ch->max_power == max_pwr)) {
  677. /* Consolidate adjacent channels */
  678. next_chan++;
  679. num_parsed_chan++;
  680. } else {
  681. /* Add this triplet */
  682. lbs_deb_11d("11D triplet (%d, %d, %d)\n",
  683. first_channel, num_parsed_chan,
  684. max_pwr);
  685. t = &domain->triplet[num_triplet];
  686. t->chans.first_channel = first_channel;
  687. t->chans.num_channels = num_parsed_chan;
  688. t->chans.max_power = max_pwr;
  689. num_triplet++;
  690. flag = 0;
  691. }
  692. }
  693. if (flag) {
  694. /* Add last triplet */
  695. lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
  696. num_parsed_chan, max_pwr);
  697. t = &domain->triplet[num_triplet];
  698. t->chans.first_channel = first_channel;
  699. t->chans.num_channels = num_parsed_chan;
  700. t->chans.max_power = max_pwr;
  701. num_triplet++;
  702. }
  703. }
  704. lbs_deb_11d("# triplets %d\n", num_triplet);
  705. /* Set command header sizes */
  706. triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
  707. domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
  708. triplet_size);
  709. lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
  710. (u8 *) &cmd.domain.country_code,
  711. le16_to_cpu(domain->header.len));
  712. cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
  713. sizeof(cmd.action) +
  714. sizeof(cmd.domain.header) +
  715. sizeof(cmd.domain.country_code) +
  716. triplet_size);
  717. ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
  718. lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
  719. return ret;
  720. }
  721. /**
  722. * @brief Read a MAC, Baseband, or RF register
  723. *
  724. * @param priv pointer to struct lbs_private
  725. * @param cmd register command, one of CMD_MAC_REG_ACCESS,
  726. * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
  727. * @param offset byte offset of the register to get
  728. * @param value on success, the value of the register at 'offset'
  729. *
  730. * @return 0 on success, error code on failure
  731. */
  732. int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
  733. {
  734. struct cmd_ds_reg_access cmd;
  735. int ret = 0;
  736. lbs_deb_enter(LBS_DEB_CMD);
  737. BUG_ON(value == NULL);
  738. memset(&cmd, 0, sizeof(cmd));
  739. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  740. cmd.action = cpu_to_le16(CMD_ACT_GET);
  741. if (reg != CMD_MAC_REG_ACCESS &&
  742. reg != CMD_BBP_REG_ACCESS &&
  743. reg != CMD_RF_REG_ACCESS) {
  744. ret = -EINVAL;
  745. goto out;
  746. }
  747. ret = lbs_cmd_with_response(priv, reg, &cmd);
  748. if (ret) {
  749. if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
  750. *value = cmd.value.bbp_rf;
  751. else if (reg == CMD_MAC_REG_ACCESS)
  752. *value = le32_to_cpu(cmd.value.mac);
  753. }
  754. out:
  755. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  756. return ret;
  757. }
  758. /**
  759. * @brief Write a MAC, Baseband, or RF register
  760. *
  761. * @param priv pointer to struct lbs_private
  762. * @param cmd register command, one of CMD_MAC_REG_ACCESS,
  763. * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
  764. * @param offset byte offset of the register to set
  765. * @param value the value to write to the register at 'offset'
  766. *
  767. * @return 0 on success, error code on failure
  768. */
  769. int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
  770. {
  771. struct cmd_ds_reg_access cmd;
  772. int ret = 0;
  773. lbs_deb_enter(LBS_DEB_CMD);
  774. memset(&cmd, 0, sizeof(cmd));
  775. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  776. cmd.action = cpu_to_le16(CMD_ACT_SET);
  777. if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
  778. cmd.value.bbp_rf = (u8) (value & 0xFF);
  779. else if (reg == CMD_MAC_REG_ACCESS)
  780. cmd.value.mac = cpu_to_le32(value);
  781. else {
  782. ret = -EINVAL;
  783. goto out;
  784. }
  785. ret = lbs_cmd_with_response(priv, reg, &cmd);
  786. out:
  787. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  788. return ret;
  789. }
  790. static void lbs_queue_cmd(struct lbs_private *priv,
  791. struct cmd_ctrl_node *cmdnode)
  792. {
  793. unsigned long flags;
  794. int addtail = 1;
  795. lbs_deb_enter(LBS_DEB_HOST);
  796. if (!cmdnode) {
  797. lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  798. goto done;
  799. }
  800. if (!cmdnode->cmdbuf->size) {
  801. lbs_deb_host("DNLD_CMD: cmd size is zero\n");
  802. goto done;
  803. }
  804. cmdnode->result = 0;
  805. /* Exit_PS command needs to be queued in the header always. */
  806. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
  807. struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
  808. if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  809. if (priv->psstate != PS_STATE_FULL_POWER)
  810. addtail = 0;
  811. }
  812. }
  813. if (le16_to_cpu(cmdnode->cmdbuf->command) ==
  814. CMD_802_11_WAKEUP_CONFIRM)
  815. addtail = 0;
  816. spin_lock_irqsave(&priv->driver_lock, flags);
  817. if (addtail)
  818. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  819. else
  820. list_add(&cmdnode->list, &priv->cmdpendingq);
  821. spin_unlock_irqrestore(&priv->driver_lock, flags);
  822. lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  823. le16_to_cpu(cmdnode->cmdbuf->command));
  824. done:
  825. lbs_deb_leave(LBS_DEB_HOST);
  826. }
  827. static void lbs_submit_command(struct lbs_private *priv,
  828. struct cmd_ctrl_node *cmdnode)
  829. {
  830. unsigned long flags;
  831. struct cmd_header *cmd;
  832. uint16_t cmdsize;
  833. uint16_t command;
  834. int timeo = 3 * HZ;
  835. int ret;
  836. lbs_deb_enter(LBS_DEB_HOST);
  837. cmd = cmdnode->cmdbuf;
  838. spin_lock_irqsave(&priv->driver_lock, flags);
  839. priv->cur_cmd = cmdnode;
  840. priv->cur_cmd_retcode = 0;
  841. spin_unlock_irqrestore(&priv->driver_lock, flags);
  842. cmdsize = le16_to_cpu(cmd->size);
  843. command = le16_to_cpu(cmd->command);
  844. /* These commands take longer */
  845. if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
  846. timeo = 5 * HZ;
  847. lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  848. command, le16_to_cpu(cmd->seqnum), cmdsize);
  849. lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  850. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  851. if (ret) {
  852. lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
  853. /* Let the timer kick in and retry, and potentially reset
  854. the whole thing if the condition persists */
  855. timeo = HZ/4;
  856. }
  857. if (command == CMD_802_11_DEEP_SLEEP) {
  858. if (priv->is_auto_deep_sleep_enabled) {
  859. priv->wakeup_dev_required = 1;
  860. priv->dnld_sent = 0;
  861. }
  862. priv->is_deep_sleep = 1;
  863. lbs_complete_command(priv, cmdnode, 0);
  864. } else {
  865. /* Setup the timer after transmit command */
  866. mod_timer(&priv->command_timer, jiffies + timeo);
  867. }
  868. lbs_deb_leave(LBS_DEB_HOST);
  869. }
  870. /**
  871. * This function inserts command node to cmdfreeq
  872. * after cleans it. Requires priv->driver_lock held.
  873. */
  874. static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  875. struct cmd_ctrl_node *cmdnode)
  876. {
  877. lbs_deb_enter(LBS_DEB_HOST);
  878. if (!cmdnode)
  879. goto out;
  880. cmdnode->callback = NULL;
  881. cmdnode->callback_arg = 0;
  882. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  883. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  884. out:
  885. lbs_deb_leave(LBS_DEB_HOST);
  886. }
  887. static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  888. struct cmd_ctrl_node *ptempcmd)
  889. {
  890. unsigned long flags;
  891. spin_lock_irqsave(&priv->driver_lock, flags);
  892. __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
  893. spin_unlock_irqrestore(&priv->driver_lock, flags);
  894. }
  895. void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
  896. int result)
  897. {
  898. if (cmd == priv->cur_cmd)
  899. priv->cur_cmd_retcode = result;
  900. cmd->result = result;
  901. cmd->cmdwaitqwoken = 1;
  902. wake_up_interruptible(&cmd->cmdwait_q);
  903. if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
  904. __lbs_cleanup_and_insert_cmd(priv, cmd);
  905. priv->cur_cmd = NULL;
  906. }
  907. int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
  908. {
  909. struct cmd_ds_802_11_radio_control cmd;
  910. int ret = -EINVAL;
  911. lbs_deb_enter(LBS_DEB_CMD);
  912. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  913. cmd.action = cpu_to_le16(CMD_ACT_SET);
  914. /* Only v8 and below support setting the preamble */
  915. if (priv->fwrelease < 0x09000000) {
  916. switch (preamble) {
  917. case RADIO_PREAMBLE_SHORT:
  918. case RADIO_PREAMBLE_AUTO:
  919. case RADIO_PREAMBLE_LONG:
  920. cmd.control = cpu_to_le16(preamble);
  921. break;
  922. default:
  923. goto out;
  924. }
  925. }
  926. if (radio_on)
  927. cmd.control |= cpu_to_le16(0x1);
  928. else {
  929. cmd.control &= cpu_to_le16(~0x1);
  930. priv->txpower_cur = 0;
  931. }
  932. lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
  933. radio_on ? "ON" : "OFF", preamble);
  934. priv->radio_on = radio_on;
  935. ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  936. out:
  937. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  938. return ret;
  939. }
  940. void lbs_set_mac_control(struct lbs_private *priv)
  941. {
  942. struct cmd_ds_mac_control cmd;
  943. lbs_deb_enter(LBS_DEB_CMD);
  944. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  945. cmd.action = cpu_to_le16(priv->mac_control);
  946. cmd.reserved = 0;
  947. lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
  948. lbs_deb_leave(LBS_DEB_CMD);
  949. }
  950. /**
  951. * @brief This function prepare the command before send to firmware.
  952. *
  953. * @param priv A pointer to struct lbs_private structure
  954. * @param cmd_no command number
  955. * @param cmd_action command action: GET or SET
  956. * @param wait_option wait option: wait response or not
  957. * @param cmd_oid cmd oid: treated as sub command
  958. * @param pdata_buf A pointer to informaion buffer
  959. * @return 0 or -1
  960. */
  961. int lbs_prepare_and_send_command(struct lbs_private *priv,
  962. u16 cmd_no,
  963. u16 cmd_action,
  964. u16 wait_option, u32 cmd_oid, void *pdata_buf)
  965. {
  966. int ret = 0;
  967. struct cmd_ctrl_node *cmdnode;
  968. struct cmd_ds_command *cmdptr;
  969. unsigned long flags;
  970. lbs_deb_enter(LBS_DEB_HOST);
  971. if (!priv) {
  972. lbs_deb_host("PREP_CMD: priv is NULL\n");
  973. ret = -1;
  974. goto done;
  975. }
  976. if (priv->surpriseremoved) {
  977. lbs_deb_host("PREP_CMD: card removed\n");
  978. ret = -1;
  979. goto done;
  980. }
  981. if (!lbs_is_cmd_allowed(priv)) {
  982. ret = -EBUSY;
  983. goto done;
  984. }
  985. cmdnode = lbs_get_cmd_ctrl_node(priv);
  986. if (cmdnode == NULL) {
  987. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  988. /* Wake up main thread to execute next command */
  989. wake_up_interruptible(&priv->waitq);
  990. ret = -1;
  991. goto done;
  992. }
  993. cmdnode->callback = NULL;
  994. cmdnode->callback_arg = (unsigned long)pdata_buf;
  995. cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
  996. lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
  997. /* Set sequence number, command and INT option */
  998. priv->seqnum++;
  999. cmdptr->seqnum = cpu_to_le16(priv->seqnum);
  1000. cmdptr->command = cpu_to_le16(cmd_no);
  1001. cmdptr->result = 0;
  1002. switch (cmd_no) {
  1003. case CMD_802_11_PS_MODE:
  1004. ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
  1005. break;
  1006. #ifdef CONFIG_LIBERTAS_MESH
  1007. case CMD_FWT_ACCESS:
  1008. ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
  1009. break;
  1010. #endif
  1011. case CMD_802_11_DEEP_SLEEP:
  1012. cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
  1013. cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
  1014. break;
  1015. default:
  1016. lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
  1017. ret = -1;
  1018. break;
  1019. }
  1020. /* return error, since the command preparation failed */
  1021. if (ret != 0) {
  1022. lbs_deb_host("PREP_CMD: command preparation failed\n");
  1023. lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1024. ret = -1;
  1025. goto done;
  1026. }
  1027. cmdnode->cmdwaitqwoken = 0;
  1028. lbs_queue_cmd(priv, cmdnode);
  1029. wake_up_interruptible(&priv->waitq);
  1030. if (wait_option & CMD_OPTION_WAITFORRSP) {
  1031. lbs_deb_host("PREP_CMD: wait for response\n");
  1032. might_sleep();
  1033. wait_event_interruptible(cmdnode->cmdwait_q,
  1034. cmdnode->cmdwaitqwoken);
  1035. }
  1036. spin_lock_irqsave(&priv->driver_lock, flags);
  1037. if (priv->cur_cmd_retcode) {
  1038. lbs_deb_host("PREP_CMD: command failed with return code %d\n",
  1039. priv->cur_cmd_retcode);
  1040. priv->cur_cmd_retcode = 0;
  1041. ret = -1;
  1042. }
  1043. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1044. done:
  1045. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1046. return ret;
  1047. }
  1048. /**
  1049. * @brief This function allocates the command buffer and link
  1050. * it to command free queue.
  1051. *
  1052. * @param priv A pointer to struct lbs_private structure
  1053. * @return 0 or -1
  1054. */
  1055. int lbs_allocate_cmd_buffer(struct lbs_private *priv)
  1056. {
  1057. int ret = 0;
  1058. u32 bufsize;
  1059. u32 i;
  1060. struct cmd_ctrl_node *cmdarray;
  1061. lbs_deb_enter(LBS_DEB_HOST);
  1062. /* Allocate and initialize the command array */
  1063. bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
  1064. if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
  1065. lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
  1066. ret = -1;
  1067. goto done;
  1068. }
  1069. priv->cmd_array = cmdarray;
  1070. /* Allocate and initialize each command buffer in the command array */
  1071. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1072. cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
  1073. if (!cmdarray[i].cmdbuf) {
  1074. lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
  1075. ret = -1;
  1076. goto done;
  1077. }
  1078. }
  1079. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1080. init_waitqueue_head(&cmdarray[i].cmdwait_q);
  1081. lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
  1082. }
  1083. ret = 0;
  1084. done:
  1085. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1086. return ret;
  1087. }
  1088. /**
  1089. * @brief This function frees the command buffer.
  1090. *
  1091. * @param priv A pointer to struct lbs_private structure
  1092. * @return 0 or -1
  1093. */
  1094. int lbs_free_cmd_buffer(struct lbs_private *priv)
  1095. {
  1096. struct cmd_ctrl_node *cmdarray;
  1097. unsigned int i;
  1098. lbs_deb_enter(LBS_DEB_HOST);
  1099. /* need to check if cmd array is allocated or not */
  1100. if (priv->cmd_array == NULL) {
  1101. lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
  1102. goto done;
  1103. }
  1104. cmdarray = priv->cmd_array;
  1105. /* Release shared memory buffers */
  1106. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1107. if (cmdarray[i].cmdbuf) {
  1108. kfree(cmdarray[i].cmdbuf);
  1109. cmdarray[i].cmdbuf = NULL;
  1110. }
  1111. }
  1112. /* Release cmd_ctrl_node */
  1113. if (priv->cmd_array) {
  1114. kfree(priv->cmd_array);
  1115. priv->cmd_array = NULL;
  1116. }
  1117. done:
  1118. lbs_deb_leave(LBS_DEB_HOST);
  1119. return 0;
  1120. }
  1121. /**
  1122. * @brief This function gets a free command node if available in
  1123. * command free queue.
  1124. *
  1125. * @param priv A pointer to struct lbs_private structure
  1126. * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
  1127. */
  1128. static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
  1129. {
  1130. struct cmd_ctrl_node *tempnode;
  1131. unsigned long flags;
  1132. lbs_deb_enter(LBS_DEB_HOST);
  1133. if (!priv)
  1134. return NULL;
  1135. spin_lock_irqsave(&priv->driver_lock, flags);
  1136. if (!list_empty(&priv->cmdfreeq)) {
  1137. tempnode = list_first_entry(&priv->cmdfreeq,
  1138. struct cmd_ctrl_node, list);
  1139. list_del(&tempnode->list);
  1140. } else {
  1141. lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
  1142. tempnode = NULL;
  1143. }
  1144. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1145. lbs_deb_leave(LBS_DEB_HOST);
  1146. return tempnode;
  1147. }
  1148. /**
  1149. * @brief This function executes next command in command
  1150. * pending queue. It will put firmware back to PS mode
  1151. * if applicable.
  1152. *
  1153. * @param priv A pointer to struct lbs_private structure
  1154. * @return 0 or -1
  1155. */
  1156. int lbs_execute_next_command(struct lbs_private *priv)
  1157. {
  1158. struct cmd_ctrl_node *cmdnode = NULL;
  1159. struct cmd_header *cmd;
  1160. unsigned long flags;
  1161. int ret = 0;
  1162. /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
  1163. * only caller to us is lbs_thread() and we get even when a
  1164. * data packet is received */
  1165. lbs_deb_enter(LBS_DEB_THREAD);
  1166. spin_lock_irqsave(&priv->driver_lock, flags);
  1167. if (priv->cur_cmd) {
  1168. lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
  1169. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1170. ret = -1;
  1171. goto done;
  1172. }
  1173. if (!list_empty(&priv->cmdpendingq)) {
  1174. cmdnode = list_first_entry(&priv->cmdpendingq,
  1175. struct cmd_ctrl_node, list);
  1176. }
  1177. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1178. if (cmdnode) {
  1179. cmd = cmdnode->cmdbuf;
  1180. if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
  1181. if ((priv->psstate == PS_STATE_SLEEP) ||
  1182. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1183. lbs_deb_host(
  1184. "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
  1185. le16_to_cpu(cmd->command),
  1186. priv->psstate);
  1187. ret = -1;
  1188. goto done;
  1189. }
  1190. lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
  1191. "0x%04x in psstate %d\n",
  1192. le16_to_cpu(cmd->command), priv->psstate);
  1193. } else if (priv->psstate != PS_STATE_FULL_POWER) {
  1194. /*
  1195. * 1. Non-PS command:
  1196. * Queue it. set needtowakeup to TRUE if current state
  1197. * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
  1198. * 2. PS command but not Exit_PS:
  1199. * Ignore it.
  1200. * 3. PS command Exit_PS:
  1201. * Set needtowakeup to TRUE if current state is SLEEP,
  1202. * otherwise send this command down to firmware
  1203. * immediately.
  1204. */
  1205. if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
  1206. /* Prepare to send Exit PS,
  1207. * this non PS command will be sent later */
  1208. if ((priv->psstate == PS_STATE_SLEEP)
  1209. || (priv->psstate == PS_STATE_PRE_SLEEP)
  1210. ) {
  1211. /* w/ new scheme, it will not reach here.
  1212. since it is blocked in main_thread. */
  1213. priv->needtowakeup = 1;
  1214. } else
  1215. lbs_ps_wakeup(priv, 0);
  1216. ret = 0;
  1217. goto done;
  1218. } else {
  1219. /*
  1220. * PS command. Ignore it if it is not Exit_PS.
  1221. * otherwise send it down immediately.
  1222. */
  1223. struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
  1224. lbs_deb_host(
  1225. "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
  1226. psm->action);
  1227. if (psm->action !=
  1228. cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  1229. lbs_deb_host(
  1230. "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
  1231. list_del(&cmdnode->list);
  1232. spin_lock_irqsave(&priv->driver_lock, flags);
  1233. lbs_complete_command(priv, cmdnode, 0);
  1234. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1235. ret = 0;
  1236. goto done;
  1237. }
  1238. if ((priv->psstate == PS_STATE_SLEEP) ||
  1239. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1240. lbs_deb_host(
  1241. "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
  1242. list_del(&cmdnode->list);
  1243. spin_lock_irqsave(&priv->driver_lock, flags);
  1244. lbs_complete_command(priv, cmdnode, 0);
  1245. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1246. priv->needtowakeup = 1;
  1247. ret = 0;
  1248. goto done;
  1249. }
  1250. lbs_deb_host(
  1251. "EXEC_NEXT_CMD: sending EXIT_PS\n");
  1252. }
  1253. }
  1254. list_del(&cmdnode->list);
  1255. lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
  1256. le16_to_cpu(cmd->command));
  1257. lbs_submit_command(priv, cmdnode);
  1258. } else {
  1259. /*
  1260. * check if in power save mode, if yes, put the device back
  1261. * to PS mode
  1262. */
  1263. #ifdef TODO
  1264. /*
  1265. * This was the old code for libertas+wext. Someone that
  1266. * understands this beast should re-code it in a sane way.
  1267. *
  1268. * I actually don't understand why this is related to WPA
  1269. * and to connection status, shouldn't powering should be
  1270. * independ of such things?
  1271. */
  1272. if ((priv->psmode != LBS802_11POWERMODECAM) &&
  1273. (priv->psstate == PS_STATE_FULL_POWER) &&
  1274. ((priv->connect_status == LBS_CONNECTED) ||
  1275. lbs_mesh_connected(priv))) {
  1276. if (priv->secinfo.WPAenabled ||
  1277. priv->secinfo.WPA2enabled) {
  1278. /* check for valid WPA group keys */
  1279. if (priv->wpa_mcast_key.len ||
  1280. priv->wpa_unicast_key.len) {
  1281. lbs_deb_host(
  1282. "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
  1283. " go back to PS_SLEEP");
  1284. lbs_ps_sleep(priv, 0);
  1285. }
  1286. } else {
  1287. lbs_deb_host(
  1288. "EXEC_NEXT_CMD: cmdpendingq empty, "
  1289. "go back to PS_SLEEP");
  1290. lbs_ps_sleep(priv, 0);
  1291. }
  1292. }
  1293. #endif
  1294. }
  1295. ret = 0;
  1296. done:
  1297. lbs_deb_leave(LBS_DEB_THREAD);
  1298. return ret;
  1299. }
  1300. static void lbs_send_confirmsleep(struct lbs_private *priv)
  1301. {
  1302. unsigned long flags;
  1303. int ret;
  1304. lbs_deb_enter(LBS_DEB_HOST);
  1305. lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
  1306. sizeof(confirm_sleep));
  1307. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
  1308. sizeof(confirm_sleep));
  1309. if (ret) {
  1310. lbs_pr_alert("confirm_sleep failed\n");
  1311. goto out;
  1312. }
  1313. spin_lock_irqsave(&priv->driver_lock, flags);
  1314. /* We don't get a response on the sleep-confirmation */
  1315. priv->dnld_sent = DNLD_RES_RECEIVED;
  1316. if (priv->is_host_sleep_configured) {
  1317. priv->is_host_sleep_activated = 1;
  1318. wake_up_interruptible(&priv->host_sleep_q);
  1319. }
  1320. /* If nothing to do, go back to sleep (?) */
  1321. if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
  1322. priv->psstate = PS_STATE_SLEEP;
  1323. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1324. out:
  1325. lbs_deb_leave(LBS_DEB_HOST);
  1326. }
  1327. void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
  1328. {
  1329. lbs_deb_enter(LBS_DEB_HOST);
  1330. /*
  1331. * PS is currently supported only in Infrastructure mode
  1332. * Remove this check if it is to be supported in IBSS mode also
  1333. */
  1334. lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  1335. CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
  1336. lbs_deb_leave(LBS_DEB_HOST);
  1337. }
  1338. /**
  1339. * @brief This function sends Exit_PS command to firmware.
  1340. *
  1341. * @param priv A pointer to struct lbs_private structure
  1342. * @param wait_option wait response or not
  1343. * @return n/a
  1344. */
  1345. void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
  1346. {
  1347. __le32 Localpsmode;
  1348. lbs_deb_enter(LBS_DEB_HOST);
  1349. Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
  1350. lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  1351. CMD_SUBCMD_EXIT_PS,
  1352. wait_option, 0, &Localpsmode);
  1353. lbs_deb_leave(LBS_DEB_HOST);
  1354. }
  1355. /**
  1356. * @brief This function checks condition and prepares to
  1357. * send sleep confirm command to firmware if ok.
  1358. *
  1359. * @param priv A pointer to struct lbs_private structure
  1360. * @param psmode Power Saving mode
  1361. * @return n/a
  1362. */
  1363. void lbs_ps_confirm_sleep(struct lbs_private *priv)
  1364. {
  1365. unsigned long flags =0;
  1366. int allowed = 1;
  1367. lbs_deb_enter(LBS_DEB_HOST);
  1368. spin_lock_irqsave(&priv->driver_lock, flags);
  1369. if (priv->dnld_sent) {
  1370. allowed = 0;
  1371. lbs_deb_host("dnld_sent was set\n");
  1372. }
  1373. /* In-progress command? */
  1374. if (priv->cur_cmd) {
  1375. allowed = 0;
  1376. lbs_deb_host("cur_cmd was set\n");
  1377. }
  1378. /* Pending events or command responses? */
  1379. if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
  1380. allowed = 0;
  1381. lbs_deb_host("pending events or command responses\n");
  1382. }
  1383. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1384. if (allowed) {
  1385. lbs_deb_host("sending lbs_ps_confirm_sleep\n");
  1386. lbs_send_confirmsleep(priv);
  1387. } else {
  1388. lbs_deb_host("sleep confirm has been delayed\n");
  1389. }
  1390. lbs_deb_leave(LBS_DEB_HOST);
  1391. }
  1392. /**
  1393. * @brief Configures the transmission power control functionality.
  1394. *
  1395. * @param priv A pointer to struct lbs_private structure
  1396. * @param enable Transmission power control enable
  1397. * @param p0 Power level when link quality is good (dBm).
  1398. * @param p1 Power level when link quality is fair (dBm).
  1399. * @param p2 Power level when link quality is poor (dBm).
  1400. * @param usesnr Use Signal to Noise Ratio in TPC
  1401. *
  1402. * @return 0 on success
  1403. */
  1404. int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
  1405. int8_t p2, int usesnr)
  1406. {
  1407. struct cmd_ds_802_11_tpc_cfg cmd;
  1408. int ret;
  1409. memset(&cmd, 0, sizeof(cmd));
  1410. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1411. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1412. cmd.enable = !!enable;
  1413. cmd.usesnr = !!usesnr;
  1414. cmd.P0 = p0;
  1415. cmd.P1 = p1;
  1416. cmd.P2 = p2;
  1417. ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
  1418. return ret;
  1419. }
  1420. /**
  1421. * @brief Configures the power adaptation settings.
  1422. *
  1423. * @param priv A pointer to struct lbs_private structure
  1424. * @param enable Power adaptation enable
  1425. * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
  1426. * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
  1427. * @param p2 Power level for 48 and 54 Mbps (dBm).
  1428. *
  1429. * @return 0 on Success
  1430. */
  1431. int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
  1432. int8_t p1, int8_t p2)
  1433. {
  1434. struct cmd_ds_802_11_pa_cfg cmd;
  1435. int ret;
  1436. memset(&cmd, 0, sizeof(cmd));
  1437. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1438. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1439. cmd.enable = !!enable;
  1440. cmd.P0 = p0;
  1441. cmd.P1 = p1;
  1442. cmd.P2 = p2;
  1443. ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
  1444. return ret;
  1445. }
  1446. struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
  1447. uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
  1448. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1449. unsigned long callback_arg)
  1450. {
  1451. struct cmd_ctrl_node *cmdnode;
  1452. lbs_deb_enter(LBS_DEB_HOST);
  1453. if (priv->surpriseremoved) {
  1454. lbs_deb_host("PREP_CMD: card removed\n");
  1455. cmdnode = ERR_PTR(-ENOENT);
  1456. goto done;
  1457. }
  1458. if (!lbs_is_cmd_allowed(priv)) {
  1459. cmdnode = ERR_PTR(-EBUSY);
  1460. goto done;
  1461. }
  1462. cmdnode = lbs_get_cmd_ctrl_node(priv);
  1463. if (cmdnode == NULL) {
  1464. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  1465. /* Wake up main thread to execute next command */
  1466. wake_up_interruptible(&priv->waitq);
  1467. cmdnode = ERR_PTR(-ENOBUFS);
  1468. goto done;
  1469. }
  1470. cmdnode->callback = callback;
  1471. cmdnode->callback_arg = callback_arg;
  1472. /* Copy the incoming command to the buffer */
  1473. memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
  1474. /* Set sequence number, clean result, move to buffer */
  1475. priv->seqnum++;
  1476. cmdnode->cmdbuf->command = cpu_to_le16(command);
  1477. cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
  1478. cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
  1479. cmdnode->cmdbuf->result = 0;
  1480. lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
  1481. cmdnode->cmdwaitqwoken = 0;
  1482. lbs_queue_cmd(priv, cmdnode);
  1483. wake_up_interruptible(&priv->waitq);
  1484. done:
  1485. lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
  1486. return cmdnode;
  1487. }
  1488. void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
  1489. struct cmd_header *in_cmd, int in_cmd_size)
  1490. {
  1491. lbs_deb_enter(LBS_DEB_CMD);
  1492. __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1493. lbs_cmd_async_callback, 0);
  1494. lbs_deb_leave(LBS_DEB_CMD);
  1495. }
  1496. int __lbs_cmd(struct lbs_private *priv, uint16_t command,
  1497. struct cmd_header *in_cmd, int in_cmd_size,
  1498. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1499. unsigned long callback_arg)
  1500. {
  1501. struct cmd_ctrl_node *cmdnode;
  1502. unsigned long flags;
  1503. int ret = 0;
  1504. lbs_deb_enter(LBS_DEB_HOST);
  1505. cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1506. callback, callback_arg);
  1507. if (IS_ERR(cmdnode)) {
  1508. ret = PTR_ERR(cmdnode);
  1509. goto done;
  1510. }
  1511. might_sleep();
  1512. wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
  1513. spin_lock_irqsave(&priv->driver_lock, flags);
  1514. ret = cmdnode->result;
  1515. if (ret)
  1516. lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
  1517. command, ret);
  1518. __lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1519. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1520. done:
  1521. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1522. return ret;
  1523. }
  1524. EXPORT_SYMBOL_GPL(__lbs_cmd);