cmd.c 51 KB

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