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 126ad10b1a4SFrédéric Dalleau 1271f435424SJohan Hedberg #define BT_SNDMTU 12 1281f435424SJohan Hedberg #define BT_RCVMTU 13 129eab2404bSLuiz Augusto von Dentz #define BT_PHY 14 130eab2404bSLuiz Augusto von Dentz 131eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_1SLOT 0x00000001 132eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_3SLOT 0x00000002 133eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_5SLOT 0x00000004 134eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_1SLOT 0x00000008 135eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_3SLOT 0x00000010 136eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_5SLOT 0x00000020 137eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_1SLOT 0x00000040 138eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_3SLOT 0x00000080 139eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_5SLOT 0x00000100 140eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_TX 0x00000200 141eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_RX 0x00000400 142eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_TX 0x00000800 143eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_RX 0x00001000 144eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_TX 0x00002000 145eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_RX 0x00004000 1461f435424SJohan Hedberg 1473ee7b7cdSLuiz Augusto von Dentz #define BT_MODE 15 1483ee7b7cdSLuiz Augusto von Dentz 1493ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_BASIC 0x00 1503ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_ERTM 0x01 1513ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_STREAMING 0x02 1523ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_LE_FLOWCTL 0x03 1533ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_EXT_FLOWCTL 0x04 1543ee7b7cdSLuiz Augusto von Dentz 15500398e1dSAlain Michaud #define BT_PKT_STATUS 16 15600398e1dSAlain Michaud 15700398e1dSAlain Michaud #define BT_SCM_PKT_STATUS 0x03 15800398e1dSAlain Michaud 15926afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS 17 16026afbd82SLuiz Augusto von Dentz 16126afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS_CIG_UNSET 0xff 16226afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS_CIS_UNSET 0xff 16326afbd82SLuiz Augusto von Dentz 164eca0ae4aSLuiz Augusto von Dentz #define BT_ISO_QOS_BIG_UNSET 0xff 165eca0ae4aSLuiz Augusto von Dentz #define BT_ISO_QOS_BIS_UNSET 0xff 166eca0ae4aSLuiz Augusto von Dentz 167*c8321645SLuiz Augusto von Dentz #define BT_ISO_SYNC_TIMEOUT 0x07d0 /* 20 secs */ 168*c8321645SLuiz Augusto von Dentz 16926afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos { 17026afbd82SLuiz Augusto von Dentz __u32 interval; 17126afbd82SLuiz Augusto von Dentz __u16 latency; 17226afbd82SLuiz Augusto von Dentz __u16 sdu; 17326afbd82SLuiz Augusto von Dentz __u8 phy; 17426afbd82SLuiz Augusto von Dentz __u8 rtn; 17526afbd82SLuiz Augusto von Dentz }; 17626afbd82SLuiz Augusto von Dentz 1770fe8c8d0SIulia Tanasescu struct bt_iso_ucast_qos { 17826afbd82SLuiz Augusto von Dentz __u8 cig; 17926afbd82SLuiz Augusto von Dentz __u8 cis; 18026afbd82SLuiz Augusto von Dentz __u8 sca; 18126afbd82SLuiz Augusto von Dentz __u8 packing; 18226afbd82SLuiz Augusto von Dentz __u8 framing; 18326afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos in; 18426afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos out; 18526afbd82SLuiz Augusto von Dentz }; 18626afbd82SLuiz Augusto von Dentz 1870fe8c8d0SIulia Tanasescu struct bt_iso_bcast_qos { 1880fe8c8d0SIulia Tanasescu __u8 big; 1890fe8c8d0SIulia Tanasescu __u8 bis; 19014f0dcecSLuiz Augusto von Dentz __u8 sync_factor; 1910fe8c8d0SIulia Tanasescu __u8 packing; 1920fe8c8d0SIulia Tanasescu __u8 framing; 1930fe8c8d0SIulia Tanasescu struct bt_iso_io_qos in; 1940fe8c8d0SIulia Tanasescu struct bt_iso_io_qos out; 1950fe8c8d0SIulia Tanasescu __u8 encryption; 1960fe8c8d0SIulia Tanasescu __u8 bcode[16]; 1970fe8c8d0SIulia Tanasescu __u8 options; 1980fe8c8d0SIulia Tanasescu __u16 skip; 1990fe8c8d0SIulia Tanasescu __u16 sync_timeout; 2000fe8c8d0SIulia Tanasescu __u8 sync_cte_type; 2010fe8c8d0SIulia Tanasescu __u8 mse; 2020fe8c8d0SIulia Tanasescu __u16 timeout; 2030fe8c8d0SIulia Tanasescu }; 2040fe8c8d0SIulia Tanasescu 2050fe8c8d0SIulia Tanasescu struct bt_iso_qos { 2060fe8c8d0SIulia Tanasescu union { 2070fe8c8d0SIulia Tanasescu struct bt_iso_ucast_qos ucast; 2080fe8c8d0SIulia Tanasescu struct bt_iso_bcast_qos bcast; 2090fe8c8d0SIulia Tanasescu }; 2100fe8c8d0SIulia Tanasescu }; 2110fe8c8d0SIulia Tanasescu 21226afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_1M 0x01 21326afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_2M 0x02 21426afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_CODED 0x04 21526afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_ANY (BT_ISO_PHY_1M | BT_ISO_PHY_2M | \ 21626afbd82SLuiz Augusto von Dentz BT_ISO_PHY_CODED) 21726afbd82SLuiz Augusto von Dentz 218248733e8SKiran K #define BT_CODEC 19 219248733e8SKiran K 220248733e8SKiran K struct bt_codec_caps { 221248733e8SKiran K __u8 len; 222248733e8SKiran K __u8 data[]; 223248733e8SKiran K } __packed; 224248733e8SKiran K 225248733e8SKiran K struct bt_codec { 226248733e8SKiran K __u8 id; 227248733e8SKiran K __u16 cid; 228248733e8SKiran K __u16 vid; 229248733e8SKiran K __u8 data_path; 230248733e8SKiran K __u8 num_caps; 231248733e8SKiran K } __packed; 232248733e8SKiran K 233248733e8SKiran K struct bt_codecs { 234248733e8SKiran K __u8 num_codecs; 235248733e8SKiran K struct bt_codec codecs[]; 236248733e8SKiran K } __packed; 237248733e8SKiran K 238f6873401SKiran K #define BT_CODEC_CVSD 0x02 239b2af264aSKiran K #define BT_CODEC_TRANSPARENT 0x03 240904c139aSKiran K #define BT_CODEC_MSBC 0x05 241f6873401SKiran K 242f764a6c2SLuiz Augusto von Dentz #define BT_ISO_BASE 20 243f764a6c2SLuiz Augusto von Dentz 2443ed7003eSJoe Perches __printf(1, 2) 2452b0bf6c8SJoe Perches void bt_info(const char *fmt, ...); 2463ed7003eSJoe Perches __printf(1, 2) 247594b31eaSFrederic Danis void bt_warn(const char *fmt, ...); 248594b31eaSFrederic Danis __printf(1, 2) 2492b0bf6c8SJoe Perches void bt_err(const char *fmt, ...); 250e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG) 251e625e50cSMarcel Holtmann void bt_dbg_set(bool enable); 252e625e50cSMarcel Holtmann bool bt_dbg_get(void); 253e625e50cSMarcel Holtmann __printf(1, 2) 254e625e50cSMarcel Holtmann void bt_dbg(const char *fmt, ...); 255e625e50cSMarcel Holtmann #endif 256e781b7f7SSzymon Janc __printf(1, 2) 25736278a5dSAlain Michaud void bt_warn_ratelimited(const char *fmt, ...); 25836278a5dSAlain Michaud __printf(1, 2) 259e781b7f7SSzymon Janc void bt_err_ratelimited(const char *fmt, ...); 260e1447d8dSJoe Perches 2613ed7003eSJoe Perches #define BT_INFO(fmt, ...) bt_info(fmt "\n", ##__VA_ARGS__) 262594b31eaSFrederic Danis #define BT_WARN(fmt, ...) bt_warn(fmt "\n", ##__VA_ARGS__) 2633ed7003eSJoe Perches #define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__) 264e625e50cSMarcel Holtmann 265e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG) 266e625e50cSMarcel Holtmann #define BT_DBG(fmt, ...) bt_dbg(fmt "\n", ##__VA_ARGS__) 267e625e50cSMarcel Holtmann #else 2683ed7003eSJoe Perches #define BT_DBG(fmt, ...) pr_debug(fmt "\n", ##__VA_ARGS__) 269e625e50cSMarcel Holtmann #endif 2701da177e4SLinus Torvalds 2719b392e0eSLuiz Augusto von Dentz #define bt_dev_name(hdev) ((hdev) ? (hdev)->name : "null") 2729b392e0eSLuiz Augusto von Dentz 2736f558b70SLoic Poulain #define bt_dev_info(hdev, fmt, ...) \ 2749b392e0eSLuiz Augusto von Dentz BT_INFO("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 275594b31eaSFrederic Danis #define bt_dev_warn(hdev, fmt, ...) \ 2769b392e0eSLuiz Augusto von Dentz BT_WARN("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2776f558b70SLoic Poulain #define bt_dev_err(hdev, fmt, ...) \ 2789b392e0eSLuiz Augusto von Dentz BT_ERR("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2796f558b70SLoic Poulain #define bt_dev_dbg(hdev, fmt, ...) \ 2809b392e0eSLuiz Augusto von Dentz BT_DBG("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2816f558b70SLoic Poulain 28236278a5dSAlain Michaud #define bt_dev_warn_ratelimited(hdev, fmt, ...) \ 2839b392e0eSLuiz Augusto von Dentz bt_warn_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2842064ee33SMarcel Holtmann #define bt_dev_err_ratelimited(hdev, fmt, ...) \ 2859b392e0eSLuiz Augusto von Dentz bt_err_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2862064ee33SMarcel Holtmann 2871da177e4SLinus Torvalds /* Connection and socket states */ 2881da177e4SLinus Torvalds enum { 2891da177e4SLinus Torvalds BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ 2901da177e4SLinus Torvalds BT_OPEN, 2911da177e4SLinus Torvalds BT_BOUND, 2921da177e4SLinus Torvalds BT_LISTEN, 2931da177e4SLinus Torvalds BT_CONNECT, 2941da177e4SLinus Torvalds BT_CONNECT2, 2951da177e4SLinus Torvalds BT_CONFIG, 2961da177e4SLinus Torvalds BT_DISCONN, 2971da177e4SLinus Torvalds BT_CLOSED 2981da177e4SLinus Torvalds }; 2991da177e4SLinus Torvalds 30020d1803aSAndrei Emeltchenko /* If unused will be removed by compiler */ 30120d1803aSAndrei Emeltchenko static inline const char *state_to_string(int state) 30220d1803aSAndrei Emeltchenko { 30320d1803aSAndrei Emeltchenko switch (state) { 30420d1803aSAndrei Emeltchenko case BT_CONNECTED: 30520d1803aSAndrei Emeltchenko return "BT_CONNECTED"; 30620d1803aSAndrei Emeltchenko case BT_OPEN: 30720d1803aSAndrei Emeltchenko return "BT_OPEN"; 30820d1803aSAndrei Emeltchenko case BT_BOUND: 30920d1803aSAndrei Emeltchenko return "BT_BOUND"; 31020d1803aSAndrei Emeltchenko case BT_LISTEN: 31120d1803aSAndrei Emeltchenko return "BT_LISTEN"; 31220d1803aSAndrei Emeltchenko case BT_CONNECT: 31320d1803aSAndrei Emeltchenko return "BT_CONNECT"; 31420d1803aSAndrei Emeltchenko case BT_CONNECT2: 31520d1803aSAndrei Emeltchenko return "BT_CONNECT2"; 31620d1803aSAndrei Emeltchenko case BT_CONFIG: 31720d1803aSAndrei Emeltchenko return "BT_CONFIG"; 31820d1803aSAndrei Emeltchenko case BT_DISCONN: 31920d1803aSAndrei Emeltchenko return "BT_DISCONN"; 32020d1803aSAndrei Emeltchenko case BT_CLOSED: 32120d1803aSAndrei Emeltchenko return "BT_CLOSED"; 32220d1803aSAndrei Emeltchenko } 32320d1803aSAndrei Emeltchenko 32420d1803aSAndrei Emeltchenko return "invalid state"; 32520d1803aSAndrei Emeltchenko } 32620d1803aSAndrei Emeltchenko 3271da177e4SLinus Torvalds /* BD Address */ 3281da177e4SLinus Torvalds typedef struct { 3291da177e4SLinus Torvalds __u8 b[6]; 33066c853ccSGustavo F. Padovan } __packed bdaddr_t; 3311da177e4SLinus Torvalds 332591f47f3SAndre Guedes /* BD Address type */ 333591f47f3SAndre Guedes #define BDADDR_BREDR 0x00 334591f47f3SAndre Guedes #define BDADDR_LE_PUBLIC 0x01 335591f47f3SAndre Guedes #define BDADDR_LE_RANDOM 0x02 336591f47f3SAndre Guedes 33721fcf572SPavel Machek static inline bool bdaddr_type_is_valid(u8 type) 338679efe2bSJohan Hedberg { 339679efe2bSJohan Hedberg switch (type) { 340679efe2bSJohan Hedberg case BDADDR_BREDR: 341679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC: 342679efe2bSJohan Hedberg case BDADDR_LE_RANDOM: 343679efe2bSJohan Hedberg return true; 344679efe2bSJohan Hedberg } 345679efe2bSJohan Hedberg 346679efe2bSJohan Hedberg return false; 347679efe2bSJohan Hedberg } 348679efe2bSJohan Hedberg 34921fcf572SPavel Machek static inline bool bdaddr_type_is_le(u8 type) 350679efe2bSJohan Hedberg { 351679efe2bSJohan Hedberg switch (type) { 352679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC: 353679efe2bSJohan Hedberg case BDADDR_LE_RANDOM: 354679efe2bSJohan Hedberg return true; 355679efe2bSJohan Hedberg } 356679efe2bSJohan Hedberg 357679efe2bSJohan Hedberg return false; 358679efe2bSJohan Hedberg } 359679efe2bSJohan Hedberg 3601da177e4SLinus Torvalds #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}}) 361a59ac2f7SMarcel Holtmann #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}) 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds /* Copy, swap, convert BD Address */ 364f53c20e9SDavid Herrmann static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2) 3651da177e4SLinus Torvalds { 3661da177e4SLinus Torvalds return memcmp(ba1, ba2, sizeof(bdaddr_t)); 3671da177e4SLinus Torvalds } 368f53c20e9SDavid Herrmann static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds memcpy(dst, src, sizeof(bdaddr_t)); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 37365bce462SLoic Poulain void baswap(bdaddr_t *dst, const bdaddr_t *src); 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds /* Common socket structures and functions */ 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds #define bt_sk(__sk) ((struct bt_sock *) __sk) 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds struct bt_sock { 3801da177e4SLinus Torvalds struct sock sk; 3811da177e4SLinus Torvalds struct list_head accept_q; 3821da177e4SLinus Torvalds struct sock *parent; 383c5daa683SGustavo Padovan unsigned long flags; 384d9763698SMarcel Holtmann void (*skb_msg_name)(struct sk_buff *, void *, int *); 38500398e1dSAlain Michaud void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *); 386c5daa683SGustavo Padovan }; 387c5daa683SGustavo Padovan 388c5daa683SGustavo Padovan enum { 389c5daa683SGustavo Padovan BT_SK_DEFER_SETUP, 390c5daa683SGustavo Padovan BT_SK_SUSPEND, 3913f19ffb2SLuiz Augusto von Dentz BT_SK_PKT_STATUS 3921da177e4SLinus Torvalds }; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds struct bt_sock_list { 3951da177e4SLinus Torvalds struct hlist_head head; 3961da177e4SLinus Torvalds rwlock_t lock; 397256a06c8SMasatake YAMATO #ifdef CONFIG_PROC_FS 398256a06c8SMasatake YAMATO int (* custom_seq_show)(struct seq_file *, void *); 399256a06c8SMasatake YAMATO #endif 4001da177e4SLinus Torvalds }; 4011da177e4SLinus Torvalds 402ec1b4cf7SStephen Hemminger int bt_sock_register(int proto, const struct net_proto_family *ops); 403be9f97f0SDavid Herrmann void bt_sock_unregister(int proto); 4041da177e4SLinus Torvalds void bt_sock_link(struct bt_sock_list *l, struct sock *s); 4051da177e4SLinus Torvalds void bt_sock_unlink(struct bt_sock_list *l, struct sock *s); 4066bfa273eSLuiz Augusto von Dentz struct sock *bt_sock_alloc(struct net *net, struct socket *sock, 4076bfa273eSLuiz Augusto von Dentz struct proto *prot, int proto, gfp_t prio, int kern); 4081b784140SYing Xue int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 4091b784140SYing Xue int flags); 4101b784140SYing Xue int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg, 4111b784140SYing Xue size_t len, int flags); 412a11e1d43SLinus Torvalds __poll_t bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait); 4133241ad82SMarcel Holtmann int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); 4141da177e4SLinus Torvalds int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo); 415da891217SGavin Li int bt_sock_wait_ready(struct sock *sk, unsigned int msg_flags); 4161da177e4SLinus Torvalds 417c4f5627fSMatthias Kaehlcke void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh); 4181da177e4SLinus Torvalds void bt_accept_unlink(struct sock *sk); 4191da177e4SLinus Torvalds struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock); 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* Skb helpers */ 42200e3112cSMat Martineau struct l2cap_ctrl { 42321fcf572SPavel Machek u8 sframe:1, 42400e3112cSMat Martineau poll:1, 42500e3112cSMat Martineau final:1, 42600e3112cSMat Martineau fcs:1, 42700e3112cSMat Martineau sar:2, 42800e3112cSMat Martineau super:2; 42921fcf572SPavel Machek 43021fcf572SPavel Machek u16 reqseq; 43121fcf572SPavel Machek u16 txseq; 43221fcf572SPavel Machek u8 retries; 433a4368ff3SJohan Hedberg __le16 psm; 434a4368ff3SJohan Hedberg bdaddr_t bdaddr; 435a4368ff3SJohan Hedberg struct l2cap_chan *chan; 43600e3112cSMat Martineau }; 43700e3112cSMat Martineau 4383119ae95SJohan Hedberg struct hci_dev; 4393119ae95SJohan Hedberg 4401904a853SMarcel Holtmann typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode); 441e6214487SJohan Hedberg typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status, 442e6214487SJohan Hedberg u16 opcode, struct sk_buff *skb); 4433119ae95SJohan Hedberg 44444d27137SJohan Hedberg #define HCI_REQ_START BIT(0) 44544d27137SJohan Hedberg #define HCI_REQ_SKB BIT(1) 44644d27137SJohan Hedberg 447242c0ebdSMarcel Holtmann struct hci_ctrl { 448cba6b758SLuiz Augusto von Dentz struct sock *sk; 44921fcf572SPavel Machek u16 opcode; 45044d27137SJohan Hedberg u8 req_flags; 451242c0ebdSMarcel Holtmann u8 req_event; 45244d27137SJohan Hedberg union { 453242c0ebdSMarcel Holtmann hci_req_complete_t req_complete; 454242c0ebdSMarcel Holtmann hci_req_complete_skb_t req_complete_skb; 455db6e3e8dSJohan Hedberg }; 45644d27137SJohan Hedberg }; 457db6e3e8dSJohan Hedberg 4588aca46f9SLuiz Augusto von Dentz struct mgmt_ctrl { 4598aca46f9SLuiz Augusto von Dentz struct hci_dev *hdev; 4608aca46f9SLuiz Augusto von Dentz u16 opcode; 4618aca46f9SLuiz Augusto von Dentz }; 4628aca46f9SLuiz Augusto von Dentz 4631da177e4SLinus Torvalds struct bt_skb_cb { 46421fcf572SPavel Machek u8 pkt_type; 46521fcf572SPavel Machek u8 force_active; 46621fcf572SPavel Machek u16 expect; 46721fcf572SPavel Machek u8 incoming:1; 4683f19ffb2SLuiz Augusto von Dentz u8 pkt_status:2; 469db6e3e8dSJohan Hedberg union { 470a4368ff3SJohan Hedberg struct l2cap_ctrl l2cap; 471242c0ebdSMarcel Holtmann struct hci_ctrl hci; 4728aca46f9SLuiz Augusto von Dentz struct mgmt_ctrl mgmt; 47369ae5065SLuiz Augusto von Dentz struct scm_creds creds; 474db6e3e8dSJohan Hedberg }; 4751da177e4SLinus Torvalds }; 4761c2acffbSGustavo F. Padovan #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb)) 4771da177e4SLinus Torvalds 478f5c4a42aSMarcel Holtmann #define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type 4793f19ffb2SLuiz Augusto von Dentz #define hci_skb_pkt_status(skb) bt_cb((skb))->pkt_status 480f5c4a42aSMarcel Holtmann #define hci_skb_expect(skb) bt_cb((skb))->expect 481f5c4a42aSMarcel Holtmann #define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode 48285b56857SLuiz Augusto von Dentz #define hci_skb_event(skb) bt_cb((skb))->hci.req_event 483cba6b758SLuiz Augusto von Dentz #define hci_skb_sk(skb) bt_cb((skb))->hci.sk 484f5c4a42aSMarcel Holtmann 485dd0fc66fSAl Viro static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds struct sk_buff *skb; 4881da177e4SLinus Torvalds 489c3c7ea65SGustavo Padovan skb = alloc_skb(len + BT_SKB_RESERVE, how); 4905fcc86bdSJohan Hedberg if (skb) 4911da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE); 4921da177e4SLinus Torvalds return skb; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4955a9d0a3fSWaldemar Rymarkiewicz static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk, 4965a9d0a3fSWaldemar Rymarkiewicz unsigned long len, int nb, int *err) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds struct sk_buff *skb; 4991da177e4SLinus Torvalds 500c3c7ea65SGustavo Padovan skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err); 5015fcc86bdSJohan Hedberg if (skb) 5021da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE); 503e454c844SGustavo F. Padovan 504e454c844SGustavo F. Padovan if (!skb && *err) 505e454c844SGustavo F. Padovan return NULL; 506e454c844SGustavo F. Padovan 507e454c844SGustavo F. Padovan *err = sock_error(sk); 508e454c844SGustavo F. Padovan if (*err) 509e454c844SGustavo F. Padovan goto out; 510e454c844SGustavo F. Padovan 511e454c844SGustavo F. Padovan if (sk->sk_shutdown) { 512e454c844SGustavo F. Padovan *err = -ECONNRESET; 513e454c844SGustavo F. Padovan goto out; 514e454c844SGustavo F. Padovan } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds return skb; 517e454c844SGustavo F. Padovan 518e454c844SGustavo F. Padovan out: 519e454c844SGustavo F. Padovan kfree_skb(skb); 520e454c844SGustavo F. Padovan return NULL; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 52338f64f65SLuiz Augusto von Dentz /* Shall not be called with lock_sock held */ 52438f64f65SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk, 52538f64f65SLuiz Augusto von Dentz struct msghdr *msg, 52638f64f65SLuiz Augusto von Dentz size_t len, size_t mtu, 52738f64f65SLuiz Augusto von Dentz size_t headroom, size_t tailroom) 52838f64f65SLuiz Augusto von Dentz { 52938f64f65SLuiz Augusto von Dentz struct sk_buff *skb; 53038f64f65SLuiz Augusto von Dentz size_t size = min_t(size_t, len, mtu); 53138f64f65SLuiz Augusto von Dentz int err; 53238f64f65SLuiz Augusto von Dentz 53338f64f65SLuiz Augusto von Dentz skb = bt_skb_send_alloc(sk, size + headroom + tailroom, 53438f64f65SLuiz Augusto von Dentz msg->msg_flags & MSG_DONTWAIT, &err); 53538f64f65SLuiz Augusto von Dentz if (!skb) 53638f64f65SLuiz Augusto von Dentz return ERR_PTR(err); 53738f64f65SLuiz Augusto von Dentz 53838f64f65SLuiz Augusto von Dentz skb_reserve(skb, headroom); 53938f64f65SLuiz Augusto von Dentz skb_tailroom_reserve(skb, mtu, tailroom); 54038f64f65SLuiz Augusto von Dentz 54138f64f65SLuiz Augusto von Dentz if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) { 54238f64f65SLuiz Augusto von Dentz kfree_skb(skb); 54338f64f65SLuiz Augusto von Dentz return ERR_PTR(-EFAULT); 54438f64f65SLuiz Augusto von Dentz } 54538f64f65SLuiz Augusto von Dentz 54638f64f65SLuiz Augusto von Dentz skb->priority = sk->sk_priority; 54738f64f65SLuiz Augusto von Dentz 54838f64f65SLuiz Augusto von Dentz return skb; 54938f64f65SLuiz Augusto von Dentz } 55038f64f65SLuiz Augusto von Dentz 55197e4e802SLuiz Augusto von Dentz /* Similar to bt_skb_sendmsg but can split the msg into multiple fragments 55297e4e802SLuiz Augusto von Dentz * accourding to the MTU. 55397e4e802SLuiz Augusto von Dentz */ 55497e4e802SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmmsg(struct sock *sk, 55597e4e802SLuiz Augusto von Dentz struct msghdr *msg, 55697e4e802SLuiz Augusto von Dentz size_t len, size_t mtu, 55797e4e802SLuiz Augusto von Dentz size_t headroom, size_t tailroom) 55897e4e802SLuiz Augusto von Dentz { 55997e4e802SLuiz Augusto von Dentz struct sk_buff *skb, **frag; 56097e4e802SLuiz Augusto von Dentz 56197e4e802SLuiz Augusto von Dentz skb = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom); 5626f43f616SDan Carpenter if (IS_ERR(skb)) 56397e4e802SLuiz Augusto von Dentz return skb; 56497e4e802SLuiz Augusto von Dentz 56597e4e802SLuiz Augusto von Dentz len -= skb->len; 56697e4e802SLuiz Augusto von Dentz if (!len) 56797e4e802SLuiz Augusto von Dentz return skb; 56897e4e802SLuiz Augusto von Dentz 56997e4e802SLuiz Augusto von Dentz /* Add remaining data over MTU as continuation fragments */ 57097e4e802SLuiz Augusto von Dentz frag = &skb_shinfo(skb)->frag_list; 57197e4e802SLuiz Augusto von Dentz while (len) { 57297e4e802SLuiz Augusto von Dentz struct sk_buff *tmp; 57397e4e802SLuiz Augusto von Dentz 57497e4e802SLuiz Augusto von Dentz tmp = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom); 575266191aaSLuiz Augusto von Dentz if (IS_ERR(tmp)) { 57629fb6083SLuiz Augusto von Dentz return skb; 57797e4e802SLuiz Augusto von Dentz } 57897e4e802SLuiz Augusto von Dentz 57997e4e802SLuiz Augusto von Dentz len -= tmp->len; 58097e4e802SLuiz Augusto von Dentz 58197e4e802SLuiz Augusto von Dentz *frag = tmp; 58297e4e802SLuiz Augusto von Dentz frag = &(*frag)->next; 58397e4e802SLuiz Augusto von Dentz } 58497e4e802SLuiz Augusto von Dentz 58597e4e802SLuiz Augusto von Dentz return skb; 58697e4e802SLuiz Augusto von Dentz } 58797e4e802SLuiz Augusto von Dentz 58821fcf572SPavel Machek int bt_to_errno(u16 code); 589ca2045e0SLuiz Augusto von Dentz __u8 bt_status(int err); 5901da177e4SLinus Torvalds 5916befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr); 5926befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr); 593c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr); 594d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk); 59570ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk); 5966befc644SMarcel Holtmann 597e74e58f8SJoe Perches int hci_sock_init(void); 598e74e58f8SJoe Perches void hci_sock_cleanup(void); 5996516455dSMarcel Holtmann 600e74e58f8SJoe Perches int bt_sysfs_init(void); 601e74e58f8SJoe Perches void bt_sysfs_cleanup(void); 6026516455dSMarcel Holtmann 603e74e58f8SJoe Perches int bt_procfs_init(struct net *net, const char *name, 604256a06c8SMasatake YAMATO struct bt_sock_list *sk_list, 605256a06c8SMasatake YAMATO int (*seq_show)(struct seq_file *, void *)); 606e74e58f8SJoe Perches void bt_procfs_cleanup(struct net *net, const char *name); 607256a06c8SMasatake YAMATO 608aef7d97cSMarcel Holtmann extern struct dentry *bt_debugfs; 609be9d1227SMarcel Holtmann 61064274518SGustavo F. Padovan int l2cap_init(void); 61164274518SGustavo F. Padovan void l2cap_exit(void); 61264274518SGustavo F. Padovan 613ff50e8afSArron Wang #if IS_ENABLED(CONFIG_BT_BREDR) 61464274518SGustavo F. Padovan int sco_init(void); 61564274518SGustavo F. Padovan void sco_exit(void); 616ff50e8afSArron Wang #else 617ff50e8afSArron Wang static inline int sco_init(void) 618ff50e8afSArron Wang { 619ff50e8afSArron Wang return 0; 620ff50e8afSArron Wang } 621ff50e8afSArron Wang 622ff50e8afSArron Wang static inline void sco_exit(void) 623ff50e8afSArron Wang { 624ff50e8afSArron Wang } 625ff50e8afSArron Wang #endif 62664274518SGustavo F. Padovan 627ccf74f23SLuiz Augusto von Dentz #if IS_ENABLED(CONFIG_BT_LE) 628ccf74f23SLuiz Augusto von Dentz int iso_init(void); 629ccf74f23SLuiz Augusto von Dentz int iso_exit(void); 630ccf74f23SLuiz Augusto von Dentz bool iso_enabled(void); 631ccf74f23SLuiz Augusto von Dentz #else 632ccf74f23SLuiz Augusto von Dentz static inline int iso_init(void) 633ccf74f23SLuiz Augusto von Dentz { 634ccf74f23SLuiz Augusto von Dentz return 0; 635ccf74f23SLuiz Augusto von Dentz } 636ccf74f23SLuiz Augusto von Dentz 637ccf74f23SLuiz Augusto von Dentz static inline int iso_exit(void) 638ccf74f23SLuiz Augusto von Dentz { 639ccf74f23SLuiz Augusto von Dentz return 0; 640ccf74f23SLuiz Augusto von Dentz } 641ccf74f23SLuiz Augusto von Dentz 642ccf74f23SLuiz Augusto von Dentz static inline bool iso_enabled(void) 643ccf74f23SLuiz Augusto von Dentz { 644ccf74f23SLuiz Augusto von Dentz return false; 645ccf74f23SLuiz Augusto von Dentz } 646ccf74f23SLuiz Augusto von Dentz #endif 647ccf74f23SLuiz Augusto von Dentz 6486d785aa3SJohan Hedberg int mgmt_init(void); 6496d785aa3SJohan Hedberg void mgmt_exit(void); 650b338d917SBrian Gix void mgmt_cleanup(struct sock *sk); 6516d785aa3SJohan Hedberg 652d22015aaSOctavian Purdila void bt_sock_reclassify_lock(struct sock *sk, int proto); 653d22015aaSOctavian Purdila 6541da177e4SLinus Torvalds #endif /* __BLUETOOTH_H */ 655