cmd.c 40 KB

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