bluetooth.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2000-2001 Qualcomm Incorporated
  4. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License version 2 as
  7. published by the Free Software Foundation;
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  13. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  17. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  18. SOFTWARE IS DISCLAIMED.
  19. */
  20. #ifndef __BLUETOOTH_H
  21. #define __BLUETOOTH_H
  22. #include <linux/poll.h>
  23. #include <net/sock.h>
  24. #include <linux/seq_file.h>
  25. #define BT_SUBSYS_VERSION 2
  26. #define BT_SUBSYS_REVISION 22
  27. #ifndef AF_BLUETOOTH
  28. #define AF_BLUETOOTH 31
  29. #define PF_BLUETOOTH AF_BLUETOOTH
  30. #endif
  31. /* Bluetooth versions */
  32. #define BLUETOOTH_VER_1_1 1
  33. #define BLUETOOTH_VER_1_2 2
  34. #define BLUETOOTH_VER_2_0 3
  35. /* Reserv for core and drivers use */
  36. #define BT_SKB_RESERVE 8
  37. #define BTPROTO_L2CAP 0
  38. #define BTPROTO_HCI 1
  39. #define BTPROTO_SCO 2
  40. #define BTPROTO_RFCOMM 3
  41. #define BTPROTO_BNEP 4
  42. #define BTPROTO_CMTP 5
  43. #define BTPROTO_HIDP 6
  44. #define BTPROTO_AVDTP 7
  45. #define SOL_HCI 0
  46. #define SOL_L2CAP 6
  47. #define SOL_SCO 17
  48. #define SOL_RFCOMM 18
  49. #define BT_SECURITY 4
  50. struct bt_security {
  51. __u8 level;
  52. __u8 key_size;
  53. };
  54. #define BT_SECURITY_SDP 0
  55. #define BT_SECURITY_LOW 1
  56. #define BT_SECURITY_MEDIUM 2
  57. #define BT_SECURITY_HIGH 3
  58. #define BT_SECURITY_FIPS 4
  59. #define BT_DEFER_SETUP 7
  60. #define BT_FLUSHABLE 8
  61. #define BT_FLUSHABLE_OFF 0
  62. #define BT_FLUSHABLE_ON 1
  63. #define BT_POWER 9
  64. struct bt_power {
  65. __u8 force_active;
  66. };
  67. #define BT_POWER_FORCE_ACTIVE_OFF 0
  68. #define BT_POWER_FORCE_ACTIVE_ON 1
  69. #define BT_CHANNEL_POLICY 10
  70. /* BR/EDR only (default policy)
  71. * AMP controllers cannot be used.
  72. * Channel move requests from the remote device are denied.
  73. * If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
  74. */
  75. #define BT_CHANNEL_POLICY_BREDR_ONLY 0
  76. /* BR/EDR Preferred
  77. * Allow use of AMP controllers.
  78. * If the L2CAP channel is currently on AMP, move it to BR/EDR.
  79. * Channel move requests from the remote device are allowed.
  80. */
  81. #define BT_CHANNEL_POLICY_BREDR_PREFERRED 1
  82. /* AMP Preferred
  83. * Allow use of AMP controllers
  84. * If the L2CAP channel is currently on BR/EDR and AMP controller
  85. * resources are available, initiate a channel move to AMP.
  86. * Channel move requests from the remote device are allowed.
  87. * If the L2CAP socket has not been connected yet, try to create
  88. * and configure the channel directly on an AMP controller rather
  89. * than BR/EDR.
  90. */
  91. #define BT_CHANNEL_POLICY_AMP_PREFERRED 2
  92. #define BT_VOICE 11
  93. struct bt_voice {
  94. __u16 setting;
  95. };
  96. #define BT_VOICE_TRANSPARENT 0x0003
  97. #define BT_VOICE_CVSD_16BIT 0x0060
  98. #define BT_SNDMTU 12
  99. #define BT_RCVMTU 13
  100. __printf(1, 2)
  101. void bt_info(const char *fmt, ...);
  102. __printf(1, 2)
  103. void bt_warn(const char *fmt, ...);
  104. __printf(1, 2)
  105. void bt_err(const char *fmt, ...);
  106. __printf(1, 2)
  107. void bt_err_ratelimited(const char *fmt, ...);
  108. #define BT_INFO(fmt, ...) bt_info(fmt "\n", ##__VA_ARGS__)
  109. #define BT_WARN(fmt, ...) bt_warn(fmt "\n", ##__VA_ARGS__)
  110. #define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__)
  111. #define BT_DBG(fmt, ...) pr_debug(fmt "\n", ##__VA_ARGS__)
  112. #define BT_ERR_RATELIMITED(fmt, ...) bt_err_ratelimited(fmt "\n", ##__VA_ARGS__)
  113. #define bt_dev_info(hdev, fmt, ...) \
  114. BT_INFO("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
  115. #define bt_dev_warn(hdev, fmt, ...) \
  116. BT_WARN("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
  117. #define bt_dev_err(hdev, fmt, ...) \
  118. BT_ERR("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
  119. #define bt_dev_dbg(hdev, fmt, ...) \
  120. BT_DBG("%s: " fmt, (hdev)->name, ##__VA_ARGS__)
  121. /* Connection and socket states */
  122. enum {
  123. BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
  124. BT_OPEN,
  125. BT_BOUND,
  126. BT_LISTEN,
  127. BT_CONNECT,
  128. BT_CONNECT2,
  129. BT_CONFIG,
  130. BT_DISCONN,
  131. BT_CLOSED
  132. };
  133. /* If unused will be removed by compiler */
  134. static inline const char *state_to_string(int state)
  135. {
  136. switch (state) {
  137. case BT_CONNECTED:
  138. return "BT_CONNECTED";
  139. case BT_OPEN:
  140. return "BT_OPEN";
  141. case BT_BOUND:
  142. return "BT_BOUND";
  143. case BT_LISTEN:
  144. return "BT_LISTEN";
  145. case BT_CONNECT:
  146. return "BT_CONNECT";
  147. case BT_CONNECT2:
  148. return "BT_CONNECT2";
  149. case BT_CONFIG:
  150. return "BT_CONFIG";
  151. case BT_DISCONN:
  152. return "BT_DISCONN";
  153. case BT_CLOSED:
  154. return "BT_CLOSED";
  155. }
  156. return "invalid state";
  157. }
  158. /* BD Address */
  159. typedef struct {
  160. __u8 b[6];
  161. } __packed bdaddr_t;
  162. /* BD Address type */
  163. #define BDADDR_BREDR 0x00
  164. #define BDADDR_LE_PUBLIC 0x01
  165. #define BDADDR_LE_RANDOM 0x02
  166. static inline bool bdaddr_type_is_valid(u8 type)
  167. {
  168. switch (type) {
  169. case BDADDR_BREDR:
  170. case BDADDR_LE_PUBLIC:
  171. case BDADDR_LE_RANDOM:
  172. return true;
  173. }
  174. return false;
  175. }
  176. static inline bool bdaddr_type_is_le(u8 type)
  177. {
  178. switch (type) {
  179. case BDADDR_LE_PUBLIC:
  180. case BDADDR_LE_RANDOM:
  181. return true;
  182. }
  183. return false;
  184. }
  185. #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
  186. #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
  187. /* Copy, swap, convert BD Address */
  188. static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
  189. {
  190. return memcmp(ba1, ba2, sizeof(bdaddr_t));
  191. }
  192. static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
  193. {
  194. memcpy(dst, src, sizeof(bdaddr_t));
  195. }
  196. void baswap(bdaddr_t *dst, const bdaddr_t *src);
  197. /* Common socket structures and functions */
  198. #define bt_sk(__sk) ((struct bt_sock *) __sk)
  199. struct bt_sock {
  200. struct sock sk;
  201. struct list_head accept_q;
  202. struct sock *parent;
  203. unsigned long flags;
  204. void (*skb_msg_name)(struct sk_buff *, void *, int *);
  205. };
  206. enum {
  207. BT_SK_DEFER_SETUP,
  208. BT_SK_SUSPEND,
  209. };
  210. struct bt_sock_list {
  211. struct hlist_head head;
  212. rwlock_t lock;
  213. #ifdef CONFIG_PROC_FS
  214. int (* custom_seq_show)(struct seq_file *, void *);
  215. #endif
  216. };
  217. int bt_sock_register(int proto, const struct net_proto_family *ops);
  218. void bt_sock_unregister(int proto);
  219. void bt_sock_link(struct bt_sock_list *l, struct sock *s);
  220. void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
  221. int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  222. int flags);
  223. int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
  224. size_t len, int flags);
  225. uint bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait);
  226. int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
  227. int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
  228. int bt_sock_wait_ready(struct sock *sk, unsigned long flags);
  229. void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh);
  230. void bt_accept_unlink(struct sock *sk);
  231. struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);
  232. /* Skb helpers */
  233. struct l2cap_ctrl {
  234. u8 sframe:1,
  235. poll:1,
  236. final:1,
  237. fcs:1,
  238. sar:2,
  239. super:2;
  240. u16 reqseq;
  241. u16 txseq;
  242. u8 retries;
  243. __le16 psm;
  244. bdaddr_t bdaddr;
  245. struct l2cap_chan *chan;
  246. };
  247. struct hci_dev;
  248. typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
  249. typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status,
  250. u16 opcode, struct sk_buff *skb);
  251. #define HCI_REQ_START BIT(0)
  252. #define HCI_REQ_SKB BIT(1)
  253. struct hci_ctrl {
  254. u16 opcode;
  255. u8 req_flags;
  256. u8 req_event;
  257. union {
  258. hci_req_complete_t req_complete;
  259. hci_req_complete_skb_t req_complete_skb;
  260. };
  261. };
  262. struct bt_skb_cb {
  263. u8 pkt_type;
  264. u8 force_active;
  265. u16 expect;
  266. u8 incoming:1;
  267. union {
  268. struct l2cap_ctrl l2cap;
  269. struct hci_ctrl hci;
  270. };
  271. };
  272. #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
  273. #define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type
  274. #define hci_skb_expect(skb) bt_cb((skb))->expect
  275. #define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode
  276. static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
  277. {
  278. struct sk_buff *skb;
  279. skb = alloc_skb(len + BT_SKB_RESERVE, how);
  280. if (skb)
  281. skb_reserve(skb, BT_SKB_RESERVE);
  282. return skb;
  283. }
  284. static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
  285. unsigned long len, int nb, int *err)
  286. {
  287. struct sk_buff *skb;
  288. skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
  289. if (skb)
  290. skb_reserve(skb, BT_SKB_RESERVE);
  291. if (!skb && *err)
  292. return NULL;
  293. *err = sock_error(sk);
  294. if (*err)
  295. goto out;
  296. if (sk->sk_shutdown) {
  297. *err = -ECONNRESET;
  298. goto out;
  299. }
  300. return skb;
  301. out:
  302. kfree_skb(skb);
  303. return NULL;
  304. }
  305. int bt_to_errno(u16 code);
  306. void hci_sock_set_flag(struct sock *sk, int nr);
  307. void hci_sock_clear_flag(struct sock *sk, int nr);
  308. int hci_sock_test_flag(struct sock *sk, int nr);
  309. unsigned short hci_sock_get_channel(struct sock *sk);
  310. u32 hci_sock_get_cookie(struct sock *sk);
  311. int hci_sock_init(void);
  312. void hci_sock_cleanup(void);
  313. int bt_sysfs_init(void);
  314. void bt_sysfs_cleanup(void);
  315. int bt_procfs_init(struct net *net, const char *name,
  316. struct bt_sock_list *sk_list,
  317. int (*seq_show)(struct seq_file *, void *));
  318. void bt_procfs_cleanup(struct net *net, const char *name);
  319. extern struct dentry *bt_debugfs;
  320. int l2cap_init(void);
  321. void l2cap_exit(void);
  322. #if IS_ENABLED(CONFIG_BT_BREDR)
  323. int sco_init(void);
  324. void sco_exit(void);
  325. #else
  326. static inline int sco_init(void)
  327. {
  328. return 0;
  329. }
  330. static inline void sco_exit(void)
  331. {
  332. }
  333. #endif
  334. int mgmt_init(void);
  335. void mgmt_exit(void);
  336. void bt_sock_reclassify_lock(struct sock *sk, int proto);
  337. #endif /* __BLUETOOTH_H */