cmd.c 43 KB

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