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 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #define SOL_HCI 0 601da177e4SLinus Torvalds #define SOL_L2CAP 6 611da177e4SLinus Torvalds #define SOL_SCO 17 621da177e4SLinus Torvalds #define SOL_RFCOMM 18 631da177e4SLinus Torvalds 648c1b2355SMarcel Holtmann #define BT_SECURITY 4 658c1b2355SMarcel Holtmann struct bt_security { 668c1b2355SMarcel Holtmann __u8 level; 678f360119SVinicius Costa Gomes __u8 key_size; 688c1b2355SMarcel Holtmann }; 698c1b2355SMarcel Holtmann #define BT_SECURITY_SDP 0 708c1b2355SMarcel Holtmann #define BT_SECURITY_LOW 1 718c1b2355SMarcel Holtmann #define BT_SECURITY_MEDIUM 2 728c1b2355SMarcel Holtmann #define BT_SECURITY_HIGH 3 737b5a9241SMarcel Holtmann #define BT_SECURITY_FIPS 4 748c1b2355SMarcel Holtmann 75c4f912e1SMarcel Holtmann #define BT_DEFER_SETUP 7 76c4f912e1SMarcel Holtmann 77e702112fSAndrei Emeltchenko #define BT_FLUSHABLE 8 78e702112fSAndrei Emeltchenko 79e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_OFF 0 80e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_ON 1 81e702112fSAndrei Emeltchenko 8214b12d0bSJaikumar Ganesh #define BT_POWER 9 8314b12d0bSJaikumar Ganesh struct bt_power { 8414b12d0bSJaikumar Ganesh __u8 force_active; 8514b12d0bSJaikumar Ganesh }; 8614b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_OFF 0 8714b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_ON 1 8814b12d0bSJaikumar Ganesh 89c14968b0SMat Martineau #define BT_CHANNEL_POLICY 10 90c14968b0SMat Martineau 91c14968b0SMat Martineau /* BR/EDR only (default policy) 92c14968b0SMat Martineau * AMP controllers cannot be used. 93c14968b0SMat Martineau * Channel move requests from the remote device are denied. 94c14968b0SMat Martineau * If the L2CAP channel is currently using AMP, move the channel to BR/EDR. 95c14968b0SMat Martineau */ 96c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_ONLY 0 97c14968b0SMat Martineau 98c14968b0SMat Martineau /* BR/EDR Preferred 99c14968b0SMat Martineau * Allow use of AMP controllers. 100c14968b0SMat Martineau * If the L2CAP channel is currently on AMP, move it to BR/EDR. 101c14968b0SMat Martineau * Channel move requests from the remote device are allowed. 102c14968b0SMat Martineau */ 103c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_PREFERRED 1 104c14968b0SMat Martineau 105c14968b0SMat Martineau /* AMP Preferred 106c14968b0SMat Martineau * Allow use of AMP controllers 107c14968b0SMat Martineau * If the L2CAP channel is currently on BR/EDR and AMP controller 108c14968b0SMat Martineau * resources are available, initiate a channel move to AMP. 109c14968b0SMat Martineau * Channel move requests from the remote device are allowed. 110c14968b0SMat Martineau * If the L2CAP socket has not been connected yet, try to create 111c14968b0SMat Martineau * and configure the channel directly on an AMP controller rather 112c14968b0SMat Martineau * than BR/EDR. 113c14968b0SMat Martineau */ 114c14968b0SMat Martineau #define BT_CHANNEL_POLICY_AMP_PREFERRED 2 115c14968b0SMat Martineau 116ad10b1a4SFrédéric Dalleau #define BT_VOICE 11 117ad10b1a4SFrédéric Dalleau struct bt_voice { 118ad10b1a4SFrédéric Dalleau __u16 setting; 119ad10b1a4SFrédéric Dalleau }; 120ad10b1a4SFrédéric Dalleau 121ad10b1a4SFrédéric Dalleau #define BT_VOICE_TRANSPARENT 0x0003 122ad10b1a4SFrédéric Dalleau #define BT_VOICE_CVSD_16BIT 0x0060 123ad10b1a4SFrédéric Dalleau 1241f435424SJohan Hedberg #define BT_SNDMTU 12 1251f435424SJohan Hedberg #define BT_RCVMTU 13 126eab2404bSLuiz Augusto von Dentz #define BT_PHY 14 127eab2404bSLuiz Augusto von Dentz 128eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_1SLOT 0x00000001 129eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_3SLOT 0x00000002 130eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_5SLOT 0x00000004 131eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_1SLOT 0x00000008 132eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_3SLOT 0x00000010 133eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_5SLOT 0x00000020 134eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_1SLOT 0x00000040 135eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_3SLOT 0x00000080 136eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_5SLOT 0x00000100 137eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_TX 0x00000200 138eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_RX 0x00000400 139eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_TX 0x00000800 140eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_RX 0x00001000 141eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_TX 0x00002000 142eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_RX 0x00004000 1431f435424SJohan Hedberg 1443ee7b7cdSLuiz Augusto von Dentz #define BT_MODE 15 1453ee7b7cdSLuiz Augusto von Dentz 1463ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_BASIC 0x00 1473ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_ERTM 0x01 1483ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_STREAMING 0x02 1493ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_LE_FLOWCTL 0x03 1503ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_EXT_FLOWCTL 0x04 1513ee7b7cdSLuiz Augusto von Dentz 15200398e1dSAlain Michaud #define BT_PKT_STATUS 16 15300398e1dSAlain Michaud 15400398e1dSAlain Michaud #define BT_SCM_PKT_STATUS 0x03 15500398e1dSAlain Michaud 156248733e8SKiran K #define BT_CODEC 19 157248733e8SKiran K 158248733e8SKiran K struct bt_codec_caps { 159248733e8SKiran K __u8 len; 160248733e8SKiran K __u8 data[]; 161248733e8SKiran K } __packed; 162248733e8SKiran K 163248733e8SKiran K struct bt_codec { 164248733e8SKiran K __u8 id; 165248733e8SKiran K __u16 cid; 166248733e8SKiran K __u16 vid; 167248733e8SKiran K __u8 data_path; 168248733e8SKiran K __u8 num_caps; 169248733e8SKiran K } __packed; 170248733e8SKiran K 171248733e8SKiran K struct bt_codecs { 172248733e8SKiran K __u8 num_codecs; 173248733e8SKiran K struct bt_codec codecs[]; 174248733e8SKiran K } __packed; 175248733e8SKiran K 176f6873401SKiran K #define BT_CODEC_CVSD 0x02 177b2af264aSKiran K #define BT_CODEC_TRANSPARENT 0x03 178904c139aSKiran K #define BT_CODEC_MSBC 0x05 179f6873401SKiran K 1803ed7003eSJoe Perches __printf(1, 2) 1812b0bf6c8SJoe Perches void bt_info(const char *fmt, ...); 1823ed7003eSJoe Perches __printf(1, 2) 183594b31eaSFrederic Danis void bt_warn(const char *fmt, ...); 184594b31eaSFrederic Danis __printf(1, 2) 1852b0bf6c8SJoe Perches void bt_err(const char *fmt, ...); 186e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG) 187e625e50cSMarcel Holtmann void bt_dbg_set(bool enable); 188e625e50cSMarcel Holtmann bool bt_dbg_get(void); 189e625e50cSMarcel Holtmann __printf(1, 2) 190e625e50cSMarcel Holtmann void bt_dbg(const char *fmt, ...); 191e625e50cSMarcel Holtmann #endif 192e781b7f7SSzymon Janc __printf(1, 2) 19336278a5dSAlain Michaud void bt_warn_ratelimited(const char *fmt, ...); 19436278a5dSAlain Michaud __printf(1, 2) 195e781b7f7SSzymon Janc void bt_err_ratelimited(const char *fmt, ...); 196e1447d8dSJoe Perches 1973ed7003eSJoe Perches #define BT_INFO(fmt, ...) bt_info(fmt "\n", ##__VA_ARGS__) 198594b31eaSFrederic Danis #define BT_WARN(fmt, ...) bt_warn(fmt "\n", ##__VA_ARGS__) 1993ed7003eSJoe Perches #define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__) 200e625e50cSMarcel Holtmann 201e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG) 202e625e50cSMarcel Holtmann #define BT_DBG(fmt, ...) bt_dbg(fmt "\n", ##__VA_ARGS__) 203e625e50cSMarcel Holtmann #else 2043ed7003eSJoe Perches #define BT_DBG(fmt, ...) pr_debug(fmt "\n", ##__VA_ARGS__) 205e625e50cSMarcel Holtmann #endif 2061da177e4SLinus Torvalds 2076f558b70SLoic Poulain #define bt_dev_info(hdev, fmt, ...) \ 2086f558b70SLoic Poulain BT_INFO("%s: " fmt, (hdev)->name, ##__VA_ARGS__) 209594b31eaSFrederic Danis #define bt_dev_warn(hdev, fmt, ...) \ 210594b31eaSFrederic Danis BT_WARN("%s: " fmt, (hdev)->name, ##__VA_ARGS__) 2116f558b70SLoic Poulain #define bt_dev_err(hdev, fmt, ...) \ 2126f558b70SLoic Poulain BT_ERR("%s: " fmt, (hdev)->name, ##__VA_ARGS__) 2136f558b70SLoic Poulain #define bt_dev_dbg(hdev, fmt, ...) \ 2146f558b70SLoic Poulain BT_DBG("%s: " fmt, (hdev)->name, ##__VA_ARGS__) 2156f558b70SLoic Poulain 21636278a5dSAlain Michaud #define bt_dev_warn_ratelimited(hdev, fmt, ...) \ 21736278a5dSAlain Michaud bt_warn_ratelimited("%s: " fmt, (hdev)->name, ##__VA_ARGS__) 2182064ee33SMarcel Holtmann #define bt_dev_err_ratelimited(hdev, fmt, ...) \ 219657cc646SMarcel Holtmann bt_err_ratelimited("%s: " fmt, (hdev)->name, ##__VA_ARGS__) 2202064ee33SMarcel Holtmann 2211da177e4SLinus Torvalds /* Connection and socket states */ 2221da177e4SLinus Torvalds enum { 2231da177e4SLinus Torvalds BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ 2241da177e4SLinus Torvalds BT_OPEN, 2251da177e4SLinus Torvalds BT_BOUND, 2261da177e4SLinus Torvalds BT_LISTEN, 2271da177e4SLinus Torvalds BT_CONNECT, 2281da177e4SLinus Torvalds BT_CONNECT2, 2291da177e4SLinus Torvalds BT_CONFIG, 2301da177e4SLinus Torvalds BT_DISCONN, 2311da177e4SLinus Torvalds BT_CLOSED 2321da177e4SLinus Torvalds }; 2331da177e4SLinus Torvalds 23420d1803aSAndrei Emeltchenko /* If unused will be removed by compiler */ 23520d1803aSAndrei Emeltchenko static inline const char *state_to_string(int state) 23620d1803aSAndrei Emeltchenko { 23720d1803aSAndrei Emeltchenko switch (state) { 23820d1803aSAndrei Emeltchenko case BT_CONNECTED: 23920d1803aSAndrei Emeltchenko return "BT_CONNECTED"; 24020d1803aSAndrei Emeltchenko case BT_OPEN: 24120d1803aSAndrei Emeltchenko return "BT_OPEN"; 24220d1803aSAndrei Emeltchenko case BT_BOUND: 24320d1803aSAndrei Emeltchenko return "BT_BOUND"; 24420d1803aSAndrei Emeltchenko case BT_LISTEN: 24520d1803aSAndrei Emeltchenko return "BT_LISTEN"; 24620d1803aSAndrei Emeltchenko case BT_CONNECT: 24720d1803aSAndrei Emeltchenko return "BT_CONNECT"; 24820d1803aSAndrei Emeltchenko case BT_CONNECT2: 24920d1803aSAndrei Emeltchenko return "BT_CONNECT2"; 25020d1803aSAndrei Emeltchenko case BT_CONFIG: 25120d1803aSAndrei Emeltchenko return "BT_CONFIG"; 25220d1803aSAndrei Emeltchenko case BT_DISCONN: 25320d1803aSAndrei Emeltchenko return "BT_DISCONN"; 25420d1803aSAndrei Emeltchenko case BT_CLOSED: 25520d1803aSAndrei Emeltchenko return "BT_CLOSED"; 25620d1803aSAndrei Emeltchenko } 25720d1803aSAndrei Emeltchenko 25820d1803aSAndrei Emeltchenko return "invalid state"; 25920d1803aSAndrei Emeltchenko } 26020d1803aSAndrei Emeltchenko 2611da177e4SLinus Torvalds /* BD Address */ 2621da177e4SLinus Torvalds typedef struct { 2631da177e4SLinus Torvalds __u8 b[6]; 26466c853ccSGustavo F. Padovan } __packed bdaddr_t; 2651da177e4SLinus Torvalds 266591f47f3SAndre Guedes /* BD Address type */ 267591f47f3SAndre Guedes #define BDADDR_BREDR 0x00 268591f47f3SAndre Guedes #define BDADDR_LE_PUBLIC 0x01 269591f47f3SAndre Guedes #define BDADDR_LE_RANDOM 0x02 270591f47f3SAndre Guedes 27121fcf572SPavel Machek static inline bool bdaddr_type_is_valid(u8 type) 272679efe2bSJohan Hedberg { 273679efe2bSJohan Hedberg switch (type) { 274679efe2bSJohan Hedberg case BDADDR_BREDR: 275679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC: 276679efe2bSJohan Hedberg case BDADDR_LE_RANDOM: 277679efe2bSJohan Hedberg return true; 278679efe2bSJohan Hedberg } 279679efe2bSJohan Hedberg 280679efe2bSJohan Hedberg return false; 281679efe2bSJohan Hedberg } 282679efe2bSJohan Hedberg 28321fcf572SPavel Machek static inline bool bdaddr_type_is_le(u8 type) 284679efe2bSJohan Hedberg { 285679efe2bSJohan Hedberg switch (type) { 286679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC: 287679efe2bSJohan Hedberg case BDADDR_LE_RANDOM: 288679efe2bSJohan Hedberg return true; 289679efe2bSJohan Hedberg } 290679efe2bSJohan Hedberg 291679efe2bSJohan Hedberg return false; 292679efe2bSJohan Hedberg } 293679efe2bSJohan Hedberg 2941da177e4SLinus Torvalds #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}}) 295a59ac2f7SMarcel Holtmann #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}) 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* Copy, swap, convert BD Address */ 298f53c20e9SDavid Herrmann static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds return memcmp(ba1, ba2, sizeof(bdaddr_t)); 3011da177e4SLinus Torvalds } 302f53c20e9SDavid Herrmann static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src) 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds memcpy(dst, src, sizeof(bdaddr_t)); 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 30765bce462SLoic Poulain void baswap(bdaddr_t *dst, const bdaddr_t *src); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds /* Common socket structures and functions */ 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds #define bt_sk(__sk) ((struct bt_sock *) __sk) 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds struct bt_sock { 3141da177e4SLinus Torvalds struct sock sk; 3151da177e4SLinus Torvalds struct list_head accept_q; 3161da177e4SLinus Torvalds struct sock *parent; 317c5daa683SGustavo Padovan unsigned long flags; 318d9763698SMarcel Holtmann void (*skb_msg_name)(struct sk_buff *, void *, int *); 31900398e1dSAlain Michaud void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *); 320c5daa683SGustavo Padovan }; 321c5daa683SGustavo Padovan 322c5daa683SGustavo Padovan enum { 323c5daa683SGustavo Padovan BT_SK_DEFER_SETUP, 324c5daa683SGustavo Padovan BT_SK_SUSPEND, 3251da177e4SLinus Torvalds }; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds struct bt_sock_list { 3281da177e4SLinus Torvalds struct hlist_head head; 3291da177e4SLinus Torvalds rwlock_t lock; 330256a06c8SMasatake YAMATO #ifdef CONFIG_PROC_FS 331256a06c8SMasatake YAMATO int (* custom_seq_show)(struct seq_file *, void *); 332256a06c8SMasatake YAMATO #endif 3331da177e4SLinus Torvalds }; 3341da177e4SLinus Torvalds 335ec1b4cf7SStephen Hemminger int bt_sock_register(int proto, const struct net_proto_family *ops); 336be9f97f0SDavid Herrmann void bt_sock_unregister(int proto); 3371da177e4SLinus Torvalds void bt_sock_link(struct bt_sock_list *l, struct sock *s); 3381da177e4SLinus Torvalds void bt_sock_unlink(struct bt_sock_list *l, struct sock *s); 3391b784140SYing Xue int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 3401b784140SYing Xue int flags); 3411b784140SYing Xue int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg, 3421b784140SYing Xue size_t len, int flags); 343a11e1d43SLinus Torvalds __poll_t bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait); 3443241ad82SMarcel Holtmann int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); 3451da177e4SLinus Torvalds int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo); 346e793dcf0SJohan Hedberg int bt_sock_wait_ready(struct sock *sk, unsigned long flags); 3471da177e4SLinus Torvalds 348c4f5627fSMatthias Kaehlcke void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh); 3491da177e4SLinus Torvalds void bt_accept_unlink(struct sock *sk); 3501da177e4SLinus Torvalds struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock); 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* Skb helpers */ 35300e3112cSMat Martineau struct l2cap_ctrl { 35421fcf572SPavel Machek u8 sframe:1, 35500e3112cSMat Martineau poll:1, 35600e3112cSMat Martineau final:1, 35700e3112cSMat Martineau fcs:1, 35800e3112cSMat Martineau sar:2, 35900e3112cSMat Martineau super:2; 36021fcf572SPavel Machek 36121fcf572SPavel Machek u16 reqseq; 36221fcf572SPavel Machek u16 txseq; 36321fcf572SPavel Machek u8 retries; 364a4368ff3SJohan Hedberg __le16 psm; 365a4368ff3SJohan Hedberg bdaddr_t bdaddr; 366a4368ff3SJohan Hedberg struct l2cap_chan *chan; 36700e3112cSMat Martineau }; 36800e3112cSMat Martineau 36900398e1dSAlain Michaud struct sco_ctrl { 37000398e1dSAlain Michaud u8 pkt_status; 37100398e1dSAlain Michaud }; 37200398e1dSAlain Michaud 3733119ae95SJohan Hedberg struct hci_dev; 3743119ae95SJohan Hedberg 3751904a853SMarcel Holtmann typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode); 376e6214487SJohan Hedberg typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status, 377e6214487SJohan Hedberg u16 opcode, struct sk_buff *skb); 3783119ae95SJohan Hedberg 37944d27137SJohan Hedberg #define HCI_REQ_START BIT(0) 38044d27137SJohan Hedberg #define HCI_REQ_SKB BIT(1) 38144d27137SJohan Hedberg 382242c0ebdSMarcel Holtmann struct hci_ctrl { 38321fcf572SPavel Machek u16 opcode; 38444d27137SJohan Hedberg u8 req_flags; 385242c0ebdSMarcel Holtmann u8 req_event; 38644d27137SJohan Hedberg union { 387242c0ebdSMarcel Holtmann hci_req_complete_t req_complete; 388242c0ebdSMarcel Holtmann hci_req_complete_skb_t req_complete_skb; 389db6e3e8dSJohan Hedberg }; 39044d27137SJohan Hedberg }; 391db6e3e8dSJohan Hedberg 3921da177e4SLinus Torvalds struct bt_skb_cb { 39321fcf572SPavel Machek u8 pkt_type; 39421fcf572SPavel Machek u8 force_active; 39521fcf572SPavel Machek u16 expect; 39621fcf572SPavel Machek u8 incoming:1; 397db6e3e8dSJohan Hedberg union { 398a4368ff3SJohan Hedberg struct l2cap_ctrl l2cap; 39900398e1dSAlain Michaud struct sco_ctrl sco; 400242c0ebdSMarcel Holtmann struct hci_ctrl hci; 401db6e3e8dSJohan Hedberg }; 4021da177e4SLinus Torvalds }; 4031c2acffbSGustavo F. Padovan #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb)) 4041da177e4SLinus Torvalds 405f5c4a42aSMarcel Holtmann #define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type 406f5c4a42aSMarcel Holtmann #define hci_skb_expect(skb) bt_cb((skb))->expect 407f5c4a42aSMarcel Holtmann #define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode 408f5c4a42aSMarcel Holtmann 409dd0fc66fSAl Viro static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds struct sk_buff *skb; 4121da177e4SLinus Torvalds 413c3c7ea65SGustavo Padovan skb = alloc_skb(len + BT_SKB_RESERVE, how); 4145fcc86bdSJohan Hedberg if (skb) 4151da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE); 4161da177e4SLinus Torvalds return skb; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4195a9d0a3fSWaldemar Rymarkiewicz static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk, 4205a9d0a3fSWaldemar Rymarkiewicz unsigned long len, int nb, int *err) 4211da177e4SLinus Torvalds { 4221da177e4SLinus Torvalds struct sk_buff *skb; 4231da177e4SLinus Torvalds 424c3c7ea65SGustavo Padovan skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err); 4255fcc86bdSJohan Hedberg if (skb) 4261da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE); 427e454c844SGustavo F. Padovan 428e454c844SGustavo F. Padovan if (!skb && *err) 429e454c844SGustavo F. Padovan return NULL; 430e454c844SGustavo F. Padovan 431e454c844SGustavo F. Padovan *err = sock_error(sk); 432e454c844SGustavo F. Padovan if (*err) 433e454c844SGustavo F. Padovan goto out; 434e454c844SGustavo F. Padovan 435e454c844SGustavo F. Padovan if (sk->sk_shutdown) { 436e454c844SGustavo F. Padovan *err = -ECONNRESET; 437e454c844SGustavo F. Padovan goto out; 438e454c844SGustavo F. Padovan } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds return skb; 441e454c844SGustavo F. Padovan 442e454c844SGustavo F. Padovan out: 443e454c844SGustavo F. Padovan kfree_skb(skb); 444e454c844SGustavo F. Padovan return NULL; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 44738f64f65SLuiz Augusto von Dentz /* Shall not be called with lock_sock held */ 44838f64f65SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk, 44938f64f65SLuiz Augusto von Dentz struct msghdr *msg, 45038f64f65SLuiz Augusto von Dentz size_t len, size_t mtu, 45138f64f65SLuiz Augusto von Dentz size_t headroom, size_t tailroom) 45238f64f65SLuiz Augusto von Dentz { 45338f64f65SLuiz Augusto von Dentz struct sk_buff *skb; 45438f64f65SLuiz Augusto von Dentz size_t size = min_t(size_t, len, mtu); 45538f64f65SLuiz Augusto von Dentz int err; 45638f64f65SLuiz Augusto von Dentz 45738f64f65SLuiz Augusto von Dentz skb = bt_skb_send_alloc(sk, size + headroom + tailroom, 45838f64f65SLuiz Augusto von Dentz msg->msg_flags & MSG_DONTWAIT, &err); 45938f64f65SLuiz Augusto von Dentz if (!skb) 46038f64f65SLuiz Augusto von Dentz return ERR_PTR(err); 46138f64f65SLuiz Augusto von Dentz 46238f64f65SLuiz Augusto von Dentz skb_reserve(skb, headroom); 46338f64f65SLuiz Augusto von Dentz skb_tailroom_reserve(skb, mtu, tailroom); 46438f64f65SLuiz Augusto von Dentz 46538f64f65SLuiz Augusto von Dentz if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) { 46638f64f65SLuiz Augusto von Dentz kfree_skb(skb); 46738f64f65SLuiz Augusto von Dentz return ERR_PTR(-EFAULT); 46838f64f65SLuiz Augusto von Dentz } 46938f64f65SLuiz Augusto von Dentz 47038f64f65SLuiz Augusto von Dentz skb->priority = sk->sk_priority; 47138f64f65SLuiz Augusto von Dentz 47238f64f65SLuiz Augusto von Dentz return skb; 47338f64f65SLuiz Augusto von Dentz } 47438f64f65SLuiz Augusto von Dentz 475*97e4e802SLuiz Augusto von Dentz /* Similar to bt_skb_sendmsg but can split the msg into multiple fragments 476*97e4e802SLuiz Augusto von Dentz * accourding to the MTU. 477*97e4e802SLuiz Augusto von Dentz */ 478*97e4e802SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmmsg(struct sock *sk, 479*97e4e802SLuiz Augusto von Dentz struct msghdr *msg, 480*97e4e802SLuiz Augusto von Dentz size_t len, size_t mtu, 481*97e4e802SLuiz Augusto von Dentz size_t headroom, size_t tailroom) 482*97e4e802SLuiz Augusto von Dentz { 483*97e4e802SLuiz Augusto von Dentz struct sk_buff *skb, **frag; 484*97e4e802SLuiz Augusto von Dentz 485*97e4e802SLuiz Augusto von Dentz skb = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom); 486*97e4e802SLuiz Augusto von Dentz if (IS_ERR_OR_NULL(skb)) 487*97e4e802SLuiz Augusto von Dentz return skb; 488*97e4e802SLuiz Augusto von Dentz 489*97e4e802SLuiz Augusto von Dentz len -= skb->len; 490*97e4e802SLuiz Augusto von Dentz if (!len) 491*97e4e802SLuiz Augusto von Dentz return skb; 492*97e4e802SLuiz Augusto von Dentz 493*97e4e802SLuiz Augusto von Dentz /* Add remaining data over MTU as continuation fragments */ 494*97e4e802SLuiz Augusto von Dentz frag = &skb_shinfo(skb)->frag_list; 495*97e4e802SLuiz Augusto von Dentz while (len) { 496*97e4e802SLuiz Augusto von Dentz struct sk_buff *tmp; 497*97e4e802SLuiz Augusto von Dentz 498*97e4e802SLuiz Augusto von Dentz tmp = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom); 499*97e4e802SLuiz Augusto von Dentz if (IS_ERR_OR_NULL(tmp)) { 500*97e4e802SLuiz Augusto von Dentz kfree_skb(skb); 501*97e4e802SLuiz Augusto von Dentz return tmp; 502*97e4e802SLuiz Augusto von Dentz } 503*97e4e802SLuiz Augusto von Dentz 504*97e4e802SLuiz Augusto von Dentz len -= tmp->len; 505*97e4e802SLuiz Augusto von Dentz 506*97e4e802SLuiz Augusto von Dentz *frag = tmp; 507*97e4e802SLuiz Augusto von Dentz frag = &(*frag)->next; 508*97e4e802SLuiz Augusto von Dentz } 509*97e4e802SLuiz Augusto von Dentz 510*97e4e802SLuiz Augusto von Dentz return skb; 511*97e4e802SLuiz Augusto von Dentz } 512*97e4e802SLuiz Augusto von Dentz 51321fcf572SPavel Machek int bt_to_errno(u16 code); 5141da177e4SLinus Torvalds 5156befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr); 5166befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr); 517c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr); 518d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk); 51970ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk); 5206befc644SMarcel Holtmann 521e74e58f8SJoe Perches int hci_sock_init(void); 522e74e58f8SJoe Perches void hci_sock_cleanup(void); 5236516455dSMarcel Holtmann 524e74e58f8SJoe Perches int bt_sysfs_init(void); 525e74e58f8SJoe Perches void bt_sysfs_cleanup(void); 5266516455dSMarcel Holtmann 527e74e58f8SJoe Perches int bt_procfs_init(struct net *net, const char *name, 528256a06c8SMasatake YAMATO struct bt_sock_list *sk_list, 529256a06c8SMasatake YAMATO int (*seq_show)(struct seq_file *, void *)); 530e74e58f8SJoe Perches void bt_procfs_cleanup(struct net *net, const char *name); 531256a06c8SMasatake YAMATO 532aef7d97cSMarcel Holtmann extern struct dentry *bt_debugfs; 533be9d1227SMarcel Holtmann 53464274518SGustavo F. Padovan int l2cap_init(void); 53564274518SGustavo F. Padovan void l2cap_exit(void); 53664274518SGustavo F. Padovan 537ff50e8afSArron Wang #if IS_ENABLED(CONFIG_BT_BREDR) 53864274518SGustavo F. Padovan int sco_init(void); 53964274518SGustavo F. Padovan void sco_exit(void); 540ff50e8afSArron Wang #else 541ff50e8afSArron Wang static inline int sco_init(void) 542ff50e8afSArron Wang { 543ff50e8afSArron Wang return 0; 544ff50e8afSArron Wang } 545ff50e8afSArron Wang 546ff50e8afSArron Wang static inline void sco_exit(void) 547ff50e8afSArron Wang { 548ff50e8afSArron Wang } 549ff50e8afSArron Wang #endif 55064274518SGustavo F. Padovan 5516d785aa3SJohan Hedberg int mgmt_init(void); 5526d785aa3SJohan Hedberg void mgmt_exit(void); 5536d785aa3SJohan Hedberg 554d22015aaSOctavian Purdila void bt_sock_reclassify_lock(struct sock *sk, int proto); 555d22015aaSOctavian Purdila 5561da177e4SLinus Torvalds #endif /* __BLUETOOTH_H */ 557