1 /* $DragonFly: src/sys/netbt/hci_link.c,v 1.2 2008/03/18 13:41:42 hasso Exp $ */
2 /* $OpenBSD: src/sys/netbt/hci_link.c,v 1.7 2008/02/24 21:34:48 uwe Exp $ */
3 /* $NetBSD: hci_link.c,v 1.16 2007/11/10 23:12:22 plunky Exp $ */
6 * Copyright (c) 2005 Iain Hibbert.
7 * Copyright (c) 2006 Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <sys/systm.h>
42 #include <sys/endian.h>
43 #include <sys/callout.h>
45 #include <net/pf/pfvar.h>
48 #include <netbt/bluetooth.h>
49 #include <netbt/hci.h>
50 #include <netbt/l2cap.h>
51 #include <netbt/sco.h>
53 /*******************************************************************************
59 * Automatically expire unused ACL connections after this number of
60 * seconds (if zero, do not expire unused connections) [sysctl]
62 int hci_acl_expiry
= 10; /* seconds */
65 * hci_acl_open(unit, bdaddr)
67 * open ACL connection to remote bdaddr. Only one ACL connection is permitted
68 * between any two Bluetooth devices, so we look for an existing one before
69 * trying to start a new one.
72 hci_acl_open(struct hci_unit
*unit
, bdaddr_t
*bdaddr
)
74 struct hci_link
*link
;
75 struct hci_memo
*memo
;
79 KKASSERT(unit
!= NULL
);
80 KKASSERT(bdaddr
!= NULL
);
82 link
= hci_link_lookup_bdaddr(unit
, bdaddr
, HCI_LINK_ACL
);
84 link
= hci_link_alloc(unit
);
88 link
->hl_type
= HCI_LINK_ACL
;
89 bdaddr_copy(&link
->hl_bdaddr
, bdaddr
);
92 switch(link
->hl_state
) {
95 * open connection to remote device
97 memset(&cp
, 0, sizeof(cp
));
98 bdaddr_copy(&cp
.bdaddr
, bdaddr
);
99 cp
.pkt_type
= htole16(unit
->hci_packet_type
);
101 memo
= hci_memo_find(unit
, bdaddr
);
103 cp
.page_scan_rep_mode
= memo
->page_scan_rep_mode
;
104 cp
.page_scan_mode
= memo
->page_scan_mode
;
105 cp
.clock_offset
= memo
->clock_offset
;
108 if (unit
->hci_link_policy
& HCI_LINK_POLICY_ENABLE_ROLE_SWITCH
)
109 cp
.accept_role_switch
= 1;
111 err
= hci_send_cmd(unit
, HCI_CMD_CREATE_CON
, &cp
, sizeof(cp
));
113 hci_link_free(link
, err
);
117 link
->hl_state
= HCI_LINK_WAIT_CONNECT
;
120 case HCI_LINK_WAIT_CONNECT
:
121 case HCI_LINK_WAIT_AUTH
:
122 case HCI_LINK_WAIT_ENCRYPT
:
123 case HCI_LINK_WAIT_SECURE
:
125 * somebody else already trying to connect, we just
126 * sit on the bench with them..
132 * If already open, halt any expiry callouts. We dont need
133 * to care about already invoking callouts since refcnt >0
134 * will keep the link alive.
136 callout_stop(&link
->hl_expire
);
140 UNKNOWN(link
->hl_state
);
151 * Close ACL connection. When there are no more references to this link,
152 * we can either close it down or schedule a delayed closedown.
155 hci_acl_close(struct hci_link
*link
, int err
)
157 KKASSERT(link
!= NULL
);
159 if (--link
->hl_refcnt
== 0) {
160 if (link
->hl_state
== HCI_LINK_CLOSED
)
161 hci_link_free(link
, err
);
162 else if (hci_acl_expiry
> 0)
163 callout_reset(&link
->hl_expire
, hci_acl_expiry
* hz
,
164 hci_acl_timeout
, link
);
169 * Incoming ACL connection.
171 * For now, we accept all connections but it would be better to check
172 * the L2CAP listen list and only accept when there is a listener
175 * There should not be a link to the same bdaddr already, we check
176 * anyway though its left unhandled for now.
179 hci_acl_newconn(struct hci_unit
*unit
, bdaddr_t
*bdaddr
)
181 struct hci_link
*link
;
183 link
= hci_link_lookup_bdaddr(unit
, bdaddr
, HCI_LINK_ACL
);
187 link
= hci_link_alloc(unit
);
189 link
->hl_state
= HCI_LINK_WAIT_CONNECT
;
190 link
->hl_type
= HCI_LINK_ACL
;
191 bdaddr_copy(&link
->hl_bdaddr
, bdaddr
);
193 if (hci_acl_expiry
> 0)
194 callout_reset(&link
->hl_expire
, hci_acl_expiry
* hz
,
195 hci_acl_timeout
, link
);
202 hci_acl_timeout(void *arg
)
204 struct hci_link
*link
= arg
;
210 if (link
->hl_refcnt
> 0)
213 DPRINTF("link #%d expired\n", link
->hl_handle
);
215 switch (link
->hl_state
) {
216 case HCI_LINK_CLOSED
:
217 case HCI_LINK_WAIT_CONNECT
:
218 hci_link_free(link
, ECONNRESET
);
221 case HCI_LINK_WAIT_AUTH
:
222 case HCI_LINK_WAIT_ENCRYPT
:
223 case HCI_LINK_WAIT_SECURE
:
225 cp
.con_handle
= htole16(link
->hl_handle
);
226 cp
.reason
= 0x13; /* "Remote User Terminated Connection" */
228 err
= hci_send_cmd(link
->hl_unit
, HCI_CMD_DISCONNECT
,
232 DPRINTF("error %d sending HCI_CMD_DISCONNECT\n",
239 UNKNOWN(link
->hl_state
);
248 * Initiate any Link Mode change requests.
251 hci_acl_setmode(struct hci_link
*link
)
255 KKASSERT(link
!= NULL
);
256 KKASSERT(link
->hl_unit
!= NULL
);
258 if (link
->hl_state
!= HCI_LINK_OPEN
)
261 if ((link
->hl_flags
& HCI_LINK_AUTH_REQ
)
262 && !(link
->hl_flags
& HCI_LINK_AUTH
)) {
265 DPRINTF("(%s) requesting auth for handle #%d\n",
266 device_get_nameunit(link
->hl_unit
->hci_dev
),
269 link
->hl_state
= HCI_LINK_WAIT_AUTH
;
270 cp
.con_handle
= htole16(link
->hl_handle
);
271 err
= hci_send_cmd(link
->hl_unit
, HCI_CMD_AUTH_REQ
,
274 return (err
== 0 ? EINPROGRESS
: err
);
277 if ((link
->hl_flags
& HCI_LINK_ENCRYPT_REQ
)
278 && !(link
->hl_flags
& HCI_LINK_ENCRYPT
)) {
279 hci_set_con_encryption_cp cp
;
281 /* XXX we should check features for encryption capability */
283 DPRINTF("(%s) requesting encryption for handle #%d\n",
284 device_get_nameunit(link
->hl_unit
->hci_dev
),
287 link
->hl_state
= HCI_LINK_WAIT_ENCRYPT
;
288 cp
.con_handle
= htole16(link
->hl_handle
);
289 cp
.encryption_enable
= 0x01;
291 err
= hci_send_cmd(link
->hl_unit
, HCI_CMD_SET_CON_ENCRYPTION
,
294 return (err
== 0 ? EINPROGRESS
: err
);
297 if ((link
->hl_flags
& HCI_LINK_SECURE_REQ
)) {
298 hci_change_con_link_key_cp cp
;
300 /* always change link key for SECURE requests */
301 link
->hl_flags
&= ~HCI_LINK_SECURE
;
303 DPRINTF("(%s) changing link key for handle #%d\n",
304 device_get_nameunit(link
->hl_unit
->hci_dev
),
307 link
->hl_state
= HCI_LINK_WAIT_SECURE
;
308 cp
.con_handle
= htole16(link
->hl_handle
);
310 err
= hci_send_cmd(link
->hl_unit
, HCI_CMD_CHANGE_CON_LINK_KEY
,
313 return (err
== 0 ? EINPROGRESS
: err
);
322 * This is called from event handlers when the mode change
323 * is complete. We notify upstream and restart the link.
326 hci_acl_linkmode(struct hci_link
*link
)
328 struct l2cap_channel
*chan
, *next
;
331 DPRINTF("(%s) handle #%d, auth %s, encrypt %s, secure %s\n",
332 device_get_nameunit(link
->hl_unit
->hci_dev
), link
->hl_handle
,
333 (link
->hl_flags
& HCI_LINK_AUTH
? "on" : "off"),
334 (link
->hl_flags
& HCI_LINK_ENCRYPT
? "on" : "off"),
335 (link
->hl_flags
& HCI_LINK_SECURE
? "on" : "off"));
337 if (link
->hl_flags
& HCI_LINK_AUTH
)
338 mode
|= L2CAP_LM_AUTH
;
340 if (link
->hl_flags
& HCI_LINK_ENCRYPT
)
341 mode
|= L2CAP_LM_ENCRYPT
;
343 if (link
->hl_flags
& HCI_LINK_SECURE
)
344 mode
|= L2CAP_LM_SECURE
;
347 * The link state will only be OPEN here if the mode change
348 * was successful. So, we can proceed with L2CAP connections,
349 * or notify already establshed channels, to allow any that
350 * are dissatisfied to disconnect before we restart.
352 next
= LIST_FIRST(&l2cap_active_list
);
353 while ((chan
= next
) != NULL
) {
354 next
= LIST_NEXT(chan
, lc_ncid
);
356 if (chan
->lc_link
!= link
)
359 switch(chan
->lc_state
) {
360 case L2CAP_WAIT_SEND_CONNECT_REQ
: /* we are connecting */
361 if ((mode
& chan
->lc_mode
) != chan
->lc_mode
) {
362 l2cap_close(chan
, ECONNABORTED
);
366 chan
->lc_state
= L2CAP_WAIT_RECV_CONNECT_RSP
;
367 err
= l2cap_send_connect_req(chan
);
369 l2cap_close(chan
, err
);
374 case L2CAP_WAIT_SEND_CONNECT_RSP
: /* they are connecting */
375 if ((mode
& chan
->lc_mode
) != chan
->lc_mode
) {
376 l2cap_send_connect_rsp(link
, chan
->lc_ident
,
378 L2CAP_SECURITY_BLOCK
);
380 l2cap_close(chan
, ECONNABORTED
);
384 l2cap_send_connect_rsp(link
, chan
->lc_ident
,
385 chan
->lc_lcid
, chan
->lc_rcid
,
388 chan
->lc_state
= L2CAP_WAIT_CONFIG
;
389 chan
->lc_flags
|= (L2CAP_WAIT_CONFIG_RSP
| L2CAP_WAIT_CONFIG_REQ
);
390 err
= l2cap_send_config_req(chan
);
392 l2cap_close(chan
, err
);
397 case L2CAP_WAIT_RECV_CONNECT_RSP
:
398 case L2CAP_WAIT_CONFIG
:
399 case L2CAP_OPEN
: /* already established */
400 (*chan
->lc_proto
->linkmode
)(chan
->lc_upper
, mode
);
408 link
->hl_state
= HCI_LINK_OPEN
;
415 * we accumulate packet fragments on the hci_link structure
416 * until a full L2CAP frame is ready, then send it on.
419 hci_acl_recv(struct mbuf
*m
, struct hci_unit
*unit
)
421 struct hci_link
*link
;
422 hci_acldata_hdr_t hdr
;
423 uint16_t handle
, want
;
427 KKASSERT(unit
!= NULL
);
429 KKASSERT(m
->m_pkthdr
.len
>= sizeof(hdr
));
430 m_copydata(m
, 0, sizeof(hdr
), (caddr_t
)&hdr
);
431 m_adj(m
, sizeof(hdr
));
434 if (hdr
.type
!= HCI_ACL_DATA_PKT
) {
435 kprintf("%s: bad ACL packet type\n",
436 device_get_nameunit(unit
->hci_dev
));
440 if (m
->m_pkthdr
.len
!= letoh16(hdr
.length
)) {
441 kprintf("%s: bad ACL packet length (%d != %d)\n",
442 device_get_nameunit(unit
->hci_dev
), m
->m_pkthdr
.len
,
443 letoh16(hdr
.length
));
448 hdr
.length
= letoh16(hdr
.length
);
449 hdr
.con_handle
= letoh16(hdr
.con_handle
);
450 handle
= HCI_CON_HANDLE(hdr
.con_handle
);
451 pb
= HCI_PB_FLAG(hdr
.con_handle
);
453 link
= hci_link_lookup_handle(unit
, handle
);
457 DPRINTF("%s: dumping packet for unknown handle #%d\n",
458 device_get_nameunit(unit
->hci_dev
), handle
);
461 * There is no way to find out what this connection handle is
462 * for, just get rid of it. This may happen, if a USB dongle
463 * is plugged into a self powered hub and does not reset when
464 * the system is shut down.
466 cp
.con_handle
= htole16(handle
);
467 cp
.reason
= 0x13; /* "Remote User Terminated Connection" */
468 hci_send_cmd(unit
, HCI_CMD_DISCONNECT
, &cp
, sizeof(cp
));
473 case HCI_PACKET_START
:
474 if (link
->hl_rxp
!= NULL
)
475 kprintf("%s: dropped incomplete ACL packet\n",
476 device_get_nameunit(unit
->hci_dev
));
478 if (m
->m_pkthdr
.len
< sizeof(l2cap_hdr_t
)) {
479 kprintf("%s: short ACL packet\n",
480 device_get_nameunit(unit
->hci_dev
));
486 got
= m
->m_pkthdr
.len
;
489 case HCI_PACKET_FRAGMENT
:
490 if (link
->hl_rxp
== NULL
) {
491 kprintf("%s: unexpected packet fragment\n",
492 device_get_nameunit(unit
->hci_dev
));
497 got
= m
->m_pkthdr
.len
+ link
->hl_rxp
->m_pkthdr
.len
;
498 m_cat(link
->hl_rxp
, m
);
500 m
->m_pkthdr
.len
= got
;
504 kprintf("%s: unknown packet type\n",
505 device_get_nameunit(unit
->hci_dev
));
510 m_copydata(m
, 0, sizeof(want
), (caddr_t
)&want
);
511 want
= letoh16(want
) + sizeof(l2cap_hdr_t
) - got
;
519 l2cap_recv_frame(m
, link
);
528 * Send ACL data on link
530 * We must fragment packets into chunks of less than unit->hci_max_acl_size and
531 * prepend a relevant ACL header to each fragment. We keep a PDU structure
532 * attached to the link, so that completed fragments can be marked off and
533 * more data requested from above once the PDU is sent.
536 hci_acl_send(struct mbuf
*m
, struct hci_link
*link
,
537 struct l2cap_channel
*chan
)
539 struct l2cap_pdu
*pdu
;
540 struct mbuf
*n
= NULL
;
541 int plen
, mlen
, num
= 0;
543 KKASSERT(link
!= NULL
);
545 KKASSERT(m
->m_flags
& M_PKTHDR
);
546 KKASSERT(m
->m_pkthdr
.len
> 0);
548 if (link
->hl_state
== HCI_LINK_CLOSED
) {
553 pdu
= pool_get(&l2cap_pdu_pool
, PR_NOWAIT
);
557 bzero(pdu
, sizeof *pdu
);
561 plen
= m
->m_pkthdr
.len
;
562 mlen
= link
->hl_unit
->hci_max_acl_size
;
564 DPRINTFN(5, "%s: handle #%d, plen = %d, max = %d\n",
565 device_get_nameunit(link
->hl_unit
->hci_dev
),
566 link
->hl_handle
, plen
, mlen
);
570 n
= m_split(m
, mlen
, MB_DONTWAIT
);
578 m
->m_flags
|= M_PROTO1
; /* tag first fragment */
580 DPRINTFN(10, "(%s) chunk of %d (plen = %d) bytes\n",
581 device_get_nameunit(link
->hl_unit
->hci_dev
), mlen
, plen
);
582 IF_ENQUEUE(&pdu
->lp_data
, m
);
587 TAILQ_INSERT_TAIL(&link
->hl_txq
, pdu
, lp_next
);
588 link
->hl_txqlen
+= num
;
597 IF_DRAIN(&pdu
->lp_data
);
598 pool_put(&l2cap_pdu_pool
, pdu
);
605 * Start sending ACL data on link.
607 * This is called when the queue may need restarting: as new data
608 * is queued, after link mode changes have completed, or when device
609 * buffers have cleared.
611 * We may use all the available packet slots. The reason that we add
612 * the ACL encapsulation here rather than in hci_acl_send() is that L2CAP
613 * signal packets may be queued before the handle is given to us..
616 hci_acl_start(struct hci_link
*link
)
618 struct hci_unit
*unit
;
619 hci_acldata_hdr_t
*hdr
;
620 struct l2cap_pdu
*pdu
;
624 KKASSERT(link
!= NULL
);
626 unit
= link
->hl_unit
;
627 KKASSERT(unit
!= NULL
);
629 /* this is mainly to block ourselves (below) */
630 if (link
->hl_state
!= HCI_LINK_OPEN
)
633 if (link
->hl_txqlen
== 0 || unit
->hci_num_acl_pkts
== 0)
636 /* find first PDU with data to send */
637 pdu
= TAILQ_FIRST(&link
->hl_txq
);
642 if (!IF_QEMPTY(&pdu
->lp_data
))
645 pdu
= TAILQ_NEXT(pdu
, lp_next
);
648 while (unit
->hci_num_acl_pkts
> 0) {
649 IF_DEQUEUE(&pdu
->lp_data
, m
);
652 if (m
->m_flags
& M_PROTO1
)
653 handle
= HCI_MK_CON_HANDLE(link
->hl_handle
,
654 HCI_PACKET_START
, 0);
656 handle
= HCI_MK_CON_HANDLE(link
->hl_handle
,
657 HCI_PACKET_FRAGMENT
, 0);
659 M_PREPEND(m
, sizeof(*hdr
), MB_DONTWAIT
);
663 hdr
= mtod(m
, hci_acldata_hdr_t
*);
664 hdr
->type
= HCI_ACL_DATA_PKT
;
665 hdr
->con_handle
= htole16(handle
);
666 hdr
->length
= htole16(m
->m_pkthdr
.len
- sizeof(*hdr
));
671 hci_output_acl(unit
, m
);
673 if (IF_QEMPTY(&pdu
->lp_data
)) {
676 * This should enable streaming of PDUs - when
677 * we have placed all the fragments on the acl
678 * output queue, we trigger the L2CAP layer to
679 * send us down one more. Use a false state so
680 * we dont run into ourselves coming back from
683 link
->hl_state
= HCI_LINK_BLOCK
;
684 l2cap_start(pdu
->lp_chan
);
685 link
->hl_state
= HCI_LINK_OPEN
;
688 pdu
= TAILQ_NEXT(pdu
, lp_next
);
695 * We had our turn now, move to the back of the queue to let
696 * other links have a go at the output buffers..
698 if (TAILQ_NEXT(link
, hl_next
)) {
699 TAILQ_REMOVE(&unit
->hci_links
, link
, hl_next
);
700 TAILQ_INSERT_TAIL(&unit
->hci_links
, link
, hl_next
);
705 * Confirm ACL packets cleared from Controller buffers. We scan our PDU
706 * list to clear pending fragments and signal upstream for more data
707 * when a PDU is complete.
710 hci_acl_complete(struct hci_link
*link
, int num
)
712 struct l2cap_pdu
*pdu
;
713 struct l2cap_channel
*chan
;
715 DPRINTFN(5, "(%s) handle #%d (%d)\n",
716 device_get_nameunit(link
->hl_unit
->hci_dev
), link
->hl_handle
, num
);
719 pdu
= TAILQ_FIRST(&link
->hl_txq
);
721 kprintf("%s: %d packets completed on handle #%x "
722 "but none pending!\n",
723 device_get_nameunit(link
->hl_unit
->hci_dev
),
724 num
, link
->hl_handle
);
728 if (num
>= pdu
->lp_pending
) {
729 num
-= pdu
->lp_pending
;
732 if (IF_QEMPTY(&pdu
->lp_data
)) {
733 TAILQ_REMOVE(&link
->hl_txq
, pdu
, lp_next
);
737 (*chan
->lc_proto
->complete
)
740 if (chan
->lc_pending
== 0)
744 pool_put(&l2cap_pdu_pool
, pdu
);
747 pdu
->lp_pending
-= num
;
753 /*******************************************************************************
755 * HCI SCO Connections
759 * Incoming SCO Connection. We check the list for anybody willing
763 hci_sco_newconn(struct hci_unit
*unit
, bdaddr_t
*bdaddr
)
765 struct sockaddr_bt laddr
, raddr
;
766 struct sco_pcb
*pcb
, *new;
767 struct hci_link
*sco
, *acl
;
769 memset(&laddr
, 0, sizeof(laddr
));
770 laddr
.bt_len
= sizeof(laddr
);
771 laddr
.bt_family
= AF_BLUETOOTH
;
772 bdaddr_copy(&laddr
.bt_bdaddr
, &unit
->hci_bdaddr
);
774 memset(&raddr
, 0, sizeof(raddr
));
775 raddr
.bt_len
= sizeof(raddr
);
776 raddr
.bt_family
= AF_BLUETOOTH
;
777 bdaddr_copy(&raddr
.bt_bdaddr
, bdaddr
);
780 * There should already be an ACL link up and running before
781 * the controller sends us SCO connection requests, but you
784 acl
= hci_link_lookup_bdaddr(unit
, bdaddr
, HCI_LINK_ACL
);
785 if (acl
== NULL
|| acl
->hl_state
!= HCI_LINK_OPEN
)
788 LIST_FOREACH(pcb
, &sco_pcb
, sp_next
) {
789 if ((pcb
->sp_flags
& SP_LISTENING
) == 0)
792 new = (*pcb
->sp_proto
->newconn
)(pcb
->sp_upper
, &laddr
, &raddr
);
797 * Ok, got new pcb so we can start a new link and fill
798 * in all the details.
800 bdaddr_copy(&new->sp_laddr
, &unit
->hci_bdaddr
);
801 bdaddr_copy(&new->sp_raddr
, bdaddr
);
803 sco
= hci_link_alloc(unit
);
809 sco
->hl_type
= HCI_LINK_SCO
;
810 bdaddr_copy(&sco
->hl_bdaddr
, bdaddr
);
812 sco
->hl_link
= hci_acl_open(unit
, bdaddr
);
813 KKASSERT(sco
->hl_link
== acl
);
818 new->sp_mtu
= unit
->hci_max_sco_size
;
826 * receive SCO packet, we only need to strip the header and send
827 * it to the right handler
830 hci_sco_recv(struct mbuf
*m
, struct hci_unit
*unit
)
832 struct hci_link
*link
;
833 hci_scodata_hdr_t hdr
;
837 KKASSERT(unit
!= NULL
);
839 KKASSERT(m
->m_pkthdr
.len
>= sizeof(hdr
));
840 m_copydata(m
, 0, sizeof(hdr
), (caddr_t
)&hdr
);
841 m_adj(m
, sizeof(hdr
));
844 if (hdr
.type
!= HCI_SCO_DATA_PKT
) {
845 kprintf("%s: bad SCO packet type\n",
846 device_get_nameunit(unit
->hci_dev
));
850 if (m
->m_pkthdr
.len
!= hdr
.length
) {
851 kprintf("%s: bad SCO packet length (%d != %d)\n",
852 device_get_nameunit(unit
->hci_dev
), m
->m_pkthdr
.len
,
858 hdr
.con_handle
= letoh16(hdr
.con_handle
);
859 handle
= HCI_CON_HANDLE(hdr
.con_handle
);
861 link
= hci_link_lookup_handle(unit
, handle
);
862 if (link
== NULL
|| link
->hl_type
== HCI_LINK_ACL
) {
863 DPRINTF("%s: dumping packet for unknown handle #%d\n",
864 device_get_nameunit(unit
->hci_dev
), handle
);
869 (*link
->hl_sco
->sp_proto
->input
)(link
->hl_sco
->sp_upper
, m
);
877 hci_sco_start(struct hci_link
*link
)
882 * SCO packets have completed at the controller, so we can
883 * signal up to free the buffer space.
886 hci_sco_complete(struct hci_link
*link
, int num
)
889 DPRINTFN(5, "handle #%d (num=%d)\n", link
->hl_handle
, num
);
890 link
->hl_sco
->sp_pending
--;
891 (*link
->hl_sco
->sp_proto
->complete
)(link
->hl_sco
->sp_upper
, num
);
894 /*******************************************************************************
896 * Generic HCI Connection alloc/free/lookup etc
900 hci_link_alloc(struct hci_unit
*unit
)
902 struct hci_link
*link
;
904 KKASSERT(unit
!= NULL
);
906 link
= kmalloc(sizeof *link
, M_BLUETOOTH
, M_NOWAIT
| M_ZERO
);
910 link
->hl_unit
= unit
;
911 link
->hl_state
= HCI_LINK_CLOSED
;
913 /* init ACL portion */
914 callout_init(&link
->hl_expire
);
917 TAILQ_INIT(&link
->hl_txq
); /* outgoing packets */
918 TAILQ_INIT(&link
->hl_reqs
); /* request queue */
920 link
->hl_mtu
= L2CAP_MTU_DEFAULT
; /* L2CAP signal mtu */
921 link
->hl_flush
= L2CAP_FLUSH_TIMO_DEFAULT
; /* flush timeout */
923 /* init SCO portion */
924 /* &link->hl_data is already zero-initialized. */
927 TAILQ_INSERT_HEAD(&unit
->hci_links
, link
, hl_next
);
933 hci_link_free(struct hci_link
*link
, int err
)
935 struct l2cap_req
*req
;
936 struct l2cap_pdu
*pdu
;
937 struct l2cap_channel
*chan
, *next
;
939 KKASSERT(link
!= NULL
);
941 DPRINTF("(%s) #%d, type = %d, state = %d, refcnt = %d\n",
942 device_get_nameunit(link
->hl_unit
->hci_dev
), link
->hl_handle
,
943 link
->hl_type
, link
->hl_state
, link
->hl_refcnt
);
945 /* ACL reference count */
946 if (link
->hl_refcnt
> 0) {
947 next
= LIST_FIRST(&l2cap_active_list
);
948 while ((chan
= next
) != NULL
) {
949 next
= LIST_NEXT(chan
, lc_ncid
);
950 if (chan
->lc_link
== link
)
951 l2cap_close(chan
, err
);
954 KKASSERT(link
->hl_refcnt
== 0);
956 /* ACL L2CAP requests.. */
957 while ((req
= TAILQ_FIRST(&link
->hl_reqs
)) != NULL
)
958 l2cap_request_free(req
);
960 KKASSERT(TAILQ_EMPTY(&link
->hl_reqs
));
962 /* ACL outgoing data queue */
963 while ((pdu
= TAILQ_FIRST(&link
->hl_txq
)) != NULL
) {
964 TAILQ_REMOVE(&link
->hl_txq
, pdu
, lp_next
);
965 IF_DRAIN(&pdu
->lp_data
);
967 link
->hl_unit
->hci_num_acl_pkts
+= pdu
->lp_pending
;
969 pool_put(&l2cap_pdu_pool
, pdu
);
972 KKASSERT(TAILQ_EMPTY(&link
->hl_txq
));
974 /* ACL incoming data packet */
975 if (link
->hl_rxp
!= NULL
) {
976 m_freem(link
->hl_rxp
);
980 /* SCO master ACL link */
981 if (link
->hl_link
!= NULL
) {
982 hci_acl_close(link
->hl_link
, err
);
983 link
->hl_link
= NULL
;
987 if (link
->hl_sco
!= NULL
) {
993 (*pcb
->sp_proto
->disconnected
)(pcb
->sp_upper
, err
);
996 /* flush any SCO data */
998 IF_DRAIN(&link
->hl_data
);
1002 * Halt the timeout - if its already running we cannot free the
1003 * link structure but the timeout function will call us back in
1006 link
->hl_state
= HCI_LINK_CLOSED
;
1007 callout_stop(&link
->hl_expire
);
1008 if (callout_active(&link
->hl_expire
))
1012 * If we made a note of clock offset, keep it in a memo
1013 * to facilitate reconnections to this device
1015 if (link
->hl_clock
!= 0) {
1016 struct hci_memo
*memo
;
1018 memo
= hci_memo_new(link
->hl_unit
, &link
->hl_bdaddr
);
1020 memo
->clock_offset
= link
->hl_clock
;
1024 TAILQ_REMOVE(&link
->hl_unit
->hci_links
, link
, hl_next
);
1026 kfree(link
, M_BLUETOOTH
);
1030 * Lookup HCI link by type and state.
1033 hci_link_lookup_state(struct hci_unit
*unit
, uint16_t type
, uint16_t state
)
1035 struct hci_link
*link
;
1037 TAILQ_FOREACH(link
, &unit
->hci_links
, hl_next
) {
1038 if (link
->hl_type
== type
&& link
->hl_state
== state
)
1046 * Lookup HCI link by address and type. Note that for SCO links there may
1047 * be more than one link per address, so we only return links with no
1048 * handle (ie new links)
1051 hci_link_lookup_bdaddr(struct hci_unit
*unit
, bdaddr_t
*bdaddr
, uint16_t type
)
1053 struct hci_link
*link
;
1055 KKASSERT(unit
!= NULL
);
1056 KKASSERT(bdaddr
!= NULL
);
1058 TAILQ_FOREACH(link
, &unit
->hci_links
, hl_next
) {
1059 if (link
->hl_type
!= type
)
1062 if (type
== HCI_LINK_SCO
&& link
->hl_handle
!= 0)
1065 if (bdaddr_same(&link
->hl_bdaddr
, bdaddr
))
1073 hci_link_lookup_handle(struct hci_unit
*unit
, uint16_t handle
)
1075 struct hci_link
*link
;
1077 KKASSERT(unit
!= NULL
);
1079 TAILQ_FOREACH(link
, &unit
->hci_links
, hl_next
) {
1080 if (handle
== link
->hl_handle
)