bluetooth.h 11.1 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
#define BT_SUBSYS_VERSION	2
33
#define BT_SUBSYS_REVISION	22
34

L
Linus Torvalds 已提交
35 36 37 38 39
#ifndef AF_BLUETOOTH
#define AF_BLUETOOTH	31
#define PF_BLUETOOTH	AF_BLUETOOTH
#endif

40 41 42 43 44
/* Bluetooth versions */
#define BLUETOOTH_VER_1_1	1
#define BLUETOOTH_VER_1_2	2
#define BLUETOOTH_VER_2_0	3

L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/* 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

62 63 64
#define BT_SECURITY	4
struct bt_security {
	__u8 level;
65
	__u8 key_size;
66 67 68 69 70
};
#define BT_SECURITY_SDP		0
#define BT_SECURITY_LOW		1
#define BT_SECURITY_MEDIUM	2
#define BT_SECURITY_HIGH	3
71
#define BT_SECURITY_FIPS	4
72

73 74
#define BT_DEFER_SETUP	7

75 76 77 78 79
#define BT_FLUSHABLE	8

#define BT_FLUSHABLE_OFF	0
#define BT_FLUSHABLE_ON		1

80 81 82 83 84 85 86
#define BT_POWER	9
struct bt_power {
	__u8 force_active;
};
#define BT_POWER_FORCE_ACTIVE_OFF 0
#define BT_POWER_FORCE_ACTIVE_ON  1

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#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

114 115 116 117 118 119 120 121
#define BT_VOICE		11
struct bt_voice {
	__u16 setting;
};

#define BT_VOICE_TRANSPARENT			0x0003
#define BT_VOICE_CVSD_16BIT			0x0060

122 123
#define BT_SNDMTU		12
#define BT_RCVMTU		13
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#define BT_PHY			14

#define BT_PHY_BR_1M_1SLOT	0x00000001
#define BT_PHY_BR_1M_3SLOT	0x00000002
#define BT_PHY_BR_1M_5SLOT	0x00000004
#define BT_PHY_EDR_2M_1SLOT	0x00000008
#define BT_PHY_EDR_2M_3SLOT	0x00000010
#define BT_PHY_EDR_2M_5SLOT	0x00000020
#define BT_PHY_EDR_3M_1SLOT	0x00000040
#define BT_PHY_EDR_3M_3SLOT	0x00000080
#define BT_PHY_EDR_3M_5SLOT	0x00000100
#define BT_PHY_LE_1M_TX		0x00000200
#define BT_PHY_LE_1M_RX		0x00000400
#define BT_PHY_LE_2M_TX		0x00000800
#define BT_PHY_LE_2M_RX		0x00001000
#define BT_PHY_LE_CODED_TX	0x00002000
#define BT_PHY_LE_CODED_RX	0x00004000
141

142 143 144 145 146 147 148 149
#define BT_MODE			15

#define BT_MODE_BASIC		0x00
#define BT_MODE_ERTM		0x01
#define BT_MODE_STREAMING	0x02
#define BT_MODE_LE_FLOWCTL	0x03
#define BT_MODE_EXT_FLOWCTL	0x04

150 151 152 153
#define BT_PKT_STATUS          16

#define BT_SCM_PKT_STATUS	0x03

154
__printf(1, 2)
155
void bt_info(const char *fmt, ...);
156
__printf(1, 2)
157 158
void bt_warn(const char *fmt, ...);
__printf(1, 2)
159
void bt_err(const char *fmt, ...);
160 161 162 163 164 165
#if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
void bt_dbg_set(bool enable);
bool bt_dbg_get(void);
__printf(1, 2)
void bt_dbg(const char *fmt, ...);
#endif
S
Szymon Janc 已提交
166
__printf(1, 2)
167 168
void bt_warn_ratelimited(const char *fmt, ...);
__printf(1, 2)
S
Szymon Janc 已提交
169
void bt_err_ratelimited(const char *fmt, ...);
170 171

#define BT_INFO(fmt, ...)	bt_info(fmt "\n", ##__VA_ARGS__)
172
#define BT_WARN(fmt, ...)	bt_warn(fmt "\n", ##__VA_ARGS__)
173
#define BT_ERR(fmt, ...)	bt_err(fmt "\n", ##__VA_ARGS__)
174 175 176 177

#if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
#define BT_DBG(fmt, ...)	bt_dbg(fmt "\n", ##__VA_ARGS__)
#else
178
#define BT_DBG(fmt, ...)	pr_debug(fmt "\n", ##__VA_ARGS__)
179
#endif
L
Linus Torvalds 已提交
180

181 182
#define bt_dev_info(hdev, fmt, ...)				\
	BT_INFO("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
183 184
#define bt_dev_warn(hdev, fmt, ...)				\
	BT_WARN("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
185 186 187 188 189
#define bt_dev_err(hdev, fmt, ...)				\
	BT_ERR("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
#define bt_dev_dbg(hdev, fmt, ...)				\
	BT_DBG("%s: " fmt, (hdev)->name, ##__VA_ARGS__)

190 191
#define bt_dev_warn_ratelimited(hdev, fmt, ...)			\
	bt_warn_ratelimited("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
192
#define bt_dev_err_ratelimited(hdev, fmt, ...)			\
193
	bt_err_ratelimited("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
194

L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207
/* 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
};

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/* 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 已提交
235 236 237
/* BD Address */
typedef struct {
	__u8 b[6];
238
} __packed bdaddr_t;
L
Linus Torvalds 已提交
239

