cmd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * Copyright (C) 2008, cozybit Inc.
  3. * Copyright (C) 2003-2006, Marvell International Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. */
  10. #include "deb_defs.h"
  11. #include "libertas_tf.h"
  12. static const struct channel_range channel_ranges[] = {
  13. { LBTF_REGDOMAIN_US, 1, 12 },
  14. { LBTF_REGDOMAIN_CA, 1, 12 },
  15. { LBTF_REGDOMAIN_EU, 1, 14 },
  16. { LBTF_REGDOMAIN_JP, 1, 14 },
  17. { LBTF_REGDOMAIN_SP, 1, 14 },
  18. { LBTF_REGDOMAIN_FR, 1, 14 },
  19. };
  20. static u16 lbtf_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  21. {
  22. LBTF_REGDOMAIN_US, LBTF_REGDOMAIN_CA, LBTF_REGDOMAIN_EU,
  23. LBTF_REGDOMAIN_SP, LBTF_REGDOMAIN_FR, LBTF_REGDOMAIN_JP,
  24. };
  25. static struct cmd_ctrl_node *lbtf_get_cmd_ctrl_node(struct lbtf_private *priv);
  26. /**
  27. * lbtf_cmd_copyback - Simple callback that copies response back into command
  28. *
  29. * @priv A pointer to struct lbtf_private structure
  30. * @extra A pointer to the original command structure for which
  31. * 'resp' is a response
  32. * @resp A pointer to the command response
  33. *
  34. * Returns: 0 on success, error on failure
  35. */
  36. int lbtf_cmd_copyback(struct lbtf_private *priv, unsigned long extra,
  37. struct cmd_header *resp)
  38. {
  39. struct cmd_header *buf = (void *)extra;
  40. uint16_t copy_len;
  41. copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
  42. memcpy(buf, resp, copy_len);
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(lbtf_cmd_copyback);
  46. #define CHAN_TO_IDX(chan) ((chan) - 1)
  47. static void lbtf_geo_init(struct lbtf_private *priv)
  48. {
  49. const struct channel_range *range = channel_ranges;
  50. u8 ch;
  51. int i;
  52. for (i = 0; i < ARRAY_SIZE(channel_ranges); i++)
  53. if (channel_ranges[i].regdomain == priv->regioncode) {
  54. range = &channel_ranges[i];
  55. break;
  56. }
  57. for (ch = priv->range.start; ch < priv->range.end; ch++)
  58. priv->channels[CHAN_TO_IDX(ch)].flags = 0;
  59. }
  60. /**
  61. * lbtf_update_hw_spec: Updates the hardware details.
  62. *
  63. * @priv A pointer to struct lbtf_private structure
  64. *
  65. * Returns: 0 on success, error on failure
  66. */
  67. int lbtf_update_hw_spec(struct lbtf_private *priv)
  68. {
  69. struct cmd_ds_get_hw_spec cmd;
  70. int ret = -1;
  71. u32 i;
  72. lbtf_deb_enter(LBTF_DEB_CMD);
  73. memset(&cmd, 0, sizeof(cmd));
  74. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  75. memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
  76. ret = lbtf_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
  77. if (ret)
  78. goto out;
  79. priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
  80. /* The firmware release is in an interesting format: the patch
  81. * level is in the most significant nibble ... so fix that: */
  82. priv->fwrelease = le32_to_cpu(cmd.fwrelease);
  83. priv->fwrelease = (priv->fwrelease << 8) |
  84. (priv->fwrelease >> 24 & 0xff);
  85. printk(KERN_INFO "libertastf: %pM, fw %u.%u.%up%u, cap 0x%08x\n",
  86. cmd.permanentaddr,
  87. priv->fwrelease >> 24 & 0xff,
  88. priv->fwrelease >> 16 & 0xff,
  89. priv->fwrelease >> 8 & 0xff,
  90. priv->fwrelease & 0xff,
  91. priv->fwcapinfo);
  92. lbtf_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
  93. cmd.hwifversion, cmd.version);
  94. /* Clamp region code to 8-bit since FW spec indicates that it should
  95. * only ever be 8-bit, even though the field size is 16-bit. Some
  96. * firmware returns non-zero high 8 bits here.
  97. */
  98. priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
  99. for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
  100. /* use the region code to search for the index */
  101. if (priv->regioncode == lbtf_region_code_to_index[i])
  102. break;
  103. }
  104. /* if it's unidentified region code, use the default (USA) */
  105. if (i >= MRVDRV_MAX_REGION_CODE) {
  106. priv->regioncode = 0x10;
  107. pr_info("unidentified region code; using the default (USA)\n");
  108. }
  109. if (priv->current_addr[0] == 0xff)
  110. memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
  111. SET_IEEE80211_PERM_ADDR(priv->hw, priv->current_addr);
  112. lbtf_geo_init(priv);
  113. out:
  114. lbtf_deb_leave(LBTF_DEB_CMD);
  115. return ret;
  116. }
  117. /**
  118. * lbtf_set_channel: Set the radio channel
  119. *
  120. * @priv A pointer to struct lbtf_private structure
  121. * @channel The desired channel, or 0 to clear a locked channel
  122. *
  123. * Returns: 0 on success, error on failure
  124. */
  125. int lbtf_set_channel(struct lbtf_private *priv, u8 channel)
  126. {
  127. int ret = 0;
  128. struct cmd_ds_802_11_rf_channel cmd;
  129. lbtf_deb_enter(LBTF_DEB_CMD);
  130. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  131. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
  132. cmd.channel = cpu_to_le16(channel);
  133. ret = lbtf_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  134. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
  135. return ret;
  136. }
  137. int lbtf_beacon_set(struct lbtf_private *priv, struct sk_buff *beacon)
  138. {
  139. struct cmd_ds_802_11_beacon_set cmd;
  140. int size;
  141. lbtf_deb_enter(LBTF_DEB_CMD);
  142. if (beacon->len > MRVL_MAX_BCN_SIZE) {
  143. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", -1);
  144. return -1;
  145. }
  146. size = sizeof(cmd) - sizeof(cmd.beacon) + beacon->len;
  147. cmd.hdr.size = cpu_to_le16(size);
  148. cmd.len = cpu_to_le16(beacon->len);
  149. memcpy(cmd.beacon, (u8 *) beacon->data, beacon->len);
  150. lbtf_cmd_async(priv, CMD_802_11_BEACON_SET, &cmd.hdr, size);
  151. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", 0);
  152. return 0;
  153. }
  154. int lbtf_beacon_ctrl(struct lbtf_private *priv, bool beacon_enable,
  155. int beacon_int)
  156. {
  157. struct cmd_ds_802_11_beacon_control cmd;
  158. lbtf_deb_enter(LBTF_DEB_CMD);
  159. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  160. cmd.action = cpu_to_le16(CMD_ACT_SET);
  161. cmd.beacon_enable = cpu_to_le16(beacon_enable);
  162. cmd.beacon_period = cpu_to_le16(beacon_int);
  163. lbtf_cmd_async(priv, CMD_802_11_BEACON_CTRL, &cmd.hdr, sizeof(cmd));
  164. lbtf_deb_leave(LBTF_DEB_CMD);
  165. return 0;
  166. }
  167. static void lbtf_queue_cmd(struct lbtf_private *priv,
  168. struct cmd_ctrl_node *cmdnode)
  169. {
  170. unsigned long flags;
  171. lbtf_deb_enter(LBTF_DEB_HOST);
  172. if (!cmdnode) {
  173. lbtf_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  174. goto qcmd_done;
  175. }
  176. if (!cmdnode->cmdbuf->size) {
  177. lbtf_deb_host("DNLD_CMD: cmd size is zero\n");
  178. goto qcmd_done;
  179. }
  180. cmdnode->result = 0;
  181. spin_lock_irqsave(&priv->driver_lock, flags);
  182. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  183. spin_unlock_irqrestore(&priv->driver_lock, flags);
  184. lbtf_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  185. le16_to_cpu(cmdnode->cmdbuf->command));
  186. qcmd_done:
  187. lbtf_deb_leave(LBTF_DEB_HOST);
  188. }
  189. static void lbtf_submit_command(struct lbtf_private *priv,
  190. struct cmd_ctrl_node *cmdnode)
  191. {
  192. unsigned long flags;
  193. struct cmd_header *cmd;
  194. uint16_t cmdsize;
  195. uint16_t command;
  196. int timeo = 5 * HZ;
  197. int ret;
  198. lbtf_deb_enter(LBTF_DEB_HOST);
  199. cmd = cmdnode->cmdbuf;
  200. spin_lock_irqsave(&priv->driver_lock, flags);
  201. priv->cur_cmd = cmdnode;
  202. cmdsize = le16_to_cpu(cmd->size);
  203. command = le16_to_cpu(cmd->command);
  204. lbtf_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  205. command, le16_to_cpu(cmd->seqnum), cmdsize);
  206. lbtf_deb_hex(LBTF_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  207. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  208. spin_unlock_irqrestore(&priv->driver_lock, flags);
  209. if (ret) {
  210. pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
  211. /* Let the timer kick in and retry, and potentially reset
  212. the whole thing if the condition persists */
  213. timeo = HZ;
  214. }
  215. /* Setup the timer after transmit command */
  216. mod_timer(&priv->command_timer, jiffies + timeo);
  217. lbtf_deb_leave(LBTF_DEB_HOST);
  218. }
  219. /**
  220. * This function inserts command node to cmdfreeq
  221. * after cleans it. Requires priv->driver_lock held.
  222. */
  223. static void __lbtf_cleanup_and_insert_cmd(struct lbtf_private *priv,
  224. struct cmd_ctrl_node *cmdnode)
  225. {
  226. lbtf_deb_enter(LBTF_DEB_HOST);
  227. if (!cmdnode)
  228. goto cl_ins_out;
  229. cmdnode->callback = NULL;
  230. cmdnode->callback_arg = 0;
  231. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  232. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  233. cl_ins_out:
  234. lbtf_deb_leave(LBTF_DEB_HOST);
  235. }
  236. static void lbtf_cleanup_and_insert_cmd(struct lbtf_private *priv,
  237. struct cmd_ctrl_node *ptempcmd)
  238. {
  239. unsigned long flags;
  240. spin_lock_irqsave(&priv->driver_lock, flags);
  241. __lbtf_cleanup_and_insert_cmd(priv, ptempcmd);
  242. spin_unlock_irqrestore(&priv->driver_lock, flags);
  243. }
  244. void lbtf_complete_command(struct lbtf_private *priv, struct cmd_ctrl_node *cmd,
  245. int result)
  246. {
  247. cmd->result = result;
  248. cmd->cmdwaitqwoken = 1;
  249. wake_up_interruptible(&cmd->cmdwait_q);
  250. if (!cmd->callback)
  251. __lbtf_cleanup_and_insert_cmd(priv, cmd);
  252. priv->cur_cmd = NULL;
  253. }
  254. int lbtf_cmd_set_mac_multicast_addr(struct lbtf_private *priv)
  255. {
  256. struct cmd_ds_mac_multicast_addr cmd;
  257. lbtf_deb_enter(LBTF_DEB_CMD);
  258. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  259. cmd.action = cpu_to_le16(CMD_ACT_SET);
  260. cmd.nr_of_adrs = cpu_to_le16((u16) priv->nr_of_multicastmacaddr);
  261. lbtf_deb_cmd("MULTICAST_ADR: setting %d addresses\n", cmd.nr_of_adrs);
  262. memcpy(cmd.maclist, priv->multicastlist,
  263. priv->nr_of_multicastmacaddr * ETH_ALEN);
  264. lbtf_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &cmd.hdr, sizeof(cmd));
  265. lbtf_deb_leave(LBTF_DEB_CMD);
  266. return 0;
  267. }
  268. void lbtf_set_mode(struct lbtf_private *priv, enum lbtf_mode mode)
  269. {
  270. struct cmd_ds_set_mode cmd;
  271. lbtf_deb_enter(LBTF_DEB_WEXT);
  272. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  273. cmd.mode = cpu_to_le16(mode);
  274. lbtf_deb_wext("Switching to mode: 0x%x\n", mode);
  275. lbtf_cmd_async(priv, CMD_802_11_SET_MODE, &cmd.hdr, sizeof(cmd));
  276. lbtf_deb_leave(LBTF_DEB_WEXT);
  277. }
  278. void lbtf_set_bssid(struct lbtf_private *priv, bool activate, const u8 *bssid)
  279. {
  280. struct cmd_ds_set_bssid cmd;
  281. lbtf_deb_enter(LBTF_DEB_CMD);
  282. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  283. cmd.activate = activate ? 1 : 0;
  284. if (activate)
  285. memcpy(cmd.bssid, bssid, ETH_ALEN);
  286. lbtf_cmd_async(priv, CMD_802_11_SET_BSSID, &cmd.hdr, sizeof(cmd));
  287. lbtf_deb_leave(LBTF_DEB_CMD);
  288. }
  289. int lbtf_set_mac_address(struct lbtf_private *priv, uint8_t *mac_addr)
  290. {
  291. struct cmd_ds_802_11_mac_address cmd;
  292. lbtf_deb_enter(LBTF_DEB_CMD);
  293. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  294. cmd.action = cpu_to_le16(CMD_ACT_SET);
  295. memcpy(cmd.macadd, mac_addr, ETH_ALEN);
  296. lbtf_cmd_async(priv, CMD_802_11_MAC_ADDRESS, &cmd.hdr, sizeof(cmd));
  297. lbtf_deb_leave(LBTF_DEB_CMD);
  298. return 0;
  299. }
  300. int lbtf_set_radio_control(struct lbtf_private *priv)
  301. {
  302. int ret = 0;
  303. struct cmd_ds_802_11_radio_control cmd;
  304. lbtf_deb_enter(LBTF_DEB_CMD);
  305. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  306. cmd.action = cpu_to_le16(CMD_ACT_SET);
  307. switch (priv->preamble) {
  308. case CMD_TYPE_SHORT_PREAMBLE:
  309. cmd.control = cpu_to_le16(SET_SHORT_PREAMBLE);
  310. break;
  311. case CMD_TYPE_LONG_PREAMBLE:
  312. cmd.control = cpu_to_le16(SET_LONG_PREAMBLE);
  313. break;
  314. case CMD_TYPE_AUTO_PREAMBLE:
  315. default:
  316. cmd.control = cpu_to_le16(SET_AUTO_PREAMBLE);
  317. break;
  318. }
  319. if (priv->radioon)
  320. cmd.control |= cpu_to_le16(TURN_ON_RF);
  321. else
  322. cmd.control &= cpu_to_le16(~TURN_ON_RF);
  323. lbtf_deb_cmd("RADIO_SET: radio %d, preamble %d\n", priv->radioon,
  324. priv->preamble);
  325. ret = lbtf_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  326. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
  327. return ret;
  328. }
  329. void lbtf_set_mac_control(struct lbtf_private *priv)
  330. {
  331. struct cmd_ds_mac_control cmd;
  332. lbtf_deb_enter(LBTF_DEB_CMD);
  333. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  334. cmd.action = cpu_to_le16(priv->mac_control);
  335. cmd.reserved = 0;
  336. lbtf_cmd_async(priv, CMD_MAC_CONTROL,
  337. &cmd.hdr, sizeof(cmd));
  338. lbtf_deb_leave(LBTF_DEB_CMD);
  339. }
  340. /**
  341. * lbtf_allocate_cmd_buffer - Allocates cmd buffer, links it to free cmd queue
  342. *
  343. * @priv A pointer to struct lbtf_private structure
  344. *
  345. * Returns: 0 on success.
  346. */
  347. int lbtf_allocate_cmd_buffer(struct lbtf_private *priv)
  348. {
  349. int ret = 0;
  350. u32 bufsize;
  351. u32 i;
  352. struct cmd_ctrl_node *cmdarray;
  353. lbtf_deb_enter(LBTF_DEB_HOST);
  354. /* Allocate and initialize the command array */
  355. bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
  356. cmdarray = kzalloc(bufsize, GFP_KERNEL);
  357. if (!cmdarray) {
  358. lbtf_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
  359. ret = -1;
  360. goto done;
  361. }
  362. priv->cmd_array = cmdarray;
  363. /* Allocate and initialize each command buffer in the command array */
  364. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  365. cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
  366. if (!cmdarray[i].cmdbuf) {
  367. lbtf_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
  368. ret = -1;
  369. goto done;
  370. }
  371. }
  372. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  373. init_waitqueue_head(&cmdarray[i].cmdwait_q);
  374. lbtf_cleanup_and_insert_cmd(priv, &cmdarray[i]);
  375. }
  376. ret = 0;
  377. done:
  378. lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %d", ret);
  379. return ret;
  380. }
  381. /**
  382. * lbtf_free_cmd_buffer - Frees the cmd buffer.
  383. *
  384. * @priv A pointer to struct lbtf_private structure
  385. *
  386. * Returns: 0
  387. */
  388. int lbtf_free_cmd_buffer(struct lbtf_private *priv)
  389. {
  390. struct cmd_ctrl_node *cmdarray;
  391. unsigned int i;
  392. lbtf_deb_enter(LBTF_DEB_HOST);
  393. /* need to check if cmd array is allocated or not */
  394. if (priv->cmd_array == NULL) {
  395. lbtf_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
  396. goto done;
  397. }
  398. cmdarray = priv->cmd_array;
  399. /* Release shared memory buffers */
  400. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  401. kfree(cmdarray[i].cmdbuf);
  402. cmdarray[i].cmdbuf = NULL;
  403. }
  404. /* Release cmd_ctrl_node */
  405. kfree(priv->cmd_array);
  406. priv->cmd_array = NULL;
  407. done:
  408. lbtf_deb_leave(LBTF_DEB_HOST);
  409. return 0;
  410. }
  411. /**
  412. * lbtf_get_cmd_ctrl_node - Gets free cmd node from free cmd queue.
  413. *
  414. * @priv A pointer to struct lbtf_private structure
  415. *
  416. * Returns: pointer to a struct cmd_ctrl_node or NULL if none available.
  417. */
  418. static struct cmd_ctrl_node *lbtf_get_cmd_ctrl_node(struct lbtf_private *priv)
  419. {
  420. struct cmd_ctrl_node *tempnode;
  421. unsigned long flags;
  422. lbtf_deb_enter(LBTF_DEB_HOST);
  423. if (!priv)
  424. return NULL;
  425. spin_lock_irqsave(&priv->driver_lock, flags);
  426. if (!list_empty(&priv->cmdfreeq)) {
  427. tempnode = list_first_entry(&priv->cmdfreeq,
  428. struct cmd_ctrl_node, list);
  429. list_del(&tempnode->list);
  430. } else {
  431. lbtf_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
  432. tempnode = NULL;
  433. }
  434. spin_unlock_irqrestore(&priv->driver_lock, flags);
  435. lbtf_deb_leave(LBTF_DEB_HOST);
  436. return tempnode;
  437. }
  438. /**
  439. * lbtf_execute_next_command: execute next command in cmd pending queue.
  440. *
  441. * @priv A pointer to struct lbtf_private structure
  442. *
  443. * Returns: 0 on success.
  444. */
  445. int lbtf_execute_next_command(struct lbtf_private *priv)
  446. {
  447. struct cmd_ctrl_node *cmdnode = NULL;
  448. struct cmd_header *cmd;
  449. unsigned long flags;
  450. int ret = 0;
  451. /* Debug group is lbtf_deb_THREAD and not lbtf_deb_HOST, because the
  452. * only caller to us is lbtf_thread() and we get even when a
  453. * data packet is received */
  454. lbtf_deb_enter(LBTF_DEB_THREAD);
  455. spin_lock_irqsave(&priv->driver_lock, flags);
  456. if (priv->cur_cmd) {
  457. pr_alert("EXEC_NEXT_CMD: already processing command!\n");
  458. spin_unlock_irqrestore(&priv->driver_lock, flags);
  459. ret = -1;
  460. goto done;
  461. }
  462. if (!list_empty(&priv->cmdpendingq)) {
  463. cmdnode = list_first_entry(&priv->cmdpendingq,
  464. struct cmd_ctrl_node, list);
  465. }
  466. if (cmdnode) {
  467. cmd = cmdnode->cmdbuf;
  468. list_del(&cmdnode->list);
  469. lbtf_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
  470. le16_to_cpu(cmd->command));
  471. spin_unlock_irqrestore(&priv->driver_lock, flags);
  472. lbtf_submit_command(priv, cmdnode);
  473. } else
  474. spin_unlock_irqrestore(&priv->driver_lock, flags);
  475. ret = 0;
  476. done:
  477. lbtf_deb_leave(LBTF_DEB_THREAD);
  478. return ret;
  479. }
  480. static struct cmd_ctrl_node *__lbtf_cmd_async(struct lbtf_private *priv,
  481. uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
  482. int (*callback)(struct lbtf_private *, unsigned long,
  483. struct cmd_header *),
  484. unsigned long callback_arg)
  485. {
  486. struct cmd_ctrl_node *cmdnode;
  487. lbtf_deb_enter(LBTF_DEB_HOST);
  488. if (priv->surpriseremoved) {
  489. lbtf_deb_host("PREP_CMD: card removed\n");
  490. cmdnode = ERR_PTR(-ENOENT);
  491. goto done;
  492. }
  493. cmdnode = lbtf_get_cmd_ctrl_node(priv);
  494. if (cmdnode == NULL) {
  495. lbtf_deb_host("PREP_CMD: cmdnode is NULL\n");
  496. /* Wake up main thread to execute next command */
  497. queue_work(lbtf_wq, &priv->cmd_work);
  498. cmdnode = ERR_PTR(-ENOBUFS);
  499. goto done;
  500. }
  501. cmdnode->callback = callback;
  502. cmdnode->callback_arg = callback_arg;
  503. /* Copy the incoming command to the buffer */
  504. memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
  505. /* Set sequence number, clean result, move to buffer */
  506. priv->seqnum++;
  507. cmdnode->cmdbuf->command = cpu_to_le16(command);
  508. cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
  509. cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
  510. cmdnode->cmdbuf->result = 0;
  511. lbtf_deb_host("PREP_CMD: command 0x%04x\n", command);
  512. cmdnode->cmdwaitqwoken = 0;
  513. lbtf_queue_cmd(priv, cmdnode);
  514. queue_work(lbtf_wq, &priv->cmd_work);
  515. done:
  516. lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %p", cmdnode);
  517. return cmdnode;
  518. }
  519. void lbtf_cmd_async(struct lbtf_private *priv, uint16_t command,
  520. struct cmd_header *in_cmd, int in_cmd_size)
  521. {
  522. lbtf_deb_enter(LBTF_DEB_CMD);
  523. __lbtf_cmd_async(priv, command, in_cmd, in_cmd_size, NULL, 0);
  524. lbtf_deb_leave(LBTF_DEB_CMD);
  525. }
  526. int __lbtf_cmd(struct lbtf_private *priv, uint16_t command,
  527. struct cmd_header *in_cmd, int in_cmd_size,
  528. int (*callback)(struct lbtf_private *,
  529. unsigned long, struct cmd_header *),
  530. unsigned long callback_arg)
  531. {
  532. struct cmd_ctrl_node *cmdnode;
  533. unsigned long flags;
  534. int ret = 0;
  535. lbtf_deb_enter(LBTF_DEB_HOST);
  536. cmdnode = __lbtf_cmd_async(priv, command, in_cmd, in_cmd_size,
  537. callback, callback_arg);
  538. if (IS_ERR(cmdnode)) {
  539. ret = PTR_ERR(cmdnode);
  540. goto done;
  541. }
  542. might_sleep();
  543. ret = wait_event_interruptible(cmdnode->cmdwait_q,
  544. cmdnode->cmdwaitqwoken);
  545. if (ret) {
  546. pr_info("PREP_CMD: command 0x%04x interrupted by signal: %d\n",
  547. command, ret);
  548. goto done;
  549. }
  550. spin_lock_irqsave(&priv->driver_lock, flags);
  551. ret = cmdnode->result;
  552. if (ret)
  553. pr_info("PREP_CMD: command 0x%04x failed: %d\n",
  554. command, ret);
  555. __lbtf_cleanup_and_insert_cmd(priv, cmdnode);
  556. spin_unlock_irqrestore(&priv->driver_lock, flags);
  557. done:
  558. lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %d", ret);
  559. return ret;
  560. }
  561. EXPORT_SYMBOL_GPL(__lbtf_cmd);
  562. /* Call holding driver_lock */
  563. void lbtf_cmd_response_rx(struct lbtf_private *priv)
  564. {
  565. priv->cmd_response_rxed = 1;
  566. queue_work(lbtf_wq, &priv->cmd_work);
  567. }
  568. EXPORT_SYMBOL_GPL(lbtf_cmd_response_rx);
  569. int lbtf_process_rx_command(struct lbtf_private *priv)
  570. {
  571. uint16_t respcmd, curcmd;
  572. struct cmd_header *resp;
  573. int ret = 0;
  574. unsigned long flags;
  575. uint16_t result;
  576. lbtf_deb_enter(LBTF_DEB_CMD);
  577. mutex_lock(&priv->lock);
  578. spin_lock_irqsave(&priv->driver_lock, flags);
  579. if (!priv->cur_cmd) {
  580. ret = -1;
  581. spin_unlock_irqrestore(&priv->driver_lock, flags);
  582. goto done;
  583. }
  584. resp = (void *)priv->cmd_resp_buff;
  585. curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
  586. respcmd = le16_to_cpu(resp->command);
  587. result = le16_to_cpu(resp->result);
  588. if (net_ratelimit())
  589. pr_info("libertastf: cmd response 0x%04x, seq %d, size %d\n",
  590. respcmd, le16_to_cpu(resp->seqnum),
  591. le16_to_cpu(resp->size));
  592. if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
  593. spin_unlock_irqrestore(&priv->driver_lock, flags);
  594. ret = -1;
  595. goto done;
  596. }
  597. if (respcmd != CMD_RET(curcmd)) {
  598. spin_unlock_irqrestore(&priv->driver_lock, flags);
  599. ret = -1;
  600. goto done;
  601. }
  602. if (resp->result == cpu_to_le16(0x0004)) {
  603. /* 0x0004 means -EAGAIN. Drop the response, let it time out
  604. and be resubmitted */
  605. spin_unlock_irqrestore(&priv->driver_lock, flags);
  606. ret = -1;
  607. goto done;
  608. }
  609. /* Now we got response from FW, cancel the command timer */
  610. del_timer(&priv->command_timer);
  611. priv->cmd_timed_out = 0;
  612. if (priv->nr_retries)
  613. priv->nr_retries = 0;
  614. /* If the command is not successful, cleanup and return failure */
  615. if ((result != 0 || !(respcmd & 0x8000))) {
  616. /*
  617. * Handling errors here
  618. */
  619. switch (respcmd) {
  620. case CMD_RET(CMD_GET_HW_SPEC):
  621. case CMD_RET(CMD_802_11_RESET):
  622. pr_info("libertastf: reset failed\n");
  623. break;
  624. }
  625. lbtf_complete_command(priv, priv->cur_cmd, result);
  626. spin_unlock_irqrestore(&priv->driver_lock, flags);
  627. ret = -1;
  628. goto done;
  629. }
  630. spin_unlock_irqrestore(&priv->driver_lock, flags);
  631. if (priv->cur_cmd && priv->cur_cmd->callback) {
  632. ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
  633. resp);
  634. }
  635. spin_lock_irqsave(&priv->driver_lock, flags);
  636. if (priv->cur_cmd) {
  637. /* Clean up and Put current command back to cmdfreeq */
  638. lbtf_complete_command(priv, priv->cur_cmd, result);
  639. }
  640. spin_unlock_irqrestore(&priv->driver_lock, flags);
  641. done:
  642. mutex_unlock(&priv->lock);
  643. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
  644. return ret;
  645. }