cmd.c 17 KB

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