cmd.c 42 KB

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