bluetooth.h 8.2 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
   BlueZ - Bluetooth protocol stack for Linux
   Copyright (C) 2000-2001 Qualcomm Incorporated

   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15 16 17
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
L
Linus Torvalds 已提交
18 19
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

20 21
   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29
   SOFTWARE IS DISCLAIMED.
*/

#ifndef __BLUETOOTH_H
#define __BLUETOOTH_H

#include <linux/poll.h>
#include <net/sock.h>
30
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36

#ifndef AF_BLUETOOTH
#define AF_BLUETOOTH	31
#define PF_BLUETOOTH	AF_BLUETOOTH
#endif

37 38 39 40 41
/* Bluetooth versions */
#define BLUETOOTH_VER_1_1	1
#define BLUETOOTH_VER_1_2	2
#define BLUETOOTH_VER_2_0	3

L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* Reserv for core and drivers use */
#define BT_SKB_RESERVE	8

#define BTPROTO_L2CAP	0
#define BTPROTO_HCI	1
#define BTPROTO_SCO	2
#define BTPROTO_RFCOMM	3
#define BTPROTO_BNEP	4
#define BTPROTO_CMTP	5
#define BTPROTO_HIDP	6
#define BTPROTO_AVDTP	7

#define SOL_HCI		0
#define SOL_L2CAP	6
#define SOL_SCO		17
#define SOL_RFCOMM	18

59 60 61
#define BT_SECURITY	4
struct bt_security {
	__u8 level;
62
	__u8 key_size;
63 64 65 66 67 68
};
#define BT_SECURITY_SDP		0
#define BT_SECURITY_LOW		1
#define BT_SECURITY_MEDIUM	2
#define BT_SECURITY_HIGH	3

69 70
#define BT_DEFER_SETUP	7

71 72 73 74 75
#define BT_FLUSHABLE	8

#define BT_FLUSHABLE_OFF	0
#define BT_FLUSHABLE_ON		1

76 77 78 79 80 81 82
#define BT_POWER	9
struct bt_power {
	__u8 force_active;
};
#define BT_POWER_FORCE_ACTIVE_OFF 0
#define BT_POWER_FORCE_ACTIVE_ON  1

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#define BT_CHANNEL_POLICY	10

/* BR/EDR only (default policy)
 *   AMP controllers cannot be used.
 *   Channel move requests from the remote device are denied.
 *   If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
 */
#define BT_CHANNEL_POLICY_BREDR_ONLY		0

/* BR/EDR Preferred
 *   Allow use of AMP controllers.
 *   If the L2CAP channel is currently on AMP, move it to BR/EDR.
 *   Channel move requests from the remote device are allowed.
 */
#define BT_CHANNEL_POLICY_BREDR_PREFERRED	1

/* AMP Preferred
 *   Allow use of AMP controllers
 *   If the L2CAP channel is currently on BR/EDR and AMP controller
 *     resources are available, initiate a channel move to AMP.
 *   Channel move requests from the remote device are allowed.
 *   If the L2CAP socket has not been connected yet, try to create
 *     and configure the channel directly on an AMP controller rather
 *     than BR/EDR.
 */
#define BT_CHANNEL_POLICY_AMP_PREFERRED		2

110 111 112 113 114 115 116 117
#define BT_VOICE		11
struct bt_voice {
	__u16 setting;
};

#define BT_VOICE_TRANSPARENT			0x0003
#define BT_VOICE_CVSD_16BIT			0x0060

118 119 120 121 122 123 124 125
__printf(1, 2)
int bt_info(const char *fmt, ...);
__printf(1, 2)
int bt_err(const char *fmt, ...);

#define BT_INFO(fmt, ...)	bt_info(fmt "\n", ##__VA_ARGS__)
#define BT_ERR(fmt, ...)	bt_err(fmt "\n", ##__VA_ARGS__)
#define BT_DBG(fmt, ...)	pr_debug(fmt "\n", ##__VA_ARGS__)
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139

/* Connection and socket states */
enum {
	BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
	BT_OPEN,
	BT_BOUND,
	BT_LISTEN,
	BT_CONNECT,
	BT_CONNECT2,
	BT_CONFIG,
	BT_DISCONN,
	BT_CLOSED
};

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/* If unused will be removed by compiler */
static inline const char *state_to_string(int state)
{
	switch (state) {
	case BT_CONNECTED:
		return "BT_CONNECTED";
	case BT_OPEN:
		return "BT_OPEN";
	case BT_BOUND:
		return "BT_BOUND";
	case BT_LISTEN:
		return "BT_LISTEN";
	case BT_CONNECT:
		return "BT_CONNECT";
	case BT_CONNECT2:
		return "BT_CONNECT2";
	case BT_CONFIG:
		return "BT_CONFIG";
	case BT_DISCONN:
		return "BT_DISCONN";
	case BT_CLOSED:
		return "BT_CLOSED";
	}

	return "invalid state";
}

L
Linus Torvalds 已提交
167 168 169
/* BD Address */
typedef struct {
	__u8 b[6];
170
} __packed bdaddr_t;
L
Linus Torvalds 已提交
171

172 173 174 175 176
/* BD Address type */
#define BDADDR_BREDR		0x00
#define BDADDR_LE_PUBLIC	0x01
#define BDADDR_LE_RANDOM	0x02

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static inline bool bdaddr_type_is_valid(__u8 type)
{
	switch (type) {
	case BDADDR_BREDR:
	case BDADDR_LE_PUBLIC:
	case BDADDR_LE_RANDOM:
		return true;
	}

	return false;
}

