Back to home page

OSCL-LXR

 
 

    


0001 /*
0002    BlueZ - Bluetooth protocol stack for Linux
0003    Copyright (C) 2000-2001 Qualcomm Incorporated
0004 
0005    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
0006 
0007    This program is free software; you can redistribute it and/or modify
0008    it under the terms of the GNU General Public License version 2 as
0009    published by the Free Software Foundation;
0010 
0011    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0012    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0013    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
0014    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
0015    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
0016    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0017    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0018    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0019 
0020    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
0021    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
0022    SOFTWARE IS DISCLAIMED.
0023 */
0024 
0025 #ifndef __BLUETOOTH_H
0026 #define __BLUETOOTH_H
0027 
0028 #include <linux/poll.h>
0029 #include <net/sock.h>
0030 #include <linux/seq_file.h>
0031 
0032 #define BT_SUBSYS_VERSION   2
0033 #define BT_SUBSYS_REVISION  22
0034 
0035 #ifndef AF_BLUETOOTH
0036 #define AF_BLUETOOTH    31
0037 #define PF_BLUETOOTH    AF_BLUETOOTH
0038 #endif
0039 
0040 /* Bluetooth versions */
0041 #define BLUETOOTH_VER_1_1   1
0042 #define BLUETOOTH_VER_1_2   2
0043 #define BLUETOOTH_VER_2_0   3
0044 #define BLUETOOTH_VER_2_1   4
0045 #define BLUETOOTH_VER_4_0   6
0046 
0047 /* Reserv for core and drivers use */
0048 #define BT_SKB_RESERVE  8
0049 
0050 #define BTPROTO_L2CAP   0
0051 #define BTPROTO_HCI 1
0052 #define BTPROTO_SCO 2
0053 #define BTPROTO_RFCOMM  3
0054 #define BTPROTO_BNEP    4
0055 #define BTPROTO_CMTP    5
0056 #define BTPROTO_HIDP    6
0057 #define BTPROTO_AVDTP   7
0058 #define BTPROTO_ISO 8
0059 #define BTPROTO_LAST    BTPROTO_ISO
0060 
0061 #define SOL_HCI     0
0062 #define SOL_L2CAP   6
0063 #define SOL_SCO     17
0064 #define SOL_RFCOMM  18
0065 
0066 #define BT_SECURITY 4
0067 struct bt_security {
0068     __u8 level;
0069     __u8 key_size;
0070 };
0071 #define BT_SECURITY_SDP     0
0072 #define BT_SECURITY_LOW     1
0073 #define BT_SECURITY_MEDIUM  2
0074 #define BT_SECURITY_HIGH    3
0075 #define BT_SECURITY_FIPS    4
0076 
0077 #define BT_DEFER_SETUP  7
0078 
0079 #define BT_FLUSHABLE    8
0080 
0081 #define BT_FLUSHABLE_OFF    0
0082 #define BT_FLUSHABLE_ON     1
0083 
0084 #define BT_POWER    9
0085 struct bt_power {
0086     __u8 force_active;
0087 };
0088 #define BT_POWER_FORCE_ACTIVE_OFF 0
0089 #define BT_POWER_FORCE_ACTIVE_ON  1
0090 
0091 #define BT_CHANNEL_POLICY   10
0092 
0093 /* BR/EDR only (default policy)
0094  *   AMP controllers cannot be used.
0095  *   Channel move requests from the remote device are denied.
0096  *   If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
0097  */
0098 #define BT_CHANNEL_POLICY_BREDR_ONLY        0
0099 
0100 /* BR/EDR Preferred
0101  *   Allow use of AMP controllers.
0102  *   If the L2CAP channel is currently on AMP, move it to BR/EDR.
0103  *   Channel move requests from the remote device are allowed.
0104  */
0105 #define BT_CHANNEL_POLICY_BREDR_PREFERRED   1
0106 
0107 /* AMP Preferred
0108  *   Allow use of AMP controllers
0109  *   If the L2CAP channel is currently on BR/EDR and AMP controller
0110  *     resources are available, initiate a channel move to AMP.
0111  *   Channel move requests from the remote device are allowed.
0112  *   If the L2CAP socket has not been connected yet, try to create
0113  *     and configure the channel directly on an AMP controller rather
0114  *     than BR/EDR.
0115  */
0116 #define BT_CHANNEL_POLICY_AMP_PREFERRED     2
0117 
0118 #define BT_VOICE        11
0119 struct bt_voice {
0120     __u16 setting;
0121 };
0122 
0123 #define BT_VOICE_TRANSPARENT            0x0003
0124 #define BT_VOICE_CVSD_16BIT         0x0060
0125 
0126 #define BT_SNDMTU       12
0127 #define BT_RCVMTU       13
0128 #define BT_PHY          14
0129 
0130 #define BT_PHY_BR_1M_1SLOT  0x00000001
0131 #define BT_PHY_BR_1M_3SLOT  0x00000002
0132 #define BT_PHY_BR_1M_5SLOT  0x00000004
0133 #define BT_PHY_EDR_2M_1SLOT 0x00000008
0134 #define BT_PHY_EDR_2M_3SLOT 0x00000010
0135 #define BT_PHY_EDR_2M_5SLOT 0x00000020
0136 #define BT_PHY_EDR_3M_1SLOT 0x00000040
0137 #define BT_PHY_EDR_3M_3SLOT 0x00000080
0138 #define BT_PHY_EDR_3M_5SLOT 0x00000100
0139 #define BT_PHY_LE_1M_TX     0x00000200
0140 #define BT_PHY_LE_1M_RX     0x00000400
0141 #define BT_PHY_LE_2M_TX     0x00000800
0142 #define BT_PHY_LE_2M_RX     0x00001000
0143 #define BT_PHY_LE_CODED_TX  0x00002000
0144 #define BT_PHY_LE_CODED_RX  0x00004000
0145 
0146 #define BT_MODE         15
0147 
0148 #define BT_MODE_BASIC       0x00
0149 #define BT_MODE_ERTM        0x01
0150 #define BT_MODE_STREAMING   0x02
0151 #define BT_MODE_LE_FLOWCTL  0x03
0152 #define BT_MODE_EXT_FLOWCTL 0x04
0153 
0154 #define BT_PKT_STATUS           16
0155 
0156 #define BT_SCM_PKT_STATUS   0x03
0157 
0158 #define BT_ISO_QOS      17
0159 
0160 #define BT_ISO_QOS_CIG_UNSET    0xff
0161 #define BT_ISO_QOS_CIS_UNSET    0xff
0162 
0163 #define BT_ISO_QOS_BIG_UNSET    0xff
0164 #define BT_ISO_QOS_BIS_UNSET    0xff
0165 
0166 struct bt_iso_io_qos {
0167     __u32 interval;
0168     __u16 latency;
0169     __u16 sdu;
0170     __u8  phy;
0171     __u8  rtn;
0172 };
0173 
0174 struct bt_iso_qos {
0175     union {
0176         __u8  cig;
0177         __u8  big;
0178     };
0179     union {
0180         __u8  cis;
0181         __u8  bis;
0182     };
0183     union {
0184         __u8  sca;
0185         __u8  sync_interval;
0186     };
0187     __u8  packing;
0188     __u8  framing;
0189     struct bt_iso_io_qos in;
0190     struct bt_iso_io_qos out;
0191 };
0192 
0193 #define BT_ISO_PHY_1M       0x01
0194 #define BT_ISO_PHY_2M       0x02
0195 #define BT_ISO_PHY_CODED    0x04
0196 #define BT_ISO_PHY_ANY      (BT_ISO_PHY_1M | BT_ISO_PHY_2M | \
0197                  BT_ISO_PHY_CODED)
0198 
0199 #define BT_CODEC    19
0200 
0201 struct  bt_codec_caps {
0202     __u8    len;
0203     __u8    data[];
0204 } __packed;
0205 
0206 struct bt_codec {
0207     __u8    id;
0208     __u16   cid;
0209     __u16   vid;
0210     __u8    data_path;
0211     __u8    num_caps;
0212 } __packed;
0213 
0214 struct bt_codecs {
0215     __u8        num_codecs;
0216     struct bt_codec codecs[];
0217 } __packed;
0218 
0219 #define BT_CODEC_CVSD       0x02
0220 #define BT_CODEC_TRANSPARENT    0x03
0221 #define BT_CODEC_MSBC       0x05
0222 
0223 #define BT_ISO_BASE     20
0224 
0225 __printf(1, 2)
0226 void bt_info(const char *fmt, ...);
0227 __printf(1, 2)
0228 void bt_warn(const char *fmt, ...);
0229 __printf(1, 2)
0230 void bt_err(const char *fmt, ...);
0231 #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
0232 void bt_dbg_set(bool enable);
0233 bool bt_dbg_get(void);
0234 __printf(1, 2)
0235 void bt_dbg(const char *fmt, ...);
0236 #endif
0237 __printf(1, 2)
0238 void bt_warn_ratelimited(const char *fmt, ...);
0239 __printf(1, 2)
0240 void bt_err_ratelimited(const char *fmt, ...);
0241 
0242 #define BT_INFO(fmt, ...)   bt_info(fmt "\n", ##__VA_ARGS__)
0243 #define BT_WARN(fmt, ...)   bt_warn(fmt "\n", ##__VA_ARGS__)
0244 #define BT_ERR(fmt, ...)    bt_err(fmt "\n", ##__VA_ARGS__)
0245 
0246 #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
0247 #define BT_DBG(fmt, ...)    bt_dbg(fmt "\n", ##__VA_ARGS__)
0248 #else
0249 #define BT_DBG(fmt, ...)    pr_debug(fmt "\n", ##__VA_ARGS__)
0250 #endif
0251 
0252 #define bt_dev_name(hdev) ((hdev) ? (hdev)->name : "null")
0253 
0254 #define bt_dev_info(hdev, fmt, ...)             \
0255     BT_INFO("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
0256 #define bt_dev_warn(hdev, fmt, ...)             \
0257     BT_WARN("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
0258 #define bt_dev_err(hdev, fmt, ...)              \
0259     BT_ERR("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
0260 #define bt_dev_dbg(hdev, fmt, ...)              \
0261     BT_DBG("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
0262 
0263 #define bt_dev_warn_ratelimited(hdev, fmt, ...)         \
0264     bt_warn_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
0265 #define bt_dev_err_ratelimited(hdev, fmt, ...)          \
0266     bt_err_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
0267 
0268 /* Connection and socket states */
0269 enum {
0270     BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
0271     BT_OPEN,
0272     BT_BOUND,
0273     BT_LISTEN,
0274     BT_CONNECT,
0275     BT_CONNECT2,
0276     BT_CONFIG,
0277     BT_DISCONN,
0278     BT_CLOSED
0279 };
0280 
0281 /* If unused will be removed by compiler */
0282 static inline const char *state_to_string(int state)
0283 {
0284     switch (state) {
0285     case BT_CONNECTED:
0286         return "BT_CONNECTED";
0287     case BT_OPEN:
0288         return "BT_OPEN";
0289     case BT_BOUND:
0290         return "BT_BOUND";
0291     case BT_LISTEN:
0292         return "BT_LISTEN";
0293     case BT_CONNECT:
0294         return "BT_CONNECT";
0295     case BT_CONNECT2:
0296         return "BT_CONNECT2";
0297     case BT_CONFIG:
0298         return "BT_CONFIG";
0299     case BT_DISCONN:
0300         return "BT_DISCONN";
0301     case BT_CLOSED:
0302         return "BT_CLOSED";
0303     }
0304 
0305     return "invalid state";
0306 }
0307 
0308 /* BD Address */
0309 typedef struct {
0310     __u8 b[6];
0311 } __packed bdaddr_t;
0312 
0313 /* BD Address type */
0314 #define BDADDR_BREDR        0x00
0315 #define BDADDR_LE_PUBLIC    0x01
0316 #define BDADDR_LE_RANDOM    0x02
0317 
0318 static inline bool bdaddr_type_is_valid(u8 type)
0319 {
0320     switch (type) {
0321     case BDADDR_BREDR:
0322     case BDADDR_LE_PUBLIC:
0323     case BDADDR_LE_RANDOM:
0324         return true;
0325     }
0326 
0327     return false;
0328 }
0329 
0330 static inline bool bdaddr_type_is_le(u8 type)
0331 {
0332     switch (type) {
0333     case BDADDR_LE_PUBLIC:
0334     case BDADDR_LE_RANDOM:
0335         return true;
0336     }
0337 
0338     return false;
0339 }
0340 
0341 #define BDADDR_ANY  (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
0342 #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
0343 
0344 /* Copy, swap, convert BD Address */
0345 static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
0346 {
0347     return memcmp(ba1, ba2, sizeof(bdaddr_t));
0348 }
0349 static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
0350 {
0351     memcpy(dst, src, sizeof(bdaddr_t));
0352 }
0353 
0354 void baswap(bdaddr_t *dst, const bdaddr_t *src);
0355 
0356 /* Common socket structures and functions */
0357 
0358 #define bt_sk(__sk) ((struct bt_sock *) __sk)
0359 
0360 struct bt_sock {
0361     struct sock sk;
0362     struct list_head accept_q;
0363     struct sock *parent;
0364     unsigned long flags;
0365     void (*skb_msg_name)(struct sk_buff *, void *, int *);
0366     void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *);
0367 };
0368 
0369 enum {
0370     BT_SK_DEFER_SETUP,
0371     BT_SK_SUSPEND,
0372 };
0373 
0374 struct bt_sock_list {
0375     struct hlist_head head;
0376     rwlock_t          lock;
0377 #ifdef CONFIG_PROC_FS
0378         int (* custom_seq_show)(struct seq_file *, void *);
0379 #endif
0380 };
0381 
0382 int  bt_sock_register(int proto, const struct net_proto_family *ops);
0383 void bt_sock_unregister(int proto);
0384 void bt_sock_link(struct bt_sock_list *l, struct sock *s);
0385 void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
0386 int  bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
0387              int flags);
0388 int  bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
0389                 size_t len, int flags);
0390 __poll_t bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait);
0391 int  bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
0392 int  bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
0393 int  bt_sock_wait_ready(struct sock *sk, unsigned int msg_flags);
0394 
0395 void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh);
0396 void bt_accept_unlink(struct sock *sk);
0397 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);
0398 
0399 /* Skb helpers */
0400 struct l2cap_ctrl {
0401     u8  sframe:1,
0402         poll:1,
0403         final:1,
0404         fcs:1,
0405         sar:2,
0406         super:2;
0407 
0408     u16 reqseq;
0409     u16 txseq;
0410     u8  retries;
0411     __le16  psm;
0412     bdaddr_t bdaddr;
0413     struct l2cap_chan *chan;
0414 };
0415 
0416 struct sco_ctrl {
0417     u8  pkt_status;
0418 };
0419 
0420 struct hci_dev;
0421 
0422 typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
0423 typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status,
0424                        u16 opcode, struct sk_buff *skb);
0425 
0426 #define HCI_REQ_START   BIT(0)
0427 #define HCI_REQ_SKB BIT(1)
0428 
0429 struct hci_ctrl {
0430     struct sock *sk;
0431     u16 opcode;
0432     u8 req_flags;
0433     u8 req_event;
0434     union {
0435         hci_req_complete_t req_complete;
0436         hci_req_complete_skb_t req_complete_skb;
0437     };
0438 };
0439 
0440 struct mgmt_ctrl {
0441     struct hci_dev *hdev;
0442     u16 opcode;
0443 };
0444 
0445 struct bt_skb_cb {
0446     u8 pkt_type;
0447     u8 force_active;
0448     u16 expect;
0449     u8 incoming:1;
0450     union {
0451         struct l2cap_ctrl l2cap;
0452         struct sco_ctrl sco;
0453         struct hci_ctrl hci;
0454         struct mgmt_ctrl mgmt;
0455     };
0456 };
0457 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
0458 
0459 #define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type
0460 #define hci_skb_expect(skb) bt_cb((skb))->expect
0461 #define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode
0462 #define hci_skb_event(skb) bt_cb((skb))->hci.req_event
0463 #define hci_skb_sk(skb) bt_cb((skb))->hci.sk
0464 
0465 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
0466 {
0467     struct sk_buff *skb;
0468 
0469     skb = alloc_skb(len + BT_SKB_RESERVE, how);
0470     if (skb)
0471         skb_reserve(skb, BT_SKB_RESERVE);
0472     return skb;
0473 }
0474 
0475 static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
0476                     unsigned long len, int nb, int *err)
0477 {
0478     struct sk_buff *skb;
0479 
0480     skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
0481     if (skb)
0482         skb_reserve(skb, BT_SKB_RESERVE);
0483 
0484     if (!skb && *err)
0485         return NULL;
0486 
0487     *err = sock_error(sk);
0488     if (*err)
0489         goto out;
0490 
0491     if (sk->sk_shutdown) {
0492         *err = -ECONNRESET;
0493         goto out;
0494     }
0495 
0496     return skb;
0497 
0498 out:
0499     kfree_skb(skb);
0500     return NULL;
0501 }
0502 
0503 /* Shall not be called with lock_sock held */
0504 static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk,
0505                          struct msghdr *msg,
0506                          size_t len, size_t mtu,
0507                          size_t headroom, size_t tailroom)
0508 {
0509     struct sk_buff *skb;
0510     size_t size = min_t(size_t, len, mtu);
0511     int err;
0512 
0513     skb = bt_skb_send_alloc(sk, size + headroom + tailroom,
0514                 msg->msg_flags & MSG_DONTWAIT, &err);
0515     if (!skb)
0516         return ERR_PTR(err);
0517 
0518     skb_reserve(skb, headroom);
0519     skb_tailroom_reserve(skb, mtu, tailroom);
0520 
0521     if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) {
0522         kfree_skb(skb);
0523         return ERR_PTR(-EFAULT);
0524     }
0525 
0526     skb->priority = sk->sk_priority;
0527 
0528     return skb;
0529 }
0530 
0531 /* Similar to bt_skb_sendmsg but can split the msg into multiple fragments
0532  * accourding to the MTU.
0533  */
0534 static inline struct sk_buff *bt_skb_sendmmsg(struct sock *sk,
0535                           struct msghdr *msg,
0536                           size_t len, size_t mtu,
0537                           size_t headroom, size_t tailroom)
0538 {
0539     struct sk_buff *skb, **frag;
0540 
0541     skb = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom);
0542     if (IS_ERR(skb))
0543         return skb;
0544 
0545     len -= skb->len;
0546     if (!len)
0547         return skb;
0548 
0549     /* Add remaining data over MTU as continuation fragments */
0550     frag = &skb_shinfo(skb)->frag_list;
0551     while (len) {
0552         struct sk_buff *tmp;
0553 
0554         tmp = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom);
0555         if (IS_ERR(tmp)) {
0556             return skb;
0557         }
0558 
0559         len -= tmp->len;
0560 
0561         *frag = tmp;
0562         frag = &(*frag)->next;
0563     }
0564 
0565     return skb;
0566 }
0567 
0568 int bt_to_errno(u16 code);
0569 __u8 bt_status(int err);
0570 
0571 void hci_sock_set_flag(struct sock *sk, int nr);
0572 void hci_sock_clear_flag(struct sock *sk, int nr);
0573 int hci_sock_test_flag(struct sock *sk, int nr);
0574 unsigned short hci_sock_get_channel(struct sock *sk);
0575 u32 hci_sock_get_cookie(struct sock *sk);
0576 
0577 int hci_sock_init(void);
0578 void hci_sock_cleanup(void);
0579 
0580 int bt_sysfs_init(void);
0581 void bt_sysfs_cleanup(void);
0582 
0583 int bt_procfs_init(struct net *net, const char *name,
0584            struct bt_sock_list *sk_list,
0585            int (*seq_show)(struct seq_file *, void *));
0586 void bt_procfs_cleanup(struct net *net, const char *name);
0587 
0588 extern struct dentry *bt_debugfs;
0589 
0590 int l2cap_init(void);
0591 void l2cap_exit(void);
0592 
0593 #if IS_ENABLED(CONFIG_BT_BREDR)
0594 int sco_init(void);
0595 void sco_exit(void);
0596 #else
0597 static inline int sco_init(void)
0598 {
0599     return 0;
0600 }
0601 
0602 static inline void sco_exit(void)
0603 {
0604 }
0605 #endif
0606 
0607 #if IS_ENABLED(CONFIG_BT_LE)
0608 int iso_init(void);
0609 int iso_exit(void);
0610 bool iso_enabled(void);
0611 #else
0612 static inline int iso_init(void)
0613 {
0614     return 0;
0615 }
0616 
0617 static inline int iso_exit(void)
0618 {
0619     return 0;
0620 }
0621 
0622 static inline bool iso_enabled(void)
0623 {
0624     return false;
0625 }
0626 #endif
0627 
0628 int mgmt_init(void);
0629 void mgmt_exit(void);
0630 
0631 void bt_sock_reclassify_lock(struct sock *sk, int proto);
0632 
0633 #endif /* __BLUETOOTH_H */