11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds BlueZ - Bluetooth protocol stack for Linux 31da177e4SLinus Torvalds Copyright (C) 2000-2001 Qualcomm Incorporated 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as 91da177e4SLinus Torvalds published by the Free Software Foundation; 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 121da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 131da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 141da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 151da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 161da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 171da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 181da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 211da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 221da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED. 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #ifndef __BLUETOOTH_H 261da177e4SLinus Torvalds #define __BLUETOOTH_H 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include <linux/poll.h> 291da177e4SLinus Torvalds #include <net/sock.h> 30256a06c8SMasatake YAMATO #include <linux/seq_file.h> 311da177e4SLinus Torvalds 329e8305b3SMarcel Holtmann #define BT_SUBSYS_VERSION 2 334037a774SMarcel Holtmann #define BT_SUBSYS_REVISION 22 34dd31506dSMarcel Holtmann 351da177e4SLinus Torvalds #ifndef AF_BLUETOOTH 361da177e4SLinus Torvalds #define AF_BLUETOOTH 31 371da177e4SLinus Torvalds #define PF_BLUETOOTH AF_BLUETOOTH 381da177e4SLinus Torvalds #endif 391da177e4SLinus Torvalds 40d095c1ebSAndrei Emeltchenko /* Bluetooth versions */ 41d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_1_1 1 42d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_1_2 2 43d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_2_0 3 44cde1a8a9SIsmael Ferreras Morezuelas #define BLUETOOTH_VER_2_1 4 45cde1a8a9SIsmael Ferreras Morezuelas #define BLUETOOTH_VER_4_0 6 46d095c1ebSAndrei Emeltchenko 471da177e4SLinus Torvalds /* Reserv for core and drivers use */ 481da177e4SLinus Torvalds #define BT_SKB_RESERVE 8 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #define BTPROTO_L2CAP 0 511da177e4SLinus Torvalds #define BTPROTO_HCI 1 521da177e4SLinus Torvalds #define BTPROTO_SCO 2 531da177e4SLinus Torvalds #define BTPROTO_RFCOMM 3 541da177e4SLinus Torvalds #define BTPROTO_BNEP 4 551da177e4SLinus Torvalds #define BTPROTO_CMTP 5 561da177e4SLinus Torvalds #define BTPROTO_HIDP 6 571da177e4SLinus Torvalds #define BTPROTO_AVDTP 7 5826afbd82SLuiz Augusto von Dentz #define BTPROTO_ISO 8 5926afbd82SLuiz Augusto von Dentz #define BTPROTO_LAST BTPROTO_ISO 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #define SOL_HCI 0 621da177e4SLinus Torvalds #define SOL_L2CAP 6 631da177e4SLinus Torvalds #define SOL_SCO 17 641da177e4SLinus Torvalds #define SOL_RFCOMM 18 651da177e4SLinus Torvalds 668c1b2355SMarcel Holtmann #define BT_SECURITY 4 678c1b2355SMarcel Holtmann struct bt_security { 688c1b2355SMarcel Holtmann __u8 level; 698f360119SVinicius Costa Gomes __u8 key_size; 708c1b2355SMarcel Holtmann }; 718c1b2355SMarcel Holtmann #define BT_SECURITY_SDP 0 728c1b2355SMarcel Holtmann #define BT_SECURITY_LOW 1 738c1b2355SMarcel Holtmann #define BT_SECURITY_MEDIUM 2 748c1b2355SMarcel Holtmann #define BT_SECURITY_HIGH 3 757b5a9241SMarcel Holtmann #define BT_SECURITY_FIPS 4 768c1b2355SMarcel Holtmann 77c4f912e1SMarcel Holtmann #define BT_DEFER_SETUP 7 78c4f912e1SMarcel Holtmann 79e702112fSAndrei Emeltchenko #define BT_FLUSHABLE 8 80e702112fSAndrei Emeltchenko 81e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_OFF 0 82e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_ON 1 83e702112fSAndrei Emeltchenko 8414b12d0bSJaikumar Ganesh #define BT_POWER 9 8514b12d0bSJaikumar Ganesh struct bt_power { 8614b12d0bSJaikumar Ganesh __u8 force_active; 8714b12d0bSJaikumar Ganesh }; 8814b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_OFF 0 8914b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_ON 1 9014b12d0bSJaikumar Ganesh 91c14968b0SMat Martineau #define BT_CHANNEL_POLICY 10 92c14968b0SMat Martineau 93c14968b0SMat Martineau /* BR/EDR only (default policy) 94c14968b0SMat Martineau * AMP controllers cannot be used. 95c14968b0SMat Martineau * Channel move requests from the remote device are denied. 96c14968b0SMat Martineau * If the L2CAP channel is currently using AMP, move the channel to BR/EDR. 97c14968b0SMat Martineau */ 98c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_ONLY 0 99c14968b0SMat Martineau 100c14968b0SMat Martineau /* BR/EDR Preferred 101c14968b0SMat Martineau * Allow use of AMP controllers. 102c14968b0SMat Martineau * If the L2CAP channel is currently on AMP, move it to BR/EDR. 103c14968b0SMat Martineau * Channel move requests from the remote device are allowed. 104c14968b0SMat Martineau */ 105c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_PREFERRED 1 106c14968b0SMat Martineau 107c14968b0SMat Martineau /* AMP Preferred 108c14968b0SMat Martineau * Allow use of AMP controllers 109c14968b0SMat Martineau * If the L2CAP channel is currently on BR/EDR and AMP controller 110c14968b0SMat Martineau * resources are available, initiate a channel move to AMP. 111c14968b0SMat Martineau * Channel move requests from the remote device are allowed. 112c14968b0SMat Martineau * If the L2CAP socket has not been connected yet, try to create 113c14968b0SMat Martineau * and configure the channel directly on an AMP controller rather 114c14968b0SMat Martineau * than BR/EDR. 115c14968b0SMat Martineau */ 116c14968b0SMat Martineau #define BT_CHANNEL_POLICY_AMP_PREFERRED 2 117c14968b0SMat Martineau 118ad10b1a4SFrédéric Dalleau #define BT_VOICE 11 119ad10b1a4SFrédéric Dalleau struct bt_voice { 120ad10b1a4SFrédéric Dalleau __u16 setting; 121ad10b1a4SFrédéric Dalleau }; 122ad10b1a4SFrédéric Dalleau 123ad10b1a4SFrédéric Dalleau #define BT_VOICE_TRANSPARENT 0x0003 124ad10b1a4SFrédéric Dalleau #define BT_VOICE_CVSD_16BIT 0x0060 125ad10b1a4SFrédéric Dalleau 1261f435424SJohan Hedberg #define BT_SNDMTU 12 1271f435424SJohan Hedberg #define BT_RCVMTU 13 128eab2404bSLuiz Augusto von Dentz #define BT_PHY 14 129eab2404bSLuiz Augusto von Dentz 130eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_1SLOT 0x00000001 131eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_3SLOT 0x00000002 132eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_5SLOT 0x00000004 133eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_1SLOT 0x00000008 134eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_3SLOT 0x00000010 135eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_5SLOT 0x00000020 136eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_1SLOT 0x00000040 137eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_3SLOT 0x00000080 138eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_5SLOT 0x00000100 139eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_TX 0x00000200 140eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_RX 0x00000400 141eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_TX 0x00000800 142eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_RX 0x00001000 143eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_TX 0x00002000 144eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_RX 0x00004000 1451f435424SJohan Hedberg 1463ee7b7cdSLuiz Augusto von Dentz #define BT_MODE 15 1473ee7b7cdSLuiz Augusto von Dentz 1483ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_BASIC 0x00 1493ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_ERTM 0x01 1503ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_STREAMING 0x02 1513ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_LE_FLOWCTL 0x03 1523ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_EXT_FLOWCTL 0x04 1533ee7b7cdSLuiz Augusto von Dentz 15400398e1dSAlain Michaud #define BT_PKT_STATUS 16 15500398e1dSAlain Michaud 15600398e1dSAlain Michaud #define BT_SCM_PKT_STATUS 0x03 15700398e1dSAlain Michaud 15826afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS 17 15926afbd82SLuiz Augusto von Dentz 16026afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS_CIG_UNSET 0xff 16126afbd82SLuiz Augusto von Dentz #define BT_ISO_QOS_CIS_UNSET 0xff 16226afbd82SLuiz Augusto von Dentz 163*eca0ae4aSLuiz Augusto von Dentz #define BT_ISO_QOS_BIG_UNSET 0xff 164*eca0ae4aSLuiz Augusto von Dentz #define BT_ISO_QOS_BIS_UNSET 0xff 165*eca0ae4aSLuiz Augusto von Dentz 16626afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos { 16726afbd82SLuiz Augusto von Dentz __u32 interval; 16826afbd82SLuiz Augusto von Dentz __u16 latency; 16926afbd82SLuiz Augusto von Dentz __u16 sdu; 17026afbd82SLuiz Augusto von Dentz __u8 phy; 17126afbd82SLuiz Augusto von Dentz __u8 rtn; 17226afbd82SLuiz Augusto von Dentz }; 17326afbd82SLuiz Augusto von Dentz 17426afbd82SLuiz Augusto von Dentz struct bt_iso_qos { 175*eca0ae4aSLuiz Augusto von Dentz union { 17626afbd82SLuiz Augusto von Dentz __u8 cig; 177*eca0ae4aSLuiz Augusto von Dentz __u8 big; 178*eca0ae4aSLuiz Augusto von Dentz }; 179*eca0ae4aSLuiz Augusto von Dentz union { 18026afbd82SLuiz Augusto von Dentz __u8 cis; 181*eca0ae4aSLuiz Augusto von Dentz __u8 bis; 182*eca0ae4aSLuiz Augusto von Dentz }; 183*eca0ae4aSLuiz Augusto von Dentz union { 18426afbd82SLuiz Augusto von Dentz __u8 sca; 185*eca0ae4aSLuiz Augusto von Dentz __u8 sync_interval; 186*eca0ae4aSLuiz Augusto von Dentz }; 18726afbd82SLuiz Augusto von Dentz __u8 packing; 18826afbd82SLuiz Augusto von Dentz __u8 framing; 18926afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos in; 19026afbd82SLuiz Augusto von Dentz struct bt_iso_io_qos out; 19126afbd82SLuiz Augusto von Dentz }; 19226afbd82SLuiz Augusto von Dentz 19326afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_1M 0x01 19426afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_2M 0x02 19526afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_CODED 0x04 19626afbd82SLuiz Augusto von Dentz #define BT_ISO_PHY_ANY (BT_ISO_PHY_1M | BT_ISO_PHY_2M | \ 19726afbd82SLuiz Augusto von Dentz BT_ISO_PHY_CODED) 19826afbd82SLuiz Augusto von Dentz 199248733e8SKiran K #define BT_CODEC 19 200248733e8SKiran K 201248733e8SKiran K struct bt_codec_caps { 202248733e8SKiran K __u8 len; 203248733e8SKiran K __u8 data[]; 204248733e8SKiran K } __packed; 205248733e8SKiran K 206248733e8SKiran K struct bt_codec { 207248733e8SKiran K __u8 id; 208248733e8SKiran K __u16 cid; 209248733e8SKiran K __u16 vid; 210248733e8SKiran K __u8 data_path; 211248733e8SKiran K __u8 num_caps; 212248733e8SKiran K } __packed; 213248733e8SKiran K 214248733e8SKiran K struct bt_codecs { 215248733e8SKiran K __u8 num_codecs; 216248733e8SKiran K struct bt_codec codecs[]; 217248733e8SKiran K } __packed; 218248733e8SKiran K 219f6873401SKiran K #define BT_CODEC_CVSD 0x02 220b2af264aSKiran K #define BT_CODEC_TRANSPARENT 0x03 221904c139aSKiran K #define BT_CODEC_MSBC 0x05 222f6873401SKiran K 2233ed7003eSJoe Perches __printf(1, 2) 2242b0bf6c8SJoe Perches void bt_info(const char *fmt, ...); 2253ed7003eSJoe Perches __printf(1, 2) 226594b31eaSFrederic Danis void bt_warn(const char *fmt, ...); 227594b31eaSFrederic Danis __printf(1, 2) 2282b0bf6c8SJoe Perches void bt_err(const char *fmt, ...); 229e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG) 230e625e50cSMarcel Holtmann void bt_dbg_set(bool enable); 231e625e50cSMarcel Holtmann bool bt_dbg_get(void); 232e625e50cSMarcel Holtmann __printf(1, 2) 233e625e50cSMarcel Holtmann void bt_dbg(const char *fmt, ...); 234e625e50cSMarcel Holtmann #endif 235e781b7f7SSzymon Janc __printf(1, 2) 23636278a5dSAlain Michaud void bt_warn_ratelimited(const char *fmt, ...); 23736278a5dSAlain Michaud __printf(1, 2) 238e781b7f7SSzymon Janc void bt_err_ratelimited(const char *fmt, ...); 239e1447d8dSJoe Perches 2403ed7003eSJoe Perches #define BT_INFO(fmt, ...) bt_info(fmt "\n", ##__VA_ARGS__) 241594b31eaSFrederic Danis #define BT_WARN(fmt, ...) bt_warn(fmt "\n", ##__VA_ARGS__) 2423ed7003eSJoe Perches #define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__) 243e625e50cSMarcel Holtmann 244e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG) 245e625e50cSMarcel Holtmann #define BT_DBG(fmt, ...) bt_dbg(fmt "\n", ##__VA_ARGS__) 246e625e50cSMarcel Holtmann #else 2473ed7003eSJoe Perches #define BT_DBG(fmt, ...) pr_debug(fmt "\n", ##__VA_ARGS__) 248e625e50cSMarcel Holtmann #endif 2491da177e4SLinus Torvalds 2509b392e0eSLuiz Augusto von Dentz #define bt_dev_name(hdev) ((hdev) ? (hdev)->name : "null") 2519b392e0eSLuiz Augusto von Dentz 2526f558b70SLoic Poulain #define bt_dev_info(hdev, fmt, ...) \ 2539b392e0eSLuiz Augusto von Dentz BT_INFO("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 254594b31eaSFrederic Danis #define bt_dev_warn(hdev, fmt, ...) \ 2559b392e0eSLuiz Augusto von Dentz BT_WARN("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2566f558b70SLoic Poulain #define bt_dev_err(hdev, fmt, ...) \ 2579b392e0eSLuiz Augusto von Dentz BT_ERR("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2586f558b70SLoic Poulain #define bt_dev_dbg(hdev, fmt, ...) \ 2599b392e0eSLuiz Augusto von Dentz BT_DBG("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2606f558b70SLoic Poulain 26136278a5dSAlain Michaud #define bt_dev_warn_ratelimited(hdev, fmt, ...) \ 2629b392e0eSLuiz Augusto von Dentz bt_warn_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2632064ee33SMarcel Holtmann #define bt_dev_err_ratelimited(hdev, fmt, ...) \ 2649b392e0eSLuiz Augusto von Dentz bt_err_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__) 2652064ee33SMarcel Holtmann 2661da177e4SLinus Torvalds /* Connection and socket states */ 2671da177e4SLinus Torvalds enum { 2681da177e4SLinus Torvalds BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ 2691da177e4SLinus Torvalds BT_OPEN, 2701da177e4SLinus Torvalds BT_BOUND, 2711da177e4SLinus Torvalds BT_LISTEN, 2721da177e4SLinus Torvalds BT_CONNECT, 2731da177e4SLinus Torvalds BT_CONNECT2, 2741da177e4SLinus Torvalds BT_CONFIG, 2751da177e4SLinus Torvalds BT_DISCONN, 2761da177e4SLinus Torvalds BT_CLOSED 2771da177e4SLinus Torvalds }; 2781da177e4SLinus Torvalds 27920d1803aSAndrei Emeltchenko /* If unused will be removed by compiler */ 28020d1803aSAndrei Emeltchenko static inline const char *state_to_string(int state) 28120d1803aSAndrei Emeltchenko { 28220d1803aSAndrei Emeltchenko switch (state) { 28320d1803aSAndrei Emeltchenko case BT_CONNECTED: 28420d1803aSAndrei Emeltchenko return "BT_CONNECTED"; 28520d1803aSAndrei Emeltchenko case BT_OPEN: 28620d1803aSAndrei Emeltchenko return "BT_OPEN"; 28720d1803aSAndrei Emeltchenko case BT_BOUND: 28820d1803aSAndrei Emeltchenko return "BT_BOUND"; 28920d1803aSAndrei Emeltchenko case BT_LISTEN: 29020d1803aSAndrei Emeltchenko return "BT_LISTEN"; 29120d1803aSAndrei Emeltchenko case BT_CONNECT: 29220d1803aSAndrei Emeltchenko return "BT_CONNECT"; 29320d1803aSAndrei Emeltchenko case BT_CONNECT2: 29420d1803aSAndrei Emeltchenko return "BT_CONNECT2"; 29520d1803aSAndrei Emeltchenko case BT_CONFIG: 29620d1803aSAndrei Emeltchenko return "BT_CONFIG"; 29720d1803aSAndrei Emeltchenko case BT_DISCONN: 29820d1803aSAndrei Emeltchenko return "BT_DISCONN"; 29920d1803aSAndrei Emeltchenko case BT_CLOSED: 30020d1803aSAndrei Emeltchenko return "BT_CLOSED"; 30120d1803aSAndrei Emeltchenko } 30220d1803aSAndrei Emeltchenko 30320d1803aSAndrei Emeltchenko return "invalid state"; 30420d1803aSAndrei Emeltchenko } 30520d1803aSAndrei Emeltchenko 3061da177e4SLinus Torvalds /* BD Address */ 3071da177e4SLinus Torvalds typedef struct { 3081da177e4SLinus Torvalds __u8 b[6]; 30966c853ccSGustavo F. Padovan } __packed bdaddr_t; 3101da177e4SLinus Torvalds 311591f47f3SAndre Guedes /* BD Address type */ 312591f47f3SAndre Guedes #define BDADDR_BREDR 0x00 313591f47f3SAndre Guedes #define BDADDR_LE_PUBLIC 0x01 314591f47f3SAndre Guedes #define BDADDR_LE_RANDOM 0x02 315591f47f3SAndre Guedes 31621fcf572SPavel Machek static inline bool bdaddr_type_is_valid(u8 type) 317679efe2bSJohan Hedberg { 318679efe2bSJohan Hedberg switch (type) { 319679efe2bSJohan Hedberg case BDADDR_BREDR: 320679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC: 321679efe2bSJohan Hedberg case BDADDR_LE_RANDOM: 322679efe2bSJohan Hedberg return true; 323679efe2bSJohan Hedberg } 324679efe2bSJohan Hedberg 325679efe2bSJohan Hedberg return false; 326679efe2bSJohan Hedberg } 327679efe2bSJohan Hedberg 32821fcf572SPavel Machek static inline bool bdaddr_type_is_le(u8 type) 329679efe2bSJohan Hedberg { 330679efe2bSJohan Hedberg switch (type) { 331679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC: 332679efe2bSJohan Hedberg case BDADDR_LE_RANDOM: 333679efe2bSJohan Hedberg return true; 334679efe2bSJohan Hedberg } 335679efe2bSJohan Hedberg 336679efe2bSJohan Hedberg return false; 337679efe2bSJohan Hedberg } 338679efe2bSJohan Hedberg 3391da177e4SLinus Torvalds #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}}) 340a59ac2f7SMarcel Holtmann #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}) 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds /* Copy, swap, convert BD Address */ 343f53c20e9SDavid Herrmann static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2) 3441da177e4SLinus Torvalds { 3451da177e4SLinus Torvalds return memcmp(ba1, ba2, sizeof(bdaddr_t)); 3461da177e4SLinus Torvalds } 347f53c20e9SDavid Herrmann static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src) 3481da177e4SLinus Torvalds { 3491da177e4SLinus Torvalds memcpy(dst, src, sizeof(bdaddr_t)); 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 35265bce462SLoic Poulain void baswap(bdaddr_t *dst, const bdaddr_t *src); 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* Common socket structures and functions */ 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds #define bt_sk(__sk) ((struct bt_sock *) __sk) 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds struct bt_sock { 3591da177e4SLinus Torvalds struct sock sk; 3601da177e4SLinus Torvalds struct list_head accept_q; 3611da177e4SLinus Torvalds struct sock *parent; 362c5daa683SGustavo Padovan unsigned long flags; 363d9763698SMarcel Holtmann void (*skb_msg_name)(struct sk_buff *, void *, int *); 36400398e1dSAlain Michaud void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *); 365c5daa683SGustavo Padovan }; 366c5daa683SGustavo Padovan 367c5daa683SGustavo Padovan enum { 368c5daa683SGustavo Padovan BT_SK_DEFER_SETUP, 369c5daa683SGustavo Padovan BT_SK_SUSPEND, 3701da177e4SLinus Torvalds }; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds struct bt_sock_list { 3731da177e4SLinus Torvalds struct hlist_head head; 3741da177e4SLinus Torvalds rwlock_t lock; 375256a06c8SMasatake YAMATO #ifdef CONFIG_PROC_FS 376256a06c8SMasatake YAMATO int (* custom_seq_show)(struct seq_file *, void *); 377256a06c8SMasatake YAMATO #endif 3781da177e4SLinus Torvalds }; 3791da177e4SLinus Torvalds 380ec1b4cf7SStephen Hemminger int bt_sock_register(int proto, const struct net_proto_family *ops); 381be9f97f0SDavid Herrmann void bt_sock_unregister(int proto); 3821da177e4SLinus Torvalds void bt_sock_link(struct bt_sock_list *l, struct sock *s); 3831da177e4SLinus Torvalds void bt_sock_unlink(struct bt_sock_list *l, struct sock *s); 3841b784140SYing Xue int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 3851b784140SYing Xue int flags); 3861b784140SYing Xue int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg, 3871b784140SYing Xue size_t len, int flags); 388a11e1d43SLinus Torvalds __poll_t bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait); 3893241ad82SMarcel Holtmann int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); 3901da177e4SLinus Torvalds int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo); 391da891217SGavin Li int bt_sock_wait_ready(struct sock *sk, unsigned int msg_flags); 3921da177e4SLinus Torvalds 393c4f5627fSMatthias Kaehlcke void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh); 3941da177e4SLinus Torvalds void bt_accept_unlink(struct sock *sk); 3951da177e4SLinus Torvalds struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock); 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* Skb helpers */ 39800e3112cSMat Martineau struct l2cap_ctrl { 39921fcf572SPavel Machek u8 sframe:1, 40000e3112cSMat Martineau poll:1, 40100e3112cSMat Martineau final:1, 40200e3112cSMat Martineau fcs:1, 40300e3112cSMat Martineau sar:2, 40400e3112cSMat Martineau super:2; 40521fcf572SPavel Machek 40621fcf572SPavel Machek u16 reqseq; 40721fcf572SPavel Machek u16 txseq; 40821fcf572SPavel Machek u8 retries; 409a4368ff3SJohan Hedberg __le16 psm; 410a4368ff3SJohan Hedberg bdaddr_t bdaddr; 411a4368ff3SJohan Hedberg struct l2cap_chan *chan; 41200e3112cSMat Martineau }; 41300e3112cSMat Martineau 41400398e1dSAlain Michaud struct sco_ctrl { 41500398e1dSAlain Michaud u8 pkt_status; 41600398e1dSAlain Michaud }; 41700398e1dSAlain Michaud 4183119ae95SJohan Hedberg struct hci_dev; 4193119ae95SJohan Hedberg 4201904a853SMarcel Holtmann typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode); 421e6214487SJohan Hedberg typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status, 422e6214487SJohan Hedberg u16 opcode, struct sk_buff *skb); 4233119ae95SJohan Hedberg 42444d27137SJohan Hedberg #define HCI_REQ_START BIT(0) 42544d27137SJohan Hedberg #define HCI_REQ_SKB BIT(1) 42644d27137SJohan Hedberg 427242c0ebdSMarcel Holtmann struct hci_ctrl { 428cba6b758SLuiz Augusto von Dentz struct sock *sk; 42921fcf572SPavel Machek u16 opcode; 43044d27137SJohan Hedberg u8 req_flags; 431242c0ebdSMarcel Holtmann u8 req_event; 43244d27137SJohan Hedberg union { 433242c0ebdSMarcel Holtmann hci_req_complete_t req_complete; 434242c0ebdSMarcel Holtmann hci_req_complete_skb_t req_complete_skb; 435db6e3e8dSJohan Hedberg }; 43644d27137SJohan Hedberg }; 437db6e3e8dSJohan Hedberg 4388aca46f9SLuiz Augusto von Dentz struct mgmt_ctrl { 4398aca46f9SLuiz Augusto von Dentz struct hci_dev *hdev; 4408aca46f9SLuiz Augusto von Dentz u16 opcode; 4418aca46f9SLuiz Augusto von Dentz }; 4428aca46f9SLuiz Augusto von Dentz 4431da177e4SLinus Torvalds struct bt_skb_cb { 44421fcf572SPavel Machek u8 pkt_type; 44521fcf572SPavel Machek u8 force_active; 44621fcf572SPavel Machek u16 expect; 44721fcf572SPavel Machek u8 incoming:1; 448db6e3e8dSJohan Hedberg union { 449a4368ff3SJohan Hedberg struct l2cap_ctrl l2cap; 45000398e1dSAlain Michaud struct sco_ctrl sco; 451242c0ebdSMarcel Holtmann struct hci_ctrl hci; 4528aca46f9SLuiz Augusto von Dentz struct mgmt_ctrl mgmt; 453db6e3e8dSJohan Hedberg }; 4541da177e4SLinus Torvalds }; 4551c2acffbSGustavo F. Padovan #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb)) 4561da177e4SLinus Torvalds 457f5c4a42aSMarcel Holtmann #define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type 458f5c4a42aSMarcel Holtmann #define hci_skb_expect(skb) bt_cb((skb))->expect 459f5c4a42aSMarcel Holtmann #define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode 46085b56857SLuiz Augusto von Dentz #define hci_skb_event(skb) bt_cb((skb))->hci.req_event 461cba6b758SLuiz Augusto von Dentz #define hci_skb_sk(skb) bt_cb((skb))->hci.sk 462f5c4a42aSMarcel Holtmann 463dd0fc66fSAl Viro static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how) 4641da177e4SLinus Torvalds { 4651da177e4SLinus Torvalds struct sk_buff *skb; 4661da177e4SLinus Torvalds 467c3c7ea65SGustavo Padovan skb = alloc_skb(len + BT_SKB_RESERVE, how); 4685fcc86bdSJohan Hedberg if (skb) 4691da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE); 4701da177e4SLinus Torvalds return skb; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4735a9d0a3fSWaldemar Rymarkiewicz static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk, 4745a9d0a3fSWaldemar Rymarkiewicz unsigned long len, int nb, int *err) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds struct sk_buff *skb; 4771da177e4SLinus Torvalds 478c3c7ea65SGustavo Padovan skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err); 4795fcc86bdSJohan Hedberg if (skb) 4801da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE); 481e454c844SGustavo F. Padovan 482e454c844SGustavo F. Padovan if (!skb && *err) 483e454c844SGustavo F. Padovan return NULL; 484e454c844SGustavo F. Padovan 485e454c844SGustavo F. Padovan *err = sock_error(sk); 486e454c844SGustavo F. Padovan if (*err) 487e454c844SGustavo F. Padovan goto out; 488e454c844SGustavo F. Padovan 489e454c844SGustavo F. Padovan if (sk->sk_shutdown) { 490e454c844SGustavo F. Padovan *err = -ECONNRESET; 491e454c844SGustavo F. Padovan goto out; 492e454c844SGustavo F. Padovan } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds return skb; 495e454c844SGustavo F. Padovan 496e454c844SGustavo F. Padovan out: 497e454c844SGustavo F. Padovan kfree_skb(skb); 498e454c844SGustavo F. Padovan return NULL; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 50138f64f65SLuiz Augusto von Dentz /* Shall not be called with lock_sock held */ 50238f64f65SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk, 50338f64f65SLuiz Augusto von Dentz struct msghdr *msg, 50438f64f65SLuiz Augusto von Dentz size_t len, size_t mtu, 50538f64f65SLuiz Augusto von Dentz size_t headroom, size_t tailroom) 50638f64f65SLuiz Augusto von Dentz { 50738f64f65SLuiz Augusto von Dentz struct sk_buff *skb; 50838f64f65SLuiz Augusto von Dentz size_t size = min_t(size_t, len, mtu); 50938f64f65SLuiz Augusto von Dentz int err; 51038f64f65SLuiz Augusto von Dentz 51138f64f65SLuiz Augusto von Dentz skb = bt_skb_send_alloc(sk, size + headroom + tailroom, 51238f64f65SLuiz Augusto von Dentz msg->msg_flags & MSG_DONTWAIT, &err); 51338f64f65SLuiz Augusto von Dentz if (!skb) 51438f64f65SLuiz Augusto von Dentz return ERR_PTR(err); 51538f64f65SLuiz Augusto von Dentz 51638f64f65SLuiz Augusto von Dentz skb_reserve(skb, headroom); 51738f64f65SLuiz Augusto von Dentz skb_tailroom_reserve(skb, mtu, tailroom); 51838f64f65SLuiz Augusto von Dentz 51938f64f65SLuiz Augusto von Dentz if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) { 52038f64f65SLuiz Augusto von Dentz kfree_skb(skb); 52138f64f65SLuiz Augusto von Dentz return ERR_PTR(-EFAULT); 52238f64f65SLuiz Augusto von Dentz } 52338f64f65SLuiz Augusto von Dentz 52438f64f65SLuiz Augusto von Dentz skb->priority = sk->sk_priority; 52538f64f65SLuiz Augusto von Dentz 52638f64f65SLuiz Augusto von Dentz return skb; 52738f64f65SLuiz Augusto von Dentz } 52838f64f65SLuiz Augusto von Dentz 52997e4e802SLuiz Augusto von Dentz /* Similar to bt_skb_sendmsg but can split the msg into multiple fragments 53097e4e802SLuiz Augusto von Dentz * accourding to the MTU. 53197e4e802SLuiz Augusto von Dentz */ 53297e4e802SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmmsg(struct sock *sk, 53397e4e802SLuiz Augusto von Dentz struct msghdr *msg, 53497e4e802SLuiz Augusto von Dentz size_t len, size_t mtu, 53597e4e802SLuiz Augusto von Dentz size_t headroom, size_t tailroom) 53697e4e802SLuiz Augusto von Dentz { 53797e4e802SLuiz Augusto von Dentz struct sk_buff *skb, **frag; 53897e4e802SLuiz Augusto von Dentz 53997e4e802SLuiz Augusto von Dentz skb = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom); 5406f43f616SDan Carpenter if (IS_ERR(skb)) 54197e4e802SLuiz Augusto von Dentz return skb; 54297e4e802SLuiz Augusto von Dentz 54397e4e802SLuiz Augusto von Dentz len -= skb->len; 54497e4e802SLuiz Augusto von Dentz if (!len) 54597e4e802SLuiz Augusto von Dentz return skb; 54697e4e802SLuiz Augusto von Dentz 54797e4e802SLuiz Augusto von Dentz /* Add remaining data over MTU as continuation fragments */ 54897e4e802SLuiz Augusto von Dentz frag = &skb_shinfo(skb)->frag_list; 54997e4e802SLuiz Augusto von Dentz while (len) { 55097e4e802SLuiz Augusto von Dentz struct sk_buff *tmp; 55197e4e802SLuiz Augusto von Dentz 55297e4e802SLuiz Augusto von Dentz tmp = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom); 553266191aaSLuiz Augusto von Dentz if (IS_ERR(tmp)) { 55429fb6083SLuiz Augusto von Dentz return skb; 55597e4e802SLuiz Augusto von Dentz } 55697e4e802SLuiz Augusto von Dentz 55797e4e802SLuiz Augusto von Dentz len -= tmp->len; 55897e4e802SLuiz Augusto von Dentz 55997e4e802SLuiz Augusto von Dentz *frag = tmp; 56097e4e802SLuiz Augusto von Dentz frag = &(*frag)->next; 56197e4e802SLuiz Augusto von Dentz } 56297e4e802SLuiz Augusto von Dentz 56397e4e802SLuiz Augusto von Dentz return skb; 56497e4e802SLuiz Augusto von Dentz } 56597e4e802SLuiz Augusto von Dentz 56621fcf572SPavel Machek int bt_to_errno(u16 code); 567ca2045e0SLuiz Augusto von Dentz __u8 bt_status(int err); 5681da177e4SLinus Torvalds 5696befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr); 5706befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr); 571c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr); 572d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk); 57370ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk); 5746befc644SMarcel Holtmann 575e74e58f8SJoe Perches int hci_sock_init(void); 576e74e58f8SJoe Perches void hci_sock_cleanup(void); 5776516455dSMarcel Holtmann 578e74e58f8SJoe Perches int bt_sysfs_init(void); 579e74e58f8SJoe Perches void bt_sysfs_cleanup(void); 5806516455dSMarcel Holtmann 581e74e58f8SJoe Perches int bt_procfs_init(struct net *net, const char *name, 582256a06c8SMasatake YAMATO struct bt_sock_list *sk_list, 583256a06c8SMasatake YAMATO int (*seq_show)(struct seq_file *, void *)); 584e74e58f8SJoe Perches void bt_procfs_cleanup(struct net *net, const char *name); 585256a06c8SMasatake YAMATO 586aef7d97cSMarcel Holtmann extern struct dentry *bt_debugfs; 587be9d1227SMarcel Holtmann 58864274518SGustavo F. Padovan int l2cap_init(void); 58964274518SGustavo F. Padovan void l2cap_exit(void); 59064274518SGustavo F. Padovan 591ff50e8afSArron Wang #if IS_ENABLED(CONFIG_BT_BREDR) 59264274518SGustavo F. Padovan int sco_init(void); 59364274518SGustavo F. Padovan void sco_exit(void); 594ff50e8afSArron Wang #else 595ff50e8afSArron Wang static inline int sco_init(void) 596ff50e8afSArron Wang { 597ff50e8afSArron Wang return 0; 598ff50e8afSArron Wang } 599ff50e8afSArron Wang 600ff50e8afSArron Wang static inline void sco_exit(void) 601ff50e8afSArron Wang { 602ff50e8afSArron Wang } 603ff50e8afSArron Wang #endif 60464274518SGustavo F. Padovan 605ccf74f23SLuiz Augusto von Dentz #if IS_ENABLED(CONFIG_BT_LE) 606ccf74f23SLuiz Augusto von Dentz int iso_init(void); 607ccf74f23SLuiz Augusto von Dentz int iso_exit(void); 608ccf74f23SLuiz Augusto von Dentz bool iso_enabled(void); 609ccf74f23SLuiz Augusto von Dentz #else 610ccf74f23SLuiz Augusto von Dentz static inline int iso_init(void) 611ccf74f23SLuiz Augusto von Dentz { 612ccf74f23SLuiz Augusto von Dentz return 0; 613ccf74f23SLuiz Augusto von Dentz } 614ccf74f23SLuiz Augusto von Dentz 615ccf74f23SLuiz Augusto von Dentz static inline int iso_exit(void) 616ccf74f23SLuiz Augusto von Dentz { 617ccf74f23SLuiz Augusto von Dentz return 0; 618ccf74f23SLuiz Augusto von Dentz } 619ccf74f23SLuiz Augusto von Dentz 620ccf74f23SLuiz Augusto von Dentz static inline bool iso_enabled(void) 621ccf74f23SLuiz Augusto von Dentz { 622ccf74f23SLuiz Augusto von Dentz return false; 623ccf74f23SLuiz Augusto von Dentz } 624ccf74f23SLuiz Augusto von Dentz #endif 625ccf74f23SLuiz Augusto von Dentz 6266d785aa3SJohan Hedberg int mgmt_init(void); 6276d785aa3SJohan Hedberg void mgmt_exit(void); 6286d785aa3SJohan Hedberg 629d22015aaSOctavian Purdila void bt_sock_reclassify_lock(struct sock *sk, int proto); 630d22015aaSOctavian Purdila 6311da177e4SLinus Torvalds #endif /* __BLUETOOTH_H */ 632