xref: /openbmc/linux/net/bluetooth/lib.c (revision 462cd772)
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4 
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License version 2 as
9    published by the Free Software Foundation;
10 
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24 
25 /* Bluetooth kernel library. */
26 
27 #define pr_fmt(fmt) "Bluetooth: " fmt
28 
29 #include <linux/export.h>
30 
31 #include <net/bluetooth/bluetooth.h>
32 
33 void baswap(bdaddr_t *dst, const bdaddr_t *src)
34 {
35 	const unsigned char *s = (const unsigned char *)src;
36 	unsigned char *d = (unsigned char *)dst;
37 	unsigned int i;
38 
39 	for (i = 0; i < 6; i++)
40 		d[i] = s[5 - i];
41 }
42 EXPORT_SYMBOL(baswap);
43 
44 /* Bluetooth error codes to Unix errno mapping */
45 int bt_to_errno(__u16 code)
46 {
47 	switch (code) {
48 	case 0:
49 		return 0;
50 
51 	case 0x01:
52 		return EBADRQC;
53 
54 	case 0x02:
55 		return ENOTCONN;
56 
57 	case 0x03:
58 		return EIO;
59 
60 	case 0x04:
61 	case 0x3c:
62 		return EHOSTDOWN;
63 
64 	case 0x05:
65 		return EACCES;
66 
67 	case 0x06:
68 		return EBADE;
69 
70 	case 0x07:
71 		return ENOMEM;
72 
73 	case 0x08:
74 		return ETIMEDOUT;
75 
76 	case 0x09:
77 		return EMLINK;
78 
79 	case 0x0a:
80 		return EMLINK;
81 
82 	case 0x0b:
83 		return EALREADY;
84 
85 	case 0x0c:
86 		return EBUSY;
87 
88 	case 0x0d:
89 	case 0x0e:
90 	case 0x0f:
91 		return ECONNREFUSED;
92 
93 	case 0x10:
94 		return ETIMEDOUT;
95 
96 	case 0x11:
97 	case 0x27:
98 	case 0x29:
99 	case 0x20:
100 		return EOPNOTSUPP;
101 
102 	case 0x12:
103 		return EINVAL;
104 
105 	case 0x13:
106 	case 0x14:
107 	case 0x15:
108 		return ECONNRESET;
109 
110 	case 0x16:
111 		return ECONNABORTED;
112 
113 	case 0x17:
114 		return ELOOP;
115 
116 	case 0x18:
117 		return EACCES;
118 
119 	case 0x1a:
120 		return EPROTONOSUPPORT;
121 
122 	case 0x1b:
123 		return ECONNREFUSED;
124 
125 	case 0x19:
126 	case 0x1e:
127 	case 0x23:
128 	case 0x24:
129 	case 0x25:
130 		return EPROTO;
131 
132 	default:
133 		return ENOSYS;
134 	}
135 }
136 EXPORT_SYMBOL(bt_to_errno);
137 
138 /* Unix errno to Bluetooth error codes mapping */
139 __u8 bt_status(int err)
140 {
141 	/* Don't convert if already positive value */
142 	if (err >= 0)
143 		return err;
144 
145 	switch (err) {
146 	case -EBADRQC:
147 		return 0x01;
148 
149 	case -ENOTCONN:
150 		return 0x02;
151 
152 	case -EIO:
153 		return 0x03;
154 
155 	case -EHOSTDOWN:
156 		return 0x04;
157 
158 	case -EACCES:
159 		return 0x05;
160 
161 	case -EBADE:
162 		return 0x06;
163 
164 	case -ENOMEM:
165 		return 0x07;
166 
167 	case -ETIMEDOUT:
168 		return 0x08;
169 
170 	case -EMLINK:
171 		return 0x09;
172 
173 	case EALREADY:
174 		return 0x0b;
175 
176 	case -EBUSY:
177 		return 0x0c;
178 
179 	case -ECONNREFUSED:
180 		return 0x0d;
181 
182 	case -EOPNOTSUPP:
183 		return 0x11;
184 
185 	case -EINVAL:
186 		return 0x12;
187 
188 	case -ECONNRESET:
189 		return 0x13;
190 
191 	case -ECONNABORTED:
192 		return 0x16;
193 
194 	case ELOOP:
195 		return 0x17;
196 
197 	case -EPROTONOSUPPORT:
198 		return 0x1a;
199 
200 	case -EPROTO:
201 		return 0x19;
202 
203 	default:
204 		return 0x1f;
205 	}
206 }
207 EXPORT_SYMBOL(bt_status);
208 
209 void bt_info(const char *format, ...)
210 {
211 	struct va_format vaf;
212 	va_list args;
213 
214 	va_start(args, format);
215 
216 	vaf.fmt = format;
217 	vaf.va = &args;
218 
219 	pr_info("%pV", &vaf);
220 
221 	va_end(args);
222 }
223 EXPORT_SYMBOL(bt_info);
224 
225 void bt_warn(const char *format, ...)
226 {
227 	struct va_format vaf;
228 	va_list args;
229 
230 	va_start(args, format);
231 
232 	vaf.fmt = format;
233 	vaf.va = &args;
234 
235 	pr_warn("%pV", &vaf);
236 
237 	va_end(args);
238 }
239 EXPORT_SYMBOL(bt_warn);
240 
241 void bt_err(const char *format, ...)
242 {
243 	struct va_format vaf;
244 	va_list args;
245 
246 	va_start(args, format);
247 
248 	vaf.fmt = format;
249 	vaf.va = &args;
250 
251 	pr_err("%pV", &vaf);
252 
253 	va_end(args);
254 }
255 EXPORT_SYMBOL(bt_err);
256 
257 #ifdef CONFIG_BT_FEATURE_DEBUG
258 static bool debug_enable;
259 
260 void bt_dbg_set(bool enable)
261 {
262 	debug_enable = enable;
263 }
264 
265 bool bt_dbg_get(void)
266 {
267 	return debug_enable;
268 }
269 
270 void bt_dbg(const char *format, ...)
271 {
272 	struct va_format vaf;
273 	va_list args;
274 
275 	if (likely(!debug_enable))
276 		return;
277 
278 	va_start(args, format);
279 
280 	vaf.fmt = format;
281 	vaf.va = &args;
282 
283 	printk(KERN_DEBUG pr_fmt("%pV"), &vaf);
284 
285 	va_end(args);
286 }
287 EXPORT_SYMBOL(bt_dbg);
288 #endif
289 
290 void bt_warn_ratelimited(const char *format, ...)
291 {
292 	struct va_format vaf;
293 	va_list args;
294 
295 	va_start(args, format);
296 
297 	vaf.fmt = format;
298 	vaf.va = &args;
299 
300 	pr_warn_ratelimited("%pV", &vaf);
301 
302 	va_end(args);
303 }
304 EXPORT_SYMBOL(bt_warn_ratelimited);
305 
306 void bt_err_ratelimited(const char *format, ...)
307 {
308 	struct va_format vaf;
309 	va_list args;
310 
311 	va_start(args, format);
312 
313 	vaf.fmt = format;
314 	vaf.va = &args;
315 
316 	pr_err_ratelimited("%pV", &vaf);
317 
318 	va_end(args);
319 }
320 EXPORT_SYMBOL(bt_err_ratelimited);
321