cmd.c 42 KB

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