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 321da177e4SLinus Torvalds #ifndef AF_BLUETOOTH 331da177e4SLinus Torvalds #define AF_BLUETOOTH 31 341da177e4SLinus Torvalds #define PF_BLUETOOTH AF_BLUETOOTH 351da177e4SLinus Torvalds #endif 361da177e4SLinus Torvalds 37d095c1ebSAndrei Emeltchenko /* Bluetooth versions */ 38d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_1_1 1 39d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_1_2 2 40d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_2_0 3 41d095c1ebSAndrei Emeltchenko 421da177e4SLinus Torvalds /* Reserv for core and drivers use */ 431da177e4SLinus Torvalds #define BT_SKB_RESERVE 8 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #define BTPROTO_L2CAP 0 461da177e4SLinus Torvalds #define BTPROTO_HCI 1 471da177e4SLinus Torvalds #define BTPROTO_SCO 2 481da177e4SLinus Torvalds #define BTPROTO_RFCOMM 3 491da177e4SLinus Torvalds #define BTPROTO_BNEP 4 501da177e4SLinus Torvalds #define BTPROTO_CMTP 5 511da177e4SLinus Torvalds #define BTPROTO_HIDP 6 521da177e4SLinus Torvalds #define BTPROTO_AVDTP 7 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #define SOL_HCI 0 551da177e4SLinus Torvalds #define SOL_L2CAP 6 561da177e4SLinus Torvalds #define SOL_SCO 17 571da177e4SLinus Torvalds #define SOL_RFCOMM 18 581da177e4SLinus Torvalds 598c1b2355SMarcel Holtmann #define BT_SECURITY 4 608c1b2355SMarcel Holtmann struct bt_security { 618c1b2355SMarcel Holtmann __u8 level; 628f360119SVinicius Costa Gomes __u8 key_size; 638c1b2355SMarcel Holtmann }; 648c1b2355SMarcel Holtmann #define BT_SECURITY_SDP 0 658c1b2355SMarcel Holtmann #define BT_SECURITY_LOW 1 668c1b2355SMarcel Holtmann #define BT_SECURITY_MEDIUM 2 678c1b2355SMarcel Holtmann #define BT_SECURITY_HIGH 3 688c1b2355SMarcel Holtmann 69c4f912e1SMarcel Holtmann #define BT_DEFER_SETUP 7 70c4f912e1SMarcel Holtmann 71e702112fSAndrei Emeltchenko #define BT_FLUSHABLE 8 72e702112fSAndrei Emeltchenko 73e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_OFF 0 74e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_ON 1 75e702112fSAndrei Emeltchenko 7614b12d0bSJaikumar Ganesh #define BT_POWER 9 7714b12d0bSJaikumar Ganesh struct bt_power { 7814b12d0bSJaikumar Ganesh __u8 force_active; 7914b12d0bSJaikumar Ganesh }; 8014b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_OFF 0 8114b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_ON 1 8214b12d0bSJaikumar Ganesh 83c14968b0SMat Martineau #define BT_CHANNEL_POLICY 10 84c14968b0SMat Martineau 85c14968b0SMat Martineau /* BR/EDR only (default policy) 86c14968b0SMat Martineau * AMP controllers cannot be used. 87c14968b0SMat Martineau * Channel move requests from the remote device are denied. 88c14968b0SMat Martineau * If the L2CAP channel is currently using AMP, move the channel to BR/EDR. 89c14968b0SMat Martineau */ 90c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_ONLY 0 91c14968b0SMat Martineau 92c14968b0SMat Martineau /* BR/EDR Preferred 93c14968b0SMat Martineau * Allow use of AMP controllers. 94c14968b0SMat Martineau * If the L2CAP channel is currently on AMP, move it to BR/EDR. 95c14968b0SMat Martineau * Channel move requests from the remote device are allowed. 96c14968b0SMat Martineau */ 97c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_PREFERRED 1 98c14968b0SMat Martineau 99c14968b0SMat Martineau /* AMP Preferred 100c14968b0SMat Martineau * Allow use of AMP controllers 101c14968b0SMat Martineau * If the L2CAP channel is currently on BR/EDR and AMP controller 102c14968b0SMat Martineau * resources are available, initiate a channel move to AMP. 103c14968b0SMat Martineau * Channel move requests from the remote device are allowed. 104c14968b0SMat Martineau * If the L2CAP socket has not been connected yet, try to create 105c14968b0SMat Martineau * and configure the channel directly on an AMP controller rather 106c14968b0SMat Martineau * than BR/EDR. 107c14968b0SMat Martineau */ 108c14968b0SMat Martineau #define BT_CHANNEL_POLICY_AMP_PREFERRED 2 109c14968b0SMat Martineau 110ad10b1a4SFrédéric Dalleau #define BT_VOICE 11 111ad10b1a4SFrédéric Dalleau struct bt_voice { 112ad10b1a4SFrédéric Dalleau __u16 setting; 113ad10b1a4SFrédéric Dalleau }; 114ad10b1a4SFrédéric Dalleau 115ad10b1a4SFrédéric Dalleau #define BT_VOICE_TRANSPARENT 0x0003 116ad10b1a4SFrédéric Dalleau #define BT_VOICE_CVSD_16BIT 0x0060 117ad10b1a4SFrédéric Dalleau 1183ed7003eSJoe Perches __printf(1, 2) 1193ed7003eSJoe Perches int bt_info(const char *fmt, ...); 1203ed7003eSJoe Perches __printf(1, 2) 1213ed7003eSJoe Perches int bt_err(const char *fmt, ...); 122e1447d8dSJoe Perches 1233ed7003eSJoe Perches #define BT_INFO(fmt, ...) bt_info(fmt "\n", ##__VA_ARGS__) 1243ed7003eSJoe Perches #define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__) 1253ed7003eSJoe Perches #define BT_DBG(fmt, ...) pr_debug(fmt "\n", ##__VA_ARGS__) 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* Connection and socket states */ 1281da177e4SLinus Torvalds enum { 1291da177e4SLinus Torvalds BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ 1301da177e4SLinus Torvalds BT_OPEN, 1311da177e4SLinus Torvalds BT_BOUND, 1321da177e4SLinus Torvalds BT_LISTEN, 1331da177e4SLinus Torvalds BT_CONNECT, 1341da177e4SLinus Torvalds BT_CONNECT2, 1351da177e4SLinus Torvalds BT_CONFIG, 1361da177e4SLinus Torvalds BT_DISCONN, 1371da177e4SLinus Torvalds BT_CLOSED 1381da177e4SLinus Torvalds }; 1391da177e4SLinus Torvalds 14020d1803aSAndrei Emeltchenko /* If unused will be removed by compiler */ 14120d1803aSAndrei Emeltchenko static inline const char *state_to_string(int state) 14220d1803aSAndrei Emeltchenko { 14320d1803aSAndrei Emeltchenko switch (state) { 14420d1803aSAndrei Emeltchenko case BT_CONNECTED: 14520d1803aSAndrei Emeltchenko return "BT_CONNECTED"; 14620d1803aSAndrei Emeltchenko case BT_OPEN: 14720d1803aSAndrei Emeltchenko return "BT_OPEN"; 14820d1803aSAndrei Emeltchenko case BT_BOUND: 14920d1803aSAndrei Emeltchenko return "BT_BOUND"; 15020d1803aSAndrei Emeltchenko case BT_LISTEN: 15120d1803aSAndrei Emeltchenko return "BT_LISTEN"; 15220d1803aSAndrei Emeltchenko case BT_CONNECT: 15320d1803aSAndrei Emeltchenko return "BT_CONNECT"; 15420d1803aSAndrei Emeltchenko case BT_CONNECT2: 15520d1803aSAndrei Emeltchenko return "BT_CONNECT2"; 15620d1803aSAndrei Emeltchenko case BT_CONFIG: 15720d1803aSAndrei Emeltchenko return "BT_CONFIG"; 15820d1803aSAndrei Emeltchenko case BT_DISCONN: 15920d1803aSAndrei Emeltchenko return "BT_DISCONN"; 16020d1803aSAndrei Emeltchenko case BT_CLOSED: 16120d1803aSAndrei Emeltchenko return "BT_CLOSED"; 16220d1803aSAndrei Emeltchenko } 16320d1803aSAndrei Emeltchenko 16420d1803aSAndrei Emeltchenko return "invalid state"; 16520d1803aSAndrei Emeltchenko } 16620d1803aSAndrei Emeltchenko 1671da177e4SLinus Torvalds /* BD Address */ 1681da177e4SLinus Torvalds typedef struct { 1691da177e4SLinus Torvalds __u8 b[6]; 17066c853ccSGustavo F. Padovan } __packed bdaddr_t; 1711da177e4SLinus Torvalds 172591f47f3SAndre Guedes /* BD Address type */ 173591f47f3SAndre Guedes #define BDADDR_BREDR 0x00 174591f47f3SAndre Guedes #define BDADDR_LE_PUBLIC 0x01 175591f47f3SAndre Guedes #define BDADDR_LE_RANDOM 0x02 176591f47f3SAndre Guedes 177679efe2bSJohan Hedberg static inline bool bdaddr_type_is_valid(__u8 type) 178679efe2bSJohan Hedberg { 179679efe2bSJohan Hedberg switch (type) { 180679efe2bSJohan Hedberg case BDADDR_BREDR: 181679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC: 182679efe2bSJohan Hedberg case BDADDR_LE_RANDOM: 183679efe2bSJohan Hedberg return true; 184679efe2bSJohan Hedberg } 185679efe2bSJohan Hedberg 186679efe2bSJohan Hedberg return false; 187679efe2bSJohan Hedberg } 188679efe2bSJohan Hedberg 189679efe2bSJohan Hedberg static inline bool bdaddr_type_is_le(__u8 type) 190679efe2bSJohan Hedberg { 191679efe2bSJohan Hedberg switch (type) { 192679efe2bSJohan Hedberg case BDADDR_LE_PUBLIC: 193679efe2bSJohan Hedberg case BDADDR_LE_RANDOM: 194679efe2bSJohan Hedberg return true; 195679efe2bSJohan Hedberg } 196679efe2bSJohan Hedberg 197679efe2bSJohan Hedberg return false; 198679efe2bSJohan Hedberg } 199679efe2bSJohan Hedberg 2001da177e4SLinus Torvalds #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}}) 201a59ac2f7SMarcel Holtmann #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}) 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* Copy, swap, convert BD Address */ 204f53c20e9SDavid Herrmann static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2) 2051da177e4SLinus Torvalds { 2061da177e4SLinus Torvalds return memcmp(ba1, ba2, sizeof(bdaddr_t)); 2071da177e4SLinus Torvalds } 208f53c20e9SDavid Herrmann static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds memcpy(dst, src, sizeof(bdaddr_t)); 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds void baswap(bdaddr_t *dst, bdaddr_t *src); 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /* Common socket structures and functions */ 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds #define bt_sk(__sk) ((struct bt_sock *) __sk) 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds struct bt_sock { 2201da177e4SLinus Torvalds struct sock sk; 2211da177e4SLinus Torvalds struct list_head accept_q; 2221da177e4SLinus Torvalds struct sock *parent; 223c5daa683SGustavo Padovan unsigned long flags; 224*d9763698SMarcel Holtmann void (*skb_msg_name)(struct sk_buff *, void *, int *); 225c5daa683SGustavo Padovan }; 226c5daa683SGustavo Padovan 227c5daa683SGustavo Padovan enum { 228c5daa683SGustavo Padovan BT_SK_DEFER_SETUP, 229c5daa683SGustavo Padovan BT_SK_SUSPEND, 2301da177e4SLinus Torvalds }; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds struct bt_sock_list { 2331da177e4SLinus Torvalds struct hlist_head head; 2341da177e4SLinus Torvalds rwlock_t lock; 235256a06c8SMasatake YAMATO #ifdef CONFIG_PROC_FS 236256a06c8SMasatake YAMATO int (* custom_seq_show)(struct seq_file *, void *); 237256a06c8SMasatake YAMATO #endif 2381da177e4SLinus Torvalds }; 2391da177e4SLinus Torvalds 240ec1b4cf7SStephen Hemminger int bt_sock_register(int proto, const struct net_proto_family *ops); 241be9f97f0SDavid Herrmann void bt_sock_unregister(int proto); 2421da177e4SLinus Torvalds void bt_sock_link(struct bt_sock_list *l, struct sock *s); 2431da177e4SLinus Torvalds void bt_sock_unlink(struct bt_sock_list *l, struct sock *s); 2445a9d0a3fSWaldemar Rymarkiewicz int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 2455a9d0a3fSWaldemar Rymarkiewicz struct msghdr *msg, size_t len, int flags); 246796c86eeSMat Martineau int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock, 247796c86eeSMat Martineau struct msghdr *msg, size_t len, int flags); 2481da177e4SLinus Torvalds uint bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait); 2493241ad82SMarcel Holtmann int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); 2501da177e4SLinus Torvalds int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo); 251e793dcf0SJohan Hedberg int bt_sock_wait_ready(struct sock *sk, unsigned long flags); 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds void bt_accept_enqueue(struct sock *parent, struct sock *sk); 2541da177e4SLinus Torvalds void bt_accept_unlink(struct sock *sk); 2551da177e4SLinus Torvalds struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock); 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds /* Skb helpers */ 25800e3112cSMat Martineau struct l2cap_ctrl { 25900e3112cSMat Martineau unsigned int sframe:1, 26000e3112cSMat Martineau poll:1, 26100e3112cSMat Martineau final:1, 26200e3112cSMat Martineau fcs:1, 26300e3112cSMat Martineau sar:2, 26400e3112cSMat Martineau super:2; 26500e3112cSMat Martineau __u16 reqseq; 26600e3112cSMat Martineau __u16 txseq; 26700e3112cSMat Martineau __u8 retries; 26800e3112cSMat Martineau }; 26900e3112cSMat Martineau 2703119ae95SJohan Hedberg struct hci_dev; 2713119ae95SJohan Hedberg 2723119ae95SJohan Hedberg typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status); 2733119ae95SJohan Hedberg 2743119ae95SJohan Hedberg struct hci_req_ctrl { 2753119ae95SJohan Hedberg bool start; 27602350a72SJohan Hedberg u8 event; 2773119ae95SJohan Hedberg hci_req_complete_t complete; 2783119ae95SJohan Hedberg }; 2793119ae95SJohan Hedberg 2801da177e4SLinus Torvalds struct bt_skb_cb { 2810d48d939SMarcel Holtmann __u8 pkt_type; 2820d48d939SMarcel Holtmann __u8 incoming; 28333e882a5SSuraj Sumangala __u16 expect; 28414b12d0bSJaikumar Ganesh __u8 force_active; 28500e3112cSMat Martineau struct l2cap_ctrl control; 2863119ae95SJohan Hedberg struct hci_req_ctrl req; 2871da177e4SLinus Torvalds }; 2881c2acffbSGustavo F. Padovan #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb)) 2891da177e4SLinus Torvalds 290dd0fc66fSAl Viro static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds struct sk_buff *skb; 2931da177e4SLinus Torvalds 294c3c7ea65SGustavo Padovan skb = alloc_skb(len + BT_SKB_RESERVE, how); 295c3c7ea65SGustavo Padovan if (skb) { 2961da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE); 2971da177e4SLinus Torvalds bt_cb(skb)->incoming = 0; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds return skb; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3025a9d0a3fSWaldemar Rymarkiewicz static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk, 3035a9d0a3fSWaldemar Rymarkiewicz unsigned long len, int nb, int *err) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds struct sk_buff *skb; 3061da177e4SLinus Torvalds 307c3c7ea65SGustavo Padovan skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err); 308c3c7ea65SGustavo Padovan if (skb) { 3091da177e4SLinus Torvalds skb_reserve(skb, BT_SKB_RESERVE); 3101da177e4SLinus Torvalds bt_cb(skb)->incoming = 0; 3111da177e4SLinus Torvalds } 312e454c844SGustavo F. Padovan 313e454c844SGustavo F. Padovan if (!skb && *err) 314e454c844SGustavo F. Padovan return NULL; 315e454c844SGustavo F. Padovan 316e454c844SGustavo F. Padovan *err = sock_error(sk); 317e454c844SGustavo F. Padovan if (*err) 318e454c844SGustavo F. Padovan goto out; 319e454c844SGustavo F. Padovan 320e454c844SGustavo F. Padovan if (sk->sk_shutdown) { 321e454c844SGustavo F. Padovan *err = -ECONNRESET; 322e454c844SGustavo F. Padovan goto out; 323e454c844SGustavo F. Padovan } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds return skb; 326e454c844SGustavo F. Padovan 327e454c844SGustavo F. Padovan out: 328e454c844SGustavo F. Padovan kfree_skb(skb); 329e454c844SGustavo F. Padovan return NULL; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 332e175072fSJoe Perches int bt_to_errno(__u16 code); 3331da177e4SLinus Torvalds 3346516455dSMarcel Holtmann extern int hci_sock_init(void); 33504005dd9STobias Klauser extern void hci_sock_cleanup(void); 3366516455dSMarcel Holtmann 3376516455dSMarcel Holtmann extern int bt_sysfs_init(void); 3386516455dSMarcel Holtmann extern void bt_sysfs_cleanup(void); 3396516455dSMarcel Holtmann 340b0316615SAl Viro extern int bt_procfs_init(struct net *net, const char *name, 341256a06c8SMasatake YAMATO struct bt_sock_list* sk_list, 342256a06c8SMasatake YAMATO int (* seq_show)(struct seq_file *, void *)); 343256a06c8SMasatake YAMATO extern void bt_procfs_cleanup(struct net *net, const char *name); 344256a06c8SMasatake YAMATO 345aef7d97cSMarcel Holtmann extern struct dentry *bt_debugfs; 346be9d1227SMarcel Holtmann 34764274518SGustavo F. Padovan int l2cap_init(void); 34864274518SGustavo F. Padovan void l2cap_exit(void); 34964274518SGustavo F. Padovan 35064274518SGustavo F. Padovan int sco_init(void); 35164274518SGustavo F. Padovan void sco_exit(void); 35264274518SGustavo F. Padovan 353d22015aaSOctavian Purdila void bt_sock_reclassify_lock(struct sock *sk, int proto); 354d22015aaSOctavian Purdila 3551da177e4SLinus Torvalds #endif /* __BLUETOOTH_H */ 356