xref: /openbmc/linux/net/bluetooth/lib.c (revision 8c520a59)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds    BlueZ - Bluetooth protocol stack for Linux
31da177e4SLinus Torvalds    Copyright (C) 2000-2001 Qualcomm Incorporated
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds    This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds    it under the terms of the GNU General Public License version 2 as
91da177e4SLinus Torvalds    published by the Free Software Foundation;
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
121da177e4SLinus Torvalds    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
131da177e4SLinus Torvalds    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
141da177e4SLinus Torvalds    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
151da177e4SLinus Torvalds    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
161da177e4SLinus Torvalds    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
171da177e4SLinus Torvalds    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
181da177e4SLinus Torvalds    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
211da177e4SLinus Torvalds    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
221da177e4SLinus Torvalds    SOFTWARE IS DISCLAIMED.
231da177e4SLinus Torvalds */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /* Bluetooth kernel library. */
261da177e4SLinus Torvalds 
273ed7003eSJoe Perches #define pr_fmt(fmt) "Bluetooth: " fmt
283ed7003eSJoe Perches 
298c520a59SGustavo Padovan #include <linux/export.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds void baswap(bdaddr_t *dst, bdaddr_t *src)
341da177e4SLinus Torvalds {
351da177e4SLinus Torvalds 	unsigned char *d = (unsigned char *) dst;
361da177e4SLinus Torvalds 	unsigned char *s = (unsigned char *) src;
371da177e4SLinus Torvalds 	unsigned int i;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	for (i = 0; i < 6; i++)
401da177e4SLinus Torvalds 		d[i] = s[5 - i];
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds EXPORT_SYMBOL(baswap);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds char *batostr(bdaddr_t *ba)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	static char str[2][18];
471da177e4SLinus Torvalds 	static int i = 1;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	i ^= 1;
501da177e4SLinus Torvalds 	sprintf(str[i], "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
51d6b2eb2fSGustavo F. Padovan 		ba->b[5], ba->b[4], ba->b[3],
52d6b2eb2fSGustavo F. Padovan 		ba->b[2], ba->b[1], ba->b[0]);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	return str[i];
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds EXPORT_SYMBOL(batostr);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /* Bluetooth error codes to Unix errno mapping */
59e175072fSJoe Perches int bt_to_errno(__u16 code)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	switch (code) {
621da177e4SLinus Torvalds 	case 0:
631da177e4SLinus Torvalds 		return 0;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	case 0x01:
661da177e4SLinus Torvalds 		return EBADRQC;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	case 0x02:
691da177e4SLinus Torvalds 		return ENOTCONN;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	case 0x03:
721da177e4SLinus Torvalds 		return EIO;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	case 0x04:
751da177e4SLinus Torvalds 		return EHOSTDOWN;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	case 0x05:
781da177e4SLinus Torvalds 		return EACCES;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	case 0x06:
811da177e4SLinus Torvalds 		return EBADE;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	case 0x07:
841da177e4SLinus Torvalds 		return ENOMEM;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	case 0x08:
871da177e4SLinus Torvalds 		return ETIMEDOUT;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	case 0x09:
901da177e4SLinus Torvalds 		return EMLINK;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	case 0x0a:
931da177e4SLinus Torvalds 		return EMLINK;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	case 0x0b:
961da177e4SLinus Torvalds 		return EALREADY;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	case 0x0c:
991da177e4SLinus Torvalds 		return EBUSY;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	case 0x0d:
1021da177e4SLinus Torvalds 	case 0x0e:
1031da177e4SLinus Torvalds 	case 0x0f:
1041da177e4SLinus Torvalds 		return ECONNREFUSED;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	case 0x10:
1071da177e4SLinus Torvalds 		return ETIMEDOUT;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	case 0x11:
1101da177e4SLinus Torvalds 	case 0x27:
1111da177e4SLinus Torvalds 	case 0x29:
1121da177e4SLinus Torvalds 	case 0x20:
1131da177e4SLinus Torvalds 		return EOPNOTSUPP;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	case 0x12:
1161da177e4SLinus Torvalds 		return EINVAL;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	case 0x13:
1191da177e4SLinus Torvalds 	case 0x14:
1201da177e4SLinus Torvalds 	case 0x15:
1211da177e4SLinus Torvalds 		return ECONNRESET;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	case 0x16:
1241da177e4SLinus Torvalds 		return ECONNABORTED;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	case 0x17:
1271da177e4SLinus Torvalds 		return ELOOP;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	case 0x18:
1301da177e4SLinus Torvalds 		return EACCES;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	case 0x1a:
1331da177e4SLinus Torvalds 		return EPROTONOSUPPORT;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	case 0x1b:
1361da177e4SLinus Torvalds 		return ECONNREFUSED;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	case 0x19:
1391da177e4SLinus Torvalds 	case 0x1e:
1401da177e4SLinus Torvalds 	case 0x23:
1411da177e4SLinus Torvalds 	case 0x24:
1421da177e4SLinus Torvalds 	case 0x25:
1431da177e4SLinus Torvalds 		return EPROTO;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	default:
1461da177e4SLinus Torvalds 		return ENOSYS;
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds }
149e175072fSJoe Perches EXPORT_SYMBOL(bt_to_errno);
150e1447d8dSJoe Perches 
1513ed7003eSJoe Perches int bt_info(const char *format, ...)
152e1447d8dSJoe Perches {
153e1447d8dSJoe Perches 	struct va_format vaf;
154e1447d8dSJoe Perches 	va_list args;
155e1447d8dSJoe Perches 	int r;
156e1447d8dSJoe Perches 
157e1447d8dSJoe Perches 	va_start(args, format);
158e1447d8dSJoe Perches 
159e1447d8dSJoe Perches 	vaf.fmt = format;
160e1447d8dSJoe Perches 	vaf.va = &args;
161e1447d8dSJoe Perches 
1623ed7003eSJoe Perches 	r = pr_info("%pV", &vaf);
163e1447d8dSJoe Perches 
164e1447d8dSJoe Perches 	va_end(args);
165e1447d8dSJoe Perches 
166e1447d8dSJoe Perches 	return r;
167e1447d8dSJoe Perches }
1683ed7003eSJoe Perches EXPORT_SYMBOL(bt_info);
1693ed7003eSJoe Perches 
1703ed7003eSJoe Perches int bt_err(const char *format, ...)
1713ed7003eSJoe Perches {
1723ed7003eSJoe Perches 	struct va_format vaf;
1733ed7003eSJoe Perches 	va_list args;
1743ed7003eSJoe Perches 	int r;
1753ed7003eSJoe Perches 
1763ed7003eSJoe Perches 	va_start(args, format);
1773ed7003eSJoe Perches 
1783ed7003eSJoe Perches 	vaf.fmt = format;
1793ed7003eSJoe Perches 	vaf.va = &args;
1803ed7003eSJoe Perches 
1813ed7003eSJoe Perches 	r = pr_err("%pV", &vaf);
1823ed7003eSJoe Perches 
1833ed7003eSJoe Perches 	va_end(args);
1843ed7003eSJoe Perches 
1853ed7003eSJoe Perches 	return r;
1863ed7003eSJoe Perches }
1873ed7003eSJoe Perches EXPORT_SYMBOL(bt_err);
188