cmd.c 40 KB

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