cmd.c 17 KB

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