cmd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "carl9170.h"
  39. #include "cmd.h"
  40. int carl9170_write_reg(struct ar9170 *ar, const u32 reg, const u32 val)
  41. {
  42. __le32 buf[2] = {
  43. cpu_to_le32(reg),
  44. cpu_to_le32(val),
  45. };
  46. int err;
  47. err = carl9170_exec_cmd(ar, CARL9170_CMD_WREG, sizeof(buf),
  48. (u8 *) buf, 0, NULL);
  49. if (err) {
  50. if (net_ratelimit()) {
  51. wiphy_err(ar->hw->wiphy, "writing reg %#x "
  52. "(val %#x) failed (%d)\n", reg, val, err);
  53. }
  54. }
  55. return err;
  56. }
  57. int carl9170_read_mreg(struct ar9170 *ar, const int nregs,
  58. const u32 *regs, u32 *out)
  59. {
  60. int i, err;
  61. __le32 *offs, *res;
  62. /* abuse "out" for the register offsets, must be same length */
  63. offs = (__le32 *)out;
  64. for (i = 0; i < nregs; i++)
  65. offs[i] = cpu_to_le32(regs[i]);
  66. /* also use the same buffer for the input */
  67. res = (__le32 *)out;
  68. err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
  69. 4 * nregs, (u8 *)offs,
  70. 4 * nregs, (u8 *)res);
  71. if (err) {
  72. if (net_ratelimit()) {
  73. wiphy_err(ar->hw->wiphy, "reading regs failed (%d)\n",
  74. err);
  75. }
  76. return err;
  77. }
  78. /* convert result to cpu endian */
  79. for (i = 0; i < nregs; i++)
  80. out[i] = le32_to_cpu(res[i]);
  81. return 0;
  82. }
  83. int carl9170_read_reg(struct ar9170 *ar, u32 reg, u32 *val)
  84. {
  85. return carl9170_read_mreg(ar, 1, &reg, val);
  86. }
  87. int carl9170_echo_test(struct ar9170 *ar, const u32 v)
  88. {
  89. u32 echores;
  90. int err;
  91. err = carl9170_exec_cmd(ar, CARL9170_CMD_ECHO,
  92. 4, (u8 *)&v,
  93. 4, (u8 *)&echores);
  94. if (err)
  95. return err;
  96. if (v != echores) {
  97. wiphy_info(ar->hw->wiphy, "wrong echo %x != %x", v, echores);
  98. return -EINVAL;
  99. }
  100. return 0;
  101. }
  102. struct carl9170_cmd *carl9170_cmd_buf(struct ar9170 *ar,
  103. const enum carl9170_cmd_oids cmd, const unsigned int len)
  104. {
  105. struct carl9170_cmd *tmp;
  106. tmp = kzalloc(sizeof(struct carl9170_cmd_head) + len, GFP_ATOMIC);
  107. if (tmp) {
  108. tmp->hdr.cmd = cmd;
  109. tmp->hdr.len = len;
  110. }
  111. return tmp;
  112. }
  113. int carl9170_reboot(struct ar9170 *ar)
  114. {
  115. struct carl9170_cmd *cmd;
  116. int err;
  117. cmd = carl9170_cmd_buf(ar, CARL9170_CMD_REBOOT_ASYNC, 0);
  118. if (!cmd)
  119. return -ENOMEM;
  120. err = __carl9170_exec_cmd(ar, (struct carl9170_cmd *)cmd, true);
  121. return err;
  122. }
  123. int carl9170_mac_reset(struct ar9170 *ar)
  124. {
  125. return carl9170_exec_cmd(ar, CARL9170_CMD_SWRST,
  126. 0, NULL, 0, NULL);
  127. }
  128. int carl9170_bcn_ctrl(struct ar9170 *ar, const unsigned int vif_id,
  129. const u32 mode, const u32 addr, const u32 len)
  130. {
  131. struct carl9170_cmd *cmd;
  132. cmd = carl9170_cmd_buf(ar, CARL9170_CMD_BCN_CTRL_ASYNC,
  133. sizeof(struct carl9170_bcn_ctrl_cmd));
  134. if (!cmd)
  135. return -ENOMEM;
  136. cmd->bcn_ctrl.vif_id = cpu_to_le32(vif_id);
  137. cmd->bcn_ctrl.mode = cpu_to_le32(mode);
  138. cmd->bcn_ctrl.bcn_addr = cpu_to_le32(addr);
  139. cmd->bcn_ctrl.bcn_len = cpu_to_le32(len);
  140. return __carl9170_exec_cmd(ar, cmd, true);
  141. }
  142. int carl9170_powersave(struct ar9170 *ar, const bool ps)
  143. {
  144. struct carl9170_cmd *cmd;
  145. u32 state;
  146. cmd = carl9170_cmd_buf(ar, CARL9170_CMD_PSM_ASYNC,
  147. sizeof(struct carl9170_psm));
  148. if (!cmd)
  149. return -ENOMEM;
  150. if (ps) {
  151. /* Sleep until next TBTT */
  152. state = CARL9170_PSM_SLEEP | 1;
  153. } else {
  154. /* wake up immediately */
  155. state = 1;
  156. }
  157. cmd->psm.state = cpu_to_le32(state);
  158. return __carl9170_exec_cmd(ar, cmd, true);
  159. }