1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
5 */
6
7 #include <linux/vmalloc.h>
8 #include "core.h"
9 #include "debug.h"
10
ath12k_info(struct ath12k_base * ab,const char * fmt,...)11 void ath12k_info(struct ath12k_base *ab, const char *fmt, ...)
12 {
13 struct va_format vaf = {
14 .fmt = fmt,
15 };
16 va_list args;
17
18 va_start(args, fmt);
19 vaf.va = &args;
20 #if defined(__linux__)
21 dev_info(ab->dev, "%pV", &vaf);
22 #elif defined(__FreeBSD__)
23 {
24 char *str;
25 vasprintf(&str, M_KMALLOC, fmt, args);
26 dev_printk(KERN_INFO, ab->dev, "%s", str);
27 free(str, M_KMALLOC);
28 }
29 #endif
30 /* TODO: Trace the log */
31 va_end(args);
32 }
33
ath12k_err(struct ath12k_base * ab,const char * fmt,...)34 void ath12k_err(struct ath12k_base *ab, const char *fmt, ...)
35 {
36 struct va_format vaf = {
37 .fmt = fmt,
38 };
39 va_list args;
40
41 va_start(args, fmt);
42 vaf.va = &args;
43 #if defined(__linux__)
44 dev_err(ab->dev, "%pV", &vaf);
45 #elif defined(__FreeBSD__)
46 {
47 char *str;
48 vasprintf(&str, M_KMALLOC, fmt, args);
49 dev_printk(KERN_ERR, ab->dev, "%s", str);
50 free(str, M_KMALLOC);
51 }
52 #endif
53 /* TODO: Trace the log */
54 va_end(args);
55 }
56
ath12k_warn(struct ath12k_base * ab,const char * fmt,...)57 void ath12k_warn(struct ath12k_base *ab, const char *fmt, ...)
58 {
59 struct va_format vaf = {
60 .fmt = fmt,
61 };
62 va_list args;
63
64 va_start(args, fmt);
65 vaf.va = &args;
66 #if defined(__linux__)
67 dev_warn_ratelimited(ab->dev, "%pV", &vaf);
68 #elif defined(__FreeBSD__)
69 {
70 static linux_ratelimit_t __ratelimited;
71
72 if (linux_ratelimited(&__ratelimited)) {
73 char *str;
74 vasprintf(&str, M_KMALLOC, fmt, args);
75 dev_printk(KERN_WARN, ab->dev, "%s", str);
76 free(str, M_KMALLOC);
77 }
78 }
79 #endif
80 /* TODO: Trace the log */
81 va_end(args);
82 }
83
84 #ifdef CONFIG_ATH12K_DEBUG
85
__ath12k_dbg(struct ath12k_base * ab,enum ath12k_debug_mask mask,const char * fmt,...)86 void __ath12k_dbg(struct ath12k_base *ab, enum ath12k_debug_mask mask,
87 const char *fmt, ...)
88 {
89 struct va_format vaf;
90 va_list args;
91
92 va_start(args, fmt);
93
94 vaf.fmt = fmt;
95 vaf.va = &args;
96
97 if (ath12k_debug_mask & mask)
98 #if defined(__linux__)
99 dev_dbg(ab->dev, "%pV", &vaf);
100 #elif defined(__FreeBSD__)
101 {
102 char *str;
103 vasprintf(&str, M_KMALLOC, fmt, args);
104 dev_printk(KERN_DEBUG, ab->dev, "%s", str);
105 free(str, M_KMALLOC);
106 }
107 #endif
108
109 /* TODO: trace log */
110
111 va_end(args);
112 }
113
ath12k_dbg_dump(struct ath12k_base * ab,enum ath12k_debug_mask mask,const char * msg,const char * prefix,const void * buf,size_t len)114 void ath12k_dbg_dump(struct ath12k_base *ab,
115 enum ath12k_debug_mask mask,
116 const char *msg, const char *prefix,
117 const void *buf, size_t len)
118 {
119 #if defined(__linux__)
120 char linebuf[256];
121 size_t linebuflen;
122 const void *ptr;
123 #elif defined(__FreeBSD__)
124 struct sbuf *sb;
125 int rc;
126 #endif
127
128 if (ath12k_debug_mask & mask) {
129 if (msg)
130 __ath12k_dbg(ab, mask, "%s\n", msg);
131
132 #if defined(__linux__)
133 for (ptr = buf; (ptr - buf) < len; ptr += 16) {
134 linebuflen = 0;
135 linebuflen += scnprintf(linebuf + linebuflen,
136 sizeof(linebuf) - linebuflen,
137 "%s%08x: ",
138 (prefix ? prefix : ""),
139 (unsigned int)(ptr - buf));
140 hex_dump_to_buffer(ptr, len - (ptr - buf), 16, 1,
141 linebuf + linebuflen,
142 sizeof(linebuf) - linebuflen, true);
143 dev_dbg(ab->dev, "%s\n", linebuf);
144 }
145 #elif defined(__FreeBSD__)
146 sb = sbuf_new_auto();
147 if (sb == NULL)
148 goto trace;
149
150 sbuf_hexdump(sb, buf, len, prefix, 0);
151 sbuf_trim(sb);
152 rc = sbuf_finish(sb);
153 if (rc == 0)
154 dev_printk(KERN_DEBUG, ab->dev, "%s\n", sbuf_data(sb));
155 sbuf_delete(sb);
156 trace: ;
157 #endif
158 }
159 }
160
161 #endif /* CONFIG_ATH12K_DEBUG */
162