11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds BlueZ - Bluetooth protocol stack for Linux
31da177e4SLinus Torvalds Copyright (C) 2000-2001 Qualcomm Incorporated
40fe8c8d0SIulia Tanasescu Copyright 2023 NXP
51da177e4SLinus Torvalds
61da177e4SLinus Torvalds Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify
91da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as
101da177e4SLinus Torvalds published by the Free Software Foundation;
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
131da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
141da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
151da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
161da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
171da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
181da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
191da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
221da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
231da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED.
241da177e4SLinus Torvalds */
251da177e4SLinus Torvalds
261da177e4SLinus Torvalds #ifndef __BLUETOOTH_H
271da177e4SLinus Torvalds #define __BLUETOOTH_H
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds #include <linux/poll.h>
301da177e4SLinus Torvalds #include <net/sock.h>
31256a06c8SMasatake YAMATO #include <linux/seq_file.h>
321da177e4SLinus Torvalds
339e8305b3SMarcel Holtmann #define BT_SUBSYS_VERSION 2
344037a774SMarcel Holtmann #define BT_SUBSYS_REVISION 22
35dd31506dSMarcel Holtmann
361da177e4SLinus Torvalds #ifndef AF_BLUETOOTH
371da177e4SLinus Torvalds #define AF_BLUETOOTH 31
381da177e4SLinus Torvalds #define PF_BLUETOOTH AF_BLUETOOTH
391da177e4SLinus Torvalds #endif
401da177e4SLinus Torvalds
41d095c1ebSAndrei Emeltchenko /* Bluetooth versions */
42d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_1_1 1
43d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_1_2 2
44d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_2_0 3
45cde1a8a9SIsmael Ferreras Morezuelas #define BLUETOOTH_VER_2_1 4
46cde1a8a9SIsmael Ferreras Morezuelas #define BLUETOOTH_VER_4_0 6
47d095c1ebSAndrei Emeltchenko
481da177e4SLinus Torvalds /* Reserv for core and drivers use */
491da177e4SLinus Torvalds #define BT_SKB_RESERVE 8
501da177e4SLinus Torvalds
511da177e4SLinus Torvalds #define BTPROTO_L2CAP 0
521da177e4SLinus Torvalds #define BTPROTO_HCI 1
531da177e4SLinus Torvalds #define BTPROTO_SCO 2
541da177e4SLinus Torvalds #define BTPROTO_RFCOMM 3
551da177e4SLinus Torvalds #define BTPROTO_BNEP 4
561da177e4SLinus Torvalds #define BTPROTO_CMTP 5
571da177e4SLinus Torvalds #define BTPROTO_HIDP 6
581da177e4SLinus Torvalds #define BTPROTO_AVDTP 7
5926afbd82SLuiz Augusto von Dentz #define BTPROTO_ISO 8
6026afbd82SLuiz Augusto von Dentz #define BTPROTO_LAST BTPROTO_ISO
611da177e4SLinus Torvalds
621da177e4SLinus Torvalds #define SOL_HCI 0
631da177e4SLinus Torvalds #define SOL_L2CAP 6
641da177e4SLinus Torvalds #define SOL_SCO 17
651da177e4SLinus Torvalds #define SOL_RFCOMM 18
661da177e4SLinus Torvalds
678c1b2355SMarcel Holtmann #define BT_SECURITY 4
688c1b2355SMarcel Holtmann struct bt_security {
698c1b2355SMarcel Holtmann __u8 level;
708f360119SVinicius Costa Gomes __u8 key_size;
718c1b2355SMarcel Holtmann };
728c1b2355SMarcel Holtmann #define BT_SECURITY_SDP 0
738c1b2355SMarcel Holtmann #define BT_SECURITY_LOW 1
748c1b2355SMarcel Holtmann #define BT_SECURITY_MEDIUM 2
758c1b2355SMarcel Holtmann #define BT_SECURITY_HIGH 3
767b5a9241SMarcel Holtmann #define BT_SECURITY_FIPS 4
778c1b2355SMarcel Holtmann
78c4f912e1SMarcel Holtmann #define BT_DEFER_SETUP 7
79c4f912e1SMarcel Holtmann
80e702112fSAndrei Emeltchenko #define BT_FLUSHABLE 8
81e702112fSAndrei Emeltchenko
82e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_OFF 0
83e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_ON 1
84e702112fSAndrei Emeltchenko
8514b12d0bSJaikumar Ganesh #define BT_POWER 9
8614b12d0bSJaikumar Ganesh struct bt_power {
8714b12d0bSJaikumar Ganesh __u8 force_active;
8814b12d0bSJaikumar Ganesh };
8914b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_OFF 0
9014b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_ON 1
9114b12d0bSJaikumar Ganesh
92c14968b0SMat Martineau #define BT_CHANNEL_POLICY 10
93c14968b0SMat Martineau
94c14968b0SMat Martineau /* BR/EDR only (default policy)
95c14968b0SMat Martineau * AMP controllers cannot be used.
96c14968b0SMat Martineau * Channel move requests from the remote device are denied.
97c14968b0SMat Martineau * If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
98c14968b0SMat Martineau */
99c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_ONLY 0
100c14968b0SMat Martineau
101c14968b0SMat Martineau /* BR/EDR Preferred
102c14968b0SMat Martineau * Allow use of AMP controllers.
103c14968b0SMat Martineau * If the L2CAP channel is currently on AMP, move it to BR/EDR.
104c14968b0SMat Martineau * Channel move requests from the remote device are allowed.
105c14968b0SMat Martineau */
106c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_PREFERRED 1
107c14968b0SMat Martineau
108c14968b0SMat Martineau /* AMP Preferred
109c14968b0SMat Martineau * Allow use of AMP controllers
110c14968b0SMat Martineau * If the L2CAP channel is currently on BR/EDR and AMP controller
111c14968b0SMat Martineau * resources are available, initiate a channel move to AMP.
112c14968b0SMat Martineau * Channel move requests from the remote device are allowed.
113c14968b0SMat Martineau * If the L2CAP socket has not been connected yet, try to create
114c14968b0SMat Martineau * and configure the channel directly on an AMP controller rather
115c14968b0SMat Martineau * than BR/EDR.
116c14968b0SMat Martineau */
117c14968b0SMat Martineau #define BT_CHANNEL_POLICY_AMP_PREFERRED 2
118c14968b0SMat Martineau
119ad10b1a4SFrédéric Dalleau #define BT_VOICE 11
120ad10b1a4SFrédéric Dalleau struct bt_voice {
121ad10b1a4SFrédéric Dalleau __u16 setting;
122ad10b1a4SFrédéric Dalleau };
123ad10b1a4SFrédéric Dalleau
124ad10b1a4SFrédéric Dalleau #define BT_VOICE_TRANSPARENT 0x0003
125ad10b1a4SFrédéric Dalleau #define BT_VOICE_CVSD_16BIT 0x0060
126*3bf09c68SFrédéric Danis #define BT_VOICE_TRANSPARENT_16BIT 0x0063
127ad10b1a4SFrédéric Dalleau
1281f435424SJohan Hedberg #define BT_SNDMTU 12
1291f435424SJohan Hedberg #define BT_RCVMTU 13
130eab2404bSLuiz Augusto von Dentz #define BT_PHY 14
131eab2404bSLuiz Augusto von Dentz
132eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_1SLOT 0x00000001
133eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_3SLOT 0x00000002
134eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_5SLOT 0x00000004
135eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_1SLOT 0x00000008
136eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_3SLOT 0x00000010
137eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_5SLOT 0x00000020
138eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_1SLOT 0x00000040
139eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_3SLOT 0x00000080
140eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_5SLOT 0x00000100
141eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_TX 0x00000200
142eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_RX 0x00000400
143eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_TX 0x00000800
144eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_RX 0x00001000
145eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_TX 0x00002000
146eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_RX 0x00004000
1471f435424SJohan Hedberg
1483ee7b7cdSLuiz Augusto von Dentz #define BT_MODE 15
1493ee7b7cdSLuiz Augusto von Dentz
1503ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_BASIC 0x00
1513ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_ERTM 0x01
1523ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_STREAMING 0x02
1533ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_LE_FLOWCTL 0x03
1543ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_EXT_FLOWCTL 0x04
1553ee7b7cdSLuiz Augusto von Dentz
15600398e1dSAlain Michaud #define BT_PKT_STATUS 16
15700398e1dSAlain Michaud
15800398e1dSAlain Michaud #define BT_SCM_PKT_STATUS 0x03
15900398e1dSAlain Michaud
16026afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS 17
16126afbd82SLuiz Augusto von Dentz
16226afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS_CIG_UNSET 0xff
16326afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS_CIS_UNSET 0xff
16426afbd82SLuiz Augusto von Dentz
165eca0ae4aSLuiz Augusto von Dentz #define BT_ISO_QOS_BIG_UNSET 0xff
166eca0ae4aSLuiz Augusto von Dentz #define BT_ISO_QOS_BIS_UNSET 0xff
167eca0ae4aSLuiz Augusto von Dentz
168c8321645SLuiz Augusto von Dentz #define BT_ISO_SYNC_TIMEOUT 0x07d0 /* 20 secs */
169c8321645SLuiz Augusto von Dentz
17026afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos {
17126afbd82SLuiz Augusto von Dentz __u32 interval;
17226afbd82SLuiz Augusto von Dentz __u16 latency;
17326afbd82SLuiz Augusto von Dentz __u16 sdu;
17426afbd82SLuiz Augusto von Dentz __u8 phy;
17526afbd82SLuiz Augusto von Dentz __u8 rtn;
17626afbd82SLuiz Augusto von Dentz };
17726afbd82SLuiz Augusto von Dentz
1780fe8c8d0SIulia Tanasescu struct bt_iso_ucast_qos {
17926afbd82SLuiz Augusto von Dentz __u8 cig;
18026afbd82SLuiz Augusto von Dentz __u8 cis;
18126afbd82SLuiz Augusto von Dentz __u8 sca;
18226afbd82SLuiz Augusto von Dentz __u8 packing;
18326afbd82SLuiz Augusto von Dentz __u8 framing;
18426afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos in;
18526afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos out;
18626afbd82SLuiz Augusto von Dentz };
18726afbd82SLuiz Augusto von Dentz
1880fe8c8d0SIulia Tanasescu struct bt_iso_bcast_qos {
1890fe8c8d0SIulia Tanasescu __u8 big;
1900fe8c8d0SIulia Tanasescu __u8 bis;
19114f0dcecSLuiz Augusto von Dentz __u8 sync_factor;
1920fe8c8d0SIulia Tanasescu __u8 packing;
1930fe8c8d0SIulia Tanasescu __u8 framing;
1940fe8c8d0SIulia Tanasescu struct bt_iso_io_qos in;
1950fe8c8d0SIulia Tanasescu struct bt_iso_io_qos out;
1960fe8c8d0SIulia Tanasescu __u8 encryption;
1970fe8c8d0SIulia Tanasescu __u8 bcode[16];
1980fe8c8d0SIulia Tanasescu __u8 options;
1990fe8c8d0SIulia Tanasescu __u16 skip;
2000fe8c8d0SIulia Tanasescu __u16 sync_timeout;
2010fe8c8d0SIulia Tanasescu __u8 sync_cte_type;
2020fe8c8d0SIulia Tanasescu __u8 mse;
2030fe8c8d0SIulia Tanasescu __u16 timeout;
2040fe8c8d0SIulia Tanasescu };
2050fe8c8d0SIulia Tanasescu
2060fe8c8d0SIulia Tanasescu struct bt_iso_qos {
2070fe8c8d0SIulia Tanasescu union {
2080fe8c8d0SIulia Tanasescu struct bt_iso_ucast_qos ucast;
2090fe8c8d0SIulia Tanasescu struct bt_iso_bcast_qos bcast;
2100fe8c8d0SIulia Tanasescu };
2110fe8c8d0SIulia Tanasescu };
2120fe8c8d0SIulia Tanasescu
21326afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_1M 0x01
21426afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_2M 0x02
21526afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_CODED 0x04
21626afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_ANY (BT_ISO_PHY_1M | BT_ISO_PHY_2M | \
21726afbd82SLuiz Augusto von Dentz BT_ISO_PHY_CODED)
21826afbd82SLuiz Augusto von Dentz
219248733e8SKiran K #define BT_CODEC 19
220248733e8SKiran K
221248733e8SKiran K struct bt_codec_caps {
222248733e8SKiran K __u8 len;
223248733e8SKiran K __u8 data[];
224248733e8SKiran K } __packed;
225248733e8SKiran K
226248733e8SKiran K struct bt_codec {
227248733e8SKiran K __u8 id;
228248733e8SKiran K __u16 cid;
229248733e8SKiran K __u16 vid;
230248733e8SKiran K __u8 data_path;
231248733e8SKiran K __u8 num_caps;
232248733e8SKiran K } __packed;
233248733e8SKiran K
234248733e8SKiran K struct bt_codecs {
235248733e8SKiran K __u8 num_codecs;
236248733e8SKiran K struct bt_codec codecs[];
237248733e8SKiran K } __packed;
238248733e8SKiran K
239f6873401SKiran K #define BT_CODEC_CVSD 0x02
240b2af264aSKiran K #define BT_CODEC_TRANSPARENT 0x03
241904c139aSKiran K #define BT_CODEC_MSBC 0x05
242f6873401SKiran K
243f764a6c2SLuiz Augusto von Dentz #define BT_ISO_BASE 20
244f764a6c2SLuiz Augusto von Dentz
2453ed7003eSJoe Perches __printf(1, 2)
2462b0bf6c8SJoe Perches void bt_info(const char *fmt, ...);
2473ed7003eSJoe Perches __printf(1, 2)
248594b31eaSFrederic Danis void bt_warn(const char *fmt, ...);
249594b31eaSFrederic Danis __printf(1, 2)
2502b0bf6c8SJoe Perches void bt_err(const char *fmt, ...);
251e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
252e625e50cSMarcel Holtmann void bt_dbg_set(bool enable);
253e625e50cSMarcel Holtmann bool bt_dbg_get(void);
254e625e50cSMarcel Holtmann __printf(1, 2)
255e625e50cSMarcel Holtmann void bt_dbg(const char *fmt, ...);
256e625e50cSMarcel Holtmann #endif
257e781b7f7SSzymon Janc __printf(1, 2)
25836278a5dSAlain Michaud void bt_warn_ratelimited(const char *fmt, ...);
25936278a5dSAlain Michaud __printf(1, 2)
260e781b7f7SSzymon Janc void bt_err_ratelimited(const char *fmt, ...);
261e1447d8dSJoe Perches
2623ed7003eSJoe Perches #define BT_INFO(fmt, ...) bt_info(fmt "\n", ##__VA_ARGS__)
263594b31eaSFrederic Danis #define BT_WARN(fmt, ...) bt_warn(fmt "\n", ##__VA_ARGS__)
2643ed7003eSJoe Perches #define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__)
265e625e50cSMarcel Holtmann
266e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
267e625e50cSMarcel Holtmann #define BT_DBG(fmt, ...) bt_dbg(fmt "\n", ##__VA_ARGS__)
268e625e50cSMarcel Holtmann #else
2693ed7003eSJoe Perches #define BT_DBG(fmt, ...) pr_debug(fmt "\n", ##__VA_ARGS__)
270e625e50cSMarcel Holtmann #endif
2711da177e4SLinus Torvalds
2729b392e0eSLuiz Augusto von Dentz #define bt_dev_name(hdev) ((hdev) ? (hdev)->name : "null")
2739b392e0eSLuiz Augusto von Dentz
2746f558b70SLoic Poulain #define bt_dev_info(hdev, fmt, ...) \
2759b392e0eSLuiz Augusto von Dentz BT_INFO("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
276594b31eaSFrederic Danis #define bt_dev_warn(hdev, fmt, ...) \
2779b392e0eSLuiz Augusto von Dentz BT_WARN("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2786f558b70SLoic Poulain #define bt_dev_err(hdev, fmt, ...) \
2799b392e0eSLuiz Augusto von Dentz BT_ERR("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2806f558b70SLoic Poulain #define bt_dev_dbg(hdev, fmt, ...) \
2819b392e0eSLuiz Augusto von Dentz BT_DBG("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2826f558b70SLoic Poulain
28336278a5dSAlain Michaud #define bt_dev_warn_ratelimited(hdev, fmt, ...) \
2849b392e0eSLuiz Augusto von Dentz bt_warn_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2852064ee33SMarcel Holtmann #define bt_dev_err_ratelimited(hdev, fmt, ...) \
2869b392e0eSLuiz Augusto von Dentz bt_err_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2872064ee33SMarcel Holtmann
2881da177e4SLinus Torvalds /* Connection and socket states */
2891da177e4SLinus Torvalds enum {
2901da177e4SLinus Torvalds BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
2911da177e4SLinus Torvalds BT_OPEN,
2921da177e4SLinus Torvalds BT_BOUND,
2931da177e4SLinus Torvalds BT_LISTEN,
2941da177e4SLinus Torvalds BT_CONNECT,
2951da177e4SLinus Torvalds BT_CONNECT2,
2961da177e4SLinus Torvalds BT_CONFIG,
2971da177e4SLinus Torvalds BT_DISCONN,
2981da177e4SLinus Torvalds BT_CLOSED
2991da177e4SLinus Torvalds };
3001da177e4SLinus Torvalds
30120d1803aSAndrei Emeltchenko /* If unused will be removed by compiler */
state_to_string(int state)30220d1803aSAndrei Emeltchenko static inline const char *state_to_string(int state)
30320d1803aSAndrei Emeltchenko {
30420d1803aSAndrei Emeltchenko switch (state) {
30520d1803aSAndrei Emeltchenko case BT_CONNECTED:
30620d1803aSAndrei Emeltchenko return "BT_CONNECTED";
30720d1803aSAndrei Emeltchenko case BT_OPEN:
30820d1803aSAndrei Emeltchenko return "BT_OPEN";
30920d1803aSAndrei Emeltchenko case BT_BOUND:
31020d1803aSAndrei Emeltchenko return "BT_BOUND";
31120d1803aSAndrei Emeltchenko case BT_LISTEN:
31220d1803aSAndrei Emeltchenko return "BT_LISTEN";
31320d1803aSAndrei Emeltchenko case BT_CONNECT:
31420d1803aSAndrei Emeltchenko return "BT_CONNECT";
31520d1803aSAndrei Emeltchenko case BT_CONNECT2:
31620d1803aSAndrei Emeltchenko return "BT_CONNECT2";
31720d1803aSAndrei Emeltchenko case BT_CONFIG:
31820d1803aSAndrei Emeltchenko return "BT_CONFIG";
31920d1803aSAndrei Emeltchenko case BT_DISCONN:
32020d1803aSAndrei Emeltchenko return "BT_DISCONN";
32120d1803aSAndrei Emeltchenko case BT_CLOSED:
32220d1803aSAndrei Emeltchenko return "BT_CLOSED";
32320d1803aSAndrei Emeltchenko }
32420d1803aSAndrei Emeltchenko
32520d1803aSAndrei Emeltchenko return "invalid state";
32620d1803aSAndrei Emeltchenko }
32720d1803aSAndrei Emeltchenko
3281da177e4SLinus Torvalds /* BD Address */
3291da177e4SLinus Torvalds typedef struct {
3301da177e4SLinus Torvalds __u8 b[6];
33166c853ccSGustavo F. Padovan } __packed bdaddr_t;
3321da177e4SLinus Torvalds
333591f47f3SAndre Guedes /* BD Address type */
334591f47f3SAndre Guedes #define BDADDR_BREDR 0x00
335591f47f3SAndre Guedes #define BDADDR_LE_PUBLIC 0x01
336591f47f3SAndre Guedes #define BDADDR_LE_RANDOM 0x02
337591f47f3SAndre Guedes
bdaddr_type_is_valid(u8 type)33821fcf572SPavel Machek static inline bool bdaddr_type_is_valid(u8 type)
339679efe2bSJohan Hedberg {
340679efe2bSJohan Hedberg switch (type) {
341679efe2bSJohan Hedberg case BDADDR_BREDR:
342679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC:
343679efe2bSJohan Hedberg case BDADDR_LE_RANDOM:
344679efe2bSJohan Hedberg return true;
345679efe2bSJohan Hedberg }
346679efe2bSJohan Hedberg
347679efe2bSJohan Hedberg return false;
348679efe2bSJohan Hedberg }
349679efe2bSJohan Hedberg
bdaddr_type_is_le(u8 type)35021fcf572SPavel Machek static inline bool bdaddr_type_is_le(u8 type)
351679efe2bSJohan Hedberg {
352679efe2bSJohan Hedberg switch (type) {
353679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC:
354679efe2bSJohan Hedberg case BDADDR_LE_RANDOM:
355679efe2bSJohan Hedberg return true;
356679efe2bSJohan Hedberg }
357679efe2bSJohan Hedberg
358679efe2bSJohan Hedberg return false;
359679efe2bSJohan Hedberg }
360679efe2bSJohan Hedberg
3611da177e4SLinus Torvalds #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
362a59ac2f7SMarcel Holtmann #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
3631da177e4SLinus Torvalds
3641da177e4SLinus Torvalds /* Copy, swap, convert BD Address */
bacmp(const bdaddr_t * ba1,const bdaddr_t * ba2)365f53c20e9SDavid Herrmann static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds return memcmp(ba1, ba2, sizeof(bdaddr_t));
3681da177e4SLinus Torvalds }
bacpy(bdaddr_t * dst,const bdaddr_t * src)369f53c20e9SDavid Herrmann static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds memcpy(dst, src, sizeof(bdaddr_t));
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds
37465bce462SLoic Poulain void baswap(bdaddr_t *dst, const bdaddr_t *src);
3751da177e4SLinus Torvalds
3761da177e4SLinus Torvalds /* Common socket structures and functions */
3771da177e4SLinus Torvalds
3781da177e4SLinus Torvalds #define bt_sk(__sk) ((struct bt_sock *) __sk)
3791da177e4SLinus Torvalds
3801da177e4SLinus Torvalds struct bt_sock {
3811da177e4SLinus Torvalds struct sock sk;
3821da177e4SLinus Torvalds struct list_head accept_q;
3831da177e4SLinus Torvalds struct sock *parent;
384c5daa683SGustavo Padovan unsigned long flags;
385d9763698SMarcel Holtmann void (*skb_msg_name)(struct sk_buff *, void *, int *);
38600398e1dSAlain Michaud void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *);
387c5daa683SGustavo Padovan };
388c5daa683SGustavo Padovan
389c5daa683SGustavo Padovan enum {
390c5daa683SGustavo Padovan BT_SK_DEFER_SETUP,
391c5daa683SGustavo Padovan BT_SK_SUSPEND,
3923f19ffb2SLuiz Augusto von Dentz BT_SK_PKT_STATUS
3931da177e4SLinus Torvalds };
3941da177e4SLinus Torvalds
3951da177e4SLinus Torvalds struct bt_sock_list {
3961da177e4SLinus Torvalds struct hlist_head head;
3971da177e4SLinus Torvalds rwlock_t lock;
398256a06c8SMasatake YAMATO #ifdef CONFIG_PROC_FS
399256a06c8SMasatake YAMATO int (* custom_seq_show)(struct seq_file *, void *);
400256a06c8SMasatake YAMATO #endif
4011da177e4SLinus Torvalds };
4021da177e4SLinus Torvalds
403ec1b4cf7SStephen Hemminger int bt_sock_register(int proto, const struct net_proto_family *ops);
404be9f97f0SDavid Herrmann void bt_sock_unregister(int proto);
4051da177e4SLinus Torvalds void bt_sock_link(struct bt_sock_list *l, struct sock *s);
4061da177e4SLinus Torvalds void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
407d30803f6SLuiz Augusto von Dentz bool bt_sock_linked(struct bt_sock_list *l, struct sock *s);
4086bfa273eSLuiz Augusto von Dentz struct sock *bt_sock_alloc(struct net *net, struct socket *sock,
4096bfa273eSLuiz Augusto von Dentz struct proto *prot, int proto, gfp_t prio, int kern);
4101b784140SYing Xue int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
4111b784140SYing Xue int flags);
4121b784140SYing Xue int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
4131b784140SYing Xue size_t len, int flags);
414a11e1d43SLinus Torvalds __poll_t bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait);
4153241ad82SMarcel Holtmann int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
4161da177e4SLinus Torvalds int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
417da891217SGavin Li int bt_sock_wait_ready(struct sock *sk, unsigned int msg_flags);
4181da177e4SLinus Torvalds
419c4f5627fSMatthias Kaehlcke void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh);
4201da177e4SLinus Torvalds void bt_accept_unlink(struct sock *sk);
4211da177e4SLinus Torvalds struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);
4221da177e4SLinus Torvalds
4231da177e4SLinus Torvalds /* Skb helpers */
42400e3112cSMat Martineau struct l2cap_ctrl {
42521fcf572SPavel Machek u8 sframe:1,
42600e3112cSMat Martineau poll:1,
42700e3112cSMat Martineau final:1,
42800e3112cSMat Martineau fcs:1,
42900e3112cSMat Martineau sar:2,
43000e3112cSMat Martineau super:2;
43121fcf572SPavel Machek
43221fcf572SPavel Machek u16 reqseq;
43321fcf572SPavel Machek u16 txseq;
43421fcf572SPavel Machek u8 retries;
435a4368ff3SJohan Hedberg __le16 psm;
436a4368ff3SJohan Hedberg bdaddr_t bdaddr;
437a4368ff3SJohan Hedberg struct l2cap_chan *chan;
43800e3112cSMat Martineau };
43900e3112cSMat Martineau
4403119ae95SJohan Hedberg struct hci_dev;
4413119ae95SJohan Hedberg
4421904a853SMarcel Holtmann typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
443e6214487SJohan Hedberg typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status,
444e6214487SJohan Hedberg u16 opcode, struct sk_buff *skb);
4453119ae95SJohan Hedberg
44644d27137SJohan Hedberg #define HCI_REQ_START BIT(0)
44744d27137SJohan Hedberg #define HCI_REQ_SKB BIT(1)
44844d27137SJohan Hedberg
449242c0ebdSMarcel Holtmann struct hci_ctrl {
450cba6b758SLuiz Augusto von Dentz struct sock *sk;
45121fcf572SPavel Machek u16 opcode;
45244d27137SJohan Hedberg u8 req_flags;
453242c0ebdSMarcel Holtmann u8 req_event;
45444d27137SJohan Hedberg union {
455242c0ebdSMarcel Holtmann hci_req_complete_t req_complete;
456242c0ebdSMarcel Holtmann hci_req_complete_skb_t req_complete_skb;
457db6e3e8dSJohan Hedberg };
45844d27137SJohan Hedberg };
459db6e3e8dSJohan Hedberg
4608aca46f9SLuiz Augusto von Dentz struct mgmt_ctrl {
4618aca46f9SLuiz Augusto von Dentz struct hci_dev *hdev;
4628aca46f9SLuiz Augusto von Dentz u16 opcode;
4638aca46f9SLuiz Augusto von Dentz };
4648aca46f9SLuiz Augusto von Dentz
4651da177e4SLinus Torvalds struct bt_skb_cb {
46621fcf572SPavel Machek u8 pkt_type;
46721fcf572SPavel Machek u8 force_active;
46821fcf572SPavel Machek u16 expect;
46921fcf572SPavel Machek u8 incoming:1;
4703f19ffb2SLuiz Augusto von Dentz u8 pkt_status:2;
471db6e3e8dSJohan Hedberg union {
472a4368ff3SJohan Hedberg struct l2cap_ctrl l2cap;
473242c0ebdSMarcel Holtmann struct hci_ctrl hci;
4748aca46f9SLuiz Augusto von Dentz struct mgmt_ctrl mgmt;
47569ae5065SLuiz Augusto von Dentz struct scm_creds creds;
476db6e3e8dSJohan Hedberg };
4771da177e4SLinus Torvalds };
4781c2acffbSGustavo F. Padovan #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
4791da177e4SLinus Torvalds
480f5c4a42aSMarcel Holtmann #define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type
4813f19ffb2SLuiz Augusto von Dentz #define hci_skb_pkt_status(skb) bt_cb((skb))->pkt_status
482f5c4a42aSMarcel Holtmann #define hci_skb_expect(skb) bt_cb((skb))->expect
483f5c4a42aSMarcel Holtmann #define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode
48485b56857SLuiz Augusto von Dentz #define hci_skb_event(skb) bt_cb((skb))->hci.req_event
485cba6b758SLuiz Augusto von Dentz #define hci_skb_sk(skb) bt_cb((skb))->hci.sk
486f5c4a42aSMarcel Holtmann
bt_skb_alloc(unsigned int len,gfp_t how)487dd0fc66fSAl Viro static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds struct sk_buff *skb;
4901da177e4SLinus Torvalds
491c3c7ea65SGustavo Padovan skb = alloc_skb(len + BT_SKB_RESERVE, how);
4925fcc86bdSJohan Hedberg if (skb)
4931da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE);
4941da177e4SLinus Torvalds return skb;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds
bt_skb_send_alloc(struct sock * sk,unsigned long len,int nb,int * err)4975a9d0a3fSWaldemar Rymarkiewicz static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
4985a9d0a3fSWaldemar Rymarkiewicz unsigned long len, int nb, int *err)
4991da177e4SLinus Torvalds {
5001da177e4SLinus Torvalds struct sk_buff *skb;
5011da177e4SLinus Torvalds
502c3c7ea65SGustavo Padovan skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
5035fcc86bdSJohan Hedberg if (skb)
5041da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE);
505e454c844SGustavo F. Padovan
506e454c844SGustavo F. Padovan if (!skb && *err)
507e454c844SGustavo F. Padovan return NULL;
508e454c844SGustavo F. Padovan
509e454c844SGustavo F. Padovan *err = sock_error(sk);
510e454c844SGustavo F. Padovan if (*err)
511e454c844SGustavo F. Padovan goto out;
512e454c844SGustavo F. Padovan
513e454c844SGustavo F. Padovan if (sk->sk_shutdown) {
514e454c844SGustavo F. Padovan *err = -ECONNRESET;
515e454c844SGustavo F. Padovan goto out;
516e454c844SGustavo F. Padovan }
5171da177e4SLinus Torvalds
5181da177e4SLinus Torvalds return skb;
519e454c844SGustavo F. Padovan
520e454c844SGustavo F. Padovan out:
521e454c844SGustavo F. Padovan kfree_skb(skb);
522e454c844SGustavo F. Padovan return NULL;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds
52538f64f65SLuiz Augusto von Dentz /* Shall not be called with lock_sock held */
bt_skb_sendmsg(struct sock * sk,struct msghdr * msg,size_t len,size_t mtu,size_t headroom,size_t tailroom)52638f64f65SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk,
52738f64f65SLuiz Augusto von Dentz struct msghdr *msg,
52838f64f65SLuiz Augusto von Dentz size_t len, size_t mtu,
52938f64f65SLuiz Augusto von Dentz size_t headroom, size_t tailroom)
53038f64f65SLuiz Augusto von Dentz {
53138f64f65SLuiz Augusto von Dentz struct sk_buff *skb;
53238f64f65SLuiz Augusto von Dentz size_t size = min_t(size_t, len, mtu);
53338f64f65SLuiz Augusto von Dentz int err;
53438f64f65SLuiz Augusto von Dentz
53538f64f65SLuiz Augusto von Dentz skb = bt_skb_send_alloc(sk, size + headroom + tailroom,
53638f64f65SLuiz Augusto von Dentz msg->msg_flags & MSG_DONTWAIT, &err);
53738f64f65SLuiz Augusto von Dentz if (!skb)
53838f64f65SLuiz Augusto von Dentz return ERR_PTR(err);
53938f64f65SLuiz Augusto von Dentz
54038f64f65SLuiz Augusto von Dentz skb_reserve(skb, headroom);
54138f64f65SLuiz Augusto von Dentz skb_tailroom_reserve(skb, mtu, tailroom);
54238f64f65SLuiz Augusto von Dentz
54338f64f65SLuiz Augusto von Dentz if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) {
54438f64f65SLuiz Augusto von Dentz kfree_skb(skb);
54538f64f65SLuiz Augusto von Dentz return ERR_PTR(-EFAULT);
54638f64f65SLuiz Augusto von Dentz }
54738f64f65SLuiz Augusto von Dentz
54838f64f65SLuiz Augusto von Dentz skb->priority = sk->sk_priority;
54938f64f65SLuiz Augusto von Dentz
55038f64f65SLuiz Augusto von Dentz return skb;
55138f64f65SLuiz Augusto von Dentz }
55238f64f65SLuiz Augusto von Dentz
55397e4e802SLuiz Augusto von Dentz /* Similar to bt_skb_sendmsg but can split the msg into multiple fragments
55497e4e802SLuiz Augusto von Dentz * accourding to the MTU.
55597e4e802SLuiz Augusto von Dentz */
bt_skb_sendmmsg(struct sock * sk,struct msghdr * msg,size_t len,size_t mtu,size_t headroom,size_t tailroom)55697e4e802SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmmsg(struct sock *sk,
55797e4e802SLuiz Augusto von Dentz struct msghdr *msg,
55897e4e802SLuiz Augusto von Dentz size_t len, size_t mtu,
55997e4e802SLuiz Augusto von Dentz size_t headroom, size_t tailroom)
56097e4e802SLuiz Augusto von Dentz {
56197e4e802SLuiz Augusto von Dentz struct sk_buff *skb, **frag;
56297e4e802SLuiz Augusto von Dentz
56397e4e802SLuiz Augusto von Dentz skb = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom);
5646f43f616SDan Carpenter if (IS_ERR(skb))
56597e4e802SLuiz Augusto von Dentz return skb;
56697e4e802SLuiz Augusto von Dentz
56797e4e802SLuiz Augusto von Dentz len -= skb->len;
56897e4e802SLuiz Augusto von Dentz if (!len)
56997e4e802SLuiz Augusto von Dentz return skb;
57097e4e802SLuiz Augusto von Dentz
57197e4e802SLuiz Augusto von Dentz /* Add remaining data over MTU as continuation fragments */
57297e4e802SLuiz Augusto von Dentz frag = &skb_shinfo(skb)->frag_list;
57397e4e802SLuiz Augusto von Dentz while (len) {
57497e4e802SLuiz Augusto von Dentz struct sk_buff *tmp;
57597e4e802SLuiz Augusto von Dentz
57697e4e802SLuiz Augusto von Dentz tmp = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom);
577266191aaSLuiz Augusto von Dentz if (IS_ERR(tmp)) {
57829fb6083SLuiz Augusto von Dentz return skb;
57997e4e802SLuiz Augusto von Dentz }
58097e4e802SLuiz Augusto von Dentz
58197e4e802SLuiz Augusto von Dentz len -= tmp->len;
58297e4e802SLuiz Augusto von Dentz
58397e4e802SLuiz Augusto von Dentz *frag = tmp;
58497e4e802SLuiz Augusto von Dentz frag = &(*frag)->next;
58597e4e802SLuiz Augusto von Dentz }
58697e4e802SLuiz Augusto von Dentz
58797e4e802SLuiz Augusto von Dentz return skb;
58897e4e802SLuiz Augusto von Dentz }
58997e4e802SLuiz Augusto von Dentz
bt_copy_from_sockptr(void * dst,size_t dst_size,sockptr_t src,size_t src_size)59072473db9SLuiz Augusto von Dentz static inline int bt_copy_from_sockptr(void *dst, size_t dst_size,
59172473db9SLuiz Augusto von Dentz sockptr_t src, size_t src_size)
59272473db9SLuiz Augusto von Dentz {
59372473db9SLuiz Augusto von Dentz if (dst_size > src_size)
59472473db9SLuiz Augusto von Dentz return -EINVAL;
59572473db9SLuiz Augusto von Dentz
59672473db9SLuiz Augusto von Dentz return copy_from_sockptr(dst, src, dst_size);
59772473db9SLuiz Augusto von Dentz }
59872473db9SLuiz Augusto von Dentz
59921fcf572SPavel Machek int bt_to_errno(u16 code);
600ca2045e0SLuiz Augusto von Dentz __u8 bt_status(int err);
6011da177e4SLinus Torvalds
6026befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr);
6036befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr);
604c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr);
605d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk);
60670ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk);
6076befc644SMarcel Holtmann
608e74e58f8SJoe Perches int hci_sock_init(void);
609e74e58f8SJoe Perches void hci_sock_cleanup(void);
6106516455dSMarcel Holtmann
611e74e58f8SJoe Perches int bt_sysfs_init(void);
612e74e58f8SJoe Perches void bt_sysfs_cleanup(void);
6136516455dSMarcel Holtmann
614e74e58f8SJoe Perches int bt_procfs_init(struct net *net, const char *name,
615256a06c8SMasatake YAMATO struct bt_sock_list *sk_list,
616256a06c8SMasatake YAMATO int (*seq_show)(struct seq_file *, void *));
617e74e58f8SJoe Perches void bt_procfs_cleanup(struct net *net, const char *name);
618256a06c8SMasatake YAMATO
619aef7d97cSMarcel Holtmann extern struct dentry *bt_debugfs;
620be9d1227SMarcel Holtmann
62164274518SGustavo F. Padovan int l2cap_init(void);
62264274518SGustavo F. Padovan void l2cap_exit(void);
62364274518SGustavo F. Padovan
624ff50e8afSArron Wang #if IS_ENABLED(CONFIG_BT_BREDR)
62564274518SGustavo F. Padovan int sco_init(void);
62664274518SGustavo F. Padovan void sco_exit(void);
627ff50e8afSArron Wang #else
sco_init(void)628ff50e8afSArron Wang static inline int sco_init(void)
629ff50e8afSArron Wang {
630ff50e8afSArron Wang return 0;
631ff50e8afSArron Wang }
632ff50e8afSArron Wang
sco_exit(void)633ff50e8afSArron Wang static inline void sco_exit(void)
634ff50e8afSArron Wang {
635ff50e8afSArron Wang }
636ff50e8afSArron Wang #endif
63764274518SGustavo F. Padovan
638ccf74f23SLuiz Augusto von Dentz #if IS_ENABLED(CONFIG_BT_LE)
639ccf74f23SLuiz Augusto von Dentz int iso_init(void);
640ccf74f23SLuiz Augusto von Dentz int iso_exit(void);
641ccf74f23SLuiz Augusto von Dentz bool iso_enabled(void);
642ccf74f23SLuiz Augusto von Dentz #else
iso_init(void)643ccf74f23SLuiz Augusto von Dentz static inline int iso_init(void)
644ccf74f23SLuiz Augusto von Dentz {
645ccf74f23SLuiz Augusto von Dentz return 0;
646ccf74f23SLuiz Augusto von Dentz }
647ccf74f23SLuiz Augusto von Dentz
iso_exit(void)648ccf74f23SLuiz Augusto von Dentz static inline int iso_exit(void)
649ccf74f23SLuiz Augusto von Dentz {
650ccf74f23SLuiz Augusto von Dentz return 0;
651ccf74f23SLuiz Augusto von Dentz }
652ccf74f23SLuiz Augusto von Dentz
iso_enabled(void)653ccf74f23SLuiz Augusto von Dentz static inline bool iso_enabled(void)
654ccf74f23SLuiz Augusto von Dentz {
655ccf74f23SLuiz Augusto von Dentz return false;
656ccf74f23SLuiz Augusto von Dentz }
657ccf74f23SLuiz Augusto von Dentz #endif
658ccf74f23SLuiz Augusto von Dentz
6596d785aa3SJohan Hedberg int mgmt_init(void);
6606d785aa3SJohan Hedberg void mgmt_exit(void);
661b338d917SBrian Gix void mgmt_cleanup(struct sock *sk);
6626d785aa3SJohan Hedberg
663d22015aaSOctavian Purdila void bt_sock_reclassify_lock(struct sock *sk, int proto);
664d22015aaSOctavian Purdila
6651da177e4SLinus Torvalds #endif /* __BLUETOOTH_H */
666