carl9170 firmware: trivial checkpatch fixes
[carl9170fw.git] / tools / carlu / src / cmd.h
1 /*
2  * carlu - userspace testing utility for ar9170 devices
3  *
4  * register/memory/command access functions
5  *
6  * Copyright 2009-2011 Christian Lamparter <chunkeey@googlemail.com>
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 along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef __CARL9170USER_CMD_H
24 #define __CARL9170USER_CMD_H
25
26 #include "carlu.h"
27
28 int carlu_cmd_echo(struct carlu *ar, const uint32_t message);
29 int carlu_cmd_reboot(struct carlu *ar);
30 int carlu_cmd_read_eeprom(struct carlu *ar);
31 int carlu_cmd_mem_dump(struct carlu *ar, const uint32_t start,
32                         const unsigned int len, void *_buf);
33 int carlu_cmd_write_mem(struct carlu *ar, const uint32_t addr,
34                         const uint32_t val);
35 int carlu_cmd_mem_watch(struct carlu *ar, const uint32_t mem,
36                         const unsigned int len, void *_buf);
37
38 struct carl9170_cmd *carlu_cmd_buf(struct carlu *ar,
39         const enum carl9170_cmd_oids cmd, const unsigned int len);
40
41 #define PAYLOAD_MAX     (CARL9170_MAX_CMD_LEN / 4 - 1)
42 /*
43  * Macros to facilitate writing multiple registers in a single
44  * write-combining USB command. Note that when the first group
45  * fails the whole thing will fail without any others attempted,
46  * but you won't know which write in the group failed.
47  */
48 #define carlu_regwrite_begin(ar)                                        \
49 do {                                                                    \
50         struct carlu *__ar = ar;                                        \
51         unsigned int __nreg = 0;                                        \
52         int __err = 0;                                                  \
53         uint32_t __dummy;
54
55 #define carlu_regwrite_flush()                                          \
56         if (__nreg) {                                                   \
57                 __err = carlusb_cmd(__ar, CARL9170_CMD_WREG,            \
58                         (u8 *)&__ar->cmd.cmd.data, 8 * __nreg,          \
59                         (u8 *)&__dummy, sizeof(__dummy));               \
60                 __nreg = 0;                                             \
61                 if (__err)                                              \
62                         goto __regwrite_out;                            \
63         }
64
65 #define carlu_regwrite(r, v) do {                                       \
66         __ar->cmd.buf4[2 * __nreg + 1] = cpu_to_le32(r);                \
67         __ar->cmd.buf4[2 * __nreg + 2] = cpu_to_le32(v);                \
68         __nreg++;                                                       \
69         if ((__nreg >= PAYLOAD_MAX / 2)) {                              \
70                 __err = carlusb_cmd(__ar, CARL9170_CMD_WREG,            \
71                         (u8 *)&__ar->cmd.cmd.data, 8 * __nreg,          \
72                         (u8 *)&__dummy, sizeof(__dummy));               \
73                                                                         \
74                 __nreg = 0;                                             \
75                 if (__err)                                              \
76                         goto __regwrite_out;                            \
77         }                                                               \
78 } while (0)
79
80 #define carlu_regwrite_finish()                                         \
81 __regwrite_out :                                                        \
82         if (__err == 0 && __nreg)                                       \
83                 carlu_regwrite_flush();
84
85 #define carlu_regwrite_result()                                         \
86         __err;                                                          \
87 } while (0);
88
89
90 #define carlu_async_get_buf()                                           \
91 do {                                                                    \
92         __cmd = carlu_cmd_buf(__carl, CARL9170_CMD_WREG_ASYNC,          \
93                                  CARL9170_MAX_CMD_PAYLOAD_LEN);         \
94         if (IS_ERR_OR_NULL(__cmd)) {                                    \
95                 __err = __cmd ? PTR_ERR(__cmd) : -ENOMEM;               \
96                 goto __async_regwrite_out;                              \
97         }                                                               \
98 } while (0);
99
100 #define carlu_async_regwrite_begin(carl)                                \
101 do {                                                                    \
102         int __nreg = 0, __err = 0;                                      \
103         struct carlu *__carl = carl;                                    \
104         struct carl9170_cmd *__cmd;                                     \
105         carlu_async_get_buf();                                          \
106
107 #define carlu_async_regwrite_flush()                                    \
108         if (__nreg) {                                                   \
109                 __cmd->hdr.len = 8 * __nreg;                            \
110                 __err = carlusb_cmd_async(__carl, __cmd, true);         \
111                 __nreg = 0;                                             \
112                 if (__err)                                              \
113                         goto __async_regwrite_out;                      \
114                 __cmd = NULL;                                           \
115                 carlu_async_get_buf();                                  \
116         }
117
118 #define carlu_async_regwrite(r, v) do {                                 \
119         __cmd->wreg.regs[__nreg].addr = cpu_to_le32(r);                 \
120         __cmd->wreg.regs[__nreg].val = cpu_to_le32(v);                  \
121         __nreg++;                                                       \
122         if ((__nreg >= PAYLOAD_MAX / 2))                                \
123                 carlu_async_regwrite_flush();                           \
124 } while (0)
125
126 #define carlu_async_regwrite_finish()                                   \
127 __async_regwrite_out :                                                  \
128         if (__err == 0 && __nreg)                                       \
129                 carlu_async_regwrite_flush();
130
131 #define carlu_async_regwrite_result()                                   \
132         __err;                                                          \
133 } while (0);
134
135 #endif /* __CARL9170USER_CMD_H */