static inline bool bdaddr_type_is_le(__u8 type)
{
	switch (type) {
	case BDADDR_LE_PUBLIC:
	case BDADDR_LE_RANDOM:
		return true;
	}

	return false;
}

200 201
#define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0} })
#define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff} })
L
Linus Torvalds 已提交
202 203

/* Copy, swap, convert BD Address */
204
static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
L
Linus Torvalds 已提交
205 206 207
{
	return memcmp(ba1, ba2, sizeof(bdaddr_t));
}
208
static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
	memcpy(dst, src, sizeof(bdaddr_t));
}

void baswap(bdaddr_t *dst, bdaddr_t *src);

/* Common socket structures and functions */

#define bt_sk(__sk) ((struct bt_sock *) __sk)

struct bt_sock {
	struct sock sk;
	bdaddr_t    src;
	bdaddr_t    dst;
	struct list_head accept_q;
	struct sock *parent;
225 226 227 228 229 230
	unsigned long flags;
};

enum {
	BT_SK_DEFER_SETUP,
	BT_SK_SUSPEND,
L
Linus Torvalds 已提交
231 232 233 234 235
};

struct bt_sock_list {
	struct hlist_head head;
	rwlock_t          lock;
236 237 238
#ifdef CONFIG_PROC_FS
        int (* custom_seq_show)(struct seq_file *, void *);
#endif
L
Linus Torvalds 已提交
239 240
};

241
int  bt_sock_register(int proto, const struct net_proto_family *ops);
242
void bt_sock_unregister(int proto);
L
Linus Torvalds 已提交
243 244
void bt_sock_link(struct bt_sock_list *l, struct sock *s);
void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
245 246
int  bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
				struct msghdr *msg, size_t len, int flags);
247 248
int  bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
			struct msghdr *msg, size_t len, int flags);
249
uint bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait);
250
int  bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
L
Linus Torvalds 已提交
251 252 253 254 255 256 257
int  bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);

void bt_accept_enqueue(struct sock *parent, struct sock *sk);
void bt_accept_unlink(struct sock *sk);
struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);

/* Skb helpers */
258
struct l2cap_ctrl {
259 260 261 262 263 264
	unsigned int	sframe:1,
			poll:1,
			final:1,
			fcs:1,
			sar:2,
			super:2;
265 266 267 268 269
	__u16		reqseq;
	__u16		txseq;
	__u8		retries;
};

270 271 272 273 274 275
struct hci_dev;

typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status);

struct hci_req_ctrl {
	bool			start;
276
	u8			event;
277 278 279
	hci_req_complete_t	complete;
};

L
Linus Torvalds 已提交
280
struct bt_skb_cb {
281 282
	__u8 pkt_type;
	__u8 incoming;
283
	__u16 expect;
284
	__u8 force_active;
285
	struct l2cap_ctrl control;
286
	struct hci_req_ctrl req;
L
Linus Torvalds 已提交
287
};
288
#define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
L
Linus Torvalds 已提交
289

A
Al Viro 已提交
290
static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
L
Linus Torvalds 已提交
291 292 293
{
	struct sk_buff *skb;

294 295
	skb = alloc_skb(len + BT_SKB_RESERVE, how);
	if (skb) {
L
Linus Torvalds 已提交
296 297 298 299 300 301
		skb_reserve(skb, BT_SKB_RESERVE);
		bt_cb(skb)->incoming  = 0;
	}
	return skb;
}

302 303
static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
					unsigned long len, int nb, int *err)
L
Linus Torvalds 已提交
304 305 306
{
	struct sk_buff *skb;

307 308
	skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
	if (skb) {
L
Linus Torvalds 已提交
309 310 311
		skb_reserve(skb, BT_SKB_RESERVE);
		bt_cb(skb)->incoming  = 0;
	}
312 313 314 315 316 317 318 319 320 321 322 323

	if (!skb && *err)
		return NULL;

	*err = sock_error(sk);
	if (*err)
		goto out;

	if (sk->sk_shutdown) {
		*err = -ECONNRESET;
		goto out;
	}
L
Linus Torvalds 已提交
324 325

	return skb;
326 327 328 329

out:
	kfree_skb(skb);
	return NULL;
L
Linus Torvalds 已提交
330 331
}

332
int bt_to_errno(__u16 code);
L
Linus Torvalds 已提交
333

334
extern int hci_sock_init(void);
335
extern void hci_sock_cleanup(void);
336 337 338 339

extern int bt_sysfs_init(void);
extern void bt_sysfs_cleanup(void);

340
extern int  bt_procfs_init(struct net *net, const char *name,
341 342 343 344
			   struct bt_sock_list* sk_list,
			   int (* seq_show)(struct seq_file *, void *));
extern void bt_procfs_cleanup(struct net *net, const char *name);

345
extern struct dentry *bt_debugfs;
346

347 348 349 350 351 352
int l2cap_init(void);
void l2cap_exit(void);

int sco_init(void);
void sco_exit(void);

353 354
void bt_sock_reclassify_lock(struct sock *sk, int proto);

L
Linus Torvalds 已提交
355
#endif /* __BLUETOOTH_H */