cmd.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  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 <net/iw_handler.h>
  6. #include <net/lib80211.h>
  7. #include <linux/kfifo.h>
  8. #include "host.h"
  9. #include "decl.h"
  10. #include "defs.h"
  11. #include "dev.h"
  12. #include "assoc.h"
  13. #include "wext.h"
  14. #include "cmd.h"
  15. static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
  16. /**
  17. * @brief Simple callback that copies response back into command
  18. *
  19. * @param priv A pointer to struct lbs_private structure
  20. * @param extra A pointer to the original command structure for which
  21. * 'resp' is a response
  22. * @param resp A pointer to the command response
  23. *
  24. * @return 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. * @brief Simple callback that ignores the result. Use this if
  38. * you just want to send a command to the hardware, but don't
  39. * care for the result.
  40. *
  41. * @param priv ignored
  42. * @param extra ignored
  43. * @param resp ignored
  44. *
  45. * @return 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. * @brief Checks whether a command is allowed in Power Save mode
  54. *
  55. * @param command the command ID
  56. * @return 1 if allowed, 0 if not allowed
  57. */
  58. static u8 is_command_allowed_in_ps(u16 cmd)
  59. {
  60. switch (cmd) {
  61. case CMD_802_11_RSSI:
  62. return 1;
  63. default:
  64. break;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * @brief This function checks if the command is allowed.
  70. *
  71. * @param priv A pointer to lbs_private structure
  72. * @return allowed or not allowed.
  73. */
  74. static int lbs_is_cmd_allowed(struct lbs_private *priv)
  75. {
  76. int ret = 1;
  77. lbs_deb_enter(LBS_DEB_CMD);
  78. if (!priv->is_auto_deep_sleep_enabled) {
  79. if (priv->is_deep_sleep) {
  80. lbs_deb_cmd("command not allowed in deep sleep\n");
  81. ret = 0;
  82. }
  83. }
  84. lbs_deb_leave(LBS_DEB_CMD);
  85. return ret;
  86. }
  87. /**
  88. * @brief Updates the hardware details like MAC address and regulatory region
  89. *
  90. * @param priv A pointer to struct lbs_private structure
  91. *
  92. * @return 0 on success, error on failure
  93. */
  94. int lbs_update_hw_spec(struct lbs_private *priv)
  95. {
  96. struct cmd_ds_get_hw_spec cmd;
  97. int ret = -1;
  98. u32 i;
  99. lbs_deb_enter(LBS_DEB_CMD);
  100. memset(&cmd, 0, sizeof(cmd));
  101. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  102. memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
  103. ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
  104. if (ret)
  105. goto out;
  106. priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
  107. /* The firmware release is in an interesting format: the patch
  108. * level is in the most significant nibble ... so fix that: */
  109. priv->fwrelease = le32_to_cpu(cmd.fwrelease);
  110. priv->fwrelease = (priv->fwrelease << 8) |
  111. (priv->fwrelease >> 24 & 0xff);
  112. /* Some firmware capabilities:
  113. * CF card firmware 5.0.16p0: cap 0x00000303
  114. * USB dongle firmware 5.110.17p2: cap 0x00000303
  115. */
  116. lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
  117. cmd.permanentaddr,
  118. priv->fwrelease >> 24 & 0xff,
  119. priv->fwrelease >> 16 & 0xff,
  120. priv->fwrelease >> 8 & 0xff,
  121. priv->fwrelease & 0xff,
  122. priv->fwcapinfo);
  123. lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
  124. cmd.hwifversion, cmd.version);
  125. /* Determine mesh_fw_ver from fwrelease and fwcapinfo */
  126. /* 5.0.16p0 9.0.0.p0 is known to NOT support any mesh */
  127. /* 5.110.22 have mesh command with 0xa3 command id */
  128. /* 10.0.0.p0 FW brings in mesh config command with different id */
  129. /* Check FW version MSB and initialize mesh_fw_ver */
  130. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  131. priv->mesh_fw_ver = MESH_FW_OLD;
  132. else if ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  133. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK))
  134. priv->mesh_fw_ver = MESH_FW_NEW;
  135. else
  136. priv->mesh_fw_ver = MESH_NONE;
  137. /* Clamp region code to 8-bit since FW spec indicates that it should
  138. * only ever be 8-bit, even though the field size is 16-bit. Some firmware
  139. * returns non-zero high 8 bits here.
  140. *
  141. * Firmware version 4.0.102 used in CF8381 has region code shifted. We
  142. * need to check for this problem and handle it properly.
  143. */
  144. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
  145. priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
  146. else
  147. priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
  148. for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
  149. /* use the region code to search for the index */
  150. if (priv->regioncode == lbs_region_code_to_index[i])
  151. break;
  152. }
  153. /* if it's unidentified region code, use the default (USA) */
  154. if (i >= MRVDRV_MAX_REGION_CODE) {
  155. priv->regioncode = 0x10;
  156. lbs_pr_info("unidentified region code; using the default (USA)\n");
  157. }
  158. if (priv->current_addr[0] == 0xff)
  159. memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
  160. memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
  161. if (priv->mesh_dev)
  162. memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
  163. if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
  164. ret = -1;
  165. goto out;
  166. }
  167. out:
  168. lbs_deb_leave(LBS_DEB_CMD);
  169. return ret;
  170. }
  171. int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
  172. struct wol_config *p_wol_config)
  173. {
  174. struct cmd_ds_host_sleep cmd_config;
  175. int ret;
  176. cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
  177. cmd_config.criteria = cpu_to_le32(criteria);
  178. cmd_config.gpio = priv->wol_gpio;
  179. cmd_config.gap = priv->wol_gap;
  180. if (p_wol_config != NULL)
  181. memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
  182. sizeof(struct wol_config));
  183. else
  184. cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
  185. ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
  186. if (!ret) {
  187. if (criteria) {
  188. lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
  189. priv->wol_criteria = criteria;
  190. } else
  191. memcpy((uint8_t *) p_wol_config,
  192. (uint8_t *)&cmd_config.wol_conf,
  193. sizeof(struct wol_config));
  194. } else {
  195. lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
  196. }
  197. return ret;
  198. }
  199. EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
  200. static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
  201. u16 cmd_action)
  202. {
  203. struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
  204. lbs_deb_enter(LBS_DEB_CMD);
  205. cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
  206. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
  207. S_DS_GEN);
  208. psm->action = cpu_to_le16(cmd_action);
  209. psm->multipledtim = 0;
  210. switch (cmd_action) {
  211. case CMD_SUBCMD_ENTER_PS:
  212. lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
  213. psm->locallisteninterval = 0;
  214. psm->nullpktinterval = 0;
  215. psm->multipledtim =
  216. cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
  217. break;
  218. case CMD_SUBCMD_EXIT_PS:
  219. lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
  220. break;
  221. case CMD_SUBCMD_SLEEP_CONFIRMED:
  222. lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
  223. break;
  224. default:
  225. break;
  226. }
  227. lbs_deb_leave(LBS_DEB_CMD);
  228. return 0;
  229. }
  230. int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
  231. struct sleep_params *sp)
  232. {
  233. struct cmd_ds_802_11_sleep_params cmd;
  234. int ret;
  235. lbs_deb_enter(LBS_DEB_CMD);
  236. if (cmd_action == CMD_ACT_GET) {
  237. memset(&cmd, 0, sizeof(cmd));
  238. } else {
  239. cmd.error = cpu_to_le16(sp->sp_error);
  240. cmd.offset = cpu_to_le16(sp->sp_offset);
  241. cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
  242. cmd.calcontrol = sp->sp_calcontrol;
  243. cmd.externalsleepclk = sp->sp_extsleepclk;
  244. cmd.reserved = cpu_to_le16(sp->sp_reserved);
  245. }
  246. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  247. cmd.action = cpu_to_le16(cmd_action);
  248. ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
  249. if (!ret) {
  250. lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
  251. "calcontrol 0x%x extsleepclk 0x%x\n",
  252. le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
  253. le16_to_cpu(cmd.stabletime), cmd.calcontrol,
  254. cmd.externalsleepclk);
  255. sp->sp_error = le16_to_cpu(cmd.error);
  256. sp->sp_offset = le16_to_cpu(cmd.offset);
  257. sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
  258. sp->sp_calcontrol = cmd.calcontrol;
  259. sp->sp_extsleepclk = cmd.externalsleepclk;
  260. sp->sp_reserved = le16_to_cpu(cmd.reserved);
  261. }
  262. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  263. return 0;
  264. }
  265. static int lbs_wait_for_ds_awake(struct lbs_private *priv)
  266. {
  267. int ret = 0;
  268. lbs_deb_enter(LBS_DEB_CMD);
  269. if (priv->is_deep_sleep) {
  270. if (!wait_event_interruptible_timeout(priv->ds_awake_q,
  271. !priv->is_deep_sleep, (10 * HZ))) {
  272. lbs_pr_err("ds_awake_q: timer expired\n");
  273. ret = -1;
  274. }
  275. }
  276. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  277. return ret;
  278. }
  279. int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
  280. {
  281. int ret = 0;
  282. lbs_deb_enter(LBS_DEB_CMD);
  283. if (deep_sleep) {
  284. if (priv->is_deep_sleep != 1) {
  285. lbs_deb_cmd("deep sleep: sleep\n");
  286. BUG_ON(!priv->enter_deep_sleep);
  287. ret = priv->enter_deep_sleep(priv);
  288. if (!ret) {
  289. netif_stop_queue(priv->dev);
  290. netif_carrier_off(priv->dev);
  291. }
  292. } else {
  293. lbs_pr_err("deep sleep: already enabled\n");
  294. }
  295. } else {
  296. if (priv->is_deep_sleep) {
  297. lbs_deb_cmd("deep sleep: wakeup\n");
  298. BUG_ON(!priv->exit_deep_sleep);
  299. ret = priv->exit_deep_sleep(priv);
  300. if (!ret) {
  301. ret = lbs_wait_for_ds_awake(priv);
  302. if (ret)
  303. lbs_pr_err("deep sleep: wakeup"
  304. "failed\n");
  305. }
  306. }
  307. }
  308. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  309. return ret;
  310. }
  311. int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
  312. struct assoc_request *assoc)
  313. {
  314. struct cmd_ds_802_11_set_wep cmd;
  315. int ret = 0;
  316. lbs_deb_enter(LBS_DEB_CMD);
  317. memset(&cmd, 0, sizeof(cmd));
  318. cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
  319. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  320. cmd.action = cpu_to_le16(cmd_action);
  321. if (cmd_action == CMD_ACT_ADD) {
  322. int i;
  323. /* default tx key index */
  324. cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
  325. CMD_WEP_KEY_INDEX_MASK);
  326. /* Copy key types and material to host command structure */
  327. for (i = 0; i < 4; i++) {
  328. struct enc_key *pkey = &assoc->wep_keys[i];
  329. switch (pkey->len) {
  330. case KEY_LEN_WEP_40:
  331. cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
  332. memmove(cmd.keymaterial[i], pkey->key, pkey->len);
  333. lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
  334. break;
  335. case KEY_LEN_WEP_104:
  336. cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
  337. memmove(cmd.keymaterial[i], pkey->key, pkey->len);
  338. lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
  339. break;
  340. case 0:
  341. break;
  342. default:
  343. lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
  344. i, pkey->len);
  345. ret = -1;
  346. goto done;
  347. break;
  348. }
  349. }
  350. } else if (cmd_action == CMD_ACT_REMOVE) {
  351. /* ACT_REMOVE clears _all_ WEP keys */
  352. /* default tx key index */
  353. cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
  354. CMD_WEP_KEY_INDEX_MASK);
  355. lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
  356. }
  357. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  358. done:
  359. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  360. return ret;
  361. }
  362. int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
  363. uint16_t *enable)
  364. {
  365. struct cmd_ds_802_11_enable_rsn cmd;
  366. int ret;
  367. lbs_deb_enter(LBS_DEB_CMD);
  368. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  369. cmd.action = cpu_to_le16(cmd_action);
  370. if (cmd_action == CMD_ACT_GET)
  371. cmd.enable = 0;
  372. else {
  373. if (*enable)
  374. cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
  375. else
  376. cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
  377. lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
  378. }
  379. ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
  380. if (!ret && cmd_action == CMD_ACT_GET)
  381. *enable = le16_to_cpu(cmd.enable);
  382. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  383. return ret;
  384. }
  385. static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
  386. struct enc_key *key)
  387. {
  388. lbs_deb_enter(LBS_DEB_CMD);
  389. if (key->flags & KEY_INFO_WPA_ENABLED)
  390. keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
  391. if (key->flags & KEY_INFO_WPA_UNICAST)
  392. keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
  393. if (key->flags & KEY_INFO_WPA_MCAST)
  394. keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
  395. keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
  396. keyparam->keytypeid = cpu_to_le16(key->type);
  397. keyparam->keylen = cpu_to_le16(key->len);
  398. memcpy(keyparam->key, key->key, key->len);
  399. /* Length field doesn't include the {type,length} header */
  400. keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
  401. lbs_deb_leave(LBS_DEB_CMD);
  402. }
  403. int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
  404. struct assoc_request *assoc)
  405. {
  406. struct cmd_ds_802_11_key_material cmd;
  407. int ret = 0;
  408. int index = 0;
  409. lbs_deb_enter(LBS_DEB_CMD);
  410. cmd.action = cpu_to_le16(cmd_action);
  411. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  412. if (cmd_action == CMD_ACT_GET) {
  413. cmd.hdr.size = cpu_to_le16(S_DS_GEN + 2);
  414. } else {
  415. memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
  416. if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
  417. set_one_wpa_key(&cmd.keyParamSet[index],
  418. &assoc->wpa_unicast_key);
  419. index++;
  420. }
  421. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
  422. set_one_wpa_key(&cmd.keyParamSet[index],
  423. &assoc->wpa_mcast_key);
  424. index++;
  425. }
  426. /* The common header and as many keys as we included */
  427. cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
  428. keyParamSet[index]));
  429. }
  430. ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
  431. /* Copy the returned key to driver private data */
  432. if (!ret && cmd_action == CMD_ACT_GET) {
  433. void *buf_ptr = cmd.keyParamSet;
  434. void *resp_end = &(&cmd)[1];
  435. while (buf_ptr < resp_end) {
  436. struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
  437. struct enc_key *key;
  438. uint16_t param_set_len = le16_to_cpu(keyparam->length);
  439. uint16_t key_len = le16_to_cpu(keyparam->keylen);
  440. uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
  441. uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
  442. void *end;
  443. end = (void *)keyparam + sizeof(keyparam->type)
  444. + sizeof(keyparam->length) + param_set_len;
  445. /* Make sure we don't access past the end of the IEs */
  446. if (end > resp_end)
  447. break;
  448. if (key_flags & KEY_INFO_WPA_UNICAST)
  449. key = &priv->wpa_unicast_key;
  450. else if (key_flags & KEY_INFO_WPA_MCAST)
  451. key = &priv->wpa_mcast_key;
  452. else
  453. break;
  454. /* Copy returned key into driver */
  455. memset(key, 0, sizeof(struct enc_key));
  456. if (key_len > sizeof(key->key))
  457. break;
  458. key->type = key_type;
  459. key->flags = key_flags;
  460. key->len = key_len;
  461. memcpy(key->key, keyparam->key, key->len);
  462. buf_ptr = end + 1;
  463. }
  464. }
  465. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  466. return ret;
  467. }
  468. /**
  469. * @brief Set an SNMP MIB value
  470. *
  471. * @param priv A pointer to struct lbs_private structure
  472. * @param oid The OID to set in the firmware
  473. * @param val Value to set the OID to
  474. *
  475. * @return 0 on success, error on failure
  476. */
  477. int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
  478. {
  479. struct cmd_ds_802_11_snmp_mib cmd;
  480. int ret;
  481. lbs_deb_enter(LBS_DEB_CMD);
  482. memset(&cmd, 0, sizeof (cmd));
  483. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  484. cmd.action = cpu_to_le16(CMD_ACT_SET);
  485. cmd.oid = cpu_to_le16((u16) oid);
  486. switch (oid) {
  487. case SNMP_MIB_OID_BSS_TYPE:
  488. cmd.bufsize = cpu_to_le16(sizeof(u8));
  489. cmd.value[0] = (val == IW_MODE_ADHOC) ? 2 : 1;
  490. break;
  491. case SNMP_MIB_OID_11D_ENABLE:
  492. case SNMP_MIB_OID_FRAG_THRESHOLD:
  493. case SNMP_MIB_OID_RTS_THRESHOLD:
  494. case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
  495. case SNMP_MIB_OID_LONG_RETRY_LIMIT:
  496. cmd.bufsize = cpu_to_le16(sizeof(u16));
  497. *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
  498. break;
  499. default:
  500. lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
  501. ret = -EINVAL;
  502. goto out;
  503. }
  504. lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
  505. le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
  506. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  507. out:
  508. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  509. return ret;
  510. }
  511. /**
  512. * @brief Get an SNMP MIB value
  513. *
  514. * @param priv A pointer to struct lbs_private structure
  515. * @param oid The OID to retrieve from the firmware
  516. * @param out_val Location for the returned value
  517. *
  518. * @return 0 on success, error on failure
  519. */
  520. int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
  521. {
  522. struct cmd_ds_802_11_snmp_mib cmd;
  523. int ret;
  524. lbs_deb_enter(LBS_DEB_CMD);
  525. memset(&cmd, 0, sizeof (cmd));
  526. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  527. cmd.action = cpu_to_le16(CMD_ACT_GET);
  528. cmd.oid = cpu_to_le16(oid);
  529. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  530. if (ret)
  531. goto out;
  532. switch (le16_to_cpu(cmd.bufsize)) {
  533. case sizeof(u8):
  534. if (oid == SNMP_MIB_OID_BSS_TYPE) {
  535. if (cmd.value[0] == 2)
  536. *out_val = IW_MODE_ADHOC;
  537. else
  538. *out_val = IW_MODE_INFRA;
  539. } else
  540. *out_val = cmd.value[0];
  541. break;
  542. case sizeof(u16):
  543. *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
  544. break;
  545. default:
  546. lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
  547. oid, le16_to_cpu(cmd.bufsize));
  548. break;
  549. }
  550. out:
  551. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  552. return ret;
  553. }
  554. /**
  555. * @brief Get the min, max, and current TX power
  556. *
  557. * @param priv A pointer to struct lbs_private structure
  558. * @param curlevel Current power level in dBm
  559. * @param minlevel Minimum supported power level in dBm (optional)
  560. * @param maxlevel Maximum supported power level in dBm (optional)
  561. *
  562. * @return 0 on success, error on failure
  563. */
  564. int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
  565. s16 *maxlevel)
  566. {
  567. struct cmd_ds_802_11_rf_tx_power cmd;
  568. int ret;
  569. lbs_deb_enter(LBS_DEB_CMD);
  570. memset(&cmd, 0, sizeof(cmd));
  571. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  572. cmd.action = cpu_to_le16(CMD_ACT_GET);
  573. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  574. if (ret == 0) {
  575. *curlevel = le16_to_cpu(cmd.curlevel);
  576. if (minlevel)
  577. *minlevel = cmd.minlevel;
  578. if (maxlevel)
  579. *maxlevel = cmd.maxlevel;
  580. }
  581. lbs_deb_leave(LBS_DEB_CMD);
  582. return ret;
  583. }
  584. /**
  585. * @brief Set the TX power
  586. *
  587. * @param priv A pointer to struct lbs_private structure
  588. * @param dbm The desired power level in dBm
  589. *
  590. * @return 0 on success, error on failure
  591. */
  592. int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
  593. {
  594. struct cmd_ds_802_11_rf_tx_power cmd;
  595. int ret;
  596. lbs_deb_enter(LBS_DEB_CMD);
  597. memset(&cmd, 0, sizeof(cmd));
  598. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  599. cmd.action = cpu_to_le16(CMD_ACT_SET);
  600. cmd.curlevel = cpu_to_le16(dbm);
  601. lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
  602. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  603. lbs_deb_leave(LBS_DEB_CMD);
  604. return ret;
  605. }
  606. static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
  607. u16 cmd_action, void *pdata_buf)
  608. {
  609. struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
  610. cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
  611. cmd->size =
  612. cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
  613. S_DS_GEN);
  614. monitor->action = cpu_to_le16(cmd_action);
  615. if (cmd_action == CMD_ACT_SET) {
  616. monitor->mode =
  617. cpu_to_le16((u16) (*(u32 *) pdata_buf));
  618. }
  619. return 0;
  620. }
  621. static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
  622. {
  623. /* Bit Rate
  624. * 15:13 Reserved
  625. * 12 54 Mbps
  626. * 11 48 Mbps
  627. * 10 36 Mbps
  628. * 9 24 Mbps
  629. * 8 18 Mbps
  630. * 7 12 Mbps
  631. * 6 9 Mbps
  632. * 5 6 Mbps
  633. * 4 Reserved
  634. * 3 11 Mbps
  635. * 2 5.5 Mbps
  636. * 1 2 Mbps
  637. * 0 1 Mbps
  638. **/
  639. uint16_t ratemask;
  640. int i = lbs_data_rate_to_fw_index(rate);
  641. if (lower_rates_ok)
  642. ratemask = (0x1fef >> (12 - i));
  643. else
  644. ratemask = (1 << i);
  645. return cpu_to_le16(ratemask);
  646. }
  647. int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
  648. uint16_t cmd_action)
  649. {
  650. struct cmd_ds_802_11_rate_adapt_rateset cmd;
  651. int ret;
  652. lbs_deb_enter(LBS_DEB_CMD);
  653. if (!priv->cur_rate && !priv->enablehwauto)
  654. return -EINVAL;
  655. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  656. cmd.action = cpu_to_le16(cmd_action);
  657. cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
  658. cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
  659. ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
  660. if (!ret && cmd_action == CMD_ACT_GET) {
  661. priv->ratebitmap = le16_to_cpu(cmd.bitmap);
  662. priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
  663. }
  664. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  665. return ret;
  666. }
  667. EXPORT_SYMBOL_GPL(lbs_cmd_802_11_rate_adapt_rateset);
  668. /**
  669. * @brief Set the data rate
  670. *
  671. * @param priv A pointer to struct lbs_private structure
  672. * @param rate The desired data rate, or 0 to clear a locked rate
  673. *
  674. * @return 0 on success, error on failure
  675. */
  676. int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
  677. {
  678. struct cmd_ds_802_11_data_rate cmd;
  679. int ret = 0;
  680. lbs_deb_enter(LBS_DEB_CMD);
  681. memset(&cmd, 0, sizeof(cmd));
  682. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  683. if (rate > 0) {
  684. cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
  685. cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
  686. if (cmd.rates[0] == 0) {
  687. lbs_deb_cmd("DATA_RATE: invalid requested rate of"
  688. " 0x%02X\n", rate);
  689. ret = 0;
  690. goto out;
  691. }
  692. lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
  693. } else {
  694. cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
  695. lbs_deb_cmd("DATA_RATE: setting auto\n");
  696. }
  697. ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
  698. if (ret)
  699. goto out;
  700. lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof (cmd));
  701. /* FIXME: get actual rates FW can do if this command actually returns
  702. * all data rates supported.
  703. */
  704. priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
  705. lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
  706. out:
  707. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  708. return ret;
  709. }
  710. /**
  711. * @brief Get the radio channel
  712. *
  713. * @param priv A pointer to struct lbs_private structure
  714. *
  715. * @return The channel on success, error on failure
  716. */
  717. static int lbs_get_channel(struct lbs_private *priv)
  718. {
  719. struct cmd_ds_802_11_rf_channel cmd;
  720. int ret = 0;
  721. lbs_deb_enter(LBS_DEB_CMD);
  722. memset(&cmd, 0, sizeof(cmd));
  723. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  724. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
  725. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  726. if (ret)
  727. goto out;
  728. ret = le16_to_cpu(cmd.channel);
  729. lbs_deb_cmd("current radio channel is %d\n", ret);
  730. out:
  731. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  732. return ret;
  733. }
  734. int lbs_update_channel(struct lbs_private *priv)
  735. {
  736. int ret;
  737. /* the channel in f/w could be out of sync; get the current channel */
  738. lbs_deb_enter(LBS_DEB_ASSOC);
  739. ret = lbs_get_channel(priv);
  740. if (ret > 0) {
  741. priv->curbssparams.channel = ret;
  742. ret = 0;
  743. }
  744. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  745. return ret;
  746. }
  747. /**
  748. * @brief Set the radio channel
  749. *
  750. * @param priv A pointer to struct lbs_private structure
  751. * @param channel The desired channel, or 0 to clear a locked channel
  752. *
  753. * @return 0 on success, error on failure
  754. */
  755. int lbs_set_channel(struct lbs_private *priv, u8 channel)
  756. {
  757. struct cmd_ds_802_11_rf_channel cmd;
  758. #ifdef DEBUG
  759. u8 old_channel = priv->curbssparams.channel;
  760. #endif
  761. int ret = 0;
  762. lbs_deb_enter(LBS_DEB_CMD);
  763. memset(&cmd, 0, sizeof(cmd));
  764. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  765. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
  766. cmd.channel = cpu_to_le16(channel);
  767. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  768. if (ret)
  769. goto out;
  770. priv->curbssparams.channel = (uint8_t) le16_to_cpu(cmd.channel);
  771. lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
  772. priv->curbssparams.channel);
  773. out:
  774. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  775. return ret;
  776. }
  777. static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
  778. struct cmd_ds_command *cmd)
  779. {
  780. lbs_deb_enter(LBS_DEB_CMD);
  781. cmd->command = cpu_to_le16(CMD_802_11_RSSI);
  782. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
  783. cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
  784. /* reset Beacon SNR/NF/RSSI values */
  785. priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
  786. priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
  787. priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
  788. priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
  789. priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
  790. priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
  791. lbs_deb_leave(LBS_DEB_CMD);
  792. return 0;
  793. }
  794. static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
  795. u8 cmd_action, void *pdata_buf)
  796. {
  797. struct lbs_offset_value *offval;
  798. lbs_deb_enter(LBS_DEB_CMD);
  799. offval = (struct lbs_offset_value *)pdata_buf;
  800. switch (le16_to_cpu(cmdptr->command)) {
  801. case CMD_MAC_REG_ACCESS:
  802. {
  803. struct cmd_ds_mac_reg_access *macreg;
  804. cmdptr->size =
  805. cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
  806. + S_DS_GEN);
  807. macreg =
  808. (struct cmd_ds_mac_reg_access *)&cmdptr->params.
  809. macreg;
  810. macreg->action = cpu_to_le16(cmd_action);
  811. macreg->offset = cpu_to_le16((u16) offval->offset);
  812. macreg->value = cpu_to_le32(offval->value);
  813. break;
  814. }
  815. case CMD_BBP_REG_ACCESS:
  816. {
  817. struct cmd_ds_bbp_reg_access *bbpreg;
  818. cmdptr->size =
  819. cpu_to_le16(sizeof
  820. (struct cmd_ds_bbp_reg_access)
  821. + S_DS_GEN);
  822. bbpreg =
  823. (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
  824. bbpreg;
  825. bbpreg->action = cpu_to_le16(cmd_action);
  826. bbpreg->offset = cpu_to_le16((u16) offval->offset);
  827. bbpreg->value = (u8) offval->value;
  828. break;
  829. }
  830. case CMD_RF_REG_ACCESS:
  831. {
  832. struct cmd_ds_rf_reg_access *rfreg;
  833. cmdptr->size =
  834. cpu_to_le16(sizeof
  835. (struct cmd_ds_rf_reg_access) +
  836. S_DS_GEN);
  837. rfreg =
  838. (struct cmd_ds_rf_reg_access *)&cmdptr->params.
  839. rfreg;
  840. rfreg->action = cpu_to_le16(cmd_action);
  841. rfreg->offset = cpu_to_le16((u16) offval->offset);
  842. rfreg->value = (u8) offval->value;
  843. break;
  844. }
  845. default:
  846. break;
  847. }
  848. lbs_deb_leave(LBS_DEB_CMD);
  849. return 0;
  850. }
  851. static int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
  852. u16 cmd_action, void *pdata_buf)
  853. {
  854. struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
  855. lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
  856. cmd->command = cpu_to_le16(CMD_BT_ACCESS);
  857. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
  858. cmd->result = 0;
  859. bt_access->action = cpu_to_le16(cmd_action);
  860. switch (cmd_action) {
  861. case CMD_ACT_BT_ACCESS_ADD:
  862. memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
  863. lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr", bt_access->addr1, 6);
  864. break;
  865. case CMD_ACT_BT_ACCESS_DEL:
  866. memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
  867. lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr", bt_access->addr1, 6);
  868. break;
  869. case CMD_ACT_BT_ACCESS_LIST:
  870. bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
  871. break;
  872. case CMD_ACT_BT_ACCESS_RESET:
  873. break;
  874. case CMD_ACT_BT_ACCESS_SET_INVERT:
  875. bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
  876. break;
  877. case CMD_ACT_BT_ACCESS_GET_INVERT:
  878. break;
  879. default:
  880. break;
  881. }
  882. lbs_deb_leave(LBS_DEB_CMD);
  883. return 0;
  884. }
  885. static int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
  886. u16 cmd_action, void *pdata_buf)
  887. {
  888. struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
  889. lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
  890. cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
  891. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
  892. cmd->result = 0;
  893. if (pdata_buf)
  894. memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
  895. else
  896. memset(fwt_access, 0, sizeof(*fwt_access));
  897. fwt_access->action = cpu_to_le16(cmd_action);
  898. lbs_deb_leave(LBS_DEB_CMD);
  899. return 0;
  900. }
  901. int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
  902. struct cmd_ds_mesh_access *cmd)
  903. {
  904. int ret;
  905. lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
  906. cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
  907. cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
  908. cmd->hdr.result = 0;
  909. cmd->action = cpu_to_le16(cmd_action);
  910. ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
  911. lbs_deb_leave(LBS_DEB_CMD);
  912. return ret;
  913. }
  914. static int __lbs_mesh_config_send(struct lbs_private *priv,
  915. struct cmd_ds_mesh_config *cmd,
  916. uint16_t action, uint16_t type)
  917. {
  918. int ret;
  919. u16 command = CMD_MESH_CONFIG_OLD;
  920. lbs_deb_enter(LBS_DEB_CMD);
  921. /*
  922. * Command id is 0xac for v10 FW along with mesh interface
  923. * id in bits 14-13-12.
  924. */
  925. if (priv->mesh_fw_ver == MESH_FW_NEW)
  926. command = CMD_MESH_CONFIG |
  927. (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
  928. cmd->hdr.command = cpu_to_le16(command);
  929. cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
  930. cmd->hdr.result = 0;
  931. cmd->type = cpu_to_le16(type);
  932. cmd->action = cpu_to_le16(action);
  933. ret = lbs_cmd_with_response(priv, command, cmd);
  934. lbs_deb_leave(LBS_DEB_CMD);
  935. return ret;
  936. }
  937. int lbs_mesh_config_send(struct lbs_private *priv,
  938. struct cmd_ds_mesh_config *cmd,
  939. uint16_t action, uint16_t type)
  940. {
  941. int ret;
  942. if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
  943. return -EOPNOTSUPP;
  944. ret = __lbs_mesh_config_send(priv, cmd, action, type);
  945. return ret;
  946. }
  947. /* This function is the CMD_MESH_CONFIG legacy function. It only handles the
  948. * START and STOP actions. The extended actions supported by CMD_MESH_CONFIG
  949. * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
  950. * lbs_mesh_config_send.
  951. */
  952. int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
  953. {
  954. struct cmd_ds_mesh_config cmd;
  955. struct mrvl_meshie *ie;
  956. DECLARE_SSID_BUF(ssid);
  957. memset(&cmd, 0, sizeof(cmd));
  958. cmd.channel = cpu_to_le16(chan);
  959. ie = (struct mrvl_meshie *)cmd.data;
  960. switch (action) {
  961. case CMD_ACT_MESH_CONFIG_START:
  962. ie->id = WLAN_EID_GENERIC;
  963. ie->val.oui[0] = 0x00;
  964. ie->val.oui[1] = 0x50;
  965. ie->val.oui[2] = 0x43;
  966. ie->val.type = MARVELL_MESH_IE_TYPE;
  967. ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
  968. ie->val.version = MARVELL_MESH_IE_VERSION;
  969. ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
  970. ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
  971. ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
  972. ie->val.mesh_id_len = priv->mesh_ssid_len;
  973. memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
  974. ie->len = sizeof(struct mrvl_meshie_val) -
  975. IEEE80211_MAX_SSID_LEN + priv->mesh_ssid_len;
  976. cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
  977. break;
  978. case CMD_ACT_MESH_CONFIG_STOP:
  979. break;
  980. default:
  981. return -1;
  982. }
  983. lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
  984. action, priv->mesh_tlv, chan,
  985. print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
  986. return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
  987. }
  988. static int lbs_cmd_bcn_ctrl(struct lbs_private * priv,
  989. struct cmd_ds_command *cmd,
  990. u16 cmd_action)
  991. {
  992. struct cmd_ds_802_11_beacon_control
  993. *bcn_ctrl = &cmd->params.bcn_ctrl;
  994. lbs_deb_enter(LBS_DEB_CMD);
  995. cmd->size =
  996. cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
  997. + S_DS_GEN);
  998. cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
  999. bcn_ctrl->action = cpu_to_le16(cmd_action);
  1000. bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
  1001. bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
  1002. lbs_deb_leave(LBS_DEB_CMD);
  1003. return 0;
  1004. }
  1005. static void lbs_queue_cmd(struct lbs_private *priv,
  1006. struct cmd_ctrl_node *cmdnode)
  1007. {
  1008. unsigned long flags;
  1009. int addtail = 1;
  1010. lbs_deb_enter(LBS_DEB_HOST);
  1011. if (!cmdnode) {
  1012. lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  1013. goto done;
  1014. }
  1015. if (!cmdnode->cmdbuf->size) {
  1016. lbs_deb_host("DNLD_CMD: cmd size is zero\n");
  1017. goto done;
  1018. }
  1019. cmdnode->result = 0;
  1020. /* Exit_PS command needs to be queued in the header always. */
  1021. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
  1022. struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
  1023. if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  1024. if (priv->psstate != PS_STATE_FULL_POWER)
  1025. addtail = 0;
  1026. }
  1027. }
  1028. spin_lock_irqsave(&priv->driver_lock, flags);
  1029. if (addtail)
  1030. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  1031. else
  1032. list_add(&cmdnode->list, &priv->cmdpendingq);
  1033. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1034. lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  1035. le16_to_cpu(cmdnode->cmdbuf->command));
  1036. done:
  1037. lbs_deb_leave(LBS_DEB_HOST);
  1038. }
  1039. static void lbs_submit_command(struct lbs_private *priv,
  1040. struct cmd_ctrl_node *cmdnode)
  1041. {
  1042. unsigned long flags;
  1043. struct cmd_header *cmd;
  1044. uint16_t cmdsize;
  1045. uint16_t command;
  1046. int timeo = 3 * HZ;
  1047. int ret;
  1048. lbs_deb_enter(LBS_DEB_HOST);
  1049. cmd = cmdnode->cmdbuf;
  1050. spin_lock_irqsave(&priv->driver_lock, flags);
  1051. priv->cur_cmd = cmdnode;
  1052. priv->cur_cmd_retcode = 0;
  1053. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1054. cmdsize = le16_to_cpu(cmd->size);
  1055. command = le16_to_cpu(cmd->command);
  1056. /* These commands take longer */
  1057. if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
  1058. timeo = 5 * HZ;
  1059. lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  1060. command, le16_to_cpu(cmd->seqnum), cmdsize);
  1061. lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  1062. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  1063. if (ret) {
  1064. lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
  1065. /* Let the timer kick in and retry, and potentially reset
  1066. the whole thing if the condition persists */
  1067. timeo = HZ/4;
  1068. }
  1069. if (command == CMD_802_11_DEEP_SLEEP) {
  1070. if (priv->is_auto_deep_sleep_enabled) {
  1071. priv->wakeup_dev_required = 1;
  1072. priv->dnld_sent = 0;
  1073. }
  1074. priv->is_deep_sleep = 1;
  1075. lbs_complete_command(priv, cmdnode, 0);
  1076. } else {
  1077. /* Setup the timer after transmit command */
  1078. mod_timer(&priv->command_timer, jiffies + timeo);
  1079. }
  1080. lbs_deb_leave(LBS_DEB_HOST);
  1081. }
  1082. /**
  1083. * This function inserts command node to cmdfreeq
  1084. * after cleans it. Requires priv->driver_lock held.
  1085. */
  1086. static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  1087. struct cmd_ctrl_node *cmdnode)
  1088. {
  1089. lbs_deb_enter(LBS_DEB_HOST);
  1090. if (!cmdnode)
  1091. goto out;
  1092. cmdnode->callback = NULL;
  1093. cmdnode->callback_arg = 0;
  1094. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  1095. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  1096. out:
  1097. lbs_deb_leave(LBS_DEB_HOST);
  1098. }
  1099. static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  1100. struct cmd_ctrl_node *ptempcmd)
  1101. {
  1102. unsigned long flags;
  1103. spin_lock_irqsave(&priv->driver_lock, flags);
  1104. __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
  1105. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1106. }
  1107. void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
  1108. int result)
  1109. {
  1110. if (cmd == priv->cur_cmd)
  1111. priv->cur_cmd_retcode = result;
  1112. cmd->result = result;
  1113. cmd->cmdwaitqwoken = 1;
  1114. wake_up_interruptible(&cmd->cmdwait_q);
  1115. if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
  1116. __lbs_cleanup_and_insert_cmd(priv, cmd);
  1117. priv->cur_cmd = NULL;
  1118. }
  1119. int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
  1120. {
  1121. struct cmd_ds_802_11_radio_control cmd;
  1122. int ret = -EINVAL;
  1123. lbs_deb_enter(LBS_DEB_CMD);
  1124. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1125. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1126. /* Only v8 and below support setting the preamble */
  1127. if (priv->fwrelease < 0x09000000) {
  1128. switch (preamble) {
  1129. case RADIO_PREAMBLE_SHORT:
  1130. if (!(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
  1131. goto out;
  1132. /* Fall through */
  1133. case RADIO_PREAMBLE_AUTO:
  1134. case RADIO_PREAMBLE_LONG:
  1135. cmd.control = cpu_to_le16(preamble);
  1136. break;
  1137. default:
  1138. goto out;
  1139. }
  1140. }
  1141. if (radio_on)
  1142. cmd.control |= cpu_to_le16(0x1);
  1143. else {
  1144. cmd.control &= cpu_to_le16(~0x1);
  1145. priv->txpower_cur = 0;
  1146. }
  1147. lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
  1148. radio_on ? "ON" : "OFF", preamble);
  1149. priv->radio_on = radio_on;
  1150. ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  1151. out:
  1152. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  1153. return ret;
  1154. }
  1155. void lbs_set_mac_control(struct lbs_private *priv)
  1156. {
  1157. struct cmd_ds_mac_control cmd;
  1158. lbs_deb_enter(LBS_DEB_CMD);
  1159. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1160. cmd.action = cpu_to_le16(priv->mac_control);
  1161. cmd.reserved = 0;
  1162. lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
  1163. lbs_deb_leave(LBS_DEB_CMD);
  1164. }
  1165. /**
  1166. * @brief This function prepare the command before send to firmware.
  1167. *
  1168. * @param priv A pointer to struct lbs_private structure
  1169. * @param cmd_no command number
  1170. * @param cmd_action command action: GET or SET
  1171. * @param wait_option wait option: wait response or not
  1172. * @param cmd_oid cmd oid: treated as sub command
  1173. * @param pdata_buf A pointer to informaion buffer
  1174. * @return 0 or -1
  1175. */
  1176. int lbs_prepare_and_send_command(struct lbs_private *priv,
  1177. u16 cmd_no,
  1178. u16 cmd_action,
  1179. u16 wait_option, u32 cmd_oid, void *pdata_buf)
  1180. {
  1181. int ret = 0;
  1182. struct cmd_ctrl_node *cmdnode;
  1183. struct cmd_ds_command *cmdptr;
  1184. unsigned long flags;
  1185. lbs_deb_enter(LBS_DEB_HOST);
  1186. if (!priv) {
  1187. lbs_deb_host("PREP_CMD: priv is NULL\n");
  1188. ret = -1;
  1189. goto done;
  1190. }
  1191. if (priv->surpriseremoved) {
  1192. lbs_deb_host("PREP_CMD: card removed\n");
  1193. ret = -1;
  1194. goto done;
  1195. }
  1196. if (!lbs_is_cmd_allowed(priv)) {
  1197. ret = -EBUSY;
  1198. goto done;
  1199. }
  1200. cmdnode = lbs_get_cmd_ctrl_node(priv);
  1201. if (cmdnode == NULL) {
  1202. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  1203. /* Wake up main thread to execute next command */
  1204. wake_up_interruptible(&priv->waitq);
  1205. ret = -1;
  1206. goto done;
  1207. }
  1208. cmdnode->callback = NULL;
  1209. cmdnode->callback_arg = (unsigned long)pdata_buf;
  1210. cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
  1211. lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
  1212. /* Set sequence number, command and INT option */
  1213. priv->seqnum++;
  1214. cmdptr->seqnum = cpu_to_le16(priv->seqnum);
  1215. cmdptr->command = cpu_to_le16(cmd_no);
  1216. cmdptr->result = 0;
  1217. switch (cmd_no) {
  1218. case CMD_802_11_PS_MODE:
  1219. ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
  1220. break;
  1221. case CMD_MAC_REG_ACCESS:
  1222. case CMD_BBP_REG_ACCESS:
  1223. case CMD_RF_REG_ACCESS:
  1224. ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
  1225. break;
  1226. case CMD_802_11_MONITOR_MODE:
  1227. ret = lbs_cmd_802_11_monitor_mode(cmdptr,
  1228. cmd_action, pdata_buf);
  1229. break;
  1230. case CMD_802_11_RSSI:
  1231. ret = lbs_cmd_802_11_rssi(priv, cmdptr);
  1232. break;
  1233. case CMD_802_11_SET_AFC:
  1234. case CMD_802_11_GET_AFC:
  1235. cmdptr->command = cpu_to_le16(cmd_no);
  1236. cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
  1237. S_DS_GEN);
  1238. memmove(&cmdptr->params.afc,
  1239. pdata_buf, sizeof(struct cmd_ds_802_11_afc));
  1240. ret = 0;
  1241. goto done;
  1242. case CMD_802_11_TPC_CFG:
  1243. cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
  1244. cmdptr->size =
  1245. cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
  1246. S_DS_GEN);
  1247. memmove(&cmdptr->params.tpccfg,
  1248. pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
  1249. ret = 0;
  1250. break;
  1251. case CMD_802_11_LED_GPIO_CTRL:
  1252. {
  1253. struct mrvl_ie_ledgpio *gpio =
  1254. (struct mrvl_ie_ledgpio*)
  1255. cmdptr->params.ledgpio.data;
  1256. memmove(&cmdptr->params.ledgpio,
  1257. pdata_buf,
  1258. sizeof(struct cmd_ds_802_11_led_ctrl));
  1259. cmdptr->command =
  1260. cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
  1261. #define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
  1262. cmdptr->size =
  1263. cpu_to_le16(le16_to_cpu(gpio->header.len)
  1264. + S_DS_GEN
  1265. + ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
  1266. gpio->header.len = gpio->header.len;
  1267. ret = 0;
  1268. break;
  1269. }
  1270. case CMD_BT_ACCESS:
  1271. ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
  1272. break;
  1273. case CMD_FWT_ACCESS:
  1274. ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
  1275. break;
  1276. case CMD_GET_TSF:
  1277. cmdptr->command = cpu_to_le16(CMD_GET_TSF);
  1278. cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
  1279. S_DS_GEN);
  1280. ret = 0;
  1281. break;
  1282. case CMD_802_11_BEACON_CTRL:
  1283. ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
  1284. break;
  1285. case CMD_802_11_DEEP_SLEEP:
  1286. cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
  1287. cmdptr->size = cpu_to_le16(S_DS_GEN);
  1288. break;
  1289. default:
  1290. lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
  1291. ret = -1;
  1292. break;
  1293. }
  1294. /* return error, since the command preparation failed */
  1295. if (ret != 0) {
  1296. lbs_deb_host("PREP_CMD: command preparation failed\n");
  1297. lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1298. ret = -1;
  1299. goto done;
  1300. }
  1301. cmdnode->cmdwaitqwoken = 0;
  1302. lbs_queue_cmd(priv, cmdnode);
  1303. wake_up_interruptible(&priv->waitq);
  1304. if (wait_option & CMD_OPTION_WAITFORRSP) {
  1305. lbs_deb_host("PREP_CMD: wait for response\n");
  1306. might_sleep();
  1307. wait_event_interruptible(cmdnode->cmdwait_q,
  1308. cmdnode->cmdwaitqwoken);
  1309. }
  1310. spin_lock_irqsave(&priv->driver_lock, flags);
  1311. if (priv->cur_cmd_retcode) {
  1312. lbs_deb_host("PREP_CMD: command failed with return code %d\n",
  1313. priv->cur_cmd_retcode);
  1314. priv->cur_cmd_retcode = 0;
  1315. ret = -1;
  1316. }
  1317. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1318. done:
  1319. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1320. return ret;
  1321. }
  1322. /**
  1323. * @brief This function allocates the command buffer and link
  1324. * it to command free queue.
  1325. *
  1326. * @param priv A pointer to struct lbs_private structure
  1327. * @return 0 or -1
  1328. */
  1329. int lbs_allocate_cmd_buffer(struct lbs_private *priv)
  1330. {
  1331. int ret = 0;
  1332. u32 bufsize;
  1333. u32 i;
  1334. struct cmd_ctrl_node *cmdarray;
  1335. lbs_deb_enter(LBS_DEB_HOST);
  1336. /* Allocate and initialize the command array */
  1337. bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
  1338. if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
  1339. lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
  1340. ret = -1;
  1341. goto done;
  1342. }
  1343. priv->cmd_array = cmdarray;
  1344. /* Allocate and initialize each command buffer in the command array */
  1345. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1346. cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
  1347. if (!cmdarray[i].cmdbuf) {
  1348. lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
  1349. ret = -1;
  1350. goto done;
  1351. }
  1352. }
  1353. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1354. init_waitqueue_head(&cmdarray[i].cmdwait_q);
  1355. lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
  1356. }
  1357. ret = 0;
  1358. done:
  1359. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1360. return ret;
  1361. }
  1362. /**
  1363. * @brief This function frees the command buffer.
  1364. *
  1365. * @param priv A pointer to struct lbs_private structure
  1366. * @return 0 or -1
  1367. */
  1368. int lbs_free_cmd_buffer(struct lbs_private *priv)
  1369. {
  1370. struct cmd_ctrl_node *cmdarray;
  1371. unsigned int i;
  1372. lbs_deb_enter(LBS_DEB_HOST);
  1373. /* need to check if cmd array is allocated or not */
  1374. if (priv->cmd_array == NULL) {
  1375. lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
  1376. goto done;
  1377. }
  1378. cmdarray = priv->cmd_array;
  1379. /* Release shared memory buffers */
  1380. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1381. if (cmdarray[i].cmdbuf) {
  1382. kfree(cmdarray[i].cmdbuf);
  1383. cmdarray[i].cmdbuf = NULL;
  1384. }
  1385. }
  1386. /* Release cmd_ctrl_node */
  1387. if (priv->cmd_array) {
  1388. kfree(priv->cmd_array);
  1389. priv->cmd_array = NULL;
  1390. }
  1391. done:
  1392. lbs_deb_leave(LBS_DEB_HOST);
  1393. return 0;
  1394. }
  1395. /**
  1396. * @brief This function gets a free command node if available in
  1397. * command free queue.
  1398. *
  1399. * @param priv A pointer to struct lbs_private structure
  1400. * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
  1401. */
  1402. static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
  1403. {
  1404. struct cmd_ctrl_node *tempnode;
  1405. unsigned long flags;
  1406. lbs_deb_enter(LBS_DEB_HOST);
  1407. if (!priv)
  1408. return NULL;
  1409. spin_lock_irqsave(&priv->driver_lock, flags);
  1410. if (!list_empty(&priv->cmdfreeq)) {
  1411. tempnode = list_first_entry(&priv->cmdfreeq,
  1412. struct cmd_ctrl_node, list);
  1413. list_del(&tempnode->list);
  1414. } else {
  1415. lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
  1416. tempnode = NULL;
  1417. }
  1418. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1419. lbs_deb_leave(LBS_DEB_HOST);
  1420. return tempnode;
  1421. }
  1422. /**
  1423. * @brief This function executes next command in command
  1424. * pending queue. It will put firmware back to PS mode
  1425. * if applicable.
  1426. *
  1427. * @param priv A pointer to struct lbs_private structure
  1428. * @return 0 or -1
  1429. */
  1430. int lbs_execute_next_command(struct lbs_private *priv)
  1431. {
  1432. struct cmd_ctrl_node *cmdnode = NULL;
  1433. struct cmd_header *cmd;
  1434. unsigned long flags;
  1435. int ret = 0;
  1436. /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
  1437. * only caller to us is lbs_thread() and we get even when a
  1438. * data packet is received */
  1439. lbs_deb_enter(LBS_DEB_THREAD);
  1440. spin_lock_irqsave(&priv->driver_lock, flags);
  1441. if (priv->cur_cmd) {
  1442. lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
  1443. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1444. ret = -1;
  1445. goto done;
  1446. }
  1447. if (!list_empty(&priv->cmdpendingq)) {
  1448. cmdnode = list_first_entry(&priv->cmdpendingq,
  1449. struct cmd_ctrl_node, list);
  1450. }
  1451. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1452. if (cmdnode) {
  1453. cmd = cmdnode->cmdbuf;
  1454. if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
  1455. if ((priv->psstate == PS_STATE_SLEEP) ||
  1456. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1457. lbs_deb_host(
  1458. "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
  1459. le16_to_cpu(cmd->command),
  1460. priv->psstate);
  1461. ret = -1;
  1462. goto done;
  1463. }
  1464. lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
  1465. "0x%04x in psstate %d\n",
  1466. le16_to_cpu(cmd->command), priv->psstate);
  1467. } else if (priv->psstate != PS_STATE_FULL_POWER) {
  1468. /*
  1469. * 1. Non-PS command:
  1470. * Queue it. set needtowakeup to TRUE if current state
  1471. * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
  1472. * 2. PS command but not Exit_PS:
  1473. * Ignore it.
  1474. * 3. PS command Exit_PS:
  1475. * Set needtowakeup to TRUE if current state is SLEEP,
  1476. * otherwise send this command down to firmware
  1477. * immediately.
  1478. */
  1479. if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
  1480. /* Prepare to send Exit PS,
  1481. * this non PS command will be sent later */
  1482. if ((priv->psstate == PS_STATE_SLEEP)
  1483. || (priv->psstate == PS_STATE_PRE_SLEEP)
  1484. ) {
  1485. /* w/ new scheme, it will not reach here.
  1486. since it is blocked in main_thread. */
  1487. priv->needtowakeup = 1;
  1488. } else
  1489. lbs_ps_wakeup(priv, 0);
  1490. ret = 0;
  1491. goto done;
  1492. } else {
  1493. /*
  1494. * PS command. Ignore it if it is not Exit_PS.
  1495. * otherwise send it down immediately.
  1496. */
  1497. struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
  1498. lbs_deb_host(
  1499. "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
  1500. psm->action);
  1501. if (psm->action !=
  1502. cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  1503. lbs_deb_host(
  1504. "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
  1505. list_del(&cmdnode->list);
  1506. spin_lock_irqsave(&priv->driver_lock, flags);
  1507. lbs_complete_command(priv, cmdnode, 0);
  1508. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1509. ret = 0;
  1510. goto done;
  1511. }
  1512. if ((priv->psstate == PS_STATE_SLEEP) ||
  1513. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1514. lbs_deb_host(
  1515. "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
  1516. list_del(&cmdnode->list);
  1517. spin_lock_irqsave(&priv->driver_lock, flags);
  1518. lbs_complete_command(priv, cmdnode, 0);
  1519. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1520. priv->needtowakeup = 1;
  1521. ret = 0;
  1522. goto done;
  1523. }
  1524. lbs_deb_host(
  1525. "EXEC_NEXT_CMD: sending EXIT_PS\n");
  1526. }
  1527. }
  1528. list_del(&cmdnode->list);
  1529. lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
  1530. le16_to_cpu(cmd->command));
  1531. lbs_submit_command(priv, cmdnode);
  1532. } else {
  1533. /*
  1534. * check if in power save mode, if yes, put the device back
  1535. * to PS mode
  1536. */
  1537. if ((priv->psmode != LBS802_11POWERMODECAM) &&
  1538. (priv->psstate == PS_STATE_FULL_POWER) &&
  1539. ((priv->connect_status == LBS_CONNECTED) ||
  1540. (priv->mesh_connect_status == LBS_CONNECTED))) {
  1541. if (priv->secinfo.WPAenabled ||
  1542. priv->secinfo.WPA2enabled) {
  1543. /* check for valid WPA group keys */
  1544. if (priv->wpa_mcast_key.len ||
  1545. priv->wpa_unicast_key.len) {
  1546. lbs_deb_host(
  1547. "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
  1548. " go back to PS_SLEEP");
  1549. lbs_ps_sleep(priv, 0);
  1550. }
  1551. } else {
  1552. lbs_deb_host(
  1553. "EXEC_NEXT_CMD: cmdpendingq empty, "
  1554. "go back to PS_SLEEP");
  1555. lbs_ps_sleep(priv, 0);
  1556. }
  1557. }
  1558. }
  1559. ret = 0;
  1560. done:
  1561. lbs_deb_leave(LBS_DEB_THREAD);
  1562. return ret;
  1563. }
  1564. void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
  1565. {
  1566. union iwreq_data iwrq;
  1567. u8 buf[50];
  1568. lbs_deb_enter(LBS_DEB_WEXT);
  1569. memset(&iwrq, 0, sizeof(union iwreq_data));
  1570. memset(buf, 0, sizeof(buf));
  1571. snprintf(buf, sizeof(buf) - 1, "%s", str);
  1572. iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;
  1573. /* Send Event to upper layer */
  1574. lbs_deb_wext("event indication string %s\n", (char *)buf);
  1575. lbs_deb_wext("event indication length %d\n", iwrq.data.length);
  1576. lbs_deb_wext("sending wireless event IWEVCUSTOM for %s\n", str);
  1577. wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
  1578. lbs_deb_leave(LBS_DEB_WEXT);
  1579. }
  1580. static void lbs_send_confirmsleep(struct lbs_private *priv)
  1581. {
  1582. unsigned long flags;
  1583. int ret;
  1584. lbs_deb_enter(LBS_DEB_HOST);
  1585. lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
  1586. sizeof(confirm_sleep));
  1587. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
  1588. sizeof(confirm_sleep));
  1589. if (ret) {
  1590. lbs_pr_alert("confirm_sleep failed\n");
  1591. goto out;
  1592. }
  1593. spin_lock_irqsave(&priv->driver_lock, flags);
  1594. /* We don't get a response on the sleep-confirmation */
  1595. priv->dnld_sent = DNLD_RES_RECEIVED;
  1596. /* If nothing to do, go back to sleep (?) */
  1597. if (!__kfifo_len(priv->event_fifo) && !priv->resp_len[priv->resp_idx])
  1598. priv->psstate = PS_STATE_SLEEP;
  1599. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1600. out:
  1601. lbs_deb_leave(LBS_DEB_HOST);
  1602. }
  1603. void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
  1604. {
  1605. lbs_deb_enter(LBS_DEB_HOST);
  1606. /*
  1607. * PS is currently supported only in Infrastructure mode
  1608. * Remove this check if it is to be supported in IBSS mode also
  1609. */
  1610. lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  1611. CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
  1612. lbs_deb_leave(LBS_DEB_HOST);
  1613. }
  1614. /**
  1615. * @brief This function sends Exit_PS command to firmware.
  1616. *
  1617. * @param priv A pointer to struct lbs_private structure
  1618. * @param wait_option wait response or not
  1619. * @return n/a
  1620. */
  1621. void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
  1622. {
  1623. __le32 Localpsmode;
  1624. lbs_deb_enter(LBS_DEB_HOST);
  1625. Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
  1626. lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  1627. CMD_SUBCMD_EXIT_PS,
  1628. wait_option, 0, &Localpsmode);
  1629. lbs_deb_leave(LBS_DEB_HOST);
  1630. }
  1631. /**
  1632. * @brief This function checks condition and prepares to
  1633. * send sleep confirm command to firmware if ok.
  1634. *
  1635. * @param priv A pointer to struct lbs_private structure
  1636. * @param psmode Power Saving mode
  1637. * @return n/a
  1638. */
  1639. void lbs_ps_confirm_sleep(struct lbs_private *priv)
  1640. {
  1641. unsigned long flags =0;
  1642. int allowed = 1;
  1643. lbs_deb_enter(LBS_DEB_HOST);
  1644. spin_lock_irqsave(&priv->driver_lock, flags);
  1645. if (priv->dnld_sent) {
  1646. allowed = 0;
  1647. lbs_deb_host("dnld_sent was set\n");
  1648. }
  1649. /* In-progress command? */
  1650. if (priv->cur_cmd) {
  1651. allowed = 0;
  1652. lbs_deb_host("cur_cmd was set\n");
  1653. }
  1654. /* Pending events or command responses? */
  1655. if (__kfifo_len(priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
  1656. allowed = 0;
  1657. lbs_deb_host("pending events or command responses\n");
  1658. }
  1659. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1660. if (allowed) {
  1661. lbs_deb_host("sending lbs_ps_confirm_sleep\n");
  1662. lbs_send_confirmsleep(priv);
  1663. } else {
  1664. lbs_deb_host("sleep confirm has been delayed\n");
  1665. }
  1666. lbs_deb_leave(LBS_DEB_HOST);
  1667. }
  1668. /**
  1669. * @brief Configures the transmission power control functionality.
  1670. *
  1671. * @param priv A pointer to struct lbs_private structure
  1672. * @param enable Transmission power control enable
  1673. * @param p0 Power level when link quality is good (dBm).
  1674. * @param p1 Power level when link quality is fair (dBm).
  1675. * @param p2 Power level when link quality is poor (dBm).
  1676. * @param usesnr Use Signal to Noise Ratio in TPC
  1677. *
  1678. * @return 0 on success
  1679. */
  1680. int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
  1681. int8_t p2, int usesnr)
  1682. {
  1683. struct cmd_ds_802_11_tpc_cfg cmd;
  1684. int ret;
  1685. memset(&cmd, 0, sizeof(cmd));
  1686. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1687. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1688. cmd.enable = !!enable;
  1689. cmd.usesnr = !!usesnr;
  1690. cmd.P0 = p0;
  1691. cmd.P1 = p1;
  1692. cmd.P2 = p2;
  1693. ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
  1694. return ret;
  1695. }
  1696. /**
  1697. * @brief Configures the power adaptation settings.
  1698. *
  1699. * @param priv A pointer to struct lbs_private structure
  1700. * @param enable Power adaptation enable
  1701. * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
  1702. * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
  1703. * @param p2 Power level for 48 and 54 Mbps (dBm).
  1704. *
  1705. * @return 0 on Success
  1706. */
  1707. int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
  1708. int8_t p1, int8_t p2)
  1709. {
  1710. struct cmd_ds_802_11_pa_cfg cmd;
  1711. int ret;
  1712. memset(&cmd, 0, sizeof(cmd));
  1713. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1714. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1715. cmd.enable = !!enable;
  1716. cmd.P0 = p0;
  1717. cmd.P1 = p1;
  1718. cmd.P2 = p2;
  1719. ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
  1720. return ret;
  1721. }
  1722. struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
  1723. uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
  1724. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1725. unsigned long callback_arg)
  1726. {
  1727. struct cmd_ctrl_node *cmdnode;
  1728. lbs_deb_enter(LBS_DEB_HOST);
  1729. if (priv->surpriseremoved) {
  1730. lbs_deb_host("PREP_CMD: card removed\n");
  1731. cmdnode = ERR_PTR(-ENOENT);
  1732. goto done;
  1733. }
  1734. if (!lbs_is_cmd_allowed(priv)) {
  1735. cmdnode = ERR_PTR(-EBUSY);
  1736. goto done;
  1737. }
  1738. cmdnode = lbs_get_cmd_ctrl_node(priv);
  1739. if (cmdnode == NULL) {
  1740. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  1741. /* Wake up main thread to execute next command */
  1742. wake_up_interruptible(&priv->waitq);
  1743. cmdnode = ERR_PTR(-ENOBUFS);
  1744. goto done;
  1745. }
  1746. cmdnode->callback = callback;
  1747. cmdnode->callback_arg = callback_arg;
  1748. /* Copy the incoming command to the buffer */
  1749. memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
  1750. /* Set sequence number, clean result, move to buffer */
  1751. priv->seqnum++;
  1752. cmdnode->cmdbuf->command = cpu_to_le16(command);
  1753. cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
  1754. cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
  1755. cmdnode->cmdbuf->result = 0;
  1756. lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
  1757. cmdnode->cmdwaitqwoken = 0;
  1758. lbs_queue_cmd(priv, cmdnode);
  1759. wake_up_interruptible(&priv->waitq);
  1760. done:
  1761. lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
  1762. return cmdnode;
  1763. }
  1764. void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
  1765. struct cmd_header *in_cmd, int in_cmd_size)
  1766. {
  1767. lbs_deb_enter(LBS_DEB_CMD);
  1768. __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1769. lbs_cmd_async_callback, 0);
  1770. lbs_deb_leave(LBS_DEB_CMD);
  1771. }
  1772. int __lbs_cmd(struct lbs_private *priv, uint16_t command,
  1773. struct cmd_header *in_cmd, int in_cmd_size,
  1774. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1775. unsigned long callback_arg)
  1776. {
  1777. struct cmd_ctrl_node *cmdnode;
  1778. unsigned long flags;
  1779. int ret = 0;
  1780. lbs_deb_enter(LBS_DEB_HOST);
  1781. cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1782. callback, callback_arg);
  1783. if (IS_ERR(cmdnode)) {
  1784. ret = PTR_ERR(cmdnode);
  1785. goto done;
  1786. }
  1787. might_sleep();
  1788. wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
  1789. spin_lock_irqsave(&priv->driver_lock, flags);
  1790. ret = cmdnode->result;
  1791. if (ret)
  1792. lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
  1793. command, ret);
  1794. __lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1795. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1796. done:
  1797. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1798. return ret;
  1799. }
  1800. EXPORT_SYMBOL_GPL(__lbs_cmd);