240 241 242 243 244
/* BD Address type */
#define BDADDR_BREDR		0x00
#define BDADDR_LE_PUBLIC	0x01
#define BDADDR_LE_RANDOM	0x02

245
static inline bool bdaddr_type_is_valid(u8 type)
246 247 248 249 250 251 252 253 254 255 256
{
	switch (type) {
	case BDADDR_BREDR:
	case BDADDR_LE_PUBLIC:
	case BDADDR_LE_RANDOM:
		return true;
	}

	return false;
}

257
static inline bool bdaddr_type_is_le(u8 type)
258 259 260 261 262 263 264 265 266 267
{
	switch (type) {
	case BDADDR_LE_PUBLIC:
	case BDADDR_LE_RANDOM:
		return true;
	}

	return false;
}

268 269
#define BDADDR_ANY  (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
#define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
L
Linus Torvalds 已提交
270 271

/* Copy, swap, convert BD Address */
272
static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
L
Linus Torvalds 已提交
273 274 275
{
	return memcmp(ba1, ba2, sizeof(bdaddr_t));
}
276
static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
L
Linus Torvalds 已提交
277 278 279 280
{
	memcpy(dst, src, sizeof(bdaddr_t));
}

L
Loic Poulain 已提交
281
void baswap(bdaddr_t *dst, const bdaddr_t *src);
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290

/* Common socket structures and functions */

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

struct bt_sock {
	struct sock sk;
	struct list_head accept_q;
	struct sock *parent;
291
	unsigned long flags;
292
	void (*skb_msg_name)(struct sk_buff *, void *, int *);
293
	void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *);
294 295 296 297 298
};

enum {
	BT_SK_DEFER_SETUP,
	BT_SK_SUSPEND,
L
Linus Torvalds 已提交
299 300 301 302 303
};

struct bt_sock_list {
	struct hlist_head head;
	rwlock_t          lock;
304 305 306
#ifdef CONFIG_PROC_FS
        int (* custom_seq_show)(struct seq_file *, void *);
#endif
L
Linus Torvalds 已提交
307 308
};

309
int  bt_sock_register(int proto, const struct net_proto_family *ops);
310
void bt_sock_unregister(int proto);
L
Linus Torvalds 已提交
311 312
void bt_sock_link(struct bt_sock_list *l, struct sock *s);
void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
313 314 315 316
int  bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
		     int flags);
int  bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
			    size_t len, int flags);
317
__poll_t bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait);
318
int  bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
L
Linus Torvalds 已提交
319
int  bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
320
int  bt_sock_wait_ready(struct sock *sk, unsigned long flags);
L
Linus Torvalds 已提交
321

