cmd.c 39 KB

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