1fe8ee9adSChristian Lamparter /*
2fe8ee9adSChristian Lamparter * Atheros CARL9170 driver
3fe8ee9adSChristian Lamparter *
4fe8ee9adSChristian Lamparter * Basic HW register/memory/command access functions
5fe8ee9adSChristian Lamparter *
6fe8ee9adSChristian Lamparter * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7fe8ee9adSChristian Lamparter * Copyright 2010, Christian Lamparter <chunkeey@googlemail.com>
8fe8ee9adSChristian Lamparter *
9fe8ee9adSChristian Lamparter * This program is free software; you can redistribute it and/or modify
10fe8ee9adSChristian Lamparter * it under the terms of the GNU General Public License as published by
11fe8ee9adSChristian Lamparter * the Free Software Foundation; either version 2 of the License, or
12fe8ee9adSChristian Lamparter * (at your option) any later version.
13fe8ee9adSChristian Lamparter *
14fe8ee9adSChristian Lamparter * This program is distributed in the hope that it will be useful,
15fe8ee9adSChristian Lamparter * but WITHOUT ANY WARRANTY; without even the implied warranty of
16fe8ee9adSChristian Lamparter * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17fe8ee9adSChristian Lamparter * GNU General Public License for more details.
18fe8ee9adSChristian Lamparter *
19fe8ee9adSChristian Lamparter * You should have received a copy of the GNU General Public License
20fe8ee9adSChristian Lamparter * along with this program; see the file COPYING. If not, see
21fe8ee9adSChristian Lamparter * http://www.gnu.org/licenses/.
22fe8ee9adSChristian Lamparter *
23fe8ee9adSChristian Lamparter * This file incorporates work covered by the following copyright and
24fe8ee9adSChristian Lamparter * permission notice:
25fe8ee9adSChristian Lamparter * Copyright (c) 2007-2008 Atheros Communications, Inc.
26fe8ee9adSChristian Lamparter *
27fe8ee9adSChristian Lamparter * Permission to use, copy, modify, and/or distribute this software for any
28fe8ee9adSChristian Lamparter * purpose with or without fee is hereby granted, provided that the above
29fe8ee9adSChristian Lamparter * copyright notice and this permission notice appear in all copies.
30fe8ee9adSChristian Lamparter *
31fe8ee9adSChristian Lamparter * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32fe8ee9adSChristian Lamparter * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33fe8ee9adSChristian Lamparter * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34fe8ee9adSChristian Lamparter * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35fe8ee9adSChristian Lamparter * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36fe8ee9adSChristian Lamparter * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37fe8ee9adSChristian Lamparter * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38fe8ee9adSChristian Lamparter */
39fe8ee9adSChristian Lamparter #ifndef __CMD_H
40fe8ee9adSChristian Lamparter #define __CMD_H
41fe8ee9adSChristian Lamparter
42fe8ee9adSChristian Lamparter #include "carl9170.h"
43fe8ee9adSChristian Lamparter
44fe8ee9adSChristian Lamparter /* basic HW access */
45fe8ee9adSChristian Lamparter int carl9170_write_reg(struct ar9170 *ar, const u32 reg, const u32 val);
46fe8ee9adSChristian Lamparter int carl9170_read_reg(struct ar9170 *ar, const u32 reg, u32 *val);
47fe8ee9adSChristian Lamparter int carl9170_read_mreg(struct ar9170 *ar, const int nregs,
48fe8ee9adSChristian Lamparter const u32 *regs, u32 *out);
49fe8ee9adSChristian Lamparter int carl9170_echo_test(struct ar9170 *ar, u32 v);
50fe8ee9adSChristian Lamparter int carl9170_reboot(struct ar9170 *ar);
51fe8ee9adSChristian Lamparter int carl9170_mac_reset(struct ar9170 *ar);
52fe8ee9adSChristian Lamparter int carl9170_powersave(struct ar9170 *ar, const bool power_on);
53acf17712SChristian Lamparter int carl9170_collect_tally(struct ar9170 *ar);
54fe8ee9adSChristian Lamparter int carl9170_bcn_ctrl(struct ar9170 *ar, const unsigned int vif_id,
55fe8ee9adSChristian Lamparter const u32 mode, const u32 addr, const u32 len);
56fe8ee9adSChristian Lamparter
carl9170_flush_cab(struct ar9170 * ar,const unsigned int vif_id)57fe8ee9adSChristian Lamparter static inline int carl9170_flush_cab(struct ar9170 *ar,
58fe8ee9adSChristian Lamparter const unsigned int vif_id)
59fe8ee9adSChristian Lamparter {
60fe8ee9adSChristian Lamparter return carl9170_bcn_ctrl(ar, vif_id, CARL9170_BCN_CTRL_DRAIN, 0, 0);
61fe8ee9adSChristian Lamparter }
62fe8ee9adSChristian Lamparter
carl9170_rx_filter(struct ar9170 * ar,const unsigned int _rx_filter)635c895691SChristian Lamparter static inline int carl9170_rx_filter(struct ar9170 *ar,
645c895691SChristian Lamparter const unsigned int _rx_filter)
655c895691SChristian Lamparter {
665c895691SChristian Lamparter __le32 rx_filter = cpu_to_le32(_rx_filter);
675c895691SChristian Lamparter
685c895691SChristian Lamparter return carl9170_exec_cmd(ar, CARL9170_CMD_RX_FILTER,
695c895691SChristian Lamparter sizeof(rx_filter), (u8 *)&rx_filter,
705c895691SChristian Lamparter 0, NULL);
715c895691SChristian Lamparter }
725c895691SChristian Lamparter
73fe8ee9adSChristian Lamparter struct carl9170_cmd *carl9170_cmd_buf(struct ar9170 *ar,
74fe8ee9adSChristian Lamparter const enum carl9170_cmd_oids cmd, const unsigned int len);
75fe8ee9adSChristian Lamparter
76fe8ee9adSChristian Lamparter /*
77fe8ee9adSChristian Lamparter * Macros to facilitate writing multiple registers in a single
78fe8ee9adSChristian Lamparter * write-combining USB command. Note that when the first group
79fe8ee9adSChristian Lamparter * fails the whole thing will fail without any others attempted,
80fe8ee9adSChristian Lamparter * but you won't know which write in the group failed.
81fe8ee9adSChristian Lamparter */
82fe8ee9adSChristian Lamparter #define carl9170_regwrite_begin(ar) \
83fe8ee9adSChristian Lamparter do { \
84fe8ee9adSChristian Lamparter int __nreg = 0, __err = 0; \
85fe8ee9adSChristian Lamparter struct ar9170 *__ar = ar;
86fe8ee9adSChristian Lamparter
87fe8ee9adSChristian Lamparter #define carl9170_regwrite(r, v) do { \
88fe8ee9adSChristian Lamparter __ar->cmd_buf[2 * __nreg + 1] = cpu_to_le32(r); \
89fe8ee9adSChristian Lamparter __ar->cmd_buf[2 * __nreg + 2] = cpu_to_le32(v); \
90fe8ee9adSChristian Lamparter __nreg++; \
91fe8ee9adSChristian Lamparter if ((__nreg >= PAYLOAD_MAX / 2)) { \
92fe8ee9adSChristian Lamparter if (IS_ACCEPTING_CMD(__ar)) \
93fe8ee9adSChristian Lamparter __err = carl9170_exec_cmd(__ar, \
94fe8ee9adSChristian Lamparter CARL9170_CMD_WREG, 8 * __nreg, \
95fe8ee9adSChristian Lamparter (u8 *) &__ar->cmd_buf[1], 0, NULL); \
96fe8ee9adSChristian Lamparter else \
97fe8ee9adSChristian Lamparter goto __regwrite_out; \
98fe8ee9adSChristian Lamparter \
99fe8ee9adSChristian Lamparter __nreg = 0; \
100fe8ee9adSChristian Lamparter if (__err) \
101fe8ee9adSChristian Lamparter goto __regwrite_out; \
102fe8ee9adSChristian Lamparter } \
103fe8ee9adSChristian Lamparter } while (0)
104fe8ee9adSChristian Lamparter
105fe8ee9adSChristian Lamparter #define carl9170_regwrite_finish() \
106fe8ee9adSChristian Lamparter __regwrite_out : \
107fe8ee9adSChristian Lamparter if (__err == 0 && __nreg) { \
108fe8ee9adSChristian Lamparter if (IS_ACCEPTING_CMD(__ar)) \
109fe8ee9adSChristian Lamparter __err = carl9170_exec_cmd(__ar, \
110fe8ee9adSChristian Lamparter CARL9170_CMD_WREG, 8 * __nreg, \
111fe8ee9adSChristian Lamparter (u8 *) &__ar->cmd_buf[1], 0, NULL); \
112fe8ee9adSChristian Lamparter __nreg = 0; \
113fe8ee9adSChristian Lamparter }
114fe8ee9adSChristian Lamparter
115fe8ee9adSChristian Lamparter #define carl9170_regwrite_result() \
116fe8ee9adSChristian Lamparter __err; \
117*da951c24SJoe Perches } while (0)
118fe8ee9adSChristian Lamparter
119fe8ee9adSChristian Lamparter
1203d2f2cd0SChristian Lamparter #define carl9170_async_regwrite_get_buf() \
121fe8ee9adSChristian Lamparter do { \
1223d2f2cd0SChristian Lamparter __nreg = 0; \
123fe8ee9adSChristian Lamparter __cmd = carl9170_cmd_buf(__carl, CARL9170_CMD_WREG_ASYNC, \
124fe8ee9adSChristian Lamparter CARL9170_MAX_CMD_PAYLOAD_LEN); \
125fe8ee9adSChristian Lamparter if (__cmd == NULL) { \
126fe8ee9adSChristian Lamparter __err = -ENOMEM; \
127fe8ee9adSChristian Lamparter goto __async_regwrite_out; \
128fe8ee9adSChristian Lamparter } \
129*da951c24SJoe Perches } while (0)
130fe8ee9adSChristian Lamparter
131fe8ee9adSChristian Lamparter #define carl9170_async_regwrite_begin(carl) \
132fe8ee9adSChristian Lamparter do { \
133fe8ee9adSChristian Lamparter struct ar9170 *__carl = carl; \
134fe8ee9adSChristian Lamparter struct carl9170_cmd *__cmd; \
1353d2f2cd0SChristian Lamparter unsigned int __nreg; \
1363d2f2cd0SChristian Lamparter int __err = 0; \
1373d2f2cd0SChristian Lamparter carl9170_async_regwrite_get_buf(); \
138fe8ee9adSChristian Lamparter
1393d2f2cd0SChristian Lamparter #define carl9170_async_regwrite_flush() \
1403d2f2cd0SChristian Lamparter do { \
1413d2f2cd0SChristian Lamparter if (__cmd == NULL || __nreg == 0) \
1423d2f2cd0SChristian Lamparter break; \
1433d2f2cd0SChristian Lamparter \
1443d2f2cd0SChristian Lamparter if (IS_ACCEPTING_CMD(__carl) && __nreg) { \
145fe8ee9adSChristian Lamparter __cmd->hdr.len = 8 * __nreg; \
146fe8ee9adSChristian Lamparter __err = __carl9170_exec_cmd(__carl, __cmd, true); \
147fe8ee9adSChristian Lamparter __cmd = NULL; \
1483d2f2cd0SChristian Lamparter break; \
149fe8ee9adSChristian Lamparter } \
150fe8ee9adSChristian Lamparter goto __async_regwrite_out; \
151fe8ee9adSChristian Lamparter } while (0)
152fe8ee9adSChristian Lamparter
1533d2f2cd0SChristian Lamparter #define carl9170_async_regwrite(r, v) do { \
1543d2f2cd0SChristian Lamparter if (__cmd == NULL) \
1553d2f2cd0SChristian Lamparter carl9170_async_regwrite_get_buf(); \
1563d2f2cd0SChristian Lamparter __cmd->wreg.regs[__nreg].addr = cpu_to_le32(r); \
1573d2f2cd0SChristian Lamparter __cmd->wreg.regs[__nreg].val = cpu_to_le32(v); \
1583d2f2cd0SChristian Lamparter __nreg++; \
1593d2f2cd0SChristian Lamparter if ((__nreg >= PAYLOAD_MAX / 2)) \
1603d2f2cd0SChristian Lamparter carl9170_async_regwrite_flush(); \
1613d2f2cd0SChristian Lamparter } while (0)
1623d2f2cd0SChristian Lamparter
1633d2f2cd0SChristian Lamparter #define carl9170_async_regwrite_finish() do { \
164fe8ee9adSChristian Lamparter __async_regwrite_out: \
1653d2f2cd0SChristian Lamparter if (__cmd != NULL && __err == 0) \
1663d2f2cd0SChristian Lamparter carl9170_async_regwrite_flush(); \
1673d2f2cd0SChristian Lamparter kfree(__cmd); \
1683d2f2cd0SChristian Lamparter } while (0) \
169fe8ee9adSChristian Lamparter
170fe8ee9adSChristian Lamparter #define carl9170_async_regwrite_result() \
171fe8ee9adSChristian Lamparter __err; \
172*da951c24SJoe Perches } while (0)
173fe8ee9adSChristian Lamparter
174fe8ee9adSChristian Lamparter #endif /* __CMD_H */
175