322
void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh);
L
Linus Torvalds 已提交
323 324 325 326
void bt_accept_unlink(struct sock *sk);
struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);

/* Skb helpers */
327
struct l2cap_ctrl {
328
	u8	sframe:1,
329 330 331 332 333
		poll:1,
		final:1,
		fcs:1,
		sar:2,
		super:2;
334 335 336 337

	u16	reqseq;
	u16	txseq;
	u8	retries;
338 339 340
	__le16  psm;
	bdaddr_t bdaddr;
	struct l2cap_chan *chan;
341 342
};

343 344 345 346
struct sco_ctrl {
	u8	pkt_status;
};

347 348
struct hci_dev;

349
typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
350 351
typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status,
				       u16 opcode, struct sk_buff *skb);
352

353 354 355
#define HCI_REQ_START	BIT(0)
#define HCI_REQ_SKB	BIT(1)

356
struct hci_ctrl {
357
	u16 opcode;
358
	u8 req_flags;
359
	u8 req_event;
360 361 362 363
	union {
		hci_req_complete_t req_complete;
		hci_req_complete_skb_t req_complete_skb;
	};
364 365
};

L
Linus Torvalds 已提交
366
struct bt_skb_cb {
367 368 369 370
	u8 pkt_type;
	u8 force_active;
	u16 expect;
	u8 incoming:1;
371 372
	union {
		struct l2cap_ctrl l2cap;
373
		struct sco_ctrl sco;
374
		struct hci_ctrl hci;
375
	};
L
Linus Torvalds 已提交
376
};
377
#define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
L
Linus Torvalds 已提交
378

379 380 381 382
#define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type
#define hci_skb_expect(skb) bt_cb((skb))->expect
#define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode

A
Al Viro 已提交
383
static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
L
Linus Torvalds 已提交
384 385 386
{
	struct sk_buff *skb;

387
	skb = alloc_skb(len + BT_SKB_RESERVE, how);
388
	if (skb)
L
Linus Torvalds 已提交
389 390 391 392
		skb_reserve(skb, BT_SKB_RESERVE);
	return skb;
}

393 394
static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
					unsigned long len, int nb, int *err)
L
Linus Torvalds 已提交
395 396 397
{
	struct sk_buff *skb;

398
	skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
399
	if (skb)
L
Linus Torvalds 已提交
400
		skb_reserve(skb, BT_SKB_RESERVE);
401 402 403 404 405 406 407 408 409 410 411 412

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

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

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

	return skb;
415 416 417 418

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

421
int bt_to_errno(u16 code);
L
Linus Torvalds 已提交
422

423 424
void hci_sock_set_flag(struct sock *sk, int nr);
void hci_sock_clear_flag(struct sock *sk, int nr);
425
int hci_sock_test_flag(struct sock *sk, int nr);
426
unsigned short hci_sock_get_channel(struct sock *sk);
427
u32 hci_sock_get_cookie(struct sock *sk);
428

429 430
int hci_sock_init(void);
void hci_sock_cleanup(void);
431

432 433
int bt_sysfs_init(void);
void bt_sysfs_cleanup(void);
434

435 436 437 438
int bt_procfs_init(struct net *net, const char *name,
		   struct bt_sock_list *sk_list,
		   int (*seq_show)(struct seq_file *, void *));
void bt_procfs_cleanup(struct net *net, const char *name);
439

440
extern struct dentry *bt_debugfs;
441

442 443 444
int l2cap_init(void);
void l2cap_exit(void);

445
#if IS_ENABLED(CONFIG_BT_BREDR)
446 447
int sco_init(void);
void sco_exit(void);
448 449 450 451 452 453 454 455 456 457
#else
static inline int sco_init(void)
{
	return 0;
}

static inline void sco_exit(void)
{
}
#endif
458

459 460 461
int mgmt_init(void);
void mgmt_exit(void);

462 463
void bt_sock_reclassify_lock(struct sock *sk, int proto);

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