cmd.c 42 KB

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