carlu: carlu_regwrite wrappers
[carl9170fw.git] / tools / carlu / src / cmd.h
1 /*
2  * carl9170user - userspace testing utility for ar9170 devices
3  *
4  * register/memory/command access functions
5  *
6  * Copyright 2009, 2010 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
36 struct carl9170_cmd *carlu_cmd_buf(struct carlu *ar,
37         const enum carl9170_cmd_oids cmd, const unsigned int len);
38
39 #define PAYLOAD_MAX     (CARL9170_MAX_CMD_LEN / 4 - 1)
40 /*
41  * Macros to facilitate writing multiple registers in a single
42  * write-combining USB command. Note that when the first group
43  * fails the whole thing will fail without any others attempted,
44  * but you won't know which write in the group failed.
45  */
46 #define carlu_regwrite_begin(ar)                                        \
47 do {                                                                    \
48         struct carlu *__ar = ar;                                        \
49         unsigned int __nreg = 0;                                        \
50         int __err = 0;                                                  \
51         uint32_t __dummy;
52
53 #define carlu_regwrite_flush()                                          \
54         if (__nreg) {                                                   \
55                 __err = carlusb_cmd(__ar, CARL9170_CMD_WREG,            \
56                         (u8 *)&__ar->cmd.cmd.data, 8 * __nreg,          \
57                         (u8 *)&__dummy, sizeof(__dummy));               \
58                 __nreg = 0;                                             \
59                 if (__err)                                              \
60                         goto __regwrite_out;                            \
61         }
62
63 #define carlu_regwrite(r, v) do {                                       \
64         __ar->cmd.buf4[2 * __nreg + 1] = cpu_to_le32(r);                \
65         __ar->cmd.buf4[2 * __nreg + 2] = cpu_to_le32(v);                \
66         __nreg++;                                                       \
67         if ((__nreg >= PAYLOAD_MAX / 2)) {                              \
68                 __err = carlusb_cmd(__ar, CARL9170_CMD_WREG,            \
69                         (u8 *)&__ar->cmd.cmd.data, 8 * __nreg,          \
70                         (u8 *)&__dummy, sizeof(__dummy));               \
71                                                                         \
72                 __nreg = 0;                                             \
73                 if (__err)                                              \
74                         goto __regwrite_out;                            \
75         }                                                               \
76 } while (0)
77
78 #define carlu_regwrite_finish()                                         \
79 __regwrite_out :                                                        \
80         if (__err == 0 && __nreg)                                       \
81                 carlu_regwrite_flush();
82
83 #define carlu_regwrite_result()                                         \
84         __err;                                                          \
85 } while (0);
86
87
88 #define carlu_async_get_buf()                                           \
89 do {                                                                    \
90         __cmd = carlu_cmd_buf(__carl, CARL9170_CMD_WREG_ASYNC,          \
91                                  CARL9170_MAX_CMD_PAYLOAD_LEN);         \
92         if (IS_ERR_OR_NULL(__cmd)) {                                    \
93                 __err = __cmd ? PTR_ERR(__cmd) : -ENOMEM;               \
94                 goto __async_regwrite_out;                              \
95         }                                                               \
96 } while (0);
97
98 #define carlu_async_regwrite_begin(carl)                                \
99 do {                                                                    \
100         int __nreg = 0, __err = 0;                                      \
101         struct carlu *__carl = carl;                                    \
102         struct carl9170_cmd *__cmd;                                     \
103         carlu_async_get_buf();                                          \
104
105 #define carlu_async_regwrite_flush()                                    \
106         if (__nreg) {                                                   \
107                 __cmd->hdr.len = 8 * __nreg;                            \
108                 __err = carlusb_cmd_async(__carl, __cmd, true);         \
109                 __nreg = 0;                                             \
110                 if (__err)                                              \
111                         goto __async_regwrite_out;                      \
112                 __cmd = NULL;                                           \
113                 carlu_async_get_buf();                                  \
114         }
115
116 #define carlu_async_regwrite(r, v) do {                                 \
117         __cmd->wreg.regs[__nreg].addr = cpu_to_le32(r);                 \
118         __cmd->wreg.regs[__nreg].val = cpu_to_le32(v);                  \
119         __nreg++;                                                       \
120         if ((__nreg >= PAYLOAD_MAX / 2))                                \
121                 carlu_async_regwrite_flush();                           \
122 } while (0)
123
124 #define carlu_async_regwrite_finish()                                   \
125 __async_regwrite_out :                                                  \
126         if (__err == 0 && __nreg)                                       \
127                 carlu_async_regwrite_flush();
128
129 #define carlu_async_regwrite_result()                                   \
130         __err;                                                          \
131 } while (0);
132
133 #endif /* __CARL9170USER_CMD_H */