/*1BlueZ - Bluetooth protocol stack for Linux2Copyright (C) 2000-2001 Qualcomm Incorporated34Written 2000,2001 by Maxim Krasnyansky <[email protected]>56This program is free software; you can redistribute it and/or modify7it under the terms of the GNU General Public License version 2 as8published by the Free Software Foundation;910THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS11OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,12FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.13IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY14CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES15WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN16ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF17OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.1819ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,20COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS21SOFTWARE IS DISCLAIMED.22*/2324/* Bluetooth kernel library. */2526#define pr_fmt(fmt) "Bluetooth: " fmt2728#include <linux/export.h>2930#include <net/bluetooth/bluetooth.h>3132/**33* baswap() - Swaps the order of a bd address34* @dst: Pointer to a bdaddr_t struct that will store the swapped35* bd address.36* @src: Pointer to the bdaddr_t struct to be swapped.37*38* This function reverses the byte order of a Bluetooth device39* address.40*/41void baswap(bdaddr_t *dst, const bdaddr_t *src)42{43const unsigned char *s = (const unsigned char *)src;44unsigned char *d = (unsigned char *)dst;45unsigned int i;4647for (i = 0; i < 6; i++)48d[i] = s[5 - i];49}50EXPORT_SYMBOL(baswap);5152/**53* bt_to_errno() - Bluetooth error codes to standard errno54* @code: Bluetooth error code to be converted55*56* This function takes a Bluetooth error code as input and converts57* it to an equivalent Unix/standard errno value.58*59* Return:60*61* If the bt error code is known, an equivalent Unix errno value62* is returned.63* If the given bt error code is not known, ENOSYS is returned.64*/65int bt_to_errno(__u16 code)66{67switch (code) {68case 0:69return 0;7071case 0x01:72return EBADRQC;7374case 0x02:75return ENOTCONN;7677case 0x03:78return EIO;7980case 0x04:81case 0x3c:82return EHOSTDOWN;8384case 0x05:85return EACCES;8687case 0x06:88return EBADE;8990case 0x07:91return ENOMEM;9293case 0x08:94return ETIMEDOUT;9596case 0x09:97return EMLINK;9899case 0x0a:100return EMLINK;101102case 0x0b:103return EALREADY;104105case 0x0c:106return EBUSY;107108case 0x0d:109case 0x0e:110case 0x0f:111return ECONNREFUSED;112113case 0x10:114return ETIMEDOUT;115116case 0x11:117case 0x27:118case 0x29:119case 0x20:120return EOPNOTSUPP;121122case 0x12:123return EINVAL;124125case 0x13:126case 0x14:127case 0x15:128return ECONNRESET;129130case 0x16:131return ECONNABORTED;132133case 0x17:134return ELOOP;135136case 0x18:137return EACCES;138139case 0x1a:140return EPROTONOSUPPORT;141142case 0x1b:143return ECONNREFUSED;144145case 0x19:146case 0x1e:147case 0x23:148case 0x24:149case 0x25:150return EPROTO;151152default:153return ENOSYS;154}155}156EXPORT_SYMBOL(bt_to_errno);157158/**159* bt_status() - Standard errno value to Bluetooth error code160* @err: Unix/standard errno value to be converted161*162* This function converts a standard/Unix errno value to an163* equivalent Bluetooth error code.164*165* Return: Bluetooth error code.166*167* If the given errno is not found, 0x1f is returned by default168* which indicates an unspecified error.169* For err >= 0, no conversion is performed, and the same value170* is immediately returned.171*/172__u8 bt_status(int err)173{174if (err >= 0)175return err;176177switch (err) {178case -EBADRQC:179return 0x01;180181case -ENOTCONN:182return 0x02;183184case -EIO:185return 0x03;186187case -EHOSTDOWN:188return 0x04;189190case -EACCES:191return 0x05;192193case -EBADE:194return 0x06;195196case -ENOMEM:197return 0x07;198199case -ETIMEDOUT:200return 0x08;201202case -EMLINK:203return 0x09;204205case -EALREADY:206return 0x0b;207208case -EBUSY:209return 0x0c;210211case -ECONNREFUSED:212return 0x0d;213214case -EOPNOTSUPP:215return 0x11;216217case -EINVAL:218return 0x12;219220case -ECONNRESET:221return 0x13;222223case -ECONNABORTED:224return 0x16;225226case -ELOOP:227return 0x17;228229case -EPROTONOSUPPORT:230return 0x1a;231232case -EPROTO:233return 0x19;234235default:236return 0x1f;237}238}239EXPORT_SYMBOL(bt_status);240241/**242* bt_info() - Log Bluetooth information message243* @format: Message's format string244*/245void bt_info(const char *format, ...)246{247struct va_format vaf;248va_list args;249250va_start(args, format);251252vaf.fmt = format;253vaf.va = &args;254255pr_info("%pV", &vaf);256257va_end(args);258}259EXPORT_SYMBOL(bt_info);260261/**262* bt_warn() - Log Bluetooth warning message263* @format: Message's format string264*/265void bt_warn(const char *format, ...)266{267struct va_format vaf;268va_list args;269270va_start(args, format);271272vaf.fmt = format;273vaf.va = &args;274275pr_warn("%pV", &vaf);276277va_end(args);278}279EXPORT_SYMBOL(bt_warn);280281/**282* bt_err() - Log Bluetooth error message283* @format: Message's format string284*/285void bt_err(const char *format, ...)286{287struct va_format vaf;288va_list args;289290va_start(args, format);291292vaf.fmt = format;293vaf.va = &args;294295pr_err("%pV", &vaf);296297va_end(args);298}299EXPORT_SYMBOL(bt_err);300301#ifdef CONFIG_BT_FEATURE_DEBUG302static bool debug_enable;303304void bt_dbg_set(bool enable)305{306debug_enable = enable;307}308309bool bt_dbg_get(void)310{311return debug_enable;312}313314/**315* bt_dbg() - Log Bluetooth debugging message316* @format: Message's format string317*/318void bt_dbg(const char *format, ...)319{320struct va_format vaf;321va_list args;322323if (likely(!debug_enable))324return;325326va_start(args, format);327328vaf.fmt = format;329vaf.va = &args;330331printk(KERN_DEBUG pr_fmt("%pV"), &vaf);332333va_end(args);334}335EXPORT_SYMBOL(bt_dbg);336#endif337338/**339* bt_warn_ratelimited() - Log rate-limited Bluetooth warning message340* @format: Message's format string341*342* This functions works like bt_warn, but it uses rate limiting343* to prevent the message from being logged too often.344*/345void bt_warn_ratelimited(const char *format, ...)346{347struct va_format vaf;348va_list args;349350va_start(args, format);351352vaf.fmt = format;353vaf.va = &args;354355pr_warn_ratelimited("%pV", &vaf);356357va_end(args);358}359EXPORT_SYMBOL(bt_warn_ratelimited);360361/**362* bt_err_ratelimited() - Log rate-limited Bluetooth error message363* @format: Message's format string364*365* This functions works like bt_err, but it uses rate limiting366* to prevent the message from being logged too often.367*/368void bt_err_ratelimited(const char *format, ...)369{370struct va_format vaf;371va_list args;372373va_start(args, format);374375vaf.fmt = format;376vaf.va = &args;377378pr_err_ratelimited("%pV", &vaf);379380va_end(args);381}382EXPORT_SYMBOL(bt_err_ratelimited);383384385