File: /usr/src/linux/net/bluetooth/lib.c

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     /*
26      * BlueZ kernel library.
27      *
28      * $Id: lib.c,v 1.3 2001/06/22 23:14:23 maxk Exp $
29      */
30     
31     #include <linux/kernel.h>
32     #include <linux/stddef.h>
33     #include <linux/string.h>
34     #include <asm/errno.h>
35     
36     #include <net/bluetooth/bluetooth.h>
37     
38     void bluez_dump(char *pref, __u8 *buf, int count)
39     {
40     	char *ptr;
41     	char line[100];
42     	int i;
43     
44     	printk(KERN_INFO "%s: dump, len %d\n", pref, count);
45     
46     	ptr = line;
47     	*ptr = 0;
48     	for (i = 0; i<count; i++) {
49     		ptr += sprintf(ptr, " %2.2X", buf[i]);
50     
51     		if (i && !((i + 1) % 20)) {
52     			printk(KERN_INFO "%s:%s\n", pref, line);
53     			ptr = line;
54     			*ptr = 0;
55     		}
56     	}
57     
58     	if (line[0])
59     		printk(KERN_INFO "%s:%s\n", pref, line);
60     }
61     
62     void baswap(bdaddr_t *dst, bdaddr_t *src)
63     {
64     	unsigned char *d = (unsigned char *) dst;
65     	unsigned char *s = (unsigned char *) src;
66     	int i;
67     
68     	for (i = 0; i < 6; i++)
69     		d[i] = s[5 - i];
70     }
71     
72     char *batostr(bdaddr_t *ba)
73     {
74     	static char str[2][18];
75     	static int i = 1;
76     
77     	i ^= 1;
78     	sprintf(str[i], "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
79     	        ba->b[0], ba->b[1], ba->b[2],
80     		ba->b[3], ba->b[4], ba->b[5]);
81     
82     	return str[i];
83     }
84     
85     /* Bluetooth error codes to Unix errno mapping */
86     int bterr(__u16 code)
87     {
88     	switch (code) {
89     	case 0:
90     		return 0;
91     
92     	case 0x01:
93     		return EBADRQC;
94     
95     	case 0x02:
96     		return ENOTCONN;
97     
98     	case 0x03:
99     		return EIO;
100     
101     	case 0x04:
102     		return EHOSTDOWN;
103     
104     	case 0x05:
105     		return EACCES;
106     
107     	case 0x06:
108     		return EINVAL;
109     
110     	case 0x07:
111     		return ENOMEM;
112     
113     	case 0x08:
114     		return ETIMEDOUT;
115     
116     	case 0x09:
117     		return EMLINK;
118     
119     	case 0x0a:
120     		return EMLINK;
121     
122     	case 0x0b:
123     		return EALREADY;
124     
125     	case 0x0c:
126     		return EBUSY;
127     
128     	case 0x0d:
129     	case 0x0e:
130     	case 0x0f:
131     		return ECONNREFUSED;
132     
133     	case 0x10:
134     		return ETIMEDOUT;
135     
136     	case 0x11:
137     	case 0x27:
138     	case 0x29:
139     	case 0x20:
140     		return EOPNOTSUPP;
141     
142     	case 0x12:
143     		return EINVAL;
144     
145     	case 0x13:
146     	case 0x14:
147     	case 0x15:
148     		return ECONNRESET;
149     
150     	case 0x16:
151     		return ECONNABORTED;
152     
153     	case 0x17:
154     		return ELOOP;
155     
156     	case 0x18:
157     		return EACCES;
158     
159     	case 0x1a:
160     		return EPROTONOSUPPORT;
161     
162     	case 0x1b:
163     		return ECONNREFUSED;
164     
165     	case 0x19:
166     	case 0x1e:
167     	case 0x23:
168     	case 0x24:
169     	case 0x25:
170     		return EPROTO;
171     
172     	default:
173     		return ENOSYS;
174     	};
175     }
176