OSDN Git Service

am c1ccb0d9: (-s ours) anthias:17614185:Bluetooth: Getbuf returning invalid buffer...
[android-x86/system-bt.git] / stack / l2cap / l2c_utils.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /******************************************************************************
20  *
21  *  This file contains L2CAP utility functions
22  *
23  ******************************************************************************/
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "gki.h"
30 #include "bt_types.h"
31 #include "hcimsgs.h"
32 #include "l2cdefs.h"
33 #include "l2c_int.h"
34 #include "hcidefs.h"
35 #include "btu.h"
36 #include "btm_api.h"
37 #include "btm_int.h"
38 #include "hcidefs.h"
39 #include "bd.h"
40 #include "bt_utils.h"
41
42 /*******************************************************************************
43 **
44 ** Function         l2cu_allocate_lcb
45 **
46 ** Description      Look for an unused LCB
47 **
48 ** Returns          LCB address or NULL if none found
49 **
50 *******************************************************************************/
51 tL2C_LCB *l2cu_allocate_lcb (BD_ADDR p_bd_addr, BOOLEAN is_bonding, tBT_TRANSPORT transport)
52 {
53     int         xx;
54     tL2C_LCB    *p_lcb = &l2cb.lcb_pool[0];
55
56     for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
57     {
58         if (!p_lcb->in_use)
59         {
60             memset (p_lcb, 0, sizeof (tL2C_LCB));
61
62             memcpy (p_lcb->remote_bd_addr, p_bd_addr, BD_ADDR_LEN);
63
64             p_lcb->in_use          = TRUE;
65             p_lcb->link_state      = LST_DISCONNECTED;
66             p_lcb->handle          = HCI_INVALID_HANDLE;
67             p_lcb->link_flush_tout = 0xFFFF;
68             p_lcb->timer_entry.param = (TIMER_PARAM_TYPE)p_lcb;
69             p_lcb->info_timer_entry.param = (TIMER_PARAM_TYPE)p_lcb;
70             p_lcb->idle_timeout    = l2cb.idle_timeout;
71             p_lcb->id              = 1;                     /* spec does not allow '0' */
72             p_lcb->is_bonding      = is_bonding;
73 #if (BLE_INCLUDED == TRUE)
74             p_lcb->transport       = transport;
75
76             if (transport == BT_TRANSPORT_LE)
77             {
78                 l2cb.num_ble_links_active++;
79                 l2c_ble_link_adjust_allocation();
80             }
81             else
82 #endif
83             {
84                 l2cb.num_links_active++;
85                 l2c_link_adjust_allocation();
86             }
87             return (p_lcb);
88         }
89     }
90
91     /* If here, no free LCB found */
92     return (NULL);
93 }
94
95 /*******************************************************************************
96 **
97 ** Function         l2cu_update_lcb_4_bonding
98 **
99 ** Description      Mark the lcb for bonding. Used when bonding takes place on
100 **                  an existing ACL connection.  (Pre-Lisbon devices)
101 **
102 ** Returns          Nothing
103 **
104 *******************************************************************************/
105 void l2cu_update_lcb_4_bonding (BD_ADDR p_bd_addr, BOOLEAN is_bonding)
106 {
107     tL2C_LCB    *p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR);
108
109     if (p_lcb)
110     {
111         L2CAP_TRACE_DEBUG ("l2cu_update_lcb_4_bonding  BDA: %08x%04x is_bonding: %d",
112                           (p_bd_addr[0]<<24)+(p_bd_addr[1]<<16)+(p_bd_addr[2]<<8)+p_bd_addr[3],
113                           (p_bd_addr[4]<<8)+p_bd_addr[5], is_bonding);
114         p_lcb->is_bonding = is_bonding;
115     }
116 }
117
118 /*******************************************************************************
119 **
120 ** Function         l2cu_release_lcb
121 **
122 ** Description      Release an LCB. All timers will be stopped, channels
123 **                  dropped, buffers returned etc.
124 **
125 ** Returns          void
126 **
127 *******************************************************************************/
128 void l2cu_release_lcb (tL2C_LCB *p_lcb)
129 {
130     tL2C_CCB    *p_ccb;
131
132     p_lcb->in_use     = FALSE;
133     p_lcb->is_bonding = FALSE;
134
135 #if (BLE_INCLUDED == TRUE)
136     btu_stop_timer(&p_lcb->conn_param_enb);
137 #endif
138     /* Stop timers */
139     btu_stop_timer (&p_lcb->timer_entry);
140     btu_stop_timer (&p_lcb->info_timer_entry);
141
142     /* Release any unfinished L2CAP packet on this link */
143     if (p_lcb->p_hcit_rcv_acl)
144     {
145         GKI_freebuf(p_lcb->p_hcit_rcv_acl);
146         p_lcb->p_hcit_rcv_acl = NULL;
147     }
148
149 #if BTM_SCO_INCLUDED == TRUE
150 #if (BLE_INCLUDED == TRUE)
151         if (p_lcb->transport == BT_TRANSPORT_BR_EDR)
152 #endif
153         /* Release all SCO links */
154         btm_remove_sco_links(p_lcb->remote_bd_addr);
155 #endif
156
157     if (p_lcb->sent_not_acked > 0)
158     {
159 #if (BLE_INCLUDED == TRUE)
160         if (p_lcb->transport == BT_TRANSPORT_LE)
161         {
162             l2cb.controller_le_xmit_window += p_lcb->sent_not_acked;
163             if (l2cb.controller_le_xmit_window > l2cb.num_lm_ble_bufs)
164             {
165                 l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
166             }
167         }
168         else
169 #endif
170         {
171             l2cb.controller_xmit_window += p_lcb->sent_not_acked;
172             if (l2cb.controller_xmit_window > l2cb.num_lm_acl_bufs)
173             {
174                 l2cb.controller_xmit_window = l2cb.num_lm_acl_bufs;
175             }
176         }
177     }
178
179 #if (BLE_INCLUDED == TRUE)
180     l2cb.is_ble_connecting = FALSE;
181 #endif
182
183 #if (L2CAP_NUM_FIXED_CHNLS > 0)
184     {
185         int         xx;
186
187         for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++)
188         {
189             if (p_lcb->p_fixed_ccbs[xx])
190             {
191                 l2cu_release_ccb (p_lcb->p_fixed_ccbs[xx]);
192                 p_lcb->p_fixed_ccbs[xx] = NULL;
193                 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
194             }
195             else if ( (p_lcb->peer_chnl_mask[0] & (1 << (xx + L2CAP_FIRST_FIXED_CHNL)))
196                    && (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL) )
197                     (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
198         }
199     }
200 #endif
201
202     /* Ensure no CCBs left on this LCB */
203     for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_lcb->ccb_queue.p_first_ccb)
204     {
205         l2cu_release_ccb (p_ccb);
206     }
207
208     /* Tell BTM Acl management the link was removed */
209     if ((p_lcb->link_state == LST_CONNECTED) || (p_lcb->link_state == LST_DISCONNECTING))
210 #if (BLE_INCLUDED == TRUE)
211         btm_acl_removed (p_lcb->remote_bd_addr, p_lcb->transport);
212 #else
213         btm_acl_removed (p_lcb->remote_bd_addr, BT_TRANSPORT_BR_EDR);
214 #endif
215     /* Release any held buffers */
216     while (p_lcb->link_xmit_data_q.p_first)
217         GKI_freebuf (GKI_dequeue (&p_lcb->link_xmit_data_q));
218
219 #if (L2CAP_UCD_INCLUDED == TRUE)
220     /* clean up any security pending UCD */
221     l2c_ucd_delete_sec_pending_q(p_lcb);
222 #endif
223
224 #if BLE_INCLUDED == TRUE
225     /* Re-adjust flow control windows make sure it does not go negative */
226     if (p_lcb->transport == BT_TRANSPORT_LE)
227     {
228         if (l2cb.num_ble_links_active >= 1)
229             l2cb.num_ble_links_active--;
230
231         l2c_ble_link_adjust_allocation();
232     }
233     else
234 #endif
235     {
236         if (l2cb.num_links_active >= 1)
237             l2cb.num_links_active--;
238
239         l2c_link_adjust_allocation();
240     }
241
242     /* Check for ping outstanding */
243     if (p_lcb->p_echo_rsp_cb)
244     {
245         tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
246
247         /* Zero out the callback in case app immediately calls us again */
248         p_lcb->p_echo_rsp_cb = NULL;
249
250         (*p_cb) (L2CAP_PING_RESULT_NO_LINK);
251     }
252 }
253
254
255 /*******************************************************************************
256 **
257 ** Function         l2cu_find_lcb_by_bd_addr
258 **
259 ** Description      Look through all active LCBs for a match based on the
260 **                  remote BD address.
261 **
262 ** Returns          pointer to matched LCB, or NULL if no match
263 **
264 *******************************************************************************/
265 tL2C_LCB  *l2cu_find_lcb_by_bd_addr (BD_ADDR p_bd_addr, tBT_TRANSPORT transport)
266 {
267     int         xx;
268     tL2C_LCB    *p_lcb = &l2cb.lcb_pool[0];
269
270     for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
271     {
272         if ((p_lcb->in_use) &&
273 #if BLE_INCLUDED == TRUE
274             p_lcb->transport == transport &&
275 #endif
276             (!memcmp (p_lcb->remote_bd_addr, p_bd_addr, BD_ADDR_LEN)))
277         {
278             return (p_lcb);
279         }
280     }
281
282     /* If here, no match found */
283     return (NULL);
284 }
285
286 /*******************************************************************************
287 **
288 ** Function         l2cu_get_conn_role
289 **
290 ** Description      Determine the desired role (master or slave) of a link.
291 **                  If already got a slave link, this one must be a master. If
292 **                  already got at least 1 link where we are the master, make this
293 **                  also a master.
294 **
295 ** Returns          HCI_ROLE_MASTER or HCI_ROLE_SLAVE
296 **
297 *******************************************************************************/
298 UINT8 l2cu_get_conn_role (tL2C_LCB *p_this_lcb)
299 {
300     UINT8 i;
301     for (i = 0; i < BTM_ROLE_DEVICE_NUM; i++) {
302         if ((btm_cb.previous_connected_role[i] != BTM_ROLE_UNDEFINED) &&
303             (!bdcmp(p_this_lcb->remote_bd_addr, btm_cb.previous_connected_remote_addr[i]))) {
304             L2CAP_TRACE_WARNING ("l2cu_get_conn_role %d",
305                                   btm_cb.previous_connected_role[i]);
306             return btm_cb.previous_connected_role[i];
307         }
308     }
309     return l2cb.desire_role;
310 }
311
312 /*******************************************************************************
313 **
314 ** Function         l2c_is_cmd_rejected
315 **
316 ** Description      Checks if cmd_code is command or response
317 **                  If a command it will be rejected per spec.
318 **                  This function is used when a illegal packet length is detected
319 **
320 ** Returns          BOOLEAN - TRUE if cmd_code is a command and it is rejected,
321 **                            FALSE if response code. (command not rejected)
322 **
323 *******************************************************************************/
324 BOOLEAN l2c_is_cmd_rejected (UINT8 cmd_code, UINT8 id, tL2C_LCB *p_lcb)
325 {
326     switch(cmd_code)
327     {
328     case L2CAP_CMD_CONN_REQ:
329     case L2CAP_CMD_CONFIG_REQ:
330     case L2CAP_CMD_DISC_REQ:
331     case L2CAP_CMD_ECHO_REQ:
332     case L2CAP_CMD_INFO_REQ:
333     case L2CAP_CMD_AMP_CONN_REQ:
334     case L2CAP_CMD_AMP_MOVE_REQ:
335     case L2CAP_CMD_BLE_UPDATE_REQ:
336         l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_MTU_EXCEEDED, id, L2CAP_DEFAULT_MTU, 0);
337         L2CAP_TRACE_WARNING ("Dumping first Command (%d)", cmd_code);
338         return TRUE;
339
340     default:    /* Otherwise a response */
341         return FALSE;
342     }
343 }
344
345 /*******************************************************************************
346 **
347 ** Function         l2cu_build_header
348 **
349 ** Description      Builds the L2CAP command packet header
350 **
351 ** Returns          Pointer to allocated packet or NULL if no resources
352 **
353 *******************************************************************************/
354 BT_HDR *l2cu_build_header (tL2C_LCB *p_lcb, UINT16 len, UINT8 cmd, UINT8 id)
355 {
356     BT_HDR  *p_buf = (BT_HDR *)GKI_getpoolbuf (L2CAP_CMD_POOL_ID);
357     UINT8   *p;
358
359     if (!p_buf)
360     {
361         L2CAP_TRACE_ERROR ("l2cu_build_header - no buffer");
362         return (NULL);
363     }
364
365     p_buf->offset = L2CAP_SEND_CMD_OFFSET;
366     p_buf->len = len + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
367     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET;
368
369     /* Put in HCI header - handle + pkt boundary */
370 #if (BLE_INCLUDED == TRUE)
371     if (p_lcb->transport == BT_TRANSPORT_LE)
372     {
373         UINT16_TO_STREAM (p, (p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT)));
374     }
375     else
376 #endif
377     {
378 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
379         UINT16_TO_STREAM (p, p_lcb->handle | l2cb.non_flushable_pbf);
380 #else
381         UINT16_TO_STREAM (p, (p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT)));
382 #endif
383     }
384
385     UINT16_TO_STREAM (p, len + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD);
386     UINT16_TO_STREAM (p, len + L2CAP_CMD_OVERHEAD);
387
388 #if (BLE_INCLUDED == TRUE)
389     if (p_lcb->transport == BT_TRANSPORT_LE)
390     {
391         UINT16_TO_STREAM (p, L2CAP_BLE_SIGNALLING_CID);
392     }
393     else
394 #endif
395     {
396         UINT16_TO_STREAM (p, L2CAP_SIGNALLING_CID);
397     }
398
399     /* Put in L2CAP command header */
400     UINT8_TO_STREAM  (p, cmd);
401     UINT8_TO_STREAM  (p, id);
402     UINT16_TO_STREAM (p, len);
403
404     return (p_buf);
405 }
406
407 /*******************************************************************************
408 **
409 ** Function         l2cu_adj_id
410 **
411 ** Description      Checks for valid ID based on specified mask
412 **                  and adjusts the id if invalid.
413 **
414 ** Returns          void
415 **
416 *******************************************************************************/
417 void l2cu_adj_id (tL2C_LCB *p_lcb, UINT8 adj_mask)
418 {
419     if ((adj_mask & L2CAP_ADJ_ZERO_ID) && !p_lcb->id)
420     {
421         p_lcb->id++;
422     }
423 }
424
425 /*******************************************************************************
426 **
427 ** Function         l2cu_send_peer_cmd_reject
428 **
429 ** Description      Build and send an L2CAP "command reject" message
430 **                  to the peer.
431 **
432 ** Returns          void
433 **
434 *******************************************************************************/
435 void l2cu_send_peer_cmd_reject (tL2C_LCB *p_lcb, UINT16 reason, UINT8 rem_id,
436                                 UINT16 p1, UINT16 p2)
437 {
438     UINT16  param_len;
439     BT_HDR  *p_buf;
440     UINT8   *p;
441
442     /* Put in L2CAP packet header */
443     if (reason == L2CAP_CMD_REJ_MTU_EXCEEDED)
444         param_len = 2;
445     else if (reason == L2CAP_CMD_REJ_INVALID_CID)
446         param_len = 4;
447     else
448         param_len = 0;
449
450     if ((p_buf = l2cu_build_header (p_lcb, (UINT16) (L2CAP_CMD_REJECT_LEN + param_len), L2CAP_CMD_REJECT, rem_id)) == NULL )
451     {
452         L2CAP_TRACE_WARNING ("L2CAP - no buffer cmd_rej");
453         return;
454     }
455
456     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
457                                L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
458
459     UINT16_TO_STREAM (p, reason);
460
461     if (param_len >= 2)
462         UINT16_TO_STREAM (p, p1);
463
464     if (param_len >= 4)
465         UINT16_TO_STREAM (p, p2);
466
467     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
468 }
469
470
471 /*******************************************************************************
472 **
473 ** Function         l2cu_send_peer_connect_req
474 **
475 ** Description      Build and send an L2CAP "connection request" message
476 **                  to the peer.
477 **
478 ** Returns          void
479 **
480 *******************************************************************************/
481 void l2cu_send_peer_connect_req (tL2C_CCB *p_ccb)
482 {
483     BT_HDR  *p_buf;
484     UINT8   *p;
485
486     /* Create an identifier for this packet */
487     p_ccb->p_lcb->id++;
488     l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
489
490     p_ccb->local_id = p_ccb->p_lcb->id;
491
492     if ((p_buf = l2cu_build_header (p_ccb->p_lcb, L2CAP_CONN_REQ_LEN, L2CAP_CMD_CONN_REQ,
493                                     p_ccb->local_id)) == NULL)
494     {
495         L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_req");
496         return;
497     }
498
499     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET+HCI_DATA_PREAMBLE_SIZE +
500         L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
501
502     UINT16_TO_STREAM (p, p_ccb->p_rcb->real_psm);
503     UINT16_TO_STREAM (p, p_ccb->local_cid);
504
505     l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
506 }
507
508
509 /*******************************************************************************
510 **
511 ** Function         l2cu_send_peer_connect_rsp
512 **
513 ** Description      Build and send an L2CAP "connection response" message
514 **                  to the peer.
515 **
516 ** Returns          void
517 **
518 *******************************************************************************/
519 void l2cu_send_peer_connect_rsp (tL2C_CCB *p_ccb, UINT16 result, UINT16 status)
520 {
521     BT_HDR  *p_buf;
522     UINT8   *p;
523
524     if (result == L2CAP_CONN_PENDING)
525     {
526         /* if we already sent pending response */
527         if (p_ccb->flags & CCB_FLAG_SENT_PENDING)
528             return;
529         else
530             p_ccb->flags |= CCB_FLAG_SENT_PENDING;
531     }
532
533     if ((p_buf=l2cu_build_header(p_ccb->p_lcb, L2CAP_CONN_RSP_LEN, L2CAP_CMD_CONN_RSP, p_ccb->remote_id)) == NULL)
534     {
535         L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_rsp");
536         return;
537     }
538
539     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET+HCI_DATA_PREAMBLE_SIZE +
540         L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
541
542     UINT16_TO_STREAM (p, p_ccb->local_cid);
543     UINT16_TO_STREAM (p, p_ccb->remote_cid);
544     UINT16_TO_STREAM (p, result);
545     UINT16_TO_STREAM (p, status);
546
547     l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
548 }
549
550
551 /*******************************************************************************
552 **
553 ** Function         l2cu_reject_connection
554 **
555 ** Description      Build and send an L2CAP "connection response neg" message
556 **                  to the peer. This function is called when there is no peer
557 **                  CCB (non-existant PSM or no resources).
558 **
559 ** Returns          void
560 **
561 *******************************************************************************/
562 void l2cu_reject_connection (tL2C_LCB *p_lcb, UINT16 remote_cid, UINT8 rem_id, UINT16 result)
563 {
564     BT_HDR  *p_buf;
565     UINT8   *p;
566
567     if ((p_buf = l2cu_build_header(p_lcb, L2CAP_CONN_RSP_LEN, L2CAP_CMD_CONN_RSP, rem_id)) == NULL )
568     {
569         L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_req");
570         return;
571     }
572
573     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET+HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
574
575     UINT16_TO_STREAM (p, 0);                    /* Local CID of 0   */
576     UINT16_TO_STREAM (p, remote_cid);
577     UINT16_TO_STREAM (p, result);
578     UINT16_TO_STREAM (p, 0);                    /* Status of 0      */
579
580     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
581 }
582
583 /*******************************************************************************
584 **
585 ** Function         l2cu_send_peer_config_req
586 **
587 ** Description      Build and send an L2CAP "configuration request" message
588 **                  to the peer.
589 **
590 ** Returns          void
591 **
592 *******************************************************************************/
593 void l2cu_send_peer_config_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
594 {
595     BT_HDR  *p_buf;
596     UINT16  cfg_len=0;
597     UINT8   *p;
598
599     /* Create an identifier for this packet */
600     p_ccb->p_lcb->id++;
601     l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
602
603     p_ccb->local_id = p_ccb->p_lcb->id;
604
605     if (p_cfg->mtu_present)
606         cfg_len += L2CAP_CFG_MTU_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
607     if (p_cfg->flush_to_present)
608         cfg_len += L2CAP_CFG_FLUSH_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
609     if (p_cfg->qos_present)
610         cfg_len += L2CAP_CFG_QOS_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
611     if (p_cfg->fcr_present)
612         cfg_len += L2CAP_CFG_FCR_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
613     if (p_cfg->fcs_present)
614         cfg_len += L2CAP_CFG_FCS_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
615     if (p_cfg->ext_flow_spec_present)
616         cfg_len += L2CAP_CFG_EXT_FLOW_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
617
618     if ((p_buf = l2cu_build_header (p_ccb->p_lcb, (UINT16) (L2CAP_CONFIG_REQ_LEN + cfg_len),
619         L2CAP_CMD_CONFIG_REQ, p_ccb->local_id)) == NULL )
620     {
621         L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_req");
622         return;
623     }
624
625     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
626                                L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
627
628     UINT16_TO_STREAM (p, p_ccb->remote_cid);
629     UINT16_TO_STREAM (p, p_cfg->flags);                    /* Flags (continuation) */
630
631     /* Now, put the options */
632     if (p_cfg->mtu_present)
633     {
634         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_MTU);
635         UINT8_TO_STREAM  (p, L2CAP_CFG_MTU_OPTION_LEN);
636         UINT16_TO_STREAM (p, p_cfg->mtu);
637     }
638     if (p_cfg->flush_to_present)
639     {
640         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_FLUSH_TOUT);
641         UINT8_TO_STREAM  (p, L2CAP_CFG_FLUSH_OPTION_LEN);
642         UINT16_TO_STREAM (p, p_cfg->flush_to);
643     }
644     if (p_cfg->qos_present)
645     {
646         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_QOS);
647         UINT8_TO_STREAM  (p, L2CAP_CFG_QOS_OPTION_LEN);
648         UINT8_TO_STREAM  (p, p_cfg->qos.qos_flags);
649         UINT8_TO_STREAM  (p, p_cfg->qos.service_type);
650         UINT32_TO_STREAM (p, p_cfg->qos.token_rate);
651         UINT32_TO_STREAM (p, p_cfg->qos.token_bucket_size);
652         UINT32_TO_STREAM (p, p_cfg->qos.peak_bandwidth);
653         UINT32_TO_STREAM (p, p_cfg->qos.latency);
654         UINT32_TO_STREAM (p, p_cfg->qos.delay_variation);
655     }
656     if (p_cfg->fcr_present)
657     {
658         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_FCR);
659         UINT8_TO_STREAM  (p, L2CAP_CFG_FCR_OPTION_LEN);
660         UINT8_TO_STREAM  (p, p_cfg->fcr.mode);
661         UINT8_TO_STREAM  (p, p_cfg->fcr.tx_win_sz);
662         UINT8_TO_STREAM  (p, p_cfg->fcr.max_transmit);
663         UINT16_TO_STREAM (p, p_cfg->fcr.rtrans_tout);
664         UINT16_TO_STREAM (p, p_cfg->fcr.mon_tout);
665         UINT16_TO_STREAM (p, p_cfg->fcr.mps);
666     }
667
668     if (p_cfg->fcs_present)
669     {
670         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_FCS);
671         UINT8_TO_STREAM  (p, L2CAP_CFG_FCS_OPTION_LEN);
672         UINT8_TO_STREAM  (p, p_cfg->fcs);
673     }
674
675     if (p_cfg->ext_flow_spec_present)
676     {
677         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_EXT_FLOW);
678         UINT8_TO_STREAM  (p, L2CAP_CFG_EXT_FLOW_OPTION_LEN);
679         UINT8_TO_STREAM  (p, p_cfg->ext_flow_spec.id);
680         UINT8_TO_STREAM  (p, p_cfg->ext_flow_spec.stype);
681         UINT16_TO_STREAM (p, p_cfg->ext_flow_spec.max_sdu_size);
682         UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.sdu_inter_time);
683         UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.access_latency);
684         UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.flush_timeout);
685     }
686
687     l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
688 }
689
690 /*******************************************************************************
691 **
692 ** Function         l2cu_send_peer_config_rsp
693 **
694 ** Description      Build and send an L2CAP "configuration response" message
695 **                  to the peer.
696 **
697 ** Returns          void
698 **
699 *******************************************************************************/
700 void l2cu_send_peer_config_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
701 {
702     BT_HDR  *p_buf;
703     UINT16  cfg_len = 0;
704     UINT8   *p;
705
706     /* Create an identifier for this packet */
707     if (p_cfg->mtu_present)
708         cfg_len += L2CAP_CFG_MTU_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
709     if (p_cfg->flush_to_present)
710         cfg_len += L2CAP_CFG_FLUSH_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
711     if (p_cfg->qos_present)
712         cfg_len += L2CAP_CFG_QOS_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
713     if (p_cfg->fcr_present)
714         cfg_len += L2CAP_CFG_FCR_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
715     if (p_cfg->ext_flow_spec_present)
716         cfg_len += L2CAP_CFG_EXT_FLOW_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
717
718     if ((p_buf = l2cu_build_header (p_ccb->p_lcb, (UINT16)(L2CAP_CONFIG_RSP_LEN + cfg_len),
719                                     L2CAP_CMD_CONFIG_RSP, p_ccb->remote_id)) == NULL )
720     {
721         L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_req");
722         return;
723     }
724
725     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET+HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
726
727     UINT16_TO_STREAM (p, p_ccb->remote_cid);
728     UINT16_TO_STREAM (p, p_cfg->flags);           /* Flags (continuation) Must match request */
729     UINT16_TO_STREAM (p, p_cfg->result);
730
731     /* Now, put the options */
732     if (p_cfg->mtu_present)
733     {
734         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_MTU);
735         UINT8_TO_STREAM  (p, L2CAP_CFG_MTU_OPTION_LEN);
736         UINT16_TO_STREAM (p, p_cfg->mtu);
737     }
738     if (p_cfg->flush_to_present)
739     {
740         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_FLUSH_TOUT);
741         UINT8_TO_STREAM  (p, L2CAP_CFG_FLUSH_OPTION_LEN);
742         UINT16_TO_STREAM (p, p_cfg->flush_to);
743     }
744     if (p_cfg->qos_present)
745     {
746         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_QOS);
747         UINT8_TO_STREAM  (p, L2CAP_CFG_QOS_OPTION_LEN);
748         UINT8_TO_STREAM  (p, p_cfg->qos.qos_flags);
749         UINT8_TO_STREAM  (p, p_cfg->qos.service_type);
750         UINT32_TO_STREAM (p, p_cfg->qos.token_rate);
751         UINT32_TO_STREAM (p, p_cfg->qos.token_bucket_size);
752         UINT32_TO_STREAM (p, p_cfg->qos.peak_bandwidth);
753         UINT32_TO_STREAM (p, p_cfg->qos.latency);
754         UINT32_TO_STREAM (p, p_cfg->qos.delay_variation);
755     }
756     if (p_cfg->fcr_present)
757     {
758         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_FCR);
759         UINT8_TO_STREAM  (p, L2CAP_CFG_FCR_OPTION_LEN);
760         UINT8_TO_STREAM  (p, p_cfg->fcr.mode);
761         UINT8_TO_STREAM  (p, p_cfg->fcr.tx_win_sz);
762         UINT8_TO_STREAM  (p, p_cfg->fcr.max_transmit);
763         UINT16_TO_STREAM (p, p_ccb->our_cfg.fcr.rtrans_tout);
764         UINT16_TO_STREAM (p, p_ccb->our_cfg.fcr.mon_tout);
765         UINT16_TO_STREAM (p, p_cfg->fcr.mps);
766     }
767
768     if (p_cfg->ext_flow_spec_present)
769     {
770         UINT8_TO_STREAM  (p, L2CAP_CFG_TYPE_EXT_FLOW);
771         UINT8_TO_STREAM  (p, L2CAP_CFG_EXT_FLOW_OPTION_LEN);
772         UINT8_TO_STREAM  (p, p_cfg->ext_flow_spec.id);
773         UINT8_TO_STREAM  (p, p_cfg->ext_flow_spec.stype);
774         UINT16_TO_STREAM (p, p_cfg->ext_flow_spec.max_sdu_size);
775         UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.sdu_inter_time);
776         UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.access_latency);
777         UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.flush_timeout);
778     }
779
780     l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
781 }
782
783 /*******************************************************************************
784 **
785 ** Function         l2cu_send_peer_config_rej
786 **
787 ** Description      Build and send an L2CAP "configuration reject" message
788 **                  to the peer.
789 **
790 ** Returns          void
791 **
792 *******************************************************************************/
793 void l2cu_send_peer_config_rej (tL2C_CCB *p_ccb, UINT8 *p_data, UINT16 data_len, UINT16 rej_len)
794 {
795     BT_HDR  *p_buf;
796     UINT16  len, cfg_len, buf_space, len1;
797     UINT8   *p, *p_hci_len, *p_data_end;
798     UINT8   cfg_code;
799
800     L2CAP_TRACE_DEBUG("l2cu_send_peer_config_rej: data_len=%d, rej_len=%d", data_len, rej_len);
801
802
803     len = BT_HDR_SIZE + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD + L2CAP_CONFIG_RSP_LEN;
804     len1 = 0xFFFF - len;
805     if (rej_len > len1)
806     {
807         L2CAP_TRACE_ERROR ("L2CAP - cfg_rej pkt size exceeds buffer design max limit.");
808         return;
809     }
810
811     p_buf = (BT_HDR *)GKI_getbuf (len + rej_len);
812
813     if (!p_buf)
814     {
815         L2CAP_TRACE_ERROR ("L2CAP - no buffer for cfg_rej");
816         return;
817     }
818
819     p_buf->offset = L2CAP_SEND_CMD_OFFSET;
820     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET;
821
822     /* Put in HCI header - handle + pkt boundary */
823 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
824     if (HCI_NON_FLUSHABLE_PB_SUPPORTED(BTM_ReadLocalFeatures ()))
825     {
826         UINT16_TO_STREAM (p, (p_ccb->p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT)));
827     }
828     else
829 #endif
830     {
831         UINT16_TO_STREAM (p, (p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT)));
832     }
833
834     /* Remember the HCI header length position, and save space for it */
835     p_hci_len = p;
836     p += 2;
837
838     /* Put in L2CAP packet header */
839     UINT16_TO_STREAM (p, L2CAP_CMD_OVERHEAD + L2CAP_CONFIG_RSP_LEN + rej_len);
840     UINT16_TO_STREAM (p, L2CAP_SIGNALLING_CID);
841
842     /* Put in L2CAP command header */
843     UINT8_TO_STREAM  (p, L2CAP_CMD_CONFIG_RSP);
844     UINT8_TO_STREAM  (p, p_ccb->remote_id);
845
846     UINT16_TO_STREAM (p, L2CAP_CONFIG_RSP_LEN + rej_len);
847
848     UINT16_TO_STREAM (p, p_ccb->remote_cid);
849     UINT16_TO_STREAM (p, 0);                    /* Flags = 0 (no continuation) */
850     UINT16_TO_STREAM (p, L2CAP_CFG_UNKNOWN_OPTIONS);
851
852     buf_space = rej_len;
853
854     /* Now, put the rejected options */
855     p_data_end = p_data + data_len;
856     while (p_data < p_data_end)
857     {
858         cfg_code = *p_data;
859         cfg_len = *(p_data + 1);
860
861         switch (cfg_code & 0x7F)
862         {
863             /* skip known options */
864             case L2CAP_CFG_TYPE_MTU:
865             case L2CAP_CFG_TYPE_FLUSH_TOUT:
866             case L2CAP_CFG_TYPE_QOS:
867                 p_data += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
868                 break;
869
870             /* unknown options; copy into rsp if not hints */
871             default:
872                 /* sanity check option length */
873                 if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= data_len)
874                 {
875                     if ((cfg_code & 0x80) == 0)
876                     {
877                         if (buf_space >= (cfg_len + L2CAP_CFG_OPTION_OVERHEAD))
878                         {
879                             memcpy(p, p_data, cfg_len + L2CAP_CFG_OPTION_OVERHEAD);
880                             p += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
881                             buf_space -= (cfg_len + L2CAP_CFG_OPTION_OVERHEAD);
882                         }
883                         else
884                         {
885                             L2CAP_TRACE_WARNING("L2CAP - cfg_rej exceeds allocated buffer");
886                             p_data = p_data_end; /* force loop exit */
887                             break;
888                         }
889                     }
890                     p_data += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
891                 }
892                 /* bad length; force loop exit */
893                 else
894                 {
895                     p_data = p_data_end;
896                 }
897                 break;
898         }
899     }
900
901     len = (UINT16) (p - p_hci_len - 2);
902     UINT16_TO_STREAM (p_hci_len, len);
903
904     p_buf->len = len + 4;
905
906     L2CAP_TRACE_DEBUG ("L2CAP - cfg_rej pkt hci_len=%d, l2cap_len=%d",
907                         len, (L2CAP_CMD_OVERHEAD+L2CAP_CONFIG_RSP_LEN+rej_len));
908
909     l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
910 }
911
912 /*******************************************************************************
913 **
914 ** Function         l2cu_send_peer_disc_req
915 **
916 ** Description      Build and send an L2CAP "disconnect request" message
917 **                  to the peer.
918 **
919 ** Returns          void
920 **
921 *******************************************************************************/
922 void l2cu_send_peer_disc_req (tL2C_CCB *p_ccb)
923 {
924     BT_HDR  *p_buf, *p_buf2;
925     UINT8   *p;
926
927     /* Create an identifier for this packet */
928     p_ccb->p_lcb->id++;
929     l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
930
931     p_ccb->local_id = p_ccb->p_lcb->id;
932
933     if ((p_buf = l2cu_build_header(p_ccb->p_lcb, L2CAP_DISC_REQ_LEN, L2CAP_CMD_DISC_REQ, p_ccb->local_id)) == NULL)
934     {
935         L2CAP_TRACE_WARNING ("L2CAP - no buffer for disc_req");
936         return;
937     }
938
939     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
940
941     UINT16_TO_STREAM (p, p_ccb->remote_cid);
942     UINT16_TO_STREAM (p, p_ccb->local_cid);
943
944     /* Move all queued data packets to the LCB. In FCR mode, assume the higher
945        layer checks that all buffers are sent before disconnecting.
946     */
947     if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE)
948     {
949         while (p_ccb->xmit_hold_q.p_first)
950         {
951             p_buf2 = (BT_HDR *)GKI_dequeue (&p_ccb->xmit_hold_q);
952             l2cu_set_acl_hci_header (p_buf2, p_ccb);
953             l2c_link_check_send_pkts (p_ccb->p_lcb, p_ccb, p_buf2);
954         }
955     }
956
957     l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
958 }
959
960
961 /*******************************************************************************
962 **
963 ** Function         l2cu_send_peer_disc_rsp
964 **
965 ** Description      Build and send an L2CAP "disconnect response" message
966 **                  to the peer.
967 **
968 **                  This function is passed the parameters for the disconnect
969 **                  response instead of the CCB address, as it may be called
970 **                  to send a disconnect response when there is no CCB.
971 **
972 ** Returns          void
973 **
974 *******************************************************************************/
975 void l2cu_send_peer_disc_rsp (tL2C_LCB *p_lcb, UINT8 remote_id, UINT16 local_cid,
976                               UINT16 remote_cid)
977 {
978     BT_HDR  *p_buf;
979     UINT8   *p;
980
981     if ((p_buf=l2cu_build_header(p_lcb, L2CAP_DISC_RSP_LEN, L2CAP_CMD_DISC_RSP, remote_id)) == NULL)
982     {
983         L2CAP_TRACE_WARNING ("L2CAP - no buffer for disc_rsp");
984         return;
985     }
986
987     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
988
989     UINT16_TO_STREAM (p, local_cid);
990     UINT16_TO_STREAM (p, remote_cid);
991
992     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
993 }
994
995
996 /*******************************************************************************
997 **
998 ** Function         l2cu_send_peer_echo_req
999 **
1000 ** Description      Build and send an L2CAP "echo request" message
1001 **                  to the peer. Note that we do not currently allow
1002 **                  data in the echo request.
1003 **
1004 ** Returns          void
1005 **
1006 *******************************************************************************/
1007 void l2cu_send_peer_echo_req (tL2C_LCB *p_lcb, UINT8 *p_data, UINT16 data_len)
1008 {
1009     BT_HDR  *p_buf;
1010     UINT8   *p;
1011
1012     p_lcb->id++;
1013     l2cu_adj_id(p_lcb, L2CAP_ADJ_ZERO_ID);  /* check for wrap to '0' */
1014
1015     if ((p_buf = l2cu_build_header(p_lcb, (UINT16) (L2CAP_ECHO_REQ_LEN + data_len), L2CAP_CMD_ECHO_REQ, p_lcb->id)) == NULL)
1016     {
1017         L2CAP_TRACE_WARNING ("L2CAP - no buffer for echo_req");
1018         return;
1019     }
1020
1021     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
1022
1023     if (data_len)
1024     {
1025         ARRAY_TO_STREAM  (p, p_data, data_len);
1026     }
1027
1028     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
1029 }
1030
1031
1032 /*******************************************************************************
1033 **
1034 ** Function         l2cu_send_peer_echo_rsp
1035 **
1036 ** Description      Build and send an L2CAP "echo response" message
1037 **                  to the peer.
1038 **
1039 ** Returns          void
1040 **
1041 *******************************************************************************/
1042 void l2cu_send_peer_echo_rsp (tL2C_LCB *p_lcb, UINT8 id, UINT8 *p_data, UINT16 data_len)
1043 {
1044     BT_HDR  *p_buf;
1045     UINT8   *p;
1046     UINT16   maxlen;
1047     /* Filter out duplicate IDs or if available buffers are low (intruder checking) */
1048     if (!id || id == p_lcb->cur_echo_id)
1049     {
1050         /* Dump this request since it is illegal */
1051         L2CAP_TRACE_WARNING ("L2CAP ignoring duplicate echo request (%d)", id);
1052         return;
1053     }
1054     else
1055         p_lcb->cur_echo_id = id;
1056      /* Don't respond if we more than 10% of our buffers are used */
1057     if (GKI_poolutilization (L2CAP_CMD_POOL_ID) > 10)
1058     {
1059         L2CAP_TRACE_WARNING ("L2CAP gki pool used up to more than 10%%, ignore echo response");
1060         return;
1061     }
1062
1063     /* Don't return data if it does not fit in ACL and L2CAP MTU */
1064     maxlen = (GKI_get_pool_bufsize(L2CAP_CMD_POOL_ID) > btu_cb.hcit_acl_pkt_size) ?
1065                btu_cb.hcit_acl_data_size : (UINT16)GKI_get_pool_bufsize(L2CAP_CMD_POOL_ID);
1066     maxlen -= (UINT16)(BT_HDR_SIZE + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD +
1067                 L2CAP_CMD_OVERHEAD + L2CAP_ECHO_RSP_LEN);
1068
1069     if (data_len > maxlen)
1070         data_len = 0;
1071
1072     if ((p_buf = l2cu_build_header (p_lcb, (UINT16)(L2CAP_ECHO_RSP_LEN + data_len), L2CAP_CMD_ECHO_RSP, id)) == NULL)
1073     {
1074         L2CAP_TRACE_WARNING ("L2CAP - no buffer for echo_rsp");
1075         return;
1076     }
1077
1078     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
1079                                L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
1080
1081     if (data_len)
1082     {
1083         ARRAY_TO_STREAM  (p, p_data, data_len);
1084     }
1085
1086     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
1087 }
1088
1089 /*******************************************************************************
1090 **
1091 ** Function         l2cu_send_peer_info_req
1092 **
1093 ** Description      Build and send an L2CAP "info request" message
1094 **                  to the peer.
1095 ** Returns          void
1096 **
1097 *******************************************************************************/
1098 void l2cu_send_peer_info_req (tL2C_LCB *p_lcb, UINT16 info_type)
1099 {
1100     BT_HDR  *p_buf;
1101     UINT8   *p;
1102
1103     /* check for wrap and/or BRCM ID */
1104     p_lcb->id++;
1105     l2cu_adj_id(p_lcb, L2CAP_ADJ_ID);
1106
1107     if ((p_buf = l2cu_build_header(p_lcb, 2, L2CAP_CMD_INFO_REQ, p_lcb->id)) == NULL)
1108     {
1109         L2CAP_TRACE_WARNING ("L2CAP - no buffer for info_req");
1110         return;
1111     }
1112
1113     L2CAP_TRACE_EVENT ("l2cu_send_peer_info_req: type 0x%04x", info_type);
1114
1115     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET+HCI_DATA_PREAMBLE_SIZE +
1116         L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
1117
1118     UINT16_TO_STREAM (p, info_type);
1119
1120     p_lcb->w4_info_rsp = TRUE;
1121     btu_start_timer (&p_lcb->info_timer_entry, BTU_TTYPE_L2CAP_INFO, L2CAP_WAIT_INFO_RSP_TOUT);
1122
1123     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
1124 }
1125
1126
1127 /*******************************************************************************
1128 **
1129 ** Function         l2cu_send_peer_info_rsp
1130 **
1131 ** Description      Build and send an L2CAP "info response" message
1132 **                  to the peer.
1133 **
1134 ** Returns          void
1135 **
1136 *******************************************************************************/
1137 void l2cu_send_peer_info_rsp (tL2C_LCB *p_lcb, UINT8 remote_id, UINT16 info_type)
1138 {
1139     BT_HDR  *p_buf;
1140     UINT8   *p;
1141     UINT16   len = L2CAP_INFO_RSP_LEN;
1142
1143 #if (L2CAP_CONFORMANCE_TESTING == TRUE)
1144     if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
1145         && (l2cb.test_info_resp & (L2CAP_EXTFEA_ENH_RETRANS   | L2CAP_EXTFEA_STREAM_MODE |
1146                                    L2CAP_EXTFEA_NO_CRC | L2CAP_EXTFEA_EXT_FLOW_SPEC |
1147                                    L2CAP_EXTFEA_FIXED_CHNLS | L2CAP_EXTFEA_EXT_WINDOW |
1148                                    L2CAP_EXTFEA_UCD_RECEPTION )) )
1149 #else
1150     if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
1151         && (L2CAP_EXTFEA_SUPPORTED_MASK & (L2CAP_EXTFEA_ENH_RETRANS | L2CAP_EXTFEA_STREAM_MODE |
1152                                            L2CAP_EXTFEA_NO_CRC |L2CAP_EXTFEA_FIXED_CHNLS |
1153                                            L2CAP_EXTFEA_UCD_RECEPTION )) )
1154 #endif
1155     {
1156         len += L2CAP_EXTENDED_FEATURES_ARRAY_SIZE;
1157     }
1158     else if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE)
1159     {
1160         len += L2CAP_FIXED_CHNL_ARRAY_SIZE;
1161     }
1162     else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE)
1163     {
1164         len += L2CAP_CONNLESS_MTU_INFO_SIZE;
1165     }
1166
1167     if ((p_buf = l2cu_build_header(p_lcb, len, L2CAP_CMD_INFO_RSP, remote_id)) == NULL)
1168     {
1169         L2CAP_TRACE_WARNING ("L2CAP - no buffer for info_rsp");
1170         return;
1171     }
1172
1173     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
1174                                L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
1175
1176     UINT16_TO_STREAM (p, info_type);
1177
1178 #if (L2CAP_CONFORMANCE_TESTING == TRUE)
1179     if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
1180         && (l2cb.test_info_resp & ( L2CAP_EXTFEA_ENH_RETRANS | L2CAP_EXTFEA_STREAM_MODE
1181                                   | L2CAP_EXTFEA_UCD_RECEPTION )) )
1182 #else
1183     if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
1184         && (L2CAP_EXTFEA_SUPPORTED_MASK & ( L2CAP_EXTFEA_ENH_RETRANS | L2CAP_EXTFEA_STREAM_MODE
1185                                           | L2CAP_EXTFEA_UCD_RECEPTION )) )
1186 #endif
1187     {
1188         UINT16_TO_STREAM (p, L2CAP_INFO_RESP_RESULT_SUCCESS);
1189 #if (BLE_INCLUDED == TRUE)
1190         if (p_lcb->transport == BT_TRANSPORT_LE)
1191         {
1192             /* optional data are not added for now */
1193             UINT32_TO_STREAM (p, L2CAP_BLE_EXTFEA_MASK);
1194         }
1195         else
1196 #endif
1197         {
1198 #if L2CAP_CONFORMANCE_TESTING == TRUE
1199         UINT32_TO_STREAM (p, l2cb.test_info_resp);
1200 #else
1201 #if (L2CAP_NUM_FIXED_CHNLS > 0)
1202         UINT32_TO_STREAM (p, L2CAP_EXTFEA_SUPPORTED_MASK | L2CAP_EXTFEA_FIXED_CHNLS);
1203 #else
1204         UINT32_TO_STREAM (p, L2CAP_EXTFEA_SUPPORTED_MASK);
1205 #endif
1206 #endif
1207         }
1208     }
1209     else if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE)
1210     {
1211         UINT16_TO_STREAM (p, L2CAP_INFO_RESP_RESULT_SUCCESS);
1212         memset (p, 0, L2CAP_FIXED_CHNL_ARRAY_SIZE);
1213
1214         p[0] = L2CAP_FIXED_CHNL_SIG_BIT;
1215
1216         if ( L2CAP_EXTFEA_SUPPORTED_MASK & L2CAP_EXTFEA_UCD_RECEPTION )
1217                 p[0] |= L2CAP_FIXED_CHNL_CNCTLESS_BIT;
1218
1219 #if (L2CAP_NUM_FIXED_CHNLS > 0)
1220         {
1221             int xx;
1222
1223             for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++)
1224                 if (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL)
1225                     p[0] |= 1 << (xx + L2CAP_FIRST_FIXED_CHNL);
1226         }
1227 #endif
1228     }
1229     else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE)
1230     {
1231         UINT16_TO_STREAM (p, L2CAP_INFO_RESP_RESULT_SUCCESS);
1232         UINT16_TO_STREAM (p, L2CAP_UCD_MTU);
1233     }
1234     else
1235     {
1236         UINT16_TO_STREAM (p, L2CAP_INFO_RESP_RESULT_NOT_SUPPORTED);  /* 'not supported' */
1237     }
1238
1239     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
1240 }
1241
1242 /******************************************************************************
1243 **
1244 ** Function         l2cu_enqueue_ccb
1245 **
1246 ** Description      queue CCB by priority. The first CCB is highest priority and
1247 **                  is served at first. The CCB is queued to an LLCB or an LCB.
1248 **
1249 ** Returns          None
1250 **
1251 *******************************************************************************/
1252 void l2cu_enqueue_ccb (tL2C_CCB *p_ccb)
1253 {
1254     tL2C_CCB        *p_ccb1;
1255     tL2C_CCB_Q      *p_q = NULL;
1256
1257     /* Find out which queue the channel is on
1258     */
1259     if (p_ccb->p_lcb != NULL)
1260         p_q = &p_ccb->p_lcb->ccb_queue;
1261
1262     if ( (!p_ccb->in_use) || (p_q == NULL) )
1263     {
1264         L2CAP_TRACE_ERROR ("l2cu_enqueue_ccb  CID: 0x%04x ERROR in_use: %u  p_lcb: 0x%08x",
1265                             p_ccb->local_cid, p_ccb->in_use, p_ccb->p_lcb);
1266         return;
1267     }
1268
1269     L2CAP_TRACE_DEBUG ("l2cu_enqueue_ccb CID: 0x%04x  priority: %d",
1270                         p_ccb->local_cid, p_ccb->ccb_priority);
1271
1272     /* If the queue is empty, we go at the front */
1273     if (!p_q->p_first_ccb)
1274     {
1275         p_q->p_first_ccb  = p_q->p_last_ccb   = p_ccb;
1276         p_ccb->p_next_ccb = p_ccb->p_prev_ccb = NULL;
1277     }
1278     else
1279     {
1280         p_ccb1 = p_q->p_first_ccb;
1281
1282         while (p_ccb1 != NULL)
1283         {
1284             /* Insert new ccb at the end of the same priority. Lower number, higher priority */
1285             if (p_ccb->ccb_priority < p_ccb1->ccb_priority)
1286             {
1287                 /* Are we at the head of the queue ? */
1288                 if (p_ccb1 == p_q->p_first_ccb)
1289                     p_q->p_first_ccb = p_ccb;
1290                 else
1291                     p_ccb1->p_prev_ccb->p_next_ccb  = p_ccb;
1292
1293                 p_ccb->p_next_ccb  = p_ccb1;
1294                 p_ccb->p_prev_ccb  = p_ccb1->p_prev_ccb;
1295                 p_ccb1->p_prev_ccb = p_ccb;
1296                 break;
1297             }
1298
1299             p_ccb1 = p_ccb1->p_next_ccb;
1300         }
1301
1302         /* If we are lower then anyone in the list, we go at the end */
1303         if (!p_ccb1)
1304         {
1305             /* add new ccb at the end of the list */
1306             p_q->p_last_ccb->p_next_ccb = p_ccb;
1307
1308             p_ccb->p_next_ccb   = NULL;
1309             p_ccb->p_prev_ccb   = p_q->p_last_ccb;
1310             p_q->p_last_ccb = p_ccb;
1311         }
1312     }
1313
1314 #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
1315     /* Adding CCB into round robin service table of its LCB */
1316     if (p_ccb->p_lcb != NULL)
1317     {
1318         /* if this is the first channel in this priority group */
1319         if (p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb == 0 )
1320         {
1321                 /* Set the first channel to this CCB */
1322             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = p_ccb;
1323                 /* Set the next serving channel in this group to this CCB */
1324             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb = p_ccb;
1325                 /* Initialize quota of this priority group based on its priority */
1326             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].quota = L2CAP_GET_PRIORITY_QUOTA(p_ccb->ccb_priority);
1327         }
1328         /* increase number of channels in this group */
1329         p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb++;
1330     }
1331 #endif
1332
1333 }
1334
1335 /******************************************************************************
1336 **
1337 ** Function         l2cu_dequeue_ccb
1338 **
1339 ** Description      dequeue CCB from a queue
1340 **
1341 ** Returns          -
1342 **
1343 *******************************************************************************/
1344 void l2cu_dequeue_ccb (tL2C_CCB *p_ccb)
1345 {
1346     tL2C_CCB_Q      *p_q = NULL;
1347
1348     L2CAP_TRACE_DEBUG ("l2cu_dequeue_ccb  CID: 0x%04x", p_ccb->local_cid);
1349
1350     /* Find out which queue the channel is on
1351     */
1352     if (p_ccb->p_lcb != NULL)
1353         p_q = &p_ccb->p_lcb->ccb_queue;
1354
1355     if ( (!p_ccb->in_use) || (p_q == NULL) || (p_q->p_first_ccb == NULL) )
1356     {
1357         L2CAP_TRACE_ERROR ("l2cu_dequeue_ccb  CID: 0x%04x ERROR in_use: %u  p_lcb: 0x%08x  p_q: 0x%08x  p_q->p_first_ccb: 0x%08x",
1358                             p_ccb->local_cid, p_ccb->in_use, p_ccb->p_lcb, p_q, p_q ? p_q->p_first_ccb : 0);
1359         return;
1360     }
1361
1362 #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
1363     /* Removing CCB from round robin service table of its LCB */
1364     if (p_ccb->p_lcb != NULL)
1365     {
1366         /* decrease number of channels in this priority group */
1367         p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb--;
1368
1369         /* if it was the last channel in the priority group */
1370         if (p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb == 0 )
1371         {
1372             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = NULL;
1373             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb = NULL;
1374         }
1375         else
1376         {
1377             /* if it is the first channel of this group */
1378             if ( p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb == p_ccb )
1379             {
1380                 p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = p_ccb->p_next_ccb;
1381             }
1382             /* if it is the next serving channel of this group */
1383             if ( p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb == p_ccb )
1384             {
1385                 /* simply, start serving from the first channel */
1386                 p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb
1387                     = p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb;
1388             }
1389         }
1390     }
1391 #endif
1392
1393     if (p_ccb == p_q->p_first_ccb)
1394     {
1395         /* We are removing the first in a queue */
1396         p_q->p_first_ccb = p_ccb->p_next_ccb;
1397
1398         if (p_q->p_first_ccb)
1399             p_q->p_first_ccb->p_prev_ccb = NULL;
1400         else
1401             p_q->p_last_ccb = NULL;
1402     }
1403     else if (p_ccb == p_q->p_last_ccb)
1404     {
1405         /* We are removing the last in a queue */
1406         p_q->p_last_ccb = p_ccb->p_prev_ccb;
1407         p_q->p_last_ccb->p_next_ccb = NULL;
1408     }
1409     else
1410     {
1411         /* In the middle of a chain. */
1412         p_ccb->p_prev_ccb->p_next_ccb = p_ccb->p_next_ccb;
1413         p_ccb->p_next_ccb->p_prev_ccb = p_ccb->p_prev_ccb;
1414     }
1415
1416     p_ccb->p_next_ccb = p_ccb->p_prev_ccb = NULL;
1417 }
1418
1419 /******************************************************************************
1420 **
1421 ** Function         l2cu_change_pri_ccb
1422 **
1423 ** Description
1424 **
1425 ** Returns          -
1426 **
1427 *******************************************************************************/
1428 void l2cu_change_pri_ccb (tL2C_CCB *p_ccb, tL2CAP_CHNL_PRIORITY priority)
1429 {
1430     if (p_ccb->ccb_priority != priority)
1431     {
1432         /* If CCB is not the only guy on the queue */
1433         if ( (p_ccb->p_next_ccb != NULL) || (p_ccb->p_prev_ccb != NULL) )
1434         {
1435             L2CAP_TRACE_DEBUG ("Update CCB list in logical link");
1436
1437             /* Remove CCB from queue and re-queue it at new priority */
1438             l2cu_dequeue_ccb (p_ccb);
1439
1440             p_ccb->ccb_priority = priority;
1441             l2cu_enqueue_ccb (p_ccb);
1442         }
1443 #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
1444         else
1445         {
1446                 /* If CCB is the only guy on the queue, no need to re-enqueue */
1447             /* update only round robin service data */
1448             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb = 0;
1449             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = NULL;
1450             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb = NULL;
1451
1452             p_ccb->ccb_priority = priority;
1453
1454             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = p_ccb;
1455             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb = p_ccb;
1456             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].quota = L2CAP_GET_PRIORITY_QUOTA(p_ccb->ccb_priority);
1457             p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb = 1;
1458         }
1459 #endif
1460     }
1461 }
1462
1463 /*******************************************************************************
1464 **
1465 ** Function         l2cu_allocate_ccb
1466 **
1467 ** Description      This function allocates a Channel Control Block and
1468 **                  attaches it to a link control block. The local CID
1469 **                  is also assigned.
1470 **
1471 ** Returns          pointer to CCB, or NULL if none
1472 **
1473 *******************************************************************************/
1474 tL2C_CCB *l2cu_allocate_ccb (tL2C_LCB *p_lcb, UINT16 cid)
1475 {
1476     tL2C_CCB    *p_ccb;
1477     tL2C_CCB    *p_prev;
1478
1479     L2CAP_TRACE_DEBUG ("l2cu_allocate_ccb: cid 0x%04x", cid);
1480
1481     if (!l2cb.p_free_ccb_first)
1482         return (NULL);
1483
1484     /* If a CID was passed in, use that, else take the first free one */
1485     if (cid == 0)
1486     {
1487         p_ccb = l2cb.p_free_ccb_first;
1488         l2cb.p_free_ccb_first = p_ccb->p_next_ccb;
1489     }
1490     else
1491     {
1492         p_prev = NULL;
1493
1494         p_ccb = &l2cb.ccb_pool[cid - L2CAP_BASE_APPL_CID];
1495
1496         if (p_ccb == l2cb.p_free_ccb_first)
1497             l2cb.p_free_ccb_first = p_ccb->p_next_ccb;
1498         else
1499         {
1500             for (p_prev = l2cb.p_free_ccb_first; p_prev != NULL; p_prev = p_prev->p_next_ccb)
1501             {
1502                 if (p_prev->p_next_ccb == p_ccb)
1503                 {
1504                     p_prev->p_next_ccb = p_ccb->p_next_ccb;
1505
1506                     if (p_ccb == l2cb.p_free_ccb_last)
1507                         l2cb.p_free_ccb_last = p_prev;
1508
1509                     break;
1510                 }
1511             }
1512             if (p_prev == NULL)
1513             {
1514                 L2CAP_TRACE_ERROR ("l2cu_allocate_ccb: could not find CCB for CID 0x%04x in the free list", cid);
1515                 return NULL;
1516             }
1517         }
1518     }
1519
1520     p_ccb->p_next_ccb = p_ccb->p_prev_ccb = NULL;
1521
1522     p_ccb->in_use = TRUE;
1523
1524     /* Get a CID for the connection */
1525     p_ccb->local_cid = L2CAP_BASE_APPL_CID + (UINT16)(p_ccb - l2cb.ccb_pool);
1526
1527     p_ccb->p_lcb = p_lcb;
1528     p_ccb->p_rcb = NULL;
1529
1530     /* Set priority then insert ccb into LCB queue (if we have an LCB) */
1531     p_ccb->ccb_priority = L2CAP_CHNL_PRIORITY_LOW;
1532
1533     if (p_lcb)
1534         l2cu_enqueue_ccb (p_ccb);
1535
1536     /* clear what peer wants to configure */
1537     p_ccb->peer_cfg_bits = 0;
1538
1539     /* Put in default values for configuration */
1540     memset (&p_ccb->our_cfg, 0, sizeof(tL2CAP_CFG_INFO));
1541     memset (&p_ccb->peer_cfg, 0, sizeof(tL2CAP_CFG_INFO));
1542
1543     /* Put in default values for local/peer configurations */
1544     p_ccb->our_cfg.flush_to              = p_ccb->peer_cfg.flush_to              = L2CAP_DEFAULT_FLUSH_TO;
1545     p_ccb->our_cfg.mtu                   = p_ccb->peer_cfg.mtu                   = L2CAP_DEFAULT_MTU;
1546     p_ccb->our_cfg.qos.service_type      = p_ccb->peer_cfg.qos.service_type      = L2CAP_DEFAULT_SERV_TYPE;
1547     p_ccb->our_cfg.qos.token_rate        = p_ccb->peer_cfg.qos.token_rate        = L2CAP_DEFAULT_TOKEN_RATE;
1548     p_ccb->our_cfg.qos.token_bucket_size = p_ccb->peer_cfg.qos.token_bucket_size = L2CAP_DEFAULT_BUCKET_SIZE;
1549     p_ccb->our_cfg.qos.peak_bandwidth    = p_ccb->peer_cfg.qos.peak_bandwidth    = L2CAP_DEFAULT_PEAK_BANDWIDTH;
1550     p_ccb->our_cfg.qos.latency           = p_ccb->peer_cfg.qos.latency           = L2CAP_DEFAULT_LATENCY;
1551     p_ccb->our_cfg.qos.delay_variation   = p_ccb->peer_cfg.qos.delay_variation   = L2CAP_DEFAULT_DELAY;
1552
1553     p_ccb->bypass_fcs = 0;
1554     memset (&p_ccb->ertm_info, 0, sizeof(tL2CAP_ERTM_INFO));
1555     p_ccb->peer_cfg_already_rejected = FALSE;
1556     p_ccb->fcr_cfg_tries         = L2CAP_MAX_FCR_CFG_TRIES;
1557     p_ccb->fcrb.ack_timer.param  = (TIMER_PARAM_TYPE)p_ccb;
1558
1559     /* if timer is running, remove it from timer list */
1560     if (p_ccb->fcrb.ack_timer.in_use)
1561         btu_stop_quick_timer (&p_ccb->fcrb.ack_timer);
1562
1563     p_ccb->fcrb.mon_retrans_timer.param  = (TIMER_PARAM_TYPE)p_ccb;
1564
1565 // btla-specific ++
1566    /*  CSP408639 Fix: When L2CAP send amp move channel request or receive
1567      * L2CEVT_AMP_MOVE_REQ do following sequence. Send channel move
1568      * request -> Stop retrans/monitor timer -> Change channel state to CST_AMP_MOVING. */
1569    if (p_ccb->fcrb.mon_retrans_timer.in_use)
1570          btu_stop_quick_timer (&p_ccb->fcrb.mon_retrans_timer);
1571 // btla-specific --
1572
1573     l2c_fcr_stop_timer (p_ccb);
1574
1575     p_ccb->ertm_info.preferred_mode  = L2CAP_FCR_BASIC_MODE;        /* Default mode for channel is basic mode */
1576     p_ccb->ertm_info.allowed_modes   = L2CAP_FCR_CHAN_OPT_BASIC;    /* Default mode for channel is basic mode */
1577     p_ccb->ertm_info.fcr_rx_pool_id  = L2CAP_FCR_RX_POOL_ID;
1578     p_ccb->ertm_info.fcr_tx_pool_id  = L2CAP_FCR_TX_POOL_ID;
1579     p_ccb->ertm_info.user_rx_pool_id = HCI_ACL_POOL_ID;
1580     p_ccb->ertm_info.user_tx_pool_id = HCI_ACL_POOL_ID;
1581     p_ccb->max_rx_mtu                = L2CAP_MTU_SIZE;
1582     p_ccb->tx_mps                    = GKI_get_pool_bufsize(HCI_ACL_POOL_ID) - 32;
1583
1584     GKI_init_q (&p_ccb->xmit_hold_q);
1585
1586     p_ccb->cong_sent    = FALSE;
1587     p_ccb->buff_quota   = 2;                /* This gets set after config */
1588
1589     /* If CCB was reserved Config_Done can already have some value */
1590     if (cid == 0)
1591         p_ccb->config_done  = 0;
1592     else
1593     {
1594         L2CAP_TRACE_DEBUG ("l2cu_allocate_ccb: cid 0x%04x config_done:0x%x", cid, p_ccb->config_done);
1595     }
1596
1597     p_ccb->chnl_state   = CST_CLOSED;
1598     p_ccb->flags        = 0;
1599     p_ccb->tx_data_rate = L2CAP_CHNL_DATA_RATE_LOW;
1600     p_ccb->rx_data_rate = L2CAP_CHNL_DATA_RATE_LOW;
1601
1602 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
1603     p_ccb->is_flushable = FALSE;
1604 #endif
1605
1606     p_ccb->timer_entry.param = (TIMER_PARAM_TYPE)p_ccb;
1607     p_ccb->timer_entry.in_use = 0;
1608
1609     l2c_link_adjust_chnl_allocation ();
1610
1611     return (p_ccb);
1612 }
1613
1614 /*******************************************************************************
1615 **
1616 ** Function         l2cu_start_post_bond_timer
1617 **
1618 ** Description      This function starts the ACL Link inactivity timer after
1619 **                  dedicated bonding
1620 **                  This timer can be longer than the normal link inactivity
1621 **                  timer for some platforms.
1622 **
1623 ** Returns          BOOLEAN  - TRUE if idle timer started or disconnect initiated
1624 **                             FALSE if there's one or more pending CCB's exist
1625 **
1626 *******************************************************************************/
1627 BOOLEAN l2cu_start_post_bond_timer (UINT16 handle)
1628 {
1629     UINT16    timeout;
1630     tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
1631
1632     if (!p_lcb)
1633         return (TRUE);
1634
1635     p_lcb->is_bonding = FALSE;
1636
1637     /* Only start timer if no control blocks allocated */
1638     if (p_lcb->ccb_queue.p_first_ccb != NULL)
1639         return (FALSE);
1640
1641     /* If no channels on the connection, start idle timeout */
1642     if ( (p_lcb->link_state == LST_CONNECTED) || (p_lcb->link_state == LST_CONNECTING) || (p_lcb->link_state == LST_DISCONNECTING) )
1643     {
1644         if (p_lcb->idle_timeout == 0)
1645         {
1646             if (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER))
1647             {
1648                 p_lcb->link_state = LST_DISCONNECTING;
1649                 timeout = L2CAP_LINK_DISCONNECT_TOUT;
1650             }
1651             else
1652                 timeout = BT_1SEC_TIMEOUT;
1653         }
1654         else
1655         {
1656             timeout = L2CAP_BONDING_TIMEOUT;
1657         }
1658
1659         if (timeout != 0xFFFF)
1660             btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, timeout);
1661
1662         return (TRUE);
1663     }
1664
1665     return (FALSE);
1666 }
1667
1668 /*******************************************************************************
1669 **
1670 ** Function         l2cu_release_ccb
1671 **
1672 ** Description      This function releases a Channel Control Block. The timer
1673 **                  is stopped, any attached buffers freed, and the CCB is removed
1674 **                  from the link control block.
1675 **
1676 ** Returns          void
1677 **
1678 *******************************************************************************/
1679 void l2cu_release_ccb (tL2C_CCB *p_ccb)
1680 {
1681     tL2C_LCB    *p_lcb = p_ccb->p_lcb;
1682     tL2C_RCB    *p_rcb = p_ccb->p_rcb;
1683
1684     L2CAP_TRACE_DEBUG ("l2cu_release_ccb: cid 0x%04x  in_use: %u", p_ccb->local_cid, p_ccb->in_use);
1685
1686     /* If already released, could be race condition */
1687     if (!p_ccb->in_use)
1688         return;
1689
1690     if (p_rcb && (p_rcb->psm != p_rcb->real_psm))
1691     {
1692         btm_sec_clr_service_by_psm(p_rcb->psm);
1693     }
1694
1695     btm_sec_clr_temp_auth_service (p_lcb->remote_bd_addr);
1696
1697     /* Stop the timer */
1698     btu_stop_timer (&p_ccb->timer_entry);
1699
1700     while (p_ccb->xmit_hold_q.p_first)
1701         GKI_freebuf (GKI_dequeue (&p_ccb->xmit_hold_q));
1702
1703     l2c_fcr_cleanup (p_ccb);
1704
1705     /* Channel may not be assigned to any LCB if it was just pre-reserved */
1706     if ( (p_lcb) &&
1707          ( (p_ccb->local_cid >= L2CAP_BASE_APPL_CID)
1708 #if (L2CAP_UCD_INCLUDED == TRUE)
1709          ||(p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID)
1710 #endif
1711          )
1712        )
1713     {
1714         l2cu_dequeue_ccb (p_ccb);
1715
1716         /* Delink the CCB from the LCB */
1717         p_ccb->p_lcb = NULL;
1718     }
1719
1720     /* Put the CCB back on the free pool */
1721     if (!l2cb.p_free_ccb_first)
1722     {
1723         l2cb.p_free_ccb_first = p_ccb;
1724         l2cb.p_free_ccb_last  = p_ccb;
1725         p_ccb->p_next_ccb     = NULL;
1726         p_ccb->p_prev_ccb     = NULL;
1727     }
1728     else
1729     {
1730         p_ccb->p_next_ccb  = NULL;
1731         p_ccb->p_prev_ccb  = l2cb.p_free_ccb_last;
1732         l2cb.p_free_ccb_last->p_next_ccb = p_ccb;
1733         l2cb.p_free_ccb_last  = p_ccb;
1734     }
1735
1736     /* Flag as not in use */
1737     p_ccb->in_use = FALSE;
1738
1739     /* If no channels on the connection, start idle timeout */
1740     if ((p_lcb) && p_lcb->in_use && (p_lcb->link_state == LST_CONNECTED))
1741     {
1742         if (!p_lcb->ccb_queue.p_first_ccb)
1743         {
1744             l2cu_no_dynamic_ccbs (p_lcb);
1745         }
1746         else
1747         {
1748             /* Link is still active, adjust channel quotas. */
1749             l2c_link_adjust_chnl_allocation ();
1750         }
1751     }
1752 }
1753
1754 /*******************************************************************************
1755 **
1756 ** Function         l2cu_find_ccb_by_remote_cid
1757 **
1758 ** Description      Look through all active CCBs on a link for a match based
1759 **                  on the remote CID.
1760 **
1761 ** Returns          pointer to matched CCB, or NULL if no match
1762 **
1763 *******************************************************************************/
1764 tL2C_CCB *l2cu_find_ccb_by_remote_cid (tL2C_LCB *p_lcb, UINT16 remote_cid)
1765 {
1766     tL2C_CCB    *p_ccb;
1767
1768     /* If LCB is NULL, look through all active links */
1769     if (!p_lcb)
1770     {
1771         return NULL;
1772     }
1773     else
1774     {
1775         for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
1776             if ((p_ccb->in_use) && (p_ccb->remote_cid == remote_cid))
1777                 return (p_ccb);
1778     }
1779
1780     /* If here, no match found */
1781     return (NULL);
1782 }
1783
1784 /*******************************************************************************
1785 **
1786 ** Function         l2cu_allocate_rcb
1787 **
1788 ** Description      Look through the Registration Control Blocks for a free
1789 **                  one.
1790 **
1791 ** Returns          Pointer to the RCB or NULL if not found
1792 **
1793 *******************************************************************************/
1794 tL2C_RCB *l2cu_allocate_rcb (UINT16 psm)
1795 {
1796     tL2C_RCB    *p_rcb = &l2cb.rcb_pool[0];
1797     UINT16      xx;
1798
1799     for (xx = 0; xx < MAX_L2CAP_CLIENTS; xx++, p_rcb++)
1800     {
1801         if (!p_rcb->in_use)
1802         {
1803             p_rcb->in_use = TRUE;
1804             p_rcb->psm    = psm;
1805 #if (L2CAP_UCD_INCLUDED == TRUE)
1806             p_rcb->ucd.state = L2C_UCD_STATE_UNUSED;
1807 #endif
1808             return (p_rcb);
1809         }
1810     }
1811
1812     /* If here, no free RCB found */
1813     return (NULL);
1814 }
1815
1816
1817 /*******************************************************************************
1818 **
1819 ** Function         l2cu_release_rcb
1820 **
1821 ** Description      Mark an RCB as no longet in use
1822 **
1823 ** Returns          void
1824 **
1825 *******************************************************************************/
1826 void l2cu_release_rcb (tL2C_RCB *p_rcb)
1827 {
1828     p_rcb->in_use = FALSE;
1829     p_rcb->psm    = 0;
1830 }
1831
1832
1833 /*******************************************************************************
1834 **
1835 ** Function         l2cu_disconnect_chnl
1836 **
1837 ** Description      Disconnect a channel. Typically, this is due to either
1838 **                  receiving a bad configuration,  bad packet or max_retries expiring.
1839 **
1840 *******************************************************************************/
1841 void l2cu_disconnect_chnl (tL2C_CCB *p_ccb)
1842 {
1843     UINT16      local_cid = p_ccb->local_cid;
1844
1845     if (local_cid >= L2CAP_BASE_APPL_CID)
1846     {
1847         tL2CA_DISCONNECT_IND_CB   *p_disc_cb = p_ccb->p_rcb->api.pL2CA_DisconnectInd_Cb;
1848
1849         L2CAP_TRACE_WARNING ("L2CAP - disconnect_chnl CID: 0x%04x", local_cid);
1850
1851         l2cu_send_peer_disc_req (p_ccb);
1852
1853         l2cu_release_ccb (p_ccb);
1854
1855         (*p_disc_cb)(local_cid, FALSE);
1856     }
1857     else
1858     {
1859         /* failure on the AMP channel, probably need to disconnect ACL */
1860         L2CAP_TRACE_ERROR ("L2CAP - disconnect_chnl CID: 0x%04x Ignored", local_cid);
1861     }
1862 }
1863
1864
1865 /*******************************************************************************
1866 **
1867 ** Function         l2cu_find_rcb_by_psm
1868 **
1869 ** Description      Look through the Registration Control Blocks to see if
1870 **                  anyone registered to handle the PSM in question
1871 **
1872 ** Returns          Pointer to the RCB or NULL if not found
1873 **
1874 *******************************************************************************/
1875 tL2C_RCB *l2cu_find_rcb_by_psm (UINT16 psm)
1876 {
1877     tL2C_RCB    *p_rcb = &l2cb.rcb_pool[0];
1878     UINT16      xx;
1879
1880     for (xx = 0; xx < MAX_L2CAP_CLIENTS; xx++, p_rcb++)
1881     {
1882         if ((p_rcb->in_use) && (p_rcb->psm == psm))
1883             return (p_rcb);
1884     }
1885
1886     /* If here, no match found */
1887     return (NULL);
1888 }
1889
1890
1891 /*******************************************************************************
1892 **
1893 ** Function         l2cu_process_peer_cfg_req
1894 **
1895 ** Description      This function is called when the peer sends us a "config request"
1896 **                  message. It extracts the configuration of interest and saves
1897 **                  it in the CCB.
1898 **
1899 **                  Note:  Negotiation of the FCR channel type is handled internally,
1900 **                         all others are passed to the upper layer.
1901 **
1902 ** Returns          UINT8 - L2CAP_PEER_CFG_OK if passed to upper layer,
1903 **                          L2CAP_PEER_CFG_UNACCEPTABLE if automatically responded to
1904 **                              because parameters are unnacceptable from a specification
1905 **                              point of view.
1906 **                          L2CAP_PEER_CFG_DISCONNECT if no compatible channel modes
1907 **                              between the two devices, and shall be closed.
1908 **
1909 *******************************************************************************/
1910 UINT8 l2cu_process_peer_cfg_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
1911 {
1912     BOOLEAN  mtu_ok      = TRUE;
1913     BOOLEAN  qos_type_ok = TRUE;
1914     BOOLEAN  flush_to_ok = TRUE;
1915     BOOLEAN  fcr_ok      = TRUE;
1916     UINT8    fcr_status;
1917
1918     /* Ignore FCR parameters for basic mode */
1919     if (!p_cfg->fcr_present)
1920         p_cfg->fcr.mode = L2CAP_FCR_BASIC_MODE;
1921
1922     /* Save the MTU that our peer can receive */
1923     if (p_cfg->mtu_present)
1924     {
1925         /* Make sure MTU is at least the minimum */
1926         if (p_cfg->mtu >= L2CAP_MIN_MTU)
1927         {
1928             /* In basic mode, limit the MTU to our buffer size */
1929             if ( (p_cfg->fcr_present == FALSE) && (p_cfg->mtu > L2CAP_MTU_SIZE) )
1930                 p_cfg->mtu = L2CAP_MTU_SIZE;
1931
1932             /* Save the accepted value in case of renegotiation */
1933             p_ccb->peer_cfg.mtu = p_cfg->mtu;
1934             p_ccb->peer_cfg.mtu_present = TRUE;
1935             p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_MTU;
1936         }
1937         else    /* Illegal MTU value */
1938         {
1939             p_cfg->mtu = L2CAP_MIN_MTU;
1940             mtu_ok     = FALSE;
1941         }
1942     }
1943     /* Reload mtu from a previously accepted config request */
1944     else if (p_ccb->peer_cfg.mtu_present)
1945     {
1946         p_cfg->mtu_present = TRUE;
1947         p_cfg->mtu = p_ccb->peer_cfg.mtu;
1948     }
1949
1950     /* Verify that the flush timeout is a valid value (0 is illegal) */
1951     if (p_cfg->flush_to_present)
1952     {
1953         if (!p_cfg->flush_to)
1954         {
1955             p_cfg->flush_to = 0xFFFF;   /* Infinite retransmissions (spec default) */
1956             flush_to_ok     = FALSE;
1957         }
1958         else    /* Save the accepted value in case of renegotiation */
1959         {
1960             p_ccb->peer_cfg.flush_to_present = TRUE;
1961             p_ccb->peer_cfg.flush_to = p_cfg->flush_to;
1962             p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_FLUSH_TO;
1963         }
1964     }
1965     /* Reload flush_to from a previously accepted config request */
1966     else if (p_ccb->peer_cfg.flush_to_present)
1967     {
1968         p_cfg->flush_to_present = TRUE;
1969         p_cfg->flush_to = p_ccb->peer_cfg.flush_to;
1970     }
1971
1972     /* Save the QOS settings the the peer is using */
1973     if (p_cfg->qos_present)
1974     {
1975         /* Make sure service type is not a reserved value; otherwise let upper
1976            layer decide if acceptable
1977         */
1978         if (p_cfg->qos.service_type <= GUARANTEED)
1979         {
1980             p_ccb->peer_cfg.qos         = p_cfg->qos;
1981             p_ccb->peer_cfg.qos_present = TRUE;
1982             p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_QOS;
1983         }
1984         else    /* Illegal service type value */
1985         {
1986             p_cfg->qos.service_type = BEST_EFFORT;
1987             qos_type_ok             = FALSE;
1988         }
1989     }
1990     /* Reload QOS from a previously accepted config request */
1991     else if (p_ccb->peer_cfg.qos_present)
1992     {
1993         p_cfg->qos_present = TRUE;
1994         p_cfg->qos         = p_ccb->peer_cfg.qos;
1995     }
1996
1997     if ((fcr_status = l2c_fcr_process_peer_cfg_req (p_ccb, p_cfg)) == L2CAP_PEER_CFG_DISCONNECT)
1998     {
1999         /* Notify caller to disconnect the channel (incompatible modes) */
2000         p_cfg->result = L2CAP_CFG_FAILED_NO_REASON;
2001         p_cfg->mtu_present = p_cfg->qos_present = p_cfg->flush_to_present = 0;
2002
2003         return (L2CAP_PEER_CFG_DISCONNECT);
2004     }
2005
2006     fcr_ok = (fcr_status == L2CAP_PEER_CFG_OK);
2007
2008     /* Return any unacceptable parameters */
2009     if (mtu_ok && flush_to_ok && qos_type_ok && fcr_ok)
2010     {
2011         l2cu_adjust_out_mps (p_ccb);
2012         return (L2CAP_PEER_CFG_OK);
2013     }
2014     else
2015     {
2016         p_cfg->result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
2017
2018         if (mtu_ok)
2019             p_cfg->mtu_present = FALSE;
2020         if (flush_to_ok)
2021             p_cfg->flush_to_present = FALSE;
2022         if (qos_type_ok)
2023             p_cfg->qos_present = FALSE;
2024         if (fcr_ok)
2025             p_cfg->fcr_present = FALSE;
2026
2027         return (L2CAP_PEER_CFG_UNACCEPTABLE);
2028     }
2029 }
2030
2031
2032 /*******************************************************************************
2033 **
2034 ** Function         l2cu_process_peer_cfg_rsp
2035 **
2036 ** Description      This function is called when the peer sends us a "config response"
2037 **                  message. It extracts the configuration of interest and saves
2038 **                  it in the CCB.
2039 **
2040 ** Returns          void
2041 **
2042 *******************************************************************************/
2043 void l2cu_process_peer_cfg_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
2044 {
2045     /* If we wanted QoS and the peer sends us a positive response with QoS, use his values */
2046     if ( (p_cfg->qos_present) && (p_ccb->our_cfg.qos_present) )
2047         p_ccb->our_cfg.qos = p_cfg->qos;
2048
2049     if (p_cfg->fcr_present)
2050     {
2051         /* Save the retransmission and monitor timeout values */
2052         if (p_cfg->fcr.mode == L2CAP_FCR_ERTM_MODE)
2053         {
2054             p_ccb->peer_cfg.fcr.rtrans_tout = p_cfg->fcr.rtrans_tout;
2055             p_ccb->peer_cfg.fcr.mon_tout = p_cfg->fcr.mon_tout;
2056         }
2057
2058         /* Calculate the max number of packets for which we can delay sending an ack */
2059         if (p_cfg->fcr.tx_win_sz < p_ccb->our_cfg.fcr.tx_win_sz)
2060             p_ccb->fcrb.max_held_acks = p_cfg->fcr.tx_win_sz / 3;
2061         else
2062             p_ccb->fcrb.max_held_acks = p_ccb->our_cfg.fcr.tx_win_sz / 3;
2063
2064         L2CAP_TRACE_DEBUG ("l2cu_process_peer_cfg_rsp(): peer tx_win_sz: %d, our tx_win_sz: %d, max_held_acks: %d",
2065                              p_cfg->fcr.tx_win_sz, p_ccb->our_cfg.fcr.tx_win_sz, p_ccb->fcrb.max_held_acks);
2066     }
2067 }
2068
2069 /*******************************************************************************
2070 **
2071 ** Function         l2cu_process_our_cfg_req
2072 **
2073 ** Description      This function is called when we send a "config request"
2074 **                  message. It extracts the configuration of interest and saves
2075 **                  it in the CCB.
2076 **
2077 ** Returns          void
2078 **
2079 *******************************************************************************/
2080 void l2cu_process_our_cfg_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
2081 {
2082     tL2C_LCB    *p_lcb;
2083     UINT16      hci_flush_to;
2084
2085     /* Save the QOS settings we are using for transmit */
2086     if (p_cfg->qos_present)
2087     {
2088         p_ccb->our_cfg.qos_present = TRUE;
2089         p_ccb->our_cfg.qos         = p_cfg->qos;
2090     }
2091
2092     if (p_cfg->fcr_present)
2093     {
2094         /* Override FCR options if attempting streaming or basic */
2095         if (p_cfg->fcr.mode == L2CAP_FCR_BASIC_MODE)
2096             memset(&p_cfg->fcr, 0, sizeof(tL2CAP_FCR_OPTS));
2097         else
2098         {
2099             /* On BR/EDR, timer values are zero in config request */
2100             /* On class 2 AMP, timer value in config request shall be non-0 processing time */
2101             /*                 timer value in config response shall be greater than received processing time */
2102             p_cfg->fcr.mon_tout = p_cfg->fcr.rtrans_tout = 0;
2103
2104             if (p_cfg->fcr.mode == L2CAP_FCR_STREAM_MODE)
2105                 p_cfg->fcr.max_transmit = p_cfg->fcr.tx_win_sz = 0;
2106         }
2107
2108         /* Set the threshold to send acks (may be updated in the cfg response) */
2109         p_ccb->fcrb.max_held_acks = p_cfg->fcr.tx_win_sz / 3;
2110
2111         /* Include FCS option only if peer can handle it */
2112         if (p_ccb->p_lcb->peer_ext_fea & L2CAP_EXTFEA_NO_CRC)
2113         {
2114             /* FCS check can be bypassed if peer also desires to bypass */
2115             if (p_cfg->fcs_present && p_cfg->fcs == L2CAP_CFG_FCS_BYPASS)
2116                 p_ccb->bypass_fcs |= L2CAP_CFG_FCS_OUR;
2117         }
2118         else
2119             p_cfg->fcs_present = FALSE;
2120     }
2121     else
2122     {
2123         p_cfg->fcr.mode = L2CAP_FCR_BASIC_MODE;
2124     }
2125
2126     p_ccb->our_cfg.fcr.mode    = p_cfg->fcr.mode;
2127     p_ccb->our_cfg.fcr_present = p_cfg->fcr_present;
2128
2129     /* Check the flush timeout. If it is lower than the current one used */
2130     /* then we need to adjust the flush timeout sent to the controller   */
2131     if (p_cfg->flush_to_present)
2132     {
2133         if ((p_cfg->flush_to == 0)||(p_cfg->flush_to == L2CAP_NO_AUTOMATIC_FLUSH))
2134         {
2135             /* don't send invalid flush timeout */
2136             /* SPEC: The sender of the Request shall specify its flush timeout value */
2137             /*       if it differs from the default value of 0xFFFF                  */
2138             p_cfg->flush_to_present = FALSE;
2139         }
2140         else
2141         {
2142             p_ccb->our_cfg.flush_to = p_cfg->flush_to;
2143             p_lcb = p_ccb->p_lcb;
2144
2145             if (p_cfg->flush_to < p_lcb->link_flush_tout)
2146             {
2147                 p_lcb->link_flush_tout = p_cfg->flush_to;
2148
2149                 /* If the timeout is within range of HCI, set the flush timeout */
2150                 if (p_cfg->flush_to <= ((HCI_MAX_AUTO_FLUSH_TOUT * 5) / 8))
2151                 {
2152                     /* Convert flush timeout to 0.625 ms units, with round */
2153                     hci_flush_to = ((p_cfg->flush_to * 8) + 3) / 5;
2154                     btsnd_hcic_write_auto_flush_tout (p_lcb->handle, hci_flush_to);
2155                 }
2156             }
2157         }
2158     }
2159 }
2160
2161
2162 /*******************************************************************************
2163 **
2164 ** Function         l2cu_process_our_cfg_rsp
2165 **
2166 ** Description      This function is called when we send the peer a "config response"
2167 **                  message. It extracts the configuration of interest and saves
2168 **                  it in the CCB.
2169 **
2170 ** Returns          void
2171 **
2172 *******************************************************************************/
2173 void l2cu_process_our_cfg_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
2174 {
2175     /* If peer wants QoS, we are allowed to change the values in a positive response */
2176     if ( (p_cfg->qos_present) && (p_ccb->peer_cfg.qos_present) )
2177         p_ccb->peer_cfg.qos = p_cfg->qos;
2178     else
2179         p_cfg->qos_present = FALSE;
2180
2181     l2c_fcr_adj_our_rsp_options (p_ccb, p_cfg);
2182 }
2183
2184
2185 /*******************************************************************************
2186 **
2187 ** Function         l2cu_device_reset
2188 **
2189 ** Description      This function is called when reset of the device is
2190 **                  completed.  For all active connection simulate HCI_DISC
2191 **
2192 ** Returns          void
2193 **
2194 *******************************************************************************/
2195 void l2cu_device_reset (void)
2196 {
2197     int         xx;
2198     tL2C_LCB    *p_lcb = &l2cb.lcb_pool[0];
2199
2200     for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
2201     {
2202         if ((p_lcb->in_use) && (p_lcb->handle != HCI_INVALID_HANDLE))
2203         {
2204             l2c_link_hci_disc_comp (p_lcb->handle, (UINT8) -1);
2205         }
2206     }
2207 #if (BLE_INCLUDED == TRUE)
2208     l2cb.is_ble_connecting = FALSE;
2209 #endif
2210 }
2211
2212 #if (TCS_WUG_MEMBER_INCLUDED == TRUE && TCS_INCLUDED == TRUE)
2213 extern UINT16 tcs_wug_get_clk_offset( BD_ADDR addr ) ;
2214 #endif
2215
2216 /*******************************************************************************
2217 **
2218 ** Function         l2cu_create_conn
2219 **
2220 ** Description      This function initiates an acl connection via HCI
2221 **
2222 ** Returns          TRUE if successful, FALSE if gki get buffer fails.
2223 **
2224 *******************************************************************************/
2225 BOOLEAN l2cu_create_conn (tL2C_LCB *p_lcb, tBT_TRANSPORT transport)
2226 {
2227     int             xx;
2228     tL2C_LCB        *p_lcb_cur = &l2cb.lcb_pool[0];
2229 #if BTM_SCO_INCLUDED == TRUE
2230     BOOLEAN         is_sco_active;
2231 #endif
2232
2233 #if (BLE_INCLUDED == TRUE)
2234     tBT_DEVICE_TYPE     dev_type;
2235     tBLE_ADDR_TYPE      addr_type;
2236
2237
2238     BTM_ReadDevInfo(p_lcb->remote_bd_addr, &dev_type, &addr_type);
2239
2240     if (transport == BT_TRANSPORT_LE)
2241     {
2242         if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
2243             return FALSE;
2244
2245         p_lcb->ble_addr_type = addr_type;
2246         p_lcb->transport = BT_TRANSPORT_LE;
2247
2248         return (l2cble_create_conn(p_lcb));
2249     }
2250 #endif
2251
2252     /* If there is a connection where we perform as a slave, try to switch roles
2253        for this connection */
2254     for (xx = 0, p_lcb_cur = &l2cb.lcb_pool[0]; xx < MAX_L2CAP_LINKS; xx++, p_lcb_cur++)
2255     {
2256         if (p_lcb_cur == p_lcb)
2257             continue;
2258
2259         if ((p_lcb_cur->in_use) && (p_lcb_cur->link_role == HCI_ROLE_SLAVE))
2260         {
2261
2262 #if BTM_SCO_INCLUDED == TRUE
2263             /* The LMP_switch_req shall be sent only if the ACL logical transport
2264             is in active mode, when encryption is disabled, and all synchronous
2265             logical transports on the same physical link are disabled." */
2266
2267             /* Check if there is any SCO Active on this BD Address */
2268             is_sco_active = btm_is_sco_active_by_bdaddr(p_lcb_cur->remote_bd_addr);
2269
2270             L2CAP_TRACE_API ("l2cu_create_conn - btm_is_sco_active_by_bdaddr() is_sco_active = %s", \
2271                 (is_sco_active == TRUE) ? "TRUE":"FALSE");
2272
2273             if (is_sco_active == TRUE)
2274                 continue; /* No Master Slave switch not allowed when SCO Active */
2275 #endif
2276             /*4_1_TODO check  if btm_cb.devcb.local_features to be used instead */
2277             if (HCI_SWITCH_SUPPORTED(BTM_ReadLocalFeatures()))
2278             {
2279                 /* mark this lcb waiting for switch to be completed and
2280                    start switch on the other one */
2281                 p_lcb->link_state = LST_CONNECTING_WAIT_SWITCH;
2282                 p_lcb->link_role  = HCI_ROLE_MASTER;
2283
2284                 if (BTM_SwitchRole (p_lcb_cur->remote_bd_addr, HCI_ROLE_MASTER, NULL) == BTM_CMD_STARTED)
2285                 {
2286                     btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_ROLE_SWITCH_TOUT);
2287                     return (TRUE);
2288                 }
2289             }
2290         }
2291     }
2292
2293     p_lcb->link_state = LST_CONNECTING;
2294
2295     return (l2cu_create_conn_after_switch (p_lcb));
2296 }
2297
2298 /*******************************************************************************
2299 **
2300 ** Function         l2cu_get_num_hi_priority
2301 **
2302 ** Description      Gets the number of high priority channels.
2303 **
2304 ** Returns
2305 **
2306 *******************************************************************************/
2307 UINT8 l2cu_get_num_hi_priority (void)
2308 {
2309     UINT8       no_hi = 0;
2310     int         xx;
2311     tL2C_LCB    *p_lcb = &l2cb.lcb_pool[0];
2312
2313     for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
2314     {
2315         if ((p_lcb->in_use) && (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH))
2316         {
2317             no_hi++;
2318         }
2319     }
2320     return no_hi;
2321 }
2322
2323
2324 /*******************************************************************************
2325 **
2326 ** Function         l2cu_create_conn_after_switch
2327 **
2328 ** Description      This function initiates an acl connection via HCI
2329 **                  If switch required to create connection it is already done.
2330 **
2331 ** Returns          TRUE if successful, FALSE if gki get buffer fails.
2332 **
2333 *******************************************************************************/
2334
2335 BOOLEAN l2cu_create_conn_after_switch (tL2C_LCB *p_lcb)
2336 {
2337     UINT8            allow_switch = HCI_CR_CONN_ALLOW_SWITCH;
2338     tBTM_INQ_INFO    *p_inq_info;
2339     UINT8            page_scan_rep_mode;
2340     UINT8            page_scan_mode;
2341     UINT16           clock_offset;
2342     UINT8            *p_features;
2343     UINT16           num_acl = BTM_GetNumAclLinks();
2344     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_lcb->remote_bd_addr);
2345     UINT8            no_hi_prio_chs = l2cu_get_num_hi_priority();
2346
2347     p_features = BTM_ReadLocalFeatures();
2348
2349     L2CAP_TRACE_DEBUG ("l2cu_create_conn_after_switch :%d num_acl:%d no_hi: %d is_bonding:%d",
2350         l2cb.disallow_switch, num_acl, no_hi_prio_chs, p_lcb->is_bonding);
2351     /* FW team says that we can participant in 4 piconets
2352      * typically 3 piconet + 1 for scanning.
2353      * We can enhance the code to count the number of piconets later. */
2354     if ( ((!l2cb.disallow_switch && (num_acl < 3)) || (p_lcb->is_bonding && (no_hi_prio_chs==0)))
2355         && HCI_SWITCH_SUPPORTED(p_features))
2356         allow_switch = HCI_CR_CONN_ALLOW_SWITCH;
2357     else
2358         allow_switch = HCI_CR_CONN_NOT_ALLOW_SWITCH;
2359
2360     p_lcb->link_state = LST_CONNECTING;
2361
2362
2363 #if (TCS_WUG_MEMBER_INCLUDED == TRUE && TCS_INCLUDED == TRUE)
2364     if ( (clock_offset = tcs_wug_get_clk_offset( p_lcb->remote_bd_addr )) != 0 )
2365     {
2366         page_scan_rep_mode = HCI_PAGE_SCAN_REP_MODE_R0;
2367         page_scan_mode = HCI_MANDATARY_PAGE_SCAN_MODE;
2368     }
2369     else
2370     {
2371 #endif
2372
2373         /* Check with the BT manager if details about remote device are known */
2374         if ((p_inq_info = BTM_InqDbRead(p_lcb->remote_bd_addr)) != NULL)
2375         {
2376             page_scan_rep_mode = p_inq_info->results.page_scan_rep_mode;
2377             page_scan_mode = p_inq_info->results.page_scan_mode;
2378             clock_offset = (UINT16)(p_inq_info->results.clock_offset);
2379         }
2380         else
2381         {
2382             /* No info known. Use default settings */
2383             page_scan_rep_mode = HCI_PAGE_SCAN_REP_MODE_R1;
2384             page_scan_mode = HCI_MANDATARY_PAGE_SCAN_MODE;
2385
2386             clock_offset = (p_dev_rec) ? p_dev_rec->clock_offset : 0;
2387         }
2388 #if (TCS_WUG_MEMBER_INCLUDED == TRUE && TCS_INCLUDED == TRUE)
2389     }
2390 #endif
2391
2392     if (!btsnd_hcic_create_conn (p_lcb->remote_bd_addr,
2393                                  ( HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1
2394                                  | HCI_PKT_TYPES_MASK_DM3 | HCI_PKT_TYPES_MASK_DH3
2395                                  | HCI_PKT_TYPES_MASK_DM5 | HCI_PKT_TYPES_MASK_DH5 ),
2396                                  page_scan_rep_mode,
2397                                  page_scan_mode,
2398                                  clock_offset,
2399                                  allow_switch))
2400
2401     {
2402         L2CAP_TRACE_ERROR ("L2CAP - no buffer for l2cu_create_conn");
2403         l2cu_release_lcb (p_lcb);
2404         return (FALSE);
2405     }
2406
2407 #if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2408     btm_acl_update_busy_level (BTM_BLI_PAGE_EVT);
2409 #endif
2410
2411     btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK,
2412                      L2CAP_LINK_CONNECT_TOUT);
2413
2414     return (TRUE);
2415 }
2416
2417
2418 /*******************************************************************************
2419 **
2420 ** Function         l2cu_find_lcb_by_state
2421 **
2422 ** Description      Look through all active LCBs for a match based on the
2423 **                  LCB state.
2424 **
2425 ** Returns          pointer to first matched LCB, or NULL if no match
2426 **
2427 *******************************************************************************/
2428 tL2C_LCB *l2cu_find_lcb_by_state (tL2C_LINK_STATE state)
2429 {
2430     UINT16      i;
2431     tL2C_LCB    *p_lcb = &l2cb.lcb_pool[0];
2432
2433     for (i = 0; i < MAX_L2CAP_LINKS; i++, p_lcb++)
2434     {
2435         if ((p_lcb->in_use) && (p_lcb->link_state == state))
2436         {
2437             return (p_lcb);
2438         }
2439     }
2440
2441     /* If here, no match found */
2442     return (NULL);
2443 }
2444
2445
2446 /*******************************************************************************
2447 **
2448 ** Function         l2cu_lcb_disconnecting
2449 **
2450 ** Description      On each active lcb, check if the lcb is in disconnecting
2451 **                  state, or if there are no ccb's on the lcb (implying
2452                     idle timeout is running), or if last ccb on the link
2453                     is in disconnecting state.
2454 **
2455 ** Returns          TRUE if any of above conditions met, FALSE otherwise
2456 **
2457 *******************************************************************************/
2458 BOOLEAN l2cu_lcb_disconnecting (void)
2459 {
2460     tL2C_LCB    *p_lcb;
2461     tL2C_CCB    *p_ccb;
2462     UINT16      i;
2463     BOOLEAN     status = FALSE;
2464
2465     p_lcb = &l2cb.lcb_pool[0];
2466
2467     for (i = 0; i < MAX_L2CAP_LINKS; i++, p_lcb++)
2468     {
2469         if (p_lcb->in_use)
2470         {
2471             /* no ccbs on lcb, or lcb is in disconnecting state */
2472             if ((!p_lcb->ccb_queue.p_first_ccb) || (p_lcb->link_state == LST_DISCONNECTING))
2473             {
2474                 status = TRUE;
2475                 break;
2476             }
2477             /* only one ccb left on lcb */
2478             else if (p_lcb->ccb_queue.p_first_ccb == p_lcb->ccb_queue.p_last_ccb)
2479             {
2480                 p_ccb = p_lcb->ccb_queue.p_first_ccb;
2481
2482                 if ((p_ccb->in_use) &&
2483                     ((p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP) ||
2484                      (p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP)))
2485                 {
2486                     status = TRUE;
2487                     break;
2488                 }
2489             }
2490         }
2491     }
2492     return status;
2493 }
2494
2495
2496 /*******************************************************************************
2497 **
2498 ** Function         l2cu_set_acl_priority
2499 **
2500 ** Description      Sets the transmission priority for a channel.
2501 **                  (For initial implementation only two values are valid.
2502 **                  L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
2503 **
2504 ** Returns          TRUE if a valid channel, else FALSE
2505 **
2506 *******************************************************************************/
2507
2508 BOOLEAN l2cu_set_acl_priority (BD_ADDR bd_addr, UINT8 priority, BOOLEAN reset_after_rs)
2509 {
2510     tL2C_LCB            *p_lcb;
2511     UINT8               *pp;
2512     UINT8                command[HCI_BRCM_ACL_PRIORITY_PARAM_SIZE];
2513     UINT8                vs_param;
2514
2515     APPL_TRACE_EVENT("SET ACL PRIORITY %d", priority);
2516
2517     /* Find the link control block for the acl channel */
2518     if ((p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
2519     {
2520         L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_SetAclPriority");
2521         return (FALSE);
2522     }
2523
2524     if (BTM_IS_BRCM_CONTROLLER())
2525     {
2526         /* Called from above L2CAP through API; send VSC if changed */
2527         if ((!reset_after_rs && (priority != p_lcb->acl_priority)) ||
2528               /* Called because of a master/slave role switch; if high resend VSC */
2529             ( reset_after_rs && p_lcb->acl_priority == L2CAP_PRIORITY_HIGH))
2530         {
2531             pp = command;
2532
2533             vs_param = (priority == L2CAP_PRIORITY_HIGH) ? HCI_BRCM_ACL_PRIORITY_HIGH : HCI_BRCM_ACL_PRIORITY_LOW;
2534
2535             UINT16_TO_STREAM (pp, p_lcb->handle);
2536             UINT8_TO_STREAM  (pp, vs_param);
2537
2538             BTM_VendorSpecificCommand (HCI_BRCM_SET_ACL_PRIORITY, HCI_BRCM_ACL_PRIORITY_PARAM_SIZE, command, NULL);
2539
2540             /* Adjust lmp buffer allocation for this channel if priority changed */
2541             if (p_lcb->acl_priority != priority)
2542             {
2543                 p_lcb->acl_priority = priority;
2544                 l2c_link_adjust_allocation();
2545             }
2546         }
2547     }
2548     return(TRUE);
2549 }
2550
2551 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
2552 /******************************************************************************
2553 **
2554 ** Function         l2cu_set_non_flushable_pbf
2555 **
2556 ** Description      set L2CAP_PKT_START_NON_FLUSHABLE if controller supoorts
2557 **
2558 ** Returns          void
2559 **
2560 *******************************************************************************/
2561 void l2cu_set_non_flushable_pbf (BOOLEAN is_supported)
2562 {
2563     if (is_supported)
2564         l2cb.non_flushable_pbf = (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT);
2565     else
2566         l2cb.non_flushable_pbf = (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
2567 }
2568 #endif
2569
2570 /*******************************************************************************
2571 **
2572 ** Function         l2cu_resubmit_pending_sec_req
2573 **
2574 ** Description      This function is called when required security procedures
2575 **                  are completed and any pending requests can be re-submitted.
2576 **
2577 ** Returns          void
2578 **
2579 *******************************************************************************/
2580 void l2cu_resubmit_pending_sec_req (BD_ADDR p_bda)
2581 {
2582     tL2C_LCB        *p_lcb;
2583     tL2C_CCB        *p_ccb;
2584     tL2C_CCB        *p_next_ccb;
2585     int             xx;
2586
2587     L2CAP_TRACE_DEBUG ("l2cu_resubmit_pending_sec_req  p_bda: 0x%08x", p_bda);
2588
2589     /* If we are called with a BDA, only resubmit for that BDA */
2590     if (p_bda)
2591     {
2592         p_lcb = l2cu_find_lcb_by_bd_addr (p_bda, BT_TRANSPORT_BR_EDR);
2593
2594         /* If we don't have one, this is an error */
2595         if (p_lcb)
2596         {
2597             /* For all channels, send the event through their FSMs */
2598             for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_next_ccb)
2599             {
2600                 p_next_ccb = p_ccb->p_next_ccb;
2601                 l2c_csm_execute (p_ccb, L2CEVT_SEC_RE_SEND_CMD, NULL);
2602             }
2603         }
2604         else
2605         {
2606             L2CAP_TRACE_WARNING ("l2cu_resubmit_pending_sec_req - unknown BD_ADDR");
2607         }
2608     }
2609     else
2610     {
2611         /* No BDA pasesed in, so check all links */
2612         for (xx = 0, p_lcb = &l2cb.lcb_pool[0]; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
2613         {
2614             if (p_lcb->in_use)
2615             {
2616                 /* For all channels, send the event through their FSMs */
2617                 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_next_ccb)
2618                 {
2619                     p_next_ccb = p_ccb->p_next_ccb;
2620                     l2c_csm_execute (p_ccb, L2CEVT_SEC_RE_SEND_CMD, NULL);
2621                 }
2622             }
2623         }
2624     }
2625 }
2626
2627 #if L2CAP_CONFORMANCE_TESTING == TRUE
2628 /*******************************************************************************
2629 **
2630 ** Function         l2cu_set_info_rsp_mask
2631 **
2632 ** Description      This function allows the script wrapper to change the
2633 **                  info resp mask for conformance testing.
2634 **
2635 ** Returns          pointer to CCB, or NULL if none
2636 **
2637 *******************************************************************************/
2638 void l2cu_set_info_rsp_mask (UINT32 mask)
2639 {
2640     l2cb.test_info_resp = mask;
2641 }
2642 #endif  /* L2CAP_CONFORMANCE_TESTING */
2643
2644 /*******************************************************************************
2645 **
2646 ** Function         l2cu_adjust_out_mps
2647 **
2648 ** Description      Sets our MPS based on current controller capabilities
2649 **
2650 ** Returns          void
2651 **
2652 *******************************************************************************/
2653 void l2cu_adjust_out_mps (tL2C_CCB *p_ccb)
2654 {
2655     UINT16 packet_size;
2656
2657     /* on the tx side MTU is selected based on packet size of the controller */
2658     packet_size = btm_get_max_packet_size (p_ccb->p_lcb->remote_bd_addr);
2659
2660     if (packet_size <= (L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD + L2CAP_SDU_LEN_OVERHEAD + L2CAP_FCS_LEN))
2661     {
2662         /* something is very wrong */
2663         L2CAP_TRACE_ERROR ("l2cu_adjust_out_mps bad packet size: %u  will use MPS: %u", packet_size, p_ccb->peer_cfg.fcr.mps);
2664         p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps;
2665     }
2666     else
2667     {
2668         packet_size -= (L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD + L2CAP_SDU_LEN_OVERHEAD + L2CAP_FCS_LEN);
2669
2670         /* We try to negotiate MTU that each packet can be split into whole
2671         number of max packets.  For example if link is 1.2 max packet size is 339 bytes.
2672         At first calculate how many whole packets it is.  MAX L2CAP is 1691 + 4 overhead.
2673         1695, that will be 5 Dh5 packets.  Now maximum L2CAP packet is
2674         5 * 339 = 1695. Minus 4 bytes L2CAP header 1691.
2675
2676         For EDR 2.0 packet size is 1027.  So we better send RFCOMM packet as 1 3DH5 packet
2677         1 * 1027 = 1027.  Minus 4 bytes L2CAP header 1023.  */
2678         if (p_ccb->peer_cfg.fcr.mps >= packet_size)
2679             p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps / packet_size * packet_size;
2680         else
2681             p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps;
2682
2683         L2CAP_TRACE_DEBUG ("l2cu_adjust_out_mps use %d   Based on peer_cfg.fcr.mps: %u  packet_size: %u",
2684                             p_ccb->tx_mps, p_ccb->peer_cfg.fcr.mps, packet_size);
2685     }
2686 }
2687
2688
2689 /*******************************************************************************
2690 **
2691 ** Function         l2cu_initialize_fixed_ccb
2692 **
2693 ** Description      Initialize a fixed channel's CCB
2694 **
2695 ** Returns          TRUE or FALSE
2696 **
2697 *******************************************************************************/
2698 BOOLEAN l2cu_initialize_fixed_ccb (tL2C_LCB *p_lcb, UINT16 fixed_cid, tL2CAP_FCR_OPTS *p_fcr)
2699 {
2700 #if (L2CAP_NUM_FIXED_CHNLS > 0)
2701     tL2C_CCB    *p_ccb;
2702
2703     /* If we already have a CCB, then simply return */
2704     if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] != NULL)
2705         return (TRUE);
2706
2707     if ((p_ccb = l2cu_allocate_ccb (NULL, 0)) == NULL)
2708         return (FALSE);
2709
2710     btu_stop_timer(&p_lcb->timer_entry);
2711
2712     /* Set CID for the connection */
2713     p_ccb->local_cid  = fixed_cid;
2714     p_ccb->remote_cid = fixed_cid;
2715
2716     GKI_init_q (&p_ccb->xmit_hold_q);
2717
2718     p_ccb->is_flushable = FALSE;
2719
2720     p_ccb->timer_entry.param  = (TIMER_PARAM_TYPE)p_ccb;
2721
2722
2723     if (p_fcr)
2724     {
2725         /* Set the FCR parameters. For now, we will use default pools */
2726         p_ccb->our_cfg.fcr = p_ccb->peer_cfg.fcr = *p_fcr;
2727
2728         p_ccb->ertm_info.fcr_rx_pool_id  = HCI_ACL_POOL_ID;
2729         p_ccb->ertm_info.fcr_tx_pool_id  = HCI_ACL_POOL_ID;
2730         p_ccb->ertm_info.user_rx_pool_id = HCI_ACL_POOL_ID;
2731         p_ccb->ertm_info.user_tx_pool_id = HCI_ACL_POOL_ID;
2732
2733         p_ccb->fcrb.max_held_acks = p_fcr->tx_win_sz / 3;
2734     }
2735
2736     /* Link ccb to lcb and lcb to ccb */
2737     p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = p_ccb;
2738     p_ccb->p_lcb = p_lcb;
2739
2740     /* There is no configuration, so if the link is up, the channel is up */
2741     if (p_lcb->link_state == LST_CONNECTED)
2742         p_ccb->chnl_state = CST_OPEN;
2743
2744     /* Set the default idle timeout value to use */
2745     p_ccb->fixed_chnl_idle_tout = l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].default_idle_tout;
2746 #endif
2747     return (TRUE);
2748 }
2749
2750 /*******************************************************************************
2751 **
2752 ** Function         l2cu_no_dynamic_ccbs
2753 **
2754 ** Description      Handles the case when there are no more dynamic CCBs. If there
2755 **                  are any fixed CCBs, start the longest of the fixed CCB timeouts,
2756 **                  otherwise start the default link idle timeout or disconnect.
2757 **
2758 ** Returns          void
2759 **
2760 *******************************************************************************/
2761 void l2cu_no_dynamic_ccbs (tL2C_LCB *p_lcb)
2762 {
2763     tBTM_STATUS     rc;
2764     UINT16          timeout = p_lcb->idle_timeout;
2765
2766 #if (L2CAP_NUM_FIXED_CHNLS > 0)
2767     int         xx;
2768
2769     for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++)
2770     {
2771         if ( (p_lcb->p_fixed_ccbs[xx] != NULL) && (p_lcb->p_fixed_ccbs[xx]->fixed_chnl_idle_tout > timeout) )
2772             timeout = p_lcb->p_fixed_ccbs[xx]->fixed_chnl_idle_tout;
2773     }
2774 #endif
2775
2776     /* If the link is pairing, do not mess with the timeouts */
2777     if (p_lcb->is_bonding)
2778         return;
2779
2780     if (timeout == 0)
2781     {
2782         L2CAP_TRACE_DEBUG ("l2cu_no_dynamic_ccbs() IDLE timer 0, disconnecting link");
2783
2784         rc = btm_sec_disconnect (p_lcb->handle, HCI_ERR_PEER_USER);
2785         if (rc == BTM_CMD_STARTED)
2786         {
2787             l2cu_process_fixed_disc_cback(p_lcb);
2788             p_lcb->link_state = LST_DISCONNECTING;
2789             timeout = L2CAP_LINK_DISCONNECT_TOUT;
2790         }
2791         else if (rc == BTM_SUCCESS)
2792         {
2793             l2cu_process_fixed_disc_cback(p_lcb);
2794             /* BTM SEC will make sure that link is release (probably after pairing is done) */
2795             p_lcb->link_state = LST_DISCONNECTING;
2796             timeout = 0xFFFF;
2797         }
2798         else if ( (p_lcb->is_bonding)
2799             &&   (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER)) )
2800         {
2801             l2cu_process_fixed_disc_cback(p_lcb);
2802             p_lcb->link_state = LST_DISCONNECTING;
2803             timeout = L2CAP_LINK_DISCONNECT_TOUT;
2804         }
2805         else
2806         {
2807             /* probably no buffer to send disconnect */
2808             timeout = BT_1SEC_TIMEOUT;
2809         }
2810     }
2811
2812     if (timeout != 0xFFFF)
2813     {
2814         L2CAP_TRACE_DEBUG ("l2cu_no_dynamic_ccbs() starting IDLE timeout: %d", timeout);
2815         btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, timeout);
2816     }
2817     else
2818     {
2819         btu_stop_timer(&p_lcb->timer_entry);
2820     }
2821 }
2822
2823 #if (L2CAP_NUM_FIXED_CHNLS > 0)
2824 /*******************************************************************************
2825 **
2826 ** Function         l2cu_process_fixed_chnl_resp
2827 **
2828 ** Description      handle a fixed channel response (or lack thereof)
2829 **                  if the link failed, or a fixed channel response was
2830 **                  not received, the bitfield is all zeros.
2831 **
2832 *******************************************************************************/
2833 void l2cu_process_fixed_chnl_resp (tL2C_LCB *p_lcb)
2834 {
2835     int     xx;
2836 #if (BLE_INCLUDED == TRUE)
2837     /* always exclude LE fixed channel on BR/EDR fix channel capability */
2838     if (p_lcb->transport == BT_TRANSPORT_BR_EDR)
2839         p_lcb->peer_chnl_mask[0] &= ~(L2CAP_FIXED_CHNL_ATT_BIT| \
2840                                       L2CAP_FIXED_CHNL_BLE_SIG_BIT| \
2841                                       L2CAP_FIXED_CHNL_SMP_BIT);
2842 #endif
2843
2844     /* Tell all registered fixed channels about the connection */
2845     for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++)
2846     {
2847 #if BLE_INCLUDED == TRUE
2848         /* skip sending LE fix channel callbacks on BR/EDR links */
2849         if (p_lcb->transport == BT_TRANSPORT_BR_EDR &&
2850             xx + L2CAP_FIRST_FIXED_CHNL >= L2CAP_ATT_CID &&
2851             xx + L2CAP_FIRST_FIXED_CHNL <= L2CAP_SMP_CID)
2852             continue;
2853 #endif
2854         if (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL)
2855         {
2856             if (p_lcb->peer_chnl_mask[0] & (1 << (xx + L2CAP_FIRST_FIXED_CHNL)))
2857             {
2858                 if (p_lcb->p_fixed_ccbs[xx])
2859                     p_lcb->p_fixed_ccbs[xx]->chnl_state = CST_OPEN;
2860 #if BLE_INCLUDED == TRUE
2861                 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, TRUE, 0, p_lcb->transport);
2862 #else
2863                 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, TRUE, 0, BT_TRANSPORT_BR_EDR);
2864 #endif
2865             }
2866             else
2867             {
2868 #if BLE_INCLUDED == TRUE
2869                 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
2870 #else
2871                 (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
2872 #endif
2873
2874                 if (p_lcb->p_fixed_ccbs[xx])
2875                 {
2876                     l2cu_release_ccb (p_lcb->p_fixed_ccbs[xx]);
2877                     p_lcb->p_fixed_ccbs[xx] = NULL;
2878                 }
2879             }
2880         }
2881     }
2882 }
2883 #endif
2884
2885
2886 /*******************************************************************************
2887 **
2888 ** Function         l2cu_process_fixed_disc_cback
2889 **
2890 ** Description      send l2cap fixed channel disconnection callback to application
2891 **
2892 **
2893 ** Returns          void
2894 **
2895 *******************************************************************************/
2896 void l2cu_process_fixed_disc_cback (tL2C_LCB *p_lcb)
2897 {
2898 #if (L2CAP_NUM_FIXED_CHNLS > 0)
2899     int         xx;
2900
2901     for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++)
2902     {
2903         if (p_lcb->p_fixed_ccbs[xx])
2904         {
2905             if (p_lcb->p_fixed_ccbs[xx] != p_lcb->p_pending_ccb)
2906             {
2907                 l2cu_release_ccb (p_lcb->p_fixed_ccbs[xx]);
2908                 p_lcb->p_fixed_ccbs[xx] = NULL;
2909 #if BLE_INCLUDED == TRUE
2910             (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
2911 #else
2912             (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
2913 #endif
2914            }
2915         }
2916         else if ( (p_lcb->peer_chnl_mask[0] & (1 << (xx + L2CAP_FIRST_FIXED_CHNL)))
2917                && (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL) )
2918 #if BLE_INCLUDED == TRUE
2919             (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
2920 #else
2921             (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
2922 #endif
2923     }
2924 #endif
2925 }
2926
2927 #if (BLE_INCLUDED == TRUE)
2928 /*******************************************************************************
2929 **
2930 ** Function         l2cu_send_peer_ble_par_req
2931 **
2932 ** Description      Build and send a BLE parameter update request message
2933 **                  to the peer.
2934 **
2935 ** Returns          void
2936 **
2937 *******************************************************************************/
2938 void l2cu_send_peer_ble_par_req (tL2C_LCB *p_lcb, UINT16 min_int, UINT16 max_int, UINT16 latency, UINT16 timeout)
2939 {
2940     BT_HDR  *p_buf;
2941     UINT8   *p;
2942
2943     /* Create an identifier for this packet */
2944     p_lcb->id++;
2945     l2cu_adj_id (p_lcb, L2CAP_ADJ_ID);
2946
2947     if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_UPD_REQ_LEN, L2CAP_CMD_BLE_UPDATE_REQ, p_lcb->id)) == NULL )
2948     {
2949         L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_par_req - no buffer");
2950         return;
2951     }
2952
2953     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
2954                                L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
2955
2956     UINT16_TO_STREAM (p, min_int);
2957     UINT16_TO_STREAM (p, max_int);
2958     UINT16_TO_STREAM (p, latency);
2959     UINT16_TO_STREAM (p, timeout);
2960
2961     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
2962 }
2963
2964 /*******************************************************************************
2965 **
2966 ** Function         l2cu_send_peer_ble_par_rsp
2967 **
2968 ** Description      Build and send a BLE parameter update response message
2969 **                  to the peer.
2970 **
2971 ** Returns          void
2972 **
2973 *******************************************************************************/
2974 void l2cu_send_peer_ble_par_rsp (tL2C_LCB *p_lcb, UINT16 reason, UINT8 rem_id)
2975 {
2976     BT_HDR  *p_buf;
2977     UINT8   *p;
2978
2979     if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_UPD_RSP_LEN, L2CAP_CMD_BLE_UPDATE_RSP, rem_id)) == NULL )
2980     {
2981         L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_par_rsp - no buffer");
2982         return;
2983     }
2984
2985     p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
2986                                L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
2987
2988     UINT16_TO_STREAM (p, reason);
2989
2990     l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
2991 }
2992
2993 #endif /* BLE_INCLUDED == TRUE */
2994
2995
2996 /*******************************************************************************
2997 ** Functions used by both Full and Light Stack
2998 ********************************************************************************/
2999
3000 /*******************************************************************************
3001 **
3002 ** Function         l2cu_find_lcb_by_handle
3003 **
3004 ** Description      Look through all active LCBs for a match based on the
3005 **                  HCI handle.
3006 **
3007 ** Returns          pointer to matched LCB, or NULL if no match
3008 **
3009 *******************************************************************************/
3010 tL2C_LCB  *l2cu_find_lcb_by_handle (UINT16 handle)
3011 {
3012     int         xx;
3013     tL2C_LCB    *p_lcb = &l2cb.lcb_pool[0];
3014
3015     for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++)
3016     {
3017         if ((p_lcb->in_use) && (p_lcb->handle == handle))
3018         {
3019             return (p_lcb);
3020         }
3021     }
3022
3023     /* If here, no match found */
3024     return (NULL);
3025 }
3026
3027 /*******************************************************************************
3028 **
3029 ** Function         l2cu_find_ccb_by_cid
3030 **
3031 ** Description      Look through all active CCBs on a link for a match based
3032 **                  on the local CID. If passed the link pointer is NULL, all
3033 **                  active links are searched.
3034 **
3035 ** Returns          pointer to matched CCB, or NULL if no match
3036 **
3037 *******************************************************************************/
3038 tL2C_CCB *l2cu_find_ccb_by_cid (tL2C_LCB *p_lcb, UINT16 local_cid)
3039 {
3040     tL2C_CCB    *p_ccb = NULL;
3041 #if (L2CAP_UCD_INCLUDED == TRUE)
3042     UINT8 xx;
3043 #endif
3044
3045     if (local_cid >= L2CAP_BASE_APPL_CID)
3046     {
3047         /* find the associated CCB by "index" */
3048         local_cid -= L2CAP_BASE_APPL_CID;
3049
3050         if (local_cid >= MAX_L2CAP_CHANNELS)
3051             return NULL;
3052
3053         p_ccb = l2cb.ccb_pool + local_cid;
3054
3055         /* make sure the CCB is in use */
3056         if (!p_ccb->in_use)
3057         {
3058             p_ccb = NULL;
3059         }
3060         /* make sure it's for the same LCB */
3061         else if (p_lcb && p_lcb != p_ccb->p_lcb)
3062         {
3063             p_ccb = NULL;
3064         }
3065     }
3066 #if (L2CAP_UCD_INCLUDED == TRUE)
3067     else
3068     {
3069         /* searching fixed channel */
3070         p_ccb = l2cb.ccb_pool;
3071         for ( xx = 0; xx < MAX_L2CAP_CHANNELS; xx++ )
3072         {
3073             if ((p_ccb->local_cid == local_cid)
3074               &&(p_ccb->in_use)
3075               &&(p_lcb == p_ccb->p_lcb))
3076                 break;
3077             else
3078                 p_ccb++;
3079         }
3080         if ( xx >= MAX_L2CAP_CHANNELS )
3081             return NULL;
3082     }
3083 #endif
3084
3085     return (p_ccb);
3086 }
3087
3088 #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
3089
3090 /******************************************************************************
3091 **
3092 ** Function         l2cu_get_next_channel_in_rr
3093 **
3094 ** Description      get the next channel to send on a link. It also adjusts the
3095 **                  CCB queue to do a basic priority and round-robin scheduling.
3096 **
3097 ** Returns          pointer to CCB or NULL
3098 **
3099 *******************************************************************************/
3100 static tL2C_CCB *l2cu_get_next_channel_in_rr(tL2C_LCB *p_lcb)
3101 {
3102     tL2C_CCB    *p_serve_ccb = NULL;
3103     tL2C_CCB    *p_ccb;
3104
3105     int i, j;
3106
3107     /* scan all of priority until finding a channel to serve */
3108     for ( i = 0; (i < L2CAP_NUM_CHNL_PRIORITY)&&(!p_serve_ccb); i++ )
3109     {
3110         /* scan all channel within serving priority group until finding a channel to serve */
3111         for ( j = 0; (j < p_lcb->rr_serv[p_lcb->rr_pri].num_ccb)&&(!p_serve_ccb); j++)
3112         {
3113             /* scaning from next serving channel */
3114             p_ccb = p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb;
3115
3116             if (!p_ccb)
3117             {
3118                 L2CAP_TRACE_ERROR("p_serve_ccb is NULL, rr_pri=%d", p_lcb->rr_pri);
3119                 return NULL;
3120             }
3121
3122             L2CAP_TRACE_DEBUG("RR scan pri=%d, lcid=0x%04x, q_cout=%d",
3123                                 p_ccb->ccb_priority, p_ccb->local_cid, p_ccb->xmit_hold_q.count );
3124
3125             /* store the next serving channel */
3126             /* this channel is the last channel of its priority group */
3127             if (( p_ccb->p_next_ccb == NULL )
3128               ||( p_ccb->p_next_ccb->ccb_priority != p_ccb->ccb_priority ))
3129             {
3130                 /* next serving channel is set to the first channel in the group */
3131                 p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb = p_lcb->rr_serv[p_lcb->rr_pri].p_first_ccb;
3132             }
3133             else
3134             {
3135                 /* next serving channel is set to the next channel in the group */
3136                 p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb = p_ccb->p_next_ccb;
3137             }
3138
3139             if (p_ccb->chnl_state != CST_OPEN)
3140                 continue;
3141
3142             /* eL2CAP option in use */
3143             if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
3144             {
3145                 if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy)
3146                     continue;
3147
3148                 if ( p_ccb->fcrb.retrans_q.count == 0 )
3149                 {
3150                     if ( p_ccb->xmit_hold_q.count == 0 )
3151                         continue;
3152
3153                     /* If using the common pool, should be at least 10% free. */
3154                     if ( (p_ccb->ertm_info.fcr_tx_pool_id == HCI_ACL_POOL_ID) && (GKI_poolutilization (HCI_ACL_POOL_ID) > 90) )
3155                         continue;
3156
3157                     /* If in eRTM mode, check for window closure */
3158                     if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) )
3159                         continue;
3160                 }
3161             }
3162             else
3163             {
3164                 if (p_ccb->xmit_hold_q.count == 0)
3165                     continue;
3166             }
3167
3168             /* found a channel to serve */
3169             p_serve_ccb = p_ccb;
3170             /* decrease quota of its priority group */
3171             p_lcb->rr_serv[p_lcb->rr_pri].quota--;
3172         }
3173
3174         /* if there is no more quota of the priority group or no channel to have data to send */
3175         if ((p_lcb->rr_serv[p_lcb->rr_pri].quota == 0)||(!p_serve_ccb))
3176         {
3177             /* serve next priority group */
3178             p_lcb->rr_pri = (p_lcb->rr_pri + 1) % L2CAP_NUM_CHNL_PRIORITY;
3179             /* initialize its quota */
3180             p_lcb->rr_serv[p_lcb->rr_pri].quota = L2CAP_GET_PRIORITY_QUOTA(p_lcb->rr_pri);
3181         }
3182     }
3183
3184     if (p_serve_ccb)
3185     {
3186         L2CAP_TRACE_DEBUG("RR service pri=%d, quota=%d, lcid=0x%04x",
3187                             p_serve_ccb->ccb_priority,
3188                             p_lcb->rr_serv[p_serve_ccb->ccb_priority].quota,
3189                             p_serve_ccb->local_cid );
3190     }
3191
3192     return p_serve_ccb;
3193 }
3194
3195 #else /* (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE) */
3196
3197 /******************************************************************************
3198 **
3199 ** Function         l2cu_get_next_channel
3200 **
3201 ** Description      get the next channel to send on a link bassed on priority
3202 **                  scheduling.
3203 **
3204 ** Returns          pointer to CCB or NULL
3205 **
3206 *******************************************************************************/
3207 static tL2C_CCB *l2cu_get_next_channel(tL2C_LCB *p_lcb)
3208 {
3209     tL2C_CCB    *p_ccb;
3210
3211     /* Get the first CCB with data to send.
3212     */
3213     for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
3214     {
3215         if (p_ccb->chnl_state != CST_OPEN)
3216             continue;
3217
3218         if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy)
3219             continue;
3220
3221         if (p_ccb->fcrb.retrans_q.count != 0)
3222             return p_ccb;
3223
3224         if (p_ccb->xmit_hold_q.count == 0)
3225             continue;
3226
3227         /* If using the common pool, should be at least 10% free. */
3228         if ( (p_ccb->ertm_info.fcr_tx_pool_id == HCI_ACL_POOL_ID) && (GKI_poolutilization (HCI_ACL_POOL_ID) > 90) )
3229             continue;
3230
3231         /* If in eRTM mode, check for window closure */
3232         if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) )
3233             continue;
3234
3235         /* If here, we found someone */
3236         return p_ccb;
3237     }
3238
3239     return NULL;
3240 }
3241 #endif /* (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE) */
3242
3243 /******************************************************************************
3244 **
3245 ** Function         l2cu_get_next_buffer_to_send
3246 **
3247 ** Description      get the next buffer to send on a link. It also adjusts the
3248 **                  CCB queue to do a basic priority and round-robin scheduling.
3249 **
3250 ** Returns          pointer to buffer or NULL
3251 **
3252 *******************************************************************************/
3253 BT_HDR *l2cu_get_next_buffer_to_send (tL2C_LCB *p_lcb)
3254 {
3255     tL2C_CCB    *p_ccb;
3256     BT_HDR      *p_buf;
3257
3258     /* Highest priority are fixed channels */
3259 #if (L2CAP_NUM_FIXED_CHNLS > 0)
3260     int         xx;
3261
3262     for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++)
3263     {
3264         if ((p_ccb = p_lcb->p_fixed_ccbs[xx]) == NULL)
3265             continue;
3266
3267         /* eL2CAP option in use */
3268         if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
3269         {
3270             if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy)
3271                 continue;
3272
3273             /* No more checks needed if sending from the reatransmit queue */
3274             if (p_ccb->fcrb.retrans_q.count == 0)
3275             {
3276                 if (p_ccb->xmit_hold_q.count == 0)
3277                     continue;
3278
3279                 /* If using the common pool, should be at least 10% free. */
3280                 if ( (p_ccb->ertm_info.fcr_tx_pool_id == HCI_ACL_POOL_ID) && (GKI_poolutilization (HCI_ACL_POOL_ID) > 90) )
3281                     continue;
3282
3283                 /* If in eRTM mode, check for window closure */
3284                 if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) )
3285                     continue;
3286             }
3287
3288             if ((p_buf = l2c_fcr_get_next_xmit_sdu_seg(p_ccb, 0)) != NULL)
3289             {
3290                 l2cu_check_channel_congestion (p_ccb);
3291                 l2cu_set_acl_hci_header (p_buf, p_ccb);
3292                 return (p_buf);
3293             }
3294         }
3295         else
3296         {
3297             if (p_ccb->xmit_hold_q.count != 0)
3298             {
3299                 p_buf = (BT_HDR *)GKI_dequeue (&p_ccb->xmit_hold_q);
3300                 if(NULL == p_buf)
3301                 {
3302                     L2CAP_TRACE_ERROR("l2cu_get_buffer_to_send: No data to be sent");
3303                     return (NULL);
3304                 }
3305                 l2cu_check_channel_congestion (p_ccb);
3306                 l2cu_set_acl_hci_header (p_buf, p_ccb);
3307                 return (p_buf);
3308             }
3309         }
3310     }
3311 #endif
3312
3313 #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
3314     /* get next serving channel in round-robin */
3315     p_ccb  = l2cu_get_next_channel_in_rr( p_lcb );
3316 #else
3317     p_ccb  = l2cu_get_next_channel( p_lcb );
3318 #endif
3319
3320     /* Return if no buffer */
3321     if (p_ccb == NULL)
3322         return (NULL);
3323
3324     if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
3325     {
3326         if ((p_buf = l2c_fcr_get_next_xmit_sdu_seg(p_ccb, 0)) == NULL)
3327             return (NULL);
3328     }
3329     else
3330     {
3331         p_buf = (BT_HDR *)GKI_dequeue (&p_ccb->xmit_hold_q);
3332         if(NULL == p_buf)
3333         {
3334             L2CAP_TRACE_ERROR("l2cu_get_buffer_to_send() #2: No data to be sent");
3335             return (NULL);
3336         }
3337     }
3338
3339     if ( p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_TxComplete_Cb && (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) )
3340         (*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, 1);
3341
3342
3343     l2cu_check_channel_congestion (p_ccb);
3344
3345     l2cu_set_acl_hci_header (p_buf, p_ccb);
3346
3347     return (p_buf);
3348 }
3349
3350 /******************************************************************************
3351 **
3352 ** Function         l2cu_set_acl_hci_header
3353 **
3354 ** Description      Set HCI handle for ACL packet
3355 **
3356 ** Returns          None
3357 **
3358 *******************************************************************************/
3359 void l2cu_set_acl_hci_header (BT_HDR *p_buf, tL2C_CCB *p_ccb)
3360 {
3361     UINT8       *p;
3362
3363     /* Set the pointer to the beginning of the data minus 4 bytes for the packet header */
3364     p = (UINT8 *)(p_buf + 1) + p_buf->offset - HCI_DATA_PREAMBLE_SIZE;
3365
3366 #if (BLE_INCLUDED == TRUE)
3367     if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE)
3368     {
3369         UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT));
3370
3371         /* The HCI transport will segment the buffers. */
3372         if (p_buf->len > btu_cb.hcit_ble_acl_data_size)
3373         {
3374             UINT16_TO_STREAM (p, btu_cb.hcit_ble_acl_data_size);
3375         }
3376         else
3377         {
3378             UINT16_TO_STREAM (p, p_buf->len);
3379         }
3380     } /* (BLE_INCLUDED == TRUE) */
3381     else
3382 #endif
3383     {
3384 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
3385         if ( (((p_buf->layer_specific & L2CAP_FLUSHABLE_MASK) == L2CAP_FLUSHABLE_CH_BASED) && (p_ccb->is_flushable))
3386                 || ((p_buf->layer_specific & L2CAP_FLUSHABLE_MASK) == L2CAP_FLUSHABLE_PKT) )
3387         {
3388             UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT));
3389         }
3390         else
3391         {
3392             UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | l2cb.non_flushable_pbf);
3393         }
3394 #else
3395         UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT));
3396 #endif
3397
3398         /* The HCI transport will segment the buffers. */
3399         if (p_buf->len > btu_cb.hcit_acl_data_size)
3400         {
3401             UINT16_TO_STREAM (p, btu_cb.hcit_acl_data_size);
3402         }
3403         else
3404         {
3405             UINT16_TO_STREAM (p, p_buf->len);
3406         }
3407     }
3408     p_buf->offset -= HCI_DATA_PREAMBLE_SIZE;
3409     p_buf->len    += HCI_DATA_PREAMBLE_SIZE;
3410 }
3411
3412 /******************************************************************************
3413 **
3414 ** Function         l2cu_check_channel_congestion
3415 **
3416 ** Description      check if any change in congestion status
3417 **
3418 ** Returns          None
3419 **
3420 *******************************************************************************/
3421 void l2cu_check_channel_congestion (tL2C_CCB *p_ccb)
3422 {
3423     UINT16 q_count = p_ccb->xmit_hold_q.count;
3424
3425 #if (L2CAP_UCD_INCLUDED == TRUE)
3426     if ( p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID )
3427     {
3428         q_count += p_ccb->p_lcb->ucd_out_sec_pending_q.count;
3429     }
3430 #endif
3431
3432     /* If the CCB queue limit is subject to a quota, check for congestion */
3433
3434     /* if this channel has outgoing traffic */
3435     if (p_ccb->buff_quota != 0)
3436     {
3437         /* If this channel was congested */
3438         if ( p_ccb->cong_sent )
3439         {
3440             /* If the channel is not congested now, tell the app */
3441             if (q_count <= (p_ccb->buff_quota / 2))
3442             {
3443                 p_ccb->cong_sent = FALSE;
3444                 if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb)
3445                 {
3446                     L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (FALSE), CID: 0x%04x  xmit_hold_q.count: %u  buff_quota: %u",
3447                                       p_ccb->local_cid, q_count, p_ccb->buff_quota);
3448
3449                     /* Prevent recursive calling */
3450                     l2cb.is_cong_cback_context = TRUE;
3451                     (*p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb)(p_ccb->local_cid, FALSE);
3452                     l2cb.is_cong_cback_context = FALSE;
3453                 }
3454 #if (L2CAP_UCD_INCLUDED == TRUE)
3455                 else if ( p_ccb->p_rcb && p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID )
3456                 {
3457                     if ( p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb )
3458                     {
3459                         L2CAP_TRACE_DEBUG ("L2CAP - Calling UCD CongestionStatus_Cb (FALSE), SecPendingQ:%u,XmitQ:%u,Quota:%u",
3460                                              p_ccb->p_lcb->ucd_out_sec_pending_q.count,
3461                                              p_ccb->xmit_hold_q.count, p_ccb->buff_quota);
3462                         p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb( p_ccb->p_lcb->remote_bd_addr, FALSE );
3463                     }
3464                 }
3465 #endif
3466 #if (L2CAP_NUM_FIXED_CHNLS > 0)
3467                 else
3468                 {
3469                     UINT8 xx;
3470                     for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx ++)
3471                     {
3472                         if (p_ccb->p_lcb->p_fixed_ccbs[xx] == p_ccb)
3473                         {
3474                             if (l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb != NULL)
3475                                 (* l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb)(p_ccb->p_lcb->remote_bd_addr, FALSE);
3476                             break;
3477                         }
3478                     }
3479                 }
3480 #endif
3481             }
3482         }
3483         else
3484         {
3485             /* If this channel was not congested but it is congested now, tell the app */
3486             if (q_count > p_ccb->buff_quota)
3487             {
3488                 p_ccb->cong_sent = TRUE;
3489                 if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb)
3490                 {
3491                     L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (TRUE),CID:0x%04x,XmitQ:%u,Quota:%u",
3492                         p_ccb->local_cid, q_count, p_ccb->buff_quota);
3493
3494                     (*p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb)(p_ccb->local_cid, TRUE);
3495                 }
3496 #if (L2CAP_UCD_INCLUDED == TRUE)
3497                 else if ( p_ccb->p_rcb && p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID )
3498                 {
3499                     if ( p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb )
3500                     {
3501                         L2CAP_TRACE_DEBUG ("L2CAP - Calling UCD CongestionStatus_Cb (TRUE), SecPendingQ:%u,XmitQ:%u,Quota:%u",
3502                                              p_ccb->p_lcb->ucd_out_sec_pending_q.count,
3503                                              p_ccb->xmit_hold_q.count, p_ccb->buff_quota);
3504                         p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb( p_ccb->p_lcb->remote_bd_addr, TRUE );
3505                     }
3506                 }
3507 #endif
3508 #if (L2CAP_NUM_FIXED_CHNLS > 0)
3509                 else
3510                 {
3511                     UINT8 xx;
3512                     for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx ++)
3513                     {
3514                         if (p_ccb->p_lcb->p_fixed_ccbs[xx] == p_ccb)
3515                         {
3516                             if (l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb != NULL)
3517                                 (* l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb)(p_ccb->p_lcb->remote_bd_addr, TRUE);
3518                             break;
3519                         }
3520                     }
3521                 }
3522 #endif
3523             }
3524         }
3525     }
3526 }
3527