rfcomm.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. RFCOMM implementation for Linux Bluetooth stack (BlueZ)
  3. Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
  4. Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
  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 __RFCOMM_H
  21. #define __RFCOMM_H
  22. #define RFCOMM_PSM 3
  23. #define RFCOMM_CONN_TIMEOUT (HZ * 30)
  24. #define RFCOMM_DISC_TIMEOUT (HZ * 20)
  25. #define RFCOMM_AUTH_TIMEOUT (HZ * 25)
  26. #define RFCOMM_IDLE_TIMEOUT (HZ * 2)
  27. #define RFCOMM_DEFAULT_MTU 127
  28. #define RFCOMM_DEFAULT_CREDITS 7
  29. #define RFCOMM_MAX_L2CAP_MTU 1013
  30. #define RFCOMM_MAX_CREDITS 40
  31. #define RFCOMM_SKB_HEAD_RESERVE 8
  32. #define RFCOMM_SKB_TAIL_RESERVE 2
  33. #define RFCOMM_SKB_RESERVE (RFCOMM_SKB_HEAD_RESERVE + RFCOMM_SKB_TAIL_RESERVE)
  34. #define RFCOMM_SABM 0x2f
  35. #define RFCOMM_DISC 0x43
  36. #define RFCOMM_UA 0x63
  37. #define RFCOMM_DM 0x0f
  38. #define RFCOMM_UIH 0xef
  39. #define RFCOMM_TEST 0x08
  40. #define RFCOMM_FCON 0x28
  41. #define RFCOMM_FCOFF 0x18
  42. #define RFCOMM_MSC 0x38
  43. #define RFCOMM_RPN 0x24
  44. #define RFCOMM_RLS 0x14
  45. #define RFCOMM_PN 0x20
  46. #define RFCOMM_NSC 0x04
  47. #define RFCOMM_V24_FC 0x02
  48. #define RFCOMM_V24_RTC 0x04
  49. #define RFCOMM_V24_RTR 0x08
  50. #define RFCOMM_V24_IC 0x40
  51. #define RFCOMM_V24_DV 0x80
  52. #define RFCOMM_RPN_BR_2400 0x0
  53. #define RFCOMM_RPN_BR_4800 0x1
  54. #define RFCOMM_RPN_BR_7200 0x2
  55. #define RFCOMM_RPN_BR_9600 0x3
  56. #define RFCOMM_RPN_BR_19200 0x4
  57. #define RFCOMM_RPN_BR_38400 0x5
  58. #define RFCOMM_RPN_BR_57600 0x6
  59. #define RFCOMM_RPN_BR_115200 0x7
  60. #define RFCOMM_RPN_BR_230400 0x8
  61. #define RFCOMM_RPN_DATA_5 0x0
  62. #define RFCOMM_RPN_DATA_6 0x1
  63. #define RFCOMM_RPN_DATA_7 0x2
  64. #define RFCOMM_RPN_DATA_8 0x3
  65. #define RFCOMM_RPN_STOP_1 0
  66. #define RFCOMM_RPN_STOP_15 1
  67. #define RFCOMM_RPN_PARITY_NONE 0x0
  68. #define RFCOMM_RPN_PARITY_ODD 0x1
  69. #define RFCOMM_RPN_PARITY_EVEN 0x3
  70. #define RFCOMM_RPN_PARITY_MARK 0x5
  71. #define RFCOMM_RPN_PARITY_SPACE 0x7
  72. #define RFCOMM_RPN_FLOW_NONE 0x00
  73. #define RFCOMM_RPN_XON_CHAR 0x11
  74. #define RFCOMM_RPN_XOFF_CHAR 0x13
  75. #define RFCOMM_RPN_PM_BITRATE 0x0001
  76. #define RFCOMM_RPN_PM_DATA 0x0002
  77. #define RFCOMM_RPN_PM_STOP 0x0004
  78. #define RFCOMM_RPN_PM_PARITY 0x0008
  79. #define RFCOMM_RPN_PM_PARITY_TYPE 0x0010
  80. #define RFCOMM_RPN_PM_XON 0x0020
  81. #define RFCOMM_RPN_PM_XOFF 0x0040
  82. #define RFCOMM_RPN_PM_FLOW 0x3F00
  83. #define RFCOMM_RPN_PM_ALL 0x3F7F
  84. struct rfcomm_hdr {
  85. u8 addr;
  86. u8 ctrl;
  87. u8 len; /* Actual size can be 2 bytes */
  88. } __packed;
  89. struct rfcomm_cmd {
  90. u8 addr;
  91. u8 ctrl;
  92. u8 len;
  93. u8 fcs;
  94. } __packed;
  95. struct rfcomm_mcc {
  96. u8 type;
  97. u8 len;
  98. } __packed;
  99. struct rfcomm_pn {
  100. u8 dlci;
  101. u8 flow_ctrl;
  102. u8 priority;
  103. u8 ack_timer;
  104. __le16 mtu;
  105. u8 max_retrans;
  106. u8 credits;
  107. } __packed;
  108. struct rfcomm_rpn {
  109. u8 dlci;
  110. u8 bit_rate;
  111. u8 line_settings;
  112. u8 flow_ctrl;
  113. u8 xon_char;
  114. u8 xoff_char;
  115. __le16 param_mask;
  116. } __packed;
  117. struct rfcomm_rls {
  118. u8 dlci;
  119. u8 status;
  120. } __packed;
  121. struct rfcomm_msc {
  122. u8 dlci;
  123. u8 v24_sig;
  124. } __packed;
  125. /* ---- Core structures, flags etc ---- */
  126. struct rfcomm_session {
  127. struct list_head list;
  128. struct socket *sock;
  129. struct timer_list timer;
  130. unsigned long state;
  131. unsigned long flags;
  132. atomic_t refcnt;
  133. int initiator;
  134. /* Default DLC parameters */
  135. int cfc;
  136. uint mtu;
  137. struct list_head dlcs;
  138. };
  139. struct rfcomm_dlc {
  140. struct list_head list;
  141. struct rfcomm_session *session;
  142. struct sk_buff_head tx_queue;
  143. struct timer_list timer;
  144. spinlock_t lock;
  145. unsigned long state;
  146. unsigned long flags;
  147. atomic_t refcnt;
  148. u8 dlci;
  149. u8 addr;
  150. u8 priority;
  151. u8 v24_sig;
  152. u8 remote_v24_sig;
  153. u8 mscex;
  154. u8 out;
  155. u8 sec_level;
  156. u8 role_switch;
  157. u32 defer_setup;
  158. uint mtu;
  159. uint cfc;
  160. uint rx_credits;
  161. uint tx_credits;
  162. void *owner;
  163. void (*data_ready)(struct rfcomm_dlc *d, struct sk_buff *skb);
  164. void (*state_change)(struct rfcomm_dlc *d, int err);
  165. void (*modem_status)(struct rfcomm_dlc *d, u8 v24_sig);
  166. };
  167. /* DLC and session flags */
  168. #define RFCOMM_RX_THROTTLED 0
  169. #define RFCOMM_TX_THROTTLED 1
  170. #define RFCOMM_TIMED_OUT 2
  171. #define RFCOMM_MSC_PENDING 3
  172. #define RFCOMM_SEC_PENDING 4
  173. #define RFCOMM_AUTH_PENDING 5
  174. #define RFCOMM_AUTH_ACCEPT 6
  175. #define RFCOMM_AUTH_REJECT 7
  176. #define RFCOMM_DEFER_SETUP 8
  177. /* Scheduling flags and events */
  178. #define RFCOMM_SCHED_WAKEUP 31
  179. /* MSC exchange flags */
  180. #define RFCOMM_MSCEX_TX 1
  181. #define RFCOMM_MSCEX_RX 2
  182. #define RFCOMM_MSCEX_OK (RFCOMM_MSCEX_TX + RFCOMM_MSCEX_RX)
  183. /* CFC states */
  184. #define RFCOMM_CFC_UNKNOWN -1
  185. #define RFCOMM_CFC_DISABLED 0
  186. #define RFCOMM_CFC_ENABLED RFCOMM_MAX_CREDITS
  187. /* ---- RFCOMM SEND RPN ---- */
  188. int rfcomm_send_rpn(struct rfcomm_session *s, int cr, u8 dlci,
  189. u8 bit_rate, u8 data_bits, u8 stop_bits,
  190. u8 parity, u8 flow_ctrl_settings,
  191. u8 xon_char, u8 xoff_char, u16 param_mask);
  192. /* ---- RFCOMM DLCs (channels) ---- */
  193. struct rfcomm_dlc *rfcomm_dlc_alloc(gfp_t prio);
  194. void rfcomm_dlc_free(struct rfcomm_dlc *d);
  195. int rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst,
  196. u8 channel);
  197. int rfcomm_dlc_close(struct rfcomm_dlc *d, int reason);
  198. int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb);
  199. int rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig);
  200. int rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig);
  201. void rfcomm_dlc_accept(struct rfcomm_dlc *d);
  202. #define rfcomm_dlc_lock(d) spin_lock(&d->lock)
  203. #define rfcomm_dlc_unlock(d) spin_unlock(&d->lock)
  204. static inline void rfcomm_dlc_hold(struct rfcomm_dlc *d)
  205. {
  206. atomic_inc(&d->refcnt);
  207. }
  208. static inline void rfcomm_dlc_put(struct rfcomm_dlc *d)
  209. {
  210. if (atomic_dec_and_test(&d->refcnt))
  211. rfcomm_dlc_free(d);
  212. }
  213. extern void __rfcomm_dlc_throttle(struct rfcomm_dlc *d);
  214. extern void __rfcomm_dlc_unthrottle(struct rfcomm_dlc *d);
  215. static inline void rfcomm_dlc_throttle(struct rfcomm_dlc *d)
  216. {
  217. if (!test_and_set_bit(RFCOMM_RX_THROTTLED, &d->flags))
  218. __rfcomm_dlc_throttle(d);
  219. }
  220. static inline void rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
  221. {
  222. if (test_and_clear_bit(RFCOMM_RX_THROTTLED, &d->flags))
  223. __rfcomm_dlc_unthrottle(d);
  224. }
  225. /* ---- RFCOMM sessions ---- */
  226. void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src,
  227. bdaddr_t *dst);
  228. static inline void rfcomm_session_hold(struct rfcomm_session *s)
  229. {
  230. atomic_inc(&s->refcnt);
  231. }
  232. /* ---- RFCOMM sockets ---- */
  233. struct sockaddr_rc {
  234. sa_family_t rc_family;
  235. bdaddr_t rc_bdaddr;
  236. u8 rc_channel;
  237. };
  238. #define RFCOMM_CONNINFO 0x02
  239. struct rfcomm_conninfo {
  240. __u16 hci_handle;
  241. __u8 dev_class[3];
  242. };
  243. #define RFCOMM_LM 0x03
  244. #define RFCOMM_LM_MASTER 0x0001
  245. #define RFCOMM_LM_AUTH 0x0002
  246. #define RFCOMM_LM_ENCRYPT 0x0004
  247. #define RFCOMM_LM_TRUSTED 0x0008
  248. #define RFCOMM_LM_RELIABLE 0x0010
  249. #define RFCOMM_LM_SECURE 0x0020
  250. #define rfcomm_pi(sk) ((struct rfcomm_pinfo *) sk)
  251. struct rfcomm_pinfo {
  252. struct bt_sock bt;
  253. struct rfcomm_dlc *dlc;
  254. u8 channel;
  255. u8 sec_level;
  256. u8 role_switch;
  257. };
  258. int rfcomm_init_sockets(void);
  259. void rfcomm_cleanup_sockets(void);
  260. int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel,
  261. struct rfcomm_dlc **d);
  262. /* ---- RFCOMM TTY ---- */
  263. #define RFCOMM_MAX_DEV 256
  264. #define RFCOMMCREATEDEV _IOW('R', 200, int)
  265. #define RFCOMMRELEASEDEV _IOW('R', 201, int)
  266. #define RFCOMMGETDEVLIST _IOR('R', 210, int)
  267. #define RFCOMMGETDEVINFO _IOR('R', 211, int)
  268. #define RFCOMMSTEALDLC _IOW('R', 220, int)
  269. #define RFCOMM_REUSE_DLC 0
  270. #define RFCOMM_RELEASE_ONHUP 1
  271. #define RFCOMM_HANGUP_NOW 2
  272. #define RFCOMM_TTY_ATTACHED 3
  273. #define RFCOMM_TTY_RELEASED 4
  274. struct rfcomm_dev_req {
  275. s16 dev_id;
  276. u32 flags;
  277. bdaddr_t src;
  278. bdaddr_t dst;
  279. u8 channel;
  280. };
  281. struct rfcomm_dev_info {
  282. s16 id;
  283. u32 flags;
  284. u16 state;
  285. bdaddr_t src;
  286. bdaddr_t dst;
  287. u8 channel;
  288. };
  289. struct rfcomm_dev_list_req {
  290. u16 dev_num;
  291. struct rfcomm_dev_info dev_info[0];
  292. };
  293. int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg);
  294. #ifdef CONFIG_BT_RFCOMM_TTY
  295. int rfcomm_init_ttys(void);
  296. void rfcomm_cleanup_ttys(void);
  297. #else
  298. static inline int rfcomm_init_ttys(void)
  299. {
  300. return 0;
  301. }
  302. static inline void rfcomm_cleanup_ttys(void)
  303. {
  304. }
  305. #endif
  306. #endif /* __RFCOMM_H */