cmd.c 53 KB

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