Back to home page

OSCL-LXR

 
 

    


0001 /*
0002    BlueZ - Bluetooth protocol stack for Linux
0003    Copyright (C) 2000-2001 Qualcomm Incorporated
0004    Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
0005    Copyright (C) 2010 Google Inc.
0006 
0007    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
0008 
0009    This program is free software; you can redistribute it and/or modify
0010    it under the terms of the GNU General Public License version 2 as
0011    published by the Free Software Foundation;
0012 
0013    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0014    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0015    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
0016    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
0017    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
0018    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0019    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0020    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0021 
0022    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
0023    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
0024    SOFTWARE IS DISCLAIMED.
0025 */
0026 
0027 #ifndef __L2CAP_H
0028 #define __L2CAP_H
0029 
0030 #include <asm/unaligned.h>
0031 #include <linux/atomic.h>
0032 
0033 /* L2CAP defaults */
0034 #define L2CAP_DEFAULT_MTU       672
0035 #define L2CAP_DEFAULT_MIN_MTU       48
0036 #define L2CAP_DEFAULT_FLUSH_TO      0xFFFF
0037 #define L2CAP_EFS_DEFAULT_FLUSH_TO  0xFFFFFFFF
0038 #define L2CAP_DEFAULT_TX_WINDOW     63
0039 #define L2CAP_DEFAULT_EXT_WINDOW    0x3FFF
0040 #define L2CAP_DEFAULT_MAX_TX        3
0041 #define L2CAP_DEFAULT_RETRANS_TO    2000    /* 2 seconds */
0042 #define L2CAP_DEFAULT_MONITOR_TO    12000   /* 12 seconds */
0043 #define L2CAP_DEFAULT_MAX_PDU_SIZE  1492    /* Sized for AMP packet */
0044 #define L2CAP_DEFAULT_ACK_TO        200
0045 #define L2CAP_DEFAULT_MAX_SDU_SIZE  0xFFFF
0046 #define L2CAP_DEFAULT_SDU_ITIME     0xFFFFFFFF
0047 #define L2CAP_DEFAULT_ACC_LAT       0xFFFFFFFF
0048 #define L2CAP_BREDR_MAX_PAYLOAD     1019    /* 3-DH5 packet */
0049 #define L2CAP_LE_MIN_MTU        23
0050 #define L2CAP_ECRED_CONN_SCID_MAX   5
0051 
0052 #define L2CAP_DISC_TIMEOUT      msecs_to_jiffies(100)
0053 #define L2CAP_DISC_REJ_TIMEOUT      msecs_to_jiffies(5000)
0054 #define L2CAP_ENC_TIMEOUT       msecs_to_jiffies(5000)
0055 #define L2CAP_CONN_TIMEOUT      msecs_to_jiffies(40000)
0056 #define L2CAP_INFO_TIMEOUT      msecs_to_jiffies(4000)
0057 #define L2CAP_MOVE_TIMEOUT      msecs_to_jiffies(4000)
0058 #define L2CAP_MOVE_ERTX_TIMEOUT     msecs_to_jiffies(60000)
0059 #define L2CAP_WAIT_ACK_POLL_PERIOD  msecs_to_jiffies(200)
0060 #define L2CAP_WAIT_ACK_TIMEOUT      msecs_to_jiffies(10000)
0061 
0062 #define L2CAP_A2MP_DEFAULT_MTU      670
0063 
0064 /* L2CAP socket address */
0065 struct sockaddr_l2 {
0066     sa_family_t l2_family;
0067     __le16      l2_psm;
0068     bdaddr_t    l2_bdaddr;
0069     __le16      l2_cid;
0070     __u8        l2_bdaddr_type;
0071 };
0072 
0073 /* L2CAP socket options */
0074 #define L2CAP_OPTIONS   0x01
0075 struct l2cap_options {
0076     __u16 omtu;
0077     __u16 imtu;
0078     __u16 flush_to;
0079     __u8  mode;
0080     __u8  fcs;
0081     __u8  max_tx;
0082     __u16 txwin_size;
0083 };
0084 
0085 #define L2CAP_CONNINFO  0x02
0086 struct l2cap_conninfo {
0087     __u16 hci_handle;
0088     __u8  dev_class[3];
0089 };
0090 
0091 #define L2CAP_LM    0x03
0092 #define L2CAP_LM_MASTER     0x0001
0093 #define L2CAP_LM_AUTH       0x0002
0094 #define L2CAP_LM_ENCRYPT    0x0004
0095 #define L2CAP_LM_TRUSTED    0x0008
0096 #define L2CAP_LM_RELIABLE   0x0010
0097 #define L2CAP_LM_SECURE     0x0020
0098 #define L2CAP_LM_FIPS       0x0040
0099 
0100 /* L2CAP command codes */
0101 #define L2CAP_COMMAND_REJ   0x01
0102 #define L2CAP_CONN_REQ      0x02
0103 #define L2CAP_CONN_RSP      0x03
0104 #define L2CAP_CONF_REQ      0x04
0105 #define L2CAP_CONF_RSP      0x05
0106 #define L2CAP_DISCONN_REQ   0x06
0107 #define L2CAP_DISCONN_RSP   0x07
0108 #define L2CAP_ECHO_REQ      0x08
0109 #define L2CAP_ECHO_RSP      0x09
0110 #define L2CAP_INFO_REQ      0x0a
0111 #define L2CAP_INFO_RSP      0x0b
0112 #define L2CAP_CREATE_CHAN_REQ   0x0c
0113 #define L2CAP_CREATE_CHAN_RSP   0x0d
0114 #define L2CAP_MOVE_CHAN_REQ 0x0e
0115 #define L2CAP_MOVE_CHAN_RSP 0x0f
0116 #define L2CAP_MOVE_CHAN_CFM 0x10
0117 #define L2CAP_MOVE_CHAN_CFM_RSP 0x11
0118 #define L2CAP_CONN_PARAM_UPDATE_REQ 0x12
0119 #define L2CAP_CONN_PARAM_UPDATE_RSP 0x13
0120 #define L2CAP_LE_CONN_REQ   0x14
0121 #define L2CAP_LE_CONN_RSP   0x15
0122 #define L2CAP_LE_CREDITS    0x16
0123 #define L2CAP_ECRED_CONN_REQ    0x17
0124 #define L2CAP_ECRED_CONN_RSP    0x18
0125 #define L2CAP_ECRED_RECONF_REQ  0x19
0126 #define L2CAP_ECRED_RECONF_RSP  0x1a
0127 
0128 /* L2CAP extended feature mask */
0129 #define L2CAP_FEAT_FLOWCTL  0x00000001
0130 #define L2CAP_FEAT_RETRANS  0x00000002
0131 #define L2CAP_FEAT_BIDIR_QOS    0x00000004
0132 #define L2CAP_FEAT_ERTM     0x00000008
0133 #define L2CAP_FEAT_STREAMING    0x00000010
0134 #define L2CAP_FEAT_FCS      0x00000020
0135 #define L2CAP_FEAT_EXT_FLOW 0x00000040
0136 #define L2CAP_FEAT_FIXED_CHAN   0x00000080
0137 #define L2CAP_FEAT_EXT_WINDOW   0x00000100
0138 #define L2CAP_FEAT_UCD      0x00000200
0139 
0140 /* L2CAP checksum option */
0141 #define L2CAP_FCS_NONE      0x00
0142 #define L2CAP_FCS_CRC16     0x01
0143 
0144 /* L2CAP fixed channels */
0145 #define L2CAP_FC_SIG_BREDR  0x02
0146 #define L2CAP_FC_CONNLESS   0x04
0147 #define L2CAP_FC_A2MP       0x08
0148 #define L2CAP_FC_ATT        0x10
0149 #define L2CAP_FC_SIG_LE     0x20
0150 #define L2CAP_FC_SMP_LE     0x40
0151 #define L2CAP_FC_SMP_BREDR  0x80
0152 
0153 /* L2CAP Control Field bit masks */
0154 #define L2CAP_CTRL_SAR          0xC000
0155 #define L2CAP_CTRL_REQSEQ       0x3F00
0156 #define L2CAP_CTRL_TXSEQ        0x007E
0157 #define L2CAP_CTRL_SUPERVISE        0x000C
0158 
0159 #define L2CAP_CTRL_RETRANS      0x0080
0160 #define L2CAP_CTRL_FINAL        0x0080
0161 #define L2CAP_CTRL_POLL         0x0010
0162 #define L2CAP_CTRL_FRAME_TYPE       0x0001 /* I- or S-Frame */
0163 
0164 #define L2CAP_CTRL_TXSEQ_SHIFT      1
0165 #define L2CAP_CTRL_SUPER_SHIFT      2
0166 #define L2CAP_CTRL_POLL_SHIFT       4
0167 #define L2CAP_CTRL_FINAL_SHIFT      7
0168 #define L2CAP_CTRL_REQSEQ_SHIFT     8
0169 #define L2CAP_CTRL_SAR_SHIFT        14
0170 
0171 /* L2CAP Extended Control Field bit mask */
0172 #define L2CAP_EXT_CTRL_TXSEQ        0xFFFC0000
0173 #define L2CAP_EXT_CTRL_SAR      0x00030000
0174 #define L2CAP_EXT_CTRL_SUPERVISE    0x00030000
0175 #define L2CAP_EXT_CTRL_REQSEQ       0x0000FFFC
0176 
0177 #define L2CAP_EXT_CTRL_POLL     0x00040000
0178 #define L2CAP_EXT_CTRL_FINAL        0x00000002
0179 #define L2CAP_EXT_CTRL_FRAME_TYPE   0x00000001 /* I- or S-Frame */
0180 
0181 #define L2CAP_EXT_CTRL_FINAL_SHIFT  1
0182 #define L2CAP_EXT_CTRL_REQSEQ_SHIFT 2
0183 #define L2CAP_EXT_CTRL_SAR_SHIFT    16
0184 #define L2CAP_EXT_CTRL_SUPER_SHIFT  16
0185 #define L2CAP_EXT_CTRL_POLL_SHIFT   18
0186 #define L2CAP_EXT_CTRL_TXSEQ_SHIFT  18
0187 
0188 /* L2CAP Supervisory Function */
0189 #define L2CAP_SUPER_RR      0x00
0190 #define L2CAP_SUPER_REJ     0x01
0191 #define L2CAP_SUPER_RNR     0x02
0192 #define L2CAP_SUPER_SREJ    0x03
0193 
0194 /* L2CAP Segmentation and Reassembly */
0195 #define L2CAP_SAR_UNSEGMENTED   0x00
0196 #define L2CAP_SAR_START     0x01
0197 #define L2CAP_SAR_END       0x02
0198 #define L2CAP_SAR_CONTINUE  0x03
0199 
0200 /* L2CAP Command rej. reasons */
0201 #define L2CAP_REJ_NOT_UNDERSTOOD    0x0000
0202 #define L2CAP_REJ_MTU_EXCEEDED      0x0001
0203 #define L2CAP_REJ_INVALID_CID       0x0002
0204 
0205 /* L2CAP structures */
0206 struct l2cap_hdr {
0207     __le16     len;
0208     __le16     cid;
0209 } __packed;
0210 #define L2CAP_LEN_SIZE      2
0211 #define L2CAP_HDR_SIZE      4
0212 #define L2CAP_ENH_HDR_SIZE  6
0213 #define L2CAP_EXT_HDR_SIZE  8
0214 
0215 #define L2CAP_FCS_SIZE      2
0216 #define L2CAP_SDULEN_SIZE   2
0217 #define L2CAP_PSMLEN_SIZE   2
0218 #define L2CAP_ENH_CTRL_SIZE 2
0219 #define L2CAP_EXT_CTRL_SIZE 4
0220 
0221 struct l2cap_cmd_hdr {
0222     __u8       code;
0223     __u8       ident;
0224     __le16     len;
0225 } __packed;
0226 #define L2CAP_CMD_HDR_SIZE  4
0227 
0228 struct l2cap_cmd_rej_unk {
0229     __le16     reason;
0230 } __packed;
0231 
0232 struct l2cap_cmd_rej_mtu {
0233     __le16     reason;
0234     __le16     max_mtu;
0235 } __packed;
0236 
0237 struct l2cap_cmd_rej_cid {
0238     __le16     reason;
0239     __le16     scid;
0240     __le16     dcid;
0241 } __packed;
0242 
0243 struct l2cap_conn_req {
0244     __le16     psm;
0245     __le16     scid;
0246 } __packed;
0247 
0248 struct l2cap_conn_rsp {
0249     __le16     dcid;
0250     __le16     scid;
0251     __le16     result;
0252     __le16     status;
0253 } __packed;
0254 
0255 /* protocol/service multiplexer (PSM) */
0256 #define L2CAP_PSM_SDP       0x0001
0257 #define L2CAP_PSM_RFCOMM    0x0003
0258 #define L2CAP_PSM_3DSP      0x0021
0259 #define L2CAP_PSM_IPSP      0x0023 /* 6LoWPAN */
0260 
0261 #define L2CAP_PSM_DYN_START 0x1001
0262 #define L2CAP_PSM_DYN_END   0xffff
0263 #define L2CAP_PSM_AUTO_END  0x10ff
0264 #define L2CAP_PSM_LE_DYN_START  0x0080
0265 #define L2CAP_PSM_LE_DYN_END    0x00ff
0266 
0267 /* channel identifier */
0268 #define L2CAP_CID_SIGNALING 0x0001
0269 #define L2CAP_CID_CONN_LESS 0x0002
0270 #define L2CAP_CID_A2MP      0x0003
0271 #define L2CAP_CID_ATT       0x0004
0272 #define L2CAP_CID_LE_SIGNALING  0x0005
0273 #define L2CAP_CID_SMP       0x0006
0274 #define L2CAP_CID_SMP_BREDR 0x0007
0275 #define L2CAP_CID_DYN_START 0x0040
0276 #define L2CAP_CID_DYN_END   0xffff
0277 #define L2CAP_CID_LE_DYN_END    0x007f
0278 
0279 /* connect/create channel results */
0280 #define L2CAP_CR_SUCCESS    0x0000
0281 #define L2CAP_CR_PEND       0x0001
0282 #define L2CAP_CR_BAD_PSM    0x0002
0283 #define L2CAP_CR_SEC_BLOCK  0x0003
0284 #define L2CAP_CR_NO_MEM     0x0004
0285 #define L2CAP_CR_BAD_AMP    0x0005
0286 #define L2CAP_CR_INVALID_SCID   0x0006
0287 #define L2CAP_CR_SCID_IN_USE    0x0007
0288 
0289 /* credit based connect results */
0290 #define L2CAP_CR_LE_SUCCESS     0x0000
0291 #define L2CAP_CR_LE_BAD_PSM     0x0002
0292 #define L2CAP_CR_LE_NO_MEM      0x0004
0293 #define L2CAP_CR_LE_AUTHENTICATION  0x0005
0294 #define L2CAP_CR_LE_AUTHORIZATION   0x0006
0295 #define L2CAP_CR_LE_BAD_KEY_SIZE    0x0007
0296 #define L2CAP_CR_LE_ENCRYPTION      0x0008
0297 #define L2CAP_CR_LE_INVALID_SCID    0x0009
0298 #define L2CAP_CR_LE_SCID_IN_USE     0X000A
0299 #define L2CAP_CR_LE_UNACCEPT_PARAMS 0X000B
0300 #define L2CAP_CR_LE_INVALID_PARAMS  0X000C
0301 
0302 /* connect/create channel status */
0303 #define L2CAP_CS_NO_INFO    0x0000
0304 #define L2CAP_CS_AUTHEN_PEND    0x0001
0305 #define L2CAP_CS_AUTHOR_PEND    0x0002
0306 
0307 struct l2cap_conf_req {
0308     __le16     dcid;
0309     __le16     flags;
0310     __u8       data[];
0311 } __packed;
0312 
0313 struct l2cap_conf_rsp {
0314     __le16     scid;
0315     __le16     flags;
0316     __le16     result;
0317     __u8       data[];
0318 } __packed;
0319 
0320 #define L2CAP_CONF_SUCCESS  0x0000
0321 #define L2CAP_CONF_UNACCEPT 0x0001
0322 #define L2CAP_CONF_REJECT   0x0002
0323 #define L2CAP_CONF_UNKNOWN  0x0003
0324 #define L2CAP_CONF_PENDING  0x0004
0325 #define L2CAP_CONF_EFS_REJECT   0x0005
0326 
0327 /* configuration req/rsp continuation flag */
0328 #define L2CAP_CONF_FLAG_CONTINUATION    0x0001
0329 
0330 struct l2cap_conf_opt {
0331     __u8       type;
0332     __u8       len;
0333     __u8       val[];
0334 } __packed;
0335 #define L2CAP_CONF_OPT_SIZE 2
0336 
0337 #define L2CAP_CONF_HINT     0x80
0338 #define L2CAP_CONF_MASK     0x7f
0339 
0340 #define L2CAP_CONF_MTU      0x01
0341 #define L2CAP_CONF_FLUSH_TO 0x02
0342 #define L2CAP_CONF_QOS      0x03
0343 #define L2CAP_CONF_RFC      0x04
0344 #define L2CAP_CONF_FCS      0x05
0345 #define L2CAP_CONF_EFS      0x06
0346 #define L2CAP_CONF_EWS      0x07
0347 
0348 #define L2CAP_CONF_MAX_SIZE 22
0349 
0350 struct l2cap_conf_rfc {
0351     __u8       mode;
0352     __u8       txwin_size;
0353     __u8       max_transmit;
0354     __le16     retrans_timeout;
0355     __le16     monitor_timeout;
0356     __le16     max_pdu_size;
0357 } __packed;
0358 
0359 #define L2CAP_MODE_BASIC    0x00
0360 #define L2CAP_MODE_RETRANS  0x01
0361 #define L2CAP_MODE_FLOWCTL  0x02
0362 #define L2CAP_MODE_ERTM     0x03
0363 #define L2CAP_MODE_STREAMING    0x04
0364 
0365 /* Unlike the above this one doesn't actually map to anything that would
0366  * ever be sent over the air. Therefore, use a value that's unlikely to
0367  * ever be used in the BR/EDR configuration phase.
0368  */
0369 #define L2CAP_MODE_LE_FLOWCTL   0x80
0370 #define L2CAP_MODE_EXT_FLOWCTL  0x81
0371 
0372 struct l2cap_conf_efs {
0373     __u8    id;
0374     __u8    stype;
0375     __le16  msdu;
0376     __le32  sdu_itime;
0377     __le32  acc_lat;
0378     __le32  flush_to;
0379 } __packed;
0380 
0381 #define L2CAP_SERV_NOTRAFIC 0x00
0382 #define L2CAP_SERV_BESTEFFORT   0x01
0383 #define L2CAP_SERV_GUARANTEED   0x02
0384 
0385 #define L2CAP_BESTEFFORT_ID 0x01
0386 
0387 struct l2cap_disconn_req {
0388     __le16     dcid;
0389     __le16     scid;
0390 } __packed;
0391 
0392 struct l2cap_disconn_rsp {
0393     __le16     dcid;
0394     __le16     scid;
0395 } __packed;
0396 
0397 struct l2cap_info_req {
0398     __le16      type;
0399 } __packed;
0400 
0401 struct l2cap_info_rsp {
0402     __le16      type;
0403     __le16      result;
0404     __u8        data[];
0405 } __packed;
0406 
0407 struct l2cap_create_chan_req {
0408     __le16      psm;
0409     __le16      scid;
0410     __u8        amp_id;
0411 } __packed;
0412 
0413 struct l2cap_create_chan_rsp {
0414     __le16      dcid;
0415     __le16      scid;
0416     __le16      result;
0417     __le16      status;
0418 } __packed;
0419 
0420 struct l2cap_move_chan_req {
0421     __le16      icid;
0422     __u8        dest_amp_id;
0423 } __packed;
0424 
0425 struct l2cap_move_chan_rsp {
0426     __le16      icid;
0427     __le16      result;
0428 } __packed;
0429 
0430 #define L2CAP_MR_SUCCESS    0x0000
0431 #define L2CAP_MR_PEND       0x0001
0432 #define L2CAP_MR_BAD_ID     0x0002
0433 #define L2CAP_MR_SAME_ID    0x0003
0434 #define L2CAP_MR_NOT_SUPP   0x0004
0435 #define L2CAP_MR_COLLISION  0x0005
0436 #define L2CAP_MR_NOT_ALLOWED    0x0006
0437 
0438 struct l2cap_move_chan_cfm {
0439     __le16      icid;
0440     __le16      result;
0441 } __packed;
0442 
0443 #define L2CAP_MC_CONFIRMED  0x0000
0444 #define L2CAP_MC_UNCONFIRMED    0x0001
0445 
0446 struct l2cap_move_chan_cfm_rsp {
0447     __le16      icid;
0448 } __packed;
0449 
0450 /* info type */
0451 #define L2CAP_IT_CL_MTU     0x0001
0452 #define L2CAP_IT_FEAT_MASK  0x0002
0453 #define L2CAP_IT_FIXED_CHAN 0x0003
0454 
0455 /* info result */
0456 #define L2CAP_IR_SUCCESS    0x0000
0457 #define L2CAP_IR_NOTSUPP    0x0001
0458 
0459 struct l2cap_conn_param_update_req {
0460     __le16      min;
0461     __le16      max;
0462     __le16      latency;
0463     __le16      to_multiplier;
0464 } __packed;
0465 
0466 struct l2cap_conn_param_update_rsp {
0467     __le16      result;
0468 } __packed;
0469 
0470 /* Connection Parameters result */
0471 #define L2CAP_CONN_PARAM_ACCEPTED   0x0000
0472 #define L2CAP_CONN_PARAM_REJECTED   0x0001
0473 
0474 struct l2cap_le_conn_req {
0475     __le16     psm;
0476     __le16     scid;
0477     __le16     mtu;
0478     __le16     mps;
0479     __le16     credits;
0480 } __packed;
0481 
0482 struct l2cap_le_conn_rsp {
0483     __le16     dcid;
0484     __le16     mtu;
0485     __le16     mps;
0486     __le16     credits;
0487     __le16     result;
0488 } __packed;
0489 
0490 struct l2cap_le_credits {
0491     __le16     cid;
0492     __le16     credits;
0493 } __packed;
0494 
0495 #define L2CAP_ECRED_MIN_MTU     64
0496 #define L2CAP_ECRED_MIN_MPS     64
0497 #define L2CAP_ECRED_MAX_CID     5
0498 
0499 struct l2cap_ecred_conn_req {
0500     __le16 psm;
0501     __le16 mtu;
0502     __le16 mps;
0503     __le16 credits;
0504     __le16 scid[];
0505 } __packed;
0506 
0507 struct l2cap_ecred_conn_rsp {
0508     __le16 mtu;
0509     __le16 mps;
0510     __le16 credits;
0511     __le16 result;
0512     __le16 dcid[];
0513 };
0514 
0515 struct l2cap_ecred_reconf_req {
0516     __le16 mtu;
0517     __le16 mps;
0518     __le16 scid[];
0519 } __packed;
0520 
0521 #define L2CAP_RECONF_SUCCESS        0x0000
0522 #define L2CAP_RECONF_INVALID_MTU    0x0001
0523 #define L2CAP_RECONF_INVALID_MPS    0x0002
0524 
0525 struct l2cap_ecred_reconf_rsp {
0526     __le16 result;
0527 } __packed;
0528 
0529 /* ----- L2CAP channels and connections ----- */
0530 struct l2cap_seq_list {
0531     __u16   head;
0532     __u16   tail;
0533     __u16   mask;
0534     __u16   *list;
0535 };
0536 
0537 #define L2CAP_SEQ_LIST_CLEAR    0xFFFF
0538 #define L2CAP_SEQ_LIST_TAIL 0x8000
0539 
0540 struct l2cap_chan {
0541     struct l2cap_conn   *conn;
0542     struct hci_conn     *hs_hcon;
0543     struct hci_chan     *hs_hchan;
0544     struct kref kref;
0545     atomic_t    nesting;
0546 
0547     __u8        state;
0548 
0549     bdaddr_t    dst;
0550     __u8        dst_type;
0551     bdaddr_t    src;
0552     __u8        src_type;
0553     __le16      psm;
0554     __le16      sport;
0555     __u16       dcid;
0556     __u16       scid;
0557 
0558     __u16       imtu;
0559     __u16       omtu;
0560     __u16       flush_to;
0561     __u8        mode;
0562     __u8        chan_type;
0563     __u8        chan_policy;
0564 
0565     __u8        sec_level;
0566 
0567     __u8        ident;
0568 
0569     __u8        conf_req[64];
0570     __u8        conf_len;
0571     __u8        num_conf_req;
0572     __u8        num_conf_rsp;
0573 
0574     __u8        fcs;
0575 
0576     __u16       tx_win;
0577     __u16       tx_win_max;
0578     __u16       ack_win;
0579     __u8        max_tx;
0580     __u16       retrans_timeout;
0581     __u16       monitor_timeout;
0582     __u16       mps;
0583 
0584     __u16       tx_credits;
0585     __u16       rx_credits;
0586 
0587     __u8        tx_state;
0588     __u8        rx_state;
0589 
0590     unsigned long   conf_state;
0591     unsigned long   conn_state;
0592     unsigned long   flags;
0593 
0594     __u8        remote_amp_id;
0595     __u8        local_amp_id;
0596     __u8        move_id;
0597     __u8        move_state;
0598     __u8        move_role;
0599 
0600     __u16       next_tx_seq;
0601     __u16       expected_ack_seq;
0602     __u16       expected_tx_seq;
0603     __u16       buffer_seq;
0604     __u16       srej_save_reqseq;
0605     __u16       last_acked_seq;
0606     __u16       frames_sent;
0607     __u16       unacked_frames;
0608     __u8        retry_count;
0609     __u16       sdu_len;
0610     struct sk_buff  *sdu;
0611     struct sk_buff  *sdu_last_frag;
0612 
0613     __u16       remote_tx_win;
0614     __u8        remote_max_tx;
0615     __u16       remote_mps;
0616 
0617     __u8        local_id;
0618     __u8        local_stype;
0619     __u16       local_msdu;
0620     __u32       local_sdu_itime;
0621     __u32       local_acc_lat;
0622     __u32       local_flush_to;
0623 
0624     __u8        remote_id;
0625     __u8        remote_stype;
0626     __u16       remote_msdu;
0627     __u32       remote_sdu_itime;
0628     __u32       remote_acc_lat;
0629     __u32       remote_flush_to;
0630 
0631     struct delayed_work chan_timer;
0632     struct delayed_work retrans_timer;
0633     struct delayed_work monitor_timer;
0634     struct delayed_work ack_timer;
0635 
0636     struct sk_buff      *tx_send_head;
0637     struct sk_buff_head tx_q;
0638     struct sk_buff_head srej_q;
0639     struct l2cap_seq_list   srej_list;
0640     struct l2cap_seq_list   retrans_list;
0641 
0642     struct list_head    list;
0643     struct list_head    global_l;
0644 
0645     void            *data;
0646     const struct l2cap_ops  *ops;
0647     struct mutex        lock;
0648 };
0649 
0650 struct l2cap_ops {
0651     char            *name;
0652 
0653     struct l2cap_chan   *(*new_connection) (struct l2cap_chan *chan);
0654     int         (*recv) (struct l2cap_chan * chan,
0655                      struct sk_buff *skb);
0656     void            (*teardown) (struct l2cap_chan *chan, int err);
0657     void            (*close) (struct l2cap_chan *chan);
0658     void            (*state_change) (struct l2cap_chan *chan,
0659                          int state, int err);
0660     void            (*ready) (struct l2cap_chan *chan);
0661     void            (*defer) (struct l2cap_chan *chan);
0662     void            (*resume) (struct l2cap_chan *chan);
0663     void            (*suspend) (struct l2cap_chan *chan);
0664     void            (*set_shutdown) (struct l2cap_chan *chan);
0665     long            (*get_sndtimeo) (struct l2cap_chan *chan);
0666     struct pid      *(*get_peer_pid) (struct l2cap_chan *chan);
0667     struct sk_buff      *(*alloc_skb) (struct l2cap_chan *chan,
0668                            unsigned long hdr_len,
0669                            unsigned long len, int nb);
0670     int         (*filter) (struct l2cap_chan * chan,
0671                        struct sk_buff *skb);
0672 };
0673 
0674 struct l2cap_conn {
0675     struct hci_conn     *hcon;
0676     struct hci_chan     *hchan;
0677 
0678     unsigned int        mtu;
0679 
0680     __u32           feat_mask;
0681     __u8            remote_fixed_chan;
0682     __u8            local_fixed_chan;
0683 
0684     __u8            info_state;
0685     __u8            info_ident;
0686 
0687     struct delayed_work info_timer;
0688 
0689     struct sk_buff      *rx_skb;
0690     __u32           rx_len;
0691     __u8            tx_ident;
0692     struct mutex        ident_lock;
0693 
0694     struct sk_buff_head pending_rx;
0695     struct work_struct  pending_rx_work;
0696 
0697     struct work_struct  id_addr_update_work;
0698 
0699     __u8            disc_reason;
0700 
0701     struct l2cap_chan   *smp;
0702 
0703     struct list_head    chan_l;
0704     struct mutex        chan_lock;
0705     struct kref     ref;
0706     struct list_head    users;
0707 };
0708 
0709 struct l2cap_user {
0710     struct list_head list;
0711     int (*probe) (struct l2cap_conn *conn, struct l2cap_user *user);
0712     void (*remove) (struct l2cap_conn *conn, struct l2cap_user *user);
0713 };
0714 
0715 #define L2CAP_INFO_CL_MTU_REQ_SENT  0x01
0716 #define L2CAP_INFO_FEAT_MASK_REQ_SENT   0x04
0717 #define L2CAP_INFO_FEAT_MASK_REQ_DONE   0x08
0718 
0719 #define L2CAP_CHAN_RAW          1
0720 #define L2CAP_CHAN_CONN_LESS        2
0721 #define L2CAP_CHAN_CONN_ORIENTED    3
0722 #define L2CAP_CHAN_FIXED        4
0723 
0724 /* ----- L2CAP socket info ----- */
0725 #define l2cap_pi(sk) ((struct l2cap_pinfo *) sk)
0726 
0727 struct l2cap_pinfo {
0728     struct bt_sock      bt;
0729     struct l2cap_chan   *chan;
0730     struct sk_buff      *rx_busy_skb;
0731 };
0732 
0733 enum {
0734     CONF_REQ_SENT,
0735     CONF_INPUT_DONE,
0736     CONF_OUTPUT_DONE,
0737     CONF_MTU_DONE,
0738     CONF_MODE_DONE,
0739     CONF_CONNECT_PEND,
0740     CONF_RECV_NO_FCS,
0741     CONF_STATE2_DEVICE,
0742     CONF_EWS_RECV,
0743     CONF_LOC_CONF_PEND,
0744     CONF_REM_CONF_PEND,
0745     CONF_NOT_COMPLETE,
0746 };
0747 
0748 #define L2CAP_CONF_MAX_CONF_REQ 2
0749 #define L2CAP_CONF_MAX_CONF_RSP 2
0750 
0751 enum {
0752     CONN_SREJ_SENT,
0753     CONN_WAIT_F,
0754     CONN_SREJ_ACT,
0755     CONN_SEND_PBIT,
0756     CONN_REMOTE_BUSY,
0757     CONN_LOCAL_BUSY,
0758     CONN_REJ_ACT,
0759     CONN_SEND_FBIT,
0760     CONN_RNR_SENT,
0761 };
0762 
0763 /* Definitions for flags in l2cap_chan */
0764 enum {
0765     FLAG_ROLE_SWITCH,
0766     FLAG_FORCE_ACTIVE,
0767     FLAG_FORCE_RELIABLE,
0768     FLAG_FLUSHABLE,
0769     FLAG_EXT_CTRL,
0770     FLAG_EFS_ENABLE,
0771     FLAG_DEFER_SETUP,
0772     FLAG_LE_CONN_REQ_SENT,
0773     FLAG_ECRED_CONN_REQ_SENT,
0774     FLAG_PENDING_SECURITY,
0775     FLAG_HOLD_HCI_CONN,
0776 };
0777 
0778 /* Lock nesting levels for L2CAP channels. We need these because lockdep
0779  * otherwise considers all channels equal and will e.g. complain about a
0780  * connection oriented channel triggering SMP procedures or a listening
0781  * channel creating and locking a child channel.
0782  */
0783 enum {
0784     L2CAP_NESTING_SMP,
0785     L2CAP_NESTING_NORMAL,
0786     L2CAP_NESTING_PARENT,
0787 };
0788 
0789 enum {
0790     L2CAP_TX_STATE_XMIT,
0791     L2CAP_TX_STATE_WAIT_F,
0792 };
0793 
0794 enum {
0795     L2CAP_RX_STATE_RECV,
0796     L2CAP_RX_STATE_SREJ_SENT,
0797     L2CAP_RX_STATE_MOVE,
0798     L2CAP_RX_STATE_WAIT_P,
0799     L2CAP_RX_STATE_WAIT_F,
0800 };
0801 
0802 enum {
0803     L2CAP_TXSEQ_EXPECTED,
0804     L2CAP_TXSEQ_EXPECTED_SREJ,
0805     L2CAP_TXSEQ_UNEXPECTED,
0806     L2CAP_TXSEQ_UNEXPECTED_SREJ,
0807     L2CAP_TXSEQ_DUPLICATE,
0808     L2CAP_TXSEQ_DUPLICATE_SREJ,
0809     L2CAP_TXSEQ_INVALID,
0810     L2CAP_TXSEQ_INVALID_IGNORE,
0811 };
0812 
0813 enum {
0814     L2CAP_EV_DATA_REQUEST,
0815     L2CAP_EV_LOCAL_BUSY_DETECTED,
0816     L2CAP_EV_LOCAL_BUSY_CLEAR,
0817     L2CAP_EV_RECV_REQSEQ_AND_FBIT,
0818     L2CAP_EV_RECV_FBIT,
0819     L2CAP_EV_RETRANS_TO,
0820     L2CAP_EV_MONITOR_TO,
0821     L2CAP_EV_EXPLICIT_POLL,
0822     L2CAP_EV_RECV_IFRAME,
0823     L2CAP_EV_RECV_RR,
0824     L2CAP_EV_RECV_REJ,
0825     L2CAP_EV_RECV_RNR,
0826     L2CAP_EV_RECV_SREJ,
0827     L2CAP_EV_RECV_FRAME,
0828 };
0829 
0830 enum {
0831     L2CAP_MOVE_ROLE_NONE,
0832     L2CAP_MOVE_ROLE_INITIATOR,
0833     L2CAP_MOVE_ROLE_RESPONDER,
0834 };
0835 
0836 enum {
0837     L2CAP_MOVE_STABLE,
0838     L2CAP_MOVE_WAIT_REQ,
0839     L2CAP_MOVE_WAIT_RSP,
0840     L2CAP_MOVE_WAIT_RSP_SUCCESS,
0841     L2CAP_MOVE_WAIT_CONFIRM,
0842     L2CAP_MOVE_WAIT_CONFIRM_RSP,
0843     L2CAP_MOVE_WAIT_LOGICAL_COMP,
0844     L2CAP_MOVE_WAIT_LOGICAL_CFM,
0845     L2CAP_MOVE_WAIT_LOCAL_BUSY,
0846     L2CAP_MOVE_WAIT_PREPARE,
0847 };
0848 
0849 void l2cap_chan_hold(struct l2cap_chan *c);
0850 struct l2cap_chan *l2cap_chan_hold_unless_zero(struct l2cap_chan *c);
0851 void l2cap_chan_put(struct l2cap_chan *c);
0852 
0853 static inline void l2cap_chan_lock(struct l2cap_chan *chan)
0854 {
0855     mutex_lock_nested(&chan->lock, atomic_read(&chan->nesting));
0856 }
0857 
0858 static inline void l2cap_chan_unlock(struct l2cap_chan *chan)
0859 {
0860     mutex_unlock(&chan->lock);
0861 }
0862 
0863 static inline void l2cap_set_timer(struct l2cap_chan *chan,
0864                    struct delayed_work *work, long timeout)
0865 {
0866     BT_DBG("chan %p state %s timeout %ld", chan,
0867            state_to_string(chan->state), timeout);
0868 
0869     /* If delayed work cancelled do not hold(chan)
0870        since it is already done with previous set_timer */
0871     if (!cancel_delayed_work(work))
0872         l2cap_chan_hold(chan);
0873 
0874     schedule_delayed_work(work, timeout);
0875 }
0876 
0877 static inline bool l2cap_clear_timer(struct l2cap_chan *chan,
0878                      struct delayed_work *work)
0879 {
0880     bool ret;
0881 
0882     /* put(chan) if delayed work cancelled otherwise it
0883        is done in delayed work function */
0884     ret = cancel_delayed_work(work);
0885     if (ret)
0886         l2cap_chan_put(chan);
0887 
0888     return ret;
0889 }
0890 
0891 #define __set_chan_timer(c, t) l2cap_set_timer(c, &c->chan_timer, (t))
0892 #define __clear_chan_timer(c) l2cap_clear_timer(c, &c->chan_timer)
0893 #define __clear_retrans_timer(c) l2cap_clear_timer(c, &c->retrans_timer)
0894 #define __clear_monitor_timer(c) l2cap_clear_timer(c, &c->monitor_timer)
0895 #define __set_ack_timer(c) l2cap_set_timer(c, &chan->ack_timer, \
0896         msecs_to_jiffies(L2CAP_DEFAULT_ACK_TO));
0897 #define __clear_ack_timer(c) l2cap_clear_timer(c, &c->ack_timer)
0898 
0899 static inline int __seq_offset(struct l2cap_chan *chan, __u16 seq1, __u16 seq2)
0900 {
0901     if (seq1 >= seq2)
0902         return seq1 - seq2;
0903     else
0904         return chan->tx_win_max + 1 - seq2 + seq1;
0905 }
0906 
0907 static inline __u16 __next_seq(struct l2cap_chan *chan, __u16 seq)
0908 {
0909     return (seq + 1) % (chan->tx_win_max + 1);
0910 }
0911 
0912 static inline struct l2cap_chan *l2cap_chan_no_new_connection(struct l2cap_chan *chan)
0913 {
0914     return NULL;
0915 }
0916 
0917 static inline int l2cap_chan_no_recv(struct l2cap_chan *chan, struct sk_buff *skb)
0918 {
0919     return -ENOSYS;
0920 }
0921 
0922 static inline struct sk_buff *l2cap_chan_no_alloc_skb(struct l2cap_chan *chan,
0923                               unsigned long hdr_len,
0924                               unsigned long len, int nb)
0925 {
0926     return ERR_PTR(-ENOSYS);
0927 }
0928 
0929 static inline void l2cap_chan_no_teardown(struct l2cap_chan *chan, int err)
0930 {
0931 }
0932 
0933 static inline void l2cap_chan_no_close(struct l2cap_chan *chan)
0934 {
0935 }
0936 
0937 static inline void l2cap_chan_no_ready(struct l2cap_chan *chan)
0938 {
0939 }
0940 
0941 static inline void l2cap_chan_no_state_change(struct l2cap_chan *chan,
0942                           int state, int err)
0943 {
0944 }
0945 
0946 static inline void l2cap_chan_no_defer(struct l2cap_chan *chan)
0947 {
0948 }
0949 
0950 static inline void l2cap_chan_no_suspend(struct l2cap_chan *chan)
0951 {
0952 }
0953 
0954 static inline void l2cap_chan_no_resume(struct l2cap_chan *chan)
0955 {
0956 }
0957 
0958 static inline void l2cap_chan_no_set_shutdown(struct l2cap_chan *chan)
0959 {
0960 }
0961 
0962 static inline long l2cap_chan_no_get_sndtimeo(struct l2cap_chan *chan)
0963 {
0964     return 0;
0965 }
0966 
0967 extern bool disable_ertm;
0968 extern bool enable_ecred;
0969 
0970 int l2cap_init_sockets(void);
0971 void l2cap_cleanup_sockets(void);
0972 bool l2cap_is_socket(struct socket *sock);
0973 
0974 void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan);
0975 void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan);
0976 void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
0977 
0978 int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);
0979 int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid);
0980 
0981 struct l2cap_chan *l2cap_chan_create(void);
0982 void l2cap_chan_close(struct l2cap_chan *chan, int reason);
0983 int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
0984                bdaddr_t *dst, u8 dst_type);
0985 int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu);
0986 int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len);
0987 void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
0988 int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator);
0989 void l2cap_chan_set_defaults(struct l2cap_chan *chan);
0990 int l2cap_ertm_init(struct l2cap_chan *chan);
0991 void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
0992 void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
0993 typedef void (*l2cap_chan_func_t)(struct l2cap_chan *chan, void *data);
0994 void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
0995              void *data);
0996 void l2cap_chan_del(struct l2cap_chan *chan, int err);
0997 void l2cap_send_conn_req(struct l2cap_chan *chan);
0998 void l2cap_move_start(struct l2cap_chan *chan);
0999 void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
1000                u8 status);
1001 void __l2cap_physical_cfm(struct l2cap_chan *chan, int result);
1002 
1003 struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn);
1004 void l2cap_conn_put(struct l2cap_conn *conn);
1005 
1006 int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user);
1007 void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user);
1008 
1009 #endif /* __L2CAP_H */