Linux 6.10-rc3
[linux-stable.git] / net / bluetooth / lib.c
blob43aa01fd07b989b48245cec842b9019e0f866c12
1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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.
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.
25 /* Bluetooth kernel library. */
27 #define pr_fmt(fmt) "Bluetooth: " fmt
29 #include <linux/export.h>
31 #include <net/bluetooth/bluetooth.h>
33 /**
34 * baswap() - Swaps the order of a bd address
35 * @dst: Pointer to a bdaddr_t struct that will store the swapped
36 * bd address.
37 * @src: Pointer to the bdaddr_t struct to be swapped.
39 * This function reverses the byte order of a Bluetooth device
40 * address.
42 void baswap(bdaddr_t *dst, const bdaddr_t *src)
44 const unsigned char *s = (const unsigned char *)src;
45 unsigned char *d = (unsigned char *)dst;
46 unsigned int i;
48 for (i = 0; i < 6; i++)
49 d[i] = s[5 - i];
51 EXPORT_SYMBOL(baswap);
53 /**
54 * bt_to_errno() - Bluetooth error codes to standard errno
55 * @code: Bluetooth error code to be converted
57 * This function takes a Bluetooth error code as input and convets
58 * it to an equivalent Unix/standard errno value.
60 * Return:
62 * If the bt error code is known, an equivalent Unix errno value
63 * is returned.
64 * If the given bt error code is not known, ENOSYS is returned.
66 int bt_to_errno(__u16 code)
68 switch (code) {
69 case 0:
70 return 0;
72 case 0x01:
73 return EBADRQC;
75 case 0x02:
76 return ENOTCONN;
78 case 0x03:
79 return EIO;
81 case 0x04:
82 case 0x3c:
83 return EHOSTDOWN;
85 case 0x05:
86 return EACCES;
88 case 0x06:
89 return EBADE;
91 case 0x07:
92 return ENOMEM;
94 case 0x08:
95 return ETIMEDOUT;
97 case 0x09:
98 return EMLINK;
100 case 0x0a:
101 return EMLINK;
103 case 0x0b:
104 return EALREADY;
106 case 0x0c:
107 return EBUSY;
109 case 0x0d:
110 case 0x0e:
111 case 0x0f:
112 return ECONNREFUSED;
114 case 0x10:
115 return ETIMEDOUT;
117 case 0x11:
118 case 0x27:
119 case 0x29:
120 case 0x20:
121 return EOPNOTSUPP;
123 case 0x12:
124 return EINVAL;
126 case 0x13:
127 case 0x14:
128 case 0x15:
129 return ECONNRESET;
131 case 0x16:
132 return ECONNABORTED;
134 case 0x17:
135 return ELOOP;
137 case 0x18:
138 return EACCES;
140 case 0x1a:
141 return EPROTONOSUPPORT;
143 case 0x1b:
144 return ECONNREFUSED;
146 case 0x19:
147 case 0x1e:
148 case 0x23:
149 case 0x24:
150 case 0x25:
151 return EPROTO;
153 default:
154 return ENOSYS;
157 EXPORT_SYMBOL(bt_to_errno);
160 * bt_status() - Standard errno value to Bluetooth error code
161 * @err: Unix/standard errno value to be converted
163 * This function converts a standard/Unix errno value to an
164 * equivalent Bluetooth error code.
166 * Return: Bluetooth error code.
168 * If the given errno is not found, 0x1f is returned by default
169 * which indicates an unspecified error.
170 * For err >= 0, no conversion is performed, and the same value
171 * is immediately returned.
173 __u8 bt_status(int err)
175 if (err >= 0)
176 return err;
178 switch (err) {
179 case -EBADRQC:
180 return 0x01;
182 case -ENOTCONN:
183 return 0x02;
185 case -EIO:
186 return 0x03;
188 case -EHOSTDOWN:
189 return 0x04;
191 case -EACCES:
192 return 0x05;
194 case -EBADE:
195 return 0x06;
197 case -ENOMEM:
198 return 0x07;
200 case -ETIMEDOUT:
201 return 0x08;
203 case -EMLINK:
204 return 0x09;
206 case -EALREADY:
207 return 0x0b;
209 case -EBUSY:
210 return 0x0c;
212 case -ECONNREFUSED:
213 return 0x0d;
215 case -EOPNOTSUPP:
216 return 0x11;
218 case -EINVAL:
219 return 0x12;
221 case -ECONNRESET:
222 return 0x13;
224 case -ECONNABORTED:
225 return 0x16;
227 case -ELOOP:
228 return 0x17;
230 case -EPROTONOSUPPORT:
231 return 0x1a;
233 case -EPROTO:
234 return 0x19;
236 default:
237 return 0x1f;
240 EXPORT_SYMBOL(bt_status);
243 * bt_info() - Log Bluetooth information message
244 * @format: Message's format string
246 void bt_info(const char *format, ...)
248 struct va_format vaf;
249 va_list args;
251 va_start(args, format);
253 vaf.fmt = format;
254 vaf.va = &args;
256 pr_info("%pV", &vaf);
258 va_end(args);
260 EXPORT_SYMBOL(bt_info);
263 * bt_warn() - Log Bluetooth warning message
264 * @format: Message's format string
266 void bt_warn(const char *format, ...)
268 struct va_format vaf;
269 va_list args;
271 va_start(args, format);
273 vaf.fmt = format;
274 vaf.va = &args;
276 pr_warn("%pV", &vaf);
278 va_end(args);
280 EXPORT_SYMBOL(bt_warn);
283 * bt_err() - Log Bluetooth error message
284 * @format: Message's format string
286 void bt_err(const char *format, ...)
288 struct va_format vaf;
289 va_list args;
291 va_start(args, format);
293 vaf.fmt = format;
294 vaf.va = &args;
296 pr_err("%pV", &vaf);
298 va_end(args);
300 EXPORT_SYMBOL(bt_err);
302 #ifdef CONFIG_BT_FEATURE_DEBUG
303 static bool debug_enable;
305 void bt_dbg_set(bool enable)
307 debug_enable = enable;
310 bool bt_dbg_get(void)
312 return debug_enable;
316 * bt_dbg() - Log Bluetooth debugging message
317 * @format: Message's format string
319 void bt_dbg(const char *format, ...)
321 struct va_format vaf;
322 va_list args;
324 if (likely(!debug_enable))
325 return;
327 va_start(args, format);
329 vaf.fmt = format;
330 vaf.va = &args;
332 printk(KERN_DEBUG pr_fmt("%pV"), &vaf);
334 va_end(args);
336 EXPORT_SYMBOL(bt_dbg);
337 #endif
340 * bt_warn_ratelimited() - Log rate-limited Bluetooth warning message
341 * @format: Message's format string
343 * This functions works like bt_warn, but it uses rate limiting
344 * to prevent the message from being logged too often.
346 void bt_warn_ratelimited(const char *format, ...)
348 struct va_format vaf;
349 va_list args;
351 va_start(args, format);
353 vaf.fmt = format;
354 vaf.va = &args;
356 pr_warn_ratelimited("%pV", &vaf);
358 va_end(args);
360 EXPORT_SYMBOL(bt_warn_ratelimited);
363 * bt_err_ratelimited() - Log rate-limited Bluetooth error message
364 * @format: Message's format string
366 * This functions works like bt_err, but it uses rate limiting
367 * to prevent the message from being logged too often.
369 void bt_err_ratelimited(const char *format, ...)
371 struct va_format vaf;
372 va_list args;
374 va_start(args, format);
376 vaf.fmt = format;
377 vaf.va = &args;
379 pr_err_ratelimited("%pV", &vaf);
381 va_end(args);
383 EXPORT_SYMBOL(bt_err_ratelimited);