forked from MathewKing/mcp2210-linux
-
Notifications
You must be signed in to change notification settings - Fork 33
/
mcp2210-debug.h
275 lines (244 loc) · 9.75 KB
/
mcp2210-debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Verbose Debug functions for MCP2210 driver & libs
*
* Copyright (c) 2013-2017 Daniel Santos <daniel.santos@pobox.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _MCP2210_DEBUG_H
#define _MCP2210_DEBUG_H
#include "mcp2210.h"
#ifdef __KERNEL__
# include <linux/kernel.h>
# include <linux/device.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Do not define CONFIG_MCP2210_LOGGING_FAST_PATH unless you're debugging a
* timing-sensitive a problem and you need log spew to be as optimized as
* possible. */
#ifdef CONFIG_MCP2210_LOGGING_FAST_PATH
# define MCP2210_LOG_UNLIKELY(expr) (expr)
#else
# define MCP2210_LOG_UNLIKELY(expr) unlikely(expr)
#endif
#ifdef __KERNEL__
#define _mcp2210_log(level, fmt, ...) \
do { \
if (MCP2210_LOG_UNLIKELY((level[1] - '0') <= debug_level))\
dev_printk(level, &dev->udev->dev, \
"%s: " fmt, __func__, ##__VA_ARGS__);\
} while(0)
/* TODO: we need to submit a cleaned up version of this to mainline for
* compiler*.h */
#define warn_on_non_const(value) \
do { \
extern void not_const_warn(void) __compiletime_warning( \
#value " is not a compile-time constant, this " \
"will cause some bloating of generated code"); \
\
if (!__builtin_constant_p(value)) \
not_const_warn(); \
} while(0)
/**
* mcp2210_log - logs messages
*
* Logs messages but assures that debug messages are completely compiled out
* when CONFIG_MCP2210_DEBUG is not enabled
*/
#define mcp2210_log(level, fmt, ...) \
do { \
const char _level = level[1] - '0'; \
const char _dbg = KERN_DEBUG[1] - '0'; \
\
warn_on_non_const(_level); \
\
/* compile-out debug messages unless \
* CONFIG_MCP2210_DEBUG is enabled */ \
if (!IS_ENABLED(CONFIG_MCP2210_DEBUG) && _level == _dbg)\
break; \
\
_mcp2210_log(level, fmt, ##__VA_ARGS__); \
} while(0)
#define mcp2210_emerg(fmt, ...) mcp2210_log(KERN_EMERG, fmt, ##__VA_ARGS__)
#define mcp2210_alert(fmt, ...) mcp2210_log(KERN_ALERT, fmt, ##__VA_ARGS__)
#define mcp2210_crit(fmt, ...) mcp2210_log(KERN_CRIT, fmt, ##__VA_ARGS__)
#define mcp2210_err(fmt, ...) mcp2210_log(KERN_ERR, fmt, ##__VA_ARGS__)
#define mcp2210_warn(fmt, ...) mcp2210_log(KERN_WARNING,fmt, ##__VA_ARGS__)
#define mcp2210_notice(fmt, ...)mcp2210_log(KERN_NOTICE,fmt, ##__VA_ARGS__)
#define mcp2210_info(fmt, ...) mcp2210_log(KERN_INFO, fmt, ##__VA_ARGS__)
#define mcp2210_debug(fmt, ...) mcp2210_log(KERN_DEBUG, fmt, ##__VA_ARGS__)
#ifdef CONFIG_MCP2210_DEBUG
# define MCP_ASSERT(cond) BUG_ON(!(cond))
#else
# define MCP_ASSERT(cond) do{}while(0)
#endif
#ifdef CONFIG_MCP2210_DEBUG_VERBOSE
void dump_dev(
const char *level, unsigned indent, const char *start,
const struct mcp2210_device *dev);
void dump_ep(
const char *level, unsigned indent, const char *start,
const struct mcp2210_endpoint *ep);
void dump_cmd_head(
const char *level, unsigned indent, const char *start,
const struct mcp2210_cmd *cmd);
void dump_cmd_ctl(
const char *level, unsigned indent, const char *start,
const struct mcp2210_cmd *cmd_head);
void dump_cmd_spi(
const char *level, unsigned indent, const char *start,
const struct mcp2210_cmd *cmd_head);
void dump_cmd_eeprom(
const char *level, unsigned indent, const char *start,
const struct mcp2210_cmd *cmd_head);
void dump_spi_message(
const char *level, unsigned indent, const char *start,
const struct spi_message *msg);
void dump_spi_transfer(
const char *level, unsigned indent, const char *start,
const struct spi_transfer *xfer);
void dump_spi_device(
const char *level, unsigned indent, const char *start,
const struct spi_device *spi_dev);
#else
# define dump_dev _dump_nothing_void
# define dump_ep _dump_nothing_void
# define dump_cmd_head _dump_nothing_cmd
# define dump_cmd_ctl _dump_nothing_cmd
# define dump_cmd_spi _dump_nothing_cmd
# define dump_cmd_eeprom _dump_nothing_cmd
# define dump_spi_message _dump_nothing_void
# define dump_spi_transfer _dump_nothing_void
# define dump_spi_device _dump_nothing_void
static inline void _dump_nothing_cmd(
const char *level, unsigned indent, const char *start,
const struct mcp2210_cmd *cmd_head){}
static inline void _dump_nothing_void(
const char *level, unsigned indent, const char *start,
const void *cmd_head){}
#endif
static inline void print_mcp_msg(const char *level, const char *start,
const struct mcp2210_msg *data)
{
print_hex_dump(level, start, DUMP_PREFIX_OFFSET, 16, 1, data, 64, true);
}
void _mcp2210_dump_urbs(struct mcp2210_device *dev, const char *level,
int urb_mask);
static inline void mcp2210_dump_urbs(struct mcp2210_device *dev,
const char *level, int urb_mask)
{
if (IS_ENABLED(CONFIG_MCP2210_DEBUG) && (dump_urbs
|| level[1] <= KERN_WARNING[1]))
_mcp2210_dump_urbs(dev, level, urb_mask);
}
void _dump_cmd(const char *level, unsigned indent, const char *start,
const struct mcp2210_cmd *cmd_head);
static inline void dump_cmd(const char *level, unsigned indent,
const char *start,
const struct mcp2210_cmd *cmd_head)
{
if (IS_ENABLED(CONFIG_MCP2210_DEBUG_VERBOSE) && dump_cmds)
_dump_cmd(level, indent, start, cmd_head);
}
#endif /* __KERNEL__ */
/* both kernel & userspace functions */
#ifdef CONFIG_MCP2210_DEBUG_VERBOSE
const char *get_cmd_str(u8 cmd);
const char *get_sub_cmd_str(u8 sub_cmd);
const char *get_status_str(u8 status);
const char *get_pin_mode_str(u8 mode);
const char *get_eeprom_status_str(u8 mode);
const char *get_cmd_type_str(u8 mode);
const char *get_state_str(u8 mode);
void dump_pin_config(
const char *level, unsigned indent, const char *start,
const struct mcp2210_pin_config *cfg);
void dump_chip_settings(
const char *level, unsigned indent, const char *start,
const struct mcp2210_chip_settings *s);
void dump_board_config(
const char *level, unsigned indent, const char *start,
const struct mcp2210_board_config *bc);
void dump_spi_xfer_settings(
const char *level, unsigned indent, const char *start,
const struct mcp2210_spi_xfer_settings *s);
void dump_usb_key_params(
const char *level, unsigned indent, const char *start,
const struct mcp2210_usb_key_params *params);
void dump_state(
const char *level, unsigned indent, const char *start,
const struct mcp2210_state *s);
void dump_mcp_msg(
const char *level, unsigned indent, const char *start,
struct mcp2210_msg *msg, int is_req);
#else
# define get_cmd_str(value) while(0){}
# define get_sub_cmd_str(value) while(0){}
# define get_status_str(value) while(0){}
# define get_pin_mode_str(value) while(0){}
# define get_eeprom_status_str(value) while(0){}
# define get_cmd_type_str(value) while(0){}
# define get_state_str(value) while(0){}
# define dump_pin_config(level, indent, start, x) while(0){}
# define dump_chip_settings(level, indent, start, x) while(0){}
# define dump_board_config(level, indent, start, x) while(0){}
# define dump_spi_xfer_settings(level, indent, start, x)while(0){}
# define dump_usb_key_params(level, indent, start, x) while(0){}
# define dump_state(level, indent, start, x) while(0){}
# define dump_mcp_msg(level, indent, start, x) while(0){}
#endif /* CONFIG_MCP2210_DEBUG_VERBOSE */
/* compile-time validation of struct mcp2210_msg */
static inline void msg_validate_size(void)
{
struct mcp2210_msg validation_msg;
BUILD_BUG_ON(IS_ENABLED(CONFIG_MCP2210_CREEK)
&& (!IS_ENABLED(CONFIG_MCP2210_EEPROM)));
/* sanity checks on struct mcp2210_msg */
BUILD_BUG_ON(sizeof(struct mcp2210_msg) != MCP2210_BUFFER_SIZE);
BUILD_BUG_ON(sizeof(validation_msg.head) != 3);
BUILD_BUG_ON(sizeof(validation_msg.body) != 60);
/* validate head offsets */
BUILD_BUG_ON(offsetof(struct mcp2210_msg, head) != 1);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, head.req) != 1);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, head.rep) != 1);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, head.rep.status) != 1);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, head.rep.xet.status) != 1);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, head.rep.xet.sub_cmd) != 2);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, head.rep.xet.reserved) != 3);
/* validate body offsets */
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body) != 4);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.chip) != 4);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.spi) != 4);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.get_usb_params) != 4);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.set_usb_params) != 4);
/* validate body sizes */
BUILD_BUG_ON(sizeof(validation_msg.body.chip) != 23);
BUILD_BUG_ON(sizeof(validation_msg.body.spi) != 17);
BUILD_BUG_ON(sizeof(validation_msg.body.get_usb_params) != 27);
BUILD_BUG_ON(sizeof(validation_msg.body.set_usb_params) != 6);
BUILD_BUG_ON(sizeof(validation_msg.body.usb_string) != 60);
BUILD_BUG_ON(sizeof(validation_msg.body.raw) != 60);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.chip.password) != 19);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.spi.mode) != 20);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.gpio) != 4);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.password) != 4);
BUILD_BUG_ON(offsetof(struct mcp2210_msg, body.raw) != 4);
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _MCP2210_DEBUG_H */