cmd.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  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. * @request: cfg80211 regulatory request structure
  632. * @bands: the device's supported bands and channels
  633. *
  634. * returns: 0 on success, error code on failure
  635. */
  636. int lbs_set_11d_domain_info(struct lbs_private *priv,
  637. struct regulatory_request *request,
  638. struct ieee80211_supported_band **bands)
  639. {
  640. struct cmd_ds_802_11d_domain_info cmd;
  641. struct mrvl_ie_domain_param_set *domain = &cmd.domain;
  642. struct ieee80211_country_ie_triplet *t;
  643. enum ieee80211_band band;
  644. struct ieee80211_channel *ch;
  645. u8 num_triplet = 0;
  646. u8 num_parsed_chan = 0;
  647. u8 first_channel = 0, next_chan = 0, max_pwr = 0;
  648. u8 i, flag = 0;
  649. size_t triplet_size;
  650. int ret;
  651. lbs_deb_enter(LBS_DEB_11D);
  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. request->alpha2[0], request->alpha2[1]);
  656. domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
  657. /* Set country code */
  658. domain->country_code[0] = request->alpha2[0];
  659. domain->country_code[1] = request->alpha2[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. lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
  730. return ret;
  731. }
  732. /**
  733. * lbs_get_reg - Read a MAC, Baseband, or RF register
  734. *
  735. * @priv: pointer to &struct lbs_private
  736. * @reg: register command, one of CMD_MAC_REG_ACCESS,
  737. * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
  738. * @offset: byte offset of the register to get
  739. * @value: on success, the value of the register at 'offset'
  740. *
  741. * returns: 0 on success, error code on failure
  742. */
  743. int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
  744. {
  745. struct cmd_ds_reg_access cmd;
  746. int ret = 0;
  747. lbs_deb_enter(LBS_DEB_CMD);
  748. BUG_ON(value == NULL);
  749. memset(&cmd, 0, sizeof(cmd));
  750. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  751. cmd.action = cpu_to_le16(CMD_ACT_GET);
  752. cmd.offset = cpu_to_le16(offset);
  753. if (reg != CMD_MAC_REG_ACCESS &&
  754. reg != CMD_BBP_REG_ACCESS &&
  755. reg != CMD_RF_REG_ACCESS) {
  756. ret = -EINVAL;
  757. goto out;
  758. }
  759. ret = lbs_cmd_with_response(priv, reg, &cmd);
  760. if (!ret) {
  761. if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
  762. *value = cmd.value.bbp_rf;
  763. else if (reg == CMD_MAC_REG_ACCESS)
  764. *value = le32_to_cpu(cmd.value.mac);
  765. }
  766. out:
  767. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  768. return ret;
  769. }
  770. /**
  771. * lbs_set_reg - Write a MAC, Baseband, or RF register
  772. *
  773. * @priv: pointer to &struct lbs_private
  774. * @reg: register command, one of CMD_MAC_REG_ACCESS,
  775. * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
  776. * @offset: byte offset of the register to set
  777. * @value: the value to write to the register at 'offset'
  778. *
  779. * returns: 0 on success, error code on failure
  780. */
  781. int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
  782. {
  783. struct cmd_ds_reg_access cmd;
  784. int ret = 0;
  785. lbs_deb_enter(LBS_DEB_CMD);
  786. memset(&cmd, 0, sizeof(cmd));
  787. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  788. cmd.action = cpu_to_le16(CMD_ACT_SET);
  789. cmd.offset = cpu_to_le16(offset);
  790. if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
  791. cmd.value.bbp_rf = (u8) (value & 0xFF);
  792. else if (reg == CMD_MAC_REG_ACCESS)
  793. cmd.value.mac = cpu_to_le32(value);
  794. else {
  795. ret = -EINVAL;
  796. goto out;
  797. }
  798. ret = lbs_cmd_with_response(priv, reg, &cmd);
  799. out:
  800. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  801. return ret;
  802. }
  803. static void lbs_queue_cmd(struct lbs_private *priv,
  804. struct cmd_ctrl_node *cmdnode)
  805. {
  806. unsigned long flags;
  807. int addtail = 1;
  808. lbs_deb_enter(LBS_DEB_HOST);
  809. if (!cmdnode) {
  810. lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  811. goto done;
  812. }
  813. if (!cmdnode->cmdbuf->size) {
  814. lbs_deb_host("DNLD_CMD: cmd size is zero\n");
  815. goto done;
  816. }
  817. cmdnode->result = 0;
  818. /* Exit_PS command needs to be queued in the header always. */
  819. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
  820. struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf;
  821. if (psm->action == cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
  822. if (priv->psstate != PS_STATE_FULL_POWER)
  823. addtail = 0;
  824. }
  825. }
  826. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
  827. addtail = 0;
  828. spin_lock_irqsave(&priv->driver_lock, flags);
  829. if (addtail)
  830. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  831. else
  832. list_add(&cmdnode->list, &priv->cmdpendingq);
  833. spin_unlock_irqrestore(&priv->driver_lock, flags);
  834. lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  835. le16_to_cpu(cmdnode->cmdbuf->command));
  836. done:
  837. lbs_deb_leave(LBS_DEB_HOST);
  838. }
  839. static void lbs_submit_command(struct lbs_private *priv,
  840. struct cmd_ctrl_node *cmdnode)
  841. {
  842. unsigned long flags;
  843. struct cmd_header *cmd;
  844. uint16_t cmdsize;
  845. uint16_t command;
  846. int timeo = 3 * HZ;
  847. int ret;
  848. lbs_deb_enter(LBS_DEB_HOST);
  849. cmd = cmdnode->cmdbuf;
  850. spin_lock_irqsave(&priv->driver_lock, flags);
  851. priv->seqnum++;
  852. cmd->seqnum = cpu_to_le16(priv->seqnum);
  853. priv->cur_cmd = cmdnode;
  854. spin_unlock_irqrestore(&priv->driver_lock, flags);
  855. cmdsize = le16_to_cpu(cmd->size);
  856. command = le16_to_cpu(cmd->command);
  857. /* These commands take longer */
  858. if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
  859. timeo = 5 * HZ;
  860. lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  861. command, le16_to_cpu(cmd->seqnum), cmdsize);
  862. lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  863. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  864. if (ret) {
  865. netdev_info(priv->dev, "DNLD_CMD: hw_host_to_card failed: %d\n",
  866. ret);
  867. /* Let the timer kick in and retry, and potentially reset
  868. the whole thing if the condition persists */
  869. timeo = HZ/4;
  870. }
  871. if (command == CMD_802_11_DEEP_SLEEP) {
  872. if (priv->is_auto_deep_sleep_enabled) {
  873. priv->wakeup_dev_required = 1;
  874. priv->dnld_sent = 0;
  875. }
  876. priv->is_deep_sleep = 1;
  877. lbs_complete_command(priv, cmdnode, 0);
  878. } else {
  879. /* Setup the timer after transmit command */
  880. mod_timer(&priv->command_timer, jiffies + timeo);
  881. }
  882. lbs_deb_leave(LBS_DEB_HOST);
  883. }
  884. /*
  885. * This function inserts command node to cmdfreeq
  886. * after cleans it. Requires priv->driver_lock held.
  887. */
  888. static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  889. struct cmd_ctrl_node *cmdnode)
  890. {
  891. lbs_deb_enter(LBS_DEB_HOST);
  892. if (!cmdnode)
  893. goto out;
  894. cmdnode->callback = NULL;
  895. cmdnode->callback_arg = 0;
  896. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  897. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  898. out:
  899. lbs_deb_leave(LBS_DEB_HOST);
  900. }
  901. static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  902. struct cmd_ctrl_node *ptempcmd)
  903. {
  904. unsigned long flags;
  905. spin_lock_irqsave(&priv->driver_lock, flags);
  906. __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
  907. spin_unlock_irqrestore(&priv->driver_lock, flags);
  908. }
  909. void __lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
  910. int result)
  911. {
  912. /*
  913. * Normally, commands are removed from cmdpendingq before being
  914. * submitted. However, we can arrive here on alternative codepaths
  915. * where the command is still pending. Make sure the command really
  916. * isn't part of a list at this point.
  917. */
  918. list_del_init(&cmd->list);
  919. cmd->result = result;
  920. cmd->cmdwaitqwoken = 1;
  921. wake_up(&cmd->cmdwait_q);
  922. if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
  923. __lbs_cleanup_and_insert_cmd(priv, cmd);
  924. priv->cur_cmd = NULL;
  925. wake_up(&priv->waitq);
  926. }
  927. void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
  928. int result)
  929. {
  930. unsigned long flags;
  931. spin_lock_irqsave(&priv->driver_lock, flags);
  932. __lbs_complete_command(priv, cmd, result);
  933. spin_unlock_irqrestore(&priv->driver_lock, flags);
  934. }
  935. int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
  936. {
  937. struct cmd_ds_802_11_radio_control cmd;
  938. int ret = -EINVAL;
  939. lbs_deb_enter(LBS_DEB_CMD);
  940. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  941. cmd.action = cpu_to_le16(CMD_ACT_SET);
  942. /* Only v8 and below support setting the preamble */
  943. if (priv->fwrelease < 0x09000000) {
  944. switch (preamble) {
  945. case RADIO_PREAMBLE_SHORT:
  946. case RADIO_PREAMBLE_AUTO:
  947. case RADIO_PREAMBLE_LONG:
  948. cmd.control = cpu_to_le16(preamble);
  949. break;
  950. default:
  951. goto out;
  952. }
  953. }
  954. if (radio_on)
  955. cmd.control |= cpu_to_le16(0x1);
  956. else {
  957. cmd.control &= cpu_to_le16(~0x1);
  958. priv->txpower_cur = 0;
  959. }
  960. lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
  961. radio_on ? "ON" : "OFF", preamble);
  962. priv->radio_on = radio_on;
  963. ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  964. out:
  965. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  966. return ret;
  967. }
  968. void lbs_set_mac_control(struct lbs_private *priv)
  969. {
  970. struct cmd_ds_mac_control cmd;
  971. lbs_deb_enter(LBS_DEB_CMD);
  972. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  973. cmd.action = cpu_to_le16(priv->mac_control);
  974. cmd.reserved = 0;
  975. lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
  976. lbs_deb_leave(LBS_DEB_CMD);
  977. }
  978. /**
  979. * lbs_allocate_cmd_buffer - allocates the command buffer and links
  980. * it to command free queue
  981. *
  982. * @priv: A pointer to &struct lbs_private structure
  983. *
  984. * returns: 0 for success or -1 on error
  985. */
  986. int lbs_allocate_cmd_buffer(struct lbs_private *priv)
  987. {
  988. int ret = 0;
  989. u32 bufsize;
  990. u32 i;
  991. struct cmd_ctrl_node *cmdarray;
  992. lbs_deb_enter(LBS_DEB_HOST);
  993. /* Allocate and initialize the command array */
  994. bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
  995. if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
  996. lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
  997. ret = -1;
  998. goto done;
  999. }
  1000. priv->cmd_array = cmdarray;
  1001. /* Allocate and initialize each command buffer in the command array */
  1002. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1003. cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
  1004. if (!cmdarray[i].cmdbuf) {
  1005. lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
  1006. ret = -1;
  1007. goto done;
  1008. }
  1009. }
  1010. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1011. init_waitqueue_head(&cmdarray[i].cmdwait_q);
  1012. lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
  1013. }
  1014. ret = 0;
  1015. done:
  1016. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1017. return ret;
  1018. }
  1019. /**
  1020. * lbs_free_cmd_buffer - free the command buffer
  1021. *
  1022. * @priv: A pointer to &struct lbs_private structure
  1023. *
  1024. * returns: 0 for success
  1025. */
  1026. int lbs_free_cmd_buffer(struct lbs_private *priv)
  1027. {
  1028. struct cmd_ctrl_node *cmdarray;
  1029. unsigned int i;
  1030. lbs_deb_enter(LBS_DEB_HOST);
  1031. /* need to check if cmd array is allocated or not */
  1032. if (priv->cmd_array == NULL) {
  1033. lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
  1034. goto done;
  1035. }
  1036. cmdarray = priv->cmd_array;
  1037. /* Release shared memory buffers */
  1038. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1039. if (cmdarray[i].cmdbuf) {
  1040. kfree(cmdarray[i].cmdbuf);
  1041. cmdarray[i].cmdbuf = NULL;
  1042. }
  1043. }
  1044. /* Release cmd_ctrl_node */
  1045. if (priv->cmd_array) {
  1046. kfree(priv->cmd_array);
  1047. priv->cmd_array = NULL;
  1048. }
  1049. done:
  1050. lbs_deb_leave(LBS_DEB_HOST);
  1051. return 0;
  1052. }
  1053. /**
  1054. * lbs_get_free_cmd_node - gets a free command node if available in
  1055. * command free queue
  1056. *
  1057. * @priv: A pointer to &struct lbs_private structure
  1058. *
  1059. * returns: A pointer to &cmd_ctrl_node structure on success
  1060. * or %NULL on error
  1061. */
  1062. static struct cmd_ctrl_node *lbs_get_free_cmd_node(struct lbs_private *priv)
  1063. {
  1064. struct cmd_ctrl_node *tempnode;
  1065. unsigned long flags;
  1066. lbs_deb_enter(LBS_DEB_HOST);
  1067. if (!priv)
  1068. return NULL;
  1069. spin_lock_irqsave(&priv->driver_lock, flags);
  1070. if (!list_empty(&priv->cmdfreeq)) {
  1071. tempnode = list_first_entry(&priv->cmdfreeq,
  1072. struct cmd_ctrl_node, list);
  1073. list_del_init(&tempnode->list);
  1074. } else {
  1075. lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
  1076. tempnode = NULL;
  1077. }
  1078. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1079. lbs_deb_leave(LBS_DEB_HOST);
  1080. return tempnode;
  1081. }
  1082. /**
  1083. * lbs_execute_next_command - execute next command in command
  1084. * pending queue. Will put firmware back to PS mode if applicable.
  1085. *
  1086. * @priv: A pointer to &struct lbs_private structure
  1087. *
  1088. * returns: 0 on success or -1 on error
  1089. */
  1090. int lbs_execute_next_command(struct lbs_private *priv)
  1091. {
  1092. struct cmd_ctrl_node *cmdnode = NULL;
  1093. struct cmd_header *cmd;
  1094. unsigned long flags;
  1095. int ret = 0;
  1096. /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
  1097. * only caller to us is lbs_thread() and we get even when a
  1098. * data packet is received */
  1099. lbs_deb_enter(LBS_DEB_THREAD);
  1100. spin_lock_irqsave(&priv->driver_lock, flags);
  1101. if (priv->cur_cmd) {
  1102. netdev_alert(priv->dev,
  1103. "EXEC_NEXT_CMD: already processing command!\n");
  1104. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1105. ret = -1;
  1106. goto done;
  1107. }
  1108. if (!list_empty(&priv->cmdpendingq)) {
  1109. cmdnode = list_first_entry(&priv->cmdpendingq,
  1110. struct cmd_ctrl_node, list);
  1111. }
  1112. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1113. if (cmdnode) {
  1114. cmd = cmdnode->cmdbuf;
  1115. if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
  1116. if ((priv->psstate == PS_STATE_SLEEP) ||
  1117. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1118. lbs_deb_host(
  1119. "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
  1120. le16_to_cpu(cmd->command),
  1121. priv->psstate);
  1122. ret = -1;
  1123. goto done;
  1124. }
  1125. lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
  1126. "0x%04x in psstate %d\n",
  1127. le16_to_cpu(cmd->command), priv->psstate);
  1128. } else if (priv->psstate != PS_STATE_FULL_POWER) {
  1129. /*
  1130. * 1. Non-PS command:
  1131. * Queue it. set needtowakeup to TRUE if current state
  1132. * is SLEEP, otherwise call send EXIT_PS.
  1133. * 2. PS command but not EXIT_PS:
  1134. * Ignore it.
  1135. * 3. PS command EXIT_PS:
  1136. * Set needtowakeup to TRUE if current state is SLEEP,
  1137. * otherwise send this command down to firmware
  1138. * immediately.
  1139. */
  1140. if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
  1141. /* Prepare to send Exit PS,
  1142. * this non PS command will be sent later */
  1143. if ((priv->psstate == PS_STATE_SLEEP)
  1144. || (priv->psstate == PS_STATE_PRE_SLEEP)
  1145. ) {
  1146. /* w/ new scheme, it will not reach here.
  1147. since it is blocked in main_thread. */
  1148. priv->needtowakeup = 1;
  1149. } else {
  1150. lbs_set_ps_mode(priv,
  1151. PS_MODE_ACTION_EXIT_PS,
  1152. false);
  1153. }
  1154. ret = 0;
  1155. goto done;
  1156. } else {
  1157. /*
  1158. * PS command. Ignore it if it is not Exit_PS.
  1159. * otherwise send it down immediately.
  1160. */
  1161. struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
  1162. lbs_deb_host(
  1163. "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
  1164. psm->action);
  1165. if (psm->action !=
  1166. cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
  1167. lbs_deb_host(
  1168. "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
  1169. lbs_complete_command(priv, cmdnode, 0);
  1170. ret = 0;
  1171. goto done;
  1172. }
  1173. if ((priv->psstate == PS_STATE_SLEEP) ||
  1174. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1175. lbs_deb_host(
  1176. "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
  1177. lbs_complete_command(priv, cmdnode, 0);
  1178. priv->needtowakeup = 1;
  1179. ret = 0;
  1180. goto done;
  1181. }
  1182. lbs_deb_host(
  1183. "EXEC_NEXT_CMD: sending EXIT_PS\n");
  1184. }
  1185. }
  1186. spin_lock_irqsave(&priv->driver_lock, flags);
  1187. list_del_init(&cmdnode->list);
  1188. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1189. lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
  1190. le16_to_cpu(cmd->command));
  1191. lbs_submit_command(priv, cmdnode);
  1192. } else {
  1193. /*
  1194. * check if in power save mode, if yes, put the device back
  1195. * to PS mode
  1196. */
  1197. #ifdef TODO
  1198. /*
  1199. * This was the old code for libertas+wext. Someone that
  1200. * understands this beast should re-code it in a sane way.
  1201. *
  1202. * I actually don't understand why this is related to WPA
  1203. * and to connection status, shouldn't powering should be
  1204. * independ of such things?
  1205. */
  1206. if ((priv->psmode != LBS802_11POWERMODECAM) &&
  1207. (priv->psstate == PS_STATE_FULL_POWER) &&
  1208. ((priv->connect_status == LBS_CONNECTED) ||
  1209. lbs_mesh_connected(priv))) {
  1210. if (priv->secinfo.WPAenabled ||
  1211. priv->secinfo.WPA2enabled) {
  1212. /* check for valid WPA group keys */
  1213. if (priv->wpa_mcast_key.len ||
  1214. priv->wpa_unicast_key.len) {
  1215. lbs_deb_host(
  1216. "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
  1217. " go back to PS_SLEEP");
  1218. lbs_set_ps_mode(priv,
  1219. PS_MODE_ACTION_ENTER_PS,
  1220. false);
  1221. }
  1222. } else {
  1223. lbs_deb_host(
  1224. "EXEC_NEXT_CMD: cmdpendingq empty, "
  1225. "go back to PS_SLEEP");
  1226. lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
  1227. false);
  1228. }
  1229. }
  1230. #endif
  1231. }
  1232. ret = 0;
  1233. done:
  1234. lbs_deb_leave(LBS_DEB_THREAD);
  1235. return ret;
  1236. }
  1237. static void lbs_send_confirmsleep(struct lbs_private *priv)
  1238. {
  1239. unsigned long flags;
  1240. int ret;
  1241. lbs_deb_enter(LBS_DEB_HOST);
  1242. lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
  1243. sizeof(confirm_sleep));
  1244. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
  1245. sizeof(confirm_sleep));
  1246. if (ret) {
  1247. netdev_alert(priv->dev, "confirm_sleep failed\n");
  1248. goto out;
  1249. }
  1250. spin_lock_irqsave(&priv->driver_lock, flags);
  1251. /* We don't get a response on the sleep-confirmation */
  1252. priv->dnld_sent = DNLD_RES_RECEIVED;
  1253. if (priv->is_host_sleep_configured) {
  1254. priv->is_host_sleep_activated = 1;
  1255. wake_up_interruptible(&priv->host_sleep_q);
  1256. }
  1257. /* If nothing to do, go back to sleep (?) */
  1258. if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
  1259. priv->psstate = PS_STATE_SLEEP;
  1260. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1261. out:
  1262. lbs_deb_leave(LBS_DEB_HOST);
  1263. }
  1264. /**
  1265. * lbs_ps_confirm_sleep - checks condition and prepares to
  1266. * send sleep confirm command to firmware if ok
  1267. *
  1268. * @priv: A pointer to &struct lbs_private structure
  1269. *
  1270. * returns: n/a
  1271. */
  1272. void lbs_ps_confirm_sleep(struct lbs_private *priv)
  1273. {
  1274. unsigned long flags =0;
  1275. int allowed = 1;
  1276. lbs_deb_enter(LBS_DEB_HOST);
  1277. spin_lock_irqsave(&priv->driver_lock, flags);
  1278. if (priv->dnld_sent) {
  1279. allowed = 0;
  1280. lbs_deb_host("dnld_sent was set\n");
  1281. }
  1282. /* In-progress command? */
  1283. if (priv->cur_cmd) {
  1284. allowed = 0;
  1285. lbs_deb_host("cur_cmd was set\n");
  1286. }
  1287. /* Pending events or command responses? */
  1288. if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
  1289. allowed = 0;
  1290. lbs_deb_host("pending events or command responses\n");
  1291. }
  1292. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1293. if (allowed) {
  1294. lbs_deb_host("sending lbs_ps_confirm_sleep\n");
  1295. lbs_send_confirmsleep(priv);
  1296. } else {
  1297. lbs_deb_host("sleep confirm has been delayed\n");
  1298. }
  1299. lbs_deb_leave(LBS_DEB_HOST);
  1300. }
  1301. /**
  1302. * lbs_set_tpc_cfg - Configures the transmission power control functionality
  1303. *
  1304. * @priv: A pointer to &struct lbs_private structure
  1305. * @enable: Transmission power control enable
  1306. * @p0: Power level when link quality is good (dBm).
  1307. * @p1: Power level when link quality is fair (dBm).
  1308. * @p2: Power level when link quality is poor (dBm).
  1309. * @usesnr: Use Signal to Noise Ratio in TPC
  1310. *
  1311. * returns: 0 on success
  1312. */
  1313. int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
  1314. int8_t p2, int usesnr)
  1315. {
  1316. struct cmd_ds_802_11_tpc_cfg cmd;
  1317. int ret;
  1318. memset(&cmd, 0, sizeof(cmd));
  1319. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1320. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1321. cmd.enable = !!enable;
  1322. cmd.usesnr = !!usesnr;
  1323. cmd.P0 = p0;
  1324. cmd.P1 = p1;
  1325. cmd.P2 = p2;
  1326. ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
  1327. return ret;
  1328. }
  1329. /**
  1330. * lbs_set_power_adapt_cfg - Configures the power adaptation settings
  1331. *
  1332. * @priv: A pointer to &struct lbs_private structure
  1333. * @enable: Power adaptation enable
  1334. * @p0: Power level for 1, 2, 5.5 and 11 Mbps (dBm).
  1335. * @p1: Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
  1336. * @p2: Power level for 48 and 54 Mbps (dBm).
  1337. *
  1338. * returns: 0 on Success
  1339. */
  1340. int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
  1341. int8_t p1, int8_t p2)
  1342. {
  1343. struct cmd_ds_802_11_pa_cfg cmd;
  1344. int ret;
  1345. memset(&cmd, 0, sizeof(cmd));
  1346. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1347. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1348. cmd.enable = !!enable;
  1349. cmd.P0 = p0;
  1350. cmd.P1 = p1;
  1351. cmd.P2 = p2;
  1352. ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
  1353. return ret;
  1354. }
  1355. struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
  1356. uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
  1357. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1358. unsigned long callback_arg)
  1359. {
  1360. struct cmd_ctrl_node *cmdnode;
  1361. lbs_deb_enter(LBS_DEB_HOST);
  1362. if (priv->surpriseremoved) {
  1363. lbs_deb_host("PREP_CMD: card removed\n");
  1364. cmdnode = ERR_PTR(-ENOENT);
  1365. goto done;
  1366. }
  1367. /* No commands are allowed in Deep Sleep until we toggle the GPIO
  1368. * to wake up the card and it has signaled that it's ready.
  1369. */
  1370. if (!priv->is_auto_deep_sleep_enabled) {
  1371. if (priv->is_deep_sleep) {
  1372. lbs_deb_cmd("command not allowed in deep sleep\n");
  1373. cmdnode = ERR_PTR(-EBUSY);
  1374. goto done;
  1375. }
  1376. }
  1377. cmdnode = lbs_get_free_cmd_node(priv);
  1378. if (cmdnode == NULL) {
  1379. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  1380. /* Wake up main thread to execute next command */
  1381. wake_up(&priv->waitq);
  1382. cmdnode = ERR_PTR(-ENOBUFS);
  1383. goto done;
  1384. }
  1385. cmdnode->callback = callback;
  1386. cmdnode->callback_arg = callback_arg;
  1387. /* Copy the incoming command to the buffer */
  1388. memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
  1389. /* Set command, clean result, move to buffer */
  1390. cmdnode->cmdbuf->command = cpu_to_le16(command);
  1391. cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
  1392. cmdnode->cmdbuf->result = 0;
  1393. lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
  1394. cmdnode->cmdwaitqwoken = 0;
  1395. lbs_queue_cmd(priv, cmdnode);
  1396. wake_up(&priv->waitq);
  1397. done:
  1398. lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
  1399. return cmdnode;
  1400. }
  1401. void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
  1402. struct cmd_header *in_cmd, int in_cmd_size)
  1403. {
  1404. lbs_deb_enter(LBS_DEB_CMD);
  1405. __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1406. lbs_cmd_async_callback, 0);
  1407. lbs_deb_leave(LBS_DEB_CMD);
  1408. }
  1409. int __lbs_cmd(struct lbs_private *priv, uint16_t command,
  1410. struct cmd_header *in_cmd, int in_cmd_size,
  1411. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1412. unsigned long callback_arg)
  1413. {
  1414. struct cmd_ctrl_node *cmdnode;
  1415. unsigned long flags;
  1416. int ret = 0;
  1417. lbs_deb_enter(LBS_DEB_HOST);
  1418. cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1419. callback, callback_arg);
  1420. if (IS_ERR(cmdnode)) {
  1421. ret = PTR_ERR(cmdnode);
  1422. goto done;
  1423. }
  1424. might_sleep();
  1425. /*
  1426. * Be careful with signals here. A signal may be received as the system
  1427. * goes into suspend or resume. We do not want this to interrupt the
  1428. * command, so we perform an uninterruptible sleep.
  1429. */
  1430. wait_event(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
  1431. spin_lock_irqsave(&priv->driver_lock, flags);
  1432. ret = cmdnode->result;
  1433. if (ret)
  1434. netdev_info(priv->dev, "PREP_CMD: command 0x%04x failed: %d\n",
  1435. command, ret);
  1436. __lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1437. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1438. done:
  1439. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1440. return ret;
  1441. }
  1442. EXPORT_SYMBOL_GPL(__lbs_cmd);