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 /** 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. 38 * 39 * This function reverses the byte order of a Bluetooth device 40 * address. 41 */ 42 void baswap(bdaddr_t *dst, const bdaddr_t *src) 43 { 44 const unsigned char *s = (const unsigned char *)src; 45 unsigned char *d = (unsigned char *)dst; 46 unsigned int i; 47 48 for (i = 0; i < 6; i++) 49 d[i] = s[5 - i]; 50 } 51 EXPORT_SYMBOL(baswap); 52 53 /** 54 * bt_to_errno() - Bluetooth error codes to standard errno 55 * @code: Bluetooth error code to be converted 56 * 57 * This function takes a Bluetooth error code as input and convets 58 * it to an equivalent Unix/standard errno value. 59 * 60 * Return: 61 * 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. 65 */ 66 int bt_to_errno(__u16 code) 67 { 68 switch (code) { 69 case 0: 70 return 0; 71 72 case 0x01: 73 return EBADRQC; 74 75 case 0x02: 76 return ENOTCONN; 77 78 case 0x03: 79 return EIO; 80 81 case 0x04: 82 case 0x3c: 83 return EHOSTDOWN; 84 85 case 0x05: 86 return EACCES; 87 88 case 0x06: 89 return EBADE; 90 91 case 0x07: 92 return ENOMEM; 93 94 case 0x08: 95 return ETIMEDOUT; 96 97 case 0x09: 98 return EMLINK; 99 100 case 0x0a: 101 return EMLINK; 102 103 case 0x0b: 104 return EALREADY; 105 106 case 0x0c: 107 return EBUSY; 108 109 case 0x0d: 110 case 0x0e: 111 case 0x0f: 112 return ECONNREFUSED; 113 114 case 0x10: 115 return ETIMEDOUT; 116 117 case 0x11: 118 case 0x27: 119 case 0x29: 120 case 0x20: 121 return EOPNOTSUPP; 122 123 case 0x12: 124 return EINVAL; 125 126 case 0x13: 127 case 0x14: 128 case 0x15: 129 return ECONNRESET; 130 131 case 0x16: 132 return ECONNABORTED; 133 134 case 0x17: 135 return ELOOP; 136 137 case 0x18: 138 return EACCES; 139 140 case 0x1a: 141 return EPROTONOSUPPORT; 142 143 case 0x1b: 144 return ECONNREFUSED; 145 146 case 0x19: 147 case 0x1e: 148 case 0x23: 149 case 0x24: 150 case 0x25: 151 return EPROTO; 152 153 default: 154 return ENOSYS; 155 } 156 } 157 EXPORT_SYMBOL(bt_to_errno); 158 159 /** 160 * bt_status() - Standard errno value to Bluetooth error code 161 * @err: Unix/standard errno value to be converted 162 * 163 * This function converts a standard/Unix errno value to an 164 * equivalent Bluetooth error code. 165 * 166 * Return: Bluetooth error code. 167 * 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. 172 */ 173 __u8 bt_status(int err) 174 { 175 if (err >= 0) 176 return err; 177 178 switch (err) { 179 case -EBADRQC: 180 return 0x01; 181 182 case -ENOTCONN: 183 return 0x02; 184 185 case -EIO: 186 return 0x03; 187 188 case -EHOSTDOWN: 189 return 0x04; 190 191 case -EACCES: 192 return 0x05; 193 194 case -EBADE: 195 return 0x06; 196 197 case -ENOMEM: 198 return 0x07; 199 200 case -ETIMEDOUT: 201 return 0x08; 202 203 case -EMLINK: 204 return 0x09; 205 206 case -EALREADY: 207 return 0x0b; 208 209 case -EBUSY: 210 return 0x0c; 211 212 case -ECONNREFUSED: 213 return 0x0d; 214 215 case -EOPNOTSUPP: 216 return 0x11; 217 218 case -EINVAL: 219 return 0x12; 220 221 case -ECONNRESET: 222 return 0x13; 223 224 case -ECONNABORTED: 225 return 0x16; 226 227 case -ELOOP: 228 return 0x17; 229 230 case -EPROTONOSUPPORT: 231 return 0x1a; 232 233 case -EPROTO: 234 return 0x19; 235 236 default: 237 return 0x1f; 238 } 239 } 240 EXPORT_SYMBOL(bt_status); 241 242 /** 243 * bt_info() - Log Bluetooth information message 244 * @format: Message's format string 245 */ 246 void bt_info(const char *format, ...) 247 { 248 struct va_format vaf; 249 va_list args; 250 251 va_start(args, format); 252 253 vaf.fmt = format; 254 vaf.va = &args; 255 256 pr_info("%pV", &vaf); 257 258 va_end(args); 259 } 260 EXPORT_SYMBOL(bt_info); 261 262 /** 263 * bt_warn() - Log Bluetooth warning message 264 * @format: Message's format string 265 */ 266 void bt_warn(const char *format, ...) 267 { 268 struct va_format vaf; 269 va_list args; 270 271 va_start(args, format); 272 273 vaf.fmt = format; 274 vaf.va = &args; 275 276 pr_warn("%pV", &vaf); 277 278 va_end(args); 279 } 280 EXPORT_SYMBOL(bt_warn); 281 282 /** 283 * bt_err() - Log Bluetooth error message 284 * @format: Message's format string 285 */ 286 void bt_err(const char *format, ...) 287 { 288 struct va_format vaf; 289 va_list args; 290 291 va_start(args, format); 292 293 vaf.fmt = format; 294 vaf.va = &args; 295 296 pr_err("%pV", &vaf); 297 298 va_end(args); 299 } 300 EXPORT_SYMBOL(bt_err); 301 302 #ifdef CONFIG_BT_FEATURE_DEBUG 303 static bool debug_enable; 304 305 void bt_dbg_set(bool enable) 306 { 307 debug_enable = enable; 308 } 309 310 bool bt_dbg_get(void) 311 { 312 return debug_enable; 313 } 314 315 /** 316 * bt_dbg() - Log Bluetooth debugging message 317 * @format: Message's format string 318 */ 319 void bt_dbg(const char *format, ...) 320 { 321 struct va_format vaf; 322 va_list args; 323 324 if (likely(!debug_enable)) 325 return; 326 327 va_start(args, format); 328 329 vaf.fmt = format; 330 vaf.va = &args; 331 332 printk(KERN_DEBUG pr_fmt("%pV"), &vaf); 333 334 va_end(args); 335 } 336 EXPORT_SYMBOL(bt_dbg); 337 #endif 338 339 /** 340 * bt_warn_ratelimited() - Log rate-limited Bluetooth warning message 341 * @format: Message's format string 342 * 343 * This functions works like bt_warn, but it uses rate limiting 344 * to prevent the message from being logged too often. 345 */ 346 void bt_warn_ratelimited(const char *format, ...) 347 { 348 struct va_format vaf; 349 va_list args; 350 351 va_start(args, format); 352 353 vaf.fmt = format; 354 vaf.va = &args; 355 356 pr_warn_ratelimited("%pV", &vaf); 357 358 va_end(args); 359 } 360 EXPORT_SYMBOL(bt_warn_ratelimited); 361 362 /** 363 * bt_err_ratelimited() - Log rate-limited Bluetooth error message 364 * @format: Message's format string 365 * 366 * This functions works like bt_err, but it uses rate limiting 367 * to prevent the message from being logged too often. 368 */ 369 void bt_err_ratelimited(const char *format, ...) 370 { 371 struct va_format vaf; 372 va_list args; 373 374 va_start(args, format); 375 376 vaf.fmt = format; 377 vaf.va = &args; 378 379 pr_err_ratelimited("%pV", &vaf); 380 381 va_end(args); 382 } 383 EXPORT_SYMBOL(bt_err_ratelimited); 384