cmd.c 42 KB

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