cmd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * Basic HW register/memory/command access functions
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; see the file COPYING. If not, see
  20. * http://www.gnu.org/licenses/.
  21. *
  22. * This file incorporates work covered by the following copyright and
  23. * permission notice:
  24. * Copyright (c) 2007-2008 Atheros Communications, Inc.
  25. *
  26. * Permission to use, copy, modify, and/or distribute this software for any
  27. * purpose with or without fee is hereby granted, provided that the above
  28. * copyright notice and this permission notice appear in all copies.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  31. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  32. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  33. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  34. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  35. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  36. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  37. */
  38. #include <asm/div64.h>
  39. #include "carl9170.h"
  40. #include "cmd.h"
  41. int carl9170_write_reg(struct ar9170 *ar, const u32 reg, const u32 val)
  42. {
  43. const __le32 buf[2] = {
  44. cpu_to_le32(reg),
  45. cpu_to_le32(val),
  46. };
  47. int err;
  48. err = carl9170_exec_cmd(ar, CARL9170_CMD_WREG, sizeof(buf),
  49. (u8 *) buf, 0, NULL);
  50. if (err) {
  51. if (net_ratelimit()) {
  52. wiphy_err(ar->hw->wiphy, "writing reg %#x "
  53. "(val %#x) failed (%d)\n", reg, val, err);
  54. }
  55. }
  56. return err;
  57. }
  58. int carl9170_read_mreg(struct ar9170 *ar, const int nregs,
  59. const u32 *regs, u32 *out)
  60. {
  61. int i, err;
  62. __le32 *offs, *res;
  63. /* abuse "out" for the register offsets, must be same length */
  64. offs = (__le32 *)out;
  65. for (i = 0; i < nregs; i++)
  66. offs[i] = cpu_to_le32(regs[i]);
  67. /* also use the same buffer for the input */
  68. res = (__le32 *)out;
  69. err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
  70. 4 * nregs, (u8 *)offs,
  71. 4 * nregs, (u8 *)res);
  72. if (err) {
  73. if (net_ratelimit()) {
  74. wiphy_err(ar->hw->wiphy, "reading regs failed (%d)\n",
  75. err);
  76. }
  77. return err;
  78. }
  79. /* convert result to cpu endian */
  80. for (i = 0; i < nregs; i++)
  81. out[i] = le32_to_cpu(res[i]);
  82. return 0;
  83. }
  84. int carl9170_read_reg(struct ar9170 *ar, u32 reg, u32 *val)
  85. {
  86. return carl9170_read_mreg(ar, 1, &reg, val);
  87. }
  88. int carl9170_echo_test(struct ar9170 *ar, const u32 v)
  89. {
  90. u32 echores;
  91. int err;
  92. err = carl9170_exec_cmd(ar, CARL9170_CMD_ECHO,
  93. 4, (u8 *)&v,
  94. 4, (u8 *)&echores);
  95. if (err)
  96. return err;
  97. if (v != echores) {
  98. wiphy_info(ar->hw->wiphy, "wrong echo %x != %x", v, echores);
  99. return -EINVAL;
  100. }
  101. return 0;
  102. }
  103. struct carl9170_cmd *carl9170_cmd_buf(struct ar9170 *ar,
  104. const enum carl9170_cmd_oids cmd, const unsigned int len)
  105. {
  106. struct carl9170_cmd *tmp;
  107. tmp = kzalloc(sizeof(struct carl9170_cmd_head) + len, GFP_ATOMIC);
  108. if (tmp) {
  109. tmp->hdr.cmd = cmd;
  110. tmp->hdr.len = len;
  111. }
  112. return tmp;
  113. }
  114. int carl9170_reboot(struct ar9170 *ar)
  115. {
  116. struct carl9170_cmd *cmd;
  117. int err;
  118. cmd = carl9170_cmd_buf(ar, CARL9170_CMD_REBOOT_ASYNC, 0);
  119. if (!cmd)
  120. return -ENOMEM;
  121. err = __carl9170_exec_cmd(ar, cmd, true);
  122. return err;
  123. }
  124. int carl9170_mac_reset(struct ar9170 *ar)
  125. {
  126. return carl9170_exec_cmd(ar, CARL9170_CMD_SWRST,
  127. 0, NULL, 0, NULL);
  128. }
  129. int carl9170_bcn_ctrl(struct ar9170 *ar, const unsigned int vif_id,
  130. const u32 mode, const u32 addr, const u32 len)
  131. {
  132. struct carl9170_cmd *cmd;
  133. cmd = carl9170_cmd_buf(ar, CARL9170_CMD_BCN_CTRL_ASYNC,
  134. sizeof(struct carl9170_bcn_ctrl_cmd));
  135. if (!cmd)
  136. return -ENOMEM;
  137. cmd->bcn_ctrl.vif_id = cpu_to_le32(vif_id);
  138. cmd->bcn_ctrl.mode = cpu_to_le32(mode);
  139. cmd->bcn_ctrl.bcn_addr = cpu_to_le32(addr);
  140. cmd->bcn_ctrl.bcn_len = cpu_to_le32(len);
  141. return __carl9170_exec_cmd(ar, cmd, true);
  142. }
  143. int carl9170_collect_tally(struct ar9170 *ar)
  144. {
  145. struct carl9170_tally_rsp tally;
  146. struct survey_info *info;
  147. unsigned int tick;
  148. int err;
  149. err = carl9170_exec_cmd(ar, CARL9170_CMD_TALLY, 0, NULL,
  150. sizeof(tally), (u8 *)&tally);
  151. if (err)
  152. return err;
  153. tick = le32_to_cpu(tally.tick);
  154. if (tick) {
  155. ar->tally.active += le32_to_cpu(tally.active) / tick;
  156. ar->tally.cca += le32_to_cpu(tally.cca) / tick;
  157. ar->tally.tx_time += le32_to_cpu(tally.tx_time) / tick;
  158. ar->tally.rx_total += le32_to_cpu(tally.rx_total);
  159. ar->tally.rx_overrun += le32_to_cpu(tally.rx_overrun);
  160. if (ar->channel) {
  161. info = &ar->survey[ar->channel->hw_value];
  162. info->time = ar->tally.active;
  163. info->time_busy = ar->tally.cca;
  164. info->time_tx = ar->tally.tx_time;
  165. do_div(info->time, 1000);
  166. do_div(info->time_busy, 1000);
  167. do_div(info->time_tx, 1000);
  168. }
  169. }
  170. return 0;
  171. }
  172. int carl9170_powersave(struct ar9170 *ar, const bool ps)
  173. {
  174. struct carl9170_cmd *cmd;
  175. u32 state;
  176. cmd = carl9170_cmd_buf(ar, CARL9170_CMD_PSM_ASYNC,
  177. sizeof(struct carl9170_psm));
  178. if (!cmd)
  179. return -ENOMEM;
  180. if (ps) {
  181. /* Sleep until next TBTT */
  182. state = CARL9170_PSM_SLEEP | 1;
  183. } else {
  184. /* wake up immediately */
  185. state = 1;
  186. }
  187. cmd->psm.state = cpu_to_le32(state);
  188. return __carl9170_exec_cmd(ar, cmd, true);
  189. }