cmd.c 42 KB

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